xref: /freebsd/sys/contrib/openzfs/module/zfs/dbuf.c (revision bb2d13b686e3ccf6c3ccb36209dfb7dcc108b182)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy /*
22eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23eda14cbcSMatt Macy  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
247877fdebSMatt Macy  * Copyright (c) 2012, 2020 by Delphix. All rights reserved.
25eda14cbcSMatt Macy  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26eda14cbcSMatt Macy  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27eda14cbcSMatt Macy  * Copyright (c) 2019, Klara Inc.
28eda14cbcSMatt Macy  * Copyright (c) 2019, Allan Jude
29eda14cbcSMatt Macy  */
30eda14cbcSMatt Macy 
31eda14cbcSMatt Macy #include <sys/zfs_context.h>
32eda14cbcSMatt Macy #include <sys/arc.h>
33eda14cbcSMatt Macy #include <sys/dmu.h>
34eda14cbcSMatt Macy #include <sys/dmu_send.h>
35eda14cbcSMatt Macy #include <sys/dmu_impl.h>
36eda14cbcSMatt Macy #include <sys/dbuf.h>
37eda14cbcSMatt Macy #include <sys/dmu_objset.h>
38eda14cbcSMatt Macy #include <sys/dsl_dataset.h>
39eda14cbcSMatt Macy #include <sys/dsl_dir.h>
40eda14cbcSMatt Macy #include <sys/dmu_tx.h>
41eda14cbcSMatt Macy #include <sys/spa.h>
42eda14cbcSMatt Macy #include <sys/zio.h>
43eda14cbcSMatt Macy #include <sys/dmu_zfetch.h>
44eda14cbcSMatt Macy #include <sys/sa.h>
45eda14cbcSMatt Macy #include <sys/sa_impl.h>
46eda14cbcSMatt Macy #include <sys/zfeature.h>
47eda14cbcSMatt Macy #include <sys/blkptr.h>
48eda14cbcSMatt Macy #include <sys/range_tree.h>
49eda14cbcSMatt Macy #include <sys/trace_zfs.h>
50eda14cbcSMatt Macy #include <sys/callb.h>
51eda14cbcSMatt Macy #include <sys/abd.h>
52eda14cbcSMatt Macy #include <sys/vdev.h>
53eda14cbcSMatt Macy #include <cityhash.h>
54eda14cbcSMatt Macy #include <sys/spa_impl.h>
550d8fe237SMartin Matuska #include <sys/wmsum.h>
56dae17134SMartin Matuska #include <sys/vdev_impl.h>
57eda14cbcSMatt Macy 
58e92ffd9bSMartin Matuska static kstat_t *dbuf_ksp;
59eda14cbcSMatt Macy 
60eda14cbcSMatt Macy typedef struct dbuf_stats {
61eda14cbcSMatt Macy 	/*
62eda14cbcSMatt Macy 	 * Various statistics about the size of the dbuf cache.
63eda14cbcSMatt Macy 	 */
64eda14cbcSMatt Macy 	kstat_named_t cache_count;
65eda14cbcSMatt Macy 	kstat_named_t cache_size_bytes;
66eda14cbcSMatt Macy 	kstat_named_t cache_size_bytes_max;
67eda14cbcSMatt Macy 	/*
68eda14cbcSMatt Macy 	 * Statistics regarding the bounds on the dbuf cache size.
69eda14cbcSMatt Macy 	 */
70eda14cbcSMatt Macy 	kstat_named_t cache_target_bytes;
71eda14cbcSMatt Macy 	kstat_named_t cache_lowater_bytes;
72eda14cbcSMatt Macy 	kstat_named_t cache_hiwater_bytes;
73eda14cbcSMatt Macy 	/*
74eda14cbcSMatt Macy 	 * Total number of dbuf cache evictions that have occurred.
75eda14cbcSMatt Macy 	 */
76eda14cbcSMatt Macy 	kstat_named_t cache_total_evicts;
77eda14cbcSMatt Macy 	/*
78eda14cbcSMatt Macy 	 * The distribution of dbuf levels in the dbuf cache and
79eda14cbcSMatt Macy 	 * the total size of all dbufs at each level.
80eda14cbcSMatt Macy 	 */
81eda14cbcSMatt Macy 	kstat_named_t cache_levels[DN_MAX_LEVELS];
82eda14cbcSMatt Macy 	kstat_named_t cache_levels_bytes[DN_MAX_LEVELS];
83eda14cbcSMatt Macy 	/*
84eda14cbcSMatt Macy 	 * Statistics about the dbuf hash table.
85eda14cbcSMatt Macy 	 */
86eda14cbcSMatt Macy 	kstat_named_t hash_hits;
87eda14cbcSMatt Macy 	kstat_named_t hash_misses;
88eda14cbcSMatt Macy 	kstat_named_t hash_collisions;
89eda14cbcSMatt Macy 	kstat_named_t hash_elements;
90eda14cbcSMatt Macy 	kstat_named_t hash_elements_max;
91eda14cbcSMatt Macy 	/*
92eda14cbcSMatt Macy 	 * Number of sublists containing more than one dbuf in the dbuf
93eda14cbcSMatt Macy 	 * hash table. Keep track of the longest hash chain.
94eda14cbcSMatt Macy 	 */
95eda14cbcSMatt Macy 	kstat_named_t hash_chains;
96eda14cbcSMatt Macy 	kstat_named_t hash_chain_max;
97eda14cbcSMatt Macy 	/*
98eda14cbcSMatt Macy 	 * Number of times a dbuf_create() discovers that a dbuf was
99eda14cbcSMatt Macy 	 * already created and in the dbuf hash table.
100eda14cbcSMatt Macy 	 */
101eda14cbcSMatt Macy 	kstat_named_t hash_insert_race;
102eda14cbcSMatt Macy 	/*
103be181ee2SMartin Matuska 	 * Number of entries in the hash table dbuf and mutex arrays.
104be181ee2SMartin Matuska 	 */
105be181ee2SMartin Matuska 	kstat_named_t hash_table_count;
106be181ee2SMartin Matuska 	kstat_named_t hash_mutex_count;
107be181ee2SMartin Matuska 	/*
108eda14cbcSMatt Macy 	 * Statistics about the size of the metadata dbuf cache.
109eda14cbcSMatt Macy 	 */
110eda14cbcSMatt Macy 	kstat_named_t metadata_cache_count;
111eda14cbcSMatt Macy 	kstat_named_t metadata_cache_size_bytes;
112eda14cbcSMatt Macy 	kstat_named_t metadata_cache_size_bytes_max;
113eda14cbcSMatt Macy 	/*
114eda14cbcSMatt Macy 	 * For diagnostic purposes, this is incremented whenever we can't add
115eda14cbcSMatt Macy 	 * something to the metadata cache because it's full, and instead put
116eda14cbcSMatt Macy 	 * the data in the regular dbuf cache.
117eda14cbcSMatt Macy 	 */
118eda14cbcSMatt Macy 	kstat_named_t metadata_cache_overflow;
119eda14cbcSMatt Macy } dbuf_stats_t;
120eda14cbcSMatt Macy 
121eda14cbcSMatt Macy dbuf_stats_t dbuf_stats = {
122eda14cbcSMatt Macy 	{ "cache_count",			KSTAT_DATA_UINT64 },
123eda14cbcSMatt Macy 	{ "cache_size_bytes",			KSTAT_DATA_UINT64 },
124eda14cbcSMatt Macy 	{ "cache_size_bytes_max",		KSTAT_DATA_UINT64 },
125eda14cbcSMatt Macy 	{ "cache_target_bytes",			KSTAT_DATA_UINT64 },
126eda14cbcSMatt Macy 	{ "cache_lowater_bytes",		KSTAT_DATA_UINT64 },
127eda14cbcSMatt Macy 	{ "cache_hiwater_bytes",		KSTAT_DATA_UINT64 },
128eda14cbcSMatt Macy 	{ "cache_total_evicts",			KSTAT_DATA_UINT64 },
129eda14cbcSMatt Macy 	{ { "cache_levels_N",			KSTAT_DATA_UINT64 } },
130eda14cbcSMatt Macy 	{ { "cache_levels_bytes_N",		KSTAT_DATA_UINT64 } },
131eda14cbcSMatt Macy 	{ "hash_hits",				KSTAT_DATA_UINT64 },
132eda14cbcSMatt Macy 	{ "hash_misses",			KSTAT_DATA_UINT64 },
133eda14cbcSMatt Macy 	{ "hash_collisions",			KSTAT_DATA_UINT64 },
134eda14cbcSMatt Macy 	{ "hash_elements",			KSTAT_DATA_UINT64 },
135eda14cbcSMatt Macy 	{ "hash_elements_max",			KSTAT_DATA_UINT64 },
136eda14cbcSMatt Macy 	{ "hash_chains",			KSTAT_DATA_UINT64 },
137eda14cbcSMatt Macy 	{ "hash_chain_max",			KSTAT_DATA_UINT64 },
138eda14cbcSMatt Macy 	{ "hash_insert_race",			KSTAT_DATA_UINT64 },
139be181ee2SMartin Matuska 	{ "hash_table_count",			KSTAT_DATA_UINT64 },
140be181ee2SMartin Matuska 	{ "hash_mutex_count",			KSTAT_DATA_UINT64 },
141eda14cbcSMatt Macy 	{ "metadata_cache_count",		KSTAT_DATA_UINT64 },
142eda14cbcSMatt Macy 	{ "metadata_cache_size_bytes",		KSTAT_DATA_UINT64 },
143eda14cbcSMatt Macy 	{ "metadata_cache_size_bytes_max",	KSTAT_DATA_UINT64 },
144eda14cbcSMatt Macy 	{ "metadata_cache_overflow",		KSTAT_DATA_UINT64 }
145eda14cbcSMatt Macy };
146eda14cbcSMatt Macy 
1470d8fe237SMartin Matuska struct {
1480d8fe237SMartin Matuska 	wmsum_t cache_count;
1490d8fe237SMartin Matuska 	wmsum_t cache_total_evicts;
1500d8fe237SMartin Matuska 	wmsum_t cache_levels[DN_MAX_LEVELS];
1510d8fe237SMartin Matuska 	wmsum_t cache_levels_bytes[DN_MAX_LEVELS];
1520d8fe237SMartin Matuska 	wmsum_t hash_hits;
1530d8fe237SMartin Matuska 	wmsum_t hash_misses;
1540d8fe237SMartin Matuska 	wmsum_t hash_collisions;
1550d8fe237SMartin Matuska 	wmsum_t hash_chains;
1560d8fe237SMartin Matuska 	wmsum_t hash_insert_race;
1570d8fe237SMartin Matuska 	wmsum_t metadata_cache_count;
1580d8fe237SMartin Matuska 	wmsum_t metadata_cache_overflow;
1590d8fe237SMartin Matuska } dbuf_sums;
1600d8fe237SMartin Matuska 
161eda14cbcSMatt Macy #define	DBUF_STAT_INCR(stat, val)	\
1620d8fe237SMartin Matuska 	wmsum_add(&dbuf_sums.stat, val);
163eda14cbcSMatt Macy #define	DBUF_STAT_DECR(stat, val)	\
164eda14cbcSMatt Macy 	DBUF_STAT_INCR(stat, -(val));
165eda14cbcSMatt Macy #define	DBUF_STAT_BUMP(stat)		\
166eda14cbcSMatt Macy 	DBUF_STAT_INCR(stat, 1);
167eda14cbcSMatt Macy #define	DBUF_STAT_BUMPDOWN(stat)	\
168eda14cbcSMatt Macy 	DBUF_STAT_INCR(stat, -1);
169eda14cbcSMatt Macy #define	DBUF_STAT_MAX(stat, v) {					\
170eda14cbcSMatt Macy 	uint64_t _m;							\
171eda14cbcSMatt Macy 	while ((v) > (_m = dbuf_stats.stat.value.ui64) &&		\
172eda14cbcSMatt Macy 	    (_m != atomic_cas_64(&dbuf_stats.stat.value.ui64, _m, (v))))\
173eda14cbcSMatt Macy 		continue;						\
174eda14cbcSMatt Macy }
175eda14cbcSMatt Macy 
176eda14cbcSMatt Macy static boolean_t dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
177eda14cbcSMatt Macy static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx);
178eda14cbcSMatt Macy static void dbuf_sync_leaf_verify_bonus_dnode(dbuf_dirty_record_t *dr);
179eda14cbcSMatt Macy static int dbuf_read_verify_dnode_crypt(dmu_buf_impl_t *db, uint32_t flags);
180eda14cbcSMatt Macy 
181eda14cbcSMatt Macy /*
182eda14cbcSMatt Macy  * Global data structures and functions for the dbuf cache.
183eda14cbcSMatt Macy  */
184eda14cbcSMatt Macy static kmem_cache_t *dbuf_kmem_cache;
185eda14cbcSMatt Macy static taskq_t *dbu_evict_taskq;
186eda14cbcSMatt Macy 
187eda14cbcSMatt Macy static kthread_t *dbuf_cache_evict_thread;
188eda14cbcSMatt Macy static kmutex_t dbuf_evict_lock;
189eda14cbcSMatt Macy static kcondvar_t dbuf_evict_cv;
190eda14cbcSMatt Macy static boolean_t dbuf_evict_thread_exit;
191eda14cbcSMatt Macy 
192eda14cbcSMatt Macy /*
193eda14cbcSMatt Macy  * There are two dbuf caches; each dbuf can only be in one of them at a time.
194eda14cbcSMatt Macy  *
195eda14cbcSMatt Macy  * 1. Cache of metadata dbufs, to help make read-heavy administrative commands
196eda14cbcSMatt Macy  *    from /sbin/zfs run faster. The "metadata cache" specifically stores dbufs
197eda14cbcSMatt Macy  *    that represent the metadata that describes filesystems/snapshots/
198eda14cbcSMatt Macy  *    bookmarks/properties/etc. We only evict from this cache when we export a
199eda14cbcSMatt Macy  *    pool, to short-circuit as much I/O as possible for all administrative
200eda14cbcSMatt Macy  *    commands that need the metadata. There is no eviction policy for this
201eda14cbcSMatt Macy  *    cache, because we try to only include types in it which would occupy a
202eda14cbcSMatt Macy  *    very small amount of space per object but create a large impact on the
203eda14cbcSMatt Macy  *    performance of these commands. Instead, after it reaches a maximum size
204eda14cbcSMatt Macy  *    (which should only happen on very small memory systems with a very large
205eda14cbcSMatt Macy  *    number of filesystem objects), we stop taking new dbufs into the
206eda14cbcSMatt Macy  *    metadata cache, instead putting them in the normal dbuf cache.
207eda14cbcSMatt Macy  *
208eda14cbcSMatt Macy  * 2. LRU cache of dbufs. The dbuf cache maintains a list of dbufs that
209eda14cbcSMatt Macy  *    are not currently held but have been recently released. These dbufs
210eda14cbcSMatt Macy  *    are not eligible for arc eviction until they are aged out of the cache.
211eda14cbcSMatt Macy  *    Dbufs that are aged out of the cache will be immediately destroyed and
212eda14cbcSMatt Macy  *    become eligible for arc eviction.
213eda14cbcSMatt Macy  *
214eda14cbcSMatt Macy  * Dbufs are added to these caches once the last hold is released. If a dbuf is
215eda14cbcSMatt Macy  * later accessed and still exists in the dbuf cache, then it will be removed
216eda14cbcSMatt Macy  * from the cache and later re-added to the head of the cache.
217eda14cbcSMatt Macy  *
218eda14cbcSMatt Macy  * If a given dbuf meets the requirements for the metadata cache, it will go
219eda14cbcSMatt Macy  * there, otherwise it will be considered for the generic LRU dbuf cache. The
220eda14cbcSMatt Macy  * caches and the refcounts tracking their sizes are stored in an array indexed
221eda14cbcSMatt Macy  * by those caches' matching enum values (from dbuf_cached_state_t).
222eda14cbcSMatt Macy  */
223eda14cbcSMatt Macy typedef struct dbuf_cache {
2243ff01b23SMartin Matuska 	multilist_t cache;
2253ff01b23SMartin Matuska 	zfs_refcount_t size ____cacheline_aligned;
226eda14cbcSMatt Macy } dbuf_cache_t;
227eda14cbcSMatt Macy dbuf_cache_t dbuf_caches[DB_CACHE_MAX];
228eda14cbcSMatt Macy 
229eda14cbcSMatt Macy /* Size limits for the caches */
230dbd5678dSMartin Matuska static uint64_t dbuf_cache_max_bytes = UINT64_MAX;
231dbd5678dSMartin Matuska static uint64_t dbuf_metadata_cache_max_bytes = UINT64_MAX;
232eda14cbcSMatt Macy 
233eda14cbcSMatt Macy /* Set the default sizes of the caches to log2 fraction of arc size */
234be181ee2SMartin Matuska static uint_t dbuf_cache_shift = 5;
235be181ee2SMartin Matuska static uint_t dbuf_metadata_cache_shift = 6;
236be181ee2SMartin Matuska 
237be181ee2SMartin Matuska /* Set the dbuf hash mutex count as log2 shift (dynamic by default) */
238be181ee2SMartin Matuska static uint_t dbuf_mutex_cache_shift = 0;
239eda14cbcSMatt Macy 
240eda14cbcSMatt Macy static unsigned long dbuf_cache_target_bytes(void);
241eda14cbcSMatt Macy static unsigned long dbuf_metadata_cache_target_bytes(void);
242eda14cbcSMatt Macy 
243eda14cbcSMatt Macy /*
244eda14cbcSMatt Macy  * The LRU dbuf cache uses a three-stage eviction policy:
245eda14cbcSMatt Macy  *	- A low water marker designates when the dbuf eviction thread
246eda14cbcSMatt Macy  *	should stop evicting from the dbuf cache.
247eda14cbcSMatt Macy  *	- When we reach the maximum size (aka mid water mark), we
248eda14cbcSMatt Macy  *	signal the eviction thread to run.
249eda14cbcSMatt Macy  *	- The high water mark indicates when the eviction thread
250eda14cbcSMatt Macy  *	is unable to keep up with the incoming load and eviction must
251eda14cbcSMatt Macy  *	happen in the context of the calling thread.
252eda14cbcSMatt Macy  *
253eda14cbcSMatt Macy  * The dbuf cache:
254eda14cbcSMatt Macy  *                                                 (max size)
255eda14cbcSMatt Macy  *                                      low water   mid water   hi water
256eda14cbcSMatt Macy  * +----------------------------------------+----------+----------+
257eda14cbcSMatt Macy  * |                                        |          |          |
258eda14cbcSMatt Macy  * |                                        |          |          |
259eda14cbcSMatt Macy  * |                                        |          |          |
260eda14cbcSMatt Macy  * |                                        |          |          |
261eda14cbcSMatt Macy  * +----------------------------------------+----------+----------+
262eda14cbcSMatt Macy  *                                        stop        signal     evict
263eda14cbcSMatt Macy  *                                      evicting     eviction   directly
264eda14cbcSMatt Macy  *                                                    thread
265eda14cbcSMatt Macy  *
266eda14cbcSMatt Macy  * The high and low water marks indicate the operating range for the eviction
267eda14cbcSMatt Macy  * thread. The low water mark is, by default, 90% of the total size of the
268eda14cbcSMatt Macy  * cache and the high water mark is at 110% (both of these percentages can be
269eda14cbcSMatt Macy  * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct,
270eda14cbcSMatt Macy  * respectively). The eviction thread will try to ensure that the cache remains
271eda14cbcSMatt Macy  * within this range by waking up every second and checking if the cache is
272eda14cbcSMatt Macy  * above the low water mark. The thread can also be woken up by callers adding
273eda14cbcSMatt Macy  * elements into the cache if the cache is larger than the mid water (i.e max
274eda14cbcSMatt Macy  * cache size). Once the eviction thread is woken up and eviction is required,
275eda14cbcSMatt Macy  * it will continue evicting buffers until it's able to reduce the cache size
276eda14cbcSMatt Macy  * to the low water mark. If the cache size continues to grow and hits the high
277eda14cbcSMatt Macy  * water mark, then callers adding elements to the cache will begin to evict
278eda14cbcSMatt Macy  * directly from the cache until the cache is no longer above the high water
279eda14cbcSMatt Macy  * mark.
280eda14cbcSMatt Macy  */
281eda14cbcSMatt Macy 
282eda14cbcSMatt Macy /*
283eda14cbcSMatt Macy  * The percentage above and below the maximum cache size.
284eda14cbcSMatt Macy  */
285e92ffd9bSMartin Matuska static uint_t dbuf_cache_hiwater_pct = 10;
286e92ffd9bSMartin Matuska static uint_t dbuf_cache_lowater_pct = 10;
287eda14cbcSMatt Macy 
288eda14cbcSMatt Macy static int
289eda14cbcSMatt Macy dbuf_cons(void *vdb, void *unused, int kmflag)
290eda14cbcSMatt Macy {
291e92ffd9bSMartin Matuska 	(void) unused, (void) kmflag;
292eda14cbcSMatt Macy 	dmu_buf_impl_t *db = vdb;
293da5137abSMartin Matuska 	memset(db, 0, sizeof (dmu_buf_impl_t));
294eda14cbcSMatt Macy 
295eda14cbcSMatt Macy 	mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL);
296eda14cbcSMatt Macy 	rw_init(&db->db_rwlock, NULL, RW_DEFAULT, NULL);
297eda14cbcSMatt Macy 	cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL);
298eda14cbcSMatt Macy 	multilist_link_init(&db->db_cache_link);
299eda14cbcSMatt Macy 	zfs_refcount_create(&db->db_holds);
300eda14cbcSMatt Macy 
301eda14cbcSMatt Macy 	return (0);
302eda14cbcSMatt Macy }
303eda14cbcSMatt Macy 
304eda14cbcSMatt Macy static void
305eda14cbcSMatt Macy dbuf_dest(void *vdb, void *unused)
306eda14cbcSMatt Macy {
307e92ffd9bSMartin Matuska 	(void) unused;
308eda14cbcSMatt Macy 	dmu_buf_impl_t *db = vdb;
309eda14cbcSMatt Macy 	mutex_destroy(&db->db_mtx);
310eda14cbcSMatt Macy 	rw_destroy(&db->db_rwlock);
311eda14cbcSMatt Macy 	cv_destroy(&db->db_changed);
312eda14cbcSMatt Macy 	ASSERT(!multilist_link_active(&db->db_cache_link));
313eda14cbcSMatt Macy 	zfs_refcount_destroy(&db->db_holds);
314eda14cbcSMatt Macy }
315eda14cbcSMatt Macy 
316eda14cbcSMatt Macy /*
317eda14cbcSMatt Macy  * dbuf hash table routines
318eda14cbcSMatt Macy  */
319eda14cbcSMatt Macy static dbuf_hash_table_t dbuf_hash_table;
320eda14cbcSMatt Macy 
321eda14cbcSMatt Macy /*
322eda14cbcSMatt Macy  * We use Cityhash for this. It's fast, and has good hash properties without
323eda14cbcSMatt Macy  * requiring any large static buffers.
324eda14cbcSMatt Macy  */
325eda14cbcSMatt Macy static uint64_t
326eda14cbcSMatt Macy dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid)
327eda14cbcSMatt Macy {
328eda14cbcSMatt Macy 	return (cityhash4((uintptr_t)os, obj, (uint64_t)lvl, blkid));
329eda14cbcSMatt Macy }
330eda14cbcSMatt Macy 
331eda14cbcSMatt Macy #define	DTRACE_SET_STATE(db, why) \
332eda14cbcSMatt Macy 	DTRACE_PROBE2(dbuf__state_change, dmu_buf_impl_t *, db,	\
333eda14cbcSMatt Macy 	    const char *, why)
334eda14cbcSMatt Macy 
335eda14cbcSMatt Macy #define	DBUF_EQUAL(dbuf, os, obj, level, blkid)		\
336eda14cbcSMatt Macy 	((dbuf)->db.db_object == (obj) &&		\
337eda14cbcSMatt Macy 	(dbuf)->db_objset == (os) &&			\
338eda14cbcSMatt Macy 	(dbuf)->db_level == (level) &&			\
339eda14cbcSMatt Macy 	(dbuf)->db_blkid == (blkid))
340eda14cbcSMatt Macy 
341eda14cbcSMatt Macy dmu_buf_impl_t *
342eda14cbcSMatt Macy dbuf_find(objset_t *os, uint64_t obj, uint8_t level, uint64_t blkid)
343eda14cbcSMatt Macy {
344eda14cbcSMatt Macy 	dbuf_hash_table_t *h = &dbuf_hash_table;
345eda14cbcSMatt Macy 	uint64_t hv;
346eda14cbcSMatt Macy 	uint64_t idx;
347eda14cbcSMatt Macy 	dmu_buf_impl_t *db;
348eda14cbcSMatt Macy 
349eda14cbcSMatt Macy 	hv = dbuf_hash(os, obj, level, blkid);
350eda14cbcSMatt Macy 	idx = hv & h->hash_table_mask;
351eda14cbcSMatt Macy 
352be181ee2SMartin Matuska 	mutex_enter(DBUF_HASH_MUTEX(h, idx));
353eda14cbcSMatt Macy 	for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) {
354eda14cbcSMatt Macy 		if (DBUF_EQUAL(db, os, obj, level, blkid)) {
355eda14cbcSMatt Macy 			mutex_enter(&db->db_mtx);
356eda14cbcSMatt Macy 			if (db->db_state != DB_EVICTING) {
357be181ee2SMartin Matuska 				mutex_exit(DBUF_HASH_MUTEX(h, idx));
358eda14cbcSMatt Macy 				return (db);
359eda14cbcSMatt Macy 			}
360eda14cbcSMatt Macy 			mutex_exit(&db->db_mtx);
361eda14cbcSMatt Macy 		}
362eda14cbcSMatt Macy 	}
363be181ee2SMartin Matuska 	mutex_exit(DBUF_HASH_MUTEX(h, idx));
364eda14cbcSMatt Macy 	return (NULL);
365eda14cbcSMatt Macy }
366eda14cbcSMatt Macy 
367eda14cbcSMatt Macy static dmu_buf_impl_t *
368eda14cbcSMatt Macy dbuf_find_bonus(objset_t *os, uint64_t object)
369eda14cbcSMatt Macy {
370eda14cbcSMatt Macy 	dnode_t *dn;
371eda14cbcSMatt Macy 	dmu_buf_impl_t *db = NULL;
372eda14cbcSMatt Macy 
373eda14cbcSMatt Macy 	if (dnode_hold(os, object, FTAG, &dn) == 0) {
374eda14cbcSMatt Macy 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
375eda14cbcSMatt Macy 		if (dn->dn_bonus != NULL) {
376eda14cbcSMatt Macy 			db = dn->dn_bonus;
377eda14cbcSMatt Macy 			mutex_enter(&db->db_mtx);
378eda14cbcSMatt Macy 		}
379eda14cbcSMatt Macy 		rw_exit(&dn->dn_struct_rwlock);
380eda14cbcSMatt Macy 		dnode_rele(dn, FTAG);
381eda14cbcSMatt Macy 	}
382eda14cbcSMatt Macy 	return (db);
383eda14cbcSMatt Macy }
384eda14cbcSMatt Macy 
385eda14cbcSMatt Macy /*
386eda14cbcSMatt Macy  * Insert an entry into the hash table.  If there is already an element
387eda14cbcSMatt Macy  * equal to elem in the hash table, then the already existing element
388eda14cbcSMatt Macy  * will be returned and the new element will not be inserted.
389eda14cbcSMatt Macy  * Otherwise returns NULL.
390eda14cbcSMatt Macy  */
391eda14cbcSMatt Macy static dmu_buf_impl_t *
392eda14cbcSMatt Macy dbuf_hash_insert(dmu_buf_impl_t *db)
393eda14cbcSMatt Macy {
394eda14cbcSMatt Macy 	dbuf_hash_table_t *h = &dbuf_hash_table;
395eda14cbcSMatt Macy 	objset_t *os = db->db_objset;
396eda14cbcSMatt Macy 	uint64_t obj = db->db.db_object;
397eda14cbcSMatt Macy 	int level = db->db_level;
398eda14cbcSMatt Macy 	uint64_t blkid, hv, idx;
399eda14cbcSMatt Macy 	dmu_buf_impl_t *dbf;
400eda14cbcSMatt Macy 	uint32_t i;
401eda14cbcSMatt Macy 
402eda14cbcSMatt Macy 	blkid = db->db_blkid;
403eda14cbcSMatt Macy 	hv = dbuf_hash(os, obj, level, blkid);
404eda14cbcSMatt Macy 	idx = hv & h->hash_table_mask;
405eda14cbcSMatt Macy 
406be181ee2SMartin Matuska 	mutex_enter(DBUF_HASH_MUTEX(h, idx));
407eda14cbcSMatt Macy 	for (dbf = h->hash_table[idx], i = 0; dbf != NULL;
408eda14cbcSMatt Macy 	    dbf = dbf->db_hash_next, i++) {
409eda14cbcSMatt Macy 		if (DBUF_EQUAL(dbf, os, obj, level, blkid)) {
410eda14cbcSMatt Macy 			mutex_enter(&dbf->db_mtx);
411eda14cbcSMatt Macy 			if (dbf->db_state != DB_EVICTING) {
412be181ee2SMartin Matuska 				mutex_exit(DBUF_HASH_MUTEX(h, idx));
413eda14cbcSMatt Macy 				return (dbf);
414eda14cbcSMatt Macy 			}
415eda14cbcSMatt Macy 			mutex_exit(&dbf->db_mtx);
416eda14cbcSMatt Macy 		}
417eda14cbcSMatt Macy 	}
418eda14cbcSMatt Macy 
419eda14cbcSMatt Macy 	if (i > 0) {
420eda14cbcSMatt Macy 		DBUF_STAT_BUMP(hash_collisions);
421eda14cbcSMatt Macy 		if (i == 1)
422eda14cbcSMatt Macy 			DBUF_STAT_BUMP(hash_chains);
423eda14cbcSMatt Macy 
424eda14cbcSMatt Macy 		DBUF_STAT_MAX(hash_chain_max, i);
425eda14cbcSMatt Macy 	}
426eda14cbcSMatt Macy 
427eda14cbcSMatt Macy 	mutex_enter(&db->db_mtx);
428eda14cbcSMatt Macy 	db->db_hash_next = h->hash_table[idx];
429eda14cbcSMatt Macy 	h->hash_table[idx] = db;
430be181ee2SMartin Matuska 	mutex_exit(DBUF_HASH_MUTEX(h, idx));
4310d8fe237SMartin Matuska 	uint64_t he = atomic_inc_64_nv(&dbuf_stats.hash_elements.value.ui64);
4320d8fe237SMartin Matuska 	DBUF_STAT_MAX(hash_elements_max, he);
433eda14cbcSMatt Macy 
434eda14cbcSMatt Macy 	return (NULL);
435eda14cbcSMatt Macy }
436eda14cbcSMatt Macy 
437eda14cbcSMatt Macy /*
438eda14cbcSMatt Macy  * This returns whether this dbuf should be stored in the metadata cache, which
439eda14cbcSMatt Macy  * is based on whether it's from one of the dnode types that store data related
440eda14cbcSMatt Macy  * to traversing dataset hierarchies.
441eda14cbcSMatt Macy  */
442eda14cbcSMatt Macy static boolean_t
443eda14cbcSMatt Macy dbuf_include_in_metadata_cache(dmu_buf_impl_t *db)
444eda14cbcSMatt Macy {
445eda14cbcSMatt Macy 	DB_DNODE_ENTER(db);
446eda14cbcSMatt Macy 	dmu_object_type_t type = DB_DNODE(db)->dn_type;
447eda14cbcSMatt Macy 	DB_DNODE_EXIT(db);
448eda14cbcSMatt Macy 
449eda14cbcSMatt Macy 	/* Check if this dbuf is one of the types we care about */
450eda14cbcSMatt Macy 	if (DMU_OT_IS_METADATA_CACHED(type)) {
451eda14cbcSMatt Macy 		/* If we hit this, then we set something up wrong in dmu_ot */
452eda14cbcSMatt Macy 		ASSERT(DMU_OT_IS_METADATA(type));
453eda14cbcSMatt Macy 
454eda14cbcSMatt Macy 		/*
455eda14cbcSMatt Macy 		 * Sanity check for small-memory systems: don't allocate too
456eda14cbcSMatt Macy 		 * much memory for this purpose.
457eda14cbcSMatt Macy 		 */
458eda14cbcSMatt Macy 		if (zfs_refcount_count(
459eda14cbcSMatt Macy 		    &dbuf_caches[DB_DBUF_METADATA_CACHE].size) >
460eda14cbcSMatt Macy 		    dbuf_metadata_cache_target_bytes()) {
461eda14cbcSMatt Macy 			DBUF_STAT_BUMP(metadata_cache_overflow);
462eda14cbcSMatt Macy 			return (B_FALSE);
463eda14cbcSMatt Macy 		}
464eda14cbcSMatt Macy 
465eda14cbcSMatt Macy 		return (B_TRUE);
466eda14cbcSMatt Macy 	}
467eda14cbcSMatt Macy 
468eda14cbcSMatt Macy 	return (B_FALSE);
469eda14cbcSMatt Macy }
470eda14cbcSMatt Macy 
471eda14cbcSMatt Macy /*
472eda14cbcSMatt Macy  * Remove an entry from the hash table.  It must be in the EVICTING state.
473eda14cbcSMatt Macy  */
474eda14cbcSMatt Macy static void
475eda14cbcSMatt Macy dbuf_hash_remove(dmu_buf_impl_t *db)
476eda14cbcSMatt Macy {
477eda14cbcSMatt Macy 	dbuf_hash_table_t *h = &dbuf_hash_table;
478eda14cbcSMatt Macy 	uint64_t hv, idx;
479eda14cbcSMatt Macy 	dmu_buf_impl_t *dbf, **dbp;
480eda14cbcSMatt Macy 
481eda14cbcSMatt Macy 	hv = dbuf_hash(db->db_objset, db->db.db_object,
482eda14cbcSMatt Macy 	    db->db_level, db->db_blkid);
483eda14cbcSMatt Macy 	idx = hv & h->hash_table_mask;
484eda14cbcSMatt Macy 
485eda14cbcSMatt Macy 	/*
486eda14cbcSMatt Macy 	 * We mustn't hold db_mtx to maintain lock ordering:
487be181ee2SMartin Matuska 	 * DBUF_HASH_MUTEX > db_mtx.
488eda14cbcSMatt Macy 	 */
489eda14cbcSMatt Macy 	ASSERT(zfs_refcount_is_zero(&db->db_holds));
490eda14cbcSMatt Macy 	ASSERT(db->db_state == DB_EVICTING);
491eda14cbcSMatt Macy 	ASSERT(!MUTEX_HELD(&db->db_mtx));
492eda14cbcSMatt Macy 
493be181ee2SMartin Matuska 	mutex_enter(DBUF_HASH_MUTEX(h, idx));
494eda14cbcSMatt Macy 	dbp = &h->hash_table[idx];
495eda14cbcSMatt Macy 	while ((dbf = *dbp) != db) {
496eda14cbcSMatt Macy 		dbp = &dbf->db_hash_next;
497eda14cbcSMatt Macy 		ASSERT(dbf != NULL);
498eda14cbcSMatt Macy 	}
499eda14cbcSMatt Macy 	*dbp = db->db_hash_next;
500eda14cbcSMatt Macy 	db->db_hash_next = NULL;
501eda14cbcSMatt Macy 	if (h->hash_table[idx] &&
502eda14cbcSMatt Macy 	    h->hash_table[idx]->db_hash_next == NULL)
503eda14cbcSMatt Macy 		DBUF_STAT_BUMPDOWN(hash_chains);
504be181ee2SMartin Matuska 	mutex_exit(DBUF_HASH_MUTEX(h, idx));
5050d8fe237SMartin Matuska 	atomic_dec_64(&dbuf_stats.hash_elements.value.ui64);
506eda14cbcSMatt Macy }
507eda14cbcSMatt Macy 
508eda14cbcSMatt Macy typedef enum {
509eda14cbcSMatt Macy 	DBVU_EVICTING,
510eda14cbcSMatt Macy 	DBVU_NOT_EVICTING
511eda14cbcSMatt Macy } dbvu_verify_type_t;
512eda14cbcSMatt Macy 
513eda14cbcSMatt Macy static void
514eda14cbcSMatt Macy dbuf_verify_user(dmu_buf_impl_t *db, dbvu_verify_type_t verify_type)
515eda14cbcSMatt Macy {
516eda14cbcSMatt Macy #ifdef ZFS_DEBUG
517eda14cbcSMatt Macy 	int64_t holds;
518eda14cbcSMatt Macy 
519eda14cbcSMatt Macy 	if (db->db_user == NULL)
520eda14cbcSMatt Macy 		return;
521eda14cbcSMatt Macy 
522eda14cbcSMatt Macy 	/* Only data blocks support the attachment of user data. */
523eda14cbcSMatt Macy 	ASSERT(db->db_level == 0);
524eda14cbcSMatt Macy 
525eda14cbcSMatt Macy 	/* Clients must resolve a dbuf before attaching user data. */
526eda14cbcSMatt Macy 	ASSERT(db->db.db_data != NULL);
527eda14cbcSMatt Macy 	ASSERT3U(db->db_state, ==, DB_CACHED);
528eda14cbcSMatt Macy 
529eda14cbcSMatt Macy 	holds = zfs_refcount_count(&db->db_holds);
530eda14cbcSMatt Macy 	if (verify_type == DBVU_EVICTING) {
531eda14cbcSMatt Macy 		/*
532eda14cbcSMatt Macy 		 * Immediate eviction occurs when holds == dirtycnt.
533eda14cbcSMatt Macy 		 * For normal eviction buffers, holds is zero on
534eda14cbcSMatt Macy 		 * eviction, except when dbuf_fix_old_data() calls
535eda14cbcSMatt Macy 		 * dbuf_clear_data().  However, the hold count can grow
536eda14cbcSMatt Macy 		 * during eviction even though db_mtx is held (see
537eda14cbcSMatt Macy 		 * dmu_bonus_hold() for an example), so we can only
538eda14cbcSMatt Macy 		 * test the generic invariant that holds >= dirtycnt.
539eda14cbcSMatt Macy 		 */
540eda14cbcSMatt Macy 		ASSERT3U(holds, >=, db->db_dirtycnt);
541eda14cbcSMatt Macy 	} else {
542eda14cbcSMatt Macy 		if (db->db_user_immediate_evict == TRUE)
543eda14cbcSMatt Macy 			ASSERT3U(holds, >=, db->db_dirtycnt);
544eda14cbcSMatt Macy 		else
545eda14cbcSMatt Macy 			ASSERT3U(holds, >, 0);
546eda14cbcSMatt Macy 	}
547eda14cbcSMatt Macy #endif
548eda14cbcSMatt Macy }
549eda14cbcSMatt Macy 
550eda14cbcSMatt Macy static void
551eda14cbcSMatt Macy dbuf_evict_user(dmu_buf_impl_t *db)
552eda14cbcSMatt Macy {
553eda14cbcSMatt Macy 	dmu_buf_user_t *dbu = db->db_user;
554eda14cbcSMatt Macy 
555eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&db->db_mtx));
556eda14cbcSMatt Macy 
557eda14cbcSMatt Macy 	if (dbu == NULL)
558eda14cbcSMatt Macy 		return;
559eda14cbcSMatt Macy 
560eda14cbcSMatt Macy 	dbuf_verify_user(db, DBVU_EVICTING);
561eda14cbcSMatt Macy 	db->db_user = NULL;
562eda14cbcSMatt Macy 
563eda14cbcSMatt Macy #ifdef ZFS_DEBUG
564eda14cbcSMatt Macy 	if (dbu->dbu_clear_on_evict_dbufp != NULL)
565eda14cbcSMatt Macy 		*dbu->dbu_clear_on_evict_dbufp = NULL;
566eda14cbcSMatt Macy #endif
567eda14cbcSMatt Macy 
568eda14cbcSMatt Macy 	/*
569eda14cbcSMatt Macy 	 * There are two eviction callbacks - one that we call synchronously
570eda14cbcSMatt Macy 	 * and one that we invoke via a taskq.  The async one is useful for
571eda14cbcSMatt Macy 	 * avoiding lock order reversals and limiting stack depth.
572eda14cbcSMatt Macy 	 *
573eda14cbcSMatt Macy 	 * Note that if we have a sync callback but no async callback,
574eda14cbcSMatt Macy 	 * it's likely that the sync callback will free the structure
575eda14cbcSMatt Macy 	 * containing the dbu.  In that case we need to take care to not
576eda14cbcSMatt Macy 	 * dereference dbu after calling the sync evict func.
577eda14cbcSMatt Macy 	 */
578eda14cbcSMatt Macy 	boolean_t has_async = (dbu->dbu_evict_func_async != NULL);
579eda14cbcSMatt Macy 
580eda14cbcSMatt Macy 	if (dbu->dbu_evict_func_sync != NULL)
581eda14cbcSMatt Macy 		dbu->dbu_evict_func_sync(dbu);
582eda14cbcSMatt Macy 
583eda14cbcSMatt Macy 	if (has_async) {
584eda14cbcSMatt Macy 		taskq_dispatch_ent(dbu_evict_taskq, dbu->dbu_evict_func_async,
585eda14cbcSMatt Macy 		    dbu, 0, &dbu->dbu_tqent);
586eda14cbcSMatt Macy 	}
587eda14cbcSMatt Macy }
588eda14cbcSMatt Macy 
589eda14cbcSMatt Macy boolean_t
590eda14cbcSMatt Macy dbuf_is_metadata(dmu_buf_impl_t *db)
591eda14cbcSMatt Macy {
592eda14cbcSMatt Macy 	/*
593eda14cbcSMatt Macy 	 * Consider indirect blocks and spill blocks to be meta data.
594eda14cbcSMatt Macy 	 */
595eda14cbcSMatt Macy 	if (db->db_level > 0 || db->db_blkid == DMU_SPILL_BLKID) {
596eda14cbcSMatt Macy 		return (B_TRUE);
597eda14cbcSMatt Macy 	} else {
598eda14cbcSMatt Macy 		boolean_t is_metadata;
599eda14cbcSMatt Macy 
600eda14cbcSMatt Macy 		DB_DNODE_ENTER(db);
601eda14cbcSMatt Macy 		is_metadata = DMU_OT_IS_METADATA(DB_DNODE(db)->dn_type);
602eda14cbcSMatt Macy 		DB_DNODE_EXIT(db);
603eda14cbcSMatt Macy 
604eda14cbcSMatt Macy 		return (is_metadata);
605eda14cbcSMatt Macy 	}
606eda14cbcSMatt Macy }
607eda14cbcSMatt Macy 
608dae17134SMartin Matuska /*
609dae17134SMartin Matuska  * We want to exclude buffers that are on a special allocation class from
610dae17134SMartin Matuska  * L2ARC.
611dae17134SMartin Matuska  */
612dae17134SMartin Matuska boolean_t
613dae17134SMartin Matuska dbuf_is_l2cacheable(dmu_buf_impl_t *db)
614dae17134SMartin Matuska {
615dae17134SMartin Matuska 	vdev_t *vd = NULL;
616dae17134SMartin Matuska 	zfs_cache_type_t cache = db->db_objset->os_secondary_cache;
617dae17134SMartin Matuska 	blkptr_t *bp = db->db_blkptr;
618dae17134SMartin Matuska 
619dae17134SMartin Matuska 	if (bp != NULL && !BP_IS_HOLE(bp)) {
620dae17134SMartin Matuska 		uint64_t vdev = DVA_GET_VDEV(bp->blk_dva);
621dae17134SMartin Matuska 		vdev_t *rvd = db->db_objset->os_spa->spa_root_vdev;
622dae17134SMartin Matuska 
623dae17134SMartin Matuska 		if (vdev < rvd->vdev_children)
624dae17134SMartin Matuska 			vd = rvd->vdev_child[vdev];
625dae17134SMartin Matuska 
626dae17134SMartin Matuska 		if (cache == ZFS_CACHE_ALL ||
627dae17134SMartin Matuska 		    (dbuf_is_metadata(db) && cache == ZFS_CACHE_METADATA)) {
628dae17134SMartin Matuska 			if (vd == NULL)
629dae17134SMartin Matuska 				return (B_TRUE);
630dae17134SMartin Matuska 
631dae17134SMartin Matuska 			if ((vd->vdev_alloc_bias != VDEV_BIAS_SPECIAL &&
632dae17134SMartin Matuska 			    vd->vdev_alloc_bias != VDEV_BIAS_DEDUP) ||
633dae17134SMartin Matuska 			    l2arc_exclude_special == 0)
634dae17134SMartin Matuska 				return (B_TRUE);
635dae17134SMartin Matuska 		}
636dae17134SMartin Matuska 	}
637dae17134SMartin Matuska 
638dae17134SMartin Matuska 	return (B_FALSE);
639dae17134SMartin Matuska }
640dae17134SMartin Matuska 
641dae17134SMartin Matuska static inline boolean_t
642dae17134SMartin Matuska dnode_level_is_l2cacheable(blkptr_t *bp, dnode_t *dn, int64_t level)
643dae17134SMartin Matuska {
644dae17134SMartin Matuska 	vdev_t *vd = NULL;
645dae17134SMartin Matuska 	zfs_cache_type_t cache = dn->dn_objset->os_secondary_cache;
646dae17134SMartin Matuska 
647dae17134SMartin Matuska 	if (bp != NULL && !BP_IS_HOLE(bp)) {
648dae17134SMartin Matuska 		uint64_t vdev = DVA_GET_VDEV(bp->blk_dva);
649dae17134SMartin Matuska 		vdev_t *rvd = dn->dn_objset->os_spa->spa_root_vdev;
650dae17134SMartin Matuska 
651dae17134SMartin Matuska 		if (vdev < rvd->vdev_children)
652dae17134SMartin Matuska 			vd = rvd->vdev_child[vdev];
653dae17134SMartin Matuska 
654dae17134SMartin Matuska 		if (cache == ZFS_CACHE_ALL || ((level > 0 ||
655dae17134SMartin Matuska 		    DMU_OT_IS_METADATA(dn->dn_handle->dnh_dnode->dn_type)) &&
656dae17134SMartin Matuska 		    cache == ZFS_CACHE_METADATA)) {
657dae17134SMartin Matuska 			if (vd == NULL)
658dae17134SMartin Matuska 				return (B_TRUE);
659dae17134SMartin Matuska 
660dae17134SMartin Matuska 			if ((vd->vdev_alloc_bias != VDEV_BIAS_SPECIAL &&
661dae17134SMartin Matuska 			    vd->vdev_alloc_bias != VDEV_BIAS_DEDUP) ||
662dae17134SMartin Matuska 			    l2arc_exclude_special == 0)
663dae17134SMartin Matuska 				return (B_TRUE);
664dae17134SMartin Matuska 		}
665dae17134SMartin Matuska 	}
666dae17134SMartin Matuska 
667dae17134SMartin Matuska 	return (B_FALSE);
668dae17134SMartin Matuska }
669dae17134SMartin Matuska 
670eda14cbcSMatt Macy 
671eda14cbcSMatt Macy /*
672eda14cbcSMatt Macy  * This function *must* return indices evenly distributed between all
673eda14cbcSMatt Macy  * sublists of the multilist. This is needed due to how the dbuf eviction
674eda14cbcSMatt Macy  * code is laid out; dbuf_evict_thread() assumes dbufs are evenly
675eda14cbcSMatt Macy  * distributed between all sublists and uses this assumption when
676eda14cbcSMatt Macy  * deciding which sublist to evict from and how much to evict from it.
677eda14cbcSMatt Macy  */
678eda14cbcSMatt Macy static unsigned int
679eda14cbcSMatt Macy dbuf_cache_multilist_index_func(multilist_t *ml, void *obj)
680eda14cbcSMatt Macy {
681eda14cbcSMatt Macy 	dmu_buf_impl_t *db = obj;
682eda14cbcSMatt Macy 
683eda14cbcSMatt Macy 	/*
684eda14cbcSMatt Macy 	 * The assumption here, is the hash value for a given
685eda14cbcSMatt Macy 	 * dmu_buf_impl_t will remain constant throughout it's lifetime
686eda14cbcSMatt Macy 	 * (i.e. it's objset, object, level and blkid fields don't change).
687eda14cbcSMatt Macy 	 * Thus, we don't need to store the dbuf's sublist index
688eda14cbcSMatt Macy 	 * on insertion, as this index can be recalculated on removal.
689eda14cbcSMatt Macy 	 *
690eda14cbcSMatt Macy 	 * Also, the low order bits of the hash value are thought to be
691eda14cbcSMatt Macy 	 * distributed evenly. Otherwise, in the case that the multilist
692eda14cbcSMatt Macy 	 * has a power of two number of sublists, each sublists' usage
6932617128aSMartin Matuska 	 * would not be evenly distributed. In this context full 64bit
6942617128aSMartin Matuska 	 * division would be a waste of time, so limit it to 32 bits.
695eda14cbcSMatt Macy 	 */
6962617128aSMartin Matuska 	return ((unsigned int)dbuf_hash(db->db_objset, db->db.db_object,
697eda14cbcSMatt Macy 	    db->db_level, db->db_blkid) %
698eda14cbcSMatt Macy 	    multilist_get_num_sublists(ml));
699eda14cbcSMatt Macy }
700eda14cbcSMatt Macy 
701eda14cbcSMatt Macy /*
702eda14cbcSMatt Macy  * The target size of the dbuf cache can grow with the ARC target,
703eda14cbcSMatt Macy  * unless limited by the tunable dbuf_cache_max_bytes.
704eda14cbcSMatt Macy  */
705eda14cbcSMatt Macy static inline unsigned long
706eda14cbcSMatt Macy dbuf_cache_target_bytes(void)
707eda14cbcSMatt Macy {
708eda14cbcSMatt Macy 	return (MIN(dbuf_cache_max_bytes,
709eda14cbcSMatt Macy 	    arc_target_bytes() >> dbuf_cache_shift));
710eda14cbcSMatt Macy }
711eda14cbcSMatt Macy 
712eda14cbcSMatt Macy /*
713eda14cbcSMatt Macy  * The target size of the dbuf metadata cache can grow with the ARC target,
714eda14cbcSMatt Macy  * unless limited by the tunable dbuf_metadata_cache_max_bytes.
715eda14cbcSMatt Macy  */
716eda14cbcSMatt Macy static inline unsigned long
717eda14cbcSMatt Macy dbuf_metadata_cache_target_bytes(void)
718eda14cbcSMatt Macy {
719eda14cbcSMatt Macy 	return (MIN(dbuf_metadata_cache_max_bytes,
720eda14cbcSMatt Macy 	    arc_target_bytes() >> dbuf_metadata_cache_shift));
721eda14cbcSMatt Macy }
722eda14cbcSMatt Macy 
723eda14cbcSMatt Macy static inline uint64_t
724eda14cbcSMatt Macy dbuf_cache_hiwater_bytes(void)
725eda14cbcSMatt Macy {
726eda14cbcSMatt Macy 	uint64_t dbuf_cache_target = dbuf_cache_target_bytes();
727eda14cbcSMatt Macy 	return (dbuf_cache_target +
728eda14cbcSMatt Macy 	    (dbuf_cache_target * dbuf_cache_hiwater_pct) / 100);
729eda14cbcSMatt Macy }
730eda14cbcSMatt Macy 
731eda14cbcSMatt Macy static inline uint64_t
732eda14cbcSMatt Macy dbuf_cache_lowater_bytes(void)
733eda14cbcSMatt Macy {
734eda14cbcSMatt Macy 	uint64_t dbuf_cache_target = dbuf_cache_target_bytes();
735eda14cbcSMatt Macy 	return (dbuf_cache_target -
736eda14cbcSMatt Macy 	    (dbuf_cache_target * dbuf_cache_lowater_pct) / 100);
737eda14cbcSMatt Macy }
738eda14cbcSMatt Macy 
739eda14cbcSMatt Macy static inline boolean_t
740eda14cbcSMatt Macy dbuf_cache_above_lowater(void)
741eda14cbcSMatt Macy {
742eda14cbcSMatt Macy 	return (zfs_refcount_count(&dbuf_caches[DB_DBUF_CACHE].size) >
743eda14cbcSMatt Macy 	    dbuf_cache_lowater_bytes());
744eda14cbcSMatt Macy }
745eda14cbcSMatt Macy 
746eda14cbcSMatt Macy /*
747eda14cbcSMatt Macy  * Evict the oldest eligible dbuf from the dbuf cache.
748eda14cbcSMatt Macy  */
749eda14cbcSMatt Macy static void
750eda14cbcSMatt Macy dbuf_evict_one(void)
751eda14cbcSMatt Macy {
7523ff01b23SMartin Matuska 	int idx = multilist_get_random_index(&dbuf_caches[DB_DBUF_CACHE].cache);
753eda14cbcSMatt Macy 	multilist_sublist_t *mls = multilist_sublist_lock(
7543ff01b23SMartin Matuska 	    &dbuf_caches[DB_DBUF_CACHE].cache, idx);
755eda14cbcSMatt Macy 
756eda14cbcSMatt Macy 	ASSERT(!MUTEX_HELD(&dbuf_evict_lock));
757eda14cbcSMatt Macy 
758eda14cbcSMatt Macy 	dmu_buf_impl_t *db = multilist_sublist_tail(mls);
759eda14cbcSMatt Macy 	while (db != NULL && mutex_tryenter(&db->db_mtx) == 0) {
760eda14cbcSMatt Macy 		db = multilist_sublist_prev(mls, db);
761eda14cbcSMatt Macy 	}
762eda14cbcSMatt Macy 
763eda14cbcSMatt Macy 	DTRACE_PROBE2(dbuf__evict__one, dmu_buf_impl_t *, db,
764eda14cbcSMatt Macy 	    multilist_sublist_t *, mls);
765eda14cbcSMatt Macy 
766eda14cbcSMatt Macy 	if (db != NULL) {
767eda14cbcSMatt Macy 		multilist_sublist_remove(mls, db);
768eda14cbcSMatt Macy 		multilist_sublist_unlock(mls);
769eda14cbcSMatt Macy 		(void) zfs_refcount_remove_many(
770eda14cbcSMatt Macy 		    &dbuf_caches[DB_DBUF_CACHE].size, db->db.db_size, db);
771eda14cbcSMatt Macy 		DBUF_STAT_BUMPDOWN(cache_levels[db->db_level]);
772eda14cbcSMatt Macy 		DBUF_STAT_BUMPDOWN(cache_count);
773eda14cbcSMatt Macy 		DBUF_STAT_DECR(cache_levels_bytes[db->db_level],
774eda14cbcSMatt Macy 		    db->db.db_size);
775eda14cbcSMatt Macy 		ASSERT3U(db->db_caching_status, ==, DB_DBUF_CACHE);
776eda14cbcSMatt Macy 		db->db_caching_status = DB_NO_CACHE;
777eda14cbcSMatt Macy 		dbuf_destroy(db);
778eda14cbcSMatt Macy 		DBUF_STAT_BUMP(cache_total_evicts);
779eda14cbcSMatt Macy 	} else {
780eda14cbcSMatt Macy 		multilist_sublist_unlock(mls);
781eda14cbcSMatt Macy 	}
782eda14cbcSMatt Macy }
783eda14cbcSMatt Macy 
784eda14cbcSMatt Macy /*
785eda14cbcSMatt Macy  * The dbuf evict thread is responsible for aging out dbufs from the
786eda14cbcSMatt Macy  * cache. Once the cache has reached it's maximum size, dbufs are removed
787eda14cbcSMatt Macy  * and destroyed. The eviction thread will continue running until the size
788eda14cbcSMatt Macy  * of the dbuf cache is at or below the maximum size. Once the dbuf is aged
789eda14cbcSMatt Macy  * out of the cache it is destroyed and becomes eligible for arc eviction.
790eda14cbcSMatt Macy  */
791da5137abSMartin Matuska static __attribute__((noreturn)) void
792eda14cbcSMatt Macy dbuf_evict_thread(void *unused)
793eda14cbcSMatt Macy {
794e92ffd9bSMartin Matuska 	(void) unused;
795eda14cbcSMatt Macy 	callb_cpr_t cpr;
796eda14cbcSMatt Macy 
797eda14cbcSMatt Macy 	CALLB_CPR_INIT(&cpr, &dbuf_evict_lock, callb_generic_cpr, FTAG);
798eda14cbcSMatt Macy 
799eda14cbcSMatt Macy 	mutex_enter(&dbuf_evict_lock);
800eda14cbcSMatt Macy 	while (!dbuf_evict_thread_exit) {
801eda14cbcSMatt Macy 		while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit) {
802eda14cbcSMatt Macy 			CALLB_CPR_SAFE_BEGIN(&cpr);
8032c48331dSMatt Macy 			(void) cv_timedwait_idle_hires(&dbuf_evict_cv,
804eda14cbcSMatt Macy 			    &dbuf_evict_lock, SEC2NSEC(1), MSEC2NSEC(1), 0);
805eda14cbcSMatt Macy 			CALLB_CPR_SAFE_END(&cpr, &dbuf_evict_lock);
806eda14cbcSMatt Macy 		}
807eda14cbcSMatt Macy 		mutex_exit(&dbuf_evict_lock);
808eda14cbcSMatt Macy 
809eda14cbcSMatt Macy 		/*
810eda14cbcSMatt Macy 		 * Keep evicting as long as we're above the low water mark
811eda14cbcSMatt Macy 		 * for the cache. We do this without holding the locks to
812eda14cbcSMatt Macy 		 * minimize lock contention.
813eda14cbcSMatt Macy 		 */
814eda14cbcSMatt Macy 		while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit) {
815eda14cbcSMatt Macy 			dbuf_evict_one();
816eda14cbcSMatt Macy 		}
817eda14cbcSMatt Macy 
818eda14cbcSMatt Macy 		mutex_enter(&dbuf_evict_lock);
819eda14cbcSMatt Macy 	}
820eda14cbcSMatt Macy 
821eda14cbcSMatt Macy 	dbuf_evict_thread_exit = B_FALSE;
822eda14cbcSMatt Macy 	cv_broadcast(&dbuf_evict_cv);
823eda14cbcSMatt Macy 	CALLB_CPR_EXIT(&cpr);	/* drops dbuf_evict_lock */
824eda14cbcSMatt Macy 	thread_exit();
825eda14cbcSMatt Macy }
826eda14cbcSMatt Macy 
827eda14cbcSMatt Macy /*
828eda14cbcSMatt Macy  * Wake up the dbuf eviction thread if the dbuf cache is at its max size.
829eda14cbcSMatt Macy  * If the dbuf cache is at its high water mark, then evict a dbuf from the
830c03c5b1cSMartin Matuska  * dbuf cache using the caller's context.
831eda14cbcSMatt Macy  */
832eda14cbcSMatt Macy static void
833eda14cbcSMatt Macy dbuf_evict_notify(uint64_t size)
834eda14cbcSMatt Macy {
835eda14cbcSMatt Macy 	/*
836eda14cbcSMatt Macy 	 * We check if we should evict without holding the dbuf_evict_lock,
837eda14cbcSMatt Macy 	 * because it's OK to occasionally make the wrong decision here,
838eda14cbcSMatt Macy 	 * and grabbing the lock results in massive lock contention.
839eda14cbcSMatt Macy 	 */
840eda14cbcSMatt Macy 	if (size > dbuf_cache_target_bytes()) {
841eda14cbcSMatt Macy 		if (size > dbuf_cache_hiwater_bytes())
842eda14cbcSMatt Macy 			dbuf_evict_one();
843eda14cbcSMatt Macy 		cv_signal(&dbuf_evict_cv);
844eda14cbcSMatt Macy 	}
845eda14cbcSMatt Macy }
846eda14cbcSMatt Macy 
847eda14cbcSMatt Macy static int
848eda14cbcSMatt Macy dbuf_kstat_update(kstat_t *ksp, int rw)
849eda14cbcSMatt Macy {
850eda14cbcSMatt Macy 	dbuf_stats_t *ds = ksp->ks_data;
851be181ee2SMartin Matuska 	dbuf_hash_table_t *h = &dbuf_hash_table;
852eda14cbcSMatt Macy 
8530d8fe237SMartin Matuska 	if (rw == KSTAT_WRITE)
854eda14cbcSMatt Macy 		return (SET_ERROR(EACCES));
8550d8fe237SMartin Matuska 
8560d8fe237SMartin Matuska 	ds->cache_count.value.ui64 =
8570d8fe237SMartin Matuska 	    wmsum_value(&dbuf_sums.cache_count);
858eda14cbcSMatt Macy 	ds->cache_size_bytes.value.ui64 =
859eda14cbcSMatt Macy 	    zfs_refcount_count(&dbuf_caches[DB_DBUF_CACHE].size);
860eda14cbcSMatt Macy 	ds->cache_target_bytes.value.ui64 = dbuf_cache_target_bytes();
861eda14cbcSMatt Macy 	ds->cache_hiwater_bytes.value.ui64 = dbuf_cache_hiwater_bytes();
862eda14cbcSMatt Macy 	ds->cache_lowater_bytes.value.ui64 = dbuf_cache_lowater_bytes();
8630d8fe237SMartin Matuska 	ds->cache_total_evicts.value.ui64 =
8640d8fe237SMartin Matuska 	    wmsum_value(&dbuf_sums.cache_total_evicts);
8650d8fe237SMartin Matuska 	for (int i = 0; i < DN_MAX_LEVELS; i++) {
8660d8fe237SMartin Matuska 		ds->cache_levels[i].value.ui64 =
8670d8fe237SMartin Matuska 		    wmsum_value(&dbuf_sums.cache_levels[i]);
8680d8fe237SMartin Matuska 		ds->cache_levels_bytes[i].value.ui64 =
8690d8fe237SMartin Matuska 		    wmsum_value(&dbuf_sums.cache_levels_bytes[i]);
870eda14cbcSMatt Macy 	}
8710d8fe237SMartin Matuska 	ds->hash_hits.value.ui64 =
8720d8fe237SMartin Matuska 	    wmsum_value(&dbuf_sums.hash_hits);
8730d8fe237SMartin Matuska 	ds->hash_misses.value.ui64 =
8740d8fe237SMartin Matuska 	    wmsum_value(&dbuf_sums.hash_misses);
8750d8fe237SMartin Matuska 	ds->hash_collisions.value.ui64 =
8760d8fe237SMartin Matuska 	    wmsum_value(&dbuf_sums.hash_collisions);
8770d8fe237SMartin Matuska 	ds->hash_chains.value.ui64 =
8780d8fe237SMartin Matuska 	    wmsum_value(&dbuf_sums.hash_chains);
8790d8fe237SMartin Matuska 	ds->hash_insert_race.value.ui64 =
8800d8fe237SMartin Matuska 	    wmsum_value(&dbuf_sums.hash_insert_race);
881be181ee2SMartin Matuska 	ds->hash_table_count.value.ui64 = h->hash_table_mask + 1;
882be181ee2SMartin Matuska 	ds->hash_mutex_count.value.ui64 = h->hash_mutex_mask + 1;
8830d8fe237SMartin Matuska 	ds->metadata_cache_count.value.ui64 =
8840d8fe237SMartin Matuska 	    wmsum_value(&dbuf_sums.metadata_cache_count);
8850d8fe237SMartin Matuska 	ds->metadata_cache_size_bytes.value.ui64 = zfs_refcount_count(
8860d8fe237SMartin Matuska 	    &dbuf_caches[DB_DBUF_METADATA_CACHE].size);
8870d8fe237SMartin Matuska 	ds->metadata_cache_overflow.value.ui64 =
8880d8fe237SMartin Matuska 	    wmsum_value(&dbuf_sums.metadata_cache_overflow);
889eda14cbcSMatt Macy 	return (0);
890eda14cbcSMatt Macy }
891eda14cbcSMatt Macy 
892eda14cbcSMatt Macy void
893eda14cbcSMatt Macy dbuf_init(void)
894eda14cbcSMatt Macy {
895be181ee2SMartin Matuska 	uint64_t hmsize, hsize = 1ULL << 16;
896eda14cbcSMatt Macy 	dbuf_hash_table_t *h = &dbuf_hash_table;
897eda14cbcSMatt Macy 
898eda14cbcSMatt Macy 	/*
8997cd22ac4SMartin Matuska 	 * The hash table is big enough to fill one eighth of physical memory
900eda14cbcSMatt Macy 	 * with an average block size of zfs_arc_average_blocksize (default 8K).
901eda14cbcSMatt Macy 	 * By default, the table will take up
902eda14cbcSMatt Macy 	 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
903eda14cbcSMatt Macy 	 */
9047cd22ac4SMartin Matuska 	while (hsize * zfs_arc_average_blocksize < arc_all_memory() / 8)
905eda14cbcSMatt Macy 		hsize <<= 1;
906eda14cbcSMatt Macy 
907be181ee2SMartin Matuska 	h->hash_table = NULL;
908be181ee2SMartin Matuska 	while (h->hash_table == NULL) {
909eda14cbcSMatt Macy 		h->hash_table_mask = hsize - 1;
910be181ee2SMartin Matuska 
911eda14cbcSMatt Macy 		h->hash_table = vmem_zalloc(hsize * sizeof (void *), KM_SLEEP);
912be181ee2SMartin Matuska 		if (h->hash_table == NULL)
913eda14cbcSMatt Macy 			hsize >>= 1;
914be181ee2SMartin Matuska 
915be181ee2SMartin Matuska 		ASSERT3U(hsize, >=, 1ULL << 10);
916be181ee2SMartin Matuska 	}
917be181ee2SMartin Matuska 
918be181ee2SMartin Matuska 	/*
919be181ee2SMartin Matuska 	 * The hash table buckets are protected by an array of mutexes where
920be181ee2SMartin Matuska 	 * each mutex is reponsible for protecting 128 buckets.  A minimum
921be181ee2SMartin Matuska 	 * array size of 8192 is targeted to avoid contention.
922be181ee2SMartin Matuska 	 */
923be181ee2SMartin Matuska 	if (dbuf_mutex_cache_shift == 0)
924be181ee2SMartin Matuska 		hmsize = MAX(hsize >> 7, 1ULL << 13);
925be181ee2SMartin Matuska 	else
926be181ee2SMartin Matuska 		hmsize = 1ULL << MIN(dbuf_mutex_cache_shift, 24);
927be181ee2SMartin Matuska 
928be181ee2SMartin Matuska 	h->hash_mutexes = NULL;
929be181ee2SMartin Matuska 	while (h->hash_mutexes == NULL) {
930be181ee2SMartin Matuska 		h->hash_mutex_mask = hmsize - 1;
931be181ee2SMartin Matuska 
932be181ee2SMartin Matuska 		h->hash_mutexes = vmem_zalloc(hmsize * sizeof (kmutex_t),
933be181ee2SMartin Matuska 		    KM_SLEEP);
934be181ee2SMartin Matuska 		if (h->hash_mutexes == NULL)
935be181ee2SMartin Matuska 			hmsize >>= 1;
936eda14cbcSMatt Macy 	}
937eda14cbcSMatt Macy 
938eda14cbcSMatt Macy 	dbuf_kmem_cache = kmem_cache_create("dmu_buf_impl_t",
939eda14cbcSMatt Macy 	    sizeof (dmu_buf_impl_t),
940eda14cbcSMatt Macy 	    0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0);
941eda14cbcSMatt Macy 
942be181ee2SMartin Matuska 	for (int i = 0; i < hmsize; i++)
943be181ee2SMartin Matuska 		mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL);
944eda14cbcSMatt Macy 
945eda14cbcSMatt Macy 	dbuf_stats_init(h);
946eda14cbcSMatt Macy 
947eda14cbcSMatt Macy 	/*
948eda14cbcSMatt Macy 	 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
949eda14cbcSMatt Macy 	 * configuration is not required.
950eda14cbcSMatt Macy 	 */
951eda14cbcSMatt Macy 	dbu_evict_taskq = taskq_create("dbu_evict", 1, defclsyspri, 0, 0, 0);
952eda14cbcSMatt Macy 
953eda14cbcSMatt Macy 	for (dbuf_cached_state_t dcs = 0; dcs < DB_CACHE_MAX; dcs++) {
9543ff01b23SMartin Matuska 		multilist_create(&dbuf_caches[dcs].cache,
9553ff01b23SMartin Matuska 		    sizeof (dmu_buf_impl_t),
956eda14cbcSMatt Macy 		    offsetof(dmu_buf_impl_t, db_cache_link),
957eda14cbcSMatt Macy 		    dbuf_cache_multilist_index_func);
958eda14cbcSMatt Macy 		zfs_refcount_create(&dbuf_caches[dcs].size);
959eda14cbcSMatt Macy 	}
960eda14cbcSMatt Macy 
961eda14cbcSMatt Macy 	dbuf_evict_thread_exit = B_FALSE;
962eda14cbcSMatt Macy 	mutex_init(&dbuf_evict_lock, NULL, MUTEX_DEFAULT, NULL);
963eda14cbcSMatt Macy 	cv_init(&dbuf_evict_cv, NULL, CV_DEFAULT, NULL);
964eda14cbcSMatt Macy 	dbuf_cache_evict_thread = thread_create(NULL, 0, dbuf_evict_thread,
965eda14cbcSMatt Macy 	    NULL, 0, &p0, TS_RUN, minclsyspri);
966eda14cbcSMatt Macy 
9670d8fe237SMartin Matuska 	wmsum_init(&dbuf_sums.cache_count, 0);
9680d8fe237SMartin Matuska 	wmsum_init(&dbuf_sums.cache_total_evicts, 0);
969be181ee2SMartin Matuska 	for (int i = 0; i < DN_MAX_LEVELS; i++) {
9700d8fe237SMartin Matuska 		wmsum_init(&dbuf_sums.cache_levels[i], 0);
9710d8fe237SMartin Matuska 		wmsum_init(&dbuf_sums.cache_levels_bytes[i], 0);
9720d8fe237SMartin Matuska 	}
9730d8fe237SMartin Matuska 	wmsum_init(&dbuf_sums.hash_hits, 0);
9740d8fe237SMartin Matuska 	wmsum_init(&dbuf_sums.hash_misses, 0);
9750d8fe237SMartin Matuska 	wmsum_init(&dbuf_sums.hash_collisions, 0);
9760d8fe237SMartin Matuska 	wmsum_init(&dbuf_sums.hash_chains, 0);
9770d8fe237SMartin Matuska 	wmsum_init(&dbuf_sums.hash_insert_race, 0);
9780d8fe237SMartin Matuska 	wmsum_init(&dbuf_sums.metadata_cache_count, 0);
9790d8fe237SMartin Matuska 	wmsum_init(&dbuf_sums.metadata_cache_overflow, 0);
9800d8fe237SMartin Matuska 
981eda14cbcSMatt Macy 	dbuf_ksp = kstat_create("zfs", 0, "dbufstats", "misc",
982eda14cbcSMatt Macy 	    KSTAT_TYPE_NAMED, sizeof (dbuf_stats) / sizeof (kstat_named_t),
983eda14cbcSMatt Macy 	    KSTAT_FLAG_VIRTUAL);
984eda14cbcSMatt Macy 	if (dbuf_ksp != NULL) {
985be181ee2SMartin Matuska 		for (int i = 0; i < DN_MAX_LEVELS; i++) {
986eda14cbcSMatt Macy 			snprintf(dbuf_stats.cache_levels[i].name,
987eda14cbcSMatt Macy 			    KSTAT_STRLEN, "cache_level_%d", i);
988eda14cbcSMatt Macy 			dbuf_stats.cache_levels[i].data_type =
989eda14cbcSMatt Macy 			    KSTAT_DATA_UINT64;
990eda14cbcSMatt Macy 			snprintf(dbuf_stats.cache_levels_bytes[i].name,
991eda14cbcSMatt Macy 			    KSTAT_STRLEN, "cache_level_%d_bytes", i);
992eda14cbcSMatt Macy 			dbuf_stats.cache_levels_bytes[i].data_type =
993eda14cbcSMatt Macy 			    KSTAT_DATA_UINT64;
994eda14cbcSMatt Macy 		}
995eda14cbcSMatt Macy 		dbuf_ksp->ks_data = &dbuf_stats;
996eda14cbcSMatt Macy 		dbuf_ksp->ks_update = dbuf_kstat_update;
997eda14cbcSMatt Macy 		kstat_install(dbuf_ksp);
998eda14cbcSMatt Macy 	}
999eda14cbcSMatt Macy }
1000eda14cbcSMatt Macy 
1001eda14cbcSMatt Macy void
1002eda14cbcSMatt Macy dbuf_fini(void)
1003eda14cbcSMatt Macy {
1004eda14cbcSMatt Macy 	dbuf_hash_table_t *h = &dbuf_hash_table;
1005eda14cbcSMatt Macy 
1006eda14cbcSMatt Macy 	dbuf_stats_destroy();
1007eda14cbcSMatt Macy 
1008be181ee2SMartin Matuska 	for (int i = 0; i < (h->hash_mutex_mask + 1); i++)
1009be181ee2SMartin Matuska 		mutex_destroy(&h->hash_mutexes[i]);
1010be181ee2SMartin Matuska 
1011eda14cbcSMatt Macy 	vmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
1012be181ee2SMartin Matuska 	vmem_free(h->hash_mutexes, (h->hash_mutex_mask + 1) *
1013be181ee2SMartin Matuska 	    sizeof (kmutex_t));
1014be181ee2SMartin Matuska 
1015eda14cbcSMatt Macy 	kmem_cache_destroy(dbuf_kmem_cache);
1016eda14cbcSMatt Macy 	taskq_destroy(dbu_evict_taskq);
1017eda14cbcSMatt Macy 
1018eda14cbcSMatt Macy 	mutex_enter(&dbuf_evict_lock);
1019eda14cbcSMatt Macy 	dbuf_evict_thread_exit = B_TRUE;
1020eda14cbcSMatt Macy 	while (dbuf_evict_thread_exit) {
1021eda14cbcSMatt Macy 		cv_signal(&dbuf_evict_cv);
1022eda14cbcSMatt Macy 		cv_wait(&dbuf_evict_cv, &dbuf_evict_lock);
1023eda14cbcSMatt Macy 	}
1024eda14cbcSMatt Macy 	mutex_exit(&dbuf_evict_lock);
1025eda14cbcSMatt Macy 
1026eda14cbcSMatt Macy 	mutex_destroy(&dbuf_evict_lock);
1027eda14cbcSMatt Macy 	cv_destroy(&dbuf_evict_cv);
1028eda14cbcSMatt Macy 
1029eda14cbcSMatt Macy 	for (dbuf_cached_state_t dcs = 0; dcs < DB_CACHE_MAX; dcs++) {
1030eda14cbcSMatt Macy 		zfs_refcount_destroy(&dbuf_caches[dcs].size);
10313ff01b23SMartin Matuska 		multilist_destroy(&dbuf_caches[dcs].cache);
1032eda14cbcSMatt Macy 	}
1033eda14cbcSMatt Macy 
1034eda14cbcSMatt Macy 	if (dbuf_ksp != NULL) {
1035eda14cbcSMatt Macy 		kstat_delete(dbuf_ksp);
1036eda14cbcSMatt Macy 		dbuf_ksp = NULL;
1037eda14cbcSMatt Macy 	}
10380d8fe237SMartin Matuska 
10390d8fe237SMartin Matuska 	wmsum_fini(&dbuf_sums.cache_count);
10400d8fe237SMartin Matuska 	wmsum_fini(&dbuf_sums.cache_total_evicts);
1041be181ee2SMartin Matuska 	for (int i = 0; i < DN_MAX_LEVELS; i++) {
10420d8fe237SMartin Matuska 		wmsum_fini(&dbuf_sums.cache_levels[i]);
10430d8fe237SMartin Matuska 		wmsum_fini(&dbuf_sums.cache_levels_bytes[i]);
10440d8fe237SMartin Matuska 	}
10450d8fe237SMartin Matuska 	wmsum_fini(&dbuf_sums.hash_hits);
10460d8fe237SMartin Matuska 	wmsum_fini(&dbuf_sums.hash_misses);
10470d8fe237SMartin Matuska 	wmsum_fini(&dbuf_sums.hash_collisions);
10480d8fe237SMartin Matuska 	wmsum_fini(&dbuf_sums.hash_chains);
10490d8fe237SMartin Matuska 	wmsum_fini(&dbuf_sums.hash_insert_race);
10500d8fe237SMartin Matuska 	wmsum_fini(&dbuf_sums.metadata_cache_count);
10510d8fe237SMartin Matuska 	wmsum_fini(&dbuf_sums.metadata_cache_overflow);
1052eda14cbcSMatt Macy }
1053eda14cbcSMatt Macy 
1054eda14cbcSMatt Macy /*
1055eda14cbcSMatt Macy  * Other stuff.
1056eda14cbcSMatt Macy  */
1057eda14cbcSMatt Macy 
1058eda14cbcSMatt Macy #ifdef ZFS_DEBUG
1059eda14cbcSMatt Macy static void
1060eda14cbcSMatt Macy dbuf_verify(dmu_buf_impl_t *db)
1061eda14cbcSMatt Macy {
1062eda14cbcSMatt Macy 	dnode_t *dn;
1063eda14cbcSMatt Macy 	dbuf_dirty_record_t *dr;
1064eda14cbcSMatt Macy 	uint32_t txg_prev;
1065eda14cbcSMatt Macy 
1066eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&db->db_mtx));
1067eda14cbcSMatt Macy 
1068eda14cbcSMatt Macy 	if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY))
1069eda14cbcSMatt Macy 		return;
1070eda14cbcSMatt Macy 
1071eda14cbcSMatt Macy 	ASSERT(db->db_objset != NULL);
1072eda14cbcSMatt Macy 	DB_DNODE_ENTER(db);
1073eda14cbcSMatt Macy 	dn = DB_DNODE(db);
1074eda14cbcSMatt Macy 	if (dn == NULL) {
1075eda14cbcSMatt Macy 		ASSERT(db->db_parent == NULL);
1076eda14cbcSMatt Macy 		ASSERT(db->db_blkptr == NULL);
1077eda14cbcSMatt Macy 	} else {
1078eda14cbcSMatt Macy 		ASSERT3U(db->db.db_object, ==, dn->dn_object);
1079eda14cbcSMatt Macy 		ASSERT3P(db->db_objset, ==, dn->dn_objset);
1080eda14cbcSMatt Macy 		ASSERT3U(db->db_level, <, dn->dn_nlevels);
1081eda14cbcSMatt Macy 		ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
1082eda14cbcSMatt Macy 		    db->db_blkid == DMU_SPILL_BLKID ||
1083eda14cbcSMatt Macy 		    !avl_is_empty(&dn->dn_dbufs));
1084eda14cbcSMatt Macy 	}
1085eda14cbcSMatt Macy 	if (db->db_blkid == DMU_BONUS_BLKID) {
1086eda14cbcSMatt Macy 		ASSERT(dn != NULL);
1087eda14cbcSMatt Macy 		ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
1088eda14cbcSMatt Macy 		ASSERT3U(db->db.db_offset, ==, DMU_BONUS_BLKID);
1089eda14cbcSMatt Macy 	} else if (db->db_blkid == DMU_SPILL_BLKID) {
1090eda14cbcSMatt Macy 		ASSERT(dn != NULL);
1091eda14cbcSMatt Macy 		ASSERT0(db->db.db_offset);
1092eda14cbcSMatt Macy 	} else {
1093eda14cbcSMatt Macy 		ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size);
1094eda14cbcSMatt Macy 	}
1095eda14cbcSMatt Macy 
1096eda14cbcSMatt Macy 	if ((dr = list_head(&db->db_dirty_records)) != NULL) {
1097eda14cbcSMatt Macy 		ASSERT(dr->dr_dbuf == db);
1098eda14cbcSMatt Macy 		txg_prev = dr->dr_txg;
1099eda14cbcSMatt Macy 		for (dr = list_next(&db->db_dirty_records, dr); dr != NULL;
1100eda14cbcSMatt Macy 		    dr = list_next(&db->db_dirty_records, dr)) {
1101eda14cbcSMatt Macy 			ASSERT(dr->dr_dbuf == db);
1102eda14cbcSMatt Macy 			ASSERT(txg_prev > dr->dr_txg);
1103eda14cbcSMatt Macy 			txg_prev = dr->dr_txg;
1104eda14cbcSMatt Macy 		}
1105eda14cbcSMatt Macy 	}
1106eda14cbcSMatt Macy 
1107eda14cbcSMatt Macy 	/*
1108eda14cbcSMatt Macy 	 * We can't assert that db_size matches dn_datablksz because it
1109eda14cbcSMatt Macy 	 * can be momentarily different when another thread is doing
1110eda14cbcSMatt Macy 	 * dnode_set_blksz().
1111eda14cbcSMatt Macy 	 */
1112eda14cbcSMatt Macy 	if (db->db_level == 0 && db->db.db_object == DMU_META_DNODE_OBJECT) {
1113eda14cbcSMatt Macy 		dr = db->db_data_pending;
1114eda14cbcSMatt Macy 		/*
1115eda14cbcSMatt Macy 		 * It should only be modified in syncing context, so
1116eda14cbcSMatt Macy 		 * make sure we only have one copy of the data.
1117eda14cbcSMatt Macy 		 */
1118eda14cbcSMatt Macy 		ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf);
1119eda14cbcSMatt Macy 	}
1120eda14cbcSMatt Macy 
1121eda14cbcSMatt Macy 	/* verify db->db_blkptr */
1122eda14cbcSMatt Macy 	if (db->db_blkptr) {
1123eda14cbcSMatt Macy 		if (db->db_parent == dn->dn_dbuf) {
1124eda14cbcSMatt Macy 			/* db is pointed to by the dnode */
1125eda14cbcSMatt Macy 			/* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
1126eda14cbcSMatt Macy 			if (DMU_OBJECT_IS_SPECIAL(db->db.db_object))
1127eda14cbcSMatt Macy 				ASSERT(db->db_parent == NULL);
1128eda14cbcSMatt Macy 			else
1129eda14cbcSMatt Macy 				ASSERT(db->db_parent != NULL);
1130eda14cbcSMatt Macy 			if (db->db_blkid != DMU_SPILL_BLKID)
1131eda14cbcSMatt Macy 				ASSERT3P(db->db_blkptr, ==,
1132eda14cbcSMatt Macy 				    &dn->dn_phys->dn_blkptr[db->db_blkid]);
1133eda14cbcSMatt Macy 		} else {
1134eda14cbcSMatt Macy 			/* db is pointed to by an indirect block */
1135eda14cbcSMatt Macy 			int epb __maybe_unused = db->db_parent->db.db_size >>
1136eda14cbcSMatt Macy 			    SPA_BLKPTRSHIFT;
1137eda14cbcSMatt Macy 			ASSERT3U(db->db_parent->db_level, ==, db->db_level+1);
1138eda14cbcSMatt Macy 			ASSERT3U(db->db_parent->db.db_object, ==,
1139eda14cbcSMatt Macy 			    db->db.db_object);
1140eda14cbcSMatt Macy 			/*
1141eda14cbcSMatt Macy 			 * dnode_grow_indblksz() can make this fail if we don't
1142eda14cbcSMatt Macy 			 * have the parent's rwlock.  XXX indblksz no longer
1143eda14cbcSMatt Macy 			 * grows.  safe to do this now?
1144eda14cbcSMatt Macy 			 */
1145eda14cbcSMatt Macy 			if (RW_LOCK_HELD(&db->db_parent->db_rwlock)) {
1146eda14cbcSMatt Macy 				ASSERT3P(db->db_blkptr, ==,
1147eda14cbcSMatt Macy 				    ((blkptr_t *)db->db_parent->db.db_data +
1148eda14cbcSMatt Macy 				    db->db_blkid % epb));
1149eda14cbcSMatt Macy 			}
1150eda14cbcSMatt Macy 		}
1151eda14cbcSMatt Macy 	}
1152eda14cbcSMatt Macy 	if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) &&
1153eda14cbcSMatt Macy 	    (db->db_buf == NULL || db->db_buf->b_data) &&
1154eda14cbcSMatt Macy 	    db->db.db_data && db->db_blkid != DMU_BONUS_BLKID &&
1155eda14cbcSMatt Macy 	    db->db_state != DB_FILL && !dn->dn_free_txg) {
1156eda14cbcSMatt Macy 		/*
1157eda14cbcSMatt Macy 		 * If the blkptr isn't set but they have nonzero data,
1158eda14cbcSMatt Macy 		 * it had better be dirty, otherwise we'll lose that
1159eda14cbcSMatt Macy 		 * data when we evict this buffer.
1160eda14cbcSMatt Macy 		 *
1161eda14cbcSMatt Macy 		 * There is an exception to this rule for indirect blocks; in
1162eda14cbcSMatt Macy 		 * this case, if the indirect block is a hole, we fill in a few
1163eda14cbcSMatt Macy 		 * fields on each of the child blocks (importantly, birth time)
1164eda14cbcSMatt Macy 		 * to prevent hole birth times from being lost when you
1165eda14cbcSMatt Macy 		 * partially fill in a hole.
1166eda14cbcSMatt Macy 		 */
1167eda14cbcSMatt Macy 		if (db->db_dirtycnt == 0) {
1168eda14cbcSMatt Macy 			if (db->db_level == 0) {
1169eda14cbcSMatt Macy 				uint64_t *buf = db->db.db_data;
1170eda14cbcSMatt Macy 				int i;
1171eda14cbcSMatt Macy 
1172eda14cbcSMatt Macy 				for (i = 0; i < db->db.db_size >> 3; i++) {
1173eda14cbcSMatt Macy 					ASSERT(buf[i] == 0);
1174eda14cbcSMatt Macy 				}
1175eda14cbcSMatt Macy 			} else {
1176eda14cbcSMatt Macy 				blkptr_t *bps = db->db.db_data;
1177eda14cbcSMatt Macy 				ASSERT3U(1 << DB_DNODE(db)->dn_indblkshift, ==,
1178eda14cbcSMatt Macy 				    db->db.db_size);
1179eda14cbcSMatt Macy 				/*
1180eda14cbcSMatt Macy 				 * We want to verify that all the blkptrs in the
1181eda14cbcSMatt Macy 				 * indirect block are holes, but we may have
1182eda14cbcSMatt Macy 				 * automatically set up a few fields for them.
1183eda14cbcSMatt Macy 				 * We iterate through each blkptr and verify
1184eda14cbcSMatt Macy 				 * they only have those fields set.
1185eda14cbcSMatt Macy 				 */
1186eda14cbcSMatt Macy 				for (int i = 0;
1187eda14cbcSMatt Macy 				    i < db->db.db_size / sizeof (blkptr_t);
1188eda14cbcSMatt Macy 				    i++) {
1189eda14cbcSMatt Macy 					blkptr_t *bp = &bps[i];
1190eda14cbcSMatt Macy 					ASSERT(ZIO_CHECKSUM_IS_ZERO(
1191eda14cbcSMatt Macy 					    &bp->blk_cksum));
1192eda14cbcSMatt Macy 					ASSERT(
1193eda14cbcSMatt Macy 					    DVA_IS_EMPTY(&bp->blk_dva[0]) &&
1194eda14cbcSMatt Macy 					    DVA_IS_EMPTY(&bp->blk_dva[1]) &&
1195eda14cbcSMatt Macy 					    DVA_IS_EMPTY(&bp->blk_dva[2]));
1196eda14cbcSMatt Macy 					ASSERT0(bp->blk_fill);
1197eda14cbcSMatt Macy 					ASSERT0(bp->blk_pad[0]);
1198eda14cbcSMatt Macy 					ASSERT0(bp->blk_pad[1]);
1199eda14cbcSMatt Macy 					ASSERT(!BP_IS_EMBEDDED(bp));
1200eda14cbcSMatt Macy 					ASSERT(BP_IS_HOLE(bp));
1201eda14cbcSMatt Macy 					ASSERT0(bp->blk_phys_birth);
1202eda14cbcSMatt Macy 				}
1203eda14cbcSMatt Macy 			}
1204eda14cbcSMatt Macy 		}
1205eda14cbcSMatt Macy 	}
1206eda14cbcSMatt Macy 	DB_DNODE_EXIT(db);
1207eda14cbcSMatt Macy }
1208eda14cbcSMatt Macy #endif
1209eda14cbcSMatt Macy 
1210eda14cbcSMatt Macy static void
1211eda14cbcSMatt Macy dbuf_clear_data(dmu_buf_impl_t *db)
1212eda14cbcSMatt Macy {
1213eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&db->db_mtx));
1214eda14cbcSMatt Macy 	dbuf_evict_user(db);
1215eda14cbcSMatt Macy 	ASSERT3P(db->db_buf, ==, NULL);
1216eda14cbcSMatt Macy 	db->db.db_data = NULL;
1217eda14cbcSMatt Macy 	if (db->db_state != DB_NOFILL) {
1218eda14cbcSMatt Macy 		db->db_state = DB_UNCACHED;
1219eda14cbcSMatt Macy 		DTRACE_SET_STATE(db, "clear data");
1220eda14cbcSMatt Macy 	}
1221eda14cbcSMatt Macy }
1222eda14cbcSMatt Macy 
1223eda14cbcSMatt Macy static void
1224eda14cbcSMatt Macy dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf)
1225eda14cbcSMatt Macy {
1226eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&db->db_mtx));
1227eda14cbcSMatt Macy 	ASSERT(buf != NULL);
1228eda14cbcSMatt Macy 
1229eda14cbcSMatt Macy 	db->db_buf = buf;
1230eda14cbcSMatt Macy 	ASSERT(buf->b_data != NULL);
1231eda14cbcSMatt Macy 	db->db.db_data = buf->b_data;
1232eda14cbcSMatt Macy }
1233eda14cbcSMatt Macy 
1234eda14cbcSMatt Macy static arc_buf_t *
1235eda14cbcSMatt Macy dbuf_alloc_arcbuf(dmu_buf_impl_t *db)
1236eda14cbcSMatt Macy {
1237eda14cbcSMatt Macy 	spa_t *spa = db->db_objset->os_spa;
1238eda14cbcSMatt Macy 
1239eda14cbcSMatt Macy 	return (arc_alloc_buf(spa, db, DBUF_GET_BUFC_TYPE(db), db->db.db_size));
1240eda14cbcSMatt Macy }
1241eda14cbcSMatt Macy 
1242eda14cbcSMatt Macy /*
1243eda14cbcSMatt Macy  * Loan out an arc_buf for read.  Return the loaned arc_buf.
1244eda14cbcSMatt Macy  */
1245eda14cbcSMatt Macy arc_buf_t *
1246eda14cbcSMatt Macy dbuf_loan_arcbuf(dmu_buf_impl_t *db)
1247eda14cbcSMatt Macy {
1248eda14cbcSMatt Macy 	arc_buf_t *abuf;
1249eda14cbcSMatt Macy 
1250eda14cbcSMatt Macy 	ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1251eda14cbcSMatt Macy 	mutex_enter(&db->db_mtx);
1252eda14cbcSMatt Macy 	if (arc_released(db->db_buf) || zfs_refcount_count(&db->db_holds) > 1) {
1253eda14cbcSMatt Macy 		int blksz = db->db.db_size;
1254eda14cbcSMatt Macy 		spa_t *spa = db->db_objset->os_spa;
1255eda14cbcSMatt Macy 
1256eda14cbcSMatt Macy 		mutex_exit(&db->db_mtx);
1257eda14cbcSMatt Macy 		abuf = arc_loan_buf(spa, B_FALSE, blksz);
1258da5137abSMartin Matuska 		memcpy(abuf->b_data, db->db.db_data, blksz);
1259eda14cbcSMatt Macy 	} else {
1260eda14cbcSMatt Macy 		abuf = db->db_buf;
1261eda14cbcSMatt Macy 		arc_loan_inuse_buf(abuf, db);
1262eda14cbcSMatt Macy 		db->db_buf = NULL;
1263eda14cbcSMatt Macy 		dbuf_clear_data(db);
1264eda14cbcSMatt Macy 		mutex_exit(&db->db_mtx);
1265eda14cbcSMatt Macy 	}
1266eda14cbcSMatt Macy 	return (abuf);
1267eda14cbcSMatt Macy }
1268eda14cbcSMatt Macy 
1269eda14cbcSMatt Macy /*
1270eda14cbcSMatt Macy  * Calculate which level n block references the data at the level 0 offset
1271eda14cbcSMatt Macy  * provided.
1272eda14cbcSMatt Macy  */
1273eda14cbcSMatt Macy uint64_t
1274eda14cbcSMatt Macy dbuf_whichblock(const dnode_t *dn, const int64_t level, const uint64_t offset)
1275eda14cbcSMatt Macy {
1276eda14cbcSMatt Macy 	if (dn->dn_datablkshift != 0 && dn->dn_indblkshift != 0) {
1277eda14cbcSMatt Macy 		/*
1278eda14cbcSMatt Macy 		 * The level n blkid is equal to the level 0 blkid divided by
1279eda14cbcSMatt Macy 		 * the number of level 0s in a level n block.
1280eda14cbcSMatt Macy 		 *
1281eda14cbcSMatt Macy 		 * The level 0 blkid is offset >> datablkshift =
1282eda14cbcSMatt Macy 		 * offset / 2^datablkshift.
1283eda14cbcSMatt Macy 		 *
1284eda14cbcSMatt Macy 		 * The number of level 0s in a level n is the number of block
1285eda14cbcSMatt Macy 		 * pointers in an indirect block, raised to the power of level.
1286eda14cbcSMatt Macy 		 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
1287eda14cbcSMatt Macy 		 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
1288eda14cbcSMatt Macy 		 *
1289eda14cbcSMatt Macy 		 * Thus, the level n blkid is: offset /
1290eda14cbcSMatt Macy 		 * ((2^datablkshift)*(2^(level*(indblkshift-SPA_BLKPTRSHIFT))))
1291eda14cbcSMatt Macy 		 * = offset / 2^(datablkshift + level *
1292eda14cbcSMatt Macy 		 *   (indblkshift - SPA_BLKPTRSHIFT))
1293eda14cbcSMatt Macy 		 * = offset >> (datablkshift + level *
1294eda14cbcSMatt Macy 		 *   (indblkshift - SPA_BLKPTRSHIFT))
1295eda14cbcSMatt Macy 		 */
1296eda14cbcSMatt Macy 
1297eda14cbcSMatt Macy 		const unsigned exp = dn->dn_datablkshift +
1298eda14cbcSMatt Macy 		    level * (dn->dn_indblkshift - SPA_BLKPTRSHIFT);
1299eda14cbcSMatt Macy 
1300eda14cbcSMatt Macy 		if (exp >= 8 * sizeof (offset)) {
1301eda14cbcSMatt Macy 			/* This only happens on the highest indirection level */
1302eda14cbcSMatt Macy 			ASSERT3U(level, ==, dn->dn_nlevels - 1);
1303eda14cbcSMatt Macy 			return (0);
1304eda14cbcSMatt Macy 		}
1305eda14cbcSMatt Macy 
1306eda14cbcSMatt Macy 		ASSERT3U(exp, <, 8 * sizeof (offset));
1307eda14cbcSMatt Macy 
1308eda14cbcSMatt Macy 		return (offset >> exp);
1309eda14cbcSMatt Macy 	} else {
1310eda14cbcSMatt Macy 		ASSERT3U(offset, <, dn->dn_datablksz);
1311eda14cbcSMatt Macy 		return (0);
1312eda14cbcSMatt Macy 	}
1313eda14cbcSMatt Macy }
1314eda14cbcSMatt Macy 
1315eda14cbcSMatt Macy /*
1316eda14cbcSMatt Macy  * This function is used to lock the parent of the provided dbuf. This should be
1317eda14cbcSMatt Macy  * used when modifying or reading db_blkptr.
1318eda14cbcSMatt Macy  */
1319eda14cbcSMatt Macy db_lock_type_t
1320a0b956f5SMartin Matuska dmu_buf_lock_parent(dmu_buf_impl_t *db, krw_t rw, const void *tag)
1321eda14cbcSMatt Macy {
1322eda14cbcSMatt Macy 	enum db_lock_type ret = DLT_NONE;
1323eda14cbcSMatt Macy 	if (db->db_parent != NULL) {
1324eda14cbcSMatt Macy 		rw_enter(&db->db_parent->db_rwlock, rw);
1325eda14cbcSMatt Macy 		ret = DLT_PARENT;
1326eda14cbcSMatt Macy 	} else if (dmu_objset_ds(db->db_objset) != NULL) {
1327eda14cbcSMatt Macy 		rrw_enter(&dmu_objset_ds(db->db_objset)->ds_bp_rwlock, rw,
1328eda14cbcSMatt Macy 		    tag);
1329eda14cbcSMatt Macy 		ret = DLT_OBJSET;
1330eda14cbcSMatt Macy 	}
1331eda14cbcSMatt Macy 	/*
1332eda14cbcSMatt Macy 	 * We only return a DLT_NONE lock when it's the top-most indirect block
1333eda14cbcSMatt Macy 	 * of the meta-dnode of the MOS.
1334eda14cbcSMatt Macy 	 */
1335eda14cbcSMatt Macy 	return (ret);
1336eda14cbcSMatt Macy }
1337eda14cbcSMatt Macy 
1338eda14cbcSMatt Macy /*
1339eda14cbcSMatt Macy  * We need to pass the lock type in because it's possible that the block will
1340eda14cbcSMatt Macy  * move from being the topmost indirect block in a dnode (and thus, have no
1341eda14cbcSMatt Macy  * parent) to not the top-most via an indirection increase. This would cause a
1342eda14cbcSMatt Macy  * panic if we didn't pass the lock type in.
1343eda14cbcSMatt Macy  */
1344eda14cbcSMatt Macy void
1345a0b956f5SMartin Matuska dmu_buf_unlock_parent(dmu_buf_impl_t *db, db_lock_type_t type, const void *tag)
1346eda14cbcSMatt Macy {
1347eda14cbcSMatt Macy 	if (type == DLT_PARENT)
1348eda14cbcSMatt Macy 		rw_exit(&db->db_parent->db_rwlock);
1349eda14cbcSMatt Macy 	else if (type == DLT_OBJSET)
1350eda14cbcSMatt Macy 		rrw_exit(&dmu_objset_ds(db->db_objset)->ds_bp_rwlock, tag);
1351eda14cbcSMatt Macy }
1352eda14cbcSMatt Macy 
1353eda14cbcSMatt Macy static void
1354eda14cbcSMatt Macy dbuf_read_done(zio_t *zio, const zbookmark_phys_t *zb, const blkptr_t *bp,
1355eda14cbcSMatt Macy     arc_buf_t *buf, void *vdb)
1356eda14cbcSMatt Macy {
1357e92ffd9bSMartin Matuska 	(void) zb, (void) bp;
1358eda14cbcSMatt Macy 	dmu_buf_impl_t *db = vdb;
1359eda14cbcSMatt Macy 
1360eda14cbcSMatt Macy 	mutex_enter(&db->db_mtx);
1361eda14cbcSMatt Macy 	ASSERT3U(db->db_state, ==, DB_READ);
1362eda14cbcSMatt Macy 	/*
1363eda14cbcSMatt Macy 	 * All reads are synchronous, so we must have a hold on the dbuf
1364eda14cbcSMatt Macy 	 */
1365eda14cbcSMatt Macy 	ASSERT(zfs_refcount_count(&db->db_holds) > 0);
1366eda14cbcSMatt Macy 	ASSERT(db->db_buf == NULL);
1367eda14cbcSMatt Macy 	ASSERT(db->db.db_data == NULL);
1368eda14cbcSMatt Macy 	if (buf == NULL) {
1369eda14cbcSMatt Macy 		/* i/o error */
1370eda14cbcSMatt Macy 		ASSERT(zio == NULL || zio->io_error != 0);
1371eda14cbcSMatt Macy 		ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1372eda14cbcSMatt Macy 		ASSERT3P(db->db_buf, ==, NULL);
1373eda14cbcSMatt Macy 		db->db_state = DB_UNCACHED;
1374eda14cbcSMatt Macy 		DTRACE_SET_STATE(db, "i/o error");
1375eda14cbcSMatt Macy 	} else if (db->db_level == 0 && db->db_freed_in_flight) {
1376eda14cbcSMatt Macy 		/* freed in flight */
1377eda14cbcSMatt Macy 		ASSERT(zio == NULL || zio->io_error == 0);
1378eda14cbcSMatt Macy 		arc_release(buf, db);
1379da5137abSMartin Matuska 		memset(buf->b_data, 0, db->db.db_size);
1380eda14cbcSMatt Macy 		arc_buf_freeze(buf);
1381eda14cbcSMatt Macy 		db->db_freed_in_flight = FALSE;
1382eda14cbcSMatt Macy 		dbuf_set_data(db, buf);
1383eda14cbcSMatt Macy 		db->db_state = DB_CACHED;
1384eda14cbcSMatt Macy 		DTRACE_SET_STATE(db, "freed in flight");
1385eda14cbcSMatt Macy 	} else {
1386eda14cbcSMatt Macy 		/* success */
1387eda14cbcSMatt Macy 		ASSERT(zio == NULL || zio->io_error == 0);
1388eda14cbcSMatt Macy 		dbuf_set_data(db, buf);
1389eda14cbcSMatt Macy 		db->db_state = DB_CACHED;
1390eda14cbcSMatt Macy 		DTRACE_SET_STATE(db, "successful read");
1391eda14cbcSMatt Macy 	}
1392eda14cbcSMatt Macy 	cv_broadcast(&db->db_changed);
1393eda14cbcSMatt Macy 	dbuf_rele_and_unlock(db, NULL, B_FALSE);
1394eda14cbcSMatt Macy }
1395eda14cbcSMatt Macy 
1396eda14cbcSMatt Macy /*
1397eda14cbcSMatt Macy  * Shortcut for performing reads on bonus dbufs.  Returns
1398eda14cbcSMatt Macy  * an error if we fail to verify the dnode associated with
1399eda14cbcSMatt Macy  * a decrypted block. Otherwise success.
1400eda14cbcSMatt Macy  */
1401eda14cbcSMatt Macy static int
1402eda14cbcSMatt Macy dbuf_read_bonus(dmu_buf_impl_t *db, dnode_t *dn, uint32_t flags)
1403eda14cbcSMatt Macy {
1404eda14cbcSMatt Macy 	int bonuslen, max_bonuslen, err;
1405eda14cbcSMatt Macy 
1406eda14cbcSMatt Macy 	err = dbuf_read_verify_dnode_crypt(db, flags);
1407eda14cbcSMatt Macy 	if (err)
1408eda14cbcSMatt Macy 		return (err);
1409eda14cbcSMatt Macy 
1410eda14cbcSMatt Macy 	bonuslen = MIN(dn->dn_bonuslen, dn->dn_phys->dn_bonuslen);
1411eda14cbcSMatt Macy 	max_bonuslen = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots);
1412eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&db->db_mtx));
1413eda14cbcSMatt Macy 	ASSERT(DB_DNODE_HELD(db));
1414eda14cbcSMatt Macy 	ASSERT3U(bonuslen, <=, db->db.db_size);
1415eda14cbcSMatt Macy 	db->db.db_data = kmem_alloc(max_bonuslen, KM_SLEEP);
1416eda14cbcSMatt Macy 	arc_space_consume(max_bonuslen, ARC_SPACE_BONUS);
1417eda14cbcSMatt Macy 	if (bonuslen < max_bonuslen)
1418da5137abSMartin Matuska 		memset(db->db.db_data, 0, max_bonuslen);
1419eda14cbcSMatt Macy 	if (bonuslen)
1420da5137abSMartin Matuska 		memcpy(db->db.db_data, DN_BONUS(dn->dn_phys), bonuslen);
1421eda14cbcSMatt Macy 	db->db_state = DB_CACHED;
1422eda14cbcSMatt Macy 	DTRACE_SET_STATE(db, "bonus buffer filled");
1423eda14cbcSMatt Macy 	return (0);
1424eda14cbcSMatt Macy }
1425eda14cbcSMatt Macy 
1426eda14cbcSMatt Macy static void
1427eda14cbcSMatt Macy dbuf_handle_indirect_hole(dmu_buf_impl_t *db, dnode_t *dn)
1428eda14cbcSMatt Macy {
1429eda14cbcSMatt Macy 	blkptr_t *bps = db->db.db_data;
1430eda14cbcSMatt Macy 	uint32_t indbs = 1ULL << dn->dn_indblkshift;
1431eda14cbcSMatt Macy 	int n_bps = indbs >> SPA_BLKPTRSHIFT;
1432eda14cbcSMatt Macy 
1433eda14cbcSMatt Macy 	for (int i = 0; i < n_bps; i++) {
1434eda14cbcSMatt Macy 		blkptr_t *bp = &bps[i];
1435eda14cbcSMatt Macy 
1436eda14cbcSMatt Macy 		ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==, indbs);
1437eda14cbcSMatt Macy 		BP_SET_LSIZE(bp, BP_GET_LEVEL(db->db_blkptr) == 1 ?
1438eda14cbcSMatt Macy 		    dn->dn_datablksz : BP_GET_LSIZE(db->db_blkptr));
1439eda14cbcSMatt Macy 		BP_SET_TYPE(bp, BP_GET_TYPE(db->db_blkptr));
1440eda14cbcSMatt Macy 		BP_SET_LEVEL(bp, BP_GET_LEVEL(db->db_blkptr) - 1);
1441eda14cbcSMatt Macy 		BP_SET_BIRTH(bp, db->db_blkptr->blk_birth, 0);
1442eda14cbcSMatt Macy 	}
1443eda14cbcSMatt Macy }
1444eda14cbcSMatt Macy 
1445eda14cbcSMatt Macy /*
1446eda14cbcSMatt Macy  * Handle reads on dbufs that are holes, if necessary.  This function
1447eda14cbcSMatt Macy  * requires that the dbuf's mutex is held. Returns success (0) if action
1448eda14cbcSMatt Macy  * was taken, ENOENT if no action was taken.
1449eda14cbcSMatt Macy  */
1450eda14cbcSMatt Macy static int
1451e92ffd9bSMartin Matuska dbuf_read_hole(dmu_buf_impl_t *db, dnode_t *dn)
1452eda14cbcSMatt Macy {
1453eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&db->db_mtx));
1454eda14cbcSMatt Macy 
1455eda14cbcSMatt Macy 	int is_hole = db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr);
1456eda14cbcSMatt Macy 	/*
1457eda14cbcSMatt Macy 	 * For level 0 blocks only, if the above check fails:
1458eda14cbcSMatt Macy 	 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
1459eda14cbcSMatt Macy 	 * processes the delete record and clears the bp while we are waiting
1460eda14cbcSMatt Macy 	 * for the dn_mtx (resulting in a "no" from block_freed).
1461eda14cbcSMatt Macy 	 */
1462eda14cbcSMatt Macy 	if (!is_hole && db->db_level == 0) {
1463eda14cbcSMatt Macy 		is_hole = dnode_block_freed(dn, db->db_blkid) ||
1464eda14cbcSMatt Macy 		    BP_IS_HOLE(db->db_blkptr);
1465eda14cbcSMatt Macy 	}
1466eda14cbcSMatt Macy 
1467eda14cbcSMatt Macy 	if (is_hole) {
1468eda14cbcSMatt Macy 		dbuf_set_data(db, dbuf_alloc_arcbuf(db));
1469da5137abSMartin Matuska 		memset(db->db.db_data, 0, db->db.db_size);
1470eda14cbcSMatt Macy 
1471eda14cbcSMatt Macy 		if (db->db_blkptr != NULL && db->db_level > 0 &&
1472eda14cbcSMatt Macy 		    BP_IS_HOLE(db->db_blkptr) &&
1473eda14cbcSMatt Macy 		    db->db_blkptr->blk_birth != 0) {
1474eda14cbcSMatt Macy 			dbuf_handle_indirect_hole(db, dn);
1475eda14cbcSMatt Macy 		}
1476eda14cbcSMatt Macy 		db->db_state = DB_CACHED;
1477eda14cbcSMatt Macy 		DTRACE_SET_STATE(db, "hole read satisfied");
1478eda14cbcSMatt Macy 		return (0);
1479eda14cbcSMatt Macy 	}
1480eda14cbcSMatt Macy 	return (ENOENT);
1481eda14cbcSMatt Macy }
1482eda14cbcSMatt Macy 
1483eda14cbcSMatt Macy /*
1484eda14cbcSMatt Macy  * This function ensures that, when doing a decrypting read of a block,
1485eda14cbcSMatt Macy  * we make sure we have decrypted the dnode associated with it. We must do
1486eda14cbcSMatt Macy  * this so that we ensure we are fully authenticating the checksum-of-MACs
1487eda14cbcSMatt Macy  * tree from the root of the objset down to this block. Indirect blocks are
1488eda14cbcSMatt Macy  * always verified against their secure checksum-of-MACs assuming that the
1489eda14cbcSMatt Macy  * dnode containing them is correct. Now that we are doing a decrypting read,
1490eda14cbcSMatt Macy  * we can be sure that the key is loaded and verify that assumption. This is
1491eda14cbcSMatt Macy  * especially important considering that we always read encrypted dnode
1492eda14cbcSMatt Macy  * blocks as raw data (without verifying their MACs) to start, and
1493eda14cbcSMatt Macy  * decrypt / authenticate them when we need to read an encrypted bonus buffer.
1494eda14cbcSMatt Macy  */
1495eda14cbcSMatt Macy static int
1496eda14cbcSMatt Macy dbuf_read_verify_dnode_crypt(dmu_buf_impl_t *db, uint32_t flags)
1497eda14cbcSMatt Macy {
1498eda14cbcSMatt Macy 	int err = 0;
1499eda14cbcSMatt Macy 	objset_t *os = db->db_objset;
1500eda14cbcSMatt Macy 	arc_buf_t *dnode_abuf;
1501eda14cbcSMatt Macy 	dnode_t *dn;
1502eda14cbcSMatt Macy 	zbookmark_phys_t zb;
1503eda14cbcSMatt Macy 
1504eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&db->db_mtx));
1505eda14cbcSMatt Macy 
1506*bb2d13b6SMartin Matuska 	if ((flags & DB_RF_NO_DECRYPT) != 0 ||
1507*bb2d13b6SMartin Matuska 	    !os->os_encrypted || os->os_raw_receive)
1508eda14cbcSMatt Macy 		return (0);
1509eda14cbcSMatt Macy 
1510eda14cbcSMatt Macy 	DB_DNODE_ENTER(db);
1511eda14cbcSMatt Macy 	dn = DB_DNODE(db);
1512eda14cbcSMatt Macy 	dnode_abuf = (dn->dn_dbuf != NULL) ? dn->dn_dbuf->db_buf : NULL;
1513eda14cbcSMatt Macy 
1514eda14cbcSMatt Macy 	if (dnode_abuf == NULL || !arc_is_encrypted(dnode_abuf)) {
1515eda14cbcSMatt Macy 		DB_DNODE_EXIT(db);
1516eda14cbcSMatt Macy 		return (0);
1517eda14cbcSMatt Macy 	}
1518eda14cbcSMatt Macy 
1519eda14cbcSMatt Macy 	SET_BOOKMARK(&zb, dmu_objset_id(os),
1520eda14cbcSMatt Macy 	    DMU_META_DNODE_OBJECT, 0, dn->dn_dbuf->db_blkid);
1521eda14cbcSMatt Macy 	err = arc_untransform(dnode_abuf, os->os_spa, &zb, B_TRUE);
1522eda14cbcSMatt Macy 
1523eda14cbcSMatt Macy 	/*
1524eda14cbcSMatt Macy 	 * An error code of EACCES tells us that the key is still not
1525eda14cbcSMatt Macy 	 * available. This is ok if we are only reading authenticated
1526eda14cbcSMatt Macy 	 * (and therefore non-encrypted) blocks.
1527eda14cbcSMatt Macy 	 */
1528eda14cbcSMatt Macy 	if (err == EACCES && ((db->db_blkid != DMU_BONUS_BLKID &&
1529eda14cbcSMatt Macy 	    !DMU_OT_IS_ENCRYPTED(dn->dn_type)) ||
1530eda14cbcSMatt Macy 	    (db->db_blkid == DMU_BONUS_BLKID &&
1531eda14cbcSMatt Macy 	    !DMU_OT_IS_ENCRYPTED(dn->dn_bonustype))))
1532eda14cbcSMatt Macy 		err = 0;
1533eda14cbcSMatt Macy 
1534eda14cbcSMatt Macy 	DB_DNODE_EXIT(db);
1535eda14cbcSMatt Macy 
1536eda14cbcSMatt Macy 	return (err);
1537eda14cbcSMatt Macy }
1538eda14cbcSMatt Macy 
1539eda14cbcSMatt Macy /*
1540eda14cbcSMatt Macy  * Drops db_mtx and the parent lock specified by dblt and tag before
1541eda14cbcSMatt Macy  * returning.
1542eda14cbcSMatt Macy  */
1543eda14cbcSMatt Macy static int
1544eda14cbcSMatt Macy dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags,
1545a0b956f5SMartin Matuska     db_lock_type_t dblt, const void *tag)
1546eda14cbcSMatt Macy {
1547eda14cbcSMatt Macy 	dnode_t *dn;
1548eda14cbcSMatt Macy 	zbookmark_phys_t zb;
1549eda14cbcSMatt Macy 	uint32_t aflags = ARC_FLAG_NOWAIT;
1550eda14cbcSMatt Macy 	int err, zio_flags;
1551eda14cbcSMatt Macy 
1552eda14cbcSMatt Macy 	DB_DNODE_ENTER(db);
1553eda14cbcSMatt Macy 	dn = DB_DNODE(db);
1554eda14cbcSMatt Macy 	ASSERT(!zfs_refcount_is_zero(&db->db_holds));
1555eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&db->db_mtx));
1556eda14cbcSMatt Macy 	ASSERT(db->db_state == DB_UNCACHED);
1557eda14cbcSMatt Macy 	ASSERT(db->db_buf == NULL);
1558eda14cbcSMatt Macy 	ASSERT(db->db_parent == NULL ||
1559eda14cbcSMatt Macy 	    RW_LOCK_HELD(&db->db_parent->db_rwlock));
1560eda14cbcSMatt Macy 
1561eda14cbcSMatt Macy 	if (db->db_blkid == DMU_BONUS_BLKID) {
1562eda14cbcSMatt Macy 		err = dbuf_read_bonus(db, dn, flags);
1563eda14cbcSMatt Macy 		goto early_unlock;
1564eda14cbcSMatt Macy 	}
1565eda14cbcSMatt Macy 
1566e92ffd9bSMartin Matuska 	err = dbuf_read_hole(db, dn);
1567eda14cbcSMatt Macy 	if (err == 0)
1568eda14cbcSMatt Macy 		goto early_unlock;
1569eda14cbcSMatt Macy 
1570eda14cbcSMatt Macy 	/*
1571eda14cbcSMatt Macy 	 * Any attempt to read a redacted block should result in an error. This
1572eda14cbcSMatt Macy 	 * will never happen under normal conditions, but can be useful for
1573eda14cbcSMatt Macy 	 * debugging purposes.
1574eda14cbcSMatt Macy 	 */
1575eda14cbcSMatt Macy 	if (BP_IS_REDACTED(db->db_blkptr)) {
1576eda14cbcSMatt Macy 		ASSERT(dsl_dataset_feature_is_active(
1577eda14cbcSMatt Macy 		    db->db_objset->os_dsl_dataset,
1578eda14cbcSMatt Macy 		    SPA_FEATURE_REDACTED_DATASETS));
1579eda14cbcSMatt Macy 		err = SET_ERROR(EIO);
1580eda14cbcSMatt Macy 		goto early_unlock;
1581eda14cbcSMatt Macy 	}
1582eda14cbcSMatt Macy 
1583eda14cbcSMatt Macy 	SET_BOOKMARK(&zb, dmu_objset_id(db->db_objset),
1584eda14cbcSMatt Macy 	    db->db.db_object, db->db_level, db->db_blkid);
1585eda14cbcSMatt Macy 
1586eda14cbcSMatt Macy 	/*
1587eda14cbcSMatt Macy 	 * All bps of an encrypted os should have the encryption bit set.
1588eda14cbcSMatt Macy 	 * If this is not true it indicates tampering and we report an error.
1589eda14cbcSMatt Macy 	 */
1590eda14cbcSMatt Macy 	if (db->db_objset->os_encrypted && !BP_USES_CRYPT(db->db_blkptr)) {
1591eda14cbcSMatt Macy 		spa_log_error(db->db_objset->os_spa, &zb);
1592eda14cbcSMatt Macy 		zfs_panic_recover("unencrypted block in encrypted "
1593eda14cbcSMatt Macy 		    "object set %llu", dmu_objset_id(db->db_objset));
1594eda14cbcSMatt Macy 		err = SET_ERROR(EIO);
1595eda14cbcSMatt Macy 		goto early_unlock;
1596eda14cbcSMatt Macy 	}
1597eda14cbcSMatt Macy 
1598eda14cbcSMatt Macy 	err = dbuf_read_verify_dnode_crypt(db, flags);
1599eda14cbcSMatt Macy 	if (err != 0)
1600eda14cbcSMatt Macy 		goto early_unlock;
1601eda14cbcSMatt Macy 
1602eda14cbcSMatt Macy 	DB_DNODE_EXIT(db);
1603eda14cbcSMatt Macy 
1604eda14cbcSMatt Macy 	db->db_state = DB_READ;
1605eda14cbcSMatt Macy 	DTRACE_SET_STATE(db, "read issued");
1606eda14cbcSMatt Macy 	mutex_exit(&db->db_mtx);
1607eda14cbcSMatt Macy 
1608dae17134SMartin Matuska 	if (dbuf_is_l2cacheable(db))
1609eda14cbcSMatt Macy 		aflags |= ARC_FLAG_L2CACHE;
1610eda14cbcSMatt Macy 
1611eda14cbcSMatt Macy 	dbuf_add_ref(db, NULL);
1612eda14cbcSMatt Macy 
1613eda14cbcSMatt Macy 	zio_flags = (flags & DB_RF_CANFAIL) ?
1614eda14cbcSMatt Macy 	    ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED;
1615eda14cbcSMatt Macy 
1616eda14cbcSMatt Macy 	if ((flags & DB_RF_NO_DECRYPT) && BP_IS_PROTECTED(db->db_blkptr))
1617eda14cbcSMatt Macy 		zio_flags |= ZIO_FLAG_RAW;
1618eda14cbcSMatt Macy 	/*
1619eda14cbcSMatt Macy 	 * The zio layer will copy the provided blkptr later, but we need to
1620eda14cbcSMatt Macy 	 * do this now so that we can release the parent's rwlock. We have to
1621eda14cbcSMatt Macy 	 * do that now so that if dbuf_read_done is called synchronously (on
1622eda14cbcSMatt Macy 	 * an l1 cache hit) we don't acquire the db_mtx while holding the
1623eda14cbcSMatt Macy 	 * parent's rwlock, which would be a lock ordering violation.
1624eda14cbcSMatt Macy 	 */
1625eda14cbcSMatt Macy 	blkptr_t bp = *db->db_blkptr;
1626eda14cbcSMatt Macy 	dmu_buf_unlock_parent(db, dblt, tag);
1627eda14cbcSMatt Macy 	(void) arc_read(zio, db->db_objset->os_spa, &bp,
1628eda14cbcSMatt Macy 	    dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ, zio_flags,
1629eda14cbcSMatt Macy 	    &aflags, &zb);
1630eda14cbcSMatt Macy 	return (err);
1631eda14cbcSMatt Macy early_unlock:
1632eda14cbcSMatt Macy 	DB_DNODE_EXIT(db);
1633eda14cbcSMatt Macy 	mutex_exit(&db->db_mtx);
1634eda14cbcSMatt Macy 	dmu_buf_unlock_parent(db, dblt, tag);
1635eda14cbcSMatt Macy 	return (err);
1636eda14cbcSMatt Macy }
1637eda14cbcSMatt Macy 
1638eda14cbcSMatt Macy /*
1639eda14cbcSMatt Macy  * This is our just-in-time copy function.  It makes a copy of buffers that
1640eda14cbcSMatt Macy  * have been modified in a previous transaction group before we access them in
1641eda14cbcSMatt Macy  * the current active group.
1642eda14cbcSMatt Macy  *
1643eda14cbcSMatt Macy  * This function is used in three places: when we are dirtying a buffer for the
1644eda14cbcSMatt Macy  * first time in a txg, when we are freeing a range in a dnode that includes
1645eda14cbcSMatt Macy  * this buffer, and when we are accessing a buffer which was received compressed
1646eda14cbcSMatt Macy  * and later referenced in a WRITE_BYREF record.
1647eda14cbcSMatt Macy  *
1648eda14cbcSMatt Macy  * Note that when we are called from dbuf_free_range() we do not put a hold on
1649eda14cbcSMatt Macy  * the buffer, we just traverse the active dbuf list for the dnode.
1650eda14cbcSMatt Macy  */
1651eda14cbcSMatt Macy static void
1652eda14cbcSMatt Macy dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg)
1653eda14cbcSMatt Macy {
1654eda14cbcSMatt Macy 	dbuf_dirty_record_t *dr = list_head(&db->db_dirty_records);
1655eda14cbcSMatt Macy 
1656eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&db->db_mtx));
1657eda14cbcSMatt Macy 	ASSERT(db->db.db_data != NULL);
1658eda14cbcSMatt Macy 	ASSERT(db->db_level == 0);
1659eda14cbcSMatt Macy 	ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT);
1660eda14cbcSMatt Macy 
1661eda14cbcSMatt Macy 	if (dr == NULL ||
1662eda14cbcSMatt Macy 	    (dr->dt.dl.dr_data !=
1663eda14cbcSMatt Macy 	    ((db->db_blkid  == DMU_BONUS_BLKID) ? db->db.db_data : db->db_buf)))
1664eda14cbcSMatt Macy 		return;
1665eda14cbcSMatt Macy 
1666eda14cbcSMatt Macy 	/*
1667eda14cbcSMatt Macy 	 * If the last dirty record for this dbuf has not yet synced
1668eda14cbcSMatt Macy 	 * and its referencing the dbuf data, either:
1669eda14cbcSMatt Macy 	 *	reset the reference to point to a new copy,
1670eda14cbcSMatt Macy 	 * or (if there a no active holders)
1671eda14cbcSMatt Macy 	 *	just null out the current db_data pointer.
1672eda14cbcSMatt Macy 	 */
1673eda14cbcSMatt Macy 	ASSERT3U(dr->dr_txg, >=, txg - 2);
1674eda14cbcSMatt Macy 	if (db->db_blkid == DMU_BONUS_BLKID) {
1675eda14cbcSMatt Macy 		dnode_t *dn = DB_DNODE(db);
1676eda14cbcSMatt Macy 		int bonuslen = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots);
1677eda14cbcSMatt Macy 		dr->dt.dl.dr_data = kmem_alloc(bonuslen, KM_SLEEP);
1678eda14cbcSMatt Macy 		arc_space_consume(bonuslen, ARC_SPACE_BONUS);
1679da5137abSMartin Matuska 		memcpy(dr->dt.dl.dr_data, db->db.db_data, bonuslen);
1680eda14cbcSMatt Macy 	} else if (zfs_refcount_count(&db->db_holds) > db->db_dirtycnt) {
168133b8c039SMartin Matuska 		dnode_t *dn = DB_DNODE(db);
168233b8c039SMartin Matuska 		int size = arc_buf_size(db->db_buf);
168333b8c039SMartin Matuska 		arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
168433b8c039SMartin Matuska 		spa_t *spa = db->db_objset->os_spa;
168533b8c039SMartin Matuska 		enum zio_compress compress_type =
168633b8c039SMartin Matuska 		    arc_get_compression(db->db_buf);
168733b8c039SMartin Matuska 		uint8_t complevel = arc_get_complevel(db->db_buf);
168833b8c039SMartin Matuska 
168933b8c039SMartin Matuska 		if (arc_is_encrypted(db->db_buf)) {
169033b8c039SMartin Matuska 			boolean_t byteorder;
169133b8c039SMartin Matuska 			uint8_t salt[ZIO_DATA_SALT_LEN];
169233b8c039SMartin Matuska 			uint8_t iv[ZIO_DATA_IV_LEN];
169333b8c039SMartin Matuska 			uint8_t mac[ZIO_DATA_MAC_LEN];
169433b8c039SMartin Matuska 
169533b8c039SMartin Matuska 			arc_get_raw_params(db->db_buf, &byteorder, salt,
169633b8c039SMartin Matuska 			    iv, mac);
169733b8c039SMartin Matuska 			dr->dt.dl.dr_data = arc_alloc_raw_buf(spa, db,
169833b8c039SMartin Matuska 			    dmu_objset_id(dn->dn_objset), byteorder, salt, iv,
169933b8c039SMartin Matuska 			    mac, dn->dn_type, size, arc_buf_lsize(db->db_buf),
170033b8c039SMartin Matuska 			    compress_type, complevel);
170133b8c039SMartin Matuska 		} else if (compress_type != ZIO_COMPRESS_OFF) {
170233b8c039SMartin Matuska 			ASSERT3U(type, ==, ARC_BUFC_DATA);
170333b8c039SMartin Matuska 			dr->dt.dl.dr_data = arc_alloc_compressed_buf(spa, db,
170433b8c039SMartin Matuska 			    size, arc_buf_lsize(db->db_buf), compress_type,
170533b8c039SMartin Matuska 			    complevel);
170633b8c039SMartin Matuska 		} else {
170733b8c039SMartin Matuska 			dr->dt.dl.dr_data = arc_alloc_buf(spa, db, type, size);
170833b8c039SMartin Matuska 		}
1709da5137abSMartin Matuska 		memcpy(dr->dt.dl.dr_data->b_data, db->db.db_data, size);
1710eda14cbcSMatt Macy 	} else {
1711eda14cbcSMatt Macy 		db->db_buf = NULL;
1712eda14cbcSMatt Macy 		dbuf_clear_data(db);
1713eda14cbcSMatt Macy 	}
1714eda14cbcSMatt Macy }
1715eda14cbcSMatt Macy 
1716eda14cbcSMatt Macy int
1717eda14cbcSMatt Macy dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
1718eda14cbcSMatt Macy {
1719eda14cbcSMatt Macy 	int err = 0;
1720eda14cbcSMatt Macy 	boolean_t prefetch;
1721eda14cbcSMatt Macy 	dnode_t *dn;
1722eda14cbcSMatt Macy 
1723eda14cbcSMatt Macy 	/*
1724eda14cbcSMatt Macy 	 * We don't have to hold the mutex to check db_state because it
1725eda14cbcSMatt Macy 	 * can't be freed while we have a hold on the buffer.
1726eda14cbcSMatt Macy 	 */
1727eda14cbcSMatt Macy 	ASSERT(!zfs_refcount_is_zero(&db->db_holds));
1728eda14cbcSMatt Macy 
1729eda14cbcSMatt Macy 	if (db->db_state == DB_NOFILL)
1730eda14cbcSMatt Macy 		return (SET_ERROR(EIO));
1731eda14cbcSMatt Macy 
1732eda14cbcSMatt Macy 	DB_DNODE_ENTER(db);
1733eda14cbcSMatt Macy 	dn = DB_DNODE(db);
1734eda14cbcSMatt Macy 
1735eda14cbcSMatt Macy 	prefetch = db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1736eda14cbcSMatt Macy 	    (flags & DB_RF_NOPREFETCH) == 0 && dn != NULL &&
1737eda14cbcSMatt Macy 	    DBUF_IS_CACHEABLE(db);
1738eda14cbcSMatt Macy 
1739eda14cbcSMatt Macy 	mutex_enter(&db->db_mtx);
1740eda14cbcSMatt Macy 	if (db->db_state == DB_CACHED) {
1741eda14cbcSMatt Macy 		/*
1742eda14cbcSMatt Macy 		 * Ensure that this block's dnode has been decrypted if
1743eda14cbcSMatt Macy 		 * the caller has requested decrypted data.
1744eda14cbcSMatt Macy 		 */
1745eda14cbcSMatt Macy 		err = dbuf_read_verify_dnode_crypt(db, flags);
1746eda14cbcSMatt Macy 
1747eda14cbcSMatt Macy 		/*
1748eda14cbcSMatt Macy 		 * If the arc buf is compressed or encrypted and the caller
1749eda14cbcSMatt Macy 		 * requested uncompressed data, we need to untransform it
1750eda14cbcSMatt Macy 		 * before returning. We also call arc_untransform() on any
1751eda14cbcSMatt Macy 		 * unauthenticated blocks, which will verify their MAC if
1752eda14cbcSMatt Macy 		 * the key is now available.
1753eda14cbcSMatt Macy 		 */
1754eda14cbcSMatt Macy 		if (err == 0 && db->db_buf != NULL &&
1755eda14cbcSMatt Macy 		    (flags & DB_RF_NO_DECRYPT) == 0 &&
1756eda14cbcSMatt Macy 		    (arc_is_encrypted(db->db_buf) ||
1757eda14cbcSMatt Macy 		    arc_is_unauthenticated(db->db_buf) ||
1758eda14cbcSMatt Macy 		    arc_get_compression(db->db_buf) != ZIO_COMPRESS_OFF)) {
1759*bb2d13b6SMartin Matuska 			spa_t *spa = dn->dn_objset->os_spa;
1760eda14cbcSMatt Macy 			zbookmark_phys_t zb;
1761eda14cbcSMatt Macy 
1762eda14cbcSMatt Macy 			SET_BOOKMARK(&zb, dmu_objset_id(db->db_objset),
1763eda14cbcSMatt Macy 			    db->db.db_object, db->db_level, db->db_blkid);
1764eda14cbcSMatt Macy 			dbuf_fix_old_data(db, spa_syncing_txg(spa));
1765eda14cbcSMatt Macy 			err = arc_untransform(db->db_buf, spa, &zb, B_FALSE);
1766eda14cbcSMatt Macy 			dbuf_set_data(db, db->db_buf);
1767eda14cbcSMatt Macy 		}
1768eda14cbcSMatt Macy 		mutex_exit(&db->db_mtx);
1769eda14cbcSMatt Macy 		if (err == 0 && prefetch) {
1770eda14cbcSMatt Macy 			dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE,
1771f9693befSMartin Matuska 			    B_FALSE, flags & DB_RF_HAVESTRUCT);
1772eda14cbcSMatt Macy 		}
1773eda14cbcSMatt Macy 		DB_DNODE_EXIT(db);
1774eda14cbcSMatt Macy 		DBUF_STAT_BUMP(hash_hits);
1775eda14cbcSMatt Macy 	} else if (db->db_state == DB_UNCACHED) {
1776eda14cbcSMatt Macy 		boolean_t need_wait = B_FALSE;
1777eda14cbcSMatt Macy 
1778eda14cbcSMatt Macy 		db_lock_type_t dblt = dmu_buf_lock_parent(db, RW_READER, FTAG);
1779eda14cbcSMatt Macy 
1780eda14cbcSMatt Macy 		if (zio == NULL &&
1781eda14cbcSMatt Macy 		    db->db_blkptr != NULL && !BP_IS_HOLE(db->db_blkptr)) {
1782*bb2d13b6SMartin Matuska 			spa_t *spa = dn->dn_objset->os_spa;
1783eda14cbcSMatt Macy 			zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
1784eda14cbcSMatt Macy 			need_wait = B_TRUE;
1785eda14cbcSMatt Macy 		}
1786eda14cbcSMatt Macy 		err = dbuf_read_impl(db, zio, flags, dblt, FTAG);
1787eda14cbcSMatt Macy 		/*
1788eda14cbcSMatt Macy 		 * dbuf_read_impl has dropped db_mtx and our parent's rwlock
1789eda14cbcSMatt Macy 		 * for us
1790eda14cbcSMatt Macy 		 */
1791eda14cbcSMatt Macy 		if (!err && prefetch) {
1792eda14cbcSMatt Macy 			dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE,
1793f9693befSMartin Matuska 			    db->db_state != DB_CACHED,
1794eda14cbcSMatt Macy 			    flags & DB_RF_HAVESTRUCT);
1795eda14cbcSMatt Macy 		}
1796eda14cbcSMatt Macy 
1797eda14cbcSMatt Macy 		DB_DNODE_EXIT(db);
1798eda14cbcSMatt Macy 		DBUF_STAT_BUMP(hash_misses);
1799eda14cbcSMatt Macy 
1800eda14cbcSMatt Macy 		/*
1801eda14cbcSMatt Macy 		 * If we created a zio_root we must execute it to avoid
1802eda14cbcSMatt Macy 		 * leaking it, even if it isn't attached to any work due
1803eda14cbcSMatt Macy 		 * to an error in dbuf_read_impl().
1804eda14cbcSMatt Macy 		 */
1805eda14cbcSMatt Macy 		if (need_wait) {
1806eda14cbcSMatt Macy 			if (err == 0)
1807eda14cbcSMatt Macy 				err = zio_wait(zio);
1808eda14cbcSMatt Macy 			else
1809eda14cbcSMatt Macy 				VERIFY0(zio_wait(zio));
1810eda14cbcSMatt Macy 		}
1811eda14cbcSMatt Macy 	} else {
1812eda14cbcSMatt Macy 		/*
1813eda14cbcSMatt Macy 		 * Another reader came in while the dbuf was in flight
1814eda14cbcSMatt Macy 		 * between UNCACHED and CACHED.  Either a writer will finish
1815eda14cbcSMatt Macy 		 * writing the buffer (sending the dbuf to CACHED) or the
1816eda14cbcSMatt Macy 		 * first reader's request will reach the read_done callback
1817eda14cbcSMatt Macy 		 * and send the dbuf to CACHED.  Otherwise, a failure
1818eda14cbcSMatt Macy 		 * occurred and the dbuf went to UNCACHED.
1819eda14cbcSMatt Macy 		 */
1820eda14cbcSMatt Macy 		mutex_exit(&db->db_mtx);
1821eda14cbcSMatt Macy 		if (prefetch) {
1822eda14cbcSMatt Macy 			dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE,
1823f9693befSMartin Matuska 			    B_TRUE, flags & DB_RF_HAVESTRUCT);
1824eda14cbcSMatt Macy 		}
1825eda14cbcSMatt Macy 		DB_DNODE_EXIT(db);
1826eda14cbcSMatt Macy 		DBUF_STAT_BUMP(hash_misses);
1827eda14cbcSMatt Macy 
1828eda14cbcSMatt Macy 		/* Skip the wait per the caller's request. */
1829eda14cbcSMatt Macy 		if ((flags & DB_RF_NEVERWAIT) == 0) {
1830eda14cbcSMatt Macy 			mutex_enter(&db->db_mtx);
1831eda14cbcSMatt Macy 			while (db->db_state == DB_READ ||
1832eda14cbcSMatt Macy 			    db->db_state == DB_FILL) {
1833eda14cbcSMatt Macy 				ASSERT(db->db_state == DB_READ ||
1834eda14cbcSMatt Macy 				    (flags & DB_RF_HAVESTRUCT) == 0);
1835eda14cbcSMatt Macy 				DTRACE_PROBE2(blocked__read, dmu_buf_impl_t *,
1836eda14cbcSMatt Macy 				    db, zio_t *, zio);
1837eda14cbcSMatt Macy 				cv_wait(&db->db_changed, &db->db_mtx);
1838eda14cbcSMatt Macy 			}
1839eda14cbcSMatt Macy 			if (db->db_state == DB_UNCACHED)
1840eda14cbcSMatt Macy 				err = SET_ERROR(EIO);
1841eda14cbcSMatt Macy 			mutex_exit(&db->db_mtx);
1842eda14cbcSMatt Macy 		}
1843eda14cbcSMatt Macy 	}
1844eda14cbcSMatt Macy 
1845eda14cbcSMatt Macy 	return (err);
1846eda14cbcSMatt Macy }
1847eda14cbcSMatt Macy 
1848eda14cbcSMatt Macy static void
1849eda14cbcSMatt Macy dbuf_noread(dmu_buf_impl_t *db)
1850eda14cbcSMatt Macy {
1851eda14cbcSMatt Macy 	ASSERT(!zfs_refcount_is_zero(&db->db_holds));
1852eda14cbcSMatt Macy 	ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1853eda14cbcSMatt Macy 	mutex_enter(&db->db_mtx);
1854eda14cbcSMatt Macy 	while (db->db_state == DB_READ || db->db_state == DB_FILL)
1855eda14cbcSMatt Macy 		cv_wait(&db->db_changed, &db->db_mtx);
1856eda14cbcSMatt Macy 	if (db->db_state == DB_UNCACHED) {
1857eda14cbcSMatt Macy 		ASSERT(db->db_buf == NULL);
1858eda14cbcSMatt Macy 		ASSERT(db->db.db_data == NULL);
1859eda14cbcSMatt Macy 		dbuf_set_data(db, dbuf_alloc_arcbuf(db));
1860eda14cbcSMatt Macy 		db->db_state = DB_FILL;
1861eda14cbcSMatt Macy 		DTRACE_SET_STATE(db, "assigning filled buffer");
1862eda14cbcSMatt Macy 	} else if (db->db_state == DB_NOFILL) {
1863eda14cbcSMatt Macy 		dbuf_clear_data(db);
1864eda14cbcSMatt Macy 	} else {
1865eda14cbcSMatt Macy 		ASSERT3U(db->db_state, ==, DB_CACHED);
1866eda14cbcSMatt Macy 	}
1867eda14cbcSMatt Macy 	mutex_exit(&db->db_mtx);
1868eda14cbcSMatt Macy }
1869eda14cbcSMatt Macy 
1870eda14cbcSMatt Macy void
1871eda14cbcSMatt Macy dbuf_unoverride(dbuf_dirty_record_t *dr)
1872eda14cbcSMatt Macy {
1873eda14cbcSMatt Macy 	dmu_buf_impl_t *db = dr->dr_dbuf;
1874eda14cbcSMatt Macy 	blkptr_t *bp = &dr->dt.dl.dr_overridden_by;
1875eda14cbcSMatt Macy 	uint64_t txg = dr->dr_txg;
1876eda14cbcSMatt Macy 
1877eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&db->db_mtx));
1878eda14cbcSMatt Macy 	/*
1879eda14cbcSMatt Macy 	 * This assert is valid because dmu_sync() expects to be called by
1880eda14cbcSMatt Macy 	 * a zilog's get_data while holding a range lock.  This call only
1881eda14cbcSMatt Macy 	 * comes from dbuf_dirty() callers who must also hold a range lock.
1882eda14cbcSMatt Macy 	 */
1883eda14cbcSMatt Macy 	ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC);
1884eda14cbcSMatt Macy 	ASSERT(db->db_level == 0);
1885eda14cbcSMatt Macy 
1886eda14cbcSMatt Macy 	if (db->db_blkid == DMU_BONUS_BLKID ||
1887eda14cbcSMatt Macy 	    dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN)
1888eda14cbcSMatt Macy 		return;
1889eda14cbcSMatt Macy 
1890eda14cbcSMatt Macy 	ASSERT(db->db_data_pending != dr);
1891eda14cbcSMatt Macy 
1892eda14cbcSMatt Macy 	/* free this block */
1893eda14cbcSMatt Macy 	if (!BP_IS_HOLE(bp) && !dr->dt.dl.dr_nopwrite)
1894eda14cbcSMatt Macy 		zio_free(db->db_objset->os_spa, txg, bp);
1895eda14cbcSMatt Macy 
1896eda14cbcSMatt Macy 	dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
1897eda14cbcSMatt Macy 	dr->dt.dl.dr_nopwrite = B_FALSE;
1898eda14cbcSMatt Macy 	dr->dt.dl.dr_has_raw_params = B_FALSE;
1899eda14cbcSMatt Macy 
1900eda14cbcSMatt Macy 	/*
1901eda14cbcSMatt Macy 	 * Release the already-written buffer, so we leave it in
1902eda14cbcSMatt Macy 	 * a consistent dirty state.  Note that all callers are
1903eda14cbcSMatt Macy 	 * modifying the buffer, so they will immediately do
1904eda14cbcSMatt Macy 	 * another (redundant) arc_release().  Therefore, leave
1905eda14cbcSMatt Macy 	 * the buf thawed to save the effort of freezing &
1906eda14cbcSMatt Macy 	 * immediately re-thawing it.
1907eda14cbcSMatt Macy 	 */
1908eda14cbcSMatt Macy 	arc_release(dr->dt.dl.dr_data, db);
1909eda14cbcSMatt Macy }
1910eda14cbcSMatt Macy 
1911eda14cbcSMatt Macy /*
1912eda14cbcSMatt Macy  * Evict (if its unreferenced) or clear (if its referenced) any level-0
1913eda14cbcSMatt Macy  * data blocks in the free range, so that any future readers will find
1914eda14cbcSMatt Macy  * empty blocks.
1915eda14cbcSMatt Macy  */
1916eda14cbcSMatt Macy void
1917eda14cbcSMatt Macy dbuf_free_range(dnode_t *dn, uint64_t start_blkid, uint64_t end_blkid,
1918eda14cbcSMatt Macy     dmu_tx_t *tx)
1919eda14cbcSMatt Macy {
1920eda14cbcSMatt Macy 	dmu_buf_impl_t *db_search;
1921eda14cbcSMatt Macy 	dmu_buf_impl_t *db, *db_next;
1922eda14cbcSMatt Macy 	uint64_t txg = tx->tx_txg;
1923eda14cbcSMatt Macy 	avl_index_t where;
1924eda14cbcSMatt Macy 	dbuf_dirty_record_t *dr;
1925eda14cbcSMatt Macy 
1926eda14cbcSMatt Macy 	if (end_blkid > dn->dn_maxblkid &&
1927eda14cbcSMatt Macy 	    !(start_blkid == DMU_SPILL_BLKID || end_blkid == DMU_SPILL_BLKID))
1928eda14cbcSMatt Macy 		end_blkid = dn->dn_maxblkid;
192933b8c039SMartin Matuska 	dprintf_dnode(dn, "start=%llu end=%llu\n", (u_longlong_t)start_blkid,
193033b8c039SMartin Matuska 	    (u_longlong_t)end_blkid);
1931eda14cbcSMatt Macy 
1932eda14cbcSMatt Macy 	db_search = kmem_alloc(sizeof (dmu_buf_impl_t), KM_SLEEP);
1933eda14cbcSMatt Macy 	db_search->db_level = 0;
1934eda14cbcSMatt Macy 	db_search->db_blkid = start_blkid;
1935eda14cbcSMatt Macy 	db_search->db_state = DB_SEARCH;
1936eda14cbcSMatt Macy 
1937eda14cbcSMatt Macy 	mutex_enter(&dn->dn_dbufs_mtx);
1938eda14cbcSMatt Macy 	db = avl_find(&dn->dn_dbufs, db_search, &where);
1939eda14cbcSMatt Macy 	ASSERT3P(db, ==, NULL);
1940eda14cbcSMatt Macy 
1941eda14cbcSMatt Macy 	db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER);
1942eda14cbcSMatt Macy 
1943eda14cbcSMatt Macy 	for (; db != NULL; db = db_next) {
1944eda14cbcSMatt Macy 		db_next = AVL_NEXT(&dn->dn_dbufs, db);
1945eda14cbcSMatt Macy 		ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1946eda14cbcSMatt Macy 
1947eda14cbcSMatt Macy 		if (db->db_level != 0 || db->db_blkid > end_blkid) {
1948eda14cbcSMatt Macy 			break;
1949eda14cbcSMatt Macy 		}
1950eda14cbcSMatt Macy 		ASSERT3U(db->db_blkid, >=, start_blkid);
1951eda14cbcSMatt Macy 
1952eda14cbcSMatt Macy 		/* found a level 0 buffer in the range */
1953eda14cbcSMatt Macy 		mutex_enter(&db->db_mtx);
1954eda14cbcSMatt Macy 		if (dbuf_undirty(db, tx)) {
1955eda14cbcSMatt Macy 			/* mutex has been dropped and dbuf destroyed */
1956eda14cbcSMatt Macy 			continue;
1957eda14cbcSMatt Macy 		}
1958eda14cbcSMatt Macy 
1959eda14cbcSMatt Macy 		if (db->db_state == DB_UNCACHED ||
1960eda14cbcSMatt Macy 		    db->db_state == DB_NOFILL ||
1961eda14cbcSMatt Macy 		    db->db_state == DB_EVICTING) {
1962eda14cbcSMatt Macy 			ASSERT(db->db.db_data == NULL);
1963eda14cbcSMatt Macy 			mutex_exit(&db->db_mtx);
1964eda14cbcSMatt Macy 			continue;
1965eda14cbcSMatt Macy 		}
1966eda14cbcSMatt Macy 		if (db->db_state == DB_READ || db->db_state == DB_FILL) {
1967eda14cbcSMatt Macy 			/* will be handled in dbuf_read_done or dbuf_rele */
1968eda14cbcSMatt Macy 			db->db_freed_in_flight = TRUE;
1969eda14cbcSMatt Macy 			mutex_exit(&db->db_mtx);
1970eda14cbcSMatt Macy 			continue;
1971eda14cbcSMatt Macy 		}
1972eda14cbcSMatt Macy 		if (zfs_refcount_count(&db->db_holds) == 0) {
1973eda14cbcSMatt Macy 			ASSERT(db->db_buf);
1974eda14cbcSMatt Macy 			dbuf_destroy(db);
1975eda14cbcSMatt Macy 			continue;
1976eda14cbcSMatt Macy 		}
1977eda14cbcSMatt Macy 		/* The dbuf is referenced */
1978eda14cbcSMatt Macy 
1979eda14cbcSMatt Macy 		dr = list_head(&db->db_dirty_records);
1980eda14cbcSMatt Macy 		if (dr != NULL) {
1981eda14cbcSMatt Macy 			if (dr->dr_txg == txg) {
1982eda14cbcSMatt Macy 				/*
1983eda14cbcSMatt Macy 				 * This buffer is "in-use", re-adjust the file
1984eda14cbcSMatt Macy 				 * size to reflect that this buffer may
1985eda14cbcSMatt Macy 				 * contain new data when we sync.
1986eda14cbcSMatt Macy 				 */
1987eda14cbcSMatt Macy 				if (db->db_blkid != DMU_SPILL_BLKID &&
1988eda14cbcSMatt Macy 				    db->db_blkid > dn->dn_maxblkid)
1989eda14cbcSMatt Macy 					dn->dn_maxblkid = db->db_blkid;
1990eda14cbcSMatt Macy 				dbuf_unoverride(dr);
1991eda14cbcSMatt Macy 			} else {
1992eda14cbcSMatt Macy 				/*
1993eda14cbcSMatt Macy 				 * This dbuf is not dirty in the open context.
1994eda14cbcSMatt Macy 				 * Either uncache it (if its not referenced in
1995eda14cbcSMatt Macy 				 * the open context) or reset its contents to
1996eda14cbcSMatt Macy 				 * empty.
1997eda14cbcSMatt Macy 				 */
1998eda14cbcSMatt Macy 				dbuf_fix_old_data(db, txg);
1999eda14cbcSMatt Macy 			}
2000eda14cbcSMatt Macy 		}
2001eda14cbcSMatt Macy 		/* clear the contents if its cached */
2002eda14cbcSMatt Macy 		if (db->db_state == DB_CACHED) {
2003eda14cbcSMatt Macy 			ASSERT(db->db.db_data != NULL);
2004eda14cbcSMatt Macy 			arc_release(db->db_buf, db);
2005eda14cbcSMatt Macy 			rw_enter(&db->db_rwlock, RW_WRITER);
2006da5137abSMartin Matuska 			memset(db->db.db_data, 0, db->db.db_size);
2007eda14cbcSMatt Macy 			rw_exit(&db->db_rwlock);
2008eda14cbcSMatt Macy 			arc_buf_freeze(db->db_buf);
2009eda14cbcSMatt Macy 		}
2010eda14cbcSMatt Macy 
2011eda14cbcSMatt Macy 		mutex_exit(&db->db_mtx);
2012eda14cbcSMatt Macy 	}
2013eda14cbcSMatt Macy 
2014eda14cbcSMatt Macy 	mutex_exit(&dn->dn_dbufs_mtx);
2015681ce946SMartin Matuska 	kmem_free(db_search, sizeof (dmu_buf_impl_t));
2016eda14cbcSMatt Macy }
2017eda14cbcSMatt Macy 
2018eda14cbcSMatt Macy void
2019eda14cbcSMatt Macy dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx)
2020eda14cbcSMatt Macy {
2021eda14cbcSMatt Macy 	arc_buf_t *buf, *old_buf;
2022eda14cbcSMatt Macy 	dbuf_dirty_record_t *dr;
2023eda14cbcSMatt Macy 	int osize = db->db.db_size;
2024eda14cbcSMatt Macy 	arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
2025eda14cbcSMatt Macy 	dnode_t *dn;
2026eda14cbcSMatt Macy 
2027eda14cbcSMatt Macy 	ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2028eda14cbcSMatt Macy 
2029eda14cbcSMatt Macy 	DB_DNODE_ENTER(db);
2030eda14cbcSMatt Macy 	dn = DB_DNODE(db);
2031eda14cbcSMatt Macy 
2032eda14cbcSMatt Macy 	/*
2033eda14cbcSMatt Macy 	 * XXX we should be doing a dbuf_read, checking the return
2034eda14cbcSMatt Macy 	 * value and returning that up to our callers
2035eda14cbcSMatt Macy 	 */
2036eda14cbcSMatt Macy 	dmu_buf_will_dirty(&db->db, tx);
2037eda14cbcSMatt Macy 
2038eda14cbcSMatt Macy 	/* create the data buffer for the new block */
2039eda14cbcSMatt Macy 	buf = arc_alloc_buf(dn->dn_objset->os_spa, db, type, size);
2040eda14cbcSMatt Macy 
2041eda14cbcSMatt Macy 	/* copy old block data to the new block */
2042eda14cbcSMatt Macy 	old_buf = db->db_buf;
2043da5137abSMartin Matuska 	memcpy(buf->b_data, old_buf->b_data, MIN(osize, size));
2044eda14cbcSMatt Macy 	/* zero the remainder */
2045eda14cbcSMatt Macy 	if (size > osize)
2046da5137abSMartin Matuska 		memset((uint8_t *)buf->b_data + osize, 0, size - osize);
2047eda14cbcSMatt Macy 
2048eda14cbcSMatt Macy 	mutex_enter(&db->db_mtx);
2049eda14cbcSMatt Macy 	dbuf_set_data(db, buf);
2050eda14cbcSMatt Macy 	arc_buf_destroy(old_buf, db);
2051eda14cbcSMatt Macy 	db->db.db_size = size;
2052eda14cbcSMatt Macy 
2053eda14cbcSMatt Macy 	dr = list_head(&db->db_dirty_records);
2054eda14cbcSMatt Macy 	/* dirty record added by dmu_buf_will_dirty() */
2055eda14cbcSMatt Macy 	VERIFY(dr != NULL);
2056eda14cbcSMatt Macy 	if (db->db_level == 0)
2057eda14cbcSMatt Macy 		dr->dt.dl.dr_data = buf;
2058eda14cbcSMatt Macy 	ASSERT3U(dr->dr_txg, ==, tx->tx_txg);
2059eda14cbcSMatt Macy 	ASSERT3U(dr->dr_accounted, ==, osize);
2060eda14cbcSMatt Macy 	dr->dr_accounted = size;
2061eda14cbcSMatt Macy 	mutex_exit(&db->db_mtx);
2062eda14cbcSMatt Macy 
2063eda14cbcSMatt Macy 	dmu_objset_willuse_space(dn->dn_objset, size - osize, tx);
2064eda14cbcSMatt Macy 	DB_DNODE_EXIT(db);
2065eda14cbcSMatt Macy }
2066eda14cbcSMatt Macy 
2067eda14cbcSMatt Macy void
2068eda14cbcSMatt Macy dbuf_release_bp(dmu_buf_impl_t *db)
2069eda14cbcSMatt Macy {
2070eda14cbcSMatt Macy 	objset_t *os __maybe_unused = db->db_objset;
2071eda14cbcSMatt Macy 
2072eda14cbcSMatt Macy 	ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
2073eda14cbcSMatt Macy 	ASSERT(arc_released(os->os_phys_buf) ||
2074eda14cbcSMatt Macy 	    list_link_active(&os->os_dsl_dataset->ds_synced_link));
2075eda14cbcSMatt Macy 	ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf));
2076eda14cbcSMatt Macy 
2077eda14cbcSMatt Macy 	(void) arc_release(db->db_buf, db);
2078eda14cbcSMatt Macy }
2079eda14cbcSMatt Macy 
2080eda14cbcSMatt Macy /*
2081eda14cbcSMatt Macy  * We already have a dirty record for this TXG, and we are being
2082eda14cbcSMatt Macy  * dirtied again.
2083eda14cbcSMatt Macy  */
2084eda14cbcSMatt Macy static void
2085eda14cbcSMatt Macy dbuf_redirty(dbuf_dirty_record_t *dr)
2086eda14cbcSMatt Macy {
2087eda14cbcSMatt Macy 	dmu_buf_impl_t *db = dr->dr_dbuf;
2088eda14cbcSMatt Macy 
2089eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&db->db_mtx));
2090eda14cbcSMatt Macy 
2091eda14cbcSMatt Macy 	if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID) {
2092eda14cbcSMatt Macy 		/*
2093eda14cbcSMatt Macy 		 * If this buffer has already been written out,
2094eda14cbcSMatt Macy 		 * we now need to reset its state.
2095eda14cbcSMatt Macy 		 */
2096eda14cbcSMatt Macy 		dbuf_unoverride(dr);
2097eda14cbcSMatt Macy 		if (db->db.db_object != DMU_META_DNODE_OBJECT &&
2098eda14cbcSMatt Macy 		    db->db_state != DB_NOFILL) {
2099eda14cbcSMatt Macy 			/* Already released on initial dirty, so just thaw. */
2100eda14cbcSMatt Macy 			ASSERT(arc_released(db->db_buf));
2101eda14cbcSMatt Macy 			arc_buf_thaw(db->db_buf);
2102eda14cbcSMatt Macy 		}
2103eda14cbcSMatt Macy 	}
2104eda14cbcSMatt Macy }
2105eda14cbcSMatt Macy 
2106eda14cbcSMatt Macy dbuf_dirty_record_t *
21077877fdebSMatt Macy dbuf_dirty_lightweight(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx)
21087877fdebSMatt Macy {
21097877fdebSMatt Macy 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
21107877fdebSMatt Macy 	IMPLY(dn->dn_objset->os_raw_receive, dn->dn_maxblkid >= blkid);
21117877fdebSMatt Macy 	dnode_new_blkid(dn, blkid, tx, B_TRUE, B_FALSE);
21127877fdebSMatt Macy 	ASSERT(dn->dn_maxblkid >= blkid);
21137877fdebSMatt Macy 
21147877fdebSMatt Macy 	dbuf_dirty_record_t *dr = kmem_zalloc(sizeof (*dr), KM_SLEEP);
21157877fdebSMatt Macy 	list_link_init(&dr->dr_dirty_node);
21167877fdebSMatt Macy 	list_link_init(&dr->dr_dbuf_node);
21177877fdebSMatt Macy 	dr->dr_dnode = dn;
21187877fdebSMatt Macy 	dr->dr_txg = tx->tx_txg;
21197877fdebSMatt Macy 	dr->dt.dll.dr_blkid = blkid;
21207877fdebSMatt Macy 	dr->dr_accounted = dn->dn_datablksz;
21217877fdebSMatt Macy 
21227877fdebSMatt Macy 	/*
21237877fdebSMatt Macy 	 * There should not be any dbuf for the block that we're dirtying.
21247877fdebSMatt Macy 	 * Otherwise the buffer contents could be inconsistent between the
21257877fdebSMatt Macy 	 * dbuf and the lightweight dirty record.
21267877fdebSMatt Macy 	 */
21277877fdebSMatt Macy 	ASSERT3P(NULL, ==, dbuf_find(dn->dn_objset, dn->dn_object, 0, blkid));
21287877fdebSMatt Macy 
21297877fdebSMatt Macy 	mutex_enter(&dn->dn_mtx);
21307877fdebSMatt Macy 	int txgoff = tx->tx_txg & TXG_MASK;
21317877fdebSMatt Macy 	if (dn->dn_free_ranges[txgoff] != NULL) {
21327877fdebSMatt Macy 		range_tree_clear(dn->dn_free_ranges[txgoff], blkid, 1);
21337877fdebSMatt Macy 	}
21347877fdebSMatt Macy 
21357877fdebSMatt Macy 	if (dn->dn_nlevels == 1) {
21367877fdebSMatt Macy 		ASSERT3U(blkid, <, dn->dn_nblkptr);
21377877fdebSMatt Macy 		list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
21387877fdebSMatt Macy 		mutex_exit(&dn->dn_mtx);
21397877fdebSMatt Macy 		rw_exit(&dn->dn_struct_rwlock);
21407877fdebSMatt Macy 		dnode_setdirty(dn, tx);
21417877fdebSMatt Macy 	} else {
21427877fdebSMatt Macy 		mutex_exit(&dn->dn_mtx);
21437877fdebSMatt Macy 
21447877fdebSMatt Macy 		int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
21457877fdebSMatt Macy 		dmu_buf_impl_t *parent_db = dbuf_hold_level(dn,
21467877fdebSMatt Macy 		    1, blkid >> epbs, FTAG);
21477877fdebSMatt Macy 		rw_exit(&dn->dn_struct_rwlock);
21487877fdebSMatt Macy 		if (parent_db == NULL) {
21497877fdebSMatt Macy 			kmem_free(dr, sizeof (*dr));
21507877fdebSMatt Macy 			return (NULL);
21517877fdebSMatt Macy 		}
21527877fdebSMatt Macy 		int err = dbuf_read(parent_db, NULL,
21537877fdebSMatt Macy 		    (DB_RF_NOPREFETCH | DB_RF_CANFAIL));
21547877fdebSMatt Macy 		if (err != 0) {
21557877fdebSMatt Macy 			dbuf_rele(parent_db, FTAG);
21567877fdebSMatt Macy 			kmem_free(dr, sizeof (*dr));
21577877fdebSMatt Macy 			return (NULL);
21587877fdebSMatt Macy 		}
21597877fdebSMatt Macy 
21607877fdebSMatt Macy 		dbuf_dirty_record_t *parent_dr = dbuf_dirty(parent_db, tx);
21617877fdebSMatt Macy 		dbuf_rele(parent_db, FTAG);
21627877fdebSMatt Macy 		mutex_enter(&parent_dr->dt.di.dr_mtx);
21637877fdebSMatt Macy 		ASSERT3U(parent_dr->dr_txg, ==, tx->tx_txg);
21647877fdebSMatt Macy 		list_insert_tail(&parent_dr->dt.di.dr_children, dr);
21657877fdebSMatt Macy 		mutex_exit(&parent_dr->dt.di.dr_mtx);
21667877fdebSMatt Macy 		dr->dr_parent = parent_dr;
21677877fdebSMatt Macy 	}
21687877fdebSMatt Macy 
21697877fdebSMatt Macy 	dmu_objset_willuse_space(dn->dn_objset, dr->dr_accounted, tx);
21707877fdebSMatt Macy 
21717877fdebSMatt Macy 	return (dr);
21727877fdebSMatt Macy }
21737877fdebSMatt Macy 
21747877fdebSMatt Macy dbuf_dirty_record_t *
2175eda14cbcSMatt Macy dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
2176eda14cbcSMatt Macy {
2177eda14cbcSMatt Macy 	dnode_t *dn;
2178eda14cbcSMatt Macy 	objset_t *os;
2179eda14cbcSMatt Macy 	dbuf_dirty_record_t *dr, *dr_next, *dr_head;
2180eda14cbcSMatt Macy 	int txgoff = tx->tx_txg & TXG_MASK;
2181eda14cbcSMatt Macy 	boolean_t drop_struct_rwlock = B_FALSE;
2182eda14cbcSMatt Macy 
2183eda14cbcSMatt Macy 	ASSERT(tx->tx_txg != 0);
2184eda14cbcSMatt Macy 	ASSERT(!zfs_refcount_is_zero(&db->db_holds));
2185eda14cbcSMatt Macy 	DMU_TX_DIRTY_BUF(tx, db);
2186eda14cbcSMatt Macy 
2187eda14cbcSMatt Macy 	DB_DNODE_ENTER(db);
2188eda14cbcSMatt Macy 	dn = DB_DNODE(db);
2189eda14cbcSMatt Macy 	/*
2190eda14cbcSMatt Macy 	 * Shouldn't dirty a regular buffer in syncing context.  Private
2191eda14cbcSMatt Macy 	 * objects may be dirtied in syncing context, but only if they
2192eda14cbcSMatt Macy 	 * were already pre-dirtied in open context.
2193eda14cbcSMatt Macy 	 */
2194eda14cbcSMatt Macy #ifdef ZFS_DEBUG
2195eda14cbcSMatt Macy 	if (dn->dn_objset->os_dsl_dataset != NULL) {
2196eda14cbcSMatt Macy 		rrw_enter(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock,
2197eda14cbcSMatt Macy 		    RW_READER, FTAG);
2198eda14cbcSMatt Macy 	}
2199eda14cbcSMatt Macy 	ASSERT(!dmu_tx_is_syncing(tx) ||
2200eda14cbcSMatt Macy 	    BP_IS_HOLE(dn->dn_objset->os_rootbp) ||
2201eda14cbcSMatt Macy 	    DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
2202eda14cbcSMatt Macy 	    dn->dn_objset->os_dsl_dataset == NULL);
2203eda14cbcSMatt Macy 	if (dn->dn_objset->os_dsl_dataset != NULL)
2204eda14cbcSMatt Macy 		rrw_exit(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock, FTAG);
2205eda14cbcSMatt Macy #endif
2206eda14cbcSMatt Macy 	/*
2207eda14cbcSMatt Macy 	 * We make this assert for private objects as well, but after we
2208eda14cbcSMatt Macy 	 * check if we're already dirty.  They are allowed to re-dirty
2209eda14cbcSMatt Macy 	 * in syncing context.
2210eda14cbcSMatt Macy 	 */
2211eda14cbcSMatt Macy 	ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
2212eda14cbcSMatt Macy 	    dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
2213eda14cbcSMatt Macy 	    (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
2214eda14cbcSMatt Macy 
2215eda14cbcSMatt Macy 	mutex_enter(&db->db_mtx);
2216eda14cbcSMatt Macy 	/*
2217eda14cbcSMatt Macy 	 * XXX make this true for indirects too?  The problem is that
2218eda14cbcSMatt Macy 	 * transactions created with dmu_tx_create_assigned() from
2219eda14cbcSMatt Macy 	 * syncing context don't bother holding ahead.
2220eda14cbcSMatt Macy 	 */
2221eda14cbcSMatt Macy 	ASSERT(db->db_level != 0 ||
2222eda14cbcSMatt Macy 	    db->db_state == DB_CACHED || db->db_state == DB_FILL ||
2223eda14cbcSMatt Macy 	    db->db_state == DB_NOFILL);
2224eda14cbcSMatt Macy 
2225eda14cbcSMatt Macy 	mutex_enter(&dn->dn_mtx);
2226eda14cbcSMatt Macy 	dnode_set_dirtyctx(dn, tx, db);
2227eda14cbcSMatt Macy 	if (tx->tx_txg > dn->dn_dirty_txg)
2228eda14cbcSMatt Macy 		dn->dn_dirty_txg = tx->tx_txg;
2229eda14cbcSMatt Macy 	mutex_exit(&dn->dn_mtx);
2230eda14cbcSMatt Macy 
2231eda14cbcSMatt Macy 	if (db->db_blkid == DMU_SPILL_BLKID)
2232eda14cbcSMatt Macy 		dn->dn_have_spill = B_TRUE;
2233eda14cbcSMatt Macy 
2234eda14cbcSMatt Macy 	/*
2235eda14cbcSMatt Macy 	 * If this buffer is already dirty, we're done.
2236eda14cbcSMatt Macy 	 */
2237eda14cbcSMatt Macy 	dr_head = list_head(&db->db_dirty_records);
2238eda14cbcSMatt Macy 	ASSERT(dr_head == NULL || dr_head->dr_txg <= tx->tx_txg ||
2239eda14cbcSMatt Macy 	    db->db.db_object == DMU_META_DNODE_OBJECT);
2240eda14cbcSMatt Macy 	dr_next = dbuf_find_dirty_lte(db, tx->tx_txg);
2241eda14cbcSMatt Macy 	if (dr_next && dr_next->dr_txg == tx->tx_txg) {
2242eda14cbcSMatt Macy 		DB_DNODE_EXIT(db);
2243eda14cbcSMatt Macy 
2244eda14cbcSMatt Macy 		dbuf_redirty(dr_next);
2245eda14cbcSMatt Macy 		mutex_exit(&db->db_mtx);
2246eda14cbcSMatt Macy 		return (dr_next);
2247eda14cbcSMatt Macy 	}
2248eda14cbcSMatt Macy 
2249eda14cbcSMatt Macy 	/*
2250eda14cbcSMatt Macy 	 * Only valid if not already dirty.
2251eda14cbcSMatt Macy 	 */
2252eda14cbcSMatt Macy 	ASSERT(dn->dn_object == 0 ||
2253eda14cbcSMatt Macy 	    dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
2254eda14cbcSMatt Macy 	    (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
2255eda14cbcSMatt Macy 
2256eda14cbcSMatt Macy 	ASSERT3U(dn->dn_nlevels, >, db->db_level);
2257eda14cbcSMatt Macy 
2258eda14cbcSMatt Macy 	/*
2259eda14cbcSMatt Macy 	 * We should only be dirtying in syncing context if it's the
2260eda14cbcSMatt Macy 	 * mos or we're initializing the os or it's a special object.
2261eda14cbcSMatt Macy 	 * However, we are allowed to dirty in syncing context provided
2262eda14cbcSMatt Macy 	 * we already dirtied it in open context.  Hence we must make
2263eda14cbcSMatt Macy 	 * this assertion only if we're not already dirty.
2264eda14cbcSMatt Macy 	 */
2265eda14cbcSMatt Macy 	os = dn->dn_objset;
2266eda14cbcSMatt Macy 	VERIFY3U(tx->tx_txg, <=, spa_final_dirty_txg(os->os_spa));
2267eda14cbcSMatt Macy #ifdef ZFS_DEBUG
2268eda14cbcSMatt Macy 	if (dn->dn_objset->os_dsl_dataset != NULL)
2269eda14cbcSMatt Macy 		rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_READER, FTAG);
2270eda14cbcSMatt Macy 	ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
2271eda14cbcSMatt Macy 	    os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp));
2272eda14cbcSMatt Macy 	if (dn->dn_objset->os_dsl_dataset != NULL)
2273eda14cbcSMatt Macy 		rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG);
2274eda14cbcSMatt Macy #endif
2275eda14cbcSMatt Macy 	ASSERT(db->db.db_size != 0);
2276eda14cbcSMatt Macy 
2277eda14cbcSMatt Macy 	dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
2278eda14cbcSMatt Macy 
2279eda14cbcSMatt Macy 	if (db->db_blkid != DMU_BONUS_BLKID) {
2280eda14cbcSMatt Macy 		dmu_objset_willuse_space(os, db->db.db_size, tx);
2281eda14cbcSMatt Macy 	}
2282eda14cbcSMatt Macy 
2283eda14cbcSMatt Macy 	/*
2284eda14cbcSMatt Macy 	 * If this buffer is dirty in an old transaction group we need
2285eda14cbcSMatt Macy 	 * to make a copy of it so that the changes we make in this
2286eda14cbcSMatt Macy 	 * transaction group won't leak out when we sync the older txg.
2287eda14cbcSMatt Macy 	 */
2288eda14cbcSMatt Macy 	dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP);
2289eda14cbcSMatt Macy 	list_link_init(&dr->dr_dirty_node);
2290eda14cbcSMatt Macy 	list_link_init(&dr->dr_dbuf_node);
22917877fdebSMatt Macy 	dr->dr_dnode = dn;
2292eda14cbcSMatt Macy 	if (db->db_level == 0) {
2293eda14cbcSMatt Macy 		void *data_old = db->db_buf;
2294eda14cbcSMatt Macy 
2295eda14cbcSMatt Macy 		if (db->db_state != DB_NOFILL) {
2296eda14cbcSMatt Macy 			if (db->db_blkid == DMU_BONUS_BLKID) {
2297eda14cbcSMatt Macy 				dbuf_fix_old_data(db, tx->tx_txg);
2298eda14cbcSMatt Macy 				data_old = db->db.db_data;
2299eda14cbcSMatt Macy 			} else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
2300eda14cbcSMatt Macy 				/*
2301eda14cbcSMatt Macy 				 * Release the data buffer from the cache so
2302eda14cbcSMatt Macy 				 * that we can modify it without impacting
2303eda14cbcSMatt Macy 				 * possible other users of this cached data
2304eda14cbcSMatt Macy 				 * block.  Note that indirect blocks and
2305eda14cbcSMatt Macy 				 * private objects are not released until the
2306eda14cbcSMatt Macy 				 * syncing state (since they are only modified
2307eda14cbcSMatt Macy 				 * then).
2308eda14cbcSMatt Macy 				 */
2309eda14cbcSMatt Macy 				arc_release(db->db_buf, db);
2310eda14cbcSMatt Macy 				dbuf_fix_old_data(db, tx->tx_txg);
2311eda14cbcSMatt Macy 				data_old = db->db_buf;
2312eda14cbcSMatt Macy 			}
2313eda14cbcSMatt Macy 			ASSERT(data_old != NULL);
2314eda14cbcSMatt Macy 		}
2315eda14cbcSMatt Macy 		dr->dt.dl.dr_data = data_old;
2316eda14cbcSMatt Macy 	} else {
2317eda14cbcSMatt Macy 		mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_NOLOCKDEP, NULL);
2318eda14cbcSMatt Macy 		list_create(&dr->dt.di.dr_children,
2319eda14cbcSMatt Macy 		    sizeof (dbuf_dirty_record_t),
2320eda14cbcSMatt Macy 		    offsetof(dbuf_dirty_record_t, dr_dirty_node));
2321eda14cbcSMatt Macy 	}
2322eda14cbcSMatt Macy 	if (db->db_blkid != DMU_BONUS_BLKID)
2323eda14cbcSMatt Macy 		dr->dr_accounted = db->db.db_size;
2324eda14cbcSMatt Macy 	dr->dr_dbuf = db;
2325eda14cbcSMatt Macy 	dr->dr_txg = tx->tx_txg;
2326eda14cbcSMatt Macy 	list_insert_before(&db->db_dirty_records, dr_next, dr);
2327eda14cbcSMatt Macy 
2328eda14cbcSMatt Macy 	/*
2329eda14cbcSMatt Macy 	 * We could have been freed_in_flight between the dbuf_noread
2330eda14cbcSMatt Macy 	 * and dbuf_dirty.  We win, as though the dbuf_noread() had
2331eda14cbcSMatt Macy 	 * happened after the free.
2332eda14cbcSMatt Macy 	 */
2333eda14cbcSMatt Macy 	if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
2334eda14cbcSMatt Macy 	    db->db_blkid != DMU_SPILL_BLKID) {
2335eda14cbcSMatt Macy 		mutex_enter(&dn->dn_mtx);
2336eda14cbcSMatt Macy 		if (dn->dn_free_ranges[txgoff] != NULL) {
2337eda14cbcSMatt Macy 			range_tree_clear(dn->dn_free_ranges[txgoff],
2338eda14cbcSMatt Macy 			    db->db_blkid, 1);
2339eda14cbcSMatt Macy 		}
2340eda14cbcSMatt Macy 		mutex_exit(&dn->dn_mtx);
2341eda14cbcSMatt Macy 		db->db_freed_in_flight = FALSE;
2342eda14cbcSMatt Macy 	}
2343eda14cbcSMatt Macy 
2344eda14cbcSMatt Macy 	/*
2345eda14cbcSMatt Macy 	 * This buffer is now part of this txg
2346eda14cbcSMatt Macy 	 */
2347eda14cbcSMatt Macy 	dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg);
2348eda14cbcSMatt Macy 	db->db_dirtycnt += 1;
2349eda14cbcSMatt Macy 	ASSERT3U(db->db_dirtycnt, <=, 3);
2350eda14cbcSMatt Macy 
2351eda14cbcSMatt Macy 	mutex_exit(&db->db_mtx);
2352eda14cbcSMatt Macy 
2353eda14cbcSMatt Macy 	if (db->db_blkid == DMU_BONUS_BLKID ||
2354eda14cbcSMatt Macy 	    db->db_blkid == DMU_SPILL_BLKID) {
2355eda14cbcSMatt Macy 		mutex_enter(&dn->dn_mtx);
2356eda14cbcSMatt Macy 		ASSERT(!list_link_active(&dr->dr_dirty_node));
2357eda14cbcSMatt Macy 		list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
2358eda14cbcSMatt Macy 		mutex_exit(&dn->dn_mtx);
2359eda14cbcSMatt Macy 		dnode_setdirty(dn, tx);
2360eda14cbcSMatt Macy 		DB_DNODE_EXIT(db);
2361eda14cbcSMatt Macy 		return (dr);
2362eda14cbcSMatt Macy 	}
2363eda14cbcSMatt Macy 
2364eda14cbcSMatt Macy 	if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
2365eda14cbcSMatt Macy 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
2366eda14cbcSMatt Macy 		drop_struct_rwlock = B_TRUE;
2367eda14cbcSMatt Macy 	}
2368eda14cbcSMatt Macy 
2369eda14cbcSMatt Macy 	/*
2370eda14cbcSMatt Macy 	 * If we are overwriting a dedup BP, then unless it is snapshotted,
2371eda14cbcSMatt Macy 	 * when we get to syncing context we will need to decrement its
2372eda14cbcSMatt Macy 	 * refcount in the DDT.  Prefetch the relevant DDT block so that
2373eda14cbcSMatt Macy 	 * syncing context won't have to wait for the i/o.
2374eda14cbcSMatt Macy 	 */
2375eda14cbcSMatt Macy 	if (db->db_blkptr != NULL) {
2376eda14cbcSMatt Macy 		db_lock_type_t dblt = dmu_buf_lock_parent(db, RW_READER, FTAG);
2377eda14cbcSMatt Macy 		ddt_prefetch(os->os_spa, db->db_blkptr);
2378eda14cbcSMatt Macy 		dmu_buf_unlock_parent(db, dblt, FTAG);
2379eda14cbcSMatt Macy 	}
2380eda14cbcSMatt Macy 
2381eda14cbcSMatt Macy 	/*
2382eda14cbcSMatt Macy 	 * We need to hold the dn_struct_rwlock to make this assertion,
2383eda14cbcSMatt Macy 	 * because it protects dn_phys / dn_next_nlevels from changing.
2384eda14cbcSMatt Macy 	 */
2385eda14cbcSMatt Macy 	ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) ||
2386eda14cbcSMatt Macy 	    dn->dn_phys->dn_nlevels > db->db_level ||
2387eda14cbcSMatt Macy 	    dn->dn_next_nlevels[txgoff] > db->db_level ||
2388eda14cbcSMatt Macy 	    dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level ||
2389eda14cbcSMatt Macy 	    dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level);
2390eda14cbcSMatt Macy 
2391eda14cbcSMatt Macy 
2392eda14cbcSMatt Macy 	if (db->db_level == 0) {
2393eda14cbcSMatt Macy 		ASSERT(!db->db_objset->os_raw_receive ||
2394eda14cbcSMatt Macy 		    dn->dn_maxblkid >= db->db_blkid);
2395eda14cbcSMatt Macy 		dnode_new_blkid(dn, db->db_blkid, tx,
2396eda14cbcSMatt Macy 		    drop_struct_rwlock, B_FALSE);
2397eda14cbcSMatt Macy 		ASSERT(dn->dn_maxblkid >= db->db_blkid);
2398eda14cbcSMatt Macy 	}
2399eda14cbcSMatt Macy 
2400eda14cbcSMatt Macy 	if (db->db_level+1 < dn->dn_nlevels) {
2401eda14cbcSMatt Macy 		dmu_buf_impl_t *parent = db->db_parent;
2402eda14cbcSMatt Macy 		dbuf_dirty_record_t *di;
2403eda14cbcSMatt Macy 		int parent_held = FALSE;
2404eda14cbcSMatt Macy 
2405eda14cbcSMatt Macy 		if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
2406eda14cbcSMatt Macy 			int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
2407eda14cbcSMatt Macy 			parent = dbuf_hold_level(dn, db->db_level + 1,
2408eda14cbcSMatt Macy 			    db->db_blkid >> epbs, FTAG);
2409eda14cbcSMatt Macy 			ASSERT(parent != NULL);
2410eda14cbcSMatt Macy 			parent_held = TRUE;
2411eda14cbcSMatt Macy 		}
2412eda14cbcSMatt Macy 		if (drop_struct_rwlock)
2413eda14cbcSMatt Macy 			rw_exit(&dn->dn_struct_rwlock);
2414eda14cbcSMatt Macy 		ASSERT3U(db->db_level + 1, ==, parent->db_level);
2415eda14cbcSMatt Macy 		di = dbuf_dirty(parent, tx);
2416eda14cbcSMatt Macy 		if (parent_held)
2417eda14cbcSMatt Macy 			dbuf_rele(parent, FTAG);
2418eda14cbcSMatt Macy 
2419eda14cbcSMatt Macy 		mutex_enter(&db->db_mtx);
2420eda14cbcSMatt Macy 		/*
2421eda14cbcSMatt Macy 		 * Since we've dropped the mutex, it's possible that
2422eda14cbcSMatt Macy 		 * dbuf_undirty() might have changed this out from under us.
2423eda14cbcSMatt Macy 		 */
2424eda14cbcSMatt Macy 		if (list_head(&db->db_dirty_records) == dr ||
2425eda14cbcSMatt Macy 		    dn->dn_object == DMU_META_DNODE_OBJECT) {
2426eda14cbcSMatt Macy 			mutex_enter(&di->dt.di.dr_mtx);
2427eda14cbcSMatt Macy 			ASSERT3U(di->dr_txg, ==, tx->tx_txg);
2428eda14cbcSMatt Macy 			ASSERT(!list_link_active(&dr->dr_dirty_node));
2429eda14cbcSMatt Macy 			list_insert_tail(&di->dt.di.dr_children, dr);
2430eda14cbcSMatt Macy 			mutex_exit(&di->dt.di.dr_mtx);
2431eda14cbcSMatt Macy 			dr->dr_parent = di;
2432eda14cbcSMatt Macy 		}
2433eda14cbcSMatt Macy 		mutex_exit(&db->db_mtx);
2434eda14cbcSMatt Macy 	} else {
2435eda14cbcSMatt Macy 		ASSERT(db->db_level + 1 == dn->dn_nlevels);
2436eda14cbcSMatt Macy 		ASSERT(db->db_blkid < dn->dn_nblkptr);
2437eda14cbcSMatt Macy 		ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf);
2438eda14cbcSMatt Macy 		mutex_enter(&dn->dn_mtx);
2439eda14cbcSMatt Macy 		ASSERT(!list_link_active(&dr->dr_dirty_node));
2440eda14cbcSMatt Macy 		list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
2441eda14cbcSMatt Macy 		mutex_exit(&dn->dn_mtx);
2442eda14cbcSMatt Macy 		if (drop_struct_rwlock)
2443eda14cbcSMatt Macy 			rw_exit(&dn->dn_struct_rwlock);
2444eda14cbcSMatt Macy 	}
2445eda14cbcSMatt Macy 
2446eda14cbcSMatt Macy 	dnode_setdirty(dn, tx);
2447eda14cbcSMatt Macy 	DB_DNODE_EXIT(db);
2448eda14cbcSMatt Macy 	return (dr);
2449eda14cbcSMatt Macy }
2450eda14cbcSMatt Macy 
2451eda14cbcSMatt Macy static void
2452eda14cbcSMatt Macy dbuf_undirty_bonus(dbuf_dirty_record_t *dr)
2453eda14cbcSMatt Macy {
2454eda14cbcSMatt Macy 	dmu_buf_impl_t *db = dr->dr_dbuf;
2455eda14cbcSMatt Macy 
2456eda14cbcSMatt Macy 	if (dr->dt.dl.dr_data != db->db.db_data) {
24577877fdebSMatt Macy 		struct dnode *dn = dr->dr_dnode;
2458eda14cbcSMatt Macy 		int max_bonuslen = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots);
2459eda14cbcSMatt Macy 
2460eda14cbcSMatt Macy 		kmem_free(dr->dt.dl.dr_data, max_bonuslen);
2461eda14cbcSMatt Macy 		arc_space_return(max_bonuslen, ARC_SPACE_BONUS);
2462eda14cbcSMatt Macy 	}
2463eda14cbcSMatt Macy 	db->db_data_pending = NULL;
2464eda14cbcSMatt Macy 	ASSERT(list_next(&db->db_dirty_records, dr) == NULL);
2465eda14cbcSMatt Macy 	list_remove(&db->db_dirty_records, dr);
2466eda14cbcSMatt Macy 	if (dr->dr_dbuf->db_level != 0) {
2467eda14cbcSMatt Macy 		mutex_destroy(&dr->dt.di.dr_mtx);
2468eda14cbcSMatt Macy 		list_destroy(&dr->dt.di.dr_children);
2469eda14cbcSMatt Macy 	}
2470eda14cbcSMatt Macy 	kmem_free(dr, sizeof (dbuf_dirty_record_t));
2471eda14cbcSMatt Macy 	ASSERT3U(db->db_dirtycnt, >, 0);
2472eda14cbcSMatt Macy 	db->db_dirtycnt -= 1;
2473eda14cbcSMatt Macy }
2474eda14cbcSMatt Macy 
2475eda14cbcSMatt Macy /*
2476eda14cbcSMatt Macy  * Undirty a buffer in the transaction group referenced by the given
2477eda14cbcSMatt Macy  * transaction.  Return whether this evicted the dbuf.
2478eda14cbcSMatt Macy  */
2479eda14cbcSMatt Macy static boolean_t
2480eda14cbcSMatt Macy dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
2481eda14cbcSMatt Macy {
2482eda14cbcSMatt Macy 	uint64_t txg = tx->tx_txg;
2483eda14cbcSMatt Macy 
2484eda14cbcSMatt Macy 	ASSERT(txg != 0);
2485eda14cbcSMatt Macy 
2486eda14cbcSMatt Macy 	/*
2487eda14cbcSMatt Macy 	 * Due to our use of dn_nlevels below, this can only be called
2488eda14cbcSMatt Macy 	 * in open context, unless we are operating on the MOS.
2489eda14cbcSMatt Macy 	 * From syncing context, dn_nlevels may be different from the
2490eda14cbcSMatt Macy 	 * dn_nlevels used when dbuf was dirtied.
2491eda14cbcSMatt Macy 	 */
2492eda14cbcSMatt Macy 	ASSERT(db->db_objset ==
2493eda14cbcSMatt Macy 	    dmu_objset_pool(db->db_objset)->dp_meta_objset ||
2494eda14cbcSMatt Macy 	    txg != spa_syncing_txg(dmu_objset_spa(db->db_objset)));
2495eda14cbcSMatt Macy 	ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2496eda14cbcSMatt Macy 	ASSERT0(db->db_level);
2497eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&db->db_mtx));
2498eda14cbcSMatt Macy 
2499eda14cbcSMatt Macy 	/*
2500eda14cbcSMatt Macy 	 * If this buffer is not dirty, we're done.
2501eda14cbcSMatt Macy 	 */
25027877fdebSMatt Macy 	dbuf_dirty_record_t *dr = dbuf_find_dirty_eq(db, txg);
2503eda14cbcSMatt Macy 	if (dr == NULL)
2504eda14cbcSMatt Macy 		return (B_FALSE);
2505eda14cbcSMatt Macy 	ASSERT(dr->dr_dbuf == db);
2506eda14cbcSMatt Macy 
25077877fdebSMatt Macy 	dnode_t *dn = dr->dr_dnode;
2508eda14cbcSMatt Macy 
2509eda14cbcSMatt Macy 	dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
2510eda14cbcSMatt Macy 
2511eda14cbcSMatt Macy 	ASSERT(db->db.db_size != 0);
2512eda14cbcSMatt Macy 
2513eda14cbcSMatt Macy 	dsl_pool_undirty_space(dmu_objset_pool(dn->dn_objset),
2514eda14cbcSMatt Macy 	    dr->dr_accounted, txg);
2515eda14cbcSMatt Macy 
2516eda14cbcSMatt Macy 	list_remove(&db->db_dirty_records, dr);
2517eda14cbcSMatt Macy 
2518eda14cbcSMatt Macy 	/*
2519eda14cbcSMatt Macy 	 * Note that there are three places in dbuf_dirty()
2520eda14cbcSMatt Macy 	 * where this dirty record may be put on a list.
2521eda14cbcSMatt Macy 	 * Make sure to do a list_remove corresponding to
2522eda14cbcSMatt Macy 	 * every one of those list_insert calls.
2523eda14cbcSMatt Macy 	 */
2524eda14cbcSMatt Macy 	if (dr->dr_parent) {
2525eda14cbcSMatt Macy 		mutex_enter(&dr->dr_parent->dt.di.dr_mtx);
2526eda14cbcSMatt Macy 		list_remove(&dr->dr_parent->dt.di.dr_children, dr);
2527eda14cbcSMatt Macy 		mutex_exit(&dr->dr_parent->dt.di.dr_mtx);
2528eda14cbcSMatt Macy 	} else if (db->db_blkid == DMU_SPILL_BLKID ||
2529eda14cbcSMatt Macy 	    db->db_level + 1 == dn->dn_nlevels) {
2530eda14cbcSMatt Macy 		ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf);
2531eda14cbcSMatt Macy 		mutex_enter(&dn->dn_mtx);
2532eda14cbcSMatt Macy 		list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr);
2533eda14cbcSMatt Macy 		mutex_exit(&dn->dn_mtx);
2534eda14cbcSMatt Macy 	}
2535eda14cbcSMatt Macy 
2536eda14cbcSMatt Macy 	if (db->db_state != DB_NOFILL) {
2537eda14cbcSMatt Macy 		dbuf_unoverride(dr);
2538eda14cbcSMatt Macy 
2539eda14cbcSMatt Macy 		ASSERT(db->db_buf != NULL);
2540eda14cbcSMatt Macy 		ASSERT(dr->dt.dl.dr_data != NULL);
2541eda14cbcSMatt Macy 		if (dr->dt.dl.dr_data != db->db_buf)
2542eda14cbcSMatt Macy 			arc_buf_destroy(dr->dt.dl.dr_data, db);
2543eda14cbcSMatt Macy 	}
2544eda14cbcSMatt Macy 
2545eda14cbcSMatt Macy 	kmem_free(dr, sizeof (dbuf_dirty_record_t));
2546eda14cbcSMatt Macy 
2547eda14cbcSMatt Macy 	ASSERT(db->db_dirtycnt > 0);
2548eda14cbcSMatt Macy 	db->db_dirtycnt -= 1;
2549eda14cbcSMatt Macy 
2550eda14cbcSMatt Macy 	if (zfs_refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) {
2551eda14cbcSMatt Macy 		ASSERT(db->db_state == DB_NOFILL || arc_released(db->db_buf));
2552eda14cbcSMatt Macy 		dbuf_destroy(db);
2553eda14cbcSMatt Macy 		return (B_TRUE);
2554eda14cbcSMatt Macy 	}
2555eda14cbcSMatt Macy 
2556eda14cbcSMatt Macy 	return (B_FALSE);
2557eda14cbcSMatt Macy }
2558eda14cbcSMatt Macy 
2559eda14cbcSMatt Macy static void
2560eda14cbcSMatt Macy dmu_buf_will_dirty_impl(dmu_buf_t *db_fake, int flags, dmu_tx_t *tx)
2561eda14cbcSMatt Macy {
2562eda14cbcSMatt Macy 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2563eda14cbcSMatt Macy 
2564eda14cbcSMatt Macy 	ASSERT(tx->tx_txg != 0);
2565eda14cbcSMatt Macy 	ASSERT(!zfs_refcount_is_zero(&db->db_holds));
2566eda14cbcSMatt Macy 
2567eda14cbcSMatt Macy 	/*
2568eda14cbcSMatt Macy 	 * Quick check for dirtiness.  For already dirty blocks, this
2569eda14cbcSMatt Macy 	 * reduces runtime of this function by >90%, and overall performance
2570eda14cbcSMatt Macy 	 * by 50% for some workloads (e.g. file deletion with indirect blocks
2571eda14cbcSMatt Macy 	 * cached).
2572eda14cbcSMatt Macy 	 */
2573eda14cbcSMatt Macy 	mutex_enter(&db->db_mtx);
2574eda14cbcSMatt Macy 
2575eda14cbcSMatt Macy 	if (db->db_state == DB_CACHED) {
2576eda14cbcSMatt Macy 		dbuf_dirty_record_t *dr = dbuf_find_dirty_eq(db, tx->tx_txg);
2577eda14cbcSMatt Macy 		/*
2578eda14cbcSMatt Macy 		 * It's possible that it is already dirty but not cached,
2579eda14cbcSMatt Macy 		 * because there are some calls to dbuf_dirty() that don't
2580eda14cbcSMatt Macy 		 * go through dmu_buf_will_dirty().
2581eda14cbcSMatt Macy 		 */
2582eda14cbcSMatt Macy 		if (dr != NULL) {
2583eda14cbcSMatt Macy 			/* This dbuf is already dirty and cached. */
2584eda14cbcSMatt Macy 			dbuf_redirty(dr);
2585eda14cbcSMatt Macy 			mutex_exit(&db->db_mtx);
2586eda14cbcSMatt Macy 			return;
2587eda14cbcSMatt Macy 		}
2588eda14cbcSMatt Macy 	}
2589eda14cbcSMatt Macy 	mutex_exit(&db->db_mtx);
2590eda14cbcSMatt Macy 
2591eda14cbcSMatt Macy 	DB_DNODE_ENTER(db);
2592eda14cbcSMatt Macy 	if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock))
2593eda14cbcSMatt Macy 		flags |= DB_RF_HAVESTRUCT;
2594eda14cbcSMatt Macy 	DB_DNODE_EXIT(db);
2595eda14cbcSMatt Macy 	(void) dbuf_read(db, NULL, flags);
2596eda14cbcSMatt Macy 	(void) dbuf_dirty(db, tx);
2597eda14cbcSMatt Macy }
2598eda14cbcSMatt Macy 
2599eda14cbcSMatt Macy void
2600eda14cbcSMatt Macy dmu_buf_will_dirty(dmu_buf_t *db_fake, dmu_tx_t *tx)
2601eda14cbcSMatt Macy {
2602eda14cbcSMatt Macy 	dmu_buf_will_dirty_impl(db_fake,
2603eda14cbcSMatt Macy 	    DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH, tx);
2604eda14cbcSMatt Macy }
2605eda14cbcSMatt Macy 
2606eda14cbcSMatt Macy boolean_t
2607eda14cbcSMatt Macy dmu_buf_is_dirty(dmu_buf_t *db_fake, dmu_tx_t *tx)
2608eda14cbcSMatt Macy {
2609eda14cbcSMatt Macy 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2610eda14cbcSMatt Macy 	dbuf_dirty_record_t *dr;
2611eda14cbcSMatt Macy 
2612eda14cbcSMatt Macy 	mutex_enter(&db->db_mtx);
2613eda14cbcSMatt Macy 	dr = dbuf_find_dirty_eq(db, tx->tx_txg);
2614eda14cbcSMatt Macy 	mutex_exit(&db->db_mtx);
2615eda14cbcSMatt Macy 	return (dr != NULL);
2616eda14cbcSMatt Macy }
2617eda14cbcSMatt Macy 
2618eda14cbcSMatt Macy void
2619eda14cbcSMatt Macy dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
2620eda14cbcSMatt Macy {
2621eda14cbcSMatt Macy 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2622eda14cbcSMatt Macy 
2623eda14cbcSMatt Macy 	db->db_state = DB_NOFILL;
2624eda14cbcSMatt Macy 	DTRACE_SET_STATE(db, "allocating NOFILL buffer");
2625eda14cbcSMatt Macy 	dmu_buf_will_fill(db_fake, tx);
2626eda14cbcSMatt Macy }
2627eda14cbcSMatt Macy 
2628eda14cbcSMatt Macy void
2629eda14cbcSMatt Macy dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
2630eda14cbcSMatt Macy {
2631eda14cbcSMatt Macy 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2632eda14cbcSMatt Macy 
2633eda14cbcSMatt Macy 	ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2634eda14cbcSMatt Macy 	ASSERT(tx->tx_txg != 0);
2635eda14cbcSMatt Macy 	ASSERT(db->db_level == 0);
2636eda14cbcSMatt Macy 	ASSERT(!zfs_refcount_is_zero(&db->db_holds));
2637eda14cbcSMatt Macy 
2638eda14cbcSMatt Macy 	ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
2639eda14cbcSMatt Macy 	    dmu_tx_private_ok(tx));
2640eda14cbcSMatt Macy 
2641eda14cbcSMatt Macy 	dbuf_noread(db);
2642eda14cbcSMatt Macy 	(void) dbuf_dirty(db, tx);
2643eda14cbcSMatt Macy }
2644eda14cbcSMatt Macy 
2645eda14cbcSMatt Macy /*
2646eda14cbcSMatt Macy  * This function is effectively the same as dmu_buf_will_dirty(), but
2647eda14cbcSMatt Macy  * indicates the caller expects raw encrypted data in the db, and provides
2648eda14cbcSMatt Macy  * the crypt params (byteorder, salt, iv, mac) which should be stored in the
2649eda14cbcSMatt Macy  * blkptr_t when this dbuf is written.  This is only used for blocks of
2650eda14cbcSMatt Macy  * dnodes, during raw receive.
2651eda14cbcSMatt Macy  */
2652eda14cbcSMatt Macy void
2653eda14cbcSMatt Macy dmu_buf_set_crypt_params(dmu_buf_t *db_fake, boolean_t byteorder,
2654eda14cbcSMatt Macy     const uint8_t *salt, const uint8_t *iv, const uint8_t *mac, dmu_tx_t *tx)
2655eda14cbcSMatt Macy {
2656eda14cbcSMatt Macy 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2657eda14cbcSMatt Macy 	dbuf_dirty_record_t *dr;
2658eda14cbcSMatt Macy 
2659eda14cbcSMatt Macy 	/*
2660eda14cbcSMatt Macy 	 * dr_has_raw_params is only processed for blocks of dnodes
2661eda14cbcSMatt Macy 	 * (see dbuf_sync_dnode_leaf_crypt()).
2662eda14cbcSMatt Macy 	 */
2663eda14cbcSMatt Macy 	ASSERT3U(db->db.db_object, ==, DMU_META_DNODE_OBJECT);
2664eda14cbcSMatt Macy 	ASSERT3U(db->db_level, ==, 0);
2665eda14cbcSMatt Macy 	ASSERT(db->db_objset->os_raw_receive);
2666eda14cbcSMatt Macy 
2667eda14cbcSMatt Macy 	dmu_buf_will_dirty_impl(db_fake,
2668eda14cbcSMatt Macy 	    DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH | DB_RF_NO_DECRYPT, tx);
2669eda14cbcSMatt Macy 
2670eda14cbcSMatt Macy 	dr = dbuf_find_dirty_eq(db, tx->tx_txg);
2671eda14cbcSMatt Macy 
2672eda14cbcSMatt Macy 	ASSERT3P(dr, !=, NULL);
2673eda14cbcSMatt Macy 
2674eda14cbcSMatt Macy 	dr->dt.dl.dr_has_raw_params = B_TRUE;
2675eda14cbcSMatt Macy 	dr->dt.dl.dr_byteorder = byteorder;
2676da5137abSMartin Matuska 	memcpy(dr->dt.dl.dr_salt, salt, ZIO_DATA_SALT_LEN);
2677da5137abSMartin Matuska 	memcpy(dr->dt.dl.dr_iv, iv, ZIO_DATA_IV_LEN);
2678da5137abSMartin Matuska 	memcpy(dr->dt.dl.dr_mac, mac, ZIO_DATA_MAC_LEN);
2679eda14cbcSMatt Macy }
2680eda14cbcSMatt Macy 
2681eda14cbcSMatt Macy static void
2682eda14cbcSMatt Macy dbuf_override_impl(dmu_buf_impl_t *db, const blkptr_t *bp, dmu_tx_t *tx)
2683eda14cbcSMatt Macy {
2684eda14cbcSMatt Macy 	struct dirty_leaf *dl;
2685eda14cbcSMatt Macy 	dbuf_dirty_record_t *dr;
2686eda14cbcSMatt Macy 
2687eda14cbcSMatt Macy 	dr = list_head(&db->db_dirty_records);
2688dbd5678dSMartin Matuska 	ASSERT3P(dr, !=, NULL);
2689eda14cbcSMatt Macy 	ASSERT3U(dr->dr_txg, ==, tx->tx_txg);
2690eda14cbcSMatt Macy 	dl = &dr->dt.dl;
2691eda14cbcSMatt Macy 	dl->dr_overridden_by = *bp;
2692eda14cbcSMatt Macy 	dl->dr_override_state = DR_OVERRIDDEN;
2693eda14cbcSMatt Macy 	dl->dr_overridden_by.blk_birth = dr->dr_txg;
2694eda14cbcSMatt Macy }
2695eda14cbcSMatt Macy 
2696eda14cbcSMatt Macy void
2697eda14cbcSMatt Macy dmu_buf_fill_done(dmu_buf_t *dbuf, dmu_tx_t *tx)
2698eda14cbcSMatt Macy {
2699e92ffd9bSMartin Matuska 	(void) tx;
2700eda14cbcSMatt Macy 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
2701eda14cbcSMatt Macy 	dbuf_states_t old_state;
2702eda14cbcSMatt Macy 	mutex_enter(&db->db_mtx);
2703eda14cbcSMatt Macy 	DBUF_VERIFY(db);
2704eda14cbcSMatt Macy 
2705eda14cbcSMatt Macy 	old_state = db->db_state;
2706eda14cbcSMatt Macy 	db->db_state = DB_CACHED;
2707eda14cbcSMatt Macy 	if (old_state == DB_FILL) {
2708eda14cbcSMatt Macy 		if (db->db_level == 0 && db->db_freed_in_flight) {
2709eda14cbcSMatt Macy 			ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2710eda14cbcSMatt Macy 			/* we were freed while filling */
2711eda14cbcSMatt Macy 			/* XXX dbuf_undirty? */
2712da5137abSMartin Matuska 			memset(db->db.db_data, 0, db->db.db_size);
2713eda14cbcSMatt Macy 			db->db_freed_in_flight = FALSE;
2714eda14cbcSMatt Macy 			DTRACE_SET_STATE(db,
2715eda14cbcSMatt Macy 			    "fill done handling freed in flight");
2716eda14cbcSMatt Macy 		} else {
2717eda14cbcSMatt Macy 			DTRACE_SET_STATE(db, "fill done");
2718eda14cbcSMatt Macy 		}
2719eda14cbcSMatt Macy 		cv_broadcast(&db->db_changed);
2720eda14cbcSMatt Macy 	}
2721eda14cbcSMatt Macy 	mutex_exit(&db->db_mtx);
2722eda14cbcSMatt Macy }
2723eda14cbcSMatt Macy 
2724eda14cbcSMatt Macy void
2725eda14cbcSMatt Macy dmu_buf_write_embedded(dmu_buf_t *dbuf, void *data,
2726eda14cbcSMatt Macy     bp_embedded_type_t etype, enum zio_compress comp,
2727eda14cbcSMatt Macy     int uncompressed_size, int compressed_size, int byteorder,
2728eda14cbcSMatt Macy     dmu_tx_t *tx)
2729eda14cbcSMatt Macy {
2730eda14cbcSMatt Macy 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
2731eda14cbcSMatt Macy 	struct dirty_leaf *dl;
2732eda14cbcSMatt Macy 	dmu_object_type_t type;
2733eda14cbcSMatt Macy 	dbuf_dirty_record_t *dr;
2734eda14cbcSMatt Macy 
2735eda14cbcSMatt Macy 	if (etype == BP_EMBEDDED_TYPE_DATA) {
2736eda14cbcSMatt Macy 		ASSERT(spa_feature_is_active(dmu_objset_spa(db->db_objset),
2737eda14cbcSMatt Macy 		    SPA_FEATURE_EMBEDDED_DATA));
2738eda14cbcSMatt Macy 	}
2739eda14cbcSMatt Macy 
2740eda14cbcSMatt Macy 	DB_DNODE_ENTER(db);
2741eda14cbcSMatt Macy 	type = DB_DNODE(db)->dn_type;
2742eda14cbcSMatt Macy 	DB_DNODE_EXIT(db);
2743eda14cbcSMatt Macy 
2744eda14cbcSMatt Macy 	ASSERT0(db->db_level);
2745eda14cbcSMatt Macy 	ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2746eda14cbcSMatt Macy 
2747eda14cbcSMatt Macy 	dmu_buf_will_not_fill(dbuf, tx);
2748eda14cbcSMatt Macy 
2749eda14cbcSMatt Macy 	dr = list_head(&db->db_dirty_records);
2750dbd5678dSMartin Matuska 	ASSERT3P(dr, !=, NULL);
2751eda14cbcSMatt Macy 	ASSERT3U(dr->dr_txg, ==, tx->tx_txg);
2752eda14cbcSMatt Macy 	dl = &dr->dt.dl;
2753eda14cbcSMatt Macy 	encode_embedded_bp_compressed(&dl->dr_overridden_by,
2754eda14cbcSMatt Macy 	    data, comp, uncompressed_size, compressed_size);
2755eda14cbcSMatt Macy 	BPE_SET_ETYPE(&dl->dr_overridden_by, etype);
2756eda14cbcSMatt Macy 	BP_SET_TYPE(&dl->dr_overridden_by, type);
2757eda14cbcSMatt Macy 	BP_SET_LEVEL(&dl->dr_overridden_by, 0);
2758eda14cbcSMatt Macy 	BP_SET_BYTEORDER(&dl->dr_overridden_by, byteorder);
2759eda14cbcSMatt Macy 
2760eda14cbcSMatt Macy 	dl->dr_override_state = DR_OVERRIDDEN;
2761eda14cbcSMatt Macy 	dl->dr_overridden_by.blk_birth = dr->dr_txg;
2762eda14cbcSMatt Macy }
2763eda14cbcSMatt Macy 
2764eda14cbcSMatt Macy void
2765eda14cbcSMatt Macy dmu_buf_redact(dmu_buf_t *dbuf, dmu_tx_t *tx)
2766eda14cbcSMatt Macy {
2767eda14cbcSMatt Macy 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
2768eda14cbcSMatt Macy 	dmu_object_type_t type;
2769eda14cbcSMatt Macy 	ASSERT(dsl_dataset_feature_is_active(db->db_objset->os_dsl_dataset,
2770eda14cbcSMatt Macy 	    SPA_FEATURE_REDACTED_DATASETS));
2771eda14cbcSMatt Macy 
2772eda14cbcSMatt Macy 	DB_DNODE_ENTER(db);
2773eda14cbcSMatt Macy 	type = DB_DNODE(db)->dn_type;
2774eda14cbcSMatt Macy 	DB_DNODE_EXIT(db);
2775eda14cbcSMatt Macy 
2776eda14cbcSMatt Macy 	ASSERT0(db->db_level);
2777eda14cbcSMatt Macy 	dmu_buf_will_not_fill(dbuf, tx);
2778eda14cbcSMatt Macy 
2779eda14cbcSMatt Macy 	blkptr_t bp = { { { {0} } } };
2780eda14cbcSMatt Macy 	BP_SET_TYPE(&bp, type);
2781eda14cbcSMatt Macy 	BP_SET_LEVEL(&bp, 0);
2782eda14cbcSMatt Macy 	BP_SET_BIRTH(&bp, tx->tx_txg, 0);
2783eda14cbcSMatt Macy 	BP_SET_REDACTED(&bp);
2784eda14cbcSMatt Macy 	BPE_SET_LSIZE(&bp, dbuf->db_size);
2785eda14cbcSMatt Macy 
2786eda14cbcSMatt Macy 	dbuf_override_impl(db, &bp, tx);
2787eda14cbcSMatt Macy }
2788eda14cbcSMatt Macy 
2789eda14cbcSMatt Macy /*
2790eda14cbcSMatt Macy  * Directly assign a provided arc buf to a given dbuf if it's not referenced
2791eda14cbcSMatt Macy  * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
2792eda14cbcSMatt Macy  */
2793eda14cbcSMatt Macy void
2794eda14cbcSMatt Macy dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx)
2795eda14cbcSMatt Macy {
2796eda14cbcSMatt Macy 	ASSERT(!zfs_refcount_is_zero(&db->db_holds));
2797eda14cbcSMatt Macy 	ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2798eda14cbcSMatt Macy 	ASSERT(db->db_level == 0);
2799eda14cbcSMatt Macy 	ASSERT3U(dbuf_is_metadata(db), ==, arc_is_metadata(buf));
2800eda14cbcSMatt Macy 	ASSERT(buf != NULL);
2801eda14cbcSMatt Macy 	ASSERT3U(arc_buf_lsize(buf), ==, db->db.db_size);
2802eda14cbcSMatt Macy 	ASSERT(tx->tx_txg != 0);
2803eda14cbcSMatt Macy 
2804eda14cbcSMatt Macy 	arc_return_buf(buf, db);
2805eda14cbcSMatt Macy 	ASSERT(arc_released(buf));
2806eda14cbcSMatt Macy 
2807eda14cbcSMatt Macy 	mutex_enter(&db->db_mtx);
2808eda14cbcSMatt Macy 
2809eda14cbcSMatt Macy 	while (db->db_state == DB_READ || db->db_state == DB_FILL)
2810eda14cbcSMatt Macy 		cv_wait(&db->db_changed, &db->db_mtx);
2811eda14cbcSMatt Macy 
2812eda14cbcSMatt Macy 	ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED);
2813eda14cbcSMatt Macy 
2814eda14cbcSMatt Macy 	if (db->db_state == DB_CACHED &&
2815eda14cbcSMatt Macy 	    zfs_refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) {
2816eda14cbcSMatt Macy 		/*
2817eda14cbcSMatt Macy 		 * In practice, we will never have a case where we have an
2818eda14cbcSMatt Macy 		 * encrypted arc buffer while additional holds exist on the
2819eda14cbcSMatt Macy 		 * dbuf. We don't handle this here so we simply assert that
2820eda14cbcSMatt Macy 		 * fact instead.
2821eda14cbcSMatt Macy 		 */
2822eda14cbcSMatt Macy 		ASSERT(!arc_is_encrypted(buf));
2823eda14cbcSMatt Macy 		mutex_exit(&db->db_mtx);
2824eda14cbcSMatt Macy 		(void) dbuf_dirty(db, tx);
2825da5137abSMartin Matuska 		memcpy(db->db.db_data, buf->b_data, db->db.db_size);
2826eda14cbcSMatt Macy 		arc_buf_destroy(buf, db);
2827eda14cbcSMatt Macy 		return;
2828eda14cbcSMatt Macy 	}
2829eda14cbcSMatt Macy 
2830eda14cbcSMatt Macy 	if (db->db_state == DB_CACHED) {
2831eda14cbcSMatt Macy 		dbuf_dirty_record_t *dr = list_head(&db->db_dirty_records);
2832eda14cbcSMatt Macy 
2833eda14cbcSMatt Macy 		ASSERT(db->db_buf != NULL);
2834eda14cbcSMatt Macy 		if (dr != NULL && dr->dr_txg == tx->tx_txg) {
2835eda14cbcSMatt Macy 			ASSERT(dr->dt.dl.dr_data == db->db_buf);
2836eda14cbcSMatt Macy 
2837eda14cbcSMatt Macy 			if (!arc_released(db->db_buf)) {
2838eda14cbcSMatt Macy 				ASSERT(dr->dt.dl.dr_override_state ==
2839eda14cbcSMatt Macy 				    DR_OVERRIDDEN);
2840eda14cbcSMatt Macy 				arc_release(db->db_buf, db);
2841eda14cbcSMatt Macy 			}
2842eda14cbcSMatt Macy 			dr->dt.dl.dr_data = buf;
2843eda14cbcSMatt Macy 			arc_buf_destroy(db->db_buf, db);
2844eda14cbcSMatt Macy 		} else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) {
2845eda14cbcSMatt Macy 			arc_release(db->db_buf, db);
2846eda14cbcSMatt Macy 			arc_buf_destroy(db->db_buf, db);
2847eda14cbcSMatt Macy 		}
2848eda14cbcSMatt Macy 		db->db_buf = NULL;
2849eda14cbcSMatt Macy 	}
2850eda14cbcSMatt Macy 	ASSERT(db->db_buf == NULL);
2851eda14cbcSMatt Macy 	dbuf_set_data(db, buf);
2852eda14cbcSMatt Macy 	db->db_state = DB_FILL;
2853eda14cbcSMatt Macy 	DTRACE_SET_STATE(db, "filling assigned arcbuf");
2854eda14cbcSMatt Macy 	mutex_exit(&db->db_mtx);
2855eda14cbcSMatt Macy 	(void) dbuf_dirty(db, tx);
2856eda14cbcSMatt Macy 	dmu_buf_fill_done(&db->db, tx);
2857eda14cbcSMatt Macy }
2858eda14cbcSMatt Macy 
2859eda14cbcSMatt Macy void
2860eda14cbcSMatt Macy dbuf_destroy(dmu_buf_impl_t *db)
2861eda14cbcSMatt Macy {
2862eda14cbcSMatt Macy 	dnode_t *dn;
2863eda14cbcSMatt Macy 	dmu_buf_impl_t *parent = db->db_parent;
2864eda14cbcSMatt Macy 	dmu_buf_impl_t *dndb;
2865eda14cbcSMatt Macy 
2866eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&db->db_mtx));
2867eda14cbcSMatt Macy 	ASSERT(zfs_refcount_is_zero(&db->db_holds));
2868eda14cbcSMatt Macy 
2869eda14cbcSMatt Macy 	if (db->db_buf != NULL) {
2870eda14cbcSMatt Macy 		arc_buf_destroy(db->db_buf, db);
2871eda14cbcSMatt Macy 		db->db_buf = NULL;
2872eda14cbcSMatt Macy 	}
2873eda14cbcSMatt Macy 
2874eda14cbcSMatt Macy 	if (db->db_blkid == DMU_BONUS_BLKID) {
2875eda14cbcSMatt Macy 		int slots = DB_DNODE(db)->dn_num_slots;
2876eda14cbcSMatt Macy 		int bonuslen = DN_SLOTS_TO_BONUSLEN(slots);
2877eda14cbcSMatt Macy 		if (db->db.db_data != NULL) {
2878eda14cbcSMatt Macy 			kmem_free(db->db.db_data, bonuslen);
2879eda14cbcSMatt Macy 			arc_space_return(bonuslen, ARC_SPACE_BONUS);
2880eda14cbcSMatt Macy 			db->db_state = DB_UNCACHED;
2881eda14cbcSMatt Macy 			DTRACE_SET_STATE(db, "buffer cleared");
2882eda14cbcSMatt Macy 		}
2883eda14cbcSMatt Macy 	}
2884eda14cbcSMatt Macy 
2885eda14cbcSMatt Macy 	dbuf_clear_data(db);
2886eda14cbcSMatt Macy 
2887eda14cbcSMatt Macy 	if (multilist_link_active(&db->db_cache_link)) {
2888eda14cbcSMatt Macy 		ASSERT(db->db_caching_status == DB_DBUF_CACHE ||
2889eda14cbcSMatt Macy 		    db->db_caching_status == DB_DBUF_METADATA_CACHE);
2890eda14cbcSMatt Macy 
28913ff01b23SMartin Matuska 		multilist_remove(&dbuf_caches[db->db_caching_status].cache, db);
2892eda14cbcSMatt Macy 		(void) zfs_refcount_remove_many(
2893eda14cbcSMatt Macy 		    &dbuf_caches[db->db_caching_status].size,
2894eda14cbcSMatt Macy 		    db->db.db_size, db);
2895eda14cbcSMatt Macy 
2896eda14cbcSMatt Macy 		if (db->db_caching_status == DB_DBUF_METADATA_CACHE) {
2897eda14cbcSMatt Macy 			DBUF_STAT_BUMPDOWN(metadata_cache_count);
2898eda14cbcSMatt Macy 		} else {
2899eda14cbcSMatt Macy 			DBUF_STAT_BUMPDOWN(cache_levels[db->db_level]);
2900eda14cbcSMatt Macy 			DBUF_STAT_BUMPDOWN(cache_count);
2901eda14cbcSMatt Macy 			DBUF_STAT_DECR(cache_levels_bytes[db->db_level],
2902eda14cbcSMatt Macy 			    db->db.db_size);
2903eda14cbcSMatt Macy 		}
2904eda14cbcSMatt Macy 		db->db_caching_status = DB_NO_CACHE;
2905eda14cbcSMatt Macy 	}
2906eda14cbcSMatt Macy 
2907eda14cbcSMatt Macy 	ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL);
2908eda14cbcSMatt Macy 	ASSERT(db->db_data_pending == NULL);
2909eda14cbcSMatt Macy 	ASSERT(list_is_empty(&db->db_dirty_records));
2910eda14cbcSMatt Macy 
2911eda14cbcSMatt Macy 	db->db_state = DB_EVICTING;
2912eda14cbcSMatt Macy 	DTRACE_SET_STATE(db, "buffer eviction started");
2913eda14cbcSMatt Macy 	db->db_blkptr = NULL;
2914eda14cbcSMatt Macy 
2915eda14cbcSMatt Macy 	/*
2916eda14cbcSMatt Macy 	 * Now that db_state is DB_EVICTING, nobody else can find this via
2917eda14cbcSMatt Macy 	 * the hash table.  We can now drop db_mtx, which allows us to
2918eda14cbcSMatt Macy 	 * acquire the dn_dbufs_mtx.
2919eda14cbcSMatt Macy 	 */
2920eda14cbcSMatt Macy 	mutex_exit(&db->db_mtx);
2921eda14cbcSMatt Macy 
2922eda14cbcSMatt Macy 	DB_DNODE_ENTER(db);
2923eda14cbcSMatt Macy 	dn = DB_DNODE(db);
2924eda14cbcSMatt Macy 	dndb = dn->dn_dbuf;
2925eda14cbcSMatt Macy 	if (db->db_blkid != DMU_BONUS_BLKID) {
2926eda14cbcSMatt Macy 		boolean_t needlock = !MUTEX_HELD(&dn->dn_dbufs_mtx);
2927eda14cbcSMatt Macy 		if (needlock)
2928eda14cbcSMatt Macy 			mutex_enter_nested(&dn->dn_dbufs_mtx,
2929eda14cbcSMatt Macy 			    NESTED_SINGLE);
2930eda14cbcSMatt Macy 		avl_remove(&dn->dn_dbufs, db);
2931eda14cbcSMatt Macy 		membar_producer();
2932eda14cbcSMatt Macy 		DB_DNODE_EXIT(db);
2933eda14cbcSMatt Macy 		if (needlock)
2934eda14cbcSMatt Macy 			mutex_exit(&dn->dn_dbufs_mtx);
2935eda14cbcSMatt Macy 		/*
2936eda14cbcSMatt Macy 		 * Decrementing the dbuf count means that the hold corresponding
2937eda14cbcSMatt Macy 		 * to the removed dbuf is no longer discounted in dnode_move(),
2938eda14cbcSMatt Macy 		 * so the dnode cannot be moved until after we release the hold.
2939eda14cbcSMatt Macy 		 * The membar_producer() ensures visibility of the decremented
2940eda14cbcSMatt Macy 		 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
2941eda14cbcSMatt Macy 		 * release any lock.
2942eda14cbcSMatt Macy 		 */
2943eda14cbcSMatt Macy 		mutex_enter(&dn->dn_mtx);
2944eda14cbcSMatt Macy 		dnode_rele_and_unlock(dn, db, B_TRUE);
2945eda14cbcSMatt Macy 		db->db_dnode_handle = NULL;
2946eda14cbcSMatt Macy 
2947eda14cbcSMatt Macy 		dbuf_hash_remove(db);
2948eda14cbcSMatt Macy 	} else {
2949eda14cbcSMatt Macy 		DB_DNODE_EXIT(db);
2950eda14cbcSMatt Macy 	}
2951eda14cbcSMatt Macy 
2952eda14cbcSMatt Macy 	ASSERT(zfs_refcount_is_zero(&db->db_holds));
2953eda14cbcSMatt Macy 
2954eda14cbcSMatt Macy 	db->db_parent = NULL;
2955eda14cbcSMatt Macy 
2956eda14cbcSMatt Macy 	ASSERT(db->db_buf == NULL);
2957eda14cbcSMatt Macy 	ASSERT(db->db.db_data == NULL);
2958eda14cbcSMatt Macy 	ASSERT(db->db_hash_next == NULL);
2959eda14cbcSMatt Macy 	ASSERT(db->db_blkptr == NULL);
2960eda14cbcSMatt Macy 	ASSERT(db->db_data_pending == NULL);
2961eda14cbcSMatt Macy 	ASSERT3U(db->db_caching_status, ==, DB_NO_CACHE);
2962eda14cbcSMatt Macy 	ASSERT(!multilist_link_active(&db->db_cache_link));
2963eda14cbcSMatt Macy 
2964eda14cbcSMatt Macy 	/*
2965eda14cbcSMatt Macy 	 * If this dbuf is referenced from an indirect dbuf,
2966eda14cbcSMatt Macy 	 * decrement the ref count on the indirect dbuf.
2967eda14cbcSMatt Macy 	 */
2968eda14cbcSMatt Macy 	if (parent && parent != dndb) {
2969eda14cbcSMatt Macy 		mutex_enter(&parent->db_mtx);
2970eda14cbcSMatt Macy 		dbuf_rele_and_unlock(parent, db, B_TRUE);
2971eda14cbcSMatt Macy 	}
2972a0b956f5SMartin Matuska 
2973a0b956f5SMartin Matuska 	kmem_cache_free(dbuf_kmem_cache, db);
2974a0b956f5SMartin Matuska 	arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_DBUF);
2975eda14cbcSMatt Macy }
2976eda14cbcSMatt Macy 
2977eda14cbcSMatt Macy /*
2978eda14cbcSMatt Macy  * Note: While bpp will always be updated if the function returns success,
2979eda14cbcSMatt Macy  * parentp will not be updated if the dnode does not have dn_dbuf filled in;
2980eda14cbcSMatt Macy  * this happens when the dnode is the meta-dnode, or {user|group|project}used
2981eda14cbcSMatt Macy  * object.
2982eda14cbcSMatt Macy  */
2983eda14cbcSMatt Macy __attribute__((always_inline))
2984eda14cbcSMatt Macy static inline int
2985eda14cbcSMatt Macy dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse,
2986eda14cbcSMatt Macy     dmu_buf_impl_t **parentp, blkptr_t **bpp)
2987eda14cbcSMatt Macy {
2988eda14cbcSMatt Macy 	*parentp = NULL;
2989eda14cbcSMatt Macy 	*bpp = NULL;
2990eda14cbcSMatt Macy 
2991eda14cbcSMatt Macy 	ASSERT(blkid != DMU_BONUS_BLKID);
2992eda14cbcSMatt Macy 
2993eda14cbcSMatt Macy 	if (blkid == DMU_SPILL_BLKID) {
2994eda14cbcSMatt Macy 		mutex_enter(&dn->dn_mtx);
2995eda14cbcSMatt Macy 		if (dn->dn_have_spill &&
2996eda14cbcSMatt Macy 		    (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
2997eda14cbcSMatt Macy 			*bpp = DN_SPILL_BLKPTR(dn->dn_phys);
2998eda14cbcSMatt Macy 		else
2999eda14cbcSMatt Macy 			*bpp = NULL;
3000eda14cbcSMatt Macy 		dbuf_add_ref(dn->dn_dbuf, NULL);
3001eda14cbcSMatt Macy 		*parentp = dn->dn_dbuf;
3002eda14cbcSMatt Macy 		mutex_exit(&dn->dn_mtx);
3003eda14cbcSMatt Macy 		return (0);
3004eda14cbcSMatt Macy 	}
3005eda14cbcSMatt Macy 
3006eda14cbcSMatt Macy 	int nlevels =
3007eda14cbcSMatt Macy 	    (dn->dn_phys->dn_nlevels == 0) ? 1 : dn->dn_phys->dn_nlevels;
3008eda14cbcSMatt Macy 	int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
3009eda14cbcSMatt Macy 
3010eda14cbcSMatt Macy 	ASSERT3U(level * epbs, <, 64);
3011eda14cbcSMatt Macy 	ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
3012eda14cbcSMatt Macy 	/*
3013eda14cbcSMatt Macy 	 * This assertion shouldn't trip as long as the max indirect block size
3014eda14cbcSMatt Macy 	 * is less than 1M.  The reason for this is that up to that point,
3015eda14cbcSMatt Macy 	 * the number of levels required to address an entire object with blocks
3016eda14cbcSMatt Macy 	 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64.	 In
3017eda14cbcSMatt Macy 	 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55
3018eda14cbcSMatt Macy 	 * (i.e. we can address the entire object), objects will all use at most
3019eda14cbcSMatt Macy 	 * N-1 levels and the assertion won't overflow.	 However, once epbs is
3020eda14cbcSMatt Macy 	 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66.  Then, 4 levels will not be
3021eda14cbcSMatt Macy 	 * enough to address an entire object, so objects will have 5 levels,
3022eda14cbcSMatt Macy 	 * but then this assertion will overflow.
3023eda14cbcSMatt Macy 	 *
3024eda14cbcSMatt Macy 	 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we
3025eda14cbcSMatt Macy 	 * need to redo this logic to handle overflows.
3026eda14cbcSMatt Macy 	 */
3027eda14cbcSMatt Macy 	ASSERT(level >= nlevels ||
3028eda14cbcSMatt Macy 	    ((nlevels - level - 1) * epbs) +
3029eda14cbcSMatt Macy 	    highbit64(dn->dn_phys->dn_nblkptr) <= 64);
3030eda14cbcSMatt Macy 	if (level >= nlevels ||
3031eda14cbcSMatt Macy 	    blkid >= ((uint64_t)dn->dn_phys->dn_nblkptr <<
3032eda14cbcSMatt Macy 	    ((nlevels - level - 1) * epbs)) ||
3033eda14cbcSMatt Macy 	    (fail_sparse &&
3034eda14cbcSMatt Macy 	    blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) {
3035eda14cbcSMatt Macy 		/* the buffer has no parent yet */
3036eda14cbcSMatt Macy 		return (SET_ERROR(ENOENT));
3037eda14cbcSMatt Macy 	} else if (level < nlevels-1) {
3038eda14cbcSMatt Macy 		/* this block is referenced from an indirect block */
3039eda14cbcSMatt Macy 		int err;
3040eda14cbcSMatt Macy 
3041eda14cbcSMatt Macy 		err = dbuf_hold_impl(dn, level + 1,
3042eda14cbcSMatt Macy 		    blkid >> epbs, fail_sparse, FALSE, NULL, parentp);
3043eda14cbcSMatt Macy 
3044eda14cbcSMatt Macy 		if (err)
3045eda14cbcSMatt Macy 			return (err);
3046eda14cbcSMatt Macy 		err = dbuf_read(*parentp, NULL,
3047eda14cbcSMatt Macy 		    (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL));
3048eda14cbcSMatt Macy 		if (err) {
3049eda14cbcSMatt Macy 			dbuf_rele(*parentp, NULL);
3050eda14cbcSMatt Macy 			*parentp = NULL;
3051eda14cbcSMatt Macy 			return (err);
3052eda14cbcSMatt Macy 		}
3053eda14cbcSMatt Macy 		rw_enter(&(*parentp)->db_rwlock, RW_READER);
3054eda14cbcSMatt Macy 		*bpp = ((blkptr_t *)(*parentp)->db.db_data) +
3055eda14cbcSMatt Macy 		    (blkid & ((1ULL << epbs) - 1));
3056eda14cbcSMatt Macy 		if (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))
3057eda14cbcSMatt Macy 			ASSERT(BP_IS_HOLE(*bpp));
3058eda14cbcSMatt Macy 		rw_exit(&(*parentp)->db_rwlock);
3059eda14cbcSMatt Macy 		return (0);
3060eda14cbcSMatt Macy 	} else {
3061eda14cbcSMatt Macy 		/* the block is referenced from the dnode */
3062eda14cbcSMatt Macy 		ASSERT3U(level, ==, nlevels-1);
3063eda14cbcSMatt Macy 		ASSERT(dn->dn_phys->dn_nblkptr == 0 ||
3064eda14cbcSMatt Macy 		    blkid < dn->dn_phys->dn_nblkptr);
3065eda14cbcSMatt Macy 		if (dn->dn_dbuf) {
3066eda14cbcSMatt Macy 			dbuf_add_ref(dn->dn_dbuf, NULL);
3067eda14cbcSMatt Macy 			*parentp = dn->dn_dbuf;
3068eda14cbcSMatt Macy 		}
3069eda14cbcSMatt Macy 		*bpp = &dn->dn_phys->dn_blkptr[blkid];
3070eda14cbcSMatt Macy 		return (0);
3071eda14cbcSMatt Macy 	}
3072eda14cbcSMatt Macy }
3073eda14cbcSMatt Macy 
3074eda14cbcSMatt Macy static dmu_buf_impl_t *
3075eda14cbcSMatt Macy dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid,
3076eda14cbcSMatt Macy     dmu_buf_impl_t *parent, blkptr_t *blkptr)
3077eda14cbcSMatt Macy {
3078eda14cbcSMatt Macy 	objset_t *os = dn->dn_objset;
3079eda14cbcSMatt Macy 	dmu_buf_impl_t *db, *odb;
3080eda14cbcSMatt Macy 
3081eda14cbcSMatt Macy 	ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
3082eda14cbcSMatt Macy 	ASSERT(dn->dn_type != DMU_OT_NONE);
3083eda14cbcSMatt Macy 
3084eda14cbcSMatt Macy 	db = kmem_cache_alloc(dbuf_kmem_cache, KM_SLEEP);
3085eda14cbcSMatt Macy 
3086eda14cbcSMatt Macy 	list_create(&db->db_dirty_records, sizeof (dbuf_dirty_record_t),
3087eda14cbcSMatt Macy 	    offsetof(dbuf_dirty_record_t, dr_dbuf_node));
3088eda14cbcSMatt Macy 
3089eda14cbcSMatt Macy 	db->db_objset = os;
3090eda14cbcSMatt Macy 	db->db.db_object = dn->dn_object;
3091eda14cbcSMatt Macy 	db->db_level = level;
3092eda14cbcSMatt Macy 	db->db_blkid = blkid;
3093eda14cbcSMatt Macy 	db->db_dirtycnt = 0;
3094eda14cbcSMatt Macy 	db->db_dnode_handle = dn->dn_handle;
3095eda14cbcSMatt Macy 	db->db_parent = parent;
3096eda14cbcSMatt Macy 	db->db_blkptr = blkptr;
3097eda14cbcSMatt Macy 
3098eda14cbcSMatt Macy 	db->db_user = NULL;
3099eda14cbcSMatt Macy 	db->db_user_immediate_evict = FALSE;
3100eda14cbcSMatt Macy 	db->db_freed_in_flight = FALSE;
3101eda14cbcSMatt Macy 	db->db_pending_evict = FALSE;
3102eda14cbcSMatt Macy 
3103eda14cbcSMatt Macy 	if (blkid == DMU_BONUS_BLKID) {
3104eda14cbcSMatt Macy 		ASSERT3P(parent, ==, dn->dn_dbuf);
3105eda14cbcSMatt Macy 		db->db.db_size = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots) -
3106eda14cbcSMatt Macy 		    (dn->dn_nblkptr-1) * sizeof (blkptr_t);
3107eda14cbcSMatt Macy 		ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
3108eda14cbcSMatt Macy 		db->db.db_offset = DMU_BONUS_BLKID;
3109eda14cbcSMatt Macy 		db->db_state = DB_UNCACHED;
3110eda14cbcSMatt Macy 		DTRACE_SET_STATE(db, "bonus buffer created");
3111eda14cbcSMatt Macy 		db->db_caching_status = DB_NO_CACHE;
3112eda14cbcSMatt Macy 		/* the bonus dbuf is not placed in the hash table */
3113eda14cbcSMatt Macy 		arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_DBUF);
3114eda14cbcSMatt Macy 		return (db);
3115eda14cbcSMatt Macy 	} else if (blkid == DMU_SPILL_BLKID) {
3116eda14cbcSMatt Macy 		db->db.db_size = (blkptr != NULL) ?
3117eda14cbcSMatt Macy 		    BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE;
3118eda14cbcSMatt Macy 		db->db.db_offset = 0;
3119eda14cbcSMatt Macy 	} else {
3120eda14cbcSMatt Macy 		int blocksize =
3121eda14cbcSMatt Macy 		    db->db_level ? 1 << dn->dn_indblkshift : dn->dn_datablksz;
3122eda14cbcSMatt Macy 		db->db.db_size = blocksize;
3123eda14cbcSMatt Macy 		db->db.db_offset = db->db_blkid * blocksize;
3124eda14cbcSMatt Macy 	}
3125eda14cbcSMatt Macy 
3126eda14cbcSMatt Macy 	/*
3127eda14cbcSMatt Macy 	 * Hold the dn_dbufs_mtx while we get the new dbuf
3128eda14cbcSMatt Macy 	 * in the hash table *and* added to the dbufs list.
3129eda14cbcSMatt Macy 	 * This prevents a possible deadlock with someone
3130eda14cbcSMatt Macy 	 * trying to look up this dbuf before it's added to the
3131eda14cbcSMatt Macy 	 * dn_dbufs list.
3132eda14cbcSMatt Macy 	 */
3133eda14cbcSMatt Macy 	mutex_enter(&dn->dn_dbufs_mtx);
3134eda14cbcSMatt Macy 	db->db_state = DB_EVICTING; /* not worth logging this state change */
3135eda14cbcSMatt Macy 	if ((odb = dbuf_hash_insert(db)) != NULL) {
3136eda14cbcSMatt Macy 		/* someone else inserted it first */
3137eda14cbcSMatt Macy 		mutex_exit(&dn->dn_dbufs_mtx);
31387cd22ac4SMartin Matuska 		kmem_cache_free(dbuf_kmem_cache, db);
3139eda14cbcSMatt Macy 		DBUF_STAT_BUMP(hash_insert_race);
3140eda14cbcSMatt Macy 		return (odb);
3141eda14cbcSMatt Macy 	}
3142eda14cbcSMatt Macy 	avl_add(&dn->dn_dbufs, db);
3143eda14cbcSMatt Macy 
3144eda14cbcSMatt Macy 	db->db_state = DB_UNCACHED;
3145eda14cbcSMatt Macy 	DTRACE_SET_STATE(db, "regular buffer created");
3146eda14cbcSMatt Macy 	db->db_caching_status = DB_NO_CACHE;
3147eda14cbcSMatt Macy 	mutex_exit(&dn->dn_dbufs_mtx);
3148eda14cbcSMatt Macy 	arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_DBUF);
3149eda14cbcSMatt Macy 
3150eda14cbcSMatt Macy 	if (parent && parent != dn->dn_dbuf)
3151eda14cbcSMatt Macy 		dbuf_add_ref(parent, db);
3152eda14cbcSMatt Macy 
3153eda14cbcSMatt Macy 	ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
3154eda14cbcSMatt Macy 	    zfs_refcount_count(&dn->dn_holds) > 0);
3155eda14cbcSMatt Macy 	(void) zfs_refcount_add(&dn->dn_holds, db);
3156eda14cbcSMatt Macy 
3157eda14cbcSMatt Macy 	dprintf_dbuf(db, "db=%p\n", db);
3158eda14cbcSMatt Macy 
3159eda14cbcSMatt Macy 	return (db);
3160eda14cbcSMatt Macy }
3161eda14cbcSMatt Macy 
3162eda14cbcSMatt Macy /*
3163eda14cbcSMatt Macy  * This function returns a block pointer and information about the object,
3164eda14cbcSMatt Macy  * given a dnode and a block.  This is a publicly accessible version of
3165eda14cbcSMatt Macy  * dbuf_findbp that only returns some information, rather than the
3166eda14cbcSMatt Macy  * dbuf.  Note that the dnode passed in must be held, and the dn_struct_rwlock
3167eda14cbcSMatt Macy  * should be locked as (at least) a reader.
3168eda14cbcSMatt Macy  */
3169eda14cbcSMatt Macy int
3170eda14cbcSMatt Macy dbuf_dnode_findbp(dnode_t *dn, uint64_t level, uint64_t blkid,
3171eda14cbcSMatt Macy     blkptr_t *bp, uint16_t *datablkszsec, uint8_t *indblkshift)
3172eda14cbcSMatt Macy {
3173eda14cbcSMatt Macy 	dmu_buf_impl_t *dbp = NULL;
3174eda14cbcSMatt Macy 	blkptr_t *bp2;
3175eda14cbcSMatt Macy 	int err = 0;
3176eda14cbcSMatt Macy 	ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
3177eda14cbcSMatt Macy 
3178eda14cbcSMatt Macy 	err = dbuf_findbp(dn, level, blkid, B_FALSE, &dbp, &bp2);
3179eda14cbcSMatt Macy 	if (err == 0) {
3180eda14cbcSMatt Macy 		*bp = *bp2;
3181eda14cbcSMatt Macy 		if (dbp != NULL)
3182eda14cbcSMatt Macy 			dbuf_rele(dbp, NULL);
3183eda14cbcSMatt Macy 		if (datablkszsec != NULL)
3184eda14cbcSMatt Macy 			*datablkszsec = dn->dn_phys->dn_datablkszsec;
3185eda14cbcSMatt Macy 		if (indblkshift != NULL)
3186eda14cbcSMatt Macy 			*indblkshift = dn->dn_phys->dn_indblkshift;
3187eda14cbcSMatt Macy 	}
3188eda14cbcSMatt Macy 
3189eda14cbcSMatt Macy 	return (err);
3190eda14cbcSMatt Macy }
3191eda14cbcSMatt Macy 
3192eda14cbcSMatt Macy typedef struct dbuf_prefetch_arg {
3193eda14cbcSMatt Macy 	spa_t *dpa_spa;	/* The spa to issue the prefetch in. */
3194eda14cbcSMatt Macy 	zbookmark_phys_t dpa_zb; /* The target block to prefetch. */
3195eda14cbcSMatt Macy 	int dpa_epbs; /* Entries (blkptr_t's) Per Block Shift. */
3196eda14cbcSMatt Macy 	int dpa_curlevel; /* The current level that we're reading */
3197eda14cbcSMatt Macy 	dnode_t *dpa_dnode; /* The dnode associated with the prefetch */
3198eda14cbcSMatt Macy 	zio_priority_t dpa_prio; /* The priority I/Os should be issued at. */
3199eda14cbcSMatt Macy 	zio_t *dpa_zio; /* The parent zio_t for all prefetches. */
3200eda14cbcSMatt Macy 	arc_flags_t dpa_aflags; /* Flags to pass to the final prefetch. */
32017877fdebSMatt Macy 	dbuf_prefetch_fn dpa_cb; /* prefetch completion callback */
32027877fdebSMatt Macy 	void *dpa_arg; /* prefetch completion arg */
3203eda14cbcSMatt Macy } dbuf_prefetch_arg_t;
3204eda14cbcSMatt Macy 
32057877fdebSMatt Macy static void
32067877fdebSMatt Macy dbuf_prefetch_fini(dbuf_prefetch_arg_t *dpa, boolean_t io_done)
32077877fdebSMatt Macy {
3208e3aa18adSMartin Matuska 	if (dpa->dpa_cb != NULL) {
3209e3aa18adSMartin Matuska 		dpa->dpa_cb(dpa->dpa_arg, dpa->dpa_zb.zb_level,
3210e3aa18adSMartin Matuska 		    dpa->dpa_zb.zb_blkid, io_done);
3211e3aa18adSMartin Matuska 	}
32127877fdebSMatt Macy 	kmem_free(dpa, sizeof (*dpa));
32137877fdebSMatt Macy }
32147877fdebSMatt Macy 
32157877fdebSMatt Macy static void
32167877fdebSMatt Macy dbuf_issue_final_prefetch_done(zio_t *zio, const zbookmark_phys_t *zb,
32177877fdebSMatt Macy     const blkptr_t *iobp, arc_buf_t *abuf, void *private)
32187877fdebSMatt Macy {
3219e92ffd9bSMartin Matuska 	(void) zio, (void) zb, (void) iobp;
32207877fdebSMatt Macy 	dbuf_prefetch_arg_t *dpa = private;
32217877fdebSMatt Macy 
32227877fdebSMatt Macy 	if (abuf != NULL)
32237877fdebSMatt Macy 		arc_buf_destroy(abuf, private);
3224a0b956f5SMartin Matuska 
3225a0b956f5SMartin Matuska 	dbuf_prefetch_fini(dpa, B_TRUE);
32267877fdebSMatt Macy }
32277877fdebSMatt Macy 
3228eda14cbcSMatt Macy /*
3229eda14cbcSMatt Macy  * Actually issue the prefetch read for the block given.
3230eda14cbcSMatt Macy  */
3231eda14cbcSMatt Macy static void
3232eda14cbcSMatt Macy dbuf_issue_final_prefetch(dbuf_prefetch_arg_t *dpa, blkptr_t *bp)
3233eda14cbcSMatt Macy {
3234eda14cbcSMatt Macy 	ASSERT(!BP_IS_REDACTED(bp) ||
3235eda14cbcSMatt Macy 	    dsl_dataset_feature_is_active(
3236eda14cbcSMatt Macy 	    dpa->dpa_dnode->dn_objset->os_dsl_dataset,
3237eda14cbcSMatt Macy 	    SPA_FEATURE_REDACTED_DATASETS));
3238eda14cbcSMatt Macy 
3239eda14cbcSMatt Macy 	if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp) || BP_IS_REDACTED(bp))
32407877fdebSMatt Macy 		return (dbuf_prefetch_fini(dpa, B_FALSE));
3241eda14cbcSMatt Macy 
3242eda14cbcSMatt Macy 	int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE;
3243eda14cbcSMatt Macy 	arc_flags_t aflags =
32447877fdebSMatt Macy 	    dpa->dpa_aflags | ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH |
32457877fdebSMatt Macy 	    ARC_FLAG_NO_BUF;
3246eda14cbcSMatt Macy 
3247eda14cbcSMatt Macy 	/* dnodes are always read as raw and then converted later */
3248eda14cbcSMatt Macy 	if (BP_GET_TYPE(bp) == DMU_OT_DNODE && BP_IS_PROTECTED(bp) &&
3249eda14cbcSMatt Macy 	    dpa->dpa_curlevel == 0)
3250eda14cbcSMatt Macy 		zio_flags |= ZIO_FLAG_RAW;
3251eda14cbcSMatt Macy 
3252eda14cbcSMatt Macy 	ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp));
3253eda14cbcSMatt Macy 	ASSERT3U(dpa->dpa_curlevel, ==, dpa->dpa_zb.zb_level);
3254eda14cbcSMatt Macy 	ASSERT(dpa->dpa_zio != NULL);
32557877fdebSMatt Macy 	(void) arc_read(dpa->dpa_zio, dpa->dpa_spa, bp,
32567877fdebSMatt Macy 	    dbuf_issue_final_prefetch_done, dpa,
3257eda14cbcSMatt Macy 	    dpa->dpa_prio, zio_flags, &aflags, &dpa->dpa_zb);
3258eda14cbcSMatt Macy }
3259eda14cbcSMatt Macy 
3260eda14cbcSMatt Macy /*
3261eda14cbcSMatt Macy  * Called when an indirect block above our prefetch target is read in.  This
3262eda14cbcSMatt Macy  * will either read in the next indirect block down the tree or issue the actual
3263eda14cbcSMatt Macy  * prefetch if the next block down is our target.
3264eda14cbcSMatt Macy  */
3265eda14cbcSMatt Macy static void
3266eda14cbcSMatt Macy dbuf_prefetch_indirect_done(zio_t *zio, const zbookmark_phys_t *zb,
3267eda14cbcSMatt Macy     const blkptr_t *iobp, arc_buf_t *abuf, void *private)
3268eda14cbcSMatt Macy {
3269e92ffd9bSMartin Matuska 	(void) zb, (void) iobp;
3270eda14cbcSMatt Macy 	dbuf_prefetch_arg_t *dpa = private;
3271eda14cbcSMatt Macy 
3272eda14cbcSMatt Macy 	ASSERT3S(dpa->dpa_zb.zb_level, <, dpa->dpa_curlevel);
3273eda14cbcSMatt Macy 	ASSERT3S(dpa->dpa_curlevel, >, 0);
3274eda14cbcSMatt Macy 
3275eda14cbcSMatt Macy 	if (abuf == NULL) {
3276eda14cbcSMatt Macy 		ASSERT(zio == NULL || zio->io_error != 0);
3277c7046f76SMartin Matuska 		dbuf_prefetch_fini(dpa, B_TRUE);
3278c7046f76SMartin Matuska 		return;
3279eda14cbcSMatt Macy 	}
3280eda14cbcSMatt Macy 	ASSERT(zio == NULL || zio->io_error == 0);
3281eda14cbcSMatt Macy 
3282eda14cbcSMatt Macy 	/*
3283eda14cbcSMatt Macy 	 * The dpa_dnode is only valid if we are called with a NULL
3284eda14cbcSMatt Macy 	 * zio. This indicates that the arc_read() returned without
3285eda14cbcSMatt Macy 	 * first calling zio_read() to issue a physical read. Once
3286eda14cbcSMatt Macy 	 * a physical read is made the dpa_dnode must be invalidated
3287eda14cbcSMatt Macy 	 * as the locks guarding it may have been dropped. If the
3288eda14cbcSMatt Macy 	 * dpa_dnode is still valid, then we want to add it to the dbuf
3289eda14cbcSMatt Macy 	 * cache. To do so, we must hold the dbuf associated with the block
3290eda14cbcSMatt Macy 	 * we just prefetched, read its contents so that we associate it
3291eda14cbcSMatt Macy 	 * with an arc_buf_t, and then release it.
3292eda14cbcSMatt Macy 	 */
3293eda14cbcSMatt Macy 	if (zio != NULL) {
3294eda14cbcSMatt Macy 		ASSERT3S(BP_GET_LEVEL(zio->io_bp), ==, dpa->dpa_curlevel);
3295eda14cbcSMatt Macy 		if (zio->io_flags & ZIO_FLAG_RAW_COMPRESS) {
3296eda14cbcSMatt Macy 			ASSERT3U(BP_GET_PSIZE(zio->io_bp), ==, zio->io_size);
3297eda14cbcSMatt Macy 		} else {
3298eda14cbcSMatt Macy 			ASSERT3U(BP_GET_LSIZE(zio->io_bp), ==, zio->io_size);
3299eda14cbcSMatt Macy 		}
3300eda14cbcSMatt Macy 		ASSERT3P(zio->io_spa, ==, dpa->dpa_spa);
3301eda14cbcSMatt Macy 
3302eda14cbcSMatt Macy 		dpa->dpa_dnode = NULL;
3303eda14cbcSMatt Macy 	} else if (dpa->dpa_dnode != NULL) {
3304eda14cbcSMatt Macy 		uint64_t curblkid = dpa->dpa_zb.zb_blkid >>
3305eda14cbcSMatt Macy 		    (dpa->dpa_epbs * (dpa->dpa_curlevel -
3306eda14cbcSMatt Macy 		    dpa->dpa_zb.zb_level));
3307eda14cbcSMatt Macy 		dmu_buf_impl_t *db = dbuf_hold_level(dpa->dpa_dnode,
3308eda14cbcSMatt Macy 		    dpa->dpa_curlevel, curblkid, FTAG);
3309eda14cbcSMatt Macy 		if (db == NULL) {
3310eda14cbcSMatt Macy 			arc_buf_destroy(abuf, private);
3311c7046f76SMartin Matuska 			dbuf_prefetch_fini(dpa, B_TRUE);
3312c7046f76SMartin Matuska 			return;
3313eda14cbcSMatt Macy 		}
3314eda14cbcSMatt Macy 		(void) dbuf_read(db, NULL,
3315eda14cbcSMatt Macy 		    DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH | DB_RF_HAVESTRUCT);
3316eda14cbcSMatt Macy 		dbuf_rele(db, FTAG);
3317eda14cbcSMatt Macy 	}
3318eda14cbcSMatt Macy 
3319eda14cbcSMatt Macy 	dpa->dpa_curlevel--;
3320eda14cbcSMatt Macy 	uint64_t nextblkid = dpa->dpa_zb.zb_blkid >>
3321eda14cbcSMatt Macy 	    (dpa->dpa_epbs * (dpa->dpa_curlevel - dpa->dpa_zb.zb_level));
3322eda14cbcSMatt Macy 	blkptr_t *bp = ((blkptr_t *)abuf->b_data) +
3323eda14cbcSMatt Macy 	    P2PHASE(nextblkid, 1ULL << dpa->dpa_epbs);
3324eda14cbcSMatt Macy 
3325*bb2d13b6SMartin Matuska 	ASSERT(!BP_IS_REDACTED(bp) || (dpa->dpa_dnode &&
3326eda14cbcSMatt Macy 	    dsl_dataset_feature_is_active(
3327eda14cbcSMatt Macy 	    dpa->dpa_dnode->dn_objset->os_dsl_dataset,
3328*bb2d13b6SMartin Matuska 	    SPA_FEATURE_REDACTED_DATASETS)));
3329eda14cbcSMatt Macy 	if (BP_IS_HOLE(bp) || BP_IS_REDACTED(bp)) {
3330c7046f76SMartin Matuska 		arc_buf_destroy(abuf, private);
33317877fdebSMatt Macy 		dbuf_prefetch_fini(dpa, B_TRUE);
3332c7046f76SMartin Matuska 		return;
3333eda14cbcSMatt Macy 	} else if (dpa->dpa_curlevel == dpa->dpa_zb.zb_level) {
3334eda14cbcSMatt Macy 		ASSERT3U(nextblkid, ==, dpa->dpa_zb.zb_blkid);
3335eda14cbcSMatt Macy 		dbuf_issue_final_prefetch(dpa, bp);
3336eda14cbcSMatt Macy 	} else {
3337eda14cbcSMatt Macy 		arc_flags_t iter_aflags = ARC_FLAG_NOWAIT;
3338eda14cbcSMatt Macy 		zbookmark_phys_t zb;
3339eda14cbcSMatt Macy 
3340eda14cbcSMatt Macy 		/* flag if L2ARC eligible, l2arc_noprefetch then decides */
3341eda14cbcSMatt Macy 		if (dpa->dpa_aflags & ARC_FLAG_L2CACHE)
3342eda14cbcSMatt Macy 			iter_aflags |= ARC_FLAG_L2CACHE;
3343eda14cbcSMatt Macy 
3344eda14cbcSMatt Macy 		ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp));
3345eda14cbcSMatt Macy 
3346eda14cbcSMatt Macy 		SET_BOOKMARK(&zb, dpa->dpa_zb.zb_objset,
3347eda14cbcSMatt Macy 		    dpa->dpa_zb.zb_object, dpa->dpa_curlevel, nextblkid);
3348eda14cbcSMatt Macy 
3349eda14cbcSMatt Macy 		(void) arc_read(dpa->dpa_zio, dpa->dpa_spa,
3350e3aa18adSMartin Matuska 		    bp, dbuf_prefetch_indirect_done, dpa,
3351e3aa18adSMartin Matuska 		    ZIO_PRIORITY_SYNC_READ,
3352eda14cbcSMatt Macy 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
3353eda14cbcSMatt Macy 		    &iter_aflags, &zb);
3354eda14cbcSMatt Macy 	}
3355eda14cbcSMatt Macy 
3356eda14cbcSMatt Macy 	arc_buf_destroy(abuf, private);
3357eda14cbcSMatt Macy }
3358eda14cbcSMatt Macy 
3359eda14cbcSMatt Macy /*
3360eda14cbcSMatt Macy  * Issue prefetch reads for the given block on the given level.  If the indirect
3361eda14cbcSMatt Macy  * blocks above that block are not in memory, we will read them in
3362eda14cbcSMatt Macy  * asynchronously.  As a result, this call never blocks waiting for a read to
3363eda14cbcSMatt Macy  * complete. Note that the prefetch might fail if the dataset is encrypted and
3364eda14cbcSMatt Macy  * the encryption key is unmapped before the IO completes.
3365eda14cbcSMatt Macy  */
33667877fdebSMatt Macy int
33677877fdebSMatt Macy dbuf_prefetch_impl(dnode_t *dn, int64_t level, uint64_t blkid,
33687877fdebSMatt Macy     zio_priority_t prio, arc_flags_t aflags, dbuf_prefetch_fn cb,
33697877fdebSMatt Macy     void *arg)
3370eda14cbcSMatt Macy {
3371eda14cbcSMatt Macy 	blkptr_t bp;
3372eda14cbcSMatt Macy 	int epbs, nlevels, curlevel;
3373eda14cbcSMatt Macy 	uint64_t curblkid;
3374eda14cbcSMatt Macy 
3375eda14cbcSMatt Macy 	ASSERT(blkid != DMU_BONUS_BLKID);
3376eda14cbcSMatt Macy 	ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
3377eda14cbcSMatt Macy 
3378eda14cbcSMatt Macy 	if (blkid > dn->dn_maxblkid)
33797877fdebSMatt Macy 		goto no_issue;
3380eda14cbcSMatt Macy 
3381eda14cbcSMatt Macy 	if (level == 0 && dnode_block_freed(dn, blkid))
33827877fdebSMatt Macy 		goto no_issue;
3383eda14cbcSMatt Macy 
3384eda14cbcSMatt Macy 	/*
3385eda14cbcSMatt Macy 	 * This dnode hasn't been written to disk yet, so there's nothing to
3386eda14cbcSMatt Macy 	 * prefetch.
3387eda14cbcSMatt Macy 	 */
3388eda14cbcSMatt Macy 	nlevels = dn->dn_phys->dn_nlevels;
3389eda14cbcSMatt Macy 	if (level >= nlevels || dn->dn_phys->dn_nblkptr == 0)
33907877fdebSMatt Macy 		goto no_issue;
3391eda14cbcSMatt Macy 
3392eda14cbcSMatt Macy 	epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
3393eda14cbcSMatt Macy 	if (dn->dn_phys->dn_maxblkid < blkid << (epbs * level))
33947877fdebSMatt Macy 		goto no_issue;
3395eda14cbcSMatt Macy 
3396eda14cbcSMatt Macy 	dmu_buf_impl_t *db = dbuf_find(dn->dn_objset, dn->dn_object,
3397eda14cbcSMatt Macy 	    level, blkid);
3398eda14cbcSMatt Macy 	if (db != NULL) {
3399eda14cbcSMatt Macy 		mutex_exit(&db->db_mtx);
3400eda14cbcSMatt Macy 		/*
3401eda14cbcSMatt Macy 		 * This dbuf already exists.  It is either CACHED, or
3402eda14cbcSMatt Macy 		 * (we assume) about to be read or filled.
3403eda14cbcSMatt Macy 		 */
34047877fdebSMatt Macy 		goto no_issue;
3405eda14cbcSMatt Macy 	}
3406eda14cbcSMatt Macy 
3407eda14cbcSMatt Macy 	/*
3408eda14cbcSMatt Macy 	 * Find the closest ancestor (indirect block) of the target block
3409eda14cbcSMatt Macy 	 * that is present in the cache.  In this indirect block, we will
3410eda14cbcSMatt Macy 	 * find the bp that is at curlevel, curblkid.
3411eda14cbcSMatt Macy 	 */
3412eda14cbcSMatt Macy 	curlevel = level;
3413eda14cbcSMatt Macy 	curblkid = blkid;
3414eda14cbcSMatt Macy 	while (curlevel < nlevels - 1) {
3415eda14cbcSMatt Macy 		int parent_level = curlevel + 1;
3416eda14cbcSMatt Macy 		uint64_t parent_blkid = curblkid >> epbs;
3417eda14cbcSMatt Macy 		dmu_buf_impl_t *db;
3418eda14cbcSMatt Macy 
3419eda14cbcSMatt Macy 		if (dbuf_hold_impl(dn, parent_level, parent_blkid,
3420eda14cbcSMatt Macy 		    FALSE, TRUE, FTAG, &db) == 0) {
3421eda14cbcSMatt Macy 			blkptr_t *bpp = db->db_buf->b_data;
3422eda14cbcSMatt Macy 			bp = bpp[P2PHASE(curblkid, 1 << epbs)];
3423eda14cbcSMatt Macy 			dbuf_rele(db, FTAG);
3424eda14cbcSMatt Macy 			break;
3425eda14cbcSMatt Macy 		}
3426eda14cbcSMatt Macy 
3427eda14cbcSMatt Macy 		curlevel = parent_level;
3428eda14cbcSMatt Macy 		curblkid = parent_blkid;
3429eda14cbcSMatt Macy 	}
3430eda14cbcSMatt Macy 
3431eda14cbcSMatt Macy 	if (curlevel == nlevels - 1) {
3432eda14cbcSMatt Macy 		/* No cached indirect blocks found. */
3433eda14cbcSMatt Macy 		ASSERT3U(curblkid, <, dn->dn_phys->dn_nblkptr);
3434eda14cbcSMatt Macy 		bp = dn->dn_phys->dn_blkptr[curblkid];
3435eda14cbcSMatt Macy 	}
3436eda14cbcSMatt Macy 	ASSERT(!BP_IS_REDACTED(&bp) ||
3437eda14cbcSMatt Macy 	    dsl_dataset_feature_is_active(dn->dn_objset->os_dsl_dataset,
3438eda14cbcSMatt Macy 	    SPA_FEATURE_REDACTED_DATASETS));
3439eda14cbcSMatt Macy 	if (BP_IS_HOLE(&bp) || BP_IS_REDACTED(&bp))
34407877fdebSMatt Macy 		goto no_issue;
3441eda14cbcSMatt Macy 
3442eda14cbcSMatt Macy 	ASSERT3U(curlevel, ==, BP_GET_LEVEL(&bp));
3443eda14cbcSMatt Macy 
3444eda14cbcSMatt Macy 	zio_t *pio = zio_root(dmu_objset_spa(dn->dn_objset), NULL, NULL,
3445eda14cbcSMatt Macy 	    ZIO_FLAG_CANFAIL);
3446eda14cbcSMatt Macy 
3447eda14cbcSMatt Macy 	dbuf_prefetch_arg_t *dpa = kmem_zalloc(sizeof (*dpa), KM_SLEEP);
3448eda14cbcSMatt Macy 	dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
3449eda14cbcSMatt Macy 	SET_BOOKMARK(&dpa->dpa_zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET,
3450eda14cbcSMatt Macy 	    dn->dn_object, level, blkid);
3451eda14cbcSMatt Macy 	dpa->dpa_curlevel = curlevel;
3452eda14cbcSMatt Macy 	dpa->dpa_prio = prio;
3453eda14cbcSMatt Macy 	dpa->dpa_aflags = aflags;
3454eda14cbcSMatt Macy 	dpa->dpa_spa = dn->dn_objset->os_spa;
3455eda14cbcSMatt Macy 	dpa->dpa_dnode = dn;
3456eda14cbcSMatt Macy 	dpa->dpa_epbs = epbs;
3457eda14cbcSMatt Macy 	dpa->dpa_zio = pio;
34587877fdebSMatt Macy 	dpa->dpa_cb = cb;
34597877fdebSMatt Macy 	dpa->dpa_arg = arg;
3460eda14cbcSMatt Macy 
3461eda14cbcSMatt Macy 	/* flag if L2ARC eligible, l2arc_noprefetch then decides */
3462dae17134SMartin Matuska 	if (dnode_level_is_l2cacheable(&bp, dn, level))
3463eda14cbcSMatt Macy 		dpa->dpa_aflags |= ARC_FLAG_L2CACHE;
3464eda14cbcSMatt Macy 
3465eda14cbcSMatt Macy 	/*
3466eda14cbcSMatt Macy 	 * If we have the indirect just above us, no need to do the asynchronous
3467eda14cbcSMatt Macy 	 * prefetch chain; we'll just run the last step ourselves.  If we're at
3468eda14cbcSMatt Macy 	 * a higher level, though, we want to issue the prefetches for all the
3469eda14cbcSMatt Macy 	 * indirect blocks asynchronously, so we can go on with whatever we were
3470eda14cbcSMatt Macy 	 * doing.
3471eda14cbcSMatt Macy 	 */
3472eda14cbcSMatt Macy 	if (curlevel == level) {
3473eda14cbcSMatt Macy 		ASSERT3U(curblkid, ==, blkid);
3474eda14cbcSMatt Macy 		dbuf_issue_final_prefetch(dpa, &bp);
3475eda14cbcSMatt Macy 	} else {
3476eda14cbcSMatt Macy 		arc_flags_t iter_aflags = ARC_FLAG_NOWAIT;
3477eda14cbcSMatt Macy 		zbookmark_phys_t zb;
3478eda14cbcSMatt Macy 
3479eda14cbcSMatt Macy 		/* flag if L2ARC eligible, l2arc_noprefetch then decides */
3480dae17134SMartin Matuska 		if (dnode_level_is_l2cacheable(&bp, dn, level))
3481eda14cbcSMatt Macy 			iter_aflags |= ARC_FLAG_L2CACHE;
3482eda14cbcSMatt Macy 
3483eda14cbcSMatt Macy 		SET_BOOKMARK(&zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET,
3484eda14cbcSMatt Macy 		    dn->dn_object, curlevel, curblkid);
3485eda14cbcSMatt Macy 		(void) arc_read(dpa->dpa_zio, dpa->dpa_spa,
3486e3aa18adSMartin Matuska 		    &bp, dbuf_prefetch_indirect_done, dpa,
3487e3aa18adSMartin Matuska 		    ZIO_PRIORITY_SYNC_READ,
3488eda14cbcSMatt Macy 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
3489eda14cbcSMatt Macy 		    &iter_aflags, &zb);
3490eda14cbcSMatt Macy 	}
3491eda14cbcSMatt Macy 	/*
3492eda14cbcSMatt Macy 	 * We use pio here instead of dpa_zio since it's possible that
3493eda14cbcSMatt Macy 	 * dpa may have already been freed.
3494eda14cbcSMatt Macy 	 */
3495eda14cbcSMatt Macy 	zio_nowait(pio);
34967877fdebSMatt Macy 	return (1);
34977877fdebSMatt Macy no_issue:
34987877fdebSMatt Macy 	if (cb != NULL)
3499e3aa18adSMartin Matuska 		cb(arg, level, blkid, B_FALSE);
35007877fdebSMatt Macy 	return (0);
35017877fdebSMatt Macy }
35027877fdebSMatt Macy 
35037877fdebSMatt Macy int
35047877fdebSMatt Macy dbuf_prefetch(dnode_t *dn, int64_t level, uint64_t blkid, zio_priority_t prio,
35057877fdebSMatt Macy     arc_flags_t aflags)
35067877fdebSMatt Macy {
35077877fdebSMatt Macy 
35087877fdebSMatt Macy 	return (dbuf_prefetch_impl(dn, level, blkid, prio, aflags, NULL, NULL));
3509eda14cbcSMatt Macy }
3510eda14cbcSMatt Macy 
3511eda14cbcSMatt Macy /*
3512eda14cbcSMatt Macy  * Helper function for dbuf_hold_impl() to copy a buffer. Handles
3513eda14cbcSMatt Macy  * the case of encrypted, compressed and uncompressed buffers by
3514eda14cbcSMatt Macy  * allocating the new buffer, respectively, with arc_alloc_raw_buf(),
3515eda14cbcSMatt Macy  * arc_alloc_compressed_buf() or arc_alloc_buf().*
3516eda14cbcSMatt Macy  *
3517eda14cbcSMatt Macy  * NOTE: Declared noinline to avoid stack bloat in dbuf_hold_impl().
3518eda14cbcSMatt Macy  */
3519eda14cbcSMatt Macy noinline static void
3520eda14cbcSMatt Macy dbuf_hold_copy(dnode_t *dn, dmu_buf_impl_t *db)
3521eda14cbcSMatt Macy {
3522eda14cbcSMatt Macy 	dbuf_dirty_record_t *dr = db->db_data_pending;
352333b8c039SMartin Matuska 	arc_buf_t *data = dr->dt.dl.dr_data;
352433b8c039SMartin Matuska 	enum zio_compress compress_type = arc_get_compression(data);
352533b8c039SMartin Matuska 	uint8_t complevel = arc_get_complevel(data);
3526eda14cbcSMatt Macy 
352733b8c039SMartin Matuska 	if (arc_is_encrypted(data)) {
352833b8c039SMartin Matuska 		boolean_t byteorder;
352933b8c039SMartin Matuska 		uint8_t salt[ZIO_DATA_SALT_LEN];
353033b8c039SMartin Matuska 		uint8_t iv[ZIO_DATA_IV_LEN];
353133b8c039SMartin Matuska 		uint8_t mac[ZIO_DATA_MAC_LEN];
353233b8c039SMartin Matuska 
353333b8c039SMartin Matuska 		arc_get_raw_params(data, &byteorder, salt, iv, mac);
353433b8c039SMartin Matuska 		dbuf_set_data(db, arc_alloc_raw_buf(dn->dn_objset->os_spa, db,
353533b8c039SMartin Matuska 		    dmu_objset_id(dn->dn_objset), byteorder, salt, iv, mac,
353633b8c039SMartin Matuska 		    dn->dn_type, arc_buf_size(data), arc_buf_lsize(data),
353733b8c039SMartin Matuska 		    compress_type, complevel));
353833b8c039SMartin Matuska 	} else if (compress_type != ZIO_COMPRESS_OFF) {
353933b8c039SMartin Matuska 		dbuf_set_data(db, arc_alloc_compressed_buf(
354033b8c039SMartin Matuska 		    dn->dn_objset->os_spa, db, arc_buf_size(data),
354133b8c039SMartin Matuska 		    arc_buf_lsize(data), compress_type, complevel));
354233b8c039SMartin Matuska 	} else {
354333b8c039SMartin Matuska 		dbuf_set_data(db, arc_alloc_buf(dn->dn_objset->os_spa, db,
354433b8c039SMartin Matuska 		    DBUF_GET_BUFC_TYPE(db), db->db.db_size));
354533b8c039SMartin Matuska 	}
354633b8c039SMartin Matuska 
3547eda14cbcSMatt Macy 	rw_enter(&db->db_rwlock, RW_WRITER);
3548da5137abSMartin Matuska 	memcpy(db->db.db_data, data->b_data, arc_buf_size(data));
3549eda14cbcSMatt Macy 	rw_exit(&db->db_rwlock);
3550eda14cbcSMatt Macy }
3551eda14cbcSMatt Macy 
3552eda14cbcSMatt Macy /*
3553eda14cbcSMatt Macy  * Returns with db_holds incremented, and db_mtx not held.
3554eda14cbcSMatt Macy  * Note: dn_struct_rwlock must be held.
3555eda14cbcSMatt Macy  */
3556eda14cbcSMatt Macy int
3557eda14cbcSMatt Macy dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid,
3558eda14cbcSMatt Macy     boolean_t fail_sparse, boolean_t fail_uncached,
3559a0b956f5SMartin Matuska     const void *tag, dmu_buf_impl_t **dbp)
3560eda14cbcSMatt Macy {
3561eda14cbcSMatt Macy 	dmu_buf_impl_t *db, *parent = NULL;
3562eda14cbcSMatt Macy 
3563eda14cbcSMatt Macy 	/* If the pool has been created, verify the tx_sync_lock is not held */
3564eda14cbcSMatt Macy 	spa_t *spa = dn->dn_objset->os_spa;
3565eda14cbcSMatt Macy 	dsl_pool_t *dp = spa->spa_dsl_pool;
3566eda14cbcSMatt Macy 	if (dp != NULL) {
3567eda14cbcSMatt Macy 		ASSERT(!MUTEX_HELD(&dp->dp_tx.tx_sync_lock));
3568eda14cbcSMatt Macy 	}
3569eda14cbcSMatt Macy 
3570eda14cbcSMatt Macy 	ASSERT(blkid != DMU_BONUS_BLKID);
3571eda14cbcSMatt Macy 	ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
3572eda14cbcSMatt Macy 	ASSERT3U(dn->dn_nlevels, >, level);
3573eda14cbcSMatt Macy 
3574eda14cbcSMatt Macy 	*dbp = NULL;
3575eda14cbcSMatt Macy 
3576eda14cbcSMatt Macy 	/* dbuf_find() returns with db_mtx held */
3577eda14cbcSMatt Macy 	db = dbuf_find(dn->dn_objset, dn->dn_object, level, blkid);
3578eda14cbcSMatt Macy 
3579eda14cbcSMatt Macy 	if (db == NULL) {
3580eda14cbcSMatt Macy 		blkptr_t *bp = NULL;
3581eda14cbcSMatt Macy 		int err;
3582eda14cbcSMatt Macy 
3583eda14cbcSMatt Macy 		if (fail_uncached)
3584eda14cbcSMatt Macy 			return (SET_ERROR(ENOENT));
3585eda14cbcSMatt Macy 
3586eda14cbcSMatt Macy 		ASSERT3P(parent, ==, NULL);
3587eda14cbcSMatt Macy 		err = dbuf_findbp(dn, level, blkid, fail_sparse, &parent, &bp);
3588eda14cbcSMatt Macy 		if (fail_sparse) {
3589eda14cbcSMatt Macy 			if (err == 0 && bp && BP_IS_HOLE(bp))
3590eda14cbcSMatt Macy 				err = SET_ERROR(ENOENT);
3591eda14cbcSMatt Macy 			if (err) {
3592eda14cbcSMatt Macy 				if (parent)
3593eda14cbcSMatt Macy 					dbuf_rele(parent, NULL);
3594eda14cbcSMatt Macy 				return (err);
3595eda14cbcSMatt Macy 			}
3596eda14cbcSMatt Macy 		}
3597eda14cbcSMatt Macy 		if (err && err != ENOENT)
3598eda14cbcSMatt Macy 			return (err);
3599eda14cbcSMatt Macy 		db = dbuf_create(dn, level, blkid, parent, bp);
3600eda14cbcSMatt Macy 	}
3601eda14cbcSMatt Macy 
3602eda14cbcSMatt Macy 	if (fail_uncached && db->db_state != DB_CACHED) {
3603eda14cbcSMatt Macy 		mutex_exit(&db->db_mtx);
3604eda14cbcSMatt Macy 		return (SET_ERROR(ENOENT));
3605eda14cbcSMatt Macy 	}
3606eda14cbcSMatt Macy 
3607eda14cbcSMatt Macy 	if (db->db_buf != NULL) {
3608eda14cbcSMatt Macy 		arc_buf_access(db->db_buf);
3609eda14cbcSMatt Macy 		ASSERT3P(db->db.db_data, ==, db->db_buf->b_data);
3610eda14cbcSMatt Macy 	}
3611eda14cbcSMatt Macy 
3612eda14cbcSMatt Macy 	ASSERT(db->db_buf == NULL || arc_referenced(db->db_buf));
3613eda14cbcSMatt Macy 
3614eda14cbcSMatt Macy 	/*
3615eda14cbcSMatt Macy 	 * If this buffer is currently syncing out, and we are
3616eda14cbcSMatt Macy 	 * still referencing it from db_data, we need to make a copy
3617eda14cbcSMatt Macy 	 * of it in case we decide we want to dirty it again in this txg.
3618eda14cbcSMatt Macy 	 */
3619eda14cbcSMatt Macy 	if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
3620eda14cbcSMatt Macy 	    dn->dn_object != DMU_META_DNODE_OBJECT &&
3621eda14cbcSMatt Macy 	    db->db_state == DB_CACHED && db->db_data_pending) {
3622eda14cbcSMatt Macy 		dbuf_dirty_record_t *dr = db->db_data_pending;
3623eda14cbcSMatt Macy 		if (dr->dt.dl.dr_data == db->db_buf)
3624eda14cbcSMatt Macy 			dbuf_hold_copy(dn, db);
3625eda14cbcSMatt Macy 	}
3626eda14cbcSMatt Macy 
3627eda14cbcSMatt Macy 	if (multilist_link_active(&db->db_cache_link)) {
3628eda14cbcSMatt Macy 		ASSERT(zfs_refcount_is_zero(&db->db_holds));
3629eda14cbcSMatt Macy 		ASSERT(db->db_caching_status == DB_DBUF_CACHE ||
3630eda14cbcSMatt Macy 		    db->db_caching_status == DB_DBUF_METADATA_CACHE);
3631eda14cbcSMatt Macy 
36323ff01b23SMartin Matuska 		multilist_remove(&dbuf_caches[db->db_caching_status].cache, db);
3633eda14cbcSMatt Macy 		(void) zfs_refcount_remove_many(
3634eda14cbcSMatt Macy 		    &dbuf_caches[db->db_caching_status].size,
3635eda14cbcSMatt Macy 		    db->db.db_size, db);
3636eda14cbcSMatt Macy 
3637eda14cbcSMatt Macy 		if (db->db_caching_status == DB_DBUF_METADATA_CACHE) {
3638eda14cbcSMatt Macy 			DBUF_STAT_BUMPDOWN(metadata_cache_count);
3639eda14cbcSMatt Macy 		} else {
3640eda14cbcSMatt Macy 			DBUF_STAT_BUMPDOWN(cache_levels[db->db_level]);
3641eda14cbcSMatt Macy 			DBUF_STAT_BUMPDOWN(cache_count);
3642eda14cbcSMatt Macy 			DBUF_STAT_DECR(cache_levels_bytes[db->db_level],
3643eda14cbcSMatt Macy 			    db->db.db_size);
3644eda14cbcSMatt Macy 		}
3645eda14cbcSMatt Macy 		db->db_caching_status = DB_NO_CACHE;
3646eda14cbcSMatt Macy 	}
3647eda14cbcSMatt Macy 	(void) zfs_refcount_add(&db->db_holds, tag);
3648eda14cbcSMatt Macy 	DBUF_VERIFY(db);
3649eda14cbcSMatt Macy 	mutex_exit(&db->db_mtx);
3650eda14cbcSMatt Macy 
3651eda14cbcSMatt Macy 	/* NOTE: we can't rele the parent until after we drop the db_mtx */
3652eda14cbcSMatt Macy 	if (parent)
3653eda14cbcSMatt Macy 		dbuf_rele(parent, NULL);
3654eda14cbcSMatt Macy 
3655eda14cbcSMatt Macy 	ASSERT3P(DB_DNODE(db), ==, dn);
3656eda14cbcSMatt Macy 	ASSERT3U(db->db_blkid, ==, blkid);
3657eda14cbcSMatt Macy 	ASSERT3U(db->db_level, ==, level);
3658eda14cbcSMatt Macy 	*dbp = db;
3659eda14cbcSMatt Macy 
3660eda14cbcSMatt Macy 	return (0);
3661eda14cbcSMatt Macy }
3662eda14cbcSMatt Macy 
3663eda14cbcSMatt Macy dmu_buf_impl_t *
3664a0b956f5SMartin Matuska dbuf_hold(dnode_t *dn, uint64_t blkid, const void *tag)
3665eda14cbcSMatt Macy {
3666eda14cbcSMatt Macy 	return (dbuf_hold_level(dn, 0, blkid, tag));
3667eda14cbcSMatt Macy }
3668eda14cbcSMatt Macy 
3669eda14cbcSMatt Macy dmu_buf_impl_t *
3670a0b956f5SMartin Matuska dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, const void *tag)
3671eda14cbcSMatt Macy {
3672eda14cbcSMatt Macy 	dmu_buf_impl_t *db;
3673eda14cbcSMatt Macy 	int err = dbuf_hold_impl(dn, level, blkid, FALSE, FALSE, tag, &db);
3674eda14cbcSMatt Macy 	return (err ? NULL : db);
3675eda14cbcSMatt Macy }
3676eda14cbcSMatt Macy 
3677eda14cbcSMatt Macy void
3678eda14cbcSMatt Macy dbuf_create_bonus(dnode_t *dn)
3679eda14cbcSMatt Macy {
3680eda14cbcSMatt Macy 	ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
3681eda14cbcSMatt Macy 
3682eda14cbcSMatt Macy 	ASSERT(dn->dn_bonus == NULL);
3683eda14cbcSMatt Macy 	dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL);
3684eda14cbcSMatt Macy }
3685eda14cbcSMatt Macy 
3686eda14cbcSMatt Macy int
3687eda14cbcSMatt Macy dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx)
3688eda14cbcSMatt Macy {
3689eda14cbcSMatt Macy 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
3690eda14cbcSMatt Macy 
3691eda14cbcSMatt Macy 	if (db->db_blkid != DMU_SPILL_BLKID)
3692eda14cbcSMatt Macy 		return (SET_ERROR(ENOTSUP));
3693eda14cbcSMatt Macy 	if (blksz == 0)
3694eda14cbcSMatt Macy 		blksz = SPA_MINBLOCKSIZE;
3695eda14cbcSMatt Macy 	ASSERT3U(blksz, <=, spa_maxblocksize(dmu_objset_spa(db->db_objset)));
3696eda14cbcSMatt Macy 	blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE);
3697eda14cbcSMatt Macy 
3698eda14cbcSMatt Macy 	dbuf_new_size(db, blksz, tx);
3699eda14cbcSMatt Macy 
3700eda14cbcSMatt Macy 	return (0);
3701eda14cbcSMatt Macy }
3702eda14cbcSMatt Macy 
3703eda14cbcSMatt Macy void
3704eda14cbcSMatt Macy dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx)
3705eda14cbcSMatt Macy {
3706eda14cbcSMatt Macy 	dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx);
3707eda14cbcSMatt Macy }
3708eda14cbcSMatt Macy 
3709eda14cbcSMatt Macy #pragma weak dmu_buf_add_ref = dbuf_add_ref
3710eda14cbcSMatt Macy void
3711a0b956f5SMartin Matuska dbuf_add_ref(dmu_buf_impl_t *db, const void *tag)
3712eda14cbcSMatt Macy {
3713eda14cbcSMatt Macy 	int64_t holds = zfs_refcount_add(&db->db_holds, tag);
3714eda14cbcSMatt Macy 	VERIFY3S(holds, >, 1);
3715eda14cbcSMatt Macy }
3716eda14cbcSMatt Macy 
3717eda14cbcSMatt Macy #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
3718eda14cbcSMatt Macy boolean_t
3719eda14cbcSMatt Macy dbuf_try_add_ref(dmu_buf_t *db_fake, objset_t *os, uint64_t obj, uint64_t blkid,
3720a0b956f5SMartin Matuska     const void *tag)
3721eda14cbcSMatt Macy {
3722eda14cbcSMatt Macy 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
3723eda14cbcSMatt Macy 	dmu_buf_impl_t *found_db;
3724eda14cbcSMatt Macy 	boolean_t result = B_FALSE;
3725eda14cbcSMatt Macy 
3726eda14cbcSMatt Macy 	if (blkid == DMU_BONUS_BLKID)
3727eda14cbcSMatt Macy 		found_db = dbuf_find_bonus(os, obj);
3728eda14cbcSMatt Macy 	else
3729eda14cbcSMatt Macy 		found_db = dbuf_find(os, obj, 0, blkid);
3730eda14cbcSMatt Macy 
3731eda14cbcSMatt Macy 	if (found_db != NULL) {
3732eda14cbcSMatt Macy 		if (db == found_db && dbuf_refcount(db) > db->db_dirtycnt) {
3733eda14cbcSMatt Macy 			(void) zfs_refcount_add(&db->db_holds, tag);
3734eda14cbcSMatt Macy 			result = B_TRUE;
3735eda14cbcSMatt Macy 		}
3736eda14cbcSMatt Macy 		mutex_exit(&found_db->db_mtx);
3737eda14cbcSMatt Macy 	}
3738eda14cbcSMatt Macy 	return (result);
3739eda14cbcSMatt Macy }
3740eda14cbcSMatt Macy 
3741eda14cbcSMatt Macy /*
3742eda14cbcSMatt Macy  * If you call dbuf_rele() you had better not be referencing the dnode handle
3743eda14cbcSMatt Macy  * unless you have some other direct or indirect hold on the dnode. (An indirect
3744eda14cbcSMatt Macy  * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
3745eda14cbcSMatt Macy  * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
3746eda14cbcSMatt Macy  * dnode's parent dbuf evicting its dnode handles.
3747eda14cbcSMatt Macy  */
3748eda14cbcSMatt Macy void
3749a0b956f5SMartin Matuska dbuf_rele(dmu_buf_impl_t *db, const void *tag)
3750eda14cbcSMatt Macy {
3751eda14cbcSMatt Macy 	mutex_enter(&db->db_mtx);
3752eda14cbcSMatt Macy 	dbuf_rele_and_unlock(db, tag, B_FALSE);
3753eda14cbcSMatt Macy }
3754eda14cbcSMatt Macy 
3755eda14cbcSMatt Macy void
3756a0b956f5SMartin Matuska dmu_buf_rele(dmu_buf_t *db, const void *tag)
3757eda14cbcSMatt Macy {
3758eda14cbcSMatt Macy 	dbuf_rele((dmu_buf_impl_t *)db, tag);
3759eda14cbcSMatt Macy }
3760eda14cbcSMatt Macy 
3761eda14cbcSMatt Macy /*
3762eda14cbcSMatt Macy  * dbuf_rele() for an already-locked dbuf.  This is necessary to allow
3763eda14cbcSMatt Macy  * db_dirtycnt and db_holds to be updated atomically.  The 'evicting'
3764eda14cbcSMatt Macy  * argument should be set if we are already in the dbuf-evicting code
3765eda14cbcSMatt Macy  * path, in which case we don't want to recursively evict.  This allows us to
3766eda14cbcSMatt Macy  * avoid deeply nested stacks that would have a call flow similar to this:
3767eda14cbcSMatt Macy  *
3768eda14cbcSMatt Macy  * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify()
3769eda14cbcSMatt Macy  *	^						|
3770eda14cbcSMatt Macy  *	|						|
3771eda14cbcSMatt Macy  *	+-----dbuf_destroy()<--dbuf_evict_one()<--------+
3772eda14cbcSMatt Macy  *
3773eda14cbcSMatt Macy  */
3774eda14cbcSMatt Macy void
3775a0b956f5SMartin Matuska dbuf_rele_and_unlock(dmu_buf_impl_t *db, const void *tag, boolean_t evicting)
3776eda14cbcSMatt Macy {
3777eda14cbcSMatt Macy 	int64_t holds;
3778eda14cbcSMatt Macy 	uint64_t size;
3779eda14cbcSMatt Macy 
3780eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&db->db_mtx));
3781eda14cbcSMatt Macy 	DBUF_VERIFY(db);
3782eda14cbcSMatt Macy 
3783eda14cbcSMatt Macy 	/*
3784eda14cbcSMatt Macy 	 * Remove the reference to the dbuf before removing its hold on the
3785eda14cbcSMatt Macy 	 * dnode so we can guarantee in dnode_move() that a referenced bonus
3786eda14cbcSMatt Macy 	 * buffer has a corresponding dnode hold.
3787eda14cbcSMatt Macy 	 */
3788eda14cbcSMatt Macy 	holds = zfs_refcount_remove(&db->db_holds, tag);
3789eda14cbcSMatt Macy 	ASSERT(holds >= 0);
3790eda14cbcSMatt Macy 
3791eda14cbcSMatt Macy 	/*
3792eda14cbcSMatt Macy 	 * We can't freeze indirects if there is a possibility that they
3793eda14cbcSMatt Macy 	 * may be modified in the current syncing context.
3794eda14cbcSMatt Macy 	 */
3795eda14cbcSMatt Macy 	if (db->db_buf != NULL &&
3796eda14cbcSMatt Macy 	    holds == (db->db_level == 0 ? db->db_dirtycnt : 0)) {
3797eda14cbcSMatt Macy 		arc_buf_freeze(db->db_buf);
3798eda14cbcSMatt Macy 	}
3799eda14cbcSMatt Macy 
3800eda14cbcSMatt Macy 	if (holds == db->db_dirtycnt &&
3801eda14cbcSMatt Macy 	    db->db_level == 0 && db->db_user_immediate_evict)
3802eda14cbcSMatt Macy 		dbuf_evict_user(db);
3803eda14cbcSMatt Macy 
3804eda14cbcSMatt Macy 	if (holds == 0) {
3805eda14cbcSMatt Macy 		if (db->db_blkid == DMU_BONUS_BLKID) {
3806eda14cbcSMatt Macy 			dnode_t *dn;
3807eda14cbcSMatt Macy 			boolean_t evict_dbuf = db->db_pending_evict;
3808eda14cbcSMatt Macy 
3809eda14cbcSMatt Macy 			/*
3810eda14cbcSMatt Macy 			 * If the dnode moves here, we cannot cross this
3811eda14cbcSMatt Macy 			 * barrier until the move completes.
3812eda14cbcSMatt Macy 			 */
3813eda14cbcSMatt Macy 			DB_DNODE_ENTER(db);
3814eda14cbcSMatt Macy 
3815eda14cbcSMatt Macy 			dn = DB_DNODE(db);
3816eda14cbcSMatt Macy 			atomic_dec_32(&dn->dn_dbufs_count);
3817eda14cbcSMatt Macy 
3818eda14cbcSMatt Macy 			/*
3819eda14cbcSMatt Macy 			 * Decrementing the dbuf count means that the bonus
3820eda14cbcSMatt Macy 			 * buffer's dnode hold is no longer discounted in
3821eda14cbcSMatt Macy 			 * dnode_move(). The dnode cannot move until after
3822eda14cbcSMatt Macy 			 * the dnode_rele() below.
3823eda14cbcSMatt Macy 			 */
3824eda14cbcSMatt Macy 			DB_DNODE_EXIT(db);
3825eda14cbcSMatt Macy 
3826eda14cbcSMatt Macy 			/*
3827eda14cbcSMatt Macy 			 * Do not reference db after its lock is dropped.
3828eda14cbcSMatt Macy 			 * Another thread may evict it.
3829eda14cbcSMatt Macy 			 */
3830eda14cbcSMatt Macy 			mutex_exit(&db->db_mtx);
3831eda14cbcSMatt Macy 
3832eda14cbcSMatt Macy 			if (evict_dbuf)
3833eda14cbcSMatt Macy 				dnode_evict_bonus(dn);
3834eda14cbcSMatt Macy 
3835eda14cbcSMatt Macy 			dnode_rele(dn, db);
3836eda14cbcSMatt Macy 		} else if (db->db_buf == NULL) {
3837eda14cbcSMatt Macy 			/*
3838eda14cbcSMatt Macy 			 * This is a special case: we never associated this
3839eda14cbcSMatt Macy 			 * dbuf with any data allocated from the ARC.
3840eda14cbcSMatt Macy 			 */
3841eda14cbcSMatt Macy 			ASSERT(db->db_state == DB_UNCACHED ||
3842eda14cbcSMatt Macy 			    db->db_state == DB_NOFILL);
3843eda14cbcSMatt Macy 			dbuf_destroy(db);
3844eda14cbcSMatt Macy 		} else if (arc_released(db->db_buf)) {
3845eda14cbcSMatt Macy 			/*
3846eda14cbcSMatt Macy 			 * This dbuf has anonymous data associated with it.
3847eda14cbcSMatt Macy 			 */
3848eda14cbcSMatt Macy 			dbuf_destroy(db);
3849eda14cbcSMatt Macy 		} else {
3850eda14cbcSMatt Macy 			boolean_t do_arc_evict = B_FALSE;
3851eda14cbcSMatt Macy 			blkptr_t bp;
3852eda14cbcSMatt Macy 			spa_t *spa = dmu_objset_spa(db->db_objset);
3853eda14cbcSMatt Macy 
3854eda14cbcSMatt Macy 			if (!DBUF_IS_CACHEABLE(db) &&
3855eda14cbcSMatt Macy 			    db->db_blkptr != NULL &&
3856eda14cbcSMatt Macy 			    !BP_IS_HOLE(db->db_blkptr) &&
3857eda14cbcSMatt Macy 			    !BP_IS_EMBEDDED(db->db_blkptr)) {
3858eda14cbcSMatt Macy 				do_arc_evict = B_TRUE;
3859eda14cbcSMatt Macy 				bp = *db->db_blkptr;
3860eda14cbcSMatt Macy 			}
3861eda14cbcSMatt Macy 
3862eda14cbcSMatt Macy 			if (!DBUF_IS_CACHEABLE(db) ||
3863eda14cbcSMatt Macy 			    db->db_pending_evict) {
3864eda14cbcSMatt Macy 				dbuf_destroy(db);
3865eda14cbcSMatt Macy 			} else if (!multilist_link_active(&db->db_cache_link)) {
3866eda14cbcSMatt Macy 				ASSERT3U(db->db_caching_status, ==,
3867eda14cbcSMatt Macy 				    DB_NO_CACHE);
3868eda14cbcSMatt Macy 
3869eda14cbcSMatt Macy 				dbuf_cached_state_t dcs =
3870eda14cbcSMatt Macy 				    dbuf_include_in_metadata_cache(db) ?
3871eda14cbcSMatt Macy 				    DB_DBUF_METADATA_CACHE : DB_DBUF_CACHE;
3872eda14cbcSMatt Macy 				db->db_caching_status = dcs;
3873eda14cbcSMatt Macy 
38743ff01b23SMartin Matuska 				multilist_insert(&dbuf_caches[dcs].cache, db);
38750d8fe237SMartin Matuska 				uint64_t db_size = db->db.db_size;
3876eda14cbcSMatt Macy 				size = zfs_refcount_add_many(
38770d8fe237SMartin Matuska 				    &dbuf_caches[dcs].size, db_size, db);
38780d8fe237SMartin Matuska 				uint8_t db_level = db->db_level;
38790d8fe237SMartin Matuska 				mutex_exit(&db->db_mtx);
3880eda14cbcSMatt Macy 
3881eda14cbcSMatt Macy 				if (dcs == DB_DBUF_METADATA_CACHE) {
3882eda14cbcSMatt Macy 					DBUF_STAT_BUMP(metadata_cache_count);
3883eda14cbcSMatt Macy 					DBUF_STAT_MAX(
3884eda14cbcSMatt Macy 					    metadata_cache_size_bytes_max,
3885eda14cbcSMatt Macy 					    size);
3886eda14cbcSMatt Macy 				} else {
3887eda14cbcSMatt Macy 					DBUF_STAT_BUMP(cache_count);
3888eda14cbcSMatt Macy 					DBUF_STAT_MAX(cache_size_bytes_max,
3889eda14cbcSMatt Macy 					    size);
38900d8fe237SMartin Matuska 					DBUF_STAT_BUMP(cache_levels[db_level]);
38910d8fe237SMartin Matuska 					DBUF_STAT_INCR(
38920d8fe237SMartin Matuska 					    cache_levels_bytes[db_level],
38930d8fe237SMartin Matuska 					    db_size);
3894eda14cbcSMatt Macy 				}
3895eda14cbcSMatt Macy 
3896eda14cbcSMatt Macy 				if (dcs == DB_DBUF_CACHE && !evicting)
3897eda14cbcSMatt Macy 					dbuf_evict_notify(size);
3898eda14cbcSMatt Macy 			}
3899eda14cbcSMatt Macy 
3900eda14cbcSMatt Macy 			if (do_arc_evict)
3901eda14cbcSMatt Macy 				arc_freed(spa, &bp);
3902eda14cbcSMatt Macy 		}
3903eda14cbcSMatt Macy 	} else {
3904eda14cbcSMatt Macy 		mutex_exit(&db->db_mtx);
3905eda14cbcSMatt Macy 	}
3906eda14cbcSMatt Macy 
3907eda14cbcSMatt Macy }
3908eda14cbcSMatt Macy 
3909eda14cbcSMatt Macy #pragma weak dmu_buf_refcount = dbuf_refcount
3910eda14cbcSMatt Macy uint64_t
3911eda14cbcSMatt Macy dbuf_refcount(dmu_buf_impl_t *db)
3912eda14cbcSMatt Macy {
3913eda14cbcSMatt Macy 	return (zfs_refcount_count(&db->db_holds));
3914eda14cbcSMatt Macy }
3915eda14cbcSMatt Macy 
3916eda14cbcSMatt Macy uint64_t
3917eda14cbcSMatt Macy dmu_buf_user_refcount(dmu_buf_t *db_fake)
3918eda14cbcSMatt Macy {
3919eda14cbcSMatt Macy 	uint64_t holds;
3920eda14cbcSMatt Macy 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
3921eda14cbcSMatt Macy 
3922eda14cbcSMatt Macy 	mutex_enter(&db->db_mtx);
3923eda14cbcSMatt Macy 	ASSERT3U(zfs_refcount_count(&db->db_holds), >=, db->db_dirtycnt);
3924eda14cbcSMatt Macy 	holds = zfs_refcount_count(&db->db_holds) - db->db_dirtycnt;
3925eda14cbcSMatt Macy 	mutex_exit(&db->db_mtx);
3926eda14cbcSMatt Macy 
3927eda14cbcSMatt Macy 	return (holds);
3928eda14cbcSMatt Macy }
3929eda14cbcSMatt Macy 
3930eda14cbcSMatt Macy void *
3931eda14cbcSMatt Macy dmu_buf_replace_user(dmu_buf_t *db_fake, dmu_buf_user_t *old_user,
3932eda14cbcSMatt Macy     dmu_buf_user_t *new_user)
3933eda14cbcSMatt Macy {
3934eda14cbcSMatt Macy 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
3935eda14cbcSMatt Macy 
3936eda14cbcSMatt Macy 	mutex_enter(&db->db_mtx);
3937eda14cbcSMatt Macy 	dbuf_verify_user(db, DBVU_NOT_EVICTING);
3938eda14cbcSMatt Macy 	if (db->db_user == old_user)
3939eda14cbcSMatt Macy 		db->db_user = new_user;
3940eda14cbcSMatt Macy 	else
3941eda14cbcSMatt Macy 		old_user = db->db_user;
3942eda14cbcSMatt Macy 	dbuf_verify_user(db, DBVU_NOT_EVICTING);
3943eda14cbcSMatt Macy 	mutex_exit(&db->db_mtx);
3944eda14cbcSMatt Macy 
3945eda14cbcSMatt Macy 	return (old_user);
3946eda14cbcSMatt Macy }
3947eda14cbcSMatt Macy 
3948eda14cbcSMatt Macy void *
3949eda14cbcSMatt Macy dmu_buf_set_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
3950eda14cbcSMatt Macy {
3951eda14cbcSMatt Macy 	return (dmu_buf_replace_user(db_fake, NULL, user));
3952eda14cbcSMatt Macy }
3953eda14cbcSMatt Macy 
3954eda14cbcSMatt Macy void *
3955eda14cbcSMatt Macy dmu_buf_set_user_ie(dmu_buf_t *db_fake, dmu_buf_user_t *user)
3956eda14cbcSMatt Macy {
3957eda14cbcSMatt Macy 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
3958eda14cbcSMatt Macy 
3959eda14cbcSMatt Macy 	db->db_user_immediate_evict = TRUE;
3960eda14cbcSMatt Macy 	return (dmu_buf_set_user(db_fake, user));
3961eda14cbcSMatt Macy }
3962eda14cbcSMatt Macy 
3963eda14cbcSMatt Macy void *
3964eda14cbcSMatt Macy dmu_buf_remove_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
3965eda14cbcSMatt Macy {
3966eda14cbcSMatt Macy 	return (dmu_buf_replace_user(db_fake, user, NULL));
3967eda14cbcSMatt Macy }
3968eda14cbcSMatt Macy 
3969eda14cbcSMatt Macy void *
3970eda14cbcSMatt Macy dmu_buf_get_user(dmu_buf_t *db_fake)
3971eda14cbcSMatt Macy {
3972eda14cbcSMatt Macy 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
3973eda14cbcSMatt Macy 
3974eda14cbcSMatt Macy 	dbuf_verify_user(db, DBVU_NOT_EVICTING);
3975eda14cbcSMatt Macy 	return (db->db_user);
3976eda14cbcSMatt Macy }
3977eda14cbcSMatt Macy 
3978eda14cbcSMatt Macy void
3979716fd348SMartin Matuska dmu_buf_user_evict_wait(void)
3980eda14cbcSMatt Macy {
3981eda14cbcSMatt Macy 	taskq_wait(dbu_evict_taskq);
3982eda14cbcSMatt Macy }
3983eda14cbcSMatt Macy 
3984eda14cbcSMatt Macy blkptr_t *
3985eda14cbcSMatt Macy dmu_buf_get_blkptr(dmu_buf_t *db)
3986eda14cbcSMatt Macy {
3987eda14cbcSMatt Macy 	dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
3988eda14cbcSMatt Macy 	return (dbi->db_blkptr);
3989eda14cbcSMatt Macy }
3990eda14cbcSMatt Macy 
3991eda14cbcSMatt Macy objset_t *
3992eda14cbcSMatt Macy dmu_buf_get_objset(dmu_buf_t *db)
3993eda14cbcSMatt Macy {
3994eda14cbcSMatt Macy 	dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
3995eda14cbcSMatt Macy 	return (dbi->db_objset);
3996eda14cbcSMatt Macy }
3997eda14cbcSMatt Macy 
3998eda14cbcSMatt Macy dnode_t *
3999eda14cbcSMatt Macy dmu_buf_dnode_enter(dmu_buf_t *db)
4000eda14cbcSMatt Macy {
4001eda14cbcSMatt Macy 	dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
4002eda14cbcSMatt Macy 	DB_DNODE_ENTER(dbi);
4003eda14cbcSMatt Macy 	return (DB_DNODE(dbi));
4004eda14cbcSMatt Macy }
4005eda14cbcSMatt Macy 
4006eda14cbcSMatt Macy void
4007eda14cbcSMatt Macy dmu_buf_dnode_exit(dmu_buf_t *db)
4008eda14cbcSMatt Macy {
4009eda14cbcSMatt Macy 	dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
4010eda14cbcSMatt Macy 	DB_DNODE_EXIT(dbi);
4011eda14cbcSMatt Macy }
4012eda14cbcSMatt Macy 
4013eda14cbcSMatt Macy static void
4014eda14cbcSMatt Macy dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db)
4015eda14cbcSMatt Macy {
4016eda14cbcSMatt Macy 	/* ASSERT(dmu_tx_is_syncing(tx) */
4017eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&db->db_mtx));
4018eda14cbcSMatt Macy 
4019eda14cbcSMatt Macy 	if (db->db_blkptr != NULL)
4020eda14cbcSMatt Macy 		return;
4021eda14cbcSMatt Macy 
4022eda14cbcSMatt Macy 	if (db->db_blkid == DMU_SPILL_BLKID) {
4023eda14cbcSMatt Macy 		db->db_blkptr = DN_SPILL_BLKPTR(dn->dn_phys);
4024eda14cbcSMatt Macy 		BP_ZERO(db->db_blkptr);
4025eda14cbcSMatt Macy 		return;
4026eda14cbcSMatt Macy 	}
4027eda14cbcSMatt Macy 	if (db->db_level == dn->dn_phys->dn_nlevels-1) {
4028eda14cbcSMatt Macy 		/*
4029eda14cbcSMatt Macy 		 * This buffer was allocated at a time when there was
4030eda14cbcSMatt Macy 		 * no available blkptrs from the dnode, or it was
4031eda14cbcSMatt Macy 		 * inappropriate to hook it in (i.e., nlevels mismatch).
4032eda14cbcSMatt Macy 		 */
4033eda14cbcSMatt Macy 		ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr);
4034eda14cbcSMatt Macy 		ASSERT(db->db_parent == NULL);
4035eda14cbcSMatt Macy 		db->db_parent = dn->dn_dbuf;
4036eda14cbcSMatt Macy 		db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
4037eda14cbcSMatt Macy 		DBUF_VERIFY(db);
4038eda14cbcSMatt Macy 	} else {
4039eda14cbcSMatt Macy 		dmu_buf_impl_t *parent = db->db_parent;
4040eda14cbcSMatt Macy 		int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
4041eda14cbcSMatt Macy 
4042eda14cbcSMatt Macy 		ASSERT(dn->dn_phys->dn_nlevels > 1);
4043eda14cbcSMatt Macy 		if (parent == NULL) {
4044eda14cbcSMatt Macy 			mutex_exit(&db->db_mtx);
4045eda14cbcSMatt Macy 			rw_enter(&dn->dn_struct_rwlock, RW_READER);
4046eda14cbcSMatt Macy 			parent = dbuf_hold_level(dn, db->db_level + 1,
4047eda14cbcSMatt Macy 			    db->db_blkid >> epbs, db);
4048eda14cbcSMatt Macy 			rw_exit(&dn->dn_struct_rwlock);
4049eda14cbcSMatt Macy 			mutex_enter(&db->db_mtx);
4050eda14cbcSMatt Macy 			db->db_parent = parent;
4051eda14cbcSMatt Macy 		}
4052eda14cbcSMatt Macy 		db->db_blkptr = (blkptr_t *)parent->db.db_data +
4053eda14cbcSMatt Macy 		    (db->db_blkid & ((1ULL << epbs) - 1));
4054eda14cbcSMatt Macy 		DBUF_VERIFY(db);
4055eda14cbcSMatt Macy 	}
4056eda14cbcSMatt Macy }
4057eda14cbcSMatt Macy 
4058eda14cbcSMatt Macy static void
4059eda14cbcSMatt Macy dbuf_sync_bonus(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
4060eda14cbcSMatt Macy {
4061eda14cbcSMatt Macy 	dmu_buf_impl_t *db = dr->dr_dbuf;
4062eda14cbcSMatt Macy 	void *data = dr->dt.dl.dr_data;
4063eda14cbcSMatt Macy 
4064eda14cbcSMatt Macy 	ASSERT0(db->db_level);
4065eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&db->db_mtx));
4066eda14cbcSMatt Macy 	ASSERT(db->db_blkid == DMU_BONUS_BLKID);
4067eda14cbcSMatt Macy 	ASSERT(data != NULL);
4068eda14cbcSMatt Macy 
40697877fdebSMatt Macy 	dnode_t *dn = dr->dr_dnode;
4070eda14cbcSMatt Macy 	ASSERT3U(DN_MAX_BONUS_LEN(dn->dn_phys), <=,
4071eda14cbcSMatt Macy 	    DN_SLOTS_TO_BONUSLEN(dn->dn_phys->dn_extra_slots + 1));
4072da5137abSMartin Matuska 	memcpy(DN_BONUS(dn->dn_phys), data, DN_MAX_BONUS_LEN(dn->dn_phys));
4073eda14cbcSMatt Macy 
4074eda14cbcSMatt Macy 	dbuf_sync_leaf_verify_bonus_dnode(dr);
4075eda14cbcSMatt Macy 
4076eda14cbcSMatt Macy 	dbuf_undirty_bonus(dr);
4077eda14cbcSMatt Macy 	dbuf_rele_and_unlock(db, (void *)(uintptr_t)tx->tx_txg, B_FALSE);
4078eda14cbcSMatt Macy }
4079eda14cbcSMatt Macy 
4080eda14cbcSMatt Macy /*
4081eda14cbcSMatt Macy  * When syncing out a blocks of dnodes, adjust the block to deal with
4082eda14cbcSMatt Macy  * encryption.  Normally, we make sure the block is decrypted before writing
4083eda14cbcSMatt Macy  * it.  If we have crypt params, then we are writing a raw (encrypted) block,
4084eda14cbcSMatt Macy  * from a raw receive.  In this case, set the ARC buf's crypt params so
4085eda14cbcSMatt Macy  * that the BP will be filled with the correct byteorder, salt, iv, and mac.
4086eda14cbcSMatt Macy  */
4087eda14cbcSMatt Macy static void
4088eda14cbcSMatt Macy dbuf_prepare_encrypted_dnode_leaf(dbuf_dirty_record_t *dr)
4089eda14cbcSMatt Macy {
4090eda14cbcSMatt Macy 	int err;
4091eda14cbcSMatt Macy 	dmu_buf_impl_t *db = dr->dr_dbuf;
4092eda14cbcSMatt Macy 
4093eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&db->db_mtx));
4094eda14cbcSMatt Macy 	ASSERT3U(db->db.db_object, ==, DMU_META_DNODE_OBJECT);
4095eda14cbcSMatt Macy 	ASSERT3U(db->db_level, ==, 0);
4096eda14cbcSMatt Macy 
4097eda14cbcSMatt Macy 	if (!db->db_objset->os_raw_receive && arc_is_encrypted(db->db_buf)) {
4098eda14cbcSMatt Macy 		zbookmark_phys_t zb;
4099eda14cbcSMatt Macy 
4100eda14cbcSMatt Macy 		/*
4101eda14cbcSMatt Macy 		 * Unfortunately, there is currently no mechanism for
4102eda14cbcSMatt Macy 		 * syncing context to handle decryption errors. An error
4103eda14cbcSMatt Macy 		 * here is only possible if an attacker maliciously
4104eda14cbcSMatt Macy 		 * changed a dnode block and updated the associated
4105eda14cbcSMatt Macy 		 * checksums going up the block tree.
4106eda14cbcSMatt Macy 		 */
4107eda14cbcSMatt Macy 		SET_BOOKMARK(&zb, dmu_objset_id(db->db_objset),
4108eda14cbcSMatt Macy 		    db->db.db_object, db->db_level, db->db_blkid);
4109eda14cbcSMatt Macy 		err = arc_untransform(db->db_buf, db->db_objset->os_spa,
4110eda14cbcSMatt Macy 		    &zb, B_TRUE);
4111eda14cbcSMatt Macy 		if (err)
4112eda14cbcSMatt Macy 			panic("Invalid dnode block MAC");
4113eda14cbcSMatt Macy 	} else if (dr->dt.dl.dr_has_raw_params) {
4114eda14cbcSMatt Macy 		(void) arc_release(dr->dt.dl.dr_data, db);
4115eda14cbcSMatt Macy 		arc_convert_to_raw(dr->dt.dl.dr_data,
4116eda14cbcSMatt Macy 		    dmu_objset_id(db->db_objset),
4117eda14cbcSMatt Macy 		    dr->dt.dl.dr_byteorder, DMU_OT_DNODE,
4118eda14cbcSMatt Macy 		    dr->dt.dl.dr_salt, dr->dt.dl.dr_iv, dr->dt.dl.dr_mac);
4119eda14cbcSMatt Macy 	}
4120eda14cbcSMatt Macy }
4121eda14cbcSMatt Macy 
4122eda14cbcSMatt Macy /*
4123eda14cbcSMatt Macy  * dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
4124eda14cbcSMatt Macy  * is critical the we not allow the compiler to inline this function in to
4125eda14cbcSMatt Macy  * dbuf_sync_list() thereby drastically bloating the stack usage.
4126eda14cbcSMatt Macy  */
4127eda14cbcSMatt Macy noinline static void
4128eda14cbcSMatt Macy dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
4129eda14cbcSMatt Macy {
4130eda14cbcSMatt Macy 	dmu_buf_impl_t *db = dr->dr_dbuf;
41317877fdebSMatt Macy 	dnode_t *dn = dr->dr_dnode;
4132eda14cbcSMatt Macy 
4133eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
4134eda14cbcSMatt Macy 
4135eda14cbcSMatt Macy 	dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
4136eda14cbcSMatt Macy 
4137eda14cbcSMatt Macy 	mutex_enter(&db->db_mtx);
4138eda14cbcSMatt Macy 
4139eda14cbcSMatt Macy 	ASSERT(db->db_level > 0);
4140eda14cbcSMatt Macy 	DBUF_VERIFY(db);
4141eda14cbcSMatt Macy 
4142eda14cbcSMatt Macy 	/* Read the block if it hasn't been read yet. */
4143eda14cbcSMatt Macy 	if (db->db_buf == NULL) {
4144eda14cbcSMatt Macy 		mutex_exit(&db->db_mtx);
4145eda14cbcSMatt Macy 		(void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
4146eda14cbcSMatt Macy 		mutex_enter(&db->db_mtx);
4147eda14cbcSMatt Macy 	}
4148eda14cbcSMatt Macy 	ASSERT3U(db->db_state, ==, DB_CACHED);
4149eda14cbcSMatt Macy 	ASSERT(db->db_buf != NULL);
4150eda14cbcSMatt Macy 
4151eda14cbcSMatt Macy 	/* Indirect block size must match what the dnode thinks it is. */
4152eda14cbcSMatt Macy 	ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
4153eda14cbcSMatt Macy 	dbuf_check_blkptr(dn, db);
4154eda14cbcSMatt Macy 
4155eda14cbcSMatt Macy 	/* Provide the pending dirty record to child dbufs */
4156eda14cbcSMatt Macy 	db->db_data_pending = dr;
4157eda14cbcSMatt Macy 
4158eda14cbcSMatt Macy 	mutex_exit(&db->db_mtx);
4159eda14cbcSMatt Macy 
4160eda14cbcSMatt Macy 	dbuf_write(dr, db->db_buf, tx);
4161eda14cbcSMatt Macy 
41627877fdebSMatt Macy 	zio_t *zio = dr->dr_zio;
4163eda14cbcSMatt Macy 	mutex_enter(&dr->dt.di.dr_mtx);
4164eda14cbcSMatt Macy 	dbuf_sync_list(&dr->dt.di.dr_children, db->db_level - 1, tx);
4165eda14cbcSMatt Macy 	ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
4166eda14cbcSMatt Macy 	mutex_exit(&dr->dt.di.dr_mtx);
4167eda14cbcSMatt Macy 	zio_nowait(zio);
4168eda14cbcSMatt Macy }
4169eda14cbcSMatt Macy 
4170eda14cbcSMatt Macy /*
4171eda14cbcSMatt Macy  * Verify that the size of the data in our bonus buffer does not exceed
4172eda14cbcSMatt Macy  * its recorded size.
4173eda14cbcSMatt Macy  *
4174eda14cbcSMatt Macy  * The purpose of this verification is to catch any cases in development
4175eda14cbcSMatt Macy  * where the size of a phys structure (i.e space_map_phys_t) grows and,
4176eda14cbcSMatt Macy  * due to incorrect feature management, older pools expect to read more
4177eda14cbcSMatt Macy  * data even though they didn't actually write it to begin with.
4178eda14cbcSMatt Macy  *
4179eda14cbcSMatt Macy  * For a example, this would catch an error in the feature logic where we
4180eda14cbcSMatt Macy  * open an older pool and we expect to write the space map histogram of
4181eda14cbcSMatt Macy  * a space map with size SPACE_MAP_SIZE_V0.
4182eda14cbcSMatt Macy  */
4183eda14cbcSMatt Macy static void
4184eda14cbcSMatt Macy dbuf_sync_leaf_verify_bonus_dnode(dbuf_dirty_record_t *dr)
4185eda14cbcSMatt Macy {
4186eda14cbcSMatt Macy #ifdef ZFS_DEBUG
41877877fdebSMatt Macy 	dnode_t *dn = dr->dr_dnode;
4188eda14cbcSMatt Macy 
4189eda14cbcSMatt Macy 	/*
4190eda14cbcSMatt Macy 	 * Encrypted bonus buffers can have data past their bonuslen.
4191eda14cbcSMatt Macy 	 * Skip the verification of these blocks.
4192eda14cbcSMatt Macy 	 */
4193eda14cbcSMatt Macy 	if (DMU_OT_IS_ENCRYPTED(dn->dn_bonustype))
4194eda14cbcSMatt Macy 		return;
4195eda14cbcSMatt Macy 
4196eda14cbcSMatt Macy 	uint16_t bonuslen = dn->dn_phys->dn_bonuslen;
4197eda14cbcSMatt Macy 	uint16_t maxbonuslen = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots);
4198eda14cbcSMatt Macy 	ASSERT3U(bonuslen, <=, maxbonuslen);
4199eda14cbcSMatt Macy 
4200eda14cbcSMatt Macy 	arc_buf_t *datap = dr->dt.dl.dr_data;
4201eda14cbcSMatt Macy 	char *datap_end = ((char *)datap) + bonuslen;
4202eda14cbcSMatt Macy 	char *datap_max = ((char *)datap) + maxbonuslen;
4203eda14cbcSMatt Macy 
4204eda14cbcSMatt Macy 	/* ensure that everything is zero after our data */
4205eda14cbcSMatt Macy 	for (; datap_end < datap_max; datap_end++)
4206eda14cbcSMatt Macy 		ASSERT(*datap_end == 0);
4207eda14cbcSMatt Macy #endif
4208eda14cbcSMatt Macy }
4209eda14cbcSMatt Macy 
42107877fdebSMatt Macy static blkptr_t *
42117877fdebSMatt Macy dbuf_lightweight_bp(dbuf_dirty_record_t *dr)
42127877fdebSMatt Macy {
42137877fdebSMatt Macy 	/* This must be a lightweight dirty record. */
42147877fdebSMatt Macy 	ASSERT3P(dr->dr_dbuf, ==, NULL);
42157877fdebSMatt Macy 	dnode_t *dn = dr->dr_dnode;
42167877fdebSMatt Macy 
42177877fdebSMatt Macy 	if (dn->dn_phys->dn_nlevels == 1) {
42187877fdebSMatt Macy 		VERIFY3U(dr->dt.dll.dr_blkid, <, dn->dn_phys->dn_nblkptr);
42197877fdebSMatt Macy 		return (&dn->dn_phys->dn_blkptr[dr->dt.dll.dr_blkid]);
42207877fdebSMatt Macy 	} else {
42217877fdebSMatt Macy 		dmu_buf_impl_t *parent_db = dr->dr_parent->dr_dbuf;
42227877fdebSMatt Macy 		int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
42237877fdebSMatt Macy 		VERIFY3U(parent_db->db_level, ==, 1);
42247877fdebSMatt Macy 		VERIFY3P(parent_db->db_dnode_handle->dnh_dnode, ==, dn);
42257877fdebSMatt Macy 		VERIFY3U(dr->dt.dll.dr_blkid >> epbs, ==, parent_db->db_blkid);
42267877fdebSMatt Macy 		blkptr_t *bp = parent_db->db.db_data;
42277877fdebSMatt Macy 		return (&bp[dr->dt.dll.dr_blkid & ((1 << epbs) - 1)]);
42287877fdebSMatt Macy 	}
42297877fdebSMatt Macy }
42307877fdebSMatt Macy 
42317877fdebSMatt Macy static void
42327877fdebSMatt Macy dbuf_lightweight_ready(zio_t *zio)
42337877fdebSMatt Macy {
42347877fdebSMatt Macy 	dbuf_dirty_record_t *dr = zio->io_private;
42357877fdebSMatt Macy 	blkptr_t *bp = zio->io_bp;
42367877fdebSMatt Macy 
42377877fdebSMatt Macy 	if (zio->io_error != 0)
42387877fdebSMatt Macy 		return;
42397877fdebSMatt Macy 
42407877fdebSMatt Macy 	dnode_t *dn = dr->dr_dnode;
42417877fdebSMatt Macy 
42427877fdebSMatt Macy 	blkptr_t *bp_orig = dbuf_lightweight_bp(dr);
42437877fdebSMatt Macy 	spa_t *spa = dmu_objset_spa(dn->dn_objset);
42447877fdebSMatt Macy 	int64_t delta = bp_get_dsize_sync(spa, bp) -
42457877fdebSMatt Macy 	    bp_get_dsize_sync(spa, bp_orig);
42467877fdebSMatt Macy 	dnode_diduse_space(dn, delta);
42477877fdebSMatt Macy 
42487877fdebSMatt Macy 	uint64_t blkid = dr->dt.dll.dr_blkid;
42497877fdebSMatt Macy 	mutex_enter(&dn->dn_mtx);
42507877fdebSMatt Macy 	if (blkid > dn->dn_phys->dn_maxblkid) {
42517877fdebSMatt Macy 		ASSERT0(dn->dn_objset->os_raw_receive);
42527877fdebSMatt Macy 		dn->dn_phys->dn_maxblkid = blkid;
42537877fdebSMatt Macy 	}
42547877fdebSMatt Macy 	mutex_exit(&dn->dn_mtx);
42557877fdebSMatt Macy 
42567877fdebSMatt Macy 	if (!BP_IS_EMBEDDED(bp)) {
42577877fdebSMatt Macy 		uint64_t fill = BP_IS_HOLE(bp) ? 0 : 1;
42587877fdebSMatt Macy 		BP_SET_FILL(bp, fill);
42597877fdebSMatt Macy 	}
42607877fdebSMatt Macy 
42617877fdebSMatt Macy 	dmu_buf_impl_t *parent_db;
42627877fdebSMatt Macy 	EQUIV(dr->dr_parent == NULL, dn->dn_phys->dn_nlevels == 1);
42637877fdebSMatt Macy 	if (dr->dr_parent == NULL) {
42647877fdebSMatt Macy 		parent_db = dn->dn_dbuf;
42657877fdebSMatt Macy 	} else {
42667877fdebSMatt Macy 		parent_db = dr->dr_parent->dr_dbuf;
42677877fdebSMatt Macy 	}
42687877fdebSMatt Macy 	rw_enter(&parent_db->db_rwlock, RW_WRITER);
42697877fdebSMatt Macy 	*bp_orig = *bp;
42707877fdebSMatt Macy 	rw_exit(&parent_db->db_rwlock);
42717877fdebSMatt Macy }
42727877fdebSMatt Macy 
42737877fdebSMatt Macy static void
42747877fdebSMatt Macy dbuf_lightweight_physdone(zio_t *zio)
42757877fdebSMatt Macy {
42767877fdebSMatt Macy 	dbuf_dirty_record_t *dr = zio->io_private;
42777877fdebSMatt Macy 	dsl_pool_t *dp = spa_get_dsl(zio->io_spa);
42787877fdebSMatt Macy 	ASSERT3U(dr->dr_txg, ==, zio->io_txg);
42797877fdebSMatt Macy 
42807877fdebSMatt Macy 	/*
42817877fdebSMatt Macy 	 * The callback will be called io_phys_children times.  Retire one
42827877fdebSMatt Macy 	 * portion of our dirty space each time we are called.  Any rounding
42837877fdebSMatt Macy 	 * error will be cleaned up by dbuf_lightweight_done().
42847877fdebSMatt Macy 	 */
42857877fdebSMatt Macy 	int delta = dr->dr_accounted / zio->io_phys_children;
42867877fdebSMatt Macy 	dsl_pool_undirty_space(dp, delta, zio->io_txg);
42877877fdebSMatt Macy }
42887877fdebSMatt Macy 
42897877fdebSMatt Macy static void
42907877fdebSMatt Macy dbuf_lightweight_done(zio_t *zio)
42917877fdebSMatt Macy {
42927877fdebSMatt Macy 	dbuf_dirty_record_t *dr = zio->io_private;
42937877fdebSMatt Macy 
42947877fdebSMatt Macy 	VERIFY0(zio->io_error);
42957877fdebSMatt Macy 
42967877fdebSMatt Macy 	objset_t *os = dr->dr_dnode->dn_objset;
42977877fdebSMatt Macy 	dmu_tx_t *tx = os->os_synctx;
42987877fdebSMatt Macy 
42997877fdebSMatt Macy 	if (zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)) {
43007877fdebSMatt Macy 		ASSERT(BP_EQUAL(zio->io_bp, &zio->io_bp_orig));
43017877fdebSMatt Macy 	} else {
43027877fdebSMatt Macy 		dsl_dataset_t *ds = os->os_dsl_dataset;
43037877fdebSMatt Macy 		(void) dsl_dataset_block_kill(ds, &zio->io_bp_orig, tx, B_TRUE);
43047877fdebSMatt Macy 		dsl_dataset_block_born(ds, zio->io_bp, tx);
43057877fdebSMatt Macy 	}
43067877fdebSMatt Macy 
43077877fdebSMatt Macy 	/*
43087877fdebSMatt Macy 	 * See comment in dbuf_write_done().
43097877fdebSMatt Macy 	 */
43107877fdebSMatt Macy 	if (zio->io_phys_children == 0) {
43117877fdebSMatt Macy 		dsl_pool_undirty_space(dmu_objset_pool(os),
43127877fdebSMatt Macy 		    dr->dr_accounted, zio->io_txg);
43137877fdebSMatt Macy 	} else {
43147877fdebSMatt Macy 		dsl_pool_undirty_space(dmu_objset_pool(os),
43157877fdebSMatt Macy 		    dr->dr_accounted % zio->io_phys_children, zio->io_txg);
43167877fdebSMatt Macy 	}
43177877fdebSMatt Macy 
43187877fdebSMatt Macy 	abd_free(dr->dt.dll.dr_abd);
43197877fdebSMatt Macy 	kmem_free(dr, sizeof (*dr));
43207877fdebSMatt Macy }
43217877fdebSMatt Macy 
43227877fdebSMatt Macy noinline static void
43237877fdebSMatt Macy dbuf_sync_lightweight(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
43247877fdebSMatt Macy {
43257877fdebSMatt Macy 	dnode_t *dn = dr->dr_dnode;
43267877fdebSMatt Macy 	zio_t *pio;
43277877fdebSMatt Macy 	if (dn->dn_phys->dn_nlevels == 1) {
43287877fdebSMatt Macy 		pio = dn->dn_zio;
43297877fdebSMatt Macy 	} else {
43307877fdebSMatt Macy 		pio = dr->dr_parent->dr_zio;
43317877fdebSMatt Macy 	}
43327877fdebSMatt Macy 
43337877fdebSMatt Macy 	zbookmark_phys_t zb = {
43347877fdebSMatt Macy 		.zb_objset = dmu_objset_id(dn->dn_objset),
43357877fdebSMatt Macy 		.zb_object = dn->dn_object,
43367877fdebSMatt Macy 		.zb_level = 0,
43377877fdebSMatt Macy 		.zb_blkid = dr->dt.dll.dr_blkid,
43387877fdebSMatt Macy 	};
43397877fdebSMatt Macy 
43407877fdebSMatt Macy 	/*
43417877fdebSMatt Macy 	 * See comment in dbuf_write().  This is so that zio->io_bp_orig
43427877fdebSMatt Macy 	 * will have the old BP in dbuf_lightweight_done().
43437877fdebSMatt Macy 	 */
43447877fdebSMatt Macy 	dr->dr_bp_copy = *dbuf_lightweight_bp(dr);
43457877fdebSMatt Macy 
43467877fdebSMatt Macy 	dr->dr_zio = zio_write(pio, dmu_objset_spa(dn->dn_objset),
43477877fdebSMatt Macy 	    dmu_tx_get_txg(tx), &dr->dr_bp_copy, dr->dt.dll.dr_abd,
43487877fdebSMatt Macy 	    dn->dn_datablksz, abd_get_size(dr->dt.dll.dr_abd),
43497877fdebSMatt Macy 	    &dr->dt.dll.dr_props, dbuf_lightweight_ready, NULL,
43507877fdebSMatt Macy 	    dbuf_lightweight_physdone, dbuf_lightweight_done, dr,
43517877fdebSMatt Macy 	    ZIO_PRIORITY_ASYNC_WRITE,
43527877fdebSMatt Macy 	    ZIO_FLAG_MUSTSUCCEED | dr->dt.dll.dr_flags, &zb);
43537877fdebSMatt Macy 
43547877fdebSMatt Macy 	zio_nowait(dr->dr_zio);
43557877fdebSMatt Macy }
43567877fdebSMatt Macy 
4357eda14cbcSMatt Macy /*
4358eda14cbcSMatt Macy  * dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
4359eda14cbcSMatt Macy  * critical the we not allow the compiler to inline this function in to
4360eda14cbcSMatt Macy  * dbuf_sync_list() thereby drastically bloating the stack usage.
4361eda14cbcSMatt Macy  */
4362eda14cbcSMatt Macy noinline static void
4363eda14cbcSMatt Macy dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
4364eda14cbcSMatt Macy {
4365eda14cbcSMatt Macy 	arc_buf_t **datap = &dr->dt.dl.dr_data;
4366eda14cbcSMatt Macy 	dmu_buf_impl_t *db = dr->dr_dbuf;
43677877fdebSMatt Macy 	dnode_t *dn = dr->dr_dnode;
4368eda14cbcSMatt Macy 	objset_t *os;
4369eda14cbcSMatt Macy 	uint64_t txg = tx->tx_txg;
4370eda14cbcSMatt Macy 
4371eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
4372eda14cbcSMatt Macy 
4373eda14cbcSMatt Macy 	dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
4374eda14cbcSMatt Macy 
4375eda14cbcSMatt Macy 	mutex_enter(&db->db_mtx);
4376eda14cbcSMatt Macy 	/*
4377eda14cbcSMatt Macy 	 * To be synced, we must be dirtied.  But we
4378eda14cbcSMatt Macy 	 * might have been freed after the dirty.
4379eda14cbcSMatt Macy 	 */
4380eda14cbcSMatt Macy 	if (db->db_state == DB_UNCACHED) {
4381eda14cbcSMatt Macy 		/* This buffer has been freed since it was dirtied */
4382eda14cbcSMatt Macy 		ASSERT(db->db.db_data == NULL);
4383eda14cbcSMatt Macy 	} else if (db->db_state == DB_FILL) {
4384eda14cbcSMatt Macy 		/* This buffer was freed and is now being re-filled */
4385eda14cbcSMatt Macy 		ASSERT(db->db.db_data != dr->dt.dl.dr_data);
4386eda14cbcSMatt Macy 	} else {
4387eda14cbcSMatt Macy 		ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL);
4388eda14cbcSMatt Macy 	}
4389eda14cbcSMatt Macy 	DBUF_VERIFY(db);
4390eda14cbcSMatt Macy 
4391eda14cbcSMatt Macy 	if (db->db_blkid == DMU_SPILL_BLKID) {
4392eda14cbcSMatt Macy 		mutex_enter(&dn->dn_mtx);
4393eda14cbcSMatt Macy 		if (!(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) {
4394eda14cbcSMatt Macy 			/*
4395eda14cbcSMatt Macy 			 * In the previous transaction group, the bonus buffer
4396eda14cbcSMatt Macy 			 * was entirely used to store the attributes for the
4397eda14cbcSMatt Macy 			 * dnode which overrode the dn_spill field.  However,
4398eda14cbcSMatt Macy 			 * when adding more attributes to the file a spill
4399eda14cbcSMatt Macy 			 * block was required to hold the extra attributes.
4400eda14cbcSMatt Macy 			 *
4401eda14cbcSMatt Macy 			 * Make sure to clear the garbage left in the dn_spill
4402eda14cbcSMatt Macy 			 * field from the previous attributes in the bonus
4403eda14cbcSMatt Macy 			 * buffer.  Otherwise, after writing out the spill
4404eda14cbcSMatt Macy 			 * block to the new allocated dva, it will free
4405eda14cbcSMatt Macy 			 * the old block pointed to by the invalid dn_spill.
4406eda14cbcSMatt Macy 			 */
4407eda14cbcSMatt Macy 			db->db_blkptr = NULL;
4408eda14cbcSMatt Macy 		}
4409eda14cbcSMatt Macy 		dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR;
4410eda14cbcSMatt Macy 		mutex_exit(&dn->dn_mtx);
4411eda14cbcSMatt Macy 	}
4412eda14cbcSMatt Macy 
4413eda14cbcSMatt Macy 	/*
4414eda14cbcSMatt Macy 	 * If this is a bonus buffer, simply copy the bonus data into the
4415eda14cbcSMatt Macy 	 * dnode.  It will be written out when the dnode is synced (and it
4416eda14cbcSMatt Macy 	 * will be synced, since it must have been dirty for dbuf_sync to
4417eda14cbcSMatt Macy 	 * be called).
4418eda14cbcSMatt Macy 	 */
4419eda14cbcSMatt Macy 	if (db->db_blkid == DMU_BONUS_BLKID) {
4420eda14cbcSMatt Macy 		ASSERT(dr->dr_dbuf == db);
4421eda14cbcSMatt Macy 		dbuf_sync_bonus(dr, tx);
4422eda14cbcSMatt Macy 		return;
4423eda14cbcSMatt Macy 	}
4424eda14cbcSMatt Macy 
4425eda14cbcSMatt Macy 	os = dn->dn_objset;
4426eda14cbcSMatt Macy 
4427eda14cbcSMatt Macy 	/*
4428eda14cbcSMatt Macy 	 * This function may have dropped the db_mtx lock allowing a dmu_sync
4429eda14cbcSMatt Macy 	 * operation to sneak in. As a result, we need to ensure that we
4430eda14cbcSMatt Macy 	 * don't check the dr_override_state until we have returned from
4431eda14cbcSMatt Macy 	 * dbuf_check_blkptr.
4432eda14cbcSMatt Macy 	 */
4433eda14cbcSMatt Macy 	dbuf_check_blkptr(dn, db);
4434eda14cbcSMatt Macy 
4435eda14cbcSMatt Macy 	/*
4436eda14cbcSMatt Macy 	 * If this buffer is in the middle of an immediate write,
4437eda14cbcSMatt Macy 	 * wait for the synchronous IO to complete.
4438eda14cbcSMatt Macy 	 */
4439eda14cbcSMatt Macy 	while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
4440eda14cbcSMatt Macy 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
4441eda14cbcSMatt Macy 		cv_wait(&db->db_changed, &db->db_mtx);
4442eda14cbcSMatt Macy 		ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN);
4443eda14cbcSMatt Macy 	}
4444eda14cbcSMatt Macy 
4445eda14cbcSMatt Macy 	/*
4446eda14cbcSMatt Macy 	 * If this is a dnode block, ensure it is appropriately encrypted
4447eda14cbcSMatt Macy 	 * or decrypted, depending on what we are writing to it this txg.
4448eda14cbcSMatt Macy 	 */
4449eda14cbcSMatt Macy 	if (os->os_encrypted && dn->dn_object == DMU_META_DNODE_OBJECT)
4450eda14cbcSMatt Macy 		dbuf_prepare_encrypted_dnode_leaf(dr);
4451eda14cbcSMatt Macy 
4452eda14cbcSMatt Macy 	if (db->db_state != DB_NOFILL &&
4453eda14cbcSMatt Macy 	    dn->dn_object != DMU_META_DNODE_OBJECT &&
4454eda14cbcSMatt Macy 	    zfs_refcount_count(&db->db_holds) > 1 &&
4455eda14cbcSMatt Macy 	    dr->dt.dl.dr_override_state != DR_OVERRIDDEN &&
4456eda14cbcSMatt Macy 	    *datap == db->db_buf) {
4457eda14cbcSMatt Macy 		/*
4458eda14cbcSMatt Macy 		 * If this buffer is currently "in use" (i.e., there
4459eda14cbcSMatt Macy 		 * are active holds and db_data still references it),
4460eda14cbcSMatt Macy 		 * then make a copy before we start the write so that
4461eda14cbcSMatt Macy 		 * any modifications from the open txg will not leak
4462eda14cbcSMatt Macy 		 * into this write.
4463eda14cbcSMatt Macy 		 *
4464eda14cbcSMatt Macy 		 * NOTE: this copy does not need to be made for
4465eda14cbcSMatt Macy 		 * objects only modified in the syncing context (e.g.
4466eda14cbcSMatt Macy 		 * DNONE_DNODE blocks).
4467eda14cbcSMatt Macy 		 */
446833b8c039SMartin Matuska 		int psize = arc_buf_size(*datap);
446933b8c039SMartin Matuska 		int lsize = arc_buf_lsize(*datap);
447033b8c039SMartin Matuska 		arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
447133b8c039SMartin Matuska 		enum zio_compress compress_type = arc_get_compression(*datap);
447233b8c039SMartin Matuska 		uint8_t complevel = arc_get_complevel(*datap);
447333b8c039SMartin Matuska 
447433b8c039SMartin Matuska 		if (arc_is_encrypted(*datap)) {
447533b8c039SMartin Matuska 			boolean_t byteorder;
447633b8c039SMartin Matuska 			uint8_t salt[ZIO_DATA_SALT_LEN];
447733b8c039SMartin Matuska 			uint8_t iv[ZIO_DATA_IV_LEN];
447833b8c039SMartin Matuska 			uint8_t mac[ZIO_DATA_MAC_LEN];
447933b8c039SMartin Matuska 
448033b8c039SMartin Matuska 			arc_get_raw_params(*datap, &byteorder, salt, iv, mac);
448133b8c039SMartin Matuska 			*datap = arc_alloc_raw_buf(os->os_spa, db,
448233b8c039SMartin Matuska 			    dmu_objset_id(os), byteorder, salt, iv, mac,
448333b8c039SMartin Matuska 			    dn->dn_type, psize, lsize, compress_type,
448433b8c039SMartin Matuska 			    complevel);
448533b8c039SMartin Matuska 		} else if (compress_type != ZIO_COMPRESS_OFF) {
448633b8c039SMartin Matuska 			ASSERT3U(type, ==, ARC_BUFC_DATA);
448733b8c039SMartin Matuska 			*datap = arc_alloc_compressed_buf(os->os_spa, db,
448833b8c039SMartin Matuska 			    psize, lsize, compress_type, complevel);
448933b8c039SMartin Matuska 		} else {
449033b8c039SMartin Matuska 			*datap = arc_alloc_buf(os->os_spa, db, type, psize);
449133b8c039SMartin Matuska 		}
4492da5137abSMartin Matuska 		memcpy((*datap)->b_data, db->db.db_data, psize);
4493eda14cbcSMatt Macy 	}
4494eda14cbcSMatt Macy 	db->db_data_pending = dr;
4495eda14cbcSMatt Macy 
4496eda14cbcSMatt Macy 	mutex_exit(&db->db_mtx);
4497eda14cbcSMatt Macy 
4498eda14cbcSMatt Macy 	dbuf_write(dr, *datap, tx);
4499eda14cbcSMatt Macy 
4500eda14cbcSMatt Macy 	ASSERT(!list_link_active(&dr->dr_dirty_node));
4501eda14cbcSMatt Macy 	if (dn->dn_object == DMU_META_DNODE_OBJECT) {
4502eda14cbcSMatt Macy 		list_insert_tail(&dn->dn_dirty_records[txg & TXG_MASK], dr);
4503eda14cbcSMatt Macy 	} else {
4504eda14cbcSMatt Macy 		zio_nowait(dr->dr_zio);
4505eda14cbcSMatt Macy 	}
4506eda14cbcSMatt Macy }
4507eda14cbcSMatt Macy 
4508eda14cbcSMatt Macy void
4509eda14cbcSMatt Macy dbuf_sync_list(list_t *list, int level, dmu_tx_t *tx)
4510eda14cbcSMatt Macy {
4511eda14cbcSMatt Macy 	dbuf_dirty_record_t *dr;
4512eda14cbcSMatt Macy 
4513eda14cbcSMatt Macy 	while ((dr = list_head(list))) {
4514eda14cbcSMatt Macy 		if (dr->dr_zio != NULL) {
4515eda14cbcSMatt Macy 			/*
4516eda14cbcSMatt Macy 			 * If we find an already initialized zio then we
4517eda14cbcSMatt Macy 			 * are processing the meta-dnode, and we have finished.
4518eda14cbcSMatt Macy 			 * The dbufs for all dnodes are put back on the list
4519eda14cbcSMatt Macy 			 * during processing, so that we can zio_wait()
4520eda14cbcSMatt Macy 			 * these IOs after initiating all child IOs.
4521eda14cbcSMatt Macy 			 */
4522eda14cbcSMatt Macy 			ASSERT3U(dr->dr_dbuf->db.db_object, ==,
4523eda14cbcSMatt Macy 			    DMU_META_DNODE_OBJECT);
4524eda14cbcSMatt Macy 			break;
4525eda14cbcSMatt Macy 		}
45267877fdebSMatt Macy 		list_remove(list, dr);
45277877fdebSMatt Macy 		if (dr->dr_dbuf == NULL) {
45287877fdebSMatt Macy 			dbuf_sync_lightweight(dr, tx);
45297877fdebSMatt Macy 		} else {
4530eda14cbcSMatt Macy 			if (dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
4531eda14cbcSMatt Macy 			    dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
4532eda14cbcSMatt Macy 				VERIFY3U(dr->dr_dbuf->db_level, ==, level);
4533eda14cbcSMatt Macy 			}
4534eda14cbcSMatt Macy 			if (dr->dr_dbuf->db_level > 0)
4535eda14cbcSMatt Macy 				dbuf_sync_indirect(dr, tx);
4536eda14cbcSMatt Macy 			else
4537eda14cbcSMatt Macy 				dbuf_sync_leaf(dr, tx);
4538eda14cbcSMatt Macy 		}
4539eda14cbcSMatt Macy 	}
45407877fdebSMatt Macy }
4541eda14cbcSMatt Macy 
4542eda14cbcSMatt Macy static void
4543eda14cbcSMatt Macy dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
4544eda14cbcSMatt Macy {
4545e92ffd9bSMartin Matuska 	(void) buf;
4546eda14cbcSMatt Macy 	dmu_buf_impl_t *db = vdb;
4547eda14cbcSMatt Macy 	dnode_t *dn;
4548eda14cbcSMatt Macy 	blkptr_t *bp = zio->io_bp;
4549eda14cbcSMatt Macy 	blkptr_t *bp_orig = &zio->io_bp_orig;
4550eda14cbcSMatt Macy 	spa_t *spa = zio->io_spa;
4551eda14cbcSMatt Macy 	int64_t delta;
4552eda14cbcSMatt Macy 	uint64_t fill = 0;
4553eda14cbcSMatt Macy 	int i;
4554eda14cbcSMatt Macy 
4555eda14cbcSMatt Macy 	ASSERT3P(db->db_blkptr, !=, NULL);
4556eda14cbcSMatt Macy 	ASSERT3P(&db->db_data_pending->dr_bp_copy, ==, bp);
4557eda14cbcSMatt Macy 
4558eda14cbcSMatt Macy 	DB_DNODE_ENTER(db);
4559eda14cbcSMatt Macy 	dn = DB_DNODE(db);
4560eda14cbcSMatt Macy 	delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig);
4561eda14cbcSMatt Macy 	dnode_diduse_space(dn, delta - zio->io_prev_space_delta);
4562eda14cbcSMatt Macy 	zio->io_prev_space_delta = delta;
4563eda14cbcSMatt Macy 
4564eda14cbcSMatt Macy 	if (bp->blk_birth != 0) {
4565eda14cbcSMatt Macy 		ASSERT((db->db_blkid != DMU_SPILL_BLKID &&
4566eda14cbcSMatt Macy 		    BP_GET_TYPE(bp) == dn->dn_type) ||
4567eda14cbcSMatt Macy 		    (db->db_blkid == DMU_SPILL_BLKID &&
4568eda14cbcSMatt Macy 		    BP_GET_TYPE(bp) == dn->dn_bonustype) ||
4569eda14cbcSMatt Macy 		    BP_IS_EMBEDDED(bp));
4570eda14cbcSMatt Macy 		ASSERT(BP_GET_LEVEL(bp) == db->db_level);
4571eda14cbcSMatt Macy 	}
4572eda14cbcSMatt Macy 
4573eda14cbcSMatt Macy 	mutex_enter(&db->db_mtx);
4574eda14cbcSMatt Macy 
4575eda14cbcSMatt Macy #ifdef ZFS_DEBUG
4576eda14cbcSMatt Macy 	if (db->db_blkid == DMU_SPILL_BLKID) {
4577eda14cbcSMatt Macy 		ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
4578eda14cbcSMatt Macy 		ASSERT(!(BP_IS_HOLE(bp)) &&
4579eda14cbcSMatt Macy 		    db->db_blkptr == DN_SPILL_BLKPTR(dn->dn_phys));
4580eda14cbcSMatt Macy 	}
4581eda14cbcSMatt Macy #endif
4582eda14cbcSMatt Macy 
4583eda14cbcSMatt Macy 	if (db->db_level == 0) {
4584eda14cbcSMatt Macy 		mutex_enter(&dn->dn_mtx);
4585eda14cbcSMatt Macy 		if (db->db_blkid > dn->dn_phys->dn_maxblkid &&
4586eda14cbcSMatt Macy 		    db->db_blkid != DMU_SPILL_BLKID) {
4587eda14cbcSMatt Macy 			ASSERT0(db->db_objset->os_raw_receive);
4588eda14cbcSMatt Macy 			dn->dn_phys->dn_maxblkid = db->db_blkid;
4589eda14cbcSMatt Macy 		}
4590eda14cbcSMatt Macy 		mutex_exit(&dn->dn_mtx);
4591eda14cbcSMatt Macy 
4592eda14cbcSMatt Macy 		if (dn->dn_type == DMU_OT_DNODE) {
4593eda14cbcSMatt Macy 			i = 0;
4594eda14cbcSMatt Macy 			while (i < db->db.db_size) {
4595eda14cbcSMatt Macy 				dnode_phys_t *dnp =
4596eda14cbcSMatt Macy 				    (void *)(((char *)db->db.db_data) + i);
4597eda14cbcSMatt Macy 
4598eda14cbcSMatt Macy 				i += DNODE_MIN_SIZE;
4599eda14cbcSMatt Macy 				if (dnp->dn_type != DMU_OT_NONE) {
4600eda14cbcSMatt Macy 					fill++;
4601eda14cbcSMatt Macy 					i += dnp->dn_extra_slots *
4602eda14cbcSMatt Macy 					    DNODE_MIN_SIZE;
4603eda14cbcSMatt Macy 				}
4604eda14cbcSMatt Macy 			}
4605eda14cbcSMatt Macy 		} else {
4606eda14cbcSMatt Macy 			if (BP_IS_HOLE(bp)) {
4607eda14cbcSMatt Macy 				fill = 0;
4608eda14cbcSMatt Macy 			} else {
4609eda14cbcSMatt Macy 				fill = 1;
4610eda14cbcSMatt Macy 			}
4611eda14cbcSMatt Macy 		}
4612eda14cbcSMatt Macy 	} else {
4613eda14cbcSMatt Macy 		blkptr_t *ibp = db->db.db_data;
4614eda14cbcSMatt Macy 		ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
4615eda14cbcSMatt Macy 		for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) {
4616eda14cbcSMatt Macy 			if (BP_IS_HOLE(ibp))
4617eda14cbcSMatt Macy 				continue;
4618eda14cbcSMatt Macy 			fill += BP_GET_FILL(ibp);
4619eda14cbcSMatt Macy 		}
4620eda14cbcSMatt Macy 	}
4621eda14cbcSMatt Macy 	DB_DNODE_EXIT(db);
4622eda14cbcSMatt Macy 
4623eda14cbcSMatt Macy 	if (!BP_IS_EMBEDDED(bp))
4624eda14cbcSMatt Macy 		BP_SET_FILL(bp, fill);
4625eda14cbcSMatt Macy 
4626eda14cbcSMatt Macy 	mutex_exit(&db->db_mtx);
4627eda14cbcSMatt Macy 
4628eda14cbcSMatt Macy 	db_lock_type_t dblt = dmu_buf_lock_parent(db, RW_WRITER, FTAG);
4629eda14cbcSMatt Macy 	*db->db_blkptr = *bp;
4630eda14cbcSMatt Macy 	dmu_buf_unlock_parent(db, dblt, FTAG);
4631eda14cbcSMatt Macy }
4632eda14cbcSMatt Macy 
4633eda14cbcSMatt Macy /*
4634eda14cbcSMatt Macy  * This function gets called just prior to running through the compression
4635eda14cbcSMatt Macy  * stage of the zio pipeline. If we're an indirect block comprised of only
4636eda14cbcSMatt Macy  * holes, then we want this indirect to be compressed away to a hole. In
4637eda14cbcSMatt Macy  * order to do that we must zero out any information about the holes that
4638eda14cbcSMatt Macy  * this indirect points to prior to before we try to compress it.
4639eda14cbcSMatt Macy  */
4640eda14cbcSMatt Macy static void
4641eda14cbcSMatt Macy dbuf_write_children_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
4642eda14cbcSMatt Macy {
4643e92ffd9bSMartin Matuska 	(void) zio, (void) buf;
4644eda14cbcSMatt Macy 	dmu_buf_impl_t *db = vdb;
4645eda14cbcSMatt Macy 	dnode_t *dn;
4646eda14cbcSMatt Macy 	blkptr_t *bp;
4647eda14cbcSMatt Macy 	unsigned int epbs, i;
4648eda14cbcSMatt Macy 
4649eda14cbcSMatt Macy 	ASSERT3U(db->db_level, >, 0);
4650eda14cbcSMatt Macy 	DB_DNODE_ENTER(db);
4651eda14cbcSMatt Macy 	dn = DB_DNODE(db);
4652eda14cbcSMatt Macy 	epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
4653eda14cbcSMatt Macy 	ASSERT3U(epbs, <, 31);
4654eda14cbcSMatt Macy 
4655eda14cbcSMatt Macy 	/* Determine if all our children are holes */
4656eda14cbcSMatt Macy 	for (i = 0, bp = db->db.db_data; i < 1ULL << epbs; i++, bp++) {
4657eda14cbcSMatt Macy 		if (!BP_IS_HOLE(bp))
4658eda14cbcSMatt Macy 			break;
4659eda14cbcSMatt Macy 	}
4660eda14cbcSMatt Macy 
4661eda14cbcSMatt Macy 	/*
4662eda14cbcSMatt Macy 	 * If all the children are holes, then zero them all out so that
4663eda14cbcSMatt Macy 	 * we may get compressed away.
4664eda14cbcSMatt Macy 	 */
4665eda14cbcSMatt Macy 	if (i == 1ULL << epbs) {
4666eda14cbcSMatt Macy 		/*
4667eda14cbcSMatt Macy 		 * We only found holes. Grab the rwlock to prevent
4668eda14cbcSMatt Macy 		 * anybody from reading the blocks we're about to
4669eda14cbcSMatt Macy 		 * zero out.
4670eda14cbcSMatt Macy 		 */
4671eda14cbcSMatt Macy 		rw_enter(&db->db_rwlock, RW_WRITER);
4672da5137abSMartin Matuska 		memset(db->db.db_data, 0, db->db.db_size);
4673eda14cbcSMatt Macy 		rw_exit(&db->db_rwlock);
4674eda14cbcSMatt Macy 	}
4675eda14cbcSMatt Macy 	DB_DNODE_EXIT(db);
4676eda14cbcSMatt Macy }
4677eda14cbcSMatt Macy 
4678eda14cbcSMatt Macy /*
4679eda14cbcSMatt Macy  * The SPA will call this callback several times for each zio - once
4680eda14cbcSMatt Macy  * for every physical child i/o (zio->io_phys_children times).  This
4681eda14cbcSMatt Macy  * allows the DMU to monitor the progress of each logical i/o.  For example,
4682eda14cbcSMatt Macy  * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
4683eda14cbcSMatt Macy  * block.  There may be a long delay before all copies/fragments are completed,
4684eda14cbcSMatt Macy  * so this callback allows us to retire dirty space gradually, as the physical
4685eda14cbcSMatt Macy  * i/os complete.
4686eda14cbcSMatt Macy  */
4687eda14cbcSMatt Macy static void
4688eda14cbcSMatt Macy dbuf_write_physdone(zio_t *zio, arc_buf_t *buf, void *arg)
4689eda14cbcSMatt Macy {
4690e92ffd9bSMartin Matuska 	(void) buf;
4691eda14cbcSMatt Macy 	dmu_buf_impl_t *db = arg;
4692eda14cbcSMatt Macy 	objset_t *os = db->db_objset;
4693eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_objset_pool(os);
4694eda14cbcSMatt Macy 	dbuf_dirty_record_t *dr;
4695eda14cbcSMatt Macy 	int delta = 0;
4696eda14cbcSMatt Macy 
4697eda14cbcSMatt Macy 	dr = db->db_data_pending;
4698eda14cbcSMatt Macy 	ASSERT3U(dr->dr_txg, ==, zio->io_txg);
4699eda14cbcSMatt Macy 
4700eda14cbcSMatt Macy 	/*
4701eda14cbcSMatt Macy 	 * The callback will be called io_phys_children times.  Retire one
4702eda14cbcSMatt Macy 	 * portion of our dirty space each time we are called.  Any rounding
4703eda14cbcSMatt Macy 	 * error will be cleaned up by dbuf_write_done().
4704eda14cbcSMatt Macy 	 */
4705eda14cbcSMatt Macy 	delta = dr->dr_accounted / zio->io_phys_children;
4706eda14cbcSMatt Macy 	dsl_pool_undirty_space(dp, delta, zio->io_txg);
4707eda14cbcSMatt Macy }
4708eda14cbcSMatt Macy 
4709eda14cbcSMatt Macy static void
4710eda14cbcSMatt Macy dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
4711eda14cbcSMatt Macy {
4712e92ffd9bSMartin Matuska 	(void) buf;
4713eda14cbcSMatt Macy 	dmu_buf_impl_t *db = vdb;
4714eda14cbcSMatt Macy 	blkptr_t *bp_orig = &zio->io_bp_orig;
4715eda14cbcSMatt Macy 	blkptr_t *bp = db->db_blkptr;
4716eda14cbcSMatt Macy 	objset_t *os = db->db_objset;
4717eda14cbcSMatt Macy 	dmu_tx_t *tx = os->os_synctx;
4718eda14cbcSMatt Macy 
4719eda14cbcSMatt Macy 	ASSERT0(zio->io_error);
4720eda14cbcSMatt Macy 	ASSERT(db->db_blkptr == bp);
4721eda14cbcSMatt Macy 
4722eda14cbcSMatt Macy 	/*
4723eda14cbcSMatt Macy 	 * For nopwrites and rewrites we ensure that the bp matches our
4724eda14cbcSMatt Macy 	 * original and bypass all the accounting.
4725eda14cbcSMatt Macy 	 */
4726eda14cbcSMatt Macy 	if (zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)) {
4727eda14cbcSMatt Macy 		ASSERT(BP_EQUAL(bp, bp_orig));
4728eda14cbcSMatt Macy 	} else {
4729eda14cbcSMatt Macy 		dsl_dataset_t *ds = os->os_dsl_dataset;
4730eda14cbcSMatt Macy 		(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
4731eda14cbcSMatt Macy 		dsl_dataset_block_born(ds, bp, tx);
4732eda14cbcSMatt Macy 	}
4733eda14cbcSMatt Macy 
4734eda14cbcSMatt Macy 	mutex_enter(&db->db_mtx);
4735eda14cbcSMatt Macy 
4736eda14cbcSMatt Macy 	DBUF_VERIFY(db);
4737eda14cbcSMatt Macy 
47387877fdebSMatt Macy 	dbuf_dirty_record_t *dr = db->db_data_pending;
47397877fdebSMatt Macy 	dnode_t *dn = dr->dr_dnode;
4740eda14cbcSMatt Macy 	ASSERT(!list_link_active(&dr->dr_dirty_node));
4741eda14cbcSMatt Macy 	ASSERT(dr->dr_dbuf == db);
4742eda14cbcSMatt Macy 	ASSERT(list_next(&db->db_dirty_records, dr) == NULL);
4743eda14cbcSMatt Macy 	list_remove(&db->db_dirty_records, dr);
4744eda14cbcSMatt Macy 
4745eda14cbcSMatt Macy #ifdef ZFS_DEBUG
4746eda14cbcSMatt Macy 	if (db->db_blkid == DMU_SPILL_BLKID) {
4747eda14cbcSMatt Macy 		ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
4748eda14cbcSMatt Macy 		ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
4749eda14cbcSMatt Macy 		    db->db_blkptr == DN_SPILL_BLKPTR(dn->dn_phys));
4750eda14cbcSMatt Macy 	}
4751eda14cbcSMatt Macy #endif
4752eda14cbcSMatt Macy 
4753eda14cbcSMatt Macy 	if (db->db_level == 0) {
4754eda14cbcSMatt Macy 		ASSERT(db->db_blkid != DMU_BONUS_BLKID);
4755eda14cbcSMatt Macy 		ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
4756eda14cbcSMatt Macy 		if (db->db_state != DB_NOFILL) {
4757eda14cbcSMatt Macy 			if (dr->dt.dl.dr_data != db->db_buf)
4758eda14cbcSMatt Macy 				arc_buf_destroy(dr->dt.dl.dr_data, db);
4759eda14cbcSMatt Macy 		}
4760eda14cbcSMatt Macy 	} else {
4761eda14cbcSMatt Macy 		ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
4762eda14cbcSMatt Macy 		ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
4763eda14cbcSMatt Macy 		if (!BP_IS_HOLE(db->db_blkptr)) {
4764eda14cbcSMatt Macy 			int epbs __maybe_unused = dn->dn_phys->dn_indblkshift -
4765eda14cbcSMatt Macy 			    SPA_BLKPTRSHIFT;
4766eda14cbcSMatt Macy 			ASSERT3U(db->db_blkid, <=,
4767eda14cbcSMatt Macy 			    dn->dn_phys->dn_maxblkid >> (db->db_level * epbs));
4768eda14cbcSMatt Macy 			ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
4769eda14cbcSMatt Macy 			    db->db.db_size);
4770eda14cbcSMatt Macy 		}
4771eda14cbcSMatt Macy 		mutex_destroy(&dr->dt.di.dr_mtx);
4772eda14cbcSMatt Macy 		list_destroy(&dr->dt.di.dr_children);
4773eda14cbcSMatt Macy 	}
4774eda14cbcSMatt Macy 
4775eda14cbcSMatt Macy 	cv_broadcast(&db->db_changed);
4776eda14cbcSMatt Macy 	ASSERT(db->db_dirtycnt > 0);
4777eda14cbcSMatt Macy 	db->db_dirtycnt -= 1;
4778eda14cbcSMatt Macy 	db->db_data_pending = NULL;
4779eda14cbcSMatt Macy 	dbuf_rele_and_unlock(db, (void *)(uintptr_t)tx->tx_txg, B_FALSE);
4780eda14cbcSMatt Macy 
4781eda14cbcSMatt Macy 	/*
4782eda14cbcSMatt Macy 	 * If we didn't do a physical write in this ZIO and we
4783eda14cbcSMatt Macy 	 * still ended up here, it means that the space of the
4784eda14cbcSMatt Macy 	 * dbuf that we just released (and undirtied) above hasn't
4785eda14cbcSMatt Macy 	 * been marked as undirtied in the pool's accounting.
4786eda14cbcSMatt Macy 	 *
4787eda14cbcSMatt Macy 	 * Thus, we undirty that space in the pool's view of the
4788eda14cbcSMatt Macy 	 * world here. For physical writes this type of update
4789eda14cbcSMatt Macy 	 * happens in dbuf_write_physdone().
4790eda14cbcSMatt Macy 	 *
4791eda14cbcSMatt Macy 	 * If we did a physical write, cleanup any rounding errors
4792eda14cbcSMatt Macy 	 * that came up due to writing multiple copies of a block
4793eda14cbcSMatt Macy 	 * on disk [see dbuf_write_physdone()].
4794eda14cbcSMatt Macy 	 */
4795eda14cbcSMatt Macy 	if (zio->io_phys_children == 0) {
4796eda14cbcSMatt Macy 		dsl_pool_undirty_space(dmu_objset_pool(os),
4797eda14cbcSMatt Macy 		    dr->dr_accounted, zio->io_txg);
4798eda14cbcSMatt Macy 	} else {
4799eda14cbcSMatt Macy 		dsl_pool_undirty_space(dmu_objset_pool(os),
4800eda14cbcSMatt Macy 		    dr->dr_accounted % zio->io_phys_children, zio->io_txg);
4801eda14cbcSMatt Macy 	}
4802eda14cbcSMatt Macy 
4803eda14cbcSMatt Macy 	kmem_free(dr, sizeof (dbuf_dirty_record_t));
4804eda14cbcSMatt Macy }
4805eda14cbcSMatt Macy 
4806eda14cbcSMatt Macy static void
4807eda14cbcSMatt Macy dbuf_write_nofill_ready(zio_t *zio)
4808eda14cbcSMatt Macy {
4809eda14cbcSMatt Macy 	dbuf_write_ready(zio, NULL, zio->io_private);
4810eda14cbcSMatt Macy }
4811eda14cbcSMatt Macy 
4812eda14cbcSMatt Macy static void
4813eda14cbcSMatt Macy dbuf_write_nofill_done(zio_t *zio)
4814eda14cbcSMatt Macy {
4815eda14cbcSMatt Macy 	dbuf_write_done(zio, NULL, zio->io_private);
4816eda14cbcSMatt Macy }
4817eda14cbcSMatt Macy 
4818eda14cbcSMatt Macy static void
4819eda14cbcSMatt Macy dbuf_write_override_ready(zio_t *zio)
4820eda14cbcSMatt Macy {
4821eda14cbcSMatt Macy 	dbuf_dirty_record_t *dr = zio->io_private;
4822eda14cbcSMatt Macy 	dmu_buf_impl_t *db = dr->dr_dbuf;
4823eda14cbcSMatt Macy 
4824eda14cbcSMatt Macy 	dbuf_write_ready(zio, NULL, db);
4825eda14cbcSMatt Macy }
4826eda14cbcSMatt Macy 
4827eda14cbcSMatt Macy static void
4828eda14cbcSMatt Macy dbuf_write_override_done(zio_t *zio)
4829eda14cbcSMatt Macy {
4830eda14cbcSMatt Macy 	dbuf_dirty_record_t *dr = zio->io_private;
4831eda14cbcSMatt Macy 	dmu_buf_impl_t *db = dr->dr_dbuf;
4832eda14cbcSMatt Macy 	blkptr_t *obp = &dr->dt.dl.dr_overridden_by;
4833eda14cbcSMatt Macy 
4834eda14cbcSMatt Macy 	mutex_enter(&db->db_mtx);
4835eda14cbcSMatt Macy 	if (!BP_EQUAL(zio->io_bp, obp)) {
4836eda14cbcSMatt Macy 		if (!BP_IS_HOLE(obp))
4837eda14cbcSMatt Macy 			dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp);
4838eda14cbcSMatt Macy 		arc_release(dr->dt.dl.dr_data, db);
4839eda14cbcSMatt Macy 	}
4840eda14cbcSMatt Macy 	mutex_exit(&db->db_mtx);
4841eda14cbcSMatt Macy 
4842eda14cbcSMatt Macy 	dbuf_write_done(zio, NULL, db);
4843eda14cbcSMatt Macy 
4844eda14cbcSMatt Macy 	if (zio->io_abd != NULL)
4845184c1b94SMartin Matuska 		abd_free(zio->io_abd);
4846eda14cbcSMatt Macy }
4847eda14cbcSMatt Macy 
4848eda14cbcSMatt Macy typedef struct dbuf_remap_impl_callback_arg {
4849eda14cbcSMatt Macy 	objset_t	*drica_os;
4850eda14cbcSMatt Macy 	uint64_t	drica_blk_birth;
4851eda14cbcSMatt Macy 	dmu_tx_t	*drica_tx;
4852eda14cbcSMatt Macy } dbuf_remap_impl_callback_arg_t;
4853eda14cbcSMatt Macy 
4854eda14cbcSMatt Macy static void
4855eda14cbcSMatt Macy dbuf_remap_impl_callback(uint64_t vdev, uint64_t offset, uint64_t size,
4856eda14cbcSMatt Macy     void *arg)
4857eda14cbcSMatt Macy {
4858eda14cbcSMatt Macy 	dbuf_remap_impl_callback_arg_t *drica = arg;
4859eda14cbcSMatt Macy 	objset_t *os = drica->drica_os;
4860eda14cbcSMatt Macy 	spa_t *spa = dmu_objset_spa(os);
4861eda14cbcSMatt Macy 	dmu_tx_t *tx = drica->drica_tx;
4862eda14cbcSMatt Macy 
4863eda14cbcSMatt Macy 	ASSERT(dsl_pool_sync_context(spa_get_dsl(spa)));
4864eda14cbcSMatt Macy 
4865eda14cbcSMatt Macy 	if (os == spa_meta_objset(spa)) {
4866eda14cbcSMatt Macy 		spa_vdev_indirect_mark_obsolete(spa, vdev, offset, size, tx);
4867eda14cbcSMatt Macy 	} else {
4868eda14cbcSMatt Macy 		dsl_dataset_block_remapped(dmu_objset_ds(os), vdev, offset,
4869eda14cbcSMatt Macy 		    size, drica->drica_blk_birth, tx);
4870eda14cbcSMatt Macy 	}
4871eda14cbcSMatt Macy }
4872eda14cbcSMatt Macy 
4873eda14cbcSMatt Macy static void
4874eda14cbcSMatt Macy dbuf_remap_impl(dnode_t *dn, blkptr_t *bp, krwlock_t *rw, dmu_tx_t *tx)
4875eda14cbcSMatt Macy {
4876eda14cbcSMatt Macy 	blkptr_t bp_copy = *bp;
4877eda14cbcSMatt Macy 	spa_t *spa = dmu_objset_spa(dn->dn_objset);
4878eda14cbcSMatt Macy 	dbuf_remap_impl_callback_arg_t drica;
4879eda14cbcSMatt Macy 
4880eda14cbcSMatt Macy 	ASSERT(dsl_pool_sync_context(spa_get_dsl(spa)));
4881eda14cbcSMatt Macy 
4882eda14cbcSMatt Macy 	drica.drica_os = dn->dn_objset;
4883eda14cbcSMatt Macy 	drica.drica_blk_birth = bp->blk_birth;
4884eda14cbcSMatt Macy 	drica.drica_tx = tx;
4885eda14cbcSMatt Macy 	if (spa_remap_blkptr(spa, &bp_copy, dbuf_remap_impl_callback,
4886eda14cbcSMatt Macy 	    &drica)) {
4887eda14cbcSMatt Macy 		/*
4888eda14cbcSMatt Macy 		 * If the blkptr being remapped is tracked by a livelist,
4889eda14cbcSMatt Macy 		 * then we need to make sure the livelist reflects the update.
4890eda14cbcSMatt Macy 		 * First, cancel out the old blkptr by appending a 'FREE'
4891eda14cbcSMatt Macy 		 * entry. Next, add an 'ALLOC' to track the new version. This
4892eda14cbcSMatt Macy 		 * way we avoid trying to free an inaccurate blkptr at delete.
4893eda14cbcSMatt Macy 		 * Note that embedded blkptrs are not tracked in livelists.
4894eda14cbcSMatt Macy 		 */
4895eda14cbcSMatt Macy 		if (dn->dn_objset != spa_meta_objset(spa)) {
4896eda14cbcSMatt Macy 			dsl_dataset_t *ds = dmu_objset_ds(dn->dn_objset);
4897eda14cbcSMatt Macy 			if (dsl_deadlist_is_open(&ds->ds_dir->dd_livelist) &&
4898eda14cbcSMatt Macy 			    bp->blk_birth > ds->ds_dir->dd_origin_txg) {
4899eda14cbcSMatt Macy 				ASSERT(!BP_IS_EMBEDDED(bp));
4900eda14cbcSMatt Macy 				ASSERT(dsl_dir_is_clone(ds->ds_dir));
4901eda14cbcSMatt Macy 				ASSERT(spa_feature_is_enabled(spa,
4902eda14cbcSMatt Macy 				    SPA_FEATURE_LIVELIST));
4903eda14cbcSMatt Macy 				bplist_append(&ds->ds_dir->dd_pending_frees,
4904eda14cbcSMatt Macy 				    bp);
4905eda14cbcSMatt Macy 				bplist_append(&ds->ds_dir->dd_pending_allocs,
4906eda14cbcSMatt Macy 				    &bp_copy);
4907eda14cbcSMatt Macy 			}
4908eda14cbcSMatt Macy 		}
4909eda14cbcSMatt Macy 
4910eda14cbcSMatt Macy 		/*
4911eda14cbcSMatt Macy 		 * The db_rwlock prevents dbuf_read_impl() from
4912eda14cbcSMatt Macy 		 * dereferencing the BP while we are changing it.  To
4913eda14cbcSMatt Macy 		 * avoid lock contention, only grab it when we are actually
4914eda14cbcSMatt Macy 		 * changing the BP.
4915eda14cbcSMatt Macy 		 */
4916eda14cbcSMatt Macy 		if (rw != NULL)
4917eda14cbcSMatt Macy 			rw_enter(rw, RW_WRITER);
4918eda14cbcSMatt Macy 		*bp = bp_copy;
4919eda14cbcSMatt Macy 		if (rw != NULL)
4920eda14cbcSMatt Macy 			rw_exit(rw);
4921eda14cbcSMatt Macy 	}
4922eda14cbcSMatt Macy }
4923eda14cbcSMatt Macy 
4924eda14cbcSMatt Macy /*
4925eda14cbcSMatt Macy  * Remap any existing BP's to concrete vdevs, if possible.
4926eda14cbcSMatt Macy  */
4927eda14cbcSMatt Macy static void
4928eda14cbcSMatt Macy dbuf_remap(dnode_t *dn, dmu_buf_impl_t *db, dmu_tx_t *tx)
4929eda14cbcSMatt Macy {
4930eda14cbcSMatt Macy 	spa_t *spa = dmu_objset_spa(db->db_objset);
4931eda14cbcSMatt Macy 	ASSERT(dsl_pool_sync_context(spa_get_dsl(spa)));
4932eda14cbcSMatt Macy 
4933eda14cbcSMatt Macy 	if (!spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL))
4934eda14cbcSMatt Macy 		return;
4935eda14cbcSMatt Macy 
4936eda14cbcSMatt Macy 	if (db->db_level > 0) {
4937eda14cbcSMatt Macy 		blkptr_t *bp = db->db.db_data;
4938eda14cbcSMatt Macy 		for (int i = 0; i < db->db.db_size >> SPA_BLKPTRSHIFT; i++) {
4939eda14cbcSMatt Macy 			dbuf_remap_impl(dn, &bp[i], &db->db_rwlock, tx);
4940eda14cbcSMatt Macy 		}
4941eda14cbcSMatt Macy 	} else if (db->db.db_object == DMU_META_DNODE_OBJECT) {
4942eda14cbcSMatt Macy 		dnode_phys_t *dnp = db->db.db_data;
4943eda14cbcSMatt Macy 		ASSERT3U(db->db_dnode_handle->dnh_dnode->dn_type, ==,
4944eda14cbcSMatt Macy 		    DMU_OT_DNODE);
4945eda14cbcSMatt Macy 		for (int i = 0; i < db->db.db_size >> DNODE_SHIFT;
4946eda14cbcSMatt Macy 		    i += dnp[i].dn_extra_slots + 1) {
4947eda14cbcSMatt Macy 			for (int j = 0; j < dnp[i].dn_nblkptr; j++) {
4948eda14cbcSMatt Macy 				krwlock_t *lock = (dn->dn_dbuf == NULL ? NULL :
4949eda14cbcSMatt Macy 				    &dn->dn_dbuf->db_rwlock);
4950eda14cbcSMatt Macy 				dbuf_remap_impl(dn, &dnp[i].dn_blkptr[j], lock,
4951eda14cbcSMatt Macy 				    tx);
4952eda14cbcSMatt Macy 			}
4953eda14cbcSMatt Macy 		}
4954eda14cbcSMatt Macy 	}
4955eda14cbcSMatt Macy }
4956eda14cbcSMatt Macy 
4957eda14cbcSMatt Macy 
4958eda14cbcSMatt Macy /* Issue I/O to commit a dirty buffer to disk. */
4959eda14cbcSMatt Macy static void
4960eda14cbcSMatt Macy dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx)
4961eda14cbcSMatt Macy {
4962eda14cbcSMatt Macy 	dmu_buf_impl_t *db = dr->dr_dbuf;
49637877fdebSMatt Macy 	dnode_t *dn = dr->dr_dnode;
4964eda14cbcSMatt Macy 	objset_t *os;
4965eda14cbcSMatt Macy 	dmu_buf_impl_t *parent = db->db_parent;
4966eda14cbcSMatt Macy 	uint64_t txg = tx->tx_txg;
4967eda14cbcSMatt Macy 	zbookmark_phys_t zb;
4968eda14cbcSMatt Macy 	zio_prop_t zp;
4969eda14cbcSMatt Macy 	zio_t *pio; /* parent I/O */
4970eda14cbcSMatt Macy 	int wp_flag = 0;
4971eda14cbcSMatt Macy 
4972eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
4973eda14cbcSMatt Macy 
4974eda14cbcSMatt Macy 	os = dn->dn_objset;
4975eda14cbcSMatt Macy 
4976eda14cbcSMatt Macy 	if (db->db_state != DB_NOFILL) {
4977eda14cbcSMatt Macy 		if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) {
4978eda14cbcSMatt Macy 			/*
4979eda14cbcSMatt Macy 			 * Private object buffers are released here rather
4980eda14cbcSMatt Macy 			 * than in dbuf_dirty() since they are only modified
4981eda14cbcSMatt Macy 			 * in the syncing context and we don't want the
4982eda14cbcSMatt Macy 			 * overhead of making multiple copies of the data.
4983eda14cbcSMatt Macy 			 */
4984eda14cbcSMatt Macy 			if (BP_IS_HOLE(db->db_blkptr)) {
4985eda14cbcSMatt Macy 				arc_buf_thaw(data);
4986eda14cbcSMatt Macy 			} else {
4987eda14cbcSMatt Macy 				dbuf_release_bp(db);
4988eda14cbcSMatt Macy 			}
4989eda14cbcSMatt Macy 			dbuf_remap(dn, db, tx);
4990eda14cbcSMatt Macy 		}
4991eda14cbcSMatt Macy 	}
4992eda14cbcSMatt Macy 
4993eda14cbcSMatt Macy 	if (parent != dn->dn_dbuf) {
4994eda14cbcSMatt Macy 		/* Our parent is an indirect block. */
4995eda14cbcSMatt Macy 		/* We have a dirty parent that has been scheduled for write. */
4996eda14cbcSMatt Macy 		ASSERT(parent && parent->db_data_pending);
4997eda14cbcSMatt Macy 		/* Our parent's buffer is one level closer to the dnode. */
4998eda14cbcSMatt Macy 		ASSERT(db->db_level == parent->db_level-1);
4999eda14cbcSMatt Macy 		/*
5000eda14cbcSMatt Macy 		 * We're about to modify our parent's db_data by modifying
5001eda14cbcSMatt Macy 		 * our block pointer, so the parent must be released.
5002eda14cbcSMatt Macy 		 */
5003eda14cbcSMatt Macy 		ASSERT(arc_released(parent->db_buf));
5004eda14cbcSMatt Macy 		pio = parent->db_data_pending->dr_zio;
5005eda14cbcSMatt Macy 	} else {
5006eda14cbcSMatt Macy 		/* Our parent is the dnode itself. */
5007eda14cbcSMatt Macy 		ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 &&
5008eda14cbcSMatt Macy 		    db->db_blkid != DMU_SPILL_BLKID) ||
5009eda14cbcSMatt Macy 		    (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0));
5010eda14cbcSMatt Macy 		if (db->db_blkid != DMU_SPILL_BLKID)
5011eda14cbcSMatt Macy 			ASSERT3P(db->db_blkptr, ==,
5012eda14cbcSMatt Macy 			    &dn->dn_phys->dn_blkptr[db->db_blkid]);
5013eda14cbcSMatt Macy 		pio = dn->dn_zio;
5014eda14cbcSMatt Macy 	}
5015eda14cbcSMatt Macy 
5016eda14cbcSMatt Macy 	ASSERT(db->db_level == 0 || data == db->db_buf);
5017eda14cbcSMatt Macy 	ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
5018eda14cbcSMatt Macy 	ASSERT(pio);
5019eda14cbcSMatt Macy 
5020eda14cbcSMatt Macy 	SET_BOOKMARK(&zb, os->os_dsl_dataset ?
5021eda14cbcSMatt Macy 	    os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
5022eda14cbcSMatt Macy 	    db->db.db_object, db->db_level, db->db_blkid);
5023eda14cbcSMatt Macy 
5024eda14cbcSMatt Macy 	if (db->db_blkid == DMU_SPILL_BLKID)
5025eda14cbcSMatt Macy 		wp_flag = WP_SPILL;
5026eda14cbcSMatt Macy 	wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
5027eda14cbcSMatt Macy 
5028eda14cbcSMatt Macy 	dmu_write_policy(os, dn, db->db_level, wp_flag, &zp);
5029eda14cbcSMatt Macy 
5030eda14cbcSMatt Macy 	/*
5031eda14cbcSMatt Macy 	 * We copy the blkptr now (rather than when we instantiate the dirty
5032eda14cbcSMatt Macy 	 * record), because its value can change between open context and
5033eda14cbcSMatt Macy 	 * syncing context. We do not need to hold dn_struct_rwlock to read
5034eda14cbcSMatt Macy 	 * db_blkptr because we are in syncing context.
5035eda14cbcSMatt Macy 	 */
5036eda14cbcSMatt Macy 	dr->dr_bp_copy = *db->db_blkptr;
5037eda14cbcSMatt Macy 
5038eda14cbcSMatt Macy 	if (db->db_level == 0 &&
5039eda14cbcSMatt Macy 	    dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
5040eda14cbcSMatt Macy 		/*
5041eda14cbcSMatt Macy 		 * The BP for this block has been provided by open context
5042eda14cbcSMatt Macy 		 * (by dmu_sync() or dmu_buf_write_embedded()).
5043eda14cbcSMatt Macy 		 */
5044eda14cbcSMatt Macy 		abd_t *contents = (data != NULL) ?
5045eda14cbcSMatt Macy 		    abd_get_from_buf(data->b_data, arc_buf_size(data)) : NULL;
5046eda14cbcSMatt Macy 
5047eda14cbcSMatt Macy 		dr->dr_zio = zio_write(pio, os->os_spa, txg, &dr->dr_bp_copy,
5048eda14cbcSMatt Macy 		    contents, db->db.db_size, db->db.db_size, &zp,
5049eda14cbcSMatt Macy 		    dbuf_write_override_ready, NULL, NULL,
5050eda14cbcSMatt Macy 		    dbuf_write_override_done,
5051eda14cbcSMatt Macy 		    dr, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
5052eda14cbcSMatt Macy 		mutex_enter(&db->db_mtx);
5053eda14cbcSMatt Macy 		dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
5054eda14cbcSMatt Macy 		zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by,
5055eda14cbcSMatt Macy 		    dr->dt.dl.dr_copies, dr->dt.dl.dr_nopwrite);
5056eda14cbcSMatt Macy 		mutex_exit(&db->db_mtx);
5057eda14cbcSMatt Macy 	} else if (db->db_state == DB_NOFILL) {
5058eda14cbcSMatt Macy 		ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF ||
5059eda14cbcSMatt Macy 		    zp.zp_checksum == ZIO_CHECKSUM_NOPARITY);
5060eda14cbcSMatt Macy 		dr->dr_zio = zio_write(pio, os->os_spa, txg,
5061eda14cbcSMatt Macy 		    &dr->dr_bp_copy, NULL, db->db.db_size, db->db.db_size, &zp,
5062eda14cbcSMatt Macy 		    dbuf_write_nofill_ready, NULL, NULL,
5063eda14cbcSMatt Macy 		    dbuf_write_nofill_done, db,
5064eda14cbcSMatt Macy 		    ZIO_PRIORITY_ASYNC_WRITE,
5065eda14cbcSMatt Macy 		    ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb);
5066eda14cbcSMatt Macy 	} else {
5067eda14cbcSMatt Macy 		ASSERT(arc_released(data));
5068eda14cbcSMatt Macy 
5069eda14cbcSMatt Macy 		/*
5070eda14cbcSMatt Macy 		 * For indirect blocks, we want to setup the children
5071eda14cbcSMatt Macy 		 * ready callback so that we can properly handle an indirect
5072eda14cbcSMatt Macy 		 * block that only contains holes.
5073eda14cbcSMatt Macy 		 */
5074eda14cbcSMatt Macy 		arc_write_done_func_t *children_ready_cb = NULL;
5075eda14cbcSMatt Macy 		if (db->db_level != 0)
5076eda14cbcSMatt Macy 			children_ready_cb = dbuf_write_children_ready;
5077eda14cbcSMatt Macy 
5078eda14cbcSMatt Macy 		dr->dr_zio = arc_write(pio, os->os_spa, txg,
5079dae17134SMartin Matuska 		    &dr->dr_bp_copy, data, dbuf_is_l2cacheable(db),
5080eda14cbcSMatt Macy 		    &zp, dbuf_write_ready,
5081eda14cbcSMatt Macy 		    children_ready_cb, dbuf_write_physdone,
5082eda14cbcSMatt Macy 		    dbuf_write_done, db, ZIO_PRIORITY_ASYNC_WRITE,
5083eda14cbcSMatt Macy 		    ZIO_FLAG_MUSTSUCCEED, &zb);
5084eda14cbcSMatt Macy 	}
5085eda14cbcSMatt Macy }
5086eda14cbcSMatt Macy 
5087eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_find);
5088eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_is_metadata);
5089eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_destroy);
5090eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_loan_arcbuf);
5091eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_whichblock);
5092eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_read);
5093eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_unoverride);
5094eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_free_range);
5095eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_new_size);
5096eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_release_bp);
5097eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_dirty);
5098eda14cbcSMatt Macy EXPORT_SYMBOL(dmu_buf_set_crypt_params);
5099eda14cbcSMatt Macy EXPORT_SYMBOL(dmu_buf_will_dirty);
5100eda14cbcSMatt Macy EXPORT_SYMBOL(dmu_buf_is_dirty);
5101eda14cbcSMatt Macy EXPORT_SYMBOL(dmu_buf_will_not_fill);
5102eda14cbcSMatt Macy EXPORT_SYMBOL(dmu_buf_will_fill);
5103eda14cbcSMatt Macy EXPORT_SYMBOL(dmu_buf_fill_done);
5104eda14cbcSMatt Macy EXPORT_SYMBOL(dmu_buf_rele);
5105eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_assign_arcbuf);
5106eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_prefetch);
5107eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_hold_impl);
5108eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_hold);
5109eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_hold_level);
5110eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_create_bonus);
5111eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_spill_set_blksz);
5112eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_rm_spill);
5113eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_add_ref);
5114eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_rele);
5115eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_rele_and_unlock);
5116eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_refcount);
5117eda14cbcSMatt Macy EXPORT_SYMBOL(dbuf_sync_list);
5118eda14cbcSMatt Macy EXPORT_SYMBOL(dmu_buf_set_user);
5119eda14cbcSMatt Macy EXPORT_SYMBOL(dmu_buf_set_user_ie);
5120eda14cbcSMatt Macy EXPORT_SYMBOL(dmu_buf_get_user);
5121eda14cbcSMatt Macy EXPORT_SYMBOL(dmu_buf_get_blkptr);
5122eda14cbcSMatt Macy 
5123dbd5678dSMartin Matuska ZFS_MODULE_PARAM(zfs_dbuf_cache, dbuf_cache_, max_bytes, U64, ZMOD_RW,
5124eda14cbcSMatt Macy 	"Maximum size in bytes of the dbuf cache.");
5125eda14cbcSMatt Macy 
5126eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_dbuf_cache, dbuf_cache_, hiwater_pct, UINT, ZMOD_RW,
5127c03c5b1cSMartin Matuska 	"Percentage over dbuf_cache_max_bytes for direct dbuf eviction.");
5128eda14cbcSMatt Macy 
5129eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_dbuf_cache, dbuf_cache_, lowater_pct, UINT, ZMOD_RW,
5130c03c5b1cSMartin Matuska 	"Percentage below dbuf_cache_max_bytes when dbuf eviction stops.");
5131eda14cbcSMatt Macy 
5132dbd5678dSMartin Matuska ZFS_MODULE_PARAM(zfs_dbuf, dbuf_, metadata_cache_max_bytes, U64, ZMOD_RW,
5133c03c5b1cSMartin Matuska 	"Maximum size in bytes of dbuf metadata cache.");
5134eda14cbcSMatt Macy 
5135be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_dbuf, dbuf_, cache_shift, UINT, ZMOD_RW,
5136c03c5b1cSMartin Matuska 	"Set size of dbuf cache to log2 fraction of arc size.");
5137eda14cbcSMatt Macy 
5138be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_dbuf, dbuf_, metadata_cache_shift, UINT, ZMOD_RW,
5139c03c5b1cSMartin Matuska 	"Set size of dbuf metadata cache to log2 fraction of arc size.");
5140be181ee2SMartin Matuska 
5141be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_dbuf, dbuf_, mutex_cache_shift, UINT, ZMOD_RD,
5142be181ee2SMartin Matuska 	"Set size of dbuf cache mutex array as log2 shift.");
5143