4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28 * Copyright (c) 2014 Integros [integros.com]
31 #include <sys/zfs_context.h>
33 #include <sys/dmu_send.h>
34 #include <sys/dmu_impl.h>
36 #include <sys/dmu_objset.h>
37 #include <sys/dsl_dataset.h>
38 #include <sys/dsl_dir.h>
39 #include <sys/dmu_tx.h>
42 #include <sys/dmu_zfetch.h>
44 #include <sys/sa_impl.h>
45 #include <sys/zfeature.h>
46 #include <sys/blkptr.h>
47 #include <sys/range_tree.h>
48 #include <sys/callb.h>
51 #include <sys/cityhash.h>
53 uint_t zfs_dbuf_evict_key;
55 static boolean_t dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
56 static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx);
59 extern inline void dmu_buf_init_user(dmu_buf_user_t *dbu,
60 dmu_buf_evict_func_t *evict_func_sync,
61 dmu_buf_evict_func_t *evict_func_async,
62 dmu_buf_t **clear_on_evict_dbufp);
66 * Global data structures and functions for the dbuf cache.
68 static kmem_cache_t *dbuf_kmem_cache;
69 static taskq_t *dbu_evict_taskq;
71 static kthread_t *dbuf_cache_evict_thread;
72 static kmutex_t dbuf_evict_lock;
73 static kcondvar_t dbuf_evict_cv;
74 static boolean_t dbuf_evict_thread_exit;
77 * LRU cache of dbufs. The dbuf cache maintains a list of dbufs that
78 * are not currently held but have been recently released. These dbufs
79 * are not eligible for arc eviction until they are aged out of the cache.
80 * Dbufs are added to the dbuf cache once the last hold is released. If a
81 * dbuf is later accessed and still exists in the dbuf cache, then it will
82 * be removed from the cache and later re-added to the head of the cache.
83 * Dbufs that are aged out of the cache will be immediately destroyed and
84 * become eligible for arc eviction.
86 static multilist_t *dbuf_cache;
87 static refcount_t dbuf_cache_size;
88 uint64_t dbuf_cache_max_bytes = 0;
90 /* Set the default size of the dbuf cache to log2 fraction of arc size. */
91 int dbuf_cache_shift = 5;
94 * The dbuf cache uses a three-stage eviction policy:
95 * - A low water marker designates when the dbuf eviction thread
96 * should stop evicting from the dbuf cache.
97 * - When we reach the maximum size (aka mid water mark), we
98 * signal the eviction thread to run.
99 * - The high water mark indicates when the eviction thread
100 * is unable to keep up with the incoming load and eviction must
101 * happen in the context of the calling thread.
105 * low water mid water hi water
106 * +----------------------------------------+----------+----------+
111 * +----------------------------------------+----------+----------+
113 * evicting eviction directly
116 * The high and low water marks indicate the operating range for the eviction
117 * thread. The low water mark is, by default, 90% of the total size of the
118 * cache and the high water mark is at 110% (both of these percentages can be
119 * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct,
120 * respectively). The eviction thread will try to ensure that the cache remains
121 * within this range by waking up every second and checking if the cache is
122 * above the low water mark. The thread can also be woken up by callers adding
123 * elements into the cache if the cache is larger than the mid water (i.e max
124 * cache size). Once the eviction thread is woken up and eviction is required,
125 * it will continue evicting buffers until it's able to reduce the cache size
126 * to the low water mark. If the cache size continues to grow and hits the high
127 * water mark, then callers adding elments to the cache will begin to evict
128 * directly from the cache until the cache is no longer above the high water
133 * The percentage above and below the maximum cache size.
135 uint_t dbuf_cache_hiwater_pct = 10;
136 uint_t dbuf_cache_lowater_pct = 10;
138 SYSCTL_DECL(_vfs_zfs);
139 SYSCTL_QUAD(_vfs_zfs, OID_AUTO, dbuf_cache_max_bytes, CTLFLAG_RWTUN,
140 &dbuf_cache_max_bytes, 0, "dbuf cache size in bytes");
141 SYSCTL_INT(_vfs_zfs, OID_AUTO, dbuf_cache_shift, CTLFLAG_RDTUN,
142 &dbuf_cache_shift, 0, "dbuf cache size as log2 fraction of ARC");
143 SYSCTL_UINT(_vfs_zfs, OID_AUTO, dbuf_cache_hiwater_pct, CTLFLAG_RWTUN,
144 &dbuf_cache_hiwater_pct, 0, "max percents above the dbuf cache size");
145 SYSCTL_UINT(_vfs_zfs, OID_AUTO, dbuf_cache_lowater_pct, CTLFLAG_RWTUN,
146 &dbuf_cache_lowater_pct, 0, "max percents below the dbuf cache size");
150 dbuf_cons(void *vdb, void *unused, int kmflag)
152 dmu_buf_impl_t *db = vdb;
153 bzero(db, sizeof (dmu_buf_impl_t));
155 mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL);
156 cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL);
157 multilist_link_init(&db->db_cache_link);
158 refcount_create(&db->db_holds);
165 dbuf_dest(void *vdb, void *unused)
167 dmu_buf_impl_t *db = vdb;
168 mutex_destroy(&db->db_mtx);
169 cv_destroy(&db->db_changed);
170 ASSERT(!multilist_link_active(&db->db_cache_link));
171 refcount_destroy(&db->db_holds);
175 * dbuf hash table routines
177 static dbuf_hash_table_t dbuf_hash_table;
179 static uint64_t dbuf_hash_count;
182 * We use Cityhash for this. It's fast, and has good hash properties without
183 * requiring any large static buffers.
186 dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid)
188 return (cityhash4((uintptr_t)os, obj, (uint64_t)lvl, blkid));
191 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
192 ((dbuf)->db.db_object == (obj) && \
193 (dbuf)->db_objset == (os) && \
194 (dbuf)->db_level == (level) && \
195 (dbuf)->db_blkid == (blkid))
198 dbuf_find(objset_t *os, uint64_t obj, uint8_t level, uint64_t blkid)
200 dbuf_hash_table_t *h = &dbuf_hash_table;
201 uint64_t hv = dbuf_hash(os, obj, level, blkid);
202 uint64_t idx = hv & h->hash_table_mask;
205 mutex_enter(DBUF_HASH_MUTEX(h, idx));
206 for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) {
207 if (DBUF_EQUAL(db, os, obj, level, blkid)) {
208 mutex_enter(&db->db_mtx);
209 if (db->db_state != DB_EVICTING) {
210 mutex_exit(DBUF_HASH_MUTEX(h, idx));
213 mutex_exit(&db->db_mtx);
216 mutex_exit(DBUF_HASH_MUTEX(h, idx));
220 static dmu_buf_impl_t *
221 dbuf_find_bonus(objset_t *os, uint64_t object)
224 dmu_buf_impl_t *db = NULL;
226 if (dnode_hold(os, object, FTAG, &dn) == 0) {
227 rw_enter(&dn->dn_struct_rwlock, RW_READER);
228 if (dn->dn_bonus != NULL) {
230 mutex_enter(&db->db_mtx);
232 rw_exit(&dn->dn_struct_rwlock);
233 dnode_rele(dn, FTAG);
239 * Insert an entry into the hash table. If there is already an element
240 * equal to elem in the hash table, then the already existing element
241 * will be returned and the new element will not be inserted.
242 * Otherwise returns NULL.
244 static dmu_buf_impl_t *
245 dbuf_hash_insert(dmu_buf_impl_t *db)
247 dbuf_hash_table_t *h = &dbuf_hash_table;
248 objset_t *os = db->db_objset;
249 uint64_t obj = db->db.db_object;
250 int level = db->db_level;
251 uint64_t blkid = db->db_blkid;
252 uint64_t hv = dbuf_hash(os, obj, level, blkid);
253 uint64_t idx = hv & h->hash_table_mask;
256 mutex_enter(DBUF_HASH_MUTEX(h, idx));
257 for (dbf = h->hash_table[idx]; dbf != NULL; dbf = dbf->db_hash_next) {
258 if (DBUF_EQUAL(dbf, os, obj, level, blkid)) {
259 mutex_enter(&dbf->db_mtx);
260 if (dbf->db_state != DB_EVICTING) {
261 mutex_exit(DBUF_HASH_MUTEX(h, idx));
264 mutex_exit(&dbf->db_mtx);
268 mutex_enter(&db->db_mtx);
269 db->db_hash_next = h->hash_table[idx];
270 h->hash_table[idx] = db;
271 mutex_exit(DBUF_HASH_MUTEX(h, idx));
272 atomic_inc_64(&dbuf_hash_count);
278 * Remove an entry from the hash table. It must be in the EVICTING state.
281 dbuf_hash_remove(dmu_buf_impl_t *db)
283 dbuf_hash_table_t *h = &dbuf_hash_table;
284 uint64_t hv = dbuf_hash(db->db_objset, db->db.db_object,
285 db->db_level, db->db_blkid);
286 uint64_t idx = hv & h->hash_table_mask;
287 dmu_buf_impl_t *dbf, **dbp;
290 * We musn't hold db_mtx to maintain lock ordering:
291 * DBUF_HASH_MUTEX > db_mtx.
293 ASSERT(refcount_is_zero(&db->db_holds));
294 ASSERT(db->db_state == DB_EVICTING);
295 ASSERT(!MUTEX_HELD(&db->db_mtx));
297 mutex_enter(DBUF_HASH_MUTEX(h, idx));
298 dbp = &h->hash_table[idx];
299 while ((dbf = *dbp) != db) {
300 dbp = &dbf->db_hash_next;
303 *dbp = db->db_hash_next;
304 db->db_hash_next = NULL;
305 mutex_exit(DBUF_HASH_MUTEX(h, idx));
306 atomic_dec_64(&dbuf_hash_count);
312 } dbvu_verify_type_t;
315 dbuf_verify_user(dmu_buf_impl_t *db, dbvu_verify_type_t verify_type)
320 if (db->db_user == NULL)
323 /* Only data blocks support the attachment of user data. */
324 ASSERT(db->db_level == 0);
326 /* Clients must resolve a dbuf before attaching user data. */
327 ASSERT(db->db.db_data != NULL);
328 ASSERT3U(db->db_state, ==, DB_CACHED);
330 holds = refcount_count(&db->db_holds);
331 if (verify_type == DBVU_EVICTING) {
333 * Immediate eviction occurs when holds == dirtycnt.
334 * For normal eviction buffers, holds is zero on
335 * eviction, except when dbuf_fix_old_data() calls
336 * dbuf_clear_data(). However, the hold count can grow
337 * during eviction even though db_mtx is held (see
338 * dmu_bonus_hold() for an example), so we can only
339 * test the generic invariant that holds >= dirtycnt.
341 ASSERT3U(holds, >=, db->db_dirtycnt);
343 if (db->db_user_immediate_evict == TRUE)
344 ASSERT3U(holds, >=, db->db_dirtycnt);
346 ASSERT3U(holds, >, 0);
352 dbuf_evict_user(dmu_buf_impl_t *db)
354 dmu_buf_user_t *dbu = db->db_user;
356 ASSERT(MUTEX_HELD(&db->db_mtx));
361 dbuf_verify_user(db, DBVU_EVICTING);
365 if (dbu->dbu_clear_on_evict_dbufp != NULL)
366 *dbu->dbu_clear_on_evict_dbufp = NULL;
370 * There are two eviction callbacks - one that we call synchronously
371 * and one that we invoke via a taskq. The async one is useful for
372 * avoiding lock order reversals and limiting stack depth.
374 * Note that if we have a sync callback but no async callback,
375 * it's likely that the sync callback will free the structure
376 * containing the dbu. In that case we need to take care to not
377 * dereference dbu after calling the sync evict func.
379 boolean_t has_async = (dbu->dbu_evict_func_async != NULL);
381 if (dbu->dbu_evict_func_sync != NULL)
382 dbu->dbu_evict_func_sync(dbu);
385 taskq_dispatch_ent(dbu_evict_taskq, dbu->dbu_evict_func_async,
386 dbu, 0, &dbu->dbu_tqent);
391 dbuf_is_metadata(dmu_buf_impl_t *db)
393 if (db->db_level > 0) {
396 boolean_t is_metadata;
399 is_metadata = DMU_OT_IS_METADATA(DB_DNODE(db)->dn_type);
402 return (is_metadata);
407 * This function *must* return indices evenly distributed between all
408 * sublists of the multilist. This is needed due to how the dbuf eviction
409 * code is laid out; dbuf_evict_thread() assumes dbufs are evenly
410 * distributed between all sublists and uses this assumption when
411 * deciding which sublist to evict from and how much to evict from it.
414 dbuf_cache_multilist_index_func(multilist_t *ml, void *obj)
416 dmu_buf_impl_t *db = obj;
419 * The assumption here, is the hash value for a given
420 * dmu_buf_impl_t will remain constant throughout it's lifetime
421 * (i.e. it's objset, object, level and blkid fields don't change).
422 * Thus, we don't need to store the dbuf's sublist index
423 * on insertion, as this index can be recalculated on removal.
425 * Also, the low order bits of the hash value are thought to be
426 * distributed evenly. Otherwise, in the case that the multilist
427 * has a power of two number of sublists, each sublists' usage
428 * would not be evenly distributed.
430 return (dbuf_hash(db->db_objset, db->db.db_object,
431 db->db_level, db->db_blkid) %
432 multilist_get_num_sublists(ml));
435 static inline boolean_t
436 dbuf_cache_above_hiwater(void)
438 uint64_t dbuf_cache_hiwater_bytes =
439 (dbuf_cache_max_bytes * dbuf_cache_hiwater_pct) / 100;
441 return (refcount_count(&dbuf_cache_size) >
442 dbuf_cache_max_bytes + dbuf_cache_hiwater_bytes);
445 static inline boolean_t
446 dbuf_cache_above_lowater(void)
448 uint64_t dbuf_cache_lowater_bytes =
449 (dbuf_cache_max_bytes * dbuf_cache_lowater_pct) / 100;
451 return (refcount_count(&dbuf_cache_size) >
452 dbuf_cache_max_bytes - dbuf_cache_lowater_bytes);
456 * Evict the oldest eligible dbuf from the dbuf cache.
461 int idx = multilist_get_random_index(dbuf_cache);
462 multilist_sublist_t *mls = multilist_sublist_lock(dbuf_cache, idx);
464 ASSERT(!MUTEX_HELD(&dbuf_evict_lock));
467 * Set the thread's tsd to indicate that it's processing evictions.
468 * Once a thread stops evicting from the dbuf cache it will
469 * reset its tsd to NULL.
471 ASSERT3P(tsd_get(zfs_dbuf_evict_key), ==, NULL);
472 (void) tsd_set(zfs_dbuf_evict_key, (void *)B_TRUE);
474 dmu_buf_impl_t *db = multilist_sublist_tail(mls);
475 while (db != NULL && mutex_tryenter(&db->db_mtx) == 0) {
476 db = multilist_sublist_prev(mls, db);
479 DTRACE_PROBE2(dbuf__evict__one, dmu_buf_impl_t *, db,
480 multilist_sublist_t *, mls);
483 multilist_sublist_remove(mls, db);
484 multilist_sublist_unlock(mls);
485 (void) refcount_remove_many(&dbuf_cache_size,
489 multilist_sublist_unlock(mls);
491 (void) tsd_set(zfs_dbuf_evict_key, NULL);
495 * The dbuf evict thread is responsible for aging out dbufs from the
496 * cache. Once the cache has reached it's maximum size, dbufs are removed
497 * and destroyed. The eviction thread will continue running until the size
498 * of the dbuf cache is at or below the maximum size. Once the dbuf is aged
499 * out of the cache it is destroyed and becomes eligible for arc eviction.
503 dbuf_evict_thread(void *unused __unused)
507 CALLB_CPR_INIT(&cpr, &dbuf_evict_lock, callb_generic_cpr, FTAG);
509 mutex_enter(&dbuf_evict_lock);
510 while (!dbuf_evict_thread_exit) {
511 while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit) {
512 CALLB_CPR_SAFE_BEGIN(&cpr);
513 (void) cv_timedwait_hires(&dbuf_evict_cv,
514 &dbuf_evict_lock, SEC2NSEC(1), MSEC2NSEC(1), 0);
515 CALLB_CPR_SAFE_END(&cpr, &dbuf_evict_lock);
517 mutex_exit(&dbuf_evict_lock);
520 * Keep evicting as long as we're above the low water mark
521 * for the cache. We do this without holding the locks to
522 * minimize lock contention.
524 while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit) {
528 mutex_enter(&dbuf_evict_lock);
531 dbuf_evict_thread_exit = B_FALSE;
532 cv_broadcast(&dbuf_evict_cv);
533 CALLB_CPR_EXIT(&cpr); /* drops dbuf_evict_lock */
538 * Wake up the dbuf eviction thread if the dbuf cache is at its max size.
539 * If the dbuf cache is at its high water mark, then evict a dbuf from the
540 * dbuf cache using the callers context.
543 dbuf_evict_notify(void)
547 * We use thread specific data to track when a thread has
548 * started processing evictions. This allows us to avoid deeply
549 * nested stacks that would have a call flow similar to this:
551 * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify()
554 * +-----dbuf_destroy()<--dbuf_evict_one()<--------+
556 * The dbuf_eviction_thread will always have its tsd set until
557 * that thread exits. All other threads will only set their tsd
558 * if they are participating in the eviction process. This only
559 * happens if the eviction thread is unable to process evictions
560 * fast enough. To keep the dbuf cache size in check, other threads
561 * can evict from the dbuf cache directly. Those threads will set
562 * their tsd values so that we ensure that they only evict one dbuf
563 * from the dbuf cache.
565 if (tsd_get(zfs_dbuf_evict_key) != NULL)
569 * We check if we should evict without holding the dbuf_evict_lock,
570 * because it's OK to occasionally make the wrong decision here,
571 * and grabbing the lock results in massive lock contention.
573 if (refcount_count(&dbuf_cache_size) > dbuf_cache_max_bytes) {
574 if (dbuf_cache_above_hiwater())
576 cv_signal(&dbuf_evict_cv);
583 uint64_t hsize = 1ULL << 16;
584 dbuf_hash_table_t *h = &dbuf_hash_table;
588 * The hash table is big enough to fill all of physical memory
589 * with an average 4K block size. The table will take up
590 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
592 while (hsize * 4096 < (uint64_t)physmem * PAGESIZE)
596 h->hash_table_mask = hsize - 1;
597 h->hash_table = kmem_zalloc(hsize * sizeof (void *), KM_NOSLEEP);
598 if (h->hash_table == NULL) {
599 /* XXX - we should really return an error instead of assert */
600 ASSERT(hsize > (1ULL << 10));
605 dbuf_kmem_cache = kmem_cache_create("dmu_buf_impl_t",
606 sizeof (dmu_buf_impl_t),
607 0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0);
609 for (i = 0; i < DBUF_MUTEXES; i++)
610 mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL);
613 * Setup the parameters for the dbuf cache. We set the size of the
614 * dbuf cache to 1/32nd (default) of the size of the ARC. If the value
615 * has been set in /etc/system and it's not greater than the size of
616 * the ARC, then we honor that value.
618 if (dbuf_cache_max_bytes == 0 ||
619 dbuf_cache_max_bytes >= arc_max_bytes()) {
620 dbuf_cache_max_bytes = arc_max_bytes() >> dbuf_cache_shift;
624 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
625 * configuration is not required.
627 dbu_evict_taskq = taskq_create("dbu_evict", 1, minclsyspri, 0, 0, 0);
629 dbuf_cache = multilist_create(sizeof (dmu_buf_impl_t),
630 offsetof(dmu_buf_impl_t, db_cache_link),
631 dbuf_cache_multilist_index_func);
632 refcount_create(&dbuf_cache_size);
634 tsd_create(&zfs_dbuf_evict_key, NULL);
635 dbuf_evict_thread_exit = B_FALSE;
636 mutex_init(&dbuf_evict_lock, NULL, MUTEX_DEFAULT, NULL);
637 cv_init(&dbuf_evict_cv, NULL, CV_DEFAULT, NULL);
638 dbuf_cache_evict_thread = thread_create(NULL, 0, dbuf_evict_thread,
639 NULL, 0, &p0, TS_RUN, minclsyspri);
645 dbuf_hash_table_t *h = &dbuf_hash_table;
648 for (i = 0; i < DBUF_MUTEXES; i++)
649 mutex_destroy(&h->hash_mutexes[i]);
650 kmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
651 kmem_cache_destroy(dbuf_kmem_cache);
652 taskq_destroy(dbu_evict_taskq);
654 mutex_enter(&dbuf_evict_lock);
655 dbuf_evict_thread_exit = B_TRUE;
656 while (dbuf_evict_thread_exit) {
657 cv_signal(&dbuf_evict_cv);
658 cv_wait(&dbuf_evict_cv, &dbuf_evict_lock);
660 mutex_exit(&dbuf_evict_lock);
661 tsd_destroy(&zfs_dbuf_evict_key);
663 mutex_destroy(&dbuf_evict_lock);
664 cv_destroy(&dbuf_evict_cv);
666 refcount_destroy(&dbuf_cache_size);
667 multilist_destroy(dbuf_cache);
676 dbuf_verify(dmu_buf_impl_t *db)
679 dbuf_dirty_record_t *dr;
681 ASSERT(MUTEX_HELD(&db->db_mtx));
683 if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY))
686 ASSERT(db->db_objset != NULL);
690 ASSERT(db->db_parent == NULL);
691 ASSERT(db->db_blkptr == NULL);
693 ASSERT3U(db->db.db_object, ==, dn->dn_object);
694 ASSERT3P(db->db_objset, ==, dn->dn_objset);
695 ASSERT3U(db->db_level, <, dn->dn_nlevels);
696 ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
697 db->db_blkid == DMU_SPILL_BLKID ||
698 !avl_is_empty(&dn->dn_dbufs));
700 if (db->db_blkid == DMU_BONUS_BLKID) {
702 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
703 ASSERT3U(db->db.db_offset, ==, DMU_BONUS_BLKID);
704 } else if (db->db_blkid == DMU_SPILL_BLKID) {
706 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
707 ASSERT0(db->db.db_offset);
709 ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size);
712 for (dr = db->db_data_pending; dr != NULL; dr = dr->dr_next)
713 ASSERT(dr->dr_dbuf == db);
715 for (dr = db->db_last_dirty; dr != NULL; dr = dr->dr_next)
716 ASSERT(dr->dr_dbuf == db);
719 * We can't assert that db_size matches dn_datablksz because it
720 * can be momentarily different when another thread is doing
723 if (db->db_level == 0 && db->db.db_object == DMU_META_DNODE_OBJECT) {
724 dr = db->db_data_pending;
726 * It should only be modified in syncing context, so
727 * make sure we only have one copy of the data.
729 ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf);
732 /* verify db->db_blkptr */
734 if (db->db_parent == dn->dn_dbuf) {
735 /* db is pointed to by the dnode */
736 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
737 if (DMU_OBJECT_IS_SPECIAL(db->db.db_object))
738 ASSERT(db->db_parent == NULL);
740 ASSERT(db->db_parent != NULL);
741 if (db->db_blkid != DMU_SPILL_BLKID)
742 ASSERT3P(db->db_blkptr, ==,
743 &dn->dn_phys->dn_blkptr[db->db_blkid]);
745 /* db is pointed to by an indirect block */
746 int epb = db->db_parent->db.db_size >> SPA_BLKPTRSHIFT;
747 ASSERT3U(db->db_parent->db_level, ==, db->db_level+1);
748 ASSERT3U(db->db_parent->db.db_object, ==,
751 * dnode_grow_indblksz() can make this fail if we don't
752 * have the struct_rwlock. XXX indblksz no longer
753 * grows. safe to do this now?
755 if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
756 ASSERT3P(db->db_blkptr, ==,
757 ((blkptr_t *)db->db_parent->db.db_data +
758 db->db_blkid % epb));
762 if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) &&
763 (db->db_buf == NULL || db->db_buf->b_data) &&
764 db->db.db_data && db->db_blkid != DMU_BONUS_BLKID &&
765 db->db_state != DB_FILL && !dn->dn_free_txg) {
767 * If the blkptr isn't set but they have nonzero data,
768 * it had better be dirty, otherwise we'll lose that
769 * data when we evict this buffer.
771 * There is an exception to this rule for indirect blocks; in
772 * this case, if the indirect block is a hole, we fill in a few
773 * fields on each of the child blocks (importantly, birth time)
774 * to prevent hole birth times from being lost when you
775 * partially fill in a hole.
777 if (db->db_dirtycnt == 0) {
778 if (db->db_level == 0) {
779 uint64_t *buf = db->db.db_data;
782 for (i = 0; i < db->db.db_size >> 3; i++) {
786 blkptr_t *bps = db->db.db_data;
787 ASSERT3U(1 << DB_DNODE(db)->dn_indblkshift, ==,
790 * We want to verify that all the blkptrs in the
791 * indirect block are holes, but we may have
792 * automatically set up a few fields for them.
793 * We iterate through each blkptr and verify
794 * they only have those fields set.
797 i < db->db.db_size / sizeof (blkptr_t);
799 blkptr_t *bp = &bps[i];
800 ASSERT(ZIO_CHECKSUM_IS_ZERO(
803 DVA_IS_EMPTY(&bp->blk_dva[0]) &&
804 DVA_IS_EMPTY(&bp->blk_dva[1]) &&
805 DVA_IS_EMPTY(&bp->blk_dva[2]));
806 ASSERT0(bp->blk_fill);
807 ASSERT0(bp->blk_pad[0]);
808 ASSERT0(bp->blk_pad[1]);
809 ASSERT(!BP_IS_EMBEDDED(bp));
810 ASSERT(BP_IS_HOLE(bp));
811 ASSERT0(bp->blk_phys_birth);
821 dbuf_clear_data(dmu_buf_impl_t *db)
823 ASSERT(MUTEX_HELD(&db->db_mtx));
825 ASSERT3P(db->db_buf, ==, NULL);
826 db->db.db_data = NULL;
827 if (db->db_state != DB_NOFILL)
828 db->db_state = DB_UNCACHED;
832 dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf)
834 ASSERT(MUTEX_HELD(&db->db_mtx));
838 ASSERT(buf->b_data != NULL);
839 db->db.db_data = buf->b_data;
843 * Loan out an arc_buf for read. Return the loaned arc_buf.
846 dbuf_loan_arcbuf(dmu_buf_impl_t *db)
850 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
851 mutex_enter(&db->db_mtx);
852 if (arc_released(db->db_buf) || refcount_count(&db->db_holds) > 1) {
853 int blksz = db->db.db_size;
854 spa_t *spa = db->db_objset->os_spa;
856 mutex_exit(&db->db_mtx);
857 abuf = arc_loan_buf(spa, B_FALSE, blksz);
858 bcopy(db->db.db_data, abuf->b_data, blksz);
861 arc_loan_inuse_buf(abuf, db);
864 mutex_exit(&db->db_mtx);
870 * Calculate which level n block references the data at the level 0 offset
874 dbuf_whichblock(dnode_t *dn, int64_t level, uint64_t offset)
876 if (dn->dn_datablkshift != 0 && dn->dn_indblkshift != 0) {
878 * The level n blkid is equal to the level 0 blkid divided by
879 * the number of level 0s in a level n block.
881 * The level 0 blkid is offset >> datablkshift =
882 * offset / 2^datablkshift.
884 * The number of level 0s in a level n is the number of block
885 * pointers in an indirect block, raised to the power of level.
886 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
887 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
889 * Thus, the level n blkid is: offset /
890 * ((2^datablkshift)*(2^(level*(indblkshift - SPA_BLKPTRSHIFT)))
891 * = offset / 2^(datablkshift + level *
892 * (indblkshift - SPA_BLKPTRSHIFT))
893 * = offset >> (datablkshift + level *
894 * (indblkshift - SPA_BLKPTRSHIFT))
896 return (offset >> (dn->dn_datablkshift + level *
897 (dn->dn_indblkshift - SPA_BLKPTRSHIFT)));
899 ASSERT3U(offset, <, dn->dn_datablksz);
905 dbuf_read_done(zio_t *zio, const zbookmark_phys_t *zb, const blkptr_t *bp,
906 arc_buf_t *buf, void *vdb)
908 dmu_buf_impl_t *db = vdb;
910 mutex_enter(&db->db_mtx);
911 ASSERT3U(db->db_state, ==, DB_READ);
913 * All reads are synchronous, so we must have a hold on the dbuf
915 ASSERT(refcount_count(&db->db_holds) > 0);
916 ASSERT(db->db_buf == NULL);
917 ASSERT(db->db.db_data == NULL);
918 if (db->db_level == 0 && db->db_freed_in_flight) {
919 /* we were freed in flight; disregard any error */
921 buf = arc_alloc_buf(db->db_objset->os_spa,
922 db, DBUF_GET_BUFC_TYPE(db), db->db.db_size);
924 arc_release(buf, db);
925 bzero(buf->b_data, db->db.db_size);
927 db->db_freed_in_flight = FALSE;
928 dbuf_set_data(db, buf);
929 db->db_state = DB_CACHED;
930 } else if (buf != NULL) {
931 dbuf_set_data(db, buf);
932 db->db_state = DB_CACHED;
934 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
935 ASSERT3P(db->db_buf, ==, NULL);
936 db->db_state = DB_UNCACHED;
938 cv_broadcast(&db->db_changed);
939 dbuf_rele_and_unlock(db, NULL);
943 dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
947 arc_flags_t aflags = ARC_FLAG_NOWAIT;
951 ASSERT(!refcount_is_zero(&db->db_holds));
952 /* We need the struct_rwlock to prevent db_blkptr from changing. */
953 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
954 ASSERT(MUTEX_HELD(&db->db_mtx));
955 ASSERT(db->db_state == DB_UNCACHED);
956 ASSERT(db->db_buf == NULL);
958 if (db->db_blkid == DMU_BONUS_BLKID) {
959 int bonuslen = MIN(dn->dn_bonuslen, dn->dn_phys->dn_bonuslen);
961 ASSERT3U(bonuslen, <=, db->db.db_size);
962 db->db.db_data = zio_buf_alloc(DN_MAX_BONUSLEN);
963 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
964 if (bonuslen < DN_MAX_BONUSLEN)
965 bzero(db->db.db_data, DN_MAX_BONUSLEN);
967 bcopy(DN_BONUS(dn->dn_phys), db->db.db_data, bonuslen);
969 db->db_state = DB_CACHED;
970 mutex_exit(&db->db_mtx);
975 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
976 * processes the delete record and clears the bp while we are waiting
977 * for the dn_mtx (resulting in a "no" from block_freed).
979 if (db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr) ||
980 (db->db_level == 0 && (dnode_block_freed(dn, db->db_blkid) ||
981 BP_IS_HOLE(db->db_blkptr)))) {
982 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
984 dbuf_set_data(db, arc_alloc_buf(db->db_objset->os_spa, db, type,
986 bzero(db->db.db_data, db->db.db_size);
988 if (db->db_blkptr != NULL && db->db_level > 0 &&
989 BP_IS_HOLE(db->db_blkptr) &&
990 db->db_blkptr->blk_birth != 0) {
991 blkptr_t *bps = db->db.db_data;
992 for (int i = 0; i < ((1 <<
993 DB_DNODE(db)->dn_indblkshift) / sizeof (blkptr_t));
995 blkptr_t *bp = &bps[i];
996 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
997 1 << dn->dn_indblkshift);
999 BP_GET_LEVEL(db->db_blkptr) == 1 ?
1001 BP_GET_LSIZE(db->db_blkptr));
1002 BP_SET_TYPE(bp, BP_GET_TYPE(db->db_blkptr));
1004 BP_GET_LEVEL(db->db_blkptr) - 1);
1005 BP_SET_BIRTH(bp, db->db_blkptr->blk_birth, 0);
1009 db->db_state = DB_CACHED;
1010 mutex_exit(&db->db_mtx);
1016 db->db_state = DB_READ;
1017 mutex_exit(&db->db_mtx);
1019 if (DBUF_IS_L2CACHEABLE(db))
1020 aflags |= ARC_FLAG_L2CACHE;
1022 SET_BOOKMARK(&zb, db->db_objset->os_dsl_dataset ?
1023 db->db_objset->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1024 db->db.db_object, db->db_level, db->db_blkid);
1026 dbuf_add_ref(db, NULL);
1028 (void) arc_read(zio, db->db_objset->os_spa, db->db_blkptr,
1029 dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ,
1030 (flags & DB_RF_CANFAIL) ? ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED,
1035 * This is our just-in-time copy function. It makes a copy of buffers that
1036 * have been modified in a previous transaction group before we access them in
1037 * the current active group.
1039 * This function is used in three places: when we are dirtying a buffer for the
1040 * first time in a txg, when we are freeing a range in a dnode that includes
1041 * this buffer, and when we are accessing a buffer which was received compressed
1042 * and later referenced in a WRITE_BYREF record.
1044 * Note that when we are called from dbuf_free_range() we do not put a hold on
1045 * the buffer, we just traverse the active dbuf list for the dnode.
1048 dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg)
1050 dbuf_dirty_record_t *dr = db->db_last_dirty;
1052 ASSERT(MUTEX_HELD(&db->db_mtx));
1053 ASSERT(db->db.db_data != NULL);
1054 ASSERT(db->db_level == 0);
1055 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT);
1058 (dr->dt.dl.dr_data !=
1059 ((db->db_blkid == DMU_BONUS_BLKID) ? db->db.db_data : db->db_buf)))
1063 * If the last dirty record for this dbuf has not yet synced
1064 * and its referencing the dbuf data, either:
1065 * reset the reference to point to a new copy,
1066 * or (if there a no active holders)
1067 * just null out the current db_data pointer.
1069 ASSERT(dr->dr_txg >= txg - 2);
1070 if (db->db_blkid == DMU_BONUS_BLKID) {
1071 /* Note that the data bufs here are zio_bufs */
1072 dr->dt.dl.dr_data = zio_buf_alloc(DN_MAX_BONUSLEN);
1073 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
1074 bcopy(db->db.db_data, dr->dt.dl.dr_data, DN_MAX_BONUSLEN);
1075 } else if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
1076 int size = arc_buf_size(db->db_buf);
1077 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1078 spa_t *spa = db->db_objset->os_spa;
1079 enum zio_compress compress_type =
1080 arc_get_compression(db->db_buf);
1082 if (compress_type == ZIO_COMPRESS_OFF) {
1083 dr->dt.dl.dr_data = arc_alloc_buf(spa, db, type, size);
1085 ASSERT3U(type, ==, ARC_BUFC_DATA);
1086 dr->dt.dl.dr_data = arc_alloc_compressed_buf(spa, db,
1087 size, arc_buf_lsize(db->db_buf), compress_type);
1089 bcopy(db->db.db_data, dr->dt.dl.dr_data->b_data, size);
1092 dbuf_clear_data(db);
1097 dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
1104 * We don't have to hold the mutex to check db_state because it
1105 * can't be freed while we have a hold on the buffer.
1107 ASSERT(!refcount_is_zero(&db->db_holds));
1109 if (db->db_state == DB_NOFILL)
1110 return (SET_ERROR(EIO));
1114 if ((flags & DB_RF_HAVESTRUCT) == 0)
1115 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1117 prefetch = db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1118 (flags & DB_RF_NOPREFETCH) == 0 && dn != NULL &&
1119 DBUF_IS_CACHEABLE(db);
1121 mutex_enter(&db->db_mtx);
1122 if (db->db_state == DB_CACHED) {
1124 * If the arc buf is compressed, we need to decompress it to
1125 * read the data. This could happen during the "zfs receive" of
1126 * a stream which is compressed and deduplicated.
1128 if (db->db_buf != NULL &&
1129 arc_get_compression(db->db_buf) != ZIO_COMPRESS_OFF) {
1130 dbuf_fix_old_data(db,
1131 spa_syncing_txg(dmu_objset_spa(db->db_objset)));
1132 err = arc_decompress(db->db_buf);
1133 dbuf_set_data(db, db->db_buf);
1135 mutex_exit(&db->db_mtx);
1137 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE);
1138 if ((flags & DB_RF_HAVESTRUCT) == 0)
1139 rw_exit(&dn->dn_struct_rwlock);
1141 } else if (db->db_state == DB_UNCACHED) {
1142 spa_t *spa = dn->dn_objset->os_spa;
1143 boolean_t need_wait = B_FALSE;
1146 db->db_blkptr != NULL && !BP_IS_HOLE(db->db_blkptr)) {
1147 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
1150 dbuf_read_impl(db, zio, flags);
1152 /* dbuf_read_impl has dropped db_mtx for us */
1155 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE);
1157 if ((flags & DB_RF_HAVESTRUCT) == 0)
1158 rw_exit(&dn->dn_struct_rwlock);
1162 err = zio_wait(zio);
1165 * Another reader came in while the dbuf was in flight
1166 * between UNCACHED and CACHED. Either a writer will finish
1167 * writing the buffer (sending the dbuf to CACHED) or the
1168 * first reader's request will reach the read_done callback
1169 * and send the dbuf to CACHED. Otherwise, a failure
1170 * occurred and the dbuf went to UNCACHED.
1172 mutex_exit(&db->db_mtx);
1174 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE);
1175 if ((flags & DB_RF_HAVESTRUCT) == 0)
1176 rw_exit(&dn->dn_struct_rwlock);
1179 /* Skip the wait per the caller's request. */
1180 mutex_enter(&db->db_mtx);
1181 if ((flags & DB_RF_NEVERWAIT) == 0) {
1182 while (db->db_state == DB_READ ||
1183 db->db_state == DB_FILL) {
1184 ASSERT(db->db_state == DB_READ ||
1185 (flags & DB_RF_HAVESTRUCT) == 0);
1186 DTRACE_PROBE2(blocked__read, dmu_buf_impl_t *,
1188 cv_wait(&db->db_changed, &db->db_mtx);
1190 if (db->db_state == DB_UNCACHED)
1191 err = SET_ERROR(EIO);
1193 mutex_exit(&db->db_mtx);
1200 dbuf_noread(dmu_buf_impl_t *db)
1202 ASSERT(!refcount_is_zero(&db->db_holds));
1203 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1204 mutex_enter(&db->db_mtx);
1205 while (db->db_state == DB_READ || db->db_state == DB_FILL)
1206 cv_wait(&db->db_changed, &db->db_mtx);
1207 if (db->db_state == DB_UNCACHED) {
1208 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1209 spa_t *spa = db->db_objset->os_spa;
1211 ASSERT(db->db_buf == NULL);
1212 ASSERT(db->db.db_data == NULL);
1213 dbuf_set_data(db, arc_alloc_buf(spa, db, type, db->db.db_size));
1214 db->db_state = DB_FILL;
1215 } else if (db->db_state == DB_NOFILL) {
1216 dbuf_clear_data(db);
1218 ASSERT3U(db->db_state, ==, DB_CACHED);
1220 mutex_exit(&db->db_mtx);
1224 dbuf_unoverride(dbuf_dirty_record_t *dr)
1226 dmu_buf_impl_t *db = dr->dr_dbuf;
1227 blkptr_t *bp = &dr->dt.dl.dr_overridden_by;
1228 uint64_t txg = dr->dr_txg;
1230 ASSERT(MUTEX_HELD(&db->db_mtx));
1232 * This assert is valid because dmu_sync() expects to be called by
1233 * a zilog's get_data while holding a range lock. This call only
1234 * comes from dbuf_dirty() callers who must also hold a range lock.
1236 ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC);
1237 ASSERT(db->db_level == 0);
1239 if (db->db_blkid == DMU_BONUS_BLKID ||
1240 dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN)
1243 ASSERT(db->db_data_pending != dr);
1245 /* free this block */
1246 if (!BP_IS_HOLE(bp) && !dr->dt.dl.dr_nopwrite)
1247 zio_free(db->db_objset->os_spa, txg, bp);
1249 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
1250 dr->dt.dl.dr_nopwrite = B_FALSE;
1253 * Release the already-written buffer, so we leave it in
1254 * a consistent dirty state. Note that all callers are
1255 * modifying the buffer, so they will immediately do
1256 * another (redundant) arc_release(). Therefore, leave
1257 * the buf thawed to save the effort of freezing &
1258 * immediately re-thawing it.
1260 arc_release(dr->dt.dl.dr_data, db);
1264 * Evict (if its unreferenced) or clear (if its referenced) any level-0
1265 * data blocks in the free range, so that any future readers will find
1269 dbuf_free_range(dnode_t *dn, uint64_t start_blkid, uint64_t end_blkid,
1272 dmu_buf_impl_t db_search;
1273 dmu_buf_impl_t *db, *db_next;
1274 uint64_t txg = tx->tx_txg;
1277 if (end_blkid > dn->dn_maxblkid &&
1278 !(start_blkid == DMU_SPILL_BLKID || end_blkid == DMU_SPILL_BLKID))
1279 end_blkid = dn->dn_maxblkid;
1280 dprintf_dnode(dn, "start=%llu end=%llu\n", start_blkid, end_blkid);
1282 db_search.db_level = 0;
1283 db_search.db_blkid = start_blkid;
1284 db_search.db_state = DB_SEARCH;
1286 mutex_enter(&dn->dn_dbufs_mtx);
1287 db = avl_find(&dn->dn_dbufs, &db_search, &where);
1288 ASSERT3P(db, ==, NULL);
1290 db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER);
1292 for (; db != NULL; db = db_next) {
1293 db_next = AVL_NEXT(&dn->dn_dbufs, db);
1294 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1296 if (db->db_level != 0 || db->db_blkid > end_blkid) {
1299 ASSERT3U(db->db_blkid, >=, start_blkid);
1301 /* found a level 0 buffer in the range */
1302 mutex_enter(&db->db_mtx);
1303 if (dbuf_undirty(db, tx)) {
1304 /* mutex has been dropped and dbuf destroyed */
1308 if (db->db_state == DB_UNCACHED ||
1309 db->db_state == DB_NOFILL ||
1310 db->db_state == DB_EVICTING) {
1311 ASSERT(db->db.db_data == NULL);
1312 mutex_exit(&db->db_mtx);
1315 if (db->db_state == DB_READ || db->db_state == DB_FILL) {
1316 /* will be handled in dbuf_read_done or dbuf_rele */
1317 db->db_freed_in_flight = TRUE;
1318 mutex_exit(&db->db_mtx);
1321 if (refcount_count(&db->db_holds) == 0) {
1326 /* The dbuf is referenced */
1328 if (db->db_last_dirty != NULL) {
1329 dbuf_dirty_record_t *dr = db->db_last_dirty;
1331 if (dr->dr_txg == txg) {
1333 * This buffer is "in-use", re-adjust the file
1334 * size to reflect that this buffer may
1335 * contain new data when we sync.
1337 if (db->db_blkid != DMU_SPILL_BLKID &&
1338 db->db_blkid > dn->dn_maxblkid)
1339 dn->dn_maxblkid = db->db_blkid;
1340 dbuf_unoverride(dr);
1343 * This dbuf is not dirty in the open context.
1344 * Either uncache it (if its not referenced in
1345 * the open context) or reset its contents to
1348 dbuf_fix_old_data(db, txg);
1351 /* clear the contents if its cached */
1352 if (db->db_state == DB_CACHED) {
1353 ASSERT(db->db.db_data != NULL);
1354 arc_release(db->db_buf, db);
1355 bzero(db->db.db_data, db->db.db_size);
1356 arc_buf_freeze(db->db_buf);
1359 mutex_exit(&db->db_mtx);
1361 mutex_exit(&dn->dn_dbufs_mtx);
1365 dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx)
1367 arc_buf_t *buf, *obuf;
1368 int osize = db->db.db_size;
1369 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1372 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1377 /* XXX does *this* func really need the lock? */
1378 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
1381 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
1382 * is OK, because there can be no other references to the db
1383 * when we are changing its size, so no concurrent DB_FILL can
1387 * XXX we should be doing a dbuf_read, checking the return
1388 * value and returning that up to our callers
1390 dmu_buf_will_dirty(&db->db, tx);
1392 /* create the data buffer for the new block */
1393 buf = arc_alloc_buf(dn->dn_objset->os_spa, db, type, size);
1395 /* copy old block data to the new block */
1397 bcopy(obuf->b_data, buf->b_data, MIN(osize, size));
1398 /* zero the remainder */
1400 bzero((uint8_t *)buf->b_data + osize, size - osize);
1402 mutex_enter(&db->db_mtx);
1403 dbuf_set_data(db, buf);
1404 arc_buf_destroy(obuf, db);
1405 db->db.db_size = size;
1407 if (db->db_level == 0) {
1408 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
1409 db->db_last_dirty->dt.dl.dr_data = buf;
1411 mutex_exit(&db->db_mtx);
1413 dmu_objset_willuse_space(dn->dn_objset, size - osize, tx);
1418 dbuf_release_bp(dmu_buf_impl_t *db)
1420 objset_t *os = db->db_objset;
1422 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
1423 ASSERT(arc_released(os->os_phys_buf) ||
1424 list_link_active(&os->os_dsl_dataset->ds_synced_link));
1425 ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf));
1427 (void) arc_release(db->db_buf, db);
1431 * We already have a dirty record for this TXG, and we are being
1435 dbuf_redirty(dbuf_dirty_record_t *dr)
1437 dmu_buf_impl_t *db = dr->dr_dbuf;
1439 ASSERT(MUTEX_HELD(&db->db_mtx));
1441 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID) {
1443 * If this buffer has already been written out,
1444 * we now need to reset its state.
1446 dbuf_unoverride(dr);
1447 if (db->db.db_object != DMU_META_DNODE_OBJECT &&
1448 db->db_state != DB_NOFILL) {
1449 /* Already released on initial dirty, so just thaw. */
1450 ASSERT(arc_released(db->db_buf));
1451 arc_buf_thaw(db->db_buf);
1456 dbuf_dirty_record_t *
1457 dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1461 dbuf_dirty_record_t **drp, *dr;
1462 int drop_struct_lock = FALSE;
1463 int txgoff = tx->tx_txg & TXG_MASK;
1465 ASSERT(tx->tx_txg != 0);
1466 ASSERT(!refcount_is_zero(&db->db_holds));
1467 DMU_TX_DIRTY_BUF(tx, db);
1472 * Shouldn't dirty a regular buffer in syncing context. Private
1473 * objects may be dirtied in syncing context, but only if they
1474 * were already pre-dirtied in open context.
1477 if (dn->dn_objset->os_dsl_dataset != NULL) {
1478 rrw_enter(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock,
1481 ASSERT(!dmu_tx_is_syncing(tx) ||
1482 BP_IS_HOLE(dn->dn_objset->os_rootbp) ||
1483 DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1484 dn->dn_objset->os_dsl_dataset == NULL);
1485 if (dn->dn_objset->os_dsl_dataset != NULL)
1486 rrw_exit(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock, FTAG);
1489 * We make this assert for private objects as well, but after we
1490 * check if we're already dirty. They are allowed to re-dirty
1491 * in syncing context.
1493 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1494 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1495 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1497 mutex_enter(&db->db_mtx);
1499 * XXX make this true for indirects too? The problem is that
1500 * transactions created with dmu_tx_create_assigned() from
1501 * syncing context don't bother holding ahead.
1503 ASSERT(db->db_level != 0 ||
1504 db->db_state == DB_CACHED || db->db_state == DB_FILL ||
1505 db->db_state == DB_NOFILL);
1507 mutex_enter(&dn->dn_mtx);
1509 * Don't set dirtyctx to SYNC if we're just modifying this as we
1510 * initialize the objset.
1512 if (dn->dn_dirtyctx == DN_UNDIRTIED) {
1513 if (dn->dn_objset->os_dsl_dataset != NULL) {
1514 rrw_enter(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock,
1517 if (!BP_IS_HOLE(dn->dn_objset->os_rootbp)) {
1518 dn->dn_dirtyctx = (dmu_tx_is_syncing(tx) ?
1519 DN_DIRTY_SYNC : DN_DIRTY_OPEN);
1520 ASSERT(dn->dn_dirtyctx_firstset == NULL);
1521 dn->dn_dirtyctx_firstset = kmem_alloc(1, KM_SLEEP);
1523 if (dn->dn_objset->os_dsl_dataset != NULL) {
1524 rrw_exit(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock,
1528 mutex_exit(&dn->dn_mtx);
1530 if (db->db_blkid == DMU_SPILL_BLKID)
1531 dn->dn_have_spill = B_TRUE;
1534 * If this buffer is already dirty, we're done.
1536 drp = &db->db_last_dirty;
1537 ASSERT(*drp == NULL || (*drp)->dr_txg <= tx->tx_txg ||
1538 db->db.db_object == DMU_META_DNODE_OBJECT);
1539 while ((dr = *drp) != NULL && dr->dr_txg > tx->tx_txg)
1541 if (dr && dr->dr_txg == tx->tx_txg) {
1545 mutex_exit(&db->db_mtx);
1550 * Only valid if not already dirty.
1552 ASSERT(dn->dn_object == 0 ||
1553 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1554 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1556 ASSERT3U(dn->dn_nlevels, >, db->db_level);
1559 * We should only be dirtying in syncing context if it's the
1560 * mos or we're initializing the os or it's a special object.
1561 * However, we are allowed to dirty in syncing context provided
1562 * we already dirtied it in open context. Hence we must make
1563 * this assertion only if we're not already dirty.
1566 VERIFY3U(tx->tx_txg, <=, spa_final_dirty_txg(os->os_spa));
1568 if (dn->dn_objset->os_dsl_dataset != NULL)
1569 rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_READER, FTAG);
1570 ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1571 os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp));
1572 if (dn->dn_objset->os_dsl_dataset != NULL)
1573 rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG);
1575 ASSERT(db->db.db_size != 0);
1577 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1579 if (db->db_blkid != DMU_BONUS_BLKID) {
1580 dmu_objset_willuse_space(os, db->db.db_size, tx);
1584 * If this buffer is dirty in an old transaction group we need
1585 * to make a copy of it so that the changes we make in this
1586 * transaction group won't leak out when we sync the older txg.
1588 dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP);
1589 if (db->db_level == 0) {
1590 void *data_old = db->db_buf;
1592 if (db->db_state != DB_NOFILL) {
1593 if (db->db_blkid == DMU_BONUS_BLKID) {
1594 dbuf_fix_old_data(db, tx->tx_txg);
1595 data_old = db->db.db_data;
1596 } else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
1598 * Release the data buffer from the cache so
1599 * that we can modify it without impacting
1600 * possible other users of this cached data
1601 * block. Note that indirect blocks and
1602 * private objects are not released until the
1603 * syncing state (since they are only modified
1606 arc_release(db->db_buf, db);
1607 dbuf_fix_old_data(db, tx->tx_txg);
1608 data_old = db->db_buf;
1610 ASSERT(data_old != NULL);
1612 dr->dt.dl.dr_data = data_old;
1614 mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_DEFAULT, NULL);
1615 list_create(&dr->dt.di.dr_children,
1616 sizeof (dbuf_dirty_record_t),
1617 offsetof(dbuf_dirty_record_t, dr_dirty_node));
1619 if (db->db_blkid != DMU_BONUS_BLKID && os->os_dsl_dataset != NULL)
1620 dr->dr_accounted = db->db.db_size;
1622 dr->dr_txg = tx->tx_txg;
1627 * We could have been freed_in_flight between the dbuf_noread
1628 * and dbuf_dirty. We win, as though the dbuf_noread() had
1629 * happened after the free.
1631 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1632 db->db_blkid != DMU_SPILL_BLKID) {
1633 mutex_enter(&dn->dn_mtx);
1634 if (dn->dn_free_ranges[txgoff] != NULL) {
1635 range_tree_clear(dn->dn_free_ranges[txgoff],
1638 mutex_exit(&dn->dn_mtx);
1639 db->db_freed_in_flight = FALSE;
1643 * This buffer is now part of this txg
1645 dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg);
1646 db->db_dirtycnt += 1;
1647 ASSERT3U(db->db_dirtycnt, <=, 3);
1649 mutex_exit(&db->db_mtx);
1651 if (db->db_blkid == DMU_BONUS_BLKID ||
1652 db->db_blkid == DMU_SPILL_BLKID) {
1653 mutex_enter(&dn->dn_mtx);
1654 ASSERT(!list_link_active(&dr->dr_dirty_node));
1655 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1656 mutex_exit(&dn->dn_mtx);
1657 dnode_setdirty(dn, tx);
1663 * The dn_struct_rwlock prevents db_blkptr from changing
1664 * due to a write from syncing context completing
1665 * while we are running, so we want to acquire it before
1666 * looking at db_blkptr.
1668 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
1669 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1670 drop_struct_lock = TRUE;
1674 * We need to hold the dn_struct_rwlock to make this assertion,
1675 * because it protects dn_phys / dn_next_nlevels from changing.
1677 ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) ||
1678 dn->dn_phys->dn_nlevels > db->db_level ||
1679 dn->dn_next_nlevels[txgoff] > db->db_level ||
1680 dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level ||
1681 dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level);
1684 * If we are overwriting a dedup BP, then unless it is snapshotted,
1685 * when we get to syncing context we will need to decrement its
1686 * refcount in the DDT. Prefetch the relevant DDT block so that
1687 * syncing context won't have to wait for the i/o.
1689 ddt_prefetch(os->os_spa, db->db_blkptr);
1691 if (db->db_level == 0) {
1692 dnode_new_blkid(dn, db->db_blkid, tx, drop_struct_lock);
1693 ASSERT(dn->dn_maxblkid >= db->db_blkid);
1696 if (db->db_level+1 < dn->dn_nlevels) {
1697 dmu_buf_impl_t *parent = db->db_parent;
1698 dbuf_dirty_record_t *di;
1699 int parent_held = FALSE;
1701 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
1702 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1704 parent = dbuf_hold_level(dn, db->db_level+1,
1705 db->db_blkid >> epbs, FTAG);
1706 ASSERT(parent != NULL);
1709 if (drop_struct_lock)
1710 rw_exit(&dn->dn_struct_rwlock);
1711 ASSERT3U(db->db_level+1, ==, parent->db_level);
1712 di = dbuf_dirty(parent, tx);
1714 dbuf_rele(parent, FTAG);
1716 mutex_enter(&db->db_mtx);
1718 * Since we've dropped the mutex, it's possible that
1719 * dbuf_undirty() might have changed this out from under us.
1721 if (db->db_last_dirty == dr ||
1722 dn->dn_object == DMU_META_DNODE_OBJECT) {
1723 mutex_enter(&di->dt.di.dr_mtx);
1724 ASSERT3U(di->dr_txg, ==, tx->tx_txg);
1725 ASSERT(!list_link_active(&dr->dr_dirty_node));
1726 list_insert_tail(&di->dt.di.dr_children, dr);
1727 mutex_exit(&di->dt.di.dr_mtx);
1730 mutex_exit(&db->db_mtx);
1732 ASSERT(db->db_level+1 == dn->dn_nlevels);
1733 ASSERT(db->db_blkid < dn->dn_nblkptr);
1734 ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf);
1735 mutex_enter(&dn->dn_mtx);
1736 ASSERT(!list_link_active(&dr->dr_dirty_node));
1737 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1738 mutex_exit(&dn->dn_mtx);
1739 if (drop_struct_lock)
1740 rw_exit(&dn->dn_struct_rwlock);
1743 dnode_setdirty(dn, tx);
1749 * Undirty a buffer in the transaction group referenced by the given
1750 * transaction. Return whether this evicted the dbuf.
1753 dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1756 uint64_t txg = tx->tx_txg;
1757 dbuf_dirty_record_t *dr, **drp;
1762 * Due to our use of dn_nlevels below, this can only be called
1763 * in open context, unless we are operating on the MOS.
1764 * From syncing context, dn_nlevels may be different from the
1765 * dn_nlevels used when dbuf was dirtied.
1767 ASSERT(db->db_objset ==
1768 dmu_objset_pool(db->db_objset)->dp_meta_objset ||
1769 txg != spa_syncing_txg(dmu_objset_spa(db->db_objset)));
1770 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1771 ASSERT0(db->db_level);
1772 ASSERT(MUTEX_HELD(&db->db_mtx));
1775 * If this buffer is not dirty, we're done.
1777 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1778 if (dr->dr_txg <= txg)
1780 if (dr == NULL || dr->dr_txg < txg)
1782 ASSERT(dr->dr_txg == txg);
1783 ASSERT(dr->dr_dbuf == db);
1788 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1790 ASSERT(db->db.db_size != 0);
1792 dsl_pool_undirty_space(dmu_objset_pool(dn->dn_objset),
1793 dr->dr_accounted, txg);
1798 * Note that there are three places in dbuf_dirty()
1799 * where this dirty record may be put on a list.
1800 * Make sure to do a list_remove corresponding to
1801 * every one of those list_insert calls.
1803 if (dr->dr_parent) {
1804 mutex_enter(&dr->dr_parent->dt.di.dr_mtx);
1805 list_remove(&dr->dr_parent->dt.di.dr_children, dr);
1806 mutex_exit(&dr->dr_parent->dt.di.dr_mtx);
1807 } else if (db->db_blkid == DMU_SPILL_BLKID ||
1808 db->db_level + 1 == dn->dn_nlevels) {
1809 ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf);
1810 mutex_enter(&dn->dn_mtx);
1811 list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr);
1812 mutex_exit(&dn->dn_mtx);
1816 if (db->db_state != DB_NOFILL) {
1817 dbuf_unoverride(dr);
1819 ASSERT(db->db_buf != NULL);
1820 ASSERT(dr->dt.dl.dr_data != NULL);
1821 if (dr->dt.dl.dr_data != db->db_buf)
1822 arc_buf_destroy(dr->dt.dl.dr_data, db);
1825 kmem_free(dr, sizeof (dbuf_dirty_record_t));
1827 ASSERT(db->db_dirtycnt > 0);
1828 db->db_dirtycnt -= 1;
1830 if (refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) {
1831 ASSERT(db->db_state == DB_NOFILL || arc_released(db->db_buf));
1840 dmu_buf_will_dirty(dmu_buf_t *db_fake, dmu_tx_t *tx)
1842 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1843 int rf = DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH;
1845 ASSERT(tx->tx_txg != 0);
1846 ASSERT(!refcount_is_zero(&db->db_holds));
1849 * Quick check for dirtyness. For already dirty blocks, this
1850 * reduces runtime of this function by >90%, and overall performance
1851 * by 50% for some workloads (e.g. file deletion with indirect blocks
1854 mutex_enter(&db->db_mtx);
1855 dbuf_dirty_record_t *dr;
1856 for (dr = db->db_last_dirty;
1857 dr != NULL && dr->dr_txg >= tx->tx_txg; dr = dr->dr_next) {
1859 * It's possible that it is already dirty but not cached,
1860 * because there are some calls to dbuf_dirty() that don't
1861 * go through dmu_buf_will_dirty().
1863 if (dr->dr_txg == tx->tx_txg && db->db_state == DB_CACHED) {
1864 /* This dbuf is already dirty and cached. */
1866 mutex_exit(&db->db_mtx);
1870 mutex_exit(&db->db_mtx);
1873 if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock))
1874 rf |= DB_RF_HAVESTRUCT;
1876 (void) dbuf_read(db, NULL, rf);
1877 (void) dbuf_dirty(db, tx);
1881 dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1883 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1885 db->db_state = DB_NOFILL;
1887 dmu_buf_will_fill(db_fake, tx);
1891 dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1893 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1895 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1896 ASSERT(tx->tx_txg != 0);
1897 ASSERT(db->db_level == 0);
1898 ASSERT(!refcount_is_zero(&db->db_holds));
1900 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
1901 dmu_tx_private_ok(tx));
1904 (void) dbuf_dirty(db, tx);
1907 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1910 dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx)
1912 mutex_enter(&db->db_mtx);
1915 if (db->db_state == DB_FILL) {
1916 if (db->db_level == 0 && db->db_freed_in_flight) {
1917 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1918 /* we were freed while filling */
1919 /* XXX dbuf_undirty? */
1920 bzero(db->db.db_data, db->db.db_size);
1921 db->db_freed_in_flight = FALSE;
1923 db->db_state = DB_CACHED;
1924 cv_broadcast(&db->db_changed);
1926 mutex_exit(&db->db_mtx);
1930 dmu_buf_write_embedded(dmu_buf_t *dbuf, void *data,
1931 bp_embedded_type_t etype, enum zio_compress comp,
1932 int uncompressed_size, int compressed_size, int byteorder,
1935 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
1936 struct dirty_leaf *dl;
1937 dmu_object_type_t type;
1939 if (etype == BP_EMBEDDED_TYPE_DATA) {
1940 ASSERT(spa_feature_is_active(dmu_objset_spa(db->db_objset),
1941 SPA_FEATURE_EMBEDDED_DATA));
1945 type = DB_DNODE(db)->dn_type;
1948 ASSERT0(db->db_level);
1949 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1951 dmu_buf_will_not_fill(dbuf, tx);
1953 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
1954 dl = &db->db_last_dirty->dt.dl;
1955 encode_embedded_bp_compressed(&dl->dr_overridden_by,
1956 data, comp, uncompressed_size, compressed_size);
1957 BPE_SET_ETYPE(&dl->dr_overridden_by, etype);
1958 BP_SET_TYPE(&dl->dr_overridden_by, type);
1959 BP_SET_LEVEL(&dl->dr_overridden_by, 0);
1960 BP_SET_BYTEORDER(&dl->dr_overridden_by, byteorder);
1962 dl->dr_override_state = DR_OVERRIDDEN;
1963 dl->dr_overridden_by.blk_birth = db->db_last_dirty->dr_txg;
1967 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1968 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1971 dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx)
1973 ASSERT(!refcount_is_zero(&db->db_holds));
1974 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1975 ASSERT(db->db_level == 0);
1976 ASSERT3U(dbuf_is_metadata(db), ==, arc_is_metadata(buf));
1977 ASSERT(buf != NULL);
1978 ASSERT(arc_buf_lsize(buf) == db->db.db_size);
1979 ASSERT(tx->tx_txg != 0);
1981 arc_return_buf(buf, db);
1982 ASSERT(arc_released(buf));
1984 mutex_enter(&db->db_mtx);
1986 while (db->db_state == DB_READ || db->db_state == DB_FILL)
1987 cv_wait(&db->db_changed, &db->db_mtx);
1989 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED);
1991 if (db->db_state == DB_CACHED &&
1992 refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) {
1993 mutex_exit(&db->db_mtx);
1994 (void) dbuf_dirty(db, tx);
1995 bcopy(buf->b_data, db->db.db_data, db->db.db_size);
1996 arc_buf_destroy(buf, db);
1997 xuio_stat_wbuf_copied();
2001 xuio_stat_wbuf_nocopy();
2002 if (db->db_state == DB_CACHED) {
2003 dbuf_dirty_record_t *dr = db->db_last_dirty;
2005 ASSERT(db->db_buf != NULL);
2006 if (dr != NULL && dr->dr_txg == tx->tx_txg) {
2007 ASSERT(dr->dt.dl.dr_data == db->db_buf);
2008 if (!arc_released(db->db_buf)) {
2009 ASSERT(dr->dt.dl.dr_override_state ==
2011 arc_release(db->db_buf, db);
2013 dr->dt.dl.dr_data = buf;
2014 arc_buf_destroy(db->db_buf, db);
2015 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) {
2016 arc_release(db->db_buf, db);
2017 arc_buf_destroy(db->db_buf, db);
2021 ASSERT(db->db_buf == NULL);
2022 dbuf_set_data(db, buf);
2023 db->db_state = DB_FILL;
2024 mutex_exit(&db->db_mtx);
2025 (void) dbuf_dirty(db, tx);
2026 dmu_buf_fill_done(&db->db, tx);
2030 dbuf_destroy(dmu_buf_impl_t *db)
2033 dmu_buf_impl_t *parent = db->db_parent;
2034 dmu_buf_impl_t *dndb;
2036 ASSERT(MUTEX_HELD(&db->db_mtx));
2037 ASSERT(refcount_is_zero(&db->db_holds));
2039 if (db->db_buf != NULL) {
2040 arc_buf_destroy(db->db_buf, db);
2044 if (db->db_blkid == DMU_BONUS_BLKID) {
2045 ASSERT(db->db.db_data != NULL);
2046 zio_buf_free(db->db.db_data, DN_MAX_BONUSLEN);
2047 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
2048 db->db_state = DB_UNCACHED;
2051 dbuf_clear_data(db);
2053 if (multilist_link_active(&db->db_cache_link)) {
2054 multilist_remove(dbuf_cache, db);
2055 (void) refcount_remove_many(&dbuf_cache_size,
2056 db->db.db_size, db);
2059 ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL);
2060 ASSERT(db->db_data_pending == NULL);
2062 db->db_state = DB_EVICTING;
2063 db->db_blkptr = NULL;
2066 * Now that db_state is DB_EVICTING, nobody else can find this via
2067 * the hash table. We can now drop db_mtx, which allows us to
2068 * acquire the dn_dbufs_mtx.
2070 mutex_exit(&db->db_mtx);
2075 if (db->db_blkid != DMU_BONUS_BLKID) {
2076 boolean_t needlock = !MUTEX_HELD(&dn->dn_dbufs_mtx);
2078 mutex_enter(&dn->dn_dbufs_mtx);
2079 avl_remove(&dn->dn_dbufs, db);
2080 atomic_dec_32(&dn->dn_dbufs_count);
2084 mutex_exit(&dn->dn_dbufs_mtx);
2086 * Decrementing the dbuf count means that the hold corresponding
2087 * to the removed dbuf is no longer discounted in dnode_move(),
2088 * so the dnode cannot be moved until after we release the hold.
2089 * The membar_producer() ensures visibility of the decremented
2090 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
2094 db->db_dnode_handle = NULL;
2096 dbuf_hash_remove(db);
2101 ASSERT(refcount_is_zero(&db->db_holds));
2103 db->db_parent = NULL;
2105 ASSERT(db->db_buf == NULL);
2106 ASSERT(db->db.db_data == NULL);
2107 ASSERT(db->db_hash_next == NULL);
2108 ASSERT(db->db_blkptr == NULL);
2109 ASSERT(db->db_data_pending == NULL);
2110 ASSERT(!multilist_link_active(&db->db_cache_link));
2112 kmem_cache_free(dbuf_kmem_cache, db);
2113 arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
2116 * If this dbuf is referenced from an indirect dbuf,
2117 * decrement the ref count on the indirect dbuf.
2119 if (parent && parent != dndb)
2120 dbuf_rele(parent, db);
2124 * Note: While bpp will always be updated if the function returns success,
2125 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
2126 * this happens when the dnode is the meta-dnode, or a userused or groupused
2130 dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse,
2131 dmu_buf_impl_t **parentp, blkptr_t **bpp)
2136 ASSERT(blkid != DMU_BONUS_BLKID);
2138 if (blkid == DMU_SPILL_BLKID) {
2139 mutex_enter(&dn->dn_mtx);
2140 if (dn->dn_have_spill &&
2141 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
2142 *bpp = &dn->dn_phys->dn_spill;
2145 dbuf_add_ref(dn->dn_dbuf, NULL);
2146 *parentp = dn->dn_dbuf;
2147 mutex_exit(&dn->dn_mtx);
2152 (dn->dn_phys->dn_nlevels == 0) ? 1 : dn->dn_phys->dn_nlevels;
2153 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
2155 ASSERT3U(level * epbs, <, 64);
2156 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2158 * This assertion shouldn't trip as long as the max indirect block size
2159 * is less than 1M. The reason for this is that up to that point,
2160 * the number of levels required to address an entire object with blocks
2161 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In
2162 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55
2163 * (i.e. we can address the entire object), objects will all use at most
2164 * N-1 levels and the assertion won't overflow. However, once epbs is
2165 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be
2166 * enough to address an entire object, so objects will have 5 levels,
2167 * but then this assertion will overflow.
2169 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we
2170 * need to redo this logic to handle overflows.
2172 ASSERT(level >= nlevels ||
2173 ((nlevels - level - 1) * epbs) +
2174 highbit64(dn->dn_phys->dn_nblkptr) <= 64);
2175 if (level >= nlevels ||
2176 blkid >= ((uint64_t)dn->dn_phys->dn_nblkptr <<
2177 ((nlevels - level - 1) * epbs)) ||
2179 blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) {
2180 /* the buffer has no parent yet */
2181 return (SET_ERROR(ENOENT));
2182 } else if (level < nlevels-1) {
2183 /* this block is referenced from an indirect block */
2184 int err = dbuf_hold_impl(dn, level+1,
2185 blkid >> epbs, fail_sparse, FALSE, NULL, parentp);
2188 err = dbuf_read(*parentp, NULL,
2189 (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL));
2191 dbuf_rele(*parentp, NULL);
2195 *bpp = ((blkptr_t *)(*parentp)->db.db_data) +
2196 (blkid & ((1ULL << epbs) - 1));
2197 if (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))
2198 ASSERT(BP_IS_HOLE(*bpp));
2201 /* the block is referenced from the dnode */
2202 ASSERT3U(level, ==, nlevels-1);
2203 ASSERT(dn->dn_phys->dn_nblkptr == 0 ||
2204 blkid < dn->dn_phys->dn_nblkptr);
2206 dbuf_add_ref(dn->dn_dbuf, NULL);
2207 *parentp = dn->dn_dbuf;
2209 *bpp = &dn->dn_phys->dn_blkptr[blkid];
2214 static dmu_buf_impl_t *
2215 dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid,
2216 dmu_buf_impl_t *parent, blkptr_t *blkptr)
2218 objset_t *os = dn->dn_objset;
2219 dmu_buf_impl_t *db, *odb;
2221 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2222 ASSERT(dn->dn_type != DMU_OT_NONE);
2224 db = kmem_cache_alloc(dbuf_kmem_cache, KM_SLEEP);
2227 db->db.db_object = dn->dn_object;
2228 db->db_level = level;
2229 db->db_blkid = blkid;
2230 db->db_last_dirty = NULL;
2231 db->db_dirtycnt = 0;
2232 db->db_dnode_handle = dn->dn_handle;
2233 db->db_parent = parent;
2234 db->db_blkptr = blkptr;
2237 db->db_user_immediate_evict = FALSE;
2238 db->db_freed_in_flight = FALSE;
2239 db->db_pending_evict = FALSE;
2241 if (blkid == DMU_BONUS_BLKID) {
2242 ASSERT3P(parent, ==, dn->dn_dbuf);
2243 db->db.db_size = DN_MAX_BONUSLEN -
2244 (dn->dn_nblkptr-1) * sizeof (blkptr_t);
2245 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
2246 db->db.db_offset = DMU_BONUS_BLKID;
2247 db->db_state = DB_UNCACHED;
2248 /* the bonus dbuf is not placed in the hash table */
2249 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
2251 } else if (blkid == DMU_SPILL_BLKID) {
2252 db->db.db_size = (blkptr != NULL) ?
2253 BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE;
2254 db->db.db_offset = 0;
2257 db->db_level ? 1 << dn->dn_indblkshift : dn->dn_datablksz;
2258 db->db.db_size = blocksize;
2259 db->db.db_offset = db->db_blkid * blocksize;
2263 * Hold the dn_dbufs_mtx while we get the new dbuf
2264 * in the hash table *and* added to the dbufs list.
2265 * This prevents a possible deadlock with someone
2266 * trying to look up this dbuf before its added to the
2269 mutex_enter(&dn->dn_dbufs_mtx);
2270 db->db_state = DB_EVICTING;
2271 if ((odb = dbuf_hash_insert(db)) != NULL) {
2272 /* someone else inserted it first */
2273 kmem_cache_free(dbuf_kmem_cache, db);
2274 mutex_exit(&dn->dn_dbufs_mtx);
2277 avl_add(&dn->dn_dbufs, db);
2279 db->db_state = DB_UNCACHED;
2280 mutex_exit(&dn->dn_dbufs_mtx);
2281 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
2283 if (parent && parent != dn->dn_dbuf)
2284 dbuf_add_ref(parent, db);
2286 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
2287 refcount_count(&dn->dn_holds) > 0);
2288 (void) refcount_add(&dn->dn_holds, db);
2289 atomic_inc_32(&dn->dn_dbufs_count);
2291 dprintf_dbuf(db, "db=%p\n", db);
2296 typedef struct dbuf_prefetch_arg {
2297 spa_t *dpa_spa; /* The spa to issue the prefetch in. */
2298 zbookmark_phys_t dpa_zb; /* The target block to prefetch. */
2299 int dpa_epbs; /* Entries (blkptr_t's) Per Block Shift. */
2300 int dpa_curlevel; /* The current level that we're reading */
2301 dnode_t *dpa_dnode; /* The dnode associated with the prefetch */
2302 zio_priority_t dpa_prio; /* The priority I/Os should be issued at. */
2303 zio_t *dpa_zio; /* The parent zio_t for all prefetches. */
2304 arc_flags_t dpa_aflags; /* Flags to pass to the final prefetch. */
2305 } dbuf_prefetch_arg_t;
2308 * Actually issue the prefetch read for the block given.
2311 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t *dpa, blkptr_t *bp)
2313 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
2316 arc_flags_t aflags =
2317 dpa->dpa_aflags | ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH;
2319 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp));
2320 ASSERT3U(dpa->dpa_curlevel, ==, dpa->dpa_zb.zb_level);
2321 ASSERT(dpa->dpa_zio != NULL);
2322 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa, bp, NULL, NULL,
2323 dpa->dpa_prio, ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2324 &aflags, &dpa->dpa_zb);
2328 * Called when an indirect block above our prefetch target is read in. This
2329 * will either read in the next indirect block down the tree or issue the actual
2330 * prefetch if the next block down is our target.
2333 dbuf_prefetch_indirect_done(zio_t *zio, const zbookmark_phys_t *zb,
2334 const blkptr_t *iobp, arc_buf_t *abuf, void *private)
2336 dbuf_prefetch_arg_t *dpa = private;
2338 ASSERT3S(dpa->dpa_zb.zb_level, <, dpa->dpa_curlevel);
2339 ASSERT3S(dpa->dpa_curlevel, >, 0);
2342 * The dpa_dnode is only valid if we are called with a NULL
2343 * zio. This indicates that the arc_read() returned without
2344 * first calling zio_read() to issue a physical read. Once
2345 * a physical read is made the dpa_dnode must be invalidated
2346 * as the locks guarding it may have been dropped. If the
2347 * dpa_dnode is still valid, then we want to add it to the dbuf
2348 * cache. To do so, we must hold the dbuf associated with the block
2349 * we just prefetched, read its contents so that we associate it
2350 * with an arc_buf_t, and then release it.
2353 ASSERT3S(BP_GET_LEVEL(zio->io_bp), ==, dpa->dpa_curlevel);
2354 if (zio->io_flags & ZIO_FLAG_RAW) {
2355 ASSERT3U(BP_GET_PSIZE(zio->io_bp), ==, zio->io_size);
2357 ASSERT3U(BP_GET_LSIZE(zio->io_bp), ==, zio->io_size);
2359 ASSERT3P(zio->io_spa, ==, dpa->dpa_spa);
2361 dpa->dpa_dnode = NULL;
2362 } else if (dpa->dpa_dnode != NULL) {
2363 uint64_t curblkid = dpa->dpa_zb.zb_blkid >>
2364 (dpa->dpa_epbs * (dpa->dpa_curlevel -
2365 dpa->dpa_zb.zb_level));
2366 dmu_buf_impl_t *db = dbuf_hold_level(dpa->dpa_dnode,
2367 dpa->dpa_curlevel, curblkid, FTAG);
2368 (void) dbuf_read(db, NULL,
2369 DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH | DB_RF_HAVESTRUCT);
2370 dbuf_rele(db, FTAG);
2374 kmem_free(dpa, sizeof(*dpa));
2378 dpa->dpa_curlevel--;
2380 uint64_t nextblkid = dpa->dpa_zb.zb_blkid >>
2381 (dpa->dpa_epbs * (dpa->dpa_curlevel - dpa->dpa_zb.zb_level));
2382 blkptr_t *bp = ((blkptr_t *)abuf->b_data) +
2383 P2PHASE(nextblkid, 1ULL << dpa->dpa_epbs);
2384 if (BP_IS_HOLE(bp)) {
2385 kmem_free(dpa, sizeof (*dpa));
2386 } else if (dpa->dpa_curlevel == dpa->dpa_zb.zb_level) {
2387 ASSERT3U(nextblkid, ==, dpa->dpa_zb.zb_blkid);
2388 dbuf_issue_final_prefetch(dpa, bp);
2389 kmem_free(dpa, sizeof (*dpa));
2391 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT;
2392 zbookmark_phys_t zb;
2394 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2395 if (dpa->dpa_aflags & ARC_FLAG_L2CACHE)
2396 iter_aflags |= ARC_FLAG_L2CACHE;
2398 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp));
2400 SET_BOOKMARK(&zb, dpa->dpa_zb.zb_objset,
2401 dpa->dpa_zb.zb_object, dpa->dpa_curlevel, nextblkid);
2403 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa,
2404 bp, dbuf_prefetch_indirect_done, dpa, dpa->dpa_prio,
2405 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2409 arc_buf_destroy(abuf, private);
2413 * Issue prefetch reads for the given block on the given level. If the indirect
2414 * blocks above that block are not in memory, we will read them in
2415 * asynchronously. As a result, this call never blocks waiting for a read to
2419 dbuf_prefetch(dnode_t *dn, int64_t level, uint64_t blkid, zio_priority_t prio,
2423 int epbs, nlevels, curlevel;
2426 ASSERT(blkid != DMU_BONUS_BLKID);
2427 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2429 if (blkid > dn->dn_maxblkid)
2432 if (dnode_block_freed(dn, blkid))
2436 * This dnode hasn't been written to disk yet, so there's nothing to
2439 nlevels = dn->dn_phys->dn_nlevels;
2440 if (level >= nlevels || dn->dn_phys->dn_nblkptr == 0)
2443 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2444 if (dn->dn_phys->dn_maxblkid < blkid << (epbs * level))
2447 dmu_buf_impl_t *db = dbuf_find(dn->dn_objset, dn->dn_object,
2450 mutex_exit(&db->db_mtx);
2452 * This dbuf already exists. It is either CACHED, or
2453 * (we assume) about to be read or filled.
2459 * Find the closest ancestor (indirect block) of the target block
2460 * that is present in the cache. In this indirect block, we will
2461 * find the bp that is at curlevel, curblkid.
2465 while (curlevel < nlevels - 1) {
2466 int parent_level = curlevel + 1;
2467 uint64_t parent_blkid = curblkid >> epbs;
2470 if (dbuf_hold_impl(dn, parent_level, parent_blkid,
2471 FALSE, TRUE, FTAG, &db) == 0) {
2472 blkptr_t *bpp = db->db_buf->b_data;
2473 bp = bpp[P2PHASE(curblkid, 1 << epbs)];
2474 dbuf_rele(db, FTAG);
2478 curlevel = parent_level;
2479 curblkid = parent_blkid;
2482 if (curlevel == nlevels - 1) {
2483 /* No cached indirect blocks found. */
2484 ASSERT3U(curblkid, <, dn->dn_phys->dn_nblkptr);
2485 bp = dn->dn_phys->dn_blkptr[curblkid];
2487 if (BP_IS_HOLE(&bp))
2490 ASSERT3U(curlevel, ==, BP_GET_LEVEL(&bp));
2492 zio_t *pio = zio_root(dmu_objset_spa(dn->dn_objset), NULL, NULL,
2495 dbuf_prefetch_arg_t *dpa = kmem_zalloc(sizeof (*dpa), KM_SLEEP);
2496 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
2497 SET_BOOKMARK(&dpa->dpa_zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET,
2498 dn->dn_object, level, blkid);
2499 dpa->dpa_curlevel = curlevel;
2500 dpa->dpa_prio = prio;
2501 dpa->dpa_aflags = aflags;
2502 dpa->dpa_spa = dn->dn_objset->os_spa;
2503 dpa->dpa_dnode = dn;
2504 dpa->dpa_epbs = epbs;
2507 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2508 if (DNODE_LEVEL_IS_L2CACHEABLE(dn, level))
2509 dpa->dpa_aflags |= ARC_FLAG_L2CACHE;
2512 * If we have the indirect just above us, no need to do the asynchronous
2513 * prefetch chain; we'll just run the last step ourselves. If we're at
2514 * a higher level, though, we want to issue the prefetches for all the
2515 * indirect blocks asynchronously, so we can go on with whatever we were
2518 if (curlevel == level) {
2519 ASSERT3U(curblkid, ==, blkid);
2520 dbuf_issue_final_prefetch(dpa, &bp);
2521 kmem_free(dpa, sizeof (*dpa));
2523 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT;
2524 zbookmark_phys_t zb;
2526 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2527 if (DNODE_LEVEL_IS_L2CACHEABLE(dn, level))
2528 iter_aflags |= ARC_FLAG_L2CACHE;
2530 SET_BOOKMARK(&zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET,
2531 dn->dn_object, curlevel, curblkid);
2532 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa,
2533 &bp, dbuf_prefetch_indirect_done, dpa, prio,
2534 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2538 * We use pio here instead of dpa_zio since it's possible that
2539 * dpa may have already been freed.
2545 * Returns with db_holds incremented, and db_mtx not held.
2546 * Note: dn_struct_rwlock must be held.
2549 dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid,
2550 boolean_t fail_sparse, boolean_t fail_uncached,
2551 void *tag, dmu_buf_impl_t **dbp)
2553 dmu_buf_impl_t *db, *parent = NULL;
2555 ASSERT(blkid != DMU_BONUS_BLKID);
2556 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2557 ASSERT3U(dn->dn_nlevels, >, level);
2561 /* dbuf_find() returns with db_mtx held */
2562 db = dbuf_find(dn->dn_objset, dn->dn_object, level, blkid);
2565 blkptr_t *bp = NULL;
2569 return (SET_ERROR(ENOENT));
2571 ASSERT3P(parent, ==, NULL);
2572 err = dbuf_findbp(dn, level, blkid, fail_sparse, &parent, &bp);
2574 if (err == 0 && bp && BP_IS_HOLE(bp))
2575 err = SET_ERROR(ENOENT);
2578 dbuf_rele(parent, NULL);
2582 if (err && err != ENOENT)
2584 db = dbuf_create(dn, level, blkid, parent, bp);
2587 if (fail_uncached && db->db_state != DB_CACHED) {
2588 mutex_exit(&db->db_mtx);
2589 return (SET_ERROR(ENOENT));
2592 if (db->db_buf != NULL) {
2593 arc_buf_access(db->db_buf);
2594 ASSERT3P(db->db.db_data, ==, db->db_buf->b_data);
2597 ASSERT(db->db_buf == NULL || arc_referenced(db->db_buf));
2600 * If this buffer is currently syncing out, and we are are
2601 * still referencing it from db_data, we need to make a copy
2602 * of it in case we decide we want to dirty it again in this txg.
2604 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
2605 dn->dn_object != DMU_META_DNODE_OBJECT &&
2606 db->db_state == DB_CACHED && db->db_data_pending) {
2607 dbuf_dirty_record_t *dr = db->db_data_pending;
2609 if (dr->dt.dl.dr_data == db->db_buf) {
2610 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
2613 arc_alloc_buf(dn->dn_objset->os_spa, db, type,
2615 bcopy(dr->dt.dl.dr_data->b_data, db->db.db_data,
2620 if (multilist_link_active(&db->db_cache_link)) {
2621 ASSERT(refcount_is_zero(&db->db_holds));
2622 multilist_remove(dbuf_cache, db);
2623 (void) refcount_remove_many(&dbuf_cache_size,
2624 db->db.db_size, db);
2626 (void) refcount_add(&db->db_holds, tag);
2628 mutex_exit(&db->db_mtx);
2630 /* NOTE: we can't rele the parent until after we drop the db_mtx */
2632 dbuf_rele(parent, NULL);
2634 ASSERT3P(DB_DNODE(db), ==, dn);
2635 ASSERT3U(db->db_blkid, ==, blkid);
2636 ASSERT3U(db->db_level, ==, level);
2643 dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag)
2645 return (dbuf_hold_level(dn, 0, blkid, tag));
2649 dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag)
2652 int err = dbuf_hold_impl(dn, level, blkid, FALSE, FALSE, tag, &db);
2653 return (err ? NULL : db);
2657 dbuf_create_bonus(dnode_t *dn)
2659 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
2661 ASSERT(dn->dn_bonus == NULL);
2662 dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL);
2666 dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx)
2668 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2671 if (db->db_blkid != DMU_SPILL_BLKID)
2672 return (SET_ERROR(ENOTSUP));
2674 blksz = SPA_MINBLOCKSIZE;
2675 ASSERT3U(blksz, <=, spa_maxblocksize(dmu_objset_spa(db->db_objset)));
2676 blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE);
2680 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2681 dbuf_new_size(db, blksz, tx);
2682 rw_exit(&dn->dn_struct_rwlock);
2689 dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx)
2691 dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx);
2694 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2696 dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
2698 int64_t holds = refcount_add(&db->db_holds, tag);
2699 ASSERT3S(holds, >, 1);
2702 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
2704 dbuf_try_add_ref(dmu_buf_t *db_fake, objset_t *os, uint64_t obj, uint64_t blkid,
2707 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2708 dmu_buf_impl_t *found_db;
2709 boolean_t result = B_FALSE;
2711 if (db->db_blkid == DMU_BONUS_BLKID)
2712 found_db = dbuf_find_bonus(os, obj);
2714 found_db = dbuf_find(os, obj, 0, blkid);
2716 if (found_db != NULL) {
2717 if (db == found_db && dbuf_refcount(db) > db->db_dirtycnt) {
2718 (void) refcount_add(&db->db_holds, tag);
2721 mutex_exit(&db->db_mtx);
2727 * If you call dbuf_rele() you had better not be referencing the dnode handle
2728 * unless you have some other direct or indirect hold on the dnode. (An indirect
2729 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2730 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2731 * dnode's parent dbuf evicting its dnode handles.
2734 dbuf_rele(dmu_buf_impl_t *db, void *tag)
2736 mutex_enter(&db->db_mtx);
2737 dbuf_rele_and_unlock(db, tag);
2741 dmu_buf_rele(dmu_buf_t *db, void *tag)
2743 dbuf_rele((dmu_buf_impl_t *)db, tag);
2747 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2748 * db_dirtycnt and db_holds to be updated atomically.
2751 dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag)
2755 ASSERT(MUTEX_HELD(&db->db_mtx));
2759 * Remove the reference to the dbuf before removing its hold on the
2760 * dnode so we can guarantee in dnode_move() that a referenced bonus
2761 * buffer has a corresponding dnode hold.
2763 holds = refcount_remove(&db->db_holds, tag);
2767 * We can't freeze indirects if there is a possibility that they
2768 * may be modified in the current syncing context.
2770 if (db->db_buf != NULL &&
2771 holds == (db->db_level == 0 ? db->db_dirtycnt : 0)) {
2772 arc_buf_freeze(db->db_buf);
2775 if (holds == db->db_dirtycnt &&
2776 db->db_level == 0 && db->db_user_immediate_evict)
2777 dbuf_evict_user(db);
2780 if (db->db_blkid == DMU_BONUS_BLKID) {
2782 boolean_t evict_dbuf = db->db_pending_evict;
2785 * If the dnode moves here, we cannot cross this
2786 * barrier until the move completes.
2791 atomic_dec_32(&dn->dn_dbufs_count);
2794 * Decrementing the dbuf count means that the bonus
2795 * buffer's dnode hold is no longer discounted in
2796 * dnode_move(). The dnode cannot move until after
2797 * the dnode_rele() below.
2802 * Do not reference db after its lock is dropped.
2803 * Another thread may evict it.
2805 mutex_exit(&db->db_mtx);
2808 dnode_evict_bonus(dn);
2811 } else if (db->db_buf == NULL) {
2813 * This is a special case: we never associated this
2814 * dbuf with any data allocated from the ARC.
2816 ASSERT(db->db_state == DB_UNCACHED ||
2817 db->db_state == DB_NOFILL);
2819 } else if (arc_released(db->db_buf)) {
2821 * This dbuf has anonymous data associated with it.
2825 boolean_t do_arc_evict = B_FALSE;
2827 spa_t *spa = dmu_objset_spa(db->db_objset);
2829 if (!DBUF_IS_CACHEABLE(db) &&
2830 db->db_blkptr != NULL &&
2831 !BP_IS_HOLE(db->db_blkptr) &&
2832 !BP_IS_EMBEDDED(db->db_blkptr)) {
2833 do_arc_evict = B_TRUE;
2834 bp = *db->db_blkptr;
2837 if (!DBUF_IS_CACHEABLE(db) ||
2838 db->db_pending_evict) {
2840 } else if (!multilist_link_active(&db->db_cache_link)) {
2841 multilist_insert(dbuf_cache, db);
2842 (void) refcount_add_many(&dbuf_cache_size,
2843 db->db.db_size, db);
2844 mutex_exit(&db->db_mtx);
2846 dbuf_evict_notify();
2850 arc_freed(spa, &bp);
2853 mutex_exit(&db->db_mtx);
2858 #pragma weak dmu_buf_refcount = dbuf_refcount
2860 dbuf_refcount(dmu_buf_impl_t *db)
2862 return (refcount_count(&db->db_holds));
2866 dmu_buf_replace_user(dmu_buf_t *db_fake, dmu_buf_user_t *old_user,
2867 dmu_buf_user_t *new_user)
2869 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2871 mutex_enter(&db->db_mtx);
2872 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2873 if (db->db_user == old_user)
2874 db->db_user = new_user;
2876 old_user = db->db_user;
2877 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2878 mutex_exit(&db->db_mtx);
2884 dmu_buf_set_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2886 return (dmu_buf_replace_user(db_fake, NULL, user));
2890 dmu_buf_set_user_ie(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2892 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2894 db->db_user_immediate_evict = TRUE;
2895 return (dmu_buf_set_user(db_fake, user));
2899 dmu_buf_remove_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2901 return (dmu_buf_replace_user(db_fake, user, NULL));
2905 dmu_buf_get_user(dmu_buf_t *db_fake)
2907 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2909 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2910 return (db->db_user);
2914 dmu_buf_user_evict_wait()
2916 taskq_wait(dbu_evict_taskq);
2920 dmu_buf_get_blkptr(dmu_buf_t *db)
2922 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2923 return (dbi->db_blkptr);
2927 dmu_buf_get_objset(dmu_buf_t *db)
2929 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2930 return (dbi->db_objset);
2934 dmu_buf_dnode_enter(dmu_buf_t *db)
2936 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2937 DB_DNODE_ENTER(dbi);
2938 return (DB_DNODE(dbi));
2942 dmu_buf_dnode_exit(dmu_buf_t *db)
2944 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2949 dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db)
2951 /* ASSERT(dmu_tx_is_syncing(tx) */
2952 ASSERT(MUTEX_HELD(&db->db_mtx));
2954 if (db->db_blkptr != NULL)
2957 if (db->db_blkid == DMU_SPILL_BLKID) {
2958 db->db_blkptr = &dn->dn_phys->dn_spill;
2959 BP_ZERO(db->db_blkptr);
2962 if (db->db_level == dn->dn_phys->dn_nlevels-1) {
2964 * This buffer was allocated at a time when there was
2965 * no available blkptrs from the dnode, or it was
2966 * inappropriate to hook it in (i.e., nlevels mis-match).
2968 ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr);
2969 ASSERT(db->db_parent == NULL);
2970 db->db_parent = dn->dn_dbuf;
2971 db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
2974 dmu_buf_impl_t *parent = db->db_parent;
2975 int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2977 ASSERT(dn->dn_phys->dn_nlevels > 1);
2978 if (parent == NULL) {
2979 mutex_exit(&db->db_mtx);
2980 rw_enter(&dn->dn_struct_rwlock, RW_READER);
2981 parent = dbuf_hold_level(dn, db->db_level + 1,
2982 db->db_blkid >> epbs, db);
2983 rw_exit(&dn->dn_struct_rwlock);
2984 mutex_enter(&db->db_mtx);
2985 db->db_parent = parent;
2987 db->db_blkptr = (blkptr_t *)parent->db.db_data +
2988 (db->db_blkid & ((1ULL << epbs) - 1));
2994 dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2996 dmu_buf_impl_t *db = dr->dr_dbuf;
3000 ASSERT(dmu_tx_is_syncing(tx));
3002 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
3004 mutex_enter(&db->db_mtx);
3006 ASSERT(db->db_level > 0);
3009 /* Read the block if it hasn't been read yet. */
3010 if (db->db_buf == NULL) {
3011 mutex_exit(&db->db_mtx);
3012 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
3013 mutex_enter(&db->db_mtx);
3015 ASSERT3U(db->db_state, ==, DB_CACHED);
3016 ASSERT(db->db_buf != NULL);
3020 /* Indirect block size must match what the dnode thinks it is. */
3021 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
3022 dbuf_check_blkptr(dn, db);
3025 /* Provide the pending dirty record to child dbufs */
3026 db->db_data_pending = dr;
3028 mutex_exit(&db->db_mtx);
3030 dbuf_write(dr, db->db_buf, tx);
3033 mutex_enter(&dr->dt.di.dr_mtx);
3034 dbuf_sync_list(&dr->dt.di.dr_children, db->db_level - 1, tx);
3035 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
3036 mutex_exit(&dr->dt.di.dr_mtx);
3041 dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
3043 arc_buf_t **datap = &dr->dt.dl.dr_data;
3044 dmu_buf_impl_t *db = dr->dr_dbuf;
3047 uint64_t txg = tx->tx_txg;
3049 ASSERT(dmu_tx_is_syncing(tx));
3051 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
3053 mutex_enter(&db->db_mtx);
3055 * To be synced, we must be dirtied. But we
3056 * might have been freed after the dirty.
3058 if (db->db_state == DB_UNCACHED) {
3059 /* This buffer has been freed since it was dirtied */
3060 ASSERT(db->db.db_data == NULL);
3061 } else if (db->db_state == DB_FILL) {
3062 /* This buffer was freed and is now being re-filled */
3063 ASSERT(db->db.db_data != dr->dt.dl.dr_data);
3065 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL);
3072 if (db->db_blkid == DMU_SPILL_BLKID) {
3073 mutex_enter(&dn->dn_mtx);
3074 dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR;
3075 mutex_exit(&dn->dn_mtx);
3079 * If this is a bonus buffer, simply copy the bonus data into the
3080 * dnode. It will be written out when the dnode is synced (and it
3081 * will be synced, since it must have been dirty for dbuf_sync to
3084 if (db->db_blkid == DMU_BONUS_BLKID) {
3085 dbuf_dirty_record_t **drp;
3087 ASSERT(*datap != NULL);
3088 ASSERT0(db->db_level);
3089 ASSERT3U(dn->dn_phys->dn_bonuslen, <=, DN_MAX_BONUSLEN);
3090 bcopy(*datap, DN_BONUS(dn->dn_phys), dn->dn_phys->dn_bonuslen);
3093 if (*datap != db->db.db_data) {
3094 zio_buf_free(*datap, DN_MAX_BONUSLEN);
3095 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
3097 db->db_data_pending = NULL;
3098 drp = &db->db_last_dirty;
3100 drp = &(*drp)->dr_next;
3101 ASSERT(dr->dr_next == NULL);
3102 ASSERT(dr->dr_dbuf == db);
3104 if (dr->dr_dbuf->db_level != 0) {
3105 list_destroy(&dr->dt.di.dr_children);
3106 mutex_destroy(&dr->dt.di.dr_mtx);
3108 kmem_free(dr, sizeof (dbuf_dirty_record_t));
3109 ASSERT(db->db_dirtycnt > 0);
3110 db->db_dirtycnt -= 1;
3111 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
3118 * This function may have dropped the db_mtx lock allowing a dmu_sync
3119 * operation to sneak in. As a result, we need to ensure that we
3120 * don't check the dr_override_state until we have returned from
3121 * dbuf_check_blkptr.
3123 dbuf_check_blkptr(dn, db);
3126 * If this buffer is in the middle of an immediate write,
3127 * wait for the synchronous IO to complete.
3129 while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
3130 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
3131 cv_wait(&db->db_changed, &db->db_mtx);
3132 ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN);
3135 if (db->db_state != DB_NOFILL &&
3136 dn->dn_object != DMU_META_DNODE_OBJECT &&
3137 refcount_count(&db->db_holds) > 1 &&
3138 dr->dt.dl.dr_override_state != DR_OVERRIDDEN &&
3139 *datap == db->db_buf) {
3141 * If this buffer is currently "in use" (i.e., there
3142 * are active holds and db_data still references it),
3143 * then make a copy before we start the write so that
3144 * any modifications from the open txg will not leak
3147 * NOTE: this copy does not need to be made for
3148 * objects only modified in the syncing context (e.g.
3149 * DNONE_DNODE blocks).
3151 int psize = arc_buf_size(*datap);
3152 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
3153 enum zio_compress compress_type = arc_get_compression(*datap);
3155 if (compress_type == ZIO_COMPRESS_OFF) {
3156 *datap = arc_alloc_buf(os->os_spa, db, type, psize);
3158 ASSERT3U(type, ==, ARC_BUFC_DATA);
3159 int lsize = arc_buf_lsize(*datap);
3160 *datap = arc_alloc_compressed_buf(os->os_spa, db,
3161 psize, lsize, compress_type);
3163 bcopy(db->db.db_data, (*datap)->b_data, psize);
3165 db->db_data_pending = dr;
3167 mutex_exit(&db->db_mtx);
3169 dbuf_write(dr, *datap, tx);
3171 ASSERT(!list_link_active(&dr->dr_dirty_node));
3172 if (dn->dn_object == DMU_META_DNODE_OBJECT) {
3173 list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr);
3177 * Although zio_nowait() does not "wait for an IO", it does
3178 * initiate the IO. If this is an empty write it seems plausible
3179 * that the IO could actually be completed before the nowait
3180 * returns. We need to DB_DNODE_EXIT() first in case
3181 * zio_nowait() invalidates the dbuf.
3184 zio_nowait(dr->dr_zio);
3189 dbuf_sync_list(list_t *list, int level, dmu_tx_t *tx)
3191 dbuf_dirty_record_t *dr;
3193 while (dr = list_head(list)) {
3194 if (dr->dr_zio != NULL) {
3196 * If we find an already initialized zio then we
3197 * are processing the meta-dnode, and we have finished.
3198 * The dbufs for all dnodes are put back on the list
3199 * during processing, so that we can zio_wait()
3200 * these IOs after initiating all child IOs.
3202 ASSERT3U(dr->dr_dbuf->db.db_object, ==,
3203 DMU_META_DNODE_OBJECT);
3206 if (dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
3207 dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
3208 VERIFY3U(dr->dr_dbuf->db_level, ==, level);
3210 list_remove(list, dr);
3211 if (dr->dr_dbuf->db_level > 0)
3212 dbuf_sync_indirect(dr, tx);
3214 dbuf_sync_leaf(dr, tx);
3220 dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
3222 dmu_buf_impl_t *db = vdb;
3224 blkptr_t *bp = zio->io_bp;
3225 blkptr_t *bp_orig = &zio->io_bp_orig;
3226 spa_t *spa = zio->io_spa;
3231 ASSERT3P(db->db_blkptr, !=, NULL);
3232 ASSERT3P(&db->db_data_pending->dr_bp_copy, ==, bp);
3236 delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig);
3237 dnode_diduse_space(dn, delta - zio->io_prev_space_delta);
3238 zio->io_prev_space_delta = delta;
3240 if (bp->blk_birth != 0) {
3241 ASSERT((db->db_blkid != DMU_SPILL_BLKID &&
3242 BP_GET_TYPE(bp) == dn->dn_type) ||
3243 (db->db_blkid == DMU_SPILL_BLKID &&
3244 BP_GET_TYPE(bp) == dn->dn_bonustype) ||
3245 BP_IS_EMBEDDED(bp));
3246 ASSERT(BP_GET_LEVEL(bp) == db->db_level);
3249 mutex_enter(&db->db_mtx);
3252 if (db->db_blkid == DMU_SPILL_BLKID) {
3253 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
3254 ASSERT(!(BP_IS_HOLE(bp)) &&
3255 db->db_blkptr == &dn->dn_phys->dn_spill);
3259 if (db->db_level == 0) {
3260 mutex_enter(&dn->dn_mtx);
3261 if (db->db_blkid > dn->dn_phys->dn_maxblkid &&
3262 db->db_blkid != DMU_SPILL_BLKID)
3263 dn->dn_phys->dn_maxblkid = db->db_blkid;
3264 mutex_exit(&dn->dn_mtx);
3266 if (dn->dn_type == DMU_OT_DNODE) {
3267 dnode_phys_t *dnp = db->db.db_data;
3268 for (i = db->db.db_size >> DNODE_SHIFT; i > 0;
3270 if (dnp->dn_type != DMU_OT_NONE)
3274 if (BP_IS_HOLE(bp)) {
3281 blkptr_t *ibp = db->db.db_data;
3282 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
3283 for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) {
3284 if (BP_IS_HOLE(ibp))
3286 fill += BP_GET_FILL(ibp);
3291 if (!BP_IS_EMBEDDED(bp))
3292 bp->blk_fill = fill;
3294 mutex_exit(&db->db_mtx);
3296 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
3297 *db->db_blkptr = *bp;
3298 rw_exit(&dn->dn_struct_rwlock);
3303 * This function gets called just prior to running through the compression
3304 * stage of the zio pipeline. If we're an indirect block comprised of only
3305 * holes, then we want this indirect to be compressed away to a hole. In
3306 * order to do that we must zero out any information about the holes that
3307 * this indirect points to prior to before we try to compress it.
3310 dbuf_write_children_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
3312 dmu_buf_impl_t *db = vdb;
3315 unsigned int epbs, i;
3317 ASSERT3U(db->db_level, >, 0);
3320 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
3321 ASSERT3U(epbs, <, 31);
3323 /* Determine if all our children are holes */
3324 for (i = 0, bp = db->db.db_data; i < 1 << epbs; i++, bp++) {
3325 if (!BP_IS_HOLE(bp))
3330 * If all the children are holes, then zero them all out so that
3331 * we may get compressed away.
3333 if (i == 1 << epbs) {
3335 * We only found holes. Grab the rwlock to prevent
3336 * anybody from reading the blocks we're about to
3339 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
3340 bzero(db->db.db_data, db->db.db_size);
3341 rw_exit(&dn->dn_struct_rwlock);
3347 * The SPA will call this callback several times for each zio - once
3348 * for every physical child i/o (zio->io_phys_children times). This
3349 * allows the DMU to monitor the progress of each logical i/o. For example,
3350 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
3351 * block. There may be a long delay before all copies/fragments are completed,
3352 * so this callback allows us to retire dirty space gradually, as the physical
3357 dbuf_write_physdone(zio_t *zio, arc_buf_t *buf, void *arg)
3359 dmu_buf_impl_t *db = arg;
3360 objset_t *os = db->db_objset;
3361 dsl_pool_t *dp = dmu_objset_pool(os);
3362 dbuf_dirty_record_t *dr;
3365 dr = db->db_data_pending;
3366 ASSERT3U(dr->dr_txg, ==, zio->io_txg);
3369 * The callback will be called io_phys_children times. Retire one
3370 * portion of our dirty space each time we are called. Any rounding
3371 * error will be cleaned up by dsl_pool_sync()'s call to
3372 * dsl_pool_undirty_space().
3374 delta = dr->dr_accounted / zio->io_phys_children;
3375 dsl_pool_undirty_space(dp, delta, zio->io_txg);
3380 dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
3382 dmu_buf_impl_t *db = vdb;
3383 blkptr_t *bp_orig = &zio->io_bp_orig;
3384 blkptr_t *bp = db->db_blkptr;
3385 objset_t *os = db->db_objset;
3386 dmu_tx_t *tx = os->os_synctx;
3387 dbuf_dirty_record_t **drp, *dr;
3389 ASSERT0(zio->io_error);
3390 ASSERT(db->db_blkptr == bp);
3393 * For nopwrites and rewrites we ensure that the bp matches our
3394 * original and bypass all the accounting.
3396 if (zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)) {
3397 ASSERT(BP_EQUAL(bp, bp_orig));
3399 dsl_dataset_t *ds = os->os_dsl_dataset;
3400 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
3401 dsl_dataset_block_born(ds, bp, tx);
3404 mutex_enter(&db->db_mtx);
3408 drp = &db->db_last_dirty;
3409 while ((dr = *drp) != db->db_data_pending)
3411 ASSERT(!list_link_active(&dr->dr_dirty_node));
3412 ASSERT(dr->dr_dbuf == db);
3413 ASSERT(dr->dr_next == NULL);
3417 if (db->db_blkid == DMU_SPILL_BLKID) {
3422 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
3423 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
3424 db->db_blkptr == &dn->dn_phys->dn_spill);
3429 if (db->db_level == 0) {
3430 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
3431 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
3432 if (db->db_state != DB_NOFILL) {
3433 if (dr->dt.dl.dr_data != db->db_buf)
3434 arc_buf_destroy(dr->dt.dl.dr_data, db);
3441 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
3442 ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
3443 if (!BP_IS_HOLE(db->db_blkptr)) {
3445 dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
3446 ASSERT3U(db->db_blkid, <=,
3447 dn->dn_phys->dn_maxblkid >> (db->db_level * epbs));
3448 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
3452 mutex_destroy(&dr->dt.di.dr_mtx);
3453 list_destroy(&dr->dt.di.dr_children);
3455 kmem_free(dr, sizeof (dbuf_dirty_record_t));
3457 cv_broadcast(&db->db_changed);
3458 ASSERT(db->db_dirtycnt > 0);
3459 db->db_dirtycnt -= 1;
3460 db->db_data_pending = NULL;
3461 dbuf_rele_and_unlock(db, (void *)(uintptr_t)tx->tx_txg);
3465 dbuf_write_nofill_ready(zio_t *zio)
3467 dbuf_write_ready(zio, NULL, zio->io_private);
3471 dbuf_write_nofill_done(zio_t *zio)
3473 dbuf_write_done(zio, NULL, zio->io_private);
3477 dbuf_write_override_ready(zio_t *zio)
3479 dbuf_dirty_record_t *dr = zio->io_private;
3480 dmu_buf_impl_t *db = dr->dr_dbuf;
3482 dbuf_write_ready(zio, NULL, db);
3486 dbuf_write_override_done(zio_t *zio)
3488 dbuf_dirty_record_t *dr = zio->io_private;
3489 dmu_buf_impl_t *db = dr->dr_dbuf;
3490 blkptr_t *obp = &dr->dt.dl.dr_overridden_by;
3492 mutex_enter(&db->db_mtx);
3493 if (!BP_EQUAL(zio->io_bp, obp)) {
3494 if (!BP_IS_HOLE(obp))
3495 dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp);
3496 arc_release(dr->dt.dl.dr_data, db);
3498 mutex_exit(&db->db_mtx);
3499 dbuf_write_done(zio, NULL, db);
3501 if (zio->io_abd != NULL)
3502 abd_put(zio->io_abd);
3505 typedef struct dbuf_remap_impl_callback_arg {
3507 uint64_t drica_blk_birth;
3509 } dbuf_remap_impl_callback_arg_t;
3512 dbuf_remap_impl_callback(uint64_t vdev, uint64_t offset, uint64_t size,
3515 dbuf_remap_impl_callback_arg_t *drica = arg;
3516 objset_t *os = drica->drica_os;
3517 spa_t *spa = dmu_objset_spa(os);
3518 dmu_tx_t *tx = drica->drica_tx;
3520 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa)));
3522 if (os == spa_meta_objset(spa)) {
3523 spa_vdev_indirect_mark_obsolete(spa, vdev, offset, size, tx);
3525 dsl_dataset_block_remapped(dmu_objset_ds(os), vdev, offset,
3526 size, drica->drica_blk_birth, tx);
3531 dbuf_remap_impl(dnode_t *dn, blkptr_t *bp, dmu_tx_t *tx)
3533 blkptr_t bp_copy = *bp;
3534 spa_t *spa = dmu_objset_spa(dn->dn_objset);
3535 dbuf_remap_impl_callback_arg_t drica;
3537 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa)));
3539 drica.drica_os = dn->dn_objset;
3540 drica.drica_blk_birth = bp->blk_birth;
3541 drica.drica_tx = tx;
3542 if (spa_remap_blkptr(spa, &bp_copy, dbuf_remap_impl_callback,
3545 * The struct_rwlock prevents dbuf_read_impl() from
3546 * dereferencing the BP while we are changing it. To
3547 * avoid lock contention, only grab it when we are actually
3550 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
3552 rw_exit(&dn->dn_struct_rwlock);
3557 * Returns true if a dbuf_remap would modify the dbuf. We do this by attempting
3558 * to remap a copy of every bp in the dbuf.
3561 dbuf_can_remap(const dmu_buf_impl_t *db)
3563 spa_t *spa = dmu_objset_spa(db->db_objset);
3564 blkptr_t *bp = db->db.db_data;
3565 boolean_t ret = B_FALSE;
3567 ASSERT3U(db->db_level, >, 0);
3568 ASSERT3S(db->db_state, ==, DB_CACHED);
3570 ASSERT(spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL));
3572 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3573 for (int i = 0; i < db->db.db_size >> SPA_BLKPTRSHIFT; i++) {
3574 blkptr_t bp_copy = bp[i];
3575 if (spa_remap_blkptr(spa, &bp_copy, NULL, NULL)) {
3580 spa_config_exit(spa, SCL_VDEV, FTAG);
3586 dnode_needs_remap(const dnode_t *dn)
3588 spa_t *spa = dmu_objset_spa(dn->dn_objset);
3589 boolean_t ret = B_FALSE;
3591 if (dn->dn_phys->dn_nlevels == 0) {
3595 ASSERT(spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL));
3597 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3598 for (int j = 0; j < dn->dn_phys->dn_nblkptr; j++) {
3599 blkptr_t bp_copy = dn->dn_phys->dn_blkptr[j];
3600 if (spa_remap_blkptr(spa, &bp_copy, NULL, NULL)) {
3605 spa_config_exit(spa, SCL_VDEV, FTAG);
3611 * Remap any existing BP's to concrete vdevs, if possible.
3614 dbuf_remap(dnode_t *dn, dmu_buf_impl_t *db, dmu_tx_t *tx)
3616 spa_t *spa = dmu_objset_spa(db->db_objset);
3617 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa)));
3619 if (!spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL))
3622 if (db->db_level > 0) {
3623 blkptr_t *bp = db->db.db_data;
3624 for (int i = 0; i < db->db.db_size >> SPA_BLKPTRSHIFT; i++) {
3625 dbuf_remap_impl(dn, &bp[i], tx);
3627 } else if (db->db.db_object == DMU_META_DNODE_OBJECT) {
3628 dnode_phys_t *dnp = db->db.db_data;
3629 ASSERT3U(db->db_dnode_handle->dnh_dnode->dn_type, ==,
3631 for (int i = 0; i < db->db.db_size >> DNODE_SHIFT; i++) {
3632 for (int j = 0; j < dnp[i].dn_nblkptr; j++) {
3633 dbuf_remap_impl(dn, &dnp[i].dn_blkptr[j], tx);
3640 /* Issue I/O to commit a dirty buffer to disk. */
3642 dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx)
3644 dmu_buf_impl_t *db = dr->dr_dbuf;
3647 dmu_buf_impl_t *parent = db->db_parent;
3648 uint64_t txg = tx->tx_txg;
3649 zbookmark_phys_t zb;
3654 ASSERT(dmu_tx_is_syncing(tx));
3660 if (db->db_state != DB_NOFILL) {
3661 if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) {
3663 * Private object buffers are released here rather
3664 * than in dbuf_dirty() since they are only modified
3665 * in the syncing context and we don't want the
3666 * overhead of making multiple copies of the data.
3668 if (BP_IS_HOLE(db->db_blkptr)) {
3671 dbuf_release_bp(db);
3673 dbuf_remap(dn, db, tx);
3677 if (parent != dn->dn_dbuf) {
3678 /* Our parent is an indirect block. */
3679 /* We have a dirty parent that has been scheduled for write. */
3680 ASSERT(parent && parent->db_data_pending);
3681 /* Our parent's buffer is one level closer to the dnode. */
3682 ASSERT(db->db_level == parent->db_level-1);
3684 * We're about to modify our parent's db_data by modifying
3685 * our block pointer, so the parent must be released.
3687 ASSERT(arc_released(parent->db_buf));
3688 zio = parent->db_data_pending->dr_zio;
3690 /* Our parent is the dnode itself. */
3691 ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 &&
3692 db->db_blkid != DMU_SPILL_BLKID) ||
3693 (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0));
3694 if (db->db_blkid != DMU_SPILL_BLKID)
3695 ASSERT3P(db->db_blkptr, ==,
3696 &dn->dn_phys->dn_blkptr[db->db_blkid]);
3700 ASSERT(db->db_level == 0 || data == db->db_buf);
3701 ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
3704 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
3705 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
3706 db->db.db_object, db->db_level, db->db_blkid);
3708 if (db->db_blkid == DMU_SPILL_BLKID)
3710 wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
3712 dmu_write_policy(os, dn, db->db_level, wp_flag, &zp);
3716 * We copy the blkptr now (rather than when we instantiate the dirty
3717 * record), because its value can change between open context and
3718 * syncing context. We do not need to hold dn_struct_rwlock to read
3719 * db_blkptr because we are in syncing context.
3721 dr->dr_bp_copy = *db->db_blkptr;
3723 if (db->db_level == 0 &&
3724 dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
3726 * The BP for this block has been provided by open context
3727 * (by dmu_sync() or dmu_buf_write_embedded()).
3729 abd_t *contents = (data != NULL) ?
3730 abd_get_from_buf(data->b_data, arc_buf_size(data)) : NULL;
3732 dr->dr_zio = zio_write(zio, os->os_spa, txg, &dr->dr_bp_copy,
3733 contents, db->db.db_size, db->db.db_size, &zp,
3734 dbuf_write_override_ready, NULL, NULL,
3735 dbuf_write_override_done,
3736 dr, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
3737 mutex_enter(&db->db_mtx);
3738 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
3739 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by,
3740 dr->dt.dl.dr_copies, dr->dt.dl.dr_nopwrite);
3741 mutex_exit(&db->db_mtx);
3742 } else if (db->db_state == DB_NOFILL) {
3743 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF ||
3744 zp.zp_checksum == ZIO_CHECKSUM_NOPARITY);
3745 dr->dr_zio = zio_write(zio, os->os_spa, txg,
3746 &dr->dr_bp_copy, NULL, db->db.db_size, db->db.db_size, &zp,
3747 dbuf_write_nofill_ready, NULL, NULL,
3748 dbuf_write_nofill_done, db,
3749 ZIO_PRIORITY_ASYNC_WRITE,
3750 ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb);
3752 ASSERT(arc_released(data));
3755 * For indirect blocks, we want to setup the children
3756 * ready callback so that we can properly handle an indirect
3757 * block that only contains holes.
3759 arc_write_done_func_t *children_ready_cb = NULL;
3760 if (db->db_level != 0)
3761 children_ready_cb = dbuf_write_children_ready;
3763 dr->dr_zio = arc_write(zio, os->os_spa, txg,
3764 &dr->dr_bp_copy, data, DBUF_IS_L2CACHEABLE(db),
3765 &zp, dbuf_write_ready, children_ready_cb,
3766 dbuf_write_physdone, dbuf_write_done, db,
3767 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);