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.
27 #include <sys/zfs_context.h>
30 #include <sys/dmu_impl.h>
32 #include <sys/dmu_objset.h>
33 #include <sys/dsl_dataset.h>
34 #include <sys/dsl_dir.h>
35 #include <sys/dmu_tx.h>
38 #include <sys/dmu_zfetch.h>
40 #include <sys/sa_impl.h>
42 struct dbuf_hold_impl_data {
43 /* Function arguments */
49 dmu_buf_impl_t **dh_dbp;
51 dmu_buf_impl_t *dh_db;
52 dmu_buf_impl_t *dh_parent;
55 dbuf_dirty_record_t *dh_dr;
56 arc_buf_contents_t dh_type;
60 static void __dbuf_hold_impl_init(struct dbuf_hold_impl_data *dh,
61 dnode_t *dn, uint8_t level, uint64_t blkid, int fail_sparse,
62 void *tag, dmu_buf_impl_t **dbp, int depth);
63 static int __dbuf_hold_impl(struct dbuf_hold_impl_data *dh);
65 static void dbuf_destroy(dmu_buf_impl_t *db);
66 static int dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
67 static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx);
70 * Global data structures and functions for the dbuf cache.
72 static kmem_cache_t *dbuf_cache;
76 dbuf_cons(void *vdb, void *unused, int kmflag)
78 dmu_buf_impl_t *db = vdb;
79 bzero(db, sizeof (dmu_buf_impl_t));
81 mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL);
82 cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL);
83 refcount_create(&db->db_holds);
84 list_link_init(&db->db_link);
90 dbuf_dest(void *vdb, void *unused)
92 dmu_buf_impl_t *db = vdb;
93 mutex_destroy(&db->db_mtx);
94 cv_destroy(&db->db_changed);
95 refcount_destroy(&db->db_holds);
99 * dbuf hash table routines
101 static dbuf_hash_table_t dbuf_hash_table;
103 static uint64_t dbuf_hash_count;
106 dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid)
108 uintptr_t osv = (uintptr_t)os;
109 uint64_t crc = -1ULL;
111 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
112 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (lvl)) & 0xFF];
113 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF];
114 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF];
115 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF];
116 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 0)) & 0xFF];
117 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 8)) & 0xFF];
119 crc ^= (osv>>14) ^ (obj>>16) ^ (blkid>>16);
124 #define DBUF_HASH(os, obj, level, blkid) dbuf_hash(os, obj, level, blkid);
126 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
127 ((dbuf)->db.db_object == (obj) && \
128 (dbuf)->db_objset == (os) && \
129 (dbuf)->db_level == (level) && \
130 (dbuf)->db_blkid == (blkid))
133 dbuf_find(dnode_t *dn, uint8_t level, uint64_t blkid)
135 dbuf_hash_table_t *h = &dbuf_hash_table;
136 objset_t *os = dn->dn_objset;
143 hv = DBUF_HASH(os, obj, level, blkid);
144 idx = hv & h->hash_table_mask;
146 mutex_enter(DBUF_HASH_MUTEX(h, idx));
147 for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) {
148 if (DBUF_EQUAL(db, os, obj, level, blkid)) {
149 mutex_enter(&db->db_mtx);
150 if (db->db_state != DB_EVICTING) {
151 mutex_exit(DBUF_HASH_MUTEX(h, idx));
154 mutex_exit(&db->db_mtx);
157 mutex_exit(DBUF_HASH_MUTEX(h, idx));
162 * Insert an entry into the hash table. If there is already an element
163 * equal to elem in the hash table, then the already existing element
164 * will be returned and the new element will not be inserted.
165 * Otherwise returns NULL.
167 static dmu_buf_impl_t *
168 dbuf_hash_insert(dmu_buf_impl_t *db)
170 dbuf_hash_table_t *h = &dbuf_hash_table;
171 objset_t *os = db->db_objset;
172 uint64_t obj = db->db.db_object;
173 int level = db->db_level;
174 uint64_t blkid, hv, idx;
177 blkid = db->db_blkid;
178 hv = DBUF_HASH(os, obj, level, blkid);
179 idx = hv & h->hash_table_mask;
181 mutex_enter(DBUF_HASH_MUTEX(h, idx));
182 for (dbf = h->hash_table[idx]; dbf != NULL; dbf = dbf->db_hash_next) {
183 if (DBUF_EQUAL(dbf, os, obj, level, blkid)) {
184 mutex_enter(&dbf->db_mtx);
185 if (dbf->db_state != DB_EVICTING) {
186 mutex_exit(DBUF_HASH_MUTEX(h, idx));
189 mutex_exit(&dbf->db_mtx);
193 mutex_enter(&db->db_mtx);
194 db->db_hash_next = h->hash_table[idx];
195 h->hash_table[idx] = db;
196 mutex_exit(DBUF_HASH_MUTEX(h, idx));
197 atomic_add_64(&dbuf_hash_count, 1);
203 * Remove an entry from the hash table. This operation will
204 * fail if there are any existing holds on the db.
207 dbuf_hash_remove(dmu_buf_impl_t *db)
209 dbuf_hash_table_t *h = &dbuf_hash_table;
211 dmu_buf_impl_t *dbf, **dbp;
213 hv = DBUF_HASH(db->db_objset, db->db.db_object,
214 db->db_level, db->db_blkid);
215 idx = hv & h->hash_table_mask;
218 * We musn't hold db_mtx to maintin lock ordering:
219 * DBUF_HASH_MUTEX > db_mtx.
221 ASSERT(refcount_is_zero(&db->db_holds));
222 ASSERT(db->db_state == DB_EVICTING);
223 ASSERT(!MUTEX_HELD(&db->db_mtx));
225 mutex_enter(DBUF_HASH_MUTEX(h, idx));
226 dbp = &h->hash_table[idx];
227 while ((dbf = *dbp) != db) {
228 dbp = &dbf->db_hash_next;
231 *dbp = db->db_hash_next;
232 db->db_hash_next = NULL;
233 mutex_exit(DBUF_HASH_MUTEX(h, idx));
234 atomic_add_64(&dbuf_hash_count, -1);
237 static arc_evict_func_t dbuf_do_evict;
240 dbuf_evict_user(dmu_buf_impl_t *db)
242 ASSERT(MUTEX_HELD(&db->db_mtx));
244 if (db->db_level != 0 || db->db_evict_func == NULL)
247 if (db->db_user_data_ptr_ptr)
248 *db->db_user_data_ptr_ptr = db->db.db_data;
249 db->db_evict_func(&db->db, db->db_user_ptr);
250 db->db_user_ptr = NULL;
251 db->db_user_data_ptr_ptr = NULL;
252 db->db_evict_func = NULL;
256 dbuf_is_metadata(dmu_buf_impl_t *db)
258 if (db->db_level > 0) {
261 boolean_t is_metadata;
264 is_metadata = DMU_OT_IS_METADATA(DB_DNODE(db)->dn_type);
267 return (is_metadata);
272 dbuf_evict(dmu_buf_impl_t *db)
274 ASSERT(MUTEX_HELD(&db->db_mtx));
275 ASSERT(db->db_buf == NULL);
276 ASSERT(db->db_data_pending == NULL);
285 uint64_t hsize = 1ULL << 16;
286 dbuf_hash_table_t *h = &dbuf_hash_table;
290 * The hash table is big enough to fill all of physical memory
291 * with an average 4K block size. The table will take up
292 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
294 while (hsize * 4096 < physmem * PAGESIZE)
298 h->hash_table_mask = hsize - 1;
299 #if defined(_KERNEL) && defined(HAVE_SPL)
300 /* Large allocations which do not require contiguous pages
301 * should be using vmem_alloc() in the linux kernel */
302 h->hash_table = vmem_zalloc(hsize * sizeof (void *), KM_PUSHPAGE);
304 h->hash_table = kmem_zalloc(hsize * sizeof (void *), KM_NOSLEEP);
306 if (h->hash_table == NULL) {
307 /* XXX - we should really return an error instead of assert */
308 ASSERT(hsize > (1ULL << 10));
313 dbuf_cache = kmem_cache_create("dmu_buf_impl_t",
314 sizeof (dmu_buf_impl_t),
315 0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0);
317 for (i = 0; i < DBUF_MUTEXES; i++)
318 mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL);
324 dbuf_hash_table_t *h = &dbuf_hash_table;
327 for (i = 0; i < DBUF_MUTEXES; i++)
328 mutex_destroy(&h->hash_mutexes[i]);
329 #if defined(_KERNEL) && defined(HAVE_SPL)
330 /* Large allocations which do not require contiguous pages
331 * should be using vmem_free() in the linux kernel */
332 vmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
334 kmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
336 kmem_cache_destroy(dbuf_cache);
345 dbuf_verify(dmu_buf_impl_t *db)
348 dbuf_dirty_record_t *dr;
350 ASSERT(MUTEX_HELD(&db->db_mtx));
352 if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY))
355 ASSERT(db->db_objset != NULL);
359 ASSERT(db->db_parent == NULL);
360 ASSERT(db->db_blkptr == NULL);
362 ASSERT3U(db->db.db_object, ==, dn->dn_object);
363 ASSERT3P(db->db_objset, ==, dn->dn_objset);
364 ASSERT3U(db->db_level, <, dn->dn_nlevels);
365 ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
366 db->db_blkid == DMU_SPILL_BLKID ||
367 !list_is_empty(&dn->dn_dbufs));
369 if (db->db_blkid == DMU_BONUS_BLKID) {
371 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
372 ASSERT3U(db->db.db_offset, ==, DMU_BONUS_BLKID);
373 } else if (db->db_blkid == DMU_SPILL_BLKID) {
375 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
376 ASSERT0(db->db.db_offset);
378 ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size);
381 for (dr = db->db_data_pending; dr != NULL; dr = dr->dr_next)
382 ASSERT(dr->dr_dbuf == db);
384 for (dr = db->db_last_dirty; dr != NULL; dr = dr->dr_next)
385 ASSERT(dr->dr_dbuf == db);
388 * We can't assert that db_size matches dn_datablksz because it
389 * can be momentarily different when another thread is doing
392 if (db->db_level == 0 && db->db.db_object == DMU_META_DNODE_OBJECT) {
393 dr = db->db_data_pending;
395 * It should only be modified in syncing context, so
396 * make sure we only have one copy of the data.
398 ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf);
401 /* verify db->db_blkptr */
403 if (db->db_parent == dn->dn_dbuf) {
404 /* db is pointed to by the dnode */
405 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
406 if (DMU_OBJECT_IS_SPECIAL(db->db.db_object))
407 ASSERT(db->db_parent == NULL);
409 ASSERT(db->db_parent != NULL);
410 if (db->db_blkid != DMU_SPILL_BLKID)
411 ASSERT3P(db->db_blkptr, ==,
412 &dn->dn_phys->dn_blkptr[db->db_blkid]);
414 /* db is pointed to by an indirect block */
415 ASSERTV(int epb = db->db_parent->db.db_size >>
417 ASSERT3U(db->db_parent->db_level, ==, db->db_level+1);
418 ASSERT3U(db->db_parent->db.db_object, ==,
421 * dnode_grow_indblksz() can make this fail if we don't
422 * have the struct_rwlock. XXX indblksz no longer
423 * grows. safe to do this now?
425 if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
426 ASSERT3P(db->db_blkptr, ==,
427 ((blkptr_t *)db->db_parent->db.db_data +
428 db->db_blkid % epb));
432 if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) &&
433 (db->db_buf == NULL || db->db_buf->b_data) &&
434 db->db.db_data && db->db_blkid != DMU_BONUS_BLKID &&
435 db->db_state != DB_FILL && !dn->dn_free_txg) {
437 * If the blkptr isn't set but they have nonzero data,
438 * it had better be dirty, otherwise we'll lose that
439 * data when we evict this buffer.
441 if (db->db_dirtycnt == 0) {
442 ASSERTV(uint64_t *buf = db->db.db_data);
445 for (i = 0; i < db->db.db_size >> 3; i++) {
455 dbuf_update_data(dmu_buf_impl_t *db)
457 ASSERT(MUTEX_HELD(&db->db_mtx));
458 if (db->db_level == 0 && db->db_user_data_ptr_ptr) {
459 ASSERT(!refcount_is_zero(&db->db_holds));
460 *db->db_user_data_ptr_ptr = db->db.db_data;
465 dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf)
467 ASSERT(MUTEX_HELD(&db->db_mtx));
468 ASSERT(db->db_buf == NULL || !arc_has_callback(db->db_buf));
471 ASSERT(buf->b_data != NULL);
472 db->db.db_data = buf->b_data;
473 if (!arc_released(buf))
474 arc_set_callback(buf, dbuf_do_evict, db);
475 dbuf_update_data(db);
478 db->db.db_data = NULL;
479 if (db->db_state != DB_NOFILL)
480 db->db_state = DB_UNCACHED;
485 * Loan out an arc_buf for read. Return the loaned arc_buf.
488 dbuf_loan_arcbuf(dmu_buf_impl_t *db)
492 mutex_enter(&db->db_mtx);
493 if (arc_released(db->db_buf) || refcount_count(&db->db_holds) > 1) {
494 int blksz = db->db.db_size;
497 mutex_exit(&db->db_mtx);
498 DB_GET_SPA(&spa, db);
499 abuf = arc_loan_buf(spa, blksz);
500 bcopy(db->db.db_data, abuf->b_data, blksz);
503 arc_loan_inuse_buf(abuf, db);
504 dbuf_set_data(db, NULL);
505 mutex_exit(&db->db_mtx);
511 dbuf_whichblock(dnode_t *dn, uint64_t offset)
513 if (dn->dn_datablkshift) {
514 return (offset >> dn->dn_datablkshift);
516 ASSERT3U(offset, <, dn->dn_datablksz);
522 dbuf_read_done(zio_t *zio, arc_buf_t *buf, void *vdb)
524 dmu_buf_impl_t *db = vdb;
526 mutex_enter(&db->db_mtx);
527 ASSERT3U(db->db_state, ==, DB_READ);
529 * All reads are synchronous, so we must have a hold on the dbuf
531 ASSERT(refcount_count(&db->db_holds) > 0);
532 ASSERT(db->db_buf == NULL);
533 ASSERT(db->db.db_data == NULL);
534 if (db->db_level == 0 && db->db_freed_in_flight) {
535 /* we were freed in flight; disregard any error */
536 arc_release(buf, db);
537 bzero(buf->b_data, db->db.db_size);
539 db->db_freed_in_flight = FALSE;
540 dbuf_set_data(db, buf);
541 db->db_state = DB_CACHED;
542 } else if (zio == NULL || zio->io_error == 0) {
543 dbuf_set_data(db, buf);
544 db->db_state = DB_CACHED;
546 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
547 ASSERT3P(db->db_buf, ==, NULL);
548 VERIFY(arc_buf_remove_ref(buf, db) == 1);
549 db->db_state = DB_UNCACHED;
551 cv_broadcast(&db->db_changed);
552 dbuf_rele_and_unlock(db, NULL);
556 dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t *flags)
561 uint32_t aflags = ARC_NOWAIT;
565 ASSERT(!refcount_is_zero(&db->db_holds));
566 /* We need the struct_rwlock to prevent db_blkptr from changing. */
567 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
568 ASSERT(MUTEX_HELD(&db->db_mtx));
569 ASSERT(db->db_state == DB_UNCACHED);
570 ASSERT(db->db_buf == NULL);
572 if (db->db_blkid == DMU_BONUS_BLKID) {
573 int bonuslen = MIN(dn->dn_bonuslen, dn->dn_phys->dn_bonuslen);
575 ASSERT3U(bonuslen, <=, db->db.db_size);
576 db->db.db_data = zio_buf_alloc(DN_MAX_BONUSLEN);
577 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
578 if (bonuslen < DN_MAX_BONUSLEN)
579 bzero(db->db.db_data, DN_MAX_BONUSLEN);
581 bcopy(DN_BONUS(dn->dn_phys), db->db.db_data, bonuslen);
583 dbuf_update_data(db);
584 db->db_state = DB_CACHED;
585 mutex_exit(&db->db_mtx);
590 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
591 * processes the delete record and clears the bp while we are waiting
592 * for the dn_mtx (resulting in a "no" from block_freed).
594 if (db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr) ||
595 (db->db_level == 0 && (dnode_block_freed(dn, db->db_blkid) ||
596 BP_IS_HOLE(db->db_blkptr)))) {
597 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
599 dbuf_set_data(db, arc_buf_alloc(dn->dn_objset->os_spa,
600 db->db.db_size, db, type));
602 bzero(db->db.db_data, db->db.db_size);
603 db->db_state = DB_CACHED;
604 *flags |= DB_RF_CACHED;
605 mutex_exit(&db->db_mtx);
609 spa = dn->dn_objset->os_spa;
612 db->db_state = DB_READ;
613 mutex_exit(&db->db_mtx);
615 if (DBUF_IS_L2CACHEABLE(db))
616 aflags |= ARC_L2CACHE;
618 SET_BOOKMARK(&zb, db->db_objset->os_dsl_dataset ?
619 db->db_objset->os_dsl_dataset->ds_object : DMU_META_OBJSET,
620 db->db.db_object, db->db_level, db->db_blkid);
622 dbuf_add_ref(db, NULL);
624 (void) arc_read(zio, spa, db->db_blkptr,
625 dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ,
626 (*flags & DB_RF_CANFAIL) ? ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED,
628 if (aflags & ARC_CACHED)
629 *flags |= DB_RF_CACHED;
633 dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
636 int havepzio = (zio != NULL);
641 * We don't have to hold the mutex to check db_state because it
642 * can't be freed while we have a hold on the buffer.
644 ASSERT(!refcount_is_zero(&db->db_holds));
646 if (db->db_state == DB_NOFILL)
651 if ((flags & DB_RF_HAVESTRUCT) == 0)
652 rw_enter(&dn->dn_struct_rwlock, RW_READER);
654 prefetch = db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
655 (flags & DB_RF_NOPREFETCH) == 0 && dn != NULL &&
656 DBUF_IS_CACHEABLE(db);
658 mutex_enter(&db->db_mtx);
659 if (db->db_state == DB_CACHED) {
660 mutex_exit(&db->db_mtx);
662 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
663 db->db.db_size, TRUE);
664 if ((flags & DB_RF_HAVESTRUCT) == 0)
665 rw_exit(&dn->dn_struct_rwlock);
667 } else if (db->db_state == DB_UNCACHED) {
668 spa_t *spa = dn->dn_objset->os_spa;
671 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
672 dbuf_read_impl(db, zio, &flags);
674 /* dbuf_read_impl has dropped db_mtx for us */
677 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
678 db->db.db_size, flags & DB_RF_CACHED);
680 if ((flags & DB_RF_HAVESTRUCT) == 0)
681 rw_exit(&dn->dn_struct_rwlock);
687 mutex_exit(&db->db_mtx);
689 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
690 db->db.db_size, TRUE);
691 if ((flags & DB_RF_HAVESTRUCT) == 0)
692 rw_exit(&dn->dn_struct_rwlock);
695 mutex_enter(&db->db_mtx);
696 if ((flags & DB_RF_NEVERWAIT) == 0) {
697 while (db->db_state == DB_READ ||
698 db->db_state == DB_FILL) {
699 ASSERT(db->db_state == DB_READ ||
700 (flags & DB_RF_HAVESTRUCT) == 0);
701 cv_wait(&db->db_changed, &db->db_mtx);
703 if (db->db_state == DB_UNCACHED)
706 mutex_exit(&db->db_mtx);
709 ASSERT(err || havepzio || db->db_state == DB_CACHED);
714 dbuf_noread(dmu_buf_impl_t *db)
716 ASSERT(!refcount_is_zero(&db->db_holds));
717 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
718 mutex_enter(&db->db_mtx);
719 while (db->db_state == DB_READ || db->db_state == DB_FILL)
720 cv_wait(&db->db_changed, &db->db_mtx);
721 if (db->db_state == DB_UNCACHED) {
722 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
725 ASSERT(db->db_buf == NULL);
726 ASSERT(db->db.db_data == NULL);
727 DB_GET_SPA(&spa, db);
728 dbuf_set_data(db, arc_buf_alloc(spa, db->db.db_size, db, type));
729 db->db_state = DB_FILL;
730 } else if (db->db_state == DB_NOFILL) {
731 dbuf_set_data(db, NULL);
733 ASSERT3U(db->db_state, ==, DB_CACHED);
735 mutex_exit(&db->db_mtx);
739 * This is our just-in-time copy function. It makes a copy of
740 * buffers, that have been modified in a previous transaction
741 * group, before we modify them in the current active group.
743 * This function is used in two places: when we are dirtying a
744 * buffer for the first time in a txg, and when we are freeing
745 * a range in a dnode that includes this buffer.
747 * Note that when we are called from dbuf_free_range() we do
748 * not put a hold on the buffer, we just traverse the active
749 * dbuf list for the dnode.
752 dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg)
754 dbuf_dirty_record_t *dr = db->db_last_dirty;
756 ASSERT(MUTEX_HELD(&db->db_mtx));
757 ASSERT(db->db.db_data != NULL);
758 ASSERT(db->db_level == 0);
759 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT);
762 (dr->dt.dl.dr_data !=
763 ((db->db_blkid == DMU_BONUS_BLKID) ? db->db.db_data : db->db_buf)))
767 * If the last dirty record for this dbuf has not yet synced
768 * and its referencing the dbuf data, either:
769 * reset the reference to point to a new copy,
770 * or (if there a no active holders)
771 * just null out the current db_data pointer.
773 ASSERT(dr->dr_txg >= txg - 2);
774 if (db->db_blkid == DMU_BONUS_BLKID) {
775 /* Note that the data bufs here are zio_bufs */
776 dr->dt.dl.dr_data = zio_buf_alloc(DN_MAX_BONUSLEN);
777 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
778 bcopy(db->db.db_data, dr->dt.dl.dr_data, DN_MAX_BONUSLEN);
779 } else if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
780 int size = db->db.db_size;
781 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
784 DB_GET_SPA(&spa, db);
785 dr->dt.dl.dr_data = arc_buf_alloc(spa, size, db, type);
786 bcopy(db->db.db_data, dr->dt.dl.dr_data->b_data, size);
788 dbuf_set_data(db, NULL);
793 dbuf_unoverride(dbuf_dirty_record_t *dr)
795 dmu_buf_impl_t *db = dr->dr_dbuf;
796 blkptr_t *bp = &dr->dt.dl.dr_overridden_by;
797 uint64_t txg = dr->dr_txg;
799 ASSERT(MUTEX_HELD(&db->db_mtx));
800 ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC);
801 ASSERT(db->db_level == 0);
803 if (db->db_blkid == DMU_BONUS_BLKID ||
804 dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN)
807 ASSERT(db->db_data_pending != dr);
809 /* free this block */
810 if (!BP_IS_HOLE(bp)) {
813 DB_GET_SPA(&spa, db);
814 zio_free(spa, txg, bp);
816 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
818 * Release the already-written buffer, so we leave it in
819 * a consistent dirty state. Note that all callers are
820 * modifying the buffer, so they will immediately do
821 * another (redundant) arc_release(). Therefore, leave
822 * the buf thawed to save the effort of freezing &
823 * immediately re-thawing it.
825 arc_release(dr->dt.dl.dr_data, db);
829 * Evict (if its unreferenced) or clear (if its referenced) any level-0
830 * data blocks in the free range, so that any future readers will find
831 * empty blocks. Also, if we happen accross any level-1 dbufs in the
832 * range that have not already been marked dirty, mark them dirty so
833 * they stay in memory.
836 dbuf_free_range(dnode_t *dn, uint64_t start, uint64_t end, dmu_tx_t *tx)
838 dmu_buf_impl_t *db, *db_next;
839 uint64_t txg = tx->tx_txg;
840 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
841 uint64_t first_l1 = start >> epbs;
842 uint64_t last_l1 = end >> epbs;
844 if (end > dn->dn_maxblkid && (end != DMU_SPILL_BLKID)) {
845 end = dn->dn_maxblkid;
846 last_l1 = end >> epbs;
848 dprintf_dnode(dn, "start=%llu end=%llu\n", start, end);
849 mutex_enter(&dn->dn_dbufs_mtx);
850 for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
851 db_next = list_next(&dn->dn_dbufs, db);
852 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
854 if (db->db_level == 1 &&
855 db->db_blkid >= first_l1 && db->db_blkid <= last_l1) {
856 mutex_enter(&db->db_mtx);
857 if (db->db_last_dirty &&
858 db->db_last_dirty->dr_txg < txg) {
859 dbuf_add_ref(db, FTAG);
860 mutex_exit(&db->db_mtx);
861 dbuf_will_dirty(db, tx);
864 mutex_exit(&db->db_mtx);
868 if (db->db_level != 0)
870 dprintf_dbuf(db, "found buf %s\n", "");
871 if (db->db_blkid < start || db->db_blkid > end)
874 /* found a level 0 buffer in the range */
875 if (dbuf_undirty(db, tx))
878 mutex_enter(&db->db_mtx);
879 if (db->db_state == DB_UNCACHED ||
880 db->db_state == DB_NOFILL ||
881 db->db_state == DB_EVICTING) {
882 ASSERT(db->db.db_data == NULL);
883 mutex_exit(&db->db_mtx);
886 if (db->db_state == DB_READ || db->db_state == DB_FILL) {
887 /* will be handled in dbuf_read_done or dbuf_rele */
888 db->db_freed_in_flight = TRUE;
889 mutex_exit(&db->db_mtx);
892 if (refcount_count(&db->db_holds) == 0) {
897 /* The dbuf is referenced */
899 if (db->db_last_dirty != NULL) {
900 dbuf_dirty_record_t *dr = db->db_last_dirty;
902 if (dr->dr_txg == txg) {
904 * This buffer is "in-use", re-adjust the file
905 * size to reflect that this buffer may
906 * contain new data when we sync.
908 if (db->db_blkid != DMU_SPILL_BLKID &&
909 db->db_blkid > dn->dn_maxblkid)
910 dn->dn_maxblkid = db->db_blkid;
914 * This dbuf is not dirty in the open context.
915 * Either uncache it (if its not referenced in
916 * the open context) or reset its contents to
919 dbuf_fix_old_data(db, txg);
922 /* clear the contents if its cached */
923 if (db->db_state == DB_CACHED) {
924 ASSERT(db->db.db_data != NULL);
925 arc_release(db->db_buf, db);
926 bzero(db->db.db_data, db->db.db_size);
927 arc_buf_freeze(db->db_buf);
930 mutex_exit(&db->db_mtx);
932 mutex_exit(&dn->dn_dbufs_mtx);
936 dbuf_block_freeable(dmu_buf_impl_t *db)
938 dsl_dataset_t *ds = db->db_objset->os_dsl_dataset;
939 uint64_t birth_txg = 0;
942 * We don't need any locking to protect db_blkptr:
943 * If it's syncing, then db_last_dirty will be set
944 * so we'll ignore db_blkptr.
946 ASSERT(MUTEX_HELD(&db->db_mtx));
947 if (db->db_last_dirty)
948 birth_txg = db->db_last_dirty->dr_txg;
949 else if (db->db_blkptr)
950 birth_txg = db->db_blkptr->blk_birth;
953 * If we don't exist or are in a snapshot, we can't be freed.
954 * Don't pass the bp to dsl_dataset_block_freeable() since we
955 * are holding the db_mtx lock and might deadlock if we are
956 * prefetching a dedup-ed block.
959 return (ds == NULL ||
960 dsl_dataset_block_freeable(ds, NULL, birth_txg));
966 dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx)
968 arc_buf_t *buf, *obuf;
969 int osize = db->db.db_size;
970 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
973 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
978 /* XXX does *this* func really need the lock? */
979 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
982 * This call to dbuf_will_dirty() with the dn_struct_rwlock held
983 * is OK, because there can be no other references to the db
984 * when we are changing its size, so no concurrent DB_FILL can
988 * XXX we should be doing a dbuf_read, checking the return
989 * value and returning that up to our callers
991 dbuf_will_dirty(db, tx);
993 /* create the data buffer for the new block */
994 buf = arc_buf_alloc(dn->dn_objset->os_spa, size, db, type);
996 /* copy old block data to the new block */
998 bcopy(obuf->b_data, buf->b_data, MIN(osize, size));
999 /* zero the remainder */
1001 bzero((uint8_t *)buf->b_data + osize, size - osize);
1003 mutex_enter(&db->db_mtx);
1004 dbuf_set_data(db, buf);
1005 VERIFY(arc_buf_remove_ref(obuf, db) == 1);
1006 db->db.db_size = size;
1008 if (db->db_level == 0) {
1009 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
1010 db->db_last_dirty->dt.dl.dr_data = buf;
1012 mutex_exit(&db->db_mtx);
1014 dnode_willuse_space(dn, size-osize, tx);
1019 dbuf_release_bp(dmu_buf_impl_t *db)
1023 DB_GET_OBJSET(&os, db);
1024 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
1025 ASSERT(arc_released(os->os_phys_buf) ||
1026 list_link_active(&os->os_dsl_dataset->ds_synced_link));
1027 ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf));
1029 (void) arc_release(db->db_buf, db);
1032 dbuf_dirty_record_t *
1033 dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1037 dbuf_dirty_record_t **drp, *dr;
1038 int drop_struct_lock = FALSE;
1039 boolean_t do_free_accounting = B_FALSE;
1040 int txgoff = tx->tx_txg & TXG_MASK;
1042 ASSERT(tx->tx_txg != 0);
1043 ASSERT(!refcount_is_zero(&db->db_holds));
1044 DMU_TX_DIRTY_BUF(tx, db);
1049 * Shouldn't dirty a regular buffer in syncing context. Private
1050 * objects may be dirtied in syncing context, but only if they
1051 * were already pre-dirtied in open context.
1053 ASSERT(!dmu_tx_is_syncing(tx) ||
1054 BP_IS_HOLE(dn->dn_objset->os_rootbp) ||
1055 DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1056 dn->dn_objset->os_dsl_dataset == NULL);
1058 * We make this assert for private objects as well, but after we
1059 * check if we're already dirty. They are allowed to re-dirty
1060 * in syncing context.
1062 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1063 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1064 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1066 mutex_enter(&db->db_mtx);
1068 * XXX make this true for indirects too? The problem is that
1069 * transactions created with dmu_tx_create_assigned() from
1070 * syncing context don't bother holding ahead.
1072 ASSERT(db->db_level != 0 ||
1073 db->db_state == DB_CACHED || db->db_state == DB_FILL ||
1074 db->db_state == DB_NOFILL);
1076 mutex_enter(&dn->dn_mtx);
1078 * Don't set dirtyctx to SYNC if we're just modifying this as we
1079 * initialize the objset.
1081 if (dn->dn_dirtyctx == DN_UNDIRTIED &&
1082 !BP_IS_HOLE(dn->dn_objset->os_rootbp)) {
1084 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN);
1085 ASSERT(dn->dn_dirtyctx_firstset == NULL);
1086 dn->dn_dirtyctx_firstset = kmem_alloc(1, KM_PUSHPAGE);
1088 mutex_exit(&dn->dn_mtx);
1090 if (db->db_blkid == DMU_SPILL_BLKID)
1091 dn->dn_have_spill = B_TRUE;
1094 * If this buffer is already dirty, we're done.
1096 drp = &db->db_last_dirty;
1097 ASSERT(*drp == NULL || (*drp)->dr_txg <= tx->tx_txg ||
1098 db->db.db_object == DMU_META_DNODE_OBJECT);
1099 while ((dr = *drp) != NULL && dr->dr_txg > tx->tx_txg)
1101 if (dr && dr->dr_txg == tx->tx_txg) {
1104 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID) {
1106 * If this buffer has already been written out,
1107 * we now need to reset its state.
1109 dbuf_unoverride(dr);
1110 if (db->db.db_object != DMU_META_DNODE_OBJECT &&
1111 db->db_state != DB_NOFILL)
1112 arc_buf_thaw(db->db_buf);
1114 mutex_exit(&db->db_mtx);
1119 * Only valid if not already dirty.
1121 ASSERT(dn->dn_object == 0 ||
1122 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1123 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1125 ASSERT3U(dn->dn_nlevels, >, db->db_level);
1126 ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) ||
1127 dn->dn_phys->dn_nlevels > db->db_level ||
1128 dn->dn_next_nlevels[txgoff] > db->db_level ||
1129 dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level ||
1130 dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level);
1133 * We should only be dirtying in syncing context if it's the
1134 * mos or we're initializing the os or it's a special object.
1135 * However, we are allowed to dirty in syncing context provided
1136 * we already dirtied it in open context. Hence we must make
1137 * this assertion only if we're not already dirty.
1140 ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1141 os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp));
1142 ASSERT(db->db.db_size != 0);
1144 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1146 if (db->db_blkid != DMU_BONUS_BLKID) {
1148 * Update the accounting.
1149 * Note: we delay "free accounting" until after we drop
1150 * the db_mtx. This keeps us from grabbing other locks
1151 * (and possibly deadlocking) in bp_get_dsize() while
1152 * also holding the db_mtx.
1154 dnode_willuse_space(dn, db->db.db_size, tx);
1155 do_free_accounting = dbuf_block_freeable(db);
1159 * If this buffer is dirty in an old transaction group we need
1160 * to make a copy of it so that the changes we make in this
1161 * transaction group won't leak out when we sync the older txg.
1163 dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_PUSHPAGE);
1164 list_link_init(&dr->dr_dirty_node);
1165 if (db->db_level == 0) {
1166 void *data_old = db->db_buf;
1168 if (db->db_state != DB_NOFILL) {
1169 if (db->db_blkid == DMU_BONUS_BLKID) {
1170 dbuf_fix_old_data(db, tx->tx_txg);
1171 data_old = db->db.db_data;
1172 } else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
1174 * Release the data buffer from the cache so
1175 * that we can modify it without impacting
1176 * possible other users of this cached data
1177 * block. Note that indirect blocks and
1178 * private objects are not released until the
1179 * syncing state (since they are only modified
1182 arc_release(db->db_buf, db);
1183 dbuf_fix_old_data(db, tx->tx_txg);
1184 data_old = db->db_buf;
1186 ASSERT(data_old != NULL);
1188 dr->dt.dl.dr_data = data_old;
1190 mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_DEFAULT, NULL);
1191 list_create(&dr->dt.di.dr_children,
1192 sizeof (dbuf_dirty_record_t),
1193 offsetof(dbuf_dirty_record_t, dr_dirty_node));
1196 dr->dr_txg = tx->tx_txg;
1201 * We could have been freed_in_flight between the dbuf_noread
1202 * and dbuf_dirty. We win, as though the dbuf_noread() had
1203 * happened after the free.
1205 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1206 db->db_blkid != DMU_SPILL_BLKID) {
1207 mutex_enter(&dn->dn_mtx);
1208 dnode_clear_range(dn, db->db_blkid, 1, tx);
1209 mutex_exit(&dn->dn_mtx);
1210 db->db_freed_in_flight = FALSE;
1214 * This buffer is now part of this txg
1216 dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg);
1217 db->db_dirtycnt += 1;
1218 ASSERT3U(db->db_dirtycnt, <=, 3);
1220 mutex_exit(&db->db_mtx);
1222 if (db->db_blkid == DMU_BONUS_BLKID ||
1223 db->db_blkid == DMU_SPILL_BLKID) {
1224 mutex_enter(&dn->dn_mtx);
1225 ASSERT(!list_link_active(&dr->dr_dirty_node));
1226 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1227 mutex_exit(&dn->dn_mtx);
1228 dnode_setdirty(dn, tx);
1231 } else if (do_free_accounting) {
1232 blkptr_t *bp = db->db_blkptr;
1233 int64_t willfree = (bp && !BP_IS_HOLE(bp)) ?
1234 bp_get_dsize(os->os_spa, bp) : db->db.db_size;
1236 * This is only a guess -- if the dbuf is dirty
1237 * in a previous txg, we don't know how much
1238 * space it will use on disk yet. We should
1239 * really have the struct_rwlock to access
1240 * db_blkptr, but since this is just a guess,
1241 * it's OK if we get an odd answer.
1243 ddt_prefetch(os->os_spa, bp);
1244 dnode_willuse_space(dn, -willfree, tx);
1247 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
1248 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1249 drop_struct_lock = TRUE;
1252 if (db->db_level == 0) {
1253 dnode_new_blkid(dn, db->db_blkid, tx, drop_struct_lock);
1254 ASSERT(dn->dn_maxblkid >= db->db_blkid);
1257 if (db->db_level+1 < dn->dn_nlevels) {
1258 dmu_buf_impl_t *parent = db->db_parent;
1259 dbuf_dirty_record_t *di;
1260 int parent_held = FALSE;
1262 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
1263 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1265 parent = dbuf_hold_level(dn, db->db_level+1,
1266 db->db_blkid >> epbs, FTAG);
1267 ASSERT(parent != NULL);
1270 if (drop_struct_lock)
1271 rw_exit(&dn->dn_struct_rwlock);
1272 ASSERT3U(db->db_level+1, ==, parent->db_level);
1273 di = dbuf_dirty(parent, tx);
1275 dbuf_rele(parent, FTAG);
1277 mutex_enter(&db->db_mtx);
1278 /* possible race with dbuf_undirty() */
1279 if (db->db_last_dirty == dr ||
1280 dn->dn_object == DMU_META_DNODE_OBJECT) {
1281 mutex_enter(&di->dt.di.dr_mtx);
1282 ASSERT3U(di->dr_txg, ==, tx->tx_txg);
1283 ASSERT(!list_link_active(&dr->dr_dirty_node));
1284 list_insert_tail(&di->dt.di.dr_children, dr);
1285 mutex_exit(&di->dt.di.dr_mtx);
1288 mutex_exit(&db->db_mtx);
1290 ASSERT(db->db_level+1 == dn->dn_nlevels);
1291 ASSERT(db->db_blkid < dn->dn_nblkptr);
1292 ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf);
1293 mutex_enter(&dn->dn_mtx);
1294 ASSERT(!list_link_active(&dr->dr_dirty_node));
1295 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1296 mutex_exit(&dn->dn_mtx);
1297 if (drop_struct_lock)
1298 rw_exit(&dn->dn_struct_rwlock);
1301 dnode_setdirty(dn, tx);
1307 dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1310 uint64_t txg = tx->tx_txg;
1311 dbuf_dirty_record_t *dr, **drp;
1314 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1316 mutex_enter(&db->db_mtx);
1318 * If this buffer is not dirty, we're done.
1320 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1321 if (dr->dr_txg <= txg)
1323 if (dr == NULL || dr->dr_txg < txg) {
1324 mutex_exit(&db->db_mtx);
1327 ASSERT(dr->dr_txg == txg);
1328 ASSERT(dr->dr_dbuf == db);
1334 * If this buffer is currently held, we cannot undirty
1335 * it, since one of the current holders may be in the
1336 * middle of an update. Note that users of dbuf_undirty()
1337 * should not place a hold on the dbuf before the call.
1338 * Also note: we can get here with a spill block, so
1339 * test for that similar to how dbuf_dirty does.
1341 if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
1342 mutex_exit(&db->db_mtx);
1343 /* Make sure we don't toss this buffer at sync phase */
1344 if (db->db_blkid != DMU_SPILL_BLKID) {
1345 mutex_enter(&dn->dn_mtx);
1346 dnode_clear_range(dn, db->db_blkid, 1, tx);
1347 mutex_exit(&dn->dn_mtx);
1353 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1355 ASSERT(db->db.db_size != 0);
1357 /* XXX would be nice to fix up dn_towrite_space[] */
1362 * Note that there are three places in dbuf_dirty()
1363 * where this dirty record may be put on a list.
1364 * Make sure to do a list_remove corresponding to
1365 * every one of those list_insert calls.
1367 if (dr->dr_parent) {
1368 mutex_enter(&dr->dr_parent->dt.di.dr_mtx);
1369 list_remove(&dr->dr_parent->dt.di.dr_children, dr);
1370 mutex_exit(&dr->dr_parent->dt.di.dr_mtx);
1371 } else if (db->db_blkid == DMU_SPILL_BLKID ||
1372 db->db_level+1 == dn->dn_nlevels) {
1373 ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf);
1374 mutex_enter(&dn->dn_mtx);
1375 list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr);
1376 mutex_exit(&dn->dn_mtx);
1380 if (db->db_level == 0) {
1381 if (db->db_state != DB_NOFILL) {
1382 dbuf_unoverride(dr);
1384 ASSERT(db->db_buf != NULL);
1385 ASSERT(dr->dt.dl.dr_data != NULL);
1386 if (dr->dt.dl.dr_data != db->db_buf)
1387 VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data,
1391 ASSERT(db->db_buf != NULL);
1392 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
1393 mutex_destroy(&dr->dt.di.dr_mtx);
1394 list_destroy(&dr->dt.di.dr_children);
1396 kmem_free(dr, sizeof (dbuf_dirty_record_t));
1398 ASSERT(db->db_dirtycnt > 0);
1399 db->db_dirtycnt -= 1;
1401 if (refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) {
1402 arc_buf_t *buf = db->db_buf;
1404 ASSERT(db->db_state == DB_NOFILL || arc_released(buf));
1405 dbuf_set_data(db, NULL);
1406 VERIFY(arc_buf_remove_ref(buf, db) == 1);
1411 mutex_exit(&db->db_mtx);
1415 #pragma weak dmu_buf_will_dirty = dbuf_will_dirty
1417 dbuf_will_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1419 int rf = DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH;
1421 ASSERT(tx->tx_txg != 0);
1422 ASSERT(!refcount_is_zero(&db->db_holds));
1425 if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock))
1426 rf |= DB_RF_HAVESTRUCT;
1428 (void) dbuf_read(db, NULL, rf);
1429 (void) dbuf_dirty(db, tx);
1433 dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1435 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1437 db->db_state = DB_NOFILL;
1439 dmu_buf_will_fill(db_fake, tx);
1443 dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1445 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1447 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1448 ASSERT(tx->tx_txg != 0);
1449 ASSERT(db->db_level == 0);
1450 ASSERT(!refcount_is_zero(&db->db_holds));
1452 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
1453 dmu_tx_private_ok(tx));
1456 (void) dbuf_dirty(db, tx);
1459 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1462 dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx)
1464 mutex_enter(&db->db_mtx);
1467 if (db->db_state == DB_FILL) {
1468 if (db->db_level == 0 && db->db_freed_in_flight) {
1469 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1470 /* we were freed while filling */
1471 /* XXX dbuf_undirty? */
1472 bzero(db->db.db_data, db->db.db_size);
1473 db->db_freed_in_flight = FALSE;
1475 db->db_state = DB_CACHED;
1476 cv_broadcast(&db->db_changed);
1478 mutex_exit(&db->db_mtx);
1482 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1483 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1486 dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx)
1488 ASSERT(!refcount_is_zero(&db->db_holds));
1489 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1490 ASSERT(db->db_level == 0);
1491 ASSERT(DBUF_GET_BUFC_TYPE(db) == ARC_BUFC_DATA);
1492 ASSERT(buf != NULL);
1493 ASSERT(arc_buf_size(buf) == db->db.db_size);
1494 ASSERT(tx->tx_txg != 0);
1496 arc_return_buf(buf, db);
1497 ASSERT(arc_released(buf));
1499 mutex_enter(&db->db_mtx);
1501 while (db->db_state == DB_READ || db->db_state == DB_FILL)
1502 cv_wait(&db->db_changed, &db->db_mtx);
1504 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED);
1506 if (db->db_state == DB_CACHED &&
1507 refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) {
1508 mutex_exit(&db->db_mtx);
1509 (void) dbuf_dirty(db, tx);
1510 bcopy(buf->b_data, db->db.db_data, db->db.db_size);
1511 VERIFY(arc_buf_remove_ref(buf, db) == 1);
1512 xuio_stat_wbuf_copied();
1516 xuio_stat_wbuf_nocopy();
1517 if (db->db_state == DB_CACHED) {
1518 dbuf_dirty_record_t *dr = db->db_last_dirty;
1520 ASSERT(db->db_buf != NULL);
1521 if (dr != NULL && dr->dr_txg == tx->tx_txg) {
1522 ASSERT(dr->dt.dl.dr_data == db->db_buf);
1523 if (!arc_released(db->db_buf)) {
1524 ASSERT(dr->dt.dl.dr_override_state ==
1526 arc_release(db->db_buf, db);
1528 dr->dt.dl.dr_data = buf;
1529 VERIFY(arc_buf_remove_ref(db->db_buf, db) == 1);
1530 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) {
1531 arc_release(db->db_buf, db);
1532 VERIFY(arc_buf_remove_ref(db->db_buf, db) == 1);
1536 ASSERT(db->db_buf == NULL);
1537 dbuf_set_data(db, buf);
1538 db->db_state = DB_FILL;
1539 mutex_exit(&db->db_mtx);
1540 (void) dbuf_dirty(db, tx);
1541 dbuf_fill_done(db, tx);
1545 * "Clear" the contents of this dbuf. This will mark the dbuf
1546 * EVICTING and clear *most* of its references. Unfortunetely,
1547 * when we are not holding the dn_dbufs_mtx, we can't clear the
1548 * entry in the dn_dbufs list. We have to wait until dbuf_destroy()
1549 * in this case. For callers from the DMU we will usually see:
1550 * dbuf_clear()->arc_buf_evict()->dbuf_do_evict()->dbuf_destroy()
1551 * For the arc callback, we will usually see:
1552 * dbuf_do_evict()->dbuf_clear();dbuf_destroy()
1553 * Sometimes, though, we will get a mix of these two:
1554 * DMU: dbuf_clear()->arc_buf_evict()
1555 * ARC: dbuf_do_evict()->dbuf_destroy()
1558 dbuf_clear(dmu_buf_impl_t *db)
1561 dmu_buf_impl_t *parent = db->db_parent;
1562 dmu_buf_impl_t *dndb;
1563 int dbuf_gone = FALSE;
1565 ASSERT(MUTEX_HELD(&db->db_mtx));
1566 ASSERT(refcount_is_zero(&db->db_holds));
1568 dbuf_evict_user(db);
1570 if (db->db_state == DB_CACHED) {
1571 ASSERT(db->db.db_data != NULL);
1572 if (db->db_blkid == DMU_BONUS_BLKID) {
1573 zio_buf_free(db->db.db_data, DN_MAX_BONUSLEN);
1574 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
1576 db->db.db_data = NULL;
1577 db->db_state = DB_UNCACHED;
1580 ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL);
1581 ASSERT(db->db_data_pending == NULL);
1583 db->db_state = DB_EVICTING;
1584 db->db_blkptr = NULL;
1589 if (db->db_blkid != DMU_BONUS_BLKID && MUTEX_HELD(&dn->dn_dbufs_mtx)) {
1590 list_remove(&dn->dn_dbufs, db);
1591 (void) atomic_dec_32_nv(&dn->dn_dbufs_count);
1595 * Decrementing the dbuf count means that the hold corresponding
1596 * to the removed dbuf is no longer discounted in dnode_move(),
1597 * so the dnode cannot be moved until after we release the hold.
1598 * The membar_producer() ensures visibility of the decremented
1599 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
1603 db->db_dnode_handle = NULL;
1609 dbuf_gone = arc_buf_evict(db->db_buf);
1612 mutex_exit(&db->db_mtx);
1615 * If this dbuf is referenced from an indirect dbuf,
1616 * decrement the ref count on the indirect dbuf.
1618 if (parent && parent != dndb)
1619 dbuf_rele(parent, db);
1622 __attribute__((always_inline))
1624 dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse,
1625 dmu_buf_impl_t **parentp, blkptr_t **bpp, struct dbuf_hold_impl_data *dh)
1632 ASSERT(blkid != DMU_BONUS_BLKID);
1634 if (blkid == DMU_SPILL_BLKID) {
1635 mutex_enter(&dn->dn_mtx);
1636 if (dn->dn_have_spill &&
1637 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
1638 *bpp = &dn->dn_phys->dn_spill;
1641 dbuf_add_ref(dn->dn_dbuf, NULL);
1642 *parentp = dn->dn_dbuf;
1643 mutex_exit(&dn->dn_mtx);
1647 if (dn->dn_phys->dn_nlevels == 0)
1650 nlevels = dn->dn_phys->dn_nlevels;
1652 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1654 ASSERT3U(level * epbs, <, 64);
1655 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1656 if (level >= nlevels ||
1657 (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) {
1658 /* the buffer has no parent yet */
1660 } else if (level < nlevels-1) {
1661 /* this block is referenced from an indirect block */
1664 err = dbuf_hold_impl(dn, level+1, blkid >> epbs,
1665 fail_sparse, NULL, parentp);
1668 __dbuf_hold_impl_init(dh + 1, dn, dh->dh_level + 1,
1669 blkid >> epbs, fail_sparse, NULL,
1670 parentp, dh->dh_depth + 1);
1671 err = __dbuf_hold_impl(dh + 1);
1675 err = dbuf_read(*parentp, NULL,
1676 (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL));
1678 dbuf_rele(*parentp, NULL);
1682 *bpp = ((blkptr_t *)(*parentp)->db.db_data) +
1683 (blkid & ((1ULL << epbs) - 1));
1686 /* the block is referenced from the dnode */
1687 ASSERT3U(level, ==, nlevels-1);
1688 ASSERT(dn->dn_phys->dn_nblkptr == 0 ||
1689 blkid < dn->dn_phys->dn_nblkptr);
1691 dbuf_add_ref(dn->dn_dbuf, NULL);
1692 *parentp = dn->dn_dbuf;
1694 *bpp = &dn->dn_phys->dn_blkptr[blkid];
1699 static dmu_buf_impl_t *
1700 dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid,
1701 dmu_buf_impl_t *parent, blkptr_t *blkptr)
1703 objset_t *os = dn->dn_objset;
1704 dmu_buf_impl_t *db, *odb;
1706 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1707 ASSERT(dn->dn_type != DMU_OT_NONE);
1709 db = kmem_cache_alloc(dbuf_cache, KM_PUSHPAGE);
1712 db->db.db_object = dn->dn_object;
1713 db->db_level = level;
1714 db->db_blkid = blkid;
1715 db->db_last_dirty = NULL;
1716 db->db_dirtycnt = 0;
1717 db->db_dnode_handle = dn->dn_handle;
1718 db->db_parent = parent;
1719 db->db_blkptr = blkptr;
1721 db->db_user_ptr = NULL;
1722 db->db_user_data_ptr_ptr = NULL;
1723 db->db_evict_func = NULL;
1724 db->db_immediate_evict = 0;
1725 db->db_freed_in_flight = 0;
1727 if (blkid == DMU_BONUS_BLKID) {
1728 ASSERT3P(parent, ==, dn->dn_dbuf);
1729 db->db.db_size = DN_MAX_BONUSLEN -
1730 (dn->dn_nblkptr-1) * sizeof (blkptr_t);
1731 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
1732 db->db.db_offset = DMU_BONUS_BLKID;
1733 db->db_state = DB_UNCACHED;
1734 /* the bonus dbuf is not placed in the hash table */
1735 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1737 } else if (blkid == DMU_SPILL_BLKID) {
1738 db->db.db_size = (blkptr != NULL) ?
1739 BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE;
1740 db->db.db_offset = 0;
1743 db->db_level ? 1<<dn->dn_indblkshift : dn->dn_datablksz;
1744 db->db.db_size = blocksize;
1745 db->db.db_offset = db->db_blkid * blocksize;
1749 * Hold the dn_dbufs_mtx while we get the new dbuf
1750 * in the hash table *and* added to the dbufs list.
1751 * This prevents a possible deadlock with someone
1752 * trying to look up this dbuf before its added to the
1755 mutex_enter(&dn->dn_dbufs_mtx);
1756 db->db_state = DB_EVICTING;
1757 if ((odb = dbuf_hash_insert(db)) != NULL) {
1758 /* someone else inserted it first */
1759 kmem_cache_free(dbuf_cache, db);
1760 mutex_exit(&dn->dn_dbufs_mtx);
1763 list_insert_head(&dn->dn_dbufs, db);
1764 db->db_state = DB_UNCACHED;
1765 mutex_exit(&dn->dn_dbufs_mtx);
1766 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1768 if (parent && parent != dn->dn_dbuf)
1769 dbuf_add_ref(parent, db);
1771 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1772 refcount_count(&dn->dn_holds) > 0);
1773 (void) refcount_add(&dn->dn_holds, db);
1774 (void) atomic_inc_32_nv(&dn->dn_dbufs_count);
1776 dprintf_dbuf(db, "db=%p\n", db);
1782 dbuf_do_evict(void *private)
1784 arc_buf_t *buf = private;
1785 dmu_buf_impl_t *db = buf->b_private;
1787 if (!MUTEX_HELD(&db->db_mtx))
1788 mutex_enter(&db->db_mtx);
1790 ASSERT(refcount_is_zero(&db->db_holds));
1792 if (db->db_state != DB_EVICTING) {
1793 ASSERT(db->db_state == DB_CACHED);
1798 mutex_exit(&db->db_mtx);
1805 dbuf_destroy(dmu_buf_impl_t *db)
1807 ASSERT(refcount_is_zero(&db->db_holds));
1809 if (db->db_blkid != DMU_BONUS_BLKID) {
1811 * If this dbuf is still on the dn_dbufs list,
1812 * remove it from that list.
1814 if (db->db_dnode_handle != NULL) {
1819 mutex_enter(&dn->dn_dbufs_mtx);
1820 list_remove(&dn->dn_dbufs, db);
1821 (void) atomic_dec_32_nv(&dn->dn_dbufs_count);
1822 mutex_exit(&dn->dn_dbufs_mtx);
1825 * Decrementing the dbuf count means that the hold
1826 * corresponding to the removed dbuf is no longer
1827 * discounted in dnode_move(), so the dnode cannot be
1828 * moved until after we release the hold.
1831 db->db_dnode_handle = NULL;
1833 dbuf_hash_remove(db);
1835 db->db_parent = NULL;
1838 ASSERT(!list_link_active(&db->db_link));
1839 ASSERT(db->db.db_data == NULL);
1840 ASSERT(db->db_hash_next == NULL);
1841 ASSERT(db->db_blkptr == NULL);
1842 ASSERT(db->db_data_pending == NULL);
1844 kmem_cache_free(dbuf_cache, db);
1845 arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1849 dbuf_prefetch(dnode_t *dn, uint64_t blkid)
1851 dmu_buf_impl_t *db = NULL;
1852 blkptr_t *bp = NULL;
1854 ASSERT(blkid != DMU_BONUS_BLKID);
1855 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1857 if (dnode_block_freed(dn, blkid))
1860 /* dbuf_find() returns with db_mtx held */
1861 if ((db = dbuf_find(dn, 0, blkid))) {
1863 * This dbuf is already in the cache. We assume that
1864 * it is already CACHED, or else about to be either
1867 mutex_exit(&db->db_mtx);
1871 if (dbuf_findbp(dn, 0, blkid, TRUE, &db, &bp, NULL) == 0) {
1872 if (bp && !BP_IS_HOLE(bp)) {
1873 int priority = dn->dn_type == DMU_OT_DDT_ZAP ?
1874 ZIO_PRIORITY_DDT_PREFETCH : ZIO_PRIORITY_ASYNC_READ;
1875 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
1876 uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
1879 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
1880 dn->dn_object, 0, blkid);
1882 (void) arc_read(NULL, dn->dn_objset->os_spa,
1883 bp, NULL, NULL, priority,
1884 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
1888 dbuf_rele(db, NULL);
1892 #define DBUF_HOLD_IMPL_MAX_DEPTH 20
1895 * Returns with db_holds incremented, and db_mtx not held.
1896 * Note: dn_struct_rwlock must be held.
1899 __dbuf_hold_impl(struct dbuf_hold_impl_data *dh)
1901 ASSERT3S(dh->dh_depth, <, DBUF_HOLD_IMPL_MAX_DEPTH);
1902 dh->dh_parent = NULL;
1904 ASSERT(dh->dh_blkid != DMU_BONUS_BLKID);
1905 ASSERT(RW_LOCK_HELD(&dh->dh_dn->dn_struct_rwlock));
1906 ASSERT3U(dh->dh_dn->dn_nlevels, >, dh->dh_level);
1908 *(dh->dh_dbp) = NULL;
1910 /* dbuf_find() returns with db_mtx held */
1911 dh->dh_db = dbuf_find(dh->dh_dn, dh->dh_level, dh->dh_blkid);
1913 if (dh->dh_db == NULL) {
1916 ASSERT3P(dh->dh_parent, ==, NULL);
1917 dh->dh_err = dbuf_findbp(dh->dh_dn, dh->dh_level, dh->dh_blkid,
1918 dh->dh_fail_sparse, &dh->dh_parent,
1920 if (dh->dh_fail_sparse) {
1921 if (dh->dh_err == 0 && dh->dh_bp && BP_IS_HOLE(dh->dh_bp))
1922 dh->dh_err = ENOENT;
1925 dbuf_rele(dh->dh_parent, NULL);
1926 return (dh->dh_err);
1929 if (dh->dh_err && dh->dh_err != ENOENT)
1930 return (dh->dh_err);
1931 dh->dh_db = dbuf_create(dh->dh_dn, dh->dh_level, dh->dh_blkid,
1932 dh->dh_parent, dh->dh_bp);
1935 if (dh->dh_db->db_buf && refcount_is_zero(&dh->dh_db->db_holds)) {
1936 arc_buf_add_ref(dh->dh_db->db_buf, dh->dh_db);
1937 if (dh->dh_db->db_buf->b_data == NULL) {
1938 dbuf_clear(dh->dh_db);
1939 if (dh->dh_parent) {
1940 dbuf_rele(dh->dh_parent, NULL);
1941 dh->dh_parent = NULL;
1945 ASSERT3P(dh->dh_db->db.db_data, ==, dh->dh_db->db_buf->b_data);
1948 ASSERT(dh->dh_db->db_buf == NULL || arc_referenced(dh->dh_db->db_buf));
1951 * If this buffer is currently syncing out, and we are are
1952 * still referencing it from db_data, we need to make a copy
1953 * of it in case we decide we want to dirty it again in this txg.
1955 if (dh->dh_db->db_level == 0 &&
1956 dh->dh_db->db_blkid != DMU_BONUS_BLKID &&
1957 dh->dh_dn->dn_object != DMU_META_DNODE_OBJECT &&
1958 dh->dh_db->db_state == DB_CACHED && dh->dh_db->db_data_pending) {
1959 dh->dh_dr = dh->dh_db->db_data_pending;
1961 if (dh->dh_dr->dt.dl.dr_data == dh->dh_db->db_buf) {
1962 dh->dh_type = DBUF_GET_BUFC_TYPE(dh->dh_db);
1964 dbuf_set_data(dh->dh_db,
1965 arc_buf_alloc(dh->dh_dn->dn_objset->os_spa,
1966 dh->dh_db->db.db_size, dh->dh_db, dh->dh_type));
1967 bcopy(dh->dh_dr->dt.dl.dr_data->b_data,
1968 dh->dh_db->db.db_data, dh->dh_db->db.db_size);
1972 (void) refcount_add(&dh->dh_db->db_holds, dh->dh_tag);
1973 dbuf_update_data(dh->dh_db);
1974 DBUF_VERIFY(dh->dh_db);
1975 mutex_exit(&dh->dh_db->db_mtx);
1977 /* NOTE: we can't rele the parent until after we drop the db_mtx */
1979 dbuf_rele(dh->dh_parent, NULL);
1981 ASSERT3P(DB_DNODE(dh->dh_db), ==, dh->dh_dn);
1982 ASSERT3U(dh->dh_db->db_blkid, ==, dh->dh_blkid);
1983 ASSERT3U(dh->dh_db->db_level, ==, dh->dh_level);
1984 *(dh->dh_dbp) = dh->dh_db;
1990 * The following code preserves the recursive function dbuf_hold_impl()
1991 * but moves the local variables AND function arguments to the heap to
1992 * minimize the stack frame size. Enough space is initially allocated
1993 * on the stack for 20 levels of recursion.
1996 dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid, int fail_sparse,
1997 void *tag, dmu_buf_impl_t **dbp)
1999 struct dbuf_hold_impl_data *dh;
2002 dh = kmem_zalloc(sizeof(struct dbuf_hold_impl_data) *
2003 DBUF_HOLD_IMPL_MAX_DEPTH, KM_PUSHPAGE);
2004 __dbuf_hold_impl_init(dh, dn, level, blkid, fail_sparse, tag, dbp, 0);
2006 error = __dbuf_hold_impl(dh);
2008 kmem_free(dh, sizeof(struct dbuf_hold_impl_data) *
2009 DBUF_HOLD_IMPL_MAX_DEPTH);
2015 __dbuf_hold_impl_init(struct dbuf_hold_impl_data *dh,
2016 dnode_t *dn, uint8_t level, uint64_t blkid, int fail_sparse,
2017 void *tag, dmu_buf_impl_t **dbp, int depth)
2020 dh->dh_level = level;
2021 dh->dh_blkid = blkid;
2022 dh->dh_fail_sparse = fail_sparse;
2025 dh->dh_depth = depth;
2029 dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag)
2032 int err = dbuf_hold_impl(dn, 0, blkid, FALSE, tag, &db);
2033 return (err ? NULL : db);
2037 dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag)
2040 int err = dbuf_hold_impl(dn, level, blkid, FALSE, tag, &db);
2041 return (err ? NULL : db);
2045 dbuf_create_bonus(dnode_t *dn)
2047 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
2049 ASSERT(dn->dn_bonus == NULL);
2050 dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL);
2054 dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx)
2056 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2059 if (db->db_blkid != DMU_SPILL_BLKID)
2062 blksz = SPA_MINBLOCKSIZE;
2063 if (blksz > SPA_MAXBLOCKSIZE)
2064 blksz = SPA_MAXBLOCKSIZE;
2066 blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE);
2070 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2071 dbuf_new_size(db, blksz, tx);
2072 rw_exit(&dn->dn_struct_rwlock);
2079 dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx)
2081 dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx);
2084 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2086 dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
2088 VERIFY(refcount_add(&db->db_holds, tag) > 1);
2092 * If you call dbuf_rele() you had better not be referencing the dnode handle
2093 * unless you have some other direct or indirect hold on the dnode. (An indirect
2094 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2095 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2096 * dnode's parent dbuf evicting its dnode handles.
2098 #pragma weak dmu_buf_rele = dbuf_rele
2100 dbuf_rele(dmu_buf_impl_t *db, void *tag)
2102 mutex_enter(&db->db_mtx);
2103 dbuf_rele_and_unlock(db, tag);
2107 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2108 * db_dirtycnt and db_holds to be updated atomically.
2111 dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag)
2115 ASSERT(MUTEX_HELD(&db->db_mtx));
2119 * Remove the reference to the dbuf before removing its hold on the
2120 * dnode so we can guarantee in dnode_move() that a referenced bonus
2121 * buffer has a corresponding dnode hold.
2123 holds = refcount_remove(&db->db_holds, tag);
2127 * We can't freeze indirects if there is a possibility that they
2128 * may be modified in the current syncing context.
2130 if (db->db_buf && holds == (db->db_level == 0 ? db->db_dirtycnt : 0))
2131 arc_buf_freeze(db->db_buf);
2133 if (holds == db->db_dirtycnt &&
2134 db->db_level == 0 && db->db_immediate_evict)
2135 dbuf_evict_user(db);
2138 if (db->db_blkid == DMU_BONUS_BLKID) {
2139 mutex_exit(&db->db_mtx);
2142 * If the dnode moves here, we cannot cross this barrier
2143 * until the move completes.
2146 (void) atomic_dec_32_nv(&DB_DNODE(db)->dn_dbufs_count);
2149 * The bonus buffer's dnode hold is no longer discounted
2150 * in dnode_move(). The dnode cannot move until after
2153 dnode_rele(DB_DNODE(db), db);
2154 } else if (db->db_buf == NULL) {
2156 * This is a special case: we never associated this
2157 * dbuf with any data allocated from the ARC.
2159 ASSERT(db->db_state == DB_UNCACHED ||
2160 db->db_state == DB_NOFILL);
2162 } else if (arc_released(db->db_buf)) {
2163 arc_buf_t *buf = db->db_buf;
2165 * This dbuf has anonymous data associated with it.
2167 dbuf_set_data(db, NULL);
2168 VERIFY(arc_buf_remove_ref(buf, db) == 1);
2171 VERIFY(arc_buf_remove_ref(db->db_buf, db) == 0);
2174 * A dbuf will be eligible for eviction if either the
2175 * 'primarycache' property is set or a duplicate
2176 * copy of this buffer is already cached in the arc.
2178 * In the case of the 'primarycache' a buffer
2179 * is considered for eviction if it matches the
2180 * criteria set in the property.
2182 * To decide if our buffer is considered a
2183 * duplicate, we must call into the arc to determine
2184 * if multiple buffers are referencing the same
2185 * block on-disk. If so, then we simply evict
2188 if (!DBUF_IS_CACHEABLE(db) ||
2189 arc_buf_eviction_needed(db->db_buf))
2192 mutex_exit(&db->db_mtx);
2195 mutex_exit(&db->db_mtx);
2199 #pragma weak dmu_buf_refcount = dbuf_refcount
2201 dbuf_refcount(dmu_buf_impl_t *db)
2203 return (refcount_count(&db->db_holds));
2207 dmu_buf_set_user(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr,
2208 dmu_buf_evict_func_t *evict_func)
2210 return (dmu_buf_update_user(db_fake, NULL, user_ptr,
2211 user_data_ptr_ptr, evict_func));
2215 dmu_buf_set_user_ie(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr,
2216 dmu_buf_evict_func_t *evict_func)
2218 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2220 db->db_immediate_evict = TRUE;
2221 return (dmu_buf_update_user(db_fake, NULL, user_ptr,
2222 user_data_ptr_ptr, evict_func));
2226 dmu_buf_update_user(dmu_buf_t *db_fake, void *old_user_ptr, void *user_ptr,
2227 void *user_data_ptr_ptr, dmu_buf_evict_func_t *evict_func)
2229 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2230 ASSERT(db->db_level == 0);
2232 ASSERT((user_ptr == NULL) == (evict_func == NULL));
2234 mutex_enter(&db->db_mtx);
2236 if (db->db_user_ptr == old_user_ptr) {
2237 db->db_user_ptr = user_ptr;
2238 db->db_user_data_ptr_ptr = user_data_ptr_ptr;
2239 db->db_evict_func = evict_func;
2241 dbuf_update_data(db);
2243 old_user_ptr = db->db_user_ptr;
2246 mutex_exit(&db->db_mtx);
2247 return (old_user_ptr);
2251 dmu_buf_get_user(dmu_buf_t *db_fake)
2253 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2254 ASSERT(!refcount_is_zero(&db->db_holds));
2256 return (db->db_user_ptr);
2260 dmu_buf_freeable(dmu_buf_t *dbuf)
2262 boolean_t res = B_FALSE;
2263 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
2266 res = dsl_dataset_block_freeable(db->db_objset->os_dsl_dataset,
2267 db->db_blkptr, db->db_blkptr->blk_birth);
2273 dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db)
2275 /* ASSERT(dmu_tx_is_syncing(tx) */
2276 ASSERT(MUTEX_HELD(&db->db_mtx));
2278 if (db->db_blkptr != NULL)
2281 if (db->db_blkid == DMU_SPILL_BLKID) {
2282 db->db_blkptr = &dn->dn_phys->dn_spill;
2283 BP_ZERO(db->db_blkptr);
2286 if (db->db_level == dn->dn_phys->dn_nlevels-1) {
2288 * This buffer was allocated at a time when there was
2289 * no available blkptrs from the dnode, or it was
2290 * inappropriate to hook it in (i.e., nlevels mis-match).
2292 ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr);
2293 ASSERT(db->db_parent == NULL);
2294 db->db_parent = dn->dn_dbuf;
2295 db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
2298 dmu_buf_impl_t *parent = db->db_parent;
2299 int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2301 ASSERT(dn->dn_phys->dn_nlevels > 1);
2302 if (parent == NULL) {
2303 mutex_exit(&db->db_mtx);
2304 rw_enter(&dn->dn_struct_rwlock, RW_READER);
2305 (void) dbuf_hold_impl(dn, db->db_level+1,
2306 db->db_blkid >> epbs, FALSE, db, &parent);
2307 rw_exit(&dn->dn_struct_rwlock);
2308 mutex_enter(&db->db_mtx);
2309 db->db_parent = parent;
2311 db->db_blkptr = (blkptr_t *)parent->db.db_data +
2312 (db->db_blkid & ((1ULL << epbs) - 1));
2317 /* dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
2318 * is critical the we not allow the compiler to inline this function in to
2319 * dbuf_sync_list() thereby drastically bloating the stack usage.
2321 noinline static void
2322 dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2324 dmu_buf_impl_t *db = dr->dr_dbuf;
2328 ASSERT(dmu_tx_is_syncing(tx));
2330 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2332 mutex_enter(&db->db_mtx);
2334 ASSERT(db->db_level > 0);
2337 if (db->db_buf == NULL) {
2338 mutex_exit(&db->db_mtx);
2339 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
2340 mutex_enter(&db->db_mtx);
2342 ASSERT3U(db->db_state, ==, DB_CACHED);
2343 ASSERT(db->db_buf != NULL);
2347 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2348 dbuf_check_blkptr(dn, db);
2351 db->db_data_pending = dr;
2353 mutex_exit(&db->db_mtx);
2354 dbuf_write(dr, db->db_buf, tx);
2357 mutex_enter(&dr->dt.di.dr_mtx);
2358 dbuf_sync_list(&dr->dt.di.dr_children, tx);
2359 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
2360 mutex_exit(&dr->dt.di.dr_mtx);
2364 /* dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
2365 * critical the we not allow the compiler to inline this function in to
2366 * dbuf_sync_list() thereby drastically bloating the stack usage.
2368 noinline static void
2369 dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2371 arc_buf_t **datap = &dr->dt.dl.dr_data;
2372 dmu_buf_impl_t *db = dr->dr_dbuf;
2375 uint64_t txg = tx->tx_txg;
2377 ASSERT(dmu_tx_is_syncing(tx));
2379 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2381 mutex_enter(&db->db_mtx);
2383 * To be synced, we must be dirtied. But we
2384 * might have been freed after the dirty.
2386 if (db->db_state == DB_UNCACHED) {
2387 /* This buffer has been freed since it was dirtied */
2388 ASSERT(db->db.db_data == NULL);
2389 } else if (db->db_state == DB_FILL) {
2390 /* This buffer was freed and is now being re-filled */
2391 ASSERT(db->db.db_data != dr->dt.dl.dr_data);
2393 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL);
2400 if (db->db_blkid == DMU_SPILL_BLKID) {
2401 mutex_enter(&dn->dn_mtx);
2402 dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR;
2403 mutex_exit(&dn->dn_mtx);
2407 * If this is a bonus buffer, simply copy the bonus data into the
2408 * dnode. It will be written out when the dnode is synced (and it
2409 * will be synced, since it must have been dirty for dbuf_sync to
2412 if (db->db_blkid == DMU_BONUS_BLKID) {
2413 dbuf_dirty_record_t **drp;
2415 ASSERT(*datap != NULL);
2416 ASSERT0(db->db_level);
2417 ASSERT3U(dn->dn_phys->dn_bonuslen, <=, DN_MAX_BONUSLEN);
2418 bcopy(*datap, DN_BONUS(dn->dn_phys), dn->dn_phys->dn_bonuslen);
2421 if (*datap != db->db.db_data) {
2422 zio_buf_free(*datap, DN_MAX_BONUSLEN);
2423 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
2425 db->db_data_pending = NULL;
2426 drp = &db->db_last_dirty;
2428 drp = &(*drp)->dr_next;
2429 ASSERT(dr->dr_next == NULL);
2430 ASSERT(dr->dr_dbuf == db);
2432 if (dr->dr_dbuf->db_level != 0) {
2433 mutex_destroy(&dr->dt.di.dr_mtx);
2434 list_destroy(&dr->dt.di.dr_children);
2436 kmem_free(dr, sizeof (dbuf_dirty_record_t));
2437 ASSERT(db->db_dirtycnt > 0);
2438 db->db_dirtycnt -= 1;
2439 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
2446 * This function may have dropped the db_mtx lock allowing a dmu_sync
2447 * operation to sneak in. As a result, we need to ensure that we
2448 * don't check the dr_override_state until we have returned from
2449 * dbuf_check_blkptr.
2451 dbuf_check_blkptr(dn, db);
2454 * If this buffer is in the middle of an immediate write,
2455 * wait for the synchronous IO to complete.
2457 while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
2458 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
2459 cv_wait(&db->db_changed, &db->db_mtx);
2460 ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN);
2463 if (db->db_state != DB_NOFILL &&
2464 dn->dn_object != DMU_META_DNODE_OBJECT &&
2465 refcount_count(&db->db_holds) > 1 &&
2466 dr->dt.dl.dr_override_state != DR_OVERRIDDEN &&
2467 *datap == db->db_buf) {
2469 * If this buffer is currently "in use" (i.e., there
2470 * are active holds and db_data still references it),
2471 * then make a copy before we start the write so that
2472 * any modifications from the open txg will not leak
2475 * NOTE: this copy does not need to be made for
2476 * objects only modified in the syncing context (e.g.
2477 * DNONE_DNODE blocks).
2479 int blksz = arc_buf_size(*datap);
2480 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
2481 *datap = arc_buf_alloc(os->os_spa, blksz, db, type);
2482 bcopy(db->db.db_data, (*datap)->b_data, blksz);
2484 db->db_data_pending = dr;
2486 mutex_exit(&db->db_mtx);
2488 dbuf_write(dr, *datap, tx);
2490 ASSERT(!list_link_active(&dr->dr_dirty_node));
2491 if (dn->dn_object == DMU_META_DNODE_OBJECT) {
2492 list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr);
2496 * Although zio_nowait() does not "wait for an IO", it does
2497 * initiate the IO. If this is an empty write it seems plausible
2498 * that the IO could actually be completed before the nowait
2499 * returns. We need to DB_DNODE_EXIT() first in case
2500 * zio_nowait() invalidates the dbuf.
2503 zio_nowait(dr->dr_zio);
2508 dbuf_sync_list(list_t *list, dmu_tx_t *tx)
2510 dbuf_dirty_record_t *dr;
2512 while ((dr = list_head(list))) {
2513 if (dr->dr_zio != NULL) {
2515 * If we find an already initialized zio then we
2516 * are processing the meta-dnode, and we have finished.
2517 * The dbufs for all dnodes are put back on the list
2518 * during processing, so that we can zio_wait()
2519 * these IOs after initiating all child IOs.
2521 ASSERT3U(dr->dr_dbuf->db.db_object, ==,
2522 DMU_META_DNODE_OBJECT);
2525 list_remove(list, dr);
2526 if (dr->dr_dbuf->db_level > 0)
2527 dbuf_sync_indirect(dr, tx);
2529 dbuf_sync_leaf(dr, tx);
2535 dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
2537 dmu_buf_impl_t *db = vdb;
2539 blkptr_t *bp = zio->io_bp;
2540 blkptr_t *bp_orig = &zio->io_bp_orig;
2541 spa_t *spa = zio->io_spa;
2546 ASSERT(db->db_blkptr == bp);
2550 delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig);
2551 dnode_diduse_space(dn, delta - zio->io_prev_space_delta);
2552 zio->io_prev_space_delta = delta;
2554 if (BP_IS_HOLE(bp)) {
2555 ASSERT(bp->blk_fill == 0);
2560 ASSERT((db->db_blkid != DMU_SPILL_BLKID &&
2561 BP_GET_TYPE(bp) == dn->dn_type) ||
2562 (db->db_blkid == DMU_SPILL_BLKID &&
2563 BP_GET_TYPE(bp) == dn->dn_bonustype));
2564 ASSERT(BP_GET_LEVEL(bp) == db->db_level);
2566 mutex_enter(&db->db_mtx);
2569 if (db->db_blkid == DMU_SPILL_BLKID) {
2570 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
2571 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
2572 db->db_blkptr == &dn->dn_phys->dn_spill);
2576 if (db->db_level == 0) {
2577 mutex_enter(&dn->dn_mtx);
2578 if (db->db_blkid > dn->dn_phys->dn_maxblkid &&
2579 db->db_blkid != DMU_SPILL_BLKID)
2580 dn->dn_phys->dn_maxblkid = db->db_blkid;
2581 mutex_exit(&dn->dn_mtx);
2583 if (dn->dn_type == DMU_OT_DNODE) {
2584 dnode_phys_t *dnp = db->db.db_data;
2585 for (i = db->db.db_size >> DNODE_SHIFT; i > 0;
2587 if (dnp->dn_type != DMU_OT_NONE)
2594 blkptr_t *ibp = db->db.db_data;
2595 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2596 for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) {
2597 if (BP_IS_HOLE(ibp))
2599 fill += ibp->blk_fill;
2604 bp->blk_fill = fill;
2606 mutex_exit(&db->db_mtx);
2611 dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
2613 dmu_buf_impl_t *db = vdb;
2614 blkptr_t *bp = zio->io_bp;
2615 blkptr_t *bp_orig = &zio->io_bp_orig;
2616 uint64_t txg = zio->io_txg;
2617 dbuf_dirty_record_t **drp, *dr;
2619 ASSERT0(zio->io_error);
2620 ASSERT(db->db_blkptr == bp);
2622 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
2623 ASSERT(BP_EQUAL(bp, bp_orig));
2629 DB_GET_OBJSET(&os, db);
2630 ds = os->os_dsl_dataset;
2633 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
2634 dsl_dataset_block_born(ds, bp, tx);
2637 mutex_enter(&db->db_mtx);
2641 drp = &db->db_last_dirty;
2642 while ((dr = *drp) != db->db_data_pending)
2644 ASSERT(!list_link_active(&dr->dr_dirty_node));
2645 ASSERT(dr->dr_txg == txg);
2646 ASSERT(dr->dr_dbuf == db);
2647 ASSERT(dr->dr_next == NULL);
2651 if (db->db_blkid == DMU_SPILL_BLKID) {
2656 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
2657 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
2658 db->db_blkptr == &dn->dn_phys->dn_spill);
2663 if (db->db_level == 0) {
2664 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2665 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
2666 if (db->db_state != DB_NOFILL) {
2667 if (dr->dt.dl.dr_data != db->db_buf)
2668 VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data,
2670 else if (!arc_released(db->db_buf))
2671 arc_set_callback(db->db_buf, dbuf_do_evict, db);
2678 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
2679 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2680 if (!BP_IS_HOLE(db->db_blkptr)) {
2681 ASSERTV(int epbs = dn->dn_phys->dn_indblkshift -
2683 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
2685 ASSERT3U(dn->dn_phys->dn_maxblkid
2686 >> (db->db_level * epbs), >=, db->db_blkid);
2687 arc_set_callback(db->db_buf, dbuf_do_evict, db);
2690 mutex_destroy(&dr->dt.di.dr_mtx);
2691 list_destroy(&dr->dt.di.dr_children);
2693 kmem_free(dr, sizeof (dbuf_dirty_record_t));
2695 cv_broadcast(&db->db_changed);
2696 ASSERT(db->db_dirtycnt > 0);
2697 db->db_dirtycnt -= 1;
2698 db->db_data_pending = NULL;
2699 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
2703 dbuf_write_nofill_ready(zio_t *zio)
2705 dbuf_write_ready(zio, NULL, zio->io_private);
2709 dbuf_write_nofill_done(zio_t *zio)
2711 dbuf_write_done(zio, NULL, zio->io_private);
2715 dbuf_write_override_ready(zio_t *zio)
2717 dbuf_dirty_record_t *dr = zio->io_private;
2718 dmu_buf_impl_t *db = dr->dr_dbuf;
2720 dbuf_write_ready(zio, NULL, db);
2724 dbuf_write_override_done(zio_t *zio)
2726 dbuf_dirty_record_t *dr = zio->io_private;
2727 dmu_buf_impl_t *db = dr->dr_dbuf;
2728 blkptr_t *obp = &dr->dt.dl.dr_overridden_by;
2730 mutex_enter(&db->db_mtx);
2731 if (!BP_EQUAL(zio->io_bp, obp)) {
2732 if (!BP_IS_HOLE(obp))
2733 dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp);
2734 arc_release(dr->dt.dl.dr_data, db);
2736 mutex_exit(&db->db_mtx);
2738 dbuf_write_done(zio, NULL, db);
2742 dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx)
2744 dmu_buf_impl_t *db = dr->dr_dbuf;
2747 dmu_buf_impl_t *parent = db->db_parent;
2748 uint64_t txg = tx->tx_txg;
2758 if (db->db_state != DB_NOFILL) {
2759 if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) {
2761 * Private object buffers are released here rather
2762 * than in dbuf_dirty() since they are only modified
2763 * in the syncing context and we don't want the
2764 * overhead of making multiple copies of the data.
2766 if (BP_IS_HOLE(db->db_blkptr)) {
2769 dbuf_release_bp(db);
2774 if (parent != dn->dn_dbuf) {
2775 ASSERT(parent && parent->db_data_pending);
2776 ASSERT(db->db_level == parent->db_level-1);
2777 ASSERT(arc_released(parent->db_buf));
2778 zio = parent->db_data_pending->dr_zio;
2780 ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 &&
2781 db->db_blkid != DMU_SPILL_BLKID) ||
2782 (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0));
2783 if (db->db_blkid != DMU_SPILL_BLKID)
2784 ASSERT3P(db->db_blkptr, ==,
2785 &dn->dn_phys->dn_blkptr[db->db_blkid]);
2789 ASSERT(db->db_level == 0 || data == db->db_buf);
2790 ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
2793 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
2794 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
2795 db->db.db_object, db->db_level, db->db_blkid);
2797 if (db->db_blkid == DMU_SPILL_BLKID)
2799 wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
2801 dmu_write_policy(os, dn, db->db_level, wp_flag, &zp);
2804 if (db->db_level == 0 && dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
2805 ASSERT(db->db_state != DB_NOFILL);
2806 dr->dr_zio = zio_write(zio, os->os_spa, txg,
2807 db->db_blkptr, data->b_data, arc_buf_size(data), &zp,
2808 dbuf_write_override_ready, dbuf_write_override_done, dr,
2809 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
2810 mutex_enter(&db->db_mtx);
2811 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
2812 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by,
2813 dr->dt.dl.dr_copies);
2814 mutex_exit(&db->db_mtx);
2815 } else if (db->db_state == DB_NOFILL) {
2816 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF);
2817 dr->dr_zio = zio_write(zio, os->os_spa, txg,
2818 db->db_blkptr, NULL, db->db.db_size, &zp,
2819 dbuf_write_nofill_ready, dbuf_write_nofill_done, db,
2820 ZIO_PRIORITY_ASYNC_WRITE,
2821 ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb);
2823 ASSERT(arc_released(data));
2824 dr->dr_zio = arc_write(zio, os->os_spa, txg,
2825 db->db_blkptr, data, DBUF_IS_L2CACHEABLE(db), &zp,
2826 dbuf_write_ready, dbuf_write_done, db,
2827 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
2831 #if defined(_KERNEL) && defined(HAVE_SPL)
2832 EXPORT_SYMBOL(dbuf_find);
2833 EXPORT_SYMBOL(dbuf_is_metadata);
2834 EXPORT_SYMBOL(dbuf_evict);
2835 EXPORT_SYMBOL(dbuf_loan_arcbuf);
2836 EXPORT_SYMBOL(dbuf_whichblock);
2837 EXPORT_SYMBOL(dbuf_read);
2838 EXPORT_SYMBOL(dbuf_unoverride);
2839 EXPORT_SYMBOL(dbuf_free_range);
2840 EXPORT_SYMBOL(dbuf_new_size);
2841 EXPORT_SYMBOL(dbuf_release_bp);
2842 EXPORT_SYMBOL(dbuf_dirty);
2843 EXPORT_SYMBOL(dmu_buf_will_dirty);
2844 EXPORT_SYMBOL(dmu_buf_will_not_fill);
2845 EXPORT_SYMBOL(dmu_buf_will_fill);
2846 EXPORT_SYMBOL(dmu_buf_fill_done);
2847 EXPORT_SYMBOL(dmu_buf_rele);
2848 EXPORT_SYMBOL(dbuf_assign_arcbuf);
2849 EXPORT_SYMBOL(dbuf_clear);
2850 EXPORT_SYMBOL(dbuf_prefetch);
2851 EXPORT_SYMBOL(dbuf_hold_impl);
2852 EXPORT_SYMBOL(dbuf_hold);
2853 EXPORT_SYMBOL(dbuf_hold_level);
2854 EXPORT_SYMBOL(dbuf_create_bonus);
2855 EXPORT_SYMBOL(dbuf_spill_set_blksz);
2856 EXPORT_SYMBOL(dbuf_rm_spill);
2857 EXPORT_SYMBOL(dbuf_add_ref);
2858 EXPORT_SYMBOL(dbuf_rele);
2859 EXPORT_SYMBOL(dbuf_rele_and_unlock);
2860 EXPORT_SYMBOL(dbuf_refcount);
2861 EXPORT_SYMBOL(dbuf_sync_list);
2862 EXPORT_SYMBOL(dmu_buf_set_user);
2863 EXPORT_SYMBOL(dmu_buf_set_user_ie);
2864 EXPORT_SYMBOL(dmu_buf_update_user);
2865 EXPORT_SYMBOL(dmu_buf_get_user);
2866 EXPORT_SYMBOL(dmu_buf_freeable);