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) 2013 by Delphix. All rights reserved.
25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
28 #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 static void dbuf_destroy(dmu_buf_impl_t *db);
43 static boolean_t dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
44 static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx);
47 * Global data structures and functions for the dbuf cache.
49 static kmem_cache_t *dbuf_cache;
53 dbuf_cons(void *vdb, void *unused, int kmflag)
55 dmu_buf_impl_t *db = vdb;
56 bzero(db, sizeof (dmu_buf_impl_t));
58 mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL);
59 cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL);
60 refcount_create(&db->db_holds);
66 dbuf_dest(void *vdb, void *unused)
68 dmu_buf_impl_t *db = vdb;
69 mutex_destroy(&db->db_mtx);
70 cv_destroy(&db->db_changed);
71 refcount_destroy(&db->db_holds);
75 * dbuf hash table routines
77 static dbuf_hash_table_t dbuf_hash_table;
79 static uint64_t dbuf_hash_count;
82 dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid)
84 uintptr_t osv = (uintptr_t)os;
87 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
88 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (lvl)) & 0xFF];
89 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF];
90 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF];
91 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF];
92 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 0)) & 0xFF];
93 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 8)) & 0xFF];
95 crc ^= (osv>>14) ^ (obj>>16) ^ (blkid>>16);
100 #define DBUF_HASH(os, obj, level, blkid) dbuf_hash(os, obj, level, blkid);
102 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
103 ((dbuf)->db.db_object == (obj) && \
104 (dbuf)->db_objset == (os) && \
105 (dbuf)->db_level == (level) && \
106 (dbuf)->db_blkid == (blkid))
109 dbuf_find(dnode_t *dn, uint8_t level, uint64_t blkid)
111 dbuf_hash_table_t *h = &dbuf_hash_table;
112 objset_t *os = dn->dn_objset;
113 uint64_t obj = dn->dn_object;
114 uint64_t hv = DBUF_HASH(os, obj, level, blkid);
115 uint64_t idx = hv & h->hash_table_mask;
118 mutex_enter(DBUF_HASH_MUTEX(h, idx));
119 for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) {
120 if (DBUF_EQUAL(db, os, obj, level, blkid)) {
121 mutex_enter(&db->db_mtx);
122 if (db->db_state != DB_EVICTING) {
123 mutex_exit(DBUF_HASH_MUTEX(h, idx));
126 mutex_exit(&db->db_mtx);
129 mutex_exit(DBUF_HASH_MUTEX(h, idx));
134 * Insert an entry into the hash table. If there is already an element
135 * equal to elem in the hash table, then the already existing element
136 * will be returned and the new element will not be inserted.
137 * Otherwise returns NULL.
139 static dmu_buf_impl_t *
140 dbuf_hash_insert(dmu_buf_impl_t *db)
142 dbuf_hash_table_t *h = &dbuf_hash_table;
143 objset_t *os = db->db_objset;
144 uint64_t obj = db->db.db_object;
145 int level = db->db_level;
146 uint64_t blkid = db->db_blkid;
147 uint64_t hv = DBUF_HASH(os, obj, level, blkid);
148 uint64_t idx = hv & h->hash_table_mask;
151 mutex_enter(DBUF_HASH_MUTEX(h, idx));
152 for (dbf = h->hash_table[idx]; dbf != NULL; dbf = dbf->db_hash_next) {
153 if (DBUF_EQUAL(dbf, os, obj, level, blkid)) {
154 mutex_enter(&dbf->db_mtx);
155 if (dbf->db_state != DB_EVICTING) {
156 mutex_exit(DBUF_HASH_MUTEX(h, idx));
159 mutex_exit(&dbf->db_mtx);
163 mutex_enter(&db->db_mtx);
164 db->db_hash_next = h->hash_table[idx];
165 h->hash_table[idx] = db;
166 mutex_exit(DBUF_HASH_MUTEX(h, idx));
167 atomic_add_64(&dbuf_hash_count, 1);
173 * Remove an entry from the hash table. This operation will
174 * fail if there are any existing holds on the db.
177 dbuf_hash_remove(dmu_buf_impl_t *db)
179 dbuf_hash_table_t *h = &dbuf_hash_table;
180 uint64_t hv = DBUF_HASH(db->db_objset, db->db.db_object,
181 db->db_level, db->db_blkid);
182 uint64_t idx = hv & h->hash_table_mask;
183 dmu_buf_impl_t *dbf, **dbp;
186 * We musn't hold db_mtx to maintin lock ordering:
187 * DBUF_HASH_MUTEX > db_mtx.
189 ASSERT(refcount_is_zero(&db->db_holds));
190 ASSERT(db->db_state == DB_EVICTING);
191 ASSERT(!MUTEX_HELD(&db->db_mtx));
193 mutex_enter(DBUF_HASH_MUTEX(h, idx));
194 dbp = &h->hash_table[idx];
195 while ((dbf = *dbp) != db) {
196 dbp = &dbf->db_hash_next;
199 *dbp = db->db_hash_next;
200 db->db_hash_next = NULL;
201 mutex_exit(DBUF_HASH_MUTEX(h, idx));
202 atomic_add_64(&dbuf_hash_count, -1);
205 static arc_evict_func_t dbuf_do_evict;
208 dbuf_evict_user(dmu_buf_impl_t *db)
210 ASSERT(MUTEX_HELD(&db->db_mtx));
212 if (db->db_level != 0 || db->db_evict_func == NULL)
215 if (db->db_user_data_ptr_ptr)
216 *db->db_user_data_ptr_ptr = db->db.db_data;
217 db->db_evict_func(&db->db, db->db_user_ptr);
218 db->db_user_ptr = NULL;
219 db->db_user_data_ptr_ptr = NULL;
220 db->db_evict_func = NULL;
224 dbuf_is_metadata(dmu_buf_impl_t *db)
226 if (db->db_level > 0) {
229 boolean_t is_metadata;
232 is_metadata = DMU_OT_IS_METADATA(DB_DNODE(db)->dn_type);
235 return (is_metadata);
240 dbuf_evict(dmu_buf_impl_t *db)
242 ASSERT(MUTEX_HELD(&db->db_mtx));
243 ASSERT(db->db_buf == NULL);
244 ASSERT(db->db_data_pending == NULL);
253 uint64_t hsize = 1ULL << 16;
254 dbuf_hash_table_t *h = &dbuf_hash_table;
258 * The hash table is big enough to fill all of physical memory
259 * with an average 4K block size. The table will take up
260 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
262 while (hsize * 4096 < physmem * PAGESIZE)
266 h->hash_table_mask = hsize - 1;
267 h->hash_table = kmem_zalloc(hsize * sizeof (void *), KM_NOSLEEP);
268 if (h->hash_table == NULL) {
269 /* XXX - we should really return an error instead of assert */
270 ASSERT(hsize > (1ULL << 10));
275 dbuf_cache = kmem_cache_create("dmu_buf_impl_t",
276 sizeof (dmu_buf_impl_t),
277 0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0);
279 for (i = 0; i < DBUF_MUTEXES; i++)
280 mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL);
286 dbuf_hash_table_t *h = &dbuf_hash_table;
289 for (i = 0; i < DBUF_MUTEXES; i++)
290 mutex_destroy(&h->hash_mutexes[i]);
291 kmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
292 kmem_cache_destroy(dbuf_cache);
301 dbuf_verify(dmu_buf_impl_t *db)
304 dbuf_dirty_record_t *dr;
306 ASSERT(MUTEX_HELD(&db->db_mtx));
308 if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY))
311 ASSERT(db->db_objset != NULL);
315 ASSERT(db->db_parent == NULL);
316 ASSERT(db->db_blkptr == NULL);
318 ASSERT3U(db->db.db_object, ==, dn->dn_object);
319 ASSERT3P(db->db_objset, ==, dn->dn_objset);
320 ASSERT3U(db->db_level, <, dn->dn_nlevels);
321 ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
322 db->db_blkid == DMU_SPILL_BLKID ||
323 !list_is_empty(&dn->dn_dbufs));
325 if (db->db_blkid == DMU_BONUS_BLKID) {
327 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
328 ASSERT3U(db->db.db_offset, ==, DMU_BONUS_BLKID);
329 } else if (db->db_blkid == DMU_SPILL_BLKID) {
331 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
332 ASSERT0(db->db.db_offset);
334 ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size);
337 for (dr = db->db_data_pending; dr != NULL; dr = dr->dr_next)
338 ASSERT(dr->dr_dbuf == db);
340 for (dr = db->db_last_dirty; dr != NULL; dr = dr->dr_next)
341 ASSERT(dr->dr_dbuf == db);
344 * We can't assert that db_size matches dn_datablksz because it
345 * can be momentarily different when another thread is doing
348 if (db->db_level == 0 && db->db.db_object == DMU_META_DNODE_OBJECT) {
349 dr = db->db_data_pending;
351 * It should only be modified in syncing context, so
352 * make sure we only have one copy of the data.
354 ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf);
357 /* verify db->db_blkptr */
359 if (db->db_parent == dn->dn_dbuf) {
360 /* db is pointed to by the dnode */
361 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
362 if (DMU_OBJECT_IS_SPECIAL(db->db.db_object))
363 ASSERT(db->db_parent == NULL);
365 ASSERT(db->db_parent != NULL);
366 if (db->db_blkid != DMU_SPILL_BLKID)
367 ASSERT3P(db->db_blkptr, ==,
368 &dn->dn_phys->dn_blkptr[db->db_blkid]);
370 /* db is pointed to by an indirect block */
371 int epb = db->db_parent->db.db_size >> SPA_BLKPTRSHIFT;
372 ASSERT3U(db->db_parent->db_level, ==, db->db_level+1);
373 ASSERT3U(db->db_parent->db.db_object, ==,
376 * dnode_grow_indblksz() can make this fail if we don't
377 * have the struct_rwlock. XXX indblksz no longer
378 * grows. safe to do this now?
380 if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
381 ASSERT3P(db->db_blkptr, ==,
382 ((blkptr_t *)db->db_parent->db.db_data +
383 db->db_blkid % epb));
387 if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) &&
388 (db->db_buf == NULL || db->db_buf->b_data) &&
389 db->db.db_data && db->db_blkid != DMU_BONUS_BLKID &&
390 db->db_state != DB_FILL && !dn->dn_free_txg) {
392 * If the blkptr isn't set but they have nonzero data,
393 * it had better be dirty, otherwise we'll lose that
394 * data when we evict this buffer.
396 if (db->db_dirtycnt == 0) {
397 uint64_t *buf = db->db.db_data;
400 for (i = 0; i < db->db.db_size >> 3; i++) {
410 dbuf_update_data(dmu_buf_impl_t *db)
412 ASSERT(MUTEX_HELD(&db->db_mtx));
413 if (db->db_level == 0 && db->db_user_data_ptr_ptr) {
414 ASSERT(!refcount_is_zero(&db->db_holds));
415 *db->db_user_data_ptr_ptr = db->db.db_data;
420 dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf)
422 ASSERT(MUTEX_HELD(&db->db_mtx));
423 ASSERT(db->db_buf == NULL || !arc_has_callback(db->db_buf));
426 ASSERT(buf->b_data != NULL);
427 db->db.db_data = buf->b_data;
428 if (!arc_released(buf))
429 arc_set_callback(buf, dbuf_do_evict, db);
430 dbuf_update_data(db);
433 db->db.db_data = NULL;
434 if (db->db_state != DB_NOFILL)
435 db->db_state = DB_UNCACHED;
440 * Loan out an arc_buf for read. Return the loaned arc_buf.
443 dbuf_loan_arcbuf(dmu_buf_impl_t *db)
447 mutex_enter(&db->db_mtx);
448 if (arc_released(db->db_buf) || refcount_count(&db->db_holds) > 1) {
449 int blksz = db->db.db_size;
452 mutex_exit(&db->db_mtx);
453 DB_GET_SPA(&spa, db);
454 abuf = arc_loan_buf(spa, blksz);
455 bcopy(db->db.db_data, abuf->b_data, blksz);
458 arc_loan_inuse_buf(abuf, db);
459 dbuf_set_data(db, NULL);
460 mutex_exit(&db->db_mtx);
466 dbuf_whichblock(dnode_t *dn, uint64_t offset)
468 if (dn->dn_datablkshift) {
469 return (offset >> dn->dn_datablkshift);
471 ASSERT3U(offset, <, dn->dn_datablksz);
477 dbuf_read_done(zio_t *zio, arc_buf_t *buf, void *vdb)
479 dmu_buf_impl_t *db = vdb;
481 mutex_enter(&db->db_mtx);
482 ASSERT3U(db->db_state, ==, DB_READ);
484 * All reads are synchronous, so we must have a hold on the dbuf
486 ASSERT(refcount_count(&db->db_holds) > 0);
487 ASSERT(db->db_buf == NULL);
488 ASSERT(db->db.db_data == NULL);
489 if (db->db_level == 0 && db->db_freed_in_flight) {
490 /* we were freed in flight; disregard any error */
491 arc_release(buf, db);
492 bzero(buf->b_data, db->db.db_size);
494 db->db_freed_in_flight = FALSE;
495 dbuf_set_data(db, buf);
496 db->db_state = DB_CACHED;
497 } else if (zio == NULL || zio->io_error == 0) {
498 dbuf_set_data(db, buf);
499 db->db_state = DB_CACHED;
501 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
502 ASSERT3P(db->db_buf, ==, NULL);
503 VERIFY(arc_buf_remove_ref(buf, db));
504 db->db_state = DB_UNCACHED;
506 cv_broadcast(&db->db_changed);
507 dbuf_rele_and_unlock(db, NULL);
511 dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t *flags)
516 uint32_t aflags = ARC_NOWAIT;
520 ASSERT(!refcount_is_zero(&db->db_holds));
521 /* We need the struct_rwlock to prevent db_blkptr from changing. */
522 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
523 ASSERT(MUTEX_HELD(&db->db_mtx));
524 ASSERT(db->db_state == DB_UNCACHED);
525 ASSERT(db->db_buf == NULL);
527 if (db->db_blkid == DMU_BONUS_BLKID) {
528 int bonuslen = MIN(dn->dn_bonuslen, dn->dn_phys->dn_bonuslen);
530 ASSERT3U(bonuslen, <=, db->db.db_size);
531 db->db.db_data = zio_buf_alloc(DN_MAX_BONUSLEN);
532 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
533 if (bonuslen < DN_MAX_BONUSLEN)
534 bzero(db->db.db_data, DN_MAX_BONUSLEN);
536 bcopy(DN_BONUS(dn->dn_phys), db->db.db_data, bonuslen);
538 dbuf_update_data(db);
539 db->db_state = DB_CACHED;
540 mutex_exit(&db->db_mtx);
545 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
546 * processes the delete record and clears the bp while we are waiting
547 * for the dn_mtx (resulting in a "no" from block_freed).
549 if (db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr) ||
550 (db->db_level == 0 && (dnode_block_freed(dn, db->db_blkid) ||
551 BP_IS_HOLE(db->db_blkptr)))) {
552 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
554 dbuf_set_data(db, arc_buf_alloc(dn->dn_objset->os_spa,
555 db->db.db_size, db, type));
557 bzero(db->db.db_data, db->db.db_size);
558 db->db_state = DB_CACHED;
559 *flags |= DB_RF_CACHED;
560 mutex_exit(&db->db_mtx);
564 spa = dn->dn_objset->os_spa;
567 db->db_state = DB_READ;
568 mutex_exit(&db->db_mtx);
570 if (DBUF_IS_L2CACHEABLE(db))
571 aflags |= ARC_L2CACHE;
572 if (DBUF_IS_L2COMPRESSIBLE(db))
573 aflags |= ARC_L2COMPRESS;
575 SET_BOOKMARK(&zb, db->db_objset->os_dsl_dataset ?
576 db->db_objset->os_dsl_dataset->ds_object : DMU_META_OBJSET,
577 db->db.db_object, db->db_level, db->db_blkid);
579 dbuf_add_ref(db, NULL);
581 (void) arc_read(zio, spa, db->db_blkptr,
582 dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ,
583 (*flags & DB_RF_CANFAIL) ? ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED,
585 if (aflags & ARC_CACHED)
586 *flags |= DB_RF_CACHED;
590 dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
593 int havepzio = (zio != NULL);
598 * We don't have to hold the mutex to check db_state because it
599 * can't be freed while we have a hold on the buffer.
601 ASSERT(!refcount_is_zero(&db->db_holds));
603 if (db->db_state == DB_NOFILL)
604 return (SET_ERROR(EIO));
608 if ((flags & DB_RF_HAVESTRUCT) == 0)
609 rw_enter(&dn->dn_struct_rwlock, RW_READER);
611 prefetch = db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
612 (flags & DB_RF_NOPREFETCH) == 0 && dn != NULL &&
613 DBUF_IS_CACHEABLE(db);
615 mutex_enter(&db->db_mtx);
616 if (db->db_state == DB_CACHED) {
617 mutex_exit(&db->db_mtx);
619 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
620 db->db.db_size, TRUE);
621 if ((flags & DB_RF_HAVESTRUCT) == 0)
622 rw_exit(&dn->dn_struct_rwlock);
624 } else if (db->db_state == DB_UNCACHED) {
625 spa_t *spa = dn->dn_objset->os_spa;
628 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
629 dbuf_read_impl(db, zio, &flags);
631 /* dbuf_read_impl has dropped db_mtx for us */
634 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
635 db->db.db_size, flags & DB_RF_CACHED);
637 if ((flags & DB_RF_HAVESTRUCT) == 0)
638 rw_exit(&dn->dn_struct_rwlock);
644 mutex_exit(&db->db_mtx);
646 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
647 db->db.db_size, TRUE);
648 if ((flags & DB_RF_HAVESTRUCT) == 0)
649 rw_exit(&dn->dn_struct_rwlock);
652 mutex_enter(&db->db_mtx);
653 if ((flags & DB_RF_NEVERWAIT) == 0) {
654 while (db->db_state == DB_READ ||
655 db->db_state == DB_FILL) {
656 ASSERT(db->db_state == DB_READ ||
657 (flags & DB_RF_HAVESTRUCT) == 0);
658 cv_wait(&db->db_changed, &db->db_mtx);
660 if (db->db_state == DB_UNCACHED)
661 err = SET_ERROR(EIO);
663 mutex_exit(&db->db_mtx);
666 ASSERT(err || havepzio || db->db_state == DB_CACHED);
671 dbuf_noread(dmu_buf_impl_t *db)
673 ASSERT(!refcount_is_zero(&db->db_holds));
674 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
675 mutex_enter(&db->db_mtx);
676 while (db->db_state == DB_READ || db->db_state == DB_FILL)
677 cv_wait(&db->db_changed, &db->db_mtx);
678 if (db->db_state == DB_UNCACHED) {
679 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
682 ASSERT(db->db_buf == NULL);
683 ASSERT(db->db.db_data == NULL);
684 DB_GET_SPA(&spa, db);
685 dbuf_set_data(db, arc_buf_alloc(spa, db->db.db_size, db, type));
686 db->db_state = DB_FILL;
687 } else if (db->db_state == DB_NOFILL) {
688 dbuf_set_data(db, NULL);
690 ASSERT3U(db->db_state, ==, DB_CACHED);
692 mutex_exit(&db->db_mtx);
696 * This is our just-in-time copy function. It makes a copy of
697 * buffers, that have been modified in a previous transaction
698 * group, before we modify them in the current active group.
700 * This function is used in two places: when we are dirtying a
701 * buffer for the first time in a txg, and when we are freeing
702 * a range in a dnode that includes this buffer.
704 * Note that when we are called from dbuf_free_range() we do
705 * not put a hold on the buffer, we just traverse the active
706 * dbuf list for the dnode.
709 dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg)
711 dbuf_dirty_record_t *dr = db->db_last_dirty;
713 ASSERT(MUTEX_HELD(&db->db_mtx));
714 ASSERT(db->db.db_data != NULL);
715 ASSERT(db->db_level == 0);
716 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT);
719 (dr->dt.dl.dr_data !=
720 ((db->db_blkid == DMU_BONUS_BLKID) ? db->db.db_data : db->db_buf)))
724 * If the last dirty record for this dbuf has not yet synced
725 * and its referencing the dbuf data, either:
726 * reset the reference to point to a new copy,
727 * or (if there a no active holders)
728 * just null out the current db_data pointer.
730 ASSERT(dr->dr_txg >= txg - 2);
731 if (db->db_blkid == DMU_BONUS_BLKID) {
732 /* Note that the data bufs here are zio_bufs */
733 dr->dt.dl.dr_data = zio_buf_alloc(DN_MAX_BONUSLEN);
734 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
735 bcopy(db->db.db_data, dr->dt.dl.dr_data, DN_MAX_BONUSLEN);
736 } else if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
737 int size = db->db.db_size;
738 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
741 DB_GET_SPA(&spa, db);
742 dr->dt.dl.dr_data = arc_buf_alloc(spa, size, db, type);
743 bcopy(db->db.db_data, dr->dt.dl.dr_data->b_data, size);
745 dbuf_set_data(db, NULL);
750 dbuf_unoverride(dbuf_dirty_record_t *dr)
752 dmu_buf_impl_t *db = dr->dr_dbuf;
753 blkptr_t *bp = &dr->dt.dl.dr_overridden_by;
754 uint64_t txg = dr->dr_txg;
756 ASSERT(MUTEX_HELD(&db->db_mtx));
757 ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC);
758 ASSERT(db->db_level == 0);
760 if (db->db_blkid == DMU_BONUS_BLKID ||
761 dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN)
764 ASSERT(db->db_data_pending != dr);
766 /* free this block */
767 if (!BP_IS_HOLE(bp) && !dr->dt.dl.dr_nopwrite) {
770 DB_GET_SPA(&spa, db);
771 zio_free(spa, txg, bp);
773 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
774 dr->dt.dl.dr_nopwrite = B_FALSE;
777 * Release the already-written buffer, so we leave it in
778 * a consistent dirty state. Note that all callers are
779 * modifying the buffer, so they will immediately do
780 * another (redundant) arc_release(). Therefore, leave
781 * the buf thawed to save the effort of freezing &
782 * immediately re-thawing it.
784 arc_release(dr->dt.dl.dr_data, db);
788 * Evict (if its unreferenced) or clear (if its referenced) any level-0
789 * data blocks in the free range, so that any future readers will find
790 * empty blocks. Also, if we happen accross any level-1 dbufs in the
791 * range that have not already been marked dirty, mark them dirty so
792 * they stay in memory.
795 dbuf_free_range(dnode_t *dn, uint64_t start, uint64_t end, dmu_tx_t *tx)
797 dmu_buf_impl_t *db, *db_next;
798 uint64_t txg = tx->tx_txg;
799 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
800 uint64_t first_l1 = start >> epbs;
801 uint64_t last_l1 = end >> epbs;
803 if (end > dn->dn_maxblkid && (end != DMU_SPILL_BLKID)) {
804 end = dn->dn_maxblkid;
805 last_l1 = end >> epbs;
807 dprintf_dnode(dn, "start=%llu end=%llu\n", start, end);
808 mutex_enter(&dn->dn_dbufs_mtx);
809 for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
810 db_next = list_next(&dn->dn_dbufs, db);
811 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
813 if (db->db_level == 1 &&
814 db->db_blkid >= first_l1 && db->db_blkid <= last_l1) {
815 mutex_enter(&db->db_mtx);
816 if (db->db_last_dirty &&
817 db->db_last_dirty->dr_txg < txg) {
818 dbuf_add_ref(db, FTAG);
819 mutex_exit(&db->db_mtx);
820 dbuf_will_dirty(db, tx);
823 mutex_exit(&db->db_mtx);
827 if (db->db_level != 0)
829 dprintf_dbuf(db, "found buf %s\n", "");
830 if (db->db_blkid < start || db->db_blkid > end)
833 /* found a level 0 buffer in the range */
834 mutex_enter(&db->db_mtx);
835 if (dbuf_undirty(db, tx)) {
836 /* mutex has been dropped and dbuf destroyed */
840 if (db->db_state == DB_UNCACHED ||
841 db->db_state == DB_NOFILL ||
842 db->db_state == DB_EVICTING) {
843 ASSERT(db->db.db_data == NULL);
844 mutex_exit(&db->db_mtx);
847 if (db->db_state == DB_READ || db->db_state == DB_FILL) {
848 /* will be handled in dbuf_read_done or dbuf_rele */
849 db->db_freed_in_flight = TRUE;
850 mutex_exit(&db->db_mtx);
853 if (refcount_count(&db->db_holds) == 0) {
858 /* The dbuf is referenced */
860 if (db->db_last_dirty != NULL) {
861 dbuf_dirty_record_t *dr = db->db_last_dirty;
863 if (dr->dr_txg == txg) {
865 * This buffer is "in-use", re-adjust the file
866 * size to reflect that this buffer may
867 * contain new data when we sync.
869 if (db->db_blkid != DMU_SPILL_BLKID &&
870 db->db_blkid > dn->dn_maxblkid)
871 dn->dn_maxblkid = db->db_blkid;
875 * This dbuf is not dirty in the open context.
876 * Either uncache it (if its not referenced in
877 * the open context) or reset its contents to
880 dbuf_fix_old_data(db, txg);
883 /* clear the contents if its cached */
884 if (db->db_state == DB_CACHED) {
885 ASSERT(db->db.db_data != NULL);
886 arc_release(db->db_buf, db);
887 bzero(db->db.db_data, db->db.db_size);
888 arc_buf_freeze(db->db_buf);
891 mutex_exit(&db->db_mtx);
893 mutex_exit(&dn->dn_dbufs_mtx);
897 dbuf_block_freeable(dmu_buf_impl_t *db)
899 dsl_dataset_t *ds = db->db_objset->os_dsl_dataset;
900 uint64_t birth_txg = 0;
903 * We don't need any locking to protect db_blkptr:
904 * If it's syncing, then db_last_dirty will be set
905 * so we'll ignore db_blkptr.
907 ASSERT(MUTEX_HELD(&db->db_mtx));
908 if (db->db_last_dirty)
909 birth_txg = db->db_last_dirty->dr_txg;
910 else if (db->db_blkptr)
911 birth_txg = db->db_blkptr->blk_birth;
914 * If we don't exist or are in a snapshot, we can't be freed.
915 * Don't pass the bp to dsl_dataset_block_freeable() since we
916 * are holding the db_mtx lock and might deadlock if we are
917 * prefetching a dedup-ed block.
920 return (ds == NULL ||
921 dsl_dataset_block_freeable(ds, NULL, birth_txg));
927 dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx)
929 arc_buf_t *buf, *obuf;
930 int osize = db->db.db_size;
931 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
934 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
939 /* XXX does *this* func really need the lock? */
940 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
943 * This call to dbuf_will_dirty() with the dn_struct_rwlock held
944 * is OK, because there can be no other references to the db
945 * when we are changing its size, so no concurrent DB_FILL can
949 * XXX we should be doing a dbuf_read, checking the return
950 * value and returning that up to our callers
952 dbuf_will_dirty(db, tx);
954 /* create the data buffer for the new block */
955 buf = arc_buf_alloc(dn->dn_objset->os_spa, size, db, type);
957 /* copy old block data to the new block */
959 bcopy(obuf->b_data, buf->b_data, MIN(osize, size));
960 /* zero the remainder */
962 bzero((uint8_t *)buf->b_data + osize, size - osize);
964 mutex_enter(&db->db_mtx);
965 dbuf_set_data(db, buf);
966 VERIFY(arc_buf_remove_ref(obuf, db));
967 db->db.db_size = size;
969 if (db->db_level == 0) {
970 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
971 db->db_last_dirty->dt.dl.dr_data = buf;
973 mutex_exit(&db->db_mtx);
975 dnode_willuse_space(dn, size-osize, tx);
980 dbuf_release_bp(dmu_buf_impl_t *db)
984 DB_GET_OBJSET(&os, db);
985 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
986 ASSERT(arc_released(os->os_phys_buf) ||
987 list_link_active(&os->os_dsl_dataset->ds_synced_link));
988 ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf));
990 (void) arc_release(db->db_buf, db);
993 dbuf_dirty_record_t *
994 dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
998 dbuf_dirty_record_t **drp, *dr;
999 int drop_struct_lock = FALSE;
1000 boolean_t do_free_accounting = B_FALSE;
1001 int txgoff = tx->tx_txg & TXG_MASK;
1003 ASSERT(tx->tx_txg != 0);
1004 ASSERT(!refcount_is_zero(&db->db_holds));
1005 DMU_TX_DIRTY_BUF(tx, db);
1010 * Shouldn't dirty a regular buffer in syncing context. Private
1011 * objects may be dirtied in syncing context, but only if they
1012 * were already pre-dirtied in open context.
1014 ASSERT(!dmu_tx_is_syncing(tx) ||
1015 BP_IS_HOLE(dn->dn_objset->os_rootbp) ||
1016 DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1017 dn->dn_objset->os_dsl_dataset == NULL);
1019 * We make this assert for private objects as well, but after we
1020 * check if we're already dirty. They are allowed to re-dirty
1021 * in syncing context.
1023 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1024 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1025 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1027 mutex_enter(&db->db_mtx);
1029 * XXX make this true for indirects too? The problem is that
1030 * transactions created with dmu_tx_create_assigned() from
1031 * syncing context don't bother holding ahead.
1033 ASSERT(db->db_level != 0 ||
1034 db->db_state == DB_CACHED || db->db_state == DB_FILL ||
1035 db->db_state == DB_NOFILL);
1037 mutex_enter(&dn->dn_mtx);
1039 * Don't set dirtyctx to SYNC if we're just modifying this as we
1040 * initialize the objset.
1042 if (dn->dn_dirtyctx == DN_UNDIRTIED &&
1043 !BP_IS_HOLE(dn->dn_objset->os_rootbp)) {
1045 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN);
1046 ASSERT(dn->dn_dirtyctx_firstset == NULL);
1047 dn->dn_dirtyctx_firstset = kmem_alloc(1, KM_SLEEP);
1049 mutex_exit(&dn->dn_mtx);
1051 if (db->db_blkid == DMU_SPILL_BLKID)
1052 dn->dn_have_spill = B_TRUE;
1055 * If this buffer is already dirty, we're done.
1057 drp = &db->db_last_dirty;
1058 ASSERT(*drp == NULL || (*drp)->dr_txg <= tx->tx_txg ||
1059 db->db.db_object == DMU_META_DNODE_OBJECT);
1060 while ((dr = *drp) != NULL && dr->dr_txg > tx->tx_txg)
1062 if (dr && dr->dr_txg == tx->tx_txg) {
1065 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID) {
1067 * If this buffer has already been written out,
1068 * we now need to reset its state.
1070 dbuf_unoverride(dr);
1071 if (db->db.db_object != DMU_META_DNODE_OBJECT &&
1072 db->db_state != DB_NOFILL)
1073 arc_buf_thaw(db->db_buf);
1075 mutex_exit(&db->db_mtx);
1080 * Only valid if not already dirty.
1082 ASSERT(dn->dn_object == 0 ||
1083 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1084 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1086 ASSERT3U(dn->dn_nlevels, >, db->db_level);
1087 ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) ||
1088 dn->dn_phys->dn_nlevels > db->db_level ||
1089 dn->dn_next_nlevels[txgoff] > db->db_level ||
1090 dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level ||
1091 dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level);
1094 * We should only be dirtying in syncing context if it's the
1095 * mos or we're initializing the os or it's a special object.
1096 * However, we are allowed to dirty in syncing context provided
1097 * we already dirtied it in open context. Hence we must make
1098 * this assertion only if we're not already dirty.
1101 ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1102 os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp));
1103 ASSERT(db->db.db_size != 0);
1105 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1107 if (db->db_blkid != DMU_BONUS_BLKID) {
1109 * Update the accounting.
1110 * Note: we delay "free accounting" until after we drop
1111 * the db_mtx. This keeps us from grabbing other locks
1112 * (and possibly deadlocking) in bp_get_dsize() while
1113 * also holding the db_mtx.
1115 dnode_willuse_space(dn, db->db.db_size, tx);
1116 do_free_accounting = dbuf_block_freeable(db);
1120 * If this buffer is dirty in an old transaction group we need
1121 * to make a copy of it so that the changes we make in this
1122 * transaction group won't leak out when we sync the older txg.
1124 dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP);
1125 if (db->db_level == 0) {
1126 void *data_old = db->db_buf;
1128 if (db->db_state != DB_NOFILL) {
1129 if (db->db_blkid == DMU_BONUS_BLKID) {
1130 dbuf_fix_old_data(db, tx->tx_txg);
1131 data_old = db->db.db_data;
1132 } else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
1134 * Release the data buffer from the cache so
1135 * that we can modify it without impacting
1136 * possible other users of this cached data
1137 * block. Note that indirect blocks and
1138 * private objects are not released until the
1139 * syncing state (since they are only modified
1142 arc_release(db->db_buf, db);
1143 dbuf_fix_old_data(db, tx->tx_txg);
1144 data_old = db->db_buf;
1146 ASSERT(data_old != NULL);
1148 dr->dt.dl.dr_data = data_old;
1150 mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_DEFAULT, NULL);
1151 list_create(&dr->dt.di.dr_children,
1152 sizeof (dbuf_dirty_record_t),
1153 offsetof(dbuf_dirty_record_t, dr_dirty_node));
1156 dr->dr_txg = tx->tx_txg;
1161 * We could have been freed_in_flight between the dbuf_noread
1162 * and dbuf_dirty. We win, as though the dbuf_noread() had
1163 * happened after the free.
1165 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1166 db->db_blkid != DMU_SPILL_BLKID) {
1167 mutex_enter(&dn->dn_mtx);
1168 dnode_clear_range(dn, db->db_blkid, 1, tx);
1169 mutex_exit(&dn->dn_mtx);
1170 db->db_freed_in_flight = FALSE;
1174 * This buffer is now part of this txg
1176 dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg);
1177 db->db_dirtycnt += 1;
1178 ASSERT3U(db->db_dirtycnt, <=, 3);
1180 mutex_exit(&db->db_mtx);
1182 if (db->db_blkid == DMU_BONUS_BLKID ||
1183 db->db_blkid == DMU_SPILL_BLKID) {
1184 mutex_enter(&dn->dn_mtx);
1185 ASSERT(!list_link_active(&dr->dr_dirty_node));
1186 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1187 mutex_exit(&dn->dn_mtx);
1188 dnode_setdirty(dn, tx);
1191 } else if (do_free_accounting) {
1192 blkptr_t *bp = db->db_blkptr;
1193 int64_t willfree = (bp && !BP_IS_HOLE(bp)) ?
1194 bp_get_dsize(os->os_spa, bp) : db->db.db_size;
1196 * This is only a guess -- if the dbuf is dirty
1197 * in a previous txg, we don't know how much
1198 * space it will use on disk yet. We should
1199 * really have the struct_rwlock to access
1200 * db_blkptr, but since this is just a guess,
1201 * it's OK if we get an odd answer.
1203 ddt_prefetch(os->os_spa, bp);
1204 dnode_willuse_space(dn, -willfree, tx);
1207 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
1208 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1209 drop_struct_lock = TRUE;
1212 if (db->db_level == 0) {
1213 dnode_new_blkid(dn, db->db_blkid, tx, drop_struct_lock);
1214 ASSERT(dn->dn_maxblkid >= db->db_blkid);
1217 if (db->db_level+1 < dn->dn_nlevels) {
1218 dmu_buf_impl_t *parent = db->db_parent;
1219 dbuf_dirty_record_t *di;
1220 int parent_held = FALSE;
1222 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
1223 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1225 parent = dbuf_hold_level(dn, db->db_level+1,
1226 db->db_blkid >> epbs, FTAG);
1227 ASSERT(parent != NULL);
1230 if (drop_struct_lock)
1231 rw_exit(&dn->dn_struct_rwlock);
1232 ASSERT3U(db->db_level+1, ==, parent->db_level);
1233 di = dbuf_dirty(parent, tx);
1235 dbuf_rele(parent, FTAG);
1237 mutex_enter(&db->db_mtx);
1238 /* possible race with dbuf_undirty() */
1239 if (db->db_last_dirty == dr ||
1240 dn->dn_object == DMU_META_DNODE_OBJECT) {
1241 mutex_enter(&di->dt.di.dr_mtx);
1242 ASSERT3U(di->dr_txg, ==, tx->tx_txg);
1243 ASSERT(!list_link_active(&dr->dr_dirty_node));
1244 list_insert_tail(&di->dt.di.dr_children, dr);
1245 mutex_exit(&di->dt.di.dr_mtx);
1248 mutex_exit(&db->db_mtx);
1250 ASSERT(db->db_level+1 == dn->dn_nlevels);
1251 ASSERT(db->db_blkid < dn->dn_nblkptr);
1252 ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf);
1253 mutex_enter(&dn->dn_mtx);
1254 ASSERT(!list_link_active(&dr->dr_dirty_node));
1255 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1256 mutex_exit(&dn->dn_mtx);
1257 if (drop_struct_lock)
1258 rw_exit(&dn->dn_struct_rwlock);
1261 dnode_setdirty(dn, tx);
1267 * Return TRUE if this evicted the dbuf.
1270 dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1273 uint64_t txg = tx->tx_txg;
1274 dbuf_dirty_record_t *dr, **drp;
1277 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1278 ASSERT0(db->db_level);
1279 ASSERT(MUTEX_HELD(&db->db_mtx));
1282 * If this buffer is not dirty, we're done.
1284 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1285 if (dr->dr_txg <= txg)
1287 if (dr == NULL || dr->dr_txg < txg)
1289 ASSERT(dr->dr_txg == txg);
1290 ASSERT(dr->dr_dbuf == db);
1296 * Note: This code will probably work even if there are concurrent
1297 * holders, but it is untested in that scenerio, as the ZPL and
1298 * ztest have additional locking (the range locks) that prevents
1299 * that type of concurrent access.
1301 ASSERT3U(refcount_count(&db->db_holds), ==, db->db_dirtycnt);
1303 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1305 ASSERT(db->db.db_size != 0);
1307 /* XXX would be nice to fix up dn_towrite_space[] */
1312 * Note that there are three places in dbuf_dirty()
1313 * where this dirty record may be put on a list.
1314 * Make sure to do a list_remove corresponding to
1315 * every one of those list_insert calls.
1317 if (dr->dr_parent) {
1318 mutex_enter(&dr->dr_parent->dt.di.dr_mtx);
1319 list_remove(&dr->dr_parent->dt.di.dr_children, dr);
1320 mutex_exit(&dr->dr_parent->dt.di.dr_mtx);
1321 } else if (db->db_blkid == DMU_SPILL_BLKID ||
1322 db->db_level+1 == dn->dn_nlevels) {
1323 ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf);
1324 mutex_enter(&dn->dn_mtx);
1325 list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr);
1326 mutex_exit(&dn->dn_mtx);
1330 if (db->db_state != DB_NOFILL) {
1331 dbuf_unoverride(dr);
1333 ASSERT(db->db_buf != NULL);
1334 ASSERT(dr->dt.dl.dr_data != NULL);
1335 if (dr->dt.dl.dr_data != db->db_buf)
1336 VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data, db));
1338 kmem_free(dr, sizeof (dbuf_dirty_record_t));
1340 ASSERT(db->db_dirtycnt > 0);
1341 db->db_dirtycnt -= 1;
1343 if (refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) {
1344 arc_buf_t *buf = db->db_buf;
1346 ASSERT(db->db_state == DB_NOFILL || arc_released(buf));
1347 dbuf_set_data(db, NULL);
1348 VERIFY(arc_buf_remove_ref(buf, db));
1356 #pragma weak dmu_buf_will_dirty = dbuf_will_dirty
1358 dbuf_will_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1360 int rf = DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH;
1362 ASSERT(tx->tx_txg != 0);
1363 ASSERT(!refcount_is_zero(&db->db_holds));
1366 if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock))
1367 rf |= DB_RF_HAVESTRUCT;
1369 (void) dbuf_read(db, NULL, rf);
1370 (void) dbuf_dirty(db, tx);
1374 dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1376 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1378 db->db_state = DB_NOFILL;
1380 dmu_buf_will_fill(db_fake, tx);
1384 dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1386 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1388 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1389 ASSERT(tx->tx_txg != 0);
1390 ASSERT(db->db_level == 0);
1391 ASSERT(!refcount_is_zero(&db->db_holds));
1393 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
1394 dmu_tx_private_ok(tx));
1397 (void) dbuf_dirty(db, tx);
1400 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1403 dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx)
1405 mutex_enter(&db->db_mtx);
1408 if (db->db_state == DB_FILL) {
1409 if (db->db_level == 0 && db->db_freed_in_flight) {
1410 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1411 /* we were freed while filling */
1412 /* XXX dbuf_undirty? */
1413 bzero(db->db.db_data, db->db.db_size);
1414 db->db_freed_in_flight = FALSE;
1416 db->db_state = DB_CACHED;
1417 cv_broadcast(&db->db_changed);
1419 mutex_exit(&db->db_mtx);
1423 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1424 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1427 dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx)
1429 ASSERT(!refcount_is_zero(&db->db_holds));
1430 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1431 ASSERT(db->db_level == 0);
1432 ASSERT(DBUF_GET_BUFC_TYPE(db) == ARC_BUFC_DATA);
1433 ASSERT(buf != NULL);
1434 ASSERT(arc_buf_size(buf) == db->db.db_size);
1435 ASSERT(tx->tx_txg != 0);
1437 arc_return_buf(buf, db);
1438 ASSERT(arc_released(buf));
1440 mutex_enter(&db->db_mtx);
1442 while (db->db_state == DB_READ || db->db_state == DB_FILL)
1443 cv_wait(&db->db_changed, &db->db_mtx);
1445 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED);
1447 if (db->db_state == DB_CACHED &&
1448 refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) {
1449 mutex_exit(&db->db_mtx);
1450 (void) dbuf_dirty(db, tx);
1451 bcopy(buf->b_data, db->db.db_data, db->db.db_size);
1452 VERIFY(arc_buf_remove_ref(buf, db));
1453 xuio_stat_wbuf_copied();
1457 xuio_stat_wbuf_nocopy();
1458 if (db->db_state == DB_CACHED) {
1459 dbuf_dirty_record_t *dr = db->db_last_dirty;
1461 ASSERT(db->db_buf != NULL);
1462 if (dr != NULL && dr->dr_txg == tx->tx_txg) {
1463 ASSERT(dr->dt.dl.dr_data == db->db_buf);
1464 if (!arc_released(db->db_buf)) {
1465 ASSERT(dr->dt.dl.dr_override_state ==
1467 arc_release(db->db_buf, db);
1469 dr->dt.dl.dr_data = buf;
1470 VERIFY(arc_buf_remove_ref(db->db_buf, db));
1471 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) {
1472 arc_release(db->db_buf, db);
1473 VERIFY(arc_buf_remove_ref(db->db_buf, db));
1477 ASSERT(db->db_buf == NULL);
1478 dbuf_set_data(db, buf);
1479 db->db_state = DB_FILL;
1480 mutex_exit(&db->db_mtx);
1481 (void) dbuf_dirty(db, tx);
1482 dbuf_fill_done(db, tx);
1486 * "Clear" the contents of this dbuf. This will mark the dbuf
1487 * EVICTING and clear *most* of its references. Unfortunetely,
1488 * when we are not holding the dn_dbufs_mtx, we can't clear the
1489 * entry in the dn_dbufs list. We have to wait until dbuf_destroy()
1490 * in this case. For callers from the DMU we will usually see:
1491 * dbuf_clear()->arc_buf_evict()->dbuf_do_evict()->dbuf_destroy()
1492 * For the arc callback, we will usually see:
1493 * dbuf_do_evict()->dbuf_clear();dbuf_destroy()
1494 * Sometimes, though, we will get a mix of these two:
1495 * DMU: dbuf_clear()->arc_buf_evict()
1496 * ARC: dbuf_do_evict()->dbuf_destroy()
1499 dbuf_clear(dmu_buf_impl_t *db)
1502 dmu_buf_impl_t *parent = db->db_parent;
1503 dmu_buf_impl_t *dndb;
1504 int dbuf_gone = FALSE;
1506 ASSERT(MUTEX_HELD(&db->db_mtx));
1507 ASSERT(refcount_is_zero(&db->db_holds));
1509 dbuf_evict_user(db);
1511 if (db->db_state == DB_CACHED) {
1512 ASSERT(db->db.db_data != NULL);
1513 if (db->db_blkid == DMU_BONUS_BLKID) {
1514 zio_buf_free(db->db.db_data, DN_MAX_BONUSLEN);
1515 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
1517 db->db.db_data = NULL;
1518 db->db_state = DB_UNCACHED;
1521 ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL);
1522 ASSERT(db->db_data_pending == NULL);
1524 db->db_state = DB_EVICTING;
1525 db->db_blkptr = NULL;
1530 if (db->db_blkid != DMU_BONUS_BLKID && MUTEX_HELD(&dn->dn_dbufs_mtx)) {
1531 list_remove(&dn->dn_dbufs, db);
1532 (void) atomic_dec_32_nv(&dn->dn_dbufs_count);
1536 * Decrementing the dbuf count means that the hold corresponding
1537 * to the removed dbuf is no longer discounted in dnode_move(),
1538 * so the dnode cannot be moved until after we release the hold.
1539 * The membar_producer() ensures visibility of the decremented
1540 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
1544 db->db_dnode_handle = NULL;
1550 dbuf_gone = arc_buf_evict(db->db_buf);
1553 mutex_exit(&db->db_mtx);
1556 * If this dbuf is referenced from an indirect dbuf,
1557 * decrement the ref count on the indirect dbuf.
1559 if (parent && parent != dndb)
1560 dbuf_rele(parent, db);
1564 dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse,
1565 dmu_buf_impl_t **parentp, blkptr_t **bpp)
1572 ASSERT(blkid != DMU_BONUS_BLKID);
1574 if (blkid == DMU_SPILL_BLKID) {
1575 mutex_enter(&dn->dn_mtx);
1576 if (dn->dn_have_spill &&
1577 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
1578 *bpp = &dn->dn_phys->dn_spill;
1581 dbuf_add_ref(dn->dn_dbuf, NULL);
1582 *parentp = dn->dn_dbuf;
1583 mutex_exit(&dn->dn_mtx);
1587 if (dn->dn_phys->dn_nlevels == 0)
1590 nlevels = dn->dn_phys->dn_nlevels;
1592 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1594 ASSERT3U(level * epbs, <, 64);
1595 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1596 if (level >= nlevels ||
1597 (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) {
1598 /* the buffer has no parent yet */
1599 return (SET_ERROR(ENOENT));
1600 } else if (level < nlevels-1) {
1601 /* this block is referenced from an indirect block */
1602 int err = dbuf_hold_impl(dn, level+1,
1603 blkid >> epbs, fail_sparse, NULL, parentp);
1606 err = dbuf_read(*parentp, NULL,
1607 (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL));
1609 dbuf_rele(*parentp, NULL);
1613 *bpp = ((blkptr_t *)(*parentp)->db.db_data) +
1614 (blkid & ((1ULL << epbs) - 1));
1617 /* the block is referenced from the dnode */
1618 ASSERT3U(level, ==, nlevels-1);
1619 ASSERT(dn->dn_phys->dn_nblkptr == 0 ||
1620 blkid < dn->dn_phys->dn_nblkptr);
1622 dbuf_add_ref(dn->dn_dbuf, NULL);
1623 *parentp = dn->dn_dbuf;
1625 *bpp = &dn->dn_phys->dn_blkptr[blkid];
1630 static dmu_buf_impl_t *
1631 dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid,
1632 dmu_buf_impl_t *parent, blkptr_t *blkptr)
1634 objset_t *os = dn->dn_objset;
1635 dmu_buf_impl_t *db, *odb;
1637 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1638 ASSERT(dn->dn_type != DMU_OT_NONE);
1640 db = kmem_cache_alloc(dbuf_cache, KM_SLEEP);
1643 db->db.db_object = dn->dn_object;
1644 db->db_level = level;
1645 db->db_blkid = blkid;
1646 db->db_last_dirty = NULL;
1647 db->db_dirtycnt = 0;
1648 db->db_dnode_handle = dn->dn_handle;
1649 db->db_parent = parent;
1650 db->db_blkptr = blkptr;
1652 db->db_user_ptr = NULL;
1653 db->db_user_data_ptr_ptr = NULL;
1654 db->db_evict_func = NULL;
1655 db->db_immediate_evict = 0;
1656 db->db_freed_in_flight = 0;
1658 if (blkid == DMU_BONUS_BLKID) {
1659 ASSERT3P(parent, ==, dn->dn_dbuf);
1660 db->db.db_size = DN_MAX_BONUSLEN -
1661 (dn->dn_nblkptr-1) * sizeof (blkptr_t);
1662 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
1663 db->db.db_offset = DMU_BONUS_BLKID;
1664 db->db_state = DB_UNCACHED;
1665 /* the bonus dbuf is not placed in the hash table */
1666 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1668 } else if (blkid == DMU_SPILL_BLKID) {
1669 db->db.db_size = (blkptr != NULL) ?
1670 BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE;
1671 db->db.db_offset = 0;
1674 db->db_level ? 1<<dn->dn_indblkshift : dn->dn_datablksz;
1675 db->db.db_size = blocksize;
1676 db->db.db_offset = db->db_blkid * blocksize;
1680 * Hold the dn_dbufs_mtx while we get the new dbuf
1681 * in the hash table *and* added to the dbufs list.
1682 * This prevents a possible deadlock with someone
1683 * trying to look up this dbuf before its added to the
1686 mutex_enter(&dn->dn_dbufs_mtx);
1687 db->db_state = DB_EVICTING;
1688 if ((odb = dbuf_hash_insert(db)) != NULL) {
1689 /* someone else inserted it first */
1690 kmem_cache_free(dbuf_cache, db);
1691 mutex_exit(&dn->dn_dbufs_mtx);
1694 list_insert_head(&dn->dn_dbufs, db);
1695 db->db_state = DB_UNCACHED;
1696 mutex_exit(&dn->dn_dbufs_mtx);
1697 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1699 if (parent && parent != dn->dn_dbuf)
1700 dbuf_add_ref(parent, db);
1702 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1703 refcount_count(&dn->dn_holds) > 0);
1704 (void) refcount_add(&dn->dn_holds, db);
1705 (void) atomic_inc_32_nv(&dn->dn_dbufs_count);
1707 dprintf_dbuf(db, "db=%p\n", db);
1713 dbuf_do_evict(void *private)
1715 arc_buf_t *buf = private;
1716 dmu_buf_impl_t *db = buf->b_private;
1718 if (!MUTEX_HELD(&db->db_mtx))
1719 mutex_enter(&db->db_mtx);
1721 ASSERT(refcount_is_zero(&db->db_holds));
1723 if (db->db_state != DB_EVICTING) {
1724 ASSERT(db->db_state == DB_CACHED);
1729 mutex_exit(&db->db_mtx);
1736 dbuf_destroy(dmu_buf_impl_t *db)
1738 ASSERT(refcount_is_zero(&db->db_holds));
1740 if (db->db_blkid != DMU_BONUS_BLKID) {
1742 * If this dbuf is still on the dn_dbufs list,
1743 * remove it from that list.
1745 if (db->db_dnode_handle != NULL) {
1750 mutex_enter(&dn->dn_dbufs_mtx);
1751 list_remove(&dn->dn_dbufs, db);
1752 (void) atomic_dec_32_nv(&dn->dn_dbufs_count);
1753 mutex_exit(&dn->dn_dbufs_mtx);
1756 * Decrementing the dbuf count means that the hold
1757 * corresponding to the removed dbuf is no longer
1758 * discounted in dnode_move(), so the dnode cannot be
1759 * moved until after we release the hold.
1762 db->db_dnode_handle = NULL;
1764 dbuf_hash_remove(db);
1766 db->db_parent = NULL;
1769 ASSERT(!list_link_active(&db->db_link));
1770 ASSERT(db->db.db_data == NULL);
1771 ASSERT(db->db_hash_next == NULL);
1772 ASSERT(db->db_blkptr == NULL);
1773 ASSERT(db->db_data_pending == NULL);
1775 kmem_cache_free(dbuf_cache, db);
1776 arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1780 dbuf_prefetch(dnode_t *dn, uint64_t blkid)
1782 dmu_buf_impl_t *db = NULL;
1783 blkptr_t *bp = NULL;
1785 ASSERT(blkid != DMU_BONUS_BLKID);
1786 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1788 if (dnode_block_freed(dn, blkid))
1791 /* dbuf_find() returns with db_mtx held */
1792 if (db = dbuf_find(dn, 0, blkid)) {
1794 * This dbuf is already in the cache. We assume that
1795 * it is already CACHED, or else about to be either
1798 mutex_exit(&db->db_mtx);
1802 if (dbuf_findbp(dn, 0, blkid, TRUE, &db, &bp) == 0) {
1803 if (bp && !BP_IS_HOLE(bp)) {
1804 int priority = dn->dn_type == DMU_OT_DDT_ZAP ?
1805 ZIO_PRIORITY_DDT_PREFETCH : ZIO_PRIORITY_ASYNC_READ;
1806 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
1807 uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
1810 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
1811 dn->dn_object, 0, blkid);
1813 (void) arc_read(NULL, dn->dn_objset->os_spa,
1814 bp, NULL, NULL, priority,
1815 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
1819 dbuf_rele(db, NULL);
1824 * Returns with db_holds incremented, and db_mtx not held.
1825 * Note: dn_struct_rwlock must be held.
1828 dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid, int fail_sparse,
1829 void *tag, dmu_buf_impl_t **dbp)
1831 dmu_buf_impl_t *db, *parent = NULL;
1833 ASSERT(blkid != DMU_BONUS_BLKID);
1834 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1835 ASSERT3U(dn->dn_nlevels, >, level);
1839 /* dbuf_find() returns with db_mtx held */
1840 db = dbuf_find(dn, level, blkid);
1843 blkptr_t *bp = NULL;
1846 ASSERT3P(parent, ==, NULL);
1847 err = dbuf_findbp(dn, level, blkid, fail_sparse, &parent, &bp);
1849 if (err == 0 && bp && BP_IS_HOLE(bp))
1850 err = SET_ERROR(ENOENT);
1853 dbuf_rele(parent, NULL);
1857 if (err && err != ENOENT)
1859 db = dbuf_create(dn, level, blkid, parent, bp);
1862 if (db->db_buf && refcount_is_zero(&db->db_holds)) {
1863 arc_buf_add_ref(db->db_buf, db);
1864 if (db->db_buf->b_data == NULL) {
1867 dbuf_rele(parent, NULL);
1872 ASSERT3P(db->db.db_data, ==, db->db_buf->b_data);
1875 ASSERT(db->db_buf == NULL || arc_referenced(db->db_buf));
1878 * If this buffer is currently syncing out, and we are are
1879 * still referencing it from db_data, we need to make a copy
1880 * of it in case we decide we want to dirty it again in this txg.
1882 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1883 dn->dn_object != DMU_META_DNODE_OBJECT &&
1884 db->db_state == DB_CACHED && db->db_data_pending) {
1885 dbuf_dirty_record_t *dr = db->db_data_pending;
1887 if (dr->dt.dl.dr_data == db->db_buf) {
1888 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1891 arc_buf_alloc(dn->dn_objset->os_spa,
1892 db->db.db_size, db, type));
1893 bcopy(dr->dt.dl.dr_data->b_data, db->db.db_data,
1898 (void) refcount_add(&db->db_holds, tag);
1899 dbuf_update_data(db);
1901 mutex_exit(&db->db_mtx);
1903 /* NOTE: we can't rele the parent until after we drop the db_mtx */
1905 dbuf_rele(parent, NULL);
1907 ASSERT3P(DB_DNODE(db), ==, dn);
1908 ASSERT3U(db->db_blkid, ==, blkid);
1909 ASSERT3U(db->db_level, ==, level);
1916 dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag)
1919 int err = dbuf_hold_impl(dn, 0, blkid, FALSE, tag, &db);
1920 return (err ? NULL : db);
1924 dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag)
1927 int err = dbuf_hold_impl(dn, level, blkid, FALSE, tag, &db);
1928 return (err ? NULL : db);
1932 dbuf_create_bonus(dnode_t *dn)
1934 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
1936 ASSERT(dn->dn_bonus == NULL);
1937 dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL);
1941 dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx)
1943 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1946 if (db->db_blkid != DMU_SPILL_BLKID)
1947 return (SET_ERROR(ENOTSUP));
1949 blksz = SPA_MINBLOCKSIZE;
1950 if (blksz > SPA_MAXBLOCKSIZE)
1951 blksz = SPA_MAXBLOCKSIZE;
1953 blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE);
1957 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1958 dbuf_new_size(db, blksz, tx);
1959 rw_exit(&dn->dn_struct_rwlock);
1966 dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx)
1968 dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx);
1971 #pragma weak dmu_buf_add_ref = dbuf_add_ref
1973 dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
1975 int64_t holds = refcount_add(&db->db_holds, tag);
1980 * If you call dbuf_rele() you had better not be referencing the dnode handle
1981 * unless you have some other direct or indirect hold on the dnode. (An indirect
1982 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
1983 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
1984 * dnode's parent dbuf evicting its dnode handles.
1986 #pragma weak dmu_buf_rele = dbuf_rele
1988 dbuf_rele(dmu_buf_impl_t *db, void *tag)
1990 mutex_enter(&db->db_mtx);
1991 dbuf_rele_and_unlock(db, tag);
1995 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
1996 * db_dirtycnt and db_holds to be updated atomically.
1999 dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag)
2003 ASSERT(MUTEX_HELD(&db->db_mtx));
2007 * Remove the reference to the dbuf before removing its hold on the
2008 * dnode so we can guarantee in dnode_move() that a referenced bonus
2009 * buffer has a corresponding dnode hold.
2011 holds = refcount_remove(&db->db_holds, tag);
2015 * We can't freeze indirects if there is a possibility that they
2016 * may be modified in the current syncing context.
2018 if (db->db_buf && holds == (db->db_level == 0 ? db->db_dirtycnt : 0))
2019 arc_buf_freeze(db->db_buf);
2021 if (holds == db->db_dirtycnt &&
2022 db->db_level == 0 && db->db_immediate_evict)
2023 dbuf_evict_user(db);
2026 if (db->db_blkid == DMU_BONUS_BLKID) {
2027 mutex_exit(&db->db_mtx);
2030 * If the dnode moves here, we cannot cross this barrier
2031 * until the move completes.
2034 (void) atomic_dec_32_nv(&DB_DNODE(db)->dn_dbufs_count);
2037 * The bonus buffer's dnode hold is no longer discounted
2038 * in dnode_move(). The dnode cannot move until after
2041 dnode_rele(DB_DNODE(db), db);
2042 } else if (db->db_buf == NULL) {
2044 * This is a special case: we never associated this
2045 * dbuf with any data allocated from the ARC.
2047 ASSERT(db->db_state == DB_UNCACHED ||
2048 db->db_state == DB_NOFILL);
2050 } else if (arc_released(db->db_buf)) {
2051 arc_buf_t *buf = db->db_buf;
2053 * This dbuf has anonymous data associated with it.
2055 dbuf_set_data(db, NULL);
2056 VERIFY(arc_buf_remove_ref(buf, db));
2059 VERIFY(!arc_buf_remove_ref(db->db_buf, db));
2062 * A dbuf will be eligible for eviction if either the
2063 * 'primarycache' property is set or a duplicate
2064 * copy of this buffer is already cached in the arc.
2066 * In the case of the 'primarycache' a buffer
2067 * is considered for eviction if it matches the
2068 * criteria set in the property.
2070 * To decide if our buffer is considered a
2071 * duplicate, we must call into the arc to determine
2072 * if multiple buffers are referencing the same
2073 * block on-disk. If so, then we simply evict
2076 if (!DBUF_IS_CACHEABLE(db) ||
2077 arc_buf_eviction_needed(db->db_buf))
2080 mutex_exit(&db->db_mtx);
2083 mutex_exit(&db->db_mtx);
2087 #pragma weak dmu_buf_refcount = dbuf_refcount
2089 dbuf_refcount(dmu_buf_impl_t *db)
2091 return (refcount_count(&db->db_holds));
2095 dmu_buf_set_user(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr,
2096 dmu_buf_evict_func_t *evict_func)
2098 return (dmu_buf_update_user(db_fake, NULL, user_ptr,
2099 user_data_ptr_ptr, evict_func));
2103 dmu_buf_set_user_ie(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr,
2104 dmu_buf_evict_func_t *evict_func)
2106 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2108 db->db_immediate_evict = TRUE;
2109 return (dmu_buf_update_user(db_fake, NULL, user_ptr,
2110 user_data_ptr_ptr, evict_func));
2114 dmu_buf_update_user(dmu_buf_t *db_fake, void *old_user_ptr, void *user_ptr,
2115 void *user_data_ptr_ptr, dmu_buf_evict_func_t *evict_func)
2117 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2118 ASSERT(db->db_level == 0);
2120 ASSERT((user_ptr == NULL) == (evict_func == NULL));
2122 mutex_enter(&db->db_mtx);
2124 if (db->db_user_ptr == old_user_ptr) {
2125 db->db_user_ptr = user_ptr;
2126 db->db_user_data_ptr_ptr = user_data_ptr_ptr;
2127 db->db_evict_func = evict_func;
2129 dbuf_update_data(db);
2131 old_user_ptr = db->db_user_ptr;
2134 mutex_exit(&db->db_mtx);
2135 return (old_user_ptr);
2139 dmu_buf_get_user(dmu_buf_t *db_fake)
2141 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2142 ASSERT(!refcount_is_zero(&db->db_holds));
2144 return (db->db_user_ptr);
2148 dmu_buf_freeable(dmu_buf_t *dbuf)
2150 boolean_t res = B_FALSE;
2151 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
2154 res = dsl_dataset_block_freeable(db->db_objset->os_dsl_dataset,
2155 db->db_blkptr, db->db_blkptr->blk_birth);
2161 dmu_buf_get_blkptr(dmu_buf_t *db)
2163 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2164 return (dbi->db_blkptr);
2168 dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db)
2170 /* ASSERT(dmu_tx_is_syncing(tx) */
2171 ASSERT(MUTEX_HELD(&db->db_mtx));
2173 if (db->db_blkptr != NULL)
2176 if (db->db_blkid == DMU_SPILL_BLKID) {
2177 db->db_blkptr = &dn->dn_phys->dn_spill;
2178 BP_ZERO(db->db_blkptr);
2181 if (db->db_level == dn->dn_phys->dn_nlevels-1) {
2183 * This buffer was allocated at a time when there was
2184 * no available blkptrs from the dnode, or it was
2185 * inappropriate to hook it in (i.e., nlevels mis-match).
2187 ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr);
2188 ASSERT(db->db_parent == NULL);
2189 db->db_parent = dn->dn_dbuf;
2190 db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
2193 dmu_buf_impl_t *parent = db->db_parent;
2194 int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2196 ASSERT(dn->dn_phys->dn_nlevels > 1);
2197 if (parent == NULL) {
2198 mutex_exit(&db->db_mtx);
2199 rw_enter(&dn->dn_struct_rwlock, RW_READER);
2200 (void) dbuf_hold_impl(dn, db->db_level+1,
2201 db->db_blkid >> epbs, FALSE, db, &parent);
2202 rw_exit(&dn->dn_struct_rwlock);
2203 mutex_enter(&db->db_mtx);
2204 db->db_parent = parent;
2206 db->db_blkptr = (blkptr_t *)parent->db.db_data +
2207 (db->db_blkid & ((1ULL << epbs) - 1));
2213 dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2215 dmu_buf_impl_t *db = dr->dr_dbuf;
2219 ASSERT(dmu_tx_is_syncing(tx));
2221 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2223 mutex_enter(&db->db_mtx);
2225 ASSERT(db->db_level > 0);
2228 if (db->db_buf == NULL) {
2229 mutex_exit(&db->db_mtx);
2230 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
2231 mutex_enter(&db->db_mtx);
2233 ASSERT3U(db->db_state, ==, DB_CACHED);
2234 ASSERT(db->db_buf != NULL);
2238 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2239 dbuf_check_blkptr(dn, db);
2242 db->db_data_pending = dr;
2244 mutex_exit(&db->db_mtx);
2245 dbuf_write(dr, db->db_buf, tx);
2248 mutex_enter(&dr->dt.di.dr_mtx);
2249 dbuf_sync_list(&dr->dt.di.dr_children, tx);
2250 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
2251 mutex_exit(&dr->dt.di.dr_mtx);
2256 dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2258 arc_buf_t **datap = &dr->dt.dl.dr_data;
2259 dmu_buf_impl_t *db = dr->dr_dbuf;
2262 uint64_t txg = tx->tx_txg;
2264 ASSERT(dmu_tx_is_syncing(tx));
2266 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2268 mutex_enter(&db->db_mtx);
2270 * To be synced, we must be dirtied. But we
2271 * might have been freed after the dirty.
2273 if (db->db_state == DB_UNCACHED) {
2274 /* This buffer has been freed since it was dirtied */
2275 ASSERT(db->db.db_data == NULL);
2276 } else if (db->db_state == DB_FILL) {
2277 /* This buffer was freed and is now being re-filled */
2278 ASSERT(db->db.db_data != dr->dt.dl.dr_data);
2280 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL);
2287 if (db->db_blkid == DMU_SPILL_BLKID) {
2288 mutex_enter(&dn->dn_mtx);
2289 dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR;
2290 mutex_exit(&dn->dn_mtx);
2294 * If this is a bonus buffer, simply copy the bonus data into the
2295 * dnode. It will be written out when the dnode is synced (and it
2296 * will be synced, since it must have been dirty for dbuf_sync to
2299 if (db->db_blkid == DMU_BONUS_BLKID) {
2300 dbuf_dirty_record_t **drp;
2302 ASSERT(*datap != NULL);
2303 ASSERT0(db->db_level);
2304 ASSERT3U(dn->dn_phys->dn_bonuslen, <=, DN_MAX_BONUSLEN);
2305 bcopy(*datap, DN_BONUS(dn->dn_phys), dn->dn_phys->dn_bonuslen);
2308 if (*datap != db->db.db_data) {
2309 zio_buf_free(*datap, DN_MAX_BONUSLEN);
2310 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
2312 db->db_data_pending = NULL;
2313 drp = &db->db_last_dirty;
2315 drp = &(*drp)->dr_next;
2316 ASSERT(dr->dr_next == NULL);
2317 ASSERT(dr->dr_dbuf == db);
2319 kmem_free(dr, sizeof (dbuf_dirty_record_t));
2320 ASSERT(db->db_dirtycnt > 0);
2321 db->db_dirtycnt -= 1;
2322 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
2329 * This function may have dropped the db_mtx lock allowing a dmu_sync
2330 * operation to sneak in. As a result, we need to ensure that we
2331 * don't check the dr_override_state until we have returned from
2332 * dbuf_check_blkptr.
2334 dbuf_check_blkptr(dn, db);
2337 * If this buffer is in the middle of an immediate write,
2338 * wait for the synchronous IO to complete.
2340 while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
2341 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
2342 cv_wait(&db->db_changed, &db->db_mtx);
2343 ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN);
2346 if (db->db_state != DB_NOFILL &&
2347 dn->dn_object != DMU_META_DNODE_OBJECT &&
2348 refcount_count(&db->db_holds) > 1 &&
2349 dr->dt.dl.dr_override_state != DR_OVERRIDDEN &&
2350 *datap == db->db_buf) {
2352 * If this buffer is currently "in use" (i.e., there
2353 * are active holds and db_data still references it),
2354 * then make a copy before we start the write so that
2355 * any modifications from the open txg will not leak
2358 * NOTE: this copy does not need to be made for
2359 * objects only modified in the syncing context (e.g.
2360 * DNONE_DNODE blocks).
2362 int blksz = arc_buf_size(*datap);
2363 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
2364 *datap = arc_buf_alloc(os->os_spa, blksz, db, type);
2365 bcopy(db->db.db_data, (*datap)->b_data, blksz);
2367 db->db_data_pending = dr;
2369 mutex_exit(&db->db_mtx);
2371 dbuf_write(dr, *datap, tx);
2373 ASSERT(!list_link_active(&dr->dr_dirty_node));
2374 if (dn->dn_object == DMU_META_DNODE_OBJECT) {
2375 list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr);
2379 * Although zio_nowait() does not "wait for an IO", it does
2380 * initiate the IO. If this is an empty write it seems plausible
2381 * that the IO could actually be completed before the nowait
2382 * returns. We need to DB_DNODE_EXIT() first in case
2383 * zio_nowait() invalidates the dbuf.
2386 zio_nowait(dr->dr_zio);
2391 dbuf_sync_list(list_t *list, dmu_tx_t *tx)
2393 dbuf_dirty_record_t *dr;
2395 while (dr = list_head(list)) {
2396 if (dr->dr_zio != NULL) {
2398 * If we find an already initialized zio then we
2399 * are processing the meta-dnode, and we have finished.
2400 * The dbufs for all dnodes are put back on the list
2401 * during processing, so that we can zio_wait()
2402 * these IOs after initiating all child IOs.
2404 ASSERT3U(dr->dr_dbuf->db.db_object, ==,
2405 DMU_META_DNODE_OBJECT);
2408 list_remove(list, dr);
2409 if (dr->dr_dbuf->db_level > 0)
2410 dbuf_sync_indirect(dr, tx);
2412 dbuf_sync_leaf(dr, tx);
2418 dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
2420 dmu_buf_impl_t *db = vdb;
2422 blkptr_t *bp = zio->io_bp;
2423 blkptr_t *bp_orig = &zio->io_bp_orig;
2424 spa_t *spa = zio->io_spa;
2429 ASSERT(db->db_blkptr == bp);
2433 delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig);
2434 dnode_diduse_space(dn, delta - zio->io_prev_space_delta);
2435 zio->io_prev_space_delta = delta;
2437 if (BP_IS_HOLE(bp)) {
2438 ASSERT(bp->blk_fill == 0);
2443 ASSERT((db->db_blkid != DMU_SPILL_BLKID &&
2444 BP_GET_TYPE(bp) == dn->dn_type) ||
2445 (db->db_blkid == DMU_SPILL_BLKID &&
2446 BP_GET_TYPE(bp) == dn->dn_bonustype));
2447 ASSERT(BP_GET_LEVEL(bp) == db->db_level);
2449 mutex_enter(&db->db_mtx);
2452 if (db->db_blkid == DMU_SPILL_BLKID) {
2453 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
2454 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
2455 db->db_blkptr == &dn->dn_phys->dn_spill);
2459 if (db->db_level == 0) {
2460 mutex_enter(&dn->dn_mtx);
2461 if (db->db_blkid > dn->dn_phys->dn_maxblkid &&
2462 db->db_blkid != DMU_SPILL_BLKID)
2463 dn->dn_phys->dn_maxblkid = db->db_blkid;
2464 mutex_exit(&dn->dn_mtx);
2466 if (dn->dn_type == DMU_OT_DNODE) {
2467 dnode_phys_t *dnp = db->db.db_data;
2468 for (i = db->db.db_size >> DNODE_SHIFT; i > 0;
2470 if (dnp->dn_type != DMU_OT_NONE)
2477 blkptr_t *ibp = db->db.db_data;
2478 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2479 for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) {
2480 if (BP_IS_HOLE(ibp))
2482 fill += ibp->blk_fill;
2487 bp->blk_fill = fill;
2489 mutex_exit(&db->db_mtx);
2494 dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
2496 dmu_buf_impl_t *db = vdb;
2497 blkptr_t *bp = zio->io_bp;
2498 blkptr_t *bp_orig = &zio->io_bp_orig;
2499 uint64_t txg = zio->io_txg;
2500 dbuf_dirty_record_t **drp, *dr;
2502 ASSERT0(zio->io_error);
2503 ASSERT(db->db_blkptr == bp);
2506 * For nopwrites and rewrites we ensure that the bp matches our
2507 * original and bypass all the accounting.
2509 if (zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)) {
2510 ASSERT(BP_EQUAL(bp, bp_orig));
2516 DB_GET_OBJSET(&os, db);
2517 ds = os->os_dsl_dataset;
2520 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
2521 dsl_dataset_block_born(ds, bp, tx);
2524 mutex_enter(&db->db_mtx);
2528 drp = &db->db_last_dirty;
2529 while ((dr = *drp) != db->db_data_pending)
2531 ASSERT(!list_link_active(&dr->dr_dirty_node));
2532 ASSERT(dr->dr_txg == txg);
2533 ASSERT(dr->dr_dbuf == db);
2534 ASSERT(dr->dr_next == NULL);
2538 if (db->db_blkid == DMU_SPILL_BLKID) {
2543 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
2544 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
2545 db->db_blkptr == &dn->dn_phys->dn_spill);
2550 if (db->db_level == 0) {
2551 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2552 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
2553 if (db->db_state != DB_NOFILL) {
2554 if (dr->dt.dl.dr_data != db->db_buf)
2555 VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data,
2557 else if (!arc_released(db->db_buf))
2558 arc_set_callback(db->db_buf, dbuf_do_evict, db);
2565 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
2566 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2567 if (!BP_IS_HOLE(db->db_blkptr)) {
2569 dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2570 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
2572 ASSERT3U(dn->dn_phys->dn_maxblkid
2573 >> (db->db_level * epbs), >=, db->db_blkid);
2574 arc_set_callback(db->db_buf, dbuf_do_evict, db);
2577 mutex_destroy(&dr->dt.di.dr_mtx);
2578 list_destroy(&dr->dt.di.dr_children);
2580 kmem_free(dr, sizeof (dbuf_dirty_record_t));
2582 cv_broadcast(&db->db_changed);
2583 ASSERT(db->db_dirtycnt > 0);
2584 db->db_dirtycnt -= 1;
2585 db->db_data_pending = NULL;
2586 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
2590 dbuf_write_nofill_ready(zio_t *zio)
2592 dbuf_write_ready(zio, NULL, zio->io_private);
2596 dbuf_write_nofill_done(zio_t *zio)
2598 dbuf_write_done(zio, NULL, zio->io_private);
2602 dbuf_write_override_ready(zio_t *zio)
2604 dbuf_dirty_record_t *dr = zio->io_private;
2605 dmu_buf_impl_t *db = dr->dr_dbuf;
2607 dbuf_write_ready(zio, NULL, db);
2611 dbuf_write_override_done(zio_t *zio)
2613 dbuf_dirty_record_t *dr = zio->io_private;
2614 dmu_buf_impl_t *db = dr->dr_dbuf;
2615 blkptr_t *obp = &dr->dt.dl.dr_overridden_by;
2617 mutex_enter(&db->db_mtx);
2618 if (!BP_EQUAL(zio->io_bp, obp)) {
2619 if (!BP_IS_HOLE(obp))
2620 dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp);
2621 arc_release(dr->dt.dl.dr_data, db);
2623 mutex_exit(&db->db_mtx);
2625 dbuf_write_done(zio, NULL, db);
2629 dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx)
2631 dmu_buf_impl_t *db = dr->dr_dbuf;
2634 dmu_buf_impl_t *parent = db->db_parent;
2635 uint64_t txg = tx->tx_txg;
2645 if (db->db_state != DB_NOFILL) {
2646 if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) {
2648 * Private object buffers are released here rather
2649 * than in dbuf_dirty() since they are only modified
2650 * in the syncing context and we don't want the
2651 * overhead of making multiple copies of the data.
2653 if (BP_IS_HOLE(db->db_blkptr)) {
2656 dbuf_release_bp(db);
2661 if (parent != dn->dn_dbuf) {
2662 ASSERT(parent && parent->db_data_pending);
2663 ASSERT(db->db_level == parent->db_level-1);
2664 ASSERT(arc_released(parent->db_buf));
2665 zio = parent->db_data_pending->dr_zio;
2667 ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 &&
2668 db->db_blkid != DMU_SPILL_BLKID) ||
2669 (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0));
2670 if (db->db_blkid != DMU_SPILL_BLKID)
2671 ASSERT3P(db->db_blkptr, ==,
2672 &dn->dn_phys->dn_blkptr[db->db_blkid]);
2676 ASSERT(db->db_level == 0 || data == db->db_buf);
2677 ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
2680 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
2681 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
2682 db->db.db_object, db->db_level, db->db_blkid);
2684 if (db->db_blkid == DMU_SPILL_BLKID)
2686 wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
2688 dmu_write_policy(os, dn, db->db_level, wp_flag, &zp);
2691 if (db->db_level == 0 && dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
2692 ASSERT(db->db_state != DB_NOFILL);
2693 dr->dr_zio = zio_write(zio, os->os_spa, txg,
2694 db->db_blkptr, data->b_data, arc_buf_size(data), &zp,
2695 dbuf_write_override_ready, dbuf_write_override_done, dr,
2696 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
2697 mutex_enter(&db->db_mtx);
2698 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
2699 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by,
2700 dr->dt.dl.dr_copies, dr->dt.dl.dr_nopwrite);
2701 mutex_exit(&db->db_mtx);
2702 } else if (db->db_state == DB_NOFILL) {
2703 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF);
2704 dr->dr_zio = zio_write(zio, os->os_spa, txg,
2705 db->db_blkptr, NULL, db->db.db_size, &zp,
2706 dbuf_write_nofill_ready, dbuf_write_nofill_done, db,
2707 ZIO_PRIORITY_ASYNC_WRITE,
2708 ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb);
2710 ASSERT(arc_released(data));
2711 dr->dr_zio = arc_write(zio, os->os_spa, txg,
2712 db->db_blkptr, data, DBUF_IS_L2CACHEABLE(db),
2713 DBUF_IS_L2COMPRESSIBLE(db), &zp, dbuf_write_ready,
2714 dbuf_write_done, db, ZIO_PRIORITY_ASYNC_WRITE,
2715 ZIO_FLAG_MUSTSUCCEED, &zb);