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, 2015 by Delphix. All rights reserved.
25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
30 #include <sys/zfs_context.h>
32 #include <sys/dmu_send.h>
33 #include <sys/dmu_impl.h>
35 #include <sys/dmu_objset.h>
36 #include <sys/dsl_dataset.h>
37 #include <sys/dsl_dir.h>
38 #include <sys/dmu_tx.h>
41 #include <sys/dmu_zfetch.h>
43 #include <sys/sa_impl.h>
44 #include <sys/zfeature.h>
45 #include <sys/blkptr.h>
46 #include <sys/range_tree.h>
49 * Number of times that zfs_free_range() took the slow path while doing
50 * a zfs receive. A nonzero value indicates a potential performance problem.
52 uint64_t zfs_free_range_recv_miss;
54 static void dbuf_destroy(dmu_buf_impl_t *db);
55 static boolean_t dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
56 static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx);
59 * Global data structures and functions for the dbuf cache.
61 static kmem_cache_t *dbuf_cache;
62 static taskq_t *dbu_evict_taskq;
66 dbuf_cons(void *vdb, void *unused, int kmflag)
68 dmu_buf_impl_t *db = vdb;
69 bzero(db, sizeof (dmu_buf_impl_t));
71 mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL);
72 cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL);
73 refcount_create(&db->db_holds);
80 dbuf_dest(void *vdb, void *unused)
82 dmu_buf_impl_t *db = vdb;
83 mutex_destroy(&db->db_mtx);
84 cv_destroy(&db->db_changed);
85 refcount_destroy(&db->db_holds);
89 * dbuf hash table routines
91 static dbuf_hash_table_t dbuf_hash_table;
93 static uint64_t dbuf_hash_count;
96 dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid)
98 uintptr_t osv = (uintptr_t)os;
101 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
102 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (lvl)) & 0xFF];
103 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF];
104 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF];
105 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF];
106 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 0)) & 0xFF];
107 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 8)) & 0xFF];
109 crc ^= (osv>>14) ^ (obj>>16) ^ (blkid>>16);
114 #define DBUF_HASH(os, obj, level, blkid) dbuf_hash(os, obj, level, blkid);
116 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
117 ((dbuf)->db.db_object == (obj) && \
118 (dbuf)->db_objset == (os) && \
119 (dbuf)->db_level == (level) && \
120 (dbuf)->db_blkid == (blkid))
123 dbuf_find(objset_t *os, uint64_t obj, uint8_t level, uint64_t blkid)
125 dbuf_hash_table_t *h = &dbuf_hash_table;
126 uint64_t hv = DBUF_HASH(os, obj, level, blkid);
127 uint64_t idx = hv & h->hash_table_mask;
130 mutex_enter(DBUF_HASH_MUTEX(h, idx));
131 for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) {
132 if (DBUF_EQUAL(db, os, obj, level, blkid)) {
133 mutex_enter(&db->db_mtx);
134 if (db->db_state != DB_EVICTING) {
135 mutex_exit(DBUF_HASH_MUTEX(h, idx));
138 mutex_exit(&db->db_mtx);
141 mutex_exit(DBUF_HASH_MUTEX(h, idx));
145 static dmu_buf_impl_t *
146 dbuf_find_bonus(objset_t *os, uint64_t object)
149 dmu_buf_impl_t *db = NULL;
151 if (dnode_hold(os, object, FTAG, &dn) == 0) {
152 rw_enter(&dn->dn_struct_rwlock, RW_READER);
153 if (dn->dn_bonus != NULL) {
155 mutex_enter(&db->db_mtx);
157 rw_exit(&dn->dn_struct_rwlock);
158 dnode_rele(dn, FTAG);
164 * Insert an entry into the hash table. If there is already an element
165 * equal to elem in the hash table, then the already existing element
166 * will be returned and the new element will not be inserted.
167 * Otherwise returns NULL.
169 static dmu_buf_impl_t *
170 dbuf_hash_insert(dmu_buf_impl_t *db)
172 dbuf_hash_table_t *h = &dbuf_hash_table;
173 objset_t *os = db->db_objset;
174 uint64_t obj = db->db.db_object;
175 int level = db->db_level;
176 uint64_t blkid = db->db_blkid;
177 uint64_t hv = DBUF_HASH(os, obj, level, blkid);
178 uint64_t 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_inc_64(&dbuf_hash_count);
203 * Remove an entry from the hash table. It must be in the EVICTING state.
206 dbuf_hash_remove(dmu_buf_impl_t *db)
208 dbuf_hash_table_t *h = &dbuf_hash_table;
209 uint64_t hv = DBUF_HASH(db->db_objset, db->db.db_object,
210 db->db_level, db->db_blkid);
211 uint64_t idx = hv & h->hash_table_mask;
212 dmu_buf_impl_t *dbf, **dbp;
215 * We musn't hold db_mtx to maintain lock ordering:
216 * DBUF_HASH_MUTEX > db_mtx.
218 ASSERT(refcount_is_zero(&db->db_holds));
219 ASSERT(db->db_state == DB_EVICTING);
220 ASSERT(!MUTEX_HELD(&db->db_mtx));
222 mutex_enter(DBUF_HASH_MUTEX(h, idx));
223 dbp = &h->hash_table[idx];
224 while ((dbf = *dbp) != db) {
225 dbp = &dbf->db_hash_next;
228 *dbp = db->db_hash_next;
229 db->db_hash_next = NULL;
230 mutex_exit(DBUF_HASH_MUTEX(h, idx));
231 atomic_dec_64(&dbuf_hash_count);
234 static arc_evict_func_t dbuf_do_evict;
239 } dbvu_verify_type_t;
242 dbuf_verify_user(dmu_buf_impl_t *db, dbvu_verify_type_t verify_type)
247 if (db->db_user == NULL)
250 /* Only data blocks support the attachment of user data. */
251 ASSERT(db->db_level == 0);
253 /* Clients must resolve a dbuf before attaching user data. */
254 ASSERT(db->db.db_data != NULL);
255 ASSERT3U(db->db_state, ==, DB_CACHED);
257 holds = refcount_count(&db->db_holds);
258 if (verify_type == DBVU_EVICTING) {
260 * Immediate eviction occurs when holds == dirtycnt.
261 * For normal eviction buffers, holds is zero on
262 * eviction, except when dbuf_fix_old_data() calls
263 * dbuf_clear_data(). However, the hold count can grow
264 * during eviction even though db_mtx is held (see
265 * dmu_bonus_hold() for an example), so we can only
266 * test the generic invariant that holds >= dirtycnt.
268 ASSERT3U(holds, >=, db->db_dirtycnt);
270 if (db->db_immediate_evict == TRUE)
271 ASSERT3U(holds, >=, db->db_dirtycnt);
273 ASSERT3U(holds, >, 0);
279 dbuf_evict_user(dmu_buf_impl_t *db)
281 dmu_buf_user_t *dbu = db->db_user;
283 ASSERT(MUTEX_HELD(&db->db_mtx));
288 dbuf_verify_user(db, DBVU_EVICTING);
292 if (dbu->dbu_clear_on_evict_dbufp != NULL)
293 *dbu->dbu_clear_on_evict_dbufp = NULL;
297 * Invoke the callback from a taskq to avoid lock order reversals
298 * and limit stack depth.
300 taskq_dispatch_ent(dbu_evict_taskq, dbu->dbu_evict_func, dbu, 0,
305 dbuf_is_metadata(dmu_buf_impl_t *db)
307 if (db->db_level > 0) {
310 boolean_t is_metadata;
313 is_metadata = DMU_OT_IS_METADATA(DB_DNODE(db)->dn_type);
316 return (is_metadata);
321 dbuf_evict(dmu_buf_impl_t *db)
323 ASSERT(MUTEX_HELD(&db->db_mtx));
324 ASSERT(db->db_buf == NULL);
325 ASSERT(db->db_data_pending == NULL);
334 uint64_t hsize = 1ULL << 16;
335 dbuf_hash_table_t *h = &dbuf_hash_table;
339 * The hash table is big enough to fill all of physical memory
340 * with an average 4K block size. The table will take up
341 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
343 while (hsize * 4096 < (uint64_t)physmem * PAGESIZE)
347 h->hash_table_mask = hsize - 1;
348 h->hash_table = kmem_zalloc(hsize * sizeof (void *), KM_NOSLEEP);
349 if (h->hash_table == NULL) {
350 /* XXX - we should really return an error instead of assert */
351 ASSERT(hsize > (1ULL << 10));
356 dbuf_cache = kmem_cache_create("dmu_buf_impl_t",
357 sizeof (dmu_buf_impl_t),
358 0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0);
360 for (i = 0; i < DBUF_MUTEXES; i++)
361 mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL);
364 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
365 * configuration is not required.
367 dbu_evict_taskq = taskq_create("dbu_evict", 1, minclsyspri, 0, 0, 0);
373 dbuf_hash_table_t *h = &dbuf_hash_table;
376 for (i = 0; i < DBUF_MUTEXES; i++)
377 mutex_destroy(&h->hash_mutexes[i]);
378 kmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
379 kmem_cache_destroy(dbuf_cache);
380 taskq_destroy(dbu_evict_taskq);
389 dbuf_verify(dmu_buf_impl_t *db)
392 dbuf_dirty_record_t *dr;
394 ASSERT(MUTEX_HELD(&db->db_mtx));
396 if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY))
399 ASSERT(db->db_objset != NULL);
403 ASSERT(db->db_parent == NULL);
404 ASSERT(db->db_blkptr == NULL);
406 ASSERT3U(db->db.db_object, ==, dn->dn_object);
407 ASSERT3P(db->db_objset, ==, dn->dn_objset);
408 ASSERT3U(db->db_level, <, dn->dn_nlevels);
409 ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
410 db->db_blkid == DMU_SPILL_BLKID ||
411 !avl_is_empty(&dn->dn_dbufs));
413 if (db->db_blkid == DMU_BONUS_BLKID) {
415 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
416 ASSERT3U(db->db.db_offset, ==, DMU_BONUS_BLKID);
417 } else if (db->db_blkid == DMU_SPILL_BLKID) {
419 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
420 ASSERT0(db->db.db_offset);
422 ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size);
425 for (dr = db->db_data_pending; dr != NULL; dr = dr->dr_next)
426 ASSERT(dr->dr_dbuf == db);
428 for (dr = db->db_last_dirty; dr != NULL; dr = dr->dr_next)
429 ASSERT(dr->dr_dbuf == db);
432 * We can't assert that db_size matches dn_datablksz because it
433 * can be momentarily different when another thread is doing
436 if (db->db_level == 0 && db->db.db_object == DMU_META_DNODE_OBJECT) {
437 dr = db->db_data_pending;
439 * It should only be modified in syncing context, so
440 * make sure we only have one copy of the data.
442 ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf);
445 /* verify db->db_blkptr */
447 if (db->db_parent == dn->dn_dbuf) {
448 /* db is pointed to by the dnode */
449 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
450 if (DMU_OBJECT_IS_SPECIAL(db->db.db_object))
451 ASSERT(db->db_parent == NULL);
453 ASSERT(db->db_parent != NULL);
454 if (db->db_blkid != DMU_SPILL_BLKID)
455 ASSERT3P(db->db_blkptr, ==,
456 &dn->dn_phys->dn_blkptr[db->db_blkid]);
458 /* db is pointed to by an indirect block */
459 int epb = db->db_parent->db.db_size >> SPA_BLKPTRSHIFT;
460 ASSERT3U(db->db_parent->db_level, ==, db->db_level+1);
461 ASSERT3U(db->db_parent->db.db_object, ==,
464 * dnode_grow_indblksz() can make this fail if we don't
465 * have the struct_rwlock. XXX indblksz no longer
466 * grows. safe to do this now?
468 if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
469 ASSERT3P(db->db_blkptr, ==,
470 ((blkptr_t *)db->db_parent->db.db_data +
471 db->db_blkid % epb));
475 if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) &&
476 (db->db_buf == NULL || db->db_buf->b_data) &&
477 db->db.db_data && db->db_blkid != DMU_BONUS_BLKID &&
478 db->db_state != DB_FILL && !dn->dn_free_txg) {
480 * If the blkptr isn't set but they have nonzero data,
481 * it had better be dirty, otherwise we'll lose that
482 * data when we evict this buffer.
484 if (db->db_dirtycnt == 0) {
485 uint64_t *buf = db->db.db_data;
488 for (i = 0; i < db->db.db_size >> 3; i++) {
498 dbuf_clear_data(dmu_buf_impl_t *db)
500 ASSERT(MUTEX_HELD(&db->db_mtx));
503 db->db.db_data = NULL;
504 if (db->db_state != DB_NOFILL)
505 db->db_state = DB_UNCACHED;
509 dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf)
511 ASSERT(MUTEX_HELD(&db->db_mtx));
515 ASSERT(buf->b_data != NULL);
516 db->db.db_data = buf->b_data;
517 if (!arc_released(buf))
518 arc_set_callback(buf, dbuf_do_evict, db);
522 * Loan out an arc_buf for read. Return the loaned arc_buf.
525 dbuf_loan_arcbuf(dmu_buf_impl_t *db)
529 mutex_enter(&db->db_mtx);
530 if (arc_released(db->db_buf) || refcount_count(&db->db_holds) > 1) {
531 int blksz = db->db.db_size;
532 spa_t *spa = db->db_objset->os_spa;
534 mutex_exit(&db->db_mtx);
535 abuf = arc_loan_buf(spa, blksz);
536 bcopy(db->db.db_data, abuf->b_data, blksz);
539 arc_loan_inuse_buf(abuf, db);
541 mutex_exit(&db->db_mtx);
547 * Calculate which level n block references the data at the level 0 offset
551 dbuf_whichblock(dnode_t *dn, int64_t level, uint64_t offset)
553 if (dn->dn_datablkshift != 0 && dn->dn_indblkshift != 0) {
555 * The level n blkid is equal to the level 0 blkid divided by
556 * the number of level 0s in a level n block.
558 * The level 0 blkid is offset >> datablkshift =
559 * offset / 2^datablkshift.
561 * The number of level 0s in a level n is the number of block
562 * pointers in an indirect block, raised to the power of level.
563 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
564 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
566 * Thus, the level n blkid is: offset /
567 * ((2^datablkshift)*(2^(level*(indblkshift - SPA_BLKPTRSHIFT)))
568 * = offset / 2^(datablkshift + level *
569 * (indblkshift - SPA_BLKPTRSHIFT))
570 * = offset >> (datablkshift + level *
571 * (indblkshift - SPA_BLKPTRSHIFT))
573 return (offset >> (dn->dn_datablkshift + level *
574 (dn->dn_indblkshift - SPA_BLKPTRSHIFT)));
576 ASSERT3U(offset, <, dn->dn_datablksz);
582 dbuf_read_done(zio_t *zio, arc_buf_t *buf, void *vdb)
584 dmu_buf_impl_t *db = vdb;
586 mutex_enter(&db->db_mtx);
587 ASSERT3U(db->db_state, ==, DB_READ);
589 * All reads are synchronous, so we must have a hold on the dbuf
591 ASSERT(refcount_count(&db->db_holds) > 0);
592 ASSERT(db->db_buf == NULL);
593 ASSERT(db->db.db_data == NULL);
594 if (db->db_level == 0 && db->db_freed_in_flight) {
595 /* we were freed in flight; disregard any error */
596 arc_release(buf, db);
597 bzero(buf->b_data, db->db.db_size);
599 db->db_freed_in_flight = FALSE;
600 dbuf_set_data(db, buf);
601 db->db_state = DB_CACHED;
602 } else if (zio == NULL || zio->io_error == 0) {
603 dbuf_set_data(db, buf);
604 db->db_state = DB_CACHED;
606 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
607 ASSERT3P(db->db_buf, ==, NULL);
608 VERIFY(arc_buf_remove_ref(buf, db));
609 db->db_state = DB_UNCACHED;
611 cv_broadcast(&db->db_changed);
612 dbuf_rele_and_unlock(db, NULL);
616 dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t *flags)
620 arc_flags_t aflags = ARC_FLAG_NOWAIT;
624 ASSERT(!refcount_is_zero(&db->db_holds));
625 /* We need the struct_rwlock to prevent db_blkptr from changing. */
626 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
627 ASSERT(MUTEX_HELD(&db->db_mtx));
628 ASSERT(db->db_state == DB_UNCACHED);
629 ASSERT(db->db_buf == NULL);
631 if (db->db_blkid == DMU_BONUS_BLKID) {
632 int bonuslen = MIN(dn->dn_bonuslen, dn->dn_phys->dn_bonuslen);
634 ASSERT3U(bonuslen, <=, db->db.db_size);
635 db->db.db_data = zio_buf_alloc(DN_MAX_BONUSLEN);
636 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
637 if (bonuslen < DN_MAX_BONUSLEN)
638 bzero(db->db.db_data, DN_MAX_BONUSLEN);
640 bcopy(DN_BONUS(dn->dn_phys), db->db.db_data, bonuslen);
642 db->db_state = DB_CACHED;
643 mutex_exit(&db->db_mtx);
648 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
649 * processes the delete record and clears the bp while we are waiting
650 * for the dn_mtx (resulting in a "no" from block_freed).
652 if (db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr) ||
653 (db->db_level == 0 && (dnode_block_freed(dn, db->db_blkid) ||
654 BP_IS_HOLE(db->db_blkptr)))) {
655 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
658 dbuf_set_data(db, arc_buf_alloc(db->db_objset->os_spa,
659 db->db.db_size, db, type));
660 bzero(db->db.db_data, db->db.db_size);
661 db->db_state = DB_CACHED;
662 *flags |= DB_RF_CACHED;
663 mutex_exit(&db->db_mtx);
669 db->db_state = DB_READ;
670 mutex_exit(&db->db_mtx);
672 if (DBUF_IS_L2CACHEABLE(db))
673 aflags |= ARC_FLAG_L2CACHE;
674 if (DBUF_IS_L2COMPRESSIBLE(db))
675 aflags |= ARC_FLAG_L2COMPRESS;
677 SET_BOOKMARK(&zb, db->db_objset->os_dsl_dataset ?
678 db->db_objset->os_dsl_dataset->ds_object : DMU_META_OBJSET,
679 db->db.db_object, db->db_level, db->db_blkid);
681 dbuf_add_ref(db, NULL);
683 (void) arc_read(zio, db->db_objset->os_spa, db->db_blkptr,
684 dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ,
685 (*flags & DB_RF_CANFAIL) ? ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED,
687 if (aflags & ARC_FLAG_CACHED)
688 *flags |= DB_RF_CACHED;
692 dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
695 boolean_t havepzio = (zio != NULL);
700 * We don't have to hold the mutex to check db_state because it
701 * can't be freed while we have a hold on the buffer.
703 ASSERT(!refcount_is_zero(&db->db_holds));
705 if (db->db_state == DB_NOFILL)
706 return (SET_ERROR(EIO));
710 if ((flags & DB_RF_HAVESTRUCT) == 0)
711 rw_enter(&dn->dn_struct_rwlock, RW_READER);
713 prefetch = db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
714 (flags & DB_RF_NOPREFETCH) == 0 && dn != NULL &&
715 DBUF_IS_CACHEABLE(db);
717 mutex_enter(&db->db_mtx);
718 if (db->db_state == DB_CACHED) {
719 mutex_exit(&db->db_mtx);
721 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
722 db->db.db_size, TRUE);
723 if ((flags & DB_RF_HAVESTRUCT) == 0)
724 rw_exit(&dn->dn_struct_rwlock);
726 } else if (db->db_state == DB_UNCACHED) {
727 spa_t *spa = dn->dn_objset->os_spa;
730 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
731 dbuf_read_impl(db, zio, &flags);
733 /* dbuf_read_impl has dropped db_mtx for us */
736 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
737 db->db.db_size, flags & DB_RF_CACHED);
739 if ((flags & DB_RF_HAVESTRUCT) == 0)
740 rw_exit(&dn->dn_struct_rwlock);
747 * Another reader came in while the dbuf was in flight
748 * between UNCACHED and CACHED. Either a writer will finish
749 * writing the buffer (sending the dbuf to CACHED) or the
750 * first reader's request will reach the read_done callback
751 * and send the dbuf to CACHED. Otherwise, a failure
752 * occurred and the dbuf went to UNCACHED.
754 mutex_exit(&db->db_mtx);
756 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
757 db->db.db_size, TRUE);
758 if ((flags & DB_RF_HAVESTRUCT) == 0)
759 rw_exit(&dn->dn_struct_rwlock);
762 /* Skip the wait per the caller's request. */
763 mutex_enter(&db->db_mtx);
764 if ((flags & DB_RF_NEVERWAIT) == 0) {
765 while (db->db_state == DB_READ ||
766 db->db_state == DB_FILL) {
767 ASSERT(db->db_state == DB_READ ||
768 (flags & DB_RF_HAVESTRUCT) == 0);
769 DTRACE_PROBE2(blocked__read, dmu_buf_impl_t *,
771 cv_wait(&db->db_changed, &db->db_mtx);
773 if (db->db_state == DB_UNCACHED)
774 err = SET_ERROR(EIO);
776 mutex_exit(&db->db_mtx);
779 ASSERT(err || havepzio || db->db_state == DB_CACHED);
784 dbuf_noread(dmu_buf_impl_t *db)
786 ASSERT(!refcount_is_zero(&db->db_holds));
787 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
788 mutex_enter(&db->db_mtx);
789 while (db->db_state == DB_READ || db->db_state == DB_FILL)
790 cv_wait(&db->db_changed, &db->db_mtx);
791 if (db->db_state == DB_UNCACHED) {
792 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
793 spa_t *spa = db->db_objset->os_spa;
795 ASSERT(db->db_buf == NULL);
796 ASSERT(db->db.db_data == NULL);
797 dbuf_set_data(db, arc_buf_alloc(spa, db->db.db_size, db, type));
798 db->db_state = DB_FILL;
799 } else if (db->db_state == DB_NOFILL) {
802 ASSERT3U(db->db_state, ==, DB_CACHED);
804 mutex_exit(&db->db_mtx);
808 * This is our just-in-time copy function. It makes a copy of
809 * buffers, that have been modified in a previous transaction
810 * group, before we modify them in the current active group.
812 * This function is used in two places: when we are dirtying a
813 * buffer for the first time in a txg, and when we are freeing
814 * a range in a dnode that includes this buffer.
816 * Note that when we are called from dbuf_free_range() we do
817 * not put a hold on the buffer, we just traverse the active
818 * dbuf list for the dnode.
821 dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg)
823 dbuf_dirty_record_t *dr = db->db_last_dirty;
825 ASSERT(MUTEX_HELD(&db->db_mtx));
826 ASSERT(db->db.db_data != NULL);
827 ASSERT(db->db_level == 0);
828 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT);
831 (dr->dt.dl.dr_data !=
832 ((db->db_blkid == DMU_BONUS_BLKID) ? db->db.db_data : db->db_buf)))
836 * If the last dirty record for this dbuf has not yet synced
837 * and its referencing the dbuf data, either:
838 * reset the reference to point to a new copy,
839 * or (if there a no active holders)
840 * just null out the current db_data pointer.
842 ASSERT(dr->dr_txg >= txg - 2);
843 if (db->db_blkid == DMU_BONUS_BLKID) {
844 /* Note that the data bufs here are zio_bufs */
845 dr->dt.dl.dr_data = zio_buf_alloc(DN_MAX_BONUSLEN);
846 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
847 bcopy(db->db.db_data, dr->dt.dl.dr_data, DN_MAX_BONUSLEN);
848 } else if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
849 int size = db->db.db_size;
850 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
851 spa_t *spa = db->db_objset->os_spa;
853 dr->dt.dl.dr_data = arc_buf_alloc(spa, size, db, type);
854 bcopy(db->db.db_data, dr->dt.dl.dr_data->b_data, size);
861 dbuf_unoverride(dbuf_dirty_record_t *dr)
863 dmu_buf_impl_t *db = dr->dr_dbuf;
864 blkptr_t *bp = &dr->dt.dl.dr_overridden_by;
865 uint64_t txg = dr->dr_txg;
867 ASSERT(MUTEX_HELD(&db->db_mtx));
868 ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC);
869 ASSERT(db->db_level == 0);
871 if (db->db_blkid == DMU_BONUS_BLKID ||
872 dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN)
875 ASSERT(db->db_data_pending != dr);
877 /* free this block */
878 if (!BP_IS_HOLE(bp) && !dr->dt.dl.dr_nopwrite)
879 zio_free(db->db_objset->os_spa, txg, bp);
881 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
882 dr->dt.dl.dr_nopwrite = B_FALSE;
885 * Release the already-written buffer, so we leave it in
886 * a consistent dirty state. Note that all callers are
887 * modifying the buffer, so they will immediately do
888 * another (redundant) arc_release(). Therefore, leave
889 * the buf thawed to save the effort of freezing &
890 * immediately re-thawing it.
892 arc_release(dr->dt.dl.dr_data, db);
896 * Evict (if its unreferenced) or clear (if its referenced) any level-0
897 * data blocks in the free range, so that any future readers will find
900 * This is a no-op if the dataset is in the middle of an incremental
901 * receive; see comment below for details.
904 dbuf_free_range(dnode_t *dn, uint64_t start_blkid, uint64_t end_blkid,
907 dmu_buf_impl_t db_search;
908 dmu_buf_impl_t *db, *db_next;
909 uint64_t txg = tx->tx_txg;
912 if (end_blkid > dn->dn_maxblkid && (end_blkid != DMU_SPILL_BLKID))
913 end_blkid = dn->dn_maxblkid;
914 dprintf_dnode(dn, "start=%llu end=%llu\n", start_blkid, end_blkid);
916 db_search.db_level = 0;
917 db_search.db_blkid = start_blkid;
918 db_search.db_state = DB_SEARCH;
920 mutex_enter(&dn->dn_dbufs_mtx);
921 if (start_blkid >= dn->dn_unlisted_l0_blkid) {
922 /* There can't be any dbufs in this range; no need to search. */
924 db = avl_find(&dn->dn_dbufs, &db_search, &where);
925 ASSERT3P(db, ==, NULL);
926 db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER);
927 ASSERT(db == NULL || db->db_level > 0);
929 mutex_exit(&dn->dn_dbufs_mtx);
931 } else if (dmu_objset_is_receiving(dn->dn_objset)) {
933 * If we are receiving, we expect there to be no dbufs in
934 * the range to be freed, because receive modifies each
935 * block at most once, and in offset order. If this is
936 * not the case, it can lead to performance problems,
937 * so note that we unexpectedly took the slow path.
939 atomic_inc_64(&zfs_free_range_recv_miss);
942 db = avl_find(&dn->dn_dbufs, &db_search, &where);
943 ASSERT3P(db, ==, NULL);
944 db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER);
946 for (; db != NULL; db = db_next) {
947 db_next = AVL_NEXT(&dn->dn_dbufs, db);
948 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
950 if (db->db_level != 0 || db->db_blkid > end_blkid) {
953 ASSERT3U(db->db_blkid, >=, start_blkid);
955 /* found a level 0 buffer in the range */
956 mutex_enter(&db->db_mtx);
957 if (dbuf_undirty(db, tx)) {
958 /* mutex has been dropped and dbuf destroyed */
962 if (db->db_state == DB_UNCACHED ||
963 db->db_state == DB_NOFILL ||
964 db->db_state == DB_EVICTING) {
965 ASSERT(db->db.db_data == NULL);
966 mutex_exit(&db->db_mtx);
969 if (db->db_state == DB_READ || db->db_state == DB_FILL) {
970 /* will be handled in dbuf_read_done or dbuf_rele */
971 db->db_freed_in_flight = TRUE;
972 mutex_exit(&db->db_mtx);
975 if (refcount_count(&db->db_holds) == 0) {
980 /* The dbuf is referenced */
982 if (db->db_last_dirty != NULL) {
983 dbuf_dirty_record_t *dr = db->db_last_dirty;
985 if (dr->dr_txg == txg) {
987 * This buffer is "in-use", re-adjust the file
988 * size to reflect that this buffer may
989 * contain new data when we sync.
991 if (db->db_blkid != DMU_SPILL_BLKID &&
992 db->db_blkid > dn->dn_maxblkid)
993 dn->dn_maxblkid = db->db_blkid;
997 * This dbuf is not dirty in the open context.
998 * Either uncache it (if its not referenced in
999 * the open context) or reset its contents to
1002 dbuf_fix_old_data(db, txg);
1005 /* clear the contents if its cached */
1006 if (db->db_state == DB_CACHED) {
1007 ASSERT(db->db.db_data != NULL);
1008 arc_release(db->db_buf, db);
1009 bzero(db->db.db_data, db->db.db_size);
1010 arc_buf_freeze(db->db_buf);
1013 mutex_exit(&db->db_mtx);
1015 mutex_exit(&dn->dn_dbufs_mtx);
1019 dbuf_block_freeable(dmu_buf_impl_t *db)
1021 dsl_dataset_t *ds = db->db_objset->os_dsl_dataset;
1022 uint64_t birth_txg = 0;
1025 * We don't need any locking to protect db_blkptr:
1026 * If it's syncing, then db_last_dirty will be set
1027 * so we'll ignore db_blkptr.
1029 * This logic ensures that only block births for
1030 * filled blocks are considered.
1032 ASSERT(MUTEX_HELD(&db->db_mtx));
1033 if (db->db_last_dirty && (db->db_blkptr == NULL ||
1034 !BP_IS_HOLE(db->db_blkptr))) {
1035 birth_txg = db->db_last_dirty->dr_txg;
1036 } else if (db->db_blkptr != NULL && !BP_IS_HOLE(db->db_blkptr)) {
1037 birth_txg = db->db_blkptr->blk_birth;
1041 * If this block don't exist or is in a snapshot, it can't be freed.
1042 * Don't pass the bp to dsl_dataset_block_freeable() since we
1043 * are holding the db_mtx lock and might deadlock if we are
1044 * prefetching a dedup-ed block.
1047 return (ds == NULL ||
1048 dsl_dataset_block_freeable(ds, NULL, birth_txg));
1054 dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx)
1056 arc_buf_t *buf, *obuf;
1057 int osize = db->db.db_size;
1058 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1061 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1066 /* XXX does *this* func really need the lock? */
1067 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
1070 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
1071 * is OK, because there can be no other references to the db
1072 * when we are changing its size, so no concurrent DB_FILL can
1076 * XXX we should be doing a dbuf_read, checking the return
1077 * value and returning that up to our callers
1079 dmu_buf_will_dirty(&db->db, tx);
1081 /* create the data buffer for the new block */
1082 buf = arc_buf_alloc(dn->dn_objset->os_spa, size, db, type);
1084 /* copy old block data to the new block */
1086 bcopy(obuf->b_data, buf->b_data, MIN(osize, size));
1087 /* zero the remainder */
1089 bzero((uint8_t *)buf->b_data + osize, size - osize);
1091 mutex_enter(&db->db_mtx);
1092 dbuf_set_data(db, buf);
1093 VERIFY(arc_buf_remove_ref(obuf, db));
1094 db->db.db_size = size;
1096 if (db->db_level == 0) {
1097 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
1098 db->db_last_dirty->dt.dl.dr_data = buf;
1100 mutex_exit(&db->db_mtx);
1102 dnode_willuse_space(dn, size-osize, tx);
1107 dbuf_release_bp(dmu_buf_impl_t *db)
1109 objset_t *os = db->db_objset;
1111 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
1112 ASSERT(arc_released(os->os_phys_buf) ||
1113 list_link_active(&os->os_dsl_dataset->ds_synced_link));
1114 ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf));
1116 (void) arc_release(db->db_buf, db);
1119 dbuf_dirty_record_t *
1120 dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1124 dbuf_dirty_record_t **drp, *dr;
1125 int drop_struct_lock = FALSE;
1126 boolean_t do_free_accounting = B_FALSE;
1127 int txgoff = tx->tx_txg & TXG_MASK;
1129 ASSERT(tx->tx_txg != 0);
1130 ASSERT(!refcount_is_zero(&db->db_holds));
1131 DMU_TX_DIRTY_BUF(tx, db);
1136 * Shouldn't dirty a regular buffer in syncing context. Private
1137 * objects may be dirtied in syncing context, but only if they
1138 * were already pre-dirtied in open context.
1140 ASSERT(!dmu_tx_is_syncing(tx) ||
1141 BP_IS_HOLE(dn->dn_objset->os_rootbp) ||
1142 DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1143 dn->dn_objset->os_dsl_dataset == NULL);
1145 * We make this assert for private objects as well, but after we
1146 * check if we're already dirty. They are allowed to re-dirty
1147 * in syncing context.
1149 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1150 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1151 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1153 mutex_enter(&db->db_mtx);
1155 * XXX make this true for indirects too? The problem is that
1156 * transactions created with dmu_tx_create_assigned() from
1157 * syncing context don't bother holding ahead.
1159 ASSERT(db->db_level != 0 ||
1160 db->db_state == DB_CACHED || db->db_state == DB_FILL ||
1161 db->db_state == DB_NOFILL);
1163 mutex_enter(&dn->dn_mtx);
1165 * Don't set dirtyctx to SYNC if we're just modifying this as we
1166 * initialize the objset.
1168 if (dn->dn_dirtyctx == DN_UNDIRTIED &&
1169 !BP_IS_HOLE(dn->dn_objset->os_rootbp)) {
1171 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN);
1172 ASSERT(dn->dn_dirtyctx_firstset == NULL);
1173 dn->dn_dirtyctx_firstset = kmem_alloc(1, KM_SLEEP);
1175 mutex_exit(&dn->dn_mtx);
1177 if (db->db_blkid == DMU_SPILL_BLKID)
1178 dn->dn_have_spill = B_TRUE;
1181 * If this buffer is already dirty, we're done.
1183 drp = &db->db_last_dirty;
1184 ASSERT(*drp == NULL || (*drp)->dr_txg <= tx->tx_txg ||
1185 db->db.db_object == DMU_META_DNODE_OBJECT);
1186 while ((dr = *drp) != NULL && dr->dr_txg > tx->tx_txg)
1188 if (dr && dr->dr_txg == tx->tx_txg) {
1191 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID) {
1193 * If this buffer has already been written out,
1194 * we now need to reset its state.
1196 dbuf_unoverride(dr);
1197 if (db->db.db_object != DMU_META_DNODE_OBJECT &&
1198 db->db_state != DB_NOFILL)
1199 arc_buf_thaw(db->db_buf);
1201 mutex_exit(&db->db_mtx);
1206 * Only valid if not already dirty.
1208 ASSERT(dn->dn_object == 0 ||
1209 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1210 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1212 ASSERT3U(dn->dn_nlevels, >, db->db_level);
1213 ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) ||
1214 dn->dn_phys->dn_nlevels > db->db_level ||
1215 dn->dn_next_nlevels[txgoff] > db->db_level ||
1216 dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level ||
1217 dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level);
1220 * We should only be dirtying in syncing context if it's the
1221 * mos or we're initializing the os or it's a special object.
1222 * However, we are allowed to dirty in syncing context provided
1223 * we already dirtied it in open context. Hence we must make
1224 * this assertion only if we're not already dirty.
1227 ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1228 os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp));
1229 ASSERT(db->db.db_size != 0);
1231 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1233 if (db->db_blkid != DMU_BONUS_BLKID) {
1235 * Update the accounting.
1236 * Note: we delay "free accounting" until after we drop
1237 * the db_mtx. This keeps us from grabbing other locks
1238 * (and possibly deadlocking) in bp_get_dsize() while
1239 * also holding the db_mtx.
1241 dnode_willuse_space(dn, db->db.db_size, tx);
1242 do_free_accounting = dbuf_block_freeable(db);
1246 * If this buffer is dirty in an old transaction group we need
1247 * to make a copy of it so that the changes we make in this
1248 * transaction group won't leak out when we sync the older txg.
1250 dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP);
1251 if (db->db_level == 0) {
1252 void *data_old = db->db_buf;
1254 if (db->db_state != DB_NOFILL) {
1255 if (db->db_blkid == DMU_BONUS_BLKID) {
1256 dbuf_fix_old_data(db, tx->tx_txg);
1257 data_old = db->db.db_data;
1258 } else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
1260 * Release the data buffer from the cache so
1261 * that we can modify it without impacting
1262 * possible other users of this cached data
1263 * block. Note that indirect blocks and
1264 * private objects are not released until the
1265 * syncing state (since they are only modified
1268 arc_release(db->db_buf, db);
1269 dbuf_fix_old_data(db, tx->tx_txg);
1270 data_old = db->db_buf;
1272 ASSERT(data_old != NULL);
1274 dr->dt.dl.dr_data = data_old;
1276 mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_DEFAULT, NULL);
1277 list_create(&dr->dt.di.dr_children,
1278 sizeof (dbuf_dirty_record_t),
1279 offsetof(dbuf_dirty_record_t, dr_dirty_node));
1281 if (db->db_blkid != DMU_BONUS_BLKID && os->os_dsl_dataset != NULL)
1282 dr->dr_accounted = db->db.db_size;
1284 dr->dr_txg = tx->tx_txg;
1289 * We could have been freed_in_flight between the dbuf_noread
1290 * and dbuf_dirty. We win, as though the dbuf_noread() had
1291 * happened after the free.
1293 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1294 db->db_blkid != DMU_SPILL_BLKID) {
1295 mutex_enter(&dn->dn_mtx);
1296 if (dn->dn_free_ranges[txgoff] != NULL) {
1297 range_tree_clear(dn->dn_free_ranges[txgoff],
1300 mutex_exit(&dn->dn_mtx);
1301 db->db_freed_in_flight = FALSE;
1305 * This buffer is now part of this txg
1307 dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg);
1308 db->db_dirtycnt += 1;
1309 ASSERT3U(db->db_dirtycnt, <=, 3);
1311 mutex_exit(&db->db_mtx);
1313 if (db->db_blkid == DMU_BONUS_BLKID ||
1314 db->db_blkid == DMU_SPILL_BLKID) {
1315 mutex_enter(&dn->dn_mtx);
1316 ASSERT(!list_link_active(&dr->dr_dirty_node));
1317 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1318 mutex_exit(&dn->dn_mtx);
1319 dnode_setdirty(dn, tx);
1322 } else if (do_free_accounting) {
1323 blkptr_t *bp = db->db_blkptr;
1324 int64_t willfree = (bp && !BP_IS_HOLE(bp)) ?
1325 bp_get_dsize(os->os_spa, bp) : db->db.db_size;
1327 * This is only a guess -- if the dbuf is dirty
1328 * in a previous txg, we don't know how much
1329 * space it will use on disk yet. We should
1330 * really have the struct_rwlock to access
1331 * db_blkptr, but since this is just a guess,
1332 * it's OK if we get an odd answer.
1334 ddt_prefetch(os->os_spa, bp);
1335 dnode_willuse_space(dn, -willfree, tx);
1338 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
1339 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1340 drop_struct_lock = TRUE;
1343 if (db->db_level == 0) {
1344 dnode_new_blkid(dn, db->db_blkid, tx, drop_struct_lock);
1345 ASSERT(dn->dn_maxblkid >= db->db_blkid);
1348 if (db->db_level+1 < dn->dn_nlevels) {
1349 dmu_buf_impl_t *parent = db->db_parent;
1350 dbuf_dirty_record_t *di;
1351 int parent_held = FALSE;
1353 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
1354 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1356 parent = dbuf_hold_level(dn, db->db_level+1,
1357 db->db_blkid >> epbs, FTAG);
1358 ASSERT(parent != NULL);
1361 if (drop_struct_lock)
1362 rw_exit(&dn->dn_struct_rwlock);
1363 ASSERT3U(db->db_level+1, ==, parent->db_level);
1364 di = dbuf_dirty(parent, tx);
1366 dbuf_rele(parent, FTAG);
1368 mutex_enter(&db->db_mtx);
1370 * Since we've dropped the mutex, it's possible that
1371 * dbuf_undirty() might have changed this out from under us.
1373 if (db->db_last_dirty == dr ||
1374 dn->dn_object == DMU_META_DNODE_OBJECT) {
1375 mutex_enter(&di->dt.di.dr_mtx);
1376 ASSERT3U(di->dr_txg, ==, tx->tx_txg);
1377 ASSERT(!list_link_active(&dr->dr_dirty_node));
1378 list_insert_tail(&di->dt.di.dr_children, dr);
1379 mutex_exit(&di->dt.di.dr_mtx);
1382 mutex_exit(&db->db_mtx);
1384 ASSERT(db->db_level+1 == dn->dn_nlevels);
1385 ASSERT(db->db_blkid < dn->dn_nblkptr);
1386 ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf);
1387 mutex_enter(&dn->dn_mtx);
1388 ASSERT(!list_link_active(&dr->dr_dirty_node));
1389 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1390 mutex_exit(&dn->dn_mtx);
1391 if (drop_struct_lock)
1392 rw_exit(&dn->dn_struct_rwlock);
1395 dnode_setdirty(dn, tx);
1401 * Undirty a buffer in the transaction group referenced by the given
1402 * transaction. Return whether this evicted the dbuf.
1405 dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1408 uint64_t txg = tx->tx_txg;
1409 dbuf_dirty_record_t *dr, **drp;
1414 * Due to our use of dn_nlevels below, this can only be called
1415 * in open context, unless we are operating on the MOS.
1416 * From syncing context, dn_nlevels may be different from the
1417 * dn_nlevels used when dbuf was dirtied.
1419 ASSERT(db->db_objset ==
1420 dmu_objset_pool(db->db_objset)->dp_meta_objset ||
1421 txg != spa_syncing_txg(dmu_objset_spa(db->db_objset)));
1422 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1423 ASSERT0(db->db_level);
1424 ASSERT(MUTEX_HELD(&db->db_mtx));
1427 * If this buffer is not dirty, we're done.
1429 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1430 if (dr->dr_txg <= txg)
1432 if (dr == NULL || dr->dr_txg < txg)
1434 ASSERT(dr->dr_txg == txg);
1435 ASSERT(dr->dr_dbuf == db);
1440 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1442 ASSERT(db->db.db_size != 0);
1444 dsl_pool_undirty_space(dmu_objset_pool(dn->dn_objset),
1445 dr->dr_accounted, txg);
1450 * Note that there are three places in dbuf_dirty()
1451 * where this dirty record may be put on a list.
1452 * Make sure to do a list_remove corresponding to
1453 * every one of those list_insert calls.
1455 if (dr->dr_parent) {
1456 mutex_enter(&dr->dr_parent->dt.di.dr_mtx);
1457 list_remove(&dr->dr_parent->dt.di.dr_children, dr);
1458 mutex_exit(&dr->dr_parent->dt.di.dr_mtx);
1459 } else if (db->db_blkid == DMU_SPILL_BLKID ||
1460 db->db_level + 1 == dn->dn_nlevels) {
1461 ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf);
1462 mutex_enter(&dn->dn_mtx);
1463 list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr);
1464 mutex_exit(&dn->dn_mtx);
1468 if (db->db_state != DB_NOFILL) {
1469 dbuf_unoverride(dr);
1471 ASSERT(db->db_buf != NULL);
1472 ASSERT(dr->dt.dl.dr_data != NULL);
1473 if (dr->dt.dl.dr_data != db->db_buf)
1474 VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data, db));
1477 kmem_free(dr, sizeof (dbuf_dirty_record_t));
1479 ASSERT(db->db_dirtycnt > 0);
1480 db->db_dirtycnt -= 1;
1482 if (refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) {
1483 arc_buf_t *buf = db->db_buf;
1485 ASSERT(db->db_state == DB_NOFILL || arc_released(buf));
1486 dbuf_clear_data(db);
1487 VERIFY(arc_buf_remove_ref(buf, db));
1496 dmu_buf_will_dirty(dmu_buf_t *db_fake, dmu_tx_t *tx)
1498 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1499 int rf = DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH;
1501 ASSERT(tx->tx_txg != 0);
1502 ASSERT(!refcount_is_zero(&db->db_holds));
1505 if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock))
1506 rf |= DB_RF_HAVESTRUCT;
1508 (void) dbuf_read(db, NULL, rf);
1509 (void) dbuf_dirty(db, tx);
1513 dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1515 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1517 db->db_state = DB_NOFILL;
1519 dmu_buf_will_fill(db_fake, tx);
1523 dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1525 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1527 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1528 ASSERT(tx->tx_txg != 0);
1529 ASSERT(db->db_level == 0);
1530 ASSERT(!refcount_is_zero(&db->db_holds));
1532 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
1533 dmu_tx_private_ok(tx));
1536 (void) dbuf_dirty(db, tx);
1539 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1542 dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx)
1544 mutex_enter(&db->db_mtx);
1547 if (db->db_state == DB_FILL) {
1548 if (db->db_level == 0 && db->db_freed_in_flight) {
1549 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1550 /* we were freed while filling */
1551 /* XXX dbuf_undirty? */
1552 bzero(db->db.db_data, db->db.db_size);
1553 db->db_freed_in_flight = FALSE;
1555 db->db_state = DB_CACHED;
1556 cv_broadcast(&db->db_changed);
1558 mutex_exit(&db->db_mtx);
1562 dmu_buf_write_embedded(dmu_buf_t *dbuf, void *data,
1563 bp_embedded_type_t etype, enum zio_compress comp,
1564 int uncompressed_size, int compressed_size, int byteorder,
1567 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
1568 struct dirty_leaf *dl;
1569 dmu_object_type_t type;
1571 if (etype == BP_EMBEDDED_TYPE_DATA) {
1572 ASSERT(spa_feature_is_active(dmu_objset_spa(db->db_objset),
1573 SPA_FEATURE_EMBEDDED_DATA));
1577 type = DB_DNODE(db)->dn_type;
1580 ASSERT0(db->db_level);
1581 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1583 dmu_buf_will_not_fill(dbuf, tx);
1585 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
1586 dl = &db->db_last_dirty->dt.dl;
1587 encode_embedded_bp_compressed(&dl->dr_overridden_by,
1588 data, comp, uncompressed_size, compressed_size);
1589 BPE_SET_ETYPE(&dl->dr_overridden_by, etype);
1590 BP_SET_TYPE(&dl->dr_overridden_by, type);
1591 BP_SET_LEVEL(&dl->dr_overridden_by, 0);
1592 BP_SET_BYTEORDER(&dl->dr_overridden_by, byteorder);
1594 dl->dr_override_state = DR_OVERRIDDEN;
1595 dl->dr_overridden_by.blk_birth = db->db_last_dirty->dr_txg;
1599 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1600 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1603 dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx)
1605 ASSERT(!refcount_is_zero(&db->db_holds));
1606 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1607 ASSERT(db->db_level == 0);
1608 ASSERT(DBUF_GET_BUFC_TYPE(db) == ARC_BUFC_DATA);
1609 ASSERT(buf != NULL);
1610 ASSERT(arc_buf_size(buf) == db->db.db_size);
1611 ASSERT(tx->tx_txg != 0);
1613 arc_return_buf(buf, db);
1614 ASSERT(arc_released(buf));
1616 mutex_enter(&db->db_mtx);
1618 while (db->db_state == DB_READ || db->db_state == DB_FILL)
1619 cv_wait(&db->db_changed, &db->db_mtx);
1621 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED);
1623 if (db->db_state == DB_CACHED &&
1624 refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) {
1625 mutex_exit(&db->db_mtx);
1626 (void) dbuf_dirty(db, tx);
1627 bcopy(buf->b_data, db->db.db_data, db->db.db_size);
1628 VERIFY(arc_buf_remove_ref(buf, db));
1629 xuio_stat_wbuf_copied();
1633 xuio_stat_wbuf_nocopy();
1634 if (db->db_state == DB_CACHED) {
1635 dbuf_dirty_record_t *dr = db->db_last_dirty;
1637 ASSERT(db->db_buf != NULL);
1638 if (dr != NULL && dr->dr_txg == tx->tx_txg) {
1639 ASSERT(dr->dt.dl.dr_data == db->db_buf);
1640 if (!arc_released(db->db_buf)) {
1641 ASSERT(dr->dt.dl.dr_override_state ==
1643 arc_release(db->db_buf, db);
1645 dr->dt.dl.dr_data = buf;
1646 VERIFY(arc_buf_remove_ref(db->db_buf, db));
1647 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) {
1648 arc_release(db->db_buf, db);
1649 VERIFY(arc_buf_remove_ref(db->db_buf, db));
1653 ASSERT(db->db_buf == NULL);
1654 dbuf_set_data(db, buf);
1655 db->db_state = DB_FILL;
1656 mutex_exit(&db->db_mtx);
1657 (void) dbuf_dirty(db, tx);
1658 dmu_buf_fill_done(&db->db, tx);
1662 * "Clear" the contents of this dbuf. This will mark the dbuf
1663 * EVICTING and clear *most* of its references. Unfortunately,
1664 * when we are not holding the dn_dbufs_mtx, we can't clear the
1665 * entry in the dn_dbufs list. We have to wait until dbuf_destroy()
1666 * in this case. For callers from the DMU we will usually see:
1667 * dbuf_clear()->arc_clear_callback()->dbuf_do_evict()->dbuf_destroy()
1668 * For the arc callback, we will usually see:
1669 * dbuf_do_evict()->dbuf_clear();dbuf_destroy()
1670 * Sometimes, though, we will get a mix of these two:
1671 * DMU: dbuf_clear()->arc_clear_callback()
1672 * ARC: dbuf_do_evict()->dbuf_destroy()
1674 * This routine will dissociate the dbuf from the arc, by calling
1675 * arc_clear_callback(), but will not evict the data from the ARC.
1678 dbuf_clear(dmu_buf_impl_t *db)
1681 dmu_buf_impl_t *parent = db->db_parent;
1682 dmu_buf_impl_t *dndb;
1683 boolean_t dbuf_gone = B_FALSE;
1685 ASSERT(MUTEX_HELD(&db->db_mtx));
1686 ASSERT(refcount_is_zero(&db->db_holds));
1688 dbuf_evict_user(db);
1690 if (db->db_state == DB_CACHED) {
1691 ASSERT(db->db.db_data != NULL);
1692 if (db->db_blkid == DMU_BONUS_BLKID) {
1693 zio_buf_free(db->db.db_data, DN_MAX_BONUSLEN);
1694 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
1696 db->db.db_data = NULL;
1697 db->db_state = DB_UNCACHED;
1700 ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL);
1701 ASSERT(db->db_data_pending == NULL);
1703 db->db_state = DB_EVICTING;
1704 db->db_blkptr = NULL;
1709 if (db->db_blkid != DMU_BONUS_BLKID && MUTEX_HELD(&dn->dn_dbufs_mtx)) {
1710 avl_remove(&dn->dn_dbufs, db);
1711 atomic_dec_32(&dn->dn_dbufs_count);
1715 * Decrementing the dbuf count means that the hold corresponding
1716 * to the removed dbuf is no longer discounted in dnode_move(),
1717 * so the dnode cannot be moved until after we release the hold.
1718 * The membar_producer() ensures visibility of the decremented
1719 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
1723 db->db_dnode_handle = NULL;
1729 dbuf_gone = arc_clear_callback(db->db_buf);
1732 mutex_exit(&db->db_mtx);
1735 * If this dbuf is referenced from an indirect dbuf,
1736 * decrement the ref count on the indirect dbuf.
1738 if (parent && parent != dndb)
1739 dbuf_rele(parent, db);
1743 * Note: While bpp will always be updated if the function returns success,
1744 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
1745 * this happens when the dnode is the meta-dnode, or a userused or groupused
1749 dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse,
1750 dmu_buf_impl_t **parentp, blkptr_t **bpp)
1757 ASSERT(blkid != DMU_BONUS_BLKID);
1759 if (blkid == DMU_SPILL_BLKID) {
1760 mutex_enter(&dn->dn_mtx);
1761 if (dn->dn_have_spill &&
1762 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
1763 *bpp = &dn->dn_phys->dn_spill;
1766 dbuf_add_ref(dn->dn_dbuf, NULL);
1767 *parentp = dn->dn_dbuf;
1768 mutex_exit(&dn->dn_mtx);
1772 if (dn->dn_phys->dn_nlevels == 0)
1775 nlevels = dn->dn_phys->dn_nlevels;
1777 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1779 ASSERT3U(level * epbs, <, 64);
1780 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1781 if (level >= nlevels ||
1782 (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) {
1783 /* the buffer has no parent yet */
1784 return (SET_ERROR(ENOENT));
1785 } else if (level < nlevels-1) {
1786 /* this block is referenced from an indirect block */
1787 int err = dbuf_hold_impl(dn, level+1,
1788 blkid >> epbs, fail_sparse, FALSE, NULL, parentp);
1791 err = dbuf_read(*parentp, NULL,
1792 (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL));
1794 dbuf_rele(*parentp, NULL);
1798 *bpp = ((blkptr_t *)(*parentp)->db.db_data) +
1799 (blkid & ((1ULL << epbs) - 1));
1802 /* the block is referenced from the dnode */
1803 ASSERT3U(level, ==, nlevels-1);
1804 ASSERT(dn->dn_phys->dn_nblkptr == 0 ||
1805 blkid < dn->dn_phys->dn_nblkptr);
1807 dbuf_add_ref(dn->dn_dbuf, NULL);
1808 *parentp = dn->dn_dbuf;
1810 *bpp = &dn->dn_phys->dn_blkptr[blkid];
1815 static dmu_buf_impl_t *
1816 dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid,
1817 dmu_buf_impl_t *parent, blkptr_t *blkptr)
1819 objset_t *os = dn->dn_objset;
1820 dmu_buf_impl_t *db, *odb;
1822 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1823 ASSERT(dn->dn_type != DMU_OT_NONE);
1825 db = kmem_cache_alloc(dbuf_cache, KM_SLEEP);
1828 db->db.db_object = dn->dn_object;
1829 db->db_level = level;
1830 db->db_blkid = blkid;
1831 db->db_last_dirty = NULL;
1832 db->db_dirtycnt = 0;
1833 db->db_dnode_handle = dn->dn_handle;
1834 db->db_parent = parent;
1835 db->db_blkptr = blkptr;
1838 db->db_immediate_evict = 0;
1839 db->db_freed_in_flight = 0;
1841 if (blkid == DMU_BONUS_BLKID) {
1842 ASSERT3P(parent, ==, dn->dn_dbuf);
1843 db->db.db_size = DN_MAX_BONUSLEN -
1844 (dn->dn_nblkptr-1) * sizeof (blkptr_t);
1845 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
1846 db->db.db_offset = DMU_BONUS_BLKID;
1847 db->db_state = DB_UNCACHED;
1848 /* the bonus dbuf is not placed in the hash table */
1849 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1851 } else if (blkid == DMU_SPILL_BLKID) {
1852 db->db.db_size = (blkptr != NULL) ?
1853 BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE;
1854 db->db.db_offset = 0;
1857 db->db_level ? 1 << dn->dn_indblkshift : dn->dn_datablksz;
1858 db->db.db_size = blocksize;
1859 db->db.db_offset = db->db_blkid * blocksize;
1863 * Hold the dn_dbufs_mtx while we get the new dbuf
1864 * in the hash table *and* added to the dbufs list.
1865 * This prevents a possible deadlock with someone
1866 * trying to look up this dbuf before its added to the
1869 mutex_enter(&dn->dn_dbufs_mtx);
1870 db->db_state = DB_EVICTING;
1871 if ((odb = dbuf_hash_insert(db)) != NULL) {
1872 /* someone else inserted it first */
1873 kmem_cache_free(dbuf_cache, db);
1874 mutex_exit(&dn->dn_dbufs_mtx);
1877 avl_add(&dn->dn_dbufs, db);
1878 if (db->db_level == 0 && db->db_blkid >=
1879 dn->dn_unlisted_l0_blkid)
1880 dn->dn_unlisted_l0_blkid = db->db_blkid + 1;
1881 db->db_state = DB_UNCACHED;
1882 mutex_exit(&dn->dn_dbufs_mtx);
1883 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1885 if (parent && parent != dn->dn_dbuf)
1886 dbuf_add_ref(parent, db);
1888 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1889 refcount_count(&dn->dn_holds) > 0);
1890 (void) refcount_add(&dn->dn_holds, db);
1891 atomic_inc_32(&dn->dn_dbufs_count);
1893 dprintf_dbuf(db, "db=%p\n", db);
1899 dbuf_do_evict(void *private)
1901 dmu_buf_impl_t *db = private;
1903 if (!MUTEX_HELD(&db->db_mtx))
1904 mutex_enter(&db->db_mtx);
1906 ASSERT(refcount_is_zero(&db->db_holds));
1908 if (db->db_state != DB_EVICTING) {
1909 ASSERT(db->db_state == DB_CACHED);
1914 mutex_exit(&db->db_mtx);
1921 dbuf_destroy(dmu_buf_impl_t *db)
1923 ASSERT(refcount_is_zero(&db->db_holds));
1925 if (db->db_blkid != DMU_BONUS_BLKID) {
1927 * If this dbuf is still on the dn_dbufs list,
1928 * remove it from that list.
1930 if (db->db_dnode_handle != NULL) {
1935 mutex_enter(&dn->dn_dbufs_mtx);
1936 avl_remove(&dn->dn_dbufs, db);
1937 atomic_dec_32(&dn->dn_dbufs_count);
1938 mutex_exit(&dn->dn_dbufs_mtx);
1941 * Decrementing the dbuf count means that the hold
1942 * corresponding to the removed dbuf is no longer
1943 * discounted in dnode_move(), so the dnode cannot be
1944 * moved until after we release the hold.
1947 db->db_dnode_handle = NULL;
1949 dbuf_hash_remove(db);
1951 db->db_parent = NULL;
1954 ASSERT(db->db.db_data == NULL);
1955 ASSERT(db->db_hash_next == NULL);
1956 ASSERT(db->db_blkptr == NULL);
1957 ASSERT(db->db_data_pending == NULL);
1959 kmem_cache_free(dbuf_cache, db);
1960 arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1963 typedef struct dbuf_prefetch_arg {
1964 spa_t *dpa_spa; /* The spa to issue the prefetch in. */
1965 zbookmark_phys_t dpa_zb; /* The target block to prefetch. */
1966 int dpa_epbs; /* Entries (blkptr_t's) Per Block Shift. */
1967 int dpa_curlevel; /* The current level that we're reading */
1968 zio_priority_t dpa_prio; /* The priority I/Os should be issued at. */
1969 zio_t *dpa_zio; /* The parent zio_t for all prefetches. */
1970 arc_flags_t dpa_aflags; /* Flags to pass to the final prefetch. */
1971 } dbuf_prefetch_arg_t;
1974 * Actually issue the prefetch read for the block given.
1977 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t *dpa, blkptr_t *bp)
1979 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
1982 arc_flags_t aflags =
1983 dpa->dpa_aflags | ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH;
1985 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp));
1986 ASSERT3U(dpa->dpa_curlevel, ==, dpa->dpa_zb.zb_level);
1987 ASSERT(dpa->dpa_zio != NULL);
1988 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa, bp, NULL, NULL,
1989 dpa->dpa_prio, ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
1990 &aflags, &dpa->dpa_zb);
1994 * Called when an indirect block above our prefetch target is read in. This
1995 * will either read in the next indirect block down the tree or issue the actual
1996 * prefetch if the next block down is our target.
1999 dbuf_prefetch_indirect_done(zio_t *zio, arc_buf_t *abuf, void *private)
2001 dbuf_prefetch_arg_t *dpa = private;
2003 ASSERT3S(dpa->dpa_zb.zb_level, <, dpa->dpa_curlevel);
2004 ASSERT3S(dpa->dpa_curlevel, >, 0);
2006 ASSERT3S(BP_GET_LEVEL(zio->io_bp), ==, dpa->dpa_curlevel);
2007 ASSERT3U(BP_GET_LSIZE(zio->io_bp), ==, zio->io_size);
2008 ASSERT3P(zio->io_spa, ==, dpa->dpa_spa);
2011 dpa->dpa_curlevel--;
2013 uint64_t nextblkid = dpa->dpa_zb.zb_blkid >>
2014 (dpa->dpa_epbs * (dpa->dpa_curlevel - dpa->dpa_zb.zb_level));
2015 blkptr_t *bp = ((blkptr_t *)abuf->b_data) +
2016 P2PHASE(nextblkid, 1ULL << dpa->dpa_epbs);
2017 if (BP_IS_HOLE(bp) || (zio != NULL && zio->io_error != 0)) {
2018 kmem_free(dpa, sizeof (*dpa));
2019 } else if (dpa->dpa_curlevel == dpa->dpa_zb.zb_level) {
2020 ASSERT3U(nextblkid, ==, dpa->dpa_zb.zb_blkid);
2021 dbuf_issue_final_prefetch(dpa, bp);
2022 kmem_free(dpa, sizeof (*dpa));
2024 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT;
2025 zbookmark_phys_t zb;
2027 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp));
2029 SET_BOOKMARK(&zb, dpa->dpa_zb.zb_objset,
2030 dpa->dpa_zb.zb_object, dpa->dpa_curlevel, nextblkid);
2032 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa,
2033 bp, dbuf_prefetch_indirect_done, dpa, dpa->dpa_prio,
2034 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2037 (void) arc_buf_remove_ref(abuf, private);
2041 * Issue prefetch reads for the given block on the given level. If the indirect
2042 * blocks above that block are not in memory, we will read them in
2043 * asynchronously. As a result, this call never blocks waiting for a read to
2047 dbuf_prefetch(dnode_t *dn, int64_t level, uint64_t blkid, zio_priority_t prio,
2051 int epbs, nlevels, curlevel;
2054 ASSERT(blkid != DMU_BONUS_BLKID);
2055 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2057 if (dnode_block_freed(dn, blkid))
2061 * This dnode hasn't been written to disk yet, so there's nothing to
2064 nlevels = dn->dn_phys->dn_nlevels;
2065 if (level >= nlevels || dn->dn_phys->dn_nblkptr == 0)
2068 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2069 if (dn->dn_phys->dn_maxblkid < blkid << (epbs * level))
2072 dmu_buf_impl_t *db = dbuf_find(dn->dn_objset, dn->dn_object,
2075 mutex_exit(&db->db_mtx);
2077 * This dbuf already exists. It is either CACHED, or
2078 * (we assume) about to be read or filled.
2084 * Find the closest ancestor (indirect block) of the target block
2085 * that is present in the cache. In this indirect block, we will
2086 * find the bp that is at curlevel, curblkid.
2090 while (curlevel < nlevels - 1) {
2091 int parent_level = curlevel + 1;
2092 uint64_t parent_blkid = curblkid >> epbs;
2095 if (dbuf_hold_impl(dn, parent_level, parent_blkid,
2096 FALSE, TRUE, FTAG, &db) == 0) {
2097 blkptr_t *bpp = db->db_buf->b_data;
2098 bp = bpp[P2PHASE(curblkid, 1 << epbs)];
2099 dbuf_rele(db, FTAG);
2103 curlevel = parent_level;
2104 curblkid = parent_blkid;
2107 if (curlevel == nlevels - 1) {
2108 /* No cached indirect blocks found. */
2109 ASSERT3U(curblkid, <, dn->dn_phys->dn_nblkptr);
2110 bp = dn->dn_phys->dn_blkptr[curblkid];
2112 if (BP_IS_HOLE(&bp))
2115 ASSERT3U(curlevel, ==, BP_GET_LEVEL(&bp));
2117 zio_t *pio = zio_root(dmu_objset_spa(dn->dn_objset), NULL, NULL,
2120 dbuf_prefetch_arg_t *dpa = kmem_zalloc(sizeof (*dpa), KM_SLEEP);
2121 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
2122 SET_BOOKMARK(&dpa->dpa_zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET,
2123 dn->dn_object, level, blkid);
2124 dpa->dpa_curlevel = curlevel;
2125 dpa->dpa_prio = prio;
2126 dpa->dpa_aflags = aflags;
2127 dpa->dpa_spa = dn->dn_objset->os_spa;
2128 dpa->dpa_epbs = epbs;
2132 * If we have the indirect just above us, no need to do the asynchronous
2133 * prefetch chain; we'll just run the last step ourselves. If we're at
2134 * a higher level, though, we want to issue the prefetches for all the
2135 * indirect blocks asynchronously, so we can go on with whatever we were
2138 if (curlevel == level) {
2139 ASSERT3U(curblkid, ==, blkid);
2140 dbuf_issue_final_prefetch(dpa, &bp);
2141 kmem_free(dpa, sizeof (*dpa));
2143 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT;
2144 zbookmark_phys_t zb;
2146 SET_BOOKMARK(&zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET,
2147 dn->dn_object, curlevel, curblkid);
2148 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa,
2149 &bp, dbuf_prefetch_indirect_done, dpa, prio,
2150 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2154 * We use pio here instead of dpa_zio since it's possible that
2155 * dpa may have already been freed.
2161 * Returns with db_holds incremented, and db_mtx not held.
2162 * Note: dn_struct_rwlock must be held.
2165 dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid,
2166 boolean_t fail_sparse, boolean_t fail_uncached,
2167 void *tag, dmu_buf_impl_t **dbp)
2169 dmu_buf_impl_t *db, *parent = NULL;
2171 ASSERT(blkid != DMU_BONUS_BLKID);
2172 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2173 ASSERT3U(dn->dn_nlevels, >, level);
2177 /* dbuf_find() returns with db_mtx held */
2178 db = dbuf_find(dn->dn_objset, dn->dn_object, level, blkid);
2181 blkptr_t *bp = NULL;
2185 return (SET_ERROR(ENOENT));
2187 ASSERT3P(parent, ==, NULL);
2188 err = dbuf_findbp(dn, level, blkid, fail_sparse, &parent, &bp);
2190 if (err == 0 && bp && BP_IS_HOLE(bp))
2191 err = SET_ERROR(ENOENT);
2194 dbuf_rele(parent, NULL);
2198 if (err && err != ENOENT)
2200 db = dbuf_create(dn, level, blkid, parent, bp);
2203 if (fail_uncached && db->db_state != DB_CACHED) {
2204 mutex_exit(&db->db_mtx);
2205 return (SET_ERROR(ENOENT));
2208 if (db->db_buf && refcount_is_zero(&db->db_holds)) {
2209 arc_buf_add_ref(db->db_buf, db);
2210 if (db->db_buf->b_data == NULL) {
2213 dbuf_rele(parent, NULL);
2218 ASSERT3P(db->db.db_data, ==, db->db_buf->b_data);
2221 ASSERT(db->db_buf == NULL || arc_referenced(db->db_buf));
2224 * If this buffer is currently syncing out, and we are are
2225 * still referencing it from db_data, we need to make a copy
2226 * of it in case we decide we want to dirty it again in this txg.
2228 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
2229 dn->dn_object != DMU_META_DNODE_OBJECT &&
2230 db->db_state == DB_CACHED && db->db_data_pending) {
2231 dbuf_dirty_record_t *dr = db->db_data_pending;
2233 if (dr->dt.dl.dr_data == db->db_buf) {
2234 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
2237 arc_buf_alloc(dn->dn_objset->os_spa,
2238 db->db.db_size, db, type));
2239 bcopy(dr->dt.dl.dr_data->b_data, db->db.db_data,
2244 (void) refcount_add(&db->db_holds, tag);
2246 mutex_exit(&db->db_mtx);
2248 /* NOTE: we can't rele the parent until after we drop the db_mtx */
2250 dbuf_rele(parent, NULL);
2252 ASSERT3P(DB_DNODE(db), ==, dn);
2253 ASSERT3U(db->db_blkid, ==, blkid);
2254 ASSERT3U(db->db_level, ==, level);
2261 dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag)
2263 return (dbuf_hold_level(dn, 0, blkid, tag));
2267 dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag)
2270 int err = dbuf_hold_impl(dn, level, blkid, FALSE, FALSE, tag, &db);
2271 return (err ? NULL : db);
2275 dbuf_create_bonus(dnode_t *dn)
2277 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
2279 ASSERT(dn->dn_bonus == NULL);
2280 dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL);
2284 dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx)
2286 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2289 if (db->db_blkid != DMU_SPILL_BLKID)
2290 return (SET_ERROR(ENOTSUP));
2292 blksz = SPA_MINBLOCKSIZE;
2293 ASSERT3U(blksz, <=, spa_maxblocksize(dmu_objset_spa(db->db_objset)));
2294 blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE);
2298 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2299 dbuf_new_size(db, blksz, tx);
2300 rw_exit(&dn->dn_struct_rwlock);
2307 dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx)
2309 dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx);
2312 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2314 dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
2316 int64_t holds = refcount_add(&db->db_holds, tag);
2320 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
2322 dbuf_try_add_ref(dmu_buf_t *db_fake, objset_t *os, uint64_t obj, uint64_t blkid,
2325 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2326 dmu_buf_impl_t *found_db;
2327 boolean_t result = B_FALSE;
2329 if (db->db_blkid == DMU_BONUS_BLKID)
2330 found_db = dbuf_find_bonus(os, obj);
2332 found_db = dbuf_find(os, obj, 0, blkid);
2334 if (found_db != NULL) {
2335 if (db == found_db && dbuf_refcount(db) > db->db_dirtycnt) {
2336 (void) refcount_add(&db->db_holds, tag);
2339 mutex_exit(&db->db_mtx);
2345 * If you call dbuf_rele() you had better not be referencing the dnode handle
2346 * unless you have some other direct or indirect hold on the dnode. (An indirect
2347 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2348 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2349 * dnode's parent dbuf evicting its dnode handles.
2352 dbuf_rele(dmu_buf_impl_t *db, void *tag)
2354 mutex_enter(&db->db_mtx);
2355 dbuf_rele_and_unlock(db, tag);
2359 dmu_buf_rele(dmu_buf_t *db, void *tag)
2361 dbuf_rele((dmu_buf_impl_t *)db, tag);
2365 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2366 * db_dirtycnt and db_holds to be updated atomically.
2369 dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag)
2373 ASSERT(MUTEX_HELD(&db->db_mtx));
2377 * Remove the reference to the dbuf before removing its hold on the
2378 * dnode so we can guarantee in dnode_move() that a referenced bonus
2379 * buffer has a corresponding dnode hold.
2381 holds = refcount_remove(&db->db_holds, tag);
2385 * We can't freeze indirects if there is a possibility that they
2386 * may be modified in the current syncing context.
2388 if (db->db_buf && holds == (db->db_level == 0 ? db->db_dirtycnt : 0))
2389 arc_buf_freeze(db->db_buf);
2391 if (holds == db->db_dirtycnt &&
2392 db->db_level == 0 && db->db_immediate_evict)
2393 dbuf_evict_user(db);
2396 if (db->db_blkid == DMU_BONUS_BLKID) {
2400 * If the dnode moves here, we cannot cross this
2401 * barrier until the move completes.
2406 atomic_dec_32(&dn->dn_dbufs_count);
2409 * Decrementing the dbuf count means that the bonus
2410 * buffer's dnode hold is no longer discounted in
2411 * dnode_move(). The dnode cannot move until after
2412 * the dnode_rele_and_unlock() below.
2417 * Do not reference db after its lock is dropped.
2418 * Another thread may evict it.
2420 mutex_exit(&db->db_mtx);
2423 * If the dnode has been freed, evict the bonus
2424 * buffer immediately. The data in the bonus
2425 * buffer is no longer relevant and this prevents
2426 * a stale bonus buffer from being associated
2427 * with this dnode_t should the dnode_t be reused
2428 * prior to being destroyed.
2430 mutex_enter(&dn->dn_mtx);
2431 if (dn->dn_type == DMU_OT_NONE ||
2432 dn->dn_free_txg != 0) {
2434 * Drop dn_mtx. It is a leaf lock and
2435 * cannot be held when dnode_evict_bonus()
2436 * acquires other locks in order to
2437 * perform the eviction.
2439 * Freed dnodes cannot be reused until the
2440 * last hold is released. Since this bonus
2441 * buffer has a hold, the dnode will remain
2442 * in the free state, even without dn_mtx
2443 * held, until the dnode_rele_and_unlock()
2446 mutex_exit(&dn->dn_mtx);
2447 dnode_evict_bonus(dn);
2448 mutex_enter(&dn->dn_mtx);
2450 dnode_rele_and_unlock(dn, db);
2451 } else if (db->db_buf == NULL) {
2453 * This is a special case: we never associated this
2454 * dbuf with any data allocated from the ARC.
2456 ASSERT(db->db_state == DB_UNCACHED ||
2457 db->db_state == DB_NOFILL);
2459 } else if (arc_released(db->db_buf)) {
2460 arc_buf_t *buf = db->db_buf;
2462 * This dbuf has anonymous data associated with it.
2464 dbuf_clear_data(db);
2465 VERIFY(arc_buf_remove_ref(buf, db));
2468 VERIFY(!arc_buf_remove_ref(db->db_buf, db));
2471 * A dbuf will be eligible for eviction if either the
2472 * 'primarycache' property is set or a duplicate
2473 * copy of this buffer is already cached in the arc.
2475 * In the case of the 'primarycache' a buffer
2476 * is considered for eviction if it matches the
2477 * criteria set in the property.
2479 * To decide if our buffer is considered a
2480 * duplicate, we must call into the arc to determine
2481 * if multiple buffers are referencing the same
2482 * block on-disk. If so, then we simply evict
2485 if (!DBUF_IS_CACHEABLE(db)) {
2486 if (db->db_blkptr != NULL &&
2487 !BP_IS_HOLE(db->db_blkptr) &&
2488 !BP_IS_EMBEDDED(db->db_blkptr)) {
2490 dmu_objset_spa(db->db_objset);
2491 blkptr_t bp = *db->db_blkptr;
2493 arc_freed(spa, &bp);
2497 } else if (db->db_objset->os_evicting ||
2498 arc_buf_eviction_needed(db->db_buf)) {
2501 mutex_exit(&db->db_mtx);
2505 mutex_exit(&db->db_mtx);
2509 #pragma weak dmu_buf_refcount = dbuf_refcount
2511 dbuf_refcount(dmu_buf_impl_t *db)
2513 return (refcount_count(&db->db_holds));
2517 dmu_buf_replace_user(dmu_buf_t *db_fake, dmu_buf_user_t *old_user,
2518 dmu_buf_user_t *new_user)
2520 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2522 mutex_enter(&db->db_mtx);
2523 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2524 if (db->db_user == old_user)
2525 db->db_user = new_user;
2527 old_user = db->db_user;
2528 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2529 mutex_exit(&db->db_mtx);
2535 dmu_buf_set_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2537 return (dmu_buf_replace_user(db_fake, NULL, user));
2541 dmu_buf_set_user_ie(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2543 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2545 db->db_immediate_evict = TRUE;
2546 return (dmu_buf_set_user(db_fake, user));
2550 dmu_buf_remove_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2552 return (dmu_buf_replace_user(db_fake, user, NULL));
2556 dmu_buf_get_user(dmu_buf_t *db_fake)
2558 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2560 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2561 return (db->db_user);
2565 dmu_buf_user_evict_wait()
2567 taskq_wait(dbu_evict_taskq);
2571 dmu_buf_freeable(dmu_buf_t *dbuf)
2573 boolean_t res = B_FALSE;
2574 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
2577 res = dsl_dataset_block_freeable(db->db_objset->os_dsl_dataset,
2578 db->db_blkptr, db->db_blkptr->blk_birth);
2584 dmu_buf_get_blkptr(dmu_buf_t *db)
2586 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2587 return (dbi->db_blkptr);
2591 dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db)
2593 /* ASSERT(dmu_tx_is_syncing(tx) */
2594 ASSERT(MUTEX_HELD(&db->db_mtx));
2596 if (db->db_blkptr != NULL)
2599 if (db->db_blkid == DMU_SPILL_BLKID) {
2600 db->db_blkptr = &dn->dn_phys->dn_spill;
2601 BP_ZERO(db->db_blkptr);
2604 if (db->db_level == dn->dn_phys->dn_nlevels-1) {
2606 * This buffer was allocated at a time when there was
2607 * no available blkptrs from the dnode, or it was
2608 * inappropriate to hook it in (i.e., nlevels mis-match).
2610 ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr);
2611 ASSERT(db->db_parent == NULL);
2612 db->db_parent = dn->dn_dbuf;
2613 db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
2616 dmu_buf_impl_t *parent = db->db_parent;
2617 int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2619 ASSERT(dn->dn_phys->dn_nlevels > 1);
2620 if (parent == NULL) {
2621 mutex_exit(&db->db_mtx);
2622 rw_enter(&dn->dn_struct_rwlock, RW_READER);
2623 parent = dbuf_hold_level(dn, db->db_level + 1,
2624 db->db_blkid >> epbs, db);
2625 rw_exit(&dn->dn_struct_rwlock);
2626 mutex_enter(&db->db_mtx);
2627 db->db_parent = parent;
2629 db->db_blkptr = (blkptr_t *)parent->db.db_data +
2630 (db->db_blkid & ((1ULL << epbs) - 1));
2636 dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2638 dmu_buf_impl_t *db = dr->dr_dbuf;
2642 ASSERT(dmu_tx_is_syncing(tx));
2644 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2646 mutex_enter(&db->db_mtx);
2648 ASSERT(db->db_level > 0);
2651 /* Read the block if it hasn't been read yet. */
2652 if (db->db_buf == NULL) {
2653 mutex_exit(&db->db_mtx);
2654 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
2655 mutex_enter(&db->db_mtx);
2657 ASSERT3U(db->db_state, ==, DB_CACHED);
2658 ASSERT(db->db_buf != NULL);
2662 /* Indirect block size must match what the dnode thinks it is. */
2663 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2664 dbuf_check_blkptr(dn, db);
2667 /* Provide the pending dirty record to child dbufs */
2668 db->db_data_pending = dr;
2670 mutex_exit(&db->db_mtx);
2671 dbuf_write(dr, db->db_buf, tx);
2674 mutex_enter(&dr->dt.di.dr_mtx);
2675 dbuf_sync_list(&dr->dt.di.dr_children, db->db_level - 1, tx);
2676 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
2677 mutex_exit(&dr->dt.di.dr_mtx);
2682 dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2684 arc_buf_t **datap = &dr->dt.dl.dr_data;
2685 dmu_buf_impl_t *db = dr->dr_dbuf;
2688 uint64_t txg = tx->tx_txg;
2690 ASSERT(dmu_tx_is_syncing(tx));
2692 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2694 mutex_enter(&db->db_mtx);
2696 * To be synced, we must be dirtied. But we
2697 * might have been freed after the dirty.
2699 if (db->db_state == DB_UNCACHED) {
2700 /* This buffer has been freed since it was dirtied */
2701 ASSERT(db->db.db_data == NULL);
2702 } else if (db->db_state == DB_FILL) {
2703 /* This buffer was freed and is now being re-filled */
2704 ASSERT(db->db.db_data != dr->dt.dl.dr_data);
2706 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL);
2713 if (db->db_blkid == DMU_SPILL_BLKID) {
2714 mutex_enter(&dn->dn_mtx);
2715 dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR;
2716 mutex_exit(&dn->dn_mtx);
2720 * If this is a bonus buffer, simply copy the bonus data into the
2721 * dnode. It will be written out when the dnode is synced (and it
2722 * will be synced, since it must have been dirty for dbuf_sync to
2725 if (db->db_blkid == DMU_BONUS_BLKID) {
2726 dbuf_dirty_record_t **drp;
2728 ASSERT(*datap != NULL);
2729 ASSERT0(db->db_level);
2730 ASSERT3U(dn->dn_phys->dn_bonuslen, <=, DN_MAX_BONUSLEN);
2731 bcopy(*datap, DN_BONUS(dn->dn_phys), dn->dn_phys->dn_bonuslen);
2734 if (*datap != db->db.db_data) {
2735 zio_buf_free(*datap, DN_MAX_BONUSLEN);
2736 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
2738 db->db_data_pending = NULL;
2739 drp = &db->db_last_dirty;
2741 drp = &(*drp)->dr_next;
2742 ASSERT(dr->dr_next == NULL);
2743 ASSERT(dr->dr_dbuf == db);
2745 if (dr->dr_dbuf->db_level != 0) {
2746 list_destroy(&dr->dt.di.dr_children);
2747 mutex_destroy(&dr->dt.di.dr_mtx);
2749 kmem_free(dr, sizeof (dbuf_dirty_record_t));
2750 ASSERT(db->db_dirtycnt > 0);
2751 db->db_dirtycnt -= 1;
2752 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
2759 * This function may have dropped the db_mtx lock allowing a dmu_sync
2760 * operation to sneak in. As a result, we need to ensure that we
2761 * don't check the dr_override_state until we have returned from
2762 * dbuf_check_blkptr.
2764 dbuf_check_blkptr(dn, db);
2767 * If this buffer is in the middle of an immediate write,
2768 * wait for the synchronous IO to complete.
2770 while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
2771 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
2772 cv_wait(&db->db_changed, &db->db_mtx);
2773 ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN);
2776 if (db->db_state != DB_NOFILL &&
2777 dn->dn_object != DMU_META_DNODE_OBJECT &&
2778 refcount_count(&db->db_holds) > 1 &&
2779 dr->dt.dl.dr_override_state != DR_OVERRIDDEN &&
2780 *datap == db->db_buf) {
2782 * If this buffer is currently "in use" (i.e., there
2783 * are active holds and db_data still references it),
2784 * then make a copy before we start the write so that
2785 * any modifications from the open txg will not leak
2788 * NOTE: this copy does not need to be made for
2789 * objects only modified in the syncing context (e.g.
2790 * DNONE_DNODE blocks).
2792 int blksz = arc_buf_size(*datap);
2793 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
2794 *datap = arc_buf_alloc(os->os_spa, blksz, db, type);
2795 bcopy(db->db.db_data, (*datap)->b_data, blksz);
2797 db->db_data_pending = dr;
2799 mutex_exit(&db->db_mtx);
2801 dbuf_write(dr, *datap, tx);
2803 ASSERT(!list_link_active(&dr->dr_dirty_node));
2804 if (dn->dn_object == DMU_META_DNODE_OBJECT) {
2805 list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr);
2809 * Although zio_nowait() does not "wait for an IO", it does
2810 * initiate the IO. If this is an empty write it seems plausible
2811 * that the IO could actually be completed before the nowait
2812 * returns. We need to DB_DNODE_EXIT() first in case
2813 * zio_nowait() invalidates the dbuf.
2816 zio_nowait(dr->dr_zio);
2821 dbuf_sync_list(list_t *list, int level, dmu_tx_t *tx)
2823 dbuf_dirty_record_t *dr;
2825 while (dr = list_head(list)) {
2826 if (dr->dr_zio != NULL) {
2828 * If we find an already initialized zio then we
2829 * are processing the meta-dnode, and we have finished.
2830 * The dbufs for all dnodes are put back on the list
2831 * during processing, so that we can zio_wait()
2832 * these IOs after initiating all child IOs.
2834 ASSERT3U(dr->dr_dbuf->db.db_object, ==,
2835 DMU_META_DNODE_OBJECT);
2838 if (dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
2839 dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
2840 VERIFY3U(dr->dr_dbuf->db_level, ==, level);
2842 list_remove(list, dr);
2843 if (dr->dr_dbuf->db_level > 0)
2844 dbuf_sync_indirect(dr, tx);
2846 dbuf_sync_leaf(dr, tx);
2852 dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
2854 dmu_buf_impl_t *db = vdb;
2856 blkptr_t *bp = zio->io_bp;
2857 blkptr_t *bp_orig = &zio->io_bp_orig;
2858 spa_t *spa = zio->io_spa;
2863 ASSERT3P(db->db_blkptr, ==, bp);
2867 delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig);
2868 dnode_diduse_space(dn, delta - zio->io_prev_space_delta);
2869 zio->io_prev_space_delta = delta;
2871 if (bp->blk_birth != 0) {
2872 ASSERT((db->db_blkid != DMU_SPILL_BLKID &&
2873 BP_GET_TYPE(bp) == dn->dn_type) ||
2874 (db->db_blkid == DMU_SPILL_BLKID &&
2875 BP_GET_TYPE(bp) == dn->dn_bonustype) ||
2876 BP_IS_EMBEDDED(bp));
2877 ASSERT(BP_GET_LEVEL(bp) == db->db_level);
2880 mutex_enter(&db->db_mtx);
2883 if (db->db_blkid == DMU_SPILL_BLKID) {
2884 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
2885 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
2886 db->db_blkptr == &dn->dn_phys->dn_spill);
2890 if (db->db_level == 0) {
2891 mutex_enter(&dn->dn_mtx);
2892 if (db->db_blkid > dn->dn_phys->dn_maxblkid &&
2893 db->db_blkid != DMU_SPILL_BLKID)
2894 dn->dn_phys->dn_maxblkid = db->db_blkid;
2895 mutex_exit(&dn->dn_mtx);
2897 if (dn->dn_type == DMU_OT_DNODE) {
2898 dnode_phys_t *dnp = db->db.db_data;
2899 for (i = db->db.db_size >> DNODE_SHIFT; i > 0;
2901 if (dnp->dn_type != DMU_OT_NONE)
2905 if (BP_IS_HOLE(bp)) {
2912 blkptr_t *ibp = db->db.db_data;
2913 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2914 for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) {
2915 if (BP_IS_HOLE(ibp))
2917 fill += BP_GET_FILL(ibp);
2922 if (!BP_IS_EMBEDDED(bp))
2923 bp->blk_fill = fill;
2925 mutex_exit(&db->db_mtx);
2929 * The SPA will call this callback several times for each zio - once
2930 * for every physical child i/o (zio->io_phys_children times). This
2931 * allows the DMU to monitor the progress of each logical i/o. For example,
2932 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
2933 * block. There may be a long delay before all copies/fragments are completed,
2934 * so this callback allows us to retire dirty space gradually, as the physical
2939 dbuf_write_physdone(zio_t *zio, arc_buf_t *buf, void *arg)
2941 dmu_buf_impl_t *db = arg;
2942 objset_t *os = db->db_objset;
2943 dsl_pool_t *dp = dmu_objset_pool(os);
2944 dbuf_dirty_record_t *dr;
2947 dr = db->db_data_pending;
2948 ASSERT3U(dr->dr_txg, ==, zio->io_txg);
2951 * The callback will be called io_phys_children times. Retire one
2952 * portion of our dirty space each time we are called. Any rounding
2953 * error will be cleaned up by dsl_pool_sync()'s call to
2954 * dsl_pool_undirty_space().
2956 delta = dr->dr_accounted / zio->io_phys_children;
2957 dsl_pool_undirty_space(dp, delta, zio->io_txg);
2962 dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
2964 dmu_buf_impl_t *db = vdb;
2965 blkptr_t *bp_orig = &zio->io_bp_orig;
2966 blkptr_t *bp = db->db_blkptr;
2967 objset_t *os = db->db_objset;
2968 dmu_tx_t *tx = os->os_synctx;
2969 dbuf_dirty_record_t **drp, *dr;
2971 ASSERT0(zio->io_error);
2972 ASSERT(db->db_blkptr == bp);
2975 * For nopwrites and rewrites we ensure that the bp matches our
2976 * original and bypass all the accounting.
2978 if (zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)) {
2979 ASSERT(BP_EQUAL(bp, bp_orig));
2981 dsl_dataset_t *ds = os->os_dsl_dataset;
2982 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
2983 dsl_dataset_block_born(ds, bp, tx);
2986 mutex_enter(&db->db_mtx);
2990 drp = &db->db_last_dirty;
2991 while ((dr = *drp) != db->db_data_pending)
2993 ASSERT(!list_link_active(&dr->dr_dirty_node));
2994 ASSERT(dr->dr_dbuf == db);
2995 ASSERT(dr->dr_next == NULL);
2999 if (db->db_blkid == DMU_SPILL_BLKID) {
3004 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
3005 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
3006 db->db_blkptr == &dn->dn_phys->dn_spill);
3011 if (db->db_level == 0) {
3012 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
3013 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
3014 if (db->db_state != DB_NOFILL) {
3015 if (dr->dt.dl.dr_data != db->db_buf)
3016 VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data,
3018 else if (!arc_released(db->db_buf))
3019 arc_set_callback(db->db_buf, dbuf_do_evict, db);
3026 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
3027 ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
3028 if (!BP_IS_HOLE(db->db_blkptr)) {
3030 dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
3031 ASSERT3U(db->db_blkid, <=,
3032 dn->dn_phys->dn_maxblkid >> (db->db_level * epbs));
3033 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
3035 if (!arc_released(db->db_buf))
3036 arc_set_callback(db->db_buf, dbuf_do_evict, db);
3039 mutex_destroy(&dr->dt.di.dr_mtx);
3040 list_destroy(&dr->dt.di.dr_children);
3042 kmem_free(dr, sizeof (dbuf_dirty_record_t));
3044 cv_broadcast(&db->db_changed);
3045 ASSERT(db->db_dirtycnt > 0);
3046 db->db_dirtycnt -= 1;
3047 db->db_data_pending = NULL;
3048 dbuf_rele_and_unlock(db, (void *)(uintptr_t)tx->tx_txg);
3052 dbuf_write_nofill_ready(zio_t *zio)
3054 dbuf_write_ready(zio, NULL, zio->io_private);
3058 dbuf_write_nofill_done(zio_t *zio)
3060 dbuf_write_done(zio, NULL, zio->io_private);
3064 dbuf_write_override_ready(zio_t *zio)
3066 dbuf_dirty_record_t *dr = zio->io_private;
3067 dmu_buf_impl_t *db = dr->dr_dbuf;
3069 dbuf_write_ready(zio, NULL, db);
3073 dbuf_write_override_done(zio_t *zio)
3075 dbuf_dirty_record_t *dr = zio->io_private;
3076 dmu_buf_impl_t *db = dr->dr_dbuf;
3077 blkptr_t *obp = &dr->dt.dl.dr_overridden_by;
3079 mutex_enter(&db->db_mtx);
3080 if (!BP_EQUAL(zio->io_bp, obp)) {
3081 if (!BP_IS_HOLE(obp))
3082 dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp);
3083 arc_release(dr->dt.dl.dr_data, db);
3085 mutex_exit(&db->db_mtx);
3087 dbuf_write_done(zio, NULL, db);
3090 /* Issue I/O to commit a dirty buffer to disk. */
3092 dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx)
3094 dmu_buf_impl_t *db = dr->dr_dbuf;
3097 dmu_buf_impl_t *parent = db->db_parent;
3098 uint64_t txg = tx->tx_txg;
3099 zbookmark_phys_t zb;
3108 if (db->db_state != DB_NOFILL) {
3109 if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) {
3111 * Private object buffers are released here rather
3112 * than in dbuf_dirty() since they are only modified
3113 * in the syncing context and we don't want the
3114 * overhead of making multiple copies of the data.
3116 if (BP_IS_HOLE(db->db_blkptr)) {
3119 dbuf_release_bp(db);
3124 if (parent != dn->dn_dbuf) {
3125 /* Our parent is an indirect block. */
3126 /* We have a dirty parent that has been scheduled for write. */
3127 ASSERT(parent && parent->db_data_pending);
3128 /* Our parent's buffer is one level closer to the dnode. */
3129 ASSERT(db->db_level == parent->db_level-1);
3131 * We're about to modify our parent's db_data by modifying
3132 * our block pointer, so the parent must be released.
3134 ASSERT(arc_released(parent->db_buf));
3135 zio = parent->db_data_pending->dr_zio;
3137 /* Our parent is the dnode itself. */
3138 ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 &&
3139 db->db_blkid != DMU_SPILL_BLKID) ||
3140 (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0));
3141 if (db->db_blkid != DMU_SPILL_BLKID)
3142 ASSERT3P(db->db_blkptr, ==,
3143 &dn->dn_phys->dn_blkptr[db->db_blkid]);
3147 ASSERT(db->db_level == 0 || data == db->db_buf);
3148 ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
3151 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
3152 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
3153 db->db.db_object, db->db_level, db->db_blkid);
3155 if (db->db_blkid == DMU_SPILL_BLKID)
3157 wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
3159 dmu_write_policy(os, dn, db->db_level, wp_flag, &zp);
3162 if (db->db_level == 0 &&
3163 dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
3165 * The BP for this block has been provided by open context
3166 * (by dmu_sync() or dmu_buf_write_embedded()).
3168 void *contents = (data != NULL) ? data->b_data : NULL;
3170 dr->dr_zio = zio_write(zio, os->os_spa, txg,
3171 db->db_blkptr, contents, db->db.db_size, &zp,
3172 dbuf_write_override_ready, NULL, dbuf_write_override_done,
3173 dr, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
3174 mutex_enter(&db->db_mtx);
3175 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
3176 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by,
3177 dr->dt.dl.dr_copies, dr->dt.dl.dr_nopwrite);
3178 mutex_exit(&db->db_mtx);
3179 } else if (db->db_state == DB_NOFILL) {
3180 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF ||
3181 zp.zp_checksum == ZIO_CHECKSUM_NOPARITY);
3182 dr->dr_zio = zio_write(zio, os->os_spa, txg,
3183 db->db_blkptr, NULL, db->db.db_size, &zp,
3184 dbuf_write_nofill_ready, NULL, dbuf_write_nofill_done, db,
3185 ZIO_PRIORITY_ASYNC_WRITE,
3186 ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb);
3188 ASSERT(arc_released(data));
3189 dr->dr_zio = arc_write(zio, os->os_spa, txg,
3190 db->db_blkptr, data, DBUF_IS_L2CACHEABLE(db),
3191 DBUF_IS_L2COMPRESSIBLE(db), &zp, dbuf_write_ready,
3192 dbuf_write_physdone, dbuf_write_done, db,
3193 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);