4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2012 by Delphix. All rights reserved.
25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
28 #include <sys/zfs_context.h>
31 #include <sys/dmu_impl.h>
33 #include <sys/dmu_objset.h>
34 #include <sys/dsl_dataset.h>
35 #include <sys/dsl_dir.h>
36 #include <sys/dmu_tx.h>
39 #include <sys/dmu_zfetch.h>
41 #include <sys/sa_impl.h>
43 struct dbuf_hold_impl_data {
44 /* Function arguments */
50 dmu_buf_impl_t **dh_dbp;
52 dmu_buf_impl_t *dh_db;
53 dmu_buf_impl_t *dh_parent;
56 dbuf_dirty_record_t *dh_dr;
57 arc_buf_contents_t dh_type;
61 static void __dbuf_hold_impl_init(struct dbuf_hold_impl_data *dh,
62 dnode_t *dn, uint8_t level, uint64_t blkid, int fail_sparse,
63 void *tag, dmu_buf_impl_t **dbp, int depth);
64 static int __dbuf_hold_impl(struct dbuf_hold_impl_data *dh);
66 static void dbuf_destroy(dmu_buf_impl_t *db);
67 static boolean_t dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
68 static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx);
71 * Global data structures and functions for the dbuf cache.
73 static kmem_cache_t *dbuf_cache;
77 dbuf_cons(void *vdb, void *unused, int kmflag)
79 dmu_buf_impl_t *db = vdb;
80 bzero(db, sizeof (dmu_buf_impl_t));
82 mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL);
83 cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL);
84 refcount_create(&db->db_holds);
85 list_link_init(&db->db_link);
91 dbuf_dest(void *vdb, void *unused)
93 dmu_buf_impl_t *db = vdb;
94 mutex_destroy(&db->db_mtx);
95 cv_destroy(&db->db_changed);
96 refcount_destroy(&db->db_holds);
100 * dbuf hash table routines
102 static dbuf_hash_table_t dbuf_hash_table;
104 static uint64_t dbuf_hash_count;
107 dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid)
109 uintptr_t osv = (uintptr_t)os;
110 uint64_t crc = -1ULL;
112 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
113 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (lvl)) & 0xFF];
114 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF];
115 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF];
116 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF];
117 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 0)) & 0xFF];
118 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 8)) & 0xFF];
120 crc ^= (osv>>14) ^ (obj>>16) ^ (blkid>>16);
125 #define DBUF_HASH(os, obj, level, blkid) dbuf_hash(os, obj, level, blkid);
127 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
128 ((dbuf)->db.db_object == (obj) && \
129 (dbuf)->db_objset == (os) && \
130 (dbuf)->db_level == (level) && \
131 (dbuf)->db_blkid == (blkid))
134 dbuf_find(dnode_t *dn, uint8_t level, uint64_t blkid)
136 dbuf_hash_table_t *h = &dbuf_hash_table;
137 objset_t *os = dn->dn_objset;
144 hv = DBUF_HASH(os, obj, level, blkid);
145 idx = hv & h->hash_table_mask;
147 mutex_enter(DBUF_HASH_MUTEX(h, idx));
148 for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) {
149 if (DBUF_EQUAL(db, os, obj, level, blkid)) {
150 mutex_enter(&db->db_mtx);
151 if (db->db_state != DB_EVICTING) {
152 mutex_exit(DBUF_HASH_MUTEX(h, idx));
155 mutex_exit(&db->db_mtx);
158 mutex_exit(DBUF_HASH_MUTEX(h, idx));
163 * Insert an entry into the hash table. If there is already an element
164 * equal to elem in the hash table, then the already existing element
165 * will be returned and the new element will not be inserted.
166 * Otherwise returns NULL.
168 static dmu_buf_impl_t *
169 dbuf_hash_insert(dmu_buf_impl_t *db)
171 dbuf_hash_table_t *h = &dbuf_hash_table;
172 objset_t *os = db->db_objset;
173 uint64_t obj = db->db.db_object;
174 int level = db->db_level;
175 uint64_t blkid, hv, idx;
178 blkid = db->db_blkid;
179 hv = DBUF_HASH(os, obj, level, blkid);
180 idx = hv & h->hash_table_mask;
182 mutex_enter(DBUF_HASH_MUTEX(h, idx));
183 for (dbf = h->hash_table[idx]; dbf != NULL; dbf = dbf->db_hash_next) {
184 if (DBUF_EQUAL(dbf, os, obj, level, blkid)) {
185 mutex_enter(&dbf->db_mtx);
186 if (dbf->db_state != DB_EVICTING) {
187 mutex_exit(DBUF_HASH_MUTEX(h, idx));
190 mutex_exit(&dbf->db_mtx);
194 mutex_enter(&db->db_mtx);
195 db->db_hash_next = h->hash_table[idx];
196 h->hash_table[idx] = db;
197 mutex_exit(DBUF_HASH_MUTEX(h, idx));
198 atomic_add_64(&dbuf_hash_count, 1);
204 * Remove an entry from the hash table. This operation will
205 * fail if there are any existing holds on the db.
208 dbuf_hash_remove(dmu_buf_impl_t *db)
210 dbuf_hash_table_t *h = &dbuf_hash_table;
212 dmu_buf_impl_t *dbf, **dbp;
214 hv = DBUF_HASH(db->db_objset, db->db.db_object,
215 db->db_level, db->db_blkid);
216 idx = hv & h->hash_table_mask;
219 * We musn't hold db_mtx to maintin lock ordering:
220 * DBUF_HASH_MUTEX > db_mtx.
222 ASSERT(refcount_is_zero(&db->db_holds));
223 ASSERT(db->db_state == DB_EVICTING);
224 ASSERT(!MUTEX_HELD(&db->db_mtx));
226 mutex_enter(DBUF_HASH_MUTEX(h, idx));
227 dbp = &h->hash_table[idx];
228 while ((dbf = *dbp) != db) {
229 dbp = &dbf->db_hash_next;
232 *dbp = db->db_hash_next;
233 db->db_hash_next = NULL;
234 mutex_exit(DBUF_HASH_MUTEX(h, idx));
235 atomic_add_64(&dbuf_hash_count, -1);
238 static arc_evict_func_t dbuf_do_evict;
241 dbuf_evict_user(dmu_buf_impl_t *db)
243 ASSERT(MUTEX_HELD(&db->db_mtx));
245 if (db->db_level != 0 || db->db_evict_func == NULL)
248 if (db->db_user_data_ptr_ptr)
249 *db->db_user_data_ptr_ptr = db->db.db_data;
250 db->db_evict_func(&db->db, db->db_user_ptr);
251 db->db_user_ptr = NULL;
252 db->db_user_data_ptr_ptr = NULL;
253 db->db_evict_func = NULL;
257 dbuf_is_metadata(dmu_buf_impl_t *db)
259 if (db->db_level > 0) {
262 boolean_t is_metadata;
265 is_metadata = DMU_OT_IS_METADATA(DB_DNODE(db)->dn_type);
268 return (is_metadata);
273 dbuf_evict(dmu_buf_impl_t *db)
275 ASSERT(MUTEX_HELD(&db->db_mtx));
276 ASSERT(db->db_buf == NULL);
277 ASSERT(db->db_data_pending == NULL);
286 uint64_t hsize = 1ULL << 16;
287 dbuf_hash_table_t *h = &dbuf_hash_table;
291 * The hash table is big enough to fill all of physical memory
292 * with an average 4K block size. The table will take up
293 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
295 while (hsize * 4096 < physmem * PAGESIZE)
299 h->hash_table_mask = hsize - 1;
300 #if defined(_KERNEL) && defined(HAVE_SPL)
301 /* Large allocations which do not require contiguous pages
302 * should be using vmem_alloc() in the linux kernel */
303 h->hash_table = vmem_zalloc(hsize * sizeof (void *), KM_PUSHPAGE);
305 h->hash_table = kmem_zalloc(hsize * sizeof (void *), KM_NOSLEEP);
307 if (h->hash_table == NULL) {
308 /* XXX - we should really return an error instead of assert */
309 ASSERT(hsize > (1ULL << 10));
314 dbuf_cache = kmem_cache_create("dmu_buf_impl_t",
315 sizeof (dmu_buf_impl_t),
316 0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0);
318 for (i = 0; i < DBUF_MUTEXES; i++)
319 mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL);
325 dbuf_hash_table_t *h = &dbuf_hash_table;
328 for (i = 0; i < DBUF_MUTEXES; i++)
329 mutex_destroy(&h->hash_mutexes[i]);
330 #if defined(_KERNEL) && defined(HAVE_SPL)
331 /* Large allocations which do not require contiguous pages
332 * should be using vmem_free() in the linux kernel */
333 vmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
335 kmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
337 kmem_cache_destroy(dbuf_cache);
346 dbuf_verify(dmu_buf_impl_t *db)
349 dbuf_dirty_record_t *dr;
351 ASSERT(MUTEX_HELD(&db->db_mtx));
353 if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY))
356 ASSERT(db->db_objset != NULL);
360 ASSERT(db->db_parent == NULL);
361 ASSERT(db->db_blkptr == NULL);
363 ASSERT3U(db->db.db_object, ==, dn->dn_object);
364 ASSERT3P(db->db_objset, ==, dn->dn_objset);
365 ASSERT3U(db->db_level, <, dn->dn_nlevels);
366 ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
367 db->db_blkid == DMU_SPILL_BLKID ||
368 !list_is_empty(&dn->dn_dbufs));
370 if (db->db_blkid == DMU_BONUS_BLKID) {
372 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
373 ASSERT3U(db->db.db_offset, ==, DMU_BONUS_BLKID);
374 } else if (db->db_blkid == DMU_SPILL_BLKID) {
376 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
377 ASSERT0(db->db.db_offset);
379 ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size);
382 for (dr = db->db_data_pending; dr != NULL; dr = dr->dr_next)
383 ASSERT(dr->dr_dbuf == db);
385 for (dr = db->db_last_dirty; dr != NULL; dr = dr->dr_next)
386 ASSERT(dr->dr_dbuf == db);
389 * We can't assert that db_size matches dn_datablksz because it
390 * can be momentarily different when another thread is doing
393 if (db->db_level == 0 && db->db.db_object == DMU_META_DNODE_OBJECT) {
394 dr = db->db_data_pending;
396 * It should only be modified in syncing context, so
397 * make sure we only have one copy of the data.
399 ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf);
402 /* verify db->db_blkptr */
404 if (db->db_parent == dn->dn_dbuf) {
405 /* db is pointed to by the dnode */
406 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
407 if (DMU_OBJECT_IS_SPECIAL(db->db.db_object))
408 ASSERT(db->db_parent == NULL);
410 ASSERT(db->db_parent != NULL);
411 if (db->db_blkid != DMU_SPILL_BLKID)
412 ASSERT3P(db->db_blkptr, ==,
413 &dn->dn_phys->dn_blkptr[db->db_blkid]);
415 /* db is pointed to by an indirect block */
416 ASSERTV(int epb = db->db_parent->db.db_size >>
418 ASSERT3U(db->db_parent->db_level, ==, db->db_level+1);
419 ASSERT3U(db->db_parent->db.db_object, ==,
422 * dnode_grow_indblksz() can make this fail if we don't
423 * have the struct_rwlock. XXX indblksz no longer
424 * grows. safe to do this now?
426 if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
427 ASSERT3P(db->db_blkptr, ==,
428 ((blkptr_t *)db->db_parent->db.db_data +
429 db->db_blkid % epb));
433 if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) &&
434 (db->db_buf == NULL || db->db_buf->b_data) &&
435 db->db.db_data && db->db_blkid != DMU_BONUS_BLKID &&
436 db->db_state != DB_FILL && !dn->dn_free_txg) {
438 * If the blkptr isn't set but they have nonzero data,
439 * it had better be dirty, otherwise we'll lose that
440 * data when we evict this buffer.
442 if (db->db_dirtycnt == 0) {
443 ASSERTV(uint64_t *buf = db->db.db_data);
446 for (i = 0; i < db->db.db_size >> 3; i++) {
456 dbuf_update_data(dmu_buf_impl_t *db)
458 ASSERT(MUTEX_HELD(&db->db_mtx));
459 if (db->db_level == 0 && db->db_user_data_ptr_ptr) {
460 ASSERT(!refcount_is_zero(&db->db_holds));
461 *db->db_user_data_ptr_ptr = db->db.db_data;
466 dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf)
468 ASSERT(MUTEX_HELD(&db->db_mtx));
469 ASSERT(db->db_buf == NULL || !arc_has_callback(db->db_buf));
472 ASSERT(buf->b_data != NULL);
473 db->db.db_data = buf->b_data;
474 if (!arc_released(buf))
475 arc_set_callback(buf, dbuf_do_evict, db);
476 dbuf_update_data(db);
479 db->db.db_data = NULL;
480 if (db->db_state != DB_NOFILL)
481 db->db_state = DB_UNCACHED;
486 * Loan out an arc_buf for read. Return the loaned arc_buf.
489 dbuf_loan_arcbuf(dmu_buf_impl_t *db)
493 mutex_enter(&db->db_mtx);
494 if (arc_released(db->db_buf) || refcount_count(&db->db_holds) > 1) {
495 int blksz = db->db.db_size;
498 mutex_exit(&db->db_mtx);
499 DB_GET_SPA(&spa, db);
500 abuf = arc_loan_buf(spa, blksz);
501 bcopy(db->db.db_data, abuf->b_data, blksz);
504 arc_loan_inuse_buf(abuf, db);
505 dbuf_set_data(db, NULL);
506 mutex_exit(&db->db_mtx);
512 dbuf_whichblock(dnode_t *dn, uint64_t offset)
514 if (dn->dn_datablkshift) {
515 return (offset >> dn->dn_datablkshift);
517 ASSERT3U(offset, <, dn->dn_datablksz);
523 dbuf_read_done(zio_t *zio, arc_buf_t *buf, void *vdb)
525 dmu_buf_impl_t *db = vdb;
527 mutex_enter(&db->db_mtx);
528 ASSERT3U(db->db_state, ==, DB_READ);
530 * All reads are synchronous, so we must have a hold on the dbuf
532 ASSERT(refcount_count(&db->db_holds) > 0);
533 ASSERT(db->db_buf == NULL);
534 ASSERT(db->db.db_data == NULL);
535 if (db->db_level == 0 && db->db_freed_in_flight) {
536 /* we were freed in flight; disregard any error */
537 arc_release(buf, db);
538 bzero(buf->b_data, db->db.db_size);
540 db->db_freed_in_flight = FALSE;
541 dbuf_set_data(db, buf);
542 db->db_state = DB_CACHED;
543 } else if (zio == NULL || zio->io_error == 0) {
544 dbuf_set_data(db, buf);
545 db->db_state = DB_CACHED;
547 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
548 ASSERT3P(db->db_buf, ==, NULL);
549 VERIFY(arc_buf_remove_ref(buf, db));
550 db->db_state = DB_UNCACHED;
552 cv_broadcast(&db->db_changed);
553 dbuf_rele_and_unlock(db, NULL);
557 dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t *flags)
562 uint32_t aflags = ARC_NOWAIT;
566 ASSERT(!refcount_is_zero(&db->db_holds));
567 /* We need the struct_rwlock to prevent db_blkptr from changing. */
568 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
569 ASSERT(MUTEX_HELD(&db->db_mtx));
570 ASSERT(db->db_state == DB_UNCACHED);
571 ASSERT(db->db_buf == NULL);
573 if (db->db_blkid == DMU_BONUS_BLKID) {
574 int bonuslen = MIN(dn->dn_bonuslen, dn->dn_phys->dn_bonuslen);
576 ASSERT3U(bonuslen, <=, db->db.db_size);
577 db->db.db_data = zio_buf_alloc(DN_MAX_BONUSLEN);
578 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
579 if (bonuslen < DN_MAX_BONUSLEN)
580 bzero(db->db.db_data, DN_MAX_BONUSLEN);
582 bcopy(DN_BONUS(dn->dn_phys), db->db.db_data, bonuslen);
584 dbuf_update_data(db);
585 db->db_state = DB_CACHED;
586 mutex_exit(&db->db_mtx);
591 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
592 * processes the delete record and clears the bp while we are waiting
593 * for the dn_mtx (resulting in a "no" from block_freed).
595 if (db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr) ||
596 (db->db_level == 0 && (dnode_block_freed(dn, db->db_blkid) ||
597 BP_IS_HOLE(db->db_blkptr)))) {
598 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
600 dbuf_set_data(db, arc_buf_alloc(dn->dn_objset->os_spa,
601 db->db.db_size, db, type));
603 bzero(db->db.db_data, db->db.db_size);
604 db->db_state = DB_CACHED;
605 *flags |= DB_RF_CACHED;
606 mutex_exit(&db->db_mtx);
610 spa = dn->dn_objset->os_spa;
613 db->db_state = DB_READ;
614 mutex_exit(&db->db_mtx);
616 if (DBUF_IS_L2CACHEABLE(db))
617 aflags |= ARC_L2CACHE;
618 if (DBUF_IS_L2COMPRESSIBLE(db))
619 aflags |= ARC_L2COMPRESS;
621 SET_BOOKMARK(&zb, db->db_objset->os_dsl_dataset ?
622 db->db_objset->os_dsl_dataset->ds_object : DMU_META_OBJSET,
623 db->db.db_object, db->db_level, db->db_blkid);
625 dbuf_add_ref(db, NULL);
627 (void) arc_read(zio, spa, db->db_blkptr,
628 dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ,
629 (*flags & DB_RF_CANFAIL) ? ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED,
631 if (aflags & ARC_CACHED)
632 *flags |= DB_RF_CACHED;
636 dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
639 int havepzio = (zio != NULL);
644 * We don't have to hold the mutex to check db_state because it
645 * can't be freed while we have a hold on the buffer.
647 ASSERT(!refcount_is_zero(&db->db_holds));
649 if (db->db_state == DB_NOFILL)
654 if ((flags & DB_RF_HAVESTRUCT) == 0)
655 rw_enter(&dn->dn_struct_rwlock, RW_READER);
657 prefetch = db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
658 (flags & DB_RF_NOPREFETCH) == 0 && dn != NULL &&
659 DBUF_IS_CACHEABLE(db);
661 mutex_enter(&db->db_mtx);
662 if (db->db_state == DB_CACHED) {
663 mutex_exit(&db->db_mtx);
665 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
666 db->db.db_size, TRUE);
667 if ((flags & DB_RF_HAVESTRUCT) == 0)
668 rw_exit(&dn->dn_struct_rwlock);
670 } else if (db->db_state == DB_UNCACHED) {
671 spa_t *spa = dn->dn_objset->os_spa;
674 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
675 dbuf_read_impl(db, zio, &flags);
677 /* dbuf_read_impl has dropped db_mtx for us */
680 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
681 db->db.db_size, flags & DB_RF_CACHED);
683 if ((flags & DB_RF_HAVESTRUCT) == 0)
684 rw_exit(&dn->dn_struct_rwlock);
690 mutex_exit(&db->db_mtx);
692 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
693 db->db.db_size, TRUE);
694 if ((flags & DB_RF_HAVESTRUCT) == 0)
695 rw_exit(&dn->dn_struct_rwlock);
698 mutex_enter(&db->db_mtx);
699 if ((flags & DB_RF_NEVERWAIT) == 0) {
700 while (db->db_state == DB_READ ||
701 db->db_state == DB_FILL) {
702 ASSERT(db->db_state == DB_READ ||
703 (flags & DB_RF_HAVESTRUCT) == 0);
704 cv_wait(&db->db_changed, &db->db_mtx);
706 if (db->db_state == DB_UNCACHED)
709 mutex_exit(&db->db_mtx);
712 ASSERT(err || havepzio || db->db_state == DB_CACHED);
717 dbuf_noread(dmu_buf_impl_t *db)
719 ASSERT(!refcount_is_zero(&db->db_holds));
720 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
721 mutex_enter(&db->db_mtx);
722 while (db->db_state == DB_READ || db->db_state == DB_FILL)
723 cv_wait(&db->db_changed, &db->db_mtx);
724 if (db->db_state == DB_UNCACHED) {
725 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
728 ASSERT(db->db_buf == NULL);
729 ASSERT(db->db.db_data == NULL);
730 DB_GET_SPA(&spa, db);
731 dbuf_set_data(db, arc_buf_alloc(spa, db->db.db_size, db, type));
732 db->db_state = DB_FILL;
733 } else if (db->db_state == DB_NOFILL) {
734 dbuf_set_data(db, NULL);
736 ASSERT3U(db->db_state, ==, DB_CACHED);
738 mutex_exit(&db->db_mtx);
742 * This is our just-in-time copy function. It makes a copy of
743 * buffers, that have been modified in a previous transaction
744 * group, before we modify them in the current active group.
746 * This function is used in two places: when we are dirtying a
747 * buffer for the first time in a txg, and when we are freeing
748 * a range in a dnode that includes this buffer.
750 * Note that when we are called from dbuf_free_range() we do
751 * not put a hold on the buffer, we just traverse the active
752 * dbuf list for the dnode.
755 dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg)
757 dbuf_dirty_record_t *dr = db->db_last_dirty;
759 ASSERT(MUTEX_HELD(&db->db_mtx));
760 ASSERT(db->db.db_data != NULL);
761 ASSERT(db->db_level == 0);
762 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT);
765 (dr->dt.dl.dr_data !=
766 ((db->db_blkid == DMU_BONUS_BLKID) ? db->db.db_data : db->db_buf)))
770 * If the last dirty record for this dbuf has not yet synced
771 * and its referencing the dbuf data, either:
772 * reset the reference to point to a new copy,
773 * or (if there a no active holders)
774 * just null out the current db_data pointer.
776 ASSERT(dr->dr_txg >= txg - 2);
777 if (db->db_blkid == DMU_BONUS_BLKID) {
778 /* Note that the data bufs here are zio_bufs */
779 dr->dt.dl.dr_data = zio_buf_alloc(DN_MAX_BONUSLEN);
780 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
781 bcopy(db->db.db_data, dr->dt.dl.dr_data, DN_MAX_BONUSLEN);
782 } else if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
783 int size = db->db.db_size;
784 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
787 DB_GET_SPA(&spa, db);
788 dr->dt.dl.dr_data = arc_buf_alloc(spa, size, db, type);
789 bcopy(db->db.db_data, dr->dt.dl.dr_data->b_data, size);
791 dbuf_set_data(db, NULL);
796 dbuf_unoverride(dbuf_dirty_record_t *dr)
798 dmu_buf_impl_t *db = dr->dr_dbuf;
799 blkptr_t *bp = &dr->dt.dl.dr_overridden_by;
800 uint64_t txg = dr->dr_txg;
802 ASSERT(MUTEX_HELD(&db->db_mtx));
803 ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC);
804 ASSERT(db->db_level == 0);
806 if (db->db_blkid == DMU_BONUS_BLKID ||
807 dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN)
810 ASSERT(db->db_data_pending != dr);
812 /* free this block */
813 if (!BP_IS_HOLE(bp)) {
816 DB_GET_SPA(&spa, db);
817 zio_free(spa, txg, bp);
819 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
821 * Release the already-written buffer, so we leave it in
822 * a consistent dirty state. Note that all callers are
823 * modifying the buffer, so they will immediately do
824 * another (redundant) arc_release(). Therefore, leave
825 * the buf thawed to save the effort of freezing &
826 * immediately re-thawing it.
828 arc_release(dr->dt.dl.dr_data, db);
832 * Evict (if its unreferenced) or clear (if its referenced) any level-0
833 * data blocks in the free range, so that any future readers will find
834 * empty blocks. Also, if we happen accross any level-1 dbufs in the
835 * range that have not already been marked dirty, mark them dirty so
836 * they stay in memory.
839 dbuf_free_range(dnode_t *dn, uint64_t start, uint64_t end, dmu_tx_t *tx)
841 dmu_buf_impl_t *db, *db_next;
842 uint64_t txg = tx->tx_txg;
843 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
844 uint64_t first_l1 = start >> epbs;
845 uint64_t last_l1 = end >> epbs;
847 if (end > dn->dn_maxblkid && (end != DMU_SPILL_BLKID)) {
848 end = dn->dn_maxblkid;
849 last_l1 = end >> epbs;
851 dprintf_dnode(dn, "start=%llu end=%llu\n", start, end);
852 mutex_enter(&dn->dn_dbufs_mtx);
853 for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
854 db_next = list_next(&dn->dn_dbufs, db);
855 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
857 if (db->db_level == 1 &&
858 db->db_blkid >= first_l1 && db->db_blkid <= last_l1) {
859 mutex_enter(&db->db_mtx);
860 if (db->db_last_dirty &&
861 db->db_last_dirty->dr_txg < txg) {
862 dbuf_add_ref(db, FTAG);
863 mutex_exit(&db->db_mtx);
864 dbuf_will_dirty(db, tx);
867 mutex_exit(&db->db_mtx);
871 if (db->db_level != 0)
873 dprintf_dbuf(db, "found buf %s\n", "");
874 if (db->db_blkid < start || db->db_blkid > end)
877 /* found a level 0 buffer in the range */
878 mutex_enter(&db->db_mtx);
879 if (dbuf_undirty(db, tx)) {
880 /* mutex has been dropped and dbuf destroyed */
884 if (db->db_state == DB_UNCACHED ||
885 db->db_state == DB_NOFILL ||
886 db->db_state == DB_EVICTING) {
887 ASSERT(db->db.db_data == NULL);
888 mutex_exit(&db->db_mtx);
891 if (db->db_state == DB_READ || db->db_state == DB_FILL) {
892 /* will be handled in dbuf_read_done or dbuf_rele */
893 db->db_freed_in_flight = TRUE;
894 mutex_exit(&db->db_mtx);
897 if (refcount_count(&db->db_holds) == 0) {
902 /* The dbuf is referenced */
904 if (db->db_last_dirty != NULL) {
905 dbuf_dirty_record_t *dr = db->db_last_dirty;
907 if (dr->dr_txg == txg) {
909 * This buffer is "in-use", re-adjust the file
910 * size to reflect that this buffer may
911 * contain new data when we sync.
913 if (db->db_blkid != DMU_SPILL_BLKID &&
914 db->db_blkid > dn->dn_maxblkid)
915 dn->dn_maxblkid = db->db_blkid;
919 * This dbuf is not dirty in the open context.
920 * Either uncache it (if its not referenced in
921 * the open context) or reset its contents to
924 dbuf_fix_old_data(db, txg);
927 /* clear the contents if its cached */
928 if (db->db_state == DB_CACHED) {
929 ASSERT(db->db.db_data != NULL);
930 arc_release(db->db_buf, db);
931 bzero(db->db.db_data, db->db.db_size);
932 arc_buf_freeze(db->db_buf);
935 mutex_exit(&db->db_mtx);
937 mutex_exit(&dn->dn_dbufs_mtx);
941 dbuf_block_freeable(dmu_buf_impl_t *db)
943 dsl_dataset_t *ds = db->db_objset->os_dsl_dataset;
944 uint64_t birth_txg = 0;
947 * We don't need any locking to protect db_blkptr:
948 * If it's syncing, then db_last_dirty will be set
949 * so we'll ignore db_blkptr.
951 ASSERT(MUTEX_HELD(&db->db_mtx));
952 if (db->db_last_dirty)
953 birth_txg = db->db_last_dirty->dr_txg;
954 else if (db->db_blkptr)
955 birth_txg = db->db_blkptr->blk_birth;
958 * If we don't exist or are in a snapshot, we can't be freed.
959 * Don't pass the bp to dsl_dataset_block_freeable() since we
960 * are holding the db_mtx lock and might deadlock if we are
961 * prefetching a dedup-ed block.
964 return (ds == NULL ||
965 dsl_dataset_block_freeable(ds, NULL, birth_txg));
971 dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx)
973 arc_buf_t *buf, *obuf;
974 int osize = db->db.db_size;
975 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
978 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
983 /* XXX does *this* func really need the lock? */
984 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
987 * This call to dbuf_will_dirty() with the dn_struct_rwlock held
988 * is OK, because there can be no other references to the db
989 * when we are changing its size, so no concurrent DB_FILL can
993 * XXX we should be doing a dbuf_read, checking the return
994 * value and returning that up to our callers
996 dbuf_will_dirty(db, tx);
998 /* create the data buffer for the new block */
999 buf = arc_buf_alloc(dn->dn_objset->os_spa, size, db, type);
1001 /* copy old block data to the new block */
1003 bcopy(obuf->b_data, buf->b_data, MIN(osize, size));
1004 /* zero the remainder */
1006 bzero((uint8_t *)buf->b_data + osize, size - osize);
1008 mutex_enter(&db->db_mtx);
1009 dbuf_set_data(db, buf);
1010 VERIFY(arc_buf_remove_ref(obuf, db));
1011 db->db.db_size = size;
1013 if (db->db_level == 0) {
1014 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
1015 db->db_last_dirty->dt.dl.dr_data = buf;
1017 mutex_exit(&db->db_mtx);
1019 dnode_willuse_space(dn, size-osize, tx);
1024 dbuf_release_bp(dmu_buf_impl_t *db)
1028 DB_GET_OBJSET(&os, db);
1029 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
1030 ASSERT(arc_released(os->os_phys_buf) ||
1031 list_link_active(&os->os_dsl_dataset->ds_synced_link));
1032 ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf));
1034 (void) arc_release(db->db_buf, db);
1037 dbuf_dirty_record_t *
1038 dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1042 dbuf_dirty_record_t **drp, *dr;
1043 int drop_struct_lock = FALSE;
1044 boolean_t do_free_accounting = B_FALSE;
1045 int txgoff = tx->tx_txg & TXG_MASK;
1047 ASSERT(tx->tx_txg != 0);
1048 ASSERT(!refcount_is_zero(&db->db_holds));
1049 DMU_TX_DIRTY_BUF(tx, db);
1054 * Shouldn't dirty a regular buffer in syncing context. Private
1055 * objects may be dirtied in syncing context, but only if they
1056 * were already pre-dirtied in open context.
1058 ASSERT(!dmu_tx_is_syncing(tx) ||
1059 BP_IS_HOLE(dn->dn_objset->os_rootbp) ||
1060 DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1061 dn->dn_objset->os_dsl_dataset == NULL);
1063 * We make this assert for private objects as well, but after we
1064 * check if we're already dirty. They are allowed to re-dirty
1065 * in syncing context.
1067 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1068 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1069 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1071 mutex_enter(&db->db_mtx);
1073 * XXX make this true for indirects too? The problem is that
1074 * transactions created with dmu_tx_create_assigned() from
1075 * syncing context don't bother holding ahead.
1077 ASSERT(db->db_level != 0 ||
1078 db->db_state == DB_CACHED || db->db_state == DB_FILL ||
1079 db->db_state == DB_NOFILL);
1081 mutex_enter(&dn->dn_mtx);
1083 * Don't set dirtyctx to SYNC if we're just modifying this as we
1084 * initialize the objset.
1086 if (dn->dn_dirtyctx == DN_UNDIRTIED &&
1087 !BP_IS_HOLE(dn->dn_objset->os_rootbp)) {
1089 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN);
1090 ASSERT(dn->dn_dirtyctx_firstset == NULL);
1091 dn->dn_dirtyctx_firstset = kmem_alloc(1, KM_PUSHPAGE);
1093 mutex_exit(&dn->dn_mtx);
1095 if (db->db_blkid == DMU_SPILL_BLKID)
1096 dn->dn_have_spill = B_TRUE;
1099 * If this buffer is already dirty, we're done.
1101 drp = &db->db_last_dirty;
1102 ASSERT(*drp == NULL || (*drp)->dr_txg <= tx->tx_txg ||
1103 db->db.db_object == DMU_META_DNODE_OBJECT);
1104 while ((dr = *drp) != NULL && dr->dr_txg > tx->tx_txg)
1106 if (dr && dr->dr_txg == tx->tx_txg) {
1109 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID) {
1111 * If this buffer has already been written out,
1112 * we now need to reset its state.
1114 dbuf_unoverride(dr);
1115 if (db->db.db_object != DMU_META_DNODE_OBJECT &&
1116 db->db_state != DB_NOFILL)
1117 arc_buf_thaw(db->db_buf);
1119 mutex_exit(&db->db_mtx);
1124 * Only valid if not already dirty.
1126 ASSERT(dn->dn_object == 0 ||
1127 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1128 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1130 ASSERT3U(dn->dn_nlevels, >, db->db_level);
1131 ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) ||
1132 dn->dn_phys->dn_nlevels > db->db_level ||
1133 dn->dn_next_nlevels[txgoff] > db->db_level ||
1134 dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level ||
1135 dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level);
1138 * We should only be dirtying in syncing context if it's the
1139 * mos or we're initializing the os or it's a special object.
1140 * However, we are allowed to dirty in syncing context provided
1141 * we already dirtied it in open context. Hence we must make
1142 * this assertion only if we're not already dirty.
1145 ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1146 os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp));
1147 ASSERT(db->db.db_size != 0);
1149 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1151 if (db->db_blkid != DMU_BONUS_BLKID) {
1153 * Update the accounting.
1154 * Note: we delay "free accounting" until after we drop
1155 * the db_mtx. This keeps us from grabbing other locks
1156 * (and possibly deadlocking) in bp_get_dsize() while
1157 * also holding the db_mtx.
1159 dnode_willuse_space(dn, db->db.db_size, tx);
1160 do_free_accounting = dbuf_block_freeable(db);
1164 * If this buffer is dirty in an old transaction group we need
1165 * to make a copy of it so that the changes we make in this
1166 * transaction group won't leak out when we sync the older txg.
1168 dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_PUSHPAGE);
1169 list_link_init(&dr->dr_dirty_node);
1170 if (db->db_level == 0) {
1171 void *data_old = db->db_buf;
1173 if (db->db_state != DB_NOFILL) {
1174 if (db->db_blkid == DMU_BONUS_BLKID) {
1175 dbuf_fix_old_data(db, tx->tx_txg);
1176 data_old = db->db.db_data;
1177 } else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
1179 * Release the data buffer from the cache so
1180 * that we can modify it without impacting
1181 * possible other users of this cached data
1182 * block. Note that indirect blocks and
1183 * private objects are not released until the
1184 * syncing state (since they are only modified
1187 arc_release(db->db_buf, db);
1188 dbuf_fix_old_data(db, tx->tx_txg);
1189 data_old = db->db_buf;
1191 ASSERT(data_old != NULL);
1193 dr->dt.dl.dr_data = data_old;
1195 mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_DEFAULT, NULL);
1196 list_create(&dr->dt.di.dr_children,
1197 sizeof (dbuf_dirty_record_t),
1198 offsetof(dbuf_dirty_record_t, dr_dirty_node));
1201 dr->dr_txg = tx->tx_txg;
1206 * We could have been freed_in_flight between the dbuf_noread
1207 * and dbuf_dirty. We win, as though the dbuf_noread() had
1208 * happened after the free.
1210 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1211 db->db_blkid != DMU_SPILL_BLKID) {
1212 mutex_enter(&dn->dn_mtx);
1213 dnode_clear_range(dn, db->db_blkid, 1, tx);
1214 mutex_exit(&dn->dn_mtx);
1215 db->db_freed_in_flight = FALSE;
1219 * This buffer is now part of this txg
1221 dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg);
1222 db->db_dirtycnt += 1;
1223 ASSERT3U(db->db_dirtycnt, <=, 3);
1225 mutex_exit(&db->db_mtx);
1227 if (db->db_blkid == DMU_BONUS_BLKID ||
1228 db->db_blkid == DMU_SPILL_BLKID) {
1229 mutex_enter(&dn->dn_mtx);
1230 ASSERT(!list_link_active(&dr->dr_dirty_node));
1231 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1232 mutex_exit(&dn->dn_mtx);
1233 dnode_setdirty(dn, tx);
1236 } else if (do_free_accounting) {
1237 blkptr_t *bp = db->db_blkptr;
1238 int64_t willfree = (bp && !BP_IS_HOLE(bp)) ?
1239 bp_get_dsize(os->os_spa, bp) : db->db.db_size;
1241 * This is only a guess -- if the dbuf is dirty
1242 * in a previous txg, we don't know how much
1243 * space it will use on disk yet. We should
1244 * really have the struct_rwlock to access
1245 * db_blkptr, but since this is just a guess,
1246 * it's OK if we get an odd answer.
1248 ddt_prefetch(os->os_spa, bp);
1249 dnode_willuse_space(dn, -willfree, tx);
1252 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
1253 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1254 drop_struct_lock = TRUE;
1257 if (db->db_level == 0) {
1258 dnode_new_blkid(dn, db->db_blkid, tx, drop_struct_lock);
1259 ASSERT(dn->dn_maxblkid >= db->db_blkid);
1262 if (db->db_level+1 < dn->dn_nlevels) {
1263 dmu_buf_impl_t *parent = db->db_parent;
1264 dbuf_dirty_record_t *di;
1265 int parent_held = FALSE;
1267 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
1268 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1270 parent = dbuf_hold_level(dn, db->db_level+1,
1271 db->db_blkid >> epbs, FTAG);
1272 ASSERT(parent != NULL);
1275 if (drop_struct_lock)
1276 rw_exit(&dn->dn_struct_rwlock);
1277 ASSERT3U(db->db_level+1, ==, parent->db_level);
1278 di = dbuf_dirty(parent, tx);
1280 dbuf_rele(parent, FTAG);
1282 mutex_enter(&db->db_mtx);
1283 /* possible race with dbuf_undirty() */
1284 if (db->db_last_dirty == dr ||
1285 dn->dn_object == DMU_META_DNODE_OBJECT) {
1286 mutex_enter(&di->dt.di.dr_mtx);
1287 ASSERT3U(di->dr_txg, ==, tx->tx_txg);
1288 ASSERT(!list_link_active(&dr->dr_dirty_node));
1289 list_insert_tail(&di->dt.di.dr_children, dr);
1290 mutex_exit(&di->dt.di.dr_mtx);
1293 mutex_exit(&db->db_mtx);
1295 ASSERT(db->db_level+1 == dn->dn_nlevels);
1296 ASSERT(db->db_blkid < dn->dn_nblkptr);
1297 ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf);
1298 mutex_enter(&dn->dn_mtx);
1299 ASSERT(!list_link_active(&dr->dr_dirty_node));
1300 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1301 mutex_exit(&dn->dn_mtx);
1302 if (drop_struct_lock)
1303 rw_exit(&dn->dn_struct_rwlock);
1306 dnode_setdirty(dn, tx);
1312 * Return TRUE if this evicted the dbuf.
1315 dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1318 uint64_t txg = tx->tx_txg;
1319 dbuf_dirty_record_t *dr, **drp;
1322 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1323 ASSERT0(db->db_level);
1324 ASSERT(MUTEX_HELD(&db->db_mtx));
1327 * If this buffer is not dirty, we're done.
1329 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1330 if (dr->dr_txg <= txg)
1332 if (dr == NULL || dr->dr_txg < txg)
1334 ASSERT(dr->dr_txg == txg);
1335 ASSERT(dr->dr_dbuf == db);
1341 * Note: This code will probably work even if there are concurrent
1342 * holders, but it is untested in that scenerio, as the ZPL and
1343 * ztest have additional locking (the range locks) that prevents
1344 * that type of concurrent access.
1346 ASSERT3U(refcount_count(&db->db_holds), ==, db->db_dirtycnt);
1348 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1350 ASSERT(db->db.db_size != 0);
1352 /* XXX would be nice to fix up dn_towrite_space[] */
1357 * Note that there are three places in dbuf_dirty()
1358 * where this dirty record may be put on a list.
1359 * Make sure to do a list_remove corresponding to
1360 * every one of those list_insert calls.
1362 if (dr->dr_parent) {
1363 mutex_enter(&dr->dr_parent->dt.di.dr_mtx);
1364 list_remove(&dr->dr_parent->dt.di.dr_children, dr);
1365 mutex_exit(&dr->dr_parent->dt.di.dr_mtx);
1366 } else if (db->db_blkid == DMU_SPILL_BLKID ||
1367 db->db_level+1 == dn->dn_nlevels) {
1368 ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf);
1369 mutex_enter(&dn->dn_mtx);
1370 list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr);
1371 mutex_exit(&dn->dn_mtx);
1375 if (db->db_state != DB_NOFILL) {
1376 dbuf_unoverride(dr);
1378 ASSERT(db->db_buf != NULL);
1379 ASSERT(dr->dt.dl.dr_data != NULL);
1380 if (dr->dt.dl.dr_data != db->db_buf)
1381 VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data, db));
1383 kmem_free(dr, sizeof (dbuf_dirty_record_t));
1385 ASSERT(db->db_dirtycnt > 0);
1386 db->db_dirtycnt -= 1;
1388 if (refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) {
1389 arc_buf_t *buf = db->db_buf;
1391 ASSERT(db->db_state == DB_NOFILL || arc_released(buf));
1392 dbuf_set_data(db, NULL);
1393 VERIFY(arc_buf_remove_ref(buf, db));
1401 #pragma weak dmu_buf_will_dirty = dbuf_will_dirty
1403 dbuf_will_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1405 int rf = DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH;
1407 ASSERT(tx->tx_txg != 0);
1408 ASSERT(!refcount_is_zero(&db->db_holds));
1411 if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock))
1412 rf |= DB_RF_HAVESTRUCT;
1414 (void) dbuf_read(db, NULL, rf);
1415 (void) dbuf_dirty(db, tx);
1419 dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1421 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1423 db->db_state = DB_NOFILL;
1425 dmu_buf_will_fill(db_fake, tx);
1429 dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1431 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1433 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1434 ASSERT(tx->tx_txg != 0);
1435 ASSERT(db->db_level == 0);
1436 ASSERT(!refcount_is_zero(&db->db_holds));
1438 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
1439 dmu_tx_private_ok(tx));
1442 (void) dbuf_dirty(db, tx);
1445 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1448 dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx)
1450 mutex_enter(&db->db_mtx);
1453 if (db->db_state == DB_FILL) {
1454 if (db->db_level == 0 && db->db_freed_in_flight) {
1455 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1456 /* we were freed while filling */
1457 /* XXX dbuf_undirty? */
1458 bzero(db->db.db_data, db->db.db_size);
1459 db->db_freed_in_flight = FALSE;
1461 db->db_state = DB_CACHED;
1462 cv_broadcast(&db->db_changed);
1464 mutex_exit(&db->db_mtx);
1468 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1469 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1472 dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx)
1474 ASSERT(!refcount_is_zero(&db->db_holds));
1475 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1476 ASSERT(db->db_level == 0);
1477 ASSERT(DBUF_GET_BUFC_TYPE(db) == ARC_BUFC_DATA);
1478 ASSERT(buf != NULL);
1479 ASSERT(arc_buf_size(buf) == db->db.db_size);
1480 ASSERT(tx->tx_txg != 0);
1482 arc_return_buf(buf, db);
1483 ASSERT(arc_released(buf));
1485 mutex_enter(&db->db_mtx);
1487 while (db->db_state == DB_READ || db->db_state == DB_FILL)
1488 cv_wait(&db->db_changed, &db->db_mtx);
1490 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED);
1492 if (db->db_state == DB_CACHED &&
1493 refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) {
1494 mutex_exit(&db->db_mtx);
1495 (void) dbuf_dirty(db, tx);
1496 bcopy(buf->b_data, db->db.db_data, db->db.db_size);
1497 VERIFY(arc_buf_remove_ref(buf, db));
1498 xuio_stat_wbuf_copied();
1502 xuio_stat_wbuf_nocopy();
1503 if (db->db_state == DB_CACHED) {
1504 dbuf_dirty_record_t *dr = db->db_last_dirty;
1506 ASSERT(db->db_buf != NULL);
1507 if (dr != NULL && dr->dr_txg == tx->tx_txg) {
1508 ASSERT(dr->dt.dl.dr_data == db->db_buf);
1509 if (!arc_released(db->db_buf)) {
1510 ASSERT(dr->dt.dl.dr_override_state ==
1512 arc_release(db->db_buf, db);
1514 dr->dt.dl.dr_data = buf;
1515 VERIFY(arc_buf_remove_ref(db->db_buf, db));
1516 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) {
1517 arc_release(db->db_buf, db);
1518 VERIFY(arc_buf_remove_ref(db->db_buf, db));
1522 ASSERT(db->db_buf == NULL);
1523 dbuf_set_data(db, buf);
1524 db->db_state = DB_FILL;
1525 mutex_exit(&db->db_mtx);
1526 (void) dbuf_dirty(db, tx);
1527 dbuf_fill_done(db, tx);
1531 * "Clear" the contents of this dbuf. This will mark the dbuf
1532 * EVICTING and clear *most* of its references. Unfortunetely,
1533 * when we are not holding the dn_dbufs_mtx, we can't clear the
1534 * entry in the dn_dbufs list. We have to wait until dbuf_destroy()
1535 * in this case. For callers from the DMU we will usually see:
1536 * dbuf_clear()->arc_buf_evict()->dbuf_do_evict()->dbuf_destroy()
1537 * For the arc callback, we will usually see:
1538 * dbuf_do_evict()->dbuf_clear();dbuf_destroy()
1539 * Sometimes, though, we will get a mix of these two:
1540 * DMU: dbuf_clear()->arc_buf_evict()
1541 * ARC: dbuf_do_evict()->dbuf_destroy()
1544 dbuf_clear(dmu_buf_impl_t *db)
1547 dmu_buf_impl_t *parent = db->db_parent;
1548 dmu_buf_impl_t *dndb;
1549 int dbuf_gone = FALSE;
1551 ASSERT(MUTEX_HELD(&db->db_mtx));
1552 ASSERT(refcount_is_zero(&db->db_holds));
1554 dbuf_evict_user(db);
1556 if (db->db_state == DB_CACHED) {
1557 ASSERT(db->db.db_data != NULL);
1558 if (db->db_blkid == DMU_BONUS_BLKID) {
1559 zio_buf_free(db->db.db_data, DN_MAX_BONUSLEN);
1560 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
1562 db->db.db_data = NULL;
1563 db->db_state = DB_UNCACHED;
1566 ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL);
1567 ASSERT(db->db_data_pending == NULL);
1569 db->db_state = DB_EVICTING;
1570 db->db_blkptr = NULL;
1575 if (db->db_blkid != DMU_BONUS_BLKID && MUTEX_HELD(&dn->dn_dbufs_mtx)) {
1576 list_remove(&dn->dn_dbufs, db);
1577 (void) atomic_dec_32_nv(&dn->dn_dbufs_count);
1581 * Decrementing the dbuf count means that the hold corresponding
1582 * to the removed dbuf is no longer discounted in dnode_move(),
1583 * so the dnode cannot be moved until after we release the hold.
1584 * The membar_producer() ensures visibility of the decremented
1585 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
1589 db->db_dnode_handle = NULL;
1595 dbuf_gone = arc_buf_evict(db->db_buf);
1598 mutex_exit(&db->db_mtx);
1601 * If this dbuf is referenced from an indirect dbuf,
1602 * decrement the ref count on the indirect dbuf.
1604 if (parent && parent != dndb)
1605 dbuf_rele(parent, db);
1608 __attribute__((always_inline))
1610 dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse,
1611 dmu_buf_impl_t **parentp, blkptr_t **bpp, struct dbuf_hold_impl_data *dh)
1618 ASSERT(blkid != DMU_BONUS_BLKID);
1620 if (blkid == DMU_SPILL_BLKID) {
1621 mutex_enter(&dn->dn_mtx);
1622 if (dn->dn_have_spill &&
1623 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
1624 *bpp = &dn->dn_phys->dn_spill;
1627 dbuf_add_ref(dn->dn_dbuf, NULL);
1628 *parentp = dn->dn_dbuf;
1629 mutex_exit(&dn->dn_mtx);
1633 if (dn->dn_phys->dn_nlevels == 0)
1636 nlevels = dn->dn_phys->dn_nlevels;
1638 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1640 ASSERT3U(level * epbs, <, 64);
1641 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1642 if (level >= nlevels ||
1643 (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) {
1644 /* the buffer has no parent yet */
1646 } else if (level < nlevels-1) {
1647 /* this block is referenced from an indirect block */
1650 err = dbuf_hold_impl(dn, level+1, blkid >> epbs,
1651 fail_sparse, NULL, parentp);
1654 __dbuf_hold_impl_init(dh + 1, dn, dh->dh_level + 1,
1655 blkid >> epbs, fail_sparse, NULL,
1656 parentp, dh->dh_depth + 1);
1657 err = __dbuf_hold_impl(dh + 1);
1661 err = dbuf_read(*parentp, NULL,
1662 (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL));
1664 dbuf_rele(*parentp, NULL);
1668 *bpp = ((blkptr_t *)(*parentp)->db.db_data) +
1669 (blkid & ((1ULL << epbs) - 1));
1672 /* the block is referenced from the dnode */
1673 ASSERT3U(level, ==, nlevels-1);
1674 ASSERT(dn->dn_phys->dn_nblkptr == 0 ||
1675 blkid < dn->dn_phys->dn_nblkptr);
1677 dbuf_add_ref(dn->dn_dbuf, NULL);
1678 *parentp = dn->dn_dbuf;
1680 *bpp = &dn->dn_phys->dn_blkptr[blkid];
1685 static dmu_buf_impl_t *
1686 dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid,
1687 dmu_buf_impl_t *parent, blkptr_t *blkptr)
1689 objset_t *os = dn->dn_objset;
1690 dmu_buf_impl_t *db, *odb;
1692 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1693 ASSERT(dn->dn_type != DMU_OT_NONE);
1695 db = kmem_cache_alloc(dbuf_cache, KM_PUSHPAGE);
1698 db->db.db_object = dn->dn_object;
1699 db->db_level = level;
1700 db->db_blkid = blkid;
1701 db->db_last_dirty = NULL;
1702 db->db_dirtycnt = 0;
1703 db->db_dnode_handle = dn->dn_handle;
1704 db->db_parent = parent;
1705 db->db_blkptr = blkptr;
1707 db->db_user_ptr = NULL;
1708 db->db_user_data_ptr_ptr = NULL;
1709 db->db_evict_func = NULL;
1710 db->db_immediate_evict = 0;
1711 db->db_freed_in_flight = 0;
1713 if (blkid == DMU_BONUS_BLKID) {
1714 ASSERT3P(parent, ==, dn->dn_dbuf);
1715 db->db.db_size = DN_MAX_BONUSLEN -
1716 (dn->dn_nblkptr-1) * sizeof (blkptr_t);
1717 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
1718 db->db.db_offset = DMU_BONUS_BLKID;
1719 db->db_state = DB_UNCACHED;
1720 /* the bonus dbuf is not placed in the hash table */
1721 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1723 } else if (blkid == DMU_SPILL_BLKID) {
1724 db->db.db_size = (blkptr != NULL) ?
1725 BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE;
1726 db->db.db_offset = 0;
1729 db->db_level ? 1<<dn->dn_indblkshift : dn->dn_datablksz;
1730 db->db.db_size = blocksize;
1731 db->db.db_offset = db->db_blkid * blocksize;
1735 * Hold the dn_dbufs_mtx while we get the new dbuf
1736 * in the hash table *and* added to the dbufs list.
1737 * This prevents a possible deadlock with someone
1738 * trying to look up this dbuf before its added to the
1741 mutex_enter(&dn->dn_dbufs_mtx);
1742 db->db_state = DB_EVICTING;
1743 if ((odb = dbuf_hash_insert(db)) != NULL) {
1744 /* someone else inserted it first */
1745 kmem_cache_free(dbuf_cache, db);
1746 mutex_exit(&dn->dn_dbufs_mtx);
1749 list_insert_head(&dn->dn_dbufs, db);
1750 db->db_state = DB_UNCACHED;
1751 mutex_exit(&dn->dn_dbufs_mtx);
1752 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1754 if (parent && parent != dn->dn_dbuf)
1755 dbuf_add_ref(parent, db);
1757 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1758 refcount_count(&dn->dn_holds) > 0);
1759 (void) refcount_add(&dn->dn_holds, db);
1760 (void) atomic_inc_32_nv(&dn->dn_dbufs_count);
1762 dprintf_dbuf(db, "db=%p\n", db);
1768 dbuf_do_evict(void *private)
1770 arc_buf_t *buf = private;
1771 dmu_buf_impl_t *db = buf->b_private;
1773 if (!MUTEX_HELD(&db->db_mtx))
1774 mutex_enter(&db->db_mtx);
1776 ASSERT(refcount_is_zero(&db->db_holds));
1778 if (db->db_state != DB_EVICTING) {
1779 ASSERT(db->db_state == DB_CACHED);
1784 mutex_exit(&db->db_mtx);
1791 dbuf_destroy(dmu_buf_impl_t *db)
1793 ASSERT(refcount_is_zero(&db->db_holds));
1795 if (db->db_blkid != DMU_BONUS_BLKID) {
1797 * If this dbuf is still on the dn_dbufs list,
1798 * remove it from that list.
1800 if (db->db_dnode_handle != NULL) {
1805 mutex_enter(&dn->dn_dbufs_mtx);
1806 list_remove(&dn->dn_dbufs, db);
1807 (void) atomic_dec_32_nv(&dn->dn_dbufs_count);
1808 mutex_exit(&dn->dn_dbufs_mtx);
1811 * Decrementing the dbuf count means that the hold
1812 * corresponding to the removed dbuf is no longer
1813 * discounted in dnode_move(), so the dnode cannot be
1814 * moved until after we release the hold.
1817 db->db_dnode_handle = NULL;
1819 dbuf_hash_remove(db);
1821 db->db_parent = NULL;
1824 ASSERT(!list_link_active(&db->db_link));
1825 ASSERT(db->db.db_data == NULL);
1826 ASSERT(db->db_hash_next == NULL);
1827 ASSERT(db->db_blkptr == NULL);
1828 ASSERT(db->db_data_pending == NULL);
1830 kmem_cache_free(dbuf_cache, db);
1831 arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1835 dbuf_prefetch(dnode_t *dn, uint64_t blkid)
1837 dmu_buf_impl_t *db = NULL;
1838 blkptr_t *bp = NULL;
1840 ASSERT(blkid != DMU_BONUS_BLKID);
1841 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1843 if (dnode_block_freed(dn, blkid))
1846 /* dbuf_find() returns with db_mtx held */
1847 if ((db = dbuf_find(dn, 0, blkid))) {
1849 * This dbuf is already in the cache. We assume that
1850 * it is already CACHED, or else about to be either
1853 mutex_exit(&db->db_mtx);
1857 if (dbuf_findbp(dn, 0, blkid, TRUE, &db, &bp, NULL) == 0) {
1858 if (bp && !BP_IS_HOLE(bp)) {
1859 int priority = dn->dn_type == DMU_OT_DDT_ZAP ?
1860 ZIO_PRIORITY_DDT_PREFETCH : ZIO_PRIORITY_ASYNC_READ;
1861 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
1862 uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
1865 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
1866 dn->dn_object, 0, blkid);
1868 (void) arc_read(NULL, dn->dn_objset->os_spa,
1869 bp, NULL, NULL, priority,
1870 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
1874 dbuf_rele(db, NULL);
1878 #define DBUF_HOLD_IMPL_MAX_DEPTH 20
1881 * Returns with db_holds incremented, and db_mtx not held.
1882 * Note: dn_struct_rwlock must be held.
1885 __dbuf_hold_impl(struct dbuf_hold_impl_data *dh)
1887 ASSERT3S(dh->dh_depth, <, DBUF_HOLD_IMPL_MAX_DEPTH);
1888 dh->dh_parent = NULL;
1890 ASSERT(dh->dh_blkid != DMU_BONUS_BLKID);
1891 ASSERT(RW_LOCK_HELD(&dh->dh_dn->dn_struct_rwlock));
1892 ASSERT3U(dh->dh_dn->dn_nlevels, >, dh->dh_level);
1894 *(dh->dh_dbp) = NULL;
1896 /* dbuf_find() returns with db_mtx held */
1897 dh->dh_db = dbuf_find(dh->dh_dn, dh->dh_level, dh->dh_blkid);
1899 if (dh->dh_db == NULL) {
1902 ASSERT3P(dh->dh_parent, ==, NULL);
1903 dh->dh_err = dbuf_findbp(dh->dh_dn, dh->dh_level, dh->dh_blkid,
1904 dh->dh_fail_sparse, &dh->dh_parent,
1906 if (dh->dh_fail_sparse) {
1907 if (dh->dh_err == 0 && dh->dh_bp && BP_IS_HOLE(dh->dh_bp))
1908 dh->dh_err = ENOENT;
1911 dbuf_rele(dh->dh_parent, NULL);
1912 return (dh->dh_err);
1915 if (dh->dh_err && dh->dh_err != ENOENT)
1916 return (dh->dh_err);
1917 dh->dh_db = dbuf_create(dh->dh_dn, dh->dh_level, dh->dh_blkid,
1918 dh->dh_parent, dh->dh_bp);
1921 if (dh->dh_db->db_buf && refcount_is_zero(&dh->dh_db->db_holds)) {
1922 arc_buf_add_ref(dh->dh_db->db_buf, dh->dh_db);
1923 if (dh->dh_db->db_buf->b_data == NULL) {
1924 dbuf_clear(dh->dh_db);
1925 if (dh->dh_parent) {
1926 dbuf_rele(dh->dh_parent, NULL);
1927 dh->dh_parent = NULL;
1931 ASSERT3P(dh->dh_db->db.db_data, ==, dh->dh_db->db_buf->b_data);
1934 ASSERT(dh->dh_db->db_buf == NULL || arc_referenced(dh->dh_db->db_buf));
1937 * If this buffer is currently syncing out, and we are are
1938 * still referencing it from db_data, we need to make a copy
1939 * of it in case we decide we want to dirty it again in this txg.
1941 if (dh->dh_db->db_level == 0 &&
1942 dh->dh_db->db_blkid != DMU_BONUS_BLKID &&
1943 dh->dh_dn->dn_object != DMU_META_DNODE_OBJECT &&
1944 dh->dh_db->db_state == DB_CACHED && dh->dh_db->db_data_pending) {
1945 dh->dh_dr = dh->dh_db->db_data_pending;
1947 if (dh->dh_dr->dt.dl.dr_data == dh->dh_db->db_buf) {
1948 dh->dh_type = DBUF_GET_BUFC_TYPE(dh->dh_db);
1950 dbuf_set_data(dh->dh_db,
1951 arc_buf_alloc(dh->dh_dn->dn_objset->os_spa,
1952 dh->dh_db->db.db_size, dh->dh_db, dh->dh_type));
1953 bcopy(dh->dh_dr->dt.dl.dr_data->b_data,
1954 dh->dh_db->db.db_data, dh->dh_db->db.db_size);
1958 (void) refcount_add(&dh->dh_db->db_holds, dh->dh_tag);
1959 dbuf_update_data(dh->dh_db);
1960 DBUF_VERIFY(dh->dh_db);
1961 mutex_exit(&dh->dh_db->db_mtx);
1963 /* NOTE: we can't rele the parent until after we drop the db_mtx */
1965 dbuf_rele(dh->dh_parent, NULL);
1967 ASSERT3P(DB_DNODE(dh->dh_db), ==, dh->dh_dn);
1968 ASSERT3U(dh->dh_db->db_blkid, ==, dh->dh_blkid);
1969 ASSERT3U(dh->dh_db->db_level, ==, dh->dh_level);
1970 *(dh->dh_dbp) = dh->dh_db;
1976 * The following code preserves the recursive function dbuf_hold_impl()
1977 * but moves the local variables AND function arguments to the heap to
1978 * minimize the stack frame size. Enough space is initially allocated
1979 * on the stack for 20 levels of recursion.
1982 dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid, int fail_sparse,
1983 void *tag, dmu_buf_impl_t **dbp)
1985 struct dbuf_hold_impl_data *dh;
1988 dh = kmem_zalloc(sizeof(struct dbuf_hold_impl_data) *
1989 DBUF_HOLD_IMPL_MAX_DEPTH, KM_PUSHPAGE);
1990 __dbuf_hold_impl_init(dh, dn, level, blkid, fail_sparse, tag, dbp, 0);
1992 error = __dbuf_hold_impl(dh);
1994 kmem_free(dh, sizeof(struct dbuf_hold_impl_data) *
1995 DBUF_HOLD_IMPL_MAX_DEPTH);
2001 __dbuf_hold_impl_init(struct dbuf_hold_impl_data *dh,
2002 dnode_t *dn, uint8_t level, uint64_t blkid, int fail_sparse,
2003 void *tag, dmu_buf_impl_t **dbp, int depth)
2006 dh->dh_level = level;
2007 dh->dh_blkid = blkid;
2008 dh->dh_fail_sparse = fail_sparse;
2011 dh->dh_depth = depth;
2015 dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag)
2018 int err = dbuf_hold_impl(dn, 0, blkid, FALSE, tag, &db);
2019 return (err ? NULL : db);
2023 dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag)
2026 int err = dbuf_hold_impl(dn, level, blkid, FALSE, tag, &db);
2027 return (err ? NULL : db);
2031 dbuf_create_bonus(dnode_t *dn)
2033 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
2035 ASSERT(dn->dn_bonus == NULL);
2036 dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL);
2040 dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx)
2042 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2045 if (db->db_blkid != DMU_SPILL_BLKID)
2048 blksz = SPA_MINBLOCKSIZE;
2049 if (blksz > SPA_MAXBLOCKSIZE)
2050 blksz = SPA_MAXBLOCKSIZE;
2052 blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE);
2056 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2057 dbuf_new_size(db, blksz, tx);
2058 rw_exit(&dn->dn_struct_rwlock);
2065 dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx)
2067 dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx);
2070 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2072 dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
2074 VERIFY(refcount_add(&db->db_holds, tag) > 1);
2078 * If you call dbuf_rele() you had better not be referencing the dnode handle
2079 * unless you have some other direct or indirect hold on the dnode. (An indirect
2080 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2081 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2082 * dnode's parent dbuf evicting its dnode handles.
2084 #pragma weak dmu_buf_rele = dbuf_rele
2086 dbuf_rele(dmu_buf_impl_t *db, void *tag)
2088 mutex_enter(&db->db_mtx);
2089 dbuf_rele_and_unlock(db, tag);
2093 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2094 * db_dirtycnt and db_holds to be updated atomically.
2097 dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag)
2101 ASSERT(MUTEX_HELD(&db->db_mtx));
2105 * Remove the reference to the dbuf before removing its hold on the
2106 * dnode so we can guarantee in dnode_move() that a referenced bonus
2107 * buffer has a corresponding dnode hold.
2109 holds = refcount_remove(&db->db_holds, tag);
2113 * We can't freeze indirects if there is a possibility that they
2114 * may be modified in the current syncing context.
2116 if (db->db_buf && holds == (db->db_level == 0 ? db->db_dirtycnt : 0))
2117 arc_buf_freeze(db->db_buf);
2119 if (holds == db->db_dirtycnt &&
2120 db->db_level == 0 && db->db_immediate_evict)
2121 dbuf_evict_user(db);
2124 if (db->db_blkid == DMU_BONUS_BLKID) {
2125 mutex_exit(&db->db_mtx);
2128 * If the dnode moves here, we cannot cross this barrier
2129 * until the move completes.
2132 (void) atomic_dec_32_nv(&DB_DNODE(db)->dn_dbufs_count);
2135 * The bonus buffer's dnode hold is no longer discounted
2136 * in dnode_move(). The dnode cannot move until after
2139 dnode_rele(DB_DNODE(db), db);
2140 } else if (db->db_buf == NULL) {
2142 * This is a special case: we never associated this
2143 * dbuf with any data allocated from the ARC.
2145 ASSERT(db->db_state == DB_UNCACHED ||
2146 db->db_state == DB_NOFILL);
2148 } else if (arc_released(db->db_buf)) {
2149 arc_buf_t *buf = db->db_buf;
2151 * This dbuf has anonymous data associated with it.
2153 dbuf_set_data(db, NULL);
2154 VERIFY(arc_buf_remove_ref(buf, db));
2157 VERIFY(!arc_buf_remove_ref(db->db_buf, db));
2160 * A dbuf will be eligible for eviction if either the
2161 * 'primarycache' property is set or a duplicate
2162 * copy of this buffer is already cached in the arc.
2164 * In the case of the 'primarycache' a buffer
2165 * is considered for eviction if it matches the
2166 * criteria set in the property.
2168 * To decide if our buffer is considered a
2169 * duplicate, we must call into the arc to determine
2170 * if multiple buffers are referencing the same
2171 * block on-disk. If so, then we simply evict
2174 if (!DBUF_IS_CACHEABLE(db) ||
2175 arc_buf_eviction_needed(db->db_buf))
2178 mutex_exit(&db->db_mtx);
2181 mutex_exit(&db->db_mtx);
2185 #pragma weak dmu_buf_refcount = dbuf_refcount
2187 dbuf_refcount(dmu_buf_impl_t *db)
2189 return (refcount_count(&db->db_holds));
2193 dmu_buf_set_user(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr,
2194 dmu_buf_evict_func_t *evict_func)
2196 return (dmu_buf_update_user(db_fake, NULL, user_ptr,
2197 user_data_ptr_ptr, evict_func));
2201 dmu_buf_set_user_ie(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr,
2202 dmu_buf_evict_func_t *evict_func)
2204 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2206 db->db_immediate_evict = TRUE;
2207 return (dmu_buf_update_user(db_fake, NULL, user_ptr,
2208 user_data_ptr_ptr, evict_func));
2212 dmu_buf_update_user(dmu_buf_t *db_fake, void *old_user_ptr, void *user_ptr,
2213 void *user_data_ptr_ptr, dmu_buf_evict_func_t *evict_func)
2215 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2216 ASSERT(db->db_level == 0);
2218 ASSERT((user_ptr == NULL) == (evict_func == NULL));
2220 mutex_enter(&db->db_mtx);
2222 if (db->db_user_ptr == old_user_ptr) {
2223 db->db_user_ptr = user_ptr;
2224 db->db_user_data_ptr_ptr = user_data_ptr_ptr;
2225 db->db_evict_func = evict_func;
2227 dbuf_update_data(db);
2229 old_user_ptr = db->db_user_ptr;
2232 mutex_exit(&db->db_mtx);
2233 return (old_user_ptr);
2237 dmu_buf_get_user(dmu_buf_t *db_fake)
2239 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2240 ASSERT(!refcount_is_zero(&db->db_holds));
2242 return (db->db_user_ptr);
2246 dmu_buf_freeable(dmu_buf_t *dbuf)
2248 boolean_t res = B_FALSE;
2249 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
2252 res = dsl_dataset_block_freeable(db->db_objset->os_dsl_dataset,
2253 db->db_blkptr, db->db_blkptr->blk_birth);
2259 dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db)
2261 /* ASSERT(dmu_tx_is_syncing(tx) */
2262 ASSERT(MUTEX_HELD(&db->db_mtx));
2264 if (db->db_blkptr != NULL)
2267 if (db->db_blkid == DMU_SPILL_BLKID) {
2268 db->db_blkptr = &dn->dn_phys->dn_spill;
2269 BP_ZERO(db->db_blkptr);
2272 if (db->db_level == dn->dn_phys->dn_nlevels-1) {
2274 * This buffer was allocated at a time when there was
2275 * no available blkptrs from the dnode, or it was
2276 * inappropriate to hook it in (i.e., nlevels mis-match).
2278 ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr);
2279 ASSERT(db->db_parent == NULL);
2280 db->db_parent = dn->dn_dbuf;
2281 db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
2284 dmu_buf_impl_t *parent = db->db_parent;
2285 int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2287 ASSERT(dn->dn_phys->dn_nlevels > 1);
2288 if (parent == NULL) {
2289 mutex_exit(&db->db_mtx);
2290 rw_enter(&dn->dn_struct_rwlock, RW_READER);
2291 (void) dbuf_hold_impl(dn, db->db_level+1,
2292 db->db_blkid >> epbs, FALSE, db, &parent);
2293 rw_exit(&dn->dn_struct_rwlock);
2294 mutex_enter(&db->db_mtx);
2295 db->db_parent = parent;
2297 db->db_blkptr = (blkptr_t *)parent->db.db_data +
2298 (db->db_blkid & ((1ULL << epbs) - 1));
2303 /* dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
2304 * is critical the we not allow the compiler to inline this function in to
2305 * dbuf_sync_list() thereby drastically bloating the stack usage.
2307 noinline static void
2308 dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2310 dmu_buf_impl_t *db = dr->dr_dbuf;
2314 ASSERT(dmu_tx_is_syncing(tx));
2316 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2318 mutex_enter(&db->db_mtx);
2320 ASSERT(db->db_level > 0);
2323 if (db->db_buf == NULL) {
2324 mutex_exit(&db->db_mtx);
2325 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
2326 mutex_enter(&db->db_mtx);
2328 ASSERT3U(db->db_state, ==, DB_CACHED);
2329 ASSERT(db->db_buf != NULL);
2333 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2334 dbuf_check_blkptr(dn, db);
2337 db->db_data_pending = dr;
2339 mutex_exit(&db->db_mtx);
2340 dbuf_write(dr, db->db_buf, tx);
2343 mutex_enter(&dr->dt.di.dr_mtx);
2344 dbuf_sync_list(&dr->dt.di.dr_children, tx);
2345 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
2346 mutex_exit(&dr->dt.di.dr_mtx);
2350 /* dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
2351 * critical the we not allow the compiler to inline this function in to
2352 * dbuf_sync_list() thereby drastically bloating the stack usage.
2354 noinline static void
2355 dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2357 arc_buf_t **datap = &dr->dt.dl.dr_data;
2358 dmu_buf_impl_t *db = dr->dr_dbuf;
2361 uint64_t txg = tx->tx_txg;
2363 ASSERT(dmu_tx_is_syncing(tx));
2365 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2367 mutex_enter(&db->db_mtx);
2369 * To be synced, we must be dirtied. But we
2370 * might have been freed after the dirty.
2372 if (db->db_state == DB_UNCACHED) {
2373 /* This buffer has been freed since it was dirtied */
2374 ASSERT(db->db.db_data == NULL);
2375 } else if (db->db_state == DB_FILL) {
2376 /* This buffer was freed and is now being re-filled */
2377 ASSERT(db->db.db_data != dr->dt.dl.dr_data);
2379 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL);
2386 if (db->db_blkid == DMU_SPILL_BLKID) {
2387 mutex_enter(&dn->dn_mtx);
2388 dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR;
2389 mutex_exit(&dn->dn_mtx);
2393 * If this is a bonus buffer, simply copy the bonus data into the
2394 * dnode. It will be written out when the dnode is synced (and it
2395 * will be synced, since it must have been dirty for dbuf_sync to
2398 if (db->db_blkid == DMU_BONUS_BLKID) {
2399 dbuf_dirty_record_t **drp;
2401 ASSERT(*datap != NULL);
2402 ASSERT0(db->db_level);
2403 ASSERT3U(dn->dn_phys->dn_bonuslen, <=, DN_MAX_BONUSLEN);
2404 bcopy(*datap, DN_BONUS(dn->dn_phys), dn->dn_phys->dn_bonuslen);
2407 if (*datap != db->db.db_data) {
2408 zio_buf_free(*datap, DN_MAX_BONUSLEN);
2409 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
2411 db->db_data_pending = NULL;
2412 drp = &db->db_last_dirty;
2414 drp = &(*drp)->dr_next;
2415 ASSERT(dr->dr_next == NULL);
2416 ASSERT(dr->dr_dbuf == db);
2418 if (dr->dr_dbuf->db_level != 0) {
2419 mutex_destroy(&dr->dt.di.dr_mtx);
2420 list_destroy(&dr->dt.di.dr_children);
2422 kmem_free(dr, sizeof (dbuf_dirty_record_t));
2423 ASSERT(db->db_dirtycnt > 0);
2424 db->db_dirtycnt -= 1;
2425 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
2432 * This function may have dropped the db_mtx lock allowing a dmu_sync
2433 * operation to sneak in. As a result, we need to ensure that we
2434 * don't check the dr_override_state until we have returned from
2435 * dbuf_check_blkptr.
2437 dbuf_check_blkptr(dn, db);
2440 * If this buffer is in the middle of an immediate write,
2441 * wait for the synchronous IO to complete.
2443 while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
2444 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
2445 cv_wait(&db->db_changed, &db->db_mtx);
2446 ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN);
2449 if (db->db_state != DB_NOFILL &&
2450 dn->dn_object != DMU_META_DNODE_OBJECT &&
2451 refcount_count(&db->db_holds) > 1 &&
2452 dr->dt.dl.dr_override_state != DR_OVERRIDDEN &&
2453 *datap == db->db_buf) {
2455 * If this buffer is currently "in use" (i.e., there
2456 * are active holds and db_data still references it),
2457 * then make a copy before we start the write so that
2458 * any modifications from the open txg will not leak
2461 * NOTE: this copy does not need to be made for
2462 * objects only modified in the syncing context (e.g.
2463 * DNONE_DNODE blocks).
2465 int blksz = arc_buf_size(*datap);
2466 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
2467 *datap = arc_buf_alloc(os->os_spa, blksz, db, type);
2468 bcopy(db->db.db_data, (*datap)->b_data, blksz);
2470 db->db_data_pending = dr;
2472 mutex_exit(&db->db_mtx);
2474 dbuf_write(dr, *datap, tx);
2476 ASSERT(!list_link_active(&dr->dr_dirty_node));
2477 if (dn->dn_object == DMU_META_DNODE_OBJECT) {
2478 list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr);
2482 * Although zio_nowait() does not "wait for an IO", it does
2483 * initiate the IO. If this is an empty write it seems plausible
2484 * that the IO could actually be completed before the nowait
2485 * returns. We need to DB_DNODE_EXIT() first in case
2486 * zio_nowait() invalidates the dbuf.
2489 zio_nowait(dr->dr_zio);
2494 dbuf_sync_list(list_t *list, dmu_tx_t *tx)
2496 dbuf_dirty_record_t *dr;
2498 while ((dr = list_head(list))) {
2499 if (dr->dr_zio != NULL) {
2501 * If we find an already initialized zio then we
2502 * are processing the meta-dnode, and we have finished.
2503 * The dbufs for all dnodes are put back on the list
2504 * during processing, so that we can zio_wait()
2505 * these IOs after initiating all child IOs.
2507 ASSERT3U(dr->dr_dbuf->db.db_object, ==,
2508 DMU_META_DNODE_OBJECT);
2511 list_remove(list, dr);
2512 if (dr->dr_dbuf->db_level > 0)
2513 dbuf_sync_indirect(dr, tx);
2515 dbuf_sync_leaf(dr, tx);
2521 dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
2523 dmu_buf_impl_t *db = vdb;
2525 blkptr_t *bp = zio->io_bp;
2526 blkptr_t *bp_orig = &zio->io_bp_orig;
2527 spa_t *spa = zio->io_spa;
2532 ASSERT(db->db_blkptr == bp);
2536 delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig);
2537 dnode_diduse_space(dn, delta - zio->io_prev_space_delta);
2538 zio->io_prev_space_delta = delta;
2540 if (BP_IS_HOLE(bp)) {
2541 ASSERT(bp->blk_fill == 0);
2546 ASSERT((db->db_blkid != DMU_SPILL_BLKID &&
2547 BP_GET_TYPE(bp) == dn->dn_type) ||
2548 (db->db_blkid == DMU_SPILL_BLKID &&
2549 BP_GET_TYPE(bp) == dn->dn_bonustype));
2550 ASSERT(BP_GET_LEVEL(bp) == db->db_level);
2552 mutex_enter(&db->db_mtx);
2555 if (db->db_blkid == DMU_SPILL_BLKID) {
2556 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
2557 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
2558 db->db_blkptr == &dn->dn_phys->dn_spill);
2562 if (db->db_level == 0) {
2563 mutex_enter(&dn->dn_mtx);
2564 if (db->db_blkid > dn->dn_phys->dn_maxblkid &&
2565 db->db_blkid != DMU_SPILL_BLKID)
2566 dn->dn_phys->dn_maxblkid = db->db_blkid;
2567 mutex_exit(&dn->dn_mtx);
2569 if (dn->dn_type == DMU_OT_DNODE) {
2570 dnode_phys_t *dnp = db->db.db_data;
2571 for (i = db->db.db_size >> DNODE_SHIFT; i > 0;
2573 if (dnp->dn_type != DMU_OT_NONE)
2580 blkptr_t *ibp = db->db.db_data;
2581 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2582 for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) {
2583 if (BP_IS_HOLE(ibp))
2585 fill += ibp->blk_fill;
2590 bp->blk_fill = fill;
2592 mutex_exit(&db->db_mtx);
2597 dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
2599 dmu_buf_impl_t *db = vdb;
2600 blkptr_t *bp = zio->io_bp;
2601 blkptr_t *bp_orig = &zio->io_bp_orig;
2602 uint64_t txg = zio->io_txg;
2603 dbuf_dirty_record_t **drp, *dr;
2605 ASSERT0(zio->io_error);
2606 ASSERT(db->db_blkptr == bp);
2608 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
2609 ASSERT(BP_EQUAL(bp, bp_orig));
2615 DB_GET_OBJSET(&os, db);
2616 ds = os->os_dsl_dataset;
2619 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
2620 dsl_dataset_block_born(ds, bp, tx);
2623 mutex_enter(&db->db_mtx);
2627 drp = &db->db_last_dirty;
2628 while ((dr = *drp) != db->db_data_pending)
2630 ASSERT(!list_link_active(&dr->dr_dirty_node));
2631 ASSERT(dr->dr_txg == txg);
2632 ASSERT(dr->dr_dbuf == db);
2633 ASSERT(dr->dr_next == NULL);
2637 if (db->db_blkid == DMU_SPILL_BLKID) {
2642 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
2643 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
2644 db->db_blkptr == &dn->dn_phys->dn_spill);
2649 if (db->db_level == 0) {
2650 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2651 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
2652 if (db->db_state != DB_NOFILL) {
2653 if (dr->dt.dl.dr_data != db->db_buf)
2654 VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data,
2656 else if (!arc_released(db->db_buf))
2657 arc_set_callback(db->db_buf, dbuf_do_evict, db);
2664 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
2665 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2666 if (!BP_IS_HOLE(db->db_blkptr)) {
2667 ASSERTV(int epbs = dn->dn_phys->dn_indblkshift -
2669 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
2671 ASSERT3U(dn->dn_phys->dn_maxblkid
2672 >> (db->db_level * epbs), >=, db->db_blkid);
2673 arc_set_callback(db->db_buf, dbuf_do_evict, db);
2676 mutex_destroy(&dr->dt.di.dr_mtx);
2677 list_destroy(&dr->dt.di.dr_children);
2679 kmem_free(dr, sizeof (dbuf_dirty_record_t));
2681 cv_broadcast(&db->db_changed);
2682 ASSERT(db->db_dirtycnt > 0);
2683 db->db_dirtycnt -= 1;
2684 db->db_data_pending = NULL;
2685 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
2689 dbuf_write_nofill_ready(zio_t *zio)
2691 dbuf_write_ready(zio, NULL, zio->io_private);
2695 dbuf_write_nofill_done(zio_t *zio)
2697 dbuf_write_done(zio, NULL, zio->io_private);
2701 dbuf_write_override_ready(zio_t *zio)
2703 dbuf_dirty_record_t *dr = zio->io_private;
2704 dmu_buf_impl_t *db = dr->dr_dbuf;
2706 dbuf_write_ready(zio, NULL, db);
2710 dbuf_write_override_done(zio_t *zio)
2712 dbuf_dirty_record_t *dr = zio->io_private;
2713 dmu_buf_impl_t *db = dr->dr_dbuf;
2714 blkptr_t *obp = &dr->dt.dl.dr_overridden_by;
2716 mutex_enter(&db->db_mtx);
2717 if (!BP_EQUAL(zio->io_bp, obp)) {
2718 if (!BP_IS_HOLE(obp))
2719 dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp);
2720 arc_release(dr->dt.dl.dr_data, db);
2722 mutex_exit(&db->db_mtx);
2724 dbuf_write_done(zio, NULL, db);
2728 dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx)
2730 dmu_buf_impl_t *db = dr->dr_dbuf;
2733 dmu_buf_impl_t *parent = db->db_parent;
2734 uint64_t txg = tx->tx_txg;
2744 if (db->db_state != DB_NOFILL) {
2745 if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) {
2747 * Private object buffers are released here rather
2748 * than in dbuf_dirty() since they are only modified
2749 * in the syncing context and we don't want the
2750 * overhead of making multiple copies of the data.
2752 if (BP_IS_HOLE(db->db_blkptr)) {
2755 dbuf_release_bp(db);
2760 if (parent != dn->dn_dbuf) {
2761 ASSERT(parent && parent->db_data_pending);
2762 ASSERT(db->db_level == parent->db_level-1);
2763 ASSERT(arc_released(parent->db_buf));
2764 zio = parent->db_data_pending->dr_zio;
2766 ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 &&
2767 db->db_blkid != DMU_SPILL_BLKID) ||
2768 (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0));
2769 if (db->db_blkid != DMU_SPILL_BLKID)
2770 ASSERT3P(db->db_blkptr, ==,
2771 &dn->dn_phys->dn_blkptr[db->db_blkid]);
2775 ASSERT(db->db_level == 0 || data == db->db_buf);
2776 ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
2779 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
2780 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
2781 db->db.db_object, db->db_level, db->db_blkid);
2783 if (db->db_blkid == DMU_SPILL_BLKID)
2785 wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
2787 dmu_write_policy(os, dn, db->db_level, wp_flag, &zp);
2790 if (db->db_level == 0 && dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
2791 ASSERT(db->db_state != DB_NOFILL);
2792 dr->dr_zio = zio_write(zio, os->os_spa, txg,
2793 db->db_blkptr, data->b_data, arc_buf_size(data), &zp,
2794 dbuf_write_override_ready, dbuf_write_override_done, dr,
2795 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
2796 mutex_enter(&db->db_mtx);
2797 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
2798 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by,
2799 dr->dt.dl.dr_copies);
2800 mutex_exit(&db->db_mtx);
2801 } else if (db->db_state == DB_NOFILL) {
2802 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF);
2803 dr->dr_zio = zio_write(zio, os->os_spa, txg,
2804 db->db_blkptr, NULL, db->db.db_size, &zp,
2805 dbuf_write_nofill_ready, dbuf_write_nofill_done, db,
2806 ZIO_PRIORITY_ASYNC_WRITE,
2807 ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb);
2809 ASSERT(arc_released(data));
2810 dr->dr_zio = arc_write(zio, os->os_spa, txg,
2811 db->db_blkptr, data, DBUF_IS_L2CACHEABLE(db),
2812 DBUF_IS_L2COMPRESSIBLE(db), &zp, dbuf_write_ready,
2813 dbuf_write_done, db, ZIO_PRIORITY_ASYNC_WRITE,
2814 ZIO_FLAG_MUSTSUCCEED, &zb);
2818 #if defined(_KERNEL) && defined(HAVE_SPL)
2819 EXPORT_SYMBOL(dbuf_find);
2820 EXPORT_SYMBOL(dbuf_is_metadata);
2821 EXPORT_SYMBOL(dbuf_evict);
2822 EXPORT_SYMBOL(dbuf_loan_arcbuf);
2823 EXPORT_SYMBOL(dbuf_whichblock);
2824 EXPORT_SYMBOL(dbuf_read);
2825 EXPORT_SYMBOL(dbuf_unoverride);
2826 EXPORT_SYMBOL(dbuf_free_range);
2827 EXPORT_SYMBOL(dbuf_new_size);
2828 EXPORT_SYMBOL(dbuf_release_bp);
2829 EXPORT_SYMBOL(dbuf_dirty);
2830 EXPORT_SYMBOL(dmu_buf_will_dirty);
2831 EXPORT_SYMBOL(dmu_buf_will_not_fill);
2832 EXPORT_SYMBOL(dmu_buf_will_fill);
2833 EXPORT_SYMBOL(dmu_buf_fill_done);
2834 EXPORT_SYMBOL(dmu_buf_rele);
2835 EXPORT_SYMBOL(dbuf_assign_arcbuf);
2836 EXPORT_SYMBOL(dbuf_clear);
2837 EXPORT_SYMBOL(dbuf_prefetch);
2838 EXPORT_SYMBOL(dbuf_hold_impl);
2839 EXPORT_SYMBOL(dbuf_hold);
2840 EXPORT_SYMBOL(dbuf_hold_level);
2841 EXPORT_SYMBOL(dbuf_create_bonus);
2842 EXPORT_SYMBOL(dbuf_spill_set_blksz);
2843 EXPORT_SYMBOL(dbuf_rm_spill);
2844 EXPORT_SYMBOL(dbuf_add_ref);
2845 EXPORT_SYMBOL(dbuf_rele);
2846 EXPORT_SYMBOL(dbuf_rele_and_unlock);
2847 EXPORT_SYMBOL(dbuf_refcount);
2848 EXPORT_SYMBOL(dbuf_sync_list);
2849 EXPORT_SYMBOL(dmu_buf_set_user);
2850 EXPORT_SYMBOL(dmu_buf_set_user_ie);
2851 EXPORT_SYMBOL(dmu_buf_update_user);
2852 EXPORT_SYMBOL(dmu_buf_get_user);
2853 EXPORT_SYMBOL(dmu_buf_freeable);