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 uint_t zfs_dbuf_evict_key;
53 static boolean_t dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
54 static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx);
57 extern inline void dmu_buf_init_user(dmu_buf_user_t *dbu,
58 dmu_buf_evict_func_t *evict_func_sync,
59 dmu_buf_evict_func_t *evict_func_async,
60 dmu_buf_t **clear_on_evict_dbufp);
64 * Global data structures and functions for the dbuf cache.
66 static kmem_cache_t *dbuf_kmem_cache;
67 static taskq_t *dbu_evict_taskq;
69 static kthread_t *dbuf_cache_evict_thread;
70 static kmutex_t dbuf_evict_lock;
71 static kcondvar_t dbuf_evict_cv;
72 static boolean_t dbuf_evict_thread_exit;
75 * LRU cache of dbufs. The dbuf cache maintains a list of dbufs that
76 * are not currently held but have been recently released. These dbufs
77 * are not eligible for arc eviction until they are aged out of the cache.
78 * Dbufs are added to the dbuf cache once the last hold is released. If a
79 * dbuf is later accessed and still exists in the dbuf cache, then it will
80 * be removed from the cache and later re-added to the head of the cache.
81 * Dbufs that are aged out of the cache will be immediately destroyed and
82 * become eligible for arc eviction.
84 static multilist_t *dbuf_cache;
85 static refcount_t dbuf_cache_size;
86 uint64_t dbuf_cache_max_bytes = 100 * 1024 * 1024;
88 /* Cap the size of the dbuf cache to log2 fraction of arc size. */
89 int dbuf_cache_max_shift = 5;
92 * The dbuf cache uses a three-stage eviction policy:
93 * - A low water marker designates when the dbuf eviction thread
94 * should stop evicting from the dbuf cache.
95 * - When we reach the maximum size (aka mid water mark), we
96 * signal the eviction thread to run.
97 * - The high water mark indicates when the eviction thread
98 * is unable to keep up with the incoming load and eviction must
99 * happen in the context of the calling thread.
103 * low water mid water hi water
104 * +----------------------------------------+----------+----------+
109 * +----------------------------------------+----------+----------+
111 * evicting eviction directly
114 * The high and low water marks indicate the operating range for the eviction
115 * thread. The low water mark is, by default, 90% of the total size of the
116 * cache and the high water mark is at 110% (both of these percentages can be
117 * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct,
118 * respectively). The eviction thread will try to ensure that the cache remains
119 * within this range by waking up every second and checking if the cache is
120 * above the low water mark. The thread can also be woken up by callers adding
121 * elements into the cache if the cache is larger than the mid water (i.e max
122 * cache size). Once the eviction thread is woken up and eviction is required,
123 * it will continue evicting buffers until it's able to reduce the cache size
124 * to the low water mark. If the cache size continues to grow and hits the high
125 * water mark, then callers adding elments to the cache will begin to evict
126 * directly from the cache until the cache is no longer above the high water
131 * The percentage above and below the maximum cache size.
133 uint_t dbuf_cache_hiwater_pct = 10;
134 uint_t dbuf_cache_lowater_pct = 10;
138 dbuf_cons(void *vdb, void *unused, int kmflag)
140 dmu_buf_impl_t *db = vdb;
141 bzero(db, sizeof (dmu_buf_impl_t));
143 mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL);
144 cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL);
145 multilist_link_init(&db->db_cache_link);
146 refcount_create(&db->db_holds);
153 dbuf_dest(void *vdb, void *unused)
155 dmu_buf_impl_t *db = vdb;
156 mutex_destroy(&db->db_mtx);
157 cv_destroy(&db->db_changed);
158 ASSERT(!multilist_link_active(&db->db_cache_link));
159 refcount_destroy(&db->db_holds);
163 * dbuf hash table routines
165 static dbuf_hash_table_t dbuf_hash_table;
167 static uint64_t dbuf_hash_count;
170 dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid)
172 uintptr_t osv = (uintptr_t)os;
173 uint64_t crc = -1ULL;
175 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
176 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (lvl)) & 0xFF];
177 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF];
178 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF];
179 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF];
180 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 0)) & 0xFF];
181 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 8)) & 0xFF];
183 crc ^= (osv>>14) ^ (obj>>16) ^ (blkid>>16);
188 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
189 ((dbuf)->db.db_object == (obj) && \
190 (dbuf)->db_objset == (os) && \
191 (dbuf)->db_level == (level) && \
192 (dbuf)->db_blkid == (blkid))
195 dbuf_find(objset_t *os, uint64_t obj, uint8_t level, uint64_t blkid)
197 dbuf_hash_table_t *h = &dbuf_hash_table;
198 uint64_t hv = dbuf_hash(os, obj, level, blkid);
199 uint64_t idx = hv & h->hash_table_mask;
202 mutex_enter(DBUF_HASH_MUTEX(h, idx));
203 for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) {
204 if (DBUF_EQUAL(db, os, obj, level, blkid)) {
205 mutex_enter(&db->db_mtx);
206 if (db->db_state != DB_EVICTING) {
207 mutex_exit(DBUF_HASH_MUTEX(h, idx));
210 mutex_exit(&db->db_mtx);
213 mutex_exit(DBUF_HASH_MUTEX(h, idx));
217 static dmu_buf_impl_t *
218 dbuf_find_bonus(objset_t *os, uint64_t object)
221 dmu_buf_impl_t *db = NULL;
223 if (dnode_hold(os, object, FTAG, &dn) == 0) {
224 rw_enter(&dn->dn_struct_rwlock, RW_READER);
225 if (dn->dn_bonus != NULL) {
227 mutex_enter(&db->db_mtx);
229 rw_exit(&dn->dn_struct_rwlock);
230 dnode_rele(dn, FTAG);
236 * Insert an entry into the hash table. If there is already an element
237 * equal to elem in the hash table, then the already existing element
238 * will be returned and the new element will not be inserted.
239 * Otherwise returns NULL.
241 static dmu_buf_impl_t *
242 dbuf_hash_insert(dmu_buf_impl_t *db)
244 dbuf_hash_table_t *h = &dbuf_hash_table;
245 objset_t *os = db->db_objset;
246 uint64_t obj = db->db.db_object;
247 int level = db->db_level;
248 uint64_t blkid = db->db_blkid;
249 uint64_t hv = dbuf_hash(os, obj, level, blkid);
250 uint64_t idx = hv & h->hash_table_mask;
253 mutex_enter(DBUF_HASH_MUTEX(h, idx));
254 for (dbf = h->hash_table[idx]; dbf != NULL; dbf = dbf->db_hash_next) {
255 if (DBUF_EQUAL(dbf, os, obj, level, blkid)) {
256 mutex_enter(&dbf->db_mtx);
257 if (dbf->db_state != DB_EVICTING) {
258 mutex_exit(DBUF_HASH_MUTEX(h, idx));
261 mutex_exit(&dbf->db_mtx);
265 mutex_enter(&db->db_mtx);
266 db->db_hash_next = h->hash_table[idx];
267 h->hash_table[idx] = db;
268 mutex_exit(DBUF_HASH_MUTEX(h, idx));
269 atomic_inc_64(&dbuf_hash_count);
275 * Remove an entry from the hash table. It must be in the EVICTING state.
278 dbuf_hash_remove(dmu_buf_impl_t *db)
280 dbuf_hash_table_t *h = &dbuf_hash_table;
281 uint64_t hv = dbuf_hash(db->db_objset, db->db.db_object,
282 db->db_level, db->db_blkid);
283 uint64_t idx = hv & h->hash_table_mask;
284 dmu_buf_impl_t *dbf, **dbp;
287 * We musn't hold db_mtx to maintain lock ordering:
288 * DBUF_HASH_MUTEX > db_mtx.
290 ASSERT(refcount_is_zero(&db->db_holds));
291 ASSERT(db->db_state == DB_EVICTING);
292 ASSERT(!MUTEX_HELD(&db->db_mtx));
294 mutex_enter(DBUF_HASH_MUTEX(h, idx));
295 dbp = &h->hash_table[idx];
296 while ((dbf = *dbp) != db) {
297 dbp = &dbf->db_hash_next;
300 *dbp = db->db_hash_next;
301 db->db_hash_next = NULL;
302 mutex_exit(DBUF_HASH_MUTEX(h, idx));
303 atomic_dec_64(&dbuf_hash_count);
309 } dbvu_verify_type_t;
312 dbuf_verify_user(dmu_buf_impl_t *db, dbvu_verify_type_t verify_type)
317 if (db->db_user == NULL)
320 /* Only data blocks support the attachment of user data. */
321 ASSERT(db->db_level == 0);
323 /* Clients must resolve a dbuf before attaching user data. */
324 ASSERT(db->db.db_data != NULL);
325 ASSERT3U(db->db_state, ==, DB_CACHED);
327 holds = refcount_count(&db->db_holds);
328 if (verify_type == DBVU_EVICTING) {
330 * Immediate eviction occurs when holds == dirtycnt.
331 * For normal eviction buffers, holds is zero on
332 * eviction, except when dbuf_fix_old_data() calls
333 * dbuf_clear_data(). However, the hold count can grow
334 * during eviction even though db_mtx is held (see
335 * dmu_bonus_hold() for an example), so we can only
336 * test the generic invariant that holds >= dirtycnt.
338 ASSERT3U(holds, >=, db->db_dirtycnt);
340 if (db->db_user_immediate_evict == TRUE)
341 ASSERT3U(holds, >=, db->db_dirtycnt);
343 ASSERT3U(holds, >, 0);
349 dbuf_evict_user(dmu_buf_impl_t *db)
351 dmu_buf_user_t *dbu = db->db_user;
353 ASSERT(MUTEX_HELD(&db->db_mtx));
358 dbuf_verify_user(db, DBVU_EVICTING);
362 if (dbu->dbu_clear_on_evict_dbufp != NULL)
363 *dbu->dbu_clear_on_evict_dbufp = NULL;
367 * There are two eviction callbacks - one that we call synchronously
368 * and one that we invoke via a taskq. The async one is useful for
369 * avoiding lock order reversals and limiting stack depth.
371 * Note that if we have a sync callback but no async callback,
372 * it's likely that the sync callback will free the structure
373 * containing the dbu. In that case we need to take care to not
374 * dereference dbu after calling the sync evict func.
376 boolean_t has_async = (dbu->dbu_evict_func_async != NULL);
378 if (dbu->dbu_evict_func_sync != NULL)
379 dbu->dbu_evict_func_sync(dbu);
382 taskq_dispatch_ent(dbu_evict_taskq, dbu->dbu_evict_func_async,
383 dbu, 0, &dbu->dbu_tqent);
388 dbuf_is_metadata(dmu_buf_impl_t *db)
390 if (db->db_level > 0) {
393 boolean_t is_metadata;
396 is_metadata = DMU_OT_IS_METADATA(DB_DNODE(db)->dn_type);
399 return (is_metadata);
404 * This function *must* return indices evenly distributed between all
405 * sublists of the multilist. This is needed due to how the dbuf eviction
406 * code is laid out; dbuf_evict_thread() assumes dbufs are evenly
407 * distributed between all sublists and uses this assumption when
408 * deciding which sublist to evict from and how much to evict from it.
411 dbuf_cache_multilist_index_func(multilist_t *ml, void *obj)
413 dmu_buf_impl_t *db = obj;
416 * The assumption here, is the hash value for a given
417 * dmu_buf_impl_t will remain constant throughout it's lifetime
418 * (i.e. it's objset, object, level and blkid fields don't change).
419 * Thus, we don't need to store the dbuf's sublist index
420 * on insertion, as this index can be recalculated on removal.
422 * Also, the low order bits of the hash value are thought to be
423 * distributed evenly. Otherwise, in the case that the multilist
424 * has a power of two number of sublists, each sublists' usage
425 * would not be evenly distributed.
427 return (dbuf_hash(db->db_objset, db->db.db_object,
428 db->db_level, db->db_blkid) %
429 multilist_get_num_sublists(ml));
432 static inline boolean_t
433 dbuf_cache_above_hiwater(void)
435 uint64_t dbuf_cache_hiwater_bytes =
436 (dbuf_cache_max_bytes * dbuf_cache_hiwater_pct) / 100;
438 return (refcount_count(&dbuf_cache_size) >
439 dbuf_cache_max_bytes + dbuf_cache_hiwater_bytes);
442 static inline boolean_t
443 dbuf_cache_above_lowater(void)
445 uint64_t dbuf_cache_lowater_bytes =
446 (dbuf_cache_max_bytes * dbuf_cache_lowater_pct) / 100;
448 return (refcount_count(&dbuf_cache_size) >
449 dbuf_cache_max_bytes - dbuf_cache_lowater_bytes);
453 * Evict the oldest eligible dbuf from the dbuf cache.
458 int idx = multilist_get_random_index(dbuf_cache);
459 multilist_sublist_t *mls = multilist_sublist_lock(dbuf_cache, idx);
461 ASSERT(!MUTEX_HELD(&dbuf_evict_lock));
464 * Set the thread's tsd to indicate that it's processing evictions.
465 * Once a thread stops evicting from the dbuf cache it will
466 * reset its tsd to NULL.
468 ASSERT3P(tsd_get(zfs_dbuf_evict_key), ==, NULL);
469 (void) tsd_set(zfs_dbuf_evict_key, (void *)B_TRUE);
471 dmu_buf_impl_t *db = multilist_sublist_tail(mls);
472 while (db != NULL && mutex_tryenter(&db->db_mtx) == 0) {
473 db = multilist_sublist_prev(mls, db);
476 DTRACE_PROBE2(dbuf__evict__one, dmu_buf_impl_t *, db,
477 multilist_sublist_t *, mls);
480 multilist_sublist_remove(mls, db);
481 multilist_sublist_unlock(mls);
482 (void) refcount_remove_many(&dbuf_cache_size,
486 multilist_sublist_unlock(mls);
488 (void) tsd_set(zfs_dbuf_evict_key, NULL);
492 * The dbuf evict thread is responsible for aging out dbufs from the
493 * cache. Once the cache has reached it's maximum size, dbufs are removed
494 * and destroyed. The eviction thread will continue running until the size
495 * of the dbuf cache is at or below the maximum size. Once the dbuf is aged
496 * out of the cache it is destroyed and becomes eligible for arc eviction.
499 dbuf_evict_thread(void *dummy __unused)
503 CALLB_CPR_INIT(&cpr, &dbuf_evict_lock, callb_generic_cpr, FTAG);
505 mutex_enter(&dbuf_evict_lock);
506 while (!dbuf_evict_thread_exit) {
507 while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit) {
508 CALLB_CPR_SAFE_BEGIN(&cpr);
509 (void) cv_timedwait_hires(&dbuf_evict_cv,
510 &dbuf_evict_lock, SEC2NSEC(1), MSEC2NSEC(1), 0);
511 CALLB_CPR_SAFE_END(&cpr, &dbuf_evict_lock);
513 mutex_exit(&dbuf_evict_lock);
516 * Keep evicting as long as we're above the low water mark
517 * for the cache. We do this without holding the locks to
518 * minimize lock contention.
520 while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit) {
524 mutex_enter(&dbuf_evict_lock);
527 dbuf_evict_thread_exit = B_FALSE;
528 cv_broadcast(&dbuf_evict_cv);
529 CALLB_CPR_EXIT(&cpr); /* drops dbuf_evict_lock */
534 * Wake up the dbuf eviction thread if the dbuf cache is at its max size.
535 * If the dbuf cache is at its high water mark, then evict a dbuf from the
536 * dbuf cache using the callers context.
539 dbuf_evict_notify(void)
543 * We use thread specific data to track when a thread has
544 * started processing evictions. This allows us to avoid deeply
545 * nested stacks that would have a call flow similar to this:
547 * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify()
550 * +-----dbuf_destroy()<--dbuf_evict_one()<--------+
552 * The dbuf_eviction_thread will always have its tsd set until
553 * that thread exits. All other threads will only set their tsd
554 * if they are participating in the eviction process. This only
555 * happens if the eviction thread is unable to process evictions
556 * fast enough. To keep the dbuf cache size in check, other threads
557 * can evict from the dbuf cache directly. Those threads will set
558 * their tsd values so that we ensure that they only evict one dbuf
559 * from the dbuf cache.
561 if (tsd_get(zfs_dbuf_evict_key) != NULL)
565 * We check if we should evict without holding the dbuf_evict_lock,
566 * because it's OK to occasionally make the wrong decision here,
567 * and grabbing the lock results in massive lock contention.
569 if (refcount_count(&dbuf_cache_size) > dbuf_cache_max_bytes) {
570 if (dbuf_cache_above_hiwater())
572 cv_signal(&dbuf_evict_cv);
579 uint64_t hsize = 1ULL << 16;
580 dbuf_hash_table_t *h = &dbuf_hash_table;
584 * The hash table is big enough to fill all of physical memory
585 * with an average 4K block size. The table will take up
586 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
588 while (hsize * 4096 < (uint64_t)physmem * PAGESIZE)
592 h->hash_table_mask = hsize - 1;
593 h->hash_table = kmem_zalloc(hsize * sizeof (void *), KM_NOSLEEP);
594 if (h->hash_table == NULL) {
595 /* XXX - we should really return an error instead of assert */
596 ASSERT(hsize > (1ULL << 10));
601 dbuf_kmem_cache = kmem_cache_create("dmu_buf_impl_t",
602 sizeof (dmu_buf_impl_t),
603 0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0);
605 for (i = 0; i < DBUF_MUTEXES; i++)
606 mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL);
609 * Setup the parameters for the dbuf cache. We cap the size of the
610 * dbuf cache to 1/32nd (default) of the size of the ARC.
612 dbuf_cache_max_bytes = MIN(dbuf_cache_max_bytes,
613 arc_max_bytes() >> dbuf_cache_max_shift);
616 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
617 * configuration is not required.
619 dbu_evict_taskq = taskq_create("dbu_evict", 1, minclsyspri, 0, 0, 0);
621 dbuf_cache = multilist_create(sizeof (dmu_buf_impl_t),
622 offsetof(dmu_buf_impl_t, db_cache_link),
623 dbuf_cache_multilist_index_func);
624 refcount_create(&dbuf_cache_size);
626 tsd_create(&zfs_dbuf_evict_key, NULL);
627 dbuf_evict_thread_exit = B_FALSE;
628 mutex_init(&dbuf_evict_lock, NULL, MUTEX_DEFAULT, NULL);
629 cv_init(&dbuf_evict_cv, NULL, CV_DEFAULT, NULL);
630 dbuf_cache_evict_thread = thread_create(NULL, 0, dbuf_evict_thread,
631 NULL, 0, &p0, TS_RUN, minclsyspri);
637 dbuf_hash_table_t *h = &dbuf_hash_table;
640 for (i = 0; i < DBUF_MUTEXES; i++)
641 mutex_destroy(&h->hash_mutexes[i]);
642 kmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
643 kmem_cache_destroy(dbuf_kmem_cache);
644 taskq_destroy(dbu_evict_taskq);
646 mutex_enter(&dbuf_evict_lock);
647 dbuf_evict_thread_exit = B_TRUE;
648 while (dbuf_evict_thread_exit) {
649 cv_signal(&dbuf_evict_cv);
650 cv_wait(&dbuf_evict_cv, &dbuf_evict_lock);
652 mutex_exit(&dbuf_evict_lock);
653 tsd_destroy(&zfs_dbuf_evict_key);
655 mutex_destroy(&dbuf_evict_lock);
656 cv_destroy(&dbuf_evict_cv);
658 refcount_destroy(&dbuf_cache_size);
659 multilist_destroy(dbuf_cache);
668 dbuf_verify(dmu_buf_impl_t *db)
671 dbuf_dirty_record_t *dr;
673 ASSERT(MUTEX_HELD(&db->db_mtx));
675 if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY))
678 ASSERT(db->db_objset != NULL);
682 ASSERT(db->db_parent == NULL);
683 ASSERT(db->db_blkptr == NULL);
685 ASSERT3U(db->db.db_object, ==, dn->dn_object);
686 ASSERT3P(db->db_objset, ==, dn->dn_objset);
687 ASSERT3U(db->db_level, <, dn->dn_nlevels);
688 ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
689 db->db_blkid == DMU_SPILL_BLKID ||
690 !avl_is_empty(&dn->dn_dbufs));
692 if (db->db_blkid == DMU_BONUS_BLKID) {
694 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
695 ASSERT3U(db->db.db_offset, ==, DMU_BONUS_BLKID);
696 } else if (db->db_blkid == DMU_SPILL_BLKID) {
698 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
699 ASSERT0(db->db.db_offset);
701 ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size);
704 for (dr = db->db_data_pending; dr != NULL; dr = dr->dr_next)
705 ASSERT(dr->dr_dbuf == db);
707 for (dr = db->db_last_dirty; dr != NULL; dr = dr->dr_next)
708 ASSERT(dr->dr_dbuf == db);
711 * We can't assert that db_size matches dn_datablksz because it
712 * can be momentarily different when another thread is doing
715 if (db->db_level == 0 && db->db.db_object == DMU_META_DNODE_OBJECT) {
716 dr = db->db_data_pending;
718 * It should only be modified in syncing context, so
719 * make sure we only have one copy of the data.
721 ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf);
724 /* verify db->db_blkptr */
726 if (db->db_parent == dn->dn_dbuf) {
727 /* db is pointed to by the dnode */
728 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
729 if (DMU_OBJECT_IS_SPECIAL(db->db.db_object))
730 ASSERT(db->db_parent == NULL);
732 ASSERT(db->db_parent != NULL);
733 if (db->db_blkid != DMU_SPILL_BLKID)
734 ASSERT3P(db->db_blkptr, ==,
735 &dn->dn_phys->dn_blkptr[db->db_blkid]);
737 /* db is pointed to by an indirect block */
738 int epb = db->db_parent->db.db_size >> SPA_BLKPTRSHIFT;
739 ASSERT3U(db->db_parent->db_level, ==, db->db_level+1);
740 ASSERT3U(db->db_parent->db.db_object, ==,
743 * dnode_grow_indblksz() can make this fail if we don't
744 * have the struct_rwlock. XXX indblksz no longer
745 * grows. safe to do this now?
747 if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
748 ASSERT3P(db->db_blkptr, ==,
749 ((blkptr_t *)db->db_parent->db.db_data +
750 db->db_blkid % epb));
754 if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) &&
755 (db->db_buf == NULL || db->db_buf->b_data) &&
756 db->db.db_data && db->db_blkid != DMU_BONUS_BLKID &&
757 db->db_state != DB_FILL && !dn->dn_free_txg) {
759 * If the blkptr isn't set but they have nonzero data,
760 * it had better be dirty, otherwise we'll lose that
761 * data when we evict this buffer.
763 * There is an exception to this rule for indirect blocks; in
764 * this case, if the indirect block is a hole, we fill in a few
765 * fields on each of the child blocks (importantly, birth time)
766 * to prevent hole birth times from being lost when you
767 * partially fill in a hole.
769 if (db->db_dirtycnt == 0) {
770 if (db->db_level == 0) {
771 uint64_t *buf = db->db.db_data;
774 for (i = 0; i < db->db.db_size >> 3; i++) {
778 blkptr_t *bps = db->db.db_data;
779 ASSERT3U(1 << DB_DNODE(db)->dn_indblkshift, ==,
782 * We want to verify that all the blkptrs in the
783 * indirect block are holes, but we may have
784 * automatically set up a few fields for them.
785 * We iterate through each blkptr and verify
786 * they only have those fields set.
789 i < db->db.db_size / sizeof (blkptr_t);
791 blkptr_t *bp = &bps[i];
792 ASSERT(ZIO_CHECKSUM_IS_ZERO(
795 DVA_IS_EMPTY(&bp->blk_dva[0]) &&
796 DVA_IS_EMPTY(&bp->blk_dva[1]) &&
797 DVA_IS_EMPTY(&bp->blk_dva[2]));
798 ASSERT0(bp->blk_fill);
799 ASSERT0(bp->blk_pad[0]);
800 ASSERT0(bp->blk_pad[1]);
801 ASSERT(!BP_IS_EMBEDDED(bp));
802 ASSERT(BP_IS_HOLE(bp));
803 ASSERT0(bp->blk_phys_birth);
813 dbuf_clear_data(dmu_buf_impl_t *db)
815 ASSERT(MUTEX_HELD(&db->db_mtx));
817 ASSERT3P(db->db_buf, ==, NULL);
818 db->db.db_data = NULL;
819 if (db->db_state != DB_NOFILL)
820 db->db_state = DB_UNCACHED;
824 dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf)
826 ASSERT(MUTEX_HELD(&db->db_mtx));
830 ASSERT(buf->b_data != NULL);
831 db->db.db_data = buf->b_data;
835 * Loan out an arc_buf for read. Return the loaned arc_buf.
838 dbuf_loan_arcbuf(dmu_buf_impl_t *db)
842 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
843 mutex_enter(&db->db_mtx);
844 if (arc_released(db->db_buf) || refcount_count(&db->db_holds) > 1) {
845 int blksz = db->db.db_size;
846 spa_t *spa = db->db_objset->os_spa;
848 mutex_exit(&db->db_mtx);
849 abuf = arc_loan_buf(spa, B_FALSE, blksz);
850 bcopy(db->db.db_data, abuf->b_data, blksz);
853 arc_loan_inuse_buf(abuf, db);
856 mutex_exit(&db->db_mtx);
862 * Calculate which level n block references the data at the level 0 offset
866 dbuf_whichblock(dnode_t *dn, int64_t level, uint64_t offset)
868 if (dn->dn_datablkshift != 0 && dn->dn_indblkshift != 0) {
870 * The level n blkid is equal to the level 0 blkid divided by
871 * the number of level 0s in a level n block.
873 * The level 0 blkid is offset >> datablkshift =
874 * offset / 2^datablkshift.
876 * The number of level 0s in a level n is the number of block
877 * pointers in an indirect block, raised to the power of level.
878 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
879 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
881 * Thus, the level n blkid is: offset /
882 * ((2^datablkshift)*(2^(level*(indblkshift - SPA_BLKPTRSHIFT)))
883 * = offset / 2^(datablkshift + level *
884 * (indblkshift - SPA_BLKPTRSHIFT))
885 * = offset >> (datablkshift + level *
886 * (indblkshift - SPA_BLKPTRSHIFT))
888 return (offset >> (dn->dn_datablkshift + level *
889 (dn->dn_indblkshift - SPA_BLKPTRSHIFT)));
891 ASSERT3U(offset, <, dn->dn_datablksz);
897 dbuf_read_done(zio_t *zio, arc_buf_t *buf, void *vdb)
899 dmu_buf_impl_t *db = vdb;
901 mutex_enter(&db->db_mtx);
902 ASSERT3U(db->db_state, ==, DB_READ);
904 * All reads are synchronous, so we must have a hold on the dbuf
906 ASSERT(refcount_count(&db->db_holds) > 0);
907 ASSERT(db->db_buf == NULL);
908 ASSERT(db->db.db_data == NULL);
909 if (db->db_level == 0 && db->db_freed_in_flight) {
910 /* we were freed in flight; disregard any error */
911 arc_release(buf, db);
912 bzero(buf->b_data, db->db.db_size);
914 db->db_freed_in_flight = FALSE;
915 dbuf_set_data(db, buf);
916 db->db_state = DB_CACHED;
917 } else if (zio == NULL || zio->io_error == 0) {
918 dbuf_set_data(db, buf);
919 db->db_state = DB_CACHED;
921 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
922 ASSERT3P(db->db_buf, ==, NULL);
923 arc_buf_destroy(buf, db);
924 db->db_state = DB_UNCACHED;
926 cv_broadcast(&db->db_changed);
927 dbuf_rele_and_unlock(db, NULL);
931 dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
935 arc_flags_t aflags = ARC_FLAG_NOWAIT;
939 ASSERT(!refcount_is_zero(&db->db_holds));
940 /* We need the struct_rwlock to prevent db_blkptr from changing. */
941 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
942 ASSERT(MUTEX_HELD(&db->db_mtx));
943 ASSERT(db->db_state == DB_UNCACHED);
944 ASSERT(db->db_buf == NULL);
946 if (db->db_blkid == DMU_BONUS_BLKID) {
947 int bonuslen = MIN(dn->dn_bonuslen, dn->dn_phys->dn_bonuslen);
949 ASSERT3U(bonuslen, <=, db->db.db_size);
950 db->db.db_data = zio_buf_alloc(DN_MAX_BONUSLEN);
951 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
952 if (bonuslen < DN_MAX_BONUSLEN)
953 bzero(db->db.db_data, DN_MAX_BONUSLEN);
955 bcopy(DN_BONUS(dn->dn_phys), db->db.db_data, bonuslen);
957 db->db_state = DB_CACHED;
958 mutex_exit(&db->db_mtx);
963 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
964 * processes the delete record and clears the bp while we are waiting
965 * for the dn_mtx (resulting in a "no" from block_freed).
967 if (db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr) ||
968 (db->db_level == 0 && (dnode_block_freed(dn, db->db_blkid) ||
969 BP_IS_HOLE(db->db_blkptr)))) {
970 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
972 dbuf_set_data(db, arc_alloc_buf(db->db_objset->os_spa, db, type,
974 bzero(db->db.db_data, db->db.db_size);
976 if (db->db_blkptr != NULL && db->db_level > 0 &&
977 BP_IS_HOLE(db->db_blkptr) &&
978 db->db_blkptr->blk_birth != 0) {
979 blkptr_t *bps = db->db.db_data;
980 for (int i = 0; i < ((1 <<
981 DB_DNODE(db)->dn_indblkshift) / sizeof (blkptr_t));
983 blkptr_t *bp = &bps[i];
984 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
985 1 << dn->dn_indblkshift);
987 BP_GET_LEVEL(db->db_blkptr) == 1 ?
989 BP_GET_LSIZE(db->db_blkptr));
990 BP_SET_TYPE(bp, BP_GET_TYPE(db->db_blkptr));
992 BP_GET_LEVEL(db->db_blkptr) - 1);
993 BP_SET_BIRTH(bp, db->db_blkptr->blk_birth, 0);
997 db->db_state = DB_CACHED;
998 mutex_exit(&db->db_mtx);
1004 db->db_state = DB_READ;
1005 mutex_exit(&db->db_mtx);
1007 if (DBUF_IS_L2CACHEABLE(db))
1008 aflags |= ARC_FLAG_L2CACHE;
1010 SET_BOOKMARK(&zb, db->db_objset->os_dsl_dataset ?
1011 db->db_objset->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1012 db->db.db_object, db->db_level, db->db_blkid);
1014 dbuf_add_ref(db, NULL);
1016 (void) arc_read(zio, db->db_objset->os_spa, db->db_blkptr,
1017 dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ,
1018 (flags & DB_RF_CANFAIL) ? ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED,
1023 * This is our just-in-time copy function. It makes a copy of buffers that
1024 * have been modified in a previous transaction group before we access them in
1025 * the current active group.
1027 * This function is used in three places: when we are dirtying a buffer for the
1028 * first time in a txg, when we are freeing a range in a dnode that includes
1029 * this buffer, and when we are accessing a buffer which was received compressed
1030 * and later referenced in a WRITE_BYREF record.
1032 * Note that when we are called from dbuf_free_range() we do not put a hold on
1033 * the buffer, we just traverse the active dbuf list for the dnode.
1036 dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg)
1038 dbuf_dirty_record_t *dr = db->db_last_dirty;
1040 ASSERT(MUTEX_HELD(&db->db_mtx));
1041 ASSERT(db->db.db_data != NULL);
1042 ASSERT(db->db_level == 0);
1043 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT);
1046 (dr->dt.dl.dr_data !=
1047 ((db->db_blkid == DMU_BONUS_BLKID) ? db->db.db_data : db->db_buf)))
1051 * If the last dirty record for this dbuf has not yet synced
1052 * and its referencing the dbuf data, either:
1053 * reset the reference to point to a new copy,
1054 * or (if there a no active holders)
1055 * just null out the current db_data pointer.
1057 ASSERT(dr->dr_txg >= txg - 2);
1058 if (db->db_blkid == DMU_BONUS_BLKID) {
1059 /* Note that the data bufs here are zio_bufs */
1060 dr->dt.dl.dr_data = zio_buf_alloc(DN_MAX_BONUSLEN);
1061 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
1062 bcopy(db->db.db_data, dr->dt.dl.dr_data, DN_MAX_BONUSLEN);
1063 } else if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
1064 int size = arc_buf_size(db->db_buf);
1065 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1066 spa_t *spa = db->db_objset->os_spa;
1067 enum zio_compress compress_type =
1068 arc_get_compression(db->db_buf);
1070 if (compress_type == ZIO_COMPRESS_OFF) {
1071 dr->dt.dl.dr_data = arc_alloc_buf(spa, db, type, size);
1073 ASSERT3U(type, ==, ARC_BUFC_DATA);
1074 dr->dt.dl.dr_data = arc_alloc_compressed_buf(spa, db,
1075 size, arc_buf_lsize(db->db_buf), compress_type);
1077 bcopy(db->db.db_data, dr->dt.dl.dr_data->b_data, size);
1080 dbuf_clear_data(db);
1085 dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
1092 * We don't have to hold the mutex to check db_state because it
1093 * can't be freed while we have a hold on the buffer.
1095 ASSERT(!refcount_is_zero(&db->db_holds));
1097 if (db->db_state == DB_NOFILL)
1098 return (SET_ERROR(EIO));
1102 if ((flags & DB_RF_HAVESTRUCT) == 0)
1103 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1105 prefetch = db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1106 (flags & DB_RF_NOPREFETCH) == 0 && dn != NULL &&
1107 DBUF_IS_CACHEABLE(db);
1109 mutex_enter(&db->db_mtx);
1110 if (db->db_state == DB_CACHED) {
1112 * If the arc buf is compressed, we need to decompress it to
1113 * read the data. This could happen during the "zfs receive" of
1114 * a stream which is compressed and deduplicated.
1116 if (db->db_buf != NULL &&
1117 arc_get_compression(db->db_buf) != ZIO_COMPRESS_OFF) {
1118 dbuf_fix_old_data(db,
1119 spa_syncing_txg(dmu_objset_spa(db->db_objset)));
1120 err = arc_decompress(db->db_buf);
1121 dbuf_set_data(db, db->db_buf);
1123 mutex_exit(&db->db_mtx);
1125 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE);
1126 if ((flags & DB_RF_HAVESTRUCT) == 0)
1127 rw_exit(&dn->dn_struct_rwlock);
1129 } else if (db->db_state == DB_UNCACHED) {
1130 spa_t *spa = dn->dn_objset->os_spa;
1131 boolean_t need_wait = B_FALSE;
1134 db->db_blkptr != NULL && !BP_IS_HOLE(db->db_blkptr)) {
1135 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
1138 dbuf_read_impl(db, zio, flags);
1140 /* dbuf_read_impl has dropped db_mtx for us */
1143 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE);
1145 if ((flags & DB_RF_HAVESTRUCT) == 0)
1146 rw_exit(&dn->dn_struct_rwlock);
1150 err = zio_wait(zio);
1153 * Another reader came in while the dbuf was in flight
1154 * between UNCACHED and CACHED. Either a writer will finish
1155 * writing the buffer (sending the dbuf to CACHED) or the
1156 * first reader's request will reach the read_done callback
1157 * and send the dbuf to CACHED. Otherwise, a failure
1158 * occurred and the dbuf went to UNCACHED.
1160 mutex_exit(&db->db_mtx);
1162 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE);
1163 if ((flags & DB_RF_HAVESTRUCT) == 0)
1164 rw_exit(&dn->dn_struct_rwlock);
1167 /* Skip the wait per the caller's request. */
1168 mutex_enter(&db->db_mtx);
1169 if ((flags & DB_RF_NEVERWAIT) == 0) {
1170 while (db->db_state == DB_READ ||
1171 db->db_state == DB_FILL) {
1172 ASSERT(db->db_state == DB_READ ||
1173 (flags & DB_RF_HAVESTRUCT) == 0);
1174 DTRACE_PROBE2(blocked__read, dmu_buf_impl_t *,
1176 cv_wait(&db->db_changed, &db->db_mtx);
1178 if (db->db_state == DB_UNCACHED)
1179 err = SET_ERROR(EIO);
1181 mutex_exit(&db->db_mtx);
1188 dbuf_noread(dmu_buf_impl_t *db)
1190 ASSERT(!refcount_is_zero(&db->db_holds));
1191 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1192 mutex_enter(&db->db_mtx);
1193 while (db->db_state == DB_READ || db->db_state == DB_FILL)
1194 cv_wait(&db->db_changed, &db->db_mtx);
1195 if (db->db_state == DB_UNCACHED) {
1196 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1197 spa_t *spa = db->db_objset->os_spa;
1199 ASSERT(db->db_buf == NULL);
1200 ASSERT(db->db.db_data == NULL);
1201 dbuf_set_data(db, arc_alloc_buf(spa, db, type, db->db.db_size));
1202 db->db_state = DB_FILL;
1203 } else if (db->db_state == DB_NOFILL) {
1204 dbuf_clear_data(db);
1206 ASSERT3U(db->db_state, ==, DB_CACHED);
1208 mutex_exit(&db->db_mtx);
1212 dbuf_unoverride(dbuf_dirty_record_t *dr)
1214 dmu_buf_impl_t *db = dr->dr_dbuf;
1215 blkptr_t *bp = &dr->dt.dl.dr_overridden_by;
1216 uint64_t txg = dr->dr_txg;
1218 ASSERT(MUTEX_HELD(&db->db_mtx));
1220 * This assert is valid because dmu_sync() expects to be called by
1221 * a zilog's get_data while holding a range lock. This call only
1222 * comes from dbuf_dirty() callers who must also hold a range lock.
1224 ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC);
1225 ASSERT(db->db_level == 0);
1227 if (db->db_blkid == DMU_BONUS_BLKID ||
1228 dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN)
1231 ASSERT(db->db_data_pending != dr);
1233 /* free this block */
1234 if (!BP_IS_HOLE(bp) && !dr->dt.dl.dr_nopwrite)
1235 zio_free(db->db_objset->os_spa, txg, bp);
1237 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
1238 dr->dt.dl.dr_nopwrite = B_FALSE;
1241 * Release the already-written buffer, so we leave it in
1242 * a consistent dirty state. Note that all callers are
1243 * modifying the buffer, so they will immediately do
1244 * another (redundant) arc_release(). Therefore, leave
1245 * the buf thawed to save the effort of freezing &
1246 * immediately re-thawing it.
1248 arc_release(dr->dt.dl.dr_data, db);
1252 * Evict (if its unreferenced) or clear (if its referenced) any level-0
1253 * data blocks in the free range, so that any future readers will find
1257 dbuf_free_range(dnode_t *dn, uint64_t start_blkid, uint64_t end_blkid,
1260 dmu_buf_impl_t db_search;
1261 dmu_buf_impl_t *db, *db_next;
1262 uint64_t txg = tx->tx_txg;
1265 if (end_blkid > dn->dn_maxblkid &&
1266 !(start_blkid == DMU_SPILL_BLKID || end_blkid == DMU_SPILL_BLKID))
1267 end_blkid = dn->dn_maxblkid;
1268 dprintf_dnode(dn, "start=%llu end=%llu\n", start_blkid, end_blkid);
1270 db_search.db_level = 0;
1271 db_search.db_blkid = start_blkid;
1272 db_search.db_state = DB_SEARCH;
1274 mutex_enter(&dn->dn_dbufs_mtx);
1275 db = avl_find(&dn->dn_dbufs, &db_search, &where);
1276 ASSERT3P(db, ==, NULL);
1278 db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER);
1280 for (; db != NULL; db = db_next) {
1281 db_next = AVL_NEXT(&dn->dn_dbufs, db);
1282 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1284 if (db->db_level != 0 || db->db_blkid > end_blkid) {
1287 ASSERT3U(db->db_blkid, >=, start_blkid);
1289 /* found a level 0 buffer in the range */
1290 mutex_enter(&db->db_mtx);
1291 if (dbuf_undirty(db, tx)) {
1292 /* mutex has been dropped and dbuf destroyed */
1296 if (db->db_state == DB_UNCACHED ||
1297 db->db_state == DB_NOFILL ||
1298 db->db_state == DB_EVICTING) {
1299 ASSERT(db->db.db_data == NULL);
1300 mutex_exit(&db->db_mtx);
1303 if (db->db_state == DB_READ || db->db_state == DB_FILL) {
1304 /* will be handled in dbuf_read_done or dbuf_rele */
1305 db->db_freed_in_flight = TRUE;
1306 mutex_exit(&db->db_mtx);
1309 if (refcount_count(&db->db_holds) == 0) {
1314 /* The dbuf is referenced */
1316 if (db->db_last_dirty != NULL) {
1317 dbuf_dirty_record_t *dr = db->db_last_dirty;
1319 if (dr->dr_txg == txg) {
1321 * This buffer is "in-use", re-adjust the file
1322 * size to reflect that this buffer may
1323 * contain new data when we sync.
1325 if (db->db_blkid != DMU_SPILL_BLKID &&
1326 db->db_blkid > dn->dn_maxblkid)
1327 dn->dn_maxblkid = db->db_blkid;
1328 dbuf_unoverride(dr);
1331 * This dbuf is not dirty in the open context.
1332 * Either uncache it (if its not referenced in
1333 * the open context) or reset its contents to
1336 dbuf_fix_old_data(db, txg);
1339 /* clear the contents if its cached */
1340 if (db->db_state == DB_CACHED) {
1341 ASSERT(db->db.db_data != NULL);
1342 arc_release(db->db_buf, db);
1343 bzero(db->db.db_data, db->db.db_size);
1344 arc_buf_freeze(db->db_buf);
1347 mutex_exit(&db->db_mtx);
1349 mutex_exit(&dn->dn_dbufs_mtx);
1353 dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx)
1355 arc_buf_t *buf, *obuf;
1356 int osize = db->db.db_size;
1357 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1360 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1365 /* XXX does *this* func really need the lock? */
1366 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
1369 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
1370 * is OK, because there can be no other references to the db
1371 * when we are changing its size, so no concurrent DB_FILL can
1375 * XXX we should be doing a dbuf_read, checking the return
1376 * value and returning that up to our callers
1378 dmu_buf_will_dirty(&db->db, tx);
1380 /* create the data buffer for the new block */
1381 buf = arc_alloc_buf(dn->dn_objset->os_spa, db, type, size);
1383 /* copy old block data to the new block */
1385 bcopy(obuf->b_data, buf->b_data, MIN(osize, size));
1386 /* zero the remainder */
1388 bzero((uint8_t *)buf->b_data + osize, size - osize);
1390 mutex_enter(&db->db_mtx);
1391 dbuf_set_data(db, buf);
1392 arc_buf_destroy(obuf, db);
1393 db->db.db_size = size;
1395 if (db->db_level == 0) {
1396 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
1397 db->db_last_dirty->dt.dl.dr_data = buf;
1399 mutex_exit(&db->db_mtx);
1401 dmu_objset_willuse_space(dn->dn_objset, size - osize, tx);
1406 dbuf_release_bp(dmu_buf_impl_t *db)
1408 objset_t *os = db->db_objset;
1410 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
1411 ASSERT(arc_released(os->os_phys_buf) ||
1412 list_link_active(&os->os_dsl_dataset->ds_synced_link));
1413 ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf));
1415 (void) arc_release(db->db_buf, db);
1419 * We already have a dirty record for this TXG, and we are being
1423 dbuf_redirty(dbuf_dirty_record_t *dr)
1425 dmu_buf_impl_t *db = dr->dr_dbuf;
1427 ASSERT(MUTEX_HELD(&db->db_mtx));
1429 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID) {
1431 * If this buffer has already been written out,
1432 * we now need to reset its state.
1434 dbuf_unoverride(dr);
1435 if (db->db.db_object != DMU_META_DNODE_OBJECT &&
1436 db->db_state != DB_NOFILL) {
1437 /* Already released on initial dirty, so just thaw. */
1438 ASSERT(arc_released(db->db_buf));
1439 arc_buf_thaw(db->db_buf);
1444 dbuf_dirty_record_t *
1445 dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1449 dbuf_dirty_record_t **drp, *dr;
1450 int drop_struct_lock = FALSE;
1451 int txgoff = tx->tx_txg & TXG_MASK;
1453 ASSERT(tx->tx_txg != 0);
1454 ASSERT(!refcount_is_zero(&db->db_holds));
1455 DMU_TX_DIRTY_BUF(tx, db);
1460 * Shouldn't dirty a regular buffer in syncing context. Private
1461 * objects may be dirtied in syncing context, but only if they
1462 * were already pre-dirtied in open context.
1465 if (dn->dn_objset->os_dsl_dataset != NULL) {
1466 rrw_enter(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock,
1469 ASSERT(!dmu_tx_is_syncing(tx) ||
1470 BP_IS_HOLE(dn->dn_objset->os_rootbp) ||
1471 DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1472 dn->dn_objset->os_dsl_dataset == NULL);
1473 if (dn->dn_objset->os_dsl_dataset != NULL)
1474 rrw_exit(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock, FTAG);
1477 * We make this assert for private objects as well, but after we
1478 * check if we're already dirty. They are allowed to re-dirty
1479 * in syncing context.
1481 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1482 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1483 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1485 mutex_enter(&db->db_mtx);
1487 * XXX make this true for indirects too? The problem is that
1488 * transactions created with dmu_tx_create_assigned() from
1489 * syncing context don't bother holding ahead.
1491 ASSERT(db->db_level != 0 ||
1492 db->db_state == DB_CACHED || db->db_state == DB_FILL ||
1493 db->db_state == DB_NOFILL);
1495 mutex_enter(&dn->dn_mtx);
1497 * Don't set dirtyctx to SYNC if we're just modifying this as we
1498 * initialize the objset.
1500 if (dn->dn_dirtyctx == DN_UNDIRTIED) {
1501 if (dn->dn_objset->os_dsl_dataset != NULL) {
1502 rrw_enter(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock,
1505 if (!BP_IS_HOLE(dn->dn_objset->os_rootbp)) {
1506 dn->dn_dirtyctx = (dmu_tx_is_syncing(tx) ?
1507 DN_DIRTY_SYNC : DN_DIRTY_OPEN);
1508 ASSERT(dn->dn_dirtyctx_firstset == NULL);
1509 dn->dn_dirtyctx_firstset = kmem_alloc(1, KM_SLEEP);
1511 if (dn->dn_objset->os_dsl_dataset != NULL) {
1512 rrw_exit(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock,
1516 mutex_exit(&dn->dn_mtx);
1518 if (db->db_blkid == DMU_SPILL_BLKID)
1519 dn->dn_have_spill = B_TRUE;
1522 * If this buffer is already dirty, we're done.
1524 drp = &db->db_last_dirty;
1525 ASSERT(*drp == NULL || (*drp)->dr_txg <= tx->tx_txg ||
1526 db->db.db_object == DMU_META_DNODE_OBJECT);
1527 while ((dr = *drp) != NULL && dr->dr_txg > tx->tx_txg)
1529 if (dr && dr->dr_txg == tx->tx_txg) {
1533 mutex_exit(&db->db_mtx);
1538 * Only valid if not already dirty.
1540 ASSERT(dn->dn_object == 0 ||
1541 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1542 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1544 ASSERT3U(dn->dn_nlevels, >, db->db_level);
1547 * We should only be dirtying in syncing context if it's the
1548 * mos or we're initializing the os or it's a special object.
1549 * However, we are allowed to dirty in syncing context provided
1550 * we already dirtied it in open context. Hence we must make
1551 * this assertion only if we're not already dirty.
1554 VERIFY3U(tx->tx_txg, <=, spa_final_dirty_txg(os->os_spa));
1556 if (dn->dn_objset->os_dsl_dataset != NULL)
1557 rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_READER, FTAG);
1558 ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1559 os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp));
1560 if (dn->dn_objset->os_dsl_dataset != NULL)
1561 rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG);
1563 ASSERT(db->db.db_size != 0);
1565 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1567 if (db->db_blkid != DMU_BONUS_BLKID) {
1568 dmu_objset_willuse_space(os, db->db.db_size, tx);
1572 * If this buffer is dirty in an old transaction group we need
1573 * to make a copy of it so that the changes we make in this
1574 * transaction group won't leak out when we sync the older txg.
1576 dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP);
1577 if (db->db_level == 0) {
1578 void *data_old = db->db_buf;
1580 if (db->db_state != DB_NOFILL) {
1581 if (db->db_blkid == DMU_BONUS_BLKID) {
1582 dbuf_fix_old_data(db, tx->tx_txg);
1583 data_old = db->db.db_data;
1584 } else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
1586 * Release the data buffer from the cache so
1587 * that we can modify it without impacting
1588 * possible other users of this cached data
1589 * block. Note that indirect blocks and
1590 * private objects are not released until the
1591 * syncing state (since they are only modified
1594 arc_release(db->db_buf, db);
1595 dbuf_fix_old_data(db, tx->tx_txg);
1596 data_old = db->db_buf;
1598 ASSERT(data_old != NULL);
1600 dr->dt.dl.dr_data = data_old;
1602 mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_DEFAULT, NULL);
1603 list_create(&dr->dt.di.dr_children,
1604 sizeof (dbuf_dirty_record_t),
1605 offsetof(dbuf_dirty_record_t, dr_dirty_node));
1607 if (db->db_blkid != DMU_BONUS_BLKID && os->os_dsl_dataset != NULL)
1608 dr->dr_accounted = db->db.db_size;
1610 dr->dr_txg = tx->tx_txg;
1615 * We could have been freed_in_flight between the dbuf_noread
1616 * and dbuf_dirty. We win, as though the dbuf_noread() had
1617 * happened after the free.
1619 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1620 db->db_blkid != DMU_SPILL_BLKID) {
1621 mutex_enter(&dn->dn_mtx);
1622 if (dn->dn_free_ranges[txgoff] != NULL) {
1623 range_tree_clear(dn->dn_free_ranges[txgoff],
1626 mutex_exit(&dn->dn_mtx);
1627 db->db_freed_in_flight = FALSE;
1631 * This buffer is now part of this txg
1633 dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg);
1634 db->db_dirtycnt += 1;
1635 ASSERT3U(db->db_dirtycnt, <=, 3);
1637 mutex_exit(&db->db_mtx);
1639 if (db->db_blkid == DMU_BONUS_BLKID ||
1640 db->db_blkid == DMU_SPILL_BLKID) {
1641 mutex_enter(&dn->dn_mtx);
1642 ASSERT(!list_link_active(&dr->dr_dirty_node));
1643 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1644 mutex_exit(&dn->dn_mtx);
1645 dnode_setdirty(dn, tx);
1651 * The dn_struct_rwlock prevents db_blkptr from changing
1652 * due to a write from syncing context completing
1653 * while we are running, so we want to acquire it before
1654 * looking at db_blkptr.
1656 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
1657 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1658 drop_struct_lock = TRUE;
1662 * We need to hold the dn_struct_rwlock to make this assertion,
1663 * because it protects dn_phys / dn_next_nlevels from changing.
1665 ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) ||
1666 dn->dn_phys->dn_nlevels > db->db_level ||
1667 dn->dn_next_nlevels[txgoff] > db->db_level ||
1668 dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level ||
1669 dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level);
1672 * If we are overwriting a dedup BP, then unless it is snapshotted,
1673 * when we get to syncing context we will need to decrement its
1674 * refcount in the DDT. Prefetch the relevant DDT block so that
1675 * syncing context won't have to wait for the i/o.
1677 ddt_prefetch(os->os_spa, db->db_blkptr);
1679 if (db->db_level == 0) {
1680 dnode_new_blkid(dn, db->db_blkid, tx, drop_struct_lock);
1681 ASSERT(dn->dn_maxblkid >= db->db_blkid);
1684 if (db->db_level+1 < dn->dn_nlevels) {
1685 dmu_buf_impl_t *parent = db->db_parent;
1686 dbuf_dirty_record_t *di;
1687 int parent_held = FALSE;
1689 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
1690 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1692 parent = dbuf_hold_level(dn, db->db_level+1,
1693 db->db_blkid >> epbs, FTAG);
1694 ASSERT(parent != NULL);
1697 if (drop_struct_lock)
1698 rw_exit(&dn->dn_struct_rwlock);
1699 ASSERT3U(db->db_level+1, ==, parent->db_level);
1700 di = dbuf_dirty(parent, tx);
1702 dbuf_rele(parent, FTAG);
1704 mutex_enter(&db->db_mtx);
1706 * Since we've dropped the mutex, it's possible that
1707 * dbuf_undirty() might have changed this out from under us.
1709 if (db->db_last_dirty == dr ||
1710 dn->dn_object == DMU_META_DNODE_OBJECT) {
1711 mutex_enter(&di->dt.di.dr_mtx);
1712 ASSERT3U(di->dr_txg, ==, tx->tx_txg);
1713 ASSERT(!list_link_active(&dr->dr_dirty_node));
1714 list_insert_tail(&di->dt.di.dr_children, dr);
1715 mutex_exit(&di->dt.di.dr_mtx);
1718 mutex_exit(&db->db_mtx);
1720 ASSERT(db->db_level+1 == dn->dn_nlevels);
1721 ASSERT(db->db_blkid < dn->dn_nblkptr);
1722 ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf);
1723 mutex_enter(&dn->dn_mtx);
1724 ASSERT(!list_link_active(&dr->dr_dirty_node));
1725 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1726 mutex_exit(&dn->dn_mtx);
1727 if (drop_struct_lock)
1728 rw_exit(&dn->dn_struct_rwlock);
1731 dnode_setdirty(dn, tx);
1737 * Undirty a buffer in the transaction group referenced by the given
1738 * transaction. Return whether this evicted the dbuf.
1741 dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1744 uint64_t txg = tx->tx_txg;
1745 dbuf_dirty_record_t *dr, **drp;
1750 * Due to our use of dn_nlevels below, this can only be called
1751 * in open context, unless we are operating on the MOS.
1752 * From syncing context, dn_nlevels may be different from the
1753 * dn_nlevels used when dbuf was dirtied.
1755 ASSERT(db->db_objset ==
1756 dmu_objset_pool(db->db_objset)->dp_meta_objset ||
1757 txg != spa_syncing_txg(dmu_objset_spa(db->db_objset)));
1758 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1759 ASSERT0(db->db_level);
1760 ASSERT(MUTEX_HELD(&db->db_mtx));
1763 * If this buffer is not dirty, we're done.
1765 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1766 if (dr->dr_txg <= txg)
1768 if (dr == NULL || dr->dr_txg < txg)
1770 ASSERT(dr->dr_txg == txg);
1771 ASSERT(dr->dr_dbuf == db);
1776 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1778 ASSERT(db->db.db_size != 0);
1780 dsl_pool_undirty_space(dmu_objset_pool(dn->dn_objset),
1781 dr->dr_accounted, txg);
1786 * Note that there are three places in dbuf_dirty()
1787 * where this dirty record may be put on a list.
1788 * Make sure to do a list_remove corresponding to
1789 * every one of those list_insert calls.
1791 if (dr->dr_parent) {
1792 mutex_enter(&dr->dr_parent->dt.di.dr_mtx);
1793 list_remove(&dr->dr_parent->dt.di.dr_children, dr);
1794 mutex_exit(&dr->dr_parent->dt.di.dr_mtx);
1795 } else if (db->db_blkid == DMU_SPILL_BLKID ||
1796 db->db_level + 1 == dn->dn_nlevels) {
1797 ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf);
1798 mutex_enter(&dn->dn_mtx);
1799 list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr);
1800 mutex_exit(&dn->dn_mtx);
1804 if (db->db_state != DB_NOFILL) {
1805 dbuf_unoverride(dr);
1807 ASSERT(db->db_buf != NULL);
1808 ASSERT(dr->dt.dl.dr_data != NULL);
1809 if (dr->dt.dl.dr_data != db->db_buf)
1810 arc_buf_destroy(dr->dt.dl.dr_data, db);
1813 kmem_free(dr, sizeof (dbuf_dirty_record_t));
1815 ASSERT(db->db_dirtycnt > 0);
1816 db->db_dirtycnt -= 1;
1818 if (refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) {
1819 ASSERT(db->db_state == DB_NOFILL || arc_released(db->db_buf));
1828 dmu_buf_will_dirty(dmu_buf_t *db_fake, dmu_tx_t *tx)
1830 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1831 int rf = DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH;
1833 ASSERT(tx->tx_txg != 0);
1834 ASSERT(!refcount_is_zero(&db->db_holds));
1837 * Quick check for dirtyness. For already dirty blocks, this
1838 * reduces runtime of this function by >90%, and overall performance
1839 * by 50% for some workloads (e.g. file deletion with indirect blocks
1842 mutex_enter(&db->db_mtx);
1843 dbuf_dirty_record_t *dr;
1844 for (dr = db->db_last_dirty;
1845 dr != NULL && dr->dr_txg >= tx->tx_txg; dr = dr->dr_next) {
1847 * It's possible that it is already dirty but not cached,
1848 * because there are some calls to dbuf_dirty() that don't
1849 * go through dmu_buf_will_dirty().
1851 if (dr->dr_txg == tx->tx_txg && db->db_state == DB_CACHED) {
1852 /* This dbuf is already dirty and cached. */
1854 mutex_exit(&db->db_mtx);
1858 mutex_exit(&db->db_mtx);
1861 if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock))
1862 rf |= DB_RF_HAVESTRUCT;
1864 (void) dbuf_read(db, NULL, rf);
1865 (void) dbuf_dirty(db, tx);
1869 dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1871 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1873 db->db_state = DB_NOFILL;
1875 dmu_buf_will_fill(db_fake, tx);
1879 dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1881 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1883 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1884 ASSERT(tx->tx_txg != 0);
1885 ASSERT(db->db_level == 0);
1886 ASSERT(!refcount_is_zero(&db->db_holds));
1888 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
1889 dmu_tx_private_ok(tx));
1892 (void) dbuf_dirty(db, tx);
1895 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1898 dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx)
1900 mutex_enter(&db->db_mtx);
1903 if (db->db_state == DB_FILL) {
1904 if (db->db_level == 0 && db->db_freed_in_flight) {
1905 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1906 /* we were freed while filling */
1907 /* XXX dbuf_undirty? */
1908 bzero(db->db.db_data, db->db.db_size);
1909 db->db_freed_in_flight = FALSE;
1911 db->db_state = DB_CACHED;
1912 cv_broadcast(&db->db_changed);
1914 mutex_exit(&db->db_mtx);
1918 dmu_buf_write_embedded(dmu_buf_t *dbuf, void *data,
1919 bp_embedded_type_t etype, enum zio_compress comp,
1920 int uncompressed_size, int compressed_size, int byteorder,
1923 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
1924 struct dirty_leaf *dl;
1925 dmu_object_type_t type;
1927 if (etype == BP_EMBEDDED_TYPE_DATA) {
1928 ASSERT(spa_feature_is_active(dmu_objset_spa(db->db_objset),
1929 SPA_FEATURE_EMBEDDED_DATA));
1933 type = DB_DNODE(db)->dn_type;
1936 ASSERT0(db->db_level);
1937 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1939 dmu_buf_will_not_fill(dbuf, tx);
1941 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
1942 dl = &db->db_last_dirty->dt.dl;
1943 encode_embedded_bp_compressed(&dl->dr_overridden_by,
1944 data, comp, uncompressed_size, compressed_size);
1945 BPE_SET_ETYPE(&dl->dr_overridden_by, etype);
1946 BP_SET_TYPE(&dl->dr_overridden_by, type);
1947 BP_SET_LEVEL(&dl->dr_overridden_by, 0);
1948 BP_SET_BYTEORDER(&dl->dr_overridden_by, byteorder);
1950 dl->dr_override_state = DR_OVERRIDDEN;
1951 dl->dr_overridden_by.blk_birth = db->db_last_dirty->dr_txg;
1955 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1956 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1959 dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx)
1961 ASSERT(!refcount_is_zero(&db->db_holds));
1962 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1963 ASSERT(db->db_level == 0);
1964 ASSERT3U(dbuf_is_metadata(db), ==, arc_is_metadata(buf));
1965 ASSERT(buf != NULL);
1966 ASSERT(arc_buf_lsize(buf) == db->db.db_size);
1967 ASSERT(tx->tx_txg != 0);
1969 arc_return_buf(buf, db);
1970 ASSERT(arc_released(buf));
1972 mutex_enter(&db->db_mtx);
1974 while (db->db_state == DB_READ || db->db_state == DB_FILL)
1975 cv_wait(&db->db_changed, &db->db_mtx);
1977 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED);
1979 if (db->db_state == DB_CACHED &&
1980 refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) {
1981 mutex_exit(&db->db_mtx);
1982 (void) dbuf_dirty(db, tx);
1983 bcopy(buf->b_data, db->db.db_data, db->db.db_size);
1984 arc_buf_destroy(buf, db);
1985 xuio_stat_wbuf_copied();
1989 xuio_stat_wbuf_nocopy();
1990 if (db->db_state == DB_CACHED) {
1991 dbuf_dirty_record_t *dr = db->db_last_dirty;
1993 ASSERT(db->db_buf != NULL);
1994 if (dr != NULL && dr->dr_txg == tx->tx_txg) {
1995 ASSERT(dr->dt.dl.dr_data == db->db_buf);
1996 if (!arc_released(db->db_buf)) {
1997 ASSERT(dr->dt.dl.dr_override_state ==
1999 arc_release(db->db_buf, db);
2001 dr->dt.dl.dr_data = buf;
2002 arc_buf_destroy(db->db_buf, db);
2003 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) {
2004 arc_release(db->db_buf, db);
2005 arc_buf_destroy(db->db_buf, db);
2009 ASSERT(db->db_buf == NULL);
2010 dbuf_set_data(db, buf);
2011 db->db_state = DB_FILL;
2012 mutex_exit(&db->db_mtx);
2013 (void) dbuf_dirty(db, tx);
2014 dmu_buf_fill_done(&db->db, tx);
2018 dbuf_destroy(dmu_buf_impl_t *db)
2021 dmu_buf_impl_t *parent = db->db_parent;
2022 dmu_buf_impl_t *dndb;
2024 ASSERT(MUTEX_HELD(&db->db_mtx));
2025 ASSERT(refcount_is_zero(&db->db_holds));
2027 if (db->db_buf != NULL) {
2028 arc_buf_destroy(db->db_buf, db);
2032 if (db->db_blkid == DMU_BONUS_BLKID) {
2033 ASSERT(db->db.db_data != NULL);
2034 zio_buf_free(db->db.db_data, DN_MAX_BONUSLEN);
2035 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
2036 db->db_state = DB_UNCACHED;
2039 dbuf_clear_data(db);
2041 if (multilist_link_active(&db->db_cache_link)) {
2042 multilist_remove(dbuf_cache, db);
2043 (void) refcount_remove_many(&dbuf_cache_size,
2044 db->db.db_size, db);
2047 ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL);
2048 ASSERT(db->db_data_pending == NULL);
2050 db->db_state = DB_EVICTING;
2051 db->db_blkptr = NULL;
2054 * Now that db_state is DB_EVICTING, nobody else can find this via
2055 * the hash table. We can now drop db_mtx, which allows us to
2056 * acquire the dn_dbufs_mtx.
2058 mutex_exit(&db->db_mtx);
2063 if (db->db_blkid != DMU_BONUS_BLKID) {
2064 boolean_t needlock = !MUTEX_HELD(&dn->dn_dbufs_mtx);
2066 mutex_enter(&dn->dn_dbufs_mtx);
2067 avl_remove(&dn->dn_dbufs, db);
2068 atomic_dec_32(&dn->dn_dbufs_count);
2072 mutex_exit(&dn->dn_dbufs_mtx);
2074 * Decrementing the dbuf count means that the hold corresponding
2075 * to the removed dbuf is no longer discounted in dnode_move(),
2076 * so the dnode cannot be moved until after we release the hold.
2077 * The membar_producer() ensures visibility of the decremented
2078 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
2082 db->db_dnode_handle = NULL;
2084 dbuf_hash_remove(db);
2089 ASSERT(refcount_is_zero(&db->db_holds));
2091 db->db_parent = NULL;
2093 ASSERT(db->db_buf == NULL);
2094 ASSERT(db->db.db_data == NULL);
2095 ASSERT(db->db_hash_next == NULL);
2096 ASSERT(db->db_blkptr == NULL);
2097 ASSERT(db->db_data_pending == NULL);
2098 ASSERT(!multilist_link_active(&db->db_cache_link));
2100 kmem_cache_free(dbuf_kmem_cache, db);
2101 arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
2104 * If this dbuf is referenced from an indirect dbuf,
2105 * decrement the ref count on the indirect dbuf.
2107 if (parent && parent != dndb)
2108 dbuf_rele(parent, db);
2112 * Note: While bpp will always be updated if the function returns success,
2113 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
2114 * this happens when the dnode is the meta-dnode, or a userused or groupused
2118 dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse,
2119 dmu_buf_impl_t **parentp, blkptr_t **bpp)
2124 ASSERT(blkid != DMU_BONUS_BLKID);
2126 if (blkid == DMU_SPILL_BLKID) {
2127 mutex_enter(&dn->dn_mtx);
2128 if (dn->dn_have_spill &&
2129 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
2130 *bpp = &dn->dn_phys->dn_spill;
2133 dbuf_add_ref(dn->dn_dbuf, NULL);
2134 *parentp = dn->dn_dbuf;
2135 mutex_exit(&dn->dn_mtx);
2140 (dn->dn_phys->dn_nlevels == 0) ? 1 : dn->dn_phys->dn_nlevels;
2141 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
2143 ASSERT3U(level * epbs, <, 64);
2144 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2146 * This assertion shouldn't trip as long as the max indirect block size
2147 * is less than 1M. The reason for this is that up to that point,
2148 * the number of levels required to address an entire object with blocks
2149 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In
2150 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55
2151 * (i.e. we can address the entire object), objects will all use at most
2152 * N-1 levels and the assertion won't overflow. However, once epbs is
2153 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be
2154 * enough to address an entire object, so objects will have 5 levels,
2155 * but then this assertion will overflow.
2157 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we
2158 * need to redo this logic to handle overflows.
2160 ASSERT(level >= nlevels ||
2161 ((nlevels - level - 1) * epbs) +
2162 highbit64(dn->dn_phys->dn_nblkptr) <= 64);
2163 if (level >= nlevels ||
2164 blkid >= ((uint64_t)dn->dn_phys->dn_nblkptr <<
2165 ((nlevels - level - 1) * epbs)) ||
2167 blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) {
2168 /* the buffer has no parent yet */
2169 return (SET_ERROR(ENOENT));
2170 } else if (level < nlevels-1) {
2171 /* this block is referenced from an indirect block */
2172 int err = dbuf_hold_impl(dn, level+1,
2173 blkid >> epbs, fail_sparse, FALSE, NULL, parentp);
2176 err = dbuf_read(*parentp, NULL,
2177 (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL));
2179 dbuf_rele(*parentp, NULL);
2183 *bpp = ((blkptr_t *)(*parentp)->db.db_data) +
2184 (blkid & ((1ULL << epbs) - 1));
2185 if (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))
2186 ASSERT(BP_IS_HOLE(*bpp));
2189 /* the block is referenced from the dnode */
2190 ASSERT3U(level, ==, nlevels-1);
2191 ASSERT(dn->dn_phys->dn_nblkptr == 0 ||
2192 blkid < dn->dn_phys->dn_nblkptr);
2194 dbuf_add_ref(dn->dn_dbuf, NULL);
2195 *parentp = dn->dn_dbuf;
2197 *bpp = &dn->dn_phys->dn_blkptr[blkid];
2202 static dmu_buf_impl_t *
2203 dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid,
2204 dmu_buf_impl_t *parent, blkptr_t *blkptr)
2206 objset_t *os = dn->dn_objset;
2207 dmu_buf_impl_t *db, *odb;
2209 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2210 ASSERT(dn->dn_type != DMU_OT_NONE);
2212 db = kmem_cache_alloc(dbuf_kmem_cache, KM_SLEEP);
2215 db->db.db_object = dn->dn_object;
2216 db->db_level = level;
2217 db->db_blkid = blkid;
2218 db->db_last_dirty = NULL;
2219 db->db_dirtycnt = 0;
2220 db->db_dnode_handle = dn->dn_handle;
2221 db->db_parent = parent;
2222 db->db_blkptr = blkptr;
2225 db->db_user_immediate_evict = FALSE;
2226 db->db_freed_in_flight = FALSE;
2227 db->db_pending_evict = FALSE;
2229 if (blkid == DMU_BONUS_BLKID) {
2230 ASSERT3P(parent, ==, dn->dn_dbuf);
2231 db->db.db_size = DN_MAX_BONUSLEN -
2232 (dn->dn_nblkptr-1) * sizeof (blkptr_t);
2233 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
2234 db->db.db_offset = DMU_BONUS_BLKID;
2235 db->db_state = DB_UNCACHED;
2236 /* the bonus dbuf is not placed in the hash table */
2237 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
2239 } else if (blkid == DMU_SPILL_BLKID) {
2240 db->db.db_size = (blkptr != NULL) ?
2241 BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE;
2242 db->db.db_offset = 0;
2245 db->db_level ? 1 << dn->dn_indblkshift : dn->dn_datablksz;
2246 db->db.db_size = blocksize;
2247 db->db.db_offset = db->db_blkid * blocksize;
2251 * Hold the dn_dbufs_mtx while we get the new dbuf
2252 * in the hash table *and* added to the dbufs list.
2253 * This prevents a possible deadlock with someone
2254 * trying to look up this dbuf before its added to the
2257 mutex_enter(&dn->dn_dbufs_mtx);
2258 db->db_state = DB_EVICTING;
2259 if ((odb = dbuf_hash_insert(db)) != NULL) {
2260 /* someone else inserted it first */
2261 kmem_cache_free(dbuf_kmem_cache, db);
2262 mutex_exit(&dn->dn_dbufs_mtx);
2265 avl_add(&dn->dn_dbufs, db);
2267 db->db_state = DB_UNCACHED;
2268 mutex_exit(&dn->dn_dbufs_mtx);
2269 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
2271 if (parent && parent != dn->dn_dbuf)
2272 dbuf_add_ref(parent, db);
2274 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
2275 refcount_count(&dn->dn_holds) > 0);
2276 (void) refcount_add(&dn->dn_holds, db);
2277 atomic_inc_32(&dn->dn_dbufs_count);
2279 dprintf_dbuf(db, "db=%p\n", db);
2284 typedef struct dbuf_prefetch_arg {
2285 spa_t *dpa_spa; /* The spa to issue the prefetch in. */
2286 zbookmark_phys_t dpa_zb; /* The target block to prefetch. */
2287 int dpa_epbs; /* Entries (blkptr_t's) Per Block Shift. */
2288 int dpa_curlevel; /* The current level that we're reading */
2289 dnode_t *dpa_dnode; /* The dnode associated with the prefetch */
2290 zio_priority_t dpa_prio; /* The priority I/Os should be issued at. */
2291 zio_t *dpa_zio; /* The parent zio_t for all prefetches. */
2292 arc_flags_t dpa_aflags; /* Flags to pass to the final prefetch. */
2293 } dbuf_prefetch_arg_t;
2296 * Actually issue the prefetch read for the block given.
2299 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t *dpa, blkptr_t *bp)
2301 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
2304 arc_flags_t aflags =
2305 dpa->dpa_aflags | ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH;
2307 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp));
2308 ASSERT3U(dpa->dpa_curlevel, ==, dpa->dpa_zb.zb_level);
2309 ASSERT(dpa->dpa_zio != NULL);
2310 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa, bp, NULL, NULL,
2311 dpa->dpa_prio, ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2312 &aflags, &dpa->dpa_zb);
2316 * Called when an indirect block above our prefetch target is read in. This
2317 * will either read in the next indirect block down the tree or issue the actual
2318 * prefetch if the next block down is our target.
2321 dbuf_prefetch_indirect_done(zio_t *zio, arc_buf_t *abuf, void *private)
2323 dbuf_prefetch_arg_t *dpa = private;
2325 ASSERT3S(dpa->dpa_zb.zb_level, <, dpa->dpa_curlevel);
2326 ASSERT3S(dpa->dpa_curlevel, >, 0);
2329 * The dpa_dnode is only valid if we are called with a NULL
2330 * zio. This indicates that the arc_read() returned without
2331 * first calling zio_read() to issue a physical read. Once
2332 * a physical read is made the dpa_dnode must be invalidated
2333 * as the locks guarding it may have been dropped. If the
2334 * dpa_dnode is still valid, then we want to add it to the dbuf
2335 * cache. To do so, we must hold the dbuf associated with the block
2336 * we just prefetched, read its contents so that we associate it
2337 * with an arc_buf_t, and then release it.
2340 ASSERT3S(BP_GET_LEVEL(zio->io_bp), ==, dpa->dpa_curlevel);
2341 if (zio->io_flags & ZIO_FLAG_RAW) {
2342 ASSERT3U(BP_GET_PSIZE(zio->io_bp), ==, zio->io_size);
2344 ASSERT3U(BP_GET_LSIZE(zio->io_bp), ==, zio->io_size);
2346 ASSERT3P(zio->io_spa, ==, dpa->dpa_spa);
2348 dpa->dpa_dnode = NULL;
2349 } else if (dpa->dpa_dnode != NULL) {
2350 uint64_t curblkid = dpa->dpa_zb.zb_blkid >>
2351 (dpa->dpa_epbs * (dpa->dpa_curlevel -
2352 dpa->dpa_zb.zb_level));
2353 dmu_buf_impl_t *db = dbuf_hold_level(dpa->dpa_dnode,
2354 dpa->dpa_curlevel, curblkid, FTAG);
2355 (void) dbuf_read(db, NULL,
2356 DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH | DB_RF_HAVESTRUCT);
2357 dbuf_rele(db, FTAG);
2360 dpa->dpa_curlevel--;
2362 uint64_t nextblkid = dpa->dpa_zb.zb_blkid >>
2363 (dpa->dpa_epbs * (dpa->dpa_curlevel - dpa->dpa_zb.zb_level));
2364 blkptr_t *bp = ((blkptr_t *)abuf->b_data) +
2365 P2PHASE(nextblkid, 1ULL << dpa->dpa_epbs);
2366 if (BP_IS_HOLE(bp) || (zio != NULL && zio->io_error != 0)) {
2367 kmem_free(dpa, sizeof (*dpa));
2368 } else if (dpa->dpa_curlevel == dpa->dpa_zb.zb_level) {
2369 ASSERT3U(nextblkid, ==, dpa->dpa_zb.zb_blkid);
2370 dbuf_issue_final_prefetch(dpa, bp);
2371 kmem_free(dpa, sizeof (*dpa));
2373 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT;
2374 zbookmark_phys_t zb;
2376 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2377 if (dpa->dpa_aflags & ARC_FLAG_L2CACHE)
2378 iter_aflags |= ARC_FLAG_L2CACHE;
2380 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp));
2382 SET_BOOKMARK(&zb, dpa->dpa_zb.zb_objset,
2383 dpa->dpa_zb.zb_object, dpa->dpa_curlevel, nextblkid);
2385 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa,
2386 bp, dbuf_prefetch_indirect_done, dpa, dpa->dpa_prio,
2387 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2391 arc_buf_destroy(abuf, private);
2395 * Issue prefetch reads for the given block on the given level. If the indirect
2396 * blocks above that block are not in memory, we will read them in
2397 * asynchronously. As a result, this call never blocks waiting for a read to
2401 dbuf_prefetch(dnode_t *dn, int64_t level, uint64_t blkid, zio_priority_t prio,
2405 int epbs, nlevels, curlevel;
2408 ASSERT(blkid != DMU_BONUS_BLKID);
2409 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2411 if (blkid > dn->dn_maxblkid)
2414 if (dnode_block_freed(dn, blkid))
2418 * This dnode hasn't been written to disk yet, so there's nothing to
2421 nlevels = dn->dn_phys->dn_nlevels;
2422 if (level >= nlevels || dn->dn_phys->dn_nblkptr == 0)
2425 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2426 if (dn->dn_phys->dn_maxblkid < blkid << (epbs * level))
2429 dmu_buf_impl_t *db = dbuf_find(dn->dn_objset, dn->dn_object,
2432 mutex_exit(&db->db_mtx);
2434 * This dbuf already exists. It is either CACHED, or
2435 * (we assume) about to be read or filled.
2441 * Find the closest ancestor (indirect block) of the target block
2442 * that is present in the cache. In this indirect block, we will
2443 * find the bp that is at curlevel, curblkid.
2447 while (curlevel < nlevels - 1) {
2448 int parent_level = curlevel + 1;
2449 uint64_t parent_blkid = curblkid >> epbs;
2452 if (dbuf_hold_impl(dn, parent_level, parent_blkid,
2453 FALSE, TRUE, FTAG, &db) == 0) {
2454 blkptr_t *bpp = db->db_buf->b_data;
2455 bp = bpp[P2PHASE(curblkid, 1 << epbs)];
2456 dbuf_rele(db, FTAG);
2460 curlevel = parent_level;
2461 curblkid = parent_blkid;
2464 if (curlevel == nlevels - 1) {
2465 /* No cached indirect blocks found. */
2466 ASSERT3U(curblkid, <, dn->dn_phys->dn_nblkptr);
2467 bp = dn->dn_phys->dn_blkptr[curblkid];
2469 if (BP_IS_HOLE(&bp))
2472 ASSERT3U(curlevel, ==, BP_GET_LEVEL(&bp));
2474 zio_t *pio = zio_root(dmu_objset_spa(dn->dn_objset), NULL, NULL,
2477 dbuf_prefetch_arg_t *dpa = kmem_zalloc(sizeof (*dpa), KM_SLEEP);
2478 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
2479 SET_BOOKMARK(&dpa->dpa_zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET,
2480 dn->dn_object, level, blkid);
2481 dpa->dpa_curlevel = curlevel;
2482 dpa->dpa_prio = prio;
2483 dpa->dpa_aflags = aflags;
2484 dpa->dpa_spa = dn->dn_objset->os_spa;
2485 dpa->dpa_dnode = dn;
2486 dpa->dpa_epbs = epbs;
2489 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2490 if (DNODE_LEVEL_IS_L2CACHEABLE(dn, level))
2491 dpa->dpa_aflags |= ARC_FLAG_L2CACHE;
2494 * If we have the indirect just above us, no need to do the asynchronous
2495 * prefetch chain; we'll just run the last step ourselves. If we're at
2496 * a higher level, though, we want to issue the prefetches for all the
2497 * indirect blocks asynchronously, so we can go on with whatever we were
2500 if (curlevel == level) {
2501 ASSERT3U(curblkid, ==, blkid);
2502 dbuf_issue_final_prefetch(dpa, &bp);
2503 kmem_free(dpa, sizeof (*dpa));
2505 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT;
2506 zbookmark_phys_t zb;
2508 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2509 if (DNODE_LEVEL_IS_L2CACHEABLE(dn, level))
2510 iter_aflags |= ARC_FLAG_L2CACHE;
2512 SET_BOOKMARK(&zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET,
2513 dn->dn_object, curlevel, curblkid);
2514 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa,
2515 &bp, dbuf_prefetch_indirect_done, dpa, prio,
2516 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2520 * We use pio here instead of dpa_zio since it's possible that
2521 * dpa may have already been freed.
2527 * Returns with db_holds incremented, and db_mtx not held.
2528 * Note: dn_struct_rwlock must be held.
2531 dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid,
2532 boolean_t fail_sparse, boolean_t fail_uncached,
2533 void *tag, dmu_buf_impl_t **dbp)
2535 dmu_buf_impl_t *db, *parent = NULL;
2537 ASSERT(blkid != DMU_BONUS_BLKID);
2538 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2539 ASSERT3U(dn->dn_nlevels, >, level);
2543 /* dbuf_find() returns with db_mtx held */
2544 db = dbuf_find(dn->dn_objset, dn->dn_object, level, blkid);
2547 blkptr_t *bp = NULL;
2551 return (SET_ERROR(ENOENT));
2553 ASSERT3P(parent, ==, NULL);
2554 err = dbuf_findbp(dn, level, blkid, fail_sparse, &parent, &bp);
2556 if (err == 0 && bp && BP_IS_HOLE(bp))
2557 err = SET_ERROR(ENOENT);
2560 dbuf_rele(parent, NULL);
2564 if (err && err != ENOENT)
2566 db = dbuf_create(dn, level, blkid, parent, bp);
2569 if (fail_uncached && db->db_state != DB_CACHED) {
2570 mutex_exit(&db->db_mtx);
2571 return (SET_ERROR(ENOENT));
2574 if (db->db_buf != NULL)
2575 ASSERT3P(db->db.db_data, ==, db->db_buf->b_data);
2577 ASSERT(db->db_buf == NULL || arc_referenced(db->db_buf));
2580 * If this buffer is currently syncing out, and we are are
2581 * still referencing it from db_data, we need to make a copy
2582 * of it in case we decide we want to dirty it again in this txg.
2584 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
2585 dn->dn_object != DMU_META_DNODE_OBJECT &&
2586 db->db_state == DB_CACHED && db->db_data_pending) {
2587 dbuf_dirty_record_t *dr = db->db_data_pending;
2589 if (dr->dt.dl.dr_data == db->db_buf) {
2590 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
2593 arc_alloc_buf(dn->dn_objset->os_spa, db, type,
2595 bcopy(dr->dt.dl.dr_data->b_data, db->db.db_data,
2600 if (multilist_link_active(&db->db_cache_link)) {
2601 ASSERT(refcount_is_zero(&db->db_holds));
2602 multilist_remove(dbuf_cache, db);
2603 (void) refcount_remove_many(&dbuf_cache_size,
2604 db->db.db_size, db);
2606 (void) refcount_add(&db->db_holds, tag);
2608 mutex_exit(&db->db_mtx);
2610 /* NOTE: we can't rele the parent until after we drop the db_mtx */
2612 dbuf_rele(parent, NULL);
2614 ASSERT3P(DB_DNODE(db), ==, dn);
2615 ASSERT3U(db->db_blkid, ==, blkid);
2616 ASSERT3U(db->db_level, ==, level);
2623 dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag)
2625 return (dbuf_hold_level(dn, 0, blkid, tag));
2629 dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag)
2632 int err = dbuf_hold_impl(dn, level, blkid, FALSE, FALSE, tag, &db);
2633 return (err ? NULL : db);
2637 dbuf_create_bonus(dnode_t *dn)
2639 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
2641 ASSERT(dn->dn_bonus == NULL);
2642 dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL);
2646 dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx)
2648 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2651 if (db->db_blkid != DMU_SPILL_BLKID)
2652 return (SET_ERROR(ENOTSUP));
2654 blksz = SPA_MINBLOCKSIZE;
2655 ASSERT3U(blksz, <=, spa_maxblocksize(dmu_objset_spa(db->db_objset)));
2656 blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE);
2660 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2661 dbuf_new_size(db, blksz, tx);
2662 rw_exit(&dn->dn_struct_rwlock);
2669 dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx)
2671 dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx);
2674 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2676 dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
2678 int64_t holds = refcount_add(&db->db_holds, tag);
2679 ASSERT3S(holds, >, 1);
2682 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
2684 dbuf_try_add_ref(dmu_buf_t *db_fake, objset_t *os, uint64_t obj, uint64_t blkid,
2687 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2688 dmu_buf_impl_t *found_db;
2689 boolean_t result = B_FALSE;
2691 if (db->db_blkid == DMU_BONUS_BLKID)
2692 found_db = dbuf_find_bonus(os, obj);
2694 found_db = dbuf_find(os, obj, 0, blkid);
2696 if (found_db != NULL) {
2697 if (db == found_db && dbuf_refcount(db) > db->db_dirtycnt) {
2698 (void) refcount_add(&db->db_holds, tag);
2701 mutex_exit(&db->db_mtx);
2707 * If you call dbuf_rele() you had better not be referencing the dnode handle
2708 * unless you have some other direct or indirect hold on the dnode. (An indirect
2709 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2710 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2711 * dnode's parent dbuf evicting its dnode handles.
2714 dbuf_rele(dmu_buf_impl_t *db, void *tag)
2716 mutex_enter(&db->db_mtx);
2717 dbuf_rele_and_unlock(db, tag);
2721 dmu_buf_rele(dmu_buf_t *db, void *tag)
2723 dbuf_rele((dmu_buf_impl_t *)db, tag);
2727 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2728 * db_dirtycnt and db_holds to be updated atomically.
2731 dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag)
2735 ASSERT(MUTEX_HELD(&db->db_mtx));
2739 * Remove the reference to the dbuf before removing its hold on the
2740 * dnode so we can guarantee in dnode_move() that a referenced bonus
2741 * buffer has a corresponding dnode hold.
2743 holds = refcount_remove(&db->db_holds, tag);
2747 * We can't freeze indirects if there is a possibility that they
2748 * may be modified in the current syncing context.
2750 if (db->db_buf != NULL &&
2751 holds == (db->db_level == 0 ? db->db_dirtycnt : 0)) {
2752 arc_buf_freeze(db->db_buf);
2755 if (holds == db->db_dirtycnt &&
2756 db->db_level == 0 && db->db_user_immediate_evict)
2757 dbuf_evict_user(db);
2760 if (db->db_blkid == DMU_BONUS_BLKID) {
2762 boolean_t evict_dbuf = db->db_pending_evict;
2765 * If the dnode moves here, we cannot cross this
2766 * barrier until the move completes.
2771 atomic_dec_32(&dn->dn_dbufs_count);
2774 * Decrementing the dbuf count means that the bonus
2775 * buffer's dnode hold is no longer discounted in
2776 * dnode_move(). The dnode cannot move until after
2777 * the dnode_rele() below.
2782 * Do not reference db after its lock is dropped.
2783 * Another thread may evict it.
2785 mutex_exit(&db->db_mtx);
2788 dnode_evict_bonus(dn);
2791 } else if (db->db_buf == NULL) {
2793 * This is a special case: we never associated this
2794 * dbuf with any data allocated from the ARC.
2796 ASSERT(db->db_state == DB_UNCACHED ||
2797 db->db_state == DB_NOFILL);
2799 } else if (arc_released(db->db_buf)) {
2801 * This dbuf has anonymous data associated with it.
2805 boolean_t do_arc_evict = B_FALSE;
2807 spa_t *spa = dmu_objset_spa(db->db_objset);
2809 if (!DBUF_IS_CACHEABLE(db) &&
2810 db->db_blkptr != NULL &&
2811 !BP_IS_HOLE(db->db_blkptr) &&
2812 !BP_IS_EMBEDDED(db->db_blkptr)) {
2813 do_arc_evict = B_TRUE;
2814 bp = *db->db_blkptr;
2817 if (!DBUF_IS_CACHEABLE(db) ||
2818 db->db_pending_evict) {
2820 } else if (!multilist_link_active(&db->db_cache_link)) {
2821 multilist_insert(dbuf_cache, db);
2822 (void) refcount_add_many(&dbuf_cache_size,
2823 db->db.db_size, db);
2824 mutex_exit(&db->db_mtx);
2826 dbuf_evict_notify();
2830 arc_freed(spa, &bp);
2833 mutex_exit(&db->db_mtx);
2838 #pragma weak dmu_buf_refcount = dbuf_refcount
2840 dbuf_refcount(dmu_buf_impl_t *db)
2842 return (refcount_count(&db->db_holds));
2846 dmu_buf_replace_user(dmu_buf_t *db_fake, dmu_buf_user_t *old_user,
2847 dmu_buf_user_t *new_user)
2849 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2851 mutex_enter(&db->db_mtx);
2852 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2853 if (db->db_user == old_user)
2854 db->db_user = new_user;
2856 old_user = db->db_user;
2857 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2858 mutex_exit(&db->db_mtx);
2864 dmu_buf_set_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2866 return (dmu_buf_replace_user(db_fake, NULL, user));
2870 dmu_buf_set_user_ie(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2872 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2874 db->db_user_immediate_evict = TRUE;
2875 return (dmu_buf_set_user(db_fake, user));
2879 dmu_buf_remove_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2881 return (dmu_buf_replace_user(db_fake, user, NULL));
2885 dmu_buf_get_user(dmu_buf_t *db_fake)
2887 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2889 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2890 return (db->db_user);
2894 dmu_buf_user_evict_wait()
2896 taskq_wait(dbu_evict_taskq);
2900 dmu_buf_get_blkptr(dmu_buf_t *db)
2902 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2903 return (dbi->db_blkptr);
2907 dmu_buf_get_objset(dmu_buf_t *db)
2909 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2910 return (dbi->db_objset);
2914 dmu_buf_dnode_enter(dmu_buf_t *db)
2916 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2917 DB_DNODE_ENTER(dbi);
2918 return (DB_DNODE(dbi));
2922 dmu_buf_dnode_exit(dmu_buf_t *db)
2924 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2929 dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db)
2931 /* ASSERT(dmu_tx_is_syncing(tx) */
2932 ASSERT(MUTEX_HELD(&db->db_mtx));
2934 if (db->db_blkptr != NULL)
2937 if (db->db_blkid == DMU_SPILL_BLKID) {
2938 db->db_blkptr = &dn->dn_phys->dn_spill;
2939 BP_ZERO(db->db_blkptr);
2942 if (db->db_level == dn->dn_phys->dn_nlevels-1) {
2944 * This buffer was allocated at a time when there was
2945 * no available blkptrs from the dnode, or it was
2946 * inappropriate to hook it in (i.e., nlevels mis-match).
2948 ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr);
2949 ASSERT(db->db_parent == NULL);
2950 db->db_parent = dn->dn_dbuf;
2951 db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
2954 dmu_buf_impl_t *parent = db->db_parent;
2955 int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2957 ASSERT(dn->dn_phys->dn_nlevels > 1);
2958 if (parent == NULL) {
2959 mutex_exit(&db->db_mtx);
2960 rw_enter(&dn->dn_struct_rwlock, RW_READER);
2961 parent = dbuf_hold_level(dn, db->db_level + 1,
2962 db->db_blkid >> epbs, db);
2963 rw_exit(&dn->dn_struct_rwlock);
2964 mutex_enter(&db->db_mtx);
2965 db->db_parent = parent;
2967 db->db_blkptr = (blkptr_t *)parent->db.db_data +
2968 (db->db_blkid & ((1ULL << epbs) - 1));
2974 dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2976 dmu_buf_impl_t *db = dr->dr_dbuf;
2980 ASSERT(dmu_tx_is_syncing(tx));
2982 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2984 mutex_enter(&db->db_mtx);
2986 ASSERT(db->db_level > 0);
2989 /* Read the block if it hasn't been read yet. */
2990 if (db->db_buf == NULL) {
2991 mutex_exit(&db->db_mtx);
2992 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
2993 mutex_enter(&db->db_mtx);
2995 ASSERT3U(db->db_state, ==, DB_CACHED);
2996 ASSERT(db->db_buf != NULL);
3000 /* Indirect block size must match what the dnode thinks it is. */
3001 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
3002 dbuf_check_blkptr(dn, db);
3005 /* Provide the pending dirty record to child dbufs */
3006 db->db_data_pending = dr;
3008 mutex_exit(&db->db_mtx);
3009 dbuf_write(dr, db->db_buf, tx);
3012 mutex_enter(&dr->dt.di.dr_mtx);
3013 dbuf_sync_list(&dr->dt.di.dr_children, db->db_level - 1, tx);
3014 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
3015 mutex_exit(&dr->dt.di.dr_mtx);
3020 dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
3022 arc_buf_t **datap = &dr->dt.dl.dr_data;
3023 dmu_buf_impl_t *db = dr->dr_dbuf;
3026 uint64_t txg = tx->tx_txg;
3028 ASSERT(dmu_tx_is_syncing(tx));
3030 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
3032 mutex_enter(&db->db_mtx);
3034 * To be synced, we must be dirtied. But we
3035 * might have been freed after the dirty.
3037 if (db->db_state == DB_UNCACHED) {
3038 /* This buffer has been freed since it was dirtied */
3039 ASSERT(db->db.db_data == NULL);
3040 } else if (db->db_state == DB_FILL) {
3041 /* This buffer was freed and is now being re-filled */
3042 ASSERT(db->db.db_data != dr->dt.dl.dr_data);
3044 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL);
3051 if (db->db_blkid == DMU_SPILL_BLKID) {
3052 mutex_enter(&dn->dn_mtx);
3053 dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR;
3054 mutex_exit(&dn->dn_mtx);
3058 * If this is a bonus buffer, simply copy the bonus data into the
3059 * dnode. It will be written out when the dnode is synced (and it
3060 * will be synced, since it must have been dirty for dbuf_sync to
3063 if (db->db_blkid == DMU_BONUS_BLKID) {
3064 dbuf_dirty_record_t **drp;
3066 ASSERT(*datap != NULL);
3067 ASSERT0(db->db_level);
3068 ASSERT3U(dn->dn_phys->dn_bonuslen, <=, DN_MAX_BONUSLEN);
3069 bcopy(*datap, DN_BONUS(dn->dn_phys), dn->dn_phys->dn_bonuslen);
3072 if (*datap != db->db.db_data) {
3073 zio_buf_free(*datap, DN_MAX_BONUSLEN);
3074 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
3076 db->db_data_pending = NULL;
3077 drp = &db->db_last_dirty;
3079 drp = &(*drp)->dr_next;
3080 ASSERT(dr->dr_next == NULL);
3081 ASSERT(dr->dr_dbuf == db);
3083 if (dr->dr_dbuf->db_level != 0) {
3084 list_destroy(&dr->dt.di.dr_children);
3085 mutex_destroy(&dr->dt.di.dr_mtx);
3087 kmem_free(dr, sizeof (dbuf_dirty_record_t));
3088 ASSERT(db->db_dirtycnt > 0);
3089 db->db_dirtycnt -= 1;
3090 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
3097 * This function may have dropped the db_mtx lock allowing a dmu_sync
3098 * operation to sneak in. As a result, we need to ensure that we
3099 * don't check the dr_override_state until we have returned from
3100 * dbuf_check_blkptr.
3102 dbuf_check_blkptr(dn, db);
3105 * If this buffer is in the middle of an immediate write,
3106 * wait for the synchronous IO to complete.
3108 while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
3109 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
3110 cv_wait(&db->db_changed, &db->db_mtx);
3111 ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN);
3114 if (db->db_state != DB_NOFILL &&
3115 dn->dn_object != DMU_META_DNODE_OBJECT &&
3116 refcount_count(&db->db_holds) > 1 &&
3117 dr->dt.dl.dr_override_state != DR_OVERRIDDEN &&
3118 *datap == db->db_buf) {
3120 * If this buffer is currently "in use" (i.e., there
3121 * are active holds and db_data still references it),
3122 * then make a copy before we start the write so that
3123 * any modifications from the open txg will not leak
3126 * NOTE: this copy does not need to be made for
3127 * objects only modified in the syncing context (e.g.
3128 * DNONE_DNODE blocks).
3130 int psize = arc_buf_size(*datap);
3131 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
3132 enum zio_compress compress_type = arc_get_compression(*datap);
3134 if (compress_type == ZIO_COMPRESS_OFF) {
3135 *datap = arc_alloc_buf(os->os_spa, db, type, psize);
3137 ASSERT3U(type, ==, ARC_BUFC_DATA);
3138 int lsize = arc_buf_lsize(*datap);
3139 *datap = arc_alloc_compressed_buf(os->os_spa, db,
3140 psize, lsize, compress_type);
3142 bcopy(db->db.db_data, (*datap)->b_data, psize);
3144 db->db_data_pending = dr;
3146 mutex_exit(&db->db_mtx);
3148 dbuf_write(dr, *datap, tx);
3150 ASSERT(!list_link_active(&dr->dr_dirty_node));
3151 if (dn->dn_object == DMU_META_DNODE_OBJECT) {
3152 list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr);
3156 * Although zio_nowait() does not "wait for an IO", it does
3157 * initiate the IO. If this is an empty write it seems plausible
3158 * that the IO could actually be completed before the nowait
3159 * returns. We need to DB_DNODE_EXIT() first in case
3160 * zio_nowait() invalidates the dbuf.
3163 zio_nowait(dr->dr_zio);
3168 dbuf_sync_list(list_t *list, int level, dmu_tx_t *tx)
3170 dbuf_dirty_record_t *dr;
3172 while (dr = list_head(list)) {
3173 if (dr->dr_zio != NULL) {
3175 * If we find an already initialized zio then we
3176 * are processing the meta-dnode, and we have finished.
3177 * The dbufs for all dnodes are put back on the list
3178 * during processing, so that we can zio_wait()
3179 * these IOs after initiating all child IOs.
3181 ASSERT3U(dr->dr_dbuf->db.db_object, ==,
3182 DMU_META_DNODE_OBJECT);
3185 if (dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
3186 dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
3187 VERIFY3U(dr->dr_dbuf->db_level, ==, level);
3189 list_remove(list, dr);
3190 if (dr->dr_dbuf->db_level > 0)
3191 dbuf_sync_indirect(dr, tx);
3193 dbuf_sync_leaf(dr, tx);
3199 dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
3201 dmu_buf_impl_t *db = vdb;
3203 blkptr_t *bp = zio->io_bp;
3204 blkptr_t *bp_orig = &zio->io_bp_orig;
3205 spa_t *spa = zio->io_spa;
3210 ASSERT3P(db->db_blkptr, !=, NULL);
3211 ASSERT3P(&db->db_data_pending->dr_bp_copy, ==, bp);
3215 delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig);
3216 dnode_diduse_space(dn, delta - zio->io_prev_space_delta);
3217 zio->io_prev_space_delta = delta;
3219 if (bp->blk_birth != 0) {
3220 ASSERT((db->db_blkid != DMU_SPILL_BLKID &&
3221 BP_GET_TYPE(bp) == dn->dn_type) ||
3222 (db->db_blkid == DMU_SPILL_BLKID &&
3223 BP_GET_TYPE(bp) == dn->dn_bonustype) ||
3224 BP_IS_EMBEDDED(bp));
3225 ASSERT(BP_GET_LEVEL(bp) == db->db_level);
3228 mutex_enter(&db->db_mtx);
3231 if (db->db_blkid == DMU_SPILL_BLKID) {
3232 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
3233 ASSERT(!(BP_IS_HOLE(bp)) &&
3234 db->db_blkptr == &dn->dn_phys->dn_spill);
3238 if (db->db_level == 0) {
3239 mutex_enter(&dn->dn_mtx);
3240 if (db->db_blkid > dn->dn_phys->dn_maxblkid &&
3241 db->db_blkid != DMU_SPILL_BLKID)
3242 dn->dn_phys->dn_maxblkid = db->db_blkid;
3243 mutex_exit(&dn->dn_mtx);
3245 if (dn->dn_type == DMU_OT_DNODE) {
3246 dnode_phys_t *dnp = db->db.db_data;
3247 for (i = db->db.db_size >> DNODE_SHIFT; i > 0;
3249 if (dnp->dn_type != DMU_OT_NONE)
3253 if (BP_IS_HOLE(bp)) {
3260 blkptr_t *ibp = db->db.db_data;
3261 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
3262 for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) {
3263 if (BP_IS_HOLE(ibp))
3265 fill += BP_GET_FILL(ibp);
3270 if (!BP_IS_EMBEDDED(bp))
3271 bp->blk_fill = fill;
3273 mutex_exit(&db->db_mtx);
3275 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
3276 *db->db_blkptr = *bp;
3277 rw_exit(&dn->dn_struct_rwlock);
3282 * This function gets called just prior to running through the compression
3283 * stage of the zio pipeline. If we're an indirect block comprised of only
3284 * holes, then we want this indirect to be compressed away to a hole. In
3285 * order to do that we must zero out any information about the holes that
3286 * this indirect points to prior to before we try to compress it.
3289 dbuf_write_children_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
3291 dmu_buf_impl_t *db = vdb;
3294 unsigned int epbs, i;
3296 ASSERT3U(db->db_level, >, 0);
3299 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
3300 ASSERT3U(epbs, <, 31);
3302 /* Determine if all our children are holes */
3303 for (i = 0, bp = db->db.db_data; i < 1 << epbs; i++, bp++) {
3304 if (!BP_IS_HOLE(bp))
3309 * If all the children are holes, then zero them all out so that
3310 * we may get compressed away.
3312 if (i == 1 << epbs) {
3314 * We only found holes. Grab the rwlock to prevent
3315 * anybody from reading the blocks we're about to
3318 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
3319 bzero(db->db.db_data, db->db.db_size);
3320 rw_exit(&dn->dn_struct_rwlock);
3326 * The SPA will call this callback several times for each zio - once
3327 * for every physical child i/o (zio->io_phys_children times). This
3328 * allows the DMU to monitor the progress of each logical i/o. For example,
3329 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
3330 * block. There may be a long delay before all copies/fragments are completed,
3331 * so this callback allows us to retire dirty space gradually, as the physical
3336 dbuf_write_physdone(zio_t *zio, arc_buf_t *buf, void *arg)
3338 dmu_buf_impl_t *db = arg;
3339 objset_t *os = db->db_objset;
3340 dsl_pool_t *dp = dmu_objset_pool(os);
3341 dbuf_dirty_record_t *dr;
3344 dr = db->db_data_pending;
3345 ASSERT3U(dr->dr_txg, ==, zio->io_txg);
3348 * The callback will be called io_phys_children times. Retire one
3349 * portion of our dirty space each time we are called. Any rounding
3350 * error will be cleaned up by dsl_pool_sync()'s call to
3351 * dsl_pool_undirty_space().
3353 delta = dr->dr_accounted / zio->io_phys_children;
3354 dsl_pool_undirty_space(dp, delta, zio->io_txg);
3359 dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
3361 dmu_buf_impl_t *db = vdb;
3362 blkptr_t *bp_orig = &zio->io_bp_orig;
3363 blkptr_t *bp = db->db_blkptr;
3364 objset_t *os = db->db_objset;
3365 dmu_tx_t *tx = os->os_synctx;
3366 dbuf_dirty_record_t **drp, *dr;
3368 ASSERT0(zio->io_error);
3369 ASSERT(db->db_blkptr == bp);
3372 * For nopwrites and rewrites we ensure that the bp matches our
3373 * original and bypass all the accounting.
3375 if (zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)) {
3376 ASSERT(BP_EQUAL(bp, bp_orig));
3378 dsl_dataset_t *ds = os->os_dsl_dataset;
3379 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
3380 dsl_dataset_block_born(ds, bp, tx);
3383 mutex_enter(&db->db_mtx);
3387 drp = &db->db_last_dirty;
3388 while ((dr = *drp) != db->db_data_pending)
3390 ASSERT(!list_link_active(&dr->dr_dirty_node));
3391 ASSERT(dr->dr_dbuf == db);
3392 ASSERT(dr->dr_next == NULL);
3396 if (db->db_blkid == DMU_SPILL_BLKID) {
3401 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
3402 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
3403 db->db_blkptr == &dn->dn_phys->dn_spill);
3408 if (db->db_level == 0) {
3409 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
3410 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
3411 if (db->db_state != DB_NOFILL) {
3412 if (dr->dt.dl.dr_data != db->db_buf)
3413 arc_buf_destroy(dr->dt.dl.dr_data, db);
3420 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
3421 ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
3422 if (!BP_IS_HOLE(db->db_blkptr)) {
3424 dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
3425 ASSERT3U(db->db_blkid, <=,
3426 dn->dn_phys->dn_maxblkid >> (db->db_level * epbs));
3427 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
3431 mutex_destroy(&dr->dt.di.dr_mtx);
3432 list_destroy(&dr->dt.di.dr_children);
3434 kmem_free(dr, sizeof (dbuf_dirty_record_t));
3436 cv_broadcast(&db->db_changed);
3437 ASSERT(db->db_dirtycnt > 0);
3438 db->db_dirtycnt -= 1;
3439 db->db_data_pending = NULL;
3440 dbuf_rele_and_unlock(db, (void *)(uintptr_t)tx->tx_txg);
3444 dbuf_write_nofill_ready(zio_t *zio)
3446 dbuf_write_ready(zio, NULL, zio->io_private);
3450 dbuf_write_nofill_done(zio_t *zio)
3452 dbuf_write_done(zio, NULL, zio->io_private);
3456 dbuf_write_override_ready(zio_t *zio)
3458 dbuf_dirty_record_t *dr = zio->io_private;
3459 dmu_buf_impl_t *db = dr->dr_dbuf;
3461 dbuf_write_ready(zio, NULL, db);
3465 dbuf_write_override_done(zio_t *zio)
3467 dbuf_dirty_record_t *dr = zio->io_private;
3468 dmu_buf_impl_t *db = dr->dr_dbuf;
3469 blkptr_t *obp = &dr->dt.dl.dr_overridden_by;
3471 mutex_enter(&db->db_mtx);
3472 if (!BP_EQUAL(zio->io_bp, obp)) {
3473 if (!BP_IS_HOLE(obp))
3474 dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp);
3475 arc_release(dr->dt.dl.dr_data, db);
3477 mutex_exit(&db->db_mtx);
3478 dbuf_write_done(zio, NULL, db);
3480 if (zio->io_abd != NULL)
3481 abd_put(zio->io_abd);
3484 /* Issue I/O to commit a dirty buffer to disk. */
3486 dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx)
3488 dmu_buf_impl_t *db = dr->dr_dbuf;
3491 dmu_buf_impl_t *parent = db->db_parent;
3492 uint64_t txg = tx->tx_txg;
3493 zbookmark_phys_t zb;
3498 ASSERT(dmu_tx_is_syncing(tx));
3504 if (db->db_state != DB_NOFILL) {
3505 if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) {
3507 * Private object buffers are released here rather
3508 * than in dbuf_dirty() since they are only modified
3509 * in the syncing context and we don't want the
3510 * overhead of making multiple copies of the data.
3512 if (BP_IS_HOLE(db->db_blkptr)) {
3515 dbuf_release_bp(db);
3520 if (parent != dn->dn_dbuf) {
3521 /* Our parent is an indirect block. */
3522 /* We have a dirty parent that has been scheduled for write. */
3523 ASSERT(parent && parent->db_data_pending);
3524 /* Our parent's buffer is one level closer to the dnode. */
3525 ASSERT(db->db_level == parent->db_level-1);
3527 * We're about to modify our parent's db_data by modifying
3528 * our block pointer, so the parent must be released.
3530 ASSERT(arc_released(parent->db_buf));
3531 zio = parent->db_data_pending->dr_zio;
3533 /* Our parent is the dnode itself. */
3534 ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 &&
3535 db->db_blkid != DMU_SPILL_BLKID) ||
3536 (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0));
3537 if (db->db_blkid != DMU_SPILL_BLKID)
3538 ASSERT3P(db->db_blkptr, ==,
3539 &dn->dn_phys->dn_blkptr[db->db_blkid]);
3543 ASSERT(db->db_level == 0 || data == db->db_buf);
3544 ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
3547 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
3548 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
3549 db->db.db_object, db->db_level, db->db_blkid);
3551 if (db->db_blkid == DMU_SPILL_BLKID)
3553 wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
3555 dmu_write_policy(os, dn, db->db_level, wp_flag, &zp);
3559 * We copy the blkptr now (rather than when we instantiate the dirty
3560 * record), because its value can change between open context and
3561 * syncing context. We do not need to hold dn_struct_rwlock to read
3562 * db_blkptr because we are in syncing context.
3564 dr->dr_bp_copy = *db->db_blkptr;
3566 if (db->db_level == 0 &&
3567 dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
3569 * The BP for this block has been provided by open context
3570 * (by dmu_sync() or dmu_buf_write_embedded()).
3572 abd_t *contents = (data != NULL) ?
3573 abd_get_from_buf(data->b_data, arc_buf_size(data)) : NULL;
3575 dr->dr_zio = zio_write(zio, os->os_spa, txg, &dr->dr_bp_copy,
3576 contents, db->db.db_size, db->db.db_size, &zp,
3577 dbuf_write_override_ready, NULL, NULL,
3578 dbuf_write_override_done,
3579 dr, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
3580 mutex_enter(&db->db_mtx);
3581 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
3582 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by,
3583 dr->dt.dl.dr_copies, dr->dt.dl.dr_nopwrite);
3584 mutex_exit(&db->db_mtx);
3585 } else if (db->db_state == DB_NOFILL) {
3586 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF ||
3587 zp.zp_checksum == ZIO_CHECKSUM_NOPARITY);
3588 dr->dr_zio = zio_write(zio, os->os_spa, txg,
3589 &dr->dr_bp_copy, NULL, db->db.db_size, db->db.db_size, &zp,
3590 dbuf_write_nofill_ready, NULL, NULL,
3591 dbuf_write_nofill_done, db,
3592 ZIO_PRIORITY_ASYNC_WRITE,
3593 ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb);
3595 ASSERT(arc_released(data));
3598 * For indirect blocks, we want to setup the children
3599 * ready callback so that we can properly handle an indirect
3600 * block that only contains holes.
3602 arc_done_func_t *children_ready_cb = NULL;
3603 if (db->db_level != 0)
3604 children_ready_cb = dbuf_write_children_ready;
3606 dr->dr_zio = arc_write(zio, os->os_spa, txg,
3607 &dr->dr_bp_copy, data, DBUF_IS_L2CACHEABLE(db),
3608 &zp, dbuf_write_ready, children_ready_cb,
3609 dbuf_write_physdone, dbuf_write_done, db,
3610 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);