4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28 * Copyright (c) 2014 Integros [integros.com]
31 #include <sys/zfs_context.h>
33 #include <sys/dmu_send.h>
34 #include <sys/dmu_impl.h>
36 #include <sys/dmu_objset.h>
37 #include <sys/dsl_dataset.h>
38 #include <sys/dsl_dir.h>
39 #include <sys/dmu_tx.h>
42 #include <sys/dmu_zfetch.h>
44 #include <sys/sa_impl.h>
45 #include <sys/zfeature.h>
46 #include <sys/blkptr.h>
47 #include <sys/range_tree.h>
50 * Number of times that zfs_free_range() took the slow path while doing
51 * a zfs receive. A nonzero value indicates a potential performance problem.
53 uint64_t zfs_free_range_recv_miss;
55 static void dbuf_destroy(dmu_buf_impl_t *db);
56 static boolean_t dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
57 static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx);
60 * Global data structures and functions for the dbuf cache.
62 static kmem_cache_t *dbuf_cache;
63 static taskq_t *dbu_evict_taskq;
67 dbuf_cons(void *vdb, void *unused, int kmflag)
69 dmu_buf_impl_t *db = vdb;
70 bzero(db, sizeof (dmu_buf_impl_t));
72 mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL);
73 cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL);
74 refcount_create(&db->db_holds);
81 dbuf_dest(void *vdb, void *unused)
83 dmu_buf_impl_t *db = vdb;
84 mutex_destroy(&db->db_mtx);
85 cv_destroy(&db->db_changed);
86 refcount_destroy(&db->db_holds);
90 * dbuf hash table routines
92 static dbuf_hash_table_t dbuf_hash_table;
94 static uint64_t dbuf_hash_count;
97 dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid)
99 uintptr_t osv = (uintptr_t)os;
100 uint64_t crc = -1ULL;
102 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
103 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (lvl)) & 0xFF];
104 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF];
105 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF];
106 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF];
107 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 0)) & 0xFF];
108 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 8)) & 0xFF];
110 crc ^= (osv>>14) ^ (obj>>16) ^ (blkid>>16);
115 #define DBUF_HASH(os, obj, level, blkid) dbuf_hash(os, obj, level, blkid);
117 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
118 ((dbuf)->db.db_object == (obj) && \
119 (dbuf)->db_objset == (os) && \
120 (dbuf)->db_level == (level) && \
121 (dbuf)->db_blkid == (blkid))
124 dbuf_find(objset_t *os, uint64_t obj, uint8_t level, uint64_t blkid)
126 dbuf_hash_table_t *h = &dbuf_hash_table;
127 uint64_t hv = DBUF_HASH(os, obj, level, blkid);
128 uint64_t idx = hv & h->hash_table_mask;
131 mutex_enter(DBUF_HASH_MUTEX(h, idx));
132 for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) {
133 if (DBUF_EQUAL(db, os, obj, level, blkid)) {
134 mutex_enter(&db->db_mtx);
135 if (db->db_state != DB_EVICTING) {
136 mutex_exit(DBUF_HASH_MUTEX(h, idx));
139 mutex_exit(&db->db_mtx);
142 mutex_exit(DBUF_HASH_MUTEX(h, idx));
146 static dmu_buf_impl_t *
147 dbuf_find_bonus(objset_t *os, uint64_t object)
150 dmu_buf_impl_t *db = NULL;
152 if (dnode_hold(os, object, FTAG, &dn) == 0) {
153 rw_enter(&dn->dn_struct_rwlock, RW_READER);
154 if (dn->dn_bonus != NULL) {
156 mutex_enter(&db->db_mtx);
158 rw_exit(&dn->dn_struct_rwlock);
159 dnode_rele(dn, FTAG);
165 * Insert an entry into the hash table. If there is already an element
166 * equal to elem in the hash table, then the already existing element
167 * will be returned and the new element will not be inserted.
168 * Otherwise returns NULL.
170 static dmu_buf_impl_t *
171 dbuf_hash_insert(dmu_buf_impl_t *db)
173 dbuf_hash_table_t *h = &dbuf_hash_table;
174 objset_t *os = db->db_objset;
175 uint64_t obj = db->db.db_object;
176 int level = db->db_level;
177 uint64_t blkid = db->db_blkid;
178 uint64_t hv = DBUF_HASH(os, obj, level, blkid);
179 uint64_t idx = hv & h->hash_table_mask;
182 mutex_enter(DBUF_HASH_MUTEX(h, idx));
183 for (dbf = h->hash_table[idx]; dbf != NULL; dbf = dbf->db_hash_next) {
184 if (DBUF_EQUAL(dbf, os, obj, level, blkid)) {
185 mutex_enter(&dbf->db_mtx);
186 if (dbf->db_state != DB_EVICTING) {
187 mutex_exit(DBUF_HASH_MUTEX(h, idx));
190 mutex_exit(&dbf->db_mtx);
194 mutex_enter(&db->db_mtx);
195 db->db_hash_next = h->hash_table[idx];
196 h->hash_table[idx] = db;
197 mutex_exit(DBUF_HASH_MUTEX(h, idx));
198 atomic_inc_64(&dbuf_hash_count);
204 * Remove an entry from the hash table. It must be in the EVICTING state.
207 dbuf_hash_remove(dmu_buf_impl_t *db)
209 dbuf_hash_table_t *h = &dbuf_hash_table;
210 uint64_t hv = DBUF_HASH(db->db_objset, db->db.db_object,
211 db->db_level, db->db_blkid);
212 uint64_t idx = hv & h->hash_table_mask;
213 dmu_buf_impl_t *dbf, **dbp;
216 * We musn't hold db_mtx to maintain lock ordering:
217 * DBUF_HASH_MUTEX > db_mtx.
219 ASSERT(refcount_is_zero(&db->db_holds));
220 ASSERT(db->db_state == DB_EVICTING);
221 ASSERT(!MUTEX_HELD(&db->db_mtx));
223 mutex_enter(DBUF_HASH_MUTEX(h, idx));
224 dbp = &h->hash_table[idx];
225 while ((dbf = *dbp) != db) {
226 dbp = &dbf->db_hash_next;
229 *dbp = db->db_hash_next;
230 db->db_hash_next = NULL;
231 mutex_exit(DBUF_HASH_MUTEX(h, idx));
232 atomic_dec_64(&dbuf_hash_count);
235 static arc_evict_func_t dbuf_do_evict;
240 } dbvu_verify_type_t;
243 dbuf_verify_user(dmu_buf_impl_t *db, dbvu_verify_type_t verify_type)
248 if (db->db_user == NULL)
251 /* Only data blocks support the attachment of user data. */
252 ASSERT(db->db_level == 0);
254 /* Clients must resolve a dbuf before attaching user data. */
255 ASSERT(db->db.db_data != NULL);
256 ASSERT3U(db->db_state, ==, DB_CACHED);
258 holds = refcount_count(&db->db_holds);
259 if (verify_type == DBVU_EVICTING) {
261 * Immediate eviction occurs when holds == dirtycnt.
262 * For normal eviction buffers, holds is zero on
263 * eviction, except when dbuf_fix_old_data() calls
264 * dbuf_clear_data(). However, the hold count can grow
265 * during eviction even though db_mtx is held (see
266 * dmu_bonus_hold() for an example), so we can only
267 * test the generic invariant that holds >= dirtycnt.
269 ASSERT3U(holds, >=, db->db_dirtycnt);
271 if (db->db_user_immediate_evict == TRUE)
272 ASSERT3U(holds, >=, db->db_dirtycnt);
274 ASSERT3U(holds, >, 0);
280 dbuf_evict_user(dmu_buf_impl_t *db)
282 dmu_buf_user_t *dbu = db->db_user;
284 ASSERT(MUTEX_HELD(&db->db_mtx));
289 dbuf_verify_user(db, DBVU_EVICTING);
293 if (dbu->dbu_clear_on_evict_dbufp != NULL)
294 *dbu->dbu_clear_on_evict_dbufp = NULL;
298 * Invoke the callback from a taskq to avoid lock order reversals
299 * and limit stack depth.
301 taskq_dispatch_ent(dbu_evict_taskq, dbu->dbu_evict_func, dbu, 0,
306 dbuf_is_metadata(dmu_buf_impl_t *db)
308 if (db->db_level > 0) {
311 boolean_t is_metadata;
314 is_metadata = DMU_OT_IS_METADATA(DB_DNODE(db)->dn_type);
317 return (is_metadata);
322 dbuf_evict(dmu_buf_impl_t *db)
324 ASSERT(MUTEX_HELD(&db->db_mtx));
325 ASSERT(db->db_buf == NULL);
326 ASSERT(db->db_data_pending == NULL);
335 uint64_t hsize = 1ULL << 16;
336 dbuf_hash_table_t *h = &dbuf_hash_table;
340 * The hash table is big enough to fill all of physical memory
341 * with an average 4K block size. The table will take up
342 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
344 while (hsize * 4096 < (uint64_t)physmem * PAGESIZE)
348 h->hash_table_mask = hsize - 1;
349 h->hash_table = kmem_zalloc(hsize * sizeof (void *), KM_NOSLEEP);
350 if (h->hash_table == NULL) {
351 /* XXX - we should really return an error instead of assert */
352 ASSERT(hsize > (1ULL << 10));
357 dbuf_cache = kmem_cache_create("dmu_buf_impl_t",
358 sizeof (dmu_buf_impl_t),
359 0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0);
361 for (i = 0; i < DBUF_MUTEXES; i++)
362 mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL);
365 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
366 * configuration is not required.
368 dbu_evict_taskq = taskq_create("dbu_evict", 1, minclsyspri, 0, 0, 0);
374 dbuf_hash_table_t *h = &dbuf_hash_table;
377 for (i = 0; i < DBUF_MUTEXES; i++)
378 mutex_destroy(&h->hash_mutexes[i]);
379 kmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
380 kmem_cache_destroy(dbuf_cache);
381 taskq_destroy(dbu_evict_taskq);
390 dbuf_verify(dmu_buf_impl_t *db)
393 dbuf_dirty_record_t *dr;
395 ASSERT(MUTEX_HELD(&db->db_mtx));
397 if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY))
400 ASSERT(db->db_objset != NULL);
404 ASSERT(db->db_parent == NULL);
405 ASSERT(db->db_blkptr == NULL);
407 ASSERT3U(db->db.db_object, ==, dn->dn_object);
408 ASSERT3P(db->db_objset, ==, dn->dn_objset);
409 ASSERT3U(db->db_level, <, dn->dn_nlevels);
410 ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
411 db->db_blkid == DMU_SPILL_BLKID ||
412 !avl_is_empty(&dn->dn_dbufs));
414 if (db->db_blkid == DMU_BONUS_BLKID) {
416 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
417 ASSERT3U(db->db.db_offset, ==, DMU_BONUS_BLKID);
418 } else if (db->db_blkid == DMU_SPILL_BLKID) {
420 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
421 ASSERT0(db->db.db_offset);
423 ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size);
426 for (dr = db->db_data_pending; dr != NULL; dr = dr->dr_next)
427 ASSERT(dr->dr_dbuf == db);
429 for (dr = db->db_last_dirty; dr != NULL; dr = dr->dr_next)
430 ASSERT(dr->dr_dbuf == db);
433 * We can't assert that db_size matches dn_datablksz because it
434 * can be momentarily different when another thread is doing
437 if (db->db_level == 0 && db->db.db_object == DMU_META_DNODE_OBJECT) {
438 dr = db->db_data_pending;
440 * It should only be modified in syncing context, so
441 * make sure we only have one copy of the data.
443 ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf);
446 /* verify db->db_blkptr */
448 if (db->db_parent == dn->dn_dbuf) {
449 /* db is pointed to by the dnode */
450 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
451 if (DMU_OBJECT_IS_SPECIAL(db->db.db_object))
452 ASSERT(db->db_parent == NULL);
454 ASSERT(db->db_parent != NULL);
455 if (db->db_blkid != DMU_SPILL_BLKID)
456 ASSERT3P(db->db_blkptr, ==,
457 &dn->dn_phys->dn_blkptr[db->db_blkid]);
459 /* db is pointed to by an indirect block */
460 int epb = db->db_parent->db.db_size >> SPA_BLKPTRSHIFT;
461 ASSERT3U(db->db_parent->db_level, ==, db->db_level+1);
462 ASSERT3U(db->db_parent->db.db_object, ==,
465 * dnode_grow_indblksz() can make this fail if we don't
466 * have the struct_rwlock. XXX indblksz no longer
467 * grows. safe to do this now?
469 if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
470 ASSERT3P(db->db_blkptr, ==,
471 ((blkptr_t *)db->db_parent->db.db_data +
472 db->db_blkid % epb));
476 if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) &&
477 (db->db_buf == NULL || db->db_buf->b_data) &&
478 db->db.db_data && db->db_blkid != DMU_BONUS_BLKID &&
479 db->db_state != DB_FILL && !dn->dn_free_txg) {
481 * If the blkptr isn't set but they have nonzero data,
482 * it had better be dirty, otherwise we'll lose that
483 * data when we evict this buffer.
485 if (db->db_dirtycnt == 0) {
486 uint64_t *buf = db->db.db_data;
489 for (i = 0; i < db->db.db_size >> 3; i++) {
499 dbuf_clear_data(dmu_buf_impl_t *db)
501 ASSERT(MUTEX_HELD(&db->db_mtx));
504 db->db.db_data = NULL;
505 if (db->db_state != DB_NOFILL)
506 db->db_state = DB_UNCACHED;
510 dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf)
512 ASSERT(MUTEX_HELD(&db->db_mtx));
516 ASSERT(buf->b_data != NULL);
517 db->db.db_data = buf->b_data;
518 if (!arc_released(buf))
519 arc_set_callback(buf, dbuf_do_evict, db);
523 * Loan out an arc_buf for read. Return the loaned arc_buf.
526 dbuf_loan_arcbuf(dmu_buf_impl_t *db)
530 mutex_enter(&db->db_mtx);
531 if (arc_released(db->db_buf) || refcount_count(&db->db_holds) > 1) {
532 int blksz = db->db.db_size;
533 spa_t *spa = db->db_objset->os_spa;
535 mutex_exit(&db->db_mtx);
536 abuf = arc_loan_buf(spa, blksz);
537 bcopy(db->db.db_data, abuf->b_data, blksz);
540 arc_loan_inuse_buf(abuf, db);
542 mutex_exit(&db->db_mtx);
548 * Calculate which level n block references the data at the level 0 offset
552 dbuf_whichblock(dnode_t *dn, int64_t level, uint64_t offset)
554 if (dn->dn_datablkshift != 0 && dn->dn_indblkshift != 0) {
556 * The level n blkid is equal to the level 0 blkid divided by
557 * the number of level 0s in a level n block.
559 * The level 0 blkid is offset >> datablkshift =
560 * offset / 2^datablkshift.
562 * The number of level 0s in a level n is the number of block
563 * pointers in an indirect block, raised to the power of level.
564 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
565 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
567 * Thus, the level n blkid is: offset /
568 * ((2^datablkshift)*(2^(level*(indblkshift - SPA_BLKPTRSHIFT)))
569 * = offset / 2^(datablkshift + level *
570 * (indblkshift - SPA_BLKPTRSHIFT))
571 * = offset >> (datablkshift + level *
572 * (indblkshift - SPA_BLKPTRSHIFT))
574 return (offset >> (dn->dn_datablkshift + level *
575 (dn->dn_indblkshift - SPA_BLKPTRSHIFT)));
577 ASSERT3U(offset, <, dn->dn_datablksz);
583 dbuf_read_done(zio_t *zio, arc_buf_t *buf, void *vdb)
585 dmu_buf_impl_t *db = vdb;
587 mutex_enter(&db->db_mtx);
588 ASSERT3U(db->db_state, ==, DB_READ);
590 * All reads are synchronous, so we must have a hold on the dbuf
592 ASSERT(refcount_count(&db->db_holds) > 0);
593 ASSERT(db->db_buf == NULL);
594 ASSERT(db->db.db_data == NULL);
595 if (db->db_level == 0 && db->db_freed_in_flight) {
596 /* we were freed in flight; disregard any error */
597 arc_release(buf, db);
598 bzero(buf->b_data, db->db.db_size);
600 db->db_freed_in_flight = FALSE;
601 dbuf_set_data(db, buf);
602 db->db_state = DB_CACHED;
603 } else if (zio == NULL || zio->io_error == 0) {
604 dbuf_set_data(db, buf);
605 db->db_state = DB_CACHED;
607 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
608 ASSERT3P(db->db_buf, ==, NULL);
609 VERIFY(arc_buf_remove_ref(buf, db));
610 db->db_state = DB_UNCACHED;
612 cv_broadcast(&db->db_changed);
613 dbuf_rele_and_unlock(db, NULL);
617 dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
621 arc_flags_t aflags = ARC_FLAG_NOWAIT;
625 ASSERT(!refcount_is_zero(&db->db_holds));
626 /* We need the struct_rwlock to prevent db_blkptr from changing. */
627 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
628 ASSERT(MUTEX_HELD(&db->db_mtx));
629 ASSERT(db->db_state == DB_UNCACHED);
630 ASSERT(db->db_buf == NULL);
632 if (db->db_blkid == DMU_BONUS_BLKID) {
633 int bonuslen = MIN(dn->dn_bonuslen, dn->dn_phys->dn_bonuslen);
635 ASSERT3U(bonuslen, <=, db->db.db_size);
636 db->db.db_data = zio_buf_alloc(DN_MAX_BONUSLEN);
637 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
638 if (bonuslen < DN_MAX_BONUSLEN)
639 bzero(db->db.db_data, DN_MAX_BONUSLEN);
641 bcopy(DN_BONUS(dn->dn_phys), db->db.db_data, bonuslen);
643 db->db_state = DB_CACHED;
644 mutex_exit(&db->db_mtx);
649 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
650 * processes the delete record and clears the bp while we are waiting
651 * for the dn_mtx (resulting in a "no" from block_freed).
653 if (db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr) ||
654 (db->db_level == 0 && (dnode_block_freed(dn, db->db_blkid) ||
655 BP_IS_HOLE(db->db_blkptr)))) {
656 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
659 dbuf_set_data(db, arc_buf_alloc(db->db_objset->os_spa,
660 db->db.db_size, db, type));
661 bzero(db->db.db_data, db->db.db_size);
662 db->db_state = DB_CACHED;
663 mutex_exit(&db->db_mtx);
669 db->db_state = DB_READ;
670 mutex_exit(&db->db_mtx);
672 if (DBUF_IS_L2CACHEABLE(db))
673 aflags |= ARC_FLAG_L2CACHE;
674 if (DBUF_IS_L2COMPRESSIBLE(db))
675 aflags |= ARC_FLAG_L2COMPRESS;
677 SET_BOOKMARK(&zb, db->db_objset->os_dsl_dataset ?
678 db->db_objset->os_dsl_dataset->ds_object : DMU_META_OBJSET,
679 db->db.db_object, db->db_level, db->db_blkid);
681 dbuf_add_ref(db, NULL);
683 (void) arc_read(zio, db->db_objset->os_spa, db->db_blkptr,
684 dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ,
685 (flags & DB_RF_CANFAIL) ? ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED,
690 dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
693 boolean_t havepzio = (zio != NULL);
698 * We don't have to hold the mutex to check db_state because it
699 * can't be freed while we have a hold on the buffer.
701 ASSERT(!refcount_is_zero(&db->db_holds));
703 if (db->db_state == DB_NOFILL)
704 return (SET_ERROR(EIO));
708 if ((flags & DB_RF_HAVESTRUCT) == 0)
709 rw_enter(&dn->dn_struct_rwlock, RW_READER);
711 prefetch = db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
712 (flags & DB_RF_NOPREFETCH) == 0 && dn != NULL &&
713 DBUF_IS_CACHEABLE(db);
715 mutex_enter(&db->db_mtx);
716 if (db->db_state == DB_CACHED) {
717 mutex_exit(&db->db_mtx);
719 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1);
720 if ((flags & DB_RF_HAVESTRUCT) == 0)
721 rw_exit(&dn->dn_struct_rwlock);
723 } else if (db->db_state == DB_UNCACHED) {
724 spa_t *spa = dn->dn_objset->os_spa;
727 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
728 dbuf_read_impl(db, zio, flags);
730 /* dbuf_read_impl has dropped db_mtx for us */
733 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1);
735 if ((flags & DB_RF_HAVESTRUCT) == 0)
736 rw_exit(&dn->dn_struct_rwlock);
743 * Another reader came in while the dbuf was in flight
744 * between UNCACHED and CACHED. Either a writer will finish
745 * writing the buffer (sending the dbuf to CACHED) or the
746 * first reader's request will reach the read_done callback
747 * and send the dbuf to CACHED. Otherwise, a failure
748 * occurred and the dbuf went to UNCACHED.
750 mutex_exit(&db->db_mtx);
752 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1);
753 if ((flags & DB_RF_HAVESTRUCT) == 0)
754 rw_exit(&dn->dn_struct_rwlock);
757 /* Skip the wait per the caller's request. */
758 mutex_enter(&db->db_mtx);
759 if ((flags & DB_RF_NEVERWAIT) == 0) {
760 while (db->db_state == DB_READ ||
761 db->db_state == DB_FILL) {
762 ASSERT(db->db_state == DB_READ ||
763 (flags & DB_RF_HAVESTRUCT) == 0);
764 DTRACE_PROBE2(blocked__read, dmu_buf_impl_t *,
766 cv_wait(&db->db_changed, &db->db_mtx);
768 if (db->db_state == DB_UNCACHED)
769 err = SET_ERROR(EIO);
771 mutex_exit(&db->db_mtx);
774 ASSERT(err || havepzio || db->db_state == DB_CACHED);
779 dbuf_noread(dmu_buf_impl_t *db)
781 ASSERT(!refcount_is_zero(&db->db_holds));
782 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
783 mutex_enter(&db->db_mtx);
784 while (db->db_state == DB_READ || db->db_state == DB_FILL)
785 cv_wait(&db->db_changed, &db->db_mtx);
786 if (db->db_state == DB_UNCACHED) {
787 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
788 spa_t *spa = db->db_objset->os_spa;
790 ASSERT(db->db_buf == NULL);
791 ASSERT(db->db.db_data == NULL);
792 dbuf_set_data(db, arc_buf_alloc(spa, db->db.db_size, db, type));
793 db->db_state = DB_FILL;
794 } else if (db->db_state == DB_NOFILL) {
797 ASSERT3U(db->db_state, ==, DB_CACHED);
799 mutex_exit(&db->db_mtx);
803 * This is our just-in-time copy function. It makes a copy of
804 * buffers, that have been modified in a previous transaction
805 * group, before we modify them in the current active group.
807 * This function is used in two places: when we are dirtying a
808 * buffer for the first time in a txg, and when we are freeing
809 * a range in a dnode that includes this buffer.
811 * Note that when we are called from dbuf_free_range() we do
812 * not put a hold on the buffer, we just traverse the active
813 * dbuf list for the dnode.
816 dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg)
818 dbuf_dirty_record_t *dr = db->db_last_dirty;
820 ASSERT(MUTEX_HELD(&db->db_mtx));
821 ASSERT(db->db.db_data != NULL);
822 ASSERT(db->db_level == 0);
823 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT);
826 (dr->dt.dl.dr_data !=
827 ((db->db_blkid == DMU_BONUS_BLKID) ? db->db.db_data : db->db_buf)))
831 * If the last dirty record for this dbuf has not yet synced
832 * and its referencing the dbuf data, either:
833 * reset the reference to point to a new copy,
834 * or (if there a no active holders)
835 * just null out the current db_data pointer.
837 ASSERT(dr->dr_txg >= txg - 2);
838 if (db->db_blkid == DMU_BONUS_BLKID) {
839 /* Note that the data bufs here are zio_bufs */
840 dr->dt.dl.dr_data = zio_buf_alloc(DN_MAX_BONUSLEN);
841 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
842 bcopy(db->db.db_data, dr->dt.dl.dr_data, DN_MAX_BONUSLEN);
843 } else if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
844 int size = db->db.db_size;
845 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
846 spa_t *spa = db->db_objset->os_spa;
848 dr->dt.dl.dr_data = arc_buf_alloc(spa, size, db, type);
849 bcopy(db->db.db_data, dr->dt.dl.dr_data->b_data, size);
856 dbuf_unoverride(dbuf_dirty_record_t *dr)
858 dmu_buf_impl_t *db = dr->dr_dbuf;
859 blkptr_t *bp = &dr->dt.dl.dr_overridden_by;
860 uint64_t txg = dr->dr_txg;
862 ASSERT(MUTEX_HELD(&db->db_mtx));
863 ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC);
864 ASSERT(db->db_level == 0);
866 if (db->db_blkid == DMU_BONUS_BLKID ||
867 dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN)
870 ASSERT(db->db_data_pending != dr);
872 /* free this block */
873 if (!BP_IS_HOLE(bp) && !dr->dt.dl.dr_nopwrite)
874 zio_free(db->db_objset->os_spa, txg, bp);
876 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
877 dr->dt.dl.dr_nopwrite = B_FALSE;
880 * Release the already-written buffer, so we leave it in
881 * a consistent dirty state. Note that all callers are
882 * modifying the buffer, so they will immediately do
883 * another (redundant) arc_release(). Therefore, leave
884 * the buf thawed to save the effort of freezing &
885 * immediately re-thawing it.
887 arc_release(dr->dt.dl.dr_data, db);
891 * Evict (if its unreferenced) or clear (if its referenced) any level-0
892 * data blocks in the free range, so that any future readers will find
895 * This is a no-op if the dataset is in the middle of an incremental
896 * receive; see comment below for details.
899 dbuf_free_range(dnode_t *dn, uint64_t start_blkid, uint64_t end_blkid,
902 dmu_buf_impl_t db_search;
903 dmu_buf_impl_t *db, *db_next;
904 uint64_t txg = tx->tx_txg;
907 if (end_blkid > dn->dn_maxblkid && (end_blkid != DMU_SPILL_BLKID))
908 end_blkid = dn->dn_maxblkid;
909 dprintf_dnode(dn, "start=%llu end=%llu\n", start_blkid, end_blkid);
911 db_search.db_level = 0;
912 db_search.db_blkid = start_blkid;
913 db_search.db_state = DB_SEARCH;
915 mutex_enter(&dn->dn_dbufs_mtx);
916 if (start_blkid >= dn->dn_unlisted_l0_blkid) {
917 /* There can't be any dbufs in this range; no need to search. */
919 db = avl_find(&dn->dn_dbufs, &db_search, &where);
920 ASSERT3P(db, ==, NULL);
921 db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER);
922 ASSERT(db == NULL || db->db_level > 0);
924 mutex_exit(&dn->dn_dbufs_mtx);
926 } else if (dmu_objset_is_receiving(dn->dn_objset)) {
928 * If we are receiving, we expect there to be no dbufs in
929 * the range to be freed, because receive modifies each
930 * block at most once, and in offset order. If this is
931 * not the case, it can lead to performance problems,
932 * so note that we unexpectedly took the slow path.
934 atomic_inc_64(&zfs_free_range_recv_miss);
937 db = avl_find(&dn->dn_dbufs, &db_search, &where);
938 ASSERT3P(db, ==, NULL);
939 db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER);
941 for (; db != NULL; db = db_next) {
942 db_next = AVL_NEXT(&dn->dn_dbufs, db);
943 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
945 if (db->db_level != 0 || db->db_blkid > end_blkid) {
948 ASSERT3U(db->db_blkid, >=, start_blkid);
950 /* found a level 0 buffer in the range */
951 mutex_enter(&db->db_mtx);
952 if (dbuf_undirty(db, tx)) {
953 /* mutex has been dropped and dbuf destroyed */
957 if (db->db_state == DB_UNCACHED ||
958 db->db_state == DB_NOFILL ||
959 db->db_state == DB_EVICTING) {
960 ASSERT(db->db.db_data == NULL);
961 mutex_exit(&db->db_mtx);
964 if (db->db_state == DB_READ || db->db_state == DB_FILL) {
965 /* will be handled in dbuf_read_done or dbuf_rele */
966 db->db_freed_in_flight = TRUE;
967 mutex_exit(&db->db_mtx);
970 if (refcount_count(&db->db_holds) == 0) {
975 /* The dbuf is referenced */
977 if (db->db_last_dirty != NULL) {
978 dbuf_dirty_record_t *dr = db->db_last_dirty;
980 if (dr->dr_txg == txg) {
982 * This buffer is "in-use", re-adjust the file
983 * size to reflect that this buffer may
984 * contain new data when we sync.
986 if (db->db_blkid != DMU_SPILL_BLKID &&
987 db->db_blkid > dn->dn_maxblkid)
988 dn->dn_maxblkid = db->db_blkid;
992 * This dbuf is not dirty in the open context.
993 * Either uncache it (if its not referenced in
994 * the open context) or reset its contents to
997 dbuf_fix_old_data(db, txg);
1000 /* clear the contents if its cached */
1001 if (db->db_state == DB_CACHED) {
1002 ASSERT(db->db.db_data != NULL);
1003 arc_release(db->db_buf, db);
1004 bzero(db->db.db_data, db->db.db_size);
1005 arc_buf_freeze(db->db_buf);
1008 mutex_exit(&db->db_mtx);
1010 mutex_exit(&dn->dn_dbufs_mtx);
1014 dbuf_block_freeable(dmu_buf_impl_t *db)
1016 dsl_dataset_t *ds = db->db_objset->os_dsl_dataset;
1017 uint64_t birth_txg = 0;
1020 * We don't need any locking to protect db_blkptr:
1021 * If it's syncing, then db_last_dirty will be set
1022 * so we'll ignore db_blkptr.
1024 * This logic ensures that only block births for
1025 * filled blocks are considered.
1027 ASSERT(MUTEX_HELD(&db->db_mtx));
1028 if (db->db_last_dirty && (db->db_blkptr == NULL ||
1029 !BP_IS_HOLE(db->db_blkptr))) {
1030 birth_txg = db->db_last_dirty->dr_txg;
1031 } else if (db->db_blkptr != NULL && !BP_IS_HOLE(db->db_blkptr)) {
1032 birth_txg = db->db_blkptr->blk_birth;
1036 * If this block don't exist or is in a snapshot, it can't be freed.
1037 * Don't pass the bp to dsl_dataset_block_freeable() since we
1038 * are holding the db_mtx lock and might deadlock if we are
1039 * prefetching a dedup-ed block.
1042 return (ds == NULL ||
1043 dsl_dataset_block_freeable(ds, NULL, birth_txg));
1049 dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx)
1051 arc_buf_t *buf, *obuf;
1052 int osize = db->db.db_size;
1053 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1056 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1061 /* XXX does *this* func really need the lock? */
1062 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
1065 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
1066 * is OK, because there can be no other references to the db
1067 * when we are changing its size, so no concurrent DB_FILL can
1071 * XXX we should be doing a dbuf_read, checking the return
1072 * value and returning that up to our callers
1074 dmu_buf_will_dirty(&db->db, tx);
1076 /* create the data buffer for the new block */
1077 buf = arc_buf_alloc(dn->dn_objset->os_spa, size, db, type);
1079 /* copy old block data to the new block */
1081 bcopy(obuf->b_data, buf->b_data, MIN(osize, size));
1082 /* zero the remainder */
1084 bzero((uint8_t *)buf->b_data + osize, size - osize);
1086 mutex_enter(&db->db_mtx);
1087 dbuf_set_data(db, buf);
1088 VERIFY(arc_buf_remove_ref(obuf, db));
1089 db->db.db_size = size;
1091 if (db->db_level == 0) {
1092 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
1093 db->db_last_dirty->dt.dl.dr_data = buf;
1095 mutex_exit(&db->db_mtx);
1097 dnode_willuse_space(dn, size-osize, tx);
1102 dbuf_release_bp(dmu_buf_impl_t *db)
1104 objset_t *os = db->db_objset;
1106 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
1107 ASSERT(arc_released(os->os_phys_buf) ||
1108 list_link_active(&os->os_dsl_dataset->ds_synced_link));
1109 ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf));
1111 (void) arc_release(db->db_buf, db);
1115 * We already have a dirty record for this TXG, and we are being
1119 dbuf_redirty(dbuf_dirty_record_t *dr)
1121 dmu_buf_impl_t *db = dr->dr_dbuf;
1123 ASSERT(MUTEX_HELD(&db->db_mtx));
1125 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID) {
1127 * If this buffer has already been written out,
1128 * we now need to reset its state.
1130 dbuf_unoverride(dr);
1131 if (db->db.db_object != DMU_META_DNODE_OBJECT &&
1132 db->db_state != DB_NOFILL) {
1133 /* Already released on initial dirty, so just thaw. */
1134 ASSERT(arc_released(db->db_buf));
1135 arc_buf_thaw(db->db_buf);
1140 dbuf_dirty_record_t *
1141 dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1145 dbuf_dirty_record_t **drp, *dr;
1146 int drop_struct_lock = FALSE;
1147 boolean_t do_free_accounting = B_FALSE;
1148 int txgoff = tx->tx_txg & TXG_MASK;
1150 ASSERT(tx->tx_txg != 0);
1151 ASSERT(!refcount_is_zero(&db->db_holds));
1152 DMU_TX_DIRTY_BUF(tx, db);
1157 * Shouldn't dirty a regular buffer in syncing context. Private
1158 * objects may be dirtied in syncing context, but only if they
1159 * were already pre-dirtied in open context.
1161 ASSERT(!dmu_tx_is_syncing(tx) ||
1162 BP_IS_HOLE(dn->dn_objset->os_rootbp) ||
1163 DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1164 dn->dn_objset->os_dsl_dataset == NULL);
1166 * We make this assert for private objects as well, but after we
1167 * check if we're already dirty. They are allowed to re-dirty
1168 * in syncing context.
1170 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1171 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1172 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1174 mutex_enter(&db->db_mtx);
1176 * XXX make this true for indirects too? The problem is that
1177 * transactions created with dmu_tx_create_assigned() from
1178 * syncing context don't bother holding ahead.
1180 ASSERT(db->db_level != 0 ||
1181 db->db_state == DB_CACHED || db->db_state == DB_FILL ||
1182 db->db_state == DB_NOFILL);
1184 mutex_enter(&dn->dn_mtx);
1186 * Don't set dirtyctx to SYNC if we're just modifying this as we
1187 * initialize the objset.
1189 if (dn->dn_dirtyctx == DN_UNDIRTIED &&
1190 !BP_IS_HOLE(dn->dn_objset->os_rootbp)) {
1192 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN);
1193 ASSERT(dn->dn_dirtyctx_firstset == NULL);
1194 dn->dn_dirtyctx_firstset = kmem_alloc(1, KM_SLEEP);
1196 mutex_exit(&dn->dn_mtx);
1198 if (db->db_blkid == DMU_SPILL_BLKID)
1199 dn->dn_have_spill = B_TRUE;
1202 * If this buffer is already dirty, we're done.
1204 drp = &db->db_last_dirty;
1205 ASSERT(*drp == NULL || (*drp)->dr_txg <= tx->tx_txg ||
1206 db->db.db_object == DMU_META_DNODE_OBJECT);
1207 while ((dr = *drp) != NULL && dr->dr_txg > tx->tx_txg)
1209 if (dr && dr->dr_txg == tx->tx_txg) {
1213 mutex_exit(&db->db_mtx);
1218 * Only valid if not already dirty.
1220 ASSERT(dn->dn_object == 0 ||
1221 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1222 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1224 ASSERT3U(dn->dn_nlevels, >, db->db_level);
1225 ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) ||
1226 dn->dn_phys->dn_nlevels > db->db_level ||
1227 dn->dn_next_nlevels[txgoff] > db->db_level ||
1228 dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level ||
1229 dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level);
1232 * We should only be dirtying in syncing context if it's the
1233 * mos or we're initializing the os or it's a special object.
1234 * However, we are allowed to dirty in syncing context provided
1235 * we already dirtied it in open context. Hence we must make
1236 * this assertion only if we're not already dirty.
1239 ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1240 os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp));
1241 ASSERT(db->db.db_size != 0);
1243 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1245 if (db->db_blkid != DMU_BONUS_BLKID) {
1247 * Update the accounting.
1248 * Note: we delay "free accounting" until after we drop
1249 * the db_mtx. This keeps us from grabbing other locks
1250 * (and possibly deadlocking) in bp_get_dsize() while
1251 * also holding the db_mtx.
1253 dnode_willuse_space(dn, db->db.db_size, tx);
1254 do_free_accounting = dbuf_block_freeable(db);
1258 * If this buffer is dirty in an old transaction group we need
1259 * to make a copy of it so that the changes we make in this
1260 * transaction group won't leak out when we sync the older txg.
1262 dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP);
1263 if (db->db_level == 0) {
1264 void *data_old = db->db_buf;
1266 if (db->db_state != DB_NOFILL) {
1267 if (db->db_blkid == DMU_BONUS_BLKID) {
1268 dbuf_fix_old_data(db, tx->tx_txg);
1269 data_old = db->db.db_data;
1270 } else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
1272 * Release the data buffer from the cache so
1273 * that we can modify it without impacting
1274 * possible other users of this cached data
1275 * block. Note that indirect blocks and
1276 * private objects are not released until the
1277 * syncing state (since they are only modified
1280 arc_release(db->db_buf, db);
1281 dbuf_fix_old_data(db, tx->tx_txg);
1282 data_old = db->db_buf;
1284 ASSERT(data_old != NULL);
1286 dr->dt.dl.dr_data = data_old;
1288 mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_DEFAULT, NULL);
1289 list_create(&dr->dt.di.dr_children,
1290 sizeof (dbuf_dirty_record_t),
1291 offsetof(dbuf_dirty_record_t, dr_dirty_node));
1293 if (db->db_blkid != DMU_BONUS_BLKID && os->os_dsl_dataset != NULL)
1294 dr->dr_accounted = db->db.db_size;
1296 dr->dr_txg = tx->tx_txg;
1301 * We could have been freed_in_flight between the dbuf_noread
1302 * and dbuf_dirty. We win, as though the dbuf_noread() had
1303 * happened after the free.
1305 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1306 db->db_blkid != DMU_SPILL_BLKID) {
1307 mutex_enter(&dn->dn_mtx);
1308 if (dn->dn_free_ranges[txgoff] != NULL) {
1309 range_tree_clear(dn->dn_free_ranges[txgoff],
1312 mutex_exit(&dn->dn_mtx);
1313 db->db_freed_in_flight = FALSE;
1317 * This buffer is now part of this txg
1319 dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg);
1320 db->db_dirtycnt += 1;
1321 ASSERT3U(db->db_dirtycnt, <=, 3);
1323 mutex_exit(&db->db_mtx);
1325 if (db->db_blkid == DMU_BONUS_BLKID ||
1326 db->db_blkid == DMU_SPILL_BLKID) {
1327 mutex_enter(&dn->dn_mtx);
1328 ASSERT(!list_link_active(&dr->dr_dirty_node));
1329 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1330 mutex_exit(&dn->dn_mtx);
1331 dnode_setdirty(dn, tx);
1334 } else if (do_free_accounting) {
1335 blkptr_t *bp = db->db_blkptr;
1336 int64_t willfree = (bp && !BP_IS_HOLE(bp)) ?
1337 bp_get_dsize(os->os_spa, bp) : db->db.db_size;
1339 * This is only a guess -- if the dbuf is dirty
1340 * in a previous txg, we don't know how much
1341 * space it will use on disk yet. We should
1342 * really have the struct_rwlock to access
1343 * db_blkptr, but since this is just a guess,
1344 * it's OK if we get an odd answer.
1346 ddt_prefetch(os->os_spa, bp);
1347 dnode_willuse_space(dn, -willfree, tx);
1350 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
1351 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1352 drop_struct_lock = TRUE;
1355 if (db->db_level == 0) {
1356 dnode_new_blkid(dn, db->db_blkid, tx, drop_struct_lock);
1357 ASSERT(dn->dn_maxblkid >= db->db_blkid);
1360 if (db->db_level+1 < dn->dn_nlevels) {
1361 dmu_buf_impl_t *parent = db->db_parent;
1362 dbuf_dirty_record_t *di;
1363 int parent_held = FALSE;
1365 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
1366 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1368 parent = dbuf_hold_level(dn, db->db_level+1,
1369 db->db_blkid >> epbs, FTAG);
1370 ASSERT(parent != NULL);
1373 if (drop_struct_lock)
1374 rw_exit(&dn->dn_struct_rwlock);
1375 ASSERT3U(db->db_level+1, ==, parent->db_level);
1376 di = dbuf_dirty(parent, tx);
1378 dbuf_rele(parent, FTAG);
1380 mutex_enter(&db->db_mtx);
1382 * Since we've dropped the mutex, it's possible that
1383 * dbuf_undirty() might have changed this out from under us.
1385 if (db->db_last_dirty == dr ||
1386 dn->dn_object == DMU_META_DNODE_OBJECT) {
1387 mutex_enter(&di->dt.di.dr_mtx);
1388 ASSERT3U(di->dr_txg, ==, tx->tx_txg);
1389 ASSERT(!list_link_active(&dr->dr_dirty_node));
1390 list_insert_tail(&di->dt.di.dr_children, dr);
1391 mutex_exit(&di->dt.di.dr_mtx);
1394 mutex_exit(&db->db_mtx);
1396 ASSERT(db->db_level+1 == dn->dn_nlevels);
1397 ASSERT(db->db_blkid < dn->dn_nblkptr);
1398 ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf);
1399 mutex_enter(&dn->dn_mtx);
1400 ASSERT(!list_link_active(&dr->dr_dirty_node));
1401 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1402 mutex_exit(&dn->dn_mtx);
1403 if (drop_struct_lock)
1404 rw_exit(&dn->dn_struct_rwlock);
1407 dnode_setdirty(dn, tx);
1413 * Undirty a buffer in the transaction group referenced by the given
1414 * transaction. Return whether this evicted the dbuf.
1417 dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1420 uint64_t txg = tx->tx_txg;
1421 dbuf_dirty_record_t *dr, **drp;
1426 * Due to our use of dn_nlevels below, this can only be called
1427 * in open context, unless we are operating on the MOS.
1428 * From syncing context, dn_nlevels may be different from the
1429 * dn_nlevels used when dbuf was dirtied.
1431 ASSERT(db->db_objset ==
1432 dmu_objset_pool(db->db_objset)->dp_meta_objset ||
1433 txg != spa_syncing_txg(dmu_objset_spa(db->db_objset)));
1434 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1435 ASSERT0(db->db_level);
1436 ASSERT(MUTEX_HELD(&db->db_mtx));
1439 * If this buffer is not dirty, we're done.
1441 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1442 if (dr->dr_txg <= txg)
1444 if (dr == NULL || dr->dr_txg < txg)
1446 ASSERT(dr->dr_txg == txg);
1447 ASSERT(dr->dr_dbuf == db);
1452 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1454 ASSERT(db->db.db_size != 0);
1456 dsl_pool_undirty_space(dmu_objset_pool(dn->dn_objset),
1457 dr->dr_accounted, txg);
1462 * Note that there are three places in dbuf_dirty()
1463 * where this dirty record may be put on a list.
1464 * Make sure to do a list_remove corresponding to
1465 * every one of those list_insert calls.
1467 if (dr->dr_parent) {
1468 mutex_enter(&dr->dr_parent->dt.di.dr_mtx);
1469 list_remove(&dr->dr_parent->dt.di.dr_children, dr);
1470 mutex_exit(&dr->dr_parent->dt.di.dr_mtx);
1471 } else if (db->db_blkid == DMU_SPILL_BLKID ||
1472 db->db_level + 1 == dn->dn_nlevels) {
1473 ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf);
1474 mutex_enter(&dn->dn_mtx);
1475 list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr);
1476 mutex_exit(&dn->dn_mtx);
1480 if (db->db_state != DB_NOFILL) {
1481 dbuf_unoverride(dr);
1483 ASSERT(db->db_buf != NULL);
1484 ASSERT(dr->dt.dl.dr_data != NULL);
1485 if (dr->dt.dl.dr_data != db->db_buf)
1486 VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data, db));
1489 kmem_free(dr, sizeof (dbuf_dirty_record_t));
1491 ASSERT(db->db_dirtycnt > 0);
1492 db->db_dirtycnt -= 1;
1494 if (refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) {
1495 arc_buf_t *buf = db->db_buf;
1497 ASSERT(db->db_state == DB_NOFILL || arc_released(buf));
1498 dbuf_clear_data(db);
1499 VERIFY(arc_buf_remove_ref(buf, db));
1508 dmu_buf_will_dirty(dmu_buf_t *db_fake, dmu_tx_t *tx)
1510 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1511 int rf = DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH;
1513 ASSERT(tx->tx_txg != 0);
1514 ASSERT(!refcount_is_zero(&db->db_holds));
1517 * Quick check for dirtyness. For already dirty blocks, this
1518 * reduces runtime of this function by >90%, and overall performance
1519 * by 50% for some workloads (e.g. file deletion with indirect blocks
1522 mutex_enter(&db->db_mtx);
1523 dbuf_dirty_record_t *dr;
1524 for (dr = db->db_last_dirty;
1525 dr != NULL && dr->dr_txg >= tx->tx_txg; dr = dr->dr_next) {
1527 * It's possible that it is already dirty but not cached,
1528 * because there are some calls to dbuf_dirty() that don't
1529 * go through dmu_buf_will_dirty().
1531 if (dr->dr_txg == tx->tx_txg && db->db_state == DB_CACHED) {
1532 /* This dbuf is already dirty and cached. */
1534 mutex_exit(&db->db_mtx);
1538 mutex_exit(&db->db_mtx);
1541 if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock))
1542 rf |= DB_RF_HAVESTRUCT;
1544 (void) dbuf_read(db, NULL, rf);
1545 (void) dbuf_dirty(db, tx);
1549 dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1551 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1553 db->db_state = DB_NOFILL;
1555 dmu_buf_will_fill(db_fake, tx);
1559 dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1561 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1563 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1564 ASSERT(tx->tx_txg != 0);
1565 ASSERT(db->db_level == 0);
1566 ASSERT(!refcount_is_zero(&db->db_holds));
1568 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
1569 dmu_tx_private_ok(tx));
1572 (void) dbuf_dirty(db, tx);
1575 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1578 dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx)
1580 mutex_enter(&db->db_mtx);
1583 if (db->db_state == DB_FILL) {
1584 if (db->db_level == 0 && db->db_freed_in_flight) {
1585 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1586 /* we were freed while filling */
1587 /* XXX dbuf_undirty? */
1588 bzero(db->db.db_data, db->db.db_size);
1589 db->db_freed_in_flight = FALSE;
1591 db->db_state = DB_CACHED;
1592 cv_broadcast(&db->db_changed);
1594 mutex_exit(&db->db_mtx);
1598 dmu_buf_write_embedded(dmu_buf_t *dbuf, void *data,
1599 bp_embedded_type_t etype, enum zio_compress comp,
1600 int uncompressed_size, int compressed_size, int byteorder,
1603 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
1604 struct dirty_leaf *dl;
1605 dmu_object_type_t type;
1607 if (etype == BP_EMBEDDED_TYPE_DATA) {
1608 ASSERT(spa_feature_is_active(dmu_objset_spa(db->db_objset),
1609 SPA_FEATURE_EMBEDDED_DATA));
1613 type = DB_DNODE(db)->dn_type;
1616 ASSERT0(db->db_level);
1617 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1619 dmu_buf_will_not_fill(dbuf, tx);
1621 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
1622 dl = &db->db_last_dirty->dt.dl;
1623 encode_embedded_bp_compressed(&dl->dr_overridden_by,
1624 data, comp, uncompressed_size, compressed_size);
1625 BPE_SET_ETYPE(&dl->dr_overridden_by, etype);
1626 BP_SET_TYPE(&dl->dr_overridden_by, type);
1627 BP_SET_LEVEL(&dl->dr_overridden_by, 0);
1628 BP_SET_BYTEORDER(&dl->dr_overridden_by, byteorder);
1630 dl->dr_override_state = DR_OVERRIDDEN;
1631 dl->dr_overridden_by.blk_birth = db->db_last_dirty->dr_txg;
1635 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1636 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1639 dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx)
1641 ASSERT(!refcount_is_zero(&db->db_holds));
1642 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1643 ASSERT(db->db_level == 0);
1644 ASSERT(DBUF_GET_BUFC_TYPE(db) == ARC_BUFC_DATA);
1645 ASSERT(buf != NULL);
1646 ASSERT(arc_buf_size(buf) == db->db.db_size);
1647 ASSERT(tx->tx_txg != 0);
1649 arc_return_buf(buf, db);
1650 ASSERT(arc_released(buf));
1652 mutex_enter(&db->db_mtx);
1654 while (db->db_state == DB_READ || db->db_state == DB_FILL)
1655 cv_wait(&db->db_changed, &db->db_mtx);
1657 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED);
1659 if (db->db_state == DB_CACHED &&
1660 refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) {
1661 mutex_exit(&db->db_mtx);
1662 (void) dbuf_dirty(db, tx);
1663 bcopy(buf->b_data, db->db.db_data, db->db.db_size);
1664 VERIFY(arc_buf_remove_ref(buf, db));
1665 xuio_stat_wbuf_copied();
1669 xuio_stat_wbuf_nocopy();
1670 if (db->db_state == DB_CACHED) {
1671 dbuf_dirty_record_t *dr = db->db_last_dirty;
1673 ASSERT(db->db_buf != NULL);
1674 if (dr != NULL && dr->dr_txg == tx->tx_txg) {
1675 ASSERT(dr->dt.dl.dr_data == db->db_buf);
1676 if (!arc_released(db->db_buf)) {
1677 ASSERT(dr->dt.dl.dr_override_state ==
1679 arc_release(db->db_buf, db);
1681 dr->dt.dl.dr_data = buf;
1682 VERIFY(arc_buf_remove_ref(db->db_buf, db));
1683 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) {
1684 arc_release(db->db_buf, db);
1685 VERIFY(arc_buf_remove_ref(db->db_buf, db));
1689 ASSERT(db->db_buf == NULL);
1690 dbuf_set_data(db, buf);
1691 db->db_state = DB_FILL;
1692 mutex_exit(&db->db_mtx);
1693 (void) dbuf_dirty(db, tx);
1694 dmu_buf_fill_done(&db->db, tx);
1698 * "Clear" the contents of this dbuf. This will mark the dbuf
1699 * EVICTING and clear *most* of its references. Unfortunately,
1700 * when we are not holding the dn_dbufs_mtx, we can't clear the
1701 * entry in the dn_dbufs list. We have to wait until dbuf_destroy()
1702 * in this case. For callers from the DMU we will usually see:
1703 * dbuf_clear()->arc_clear_callback()->dbuf_do_evict()->dbuf_destroy()
1704 * For the arc callback, we will usually see:
1705 * dbuf_do_evict()->dbuf_clear();dbuf_destroy()
1706 * Sometimes, though, we will get a mix of these two:
1707 * DMU: dbuf_clear()->arc_clear_callback()
1708 * ARC: dbuf_do_evict()->dbuf_destroy()
1710 * This routine will dissociate the dbuf from the arc, by calling
1711 * arc_clear_callback(), but will not evict the data from the ARC.
1714 dbuf_clear(dmu_buf_impl_t *db)
1717 dmu_buf_impl_t *parent = db->db_parent;
1718 dmu_buf_impl_t *dndb;
1719 boolean_t dbuf_gone = B_FALSE;
1721 ASSERT(MUTEX_HELD(&db->db_mtx));
1722 ASSERT(refcount_is_zero(&db->db_holds));
1724 dbuf_evict_user(db);
1726 if (db->db_state == DB_CACHED) {
1727 ASSERT(db->db.db_data != NULL);
1728 if (db->db_blkid == DMU_BONUS_BLKID) {
1729 zio_buf_free(db->db.db_data, DN_MAX_BONUSLEN);
1730 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
1732 db->db.db_data = NULL;
1733 db->db_state = DB_UNCACHED;
1736 ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL);
1737 ASSERT(db->db_data_pending == NULL);
1739 db->db_state = DB_EVICTING;
1740 db->db_blkptr = NULL;
1745 if (db->db_blkid != DMU_BONUS_BLKID && MUTEX_HELD(&dn->dn_dbufs_mtx)) {
1746 avl_remove(&dn->dn_dbufs, db);
1747 atomic_dec_32(&dn->dn_dbufs_count);
1751 * Decrementing the dbuf count means that the hold corresponding
1752 * to the removed dbuf is no longer discounted in dnode_move(),
1753 * so the dnode cannot be moved until after we release the hold.
1754 * The membar_producer() ensures visibility of the decremented
1755 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
1759 db->db_dnode_handle = NULL;
1765 dbuf_gone = arc_clear_callback(db->db_buf);
1768 mutex_exit(&db->db_mtx);
1771 * If this dbuf is referenced from an indirect dbuf,
1772 * decrement the ref count on the indirect dbuf.
1774 if (parent && parent != dndb)
1775 dbuf_rele(parent, db);
1779 * Note: While bpp will always be updated if the function returns success,
1780 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
1781 * this happens when the dnode is the meta-dnode, or a userused or groupused
1785 dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse,
1786 dmu_buf_impl_t **parentp, blkptr_t **bpp)
1793 ASSERT(blkid != DMU_BONUS_BLKID);
1795 if (blkid == DMU_SPILL_BLKID) {
1796 mutex_enter(&dn->dn_mtx);
1797 if (dn->dn_have_spill &&
1798 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
1799 *bpp = &dn->dn_phys->dn_spill;
1802 dbuf_add_ref(dn->dn_dbuf, NULL);
1803 *parentp = dn->dn_dbuf;
1804 mutex_exit(&dn->dn_mtx);
1808 if (dn->dn_phys->dn_nlevels == 0)
1811 nlevels = dn->dn_phys->dn_nlevels;
1813 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1815 ASSERT3U(level * epbs, <, 64);
1816 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1817 if (level >= nlevels ||
1818 (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) {
1819 /* the buffer has no parent yet */
1820 return (SET_ERROR(ENOENT));
1821 } else if (level < nlevels-1) {
1822 /* this block is referenced from an indirect block */
1823 int err = dbuf_hold_impl(dn, level+1,
1824 blkid >> epbs, fail_sparse, FALSE, NULL, parentp);
1827 err = dbuf_read(*parentp, NULL,
1828 (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL));
1830 dbuf_rele(*parentp, NULL);
1834 *bpp = ((blkptr_t *)(*parentp)->db.db_data) +
1835 (blkid & ((1ULL << epbs) - 1));
1838 /* the block is referenced from the dnode */
1839 ASSERT3U(level, ==, nlevels-1);
1840 ASSERT(dn->dn_phys->dn_nblkptr == 0 ||
1841 blkid < dn->dn_phys->dn_nblkptr);
1843 dbuf_add_ref(dn->dn_dbuf, NULL);
1844 *parentp = dn->dn_dbuf;
1846 *bpp = &dn->dn_phys->dn_blkptr[blkid];
1851 static dmu_buf_impl_t *
1852 dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid,
1853 dmu_buf_impl_t *parent, blkptr_t *blkptr)
1855 objset_t *os = dn->dn_objset;
1856 dmu_buf_impl_t *db, *odb;
1858 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1859 ASSERT(dn->dn_type != DMU_OT_NONE);
1861 db = kmem_cache_alloc(dbuf_cache, KM_SLEEP);
1864 db->db.db_object = dn->dn_object;
1865 db->db_level = level;
1866 db->db_blkid = blkid;
1867 db->db_last_dirty = NULL;
1868 db->db_dirtycnt = 0;
1869 db->db_dnode_handle = dn->dn_handle;
1870 db->db_parent = parent;
1871 db->db_blkptr = blkptr;
1874 db->db_user_immediate_evict = FALSE;
1875 db->db_freed_in_flight = FALSE;
1876 db->db_pending_evict = FALSE;
1878 if (blkid == DMU_BONUS_BLKID) {
1879 ASSERT3P(parent, ==, dn->dn_dbuf);
1880 db->db.db_size = DN_MAX_BONUSLEN -
1881 (dn->dn_nblkptr-1) * sizeof (blkptr_t);
1882 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
1883 db->db.db_offset = DMU_BONUS_BLKID;
1884 db->db_state = DB_UNCACHED;
1885 /* the bonus dbuf is not placed in the hash table */
1886 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1888 } else if (blkid == DMU_SPILL_BLKID) {
1889 db->db.db_size = (blkptr != NULL) ?
1890 BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE;
1891 db->db.db_offset = 0;
1894 db->db_level ? 1 << dn->dn_indblkshift : dn->dn_datablksz;
1895 db->db.db_size = blocksize;
1896 db->db.db_offset = db->db_blkid * blocksize;
1900 * Hold the dn_dbufs_mtx while we get the new dbuf
1901 * in the hash table *and* added to the dbufs list.
1902 * This prevents a possible deadlock with someone
1903 * trying to look up this dbuf before its added to the
1906 mutex_enter(&dn->dn_dbufs_mtx);
1907 db->db_state = DB_EVICTING;
1908 if ((odb = dbuf_hash_insert(db)) != NULL) {
1909 /* someone else inserted it first */
1910 kmem_cache_free(dbuf_cache, db);
1911 mutex_exit(&dn->dn_dbufs_mtx);
1914 avl_add(&dn->dn_dbufs, db);
1915 if (db->db_level == 0 && db->db_blkid >=
1916 dn->dn_unlisted_l0_blkid)
1917 dn->dn_unlisted_l0_blkid = db->db_blkid + 1;
1918 db->db_state = DB_UNCACHED;
1919 mutex_exit(&dn->dn_dbufs_mtx);
1920 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1922 if (parent && parent != dn->dn_dbuf)
1923 dbuf_add_ref(parent, db);
1925 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1926 refcount_count(&dn->dn_holds) > 0);
1927 (void) refcount_add(&dn->dn_holds, db);
1928 atomic_inc_32(&dn->dn_dbufs_count);
1930 dprintf_dbuf(db, "db=%p\n", db);
1936 dbuf_do_evict(void *private)
1938 dmu_buf_impl_t *db = private;
1940 if (!MUTEX_HELD(&db->db_mtx))
1941 mutex_enter(&db->db_mtx);
1943 ASSERT(refcount_is_zero(&db->db_holds));
1945 if (db->db_state != DB_EVICTING) {
1946 ASSERT(db->db_state == DB_CACHED);
1951 mutex_exit(&db->db_mtx);
1958 dbuf_destroy(dmu_buf_impl_t *db)
1960 ASSERT(refcount_is_zero(&db->db_holds));
1962 if (db->db_blkid != DMU_BONUS_BLKID) {
1964 * If this dbuf is still on the dn_dbufs list,
1965 * remove it from that list.
1967 if (db->db_dnode_handle != NULL) {
1972 mutex_enter(&dn->dn_dbufs_mtx);
1973 avl_remove(&dn->dn_dbufs, db);
1974 atomic_dec_32(&dn->dn_dbufs_count);
1975 mutex_exit(&dn->dn_dbufs_mtx);
1978 * Decrementing the dbuf count means that the hold
1979 * corresponding to the removed dbuf is no longer
1980 * discounted in dnode_move(), so the dnode cannot be
1981 * moved until after we release the hold.
1984 db->db_dnode_handle = NULL;
1986 dbuf_hash_remove(db);
1988 db->db_parent = NULL;
1991 ASSERT(db->db.db_data == NULL);
1992 ASSERT(db->db_hash_next == NULL);
1993 ASSERT(db->db_blkptr == NULL);
1994 ASSERT(db->db_data_pending == NULL);
1996 kmem_cache_free(dbuf_cache, db);
1997 arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
2000 typedef struct dbuf_prefetch_arg {
2001 spa_t *dpa_spa; /* The spa to issue the prefetch in. */
2002 zbookmark_phys_t dpa_zb; /* The target block to prefetch. */
2003 int dpa_epbs; /* Entries (blkptr_t's) Per Block Shift. */
2004 int dpa_curlevel; /* The current level that we're reading */
2005 zio_priority_t dpa_prio; /* The priority I/Os should be issued at. */
2006 zio_t *dpa_zio; /* The parent zio_t for all prefetches. */
2007 arc_flags_t dpa_aflags; /* Flags to pass to the final prefetch. */
2008 } dbuf_prefetch_arg_t;
2011 * Actually issue the prefetch read for the block given.
2014 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t *dpa, blkptr_t *bp)
2016 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
2019 arc_flags_t aflags =
2020 dpa->dpa_aflags | ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH;
2022 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp));
2023 ASSERT3U(dpa->dpa_curlevel, ==, dpa->dpa_zb.zb_level);
2024 ASSERT(dpa->dpa_zio != NULL);
2025 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa, bp, NULL, NULL,
2026 dpa->dpa_prio, ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2027 &aflags, &dpa->dpa_zb);
2031 * Called when an indirect block above our prefetch target is read in. This
2032 * will either read in the next indirect block down the tree or issue the actual
2033 * prefetch if the next block down is our target.
2036 dbuf_prefetch_indirect_done(zio_t *zio, arc_buf_t *abuf, void *private)
2038 dbuf_prefetch_arg_t *dpa = private;
2040 ASSERT3S(dpa->dpa_zb.zb_level, <, dpa->dpa_curlevel);
2041 ASSERT3S(dpa->dpa_curlevel, >, 0);
2043 ASSERT3S(BP_GET_LEVEL(zio->io_bp), ==, dpa->dpa_curlevel);
2044 ASSERT3U(BP_GET_LSIZE(zio->io_bp), ==, zio->io_size);
2045 ASSERT3P(zio->io_spa, ==, dpa->dpa_spa);
2048 dpa->dpa_curlevel--;
2050 uint64_t nextblkid = dpa->dpa_zb.zb_blkid >>
2051 (dpa->dpa_epbs * (dpa->dpa_curlevel - dpa->dpa_zb.zb_level));
2052 blkptr_t *bp = ((blkptr_t *)abuf->b_data) +
2053 P2PHASE(nextblkid, 1ULL << dpa->dpa_epbs);
2054 if (BP_IS_HOLE(bp) || (zio != NULL && zio->io_error != 0)) {
2055 kmem_free(dpa, sizeof (*dpa));
2056 } else if (dpa->dpa_curlevel == dpa->dpa_zb.zb_level) {
2057 ASSERT3U(nextblkid, ==, dpa->dpa_zb.zb_blkid);
2058 dbuf_issue_final_prefetch(dpa, bp);
2059 kmem_free(dpa, sizeof (*dpa));
2061 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT;
2062 zbookmark_phys_t zb;
2064 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp));
2066 SET_BOOKMARK(&zb, dpa->dpa_zb.zb_objset,
2067 dpa->dpa_zb.zb_object, dpa->dpa_curlevel, nextblkid);
2069 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa,
2070 bp, dbuf_prefetch_indirect_done, dpa, dpa->dpa_prio,
2071 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2074 (void) arc_buf_remove_ref(abuf, private);
2078 * Issue prefetch reads for the given block on the given level. If the indirect
2079 * blocks above that block are not in memory, we will read them in
2080 * asynchronously. As a result, this call never blocks waiting for a read to
2084 dbuf_prefetch(dnode_t *dn, int64_t level, uint64_t blkid, zio_priority_t prio,
2088 int epbs, nlevels, curlevel;
2091 ASSERT(blkid != DMU_BONUS_BLKID);
2092 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2094 if (blkid > dn->dn_maxblkid)
2097 if (dnode_block_freed(dn, blkid))
2101 * This dnode hasn't been written to disk yet, so there's nothing to
2104 nlevels = dn->dn_phys->dn_nlevels;
2105 if (level >= nlevels || dn->dn_phys->dn_nblkptr == 0)
2108 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2109 if (dn->dn_phys->dn_maxblkid < blkid << (epbs * level))
2112 dmu_buf_impl_t *db = dbuf_find(dn->dn_objset, dn->dn_object,
2115 mutex_exit(&db->db_mtx);
2117 * This dbuf already exists. It is either CACHED, or
2118 * (we assume) about to be read or filled.
2124 * Find the closest ancestor (indirect block) of the target block
2125 * that is present in the cache. In this indirect block, we will
2126 * find the bp that is at curlevel, curblkid.
2130 while (curlevel < nlevels - 1) {
2131 int parent_level = curlevel + 1;
2132 uint64_t parent_blkid = curblkid >> epbs;
2135 if (dbuf_hold_impl(dn, parent_level, parent_blkid,
2136 FALSE, TRUE, FTAG, &db) == 0) {
2137 blkptr_t *bpp = db->db_buf->b_data;
2138 bp = bpp[P2PHASE(curblkid, 1 << epbs)];
2139 dbuf_rele(db, FTAG);
2143 curlevel = parent_level;
2144 curblkid = parent_blkid;
2147 if (curlevel == nlevels - 1) {
2148 /* No cached indirect blocks found. */
2149 ASSERT3U(curblkid, <, dn->dn_phys->dn_nblkptr);
2150 bp = dn->dn_phys->dn_blkptr[curblkid];
2152 if (BP_IS_HOLE(&bp))
2155 ASSERT3U(curlevel, ==, BP_GET_LEVEL(&bp));
2157 zio_t *pio = zio_root(dmu_objset_spa(dn->dn_objset), NULL, NULL,
2160 dbuf_prefetch_arg_t *dpa = kmem_zalloc(sizeof (*dpa), KM_SLEEP);
2161 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
2162 SET_BOOKMARK(&dpa->dpa_zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET,
2163 dn->dn_object, level, blkid);
2164 dpa->dpa_curlevel = curlevel;
2165 dpa->dpa_prio = prio;
2166 dpa->dpa_aflags = aflags;
2167 dpa->dpa_spa = dn->dn_objset->os_spa;
2168 dpa->dpa_epbs = epbs;
2172 * If we have the indirect just above us, no need to do the asynchronous
2173 * prefetch chain; we'll just run the last step ourselves. If we're at
2174 * a higher level, though, we want to issue the prefetches for all the
2175 * indirect blocks asynchronously, so we can go on with whatever we were
2178 if (curlevel == level) {
2179 ASSERT3U(curblkid, ==, blkid);
2180 dbuf_issue_final_prefetch(dpa, &bp);
2181 kmem_free(dpa, sizeof (*dpa));
2183 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT;
2184 zbookmark_phys_t zb;
2186 SET_BOOKMARK(&zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET,
2187 dn->dn_object, curlevel, curblkid);
2188 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa,
2189 &bp, dbuf_prefetch_indirect_done, dpa, prio,
2190 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2194 * We use pio here instead of dpa_zio since it's possible that
2195 * dpa may have already been freed.
2201 * Returns with db_holds incremented, and db_mtx not held.
2202 * Note: dn_struct_rwlock must be held.
2205 dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid,
2206 boolean_t fail_sparse, boolean_t fail_uncached,
2207 void *tag, dmu_buf_impl_t **dbp)
2209 dmu_buf_impl_t *db, *parent = NULL;
2211 ASSERT(blkid != DMU_BONUS_BLKID);
2212 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2213 ASSERT3U(dn->dn_nlevels, >, level);
2217 /* dbuf_find() returns with db_mtx held */
2218 db = dbuf_find(dn->dn_objset, dn->dn_object, level, blkid);
2221 blkptr_t *bp = NULL;
2225 return (SET_ERROR(ENOENT));
2227 ASSERT3P(parent, ==, NULL);
2228 err = dbuf_findbp(dn, level, blkid, fail_sparse, &parent, &bp);
2230 if (err == 0 && bp && BP_IS_HOLE(bp))
2231 err = SET_ERROR(ENOENT);
2234 dbuf_rele(parent, NULL);
2238 if (err && err != ENOENT)
2240 db = dbuf_create(dn, level, blkid, parent, bp);
2243 if (fail_uncached && db->db_state != DB_CACHED) {
2244 mutex_exit(&db->db_mtx);
2245 return (SET_ERROR(ENOENT));
2248 if (db->db_buf && refcount_is_zero(&db->db_holds)) {
2249 arc_buf_add_ref(db->db_buf, db);
2250 if (db->db_buf->b_data == NULL) {
2253 dbuf_rele(parent, NULL);
2258 ASSERT3P(db->db.db_data, ==, db->db_buf->b_data);
2261 ASSERT(db->db_buf == NULL || arc_referenced(db->db_buf));
2264 * If this buffer is currently syncing out, and we are are
2265 * still referencing it from db_data, we need to make a copy
2266 * of it in case we decide we want to dirty it again in this txg.
2268 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
2269 dn->dn_object != DMU_META_DNODE_OBJECT &&
2270 db->db_state == DB_CACHED && db->db_data_pending) {
2271 dbuf_dirty_record_t *dr = db->db_data_pending;
2273 if (dr->dt.dl.dr_data == db->db_buf) {
2274 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
2277 arc_buf_alloc(dn->dn_objset->os_spa,
2278 db->db.db_size, db, type));
2279 bcopy(dr->dt.dl.dr_data->b_data, db->db.db_data,
2284 (void) refcount_add(&db->db_holds, tag);
2286 mutex_exit(&db->db_mtx);
2288 /* NOTE: we can't rele the parent until after we drop the db_mtx */
2290 dbuf_rele(parent, NULL);
2292 ASSERT3P(DB_DNODE(db), ==, dn);
2293 ASSERT3U(db->db_blkid, ==, blkid);
2294 ASSERT3U(db->db_level, ==, level);
2301 dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag)
2303 return (dbuf_hold_level(dn, 0, blkid, tag));
2307 dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag)
2310 int err = dbuf_hold_impl(dn, level, blkid, FALSE, FALSE, tag, &db);
2311 return (err ? NULL : db);
2315 dbuf_create_bonus(dnode_t *dn)
2317 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
2319 ASSERT(dn->dn_bonus == NULL);
2320 dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL);
2324 dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx)
2326 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2329 if (db->db_blkid != DMU_SPILL_BLKID)
2330 return (SET_ERROR(ENOTSUP));
2332 blksz = SPA_MINBLOCKSIZE;
2333 ASSERT3U(blksz, <=, spa_maxblocksize(dmu_objset_spa(db->db_objset)));
2334 blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE);
2338 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2339 dbuf_new_size(db, blksz, tx);
2340 rw_exit(&dn->dn_struct_rwlock);
2347 dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx)
2349 dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx);
2352 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2354 dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
2356 int64_t holds = refcount_add(&db->db_holds, tag);
2360 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
2362 dbuf_try_add_ref(dmu_buf_t *db_fake, objset_t *os, uint64_t obj, uint64_t blkid,
2365 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2366 dmu_buf_impl_t *found_db;
2367 boolean_t result = B_FALSE;
2369 if (db->db_blkid == DMU_BONUS_BLKID)
2370 found_db = dbuf_find_bonus(os, obj);
2372 found_db = dbuf_find(os, obj, 0, blkid);
2374 if (found_db != NULL) {
2375 if (db == found_db && dbuf_refcount(db) > db->db_dirtycnt) {
2376 (void) refcount_add(&db->db_holds, tag);
2379 mutex_exit(&db->db_mtx);
2385 * If you call dbuf_rele() you had better not be referencing the dnode handle
2386 * unless you have some other direct or indirect hold on the dnode. (An indirect
2387 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2388 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2389 * dnode's parent dbuf evicting its dnode handles.
2392 dbuf_rele(dmu_buf_impl_t *db, void *tag)
2394 mutex_enter(&db->db_mtx);
2395 dbuf_rele_and_unlock(db, tag);
2399 dmu_buf_rele(dmu_buf_t *db, void *tag)
2401 dbuf_rele((dmu_buf_impl_t *)db, tag);
2405 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2406 * db_dirtycnt and db_holds to be updated atomically.
2409 dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag)
2413 ASSERT(MUTEX_HELD(&db->db_mtx));
2417 * Remove the reference to the dbuf before removing its hold on the
2418 * dnode so we can guarantee in dnode_move() that a referenced bonus
2419 * buffer has a corresponding dnode hold.
2421 holds = refcount_remove(&db->db_holds, tag);
2425 * We can't freeze indirects if there is a possibility that they
2426 * may be modified in the current syncing context.
2428 if (db->db_buf && holds == (db->db_level == 0 ? db->db_dirtycnt : 0))
2429 arc_buf_freeze(db->db_buf);
2431 if (holds == db->db_dirtycnt &&
2432 db->db_level == 0 && db->db_user_immediate_evict)
2433 dbuf_evict_user(db);
2436 if (db->db_blkid == DMU_BONUS_BLKID) {
2438 boolean_t evict_dbuf = db->db_pending_evict;
2441 * If the dnode moves here, we cannot cross this
2442 * barrier until the move completes.
2447 atomic_dec_32(&dn->dn_dbufs_count);
2450 * Decrementing the dbuf count means that the bonus
2451 * buffer's dnode hold is no longer discounted in
2452 * dnode_move(). The dnode cannot move until after
2453 * the dnode_rele() below.
2458 * Do not reference db after its lock is dropped.
2459 * Another thread may evict it.
2461 mutex_exit(&db->db_mtx);
2464 dnode_evict_bonus(dn);
2467 } else if (db->db_buf == NULL) {
2469 * This is a special case: we never associated this
2470 * dbuf with any data allocated from the ARC.
2472 ASSERT(db->db_state == DB_UNCACHED ||
2473 db->db_state == DB_NOFILL);
2475 } else if (arc_released(db->db_buf)) {
2476 arc_buf_t *buf = db->db_buf;
2478 * This dbuf has anonymous data associated with it.
2480 dbuf_clear_data(db);
2481 VERIFY(arc_buf_remove_ref(buf, db));
2484 VERIFY(!arc_buf_remove_ref(db->db_buf, db));
2487 * A dbuf will be eligible for eviction if either the
2488 * 'primarycache' property is set or a duplicate
2489 * copy of this buffer is already cached in the arc.
2491 * In the case of the 'primarycache' a buffer
2492 * is considered for eviction if it matches the
2493 * criteria set in the property.
2495 * To decide if our buffer is considered a
2496 * duplicate, we must call into the arc to determine
2497 * if multiple buffers are referencing the same
2498 * block on-disk. If so, then we simply evict
2501 if (!DBUF_IS_CACHEABLE(db)) {
2502 if (db->db_blkptr != NULL &&
2503 !BP_IS_HOLE(db->db_blkptr) &&
2504 !BP_IS_EMBEDDED(db->db_blkptr)) {
2506 dmu_objset_spa(db->db_objset);
2507 blkptr_t bp = *db->db_blkptr;
2509 arc_freed(spa, &bp);
2513 } else if (db->db_pending_evict ||
2514 arc_buf_eviction_needed(db->db_buf)) {
2517 mutex_exit(&db->db_mtx);
2521 mutex_exit(&db->db_mtx);
2525 #pragma weak dmu_buf_refcount = dbuf_refcount
2527 dbuf_refcount(dmu_buf_impl_t *db)
2529 return (refcount_count(&db->db_holds));
2533 dmu_buf_replace_user(dmu_buf_t *db_fake, dmu_buf_user_t *old_user,
2534 dmu_buf_user_t *new_user)
2536 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2538 mutex_enter(&db->db_mtx);
2539 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2540 if (db->db_user == old_user)
2541 db->db_user = new_user;
2543 old_user = db->db_user;
2544 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2545 mutex_exit(&db->db_mtx);
2551 dmu_buf_set_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2553 return (dmu_buf_replace_user(db_fake, NULL, user));
2557 dmu_buf_set_user_ie(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2559 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2561 db->db_user_immediate_evict = TRUE;
2562 return (dmu_buf_set_user(db_fake, user));
2566 dmu_buf_remove_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
2568 return (dmu_buf_replace_user(db_fake, user, NULL));
2572 dmu_buf_get_user(dmu_buf_t *db_fake)
2574 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2576 dbuf_verify_user(db, DBVU_NOT_EVICTING);
2577 return (db->db_user);
2581 dmu_buf_user_evict_wait()
2583 taskq_wait(dbu_evict_taskq);
2587 dmu_buf_freeable(dmu_buf_t *dbuf)
2589 boolean_t res = B_FALSE;
2590 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
2593 res = dsl_dataset_block_freeable(db->db_objset->os_dsl_dataset,
2594 db->db_blkptr, db->db_blkptr->blk_birth);
2600 dmu_buf_get_blkptr(dmu_buf_t *db)
2602 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2603 return (dbi->db_blkptr);
2607 dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db)
2609 /* ASSERT(dmu_tx_is_syncing(tx) */
2610 ASSERT(MUTEX_HELD(&db->db_mtx));
2612 if (db->db_blkptr != NULL)
2615 if (db->db_blkid == DMU_SPILL_BLKID) {
2616 db->db_blkptr = &dn->dn_phys->dn_spill;
2617 BP_ZERO(db->db_blkptr);
2620 if (db->db_level == dn->dn_phys->dn_nlevels-1) {
2622 * This buffer was allocated at a time when there was
2623 * no available blkptrs from the dnode, or it was
2624 * inappropriate to hook it in (i.e., nlevels mis-match).
2626 ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr);
2627 ASSERT(db->db_parent == NULL);
2628 db->db_parent = dn->dn_dbuf;
2629 db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
2632 dmu_buf_impl_t *parent = db->db_parent;
2633 int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2635 ASSERT(dn->dn_phys->dn_nlevels > 1);
2636 if (parent == NULL) {
2637 mutex_exit(&db->db_mtx);
2638 rw_enter(&dn->dn_struct_rwlock, RW_READER);
2639 parent = dbuf_hold_level(dn, db->db_level + 1,
2640 db->db_blkid >> epbs, db);
2641 rw_exit(&dn->dn_struct_rwlock);
2642 mutex_enter(&db->db_mtx);
2643 db->db_parent = parent;
2645 db->db_blkptr = (blkptr_t *)parent->db.db_data +
2646 (db->db_blkid & ((1ULL << epbs) - 1));
2652 dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2654 dmu_buf_impl_t *db = dr->dr_dbuf;
2658 ASSERT(dmu_tx_is_syncing(tx));
2660 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2662 mutex_enter(&db->db_mtx);
2664 ASSERT(db->db_level > 0);
2667 /* Read the block if it hasn't been read yet. */
2668 if (db->db_buf == NULL) {
2669 mutex_exit(&db->db_mtx);
2670 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
2671 mutex_enter(&db->db_mtx);
2673 ASSERT3U(db->db_state, ==, DB_CACHED);
2674 ASSERT(db->db_buf != NULL);
2678 /* Indirect block size must match what the dnode thinks it is. */
2679 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2680 dbuf_check_blkptr(dn, db);
2683 /* Provide the pending dirty record to child dbufs */
2684 db->db_data_pending = dr;
2686 mutex_exit(&db->db_mtx);
2687 dbuf_write(dr, db->db_buf, tx);
2690 mutex_enter(&dr->dt.di.dr_mtx);
2691 dbuf_sync_list(&dr->dt.di.dr_children, db->db_level - 1, tx);
2692 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
2693 mutex_exit(&dr->dt.di.dr_mtx);
2698 dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2700 arc_buf_t **datap = &dr->dt.dl.dr_data;
2701 dmu_buf_impl_t *db = dr->dr_dbuf;
2704 uint64_t txg = tx->tx_txg;
2706 ASSERT(dmu_tx_is_syncing(tx));
2708 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2710 mutex_enter(&db->db_mtx);
2712 * To be synced, we must be dirtied. But we
2713 * might have been freed after the dirty.
2715 if (db->db_state == DB_UNCACHED) {
2716 /* This buffer has been freed since it was dirtied */
2717 ASSERT(db->db.db_data == NULL);
2718 } else if (db->db_state == DB_FILL) {
2719 /* This buffer was freed and is now being re-filled */
2720 ASSERT(db->db.db_data != dr->dt.dl.dr_data);
2722 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL);
2729 if (db->db_blkid == DMU_SPILL_BLKID) {
2730 mutex_enter(&dn->dn_mtx);
2731 dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR;
2732 mutex_exit(&dn->dn_mtx);
2736 * If this is a bonus buffer, simply copy the bonus data into the
2737 * dnode. It will be written out when the dnode is synced (and it
2738 * will be synced, since it must have been dirty for dbuf_sync to
2741 if (db->db_blkid == DMU_BONUS_BLKID) {
2742 dbuf_dirty_record_t **drp;
2744 ASSERT(*datap != NULL);
2745 ASSERT0(db->db_level);
2746 ASSERT3U(dn->dn_phys->dn_bonuslen, <=, DN_MAX_BONUSLEN);
2747 bcopy(*datap, DN_BONUS(dn->dn_phys), dn->dn_phys->dn_bonuslen);
2750 if (*datap != db->db.db_data) {
2751 zio_buf_free(*datap, DN_MAX_BONUSLEN);
2752 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
2754 db->db_data_pending = NULL;
2755 drp = &db->db_last_dirty;
2757 drp = &(*drp)->dr_next;
2758 ASSERT(dr->dr_next == NULL);
2759 ASSERT(dr->dr_dbuf == db);
2761 if (dr->dr_dbuf->db_level != 0) {
2762 list_destroy(&dr->dt.di.dr_children);
2763 mutex_destroy(&dr->dt.di.dr_mtx);
2765 kmem_free(dr, sizeof (dbuf_dirty_record_t));
2766 ASSERT(db->db_dirtycnt > 0);
2767 db->db_dirtycnt -= 1;
2768 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
2775 * This function may have dropped the db_mtx lock allowing a dmu_sync
2776 * operation to sneak in. As a result, we need to ensure that we
2777 * don't check the dr_override_state until we have returned from
2778 * dbuf_check_blkptr.
2780 dbuf_check_blkptr(dn, db);
2783 * If this buffer is in the middle of an immediate write,
2784 * wait for the synchronous IO to complete.
2786 while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
2787 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
2788 cv_wait(&db->db_changed, &db->db_mtx);
2789 ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN);
2792 if (db->db_state != DB_NOFILL &&
2793 dn->dn_object != DMU_META_DNODE_OBJECT &&
2794 refcount_count(&db->db_holds) > 1 &&
2795 dr->dt.dl.dr_override_state != DR_OVERRIDDEN &&
2796 *datap == db->db_buf) {
2798 * If this buffer is currently "in use" (i.e., there
2799 * are active holds and db_data still references it),
2800 * then make a copy before we start the write so that
2801 * any modifications from the open txg will not leak
2804 * NOTE: this copy does not need to be made for
2805 * objects only modified in the syncing context (e.g.
2806 * DNONE_DNODE blocks).
2808 int blksz = arc_buf_size(*datap);
2809 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
2810 *datap = arc_buf_alloc(os->os_spa, blksz, db, type);
2811 bcopy(db->db.db_data, (*datap)->b_data, blksz);
2813 db->db_data_pending = dr;
2815 mutex_exit(&db->db_mtx);
2817 dbuf_write(dr, *datap, tx);
2819 ASSERT(!list_link_active(&dr->dr_dirty_node));
2820 if (dn->dn_object == DMU_META_DNODE_OBJECT) {
2821 list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr);
2825 * Although zio_nowait() does not "wait for an IO", it does
2826 * initiate the IO. If this is an empty write it seems plausible
2827 * that the IO could actually be completed before the nowait
2828 * returns. We need to DB_DNODE_EXIT() first in case
2829 * zio_nowait() invalidates the dbuf.
2832 zio_nowait(dr->dr_zio);
2837 dbuf_sync_list(list_t *list, int level, dmu_tx_t *tx)
2839 dbuf_dirty_record_t *dr;
2841 while (dr = list_head(list)) {
2842 if (dr->dr_zio != NULL) {
2844 * If we find an already initialized zio then we
2845 * are processing the meta-dnode, and we have finished.
2846 * The dbufs for all dnodes are put back on the list
2847 * during processing, so that we can zio_wait()
2848 * these IOs after initiating all child IOs.
2850 ASSERT3U(dr->dr_dbuf->db.db_object, ==,
2851 DMU_META_DNODE_OBJECT);
2854 if (dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
2855 dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
2856 VERIFY3U(dr->dr_dbuf->db_level, ==, level);
2858 list_remove(list, dr);
2859 if (dr->dr_dbuf->db_level > 0)
2860 dbuf_sync_indirect(dr, tx);
2862 dbuf_sync_leaf(dr, tx);
2868 dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
2870 dmu_buf_impl_t *db = vdb;
2872 blkptr_t *bp = zio->io_bp;
2873 blkptr_t *bp_orig = &zio->io_bp_orig;
2874 spa_t *spa = zio->io_spa;
2879 ASSERT3P(db->db_blkptr, ==, bp);
2883 delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig);
2884 dnode_diduse_space(dn, delta - zio->io_prev_space_delta);
2885 zio->io_prev_space_delta = delta;
2887 if (bp->blk_birth != 0) {
2888 ASSERT((db->db_blkid != DMU_SPILL_BLKID &&
2889 BP_GET_TYPE(bp) == dn->dn_type) ||
2890 (db->db_blkid == DMU_SPILL_BLKID &&
2891 BP_GET_TYPE(bp) == dn->dn_bonustype) ||
2892 BP_IS_EMBEDDED(bp));
2893 ASSERT(BP_GET_LEVEL(bp) == db->db_level);
2896 mutex_enter(&db->db_mtx);
2899 if (db->db_blkid == DMU_SPILL_BLKID) {
2900 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
2901 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
2902 db->db_blkptr == &dn->dn_phys->dn_spill);
2906 if (db->db_level == 0) {
2907 mutex_enter(&dn->dn_mtx);
2908 if (db->db_blkid > dn->dn_phys->dn_maxblkid &&
2909 db->db_blkid != DMU_SPILL_BLKID)
2910 dn->dn_phys->dn_maxblkid = db->db_blkid;
2911 mutex_exit(&dn->dn_mtx);
2913 if (dn->dn_type == DMU_OT_DNODE) {
2914 dnode_phys_t *dnp = db->db.db_data;
2915 for (i = db->db.db_size >> DNODE_SHIFT; i > 0;
2917 if (dnp->dn_type != DMU_OT_NONE)
2921 if (BP_IS_HOLE(bp)) {
2928 blkptr_t *ibp = db->db.db_data;
2929 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2930 for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) {
2931 if (BP_IS_HOLE(ibp))
2933 fill += BP_GET_FILL(ibp);
2938 if (!BP_IS_EMBEDDED(bp))
2939 bp->blk_fill = fill;
2941 mutex_exit(&db->db_mtx);
2945 * The SPA will call this callback several times for each zio - once
2946 * for every physical child i/o (zio->io_phys_children times). This
2947 * allows the DMU to monitor the progress of each logical i/o. For example,
2948 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
2949 * block. There may be a long delay before all copies/fragments are completed,
2950 * so this callback allows us to retire dirty space gradually, as the physical
2955 dbuf_write_physdone(zio_t *zio, arc_buf_t *buf, void *arg)
2957 dmu_buf_impl_t *db = arg;
2958 objset_t *os = db->db_objset;
2959 dsl_pool_t *dp = dmu_objset_pool(os);
2960 dbuf_dirty_record_t *dr;
2963 dr = db->db_data_pending;
2964 ASSERT3U(dr->dr_txg, ==, zio->io_txg);
2967 * The callback will be called io_phys_children times. Retire one
2968 * portion of our dirty space each time we are called. Any rounding
2969 * error will be cleaned up by dsl_pool_sync()'s call to
2970 * dsl_pool_undirty_space().
2972 delta = dr->dr_accounted / zio->io_phys_children;
2973 dsl_pool_undirty_space(dp, delta, zio->io_txg);
2978 dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
2980 dmu_buf_impl_t *db = vdb;
2981 blkptr_t *bp_orig = &zio->io_bp_orig;
2982 blkptr_t *bp = db->db_blkptr;
2983 objset_t *os = db->db_objset;
2984 dmu_tx_t *tx = os->os_synctx;
2985 dbuf_dirty_record_t **drp, *dr;
2987 ASSERT0(zio->io_error);
2988 ASSERT(db->db_blkptr == bp);
2991 * For nopwrites and rewrites we ensure that the bp matches our
2992 * original and bypass all the accounting.
2994 if (zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)) {
2995 ASSERT(BP_EQUAL(bp, bp_orig));
2997 dsl_dataset_t *ds = os->os_dsl_dataset;
2998 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
2999 dsl_dataset_block_born(ds, bp, tx);
3002 mutex_enter(&db->db_mtx);
3006 drp = &db->db_last_dirty;
3007 while ((dr = *drp) != db->db_data_pending)
3009 ASSERT(!list_link_active(&dr->dr_dirty_node));
3010 ASSERT(dr->dr_dbuf == db);
3011 ASSERT(dr->dr_next == NULL);
3015 if (db->db_blkid == DMU_SPILL_BLKID) {
3020 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
3021 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
3022 db->db_blkptr == &dn->dn_phys->dn_spill);
3027 if (db->db_level == 0) {
3028 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
3029 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
3030 if (db->db_state != DB_NOFILL) {
3031 if (dr->dt.dl.dr_data != db->db_buf)
3032 VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data,
3034 else if (!arc_released(db->db_buf))
3035 arc_set_callback(db->db_buf, dbuf_do_evict, db);
3042 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
3043 ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
3044 if (!BP_IS_HOLE(db->db_blkptr)) {
3046 dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
3047 ASSERT3U(db->db_blkid, <=,
3048 dn->dn_phys->dn_maxblkid >> (db->db_level * epbs));
3049 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
3051 if (!arc_released(db->db_buf))
3052 arc_set_callback(db->db_buf, dbuf_do_evict, db);
3055 mutex_destroy(&dr->dt.di.dr_mtx);
3056 list_destroy(&dr->dt.di.dr_children);
3058 kmem_free(dr, sizeof (dbuf_dirty_record_t));
3060 cv_broadcast(&db->db_changed);
3061 ASSERT(db->db_dirtycnt > 0);
3062 db->db_dirtycnt -= 1;
3063 db->db_data_pending = NULL;
3064 dbuf_rele_and_unlock(db, (void *)(uintptr_t)tx->tx_txg);
3068 dbuf_write_nofill_ready(zio_t *zio)
3070 dbuf_write_ready(zio, NULL, zio->io_private);
3074 dbuf_write_nofill_done(zio_t *zio)
3076 dbuf_write_done(zio, NULL, zio->io_private);
3080 dbuf_write_override_ready(zio_t *zio)
3082 dbuf_dirty_record_t *dr = zio->io_private;
3083 dmu_buf_impl_t *db = dr->dr_dbuf;
3085 dbuf_write_ready(zio, NULL, db);
3089 dbuf_write_override_done(zio_t *zio)
3091 dbuf_dirty_record_t *dr = zio->io_private;
3092 dmu_buf_impl_t *db = dr->dr_dbuf;
3093 blkptr_t *obp = &dr->dt.dl.dr_overridden_by;
3095 mutex_enter(&db->db_mtx);
3096 if (!BP_EQUAL(zio->io_bp, obp)) {
3097 if (!BP_IS_HOLE(obp))
3098 dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp);
3099 arc_release(dr->dt.dl.dr_data, db);
3101 mutex_exit(&db->db_mtx);
3103 dbuf_write_done(zio, NULL, db);
3106 /* Issue I/O to commit a dirty buffer to disk. */
3108 dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx)
3110 dmu_buf_impl_t *db = dr->dr_dbuf;
3113 dmu_buf_impl_t *parent = db->db_parent;
3114 uint64_t txg = tx->tx_txg;
3115 zbookmark_phys_t zb;
3124 if (db->db_state != DB_NOFILL) {
3125 if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) {
3127 * Private object buffers are released here rather
3128 * than in dbuf_dirty() since they are only modified
3129 * in the syncing context and we don't want the
3130 * overhead of making multiple copies of the data.
3132 if (BP_IS_HOLE(db->db_blkptr)) {
3135 dbuf_release_bp(db);
3140 if (parent != dn->dn_dbuf) {
3141 /* Our parent is an indirect block. */
3142 /* We have a dirty parent that has been scheduled for write. */
3143 ASSERT(parent && parent->db_data_pending);
3144 /* Our parent's buffer is one level closer to the dnode. */
3145 ASSERT(db->db_level == parent->db_level-1);
3147 * We're about to modify our parent's db_data by modifying
3148 * our block pointer, so the parent must be released.
3150 ASSERT(arc_released(parent->db_buf));
3151 zio = parent->db_data_pending->dr_zio;
3153 /* Our parent is the dnode itself. */
3154 ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 &&
3155 db->db_blkid != DMU_SPILL_BLKID) ||
3156 (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0));
3157 if (db->db_blkid != DMU_SPILL_BLKID)
3158 ASSERT3P(db->db_blkptr, ==,
3159 &dn->dn_phys->dn_blkptr[db->db_blkid]);
3163 ASSERT(db->db_level == 0 || data == db->db_buf);
3164 ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
3167 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
3168 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
3169 db->db.db_object, db->db_level, db->db_blkid);
3171 if (db->db_blkid == DMU_SPILL_BLKID)
3173 wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
3175 dmu_write_policy(os, dn, db->db_level, wp_flag, &zp);
3178 if (db->db_level == 0 &&
3179 dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
3181 * The BP for this block has been provided by open context
3182 * (by dmu_sync() or dmu_buf_write_embedded()).
3184 void *contents = (data != NULL) ? data->b_data : NULL;
3186 dr->dr_zio = zio_write(zio, os->os_spa, txg,
3187 db->db_blkptr, contents, db->db.db_size, &zp,
3188 dbuf_write_override_ready, NULL, dbuf_write_override_done,
3189 dr, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
3190 mutex_enter(&db->db_mtx);
3191 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
3192 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by,
3193 dr->dt.dl.dr_copies, dr->dt.dl.dr_nopwrite);
3194 mutex_exit(&db->db_mtx);
3195 } else if (db->db_state == DB_NOFILL) {
3196 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF ||
3197 zp.zp_checksum == ZIO_CHECKSUM_NOPARITY);
3198 dr->dr_zio = zio_write(zio, os->os_spa, txg,
3199 db->db_blkptr, NULL, db->db.db_size, &zp,
3200 dbuf_write_nofill_ready, NULL, dbuf_write_nofill_done, db,
3201 ZIO_PRIORITY_ASYNC_WRITE,
3202 ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb);
3204 ASSERT(arc_released(data));
3205 dr->dr_zio = arc_write(zio, os->os_spa, txg,
3206 db->db_blkptr, data, DBUF_IS_L2CACHEABLE(db),
3207 DBUF_IS_L2COMPRESSIBLE(db), &zp, dbuf_write_ready,
3208 dbuf_write_physdone, dbuf_write_done, db,
3209 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);