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, 2014 by Delphix. All rights reserved.
25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
29 #include <sys/zfs_context.h>
31 #include <sys/dmu_send.h>
32 #include <sys/dmu_impl.h>
34 #include <sys/dmu_objset.h>
35 #include <sys/dsl_dataset.h>
36 #include <sys/dsl_dir.h>
37 #include <sys/dmu_tx.h>
40 #include <sys/dmu_zfetch.h>
42 #include <sys/sa_impl.h>
43 #include <sys/zfeature.h>
44 #include <sys/blkptr.h>
45 #include <sys/range_tree.h>
48 * Number of times that zfs_free_range() took the slow path while doing
49 * a zfs receive. A nonzero value indicates a potential performance problem.
51 uint64_t zfs_free_range_recv_miss;
53 static void dbuf_destroy(dmu_buf_impl_t *db);
54 static boolean_t dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
55 static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx);
58 * Global data structures and functions for the dbuf cache.
60 static kmem_cache_t *dbuf_cache;
64 dbuf_cons(void *vdb, void *unused, int kmflag)
66 dmu_buf_impl_t *db = vdb;
67 bzero(db, sizeof (dmu_buf_impl_t));
69 mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL);
70 cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL);
71 refcount_create(&db->db_holds);
73 #if defined(illumos) || !defined(_KERNEL)
74 db->db_creation = gethrtime();
76 db->db_creation = cpu_ticks() ^ ((uint64_t)CPU_SEQID << 48);
84 dbuf_dest(void *vdb, void *unused)
86 dmu_buf_impl_t *db = vdb;
87 mutex_destroy(&db->db_mtx);
88 cv_destroy(&db->db_changed);
89 refcount_destroy(&db->db_holds);
93 * dbuf hash table routines
95 static dbuf_hash_table_t dbuf_hash_table;
97 static uint64_t dbuf_hash_count;
100 dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid)
102 uintptr_t osv = (uintptr_t)os;
103 uint64_t crc = -1ULL;
105 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
106 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (lvl)) & 0xFF];
107 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF];
108 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF];
109 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF];
110 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 0)) & 0xFF];
111 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 8)) & 0xFF];
113 crc ^= (osv>>14) ^ (obj>>16) ^ (blkid>>16);
118 #define DBUF_HASH(os, obj, level, blkid) dbuf_hash(os, obj, level, blkid);
120 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
121 ((dbuf)->db.db_object == (obj) && \
122 (dbuf)->db_objset == (os) && \
123 (dbuf)->db_level == (level) && \
124 (dbuf)->db_blkid == (blkid))
127 dbuf_find(dnode_t *dn, uint8_t level, uint64_t blkid)
129 dbuf_hash_table_t *h = &dbuf_hash_table;
130 objset_t *os = dn->dn_objset;
131 uint64_t obj = dn->dn_object;
132 uint64_t hv = DBUF_HASH(os, obj, level, blkid);
133 uint64_t idx = hv & h->hash_table_mask;
136 mutex_enter(DBUF_HASH_MUTEX(h, idx));
137 for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) {
138 if (DBUF_EQUAL(db, os, obj, level, blkid)) {
139 mutex_enter(&db->db_mtx);
140 if (db->db_state != DB_EVICTING) {
141 mutex_exit(DBUF_HASH_MUTEX(h, idx));
144 mutex_exit(&db->db_mtx);
147 mutex_exit(DBUF_HASH_MUTEX(h, idx));
152 * Insert an entry into the hash table. If there is already an element
153 * equal to elem in the hash table, then the already existing element
154 * will be returned and the new element will not be inserted.
155 * Otherwise returns NULL.
157 static dmu_buf_impl_t *
158 dbuf_hash_insert(dmu_buf_impl_t *db)
160 dbuf_hash_table_t *h = &dbuf_hash_table;
161 objset_t *os = db->db_objset;
162 uint64_t obj = db->db.db_object;
163 int level = db->db_level;
164 uint64_t blkid = db->db_blkid;
165 uint64_t hv = DBUF_HASH(os, obj, level, blkid);
166 uint64_t idx = hv & h->hash_table_mask;
169 mutex_enter(DBUF_HASH_MUTEX(h, idx));
170 for (dbf = h->hash_table[idx]; dbf != NULL; dbf = dbf->db_hash_next) {
171 if (DBUF_EQUAL(dbf, os, obj, level, blkid)) {
172 mutex_enter(&dbf->db_mtx);
173 if (dbf->db_state != DB_EVICTING) {
174 mutex_exit(DBUF_HASH_MUTEX(h, idx));
177 mutex_exit(&dbf->db_mtx);
181 mutex_enter(&db->db_mtx);
182 db->db_hash_next = h->hash_table[idx];
183 h->hash_table[idx] = db;
184 mutex_exit(DBUF_HASH_MUTEX(h, idx));
185 atomic_add_64(&dbuf_hash_count, 1);
191 * Remove an entry from the hash table. It must be in the EVICTING state.
194 dbuf_hash_remove(dmu_buf_impl_t *db)
196 dbuf_hash_table_t *h = &dbuf_hash_table;
197 uint64_t hv = DBUF_HASH(db->db_objset, db->db.db_object,
198 db->db_level, db->db_blkid);
199 uint64_t idx = hv & h->hash_table_mask;
200 dmu_buf_impl_t *dbf, **dbp;
203 * We musn't hold db_mtx to maintain lock ordering:
204 * DBUF_HASH_MUTEX > db_mtx.
206 ASSERT(refcount_is_zero(&db->db_holds));
207 ASSERT(db->db_state == DB_EVICTING);
208 ASSERT(!MUTEX_HELD(&db->db_mtx));
210 mutex_enter(DBUF_HASH_MUTEX(h, idx));
211 dbp = &h->hash_table[idx];
212 while ((dbf = *dbp) != db) {
213 dbp = &dbf->db_hash_next;
216 *dbp = db->db_hash_next;
217 db->db_hash_next = NULL;
218 mutex_exit(DBUF_HASH_MUTEX(h, idx));
219 atomic_add_64(&dbuf_hash_count, -1);
222 static arc_evict_func_t dbuf_do_evict;
225 dbuf_evict_user(dmu_buf_impl_t *db)
227 ASSERT(MUTEX_HELD(&db->db_mtx));
229 if (db->db_level != 0 || db->db_evict_func == NULL)
232 if (db->db_user_data_ptr_ptr)
233 *db->db_user_data_ptr_ptr = db->db.db_data;
234 db->db_evict_func(&db->db, db->db_user_ptr);
235 db->db_user_ptr = NULL;
236 db->db_user_data_ptr_ptr = NULL;
237 db->db_evict_func = NULL;
241 dbuf_is_metadata(dmu_buf_impl_t *db)
243 if (db->db_level > 0) {
246 boolean_t is_metadata;
249 is_metadata = DMU_OT_IS_METADATA(DB_DNODE(db)->dn_type);
252 return (is_metadata);
257 dbuf_evict(dmu_buf_impl_t *db)
259 ASSERT(MUTEX_HELD(&db->db_mtx));
260 ASSERT(db->db_buf == NULL);
261 ASSERT(db->db_data_pending == NULL);
270 uint64_t hsize = 1ULL << 16;
271 dbuf_hash_table_t *h = &dbuf_hash_table;
275 * The hash table is big enough to fill all of physical memory
276 * with an average 4K block size. The table will take up
277 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
279 while (hsize * 4096 < (uint64_t)physmem * PAGESIZE)
283 h->hash_table_mask = hsize - 1;
284 h->hash_table = kmem_zalloc(hsize * sizeof (void *), KM_NOSLEEP);
285 if (h->hash_table == NULL) {
286 /* XXX - we should really return an error instead of assert */
287 ASSERT(hsize > (1ULL << 10));
292 dbuf_cache = kmem_cache_create("dmu_buf_impl_t",
293 sizeof (dmu_buf_impl_t),
294 0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0);
296 for (i = 0; i < DBUF_MUTEXES; i++)
297 mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL);
303 dbuf_hash_table_t *h = &dbuf_hash_table;
306 for (i = 0; i < DBUF_MUTEXES; i++)
307 mutex_destroy(&h->hash_mutexes[i]);
308 kmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
309 kmem_cache_destroy(dbuf_cache);
318 dbuf_verify(dmu_buf_impl_t *db)
321 dbuf_dirty_record_t *dr;
323 ASSERT(MUTEX_HELD(&db->db_mtx));
325 if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY))
328 ASSERT(db->db_objset != NULL);
332 ASSERT(db->db_parent == NULL);
333 ASSERT(db->db_blkptr == NULL);
335 ASSERT3U(db->db.db_object, ==, dn->dn_object);
336 ASSERT3P(db->db_objset, ==, dn->dn_objset);
337 ASSERT3U(db->db_level, <, dn->dn_nlevels);
338 ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
339 db->db_blkid == DMU_SPILL_BLKID ||
340 !avl_is_empty(&dn->dn_dbufs));
342 if (db->db_blkid == DMU_BONUS_BLKID) {
344 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
345 ASSERT3U(db->db.db_offset, ==, DMU_BONUS_BLKID);
346 } else if (db->db_blkid == DMU_SPILL_BLKID) {
348 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
349 ASSERT0(db->db.db_offset);
351 ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size);
354 for (dr = db->db_data_pending; dr != NULL; dr = dr->dr_next)
355 ASSERT(dr->dr_dbuf == db);
357 for (dr = db->db_last_dirty; dr != NULL; dr = dr->dr_next)
358 ASSERT(dr->dr_dbuf == db);
361 * We can't assert that db_size matches dn_datablksz because it
362 * can be momentarily different when another thread is doing
365 if (db->db_level == 0 && db->db.db_object == DMU_META_DNODE_OBJECT) {
366 dr = db->db_data_pending;
368 * It should only be modified in syncing context, so
369 * make sure we only have one copy of the data.
371 ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf);
374 /* verify db->db_blkptr */
376 if (db->db_parent == dn->dn_dbuf) {
377 /* db is pointed to by the dnode */
378 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
379 if (DMU_OBJECT_IS_SPECIAL(db->db.db_object))
380 ASSERT(db->db_parent == NULL);
382 ASSERT(db->db_parent != NULL);
383 if (db->db_blkid != DMU_SPILL_BLKID)
384 ASSERT3P(db->db_blkptr, ==,
385 &dn->dn_phys->dn_blkptr[db->db_blkid]);
387 /* db is pointed to by an indirect block */
388 int epb = db->db_parent->db.db_size >> SPA_BLKPTRSHIFT;
389 ASSERT3U(db->db_parent->db_level, ==, db->db_level+1);
390 ASSERT3U(db->db_parent->db.db_object, ==,
393 * dnode_grow_indblksz() can make this fail if we don't
394 * have the struct_rwlock. XXX indblksz no longer
395 * grows. safe to do this now?
397 if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
398 ASSERT3P(db->db_blkptr, ==,
399 ((blkptr_t *)db->db_parent->db.db_data +
400 db->db_blkid % epb));
404 if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) &&
405 (db->db_buf == NULL || db->db_buf->b_data) &&
406 db->db.db_data && db->db_blkid != DMU_BONUS_BLKID &&
407 db->db_state != DB_FILL && !dn->dn_free_txg) {
409 * If the blkptr isn't set but they have nonzero data,
410 * it had better be dirty, otherwise we'll lose that
411 * data when we evict this buffer.
413 if (db->db_dirtycnt == 0) {
414 uint64_t *buf = db->db.db_data;
417 for (i = 0; i < db->db.db_size >> 3; i++) {
427 dbuf_update_data(dmu_buf_impl_t *db)
429 ASSERT(MUTEX_HELD(&db->db_mtx));
430 if (db->db_level == 0 && db->db_user_data_ptr_ptr) {
431 ASSERT(!refcount_is_zero(&db->db_holds));
432 *db->db_user_data_ptr_ptr = db->db.db_data;
437 dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf)
439 ASSERT(MUTEX_HELD(&db->db_mtx));
442 ASSERT(buf->b_data != NULL);
443 db->db.db_data = buf->b_data;
444 if (!arc_released(buf))
445 arc_set_callback(buf, dbuf_do_evict, db);
446 dbuf_update_data(db);
449 db->db.db_data = NULL;
450 if (db->db_state != DB_NOFILL)
451 db->db_state = DB_UNCACHED;
456 * Loan out an arc_buf for read. Return the loaned arc_buf.
459 dbuf_loan_arcbuf(dmu_buf_impl_t *db)
463 mutex_enter(&db->db_mtx);
464 if (arc_released(db->db_buf) || refcount_count(&db->db_holds) > 1) {
465 int blksz = db->db.db_size;
466 spa_t *spa = db->db_objset->os_spa;
468 mutex_exit(&db->db_mtx);
469 abuf = arc_loan_buf(spa, blksz);
470 bcopy(db->db.db_data, abuf->b_data, blksz);
473 arc_loan_inuse_buf(abuf, db);
474 dbuf_set_data(db, NULL);
475 mutex_exit(&db->db_mtx);
481 dbuf_whichblock(dnode_t *dn, uint64_t offset)
483 if (dn->dn_datablkshift) {
484 return (offset >> dn->dn_datablkshift);
486 ASSERT3U(offset, <, dn->dn_datablksz);
492 dbuf_read_done(zio_t *zio, arc_buf_t *buf, void *vdb)
494 dmu_buf_impl_t *db = vdb;
496 mutex_enter(&db->db_mtx);
497 ASSERT3U(db->db_state, ==, DB_READ);
499 * All reads are synchronous, so we must have a hold on the dbuf
501 ASSERT(refcount_count(&db->db_holds) > 0);
502 ASSERT(db->db_buf == NULL);
503 ASSERT(db->db.db_data == NULL);
504 if (db->db_level == 0 && db->db_freed_in_flight) {
505 /* we were freed in flight; disregard any error */
506 arc_release(buf, db);
507 bzero(buf->b_data, db->db.db_size);
509 db->db_freed_in_flight = FALSE;
510 dbuf_set_data(db, buf);
511 db->db_state = DB_CACHED;
512 } else if (zio == NULL || zio->io_error == 0) {
513 dbuf_set_data(db, buf);
514 db->db_state = DB_CACHED;
516 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
517 ASSERT3P(db->db_buf, ==, NULL);
518 VERIFY(arc_buf_remove_ref(buf, db));
519 db->db_state = DB_UNCACHED;
521 cv_broadcast(&db->db_changed);
522 dbuf_rele_and_unlock(db, NULL);
526 dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t *flags)
530 uint32_t aflags = ARC_NOWAIT;
534 ASSERT(!refcount_is_zero(&db->db_holds));
535 /* We need the struct_rwlock to prevent db_blkptr from changing. */
536 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
537 ASSERT(MUTEX_HELD(&db->db_mtx));
538 ASSERT(db->db_state == DB_UNCACHED);
539 ASSERT(db->db_buf == NULL);
541 if (db->db_blkid == DMU_BONUS_BLKID) {
542 int bonuslen = MIN(dn->dn_bonuslen, dn->dn_phys->dn_bonuslen);
544 ASSERT3U(bonuslen, <=, db->db.db_size);
545 db->db.db_data = zio_buf_alloc(DN_MAX_BONUSLEN);
546 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
547 if (bonuslen < DN_MAX_BONUSLEN)
548 bzero(db->db.db_data, DN_MAX_BONUSLEN);
550 bcopy(DN_BONUS(dn->dn_phys), db->db.db_data, bonuslen);
552 dbuf_update_data(db);
553 db->db_state = DB_CACHED;
554 mutex_exit(&db->db_mtx);
559 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
560 * processes the delete record and clears the bp while we are waiting
561 * for the dn_mtx (resulting in a "no" from block_freed).
563 if (db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr) ||
564 (db->db_level == 0 && (dnode_block_freed(dn, db->db_blkid) ||
565 BP_IS_HOLE(db->db_blkptr)))) {
566 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
569 dbuf_set_data(db, arc_buf_alloc(db->db_objset->os_spa,
570 db->db.db_size, db, type));
571 bzero(db->db.db_data, db->db.db_size);
572 db->db_state = DB_CACHED;
573 *flags |= DB_RF_CACHED;
574 mutex_exit(&db->db_mtx);
580 db->db_state = DB_READ;
581 mutex_exit(&db->db_mtx);
583 if (DBUF_IS_L2CACHEABLE(db))
584 aflags |= ARC_L2CACHE;
585 if (DBUF_IS_L2COMPRESSIBLE(db))
586 aflags |= ARC_L2COMPRESS;
588 SET_BOOKMARK(&zb, db->db_objset->os_dsl_dataset ?
589 db->db_objset->os_dsl_dataset->ds_object : DMU_META_OBJSET,
590 db->db.db_object, db->db_level, db->db_blkid);
592 dbuf_add_ref(db, NULL);
594 (void) arc_read(zio, db->db_objset->os_spa, db->db_blkptr,
595 dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ,
596 (*flags & DB_RF_CANFAIL) ? ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED,
598 if (aflags & ARC_CACHED)
599 *flags |= DB_RF_CACHED;
603 dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
606 boolean_t havepzio = (zio != NULL);
611 * We don't have to hold the mutex to check db_state because it
612 * can't be freed while we have a hold on the buffer.
614 ASSERT(!refcount_is_zero(&db->db_holds));
616 if (db->db_state == DB_NOFILL)
617 return (SET_ERROR(EIO));
621 if ((flags & DB_RF_HAVESTRUCT) == 0)
622 rw_enter(&dn->dn_struct_rwlock, RW_READER);
624 prefetch = db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
625 (flags & DB_RF_NOPREFETCH) == 0 && dn != NULL &&
626 DBUF_IS_CACHEABLE(db);
628 mutex_enter(&db->db_mtx);
629 if (db->db_state == DB_CACHED) {
630 mutex_exit(&db->db_mtx);
632 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
633 db->db.db_size, TRUE);
634 if ((flags & DB_RF_HAVESTRUCT) == 0)
635 rw_exit(&dn->dn_struct_rwlock);
637 } else if (db->db_state == DB_UNCACHED) {
638 spa_t *spa = dn->dn_objset->os_spa;
641 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
642 dbuf_read_impl(db, zio, &flags);
644 /* dbuf_read_impl has dropped db_mtx for us */
647 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
648 db->db.db_size, flags & DB_RF_CACHED);
650 if ((flags & DB_RF_HAVESTRUCT) == 0)
651 rw_exit(&dn->dn_struct_rwlock);
658 * Another reader came in while the dbuf was in flight
659 * between UNCACHED and CACHED. Either a writer will finish
660 * writing the buffer (sending the dbuf to CACHED) or the
661 * first reader's request will reach the read_done callback
662 * and send the dbuf to CACHED. Otherwise, a failure
663 * occurred and the dbuf went to UNCACHED.
665 mutex_exit(&db->db_mtx);
667 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
668 db->db.db_size, TRUE);
669 if ((flags & DB_RF_HAVESTRUCT) == 0)
670 rw_exit(&dn->dn_struct_rwlock);
673 /* Skip the wait per the caller's request. */
674 mutex_enter(&db->db_mtx);
675 if ((flags & DB_RF_NEVERWAIT) == 0) {
676 while (db->db_state == DB_READ ||
677 db->db_state == DB_FILL) {
678 ASSERT(db->db_state == DB_READ ||
679 (flags & DB_RF_HAVESTRUCT) == 0);
680 cv_wait(&db->db_changed, &db->db_mtx);
682 if (db->db_state == DB_UNCACHED)
683 err = SET_ERROR(EIO);
685 mutex_exit(&db->db_mtx);
688 ASSERT(err || havepzio || db->db_state == DB_CACHED);
693 dbuf_noread(dmu_buf_impl_t *db)
695 ASSERT(!refcount_is_zero(&db->db_holds));
696 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
697 mutex_enter(&db->db_mtx);
698 while (db->db_state == DB_READ || db->db_state == DB_FILL)
699 cv_wait(&db->db_changed, &db->db_mtx);
700 if (db->db_state == DB_UNCACHED) {
701 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
702 spa_t *spa = db->db_objset->os_spa;
704 ASSERT(db->db_buf == NULL);
705 ASSERT(db->db.db_data == NULL);
706 dbuf_set_data(db, arc_buf_alloc(spa, db->db.db_size, db, type));
707 db->db_state = DB_FILL;
708 } else if (db->db_state == DB_NOFILL) {
709 dbuf_set_data(db, NULL);
711 ASSERT3U(db->db_state, ==, DB_CACHED);
713 mutex_exit(&db->db_mtx);
717 * This is our just-in-time copy function. It makes a copy of
718 * buffers, that have been modified in a previous transaction
719 * group, before we modify them in the current active group.
721 * This function is used in two places: when we are dirtying a
722 * buffer for the first time in a txg, and when we are freeing
723 * a range in a dnode that includes this buffer.
725 * Note that when we are called from dbuf_free_range() we do
726 * not put a hold on the buffer, we just traverse the active
727 * dbuf list for the dnode.
730 dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg)
732 dbuf_dirty_record_t *dr = db->db_last_dirty;
734 ASSERT(MUTEX_HELD(&db->db_mtx));
735 ASSERT(db->db.db_data != NULL);
736 ASSERT(db->db_level == 0);
737 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT);
740 (dr->dt.dl.dr_data !=
741 ((db->db_blkid == DMU_BONUS_BLKID) ? db->db.db_data : db->db_buf)))
745 * If the last dirty record for this dbuf has not yet synced
746 * and its referencing the dbuf data, either:
747 * reset the reference to point to a new copy,
748 * or (if there a no active holders)
749 * just null out the current db_data pointer.
751 ASSERT(dr->dr_txg >= txg - 2);
752 if (db->db_blkid == DMU_BONUS_BLKID) {
753 /* Note that the data bufs here are zio_bufs */
754 dr->dt.dl.dr_data = zio_buf_alloc(DN_MAX_BONUSLEN);
755 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
756 bcopy(db->db.db_data, dr->dt.dl.dr_data, DN_MAX_BONUSLEN);
757 } else if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
758 int size = db->db.db_size;
759 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
760 spa_t *spa = db->db_objset->os_spa;
762 dr->dt.dl.dr_data = arc_buf_alloc(spa, size, db, type);
763 bcopy(db->db.db_data, dr->dt.dl.dr_data->b_data, size);
765 dbuf_set_data(db, NULL);
770 dbuf_unoverride(dbuf_dirty_record_t *dr)
772 dmu_buf_impl_t *db = dr->dr_dbuf;
773 blkptr_t *bp = &dr->dt.dl.dr_overridden_by;
774 uint64_t txg = dr->dr_txg;
776 ASSERT(MUTEX_HELD(&db->db_mtx));
777 ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC);
778 ASSERT(db->db_level == 0);
780 if (db->db_blkid == DMU_BONUS_BLKID ||
781 dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN)
784 ASSERT(db->db_data_pending != dr);
786 /* free this block */
787 if (!BP_IS_HOLE(bp) && !dr->dt.dl.dr_nopwrite)
788 zio_free(db->db_objset->os_spa, txg, bp);
790 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
791 dr->dt.dl.dr_nopwrite = B_FALSE;
794 * Release the already-written buffer, so we leave it in
795 * a consistent dirty state. Note that all callers are
796 * modifying the buffer, so they will immediately do
797 * another (redundant) arc_release(). Therefore, leave
798 * the buf thawed to save the effort of freezing &
799 * immediately re-thawing it.
801 arc_release(dr->dt.dl.dr_data, db);
805 * Evict (if its unreferenced) or clear (if its referenced) any level-0
806 * data blocks in the free range, so that any future readers will find
809 * This is a no-op if the dataset is in the middle of an incremental
810 * receive; see comment below for details.
813 dbuf_free_range(dnode_t *dn, uint64_t start_blkid, uint64_t end_blkid,
816 dmu_buf_impl_t *db, *db_next, db_search;
817 uint64_t txg = tx->tx_txg;
820 if (end_blkid > dn->dn_maxblkid && (end_blkid != DMU_SPILL_BLKID))
821 end_blkid = dn->dn_maxblkid;
822 dprintf_dnode(dn, "start=%llu end=%llu\n", start_blkid, end_blkid);
824 db_search.db_level = 0;
825 db_search.db_blkid = start_blkid;
826 db_search.db_creation = 0;
828 mutex_enter(&dn->dn_dbufs_mtx);
829 if (start_blkid >= dn->dn_unlisted_l0_blkid) {
830 /* There can't be any dbufs in this range; no need to search. */
832 db = avl_find(&dn->dn_dbufs, &db_search, &where);
833 ASSERT3P(db, ==, NULL);
834 db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER);
835 ASSERT(db == NULL || db->db_level > 0);
837 mutex_exit(&dn->dn_dbufs_mtx);
839 } else if (dmu_objset_is_receiving(dn->dn_objset)) {
841 * If we are receiving, we expect there to be no dbufs in
842 * the range to be freed, because receive modifies each
843 * block at most once, and in offset order. If this is
844 * not the case, it can lead to performance problems,
845 * so note that we unexpectedly took the slow path.
847 atomic_inc_64(&zfs_free_range_recv_miss);
850 db = avl_find(&dn->dn_dbufs, &db_search, &where);
851 ASSERT3P(db, ==, NULL);
852 db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER);
854 for (; db != NULL; db = db_next) {
855 db_next = AVL_NEXT(&dn->dn_dbufs, db);
856 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
858 if (db->db_level != 0 || db->db_blkid > end_blkid) {
861 ASSERT3U(db->db_blkid, >=, start_blkid);
863 /* found a level 0 buffer in the range */
864 mutex_enter(&db->db_mtx);
865 if (dbuf_undirty(db, tx)) {
866 /* mutex has been dropped and dbuf destroyed */
870 if (db->db_state == DB_UNCACHED ||
871 db->db_state == DB_NOFILL ||
872 db->db_state == DB_EVICTING) {
873 ASSERT(db->db.db_data == NULL);
874 mutex_exit(&db->db_mtx);
877 if (db->db_state == DB_READ || db->db_state == DB_FILL) {
878 /* will be handled in dbuf_read_done or dbuf_rele */
879 db->db_freed_in_flight = TRUE;
880 mutex_exit(&db->db_mtx);
883 if (refcount_count(&db->db_holds) == 0) {
888 /* The dbuf is referenced */
890 if (db->db_last_dirty != NULL) {
891 dbuf_dirty_record_t *dr = db->db_last_dirty;
893 if (dr->dr_txg == txg) {
895 * This buffer is "in-use", re-adjust the file
896 * size to reflect that this buffer may
897 * contain new data when we sync.
899 if (db->db_blkid != DMU_SPILL_BLKID &&
900 db->db_blkid > dn->dn_maxblkid)
901 dn->dn_maxblkid = db->db_blkid;
905 * This dbuf is not dirty in the open context.
906 * Either uncache it (if its not referenced in
907 * the open context) or reset its contents to
910 dbuf_fix_old_data(db, txg);
913 /* clear the contents if its cached */
914 if (db->db_state == DB_CACHED) {
915 ASSERT(db->db.db_data != NULL);
916 arc_release(db->db_buf, db);
917 bzero(db->db.db_data, db->db.db_size);
918 arc_buf_freeze(db->db_buf);
921 mutex_exit(&db->db_mtx);
923 mutex_exit(&dn->dn_dbufs_mtx);
927 dbuf_block_freeable(dmu_buf_impl_t *db)
929 dsl_dataset_t *ds = db->db_objset->os_dsl_dataset;
930 uint64_t birth_txg = 0;
933 * We don't need any locking to protect db_blkptr:
934 * If it's syncing, then db_last_dirty will be set
935 * so we'll ignore db_blkptr.
937 * This logic ensures that only block births for
938 * filled blocks are considered.
940 ASSERT(MUTEX_HELD(&db->db_mtx));
941 if (db->db_last_dirty && (db->db_blkptr == NULL ||
942 !BP_IS_HOLE(db->db_blkptr))) {
943 birth_txg = db->db_last_dirty->dr_txg;
944 } else if (db->db_blkptr != NULL && !BP_IS_HOLE(db->db_blkptr)) {
945 birth_txg = db->db_blkptr->blk_birth;
949 * If this block don't exist or is in a snapshot, it can't be freed.
950 * Don't pass the bp to dsl_dataset_block_freeable() since we
951 * are holding the db_mtx lock and might deadlock if we are
952 * prefetching a dedup-ed block.
955 return (ds == NULL ||
956 dsl_dataset_block_freeable(ds, NULL, birth_txg));
962 dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx)
964 arc_buf_t *buf, *obuf;
965 int osize = db->db.db_size;
966 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
969 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
974 /* XXX does *this* func really need the lock? */
975 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
978 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
979 * is OK, because there can be no other references to the db
980 * when we are changing its size, so no concurrent DB_FILL can
984 * XXX we should be doing a dbuf_read, checking the return
985 * value and returning that up to our callers
987 dmu_buf_will_dirty(&db->db, tx);
989 /* create the data buffer for the new block */
990 buf = arc_buf_alloc(dn->dn_objset->os_spa, size, db, type);
992 /* copy old block data to the new block */
994 bcopy(obuf->b_data, buf->b_data, MIN(osize, size));
995 /* zero the remainder */
997 bzero((uint8_t *)buf->b_data + osize, size - osize);
999 mutex_enter(&db->db_mtx);
1000 dbuf_set_data(db, buf);
1001 VERIFY(arc_buf_remove_ref(obuf, db));
1002 db->db.db_size = size;
1004 if (db->db_level == 0) {
1005 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
1006 db->db_last_dirty->dt.dl.dr_data = buf;
1008 mutex_exit(&db->db_mtx);
1010 dnode_willuse_space(dn, size-osize, tx);
1015 dbuf_release_bp(dmu_buf_impl_t *db)
1017 objset_t *os = db->db_objset;
1019 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
1020 ASSERT(arc_released(os->os_phys_buf) ||
1021 list_link_active(&os->os_dsl_dataset->ds_synced_link));
1022 ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf));
1024 (void) arc_release(db->db_buf, db);
1027 dbuf_dirty_record_t *
1028 dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1032 dbuf_dirty_record_t **drp, *dr;
1033 int drop_struct_lock = FALSE;
1034 boolean_t do_free_accounting = B_FALSE;
1035 int txgoff = tx->tx_txg & TXG_MASK;
1037 ASSERT(tx->tx_txg != 0);
1038 ASSERT(!refcount_is_zero(&db->db_holds));
1039 DMU_TX_DIRTY_BUF(tx, db);
1044 * Shouldn't dirty a regular buffer in syncing context. Private
1045 * objects may be dirtied in syncing context, but only if they
1046 * were already pre-dirtied in open context.
1048 ASSERT(!dmu_tx_is_syncing(tx) ||
1049 BP_IS_HOLE(dn->dn_objset->os_rootbp) ||
1050 DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1051 dn->dn_objset->os_dsl_dataset == NULL);
1053 * We make this assert for private objects as well, but after we
1054 * check if we're already dirty. They are allowed to re-dirty
1055 * in syncing context.
1057 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1058 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1059 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1061 mutex_enter(&db->db_mtx);
1063 * XXX make this true for indirects too? The problem is that
1064 * transactions created with dmu_tx_create_assigned() from
1065 * syncing context don't bother holding ahead.
1067 ASSERT(db->db_level != 0 ||
1068 db->db_state == DB_CACHED || db->db_state == DB_FILL ||
1069 db->db_state == DB_NOFILL);
1071 mutex_enter(&dn->dn_mtx);
1073 * Don't set dirtyctx to SYNC if we're just modifying this as we
1074 * initialize the objset.
1076 if (dn->dn_dirtyctx == DN_UNDIRTIED &&
1077 !BP_IS_HOLE(dn->dn_objset->os_rootbp)) {
1079 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN);
1080 ASSERT(dn->dn_dirtyctx_firstset == NULL);
1081 dn->dn_dirtyctx_firstset = kmem_alloc(1, KM_SLEEP);
1083 mutex_exit(&dn->dn_mtx);
1085 if (db->db_blkid == DMU_SPILL_BLKID)
1086 dn->dn_have_spill = B_TRUE;
1089 * If this buffer is already dirty, we're done.
1091 drp = &db->db_last_dirty;
1092 ASSERT(*drp == NULL || (*drp)->dr_txg <= tx->tx_txg ||
1093 db->db.db_object == DMU_META_DNODE_OBJECT);
1094 while ((dr = *drp) != NULL && dr->dr_txg > tx->tx_txg)
1096 if (dr && dr->dr_txg == tx->tx_txg) {
1099 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID) {
1101 * If this buffer has already been written out,
1102 * we now need to reset its state.
1104 dbuf_unoverride(dr);
1105 if (db->db.db_object != DMU_META_DNODE_OBJECT &&
1106 db->db_state != DB_NOFILL)
1107 arc_buf_thaw(db->db_buf);
1109 mutex_exit(&db->db_mtx);
1114 * Only valid if not already dirty.
1116 ASSERT(dn->dn_object == 0 ||
1117 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1118 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1120 ASSERT3U(dn->dn_nlevels, >, db->db_level);
1121 ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) ||
1122 dn->dn_phys->dn_nlevels > db->db_level ||
1123 dn->dn_next_nlevels[txgoff] > db->db_level ||
1124 dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level ||
1125 dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level);
1128 * We should only be dirtying in syncing context if it's the
1129 * mos or we're initializing the os or it's a special object.
1130 * However, we are allowed to dirty in syncing context provided
1131 * we already dirtied it in open context. Hence we must make
1132 * this assertion only if we're not already dirty.
1135 ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1136 os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp));
1137 ASSERT(db->db.db_size != 0);
1139 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1141 if (db->db_blkid != DMU_BONUS_BLKID) {
1143 * Update the accounting.
1144 * Note: we delay "free accounting" until after we drop
1145 * the db_mtx. This keeps us from grabbing other locks
1146 * (and possibly deadlocking) in bp_get_dsize() while
1147 * also holding the db_mtx.
1149 dnode_willuse_space(dn, db->db.db_size, tx);
1150 do_free_accounting = dbuf_block_freeable(db);
1154 * If this buffer is dirty in an old transaction group we need
1155 * to make a copy of it so that the changes we make in this
1156 * transaction group won't leak out when we sync the older txg.
1158 dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP);
1159 if (db->db_level == 0) {
1160 void *data_old = db->db_buf;
1162 if (db->db_state != DB_NOFILL) {
1163 if (db->db_blkid == DMU_BONUS_BLKID) {
1164 dbuf_fix_old_data(db, tx->tx_txg);
1165 data_old = db->db.db_data;
1166 } else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
1168 * Release the data buffer from the cache so
1169 * that we can modify it without impacting
1170 * possible other users of this cached data
1171 * block. Note that indirect blocks and
1172 * private objects are not released until the
1173 * syncing state (since they are only modified
1176 arc_release(db->db_buf, db);
1177 dbuf_fix_old_data(db, tx->tx_txg);
1178 data_old = db->db_buf;
1180 ASSERT(data_old != NULL);
1182 dr->dt.dl.dr_data = data_old;
1184 mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_DEFAULT, NULL);
1185 list_create(&dr->dt.di.dr_children,
1186 sizeof (dbuf_dirty_record_t),
1187 offsetof(dbuf_dirty_record_t, dr_dirty_node));
1189 if (db->db_blkid != DMU_BONUS_BLKID && os->os_dsl_dataset != NULL)
1190 dr->dr_accounted = db->db.db_size;
1192 dr->dr_txg = tx->tx_txg;
1197 * We could have been freed_in_flight between the dbuf_noread
1198 * and dbuf_dirty. We win, as though the dbuf_noread() had
1199 * happened after the free.
1201 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1202 db->db_blkid != DMU_SPILL_BLKID) {
1203 mutex_enter(&dn->dn_mtx);
1204 if (dn->dn_free_ranges[txgoff] != NULL) {
1205 range_tree_clear(dn->dn_free_ranges[txgoff],
1208 mutex_exit(&dn->dn_mtx);
1209 db->db_freed_in_flight = FALSE;
1213 * This buffer is now part of this txg
1215 dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg);
1216 db->db_dirtycnt += 1;
1217 ASSERT3U(db->db_dirtycnt, <=, 3);
1219 mutex_exit(&db->db_mtx);
1221 if (db->db_blkid == DMU_BONUS_BLKID ||
1222 db->db_blkid == DMU_SPILL_BLKID) {
1223 mutex_enter(&dn->dn_mtx);
1224 ASSERT(!list_link_active(&dr->dr_dirty_node));
1225 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1226 mutex_exit(&dn->dn_mtx);
1227 dnode_setdirty(dn, tx);
1230 } else if (do_free_accounting) {
1231 blkptr_t *bp = db->db_blkptr;
1232 int64_t willfree = (bp && !BP_IS_HOLE(bp)) ?
1233 bp_get_dsize(os->os_spa, bp) : db->db.db_size;
1235 * This is only a guess -- if the dbuf is dirty
1236 * in a previous txg, we don't know how much
1237 * space it will use on disk yet. We should
1238 * really have the struct_rwlock to access
1239 * db_blkptr, but since this is just a guess,
1240 * it's OK if we get an odd answer.
1242 ddt_prefetch(os->os_spa, bp);
1243 dnode_willuse_space(dn, -willfree, tx);
1246 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
1247 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1248 drop_struct_lock = TRUE;
1251 if (db->db_level == 0) {
1252 dnode_new_blkid(dn, db->db_blkid, tx, drop_struct_lock);
1253 ASSERT(dn->dn_maxblkid >= db->db_blkid);
1256 if (db->db_level+1 < dn->dn_nlevels) {
1257 dmu_buf_impl_t *parent = db->db_parent;
1258 dbuf_dirty_record_t *di;
1259 int parent_held = FALSE;
1261 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
1262 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1264 parent = dbuf_hold_level(dn, db->db_level+1,
1265 db->db_blkid >> epbs, FTAG);
1266 ASSERT(parent != NULL);
1269 if (drop_struct_lock)
1270 rw_exit(&dn->dn_struct_rwlock);
1271 ASSERT3U(db->db_level+1, ==, parent->db_level);
1272 di = dbuf_dirty(parent, tx);
1274 dbuf_rele(parent, FTAG);
1276 mutex_enter(&db->db_mtx);
1278 * Since we've dropped the mutex, it's possible that
1279 * dbuf_undirty() might have changed this out from under us.
1281 if (db->db_last_dirty == dr ||
1282 dn->dn_object == DMU_META_DNODE_OBJECT) {
1283 mutex_enter(&di->dt.di.dr_mtx);
1284 ASSERT3U(di->dr_txg, ==, tx->tx_txg);
1285 ASSERT(!list_link_active(&dr->dr_dirty_node));
1286 list_insert_tail(&di->dt.di.dr_children, dr);
1287 mutex_exit(&di->dt.di.dr_mtx);
1290 mutex_exit(&db->db_mtx);
1292 ASSERT(db->db_level+1 == dn->dn_nlevels);
1293 ASSERT(db->db_blkid < dn->dn_nblkptr);
1294 ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf);
1295 mutex_enter(&dn->dn_mtx);
1296 ASSERT(!list_link_active(&dr->dr_dirty_node));
1297 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1298 mutex_exit(&dn->dn_mtx);
1299 if (drop_struct_lock)
1300 rw_exit(&dn->dn_struct_rwlock);
1303 dnode_setdirty(dn, tx);
1309 * Undirty a buffer in the transaction group referenced by the given
1310 * transaction. Return whether this evicted the dbuf.
1313 dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1316 uint64_t txg = tx->tx_txg;
1317 dbuf_dirty_record_t *dr, **drp;
1320 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1321 ASSERT0(db->db_level);
1322 ASSERT(MUTEX_HELD(&db->db_mtx));
1325 * If this buffer is not dirty, we're done.
1327 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1328 if (dr->dr_txg <= txg)
1330 if (dr == NULL || dr->dr_txg < txg)
1332 ASSERT(dr->dr_txg == txg);
1333 ASSERT(dr->dr_dbuf == db);
1338 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1340 ASSERT(db->db.db_size != 0);
1343 * Any space we accounted for in dp_dirty_* will be cleaned up by
1344 * dsl_pool_sync(). This is relatively rare so the discrepancy
1345 * is not a big deal.
1351 * Note that there are three places in dbuf_dirty()
1352 * where this dirty record may be put on a list.
1353 * Make sure to do a list_remove corresponding to
1354 * every one of those list_insert calls.
1356 if (dr->dr_parent) {
1357 mutex_enter(&dr->dr_parent->dt.di.dr_mtx);
1358 list_remove(&dr->dr_parent->dt.di.dr_children, dr);
1359 mutex_exit(&dr->dr_parent->dt.di.dr_mtx);
1360 } else if (db->db_blkid == DMU_SPILL_BLKID ||
1361 db->db_level+1 == dn->dn_nlevels) {
1362 ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf);
1363 mutex_enter(&dn->dn_mtx);
1364 list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr);
1365 mutex_exit(&dn->dn_mtx);
1369 if (db->db_state != DB_NOFILL) {
1370 dbuf_unoverride(dr);
1372 ASSERT(db->db_buf != NULL);
1373 ASSERT(dr->dt.dl.dr_data != NULL);
1374 if (dr->dt.dl.dr_data != db->db_buf)
1375 VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data, db));
1378 if (db->db_level != 0) {
1379 mutex_destroy(&dr->dt.di.dr_mtx);
1380 list_destroy(&dr->dt.di.dr_children);
1383 kmem_free(dr, sizeof (dbuf_dirty_record_t));
1385 ASSERT(db->db_dirtycnt > 0);
1386 db->db_dirtycnt -= 1;
1388 if (refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) {
1389 arc_buf_t *buf = db->db_buf;
1391 ASSERT(db->db_state == DB_NOFILL || arc_released(buf));
1392 dbuf_set_data(db, NULL);
1393 VERIFY(arc_buf_remove_ref(buf, db));
1402 dmu_buf_will_dirty(dmu_buf_t *db_fake, dmu_tx_t *tx)
1404 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1405 int rf = DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH;
1407 ASSERT(tx->tx_txg != 0);
1408 ASSERT(!refcount_is_zero(&db->db_holds));
1411 if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock))
1412 rf |= DB_RF_HAVESTRUCT;
1414 (void) dbuf_read(db, NULL, rf);
1415 (void) dbuf_dirty(db, tx);
1419 dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1421 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1423 db->db_state = DB_NOFILL;
1425 dmu_buf_will_fill(db_fake, tx);
1429 dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1431 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1433 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1434 ASSERT(tx->tx_txg != 0);
1435 ASSERT(db->db_level == 0);
1436 ASSERT(!refcount_is_zero(&db->db_holds));
1438 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
1439 dmu_tx_private_ok(tx));
1442 (void) dbuf_dirty(db, tx);
1445 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1448 dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx)
1450 mutex_enter(&db->db_mtx);
1453 if (db->db_state == DB_FILL) {
1454 if (db->db_level == 0 && db->db_freed_in_flight) {
1455 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1456 /* we were freed while filling */
1457 /* XXX dbuf_undirty? */
1458 bzero(db->db.db_data, db->db.db_size);
1459 db->db_freed_in_flight = FALSE;
1461 db->db_state = DB_CACHED;
1462 cv_broadcast(&db->db_changed);
1464 mutex_exit(&db->db_mtx);
1468 dmu_buf_write_embedded(dmu_buf_t *dbuf, void *data,
1469 bp_embedded_type_t etype, enum zio_compress comp,
1470 int uncompressed_size, int compressed_size, int byteorder,
1473 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
1474 struct dirty_leaf *dl;
1475 dmu_object_type_t type;
1478 type = DB_DNODE(db)->dn_type;
1481 ASSERT0(db->db_level);
1482 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1484 dmu_buf_will_not_fill(dbuf, tx);
1486 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
1487 dl = &db->db_last_dirty->dt.dl;
1488 encode_embedded_bp_compressed(&dl->dr_overridden_by,
1489 data, comp, uncompressed_size, compressed_size);
1490 BPE_SET_ETYPE(&dl->dr_overridden_by, etype);
1491 BP_SET_TYPE(&dl->dr_overridden_by, type);
1492 BP_SET_LEVEL(&dl->dr_overridden_by, 0);
1493 BP_SET_BYTEORDER(&dl->dr_overridden_by, byteorder);
1495 dl->dr_override_state = DR_OVERRIDDEN;
1496 dl->dr_overridden_by.blk_birth = db->db_last_dirty->dr_txg;
1500 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1501 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1504 dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx)
1506 ASSERT(!refcount_is_zero(&db->db_holds));
1507 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1508 ASSERT(db->db_level == 0);
1509 ASSERT(DBUF_GET_BUFC_TYPE(db) == ARC_BUFC_DATA);
1510 ASSERT(buf != NULL);
1511 ASSERT(arc_buf_size(buf) == db->db.db_size);
1512 ASSERT(tx->tx_txg != 0);
1514 arc_return_buf(buf, db);
1515 ASSERT(arc_released(buf));
1517 mutex_enter(&db->db_mtx);
1519 while (db->db_state == DB_READ || db->db_state == DB_FILL)
1520 cv_wait(&db->db_changed, &db->db_mtx);
1522 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED);
1524 if (db->db_state == DB_CACHED &&
1525 refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) {
1526 mutex_exit(&db->db_mtx);
1527 (void) dbuf_dirty(db, tx);
1528 bcopy(buf->b_data, db->db.db_data, db->db.db_size);
1529 VERIFY(arc_buf_remove_ref(buf, db));
1530 xuio_stat_wbuf_copied();
1534 xuio_stat_wbuf_nocopy();
1535 if (db->db_state == DB_CACHED) {
1536 dbuf_dirty_record_t *dr = db->db_last_dirty;
1538 ASSERT(db->db_buf != NULL);
1539 if (dr != NULL && dr->dr_txg == tx->tx_txg) {
1540 ASSERT(dr->dt.dl.dr_data == db->db_buf);
1541 if (!arc_released(db->db_buf)) {
1542 ASSERT(dr->dt.dl.dr_override_state ==
1544 arc_release(db->db_buf, db);
1546 dr->dt.dl.dr_data = buf;
1547 VERIFY(arc_buf_remove_ref(db->db_buf, db));
1548 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) {
1549 arc_release(db->db_buf, db);
1550 VERIFY(arc_buf_remove_ref(db->db_buf, db));
1554 ASSERT(db->db_buf == NULL);
1555 dbuf_set_data(db, buf);
1556 db->db_state = DB_FILL;
1557 mutex_exit(&db->db_mtx);
1558 (void) dbuf_dirty(db, tx);
1559 dmu_buf_fill_done(&db->db, tx);
1563 * "Clear" the contents of this dbuf. This will mark the dbuf
1564 * EVICTING and clear *most* of its references. Unfortunately,
1565 * when we are not holding the dn_dbufs_mtx, we can't clear the
1566 * entry in the dn_dbufs list. We have to wait until dbuf_destroy()
1567 * in this case. For callers from the DMU we will usually see:
1568 * dbuf_clear()->arc_clear_callback()->dbuf_do_evict()->dbuf_destroy()
1569 * For the arc callback, we will usually see:
1570 * dbuf_do_evict()->dbuf_clear();dbuf_destroy()
1571 * Sometimes, though, we will get a mix of these two:
1572 * DMU: dbuf_clear()->arc_clear_callback()
1573 * ARC: dbuf_do_evict()->dbuf_destroy()
1575 * This routine will dissociate the dbuf from the arc, by calling
1576 * arc_clear_callback(), but will not evict the data from the ARC.
1579 dbuf_clear(dmu_buf_impl_t *db)
1582 dmu_buf_impl_t *parent = db->db_parent;
1583 dmu_buf_impl_t *dndb;
1584 boolean_t dbuf_gone = B_FALSE;
1586 ASSERT(MUTEX_HELD(&db->db_mtx));
1587 ASSERT(refcount_is_zero(&db->db_holds));
1589 dbuf_evict_user(db);
1591 if (db->db_state == DB_CACHED) {
1592 ASSERT(db->db.db_data != NULL);
1593 if (db->db_blkid == DMU_BONUS_BLKID) {
1594 zio_buf_free(db->db.db_data, DN_MAX_BONUSLEN);
1595 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
1597 db->db.db_data = NULL;
1598 db->db_state = DB_UNCACHED;
1601 ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL);
1602 ASSERT(db->db_data_pending == NULL);
1604 db->db_state = DB_EVICTING;
1605 db->db_blkptr = NULL;
1610 if (db->db_blkid != DMU_BONUS_BLKID && MUTEX_HELD(&dn->dn_dbufs_mtx)) {
1611 avl_remove(&dn->dn_dbufs, db);
1612 (void) atomic_dec_32_nv(&dn->dn_dbufs_count);
1616 * Decrementing the dbuf count means that the hold corresponding
1617 * to the removed dbuf is no longer discounted in dnode_move(),
1618 * so the dnode cannot be moved until after we release the hold.
1619 * The membar_producer() ensures visibility of the decremented
1620 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
1624 db->db_dnode_handle = NULL;
1630 dbuf_gone = arc_clear_callback(db->db_buf);
1633 mutex_exit(&db->db_mtx);
1636 * If this dbuf is referenced from an indirect dbuf,
1637 * decrement the ref count on the indirect dbuf.
1639 if (parent && parent != dndb)
1640 dbuf_rele(parent, db);
1644 dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse,
1645 dmu_buf_impl_t **parentp, blkptr_t **bpp)
1652 ASSERT(blkid != DMU_BONUS_BLKID);
1654 if (blkid == DMU_SPILL_BLKID) {
1655 mutex_enter(&dn->dn_mtx);
1656 if (dn->dn_have_spill &&
1657 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
1658 *bpp = &dn->dn_phys->dn_spill;
1661 dbuf_add_ref(dn->dn_dbuf, NULL);
1662 *parentp = dn->dn_dbuf;
1663 mutex_exit(&dn->dn_mtx);
1667 if (dn->dn_phys->dn_nlevels == 0)
1670 nlevels = dn->dn_phys->dn_nlevels;
1672 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1674 ASSERT3U(level * epbs, <, 64);
1675 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1676 if (level >= nlevels ||
1677 (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) {
1678 /* the buffer has no parent yet */
1679 return (SET_ERROR(ENOENT));
1680 } else if (level < nlevels-1) {
1681 /* this block is referenced from an indirect block */
1682 int err = dbuf_hold_impl(dn, level+1,
1683 blkid >> epbs, fail_sparse, NULL, parentp);
1686 err = dbuf_read(*parentp, NULL,
1687 (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL));
1689 dbuf_rele(*parentp, NULL);
1693 *bpp = ((blkptr_t *)(*parentp)->db.db_data) +
1694 (blkid & ((1ULL << epbs) - 1));
1697 /* the block is referenced from the dnode */
1698 ASSERT3U(level, ==, nlevels-1);
1699 ASSERT(dn->dn_phys->dn_nblkptr == 0 ||
1700 blkid < dn->dn_phys->dn_nblkptr);
1702 dbuf_add_ref(dn->dn_dbuf, NULL);
1703 *parentp = dn->dn_dbuf;
1705 *bpp = &dn->dn_phys->dn_blkptr[blkid];
1710 static dmu_buf_impl_t *
1711 dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid,
1712 dmu_buf_impl_t *parent, blkptr_t *blkptr)
1714 objset_t *os = dn->dn_objset;
1715 dmu_buf_impl_t *db, *odb;
1717 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1718 ASSERT(dn->dn_type != DMU_OT_NONE);
1720 db = kmem_cache_alloc(dbuf_cache, KM_SLEEP);
1723 db->db.db_object = dn->dn_object;
1724 db->db_level = level;
1725 db->db_blkid = blkid;
1726 db->db_last_dirty = NULL;
1727 db->db_dirtycnt = 0;
1728 db->db_dnode_handle = dn->dn_handle;
1729 db->db_parent = parent;
1730 db->db_blkptr = blkptr;
1732 db->db_user_ptr = NULL;
1733 db->db_user_data_ptr_ptr = NULL;
1734 db->db_evict_func = NULL;
1735 db->db_immediate_evict = 0;
1736 db->db_freed_in_flight = 0;
1738 if (blkid == DMU_BONUS_BLKID) {
1739 ASSERT3P(parent, ==, dn->dn_dbuf);
1740 db->db.db_size = DN_MAX_BONUSLEN -
1741 (dn->dn_nblkptr-1) * sizeof (blkptr_t);
1742 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
1743 db->db.db_offset = DMU_BONUS_BLKID;
1744 db->db_state = DB_UNCACHED;
1745 /* the bonus dbuf is not placed in the hash table */
1746 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1748 } else if (blkid == DMU_SPILL_BLKID) {
1749 db->db.db_size = (blkptr != NULL) ?
1750 BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE;
1751 db->db.db_offset = 0;
1754 db->db_level ? 1 << dn->dn_indblkshift : dn->dn_datablksz;
1755 db->db.db_size = blocksize;
1756 db->db.db_offset = db->db_blkid * blocksize;
1760 * Hold the dn_dbufs_mtx while we get the new dbuf
1761 * in the hash table *and* added to the dbufs list.
1762 * This prevents a possible deadlock with someone
1763 * trying to look up this dbuf before its added to the
1766 mutex_enter(&dn->dn_dbufs_mtx);
1767 db->db_state = DB_EVICTING;
1768 if ((odb = dbuf_hash_insert(db)) != NULL) {
1769 /* someone else inserted it first */
1770 kmem_cache_free(dbuf_cache, db);
1771 mutex_exit(&dn->dn_dbufs_mtx);
1774 avl_add(&dn->dn_dbufs, db);
1775 if (db->db_level == 0 && db->db_blkid >=
1776 dn->dn_unlisted_l0_blkid)
1777 dn->dn_unlisted_l0_blkid = db->db_blkid + 1;
1778 db->db_state = DB_UNCACHED;
1779 mutex_exit(&dn->dn_dbufs_mtx);
1780 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1782 if (parent && parent != dn->dn_dbuf)
1783 dbuf_add_ref(parent, db);
1785 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1786 refcount_count(&dn->dn_holds) > 0);
1787 (void) refcount_add(&dn->dn_holds, db);
1788 (void) atomic_inc_32_nv(&dn->dn_dbufs_count);
1790 dprintf_dbuf(db, "db=%p\n", db);
1796 dbuf_do_evict(void *private)
1798 dmu_buf_impl_t *db = private;
1800 if (!MUTEX_HELD(&db->db_mtx))
1801 mutex_enter(&db->db_mtx);
1803 ASSERT(refcount_is_zero(&db->db_holds));
1805 if (db->db_state != DB_EVICTING) {
1806 ASSERT(db->db_state == DB_CACHED);
1811 mutex_exit(&db->db_mtx);
1818 dbuf_destroy(dmu_buf_impl_t *db)
1820 ASSERT(refcount_is_zero(&db->db_holds));
1822 if (db->db_blkid != DMU_BONUS_BLKID) {
1824 * If this dbuf is still on the dn_dbufs list,
1825 * remove it from that list.
1827 if (db->db_dnode_handle != NULL) {
1832 mutex_enter(&dn->dn_dbufs_mtx);
1833 avl_remove(&dn->dn_dbufs, db);
1834 (void) atomic_dec_32_nv(&dn->dn_dbufs_count);
1835 mutex_exit(&dn->dn_dbufs_mtx);
1838 * Decrementing the dbuf count means that the hold
1839 * corresponding to the removed dbuf is no longer
1840 * discounted in dnode_move(), so the dnode cannot be
1841 * moved until after we release the hold.
1844 db->db_dnode_handle = NULL;
1846 dbuf_hash_remove(db);
1848 db->db_parent = NULL;
1851 ASSERT(db->db.db_data == NULL);
1852 ASSERT(db->db_hash_next == NULL);
1853 ASSERT(db->db_blkptr == NULL);
1854 ASSERT(db->db_data_pending == NULL);
1856 kmem_cache_free(dbuf_cache, db);
1857 arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1861 dbuf_prefetch(dnode_t *dn, uint64_t blkid, zio_priority_t prio)
1863 dmu_buf_impl_t *db = NULL;
1864 blkptr_t *bp = NULL;
1866 ASSERT(blkid != DMU_BONUS_BLKID);
1867 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1869 if (dnode_block_freed(dn, blkid))
1872 /* dbuf_find() returns with db_mtx held */
1873 if (db = dbuf_find(dn, 0, blkid)) {
1875 * This dbuf is already in the cache. We assume that
1876 * it is already CACHED, or else about to be either
1879 mutex_exit(&db->db_mtx);
1883 if (dbuf_findbp(dn, 0, blkid, TRUE, &db, &bp) == 0) {
1884 if (bp && !BP_IS_HOLE(bp) && !BP_IS_EMBEDDED(bp)) {
1885 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
1886 uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
1887 zbookmark_phys_t zb;
1889 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
1890 dn->dn_object, 0, blkid);
1892 (void) arc_read(NULL, dn->dn_objset->os_spa,
1893 bp, NULL, NULL, prio,
1894 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
1898 dbuf_rele(db, NULL);
1903 * Returns with db_holds incremented, and db_mtx not held.
1904 * Note: dn_struct_rwlock must be held.
1907 dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid, int fail_sparse,
1908 void *tag, dmu_buf_impl_t **dbp)
1910 dmu_buf_impl_t *db, *parent = NULL;
1912 ASSERT(blkid != DMU_BONUS_BLKID);
1913 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1914 ASSERT3U(dn->dn_nlevels, >, level);
1918 /* dbuf_find() returns with db_mtx held */
1919 db = dbuf_find(dn, level, blkid);
1922 blkptr_t *bp = NULL;
1925 ASSERT3P(parent, ==, NULL);
1926 err = dbuf_findbp(dn, level, blkid, fail_sparse, &parent, &bp);
1928 if (err == 0 && bp && BP_IS_HOLE(bp))
1929 err = SET_ERROR(ENOENT);
1932 dbuf_rele(parent, NULL);
1936 if (err && err != ENOENT)
1938 db = dbuf_create(dn, level, blkid, parent, bp);
1941 if (db->db_buf && refcount_is_zero(&db->db_holds)) {
1942 arc_buf_add_ref(db->db_buf, db);
1943 if (db->db_buf->b_data == NULL) {
1946 dbuf_rele(parent, NULL);
1951 ASSERT3P(db->db.db_data, ==, db->db_buf->b_data);
1954 ASSERT(db->db_buf == NULL || arc_referenced(db->db_buf));
1957 * If this buffer is currently syncing out, and we are are
1958 * still referencing it from db_data, we need to make a copy
1959 * of it in case we decide we want to dirty it again in this txg.
1961 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1962 dn->dn_object != DMU_META_DNODE_OBJECT &&
1963 db->db_state == DB_CACHED && db->db_data_pending) {
1964 dbuf_dirty_record_t *dr = db->db_data_pending;
1966 if (dr->dt.dl.dr_data == db->db_buf) {
1967 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1970 arc_buf_alloc(dn->dn_objset->os_spa,
1971 db->db.db_size, db, type));
1972 bcopy(dr->dt.dl.dr_data->b_data, db->db.db_data,
1977 (void) refcount_add(&db->db_holds, tag);
1978 dbuf_update_data(db);
1980 mutex_exit(&db->db_mtx);
1982 /* NOTE: we can't rele the parent until after we drop the db_mtx */
1984 dbuf_rele(parent, NULL);
1986 ASSERT3P(DB_DNODE(db), ==, dn);
1987 ASSERT3U(db->db_blkid, ==, blkid);
1988 ASSERT3U(db->db_level, ==, level);
1995 dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag)
1998 int err = dbuf_hold_impl(dn, 0, blkid, FALSE, tag, &db);
1999 return (err ? NULL : db);
2003 dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag)
2006 int err = dbuf_hold_impl(dn, level, blkid, FALSE, tag, &db);
2007 return (err ? NULL : db);
2011 dbuf_create_bonus(dnode_t *dn)
2013 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
2015 ASSERT(dn->dn_bonus == NULL);
2016 dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL);
2020 dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx)
2022 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2025 if (db->db_blkid != DMU_SPILL_BLKID)
2026 return (SET_ERROR(ENOTSUP));
2028 blksz = SPA_MINBLOCKSIZE;
2029 if (blksz > SPA_MAXBLOCKSIZE)
2030 blksz = SPA_MAXBLOCKSIZE;
2032 blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE);
2036 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2037 dbuf_new_size(db, blksz, tx);
2038 rw_exit(&dn->dn_struct_rwlock);
2045 dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx)
2047 dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx);
2050 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2052 dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
2054 int64_t holds = refcount_add(&db->db_holds, tag);
2059 * If you call dbuf_rele() you had better not be referencing the dnode handle
2060 * unless you have some other direct or indirect hold on the dnode. (An indirect
2061 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2062 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2063 * dnode's parent dbuf evicting its dnode handles.
2066 dbuf_rele(dmu_buf_impl_t *db, void *tag)
2068 mutex_enter(&db->db_mtx);
2069 dbuf_rele_and_unlock(db, tag);
2073 dmu_buf_rele(dmu_buf_t *db, void *tag)
2075 dbuf_rele((dmu_buf_impl_t *)db, tag);
2079 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2080 * db_dirtycnt and db_holds to be updated atomically.
2083 dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag)
2087 ASSERT(MUTEX_HELD(&db->db_mtx));
2091 * Remove the reference to the dbuf before removing its hold on the
2092 * dnode so we can guarantee in dnode_move() that a referenced bonus
2093 * buffer has a corresponding dnode hold.
2095 holds = refcount_remove(&db->db_holds, tag);
2099 * We can't freeze indirects if there is a possibility that they
2100 * may be modified in the current syncing context.
2102 if (db->db_buf && holds == (db->db_level == 0 ? db->db_dirtycnt : 0))
2103 arc_buf_freeze(db->db_buf);
2105 if (holds == db->db_dirtycnt &&
2106 db->db_level == 0 && db->db_immediate_evict)
2107 dbuf_evict_user(db);
2110 if (db->db_blkid == DMU_BONUS_BLKID) {
2111 mutex_exit(&db->db_mtx);
2114 * If the dnode moves here, we cannot cross this barrier
2115 * until the move completes.
2118 (void) atomic_dec_32_nv(&DB_DNODE(db)->dn_dbufs_count);
2121 * The bonus buffer's dnode hold is no longer discounted
2122 * in dnode_move(). The dnode cannot move until after
2125 dnode_rele(DB_DNODE(db), db);
2126 } else if (db->db_buf == NULL) {
2128 * This is a special case: we never associated this
2129 * dbuf with any data allocated from the ARC.
2131 ASSERT(db->db_state == DB_UNCACHED ||
2132 db->db_state == DB_NOFILL);
2134 } else if (arc_released(db->db_buf)) {
2135 arc_buf_t *buf = db->db_buf;
2137 * This dbuf has anonymous data associated with it.
2139 dbuf_set_data(db, NULL);
2140 VERIFY(arc_buf_remove_ref(buf, db));
2143 VERIFY(!arc_buf_remove_ref(db->db_buf, db));
2146 * A dbuf will be eligible for eviction if either the
2147 * 'primarycache' property is set or a duplicate
2148 * copy of this buffer is already cached in the arc.
2150 * In the case of the 'primarycache' a buffer
2151 * is considered for eviction if it matches the
2152 * criteria set in the property.
2154 * To decide if our buffer is considered a
2155 * duplicate, we must call into the arc to determine
2156 * if multiple buffers are referencing the same
2157 * block on-disk. If so, then we simply evict
2160 if (!DBUF_IS_CACHEABLE(db)) {
2161 if (db->db_blkptr != NULL &&
2162 !BP_IS_HOLE(db->db_blkptr) &&
2163 !BP_IS_EMBEDDED(db->db_blkptr)) {
2165 dmu_objset_spa(db->db_objset);
2166 blkptr_t bp = *db->db_blkptr;
2168 arc_freed(spa, &bp);
2172 } else if (arc_buf_eviction_needed(db->db_buf)) {
2175 mutex_exit(&db->db_mtx);
2179 mutex_exit(&db->db_mtx);
2183 #pragma weak dmu_buf_refcount = dbuf_refcount
2185 dbuf_refcount(dmu_buf_impl_t *db)
2187 return (refcount_count(&db->db_holds));
2191 dmu_buf_set_user(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr,
2192 dmu_buf_evict_func_t *evict_func)
2194 return (dmu_buf_update_user(db_fake, NULL, user_ptr,
2195 user_data_ptr_ptr, evict_func));
2199 dmu_buf_set_user_ie(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr,
2200 dmu_buf_evict_func_t *evict_func)
2202 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2204 db->db_immediate_evict = TRUE;
2205 return (dmu_buf_update_user(db_fake, NULL, user_ptr,
2206 user_data_ptr_ptr, evict_func));
2210 dmu_buf_update_user(dmu_buf_t *db_fake, void *old_user_ptr, void *user_ptr,
2211 void *user_data_ptr_ptr, dmu_buf_evict_func_t *evict_func)
2213 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2214 ASSERT(db->db_level == 0);
2216 ASSERT((user_ptr == NULL) == (evict_func == NULL));
2218 mutex_enter(&db->db_mtx);
2220 if (db->db_user_ptr == old_user_ptr) {
2221 db->db_user_ptr = user_ptr;
2222 db->db_user_data_ptr_ptr = user_data_ptr_ptr;
2223 db->db_evict_func = evict_func;
2225 dbuf_update_data(db);
2227 old_user_ptr = db->db_user_ptr;
2230 mutex_exit(&db->db_mtx);
2231 return (old_user_ptr);
2235 dmu_buf_get_user(dmu_buf_t *db_fake)
2237 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2238 ASSERT(!refcount_is_zero(&db->db_holds));
2240 return (db->db_user_ptr);
2244 dmu_buf_freeable(dmu_buf_t *dbuf)
2246 boolean_t res = B_FALSE;
2247 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
2250 res = dsl_dataset_block_freeable(db->db_objset->os_dsl_dataset,
2251 db->db_blkptr, db->db_blkptr->blk_birth);
2257 dmu_buf_get_blkptr(dmu_buf_t *db)
2259 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2260 return (dbi->db_blkptr);
2264 dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db)
2266 /* ASSERT(dmu_tx_is_syncing(tx) */
2267 ASSERT(MUTEX_HELD(&db->db_mtx));
2269 if (db->db_blkptr != NULL)
2272 if (db->db_blkid == DMU_SPILL_BLKID) {
2273 db->db_blkptr = &dn->dn_phys->dn_spill;
2274 BP_ZERO(db->db_blkptr);
2277 if (db->db_level == dn->dn_phys->dn_nlevels-1) {
2279 * This buffer was allocated at a time when there was
2280 * no available blkptrs from the dnode, or it was
2281 * inappropriate to hook it in (i.e., nlevels mis-match).
2283 ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr);
2284 ASSERT(db->db_parent == NULL);
2285 db->db_parent = dn->dn_dbuf;
2286 db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
2289 dmu_buf_impl_t *parent = db->db_parent;
2290 int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2292 ASSERT(dn->dn_phys->dn_nlevels > 1);
2293 if (parent == NULL) {
2294 mutex_exit(&db->db_mtx);
2295 rw_enter(&dn->dn_struct_rwlock, RW_READER);
2296 (void) dbuf_hold_impl(dn, db->db_level+1,
2297 db->db_blkid >> epbs, FALSE, db, &parent);
2298 rw_exit(&dn->dn_struct_rwlock);
2299 mutex_enter(&db->db_mtx);
2300 db->db_parent = parent;
2302 db->db_blkptr = (blkptr_t *)parent->db.db_data +
2303 (db->db_blkid & ((1ULL << epbs) - 1));
2309 dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2311 dmu_buf_impl_t *db = dr->dr_dbuf;
2315 ASSERT(dmu_tx_is_syncing(tx));
2317 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2319 mutex_enter(&db->db_mtx);
2321 ASSERT(db->db_level > 0);
2324 /* Read the block if it hasn't been read yet. */
2325 if (db->db_buf == NULL) {
2326 mutex_exit(&db->db_mtx);
2327 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
2328 mutex_enter(&db->db_mtx);
2330 ASSERT3U(db->db_state, ==, DB_CACHED);
2331 ASSERT(db->db_buf != NULL);
2335 /* Indirect block size must match what the dnode thinks it is. */
2336 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2337 dbuf_check_blkptr(dn, db);
2340 /* Provide the pending dirty record to child dbufs */
2341 db->db_data_pending = dr;
2343 mutex_exit(&db->db_mtx);
2344 dbuf_write(dr, db->db_buf, tx);
2347 mutex_enter(&dr->dt.di.dr_mtx);
2348 dbuf_sync_list(&dr->dt.di.dr_children, tx);
2349 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
2350 mutex_exit(&dr->dt.di.dr_mtx);
2355 dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2357 arc_buf_t **datap = &dr->dt.dl.dr_data;
2358 dmu_buf_impl_t *db = dr->dr_dbuf;
2361 uint64_t txg = tx->tx_txg;
2363 ASSERT(dmu_tx_is_syncing(tx));
2365 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2367 mutex_enter(&db->db_mtx);
2369 * To be synced, we must be dirtied. But we
2370 * might have been freed after the dirty.
2372 if (db->db_state == DB_UNCACHED) {
2373 /* This buffer has been freed since it was dirtied */
2374 ASSERT(db->db.db_data == NULL);
2375 } else if (db->db_state == DB_FILL) {
2376 /* This buffer was freed and is now being re-filled */
2377 ASSERT(db->db.db_data != dr->dt.dl.dr_data);
2379 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL);
2386 if (db->db_blkid == DMU_SPILL_BLKID) {
2387 mutex_enter(&dn->dn_mtx);
2388 dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR;
2389 mutex_exit(&dn->dn_mtx);
2393 * If this is a bonus buffer, simply copy the bonus data into the
2394 * dnode. It will be written out when the dnode is synced (and it
2395 * will be synced, since it must have been dirty for dbuf_sync to
2398 if (db->db_blkid == DMU_BONUS_BLKID) {
2399 dbuf_dirty_record_t **drp;
2401 ASSERT(*datap != NULL);
2402 ASSERT0(db->db_level);
2403 ASSERT3U(dn->dn_phys->dn_bonuslen, <=, DN_MAX_BONUSLEN);
2404 bcopy(*datap, DN_BONUS(dn->dn_phys), dn->dn_phys->dn_bonuslen);
2407 if (*datap != db->db.db_data) {
2408 zio_buf_free(*datap, DN_MAX_BONUSLEN);
2409 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
2411 db->db_data_pending = NULL;
2412 drp = &db->db_last_dirty;
2414 drp = &(*drp)->dr_next;
2415 ASSERT(dr->dr_next == NULL);
2416 ASSERT(dr->dr_dbuf == db);
2418 if (dr->dr_dbuf->db_level != 0) {
2419 list_destroy(&dr->dt.di.dr_children);
2420 mutex_destroy(&dr->dt.di.dr_mtx);
2422 kmem_free(dr, sizeof (dbuf_dirty_record_t));
2423 ASSERT(db->db_dirtycnt > 0);
2424 db->db_dirtycnt -= 1;
2425 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
2432 * This function may have dropped the db_mtx lock allowing a dmu_sync
2433 * operation to sneak in. As a result, we need to ensure that we
2434 * don't check the dr_override_state until we have returned from
2435 * dbuf_check_blkptr.
2437 dbuf_check_blkptr(dn, db);
2440 * If this buffer is in the middle of an immediate write,
2441 * wait for the synchronous IO to complete.
2443 while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
2444 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
2445 cv_wait(&db->db_changed, &db->db_mtx);
2446 ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN);
2449 if (db->db_state != DB_NOFILL &&
2450 dn->dn_object != DMU_META_DNODE_OBJECT &&
2451 refcount_count(&db->db_holds) > 1 &&
2452 dr->dt.dl.dr_override_state != DR_OVERRIDDEN &&
2453 *datap == db->db_buf) {
2455 * If this buffer is currently "in use" (i.e., there
2456 * are active holds and db_data still references it),
2457 * then make a copy before we start the write so that
2458 * any modifications from the open txg will not leak
2461 * NOTE: this copy does not need to be made for
2462 * objects only modified in the syncing context (e.g.
2463 * DNONE_DNODE blocks).
2465 int blksz = arc_buf_size(*datap);
2466 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
2467 *datap = arc_buf_alloc(os->os_spa, blksz, db, type);
2468 bcopy(db->db.db_data, (*datap)->b_data, blksz);
2470 db->db_data_pending = dr;
2472 mutex_exit(&db->db_mtx);
2474 dbuf_write(dr, *datap, tx);
2476 ASSERT(!list_link_active(&dr->dr_dirty_node));
2477 if (dn->dn_object == DMU_META_DNODE_OBJECT) {
2478 list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr);
2482 * Although zio_nowait() does not "wait for an IO", it does
2483 * initiate the IO. If this is an empty write it seems plausible
2484 * that the IO could actually be completed before the nowait
2485 * returns. We need to DB_DNODE_EXIT() first in case
2486 * zio_nowait() invalidates the dbuf.
2489 zio_nowait(dr->dr_zio);
2494 dbuf_sync_list(list_t *list, dmu_tx_t *tx)
2496 dbuf_dirty_record_t *dr;
2498 while (dr = list_head(list)) {
2499 if (dr->dr_zio != NULL) {
2501 * If we find an already initialized zio then we
2502 * are processing the meta-dnode, and we have finished.
2503 * The dbufs for all dnodes are put back on the list
2504 * during processing, so that we can zio_wait()
2505 * these IOs after initiating all child IOs.
2507 ASSERT3U(dr->dr_dbuf->db.db_object, ==,
2508 DMU_META_DNODE_OBJECT);
2511 list_remove(list, dr);
2512 if (dr->dr_dbuf->db_level > 0)
2513 dbuf_sync_indirect(dr, tx);
2515 dbuf_sync_leaf(dr, tx);
2521 dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
2523 dmu_buf_impl_t *db = vdb;
2525 blkptr_t *bp = zio->io_bp;
2526 blkptr_t *bp_orig = &zio->io_bp_orig;
2527 spa_t *spa = zio->io_spa;
2532 ASSERT3P(db->db_blkptr, ==, bp);
2536 delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig);
2537 dnode_diduse_space(dn, delta - zio->io_prev_space_delta);
2538 zio->io_prev_space_delta = delta;
2540 if (bp->blk_birth != 0) {
2541 ASSERT((db->db_blkid != DMU_SPILL_BLKID &&
2542 BP_GET_TYPE(bp) == dn->dn_type) ||
2543 (db->db_blkid == DMU_SPILL_BLKID &&
2544 BP_GET_TYPE(bp) == dn->dn_bonustype) ||
2545 BP_IS_EMBEDDED(bp));
2546 ASSERT(BP_GET_LEVEL(bp) == db->db_level);
2549 mutex_enter(&db->db_mtx);
2552 if (db->db_blkid == DMU_SPILL_BLKID) {
2553 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
2554 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
2555 db->db_blkptr == &dn->dn_phys->dn_spill);
2559 if (db->db_level == 0) {
2560 mutex_enter(&dn->dn_mtx);
2561 if (db->db_blkid > dn->dn_phys->dn_maxblkid &&
2562 db->db_blkid != DMU_SPILL_BLKID)
2563 dn->dn_phys->dn_maxblkid = db->db_blkid;
2564 mutex_exit(&dn->dn_mtx);
2566 if (dn->dn_type == DMU_OT_DNODE) {
2567 dnode_phys_t *dnp = db->db.db_data;
2568 for (i = db->db.db_size >> DNODE_SHIFT; i > 0;
2570 if (dnp->dn_type != DMU_OT_NONE)
2574 if (BP_IS_HOLE(bp)) {
2581 blkptr_t *ibp = db->db.db_data;
2582 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2583 for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) {
2584 if (BP_IS_HOLE(ibp))
2586 fill += BP_GET_FILL(ibp);
2591 if (!BP_IS_EMBEDDED(bp))
2592 bp->blk_fill = fill;
2594 mutex_exit(&db->db_mtx);
2598 * The SPA will call this callback several times for each zio - once
2599 * for every physical child i/o (zio->io_phys_children times). This
2600 * allows the DMU to monitor the progress of each logical i/o. For example,
2601 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
2602 * block. There may be a long delay before all copies/fragments are completed,
2603 * so this callback allows us to retire dirty space gradually, as the physical
2608 dbuf_write_physdone(zio_t *zio, arc_buf_t *buf, void *arg)
2610 dmu_buf_impl_t *db = arg;
2611 objset_t *os = db->db_objset;
2612 dsl_pool_t *dp = dmu_objset_pool(os);
2613 dbuf_dirty_record_t *dr;
2616 dr = db->db_data_pending;
2617 ASSERT3U(dr->dr_txg, ==, zio->io_txg);
2620 * The callback will be called io_phys_children times. Retire one
2621 * portion of our dirty space each time we are called. Any rounding
2622 * error will be cleaned up by dsl_pool_sync()'s call to
2623 * dsl_pool_undirty_space().
2625 delta = dr->dr_accounted / zio->io_phys_children;
2626 dsl_pool_undirty_space(dp, delta, zio->io_txg);
2631 dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
2633 dmu_buf_impl_t *db = vdb;
2634 blkptr_t *bp_orig = &zio->io_bp_orig;
2635 blkptr_t *bp = db->db_blkptr;
2636 objset_t *os = db->db_objset;
2637 dmu_tx_t *tx = os->os_synctx;
2638 dbuf_dirty_record_t **drp, *dr;
2640 ASSERT0(zio->io_error);
2641 ASSERT(db->db_blkptr == bp);
2644 * For nopwrites and rewrites we ensure that the bp matches our
2645 * original and bypass all the accounting.
2647 if (zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)) {
2648 ASSERT(BP_EQUAL(bp, bp_orig));
2650 dsl_dataset_t *ds = os->os_dsl_dataset;
2651 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
2652 dsl_dataset_block_born(ds, bp, tx);
2655 mutex_enter(&db->db_mtx);
2659 drp = &db->db_last_dirty;
2660 while ((dr = *drp) != db->db_data_pending)
2662 ASSERT(!list_link_active(&dr->dr_dirty_node));
2663 ASSERT(dr->dr_dbuf == db);
2664 ASSERT(dr->dr_next == NULL);
2668 if (db->db_blkid == DMU_SPILL_BLKID) {
2673 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
2674 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
2675 db->db_blkptr == &dn->dn_phys->dn_spill);
2680 if (db->db_level == 0) {
2681 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2682 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
2683 if (db->db_state != DB_NOFILL) {
2684 if (dr->dt.dl.dr_data != db->db_buf)
2685 VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data,
2687 else if (!arc_released(db->db_buf))
2688 arc_set_callback(db->db_buf, dbuf_do_evict, db);
2695 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
2696 ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
2697 if (!BP_IS_HOLE(db->db_blkptr)) {
2699 dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2700 ASSERT3U(db->db_blkid, <=,
2701 dn->dn_phys->dn_maxblkid >> (db->db_level * epbs));
2702 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
2704 if (!arc_released(db->db_buf))
2705 arc_set_callback(db->db_buf, dbuf_do_evict, db);
2708 mutex_destroy(&dr->dt.di.dr_mtx);
2709 list_destroy(&dr->dt.di.dr_children);
2711 kmem_free(dr, sizeof (dbuf_dirty_record_t));
2713 cv_broadcast(&db->db_changed);
2714 ASSERT(db->db_dirtycnt > 0);
2715 db->db_dirtycnt -= 1;
2716 db->db_data_pending = NULL;
2717 dbuf_rele_and_unlock(db, (void *)(uintptr_t)tx->tx_txg);
2721 dbuf_write_nofill_ready(zio_t *zio)
2723 dbuf_write_ready(zio, NULL, zio->io_private);
2727 dbuf_write_nofill_done(zio_t *zio)
2729 dbuf_write_done(zio, NULL, zio->io_private);
2733 dbuf_write_override_ready(zio_t *zio)
2735 dbuf_dirty_record_t *dr = zio->io_private;
2736 dmu_buf_impl_t *db = dr->dr_dbuf;
2738 dbuf_write_ready(zio, NULL, db);
2742 dbuf_write_override_done(zio_t *zio)
2744 dbuf_dirty_record_t *dr = zio->io_private;
2745 dmu_buf_impl_t *db = dr->dr_dbuf;
2746 blkptr_t *obp = &dr->dt.dl.dr_overridden_by;
2748 mutex_enter(&db->db_mtx);
2749 if (!BP_EQUAL(zio->io_bp, obp)) {
2750 if (!BP_IS_HOLE(obp))
2751 dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp);
2752 arc_release(dr->dt.dl.dr_data, db);
2754 mutex_exit(&db->db_mtx);
2756 dbuf_write_done(zio, NULL, db);
2759 /* Issue I/O to commit a dirty buffer to disk. */
2761 dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx)
2763 dmu_buf_impl_t *db = dr->dr_dbuf;
2766 dmu_buf_impl_t *parent = db->db_parent;
2767 uint64_t txg = tx->tx_txg;
2768 zbookmark_phys_t zb;
2777 if (db->db_state != DB_NOFILL) {
2778 if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) {
2780 * Private object buffers are released here rather
2781 * than in dbuf_dirty() since they are only modified
2782 * in the syncing context and we don't want the
2783 * overhead of making multiple copies of the data.
2785 if (BP_IS_HOLE(db->db_blkptr)) {
2788 dbuf_release_bp(db);
2793 if (parent != dn->dn_dbuf) {
2794 /* Our parent is an indirect block. */
2795 /* We have a dirty parent that has been scheduled for write. */
2796 ASSERT(parent && parent->db_data_pending);
2797 /* Our parent's buffer is one level closer to the dnode. */
2798 ASSERT(db->db_level == parent->db_level-1);
2800 * We're about to modify our parent's db_data by modifying
2801 * our block pointer, so the parent must be released.
2803 ASSERT(arc_released(parent->db_buf));
2804 zio = parent->db_data_pending->dr_zio;
2806 /* Our parent is the dnode itself. */
2807 ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 &&
2808 db->db_blkid != DMU_SPILL_BLKID) ||
2809 (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0));
2810 if (db->db_blkid != DMU_SPILL_BLKID)
2811 ASSERT3P(db->db_blkptr, ==,
2812 &dn->dn_phys->dn_blkptr[db->db_blkid]);
2816 ASSERT(db->db_level == 0 || data == db->db_buf);
2817 ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
2820 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
2821 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
2822 db->db.db_object, db->db_level, db->db_blkid);
2824 if (db->db_blkid == DMU_SPILL_BLKID)
2826 wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
2828 dmu_write_policy(os, dn, db->db_level, wp_flag, &zp);
2831 if (db->db_level == 0 &&
2832 dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
2834 * The BP for this block has been provided by open context
2835 * (by dmu_sync() or dmu_buf_write_embedded()).
2837 void *contents = (data != NULL) ? data->b_data : NULL;
2839 dr->dr_zio = zio_write(zio, os->os_spa, txg,
2840 db->db_blkptr, contents, db->db.db_size, &zp,
2841 dbuf_write_override_ready, NULL, dbuf_write_override_done,
2842 dr, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
2843 mutex_enter(&db->db_mtx);
2844 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
2845 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by,
2846 dr->dt.dl.dr_copies, dr->dt.dl.dr_nopwrite);
2847 mutex_exit(&db->db_mtx);
2848 } else if (db->db_state == DB_NOFILL) {
2849 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF ||
2850 zp.zp_checksum == ZIO_CHECKSUM_NOPARITY);
2851 dr->dr_zio = zio_write(zio, os->os_spa, txg,
2852 db->db_blkptr, NULL, db->db.db_size, &zp,
2853 dbuf_write_nofill_ready, NULL, dbuf_write_nofill_done, db,
2854 ZIO_PRIORITY_ASYNC_WRITE,
2855 ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb);
2857 ASSERT(arc_released(data));
2858 dr->dr_zio = arc_write(zio, os->os_spa, txg,
2859 db->db_blkptr, data, DBUF_IS_L2CACHEABLE(db),
2860 DBUF_IS_L2COMPRESSIBLE(db), &zp, dbuf_write_ready,
2861 dbuf_write_physdone, dbuf_write_done, db,
2862 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);