]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/dbuf.c
OpenZFS 8081 - Compiler warnings in zdb
[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
9 * or http://www.opensolaris.org/os/licensing.
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.
64fc7762 24 * Copyright (c) 2012, 2017 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.
34dc7c2f
BB
27 */
28
34dc7c2f 29#include <sys/zfs_context.h>
c28b2279 30#include <sys/arc.h>
34dc7c2f 31#include <sys/dmu.h>
ea97f8ce 32#include <sys/dmu_send.h>
34dc7c2f
BB
33#include <sys/dmu_impl.h>
34#include <sys/dbuf.h>
35#include <sys/dmu_objset.h>
36#include <sys/dsl_dataset.h>
37#include <sys/dsl_dir.h>
38#include <sys/dmu_tx.h>
39#include <sys/spa.h>
40#include <sys/zio.h>
41#include <sys/dmu_zfetch.h>
428870ff
BB
42#include <sys/sa.h>
43#include <sys/sa_impl.h>
9b67f605
MA
44#include <sys/zfeature.h>
45#include <sys/blkptr.h>
9bd274dd 46#include <sys/range_tree.h>
49ee64e5 47#include <sys/trace_dbuf.h>
d3c2ae1c 48#include <sys/callb.h>
a6255b7f 49#include <sys/abd.h>
34dc7c2f 50
fc5bb51f
BB
51struct dbuf_hold_impl_data {
52 /* Function arguments */
53 dnode_t *dh_dn;
54 uint8_t dh_level;
55 uint64_t dh_blkid;
fcff0f35
PD
56 boolean_t dh_fail_sparse;
57 boolean_t dh_fail_uncached;
fc5bb51f
BB
58 void *dh_tag;
59 dmu_buf_impl_t **dh_dbp;
60 /* Local variables */
61 dmu_buf_impl_t *dh_db;
62 dmu_buf_impl_t *dh_parent;
63 blkptr_t *dh_bp;
64 int dh_err;
65 dbuf_dirty_record_t *dh_dr;
66 arc_buf_contents_t dh_type;
67 int dh_depth;
68};
69
70static void __dbuf_hold_impl_init(struct dbuf_hold_impl_data *dh,
fcff0f35
PD
71 dnode_t *dn, uint8_t level, uint64_t blkid, boolean_t fail_sparse,
72 boolean_t fail_uncached,
73 void *tag, dmu_buf_impl_t **dbp, int depth);
fc5bb51f
BB
74static int __dbuf_hold_impl(struct dbuf_hold_impl_data *dh);
75
d3c2ae1c 76uint_t zfs_dbuf_evict_key;
b663a23d 77
13fe0198 78static boolean_t dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
b128c09f 79static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx);
34dc7c2f 80
0c66c32d 81extern inline void dmu_buf_init_user(dmu_buf_user_t *dbu,
39efbde7
GM
82 dmu_buf_evict_func_t *evict_func_sync,
83 dmu_buf_evict_func_t *evict_func_async,
84 dmu_buf_t **clear_on_evict_dbufp);
0c66c32d 85
34dc7c2f
BB
86/*
87 * Global data structures and functions for the dbuf cache.
88 */
d3c2ae1c 89static kmem_cache_t *dbuf_kmem_cache;
0c66c32d 90static taskq_t *dbu_evict_taskq;
34dc7c2f 91
d3c2ae1c
GW
92static kthread_t *dbuf_cache_evict_thread;
93static kmutex_t dbuf_evict_lock;
94static kcondvar_t dbuf_evict_cv;
95static boolean_t dbuf_evict_thread_exit;
96
97/*
98 * LRU cache of dbufs. The dbuf cache maintains a list of dbufs that
99 * are not currently held but have been recently released. These dbufs
100 * are not eligible for arc eviction until they are aged out of the cache.
101 * Dbufs are added to the dbuf cache once the last hold is released. If a
102 * dbuf is later accessed and still exists in the dbuf cache, then it will
103 * be removed from the cache and later re-added to the head of the cache.
104 * Dbufs that are aged out of the cache will be immediately destroyed and
105 * become eligible for arc eviction.
106 */
64fc7762 107static multilist_t *dbuf_cache;
d3c2ae1c
GW
108static refcount_t dbuf_cache_size;
109unsigned long dbuf_cache_max_bytes = 100 * 1024 * 1024;
110
111/* Cap the size of the dbuf cache to log2 fraction of arc size. */
112int dbuf_cache_max_shift = 5;
113
114/*
115 * The dbuf cache uses a three-stage eviction policy:
116 * - A low water marker designates when the dbuf eviction thread
117 * should stop evicting from the dbuf cache.
118 * - When we reach the maximum size (aka mid water mark), we
119 * signal the eviction thread to run.
120 * - The high water mark indicates when the eviction thread
121 * is unable to keep up with the incoming load and eviction must
122 * happen in the context of the calling thread.
123 *
124 * The dbuf cache:
125 * (max size)
126 * low water mid water hi water
127 * +----------------------------------------+----------+----------+
128 * | | | |
129 * | | | |
130 * | | | |
131 * | | | |
132 * +----------------------------------------+----------+----------+
133 * stop signal evict
134 * evicting eviction directly
135 * thread
136 *
137 * The high and low water marks indicate the operating range for the eviction
138 * thread. The low water mark is, by default, 90% of the total size of the
139 * cache and the high water mark is at 110% (both of these percentages can be
140 * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct,
141 * respectively). The eviction thread will try to ensure that the cache remains
142 * within this range by waking up every second and checking if the cache is
143 * above the low water mark. The thread can also be woken up by callers adding
144 * elements into the cache if the cache is larger than the mid water (i.e max
145 * cache size). Once the eviction thread is woken up and eviction is required,
146 * it will continue evicting buffers until it's able to reduce the cache size
147 * to the low water mark. If the cache size continues to grow and hits the high
4e33ba4c 148 * water mark, then callers adding elements to the cache will begin to evict
d3c2ae1c
GW
149 * directly from the cache until the cache is no longer above the high water
150 * mark.
151 */
152
153/*
154 * The percentage above and below the maximum cache size.
155 */
156uint_t dbuf_cache_hiwater_pct = 10;
157uint_t dbuf_cache_lowater_pct = 10;
158
34dc7c2f
BB
159/* ARGSUSED */
160static int
161dbuf_cons(void *vdb, void *unused, int kmflag)
162{
163 dmu_buf_impl_t *db = vdb;
164 bzero(db, sizeof (dmu_buf_impl_t));
165
166 mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL);
167 cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL);
d3c2ae1c 168 multilist_link_init(&db->db_cache_link);
34dc7c2f 169 refcount_create(&db->db_holds);
8951cb8d 170
34dc7c2f
BB
171 return (0);
172}
173
174/* ARGSUSED */
175static void
176dbuf_dest(void *vdb, void *unused)
177{
178 dmu_buf_impl_t *db = vdb;
179 mutex_destroy(&db->db_mtx);
180 cv_destroy(&db->db_changed);
d3c2ae1c 181 ASSERT(!multilist_link_active(&db->db_cache_link));
34dc7c2f
BB
182 refcount_destroy(&db->db_holds);
183}
184
185/*
186 * dbuf hash table routines
187 */
188static dbuf_hash_table_t dbuf_hash_table;
189
190static uint64_t dbuf_hash_count;
191
192static uint64_t
193dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid)
194{
195 uintptr_t osv = (uintptr_t)os;
196 uint64_t crc = -1ULL;
197
198 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
199 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (lvl)) & 0xFF];
200 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF];
201 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF];
202 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF];
203 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 0)) & 0xFF];
204 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 8)) & 0xFF];
205
206 crc ^= (osv>>14) ^ (obj>>16) ^ (blkid>>16);
207
208 return (crc);
209}
210
34dc7c2f
BB
211#define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
212 ((dbuf)->db.db_object == (obj) && \
213 (dbuf)->db_objset == (os) && \
214 (dbuf)->db_level == (level) && \
215 (dbuf)->db_blkid == (blkid))
216
217dmu_buf_impl_t *
6ebebace 218dbuf_find(objset_t *os, uint64_t obj, uint8_t level, uint64_t blkid)
34dc7c2f
BB
219{
220 dbuf_hash_table_t *h = &dbuf_hash_table;
d6320ddb
BB
221 uint64_t hv;
222 uint64_t idx;
34dc7c2f
BB
223 dmu_buf_impl_t *db;
224
d3c2ae1c 225 hv = dbuf_hash(os, obj, level, blkid);
d6320ddb
BB
226 idx = hv & h->hash_table_mask;
227
34dc7c2f
BB
228 mutex_enter(DBUF_HASH_MUTEX(h, idx));
229 for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) {
230 if (DBUF_EQUAL(db, os, obj, level, blkid)) {
231 mutex_enter(&db->db_mtx);
232 if (db->db_state != DB_EVICTING) {
233 mutex_exit(DBUF_HASH_MUTEX(h, idx));
234 return (db);
235 }
236 mutex_exit(&db->db_mtx);
237 }
238 }
239 mutex_exit(DBUF_HASH_MUTEX(h, idx));
240 return (NULL);
241}
242
6ebebace
JG
243static dmu_buf_impl_t *
244dbuf_find_bonus(objset_t *os, uint64_t object)
245{
246 dnode_t *dn;
247 dmu_buf_impl_t *db = NULL;
248
249 if (dnode_hold(os, object, FTAG, &dn) == 0) {
250 rw_enter(&dn->dn_struct_rwlock, RW_READER);
251 if (dn->dn_bonus != NULL) {
252 db = dn->dn_bonus;
253 mutex_enter(&db->db_mtx);
254 }
255 rw_exit(&dn->dn_struct_rwlock);
256 dnode_rele(dn, FTAG);
257 }
258 return (db);
259}
260
34dc7c2f
BB
261/*
262 * Insert an entry into the hash table. If there is already an element
263 * equal to elem in the hash table, then the already existing element
264 * will be returned and the new element will not be inserted.
265 * Otherwise returns NULL.
266 */
267static dmu_buf_impl_t *
268dbuf_hash_insert(dmu_buf_impl_t *db)
269{
270 dbuf_hash_table_t *h = &dbuf_hash_table;
428870ff 271 objset_t *os = db->db_objset;
34dc7c2f
BB
272 uint64_t obj = db->db.db_object;
273 int level = db->db_level;
d6320ddb 274 uint64_t blkid, hv, idx;
34dc7c2f
BB
275 dmu_buf_impl_t *dbf;
276
d6320ddb 277 blkid = db->db_blkid;
d3c2ae1c 278 hv = dbuf_hash(os, obj, level, blkid);
d6320ddb
BB
279 idx = hv & h->hash_table_mask;
280
34dc7c2f
BB
281 mutex_enter(DBUF_HASH_MUTEX(h, idx));
282 for (dbf = h->hash_table[idx]; dbf != NULL; dbf = dbf->db_hash_next) {
283 if (DBUF_EQUAL(dbf, os, obj, level, blkid)) {
284 mutex_enter(&dbf->db_mtx);
285 if (dbf->db_state != DB_EVICTING) {
286 mutex_exit(DBUF_HASH_MUTEX(h, idx));
287 return (dbf);
288 }
289 mutex_exit(&dbf->db_mtx);
290 }
291 }
292
293 mutex_enter(&db->db_mtx);
294 db->db_hash_next = h->hash_table[idx];
295 h->hash_table[idx] = db;
296 mutex_exit(DBUF_HASH_MUTEX(h, idx));
bc89ac84 297 atomic_inc_64(&dbuf_hash_count);
34dc7c2f
BB
298
299 return (NULL);
300}
301
302/*
bd089c54 303 * Remove an entry from the hash table. It must be in the EVICTING state.
34dc7c2f
BB
304 */
305static void
306dbuf_hash_remove(dmu_buf_impl_t *db)
307{
308 dbuf_hash_table_t *h = &dbuf_hash_table;
d6320ddb 309 uint64_t hv, idx;
34dc7c2f
BB
310 dmu_buf_impl_t *dbf, **dbp;
311
d3c2ae1c 312 hv = dbuf_hash(db->db_objset, db->db.db_object,
d6320ddb
BB
313 db->db_level, db->db_blkid);
314 idx = hv & h->hash_table_mask;
315
34dc7c2f 316 /*
4e33ba4c 317 * We mustn't hold db_mtx to maintain lock ordering:
34dc7c2f
BB
318 * DBUF_HASH_MUTEX > db_mtx.
319 */
320 ASSERT(refcount_is_zero(&db->db_holds));
321 ASSERT(db->db_state == DB_EVICTING);
322 ASSERT(!MUTEX_HELD(&db->db_mtx));
323
324 mutex_enter(DBUF_HASH_MUTEX(h, idx));
325 dbp = &h->hash_table[idx];
326 while ((dbf = *dbp) != db) {
327 dbp = &dbf->db_hash_next;
328 ASSERT(dbf != NULL);
329 }
330 *dbp = db->db_hash_next;
331 db->db_hash_next = NULL;
332 mutex_exit(DBUF_HASH_MUTEX(h, idx));
bc89ac84 333 atomic_dec_64(&dbuf_hash_count);
34dc7c2f
BB
334}
335
0c66c32d
JG
336typedef enum {
337 DBVU_EVICTING,
338 DBVU_NOT_EVICTING
339} dbvu_verify_type_t;
340
341static void
342dbuf_verify_user(dmu_buf_impl_t *db, dbvu_verify_type_t verify_type)
343{
344#ifdef ZFS_DEBUG
345 int64_t holds;
346
347 if (db->db_user == NULL)
348 return;
349
350 /* Only data blocks support the attachment of user data. */
351 ASSERT(db->db_level == 0);
352
353 /* Clients must resolve a dbuf before attaching user data. */
354 ASSERT(db->db.db_data != NULL);
355 ASSERT3U(db->db_state, ==, DB_CACHED);
356
357 holds = refcount_count(&db->db_holds);
358 if (verify_type == DBVU_EVICTING) {
359 /*
360 * Immediate eviction occurs when holds == dirtycnt.
361 * For normal eviction buffers, holds is zero on
362 * eviction, except when dbuf_fix_old_data() calls
363 * dbuf_clear_data(). However, the hold count can grow
364 * during eviction even though db_mtx is held (see
365 * dmu_bonus_hold() for an example), so we can only
366 * test the generic invariant that holds >= dirtycnt.
367 */
368 ASSERT3U(holds, >=, db->db_dirtycnt);
369 } else {
bc4501f7 370 if (db->db_user_immediate_evict == TRUE)
0c66c32d
JG
371 ASSERT3U(holds, >=, db->db_dirtycnt);
372 else
373 ASSERT3U(holds, >, 0);
374 }
375#endif
376}
377
34dc7c2f
BB
378static void
379dbuf_evict_user(dmu_buf_impl_t *db)
380{
0c66c32d
JG
381 dmu_buf_user_t *dbu = db->db_user;
382
34dc7c2f
BB
383 ASSERT(MUTEX_HELD(&db->db_mtx));
384
0c66c32d 385 if (dbu == NULL)
34dc7c2f
BB
386 return;
387
0c66c32d
JG
388 dbuf_verify_user(db, DBVU_EVICTING);
389 db->db_user = NULL;
390
391#ifdef ZFS_DEBUG
392 if (dbu->dbu_clear_on_evict_dbufp != NULL)
393 *dbu->dbu_clear_on_evict_dbufp = NULL;
394#endif
395
396 /*
39efbde7
GM
397 * There are two eviction callbacks - one that we call synchronously
398 * and one that we invoke via a taskq. The async one is useful for
399 * avoiding lock order reversals and limiting stack depth.
400 *
401 * Note that if we have a sync callback but no async callback,
402 * it's likely that the sync callback will free the structure
403 * containing the dbu. In that case we need to take care to not
404 * dereference dbu after calling the sync evict func.
0c66c32d 405 */
a7004725 406 boolean_t has_async = (dbu->dbu_evict_func_async != NULL);
39efbde7
GM
407
408 if (dbu->dbu_evict_func_sync != NULL)
409 dbu->dbu_evict_func_sync(dbu);
410
411 if (has_async) {
412 taskq_dispatch_ent(dbu_evict_taskq, dbu->dbu_evict_func_async,
413 dbu, 0, &dbu->dbu_tqent);
414 }
34dc7c2f
BB
415}
416
572e2857
BB
417boolean_t
418dbuf_is_metadata(dmu_buf_impl_t *db)
419{
cc79a5c2
BB
420 /*
421 * Consider indirect blocks and spill blocks to be meta data.
422 */
423 if (db->db_level > 0 || db->db_blkid == DMU_SPILL_BLKID) {
572e2857
BB
424 return (B_TRUE);
425 } else {
426 boolean_t is_metadata;
427
428 DB_DNODE_ENTER(db);
9ae529ec 429 is_metadata = DMU_OT_IS_METADATA(DB_DNODE(db)->dn_type);
572e2857
BB
430 DB_DNODE_EXIT(db);
431
432 return (is_metadata);
433 }
434}
435
d3c2ae1c
GW
436
437/*
438 * This function *must* return indices evenly distributed between all
439 * sublists of the multilist. This is needed due to how the dbuf eviction
440 * code is laid out; dbuf_evict_thread() assumes dbufs are evenly
441 * distributed between all sublists and uses this assumption when
442 * deciding which sublist to evict from and how much to evict from it.
443 */
444unsigned int
445dbuf_cache_multilist_index_func(multilist_t *ml, void *obj)
34dc7c2f 446{
d3c2ae1c
GW
447 dmu_buf_impl_t *db = obj;
448
449 /*
450 * The assumption here, is the hash value for a given
451 * dmu_buf_impl_t will remain constant throughout it's lifetime
452 * (i.e. it's objset, object, level and blkid fields don't change).
453 * Thus, we don't need to store the dbuf's sublist index
454 * on insertion, as this index can be recalculated on removal.
455 *
456 * Also, the low order bits of the hash value are thought to be
457 * distributed evenly. Otherwise, in the case that the multilist
458 * has a power of two number of sublists, each sublists' usage
459 * would not be evenly distributed.
460 */
461 return (dbuf_hash(db->db_objset, db->db.db_object,
462 db->db_level, db->db_blkid) %
463 multilist_get_num_sublists(ml));
464}
465
e71cade6 466static inline unsigned long
467dbuf_cache_target_bytes(void)
468{
469 return MIN(dbuf_cache_max_bytes,
470 arc_target_bytes() >> dbuf_cache_max_shift);
471}
472
d3c2ae1c
GW
473static inline boolean_t
474dbuf_cache_above_hiwater(void)
475{
e71cade6 476 uint64_t dbuf_cache_target = dbuf_cache_target_bytes();
477
d3c2ae1c 478 uint64_t dbuf_cache_hiwater_bytes =
e71cade6 479 (dbuf_cache_target * dbuf_cache_hiwater_pct) / 100;
d3c2ae1c
GW
480
481 return (refcount_count(&dbuf_cache_size) >
e71cade6 482 dbuf_cache_target + dbuf_cache_hiwater_bytes);
d3c2ae1c
GW
483}
484
485static inline boolean_t
486dbuf_cache_above_lowater(void)
487{
e71cade6 488 uint64_t dbuf_cache_target = dbuf_cache_target_bytes();
489
d3c2ae1c 490 uint64_t dbuf_cache_lowater_bytes =
e71cade6 491 (dbuf_cache_target * dbuf_cache_lowater_pct) / 100;
d3c2ae1c
GW
492
493 return (refcount_count(&dbuf_cache_size) >
e71cade6 494 dbuf_cache_target - dbuf_cache_lowater_bytes);
d3c2ae1c
GW
495}
496
497/*
498 * Evict the oldest eligible dbuf from the dbuf cache.
499 */
500static void
501dbuf_evict_one(void)
502{
64fc7762
MA
503 int idx = multilist_get_random_index(dbuf_cache);
504 multilist_sublist_t *mls = multilist_sublist_lock(dbuf_cache, idx);
d3c2ae1c
GW
505 dmu_buf_impl_t *db;
506 ASSERT(!MUTEX_HELD(&dbuf_evict_lock));
507
508 /*
509 * Set the thread's tsd to indicate that it's processing evictions.
510 * Once a thread stops evicting from the dbuf cache it will
511 * reset its tsd to NULL.
512 */
513 ASSERT3P(tsd_get(zfs_dbuf_evict_key), ==, NULL);
514 (void) tsd_set(zfs_dbuf_evict_key, (void *)B_TRUE);
515
516 db = multilist_sublist_tail(mls);
517 while (db != NULL && mutex_tryenter(&db->db_mtx) == 0) {
518 db = multilist_sublist_prev(mls, db);
519 }
520
521 DTRACE_PROBE2(dbuf__evict__one, dmu_buf_impl_t *, db,
522 multilist_sublist_t *, mls);
523
524 if (db != NULL) {
525 multilist_sublist_remove(mls, db);
526 multilist_sublist_unlock(mls);
527 (void) refcount_remove_many(&dbuf_cache_size,
528 db->db.db_size, db);
529 dbuf_destroy(db);
530 } else {
531 multilist_sublist_unlock(mls);
532 }
533 (void) tsd_set(zfs_dbuf_evict_key, NULL);
534}
535
536/*
537 * The dbuf evict thread is responsible for aging out dbufs from the
538 * cache. Once the cache has reached it's maximum size, dbufs are removed
539 * and destroyed. The eviction thread will continue running until the size
540 * of the dbuf cache is at or below the maximum size. Once the dbuf is aged
541 * out of the cache it is destroyed and becomes eligible for arc eviction.
542 */
867959b5 543/* ARGSUSED */
d3c2ae1c 544static void
c25b8f99 545dbuf_evict_thread(void *unused)
d3c2ae1c
GW
546{
547 callb_cpr_t cpr;
548
549 CALLB_CPR_INIT(&cpr, &dbuf_evict_lock, callb_generic_cpr, FTAG);
550
551 mutex_enter(&dbuf_evict_lock);
552 while (!dbuf_evict_thread_exit) {
553 while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit) {
554 CALLB_CPR_SAFE_BEGIN(&cpr);
555 (void) cv_timedwait_sig_hires(&dbuf_evict_cv,
556 &dbuf_evict_lock, SEC2NSEC(1), MSEC2NSEC(1), 0);
557 CALLB_CPR_SAFE_END(&cpr, &dbuf_evict_lock);
558 }
559 mutex_exit(&dbuf_evict_lock);
560
561 /*
562 * Keep evicting as long as we're above the low water mark
563 * for the cache. We do this without holding the locks to
564 * minimize lock contention.
565 */
566 while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit) {
567 dbuf_evict_one();
568 }
569
570 mutex_enter(&dbuf_evict_lock);
571 }
572
573 dbuf_evict_thread_exit = B_FALSE;
574 cv_broadcast(&dbuf_evict_cv);
575 CALLB_CPR_EXIT(&cpr); /* drops dbuf_evict_lock */
576 thread_exit();
577}
578
579/*
580 * Wake up the dbuf eviction thread if the dbuf cache is at its max size.
581 * If the dbuf cache is at its high water mark, then evict a dbuf from the
582 * dbuf cache using the callers context.
583 */
584static void
585dbuf_evict_notify(void)
586{
587
588 /*
589 * We use thread specific data to track when a thread has
590 * started processing evictions. This allows us to avoid deeply
591 * nested stacks that would have a call flow similar to this:
592 *
593 * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify()
594 * ^ |
595 * | |
596 * +-----dbuf_destroy()<--dbuf_evict_one()<--------+
597 *
598 * The dbuf_eviction_thread will always have its tsd set until
599 * that thread exits. All other threads will only set their tsd
600 * if they are participating in the eviction process. This only
601 * happens if the eviction thread is unable to process evictions
602 * fast enough. To keep the dbuf cache size in check, other threads
603 * can evict from the dbuf cache directly. Those threads will set
604 * their tsd values so that we ensure that they only evict one dbuf
605 * from the dbuf cache.
606 */
607 if (tsd_get(zfs_dbuf_evict_key) != NULL)
608 return;
609
38240ebd
MA
610 /*
611 * We check if we should evict without holding the dbuf_evict_lock,
612 * because it's OK to occasionally make the wrong decision here,
613 * and grabbing the lock results in massive lock contention.
614 */
e71cade6 615 if (refcount_count(&dbuf_cache_size) > dbuf_cache_target_bytes()) {
38240ebd 616 if (dbuf_cache_above_hiwater())
d3c2ae1c 617 dbuf_evict_one();
38240ebd 618 cv_signal(&dbuf_evict_cv);
d3c2ae1c 619 }
34dc7c2f
BB
620}
621
d3c2ae1c
GW
622
623
34dc7c2f
BB
624void
625dbuf_init(void)
626{
627 uint64_t hsize = 1ULL << 16;
628 dbuf_hash_table_t *h = &dbuf_hash_table;
629 int i;
630
631 /*
632 * The hash table is big enough to fill all of physical memory
69de3421
TC
633 * with an average block size of zfs_arc_average_blocksize (default 8K).
634 * By default, the table will take up
635 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
34dc7c2f 636 */
69de3421 637 while (hsize * zfs_arc_average_blocksize < physmem * PAGESIZE)
34dc7c2f
BB
638 hsize <<= 1;
639
640retry:
641 h->hash_table_mask = hsize - 1;
00b46022 642#if defined(_KERNEL) && defined(HAVE_SPL)
d1d7e268
MK
643 /*
644 * Large allocations which do not require contiguous pages
645 * should be using vmem_alloc() in the linux kernel
646 */
79c76d5b 647 h->hash_table = vmem_zalloc(hsize * sizeof (void *), KM_SLEEP);
00b46022 648#else
34dc7c2f 649 h->hash_table = kmem_zalloc(hsize * sizeof (void *), KM_NOSLEEP);
00b46022 650#endif
34dc7c2f
BB
651 if (h->hash_table == NULL) {
652 /* XXX - we should really return an error instead of assert */
653 ASSERT(hsize > (1ULL << 10));
654 hsize >>= 1;
655 goto retry;
656 }
657
d3c2ae1c 658 dbuf_kmem_cache = kmem_cache_create("dmu_buf_impl_t",
34dc7c2f
BB
659 sizeof (dmu_buf_impl_t),
660 0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0);
661
662 for (i = 0; i < DBUF_MUTEXES; i++)
40d06e3c 663 mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL);
e0b0ca98
BB
664
665 dbuf_stats_init(h);
0c66c32d 666
d3c2ae1c
GW
667 /*
668 * Setup the parameters for the dbuf cache. We cap the size of the
669 * dbuf cache to 1/32nd (default) of the size of the ARC.
670 */
671 dbuf_cache_max_bytes = MIN(dbuf_cache_max_bytes,
e71cade6 672 arc_target_bytes() >> dbuf_cache_max_shift);
d3c2ae1c 673
0c66c32d
JG
674 /*
675 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
676 * configuration is not required.
677 */
1229323d 678 dbu_evict_taskq = taskq_create("dbu_evict", 1, defclsyspri, 0, 0, 0);
d3c2ae1c 679
64fc7762 680 dbuf_cache = multilist_create(sizeof (dmu_buf_impl_t),
d3c2ae1c 681 offsetof(dmu_buf_impl_t, db_cache_link),
d3c2ae1c
GW
682 dbuf_cache_multilist_index_func);
683 refcount_create(&dbuf_cache_size);
684
685 tsd_create(&zfs_dbuf_evict_key, NULL);
686 dbuf_evict_thread_exit = B_FALSE;
687 mutex_init(&dbuf_evict_lock, NULL, MUTEX_DEFAULT, NULL);
688 cv_init(&dbuf_evict_cv, NULL, CV_DEFAULT, NULL);
689 dbuf_cache_evict_thread = thread_create(NULL, 0, dbuf_evict_thread,
690 NULL, 0, &p0, TS_RUN, minclsyspri);
34dc7c2f
BB
691}
692
693void
694dbuf_fini(void)
695{
696 dbuf_hash_table_t *h = &dbuf_hash_table;
697 int i;
698
e0b0ca98
BB
699 dbuf_stats_destroy();
700
34dc7c2f
BB
701 for (i = 0; i < DBUF_MUTEXES; i++)
702 mutex_destroy(&h->hash_mutexes[i]);
00b46022 703#if defined(_KERNEL) && defined(HAVE_SPL)
d1d7e268
MK
704 /*
705 * Large allocations which do not require contiguous pages
706 * should be using vmem_free() in the linux kernel
707 */
00b46022
BB
708 vmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
709#else
34dc7c2f 710 kmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
00b46022 711#endif
d3c2ae1c 712 kmem_cache_destroy(dbuf_kmem_cache);
0c66c32d 713 taskq_destroy(dbu_evict_taskq);
d3c2ae1c
GW
714
715 mutex_enter(&dbuf_evict_lock);
716 dbuf_evict_thread_exit = B_TRUE;
717 while (dbuf_evict_thread_exit) {
718 cv_signal(&dbuf_evict_cv);
719 cv_wait(&dbuf_evict_cv, &dbuf_evict_lock);
720 }
721 mutex_exit(&dbuf_evict_lock);
722 tsd_destroy(&zfs_dbuf_evict_key);
723
724 mutex_destroy(&dbuf_evict_lock);
725 cv_destroy(&dbuf_evict_cv);
726
727 refcount_destroy(&dbuf_cache_size);
64fc7762 728 multilist_destroy(dbuf_cache);
34dc7c2f
BB
729}
730
731/*
732 * Other stuff.
733 */
734
735#ifdef ZFS_DEBUG
736static void
737dbuf_verify(dmu_buf_impl_t *db)
738{
572e2857 739 dnode_t *dn;
428870ff 740 dbuf_dirty_record_t *dr;
34dc7c2f
BB
741
742 ASSERT(MUTEX_HELD(&db->db_mtx));
743
744 if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY))
745 return;
746
747 ASSERT(db->db_objset != NULL);
572e2857
BB
748 DB_DNODE_ENTER(db);
749 dn = DB_DNODE(db);
34dc7c2f
BB
750 if (dn == NULL) {
751 ASSERT(db->db_parent == NULL);
752 ASSERT(db->db_blkptr == NULL);
753 } else {
754 ASSERT3U(db->db.db_object, ==, dn->dn_object);
755 ASSERT3P(db->db_objset, ==, dn->dn_objset);
756 ASSERT3U(db->db_level, <, dn->dn_nlevels);
572e2857
BB
757 ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
758 db->db_blkid == DMU_SPILL_BLKID ||
8951cb8d 759 !avl_is_empty(&dn->dn_dbufs));
34dc7c2f 760 }
428870ff
BB
761 if (db->db_blkid == DMU_BONUS_BLKID) {
762 ASSERT(dn != NULL);
763 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
764 ASSERT3U(db->db.db_offset, ==, DMU_BONUS_BLKID);
765 } else if (db->db_blkid == DMU_SPILL_BLKID) {
34dc7c2f 766 ASSERT(dn != NULL);
c99c9001 767 ASSERT0(db->db.db_offset);
34dc7c2f
BB
768 } else {
769 ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size);
770 }
771
428870ff
BB
772 for (dr = db->db_data_pending; dr != NULL; dr = dr->dr_next)
773 ASSERT(dr->dr_dbuf == db);
774
775 for (dr = db->db_last_dirty; dr != NULL; dr = dr->dr_next)
776 ASSERT(dr->dr_dbuf == db);
777
b128c09f
BB
778 /*
779 * We can't assert that db_size matches dn_datablksz because it
780 * can be momentarily different when another thread is doing
781 * dnode_set_blksz().
782 */
783 if (db->db_level == 0 && db->db.db_object == DMU_META_DNODE_OBJECT) {
428870ff 784 dr = db->db_data_pending;
b128c09f
BB
785 /*
786 * It should only be modified in syncing context, so
787 * make sure we only have one copy of the data.
788 */
789 ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf);
34dc7c2f
BB
790 }
791
792 /* verify db->db_blkptr */
793 if (db->db_blkptr) {
794 if (db->db_parent == dn->dn_dbuf) {
795 /* db is pointed to by the dnode */
796 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
9babb374 797 if (DMU_OBJECT_IS_SPECIAL(db->db.db_object))
34dc7c2f
BB
798 ASSERT(db->db_parent == NULL);
799 else
800 ASSERT(db->db_parent != NULL);
428870ff
BB
801 if (db->db_blkid != DMU_SPILL_BLKID)
802 ASSERT3P(db->db_blkptr, ==,
803 &dn->dn_phys->dn_blkptr[db->db_blkid]);
34dc7c2f
BB
804 } else {
805 /* db is pointed to by an indirect block */
1fde1e37 806 ASSERTV(int epb = db->db_parent->db.db_size >>
02730c33 807 SPA_BLKPTRSHIFT);
34dc7c2f
BB
808 ASSERT3U(db->db_parent->db_level, ==, db->db_level+1);
809 ASSERT3U(db->db_parent->db.db_object, ==,
810 db->db.db_object);
811 /*
812 * dnode_grow_indblksz() can make this fail if we don't
813 * have the struct_rwlock. XXX indblksz no longer
814 * grows. safe to do this now?
815 */
572e2857 816 if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
34dc7c2f
BB
817 ASSERT3P(db->db_blkptr, ==,
818 ((blkptr_t *)db->db_parent->db.db_data +
819 db->db_blkid % epb));
820 }
821 }
822 }
823 if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) &&
428870ff
BB
824 (db->db_buf == NULL || db->db_buf->b_data) &&
825 db->db.db_data && db->db_blkid != DMU_BONUS_BLKID &&
34dc7c2f
BB
826 db->db_state != DB_FILL && !dn->dn_free_txg) {
827 /*
828 * If the blkptr isn't set but they have nonzero data,
829 * it had better be dirty, otherwise we'll lose that
830 * data when we evict this buffer.
bc77ba73
PD
831 *
832 * There is an exception to this rule for indirect blocks; in
833 * this case, if the indirect block is a hole, we fill in a few
834 * fields on each of the child blocks (importantly, birth time)
835 * to prevent hole birth times from being lost when you
836 * partially fill in a hole.
34dc7c2f
BB
837 */
838 if (db->db_dirtycnt == 0) {
bc77ba73
PD
839 if (db->db_level == 0) {
840 uint64_t *buf = db->db.db_data;
841 int i;
34dc7c2f 842
bc77ba73
PD
843 for (i = 0; i < db->db.db_size >> 3; i++) {
844 ASSERT(buf[i] == 0);
845 }
846 } else {
847 int i;
848 blkptr_t *bps = db->db.db_data;
849 ASSERT3U(1 << DB_DNODE(db)->dn_indblkshift, ==,
850 db->db.db_size);
851 /*
852 * We want to verify that all the blkptrs in the
853 * indirect block are holes, but we may have
854 * automatically set up a few fields for them.
855 * We iterate through each blkptr and verify
856 * they only have those fields set.
857 */
858 for (i = 0;
859 i < db->db.db_size / sizeof (blkptr_t);
860 i++) {
861 blkptr_t *bp = &bps[i];
862 ASSERT(ZIO_CHECKSUM_IS_ZERO(
863 &bp->blk_cksum));
864 ASSERT(
865 DVA_IS_EMPTY(&bp->blk_dva[0]) &&
866 DVA_IS_EMPTY(&bp->blk_dva[1]) &&
867 DVA_IS_EMPTY(&bp->blk_dva[2]));
868 ASSERT0(bp->blk_fill);
869 ASSERT0(bp->blk_pad[0]);
870 ASSERT0(bp->blk_pad[1]);
871 ASSERT(!BP_IS_EMBEDDED(bp));
872 ASSERT(BP_IS_HOLE(bp));
873 ASSERT0(bp->blk_phys_birth);
874 }
34dc7c2f
BB
875 }
876 }
877 }
572e2857 878 DB_DNODE_EXIT(db);
34dc7c2f
BB
879}
880#endif
881
0c66c32d
JG
882static void
883dbuf_clear_data(dmu_buf_impl_t *db)
884{
885 ASSERT(MUTEX_HELD(&db->db_mtx));
886 dbuf_evict_user(db);
d3c2ae1c 887 ASSERT3P(db->db_buf, ==, NULL);
0c66c32d
JG
888 db->db.db_data = NULL;
889 if (db->db_state != DB_NOFILL)
890 db->db_state = DB_UNCACHED;
891}
892
34dc7c2f
BB
893static void
894dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf)
895{
896 ASSERT(MUTEX_HELD(&db->db_mtx));
0c66c32d
JG
897 ASSERT(buf != NULL);
898
34dc7c2f 899 db->db_buf = buf;
0c66c32d
JG
900 ASSERT(buf->b_data != NULL);
901 db->db.db_data = buf->b_data;
34dc7c2f
BB
902}
903
428870ff
BB
904/*
905 * Loan out an arc_buf for read. Return the loaned arc_buf.
906 */
907arc_buf_t *
908dbuf_loan_arcbuf(dmu_buf_impl_t *db)
909{
910 arc_buf_t *abuf;
911
d3c2ae1c 912 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
428870ff
BB
913 mutex_enter(&db->db_mtx);
914 if (arc_released(db->db_buf) || refcount_count(&db->db_holds) > 1) {
915 int blksz = db->db.db_size;
b0bc7a84 916 spa_t *spa = db->db_objset->os_spa;
572e2857 917
428870ff 918 mutex_exit(&db->db_mtx);
2aa34383 919 abuf = arc_loan_buf(spa, B_FALSE, blksz);
428870ff
BB
920 bcopy(db->db.db_data, abuf->b_data, blksz);
921 } else {
922 abuf = db->db_buf;
923 arc_loan_inuse_buf(abuf, db);
d3c2ae1c 924 db->db_buf = NULL;
0c66c32d 925 dbuf_clear_data(db);
428870ff
BB
926 mutex_exit(&db->db_mtx);
927 }
928 return (abuf);
929}
930
fcff0f35
PD
931/*
932 * Calculate which level n block references the data at the level 0 offset
933 * provided.
934 */
34dc7c2f 935uint64_t
031d7c2f 936dbuf_whichblock(const dnode_t *dn, const int64_t level, const uint64_t offset)
34dc7c2f 937{
fcff0f35
PD
938 if (dn->dn_datablkshift != 0 && dn->dn_indblkshift != 0) {
939 /*
940 * The level n blkid is equal to the level 0 blkid divided by
941 * the number of level 0s in a level n block.
942 *
943 * The level 0 blkid is offset >> datablkshift =
944 * offset / 2^datablkshift.
945 *
946 * The number of level 0s in a level n is the number of block
947 * pointers in an indirect block, raised to the power of level.
948 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
949 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
950 *
951 * Thus, the level n blkid is: offset /
952 * ((2^datablkshift)*(2^(level*(indblkshift - SPA_BLKPTRSHIFT)))
953 * = offset / 2^(datablkshift + level *
954 * (indblkshift - SPA_BLKPTRSHIFT))
955 * = offset >> (datablkshift + level *
956 * (indblkshift - SPA_BLKPTRSHIFT))
957 */
031d7c2f
GN
958
959 const unsigned exp = dn->dn_datablkshift +
960 level * (dn->dn_indblkshift - SPA_BLKPTRSHIFT);
961
962 if (exp >= 8 * sizeof (offset)) {
963 /* This only happens on the highest indirection level */
964 ASSERT3U(level, ==, dn->dn_nlevels - 1);
965 return (0);
966 }
967
968 ASSERT3U(exp, <, 8 * sizeof (offset));
969
970 return (offset >> exp);
34dc7c2f
BB
971 } else {
972 ASSERT3U(offset, <, dn->dn_datablksz);
973 return (0);
974 }
975}
976
977static void
b5256303 978dbuf_read_done(zio_t *zio, int err, arc_buf_t *buf, void *vdb)
34dc7c2f
BB
979{
980 dmu_buf_impl_t *db = vdb;
981
982 mutex_enter(&db->db_mtx);
983 ASSERT3U(db->db_state, ==, DB_READ);
984 /*
985 * All reads are synchronous, so we must have a hold on the dbuf
986 */
987 ASSERT(refcount_count(&db->db_holds) > 0);
988 ASSERT(db->db_buf == NULL);
989 ASSERT(db->db.db_data == NULL);
990 if (db->db_level == 0 && db->db_freed_in_flight) {
991 /* we were freed in flight; disregard any error */
992 arc_release(buf, db);
993 bzero(buf->b_data, db->db.db_size);
994 arc_buf_freeze(buf);
995 db->db_freed_in_flight = FALSE;
996 dbuf_set_data(db, buf);
997 db->db_state = DB_CACHED;
b5256303 998 } else if (err == 0) {
34dc7c2f
BB
999 dbuf_set_data(db, buf);
1000 db->db_state = DB_CACHED;
1001 } else {
428870ff 1002 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
34dc7c2f 1003 ASSERT3P(db->db_buf, ==, NULL);
d3c2ae1c 1004 arc_buf_destroy(buf, db);
34dc7c2f
BB
1005 db->db_state = DB_UNCACHED;
1006 }
1007 cv_broadcast(&db->db_changed);
428870ff 1008 dbuf_rele_and_unlock(db, NULL);
34dc7c2f
BB
1009}
1010
5f6d0b6f 1011static int
7f60329a 1012dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
34dc7c2f 1013{
572e2857 1014 dnode_t *dn;
5dbd68a3 1015 zbookmark_phys_t zb;
2a432414 1016 uint32_t aflags = ARC_FLAG_NOWAIT;
b5256303 1017 int err, zio_flags = 0;
34dc7c2f 1018
572e2857
BB
1019 DB_DNODE_ENTER(db);
1020 dn = DB_DNODE(db);
34dc7c2f
BB
1021 ASSERT(!refcount_is_zero(&db->db_holds));
1022 /* We need the struct_rwlock to prevent db_blkptr from changing. */
b128c09f 1023 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
34dc7c2f
BB
1024 ASSERT(MUTEX_HELD(&db->db_mtx));
1025 ASSERT(db->db_state == DB_UNCACHED);
1026 ASSERT(db->db_buf == NULL);
1027
428870ff 1028 if (db->db_blkid == DMU_BONUS_BLKID) {
50c957f7
NB
1029 /*
1030 * The bonus length stored in the dnode may be less than
1031 * the maximum available space in the bonus buffer.
1032 */
9babb374 1033 int bonuslen = MIN(dn->dn_bonuslen, dn->dn_phys->dn_bonuslen);
50c957f7 1034 int max_bonuslen = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots);
b5256303
TC
1035 arc_buf_t *dn_buf = (dn->dn_dbuf != NULL) ?
1036 dn->dn_dbuf->db_buf : NULL;
1037
1038 /* if the underlying dnode block is encrypted, decrypt it */
1039 if (dn_buf != NULL && dn->dn_objset->os_encrypted &&
1040 DMU_OT_IS_ENCRYPTED(dn->dn_bonustype) &&
1041 (flags & DB_RF_NO_DECRYPT) == 0 &&
1042 arc_is_encrypted(dn_buf)) {
1043 err = arc_untransform(dn_buf, dn->dn_objset->os_spa,
1044 dmu_objset_id(dn->dn_objset), B_TRUE);
1045 if (err != 0) {
1046 DB_DNODE_EXIT(db);
1047 mutex_exit(&db->db_mtx);
1048 return (err);
1049 }
1050 }
34dc7c2f
BB
1051
1052 ASSERT3U(bonuslen, <=, db->db.db_size);
a3fd9d9e 1053 db->db.db_data = kmem_alloc(max_bonuslen, KM_SLEEP);
25458cbe 1054 arc_space_consume(max_bonuslen, ARC_SPACE_BONUS);
50c957f7
NB
1055 if (bonuslen < max_bonuslen)
1056 bzero(db->db.db_data, max_bonuslen);
9babb374
BB
1057 if (bonuslen)
1058 bcopy(DN_BONUS(dn->dn_phys), db->db.db_data, bonuslen);
572e2857 1059 DB_DNODE_EXIT(db);
34dc7c2f
BB
1060 db->db_state = DB_CACHED;
1061 mutex_exit(&db->db_mtx);
5f6d0b6f 1062 return (0);
34dc7c2f
BB
1063 }
1064
b128c09f
BB
1065 /*
1066 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
1067 * processes the delete record and clears the bp while we are waiting
1068 * for the dn_mtx (resulting in a "no" from block_freed).
1069 */
1070 if (db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr) ||
1071 (db->db_level == 0 && (dnode_block_freed(dn, db->db_blkid) ||
1072 BP_IS_HOLE(db->db_blkptr)))) {
34dc7c2f
BB
1073 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1074
2aa34383
DK
1075 dbuf_set_data(db, arc_alloc_buf(db->db_objset->os_spa, db, type,
1076 db->db.db_size));
34dc7c2f 1077 bzero(db->db.db_data, db->db.db_size);
bc77ba73
PD
1078
1079 if (db->db_blkptr != NULL && db->db_level > 0 &&
1080 BP_IS_HOLE(db->db_blkptr) &&
1081 db->db_blkptr->blk_birth != 0) {
1082 blkptr_t *bps = db->db.db_data;
1083 int i;
1084 for (i = 0; i < ((1 <<
1085 DB_DNODE(db)->dn_indblkshift) / sizeof (blkptr_t));
1086 i++) {
1087 blkptr_t *bp = &bps[i];
1088 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
1089 1 << dn->dn_indblkshift);
1090 BP_SET_LSIZE(bp,
1091 BP_GET_LEVEL(db->db_blkptr) == 1 ?
1092 dn->dn_datablksz :
1093 BP_GET_LSIZE(db->db_blkptr));
1094 BP_SET_TYPE(bp, BP_GET_TYPE(db->db_blkptr));
1095 BP_SET_LEVEL(bp,
1096 BP_GET_LEVEL(db->db_blkptr) - 1);
1097 BP_SET_BIRTH(bp, db->db_blkptr->blk_birth, 0);
1098 }
1099 }
1100 DB_DNODE_EXIT(db);
34dc7c2f 1101 db->db_state = DB_CACHED;
34dc7c2f 1102 mutex_exit(&db->db_mtx);
5f6d0b6f 1103 return (0);
34dc7c2f
BB
1104 }
1105
572e2857
BB
1106 DB_DNODE_EXIT(db);
1107
34dc7c2f
BB
1108 db->db_state = DB_READ;
1109 mutex_exit(&db->db_mtx);
1110
b128c09f 1111 if (DBUF_IS_L2CACHEABLE(db))
2a432414 1112 aflags |= ARC_FLAG_L2CACHE;
b128c09f 1113
428870ff
BB
1114 SET_BOOKMARK(&zb, db->db_objset->os_dsl_dataset ?
1115 db->db_objset->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1116 db->db.db_object, db->db_level, db->db_blkid);
34dc7c2f 1117
b5256303
TC
1118 /*
1119 * All bps of an encrypted os should have the encryption bit set.
1120 * If this is not true it indicates tampering and we report an error.
1121 */
1122 if (db->db_objset->os_encrypted && !BP_USES_CRYPT(db->db_blkptr)) {
1123 spa_log_error(db->db_objset->os_spa, &zb);
1124 zfs_panic_recover("unencrypted block in encrypted "
1125 "object set %llu", dmu_objset_id(db->db_objset));
1126 return (SET_ERROR(EIO));
1127 }
1128
34dc7c2f 1129 dbuf_add_ref(db, NULL);
b128c09f 1130
b5256303
TC
1131 zio_flags = (flags & DB_RF_CANFAIL) ?
1132 ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED;
1133
1134 if ((flags & DB_RF_NO_DECRYPT) && BP_IS_PROTECTED(db->db_blkptr))
1135 zio_flags |= ZIO_FLAG_RAW;
1136
5f6d0b6f 1137 err = arc_read(zio, db->db_objset->os_spa, db->db_blkptr,
b5256303 1138 dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ, zio_flags,
34dc7c2f 1139 &aflags, &zb);
5f6d0b6f 1140
da8d5748 1141 return (err);
34dc7c2f
BB
1142}
1143
2aa34383
DK
1144/*
1145 * This is our just-in-time copy function. It makes a copy of buffers that
1146 * have been modified in a previous transaction group before we access them in
1147 * the current active group.
1148 *
1149 * This function is used in three places: when we are dirtying a buffer for the
1150 * first time in a txg, when we are freeing a range in a dnode that includes
1151 * this buffer, and when we are accessing a buffer which was received compressed
1152 * and later referenced in a WRITE_BYREF record.
1153 *
1154 * Note that when we are called from dbuf_free_range() we do not put a hold on
1155 * the buffer, we just traverse the active dbuf list for the dnode.
1156 */
1157static void
1158dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg)
1159{
1160 dbuf_dirty_record_t *dr = db->db_last_dirty;
1161
1162 ASSERT(MUTEX_HELD(&db->db_mtx));
1163 ASSERT(db->db.db_data != NULL);
1164 ASSERT(db->db_level == 0);
1165 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT);
1166
1167 if (dr == NULL ||
1168 (dr->dt.dl.dr_data !=
1169 ((db->db_blkid == DMU_BONUS_BLKID) ? db->db.db_data : db->db_buf)))
1170 return;
1171
1172 /*
1173 * If the last dirty record for this dbuf has not yet synced
1174 * and its referencing the dbuf data, either:
1175 * reset the reference to point to a new copy,
1176 * or (if there a no active holders)
1177 * just null out the current db_data pointer.
1178 */
4807c0ba 1179 ASSERT3U(dr->dr_txg, >=, txg - 2);
2aa34383 1180 if (db->db_blkid == DMU_BONUS_BLKID) {
2aa34383
DK
1181 dnode_t *dn = DB_DNODE(db);
1182 int bonuslen = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots);
a3fd9d9e 1183 dr->dt.dl.dr_data = kmem_alloc(bonuslen, KM_SLEEP);
2aa34383
DK
1184 arc_space_consume(bonuslen, ARC_SPACE_BONUS);
1185 bcopy(db->db.db_data, dr->dt.dl.dr_data, bonuslen);
1186 } else if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
b5256303 1187 dnode_t *dn = DB_DNODE(db);
2aa34383
DK
1188 int size = arc_buf_size(db->db_buf);
1189 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1190 spa_t *spa = db->db_objset->os_spa;
1191 enum zio_compress compress_type =
1192 arc_get_compression(db->db_buf);
1193
b5256303
TC
1194 if (arc_is_encrypted(db->db_buf)) {
1195 boolean_t byteorder;
1196 uint8_t salt[ZIO_DATA_SALT_LEN];
1197 uint8_t iv[ZIO_DATA_IV_LEN];
1198 uint8_t mac[ZIO_DATA_MAC_LEN];
1199
1200 arc_get_raw_params(db->db_buf, &byteorder, salt,
1201 iv, mac);
1202 dr->dt.dl.dr_data = arc_alloc_raw_buf(spa, db,
1203 dmu_objset_id(dn->dn_objset), byteorder, salt, iv,
1204 mac, dn->dn_type, size, arc_buf_lsize(db->db_buf),
1205 compress_type);
1206 } else if (compress_type != ZIO_COMPRESS_OFF) {
2aa34383
DK
1207 ASSERT3U(type, ==, ARC_BUFC_DATA);
1208 dr->dt.dl.dr_data = arc_alloc_compressed_buf(spa, db,
1209 size, arc_buf_lsize(db->db_buf), compress_type);
b5256303
TC
1210 } else {
1211 dr->dt.dl.dr_data = arc_alloc_buf(spa, db, type, size);
2aa34383
DK
1212 }
1213 bcopy(db->db.db_data, dr->dt.dl.dr_data->b_data, size);
1214 } else {
1215 db->db_buf = NULL;
1216 dbuf_clear_data(db);
1217 }
1218}
1219
34dc7c2f
BB
1220int
1221dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
1222{
1223 int err = 0;
b0bc7a84 1224 boolean_t prefetch;
572e2857 1225 dnode_t *dn;
34dc7c2f
BB
1226
1227 /*
1228 * We don't have to hold the mutex to check db_state because it
1229 * can't be freed while we have a hold on the buffer.
1230 */
1231 ASSERT(!refcount_is_zero(&db->db_holds));
1232
b128c09f 1233 if (db->db_state == DB_NOFILL)
2e528b49 1234 return (SET_ERROR(EIO));
b128c09f 1235
572e2857
BB
1236 DB_DNODE_ENTER(db);
1237 dn = DB_DNODE(db);
34dc7c2f 1238 if ((flags & DB_RF_HAVESTRUCT) == 0)
572e2857 1239 rw_enter(&dn->dn_struct_rwlock, RW_READER);
34dc7c2f 1240
428870ff 1241 prefetch = db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
572e2857 1242 (flags & DB_RF_NOPREFETCH) == 0 && dn != NULL &&
b128c09f 1243 DBUF_IS_CACHEABLE(db);
34dc7c2f
BB
1244
1245 mutex_enter(&db->db_mtx);
1246 if (db->db_state == DB_CACHED) {
b5256303
TC
1247 spa_t *spa = dn->dn_objset->os_spa;
1248
2aa34383 1249 /*
b5256303
TC
1250 * If the arc buf is compressed or encrypted, we need to
1251 * untransform it to read the data. This could happen during
1252 * the "zfs receive" of a stream which is deduplicated and
1253 * either raw or compressed. We do not need to do this if the
1254 * caller wants raw encrypted data.
2aa34383 1255 */
b5256303
TC
1256 if (db->db_buf != NULL && (flags & DB_RF_NO_DECRYPT) == 0 &&
1257 (arc_is_encrypted(db->db_buf) ||
1258 arc_get_compression(db->db_buf) != ZIO_COMPRESS_OFF)) {
1259 dbuf_fix_old_data(db, spa_syncing_txg(spa));
1260 err = arc_untransform(db->db_buf, spa,
1261 dmu_objset_id(db->db_objset), B_FALSE);
2aa34383
DK
1262 dbuf_set_data(db, db->db_buf);
1263 }
34dc7c2f
BB
1264 mutex_exit(&db->db_mtx);
1265 if (prefetch)
755065f3 1266 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE);
34dc7c2f 1267 if ((flags & DB_RF_HAVESTRUCT) == 0)
572e2857
BB
1268 rw_exit(&dn->dn_struct_rwlock);
1269 DB_DNODE_EXIT(db);
34dc7c2f 1270 } else if (db->db_state == DB_UNCACHED) {
572e2857 1271 spa_t *spa = dn->dn_objset->os_spa;
a0043383 1272 boolean_t need_wait = B_FALSE;
572e2857 1273
b0319c1f 1274 if (zio == NULL &&
a0043383 1275 db->db_blkptr != NULL && !BP_IS_HOLE(db->db_blkptr)) {
572e2857 1276 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
a0043383
MA
1277 need_wait = B_TRUE;
1278 }
7f60329a 1279 err = dbuf_read_impl(db, zio, flags);
34dc7c2f
BB
1280
1281 /* dbuf_read_impl has dropped db_mtx for us */
1282
5f6d0b6f 1283 if (!err && prefetch)
755065f3 1284 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE);
34dc7c2f
BB
1285
1286 if ((flags & DB_RF_HAVESTRUCT) == 0)
572e2857
BB
1287 rw_exit(&dn->dn_struct_rwlock);
1288 DB_DNODE_EXIT(db);
34dc7c2f 1289
a0043383 1290 if (!err && need_wait)
34dc7c2f
BB
1291 err = zio_wait(zio);
1292 } else {
e49f1e20
WA
1293 /*
1294 * Another reader came in while the dbuf was in flight
1295 * between UNCACHED and CACHED. Either a writer will finish
1296 * writing the buffer (sending the dbuf to CACHED) or the
1297 * first reader's request will reach the read_done callback
1298 * and send the dbuf to CACHED. Otherwise, a failure
1299 * occurred and the dbuf went to UNCACHED.
1300 */
34dc7c2f
BB
1301 mutex_exit(&db->db_mtx);
1302 if (prefetch)
755065f3 1303 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE);
34dc7c2f 1304 if ((flags & DB_RF_HAVESTRUCT) == 0)
572e2857
BB
1305 rw_exit(&dn->dn_struct_rwlock);
1306 DB_DNODE_EXIT(db);
34dc7c2f 1307
e49f1e20 1308 /* Skip the wait per the caller's request. */
34dc7c2f
BB
1309 mutex_enter(&db->db_mtx);
1310 if ((flags & DB_RF_NEVERWAIT) == 0) {
1311 while (db->db_state == DB_READ ||
1312 db->db_state == DB_FILL) {
1313 ASSERT(db->db_state == DB_READ ||
1314 (flags & DB_RF_HAVESTRUCT) == 0);
64dbba36
AL
1315 DTRACE_PROBE2(blocked__read, dmu_buf_impl_t *,
1316 db, zio_t *, zio);
34dc7c2f
BB
1317 cv_wait(&db->db_changed, &db->db_mtx);
1318 }
1319 if (db->db_state == DB_UNCACHED)
2e528b49 1320 err = SET_ERROR(EIO);
34dc7c2f
BB
1321 }
1322 mutex_exit(&db->db_mtx);
1323 }
1324
34dc7c2f
BB
1325 return (err);
1326}
1327
1328static void
1329dbuf_noread(dmu_buf_impl_t *db)
1330{
1331 ASSERT(!refcount_is_zero(&db->db_holds));
428870ff 1332 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
34dc7c2f
BB
1333 mutex_enter(&db->db_mtx);
1334 while (db->db_state == DB_READ || db->db_state == DB_FILL)
1335 cv_wait(&db->db_changed, &db->db_mtx);
1336 if (db->db_state == DB_UNCACHED) {
1337 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
b0bc7a84 1338 spa_t *spa = db->db_objset->os_spa;
34dc7c2f
BB
1339
1340 ASSERT(db->db_buf == NULL);
1341 ASSERT(db->db.db_data == NULL);
2aa34383 1342 dbuf_set_data(db, arc_alloc_buf(spa, db, type, db->db.db_size));
34dc7c2f 1343 db->db_state = DB_FILL;
b128c09f 1344 } else if (db->db_state == DB_NOFILL) {
0c66c32d 1345 dbuf_clear_data(db);
34dc7c2f
BB
1346 } else {
1347 ASSERT3U(db->db_state, ==, DB_CACHED);
1348 }
1349 mutex_exit(&db->db_mtx);
1350}
1351
34dc7c2f
BB
1352void
1353dbuf_unoverride(dbuf_dirty_record_t *dr)
1354{
1355 dmu_buf_impl_t *db = dr->dr_dbuf;
428870ff 1356 blkptr_t *bp = &dr->dt.dl.dr_overridden_by;
34dc7c2f
BB
1357 uint64_t txg = dr->dr_txg;
1358
1359 ASSERT(MUTEX_HELD(&db->db_mtx));
00710365
AS
1360 /*
1361 * This assert is valid because dmu_sync() expects to be called by
1362 * a zilog's get_data while holding a range lock. This call only
1363 * comes from dbuf_dirty() callers who must also hold a range lock.
1364 */
34dc7c2f
BB
1365 ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC);
1366 ASSERT(db->db_level == 0);
1367
428870ff 1368 if (db->db_blkid == DMU_BONUS_BLKID ||
34dc7c2f
BB
1369 dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN)
1370 return;
1371
428870ff
BB
1372 ASSERT(db->db_data_pending != dr);
1373
34dc7c2f 1374 /* free this block */
b0bc7a84
MG
1375 if (!BP_IS_HOLE(bp) && !dr->dt.dl.dr_nopwrite)
1376 zio_free(db->db_objset->os_spa, txg, bp);
428870ff 1377
34dc7c2f 1378 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
03c6040b 1379 dr->dt.dl.dr_nopwrite = B_FALSE;
b5256303 1380 dr->dt.dl.dr_raw = B_FALSE;
03c6040b 1381
34dc7c2f
BB
1382 /*
1383 * Release the already-written buffer, so we leave it in
1384 * a consistent dirty state. Note that all callers are
1385 * modifying the buffer, so they will immediately do
1386 * another (redundant) arc_release(). Therefore, leave
1387 * the buf thawed to save the effort of freezing &
1388 * immediately re-thawing it.
1389 */
1390 arc_release(dr->dt.dl.dr_data, db);
1391}
1392
b128c09f
BB
1393/*
1394 * Evict (if its unreferenced) or clear (if its referenced) any level-0
1395 * data blocks in the free range, so that any future readers will find
b0bc7a84 1396 * empty blocks.
b128c09f 1397 */
34dc7c2f 1398void
8951cb8d
AR
1399dbuf_free_range(dnode_t *dn, uint64_t start_blkid, uint64_t end_blkid,
1400 dmu_tx_t *tx)
34dc7c2f 1401{
0c66c32d
JG
1402 dmu_buf_impl_t *db_search;
1403 dmu_buf_impl_t *db, *db_next;
34dc7c2f 1404 uint64_t txg = tx->tx_txg;
8951cb8d 1405 avl_index_t where;
8951cb8d 1406
9c9531cb
GM
1407 if (end_blkid > dn->dn_maxblkid &&
1408 !(start_blkid == DMU_SPILL_BLKID || end_blkid == DMU_SPILL_BLKID))
8951cb8d
AR
1409 end_blkid = dn->dn_maxblkid;
1410 dprintf_dnode(dn, "start=%llu end=%llu\n", start_blkid, end_blkid);
34dc7c2f 1411
0c66c32d 1412 db_search = kmem_alloc(sizeof (dmu_buf_impl_t), KM_SLEEP);
8951cb8d
AR
1413 db_search->db_level = 0;
1414 db_search->db_blkid = start_blkid;
9925c28c 1415 db_search->db_state = DB_SEARCH;
ea97f8ce 1416
b663a23d 1417 mutex_enter(&dn->dn_dbufs_mtx);
8951cb8d
AR
1418 db = avl_find(&dn->dn_dbufs, db_search, &where);
1419 ASSERT3P(db, ==, NULL);
9c9531cb 1420
8951cb8d
AR
1421 db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER);
1422
1423 for (; db != NULL; db = db_next) {
1424 db_next = AVL_NEXT(&dn->dn_dbufs, db);
428870ff 1425 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
b128c09f 1426
8951cb8d
AR
1427 if (db->db_level != 0 || db->db_blkid > end_blkid) {
1428 break;
1429 }
1430 ASSERT3U(db->db_blkid, >=, start_blkid);
34dc7c2f
BB
1431
1432 /* found a level 0 buffer in the range */
13fe0198
MA
1433 mutex_enter(&db->db_mtx);
1434 if (dbuf_undirty(db, tx)) {
1435 /* mutex has been dropped and dbuf destroyed */
34dc7c2f 1436 continue;
13fe0198 1437 }
34dc7c2f 1438
34dc7c2f 1439 if (db->db_state == DB_UNCACHED ||
b128c09f 1440 db->db_state == DB_NOFILL ||
34dc7c2f
BB
1441 db->db_state == DB_EVICTING) {
1442 ASSERT(db->db.db_data == NULL);
1443 mutex_exit(&db->db_mtx);
1444 continue;
1445 }
1446 if (db->db_state == DB_READ || db->db_state == DB_FILL) {
1447 /* will be handled in dbuf_read_done or dbuf_rele */
1448 db->db_freed_in_flight = TRUE;
1449 mutex_exit(&db->db_mtx);
1450 continue;
1451 }
1452 if (refcount_count(&db->db_holds) == 0) {
1453 ASSERT(db->db_buf);
d3c2ae1c 1454 dbuf_destroy(db);
34dc7c2f
BB
1455 continue;
1456 }
1457 /* The dbuf is referenced */
1458
1459 if (db->db_last_dirty != NULL) {
1460 dbuf_dirty_record_t *dr = db->db_last_dirty;
1461
1462 if (dr->dr_txg == txg) {
1463 /*
1464 * This buffer is "in-use", re-adjust the file
1465 * size to reflect that this buffer may
1466 * contain new data when we sync.
1467 */
428870ff
BB
1468 if (db->db_blkid != DMU_SPILL_BLKID &&
1469 db->db_blkid > dn->dn_maxblkid)
34dc7c2f
BB
1470 dn->dn_maxblkid = db->db_blkid;
1471 dbuf_unoverride(dr);
1472 } else {
1473 /*
1474 * This dbuf is not dirty in the open context.
1475 * Either uncache it (if its not referenced in
1476 * the open context) or reset its contents to
1477 * empty.
1478 */
1479 dbuf_fix_old_data(db, txg);
1480 }
1481 }
1482 /* clear the contents if its cached */
1483 if (db->db_state == DB_CACHED) {
1484 ASSERT(db->db.db_data != NULL);
1485 arc_release(db->db_buf, db);
1486 bzero(db->db.db_data, db->db.db_size);
1487 arc_buf_freeze(db->db_buf);
1488 }
1489
1490 mutex_exit(&db->db_mtx);
1491 }
8951cb8d 1492
8951cb8d 1493 kmem_free(db_search, sizeof (dmu_buf_impl_t));
34dc7c2f
BB
1494 mutex_exit(&dn->dn_dbufs_mtx);
1495}
1496
34dc7c2f
BB
1497void
1498dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx)
1499{
1500 arc_buf_t *buf, *obuf;
1501 int osize = db->db.db_size;
1502 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
572e2857 1503 dnode_t *dn;
34dc7c2f 1504
428870ff 1505 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
34dc7c2f 1506
572e2857
BB
1507 DB_DNODE_ENTER(db);
1508 dn = DB_DNODE(db);
1509
34dc7c2f 1510 /* XXX does *this* func really need the lock? */
572e2857 1511 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
34dc7c2f
BB
1512
1513 /*
b0bc7a84 1514 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
34dc7c2f
BB
1515 * is OK, because there can be no other references to the db
1516 * when we are changing its size, so no concurrent DB_FILL can
1517 * be happening.
1518 */
1519 /*
1520 * XXX we should be doing a dbuf_read, checking the return
1521 * value and returning that up to our callers
1522 */
b0bc7a84 1523 dmu_buf_will_dirty(&db->db, tx);
34dc7c2f
BB
1524
1525 /* create the data buffer for the new block */
2aa34383 1526 buf = arc_alloc_buf(dn->dn_objset->os_spa, db, type, size);
34dc7c2f
BB
1527
1528 /* copy old block data to the new block */
1529 obuf = db->db_buf;
1530 bcopy(obuf->b_data, buf->b_data, MIN(osize, size));
1531 /* zero the remainder */
1532 if (size > osize)
1533 bzero((uint8_t *)buf->b_data + osize, size - osize);
1534
1535 mutex_enter(&db->db_mtx);
1536 dbuf_set_data(db, buf);
d3c2ae1c 1537 arc_buf_destroy(obuf, db);
34dc7c2f
BB
1538 db->db.db_size = size;
1539
1540 if (db->db_level == 0) {
1541 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
1542 db->db_last_dirty->dt.dl.dr_data = buf;
1543 }
1544 mutex_exit(&db->db_mtx);
1545
3ec3bc21 1546 dmu_objset_willuse_space(dn->dn_objset, size - osize, tx);
572e2857 1547 DB_DNODE_EXIT(db);
34dc7c2f
BB
1548}
1549
428870ff
BB
1550void
1551dbuf_release_bp(dmu_buf_impl_t *db)
1552{
b0bc7a84 1553 ASSERTV(objset_t *os = db->db_objset);
428870ff
BB
1554
1555 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
1556 ASSERT(arc_released(os->os_phys_buf) ||
1557 list_link_active(&os->os_dsl_dataset->ds_synced_link));
1558 ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf));
1559
294f6806 1560 (void) arc_release(db->db_buf, db);
428870ff
BB
1561}
1562
5a28a973
MA
1563/*
1564 * We already have a dirty record for this TXG, and we are being
1565 * dirtied again.
1566 */
1567static void
1568dbuf_redirty(dbuf_dirty_record_t *dr)
1569{
1570 dmu_buf_impl_t *db = dr->dr_dbuf;
1571
1572 ASSERT(MUTEX_HELD(&db->db_mtx));
1573
1574 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID) {
1575 /*
1576 * If this buffer has already been written out,
1577 * we now need to reset its state.
1578 */
1579 dbuf_unoverride(dr);
1580 if (db->db.db_object != DMU_META_DNODE_OBJECT &&
1581 db->db_state != DB_NOFILL) {
1582 /* Already released on initial dirty, so just thaw. */
1583 ASSERT(arc_released(db->db_buf));
1584 arc_buf_thaw(db->db_buf);
1585 }
1586 }
1587}
1588
34dc7c2f
BB
1589dbuf_dirty_record_t *
1590dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1591{
572e2857
BB
1592 dnode_t *dn;
1593 objset_t *os;
34dc7c2f
BB
1594 dbuf_dirty_record_t **drp, *dr;
1595 int drop_struct_lock = FALSE;
1596 int txgoff = tx->tx_txg & TXG_MASK;
1597
1598 ASSERT(tx->tx_txg != 0);
1599 ASSERT(!refcount_is_zero(&db->db_holds));
1600 DMU_TX_DIRTY_BUF(tx, db);
1601
572e2857
BB
1602 DB_DNODE_ENTER(db);
1603 dn = DB_DNODE(db);
34dc7c2f
BB
1604 /*
1605 * Shouldn't dirty a regular buffer in syncing context. Private
1606 * objects may be dirtied in syncing context, but only if they
1607 * were already pre-dirtied in open context.
34dc7c2f 1608 */
cc9bb3e5
GM
1609#ifdef DEBUG
1610 if (dn->dn_objset->os_dsl_dataset != NULL) {
1611 rrw_enter(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock,
1612 RW_READER, FTAG);
1613 }
34dc7c2f
BB
1614 ASSERT(!dmu_tx_is_syncing(tx) ||
1615 BP_IS_HOLE(dn->dn_objset->os_rootbp) ||
9babb374
BB
1616 DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1617 dn->dn_objset->os_dsl_dataset == NULL);
cc9bb3e5
GM
1618 if (dn->dn_objset->os_dsl_dataset != NULL)
1619 rrw_exit(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock, FTAG);
1620#endif
34dc7c2f
BB
1621 /*
1622 * We make this assert for private objects as well, but after we
1623 * check if we're already dirty. They are allowed to re-dirty
1624 * in syncing context.
1625 */
1626 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1627 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1628 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1629
1630 mutex_enter(&db->db_mtx);
1631 /*
1632 * XXX make this true for indirects too? The problem is that
1633 * transactions created with dmu_tx_create_assigned() from
1634 * syncing context don't bother holding ahead.
1635 */
1636 ASSERT(db->db_level != 0 ||
b128c09f
BB
1637 db->db_state == DB_CACHED || db->db_state == DB_FILL ||
1638 db->db_state == DB_NOFILL);
34dc7c2f
BB
1639
1640 mutex_enter(&dn->dn_mtx);
1641 /*
1642 * Don't set dirtyctx to SYNC if we're just modifying this as we
1643 * initialize the objset.
1644 */
cc9bb3e5
GM
1645 if (dn->dn_dirtyctx == DN_UNDIRTIED) {
1646 if (dn->dn_objset->os_dsl_dataset != NULL) {
1647 rrw_enter(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock,
1648 RW_READER, FTAG);
1649 }
1650 if (!BP_IS_HOLE(dn->dn_objset->os_rootbp)) {
1651 dn->dn_dirtyctx = (dmu_tx_is_syncing(tx) ?
1652 DN_DIRTY_SYNC : DN_DIRTY_OPEN);
1653 ASSERT(dn->dn_dirtyctx_firstset == NULL);
1654 dn->dn_dirtyctx_firstset = kmem_alloc(1, KM_SLEEP);
1655 }
1656 if (dn->dn_objset->os_dsl_dataset != NULL) {
1657 rrw_exit(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock,
1658 FTAG);
1659 }
34dc7c2f
BB
1660 }
1661 mutex_exit(&dn->dn_mtx);
1662
428870ff
BB
1663 if (db->db_blkid == DMU_SPILL_BLKID)
1664 dn->dn_have_spill = B_TRUE;
1665
34dc7c2f
BB
1666 /*
1667 * If this buffer is already dirty, we're done.
1668 */
1669 drp = &db->db_last_dirty;
1670 ASSERT(*drp == NULL || (*drp)->dr_txg <= tx->tx_txg ||
1671 db->db.db_object == DMU_META_DNODE_OBJECT);
1672 while ((dr = *drp) != NULL && dr->dr_txg > tx->tx_txg)
1673 drp = &dr->dr_next;
1674 if (dr && dr->dr_txg == tx->tx_txg) {
572e2857
BB
1675 DB_DNODE_EXIT(db);
1676
5a28a973 1677 dbuf_redirty(dr);
34dc7c2f
BB
1678 mutex_exit(&db->db_mtx);
1679 return (dr);
1680 }
1681
1682 /*
1683 * Only valid if not already dirty.
1684 */
9babb374
BB
1685 ASSERT(dn->dn_object == 0 ||
1686 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
34dc7c2f
BB
1687 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1688
1689 ASSERT3U(dn->dn_nlevels, >, db->db_level);
34dc7c2f
BB
1690
1691 /*
1692 * We should only be dirtying in syncing context if it's the
9babb374
BB
1693 * mos or we're initializing the os or it's a special object.
1694 * However, we are allowed to dirty in syncing context provided
1695 * we already dirtied it in open context. Hence we must make
1696 * this assertion only if we're not already dirty.
34dc7c2f 1697 */
572e2857 1698 os = dn->dn_objset;
3b7f360c 1699 VERIFY3U(tx->tx_txg, <=, spa_final_dirty_txg(os->os_spa));
cc9bb3e5
GM
1700#ifdef DEBUG
1701 if (dn->dn_objset->os_dsl_dataset != NULL)
1702 rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_READER, FTAG);
9babb374
BB
1703 ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1704 os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp));
cc9bb3e5
GM
1705 if (dn->dn_objset->os_dsl_dataset != NULL)
1706 rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG);
1707#endif
34dc7c2f
BB
1708 ASSERT(db->db.db_size != 0);
1709
1710 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1711
428870ff 1712 if (db->db_blkid != DMU_BONUS_BLKID) {
3ec3bc21 1713 dmu_objset_willuse_space(os, db->db.db_size, tx);
34dc7c2f
BB
1714 }
1715
1716 /*
1717 * If this buffer is dirty in an old transaction group we need
1718 * to make a copy of it so that the changes we make in this
1719 * transaction group won't leak out when we sync the older txg.
1720 */
79c76d5b 1721 dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP);
98f72a53 1722 list_link_init(&dr->dr_dirty_node);
34dc7c2f
BB
1723 if (db->db_level == 0) {
1724 void *data_old = db->db_buf;
1725
b128c09f 1726 if (db->db_state != DB_NOFILL) {
428870ff 1727 if (db->db_blkid == DMU_BONUS_BLKID) {
b128c09f
BB
1728 dbuf_fix_old_data(db, tx->tx_txg);
1729 data_old = db->db.db_data;
1730 } else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
1731 /*
1732 * Release the data buffer from the cache so
1733 * that we can modify it without impacting
1734 * possible other users of this cached data
1735 * block. Note that indirect blocks and
1736 * private objects are not released until the
1737 * syncing state (since they are only modified
1738 * then).
1739 */
1740 arc_release(db->db_buf, db);
1741 dbuf_fix_old_data(db, tx->tx_txg);
1742 data_old = db->db_buf;
1743 }
1744 ASSERT(data_old != NULL);
34dc7c2f 1745 }
34dc7c2f
BB
1746 dr->dt.dl.dr_data = data_old;
1747 } else {
448d7aaa 1748 mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_NOLOCKDEP, NULL);
34dc7c2f
BB
1749 list_create(&dr->dt.di.dr_children,
1750 sizeof (dbuf_dirty_record_t),
1751 offsetof(dbuf_dirty_record_t, dr_dirty_node));
1752 }
e8b96c60
MA
1753 if (db->db_blkid != DMU_BONUS_BLKID && os->os_dsl_dataset != NULL)
1754 dr->dr_accounted = db->db.db_size;
34dc7c2f
BB
1755 dr->dr_dbuf = db;
1756 dr->dr_txg = tx->tx_txg;
1757 dr->dr_next = *drp;
1758 *drp = dr;
1759
1760 /*
1761 * We could have been freed_in_flight between the dbuf_noread
1762 * and dbuf_dirty. We win, as though the dbuf_noread() had
1763 * happened after the free.
1764 */
428870ff
BB
1765 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1766 db->db_blkid != DMU_SPILL_BLKID) {
34dc7c2f 1767 mutex_enter(&dn->dn_mtx);
9bd274dd
MA
1768 if (dn->dn_free_ranges[txgoff] != NULL) {
1769 range_tree_clear(dn->dn_free_ranges[txgoff],
1770 db->db_blkid, 1);
1771 }
34dc7c2f
BB
1772 mutex_exit(&dn->dn_mtx);
1773 db->db_freed_in_flight = FALSE;
1774 }
1775
1776 /*
1777 * This buffer is now part of this txg
1778 */
1779 dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg);
1780 db->db_dirtycnt += 1;
1781 ASSERT3U(db->db_dirtycnt, <=, 3);
1782
1783 mutex_exit(&db->db_mtx);
1784
428870ff
BB
1785 if (db->db_blkid == DMU_BONUS_BLKID ||
1786 db->db_blkid == DMU_SPILL_BLKID) {
34dc7c2f
BB
1787 mutex_enter(&dn->dn_mtx);
1788 ASSERT(!list_link_active(&dr->dr_dirty_node));
1789 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1790 mutex_exit(&dn->dn_mtx);
1791 dnode_setdirty(dn, tx);
572e2857 1792 DB_DNODE_EXIT(db);
34dc7c2f 1793 return (dr);
98ace739
MA
1794 }
1795
1796 /*
1797 * The dn_struct_rwlock prevents db_blkptr from changing
1798 * due to a write from syncing context completing
1799 * while we are running, so we want to acquire it before
1800 * looking at db_blkptr.
1801 */
1802 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
1803 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1804 drop_struct_lock = TRUE;
1805 }
1806
2ade4a99
MA
1807 /*
1808 * We need to hold the dn_struct_rwlock to make this assertion,
1809 * because it protects dn_phys / dn_next_nlevels from changing.
1810 */
1811 ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) ||
1812 dn->dn_phys->dn_nlevels > db->db_level ||
1813 dn->dn_next_nlevels[txgoff] > db->db_level ||
1814 dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level ||
1815 dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level);
1816
3ec3bc21
BB
1817 /*
1818 * If we are overwriting a dedup BP, then unless it is snapshotted,
1819 * when we get to syncing context we will need to decrement its
1820 * refcount in the DDT. Prefetch the relevant DDT block so that
1821 * syncing context won't have to wait for the i/o.
1822 */
1823 ddt_prefetch(os->os_spa, db->db_blkptr);
34dc7c2f 1824
b128c09f
BB
1825 if (db->db_level == 0) {
1826 dnode_new_blkid(dn, db->db_blkid, tx, drop_struct_lock);
1827 ASSERT(dn->dn_maxblkid >= db->db_blkid);
1828 }
1829
34dc7c2f
BB
1830 if (db->db_level+1 < dn->dn_nlevels) {
1831 dmu_buf_impl_t *parent = db->db_parent;
1832 dbuf_dirty_record_t *di;
1833 int parent_held = FALSE;
1834
1835 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
1836 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1837
1838 parent = dbuf_hold_level(dn, db->db_level+1,
1839 db->db_blkid >> epbs, FTAG);
428870ff 1840 ASSERT(parent != NULL);
34dc7c2f
BB
1841 parent_held = TRUE;
1842 }
1843 if (drop_struct_lock)
1844 rw_exit(&dn->dn_struct_rwlock);
1845 ASSERT3U(db->db_level+1, ==, parent->db_level);
1846 di = dbuf_dirty(parent, tx);
1847 if (parent_held)
1848 dbuf_rele(parent, FTAG);
1849
1850 mutex_enter(&db->db_mtx);
e8b96c60
MA
1851 /*
1852 * Since we've dropped the mutex, it's possible that
1853 * dbuf_undirty() might have changed this out from under us.
1854 */
34dc7c2f
BB
1855 if (db->db_last_dirty == dr ||
1856 dn->dn_object == DMU_META_DNODE_OBJECT) {
1857 mutex_enter(&di->dt.di.dr_mtx);
1858 ASSERT3U(di->dr_txg, ==, tx->tx_txg);
1859 ASSERT(!list_link_active(&dr->dr_dirty_node));
1860 list_insert_tail(&di->dt.di.dr_children, dr);
1861 mutex_exit(&di->dt.di.dr_mtx);
1862 dr->dr_parent = di;
1863 }
1864 mutex_exit(&db->db_mtx);
1865 } else {
1866 ASSERT(db->db_level+1 == dn->dn_nlevels);
1867 ASSERT(db->db_blkid < dn->dn_nblkptr);
572e2857 1868 ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf);
34dc7c2f
BB
1869 mutex_enter(&dn->dn_mtx);
1870 ASSERT(!list_link_active(&dr->dr_dirty_node));
1871 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1872 mutex_exit(&dn->dn_mtx);
1873 if (drop_struct_lock)
1874 rw_exit(&dn->dn_struct_rwlock);
1875 }
1876
1877 dnode_setdirty(dn, tx);
572e2857 1878 DB_DNODE_EXIT(db);
34dc7c2f
BB
1879 return (dr);
1880}
1881
13fe0198 1882/*
e49f1e20
WA
1883 * Undirty a buffer in the transaction group referenced by the given
1884 * transaction. Return whether this evicted the dbuf.
13fe0198
MA
1885 */
1886static boolean_t
34dc7c2f
BB
1887dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1888{
572e2857 1889 dnode_t *dn;
34dc7c2f
BB
1890 uint64_t txg = tx->tx_txg;
1891 dbuf_dirty_record_t *dr, **drp;
1892
1893 ASSERT(txg != 0);
4bda3bd0
MA
1894
1895 /*
1896 * Due to our use of dn_nlevels below, this can only be called
1897 * in open context, unless we are operating on the MOS.
1898 * From syncing context, dn_nlevels may be different from the
1899 * dn_nlevels used when dbuf was dirtied.
1900 */
1901 ASSERT(db->db_objset ==
1902 dmu_objset_pool(db->db_objset)->dp_meta_objset ||
1903 txg != spa_syncing_txg(dmu_objset_spa(db->db_objset)));
428870ff 1904 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
13fe0198
MA
1905 ASSERT0(db->db_level);
1906 ASSERT(MUTEX_HELD(&db->db_mtx));
34dc7c2f 1907
34dc7c2f
BB
1908 /*
1909 * If this buffer is not dirty, we're done.
1910 */
1911 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1912 if (dr->dr_txg <= txg)
1913 break;
13fe0198
MA
1914 if (dr == NULL || dr->dr_txg < txg)
1915 return (B_FALSE);
34dc7c2f 1916 ASSERT(dr->dr_txg == txg);
428870ff 1917 ASSERT(dr->dr_dbuf == db);
34dc7c2f 1918
572e2857
BB
1919 DB_DNODE_ENTER(db);
1920 dn = DB_DNODE(db);
1921
34dc7c2f
BB
1922 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1923
1924 ASSERT(db->db.db_size != 0);
1925
4bda3bd0
MA
1926 dsl_pool_undirty_space(dmu_objset_pool(dn->dn_objset),
1927 dr->dr_accounted, txg);
34dc7c2f
BB
1928
1929 *drp = dr->dr_next;
1930
ef3c1dea
GR
1931 /*
1932 * Note that there are three places in dbuf_dirty()
1933 * where this dirty record may be put on a list.
1934 * Make sure to do a list_remove corresponding to
1935 * every one of those list_insert calls.
1936 */
34dc7c2f
BB
1937 if (dr->dr_parent) {
1938 mutex_enter(&dr->dr_parent->dt.di.dr_mtx);
1939 list_remove(&dr->dr_parent->dt.di.dr_children, dr);
1940 mutex_exit(&dr->dr_parent->dt.di.dr_mtx);
ef3c1dea 1941 } else if (db->db_blkid == DMU_SPILL_BLKID ||
4bda3bd0 1942 db->db_level + 1 == dn->dn_nlevels) {
b128c09f 1943 ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf);
34dc7c2f
BB
1944 mutex_enter(&dn->dn_mtx);
1945 list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr);
1946 mutex_exit(&dn->dn_mtx);
1947 }
572e2857 1948 DB_DNODE_EXIT(db);
34dc7c2f 1949
13fe0198
MA
1950 if (db->db_state != DB_NOFILL) {
1951 dbuf_unoverride(dr);
34dc7c2f 1952
34dc7c2f 1953 ASSERT(db->db_buf != NULL);
13fe0198
MA
1954 ASSERT(dr->dt.dl.dr_data != NULL);
1955 if (dr->dt.dl.dr_data != db->db_buf)
d3c2ae1c 1956 arc_buf_destroy(dr->dt.dl.dr_data, db);
34dc7c2f 1957 }
58c4aa00 1958
34dc7c2f
BB
1959 kmem_free(dr, sizeof (dbuf_dirty_record_t));
1960
1961 ASSERT(db->db_dirtycnt > 0);
1962 db->db_dirtycnt -= 1;
1963
1964 if (refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) {
d3c2ae1c
GW
1965 ASSERT(db->db_state == DB_NOFILL || arc_released(db->db_buf));
1966 dbuf_destroy(db);
13fe0198 1967 return (B_TRUE);
34dc7c2f
BB
1968 }
1969
13fe0198 1970 return (B_FALSE);
34dc7c2f
BB
1971}
1972
b5256303
TC
1973static void
1974dmu_buf_will_dirty_impl(dmu_buf_t *db_fake, int flags, dmu_tx_t *tx)
34dc7c2f 1975{
b0bc7a84 1976 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
5a28a973 1977 dbuf_dirty_record_t *dr;
34dc7c2f
BB
1978
1979 ASSERT(tx->tx_txg != 0);
1980 ASSERT(!refcount_is_zero(&db->db_holds));
1981
5a28a973
MA
1982 /*
1983 * Quick check for dirtyness. For already dirty blocks, this
1984 * reduces runtime of this function by >90%, and overall performance
1985 * by 50% for some workloads (e.g. file deletion with indirect blocks
1986 * cached).
1987 */
1988 mutex_enter(&db->db_mtx);
1989
1990 for (dr = db->db_last_dirty;
1991 dr != NULL && dr->dr_txg >= tx->tx_txg; dr = dr->dr_next) {
1992 /*
1993 * It's possible that it is already dirty but not cached,
1994 * because there are some calls to dbuf_dirty() that don't
1995 * go through dmu_buf_will_dirty().
1996 */
1997 if (dr->dr_txg == tx->tx_txg && db->db_state == DB_CACHED) {
1998 /* This dbuf is already dirty and cached. */
1999 dbuf_redirty(dr);
2000 mutex_exit(&db->db_mtx);
2001 return;
2002 }
2003 }
2004 mutex_exit(&db->db_mtx);
2005
572e2857
BB
2006 DB_DNODE_ENTER(db);
2007 if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock))
b5256303 2008 flags |= DB_RF_HAVESTRUCT;
572e2857 2009 DB_DNODE_EXIT(db);
b5256303 2010 (void) dbuf_read(db, NULL, flags);
34dc7c2f
BB
2011 (void) dbuf_dirty(db, tx);
2012}
2013
b5256303
TC
2014void
2015dmu_buf_will_dirty(dmu_buf_t *db_fake, dmu_tx_t *tx)
2016{
2017 dmu_buf_will_dirty_impl(db_fake,
2018 DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH, tx);
2019}
2020
b128c09f
BB
2021void
2022dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
2023{
2024 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2025
2026 db->db_state = DB_NOFILL;
2027
2028 dmu_buf_will_fill(db_fake, tx);
2029}
2030
34dc7c2f
BB
2031void
2032dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
2033{
2034 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2035
428870ff 2036 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
34dc7c2f
BB
2037 ASSERT(tx->tx_txg != 0);
2038 ASSERT(db->db_level == 0);
2039 ASSERT(!refcount_is_zero(&db->db_holds));
2040
2041 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
2042 dmu_tx_private_ok(tx));
2043
2044 dbuf_noread(db);
2045 (void) dbuf_dirty(db, tx);
2046}
2047
b5256303
TC
2048/*
2049 * This function is effectively the same as dmu_buf_will_dirty(), but
2050 * indicates the caller expects raw encrypted data in the db. It will
2051 * also set the raw flag on the created dirty record.
2052 */
2053void
2054dmu_buf_will_change_crypt_params(dmu_buf_t *db_fake, dmu_tx_t *tx)
2055{
2056 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2057 dbuf_dirty_record_t *dr;
2058
2059 dmu_buf_will_dirty_impl(db_fake,
2060 DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH | DB_RF_NO_DECRYPT, tx);
2061
2062 dr = db->db_last_dirty;
2063 while (dr != NULL && dr->dr_txg > tx->tx_txg)
2064 dr = dr->dr_next;
2065
2066 ASSERT3P(dr, !=, NULL);
2067 ASSERT3U(dr->dr_txg, ==, tx->tx_txg);
2068 dr->dt.dl.dr_raw = B_TRUE;
2069}
2070
34dc7c2f
BB
2071#pragma weak dmu_buf_fill_done = dbuf_fill_done
2072/* ARGSUSED */
2073void
2074dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx)
2075{
2076 mutex_enter(&db->db_mtx);
2077 DBUF_VERIFY(db);
2078
2079 if (db->db_state == DB_FILL) {
2080 if (db->db_level == 0 && db->db_freed_in_flight) {
428870ff 2081 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
34dc7c2f
BB
2082 /* we were freed while filling */
2083 /* XXX dbuf_undirty? */
2084 bzero(db->db.db_data, db->db.db_size);
2085 db->db_freed_in_flight = FALSE;
2086 }
2087 db->db_state = DB_CACHED;
2088 cv_broadcast(&db->db_changed);
2089 }
2090 mutex_exit(&db->db_mtx);
2091}
2092
9b67f605
MA
2093void
2094dmu_buf_write_embedded(dmu_buf_t *dbuf, void *data,
2095 bp_embedded_type_t etype, enum zio_compress comp,
2096 int uncompressed_size, int compressed_size, int byteorder,
2097 dmu_tx_t *tx)
2098{
2099 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
2100 struct dirty_leaf *dl;
2101 dmu_object_type_t type;
2102
241b5415
MA
2103 if (etype == BP_EMBEDDED_TYPE_DATA) {
2104 ASSERT(spa_feature_is_active(dmu_objset_spa(db->db_objset),
2105 SPA_FEATURE_EMBEDDED_DATA));
2106 }
2107
9b67f605
MA
2108 DB_DNODE_ENTER(db);
2109 type = DB_DNODE(db)->dn_type;
2110 DB_DNODE_EXIT(db);
2111
2112 ASSERT0(db->db_level);
2113 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2114
2115 dmu_buf_will_not_fill(dbuf, tx);
2116
2117 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
2118 dl = &db->db_last_dirty->dt.dl;
2119 encode_embedded_bp_compressed(&dl->dr_overridden_by,
2120 data, comp, uncompressed_size, compressed_size);
2121 BPE_SET_ETYPE(&dl->dr_overridden_by, etype);
2122 BP_SET_TYPE(&dl->dr_overridden_by, type);
2123 BP_SET_LEVEL(&dl->dr_overridden_by, 0);
2124 BP_SET_BYTEORDER(&dl->dr_overridden_by, byteorder);
2125
2126 dl->dr_override_state = DR_OVERRIDDEN;
2127 dl->dr_overridden_by.blk_birth = db->db_last_dirty->dr_txg;
2128}
2129
9babb374
BB
2130/*
2131 * Directly assign a provided arc buf to a given dbuf if it's not referenced
2132 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
2133 */
2134void
2135dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx)
2136{
2137 ASSERT(!refcount_is_zero(&db->db_holds));
428870ff 2138 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
9babb374 2139 ASSERT(db->db_level == 0);
2aa34383 2140 ASSERT3U(dbuf_is_metadata(db), ==, arc_is_metadata(buf));
9babb374 2141 ASSERT(buf != NULL);
2aa34383 2142 ASSERT(arc_buf_lsize(buf) == db->db.db_size);
9babb374
BB
2143 ASSERT(tx->tx_txg != 0);
2144
2145 arc_return_buf(buf, db);
2146 ASSERT(arc_released(buf));
2147
2148 mutex_enter(&db->db_mtx);
2149
2150 while (db->db_state == DB_READ || db->db_state == DB_FILL)
2151 cv_wait(&db->db_changed, &db->db_mtx);
2152
2153 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED);
2154
2155 if (db->db_state == DB_CACHED &&
2156 refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) {
440a3eb9
TC
2157 /*
2158 * In practice, we will never have a case where we have an
2159 * encrypted arc buffer while additional holds exist on the
2160 * dbuf. We don't handle this here so we simply assert that
2161 * fact instead.
2162 */
2163 ASSERT(!arc_is_encrypted(buf));
9babb374
BB
2164 mutex_exit(&db->db_mtx);
2165 (void) dbuf_dirty(db, tx);
2166 bcopy(buf->b_data, db->db.db_data, db->db.db_size);
d3c2ae1c 2167 arc_buf_destroy(buf, db);
428870ff 2168 xuio_stat_wbuf_copied();
9babb374
BB
2169 return;
2170 }
2171
428870ff 2172 xuio_stat_wbuf_nocopy();
9babb374
BB
2173 if (db->db_state == DB_CACHED) {
2174 dbuf_dirty_record_t *dr = db->db_last_dirty;
2175
2176 ASSERT(db->db_buf != NULL);
2177 if (dr != NULL && dr->dr_txg == tx->tx_txg) {
2178 ASSERT(dr->dt.dl.dr_data == db->db_buf);
440a3eb9
TC
2179 IMPLY(arc_is_encrypted(buf), dr->dt.dl.dr_raw);
2180
9babb374
BB
2181 if (!arc_released(db->db_buf)) {
2182 ASSERT(dr->dt.dl.dr_override_state ==
2183 DR_OVERRIDDEN);
2184 arc_release(db->db_buf, db);
2185 }
2186 dr->dt.dl.dr_data = buf;
d3c2ae1c 2187 arc_buf_destroy(db->db_buf, db);
9babb374
BB
2188 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) {
2189 arc_release(db->db_buf, db);
d3c2ae1c 2190 arc_buf_destroy(db->db_buf, db);
9babb374
BB
2191 }
2192 db->db_buf = NULL;
2193 }
2194 ASSERT(db->db_buf == NULL);
2195 dbuf_set_data(db, buf);
2196 db->db_state = DB_FILL;
2197 mutex_exit(&db->db_mtx);
2198 (void) dbuf_dirty(db, tx);
b0bc7a84 2199 dmu_buf_fill_done(&db->db, tx);
9babb374
BB
2200}
2201
34dc7c2f 2202void
d3c2ae1c 2203dbuf_destroy(dmu_buf_impl_t *db)
34dc7c2f 2204{
572e2857 2205 dnode_t *dn;
34dc7c2f 2206 dmu_buf_impl_t *parent = db->db_parent;
572e2857 2207 dmu_buf_impl_t *dndb;
34dc7c2f
BB
2208
2209 ASSERT(MUTEX_HELD(&db->db_mtx));
2210 ASSERT(refcount_is_zero(&db->db_holds));
2211
d3c2ae1c
GW
2212 if (db->db_buf != NULL) {
2213 arc_buf_destroy(db->db_buf, db);
2214 db->db_buf = NULL;
2215 }
34dc7c2f 2216
d3c2ae1c
GW
2217 if (db->db_blkid == DMU_BONUS_BLKID) {
2218 int slots = DB_DNODE(db)->dn_num_slots;
2219 int bonuslen = DN_SLOTS_TO_BONUSLEN(slots);
b5256303
TC
2220 if (db->db.db_data != NULL) {
2221 kmem_free(db->db.db_data, bonuslen);
2222 arc_space_return(bonuslen, ARC_SPACE_BONUS);
2223 db->db_state = DB_UNCACHED;
2224 }
34dc7c2f
BB
2225 }
2226
d3c2ae1c
GW
2227 dbuf_clear_data(db);
2228
2229 if (multilist_link_active(&db->db_cache_link)) {
64fc7762 2230 multilist_remove(dbuf_cache, db);
d3c2ae1c
GW
2231 (void) refcount_remove_many(&dbuf_cache_size,
2232 db->db.db_size, db);
2233 }
2234
b128c09f 2235 ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL);
34dc7c2f
BB
2236 ASSERT(db->db_data_pending == NULL);
2237
2238 db->db_state = DB_EVICTING;
2239 db->db_blkptr = NULL;
2240
d3c2ae1c
GW
2241 /*
2242 * Now that db_state is DB_EVICTING, nobody else can find this via
2243 * the hash table. We can now drop db_mtx, which allows us to
2244 * acquire the dn_dbufs_mtx.
2245 */
2246 mutex_exit(&db->db_mtx);
2247
572e2857
BB
2248 DB_DNODE_ENTER(db);
2249 dn = DB_DNODE(db);
2250 dndb = dn->dn_dbuf;
d3c2ae1c
GW
2251 if (db->db_blkid != DMU_BONUS_BLKID) {
2252 boolean_t needlock = !MUTEX_HELD(&dn->dn_dbufs_mtx);
2253 if (needlock)
2254 mutex_enter(&dn->dn_dbufs_mtx);
8951cb8d 2255 avl_remove(&dn->dn_dbufs, db);
73ad4a9f 2256 atomic_dec_32(&dn->dn_dbufs_count);
572e2857
BB
2257 membar_producer();
2258 DB_DNODE_EXIT(db);
d3c2ae1c
GW
2259 if (needlock)
2260 mutex_exit(&dn->dn_dbufs_mtx);
572e2857
BB
2261 /*
2262 * Decrementing the dbuf count means that the hold corresponding
2263 * to the removed dbuf is no longer discounted in dnode_move(),
2264 * so the dnode cannot be moved until after we release the hold.
2265 * The membar_producer() ensures visibility of the decremented
2266 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
2267 * release any lock.
2268 */
34dc7c2f 2269 dnode_rele(dn, db);
572e2857 2270 db->db_dnode_handle = NULL;
d3c2ae1c
GW
2271
2272 dbuf_hash_remove(db);
572e2857
BB
2273 } else {
2274 DB_DNODE_EXIT(db);
34dc7c2f
BB
2275 }
2276
d3c2ae1c 2277 ASSERT(refcount_is_zero(&db->db_holds));
34dc7c2f 2278
d3c2ae1c
GW
2279 db->db_parent = NULL;
2280
2281 ASSERT(db->db_buf == NULL);
2282 ASSERT(db->db.db_data == NULL);
2283 ASSERT(db->db_hash_next == NULL);
2284 ASSERT(db->db_blkptr == NULL);
2285 ASSERT(db->db_data_pending == NULL);
2286 ASSERT(!multilist_link_active(&db->db_cache_link));
2287
2288 kmem_cache_free(dbuf_kmem_cache, db);
2289 arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_DBUF);
34dc7c2f
BB
2290
2291 /*
572e2857 2292 * If this dbuf is referenced from an indirect dbuf,
34dc7c2f
BB
2293 * decrement the ref count on the indirect dbuf.
2294 */
2295 if (parent && parent != dndb)
2296 dbuf_rele(parent, db);
2297}
2298
fcff0f35
PD
2299/*
2300 * Note: While bpp will always be updated if the function returns success,
2301 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
2302 * this happens when the dnode is the meta-dnode, or a userused or groupused
2303 * object.
2304 */
bf701a83
BB
2305__attribute__((always_inline))
2306static inline int
34dc7c2f 2307dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse,
fc5bb51f 2308 dmu_buf_impl_t **parentp, blkptr_t **bpp, struct dbuf_hold_impl_data *dh)
34dc7c2f
BB
2309{
2310 int nlevels, epbs;
2311
2312 *parentp = NULL;
2313 *bpp = NULL;
2314
428870ff
BB
2315 ASSERT(blkid != DMU_BONUS_BLKID);
2316
2317 if (blkid == DMU_SPILL_BLKID) {
2318 mutex_enter(&dn->dn_mtx);
2319 if (dn->dn_have_spill &&
2320 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
50c957f7 2321 *bpp = DN_SPILL_BLKPTR(dn->dn_phys);
428870ff
BB
2322 else
2323 *bpp = NULL;
2324 dbuf_add_ref(dn->dn_dbuf, NULL);
2325 *parentp = dn->dn_dbuf;
2326 mutex_exit(&dn->dn_mtx);
2327 return (0);
2328 }
34dc7c2f 2329
32d41fb7
PD
2330 nlevels =
2331 (dn->dn_phys->dn_nlevels == 0) ? 1 : dn->dn_phys->dn_nlevels;
34dc7c2f
BB
2332 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
2333
2334 ASSERT3U(level * epbs, <, 64);
2335 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
32d41fb7
PD
2336 /*
2337 * This assertion shouldn't trip as long as the max indirect block size
2338 * is less than 1M. The reason for this is that up to that point,
2339 * the number of levels required to address an entire object with blocks
2340 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In
2341 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55
2342 * (i.e. we can address the entire object), objects will all use at most
2343 * N-1 levels and the assertion won't overflow. However, once epbs is
2344 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be
2345 * enough to address an entire object, so objects will have 5 levels,
2346 * but then this assertion will overflow.
2347 *
2348 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we
2349 * need to redo this logic to handle overflows.
2350 */
2351 ASSERT(level >= nlevels ||
2352 ((nlevels - level - 1) * epbs) +
2353 highbit64(dn->dn_phys->dn_nblkptr) <= 64);
34dc7c2f 2354 if (level >= nlevels ||
32d41fb7
PD
2355 blkid >= ((uint64_t)dn->dn_phys->dn_nblkptr <<
2356 ((nlevels - level - 1) * epbs)) ||
2357 (fail_sparse &&
2358 blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) {
34dc7c2f 2359 /* the buffer has no parent yet */
2e528b49 2360 return (SET_ERROR(ENOENT));
34dc7c2f
BB
2361 } else if (level < nlevels-1) {
2362 /* this block is referenced from an indirect block */
fc5bb51f
BB
2363 int err;
2364 if (dh == NULL) {
fcff0f35
PD
2365 err = dbuf_hold_impl(dn, level+1,
2366 blkid >> epbs, fail_sparse, FALSE, NULL, parentp);
d1d7e268 2367 } else {
fc5bb51f 2368 __dbuf_hold_impl_init(dh + 1, dn, dh->dh_level + 1,
fcff0f35
PD
2369 blkid >> epbs, fail_sparse, FALSE, NULL,
2370 parentp, dh->dh_depth + 1);
fc5bb51f
BB
2371 err = __dbuf_hold_impl(dh + 1);
2372 }
34dc7c2f
BB
2373 if (err)
2374 return (err);
2375 err = dbuf_read(*parentp, NULL,
2376 (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL));
2377 if (err) {
2378 dbuf_rele(*parentp, NULL);
2379 *parentp = NULL;
2380 return (err);
2381 }
2382 *bpp = ((blkptr_t *)(*parentp)->db.db_data) +
2383 (blkid & ((1ULL << epbs) - 1));
32d41fb7
PD
2384 if (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))
2385 ASSERT(BP_IS_HOLE(*bpp));
34dc7c2f
BB
2386 return (0);
2387 } else {
2388 /* the block is referenced from the dnode */
2389 ASSERT3U(level, ==, nlevels-1);
2390 ASSERT(dn->dn_phys->dn_nblkptr == 0 ||
2391 blkid < dn->dn_phys->dn_nblkptr);
2392 if (dn->dn_dbuf) {
2393 dbuf_add_ref(dn->dn_dbuf, NULL);
2394 *parentp = dn->dn_dbuf;
2395 }
2396 *bpp = &dn->dn_phys->dn_blkptr[blkid];
2397 return (0);
2398 }
2399}
2400
2401static dmu_buf_impl_t *
2402dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid,
2403 dmu_buf_impl_t *parent, blkptr_t *blkptr)
2404{
428870ff 2405 objset_t *os = dn->dn_objset;
34dc7c2f
BB
2406 dmu_buf_impl_t *db, *odb;
2407
2408 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2409 ASSERT(dn->dn_type != DMU_OT_NONE);
2410
d3c2ae1c 2411 db = kmem_cache_alloc(dbuf_kmem_cache, KM_SLEEP);
34dc7c2f
BB
2412
2413 db->db_objset = os;
2414 db->db.db_object = dn->dn_object;
2415 db->db_level = level;
2416 db->db_blkid = blkid;
2417 db->db_last_dirty = NULL;
2418 db->db_dirtycnt = 0;
572e2857 2419 db->db_dnode_handle = dn->dn_handle;
34dc7c2f
BB
2420 db->db_parent = parent;
2421 db->db_blkptr = blkptr;
2422
0c66c32d 2423 db->db_user = NULL;
bc4501f7
JG
2424 db->db_user_immediate_evict = FALSE;
2425 db->db_freed_in_flight = FALSE;
2426 db->db_pending_evict = FALSE;
34dc7c2f 2427
428870ff 2428 if (blkid == DMU_BONUS_BLKID) {
34dc7c2f 2429 ASSERT3P(parent, ==, dn->dn_dbuf);
50c957f7 2430 db->db.db_size = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots) -
34dc7c2f
BB
2431 (dn->dn_nblkptr-1) * sizeof (blkptr_t);
2432 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
428870ff 2433 db->db.db_offset = DMU_BONUS_BLKID;
34dc7c2f
BB
2434 db->db_state = DB_UNCACHED;
2435 /* the bonus dbuf is not placed in the hash table */
25458cbe 2436 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_DBUF);
34dc7c2f 2437 return (db);
428870ff
BB
2438 } else if (blkid == DMU_SPILL_BLKID) {
2439 db->db.db_size = (blkptr != NULL) ?
2440 BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE;
2441 db->db.db_offset = 0;
34dc7c2f
BB
2442 } else {
2443 int blocksize =
e8b96c60 2444 db->db_level ? 1 << dn->dn_indblkshift : dn->dn_datablksz;
34dc7c2f
BB
2445 db->db.db_size = blocksize;
2446 db->db.db_offset = db->db_blkid * blocksize;
2447 }
2448
2449 /*
2450 * Hold the dn_dbufs_mtx while we get the new dbuf
2451 * in the hash table *and* added to the dbufs list.
2452 * This prevents a possible deadlock with someone
2453 * trying to look up this dbuf before its added to the
2454 * dn_dbufs list.
2455 */
2456 mutex_enter(&dn->dn_dbufs_mtx);
2457 db->db_state = DB_EVICTING;
2458 if ((odb = dbuf_hash_insert(db)) != NULL) {
2459 /* someone else inserted it first */
d3c2ae1c 2460 kmem_cache_free(dbuf_kmem_cache, db);
34dc7c2f
BB
2461 mutex_exit(&dn->dn_dbufs_mtx);
2462 return (odb);
2463 }
8951cb8d 2464 avl_add(&dn->dn_dbufs, db);
9c9531cb 2465
34dc7c2f
BB
2466 db->db_state = DB_UNCACHED;
2467 mutex_exit(&dn->dn_dbufs_mtx);
25458cbe 2468 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_DBUF);
34dc7c2f
BB
2469
2470 if (parent && parent != dn->dn_dbuf)
2471 dbuf_add_ref(parent, db);
2472
2473 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
2474 refcount_count(&dn->dn_holds) > 0);
2475 (void) refcount_add(&dn->dn_holds, db);
73ad4a9f 2476 atomic_inc_32(&dn->dn_dbufs_count);
34dc7c2f
BB
2477
2478 dprintf_dbuf(db, "db=%p\n", db);
2479
2480 return (db);
2481}
2482
fcff0f35
PD
2483typedef struct dbuf_prefetch_arg {
2484 spa_t *dpa_spa; /* The spa to issue the prefetch in. */
2485 zbookmark_phys_t dpa_zb; /* The target block to prefetch. */
2486 int dpa_epbs; /* Entries (blkptr_t's) Per Block Shift. */
2487 int dpa_curlevel; /* The current level that we're reading */
d3c2ae1c 2488 dnode_t *dpa_dnode; /* The dnode associated with the prefetch */
fcff0f35
PD
2489 zio_priority_t dpa_prio; /* The priority I/Os should be issued at. */
2490 zio_t *dpa_zio; /* The parent zio_t for all prefetches. */
2491 arc_flags_t dpa_aflags; /* Flags to pass to the final prefetch. */
2492} dbuf_prefetch_arg_t;
2493
2494/*
2495 * Actually issue the prefetch read for the block given.
2496 */
2497static void
2498dbuf_issue_final_prefetch(dbuf_prefetch_arg_t *dpa, blkptr_t *bp)
2499{
2500 arc_flags_t aflags;
2501 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
2502 return;
2503
2504 aflags = dpa->dpa_aflags | ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH;
2505
2506 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp));
2507 ASSERT3U(dpa->dpa_curlevel, ==, dpa->dpa_zb.zb_level);
2508 ASSERT(dpa->dpa_zio != NULL);
2509 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa, bp, NULL, NULL,
2510 dpa->dpa_prio, ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2511 &aflags, &dpa->dpa_zb);
2512}
2513
2514/*
2515 * Called when an indirect block above our prefetch target is read in. This
2516 * will either read in the next indirect block down the tree or issue the actual
2517 * prefetch if the next block down is our target.
2518 */
2519static void
b5256303 2520dbuf_prefetch_indirect_done(zio_t *zio, int err, arc_buf_t *abuf, void *private)
fcff0f35
PD
2521{
2522 dbuf_prefetch_arg_t *dpa = private;
2523 uint64_t nextblkid;
2524 blkptr_t *bp;
2525
2526 ASSERT3S(dpa->dpa_zb.zb_level, <, dpa->dpa_curlevel);
2527 ASSERT3S(dpa->dpa_curlevel, >, 0);
d3c2ae1c
GW
2528
2529 /*
2530 * The dpa_dnode is only valid if we are called with a NULL
2531 * zio. This indicates that the arc_read() returned without
2532 * first calling zio_read() to issue a physical read. Once
2533 * a physical read is made the dpa_dnode must be invalidated
2534 * as the locks guarding it may have been dropped. If the
2535 * dpa_dnode is still valid, then we want to add it to the dbuf
2536 * cache. To do so, we must hold the dbuf associated with the block
2537 * we just prefetched, read its contents so that we associate it
2538 * with an arc_buf_t, and then release it.
2539 */
fcff0f35
PD
2540 if (zio != NULL) {
2541 ASSERT3S(BP_GET_LEVEL(zio->io_bp), ==, dpa->dpa_curlevel);
b5256303 2542 if (zio->io_flags & ZIO_FLAG_RAW_COMPRESS) {
d3c2ae1c
GW
2543 ASSERT3U(BP_GET_PSIZE(zio->io_bp), ==, zio->io_size);
2544 } else {
2545 ASSERT3U(BP_GET_LSIZE(zio->io_bp), ==, zio->io_size);
2546 }
fcff0f35 2547 ASSERT3P(zio->io_spa, ==, dpa->dpa_spa);
d3c2ae1c
GW
2548
2549 dpa->dpa_dnode = NULL;
2550 } else if (dpa->dpa_dnode != NULL) {
2551 uint64_t curblkid = dpa->dpa_zb.zb_blkid >>
2552 (dpa->dpa_epbs * (dpa->dpa_curlevel -
2553 dpa->dpa_zb.zb_level));
2554 dmu_buf_impl_t *db = dbuf_hold_level(dpa->dpa_dnode,
2555 dpa->dpa_curlevel, curblkid, FTAG);
2556 (void) dbuf_read(db, NULL,
2557 DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH | DB_RF_HAVESTRUCT);
2558 dbuf_rele(db, FTAG);
fcff0f35
PD
2559 }
2560
2561 dpa->dpa_curlevel--;
2562
2563 nextblkid = dpa->dpa_zb.zb_blkid >>
2564 (dpa->dpa_epbs * (dpa->dpa_curlevel - dpa->dpa_zb.zb_level));
2565 bp = ((blkptr_t *)abuf->b_data) +
2566 P2PHASE(nextblkid, 1ULL << dpa->dpa_epbs);
b5256303 2567 if (BP_IS_HOLE(bp) || err != 0) {
fcff0f35
PD
2568 kmem_free(dpa, sizeof (*dpa));
2569 } else if (dpa->dpa_curlevel == dpa->dpa_zb.zb_level) {
2570 ASSERT3U(nextblkid, ==, dpa->dpa_zb.zb_blkid);
2571 dbuf_issue_final_prefetch(dpa, bp);
2572 kmem_free(dpa, sizeof (*dpa));
2573 } else {
2574 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT;
2575 zbookmark_phys_t zb;
2576
2577 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp));
2578
2579 SET_BOOKMARK(&zb, dpa->dpa_zb.zb_objset,
2580 dpa->dpa_zb.zb_object, dpa->dpa_curlevel, nextblkid);
2581
2582 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa,
2583 bp, dbuf_prefetch_indirect_done, dpa, dpa->dpa_prio,
2584 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2585 &iter_aflags, &zb);
2586 }
d3c2ae1c
GW
2587
2588 arc_buf_destroy(abuf, private);
fcff0f35
PD
2589}
2590
2591/*
2592 * Issue prefetch reads for the given block on the given level. If the indirect
2593 * blocks above that block are not in memory, we will read them in
2594 * asynchronously. As a result, this call never blocks waiting for a read to
b5256303
TC
2595 * complete. Note that the prefetch might fail if the dataset is encrypted and
2596 * the encryption key is unmapped before the IO completes.
fcff0f35 2597 */
34dc7c2f 2598void
fcff0f35
PD
2599dbuf_prefetch(dnode_t *dn, int64_t level, uint64_t blkid, zio_priority_t prio,
2600 arc_flags_t aflags)
34dc7c2f 2601{
fcff0f35
PD
2602 blkptr_t bp;
2603 int epbs, nlevels, curlevel;
2604 uint64_t curblkid;
2605 dmu_buf_impl_t *db;
2606 zio_t *pio;
2607 dbuf_prefetch_arg_t *dpa;
2608 dsl_dataset_t *ds;
34dc7c2f 2609
428870ff 2610 ASSERT(blkid != DMU_BONUS_BLKID);
34dc7c2f
BB
2611 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2612
7f60329a
MA
2613 if (blkid > dn->dn_maxblkid)
2614 return;
2615
34dc7c2f
BB
2616 if (dnode_block_freed(dn, blkid))
2617 return;
2618
fcff0f35
PD
2619 /*
2620 * This dnode hasn't been written to disk yet, so there's nothing to
2621 * prefetch.
2622 */
2623 nlevels = dn->dn_phys->dn_nlevels;
2624 if (level >= nlevels || dn->dn_phys->dn_nblkptr == 0)
2625 return;
2626
2627 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2628 if (dn->dn_phys->dn_maxblkid < blkid << (epbs * level))
2629 return;
2630
2631 db = dbuf_find(dn->dn_objset, dn->dn_object,
2632 level, blkid);
2633 if (db != NULL) {
2634 mutex_exit(&db->db_mtx);
572e2857 2635 /*
fcff0f35
PD
2636 * This dbuf already exists. It is either CACHED, or
2637 * (we assume) about to be read or filled.
572e2857 2638 */
572e2857 2639 return;
34dc7c2f
BB
2640 }
2641
fcff0f35
PD
2642 /*
2643 * Find the closest ancestor (indirect block) of the target block
2644 * that is present in the cache. In this indirect block, we will
2645 * find the bp that is at curlevel, curblkid.
2646 */
2647 curlevel = level;
2648 curblkid = blkid;
2649 while (curlevel < nlevels - 1) {
2650 int parent_level = curlevel + 1;
2651 uint64_t parent_blkid = curblkid >> epbs;
2652 dmu_buf_impl_t *db;
2653
2654 if (dbuf_hold_impl(dn, parent_level, parent_blkid,
2655 FALSE, TRUE, FTAG, &db) == 0) {
2656 blkptr_t *bpp = db->db_buf->b_data;
2657 bp = bpp[P2PHASE(curblkid, 1 << epbs)];
2658 dbuf_rele(db, FTAG);
2659 break;
2660 }
428870ff 2661
fcff0f35
PD
2662 curlevel = parent_level;
2663 curblkid = parent_blkid;
2664 }
34dc7c2f 2665
fcff0f35
PD
2666 if (curlevel == nlevels - 1) {
2667 /* No cached indirect blocks found. */
2668 ASSERT3U(curblkid, <, dn->dn_phys->dn_nblkptr);
2669 bp = dn->dn_phys->dn_blkptr[curblkid];
34dc7c2f 2670 }
fcff0f35
PD
2671 if (BP_IS_HOLE(&bp))
2672 return;
2673
2674 ASSERT3U(curlevel, ==, BP_GET_LEVEL(&bp));
2675
2676 pio = zio_root(dmu_objset_spa(dn->dn_objset), NULL, NULL,
2677 ZIO_FLAG_CANFAIL);
2678
2679 dpa = kmem_zalloc(sizeof (*dpa), KM_SLEEP);
2680 ds = dn->dn_objset->os_dsl_dataset;
2681 SET_BOOKMARK(&dpa->dpa_zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET,
2682 dn->dn_object, level, blkid);
2683 dpa->dpa_curlevel = curlevel;
2684 dpa->dpa_prio = prio;
2685 dpa->dpa_aflags = aflags;
2686 dpa->dpa_spa = dn->dn_objset->os_spa;
d3c2ae1c 2687 dpa->dpa_dnode = dn;
fcff0f35
PD
2688 dpa->dpa_epbs = epbs;
2689 dpa->dpa_zio = pio;
2690
2691 /*
2692 * If we have the indirect just above us, no need to do the asynchronous
2693 * prefetch chain; we'll just run the last step ourselves. If we're at
2694 * a higher level, though, we want to issue the prefetches for all the
2695 * indirect blocks asynchronously, so we can go on with whatever we were
2696 * doing.
2697 */
2698 if (curlevel == level) {
2699 ASSERT3U(curblkid, ==, blkid);
2700 dbuf_issue_final_prefetch(dpa, &bp);
2701 kmem_free(dpa, sizeof (*dpa));
2702 } else {
2703 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT;
2704 zbookmark_phys_t zb;
2705
2706 SET_BOOKMARK(&zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET,
2707 dn->dn_object, curlevel, curblkid);
2708 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa,
2709 &bp, dbuf_prefetch_indirect_done, dpa, prio,
2710 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2711 &iter_aflags, &zb);
2712 }
2713 /*
2714 * We use pio here instead of dpa_zio since it's possible that
2715 * dpa may have already been freed.
2716 */
2717 zio_nowait(pio);
34dc7c2f
BB
2718}
2719
d1d7e268 2720#define DBUF_HOLD_IMPL_MAX_DEPTH 20
fc5bb51f 2721
34dc7c2f
BB
2722/*
2723 * Returns with db_holds incremented, and db_mtx not held.
2724 * Note: dn_struct_rwlock must be held.
2725 */
fc5bb51f
BB
2726static int
2727__dbuf_hold_impl(struct dbuf_hold_impl_data *dh)
34dc7c2f 2728{
fc5bb51f
BB
2729 ASSERT3S(dh->dh_depth, <, DBUF_HOLD_IMPL_MAX_DEPTH);
2730 dh->dh_parent = NULL;
34dc7c2f 2731
fc5bb51f
BB
2732 ASSERT(dh->dh_blkid != DMU_BONUS_BLKID);
2733 ASSERT(RW_LOCK_HELD(&dh->dh_dn->dn_struct_rwlock));
2734 ASSERT3U(dh->dh_dn->dn_nlevels, >, dh->dh_level);
34dc7c2f 2735
fc5bb51f 2736 *(dh->dh_dbp) = NULL;
d3c2ae1c 2737
34dc7c2f 2738 /* dbuf_find() returns with db_mtx held */
6ebebace
JG
2739 dh->dh_db = dbuf_find(dh->dh_dn->dn_objset, dh->dh_dn->dn_object,
2740 dh->dh_level, dh->dh_blkid);
fc5bb51f
BB
2741
2742 if (dh->dh_db == NULL) {
2743 dh->dh_bp = NULL;
2744
fcff0f35
PD
2745 if (dh->dh_fail_uncached)
2746 return (SET_ERROR(ENOENT));
2747
fc5bb51f
BB
2748 ASSERT3P(dh->dh_parent, ==, NULL);
2749 dh->dh_err = dbuf_findbp(dh->dh_dn, dh->dh_level, dh->dh_blkid,
02730c33 2750 dh->dh_fail_sparse, &dh->dh_parent, &dh->dh_bp, dh);
fc5bb51f 2751 if (dh->dh_fail_sparse) {
d1d7e268
MK
2752 if (dh->dh_err == 0 &&
2753 dh->dh_bp && BP_IS_HOLE(dh->dh_bp))
2e528b49 2754 dh->dh_err = SET_ERROR(ENOENT);
fc5bb51f
BB
2755 if (dh->dh_err) {
2756 if (dh->dh_parent)
2757 dbuf_rele(dh->dh_parent, NULL);
2758 return (dh->dh_err);
34dc7c2f
BB
2759 }
2760 }
fc5bb51f
BB
2761 if (dh->dh_err && dh->dh_err != ENOENT)
2762 return (dh->dh_err);
2763 dh->dh_db = dbuf_create(dh->dh_dn, dh->dh_level, dh->dh_blkid,
02730c33 2764 dh->dh_parent, dh->dh_bp);
34dc7c2f
BB
2765 }
2766
fcff0f35
PD
2767 if (dh->dh_fail_uncached && dh->dh_db->db_state != DB_CACHED) {
2768 mutex_exit(&dh->dh_db->db_mtx);
2769 return (SET_ERROR(ENOENT));
2770 }
2771
d3c2ae1c 2772 if (dh->dh_db->db_buf != NULL)
fc5bb51f 2773 ASSERT3P(dh->dh_db->db.db_data, ==, dh->dh_db->db_buf->b_data);
34dc7c2f 2774
fc5bb51f 2775 ASSERT(dh->dh_db->db_buf == NULL || arc_referenced(dh->dh_db->db_buf));
34dc7c2f
BB
2776
2777 /*
2778 * If this buffer is currently syncing out, and we are are
2779 * still referencing it from db_data, we need to make a copy
2780 * of it in case we decide we want to dirty it again in this txg.
2781 */
fc5bb51f
BB
2782 if (dh->dh_db->db_level == 0 &&
2783 dh->dh_db->db_blkid != DMU_BONUS_BLKID &&
2784 dh->dh_dn->dn_object != DMU_META_DNODE_OBJECT &&
2785 dh->dh_db->db_state == DB_CACHED && dh->dh_db->db_data_pending) {
2786 dh->dh_dr = dh->dh_db->db_data_pending;
2787
2788 if (dh->dh_dr->dt.dl.dr_data == dh->dh_db->db_buf) {
2789 dh->dh_type = DBUF_GET_BUFC_TYPE(dh->dh_db);
2790
2791 dbuf_set_data(dh->dh_db,
d3c2ae1c 2792 arc_alloc_buf(dh->dh_dn->dn_objset->os_spa,
2aa34383 2793 dh->dh_db, dh->dh_type, dh->dh_db->db.db_size));
fc5bb51f
BB
2794 bcopy(dh->dh_dr->dt.dl.dr_data->b_data,
2795 dh->dh_db->db.db_data, dh->dh_db->db.db_size);
34dc7c2f
BB
2796 }
2797 }
2798
d3c2ae1c
GW
2799 if (multilist_link_active(&dh->dh_db->db_cache_link)) {
2800 ASSERT(refcount_is_zero(&dh->dh_db->db_holds));
64fc7762 2801 multilist_remove(dbuf_cache, dh->dh_db);
d3c2ae1c
GW
2802 (void) refcount_remove_many(&dbuf_cache_size,
2803 dh->dh_db->db.db_size, dh->dh_db);
2804 }
fc5bb51f 2805 (void) refcount_add(&dh->dh_db->db_holds, dh->dh_tag);
fc5bb51f
BB
2806 DBUF_VERIFY(dh->dh_db);
2807 mutex_exit(&dh->dh_db->db_mtx);
34dc7c2f
BB
2808
2809 /* NOTE: we can't rele the parent until after we drop the db_mtx */
fc5bb51f
BB
2810 if (dh->dh_parent)
2811 dbuf_rele(dh->dh_parent, NULL);
34dc7c2f 2812
fc5bb51f
BB
2813 ASSERT3P(DB_DNODE(dh->dh_db), ==, dh->dh_dn);
2814 ASSERT3U(dh->dh_db->db_blkid, ==, dh->dh_blkid);
2815 ASSERT3U(dh->dh_db->db_level, ==, dh->dh_level);
2816 *(dh->dh_dbp) = dh->dh_db;
34dc7c2f
BB
2817
2818 return (0);
2819}
2820
fc5bb51f
BB
2821/*
2822 * The following code preserves the recursive function dbuf_hold_impl()
2823 * but moves the local variables AND function arguments to the heap to
2824 * minimize the stack frame size. Enough space is initially allocated
2825 * on the stack for 20 levels of recursion.
2826 */
2827int
fcff0f35
PD
2828dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid,
2829 boolean_t fail_sparse, boolean_t fail_uncached,
fc5bb51f
BB
2830 void *tag, dmu_buf_impl_t **dbp)
2831{
2832 struct dbuf_hold_impl_data *dh;
2833 int error;
2834
d9eea113 2835 dh = kmem_alloc(sizeof (struct dbuf_hold_impl_data) *
79c76d5b 2836 DBUF_HOLD_IMPL_MAX_DEPTH, KM_SLEEP);
fcff0f35 2837 __dbuf_hold_impl_init(dh, dn, level, blkid, fail_sparse,
02730c33 2838 fail_uncached, tag, dbp, 0);
fc5bb51f
BB
2839
2840 error = __dbuf_hold_impl(dh);
2841
d1d7e268 2842 kmem_free(dh, sizeof (struct dbuf_hold_impl_data) *
fc5bb51f
BB
2843 DBUF_HOLD_IMPL_MAX_DEPTH);
2844
2845 return (error);
2846}
2847
2848static void
2849__dbuf_hold_impl_init(struct dbuf_hold_impl_data *dh,
fcff0f35 2850 dnode_t *dn, uint8_t level, uint64_t blkid,
4ea3f864
GM
2851 boolean_t fail_sparse, boolean_t fail_uncached,
2852 void *tag, dmu_buf_impl_t **dbp, int depth)
fc5bb51f
BB
2853{
2854 dh->dh_dn = dn;
2855 dh->dh_level = level;
2856 dh->dh_blkid = blkid;
fcff0f35 2857
fc5bb51f 2858 dh->dh_fail_sparse = fail_sparse;
fcff0f35
PD
2859 dh->dh_fail_uncached = fail_uncached;
2860
fc5bb51f
BB
2861 dh->dh_tag = tag;
2862 dh->dh_dbp = dbp;
d9eea113
MA
2863
2864 dh->dh_db = NULL;
2865 dh->dh_parent = NULL;
2866 dh->dh_bp = NULL;
2867 dh->dh_err = 0;
2868 dh->dh_dr = NULL;
2869 dh->dh_type = 0;
2870
fc5bb51f
BB
2871 dh->dh_depth = depth;
2872}
2873
34dc7c2f
BB
2874dmu_buf_impl_t *
2875dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag)
2876{
fcff0f35 2877 return (dbuf_hold_level(dn, 0, blkid, tag));
34dc7c2f
BB
2878}
2879
2880dmu_buf_impl_t *
2881dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag)
2882{
2883 dmu_buf_impl_t *db;
fcff0f35 2884 int err = dbuf_hold_impl(dn, level, blkid, FALSE, FALSE, tag, &db);
34dc7c2f
BB
2885 return (err ? NULL : db);
2886}
2887
2888void
2889dbuf_create_bonus(dnode_t *dn)
2890{
2891 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
2892
2893 ASSERT(dn->dn_bonus == NULL);
428870ff
BB
2894 dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL);
2895}
2896
2897int
2898dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx)
2899{
2900 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
572e2857
BB
2901 dnode_t *dn;
2902
428870ff 2903 if (db->db_blkid != DMU_SPILL_BLKID)
2e528b49 2904 return (SET_ERROR(ENOTSUP));
428870ff
BB
2905 if (blksz == 0)
2906 blksz = SPA_MINBLOCKSIZE;
f1512ee6
MA
2907 ASSERT3U(blksz, <=, spa_maxblocksize(dmu_objset_spa(db->db_objset)));
2908 blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE);
428870ff 2909
572e2857
BB
2910 DB_DNODE_ENTER(db);
2911 dn = DB_DNODE(db);
2912 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
428870ff 2913 dbuf_new_size(db, blksz, tx);
572e2857
BB
2914 rw_exit(&dn->dn_struct_rwlock);
2915 DB_DNODE_EXIT(db);
428870ff
BB
2916
2917 return (0);
2918}
2919
2920void
2921dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx)
2922{
2923 dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx);
34dc7c2f
BB
2924}
2925
2926#pragma weak dmu_buf_add_ref = dbuf_add_ref
2927void
2928dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
2929{
d3c2ae1c
GW
2930 int64_t holds = refcount_add(&db->db_holds, tag);
2931 VERIFY3S(holds, >, 1);
34dc7c2f
BB
2932}
2933
6ebebace
JG
2934#pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
2935boolean_t
2936dbuf_try_add_ref(dmu_buf_t *db_fake, objset_t *os, uint64_t obj, uint64_t blkid,
2937 void *tag)
2938{
2939 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2940 dmu_buf_impl_t *found_db;
2941 boolean_t result = B_FALSE;
2942
d617648c 2943 if (blkid == DMU_BONUS_BLKID)
6ebebace
JG
2944 found_db = dbuf_find_bonus(os, obj);
2945 else
2946 found_db = dbuf_find(os, obj, 0, blkid);
2947
2948 if (found_db != NULL) {
2949 if (db == found_db && dbuf_refcount(db) > db->db_dirtycnt) {
2950 (void) refcount_add(&db->db_holds, tag);
2951 result = B_TRUE;
2952 }
d617648c 2953 mutex_exit(&found_db->db_mtx);
6ebebace
JG
2954 }
2955 return (result);
2956}
2957
572e2857
BB
2958/*
2959 * If you call dbuf_rele() you had better not be referencing the dnode handle
2960 * unless you have some other direct or indirect hold on the dnode. (An indirect
2961 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2962 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2963 * dnode's parent dbuf evicting its dnode handles.
2964 */
34dc7c2f
BB
2965void
2966dbuf_rele(dmu_buf_impl_t *db, void *tag)
428870ff
BB
2967{
2968 mutex_enter(&db->db_mtx);
2969 dbuf_rele_and_unlock(db, tag);
2970}
2971
b0bc7a84
MG
2972void
2973dmu_buf_rele(dmu_buf_t *db, void *tag)
2974{
2975 dbuf_rele((dmu_buf_impl_t *)db, tag);
2976}
2977
428870ff
BB
2978/*
2979 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2980 * db_dirtycnt and db_holds to be updated atomically.
2981 */
2982void
2983dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag)
34dc7c2f
BB
2984{
2985 int64_t holds;
2986
428870ff 2987 ASSERT(MUTEX_HELD(&db->db_mtx));
34dc7c2f
BB
2988 DBUF_VERIFY(db);
2989
572e2857
BB
2990 /*
2991 * Remove the reference to the dbuf before removing its hold on the
2992 * dnode so we can guarantee in dnode_move() that a referenced bonus
2993 * buffer has a corresponding dnode hold.
2994 */
34dc7c2f
BB
2995 holds = refcount_remove(&db->db_holds, tag);
2996 ASSERT(holds >= 0);
2997
2998 /*
2999 * We can't freeze indirects if there is a possibility that they
3000 * may be modified in the current syncing context.
3001 */
d3c2ae1c
GW
3002 if (db->db_buf != NULL &&
3003 holds == (db->db_level == 0 ? db->db_dirtycnt : 0)) {
34dc7c2f 3004 arc_buf_freeze(db->db_buf);
d3c2ae1c 3005 }
34dc7c2f
BB
3006
3007 if (holds == db->db_dirtycnt &&
bc4501f7 3008 db->db_level == 0 && db->db_user_immediate_evict)
34dc7c2f
BB
3009 dbuf_evict_user(db);
3010
3011 if (holds == 0) {
428870ff 3012 if (db->db_blkid == DMU_BONUS_BLKID) {
4c7b7eed 3013 dnode_t *dn;
bc4501f7 3014 boolean_t evict_dbuf = db->db_pending_evict;
572e2857
BB
3015
3016 /*
4c7b7eed
JG
3017 * If the dnode moves here, we cannot cross this
3018 * barrier until the move completes.
572e2857
BB
3019 */
3020 DB_DNODE_ENTER(db);
4c7b7eed
JG
3021
3022 dn = DB_DNODE(db);
3023 atomic_dec_32(&dn->dn_dbufs_count);
3024
3025 /*
3026 * Decrementing the dbuf count means that the bonus
3027 * buffer's dnode hold is no longer discounted in
3028 * dnode_move(). The dnode cannot move until after
bc4501f7 3029 * the dnode_rele() below.
4c7b7eed 3030 */
572e2857 3031 DB_DNODE_EXIT(db);
4c7b7eed
JG
3032
3033 /*
3034 * Do not reference db after its lock is dropped.
3035 * Another thread may evict it.
3036 */
3037 mutex_exit(&db->db_mtx);
3038
bc4501f7 3039 if (evict_dbuf)
4c7b7eed 3040 dnode_evict_bonus(dn);
bc4501f7
JG
3041
3042 dnode_rele(dn, db);
34dc7c2f
BB
3043 } else if (db->db_buf == NULL) {
3044 /*
3045 * This is a special case: we never associated this
3046 * dbuf with any data allocated from the ARC.
3047 */
b128c09f
BB
3048 ASSERT(db->db_state == DB_UNCACHED ||
3049 db->db_state == DB_NOFILL);
d3c2ae1c 3050 dbuf_destroy(db);
34dc7c2f 3051 } else if (arc_released(db->db_buf)) {
34dc7c2f
BB
3052 /*
3053 * This dbuf has anonymous data associated with it.
3054 */
d3c2ae1c 3055 dbuf_destroy(db);
34dc7c2f 3056 } else {
d3c2ae1c
GW
3057 boolean_t do_arc_evict = B_FALSE;
3058 blkptr_t bp;
3059 spa_t *spa = dmu_objset_spa(db->db_objset);
3060
3061 if (!DBUF_IS_CACHEABLE(db) &&
3062 db->db_blkptr != NULL &&
3063 !BP_IS_HOLE(db->db_blkptr) &&
3064 !BP_IS_EMBEDDED(db->db_blkptr)) {
3065 do_arc_evict = B_TRUE;
3066 bp = *db->db_blkptr;
3067 }
1eb5bfa3 3068
d3c2ae1c
GW
3069 if (!DBUF_IS_CACHEABLE(db) ||
3070 db->db_pending_evict) {
3071 dbuf_destroy(db);
3072 } else if (!multilist_link_active(&db->db_cache_link)) {
64fc7762 3073 multilist_insert(dbuf_cache, db);
d3c2ae1c
GW
3074 (void) refcount_add_many(&dbuf_cache_size,
3075 db->db.db_size, db);
b128c09f 3076 mutex_exit(&db->db_mtx);
d3c2ae1c
GW
3077
3078 dbuf_evict_notify();
bd089c54 3079 }
d3c2ae1c
GW
3080
3081 if (do_arc_evict)
3082 arc_freed(spa, &bp);
34dc7c2f
BB
3083 }
3084 } else {
3085 mutex_exit(&db->db_mtx);
3086 }
d3c2ae1c 3087
34dc7c2f
BB
3088}
3089
3090#pragma weak dmu_buf_refcount = dbuf_refcount
3091uint64_t
3092dbuf_refcount(dmu_buf_impl_t *db)
3093{
3094 return (refcount_count(&db->db_holds));
3095}
3096
3097void *
0c66c32d
JG
3098dmu_buf_replace_user(dmu_buf_t *db_fake, dmu_buf_user_t *old_user,
3099 dmu_buf_user_t *new_user)
34dc7c2f 3100{
0c66c32d
JG
3101 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
3102
3103 mutex_enter(&db->db_mtx);
3104 dbuf_verify_user(db, DBVU_NOT_EVICTING);
3105 if (db->db_user == old_user)
3106 db->db_user = new_user;
3107 else
3108 old_user = db->db_user;
3109 dbuf_verify_user(db, DBVU_NOT_EVICTING);
3110 mutex_exit(&db->db_mtx);
3111
3112 return (old_user);
34dc7c2f
BB
3113}
3114
3115void *
0c66c32d 3116dmu_buf_set_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
34dc7c2f 3117{
0c66c32d 3118 return (dmu_buf_replace_user(db_fake, NULL, user));
34dc7c2f
BB
3119}
3120
3121void *
0c66c32d 3122dmu_buf_set_user_ie(dmu_buf_t *db_fake, dmu_buf_user_t *user)
34dc7c2f
BB
3123{
3124 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
34dc7c2f 3125
bc4501f7 3126 db->db_user_immediate_evict = TRUE;
0c66c32d
JG
3127 return (dmu_buf_set_user(db_fake, user));
3128}
34dc7c2f 3129
0c66c32d
JG
3130void *
3131dmu_buf_remove_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
3132{
3133 return (dmu_buf_replace_user(db_fake, user, NULL));
34dc7c2f
BB
3134}
3135
3136void *
3137dmu_buf_get_user(dmu_buf_t *db_fake)
3138{
3139 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
34dc7c2f 3140
0c66c32d
JG
3141 dbuf_verify_user(db, DBVU_NOT_EVICTING);
3142 return (db->db_user);
3143}
3144
3145void
3146dmu_buf_user_evict_wait()
3147{
3148 taskq_wait(dbu_evict_taskq);
34dc7c2f
BB
3149}
3150
03c6040b
GW
3151blkptr_t *
3152dmu_buf_get_blkptr(dmu_buf_t *db)
3153{
3154 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
3155 return (dbi->db_blkptr);
3156}
3157
8bea9815
MA
3158objset_t *
3159dmu_buf_get_objset(dmu_buf_t *db)
3160{
3161 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
3162 return (dbi->db_objset);
3163}
3164
2bce8049
MA
3165dnode_t *
3166dmu_buf_dnode_enter(dmu_buf_t *db)
3167{
3168 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
3169 DB_DNODE_ENTER(dbi);
3170 return (DB_DNODE(dbi));
3171}
3172
3173void
3174dmu_buf_dnode_exit(dmu_buf_t *db)
3175{
3176 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
3177 DB_DNODE_EXIT(dbi);
3178}
3179
34dc7c2f
BB
3180static void
3181dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db)
3182{
3183 /* ASSERT(dmu_tx_is_syncing(tx) */
3184 ASSERT(MUTEX_HELD(&db->db_mtx));
3185
3186 if (db->db_blkptr != NULL)
3187 return;
3188
428870ff 3189 if (db->db_blkid == DMU_SPILL_BLKID) {
50c957f7 3190 db->db_blkptr = DN_SPILL_BLKPTR(dn->dn_phys);
428870ff
BB
3191 BP_ZERO(db->db_blkptr);
3192 return;
3193 }
34dc7c2f
BB
3194 if (db->db_level == dn->dn_phys->dn_nlevels-1) {
3195 /*
3196 * This buffer was allocated at a time when there was
3197 * no available blkptrs from the dnode, or it was
3198 * inappropriate to hook it in (i.e., nlevels mis-match).
3199 */
3200 ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr);
3201 ASSERT(db->db_parent == NULL);
3202 db->db_parent = dn->dn_dbuf;
3203 db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
3204 DBUF_VERIFY(db);
3205 } else {
3206 dmu_buf_impl_t *parent = db->db_parent;
3207 int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
3208
3209 ASSERT(dn->dn_phys->dn_nlevels > 1);
3210 if (parent == NULL) {
3211 mutex_exit(&db->db_mtx);
3212 rw_enter(&dn->dn_struct_rwlock, RW_READER);
fcff0f35
PD
3213 parent = dbuf_hold_level(dn, db->db_level + 1,
3214 db->db_blkid >> epbs, db);
34dc7c2f
BB
3215 rw_exit(&dn->dn_struct_rwlock);
3216 mutex_enter(&db->db_mtx);
3217 db->db_parent = parent;
3218 }
3219 db->db_blkptr = (blkptr_t *)parent->db.db_data +
3220 (db->db_blkid & ((1ULL << epbs) - 1));
3221 DBUF_VERIFY(db);
3222 }
3223}
3224
b5256303
TC
3225/*
3226 * Ensure the dbuf's data is untransformed if the associated dirty
3227 * record requires it. This is used by dbuf_sync_leaf() to ensure
3228 * that a dnode block is decrypted before we write new data to it.
3229 * For raw writes we assert that the buffer is already encrypted.
3230 */
3231static void
3232dbuf_check_crypt(dbuf_dirty_record_t *dr)
3233{
3234 int err;
3235 dmu_buf_impl_t *db = dr->dr_dbuf;
3236
3237 ASSERT(MUTEX_HELD(&db->db_mtx));
3238
3239 if (!dr->dt.dl.dr_raw && arc_is_encrypted(db->db_buf)) {
3240 /*
3241 * Unfortunately, there is currently no mechanism for
3242 * syncing context to handle decryption errors. An error
3243 * here is only possible if an attacker maliciously
3244 * changed a dnode block and updated the associated
3245 * checksums going up the block tree.
3246 */
3247 err = arc_untransform(db->db_buf, db->db_objset->os_spa,
3248 dmu_objset_id(db->db_objset), B_TRUE);
3249 if (err)
3250 panic("Invalid dnode block MAC");
3251 } else if (dr->dt.dl.dr_raw) {
3252 /*
3253 * Writing raw encrypted data requires the db's arc buffer
3254 * to be converted to raw by the caller.
3255 */
3256 ASSERT(arc_is_encrypted(db->db_buf));
3257 }
3258}
3259
d1d7e268
MK
3260/*
3261 * dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
60948de1
BB
3262 * is critical the we not allow the compiler to inline this function in to
3263 * dbuf_sync_list() thereby drastically bloating the stack usage.
3264 */
3265noinline static void
34dc7c2f
BB
3266dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
3267{
3268 dmu_buf_impl_t *db = dr->dr_dbuf;
572e2857 3269 dnode_t *dn;
34dc7c2f
BB
3270 zio_t *zio;
3271
3272 ASSERT(dmu_tx_is_syncing(tx));
3273
3274 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
3275
3276 mutex_enter(&db->db_mtx);
3277
3278 ASSERT(db->db_level > 0);
3279 DBUF_VERIFY(db);
3280
e49f1e20 3281 /* Read the block if it hasn't been read yet. */
34dc7c2f
BB
3282 if (db->db_buf == NULL) {
3283 mutex_exit(&db->db_mtx);
3284 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
3285 mutex_enter(&db->db_mtx);
3286 }
3287 ASSERT3U(db->db_state, ==, DB_CACHED);
34dc7c2f
BB
3288 ASSERT(db->db_buf != NULL);
3289
572e2857
BB
3290 DB_DNODE_ENTER(db);
3291 dn = DB_DNODE(db);
e49f1e20 3292 /* Indirect block size must match what the dnode thinks it is. */
572e2857 3293 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
34dc7c2f 3294 dbuf_check_blkptr(dn, db);
572e2857 3295 DB_DNODE_EXIT(db);
34dc7c2f 3296
e49f1e20 3297 /* Provide the pending dirty record to child dbufs */
34dc7c2f
BB
3298 db->db_data_pending = dr;
3299
34dc7c2f 3300 mutex_exit(&db->db_mtx);
b128c09f 3301 dbuf_write(dr, db->db_buf, tx);
34dc7c2f
BB
3302
3303 zio = dr->dr_zio;
3304 mutex_enter(&dr->dt.di.dr_mtx);
4bda3bd0 3305 dbuf_sync_list(&dr->dt.di.dr_children, db->db_level - 1, tx);
34dc7c2f
BB
3306 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
3307 mutex_exit(&dr->dt.di.dr_mtx);
3308 zio_nowait(zio);
3309}
3310
d1d7e268
MK
3311/*
3312 * dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
60948de1
BB
3313 * critical the we not allow the compiler to inline this function in to
3314 * dbuf_sync_list() thereby drastically bloating the stack usage.
3315 */
3316noinline static void
34dc7c2f
BB
3317dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
3318{
3319 arc_buf_t **datap = &dr->dt.dl.dr_data;
3320 dmu_buf_impl_t *db = dr->dr_dbuf;
572e2857
BB
3321 dnode_t *dn;
3322 objset_t *os;
34dc7c2f 3323 uint64_t txg = tx->tx_txg;
34dc7c2f
BB
3324
3325 ASSERT(dmu_tx_is_syncing(tx));
3326
3327 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
3328
3329 mutex_enter(&db->db_mtx);
3330 /*
3331 * To be synced, we must be dirtied. But we
3332 * might have been freed after the dirty.
3333 */
3334 if (db->db_state == DB_UNCACHED) {
3335 /* This buffer has been freed since it was dirtied */
3336 ASSERT(db->db.db_data == NULL);
3337 } else if (db->db_state == DB_FILL) {
3338 /* This buffer was freed and is now being re-filled */
3339 ASSERT(db->db.db_data != dr->dt.dl.dr_data);
3340 } else {
b128c09f 3341 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL);
34dc7c2f
BB
3342 }
3343 DBUF_VERIFY(db);
3344
572e2857
BB
3345 DB_DNODE_ENTER(db);
3346 dn = DB_DNODE(db);
3347
428870ff
BB
3348 if (db->db_blkid == DMU_SPILL_BLKID) {
3349 mutex_enter(&dn->dn_mtx);
81edd3e8
P
3350 if (!(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) {
3351 /*
3352 * In the previous transaction group, the bonus buffer
3353 * was entirely used to store the attributes for the
3354 * dnode which overrode the dn_spill field. However,
3355 * when adding more attributes to the file a spill
3356 * block was required to hold the extra attributes.
3357 *
3358 * Make sure to clear the garbage left in the dn_spill
3359 * field from the previous attributes in the bonus
3360 * buffer. Otherwise, after writing out the spill
3361 * block to the new allocated dva, it will free
3362 * the old block pointed to by the invalid dn_spill.
3363 */
3364 db->db_blkptr = NULL;
3365 }
428870ff
BB
3366 dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR;
3367 mutex_exit(&dn->dn_mtx);
3368 }
3369
34dc7c2f
BB
3370 /*
3371 * If this is a bonus buffer, simply copy the bonus data into the
3372 * dnode. It will be written out when the dnode is synced (and it
3373 * will be synced, since it must have been dirty for dbuf_sync to
3374 * be called).
3375 */
428870ff 3376 if (db->db_blkid == DMU_BONUS_BLKID) {
34dc7c2f
BB
3377 dbuf_dirty_record_t **drp;
3378
3379 ASSERT(*datap != NULL);
c99c9001 3380 ASSERT0(db->db_level);
b5256303 3381 ASSERT3U(DN_MAX_BONUS_LEN(dn->dn_phys), <=,
50c957f7 3382 DN_SLOTS_TO_BONUSLEN(dn->dn_phys->dn_extra_slots + 1));
b5256303
TC
3383 bcopy(*datap, DN_BONUS(dn->dn_phys),
3384 DN_MAX_BONUS_LEN(dn->dn_phys));
572e2857
BB
3385 DB_DNODE_EXIT(db);
3386
34dc7c2f 3387 if (*datap != db->db.db_data) {
50c957f7
NB
3388 int slots = DB_DNODE(db)->dn_num_slots;
3389 int bonuslen = DN_SLOTS_TO_BONUSLEN(slots);
a3fd9d9e 3390 kmem_free(*datap, bonuslen);
25458cbe 3391 arc_space_return(bonuslen, ARC_SPACE_BONUS);
34dc7c2f
BB
3392 }
3393 db->db_data_pending = NULL;
3394 drp = &db->db_last_dirty;
3395 while (*drp != dr)
3396 drp = &(*drp)->dr_next;
3397 ASSERT(dr->dr_next == NULL);
428870ff 3398 ASSERT(dr->dr_dbuf == db);
34dc7c2f 3399 *drp = dr->dr_next;
753972fc
BB
3400 if (dr->dr_dbuf->db_level != 0) {
3401 mutex_destroy(&dr->dt.di.dr_mtx);
3402 list_destroy(&dr->dt.di.dr_children);
3403 }
34dc7c2f
BB
3404 kmem_free(dr, sizeof (dbuf_dirty_record_t));
3405 ASSERT(db->db_dirtycnt > 0);
3406 db->db_dirtycnt -= 1;
428870ff 3407 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
34dc7c2f
BB
3408 return;
3409 }
3410
572e2857
BB
3411 os = dn->dn_objset;
3412
34dc7c2f
BB
3413 /*
3414 * This function may have dropped the db_mtx lock allowing a dmu_sync
3415 * operation to sneak in. As a result, we need to ensure that we
3416 * don't check the dr_override_state until we have returned from
3417 * dbuf_check_blkptr.
3418 */
3419 dbuf_check_blkptr(dn, db);
3420
3421 /*
572e2857 3422 * If this buffer is in the middle of an immediate write,
34dc7c2f
BB
3423 * wait for the synchronous IO to complete.
3424 */
3425 while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
3426 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
3427 cv_wait(&db->db_changed, &db->db_mtx);
3428 ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN);
3429 }
3430
b5256303
TC
3431 /*
3432 * If this is a dnode block, ensure it is appropriately encrypted
3433 * or decrypted, depending on what we are writing to it this txg.
3434 */
3435 if (os->os_encrypted && dn->dn_object == DMU_META_DNODE_OBJECT)
3436 dbuf_check_crypt(dr);
3437
9babb374
BB
3438 if (db->db_state != DB_NOFILL &&
3439 dn->dn_object != DMU_META_DNODE_OBJECT &&
3440 refcount_count(&db->db_holds) > 1 &&
428870ff 3441 dr->dt.dl.dr_override_state != DR_OVERRIDDEN &&
9babb374
BB
3442 *datap == db->db_buf) {
3443 /*
3444 * If this buffer is currently "in use" (i.e., there
3445 * are active holds and db_data still references it),
3446 * then make a copy before we start the write so that
3447 * any modifications from the open txg will not leak
3448 * into this write.
3449 *
3450 * NOTE: this copy does not need to be made for
3451 * objects only modified in the syncing context (e.g.
3452 * DNONE_DNODE blocks).
3453 */
2aa34383 3454 int psize = arc_buf_size(*datap);
b5256303 3455 int lsize = arc_buf_lsize(*datap);
9babb374 3456 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
2aa34383
DK
3457 enum zio_compress compress_type = arc_get_compression(*datap);
3458
b5256303
TC
3459 if (arc_is_encrypted(*datap)) {
3460 boolean_t byteorder;
3461 uint8_t salt[ZIO_DATA_SALT_LEN];
3462 uint8_t iv[ZIO_DATA_IV_LEN];
3463 uint8_t mac[ZIO_DATA_MAC_LEN];
3464
3465 arc_get_raw_params(*datap, &byteorder, salt, iv, mac);
3466 *datap = arc_alloc_raw_buf(os->os_spa, db,
3467 dmu_objset_id(os), byteorder, salt, iv, mac,
3468 dn->dn_type, psize, lsize, compress_type);
3469 } else if (compress_type != ZIO_COMPRESS_OFF) {
2aa34383
DK
3470 ASSERT3U(type, ==, ARC_BUFC_DATA);
3471 *datap = arc_alloc_compressed_buf(os->os_spa, db,
3472 psize, lsize, compress_type);
b5256303
TC
3473 } else {
3474 *datap = arc_alloc_buf(os->os_spa, db, type, psize);
2aa34383
DK
3475 }
3476 bcopy(db->db.db_data, (*datap)->b_data, psize);
b128c09f 3477 }
34dc7c2f
BB
3478 db->db_data_pending = dr;
3479
3480 mutex_exit(&db->db_mtx);
3481
b128c09f 3482 dbuf_write(dr, *datap, tx);
34dc7c2f
BB
3483
3484 ASSERT(!list_link_active(&dr->dr_dirty_node));
572e2857 3485 if (dn->dn_object == DMU_META_DNODE_OBJECT) {
34dc7c2f 3486 list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr);
572e2857
BB
3487 DB_DNODE_EXIT(db);
3488 } else {
3489 /*
3490 * Although zio_nowait() does not "wait for an IO", it does
3491 * initiate the IO. If this is an empty write it seems plausible
3492 * that the IO could actually be completed before the nowait
3493 * returns. We need to DB_DNODE_EXIT() first in case
3494 * zio_nowait() invalidates the dbuf.
3495 */
3496 DB_DNODE_EXIT(db);
34dc7c2f 3497 zio_nowait(dr->dr_zio);
572e2857 3498 }
34dc7c2f
BB
3499}
3500
3501void
4bda3bd0 3502dbuf_sync_list(list_t *list, int level, dmu_tx_t *tx)
34dc7c2f
BB
3503{
3504 dbuf_dirty_record_t *dr;
3505
c65aa5b2 3506 while ((dr = list_head(list))) {
34dc7c2f
BB
3507 if (dr->dr_zio != NULL) {
3508 /*
3509 * If we find an already initialized zio then we
3510 * are processing the meta-dnode, and we have finished.
3511 * The dbufs for all dnodes are put back on the list
3512 * during processing, so that we can zio_wait()
3513 * these IOs after initiating all child IOs.
3514 */
3515 ASSERT3U(dr->dr_dbuf->db.db_object, ==,
3516 DMU_META_DNODE_OBJECT);
3517 break;
3518 }
4bda3bd0
MA
3519 if (dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
3520 dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
3521 VERIFY3U(dr->dr_dbuf->db_level, ==, level);
3522 }
34dc7c2f
BB
3523 list_remove(list, dr);
3524 if (dr->dr_dbuf->db_level > 0)
3525 dbuf_sync_indirect(dr, tx);
3526 else
3527 dbuf_sync_leaf(dr, tx);
3528 }
3529}
3530
34dc7c2f
BB
3531/* ARGSUSED */
3532static void
3533dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
3534{
3535 dmu_buf_impl_t *db = vdb;
572e2857 3536 dnode_t *dn;
b128c09f 3537 blkptr_t *bp = zio->io_bp;
34dc7c2f 3538 blkptr_t *bp_orig = &zio->io_bp_orig;
428870ff
BB
3539 spa_t *spa = zio->io_spa;
3540 int64_t delta;
34dc7c2f 3541 uint64_t fill = 0;
428870ff 3542 int i;
34dc7c2f 3543
463a8cfe
AR
3544 ASSERT3P(db->db_blkptr, !=, NULL);
3545 ASSERT3P(&db->db_data_pending->dr_bp_copy, ==, bp);
b128c09f 3546
572e2857
BB
3547 DB_DNODE_ENTER(db);
3548 dn = DB_DNODE(db);
428870ff
BB
3549 delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig);
3550 dnode_diduse_space(dn, delta - zio->io_prev_space_delta);
3551 zio->io_prev_space_delta = delta;
34dc7c2f 3552
b0bc7a84
MG
3553 if (bp->blk_birth != 0) {
3554 ASSERT((db->db_blkid != DMU_SPILL_BLKID &&
3555 BP_GET_TYPE(bp) == dn->dn_type) ||
3556 (db->db_blkid == DMU_SPILL_BLKID &&
9b67f605
MA
3557 BP_GET_TYPE(bp) == dn->dn_bonustype) ||
3558 BP_IS_EMBEDDED(bp));
b0bc7a84 3559 ASSERT(BP_GET_LEVEL(bp) == db->db_level);
34dc7c2f
BB
3560 }
3561
3562 mutex_enter(&db->db_mtx);
3563
428870ff
BB
3564#ifdef ZFS_DEBUG
3565 if (db->db_blkid == DMU_SPILL_BLKID) {
428870ff 3566 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
463a8cfe 3567 ASSERT(!(BP_IS_HOLE(bp)) &&
50c957f7 3568 db->db_blkptr == DN_SPILL_BLKPTR(dn->dn_phys));
428870ff
BB
3569 }
3570#endif
3571
34dc7c2f
BB
3572 if (db->db_level == 0) {
3573 mutex_enter(&dn->dn_mtx);
428870ff
BB
3574 if (db->db_blkid > dn->dn_phys->dn_maxblkid &&
3575 db->db_blkid != DMU_SPILL_BLKID)
34dc7c2f
BB
3576 dn->dn_phys->dn_maxblkid = db->db_blkid;
3577 mutex_exit(&dn->dn_mtx);
3578
3579 if (dn->dn_type == DMU_OT_DNODE) {
50c957f7
NB
3580 i = 0;
3581 while (i < db->db.db_size) {
817b1b6e
MA
3582 dnode_phys_t *dnp =
3583 (void *)(((char *)db->db.db_data) + i);
50c957f7
NB
3584
3585 i += DNODE_MIN_SIZE;
3586 if (dnp->dn_type != DMU_OT_NONE) {
34dc7c2f 3587 fill++;
50c957f7
NB
3588 i += dnp->dn_extra_slots *
3589 DNODE_MIN_SIZE;
3590 }
34dc7c2f
BB
3591 }
3592 } else {
b0bc7a84
MG
3593 if (BP_IS_HOLE(bp)) {
3594 fill = 0;
3595 } else {
3596 fill = 1;
3597 }
34dc7c2f
BB
3598 }
3599 } else {
b128c09f 3600 blkptr_t *ibp = db->db.db_data;
34dc7c2f 3601 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
b128c09f
BB
3602 for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) {
3603 if (BP_IS_HOLE(ibp))
34dc7c2f 3604 continue;
9b67f605 3605 fill += BP_GET_FILL(ibp);
34dc7c2f
BB
3606 }
3607 }
572e2857 3608 DB_DNODE_EXIT(db);
34dc7c2f 3609
9b67f605 3610 if (!BP_IS_EMBEDDED(bp))
b5256303 3611 BP_SET_FILL(bp, fill);
34dc7c2f
BB
3612
3613 mutex_exit(&db->db_mtx);
463a8cfe
AR
3614
3615 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
3616 *db->db_blkptr = *bp;
3617 rw_exit(&dn->dn_struct_rwlock);
34dc7c2f
BB
3618}
3619
bc77ba73
PD
3620/* ARGSUSED */
3621/*
3622 * This function gets called just prior to running through the compression
3623 * stage of the zio pipeline. If we're an indirect block comprised of only
3624 * holes, then we want this indirect to be compressed away to a hole. In
3625 * order to do that we must zero out any information about the holes that
3626 * this indirect points to prior to before we try to compress it.
3627 */
3628static void
3629dbuf_write_children_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
3630{
3631 dmu_buf_impl_t *db = vdb;
3632 dnode_t *dn;
3633 blkptr_t *bp;
721ed0ee 3634 unsigned int epbs, i;
bc77ba73
PD
3635
3636 ASSERT3U(db->db_level, >, 0);
3637 DB_DNODE_ENTER(db);
3638 dn = DB_DNODE(db);
3639 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
721ed0ee 3640 ASSERT3U(epbs, <, 31);
bc77ba73
PD
3641
3642 /* Determine if all our children are holes */
3f93077b 3643 for (i = 0, bp = db->db.db_data; i < 1ULL << epbs; i++, bp++) {
bc77ba73
PD
3644 if (!BP_IS_HOLE(bp))
3645 break;
3646 }
3647
3648 /*
3649 * If all the children are holes, then zero them all out so that
3650 * we may get compressed away.
3651 */
3f93077b 3652 if (i == 1ULL << epbs) {
721ed0ee
GM
3653 /*
3654 * We only found holes. Grab the rwlock to prevent
3655 * anybody from reading the blocks we're about to
3656 * zero out.
3657 */
3658 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
bc77ba73 3659 bzero(db->db.db_data, db->db.db_size);
721ed0ee 3660 rw_exit(&dn->dn_struct_rwlock);
bc77ba73
PD
3661 }
3662 DB_DNODE_EXIT(db);
3663}
3664
e8b96c60
MA
3665/*
3666 * The SPA will call this callback several times for each zio - once
3667 * for every physical child i/o (zio->io_phys_children times). This
3668 * allows the DMU to monitor the progress of each logical i/o. For example,
3669 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
3670 * block. There may be a long delay before all copies/fragments are completed,
3671 * so this callback allows us to retire dirty space gradually, as the physical
3672 * i/os complete.
3673 */
3674/* ARGSUSED */
3675static void
3676dbuf_write_physdone(zio_t *zio, arc_buf_t *buf, void *arg)
3677{
3678 dmu_buf_impl_t *db = arg;
3679 objset_t *os = db->db_objset;
3680 dsl_pool_t *dp = dmu_objset_pool(os);
3681 dbuf_dirty_record_t *dr;
3682 int delta = 0;
3683
3684 dr = db->db_data_pending;
3685 ASSERT3U(dr->dr_txg, ==, zio->io_txg);
3686
3687 /*
3688 * The callback will be called io_phys_children times. Retire one
3689 * portion of our dirty space each time we are called. Any rounding
3690 * error will be cleaned up by dsl_pool_sync()'s call to
3691 * dsl_pool_undirty_space().
3692 */
3693 delta = dr->dr_accounted / zio->io_phys_children;
3694 dsl_pool_undirty_space(dp, delta, zio->io_txg);
3695}
3696
34dc7c2f
BB
3697/* ARGSUSED */
3698static void
3699dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
3700{
3701 dmu_buf_impl_t *db = vdb;
428870ff 3702 blkptr_t *bp_orig = &zio->io_bp_orig;
b0bc7a84
MG
3703 blkptr_t *bp = db->db_blkptr;
3704 objset_t *os = db->db_objset;
3705 dmu_tx_t *tx = os->os_synctx;
34dc7c2f
BB
3706 dbuf_dirty_record_t **drp, *dr;
3707
c99c9001 3708 ASSERT0(zio->io_error);
428870ff
BB
3709 ASSERT(db->db_blkptr == bp);
3710
03c6040b
GW
3711 /*
3712 * For nopwrites and rewrites we ensure that the bp matches our
3713 * original and bypass all the accounting.
3714 */
3715 if (zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)) {
428870ff
BB
3716 ASSERT(BP_EQUAL(bp, bp_orig));
3717 } else {
b0bc7a84 3718 dsl_dataset_t *ds = os->os_dsl_dataset;
428870ff
BB
3719 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
3720 dsl_dataset_block_born(ds, bp, tx);
3721 }
34dc7c2f
BB
3722
3723 mutex_enter(&db->db_mtx);
3724
428870ff
BB
3725 DBUF_VERIFY(db);
3726
34dc7c2f
BB
3727 drp = &db->db_last_dirty;
3728 while ((dr = *drp) != db->db_data_pending)
3729 drp = &dr->dr_next;
3730 ASSERT(!list_link_active(&dr->dr_dirty_node));
428870ff 3731 ASSERT(dr->dr_dbuf == db);
34dc7c2f
BB
3732 ASSERT(dr->dr_next == NULL);
3733 *drp = dr->dr_next;
3734
428870ff
BB
3735#ifdef ZFS_DEBUG
3736 if (db->db_blkid == DMU_SPILL_BLKID) {
572e2857
BB
3737 dnode_t *dn;
3738
3739 DB_DNODE_ENTER(db);
3740 dn = DB_DNODE(db);
428870ff
BB
3741 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
3742 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
50c957f7 3743 db->db_blkptr == DN_SPILL_BLKPTR(dn->dn_phys));
572e2857 3744 DB_DNODE_EXIT(db);
428870ff
BB
3745 }
3746#endif
3747
34dc7c2f 3748 if (db->db_level == 0) {
428870ff 3749 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
34dc7c2f 3750 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
b128c09f
BB
3751 if (db->db_state != DB_NOFILL) {
3752 if (dr->dt.dl.dr_data != db->db_buf)
d3c2ae1c 3753 arc_buf_destroy(dr->dt.dl.dr_data, db);
b128c09f 3754 }
34dc7c2f 3755 } else {
572e2857
BB
3756 dnode_t *dn;
3757
3758 DB_DNODE_ENTER(db);
3759 dn = DB_DNODE(db);
34dc7c2f 3760 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
b0bc7a84 3761 ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
34dc7c2f 3762 if (!BP_IS_HOLE(db->db_blkptr)) {
1fde1e37
BB
3763 ASSERTV(int epbs = dn->dn_phys->dn_indblkshift -
3764 SPA_BLKPTRSHIFT);
b0bc7a84
MG
3765 ASSERT3U(db->db_blkid, <=,
3766 dn->dn_phys->dn_maxblkid >> (db->db_level * epbs));
34dc7c2f
BB
3767 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
3768 db->db.db_size);
34dc7c2f 3769 }
572e2857 3770 DB_DNODE_EXIT(db);
34dc7c2f
BB
3771 mutex_destroy(&dr->dt.di.dr_mtx);
3772 list_destroy(&dr->dt.di.dr_children);
3773 }
3774 kmem_free(dr, sizeof (dbuf_dirty_record_t));
3775
3776 cv_broadcast(&db->db_changed);
3777 ASSERT(db->db_dirtycnt > 0);
3778 db->db_dirtycnt -= 1;
3779 db->db_data_pending = NULL;
b0bc7a84 3780 dbuf_rele_and_unlock(db, (void *)(uintptr_t)tx->tx_txg);
428870ff
BB
3781}
3782
3783static void
3784dbuf_write_nofill_ready(zio_t *zio)
3785{
3786 dbuf_write_ready(zio, NULL, zio->io_private);
3787}
3788
3789static void
3790dbuf_write_nofill_done(zio_t *zio)
3791{
3792 dbuf_write_done(zio, NULL, zio->io_private);
3793}
3794
3795static void
3796dbuf_write_override_ready(zio_t *zio)
3797{
3798 dbuf_dirty_record_t *dr = zio->io_private;
3799 dmu_buf_impl_t *db = dr->dr_dbuf;
3800
3801 dbuf_write_ready(zio, NULL, db);
3802}
3803
3804static void
3805dbuf_write_override_done(zio_t *zio)
3806{
3807 dbuf_dirty_record_t *dr = zio->io_private;
3808 dmu_buf_impl_t *db = dr->dr_dbuf;
3809 blkptr_t *obp = &dr->dt.dl.dr_overridden_by;
3810
3811 mutex_enter(&db->db_mtx);
3812 if (!BP_EQUAL(zio->io_bp, obp)) {
3813 if (!BP_IS_HOLE(obp))
3814 dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp);
3815 arc_release(dr->dt.dl.dr_data, db);
3816 }
34dc7c2f
BB
3817 mutex_exit(&db->db_mtx);
3818
428870ff 3819 dbuf_write_done(zio, NULL, db);
a6255b7f
DQ
3820
3821 if (zio->io_abd != NULL)
3822 abd_put(zio->io_abd);
428870ff
BB
3823}
3824
e49f1e20 3825/* Issue I/O to commit a dirty buffer to disk. */
428870ff
BB
3826static void
3827dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx)
3828{
3829 dmu_buf_impl_t *db = dr->dr_dbuf;
572e2857
BB
3830 dnode_t *dn;
3831 objset_t *os;
428870ff
BB
3832 dmu_buf_impl_t *parent = db->db_parent;
3833 uint64_t txg = tx->tx_txg;
5dbd68a3 3834 zbookmark_phys_t zb;
428870ff
BB
3835 zio_prop_t zp;
3836 zio_t *zio;
3837 int wp_flag = 0;
34dc7c2f 3838
463a8cfe
AR
3839 ASSERT(dmu_tx_is_syncing(tx));
3840
572e2857
BB
3841 DB_DNODE_ENTER(db);
3842 dn = DB_DNODE(db);
3843 os = dn->dn_objset;
3844
428870ff
BB
3845 if (db->db_state != DB_NOFILL) {
3846 if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) {
3847 /*
3848 * Private object buffers are released here rather
3849 * than in dbuf_dirty() since they are only modified
3850 * in the syncing context and we don't want the
3851 * overhead of making multiple copies of the data.
3852 */
3853 if (BP_IS_HOLE(db->db_blkptr)) {
3854 arc_buf_thaw(data);
3855 } else {
3856 dbuf_release_bp(db);
3857 }
3858 }
3859 }
3860
3861 if (parent != dn->dn_dbuf) {
e49f1e20
WA
3862 /* Our parent is an indirect block. */
3863 /* We have a dirty parent that has been scheduled for write. */
428870ff 3864 ASSERT(parent && parent->db_data_pending);
e49f1e20 3865 /* Our parent's buffer is one level closer to the dnode. */
428870ff 3866 ASSERT(db->db_level == parent->db_level-1);
e49f1e20
WA
3867 /*
3868 * We're about to modify our parent's db_data by modifying
3869 * our block pointer, so the parent must be released.
3870 */
428870ff
BB
3871 ASSERT(arc_released(parent->db_buf));
3872 zio = parent->db_data_pending->dr_zio;
3873 } else {
e49f1e20 3874 /* Our parent is the dnode itself. */
428870ff
BB
3875 ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 &&
3876 db->db_blkid != DMU_SPILL_BLKID) ||
3877 (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0));
3878 if (db->db_blkid != DMU_SPILL_BLKID)
3879 ASSERT3P(db->db_blkptr, ==,
3880 &dn->dn_phys->dn_blkptr[db->db_blkid]);
3881 zio = dn->dn_zio;
3882 }
3883
3884 ASSERT(db->db_level == 0 || data == db->db_buf);
3885 ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
3886 ASSERT(zio);
3887
3888 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
3889 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
3890 db->db.db_object, db->db_level, db->db_blkid);
3891
3892 if (db->db_blkid == DMU_SPILL_BLKID)
3893 wp_flag = WP_SPILL;
3894 wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
3895
82644107 3896 dmu_write_policy(os, dn, db->db_level, wp_flag, &zp);
572e2857 3897 DB_DNODE_EXIT(db);
428870ff 3898
463a8cfe
AR
3899 /*
3900 * We copy the blkptr now (rather than when we instantiate the dirty
3901 * record), because its value can change between open context and
3902 * syncing context. We do not need to hold dn_struct_rwlock to read
3903 * db_blkptr because we are in syncing context.
3904 */
3905 dr->dr_bp_copy = *db->db_blkptr;
3906
9b67f605
MA
3907 if (db->db_level == 0 &&
3908 dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
3909 /*
3910 * The BP for this block has been provided by open context
3911 * (by dmu_sync() or dmu_buf_write_embedded()).
3912 */
a6255b7f
DQ
3913 abd_t *contents = (data != NULL) ?
3914 abd_get_from_buf(data->b_data, arc_buf_size(data)) : NULL;
9b67f605 3915
428870ff 3916 dr->dr_zio = zio_write(zio, os->os_spa, txg,
2aa34383
DK
3917 &dr->dr_bp_copy, contents, db->db.db_size, db->db.db_size,
3918 &zp, dbuf_write_override_ready, NULL, NULL,
bc77ba73 3919 dbuf_write_override_done,
e8b96c60 3920 dr, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
428870ff
BB
3921 mutex_enter(&db->db_mtx);
3922 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
3923 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by,
03c6040b 3924 dr->dt.dl.dr_copies, dr->dt.dl.dr_nopwrite);
428870ff
BB
3925 mutex_exit(&db->db_mtx);
3926 } else if (db->db_state == DB_NOFILL) {
3c67d83a
TH
3927 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF ||
3928 zp.zp_checksum == ZIO_CHECKSUM_NOPARITY);
428870ff 3929 dr->dr_zio = zio_write(zio, os->os_spa, txg,
2aa34383 3930 &dr->dr_bp_copy, NULL, db->db.db_size, db->db.db_size, &zp,
bc77ba73
PD
3931 dbuf_write_nofill_ready, NULL, NULL,
3932 dbuf_write_nofill_done, db,
428870ff
BB
3933 ZIO_PRIORITY_ASYNC_WRITE,
3934 ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb);
3935 } else {
b5256303 3936 arc_write_done_func_t *children_ready_cb = NULL;
428870ff 3937 ASSERT(arc_released(data));
bc77ba73
PD
3938
3939 /*
3940 * For indirect blocks, we want to setup the children
3941 * ready callback so that we can properly handle an indirect
3942 * block that only contains holes.
3943 */
3944 if (db->db_level != 0)
3945 children_ready_cb = dbuf_write_children_ready;
3946
428870ff 3947 dr->dr_zio = arc_write(zio, os->os_spa, txg,
463a8cfe 3948 &dr->dr_bp_copy, data, DBUF_IS_L2CACHEABLE(db),
d3c2ae1c
GW
3949 &zp, dbuf_write_ready,
3950 children_ready_cb, dbuf_write_physdone,
3951 dbuf_write_done, db, ZIO_PRIORITY_ASYNC_WRITE,
3952 ZIO_FLAG_MUSTSUCCEED, &zb);
428870ff 3953 }
34dc7c2f 3954}
c28b2279
BB
3955
3956#if defined(_KERNEL) && defined(HAVE_SPL)
8f576c23
BB
3957EXPORT_SYMBOL(dbuf_find);
3958EXPORT_SYMBOL(dbuf_is_metadata);
d3c2ae1c 3959EXPORT_SYMBOL(dbuf_destroy);
8f576c23
BB
3960EXPORT_SYMBOL(dbuf_loan_arcbuf);
3961EXPORT_SYMBOL(dbuf_whichblock);
3962EXPORT_SYMBOL(dbuf_read);
3963EXPORT_SYMBOL(dbuf_unoverride);
3964EXPORT_SYMBOL(dbuf_free_range);
3965EXPORT_SYMBOL(dbuf_new_size);
3966EXPORT_SYMBOL(dbuf_release_bp);
3967EXPORT_SYMBOL(dbuf_dirty);
b5256303 3968EXPORT_SYMBOL(dmu_buf_will_change_crypt_params);
c28b2279 3969EXPORT_SYMBOL(dmu_buf_will_dirty);
8f576c23
BB
3970EXPORT_SYMBOL(dmu_buf_will_not_fill);
3971EXPORT_SYMBOL(dmu_buf_will_fill);
3972EXPORT_SYMBOL(dmu_buf_fill_done);
4047414a 3973EXPORT_SYMBOL(dmu_buf_rele);
8f576c23 3974EXPORT_SYMBOL(dbuf_assign_arcbuf);
8f576c23
BB
3975EXPORT_SYMBOL(dbuf_prefetch);
3976EXPORT_SYMBOL(dbuf_hold_impl);
3977EXPORT_SYMBOL(dbuf_hold);
3978EXPORT_SYMBOL(dbuf_hold_level);
3979EXPORT_SYMBOL(dbuf_create_bonus);
3980EXPORT_SYMBOL(dbuf_spill_set_blksz);
3981EXPORT_SYMBOL(dbuf_rm_spill);
3982EXPORT_SYMBOL(dbuf_add_ref);
3983EXPORT_SYMBOL(dbuf_rele);
3984EXPORT_SYMBOL(dbuf_rele_and_unlock);
3985EXPORT_SYMBOL(dbuf_refcount);
3986EXPORT_SYMBOL(dbuf_sync_list);
3987EXPORT_SYMBOL(dmu_buf_set_user);
3988EXPORT_SYMBOL(dmu_buf_set_user_ie);
8f576c23 3989EXPORT_SYMBOL(dmu_buf_get_user);
0f699108 3990EXPORT_SYMBOL(dmu_buf_get_blkptr);
d3c2ae1c 3991
02730c33 3992/* BEGIN CSTYLED */
d3c2ae1c
GW
3993module_param(dbuf_cache_max_bytes, ulong, 0644);
3994MODULE_PARM_DESC(dbuf_cache_max_bytes,
02730c33 3995 "Maximum size in bytes of the dbuf cache.");
d3c2ae1c
GW
3996
3997module_param(dbuf_cache_hiwater_pct, uint, 0644);
3998MODULE_PARM_DESC(dbuf_cache_hiwater_pct,
f974e25d 3999 "Percentage over dbuf_cache_max_bytes when dbufs must be evicted "
4000 "directly.");
d3c2ae1c
GW
4001
4002module_param(dbuf_cache_lowater_pct, uint, 0644);
4003MODULE_PARM_DESC(dbuf_cache_lowater_pct,
f974e25d 4004 "Percentage below dbuf_cache_max_bytes when the evict thread stops "
4005 "evicting dbufs.");
d3c2ae1c
GW
4006
4007module_param(dbuf_cache_max_shift, int, 0644);
4008MODULE_PARM_DESC(dbuf_cache_max_shift,
f974e25d 4009 "Cap the size of the dbuf cache to a log2 fraction of arc size.");
02730c33 4010/* END CSTYLED */
c28b2279 4011#endif