8070 Add some ZFS comments
[unleashed.git] / usr / src / uts / common / fs / zfs / dbuf.c
blobe039135aef0a153c25b0b89f8fb43d98bbfa18b1
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, 2017 by Delphix. All rights reserved.
25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28 * Copyright (c) 2014 Integros [integros.com]
31 #include <sys/zfs_context.h>
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>
50 uint_t zfs_dbuf_evict_key;
52 static boolean_t dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
53 static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx);
55 #ifndef __lint
56 extern inline void dmu_buf_init_user(dmu_buf_user_t *dbu,
57 dmu_buf_evict_func_t *evict_func_sync,
58 dmu_buf_evict_func_t *evict_func_async,
59 dmu_buf_t **clear_on_evict_dbufp);
60 #endif /* ! __lint */
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 * LRU cache of dbufs. The dbuf cache maintains a list of dbufs that
75 * are not currently held but have been recently released. These dbufs
76 * are not eligible for arc eviction until they are aged out of the cache.
77 * Dbufs are added to the dbuf cache once the last hold is released. If a
78 * dbuf is later accessed and still exists in the dbuf cache, then it will
79 * be removed from the cache and later re-added to the head of the cache.
80 * Dbufs that are aged out of the cache will be immediately destroyed and
81 * become eligible for arc eviction.
83 static multilist_t *dbuf_cache;
84 static refcount_t dbuf_cache_size;
85 uint64_t dbuf_cache_max_bytes = 100 * 1024 * 1024;
87 /* Cap the size of the dbuf cache to log2 fraction of arc size. */
88 int dbuf_cache_max_shift = 5;
91 * The dbuf cache uses a three-stage eviction policy:
92 * - A low water marker designates when the dbuf eviction thread
93 * should stop evicting from the dbuf cache.
94 * - When we reach the maximum size (aka mid water mark), we
95 * signal the eviction thread to run.
96 * - The high water mark indicates when the eviction thread
97 * is unable to keep up with the incoming load and eviction must
98 * happen in the context of the calling thread.
100 * The dbuf cache:
101 * (max size)
102 * low water mid water hi water
103 * +----------------------------------------+----------+----------+
104 * | | | |
105 * | | | |
106 * | | | |
107 * | | | |
108 * +----------------------------------------+----------+----------+
109 * stop signal evict
110 * evicting eviction directly
111 * thread
113 * The high and low water marks indicate the operating range for the eviction
114 * thread. The low water mark is, by default, 90% of the total size of the
115 * cache and the high water mark is at 110% (both of these percentages can be
116 * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct,
117 * respectively). The eviction thread will try to ensure that the cache remains
118 * within this range by waking up every second and checking if the cache is
119 * above the low water mark. The thread can also be woken up by callers adding
120 * elements into the cache if the cache is larger than the mid water (i.e max
121 * cache size). Once the eviction thread is woken up and eviction is required,
122 * it will continue evicting buffers until it's able to reduce the cache size
123 * to the low water mark. If the cache size continues to grow and hits the high
124 * water mark, then callers adding elments to the cache will begin to evict
125 * directly from the cache until the cache is no longer above the high water
126 * mark.
130 * The percentage above and below the maximum cache size.
132 uint_t dbuf_cache_hiwater_pct = 10;
133 uint_t dbuf_cache_lowater_pct = 10;
135 /* ARGSUSED */
136 static int
137 dbuf_cons(void *vdb, void *unused, int kmflag)
139 dmu_buf_impl_t *db = vdb;
140 bzero(db, sizeof (dmu_buf_impl_t));
142 mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL);
143 cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL);
144 multilist_link_init(&db->db_cache_link);
145 refcount_create(&db->db_holds);
147 return (0);
150 /* ARGSUSED */
151 static void
152 dbuf_dest(void *vdb, void *unused)
154 dmu_buf_impl_t *db = vdb;
155 mutex_destroy(&db->db_mtx);
156 cv_destroy(&db->db_changed);
157 ASSERT(!multilist_link_active(&db->db_cache_link));
158 refcount_destroy(&db->db_holds);
162 * dbuf hash table routines
164 static dbuf_hash_table_t dbuf_hash_table;
166 static uint64_t dbuf_hash_count;
168 static uint64_t
169 dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid)
171 uintptr_t osv = (uintptr_t)os;
172 uint64_t crc = -1ULL;
174 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
175 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (lvl)) & 0xFF];
176 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF];
177 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF];
178 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF];
179 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 0)) & 0xFF];
180 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 8)) & 0xFF];
182 crc ^= (osv>>14) ^ (obj>>16) ^ (blkid>>16);
184 return (crc);
187 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
188 ((dbuf)->db.db_object == (obj) && \
189 (dbuf)->db_objset == (os) && \
190 (dbuf)->db_level == (level) && \
191 (dbuf)->db_blkid == (blkid))
193 dmu_buf_impl_t *
194 dbuf_find(objset_t *os, uint64_t obj, uint8_t level, uint64_t blkid)
196 dbuf_hash_table_t *h = &dbuf_hash_table;
197 uint64_t hv = dbuf_hash(os, obj, level, blkid);
198 uint64_t idx = hv & h->hash_table_mask;
199 dmu_buf_impl_t *db;
201 mutex_enter(DBUF_HASH_MUTEX(h, idx));
202 for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) {
203 if (DBUF_EQUAL(db, os, obj, level, blkid)) {
204 mutex_enter(&db->db_mtx);
205 if (db->db_state != DB_EVICTING) {
206 mutex_exit(DBUF_HASH_MUTEX(h, idx));
207 return (db);
209 mutex_exit(&db->db_mtx);
212 mutex_exit(DBUF_HASH_MUTEX(h, idx));
213 return (NULL);
216 static dmu_buf_impl_t *
217 dbuf_find_bonus(objset_t *os, uint64_t object)
219 dnode_t *dn;
220 dmu_buf_impl_t *db = NULL;
222 if (dnode_hold(os, object, FTAG, &dn) == 0) {
223 rw_enter(&dn->dn_struct_rwlock, RW_READER);
224 if (dn->dn_bonus != NULL) {
225 db = dn->dn_bonus;
226 mutex_enter(&db->db_mtx);
228 rw_exit(&dn->dn_struct_rwlock);
229 dnode_rele(dn, FTAG);
231 return (db);
235 * Insert an entry into the hash table. If there is already an element
236 * equal to elem in the hash table, then the already existing element
237 * will be returned and the new element will not be inserted.
238 * Otherwise returns NULL.
240 static dmu_buf_impl_t *
241 dbuf_hash_insert(dmu_buf_impl_t *db)
243 dbuf_hash_table_t *h = &dbuf_hash_table;
244 objset_t *os = db->db_objset;
245 uint64_t obj = db->db.db_object;
246 int level = db->db_level;
247 uint64_t blkid = db->db_blkid;
248 uint64_t hv = dbuf_hash(os, obj, level, blkid);
249 uint64_t idx = hv & h->hash_table_mask;
250 dmu_buf_impl_t *dbf;
252 mutex_enter(DBUF_HASH_MUTEX(h, idx));
253 for (dbf = h->hash_table[idx]; dbf != NULL; dbf = dbf->db_hash_next) {
254 if (DBUF_EQUAL(dbf, os, obj, level, blkid)) {
255 mutex_enter(&dbf->db_mtx);
256 if (dbf->db_state != DB_EVICTING) {
257 mutex_exit(DBUF_HASH_MUTEX(h, idx));
258 return (dbf);
260 mutex_exit(&dbf->db_mtx);
264 mutex_enter(&db->db_mtx);
265 db->db_hash_next = h->hash_table[idx];
266 h->hash_table[idx] = db;
267 mutex_exit(DBUF_HASH_MUTEX(h, idx));
268 atomic_inc_64(&dbuf_hash_count);
270 return (NULL);
274 * Remove an entry from the hash table. It must be in the EVICTING state.
276 static void
277 dbuf_hash_remove(dmu_buf_impl_t *db)
279 dbuf_hash_table_t *h = &dbuf_hash_table;
280 uint64_t hv = dbuf_hash(db->db_objset, db->db.db_object,
281 db->db_level, db->db_blkid);
282 uint64_t idx = hv & h->hash_table_mask;
283 dmu_buf_impl_t *dbf, **dbp;
286 * We musn't hold db_mtx to maintain lock ordering:
287 * DBUF_HASH_MUTEX > db_mtx.
289 ASSERT(refcount_is_zero(&db->db_holds));
290 ASSERT(db->db_state == DB_EVICTING);
291 ASSERT(!MUTEX_HELD(&db->db_mtx));
293 mutex_enter(DBUF_HASH_MUTEX(h, idx));
294 dbp = &h->hash_table[idx];
295 while ((dbf = *dbp) != db) {
296 dbp = &dbf->db_hash_next;
297 ASSERT(dbf != NULL);
299 *dbp = db->db_hash_next;
300 db->db_hash_next = NULL;
301 mutex_exit(DBUF_HASH_MUTEX(h, idx));
302 atomic_dec_64(&dbuf_hash_count);
305 typedef enum {
306 DBVU_EVICTING,
307 DBVU_NOT_EVICTING
308 } dbvu_verify_type_t;
310 static void
311 dbuf_verify_user(dmu_buf_impl_t *db, dbvu_verify_type_t verify_type)
313 #ifdef ZFS_DEBUG
314 int64_t holds;
316 if (db->db_user == NULL)
317 return;
319 /* Only data blocks support the attachment of user data. */
320 ASSERT(db->db_level == 0);
322 /* Clients must resolve a dbuf before attaching user data. */
323 ASSERT(db->db.db_data != NULL);
324 ASSERT3U(db->db_state, ==, DB_CACHED);
326 holds = refcount_count(&db->db_holds);
327 if (verify_type == DBVU_EVICTING) {
329 * Immediate eviction occurs when holds == dirtycnt.
330 * For normal eviction buffers, holds is zero on
331 * eviction, except when dbuf_fix_old_data() calls
332 * dbuf_clear_data(). However, the hold count can grow
333 * during eviction even though db_mtx is held (see
334 * dmu_bonus_hold() for an example), so we can only
335 * test the generic invariant that holds >= dirtycnt.
337 ASSERT3U(holds, >=, db->db_dirtycnt);
338 } else {
339 if (db->db_user_immediate_evict == TRUE)
340 ASSERT3U(holds, >=, db->db_dirtycnt);
341 else
342 ASSERT3U(holds, >, 0);
344 #endif
347 static void
348 dbuf_evict_user(dmu_buf_impl_t *db)
350 dmu_buf_user_t *dbu = db->db_user;
352 ASSERT(MUTEX_HELD(&db->db_mtx));
354 if (dbu == NULL)
355 return;
357 dbuf_verify_user(db, DBVU_EVICTING);
358 db->db_user = NULL;
360 #ifdef ZFS_DEBUG
361 if (dbu->dbu_clear_on_evict_dbufp != NULL)
362 *dbu->dbu_clear_on_evict_dbufp = NULL;
363 #endif
366 * There are two eviction callbacks - one that we call synchronously
367 * and one that we invoke via a taskq. The async one is useful for
368 * avoiding lock order reversals and limiting stack depth.
370 * Note that if we have a sync callback but no async callback,
371 * it's likely that the sync callback will free the structure
372 * containing the dbu. In that case we need to take care to not
373 * dereference dbu after calling the sync evict func.
375 boolean_t has_async = (dbu->dbu_evict_func_async != NULL);
377 if (dbu->dbu_evict_func_sync != NULL)
378 dbu->dbu_evict_func_sync(dbu);
380 if (has_async) {
381 taskq_dispatch_ent(dbu_evict_taskq, dbu->dbu_evict_func_async,
382 dbu, 0, &dbu->dbu_tqent);
386 boolean_t
387 dbuf_is_metadata(dmu_buf_impl_t *db)
389 if (db->db_level > 0) {
390 return (B_TRUE);
391 } else {
392 boolean_t is_metadata;
394 DB_DNODE_ENTER(db);
395 is_metadata = DMU_OT_IS_METADATA(DB_DNODE(db)->dn_type);
396 DB_DNODE_EXIT(db);
398 return (is_metadata);
403 * This function *must* return indices evenly distributed between all
404 * sublists of the multilist. This is needed due to how the dbuf eviction
405 * code is laid out; dbuf_evict_thread() assumes dbufs are evenly
406 * distributed between all sublists and uses this assumption when
407 * deciding which sublist to evict from and how much to evict from it.
409 unsigned int
410 dbuf_cache_multilist_index_func(multilist_t *ml, void *obj)
412 dmu_buf_impl_t *db = obj;
415 * The assumption here, is the hash value for a given
416 * dmu_buf_impl_t will remain constant throughout it's lifetime
417 * (i.e. it's objset, object, level and blkid fields don't change).
418 * Thus, we don't need to store the dbuf's sublist index
419 * on insertion, as this index can be recalculated on removal.
421 * Also, the low order bits of the hash value are thought to be
422 * distributed evenly. Otherwise, in the case that the multilist
423 * has a power of two number of sublists, each sublists' usage
424 * would not be evenly distributed.
426 return (dbuf_hash(db->db_objset, db->db.db_object,
427 db->db_level, db->db_blkid) %
428 multilist_get_num_sublists(ml));
431 static inline boolean_t
432 dbuf_cache_above_hiwater(void)
434 uint64_t dbuf_cache_hiwater_bytes =
435 (dbuf_cache_max_bytes * dbuf_cache_hiwater_pct) / 100;
437 return (refcount_count(&dbuf_cache_size) >
438 dbuf_cache_max_bytes + dbuf_cache_hiwater_bytes);
441 static inline boolean_t
442 dbuf_cache_above_lowater(void)
444 uint64_t dbuf_cache_lowater_bytes =
445 (dbuf_cache_max_bytes * dbuf_cache_lowater_pct) / 100;
447 return (refcount_count(&dbuf_cache_size) >
448 dbuf_cache_max_bytes - dbuf_cache_lowater_bytes);
452 * Evict the oldest eligible dbuf from the dbuf cache.
454 static void
455 dbuf_evict_one(void)
457 int idx = multilist_get_random_index(dbuf_cache);
458 multilist_sublist_t *mls = multilist_sublist_lock(dbuf_cache, idx);
460 ASSERT(!MUTEX_HELD(&dbuf_evict_lock));
463 * Set the thread's tsd to indicate that it's processing evictions.
464 * Once a thread stops evicting from the dbuf cache it will
465 * reset its tsd to NULL.
467 ASSERT3P(tsd_get(zfs_dbuf_evict_key), ==, NULL);
468 (void) tsd_set(zfs_dbuf_evict_key, (void *)B_TRUE);
470 dmu_buf_impl_t *db = multilist_sublist_tail(mls);
471 while (db != NULL && mutex_tryenter(&db->db_mtx) == 0) {
472 db = multilist_sublist_prev(mls, db);
475 DTRACE_PROBE2(dbuf__evict__one, dmu_buf_impl_t *, db,
476 multilist_sublist_t *, mls);
478 if (db != NULL) {
479 multilist_sublist_remove(mls, db);
480 multilist_sublist_unlock(mls);
481 (void) refcount_remove_many(&dbuf_cache_size,
482 db->db.db_size, db);
483 dbuf_destroy(db);
484 } else {
485 multilist_sublist_unlock(mls);
487 (void) tsd_set(zfs_dbuf_evict_key, NULL);
491 * The dbuf evict thread is responsible for aging out dbufs from the
492 * cache. Once the cache has reached it's maximum size, dbufs are removed
493 * and destroyed. The eviction thread will continue running until the size
494 * of the dbuf cache is at or below the maximum size. Once the dbuf is aged
495 * out of the cache it is destroyed and becomes eligible for arc eviction.
497 static void
498 dbuf_evict_thread(void)
500 callb_cpr_t cpr;
502 CALLB_CPR_INIT(&cpr, &dbuf_evict_lock, callb_generic_cpr, FTAG);
504 mutex_enter(&dbuf_evict_lock);
505 while (!dbuf_evict_thread_exit) {
506 while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit) {
507 CALLB_CPR_SAFE_BEGIN(&cpr);
508 (void) cv_timedwait_hires(&dbuf_evict_cv,
509 &dbuf_evict_lock, SEC2NSEC(1), MSEC2NSEC(1), 0);
510 CALLB_CPR_SAFE_END(&cpr, &dbuf_evict_lock);
512 mutex_exit(&dbuf_evict_lock);
515 * Keep evicting as long as we're above the low water mark
516 * for the cache. We do this without holding the locks to
517 * minimize lock contention.
519 while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit) {
520 dbuf_evict_one();
523 mutex_enter(&dbuf_evict_lock);
526 dbuf_evict_thread_exit = B_FALSE;
527 cv_broadcast(&dbuf_evict_cv);
528 CALLB_CPR_EXIT(&cpr); /* drops dbuf_evict_lock */
529 thread_exit();
533 * Wake up the dbuf eviction thread if the dbuf cache is at its max size.
534 * If the dbuf cache is at its high water mark, then evict a dbuf from the
535 * dbuf cache using the callers context.
537 static void
538 dbuf_evict_notify(void)
542 * We use thread specific data to track when a thread has
543 * started processing evictions. This allows us to avoid deeply
544 * nested stacks that would have a call flow similar to this:
546 * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify()
547 * ^ |
548 * | |
549 * +-----dbuf_destroy()<--dbuf_evict_one()<--------+
551 * The dbuf_eviction_thread will always have its tsd set until
552 * that thread exits. All other threads will only set their tsd
553 * if they are participating in the eviction process. This only
554 * happens if the eviction thread is unable to process evictions
555 * fast enough. To keep the dbuf cache size in check, other threads
556 * can evict from the dbuf cache directly. Those threads will set
557 * their tsd values so that we ensure that they only evict one dbuf
558 * from the dbuf cache.
560 if (tsd_get(zfs_dbuf_evict_key) != NULL)
561 return;
563 if (refcount_count(&dbuf_cache_size) > dbuf_cache_max_bytes) {
564 boolean_t evict_now = B_FALSE;
566 mutex_enter(&dbuf_evict_lock);
567 if (refcount_count(&dbuf_cache_size) > dbuf_cache_max_bytes) {
568 evict_now = dbuf_cache_above_hiwater();
569 cv_signal(&dbuf_evict_cv);
571 mutex_exit(&dbuf_evict_lock);
573 if (evict_now) {
574 dbuf_evict_one();
579 void
580 dbuf_init(void)
582 uint64_t hsize = 1ULL << 16;
583 dbuf_hash_table_t *h = &dbuf_hash_table;
584 int i;
587 * The hash table is big enough to fill all of physical memory
588 * with an average 4K block size. The table will take up
589 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
591 while (hsize * 4096 < physmem * PAGESIZE)
592 hsize <<= 1;
594 retry:
595 h->hash_table_mask = hsize - 1;
596 h->hash_table = kmem_zalloc(hsize * sizeof (void *), KM_NOSLEEP);
597 if (h->hash_table == NULL) {
598 /* XXX - we should really return an error instead of assert */
599 ASSERT(hsize > (1ULL << 10));
600 hsize >>= 1;
601 goto retry;
604 dbuf_kmem_cache = kmem_cache_create("dmu_buf_impl_t",
605 sizeof (dmu_buf_impl_t),
606 0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0);
608 for (i = 0; i < DBUF_MUTEXES; i++)
609 mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL);
612 * Setup the parameters for the dbuf cache. We cap the size of the
613 * dbuf cache to 1/32nd (default) of the size of the ARC.
615 dbuf_cache_max_bytes = MIN(dbuf_cache_max_bytes,
616 arc_max_bytes() >> dbuf_cache_max_shift);
619 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
620 * configuration is not required.
622 dbu_evict_taskq = taskq_create("dbu_evict", 1, minclsyspri, 0, 0, 0);
624 dbuf_cache = multilist_create(sizeof (dmu_buf_impl_t),
625 offsetof(dmu_buf_impl_t, db_cache_link),
626 dbuf_cache_multilist_index_func);
627 refcount_create(&dbuf_cache_size);
629 tsd_create(&zfs_dbuf_evict_key, NULL);
630 dbuf_evict_thread_exit = B_FALSE;
631 mutex_init(&dbuf_evict_lock, NULL, MUTEX_DEFAULT, NULL);
632 cv_init(&dbuf_evict_cv, NULL, CV_DEFAULT, NULL);
633 dbuf_cache_evict_thread = thread_create(NULL, 0, dbuf_evict_thread,
634 NULL, 0, &p0, TS_RUN, minclsyspri);
637 void
638 dbuf_fini(void)
640 dbuf_hash_table_t *h = &dbuf_hash_table;
641 int i;
643 for (i = 0; i < DBUF_MUTEXES; i++)
644 mutex_destroy(&h->hash_mutexes[i]);
645 kmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
646 kmem_cache_destroy(dbuf_kmem_cache);
647 taskq_destroy(dbu_evict_taskq);
649 mutex_enter(&dbuf_evict_lock);
650 dbuf_evict_thread_exit = B_TRUE;
651 while (dbuf_evict_thread_exit) {
652 cv_signal(&dbuf_evict_cv);
653 cv_wait(&dbuf_evict_cv, &dbuf_evict_lock);
655 mutex_exit(&dbuf_evict_lock);
656 tsd_destroy(&zfs_dbuf_evict_key);
658 mutex_destroy(&dbuf_evict_lock);
659 cv_destroy(&dbuf_evict_cv);
661 refcount_destroy(&dbuf_cache_size);
662 multilist_destroy(dbuf_cache);
666 * Other stuff.
669 #ifdef ZFS_DEBUG
670 static void
671 dbuf_verify(dmu_buf_impl_t *db)
673 dnode_t *dn;
674 dbuf_dirty_record_t *dr;
676 ASSERT(MUTEX_HELD(&db->db_mtx));
678 if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY))
679 return;
681 ASSERT(db->db_objset != NULL);
682 DB_DNODE_ENTER(db);
683 dn = DB_DNODE(db);
684 if (dn == NULL) {
685 ASSERT(db->db_parent == NULL);
686 ASSERT(db->db_blkptr == NULL);
687 } else {
688 ASSERT3U(db->db.db_object, ==, dn->dn_object);
689 ASSERT3P(db->db_objset, ==, dn->dn_objset);
690 ASSERT3U(db->db_level, <, dn->dn_nlevels);
691 ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
692 db->db_blkid == DMU_SPILL_BLKID ||
693 !avl_is_empty(&dn->dn_dbufs));
695 if (db->db_blkid == DMU_BONUS_BLKID) {
696 ASSERT(dn != NULL);
697 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
698 ASSERT3U(db->db.db_offset, ==, DMU_BONUS_BLKID);
699 } else if (db->db_blkid == DMU_SPILL_BLKID) {
700 ASSERT(dn != NULL);
701 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
702 ASSERT0(db->db.db_offset);
703 } else {
704 ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size);
707 for (dr = db->db_data_pending; dr != NULL; dr = dr->dr_next)
708 ASSERT(dr->dr_dbuf == db);
710 for (dr = db->db_last_dirty; dr != NULL; dr = dr->dr_next)
711 ASSERT(dr->dr_dbuf == db);
714 * We can't assert that db_size matches dn_datablksz because it
715 * can be momentarily different when another thread is doing
716 * dnode_set_blksz().
718 if (db->db_level == 0 && db->db.db_object == DMU_META_DNODE_OBJECT) {
719 dr = db->db_data_pending;
721 * It should only be modified in syncing context, so
722 * make sure we only have one copy of the data.
724 ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf);
727 /* verify db->db_blkptr */
728 if (db->db_blkptr) {
729 if (db->db_parent == dn->dn_dbuf) {
730 /* db is pointed to by the dnode */
731 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
732 if (DMU_OBJECT_IS_SPECIAL(db->db.db_object))
733 ASSERT(db->db_parent == NULL);
734 else
735 ASSERT(db->db_parent != NULL);
736 if (db->db_blkid != DMU_SPILL_BLKID)
737 ASSERT3P(db->db_blkptr, ==,
738 &dn->dn_phys->dn_blkptr[db->db_blkid]);
739 } else {
740 /* db is pointed to by an indirect block */
741 int epb = db->db_parent->db.db_size >> SPA_BLKPTRSHIFT;
742 ASSERT3U(db->db_parent->db_level, ==, db->db_level+1);
743 ASSERT3U(db->db_parent->db.db_object, ==,
744 db->db.db_object);
746 * dnode_grow_indblksz() can make this fail if we don't
747 * have the struct_rwlock. XXX indblksz no longer
748 * grows. safe to do this now?
750 if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
751 ASSERT3P(db->db_blkptr, ==,
752 ((blkptr_t *)db->db_parent->db.db_data +
753 db->db_blkid % epb));
757 if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) &&
758 (db->db_buf == NULL || db->db_buf->b_data) &&
759 db->db.db_data && db->db_blkid != DMU_BONUS_BLKID &&
760 db->db_state != DB_FILL && !dn->dn_free_txg) {
762 * If the blkptr isn't set but they have nonzero data,
763 * it had better be dirty, otherwise we'll lose that
764 * data when we evict this buffer.
766 * There is an exception to this rule for indirect blocks; in
767 * this case, if the indirect block is a hole, we fill in a few
768 * fields on each of the child blocks (importantly, birth time)
769 * to prevent hole birth times from being lost when you
770 * partially fill in a hole.
772 if (db->db_dirtycnt == 0) {
773 if (db->db_level == 0) {
774 uint64_t *buf = db->db.db_data;
775 int i;
777 for (i = 0; i < db->db.db_size >> 3; i++) {
778 ASSERT(buf[i] == 0);
780 } else {
781 blkptr_t *bps = db->db.db_data;
782 ASSERT3U(1 << DB_DNODE(db)->dn_indblkshift, ==,
783 db->db.db_size);
785 * We want to verify that all the blkptrs in the
786 * indirect block are holes, but we may have
787 * automatically set up a few fields for them.
788 * We iterate through each blkptr and verify
789 * they only have those fields set.
791 for (int i = 0;
792 i < db->db.db_size / sizeof (blkptr_t);
793 i++) {
794 blkptr_t *bp = &bps[i];
795 ASSERT(ZIO_CHECKSUM_IS_ZERO(
796 &bp->blk_cksum));
797 ASSERT(
798 DVA_IS_EMPTY(&bp->blk_dva[0]) &&
799 DVA_IS_EMPTY(&bp->blk_dva[1]) &&
800 DVA_IS_EMPTY(&bp->blk_dva[2]));
801 ASSERT0(bp->blk_fill);
802 ASSERT0(bp->blk_pad[0]);
803 ASSERT0(bp->blk_pad[1]);
804 ASSERT(!BP_IS_EMBEDDED(bp));
805 ASSERT(BP_IS_HOLE(bp));
806 ASSERT0(bp->blk_phys_birth);
811 DB_DNODE_EXIT(db);
813 #endif
815 static void
816 dbuf_clear_data(dmu_buf_impl_t *db)
818 ASSERT(MUTEX_HELD(&db->db_mtx));
819 dbuf_evict_user(db);
820 ASSERT3P(db->db_buf, ==, NULL);
821 db->db.db_data = NULL;
822 if (db->db_state != DB_NOFILL)
823 db->db_state = DB_UNCACHED;
826 static void
827 dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf)
829 ASSERT(MUTEX_HELD(&db->db_mtx));
830 ASSERT(buf != NULL);
832 db->db_buf = buf;
833 ASSERT(buf->b_data != NULL);
834 db->db.db_data = buf->b_data;
838 * Loan out an arc_buf for read. Return the loaned arc_buf.
840 arc_buf_t *
841 dbuf_loan_arcbuf(dmu_buf_impl_t *db)
843 arc_buf_t *abuf;
845 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
846 mutex_enter(&db->db_mtx);
847 if (arc_released(db->db_buf) || refcount_count(&db->db_holds) > 1) {
848 int blksz = db->db.db_size;
849 spa_t *spa = db->db_objset->os_spa;
851 mutex_exit(&db->db_mtx);
852 abuf = arc_loan_buf(spa, B_FALSE, blksz);
853 bcopy(db->db.db_data, abuf->b_data, blksz);
854 } else {
855 abuf = db->db_buf;
856 arc_loan_inuse_buf(abuf, db);
857 db->db_buf = NULL;
858 dbuf_clear_data(db);
859 mutex_exit(&db->db_mtx);
861 return (abuf);
865 * Calculate which level n block references the data at the level 0 offset
866 * provided.
868 uint64_t
869 dbuf_whichblock(dnode_t *dn, int64_t level, uint64_t offset)
871 if (dn->dn_datablkshift != 0 && dn->dn_indblkshift != 0) {
873 * The level n blkid is equal to the level 0 blkid divided by
874 * the number of level 0s in a level n block.
876 * The level 0 blkid is offset >> datablkshift =
877 * offset / 2^datablkshift.
879 * The number of level 0s in a level n is the number of block
880 * pointers in an indirect block, raised to the power of level.
881 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
882 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
884 * Thus, the level n blkid is: offset /
885 * ((2^datablkshift)*(2^(level*(indblkshift - SPA_BLKPTRSHIFT)))
886 * = offset / 2^(datablkshift + level *
887 * (indblkshift - SPA_BLKPTRSHIFT))
888 * = offset >> (datablkshift + level *
889 * (indblkshift - SPA_BLKPTRSHIFT))
891 return (offset >> (dn->dn_datablkshift + level *
892 (dn->dn_indblkshift - SPA_BLKPTRSHIFT)));
893 } else {
894 ASSERT3U(offset, <, dn->dn_datablksz);
895 return (0);
899 static void
900 dbuf_read_done(zio_t *zio, arc_buf_t *buf, void *vdb)
902 dmu_buf_impl_t *db = vdb;
904 mutex_enter(&db->db_mtx);
905 ASSERT3U(db->db_state, ==, DB_READ);
907 * All reads are synchronous, so we must have a hold on the dbuf
909 ASSERT(refcount_count(&db->db_holds) > 0);
910 ASSERT(db->db_buf == NULL);
911 ASSERT(db->db.db_data == NULL);
912 if (db->db_level == 0 && db->db_freed_in_flight) {
913 /* we were freed in flight; disregard any error */
914 arc_release(buf, db);
915 bzero(buf->b_data, db->db.db_size);
916 arc_buf_freeze(buf);
917 db->db_freed_in_flight = FALSE;
918 dbuf_set_data(db, buf);
919 db->db_state = DB_CACHED;
920 } else if (zio == NULL || zio->io_error == 0) {
921 dbuf_set_data(db, buf);
922 db->db_state = DB_CACHED;
923 } else {
924 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
925 ASSERT3P(db->db_buf, ==, NULL);
926 arc_buf_destroy(buf, db);
927 db->db_state = DB_UNCACHED;
929 cv_broadcast(&db->db_changed);
930 dbuf_rele_and_unlock(db, NULL);
933 static void
934 dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
936 dnode_t *dn;
937 zbookmark_phys_t zb;
938 arc_flags_t aflags = ARC_FLAG_NOWAIT;
940 DB_DNODE_ENTER(db);
941 dn = DB_DNODE(db);
942 ASSERT(!refcount_is_zero(&db->db_holds));
943 /* We need the struct_rwlock to prevent db_blkptr from changing. */
944 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
945 ASSERT(MUTEX_HELD(&db->db_mtx));
946 ASSERT(db->db_state == DB_UNCACHED);
947 ASSERT(db->db_buf == NULL);
949 if (db->db_blkid == DMU_BONUS_BLKID) {
950 int bonuslen = MIN(dn->dn_bonuslen, dn->dn_phys->dn_bonuslen);
952 ASSERT3U(bonuslen, <=, db->db.db_size);
953 db->db.db_data = zio_buf_alloc(DN_MAX_BONUSLEN);
954 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
955 if (bonuslen < DN_MAX_BONUSLEN)
956 bzero(db->db.db_data, DN_MAX_BONUSLEN);
957 if (bonuslen)
958 bcopy(DN_BONUS(dn->dn_phys), db->db.db_data, bonuslen);
959 DB_DNODE_EXIT(db);
960 db->db_state = DB_CACHED;
961 mutex_exit(&db->db_mtx);
962 return;
966 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
967 * processes the delete record and clears the bp while we are waiting
968 * for the dn_mtx (resulting in a "no" from block_freed).
970 if (db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr) ||
971 (db->db_level == 0 && (dnode_block_freed(dn, db->db_blkid) ||
972 BP_IS_HOLE(db->db_blkptr)))) {
973 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
975 dbuf_set_data(db, arc_alloc_buf(db->db_objset->os_spa, db, type,
976 db->db.db_size));
977 bzero(db->db.db_data, db->db.db_size);
979 if (db->db_blkptr != NULL && db->db_level > 0 &&
980 BP_IS_HOLE(db->db_blkptr) &&
981 db->db_blkptr->blk_birth != 0) {
982 blkptr_t *bps = db->db.db_data;
983 for (int i = 0; i < ((1 <<
984 DB_DNODE(db)->dn_indblkshift) / sizeof (blkptr_t));
985 i++) {
986 blkptr_t *bp = &bps[i];
987 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
988 1 << dn->dn_indblkshift);
989 BP_SET_LSIZE(bp,
990 BP_GET_LEVEL(db->db_blkptr) == 1 ?
991 dn->dn_datablksz :
992 BP_GET_LSIZE(db->db_blkptr));
993 BP_SET_TYPE(bp, BP_GET_TYPE(db->db_blkptr));
994 BP_SET_LEVEL(bp,
995 BP_GET_LEVEL(db->db_blkptr) - 1);
996 BP_SET_BIRTH(bp, db->db_blkptr->blk_birth, 0);
999 DB_DNODE_EXIT(db);
1000 db->db_state = DB_CACHED;
1001 mutex_exit(&db->db_mtx);
1002 return;
1005 DB_DNODE_EXIT(db);
1007 db->db_state = DB_READ;
1008 mutex_exit(&db->db_mtx);
1010 if (DBUF_IS_L2CACHEABLE(db))
1011 aflags |= ARC_FLAG_L2CACHE;
1013 SET_BOOKMARK(&zb, db->db_objset->os_dsl_dataset ?
1014 db->db_objset->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1015 db->db.db_object, db->db_level, db->db_blkid);
1017 dbuf_add_ref(db, NULL);
1019 (void) arc_read(zio, db->db_objset->os_spa, db->db_blkptr,
1020 dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ,
1021 (flags & DB_RF_CANFAIL) ? ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED,
1022 &aflags, &zb);
1026 * This is our just-in-time copy function. It makes a copy of buffers that
1027 * have been modified in a previous transaction group before we access them in
1028 * the current active group.
1030 * This function is used in three places: when we are dirtying a buffer for the
1031 * first time in a txg, when we are freeing a range in a dnode that includes
1032 * this buffer, and when we are accessing a buffer which was received compressed
1033 * and later referenced in a WRITE_BYREF record.
1035 * Note that when we are called from dbuf_free_range() we do not put a hold on
1036 * the buffer, we just traverse the active dbuf list for the dnode.
1038 static void
1039 dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg)
1041 dbuf_dirty_record_t *dr = db->db_last_dirty;
1043 ASSERT(MUTEX_HELD(&db->db_mtx));
1044 ASSERT(db->db.db_data != NULL);
1045 ASSERT(db->db_level == 0);
1046 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT);
1048 if (dr == NULL ||
1049 (dr->dt.dl.dr_data !=
1050 ((db->db_blkid == DMU_BONUS_BLKID) ? db->db.db_data : db->db_buf)))
1051 return;
1054 * If the last dirty record for this dbuf has not yet synced
1055 * and its referencing the dbuf data, either:
1056 * reset the reference to point to a new copy,
1057 * or (if there a no active holders)
1058 * just null out the current db_data pointer.
1060 ASSERT(dr->dr_txg >= txg - 2);
1061 if (db->db_blkid == DMU_BONUS_BLKID) {
1062 /* Note that the data bufs here are zio_bufs */
1063 dr->dt.dl.dr_data = zio_buf_alloc(DN_MAX_BONUSLEN);
1064 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
1065 bcopy(db->db.db_data, dr->dt.dl.dr_data, DN_MAX_BONUSLEN);
1066 } else if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
1067 int size = arc_buf_size(db->db_buf);
1068 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1069 spa_t *spa = db->db_objset->os_spa;
1070 enum zio_compress compress_type =
1071 arc_get_compression(db->db_buf);
1073 if (compress_type == ZIO_COMPRESS_OFF) {
1074 dr->dt.dl.dr_data = arc_alloc_buf(spa, db, type, size);
1075 } else {
1076 ASSERT3U(type, ==, ARC_BUFC_DATA);
1077 dr->dt.dl.dr_data = arc_alloc_compressed_buf(spa, db,
1078 size, arc_buf_lsize(db->db_buf), compress_type);
1080 bcopy(db->db.db_data, dr->dt.dl.dr_data->b_data, size);
1081 } else {
1082 db->db_buf = NULL;
1083 dbuf_clear_data(db);
1088 dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
1090 int err = 0;
1091 boolean_t prefetch;
1092 dnode_t *dn;
1095 * We don't have to hold the mutex to check db_state because it
1096 * can't be freed while we have a hold on the buffer.
1098 ASSERT(!refcount_is_zero(&db->db_holds));
1100 if (db->db_state == DB_NOFILL)
1101 return (SET_ERROR(EIO));
1103 DB_DNODE_ENTER(db);
1104 dn = DB_DNODE(db);
1105 if ((flags & DB_RF_HAVESTRUCT) == 0)
1106 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1108 prefetch = db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1109 (flags & DB_RF_NOPREFETCH) == 0 && dn != NULL &&
1110 DBUF_IS_CACHEABLE(db);
1112 mutex_enter(&db->db_mtx);
1113 if (db->db_state == DB_CACHED) {
1115 * If the arc buf is compressed, we need to decompress it to
1116 * read the data. This could happen during the "zfs receive" of
1117 * a stream which is compressed and deduplicated.
1119 if (db->db_buf != NULL &&
1120 arc_get_compression(db->db_buf) != ZIO_COMPRESS_OFF) {
1121 dbuf_fix_old_data(db,
1122 spa_syncing_txg(dmu_objset_spa(db->db_objset)));
1123 err = arc_decompress(db->db_buf);
1124 dbuf_set_data(db, db->db_buf);
1126 mutex_exit(&db->db_mtx);
1127 if (prefetch)
1128 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE);
1129 if ((flags & DB_RF_HAVESTRUCT) == 0)
1130 rw_exit(&dn->dn_struct_rwlock);
1131 DB_DNODE_EXIT(db);
1132 } else if (db->db_state == DB_UNCACHED) {
1133 spa_t *spa = dn->dn_objset->os_spa;
1134 boolean_t need_wait = B_FALSE;
1136 if (zio == NULL &&
1137 db->db_blkptr != NULL && !BP_IS_HOLE(db->db_blkptr)) {
1138 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
1139 need_wait = B_TRUE;
1141 dbuf_read_impl(db, zio, flags);
1143 /* dbuf_read_impl has dropped db_mtx for us */
1145 if (prefetch)
1146 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE);
1148 if ((flags & DB_RF_HAVESTRUCT) == 0)
1149 rw_exit(&dn->dn_struct_rwlock);
1150 DB_DNODE_EXIT(db);
1152 if (need_wait)
1153 err = zio_wait(zio);
1154 } else {
1156 * Another reader came in while the dbuf was in flight
1157 * between UNCACHED and CACHED. Either a writer will finish
1158 * writing the buffer (sending the dbuf to CACHED) or the
1159 * first reader's request will reach the read_done callback
1160 * and send the dbuf to CACHED. Otherwise, a failure
1161 * occurred and the dbuf went to UNCACHED.
1163 mutex_exit(&db->db_mtx);
1164 if (prefetch)
1165 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE);
1166 if ((flags & DB_RF_HAVESTRUCT) == 0)
1167 rw_exit(&dn->dn_struct_rwlock);
1168 DB_DNODE_EXIT(db);
1170 /* Skip the wait per the caller's request. */
1171 mutex_enter(&db->db_mtx);
1172 if ((flags & DB_RF_NEVERWAIT) == 0) {
1173 while (db->db_state == DB_READ ||
1174 db->db_state == DB_FILL) {
1175 ASSERT(db->db_state == DB_READ ||
1176 (flags & DB_RF_HAVESTRUCT) == 0);
1177 DTRACE_PROBE2(blocked__read, dmu_buf_impl_t *,
1178 db, zio_t *, zio);
1179 cv_wait(&db->db_changed, &db->db_mtx);
1181 if (db->db_state == DB_UNCACHED)
1182 err = SET_ERROR(EIO);
1184 mutex_exit(&db->db_mtx);
1187 return (err);
1190 static void
1191 dbuf_noread(dmu_buf_impl_t *db)
1193 ASSERT(!refcount_is_zero(&db->db_holds));
1194 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1195 mutex_enter(&db->db_mtx);
1196 while (db->db_state == DB_READ || db->db_state == DB_FILL)
1197 cv_wait(&db->db_changed, &db->db_mtx);
1198 if (db->db_state == DB_UNCACHED) {
1199 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1200 spa_t *spa = db->db_objset->os_spa;
1202 ASSERT(db->db_buf == NULL);
1203 ASSERT(db->db.db_data == NULL);
1204 dbuf_set_data(db, arc_alloc_buf(spa, db, type, db->db.db_size));
1205 db->db_state = DB_FILL;
1206 } else if (db->db_state == DB_NOFILL) {
1207 dbuf_clear_data(db);
1208 } else {
1209 ASSERT3U(db->db_state, ==, DB_CACHED);
1211 mutex_exit(&db->db_mtx);
1214 void
1215 dbuf_unoverride(dbuf_dirty_record_t *dr)
1217 dmu_buf_impl_t *db = dr->dr_dbuf;
1218 blkptr_t *bp = &dr->dt.dl.dr_overridden_by;
1219 uint64_t txg = dr->dr_txg;
1221 ASSERT(MUTEX_HELD(&db->db_mtx));
1223 * This assert is valid because dmu_sync() expects to be called by
1224 * a zilog's get_data while holding a range lock. This call only
1225 * comes from dbuf_dirty() callers who must also hold a range lock.
1227 ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC);
1228 ASSERT(db->db_level == 0);
1230 if (db->db_blkid == DMU_BONUS_BLKID ||
1231 dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN)
1232 return;
1234 ASSERT(db->db_data_pending != dr);
1236 /* free this block */
1237 if (!BP_IS_HOLE(bp) && !dr->dt.dl.dr_nopwrite)
1238 zio_free(db->db_objset->os_spa, txg, bp);
1240 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
1241 dr->dt.dl.dr_nopwrite = B_FALSE;
1244 * Release the already-written buffer, so we leave it in
1245 * a consistent dirty state. Note that all callers are
1246 * modifying the buffer, so they will immediately do
1247 * another (redundant) arc_release(). Therefore, leave
1248 * the buf thawed to save the effort of freezing &
1249 * immediately re-thawing it.
1251 arc_release(dr->dt.dl.dr_data, db);
1255 * Evict (if its unreferenced) or clear (if its referenced) any level-0
1256 * data blocks in the free range, so that any future readers will find
1257 * empty blocks.
1259 void
1260 dbuf_free_range(dnode_t *dn, uint64_t start_blkid, uint64_t end_blkid,
1261 dmu_tx_t *tx)
1263 dmu_buf_impl_t db_search;
1264 dmu_buf_impl_t *db, *db_next;
1265 uint64_t txg = tx->tx_txg;
1266 avl_index_t where;
1268 if (end_blkid > dn->dn_maxblkid &&
1269 !(start_blkid == DMU_SPILL_BLKID || end_blkid == DMU_SPILL_BLKID))
1270 end_blkid = dn->dn_maxblkid;
1271 dprintf_dnode(dn, "start=%llu end=%llu\n", start_blkid, end_blkid);
1273 db_search.db_level = 0;
1274 db_search.db_blkid = start_blkid;
1275 db_search.db_state = DB_SEARCH;
1277 mutex_enter(&dn->dn_dbufs_mtx);
1278 db = avl_find(&dn->dn_dbufs, &db_search, &where);
1279 ASSERT3P(db, ==, NULL);
1281 db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER);
1283 for (; db != NULL; db = db_next) {
1284 db_next = AVL_NEXT(&dn->dn_dbufs, db);
1285 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1287 if (db->db_level != 0 || db->db_blkid > end_blkid) {
1288 break;
1290 ASSERT3U(db->db_blkid, >=, start_blkid);
1292 /* found a level 0 buffer in the range */
1293 mutex_enter(&db->db_mtx);
1294 if (dbuf_undirty(db, tx)) {
1295 /* mutex has been dropped and dbuf destroyed */
1296 continue;
1299 if (db->db_state == DB_UNCACHED ||
1300 db->db_state == DB_NOFILL ||
1301 db->db_state == DB_EVICTING) {
1302 ASSERT(db->db.db_data == NULL);
1303 mutex_exit(&db->db_mtx);
1304 continue;
1306 if (db->db_state == DB_READ || db->db_state == DB_FILL) {
1307 /* will be handled in dbuf_read_done or dbuf_rele */
1308 db->db_freed_in_flight = TRUE;
1309 mutex_exit(&db->db_mtx);
1310 continue;
1312 if (refcount_count(&db->db_holds) == 0) {
1313 ASSERT(db->db_buf);
1314 dbuf_destroy(db);
1315 continue;
1317 /* The dbuf is referenced */
1319 if (db->db_last_dirty != NULL) {
1320 dbuf_dirty_record_t *dr = db->db_last_dirty;
1322 if (dr->dr_txg == txg) {
1324 * This buffer is "in-use", re-adjust the file
1325 * size to reflect that this buffer may
1326 * contain new data when we sync.
1328 if (db->db_blkid != DMU_SPILL_BLKID &&
1329 db->db_blkid > dn->dn_maxblkid)
1330 dn->dn_maxblkid = db->db_blkid;
1331 dbuf_unoverride(dr);
1332 } else {
1334 * This dbuf is not dirty in the open context.
1335 * Either uncache it (if its not referenced in
1336 * the open context) or reset its contents to
1337 * empty.
1339 dbuf_fix_old_data(db, txg);
1342 /* clear the contents if its cached */
1343 if (db->db_state == DB_CACHED) {
1344 ASSERT(db->db.db_data != NULL);
1345 arc_release(db->db_buf, db);
1346 bzero(db->db.db_data, db->db.db_size);
1347 arc_buf_freeze(db->db_buf);
1350 mutex_exit(&db->db_mtx);
1352 mutex_exit(&dn->dn_dbufs_mtx);
1355 void
1356 dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx)
1358 arc_buf_t *buf, *obuf;
1359 int osize = db->db.db_size;
1360 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1361 dnode_t *dn;
1363 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1365 DB_DNODE_ENTER(db);
1366 dn = DB_DNODE(db);
1368 /* XXX does *this* func really need the lock? */
1369 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
1372 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
1373 * is OK, because there can be no other references to the db
1374 * when we are changing its size, so no concurrent DB_FILL can
1375 * be happening.
1378 * XXX we should be doing a dbuf_read, checking the return
1379 * value and returning that up to our callers
1381 dmu_buf_will_dirty(&db->db, tx);
1383 /* create the data buffer for the new block */
1384 buf = arc_alloc_buf(dn->dn_objset->os_spa, db, type, size);
1386 /* copy old block data to the new block */
1387 obuf = db->db_buf;
1388 bcopy(obuf->b_data, buf->b_data, MIN(osize, size));
1389 /* zero the remainder */
1390 if (size > osize)
1391 bzero((uint8_t *)buf->b_data + osize, size - osize);
1393 mutex_enter(&db->db_mtx);
1394 dbuf_set_data(db, buf);
1395 arc_buf_destroy(obuf, db);
1396 db->db.db_size = size;
1398 if (db->db_level == 0) {
1399 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
1400 db->db_last_dirty->dt.dl.dr_data = buf;
1402 mutex_exit(&db->db_mtx);
1404 dmu_objset_willuse_space(dn->dn_objset, size - osize, tx);
1405 DB_DNODE_EXIT(db);
1408 void
1409 dbuf_release_bp(dmu_buf_impl_t *db)
1411 objset_t *os = db->db_objset;
1413 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
1414 ASSERT(arc_released(os->os_phys_buf) ||
1415 list_link_active(&os->os_dsl_dataset->ds_synced_link));
1416 ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf));
1418 (void) arc_release(db->db_buf, db);
1422 * We already have a dirty record for this TXG, and we are being
1423 * dirtied again.
1425 static void
1426 dbuf_redirty(dbuf_dirty_record_t *dr)
1428 dmu_buf_impl_t *db = dr->dr_dbuf;
1430 ASSERT(MUTEX_HELD(&db->db_mtx));
1432 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID) {
1434 * If this buffer has already been written out,
1435 * we now need to reset its state.
1437 dbuf_unoverride(dr);
1438 if (db->db.db_object != DMU_META_DNODE_OBJECT &&
1439 db->db_state != DB_NOFILL) {
1440 /* Already released on initial dirty, so just thaw. */
1441 ASSERT(arc_released(db->db_buf));
1442 arc_buf_thaw(db->db_buf);
1447 dbuf_dirty_record_t *
1448 dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1450 dnode_t *dn;
1451 objset_t *os;
1452 dbuf_dirty_record_t **drp, *dr;
1453 int drop_struct_lock = FALSE;
1454 int txgoff = tx->tx_txg & TXG_MASK;
1456 ASSERT(tx->tx_txg != 0);
1457 ASSERT(!refcount_is_zero(&db->db_holds));
1458 DMU_TX_DIRTY_BUF(tx, db);
1460 DB_DNODE_ENTER(db);
1461 dn = DB_DNODE(db);
1463 * Shouldn't dirty a regular buffer in syncing context. Private
1464 * objects may be dirtied in syncing context, but only if they
1465 * were already pre-dirtied in open context.
1467 #ifdef DEBUG
1468 if (dn->dn_objset->os_dsl_dataset != NULL) {
1469 rrw_enter(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock,
1470 RW_READER, FTAG);
1472 ASSERT(!dmu_tx_is_syncing(tx) ||
1473 BP_IS_HOLE(dn->dn_objset->os_rootbp) ||
1474 DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1475 dn->dn_objset->os_dsl_dataset == NULL);
1476 if (dn->dn_objset->os_dsl_dataset != NULL)
1477 rrw_exit(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock, FTAG);
1478 #endif
1480 * We make this assert for private objects as well, but after we
1481 * check if we're already dirty. They are allowed to re-dirty
1482 * in syncing context.
1484 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1485 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1486 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1488 mutex_enter(&db->db_mtx);
1490 * XXX make this true for indirects too? The problem is that
1491 * transactions created with dmu_tx_create_assigned() from
1492 * syncing context don't bother holding ahead.
1494 ASSERT(db->db_level != 0 ||
1495 db->db_state == DB_CACHED || db->db_state == DB_FILL ||
1496 db->db_state == DB_NOFILL);
1498 mutex_enter(&dn->dn_mtx);
1500 * Don't set dirtyctx to SYNC if we're just modifying this as we
1501 * initialize the objset.
1503 if (dn->dn_dirtyctx == DN_UNDIRTIED) {
1504 if (dn->dn_objset->os_dsl_dataset != NULL) {
1505 rrw_enter(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock,
1506 RW_READER, FTAG);
1508 if (!BP_IS_HOLE(dn->dn_objset->os_rootbp)) {
1509 dn->dn_dirtyctx = (dmu_tx_is_syncing(tx) ?
1510 DN_DIRTY_SYNC : DN_DIRTY_OPEN);
1511 ASSERT(dn->dn_dirtyctx_firstset == NULL);
1512 dn->dn_dirtyctx_firstset = kmem_alloc(1, KM_SLEEP);
1514 if (dn->dn_objset->os_dsl_dataset != NULL) {
1515 rrw_exit(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock,
1516 FTAG);
1519 mutex_exit(&dn->dn_mtx);
1521 if (db->db_blkid == DMU_SPILL_BLKID)
1522 dn->dn_have_spill = B_TRUE;
1525 * If this buffer is already dirty, we're done.
1527 drp = &db->db_last_dirty;
1528 ASSERT(*drp == NULL || (*drp)->dr_txg <= tx->tx_txg ||
1529 db->db.db_object == DMU_META_DNODE_OBJECT);
1530 while ((dr = *drp) != NULL && dr->dr_txg > tx->tx_txg)
1531 drp = &dr->dr_next;
1532 if (dr && dr->dr_txg == tx->tx_txg) {
1533 DB_DNODE_EXIT(db);
1535 dbuf_redirty(dr);
1536 mutex_exit(&db->db_mtx);
1537 return (dr);
1541 * Only valid if not already dirty.
1543 ASSERT(dn->dn_object == 0 ||
1544 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1545 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1547 ASSERT3U(dn->dn_nlevels, >, db->db_level);
1548 ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) ||
1549 dn->dn_phys->dn_nlevels > db->db_level ||
1550 dn->dn_next_nlevels[txgoff] > db->db_level ||
1551 dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level ||
1552 dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level);
1555 * We should only be dirtying in syncing context if it's the
1556 * mos or we're initializing the os or it's a special object.
1557 * However, we are allowed to dirty in syncing context provided
1558 * we already dirtied it in open context. Hence we must make
1559 * this assertion only if we're not already dirty.
1561 os = dn->dn_objset;
1562 VERIFY3U(tx->tx_txg, <=, spa_final_dirty_txg(os->os_spa));
1563 #ifdef DEBUG
1564 if (dn->dn_objset->os_dsl_dataset != NULL)
1565 rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_READER, FTAG);
1566 ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1567 os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp));
1568 if (dn->dn_objset->os_dsl_dataset != NULL)
1569 rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG);
1570 #endif
1571 ASSERT(db->db.db_size != 0);
1573 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1575 if (db->db_blkid != DMU_BONUS_BLKID) {
1576 dmu_objset_willuse_space(os, db->db.db_size, tx);
1580 * If this buffer is dirty in an old transaction group we need
1581 * to make a copy of it so that the changes we make in this
1582 * transaction group won't leak out when we sync the older txg.
1584 dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP);
1585 if (db->db_level == 0) {
1586 void *data_old = db->db_buf;
1588 if (db->db_state != DB_NOFILL) {
1589 if (db->db_blkid == DMU_BONUS_BLKID) {
1590 dbuf_fix_old_data(db, tx->tx_txg);
1591 data_old = db->db.db_data;
1592 } else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
1594 * Release the data buffer from the cache so
1595 * that we can modify it without impacting
1596 * possible other users of this cached data
1597 * block. Note that indirect blocks and
1598 * private objects are not released until the
1599 * syncing state (since they are only modified
1600 * then).
1602 arc_release(db->db_buf, db);
1603 dbuf_fix_old_data(db, tx->tx_txg);
1604 data_old = db->db_buf;
1606 ASSERT(data_old != NULL);
1608 dr->dt.dl.dr_data = data_old;
1609 } else {
1610 mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_DEFAULT, NULL);
1611 list_create(&dr->dt.di.dr_children,
1612 sizeof (dbuf_dirty_record_t),
1613 offsetof(dbuf_dirty_record_t, dr_dirty_node));
1615 if (db->db_blkid != DMU_BONUS_BLKID && os->os_dsl_dataset != NULL)
1616 dr->dr_accounted = db->db.db_size;
1617 dr->dr_dbuf = db;
1618 dr->dr_txg = tx->tx_txg;
1619 dr->dr_next = *drp;
1620 *drp = dr;
1623 * We could have been freed_in_flight between the dbuf_noread
1624 * and dbuf_dirty. We win, as though the dbuf_noread() had
1625 * happened after the free.
1627 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1628 db->db_blkid != DMU_SPILL_BLKID) {
1629 mutex_enter(&dn->dn_mtx);
1630 if (dn->dn_free_ranges[txgoff] != NULL) {
1631 range_tree_clear(dn->dn_free_ranges[txgoff],
1632 db->db_blkid, 1);
1634 mutex_exit(&dn->dn_mtx);
1635 db->db_freed_in_flight = FALSE;
1639 * This buffer is now part of this txg
1641 dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg);
1642 db->db_dirtycnt += 1;
1643 ASSERT3U(db->db_dirtycnt, <=, 3);
1645 mutex_exit(&db->db_mtx);
1647 if (db->db_blkid == DMU_BONUS_BLKID ||
1648 db->db_blkid == DMU_SPILL_BLKID) {
1649 mutex_enter(&dn->dn_mtx);
1650 ASSERT(!list_link_active(&dr->dr_dirty_node));
1651 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1652 mutex_exit(&dn->dn_mtx);
1653 dnode_setdirty(dn, tx);
1654 DB_DNODE_EXIT(db);
1655 return (dr);
1659 * The dn_struct_rwlock prevents db_blkptr from changing
1660 * due to a write from syncing context completing
1661 * while we are running, so we want to acquire it before
1662 * looking at db_blkptr.
1664 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
1665 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1666 drop_struct_lock = TRUE;
1670 * If we are overwriting a dedup BP, then unless it is snapshotted,
1671 * when we get to syncing context we will need to decrement its
1672 * refcount in the DDT. Prefetch the relevant DDT block so that
1673 * syncing context won't have to wait for the i/o.
1675 ddt_prefetch(os->os_spa, db->db_blkptr);
1677 if (db->db_level == 0) {
1678 dnode_new_blkid(dn, db->db_blkid, tx, drop_struct_lock);
1679 ASSERT(dn->dn_maxblkid >= db->db_blkid);
1682 if (db->db_level+1 < dn->dn_nlevels) {
1683 dmu_buf_impl_t *parent = db->db_parent;
1684 dbuf_dirty_record_t *di;
1685 int parent_held = FALSE;
1687 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
1688 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1690 parent = dbuf_hold_level(dn, db->db_level+1,
1691 db->db_blkid >> epbs, FTAG);
1692 ASSERT(parent != NULL);
1693 parent_held = TRUE;
1695 if (drop_struct_lock)
1696 rw_exit(&dn->dn_struct_rwlock);
1697 ASSERT3U(db->db_level+1, ==, parent->db_level);
1698 di = dbuf_dirty(parent, tx);
1699 if (parent_held)
1700 dbuf_rele(parent, FTAG);
1702 mutex_enter(&db->db_mtx);
1704 * Since we've dropped the mutex, it's possible that
1705 * dbuf_undirty() might have changed this out from under us.
1707 if (db->db_last_dirty == dr ||
1708 dn->dn_object == DMU_META_DNODE_OBJECT) {
1709 mutex_enter(&di->dt.di.dr_mtx);
1710 ASSERT3U(di->dr_txg, ==, tx->tx_txg);
1711 ASSERT(!list_link_active(&dr->dr_dirty_node));
1712 list_insert_tail(&di->dt.di.dr_children, dr);
1713 mutex_exit(&di->dt.di.dr_mtx);
1714 dr->dr_parent = di;
1716 mutex_exit(&db->db_mtx);
1717 } else {
1718 ASSERT(db->db_level+1 == dn->dn_nlevels);
1719 ASSERT(db->db_blkid < dn->dn_nblkptr);
1720 ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf);
1721 mutex_enter(&dn->dn_mtx);
1722 ASSERT(!list_link_active(&dr->dr_dirty_node));
1723 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1724 mutex_exit(&dn->dn_mtx);
1725 if (drop_struct_lock)
1726 rw_exit(&dn->dn_struct_rwlock);
1729 dnode_setdirty(dn, tx);
1730 DB_DNODE_EXIT(db);
1731 return (dr);
1735 * Undirty a buffer in the transaction group referenced by the given
1736 * transaction. Return whether this evicted the dbuf.
1738 static boolean_t
1739 dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1741 dnode_t *dn;
1742 uint64_t txg = tx->tx_txg;
1743 dbuf_dirty_record_t *dr, **drp;
1745 ASSERT(txg != 0);
1748 * Due to our use of dn_nlevels below, this can only be called
1749 * in open context, unless we are operating on the MOS.
1750 * From syncing context, dn_nlevels may be different from the
1751 * dn_nlevels used when dbuf was dirtied.
1753 ASSERT(db->db_objset ==
1754 dmu_objset_pool(db->db_objset)->dp_meta_objset ||
1755 txg != spa_syncing_txg(dmu_objset_spa(db->db_objset)));
1756 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1757 ASSERT0(db->db_level);
1758 ASSERT(MUTEX_HELD(&db->db_mtx));
1761 * If this buffer is not dirty, we're done.
1763 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1764 if (dr->dr_txg <= txg)
1765 break;
1766 if (dr == NULL || dr->dr_txg < txg)
1767 return (B_FALSE);
1768 ASSERT(dr->dr_txg == txg);
1769 ASSERT(dr->dr_dbuf == db);
1771 DB_DNODE_ENTER(db);
1772 dn = DB_DNODE(db);
1774 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1776 ASSERT(db->db.db_size != 0);
1778 dsl_pool_undirty_space(dmu_objset_pool(dn->dn_objset),
1779 dr->dr_accounted, txg);
1781 *drp = dr->dr_next;
1784 * Note that there are three places in dbuf_dirty()
1785 * where this dirty record may be put on a list.
1786 * Make sure to do a list_remove corresponding to
1787 * every one of those list_insert calls.
1789 if (dr->dr_parent) {
1790 mutex_enter(&dr->dr_parent->dt.di.dr_mtx);
1791 list_remove(&dr->dr_parent->dt.di.dr_children, dr);
1792 mutex_exit(&dr->dr_parent->dt.di.dr_mtx);
1793 } else if (db->db_blkid == DMU_SPILL_BLKID ||
1794 db->db_level + 1 == dn->dn_nlevels) {
1795 ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf);
1796 mutex_enter(&dn->dn_mtx);
1797 list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr);
1798 mutex_exit(&dn->dn_mtx);
1800 DB_DNODE_EXIT(db);
1802 if (db->db_state != DB_NOFILL) {
1803 dbuf_unoverride(dr);
1805 ASSERT(db->db_buf != NULL);
1806 ASSERT(dr->dt.dl.dr_data != NULL);
1807 if (dr->dt.dl.dr_data != db->db_buf)
1808 arc_buf_destroy(dr->dt.dl.dr_data, db);
1811 kmem_free(dr, sizeof (dbuf_dirty_record_t));
1813 ASSERT(db->db_dirtycnt > 0);
1814 db->db_dirtycnt -= 1;
1816 if (refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) {
1817 ASSERT(db->db_state == DB_NOFILL || arc_released(db->db_buf));
1818 dbuf_destroy(db);
1819 return (B_TRUE);
1822 return (B_FALSE);
1825 void
1826 dmu_buf_will_dirty(dmu_buf_t *db_fake, dmu_tx_t *tx)
1828 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1829 int rf = DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH;
1831 ASSERT(tx->tx_txg != 0);
1832 ASSERT(!refcount_is_zero(&db->db_holds));
1835 * Quick check for dirtyness. For already dirty blocks, this
1836 * reduces runtime of this function by >90%, and overall performance
1837 * by 50% for some workloads (e.g. file deletion with indirect blocks
1838 * cached).
1840 mutex_enter(&db->db_mtx);
1841 dbuf_dirty_record_t *dr;
1842 for (dr = db->db_last_dirty;
1843 dr != NULL && dr->dr_txg >= tx->tx_txg; dr = dr->dr_next) {
1845 * It's possible that it is already dirty but not cached,
1846 * because there are some calls to dbuf_dirty() that don't
1847 * go through dmu_buf_will_dirty().
1849 if (dr->dr_txg == tx->tx_txg && db->db_state == DB_CACHED) {
1850 /* This dbuf is already dirty and cached. */
1851 dbuf_redirty(dr);
1852 mutex_exit(&db->db_mtx);
1853 return;
1856 mutex_exit(&db->db_mtx);
1858 DB_DNODE_ENTER(db);
1859 if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock))
1860 rf |= DB_RF_HAVESTRUCT;
1861 DB_DNODE_EXIT(db);
1862 (void) dbuf_read(db, NULL, rf);
1863 (void) dbuf_dirty(db, tx);
1866 void
1867 dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1869 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1871 db->db_state = DB_NOFILL;
1873 dmu_buf_will_fill(db_fake, tx);
1876 void
1877 dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1879 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1881 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1882 ASSERT(tx->tx_txg != 0);
1883 ASSERT(db->db_level == 0);
1884 ASSERT(!refcount_is_zero(&db->db_holds));
1886 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
1887 dmu_tx_private_ok(tx));
1889 dbuf_noread(db);
1890 (void) dbuf_dirty(db, tx);
1893 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1894 /* ARGSUSED */
1895 void
1896 dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx)
1898 mutex_enter(&db->db_mtx);
1899 DBUF_VERIFY(db);
1901 if (db->db_state == DB_FILL) {
1902 if (db->db_level == 0 && db->db_freed_in_flight) {
1903 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1904 /* we were freed while filling */
1905 /* XXX dbuf_undirty? */
1906 bzero(db->db.db_data, db->db.db_size);
1907 db->db_freed_in_flight = FALSE;
1909 db->db_state = DB_CACHED;
1910 cv_broadcast(&db->db_changed);
1912 mutex_exit(&db->db_mtx);
1915 void
1916 dmu_buf_write_embedded(dmu_buf_t *dbuf, void *data,
1917 bp_embedded_type_t etype, enum zio_compress comp,
1918 int uncompressed_size, int compressed_size, int byteorder,
1919 dmu_tx_t *tx)
1921 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
1922 struct dirty_leaf *dl;
1923 dmu_object_type_t type;
1925 if (etype == BP_EMBEDDED_TYPE_DATA) {
1926 ASSERT(spa_feature_is_active(dmu_objset_spa(db->db_objset),
1927 SPA_FEATURE_EMBEDDED_DATA));
1930 DB_DNODE_ENTER(db);
1931 type = DB_DNODE(db)->dn_type;
1932 DB_DNODE_EXIT(db);
1934 ASSERT0(db->db_level);
1935 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1937 dmu_buf_will_not_fill(dbuf, tx);
1939 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
1940 dl = &db->db_last_dirty->dt.dl;
1941 encode_embedded_bp_compressed(&dl->dr_overridden_by,
1942 data, comp, uncompressed_size, compressed_size);
1943 BPE_SET_ETYPE(&dl->dr_overridden_by, etype);
1944 BP_SET_TYPE(&dl->dr_overridden_by, type);
1945 BP_SET_LEVEL(&dl->dr_overridden_by, 0);
1946 BP_SET_BYTEORDER(&dl->dr_overridden_by, byteorder);
1948 dl->dr_override_state = DR_OVERRIDDEN;
1949 dl->dr_overridden_by.blk_birth = db->db_last_dirty->dr_txg;
1953 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1954 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1956 void
1957 dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx)
1959 ASSERT(!refcount_is_zero(&db->db_holds));
1960 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1961 ASSERT(db->db_level == 0);
1962 ASSERT3U(dbuf_is_metadata(db), ==, arc_is_metadata(buf));
1963 ASSERT(buf != NULL);
1964 ASSERT(arc_buf_lsize(buf) == db->db.db_size);
1965 ASSERT(tx->tx_txg != 0);
1967 arc_return_buf(buf, db);
1968 ASSERT(arc_released(buf));
1970 mutex_enter(&db->db_mtx);
1972 while (db->db_state == DB_READ || db->db_state == DB_FILL)
1973 cv_wait(&db->db_changed, &db->db_mtx);
1975 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED);
1977 if (db->db_state == DB_CACHED &&
1978 refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) {
1979 mutex_exit(&db->db_mtx);
1980 (void) dbuf_dirty(db, tx);
1981 bcopy(buf->b_data, db->db.db_data, db->db.db_size);
1982 arc_buf_destroy(buf, db);
1983 xuio_stat_wbuf_copied();
1984 return;
1987 xuio_stat_wbuf_nocopy();
1988 if (db->db_state == DB_CACHED) {
1989 dbuf_dirty_record_t *dr = db->db_last_dirty;
1991 ASSERT(db->db_buf != NULL);
1992 if (dr != NULL && dr->dr_txg == tx->tx_txg) {
1993 ASSERT(dr->dt.dl.dr_data == db->db_buf);
1994 if (!arc_released(db->db_buf)) {
1995 ASSERT(dr->dt.dl.dr_override_state ==
1996 DR_OVERRIDDEN);
1997 arc_release(db->db_buf, db);
1999 dr->dt.dl.dr_data = buf;
2000 arc_buf_destroy(db->db_buf, db);
2001 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) {
2002 arc_release(db->db_buf, db);
2003 arc_buf_destroy(db->db_buf, db);
2005 db->db_buf = NULL;
2007 ASSERT(db->db_buf == NULL);
2008 dbuf_set_data(db, buf);
2009 db->db_state = DB_FILL;
2010 mutex_exit(&db->db_mtx);
2011 (void) dbuf_dirty(db, tx);
2012 dmu_buf_fill_done(&db->db, tx);
2015 void
2016 dbuf_destroy(dmu_buf_impl_t *db)
2018 dnode_t *dn;
2019 dmu_buf_impl_t *parent = db->db_parent;
2020 dmu_buf_impl_t *dndb;
2022 ASSERT(MUTEX_HELD(&db->db_mtx));
2023 ASSERT(refcount_is_zero(&db->db_holds));
2025 if (db->db_buf != NULL) {
2026 arc_buf_destroy(db->db_buf, db);
2027 db->db_buf = NULL;
2030 if (db->db_blkid == DMU_BONUS_BLKID) {
2031 ASSERT(db->db.db_data != NULL);
2032 zio_buf_free(db->db.db_data, DN_MAX_BONUSLEN);
2033 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
2034 db->db_state = DB_UNCACHED;
2037 dbuf_clear_data(db);
2039 if (multilist_link_active(&db->db_cache_link)) {
2040 multilist_remove(dbuf_cache, db);
2041 (void) refcount_remove_many(&dbuf_cache_size,
2042 db->db.db_size, db);
2045 ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL);
2046 ASSERT(db->db_data_pending == NULL);
2048 db->db_state = DB_EVICTING;
2049 db->db_blkptr = NULL;
2052 * Now that db_state is DB_EVICTING, nobody else can find this via
2053 * the hash table. We can now drop db_mtx, which allows us to
2054 * acquire the dn_dbufs_mtx.
2056 mutex_exit(&db->db_mtx);
2058 DB_DNODE_ENTER(db);
2059 dn = DB_DNODE(db);
2060 dndb = dn->dn_dbuf;
2061 if (db->db_blkid != DMU_BONUS_BLKID) {
2062 boolean_t needlock = !MUTEX_HELD(&dn->dn_dbufs_mtx);
2063 if (needlock)
2064 mutex_enter(&dn->dn_dbufs_mtx);
2065 avl_remove(&dn->dn_dbufs, db);
2066 atomic_dec_32(&dn->dn_dbufs_count);
2067 membar_producer();
2068 DB_DNODE_EXIT(db);
2069 if (needlock)
2070 mutex_exit(&dn->dn_dbufs_mtx);
2072 * Decrementing the dbuf count means that the hold corresponding
2073 * to the removed dbuf is no longer discounted in dnode_move(),
2074 * so the dnode cannot be moved until after we release the hold.
2075 * The membar_producer() ensures visibility of the decremented
2076 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
2077 * release any lock.
2079 dnode_rele(dn, db);
2080 db->db_dnode_handle = NULL;
2082 dbuf_hash_remove(db);
2083 } else {
2084 DB_DNODE_EXIT(db);
2087 ASSERT(refcount_is_zero(&db->db_holds));
2089 db->db_parent = NULL;
2091 ASSERT(db->db_buf == NULL);
2092 ASSERT(db->db.db_data == NULL);
2093 ASSERT(db->db_hash_next == NULL);
2094 ASSERT(db->db_blkptr == NULL);
2095 ASSERT(db->db_data_pending == NULL);
2096 ASSERT(!multilist_link_active(&db->db_cache_link));
2098 kmem_cache_free(dbuf_kmem_cache, db);
2099 arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
2102 * If this dbuf is referenced from an indirect dbuf,
2103 * decrement the ref count on the indirect dbuf.
2105 if (parent && parent != dndb)
2106 dbuf_rele(parent, db);
2110 * Note: While bpp will always be updated if the function returns success,
2111 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
2112 * this happens when the dnode is the meta-dnode, or a userused or groupused
2113 * object.
2115 static int
2116 dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse,
2117 dmu_buf_impl_t **parentp, blkptr_t **bpp)
2119 *parentp = NULL;
2120 *bpp = NULL;
2122 ASSERT(blkid != DMU_BONUS_BLKID);
2124 if (blkid == DMU_SPILL_BLKID) {
2125 mutex_enter(&dn->dn_mtx);
2126 if (dn->dn_have_spill &&
2127 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
2128 *bpp = &dn->dn_phys->dn_spill;
2129 else
2130 *bpp = NULL;
2131 dbuf_add_ref(dn->dn_dbuf, NULL);
2132 *parentp = dn->dn_dbuf;
2133 mutex_exit(&dn->dn_mtx);
2134 return (0);
2137 int nlevels =
2138 (dn->dn_phys->dn_nlevels == 0) ? 1 : dn->dn_phys->dn_nlevels;
2139 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
2141 ASSERT3U(level * epbs, <, 64);
2142 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2144 * This assertion shouldn't trip as long as the max indirect block size
2145 * is less than 1M. The reason for this is that up to that point,
2146 * the number of levels required to address an entire object with blocks
2147 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In
2148 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55
2149 * (i.e. we can address the entire object), objects will all use at most
2150 * N-1 levels and the assertion won't overflow. However, once epbs is
2151 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be
2152 * enough to address an entire object, so objects will have 5 levels,
2153 * but then this assertion will overflow.
2155 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we
2156 * need to redo this logic to handle overflows.
2158 ASSERT(level >= nlevels ||
2159 ((nlevels - level - 1) * epbs) +
2160 highbit64(dn->dn_phys->dn_nblkptr) <= 64);
2161 if (level >= nlevels ||
2162 blkid >= ((uint64_t)dn->dn_phys->dn_nblkptr <<
2163 ((nlevels - level - 1) * epbs)) ||
2164 (fail_sparse &&
2165 blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) {
2166 /* the buffer has no parent yet */
2167 return (SET_ERROR(ENOENT));
2168 } else if (level < nlevels-1) {
2169 /* this block is referenced from an indirect block */
2170 int err = dbuf_hold_impl(dn, level+1,
2171 blkid >> epbs, fail_sparse, FALSE, NULL, parentp);
2172 if (err)
2173 return (err);
2174 err = dbuf_read(*parentp, NULL,
2175 (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL));
2176 if (err) {
2177 dbuf_rele(*parentp, NULL);
2178 *parentp = NULL;
2179 return (err);
2181 *bpp = ((blkptr_t *)(*parentp)->db.db_data) +
2182 (blkid & ((1ULL << epbs) - 1));
2183 if (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))
2184 ASSERT(BP_IS_HOLE(*bpp));
2185 return (0);
2186 } else {
2187 /* the block is referenced from the dnode */
2188 ASSERT3U(level, ==, nlevels-1);
2189 ASSERT(dn->dn_phys->dn_nblkptr == 0 ||
2190 blkid < dn->dn_phys->dn_nblkptr);
2191 if (dn->dn_dbuf) {
2192 dbuf_add_ref(dn->dn_dbuf, NULL);
2193 *parentp = dn->dn_dbuf;
2195 *bpp = &dn->dn_phys->dn_blkptr[blkid];
2196 return (0);
2200 static dmu_buf_impl_t *
2201 dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid,
2202 dmu_buf_impl_t *parent, blkptr_t *blkptr)
2204 objset_t *os = dn->dn_objset;
2205 dmu_buf_impl_t *db, *odb;
2207 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2208 ASSERT(dn->dn_type != DMU_OT_NONE);
2210 db = kmem_cache_alloc(dbuf_kmem_cache, KM_SLEEP);
2212 db->db_objset = os;
2213 db->db.db_object = dn->dn_object;
2214 db->db_level = level;
2215 db->db_blkid = blkid;
2216 db->db_last_dirty = NULL;
2217 db->db_dirtycnt = 0;
2218 db->db_dnode_handle = dn->dn_handle;
2219 db->db_parent = parent;
2220 db->db_blkptr = blkptr;
2222 db->db_user = NULL;
2223 db->db_user_immediate_evict = FALSE;
2224 db->db_freed_in_flight = FALSE;
2225 db->db_pending_evict = FALSE;
2227 if (blkid == DMU_BONUS_BLKID) {
2228 ASSERT3P(parent, ==, dn->dn_dbuf);
2229 db->db.db_size = DN_MAX_BONUSLEN -
2230 (dn->dn_nblkptr-1) * sizeof (blkptr_t);
2231 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
2232 db->db.db_offset = DMU_BONUS_BLKID;
2233 db->db_state = DB_UNCACHED;
2234 /* the bonus dbuf is not placed in the hash table */
2235 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
2236 return (db);
2237 } else if (blkid == DMU_SPILL_BLKID) {
2238 db->db.db_size = (blkptr != NULL) ?
2239 BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE;
2240 db->db.db_offset = 0;
2241 } else {
2242 int blocksize =
2243 db->db_level ? 1 << dn->dn_indblkshift : dn->dn_datablksz;
2244 db->db.db_size = blocksize;
2245 db->db.db_offset = db->db_blkid * blocksize;
2249 * Hold the dn_dbufs_mtx while we get the new dbuf
2250 * in the hash table *and* added to the dbufs list.
2251 * This prevents a possible deadlock with someone
2252 * trying to look up this dbuf before its added to the
2253 * dn_dbufs list.
2255 mutex_enter(&dn->dn_dbufs_mtx);
2256 db->db_state = DB_EVICTING;
2257 if ((odb = dbuf_hash_insert(db)) != NULL) {
2258 /* someone else inserted it first */
2259 kmem_cache_free(dbuf_kmem_cache, db);
2260 mutex_exit(&dn->dn_dbufs_mtx);
2261 return (odb);
2263 avl_add(&dn->dn_dbufs, db);
2265 db->db_state = DB_UNCACHED;
2266 mutex_exit(&dn->dn_dbufs_mtx);
2267 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
2269 if (parent && parent != dn->dn_dbuf)
2270 dbuf_add_ref(parent, db);
2272 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
2273 refcount_count(&dn->dn_holds) > 0);
2274 (void) refcount_add(&dn->dn_holds, db);
2275 atomic_inc_32(&dn->dn_dbufs_count);
2277 dprintf_dbuf(db, "db=%p\n", db);
2279 return (db);
2282 typedef struct dbuf_prefetch_arg {
2283 spa_t *dpa_spa; /* The spa to issue the prefetch in. */
2284 zbookmark_phys_t dpa_zb; /* The target block to prefetch. */
2285 int dpa_epbs; /* Entries (blkptr_t's) Per Block Shift. */
2286 int dpa_curlevel; /* The current level that we're reading */
2287 dnode_t *dpa_dnode; /* The dnode associated with the prefetch */
2288 zio_priority_t dpa_prio; /* The priority I/Os should be issued at. */
2289 zio_t *dpa_zio; /* The parent zio_t for all prefetches. */
2290 arc_flags_t dpa_aflags; /* Flags to pass to the final prefetch. */
2291 } dbuf_prefetch_arg_t;
2294 * Actually issue the prefetch read for the block given.
2296 static void
2297 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t *dpa, blkptr_t *bp)
2299 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
2300 return;
2302 arc_flags_t aflags =
2303 dpa->dpa_aflags | ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH;
2305 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp));
2306 ASSERT3U(dpa->dpa_curlevel, ==, dpa->dpa_zb.zb_level);
2307 ASSERT(dpa->dpa_zio != NULL);
2308 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa, bp, NULL, NULL,
2309 dpa->dpa_prio, ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2310 &aflags, &dpa->dpa_zb);
2314 * Called when an indirect block above our prefetch target is read in. This
2315 * will either read in the next indirect block down the tree or issue the actual
2316 * prefetch if the next block down is our target.
2318 static void
2319 dbuf_prefetch_indirect_done(zio_t *zio, arc_buf_t *abuf, void *private)
2321 dbuf_prefetch_arg_t *dpa = private;
2323 ASSERT3S(dpa->dpa_zb.zb_level, <, dpa->dpa_curlevel);
2324 ASSERT3S(dpa->dpa_curlevel, >, 0);
2327 * The dpa_dnode is only valid if we are called with a NULL
2328 * zio. This indicates that the arc_read() returned without
2329 * first calling zio_read() to issue a physical read. Once
2330 * a physical read is made the dpa_dnode must be invalidated
2331 * as the locks guarding it may have been dropped. If the
2332 * dpa_dnode is still valid, then we want to add it to the dbuf
2333 * cache. To do so, we must hold the dbuf associated with the block
2334 * we just prefetched, read its contents so that we associate it
2335 * with an arc_buf_t, and then release it.
2337 if (zio != NULL) {
2338 ASSERT3S(BP_GET_LEVEL(zio->io_bp), ==, dpa->dpa_curlevel);
2339 if (zio->io_flags & ZIO_FLAG_RAW) {
2340 ASSERT3U(BP_GET_PSIZE(zio->io_bp), ==, zio->io_size);
2341 } else {
2342 ASSERT3U(BP_GET_LSIZE(zio->io_bp), ==, zio->io_size);
2344 ASSERT3P(zio->io_spa, ==, dpa->dpa_spa);
2346 dpa->dpa_dnode = NULL;
2347 } else if (dpa->dpa_dnode != NULL) {
2348 uint64_t curblkid = dpa->dpa_zb.zb_blkid >>
2349 (dpa->dpa_epbs * (dpa->dpa_curlevel -
2350 dpa->dpa_zb.zb_level));
2351 dmu_buf_impl_t *db = dbuf_hold_level(dpa->dpa_dnode,
2352 dpa->dpa_curlevel, curblkid, FTAG);
2353 (void) dbuf_read(db, NULL,
2354 DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH | DB_RF_HAVESTRUCT);
2355 dbuf_rele(db, FTAG);
2358 dpa->dpa_curlevel--;
2360 uint64_t nextblkid = dpa->dpa_zb.zb_blkid >>
2361 (dpa->dpa_epbs * (dpa->dpa_curlevel - dpa->dpa_zb.zb_level));
2362 blkptr_t *bp = ((blkptr_t *)abuf->b_data) +
2363 P2PHASE(nextblkid, 1ULL << dpa->dpa_epbs);
2364 if (BP_IS_HOLE(bp) || (zio != NULL && zio->io_error != 0)) {
2365 kmem_free(dpa, sizeof (*dpa));
2366 } else if (dpa->dpa_curlevel == dpa->dpa_zb.zb_level) {
2367 ASSERT3U(nextblkid, ==, dpa->dpa_zb.zb_blkid);
2368 dbuf_issue_final_prefetch(dpa, bp);
2369 kmem_free(dpa, sizeof (*dpa));
2370 } else {
2371 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT;
2372 zbookmark_phys_t zb;
2374 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp));
2376 SET_BOOKMARK(&zb, dpa->dpa_zb.zb_objset,
2377 dpa->dpa_zb.zb_object, dpa->dpa_curlevel, nextblkid);
2379 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa,
2380 bp, dbuf_prefetch_indirect_done, dpa, dpa->dpa_prio,
2381 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2382 &iter_aflags, &zb);
2385 arc_buf_destroy(abuf, private);
2389 * Issue prefetch reads for the given block on the given level. If the indirect
2390 * blocks above that block are not in memory, we will read them in
2391 * asynchronously. As a result, this call never blocks waiting for a read to
2392 * complete.
2394 void
2395 dbuf_prefetch(dnode_t *dn, int64_t level, uint64_t blkid, zio_priority_t prio,
2396 arc_flags_t aflags)
2398 blkptr_t bp;
2399 int epbs, nlevels, curlevel;
2400 uint64_t curblkid;
2402 ASSERT(blkid != DMU_BONUS_BLKID);
2403 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2405 if (blkid > dn->dn_maxblkid)
2406 return;
2408 if (dnode_block_freed(dn, blkid))
2409 return;
2412 * This dnode hasn't been written to disk yet, so there's nothing to
2413 * prefetch.
2415 nlevels = dn->dn_phys->dn_nlevels;
2416 if (level >= nlevels || dn->dn_phys->dn_nblkptr == 0)
2417 return;
2419 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2420 if (dn->dn_phys->dn_maxblkid < blkid << (epbs * level))
2421 return;
2423 dmu_buf_impl_t *db = dbuf_find(dn->dn_objset, dn->dn_object,
2424 level, blkid);
2425 if (db != NULL) {
2426 mutex_exit(&db->db_mtx);
2428 * This dbuf already exists. It is either CACHED, or
2429 * (we assume) about to be read or filled.
2431 return;
2435 * Find the closest ancestor (indirect block) of the target block
2436 * that is present in the cache. In this indirect block, we will
2437 * find the bp that is at curlevel, curblkid.
2439 curlevel = level;
2440 curblkid = blkid;
2441 while (curlevel < nlevels - 1) {
2442 int parent_level = curlevel + 1;
2443 uint64_t parent_blkid = curblkid >> epbs;
2444 dmu_buf_impl_t *db;
2446 if (dbuf_hold_impl(dn, parent_level, parent_blkid,
2447 FALSE, TRUE, FTAG, &db) == 0) {
2448 blkptr_t *bpp = db->db_buf->b_data;
2449 bp = bpp[P2PHASE(curblkid, 1 << epbs)];
2450 dbuf_rele(db, FTAG);
2451 break;
2454 curlevel = parent_level;
2455 curblkid = parent_blkid;
2458 if (curlevel == nlevels - 1) {
2459 /* No cached indirect blocks found. */
2460 ASSERT3U(curblkid, <, dn->dn_phys->dn_nblkptr);
2461 bp = dn->dn_phys->dn_blkptr[curblkid];
2463 if (BP_IS_HOLE(&bp))
2464 return;
2466 ASSERT3U(curlevel, ==, BP_GET_LEVEL(&bp));
2468 zio_t *pio = zio_root(dmu_objset_spa(dn->dn_objset), NULL, NULL,
2469 ZIO_FLAG_CANFAIL);
2471 dbuf_prefetch_arg_t *dpa = kmem_zalloc(sizeof (*dpa), KM_SLEEP);
2472 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
2473 SET_BOOKMARK(&dpa->dpa_zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET,
2474 dn->dn_object, level, blkid);
2475 dpa->dpa_curlevel = curlevel;
2476 dpa->dpa_prio = prio;
2477 dpa->dpa_aflags = aflags;
2478 dpa->dpa_spa = dn->dn_objset->os_spa;
2479 dpa->dpa_dnode = dn;
2480 dpa->dpa_epbs = epbs;
2481 dpa->dpa_zio = pio;
2484 * If we have the indirect just above us, no need to do the asynchronous
2485 * prefetch chain; we'll just run the last step ourselves. If we're at
2486 * a higher level, though, we want to issue the prefetches for all the
2487 * indirect blocks asynchronously, so we can go on with whatever we were
2488 * doing.
2490 if (curlevel == level) {
2491 ASSERT3U(curblkid, ==, blkid);
2492 dbuf_issue_final_prefetch(dpa, &bp);
2493 kmem_free(dpa, sizeof (*dpa));
2494 } else {
2495 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT;
2496 zbookmark_phys_t zb;
2498 SET_BOOKMARK(&zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET,
2499 dn->dn_object, curlevel, curblkid);
2500 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa,
2501 &bp, dbuf_prefetch_indirect_done, dpa, prio,
2502 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2503 &iter_aflags, &zb);
2506 * We use pio here instead of dpa_zio since it's possible that
2507 * dpa may have already been freed.
2509 zio_nowait(pio);
2513 * Returns with db_holds incremented, and db_mtx not held.
2514 * Note: dn_struct_rwlock must be held.
2517 dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid,
2518 boolean_t fail_sparse, boolean_t fail_uncached,
2519 void *tag, dmu_buf_impl_t **dbp)
2521 dmu_buf_impl_t *db, *parent = NULL;
2523 ASSERT(blkid != DMU_BONUS_BLKID);
2524 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2525 ASSERT3U(dn->dn_nlevels, >, level);
2527 *dbp = NULL;
2528 top:
2529 /* dbuf_find() returns with db_mtx held */
2530 db = dbuf_find(dn->dn_objset, dn->dn_object, level, blkid);
2532 if (db == NULL) {
2533 blkptr_t *bp = NULL;
2534 int err;
2536 if (fail_uncached)
2537 return (SET_ERROR(ENOENT));
2539 ASSERT3P(parent, ==, NULL);
2540 err = dbuf_findbp(dn, level, blkid, fail_sparse, &parent, &bp);
2541 if (fail_sparse) {
2542 if (err == 0 && bp && BP_IS_HOLE(bp))
2543 err = SET_ERROR(ENOENT);
2544 if (err) {
2545 if (parent)
2546 dbuf_rele(parent, NULL);
2547 return (err);
2550 if (err && err != ENOENT)
2551 return (err);
2552 db = dbuf_create(dn, level, blkid, parent, bp);
2555 if (fail_uncached && db->db_state != DB_CACHED) {
2556 mutex_exit(&db->db_mtx);
2557 return (SET_ERROR(ENOENT));
2560 if (db->db_buf != NULL)
2561 ASSERT3P(db->db.db_data, ==, db->db_buf->b_data);
2563 ASSERT(db->db_buf == NULL || arc_referenced(db->db_buf));
2566 * If this buffer is currently syncing out, and we are are
2567 * still referencing it from db_data, we need to make a copy
2568 * of it in case we decide we want to dirty it again in this txg.
2570 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
2571 dn->dn_object != DMU_META_DNODE_OBJECT &&
2572 db->db_state == DB_CACHED && db->db_data_pending) {
2573 dbuf_dirty_record_t *dr = db->db_data_pending;
2575 if (dr->dt.dl.dr_data == db->db_buf) {
2576 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
2578 dbuf_set_data(db,
2579 arc_alloc_buf(dn->dn_objset->os_spa, db, type,
2580 db->db.db_size));
2581 bcopy(dr->dt.dl.dr_data->b_data, db->db.db_data,
2582 db->db.db_size);
2586 if (multilist_link_active(&db->db_cache_link)) {
2587 ASSERT(refcount_is_zero(&db->db_holds));
2588 multilist_remove(dbuf_cache, db);
2589 (void) refcount_remove_many(&dbuf_cache_size,
2590 db->db.db_size, db);
2592 (void) refcount_add(&db->db_holds, tag);
2593 DBUF_VERIFY(db);
2594 mutex_exit(&db->db_mtx);
2596 /* NOTE: we can't rele the parent until after we drop the db_mtx */
2597 if (parent)
2598 dbuf_rele(parent, NULL);
2600 ASSERT3P(DB_DNODE(db), ==, dn);
2601 ASSERT3U(db->db_blkid, ==, blkid);
2602 ASSERT3U(db->db_level, ==, level);
2603 *dbp = db;
2605 return (0);
2608 dmu_buf_impl_t *
2609 dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag)
2611 return (dbuf_hold_level(dn, 0, blkid, tag));
2614 dmu_buf_impl_t *
2615 dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag)
2617 dmu_buf_impl_t *db;
2618 int err = dbuf_hold_impl(dn, level, blkid, FALSE, FALSE, tag, &db);
2619 return (err ? NULL : db);
2622 void
2623 dbuf_create_bonus(dnode_t *dn)
2625 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
2627 ASSERT(dn->dn_bonus == NULL);
2628 dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL);
2632 dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx)
2634 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2635 dnode_t *dn;
2637 if (db->db_blkid != DMU_SPILL_BLKID)
2638 return (SET_ERROR(ENOTSUP));
2639 if (blksz == 0)
2640 blksz = SPA_MINBLOCKSIZE;
2641 ASSERT3U(blksz, <=, spa_maxblocksize(dmu_objset_spa(db->db_objset)));
2642 blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE);
2644 DB_DNODE_ENTER(db);
2645 dn = DB_DNODE(db);
2646 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2647 dbuf_new_size(db, blksz, tx);
2648 rw_exit(&dn->dn_struct_rwlock);
2649 DB_DNODE_EXIT(db);
2651 return (0);
2654 void
2655 dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx)
2657 dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx);
2660 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2661 void
2662 dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
2664 int64_t holds = refcount_add(&db->db_holds, tag);
2665 ASSERT3S(holds, >, 1);
2668 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
2669 boolean_t
2670 dbuf_try_add_ref(dmu_buf_t *db_fake, objset_t *os, uint64_t obj, uint64_t blkid,
2671 void *tag)
2673 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2674 dmu_buf_impl_t *found_db;
2675 boolean_t result = B_FALSE;
2677 if (db->db_blkid == DMU_BONUS_BLKID)
2678 found_db = dbuf_find_bonus(os, obj);
2679 else
2680 found_db = dbuf_find(os, obj, 0, blkid);
2682 if (found_db != NULL) {
2683 if (db == found_db && dbuf_refcount(db) > db->db_dirtycnt) {
2684 (void) refcount_add(&db->db_holds, tag);
2685 result = B_TRUE;
2687 mutex_exit(&db->db_mtx);
2689 return (result);
2693 * If you call dbuf_rele() you had better not be referencing the dnode handle
2694 * unless you have some other direct or indirect hold on the dnode. (An indirect
2695 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2696 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2697 * dnode's parent dbuf evicting its dnode handles.
2699 void
2700 dbuf_rele(dmu_buf_impl_t *db, void *tag)
2702 mutex_enter(&db->db_mtx);
2703 dbuf_rele_and_unlock(db, tag);
2706 void
2707 dmu_buf_rele(dmu_buf_t *db, void *tag)
2709 dbuf_rele((dmu_buf_impl_t *)db, tag);
2713 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2714 * db_dirtycnt and db_holds to be updated atomically.
2716 void
2717 dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag)
2719 int64_t holds;
2721 ASSERT(MUTEX_HELD(&db->db_mtx));
2722 DBUF_VERIFY(db);
2725 * Remove the reference to the dbuf before removing its hold on the
2726 * dnode so we can guarantee in dnode_move() that a referenced bonus
2727 * buffer has a corresponding dnode hold.
2729 holds = refcount_remove(&db->db_holds, tag);
2730 ASSERT(holds >= 0);
2733 * We can't freeze indirects if there is a possibility that they
2734 * may be modified in the current syncing context.
2736 if (db->db_buf != NULL &&
2737 holds == (db->db_level == 0 ? db->db_dirtycnt : 0)) {
2738 arc_buf_freeze(db->db_buf);
2741 if (holds == db->db_dirtycnt &&
2742 db->db_level == 0 && db->db_user_immediate_evict)
2743 dbuf_evict_user(db);
2745 if (holds == 0) {
2746 if (db->db_blkid == DMU_BONUS_BLKID) {
2747 dnode_t *dn;
2748 boolean_t evict_dbuf = db->db_pending_evict;
2751 * If the dnode moves here, we cannot cross this
2752 * barrier until the move completes.
2754 DB_DNODE_ENTER(db);
2756 dn = DB_DNODE(db);
2757 atomic_dec_32(&dn->dn_dbufs_count);
2760 * Decrementing the dbuf count means that the bonus
2761 * buffer's dnode hold is no longer discounted in
2762 * dnode_move(). The dnode cannot move until after
2763 * the dnode_rele() below.
2765 DB_DNODE_EXIT(db);
2768 * Do not reference db after its lock is dropped.
2769 * Another thread may evict it.
2771 mutex_exit(&db->db_mtx);
2773 if (evict_dbuf)
2774 dnode_evict_bonus(dn);
2776 dnode_rele(dn, db);
2777 } else if (db->db_buf == NULL) {
2779 * This is a special case: we never associated this
2780 * dbuf with any data allocated from the ARC.
2782 ASSERT(db->db_state == DB_UNCACHED ||
2783 db->db_state == DB_NOFILL);
2784 dbuf_destroy(db);
2785 } else if (arc_released(db->db_buf)) {
2787 * This dbuf has anonymous data associated with it.
2789 dbuf_destroy(db);
2790 } else {
2791 boolean_t do_arc_evict = B_FALSE;
2792 blkptr_t bp;
2793 spa_t *spa = dmu_objset_spa(db->db_objset);
2795 if (!DBUF_IS_CACHEABLE(db) &&
2796 db->db_blkptr != NULL &&
2797 !BP_IS_HOLE(db->db_blkptr) &&
2798 !BP_IS_EMBEDDED(db->db_blkptr)) {
2799 do_arc_evict = B_TRUE;
2800 bp = *db->db_blkptr;
2803 if (!DBUF_IS_CACHEABLE(db) ||
2804 db->db_pending_evict) {
2805 dbuf_destroy(db);
2806 } else if (!multilist_link_active(&db->db_cache_link)) {
2807 multilist_insert(dbuf_cache, db);
2808 (void) refcount_add_many(&dbuf_cache_size,
2809 db->db.db_size, db);
2810 mutex_exit(&db->db_mtx);
2812 dbuf_evict_notify();
2815 if (do_arc_evict)
2816 arc_freed(spa, &bp);
2818 } else {
2819 mutex_exit(&db->db_mtx);
2824 #pragma weak dmu_buf_refcount = dbuf_refcount
2825 uint64_t
2826 dbuf_refcount(dmu_buf_impl_t *db)
2828 return (refcount_count(&db->db_holds));
2831 void *
2832 dmu_buf_replace_user(dmu_buf_t *db_fake, dmu_buf_user_t *old_user,
2833 dmu_buf_user_t *new_user)
2835 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2837 mutex_enter(&db->db_mtx);
2838 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2839 if (db->db_user == old_user)
2840 db->db_user = new_user;
2841 else
2842 old_user = db->db_user;
2843 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2844 mutex_exit(&db->db_mtx);
2846 return (old_user);
2849 void *
2850 dmu_buf_set_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2852 return (dmu_buf_replace_user(db_fake, NULL, user));
2855 void *
2856 dmu_buf_set_user_ie(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2858 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2860 db->db_user_immediate_evict = TRUE;
2861 return (dmu_buf_set_user(db_fake, user));
2864 void *
2865 dmu_buf_remove_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2867 return (dmu_buf_replace_user(db_fake, user, NULL));
2870 void *
2871 dmu_buf_get_user(dmu_buf_t *db_fake)
2873 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2875 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2876 return (db->db_user);
2879 void
2880 dmu_buf_user_evict_wait()
2882 taskq_wait(dbu_evict_taskq);
2885 blkptr_t *
2886 dmu_buf_get_blkptr(dmu_buf_t *db)
2888 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2889 return (dbi->db_blkptr);
2892 objset_t *
2893 dmu_buf_get_objset(dmu_buf_t *db)
2895 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2896 return (dbi->db_objset);
2899 dnode_t *
2900 dmu_buf_dnode_enter(dmu_buf_t *db)
2902 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2903 DB_DNODE_ENTER(dbi);
2904 return (DB_DNODE(dbi));
2907 void
2908 dmu_buf_dnode_exit(dmu_buf_t *db)
2910 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2911 DB_DNODE_EXIT(dbi);
2914 static void
2915 dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db)
2917 /* ASSERT(dmu_tx_is_syncing(tx) */
2918 ASSERT(MUTEX_HELD(&db->db_mtx));
2920 if (db->db_blkptr != NULL)
2921 return;
2923 if (db->db_blkid == DMU_SPILL_BLKID) {
2924 db->db_blkptr = &dn->dn_phys->dn_spill;
2925 BP_ZERO(db->db_blkptr);
2926 return;
2928 if (db->db_level == dn->dn_phys->dn_nlevels-1) {
2930 * This buffer was allocated at a time when there was
2931 * no available blkptrs from the dnode, or it was
2932 * inappropriate to hook it in (i.e., nlevels mis-match).
2934 ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr);
2935 ASSERT(db->db_parent == NULL);
2936 db->db_parent = dn->dn_dbuf;
2937 db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
2938 DBUF_VERIFY(db);
2939 } else {
2940 dmu_buf_impl_t *parent = db->db_parent;
2941 int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2943 ASSERT(dn->dn_phys->dn_nlevels > 1);
2944 if (parent == NULL) {
2945 mutex_exit(&db->db_mtx);
2946 rw_enter(&dn->dn_struct_rwlock, RW_READER);
2947 parent = dbuf_hold_level(dn, db->db_level + 1,
2948 db->db_blkid >> epbs, db);
2949 rw_exit(&dn->dn_struct_rwlock);
2950 mutex_enter(&db->db_mtx);
2951 db->db_parent = parent;
2953 db->db_blkptr = (blkptr_t *)parent->db.db_data +
2954 (db->db_blkid & ((1ULL << epbs) - 1));
2955 DBUF_VERIFY(db);
2959 static void
2960 dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2962 dmu_buf_impl_t *db = dr->dr_dbuf;
2963 dnode_t *dn;
2964 zio_t *zio;
2966 ASSERT(dmu_tx_is_syncing(tx));
2968 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2970 mutex_enter(&db->db_mtx);
2972 ASSERT(db->db_level > 0);
2973 DBUF_VERIFY(db);
2975 /* Read the block if it hasn't been read yet. */
2976 if (db->db_buf == NULL) {
2977 mutex_exit(&db->db_mtx);
2978 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
2979 mutex_enter(&db->db_mtx);
2981 ASSERT3U(db->db_state, ==, DB_CACHED);
2982 ASSERT(db->db_buf != NULL);
2984 DB_DNODE_ENTER(db);
2985 dn = DB_DNODE(db);
2986 /* Indirect block size must match what the dnode thinks it is. */
2987 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2988 dbuf_check_blkptr(dn, db);
2989 DB_DNODE_EXIT(db);
2991 /* Provide the pending dirty record to child dbufs */
2992 db->db_data_pending = dr;
2994 mutex_exit(&db->db_mtx);
2995 dbuf_write(dr, db->db_buf, tx);
2997 zio = dr->dr_zio;
2998 mutex_enter(&dr->dt.di.dr_mtx);
2999 dbuf_sync_list(&dr->dt.di.dr_children, db->db_level - 1, tx);
3000 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
3001 mutex_exit(&dr->dt.di.dr_mtx);
3002 zio_nowait(zio);
3005 static void
3006 dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
3008 arc_buf_t **datap = &dr->dt.dl.dr_data;
3009 dmu_buf_impl_t *db = dr->dr_dbuf;
3010 dnode_t *dn;
3011 objset_t *os;
3012 uint64_t txg = tx->tx_txg;
3014 ASSERT(dmu_tx_is_syncing(tx));
3016 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
3018 mutex_enter(&db->db_mtx);
3020 * To be synced, we must be dirtied. But we
3021 * might have been freed after the dirty.
3023 if (db->db_state == DB_UNCACHED) {
3024 /* This buffer has been freed since it was dirtied */
3025 ASSERT(db->db.db_data == NULL);
3026 } else if (db->db_state == DB_FILL) {
3027 /* This buffer was freed and is now being re-filled */
3028 ASSERT(db->db.db_data != dr->dt.dl.dr_data);
3029 } else {
3030 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL);
3032 DBUF_VERIFY(db);
3034 DB_DNODE_ENTER(db);
3035 dn = DB_DNODE(db);
3037 if (db->db_blkid == DMU_SPILL_BLKID) {
3038 mutex_enter(&dn->dn_mtx);
3039 dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR;
3040 mutex_exit(&dn->dn_mtx);
3044 * If this is a bonus buffer, simply copy the bonus data into the
3045 * dnode. It will be written out when the dnode is synced (and it
3046 * will be synced, since it must have been dirty for dbuf_sync to
3047 * be called).
3049 if (db->db_blkid == DMU_BONUS_BLKID) {
3050 dbuf_dirty_record_t **drp;
3052 ASSERT(*datap != NULL);
3053 ASSERT0(db->db_level);
3054 ASSERT3U(dn->dn_phys->dn_bonuslen, <=, DN_MAX_BONUSLEN);
3055 bcopy(*datap, DN_BONUS(dn->dn_phys), dn->dn_phys->dn_bonuslen);
3056 DB_DNODE_EXIT(db);
3058 if (*datap != db->db.db_data) {
3059 zio_buf_free(*datap, DN_MAX_BONUSLEN);
3060 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
3062 db->db_data_pending = NULL;
3063 drp = &db->db_last_dirty;
3064 while (*drp != dr)
3065 drp = &(*drp)->dr_next;
3066 ASSERT(dr->dr_next == NULL);
3067 ASSERT(dr->dr_dbuf == db);
3068 *drp = dr->dr_next;
3069 kmem_free(dr, sizeof (dbuf_dirty_record_t));
3070 ASSERT(db->db_dirtycnt > 0);
3071 db->db_dirtycnt -= 1;
3072 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
3073 return;
3076 os = dn->dn_objset;
3079 * This function may have dropped the db_mtx lock allowing a dmu_sync
3080 * operation to sneak in. As a result, we need to ensure that we
3081 * don't check the dr_override_state until we have returned from
3082 * dbuf_check_blkptr.
3084 dbuf_check_blkptr(dn, db);
3087 * If this buffer is in the middle of an immediate write,
3088 * wait for the synchronous IO to complete.
3090 while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
3091 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
3092 cv_wait(&db->db_changed, &db->db_mtx);
3093 ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN);
3096 if (db->db_state != DB_NOFILL &&
3097 dn->dn_object != DMU_META_DNODE_OBJECT &&
3098 refcount_count(&db->db_holds) > 1 &&
3099 dr->dt.dl.dr_override_state != DR_OVERRIDDEN &&
3100 *datap == db->db_buf) {
3102 * If this buffer is currently "in use" (i.e., there
3103 * are active holds and db_data still references it),
3104 * then make a copy before we start the write so that
3105 * any modifications from the open txg will not leak
3106 * into this write.
3108 * NOTE: this copy does not need to be made for
3109 * objects only modified in the syncing context (e.g.
3110 * DNONE_DNODE blocks).
3112 int psize = arc_buf_size(*datap);
3113 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
3114 enum zio_compress compress_type = arc_get_compression(*datap);
3116 if (compress_type == ZIO_COMPRESS_OFF) {
3117 *datap = arc_alloc_buf(os->os_spa, db, type, psize);
3118 } else {
3119 ASSERT3U(type, ==, ARC_BUFC_DATA);
3120 int lsize = arc_buf_lsize(*datap);
3121 *datap = arc_alloc_compressed_buf(os->os_spa, db,
3122 psize, lsize, compress_type);
3124 bcopy(db->db.db_data, (*datap)->b_data, psize);
3126 db->db_data_pending = dr;
3128 mutex_exit(&db->db_mtx);
3130 dbuf_write(dr, *datap, tx);
3132 ASSERT(!list_link_active(&dr->dr_dirty_node));
3133 if (dn->dn_object == DMU_META_DNODE_OBJECT) {
3134 list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr);
3135 DB_DNODE_EXIT(db);
3136 } else {
3138 * Although zio_nowait() does not "wait for an IO", it does
3139 * initiate the IO. If this is an empty write it seems plausible
3140 * that the IO could actually be completed before the nowait
3141 * returns. We need to DB_DNODE_EXIT() first in case
3142 * zio_nowait() invalidates the dbuf.
3144 DB_DNODE_EXIT(db);
3145 zio_nowait(dr->dr_zio);
3149 void
3150 dbuf_sync_list(list_t *list, int level, dmu_tx_t *tx)
3152 dbuf_dirty_record_t *dr;
3154 while (dr = list_head(list)) {
3155 if (dr->dr_zio != NULL) {
3157 * If we find an already initialized zio then we
3158 * are processing the meta-dnode, and we have finished.
3159 * The dbufs for all dnodes are put back on the list
3160 * during processing, so that we can zio_wait()
3161 * these IOs after initiating all child IOs.
3163 ASSERT3U(dr->dr_dbuf->db.db_object, ==,
3164 DMU_META_DNODE_OBJECT);
3165 break;
3167 if (dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
3168 dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
3169 VERIFY3U(dr->dr_dbuf->db_level, ==, level);
3171 list_remove(list, dr);
3172 if (dr->dr_dbuf->db_level > 0)
3173 dbuf_sync_indirect(dr, tx);
3174 else
3175 dbuf_sync_leaf(dr, tx);
3179 /* ARGSUSED */
3180 static void
3181 dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
3183 dmu_buf_impl_t *db = vdb;
3184 dnode_t *dn;
3185 blkptr_t *bp = zio->io_bp;
3186 blkptr_t *bp_orig = &zio->io_bp_orig;
3187 spa_t *spa = zio->io_spa;
3188 int64_t delta;
3189 uint64_t fill = 0;
3190 int i;
3192 ASSERT3P(db->db_blkptr, !=, NULL);
3193 ASSERT3P(&db->db_data_pending->dr_bp_copy, ==, bp);
3195 DB_DNODE_ENTER(db);
3196 dn = DB_DNODE(db);
3197 delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig);
3198 dnode_diduse_space(dn, delta - zio->io_prev_space_delta);
3199 zio->io_prev_space_delta = delta;
3201 if (bp->blk_birth != 0) {
3202 ASSERT((db->db_blkid != DMU_SPILL_BLKID &&
3203 BP_GET_TYPE(bp) == dn->dn_type) ||
3204 (db->db_blkid == DMU_SPILL_BLKID &&
3205 BP_GET_TYPE(bp) == dn->dn_bonustype) ||
3206 BP_IS_EMBEDDED(bp));
3207 ASSERT(BP_GET_LEVEL(bp) == db->db_level);
3210 mutex_enter(&db->db_mtx);
3212 #ifdef ZFS_DEBUG
3213 if (db->db_blkid == DMU_SPILL_BLKID) {
3214 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
3215 ASSERT(!(BP_IS_HOLE(bp)) &&
3216 db->db_blkptr == &dn->dn_phys->dn_spill);
3218 #endif
3220 if (db->db_level == 0) {
3221 mutex_enter(&dn->dn_mtx);
3222 if (db->db_blkid > dn->dn_phys->dn_maxblkid &&
3223 db->db_blkid != DMU_SPILL_BLKID)
3224 dn->dn_phys->dn_maxblkid = db->db_blkid;
3225 mutex_exit(&dn->dn_mtx);
3227 if (dn->dn_type == DMU_OT_DNODE) {
3228 dnode_phys_t *dnp = db->db.db_data;
3229 for (i = db->db.db_size >> DNODE_SHIFT; i > 0;
3230 i--, dnp++) {
3231 if (dnp->dn_type != DMU_OT_NONE)
3232 fill++;
3234 } else {
3235 if (BP_IS_HOLE(bp)) {
3236 fill = 0;
3237 } else {
3238 fill = 1;
3241 } else {
3242 blkptr_t *ibp = db->db.db_data;
3243 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
3244 for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) {
3245 if (BP_IS_HOLE(ibp))
3246 continue;
3247 fill += BP_GET_FILL(ibp);
3250 DB_DNODE_EXIT(db);
3252 if (!BP_IS_EMBEDDED(bp))
3253 bp->blk_fill = fill;
3255 mutex_exit(&db->db_mtx);
3257 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
3258 *db->db_blkptr = *bp;
3259 rw_exit(&dn->dn_struct_rwlock);
3262 /* ARGSUSED */
3264 * This function gets called just prior to running through the compression
3265 * stage of the zio pipeline. If we're an indirect block comprised of only
3266 * holes, then we want this indirect to be compressed away to a hole. In
3267 * order to do that we must zero out any information about the holes that
3268 * this indirect points to prior to before we try to compress it.
3270 static void
3271 dbuf_write_children_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
3273 dmu_buf_impl_t *db = vdb;
3274 dnode_t *dn;
3275 blkptr_t *bp;
3276 unsigned int epbs, i;
3278 ASSERT3U(db->db_level, >, 0);
3279 DB_DNODE_ENTER(db);
3280 dn = DB_DNODE(db);
3281 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
3282 ASSERT3U(epbs, <, 31);
3284 /* Determine if all our children are holes */
3285 for (i = 0, bp = db->db.db_data; i < 1 << epbs; i++, bp++) {
3286 if (!BP_IS_HOLE(bp))
3287 break;
3291 * If all the children are holes, then zero them all out so that
3292 * we may get compressed away.
3294 if (i == 1 << epbs) {
3296 * We only found holes. Grab the rwlock to prevent
3297 * anybody from reading the blocks we're about to
3298 * zero out.
3300 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
3301 bzero(db->db.db_data, db->db.db_size);
3302 rw_exit(&dn->dn_struct_rwlock);
3304 DB_DNODE_EXIT(db);
3308 * The SPA will call this callback several times for each zio - once
3309 * for every physical child i/o (zio->io_phys_children times). This
3310 * allows the DMU to monitor the progress of each logical i/o. For example,
3311 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
3312 * block. There may be a long delay before all copies/fragments are completed,
3313 * so this callback allows us to retire dirty space gradually, as the physical
3314 * i/os complete.
3316 /* ARGSUSED */
3317 static void
3318 dbuf_write_physdone(zio_t *zio, arc_buf_t *buf, void *arg)
3320 dmu_buf_impl_t *db = arg;
3321 objset_t *os = db->db_objset;
3322 dsl_pool_t *dp = dmu_objset_pool(os);
3323 dbuf_dirty_record_t *dr;
3324 int delta = 0;
3326 dr = db->db_data_pending;
3327 ASSERT3U(dr->dr_txg, ==, zio->io_txg);
3330 * The callback will be called io_phys_children times. Retire one
3331 * portion of our dirty space each time we are called. Any rounding
3332 * error will be cleaned up by dsl_pool_sync()'s call to
3333 * dsl_pool_undirty_space().
3335 delta = dr->dr_accounted / zio->io_phys_children;
3336 dsl_pool_undirty_space(dp, delta, zio->io_txg);
3339 /* ARGSUSED */
3340 static void
3341 dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
3343 dmu_buf_impl_t *db = vdb;
3344 blkptr_t *bp_orig = &zio->io_bp_orig;
3345 blkptr_t *bp = db->db_blkptr;
3346 objset_t *os = db->db_objset;
3347 dmu_tx_t *tx = os->os_synctx;
3348 dbuf_dirty_record_t **drp, *dr;
3350 ASSERT0(zio->io_error);
3351 ASSERT(db->db_blkptr == bp);
3354 * For nopwrites and rewrites we ensure that the bp matches our
3355 * original and bypass all the accounting.
3357 if (zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)) {
3358 ASSERT(BP_EQUAL(bp, bp_orig));
3359 } else {
3360 dsl_dataset_t *ds = os->os_dsl_dataset;
3361 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
3362 dsl_dataset_block_born(ds, bp, tx);
3365 mutex_enter(&db->db_mtx);
3367 DBUF_VERIFY(db);
3369 drp = &db->db_last_dirty;
3370 while ((dr = *drp) != db->db_data_pending)
3371 drp = &dr->dr_next;
3372 ASSERT(!list_link_active(&dr->dr_dirty_node));
3373 ASSERT(dr->dr_dbuf == db);
3374 ASSERT(dr->dr_next == NULL);
3375 *drp = dr->dr_next;
3377 #ifdef ZFS_DEBUG
3378 if (db->db_blkid == DMU_SPILL_BLKID) {
3379 dnode_t *dn;
3381 DB_DNODE_ENTER(db);
3382 dn = DB_DNODE(db);
3383 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
3384 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
3385 db->db_blkptr == &dn->dn_phys->dn_spill);
3386 DB_DNODE_EXIT(db);
3388 #endif
3390 if (db->db_level == 0) {
3391 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
3392 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
3393 if (db->db_state != DB_NOFILL) {
3394 if (dr->dt.dl.dr_data != db->db_buf)
3395 arc_buf_destroy(dr->dt.dl.dr_data, db);
3397 } else {
3398 dnode_t *dn;
3400 DB_DNODE_ENTER(db);
3401 dn = DB_DNODE(db);
3402 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
3403 ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
3404 if (!BP_IS_HOLE(db->db_blkptr)) {
3405 int epbs =
3406 dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
3407 ASSERT3U(db->db_blkid, <=,
3408 dn->dn_phys->dn_maxblkid >> (db->db_level * epbs));
3409 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
3410 db->db.db_size);
3412 DB_DNODE_EXIT(db);
3413 mutex_destroy(&dr->dt.di.dr_mtx);
3414 list_destroy(&dr->dt.di.dr_children);
3416 kmem_free(dr, sizeof (dbuf_dirty_record_t));
3418 cv_broadcast(&db->db_changed);
3419 ASSERT(db->db_dirtycnt > 0);
3420 db->db_dirtycnt -= 1;
3421 db->db_data_pending = NULL;
3422 dbuf_rele_and_unlock(db, (void *)(uintptr_t)tx->tx_txg);
3425 static void
3426 dbuf_write_nofill_ready(zio_t *zio)
3428 dbuf_write_ready(zio, NULL, zio->io_private);
3431 static void
3432 dbuf_write_nofill_done(zio_t *zio)
3434 dbuf_write_done(zio, NULL, zio->io_private);
3437 static void
3438 dbuf_write_override_ready(zio_t *zio)
3440 dbuf_dirty_record_t *dr = zio->io_private;
3441 dmu_buf_impl_t *db = dr->dr_dbuf;
3443 dbuf_write_ready(zio, NULL, db);
3446 static void
3447 dbuf_write_override_done(zio_t *zio)
3449 dbuf_dirty_record_t *dr = zio->io_private;
3450 dmu_buf_impl_t *db = dr->dr_dbuf;
3451 blkptr_t *obp = &dr->dt.dl.dr_overridden_by;
3453 mutex_enter(&db->db_mtx);
3454 if (!BP_EQUAL(zio->io_bp, obp)) {
3455 if (!BP_IS_HOLE(obp))
3456 dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp);
3457 arc_release(dr->dt.dl.dr_data, db);
3459 mutex_exit(&db->db_mtx);
3461 dbuf_write_done(zio, NULL, db);
3464 /* Issue I/O to commit a dirty buffer to disk. */
3465 static void
3466 dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx)
3468 dmu_buf_impl_t *db = dr->dr_dbuf;
3469 dnode_t *dn;
3470 objset_t *os;
3471 dmu_buf_impl_t *parent = db->db_parent;
3472 uint64_t txg = tx->tx_txg;
3473 zbookmark_phys_t zb;
3474 zio_prop_t zp;
3475 zio_t *zio;
3476 int wp_flag = 0;
3478 ASSERT(dmu_tx_is_syncing(tx));
3480 DB_DNODE_ENTER(db);
3481 dn = DB_DNODE(db);
3482 os = dn->dn_objset;
3484 if (db->db_state != DB_NOFILL) {
3485 if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) {
3487 * Private object buffers are released here rather
3488 * than in dbuf_dirty() since they are only modified
3489 * in the syncing context and we don't want the
3490 * overhead of making multiple copies of the data.
3492 if (BP_IS_HOLE(db->db_blkptr)) {
3493 arc_buf_thaw(data);
3494 } else {
3495 dbuf_release_bp(db);
3500 if (parent != dn->dn_dbuf) {
3501 /* Our parent is an indirect block. */
3502 /* We have a dirty parent that has been scheduled for write. */
3503 ASSERT(parent && parent->db_data_pending);
3504 /* Our parent's buffer is one level closer to the dnode. */
3505 ASSERT(db->db_level == parent->db_level-1);
3507 * We're about to modify our parent's db_data by modifying
3508 * our block pointer, so the parent must be released.
3510 ASSERT(arc_released(parent->db_buf));
3511 zio = parent->db_data_pending->dr_zio;
3512 } else {
3513 /* Our parent is the dnode itself. */
3514 ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 &&
3515 db->db_blkid != DMU_SPILL_BLKID) ||
3516 (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0));
3517 if (db->db_blkid != DMU_SPILL_BLKID)
3518 ASSERT3P(db->db_blkptr, ==,
3519 &dn->dn_phys->dn_blkptr[db->db_blkid]);
3520 zio = dn->dn_zio;
3523 ASSERT(db->db_level == 0 || data == db->db_buf);
3524 ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
3525 ASSERT(zio);
3527 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
3528 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
3529 db->db.db_object, db->db_level, db->db_blkid);
3531 if (db->db_blkid == DMU_SPILL_BLKID)
3532 wp_flag = WP_SPILL;
3533 wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
3535 dmu_write_policy(os, dn, db->db_level, wp_flag,
3536 (data != NULL && arc_get_compression(data) != ZIO_COMPRESS_OFF) ?
3537 arc_get_compression(data) : ZIO_COMPRESS_INHERIT, &zp);
3538 DB_DNODE_EXIT(db);
3541 * We copy the blkptr now (rather than when we instantiate the dirty
3542 * record), because its value can change between open context and
3543 * syncing context. We do not need to hold dn_struct_rwlock to read
3544 * db_blkptr because we are in syncing context.
3546 dr->dr_bp_copy = *db->db_blkptr;
3548 if (db->db_level == 0 &&
3549 dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
3551 * The BP for this block has been provided by open context
3552 * (by dmu_sync() or dmu_buf_write_embedded()).
3554 void *contents = (data != NULL) ? data->b_data : NULL;
3556 dr->dr_zio = zio_write(zio, os->os_spa, txg, &dr->dr_bp_copy,
3557 contents, db->db.db_size, db->db.db_size, &zp,
3558 dbuf_write_override_ready, NULL, NULL,
3559 dbuf_write_override_done,
3560 dr, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
3561 mutex_enter(&db->db_mtx);
3562 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
3563 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by,
3564 dr->dt.dl.dr_copies, dr->dt.dl.dr_nopwrite);
3565 mutex_exit(&db->db_mtx);
3566 } else if (db->db_state == DB_NOFILL) {
3567 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF ||
3568 zp.zp_checksum == ZIO_CHECKSUM_NOPARITY);
3569 dr->dr_zio = zio_write(zio, os->os_spa, txg,
3570 &dr->dr_bp_copy, NULL, db->db.db_size, db->db.db_size, &zp,
3571 dbuf_write_nofill_ready, NULL, NULL,
3572 dbuf_write_nofill_done, db,
3573 ZIO_PRIORITY_ASYNC_WRITE,
3574 ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb);
3575 } else {
3576 ASSERT(arc_released(data));
3579 * For indirect blocks, we want to setup the children
3580 * ready callback so that we can properly handle an indirect
3581 * block that only contains holes.
3583 arc_done_func_t *children_ready_cb = NULL;
3584 if (db->db_level != 0)
3585 children_ready_cb = dbuf_write_children_ready;
3587 dr->dr_zio = arc_write(zio, os->os_spa, txg,
3588 &dr->dr_bp_copy, data, DBUF_IS_L2CACHEABLE(db),
3589 &zp, dbuf_write_ready, children_ready_cb,
3590 dbuf_write_physdone, dbuf_write_done, db,
3591 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);