Merge commit '7e3488dc6cdcb0c04e1ce167a1a3bfef83b5f2e0'
[unleashed.git] / kernel / fs / zfs / dbuf.c
blob4fe6391658696996bf9d2df7fc5bfdb218ec1f03
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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, 2018 by Delphix. All rights reserved.
25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28 * Copyright (c) 2014 Integros [integros.com]
31 #include <sys/zfs_context.h>
32 #include <sys/dmu.h>
33 #include <sys/dmu_send.h>
34 #include <sys/dmu_impl.h>
35 #include <sys/dbuf.h>
36 #include <sys/dmu_objset.h>
37 #include <sys/dsl_dataset.h>
38 #include <sys/dsl_dir.h>
39 #include <sys/dmu_tx.h>
40 #include <sys/spa.h>
41 #include <sys/zio.h>
42 #include <sys/dmu_zfetch.h>
43 #include <sys/sa.h>
44 #include <sys/sa_impl.h>
45 #include <sys/zfeature.h>
46 #include <sys/blkptr.h>
47 #include <sys/range_tree.h>
48 #include <sys/callb.h>
49 #include <sys/abd.h>
50 #include <sys/vdev.h>
51 #include <sys/cityhash.h>
52 #include <sys/spa_impl.h>
54 static boolean_t dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
55 static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx);
57 extern inline void dmu_buf_init_user(dmu_buf_user_t *dbu,
58 dmu_buf_evict_func_t *evict_func_sync,
59 dmu_buf_evict_func_t *evict_func_async,
60 dmu_buf_t **clear_on_evict_dbufp);
63 * Global data structures and functions for the dbuf cache.
65 static kmem_cache_t *dbuf_kmem_cache;
66 static taskq_t *dbu_evict_taskq;
68 static kthread_t *dbuf_cache_evict_thread;
69 static kmutex_t dbuf_evict_lock;
70 static kcondvar_t dbuf_evict_cv;
71 static boolean_t dbuf_evict_thread_exit;
74 * There are two dbuf caches; each dbuf can only be in one of them at a time.
76 * 1. Cache of metadata dbufs, to help make read-heavy administrative commands
77 * from /sbin/zfs run faster. The "metadata cache" specifically stores dbufs
78 * that represent the metadata that describes filesystems/snapshots/
79 * bookmarks/properties/etc. We only evict from this cache when we export a
80 * pool, to short-circuit as much I/O as possible for all administrative
81 * commands that need the metadata. There is no eviction policy for this
82 * cache, because we try to only include types in it which would occupy a
83 * very small amount of space per object but create a large impact on the
84 * performance of these commands. Instead, after it reaches a maximum size
85 * (which should only happen on very small memory systems with a very large
86 * number of filesystem objects), we stop taking new dbufs into the
87 * metadata cache, instead putting them in the normal dbuf cache.
89 * 2. LRU cache of dbufs. The "dbuf cache" maintains a list of dbufs that
90 * are not currently held but have been recently released. These dbufs
91 * are not eligible for arc eviction until they are aged out of the cache.
92 * Dbufs that are aged out of the cache will be immediately destroyed and
93 * become eligible for arc eviction.
95 * Dbufs are added to these caches once the last hold is released. If a dbuf is
96 * later accessed and still exists in the dbuf cache, then it will be removed
97 * from the cache and later re-added to the head of the cache.
99 * If a given dbuf meets the requirements for the metadata cache, it will go
100 * there, otherwise it will be considered for the generic LRU dbuf cache. The
101 * caches and the refcounts tracking their sizes are stored in an array indexed
102 * by those caches' matching enum values (from dbuf_cached_state_t).
104 typedef struct dbuf_cache {
105 multilist_t *cache;
106 refcount_t size;
107 } dbuf_cache_t;
108 dbuf_cache_t dbuf_caches[DB_CACHE_MAX];
110 /* Size limits for the caches */
111 uint64_t dbuf_cache_max_bytes = 0;
112 uint64_t dbuf_metadata_cache_max_bytes = 0;
113 /* Set the default sizes of the caches to log2 fraction of arc size */
114 int dbuf_cache_shift = 5;
115 int dbuf_metadata_cache_shift = 6;
118 * For diagnostic purposes, this is incremented whenever we can't add
119 * something to the metadata cache because it's full, and instead put
120 * the data in the regular dbuf cache.
122 uint64_t dbuf_metadata_cache_overflow;
125 * The LRU dbuf cache uses a three-stage eviction policy:
126 * - A low water marker designates when the dbuf eviction thread
127 * should stop evicting from the dbuf cache.
128 * - When we reach the maximum size (aka mid water mark), we
129 * signal the eviction thread to run.
130 * - The high water mark indicates when the eviction thread
131 * is unable to keep up with the incoming load and eviction must
132 * happen in the context of the calling thread.
134 * The dbuf cache:
135 * (max size)
136 * low water mid water hi water
137 * +----------------------------------------+----------+----------+
138 * | | | |
139 * | | | |
140 * | | | |
141 * | | | |
142 * +----------------------------------------+----------+----------+
143 * stop signal evict
144 * evicting eviction directly
145 * thread
147 * The high and low water marks indicate the operating range for the eviction
148 * thread. The low water mark is, by default, 90% of the total size of the
149 * cache and the high water mark is at 110% (both of these percentages can be
150 * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct,
151 * respectively). The eviction thread will try to ensure that the cache remains
152 * within this range by waking up every second and checking if the cache is
153 * above the low water mark. The thread can also be woken up by callers adding
154 * elements into the cache if the cache is larger than the mid water (i.e max
155 * cache size). Once the eviction thread is woken up and eviction is required,
156 * it will continue evicting buffers until it's able to reduce the cache size
157 * to the low water mark. If the cache size continues to grow and hits the high
158 * water mark, then callers adding elments to the cache will begin to evict
159 * directly from the cache until the cache is no longer above the high water
160 * mark.
164 * The percentage above and below the maximum cache size.
166 uint_t dbuf_cache_hiwater_pct = 10;
167 uint_t dbuf_cache_lowater_pct = 10;
169 /* ARGSUSED */
170 static int
171 dbuf_cons(void *vdb, void *unused, int kmflag)
173 dmu_buf_impl_t *db = vdb;
174 bzero(db, sizeof (dmu_buf_impl_t));
176 mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL);
177 cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL);
178 multilist_link_init(&db->db_cache_link);
179 refcount_create(&db->db_holds);
181 return (0);
184 /* ARGSUSED */
185 static void
186 dbuf_dest(void *vdb, void *unused)
188 dmu_buf_impl_t *db = vdb;
189 mutex_destroy(&db->db_mtx);
190 cv_destroy(&db->db_changed);
191 ASSERT(!multilist_link_active(&db->db_cache_link));
192 refcount_destroy(&db->db_holds);
196 * dbuf hash table routines
198 static dbuf_hash_table_t dbuf_hash_table;
200 static uint64_t dbuf_hash_count;
203 * We use Cityhash for this. It's fast, and has good hash properties without
204 * requiring any large static buffers.
206 static uint64_t
207 dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid)
209 return (cityhash4((uintptr_t)os, obj, (uint64_t)lvl, blkid));
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))
218 dmu_buf_impl_t *
219 dbuf_find(objset_t *os, uint64_t obj, uint8_t level, uint64_t blkid)
221 dbuf_hash_table_t *h = &dbuf_hash_table;
222 uint64_t hv = dbuf_hash(os, obj, level, blkid);
223 uint64_t idx = hv & h->hash_table_mask;
224 dmu_buf_impl_t *db;
226 mutex_enter(DBUF_HASH_MUTEX(h, idx));
227 for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) {
228 if (DBUF_EQUAL(db, os, obj, level, blkid)) {
229 mutex_enter(&db->db_mtx);
230 if (db->db_state != DB_EVICTING) {
231 mutex_exit(DBUF_HASH_MUTEX(h, idx));
232 return (db);
234 mutex_exit(&db->db_mtx);
237 mutex_exit(DBUF_HASH_MUTEX(h, idx));
238 return (NULL);
241 static dmu_buf_impl_t *
242 dbuf_find_bonus(objset_t *os, uint64_t object)
244 dnode_t *dn;
245 dmu_buf_impl_t *db = NULL;
247 if (dnode_hold(os, object, FTAG, &dn) == 0) {
248 rw_enter(&dn->dn_struct_rwlock, RW_READER);
249 if (dn->dn_bonus != NULL) {
250 db = dn->dn_bonus;
251 mutex_enter(&db->db_mtx);
253 rw_exit(&dn->dn_struct_rwlock);
254 dnode_rele(dn, FTAG);
256 return (db);
260 * Insert an entry into the hash table. If there is already an element
261 * equal to elem in the hash table, then the already existing element
262 * will be returned and the new element will not be inserted.
263 * Otherwise returns NULL.
265 static dmu_buf_impl_t *
266 dbuf_hash_insert(dmu_buf_impl_t *db)
268 dbuf_hash_table_t *h = &dbuf_hash_table;
269 objset_t *os = db->db_objset;
270 uint64_t obj = db->db.db_object;
271 int level = db->db_level;
272 uint64_t blkid = db->db_blkid;
273 uint64_t hv = dbuf_hash(os, obj, level, blkid);
274 uint64_t idx = hv & h->hash_table_mask;
275 dmu_buf_impl_t *dbf;
277 mutex_enter(DBUF_HASH_MUTEX(h, idx));
278 for (dbf = h->hash_table[idx]; dbf != NULL; dbf = dbf->db_hash_next) {
279 if (DBUF_EQUAL(dbf, os, obj, level, blkid)) {
280 mutex_enter(&dbf->db_mtx);
281 if (dbf->db_state != DB_EVICTING) {
282 mutex_exit(DBUF_HASH_MUTEX(h, idx));
283 return (dbf);
285 mutex_exit(&dbf->db_mtx);
289 mutex_enter(&db->db_mtx);
290 db->db_hash_next = h->hash_table[idx];
291 h->hash_table[idx] = db;
292 mutex_exit(DBUF_HASH_MUTEX(h, idx));
293 atomic_inc_64(&dbuf_hash_count);
295 return (NULL);
299 * Remove an entry from the hash table. It must be in the EVICTING state.
301 static void
302 dbuf_hash_remove(dmu_buf_impl_t *db)
304 dbuf_hash_table_t *h = &dbuf_hash_table;
305 uint64_t hv = dbuf_hash(db->db_objset, db->db.db_object,
306 db->db_level, db->db_blkid);
307 uint64_t idx = hv & h->hash_table_mask;
308 dmu_buf_impl_t *dbf, **dbp;
311 * We musn't hold db_mtx to maintain lock ordering:
312 * DBUF_HASH_MUTEX > db_mtx.
314 ASSERT(refcount_is_zero(&db->db_holds));
315 ASSERT(db->db_state == DB_EVICTING);
316 ASSERT(!MUTEX_HELD(&db->db_mtx));
318 mutex_enter(DBUF_HASH_MUTEX(h, idx));
319 dbp = &h->hash_table[idx];
320 while ((dbf = *dbp) != db) {
321 dbp = &dbf->db_hash_next;
322 ASSERT(dbf != NULL);
324 *dbp = db->db_hash_next;
325 db->db_hash_next = NULL;
326 mutex_exit(DBUF_HASH_MUTEX(h, idx));
327 atomic_dec_64(&dbuf_hash_count);
330 typedef enum {
331 DBVU_EVICTING,
332 DBVU_NOT_EVICTING
333 } dbvu_verify_type_t;
335 static void
336 dbuf_verify_user(dmu_buf_impl_t *db, dbvu_verify_type_t verify_type)
338 #ifdef ZFS_DEBUG
339 int64_t holds;
341 if (db->db_user == NULL)
342 return;
344 /* Only data blocks support the attachment of user data. */
345 ASSERT(db->db_level == 0);
347 /* Clients must resolve a dbuf before attaching user data. */
348 ASSERT(db->db.db_data != NULL);
349 ASSERT3U(db->db_state, ==, DB_CACHED);
351 holds = refcount_count(&db->db_holds);
352 if (verify_type == DBVU_EVICTING) {
354 * Immediate eviction occurs when holds == dirtycnt.
355 * For normal eviction buffers, holds is zero on
356 * eviction, except when dbuf_fix_old_data() calls
357 * dbuf_clear_data(). However, the hold count can grow
358 * during eviction even though db_mtx is held (see
359 * dmu_bonus_hold() for an example), so we can only
360 * test the generic invariant that holds >= dirtycnt.
362 ASSERT3U(holds, >=, db->db_dirtycnt);
363 } else {
364 if (db->db_user_immediate_evict == TRUE)
365 ASSERT3U(holds, >=, db->db_dirtycnt);
366 else
367 ASSERT3U(holds, >, 0);
369 #endif
372 static void
373 dbuf_evict_user(dmu_buf_impl_t *db)
375 dmu_buf_user_t *dbu = db->db_user;
377 ASSERT(MUTEX_HELD(&db->db_mtx));
379 if (dbu == NULL)
380 return;
382 dbuf_verify_user(db, DBVU_EVICTING);
383 db->db_user = NULL;
385 #ifdef ZFS_DEBUG
386 if (dbu->dbu_clear_on_evict_dbufp != NULL)
387 *dbu->dbu_clear_on_evict_dbufp = NULL;
388 #endif
391 * There are two eviction callbacks - one that we call synchronously
392 * and one that we invoke via a taskq. The async one is useful for
393 * avoiding lock order reversals and limiting stack depth.
395 * Note that if we have a sync callback but no async callback,
396 * it's likely that the sync callback will free the structure
397 * containing the dbu. In that case we need to take care to not
398 * dereference dbu after calling the sync evict func.
400 boolean_t has_async = (dbu->dbu_evict_func_async != NULL);
402 if (dbu->dbu_evict_func_sync != NULL)
403 dbu->dbu_evict_func_sync(dbu);
405 if (has_async) {
406 taskq_dispatch_ent(dbu_evict_taskq, dbu->dbu_evict_func_async,
407 dbu, 0, &dbu->dbu_tqent);
411 boolean_t
412 dbuf_is_metadata(dmu_buf_impl_t *db)
414 if (db->db_level > 0) {
415 return (B_TRUE);
416 } else {
417 boolean_t is_metadata;
419 DB_DNODE_ENTER(db);
420 is_metadata = DMU_OT_IS_METADATA(DB_DNODE(db)->dn_type);
421 DB_DNODE_EXIT(db);
423 return (is_metadata);
428 * This returns whether this dbuf should be stored in the metadata cache, which
429 * is based on whether it's from one of the dnode types that store data related
430 * to traversing dataset hierarchies.
432 static boolean_t
433 dbuf_include_in_metadata_cache(dmu_buf_impl_t *db)
435 DB_DNODE_ENTER(db);
436 dmu_object_type_t type = DB_DNODE(db)->dn_type;
437 DB_DNODE_EXIT(db);
439 /* Check if this dbuf is one of the types we care about */
440 if (DMU_OT_IS_METADATA_CACHED(type)) {
441 /* If we hit this, then we set something up wrong in dmu_ot */
442 ASSERT(DMU_OT_IS_METADATA(type));
445 * Sanity check for small-memory systems: don't allocate too
446 * much memory for this purpose.
448 if (refcount_count(&dbuf_caches[DB_DBUF_METADATA_CACHE].size) >
449 dbuf_metadata_cache_max_bytes) {
450 dbuf_metadata_cache_overflow++;
451 DTRACE_PROBE1(dbuf__metadata__cache__overflow,
452 dmu_buf_impl_t *, db);
453 return (B_FALSE);
456 return (B_TRUE);
459 return (B_FALSE);
463 * This function *must* return indices evenly distributed between all
464 * sublists of the multilist. This is needed due to how the dbuf eviction
465 * code is laid out; dbuf_evict_thread() assumes dbufs are evenly
466 * distributed between all sublists and uses this assumption when
467 * deciding which sublist to evict from and how much to evict from it.
469 unsigned int
470 dbuf_cache_multilist_index_func(multilist_t *ml, void *obj)
472 dmu_buf_impl_t *db = obj;
475 * The assumption here, is the hash value for a given
476 * dmu_buf_impl_t will remain constant throughout it's lifetime
477 * (i.e. it's objset, object, level and blkid fields don't change).
478 * Thus, we don't need to store the dbuf's sublist index
479 * on insertion, as this index can be recalculated on removal.
481 * Also, the low order bits of the hash value are thought to be
482 * distributed evenly. Otherwise, in the case that the multilist
483 * has a power of two number of sublists, each sublists' usage
484 * would not be evenly distributed.
486 return (dbuf_hash(db->db_objset, db->db.db_object,
487 db->db_level, db->db_blkid) %
488 multilist_get_num_sublists(ml));
491 static inline boolean_t
492 dbuf_cache_above_hiwater(void)
494 uint64_t dbuf_cache_hiwater_bytes =
495 (dbuf_cache_max_bytes * dbuf_cache_hiwater_pct) / 100;
497 return (refcount_count(&dbuf_caches[DB_DBUF_CACHE].size) >
498 dbuf_cache_max_bytes + dbuf_cache_hiwater_bytes);
501 static inline boolean_t
502 dbuf_cache_above_lowater(void)
504 uint64_t dbuf_cache_lowater_bytes =
505 (dbuf_cache_max_bytes * dbuf_cache_lowater_pct) / 100;
507 return (refcount_count(&dbuf_caches[DB_DBUF_CACHE].size) >
508 dbuf_cache_max_bytes - dbuf_cache_lowater_bytes);
512 * Evict the oldest eligible dbuf from the dbuf cache.
514 static void
515 dbuf_evict_one(void)
517 int idx = multilist_get_random_index(dbuf_caches[DB_DBUF_CACHE].cache);
518 multilist_sublist_t *mls = multilist_sublist_lock(
519 dbuf_caches[DB_DBUF_CACHE].cache, idx);
521 ASSERT(!MUTEX_HELD(&dbuf_evict_lock));
523 dmu_buf_impl_t *db = multilist_sublist_tail(mls);
524 while (db != NULL && mutex_tryenter(&db->db_mtx) == 0) {
525 db = multilist_sublist_prev(mls, db);
528 DTRACE_PROBE2(dbuf__evict__one, dmu_buf_impl_t *, db,
529 multilist_sublist_t *, mls);
531 if (db != NULL) {
532 multilist_sublist_remove(mls, db);
533 multilist_sublist_unlock(mls);
534 (void) refcount_remove_many(&dbuf_caches[DB_DBUF_CACHE].size,
535 db->db.db_size, db);
536 ASSERT3U(db->db_caching_status, ==, DB_DBUF_CACHE);
537 db->db_caching_status = DB_NO_CACHE;
538 dbuf_destroy(db);
539 } else {
540 multilist_sublist_unlock(mls);
545 * The dbuf evict thread is responsible for aging out dbufs from the
546 * cache. Once the cache has reached it's maximum size, dbufs are removed
547 * and destroyed. The eviction thread will continue running until the size
548 * of the dbuf cache is at or below the maximum size. Once the dbuf is aged
549 * out of the cache it is destroyed and becomes eligible for arc eviction.
551 /* ARGSUSED */
552 static void
553 dbuf_evict_thread(void *unused)
555 callb_cpr_t cpr;
557 CALLB_CPR_INIT(&cpr, &dbuf_evict_lock, callb_generic_cpr, FTAG);
559 mutex_enter(&dbuf_evict_lock);
560 while (!dbuf_evict_thread_exit) {
561 while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit) {
562 CALLB_CPR_SAFE_BEGIN(&cpr);
563 (void) cv_timedwait_hires(&dbuf_evict_cv,
564 &dbuf_evict_lock, SEC2NSEC(1), MSEC2NSEC(1), 0);
565 CALLB_CPR_SAFE_END(&cpr, &dbuf_evict_lock);
567 mutex_exit(&dbuf_evict_lock);
570 * Keep evicting as long as we're above the low water mark
571 * for the cache. We do this without holding the locks to
572 * minimize lock contention.
574 while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit) {
575 dbuf_evict_one();
578 mutex_enter(&dbuf_evict_lock);
581 dbuf_evict_thread_exit = B_FALSE;
582 cv_broadcast(&dbuf_evict_cv);
583 CALLB_CPR_EXIT(&cpr); /* drops dbuf_evict_lock */
584 thread_exit();
588 * Wake up the dbuf eviction thread if the dbuf cache is at its max size.
589 * If the dbuf cache is at its high water mark, then evict a dbuf from the
590 * dbuf cache using the callers context.
592 static void
593 dbuf_evict_notify(void)
596 * We check if we should evict without holding the dbuf_evict_lock,
597 * because it's OK to occasionally make the wrong decision here,
598 * and grabbing the lock results in massive lock contention.
600 if (refcount_count(&dbuf_caches[DB_DBUF_CACHE].size) >
601 dbuf_cache_max_bytes) {
602 if (dbuf_cache_above_hiwater())
603 dbuf_evict_one();
604 cv_signal(&dbuf_evict_cv);
608 void
609 dbuf_init(void)
611 uint64_t hsize = 1ULL << 16;
612 dbuf_hash_table_t *h = &dbuf_hash_table;
613 int i;
616 * The hash table is big enough to fill all of physical memory
617 * with an average 4K block size. The table will take up
618 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
620 while (hsize * 4096 < physmem * PAGESIZE)
621 hsize <<= 1;
623 retry:
624 h->hash_table_mask = hsize - 1;
625 h->hash_table = kmem_zalloc(hsize * sizeof (void *), KM_NOSLEEP);
626 if (h->hash_table == NULL) {
627 /* XXX - we should really return an error instead of assert */
628 ASSERT(hsize > (1ULL << 10));
629 hsize >>= 1;
630 goto retry;
633 dbuf_kmem_cache = kmem_cache_create("dmu_buf_impl_t",
634 sizeof (dmu_buf_impl_t),
635 0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0);
637 for (i = 0; i < DBUF_MUTEXES; i++)
638 mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL);
641 * Setup the parameters for the dbuf caches. We set the sizes of the
642 * dbuf cache and the metadata cache to 1/32nd and 1/16th (default)
643 * of the size of the ARC, respectively. If the values are set in
644 * /etc/system and they're not greater than the size of the ARC, then
645 * we honor that value.
647 if (dbuf_cache_max_bytes == 0 ||
648 dbuf_cache_max_bytes >= arc_max_bytes()) {
649 dbuf_cache_max_bytes = arc_max_bytes() >> dbuf_cache_shift;
651 if (dbuf_metadata_cache_max_bytes == 0 ||
652 dbuf_metadata_cache_max_bytes >= arc_max_bytes()) {
653 dbuf_metadata_cache_max_bytes =
654 arc_max_bytes() >> dbuf_metadata_cache_shift;
658 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
659 * configuration is not required.
661 dbu_evict_taskq = taskq_create("dbu_evict", 1, minclsyspri, 0, 0, 0);
663 for (dbuf_cached_state_t dcs = 0; dcs < DB_CACHE_MAX; dcs++) {
664 dbuf_caches[dcs].cache =
665 multilist_create(sizeof (dmu_buf_impl_t),
666 offsetof(dmu_buf_impl_t, db_cache_link),
667 dbuf_cache_multilist_index_func);
668 refcount_create(&dbuf_caches[dcs].size);
671 dbuf_evict_thread_exit = B_FALSE;
672 mutex_init(&dbuf_evict_lock, NULL, MUTEX_DEFAULT, NULL);
673 cv_init(&dbuf_evict_cv, NULL, CV_DEFAULT, NULL);
674 dbuf_cache_evict_thread = thread_create(NULL, 0, dbuf_evict_thread,
675 NULL, 0, &p0, TS_RUN, minclsyspri);
678 void
679 dbuf_fini(void)
681 dbuf_hash_table_t *h = &dbuf_hash_table;
682 int i;
684 for (i = 0; i < DBUF_MUTEXES; i++)
685 mutex_destroy(&h->hash_mutexes[i]);
686 kmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
687 kmem_cache_destroy(dbuf_kmem_cache);
688 taskq_destroy(dbu_evict_taskq);
690 mutex_enter(&dbuf_evict_lock);
691 dbuf_evict_thread_exit = B_TRUE;
692 while (dbuf_evict_thread_exit) {
693 cv_signal(&dbuf_evict_cv);
694 cv_wait(&dbuf_evict_cv, &dbuf_evict_lock);
696 mutex_exit(&dbuf_evict_lock);
698 mutex_destroy(&dbuf_evict_lock);
699 cv_destroy(&dbuf_evict_cv);
701 for (dbuf_cached_state_t dcs = 0; dcs < DB_CACHE_MAX; dcs++) {
702 refcount_destroy(&dbuf_caches[dcs].size);
703 multilist_destroy(dbuf_caches[dcs].cache);
708 * Other stuff.
711 #ifdef ZFS_DEBUG
712 static void
713 dbuf_verify(dmu_buf_impl_t *db)
715 dnode_t *dn;
716 dbuf_dirty_record_t *dr;
718 ASSERT(MUTEX_HELD(&db->db_mtx));
720 if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY))
721 return;
723 ASSERT(db->db_objset != NULL);
724 DB_DNODE_ENTER(db);
725 dn = DB_DNODE(db);
726 if (dn == NULL) {
727 ASSERT(db->db_parent == NULL);
728 ASSERT(db->db_blkptr == NULL);
729 } else {
730 ASSERT3U(db->db.db_object, ==, dn->dn_object);
731 ASSERT3P(db->db_objset, ==, dn->dn_objset);
732 ASSERT3U(db->db_level, <, dn->dn_nlevels);
733 ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
734 db->db_blkid == DMU_SPILL_BLKID ||
735 !avl_is_empty(&dn->dn_dbufs));
737 if (db->db_blkid == DMU_BONUS_BLKID) {
738 ASSERT(dn != NULL);
739 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
740 ASSERT3U(db->db.db_offset, ==, DMU_BONUS_BLKID);
741 } else if (db->db_blkid == DMU_SPILL_BLKID) {
742 ASSERT(dn != NULL);
743 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
744 ASSERT0(db->db.db_offset);
745 } else {
746 ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size);
749 for (dr = db->db_data_pending; dr != NULL; dr = dr->dr_next)
750 ASSERT(dr->dr_dbuf == db);
752 for (dr = db->db_last_dirty; dr != NULL; dr = dr->dr_next)
753 ASSERT(dr->dr_dbuf == db);
756 * We can't assert that db_size matches dn_datablksz because it
757 * can be momentarily different when another thread is doing
758 * dnode_set_blksz().
760 if (db->db_level == 0 && db->db.db_object == DMU_META_DNODE_OBJECT) {
761 dr = db->db_data_pending;
763 * It should only be modified in syncing context, so
764 * make sure we only have one copy of the data.
766 ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf);
769 /* verify db->db_blkptr */
770 if (db->db_blkptr) {
771 if (db->db_parent == dn->dn_dbuf) {
772 /* db is pointed to by the dnode */
773 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
774 if (DMU_OBJECT_IS_SPECIAL(db->db.db_object))
775 ASSERT(db->db_parent == NULL);
776 else
777 ASSERT(db->db_parent != NULL);
778 if (db->db_blkid != DMU_SPILL_BLKID)
779 ASSERT3P(db->db_blkptr, ==,
780 &dn->dn_phys->dn_blkptr[db->db_blkid]);
781 } else {
782 /* db is pointed to by an indirect block */
783 int epb = db->db_parent->db.db_size >> SPA_BLKPTRSHIFT;
784 ASSERT3U(db->db_parent->db_level, ==, db->db_level+1);
785 ASSERT3U(db->db_parent->db.db_object, ==,
786 db->db.db_object);
788 * dnode_grow_indblksz() can make this fail if we don't
789 * have the struct_rwlock. XXX indblksz no longer
790 * grows. safe to do this now?
792 if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
793 ASSERT3P(db->db_blkptr, ==,
794 ((blkptr_t *)db->db_parent->db.db_data +
795 db->db_blkid % epb));
799 if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) &&
800 (db->db_buf == NULL || db->db_buf->b_data) &&
801 db->db.db_data && db->db_blkid != DMU_BONUS_BLKID &&
802 db->db_state != DB_FILL && !dn->dn_free_txg) {
804 * If the blkptr isn't set but they have nonzero data,
805 * it had better be dirty, otherwise we'll lose that
806 * data when we evict this buffer.
808 * There is an exception to this rule for indirect blocks; in
809 * this case, if the indirect block is a hole, we fill in a few
810 * fields on each of the child blocks (importantly, birth time)
811 * to prevent hole birth times from being lost when you
812 * partially fill in a hole.
814 if (db->db_dirtycnt == 0) {
815 if (db->db_level == 0) {
816 uint64_t *buf = db->db.db_data;
817 int i;
819 for (i = 0; i < db->db.db_size >> 3; i++) {
820 ASSERT(buf[i] == 0);
822 } else {
823 blkptr_t *bps = db->db.db_data;
824 ASSERT3U(1 << DB_DNODE(db)->dn_indblkshift, ==,
825 db->db.db_size);
827 * We want to verify that all the blkptrs in the
828 * indirect block are holes, but we may have
829 * automatically set up a few fields for them.
830 * We iterate through each blkptr and verify
831 * they only have those fields set.
833 for (int i = 0;
834 i < db->db.db_size / sizeof (blkptr_t);
835 i++) {
836 blkptr_t *bp = &bps[i];
837 ASSERT(ZIO_CHECKSUM_IS_ZERO(
838 &bp->blk_cksum));
839 ASSERT(
840 DVA_IS_EMPTY(&bp->blk_dva[0]) &&
841 DVA_IS_EMPTY(&bp->blk_dva[1]) &&
842 DVA_IS_EMPTY(&bp->blk_dva[2]));
843 ASSERT0(bp->blk_fill);
844 ASSERT0(bp->blk_pad[0]);
845 ASSERT0(bp->blk_pad[1]);
846 ASSERT(!BP_IS_EMBEDDED(bp));
847 ASSERT(BP_IS_HOLE(bp));
848 ASSERT0(bp->blk_phys_birth);
853 DB_DNODE_EXIT(db);
855 #endif
857 static void
858 dbuf_clear_data(dmu_buf_impl_t *db)
860 ASSERT(MUTEX_HELD(&db->db_mtx));
861 dbuf_evict_user(db);
862 ASSERT3P(db->db_buf, ==, NULL);
863 db->db.db_data = NULL;
864 if (db->db_state != DB_NOFILL)
865 db->db_state = DB_UNCACHED;
868 static void
869 dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf)
871 ASSERT(MUTEX_HELD(&db->db_mtx));
872 ASSERT(buf != NULL);
874 db->db_buf = buf;
875 ASSERT(buf->b_data != NULL);
876 db->db.db_data = buf->b_data;
880 * Loan out an arc_buf for read. Return the loaned arc_buf.
882 arc_buf_t *
883 dbuf_loan_arcbuf(dmu_buf_impl_t *db)
885 arc_buf_t *abuf;
887 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
888 mutex_enter(&db->db_mtx);
889 if (arc_released(db->db_buf) || refcount_count(&db->db_holds) > 1) {
890 int blksz = db->db.db_size;
891 spa_t *spa = db->db_objset->os_spa;
893 mutex_exit(&db->db_mtx);
894 abuf = arc_loan_buf(spa, B_FALSE, blksz);
895 bcopy(db->db.db_data, abuf->b_data, blksz);
896 } else {
897 abuf = db->db_buf;
898 arc_loan_inuse_buf(abuf, db);
899 db->db_buf = NULL;
900 dbuf_clear_data(db);
901 mutex_exit(&db->db_mtx);
903 return (abuf);
907 * Calculate which level n block references the data at the level 0 offset
908 * provided.
910 uint64_t
911 dbuf_whichblock(dnode_t *dn, int64_t level, uint64_t offset)
913 if (dn->dn_datablkshift != 0 && dn->dn_indblkshift != 0) {
915 * The level n blkid is equal to the level 0 blkid divided by
916 * the number of level 0s in a level n block.
918 * The level 0 blkid is offset >> datablkshift =
919 * offset / 2^datablkshift.
921 * The number of level 0s in a level n is the number of block
922 * pointers in an indirect block, raised to the power of level.
923 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
924 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
926 * Thus, the level n blkid is: offset /
927 * ((2^datablkshift)*(2^(level*(indblkshift - SPA_BLKPTRSHIFT)))
928 * = offset / 2^(datablkshift + level *
929 * (indblkshift - SPA_BLKPTRSHIFT))
930 * = offset >> (datablkshift + level *
931 * (indblkshift - SPA_BLKPTRSHIFT))
933 return (offset >> (dn->dn_datablkshift + level *
934 (dn->dn_indblkshift - SPA_BLKPTRSHIFT)));
935 } else {
936 ASSERT3U(offset, <, dn->dn_datablksz);
937 return (0);
941 static void
942 dbuf_read_done(zio_t *zio, arc_buf_t *buf, void *vdb)
944 dmu_buf_impl_t *db = vdb;
946 mutex_enter(&db->db_mtx);
947 ASSERT3U(db->db_state, ==, DB_READ);
949 * All reads are synchronous, so we must have a hold on the dbuf
951 ASSERT(refcount_count(&db->db_holds) > 0);
952 ASSERT(db->db_buf == NULL);
953 ASSERT(db->db.db_data == NULL);
954 if (buf == NULL) {
955 /* i/o error */
956 ASSERT(zio == NULL || zio->io_error != 0);
957 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
958 ASSERT3P(db->db_buf, ==, NULL);
959 db->db_state = DB_UNCACHED;
960 } else if (db->db_level == 0 && db->db_freed_in_flight) {
961 /* freed in flight */
962 ASSERT(zio == NULL || zio->io_error == 0);
963 arc_release(buf, db);
964 bzero(buf->b_data, db->db.db_size);
965 arc_buf_freeze(buf);
966 db->db_freed_in_flight = FALSE;
967 dbuf_set_data(db, buf);
968 db->db_state = DB_CACHED;
969 } else {
970 /* success */
971 ASSERT(zio == NULL || zio->io_error == 0);
972 dbuf_set_data(db, buf);
973 db->db_state = DB_CACHED;
975 cv_broadcast(&db->db_changed);
976 dbuf_rele_and_unlock(db, NULL, B_FALSE);
979 static void
980 dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
982 dnode_t *dn;
983 zbookmark_phys_t zb;
984 arc_flags_t aflags = ARC_FLAG_NOWAIT;
986 DB_DNODE_ENTER(db);
987 dn = DB_DNODE(db);
988 ASSERT(!refcount_is_zero(&db->db_holds));
989 /* We need the struct_rwlock to prevent db_blkptr from changing. */
990 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
991 ASSERT(MUTEX_HELD(&db->db_mtx));
992 ASSERT(db->db_state == DB_UNCACHED);
993 ASSERT(db->db_buf == NULL);
995 if (db->db_blkid == DMU_BONUS_BLKID) {
996 int bonuslen = MIN(dn->dn_bonuslen, dn->dn_phys->dn_bonuslen);
998 ASSERT3U(bonuslen, <=, db->db.db_size);
999 db->db.db_data = zio_buf_alloc(DN_MAX_BONUSLEN);
1000 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
1001 if (bonuslen < DN_MAX_BONUSLEN)
1002 bzero(db->db.db_data, DN_MAX_BONUSLEN);
1003 if (bonuslen)
1004 bcopy(DN_BONUS(dn->dn_phys), db->db.db_data, bonuslen);
1005 DB_DNODE_EXIT(db);
1006 db->db_state = DB_CACHED;
1007 mutex_exit(&db->db_mtx);
1008 return;
1012 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
1013 * processes the delete record and clears the bp while we are waiting
1014 * for the dn_mtx (resulting in a "no" from block_freed).
1016 if (db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr) ||
1017 (db->db_level == 0 && (dnode_block_freed(dn, db->db_blkid) ||
1018 BP_IS_HOLE(db->db_blkptr)))) {
1019 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1021 dbuf_set_data(db, arc_alloc_buf(db->db_objset->os_spa, db, type,
1022 db->db.db_size));
1023 bzero(db->db.db_data, db->db.db_size);
1025 if (db->db_blkptr != NULL && db->db_level > 0 &&
1026 BP_IS_HOLE(db->db_blkptr) &&
1027 db->db_blkptr->blk_birth != 0) {
1028 blkptr_t *bps = db->db.db_data;
1029 for (int i = 0; i < ((1 <<
1030 DB_DNODE(db)->dn_indblkshift) / sizeof (blkptr_t));
1031 i++) {
1032 blkptr_t *bp = &bps[i];
1033 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
1034 1 << dn->dn_indblkshift);
1035 BP_SET_LSIZE(bp,
1036 BP_GET_LEVEL(db->db_blkptr) == 1 ?
1037 dn->dn_datablksz :
1038 BP_GET_LSIZE(db->db_blkptr));
1039 BP_SET_TYPE(bp, BP_GET_TYPE(db->db_blkptr));
1040 BP_SET_LEVEL(bp,
1041 BP_GET_LEVEL(db->db_blkptr) - 1);
1042 BP_SET_BIRTH(bp, db->db_blkptr->blk_birth, 0);
1045 DB_DNODE_EXIT(db);
1046 db->db_state = DB_CACHED;
1047 mutex_exit(&db->db_mtx);
1048 return;
1051 DB_DNODE_EXIT(db);
1053 db->db_state = DB_READ;
1054 mutex_exit(&db->db_mtx);
1056 if (DBUF_IS_L2CACHEABLE(db))
1057 aflags |= ARC_FLAG_L2CACHE;
1059 SET_BOOKMARK(&zb, db->db_objset->os_dsl_dataset ?
1060 db->db_objset->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1061 db->db.db_object, db->db_level, db->db_blkid);
1063 dbuf_add_ref(db, NULL);
1065 (void) arc_read(zio, db->db_objset->os_spa, db->db_blkptr,
1066 dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ,
1067 (flags & DB_RF_CANFAIL) ? ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED,
1068 &aflags, &zb);
1072 * This is our just-in-time copy function. It makes a copy of buffers that
1073 * have been modified in a previous transaction group before we access them in
1074 * the current active group.
1076 * This function is used in three places: when we are dirtying a buffer for the
1077 * first time in a txg, when we are freeing a range in a dnode that includes
1078 * this buffer, and when we are accessing a buffer which was received compressed
1079 * and later referenced in a WRITE_BYREF record.
1081 * Note that when we are called from dbuf_free_range() we do not put a hold on
1082 * the buffer, we just traverse the active dbuf list for the dnode.
1084 static void
1085 dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg)
1087 dbuf_dirty_record_t *dr = db->db_last_dirty;
1089 ASSERT(MUTEX_HELD(&db->db_mtx));
1090 ASSERT(db->db.db_data != NULL);
1091 ASSERT(db->db_level == 0);
1092 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT);
1094 if (dr == NULL ||
1095 (dr->dt.dl.dr_data !=
1096 ((db->db_blkid == DMU_BONUS_BLKID) ? db->db.db_data : db->db_buf)))
1097 return;
1100 * If the last dirty record for this dbuf has not yet synced
1101 * and its referencing the dbuf data, either:
1102 * reset the reference to point to a new copy,
1103 * or (if there a no active holders)
1104 * just null out the current db_data pointer.
1106 ASSERT(dr->dr_txg >= txg - 2);
1107 if (db->db_blkid == DMU_BONUS_BLKID) {
1108 /* Note that the data bufs here are zio_bufs */
1109 dr->dt.dl.dr_data = zio_buf_alloc(DN_MAX_BONUSLEN);
1110 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
1111 bcopy(db->db.db_data, dr->dt.dl.dr_data, DN_MAX_BONUSLEN);
1112 } else if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
1113 int size = arc_buf_size(db->db_buf);
1114 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1115 spa_t *spa = db->db_objset->os_spa;
1116 enum zio_compress compress_type =
1117 arc_get_compression(db->db_buf);
1119 if (compress_type == ZIO_COMPRESS_OFF) {
1120 dr->dt.dl.dr_data = arc_alloc_buf(spa, db, type, size);
1121 } else {
1122 ASSERT3U(type, ==, ARC_BUFC_DATA);
1123 dr->dt.dl.dr_data = arc_alloc_compressed_buf(spa, db,
1124 size, arc_buf_lsize(db->db_buf), compress_type);
1126 bcopy(db->db.db_data, dr->dt.dl.dr_data->b_data, size);
1127 } else {
1128 db->db_buf = NULL;
1129 dbuf_clear_data(db);
1134 dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
1136 int err = 0;
1137 boolean_t prefetch;
1138 dnode_t *dn;
1141 * We don't have to hold the mutex to check db_state because it
1142 * can't be freed while we have a hold on the buffer.
1144 ASSERT(!refcount_is_zero(&db->db_holds));
1146 if (db->db_state == DB_NOFILL)
1147 return (SET_ERROR(EIO));
1149 DB_DNODE_ENTER(db);
1150 dn = DB_DNODE(db);
1151 if ((flags & DB_RF_HAVESTRUCT) == 0)
1152 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1154 prefetch = db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1155 (flags & DB_RF_NOPREFETCH) == 0 && dn != NULL &&
1156 DBUF_IS_CACHEABLE(db);
1158 mutex_enter(&db->db_mtx);
1159 if (db->db_state == DB_CACHED) {
1161 * If the arc buf is compressed, we need to decompress it to
1162 * read the data. This could happen during the "zfs receive" of
1163 * a stream which is compressed and deduplicated.
1165 if (db->db_buf != NULL &&
1166 arc_get_compression(db->db_buf) != ZIO_COMPRESS_OFF) {
1167 dbuf_fix_old_data(db,
1168 spa_syncing_txg(dmu_objset_spa(db->db_objset)));
1169 err = arc_decompress(db->db_buf);
1170 dbuf_set_data(db, db->db_buf);
1172 mutex_exit(&db->db_mtx);
1173 if (prefetch)
1174 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE);
1175 if ((flags & DB_RF_HAVESTRUCT) == 0)
1176 rw_exit(&dn->dn_struct_rwlock);
1177 DB_DNODE_EXIT(db);
1178 } else if (db->db_state == DB_UNCACHED) {
1179 spa_t *spa = dn->dn_objset->os_spa;
1180 boolean_t need_wait = B_FALSE;
1182 if (zio == NULL &&
1183 db->db_blkptr != NULL && !BP_IS_HOLE(db->db_blkptr)) {
1184 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
1185 need_wait = B_TRUE;
1187 dbuf_read_impl(db, zio, flags);
1189 /* dbuf_read_impl has dropped db_mtx for us */
1191 if (prefetch)
1192 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE);
1194 if ((flags & DB_RF_HAVESTRUCT) == 0)
1195 rw_exit(&dn->dn_struct_rwlock);
1196 DB_DNODE_EXIT(db);
1198 if (need_wait)
1199 err = zio_wait(zio);
1200 } else {
1202 * Another reader came in while the dbuf was in flight
1203 * between UNCACHED and CACHED. Either a writer will finish
1204 * writing the buffer (sending the dbuf to CACHED) or the
1205 * first reader's request will reach the read_done callback
1206 * and send the dbuf to CACHED. Otherwise, a failure
1207 * occurred and the dbuf went to UNCACHED.
1209 mutex_exit(&db->db_mtx);
1210 if (prefetch)
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);
1214 DB_DNODE_EXIT(db);
1216 /* Skip the wait per the caller's request. */
1217 mutex_enter(&db->db_mtx);
1218 if ((flags & DB_RF_NEVERWAIT) == 0) {
1219 while (db->db_state == DB_READ ||
1220 db->db_state == DB_FILL) {
1221 ASSERT(db->db_state == DB_READ ||
1222 (flags & DB_RF_HAVESTRUCT) == 0);
1223 DTRACE_PROBE2(blocked__read, dmu_buf_impl_t *,
1224 db, zio_t *, zio);
1225 cv_wait(&db->db_changed, &db->db_mtx);
1227 if (db->db_state == DB_UNCACHED)
1228 err = SET_ERROR(EIO);
1230 mutex_exit(&db->db_mtx);
1233 return (err);
1236 static void
1237 dbuf_noread(dmu_buf_impl_t *db)
1239 ASSERT(!refcount_is_zero(&db->db_holds));
1240 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1241 mutex_enter(&db->db_mtx);
1242 while (db->db_state == DB_READ || db->db_state == DB_FILL)
1243 cv_wait(&db->db_changed, &db->db_mtx);
1244 if (db->db_state == DB_UNCACHED) {
1245 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1246 spa_t *spa = db->db_objset->os_spa;
1248 ASSERT(db->db_buf == NULL);
1249 ASSERT(db->db.db_data == NULL);
1250 dbuf_set_data(db, arc_alloc_buf(spa, db, type, db->db.db_size));
1251 db->db_state = DB_FILL;
1252 } else if (db->db_state == DB_NOFILL) {
1253 dbuf_clear_data(db);
1254 } else {
1255 ASSERT3U(db->db_state, ==, DB_CACHED);
1257 mutex_exit(&db->db_mtx);
1260 void
1261 dbuf_unoverride(dbuf_dirty_record_t *dr)
1263 dmu_buf_impl_t *db = dr->dr_dbuf;
1264 blkptr_t *bp = &dr->dt.dl.dr_overridden_by;
1265 uint64_t txg = dr->dr_txg;
1267 ASSERT(MUTEX_HELD(&db->db_mtx));
1269 * This assert is valid because dmu_sync() expects to be called by
1270 * a zilog's get_data while holding a range lock. This call only
1271 * comes from dbuf_dirty() callers who must also hold a range lock.
1273 ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC);
1274 ASSERT(db->db_level == 0);
1276 if (db->db_blkid == DMU_BONUS_BLKID ||
1277 dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN)
1278 return;
1280 ASSERT(db->db_data_pending != dr);
1282 /* free this block */
1283 if (!BP_IS_HOLE(bp) && !dr->dt.dl.dr_nopwrite)
1284 zio_free(db->db_objset->os_spa, txg, bp);
1286 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
1287 dr->dt.dl.dr_nopwrite = B_FALSE;
1290 * Release the already-written buffer, so we leave it in
1291 * a consistent dirty state. Note that all callers are
1292 * modifying the buffer, so they will immediately do
1293 * another (redundant) arc_release(). Therefore, leave
1294 * the buf thawed to save the effort of freezing &
1295 * immediately re-thawing it.
1297 arc_release(dr->dt.dl.dr_data, db);
1301 * Evict (if its unreferenced) or clear (if its referenced) any level-0
1302 * data blocks in the free range, so that any future readers will find
1303 * empty blocks.
1305 void
1306 dbuf_free_range(dnode_t *dn, uint64_t start_blkid, uint64_t end_blkid,
1307 dmu_tx_t *tx)
1309 dmu_buf_impl_t db_search;
1310 dmu_buf_impl_t *db, *db_next;
1311 uint64_t txg = tx->tx_txg;
1312 avl_index_t where;
1314 if (end_blkid > dn->dn_maxblkid &&
1315 !(start_blkid == DMU_SPILL_BLKID || end_blkid == DMU_SPILL_BLKID))
1316 end_blkid = dn->dn_maxblkid;
1317 dprintf_dnode(dn, "start=%llu end=%llu\n", start_blkid, end_blkid);
1319 db_search.db_level = 0;
1320 db_search.db_blkid = start_blkid;
1321 db_search.db_state = DB_SEARCH;
1323 mutex_enter(&dn->dn_dbufs_mtx);
1324 db = avl_find(&dn->dn_dbufs, &db_search, &where);
1325 ASSERT3P(db, ==, NULL);
1327 db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER);
1329 for (; db != NULL; db = db_next) {
1330 db_next = AVL_NEXT(&dn->dn_dbufs, db);
1331 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1333 if (db->db_level != 0 || db->db_blkid > end_blkid) {
1334 break;
1336 ASSERT3U(db->db_blkid, >=, start_blkid);
1338 /* found a level 0 buffer in the range */
1339 mutex_enter(&db->db_mtx);
1340 if (dbuf_undirty(db, tx)) {
1341 /* mutex has been dropped and dbuf destroyed */
1342 continue;
1345 if (db->db_state == DB_UNCACHED ||
1346 db->db_state == DB_NOFILL ||
1347 db->db_state == DB_EVICTING) {
1348 ASSERT(db->db.db_data == NULL);
1349 mutex_exit(&db->db_mtx);
1350 continue;
1352 if (db->db_state == DB_READ || db->db_state == DB_FILL) {
1353 /* will be handled in dbuf_read_done or dbuf_rele */
1354 db->db_freed_in_flight = TRUE;
1355 mutex_exit(&db->db_mtx);
1356 continue;
1358 if (refcount_count(&db->db_holds) == 0) {
1359 ASSERT(db->db_buf);
1360 dbuf_destroy(db);
1361 continue;
1363 /* The dbuf is referenced */
1365 if (db->db_last_dirty != NULL) {
1366 dbuf_dirty_record_t *dr = db->db_last_dirty;
1368 if (dr->dr_txg == txg) {
1370 * This buffer is "in-use", re-adjust the file
1371 * size to reflect that this buffer may
1372 * contain new data when we sync.
1374 if (db->db_blkid != DMU_SPILL_BLKID &&
1375 db->db_blkid > dn->dn_maxblkid)
1376 dn->dn_maxblkid = db->db_blkid;
1377 dbuf_unoverride(dr);
1378 } else {
1380 * This dbuf is not dirty in the open context.
1381 * Either uncache it (if its not referenced in
1382 * the open context) or reset its contents to
1383 * empty.
1385 dbuf_fix_old_data(db, txg);
1388 /* clear the contents if its cached */
1389 if (db->db_state == DB_CACHED) {
1390 ASSERT(db->db.db_data != NULL);
1391 arc_release(db->db_buf, db);
1392 bzero(db->db.db_data, db->db.db_size);
1393 arc_buf_freeze(db->db_buf);
1396 mutex_exit(&db->db_mtx);
1398 mutex_exit(&dn->dn_dbufs_mtx);
1401 void
1402 dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx)
1404 arc_buf_t *buf, *obuf;
1405 int osize = db->db.db_size;
1406 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1407 dnode_t *dn;
1409 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1411 DB_DNODE_ENTER(db);
1412 dn = DB_DNODE(db);
1414 /* XXX does *this* func really need the lock? */
1415 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
1418 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
1419 * is OK, because there can be no other references to the db
1420 * when we are changing its size, so no concurrent DB_FILL can
1421 * be happening.
1424 * XXX we should be doing a dbuf_read, checking the return
1425 * value and returning that up to our callers
1427 dmu_buf_will_dirty(&db->db, tx);
1429 /* create the data buffer for the new block */
1430 buf = arc_alloc_buf(dn->dn_objset->os_spa, db, type, size);
1432 /* copy old block data to the new block */
1433 obuf = db->db_buf;
1434 bcopy(obuf->b_data, buf->b_data, MIN(osize, size));
1435 /* zero the remainder */
1436 if (size > osize)
1437 bzero((uint8_t *)buf->b_data + osize, size - osize);
1439 mutex_enter(&db->db_mtx);
1440 dbuf_set_data(db, buf);
1441 arc_buf_destroy(obuf, db);
1442 db->db.db_size = size;
1444 if (db->db_level == 0) {
1445 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
1446 db->db_last_dirty->dt.dl.dr_data = buf;
1448 mutex_exit(&db->db_mtx);
1450 dmu_objset_willuse_space(dn->dn_objset, size - osize, tx);
1451 DB_DNODE_EXIT(db);
1454 void
1455 dbuf_release_bp(dmu_buf_impl_t *db)
1457 objset_t *os = db->db_objset;
1459 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
1460 ASSERT(arc_released(os->os_phys_buf) ||
1461 list_link_active(&os->os_dsl_dataset->ds_synced_link));
1462 ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf));
1464 (void) arc_release(db->db_buf, db);
1468 * We already have a dirty record for this TXG, and we are being
1469 * dirtied again.
1471 static void
1472 dbuf_redirty(dbuf_dirty_record_t *dr)
1474 dmu_buf_impl_t *db = dr->dr_dbuf;
1476 ASSERT(MUTEX_HELD(&db->db_mtx));
1478 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID) {
1480 * If this buffer has already been written out,
1481 * we now need to reset its state.
1483 dbuf_unoverride(dr);
1484 if (db->db.db_object != DMU_META_DNODE_OBJECT &&
1485 db->db_state != DB_NOFILL) {
1486 /* Already released on initial dirty, so just thaw. */
1487 ASSERT(arc_released(db->db_buf));
1488 arc_buf_thaw(db->db_buf);
1493 dbuf_dirty_record_t *
1494 dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1496 dnode_t *dn;
1497 objset_t *os;
1498 dbuf_dirty_record_t **drp, *dr;
1499 int drop_struct_lock = FALSE;
1500 int txgoff = tx->tx_txg & TXG_MASK;
1502 ASSERT(tx->tx_txg != 0);
1503 ASSERT(!refcount_is_zero(&db->db_holds));
1504 DMU_TX_DIRTY_BUF(tx, db);
1506 DB_DNODE_ENTER(db);
1507 dn = DB_DNODE(db);
1509 * Shouldn't dirty a regular buffer in syncing context. Private
1510 * objects may be dirtied in syncing context, but only if they
1511 * were already pre-dirtied in open context.
1513 #ifdef DEBUG
1514 if (dn->dn_objset->os_dsl_dataset != NULL) {
1515 rrw_enter(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock,
1516 RW_READER, FTAG);
1518 ASSERT(!dmu_tx_is_syncing(tx) ||
1519 BP_IS_HOLE(dn->dn_objset->os_rootbp) ||
1520 DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1521 dn->dn_objset->os_dsl_dataset == NULL);
1522 if (dn->dn_objset->os_dsl_dataset != NULL)
1523 rrw_exit(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock, FTAG);
1524 #endif
1526 * We make this assert for private objects as well, but after we
1527 * check if we're already dirty. They are allowed to re-dirty
1528 * in syncing context.
1530 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1531 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1532 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1534 mutex_enter(&db->db_mtx);
1536 * XXX make this true for indirects too? The problem is that
1537 * transactions created with dmu_tx_create_assigned() from
1538 * syncing context don't bother holding ahead.
1540 ASSERT(db->db_level != 0 ||
1541 db->db_state == DB_CACHED || db->db_state == DB_FILL ||
1542 db->db_state == DB_NOFILL);
1544 mutex_enter(&dn->dn_mtx);
1546 * Don't set dirtyctx to SYNC if we're just modifying this as we
1547 * initialize the objset.
1549 if (dn->dn_dirtyctx == DN_UNDIRTIED) {
1550 if (dn->dn_objset->os_dsl_dataset != NULL) {
1551 rrw_enter(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock,
1552 RW_READER, FTAG);
1554 if (!BP_IS_HOLE(dn->dn_objset->os_rootbp)) {
1555 dn->dn_dirtyctx = (dmu_tx_is_syncing(tx) ?
1556 DN_DIRTY_SYNC : DN_DIRTY_OPEN);
1557 ASSERT(dn->dn_dirtyctx_firstset == NULL);
1558 dn->dn_dirtyctx_firstset = kmem_alloc(1, KM_SLEEP);
1560 if (dn->dn_objset->os_dsl_dataset != NULL) {
1561 rrw_exit(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock,
1562 FTAG);
1565 mutex_exit(&dn->dn_mtx);
1567 if (db->db_blkid == DMU_SPILL_BLKID)
1568 dn->dn_have_spill = B_TRUE;
1571 * If this buffer is already dirty, we're done.
1573 drp = &db->db_last_dirty;
1574 ASSERT(*drp == NULL || (*drp)->dr_txg <= tx->tx_txg ||
1575 db->db.db_object == DMU_META_DNODE_OBJECT);
1576 while ((dr = *drp) != NULL && dr->dr_txg > tx->tx_txg)
1577 drp = &dr->dr_next;
1578 if (dr && dr->dr_txg == tx->tx_txg) {
1579 DB_DNODE_EXIT(db);
1581 dbuf_redirty(dr);
1582 mutex_exit(&db->db_mtx);
1583 return (dr);
1587 * Only valid if not already dirty.
1589 ASSERT(dn->dn_object == 0 ||
1590 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1591 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1593 ASSERT3U(dn->dn_nlevels, >, db->db_level);
1596 * We should only be dirtying in syncing context if it's the
1597 * mos or we're initializing the os or it's a special object.
1598 * However, we are allowed to dirty in syncing context provided
1599 * we already dirtied it in open context. Hence we must make
1600 * this assertion only if we're not already dirty.
1602 os = dn->dn_objset;
1603 VERIFY3U(tx->tx_txg, <=, spa_final_dirty_txg(os->os_spa));
1604 #ifdef DEBUG
1605 if (dn->dn_objset->os_dsl_dataset != NULL)
1606 rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_READER, FTAG);
1607 ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1608 os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp));
1609 if (dn->dn_objset->os_dsl_dataset != NULL)
1610 rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG);
1611 #endif
1612 ASSERT(db->db.db_size != 0);
1614 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1616 if (db->db_blkid != DMU_BONUS_BLKID) {
1617 dmu_objset_willuse_space(os, db->db.db_size, tx);
1621 * If this buffer is dirty in an old transaction group we need
1622 * to make a copy of it so that the changes we make in this
1623 * transaction group won't leak out when we sync the older txg.
1625 dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP);
1626 if (db->db_level == 0) {
1627 void *data_old = db->db_buf;
1629 if (db->db_state != DB_NOFILL) {
1630 if (db->db_blkid == DMU_BONUS_BLKID) {
1631 dbuf_fix_old_data(db, tx->tx_txg);
1632 data_old = db->db.db_data;
1633 } else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
1635 * Release the data buffer from the cache so
1636 * that we can modify it without impacting
1637 * possible other users of this cached data
1638 * block. Note that indirect blocks and
1639 * private objects are not released until the
1640 * syncing state (since they are only modified
1641 * then).
1643 arc_release(db->db_buf, db);
1644 dbuf_fix_old_data(db, tx->tx_txg);
1645 data_old = db->db_buf;
1647 ASSERT(data_old != NULL);
1649 dr->dt.dl.dr_data = data_old;
1650 } else {
1651 mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_DEFAULT, NULL);
1652 list_create(&dr->dt.di.dr_children,
1653 sizeof (dbuf_dirty_record_t),
1654 offsetof(dbuf_dirty_record_t, dr_dirty_node));
1656 if (db->db_blkid != DMU_BONUS_BLKID && os->os_dsl_dataset != NULL)
1657 dr->dr_accounted = db->db.db_size;
1658 dr->dr_dbuf = db;
1659 dr->dr_txg = tx->tx_txg;
1660 dr->dr_next = *drp;
1661 *drp = dr;
1664 * We could have been freed_in_flight between the dbuf_noread
1665 * and dbuf_dirty. We win, as though the dbuf_noread() had
1666 * happened after the free.
1668 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1669 db->db_blkid != DMU_SPILL_BLKID) {
1670 mutex_enter(&dn->dn_mtx);
1671 if (dn->dn_free_ranges[txgoff] != NULL) {
1672 range_tree_clear(dn->dn_free_ranges[txgoff],
1673 db->db_blkid, 1);
1675 mutex_exit(&dn->dn_mtx);
1676 db->db_freed_in_flight = FALSE;
1680 * This buffer is now part of this txg
1682 dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg);
1683 db->db_dirtycnt += 1;
1684 ASSERT3U(db->db_dirtycnt, <=, 3);
1686 mutex_exit(&db->db_mtx);
1688 if (db->db_blkid == DMU_BONUS_BLKID ||
1689 db->db_blkid == DMU_SPILL_BLKID) {
1690 mutex_enter(&dn->dn_mtx);
1691 ASSERT(!list_link_active(&dr->dr_dirty_node));
1692 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1693 mutex_exit(&dn->dn_mtx);
1694 dnode_setdirty(dn, tx);
1695 DB_DNODE_EXIT(db);
1696 return (dr);
1700 * The dn_struct_rwlock prevents db_blkptr from changing
1701 * due to a write from syncing context completing
1702 * while we are running, so we want to acquire it before
1703 * looking at db_blkptr.
1705 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
1706 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1707 drop_struct_lock = TRUE;
1711 * We need to hold the dn_struct_rwlock to make this assertion,
1712 * because it protects dn_phys / dn_next_nlevels from changing.
1714 ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) ||
1715 dn->dn_phys->dn_nlevels > db->db_level ||
1716 dn->dn_next_nlevels[txgoff] > db->db_level ||
1717 dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level ||
1718 dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level);
1721 * If we are overwriting a dedup BP, then unless it is snapshotted,
1722 * when we get to syncing context we will need to decrement its
1723 * refcount in the DDT. Prefetch the relevant DDT block so that
1724 * syncing context won't have to wait for the i/o.
1726 ddt_prefetch(os->os_spa, db->db_blkptr);
1728 if (db->db_level == 0) {
1729 dnode_new_blkid(dn, db->db_blkid, tx, drop_struct_lock);
1730 ASSERT(dn->dn_maxblkid >= db->db_blkid);
1733 if (db->db_level+1 < dn->dn_nlevels) {
1734 dmu_buf_impl_t *parent = db->db_parent;
1735 dbuf_dirty_record_t *di;
1736 int parent_held = FALSE;
1738 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
1739 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1741 parent = dbuf_hold_level(dn, db->db_level+1,
1742 db->db_blkid >> epbs, FTAG);
1743 ASSERT(parent != NULL);
1744 parent_held = TRUE;
1746 if (drop_struct_lock)
1747 rw_exit(&dn->dn_struct_rwlock);
1748 ASSERT3U(db->db_level+1, ==, parent->db_level);
1749 di = dbuf_dirty(parent, tx);
1750 if (parent_held)
1751 dbuf_rele(parent, FTAG);
1753 mutex_enter(&db->db_mtx);
1755 * Since we've dropped the mutex, it's possible that
1756 * dbuf_undirty() might have changed this out from under us.
1758 if (db->db_last_dirty == dr ||
1759 dn->dn_object == DMU_META_DNODE_OBJECT) {
1760 mutex_enter(&di->dt.di.dr_mtx);
1761 ASSERT3U(di->dr_txg, ==, tx->tx_txg);
1762 ASSERT(!list_link_active(&dr->dr_dirty_node));
1763 list_insert_tail(&di->dt.di.dr_children, dr);
1764 mutex_exit(&di->dt.di.dr_mtx);
1765 dr->dr_parent = di;
1767 mutex_exit(&db->db_mtx);
1768 } else {
1769 ASSERT(db->db_level+1 == dn->dn_nlevels);
1770 ASSERT(db->db_blkid < dn->dn_nblkptr);
1771 ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf);
1772 mutex_enter(&dn->dn_mtx);
1773 ASSERT(!list_link_active(&dr->dr_dirty_node));
1774 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1775 mutex_exit(&dn->dn_mtx);
1776 if (drop_struct_lock)
1777 rw_exit(&dn->dn_struct_rwlock);
1780 dnode_setdirty(dn, tx);
1781 DB_DNODE_EXIT(db);
1782 return (dr);
1786 * Undirty a buffer in the transaction group referenced by the given
1787 * transaction. Return whether this evicted the dbuf.
1789 static boolean_t
1790 dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1792 dnode_t *dn;
1793 uint64_t txg = tx->tx_txg;
1794 dbuf_dirty_record_t *dr, **drp;
1796 ASSERT(txg != 0);
1799 * Due to our use of dn_nlevels below, this can only be called
1800 * in open context, unless we are operating on the MOS.
1801 * From syncing context, dn_nlevels may be different from the
1802 * dn_nlevels used when dbuf was dirtied.
1804 ASSERT(db->db_objset ==
1805 dmu_objset_pool(db->db_objset)->dp_meta_objset ||
1806 txg != spa_syncing_txg(dmu_objset_spa(db->db_objset)));
1807 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1808 ASSERT0(db->db_level);
1809 ASSERT(MUTEX_HELD(&db->db_mtx));
1812 * If this buffer is not dirty, we're done.
1814 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1815 if (dr->dr_txg <= txg)
1816 break;
1817 if (dr == NULL || dr->dr_txg < txg)
1818 return (B_FALSE);
1819 ASSERT(dr->dr_txg == txg);
1820 ASSERT(dr->dr_dbuf == db);
1822 DB_DNODE_ENTER(db);
1823 dn = DB_DNODE(db);
1825 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1827 ASSERT(db->db.db_size != 0);
1829 dsl_pool_undirty_space(dmu_objset_pool(dn->dn_objset),
1830 dr->dr_accounted, txg);
1832 *drp = dr->dr_next;
1835 * Note that there are three places in dbuf_dirty()
1836 * where this dirty record may be put on a list.
1837 * Make sure to do a list_remove corresponding to
1838 * every one of those list_insert calls.
1840 if (dr->dr_parent) {
1841 mutex_enter(&dr->dr_parent->dt.di.dr_mtx);
1842 list_remove(&dr->dr_parent->dt.di.dr_children, dr);
1843 mutex_exit(&dr->dr_parent->dt.di.dr_mtx);
1844 } else if (db->db_blkid == DMU_SPILL_BLKID ||
1845 db->db_level + 1 == dn->dn_nlevels) {
1846 ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf);
1847 mutex_enter(&dn->dn_mtx);
1848 list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr);
1849 mutex_exit(&dn->dn_mtx);
1851 DB_DNODE_EXIT(db);
1853 if (db->db_state != DB_NOFILL) {
1854 dbuf_unoverride(dr);
1856 ASSERT(db->db_buf != NULL);
1857 ASSERT(dr->dt.dl.dr_data != NULL);
1858 if (dr->dt.dl.dr_data != db->db_buf)
1859 arc_buf_destroy(dr->dt.dl.dr_data, db);
1862 kmem_free(dr, sizeof (dbuf_dirty_record_t));
1864 ASSERT(db->db_dirtycnt > 0);
1865 db->db_dirtycnt -= 1;
1867 if (refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) {
1868 ASSERT(db->db_state == DB_NOFILL || arc_released(db->db_buf));
1869 dbuf_destroy(db);
1870 return (B_TRUE);
1873 return (B_FALSE);
1876 void
1877 dmu_buf_will_dirty(dmu_buf_t *db_fake, dmu_tx_t *tx)
1879 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1880 int rf = DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH;
1882 ASSERT(tx->tx_txg != 0);
1883 ASSERT(!refcount_is_zero(&db->db_holds));
1886 * Quick check for dirtyness. For already dirty blocks, this
1887 * reduces runtime of this function by >90%, and overall performance
1888 * by 50% for some workloads (e.g. file deletion with indirect blocks
1889 * cached).
1891 mutex_enter(&db->db_mtx);
1892 dbuf_dirty_record_t *dr;
1893 for (dr = db->db_last_dirty;
1894 dr != NULL && dr->dr_txg >= tx->tx_txg; dr = dr->dr_next) {
1896 * It's possible that it is already dirty but not cached,
1897 * because there are some calls to dbuf_dirty() that don't
1898 * go through dmu_buf_will_dirty().
1900 if (dr->dr_txg == tx->tx_txg && db->db_state == DB_CACHED) {
1901 /* This dbuf is already dirty and cached. */
1902 dbuf_redirty(dr);
1903 mutex_exit(&db->db_mtx);
1904 return;
1907 mutex_exit(&db->db_mtx);
1909 DB_DNODE_ENTER(db);
1910 if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock))
1911 rf |= DB_RF_HAVESTRUCT;
1912 DB_DNODE_EXIT(db);
1913 (void) dbuf_read(db, NULL, rf);
1914 (void) dbuf_dirty(db, tx);
1917 void
1918 dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1920 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1922 db->db_state = DB_NOFILL;
1924 dmu_buf_will_fill(db_fake, tx);
1927 void
1928 dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1930 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1932 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1933 ASSERT(tx->tx_txg != 0);
1934 ASSERT(db->db_level == 0);
1935 ASSERT(!refcount_is_zero(&db->db_holds));
1937 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
1938 dmu_tx_private_ok(tx));
1940 dbuf_noread(db);
1941 (void) dbuf_dirty(db, tx);
1944 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1945 /* ARGSUSED */
1946 void
1947 dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx)
1949 mutex_enter(&db->db_mtx);
1950 DBUF_VERIFY(db);
1952 if (db->db_state == DB_FILL) {
1953 if (db->db_level == 0 && db->db_freed_in_flight) {
1954 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1955 /* we were freed while filling */
1956 /* XXX dbuf_undirty? */
1957 bzero(db->db.db_data, db->db.db_size);
1958 db->db_freed_in_flight = FALSE;
1960 db->db_state = DB_CACHED;
1961 cv_broadcast(&db->db_changed);
1963 mutex_exit(&db->db_mtx);
1966 void
1967 dmu_buf_write_embedded(dmu_buf_t *dbuf, void *data,
1968 bp_embedded_type_t etype, enum zio_compress comp,
1969 int uncompressed_size, int compressed_size, int byteorder,
1970 dmu_tx_t *tx)
1972 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
1973 struct dirty_leaf *dl;
1974 dmu_object_type_t type;
1976 if (etype == BP_EMBEDDED_TYPE_DATA) {
1977 ASSERT(spa_feature_is_active(dmu_objset_spa(db->db_objset),
1978 SPA_FEATURE_EMBEDDED_DATA));
1981 DB_DNODE_ENTER(db);
1982 type = DB_DNODE(db)->dn_type;
1983 DB_DNODE_EXIT(db);
1985 ASSERT0(db->db_level);
1986 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1988 dmu_buf_will_not_fill(dbuf, tx);
1990 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
1991 dl = &db->db_last_dirty->dt.dl;
1992 encode_embedded_bp_compressed(&dl->dr_overridden_by,
1993 data, comp, uncompressed_size, compressed_size);
1994 BPE_SET_ETYPE(&dl->dr_overridden_by, etype);
1995 BP_SET_TYPE(&dl->dr_overridden_by, type);
1996 BP_SET_LEVEL(&dl->dr_overridden_by, 0);
1997 BP_SET_BYTEORDER(&dl->dr_overridden_by, byteorder);
1999 dl->dr_override_state = DR_OVERRIDDEN;
2000 dl->dr_overridden_by.blk_birth = db->db_last_dirty->dr_txg;
2004 * Directly assign a provided arc buf to a given dbuf if it's not referenced
2005 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
2007 void
2008 dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx)
2010 ASSERT(!refcount_is_zero(&db->db_holds));
2011 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2012 ASSERT(db->db_level == 0);
2013 ASSERT3U(dbuf_is_metadata(db), ==, arc_is_metadata(buf));
2014 ASSERT(buf != NULL);
2015 ASSERT(arc_buf_lsize(buf) == db->db.db_size);
2016 ASSERT(tx->tx_txg != 0);
2018 arc_return_buf(buf, db);
2019 ASSERT(arc_released(buf));
2021 mutex_enter(&db->db_mtx);
2023 while (db->db_state == DB_READ || db->db_state == DB_FILL)
2024 cv_wait(&db->db_changed, &db->db_mtx);
2026 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED);
2028 if (db->db_state == DB_CACHED &&
2029 refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) {
2030 mutex_exit(&db->db_mtx);
2031 (void) dbuf_dirty(db, tx);
2032 bcopy(buf->b_data, db->db.db_data, db->db.db_size);
2033 arc_buf_destroy(buf, db);
2034 xuio_stat_wbuf_copied();
2035 return;
2038 xuio_stat_wbuf_nocopy();
2039 if (db->db_state == DB_CACHED) {
2040 dbuf_dirty_record_t *dr = db->db_last_dirty;
2042 ASSERT(db->db_buf != NULL);
2043 if (dr != NULL && dr->dr_txg == tx->tx_txg) {
2044 ASSERT(dr->dt.dl.dr_data == db->db_buf);
2045 if (!arc_released(db->db_buf)) {
2046 ASSERT(dr->dt.dl.dr_override_state ==
2047 DR_OVERRIDDEN);
2048 arc_release(db->db_buf, db);
2050 dr->dt.dl.dr_data = buf;
2051 arc_buf_destroy(db->db_buf, db);
2052 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) {
2053 arc_release(db->db_buf, db);
2054 arc_buf_destroy(db->db_buf, db);
2056 db->db_buf = NULL;
2058 ASSERT(db->db_buf == NULL);
2059 dbuf_set_data(db, buf);
2060 db->db_state = DB_FILL;
2061 mutex_exit(&db->db_mtx);
2062 (void) dbuf_dirty(db, tx);
2063 dmu_buf_fill_done(&db->db, tx);
2066 void
2067 dbuf_destroy(dmu_buf_impl_t *db)
2069 dnode_t *dn;
2070 dmu_buf_impl_t *parent = db->db_parent;
2071 dmu_buf_impl_t *dndb;
2073 ASSERT(MUTEX_HELD(&db->db_mtx));
2074 ASSERT(refcount_is_zero(&db->db_holds));
2076 if (db->db_buf != NULL) {
2077 arc_buf_destroy(db->db_buf, db);
2078 db->db_buf = NULL;
2081 if (db->db_blkid == DMU_BONUS_BLKID) {
2082 ASSERT(db->db.db_data != NULL);
2083 zio_buf_free(db->db.db_data, DN_MAX_BONUSLEN);
2084 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
2085 db->db_state = DB_UNCACHED;
2088 dbuf_clear_data(db);
2090 if (multilist_link_active(&db->db_cache_link)) {
2091 ASSERT(db->db_caching_status == DB_DBUF_CACHE ||
2092 db->db_caching_status == DB_DBUF_METADATA_CACHE);
2094 multilist_remove(dbuf_caches[db->db_caching_status].cache, db);
2095 (void) refcount_remove_many(
2096 &dbuf_caches[db->db_caching_status].size,
2097 db->db.db_size, db);
2099 db->db_caching_status = DB_NO_CACHE;
2102 ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL);
2103 ASSERT(db->db_data_pending == NULL);
2105 db->db_state = DB_EVICTING;
2106 db->db_blkptr = NULL;
2109 * Now that db_state is DB_EVICTING, nobody else can find this via
2110 * the hash table. We can now drop db_mtx, which allows us to
2111 * acquire the dn_dbufs_mtx.
2113 mutex_exit(&db->db_mtx);
2115 DB_DNODE_ENTER(db);
2116 dn = DB_DNODE(db);
2117 dndb = dn->dn_dbuf;
2118 if (db->db_blkid != DMU_BONUS_BLKID) {
2119 boolean_t needlock = !MUTEX_HELD(&dn->dn_dbufs_mtx);
2120 if (needlock)
2121 mutex_enter(&dn->dn_dbufs_mtx);
2122 avl_remove(&dn->dn_dbufs, db);
2123 atomic_dec_32(&dn->dn_dbufs_count);
2124 membar_producer();
2125 DB_DNODE_EXIT(db);
2126 if (needlock)
2127 mutex_exit(&dn->dn_dbufs_mtx);
2129 * Decrementing the dbuf count means that the hold corresponding
2130 * to the removed dbuf is no longer discounted in dnode_move(),
2131 * so the dnode cannot be moved until after we release the hold.
2132 * The membar_producer() ensures visibility of the decremented
2133 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
2134 * release any lock.
2136 mutex_enter(&dn->dn_mtx);
2137 dnode_rele_and_unlock(dn, db, B_TRUE);
2138 db->db_dnode_handle = NULL;
2140 dbuf_hash_remove(db);
2141 } else {
2142 DB_DNODE_EXIT(db);
2145 ASSERT(refcount_is_zero(&db->db_holds));
2147 db->db_parent = NULL;
2149 ASSERT(db->db_buf == NULL);
2150 ASSERT(db->db.db_data == NULL);
2151 ASSERT(db->db_hash_next == NULL);
2152 ASSERT(db->db_blkptr == NULL);
2153 ASSERT(db->db_data_pending == NULL);
2154 ASSERT3U(db->db_caching_status, ==, DB_NO_CACHE);
2155 ASSERT(!multilist_link_active(&db->db_cache_link));
2157 kmem_cache_free(dbuf_kmem_cache, db);
2158 arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
2161 * If this dbuf is referenced from an indirect dbuf,
2162 * decrement the ref count on the indirect dbuf.
2164 if (parent && parent != dndb) {
2165 mutex_enter(&parent->db_mtx);
2166 dbuf_rele_and_unlock(parent, db, B_TRUE);
2171 * Note: While bpp will always be updated if the function returns success,
2172 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
2173 * this happens when the dnode is the meta-dnode, or a userused or groupused
2174 * object.
2176 static int
2177 dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse,
2178 dmu_buf_impl_t **parentp, blkptr_t **bpp)
2180 *parentp = NULL;
2181 *bpp = NULL;
2183 ASSERT(blkid != DMU_BONUS_BLKID);
2185 if (blkid == DMU_SPILL_BLKID) {
2186 mutex_enter(&dn->dn_mtx);
2187 if (dn->dn_have_spill &&
2188 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
2189 *bpp = &dn->dn_phys->dn_spill;
2190 else
2191 *bpp = NULL;
2192 dbuf_add_ref(dn->dn_dbuf, NULL);
2193 *parentp = dn->dn_dbuf;
2194 mutex_exit(&dn->dn_mtx);
2195 return (0);
2198 int nlevels =
2199 (dn->dn_phys->dn_nlevels == 0) ? 1 : dn->dn_phys->dn_nlevels;
2200 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
2202 ASSERT3U(level * epbs, <, 64);
2203 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2205 * This assertion shouldn't trip as long as the max indirect block size
2206 * is less than 1M. The reason for this is that up to that point,
2207 * the number of levels required to address an entire object with blocks
2208 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In
2209 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55
2210 * (i.e. we can address the entire object), objects will all use at most
2211 * N-1 levels and the assertion won't overflow. However, once epbs is
2212 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be
2213 * enough to address an entire object, so objects will have 5 levels,
2214 * but then this assertion will overflow.
2216 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we
2217 * need to redo this logic to handle overflows.
2219 ASSERT(level >= nlevels ||
2220 ((nlevels - level - 1) * epbs) +
2221 highbit64(dn->dn_phys->dn_nblkptr) <= 64);
2222 if (level >= nlevels ||
2223 blkid >= ((uint64_t)dn->dn_phys->dn_nblkptr <<
2224 ((nlevels - level - 1) * epbs)) ||
2225 (fail_sparse &&
2226 blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) {
2227 /* the buffer has no parent yet */
2228 return (SET_ERROR(ENOENT));
2229 } else if (level < nlevels-1) {
2230 /* this block is referenced from an indirect block */
2231 int err = dbuf_hold_impl(dn, level+1,
2232 blkid >> epbs, fail_sparse, FALSE, NULL, parentp);
2233 if (err)
2234 return (err);
2235 err = dbuf_read(*parentp, NULL,
2236 (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL));
2237 if (err) {
2238 dbuf_rele(*parentp, NULL);
2239 *parentp = NULL;
2240 return (err);
2242 *bpp = ((blkptr_t *)(*parentp)->db.db_data) +
2243 (blkid & ((1ULL << epbs) - 1));
2244 if (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))
2245 ASSERT(BP_IS_HOLE(*bpp));
2246 return (0);
2247 } else {
2248 /* the block is referenced from the dnode */
2249 ASSERT3U(level, ==, nlevels-1);
2250 ASSERT(dn->dn_phys->dn_nblkptr == 0 ||
2251 blkid < dn->dn_phys->dn_nblkptr);
2252 if (dn->dn_dbuf) {
2253 dbuf_add_ref(dn->dn_dbuf, NULL);
2254 *parentp = dn->dn_dbuf;
2256 *bpp = &dn->dn_phys->dn_blkptr[blkid];
2257 return (0);
2261 static dmu_buf_impl_t *
2262 dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid,
2263 dmu_buf_impl_t *parent, blkptr_t *blkptr)
2265 objset_t *os = dn->dn_objset;
2266 dmu_buf_impl_t *db, *odb;
2268 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2269 ASSERT(dn->dn_type != DMU_OT_NONE);
2271 db = kmem_cache_alloc(dbuf_kmem_cache, KM_SLEEP);
2273 db->db_objset = os;
2274 db->db.db_object = dn->dn_object;
2275 db->db_level = level;
2276 db->db_blkid = blkid;
2277 db->db_last_dirty = NULL;
2278 db->db_dirtycnt = 0;
2279 db->db_dnode_handle = dn->dn_handle;
2280 db->db_parent = parent;
2281 db->db_blkptr = blkptr;
2283 db->db_user = NULL;
2284 db->db_user_immediate_evict = FALSE;
2285 db->db_freed_in_flight = FALSE;
2286 db->db_pending_evict = FALSE;
2288 if (blkid == DMU_BONUS_BLKID) {
2289 ASSERT3P(parent, ==, dn->dn_dbuf);
2290 db->db.db_size = DN_MAX_BONUSLEN -
2291 (dn->dn_nblkptr-1) * sizeof (blkptr_t);
2292 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
2293 db->db.db_offset = DMU_BONUS_BLKID;
2294 db->db_state = DB_UNCACHED;
2295 db->db_caching_status = DB_NO_CACHE;
2296 /* the bonus dbuf is not placed in the hash table */
2297 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
2298 return (db);
2299 } else if (blkid == DMU_SPILL_BLKID) {
2300 db->db.db_size = (blkptr != NULL) ?
2301 BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE;
2302 db->db.db_offset = 0;
2303 } else {
2304 int blocksize =
2305 db->db_level ? 1 << dn->dn_indblkshift : dn->dn_datablksz;
2306 db->db.db_size = blocksize;
2307 db->db.db_offset = db->db_blkid * blocksize;
2311 * Hold the dn_dbufs_mtx while we get the new dbuf
2312 * in the hash table *and* added to the dbufs list.
2313 * This prevents a possible deadlock with someone
2314 * trying to look up this dbuf before its added to the
2315 * dn_dbufs list.
2317 mutex_enter(&dn->dn_dbufs_mtx);
2318 db->db_state = DB_EVICTING;
2319 if ((odb = dbuf_hash_insert(db)) != NULL) {
2320 /* someone else inserted it first */
2321 kmem_cache_free(dbuf_kmem_cache, db);
2322 mutex_exit(&dn->dn_dbufs_mtx);
2323 return (odb);
2325 avl_add(&dn->dn_dbufs, db);
2327 db->db_state = DB_UNCACHED;
2328 db->db_caching_status = DB_NO_CACHE;
2329 mutex_exit(&dn->dn_dbufs_mtx);
2330 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
2332 if (parent && parent != dn->dn_dbuf)
2333 dbuf_add_ref(parent, db);
2335 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
2336 refcount_count(&dn->dn_holds) > 0);
2337 (void) refcount_add(&dn->dn_holds, db);
2338 atomic_inc_32(&dn->dn_dbufs_count);
2340 dprintf_dbuf(db, "db=%p\n", db);
2342 return (db);
2345 typedef struct dbuf_prefetch_arg {
2346 spa_t *dpa_spa; /* The spa to issue the prefetch in. */
2347 zbookmark_phys_t dpa_zb; /* The target block to prefetch. */
2348 int dpa_epbs; /* Entries (blkptr_t's) Per Block Shift. */
2349 int dpa_curlevel; /* The current level that we're reading */
2350 dnode_t *dpa_dnode; /* The dnode associated with the prefetch */
2351 zio_priority_t dpa_prio; /* The priority I/Os should be issued at. */
2352 zio_t *dpa_zio; /* The parent zio_t for all prefetches. */
2353 arc_flags_t dpa_aflags; /* Flags to pass to the final prefetch. */
2354 } dbuf_prefetch_arg_t;
2357 * Actually issue the prefetch read for the block given.
2359 static void
2360 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t *dpa, blkptr_t *bp)
2362 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
2363 return;
2365 arc_flags_t aflags =
2366 dpa->dpa_aflags | ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH;
2368 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp));
2369 ASSERT3U(dpa->dpa_curlevel, ==, dpa->dpa_zb.zb_level);
2370 ASSERT(dpa->dpa_zio != NULL);
2371 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa, bp, NULL, NULL,
2372 dpa->dpa_prio, ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2373 &aflags, &dpa->dpa_zb);
2377 * Called when an indirect block above our prefetch target is read in. This
2378 * will either read in the next indirect block down the tree or issue the actual
2379 * prefetch if the next block down is our target.
2381 static void
2382 dbuf_prefetch_indirect_done(zio_t *zio, arc_buf_t *abuf, void *private)
2384 dbuf_prefetch_arg_t *dpa = private;
2386 ASSERT3S(dpa->dpa_zb.zb_level, <, dpa->dpa_curlevel);
2387 ASSERT3S(dpa->dpa_curlevel, >, 0);
2389 if (abuf == NULL) {
2390 ASSERT(zio == NULL || zio->io_error != 0);
2391 kmem_free(dpa, sizeof (*dpa));
2392 return;
2394 ASSERT(zio == NULL || zio->io_error == 0);
2397 * The dpa_dnode is only valid if we are called with a NULL
2398 * zio. This indicates that the arc_read() returned without
2399 * first calling zio_read() to issue a physical read. Once
2400 * a physical read is made the dpa_dnode must be invalidated
2401 * as the locks guarding it may have been dropped. If the
2402 * dpa_dnode is still valid, then we want to add it to the dbuf
2403 * cache. To do so, we must hold the dbuf associated with the block
2404 * we just prefetched, read its contents so that we associate it
2405 * with an arc_buf_t, and then release it.
2407 if (zio != NULL) {
2408 ASSERT3S(BP_GET_LEVEL(zio->io_bp), ==, dpa->dpa_curlevel);
2409 if (zio->io_flags & ZIO_FLAG_RAW) {
2410 ASSERT3U(BP_GET_PSIZE(zio->io_bp), ==, zio->io_size);
2411 } else {
2412 ASSERT3U(BP_GET_LSIZE(zio->io_bp), ==, zio->io_size);
2414 ASSERT3P(zio->io_spa, ==, dpa->dpa_spa);
2416 dpa->dpa_dnode = NULL;
2417 } else if (dpa->dpa_dnode != NULL) {
2418 uint64_t curblkid = dpa->dpa_zb.zb_blkid >>
2419 (dpa->dpa_epbs * (dpa->dpa_curlevel -
2420 dpa->dpa_zb.zb_level));
2421 dmu_buf_impl_t *db = dbuf_hold_level(dpa->dpa_dnode,
2422 dpa->dpa_curlevel, curblkid, FTAG);
2423 (void) dbuf_read(db, NULL,
2424 DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH | DB_RF_HAVESTRUCT);
2425 dbuf_rele(db, FTAG);
2428 dpa->dpa_curlevel--;
2430 uint64_t nextblkid = dpa->dpa_zb.zb_blkid >>
2431 (dpa->dpa_epbs * (dpa->dpa_curlevel - dpa->dpa_zb.zb_level));
2432 blkptr_t *bp = ((blkptr_t *)abuf->b_data) +
2433 P2PHASE(nextblkid, 1ULL << dpa->dpa_epbs);
2434 if (BP_IS_HOLE(bp)) {
2435 kmem_free(dpa, sizeof (*dpa));
2436 } else if (dpa->dpa_curlevel == dpa->dpa_zb.zb_level) {
2437 ASSERT3U(nextblkid, ==, dpa->dpa_zb.zb_blkid);
2438 dbuf_issue_final_prefetch(dpa, bp);
2439 kmem_free(dpa, sizeof (*dpa));
2440 } else {
2441 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT;
2442 zbookmark_phys_t zb;
2444 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2445 if (dpa->dpa_aflags & ARC_FLAG_L2CACHE)
2446 iter_aflags |= ARC_FLAG_L2CACHE;
2448 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp));
2450 SET_BOOKMARK(&zb, dpa->dpa_zb.zb_objset,
2451 dpa->dpa_zb.zb_object, dpa->dpa_curlevel, nextblkid);
2453 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa,
2454 bp, dbuf_prefetch_indirect_done, dpa, dpa->dpa_prio,
2455 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2456 &iter_aflags, &zb);
2459 arc_buf_destroy(abuf, private);
2463 * Issue prefetch reads for the given block on the given level. If the indirect
2464 * blocks above that block are not in memory, we will read them in
2465 * asynchronously. As a result, this call never blocks waiting for a read to
2466 * complete.
2468 void
2469 dbuf_prefetch(dnode_t *dn, int64_t level, uint64_t blkid, zio_priority_t prio,
2470 arc_flags_t aflags)
2472 blkptr_t bp;
2473 int epbs, nlevels, curlevel;
2474 uint64_t curblkid;
2476 ASSERT(blkid != DMU_BONUS_BLKID);
2477 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2479 if (blkid > dn->dn_maxblkid)
2480 return;
2482 if (dnode_block_freed(dn, blkid))
2483 return;
2486 * This dnode hasn't been written to disk yet, so there's nothing to
2487 * prefetch.
2489 nlevels = dn->dn_phys->dn_nlevels;
2490 if (level >= nlevels || dn->dn_phys->dn_nblkptr == 0)
2491 return;
2493 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2494 if (dn->dn_phys->dn_maxblkid < blkid << (epbs * level))
2495 return;
2497 dmu_buf_impl_t *db = dbuf_find(dn->dn_objset, dn->dn_object,
2498 level, blkid);
2499 if (db != NULL) {
2500 mutex_exit(&db->db_mtx);
2502 * This dbuf already exists. It is either CACHED, or
2503 * (we assume) about to be read or filled.
2505 return;
2509 * Find the closest ancestor (indirect block) of the target block
2510 * that is present in the cache. In this indirect block, we will
2511 * find the bp that is at curlevel, curblkid.
2513 curlevel = level;
2514 curblkid = blkid;
2515 while (curlevel < nlevels - 1) {
2516 int parent_level = curlevel + 1;
2517 uint64_t parent_blkid = curblkid >> epbs;
2518 dmu_buf_impl_t *db;
2520 if (dbuf_hold_impl(dn, parent_level, parent_blkid,
2521 FALSE, TRUE, FTAG, &db) == 0) {
2522 blkptr_t *bpp = db->db_buf->b_data;
2523 bp = bpp[P2PHASE(curblkid, 1 << epbs)];
2524 dbuf_rele(db, FTAG);
2525 break;
2528 curlevel = parent_level;
2529 curblkid = parent_blkid;
2532 if (curlevel == nlevels - 1) {
2533 /* No cached indirect blocks found. */
2534 ASSERT3U(curblkid, <, dn->dn_phys->dn_nblkptr);
2535 bp = dn->dn_phys->dn_blkptr[curblkid];
2537 if (BP_IS_HOLE(&bp))
2538 return;
2540 ASSERT3U(curlevel, ==, BP_GET_LEVEL(&bp));
2542 zio_t *pio = zio_root(dmu_objset_spa(dn->dn_objset), NULL, NULL,
2543 ZIO_FLAG_CANFAIL);
2545 dbuf_prefetch_arg_t *dpa = kmem_zalloc(sizeof (*dpa), KM_SLEEP);
2546 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
2547 SET_BOOKMARK(&dpa->dpa_zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET,
2548 dn->dn_object, level, blkid);
2549 dpa->dpa_curlevel = curlevel;
2550 dpa->dpa_prio = prio;
2551 dpa->dpa_aflags = aflags;
2552 dpa->dpa_spa = dn->dn_objset->os_spa;
2553 dpa->dpa_dnode = dn;
2554 dpa->dpa_epbs = epbs;
2555 dpa->dpa_zio = pio;
2557 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2558 if (DNODE_LEVEL_IS_L2CACHEABLE(dn, level))
2559 dpa->dpa_aflags |= ARC_FLAG_L2CACHE;
2562 * If we have the indirect just above us, no need to do the asynchronous
2563 * prefetch chain; we'll just run the last step ourselves. If we're at
2564 * a higher level, though, we want to issue the prefetches for all the
2565 * indirect blocks asynchronously, so we can go on with whatever we were
2566 * doing.
2568 if (curlevel == level) {
2569 ASSERT3U(curblkid, ==, blkid);
2570 dbuf_issue_final_prefetch(dpa, &bp);
2571 kmem_free(dpa, sizeof (*dpa));
2572 } else {
2573 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT;
2574 zbookmark_phys_t zb;
2576 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2577 if (DNODE_LEVEL_IS_L2CACHEABLE(dn, level))
2578 iter_aflags |= ARC_FLAG_L2CACHE;
2580 SET_BOOKMARK(&zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET,
2581 dn->dn_object, curlevel, curblkid);
2582 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa,
2583 &bp, dbuf_prefetch_indirect_done, dpa, prio,
2584 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2585 &iter_aflags, &zb);
2588 * We use pio here instead of dpa_zio since it's possible that
2589 * dpa may have already been freed.
2591 zio_nowait(pio);
2595 * Returns with db_holds incremented, and db_mtx not held.
2596 * Note: dn_struct_rwlock must be held.
2599 dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid,
2600 boolean_t fail_sparse, boolean_t fail_uncached,
2601 void *tag, dmu_buf_impl_t **dbp)
2603 dmu_buf_impl_t *db, *parent = NULL;
2605 ASSERT(blkid != DMU_BONUS_BLKID);
2606 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2607 ASSERT3U(dn->dn_nlevels, >, level);
2609 *dbp = NULL;
2610 top:
2611 /* dbuf_find() returns with db_mtx held */
2612 db = dbuf_find(dn->dn_objset, dn->dn_object, level, blkid);
2614 if (db == NULL) {
2615 blkptr_t *bp = NULL;
2616 int err;
2618 if (fail_uncached)
2619 return (SET_ERROR(ENOENT));
2621 ASSERT3P(parent, ==, NULL);
2622 err = dbuf_findbp(dn, level, blkid, fail_sparse, &parent, &bp);
2623 if (fail_sparse) {
2624 if (err == 0 && bp && BP_IS_HOLE(bp))
2625 err = SET_ERROR(ENOENT);
2626 if (err) {
2627 if (parent)
2628 dbuf_rele(parent, NULL);
2629 return (err);
2632 if (err && err != ENOENT)
2633 return (err);
2634 db = dbuf_create(dn, level, blkid, parent, bp);
2637 if (fail_uncached && db->db_state != DB_CACHED) {
2638 mutex_exit(&db->db_mtx);
2639 return (SET_ERROR(ENOENT));
2642 if (db->db_buf != NULL)
2643 ASSERT3P(db->db.db_data, ==, db->db_buf->b_data);
2645 ASSERT(db->db_buf == NULL || arc_referenced(db->db_buf));
2648 * If this buffer is currently syncing out, and we are are
2649 * still referencing it from db_data, we need to make a copy
2650 * of it in case we decide we want to dirty it again in this txg.
2652 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
2653 dn->dn_object != DMU_META_DNODE_OBJECT &&
2654 db->db_state == DB_CACHED && db->db_data_pending) {
2655 dbuf_dirty_record_t *dr = db->db_data_pending;
2657 if (dr->dt.dl.dr_data == db->db_buf) {
2658 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
2660 dbuf_set_data(db,
2661 arc_alloc_buf(dn->dn_objset->os_spa, db, type,
2662 db->db.db_size));
2663 bcopy(dr->dt.dl.dr_data->b_data, db->db.db_data,
2664 db->db.db_size);
2668 if (multilist_link_active(&db->db_cache_link)) {
2669 ASSERT(refcount_is_zero(&db->db_holds));
2670 ASSERT(db->db_caching_status == DB_DBUF_CACHE ||
2671 db->db_caching_status == DB_DBUF_METADATA_CACHE);
2673 multilist_remove(dbuf_caches[db->db_caching_status].cache, db);
2674 (void) refcount_remove_many(
2675 &dbuf_caches[db->db_caching_status].size,
2676 db->db.db_size, db);
2678 db->db_caching_status = DB_NO_CACHE;
2680 (void) refcount_add(&db->db_holds, tag);
2681 DBUF_VERIFY(db);
2682 mutex_exit(&db->db_mtx);
2684 /* NOTE: we can't rele the parent until after we drop the db_mtx */
2685 if (parent)
2686 dbuf_rele(parent, NULL);
2688 ASSERT3P(DB_DNODE(db), ==, dn);
2689 ASSERT3U(db->db_blkid, ==, blkid);
2690 ASSERT3U(db->db_level, ==, level);
2691 *dbp = db;
2693 return (0);
2696 dmu_buf_impl_t *
2697 dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag)
2699 return (dbuf_hold_level(dn, 0, blkid, tag));
2702 dmu_buf_impl_t *
2703 dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag)
2705 dmu_buf_impl_t *db;
2706 int err = dbuf_hold_impl(dn, level, blkid, FALSE, FALSE, tag, &db);
2707 return (err ? NULL : db);
2710 void
2711 dbuf_create_bonus(dnode_t *dn)
2713 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
2715 ASSERT(dn->dn_bonus == NULL);
2716 dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL);
2720 dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx)
2722 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2723 dnode_t *dn;
2725 if (db->db_blkid != DMU_SPILL_BLKID)
2726 return (SET_ERROR(ENOTSUP));
2727 if (blksz == 0)
2728 blksz = SPA_MINBLOCKSIZE;
2729 ASSERT3U(blksz, <=, spa_maxblocksize(dmu_objset_spa(db->db_objset)));
2730 blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE);
2732 DB_DNODE_ENTER(db);
2733 dn = DB_DNODE(db);
2734 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2735 dbuf_new_size(db, blksz, tx);
2736 rw_exit(&dn->dn_struct_rwlock);
2737 DB_DNODE_EXIT(db);
2739 return (0);
2742 void
2743 dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx)
2745 dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx);
2748 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2749 void
2750 dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
2752 int64_t holds = refcount_add(&db->db_holds, tag);
2753 ASSERT3S(holds, >, 1);
2756 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
2757 boolean_t
2758 dbuf_try_add_ref(dmu_buf_t *db_fake, objset_t *os, uint64_t obj, uint64_t blkid,
2759 void *tag)
2761 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2762 dmu_buf_impl_t *found_db;
2763 boolean_t result = B_FALSE;
2765 if (db->db_blkid == DMU_BONUS_BLKID)
2766 found_db = dbuf_find_bonus(os, obj);
2767 else
2768 found_db = dbuf_find(os, obj, 0, blkid);
2770 if (found_db != NULL) {
2771 if (db == found_db && dbuf_refcount(db) > db->db_dirtycnt) {
2772 (void) refcount_add(&db->db_holds, tag);
2773 result = B_TRUE;
2775 mutex_exit(&db->db_mtx);
2777 return (result);
2781 * If you call dbuf_rele() you had better not be referencing the dnode handle
2782 * unless you have some other direct or indirect hold on the dnode. (An indirect
2783 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2784 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2785 * dnode's parent dbuf evicting its dnode handles.
2787 void
2788 dbuf_rele(dmu_buf_impl_t *db, void *tag)
2790 mutex_enter(&db->db_mtx);
2791 dbuf_rele_and_unlock(db, tag, B_FALSE);
2794 void
2795 dmu_buf_rele(dmu_buf_t *db, void *tag)
2797 dbuf_rele((dmu_buf_impl_t *)db, tag);
2801 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2802 * db_dirtycnt and db_holds to be updated atomically. The 'evicting'
2803 * argument should be set if we are already in the dbuf-evicting code
2804 * path, in which case we don't want to recursively evict. This allows us to
2805 * avoid deeply nested stacks that would have a call flow similar to this:
2807 * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify()
2808 * ^ |
2809 * | |
2810 * +-----dbuf_destroy()<--dbuf_evict_one()<--------+
2813 void
2814 dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag, boolean_t evicting)
2816 int64_t holds;
2818 ASSERT(MUTEX_HELD(&db->db_mtx));
2819 DBUF_VERIFY(db);
2822 * Remove the reference to the dbuf before removing its hold on the
2823 * dnode so we can guarantee in dnode_move() that a referenced bonus
2824 * buffer has a corresponding dnode hold.
2826 holds = refcount_remove(&db->db_holds, tag);
2827 ASSERT(holds >= 0);
2830 * We can't freeze indirects if there is a possibility that they
2831 * may be modified in the current syncing context.
2833 if (db->db_buf != NULL &&
2834 holds == (db->db_level == 0 ? db->db_dirtycnt : 0)) {
2835 arc_buf_freeze(db->db_buf);
2838 if (holds == db->db_dirtycnt &&
2839 db->db_level == 0 && db->db_user_immediate_evict)
2840 dbuf_evict_user(db);
2842 if (holds == 0) {
2843 if (db->db_blkid == DMU_BONUS_BLKID) {
2844 dnode_t *dn;
2845 boolean_t evict_dbuf = db->db_pending_evict;
2848 * If the dnode moves here, we cannot cross this
2849 * barrier until the move completes.
2851 DB_DNODE_ENTER(db);
2853 dn = DB_DNODE(db);
2854 atomic_dec_32(&dn->dn_dbufs_count);
2857 * Decrementing the dbuf count means that the bonus
2858 * buffer's dnode hold is no longer discounted in
2859 * dnode_move(). The dnode cannot move until after
2860 * the dnode_rele() below.
2862 DB_DNODE_EXIT(db);
2865 * Do not reference db after its lock is dropped.
2866 * Another thread may evict it.
2868 mutex_exit(&db->db_mtx);
2870 if (evict_dbuf)
2871 dnode_evict_bonus(dn);
2873 dnode_rele(dn, db);
2874 } else if (db->db_buf == NULL) {
2876 * This is a special case: we never associated this
2877 * dbuf with any data allocated from the ARC.
2879 ASSERT(db->db_state == DB_UNCACHED ||
2880 db->db_state == DB_NOFILL);
2881 dbuf_destroy(db);
2882 } else if (arc_released(db->db_buf)) {
2884 * This dbuf has anonymous data associated with it.
2886 dbuf_destroy(db);
2887 } else {
2888 boolean_t do_arc_evict = B_FALSE;
2889 blkptr_t bp;
2890 spa_t *spa = dmu_objset_spa(db->db_objset);
2892 if (!DBUF_IS_CACHEABLE(db) &&
2893 db->db_blkptr != NULL &&
2894 !BP_IS_HOLE(db->db_blkptr) &&
2895 !BP_IS_EMBEDDED(db->db_blkptr)) {
2896 do_arc_evict = B_TRUE;
2897 bp = *db->db_blkptr;
2900 if (!DBUF_IS_CACHEABLE(db) ||
2901 db->db_pending_evict) {
2902 dbuf_destroy(db);
2903 } else if (!multilist_link_active(&db->db_cache_link)) {
2904 ASSERT3U(db->db_caching_status, ==,
2905 DB_NO_CACHE);
2907 dbuf_cached_state_t dcs =
2908 dbuf_include_in_metadata_cache(db) ?
2909 DB_DBUF_METADATA_CACHE : DB_DBUF_CACHE;
2910 db->db_caching_status = dcs;
2912 multilist_insert(dbuf_caches[dcs].cache, db);
2913 (void) refcount_add_many(&dbuf_caches[dcs].size,
2914 db->db.db_size, db);
2915 mutex_exit(&db->db_mtx);
2917 if (db->db_caching_status == DB_DBUF_CACHE &&
2918 !evicting) {
2919 dbuf_evict_notify();
2923 if (do_arc_evict)
2924 arc_freed(spa, &bp);
2926 } else {
2927 mutex_exit(&db->db_mtx);
2932 #pragma weak dmu_buf_refcount = dbuf_refcount
2933 uint64_t
2934 dbuf_refcount(dmu_buf_impl_t *db)
2936 return (refcount_count(&db->db_holds));
2939 void *
2940 dmu_buf_replace_user(dmu_buf_t *db_fake, dmu_buf_user_t *old_user,
2941 dmu_buf_user_t *new_user)
2943 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2945 mutex_enter(&db->db_mtx);
2946 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2947 if (db->db_user == old_user)
2948 db->db_user = new_user;
2949 else
2950 old_user = db->db_user;
2951 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2952 mutex_exit(&db->db_mtx);
2954 return (old_user);
2957 void *
2958 dmu_buf_set_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2960 return (dmu_buf_replace_user(db_fake, NULL, user));
2963 void *
2964 dmu_buf_set_user_ie(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2966 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2968 db->db_user_immediate_evict = TRUE;
2969 return (dmu_buf_set_user(db_fake, user));
2972 void *
2973 dmu_buf_remove_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2975 return (dmu_buf_replace_user(db_fake, user, NULL));
2978 void *
2979 dmu_buf_get_user(dmu_buf_t *db_fake)
2981 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2983 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2984 return (db->db_user);
2987 void
2988 dmu_buf_user_evict_wait()
2990 taskq_wait(dbu_evict_taskq);
2993 blkptr_t *
2994 dmu_buf_get_blkptr(dmu_buf_t *db)
2996 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2997 return (dbi->db_blkptr);
3000 objset_t *
3001 dmu_buf_get_objset(dmu_buf_t *db)
3003 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
3004 return (dbi->db_objset);
3007 dnode_t *
3008 dmu_buf_dnode_enter(dmu_buf_t *db)
3010 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
3011 DB_DNODE_ENTER(dbi);
3012 return (DB_DNODE(dbi));
3015 void
3016 dmu_buf_dnode_exit(dmu_buf_t *db)
3018 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
3019 DB_DNODE_EXIT(dbi);
3022 static void
3023 dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db)
3025 /* ASSERT(dmu_tx_is_syncing(tx) */
3026 ASSERT(MUTEX_HELD(&db->db_mtx));
3028 if (db->db_blkptr != NULL)
3029 return;
3031 if (db->db_blkid == DMU_SPILL_BLKID) {
3032 db->db_blkptr = &dn->dn_phys->dn_spill;
3033 BP_ZERO(db->db_blkptr);
3034 return;
3036 if (db->db_level == dn->dn_phys->dn_nlevels-1) {
3038 * This buffer was allocated at a time when there was
3039 * no available blkptrs from the dnode, or it was
3040 * inappropriate to hook it in (i.e., nlevels mis-match).
3042 ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr);
3043 ASSERT(db->db_parent == NULL);
3044 db->db_parent = dn->dn_dbuf;
3045 db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
3046 DBUF_VERIFY(db);
3047 } else {
3048 dmu_buf_impl_t *parent = db->db_parent;
3049 int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
3051 ASSERT(dn->dn_phys->dn_nlevels > 1);
3052 if (parent == NULL) {
3053 mutex_exit(&db->db_mtx);
3054 rw_enter(&dn->dn_struct_rwlock, RW_READER);
3055 parent = dbuf_hold_level(dn, db->db_level + 1,
3056 db->db_blkid >> epbs, db);
3057 rw_exit(&dn->dn_struct_rwlock);
3058 mutex_enter(&db->db_mtx);
3059 db->db_parent = parent;
3061 db->db_blkptr = (blkptr_t *)parent->db.db_data +
3062 (db->db_blkid & ((1ULL << epbs) - 1));
3063 DBUF_VERIFY(db);
3067 static void
3068 dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
3070 dmu_buf_impl_t *db = dr->dr_dbuf;
3071 dnode_t *dn;
3072 zio_t *zio;
3074 ASSERT(dmu_tx_is_syncing(tx));
3076 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
3078 mutex_enter(&db->db_mtx);
3080 ASSERT(db->db_level > 0);
3081 DBUF_VERIFY(db);
3083 /* Read the block if it hasn't been read yet. */
3084 if (db->db_buf == NULL) {
3085 mutex_exit(&db->db_mtx);
3086 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
3087 mutex_enter(&db->db_mtx);
3089 ASSERT3U(db->db_state, ==, DB_CACHED);
3090 ASSERT(db->db_buf != NULL);
3092 DB_DNODE_ENTER(db);
3093 dn = DB_DNODE(db);
3094 /* Indirect block size must match what the dnode thinks it is. */
3095 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
3096 dbuf_check_blkptr(dn, db);
3097 DB_DNODE_EXIT(db);
3099 /* Provide the pending dirty record to child dbufs */
3100 db->db_data_pending = dr;
3102 mutex_exit(&db->db_mtx);
3104 dbuf_write(dr, db->db_buf, tx);
3106 zio = dr->dr_zio;
3107 mutex_enter(&dr->dt.di.dr_mtx);
3108 dbuf_sync_list(&dr->dt.di.dr_children, db->db_level - 1, tx);
3109 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
3110 mutex_exit(&dr->dt.di.dr_mtx);
3111 zio_nowait(zio);
3114 static void
3115 dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
3117 arc_buf_t **datap = &dr->dt.dl.dr_data;
3118 dmu_buf_impl_t *db = dr->dr_dbuf;
3119 dnode_t *dn;
3120 objset_t *os;
3121 uint64_t txg = tx->tx_txg;
3123 ASSERT(dmu_tx_is_syncing(tx));
3125 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
3127 mutex_enter(&db->db_mtx);
3129 * To be synced, we must be dirtied. But we
3130 * might have been freed after the dirty.
3132 if (db->db_state == DB_UNCACHED) {
3133 /* This buffer has been freed since it was dirtied */
3134 ASSERT(db->db.db_data == NULL);
3135 } else if (db->db_state == DB_FILL) {
3136 /* This buffer was freed and is now being re-filled */
3137 ASSERT(db->db.db_data != dr->dt.dl.dr_data);
3138 } else {
3139 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL);
3141 DBUF_VERIFY(db);
3143 DB_DNODE_ENTER(db);
3144 dn = DB_DNODE(db);
3146 if (db->db_blkid == DMU_SPILL_BLKID) {
3147 mutex_enter(&dn->dn_mtx);
3148 dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR;
3149 mutex_exit(&dn->dn_mtx);
3153 * If this is a bonus buffer, simply copy the bonus data into the
3154 * dnode. It will be written out when the dnode is synced (and it
3155 * will be synced, since it must have been dirty for dbuf_sync to
3156 * be called).
3158 if (db->db_blkid == DMU_BONUS_BLKID) {
3159 dbuf_dirty_record_t **drp;
3161 ASSERT(*datap != NULL);
3162 ASSERT0(db->db_level);
3163 ASSERT3U(dn->dn_phys->dn_bonuslen, <=, DN_MAX_BONUSLEN);
3164 bcopy(*datap, DN_BONUS(dn->dn_phys), dn->dn_phys->dn_bonuslen);
3165 DB_DNODE_EXIT(db);
3167 if (*datap != db->db.db_data) {
3168 zio_buf_free(*datap, DN_MAX_BONUSLEN);
3169 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
3171 db->db_data_pending = NULL;
3172 drp = &db->db_last_dirty;
3173 while (*drp != dr)
3174 drp = &(*drp)->dr_next;
3175 ASSERT(dr->dr_next == NULL);
3176 ASSERT(dr->dr_dbuf == db);
3177 *drp = dr->dr_next;
3178 kmem_free(dr, sizeof (dbuf_dirty_record_t));
3179 ASSERT(db->db_dirtycnt > 0);
3180 db->db_dirtycnt -= 1;
3181 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg, B_FALSE);
3182 return;
3185 os = dn->dn_objset;
3188 * This function may have dropped the db_mtx lock allowing a dmu_sync
3189 * operation to sneak in. As a result, we need to ensure that we
3190 * don't check the dr_override_state until we have returned from
3191 * dbuf_check_blkptr.
3193 dbuf_check_blkptr(dn, db);
3196 * If this buffer is in the middle of an immediate write,
3197 * wait for the synchronous IO to complete.
3199 while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
3200 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
3201 cv_wait(&db->db_changed, &db->db_mtx);
3202 ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN);
3205 if (db->db_state != DB_NOFILL &&
3206 dn->dn_object != DMU_META_DNODE_OBJECT &&
3207 refcount_count(&db->db_holds) > 1 &&
3208 dr->dt.dl.dr_override_state != DR_OVERRIDDEN &&
3209 *datap == db->db_buf) {
3211 * If this buffer is currently "in use" (i.e., there
3212 * are active holds and db_data still references it),
3213 * then make a copy before we start the write so that
3214 * any modifications from the open txg will not leak
3215 * into this write.
3217 * NOTE: this copy does not need to be made for
3218 * objects only modified in the syncing context (e.g.
3219 * DNONE_DNODE blocks).
3221 int psize = arc_buf_size(*datap);
3222 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
3223 enum zio_compress compress_type = arc_get_compression(*datap);
3225 if (compress_type == ZIO_COMPRESS_OFF) {
3226 *datap = arc_alloc_buf(os->os_spa, db, type, psize);
3227 } else {
3228 ASSERT3U(type, ==, ARC_BUFC_DATA);
3229 int lsize = arc_buf_lsize(*datap);
3230 *datap = arc_alloc_compressed_buf(os->os_spa, db,
3231 psize, lsize, compress_type);
3233 bcopy(db->db.db_data, (*datap)->b_data, psize);
3235 db->db_data_pending = dr;
3237 mutex_exit(&db->db_mtx);
3239 dbuf_write(dr, *datap, tx);
3241 ASSERT(!list_link_active(&dr->dr_dirty_node));
3242 if (dn->dn_object == DMU_META_DNODE_OBJECT) {
3243 list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr);
3244 DB_DNODE_EXIT(db);
3245 } else {
3247 * Although zio_nowait() does not "wait for an IO", it does
3248 * initiate the IO. If this is an empty write it seems plausible
3249 * that the IO could actually be completed before the nowait
3250 * returns. We need to DB_DNODE_EXIT() first in case
3251 * zio_nowait() invalidates the dbuf.
3253 DB_DNODE_EXIT(db);
3254 zio_nowait(dr->dr_zio);
3258 void
3259 dbuf_sync_list(list_t *list, int level, dmu_tx_t *tx)
3261 dbuf_dirty_record_t *dr;
3263 while (dr = list_head(list)) {
3264 if (dr->dr_zio != NULL) {
3266 * If we find an already initialized zio then we
3267 * are processing the meta-dnode, and we have finished.
3268 * The dbufs for all dnodes are put back on the list
3269 * during processing, so that we can zio_wait()
3270 * these IOs after initiating all child IOs.
3272 ASSERT3U(dr->dr_dbuf->db.db_object, ==,
3273 DMU_META_DNODE_OBJECT);
3274 break;
3276 if (dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
3277 dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
3278 VERIFY3U(dr->dr_dbuf->db_level, ==, level);
3280 list_remove(list, dr);
3281 if (dr->dr_dbuf->db_level > 0)
3282 dbuf_sync_indirect(dr, tx);
3283 else
3284 dbuf_sync_leaf(dr, tx);
3288 /* ARGSUSED */
3289 static void
3290 dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
3292 dmu_buf_impl_t *db = vdb;
3293 dnode_t *dn;
3294 blkptr_t *bp = zio->io_bp;
3295 blkptr_t *bp_orig = &zio->io_bp_orig;
3296 spa_t *spa = zio->io_spa;
3297 int64_t delta;
3298 uint64_t fill = 0;
3299 int i;
3301 ASSERT3P(db->db_blkptr, !=, NULL);
3302 ASSERT3P(&db->db_data_pending->dr_bp_copy, ==, bp);
3304 DB_DNODE_ENTER(db);
3305 dn = DB_DNODE(db);
3306 delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig);
3307 dnode_diduse_space(dn, delta - zio->io_prev_space_delta);
3308 zio->io_prev_space_delta = delta;
3310 if (bp->blk_birth != 0) {
3311 ASSERT((db->db_blkid != DMU_SPILL_BLKID &&
3312 BP_GET_TYPE(bp) == dn->dn_type) ||
3313 (db->db_blkid == DMU_SPILL_BLKID &&
3314 BP_GET_TYPE(bp) == dn->dn_bonustype) ||
3315 BP_IS_EMBEDDED(bp));
3316 ASSERT(BP_GET_LEVEL(bp) == db->db_level);
3319 mutex_enter(&db->db_mtx);
3321 #ifdef ZFS_DEBUG
3322 if (db->db_blkid == DMU_SPILL_BLKID) {
3323 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
3324 ASSERT(!(BP_IS_HOLE(bp)) &&
3325 db->db_blkptr == &dn->dn_phys->dn_spill);
3327 #endif
3329 if (db->db_level == 0) {
3330 mutex_enter(&dn->dn_mtx);
3331 if (db->db_blkid > dn->dn_phys->dn_maxblkid &&
3332 db->db_blkid != DMU_SPILL_BLKID)
3333 dn->dn_phys->dn_maxblkid = db->db_blkid;
3334 mutex_exit(&dn->dn_mtx);
3336 if (dn->dn_type == DMU_OT_DNODE) {
3337 dnode_phys_t *dnp = db->db.db_data;
3338 for (i = db->db.db_size >> DNODE_SHIFT; i > 0;
3339 i--, dnp++) {
3340 if (dnp->dn_type != DMU_OT_NONE)
3341 fill++;
3343 } else {
3344 if (BP_IS_HOLE(bp)) {
3345 fill = 0;
3346 } else {
3347 fill = 1;
3350 } else {
3351 blkptr_t *ibp = db->db.db_data;
3352 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
3353 for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) {
3354 if (BP_IS_HOLE(ibp))
3355 continue;
3356 fill += BP_GET_FILL(ibp);
3359 DB_DNODE_EXIT(db);
3361 if (!BP_IS_EMBEDDED(bp))
3362 bp->blk_fill = fill;
3364 mutex_exit(&db->db_mtx);
3366 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
3367 *db->db_blkptr = *bp;
3368 rw_exit(&dn->dn_struct_rwlock);
3371 /* ARGSUSED */
3373 * This function gets called just prior to running through the compression
3374 * stage of the zio pipeline. If we're an indirect block comprised of only
3375 * holes, then we want this indirect to be compressed away to a hole. In
3376 * order to do that we must zero out any information about the holes that
3377 * this indirect points to prior to before we try to compress it.
3379 static void
3380 dbuf_write_children_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
3382 dmu_buf_impl_t *db = vdb;
3383 dnode_t *dn;
3384 blkptr_t *bp;
3385 unsigned int epbs, i;
3387 ASSERT3U(db->db_level, >, 0);
3388 DB_DNODE_ENTER(db);
3389 dn = DB_DNODE(db);
3390 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
3391 ASSERT3U(epbs, <, 31);
3393 /* Determine if all our children are holes */
3394 for (i = 0, bp = db->db.db_data; i < 1 << epbs; i++, bp++) {
3395 if (!BP_IS_HOLE(bp))
3396 break;
3400 * If all the children are holes, then zero them all out so that
3401 * we may get compressed away.
3403 if (i == 1 << epbs) {
3405 * We only found holes. Grab the rwlock to prevent
3406 * anybody from reading the blocks we're about to
3407 * zero out.
3409 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
3410 bzero(db->db.db_data, db->db.db_size);
3411 rw_exit(&dn->dn_struct_rwlock);
3413 DB_DNODE_EXIT(db);
3417 * The SPA will call this callback several times for each zio - once
3418 * for every physical child i/o (zio->io_phys_children times). This
3419 * allows the DMU to monitor the progress of each logical i/o. For example,
3420 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
3421 * block. There may be a long delay before all copies/fragments are completed,
3422 * so this callback allows us to retire dirty space gradually, as the physical
3423 * i/os complete.
3425 /* ARGSUSED */
3426 static void
3427 dbuf_write_physdone(zio_t *zio, arc_buf_t *buf, void *arg)
3429 dmu_buf_impl_t *db = arg;
3430 objset_t *os = db->db_objset;
3431 dsl_pool_t *dp = dmu_objset_pool(os);
3432 dbuf_dirty_record_t *dr;
3433 int delta = 0;
3435 dr = db->db_data_pending;
3436 ASSERT3U(dr->dr_txg, ==, zio->io_txg);
3439 * The callback will be called io_phys_children times. Retire one
3440 * portion of our dirty space each time we are called. Any rounding
3441 * error will be cleaned up by dsl_pool_sync()'s call to
3442 * dsl_pool_undirty_space().
3444 delta = dr->dr_accounted / zio->io_phys_children;
3445 dsl_pool_undirty_space(dp, delta, zio->io_txg);
3448 /* ARGSUSED */
3449 static void
3450 dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
3452 dmu_buf_impl_t *db = vdb;
3453 blkptr_t *bp_orig = &zio->io_bp_orig;
3454 blkptr_t *bp = db->db_blkptr;
3455 objset_t *os = db->db_objset;
3456 dmu_tx_t *tx = os->os_synctx;
3457 dbuf_dirty_record_t **drp, *dr;
3459 ASSERT0(zio->io_error);
3460 ASSERT(db->db_blkptr == bp);
3463 * For nopwrites and rewrites we ensure that the bp matches our
3464 * original and bypass all the accounting.
3466 if (zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)) {
3467 ASSERT(BP_EQUAL(bp, bp_orig));
3468 } else {
3469 dsl_dataset_t *ds = os->os_dsl_dataset;
3470 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
3471 dsl_dataset_block_born(ds, bp, tx);
3474 mutex_enter(&db->db_mtx);
3476 DBUF_VERIFY(db);
3478 drp = &db->db_last_dirty;
3479 while ((dr = *drp) != db->db_data_pending)
3480 drp = &dr->dr_next;
3481 ASSERT(!list_link_active(&dr->dr_dirty_node));
3482 ASSERT(dr->dr_dbuf == db);
3483 ASSERT(dr->dr_next == NULL);
3484 *drp = dr->dr_next;
3486 #ifdef ZFS_DEBUG
3487 if (db->db_blkid == DMU_SPILL_BLKID) {
3488 dnode_t *dn;
3490 DB_DNODE_ENTER(db);
3491 dn = DB_DNODE(db);
3492 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
3493 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
3494 db->db_blkptr == &dn->dn_phys->dn_spill);
3495 DB_DNODE_EXIT(db);
3497 #endif
3499 if (db->db_level == 0) {
3500 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
3501 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
3502 if (db->db_state != DB_NOFILL) {
3503 if (dr->dt.dl.dr_data != db->db_buf)
3504 arc_buf_destroy(dr->dt.dl.dr_data, db);
3506 } else {
3507 dnode_t *dn;
3509 DB_DNODE_ENTER(db);
3510 dn = DB_DNODE(db);
3511 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
3512 ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
3513 if (!BP_IS_HOLE(db->db_blkptr)) {
3514 int epbs =
3515 dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
3516 ASSERT3U(db->db_blkid, <=,
3517 dn->dn_phys->dn_maxblkid >> (db->db_level * epbs));
3518 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
3519 db->db.db_size);
3521 DB_DNODE_EXIT(db);
3522 mutex_destroy(&dr->dt.di.dr_mtx);
3523 list_destroy(&dr->dt.di.dr_children);
3525 kmem_free(dr, sizeof (dbuf_dirty_record_t));
3527 cv_broadcast(&db->db_changed);
3528 ASSERT(db->db_dirtycnt > 0);
3529 db->db_dirtycnt -= 1;
3530 db->db_data_pending = NULL;
3531 dbuf_rele_and_unlock(db, (void *)(uintptr_t)tx->tx_txg, B_FALSE);
3534 static void
3535 dbuf_write_nofill_ready(zio_t *zio)
3537 dbuf_write_ready(zio, NULL, zio->io_private);
3540 static void
3541 dbuf_write_nofill_done(zio_t *zio)
3543 dbuf_write_done(zio, NULL, zio->io_private);
3546 static void
3547 dbuf_write_override_ready(zio_t *zio)
3549 dbuf_dirty_record_t *dr = zio->io_private;
3550 dmu_buf_impl_t *db = dr->dr_dbuf;
3552 dbuf_write_ready(zio, NULL, db);
3555 static void
3556 dbuf_write_override_done(zio_t *zio)
3558 dbuf_dirty_record_t *dr = zio->io_private;
3559 dmu_buf_impl_t *db = dr->dr_dbuf;
3560 blkptr_t *obp = &dr->dt.dl.dr_overridden_by;
3562 mutex_enter(&db->db_mtx);
3563 if (!BP_EQUAL(zio->io_bp, obp)) {
3564 if (!BP_IS_HOLE(obp))
3565 dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp);
3566 arc_release(dr->dt.dl.dr_data, db);
3568 mutex_exit(&db->db_mtx);
3569 dbuf_write_done(zio, NULL, db);
3571 if (zio->io_abd != NULL)
3572 abd_put(zio->io_abd);
3575 typedef struct dbuf_remap_impl_callback_arg {
3576 objset_t *drica_os;
3577 uint64_t drica_blk_birth;
3578 dmu_tx_t *drica_tx;
3579 } dbuf_remap_impl_callback_arg_t;
3581 static void
3582 dbuf_remap_impl_callback(uint64_t vdev, uint64_t offset, uint64_t size,
3583 void *arg)
3585 dbuf_remap_impl_callback_arg_t *drica = arg;
3586 objset_t *os = drica->drica_os;
3587 spa_t *spa = dmu_objset_spa(os);
3588 dmu_tx_t *tx = drica->drica_tx;
3590 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa)));
3592 if (os == spa_meta_objset(spa)) {
3593 spa_vdev_indirect_mark_obsolete(spa, vdev, offset, size, tx);
3594 } else {
3595 dsl_dataset_block_remapped(dmu_objset_ds(os), vdev, offset,
3596 size, drica->drica_blk_birth, tx);
3600 static void
3601 dbuf_remap_impl(dnode_t *dn, blkptr_t *bp, dmu_tx_t *tx)
3603 blkptr_t bp_copy = *bp;
3604 spa_t *spa = dmu_objset_spa(dn->dn_objset);
3605 dbuf_remap_impl_callback_arg_t drica;
3607 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa)));
3609 drica.drica_os = dn->dn_objset;
3610 drica.drica_blk_birth = bp->blk_birth;
3611 drica.drica_tx = tx;
3612 if (spa_remap_blkptr(spa, &bp_copy, dbuf_remap_impl_callback,
3613 &drica)) {
3615 * The struct_rwlock prevents dbuf_read_impl() from
3616 * dereferencing the BP while we are changing it. To
3617 * avoid lock contention, only grab it when we are actually
3618 * changing the BP.
3620 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
3621 *bp = bp_copy;
3622 rw_exit(&dn->dn_struct_rwlock);
3627 * Returns true if a dbuf_remap would modify the dbuf. We do this by attempting
3628 * to remap a copy of every bp in the dbuf.
3630 boolean_t
3631 dbuf_can_remap(const dmu_buf_impl_t *db)
3633 spa_t *spa = dmu_objset_spa(db->db_objset);
3634 blkptr_t *bp = db->db.db_data;
3635 boolean_t ret = B_FALSE;
3637 ASSERT3U(db->db_level, >, 0);
3638 ASSERT3S(db->db_state, ==, DB_CACHED);
3640 ASSERT(spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL));
3642 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3643 for (int i = 0; i < db->db.db_size >> SPA_BLKPTRSHIFT; i++) {
3644 blkptr_t bp_copy = bp[i];
3645 if (spa_remap_blkptr(spa, &bp_copy, NULL, NULL)) {
3646 ret = B_TRUE;
3647 break;
3650 spa_config_exit(spa, SCL_VDEV, FTAG);
3652 return (ret);
3655 boolean_t
3656 dnode_needs_remap(const dnode_t *dn)
3658 spa_t *spa = dmu_objset_spa(dn->dn_objset);
3659 boolean_t ret = B_FALSE;
3661 if (dn->dn_phys->dn_nlevels == 0) {
3662 return (B_FALSE);
3665 ASSERT(spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL));
3667 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3668 for (int j = 0; j < dn->dn_phys->dn_nblkptr; j++) {
3669 blkptr_t bp_copy = dn->dn_phys->dn_blkptr[j];
3670 if (spa_remap_blkptr(spa, &bp_copy, NULL, NULL)) {
3671 ret = B_TRUE;
3672 break;
3675 spa_config_exit(spa, SCL_VDEV, FTAG);
3677 return (ret);
3681 * Remap any existing BP's to concrete vdevs, if possible.
3683 static void
3684 dbuf_remap(dnode_t *dn, dmu_buf_impl_t *db, dmu_tx_t *tx)
3686 spa_t *spa = dmu_objset_spa(db->db_objset);
3687 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa)));
3689 if (!spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL))
3690 return;
3692 if (db->db_level > 0) {
3693 blkptr_t *bp = db->db.db_data;
3694 for (int i = 0; i < db->db.db_size >> SPA_BLKPTRSHIFT; i++) {
3695 dbuf_remap_impl(dn, &bp[i], tx);
3697 } else if (db->db.db_object == DMU_META_DNODE_OBJECT) {
3698 dnode_phys_t *dnp = db->db.db_data;
3699 ASSERT3U(db->db_dnode_handle->dnh_dnode->dn_type, ==,
3700 DMU_OT_DNODE);
3701 for (int i = 0; i < db->db.db_size >> DNODE_SHIFT; i++) {
3702 for (int j = 0; j < dnp[i].dn_nblkptr; j++) {
3703 dbuf_remap_impl(dn, &dnp[i].dn_blkptr[j], tx);
3710 /* Issue I/O to commit a dirty buffer to disk. */
3711 static void
3712 dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx)
3714 dmu_buf_impl_t *db = dr->dr_dbuf;
3715 dnode_t *dn;
3716 objset_t *os;
3717 dmu_buf_impl_t *parent = db->db_parent;
3718 uint64_t txg = tx->tx_txg;
3719 zbookmark_phys_t zb;
3720 zio_prop_t zp;
3721 zio_t *zio;
3722 int wp_flag = 0;
3724 ASSERT(dmu_tx_is_syncing(tx));
3726 DB_DNODE_ENTER(db);
3727 dn = DB_DNODE(db);
3728 os = dn->dn_objset;
3730 if (db->db_state != DB_NOFILL) {
3731 if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) {
3733 * Private object buffers are released here rather
3734 * than in dbuf_dirty() since they are only modified
3735 * in the syncing context and we don't want the
3736 * overhead of making multiple copies of the data.
3738 if (BP_IS_HOLE(db->db_blkptr)) {
3739 arc_buf_thaw(data);
3740 } else {
3741 dbuf_release_bp(db);
3743 dbuf_remap(dn, db, tx);
3747 if (parent != dn->dn_dbuf) {
3748 /* Our parent is an indirect block. */
3749 /* We have a dirty parent that has been scheduled for write. */
3750 ASSERT(parent && parent->db_data_pending);
3751 /* Our parent's buffer is one level closer to the dnode. */
3752 ASSERT(db->db_level == parent->db_level-1);
3754 * We're about to modify our parent's db_data by modifying
3755 * our block pointer, so the parent must be released.
3757 ASSERT(arc_released(parent->db_buf));
3758 zio = parent->db_data_pending->dr_zio;
3759 } else {
3760 /* Our parent is the dnode itself. */
3761 ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 &&
3762 db->db_blkid != DMU_SPILL_BLKID) ||
3763 (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0));
3764 if (db->db_blkid != DMU_SPILL_BLKID)
3765 ASSERT3P(db->db_blkptr, ==,
3766 &dn->dn_phys->dn_blkptr[db->db_blkid]);
3767 zio = dn->dn_zio;
3770 ASSERT(db->db_level == 0 || data == db->db_buf);
3771 ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
3772 ASSERT(zio);
3774 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
3775 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
3776 db->db.db_object, db->db_level, db->db_blkid);
3778 if (db->db_blkid == DMU_SPILL_BLKID)
3779 wp_flag = WP_SPILL;
3780 wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
3782 dmu_write_policy(os, dn, db->db_level, wp_flag, &zp);
3783 DB_DNODE_EXIT(db);
3786 * We copy the blkptr now (rather than when we instantiate the dirty
3787 * record), because its value can change between open context and
3788 * syncing context. We do not need to hold dn_struct_rwlock to read
3789 * db_blkptr because we are in syncing context.
3791 dr->dr_bp_copy = *db->db_blkptr;
3793 if (db->db_level == 0 &&
3794 dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
3796 * The BP for this block has been provided by open context
3797 * (by dmu_sync() or dmu_buf_write_embedded()).
3799 abd_t *contents = (data != NULL) ?
3800 abd_get_from_buf(data->b_data, arc_buf_size(data)) : NULL;
3802 dr->dr_zio = zio_write(zio, os->os_spa, txg, &dr->dr_bp_copy,
3803 contents, db->db.db_size, db->db.db_size, &zp,
3804 dbuf_write_override_ready, NULL, NULL,
3805 dbuf_write_override_done,
3806 dr, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
3807 mutex_enter(&db->db_mtx);
3808 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
3809 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by,
3810 dr->dt.dl.dr_copies, dr->dt.dl.dr_nopwrite);
3811 mutex_exit(&db->db_mtx);
3812 } else if (db->db_state == DB_NOFILL) {
3813 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF ||
3814 zp.zp_checksum == ZIO_CHECKSUM_NOPARITY);
3815 dr->dr_zio = zio_write(zio, os->os_spa, txg,
3816 &dr->dr_bp_copy, NULL, db->db.db_size, db->db.db_size, &zp,
3817 dbuf_write_nofill_ready, NULL, NULL,
3818 dbuf_write_nofill_done, db,
3819 ZIO_PRIORITY_ASYNC_WRITE,
3820 ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb);
3821 } else {
3822 ASSERT(arc_released(data));
3825 * For indirect blocks, we want to setup the children
3826 * ready callback so that we can properly handle an indirect
3827 * block that only contains holes.
3829 arc_done_func_t *children_ready_cb = NULL;
3830 if (db->db_level != 0)
3831 children_ready_cb = dbuf_write_children_ready;
3833 dr->dr_zio = arc_write(zio, os->os_spa, txg,
3834 &dr->dr_bp_copy, data, DBUF_IS_L2CACHEABLE(db),
3835 &zp, dbuf_write_ready, children_ready_cb,
3836 dbuf_write_physdone, dbuf_write_done, db,
3837 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);