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;
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;
573 SET_BOOKMARK(&zb, db->db_objset->os_dsl_dataset ?
574 db->db_objset->os_dsl_dataset->ds_object : DMU_META_OBJSET,
575 db->db.db_object, db->db_level, db->db_blkid);
577 dbuf_add_ref(db, NULL);
578 /* ZIO_FLAG_CANFAIL callers have to check the parent zio's error */
581 pbuf = db->db_parent->db_buf;
583 pbuf = db->db_objset->os_phys_buf;
585 (void) dsl_read(zio, spa, db->db_blkptr, pbuf,
586 dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ,
587 (*flags & DB_RF_CANFAIL) ? ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED,
589 if (aflags & ARC_CACHED)
590 *flags |= DB_RF_CACHED;
594 dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
597 int havepzio = (zio != NULL);
602 * We don't have to hold the mutex to check db_state because it
603 * can't be freed while we have a hold on the buffer.
605 ASSERT(!refcount_is_zero(&db->db_holds));
607 if (db->db_state == DB_NOFILL)
612 if ((flags & DB_RF_HAVESTRUCT) == 0)
613 rw_enter(&dn->dn_struct_rwlock, RW_READER);
615 prefetch = db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
616 (flags & DB_RF_NOPREFETCH) == 0 && dn != NULL &&
617 DBUF_IS_CACHEABLE(db);
619 mutex_enter(&db->db_mtx);
620 if (db->db_state == DB_CACHED) {
621 mutex_exit(&db->db_mtx);
623 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
624 db->db.db_size, TRUE);
625 if ((flags & DB_RF_HAVESTRUCT) == 0)
626 rw_exit(&dn->dn_struct_rwlock);
628 } else if (db->db_state == DB_UNCACHED) {
629 spa_t *spa = dn->dn_objset->os_spa;
632 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
633 dbuf_read_impl(db, zio, &flags);
635 /* dbuf_read_impl has dropped db_mtx for us */
638 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
639 db->db.db_size, flags & DB_RF_CACHED);
641 if ((flags & DB_RF_HAVESTRUCT) == 0)
642 rw_exit(&dn->dn_struct_rwlock);
648 mutex_exit(&db->db_mtx);
650 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
651 db->db.db_size, TRUE);
652 if ((flags & DB_RF_HAVESTRUCT) == 0)
653 rw_exit(&dn->dn_struct_rwlock);
656 mutex_enter(&db->db_mtx);
657 if ((flags & DB_RF_NEVERWAIT) == 0) {
658 while (db->db_state == DB_READ ||
659 db->db_state == DB_FILL) {
660 ASSERT(db->db_state == DB_READ ||
661 (flags & DB_RF_HAVESTRUCT) == 0);
662 cv_wait(&db->db_changed, &db->db_mtx);
664 if (db->db_state == DB_UNCACHED)
667 mutex_exit(&db->db_mtx);
670 ASSERT(err || havepzio || db->db_state == DB_CACHED);
675 dbuf_noread(dmu_buf_impl_t *db)
677 ASSERT(!refcount_is_zero(&db->db_holds));
678 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
679 mutex_enter(&db->db_mtx);
680 while (db->db_state == DB_READ || db->db_state == DB_FILL)
681 cv_wait(&db->db_changed, &db->db_mtx);
682 if (db->db_state == DB_UNCACHED) {
683 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
686 ASSERT(db->db_buf == NULL);
687 ASSERT(db->db.db_data == NULL);
688 DB_GET_SPA(&spa, db);
689 dbuf_set_data(db, arc_buf_alloc(spa, db->db.db_size, db, type));
690 db->db_state = DB_FILL;
691 } else if (db->db_state == DB_NOFILL) {
692 dbuf_set_data(db, NULL);
694 ASSERT3U(db->db_state, ==, DB_CACHED);
696 mutex_exit(&db->db_mtx);
700 * This is our just-in-time copy function. It makes a copy of
701 * buffers, that have been modified in a previous transaction
702 * group, before we modify them in the current active group.
704 * This function is used in two places: when we are dirtying a
705 * buffer for the first time in a txg, and when we are freeing
706 * a range in a dnode that includes this buffer.
708 * Note that when we are called from dbuf_free_range() we do
709 * not put a hold on the buffer, we just traverse the active
710 * dbuf list for the dnode.
713 dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg)
715 dbuf_dirty_record_t *dr = db->db_last_dirty;
717 ASSERT(MUTEX_HELD(&db->db_mtx));
718 ASSERT(db->db.db_data != NULL);
719 ASSERT(db->db_level == 0);
720 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT);
723 (dr->dt.dl.dr_data !=
724 ((db->db_blkid == DMU_BONUS_BLKID) ? db->db.db_data : db->db_buf)))
728 * If the last dirty record for this dbuf has not yet synced
729 * and its referencing the dbuf data, either:
730 * reset the reference to point to a new copy,
731 * or (if there a no active holders)
732 * just null out the current db_data pointer.
734 ASSERT(dr->dr_txg >= txg - 2);
735 if (db->db_blkid == DMU_BONUS_BLKID) {
736 /* Note that the data bufs here are zio_bufs */
737 dr->dt.dl.dr_data = zio_buf_alloc(DN_MAX_BONUSLEN);
738 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
739 bcopy(db->db.db_data, dr->dt.dl.dr_data, DN_MAX_BONUSLEN);
740 } else if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
741 int size = db->db.db_size;
742 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
745 DB_GET_SPA(&spa, db);
746 dr->dt.dl.dr_data = arc_buf_alloc(spa, size, db, type);
747 bcopy(db->db.db_data, dr->dt.dl.dr_data->b_data, size);
749 dbuf_set_data(db, NULL);
754 dbuf_unoverride(dbuf_dirty_record_t *dr)
756 dmu_buf_impl_t *db = dr->dr_dbuf;
757 blkptr_t *bp = &dr->dt.dl.dr_overridden_by;
758 uint64_t txg = dr->dr_txg;
760 ASSERT(MUTEX_HELD(&db->db_mtx));
761 ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC);
762 ASSERT(db->db_level == 0);
764 if (db->db_blkid == DMU_BONUS_BLKID ||
765 dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN)
768 ASSERT(db->db_data_pending != dr);
770 /* free this block */
771 if (!BP_IS_HOLE(bp)) {
774 DB_GET_SPA(&spa, db);
775 zio_free(spa, txg, bp);
777 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
779 * Release the already-written buffer, so we leave it in
780 * a consistent dirty state. Note that all callers are
781 * modifying the buffer, so they will immediately do
782 * another (redundant) arc_release(). Therefore, leave
783 * the buf thawed to save the effort of freezing &
784 * immediately re-thawing it.
786 arc_release(dr->dt.dl.dr_data, db);
790 * Evict (if its unreferenced) or clear (if its referenced) any level-0
791 * data blocks in the free range, so that any future readers will find
792 * empty blocks. Also, if we happen accross any level-1 dbufs in the
793 * range that have not already been marked dirty, mark them dirty so
794 * they stay in memory.
797 dbuf_free_range(dnode_t *dn, uint64_t start, uint64_t end, dmu_tx_t *tx)
799 dmu_buf_impl_t *db, *db_next;
800 uint64_t txg = tx->tx_txg;
801 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
802 uint64_t first_l1 = start >> epbs;
803 uint64_t last_l1 = end >> epbs;
805 if (end > dn->dn_maxblkid && (end != DMU_SPILL_BLKID)) {
806 end = dn->dn_maxblkid;
807 last_l1 = end >> epbs;
809 dprintf_dnode(dn, "start=%llu end=%llu\n", start, end);
810 mutex_enter(&dn->dn_dbufs_mtx);
811 for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
812 db_next = list_next(&dn->dn_dbufs, db);
813 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
815 if (db->db_level == 1 &&
816 db->db_blkid >= first_l1 && db->db_blkid <= last_l1) {
817 mutex_enter(&db->db_mtx);
818 if (db->db_last_dirty &&
819 db->db_last_dirty->dr_txg < txg) {
820 dbuf_add_ref(db, FTAG);
821 mutex_exit(&db->db_mtx);
822 dbuf_will_dirty(db, tx);
825 mutex_exit(&db->db_mtx);
829 if (db->db_level != 0)
831 dprintf_dbuf(db, "found buf %s\n", "");
832 if (db->db_blkid < start || db->db_blkid > end)
835 /* found a level 0 buffer in the range */
836 if (dbuf_undirty(db, tx))
839 mutex_enter(&db->db_mtx);
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) == 1);
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)
985 DB_GET_OBJSET(&os, db);
986 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
987 ASSERT(arc_released(os->os_phys_buf) ||
988 list_link_active(&os->os_dsl_dataset->ds_synced_link));
989 ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf));
991 zb.zb_objset = os->os_dsl_dataset ?
992 os->os_dsl_dataset->ds_object : 0;
993 zb.zb_object = db->db.db_object;
994 zb.zb_level = db->db_level;
995 zb.zb_blkid = db->db_blkid;
996 (void) arc_release_bp(db->db_buf, db,
997 db->db_blkptr, os->os_spa, &zb);
1000 dbuf_dirty_record_t *
1001 dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1005 dbuf_dirty_record_t **drp, *dr;
1006 int drop_struct_lock = FALSE;
1007 boolean_t do_free_accounting = B_FALSE;
1008 int txgoff = tx->tx_txg & TXG_MASK;
1010 ASSERT(tx->tx_txg != 0);
1011 ASSERT(!refcount_is_zero(&db->db_holds));
1012 DMU_TX_DIRTY_BUF(tx, db);
1017 * Shouldn't dirty a regular buffer in syncing context. Private
1018 * objects may be dirtied in syncing context, but only if they
1019 * were already pre-dirtied in open context.
1021 ASSERT(!dmu_tx_is_syncing(tx) ||
1022 BP_IS_HOLE(dn->dn_objset->os_rootbp) ||
1023 DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1024 dn->dn_objset->os_dsl_dataset == NULL);
1026 * We make this assert for private objects as well, but after we
1027 * check if we're already dirty. They are allowed to re-dirty
1028 * in syncing context.
1030 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1031 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1032 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1034 mutex_enter(&db->db_mtx);
1036 * XXX make this true for indirects too? The problem is that
1037 * transactions created with dmu_tx_create_assigned() from
1038 * syncing context don't bother holding ahead.
1040 ASSERT(db->db_level != 0 ||
1041 db->db_state == DB_CACHED || db->db_state == DB_FILL ||
1042 db->db_state == DB_NOFILL);
1044 mutex_enter(&dn->dn_mtx);
1046 * Don't set dirtyctx to SYNC if we're just modifying this as we
1047 * initialize the objset.
1049 if (dn->dn_dirtyctx == DN_UNDIRTIED &&
1050 !BP_IS_HOLE(dn->dn_objset->os_rootbp)) {
1052 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN);
1053 ASSERT(dn->dn_dirtyctx_firstset == NULL);
1054 dn->dn_dirtyctx_firstset = kmem_alloc(1, KM_SLEEP);
1056 mutex_exit(&dn->dn_mtx);
1058 if (db->db_blkid == DMU_SPILL_BLKID)
1059 dn->dn_have_spill = B_TRUE;
1062 * If this buffer is already dirty, we're done.
1064 drp = &db->db_last_dirty;
1065 ASSERT(*drp == NULL || (*drp)->dr_txg <= tx->tx_txg ||
1066 db->db.db_object == DMU_META_DNODE_OBJECT);
1067 while ((dr = *drp) != NULL && dr->dr_txg > tx->tx_txg)
1069 if (dr && dr->dr_txg == tx->tx_txg) {
1072 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID) {
1074 * If this buffer has already been written out,
1075 * we now need to reset its state.
1077 dbuf_unoverride(dr);
1078 if (db->db.db_object != DMU_META_DNODE_OBJECT &&
1079 db->db_state != DB_NOFILL)
1080 arc_buf_thaw(db->db_buf);
1082 mutex_exit(&db->db_mtx);
1087 * Only valid if not already dirty.
1089 ASSERT(dn->dn_object == 0 ||
1090 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1091 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1093 ASSERT3U(dn->dn_nlevels, >, db->db_level);
1094 ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) ||
1095 dn->dn_phys->dn_nlevels > db->db_level ||
1096 dn->dn_next_nlevels[txgoff] > db->db_level ||
1097 dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level ||
1098 dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level);
1101 * We should only be dirtying in syncing context if it's the
1102 * mos or we're initializing the os or it's a special object.
1103 * However, we are allowed to dirty in syncing context provided
1104 * we already dirtied it in open context. Hence we must make
1105 * this assertion only if we're not already dirty.
1108 ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1109 os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp));
1110 ASSERT(db->db.db_size != 0);
1112 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1114 if (db->db_blkid != DMU_BONUS_BLKID) {
1116 * Update the accounting.
1117 * Note: we delay "free accounting" until after we drop
1118 * the db_mtx. This keeps us from grabbing other locks
1119 * (and possibly deadlocking) in bp_get_dsize() while
1120 * also holding the db_mtx.
1122 dnode_willuse_space(dn, db->db.db_size, tx);
1123 do_free_accounting = dbuf_block_freeable(db);
1127 * If this buffer is dirty in an old transaction group we need
1128 * to make a copy of it so that the changes we make in this
1129 * transaction group won't leak out when we sync the older txg.
1131 dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP);
1132 if (db->db_level == 0) {
1133 void *data_old = db->db_buf;
1135 if (db->db_state != DB_NOFILL) {
1136 if (db->db_blkid == DMU_BONUS_BLKID) {
1137 dbuf_fix_old_data(db, tx->tx_txg);
1138 data_old = db->db.db_data;
1139 } else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
1141 * Release the data buffer from the cache so
1142 * that we can modify it without impacting
1143 * possible other users of this cached data
1144 * block. Note that indirect blocks and
1145 * private objects are not released until the
1146 * syncing state (since they are only modified
1149 arc_release(db->db_buf, db);
1150 dbuf_fix_old_data(db, tx->tx_txg);
1151 data_old = db->db_buf;
1153 ASSERT(data_old != NULL);
1155 dr->dt.dl.dr_data = data_old;
1157 mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_DEFAULT, NULL);
1158 list_create(&dr->dt.di.dr_children,
1159 sizeof (dbuf_dirty_record_t),
1160 offsetof(dbuf_dirty_record_t, dr_dirty_node));
1163 dr->dr_txg = tx->tx_txg;
1168 * We could have been freed_in_flight between the dbuf_noread
1169 * and dbuf_dirty. We win, as though the dbuf_noread() had
1170 * happened after the free.
1172 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1173 db->db_blkid != DMU_SPILL_BLKID) {
1174 mutex_enter(&dn->dn_mtx);
1175 dnode_clear_range(dn, db->db_blkid, 1, tx);
1176 mutex_exit(&dn->dn_mtx);
1177 db->db_freed_in_flight = FALSE;
1181 * This buffer is now part of this txg
1183 dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg);
1184 db->db_dirtycnt += 1;
1185 ASSERT3U(db->db_dirtycnt, <=, 3);
1187 mutex_exit(&db->db_mtx);
1189 if (db->db_blkid == DMU_BONUS_BLKID ||
1190 db->db_blkid == DMU_SPILL_BLKID) {
1191 mutex_enter(&dn->dn_mtx);
1192 ASSERT(!list_link_active(&dr->dr_dirty_node));
1193 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1194 mutex_exit(&dn->dn_mtx);
1195 dnode_setdirty(dn, tx);
1198 } else if (do_free_accounting) {
1199 blkptr_t *bp = db->db_blkptr;
1200 int64_t willfree = (bp && !BP_IS_HOLE(bp)) ?
1201 bp_get_dsize(os->os_spa, bp) : db->db.db_size;
1203 * This is only a guess -- if the dbuf is dirty
1204 * in a previous txg, we don't know how much
1205 * space it will use on disk yet. We should
1206 * really have the struct_rwlock to access
1207 * db_blkptr, but since this is just a guess,
1208 * it's OK if we get an odd answer.
1210 ddt_prefetch(os->os_spa, bp);
1211 dnode_willuse_space(dn, -willfree, tx);
1214 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
1215 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1216 drop_struct_lock = TRUE;
1219 if (db->db_level == 0) {
1220 dnode_new_blkid(dn, db->db_blkid, tx, drop_struct_lock);
1221 ASSERT(dn->dn_maxblkid >= db->db_blkid);
1224 if (db->db_level+1 < dn->dn_nlevels) {
1225 dmu_buf_impl_t *parent = db->db_parent;
1226 dbuf_dirty_record_t *di;
1227 int parent_held = FALSE;
1229 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
1230 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1232 parent = dbuf_hold_level(dn, db->db_level+1,
1233 db->db_blkid >> epbs, FTAG);
1234 ASSERT(parent != NULL);
1237 if (drop_struct_lock)
1238 rw_exit(&dn->dn_struct_rwlock);
1239 ASSERT3U(db->db_level+1, ==, parent->db_level);
1240 di = dbuf_dirty(parent, tx);
1242 dbuf_rele(parent, FTAG);
1244 mutex_enter(&db->db_mtx);
1245 /* possible race with dbuf_undirty() */
1246 if (db->db_last_dirty == dr ||
1247 dn->dn_object == DMU_META_DNODE_OBJECT) {
1248 mutex_enter(&di->dt.di.dr_mtx);
1249 ASSERT3U(di->dr_txg, ==, tx->tx_txg);
1250 ASSERT(!list_link_active(&dr->dr_dirty_node));
1251 list_insert_tail(&di->dt.di.dr_children, dr);
1252 mutex_exit(&di->dt.di.dr_mtx);
1255 mutex_exit(&db->db_mtx);
1257 ASSERT(db->db_level+1 == dn->dn_nlevels);
1258 ASSERT(db->db_blkid < dn->dn_nblkptr);
1259 ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf);
1260 mutex_enter(&dn->dn_mtx);
1261 ASSERT(!list_link_active(&dr->dr_dirty_node));
1262 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1263 mutex_exit(&dn->dn_mtx);
1264 if (drop_struct_lock)
1265 rw_exit(&dn->dn_struct_rwlock);
1268 dnode_setdirty(dn, tx);
1274 dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1277 uint64_t txg = tx->tx_txg;
1278 dbuf_dirty_record_t *dr, **drp;
1281 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1283 mutex_enter(&db->db_mtx);
1285 * If this buffer is not dirty, we're done.
1287 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1288 if (dr->dr_txg <= txg)
1290 if (dr == NULL || dr->dr_txg < txg) {
1291 mutex_exit(&db->db_mtx);
1294 ASSERT(dr->dr_txg == txg);
1295 ASSERT(dr->dr_dbuf == db);
1301 * If this buffer is currently held, we cannot undirty
1302 * it, since one of the current holders may be in the
1303 * middle of an update. Note that users of dbuf_undirty()
1304 * should not place a hold on the dbuf before the call.
1305 * Also note: we can get here with a spill block, so
1306 * test for that similar to how dbuf_dirty does.
1308 if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
1309 mutex_exit(&db->db_mtx);
1310 /* Make sure we don't toss this buffer at sync phase */
1311 if (db->db_blkid != DMU_SPILL_BLKID) {
1312 mutex_enter(&dn->dn_mtx);
1313 dnode_clear_range(dn, db->db_blkid, 1, tx);
1314 mutex_exit(&dn->dn_mtx);
1320 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1322 ASSERT(db->db.db_size != 0);
1324 /* XXX would be nice to fix up dn_towrite_space[] */
1329 * Note that there are three places in dbuf_dirty()
1330 * where this dirty record may be put on a list.
1331 * Make sure to do a list_remove corresponding to
1332 * every one of those list_insert calls.
1334 if (dr->dr_parent) {
1335 mutex_enter(&dr->dr_parent->dt.di.dr_mtx);
1336 list_remove(&dr->dr_parent->dt.di.dr_children, dr);
1337 mutex_exit(&dr->dr_parent->dt.di.dr_mtx);
1338 } else if (db->db_blkid == DMU_SPILL_BLKID ||
1339 db->db_level+1 == dn->dn_nlevels) {
1340 ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf);
1341 mutex_enter(&dn->dn_mtx);
1342 list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr);
1343 mutex_exit(&dn->dn_mtx);
1347 if (db->db_level == 0) {
1348 if (db->db_state != DB_NOFILL) {
1349 dbuf_unoverride(dr);
1351 ASSERT(db->db_buf != NULL);
1352 ASSERT(dr->dt.dl.dr_data != NULL);
1353 if (dr->dt.dl.dr_data != db->db_buf)
1354 VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data,
1358 ASSERT(db->db_buf != NULL);
1359 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
1360 mutex_destroy(&dr->dt.di.dr_mtx);
1361 list_destroy(&dr->dt.di.dr_children);
1363 kmem_free(dr, sizeof (dbuf_dirty_record_t));
1365 ASSERT(db->db_dirtycnt > 0);
1366 db->db_dirtycnt -= 1;
1368 if (refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) {
1369 arc_buf_t *buf = db->db_buf;
1371 ASSERT(db->db_state == DB_NOFILL || arc_released(buf));
1372 dbuf_set_data(db, NULL);
1373 VERIFY(arc_buf_remove_ref(buf, db) == 1);
1378 mutex_exit(&db->db_mtx);
1382 #pragma weak dmu_buf_will_dirty = dbuf_will_dirty
1384 dbuf_will_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1386 int rf = DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH;
1388 ASSERT(tx->tx_txg != 0);
1389 ASSERT(!refcount_is_zero(&db->db_holds));
1392 if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock))
1393 rf |= DB_RF_HAVESTRUCT;
1395 (void) dbuf_read(db, NULL, rf);
1396 (void) dbuf_dirty(db, tx);
1400 dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1402 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1404 db->db_state = DB_NOFILL;
1406 dmu_buf_will_fill(db_fake, tx);
1410 dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1412 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1414 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1415 ASSERT(tx->tx_txg != 0);
1416 ASSERT(db->db_level == 0);
1417 ASSERT(!refcount_is_zero(&db->db_holds));
1419 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
1420 dmu_tx_private_ok(tx));
1423 (void) dbuf_dirty(db, tx);
1426 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1429 dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx)
1431 mutex_enter(&db->db_mtx);
1434 if (db->db_state == DB_FILL) {
1435 if (db->db_level == 0 && db->db_freed_in_flight) {
1436 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1437 /* we were freed while filling */
1438 /* XXX dbuf_undirty? */
1439 bzero(db->db.db_data, db->db.db_size);
1440 db->db_freed_in_flight = FALSE;
1442 db->db_state = DB_CACHED;
1443 cv_broadcast(&db->db_changed);
1445 mutex_exit(&db->db_mtx);
1449 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1450 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1453 dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx)
1455 ASSERT(!refcount_is_zero(&db->db_holds));
1456 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1457 ASSERT(db->db_level == 0);
1458 ASSERT(DBUF_GET_BUFC_TYPE(db) == ARC_BUFC_DATA);
1459 ASSERT(buf != NULL);
1460 ASSERT(arc_buf_size(buf) == db->db.db_size);
1461 ASSERT(tx->tx_txg != 0);
1463 arc_return_buf(buf, db);
1464 ASSERT(arc_released(buf));
1466 mutex_enter(&db->db_mtx);
1468 while (db->db_state == DB_READ || db->db_state == DB_FILL)
1469 cv_wait(&db->db_changed, &db->db_mtx);
1471 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED);
1473 if (db->db_state == DB_CACHED &&
1474 refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) {
1475 mutex_exit(&db->db_mtx);
1476 (void) dbuf_dirty(db, tx);
1477 bcopy(buf->b_data, db->db.db_data, db->db.db_size);
1478 VERIFY(arc_buf_remove_ref(buf, db) == 1);
1479 xuio_stat_wbuf_copied();
1483 xuio_stat_wbuf_nocopy();
1484 if (db->db_state == DB_CACHED) {
1485 dbuf_dirty_record_t *dr = db->db_last_dirty;
1487 ASSERT(db->db_buf != NULL);
1488 if (dr != NULL && dr->dr_txg == tx->tx_txg) {
1489 ASSERT(dr->dt.dl.dr_data == db->db_buf);
1490 if (!arc_released(db->db_buf)) {
1491 ASSERT(dr->dt.dl.dr_override_state ==
1493 arc_release(db->db_buf, db);
1495 dr->dt.dl.dr_data = buf;
1496 VERIFY(arc_buf_remove_ref(db->db_buf, db) == 1);
1497 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) {
1498 arc_release(db->db_buf, db);
1499 VERIFY(arc_buf_remove_ref(db->db_buf, db) == 1);
1503 ASSERT(db->db_buf == NULL);
1504 dbuf_set_data(db, buf);
1505 db->db_state = DB_FILL;
1506 mutex_exit(&db->db_mtx);
1507 (void) dbuf_dirty(db, tx);
1508 dbuf_fill_done(db, tx);
1512 * "Clear" the contents of this dbuf. This will mark the dbuf
1513 * EVICTING and clear *most* of its references. Unfortunetely,
1514 * when we are not holding the dn_dbufs_mtx, we can't clear the
1515 * entry in the dn_dbufs list. We have to wait until dbuf_destroy()
1516 * in this case. For callers from the DMU we will usually see:
1517 * dbuf_clear()->arc_buf_evict()->dbuf_do_evict()->dbuf_destroy()
1518 * For the arc callback, we will usually see:
1519 * dbuf_do_evict()->dbuf_clear();dbuf_destroy()
1520 * Sometimes, though, we will get a mix of these two:
1521 * DMU: dbuf_clear()->arc_buf_evict()
1522 * ARC: dbuf_do_evict()->dbuf_destroy()
1525 dbuf_clear(dmu_buf_impl_t *db)
1528 dmu_buf_impl_t *parent = db->db_parent;
1529 dmu_buf_impl_t *dndb;
1530 int dbuf_gone = FALSE;
1532 ASSERT(MUTEX_HELD(&db->db_mtx));
1533 ASSERT(refcount_is_zero(&db->db_holds));
1535 dbuf_evict_user(db);
1537 if (db->db_state == DB_CACHED) {
1538 ASSERT(db->db.db_data != NULL);
1539 if (db->db_blkid == DMU_BONUS_BLKID) {
1540 zio_buf_free(db->db.db_data, DN_MAX_BONUSLEN);
1541 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
1543 db->db.db_data = NULL;
1544 db->db_state = DB_UNCACHED;
1547 ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL);
1548 ASSERT(db->db_data_pending == NULL);
1550 db->db_state = DB_EVICTING;
1551 db->db_blkptr = NULL;
1556 if (db->db_blkid != DMU_BONUS_BLKID && MUTEX_HELD(&dn->dn_dbufs_mtx)) {
1557 list_remove(&dn->dn_dbufs, db);
1558 (void) atomic_dec_32_nv(&dn->dn_dbufs_count);
1562 * Decrementing the dbuf count means that the hold corresponding
1563 * to the removed dbuf is no longer discounted in dnode_move(),
1564 * so the dnode cannot be moved until after we release the hold.
1565 * The membar_producer() ensures visibility of the decremented
1566 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
1570 db->db_dnode_handle = NULL;
1576 dbuf_gone = arc_buf_evict(db->db_buf);
1579 mutex_exit(&db->db_mtx);
1582 * If this dbuf is referenced from an indirect dbuf,
1583 * decrement the ref count on the indirect dbuf.
1585 if (parent && parent != dndb)
1586 dbuf_rele(parent, db);
1590 dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse,
1591 dmu_buf_impl_t **parentp, blkptr_t **bpp)
1598 ASSERT(blkid != DMU_BONUS_BLKID);
1600 if (blkid == DMU_SPILL_BLKID) {
1601 mutex_enter(&dn->dn_mtx);
1602 if (dn->dn_have_spill &&
1603 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
1604 *bpp = &dn->dn_phys->dn_spill;
1607 dbuf_add_ref(dn->dn_dbuf, NULL);
1608 *parentp = dn->dn_dbuf;
1609 mutex_exit(&dn->dn_mtx);
1613 if (dn->dn_phys->dn_nlevels == 0)
1616 nlevels = dn->dn_phys->dn_nlevels;
1618 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1620 ASSERT3U(level * epbs, <, 64);
1621 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1622 if (level >= nlevels ||
1623 (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) {
1624 /* the buffer has no parent yet */
1626 } else if (level < nlevels-1) {
1627 /* this block is referenced from an indirect block */
1628 int err = dbuf_hold_impl(dn, level+1,
1629 blkid >> epbs, fail_sparse, NULL, parentp);
1632 err = dbuf_read(*parentp, NULL,
1633 (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL));
1635 dbuf_rele(*parentp, NULL);
1639 *bpp = ((blkptr_t *)(*parentp)->db.db_data) +
1640 (blkid & ((1ULL << epbs) - 1));
1643 /* the block is referenced from the dnode */
1644 ASSERT3U(level, ==, nlevels-1);
1645 ASSERT(dn->dn_phys->dn_nblkptr == 0 ||
1646 blkid < dn->dn_phys->dn_nblkptr);
1648 dbuf_add_ref(dn->dn_dbuf, NULL);
1649 *parentp = dn->dn_dbuf;
1651 *bpp = &dn->dn_phys->dn_blkptr[blkid];
1656 static dmu_buf_impl_t *
1657 dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid,
1658 dmu_buf_impl_t *parent, blkptr_t *blkptr)
1660 objset_t *os = dn->dn_objset;
1661 dmu_buf_impl_t *db, *odb;
1663 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1664 ASSERT(dn->dn_type != DMU_OT_NONE);
1666 db = kmem_cache_alloc(dbuf_cache, KM_SLEEP);
1669 db->db.db_object = dn->dn_object;
1670 db->db_level = level;
1671 db->db_blkid = blkid;
1672 db->db_last_dirty = NULL;
1673 db->db_dirtycnt = 0;
1674 db->db_dnode_handle = dn->dn_handle;
1675 db->db_parent = parent;
1676 db->db_blkptr = blkptr;
1678 db->db_user_ptr = NULL;
1679 db->db_user_data_ptr_ptr = NULL;
1680 db->db_evict_func = NULL;
1681 db->db_immediate_evict = 0;
1682 db->db_freed_in_flight = 0;
1684 if (blkid == DMU_BONUS_BLKID) {
1685 ASSERT3P(parent, ==, dn->dn_dbuf);
1686 db->db.db_size = DN_MAX_BONUSLEN -
1687 (dn->dn_nblkptr-1) * sizeof (blkptr_t);
1688 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
1689 db->db.db_offset = DMU_BONUS_BLKID;
1690 db->db_state = DB_UNCACHED;
1691 /* the bonus dbuf is not placed in the hash table */
1692 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1694 } else if (blkid == DMU_SPILL_BLKID) {
1695 db->db.db_size = (blkptr != NULL) ?
1696 BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE;
1697 db->db.db_offset = 0;
1700 db->db_level ? 1<<dn->dn_indblkshift : dn->dn_datablksz;
1701 db->db.db_size = blocksize;
1702 db->db.db_offset = db->db_blkid * blocksize;
1706 * Hold the dn_dbufs_mtx while we get the new dbuf
1707 * in the hash table *and* added to the dbufs list.
1708 * This prevents a possible deadlock with someone
1709 * trying to look up this dbuf before its added to the
1712 mutex_enter(&dn->dn_dbufs_mtx);
1713 db->db_state = DB_EVICTING;
1714 if ((odb = dbuf_hash_insert(db)) != NULL) {
1715 /* someone else inserted it first */
1716 kmem_cache_free(dbuf_cache, db);
1717 mutex_exit(&dn->dn_dbufs_mtx);
1720 list_insert_head(&dn->dn_dbufs, db);
1721 db->db_state = DB_UNCACHED;
1722 mutex_exit(&dn->dn_dbufs_mtx);
1723 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1725 if (parent && parent != dn->dn_dbuf)
1726 dbuf_add_ref(parent, db);
1728 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1729 refcount_count(&dn->dn_holds) > 0);
1730 (void) refcount_add(&dn->dn_holds, db);
1731 (void) atomic_inc_32_nv(&dn->dn_dbufs_count);
1733 dprintf_dbuf(db, "db=%p\n", db);
1739 dbuf_do_evict(void *private)
1741 arc_buf_t *buf = private;
1742 dmu_buf_impl_t *db = buf->b_private;
1744 if (!MUTEX_HELD(&db->db_mtx))
1745 mutex_enter(&db->db_mtx);
1747 ASSERT(refcount_is_zero(&db->db_holds));
1749 if (db->db_state != DB_EVICTING) {
1750 ASSERT(db->db_state == DB_CACHED);
1755 mutex_exit(&db->db_mtx);
1762 dbuf_destroy(dmu_buf_impl_t *db)
1764 ASSERT(refcount_is_zero(&db->db_holds));
1766 if (db->db_blkid != DMU_BONUS_BLKID) {
1768 * If this dbuf is still on the dn_dbufs list,
1769 * remove it from that list.
1771 if (db->db_dnode_handle != NULL) {
1776 mutex_enter(&dn->dn_dbufs_mtx);
1777 list_remove(&dn->dn_dbufs, db);
1778 (void) atomic_dec_32_nv(&dn->dn_dbufs_count);
1779 mutex_exit(&dn->dn_dbufs_mtx);
1782 * Decrementing the dbuf count means that the hold
1783 * corresponding to the removed dbuf is no longer
1784 * discounted in dnode_move(), so the dnode cannot be
1785 * moved until after we release the hold.
1788 db->db_dnode_handle = NULL;
1790 dbuf_hash_remove(db);
1792 db->db_parent = NULL;
1795 ASSERT(!list_link_active(&db->db_link));
1796 ASSERT(db->db.db_data == NULL);
1797 ASSERT(db->db_hash_next == NULL);
1798 ASSERT(db->db_blkptr == NULL);
1799 ASSERT(db->db_data_pending == NULL);
1801 kmem_cache_free(dbuf_cache, db);
1802 arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1806 dbuf_prefetch(dnode_t *dn, uint64_t blkid)
1808 dmu_buf_impl_t *db = NULL;
1809 blkptr_t *bp = NULL;
1811 ASSERT(blkid != DMU_BONUS_BLKID);
1812 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1814 if (dnode_block_freed(dn, blkid))
1817 /* dbuf_find() returns with db_mtx held */
1818 if (db = dbuf_find(dn, 0, blkid)) {
1820 * This dbuf is already in the cache. We assume that
1821 * it is already CACHED, or else about to be either
1824 mutex_exit(&db->db_mtx);
1828 if (dbuf_findbp(dn, 0, blkid, TRUE, &db, &bp) == 0) {
1829 if (bp && !BP_IS_HOLE(bp)) {
1830 int priority = dn->dn_type == DMU_OT_DDT_ZAP ?
1831 ZIO_PRIORITY_DDT_PREFETCH : ZIO_PRIORITY_ASYNC_READ;
1833 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
1834 uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
1837 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
1838 dn->dn_object, 0, blkid);
1843 pbuf = dn->dn_objset->os_phys_buf;
1845 (void) dsl_read(NULL, dn->dn_objset->os_spa,
1846 bp, pbuf, NULL, NULL, priority,
1847 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
1851 dbuf_rele(db, NULL);
1856 * Returns with db_holds incremented, and db_mtx not held.
1857 * Note: dn_struct_rwlock must be held.
1860 dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid, int fail_sparse,
1861 void *tag, dmu_buf_impl_t **dbp)
1863 dmu_buf_impl_t *db, *parent = NULL;
1865 ASSERT(blkid != DMU_BONUS_BLKID);
1866 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1867 ASSERT3U(dn->dn_nlevels, >, level);
1871 /* dbuf_find() returns with db_mtx held */
1872 db = dbuf_find(dn, level, blkid);
1875 blkptr_t *bp = NULL;
1878 ASSERT3P(parent, ==, NULL);
1879 err = dbuf_findbp(dn, level, blkid, fail_sparse, &parent, &bp);
1881 if (err == 0 && bp && BP_IS_HOLE(bp))
1885 dbuf_rele(parent, NULL);
1889 if (err && err != ENOENT)
1891 db = dbuf_create(dn, level, blkid, parent, bp);
1894 if (db->db_buf && refcount_is_zero(&db->db_holds)) {
1895 arc_buf_add_ref(db->db_buf, db);
1896 if (db->db_buf->b_data == NULL) {
1899 dbuf_rele(parent, NULL);
1904 ASSERT3P(db->db.db_data, ==, db->db_buf->b_data);
1907 ASSERT(db->db_buf == NULL || arc_referenced(db->db_buf));
1910 * If this buffer is currently syncing out, and we are are
1911 * still referencing it from db_data, we need to make a copy
1912 * of it in case we decide we want to dirty it again in this txg.
1914 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1915 dn->dn_object != DMU_META_DNODE_OBJECT &&
1916 db->db_state == DB_CACHED && db->db_data_pending) {
1917 dbuf_dirty_record_t *dr = db->db_data_pending;
1919 if (dr->dt.dl.dr_data == db->db_buf) {
1920 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1923 arc_buf_alloc(dn->dn_objset->os_spa,
1924 db->db.db_size, db, type));
1925 bcopy(dr->dt.dl.dr_data->b_data, db->db.db_data,
1930 (void) refcount_add(&db->db_holds, tag);
1931 dbuf_update_data(db);
1933 mutex_exit(&db->db_mtx);
1935 /* NOTE: we can't rele the parent until after we drop the db_mtx */
1937 dbuf_rele(parent, NULL);
1939 ASSERT3P(DB_DNODE(db), ==, dn);
1940 ASSERT3U(db->db_blkid, ==, blkid);
1941 ASSERT3U(db->db_level, ==, level);
1948 dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag)
1951 int err = dbuf_hold_impl(dn, 0, blkid, FALSE, tag, &db);
1952 return (err ? NULL : db);
1956 dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag)
1959 int err = dbuf_hold_impl(dn, level, blkid, FALSE, tag, &db);
1960 return (err ? NULL : db);
1964 dbuf_create_bonus(dnode_t *dn)
1966 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
1968 ASSERT(dn->dn_bonus == NULL);
1969 dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL);
1973 dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx)
1975 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1978 if (db->db_blkid != DMU_SPILL_BLKID)
1981 blksz = SPA_MINBLOCKSIZE;
1982 if (blksz > SPA_MAXBLOCKSIZE)
1983 blksz = SPA_MAXBLOCKSIZE;
1985 blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE);
1989 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1990 dbuf_new_size(db, blksz, tx);
1991 rw_exit(&dn->dn_struct_rwlock);
1998 dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx)
2000 dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx);
2003 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2005 dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
2007 int64_t holds = refcount_add(&db->db_holds, tag);
2012 * If you call dbuf_rele() you had better not be referencing the dnode handle
2013 * unless you have some other direct or indirect hold on the dnode. (An indirect
2014 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2015 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2016 * dnode's parent dbuf evicting its dnode handles.
2018 #pragma weak dmu_buf_rele = dbuf_rele
2020 dbuf_rele(dmu_buf_impl_t *db, void *tag)
2022 mutex_enter(&db->db_mtx);
2023 dbuf_rele_and_unlock(db, tag);
2027 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2028 * db_dirtycnt and db_holds to be updated atomically.
2031 dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag)
2035 ASSERT(MUTEX_HELD(&db->db_mtx));
2039 * Remove the reference to the dbuf before removing its hold on the
2040 * dnode so we can guarantee in dnode_move() that a referenced bonus
2041 * buffer has a corresponding dnode hold.
2043 holds = refcount_remove(&db->db_holds, tag);
2047 * We can't freeze indirects if there is a possibility that they
2048 * may be modified in the current syncing context.
2050 if (db->db_buf && holds == (db->db_level == 0 ? db->db_dirtycnt : 0))
2051 arc_buf_freeze(db->db_buf);
2053 if (holds == db->db_dirtycnt &&
2054 db->db_level == 0 && db->db_immediate_evict)
2055 dbuf_evict_user(db);
2058 if (db->db_blkid == DMU_BONUS_BLKID) {
2059 mutex_exit(&db->db_mtx);
2062 * If the dnode moves here, we cannot cross this barrier
2063 * until the move completes.
2066 (void) atomic_dec_32_nv(&DB_DNODE(db)->dn_dbufs_count);
2069 * The bonus buffer's dnode hold is no longer discounted
2070 * in dnode_move(). The dnode cannot move until after
2073 dnode_rele(DB_DNODE(db), db);
2074 } else if (db->db_buf == NULL) {
2076 * This is a special case: we never associated this
2077 * dbuf with any data allocated from the ARC.
2079 ASSERT(db->db_state == DB_UNCACHED ||
2080 db->db_state == DB_NOFILL);
2082 } else if (arc_released(db->db_buf)) {
2083 arc_buf_t *buf = db->db_buf;
2085 * This dbuf has anonymous data associated with it.
2087 dbuf_set_data(db, NULL);
2088 VERIFY(arc_buf_remove_ref(buf, db) == 1);
2091 VERIFY(arc_buf_remove_ref(db->db_buf, db) == 0);
2092 if (!DBUF_IS_CACHEABLE(db))
2095 mutex_exit(&db->db_mtx);
2098 mutex_exit(&db->db_mtx);
2102 #pragma weak dmu_buf_refcount = dbuf_refcount
2104 dbuf_refcount(dmu_buf_impl_t *db)
2106 return (refcount_count(&db->db_holds));
2110 dmu_buf_set_user(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr,
2111 dmu_buf_evict_func_t *evict_func)
2113 return (dmu_buf_update_user(db_fake, NULL, user_ptr,
2114 user_data_ptr_ptr, evict_func));
2118 dmu_buf_set_user_ie(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr,
2119 dmu_buf_evict_func_t *evict_func)
2121 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2123 db->db_immediate_evict = TRUE;
2124 return (dmu_buf_update_user(db_fake, NULL, user_ptr,
2125 user_data_ptr_ptr, evict_func));
2129 dmu_buf_update_user(dmu_buf_t *db_fake, void *old_user_ptr, void *user_ptr,
2130 void *user_data_ptr_ptr, dmu_buf_evict_func_t *evict_func)
2132 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2133 ASSERT(db->db_level == 0);
2135 ASSERT((user_ptr == NULL) == (evict_func == NULL));
2137 mutex_enter(&db->db_mtx);
2139 if (db->db_user_ptr == old_user_ptr) {
2140 db->db_user_ptr = user_ptr;
2141 db->db_user_data_ptr_ptr = user_data_ptr_ptr;
2142 db->db_evict_func = evict_func;
2144 dbuf_update_data(db);
2146 old_user_ptr = db->db_user_ptr;
2149 mutex_exit(&db->db_mtx);
2150 return (old_user_ptr);
2154 dmu_buf_get_user(dmu_buf_t *db_fake)
2156 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2157 ASSERT(!refcount_is_zero(&db->db_holds));
2159 return (db->db_user_ptr);
2163 dmu_buf_freeable(dmu_buf_t *dbuf)
2165 boolean_t res = B_FALSE;
2166 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
2169 res = dsl_dataset_block_freeable(db->db_objset->os_dsl_dataset,
2170 db->db_blkptr, db->db_blkptr->blk_birth);
2176 dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db)
2178 /* ASSERT(dmu_tx_is_syncing(tx) */
2179 ASSERT(MUTEX_HELD(&db->db_mtx));
2181 if (db->db_blkptr != NULL)
2184 if (db->db_blkid == DMU_SPILL_BLKID) {
2185 db->db_blkptr = &dn->dn_phys->dn_spill;
2186 BP_ZERO(db->db_blkptr);
2189 if (db->db_level == dn->dn_phys->dn_nlevels-1) {
2191 * This buffer was allocated at a time when there was
2192 * no available blkptrs from the dnode, or it was
2193 * inappropriate to hook it in (i.e., nlevels mis-match).
2195 ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr);
2196 ASSERT(db->db_parent == NULL);
2197 db->db_parent = dn->dn_dbuf;
2198 db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
2201 dmu_buf_impl_t *parent = db->db_parent;
2202 int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2204 ASSERT(dn->dn_phys->dn_nlevels > 1);
2205 if (parent == NULL) {
2206 mutex_exit(&db->db_mtx);
2207 rw_enter(&dn->dn_struct_rwlock, RW_READER);
2208 (void) dbuf_hold_impl(dn, db->db_level+1,
2209 db->db_blkid >> epbs, FALSE, db, &parent);
2210 rw_exit(&dn->dn_struct_rwlock);
2211 mutex_enter(&db->db_mtx);
2212 db->db_parent = parent;
2214 db->db_blkptr = (blkptr_t *)parent->db.db_data +
2215 (db->db_blkid & ((1ULL << epbs) - 1));
2221 dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2223 dmu_buf_impl_t *db = dr->dr_dbuf;
2227 ASSERT(dmu_tx_is_syncing(tx));
2229 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2231 mutex_enter(&db->db_mtx);
2233 ASSERT(db->db_level > 0);
2236 if (db->db_buf == NULL) {
2237 mutex_exit(&db->db_mtx);
2238 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
2239 mutex_enter(&db->db_mtx);
2241 ASSERT3U(db->db_state, ==, DB_CACHED);
2242 ASSERT(db->db_buf != NULL);
2246 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2247 dbuf_check_blkptr(dn, db);
2250 db->db_data_pending = dr;
2252 mutex_exit(&db->db_mtx);
2253 dbuf_write(dr, db->db_buf, tx);
2256 mutex_enter(&dr->dt.di.dr_mtx);
2257 dbuf_sync_list(&dr->dt.di.dr_children, tx);
2258 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
2259 mutex_exit(&dr->dt.di.dr_mtx);
2264 dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2266 arc_buf_t **datap = &dr->dt.dl.dr_data;
2267 dmu_buf_impl_t *db = dr->dr_dbuf;
2270 uint64_t txg = tx->tx_txg;
2272 ASSERT(dmu_tx_is_syncing(tx));
2274 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2276 mutex_enter(&db->db_mtx);
2278 * To be synced, we must be dirtied. But we
2279 * might have been freed after the dirty.
2281 if (db->db_state == DB_UNCACHED) {
2282 /* This buffer has been freed since it was dirtied */
2283 ASSERT(db->db.db_data == NULL);
2284 } else if (db->db_state == DB_FILL) {
2285 /* This buffer was freed and is now being re-filled */
2286 ASSERT(db->db.db_data != dr->dt.dl.dr_data);
2288 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL);
2295 if (db->db_blkid == DMU_SPILL_BLKID) {
2296 mutex_enter(&dn->dn_mtx);
2297 dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR;
2298 mutex_exit(&dn->dn_mtx);
2302 * If this is a bonus buffer, simply copy the bonus data into the
2303 * dnode. It will be written out when the dnode is synced (and it
2304 * will be synced, since it must have been dirty for dbuf_sync to
2307 if (db->db_blkid == DMU_BONUS_BLKID) {
2308 dbuf_dirty_record_t **drp;
2310 ASSERT(*datap != NULL);
2311 ASSERT0(db->db_level);
2312 ASSERT3U(dn->dn_phys->dn_bonuslen, <=, DN_MAX_BONUSLEN);
2313 bcopy(*datap, DN_BONUS(dn->dn_phys), dn->dn_phys->dn_bonuslen);
2316 if (*datap != db->db.db_data) {
2317 zio_buf_free(*datap, DN_MAX_BONUSLEN);
2318 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
2320 db->db_data_pending = NULL;
2321 drp = &db->db_last_dirty;
2323 drp = &(*drp)->dr_next;
2324 ASSERT(dr->dr_next == NULL);
2325 ASSERT(dr->dr_dbuf == db);
2327 if (dr->dr_dbuf->db_level != 0) {
2328 list_destroy(&dr->dt.di.dr_children);
2329 mutex_destroy(&dr->dt.di.dr_mtx);
2331 kmem_free(dr, sizeof (dbuf_dirty_record_t));
2332 ASSERT(db->db_dirtycnt > 0);
2333 db->db_dirtycnt -= 1;
2334 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
2341 * This function may have dropped the db_mtx lock allowing a dmu_sync
2342 * operation to sneak in. As a result, we need to ensure that we
2343 * don't check the dr_override_state until we have returned from
2344 * dbuf_check_blkptr.
2346 dbuf_check_blkptr(dn, db);
2349 * If this buffer is in the middle of an immediate write,
2350 * wait for the synchronous IO to complete.
2352 while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
2353 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
2354 cv_wait(&db->db_changed, &db->db_mtx);
2355 ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN);
2358 if (db->db_state != DB_NOFILL &&
2359 dn->dn_object != DMU_META_DNODE_OBJECT &&
2360 refcount_count(&db->db_holds) > 1 &&
2361 dr->dt.dl.dr_override_state != DR_OVERRIDDEN &&
2362 *datap == db->db_buf) {
2364 * If this buffer is currently "in use" (i.e., there
2365 * are active holds and db_data still references it),
2366 * then make a copy before we start the write so that
2367 * any modifications from the open txg will not leak
2370 * NOTE: this copy does not need to be made for
2371 * objects only modified in the syncing context (e.g.
2372 * DNONE_DNODE blocks).
2374 int blksz = arc_buf_size(*datap);
2375 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
2376 *datap = arc_buf_alloc(os->os_spa, blksz, db, type);
2377 bcopy(db->db.db_data, (*datap)->b_data, blksz);
2379 db->db_data_pending = dr;
2381 mutex_exit(&db->db_mtx);
2383 dbuf_write(dr, *datap, tx);
2385 ASSERT(!list_link_active(&dr->dr_dirty_node));
2386 if (dn->dn_object == DMU_META_DNODE_OBJECT) {
2387 list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr);
2391 * Although zio_nowait() does not "wait for an IO", it does
2392 * initiate the IO. If this is an empty write it seems plausible
2393 * that the IO could actually be completed before the nowait
2394 * returns. We need to DB_DNODE_EXIT() first in case
2395 * zio_nowait() invalidates the dbuf.
2398 zio_nowait(dr->dr_zio);
2403 dbuf_sync_list(list_t *list, dmu_tx_t *tx)
2405 dbuf_dirty_record_t *dr;
2407 while (dr = list_head(list)) {
2408 if (dr->dr_zio != NULL) {
2410 * If we find an already initialized zio then we
2411 * are processing the meta-dnode, and we have finished.
2412 * The dbufs for all dnodes are put back on the list
2413 * during processing, so that we can zio_wait()
2414 * these IOs after initiating all child IOs.
2416 ASSERT3U(dr->dr_dbuf->db.db_object, ==,
2417 DMU_META_DNODE_OBJECT);
2420 list_remove(list, dr);
2421 if (dr->dr_dbuf->db_level > 0)
2422 dbuf_sync_indirect(dr, tx);
2424 dbuf_sync_leaf(dr, tx);
2430 dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
2432 dmu_buf_impl_t *db = vdb;
2434 blkptr_t *bp = zio->io_bp;
2435 blkptr_t *bp_orig = &zio->io_bp_orig;
2436 spa_t *spa = zio->io_spa;
2441 ASSERT(db->db_blkptr == bp);
2445 delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig);
2446 dnode_diduse_space(dn, delta - zio->io_prev_space_delta);
2447 zio->io_prev_space_delta = delta;
2449 if (BP_IS_HOLE(bp)) {
2450 ASSERT(bp->blk_fill == 0);
2455 ASSERT((db->db_blkid != DMU_SPILL_BLKID &&
2456 BP_GET_TYPE(bp) == dn->dn_type) ||
2457 (db->db_blkid == DMU_SPILL_BLKID &&
2458 BP_GET_TYPE(bp) == dn->dn_bonustype));
2459 ASSERT(BP_GET_LEVEL(bp) == db->db_level);
2461 mutex_enter(&db->db_mtx);
2464 if (db->db_blkid == DMU_SPILL_BLKID) {
2465 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
2466 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
2467 db->db_blkptr == &dn->dn_phys->dn_spill);
2471 if (db->db_level == 0) {
2472 mutex_enter(&dn->dn_mtx);
2473 if (db->db_blkid > dn->dn_phys->dn_maxblkid &&
2474 db->db_blkid != DMU_SPILL_BLKID)
2475 dn->dn_phys->dn_maxblkid = db->db_blkid;
2476 mutex_exit(&dn->dn_mtx);
2478 if (dn->dn_type == DMU_OT_DNODE) {
2479 dnode_phys_t *dnp = db->db.db_data;
2480 for (i = db->db.db_size >> DNODE_SHIFT; i > 0;
2482 if (dnp->dn_type != DMU_OT_NONE)
2489 blkptr_t *ibp = db->db.db_data;
2490 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2491 for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) {
2492 if (BP_IS_HOLE(ibp))
2494 fill += ibp->blk_fill;
2499 bp->blk_fill = fill;
2501 mutex_exit(&db->db_mtx);
2506 dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
2508 dmu_buf_impl_t *db = vdb;
2509 blkptr_t *bp = zio->io_bp;
2510 blkptr_t *bp_orig = &zio->io_bp_orig;
2511 uint64_t txg = zio->io_txg;
2512 dbuf_dirty_record_t **drp, *dr;
2514 ASSERT0(zio->io_error);
2515 ASSERT(db->db_blkptr == bp);
2517 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
2518 ASSERT(BP_EQUAL(bp, bp_orig));
2524 DB_GET_OBJSET(&os, db);
2525 ds = os->os_dsl_dataset;
2528 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
2529 dsl_dataset_block_born(ds, bp, tx);
2532 mutex_enter(&db->db_mtx);
2536 drp = &db->db_last_dirty;
2537 while ((dr = *drp) != db->db_data_pending)
2539 ASSERT(!list_link_active(&dr->dr_dirty_node));
2540 ASSERT(dr->dr_txg == txg);
2541 ASSERT(dr->dr_dbuf == db);
2542 ASSERT(dr->dr_next == NULL);
2546 if (db->db_blkid == DMU_SPILL_BLKID) {
2551 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
2552 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
2553 db->db_blkptr == &dn->dn_phys->dn_spill);
2558 if (db->db_level == 0) {
2559 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2560 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
2561 if (db->db_state != DB_NOFILL) {
2562 if (dr->dt.dl.dr_data != db->db_buf)
2563 VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data,
2565 else if (!arc_released(db->db_buf))
2566 arc_set_callback(db->db_buf, dbuf_do_evict, db);
2573 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
2574 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2575 if (!BP_IS_HOLE(db->db_blkptr)) {
2577 dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2578 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
2580 ASSERT3U(dn->dn_phys->dn_maxblkid
2581 >> (db->db_level * epbs), >=, db->db_blkid);
2582 arc_set_callback(db->db_buf, dbuf_do_evict, db);
2585 mutex_destroy(&dr->dt.di.dr_mtx);
2586 list_destroy(&dr->dt.di.dr_children);
2588 kmem_free(dr, sizeof (dbuf_dirty_record_t));
2590 cv_broadcast(&db->db_changed);
2591 ASSERT(db->db_dirtycnt > 0);
2592 db->db_dirtycnt -= 1;
2593 db->db_data_pending = NULL;
2594 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
2598 dbuf_write_nofill_ready(zio_t *zio)
2600 dbuf_write_ready(zio, NULL, zio->io_private);
2604 dbuf_write_nofill_done(zio_t *zio)
2606 dbuf_write_done(zio, NULL, zio->io_private);
2610 dbuf_write_override_ready(zio_t *zio)
2612 dbuf_dirty_record_t *dr = zio->io_private;
2613 dmu_buf_impl_t *db = dr->dr_dbuf;
2615 dbuf_write_ready(zio, NULL, db);
2619 dbuf_write_override_done(zio_t *zio)
2621 dbuf_dirty_record_t *dr = zio->io_private;
2622 dmu_buf_impl_t *db = dr->dr_dbuf;
2623 blkptr_t *obp = &dr->dt.dl.dr_overridden_by;
2625 mutex_enter(&db->db_mtx);
2626 if (!BP_EQUAL(zio->io_bp, obp)) {
2627 if (!BP_IS_HOLE(obp))
2628 dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp);
2629 arc_release(dr->dt.dl.dr_data, db);
2631 mutex_exit(&db->db_mtx);
2633 dbuf_write_done(zio, NULL, db);
2637 dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx)
2639 dmu_buf_impl_t *db = dr->dr_dbuf;
2642 dmu_buf_impl_t *parent = db->db_parent;
2643 uint64_t txg = tx->tx_txg;
2653 if (db->db_state != DB_NOFILL) {
2654 if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) {
2656 * Private object buffers are released here rather
2657 * than in dbuf_dirty() since they are only modified
2658 * in the syncing context and we don't want the
2659 * overhead of making multiple copies of the data.
2661 if (BP_IS_HOLE(db->db_blkptr)) {
2664 dbuf_release_bp(db);
2669 if (parent != dn->dn_dbuf) {
2670 ASSERT(parent && parent->db_data_pending);
2671 ASSERT(db->db_level == parent->db_level-1);
2672 ASSERT(arc_released(parent->db_buf));
2673 zio = parent->db_data_pending->dr_zio;
2675 ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 &&
2676 db->db_blkid != DMU_SPILL_BLKID) ||
2677 (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0));
2678 if (db->db_blkid != DMU_SPILL_BLKID)
2679 ASSERT3P(db->db_blkptr, ==,
2680 &dn->dn_phys->dn_blkptr[db->db_blkid]);
2684 ASSERT(db->db_level == 0 || data == db->db_buf);
2685 ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
2688 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
2689 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
2690 db->db.db_object, db->db_level, db->db_blkid);
2692 if (db->db_blkid == DMU_SPILL_BLKID)
2694 wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
2696 dmu_write_policy(os, dn, db->db_level, wp_flag, &zp);
2699 if (db->db_level == 0 && dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
2700 ASSERT(db->db_state != DB_NOFILL);
2701 dr->dr_zio = zio_write(zio, os->os_spa, txg,
2702 db->db_blkptr, data->b_data, arc_buf_size(data), &zp,
2703 dbuf_write_override_ready, dbuf_write_override_done, dr,
2704 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
2705 mutex_enter(&db->db_mtx);
2706 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
2707 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by,
2708 dr->dt.dl.dr_copies);
2709 mutex_exit(&db->db_mtx);
2710 } else if (db->db_state == DB_NOFILL) {
2711 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF);
2712 dr->dr_zio = zio_write(zio, os->os_spa, txg,
2713 db->db_blkptr, NULL, db->db.db_size, &zp,
2714 dbuf_write_nofill_ready, dbuf_write_nofill_done, db,
2715 ZIO_PRIORITY_ASYNC_WRITE,
2716 ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb);
2718 ASSERT(arc_released(data));
2719 dr->dr_zio = arc_write(zio, os->os_spa, txg,
2720 db->db_blkptr, data, DBUF_IS_L2CACHEABLE(db), &zp,
2721 dbuf_write_ready, dbuf_write_done, db,
2722 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);