4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2012 by Delphix. All rights reserved.
27 #include <sys/zfs_context.h>
29 #include <sys/dmu_impl.h>
31 #include <sys/dmu_objset.h>
32 #include <sys/dsl_dataset.h>
33 #include <sys/dsl_dir.h>
34 #include <sys/dmu_tx.h>
37 #include <sys/dmu_zfetch.h>
39 #include <sys/sa_impl.h>
41 static void dbuf_destroy(dmu_buf_impl_t *db);
42 static int dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
43 static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx);
46 * Global data structures and functions for the dbuf cache.
48 static kmem_cache_t *dbuf_cache;
52 dbuf_cons(void *vdb, void *unused, int kmflag)
54 dmu_buf_impl_t *db = vdb;
55 bzero(db, sizeof (dmu_buf_impl_t));
57 mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL);
58 cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL);
59 refcount_create(&db->db_holds);
65 dbuf_dest(void *vdb, void *unused)
67 dmu_buf_impl_t *db = vdb;
68 mutex_destroy(&db->db_mtx);
69 cv_destroy(&db->db_changed);
70 refcount_destroy(&db->db_holds);
74 * dbuf hash table routines
76 static dbuf_hash_table_t dbuf_hash_table;
78 static uint64_t dbuf_hash_count;
81 dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid)
83 uintptr_t osv = (uintptr_t)os;
86 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
87 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (lvl)) & 0xFF];
88 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF];
89 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF];
90 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF];
91 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 0)) & 0xFF];
92 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 8)) & 0xFF];
94 crc ^= (osv>>14) ^ (obj>>16) ^ (blkid>>16);
99 #define DBUF_HASH(os, obj, level, blkid) dbuf_hash(os, obj, level, blkid);
101 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
102 ((dbuf)->db.db_object == (obj) && \
103 (dbuf)->db_objset == (os) && \
104 (dbuf)->db_level == (level) && \
105 (dbuf)->db_blkid == (blkid))
108 dbuf_find(dnode_t *dn, uint8_t level, uint64_t blkid)
110 dbuf_hash_table_t *h = &dbuf_hash_table;
111 objset_t *os = dn->dn_objset;
112 uint64_t obj = dn->dn_object;
113 uint64_t hv = DBUF_HASH(os, obj, level, blkid);
114 uint64_t idx = hv & h->hash_table_mask;
117 mutex_enter(DBUF_HASH_MUTEX(h, idx));
118 for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) {
119 if (DBUF_EQUAL(db, os, obj, level, blkid)) {
120 mutex_enter(&db->db_mtx);
121 if (db->db_state != DB_EVICTING) {
122 mutex_exit(DBUF_HASH_MUTEX(h, idx));
125 mutex_exit(&db->db_mtx);
128 mutex_exit(DBUF_HASH_MUTEX(h, idx));
133 * Insert an entry into the hash table. If there is already an element
134 * equal to elem in the hash table, then the already existing element
135 * will be returned and the new element will not be inserted.
136 * Otherwise returns NULL.
138 static dmu_buf_impl_t *
139 dbuf_hash_insert(dmu_buf_impl_t *db)
141 dbuf_hash_table_t *h = &dbuf_hash_table;
142 objset_t *os = db->db_objset;
143 uint64_t obj = db->db.db_object;
144 int level = db->db_level;
145 uint64_t blkid = db->db_blkid;
146 uint64_t hv = DBUF_HASH(os, obj, level, blkid);
147 uint64_t idx = hv & h->hash_table_mask;
150 mutex_enter(DBUF_HASH_MUTEX(h, idx));
151 for (dbf = h->hash_table[idx]; dbf != NULL; dbf = dbf->db_hash_next) {
152 if (DBUF_EQUAL(dbf, os, obj, level, blkid)) {
153 mutex_enter(&dbf->db_mtx);
154 if (dbf->db_state != DB_EVICTING) {
155 mutex_exit(DBUF_HASH_MUTEX(h, idx));
158 mutex_exit(&dbf->db_mtx);
162 mutex_enter(&db->db_mtx);
163 db->db_hash_next = h->hash_table[idx];
164 h->hash_table[idx] = db;
165 mutex_exit(DBUF_HASH_MUTEX(h, idx));
166 atomic_add_64(&dbuf_hash_count, 1);
172 * Remove an entry from the hash table. This operation will
173 * fail if there are any existing holds on the db.
176 dbuf_hash_remove(dmu_buf_impl_t *db)
178 dbuf_hash_table_t *h = &dbuf_hash_table;
179 uint64_t hv = DBUF_HASH(db->db_objset, db->db.db_object,
180 db->db_level, db->db_blkid);
181 uint64_t idx = hv & h->hash_table_mask;
182 dmu_buf_impl_t *dbf, **dbp;
185 * We musn't hold db_mtx to maintin lock ordering:
186 * DBUF_HASH_MUTEX > db_mtx.
188 ASSERT(refcount_is_zero(&db->db_holds));
189 ASSERT(db->db_state == DB_EVICTING);
190 ASSERT(!MUTEX_HELD(&db->db_mtx));
192 mutex_enter(DBUF_HASH_MUTEX(h, idx));
193 dbp = &h->hash_table[idx];
194 while ((dbf = *dbp) != db) {
195 dbp = &dbf->db_hash_next;
198 *dbp = db->db_hash_next;
199 db->db_hash_next = NULL;
200 mutex_exit(DBUF_HASH_MUTEX(h, idx));
201 atomic_add_64(&dbuf_hash_count, -1);
204 static arc_evict_func_t dbuf_do_evict;
207 dbuf_evict_user(dmu_buf_impl_t *db)
209 ASSERT(MUTEX_HELD(&db->db_mtx));
211 if (db->db_level != 0 || db->db_evict_func == NULL)
214 if (db->db_user_data_ptr_ptr)
215 *db->db_user_data_ptr_ptr = db->db.db_data;
216 db->db_evict_func(&db->db, db->db_user_ptr);
217 db->db_user_ptr = NULL;
218 db->db_user_data_ptr_ptr = NULL;
219 db->db_evict_func = NULL;
223 dbuf_is_metadata(dmu_buf_impl_t *db)
225 if (db->db_level > 0) {
228 boolean_t is_metadata;
231 is_metadata = DMU_OT_IS_METADATA(DB_DNODE(db)->dn_type);
234 return (is_metadata);
239 dbuf_evict(dmu_buf_impl_t *db)
241 ASSERT(MUTEX_HELD(&db->db_mtx));
242 ASSERT(db->db_buf == NULL);
243 ASSERT(db->db_data_pending == NULL);
252 uint64_t hsize = 1ULL << 16;
253 dbuf_hash_table_t *h = &dbuf_hash_table;
257 * The hash table is big enough to fill all of physical memory
258 * with an average 4K block size. The table will take up
259 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
261 while (hsize * 4096 < (uint64_t)physmem * PAGESIZE)
265 h->hash_table_mask = hsize - 1;
266 h->hash_table = kmem_zalloc(hsize * sizeof (void *), KM_NOSLEEP);
267 if (h->hash_table == NULL) {
268 /* XXX - we should really return an error instead of assert */
269 ASSERT(hsize > (1ULL << 10));
274 dbuf_cache = kmem_cache_create("dmu_buf_impl_t",
275 sizeof (dmu_buf_impl_t),
276 0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0);
278 for (i = 0; i < DBUF_MUTEXES; i++)
279 mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL);
285 dbuf_hash_table_t *h = &dbuf_hash_table;
288 for (i = 0; i < DBUF_MUTEXES; i++)
289 mutex_destroy(&h->hash_mutexes[i]);
290 kmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
291 kmem_cache_destroy(dbuf_cache);
300 dbuf_verify(dmu_buf_impl_t *db)
303 dbuf_dirty_record_t *dr;
305 ASSERT(MUTEX_HELD(&db->db_mtx));
307 if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY))
310 ASSERT(db->db_objset != NULL);
314 ASSERT(db->db_parent == NULL);
315 ASSERT(db->db_blkptr == NULL);
317 ASSERT3U(db->db.db_object, ==, dn->dn_object);
318 ASSERT3P(db->db_objset, ==, dn->dn_objset);
319 ASSERT3U(db->db_level, <, dn->dn_nlevels);
320 ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
321 db->db_blkid == DMU_SPILL_BLKID ||
322 !list_is_empty(&dn->dn_dbufs));
324 if (db->db_blkid == DMU_BONUS_BLKID) {
326 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
327 ASSERT3U(db->db.db_offset, ==, DMU_BONUS_BLKID);
328 } else if (db->db_blkid == DMU_SPILL_BLKID) {
330 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
331 ASSERT0(db->db.db_offset);
333 ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size);
336 for (dr = db->db_data_pending; dr != NULL; dr = dr->dr_next)
337 ASSERT(dr->dr_dbuf == db);
339 for (dr = db->db_last_dirty; dr != NULL; dr = dr->dr_next)
340 ASSERT(dr->dr_dbuf == db);
343 * We can't assert that db_size matches dn_datablksz because it
344 * can be momentarily different when another thread is doing
347 if (db->db_level == 0 && db->db.db_object == DMU_META_DNODE_OBJECT) {
348 dr = db->db_data_pending;
350 * It should only be modified in syncing context, so
351 * make sure we only have one copy of the data.
353 ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf);
356 /* verify db->db_blkptr */
358 if (db->db_parent == dn->dn_dbuf) {
359 /* db is pointed to by the dnode */
360 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
361 if (DMU_OBJECT_IS_SPECIAL(db->db.db_object))
362 ASSERT(db->db_parent == NULL);
364 ASSERT(db->db_parent != NULL);
365 if (db->db_blkid != DMU_SPILL_BLKID)
366 ASSERT3P(db->db_blkptr, ==,
367 &dn->dn_phys->dn_blkptr[db->db_blkid]);
369 /* db is pointed to by an indirect block */
370 int epb = db->db_parent->db.db_size >> SPA_BLKPTRSHIFT;
371 ASSERT3U(db->db_parent->db_level, ==, db->db_level+1);
372 ASSERT3U(db->db_parent->db.db_object, ==,
375 * dnode_grow_indblksz() can make this fail if we don't
376 * have the struct_rwlock. XXX indblksz no longer
377 * grows. safe to do this now?
379 if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
380 ASSERT3P(db->db_blkptr, ==,
381 ((blkptr_t *)db->db_parent->db.db_data +
382 db->db_blkid % epb));
386 if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) &&
387 (db->db_buf == NULL || db->db_buf->b_data) &&
388 db->db.db_data && db->db_blkid != DMU_BONUS_BLKID &&
389 db->db_state != DB_FILL && !dn->dn_free_txg) {
391 * If the blkptr isn't set but they have nonzero data,
392 * it had better be dirty, otherwise we'll lose that
393 * data when we evict this buffer.
395 if (db->db_dirtycnt == 0) {
396 uint64_t *buf = db->db.db_data;
399 for (i = 0; i < db->db.db_size >> 3; i++) {
409 dbuf_update_data(dmu_buf_impl_t *db)
411 ASSERT(MUTEX_HELD(&db->db_mtx));
412 if (db->db_level == 0 && db->db_user_data_ptr_ptr) {
413 ASSERT(!refcount_is_zero(&db->db_holds));
414 *db->db_user_data_ptr_ptr = db->db.db_data;
419 dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf)
421 ASSERT(MUTEX_HELD(&db->db_mtx));
422 ASSERT(db->db_buf == NULL || !arc_has_callback(db->db_buf));
425 ASSERT(buf->b_data != NULL);
426 db->db.db_data = buf->b_data;
427 if (!arc_released(buf))
428 arc_set_callback(buf, dbuf_do_evict, db);
429 dbuf_update_data(db);
432 db->db.db_data = NULL;
433 if (db->db_state != DB_NOFILL)
434 db->db_state = DB_UNCACHED;
439 * Loan out an arc_buf for read. Return the loaned arc_buf.
442 dbuf_loan_arcbuf(dmu_buf_impl_t *db)
446 mutex_enter(&db->db_mtx);
447 if (arc_released(db->db_buf) || refcount_count(&db->db_holds) > 1) {
448 int blksz = db->db.db_size;
451 mutex_exit(&db->db_mtx);
452 DB_GET_SPA(&spa, db);
453 abuf = arc_loan_buf(spa, blksz);
454 bcopy(db->db.db_data, abuf->b_data, blksz);
457 arc_loan_inuse_buf(abuf, db);
458 dbuf_set_data(db, NULL);
459 mutex_exit(&db->db_mtx);
465 dbuf_whichblock(dnode_t *dn, uint64_t offset)
467 if (dn->dn_datablkshift) {
468 return (offset >> dn->dn_datablkshift);
470 ASSERT3U(offset, <, dn->dn_datablksz);
476 dbuf_read_done(zio_t *zio, arc_buf_t *buf, void *vdb)
478 dmu_buf_impl_t *db = vdb;
480 mutex_enter(&db->db_mtx);
481 ASSERT3U(db->db_state, ==, DB_READ);
483 * All reads are synchronous, so we must have a hold on the dbuf
485 ASSERT(refcount_count(&db->db_holds) > 0);
486 ASSERT(db->db_buf == NULL);
487 ASSERT(db->db.db_data == NULL);
488 if (db->db_level == 0 && db->db_freed_in_flight) {
489 /* we were freed in flight; disregard any error */
490 arc_release(buf, db);
491 bzero(buf->b_data, db->db.db_size);
493 db->db_freed_in_flight = FALSE;
494 dbuf_set_data(db, buf);
495 db->db_state = DB_CACHED;
496 } else if (zio == NULL || zio->io_error == 0) {
497 dbuf_set_data(db, buf);
498 db->db_state = DB_CACHED;
500 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
501 ASSERT3P(db->db_buf, ==, NULL);
502 VERIFY(arc_buf_remove_ref(buf, db) == 1);
503 db->db_state = DB_UNCACHED;
505 cv_broadcast(&db->db_changed);
506 dbuf_rele_and_unlock(db, NULL);
510 dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t *flags)
515 uint32_t aflags = ARC_NOWAIT;
519 ASSERT(!refcount_is_zero(&db->db_holds));
520 /* We need the struct_rwlock to prevent db_blkptr from changing. */
521 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
522 ASSERT(MUTEX_HELD(&db->db_mtx));
523 ASSERT(db->db_state == DB_UNCACHED);
524 ASSERT(db->db_buf == NULL);
526 if (db->db_blkid == DMU_BONUS_BLKID) {
527 int bonuslen = MIN(dn->dn_bonuslen, dn->dn_phys->dn_bonuslen);
529 ASSERT3U(bonuslen, <=, db->db.db_size);
530 db->db.db_data = zio_buf_alloc(DN_MAX_BONUSLEN);
531 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
532 if (bonuslen < DN_MAX_BONUSLEN)
533 bzero(db->db.db_data, DN_MAX_BONUSLEN);
535 bcopy(DN_BONUS(dn->dn_phys), db->db.db_data, bonuslen);
537 dbuf_update_data(db);
538 db->db_state = DB_CACHED;
539 mutex_exit(&db->db_mtx);
544 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
545 * processes the delete record and clears the bp while we are waiting
546 * for the dn_mtx (resulting in a "no" from block_freed).
548 if (db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr) ||
549 (db->db_level == 0 && (dnode_block_freed(dn, db->db_blkid) ||
550 BP_IS_HOLE(db->db_blkptr)))) {
551 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
553 dbuf_set_data(db, arc_buf_alloc(dn->dn_objset->os_spa,
554 db->db.db_size, db, type));
556 bzero(db->db.db_data, db->db.db_size);
557 db->db_state = DB_CACHED;
558 *flags |= DB_RF_CACHED;
559 mutex_exit(&db->db_mtx);
563 spa = dn->dn_objset->os_spa;
566 db->db_state = DB_READ;
567 mutex_exit(&db->db_mtx);
569 if (DBUF_IS_L2CACHEABLE(db))
570 aflags |= ARC_L2CACHE;
572 SET_BOOKMARK(&zb, db->db_objset->os_dsl_dataset ?
573 db->db_objset->os_dsl_dataset->ds_object : DMU_META_OBJSET,
574 db->db.db_object, db->db_level, db->db_blkid);
576 dbuf_add_ref(db, NULL);
578 (void) arc_read(zio, spa, db->db_blkptr,
579 dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ,
580 (*flags & DB_RF_CANFAIL) ? ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED,
582 if (aflags & ARC_CACHED)
583 *flags |= DB_RF_CACHED;
587 dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
590 int havepzio = (zio != NULL);
595 * We don't have to hold the mutex to check db_state because it
596 * can't be freed while we have a hold on the buffer.
598 ASSERT(!refcount_is_zero(&db->db_holds));
600 if (db->db_state == DB_NOFILL)
605 if ((flags & DB_RF_HAVESTRUCT) == 0)
606 rw_enter(&dn->dn_struct_rwlock, RW_READER);
608 prefetch = db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
609 (flags & DB_RF_NOPREFETCH) == 0 && dn != NULL &&
610 DBUF_IS_CACHEABLE(db);
612 mutex_enter(&db->db_mtx);
613 if (db->db_state == DB_CACHED) {
614 mutex_exit(&db->db_mtx);
616 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
617 db->db.db_size, TRUE);
618 if ((flags & DB_RF_HAVESTRUCT) == 0)
619 rw_exit(&dn->dn_struct_rwlock);
621 } else if (db->db_state == DB_UNCACHED) {
622 spa_t *spa = dn->dn_objset->os_spa;
625 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
626 dbuf_read_impl(db, zio, &flags);
628 /* dbuf_read_impl has dropped db_mtx for us */
631 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
632 db->db.db_size, flags & DB_RF_CACHED);
634 if ((flags & DB_RF_HAVESTRUCT) == 0)
635 rw_exit(&dn->dn_struct_rwlock);
641 mutex_exit(&db->db_mtx);
643 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
644 db->db.db_size, TRUE);
645 if ((flags & DB_RF_HAVESTRUCT) == 0)
646 rw_exit(&dn->dn_struct_rwlock);
649 mutex_enter(&db->db_mtx);
650 if ((flags & DB_RF_NEVERWAIT) == 0) {
651 while (db->db_state == DB_READ ||
652 db->db_state == DB_FILL) {
653 ASSERT(db->db_state == DB_READ ||
654 (flags & DB_RF_HAVESTRUCT) == 0);
655 cv_wait(&db->db_changed, &db->db_mtx);
657 if (db->db_state == DB_UNCACHED)
660 mutex_exit(&db->db_mtx);
663 ASSERT(err || havepzio || db->db_state == DB_CACHED);
668 dbuf_noread(dmu_buf_impl_t *db)
670 ASSERT(!refcount_is_zero(&db->db_holds));
671 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
672 mutex_enter(&db->db_mtx);
673 while (db->db_state == DB_READ || db->db_state == DB_FILL)
674 cv_wait(&db->db_changed, &db->db_mtx);
675 if (db->db_state == DB_UNCACHED) {
676 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
679 ASSERT(db->db_buf == NULL);
680 ASSERT(db->db.db_data == NULL);
681 DB_GET_SPA(&spa, db);
682 dbuf_set_data(db, arc_buf_alloc(spa, db->db.db_size, db, type));
683 db->db_state = DB_FILL;
684 } else if (db->db_state == DB_NOFILL) {
685 dbuf_set_data(db, NULL);
687 ASSERT3U(db->db_state, ==, DB_CACHED);
689 mutex_exit(&db->db_mtx);
693 * This is our just-in-time copy function. It makes a copy of
694 * buffers, that have been modified in a previous transaction
695 * group, before we modify them in the current active group.
697 * This function is used in two places: when we are dirtying a
698 * buffer for the first time in a txg, and when we are freeing
699 * a range in a dnode that includes this buffer.
701 * Note that when we are called from dbuf_free_range() we do
702 * not put a hold on the buffer, we just traverse the active
703 * dbuf list for the dnode.
706 dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg)
708 dbuf_dirty_record_t *dr = db->db_last_dirty;
710 ASSERT(MUTEX_HELD(&db->db_mtx));
711 ASSERT(db->db.db_data != NULL);
712 ASSERT(db->db_level == 0);
713 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT);
716 (dr->dt.dl.dr_data !=
717 ((db->db_blkid == DMU_BONUS_BLKID) ? db->db.db_data : db->db_buf)))
721 * If the last dirty record for this dbuf has not yet synced
722 * and its referencing the dbuf data, either:
723 * reset the reference to point to a new copy,
724 * or (if there a no active holders)
725 * just null out the current db_data pointer.
727 ASSERT(dr->dr_txg >= txg - 2);
728 if (db->db_blkid == DMU_BONUS_BLKID) {
729 /* Note that the data bufs here are zio_bufs */
730 dr->dt.dl.dr_data = zio_buf_alloc(DN_MAX_BONUSLEN);
731 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
732 bcopy(db->db.db_data, dr->dt.dl.dr_data, DN_MAX_BONUSLEN);
733 } else if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
734 int size = db->db.db_size;
735 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
738 DB_GET_SPA(&spa, db);
739 dr->dt.dl.dr_data = arc_buf_alloc(spa, size, db, type);
740 bcopy(db->db.db_data, dr->dt.dl.dr_data->b_data, size);
742 dbuf_set_data(db, NULL);
747 dbuf_unoverride(dbuf_dirty_record_t *dr)
749 dmu_buf_impl_t *db = dr->dr_dbuf;
750 blkptr_t *bp = &dr->dt.dl.dr_overridden_by;
751 uint64_t txg = dr->dr_txg;
753 ASSERT(MUTEX_HELD(&db->db_mtx));
754 ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC);
755 ASSERT(db->db_level == 0);
757 if (db->db_blkid == DMU_BONUS_BLKID ||
758 dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN)
761 ASSERT(db->db_data_pending != dr);
763 /* free this block */
764 if (!BP_IS_HOLE(bp) && !dr->dt.dl.dr_nopwrite) {
767 DB_GET_SPA(&spa, db);
768 zio_free(spa, txg, bp);
770 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
771 dr->dt.dl.dr_nopwrite = B_FALSE;
774 * Release the already-written buffer, so we leave it in
775 * a consistent dirty state. Note that all callers are
776 * modifying the buffer, so they will immediately do
777 * another (redundant) arc_release(). Therefore, leave
778 * the buf thawed to save the effort of freezing &
779 * immediately re-thawing it.
781 arc_release(dr->dt.dl.dr_data, db);
785 * Evict (if its unreferenced) or clear (if its referenced) any level-0
786 * data blocks in the free range, so that any future readers will find
787 * empty blocks. Also, if we happen accross any level-1 dbufs in the
788 * range that have not already been marked dirty, mark them dirty so
789 * they stay in memory.
792 dbuf_free_range(dnode_t *dn, uint64_t start, uint64_t end, dmu_tx_t *tx)
794 dmu_buf_impl_t *db, *db_next;
795 uint64_t txg = tx->tx_txg;
796 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
797 uint64_t first_l1 = start >> epbs;
798 uint64_t last_l1 = end >> epbs;
800 if (end > dn->dn_maxblkid && (end != DMU_SPILL_BLKID)) {
801 end = dn->dn_maxblkid;
802 last_l1 = end >> epbs;
804 dprintf_dnode(dn, "start=%llu end=%llu\n", start, end);
805 mutex_enter(&dn->dn_dbufs_mtx);
806 for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
807 db_next = list_next(&dn->dn_dbufs, db);
808 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
810 if (db->db_level == 1 &&
811 db->db_blkid >= first_l1 && db->db_blkid <= last_l1) {
812 mutex_enter(&db->db_mtx);
813 if (db->db_last_dirty &&
814 db->db_last_dirty->dr_txg < txg) {
815 dbuf_add_ref(db, FTAG);
816 mutex_exit(&db->db_mtx);
817 dbuf_will_dirty(db, tx);
820 mutex_exit(&db->db_mtx);
824 if (db->db_level != 0)
826 dprintf_dbuf(db, "found buf %s\n", "");
827 if (db->db_blkid < start || db->db_blkid > end)
830 /* found a level 0 buffer in the range */
831 if (dbuf_undirty(db, tx))
834 mutex_enter(&db->db_mtx);
835 if (db->db_state == DB_UNCACHED ||
836 db->db_state == DB_NOFILL ||
837 db->db_state == DB_EVICTING) {
838 ASSERT(db->db.db_data == NULL);
839 mutex_exit(&db->db_mtx);
842 if (db->db_state == DB_READ || db->db_state == DB_FILL) {
843 /* will be handled in dbuf_read_done or dbuf_rele */
844 db->db_freed_in_flight = TRUE;
845 mutex_exit(&db->db_mtx);
848 if (refcount_count(&db->db_holds) == 0) {
853 /* The dbuf is referenced */
855 if (db->db_last_dirty != NULL) {
856 dbuf_dirty_record_t *dr = db->db_last_dirty;
858 if (dr->dr_txg == txg) {
860 * This buffer is "in-use", re-adjust the file
861 * size to reflect that this buffer may
862 * contain new data when we sync.
864 if (db->db_blkid != DMU_SPILL_BLKID &&
865 db->db_blkid > dn->dn_maxblkid)
866 dn->dn_maxblkid = db->db_blkid;
870 * This dbuf is not dirty in the open context.
871 * Either uncache it (if its not referenced in
872 * the open context) or reset its contents to
875 dbuf_fix_old_data(db, txg);
878 /* clear the contents if its cached */
879 if (db->db_state == DB_CACHED) {
880 ASSERT(db->db.db_data != NULL);
881 arc_release(db->db_buf, db);
882 bzero(db->db.db_data, db->db.db_size);
883 arc_buf_freeze(db->db_buf);
886 mutex_exit(&db->db_mtx);
888 mutex_exit(&dn->dn_dbufs_mtx);
892 dbuf_block_freeable(dmu_buf_impl_t *db)
894 dsl_dataset_t *ds = db->db_objset->os_dsl_dataset;
895 uint64_t birth_txg = 0;
898 * We don't need any locking to protect db_blkptr:
899 * If it's syncing, then db_last_dirty will be set
900 * so we'll ignore db_blkptr.
902 ASSERT(MUTEX_HELD(&db->db_mtx));
903 if (db->db_last_dirty)
904 birth_txg = db->db_last_dirty->dr_txg;
905 else if (db->db_blkptr)
906 birth_txg = db->db_blkptr->blk_birth;
909 * If we don't exist or are in a snapshot, we can't be freed.
910 * Don't pass the bp to dsl_dataset_block_freeable() since we
911 * are holding the db_mtx lock and might deadlock if we are
912 * prefetching a dedup-ed block.
915 return (ds == NULL ||
916 dsl_dataset_block_freeable(ds, NULL, birth_txg));
922 dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx)
924 arc_buf_t *buf, *obuf;
925 int osize = db->db.db_size;
926 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
929 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
934 /* XXX does *this* func really need the lock? */
935 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
938 * This call to dbuf_will_dirty() with the dn_struct_rwlock held
939 * is OK, because there can be no other references to the db
940 * when we are changing its size, so no concurrent DB_FILL can
944 * XXX we should be doing a dbuf_read, checking the return
945 * value and returning that up to our callers
947 dbuf_will_dirty(db, tx);
949 /* create the data buffer for the new block */
950 buf = arc_buf_alloc(dn->dn_objset->os_spa, size, db, type);
952 /* copy old block data to the new block */
954 bcopy(obuf->b_data, buf->b_data, MIN(osize, size));
955 /* zero the remainder */
957 bzero((uint8_t *)buf->b_data + osize, size - osize);
959 mutex_enter(&db->db_mtx);
960 dbuf_set_data(db, buf);
961 VERIFY(arc_buf_remove_ref(obuf, db) == 1);
962 db->db.db_size = size;
964 if (db->db_level == 0) {
965 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
966 db->db_last_dirty->dt.dl.dr_data = buf;
968 mutex_exit(&db->db_mtx);
970 dnode_willuse_space(dn, size-osize, tx);
975 dbuf_release_bp(dmu_buf_impl_t *db)
979 DB_GET_OBJSET(&os, db);
980 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
981 ASSERT(arc_released(os->os_phys_buf) ||
982 list_link_active(&os->os_dsl_dataset->ds_synced_link));
983 ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf));
985 (void) arc_release(db->db_buf, db);
988 dbuf_dirty_record_t *
989 dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
993 dbuf_dirty_record_t **drp, *dr;
994 int drop_struct_lock = FALSE;
995 boolean_t do_free_accounting = B_FALSE;
996 int txgoff = tx->tx_txg & TXG_MASK;
998 ASSERT(tx->tx_txg != 0);
999 ASSERT(!refcount_is_zero(&db->db_holds));
1000 DMU_TX_DIRTY_BUF(tx, db);
1005 * Shouldn't dirty a regular buffer in syncing context. Private
1006 * objects may be dirtied in syncing context, but only if they
1007 * were already pre-dirtied in open context.
1009 ASSERT(!dmu_tx_is_syncing(tx) ||
1010 BP_IS_HOLE(dn->dn_objset->os_rootbp) ||
1011 DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1012 dn->dn_objset->os_dsl_dataset == NULL);
1014 * We make this assert for private objects as well, but after we
1015 * check if we're already dirty. They are allowed to re-dirty
1016 * in syncing context.
1018 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1019 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1020 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1022 mutex_enter(&db->db_mtx);
1024 * XXX make this true for indirects too? The problem is that
1025 * transactions created with dmu_tx_create_assigned() from
1026 * syncing context don't bother holding ahead.
1028 ASSERT(db->db_level != 0 ||
1029 db->db_state == DB_CACHED || db->db_state == DB_FILL ||
1030 db->db_state == DB_NOFILL);
1032 mutex_enter(&dn->dn_mtx);
1034 * Don't set dirtyctx to SYNC if we're just modifying this as we
1035 * initialize the objset.
1037 if (dn->dn_dirtyctx == DN_UNDIRTIED &&
1038 !BP_IS_HOLE(dn->dn_objset->os_rootbp)) {
1040 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN);
1041 ASSERT(dn->dn_dirtyctx_firstset == NULL);
1042 dn->dn_dirtyctx_firstset = kmem_alloc(1, KM_SLEEP);
1044 mutex_exit(&dn->dn_mtx);
1046 if (db->db_blkid == DMU_SPILL_BLKID)
1047 dn->dn_have_spill = B_TRUE;
1050 * If this buffer is already dirty, we're done.
1052 drp = &db->db_last_dirty;
1053 ASSERT(*drp == NULL || (*drp)->dr_txg <= tx->tx_txg ||
1054 db->db.db_object == DMU_META_DNODE_OBJECT);
1055 while ((dr = *drp) != NULL && dr->dr_txg > tx->tx_txg)
1057 if (dr && dr->dr_txg == tx->tx_txg) {
1060 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID) {
1062 * If this buffer has already been written out,
1063 * we now need to reset its state.
1065 dbuf_unoverride(dr);
1066 if (db->db.db_object != DMU_META_DNODE_OBJECT &&
1067 db->db_state != DB_NOFILL)
1068 arc_buf_thaw(db->db_buf);
1070 mutex_exit(&db->db_mtx);
1075 * Only valid if not already dirty.
1077 ASSERT(dn->dn_object == 0 ||
1078 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1079 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1081 ASSERT3U(dn->dn_nlevels, >, db->db_level);
1082 ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) ||
1083 dn->dn_phys->dn_nlevels > db->db_level ||
1084 dn->dn_next_nlevels[txgoff] > db->db_level ||
1085 dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level ||
1086 dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level);
1089 * We should only be dirtying in syncing context if it's the
1090 * mos or we're initializing the os or it's a special object.
1091 * However, we are allowed to dirty in syncing context provided
1092 * we already dirtied it in open context. Hence we must make
1093 * this assertion only if we're not already dirty.
1096 ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1097 os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp));
1098 ASSERT(db->db.db_size != 0);
1100 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1102 if (db->db_blkid != DMU_BONUS_BLKID) {
1104 * Update the accounting.
1105 * Note: we delay "free accounting" until after we drop
1106 * the db_mtx. This keeps us from grabbing other locks
1107 * (and possibly deadlocking) in bp_get_dsize() while
1108 * also holding the db_mtx.
1110 dnode_willuse_space(dn, db->db.db_size, tx);
1111 do_free_accounting = dbuf_block_freeable(db);
1115 * If this buffer is dirty in an old transaction group we need
1116 * to make a copy of it so that the changes we make in this
1117 * transaction group won't leak out when we sync the older txg.
1119 dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP);
1120 if (db->db_level == 0) {
1121 void *data_old = db->db_buf;
1123 if (db->db_state != DB_NOFILL) {
1124 if (db->db_blkid == DMU_BONUS_BLKID) {
1125 dbuf_fix_old_data(db, tx->tx_txg);
1126 data_old = db->db.db_data;
1127 } else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
1129 * Release the data buffer from the cache so
1130 * that we can modify it without impacting
1131 * possible other users of this cached data
1132 * block. Note that indirect blocks and
1133 * private objects are not released until the
1134 * syncing state (since they are only modified
1137 arc_release(db->db_buf, db);
1138 dbuf_fix_old_data(db, tx->tx_txg);
1139 data_old = db->db_buf;
1141 ASSERT(data_old != NULL);
1143 dr->dt.dl.dr_data = data_old;
1145 mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_DEFAULT, NULL);
1146 list_create(&dr->dt.di.dr_children,
1147 sizeof (dbuf_dirty_record_t),
1148 offsetof(dbuf_dirty_record_t, dr_dirty_node));
1151 dr->dr_txg = tx->tx_txg;
1156 * We could have been freed_in_flight between the dbuf_noread
1157 * and dbuf_dirty. We win, as though the dbuf_noread() had
1158 * happened after the free.
1160 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1161 db->db_blkid != DMU_SPILL_BLKID) {
1162 mutex_enter(&dn->dn_mtx);
1163 dnode_clear_range(dn, db->db_blkid, 1, tx);
1164 mutex_exit(&dn->dn_mtx);
1165 db->db_freed_in_flight = FALSE;
1169 * This buffer is now part of this txg
1171 dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg);
1172 db->db_dirtycnt += 1;
1173 ASSERT3U(db->db_dirtycnt, <=, 3);
1175 mutex_exit(&db->db_mtx);
1177 if (db->db_blkid == DMU_BONUS_BLKID ||
1178 db->db_blkid == DMU_SPILL_BLKID) {
1179 mutex_enter(&dn->dn_mtx);
1180 ASSERT(!list_link_active(&dr->dr_dirty_node));
1181 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1182 mutex_exit(&dn->dn_mtx);
1183 dnode_setdirty(dn, tx);
1186 } else if (do_free_accounting) {
1187 blkptr_t *bp = db->db_blkptr;
1188 int64_t willfree = (bp && !BP_IS_HOLE(bp)) ?
1189 bp_get_dsize(os->os_spa, bp) : db->db.db_size;
1191 * This is only a guess -- if the dbuf is dirty
1192 * in a previous txg, we don't know how much
1193 * space it will use on disk yet. We should
1194 * really have the struct_rwlock to access
1195 * db_blkptr, but since this is just a guess,
1196 * it's OK if we get an odd answer.
1198 ddt_prefetch(os->os_spa, bp);
1199 dnode_willuse_space(dn, -willfree, tx);
1202 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
1203 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1204 drop_struct_lock = TRUE;
1207 if (db->db_level == 0) {
1208 dnode_new_blkid(dn, db->db_blkid, tx, drop_struct_lock);
1209 ASSERT(dn->dn_maxblkid >= db->db_blkid);
1212 if (db->db_level+1 < dn->dn_nlevels) {
1213 dmu_buf_impl_t *parent = db->db_parent;
1214 dbuf_dirty_record_t *di;
1215 int parent_held = FALSE;
1217 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
1218 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1220 parent = dbuf_hold_level(dn, db->db_level+1,
1221 db->db_blkid >> epbs, FTAG);
1222 ASSERT(parent != NULL);
1225 if (drop_struct_lock)
1226 rw_exit(&dn->dn_struct_rwlock);
1227 ASSERT3U(db->db_level+1, ==, parent->db_level);
1228 di = dbuf_dirty(parent, tx);
1230 dbuf_rele(parent, FTAG);
1232 mutex_enter(&db->db_mtx);
1233 /* possible race with dbuf_undirty() */
1234 if (db->db_last_dirty == dr ||
1235 dn->dn_object == DMU_META_DNODE_OBJECT) {
1236 mutex_enter(&di->dt.di.dr_mtx);
1237 ASSERT3U(di->dr_txg, ==, tx->tx_txg);
1238 ASSERT(!list_link_active(&dr->dr_dirty_node));
1239 list_insert_tail(&di->dt.di.dr_children, dr);
1240 mutex_exit(&di->dt.di.dr_mtx);
1243 mutex_exit(&db->db_mtx);
1245 ASSERT(db->db_level+1 == dn->dn_nlevels);
1246 ASSERT(db->db_blkid < dn->dn_nblkptr);
1247 ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf);
1248 mutex_enter(&dn->dn_mtx);
1249 ASSERT(!list_link_active(&dr->dr_dirty_node));
1250 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1251 mutex_exit(&dn->dn_mtx);
1252 if (drop_struct_lock)
1253 rw_exit(&dn->dn_struct_rwlock);
1256 dnode_setdirty(dn, tx);
1262 dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1265 uint64_t txg = tx->tx_txg;
1266 dbuf_dirty_record_t *dr, **drp;
1269 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1271 mutex_enter(&db->db_mtx);
1273 * If this buffer is not dirty, we're done.
1275 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1276 if (dr->dr_txg <= txg)
1278 if (dr == NULL || dr->dr_txg < txg) {
1279 mutex_exit(&db->db_mtx);
1282 ASSERT(dr->dr_txg == txg);
1283 ASSERT(dr->dr_dbuf == db);
1289 * If this buffer is currently held, we cannot undirty
1290 * it, since one of the current holders may be in the
1291 * middle of an update. Note that users of dbuf_undirty()
1292 * should not place a hold on the dbuf before the call.
1293 * Also note: we can get here with a spill block, so
1294 * test for that similar to how dbuf_dirty does.
1296 if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
1297 mutex_exit(&db->db_mtx);
1298 /* Make sure we don't toss this buffer at sync phase */
1299 if (db->db_blkid != DMU_SPILL_BLKID) {
1300 mutex_enter(&dn->dn_mtx);
1301 dnode_clear_range(dn, db->db_blkid, 1, tx);
1302 mutex_exit(&dn->dn_mtx);
1308 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1310 ASSERT(db->db.db_size != 0);
1312 /* XXX would be nice to fix up dn_towrite_space[] */
1317 * Note that there are three places in dbuf_dirty()
1318 * where this dirty record may be put on a list.
1319 * Make sure to do a list_remove corresponding to
1320 * every one of those list_insert calls.
1322 if (dr->dr_parent) {
1323 mutex_enter(&dr->dr_parent->dt.di.dr_mtx);
1324 list_remove(&dr->dr_parent->dt.di.dr_children, dr);
1325 mutex_exit(&dr->dr_parent->dt.di.dr_mtx);
1326 } else if (db->db_blkid == DMU_SPILL_BLKID ||
1327 db->db_level+1 == dn->dn_nlevels) {
1328 ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf);
1329 mutex_enter(&dn->dn_mtx);
1330 list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr);
1331 mutex_exit(&dn->dn_mtx);
1335 if (db->db_level == 0) {
1336 if (db->db_state != DB_NOFILL) {
1337 dbuf_unoverride(dr);
1339 ASSERT(db->db_buf != NULL);
1340 ASSERT(dr->dt.dl.dr_data != NULL);
1341 if (dr->dt.dl.dr_data != db->db_buf)
1342 VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data,
1346 ASSERT(db->db_buf != NULL);
1347 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
1348 mutex_destroy(&dr->dt.di.dr_mtx);
1349 list_destroy(&dr->dt.di.dr_children);
1351 kmem_free(dr, sizeof (dbuf_dirty_record_t));
1353 ASSERT(db->db_dirtycnt > 0);
1354 db->db_dirtycnt -= 1;
1356 if (refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) {
1357 arc_buf_t *buf = db->db_buf;
1359 ASSERT(db->db_state == DB_NOFILL || arc_released(buf));
1360 dbuf_set_data(db, NULL);
1361 VERIFY(arc_buf_remove_ref(buf, db) == 1);
1366 mutex_exit(&db->db_mtx);
1370 #pragma weak dmu_buf_will_dirty = dbuf_will_dirty
1372 dbuf_will_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1374 int rf = DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH;
1376 ASSERT(tx->tx_txg != 0);
1377 ASSERT(!refcount_is_zero(&db->db_holds));
1380 if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock))
1381 rf |= DB_RF_HAVESTRUCT;
1383 (void) dbuf_read(db, NULL, rf);
1384 (void) dbuf_dirty(db, tx);
1388 dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1390 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1392 db->db_state = DB_NOFILL;
1394 dmu_buf_will_fill(db_fake, tx);
1398 dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1400 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1402 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1403 ASSERT(tx->tx_txg != 0);
1404 ASSERT(db->db_level == 0);
1405 ASSERT(!refcount_is_zero(&db->db_holds));
1407 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
1408 dmu_tx_private_ok(tx));
1411 (void) dbuf_dirty(db, tx);
1414 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1417 dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx)
1419 mutex_enter(&db->db_mtx);
1422 if (db->db_state == DB_FILL) {
1423 if (db->db_level == 0 && db->db_freed_in_flight) {
1424 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1425 /* we were freed while filling */
1426 /* XXX dbuf_undirty? */
1427 bzero(db->db.db_data, db->db.db_size);
1428 db->db_freed_in_flight = FALSE;
1430 db->db_state = DB_CACHED;
1431 cv_broadcast(&db->db_changed);
1433 mutex_exit(&db->db_mtx);
1437 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1438 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1441 dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx)
1443 ASSERT(!refcount_is_zero(&db->db_holds));
1444 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1445 ASSERT(db->db_level == 0);
1446 ASSERT(DBUF_GET_BUFC_TYPE(db) == ARC_BUFC_DATA);
1447 ASSERT(buf != NULL);
1448 ASSERT(arc_buf_size(buf) == db->db.db_size);
1449 ASSERT(tx->tx_txg != 0);
1451 arc_return_buf(buf, db);
1452 ASSERT(arc_released(buf));
1454 mutex_enter(&db->db_mtx);
1456 while (db->db_state == DB_READ || db->db_state == DB_FILL)
1457 cv_wait(&db->db_changed, &db->db_mtx);
1459 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED);
1461 if (db->db_state == DB_CACHED &&
1462 refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) {
1463 mutex_exit(&db->db_mtx);
1464 (void) dbuf_dirty(db, tx);
1465 bcopy(buf->b_data, db->db.db_data, db->db.db_size);
1466 VERIFY(arc_buf_remove_ref(buf, db) == 1);
1467 xuio_stat_wbuf_copied();
1471 xuio_stat_wbuf_nocopy();
1472 if (db->db_state == DB_CACHED) {
1473 dbuf_dirty_record_t *dr = db->db_last_dirty;
1475 ASSERT(db->db_buf != NULL);
1476 if (dr != NULL && dr->dr_txg == tx->tx_txg) {
1477 ASSERT(dr->dt.dl.dr_data == db->db_buf);
1478 if (!arc_released(db->db_buf)) {
1479 ASSERT(dr->dt.dl.dr_override_state ==
1481 arc_release(db->db_buf, db);
1483 dr->dt.dl.dr_data = buf;
1484 VERIFY(arc_buf_remove_ref(db->db_buf, db) == 1);
1485 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) {
1486 arc_release(db->db_buf, db);
1487 VERIFY(arc_buf_remove_ref(db->db_buf, db) == 1);
1491 ASSERT(db->db_buf == NULL);
1492 dbuf_set_data(db, buf);
1493 db->db_state = DB_FILL;
1494 mutex_exit(&db->db_mtx);
1495 (void) dbuf_dirty(db, tx);
1496 dbuf_fill_done(db, tx);
1500 * "Clear" the contents of this dbuf. This will mark the dbuf
1501 * EVICTING and clear *most* of its references. Unfortunetely,
1502 * when we are not holding the dn_dbufs_mtx, we can't clear the
1503 * entry in the dn_dbufs list. We have to wait until dbuf_destroy()
1504 * in this case. For callers from the DMU we will usually see:
1505 * dbuf_clear()->arc_buf_evict()->dbuf_do_evict()->dbuf_destroy()
1506 * For the arc callback, we will usually see:
1507 * dbuf_do_evict()->dbuf_clear();dbuf_destroy()
1508 * Sometimes, though, we will get a mix of these two:
1509 * DMU: dbuf_clear()->arc_buf_evict()
1510 * ARC: dbuf_do_evict()->dbuf_destroy()
1513 dbuf_clear(dmu_buf_impl_t *db)
1516 dmu_buf_impl_t *parent = db->db_parent;
1517 dmu_buf_impl_t *dndb;
1518 int dbuf_gone = FALSE;
1520 ASSERT(MUTEX_HELD(&db->db_mtx));
1521 ASSERT(refcount_is_zero(&db->db_holds));
1523 dbuf_evict_user(db);
1525 if (db->db_state == DB_CACHED) {
1526 ASSERT(db->db.db_data != NULL);
1527 if (db->db_blkid == DMU_BONUS_BLKID) {
1528 zio_buf_free(db->db.db_data, DN_MAX_BONUSLEN);
1529 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
1531 db->db.db_data = NULL;
1532 db->db_state = DB_UNCACHED;
1535 ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL);
1536 ASSERT(db->db_data_pending == NULL);
1538 db->db_state = DB_EVICTING;
1539 db->db_blkptr = NULL;
1544 if (db->db_blkid != DMU_BONUS_BLKID && MUTEX_HELD(&dn->dn_dbufs_mtx)) {
1545 list_remove(&dn->dn_dbufs, db);
1546 (void) atomic_dec_32_nv(&dn->dn_dbufs_count);
1550 * Decrementing the dbuf count means that the hold corresponding
1551 * to the removed dbuf is no longer discounted in dnode_move(),
1552 * so the dnode cannot be moved until after we release the hold.
1553 * The membar_producer() ensures visibility of the decremented
1554 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
1558 db->db_dnode_handle = NULL;
1564 dbuf_gone = arc_buf_evict(db->db_buf);
1567 mutex_exit(&db->db_mtx);
1570 * If this dbuf is referenced from an indirect dbuf,
1571 * decrement the ref count on the indirect dbuf.
1573 if (parent && parent != dndb)
1574 dbuf_rele(parent, db);
1578 dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse,
1579 dmu_buf_impl_t **parentp, blkptr_t **bpp)
1586 ASSERT(blkid != DMU_BONUS_BLKID);
1588 if (blkid == DMU_SPILL_BLKID) {
1589 mutex_enter(&dn->dn_mtx);
1590 if (dn->dn_have_spill &&
1591 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
1592 *bpp = &dn->dn_phys->dn_spill;
1595 dbuf_add_ref(dn->dn_dbuf, NULL);
1596 *parentp = dn->dn_dbuf;
1597 mutex_exit(&dn->dn_mtx);
1601 if (dn->dn_phys->dn_nlevels == 0)
1604 nlevels = dn->dn_phys->dn_nlevels;
1606 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1608 ASSERT3U(level * epbs, <, 64);
1609 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1610 if (level >= nlevels ||
1611 (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) {
1612 /* the buffer has no parent yet */
1614 } else if (level < nlevels-1) {
1615 /* this block is referenced from an indirect block */
1616 int err = dbuf_hold_impl(dn, level+1,
1617 blkid >> epbs, fail_sparse, NULL, parentp);
1620 err = dbuf_read(*parentp, NULL,
1621 (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL));
1623 dbuf_rele(*parentp, NULL);
1627 *bpp = ((blkptr_t *)(*parentp)->db.db_data) +
1628 (blkid & ((1ULL << epbs) - 1));
1631 /* the block is referenced from the dnode */
1632 ASSERT3U(level, ==, nlevels-1);
1633 ASSERT(dn->dn_phys->dn_nblkptr == 0 ||
1634 blkid < dn->dn_phys->dn_nblkptr);
1636 dbuf_add_ref(dn->dn_dbuf, NULL);
1637 *parentp = dn->dn_dbuf;
1639 *bpp = &dn->dn_phys->dn_blkptr[blkid];
1644 static dmu_buf_impl_t *
1645 dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid,
1646 dmu_buf_impl_t *parent, blkptr_t *blkptr)
1648 objset_t *os = dn->dn_objset;
1649 dmu_buf_impl_t *db, *odb;
1651 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1652 ASSERT(dn->dn_type != DMU_OT_NONE);
1654 db = kmem_cache_alloc(dbuf_cache, KM_SLEEP);
1657 db->db.db_object = dn->dn_object;
1658 db->db_level = level;
1659 db->db_blkid = blkid;
1660 db->db_last_dirty = NULL;
1661 db->db_dirtycnt = 0;
1662 db->db_dnode_handle = dn->dn_handle;
1663 db->db_parent = parent;
1664 db->db_blkptr = blkptr;
1666 db->db_user_ptr = NULL;
1667 db->db_user_data_ptr_ptr = NULL;
1668 db->db_evict_func = NULL;
1669 db->db_immediate_evict = 0;
1670 db->db_freed_in_flight = 0;
1672 if (blkid == DMU_BONUS_BLKID) {
1673 ASSERT3P(parent, ==, dn->dn_dbuf);
1674 db->db.db_size = DN_MAX_BONUSLEN -
1675 (dn->dn_nblkptr-1) * sizeof (blkptr_t);
1676 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
1677 db->db.db_offset = DMU_BONUS_BLKID;
1678 db->db_state = DB_UNCACHED;
1679 /* the bonus dbuf is not placed in the hash table */
1680 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1682 } else if (blkid == DMU_SPILL_BLKID) {
1683 db->db.db_size = (blkptr != NULL) ?
1684 BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE;
1685 db->db.db_offset = 0;
1688 db->db_level ? 1<<dn->dn_indblkshift : dn->dn_datablksz;
1689 db->db.db_size = blocksize;
1690 db->db.db_offset = db->db_blkid * blocksize;
1694 * Hold the dn_dbufs_mtx while we get the new dbuf
1695 * in the hash table *and* added to the dbufs list.
1696 * This prevents a possible deadlock with someone
1697 * trying to look up this dbuf before its added to the
1700 mutex_enter(&dn->dn_dbufs_mtx);
1701 db->db_state = DB_EVICTING;
1702 if ((odb = dbuf_hash_insert(db)) != NULL) {
1703 /* someone else inserted it first */
1704 kmem_cache_free(dbuf_cache, db);
1705 mutex_exit(&dn->dn_dbufs_mtx);
1708 list_insert_head(&dn->dn_dbufs, db);
1709 db->db_state = DB_UNCACHED;
1710 mutex_exit(&dn->dn_dbufs_mtx);
1711 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1713 if (parent && parent != dn->dn_dbuf)
1714 dbuf_add_ref(parent, db);
1716 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1717 refcount_count(&dn->dn_holds) > 0);
1718 (void) refcount_add(&dn->dn_holds, db);
1719 (void) atomic_inc_32_nv(&dn->dn_dbufs_count);
1721 dprintf_dbuf(db, "db=%p\n", db);
1727 dbuf_do_evict(void *private)
1729 arc_buf_t *buf = private;
1730 dmu_buf_impl_t *db = buf->b_private;
1732 if (!MUTEX_HELD(&db->db_mtx))
1733 mutex_enter(&db->db_mtx);
1735 ASSERT(refcount_is_zero(&db->db_holds));
1737 if (db->db_state != DB_EVICTING) {
1738 ASSERT(db->db_state == DB_CACHED);
1743 mutex_exit(&db->db_mtx);
1750 dbuf_destroy(dmu_buf_impl_t *db)
1752 ASSERT(refcount_is_zero(&db->db_holds));
1754 if (db->db_blkid != DMU_BONUS_BLKID) {
1756 * If this dbuf is still on the dn_dbufs list,
1757 * remove it from that list.
1759 if (db->db_dnode_handle != NULL) {
1764 mutex_enter(&dn->dn_dbufs_mtx);
1765 list_remove(&dn->dn_dbufs, db);
1766 (void) atomic_dec_32_nv(&dn->dn_dbufs_count);
1767 mutex_exit(&dn->dn_dbufs_mtx);
1770 * Decrementing the dbuf count means that the hold
1771 * corresponding to the removed dbuf is no longer
1772 * discounted in dnode_move(), so the dnode cannot be
1773 * moved until after we release the hold.
1776 db->db_dnode_handle = NULL;
1778 dbuf_hash_remove(db);
1780 db->db_parent = NULL;
1783 ASSERT(!list_link_active(&db->db_link));
1784 ASSERT(db->db.db_data == NULL);
1785 ASSERT(db->db_hash_next == NULL);
1786 ASSERT(db->db_blkptr == NULL);
1787 ASSERT(db->db_data_pending == NULL);
1789 kmem_cache_free(dbuf_cache, db);
1790 arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1794 dbuf_prefetch(dnode_t *dn, uint64_t blkid)
1796 dmu_buf_impl_t *db = NULL;
1797 blkptr_t *bp = NULL;
1799 ASSERT(blkid != DMU_BONUS_BLKID);
1800 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1802 if (dnode_block_freed(dn, blkid))
1805 /* dbuf_find() returns with db_mtx held */
1806 if (db = dbuf_find(dn, 0, blkid)) {
1808 * This dbuf is already in the cache. We assume that
1809 * it is already CACHED, or else about to be either
1812 mutex_exit(&db->db_mtx);
1816 if (dbuf_findbp(dn, 0, blkid, TRUE, &db, &bp) == 0) {
1817 if (bp && !BP_IS_HOLE(bp)) {
1818 int priority = dn->dn_type == DMU_OT_DDT_ZAP ?
1819 ZIO_PRIORITY_DDT_PREFETCH : ZIO_PRIORITY_ASYNC_READ;
1820 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
1821 uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
1824 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
1825 dn->dn_object, 0, blkid);
1827 (void) arc_read(NULL, dn->dn_objset->os_spa,
1828 bp, NULL, NULL, priority,
1829 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
1833 dbuf_rele(db, NULL);
1838 * Returns with db_holds incremented, and db_mtx not held.
1839 * Note: dn_struct_rwlock must be held.
1842 dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid, int fail_sparse,
1843 void *tag, dmu_buf_impl_t **dbp)
1845 dmu_buf_impl_t *db, *parent = NULL;
1847 ASSERT(blkid != DMU_BONUS_BLKID);
1848 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1849 ASSERT3U(dn->dn_nlevels, >, level);
1853 /* dbuf_find() returns with db_mtx held */
1854 db = dbuf_find(dn, level, blkid);
1857 blkptr_t *bp = NULL;
1860 ASSERT3P(parent, ==, NULL);
1861 err = dbuf_findbp(dn, level, blkid, fail_sparse, &parent, &bp);
1863 if (err == 0 && bp && BP_IS_HOLE(bp))
1867 dbuf_rele(parent, NULL);
1871 if (err && err != ENOENT)
1873 db = dbuf_create(dn, level, blkid, parent, bp);
1876 if (db->db_buf && refcount_is_zero(&db->db_holds)) {
1877 arc_buf_add_ref(db->db_buf, db);
1878 if (db->db_buf->b_data == NULL) {
1881 dbuf_rele(parent, NULL);
1886 ASSERT3P(db->db.db_data, ==, db->db_buf->b_data);
1889 ASSERT(db->db_buf == NULL || arc_referenced(db->db_buf));
1892 * If this buffer is currently syncing out, and we are are
1893 * still referencing it from db_data, we need to make a copy
1894 * of it in case we decide we want to dirty it again in this txg.
1896 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1897 dn->dn_object != DMU_META_DNODE_OBJECT &&
1898 db->db_state == DB_CACHED && db->db_data_pending) {
1899 dbuf_dirty_record_t *dr = db->db_data_pending;
1901 if (dr->dt.dl.dr_data == db->db_buf) {
1902 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1905 arc_buf_alloc(dn->dn_objset->os_spa,
1906 db->db.db_size, db, type));
1907 bcopy(dr->dt.dl.dr_data->b_data, db->db.db_data,
1912 (void) refcount_add(&db->db_holds, tag);
1913 dbuf_update_data(db);
1915 mutex_exit(&db->db_mtx);
1917 /* NOTE: we can't rele the parent until after we drop the db_mtx */
1919 dbuf_rele(parent, NULL);
1921 ASSERT3P(DB_DNODE(db), ==, dn);
1922 ASSERT3U(db->db_blkid, ==, blkid);
1923 ASSERT3U(db->db_level, ==, level);
1930 dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag)
1933 int err = dbuf_hold_impl(dn, 0, blkid, FALSE, tag, &db);
1934 return (err ? NULL : db);
1938 dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag)
1941 int err = dbuf_hold_impl(dn, level, blkid, FALSE, tag, &db);
1942 return (err ? NULL : db);
1946 dbuf_create_bonus(dnode_t *dn)
1948 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
1950 ASSERT(dn->dn_bonus == NULL);
1951 dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL);
1955 dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx)
1957 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1960 if (db->db_blkid != DMU_SPILL_BLKID)
1963 blksz = SPA_MINBLOCKSIZE;
1964 if (blksz > SPA_MAXBLOCKSIZE)
1965 blksz = SPA_MAXBLOCKSIZE;
1967 blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE);
1971 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1972 dbuf_new_size(db, blksz, tx);
1973 rw_exit(&dn->dn_struct_rwlock);
1980 dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx)
1982 dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx);
1985 #pragma weak dmu_buf_add_ref = dbuf_add_ref
1987 dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
1989 int64_t holds = refcount_add(&db->db_holds, tag);
1994 * If you call dbuf_rele() you had better not be referencing the dnode handle
1995 * unless you have some other direct or indirect hold on the dnode. (An indirect
1996 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
1997 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
1998 * dnode's parent dbuf evicting its dnode handles.
2000 #pragma weak dmu_buf_rele = dbuf_rele
2002 dbuf_rele(dmu_buf_impl_t *db, void *tag)
2004 mutex_enter(&db->db_mtx);
2005 dbuf_rele_and_unlock(db, tag);
2009 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2010 * db_dirtycnt and db_holds to be updated atomically.
2013 dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag)
2017 ASSERT(MUTEX_HELD(&db->db_mtx));
2021 * Remove the reference to the dbuf before removing its hold on the
2022 * dnode so we can guarantee in dnode_move() that a referenced bonus
2023 * buffer has a corresponding dnode hold.
2025 holds = refcount_remove(&db->db_holds, tag);
2029 * We can't freeze indirects if there is a possibility that they
2030 * may be modified in the current syncing context.
2032 if (db->db_buf && holds == (db->db_level == 0 ? db->db_dirtycnt : 0))
2033 arc_buf_freeze(db->db_buf);
2035 if (holds == db->db_dirtycnt &&
2036 db->db_level == 0 && db->db_immediate_evict)
2037 dbuf_evict_user(db);
2040 if (db->db_blkid == DMU_BONUS_BLKID) {
2041 mutex_exit(&db->db_mtx);
2044 * If the dnode moves here, we cannot cross this barrier
2045 * until the move completes.
2048 (void) atomic_dec_32_nv(&DB_DNODE(db)->dn_dbufs_count);
2051 * The bonus buffer's dnode hold is no longer discounted
2052 * in dnode_move(). The dnode cannot move until after
2055 dnode_rele(DB_DNODE(db), db);
2056 } else if (db->db_buf == NULL) {
2058 * This is a special case: we never associated this
2059 * dbuf with any data allocated from the ARC.
2061 ASSERT(db->db_state == DB_UNCACHED ||
2062 db->db_state == DB_NOFILL);
2064 } else if (arc_released(db->db_buf)) {
2065 arc_buf_t *buf = db->db_buf;
2067 * This dbuf has anonymous data associated with it.
2069 dbuf_set_data(db, NULL);
2070 VERIFY(arc_buf_remove_ref(buf, db) == 1);
2073 VERIFY(arc_buf_remove_ref(db->db_buf, db) == 0);
2076 * A dbuf will be eligible for eviction if either the
2077 * 'primarycache' property is set or a duplicate
2078 * copy of this buffer is already cached in the arc.
2080 * In the case of the 'primarycache' a buffer
2081 * is considered for eviction if it matches the
2082 * criteria set in the property.
2084 * To decide if our buffer is considered a
2085 * duplicate, we must call into the arc to determine
2086 * if multiple buffers are referencing the same
2087 * block on-disk. If so, then we simply evict
2090 if (!DBUF_IS_CACHEABLE(db) ||
2091 arc_buf_eviction_needed(db->db_buf))
2094 mutex_exit(&db->db_mtx);
2097 mutex_exit(&db->db_mtx);
2101 #pragma weak dmu_buf_refcount = dbuf_refcount
2103 dbuf_refcount(dmu_buf_impl_t *db)
2105 return (refcount_count(&db->db_holds));
2109 dmu_buf_set_user(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr,
2110 dmu_buf_evict_func_t *evict_func)
2112 return (dmu_buf_update_user(db_fake, NULL, user_ptr,
2113 user_data_ptr_ptr, evict_func));
2117 dmu_buf_set_user_ie(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr,
2118 dmu_buf_evict_func_t *evict_func)
2120 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2122 db->db_immediate_evict = TRUE;
2123 return (dmu_buf_update_user(db_fake, NULL, user_ptr,
2124 user_data_ptr_ptr, evict_func));
2128 dmu_buf_update_user(dmu_buf_t *db_fake, void *old_user_ptr, void *user_ptr,
2129 void *user_data_ptr_ptr, dmu_buf_evict_func_t *evict_func)
2131 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2132 ASSERT(db->db_level == 0);
2134 ASSERT((user_ptr == NULL) == (evict_func == NULL));
2136 mutex_enter(&db->db_mtx);
2138 if (db->db_user_ptr == old_user_ptr) {
2139 db->db_user_ptr = user_ptr;
2140 db->db_user_data_ptr_ptr = user_data_ptr_ptr;
2141 db->db_evict_func = evict_func;
2143 dbuf_update_data(db);
2145 old_user_ptr = db->db_user_ptr;
2148 mutex_exit(&db->db_mtx);
2149 return (old_user_ptr);
2153 dmu_buf_get_user(dmu_buf_t *db_fake)
2155 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2156 ASSERT(!refcount_is_zero(&db->db_holds));
2158 return (db->db_user_ptr);
2162 dmu_buf_freeable(dmu_buf_t *dbuf)
2164 boolean_t res = B_FALSE;
2165 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
2168 res = dsl_dataset_block_freeable(db->db_objset->os_dsl_dataset,
2169 db->db_blkptr, db->db_blkptr->blk_birth);
2175 dmu_buf_get_blkptr(dmu_buf_t *db)
2177 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2178 return (dbi->db_blkptr);
2182 dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db)
2184 /* ASSERT(dmu_tx_is_syncing(tx) */
2185 ASSERT(MUTEX_HELD(&db->db_mtx));
2187 if (db->db_blkptr != NULL)
2190 if (db->db_blkid == DMU_SPILL_BLKID) {
2191 db->db_blkptr = &dn->dn_phys->dn_spill;
2192 BP_ZERO(db->db_blkptr);
2195 if (db->db_level == dn->dn_phys->dn_nlevels-1) {
2197 * This buffer was allocated at a time when there was
2198 * no available blkptrs from the dnode, or it was
2199 * inappropriate to hook it in (i.e., nlevels mis-match).
2201 ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr);
2202 ASSERT(db->db_parent == NULL);
2203 db->db_parent = dn->dn_dbuf;
2204 db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
2207 dmu_buf_impl_t *parent = db->db_parent;
2208 int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2210 ASSERT(dn->dn_phys->dn_nlevels > 1);
2211 if (parent == NULL) {
2212 mutex_exit(&db->db_mtx);
2213 rw_enter(&dn->dn_struct_rwlock, RW_READER);
2214 (void) dbuf_hold_impl(dn, db->db_level+1,
2215 db->db_blkid >> epbs, FALSE, db, &parent);
2216 rw_exit(&dn->dn_struct_rwlock);
2217 mutex_enter(&db->db_mtx);
2218 db->db_parent = parent;
2220 db->db_blkptr = (blkptr_t *)parent->db.db_data +
2221 (db->db_blkid & ((1ULL << epbs) - 1));
2227 dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2229 dmu_buf_impl_t *db = dr->dr_dbuf;
2233 ASSERT(dmu_tx_is_syncing(tx));
2235 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2237 mutex_enter(&db->db_mtx);
2239 ASSERT(db->db_level > 0);
2242 if (db->db_buf == NULL) {
2243 mutex_exit(&db->db_mtx);
2244 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
2245 mutex_enter(&db->db_mtx);
2247 ASSERT3U(db->db_state, ==, DB_CACHED);
2248 ASSERT(db->db_buf != NULL);
2252 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2253 dbuf_check_blkptr(dn, db);
2256 db->db_data_pending = dr;
2258 mutex_exit(&db->db_mtx);
2259 dbuf_write(dr, db->db_buf, tx);
2262 mutex_enter(&dr->dt.di.dr_mtx);
2263 dbuf_sync_list(&dr->dt.di.dr_children, tx);
2264 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
2265 mutex_exit(&dr->dt.di.dr_mtx);
2270 dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2272 arc_buf_t **datap = &dr->dt.dl.dr_data;
2273 dmu_buf_impl_t *db = dr->dr_dbuf;
2276 uint64_t txg = tx->tx_txg;
2278 ASSERT(dmu_tx_is_syncing(tx));
2280 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2282 mutex_enter(&db->db_mtx);
2284 * To be synced, we must be dirtied. But we
2285 * might have been freed after the dirty.
2287 if (db->db_state == DB_UNCACHED) {
2288 /* This buffer has been freed since it was dirtied */
2289 ASSERT(db->db.db_data == NULL);
2290 } else if (db->db_state == DB_FILL) {
2291 /* This buffer was freed and is now being re-filled */
2292 ASSERT(db->db.db_data != dr->dt.dl.dr_data);
2294 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL);
2301 if (db->db_blkid == DMU_SPILL_BLKID) {
2302 mutex_enter(&dn->dn_mtx);
2303 dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR;
2304 mutex_exit(&dn->dn_mtx);
2308 * If this is a bonus buffer, simply copy the bonus data into the
2309 * dnode. It will be written out when the dnode is synced (and it
2310 * will be synced, since it must have been dirty for dbuf_sync to
2313 if (db->db_blkid == DMU_BONUS_BLKID) {
2314 dbuf_dirty_record_t **drp;
2316 ASSERT(*datap != NULL);
2317 ASSERT0(db->db_level);
2318 ASSERT3U(dn->dn_phys->dn_bonuslen, <=, DN_MAX_BONUSLEN);
2319 bcopy(*datap, DN_BONUS(dn->dn_phys), dn->dn_phys->dn_bonuslen);
2322 if (*datap != db->db.db_data) {
2323 zio_buf_free(*datap, DN_MAX_BONUSLEN);
2324 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
2326 db->db_data_pending = NULL;
2327 drp = &db->db_last_dirty;
2329 drp = &(*drp)->dr_next;
2330 ASSERT(dr->dr_next == NULL);
2331 ASSERT(dr->dr_dbuf == db);
2333 if (dr->dr_dbuf->db_level != 0) {
2334 list_destroy(&dr->dt.di.dr_children);
2335 mutex_destroy(&dr->dt.di.dr_mtx);
2337 kmem_free(dr, sizeof (dbuf_dirty_record_t));
2338 ASSERT(db->db_dirtycnt > 0);
2339 db->db_dirtycnt -= 1;
2340 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
2347 * This function may have dropped the db_mtx lock allowing a dmu_sync
2348 * operation to sneak in. As a result, we need to ensure that we
2349 * don't check the dr_override_state until we have returned from
2350 * dbuf_check_blkptr.
2352 dbuf_check_blkptr(dn, db);
2355 * If this buffer is in the middle of an immediate write,
2356 * wait for the synchronous IO to complete.
2358 while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
2359 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
2360 cv_wait(&db->db_changed, &db->db_mtx);
2361 ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN);
2364 if (db->db_state != DB_NOFILL &&
2365 dn->dn_object != DMU_META_DNODE_OBJECT &&
2366 refcount_count(&db->db_holds) > 1 &&
2367 dr->dt.dl.dr_override_state != DR_OVERRIDDEN &&
2368 *datap == db->db_buf) {
2370 * If this buffer is currently "in use" (i.e., there
2371 * are active holds and db_data still references it),
2372 * then make a copy before we start the write so that
2373 * any modifications from the open txg will not leak
2376 * NOTE: this copy does not need to be made for
2377 * objects only modified in the syncing context (e.g.
2378 * DNONE_DNODE blocks).
2380 int blksz = arc_buf_size(*datap);
2381 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
2382 *datap = arc_buf_alloc(os->os_spa, blksz, db, type);
2383 bcopy(db->db.db_data, (*datap)->b_data, blksz);
2385 db->db_data_pending = dr;
2387 mutex_exit(&db->db_mtx);
2389 dbuf_write(dr, *datap, tx);
2391 ASSERT(!list_link_active(&dr->dr_dirty_node));
2392 if (dn->dn_object == DMU_META_DNODE_OBJECT) {
2393 list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr);
2397 * Although zio_nowait() does not "wait for an IO", it does
2398 * initiate the IO. If this is an empty write it seems plausible
2399 * that the IO could actually be completed before the nowait
2400 * returns. We need to DB_DNODE_EXIT() first in case
2401 * zio_nowait() invalidates the dbuf.
2404 zio_nowait(dr->dr_zio);
2409 dbuf_sync_list(list_t *list, dmu_tx_t *tx)
2411 dbuf_dirty_record_t *dr;
2413 while (dr = list_head(list)) {
2414 if (dr->dr_zio != NULL) {
2416 * If we find an already initialized zio then we
2417 * are processing the meta-dnode, and we have finished.
2418 * The dbufs for all dnodes are put back on the list
2419 * during processing, so that we can zio_wait()
2420 * these IOs after initiating all child IOs.
2422 ASSERT3U(dr->dr_dbuf->db.db_object, ==,
2423 DMU_META_DNODE_OBJECT);
2426 list_remove(list, dr);
2427 if (dr->dr_dbuf->db_level > 0)
2428 dbuf_sync_indirect(dr, tx);
2430 dbuf_sync_leaf(dr, tx);
2436 dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
2438 dmu_buf_impl_t *db = vdb;
2440 blkptr_t *bp = zio->io_bp;
2441 blkptr_t *bp_orig = &zio->io_bp_orig;
2442 spa_t *spa = zio->io_spa;
2447 ASSERT(db->db_blkptr == bp);
2451 delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig);
2452 dnode_diduse_space(dn, delta - zio->io_prev_space_delta);
2453 zio->io_prev_space_delta = delta;
2455 if (BP_IS_HOLE(bp)) {
2456 ASSERT(bp->blk_fill == 0);
2461 ASSERT((db->db_blkid != DMU_SPILL_BLKID &&
2462 BP_GET_TYPE(bp) == dn->dn_type) ||
2463 (db->db_blkid == DMU_SPILL_BLKID &&
2464 BP_GET_TYPE(bp) == dn->dn_bonustype));
2465 ASSERT(BP_GET_LEVEL(bp) == db->db_level);
2467 mutex_enter(&db->db_mtx);
2470 if (db->db_blkid == DMU_SPILL_BLKID) {
2471 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
2472 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
2473 db->db_blkptr == &dn->dn_phys->dn_spill);
2477 if (db->db_level == 0) {
2478 mutex_enter(&dn->dn_mtx);
2479 if (db->db_blkid > dn->dn_phys->dn_maxblkid &&
2480 db->db_blkid != DMU_SPILL_BLKID)
2481 dn->dn_phys->dn_maxblkid = db->db_blkid;
2482 mutex_exit(&dn->dn_mtx);
2484 if (dn->dn_type == DMU_OT_DNODE) {
2485 dnode_phys_t *dnp = db->db.db_data;
2486 for (i = db->db.db_size >> DNODE_SHIFT; i > 0;
2488 if (dnp->dn_type != DMU_OT_NONE)
2495 blkptr_t *ibp = db->db.db_data;
2496 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2497 for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) {
2498 if (BP_IS_HOLE(ibp))
2500 fill += ibp->blk_fill;
2505 bp->blk_fill = fill;
2507 mutex_exit(&db->db_mtx);
2512 dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
2514 dmu_buf_impl_t *db = vdb;
2515 blkptr_t *bp = zio->io_bp;
2516 blkptr_t *bp_orig = &zio->io_bp_orig;
2517 uint64_t txg = zio->io_txg;
2518 dbuf_dirty_record_t **drp, *dr;
2520 ASSERT0(zio->io_error);
2521 ASSERT(db->db_blkptr == bp);
2524 * For nopwrites and rewrites we ensure that the bp matches our
2525 * original and bypass all the accounting.
2527 if (zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)) {
2528 ASSERT(BP_EQUAL(bp, bp_orig));
2534 DB_GET_OBJSET(&os, db);
2535 ds = os->os_dsl_dataset;
2538 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
2539 dsl_dataset_block_born(ds, bp, tx);
2542 mutex_enter(&db->db_mtx);
2546 drp = &db->db_last_dirty;
2547 while ((dr = *drp) != db->db_data_pending)
2549 ASSERT(!list_link_active(&dr->dr_dirty_node));
2550 ASSERT(dr->dr_txg == txg);
2551 ASSERT(dr->dr_dbuf == db);
2552 ASSERT(dr->dr_next == NULL);
2556 if (db->db_blkid == DMU_SPILL_BLKID) {
2561 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
2562 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
2563 db->db_blkptr == &dn->dn_phys->dn_spill);
2568 if (db->db_level == 0) {
2569 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2570 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
2571 if (db->db_state != DB_NOFILL) {
2572 if (dr->dt.dl.dr_data != db->db_buf)
2573 VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data,
2575 else if (!arc_released(db->db_buf))
2576 arc_set_callback(db->db_buf, dbuf_do_evict, db);
2583 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
2584 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2585 if (!BP_IS_HOLE(db->db_blkptr)) {
2587 dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2588 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
2590 ASSERT3U(dn->dn_phys->dn_maxblkid
2591 >> (db->db_level * epbs), >=, db->db_blkid);
2592 arc_set_callback(db->db_buf, dbuf_do_evict, db);
2595 mutex_destroy(&dr->dt.di.dr_mtx);
2596 list_destroy(&dr->dt.di.dr_children);
2598 kmem_free(dr, sizeof (dbuf_dirty_record_t));
2600 cv_broadcast(&db->db_changed);
2601 ASSERT(db->db_dirtycnt > 0);
2602 db->db_dirtycnt -= 1;
2603 db->db_data_pending = NULL;
2604 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
2608 dbuf_write_nofill_ready(zio_t *zio)
2610 dbuf_write_ready(zio, NULL, zio->io_private);
2614 dbuf_write_nofill_done(zio_t *zio)
2616 dbuf_write_done(zio, NULL, zio->io_private);
2620 dbuf_write_override_ready(zio_t *zio)
2622 dbuf_dirty_record_t *dr = zio->io_private;
2623 dmu_buf_impl_t *db = dr->dr_dbuf;
2625 dbuf_write_ready(zio, NULL, db);
2629 dbuf_write_override_done(zio_t *zio)
2631 dbuf_dirty_record_t *dr = zio->io_private;
2632 dmu_buf_impl_t *db = dr->dr_dbuf;
2633 blkptr_t *obp = &dr->dt.dl.dr_overridden_by;
2635 mutex_enter(&db->db_mtx);
2636 if (!BP_EQUAL(zio->io_bp, obp)) {
2637 if (!BP_IS_HOLE(obp))
2638 dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp);
2639 arc_release(dr->dt.dl.dr_data, db);
2641 mutex_exit(&db->db_mtx);
2643 dbuf_write_done(zio, NULL, db);
2647 dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx)
2649 dmu_buf_impl_t *db = dr->dr_dbuf;
2652 dmu_buf_impl_t *parent = db->db_parent;
2653 uint64_t txg = tx->tx_txg;
2663 if (db->db_state != DB_NOFILL) {
2664 if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) {
2666 * Private object buffers are released here rather
2667 * than in dbuf_dirty() since they are only modified
2668 * in the syncing context and we don't want the
2669 * overhead of making multiple copies of the data.
2671 if (BP_IS_HOLE(db->db_blkptr)) {
2674 dbuf_release_bp(db);
2679 if (parent != dn->dn_dbuf) {
2680 ASSERT(parent && parent->db_data_pending);
2681 ASSERT(db->db_level == parent->db_level-1);
2682 ASSERT(arc_released(parent->db_buf));
2683 zio = parent->db_data_pending->dr_zio;
2685 ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 &&
2686 db->db_blkid != DMU_SPILL_BLKID) ||
2687 (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0));
2688 if (db->db_blkid != DMU_SPILL_BLKID)
2689 ASSERT3P(db->db_blkptr, ==,
2690 &dn->dn_phys->dn_blkptr[db->db_blkid]);
2694 ASSERT(db->db_level == 0 || data == db->db_buf);
2695 ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
2698 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
2699 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
2700 db->db.db_object, db->db_level, db->db_blkid);
2702 if (db->db_blkid == DMU_SPILL_BLKID)
2704 wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
2706 dmu_write_policy(os, dn, db->db_level, wp_flag, &zp);
2709 if (db->db_level == 0 && dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
2710 ASSERT(db->db_state != DB_NOFILL);
2711 dr->dr_zio = zio_write(zio, os->os_spa, txg,
2712 db->db_blkptr, data->b_data, arc_buf_size(data), &zp,
2713 dbuf_write_override_ready, dbuf_write_override_done, dr,
2714 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
2715 mutex_enter(&db->db_mtx);
2716 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
2717 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by,
2718 dr->dt.dl.dr_copies, dr->dt.dl.dr_nopwrite);
2719 mutex_exit(&db->db_mtx);
2720 } else if (db->db_state == DB_NOFILL) {
2721 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF);
2722 dr->dr_zio = zio_write(zio, os->os_spa, txg,
2723 db->db_blkptr, NULL, db->db.db_size, &zp,
2724 dbuf_write_nofill_ready, dbuf_write_nofill_done, db,
2725 ZIO_PRIORITY_ASYNC_WRITE,
2726 ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb);
2728 ASSERT(arc_released(data));
2729 dr->dr_zio = arc_write(zio, os->os_spa, txg,
2730 db->db_blkptr, data, DBUF_IS_L2CACHEABLE(db), &zp,
2731 dbuf_write_ready, dbuf_write_done, db,
2732 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);