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) 2014 Spectra Logic Corporation, All rights reserved.
29 #include <sys/zfs_context.h>
32 #include <sys/dmu_send.h>
33 #include <sys/dmu_impl.h>
35 #include <sys/dmu_objset.h>
36 #include <sys/dsl_dataset.h>
37 #include <sys/dsl_dir.h>
38 #include <sys/dmu_tx.h>
41 #include <sys/dmu_zfetch.h>
43 #include <sys/sa_impl.h>
44 #include <sys/zfeature.h>
45 #include <sys/blkptr.h>
46 #include <sys/range_tree.h>
47 #include <sys/trace_dbuf.h>
48 #include <sys/callb.h>
51 struct dbuf_hold_impl_data {
52 /* Function arguments */
56 boolean_t dh_fail_sparse;
57 boolean_t dh_fail_uncached;
59 dmu_buf_impl_t **dh_dbp;
61 dmu_buf_impl_t *dh_db;
62 dmu_buf_impl_t *dh_parent;
65 dbuf_dirty_record_t *dh_dr;
66 arc_buf_contents_t dh_type;
70 static void __dbuf_hold_impl_init(struct dbuf_hold_impl_data *dh,
71 dnode_t *dn, uint8_t level, uint64_t blkid, boolean_t fail_sparse,
72 boolean_t fail_uncached,
73 void *tag, dmu_buf_impl_t **dbp, int depth);
74 static int __dbuf_hold_impl(struct dbuf_hold_impl_data *dh);
76 uint_t zfs_dbuf_evict_key;
78 static boolean_t dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
79 static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx);
81 extern inline void dmu_buf_init_user(dmu_buf_user_t *dbu,
82 dmu_buf_evict_func_t *evict_func_sync,
83 dmu_buf_evict_func_t *evict_func_async,
84 dmu_buf_t **clear_on_evict_dbufp);
87 * Global data structures and functions for the dbuf cache.
89 static kmem_cache_t *dbuf_kmem_cache;
90 static taskq_t *dbu_evict_taskq;
92 static kthread_t *dbuf_cache_evict_thread;
93 static kmutex_t dbuf_evict_lock;
94 static kcondvar_t dbuf_evict_cv;
95 static boolean_t dbuf_evict_thread_exit;
98 * LRU cache of dbufs. The dbuf cache maintains a list of dbufs that
99 * are not currently held but have been recently released. These dbufs
100 * are not eligible for arc eviction until they are aged out of the cache.
101 * Dbufs are added to the dbuf cache once the last hold is released. If a
102 * dbuf is later accessed and still exists in the dbuf cache, then it will
103 * be removed from the cache and later re-added to the head of the cache.
104 * Dbufs that are aged out of the cache will be immediately destroyed and
105 * become eligible for arc eviction.
107 static multilist_t *dbuf_cache;
108 static refcount_t dbuf_cache_size;
109 unsigned long dbuf_cache_max_bytes = 100 * 1024 * 1024;
111 /* Cap the size of the dbuf cache to log2 fraction of arc size. */
112 int dbuf_cache_max_shift = 5;
115 * The dbuf cache uses a three-stage eviction policy:
116 * - A low water marker designates when the dbuf eviction thread
117 * should stop evicting from the dbuf cache.
118 * - When we reach the maximum size (aka mid water mark), we
119 * signal the eviction thread to run.
120 * - The high water mark indicates when the eviction thread
121 * is unable to keep up with the incoming load and eviction must
122 * happen in the context of the calling thread.
126 * low water mid water hi water
127 * +----------------------------------------+----------+----------+
132 * +----------------------------------------+----------+----------+
134 * evicting eviction directly
137 * The high and low water marks indicate the operating range for the eviction
138 * thread. The low water mark is, by default, 90% of the total size of the
139 * cache and the high water mark is at 110% (both of these percentages can be
140 * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct,
141 * respectively). The eviction thread will try to ensure that the cache remains
142 * within this range by waking up every second and checking if the cache is
143 * above the low water mark. The thread can also be woken up by callers adding
144 * elements into the cache if the cache is larger than the mid water (i.e max
145 * cache size). Once the eviction thread is woken up and eviction is required,
146 * it will continue evicting buffers until it's able to reduce the cache size
147 * to the low water mark. If the cache size continues to grow and hits the high
148 * water mark, then callers adding elements to the cache will begin to evict
149 * directly from the cache until the cache is no longer above the high water
154 * The percentage above and below the maximum cache size.
156 uint_t dbuf_cache_hiwater_pct = 10;
157 uint_t dbuf_cache_lowater_pct = 10;
161 dbuf_cons(void *vdb, void *unused, int kmflag)
163 dmu_buf_impl_t *db = vdb;
164 bzero(db, sizeof (dmu_buf_impl_t));
166 mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL);
167 cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL);
168 multilist_link_init(&db->db_cache_link);
169 refcount_create(&db->db_holds);
170 multilist_link_init(&db->db_cache_link);
177 dbuf_dest(void *vdb, void *unused)
179 dmu_buf_impl_t *db = vdb;
180 mutex_destroy(&db->db_mtx);
181 cv_destroy(&db->db_changed);
182 ASSERT(!multilist_link_active(&db->db_cache_link));
183 refcount_destroy(&db->db_holds);
187 * dbuf hash table routines
189 static dbuf_hash_table_t dbuf_hash_table;
191 static uint64_t dbuf_hash_count;
194 dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid)
196 uintptr_t osv = (uintptr_t)os;
197 uint64_t crc = -1ULL;
199 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
200 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (lvl)) & 0xFF];
201 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF];
202 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF];
203 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF];
204 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 0)) & 0xFF];
205 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 8)) & 0xFF];
207 crc ^= (osv>>14) ^ (obj>>16) ^ (blkid>>16);
212 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
213 ((dbuf)->db.db_object == (obj) && \
214 (dbuf)->db_objset == (os) && \
215 (dbuf)->db_level == (level) && \
216 (dbuf)->db_blkid == (blkid))
219 dbuf_find(objset_t *os, uint64_t obj, uint8_t level, uint64_t blkid)
221 dbuf_hash_table_t *h = &dbuf_hash_table;
226 hv = dbuf_hash(os, obj, level, blkid);
227 idx = hv & h->hash_table_mask;
229 mutex_enter(DBUF_HASH_MUTEX(h, idx));
230 for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) {
231 if (DBUF_EQUAL(db, os, obj, level, blkid)) {
232 mutex_enter(&db->db_mtx);
233 if (db->db_state != DB_EVICTING) {
234 mutex_exit(DBUF_HASH_MUTEX(h, idx));
237 mutex_exit(&db->db_mtx);
240 mutex_exit(DBUF_HASH_MUTEX(h, idx));
244 static dmu_buf_impl_t *
245 dbuf_find_bonus(objset_t *os, uint64_t object)
248 dmu_buf_impl_t *db = NULL;
250 if (dnode_hold(os, object, FTAG, &dn) == 0) {
251 rw_enter(&dn->dn_struct_rwlock, RW_READER);
252 if (dn->dn_bonus != NULL) {
254 mutex_enter(&db->db_mtx);
256 rw_exit(&dn->dn_struct_rwlock);
257 dnode_rele(dn, FTAG);
263 * Insert an entry into the hash table. If there is already an element
264 * equal to elem in the hash table, then the already existing element
265 * will be returned and the new element will not be inserted.
266 * Otherwise returns NULL.
268 static dmu_buf_impl_t *
269 dbuf_hash_insert(dmu_buf_impl_t *db)
271 dbuf_hash_table_t *h = &dbuf_hash_table;
272 objset_t *os = db->db_objset;
273 uint64_t obj = db->db.db_object;
274 int level = db->db_level;
275 uint64_t blkid, hv, idx;
278 blkid = db->db_blkid;
279 hv = dbuf_hash(os, obj, level, blkid);
280 idx = hv & h->hash_table_mask;
282 mutex_enter(DBUF_HASH_MUTEX(h, idx));
283 for (dbf = h->hash_table[idx]; dbf != NULL; dbf = dbf->db_hash_next) {
284 if (DBUF_EQUAL(dbf, os, obj, level, blkid)) {
285 mutex_enter(&dbf->db_mtx);
286 if (dbf->db_state != DB_EVICTING) {
287 mutex_exit(DBUF_HASH_MUTEX(h, idx));
290 mutex_exit(&dbf->db_mtx);
294 mutex_enter(&db->db_mtx);
295 db->db_hash_next = h->hash_table[idx];
296 h->hash_table[idx] = db;
297 mutex_exit(DBUF_HASH_MUTEX(h, idx));
298 atomic_inc_64(&dbuf_hash_count);
304 * Remove an entry from the hash table. It must be in the EVICTING state.
307 dbuf_hash_remove(dmu_buf_impl_t *db)
309 dbuf_hash_table_t *h = &dbuf_hash_table;
311 dmu_buf_impl_t *dbf, **dbp;
313 hv = dbuf_hash(db->db_objset, db->db.db_object,
314 db->db_level, db->db_blkid);
315 idx = hv & h->hash_table_mask;
318 * We mustn't hold db_mtx to maintain lock ordering:
319 * DBUF_HASH_MUTEX > db_mtx.
321 ASSERT(refcount_is_zero(&db->db_holds));
322 ASSERT(db->db_state == DB_EVICTING);
323 ASSERT(!MUTEX_HELD(&db->db_mtx));
325 mutex_enter(DBUF_HASH_MUTEX(h, idx));
326 dbp = &h->hash_table[idx];
327 while ((dbf = *dbp) != db) {
328 dbp = &dbf->db_hash_next;
331 *dbp = db->db_hash_next;
332 db->db_hash_next = NULL;
333 mutex_exit(DBUF_HASH_MUTEX(h, idx));
334 atomic_dec_64(&dbuf_hash_count);
340 } dbvu_verify_type_t;
343 dbuf_verify_user(dmu_buf_impl_t *db, dbvu_verify_type_t verify_type)
348 if (db->db_user == NULL)
351 /* Only data blocks support the attachment of user data. */
352 ASSERT(db->db_level == 0);
354 /* Clients must resolve a dbuf before attaching user data. */
355 ASSERT(db->db.db_data != NULL);
356 ASSERT3U(db->db_state, ==, DB_CACHED);
358 holds = refcount_count(&db->db_holds);
359 if (verify_type == DBVU_EVICTING) {
361 * Immediate eviction occurs when holds == dirtycnt.
362 * For normal eviction buffers, holds is zero on
363 * eviction, except when dbuf_fix_old_data() calls
364 * dbuf_clear_data(). However, the hold count can grow
365 * during eviction even though db_mtx is held (see
366 * dmu_bonus_hold() for an example), so we can only
367 * test the generic invariant that holds >= dirtycnt.
369 ASSERT3U(holds, >=, db->db_dirtycnt);
371 if (db->db_user_immediate_evict == TRUE)
372 ASSERT3U(holds, >=, db->db_dirtycnt);
374 ASSERT3U(holds, >, 0);
380 dbuf_evict_user(dmu_buf_impl_t *db)
382 dmu_buf_user_t *dbu = db->db_user;
385 ASSERT(MUTEX_HELD(&db->db_mtx));
390 dbuf_verify_user(db, DBVU_EVICTING);
394 if (dbu->dbu_clear_on_evict_dbufp != NULL)
395 *dbu->dbu_clear_on_evict_dbufp = NULL;
399 * There are two eviction callbacks - one that we call synchronously
400 * and one that we invoke via a taskq. The async one is useful for
401 * avoiding lock order reversals and limiting stack depth.
403 * Note that if we have a sync callback but no async callback,
404 * it's likely that the sync callback will free the structure
405 * containing the dbu. In that case we need to take care to not
406 * dereference dbu after calling the sync evict func.
408 has_async = (dbu->dbu_evict_func_async != NULL);
410 if (dbu->dbu_evict_func_sync != NULL)
411 dbu->dbu_evict_func_sync(dbu);
414 taskq_dispatch_ent(dbu_evict_taskq, dbu->dbu_evict_func_async,
415 dbu, 0, &dbu->dbu_tqent);
420 dbuf_is_metadata(dmu_buf_impl_t *db)
423 * Consider indirect blocks and spill blocks to be meta data.
425 if (db->db_level > 0 || db->db_blkid == DMU_SPILL_BLKID) {
428 boolean_t is_metadata;
431 is_metadata = DMU_OT_IS_METADATA(DB_DNODE(db)->dn_type);
434 return (is_metadata);
440 * This function *must* return indices evenly distributed between all
441 * sublists of the multilist. This is needed due to how the dbuf eviction
442 * code is laid out; dbuf_evict_thread() assumes dbufs are evenly
443 * distributed between all sublists and uses this assumption when
444 * deciding which sublist to evict from and how much to evict from it.
447 dbuf_cache_multilist_index_func(multilist_t *ml, void *obj)
449 dmu_buf_impl_t *db = obj;
452 * The assumption here, is the hash value for a given
453 * dmu_buf_impl_t will remain constant throughout it's lifetime
454 * (i.e. it's objset, object, level and blkid fields don't change).
455 * Thus, we don't need to store the dbuf's sublist index
456 * on insertion, as this index can be recalculated on removal.
458 * Also, the low order bits of the hash value are thought to be
459 * distributed evenly. Otherwise, in the case that the multilist
460 * has a power of two number of sublists, each sublists' usage
461 * would not be evenly distributed.
463 return (dbuf_hash(db->db_objset, db->db.db_object,
464 db->db_level, db->db_blkid) %
465 multilist_get_num_sublists(ml));
468 static inline boolean_t
469 dbuf_cache_above_hiwater(void)
471 uint64_t dbuf_cache_hiwater_bytes =
472 (dbuf_cache_max_bytes * dbuf_cache_hiwater_pct) / 100;
474 return (refcount_count(&dbuf_cache_size) >
475 dbuf_cache_max_bytes + dbuf_cache_hiwater_bytes);
478 static inline boolean_t
479 dbuf_cache_above_lowater(void)
481 uint64_t dbuf_cache_lowater_bytes =
482 (dbuf_cache_max_bytes * dbuf_cache_lowater_pct) / 100;
484 return (refcount_count(&dbuf_cache_size) >
485 dbuf_cache_max_bytes - dbuf_cache_lowater_bytes);
489 * Evict the oldest eligible dbuf from the dbuf cache.
494 int idx = multilist_get_random_index(dbuf_cache);
495 multilist_sublist_t *mls = multilist_sublist_lock(dbuf_cache, idx);
497 ASSERT(!MUTEX_HELD(&dbuf_evict_lock));
500 * Set the thread's tsd to indicate that it's processing evictions.
501 * Once a thread stops evicting from the dbuf cache it will
502 * reset its tsd to NULL.
504 ASSERT3P(tsd_get(zfs_dbuf_evict_key), ==, NULL);
505 (void) tsd_set(zfs_dbuf_evict_key, (void *)B_TRUE);
507 db = multilist_sublist_tail(mls);
508 while (db != NULL && mutex_tryenter(&db->db_mtx) == 0) {
509 db = multilist_sublist_prev(mls, db);
512 DTRACE_PROBE2(dbuf__evict__one, dmu_buf_impl_t *, db,
513 multilist_sublist_t *, mls);
516 multilist_sublist_remove(mls, db);
517 multilist_sublist_unlock(mls);
518 (void) refcount_remove_many(&dbuf_cache_size,
522 multilist_sublist_unlock(mls);
524 (void) tsd_set(zfs_dbuf_evict_key, NULL);
528 * The dbuf evict thread is responsible for aging out dbufs from the
529 * cache. Once the cache has reached it's maximum size, dbufs are removed
530 * and destroyed. The eviction thread will continue running until the size
531 * of the dbuf cache is at or below the maximum size. Once the dbuf is aged
532 * out of the cache it is destroyed and becomes eligible for arc eviction.
535 dbuf_evict_thread(void)
539 CALLB_CPR_INIT(&cpr, &dbuf_evict_lock, callb_generic_cpr, FTAG);
541 mutex_enter(&dbuf_evict_lock);
542 while (!dbuf_evict_thread_exit) {
543 while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit) {
544 CALLB_CPR_SAFE_BEGIN(&cpr);
545 (void) cv_timedwait_sig_hires(&dbuf_evict_cv,
546 &dbuf_evict_lock, SEC2NSEC(1), MSEC2NSEC(1), 0);
547 CALLB_CPR_SAFE_END(&cpr, &dbuf_evict_lock);
549 mutex_exit(&dbuf_evict_lock);
552 * Keep evicting as long as we're above the low water mark
553 * for the cache. We do this without holding the locks to
554 * minimize lock contention.
556 while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit) {
560 mutex_enter(&dbuf_evict_lock);
563 dbuf_evict_thread_exit = B_FALSE;
564 cv_broadcast(&dbuf_evict_cv);
565 CALLB_CPR_EXIT(&cpr); /* drops dbuf_evict_lock */
570 * Wake up the dbuf eviction thread if the dbuf cache is at its max size.
571 * If the dbuf cache is at its high water mark, then evict a dbuf from the
572 * dbuf cache using the callers context.
575 dbuf_evict_notify(void)
579 * We use thread specific data to track when a thread has
580 * started processing evictions. This allows us to avoid deeply
581 * nested stacks that would have a call flow similar to this:
583 * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify()
586 * +-----dbuf_destroy()<--dbuf_evict_one()<--------+
588 * The dbuf_eviction_thread will always have its tsd set until
589 * that thread exits. All other threads will only set their tsd
590 * if they are participating in the eviction process. This only
591 * happens if the eviction thread is unable to process evictions
592 * fast enough. To keep the dbuf cache size in check, other threads
593 * can evict from the dbuf cache directly. Those threads will set
594 * their tsd values so that we ensure that they only evict one dbuf
595 * from the dbuf cache.
597 if (tsd_get(zfs_dbuf_evict_key) != NULL)
600 if (refcount_count(&dbuf_cache_size) > dbuf_cache_max_bytes) {
601 boolean_t evict_now = B_FALSE;
603 mutex_enter(&dbuf_evict_lock);
604 if (refcount_count(&dbuf_cache_size) > dbuf_cache_max_bytes) {
605 evict_now = dbuf_cache_above_hiwater();
606 cv_signal(&dbuf_evict_cv);
608 mutex_exit(&dbuf_evict_lock);
621 uint64_t hsize = 1ULL << 16;
622 dbuf_hash_table_t *h = &dbuf_hash_table;
626 * The hash table is big enough to fill all of physical memory
627 * with an average block size of zfs_arc_average_blocksize (default 8K).
628 * By default, the table will take up
629 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
631 while (hsize * zfs_arc_average_blocksize < physmem * PAGESIZE)
635 h->hash_table_mask = hsize - 1;
636 #if defined(_KERNEL) && defined(HAVE_SPL)
638 * Large allocations which do not require contiguous pages
639 * should be using vmem_alloc() in the linux kernel
641 h->hash_table = vmem_zalloc(hsize * sizeof (void *), KM_SLEEP);
643 h->hash_table = kmem_zalloc(hsize * sizeof (void *), KM_NOSLEEP);
645 if (h->hash_table == NULL) {
646 /* XXX - we should really return an error instead of assert */
647 ASSERT(hsize > (1ULL << 10));
652 dbuf_kmem_cache = kmem_cache_create("dmu_buf_impl_t",
653 sizeof (dmu_buf_impl_t),
654 0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0);
656 for (i = 0; i < DBUF_MUTEXES; i++)
657 mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL);
662 * Setup the parameters for the dbuf cache. We cap the size of the
663 * dbuf cache to 1/32nd (default) of the size of the ARC.
665 dbuf_cache_max_bytes = MIN(dbuf_cache_max_bytes,
666 arc_max_bytes() >> dbuf_cache_max_shift);
669 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
670 * configuration is not required.
672 dbu_evict_taskq = taskq_create("dbu_evict", 1, defclsyspri, 0, 0, 0);
674 dbuf_cache = multilist_create(sizeof (dmu_buf_impl_t),
675 offsetof(dmu_buf_impl_t, db_cache_link),
676 dbuf_cache_multilist_index_func);
677 refcount_create(&dbuf_cache_size);
679 tsd_create(&zfs_dbuf_evict_key, NULL);
680 dbuf_evict_thread_exit = B_FALSE;
681 mutex_init(&dbuf_evict_lock, NULL, MUTEX_DEFAULT, NULL);
682 cv_init(&dbuf_evict_cv, NULL, CV_DEFAULT, NULL);
683 dbuf_cache_evict_thread = thread_create(NULL, 0, dbuf_evict_thread,
684 NULL, 0, &p0, TS_RUN, minclsyspri);
690 dbuf_hash_table_t *h = &dbuf_hash_table;
693 dbuf_stats_destroy();
695 for (i = 0; i < DBUF_MUTEXES; i++)
696 mutex_destroy(&h->hash_mutexes[i]);
697 #if defined(_KERNEL) && defined(HAVE_SPL)
699 * Large allocations which do not require contiguous pages
700 * should be using vmem_free() in the linux kernel
702 vmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
704 kmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
706 kmem_cache_destroy(dbuf_kmem_cache);
707 taskq_destroy(dbu_evict_taskq);
709 mutex_enter(&dbuf_evict_lock);
710 dbuf_evict_thread_exit = B_TRUE;
711 while (dbuf_evict_thread_exit) {
712 cv_signal(&dbuf_evict_cv);
713 cv_wait(&dbuf_evict_cv, &dbuf_evict_lock);
715 mutex_exit(&dbuf_evict_lock);
716 tsd_destroy(&zfs_dbuf_evict_key);
718 mutex_destroy(&dbuf_evict_lock);
719 cv_destroy(&dbuf_evict_cv);
721 refcount_destroy(&dbuf_cache_size);
722 multilist_destroy(dbuf_cache);
731 dbuf_verify(dmu_buf_impl_t *db)
734 dbuf_dirty_record_t *dr;
736 ASSERT(MUTEX_HELD(&db->db_mtx));
738 if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY))
741 ASSERT(db->db_objset != NULL);
745 ASSERT(db->db_parent == NULL);
746 ASSERT(db->db_blkptr == NULL);
748 ASSERT3U(db->db.db_object, ==, dn->dn_object);
749 ASSERT3P(db->db_objset, ==, dn->dn_objset);
750 ASSERT3U(db->db_level, <, dn->dn_nlevels);
751 ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
752 db->db_blkid == DMU_SPILL_BLKID ||
753 !avl_is_empty(&dn->dn_dbufs));
755 if (db->db_blkid == DMU_BONUS_BLKID) {
757 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
758 ASSERT3U(db->db.db_offset, ==, DMU_BONUS_BLKID);
759 } else if (db->db_blkid == DMU_SPILL_BLKID) {
761 ASSERT0(db->db.db_offset);
763 ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size);
766 for (dr = db->db_data_pending; dr != NULL; dr = dr->dr_next)
767 ASSERT(dr->dr_dbuf == db);
769 for (dr = db->db_last_dirty; dr != NULL; dr = dr->dr_next)
770 ASSERT(dr->dr_dbuf == db);
773 * We can't assert that db_size matches dn_datablksz because it
774 * can be momentarily different when another thread is doing
777 if (db->db_level == 0 && db->db.db_object == DMU_META_DNODE_OBJECT) {
778 dr = db->db_data_pending;
780 * It should only be modified in syncing context, so
781 * make sure we only have one copy of the data.
783 ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf);
786 /* verify db->db_blkptr */
788 if (db->db_parent == dn->dn_dbuf) {
789 /* db is pointed to by the dnode */
790 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
791 if (DMU_OBJECT_IS_SPECIAL(db->db.db_object))
792 ASSERT(db->db_parent == NULL);
794 ASSERT(db->db_parent != NULL);
795 if (db->db_blkid != DMU_SPILL_BLKID)
796 ASSERT3P(db->db_blkptr, ==,
797 &dn->dn_phys->dn_blkptr[db->db_blkid]);
799 /* db is pointed to by an indirect block */
800 ASSERTV(int epb = db->db_parent->db.db_size >>
802 ASSERT3U(db->db_parent->db_level, ==, db->db_level+1);
803 ASSERT3U(db->db_parent->db.db_object, ==,
806 * dnode_grow_indblksz() can make this fail if we don't
807 * have the struct_rwlock. XXX indblksz no longer
808 * grows. safe to do this now?
810 if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
811 ASSERT3P(db->db_blkptr, ==,
812 ((blkptr_t *)db->db_parent->db.db_data +
813 db->db_blkid % epb));
817 if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) &&
818 (db->db_buf == NULL || db->db_buf->b_data) &&
819 db->db.db_data && db->db_blkid != DMU_BONUS_BLKID &&
820 db->db_state != DB_FILL && !dn->dn_free_txg) {
822 * If the blkptr isn't set but they have nonzero data,
823 * it had better be dirty, otherwise we'll lose that
824 * data when we evict this buffer.
826 * There is an exception to this rule for indirect blocks; in
827 * this case, if the indirect block is a hole, we fill in a few
828 * fields on each of the child blocks (importantly, birth time)
829 * to prevent hole birth times from being lost when you
830 * partially fill in a hole.
832 if (db->db_dirtycnt == 0) {
833 if (db->db_level == 0) {
834 uint64_t *buf = db->db.db_data;
837 for (i = 0; i < db->db.db_size >> 3; i++) {
842 blkptr_t *bps = db->db.db_data;
843 ASSERT3U(1 << DB_DNODE(db)->dn_indblkshift, ==,
846 * We want to verify that all the blkptrs in the
847 * indirect block are holes, but we may have
848 * automatically set up a few fields for them.
849 * We iterate through each blkptr and verify
850 * they only have those fields set.
853 i < db->db.db_size / sizeof (blkptr_t);
855 blkptr_t *bp = &bps[i];
856 ASSERT(ZIO_CHECKSUM_IS_ZERO(
859 DVA_IS_EMPTY(&bp->blk_dva[0]) &&
860 DVA_IS_EMPTY(&bp->blk_dva[1]) &&
861 DVA_IS_EMPTY(&bp->blk_dva[2]));
862 ASSERT0(bp->blk_fill);
863 ASSERT0(bp->blk_pad[0]);
864 ASSERT0(bp->blk_pad[1]);
865 ASSERT(!BP_IS_EMBEDDED(bp));
866 ASSERT(BP_IS_HOLE(bp));
867 ASSERT0(bp->blk_phys_birth);
877 dbuf_clear_data(dmu_buf_impl_t *db)
879 ASSERT(MUTEX_HELD(&db->db_mtx));
881 ASSERT3P(db->db_buf, ==, NULL);
882 db->db.db_data = NULL;
883 if (db->db_state != DB_NOFILL)
884 db->db_state = DB_UNCACHED;
888 dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf)
890 ASSERT(MUTEX_HELD(&db->db_mtx));
894 ASSERT(buf->b_data != NULL);
895 db->db.db_data = buf->b_data;
899 * Loan out an arc_buf for read. Return the loaned arc_buf.
902 dbuf_loan_arcbuf(dmu_buf_impl_t *db)
906 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
907 mutex_enter(&db->db_mtx);
908 if (arc_released(db->db_buf) || refcount_count(&db->db_holds) > 1) {
909 int blksz = db->db.db_size;
910 spa_t *spa = db->db_objset->os_spa;
912 mutex_exit(&db->db_mtx);
913 abuf = arc_loan_buf(spa, B_FALSE, blksz);
914 bcopy(db->db.db_data, abuf->b_data, blksz);
917 arc_loan_inuse_buf(abuf, db);
920 mutex_exit(&db->db_mtx);
926 * Calculate which level n block references the data at the level 0 offset
930 dbuf_whichblock(const dnode_t *dn, const int64_t level, const uint64_t offset)
932 if (dn->dn_datablkshift != 0 && dn->dn_indblkshift != 0) {
934 * The level n blkid is equal to the level 0 blkid divided by
935 * the number of level 0s in a level n block.
937 * The level 0 blkid is offset >> datablkshift =
938 * offset / 2^datablkshift.
940 * The number of level 0s in a level n is the number of block
941 * pointers in an indirect block, raised to the power of level.
942 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
943 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
945 * Thus, the level n blkid is: offset /
946 * ((2^datablkshift)*(2^(level*(indblkshift - SPA_BLKPTRSHIFT)))
947 * = offset / 2^(datablkshift + level *
948 * (indblkshift - SPA_BLKPTRSHIFT))
949 * = offset >> (datablkshift + level *
950 * (indblkshift - SPA_BLKPTRSHIFT))
953 const unsigned exp = dn->dn_datablkshift +
954 level * (dn->dn_indblkshift - SPA_BLKPTRSHIFT);
956 if (exp >= 8 * sizeof (offset)) {
957 /* This only happens on the highest indirection level */
958 ASSERT3U(level, ==, dn->dn_nlevels - 1);
962 ASSERT3U(exp, <, 8 * sizeof (offset));
964 return (offset >> exp);
966 ASSERT3U(offset, <, dn->dn_datablksz);
972 dbuf_read_done(zio_t *zio, arc_buf_t *buf, void *vdb)
974 dmu_buf_impl_t *db = vdb;
976 mutex_enter(&db->db_mtx);
977 ASSERT3U(db->db_state, ==, DB_READ);
979 * All reads are synchronous, so we must have a hold on the dbuf
981 ASSERT(refcount_count(&db->db_holds) > 0);
982 ASSERT(db->db_buf == NULL);
983 ASSERT(db->db.db_data == NULL);
984 if (db->db_level == 0 && db->db_freed_in_flight) {
985 /* we were freed in flight; disregard any error */
986 arc_release(buf, db);
987 bzero(buf->b_data, db->db.db_size);
989 db->db_freed_in_flight = FALSE;
990 dbuf_set_data(db, buf);
991 db->db_state = DB_CACHED;
992 } else if (zio == NULL || zio->io_error == 0) {
993 dbuf_set_data(db, buf);
994 db->db_state = DB_CACHED;
996 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
997 ASSERT3P(db->db_buf, ==, NULL);
998 arc_buf_destroy(buf, db);
999 db->db_state = DB_UNCACHED;
1001 cv_broadcast(&db->db_changed);
1002 dbuf_rele_and_unlock(db, NULL);
1006 dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
1009 zbookmark_phys_t zb;
1010 uint32_t aflags = ARC_FLAG_NOWAIT;
1015 ASSERT(!refcount_is_zero(&db->db_holds));
1016 /* We need the struct_rwlock to prevent db_blkptr from changing. */
1017 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1018 ASSERT(MUTEX_HELD(&db->db_mtx));
1019 ASSERT(db->db_state == DB_UNCACHED);
1020 ASSERT(db->db_buf == NULL);
1022 if (db->db_blkid == DMU_BONUS_BLKID) {
1024 * The bonus length stored in the dnode may be less than
1025 * the maximum available space in the bonus buffer.
1027 int bonuslen = MIN(dn->dn_bonuslen, dn->dn_phys->dn_bonuslen);
1028 int max_bonuslen = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots);
1030 ASSERT3U(bonuslen, <=, db->db.db_size);
1031 db->db.db_data = kmem_alloc(max_bonuslen, KM_SLEEP);
1032 arc_space_consume(max_bonuslen, ARC_SPACE_BONUS);
1033 if (bonuslen < max_bonuslen)
1034 bzero(db->db.db_data, max_bonuslen);
1036 bcopy(DN_BONUS(dn->dn_phys), db->db.db_data, bonuslen);
1038 db->db_state = DB_CACHED;
1039 mutex_exit(&db->db_mtx);
1044 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
1045 * processes the delete record and clears the bp while we are waiting
1046 * for the dn_mtx (resulting in a "no" from block_freed).
1048 if (db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr) ||
1049 (db->db_level == 0 && (dnode_block_freed(dn, db->db_blkid) ||
1050 BP_IS_HOLE(db->db_blkptr)))) {
1051 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1053 dbuf_set_data(db, arc_alloc_buf(db->db_objset->os_spa, db, type,
1055 bzero(db->db.db_data, db->db.db_size);
1057 if (db->db_blkptr != NULL && db->db_level > 0 &&
1058 BP_IS_HOLE(db->db_blkptr) &&
1059 db->db_blkptr->blk_birth != 0) {
1060 blkptr_t *bps = db->db.db_data;
1062 for (i = 0; i < ((1 <<
1063 DB_DNODE(db)->dn_indblkshift) / sizeof (blkptr_t));
1065 blkptr_t *bp = &bps[i];
1066 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
1067 1 << dn->dn_indblkshift);
1069 BP_GET_LEVEL(db->db_blkptr) == 1 ?
1071 BP_GET_LSIZE(db->db_blkptr));
1072 BP_SET_TYPE(bp, BP_GET_TYPE(db->db_blkptr));
1074 BP_GET_LEVEL(db->db_blkptr) - 1);
1075 BP_SET_BIRTH(bp, db->db_blkptr->blk_birth, 0);
1079 db->db_state = DB_CACHED;
1080 mutex_exit(&db->db_mtx);
1086 db->db_state = DB_READ;
1087 mutex_exit(&db->db_mtx);
1089 if (DBUF_IS_L2CACHEABLE(db))
1090 aflags |= ARC_FLAG_L2CACHE;
1092 SET_BOOKMARK(&zb, db->db_objset->os_dsl_dataset ?
1093 db->db_objset->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1094 db->db.db_object, db->db_level, db->db_blkid);
1096 dbuf_add_ref(db, NULL);
1098 err = arc_read(zio, db->db_objset->os_spa, db->db_blkptr,
1099 dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ,
1100 (flags & DB_RF_CANFAIL) ? ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED,
1107 * This is our just-in-time copy function. It makes a copy of buffers that
1108 * have been modified in a previous transaction group before we access them in
1109 * the current active group.
1111 * This function is used in three places: when we are dirtying a buffer for the
1112 * first time in a txg, when we are freeing a range in a dnode that includes
1113 * this buffer, and when we are accessing a buffer which was received compressed
1114 * and later referenced in a WRITE_BYREF record.
1116 * Note that when we are called from dbuf_free_range() we do not put a hold on
1117 * the buffer, we just traverse the active dbuf list for the dnode.
1120 dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg)
1122 dbuf_dirty_record_t *dr = db->db_last_dirty;
1124 ASSERT(MUTEX_HELD(&db->db_mtx));
1125 ASSERT(db->db.db_data != NULL);
1126 ASSERT(db->db_level == 0);
1127 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT);
1130 (dr->dt.dl.dr_data !=
1131 ((db->db_blkid == DMU_BONUS_BLKID) ? db->db.db_data : db->db_buf)))
1135 * If the last dirty record for this dbuf has not yet synced
1136 * and its referencing the dbuf data, either:
1137 * reset the reference to point to a new copy,
1138 * or (if there a no active holders)
1139 * just null out the current db_data pointer.
1141 ASSERT(dr->dr_txg >= txg - 2);
1142 if (db->db_blkid == DMU_BONUS_BLKID) {
1143 dnode_t *dn = DB_DNODE(db);
1144 int bonuslen = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots);
1145 dr->dt.dl.dr_data = kmem_alloc(bonuslen, KM_SLEEP);
1146 arc_space_consume(bonuslen, ARC_SPACE_BONUS);
1147 bcopy(db->db.db_data, dr->dt.dl.dr_data, bonuslen);
1148 } else if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
1149 int size = arc_buf_size(db->db_buf);
1150 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1151 spa_t *spa = db->db_objset->os_spa;
1152 enum zio_compress compress_type =
1153 arc_get_compression(db->db_buf);
1155 if (compress_type == ZIO_COMPRESS_OFF) {
1156 dr->dt.dl.dr_data = arc_alloc_buf(spa, db, type, size);
1158 ASSERT3U(type, ==, ARC_BUFC_DATA);
1159 dr->dt.dl.dr_data = arc_alloc_compressed_buf(spa, db,
1160 size, arc_buf_lsize(db->db_buf), compress_type);
1162 bcopy(db->db.db_data, dr->dt.dl.dr_data->b_data, size);
1165 dbuf_clear_data(db);
1170 dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
1173 boolean_t havepzio = (zio != NULL);
1178 * We don't have to hold the mutex to check db_state because it
1179 * can't be freed while we have a hold on the buffer.
1181 ASSERT(!refcount_is_zero(&db->db_holds));
1183 if (db->db_state == DB_NOFILL)
1184 return (SET_ERROR(EIO));
1188 if ((flags & DB_RF_HAVESTRUCT) == 0)
1189 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1191 prefetch = db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1192 (flags & DB_RF_NOPREFETCH) == 0 && dn != NULL &&
1193 DBUF_IS_CACHEABLE(db);
1195 mutex_enter(&db->db_mtx);
1196 if (db->db_state == DB_CACHED) {
1198 * If the arc buf is compressed, we need to decompress it to
1199 * read the data. This could happen during the "zfs receive" of
1200 * a stream which is compressed and deduplicated.
1202 if (db->db_buf != NULL &&
1203 arc_get_compression(db->db_buf) != ZIO_COMPRESS_OFF) {
1204 dbuf_fix_old_data(db,
1205 spa_syncing_txg(dmu_objset_spa(db->db_objset)));
1206 err = arc_decompress(db->db_buf);
1207 dbuf_set_data(db, db->db_buf);
1209 mutex_exit(&db->db_mtx);
1211 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE);
1212 if ((flags & DB_RF_HAVESTRUCT) == 0)
1213 rw_exit(&dn->dn_struct_rwlock);
1215 } else if (db->db_state == DB_UNCACHED) {
1216 spa_t *spa = dn->dn_objset->os_spa;
1219 db->db_blkptr != NULL && !BP_IS_HOLE(db->db_blkptr))
1220 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
1222 err = dbuf_read_impl(db, zio, flags);
1224 /* dbuf_read_impl has dropped db_mtx for us */
1226 if (!err && prefetch)
1227 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE);
1229 if ((flags & DB_RF_HAVESTRUCT) == 0)
1230 rw_exit(&dn->dn_struct_rwlock);
1233 if (!err && !havepzio && zio != NULL)
1234 err = zio_wait(zio);
1237 * Another reader came in while the dbuf was in flight
1238 * between UNCACHED and CACHED. Either a writer will finish
1239 * writing the buffer (sending the dbuf to CACHED) or the
1240 * first reader's request will reach the read_done callback
1241 * and send the dbuf to CACHED. Otherwise, a failure
1242 * occurred and the dbuf went to UNCACHED.
1244 mutex_exit(&db->db_mtx);
1246 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE);
1247 if ((flags & DB_RF_HAVESTRUCT) == 0)
1248 rw_exit(&dn->dn_struct_rwlock);
1251 /* Skip the wait per the caller's request. */
1252 mutex_enter(&db->db_mtx);
1253 if ((flags & DB_RF_NEVERWAIT) == 0) {
1254 while (db->db_state == DB_READ ||
1255 db->db_state == DB_FILL) {
1256 ASSERT(db->db_state == DB_READ ||
1257 (flags & DB_RF_HAVESTRUCT) == 0);
1258 DTRACE_PROBE2(blocked__read, dmu_buf_impl_t *,
1260 cv_wait(&db->db_changed, &db->db_mtx);
1262 if (db->db_state == DB_UNCACHED)
1263 err = SET_ERROR(EIO);
1265 mutex_exit(&db->db_mtx);
1268 ASSERT(err || havepzio || db->db_state == DB_CACHED);
1273 dbuf_noread(dmu_buf_impl_t *db)
1275 ASSERT(!refcount_is_zero(&db->db_holds));
1276 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1277 mutex_enter(&db->db_mtx);
1278 while (db->db_state == DB_READ || db->db_state == DB_FILL)
1279 cv_wait(&db->db_changed, &db->db_mtx);
1280 if (db->db_state == DB_UNCACHED) {
1281 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1282 spa_t *spa = db->db_objset->os_spa;
1284 ASSERT(db->db_buf == NULL);
1285 ASSERT(db->db.db_data == NULL);
1286 dbuf_set_data(db, arc_alloc_buf(spa, db, type, db->db.db_size));
1287 db->db_state = DB_FILL;
1288 } else if (db->db_state == DB_NOFILL) {
1289 dbuf_clear_data(db);
1291 ASSERT3U(db->db_state, ==, DB_CACHED);
1293 mutex_exit(&db->db_mtx);
1297 dbuf_unoverride(dbuf_dirty_record_t *dr)
1299 dmu_buf_impl_t *db = dr->dr_dbuf;
1300 blkptr_t *bp = &dr->dt.dl.dr_overridden_by;
1301 uint64_t txg = dr->dr_txg;
1303 ASSERT(MUTEX_HELD(&db->db_mtx));
1304 ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC);
1305 ASSERT(db->db_level == 0);
1307 if (db->db_blkid == DMU_BONUS_BLKID ||
1308 dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN)
1311 ASSERT(db->db_data_pending != dr);
1313 /* free this block */
1314 if (!BP_IS_HOLE(bp) && !dr->dt.dl.dr_nopwrite)
1315 zio_free(db->db_objset->os_spa, txg, bp);
1317 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
1318 dr->dt.dl.dr_nopwrite = B_FALSE;
1321 * Release the already-written buffer, so we leave it in
1322 * a consistent dirty state. Note that all callers are
1323 * modifying the buffer, so they will immediately do
1324 * another (redundant) arc_release(). Therefore, leave
1325 * the buf thawed to save the effort of freezing &
1326 * immediately re-thawing it.
1328 arc_release(dr->dt.dl.dr_data, db);
1332 * Evict (if its unreferenced) or clear (if its referenced) any level-0
1333 * data blocks in the free range, so that any future readers will find
1337 dbuf_free_range(dnode_t *dn, uint64_t start_blkid, uint64_t end_blkid,
1340 dmu_buf_impl_t *db_search;
1341 dmu_buf_impl_t *db, *db_next;
1342 uint64_t txg = tx->tx_txg;
1345 if (end_blkid > dn->dn_maxblkid &&
1346 !(start_blkid == DMU_SPILL_BLKID || end_blkid == DMU_SPILL_BLKID))
1347 end_blkid = dn->dn_maxblkid;
1348 dprintf_dnode(dn, "start=%llu end=%llu\n", start_blkid, end_blkid);
1350 db_search = kmem_alloc(sizeof (dmu_buf_impl_t), KM_SLEEP);
1351 db_search->db_level = 0;
1352 db_search->db_blkid = start_blkid;
1353 db_search->db_state = DB_SEARCH;
1355 mutex_enter(&dn->dn_dbufs_mtx);
1356 db = avl_find(&dn->dn_dbufs, db_search, &where);
1357 ASSERT3P(db, ==, NULL);
1359 db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER);
1361 for (; db != NULL; db = db_next) {
1362 db_next = AVL_NEXT(&dn->dn_dbufs, db);
1363 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1365 if (db->db_level != 0 || db->db_blkid > end_blkid) {
1368 ASSERT3U(db->db_blkid, >=, start_blkid);
1370 /* found a level 0 buffer in the range */
1371 mutex_enter(&db->db_mtx);
1372 if (dbuf_undirty(db, tx)) {
1373 /* mutex has been dropped and dbuf destroyed */
1377 if (db->db_state == DB_UNCACHED ||
1378 db->db_state == DB_NOFILL ||
1379 db->db_state == DB_EVICTING) {
1380 ASSERT(db->db.db_data == NULL);
1381 mutex_exit(&db->db_mtx);
1384 if (db->db_state == DB_READ || db->db_state == DB_FILL) {
1385 /* will be handled in dbuf_read_done or dbuf_rele */
1386 db->db_freed_in_flight = TRUE;
1387 mutex_exit(&db->db_mtx);
1390 if (refcount_count(&db->db_holds) == 0) {
1395 /* The dbuf is referenced */
1397 if (db->db_last_dirty != NULL) {
1398 dbuf_dirty_record_t *dr = db->db_last_dirty;
1400 if (dr->dr_txg == txg) {
1402 * This buffer is "in-use", re-adjust the file
1403 * size to reflect that this buffer may
1404 * contain new data when we sync.
1406 if (db->db_blkid != DMU_SPILL_BLKID &&
1407 db->db_blkid > dn->dn_maxblkid)
1408 dn->dn_maxblkid = db->db_blkid;
1409 dbuf_unoverride(dr);
1412 * This dbuf is not dirty in the open context.
1413 * Either uncache it (if its not referenced in
1414 * the open context) or reset its contents to
1417 dbuf_fix_old_data(db, txg);
1420 /* clear the contents if its cached */
1421 if (db->db_state == DB_CACHED) {
1422 ASSERT(db->db.db_data != NULL);
1423 arc_release(db->db_buf, db);
1424 bzero(db->db.db_data, db->db.db_size);
1425 arc_buf_freeze(db->db_buf);
1428 mutex_exit(&db->db_mtx);
1431 kmem_free(db_search, sizeof (dmu_buf_impl_t));
1432 mutex_exit(&dn->dn_dbufs_mtx);
1436 dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx)
1438 arc_buf_t *buf, *obuf;
1439 int osize = db->db.db_size;
1440 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1443 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1448 /* XXX does *this* func really need the lock? */
1449 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
1452 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
1453 * is OK, because there can be no other references to the db
1454 * when we are changing its size, so no concurrent DB_FILL can
1458 * XXX we should be doing a dbuf_read, checking the return
1459 * value and returning that up to our callers
1461 dmu_buf_will_dirty(&db->db, tx);
1463 /* create the data buffer for the new block */
1464 buf = arc_alloc_buf(dn->dn_objset->os_spa, db, type, size);
1466 /* copy old block data to the new block */
1468 bcopy(obuf->b_data, buf->b_data, MIN(osize, size));
1469 /* zero the remainder */
1471 bzero((uint8_t *)buf->b_data + osize, size - osize);
1473 mutex_enter(&db->db_mtx);
1474 dbuf_set_data(db, buf);
1475 arc_buf_destroy(obuf, db);
1476 db->db.db_size = size;
1478 if (db->db_level == 0) {
1479 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
1480 db->db_last_dirty->dt.dl.dr_data = buf;
1482 mutex_exit(&db->db_mtx);
1484 dmu_objset_willuse_space(dn->dn_objset, size - osize, tx);
1489 dbuf_release_bp(dmu_buf_impl_t *db)
1491 ASSERTV(objset_t *os = db->db_objset);
1493 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
1494 ASSERT(arc_released(os->os_phys_buf) ||
1495 list_link_active(&os->os_dsl_dataset->ds_synced_link));
1496 ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf));
1498 (void) arc_release(db->db_buf, db);
1502 * We already have a dirty record for this TXG, and we are being
1506 dbuf_redirty(dbuf_dirty_record_t *dr)
1508 dmu_buf_impl_t *db = dr->dr_dbuf;
1510 ASSERT(MUTEX_HELD(&db->db_mtx));
1512 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID) {
1514 * If this buffer has already been written out,
1515 * we now need to reset its state.
1517 dbuf_unoverride(dr);
1518 if (db->db.db_object != DMU_META_DNODE_OBJECT &&
1519 db->db_state != DB_NOFILL) {
1520 /* Already released on initial dirty, so just thaw. */
1521 ASSERT(arc_released(db->db_buf));
1522 arc_buf_thaw(db->db_buf);
1527 dbuf_dirty_record_t *
1528 dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1532 dbuf_dirty_record_t **drp, *dr;
1533 int drop_struct_lock = FALSE;
1534 int txgoff = tx->tx_txg & TXG_MASK;
1536 ASSERT(tx->tx_txg != 0);
1537 ASSERT(!refcount_is_zero(&db->db_holds));
1538 DMU_TX_DIRTY_BUF(tx, db);
1543 * Shouldn't dirty a regular buffer in syncing context. Private
1544 * objects may be dirtied in syncing context, but only if they
1545 * were already pre-dirtied in open context.
1548 if (dn->dn_objset->os_dsl_dataset != NULL) {
1549 rrw_enter(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock,
1552 ASSERT(!dmu_tx_is_syncing(tx) ||
1553 BP_IS_HOLE(dn->dn_objset->os_rootbp) ||
1554 DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1555 dn->dn_objset->os_dsl_dataset == NULL);
1556 if (dn->dn_objset->os_dsl_dataset != NULL)
1557 rrw_exit(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock, FTAG);
1560 * We make this assert for private objects as well, but after we
1561 * check if we're already dirty. They are allowed to re-dirty
1562 * in syncing context.
1564 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1565 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1566 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1568 mutex_enter(&db->db_mtx);
1570 * XXX make this true for indirects too? The problem is that
1571 * transactions created with dmu_tx_create_assigned() from
1572 * syncing context don't bother holding ahead.
1574 ASSERT(db->db_level != 0 ||
1575 db->db_state == DB_CACHED || db->db_state == DB_FILL ||
1576 db->db_state == DB_NOFILL);
1578 mutex_enter(&dn->dn_mtx);
1580 * Don't set dirtyctx to SYNC if we're just modifying this as we
1581 * initialize the objset.
1583 if (dn->dn_dirtyctx == DN_UNDIRTIED) {
1584 if (dn->dn_objset->os_dsl_dataset != NULL) {
1585 rrw_enter(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock,
1588 if (!BP_IS_HOLE(dn->dn_objset->os_rootbp)) {
1589 dn->dn_dirtyctx = (dmu_tx_is_syncing(tx) ?
1590 DN_DIRTY_SYNC : DN_DIRTY_OPEN);
1591 ASSERT(dn->dn_dirtyctx_firstset == NULL);
1592 dn->dn_dirtyctx_firstset = kmem_alloc(1, KM_SLEEP);
1594 if (dn->dn_objset->os_dsl_dataset != NULL) {
1595 rrw_exit(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock,
1599 mutex_exit(&dn->dn_mtx);
1601 if (db->db_blkid == DMU_SPILL_BLKID)
1602 dn->dn_have_spill = B_TRUE;
1605 * If this buffer is already dirty, we're done.
1607 drp = &db->db_last_dirty;
1608 ASSERT(*drp == NULL || (*drp)->dr_txg <= tx->tx_txg ||
1609 db->db.db_object == DMU_META_DNODE_OBJECT);
1610 while ((dr = *drp) != NULL && dr->dr_txg > tx->tx_txg)
1612 if (dr && dr->dr_txg == tx->tx_txg) {
1616 mutex_exit(&db->db_mtx);
1621 * Only valid if not already dirty.
1623 ASSERT(dn->dn_object == 0 ||
1624 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1625 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1627 ASSERT3U(dn->dn_nlevels, >, db->db_level);
1628 ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) ||
1629 dn->dn_phys->dn_nlevels > db->db_level ||
1630 dn->dn_next_nlevels[txgoff] > db->db_level ||
1631 dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level ||
1632 dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level);
1635 * We should only be dirtying in syncing context if it's the
1636 * mos or we're initializing the os or it's a special object.
1637 * However, we are allowed to dirty in syncing context provided
1638 * we already dirtied it in open context. Hence we must make
1639 * this assertion only if we're not already dirty.
1643 if (dn->dn_objset->os_dsl_dataset != NULL)
1644 rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_READER, FTAG);
1645 ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1646 os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp));
1647 if (dn->dn_objset->os_dsl_dataset != NULL)
1648 rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG);
1650 ASSERT(db->db.db_size != 0);
1652 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1654 if (db->db_blkid != DMU_BONUS_BLKID) {
1655 dmu_objset_willuse_space(os, db->db.db_size, tx);
1659 * If this buffer is dirty in an old transaction group we need
1660 * to make a copy of it so that the changes we make in this
1661 * transaction group won't leak out when we sync the older txg.
1663 dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP);
1664 list_link_init(&dr->dr_dirty_node);
1665 if (db->db_level == 0) {
1666 void *data_old = db->db_buf;
1668 if (db->db_state != DB_NOFILL) {
1669 if (db->db_blkid == DMU_BONUS_BLKID) {
1670 dbuf_fix_old_data(db, tx->tx_txg);
1671 data_old = db->db.db_data;
1672 } else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
1674 * Release the data buffer from the cache so
1675 * that we can modify it without impacting
1676 * possible other users of this cached data
1677 * block. Note that indirect blocks and
1678 * private objects are not released until the
1679 * syncing state (since they are only modified
1682 arc_release(db->db_buf, db);
1683 dbuf_fix_old_data(db, tx->tx_txg);
1684 data_old = db->db_buf;
1686 ASSERT(data_old != NULL);
1688 dr->dt.dl.dr_data = data_old;
1690 mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_NOLOCKDEP, NULL);
1691 list_create(&dr->dt.di.dr_children,
1692 sizeof (dbuf_dirty_record_t),
1693 offsetof(dbuf_dirty_record_t, dr_dirty_node));
1695 if (db->db_blkid != DMU_BONUS_BLKID && os->os_dsl_dataset != NULL)
1696 dr->dr_accounted = db->db.db_size;
1698 dr->dr_txg = tx->tx_txg;
1703 * We could have been freed_in_flight between the dbuf_noread
1704 * and dbuf_dirty. We win, as though the dbuf_noread() had
1705 * happened after the free.
1707 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1708 db->db_blkid != DMU_SPILL_BLKID) {
1709 mutex_enter(&dn->dn_mtx);
1710 if (dn->dn_free_ranges[txgoff] != NULL) {
1711 range_tree_clear(dn->dn_free_ranges[txgoff],
1714 mutex_exit(&dn->dn_mtx);
1715 db->db_freed_in_flight = FALSE;
1719 * This buffer is now part of this txg
1721 dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg);
1722 db->db_dirtycnt += 1;
1723 ASSERT3U(db->db_dirtycnt, <=, 3);
1725 mutex_exit(&db->db_mtx);
1727 if (db->db_blkid == DMU_BONUS_BLKID ||
1728 db->db_blkid == DMU_SPILL_BLKID) {
1729 mutex_enter(&dn->dn_mtx);
1730 ASSERT(!list_link_active(&dr->dr_dirty_node));
1731 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1732 mutex_exit(&dn->dn_mtx);
1733 dnode_setdirty(dn, tx);
1739 * The dn_struct_rwlock prevents db_blkptr from changing
1740 * due to a write from syncing context completing
1741 * while we are running, so we want to acquire it before
1742 * looking at db_blkptr.
1744 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
1745 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1746 drop_struct_lock = TRUE;
1750 * If we are overwriting a dedup BP, then unless it is snapshotted,
1751 * when we get to syncing context we will need to decrement its
1752 * refcount in the DDT. Prefetch the relevant DDT block so that
1753 * syncing context won't have to wait for the i/o.
1755 ddt_prefetch(os->os_spa, db->db_blkptr);
1757 if (db->db_level == 0) {
1758 dnode_new_blkid(dn, db->db_blkid, tx, drop_struct_lock);
1759 ASSERT(dn->dn_maxblkid >= db->db_blkid);
1762 if (db->db_level+1 < dn->dn_nlevels) {
1763 dmu_buf_impl_t *parent = db->db_parent;
1764 dbuf_dirty_record_t *di;
1765 int parent_held = FALSE;
1767 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
1768 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1770 parent = dbuf_hold_level(dn, db->db_level+1,
1771 db->db_blkid >> epbs, FTAG);
1772 ASSERT(parent != NULL);
1775 if (drop_struct_lock)
1776 rw_exit(&dn->dn_struct_rwlock);
1777 ASSERT3U(db->db_level+1, ==, parent->db_level);
1778 di = dbuf_dirty(parent, tx);
1780 dbuf_rele(parent, FTAG);
1782 mutex_enter(&db->db_mtx);
1784 * Since we've dropped the mutex, it's possible that
1785 * dbuf_undirty() might have changed this out from under us.
1787 if (db->db_last_dirty == dr ||
1788 dn->dn_object == DMU_META_DNODE_OBJECT) {
1789 mutex_enter(&di->dt.di.dr_mtx);
1790 ASSERT3U(di->dr_txg, ==, tx->tx_txg);
1791 ASSERT(!list_link_active(&dr->dr_dirty_node));
1792 list_insert_tail(&di->dt.di.dr_children, dr);
1793 mutex_exit(&di->dt.di.dr_mtx);
1796 mutex_exit(&db->db_mtx);
1798 ASSERT(db->db_level+1 == dn->dn_nlevels);
1799 ASSERT(db->db_blkid < dn->dn_nblkptr);
1800 ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf);
1801 mutex_enter(&dn->dn_mtx);
1802 ASSERT(!list_link_active(&dr->dr_dirty_node));
1803 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1804 mutex_exit(&dn->dn_mtx);
1805 if (drop_struct_lock)
1806 rw_exit(&dn->dn_struct_rwlock);
1809 dnode_setdirty(dn, tx);
1815 * Undirty a buffer in the transaction group referenced by the given
1816 * transaction. Return whether this evicted the dbuf.
1819 dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1822 uint64_t txg = tx->tx_txg;
1823 dbuf_dirty_record_t *dr, **drp;
1828 * Due to our use of dn_nlevels below, this can only be called
1829 * in open context, unless we are operating on the MOS.
1830 * From syncing context, dn_nlevels may be different from the
1831 * dn_nlevels used when dbuf was dirtied.
1833 ASSERT(db->db_objset ==
1834 dmu_objset_pool(db->db_objset)->dp_meta_objset ||
1835 txg != spa_syncing_txg(dmu_objset_spa(db->db_objset)));
1836 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1837 ASSERT0(db->db_level);
1838 ASSERT(MUTEX_HELD(&db->db_mtx));
1841 * If this buffer is not dirty, we're done.
1843 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1844 if (dr->dr_txg <= txg)
1846 if (dr == NULL || dr->dr_txg < txg)
1848 ASSERT(dr->dr_txg == txg);
1849 ASSERT(dr->dr_dbuf == db);
1854 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1856 ASSERT(db->db.db_size != 0);
1858 dsl_pool_undirty_space(dmu_objset_pool(dn->dn_objset),
1859 dr->dr_accounted, txg);
1864 * Note that there are three places in dbuf_dirty()
1865 * where this dirty record may be put on a list.
1866 * Make sure to do a list_remove corresponding to
1867 * every one of those list_insert calls.
1869 if (dr->dr_parent) {
1870 mutex_enter(&dr->dr_parent->dt.di.dr_mtx);
1871 list_remove(&dr->dr_parent->dt.di.dr_children, dr);
1872 mutex_exit(&dr->dr_parent->dt.di.dr_mtx);
1873 } else if (db->db_blkid == DMU_SPILL_BLKID ||
1874 db->db_level + 1 == dn->dn_nlevels) {
1875 ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf);
1876 mutex_enter(&dn->dn_mtx);
1877 list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr);
1878 mutex_exit(&dn->dn_mtx);
1882 if (db->db_state != DB_NOFILL) {
1883 dbuf_unoverride(dr);
1885 ASSERT(db->db_buf != NULL);
1886 ASSERT(dr->dt.dl.dr_data != NULL);
1887 if (dr->dt.dl.dr_data != db->db_buf)
1888 arc_buf_destroy(dr->dt.dl.dr_data, db);
1891 kmem_free(dr, sizeof (dbuf_dirty_record_t));
1893 ASSERT(db->db_dirtycnt > 0);
1894 db->db_dirtycnt -= 1;
1896 if (refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) {
1897 ASSERT(db->db_state == DB_NOFILL || arc_released(db->db_buf));
1906 dmu_buf_will_dirty(dmu_buf_t *db_fake, dmu_tx_t *tx)
1908 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1909 int rf = DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH;
1910 dbuf_dirty_record_t *dr;
1912 ASSERT(tx->tx_txg != 0);
1913 ASSERT(!refcount_is_zero(&db->db_holds));
1916 * Quick check for dirtyness. For already dirty blocks, this
1917 * reduces runtime of this function by >90%, and overall performance
1918 * by 50% for some workloads (e.g. file deletion with indirect blocks
1921 mutex_enter(&db->db_mtx);
1923 for (dr = db->db_last_dirty;
1924 dr != NULL && dr->dr_txg >= tx->tx_txg; dr = dr->dr_next) {
1926 * It's possible that it is already dirty but not cached,
1927 * because there are some calls to dbuf_dirty() that don't
1928 * go through dmu_buf_will_dirty().
1930 if (dr->dr_txg == tx->tx_txg && db->db_state == DB_CACHED) {
1931 /* This dbuf is already dirty and cached. */
1933 mutex_exit(&db->db_mtx);
1937 mutex_exit(&db->db_mtx);
1940 if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock))
1941 rf |= DB_RF_HAVESTRUCT;
1943 (void) dbuf_read(db, NULL, rf);
1944 (void) dbuf_dirty(db, tx);
1948 dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1950 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1952 db->db_state = DB_NOFILL;
1954 dmu_buf_will_fill(db_fake, tx);
1958 dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1960 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1962 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1963 ASSERT(tx->tx_txg != 0);
1964 ASSERT(db->db_level == 0);
1965 ASSERT(!refcount_is_zero(&db->db_holds));
1967 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
1968 dmu_tx_private_ok(tx));
1971 (void) dbuf_dirty(db, tx);
1974 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1977 dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx)
1979 mutex_enter(&db->db_mtx);
1982 if (db->db_state == DB_FILL) {
1983 if (db->db_level == 0 && db->db_freed_in_flight) {
1984 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1985 /* we were freed while filling */
1986 /* XXX dbuf_undirty? */
1987 bzero(db->db.db_data, db->db.db_size);
1988 db->db_freed_in_flight = FALSE;
1990 db->db_state = DB_CACHED;
1991 cv_broadcast(&db->db_changed);
1993 mutex_exit(&db->db_mtx);
1997 dmu_buf_write_embedded(dmu_buf_t *dbuf, void *data,
1998 bp_embedded_type_t etype, enum zio_compress comp,
1999 int uncompressed_size, int compressed_size, int byteorder,
2002 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
2003 struct dirty_leaf *dl;
2004 dmu_object_type_t type;
2006 if (etype == BP_EMBEDDED_TYPE_DATA) {
2007 ASSERT(spa_feature_is_active(dmu_objset_spa(db->db_objset),
2008 SPA_FEATURE_EMBEDDED_DATA));
2012 type = DB_DNODE(db)->dn_type;
2015 ASSERT0(db->db_level);
2016 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2018 dmu_buf_will_not_fill(dbuf, tx);
2020 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
2021 dl = &db->db_last_dirty->dt.dl;
2022 encode_embedded_bp_compressed(&dl->dr_overridden_by,
2023 data, comp, uncompressed_size, compressed_size);
2024 BPE_SET_ETYPE(&dl->dr_overridden_by, etype);
2025 BP_SET_TYPE(&dl->dr_overridden_by, type);
2026 BP_SET_LEVEL(&dl->dr_overridden_by, 0);
2027 BP_SET_BYTEORDER(&dl->dr_overridden_by, byteorder);
2029 dl->dr_override_state = DR_OVERRIDDEN;
2030 dl->dr_overridden_by.blk_birth = db->db_last_dirty->dr_txg;
2034 * Directly assign a provided arc buf to a given dbuf if it's not referenced
2035 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
2038 dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx)
2040 ASSERT(!refcount_is_zero(&db->db_holds));
2041 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2042 ASSERT(db->db_level == 0);
2043 ASSERT3U(dbuf_is_metadata(db), ==, arc_is_metadata(buf));
2044 ASSERT(buf != NULL);
2045 ASSERT(arc_buf_lsize(buf) == db->db.db_size);
2046 ASSERT(tx->tx_txg != 0);
2048 arc_return_buf(buf, db);
2049 ASSERT(arc_released(buf));
2051 mutex_enter(&db->db_mtx);
2053 while (db->db_state == DB_READ || db->db_state == DB_FILL)
2054 cv_wait(&db->db_changed, &db->db_mtx);
2056 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED);
2058 if (db->db_state == DB_CACHED &&
2059 refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) {
2060 mutex_exit(&db->db_mtx);
2061 (void) dbuf_dirty(db, tx);
2062 bcopy(buf->b_data, db->db.db_data, db->db.db_size);
2063 arc_buf_destroy(buf, db);
2064 xuio_stat_wbuf_copied();
2068 xuio_stat_wbuf_nocopy();
2069 if (db->db_state == DB_CACHED) {
2070 dbuf_dirty_record_t *dr = db->db_last_dirty;
2072 ASSERT(db->db_buf != NULL);
2073 if (dr != NULL && dr->dr_txg == tx->tx_txg) {
2074 ASSERT(dr->dt.dl.dr_data == db->db_buf);
2075 if (!arc_released(db->db_buf)) {
2076 ASSERT(dr->dt.dl.dr_override_state ==
2078 arc_release(db->db_buf, db);
2080 dr->dt.dl.dr_data = buf;
2081 arc_buf_destroy(db->db_buf, db);
2082 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) {
2083 arc_release(db->db_buf, db);
2084 arc_buf_destroy(db->db_buf, db);
2088 ASSERT(db->db_buf == NULL);
2089 dbuf_set_data(db, buf);
2090 db->db_state = DB_FILL;
2091 mutex_exit(&db->db_mtx);
2092 (void) dbuf_dirty(db, tx);
2093 dmu_buf_fill_done(&db->db, tx);
2097 dbuf_destroy(dmu_buf_impl_t *db)
2100 dmu_buf_impl_t *parent = db->db_parent;
2101 dmu_buf_impl_t *dndb;
2103 ASSERT(MUTEX_HELD(&db->db_mtx));
2104 ASSERT(refcount_is_zero(&db->db_holds));
2106 if (db->db_buf != NULL) {
2107 arc_buf_destroy(db->db_buf, db);
2111 if (db->db_blkid == DMU_BONUS_BLKID) {
2112 int slots = DB_DNODE(db)->dn_num_slots;
2113 int bonuslen = DN_SLOTS_TO_BONUSLEN(slots);
2114 ASSERT(db->db.db_data != NULL);
2115 kmem_free(db->db.db_data, bonuslen);
2116 arc_space_return(bonuslen, ARC_SPACE_BONUS);
2117 db->db_state = DB_UNCACHED;
2120 dbuf_clear_data(db);
2122 if (multilist_link_active(&db->db_cache_link)) {
2123 multilist_remove(dbuf_cache, db);
2124 (void) refcount_remove_many(&dbuf_cache_size,
2125 db->db.db_size, db);
2128 ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL);
2129 ASSERT(db->db_data_pending == NULL);
2131 db->db_state = DB_EVICTING;
2132 db->db_blkptr = NULL;
2135 * Now that db_state is DB_EVICTING, nobody else can find this via
2136 * the hash table. We can now drop db_mtx, which allows us to
2137 * acquire the dn_dbufs_mtx.
2139 mutex_exit(&db->db_mtx);
2144 if (db->db_blkid != DMU_BONUS_BLKID) {
2145 boolean_t needlock = !MUTEX_HELD(&dn->dn_dbufs_mtx);
2147 mutex_enter(&dn->dn_dbufs_mtx);
2148 avl_remove(&dn->dn_dbufs, db);
2149 atomic_dec_32(&dn->dn_dbufs_count);
2153 mutex_exit(&dn->dn_dbufs_mtx);
2155 * Decrementing the dbuf count means that the hold corresponding
2156 * to the removed dbuf is no longer discounted in dnode_move(),
2157 * so the dnode cannot be moved until after we release the hold.
2158 * The membar_producer() ensures visibility of the decremented
2159 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
2163 db->db_dnode_handle = NULL;
2165 dbuf_hash_remove(db);
2170 ASSERT(refcount_is_zero(&db->db_holds));
2172 db->db_parent = NULL;
2174 ASSERT(db->db_buf == NULL);
2175 ASSERT(db->db.db_data == NULL);
2176 ASSERT(db->db_hash_next == NULL);
2177 ASSERT(db->db_blkptr == NULL);
2178 ASSERT(db->db_data_pending == NULL);
2179 ASSERT(!multilist_link_active(&db->db_cache_link));
2181 kmem_cache_free(dbuf_kmem_cache, db);
2182 arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_DBUF);
2185 * If this dbuf is referenced from an indirect dbuf,
2186 * decrement the ref count on the indirect dbuf.
2188 if (parent && parent != dndb)
2189 dbuf_rele(parent, db);
2193 * Note: While bpp will always be updated if the function returns success,
2194 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
2195 * this happens when the dnode is the meta-dnode, or a userused or groupused
2198 __attribute__((always_inline))
2200 dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse,
2201 dmu_buf_impl_t **parentp, blkptr_t **bpp, struct dbuf_hold_impl_data *dh)
2208 ASSERT(blkid != DMU_BONUS_BLKID);
2210 if (blkid == DMU_SPILL_BLKID) {
2211 mutex_enter(&dn->dn_mtx);
2212 if (dn->dn_have_spill &&
2213 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
2214 *bpp = DN_SPILL_BLKPTR(dn->dn_phys);
2217 dbuf_add_ref(dn->dn_dbuf, NULL);
2218 *parentp = dn->dn_dbuf;
2219 mutex_exit(&dn->dn_mtx);
2224 (dn->dn_phys->dn_nlevels == 0) ? 1 : dn->dn_phys->dn_nlevels;
2225 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
2227 ASSERT3U(level * epbs, <, 64);
2228 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2230 * This assertion shouldn't trip as long as the max indirect block size
2231 * is less than 1M. The reason for this is that up to that point,
2232 * the number of levels required to address an entire object with blocks
2233 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In
2234 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55
2235 * (i.e. we can address the entire object), objects will all use at most
2236 * N-1 levels and the assertion won't overflow. However, once epbs is
2237 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be
2238 * enough to address an entire object, so objects will have 5 levels,
2239 * but then this assertion will overflow.
2241 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we
2242 * need to redo this logic to handle overflows.
2244 ASSERT(level >= nlevels ||
2245 ((nlevels - level - 1) * epbs) +
2246 highbit64(dn->dn_phys->dn_nblkptr) <= 64);
2247 if (level >= nlevels ||
2248 blkid >= ((uint64_t)dn->dn_phys->dn_nblkptr <<
2249 ((nlevels - level - 1) * epbs)) ||
2251 blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) {
2252 /* the buffer has no parent yet */
2253 return (SET_ERROR(ENOENT));
2254 } else if (level < nlevels-1) {
2255 /* this block is referenced from an indirect block */
2258 err = dbuf_hold_impl(dn, level+1,
2259 blkid >> epbs, fail_sparse, FALSE, NULL, parentp);
2261 __dbuf_hold_impl_init(dh + 1, dn, dh->dh_level + 1,
2262 blkid >> epbs, fail_sparse, FALSE, NULL,
2263 parentp, dh->dh_depth + 1);
2264 err = __dbuf_hold_impl(dh + 1);
2268 err = dbuf_read(*parentp, NULL,
2269 (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL));
2271 dbuf_rele(*parentp, NULL);
2275 *bpp = ((blkptr_t *)(*parentp)->db.db_data) +
2276 (blkid & ((1ULL << epbs) - 1));
2277 if (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))
2278 ASSERT(BP_IS_HOLE(*bpp));
2281 /* the block is referenced from the dnode */
2282 ASSERT3U(level, ==, nlevels-1);
2283 ASSERT(dn->dn_phys->dn_nblkptr == 0 ||
2284 blkid < dn->dn_phys->dn_nblkptr);
2286 dbuf_add_ref(dn->dn_dbuf, NULL);
2287 *parentp = dn->dn_dbuf;
2289 *bpp = &dn->dn_phys->dn_blkptr[blkid];
2294 static dmu_buf_impl_t *
2295 dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid,
2296 dmu_buf_impl_t *parent, blkptr_t *blkptr)
2298 objset_t *os = dn->dn_objset;
2299 dmu_buf_impl_t *db, *odb;
2301 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2302 ASSERT(dn->dn_type != DMU_OT_NONE);
2304 db = kmem_cache_alloc(dbuf_kmem_cache, KM_SLEEP);
2307 db->db.db_object = dn->dn_object;
2308 db->db_level = level;
2309 db->db_blkid = blkid;
2310 db->db_last_dirty = NULL;
2311 db->db_dirtycnt = 0;
2312 db->db_dnode_handle = dn->dn_handle;
2313 db->db_parent = parent;
2314 db->db_blkptr = blkptr;
2317 db->db_user_immediate_evict = FALSE;
2318 db->db_freed_in_flight = FALSE;
2319 db->db_pending_evict = FALSE;
2321 if (blkid == DMU_BONUS_BLKID) {
2322 ASSERT3P(parent, ==, dn->dn_dbuf);
2323 db->db.db_size = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots) -
2324 (dn->dn_nblkptr-1) * sizeof (blkptr_t);
2325 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
2326 db->db.db_offset = DMU_BONUS_BLKID;
2327 db->db_state = DB_UNCACHED;
2328 /* the bonus dbuf is not placed in the hash table */
2329 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_DBUF);
2331 } else if (blkid == DMU_SPILL_BLKID) {
2332 db->db.db_size = (blkptr != NULL) ?
2333 BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE;
2334 db->db.db_offset = 0;
2337 db->db_level ? 1 << dn->dn_indblkshift : dn->dn_datablksz;
2338 db->db.db_size = blocksize;
2339 db->db.db_offset = db->db_blkid * blocksize;
2343 * Hold the dn_dbufs_mtx while we get the new dbuf
2344 * in the hash table *and* added to the dbufs list.
2345 * This prevents a possible deadlock with someone
2346 * trying to look up this dbuf before its added to the
2349 mutex_enter(&dn->dn_dbufs_mtx);
2350 db->db_state = DB_EVICTING;
2351 if ((odb = dbuf_hash_insert(db)) != NULL) {
2352 /* someone else inserted it first */
2353 kmem_cache_free(dbuf_kmem_cache, db);
2354 mutex_exit(&dn->dn_dbufs_mtx);
2357 avl_add(&dn->dn_dbufs, db);
2359 db->db_state = DB_UNCACHED;
2360 mutex_exit(&dn->dn_dbufs_mtx);
2361 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_DBUF);
2363 if (parent && parent != dn->dn_dbuf)
2364 dbuf_add_ref(parent, db);
2366 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
2367 refcount_count(&dn->dn_holds) > 0);
2368 (void) refcount_add(&dn->dn_holds, db);
2369 atomic_inc_32(&dn->dn_dbufs_count);
2371 dprintf_dbuf(db, "db=%p\n", db);
2376 typedef struct dbuf_prefetch_arg {
2377 spa_t *dpa_spa; /* The spa to issue the prefetch in. */
2378 zbookmark_phys_t dpa_zb; /* The target block to prefetch. */
2379 int dpa_epbs; /* Entries (blkptr_t's) Per Block Shift. */
2380 int dpa_curlevel; /* The current level that we're reading */
2381 dnode_t *dpa_dnode; /* The dnode associated with the prefetch */
2382 zio_priority_t dpa_prio; /* The priority I/Os should be issued at. */
2383 zio_t *dpa_zio; /* The parent zio_t for all prefetches. */
2384 arc_flags_t dpa_aflags; /* Flags to pass to the final prefetch. */
2385 } dbuf_prefetch_arg_t;
2388 * Actually issue the prefetch read for the block given.
2391 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t *dpa, blkptr_t *bp)
2394 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
2397 aflags = dpa->dpa_aflags | ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH;
2399 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp));
2400 ASSERT3U(dpa->dpa_curlevel, ==, dpa->dpa_zb.zb_level);
2401 ASSERT(dpa->dpa_zio != NULL);
2402 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa, bp, NULL, NULL,
2403 dpa->dpa_prio, ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2404 &aflags, &dpa->dpa_zb);
2408 * Called when an indirect block above our prefetch target is read in. This
2409 * will either read in the next indirect block down the tree or issue the actual
2410 * prefetch if the next block down is our target.
2413 dbuf_prefetch_indirect_done(zio_t *zio, arc_buf_t *abuf, void *private)
2415 dbuf_prefetch_arg_t *dpa = private;
2419 ASSERT3S(dpa->dpa_zb.zb_level, <, dpa->dpa_curlevel);
2420 ASSERT3S(dpa->dpa_curlevel, >, 0);
2423 * The dpa_dnode is only valid if we are called with a NULL
2424 * zio. This indicates that the arc_read() returned without
2425 * first calling zio_read() to issue a physical read. Once
2426 * a physical read is made the dpa_dnode must be invalidated
2427 * as the locks guarding it may have been dropped. If the
2428 * dpa_dnode is still valid, then we want to add it to the dbuf
2429 * cache. To do so, we must hold the dbuf associated with the block
2430 * we just prefetched, read its contents so that we associate it
2431 * with an arc_buf_t, and then release it.
2434 ASSERT3S(BP_GET_LEVEL(zio->io_bp), ==, dpa->dpa_curlevel);
2435 if (zio->io_flags & ZIO_FLAG_RAW) {
2436 ASSERT3U(BP_GET_PSIZE(zio->io_bp), ==, zio->io_size);
2438 ASSERT3U(BP_GET_LSIZE(zio->io_bp), ==, zio->io_size);
2440 ASSERT3P(zio->io_spa, ==, dpa->dpa_spa);
2442 dpa->dpa_dnode = NULL;
2443 } else if (dpa->dpa_dnode != NULL) {
2444 uint64_t curblkid = dpa->dpa_zb.zb_blkid >>
2445 (dpa->dpa_epbs * (dpa->dpa_curlevel -
2446 dpa->dpa_zb.zb_level));
2447 dmu_buf_impl_t *db = dbuf_hold_level(dpa->dpa_dnode,
2448 dpa->dpa_curlevel, curblkid, FTAG);
2449 (void) dbuf_read(db, NULL,
2450 DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH | DB_RF_HAVESTRUCT);
2451 dbuf_rele(db, FTAG);
2454 dpa->dpa_curlevel--;
2456 nextblkid = dpa->dpa_zb.zb_blkid >>
2457 (dpa->dpa_epbs * (dpa->dpa_curlevel - dpa->dpa_zb.zb_level));
2458 bp = ((blkptr_t *)abuf->b_data) +
2459 P2PHASE(nextblkid, 1ULL << dpa->dpa_epbs);
2460 if (BP_IS_HOLE(bp) || (zio != NULL && zio->io_error != 0)) {
2461 kmem_free(dpa, sizeof (*dpa));
2462 } else if (dpa->dpa_curlevel == dpa->dpa_zb.zb_level) {
2463 ASSERT3U(nextblkid, ==, dpa->dpa_zb.zb_blkid);
2464 dbuf_issue_final_prefetch(dpa, bp);
2465 kmem_free(dpa, sizeof (*dpa));
2467 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT;
2468 zbookmark_phys_t zb;
2470 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp));
2472 SET_BOOKMARK(&zb, dpa->dpa_zb.zb_objset,
2473 dpa->dpa_zb.zb_object, dpa->dpa_curlevel, nextblkid);
2475 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa,
2476 bp, dbuf_prefetch_indirect_done, dpa, dpa->dpa_prio,
2477 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2481 arc_buf_destroy(abuf, private);
2485 * Issue prefetch reads for the given block on the given level. If the indirect
2486 * blocks above that block are not in memory, we will read them in
2487 * asynchronously. As a result, this call never blocks waiting for a read to
2491 dbuf_prefetch(dnode_t *dn, int64_t level, uint64_t blkid, zio_priority_t prio,
2495 int epbs, nlevels, curlevel;
2499 dbuf_prefetch_arg_t *dpa;
2502 ASSERT(blkid != DMU_BONUS_BLKID);
2503 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2505 if (blkid > dn->dn_maxblkid)
2508 if (dnode_block_freed(dn, blkid))
2512 * This dnode hasn't been written to disk yet, so there's nothing to
2515 nlevels = dn->dn_phys->dn_nlevels;
2516 if (level >= nlevels || dn->dn_phys->dn_nblkptr == 0)
2519 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2520 if (dn->dn_phys->dn_maxblkid < blkid << (epbs * level))
2523 db = dbuf_find(dn->dn_objset, dn->dn_object,
2526 mutex_exit(&db->db_mtx);
2528 * This dbuf already exists. It is either CACHED, or
2529 * (we assume) about to be read or filled.
2535 * Find the closest ancestor (indirect block) of the target block
2536 * that is present in the cache. In this indirect block, we will
2537 * find the bp that is at curlevel, curblkid.
2541 while (curlevel < nlevels - 1) {
2542 int parent_level = curlevel + 1;
2543 uint64_t parent_blkid = curblkid >> epbs;
2546 if (dbuf_hold_impl(dn, parent_level, parent_blkid,
2547 FALSE, TRUE, FTAG, &db) == 0) {
2548 blkptr_t *bpp = db->db_buf->b_data;
2549 bp = bpp[P2PHASE(curblkid, 1 << epbs)];
2550 dbuf_rele(db, FTAG);
2554 curlevel = parent_level;
2555 curblkid = parent_blkid;
2558 if (curlevel == nlevels - 1) {
2559 /* No cached indirect blocks found. */
2560 ASSERT3U(curblkid, <, dn->dn_phys->dn_nblkptr);
2561 bp = dn->dn_phys->dn_blkptr[curblkid];
2563 if (BP_IS_HOLE(&bp))
2566 ASSERT3U(curlevel, ==, BP_GET_LEVEL(&bp));
2568 pio = zio_root(dmu_objset_spa(dn->dn_objset), NULL, NULL,
2571 dpa = kmem_zalloc(sizeof (*dpa), KM_SLEEP);
2572 ds = dn->dn_objset->os_dsl_dataset;
2573 SET_BOOKMARK(&dpa->dpa_zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET,
2574 dn->dn_object, level, blkid);
2575 dpa->dpa_curlevel = curlevel;
2576 dpa->dpa_prio = prio;
2577 dpa->dpa_aflags = aflags;
2578 dpa->dpa_spa = dn->dn_objset->os_spa;
2579 dpa->dpa_dnode = dn;
2580 dpa->dpa_epbs = epbs;
2584 * If we have the indirect just above us, no need to do the asynchronous
2585 * prefetch chain; we'll just run the last step ourselves. If we're at
2586 * a higher level, though, we want to issue the prefetches for all the
2587 * indirect blocks asynchronously, so we can go on with whatever we were
2590 if (curlevel == level) {
2591 ASSERT3U(curblkid, ==, blkid);
2592 dbuf_issue_final_prefetch(dpa, &bp);
2593 kmem_free(dpa, sizeof (*dpa));
2595 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT;
2596 zbookmark_phys_t zb;
2598 SET_BOOKMARK(&zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET,
2599 dn->dn_object, curlevel, curblkid);
2600 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa,
2601 &bp, dbuf_prefetch_indirect_done, dpa, prio,
2602 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2606 * We use pio here instead of dpa_zio since it's possible that
2607 * dpa may have already been freed.
2612 #define DBUF_HOLD_IMPL_MAX_DEPTH 20
2615 * Returns with db_holds incremented, and db_mtx not held.
2616 * Note: dn_struct_rwlock must be held.
2619 __dbuf_hold_impl(struct dbuf_hold_impl_data *dh)
2621 ASSERT3S(dh->dh_depth, <, DBUF_HOLD_IMPL_MAX_DEPTH);
2622 dh->dh_parent = NULL;
2624 ASSERT(dh->dh_blkid != DMU_BONUS_BLKID);
2625 ASSERT(RW_LOCK_HELD(&dh->dh_dn->dn_struct_rwlock));
2626 ASSERT3U(dh->dh_dn->dn_nlevels, >, dh->dh_level);
2628 *(dh->dh_dbp) = NULL;
2630 /* dbuf_find() returns with db_mtx held */
2631 dh->dh_db = dbuf_find(dh->dh_dn->dn_objset, dh->dh_dn->dn_object,
2632 dh->dh_level, dh->dh_blkid);
2634 if (dh->dh_db == NULL) {
2637 if (dh->dh_fail_uncached)
2638 return (SET_ERROR(ENOENT));
2640 ASSERT3P(dh->dh_parent, ==, NULL);
2641 dh->dh_err = dbuf_findbp(dh->dh_dn, dh->dh_level, dh->dh_blkid,
2642 dh->dh_fail_sparse, &dh->dh_parent, &dh->dh_bp, dh);
2643 if (dh->dh_fail_sparse) {
2644 if (dh->dh_err == 0 &&
2645 dh->dh_bp && BP_IS_HOLE(dh->dh_bp))
2646 dh->dh_err = SET_ERROR(ENOENT);
2649 dbuf_rele(dh->dh_parent, NULL);
2650 return (dh->dh_err);
2653 if (dh->dh_err && dh->dh_err != ENOENT)
2654 return (dh->dh_err);
2655 dh->dh_db = dbuf_create(dh->dh_dn, dh->dh_level, dh->dh_blkid,
2656 dh->dh_parent, dh->dh_bp);
2659 if (dh->dh_fail_uncached && dh->dh_db->db_state != DB_CACHED) {
2660 mutex_exit(&dh->dh_db->db_mtx);
2661 return (SET_ERROR(ENOENT));
2664 if (dh->dh_db->db_buf != NULL)
2665 ASSERT3P(dh->dh_db->db.db_data, ==, dh->dh_db->db_buf->b_data);
2667 ASSERT(dh->dh_db->db_buf == NULL || arc_referenced(dh->dh_db->db_buf));
2670 * If this buffer is currently syncing out, and we are are
2671 * still referencing it from db_data, we need to make a copy
2672 * of it in case we decide we want to dirty it again in this txg.
2674 if (dh->dh_db->db_level == 0 &&
2675 dh->dh_db->db_blkid != DMU_BONUS_BLKID &&
2676 dh->dh_dn->dn_object != DMU_META_DNODE_OBJECT &&
2677 dh->dh_db->db_state == DB_CACHED && dh->dh_db->db_data_pending) {
2678 dh->dh_dr = dh->dh_db->db_data_pending;
2680 if (dh->dh_dr->dt.dl.dr_data == dh->dh_db->db_buf) {
2681 dh->dh_type = DBUF_GET_BUFC_TYPE(dh->dh_db);
2683 dbuf_set_data(dh->dh_db,
2684 arc_alloc_buf(dh->dh_dn->dn_objset->os_spa,
2685 dh->dh_db, dh->dh_type, dh->dh_db->db.db_size));
2686 bcopy(dh->dh_dr->dt.dl.dr_data->b_data,
2687 dh->dh_db->db.db_data, dh->dh_db->db.db_size);
2691 if (multilist_link_active(&dh->dh_db->db_cache_link)) {
2692 ASSERT(refcount_is_zero(&dh->dh_db->db_holds));
2693 multilist_remove(dbuf_cache, dh->dh_db);
2694 (void) refcount_remove_many(&dbuf_cache_size,
2695 dh->dh_db->db.db_size, dh->dh_db);
2697 (void) refcount_add(&dh->dh_db->db_holds, dh->dh_tag);
2698 DBUF_VERIFY(dh->dh_db);
2699 mutex_exit(&dh->dh_db->db_mtx);
2701 /* NOTE: we can't rele the parent until after we drop the db_mtx */
2703 dbuf_rele(dh->dh_parent, NULL);
2705 ASSERT3P(DB_DNODE(dh->dh_db), ==, dh->dh_dn);
2706 ASSERT3U(dh->dh_db->db_blkid, ==, dh->dh_blkid);
2707 ASSERT3U(dh->dh_db->db_level, ==, dh->dh_level);
2708 *(dh->dh_dbp) = dh->dh_db;
2714 * The following code preserves the recursive function dbuf_hold_impl()
2715 * but moves the local variables AND function arguments to the heap to
2716 * minimize the stack frame size. Enough space is initially allocated
2717 * on the stack for 20 levels of recursion.
2720 dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid,
2721 boolean_t fail_sparse, boolean_t fail_uncached,
2722 void *tag, dmu_buf_impl_t **dbp)
2724 struct dbuf_hold_impl_data *dh;
2727 dh = kmem_alloc(sizeof (struct dbuf_hold_impl_data) *
2728 DBUF_HOLD_IMPL_MAX_DEPTH, KM_SLEEP);
2729 __dbuf_hold_impl_init(dh, dn, level, blkid, fail_sparse,
2730 fail_uncached, tag, dbp, 0);
2732 error = __dbuf_hold_impl(dh);
2734 kmem_free(dh, sizeof (struct dbuf_hold_impl_data) *
2735 DBUF_HOLD_IMPL_MAX_DEPTH);
2741 __dbuf_hold_impl_init(struct dbuf_hold_impl_data *dh,
2742 dnode_t *dn, uint8_t level, uint64_t blkid,
2743 boolean_t fail_sparse, boolean_t fail_uncached,
2744 void *tag, dmu_buf_impl_t **dbp, int depth)
2747 dh->dh_level = level;
2748 dh->dh_blkid = blkid;
2750 dh->dh_fail_sparse = fail_sparse;
2751 dh->dh_fail_uncached = fail_uncached;
2757 dh->dh_parent = NULL;
2763 dh->dh_depth = depth;
2767 dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag)
2769 return (dbuf_hold_level(dn, 0, blkid, tag));
2773 dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag)
2776 int err = dbuf_hold_impl(dn, level, blkid, FALSE, FALSE, tag, &db);
2777 return (err ? NULL : db);
2781 dbuf_create_bonus(dnode_t *dn)
2783 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
2785 ASSERT(dn->dn_bonus == NULL);
2786 dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL);
2790 dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx)
2792 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2795 if (db->db_blkid != DMU_SPILL_BLKID)
2796 return (SET_ERROR(ENOTSUP));
2798 blksz = SPA_MINBLOCKSIZE;
2799 ASSERT3U(blksz, <=, spa_maxblocksize(dmu_objset_spa(db->db_objset)));
2800 blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE);
2804 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2805 dbuf_new_size(db, blksz, tx);
2806 rw_exit(&dn->dn_struct_rwlock);
2813 dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx)
2815 dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx);
2818 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2820 dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
2822 int64_t holds = refcount_add(&db->db_holds, tag);
2823 VERIFY3S(holds, >, 1);
2826 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
2828 dbuf_try_add_ref(dmu_buf_t *db_fake, objset_t *os, uint64_t obj, uint64_t blkid,
2831 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2832 dmu_buf_impl_t *found_db;
2833 boolean_t result = B_FALSE;
2835 if (blkid == DMU_BONUS_BLKID)
2836 found_db = dbuf_find_bonus(os, obj);
2838 found_db = dbuf_find(os, obj, 0, blkid);
2840 if (found_db != NULL) {
2841 if (db == found_db && dbuf_refcount(db) > db->db_dirtycnt) {
2842 (void) refcount_add(&db->db_holds, tag);
2845 mutex_exit(&found_db->db_mtx);
2851 * If you call dbuf_rele() you had better not be referencing the dnode handle
2852 * unless you have some other direct or indirect hold on the dnode. (An indirect
2853 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2854 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2855 * dnode's parent dbuf evicting its dnode handles.
2858 dbuf_rele(dmu_buf_impl_t *db, void *tag)
2860 mutex_enter(&db->db_mtx);
2861 dbuf_rele_and_unlock(db, tag);
2865 dmu_buf_rele(dmu_buf_t *db, void *tag)
2867 dbuf_rele((dmu_buf_impl_t *)db, tag);
2871 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2872 * db_dirtycnt and db_holds to be updated atomically.
2875 dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag)
2879 ASSERT(MUTEX_HELD(&db->db_mtx));
2883 * Remove the reference to the dbuf before removing its hold on the
2884 * dnode so we can guarantee in dnode_move() that a referenced bonus
2885 * buffer has a corresponding dnode hold.
2887 holds = refcount_remove(&db->db_holds, tag);
2891 * We can't freeze indirects if there is a possibility that they
2892 * may be modified in the current syncing context.
2894 if (db->db_buf != NULL &&
2895 holds == (db->db_level == 0 ? db->db_dirtycnt : 0)) {
2896 arc_buf_freeze(db->db_buf);
2899 if (holds == db->db_dirtycnt &&
2900 db->db_level == 0 && db->db_user_immediate_evict)
2901 dbuf_evict_user(db);
2904 if (db->db_blkid == DMU_BONUS_BLKID) {
2906 boolean_t evict_dbuf = db->db_pending_evict;
2909 * If the dnode moves here, we cannot cross this
2910 * barrier until the move completes.
2915 atomic_dec_32(&dn->dn_dbufs_count);
2918 * Decrementing the dbuf count means that the bonus
2919 * buffer's dnode hold is no longer discounted in
2920 * dnode_move(). The dnode cannot move until after
2921 * the dnode_rele() below.
2926 * Do not reference db after its lock is dropped.
2927 * Another thread may evict it.
2929 mutex_exit(&db->db_mtx);
2932 dnode_evict_bonus(dn);
2935 } else if (db->db_buf == NULL) {
2937 * This is a special case: we never associated this
2938 * dbuf with any data allocated from the ARC.
2940 ASSERT(db->db_state == DB_UNCACHED ||
2941 db->db_state == DB_NOFILL);
2943 } else if (arc_released(db->db_buf)) {
2945 * This dbuf has anonymous data associated with it.
2949 boolean_t do_arc_evict = B_FALSE;
2951 spa_t *spa = dmu_objset_spa(db->db_objset);
2953 if (!DBUF_IS_CACHEABLE(db) &&
2954 db->db_blkptr != NULL &&
2955 !BP_IS_HOLE(db->db_blkptr) &&
2956 !BP_IS_EMBEDDED(db->db_blkptr)) {
2957 do_arc_evict = B_TRUE;
2958 bp = *db->db_blkptr;
2961 if (!DBUF_IS_CACHEABLE(db) ||
2962 db->db_pending_evict) {
2964 } else if (!multilist_link_active(&db->db_cache_link)) {
2965 multilist_insert(dbuf_cache, db);
2966 (void) refcount_add_many(&dbuf_cache_size,
2967 db->db.db_size, db);
2968 mutex_exit(&db->db_mtx);
2970 dbuf_evict_notify();
2974 arc_freed(spa, &bp);
2977 mutex_exit(&db->db_mtx);
2982 #pragma weak dmu_buf_refcount = dbuf_refcount
2984 dbuf_refcount(dmu_buf_impl_t *db)
2986 return (refcount_count(&db->db_holds));
2990 dmu_buf_replace_user(dmu_buf_t *db_fake, dmu_buf_user_t *old_user,
2991 dmu_buf_user_t *new_user)
2993 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2995 mutex_enter(&db->db_mtx);
2996 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2997 if (db->db_user == old_user)
2998 db->db_user = new_user;
3000 old_user = db->db_user;
3001 dbuf_verify_user(db, DBVU_NOT_EVICTING);
3002 mutex_exit(&db->db_mtx);
3008 dmu_buf_set_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
3010 return (dmu_buf_replace_user(db_fake, NULL, user));
3014 dmu_buf_set_user_ie(dmu_buf_t *db_fake, dmu_buf_user_t *user)
3016 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
3018 db->db_user_immediate_evict = TRUE;
3019 return (dmu_buf_set_user(db_fake, user));
3023 dmu_buf_remove_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
3025 return (dmu_buf_replace_user(db_fake, user, NULL));
3029 dmu_buf_get_user(dmu_buf_t *db_fake)
3031 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
3033 dbuf_verify_user(db, DBVU_NOT_EVICTING);
3034 return (db->db_user);
3038 dmu_buf_user_evict_wait()
3040 taskq_wait(dbu_evict_taskq);
3044 dmu_buf_get_blkptr(dmu_buf_t *db)
3046 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
3047 return (dbi->db_blkptr);
3051 dmu_buf_get_objset(dmu_buf_t *db)
3053 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
3054 return (dbi->db_objset);
3058 dmu_buf_dnode_enter(dmu_buf_t *db)
3060 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
3061 DB_DNODE_ENTER(dbi);
3062 return (DB_DNODE(dbi));
3066 dmu_buf_dnode_exit(dmu_buf_t *db)
3068 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
3073 dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db)
3075 /* ASSERT(dmu_tx_is_syncing(tx) */
3076 ASSERT(MUTEX_HELD(&db->db_mtx));
3078 if (db->db_blkptr != NULL)
3081 if (db->db_blkid == DMU_SPILL_BLKID) {
3082 db->db_blkptr = DN_SPILL_BLKPTR(dn->dn_phys);
3083 BP_ZERO(db->db_blkptr);
3086 if (db->db_level == dn->dn_phys->dn_nlevels-1) {
3088 * This buffer was allocated at a time when there was
3089 * no available blkptrs from the dnode, or it was
3090 * inappropriate to hook it in (i.e., nlevels mis-match).
3092 ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr);
3093 ASSERT(db->db_parent == NULL);
3094 db->db_parent = dn->dn_dbuf;
3095 db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
3098 dmu_buf_impl_t *parent = db->db_parent;
3099 int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
3101 ASSERT(dn->dn_phys->dn_nlevels > 1);
3102 if (parent == NULL) {
3103 mutex_exit(&db->db_mtx);
3104 rw_enter(&dn->dn_struct_rwlock, RW_READER);
3105 parent = dbuf_hold_level(dn, db->db_level + 1,
3106 db->db_blkid >> epbs, db);
3107 rw_exit(&dn->dn_struct_rwlock);
3108 mutex_enter(&db->db_mtx);
3109 db->db_parent = parent;
3111 db->db_blkptr = (blkptr_t *)parent->db.db_data +
3112 (db->db_blkid & ((1ULL << epbs) - 1));
3118 * dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
3119 * is critical the we not allow the compiler to inline this function in to
3120 * dbuf_sync_list() thereby drastically bloating the stack usage.
3122 noinline static void
3123 dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
3125 dmu_buf_impl_t *db = dr->dr_dbuf;
3129 ASSERT(dmu_tx_is_syncing(tx));
3131 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
3133 mutex_enter(&db->db_mtx);
3135 ASSERT(db->db_level > 0);
3138 /* Read the block if it hasn't been read yet. */
3139 if (db->db_buf == NULL) {
3140 mutex_exit(&db->db_mtx);
3141 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
3142 mutex_enter(&db->db_mtx);
3144 ASSERT3U(db->db_state, ==, DB_CACHED);
3145 ASSERT(db->db_buf != NULL);
3149 /* Indirect block size must match what the dnode thinks it is. */
3150 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
3151 dbuf_check_blkptr(dn, db);
3154 /* Provide the pending dirty record to child dbufs */
3155 db->db_data_pending = dr;
3157 mutex_exit(&db->db_mtx);
3158 dbuf_write(dr, db->db_buf, tx);
3161 mutex_enter(&dr->dt.di.dr_mtx);
3162 dbuf_sync_list(&dr->dt.di.dr_children, db->db_level - 1, tx);
3163 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
3164 mutex_exit(&dr->dt.di.dr_mtx);
3169 * dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
3170 * critical the we not allow the compiler to inline this function in to
3171 * dbuf_sync_list() thereby drastically bloating the stack usage.
3173 noinline static void
3174 dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
3176 arc_buf_t **datap = &dr->dt.dl.dr_data;
3177 dmu_buf_impl_t *db = dr->dr_dbuf;
3180 uint64_t txg = tx->tx_txg;
3182 ASSERT(dmu_tx_is_syncing(tx));
3184 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
3186 mutex_enter(&db->db_mtx);
3188 * To be synced, we must be dirtied. But we
3189 * might have been freed after the dirty.
3191 if (db->db_state == DB_UNCACHED) {
3192 /* This buffer has been freed since it was dirtied */
3193 ASSERT(db->db.db_data == NULL);
3194 } else if (db->db_state == DB_FILL) {
3195 /* This buffer was freed and is now being re-filled */
3196 ASSERT(db->db.db_data != dr->dt.dl.dr_data);
3198 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL);
3205 if (db->db_blkid == DMU_SPILL_BLKID) {
3206 mutex_enter(&dn->dn_mtx);
3207 if (!(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) {
3209 * In the previous transaction group, the bonus buffer
3210 * was entirely used to store the attributes for the
3211 * dnode which overrode the dn_spill field. However,
3212 * when adding more attributes to the file a spill
3213 * block was required to hold the extra attributes.
3215 * Make sure to clear the garbage left in the dn_spill
3216 * field from the previous attributes in the bonus
3217 * buffer. Otherwise, after writing out the spill
3218 * block to the new allocated dva, it will free
3219 * the old block pointed to by the invalid dn_spill.
3221 db->db_blkptr = NULL;
3223 dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR;
3224 mutex_exit(&dn->dn_mtx);
3228 * If this is a bonus buffer, simply copy the bonus data into the
3229 * dnode. It will be written out when the dnode is synced (and it
3230 * will be synced, since it must have been dirty for dbuf_sync to
3233 if (db->db_blkid == DMU_BONUS_BLKID) {
3234 dbuf_dirty_record_t **drp;
3236 ASSERT(*datap != NULL);
3237 ASSERT0(db->db_level);
3238 ASSERT3U(dn->dn_phys->dn_bonuslen, <=,
3239 DN_SLOTS_TO_BONUSLEN(dn->dn_phys->dn_extra_slots + 1));
3240 bcopy(*datap, DN_BONUS(dn->dn_phys), dn->dn_phys->dn_bonuslen);
3243 if (*datap != db->db.db_data) {
3244 int slots = DB_DNODE(db)->dn_num_slots;
3245 int bonuslen = DN_SLOTS_TO_BONUSLEN(slots);
3246 kmem_free(*datap, bonuslen);
3247 arc_space_return(bonuslen, ARC_SPACE_BONUS);
3249 db->db_data_pending = NULL;
3250 drp = &db->db_last_dirty;
3252 drp = &(*drp)->dr_next;
3253 ASSERT(dr->dr_next == NULL);
3254 ASSERT(dr->dr_dbuf == db);
3256 if (dr->dr_dbuf->db_level != 0) {
3257 mutex_destroy(&dr->dt.di.dr_mtx);
3258 list_destroy(&dr->dt.di.dr_children);
3260 kmem_free(dr, sizeof (dbuf_dirty_record_t));
3261 ASSERT(db->db_dirtycnt > 0);
3262 db->db_dirtycnt -= 1;
3263 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
3270 * This function may have dropped the db_mtx lock allowing a dmu_sync
3271 * operation to sneak in. As a result, we need to ensure that we
3272 * don't check the dr_override_state until we have returned from
3273 * dbuf_check_blkptr.
3275 dbuf_check_blkptr(dn, db);
3278 * If this buffer is in the middle of an immediate write,
3279 * wait for the synchronous IO to complete.
3281 while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
3282 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
3283 cv_wait(&db->db_changed, &db->db_mtx);
3284 ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN);
3287 if (db->db_state != DB_NOFILL &&
3288 dn->dn_object != DMU_META_DNODE_OBJECT &&
3289 refcount_count(&db->db_holds) > 1 &&
3290 dr->dt.dl.dr_override_state != DR_OVERRIDDEN &&
3291 *datap == db->db_buf) {
3293 * If this buffer is currently "in use" (i.e., there
3294 * are active holds and db_data still references it),
3295 * then make a copy before we start the write so that
3296 * any modifications from the open txg will not leak
3299 * NOTE: this copy does not need to be made for
3300 * objects only modified in the syncing context (e.g.
3301 * DNONE_DNODE blocks).
3303 int psize = arc_buf_size(*datap);
3304 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
3305 enum zio_compress compress_type = arc_get_compression(*datap);
3307 if (compress_type == ZIO_COMPRESS_OFF) {
3308 *datap = arc_alloc_buf(os->os_spa, db, type, psize);
3310 int lsize = arc_buf_lsize(*datap);
3311 ASSERT3U(type, ==, ARC_BUFC_DATA);
3312 *datap = arc_alloc_compressed_buf(os->os_spa, db,
3313 psize, lsize, compress_type);
3315 bcopy(db->db.db_data, (*datap)->b_data, psize);
3317 db->db_data_pending = dr;
3319 mutex_exit(&db->db_mtx);
3321 dbuf_write(dr, *datap, tx);
3323 ASSERT(!list_link_active(&dr->dr_dirty_node));
3324 if (dn->dn_object == DMU_META_DNODE_OBJECT) {
3325 list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr);
3329 * Although zio_nowait() does not "wait for an IO", it does
3330 * initiate the IO. If this is an empty write it seems plausible
3331 * that the IO could actually be completed before the nowait
3332 * returns. We need to DB_DNODE_EXIT() first in case
3333 * zio_nowait() invalidates the dbuf.
3336 zio_nowait(dr->dr_zio);
3341 dbuf_sync_list(list_t *list, int level, dmu_tx_t *tx)
3343 dbuf_dirty_record_t *dr;
3345 while ((dr = list_head(list))) {
3346 if (dr->dr_zio != NULL) {
3348 * If we find an already initialized zio then we
3349 * are processing the meta-dnode, and we have finished.
3350 * The dbufs for all dnodes are put back on the list
3351 * during processing, so that we can zio_wait()
3352 * these IOs after initiating all child IOs.
3354 ASSERT3U(dr->dr_dbuf->db.db_object, ==,
3355 DMU_META_DNODE_OBJECT);
3358 if (dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
3359 dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
3360 VERIFY3U(dr->dr_dbuf->db_level, ==, level);
3362 list_remove(list, dr);
3363 if (dr->dr_dbuf->db_level > 0)
3364 dbuf_sync_indirect(dr, tx);
3366 dbuf_sync_leaf(dr, tx);
3372 dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
3374 dmu_buf_impl_t *db = vdb;
3376 blkptr_t *bp = zio->io_bp;
3377 blkptr_t *bp_orig = &zio->io_bp_orig;
3378 spa_t *spa = zio->io_spa;
3383 ASSERT3P(db->db_blkptr, !=, NULL);
3384 ASSERT3P(&db->db_data_pending->dr_bp_copy, ==, bp);
3388 delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig);
3389 dnode_diduse_space(dn, delta - zio->io_prev_space_delta);
3390 zio->io_prev_space_delta = delta;
3392 if (bp->blk_birth != 0) {
3393 ASSERT((db->db_blkid != DMU_SPILL_BLKID &&
3394 BP_GET_TYPE(bp) == dn->dn_type) ||
3395 (db->db_blkid == DMU_SPILL_BLKID &&
3396 BP_GET_TYPE(bp) == dn->dn_bonustype) ||
3397 BP_IS_EMBEDDED(bp));
3398 ASSERT(BP_GET_LEVEL(bp) == db->db_level);
3401 mutex_enter(&db->db_mtx);
3404 if (db->db_blkid == DMU_SPILL_BLKID) {
3405 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
3406 ASSERT(!(BP_IS_HOLE(bp)) &&
3407 db->db_blkptr == DN_SPILL_BLKPTR(dn->dn_phys));
3411 if (db->db_level == 0) {
3412 mutex_enter(&dn->dn_mtx);
3413 if (db->db_blkid > dn->dn_phys->dn_maxblkid &&
3414 db->db_blkid != DMU_SPILL_BLKID)
3415 dn->dn_phys->dn_maxblkid = db->db_blkid;
3416 mutex_exit(&dn->dn_mtx);
3418 if (dn->dn_type == DMU_OT_DNODE) {
3420 while (i < db->db.db_size) {
3421 dnode_phys_t *dnp = db->db.db_data + i;
3423 i += DNODE_MIN_SIZE;
3424 if (dnp->dn_type != DMU_OT_NONE) {
3426 i += dnp->dn_extra_slots *
3431 if (BP_IS_HOLE(bp)) {
3438 blkptr_t *ibp = db->db.db_data;
3439 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
3440 for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) {
3441 if (BP_IS_HOLE(ibp))
3443 fill += BP_GET_FILL(ibp);
3448 if (!BP_IS_EMBEDDED(bp))
3449 bp->blk_fill = fill;
3451 mutex_exit(&db->db_mtx);
3453 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
3454 *db->db_blkptr = *bp;
3455 rw_exit(&dn->dn_struct_rwlock);
3460 * This function gets called just prior to running through the compression
3461 * stage of the zio pipeline. If we're an indirect block comprised of only
3462 * holes, then we want this indirect to be compressed away to a hole. In
3463 * order to do that we must zero out any information about the holes that
3464 * this indirect points to prior to before we try to compress it.
3467 dbuf_write_children_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
3469 dmu_buf_impl_t *db = vdb;
3472 unsigned int epbs, i;
3474 ASSERT3U(db->db_level, >, 0);
3477 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
3478 ASSERT3U(epbs, <, 31);
3480 /* Determine if all our children are holes */
3481 for (i = 0, bp = db->db.db_data; i < 1ULL << epbs; i++, bp++) {
3482 if (!BP_IS_HOLE(bp))
3487 * If all the children are holes, then zero them all out so that
3488 * we may get compressed away.
3490 if (i == 1ULL << epbs) {
3492 * We only found holes. Grab the rwlock to prevent
3493 * anybody from reading the blocks we're about to
3496 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
3497 bzero(db->db.db_data, db->db.db_size);
3498 rw_exit(&dn->dn_struct_rwlock);
3504 * The SPA will call this callback several times for each zio - once
3505 * for every physical child i/o (zio->io_phys_children times). This
3506 * allows the DMU to monitor the progress of each logical i/o. For example,
3507 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
3508 * block. There may be a long delay before all copies/fragments are completed,
3509 * so this callback allows us to retire dirty space gradually, as the physical
3514 dbuf_write_physdone(zio_t *zio, arc_buf_t *buf, void *arg)
3516 dmu_buf_impl_t *db = arg;
3517 objset_t *os = db->db_objset;
3518 dsl_pool_t *dp = dmu_objset_pool(os);
3519 dbuf_dirty_record_t *dr;
3522 dr = db->db_data_pending;
3523 ASSERT3U(dr->dr_txg, ==, zio->io_txg);
3526 * The callback will be called io_phys_children times. Retire one
3527 * portion of our dirty space each time we are called. Any rounding
3528 * error will be cleaned up by dsl_pool_sync()'s call to
3529 * dsl_pool_undirty_space().
3531 delta = dr->dr_accounted / zio->io_phys_children;
3532 dsl_pool_undirty_space(dp, delta, zio->io_txg);
3537 dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
3539 dmu_buf_impl_t *db = vdb;
3540 blkptr_t *bp_orig = &zio->io_bp_orig;
3541 blkptr_t *bp = db->db_blkptr;
3542 objset_t *os = db->db_objset;
3543 dmu_tx_t *tx = os->os_synctx;
3544 dbuf_dirty_record_t **drp, *dr;
3546 ASSERT0(zio->io_error);
3547 ASSERT(db->db_blkptr == bp);
3550 * For nopwrites and rewrites we ensure that the bp matches our
3551 * original and bypass all the accounting.
3553 if (zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)) {
3554 ASSERT(BP_EQUAL(bp, bp_orig));
3556 dsl_dataset_t *ds = os->os_dsl_dataset;
3557 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
3558 dsl_dataset_block_born(ds, bp, tx);
3561 mutex_enter(&db->db_mtx);
3565 drp = &db->db_last_dirty;
3566 while ((dr = *drp) != db->db_data_pending)
3568 ASSERT(!list_link_active(&dr->dr_dirty_node));
3569 ASSERT(dr->dr_dbuf == db);
3570 ASSERT(dr->dr_next == NULL);
3574 if (db->db_blkid == DMU_SPILL_BLKID) {
3579 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
3580 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
3581 db->db_blkptr == DN_SPILL_BLKPTR(dn->dn_phys));
3586 if (db->db_level == 0) {
3587 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
3588 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
3589 if (db->db_state != DB_NOFILL) {
3590 if (dr->dt.dl.dr_data != db->db_buf)
3591 arc_buf_destroy(dr->dt.dl.dr_data, db);
3598 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
3599 ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
3600 if (!BP_IS_HOLE(db->db_blkptr)) {
3601 ASSERTV(int epbs = dn->dn_phys->dn_indblkshift -
3603 ASSERT3U(db->db_blkid, <=,
3604 dn->dn_phys->dn_maxblkid >> (db->db_level * epbs));
3605 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
3609 mutex_destroy(&dr->dt.di.dr_mtx);
3610 list_destroy(&dr->dt.di.dr_children);
3612 kmem_free(dr, sizeof (dbuf_dirty_record_t));
3614 cv_broadcast(&db->db_changed);
3615 ASSERT(db->db_dirtycnt > 0);
3616 db->db_dirtycnt -= 1;
3617 db->db_data_pending = NULL;
3618 dbuf_rele_and_unlock(db, (void *)(uintptr_t)tx->tx_txg);
3622 dbuf_write_nofill_ready(zio_t *zio)
3624 dbuf_write_ready(zio, NULL, zio->io_private);
3628 dbuf_write_nofill_done(zio_t *zio)
3630 dbuf_write_done(zio, NULL, zio->io_private);
3634 dbuf_write_override_ready(zio_t *zio)
3636 dbuf_dirty_record_t *dr = zio->io_private;
3637 dmu_buf_impl_t *db = dr->dr_dbuf;
3639 dbuf_write_ready(zio, NULL, db);
3643 dbuf_write_override_done(zio_t *zio)
3645 dbuf_dirty_record_t *dr = zio->io_private;
3646 dmu_buf_impl_t *db = dr->dr_dbuf;
3647 blkptr_t *obp = &dr->dt.dl.dr_overridden_by;
3649 mutex_enter(&db->db_mtx);
3650 if (!BP_EQUAL(zio->io_bp, obp)) {
3651 if (!BP_IS_HOLE(obp))
3652 dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp);
3653 arc_release(dr->dt.dl.dr_data, db);
3655 mutex_exit(&db->db_mtx);
3657 dbuf_write_done(zio, NULL, db);
3659 if (zio->io_abd != NULL)
3660 abd_put(zio->io_abd);
3663 /* Issue I/O to commit a dirty buffer to disk. */
3665 dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx)
3667 dmu_buf_impl_t *db = dr->dr_dbuf;
3670 dmu_buf_impl_t *parent = db->db_parent;
3671 uint64_t txg = tx->tx_txg;
3672 zbookmark_phys_t zb;
3677 ASSERT(dmu_tx_is_syncing(tx));
3683 if (db->db_state != DB_NOFILL) {
3684 if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) {
3686 * Private object buffers are released here rather
3687 * than in dbuf_dirty() since they are only modified
3688 * in the syncing context and we don't want the
3689 * overhead of making multiple copies of the data.
3691 if (BP_IS_HOLE(db->db_blkptr)) {
3694 dbuf_release_bp(db);
3699 if (parent != dn->dn_dbuf) {
3700 /* Our parent is an indirect block. */
3701 /* We have a dirty parent that has been scheduled for write. */
3702 ASSERT(parent && parent->db_data_pending);
3703 /* Our parent's buffer is one level closer to the dnode. */
3704 ASSERT(db->db_level == parent->db_level-1);
3706 * We're about to modify our parent's db_data by modifying
3707 * our block pointer, so the parent must be released.
3709 ASSERT(arc_released(parent->db_buf));
3710 zio = parent->db_data_pending->dr_zio;
3712 /* Our parent is the dnode itself. */
3713 ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 &&
3714 db->db_blkid != DMU_SPILL_BLKID) ||
3715 (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0));
3716 if (db->db_blkid != DMU_SPILL_BLKID)
3717 ASSERT3P(db->db_blkptr, ==,
3718 &dn->dn_phys->dn_blkptr[db->db_blkid]);
3722 ASSERT(db->db_level == 0 || data == db->db_buf);
3723 ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
3726 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
3727 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
3728 db->db.db_object, db->db_level, db->db_blkid);
3730 if (db->db_blkid == DMU_SPILL_BLKID)
3732 wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
3734 dmu_write_policy(os, dn, db->db_level, wp_flag,
3735 (data != NULL && arc_get_compression(data) != ZIO_COMPRESS_OFF) ?
3736 arc_get_compression(data) : ZIO_COMPRESS_INHERIT, &zp);
3740 * We copy the blkptr now (rather than when we instantiate the dirty
3741 * record), because its value can change between open context and
3742 * syncing context. We do not need to hold dn_struct_rwlock to read
3743 * db_blkptr because we are in syncing context.
3745 dr->dr_bp_copy = *db->db_blkptr;
3747 if (db->db_level == 0 &&
3748 dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
3750 * The BP for this block has been provided by open context
3751 * (by dmu_sync() or dmu_buf_write_embedded()).
3753 abd_t *contents = (data != NULL) ?
3754 abd_get_from_buf(data->b_data, arc_buf_size(data)) : NULL;
3756 dr->dr_zio = zio_write(zio, os->os_spa, txg,
3757 &dr->dr_bp_copy, contents, db->db.db_size, db->db.db_size,
3758 &zp, dbuf_write_override_ready, NULL, NULL,
3759 dbuf_write_override_done,
3760 dr, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
3761 mutex_enter(&db->db_mtx);
3762 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
3763 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by,
3764 dr->dt.dl.dr_copies, dr->dt.dl.dr_nopwrite);
3765 mutex_exit(&db->db_mtx);
3766 } else if (db->db_state == DB_NOFILL) {
3767 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF ||
3768 zp.zp_checksum == ZIO_CHECKSUM_NOPARITY);
3769 dr->dr_zio = zio_write(zio, os->os_spa, txg,
3770 &dr->dr_bp_copy, NULL, db->db.db_size, db->db.db_size, &zp,
3771 dbuf_write_nofill_ready, NULL, NULL,
3772 dbuf_write_nofill_done, db,
3773 ZIO_PRIORITY_ASYNC_WRITE,
3774 ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb);
3776 arc_done_func_t *children_ready_cb = NULL;
3777 ASSERT(arc_released(data));
3780 * For indirect blocks, we want to setup the children
3781 * ready callback so that we can properly handle an indirect
3782 * block that only contains holes.
3784 if (db->db_level != 0)
3785 children_ready_cb = dbuf_write_children_ready;
3787 dr->dr_zio = arc_write(zio, os->os_spa, txg,
3788 &dr->dr_bp_copy, data, DBUF_IS_L2CACHEABLE(db),
3789 &zp, dbuf_write_ready,
3790 children_ready_cb, dbuf_write_physdone,
3791 dbuf_write_done, db, ZIO_PRIORITY_ASYNC_WRITE,
3792 ZIO_FLAG_MUSTSUCCEED, &zb);
3796 #if defined(_KERNEL) && defined(HAVE_SPL)
3797 EXPORT_SYMBOL(dbuf_find);
3798 EXPORT_SYMBOL(dbuf_is_metadata);
3799 EXPORT_SYMBOL(dbuf_destroy);
3800 EXPORT_SYMBOL(dbuf_loan_arcbuf);
3801 EXPORT_SYMBOL(dbuf_whichblock);
3802 EXPORT_SYMBOL(dbuf_read);
3803 EXPORT_SYMBOL(dbuf_unoverride);
3804 EXPORT_SYMBOL(dbuf_free_range);
3805 EXPORT_SYMBOL(dbuf_new_size);
3806 EXPORT_SYMBOL(dbuf_release_bp);
3807 EXPORT_SYMBOL(dbuf_dirty);
3808 EXPORT_SYMBOL(dmu_buf_will_dirty);
3809 EXPORT_SYMBOL(dmu_buf_will_not_fill);
3810 EXPORT_SYMBOL(dmu_buf_will_fill);
3811 EXPORT_SYMBOL(dmu_buf_fill_done);
3812 EXPORT_SYMBOL(dmu_buf_rele);
3813 EXPORT_SYMBOL(dbuf_assign_arcbuf);
3814 EXPORT_SYMBOL(dbuf_prefetch);
3815 EXPORT_SYMBOL(dbuf_hold_impl);
3816 EXPORT_SYMBOL(dbuf_hold);
3817 EXPORT_SYMBOL(dbuf_hold_level);
3818 EXPORT_SYMBOL(dbuf_create_bonus);
3819 EXPORT_SYMBOL(dbuf_spill_set_blksz);
3820 EXPORT_SYMBOL(dbuf_rm_spill);
3821 EXPORT_SYMBOL(dbuf_add_ref);
3822 EXPORT_SYMBOL(dbuf_rele);
3823 EXPORT_SYMBOL(dbuf_rele_and_unlock);
3824 EXPORT_SYMBOL(dbuf_refcount);
3825 EXPORT_SYMBOL(dbuf_sync_list);
3826 EXPORT_SYMBOL(dmu_buf_set_user);
3827 EXPORT_SYMBOL(dmu_buf_set_user_ie);
3828 EXPORT_SYMBOL(dmu_buf_get_user);
3829 EXPORT_SYMBOL(dmu_buf_get_blkptr);
3832 module_param(dbuf_cache_max_bytes, ulong, 0644);
3833 MODULE_PARM_DESC(dbuf_cache_max_bytes,
3834 "Maximum size in bytes of the dbuf cache.");
3836 module_param(dbuf_cache_hiwater_pct, uint, 0644);
3837 MODULE_PARM_DESC(dbuf_cache_hiwater_pct,
3838 "Percentage over dbuf_cache_max_bytes when dbufs must be evicted "
3841 module_param(dbuf_cache_lowater_pct, uint, 0644);
3842 MODULE_PARM_DESC(dbuf_cache_lowater_pct,
3843 "Percentage below dbuf_cache_max_bytes when the evict thread stops "
3846 module_param(dbuf_cache_max_shift, int, 0644);
3847 MODULE_PARM_DESC(dbuf_cache_max_shift,
3848 "Cap the size of the dbuf cache to a log2 fraction of arc size.");