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, 2016 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>
50 uint_t zfs_dbuf_evict_key;
53 * Number of times that zfs_free_range() took the slow path while doing
54 * a zfs receive. A nonzero value indicates a potential performance problem.
56 uint64_t zfs_free_range_recv_miss;
58 static boolean_t dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
59 static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx);
62 extern inline void dmu_buf_init_user(dmu_buf_user_t *dbu,
63 dmu_buf_evict_func_t *evict_func, dmu_buf_t **clear_on_evict_dbufp);
67 * Global data structures and functions for the dbuf cache.
69 static kmem_cache_t *dbuf_kmem_cache;
70 static taskq_t *dbu_evict_taskq;
72 static kthread_t *dbuf_cache_evict_thread;
73 static kmutex_t dbuf_evict_lock;
74 static kcondvar_t dbuf_evict_cv;
75 static boolean_t dbuf_evict_thread_exit;
78 * LRU cache of dbufs. The dbuf cache maintains a list of dbufs that
79 * are not currently held but have been recently released. These dbufs
80 * are not eligible for arc eviction until they are aged out of the cache.
81 * Dbufs are added to the dbuf cache once the last hold is released. If a
82 * dbuf is later accessed and still exists in the dbuf cache, then it will
83 * be removed from the cache and later re-added to the head of the cache.
84 * Dbufs that are aged out of the cache will be immediately destroyed and
85 * become eligible for arc eviction.
87 static multilist_t dbuf_cache;
88 static refcount_t dbuf_cache_size;
89 uint64_t dbuf_cache_max_bytes = 100 * 1024 * 1024;
91 /* Cap the size of the dbuf cache to log2 fraction of arc size. */
92 int dbuf_cache_max_shift = 5;
95 * The dbuf cache uses a three-stage eviction policy:
96 * - A low water marker designates when the dbuf eviction thread
97 * should stop evicting from the dbuf cache.
98 * - When we reach the maximum size (aka mid water mark), we
99 * signal the eviction thread to run.
100 * - The high water mark indicates when the eviction thread
101 * is unable to keep up with the incoming load and eviction must
102 * happen in the context of the calling thread.
106 * low water mid water hi water
107 * +----------------------------------------+----------+----------+
112 * +----------------------------------------+----------+----------+
114 * evicting eviction directly
117 * The high and low water marks indicate the operating range for the eviction
118 * thread. The low water mark is, by default, 90% of the total size of the
119 * cache and the high water mark is at 110% (both of these percentages can be
120 * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct,
121 * respectively). The eviction thread will try to ensure that the cache remains
122 * within this range by waking up every second and checking if the cache is
123 * above the low water mark. The thread can also be woken up by callers adding
124 * elements into the cache if the cache is larger than the mid water (i.e max
125 * cache size). Once the eviction thread is woken up and eviction is required,
126 * it will continue evicting buffers until it's able to reduce the cache size
127 * to the low water mark. If the cache size continues to grow and hits the high
128 * water mark, then callers adding elments to the cache will begin to evict
129 * directly from the cache until the cache is no longer above the high water
134 * The percentage above and below the maximum cache size.
136 uint_t dbuf_cache_hiwater_pct = 10;
137 uint_t dbuf_cache_lowater_pct = 10;
141 dbuf_cons(void *vdb, void *unused, int kmflag)
143 dmu_buf_impl_t *db = vdb;
144 bzero(db, sizeof (dmu_buf_impl_t));
146 mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL);
147 cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL);
148 multilist_link_init(&db->db_cache_link);
149 refcount_create(&db->db_holds);
156 dbuf_dest(void *vdb, void *unused)
158 dmu_buf_impl_t *db = vdb;
159 mutex_destroy(&db->db_mtx);
160 cv_destroy(&db->db_changed);
161 ASSERT(!multilist_link_active(&db->db_cache_link));
162 refcount_destroy(&db->db_holds);
166 * dbuf hash table routines
168 static dbuf_hash_table_t dbuf_hash_table;
170 static uint64_t dbuf_hash_count;
173 dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid)
175 uintptr_t osv = (uintptr_t)os;
176 uint64_t crc = -1ULL;
178 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
179 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (lvl)) & 0xFF];
180 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF];
181 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF];
182 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF];
183 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 0)) & 0xFF];
184 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 8)) & 0xFF];
186 crc ^= (osv>>14) ^ (obj>>16) ^ (blkid>>16);
191 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
192 ((dbuf)->db.db_object == (obj) && \
193 (dbuf)->db_objset == (os) && \
194 (dbuf)->db_level == (level) && \
195 (dbuf)->db_blkid == (blkid))
198 dbuf_find(objset_t *os, uint64_t obj, uint8_t level, uint64_t blkid)
200 dbuf_hash_table_t *h = &dbuf_hash_table;
201 uint64_t hv = dbuf_hash(os, obj, level, blkid);
202 uint64_t idx = hv & h->hash_table_mask;
205 mutex_enter(DBUF_HASH_MUTEX(h, idx));
206 for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) {
207 if (DBUF_EQUAL(db, os, obj, level, blkid)) {
208 mutex_enter(&db->db_mtx);
209 if (db->db_state != DB_EVICTING) {
210 mutex_exit(DBUF_HASH_MUTEX(h, idx));
213 mutex_exit(&db->db_mtx);
216 mutex_exit(DBUF_HASH_MUTEX(h, idx));
220 static dmu_buf_impl_t *
221 dbuf_find_bonus(objset_t *os, uint64_t object)
224 dmu_buf_impl_t *db = NULL;
226 if (dnode_hold(os, object, FTAG, &dn) == 0) {
227 rw_enter(&dn->dn_struct_rwlock, RW_READER);
228 if (dn->dn_bonus != NULL) {
230 mutex_enter(&db->db_mtx);
232 rw_exit(&dn->dn_struct_rwlock);
233 dnode_rele(dn, FTAG);
239 * Insert an entry into the hash table. If there is already an element
240 * equal to elem in the hash table, then the already existing element
241 * will be returned and the new element will not be inserted.
242 * Otherwise returns NULL.
244 static dmu_buf_impl_t *
245 dbuf_hash_insert(dmu_buf_impl_t *db)
247 dbuf_hash_table_t *h = &dbuf_hash_table;
248 objset_t *os = db->db_objset;
249 uint64_t obj = db->db.db_object;
250 int level = db->db_level;
251 uint64_t blkid = db->db_blkid;
252 uint64_t hv = dbuf_hash(os, obj, level, blkid);
253 uint64_t idx = hv & h->hash_table_mask;
256 mutex_enter(DBUF_HASH_MUTEX(h, idx));
257 for (dbf = h->hash_table[idx]; dbf != NULL; dbf = dbf->db_hash_next) {
258 if (DBUF_EQUAL(dbf, os, obj, level, blkid)) {
259 mutex_enter(&dbf->db_mtx);
260 if (dbf->db_state != DB_EVICTING) {
261 mutex_exit(DBUF_HASH_MUTEX(h, idx));
264 mutex_exit(&dbf->db_mtx);
268 mutex_enter(&db->db_mtx);
269 db->db_hash_next = h->hash_table[idx];
270 h->hash_table[idx] = db;
271 mutex_exit(DBUF_HASH_MUTEX(h, idx));
272 atomic_inc_64(&dbuf_hash_count);
278 * Remove an entry from the hash table. It must be in the EVICTING state.
281 dbuf_hash_remove(dmu_buf_impl_t *db)
283 dbuf_hash_table_t *h = &dbuf_hash_table;
284 uint64_t hv = dbuf_hash(db->db_objset, db->db.db_object,
285 db->db_level, db->db_blkid);
286 uint64_t idx = hv & h->hash_table_mask;
287 dmu_buf_impl_t *dbf, **dbp;
290 * We musn't hold db_mtx to maintain lock ordering:
291 * DBUF_HASH_MUTEX > db_mtx.
293 ASSERT(refcount_is_zero(&db->db_holds));
294 ASSERT(db->db_state == DB_EVICTING);
295 ASSERT(!MUTEX_HELD(&db->db_mtx));
297 mutex_enter(DBUF_HASH_MUTEX(h, idx));
298 dbp = &h->hash_table[idx];
299 while ((dbf = *dbp) != db) {
300 dbp = &dbf->db_hash_next;
303 *dbp = db->db_hash_next;
304 db->db_hash_next = NULL;
305 mutex_exit(DBUF_HASH_MUTEX(h, idx));
306 atomic_dec_64(&dbuf_hash_count);
312 } dbvu_verify_type_t;
315 dbuf_verify_user(dmu_buf_impl_t *db, dbvu_verify_type_t verify_type)
320 if (db->db_user == NULL)
323 /* Only data blocks support the attachment of user data. */
324 ASSERT(db->db_level == 0);
326 /* Clients must resolve a dbuf before attaching user data. */
327 ASSERT(db->db.db_data != NULL);
328 ASSERT3U(db->db_state, ==, DB_CACHED);
330 holds = refcount_count(&db->db_holds);
331 if (verify_type == DBVU_EVICTING) {
333 * Immediate eviction occurs when holds == dirtycnt.
334 * For normal eviction buffers, holds is zero on
335 * eviction, except when dbuf_fix_old_data() calls
336 * dbuf_clear_data(). However, the hold count can grow
337 * during eviction even though db_mtx is held (see
338 * dmu_bonus_hold() for an example), so we can only
339 * test the generic invariant that holds >= dirtycnt.
341 ASSERT3U(holds, >=, db->db_dirtycnt);
343 if (db->db_user_immediate_evict == TRUE)
344 ASSERT3U(holds, >=, db->db_dirtycnt);
346 ASSERT3U(holds, >, 0);
352 dbuf_evict_user(dmu_buf_impl_t *db)
354 dmu_buf_user_t *dbu = db->db_user;
356 ASSERT(MUTEX_HELD(&db->db_mtx));
361 dbuf_verify_user(db, DBVU_EVICTING);
365 if (dbu->dbu_clear_on_evict_dbufp != NULL)
366 *dbu->dbu_clear_on_evict_dbufp = NULL;
370 * Invoke the callback from a taskq to avoid lock order reversals
371 * and limit stack depth.
373 taskq_dispatch_ent(dbu_evict_taskq, dbu->dbu_evict_func, dbu, 0,
378 dbuf_is_metadata(dmu_buf_impl_t *db)
380 if (db->db_level > 0) {
383 boolean_t is_metadata;
386 is_metadata = DMU_OT_IS_METADATA(DB_DNODE(db)->dn_type);
389 return (is_metadata);
394 * This function *must* return indices evenly distributed between all
395 * sublists of the multilist. This is needed due to how the dbuf eviction
396 * code is laid out; dbuf_evict_thread() assumes dbufs are evenly
397 * distributed between all sublists and uses this assumption when
398 * deciding which sublist to evict from and how much to evict from it.
401 dbuf_cache_multilist_index_func(multilist_t *ml, void *obj)
403 dmu_buf_impl_t *db = obj;
406 * The assumption here, is the hash value for a given
407 * dmu_buf_impl_t will remain constant throughout it's lifetime
408 * (i.e. it's objset, object, level and blkid fields don't change).
409 * Thus, we don't need to store the dbuf's sublist index
410 * on insertion, as this index can be recalculated on removal.
412 * Also, the low order bits of the hash value are thought to be
413 * distributed evenly. Otherwise, in the case that the multilist
414 * has a power of two number of sublists, each sublists' usage
415 * would not be evenly distributed.
417 return (dbuf_hash(db->db_objset, db->db.db_object,
418 db->db_level, db->db_blkid) %
419 multilist_get_num_sublists(ml));
422 static inline boolean_t
423 dbuf_cache_above_hiwater(void)
425 uint64_t dbuf_cache_hiwater_bytes =
426 (dbuf_cache_max_bytes * dbuf_cache_hiwater_pct) / 100;
428 return (refcount_count(&dbuf_cache_size) >
429 dbuf_cache_max_bytes + dbuf_cache_hiwater_bytes);
432 static inline boolean_t
433 dbuf_cache_above_lowater(void)
435 uint64_t dbuf_cache_lowater_bytes =
436 (dbuf_cache_max_bytes * dbuf_cache_lowater_pct) / 100;
438 return (refcount_count(&dbuf_cache_size) >
439 dbuf_cache_max_bytes - dbuf_cache_lowater_bytes);
443 * Evict the oldest eligible dbuf from the dbuf cache.
448 int idx = multilist_get_random_index(&dbuf_cache);
449 multilist_sublist_t *mls = multilist_sublist_lock(&dbuf_cache, idx);
451 ASSERT(!MUTEX_HELD(&dbuf_evict_lock));
454 * Set the thread's tsd to indicate that it's processing evictions.
455 * Once a thread stops evicting from the dbuf cache it will
456 * reset its tsd to NULL.
458 ASSERT3P(tsd_get(zfs_dbuf_evict_key), ==, NULL);
459 (void) tsd_set(zfs_dbuf_evict_key, (void *)B_TRUE);
461 dmu_buf_impl_t *db = multilist_sublist_tail(mls);
462 while (db != NULL && mutex_tryenter(&db->db_mtx) == 0) {
463 db = multilist_sublist_prev(mls, db);
466 DTRACE_PROBE2(dbuf__evict__one, dmu_buf_impl_t *, db,
467 multilist_sublist_t *, mls);
470 multilist_sublist_remove(mls, db);
471 multilist_sublist_unlock(mls);
472 (void) refcount_remove_many(&dbuf_cache_size,
476 multilist_sublist_unlock(mls);
478 (void) tsd_set(zfs_dbuf_evict_key, NULL);
482 * The dbuf evict thread is responsible for aging out dbufs from the
483 * cache. Once the cache has reached it's maximum size, dbufs are removed
484 * and destroyed. The eviction thread will continue running until the size
485 * of the dbuf cache is at or below the maximum size. Once the dbuf is aged
486 * out of the cache it is destroyed and becomes eligible for arc eviction.
489 dbuf_evict_thread(void *dummy __unused)
493 CALLB_CPR_INIT(&cpr, &dbuf_evict_lock, callb_generic_cpr, FTAG);
495 mutex_enter(&dbuf_evict_lock);
496 while (!dbuf_evict_thread_exit) {
497 while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit) {
498 CALLB_CPR_SAFE_BEGIN(&cpr);
499 (void) cv_timedwait_hires(&dbuf_evict_cv,
500 &dbuf_evict_lock, SEC2NSEC(1), MSEC2NSEC(1), 0);
501 CALLB_CPR_SAFE_END(&cpr, &dbuf_evict_lock);
503 mutex_exit(&dbuf_evict_lock);
506 * Keep evicting as long as we're above the low water mark
507 * for the cache. We do this without holding the locks to
508 * minimize lock contention.
510 while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit) {
514 mutex_enter(&dbuf_evict_lock);
517 dbuf_evict_thread_exit = B_FALSE;
518 cv_broadcast(&dbuf_evict_cv);
519 CALLB_CPR_EXIT(&cpr); /* drops dbuf_evict_lock */
524 * Wake up the dbuf eviction thread if the dbuf cache is at its max size.
525 * If the dbuf cache is at its high water mark, then evict a dbuf from the
526 * dbuf cache using the callers context.
529 dbuf_evict_notify(void)
533 * We use thread specific data to track when a thread has
534 * started processing evictions. This allows us to avoid deeply
535 * nested stacks that would have a call flow similar to this:
537 * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify()
540 * +-----dbuf_destroy()<--dbuf_evict_one()<--------+
542 * The dbuf_eviction_thread will always have its tsd set until
543 * that thread exits. All other threads will only set their tsd
544 * if they are participating in the eviction process. This only
545 * happens if the eviction thread is unable to process evictions
546 * fast enough. To keep the dbuf cache size in check, other threads
547 * can evict from the dbuf cache directly. Those threads will set
548 * their tsd values so that we ensure that they only evict one dbuf
549 * from the dbuf cache.
551 if (tsd_get(zfs_dbuf_evict_key) != NULL)
554 if (refcount_count(&dbuf_cache_size) > dbuf_cache_max_bytes) {
555 boolean_t evict_now = B_FALSE;
557 mutex_enter(&dbuf_evict_lock);
558 if (refcount_count(&dbuf_cache_size) > dbuf_cache_max_bytes) {
559 evict_now = dbuf_cache_above_hiwater();
560 cv_signal(&dbuf_evict_cv);
562 mutex_exit(&dbuf_evict_lock);
573 uint64_t hsize = 1ULL << 16;
574 dbuf_hash_table_t *h = &dbuf_hash_table;
578 * The hash table is big enough to fill all of physical memory
579 * with an average 4K block size. The table will take up
580 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
582 while (hsize * 4096 < (uint64_t)physmem * PAGESIZE)
586 h->hash_table_mask = hsize - 1;
587 h->hash_table = kmem_zalloc(hsize * sizeof (void *), KM_NOSLEEP);
588 if (h->hash_table == NULL) {
589 /* XXX - we should really return an error instead of assert */
590 ASSERT(hsize > (1ULL << 10));
595 dbuf_kmem_cache = kmem_cache_create("dmu_buf_impl_t",
596 sizeof (dmu_buf_impl_t),
597 0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0);
599 for (i = 0; i < DBUF_MUTEXES; i++)
600 mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL);
603 * Setup the parameters for the dbuf cache. We cap the size of the
604 * dbuf cache to 1/32nd (default) of the size of the ARC.
606 dbuf_cache_max_bytes = MIN(dbuf_cache_max_bytes,
607 arc_max_bytes() >> dbuf_cache_max_shift);
610 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
611 * configuration is not required.
613 dbu_evict_taskq = taskq_create("dbu_evict", 1, minclsyspri, 0, 0, 0);
615 multilist_create(&dbuf_cache, sizeof (dmu_buf_impl_t),
616 offsetof(dmu_buf_impl_t, db_cache_link),
617 zfs_arc_num_sublists_per_state,
618 dbuf_cache_multilist_index_func);
619 refcount_create(&dbuf_cache_size);
621 tsd_create(&zfs_dbuf_evict_key, NULL);
622 dbuf_evict_thread_exit = B_FALSE;
623 mutex_init(&dbuf_evict_lock, NULL, MUTEX_DEFAULT, NULL);
624 cv_init(&dbuf_evict_cv, NULL, CV_DEFAULT, NULL);
625 dbuf_cache_evict_thread = thread_create(NULL, 0, dbuf_evict_thread,
626 NULL, 0, &p0, TS_RUN, minclsyspri);
632 dbuf_hash_table_t *h = &dbuf_hash_table;
635 for (i = 0; i < DBUF_MUTEXES; i++)
636 mutex_destroy(&h->hash_mutexes[i]);
637 kmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
638 kmem_cache_destroy(dbuf_kmem_cache);
639 taskq_destroy(dbu_evict_taskq);
641 mutex_enter(&dbuf_evict_lock);
642 dbuf_evict_thread_exit = B_TRUE;
643 while (dbuf_evict_thread_exit) {
644 cv_signal(&dbuf_evict_cv);
645 cv_wait(&dbuf_evict_cv, &dbuf_evict_lock);
647 mutex_exit(&dbuf_evict_lock);
648 tsd_destroy(&zfs_dbuf_evict_key);
650 mutex_destroy(&dbuf_evict_lock);
651 cv_destroy(&dbuf_evict_cv);
653 refcount_destroy(&dbuf_cache_size);
654 multilist_destroy(&dbuf_cache);
663 dbuf_verify(dmu_buf_impl_t *db)
666 dbuf_dirty_record_t *dr;
668 ASSERT(MUTEX_HELD(&db->db_mtx));
670 if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY))
673 ASSERT(db->db_objset != NULL);
677 ASSERT(db->db_parent == NULL);
678 ASSERT(db->db_blkptr == NULL);
680 ASSERT3U(db->db.db_object, ==, dn->dn_object);
681 ASSERT3P(db->db_objset, ==, dn->dn_objset);
682 ASSERT3U(db->db_level, <, dn->dn_nlevels);
683 ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
684 db->db_blkid == DMU_SPILL_BLKID ||
685 !avl_is_empty(&dn->dn_dbufs));
687 if (db->db_blkid == DMU_BONUS_BLKID) {
689 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
690 ASSERT3U(db->db.db_offset, ==, DMU_BONUS_BLKID);
691 } else if (db->db_blkid == DMU_SPILL_BLKID) {
693 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
694 ASSERT0(db->db.db_offset);
696 ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size);
699 for (dr = db->db_data_pending; dr != NULL; dr = dr->dr_next)
700 ASSERT(dr->dr_dbuf == db);
702 for (dr = db->db_last_dirty; dr != NULL; dr = dr->dr_next)
703 ASSERT(dr->dr_dbuf == db);
706 * We can't assert that db_size matches dn_datablksz because it
707 * can be momentarily different when another thread is doing
710 if (db->db_level == 0 && db->db.db_object == DMU_META_DNODE_OBJECT) {
711 dr = db->db_data_pending;
713 * It should only be modified in syncing context, so
714 * make sure we only have one copy of the data.
716 ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf);
719 /* verify db->db_blkptr */
721 if (db->db_parent == dn->dn_dbuf) {
722 /* db is pointed to by the dnode */
723 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
724 if (DMU_OBJECT_IS_SPECIAL(db->db.db_object))
725 ASSERT(db->db_parent == NULL);
727 ASSERT(db->db_parent != NULL);
728 if (db->db_blkid != DMU_SPILL_BLKID)
729 ASSERT3P(db->db_blkptr, ==,
730 &dn->dn_phys->dn_blkptr[db->db_blkid]);
732 /* db is pointed to by an indirect block */
733 int epb = db->db_parent->db.db_size >> SPA_BLKPTRSHIFT;
734 ASSERT3U(db->db_parent->db_level, ==, db->db_level+1);
735 ASSERT3U(db->db_parent->db.db_object, ==,
738 * dnode_grow_indblksz() can make this fail if we don't
739 * have the struct_rwlock. XXX indblksz no longer
740 * grows. safe to do this now?
742 if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
743 ASSERT3P(db->db_blkptr, ==,
744 ((blkptr_t *)db->db_parent->db.db_data +
745 db->db_blkid % epb));
749 if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) &&
750 (db->db_buf == NULL || db->db_buf->b_data) &&
751 db->db.db_data && db->db_blkid != DMU_BONUS_BLKID &&
752 db->db_state != DB_FILL && !dn->dn_free_txg) {
754 * If the blkptr isn't set but they have nonzero data,
755 * it had better be dirty, otherwise we'll lose that
756 * data when we evict this buffer.
758 * There is an exception to this rule for indirect blocks; in
759 * this case, if the indirect block is a hole, we fill in a few
760 * fields on each of the child blocks (importantly, birth time)
761 * to prevent hole birth times from being lost when you
762 * partially fill in a hole.
764 if (db->db_dirtycnt == 0) {
765 if (db->db_level == 0) {
766 uint64_t *buf = db->db.db_data;
769 for (i = 0; i < db->db.db_size >> 3; i++) {
773 blkptr_t *bps = db->db.db_data;
774 ASSERT3U(1 << DB_DNODE(db)->dn_indblkshift, ==,
777 * We want to verify that all the blkptrs in the
778 * indirect block are holes, but we may have
779 * automatically set up a few fields for them.
780 * We iterate through each blkptr and verify
781 * they only have those fields set.
784 i < db->db.db_size / sizeof (blkptr_t);
786 blkptr_t *bp = &bps[i];
787 ASSERT(ZIO_CHECKSUM_IS_ZERO(
790 DVA_IS_EMPTY(&bp->blk_dva[0]) &&
791 DVA_IS_EMPTY(&bp->blk_dva[1]) &&
792 DVA_IS_EMPTY(&bp->blk_dva[2]));
793 ASSERT0(bp->blk_fill);
794 ASSERT0(bp->blk_pad[0]);
795 ASSERT0(bp->blk_pad[1]);
796 ASSERT(!BP_IS_EMBEDDED(bp));
797 ASSERT(BP_IS_HOLE(bp));
798 ASSERT0(bp->blk_phys_birth);
808 dbuf_clear_data(dmu_buf_impl_t *db)
810 ASSERT(MUTEX_HELD(&db->db_mtx));
812 ASSERT3P(db->db_buf, ==, NULL);
813 db->db.db_data = NULL;
814 if (db->db_state != DB_NOFILL)
815 db->db_state = DB_UNCACHED;
819 dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf)
821 ASSERT(MUTEX_HELD(&db->db_mtx));
825 ASSERT(buf->b_data != NULL);
826 db->db.db_data = buf->b_data;
830 * Loan out an arc_buf for read. Return the loaned arc_buf.
833 dbuf_loan_arcbuf(dmu_buf_impl_t *db)
837 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
838 mutex_enter(&db->db_mtx);
839 if (arc_released(db->db_buf) || refcount_count(&db->db_holds) > 1) {
840 int blksz = db->db.db_size;
841 spa_t *spa = db->db_objset->os_spa;
843 mutex_exit(&db->db_mtx);
844 abuf = arc_loan_buf(spa, blksz);
845 bcopy(db->db.db_data, abuf->b_data, blksz);
848 arc_loan_inuse_buf(abuf, db);
851 mutex_exit(&db->db_mtx);
857 * Calculate which level n block references the data at the level 0 offset
861 dbuf_whichblock(dnode_t *dn, int64_t level, uint64_t offset)
863 if (dn->dn_datablkshift != 0 && dn->dn_indblkshift != 0) {
865 * The level n blkid is equal to the level 0 blkid divided by
866 * the number of level 0s in a level n block.
868 * The level 0 blkid is offset >> datablkshift =
869 * offset / 2^datablkshift.
871 * The number of level 0s in a level n is the number of block
872 * pointers in an indirect block, raised to the power of level.
873 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
874 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
876 * Thus, the level n blkid is: offset /
877 * ((2^datablkshift)*(2^(level*(indblkshift - SPA_BLKPTRSHIFT)))
878 * = offset / 2^(datablkshift + level *
879 * (indblkshift - SPA_BLKPTRSHIFT))
880 * = offset >> (datablkshift + level *
881 * (indblkshift - SPA_BLKPTRSHIFT))
883 return (offset >> (dn->dn_datablkshift + level *
884 (dn->dn_indblkshift - SPA_BLKPTRSHIFT)));
886 ASSERT3U(offset, <, dn->dn_datablksz);
892 dbuf_read_done(zio_t *zio, arc_buf_t *buf, void *vdb)
894 dmu_buf_impl_t *db = vdb;
896 mutex_enter(&db->db_mtx);
897 ASSERT3U(db->db_state, ==, DB_READ);
899 * All reads are synchronous, so we must have a hold on the dbuf
901 ASSERT(refcount_count(&db->db_holds) > 0);
902 ASSERT(db->db_buf == NULL);
903 ASSERT(db->db.db_data == NULL);
904 if (db->db_level == 0 && db->db_freed_in_flight) {
905 /* we were freed in flight; disregard any error */
906 arc_release(buf, db);
907 bzero(buf->b_data, db->db.db_size);
909 db->db_freed_in_flight = FALSE;
910 dbuf_set_data(db, buf);
911 db->db_state = DB_CACHED;
912 } else if (zio == NULL || zio->io_error == 0) {
913 dbuf_set_data(db, buf);
914 db->db_state = DB_CACHED;
916 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
917 ASSERT3P(db->db_buf, ==, NULL);
918 arc_buf_destroy(buf, db);
919 db->db_state = DB_UNCACHED;
921 cv_broadcast(&db->db_changed);
922 dbuf_rele_and_unlock(db, NULL);
926 dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
930 arc_flags_t aflags = ARC_FLAG_NOWAIT;
934 ASSERT(!refcount_is_zero(&db->db_holds));
935 /* We need the struct_rwlock to prevent db_blkptr from changing. */
936 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
937 ASSERT(MUTEX_HELD(&db->db_mtx));
938 ASSERT(db->db_state == DB_UNCACHED);
939 ASSERT(db->db_buf == NULL);
941 if (db->db_blkid == DMU_BONUS_BLKID) {
942 int bonuslen = MIN(dn->dn_bonuslen, dn->dn_phys->dn_bonuslen);
944 ASSERT3U(bonuslen, <=, db->db.db_size);
945 db->db.db_data = zio_buf_alloc(DN_MAX_BONUSLEN);
946 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
947 if (bonuslen < DN_MAX_BONUSLEN)
948 bzero(db->db.db_data, DN_MAX_BONUSLEN);
950 bcopy(DN_BONUS(dn->dn_phys), db->db.db_data, bonuslen);
952 db->db_state = DB_CACHED;
953 mutex_exit(&db->db_mtx);
958 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
959 * processes the delete record and clears the bp while we are waiting
960 * for the dn_mtx (resulting in a "no" from block_freed).
962 if (db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr) ||
963 (db->db_level == 0 && (dnode_block_freed(dn, db->db_blkid) ||
964 BP_IS_HOLE(db->db_blkptr)))) {
965 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
967 dbuf_set_data(db, arc_alloc_buf(db->db_objset->os_spa,
968 db->db.db_size, db, type));
969 bzero(db->db.db_data, db->db.db_size);
971 if (db->db_blkptr != NULL && db->db_level > 0 &&
972 BP_IS_HOLE(db->db_blkptr) &&
973 db->db_blkptr->blk_birth != 0) {
974 blkptr_t *bps = db->db.db_data;
975 for (int i = 0; i < ((1 <<
976 DB_DNODE(db)->dn_indblkshift) / sizeof (blkptr_t));
978 blkptr_t *bp = &bps[i];
979 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
980 1 << dn->dn_indblkshift);
982 BP_GET_LEVEL(db->db_blkptr) == 1 ?
984 BP_GET_LSIZE(db->db_blkptr));
985 BP_SET_TYPE(bp, BP_GET_TYPE(db->db_blkptr));
987 BP_GET_LEVEL(db->db_blkptr) - 1);
988 BP_SET_BIRTH(bp, db->db_blkptr->blk_birth, 0);
992 db->db_state = DB_CACHED;
993 mutex_exit(&db->db_mtx);
999 db->db_state = DB_READ;
1000 mutex_exit(&db->db_mtx);
1002 if (DBUF_IS_L2CACHEABLE(db))
1003 aflags |= ARC_FLAG_L2CACHE;
1005 SET_BOOKMARK(&zb, db->db_objset->os_dsl_dataset ?
1006 db->db_objset->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1007 db->db.db_object, db->db_level, db->db_blkid);
1009 dbuf_add_ref(db, NULL);
1011 (void) arc_read(zio, db->db_objset->os_spa, db->db_blkptr,
1012 dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ,
1013 (flags & DB_RF_CANFAIL) ? ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED,
1018 dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
1021 boolean_t havepzio = (zio != NULL);
1026 * We don't have to hold the mutex to check db_state because it
1027 * can't be freed while we have a hold on the buffer.
1029 ASSERT(!refcount_is_zero(&db->db_holds));
1031 if (db->db_state == DB_NOFILL)
1032 return (SET_ERROR(EIO));
1036 if ((flags & DB_RF_HAVESTRUCT) == 0)
1037 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1039 prefetch = db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1040 (flags & DB_RF_NOPREFETCH) == 0 && dn != NULL &&
1041 DBUF_IS_CACHEABLE(db);
1043 mutex_enter(&db->db_mtx);
1044 if (db->db_state == DB_CACHED) {
1045 mutex_exit(&db->db_mtx);
1047 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE);
1048 if ((flags & DB_RF_HAVESTRUCT) == 0)
1049 rw_exit(&dn->dn_struct_rwlock);
1051 } else if (db->db_state == DB_UNCACHED) {
1052 spa_t *spa = dn->dn_objset->os_spa;
1055 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
1056 dbuf_read_impl(db, zio, flags);
1058 /* dbuf_read_impl has dropped db_mtx for us */
1061 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE);
1063 if ((flags & DB_RF_HAVESTRUCT) == 0)
1064 rw_exit(&dn->dn_struct_rwlock);
1068 err = zio_wait(zio);
1071 * Another reader came in while the dbuf was in flight
1072 * between UNCACHED and CACHED. Either a writer will finish
1073 * writing the buffer (sending the dbuf to CACHED) or the
1074 * first reader's request will reach the read_done callback
1075 * and send the dbuf to CACHED. Otherwise, a failure
1076 * occurred and the dbuf went to UNCACHED.
1078 mutex_exit(&db->db_mtx);
1080 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE);
1081 if ((flags & DB_RF_HAVESTRUCT) == 0)
1082 rw_exit(&dn->dn_struct_rwlock);
1085 /* Skip the wait per the caller's request. */
1086 mutex_enter(&db->db_mtx);
1087 if ((flags & DB_RF_NEVERWAIT) == 0) {
1088 while (db->db_state == DB_READ ||
1089 db->db_state == DB_FILL) {
1090 ASSERT(db->db_state == DB_READ ||
1091 (flags & DB_RF_HAVESTRUCT) == 0);
1092 DTRACE_PROBE2(blocked__read, dmu_buf_impl_t *,
1094 cv_wait(&db->db_changed, &db->db_mtx);
1096 if (db->db_state == DB_UNCACHED)
1097 err = SET_ERROR(EIO);
1099 mutex_exit(&db->db_mtx);
1102 ASSERT(err || havepzio || db->db_state == DB_CACHED);
1107 dbuf_noread(dmu_buf_impl_t *db)
1109 ASSERT(!refcount_is_zero(&db->db_holds));
1110 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1111 mutex_enter(&db->db_mtx);
1112 while (db->db_state == DB_READ || db->db_state == DB_FILL)
1113 cv_wait(&db->db_changed, &db->db_mtx);
1114 if (db->db_state == DB_UNCACHED) {
1115 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1116 spa_t *spa = db->db_objset->os_spa;
1118 ASSERT(db->db_buf == NULL);
1119 ASSERT(db->db.db_data == NULL);
1120 dbuf_set_data(db, arc_alloc_buf(spa, db->db.db_size, db, type));
1121 db->db_state = DB_FILL;
1122 } else if (db->db_state == DB_NOFILL) {
1123 dbuf_clear_data(db);
1125 ASSERT3U(db->db_state, ==, DB_CACHED);
1127 mutex_exit(&db->db_mtx);
1131 * This is our just-in-time copy function. It makes a copy of
1132 * buffers, that have been modified in a previous transaction
1133 * group, before we modify them in the current active group.
1135 * This function is used in two places: when we are dirtying a
1136 * buffer for the first time in a txg, and when we are freeing
1137 * a range in a dnode that includes this buffer.
1139 * Note that when we are called from dbuf_free_range() we do
1140 * not put a hold on the buffer, we just traverse the active
1141 * dbuf list for the dnode.
1144 dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg)
1146 dbuf_dirty_record_t *dr = db->db_last_dirty;
1148 ASSERT(MUTEX_HELD(&db->db_mtx));
1149 ASSERT(db->db.db_data != NULL);
1150 ASSERT(db->db_level == 0);
1151 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT);
1154 (dr->dt.dl.dr_data !=
1155 ((db->db_blkid == DMU_BONUS_BLKID) ? db->db.db_data : db->db_buf)))
1159 * If the last dirty record for this dbuf has not yet synced
1160 * and its referencing the dbuf data, either:
1161 * reset the reference to point to a new copy,
1162 * or (if there a no active holders)
1163 * just null out the current db_data pointer.
1165 ASSERT(dr->dr_txg >= txg - 2);
1166 if (db->db_blkid == DMU_BONUS_BLKID) {
1167 /* Note that the data bufs here are zio_bufs */
1168 dr->dt.dl.dr_data = zio_buf_alloc(DN_MAX_BONUSLEN);
1169 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
1170 bcopy(db->db.db_data, dr->dt.dl.dr_data, DN_MAX_BONUSLEN);
1171 } else if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
1172 int size = db->db.db_size;
1173 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1174 spa_t *spa = db->db_objset->os_spa;
1176 dr->dt.dl.dr_data = arc_alloc_buf(spa, size, db, type);
1177 bcopy(db->db.db_data, dr->dt.dl.dr_data->b_data, size);
1180 dbuf_clear_data(db);
1185 dbuf_unoverride(dbuf_dirty_record_t *dr)
1187 dmu_buf_impl_t *db = dr->dr_dbuf;
1188 blkptr_t *bp = &dr->dt.dl.dr_overridden_by;
1189 uint64_t txg = dr->dr_txg;
1191 ASSERT(MUTEX_HELD(&db->db_mtx));
1192 ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC);
1193 ASSERT(db->db_level == 0);
1195 if (db->db_blkid == DMU_BONUS_BLKID ||
1196 dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN)
1199 ASSERT(db->db_data_pending != dr);
1201 /* free this block */
1202 if (!BP_IS_HOLE(bp) && !dr->dt.dl.dr_nopwrite)
1203 zio_free(db->db_objset->os_spa, txg, bp);
1205 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
1206 dr->dt.dl.dr_nopwrite = B_FALSE;
1209 * Release the already-written buffer, so we leave it in
1210 * a consistent dirty state. Note that all callers are
1211 * modifying the buffer, so they will immediately do
1212 * another (redundant) arc_release(). Therefore, leave
1213 * the buf thawed to save the effort of freezing &
1214 * immediately re-thawing it.
1216 arc_release(dr->dt.dl.dr_data, db);
1220 * Evict (if its unreferenced) or clear (if its referenced) any level-0
1221 * data blocks in the free range, so that any future readers will find
1224 * This is a no-op if the dataset is in the middle of an incremental
1225 * receive; see comment below for details.
1228 dbuf_free_range(dnode_t *dn, uint64_t start_blkid, uint64_t end_blkid,
1231 dmu_buf_impl_t db_search;
1232 dmu_buf_impl_t *db, *db_next;
1233 uint64_t txg = tx->tx_txg;
1235 boolean_t freespill =
1236 (start_blkid == DMU_SPILL_BLKID || end_blkid == DMU_SPILL_BLKID);
1238 if (end_blkid > dn->dn_maxblkid && !freespill)
1239 end_blkid = dn->dn_maxblkid;
1240 dprintf_dnode(dn, "start=%llu end=%llu\n", start_blkid, end_blkid);
1242 db_search.db_level = 0;
1243 db_search.db_blkid = start_blkid;
1244 db_search.db_state = DB_SEARCH;
1246 mutex_enter(&dn->dn_dbufs_mtx);
1247 if (start_blkid >= dn->dn_unlisted_l0_blkid && !freespill) {
1248 /* There can't be any dbufs in this range; no need to search. */
1250 db = avl_find(&dn->dn_dbufs, &db_search, &where);
1251 ASSERT3P(db, ==, NULL);
1252 db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER);
1253 ASSERT(db == NULL || db->db_level > 0);
1255 mutex_exit(&dn->dn_dbufs_mtx);
1257 } else if (dmu_objset_is_receiving(dn->dn_objset)) {
1259 * If we are receiving, we expect there to be no dbufs in
1260 * the range to be freed, because receive modifies each
1261 * block at most once, and in offset order. If this is
1262 * not the case, it can lead to performance problems,
1263 * so note that we unexpectedly took the slow path.
1265 atomic_inc_64(&zfs_free_range_recv_miss);
1268 db = avl_find(&dn->dn_dbufs, &db_search, &where);
1269 ASSERT3P(db, ==, NULL);
1270 db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER);
1272 for (; db != NULL; db = db_next) {
1273 db_next = AVL_NEXT(&dn->dn_dbufs, db);
1274 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1276 if (db->db_level != 0 || db->db_blkid > end_blkid) {
1279 ASSERT3U(db->db_blkid, >=, start_blkid);
1281 /* found a level 0 buffer in the range */
1282 mutex_enter(&db->db_mtx);
1283 if (dbuf_undirty(db, tx)) {
1284 /* mutex has been dropped and dbuf destroyed */
1288 if (db->db_state == DB_UNCACHED ||
1289 db->db_state == DB_NOFILL ||
1290 db->db_state == DB_EVICTING) {
1291 ASSERT(db->db.db_data == NULL);
1292 mutex_exit(&db->db_mtx);
1295 if (db->db_state == DB_READ || db->db_state == DB_FILL) {
1296 /* will be handled in dbuf_read_done or dbuf_rele */
1297 db->db_freed_in_flight = TRUE;
1298 mutex_exit(&db->db_mtx);
1301 if (refcount_count(&db->db_holds) == 0) {
1306 /* The dbuf is referenced */
1308 if (db->db_last_dirty != NULL) {
1309 dbuf_dirty_record_t *dr = db->db_last_dirty;
1311 if (dr->dr_txg == txg) {
1313 * This buffer is "in-use", re-adjust the file
1314 * size to reflect that this buffer may
1315 * contain new data when we sync.
1317 if (db->db_blkid != DMU_SPILL_BLKID &&
1318 db->db_blkid > dn->dn_maxblkid)
1319 dn->dn_maxblkid = db->db_blkid;
1320 dbuf_unoverride(dr);
1323 * This dbuf is not dirty in the open context.
1324 * Either uncache it (if its not referenced in
1325 * the open context) or reset its contents to
1328 dbuf_fix_old_data(db, txg);
1331 /* clear the contents if its cached */
1332 if (db->db_state == DB_CACHED) {
1333 ASSERT(db->db.db_data != NULL);
1334 arc_release(db->db_buf, db);
1335 bzero(db->db.db_data, db->db.db_size);
1336 arc_buf_freeze(db->db_buf);
1339 mutex_exit(&db->db_mtx);
1341 mutex_exit(&dn->dn_dbufs_mtx);
1345 dbuf_block_freeable(dmu_buf_impl_t *db)
1347 dsl_dataset_t *ds = db->db_objset->os_dsl_dataset;
1348 uint64_t birth_txg = 0;
1351 * We don't need any locking to protect db_blkptr:
1352 * If it's syncing, then db_last_dirty will be set
1353 * so we'll ignore db_blkptr.
1355 * This logic ensures that only block births for
1356 * filled blocks are considered.
1358 ASSERT(MUTEX_HELD(&db->db_mtx));
1359 if (db->db_last_dirty && (db->db_blkptr == NULL ||
1360 !BP_IS_HOLE(db->db_blkptr))) {
1361 birth_txg = db->db_last_dirty->dr_txg;
1362 } else if (db->db_blkptr != NULL && !BP_IS_HOLE(db->db_blkptr)) {
1363 birth_txg = db->db_blkptr->blk_birth;
1367 * If this block don't exist or is in a snapshot, it can't be freed.
1368 * Don't pass the bp to dsl_dataset_block_freeable() since we
1369 * are holding the db_mtx lock and might deadlock if we are
1370 * prefetching a dedup-ed block.
1373 return (ds == NULL ||
1374 dsl_dataset_block_freeable(ds, NULL, birth_txg));
1380 dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx)
1382 arc_buf_t *buf, *obuf;
1383 int osize = db->db.db_size;
1384 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1387 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1392 /* XXX does *this* func really need the lock? */
1393 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
1396 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
1397 * is OK, because there can be no other references to the db
1398 * when we are changing its size, so no concurrent DB_FILL can
1402 * XXX we should be doing a dbuf_read, checking the return
1403 * value and returning that up to our callers
1405 dmu_buf_will_dirty(&db->db, tx);
1407 /* create the data buffer for the new block */
1408 buf = arc_alloc_buf(dn->dn_objset->os_spa, size, db, type);
1410 /* copy old block data to the new block */
1412 bcopy(obuf->b_data, buf->b_data, MIN(osize, size));
1413 /* zero the remainder */
1415 bzero((uint8_t *)buf->b_data + osize, size - osize);
1417 mutex_enter(&db->db_mtx);
1418 dbuf_set_data(db, buf);
1419 arc_buf_destroy(obuf, db);
1420 db->db.db_size = size;
1422 if (db->db_level == 0) {
1423 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
1424 db->db_last_dirty->dt.dl.dr_data = buf;
1426 mutex_exit(&db->db_mtx);
1428 dnode_willuse_space(dn, size-osize, tx);
1433 dbuf_release_bp(dmu_buf_impl_t *db)
1435 objset_t *os = db->db_objset;
1437 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
1438 ASSERT(arc_released(os->os_phys_buf) ||
1439 list_link_active(&os->os_dsl_dataset->ds_synced_link));
1440 ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf));
1442 (void) arc_release(db->db_buf, db);
1446 * We already have a dirty record for this TXG, and we are being
1450 dbuf_redirty(dbuf_dirty_record_t *dr)
1452 dmu_buf_impl_t *db = dr->dr_dbuf;
1454 ASSERT(MUTEX_HELD(&db->db_mtx));
1456 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID) {
1458 * If this buffer has already been written out,
1459 * we now need to reset its state.
1461 dbuf_unoverride(dr);
1462 if (db->db.db_object != DMU_META_DNODE_OBJECT &&
1463 db->db_state != DB_NOFILL) {
1464 /* Already released on initial dirty, so just thaw. */
1465 ASSERT(arc_released(db->db_buf));
1466 arc_buf_thaw(db->db_buf);
1471 dbuf_dirty_record_t *
1472 dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1476 dbuf_dirty_record_t **drp, *dr;
1477 int drop_struct_lock = FALSE;
1478 boolean_t do_free_accounting = B_FALSE;
1479 int txgoff = tx->tx_txg & TXG_MASK;
1481 ASSERT(tx->tx_txg != 0);
1482 ASSERT(!refcount_is_zero(&db->db_holds));
1483 DMU_TX_DIRTY_BUF(tx, db);
1488 * Shouldn't dirty a regular buffer in syncing context. Private
1489 * objects may be dirtied in syncing context, but only if they
1490 * were already pre-dirtied in open context.
1493 if (dn->dn_objset->os_dsl_dataset != NULL) {
1494 rrw_enter(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock,
1497 ASSERT(!dmu_tx_is_syncing(tx) ||
1498 BP_IS_HOLE(dn->dn_objset->os_rootbp) ||
1499 DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1500 dn->dn_objset->os_dsl_dataset == NULL);
1501 if (dn->dn_objset->os_dsl_dataset != NULL)
1502 rrw_exit(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock, FTAG);
1505 * We make this assert for private objects as well, but after we
1506 * check if we're already dirty. They are allowed to re-dirty
1507 * in syncing context.
1509 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1510 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1511 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1513 mutex_enter(&db->db_mtx);
1515 * XXX make this true for indirects too? The problem is that
1516 * transactions created with dmu_tx_create_assigned() from
1517 * syncing context don't bother holding ahead.
1519 ASSERT(db->db_level != 0 ||
1520 db->db_state == DB_CACHED || db->db_state == DB_FILL ||
1521 db->db_state == DB_NOFILL);
1523 mutex_enter(&dn->dn_mtx);
1525 * Don't set dirtyctx to SYNC if we're just modifying this as we
1526 * initialize the objset.
1528 if (dn->dn_dirtyctx == DN_UNDIRTIED) {
1529 if (dn->dn_objset->os_dsl_dataset != NULL) {
1530 rrw_enter(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock,
1533 if (!BP_IS_HOLE(dn->dn_objset->os_rootbp)) {
1534 dn->dn_dirtyctx = (dmu_tx_is_syncing(tx) ?
1535 DN_DIRTY_SYNC : DN_DIRTY_OPEN);
1536 ASSERT(dn->dn_dirtyctx_firstset == NULL);
1537 dn->dn_dirtyctx_firstset = kmem_alloc(1, KM_SLEEP);
1539 if (dn->dn_objset->os_dsl_dataset != NULL) {
1540 rrw_exit(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock,
1544 mutex_exit(&dn->dn_mtx);
1546 if (db->db_blkid == DMU_SPILL_BLKID)
1547 dn->dn_have_spill = B_TRUE;
1550 * If this buffer is already dirty, we're done.
1552 drp = &db->db_last_dirty;
1553 ASSERT(*drp == NULL || (*drp)->dr_txg <= tx->tx_txg ||
1554 db->db.db_object == DMU_META_DNODE_OBJECT);
1555 while ((dr = *drp) != NULL && dr->dr_txg > tx->tx_txg)
1557 if (dr && dr->dr_txg == tx->tx_txg) {
1561 mutex_exit(&db->db_mtx);
1566 * Only valid if not already dirty.
1568 ASSERT(dn->dn_object == 0 ||
1569 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1570 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1572 ASSERT3U(dn->dn_nlevels, >, db->db_level);
1573 ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) ||
1574 dn->dn_phys->dn_nlevels > db->db_level ||
1575 dn->dn_next_nlevels[txgoff] > db->db_level ||
1576 dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level ||
1577 dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level);
1580 * We should only be dirtying in syncing context if it's the
1581 * mos or we're initializing the os or it's a special object.
1582 * However, we are allowed to dirty in syncing context provided
1583 * we already dirtied it in open context. Hence we must make
1584 * this assertion only if we're not already dirty.
1588 if (dn->dn_objset->os_dsl_dataset != NULL)
1589 rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_READER, FTAG);
1590 ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1591 os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp));
1592 if (dn->dn_objset->os_dsl_dataset != NULL)
1593 rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG);
1595 ASSERT(db->db.db_size != 0);
1597 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1599 if (db->db_blkid != DMU_BONUS_BLKID) {
1601 * Update the accounting.
1602 * Note: we delay "free accounting" until after we drop
1603 * the db_mtx. This keeps us from grabbing other locks
1604 * (and possibly deadlocking) in bp_get_dsize() while
1605 * also holding the db_mtx.
1607 dnode_willuse_space(dn, db->db.db_size, tx);
1608 do_free_accounting = dbuf_block_freeable(db);
1612 * If this buffer is dirty in an old transaction group we need
1613 * to make a copy of it so that the changes we make in this
1614 * transaction group won't leak out when we sync the older txg.
1616 dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP);
1617 if (db->db_level == 0) {
1618 void *data_old = db->db_buf;
1620 if (db->db_state != DB_NOFILL) {
1621 if (db->db_blkid == DMU_BONUS_BLKID) {
1622 dbuf_fix_old_data(db, tx->tx_txg);
1623 data_old = db->db.db_data;
1624 } else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
1626 * Release the data buffer from the cache so
1627 * that we can modify it without impacting
1628 * possible other users of this cached data
1629 * block. Note that indirect blocks and
1630 * private objects are not released until the
1631 * syncing state (since they are only modified
1634 arc_release(db->db_buf, db);
1635 dbuf_fix_old_data(db, tx->tx_txg);
1636 data_old = db->db_buf;
1638 ASSERT(data_old != NULL);
1640 dr->dt.dl.dr_data = data_old;
1642 mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_DEFAULT, NULL);
1643 list_create(&dr->dt.di.dr_children,
1644 sizeof (dbuf_dirty_record_t),
1645 offsetof(dbuf_dirty_record_t, dr_dirty_node));
1647 if (db->db_blkid != DMU_BONUS_BLKID && os->os_dsl_dataset != NULL)
1648 dr->dr_accounted = db->db.db_size;
1650 dr->dr_txg = tx->tx_txg;
1655 * We could have been freed_in_flight between the dbuf_noread
1656 * and dbuf_dirty. We win, as though the dbuf_noread() had
1657 * happened after the free.
1659 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1660 db->db_blkid != DMU_SPILL_BLKID) {
1661 mutex_enter(&dn->dn_mtx);
1662 if (dn->dn_free_ranges[txgoff] != NULL) {
1663 range_tree_clear(dn->dn_free_ranges[txgoff],
1666 mutex_exit(&dn->dn_mtx);
1667 db->db_freed_in_flight = FALSE;
1671 * This buffer is now part of this txg
1673 dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg);
1674 db->db_dirtycnt += 1;
1675 ASSERT3U(db->db_dirtycnt, <=, 3);
1677 mutex_exit(&db->db_mtx);
1679 if (db->db_blkid == DMU_BONUS_BLKID ||
1680 db->db_blkid == DMU_SPILL_BLKID) {
1681 mutex_enter(&dn->dn_mtx);
1682 ASSERT(!list_link_active(&dr->dr_dirty_node));
1683 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1684 mutex_exit(&dn->dn_mtx);
1685 dnode_setdirty(dn, tx);
1691 * The dn_struct_rwlock prevents db_blkptr from changing
1692 * due to a write from syncing context completing
1693 * while we are running, so we want to acquire it before
1694 * looking at db_blkptr.
1696 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
1697 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1698 drop_struct_lock = TRUE;
1701 if (do_free_accounting) {
1702 blkptr_t *bp = db->db_blkptr;
1703 int64_t willfree = (bp && !BP_IS_HOLE(bp)) ?
1704 bp_get_dsize(os->os_spa, bp) : db->db.db_size;
1706 * This is only a guess -- if the dbuf is dirty
1707 * in a previous txg, we don't know how much
1708 * space it will use on disk yet. We should
1709 * really have the struct_rwlock to access
1710 * db_blkptr, but since this is just a guess,
1711 * it's OK if we get an odd answer.
1713 ddt_prefetch(os->os_spa, bp);
1714 dnode_willuse_space(dn, -willfree, tx);
1717 if (db->db_level == 0) {
1718 dnode_new_blkid(dn, db->db_blkid, tx, drop_struct_lock);
1719 ASSERT(dn->dn_maxblkid >= db->db_blkid);
1722 if (db->db_level+1 < dn->dn_nlevels) {
1723 dmu_buf_impl_t *parent = db->db_parent;
1724 dbuf_dirty_record_t *di;
1725 int parent_held = FALSE;
1727 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
1728 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1730 parent = dbuf_hold_level(dn, db->db_level+1,
1731 db->db_blkid >> epbs, FTAG);
1732 ASSERT(parent != NULL);
1735 if (drop_struct_lock)
1736 rw_exit(&dn->dn_struct_rwlock);
1737 ASSERT3U(db->db_level+1, ==, parent->db_level);
1738 di = dbuf_dirty(parent, tx);
1740 dbuf_rele(parent, FTAG);
1742 mutex_enter(&db->db_mtx);
1744 * Since we've dropped the mutex, it's possible that
1745 * dbuf_undirty() might have changed this out from under us.
1747 if (db->db_last_dirty == dr ||
1748 dn->dn_object == DMU_META_DNODE_OBJECT) {
1749 mutex_enter(&di->dt.di.dr_mtx);
1750 ASSERT3U(di->dr_txg, ==, tx->tx_txg);
1751 ASSERT(!list_link_active(&dr->dr_dirty_node));
1752 list_insert_tail(&di->dt.di.dr_children, dr);
1753 mutex_exit(&di->dt.di.dr_mtx);
1756 mutex_exit(&db->db_mtx);
1758 ASSERT(db->db_level+1 == dn->dn_nlevels);
1759 ASSERT(db->db_blkid < dn->dn_nblkptr);
1760 ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf);
1761 mutex_enter(&dn->dn_mtx);
1762 ASSERT(!list_link_active(&dr->dr_dirty_node));
1763 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1764 mutex_exit(&dn->dn_mtx);
1765 if (drop_struct_lock)
1766 rw_exit(&dn->dn_struct_rwlock);
1769 dnode_setdirty(dn, tx);
1775 * Undirty a buffer in the transaction group referenced by the given
1776 * transaction. Return whether this evicted the dbuf.
1779 dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1782 uint64_t txg = tx->tx_txg;
1783 dbuf_dirty_record_t *dr, **drp;
1788 * Due to our use of dn_nlevels below, this can only be called
1789 * in open context, unless we are operating on the MOS.
1790 * From syncing context, dn_nlevels may be different from the
1791 * dn_nlevels used when dbuf was dirtied.
1793 ASSERT(db->db_objset ==
1794 dmu_objset_pool(db->db_objset)->dp_meta_objset ||
1795 txg != spa_syncing_txg(dmu_objset_spa(db->db_objset)));
1796 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1797 ASSERT0(db->db_level);
1798 ASSERT(MUTEX_HELD(&db->db_mtx));
1801 * If this buffer is not dirty, we're done.
1803 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1804 if (dr->dr_txg <= txg)
1806 if (dr == NULL || dr->dr_txg < txg)
1808 ASSERT(dr->dr_txg == txg);
1809 ASSERT(dr->dr_dbuf == db);
1814 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1816 ASSERT(db->db.db_size != 0);
1818 dsl_pool_undirty_space(dmu_objset_pool(dn->dn_objset),
1819 dr->dr_accounted, txg);
1824 * Note that there are three places in dbuf_dirty()
1825 * where this dirty record may be put on a list.
1826 * Make sure to do a list_remove corresponding to
1827 * every one of those list_insert calls.
1829 if (dr->dr_parent) {
1830 mutex_enter(&dr->dr_parent->dt.di.dr_mtx);
1831 list_remove(&dr->dr_parent->dt.di.dr_children, dr);
1832 mutex_exit(&dr->dr_parent->dt.di.dr_mtx);
1833 } else if (db->db_blkid == DMU_SPILL_BLKID ||
1834 db->db_level + 1 == dn->dn_nlevels) {
1835 ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf);
1836 mutex_enter(&dn->dn_mtx);
1837 list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr);
1838 mutex_exit(&dn->dn_mtx);
1842 if (db->db_state != DB_NOFILL) {
1843 dbuf_unoverride(dr);
1845 ASSERT(db->db_buf != NULL);
1846 ASSERT(dr->dt.dl.dr_data != NULL);
1847 if (dr->dt.dl.dr_data != db->db_buf)
1848 arc_buf_destroy(dr->dt.dl.dr_data, db);
1851 kmem_free(dr, sizeof (dbuf_dirty_record_t));
1853 ASSERT(db->db_dirtycnt > 0);
1854 db->db_dirtycnt -= 1;
1856 if (refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) {
1857 ASSERT(db->db_state == DB_NOFILL || arc_released(db->db_buf));
1866 dmu_buf_will_dirty(dmu_buf_t *db_fake, dmu_tx_t *tx)
1868 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1869 int rf = DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH;
1871 ASSERT(tx->tx_txg != 0);
1872 ASSERT(!refcount_is_zero(&db->db_holds));
1875 * Quick check for dirtyness. For already dirty blocks, this
1876 * reduces runtime of this function by >90%, and overall performance
1877 * by 50% for some workloads (e.g. file deletion with indirect blocks
1880 mutex_enter(&db->db_mtx);
1881 dbuf_dirty_record_t *dr;
1882 for (dr = db->db_last_dirty;
1883 dr != NULL && dr->dr_txg >= tx->tx_txg; dr = dr->dr_next) {
1885 * It's possible that it is already dirty but not cached,
1886 * because there are some calls to dbuf_dirty() that don't
1887 * go through dmu_buf_will_dirty().
1889 if (dr->dr_txg == tx->tx_txg && db->db_state == DB_CACHED) {
1890 /* This dbuf is already dirty and cached. */
1892 mutex_exit(&db->db_mtx);
1896 mutex_exit(&db->db_mtx);
1899 if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock))
1900 rf |= DB_RF_HAVESTRUCT;
1902 (void) dbuf_read(db, NULL, rf);
1903 (void) dbuf_dirty(db, tx);
1907 dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1909 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1911 db->db_state = DB_NOFILL;
1913 dmu_buf_will_fill(db_fake, tx);
1917 dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1919 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1921 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1922 ASSERT(tx->tx_txg != 0);
1923 ASSERT(db->db_level == 0);
1924 ASSERT(!refcount_is_zero(&db->db_holds));
1926 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
1927 dmu_tx_private_ok(tx));
1930 (void) dbuf_dirty(db, tx);
1933 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1936 dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx)
1938 mutex_enter(&db->db_mtx);
1941 if (db->db_state == DB_FILL) {
1942 if (db->db_level == 0 && db->db_freed_in_flight) {
1943 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1944 /* we were freed while filling */
1945 /* XXX dbuf_undirty? */
1946 bzero(db->db.db_data, db->db.db_size);
1947 db->db_freed_in_flight = FALSE;
1949 db->db_state = DB_CACHED;
1950 cv_broadcast(&db->db_changed);
1952 mutex_exit(&db->db_mtx);
1956 dmu_buf_write_embedded(dmu_buf_t *dbuf, void *data,
1957 bp_embedded_type_t etype, enum zio_compress comp,
1958 int uncompressed_size, int compressed_size, int byteorder,
1961 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
1962 struct dirty_leaf *dl;
1963 dmu_object_type_t type;
1965 if (etype == BP_EMBEDDED_TYPE_DATA) {
1966 ASSERT(spa_feature_is_active(dmu_objset_spa(db->db_objset),
1967 SPA_FEATURE_EMBEDDED_DATA));
1971 type = DB_DNODE(db)->dn_type;
1974 ASSERT0(db->db_level);
1975 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1977 dmu_buf_will_not_fill(dbuf, tx);
1979 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
1980 dl = &db->db_last_dirty->dt.dl;
1981 encode_embedded_bp_compressed(&dl->dr_overridden_by,
1982 data, comp, uncompressed_size, compressed_size);
1983 BPE_SET_ETYPE(&dl->dr_overridden_by, etype);
1984 BP_SET_TYPE(&dl->dr_overridden_by, type);
1985 BP_SET_LEVEL(&dl->dr_overridden_by, 0);
1986 BP_SET_BYTEORDER(&dl->dr_overridden_by, byteorder);
1988 dl->dr_override_state = DR_OVERRIDDEN;
1989 dl->dr_overridden_by.blk_birth = db->db_last_dirty->dr_txg;
1993 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1994 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1997 dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx)
1999 ASSERT(!refcount_is_zero(&db->db_holds));
2000 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2001 ASSERT(db->db_level == 0);
2002 ASSERT(DBUF_GET_BUFC_TYPE(db) == ARC_BUFC_DATA);
2003 ASSERT(buf != NULL);
2004 ASSERT(arc_buf_size(buf) == db->db.db_size);
2005 ASSERT(tx->tx_txg != 0);
2007 arc_return_buf(buf, db);
2008 ASSERT(arc_released(buf));
2010 mutex_enter(&db->db_mtx);
2012 while (db->db_state == DB_READ || db->db_state == DB_FILL)
2013 cv_wait(&db->db_changed, &db->db_mtx);
2015 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED);
2017 if (db->db_state == DB_CACHED &&
2018 refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) {
2019 mutex_exit(&db->db_mtx);
2020 (void) dbuf_dirty(db, tx);
2021 bcopy(buf->b_data, db->db.db_data, db->db.db_size);
2022 arc_buf_destroy(buf, db);
2023 xuio_stat_wbuf_copied();
2027 xuio_stat_wbuf_nocopy();
2028 if (db->db_state == DB_CACHED) {
2029 dbuf_dirty_record_t *dr = db->db_last_dirty;
2031 ASSERT(db->db_buf != NULL);
2032 if (dr != NULL && dr->dr_txg == tx->tx_txg) {
2033 ASSERT(dr->dt.dl.dr_data == db->db_buf);
2034 if (!arc_released(db->db_buf)) {
2035 ASSERT(dr->dt.dl.dr_override_state ==
2037 arc_release(db->db_buf, db);
2039 dr->dt.dl.dr_data = buf;
2040 arc_buf_destroy(db->db_buf, db);
2041 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) {
2042 arc_release(db->db_buf, db);
2043 arc_buf_destroy(db->db_buf, db);
2047 ASSERT(db->db_buf == NULL);
2048 dbuf_set_data(db, buf);
2049 db->db_state = DB_FILL;
2050 mutex_exit(&db->db_mtx);
2051 (void) dbuf_dirty(db, tx);
2052 dmu_buf_fill_done(&db->db, tx);
2056 dbuf_destroy(dmu_buf_impl_t *db)
2059 dmu_buf_impl_t *parent = db->db_parent;
2060 dmu_buf_impl_t *dndb;
2062 ASSERT(MUTEX_HELD(&db->db_mtx));
2063 ASSERT(refcount_is_zero(&db->db_holds));
2065 if (db->db_buf != NULL) {
2066 arc_buf_destroy(db->db_buf, db);
2070 if (db->db_blkid == DMU_BONUS_BLKID) {
2071 ASSERT(db->db.db_data != NULL);
2072 zio_buf_free(db->db.db_data, DN_MAX_BONUSLEN);
2073 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
2074 db->db_state = DB_UNCACHED;
2077 dbuf_clear_data(db);
2079 if (multilist_link_active(&db->db_cache_link)) {
2080 multilist_remove(&dbuf_cache, db);
2081 (void) refcount_remove_many(&dbuf_cache_size,
2082 db->db.db_size, db);
2085 ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL);
2086 ASSERT(db->db_data_pending == NULL);
2088 db->db_state = DB_EVICTING;
2089 db->db_blkptr = NULL;
2092 * Now that db_state is DB_EVICTING, nobody else can find this via
2093 * the hash table. We can now drop db_mtx, which allows us to
2094 * acquire the dn_dbufs_mtx.
2096 mutex_exit(&db->db_mtx);
2101 if (db->db_blkid != DMU_BONUS_BLKID) {
2102 boolean_t needlock = !MUTEX_HELD(&dn->dn_dbufs_mtx);
2104 mutex_enter(&dn->dn_dbufs_mtx);
2105 avl_remove(&dn->dn_dbufs, db);
2106 atomic_dec_32(&dn->dn_dbufs_count);
2110 mutex_exit(&dn->dn_dbufs_mtx);
2112 * Decrementing the dbuf count means that the hold corresponding
2113 * to the removed dbuf is no longer discounted in dnode_move(),
2114 * so the dnode cannot be moved until after we release the hold.
2115 * The membar_producer() ensures visibility of the decremented
2116 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
2120 db->db_dnode_handle = NULL;
2122 dbuf_hash_remove(db);
2127 ASSERT(refcount_is_zero(&db->db_holds));
2129 db->db_parent = NULL;
2131 ASSERT(db->db_buf == NULL);
2132 ASSERT(db->db.db_data == NULL);
2133 ASSERT(db->db_hash_next == NULL);
2134 ASSERT(db->db_blkptr == NULL);
2135 ASSERT(db->db_data_pending == NULL);
2136 ASSERT(!multilist_link_active(&db->db_cache_link));
2138 kmem_cache_free(dbuf_kmem_cache, db);
2139 arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
2142 * If this dbuf is referenced from an indirect dbuf,
2143 * decrement the ref count on the indirect dbuf.
2145 if (parent && parent != dndb)
2146 dbuf_rele(parent, db);
2150 * Note: While bpp will always be updated if the function returns success,
2151 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
2152 * this happens when the dnode is the meta-dnode, or a userused or groupused
2156 dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse,
2157 dmu_buf_impl_t **parentp, blkptr_t **bpp)
2164 ASSERT(blkid != DMU_BONUS_BLKID);
2166 if (blkid == DMU_SPILL_BLKID) {
2167 mutex_enter(&dn->dn_mtx);
2168 if (dn->dn_have_spill &&
2169 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
2170 *bpp = &dn->dn_phys->dn_spill;
2173 dbuf_add_ref(dn->dn_dbuf, NULL);
2174 *parentp = dn->dn_dbuf;
2175 mutex_exit(&dn->dn_mtx);
2179 if (dn->dn_phys->dn_nlevels == 0)
2182 nlevels = dn->dn_phys->dn_nlevels;
2184 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
2186 ASSERT3U(level * epbs, <, 64);
2187 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2188 if (level >= nlevels ||
2189 (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) {
2190 /* the buffer has no parent yet */
2191 return (SET_ERROR(ENOENT));
2192 } else if (level < nlevels-1) {
2193 /* this block is referenced from an indirect block */
2194 int err = dbuf_hold_impl(dn, level+1,
2195 blkid >> epbs, fail_sparse, FALSE, NULL, parentp);
2198 err = dbuf_read(*parentp, NULL,
2199 (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL));
2201 dbuf_rele(*parentp, NULL);
2205 *bpp = ((blkptr_t *)(*parentp)->db.db_data) +
2206 (blkid & ((1ULL << epbs) - 1));
2209 /* the block is referenced from the dnode */
2210 ASSERT3U(level, ==, nlevels-1);
2211 ASSERT(dn->dn_phys->dn_nblkptr == 0 ||
2212 blkid < dn->dn_phys->dn_nblkptr);
2214 dbuf_add_ref(dn->dn_dbuf, NULL);
2215 *parentp = dn->dn_dbuf;
2217 *bpp = &dn->dn_phys->dn_blkptr[blkid];
2222 static dmu_buf_impl_t *
2223 dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid,
2224 dmu_buf_impl_t *parent, blkptr_t *blkptr)
2226 objset_t *os = dn->dn_objset;
2227 dmu_buf_impl_t *db, *odb;
2229 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2230 ASSERT(dn->dn_type != DMU_OT_NONE);
2232 db = kmem_cache_alloc(dbuf_kmem_cache, KM_SLEEP);
2235 db->db.db_object = dn->dn_object;
2236 db->db_level = level;
2237 db->db_blkid = blkid;
2238 db->db_last_dirty = NULL;
2239 db->db_dirtycnt = 0;
2240 db->db_dnode_handle = dn->dn_handle;
2241 db->db_parent = parent;
2242 db->db_blkptr = blkptr;
2245 db->db_user_immediate_evict = FALSE;
2246 db->db_freed_in_flight = FALSE;
2247 db->db_pending_evict = FALSE;
2249 if (blkid == DMU_BONUS_BLKID) {
2250 ASSERT3P(parent, ==, dn->dn_dbuf);
2251 db->db.db_size = DN_MAX_BONUSLEN -
2252 (dn->dn_nblkptr-1) * sizeof (blkptr_t);
2253 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
2254 db->db.db_offset = DMU_BONUS_BLKID;
2255 db->db_state = DB_UNCACHED;
2256 /* the bonus dbuf is not placed in the hash table */
2257 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
2259 } else if (blkid == DMU_SPILL_BLKID) {
2260 db->db.db_size = (blkptr != NULL) ?
2261 BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE;
2262 db->db.db_offset = 0;
2265 db->db_level ? 1 << dn->dn_indblkshift : dn->dn_datablksz;
2266 db->db.db_size = blocksize;
2267 db->db.db_offset = db->db_blkid * blocksize;
2271 * Hold the dn_dbufs_mtx while we get the new dbuf
2272 * in the hash table *and* added to the dbufs list.
2273 * This prevents a possible deadlock with someone
2274 * trying to look up this dbuf before its added to the
2277 mutex_enter(&dn->dn_dbufs_mtx);
2278 db->db_state = DB_EVICTING;
2279 if ((odb = dbuf_hash_insert(db)) != NULL) {
2280 /* someone else inserted it first */
2281 kmem_cache_free(dbuf_kmem_cache, db);
2282 mutex_exit(&dn->dn_dbufs_mtx);
2285 avl_add(&dn->dn_dbufs, db);
2286 if (db->db_level == 0 && db->db_blkid >=
2287 dn->dn_unlisted_l0_blkid)
2288 dn->dn_unlisted_l0_blkid = db->db_blkid + 1;
2289 db->db_state = DB_UNCACHED;
2290 mutex_exit(&dn->dn_dbufs_mtx);
2291 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
2293 if (parent && parent != dn->dn_dbuf)
2294 dbuf_add_ref(parent, db);
2296 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
2297 refcount_count(&dn->dn_holds) > 0);
2298 (void) refcount_add(&dn->dn_holds, db);
2299 atomic_inc_32(&dn->dn_dbufs_count);
2301 dprintf_dbuf(db, "db=%p\n", db);
2306 typedef struct dbuf_prefetch_arg {
2307 spa_t *dpa_spa; /* The spa to issue the prefetch in. */
2308 zbookmark_phys_t dpa_zb; /* The target block to prefetch. */
2309 int dpa_epbs; /* Entries (blkptr_t's) Per Block Shift. */
2310 int dpa_curlevel; /* The current level that we're reading */
2311 dnode_t *dpa_dnode; /* The dnode associated with the prefetch */
2312 zio_priority_t dpa_prio; /* The priority I/Os should be issued at. */
2313 zio_t *dpa_zio; /* The parent zio_t for all prefetches. */
2314 arc_flags_t dpa_aflags; /* Flags to pass to the final prefetch. */
2315 } dbuf_prefetch_arg_t;
2318 * Actually issue the prefetch read for the block given.
2321 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t *dpa, blkptr_t *bp)
2323 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
2326 arc_flags_t aflags =
2327 dpa->dpa_aflags | ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH;
2329 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp));
2330 ASSERT3U(dpa->dpa_curlevel, ==, dpa->dpa_zb.zb_level);
2331 ASSERT(dpa->dpa_zio != NULL);
2332 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa, bp, NULL, NULL,
2333 dpa->dpa_prio, ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2334 &aflags, &dpa->dpa_zb);
2338 * Called when an indirect block above our prefetch target is read in. This
2339 * will either read in the next indirect block down the tree or issue the actual
2340 * prefetch if the next block down is our target.
2343 dbuf_prefetch_indirect_done(zio_t *zio, arc_buf_t *abuf, void *private)
2345 dbuf_prefetch_arg_t *dpa = private;
2347 ASSERT3S(dpa->dpa_zb.zb_level, <, dpa->dpa_curlevel);
2348 ASSERT3S(dpa->dpa_curlevel, >, 0);
2351 * The dpa_dnode is only valid if we are called with a NULL
2352 * zio. This indicates that the arc_read() returned without
2353 * first calling zio_read() to issue a physical read. Once
2354 * a physical read is made the dpa_dnode must be invalidated
2355 * as the locks guarding it may have been dropped. If the
2356 * dpa_dnode is still valid, then we want to add it to the dbuf
2357 * cache. To do so, we must hold the dbuf associated with the block
2358 * we just prefetched, read its contents so that we associate it
2359 * with an arc_buf_t, and then release it.
2362 ASSERT3S(BP_GET_LEVEL(zio->io_bp), ==, dpa->dpa_curlevel);
2363 if (zio->io_flags & ZIO_FLAG_RAW) {
2364 ASSERT3U(BP_GET_PSIZE(zio->io_bp), ==, zio->io_size);
2366 ASSERT3U(BP_GET_LSIZE(zio->io_bp), ==, zio->io_size);
2368 ASSERT3P(zio->io_spa, ==, dpa->dpa_spa);
2370 dpa->dpa_dnode = NULL;
2371 } else if (dpa->dpa_dnode != NULL) {
2372 uint64_t curblkid = dpa->dpa_zb.zb_blkid >>
2373 (dpa->dpa_epbs * (dpa->dpa_curlevel -
2374 dpa->dpa_zb.zb_level));
2375 dmu_buf_impl_t *db = dbuf_hold_level(dpa->dpa_dnode,
2376 dpa->dpa_curlevel, curblkid, FTAG);
2377 (void) dbuf_read(db, NULL,
2378 DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH | DB_RF_HAVESTRUCT);
2379 dbuf_rele(db, FTAG);
2382 dpa->dpa_curlevel--;
2384 uint64_t nextblkid = dpa->dpa_zb.zb_blkid >>
2385 (dpa->dpa_epbs * (dpa->dpa_curlevel - dpa->dpa_zb.zb_level));
2386 blkptr_t *bp = ((blkptr_t *)abuf->b_data) +
2387 P2PHASE(nextblkid, 1ULL << dpa->dpa_epbs);
2388 if (BP_IS_HOLE(bp) || (zio != NULL && zio->io_error != 0)) {
2389 kmem_free(dpa, sizeof (*dpa));
2390 } else if (dpa->dpa_curlevel == dpa->dpa_zb.zb_level) {
2391 ASSERT3U(nextblkid, ==, dpa->dpa_zb.zb_blkid);
2392 dbuf_issue_final_prefetch(dpa, bp);
2393 kmem_free(dpa, sizeof (*dpa));
2395 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT;
2396 zbookmark_phys_t zb;
2398 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp));
2400 SET_BOOKMARK(&zb, dpa->dpa_zb.zb_objset,
2401 dpa->dpa_zb.zb_object, dpa->dpa_curlevel, nextblkid);
2403 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa,
2404 bp, dbuf_prefetch_indirect_done, dpa, dpa->dpa_prio,
2405 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2409 arc_buf_destroy(abuf, private);
2413 * Issue prefetch reads for the given block on the given level. If the indirect
2414 * blocks above that block are not in memory, we will read them in
2415 * asynchronously. As a result, this call never blocks waiting for a read to
2419 dbuf_prefetch(dnode_t *dn, int64_t level, uint64_t blkid, zio_priority_t prio,
2423 int epbs, nlevels, curlevel;
2426 ASSERT(blkid != DMU_BONUS_BLKID);
2427 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2429 if (blkid > dn->dn_maxblkid)
2432 if (dnode_block_freed(dn, blkid))
2436 * This dnode hasn't been written to disk yet, so there's nothing to
2439 nlevels = dn->dn_phys->dn_nlevels;
2440 if (level >= nlevels || dn->dn_phys->dn_nblkptr == 0)
2443 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2444 if (dn->dn_phys->dn_maxblkid < blkid << (epbs * level))
2447 dmu_buf_impl_t *db = dbuf_find(dn->dn_objset, dn->dn_object,
2450 mutex_exit(&db->db_mtx);
2452 * This dbuf already exists. It is either CACHED, or
2453 * (we assume) about to be read or filled.
2459 * Find the closest ancestor (indirect block) of the target block
2460 * that is present in the cache. In this indirect block, we will
2461 * find the bp that is at curlevel, curblkid.
2465 while (curlevel < nlevels - 1) {
2466 int parent_level = curlevel + 1;
2467 uint64_t parent_blkid = curblkid >> epbs;
2470 if (dbuf_hold_impl(dn, parent_level, parent_blkid,
2471 FALSE, TRUE, FTAG, &db) == 0) {
2472 blkptr_t *bpp = db->db_buf->b_data;
2473 bp = bpp[P2PHASE(curblkid, 1 << epbs)];
2474 dbuf_rele(db, FTAG);
2478 curlevel = parent_level;
2479 curblkid = parent_blkid;
2482 if (curlevel == nlevels - 1) {
2483 /* No cached indirect blocks found. */
2484 ASSERT3U(curblkid, <, dn->dn_phys->dn_nblkptr);
2485 bp = dn->dn_phys->dn_blkptr[curblkid];
2487 if (BP_IS_HOLE(&bp))
2490 ASSERT3U(curlevel, ==, BP_GET_LEVEL(&bp));
2492 zio_t *pio = zio_root(dmu_objset_spa(dn->dn_objset), NULL, NULL,
2495 dbuf_prefetch_arg_t *dpa = kmem_zalloc(sizeof (*dpa), KM_SLEEP);
2496 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
2497 SET_BOOKMARK(&dpa->dpa_zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET,
2498 dn->dn_object, level, blkid);
2499 dpa->dpa_curlevel = curlevel;
2500 dpa->dpa_prio = prio;
2501 dpa->dpa_aflags = aflags;
2502 dpa->dpa_spa = dn->dn_objset->os_spa;
2503 dpa->dpa_dnode = dn;
2504 dpa->dpa_epbs = epbs;
2508 * If we have the indirect just above us, no need to do the asynchronous
2509 * prefetch chain; we'll just run the last step ourselves. If we're at
2510 * a higher level, though, we want to issue the prefetches for all the
2511 * indirect blocks asynchronously, so we can go on with whatever we were
2514 if (curlevel == level) {
2515 ASSERT3U(curblkid, ==, blkid);
2516 dbuf_issue_final_prefetch(dpa, &bp);
2517 kmem_free(dpa, sizeof (*dpa));
2519 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT;
2520 zbookmark_phys_t zb;
2522 SET_BOOKMARK(&zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET,
2523 dn->dn_object, curlevel, curblkid);
2524 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa,
2525 &bp, dbuf_prefetch_indirect_done, dpa, prio,
2526 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2530 * We use pio here instead of dpa_zio since it's possible that
2531 * dpa may have already been freed.
2537 * Returns with db_holds incremented, and db_mtx not held.
2538 * Note: dn_struct_rwlock must be held.
2541 dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid,
2542 boolean_t fail_sparse, boolean_t fail_uncached,
2543 void *tag, dmu_buf_impl_t **dbp)
2545 dmu_buf_impl_t *db, *parent = NULL;
2547 ASSERT(blkid != DMU_BONUS_BLKID);
2548 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2549 ASSERT3U(dn->dn_nlevels, >, level);
2553 /* dbuf_find() returns with db_mtx held */
2554 db = dbuf_find(dn->dn_objset, dn->dn_object, level, blkid);
2557 blkptr_t *bp = NULL;
2561 return (SET_ERROR(ENOENT));
2563 ASSERT3P(parent, ==, NULL);
2564 err = dbuf_findbp(dn, level, blkid, fail_sparse, &parent, &bp);
2566 if (err == 0 && bp && BP_IS_HOLE(bp))
2567 err = SET_ERROR(ENOENT);
2570 dbuf_rele(parent, NULL);
2574 if (err && err != ENOENT)
2576 db = dbuf_create(dn, level, blkid, parent, bp);
2579 if (fail_uncached && db->db_state != DB_CACHED) {
2580 mutex_exit(&db->db_mtx);
2581 return (SET_ERROR(ENOENT));
2584 if (db->db_buf != NULL)
2585 ASSERT3P(db->db.db_data, ==, db->db_buf->b_data);
2587 ASSERT(db->db_buf == NULL || arc_referenced(db->db_buf));
2590 * If this buffer is currently syncing out, and we are are
2591 * still referencing it from db_data, we need to make a copy
2592 * of it in case we decide we want to dirty it again in this txg.
2594 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
2595 dn->dn_object != DMU_META_DNODE_OBJECT &&
2596 db->db_state == DB_CACHED && db->db_data_pending) {
2597 dbuf_dirty_record_t *dr = db->db_data_pending;
2599 if (dr->dt.dl.dr_data == db->db_buf) {
2600 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
2603 arc_alloc_buf(dn->dn_objset->os_spa,
2604 db->db.db_size, db, type));
2605 bcopy(dr->dt.dl.dr_data->b_data, db->db.db_data,
2610 if (multilist_link_active(&db->db_cache_link)) {
2611 ASSERT(refcount_is_zero(&db->db_holds));
2612 multilist_remove(&dbuf_cache, db);
2613 (void) refcount_remove_many(&dbuf_cache_size,
2614 db->db.db_size, db);
2616 (void) refcount_add(&db->db_holds, tag);
2618 mutex_exit(&db->db_mtx);
2620 /* NOTE: we can't rele the parent until after we drop the db_mtx */
2622 dbuf_rele(parent, NULL);
2624 ASSERT3P(DB_DNODE(db), ==, dn);
2625 ASSERT3U(db->db_blkid, ==, blkid);
2626 ASSERT3U(db->db_level, ==, level);
2633 dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag)
2635 return (dbuf_hold_level(dn, 0, blkid, tag));
2639 dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag)
2642 int err = dbuf_hold_impl(dn, level, blkid, FALSE, FALSE, tag, &db);
2643 return (err ? NULL : db);
2647 dbuf_create_bonus(dnode_t *dn)
2649 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
2651 ASSERT(dn->dn_bonus == NULL);
2652 dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL);
2656 dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx)
2658 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2661 if (db->db_blkid != DMU_SPILL_BLKID)
2662 return (SET_ERROR(ENOTSUP));
2664 blksz = SPA_MINBLOCKSIZE;
2665 ASSERT3U(blksz, <=, spa_maxblocksize(dmu_objset_spa(db->db_objset)));
2666 blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE);
2670 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2671 dbuf_new_size(db, blksz, tx);
2672 rw_exit(&dn->dn_struct_rwlock);
2679 dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx)
2681 dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx);
2684 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2686 dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
2688 int64_t holds = refcount_add(&db->db_holds, tag);
2689 ASSERT3S(holds, >, 1);
2692 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
2694 dbuf_try_add_ref(dmu_buf_t *db_fake, objset_t *os, uint64_t obj, uint64_t blkid,
2697 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2698 dmu_buf_impl_t *found_db;
2699 boolean_t result = B_FALSE;
2701 if (db->db_blkid == DMU_BONUS_BLKID)
2702 found_db = dbuf_find_bonus(os, obj);
2704 found_db = dbuf_find(os, obj, 0, blkid);
2706 if (found_db != NULL) {
2707 if (db == found_db && dbuf_refcount(db) > db->db_dirtycnt) {
2708 (void) refcount_add(&db->db_holds, tag);
2711 mutex_exit(&db->db_mtx);
2717 * If you call dbuf_rele() you had better not be referencing the dnode handle
2718 * unless you have some other direct or indirect hold on the dnode. (An indirect
2719 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2720 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2721 * dnode's parent dbuf evicting its dnode handles.
2724 dbuf_rele(dmu_buf_impl_t *db, void *tag)
2726 mutex_enter(&db->db_mtx);
2727 dbuf_rele_and_unlock(db, tag);
2731 dmu_buf_rele(dmu_buf_t *db, void *tag)
2733 dbuf_rele((dmu_buf_impl_t *)db, tag);
2737 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2738 * db_dirtycnt and db_holds to be updated atomically.
2741 dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag)
2745 ASSERT(MUTEX_HELD(&db->db_mtx));
2749 * Remove the reference to the dbuf before removing its hold on the
2750 * dnode so we can guarantee in dnode_move() that a referenced bonus
2751 * buffer has a corresponding dnode hold.
2753 holds = refcount_remove(&db->db_holds, tag);
2757 * We can't freeze indirects if there is a possibility that they
2758 * may be modified in the current syncing context.
2760 if (db->db_buf != NULL &&
2761 holds == (db->db_level == 0 ? db->db_dirtycnt : 0)) {
2762 arc_buf_freeze(db->db_buf);
2765 if (holds == db->db_dirtycnt &&
2766 db->db_level == 0 && db->db_user_immediate_evict)
2767 dbuf_evict_user(db);
2770 if (db->db_blkid == DMU_BONUS_BLKID) {
2772 boolean_t evict_dbuf = db->db_pending_evict;
2775 * If the dnode moves here, we cannot cross this
2776 * barrier until the move completes.
2781 atomic_dec_32(&dn->dn_dbufs_count);
2784 * Decrementing the dbuf count means that the bonus
2785 * buffer's dnode hold is no longer discounted in
2786 * dnode_move(). The dnode cannot move until after
2787 * the dnode_rele() below.
2792 * Do not reference db after its lock is dropped.
2793 * Another thread may evict it.
2795 mutex_exit(&db->db_mtx);
2798 dnode_evict_bonus(dn);
2801 } else if (db->db_buf == NULL) {
2803 * This is a special case: we never associated this
2804 * dbuf with any data allocated from the ARC.
2806 ASSERT(db->db_state == DB_UNCACHED ||
2807 db->db_state == DB_NOFILL);
2809 } else if (arc_released(db->db_buf)) {
2811 * This dbuf has anonymous data associated with it.
2815 boolean_t do_arc_evict = B_FALSE;
2817 spa_t *spa = dmu_objset_spa(db->db_objset);
2819 if (!DBUF_IS_CACHEABLE(db) &&
2820 db->db_blkptr != NULL &&
2821 !BP_IS_HOLE(db->db_blkptr) &&
2822 !BP_IS_EMBEDDED(db->db_blkptr)) {
2823 do_arc_evict = B_TRUE;
2824 bp = *db->db_blkptr;
2827 if (!DBUF_IS_CACHEABLE(db) ||
2828 db->db_pending_evict) {
2830 } else if (!multilist_link_active(&db->db_cache_link)) {
2831 multilist_insert(&dbuf_cache, db);
2832 (void) refcount_add_many(&dbuf_cache_size,
2833 db->db.db_size, db);
2834 mutex_exit(&db->db_mtx);
2836 dbuf_evict_notify();
2840 arc_freed(spa, &bp);
2843 mutex_exit(&db->db_mtx);
2848 #pragma weak dmu_buf_refcount = dbuf_refcount
2850 dbuf_refcount(dmu_buf_impl_t *db)
2852 return (refcount_count(&db->db_holds));
2856 dmu_buf_replace_user(dmu_buf_t *db_fake, dmu_buf_user_t *old_user,
2857 dmu_buf_user_t *new_user)
2859 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2861 mutex_enter(&db->db_mtx);
2862 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2863 if (db->db_user == old_user)
2864 db->db_user = new_user;
2866 old_user = db->db_user;
2867 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2868 mutex_exit(&db->db_mtx);
2874 dmu_buf_set_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2876 return (dmu_buf_replace_user(db_fake, NULL, user));
2880 dmu_buf_set_user_ie(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2882 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2884 db->db_user_immediate_evict = TRUE;
2885 return (dmu_buf_set_user(db_fake, user));
2889 dmu_buf_remove_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2891 return (dmu_buf_replace_user(db_fake, user, NULL));
2895 dmu_buf_get_user(dmu_buf_t *db_fake)
2897 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2899 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2900 return (db->db_user);
2904 dmu_buf_user_evict_wait()
2906 taskq_wait(dbu_evict_taskq);
2910 dmu_buf_freeable(dmu_buf_t *dbuf)
2912 boolean_t res = B_FALSE;
2913 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
2916 res = dsl_dataset_block_freeable(db->db_objset->os_dsl_dataset,
2917 db->db_blkptr, db->db_blkptr->blk_birth);
2923 dmu_buf_get_blkptr(dmu_buf_t *db)
2925 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2926 return (dbi->db_blkptr);
2930 dmu_buf_get_objset(dmu_buf_t *db)
2932 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2933 return (dbi->db_objset);
2937 dmu_buf_dnode_enter(dmu_buf_t *db)
2939 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2940 DB_DNODE_ENTER(dbi);
2941 return (DB_DNODE(dbi));
2945 dmu_buf_dnode_exit(dmu_buf_t *db)
2947 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2952 dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db)
2954 /* ASSERT(dmu_tx_is_syncing(tx) */
2955 ASSERT(MUTEX_HELD(&db->db_mtx));
2957 if (db->db_blkptr != NULL)
2960 if (db->db_blkid == DMU_SPILL_BLKID) {
2961 db->db_blkptr = &dn->dn_phys->dn_spill;
2962 BP_ZERO(db->db_blkptr);
2965 if (db->db_level == dn->dn_phys->dn_nlevels-1) {
2967 * This buffer was allocated at a time when there was
2968 * no available blkptrs from the dnode, or it was
2969 * inappropriate to hook it in (i.e., nlevels mis-match).
2971 ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr);
2972 ASSERT(db->db_parent == NULL);
2973 db->db_parent = dn->dn_dbuf;
2974 db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
2977 dmu_buf_impl_t *parent = db->db_parent;
2978 int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2980 ASSERT(dn->dn_phys->dn_nlevels > 1);
2981 if (parent == NULL) {
2982 mutex_exit(&db->db_mtx);
2983 rw_enter(&dn->dn_struct_rwlock, RW_READER);
2984 parent = dbuf_hold_level(dn, db->db_level + 1,
2985 db->db_blkid >> epbs, db);
2986 rw_exit(&dn->dn_struct_rwlock);
2987 mutex_enter(&db->db_mtx);
2988 db->db_parent = parent;
2990 db->db_blkptr = (blkptr_t *)parent->db.db_data +
2991 (db->db_blkid & ((1ULL << epbs) - 1));
2997 dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2999 dmu_buf_impl_t *db = dr->dr_dbuf;
3003 ASSERT(dmu_tx_is_syncing(tx));
3005 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
3007 mutex_enter(&db->db_mtx);
3009 ASSERT(db->db_level > 0);
3012 /* Read the block if it hasn't been read yet. */
3013 if (db->db_buf == NULL) {
3014 mutex_exit(&db->db_mtx);
3015 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
3016 mutex_enter(&db->db_mtx);
3018 ASSERT3U(db->db_state, ==, DB_CACHED);
3019 ASSERT(db->db_buf != NULL);
3023 /* Indirect block size must match what the dnode thinks it is. */
3024 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
3025 dbuf_check_blkptr(dn, db);
3028 /* Provide the pending dirty record to child dbufs */
3029 db->db_data_pending = dr;
3031 mutex_exit(&db->db_mtx);
3032 dbuf_write(dr, db->db_buf, tx);
3035 mutex_enter(&dr->dt.di.dr_mtx);
3036 dbuf_sync_list(&dr->dt.di.dr_children, db->db_level - 1, tx);
3037 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
3038 mutex_exit(&dr->dt.di.dr_mtx);
3043 dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
3045 arc_buf_t **datap = &dr->dt.dl.dr_data;
3046 dmu_buf_impl_t *db = dr->dr_dbuf;
3049 uint64_t txg = tx->tx_txg;
3051 ASSERT(dmu_tx_is_syncing(tx));
3053 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
3055 mutex_enter(&db->db_mtx);
3057 * To be synced, we must be dirtied. But we
3058 * might have been freed after the dirty.
3060 if (db->db_state == DB_UNCACHED) {
3061 /* This buffer has been freed since it was dirtied */
3062 ASSERT(db->db.db_data == NULL);
3063 } else if (db->db_state == DB_FILL) {
3064 /* This buffer was freed and is now being re-filled */
3065 ASSERT(db->db.db_data != dr->dt.dl.dr_data);
3067 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL);
3074 if (db->db_blkid == DMU_SPILL_BLKID) {
3075 mutex_enter(&dn->dn_mtx);
3076 dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR;
3077 mutex_exit(&dn->dn_mtx);
3081 * If this is a bonus buffer, simply copy the bonus data into the
3082 * dnode. It will be written out when the dnode is synced (and it
3083 * will be synced, since it must have been dirty for dbuf_sync to
3086 if (db->db_blkid == DMU_BONUS_BLKID) {
3087 dbuf_dirty_record_t **drp;
3089 ASSERT(*datap != NULL);
3090 ASSERT0(db->db_level);
3091 ASSERT3U(dn->dn_phys->dn_bonuslen, <=, DN_MAX_BONUSLEN);
3092 bcopy(*datap, DN_BONUS(dn->dn_phys), dn->dn_phys->dn_bonuslen);
3095 if (*datap != db->db.db_data) {
3096 zio_buf_free(*datap, DN_MAX_BONUSLEN);
3097 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
3099 db->db_data_pending = NULL;
3100 drp = &db->db_last_dirty;
3102 drp = &(*drp)->dr_next;
3103 ASSERT(dr->dr_next == NULL);
3104 ASSERT(dr->dr_dbuf == db);
3106 if (dr->dr_dbuf->db_level != 0) {
3107 list_destroy(&dr->dt.di.dr_children);
3108 mutex_destroy(&dr->dt.di.dr_mtx);
3110 kmem_free(dr, sizeof (dbuf_dirty_record_t));
3111 ASSERT(db->db_dirtycnt > 0);
3112 db->db_dirtycnt -= 1;
3113 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
3120 * This function may have dropped the db_mtx lock allowing a dmu_sync
3121 * operation to sneak in. As a result, we need to ensure that we
3122 * don't check the dr_override_state until we have returned from
3123 * dbuf_check_blkptr.
3125 dbuf_check_blkptr(dn, db);
3128 * If this buffer is in the middle of an immediate write,
3129 * wait for the synchronous IO to complete.
3131 while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
3132 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
3133 cv_wait(&db->db_changed, &db->db_mtx);
3134 ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN);
3137 if (db->db_state != DB_NOFILL &&
3138 dn->dn_object != DMU_META_DNODE_OBJECT &&
3139 refcount_count(&db->db_holds) > 1 &&
3140 dr->dt.dl.dr_override_state != DR_OVERRIDDEN &&
3141 *datap == db->db_buf) {
3143 * If this buffer is currently "in use" (i.e., there
3144 * are active holds and db_data still references it),
3145 * then make a copy before we start the write so that
3146 * any modifications from the open txg will not leak
3149 * NOTE: this copy does not need to be made for
3150 * objects only modified in the syncing context (e.g.
3151 * DNONE_DNODE blocks).
3153 int blksz = arc_buf_size(*datap);
3154 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
3155 *datap = arc_alloc_buf(os->os_spa, blksz, db, type);
3156 bcopy(db->db.db_data, (*datap)->b_data, blksz);
3158 db->db_data_pending = dr;
3160 mutex_exit(&db->db_mtx);
3162 dbuf_write(dr, *datap, tx);
3164 ASSERT(!list_link_active(&dr->dr_dirty_node));
3165 if (dn->dn_object == DMU_META_DNODE_OBJECT) {
3166 list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr);
3170 * Although zio_nowait() does not "wait for an IO", it does
3171 * initiate the IO. If this is an empty write it seems plausible
3172 * that the IO could actually be completed before the nowait
3173 * returns. We need to DB_DNODE_EXIT() first in case
3174 * zio_nowait() invalidates the dbuf.
3177 zio_nowait(dr->dr_zio);
3182 dbuf_sync_list(list_t *list, int level, dmu_tx_t *tx)
3184 dbuf_dirty_record_t *dr;
3186 while (dr = list_head(list)) {
3187 if (dr->dr_zio != NULL) {
3189 * If we find an already initialized zio then we
3190 * are processing the meta-dnode, and we have finished.
3191 * The dbufs for all dnodes are put back on the list
3192 * during processing, so that we can zio_wait()
3193 * these IOs after initiating all child IOs.
3195 ASSERT3U(dr->dr_dbuf->db.db_object, ==,
3196 DMU_META_DNODE_OBJECT);
3199 if (dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
3200 dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
3201 VERIFY3U(dr->dr_dbuf->db_level, ==, level);
3203 list_remove(list, dr);
3204 if (dr->dr_dbuf->db_level > 0)
3205 dbuf_sync_indirect(dr, tx);
3207 dbuf_sync_leaf(dr, tx);
3213 dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
3215 dmu_buf_impl_t *db = vdb;
3217 blkptr_t *bp = zio->io_bp;
3218 blkptr_t *bp_orig = &zio->io_bp_orig;
3219 spa_t *spa = zio->io_spa;
3224 ASSERT3P(db->db_blkptr, !=, NULL);
3225 ASSERT3P(&db->db_data_pending->dr_bp_copy, ==, bp);
3229 delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig);
3230 dnode_diduse_space(dn, delta - zio->io_prev_space_delta);
3231 zio->io_prev_space_delta = delta;
3233 if (bp->blk_birth != 0) {
3234 ASSERT((db->db_blkid != DMU_SPILL_BLKID &&
3235 BP_GET_TYPE(bp) == dn->dn_type) ||
3236 (db->db_blkid == DMU_SPILL_BLKID &&
3237 BP_GET_TYPE(bp) == dn->dn_bonustype) ||
3238 BP_IS_EMBEDDED(bp));
3239 ASSERT(BP_GET_LEVEL(bp) == db->db_level);
3242 mutex_enter(&db->db_mtx);
3245 if (db->db_blkid == DMU_SPILL_BLKID) {
3246 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
3247 ASSERT(!(BP_IS_HOLE(bp)) &&
3248 db->db_blkptr == &dn->dn_phys->dn_spill);
3252 if (db->db_level == 0) {
3253 mutex_enter(&dn->dn_mtx);
3254 if (db->db_blkid > dn->dn_phys->dn_maxblkid &&
3255 db->db_blkid != DMU_SPILL_BLKID)
3256 dn->dn_phys->dn_maxblkid = db->db_blkid;
3257 mutex_exit(&dn->dn_mtx);
3259 if (dn->dn_type == DMU_OT_DNODE) {
3260 dnode_phys_t *dnp = db->db.db_data;
3261 for (i = db->db.db_size >> DNODE_SHIFT; i > 0;
3263 if (dnp->dn_type != DMU_OT_NONE)
3267 if (BP_IS_HOLE(bp)) {
3274 blkptr_t *ibp = db->db.db_data;
3275 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
3276 for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) {
3277 if (BP_IS_HOLE(ibp))
3279 fill += BP_GET_FILL(ibp);
3284 if (!BP_IS_EMBEDDED(bp))
3285 bp->blk_fill = fill;
3287 mutex_exit(&db->db_mtx);
3289 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
3290 *db->db_blkptr = *bp;
3291 rw_exit(&dn->dn_struct_rwlock);
3296 * This function gets called just prior to running through the compression
3297 * stage of the zio pipeline. If we're an indirect block comprised of only
3298 * holes, then we want this indirect to be compressed away to a hole. In
3299 * order to do that we must zero out any information about the holes that
3300 * this indirect points to prior to before we try to compress it.
3303 dbuf_write_children_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
3305 dmu_buf_impl_t *db = vdb;
3311 ASSERT3U(db->db_level, >, 0);
3314 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
3316 /* Determine if all our children are holes */
3317 for (i = 0, bp = db->db.db_data; i < 1 << epbs; i++, bp++) {
3318 if (!BP_IS_HOLE(bp))
3323 * If all the children are holes, then zero them all out so that
3324 * we may get compressed away.
3326 if (i == 1 << epbs) {
3327 /* didn't find any non-holes */
3328 bzero(db->db.db_data, db->db.db_size);
3334 * The SPA will call this callback several times for each zio - once
3335 * for every physical child i/o (zio->io_phys_children times). This
3336 * allows the DMU to monitor the progress of each logical i/o. For example,
3337 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
3338 * block. There may be a long delay before all copies/fragments are completed,
3339 * so this callback allows us to retire dirty space gradually, as the physical
3344 dbuf_write_physdone(zio_t *zio, arc_buf_t *buf, void *arg)
3346 dmu_buf_impl_t *db = arg;
3347 objset_t *os = db->db_objset;
3348 dsl_pool_t *dp = dmu_objset_pool(os);
3349 dbuf_dirty_record_t *dr;
3352 dr = db->db_data_pending;
3353 ASSERT3U(dr->dr_txg, ==, zio->io_txg);
3356 * The callback will be called io_phys_children times. Retire one
3357 * portion of our dirty space each time we are called. Any rounding
3358 * error will be cleaned up by dsl_pool_sync()'s call to
3359 * dsl_pool_undirty_space().
3361 delta = dr->dr_accounted / zio->io_phys_children;
3362 dsl_pool_undirty_space(dp, delta, zio->io_txg);
3367 dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
3369 dmu_buf_impl_t *db = vdb;
3370 blkptr_t *bp_orig = &zio->io_bp_orig;
3371 blkptr_t *bp = db->db_blkptr;
3372 objset_t *os = db->db_objset;
3373 dmu_tx_t *tx = os->os_synctx;
3374 dbuf_dirty_record_t **drp, *dr;
3376 ASSERT0(zio->io_error);
3377 ASSERT(db->db_blkptr == bp);
3380 * For nopwrites and rewrites we ensure that the bp matches our
3381 * original and bypass all the accounting.
3383 if (zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)) {
3384 ASSERT(BP_EQUAL(bp, bp_orig));
3386 dsl_dataset_t *ds = os->os_dsl_dataset;
3387 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
3388 dsl_dataset_block_born(ds, bp, tx);
3391 mutex_enter(&db->db_mtx);
3395 drp = &db->db_last_dirty;
3396 while ((dr = *drp) != db->db_data_pending)
3398 ASSERT(!list_link_active(&dr->dr_dirty_node));
3399 ASSERT(dr->dr_dbuf == db);
3400 ASSERT(dr->dr_next == NULL);
3404 if (db->db_blkid == DMU_SPILL_BLKID) {
3409 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
3410 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
3411 db->db_blkptr == &dn->dn_phys->dn_spill);
3416 if (db->db_level == 0) {
3417 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
3418 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
3419 if (db->db_state != DB_NOFILL) {
3420 if (dr->dt.dl.dr_data != db->db_buf)
3421 arc_buf_destroy(dr->dt.dl.dr_data, db);
3428 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
3429 ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
3430 if (!BP_IS_HOLE(db->db_blkptr)) {
3432 dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
3433 ASSERT3U(db->db_blkid, <=,
3434 dn->dn_phys->dn_maxblkid >> (db->db_level * epbs));
3435 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
3439 mutex_destroy(&dr->dt.di.dr_mtx);
3440 list_destroy(&dr->dt.di.dr_children);
3442 kmem_free(dr, sizeof (dbuf_dirty_record_t));
3444 cv_broadcast(&db->db_changed);
3445 ASSERT(db->db_dirtycnt > 0);
3446 db->db_dirtycnt -= 1;
3447 db->db_data_pending = NULL;
3448 dbuf_rele_and_unlock(db, (void *)(uintptr_t)tx->tx_txg);
3452 dbuf_write_nofill_ready(zio_t *zio)
3454 dbuf_write_ready(zio, NULL, zio->io_private);
3458 dbuf_write_nofill_done(zio_t *zio)
3460 dbuf_write_done(zio, NULL, zio->io_private);
3464 dbuf_write_override_ready(zio_t *zio)
3466 dbuf_dirty_record_t *dr = zio->io_private;
3467 dmu_buf_impl_t *db = dr->dr_dbuf;
3469 dbuf_write_ready(zio, NULL, db);
3473 dbuf_write_override_done(zio_t *zio)
3475 dbuf_dirty_record_t *dr = zio->io_private;
3476 dmu_buf_impl_t *db = dr->dr_dbuf;
3477 blkptr_t *obp = &dr->dt.dl.dr_overridden_by;
3479 mutex_enter(&db->db_mtx);
3480 if (!BP_EQUAL(zio->io_bp, obp)) {
3481 if (!BP_IS_HOLE(obp))
3482 dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp);
3483 arc_release(dr->dt.dl.dr_data, db);
3485 mutex_exit(&db->db_mtx);
3487 dbuf_write_done(zio, NULL, db);
3490 /* Issue I/O to commit a dirty buffer to disk. */
3492 dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx)
3494 dmu_buf_impl_t *db = dr->dr_dbuf;
3497 dmu_buf_impl_t *parent = db->db_parent;
3498 uint64_t txg = tx->tx_txg;
3499 zbookmark_phys_t zb;
3504 ASSERT(dmu_tx_is_syncing(tx));
3510 if (db->db_state != DB_NOFILL) {
3511 if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) {
3513 * Private object buffers are released here rather
3514 * than in dbuf_dirty() since they are only modified
3515 * in the syncing context and we don't want the
3516 * overhead of making multiple copies of the data.
3518 if (BP_IS_HOLE(db->db_blkptr)) {
3521 dbuf_release_bp(db);
3526 if (parent != dn->dn_dbuf) {
3527 /* Our parent is an indirect block. */
3528 /* We have a dirty parent that has been scheduled for write. */
3529 ASSERT(parent && parent->db_data_pending);
3530 /* Our parent's buffer is one level closer to the dnode. */
3531 ASSERT(db->db_level == parent->db_level-1);
3533 * We're about to modify our parent's db_data by modifying
3534 * our block pointer, so the parent must be released.
3536 ASSERT(arc_released(parent->db_buf));
3537 zio = parent->db_data_pending->dr_zio;
3539 /* Our parent is the dnode itself. */
3540 ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 &&
3541 db->db_blkid != DMU_SPILL_BLKID) ||
3542 (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0));
3543 if (db->db_blkid != DMU_SPILL_BLKID)
3544 ASSERT3P(db->db_blkptr, ==,
3545 &dn->dn_phys->dn_blkptr[db->db_blkid]);
3549 ASSERT(db->db_level == 0 || data == db->db_buf);
3550 ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
3553 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
3554 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
3555 db->db.db_object, db->db_level, db->db_blkid);
3557 if (db->db_blkid == DMU_SPILL_BLKID)
3559 wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
3561 dmu_write_policy(os, dn, db->db_level, wp_flag, &zp);
3565 * We copy the blkptr now (rather than when we instantiate the dirty
3566 * record), because its value can change between open context and
3567 * syncing context. We do not need to hold dn_struct_rwlock to read
3568 * db_blkptr because we are in syncing context.
3570 dr->dr_bp_copy = *db->db_blkptr;
3572 if (db->db_level == 0 &&
3573 dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
3575 * The BP for this block has been provided by open context
3576 * (by dmu_sync() or dmu_buf_write_embedded()).
3578 void *contents = (data != NULL) ? data->b_data : NULL;
3580 dr->dr_zio = zio_write(zio, os->os_spa, txg,
3581 &dr->dr_bp_copy, contents, db->db.db_size, &zp,
3582 dbuf_write_override_ready, NULL, NULL,
3583 dbuf_write_override_done,
3584 dr, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
3585 mutex_enter(&db->db_mtx);
3586 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
3587 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by,
3588 dr->dt.dl.dr_copies, dr->dt.dl.dr_nopwrite);
3589 mutex_exit(&db->db_mtx);
3590 } else if (db->db_state == DB_NOFILL) {
3591 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF ||
3592 zp.zp_checksum == ZIO_CHECKSUM_NOPARITY);
3593 dr->dr_zio = zio_write(zio, os->os_spa, txg,
3594 &dr->dr_bp_copy, NULL, db->db.db_size, &zp,
3595 dbuf_write_nofill_ready, NULL, NULL,
3596 dbuf_write_nofill_done, db,
3597 ZIO_PRIORITY_ASYNC_WRITE,
3598 ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb);
3600 ASSERT(arc_released(data));
3603 * For indirect blocks, we want to setup the children
3604 * ready callback so that we can properly handle an indirect
3605 * block that only contains holes.
3607 arc_done_func_t *children_ready_cb = NULL;
3608 if (db->db_level != 0)
3609 children_ready_cb = dbuf_write_children_ready;
3611 dr->dr_zio = arc_write(zio, os->os_spa, txg,
3612 &dr->dr_bp_copy, data, DBUF_IS_L2CACHEABLE(db),
3613 &zp, dbuf_write_ready, children_ready_cb,
3614 dbuf_write_physdone, dbuf_write_done, db,
3615 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);