4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28 * Copyright (c) 2014 Integros [integros.com]
31 #include <sys/zfs_context.h>
33 #include <sys/dmu_send.h>
34 #include <sys/dmu_impl.h>
36 #include <sys/dmu_objset.h>
37 #include <sys/dsl_dataset.h>
38 #include <sys/dsl_dir.h>
39 #include <sys/dmu_tx.h>
42 #include <sys/dmu_zfetch.h>
44 #include <sys/sa_impl.h>
45 #include <sys/zfeature.h>
46 #include <sys/blkptr.h>
47 #include <sys/range_tree.h>
48 #include <sys/callb.h>
52 uint_t zfs_dbuf_evict_key;
54 static boolean_t dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
55 static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx);
58 extern inline void dmu_buf_init_user(dmu_buf_user_t *dbu,
59 dmu_buf_evict_func_t *evict_func_sync,
60 dmu_buf_evict_func_t *evict_func_async,
61 dmu_buf_t **clear_on_evict_dbufp);
65 * Global data structures and functions for the dbuf cache.
67 static kmem_cache_t *dbuf_kmem_cache;
68 static taskq_t *dbu_evict_taskq;
70 static kthread_t *dbuf_cache_evict_thread;
71 static kmutex_t dbuf_evict_lock;
72 static kcondvar_t dbuf_evict_cv;
73 static boolean_t dbuf_evict_thread_exit;
76 * LRU cache of dbufs. The dbuf cache maintains a list of dbufs that
77 * are not currently held but have been recently released. These dbufs
78 * are not eligible for arc eviction until they are aged out of the cache.
79 * Dbufs are added to the dbuf cache once the last hold is released. If a
80 * dbuf is later accessed and still exists in the dbuf cache, then it will
81 * be removed from the cache and later re-added to the head of the cache.
82 * Dbufs that are aged out of the cache will be immediately destroyed and
83 * become eligible for arc eviction.
85 static multilist_t *dbuf_cache;
86 static refcount_t dbuf_cache_size;
87 uint64_t dbuf_cache_max_bytes = 100 * 1024 * 1024;
89 /* Cap the size of the dbuf cache to log2 fraction of arc size. */
90 int dbuf_cache_max_shift = 5;
93 * The dbuf cache uses a three-stage eviction policy:
94 * - A low water marker designates when the dbuf eviction thread
95 * should stop evicting from the dbuf cache.
96 * - When we reach the maximum size (aka mid water mark), we
97 * signal the eviction thread to run.
98 * - The high water mark indicates when the eviction thread
99 * is unable to keep up with the incoming load and eviction must
100 * happen in the context of the calling thread.
104 * low water mid water hi water
105 * +----------------------------------------+----------+----------+
110 * +----------------------------------------+----------+----------+
112 * evicting eviction directly
115 * The high and low water marks indicate the operating range for the eviction
116 * thread. The low water mark is, by default, 90% of the total size of the
117 * cache and the high water mark is at 110% (both of these percentages can be
118 * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct,
119 * respectively). The eviction thread will try to ensure that the cache remains
120 * within this range by waking up every second and checking if the cache is
121 * above the low water mark. The thread can also be woken up by callers adding
122 * elements into the cache if the cache is larger than the mid water (i.e max
123 * cache size). Once the eviction thread is woken up and eviction is required,
124 * it will continue evicting buffers until it's able to reduce the cache size
125 * to the low water mark. If the cache size continues to grow and hits the high
126 * water mark, then callers adding elments to the cache will begin to evict
127 * directly from the cache until the cache is no longer above the high water
132 * The percentage above and below the maximum cache size.
134 uint_t dbuf_cache_hiwater_pct = 10;
135 uint_t dbuf_cache_lowater_pct = 10;
139 dbuf_cons(void *vdb, void *unused, int kmflag)
141 dmu_buf_impl_t *db = vdb;
142 bzero(db, sizeof (dmu_buf_impl_t));
144 mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL);
145 cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL);
146 multilist_link_init(&db->db_cache_link);
147 refcount_create(&db->db_holds);
154 dbuf_dest(void *vdb, void *unused)
156 dmu_buf_impl_t *db = vdb;
157 mutex_destroy(&db->db_mtx);
158 cv_destroy(&db->db_changed);
159 ASSERT(!multilist_link_active(&db->db_cache_link));
160 refcount_destroy(&db->db_holds);
164 * dbuf hash table routines
166 static dbuf_hash_table_t dbuf_hash_table;
168 static uint64_t dbuf_hash_count;
171 dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid)
173 uintptr_t osv = (uintptr_t)os;
174 uint64_t crc = -1ULL;
176 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
177 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (lvl)) & 0xFF];
178 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF];
179 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF];
180 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF];
181 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 0)) & 0xFF];
182 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 8)) & 0xFF];
184 crc ^= (osv>>14) ^ (obj>>16) ^ (blkid>>16);
189 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
190 ((dbuf)->db.db_object == (obj) && \
191 (dbuf)->db_objset == (os) && \
192 (dbuf)->db_level == (level) && \
193 (dbuf)->db_blkid == (blkid))
196 dbuf_find(objset_t *os, uint64_t obj, uint8_t level, uint64_t blkid)
198 dbuf_hash_table_t *h = &dbuf_hash_table;
199 uint64_t hv = dbuf_hash(os, obj, level, blkid);
200 uint64_t idx = hv & h->hash_table_mask;
203 mutex_enter(DBUF_HASH_MUTEX(h, idx));
204 for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) {
205 if (DBUF_EQUAL(db, os, obj, level, blkid)) {
206 mutex_enter(&db->db_mtx);
207 if (db->db_state != DB_EVICTING) {
208 mutex_exit(DBUF_HASH_MUTEX(h, idx));
211 mutex_exit(&db->db_mtx);
214 mutex_exit(DBUF_HASH_MUTEX(h, idx));
218 static dmu_buf_impl_t *
219 dbuf_find_bonus(objset_t *os, uint64_t object)
222 dmu_buf_impl_t *db = NULL;
224 if (dnode_hold(os, object, FTAG, &dn) == 0) {
225 rw_enter(&dn->dn_struct_rwlock, RW_READER);
226 if (dn->dn_bonus != NULL) {
228 mutex_enter(&db->db_mtx);
230 rw_exit(&dn->dn_struct_rwlock);
231 dnode_rele(dn, FTAG);
237 * Insert an entry into the hash table. If there is already an element
238 * equal to elem in the hash table, then the already existing element
239 * will be returned and the new element will not be inserted.
240 * Otherwise returns NULL.
242 static dmu_buf_impl_t *
243 dbuf_hash_insert(dmu_buf_impl_t *db)
245 dbuf_hash_table_t *h = &dbuf_hash_table;
246 objset_t *os = db->db_objset;
247 uint64_t obj = db->db.db_object;
248 int level = db->db_level;
249 uint64_t blkid = db->db_blkid;
250 uint64_t hv = dbuf_hash(os, obj, level, blkid);
251 uint64_t idx = hv & h->hash_table_mask;
254 mutex_enter(DBUF_HASH_MUTEX(h, idx));
255 for (dbf = h->hash_table[idx]; dbf != NULL; dbf = dbf->db_hash_next) {
256 if (DBUF_EQUAL(dbf, os, obj, level, blkid)) {
257 mutex_enter(&dbf->db_mtx);
258 if (dbf->db_state != DB_EVICTING) {
259 mutex_exit(DBUF_HASH_MUTEX(h, idx));
262 mutex_exit(&dbf->db_mtx);
266 mutex_enter(&db->db_mtx);
267 db->db_hash_next = h->hash_table[idx];
268 h->hash_table[idx] = db;
269 mutex_exit(DBUF_HASH_MUTEX(h, idx));
270 atomic_inc_64(&dbuf_hash_count);
276 * Remove an entry from the hash table. It must be in the EVICTING state.
279 dbuf_hash_remove(dmu_buf_impl_t *db)
281 dbuf_hash_table_t *h = &dbuf_hash_table;
282 uint64_t hv = dbuf_hash(db->db_objset, db->db.db_object,
283 db->db_level, db->db_blkid);
284 uint64_t idx = hv & h->hash_table_mask;
285 dmu_buf_impl_t *dbf, **dbp;
288 * We musn't hold db_mtx to maintain lock ordering:
289 * DBUF_HASH_MUTEX > db_mtx.
291 ASSERT(refcount_is_zero(&db->db_holds));
292 ASSERT(db->db_state == DB_EVICTING);
293 ASSERT(!MUTEX_HELD(&db->db_mtx));
295 mutex_enter(DBUF_HASH_MUTEX(h, idx));
296 dbp = &h->hash_table[idx];
297 while ((dbf = *dbp) != db) {
298 dbp = &dbf->db_hash_next;
301 *dbp = db->db_hash_next;
302 db->db_hash_next = NULL;
303 mutex_exit(DBUF_HASH_MUTEX(h, idx));
304 atomic_dec_64(&dbuf_hash_count);
310 } dbvu_verify_type_t;
313 dbuf_verify_user(dmu_buf_impl_t *db, dbvu_verify_type_t verify_type)
318 if (db->db_user == NULL)
321 /* Only data blocks support the attachment of user data. */
322 ASSERT(db->db_level == 0);
324 /* Clients must resolve a dbuf before attaching user data. */
325 ASSERT(db->db.db_data != NULL);
326 ASSERT3U(db->db_state, ==, DB_CACHED);
328 holds = refcount_count(&db->db_holds);
329 if (verify_type == DBVU_EVICTING) {
331 * Immediate eviction occurs when holds == dirtycnt.
332 * For normal eviction buffers, holds is zero on
333 * eviction, except when dbuf_fix_old_data() calls
334 * dbuf_clear_data(). However, the hold count can grow
335 * during eviction even though db_mtx is held (see
336 * dmu_bonus_hold() for an example), so we can only
337 * test the generic invariant that holds >= dirtycnt.
339 ASSERT3U(holds, >=, db->db_dirtycnt);
341 if (db->db_user_immediate_evict == TRUE)
342 ASSERT3U(holds, >=, db->db_dirtycnt);
344 ASSERT3U(holds, >, 0);
350 dbuf_evict_user(dmu_buf_impl_t *db)
352 dmu_buf_user_t *dbu = db->db_user;
354 ASSERT(MUTEX_HELD(&db->db_mtx));
359 dbuf_verify_user(db, DBVU_EVICTING);
363 if (dbu->dbu_clear_on_evict_dbufp != NULL)
364 *dbu->dbu_clear_on_evict_dbufp = NULL;
368 * There are two eviction callbacks - one that we call synchronously
369 * and one that we invoke via a taskq. The async one is useful for
370 * avoiding lock order reversals and limiting stack depth.
372 * Note that if we have a sync callback but no async callback,
373 * it's likely that the sync callback will free the structure
374 * containing the dbu. In that case we need to take care to not
375 * dereference dbu after calling the sync evict func.
377 boolean_t has_async = (dbu->dbu_evict_func_async != NULL);
379 if (dbu->dbu_evict_func_sync != NULL)
380 dbu->dbu_evict_func_sync(dbu);
383 taskq_dispatch_ent(dbu_evict_taskq, dbu->dbu_evict_func_async,
384 dbu, 0, &dbu->dbu_tqent);
389 dbuf_is_metadata(dmu_buf_impl_t *db)
391 if (db->db_level > 0) {
394 boolean_t is_metadata;
397 is_metadata = DMU_OT_IS_METADATA(DB_DNODE(db)->dn_type);
400 return (is_metadata);
405 * This function *must* return indices evenly distributed between all
406 * sublists of the multilist. This is needed due to how the dbuf eviction
407 * code is laid out; dbuf_evict_thread() assumes dbufs are evenly
408 * distributed between all sublists and uses this assumption when
409 * deciding which sublist to evict from and how much to evict from it.
412 dbuf_cache_multilist_index_func(multilist_t *ml, void *obj)
414 dmu_buf_impl_t *db = obj;
417 * The assumption here, is the hash value for a given
418 * dmu_buf_impl_t will remain constant throughout it's lifetime
419 * (i.e. it's objset, object, level and blkid fields don't change).
420 * Thus, we don't need to store the dbuf's sublist index
421 * on insertion, as this index can be recalculated on removal.
423 * Also, the low order bits of the hash value are thought to be
424 * distributed evenly. Otherwise, in the case that the multilist
425 * has a power of two number of sublists, each sublists' usage
426 * would not be evenly distributed.
428 return (dbuf_hash(db->db_objset, db->db.db_object,
429 db->db_level, db->db_blkid) %
430 multilist_get_num_sublists(ml));
433 static inline boolean_t
434 dbuf_cache_above_hiwater(void)
436 uint64_t dbuf_cache_hiwater_bytes =
437 (dbuf_cache_max_bytes * dbuf_cache_hiwater_pct) / 100;
439 return (refcount_count(&dbuf_cache_size) >
440 dbuf_cache_max_bytes + dbuf_cache_hiwater_bytes);
443 static inline boolean_t
444 dbuf_cache_above_lowater(void)
446 uint64_t dbuf_cache_lowater_bytes =
447 (dbuf_cache_max_bytes * dbuf_cache_lowater_pct) / 100;
449 return (refcount_count(&dbuf_cache_size) >
450 dbuf_cache_max_bytes - dbuf_cache_lowater_bytes);
454 * Evict the oldest eligible dbuf from the dbuf cache.
459 int idx = multilist_get_random_index(dbuf_cache);
460 multilist_sublist_t *mls = multilist_sublist_lock(dbuf_cache, idx);
462 ASSERT(!MUTEX_HELD(&dbuf_evict_lock));
465 * Set the thread's tsd to indicate that it's processing evictions.
466 * Once a thread stops evicting from the dbuf cache it will
467 * reset its tsd to NULL.
469 ASSERT3P(tsd_get(zfs_dbuf_evict_key), ==, NULL);
470 (void) tsd_set(zfs_dbuf_evict_key, (void *)B_TRUE);
472 dmu_buf_impl_t *db = multilist_sublist_tail(mls);
473 while (db != NULL && mutex_tryenter(&db->db_mtx) == 0) {
474 db = multilist_sublist_prev(mls, db);
477 DTRACE_PROBE2(dbuf__evict__one, dmu_buf_impl_t *, db,
478 multilist_sublist_t *, mls);
481 multilist_sublist_remove(mls, db);
482 multilist_sublist_unlock(mls);
483 (void) refcount_remove_many(&dbuf_cache_size,
487 multilist_sublist_unlock(mls);
489 (void) tsd_set(zfs_dbuf_evict_key, NULL);
493 * The dbuf evict thread is responsible for aging out dbufs from the
494 * cache. Once the cache has reached it's maximum size, dbufs are removed
495 * and destroyed. The eviction thread will continue running until the size
496 * of the dbuf cache is at or below the maximum size. Once the dbuf is aged
497 * out of the cache it is destroyed and becomes eligible for arc eviction.
501 dbuf_evict_thread(void *unused __unused)
505 CALLB_CPR_INIT(&cpr, &dbuf_evict_lock, callb_generic_cpr, FTAG);
507 mutex_enter(&dbuf_evict_lock);
508 while (!dbuf_evict_thread_exit) {
509 while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit) {
510 CALLB_CPR_SAFE_BEGIN(&cpr);
511 (void) cv_timedwait_hires(&dbuf_evict_cv,
512 &dbuf_evict_lock, SEC2NSEC(1), MSEC2NSEC(1), 0);
513 CALLB_CPR_SAFE_END(&cpr, &dbuf_evict_lock);
515 mutex_exit(&dbuf_evict_lock);
518 * Keep evicting as long as we're above the low water mark
519 * for the cache. We do this without holding the locks to
520 * minimize lock contention.
522 while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit) {
526 mutex_enter(&dbuf_evict_lock);
529 dbuf_evict_thread_exit = B_FALSE;
530 cv_broadcast(&dbuf_evict_cv);
531 CALLB_CPR_EXIT(&cpr); /* drops dbuf_evict_lock */
536 * Wake up the dbuf eviction thread if the dbuf cache is at its max size.
537 * If the dbuf cache is at its high water mark, then evict a dbuf from the
538 * dbuf cache using the callers context.
541 dbuf_evict_notify(void)
545 * We use thread specific data to track when a thread has
546 * started processing evictions. This allows us to avoid deeply
547 * nested stacks that would have a call flow similar to this:
549 * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify()
552 * +-----dbuf_destroy()<--dbuf_evict_one()<--------+
554 * The dbuf_eviction_thread will always have its tsd set until
555 * that thread exits. All other threads will only set their tsd
556 * if they are participating in the eviction process. This only
557 * happens if the eviction thread is unable to process evictions
558 * fast enough. To keep the dbuf cache size in check, other threads
559 * can evict from the dbuf cache directly. Those threads will set
560 * their tsd values so that we ensure that they only evict one dbuf
561 * from the dbuf cache.
563 if (tsd_get(zfs_dbuf_evict_key) != NULL)
567 * We check if we should evict without holding the dbuf_evict_lock,
568 * because it's OK to occasionally make the wrong decision here,
569 * and grabbing the lock results in massive lock contention.
571 if (refcount_count(&dbuf_cache_size) > dbuf_cache_max_bytes) {
572 if (dbuf_cache_above_hiwater())
574 cv_signal(&dbuf_evict_cv);
581 uint64_t hsize = 1ULL << 16;
582 dbuf_hash_table_t *h = &dbuf_hash_table;
586 * The hash table is big enough to fill all of physical memory
587 * with an average 4K block size. The table will take up
588 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
590 while (hsize * 4096 < (uint64_t)physmem * PAGESIZE)
594 h->hash_table_mask = hsize - 1;
595 h->hash_table = kmem_zalloc(hsize * sizeof (void *), KM_NOSLEEP);
596 if (h->hash_table == NULL) {
597 /* XXX - we should really return an error instead of assert */
598 ASSERT(hsize > (1ULL << 10));
603 dbuf_kmem_cache = kmem_cache_create("dmu_buf_impl_t",
604 sizeof (dmu_buf_impl_t),
605 0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0);
607 for (i = 0; i < DBUF_MUTEXES; i++)
608 mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL);
611 * Setup the parameters for the dbuf cache. We cap the size of the
612 * dbuf cache to 1/32nd (default) of the size of the ARC.
614 dbuf_cache_max_bytes = MIN(dbuf_cache_max_bytes,
615 arc_max_bytes() >> dbuf_cache_max_shift);
618 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
619 * configuration is not required.
621 dbu_evict_taskq = taskq_create("dbu_evict", 1, minclsyspri, 0, 0, 0);
623 dbuf_cache = multilist_create(sizeof (dmu_buf_impl_t),
624 offsetof(dmu_buf_impl_t, db_cache_link),
625 dbuf_cache_multilist_index_func);
626 refcount_create(&dbuf_cache_size);
628 tsd_create(&zfs_dbuf_evict_key, NULL);
629 dbuf_evict_thread_exit = B_FALSE;
630 mutex_init(&dbuf_evict_lock, NULL, MUTEX_DEFAULT, NULL);
631 cv_init(&dbuf_evict_cv, NULL, CV_DEFAULT, NULL);
632 dbuf_cache_evict_thread = thread_create(NULL, 0, dbuf_evict_thread,
633 NULL, 0, &p0, TS_RUN, minclsyspri);
639 dbuf_hash_table_t *h = &dbuf_hash_table;
642 for (i = 0; i < DBUF_MUTEXES; i++)
643 mutex_destroy(&h->hash_mutexes[i]);
644 kmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
645 kmem_cache_destroy(dbuf_kmem_cache);
646 taskq_destroy(dbu_evict_taskq);
648 mutex_enter(&dbuf_evict_lock);
649 dbuf_evict_thread_exit = B_TRUE;
650 while (dbuf_evict_thread_exit) {
651 cv_signal(&dbuf_evict_cv);
652 cv_wait(&dbuf_evict_cv, &dbuf_evict_lock);
654 mutex_exit(&dbuf_evict_lock);
655 tsd_destroy(&zfs_dbuf_evict_key);
657 mutex_destroy(&dbuf_evict_lock);
658 cv_destroy(&dbuf_evict_cv);
660 refcount_destroy(&dbuf_cache_size);
661 multilist_destroy(dbuf_cache);
670 dbuf_verify(dmu_buf_impl_t *db)
673 dbuf_dirty_record_t *dr;
675 ASSERT(MUTEX_HELD(&db->db_mtx));
677 if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY))
680 ASSERT(db->db_objset != NULL);
684 ASSERT(db->db_parent == NULL);
685 ASSERT(db->db_blkptr == NULL);
687 ASSERT3U(db->db.db_object, ==, dn->dn_object);
688 ASSERT3P(db->db_objset, ==, dn->dn_objset);
689 ASSERT3U(db->db_level, <, dn->dn_nlevels);
690 ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
691 db->db_blkid == DMU_SPILL_BLKID ||
692 !avl_is_empty(&dn->dn_dbufs));
694 if (db->db_blkid == DMU_BONUS_BLKID) {
696 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
697 ASSERT3U(db->db.db_offset, ==, DMU_BONUS_BLKID);
698 } else if (db->db_blkid == DMU_SPILL_BLKID) {
700 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
701 ASSERT0(db->db.db_offset);
703 ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size);
706 for (dr = db->db_data_pending; dr != NULL; dr = dr->dr_next)
707 ASSERT(dr->dr_dbuf == db);
709 for (dr = db->db_last_dirty; dr != NULL; dr = dr->dr_next)
710 ASSERT(dr->dr_dbuf == db);
713 * We can't assert that db_size matches dn_datablksz because it
714 * can be momentarily different when another thread is doing
717 if (db->db_level == 0 && db->db.db_object == DMU_META_DNODE_OBJECT) {
718 dr = db->db_data_pending;
720 * It should only be modified in syncing context, so
721 * make sure we only have one copy of the data.
723 ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf);
726 /* verify db->db_blkptr */
728 if (db->db_parent == dn->dn_dbuf) {
729 /* db is pointed to by the dnode */
730 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
731 if (DMU_OBJECT_IS_SPECIAL(db->db.db_object))
732 ASSERT(db->db_parent == NULL);
734 ASSERT(db->db_parent != NULL);
735 if (db->db_blkid != DMU_SPILL_BLKID)
736 ASSERT3P(db->db_blkptr, ==,
737 &dn->dn_phys->dn_blkptr[db->db_blkid]);
739 /* db is pointed to by an indirect block */
740 int epb = db->db_parent->db.db_size >> SPA_BLKPTRSHIFT;
741 ASSERT3U(db->db_parent->db_level, ==, db->db_level+1);
742 ASSERT3U(db->db_parent->db.db_object, ==,
745 * dnode_grow_indblksz() can make this fail if we don't
746 * have the struct_rwlock. XXX indblksz no longer
747 * grows. safe to do this now?
749 if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
750 ASSERT3P(db->db_blkptr, ==,
751 ((blkptr_t *)db->db_parent->db.db_data +
752 db->db_blkid % epb));
756 if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) &&
757 (db->db_buf == NULL || db->db_buf->b_data) &&
758 db->db.db_data && db->db_blkid != DMU_BONUS_BLKID &&
759 db->db_state != DB_FILL && !dn->dn_free_txg) {
761 * If the blkptr isn't set but they have nonzero data,
762 * it had better be dirty, otherwise we'll lose that
763 * data when we evict this buffer.
765 * There is an exception to this rule for indirect blocks; in
766 * this case, if the indirect block is a hole, we fill in a few
767 * fields on each of the child blocks (importantly, birth time)
768 * to prevent hole birth times from being lost when you
769 * partially fill in a hole.
771 if (db->db_dirtycnt == 0) {
772 if (db->db_level == 0) {
773 uint64_t *buf = db->db.db_data;
776 for (i = 0; i < db->db.db_size >> 3; i++) {
780 blkptr_t *bps = db->db.db_data;
781 ASSERT3U(1 << DB_DNODE(db)->dn_indblkshift, ==,
784 * We want to verify that all the blkptrs in the
785 * indirect block are holes, but we may have
786 * automatically set up a few fields for them.
787 * We iterate through each blkptr and verify
788 * they only have those fields set.
791 i < db->db.db_size / sizeof (blkptr_t);
793 blkptr_t *bp = &bps[i];
794 ASSERT(ZIO_CHECKSUM_IS_ZERO(
797 DVA_IS_EMPTY(&bp->blk_dva[0]) &&
798 DVA_IS_EMPTY(&bp->blk_dva[1]) &&
799 DVA_IS_EMPTY(&bp->blk_dva[2]));
800 ASSERT0(bp->blk_fill);
801 ASSERT0(bp->blk_pad[0]);
802 ASSERT0(bp->blk_pad[1]);
803 ASSERT(!BP_IS_EMBEDDED(bp));
804 ASSERT(BP_IS_HOLE(bp));
805 ASSERT0(bp->blk_phys_birth);
815 dbuf_clear_data(dmu_buf_impl_t *db)
817 ASSERT(MUTEX_HELD(&db->db_mtx));
819 ASSERT3P(db->db_buf, ==, NULL);
820 db->db.db_data = NULL;
821 if (db->db_state != DB_NOFILL)
822 db->db_state = DB_UNCACHED;
826 dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf)
828 ASSERT(MUTEX_HELD(&db->db_mtx));
832 ASSERT(buf->b_data != NULL);
833 db->db.db_data = buf->b_data;
837 * Loan out an arc_buf for read. Return the loaned arc_buf.
840 dbuf_loan_arcbuf(dmu_buf_impl_t *db)
844 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
845 mutex_enter(&db->db_mtx);
846 if (arc_released(db->db_buf) || refcount_count(&db->db_holds) > 1) {
847 int blksz = db->db.db_size;
848 spa_t *spa = db->db_objset->os_spa;
850 mutex_exit(&db->db_mtx);
851 abuf = arc_loan_buf(spa, B_FALSE, blksz);
852 bcopy(db->db.db_data, abuf->b_data, blksz);
855 arc_loan_inuse_buf(abuf, db);
858 mutex_exit(&db->db_mtx);
864 * Calculate which level n block references the data at the level 0 offset
868 dbuf_whichblock(dnode_t *dn, int64_t level, uint64_t offset)
870 if (dn->dn_datablkshift != 0 && dn->dn_indblkshift != 0) {
872 * The level n blkid is equal to the level 0 blkid divided by
873 * the number of level 0s in a level n block.
875 * The level 0 blkid is offset >> datablkshift =
876 * offset / 2^datablkshift.
878 * The number of level 0s in a level n is the number of block
879 * pointers in an indirect block, raised to the power of level.
880 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
881 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
883 * Thus, the level n blkid is: offset /
884 * ((2^datablkshift)*(2^(level*(indblkshift - SPA_BLKPTRSHIFT)))
885 * = offset / 2^(datablkshift + level *
886 * (indblkshift - SPA_BLKPTRSHIFT))
887 * = offset >> (datablkshift + level *
888 * (indblkshift - SPA_BLKPTRSHIFT))
890 return (offset >> (dn->dn_datablkshift + level *
891 (dn->dn_indblkshift - SPA_BLKPTRSHIFT)));
893 ASSERT3U(offset, <, dn->dn_datablksz);
899 dbuf_read_done(zio_t *zio, arc_buf_t *buf, void *vdb)
901 dmu_buf_impl_t *db = vdb;
903 mutex_enter(&db->db_mtx);
904 ASSERT3U(db->db_state, ==, DB_READ);
906 * All reads are synchronous, so we must have a hold on the dbuf
908 ASSERT(refcount_count(&db->db_holds) > 0);
909 ASSERT(db->db_buf == NULL);
910 ASSERT(db->db.db_data == NULL);
911 if (db->db_level == 0 && db->db_freed_in_flight) {
912 /* we were freed in flight; disregard any error */
913 arc_release(buf, db);
914 bzero(buf->b_data, db->db.db_size);
916 db->db_freed_in_flight = FALSE;
917 dbuf_set_data(db, buf);
918 db->db_state = DB_CACHED;
919 } else if (zio == NULL || zio->io_error == 0) {
920 dbuf_set_data(db, buf);
921 db->db_state = DB_CACHED;
923 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
924 ASSERT3P(db->db_buf, ==, NULL);
925 arc_buf_destroy(buf, db);
926 db->db_state = DB_UNCACHED;
928 cv_broadcast(&db->db_changed);
929 dbuf_rele_and_unlock(db, NULL);
933 dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
937 arc_flags_t aflags = ARC_FLAG_NOWAIT;
941 ASSERT(!refcount_is_zero(&db->db_holds));
942 /* We need the struct_rwlock to prevent db_blkptr from changing. */
943 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
944 ASSERT(MUTEX_HELD(&db->db_mtx));
945 ASSERT(db->db_state == DB_UNCACHED);
946 ASSERT(db->db_buf == NULL);
948 if (db->db_blkid == DMU_BONUS_BLKID) {
949 int bonuslen = MIN(dn->dn_bonuslen, dn->dn_phys->dn_bonuslen);
951 ASSERT3U(bonuslen, <=, db->db.db_size);
952 db->db.db_data = zio_buf_alloc(DN_MAX_BONUSLEN);
953 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
954 if (bonuslen < DN_MAX_BONUSLEN)
955 bzero(db->db.db_data, DN_MAX_BONUSLEN);
957 bcopy(DN_BONUS(dn->dn_phys), db->db.db_data, bonuslen);
959 db->db_state = DB_CACHED;
960 mutex_exit(&db->db_mtx);
965 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
966 * processes the delete record and clears the bp while we are waiting
967 * for the dn_mtx (resulting in a "no" from block_freed).
969 if (db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr) ||
970 (db->db_level == 0 && (dnode_block_freed(dn, db->db_blkid) ||
971 BP_IS_HOLE(db->db_blkptr)))) {
972 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
974 dbuf_set_data(db, arc_alloc_buf(db->db_objset->os_spa, db, type,
976 bzero(db->db.db_data, db->db.db_size);
978 if (db->db_blkptr != NULL && db->db_level > 0 &&
979 BP_IS_HOLE(db->db_blkptr) &&
980 db->db_blkptr->blk_birth != 0) {
981 blkptr_t *bps = db->db.db_data;
982 for (int i = 0; i < ((1 <<
983 DB_DNODE(db)->dn_indblkshift) / sizeof (blkptr_t));
985 blkptr_t *bp = &bps[i];
986 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
987 1 << dn->dn_indblkshift);
989 BP_GET_LEVEL(db->db_blkptr) == 1 ?
991 BP_GET_LSIZE(db->db_blkptr));
992 BP_SET_TYPE(bp, BP_GET_TYPE(db->db_blkptr));
994 BP_GET_LEVEL(db->db_blkptr) - 1);
995 BP_SET_BIRTH(bp, db->db_blkptr->blk_birth, 0);
999 db->db_state = DB_CACHED;
1000 mutex_exit(&db->db_mtx);
1006 db->db_state = DB_READ;
1007 mutex_exit(&db->db_mtx);
1009 if (DBUF_IS_L2CACHEABLE(db))
1010 aflags |= ARC_FLAG_L2CACHE;
1012 SET_BOOKMARK(&zb, db->db_objset->os_dsl_dataset ?
1013 db->db_objset->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1014 db->db.db_object, db->db_level, db->db_blkid);
1016 dbuf_add_ref(db, NULL);
1018 (void) arc_read(zio, db->db_objset->os_spa, db->db_blkptr,
1019 dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ,
1020 (flags & DB_RF_CANFAIL) ? ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED,
1025 * This is our just-in-time copy function. It makes a copy of buffers that
1026 * have been modified in a previous transaction group before we access them in
1027 * the current active group.
1029 * This function is used in three places: when we are dirtying a buffer for the
1030 * first time in a txg, when we are freeing a range in a dnode that includes
1031 * this buffer, and when we are accessing a buffer which was received compressed
1032 * and later referenced in a WRITE_BYREF record.
1034 * Note that when we are called from dbuf_free_range() we do not put a hold on
1035 * the buffer, we just traverse the active dbuf list for the dnode.
1038 dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg)
1040 dbuf_dirty_record_t *dr = db->db_last_dirty;
1042 ASSERT(MUTEX_HELD(&db->db_mtx));
1043 ASSERT(db->db.db_data != NULL);
1044 ASSERT(db->db_level == 0);
1045 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT);
1048 (dr->dt.dl.dr_data !=
1049 ((db->db_blkid == DMU_BONUS_BLKID) ? db->db.db_data : db->db_buf)))
1053 * If the last dirty record for this dbuf has not yet synced
1054 * and its referencing the dbuf data, either:
1055 * reset the reference to point to a new copy,
1056 * or (if there a no active holders)
1057 * just null out the current db_data pointer.
1059 ASSERT(dr->dr_txg >= txg - 2);
1060 if (db->db_blkid == DMU_BONUS_BLKID) {
1061 /* Note that the data bufs here are zio_bufs */
1062 dr->dt.dl.dr_data = zio_buf_alloc(DN_MAX_BONUSLEN);
1063 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
1064 bcopy(db->db.db_data, dr->dt.dl.dr_data, DN_MAX_BONUSLEN);
1065 } else if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
1066 int size = arc_buf_size(db->db_buf);
1067 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1068 spa_t *spa = db->db_objset->os_spa;
1069 enum zio_compress compress_type =
1070 arc_get_compression(db->db_buf);
1072 if (compress_type == ZIO_COMPRESS_OFF) {
1073 dr->dt.dl.dr_data = arc_alloc_buf(spa, db, type, size);
1075 ASSERT3U(type, ==, ARC_BUFC_DATA);
1076 dr->dt.dl.dr_data = arc_alloc_compressed_buf(spa, db,
1077 size, arc_buf_lsize(db->db_buf), compress_type);
1079 bcopy(db->db.db_data, dr->dt.dl.dr_data->b_data, size);
1082 dbuf_clear_data(db);
1087 dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
1094 * We don't have to hold the mutex to check db_state because it
1095 * can't be freed while we have a hold on the buffer.
1097 ASSERT(!refcount_is_zero(&db->db_holds));
1099 if (db->db_state == DB_NOFILL)
1100 return (SET_ERROR(EIO));
1104 if ((flags & DB_RF_HAVESTRUCT) == 0)
1105 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1107 prefetch = db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1108 (flags & DB_RF_NOPREFETCH) == 0 && dn != NULL &&
1109 DBUF_IS_CACHEABLE(db);
1111 mutex_enter(&db->db_mtx);
1112 if (db->db_state == DB_CACHED) {
1114 * If the arc buf is compressed, we need to decompress it to
1115 * read the data. This could happen during the "zfs receive" of
1116 * a stream which is compressed and deduplicated.
1118 if (db->db_buf != NULL &&
1119 arc_get_compression(db->db_buf) != ZIO_COMPRESS_OFF) {
1120 dbuf_fix_old_data(db,
1121 spa_syncing_txg(dmu_objset_spa(db->db_objset)));
1122 err = arc_decompress(db->db_buf);
1123 dbuf_set_data(db, db->db_buf);
1125 mutex_exit(&db->db_mtx);
1127 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE);
1128 if ((flags & DB_RF_HAVESTRUCT) == 0)
1129 rw_exit(&dn->dn_struct_rwlock);
1131 } else if (db->db_state == DB_UNCACHED) {
1132 spa_t *spa = dn->dn_objset->os_spa;
1133 boolean_t need_wait = B_FALSE;
1136 db->db_blkptr != NULL && !BP_IS_HOLE(db->db_blkptr)) {
1137 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
1140 dbuf_read_impl(db, zio, flags);
1142 /* dbuf_read_impl has dropped db_mtx for us */
1145 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE);
1147 if ((flags & DB_RF_HAVESTRUCT) == 0)
1148 rw_exit(&dn->dn_struct_rwlock);
1152 err = zio_wait(zio);
1155 * Another reader came in while the dbuf was in flight
1156 * between UNCACHED and CACHED. Either a writer will finish
1157 * writing the buffer (sending the dbuf to CACHED) or the
1158 * first reader's request will reach the read_done callback
1159 * and send the dbuf to CACHED. Otherwise, a failure
1160 * occurred and the dbuf went to UNCACHED.
1162 mutex_exit(&db->db_mtx);
1164 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE);
1165 if ((flags & DB_RF_HAVESTRUCT) == 0)
1166 rw_exit(&dn->dn_struct_rwlock);
1169 /* Skip the wait per the caller's request. */
1170 mutex_enter(&db->db_mtx);
1171 if ((flags & DB_RF_NEVERWAIT) == 0) {
1172 while (db->db_state == DB_READ ||
1173 db->db_state == DB_FILL) {
1174 ASSERT(db->db_state == DB_READ ||
1175 (flags & DB_RF_HAVESTRUCT) == 0);
1176 DTRACE_PROBE2(blocked__read, dmu_buf_impl_t *,
1178 cv_wait(&db->db_changed, &db->db_mtx);
1180 if (db->db_state == DB_UNCACHED)
1181 err = SET_ERROR(EIO);
1183 mutex_exit(&db->db_mtx);
1190 dbuf_noread(dmu_buf_impl_t *db)
1192 ASSERT(!refcount_is_zero(&db->db_holds));
1193 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1194 mutex_enter(&db->db_mtx);
1195 while (db->db_state == DB_READ || db->db_state == DB_FILL)
1196 cv_wait(&db->db_changed, &db->db_mtx);
1197 if (db->db_state == DB_UNCACHED) {
1198 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1199 spa_t *spa = db->db_objset->os_spa;
1201 ASSERT(db->db_buf == NULL);
1202 ASSERT(db->db.db_data == NULL);
1203 dbuf_set_data(db, arc_alloc_buf(spa, db, type, db->db.db_size));
1204 db->db_state = DB_FILL;
1205 } else if (db->db_state == DB_NOFILL) {
1206 dbuf_clear_data(db);
1208 ASSERT3U(db->db_state, ==, DB_CACHED);
1210 mutex_exit(&db->db_mtx);
1214 dbuf_unoverride(dbuf_dirty_record_t *dr)
1216 dmu_buf_impl_t *db = dr->dr_dbuf;
1217 blkptr_t *bp = &dr->dt.dl.dr_overridden_by;
1218 uint64_t txg = dr->dr_txg;
1220 ASSERT(MUTEX_HELD(&db->db_mtx));
1222 * This assert is valid because dmu_sync() expects to be called by
1223 * a zilog's get_data while holding a range lock. This call only
1224 * comes from dbuf_dirty() callers who must also hold a range lock.
1226 ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC);
1227 ASSERT(db->db_level == 0);
1229 if (db->db_blkid == DMU_BONUS_BLKID ||
1230 dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN)
1233 ASSERT(db->db_data_pending != dr);
1235 /* free this block */
1236 if (!BP_IS_HOLE(bp) && !dr->dt.dl.dr_nopwrite)
1237 zio_free(db->db_objset->os_spa, txg, bp);
1239 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
1240 dr->dt.dl.dr_nopwrite = B_FALSE;
1243 * Release the already-written buffer, so we leave it in
1244 * a consistent dirty state. Note that all callers are
1245 * modifying the buffer, so they will immediately do
1246 * another (redundant) arc_release(). Therefore, leave
1247 * the buf thawed to save the effort of freezing &
1248 * immediately re-thawing it.
1250 arc_release(dr->dt.dl.dr_data, db);
1254 * Evict (if its unreferenced) or clear (if its referenced) any level-0
1255 * data blocks in the free range, so that any future readers will find
1259 dbuf_free_range(dnode_t *dn, uint64_t start_blkid, uint64_t end_blkid,
1262 dmu_buf_impl_t db_search;
1263 dmu_buf_impl_t *db, *db_next;
1264 uint64_t txg = tx->tx_txg;
1267 if (end_blkid > dn->dn_maxblkid &&
1268 !(start_blkid == DMU_SPILL_BLKID || end_blkid == DMU_SPILL_BLKID))
1269 end_blkid = dn->dn_maxblkid;
1270 dprintf_dnode(dn, "start=%llu end=%llu\n", start_blkid, end_blkid);
1272 db_search.db_level = 0;
1273 db_search.db_blkid = start_blkid;
1274 db_search.db_state = DB_SEARCH;
1276 mutex_enter(&dn->dn_dbufs_mtx);
1277 db = avl_find(&dn->dn_dbufs, &db_search, &where);
1278 ASSERT3P(db, ==, NULL);
1280 db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER);
1282 for (; db != NULL; db = db_next) {
1283 db_next = AVL_NEXT(&dn->dn_dbufs, db);
1284 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1286 if (db->db_level != 0 || db->db_blkid > end_blkid) {
1289 ASSERT3U(db->db_blkid, >=, start_blkid);
1291 /* found a level 0 buffer in the range */
1292 mutex_enter(&db->db_mtx);
1293 if (dbuf_undirty(db, tx)) {
1294 /* mutex has been dropped and dbuf destroyed */
1298 if (db->db_state == DB_UNCACHED ||
1299 db->db_state == DB_NOFILL ||
1300 db->db_state == DB_EVICTING) {
1301 ASSERT(db->db.db_data == NULL);
1302 mutex_exit(&db->db_mtx);
1305 if (db->db_state == DB_READ || db->db_state == DB_FILL) {
1306 /* will be handled in dbuf_read_done or dbuf_rele */
1307 db->db_freed_in_flight = TRUE;
1308 mutex_exit(&db->db_mtx);
1311 if (refcount_count(&db->db_holds) == 0) {
1316 /* The dbuf is referenced */
1318 if (db->db_last_dirty != NULL) {
1319 dbuf_dirty_record_t *dr = db->db_last_dirty;
1321 if (dr->dr_txg == txg) {
1323 * This buffer is "in-use", re-adjust the file
1324 * size to reflect that this buffer may
1325 * contain new data when we sync.
1327 if (db->db_blkid != DMU_SPILL_BLKID &&
1328 db->db_blkid > dn->dn_maxblkid)
1329 dn->dn_maxblkid = db->db_blkid;
1330 dbuf_unoverride(dr);
1333 * This dbuf is not dirty in the open context.
1334 * Either uncache it (if its not referenced in
1335 * the open context) or reset its contents to
1338 dbuf_fix_old_data(db, txg);
1341 /* clear the contents if its cached */
1342 if (db->db_state == DB_CACHED) {
1343 ASSERT(db->db.db_data != NULL);
1344 arc_release(db->db_buf, db);
1345 bzero(db->db.db_data, db->db.db_size);
1346 arc_buf_freeze(db->db_buf);
1349 mutex_exit(&db->db_mtx);
1351 mutex_exit(&dn->dn_dbufs_mtx);
1355 dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx)
1357 arc_buf_t *buf, *obuf;
1358 int osize = db->db.db_size;
1359 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1362 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1367 /* XXX does *this* func really need the lock? */
1368 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
1371 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
1372 * is OK, because there can be no other references to the db
1373 * when we are changing its size, so no concurrent DB_FILL can
1377 * XXX we should be doing a dbuf_read, checking the return
1378 * value and returning that up to our callers
1380 dmu_buf_will_dirty(&db->db, tx);
1382 /* create the data buffer for the new block */
1383 buf = arc_alloc_buf(dn->dn_objset->os_spa, db, type, size);
1385 /* copy old block data to the new block */
1387 bcopy(obuf->b_data, buf->b_data, MIN(osize, size));
1388 /* zero the remainder */
1390 bzero((uint8_t *)buf->b_data + osize, size - osize);
1392 mutex_enter(&db->db_mtx);
1393 dbuf_set_data(db, buf);
1394 arc_buf_destroy(obuf, db);
1395 db->db.db_size = size;
1397 if (db->db_level == 0) {
1398 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
1399 db->db_last_dirty->dt.dl.dr_data = buf;
1401 mutex_exit(&db->db_mtx);
1403 dmu_objset_willuse_space(dn->dn_objset, size - osize, tx);
1408 dbuf_release_bp(dmu_buf_impl_t *db)
1410 objset_t *os = db->db_objset;
1412 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
1413 ASSERT(arc_released(os->os_phys_buf) ||
1414 list_link_active(&os->os_dsl_dataset->ds_synced_link));
1415 ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf));
1417 (void) arc_release(db->db_buf, db);
1421 * We already have a dirty record for this TXG, and we are being
1425 dbuf_redirty(dbuf_dirty_record_t *dr)
1427 dmu_buf_impl_t *db = dr->dr_dbuf;
1429 ASSERT(MUTEX_HELD(&db->db_mtx));
1431 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID) {
1433 * If this buffer has already been written out,
1434 * we now need to reset its state.
1436 dbuf_unoverride(dr);
1437 if (db->db.db_object != DMU_META_DNODE_OBJECT &&
1438 db->db_state != DB_NOFILL) {
1439 /* Already released on initial dirty, so just thaw. */
1440 ASSERT(arc_released(db->db_buf));
1441 arc_buf_thaw(db->db_buf);
1446 dbuf_dirty_record_t *
1447 dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1451 dbuf_dirty_record_t **drp, *dr;
1452 int drop_struct_lock = FALSE;
1453 int txgoff = tx->tx_txg & TXG_MASK;
1455 ASSERT(tx->tx_txg != 0);
1456 ASSERT(!refcount_is_zero(&db->db_holds));
1457 DMU_TX_DIRTY_BUF(tx, db);
1462 * Shouldn't dirty a regular buffer in syncing context. Private
1463 * objects may be dirtied in syncing context, but only if they
1464 * were already pre-dirtied in open context.
1467 if (dn->dn_objset->os_dsl_dataset != NULL) {
1468 rrw_enter(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock,
1471 ASSERT(!dmu_tx_is_syncing(tx) ||
1472 BP_IS_HOLE(dn->dn_objset->os_rootbp) ||
1473 DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1474 dn->dn_objset->os_dsl_dataset == NULL);
1475 if (dn->dn_objset->os_dsl_dataset != NULL)
1476 rrw_exit(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock, FTAG);
1479 * We make this assert for private objects as well, but after we
1480 * check if we're already dirty. They are allowed to re-dirty
1481 * in syncing context.
1483 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1484 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1485 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1487 mutex_enter(&db->db_mtx);
1489 * XXX make this true for indirects too? The problem is that
1490 * transactions created with dmu_tx_create_assigned() from
1491 * syncing context don't bother holding ahead.
1493 ASSERT(db->db_level != 0 ||
1494 db->db_state == DB_CACHED || db->db_state == DB_FILL ||
1495 db->db_state == DB_NOFILL);
1497 mutex_enter(&dn->dn_mtx);
1499 * Don't set dirtyctx to SYNC if we're just modifying this as we
1500 * initialize the objset.
1502 if (dn->dn_dirtyctx == DN_UNDIRTIED) {
1503 if (dn->dn_objset->os_dsl_dataset != NULL) {
1504 rrw_enter(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock,
1507 if (!BP_IS_HOLE(dn->dn_objset->os_rootbp)) {
1508 dn->dn_dirtyctx = (dmu_tx_is_syncing(tx) ?
1509 DN_DIRTY_SYNC : DN_DIRTY_OPEN);
1510 ASSERT(dn->dn_dirtyctx_firstset == NULL);
1511 dn->dn_dirtyctx_firstset = kmem_alloc(1, KM_SLEEP);
1513 if (dn->dn_objset->os_dsl_dataset != NULL) {
1514 rrw_exit(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock,
1518 mutex_exit(&dn->dn_mtx);
1520 if (db->db_blkid == DMU_SPILL_BLKID)
1521 dn->dn_have_spill = B_TRUE;
1524 * If this buffer is already dirty, we're done.
1526 drp = &db->db_last_dirty;
1527 ASSERT(*drp == NULL || (*drp)->dr_txg <= tx->tx_txg ||
1528 db->db.db_object == DMU_META_DNODE_OBJECT);
1529 while ((dr = *drp) != NULL && dr->dr_txg > tx->tx_txg)
1531 if (dr && dr->dr_txg == tx->tx_txg) {
1535 mutex_exit(&db->db_mtx);
1540 * Only valid if not already dirty.
1542 ASSERT(dn->dn_object == 0 ||
1543 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1544 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1546 ASSERT3U(dn->dn_nlevels, >, db->db_level);
1549 * We should only be dirtying in syncing context if it's the
1550 * mos or we're initializing the os or it's a special object.
1551 * However, we are allowed to dirty in syncing context provided
1552 * we already dirtied it in open context. Hence we must make
1553 * this assertion only if we're not already dirty.
1556 VERIFY3U(tx->tx_txg, <=, spa_final_dirty_txg(os->os_spa));
1558 if (dn->dn_objset->os_dsl_dataset != NULL)
1559 rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_READER, FTAG);
1560 ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1561 os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp));
1562 if (dn->dn_objset->os_dsl_dataset != NULL)
1563 rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG);
1565 ASSERT(db->db.db_size != 0);
1567 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1569 if (db->db_blkid != DMU_BONUS_BLKID) {
1570 dmu_objset_willuse_space(os, db->db.db_size, tx);
1574 * If this buffer is dirty in an old transaction group we need
1575 * to make a copy of it so that the changes we make in this
1576 * transaction group won't leak out when we sync the older txg.
1578 dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP);
1579 if (db->db_level == 0) {
1580 void *data_old = db->db_buf;
1582 if (db->db_state != DB_NOFILL) {
1583 if (db->db_blkid == DMU_BONUS_BLKID) {
1584 dbuf_fix_old_data(db, tx->tx_txg);
1585 data_old = db->db.db_data;
1586 } else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
1588 * Release the data buffer from the cache so
1589 * that we can modify it without impacting
1590 * possible other users of this cached data
1591 * block. Note that indirect blocks and
1592 * private objects are not released until the
1593 * syncing state (since they are only modified
1596 arc_release(db->db_buf, db);
1597 dbuf_fix_old_data(db, tx->tx_txg);
1598 data_old = db->db_buf;
1600 ASSERT(data_old != NULL);
1602 dr->dt.dl.dr_data = data_old;
1604 mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_DEFAULT, NULL);
1605 list_create(&dr->dt.di.dr_children,
1606 sizeof (dbuf_dirty_record_t),
1607 offsetof(dbuf_dirty_record_t, dr_dirty_node));
1609 if (db->db_blkid != DMU_BONUS_BLKID && os->os_dsl_dataset != NULL)
1610 dr->dr_accounted = db->db.db_size;
1612 dr->dr_txg = tx->tx_txg;
1617 * We could have been freed_in_flight between the dbuf_noread
1618 * and dbuf_dirty. We win, as though the dbuf_noread() had
1619 * happened after the free.
1621 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1622 db->db_blkid != DMU_SPILL_BLKID) {
1623 mutex_enter(&dn->dn_mtx);
1624 if (dn->dn_free_ranges[txgoff] != NULL) {
1625 range_tree_clear(dn->dn_free_ranges[txgoff],
1628 mutex_exit(&dn->dn_mtx);
1629 db->db_freed_in_flight = FALSE;
1633 * This buffer is now part of this txg
1635 dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg);
1636 db->db_dirtycnt += 1;
1637 ASSERT3U(db->db_dirtycnt, <=, 3);
1639 mutex_exit(&db->db_mtx);
1641 if (db->db_blkid == DMU_BONUS_BLKID ||
1642 db->db_blkid == DMU_SPILL_BLKID) {
1643 mutex_enter(&dn->dn_mtx);
1644 ASSERT(!list_link_active(&dr->dr_dirty_node));
1645 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1646 mutex_exit(&dn->dn_mtx);
1647 dnode_setdirty(dn, tx);
1653 * The dn_struct_rwlock prevents db_blkptr from changing
1654 * due to a write from syncing context completing
1655 * while we are running, so we want to acquire it before
1656 * looking at db_blkptr.
1658 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
1659 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1660 drop_struct_lock = TRUE;
1664 * We need to hold the dn_struct_rwlock to make this assertion,
1665 * because it protects dn_phys / dn_next_nlevels from changing.
1667 ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) ||
1668 dn->dn_phys->dn_nlevels > db->db_level ||
1669 dn->dn_next_nlevels[txgoff] > db->db_level ||
1670 dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level ||
1671 dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level);
1674 * If we are overwriting a dedup BP, then unless it is snapshotted,
1675 * when we get to syncing context we will need to decrement its
1676 * refcount in the DDT. Prefetch the relevant DDT block so that
1677 * syncing context won't have to wait for the i/o.
1679 ddt_prefetch(os->os_spa, db->db_blkptr);
1681 if (db->db_level == 0) {
1682 dnode_new_blkid(dn, db->db_blkid, tx, drop_struct_lock);
1683 ASSERT(dn->dn_maxblkid >= db->db_blkid);
1686 if (db->db_level+1 < dn->dn_nlevels) {
1687 dmu_buf_impl_t *parent = db->db_parent;
1688 dbuf_dirty_record_t *di;
1689 int parent_held = FALSE;
1691 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
1692 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1694 parent = dbuf_hold_level(dn, db->db_level+1,
1695 db->db_blkid >> epbs, FTAG);
1696 ASSERT(parent != NULL);
1699 if (drop_struct_lock)
1700 rw_exit(&dn->dn_struct_rwlock);
1701 ASSERT3U(db->db_level+1, ==, parent->db_level);
1702 di = dbuf_dirty(parent, tx);
1704 dbuf_rele(parent, FTAG);
1706 mutex_enter(&db->db_mtx);
1708 * Since we've dropped the mutex, it's possible that
1709 * dbuf_undirty() might have changed this out from under us.
1711 if (db->db_last_dirty == dr ||
1712 dn->dn_object == DMU_META_DNODE_OBJECT) {
1713 mutex_enter(&di->dt.di.dr_mtx);
1714 ASSERT3U(di->dr_txg, ==, tx->tx_txg);
1715 ASSERT(!list_link_active(&dr->dr_dirty_node));
1716 list_insert_tail(&di->dt.di.dr_children, dr);
1717 mutex_exit(&di->dt.di.dr_mtx);
1720 mutex_exit(&db->db_mtx);
1722 ASSERT(db->db_level+1 == dn->dn_nlevels);
1723 ASSERT(db->db_blkid < dn->dn_nblkptr);
1724 ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf);
1725 mutex_enter(&dn->dn_mtx);
1726 ASSERT(!list_link_active(&dr->dr_dirty_node));
1727 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1728 mutex_exit(&dn->dn_mtx);
1729 if (drop_struct_lock)
1730 rw_exit(&dn->dn_struct_rwlock);
1733 dnode_setdirty(dn, tx);
1739 * Undirty a buffer in the transaction group referenced by the given
1740 * transaction. Return whether this evicted the dbuf.
1743 dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1746 uint64_t txg = tx->tx_txg;
1747 dbuf_dirty_record_t *dr, **drp;
1752 * Due to our use of dn_nlevels below, this can only be called
1753 * in open context, unless we are operating on the MOS.
1754 * From syncing context, dn_nlevels may be different from the
1755 * dn_nlevels used when dbuf was dirtied.
1757 ASSERT(db->db_objset ==
1758 dmu_objset_pool(db->db_objset)->dp_meta_objset ||
1759 txg != spa_syncing_txg(dmu_objset_spa(db->db_objset)));
1760 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1761 ASSERT0(db->db_level);
1762 ASSERT(MUTEX_HELD(&db->db_mtx));
1765 * If this buffer is not dirty, we're done.
1767 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1768 if (dr->dr_txg <= txg)
1770 if (dr == NULL || dr->dr_txg < txg)
1772 ASSERT(dr->dr_txg == txg);
1773 ASSERT(dr->dr_dbuf == db);
1778 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1780 ASSERT(db->db.db_size != 0);
1782 dsl_pool_undirty_space(dmu_objset_pool(dn->dn_objset),
1783 dr->dr_accounted, txg);
1788 * Note that there are three places in dbuf_dirty()
1789 * where this dirty record may be put on a list.
1790 * Make sure to do a list_remove corresponding to
1791 * every one of those list_insert calls.
1793 if (dr->dr_parent) {
1794 mutex_enter(&dr->dr_parent->dt.di.dr_mtx);
1795 list_remove(&dr->dr_parent->dt.di.dr_children, dr);
1796 mutex_exit(&dr->dr_parent->dt.di.dr_mtx);
1797 } else if (db->db_blkid == DMU_SPILL_BLKID ||
1798 db->db_level + 1 == dn->dn_nlevels) {
1799 ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf);
1800 mutex_enter(&dn->dn_mtx);
1801 list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr);
1802 mutex_exit(&dn->dn_mtx);
1806 if (db->db_state != DB_NOFILL) {
1807 dbuf_unoverride(dr);
1809 ASSERT(db->db_buf != NULL);
1810 ASSERT(dr->dt.dl.dr_data != NULL);
1811 if (dr->dt.dl.dr_data != db->db_buf)
1812 arc_buf_destroy(dr->dt.dl.dr_data, db);
1815 kmem_free(dr, sizeof (dbuf_dirty_record_t));
1817 ASSERT(db->db_dirtycnt > 0);
1818 db->db_dirtycnt -= 1;
1820 if (refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) {
1821 ASSERT(db->db_state == DB_NOFILL || arc_released(db->db_buf));
1830 dmu_buf_will_dirty(dmu_buf_t *db_fake, dmu_tx_t *tx)
1832 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1833 int rf = DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH;
1835 ASSERT(tx->tx_txg != 0);
1836 ASSERT(!refcount_is_zero(&db->db_holds));
1839 * Quick check for dirtyness. For already dirty blocks, this
1840 * reduces runtime of this function by >90%, and overall performance
1841 * by 50% for some workloads (e.g. file deletion with indirect blocks
1844 mutex_enter(&db->db_mtx);
1845 dbuf_dirty_record_t *dr;
1846 for (dr = db->db_last_dirty;
1847 dr != NULL && dr->dr_txg >= tx->tx_txg; dr = dr->dr_next) {
1849 * It's possible that it is already dirty but not cached,
1850 * because there are some calls to dbuf_dirty() that don't
1851 * go through dmu_buf_will_dirty().
1853 if (dr->dr_txg == tx->tx_txg && db->db_state == DB_CACHED) {
1854 /* This dbuf is already dirty and cached. */
1856 mutex_exit(&db->db_mtx);
1860 mutex_exit(&db->db_mtx);
1863 if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock))
1864 rf |= DB_RF_HAVESTRUCT;
1866 (void) dbuf_read(db, NULL, rf);
1867 (void) dbuf_dirty(db, tx);
1871 dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1873 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1875 db->db_state = DB_NOFILL;
1877 dmu_buf_will_fill(db_fake, tx);
1881 dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1883 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1885 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1886 ASSERT(tx->tx_txg != 0);
1887 ASSERT(db->db_level == 0);
1888 ASSERT(!refcount_is_zero(&db->db_holds));
1890 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
1891 dmu_tx_private_ok(tx));
1894 (void) dbuf_dirty(db, tx);
1897 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1900 dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx)
1902 mutex_enter(&db->db_mtx);
1905 if (db->db_state == DB_FILL) {
1906 if (db->db_level == 0 && db->db_freed_in_flight) {
1907 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1908 /* we were freed while filling */
1909 /* XXX dbuf_undirty? */
1910 bzero(db->db.db_data, db->db.db_size);
1911 db->db_freed_in_flight = FALSE;
1913 db->db_state = DB_CACHED;
1914 cv_broadcast(&db->db_changed);
1916 mutex_exit(&db->db_mtx);
1920 dmu_buf_write_embedded(dmu_buf_t *dbuf, void *data,
1921 bp_embedded_type_t etype, enum zio_compress comp,
1922 int uncompressed_size, int compressed_size, int byteorder,
1925 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
1926 struct dirty_leaf *dl;
1927 dmu_object_type_t type;
1929 if (etype == BP_EMBEDDED_TYPE_DATA) {
1930 ASSERT(spa_feature_is_active(dmu_objset_spa(db->db_objset),
1931 SPA_FEATURE_EMBEDDED_DATA));
1935 type = DB_DNODE(db)->dn_type;
1938 ASSERT0(db->db_level);
1939 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1941 dmu_buf_will_not_fill(dbuf, tx);
1943 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
1944 dl = &db->db_last_dirty->dt.dl;
1945 encode_embedded_bp_compressed(&dl->dr_overridden_by,
1946 data, comp, uncompressed_size, compressed_size);
1947 BPE_SET_ETYPE(&dl->dr_overridden_by, etype);
1948 BP_SET_TYPE(&dl->dr_overridden_by, type);
1949 BP_SET_LEVEL(&dl->dr_overridden_by, 0);
1950 BP_SET_BYTEORDER(&dl->dr_overridden_by, byteorder);
1952 dl->dr_override_state = DR_OVERRIDDEN;
1953 dl->dr_overridden_by.blk_birth = db->db_last_dirty->dr_txg;
1957 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1958 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1961 dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx)
1963 ASSERT(!refcount_is_zero(&db->db_holds));
1964 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1965 ASSERT(db->db_level == 0);
1966 ASSERT3U(dbuf_is_metadata(db), ==, arc_is_metadata(buf));
1967 ASSERT(buf != NULL);
1968 ASSERT(arc_buf_lsize(buf) == db->db.db_size);
1969 ASSERT(tx->tx_txg != 0);
1971 arc_return_buf(buf, db);
1972 ASSERT(arc_released(buf));
1974 mutex_enter(&db->db_mtx);
1976 while (db->db_state == DB_READ || db->db_state == DB_FILL)
1977 cv_wait(&db->db_changed, &db->db_mtx);
1979 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED);
1981 if (db->db_state == DB_CACHED &&
1982 refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) {
1983 mutex_exit(&db->db_mtx);
1984 (void) dbuf_dirty(db, tx);
1985 bcopy(buf->b_data, db->db.db_data, db->db.db_size);
1986 arc_buf_destroy(buf, db);
1987 xuio_stat_wbuf_copied();
1991 xuio_stat_wbuf_nocopy();
1992 if (db->db_state == DB_CACHED) {
1993 dbuf_dirty_record_t *dr = db->db_last_dirty;
1995 ASSERT(db->db_buf != NULL);
1996 if (dr != NULL && dr->dr_txg == tx->tx_txg) {
1997 ASSERT(dr->dt.dl.dr_data == db->db_buf);
1998 if (!arc_released(db->db_buf)) {
1999 ASSERT(dr->dt.dl.dr_override_state ==
2001 arc_release(db->db_buf, db);
2003 dr->dt.dl.dr_data = buf;
2004 arc_buf_destroy(db->db_buf, db);
2005 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) {
2006 arc_release(db->db_buf, db);
2007 arc_buf_destroy(db->db_buf, db);
2011 ASSERT(db->db_buf == NULL);
2012 dbuf_set_data(db, buf);
2013 db->db_state = DB_FILL;
2014 mutex_exit(&db->db_mtx);
2015 (void) dbuf_dirty(db, tx);
2016 dmu_buf_fill_done(&db->db, tx);
2020 dbuf_destroy(dmu_buf_impl_t *db)
2023 dmu_buf_impl_t *parent = db->db_parent;
2024 dmu_buf_impl_t *dndb;
2026 ASSERT(MUTEX_HELD(&db->db_mtx));
2027 ASSERT(refcount_is_zero(&db->db_holds));
2029 if (db->db_buf != NULL) {
2030 arc_buf_destroy(db->db_buf, db);
2034 if (db->db_blkid == DMU_BONUS_BLKID) {
2035 ASSERT(db->db.db_data != NULL);
2036 zio_buf_free(db->db.db_data, DN_MAX_BONUSLEN);
2037 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
2038 db->db_state = DB_UNCACHED;
2041 dbuf_clear_data(db);
2043 if (multilist_link_active(&db->db_cache_link)) {
2044 multilist_remove(dbuf_cache, db);
2045 (void) refcount_remove_many(&dbuf_cache_size,
2046 db->db.db_size, db);
2049 ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL);
2050 ASSERT(db->db_data_pending == NULL);
2052 db->db_state = DB_EVICTING;
2053 db->db_blkptr = NULL;
2056 * Now that db_state is DB_EVICTING, nobody else can find this via
2057 * the hash table. We can now drop db_mtx, which allows us to
2058 * acquire the dn_dbufs_mtx.
2060 mutex_exit(&db->db_mtx);
2065 if (db->db_blkid != DMU_BONUS_BLKID) {
2066 boolean_t needlock = !MUTEX_HELD(&dn->dn_dbufs_mtx);
2068 mutex_enter(&dn->dn_dbufs_mtx);
2069 avl_remove(&dn->dn_dbufs, db);
2070 atomic_dec_32(&dn->dn_dbufs_count);
2074 mutex_exit(&dn->dn_dbufs_mtx);
2076 * Decrementing the dbuf count means that the hold corresponding
2077 * to the removed dbuf is no longer discounted in dnode_move(),
2078 * so the dnode cannot be moved until after we release the hold.
2079 * The membar_producer() ensures visibility of the decremented
2080 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
2084 db->db_dnode_handle = NULL;
2086 dbuf_hash_remove(db);
2091 ASSERT(refcount_is_zero(&db->db_holds));
2093 db->db_parent = NULL;
2095 ASSERT(db->db_buf == NULL);
2096 ASSERT(db->db.db_data == NULL);
2097 ASSERT(db->db_hash_next == NULL);
2098 ASSERT(db->db_blkptr == NULL);
2099 ASSERT(db->db_data_pending == NULL);
2100 ASSERT(!multilist_link_active(&db->db_cache_link));
2102 kmem_cache_free(dbuf_kmem_cache, db);
2103 arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
2106 * If this dbuf is referenced from an indirect dbuf,
2107 * decrement the ref count on the indirect dbuf.
2109 if (parent && parent != dndb)
2110 dbuf_rele(parent, db);
2114 * Note: While bpp will always be updated if the function returns success,
2115 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
2116 * this happens when the dnode is the meta-dnode, or a userused or groupused
2120 dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse,
2121 dmu_buf_impl_t **parentp, blkptr_t **bpp)
2126 ASSERT(blkid != DMU_BONUS_BLKID);
2128 if (blkid == DMU_SPILL_BLKID) {
2129 mutex_enter(&dn->dn_mtx);
2130 if (dn->dn_have_spill &&
2131 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
2132 *bpp = &dn->dn_phys->dn_spill;
2135 dbuf_add_ref(dn->dn_dbuf, NULL);
2136 *parentp = dn->dn_dbuf;
2137 mutex_exit(&dn->dn_mtx);
2142 (dn->dn_phys->dn_nlevels == 0) ? 1 : dn->dn_phys->dn_nlevels;
2143 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
2145 ASSERT3U(level * epbs, <, 64);
2146 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2148 * This assertion shouldn't trip as long as the max indirect block size
2149 * is less than 1M. The reason for this is that up to that point,
2150 * the number of levels required to address an entire object with blocks
2151 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In
2152 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55
2153 * (i.e. we can address the entire object), objects will all use at most
2154 * N-1 levels and the assertion won't overflow. However, once epbs is
2155 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be
2156 * enough to address an entire object, so objects will have 5 levels,
2157 * but then this assertion will overflow.
2159 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we
2160 * need to redo this logic to handle overflows.
2162 ASSERT(level >= nlevels ||
2163 ((nlevels - level - 1) * epbs) +
2164 highbit64(dn->dn_phys->dn_nblkptr) <= 64);
2165 if (level >= nlevels ||
2166 blkid >= ((uint64_t)dn->dn_phys->dn_nblkptr <<
2167 ((nlevels - level - 1) * epbs)) ||
2169 blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) {
2170 /* the buffer has no parent yet */
2171 return (SET_ERROR(ENOENT));
2172 } else if (level < nlevels-1) {
2173 /* this block is referenced from an indirect block */
2174 int err = dbuf_hold_impl(dn, level+1,
2175 blkid >> epbs, fail_sparse, FALSE, NULL, parentp);
2178 err = dbuf_read(*parentp, NULL,
2179 (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL));
2181 dbuf_rele(*parentp, NULL);
2185 *bpp = ((blkptr_t *)(*parentp)->db.db_data) +
2186 (blkid & ((1ULL << epbs) - 1));
2187 if (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))
2188 ASSERT(BP_IS_HOLE(*bpp));
2191 /* the block is referenced from the dnode */
2192 ASSERT3U(level, ==, nlevels-1);
2193 ASSERT(dn->dn_phys->dn_nblkptr == 0 ||
2194 blkid < dn->dn_phys->dn_nblkptr);
2196 dbuf_add_ref(dn->dn_dbuf, NULL);
2197 *parentp = dn->dn_dbuf;
2199 *bpp = &dn->dn_phys->dn_blkptr[blkid];
2204 static dmu_buf_impl_t *
2205 dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid,
2206 dmu_buf_impl_t *parent, blkptr_t *blkptr)
2208 objset_t *os = dn->dn_objset;
2209 dmu_buf_impl_t *db, *odb;
2211 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2212 ASSERT(dn->dn_type != DMU_OT_NONE);
2214 db = kmem_cache_alloc(dbuf_kmem_cache, KM_SLEEP);
2217 db->db.db_object = dn->dn_object;
2218 db->db_level = level;
2219 db->db_blkid = blkid;
2220 db->db_last_dirty = NULL;
2221 db->db_dirtycnt = 0;
2222 db->db_dnode_handle = dn->dn_handle;
2223 db->db_parent = parent;
2224 db->db_blkptr = blkptr;
2227 db->db_user_immediate_evict = FALSE;
2228 db->db_freed_in_flight = FALSE;
2229 db->db_pending_evict = FALSE;
2231 if (blkid == DMU_BONUS_BLKID) {
2232 ASSERT3P(parent, ==, dn->dn_dbuf);
2233 db->db.db_size = DN_MAX_BONUSLEN -
2234 (dn->dn_nblkptr-1) * sizeof (blkptr_t);
2235 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
2236 db->db.db_offset = DMU_BONUS_BLKID;
2237 db->db_state = DB_UNCACHED;
2238 /* the bonus dbuf is not placed in the hash table */
2239 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
2241 } else if (blkid == DMU_SPILL_BLKID) {
2242 db->db.db_size = (blkptr != NULL) ?
2243 BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE;
2244 db->db.db_offset = 0;
2247 db->db_level ? 1 << dn->dn_indblkshift : dn->dn_datablksz;
2248 db->db.db_size = blocksize;
2249 db->db.db_offset = db->db_blkid * blocksize;
2253 * Hold the dn_dbufs_mtx while we get the new dbuf
2254 * in the hash table *and* added to the dbufs list.
2255 * This prevents a possible deadlock with someone
2256 * trying to look up this dbuf before its added to the
2259 mutex_enter(&dn->dn_dbufs_mtx);
2260 db->db_state = DB_EVICTING;
2261 if ((odb = dbuf_hash_insert(db)) != NULL) {
2262 /* someone else inserted it first */
2263 kmem_cache_free(dbuf_kmem_cache, db);
2264 mutex_exit(&dn->dn_dbufs_mtx);
2267 avl_add(&dn->dn_dbufs, db);
2269 db->db_state = DB_UNCACHED;
2270 mutex_exit(&dn->dn_dbufs_mtx);
2271 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
2273 if (parent && parent != dn->dn_dbuf)
2274 dbuf_add_ref(parent, db);
2276 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
2277 refcount_count(&dn->dn_holds) > 0);
2278 (void) refcount_add(&dn->dn_holds, db);
2279 atomic_inc_32(&dn->dn_dbufs_count);
2281 dprintf_dbuf(db, "db=%p\n", db);
2286 typedef struct dbuf_prefetch_arg {
2287 spa_t *dpa_spa; /* The spa to issue the prefetch in. */
2288 zbookmark_phys_t dpa_zb; /* The target block to prefetch. */
2289 int dpa_epbs; /* Entries (blkptr_t's) Per Block Shift. */
2290 int dpa_curlevel; /* The current level that we're reading */
2291 dnode_t *dpa_dnode; /* The dnode associated with the prefetch */
2292 zio_priority_t dpa_prio; /* The priority I/Os should be issued at. */
2293 zio_t *dpa_zio; /* The parent zio_t for all prefetches. */
2294 arc_flags_t dpa_aflags; /* Flags to pass to the final prefetch. */
2295 } dbuf_prefetch_arg_t;
2298 * Actually issue the prefetch read for the block given.
2301 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t *dpa, blkptr_t *bp)
2303 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
2306 arc_flags_t aflags =
2307 dpa->dpa_aflags | ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH;
2309 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp));
2310 ASSERT3U(dpa->dpa_curlevel, ==, dpa->dpa_zb.zb_level);
2311 ASSERT(dpa->dpa_zio != NULL);
2312 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa, bp, NULL, NULL,
2313 dpa->dpa_prio, ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2314 &aflags, &dpa->dpa_zb);
2318 * Called when an indirect block above our prefetch target is read in. This
2319 * will either read in the next indirect block down the tree or issue the actual
2320 * prefetch if the next block down is our target.
2323 dbuf_prefetch_indirect_done(zio_t *zio, arc_buf_t *abuf, void *private)
2325 dbuf_prefetch_arg_t *dpa = private;
2327 ASSERT3S(dpa->dpa_zb.zb_level, <, dpa->dpa_curlevel);
2328 ASSERT3S(dpa->dpa_curlevel, >, 0);
2331 * The dpa_dnode is only valid if we are called with a NULL
2332 * zio. This indicates that the arc_read() returned without
2333 * first calling zio_read() to issue a physical read. Once
2334 * a physical read is made the dpa_dnode must be invalidated
2335 * as the locks guarding it may have been dropped. If the
2336 * dpa_dnode is still valid, then we want to add it to the dbuf
2337 * cache. To do so, we must hold the dbuf associated with the block
2338 * we just prefetched, read its contents so that we associate it
2339 * with an arc_buf_t, and then release it.
2342 ASSERT3S(BP_GET_LEVEL(zio->io_bp), ==, dpa->dpa_curlevel);
2343 if (zio->io_flags & ZIO_FLAG_RAW) {
2344 ASSERT3U(BP_GET_PSIZE(zio->io_bp), ==, zio->io_size);
2346 ASSERT3U(BP_GET_LSIZE(zio->io_bp), ==, zio->io_size);
2348 ASSERT3P(zio->io_spa, ==, dpa->dpa_spa);
2350 dpa->dpa_dnode = NULL;
2351 } else if (dpa->dpa_dnode != NULL) {
2352 uint64_t curblkid = dpa->dpa_zb.zb_blkid >>
2353 (dpa->dpa_epbs * (dpa->dpa_curlevel -
2354 dpa->dpa_zb.zb_level));
2355 dmu_buf_impl_t *db = dbuf_hold_level(dpa->dpa_dnode,
2356 dpa->dpa_curlevel, curblkid, FTAG);
2357 (void) dbuf_read(db, NULL,
2358 DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH | DB_RF_HAVESTRUCT);
2359 dbuf_rele(db, FTAG);
2362 dpa->dpa_curlevel--;
2364 uint64_t nextblkid = dpa->dpa_zb.zb_blkid >>
2365 (dpa->dpa_epbs * (dpa->dpa_curlevel - dpa->dpa_zb.zb_level));
2366 blkptr_t *bp = ((blkptr_t *)abuf->b_data) +
2367 P2PHASE(nextblkid, 1ULL << dpa->dpa_epbs);
2368 if (BP_IS_HOLE(bp) || (zio != NULL && zio->io_error != 0)) {
2369 kmem_free(dpa, sizeof (*dpa));
2370 } else if (dpa->dpa_curlevel == dpa->dpa_zb.zb_level) {
2371 ASSERT3U(nextblkid, ==, dpa->dpa_zb.zb_blkid);
2372 dbuf_issue_final_prefetch(dpa, bp);
2373 kmem_free(dpa, sizeof (*dpa));
2375 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT;
2376 zbookmark_phys_t zb;
2378 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2379 if (dpa->dpa_aflags & ARC_FLAG_L2CACHE)
2380 iter_aflags |= ARC_FLAG_L2CACHE;
2382 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp));
2384 SET_BOOKMARK(&zb, dpa->dpa_zb.zb_objset,
2385 dpa->dpa_zb.zb_object, dpa->dpa_curlevel, nextblkid);
2387 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa,
2388 bp, dbuf_prefetch_indirect_done, dpa, dpa->dpa_prio,
2389 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2393 arc_buf_destroy(abuf, private);
2397 * Issue prefetch reads for the given block on the given level. If the indirect
2398 * blocks above that block are not in memory, we will read them in
2399 * asynchronously. As a result, this call never blocks waiting for a read to
2403 dbuf_prefetch(dnode_t *dn, int64_t level, uint64_t blkid, zio_priority_t prio,
2407 int epbs, nlevels, curlevel;
2410 ASSERT(blkid != DMU_BONUS_BLKID);
2411 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2413 if (blkid > dn->dn_maxblkid)
2416 if (dnode_block_freed(dn, blkid))
2420 * This dnode hasn't been written to disk yet, so there's nothing to
2423 nlevels = dn->dn_phys->dn_nlevels;
2424 if (level >= nlevels || dn->dn_phys->dn_nblkptr == 0)
2427 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2428 if (dn->dn_phys->dn_maxblkid < blkid << (epbs * level))
2431 dmu_buf_impl_t *db = dbuf_find(dn->dn_objset, dn->dn_object,
2434 mutex_exit(&db->db_mtx);
2436 * This dbuf already exists. It is either CACHED, or
2437 * (we assume) about to be read or filled.
2443 * Find the closest ancestor (indirect block) of the target block
2444 * that is present in the cache. In this indirect block, we will
2445 * find the bp that is at curlevel, curblkid.
2449 while (curlevel < nlevels - 1) {
2450 int parent_level = curlevel + 1;
2451 uint64_t parent_blkid = curblkid >> epbs;
2454 if (dbuf_hold_impl(dn, parent_level, parent_blkid,
2455 FALSE, TRUE, FTAG, &db) == 0) {
2456 blkptr_t *bpp = db->db_buf->b_data;
2457 bp = bpp[P2PHASE(curblkid, 1 << epbs)];
2458 dbuf_rele(db, FTAG);
2462 curlevel = parent_level;
2463 curblkid = parent_blkid;
2466 if (curlevel == nlevels - 1) {
2467 /* No cached indirect blocks found. */
2468 ASSERT3U(curblkid, <, dn->dn_phys->dn_nblkptr);
2469 bp = dn->dn_phys->dn_blkptr[curblkid];
2471 if (BP_IS_HOLE(&bp))
2474 ASSERT3U(curlevel, ==, BP_GET_LEVEL(&bp));
2476 zio_t *pio = zio_root(dmu_objset_spa(dn->dn_objset), NULL, NULL,
2479 dbuf_prefetch_arg_t *dpa = kmem_zalloc(sizeof (*dpa), KM_SLEEP);
2480 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
2481 SET_BOOKMARK(&dpa->dpa_zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET,
2482 dn->dn_object, level, blkid);
2483 dpa->dpa_curlevel = curlevel;
2484 dpa->dpa_prio = prio;
2485 dpa->dpa_aflags = aflags;
2486 dpa->dpa_spa = dn->dn_objset->os_spa;
2487 dpa->dpa_dnode = dn;
2488 dpa->dpa_epbs = epbs;
2491 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2492 if (DNODE_LEVEL_IS_L2CACHEABLE(dn, level))
2493 dpa->dpa_aflags |= ARC_FLAG_L2CACHE;
2496 * If we have the indirect just above us, no need to do the asynchronous
2497 * prefetch chain; we'll just run the last step ourselves. If we're at
2498 * a higher level, though, we want to issue the prefetches for all the
2499 * indirect blocks asynchronously, so we can go on with whatever we were
2502 if (curlevel == level) {
2503 ASSERT3U(curblkid, ==, blkid);
2504 dbuf_issue_final_prefetch(dpa, &bp);
2505 kmem_free(dpa, sizeof (*dpa));
2507 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT;
2508 zbookmark_phys_t zb;
2510 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2511 if (DNODE_LEVEL_IS_L2CACHEABLE(dn, level))
2512 iter_aflags |= ARC_FLAG_L2CACHE;
2514 SET_BOOKMARK(&zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET,
2515 dn->dn_object, curlevel, curblkid);
2516 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa,
2517 &bp, dbuf_prefetch_indirect_done, dpa, prio,
2518 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2522 * We use pio here instead of dpa_zio since it's possible that
2523 * dpa may have already been freed.
2529 * Returns with db_holds incremented, and db_mtx not held.
2530 * Note: dn_struct_rwlock must be held.
2533 dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid,
2534 boolean_t fail_sparse, boolean_t fail_uncached,
2535 void *tag, dmu_buf_impl_t **dbp)
2537 dmu_buf_impl_t *db, *parent = NULL;
2539 ASSERT(blkid != DMU_BONUS_BLKID);
2540 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2541 ASSERT3U(dn->dn_nlevels, >, level);
2545 /* dbuf_find() returns with db_mtx held */
2546 db = dbuf_find(dn->dn_objset, dn->dn_object, level, blkid);
2549 blkptr_t *bp = NULL;
2553 return (SET_ERROR(ENOENT));
2555 ASSERT3P(parent, ==, NULL);
2556 err = dbuf_findbp(dn, level, blkid, fail_sparse, &parent, &bp);
2558 if (err == 0 && bp && BP_IS_HOLE(bp))
2559 err = SET_ERROR(ENOENT);
2562 dbuf_rele(parent, NULL);
2566 if (err && err != ENOENT)
2568 db = dbuf_create(dn, level, blkid, parent, bp);
2571 if (fail_uncached && db->db_state != DB_CACHED) {
2572 mutex_exit(&db->db_mtx);
2573 return (SET_ERROR(ENOENT));
2576 if (db->db_buf != NULL)
2577 ASSERT3P(db->db.db_data, ==, db->db_buf->b_data);
2579 ASSERT(db->db_buf == NULL || arc_referenced(db->db_buf));
2582 * If this buffer is currently syncing out, and we are are
2583 * still referencing it from db_data, we need to make a copy
2584 * of it in case we decide we want to dirty it again in this txg.
2586 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
2587 dn->dn_object != DMU_META_DNODE_OBJECT &&
2588 db->db_state == DB_CACHED && db->db_data_pending) {
2589 dbuf_dirty_record_t *dr = db->db_data_pending;
2591 if (dr->dt.dl.dr_data == db->db_buf) {
2592 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
2595 arc_alloc_buf(dn->dn_objset->os_spa, db, type,
2597 bcopy(dr->dt.dl.dr_data->b_data, db->db.db_data,
2602 if (multilist_link_active(&db->db_cache_link)) {
2603 ASSERT(refcount_is_zero(&db->db_holds));
2604 multilist_remove(dbuf_cache, db);
2605 (void) refcount_remove_many(&dbuf_cache_size,
2606 db->db.db_size, db);
2608 (void) refcount_add(&db->db_holds, tag);
2610 mutex_exit(&db->db_mtx);
2612 /* NOTE: we can't rele the parent until after we drop the db_mtx */
2614 dbuf_rele(parent, NULL);
2616 ASSERT3P(DB_DNODE(db), ==, dn);
2617 ASSERT3U(db->db_blkid, ==, blkid);
2618 ASSERT3U(db->db_level, ==, level);
2625 dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag)
2627 return (dbuf_hold_level(dn, 0, blkid, tag));
2631 dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag)
2634 int err = dbuf_hold_impl(dn, level, blkid, FALSE, FALSE, tag, &db);
2635 return (err ? NULL : db);
2639 dbuf_create_bonus(dnode_t *dn)
2641 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
2643 ASSERT(dn->dn_bonus == NULL);
2644 dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL);
2648 dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx)
2650 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2653 if (db->db_blkid != DMU_SPILL_BLKID)
2654 return (SET_ERROR(ENOTSUP));
2656 blksz = SPA_MINBLOCKSIZE;
2657 ASSERT3U(blksz, <=, spa_maxblocksize(dmu_objset_spa(db->db_objset)));
2658 blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE);
2662 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2663 dbuf_new_size(db, blksz, tx);
2664 rw_exit(&dn->dn_struct_rwlock);
2671 dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx)
2673 dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx);
2676 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2678 dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
2680 int64_t holds = refcount_add(&db->db_holds, tag);
2681 ASSERT3S(holds, >, 1);
2684 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
2686 dbuf_try_add_ref(dmu_buf_t *db_fake, objset_t *os, uint64_t obj, uint64_t blkid,
2689 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2690 dmu_buf_impl_t *found_db;
2691 boolean_t result = B_FALSE;
2693 if (db->db_blkid == DMU_BONUS_BLKID)
2694 found_db = dbuf_find_bonus(os, obj);
2696 found_db = dbuf_find(os, obj, 0, blkid);
2698 if (found_db != NULL) {
2699 if (db == found_db && dbuf_refcount(db) > db->db_dirtycnt) {
2700 (void) refcount_add(&db->db_holds, tag);
2703 mutex_exit(&db->db_mtx);
2709 * If you call dbuf_rele() you had better not be referencing the dnode handle
2710 * unless you have some other direct or indirect hold on the dnode. (An indirect
2711 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2712 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2713 * dnode's parent dbuf evicting its dnode handles.
2716 dbuf_rele(dmu_buf_impl_t *db, void *tag)
2718 mutex_enter(&db->db_mtx);
2719 dbuf_rele_and_unlock(db, tag);
2723 dmu_buf_rele(dmu_buf_t *db, void *tag)
2725 dbuf_rele((dmu_buf_impl_t *)db, tag);
2729 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2730 * db_dirtycnt and db_holds to be updated atomically.
2733 dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag)
2737 ASSERT(MUTEX_HELD(&db->db_mtx));
2741 * Remove the reference to the dbuf before removing its hold on the
2742 * dnode so we can guarantee in dnode_move() that a referenced bonus
2743 * buffer has a corresponding dnode hold.
2745 holds = refcount_remove(&db->db_holds, tag);
2749 * We can't freeze indirects if there is a possibility that they
2750 * may be modified in the current syncing context.
2752 if (db->db_buf != NULL &&
2753 holds == (db->db_level == 0 ? db->db_dirtycnt : 0)) {
2754 arc_buf_freeze(db->db_buf);
2757 if (holds == db->db_dirtycnt &&
2758 db->db_level == 0 && db->db_user_immediate_evict)
2759 dbuf_evict_user(db);
2762 if (db->db_blkid == DMU_BONUS_BLKID) {
2764 boolean_t evict_dbuf = db->db_pending_evict;
2767 * If the dnode moves here, we cannot cross this
2768 * barrier until the move completes.
2773 atomic_dec_32(&dn->dn_dbufs_count);
2776 * Decrementing the dbuf count means that the bonus
2777 * buffer's dnode hold is no longer discounted in
2778 * dnode_move(). The dnode cannot move until after
2779 * the dnode_rele() below.
2784 * Do not reference db after its lock is dropped.
2785 * Another thread may evict it.
2787 mutex_exit(&db->db_mtx);
2790 dnode_evict_bonus(dn);
2793 } else if (db->db_buf == NULL) {
2795 * This is a special case: we never associated this
2796 * dbuf with any data allocated from the ARC.
2798 ASSERT(db->db_state == DB_UNCACHED ||
2799 db->db_state == DB_NOFILL);
2801 } else if (arc_released(db->db_buf)) {
2803 * This dbuf has anonymous data associated with it.
2807 boolean_t do_arc_evict = B_FALSE;
2809 spa_t *spa = dmu_objset_spa(db->db_objset);
2811 if (!DBUF_IS_CACHEABLE(db) &&
2812 db->db_blkptr != NULL &&
2813 !BP_IS_HOLE(db->db_blkptr) &&
2814 !BP_IS_EMBEDDED(db->db_blkptr)) {
2815 do_arc_evict = B_TRUE;
2816 bp = *db->db_blkptr;
2819 if (!DBUF_IS_CACHEABLE(db) ||
2820 db->db_pending_evict) {
2822 } else if (!multilist_link_active(&db->db_cache_link)) {
2823 multilist_insert(dbuf_cache, db);
2824 (void) refcount_add_many(&dbuf_cache_size,
2825 db->db.db_size, db);
2826 mutex_exit(&db->db_mtx);
2828 dbuf_evict_notify();
2832 arc_freed(spa, &bp);
2835 mutex_exit(&db->db_mtx);
2840 #pragma weak dmu_buf_refcount = dbuf_refcount
2842 dbuf_refcount(dmu_buf_impl_t *db)
2844 return (refcount_count(&db->db_holds));
2848 dmu_buf_replace_user(dmu_buf_t *db_fake, dmu_buf_user_t *old_user,
2849 dmu_buf_user_t *new_user)
2851 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2853 mutex_enter(&db->db_mtx);
2854 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2855 if (db->db_user == old_user)
2856 db->db_user = new_user;
2858 old_user = db->db_user;
2859 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2860 mutex_exit(&db->db_mtx);
2866 dmu_buf_set_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2868 return (dmu_buf_replace_user(db_fake, NULL, user));
2872 dmu_buf_set_user_ie(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2874 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2876 db->db_user_immediate_evict = TRUE;
2877 return (dmu_buf_set_user(db_fake, user));
2881 dmu_buf_remove_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2883 return (dmu_buf_replace_user(db_fake, user, NULL));
2887 dmu_buf_get_user(dmu_buf_t *db_fake)
2889 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2891 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2892 return (db->db_user);
2896 dmu_buf_user_evict_wait()
2898 taskq_wait(dbu_evict_taskq);
2902 dmu_buf_get_blkptr(dmu_buf_t *db)
2904 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2905 return (dbi->db_blkptr);
2909 dmu_buf_get_objset(dmu_buf_t *db)
2911 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2912 return (dbi->db_objset);
2916 dmu_buf_dnode_enter(dmu_buf_t *db)
2918 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2919 DB_DNODE_ENTER(dbi);
2920 return (DB_DNODE(dbi));
2924 dmu_buf_dnode_exit(dmu_buf_t *db)
2926 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2931 dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db)
2933 /* ASSERT(dmu_tx_is_syncing(tx) */
2934 ASSERT(MUTEX_HELD(&db->db_mtx));
2936 if (db->db_blkptr != NULL)
2939 if (db->db_blkid == DMU_SPILL_BLKID) {
2940 db->db_blkptr = &dn->dn_phys->dn_spill;
2941 BP_ZERO(db->db_blkptr);
2944 if (db->db_level == dn->dn_phys->dn_nlevels-1) {
2946 * This buffer was allocated at a time when there was
2947 * no available blkptrs from the dnode, or it was
2948 * inappropriate to hook it in (i.e., nlevels mis-match).
2950 ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr);
2951 ASSERT(db->db_parent == NULL);
2952 db->db_parent = dn->dn_dbuf;
2953 db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
2956 dmu_buf_impl_t *parent = db->db_parent;
2957 int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2959 ASSERT(dn->dn_phys->dn_nlevels > 1);
2960 if (parent == NULL) {
2961 mutex_exit(&db->db_mtx);
2962 rw_enter(&dn->dn_struct_rwlock, RW_READER);
2963 parent = dbuf_hold_level(dn, db->db_level + 1,
2964 db->db_blkid >> epbs, db);
2965 rw_exit(&dn->dn_struct_rwlock);
2966 mutex_enter(&db->db_mtx);
2967 db->db_parent = parent;
2969 db->db_blkptr = (blkptr_t *)parent->db.db_data +
2970 (db->db_blkid & ((1ULL << epbs) - 1));
2976 dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2978 dmu_buf_impl_t *db = dr->dr_dbuf;
2982 ASSERT(dmu_tx_is_syncing(tx));
2984 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2986 mutex_enter(&db->db_mtx);
2988 ASSERT(db->db_level > 0);
2991 /* Read the block if it hasn't been read yet. */
2992 if (db->db_buf == NULL) {
2993 mutex_exit(&db->db_mtx);
2994 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
2995 mutex_enter(&db->db_mtx);
2997 ASSERT3U(db->db_state, ==, DB_CACHED);
2998 ASSERT(db->db_buf != NULL);
3002 /* Indirect block size must match what the dnode thinks it is. */
3003 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
3004 dbuf_check_blkptr(dn, db);
3007 /* Provide the pending dirty record to child dbufs */
3008 db->db_data_pending = dr;
3010 mutex_exit(&db->db_mtx);
3012 dbuf_write(dr, db->db_buf, tx);
3015 mutex_enter(&dr->dt.di.dr_mtx);
3016 dbuf_sync_list(&dr->dt.di.dr_children, db->db_level - 1, tx);
3017 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
3018 mutex_exit(&dr->dt.di.dr_mtx);
3023 dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
3025 arc_buf_t **datap = &dr->dt.dl.dr_data;
3026 dmu_buf_impl_t *db = dr->dr_dbuf;
3029 uint64_t txg = tx->tx_txg;
3031 ASSERT(dmu_tx_is_syncing(tx));
3033 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
3035 mutex_enter(&db->db_mtx);
3037 * To be synced, we must be dirtied. But we
3038 * might have been freed after the dirty.
3040 if (db->db_state == DB_UNCACHED) {
3041 /* This buffer has been freed since it was dirtied */
3042 ASSERT(db->db.db_data == NULL);
3043 } else if (db->db_state == DB_FILL) {
3044 /* This buffer was freed and is now being re-filled */
3045 ASSERT(db->db.db_data != dr->dt.dl.dr_data);
3047 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL);
3054 if (db->db_blkid == DMU_SPILL_BLKID) {
3055 mutex_enter(&dn->dn_mtx);
3056 dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR;
3057 mutex_exit(&dn->dn_mtx);
3061 * If this is a bonus buffer, simply copy the bonus data into the
3062 * dnode. It will be written out when the dnode is synced (and it
3063 * will be synced, since it must have been dirty for dbuf_sync to
3066 if (db->db_blkid == DMU_BONUS_BLKID) {
3067 dbuf_dirty_record_t **drp;
3069 ASSERT(*datap != NULL);
3070 ASSERT0(db->db_level);
3071 ASSERT3U(dn->dn_phys->dn_bonuslen, <=, DN_MAX_BONUSLEN);
3072 bcopy(*datap, DN_BONUS(dn->dn_phys), dn->dn_phys->dn_bonuslen);
3075 if (*datap != db->db.db_data) {
3076 zio_buf_free(*datap, DN_MAX_BONUSLEN);
3077 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
3079 db->db_data_pending = NULL;
3080 drp = &db->db_last_dirty;
3082 drp = &(*drp)->dr_next;
3083 ASSERT(dr->dr_next == NULL);
3084 ASSERT(dr->dr_dbuf == db);
3086 if (dr->dr_dbuf->db_level != 0) {
3087 list_destroy(&dr->dt.di.dr_children);
3088 mutex_destroy(&dr->dt.di.dr_mtx);
3090 kmem_free(dr, sizeof (dbuf_dirty_record_t));
3091 ASSERT(db->db_dirtycnt > 0);
3092 db->db_dirtycnt -= 1;
3093 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
3100 * This function may have dropped the db_mtx lock allowing a dmu_sync
3101 * operation to sneak in. As a result, we need to ensure that we
3102 * don't check the dr_override_state until we have returned from
3103 * dbuf_check_blkptr.
3105 dbuf_check_blkptr(dn, db);
3108 * If this buffer is in the middle of an immediate write,
3109 * wait for the synchronous IO to complete.
3111 while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
3112 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
3113 cv_wait(&db->db_changed, &db->db_mtx);
3114 ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN);
3117 if (db->db_state != DB_NOFILL &&
3118 dn->dn_object != DMU_META_DNODE_OBJECT &&
3119 refcount_count(&db->db_holds) > 1 &&
3120 dr->dt.dl.dr_override_state != DR_OVERRIDDEN &&
3121 *datap == db->db_buf) {
3123 * If this buffer is currently "in use" (i.e., there
3124 * are active holds and db_data still references it),
3125 * then make a copy before we start the write so that
3126 * any modifications from the open txg will not leak
3129 * NOTE: this copy does not need to be made for
3130 * objects only modified in the syncing context (e.g.
3131 * DNONE_DNODE blocks).
3133 int psize = arc_buf_size(*datap);
3134 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
3135 enum zio_compress compress_type = arc_get_compression(*datap);
3137 if (compress_type == ZIO_COMPRESS_OFF) {
3138 *datap = arc_alloc_buf(os->os_spa, db, type, psize);
3140 ASSERT3U(type, ==, ARC_BUFC_DATA);
3141 int lsize = arc_buf_lsize(*datap);
3142 *datap = arc_alloc_compressed_buf(os->os_spa, db,
3143 psize, lsize, compress_type);
3145 bcopy(db->db.db_data, (*datap)->b_data, psize);
3147 db->db_data_pending = dr;
3149 mutex_exit(&db->db_mtx);
3151 dbuf_write(dr, *datap, tx);
3153 ASSERT(!list_link_active(&dr->dr_dirty_node));
3154 if (dn->dn_object == DMU_META_DNODE_OBJECT) {
3155 list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr);
3159 * Although zio_nowait() does not "wait for an IO", it does
3160 * initiate the IO. If this is an empty write it seems plausible
3161 * that the IO could actually be completed before the nowait
3162 * returns. We need to DB_DNODE_EXIT() first in case
3163 * zio_nowait() invalidates the dbuf.
3166 zio_nowait(dr->dr_zio);
3171 dbuf_sync_list(list_t *list, int level, dmu_tx_t *tx)
3173 dbuf_dirty_record_t *dr;
3175 while (dr = list_head(list)) {
3176 if (dr->dr_zio != NULL) {
3178 * If we find an already initialized zio then we
3179 * are processing the meta-dnode, and we have finished.
3180 * The dbufs for all dnodes are put back on the list
3181 * during processing, so that we can zio_wait()
3182 * these IOs after initiating all child IOs.
3184 ASSERT3U(dr->dr_dbuf->db.db_object, ==,
3185 DMU_META_DNODE_OBJECT);
3188 if (dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
3189 dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
3190 VERIFY3U(dr->dr_dbuf->db_level, ==, level);
3192 list_remove(list, dr);
3193 if (dr->dr_dbuf->db_level > 0)
3194 dbuf_sync_indirect(dr, tx);
3196 dbuf_sync_leaf(dr, tx);
3202 dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
3204 dmu_buf_impl_t *db = vdb;
3206 blkptr_t *bp = zio->io_bp;
3207 blkptr_t *bp_orig = &zio->io_bp_orig;
3208 spa_t *spa = zio->io_spa;
3213 ASSERT3P(db->db_blkptr, !=, NULL);
3214 ASSERT3P(&db->db_data_pending->dr_bp_copy, ==, bp);
3218 delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig);
3219 dnode_diduse_space(dn, delta - zio->io_prev_space_delta);
3220 zio->io_prev_space_delta = delta;
3222 if (bp->blk_birth != 0) {
3223 ASSERT((db->db_blkid != DMU_SPILL_BLKID &&
3224 BP_GET_TYPE(bp) == dn->dn_type) ||
3225 (db->db_blkid == DMU_SPILL_BLKID &&
3226 BP_GET_TYPE(bp) == dn->dn_bonustype) ||
3227 BP_IS_EMBEDDED(bp));
3228 ASSERT(BP_GET_LEVEL(bp) == db->db_level);
3231 mutex_enter(&db->db_mtx);
3234 if (db->db_blkid == DMU_SPILL_BLKID) {
3235 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
3236 ASSERT(!(BP_IS_HOLE(bp)) &&
3237 db->db_blkptr == &dn->dn_phys->dn_spill);
3241 if (db->db_level == 0) {
3242 mutex_enter(&dn->dn_mtx);
3243 if (db->db_blkid > dn->dn_phys->dn_maxblkid &&
3244 db->db_blkid != DMU_SPILL_BLKID)
3245 dn->dn_phys->dn_maxblkid = db->db_blkid;
3246 mutex_exit(&dn->dn_mtx);
3248 if (dn->dn_type == DMU_OT_DNODE) {
3249 dnode_phys_t *dnp = db->db.db_data;
3250 for (i = db->db.db_size >> DNODE_SHIFT; i > 0;
3252 if (dnp->dn_type != DMU_OT_NONE)
3256 if (BP_IS_HOLE(bp)) {
3263 blkptr_t *ibp = db->db.db_data;
3264 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
3265 for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) {
3266 if (BP_IS_HOLE(ibp))
3268 fill += BP_GET_FILL(ibp);
3273 if (!BP_IS_EMBEDDED(bp))
3274 bp->blk_fill = fill;
3276 mutex_exit(&db->db_mtx);
3278 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
3279 *db->db_blkptr = *bp;
3280 rw_exit(&dn->dn_struct_rwlock);
3285 * This function gets called just prior to running through the compression
3286 * stage of the zio pipeline. If we're an indirect block comprised of only
3287 * holes, then we want this indirect to be compressed away to a hole. In
3288 * order to do that we must zero out any information about the holes that
3289 * this indirect points to prior to before we try to compress it.
3292 dbuf_write_children_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
3294 dmu_buf_impl_t *db = vdb;
3297 unsigned int epbs, i;
3299 ASSERT3U(db->db_level, >, 0);
3302 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
3303 ASSERT3U(epbs, <, 31);
3305 /* Determine if all our children are holes */
3306 for (i = 0, bp = db->db.db_data; i < 1 << epbs; i++, bp++) {
3307 if (!BP_IS_HOLE(bp))
3312 * If all the children are holes, then zero them all out so that
3313 * we may get compressed away.
3315 if (i == 1 << epbs) {
3317 * We only found holes. Grab the rwlock to prevent
3318 * anybody from reading the blocks we're about to
3321 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
3322 bzero(db->db.db_data, db->db.db_size);
3323 rw_exit(&dn->dn_struct_rwlock);
3329 * The SPA will call this callback several times for each zio - once
3330 * for every physical child i/o (zio->io_phys_children times). This
3331 * allows the DMU to monitor the progress of each logical i/o. For example,
3332 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
3333 * block. There may be a long delay before all copies/fragments are completed,
3334 * so this callback allows us to retire dirty space gradually, as the physical
3339 dbuf_write_physdone(zio_t *zio, arc_buf_t *buf, void *arg)
3341 dmu_buf_impl_t *db = arg;
3342 objset_t *os = db->db_objset;
3343 dsl_pool_t *dp = dmu_objset_pool(os);
3344 dbuf_dirty_record_t *dr;
3347 dr = db->db_data_pending;
3348 ASSERT3U(dr->dr_txg, ==, zio->io_txg);
3351 * The callback will be called io_phys_children times. Retire one
3352 * portion of our dirty space each time we are called. Any rounding
3353 * error will be cleaned up by dsl_pool_sync()'s call to
3354 * dsl_pool_undirty_space().
3356 delta = dr->dr_accounted / zio->io_phys_children;
3357 dsl_pool_undirty_space(dp, delta, zio->io_txg);
3362 dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
3364 dmu_buf_impl_t *db = vdb;
3365 blkptr_t *bp_orig = &zio->io_bp_orig;
3366 blkptr_t *bp = db->db_blkptr;
3367 objset_t *os = db->db_objset;
3368 dmu_tx_t *tx = os->os_synctx;
3369 dbuf_dirty_record_t **drp, *dr;
3371 ASSERT0(zio->io_error);
3372 ASSERT(db->db_blkptr == bp);
3375 * For nopwrites and rewrites we ensure that the bp matches our
3376 * original and bypass all the accounting.
3378 if (zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)) {
3379 ASSERT(BP_EQUAL(bp, bp_orig));
3381 dsl_dataset_t *ds = os->os_dsl_dataset;
3382 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
3383 dsl_dataset_block_born(ds, bp, tx);
3386 mutex_enter(&db->db_mtx);
3390 drp = &db->db_last_dirty;
3391 while ((dr = *drp) != db->db_data_pending)
3393 ASSERT(!list_link_active(&dr->dr_dirty_node));
3394 ASSERT(dr->dr_dbuf == db);
3395 ASSERT(dr->dr_next == NULL);
3399 if (db->db_blkid == DMU_SPILL_BLKID) {
3404 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
3405 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
3406 db->db_blkptr == &dn->dn_phys->dn_spill);
3411 if (db->db_level == 0) {
3412 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
3413 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
3414 if (db->db_state != DB_NOFILL) {
3415 if (dr->dt.dl.dr_data != db->db_buf)
3416 arc_buf_destroy(dr->dt.dl.dr_data, db);
3423 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
3424 ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
3425 if (!BP_IS_HOLE(db->db_blkptr)) {
3427 dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
3428 ASSERT3U(db->db_blkid, <=,
3429 dn->dn_phys->dn_maxblkid >> (db->db_level * epbs));
3430 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
3434 mutex_destroy(&dr->dt.di.dr_mtx);
3435 list_destroy(&dr->dt.di.dr_children);
3437 kmem_free(dr, sizeof (dbuf_dirty_record_t));
3439 cv_broadcast(&db->db_changed);
3440 ASSERT(db->db_dirtycnt > 0);
3441 db->db_dirtycnt -= 1;
3442 db->db_data_pending = NULL;
3443 dbuf_rele_and_unlock(db, (void *)(uintptr_t)tx->tx_txg);
3447 dbuf_write_nofill_ready(zio_t *zio)
3449 dbuf_write_ready(zio, NULL, zio->io_private);
3453 dbuf_write_nofill_done(zio_t *zio)
3455 dbuf_write_done(zio, NULL, zio->io_private);
3459 dbuf_write_override_ready(zio_t *zio)
3461 dbuf_dirty_record_t *dr = zio->io_private;
3462 dmu_buf_impl_t *db = dr->dr_dbuf;
3464 dbuf_write_ready(zio, NULL, db);
3468 dbuf_write_override_done(zio_t *zio)
3470 dbuf_dirty_record_t *dr = zio->io_private;
3471 dmu_buf_impl_t *db = dr->dr_dbuf;
3472 blkptr_t *obp = &dr->dt.dl.dr_overridden_by;
3474 mutex_enter(&db->db_mtx);
3475 if (!BP_EQUAL(zio->io_bp, obp)) {
3476 if (!BP_IS_HOLE(obp))
3477 dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp);
3478 arc_release(dr->dt.dl.dr_data, db);
3480 mutex_exit(&db->db_mtx);
3481 dbuf_write_done(zio, NULL, db);
3483 if (zio->io_abd != NULL)
3484 abd_put(zio->io_abd);
3487 typedef struct dbuf_remap_impl_callback_arg {
3489 uint64_t drica_blk_birth;
3491 } dbuf_remap_impl_callback_arg_t;
3494 dbuf_remap_impl_callback(uint64_t vdev, uint64_t offset, uint64_t size,
3497 dbuf_remap_impl_callback_arg_t *drica = arg;
3498 objset_t *os = drica->drica_os;
3499 spa_t *spa = dmu_objset_spa(os);
3500 dmu_tx_t *tx = drica->drica_tx;
3502 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa)));
3504 if (os == spa_meta_objset(spa)) {
3505 spa_vdev_indirect_mark_obsolete(spa, vdev, offset, size, tx);
3507 dsl_dataset_block_remapped(dmu_objset_ds(os), vdev, offset,
3508 size, drica->drica_blk_birth, tx);
3513 dbuf_remap_impl(dnode_t *dn, blkptr_t *bp, dmu_tx_t *tx)
3515 blkptr_t bp_copy = *bp;
3516 spa_t *spa = dmu_objset_spa(dn->dn_objset);
3517 dbuf_remap_impl_callback_arg_t drica;
3519 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa)));
3521 drica.drica_os = dn->dn_objset;
3522 drica.drica_blk_birth = bp->blk_birth;
3523 drica.drica_tx = tx;
3524 if (spa_remap_blkptr(spa, &bp_copy, dbuf_remap_impl_callback,
3527 * The struct_rwlock prevents dbuf_read_impl() from
3528 * dereferencing the BP while we are changing it. To
3529 * avoid lock contention, only grab it when we are actually
3532 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
3534 rw_exit(&dn->dn_struct_rwlock);
3539 * Returns true if a dbuf_remap would modify the dbuf. We do this by attempting
3540 * to remap a copy of every bp in the dbuf.
3543 dbuf_can_remap(const dmu_buf_impl_t *db)
3545 spa_t *spa = dmu_objset_spa(db->db_objset);
3546 blkptr_t *bp = db->db.db_data;
3547 boolean_t ret = B_FALSE;
3549 ASSERT3U(db->db_level, >, 0);
3550 ASSERT3S(db->db_state, ==, DB_CACHED);
3552 ASSERT(spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL));
3554 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3555 for (int i = 0; i < db->db.db_size >> SPA_BLKPTRSHIFT; i++) {
3556 blkptr_t bp_copy = bp[i];
3557 if (spa_remap_blkptr(spa, &bp_copy, NULL, NULL)) {
3562 spa_config_exit(spa, SCL_VDEV, FTAG);
3568 dnode_needs_remap(const dnode_t *dn)
3570 spa_t *spa = dmu_objset_spa(dn->dn_objset);
3571 boolean_t ret = B_FALSE;
3573 if (dn->dn_phys->dn_nlevels == 0) {
3577 ASSERT(spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL));
3579 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3580 for (int j = 0; j < dn->dn_phys->dn_nblkptr; j++) {
3581 blkptr_t bp_copy = dn->dn_phys->dn_blkptr[j];
3582 if (spa_remap_blkptr(spa, &bp_copy, NULL, NULL)) {
3587 spa_config_exit(spa, SCL_VDEV, FTAG);
3593 * Remap any existing BP's to concrete vdevs, if possible.
3596 dbuf_remap(dnode_t *dn, dmu_buf_impl_t *db, dmu_tx_t *tx)
3598 spa_t *spa = dmu_objset_spa(db->db_objset);
3599 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa)));
3601 if (!spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL))
3604 if (db->db_level > 0) {
3605 blkptr_t *bp = db->db.db_data;
3606 for (int i = 0; i < db->db.db_size >> SPA_BLKPTRSHIFT; i++) {
3607 dbuf_remap_impl(dn, &bp[i], tx);
3609 } else if (db->db.db_object == DMU_META_DNODE_OBJECT) {
3610 dnode_phys_t *dnp = db->db.db_data;
3611 ASSERT3U(db->db_dnode_handle->dnh_dnode->dn_type, ==,
3613 for (int i = 0; i < db->db.db_size >> DNODE_SHIFT; i++) {
3614 for (int j = 0; j < dnp[i].dn_nblkptr; j++) {
3615 dbuf_remap_impl(dn, &dnp[i].dn_blkptr[j], tx);
3622 /* Issue I/O to commit a dirty buffer to disk. */
3624 dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx)
3626 dmu_buf_impl_t *db = dr->dr_dbuf;
3629 dmu_buf_impl_t *parent = db->db_parent;
3630 uint64_t txg = tx->tx_txg;
3631 zbookmark_phys_t zb;
3636 ASSERT(dmu_tx_is_syncing(tx));
3642 if (db->db_state != DB_NOFILL) {
3643 if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) {
3645 * Private object buffers are released here rather
3646 * than in dbuf_dirty() since they are only modified
3647 * in the syncing context and we don't want the
3648 * overhead of making multiple copies of the data.
3650 if (BP_IS_HOLE(db->db_blkptr)) {
3653 dbuf_release_bp(db);
3655 dbuf_remap(dn, db, tx);
3659 if (parent != dn->dn_dbuf) {
3660 /* Our parent is an indirect block. */
3661 /* We have a dirty parent that has been scheduled for write. */
3662 ASSERT(parent && parent->db_data_pending);
3663 /* Our parent's buffer is one level closer to the dnode. */
3664 ASSERT(db->db_level == parent->db_level-1);
3666 * We're about to modify our parent's db_data by modifying
3667 * our block pointer, so the parent must be released.
3669 ASSERT(arc_released(parent->db_buf));
3670 zio = parent->db_data_pending->dr_zio;
3672 /* Our parent is the dnode itself. */
3673 ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 &&
3674 db->db_blkid != DMU_SPILL_BLKID) ||
3675 (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0));
3676 if (db->db_blkid != DMU_SPILL_BLKID)
3677 ASSERT3P(db->db_blkptr, ==,
3678 &dn->dn_phys->dn_blkptr[db->db_blkid]);
3682 ASSERT(db->db_level == 0 || data == db->db_buf);
3683 ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
3686 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
3687 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
3688 db->db.db_object, db->db_level, db->db_blkid);
3690 if (db->db_blkid == DMU_SPILL_BLKID)
3692 wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
3694 dmu_write_policy(os, dn, db->db_level, wp_flag, &zp);
3698 * We copy the blkptr now (rather than when we instantiate the dirty
3699 * record), because its value can change between open context and
3700 * syncing context. We do not need to hold dn_struct_rwlock to read
3701 * db_blkptr because we are in syncing context.
3703 dr->dr_bp_copy = *db->db_blkptr;
3705 if (db->db_level == 0 &&
3706 dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
3708 * The BP for this block has been provided by open context
3709 * (by dmu_sync() or dmu_buf_write_embedded()).
3711 abd_t *contents = (data != NULL) ?
3712 abd_get_from_buf(data->b_data, arc_buf_size(data)) : NULL;
3714 dr->dr_zio = zio_write(zio, os->os_spa, txg, &dr->dr_bp_copy,
3715 contents, db->db.db_size, db->db.db_size, &zp,
3716 dbuf_write_override_ready, NULL, NULL,
3717 dbuf_write_override_done,
3718 dr, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
3719 mutex_enter(&db->db_mtx);
3720 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
3721 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by,
3722 dr->dt.dl.dr_copies, dr->dt.dl.dr_nopwrite);
3723 mutex_exit(&db->db_mtx);
3724 } else if (db->db_state == DB_NOFILL) {
3725 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF ||
3726 zp.zp_checksum == ZIO_CHECKSUM_NOPARITY);
3727 dr->dr_zio = zio_write(zio, os->os_spa, txg,
3728 &dr->dr_bp_copy, NULL, db->db.db_size, db->db.db_size, &zp,
3729 dbuf_write_nofill_ready, NULL, NULL,
3730 dbuf_write_nofill_done, db,
3731 ZIO_PRIORITY_ASYNC_WRITE,
3732 ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb);
3734 ASSERT(arc_released(data));
3737 * For indirect blocks, we want to setup the children
3738 * ready callback so that we can properly handle an indirect
3739 * block that only contains holes.
3741 arc_done_func_t *children_ready_cb = NULL;
3742 if (db->db_level != 0)
3743 children_ready_cb = dbuf_write_children_ready;
3745 dr->dr_zio = arc_write(zio, os->os_spa, txg,
3746 &dr->dr_bp_copy, data, DBUF_IS_L2CACHEABLE(db),
3747 &zp, dbuf_write_ready, children_ready_cb,
3748 dbuf_write_physdone, dbuf_write_done, db,
3749 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);