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