xref: /titanic_50/usr/src/uts/common/fs/zfs/dbuf.c (revision a237e38e9161f0acd6451439d4a7dd597e66291d)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/zfs_context.h>
29 #include <sys/dmu.h>
30 #include <sys/dmu_impl.h>
31 #include <sys/dbuf.h>
32 #include <sys/dmu_objset.h>
33 #include <sys/dsl_dataset.h>
34 #include <sys/dsl_dir.h>
35 #include <sys/dmu_tx.h>
36 #include <sys/spa.h>
37 #include <sys/zio.h>
38 #include <sys/dmu_zfetch.h>
39 
40 static void dbuf_destroy(dmu_buf_impl_t *db);
41 static int dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
42 static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, int checksum,
43     int compress, dmu_tx_t *tx);
44 static arc_done_func_t dbuf_write_ready;
45 static arc_done_func_t dbuf_write_done;
46 
47 int zfs_mdcomp_disable = 0;
48 
49 /*
50  * Global data structures and functions for the dbuf cache.
51  */
52 static kmem_cache_t *dbuf_cache;
53 
54 /* ARGSUSED */
55 static int
56 dbuf_cons(void *vdb, void *unused, int kmflag)
57 {
58 	dmu_buf_impl_t *db = vdb;
59 	bzero(db, sizeof (dmu_buf_impl_t));
60 
61 	mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL);
62 	cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL);
63 	refcount_create(&db->db_holds);
64 	return (0);
65 }
66 
67 /* ARGSUSED */
68 static void
69 dbuf_dest(void *vdb, void *unused)
70 {
71 	dmu_buf_impl_t *db = vdb;
72 	mutex_destroy(&db->db_mtx);
73 	cv_destroy(&db->db_changed);
74 	refcount_destroy(&db->db_holds);
75 }
76 
77 /*
78  * dbuf hash table routines
79  */
80 static dbuf_hash_table_t dbuf_hash_table;
81 
82 static uint64_t dbuf_hash_count;
83 
84 static uint64_t
85 dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid)
86 {
87 	uintptr_t osv = (uintptr_t)os;
88 	uint64_t crc = -1ULL;
89 
90 	ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
91 	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (lvl)) & 0xFF];
92 	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF];
93 	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF];
94 	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF];
95 	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 0)) & 0xFF];
96 	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 8)) & 0xFF];
97 
98 	crc ^= (osv>>14) ^ (obj>>16) ^ (blkid>>16);
99 
100 	return (crc);
101 }
102 
103 #define	DBUF_HASH(os, obj, level, blkid) dbuf_hash(os, obj, level, blkid);
104 
105 #define	DBUF_EQUAL(dbuf, os, obj, level, blkid)		\
106 	((dbuf)->db.db_object == (obj) &&		\
107 	(dbuf)->db_objset == (os) &&			\
108 	(dbuf)->db_level == (level) &&			\
109 	(dbuf)->db_blkid == (blkid))
110 
111 dmu_buf_impl_t *
112 dbuf_find(dnode_t *dn, uint8_t level, uint64_t blkid)
113 {
114 	dbuf_hash_table_t *h = &dbuf_hash_table;
115 	objset_impl_t *os = dn->dn_objset;
116 	uint64_t obj = dn->dn_object;
117 	uint64_t hv = DBUF_HASH(os, obj, level, blkid);
118 	uint64_t idx = hv & h->hash_table_mask;
119 	dmu_buf_impl_t *db;
120 
121 	mutex_enter(DBUF_HASH_MUTEX(h, idx));
122 	for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) {
123 		if (DBUF_EQUAL(db, os, obj, level, blkid)) {
124 			mutex_enter(&db->db_mtx);
125 			if (db->db_state != DB_EVICTING) {
126 				mutex_exit(DBUF_HASH_MUTEX(h, idx));
127 				return (db);
128 			}
129 			mutex_exit(&db->db_mtx);
130 		}
131 	}
132 	mutex_exit(DBUF_HASH_MUTEX(h, idx));
133 	return (NULL);
134 }
135 
136 /*
137  * Insert an entry into the hash table.  If there is already an element
138  * equal to elem in the hash table, then the already existing element
139  * will be returned and the new element will not be inserted.
140  * Otherwise returns NULL.
141  */
142 static dmu_buf_impl_t *
143 dbuf_hash_insert(dmu_buf_impl_t *db)
144 {
145 	dbuf_hash_table_t *h = &dbuf_hash_table;
146 	objset_impl_t *os = db->db_objset;
147 	uint64_t obj = db->db.db_object;
148 	int level = db->db_level;
149 	uint64_t blkid = db->db_blkid;
150 	uint64_t hv = DBUF_HASH(os, obj, level, blkid);
151 	uint64_t idx = hv & h->hash_table_mask;
152 	dmu_buf_impl_t *dbf;
153 
154 	mutex_enter(DBUF_HASH_MUTEX(h, idx));
155 	for (dbf = h->hash_table[idx]; dbf != NULL; dbf = dbf->db_hash_next) {
156 		if (DBUF_EQUAL(dbf, os, obj, level, blkid)) {
157 			mutex_enter(&dbf->db_mtx);
158 			if (dbf->db_state != DB_EVICTING) {
159 				mutex_exit(DBUF_HASH_MUTEX(h, idx));
160 				return (dbf);
161 			}
162 			mutex_exit(&dbf->db_mtx);
163 		}
164 	}
165 
166 	mutex_enter(&db->db_mtx);
167 	db->db_hash_next = h->hash_table[idx];
168 	h->hash_table[idx] = db;
169 	mutex_exit(DBUF_HASH_MUTEX(h, idx));
170 	atomic_add_64(&dbuf_hash_count, 1);
171 
172 	return (NULL);
173 }
174 
175 /*
176  * Remove an entry from the hash table.  This operation will
177  * fail if there are any existing holds on the db.
178  */
179 static void
180 dbuf_hash_remove(dmu_buf_impl_t *db)
181 {
182 	dbuf_hash_table_t *h = &dbuf_hash_table;
183 	uint64_t hv = DBUF_HASH(db->db_objset, db->db.db_object,
184 	    db->db_level, db->db_blkid);
185 	uint64_t idx = hv & h->hash_table_mask;
186 	dmu_buf_impl_t *dbf, **dbp;
187 
188 	/*
189 	 * We musn't hold db_mtx to maintin lock ordering:
190 	 * DBUF_HASH_MUTEX > db_mtx.
191 	 */
192 	ASSERT(refcount_is_zero(&db->db_holds));
193 	ASSERT(db->db_state == DB_EVICTING);
194 	ASSERT(!MUTEX_HELD(&db->db_mtx));
195 
196 	mutex_enter(DBUF_HASH_MUTEX(h, idx));
197 	dbp = &h->hash_table[idx];
198 	while ((dbf = *dbp) != db) {
199 		dbp = &dbf->db_hash_next;
200 		ASSERT(dbf != NULL);
201 	}
202 	*dbp = db->db_hash_next;
203 	db->db_hash_next = NULL;
204 	mutex_exit(DBUF_HASH_MUTEX(h, idx));
205 	atomic_add_64(&dbuf_hash_count, -1);
206 }
207 
208 static arc_evict_func_t dbuf_do_evict;
209 
210 static void
211 dbuf_evict_user(dmu_buf_impl_t *db)
212 {
213 	ASSERT(MUTEX_HELD(&db->db_mtx));
214 
215 	if (db->db_level != 0 || db->db_evict_func == NULL)
216 		return;
217 
218 	if (db->db_user_data_ptr_ptr)
219 		*db->db_user_data_ptr_ptr = db->db.db_data;
220 	db->db_evict_func(&db->db, db->db_user_ptr);
221 	db->db_user_ptr = NULL;
222 	db->db_user_data_ptr_ptr = NULL;
223 	db->db_evict_func = NULL;
224 }
225 
226 void
227 dbuf_evict(dmu_buf_impl_t *db)
228 {
229 	ASSERT(MUTEX_HELD(&db->db_mtx));
230 	ASSERT(db->db_buf == NULL);
231 	ASSERT(db->db_data_pending == NULL);
232 
233 	dbuf_clear(db);
234 	dbuf_destroy(db);
235 }
236 
237 void
238 dbuf_init(void)
239 {
240 	uint64_t hsize = 1ULL << 16;
241 	dbuf_hash_table_t *h = &dbuf_hash_table;
242 	int i;
243 
244 	/*
245 	 * The hash table is big enough to fill all of physical memory
246 	 * with an average 4K block size.  The table will take up
247 	 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
248 	 */
249 	while (hsize * 4096 < physmem * PAGESIZE)
250 		hsize <<= 1;
251 
252 retry:
253 	h->hash_table_mask = hsize - 1;
254 	h->hash_table = kmem_zalloc(hsize * sizeof (void *), KM_NOSLEEP);
255 	if (h->hash_table == NULL) {
256 		/* XXX - we should really return an error instead of assert */
257 		ASSERT(hsize > (1ULL << 10));
258 		hsize >>= 1;
259 		goto retry;
260 	}
261 
262 	dbuf_cache = kmem_cache_create("dmu_buf_impl_t",
263 	    sizeof (dmu_buf_impl_t),
264 	    0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0);
265 
266 	for (i = 0; i < DBUF_MUTEXES; i++)
267 		mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL);
268 }
269 
270 void
271 dbuf_fini(void)
272 {
273 	dbuf_hash_table_t *h = &dbuf_hash_table;
274 	int i;
275 
276 	for (i = 0; i < DBUF_MUTEXES; i++)
277 		mutex_destroy(&h->hash_mutexes[i]);
278 	kmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
279 	kmem_cache_destroy(dbuf_cache);
280 }
281 
282 /*
283  * Other stuff.
284  */
285 
286 #ifdef ZFS_DEBUG
287 static void
288 dbuf_verify(dmu_buf_impl_t *db)
289 {
290 	dnode_t *dn = db->db_dnode;
291 
292 	ASSERT(MUTEX_HELD(&db->db_mtx));
293 
294 	if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY))
295 		return;
296 
297 	ASSERT(db->db_objset != NULL);
298 	if (dn == NULL) {
299 		ASSERT(db->db_parent == NULL);
300 		ASSERT(db->db_blkptr == NULL);
301 	} else {
302 		ASSERT3U(db->db.db_object, ==, dn->dn_object);
303 		ASSERT3P(db->db_objset, ==, dn->dn_objset);
304 		ASSERT3U(db->db_level, <, dn->dn_nlevels);
305 		ASSERT(db->db_blkid == DB_BONUS_BLKID ||
306 		    list_head(&dn->dn_dbufs));
307 	}
308 	if (db->db_blkid == DB_BONUS_BLKID) {
309 		ASSERT(dn != NULL);
310 		ASSERT3U(db->db.db_size, ==, dn->dn_bonuslen);
311 		ASSERT3U(db->db.db_offset, ==, DB_BONUS_BLKID);
312 	} else {
313 		ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size);
314 	}
315 
316 	if (db->db_level == 0) {
317 		/* we can be momentarily larger in dnode_set_blksz() */
318 		if (db->db_blkid != DB_BONUS_BLKID && dn) {
319 			ASSERT3U(db->db.db_size, >=, dn->dn_datablksz);
320 		}
321 		if (db->db.db_object == DMU_META_DNODE_OBJECT) {
322 			dbuf_dirty_record_t *dr = db->db_data_pending;
323 			/*
324 			 * it should only be modified in syncing
325 			 * context, so make sure we only have
326 			 * one copy of the data.
327 			 */
328 			ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf);
329 		}
330 	}
331 
332 	/* verify db->db_blkptr */
333 	if (db->db_blkptr) {
334 		if (db->db_parent == dn->dn_dbuf) {
335 			/* db is pointed to by the dnode */
336 			/* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
337 			if (db->db.db_object == DMU_META_DNODE_OBJECT)
338 				ASSERT(db->db_parent == NULL);
339 			else
340 				ASSERT(db->db_parent != NULL);
341 			ASSERT3P(db->db_blkptr, ==,
342 			    &dn->dn_phys->dn_blkptr[db->db_blkid]);
343 		} else {
344 			/* db is pointed to by an indirect block */
345 			int epb = db->db_parent->db.db_size >> SPA_BLKPTRSHIFT;
346 			ASSERT3U(db->db_parent->db_level, ==, db->db_level+1);
347 			ASSERT3U(db->db_parent->db.db_object, ==,
348 			    db->db.db_object);
349 			/*
350 			 * dnode_grow_indblksz() can make this fail if we don't
351 			 * have the struct_rwlock.  XXX indblksz no longer
352 			 * grows.  safe to do this now?
353 			 */
354 			if (RW_WRITE_HELD(&db->db_dnode->dn_struct_rwlock)) {
355 				ASSERT3P(db->db_blkptr, ==,
356 				    ((blkptr_t *)db->db_parent->db.db_data +
357 				    db->db_blkid % epb));
358 			}
359 		}
360 	}
361 	if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) &&
362 	    db->db.db_data && db->db_blkid != DB_BONUS_BLKID &&
363 	    db->db_state != DB_FILL && !dn->dn_free_txg) {
364 		/*
365 		 * If the blkptr isn't set but they have nonzero data,
366 		 * it had better be dirty, otherwise we'll lose that
367 		 * data when we evict this buffer.
368 		 */
369 		if (db->db_dirtycnt == 0) {
370 			uint64_t *buf = db->db.db_data;
371 			int i;
372 
373 			for (i = 0; i < db->db.db_size >> 3; i++) {
374 				ASSERT(buf[i] == 0);
375 			}
376 		}
377 	}
378 }
379 #endif
380 
381 static void
382 dbuf_update_data(dmu_buf_impl_t *db)
383 {
384 	ASSERT(MUTEX_HELD(&db->db_mtx));
385 	if (db->db_level == 0 && db->db_user_data_ptr_ptr) {
386 		ASSERT(!refcount_is_zero(&db->db_holds));
387 		*db->db_user_data_ptr_ptr = db->db.db_data;
388 	}
389 }
390 
391 static void
392 dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf)
393 {
394 	ASSERT(MUTEX_HELD(&db->db_mtx));
395 	ASSERT(db->db_buf == NULL || !arc_has_callback(db->db_buf));
396 	db->db_buf = buf;
397 	if (buf != NULL) {
398 		ASSERT(buf->b_data != NULL);
399 		db->db.db_data = buf->b_data;
400 		if (!arc_released(buf))
401 			arc_set_callback(buf, dbuf_do_evict, db);
402 		dbuf_update_data(db);
403 	} else {
404 		dbuf_evict_user(db);
405 		db->db.db_data = NULL;
406 		db->db_state = DB_UNCACHED;
407 	}
408 }
409 
410 uint64_t
411 dbuf_whichblock(dnode_t *dn, uint64_t offset)
412 {
413 	if (dn->dn_datablkshift) {
414 		return (offset >> dn->dn_datablkshift);
415 	} else {
416 		ASSERT3U(offset, <, dn->dn_datablksz);
417 		return (0);
418 	}
419 }
420 
421 static void
422 dbuf_read_done(zio_t *zio, arc_buf_t *buf, void *vdb)
423 {
424 	dmu_buf_impl_t *db = vdb;
425 
426 	mutex_enter(&db->db_mtx);
427 	ASSERT3U(db->db_state, ==, DB_READ);
428 	/*
429 	 * All reads are synchronous, so we must have a hold on the dbuf
430 	 */
431 	ASSERT(refcount_count(&db->db_holds) > 0);
432 	ASSERT(db->db_buf == NULL);
433 	ASSERT(db->db.db_data == NULL);
434 	if (db->db_level == 0 && db->db_freed_in_flight) {
435 		/* we were freed in flight; disregard any error */
436 		arc_release(buf, db);
437 		bzero(buf->b_data, db->db.db_size);
438 		arc_buf_freeze(buf);
439 		db->db_freed_in_flight = FALSE;
440 		dbuf_set_data(db, buf);
441 		db->db_state = DB_CACHED;
442 	} else if (zio == NULL || zio->io_error == 0) {
443 		dbuf_set_data(db, buf);
444 		db->db_state = DB_CACHED;
445 	} else {
446 		ASSERT(db->db_blkid != DB_BONUS_BLKID);
447 		ASSERT3P(db->db_buf, ==, NULL);
448 		VERIFY(arc_buf_remove_ref(buf, db) == 1);
449 		db->db_state = DB_UNCACHED;
450 	}
451 	cv_broadcast(&db->db_changed);
452 	mutex_exit(&db->db_mtx);
453 	dbuf_rele(db, NULL);
454 }
455 
456 static void
457 dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t *flags)
458 {
459 	blkptr_t *bp;
460 	zbookmark_t zb;
461 	uint32_t aflags = ARC_NOWAIT;
462 
463 	ASSERT(!refcount_is_zero(&db->db_holds));
464 	/* We need the struct_rwlock to prevent db_blkptr from changing. */
465 	ASSERT(RW_LOCK_HELD(&db->db_dnode->dn_struct_rwlock));
466 	ASSERT(MUTEX_HELD(&db->db_mtx));
467 	ASSERT(db->db_state == DB_UNCACHED);
468 	ASSERT(db->db_buf == NULL);
469 
470 	if (db->db_blkid == DB_BONUS_BLKID) {
471 		ASSERT3U(db->db_dnode->dn_bonuslen, ==, db->db.db_size);
472 		db->db.db_data = zio_buf_alloc(DN_MAX_BONUSLEN);
473 		if (db->db.db_size < DN_MAX_BONUSLEN)
474 			bzero(db->db.db_data, DN_MAX_BONUSLEN);
475 		bcopy(DN_BONUS(db->db_dnode->dn_phys), db->db.db_data,
476 		    db->db.db_size);
477 		dbuf_update_data(db);
478 		db->db_state = DB_CACHED;
479 		mutex_exit(&db->db_mtx);
480 		return;
481 	}
482 
483 	if (db->db_level == 0 && dnode_block_freed(db->db_dnode, db->db_blkid))
484 		bp = NULL;
485 	else
486 		bp = db->db_blkptr;
487 
488 	if (bp == NULL)
489 		dprintf_dbuf(db, "blkptr: %s\n", "NULL");
490 	else
491 		dprintf_dbuf_bp(db, bp, "%s", "blkptr:");
492 
493 	if (bp == NULL || BP_IS_HOLE(bp)) {
494 		arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
495 
496 		ASSERT(bp == NULL || BP_IS_HOLE(bp));
497 		dbuf_set_data(db, arc_buf_alloc(db->db_dnode->dn_objset->os_spa,
498 		    db->db.db_size, db, type));
499 		bzero(db->db.db_data, db->db.db_size);
500 		db->db_state = DB_CACHED;
501 		*flags |= DB_RF_CACHED;
502 		mutex_exit(&db->db_mtx);
503 		return;
504 	}
505 
506 	db->db_state = DB_READ;
507 	mutex_exit(&db->db_mtx);
508 
509 	zb.zb_objset = db->db_objset->os_dsl_dataset ?
510 	    db->db_objset->os_dsl_dataset->ds_object : 0;
511 	zb.zb_object = db->db.db_object;
512 	zb.zb_level = db->db_level;
513 	zb.zb_blkid = db->db_blkid;
514 
515 	dbuf_add_ref(db, NULL);
516 	/* ZIO_FLAG_CANFAIL callers have to check the parent zio's error */
517 	ASSERT3U(db->db_dnode->dn_type, <, DMU_OT_NUMTYPES);
518 	(void) arc_read(zio, db->db_dnode->dn_objset->os_spa, bp,
519 	    db->db_level > 0 ? byteswap_uint64_array :
520 	    dmu_ot[db->db_dnode->dn_type].ot_byteswap,
521 	    dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ,
522 	    (*flags & DB_RF_CANFAIL) ? ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED,
523 	    &aflags, &zb);
524 	if (aflags & ARC_CACHED)
525 		*flags |= DB_RF_CACHED;
526 }
527 
528 int
529 dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
530 {
531 	int err = 0;
532 	int havepzio = (zio != NULL);
533 	int prefetch;
534 
535 	/*
536 	 * We don't have to hold the mutex to check db_state because it
537 	 * can't be freed while we have a hold on the buffer.
538 	 */
539 	ASSERT(!refcount_is_zero(&db->db_holds));
540 
541 	if ((flags & DB_RF_HAVESTRUCT) == 0)
542 		rw_enter(&db->db_dnode->dn_struct_rwlock, RW_READER);
543 
544 	prefetch = db->db_level == 0 && db->db_blkid != DB_BONUS_BLKID &&
545 	    (flags & DB_RF_NOPREFETCH) == 0 && db->db_dnode != NULL;
546 
547 	mutex_enter(&db->db_mtx);
548 	if (db->db_state == DB_CACHED) {
549 		mutex_exit(&db->db_mtx);
550 		if (prefetch)
551 			dmu_zfetch(&db->db_dnode->dn_zfetch, db->db.db_offset,
552 			    db->db.db_size, TRUE);
553 		if ((flags & DB_RF_HAVESTRUCT) == 0)
554 			rw_exit(&db->db_dnode->dn_struct_rwlock);
555 	} else if (db->db_state == DB_UNCACHED) {
556 		if (zio == NULL) {
557 			zio = zio_root(db->db_dnode->dn_objset->os_spa,
558 			    NULL, NULL, ZIO_FLAG_CANFAIL);
559 		}
560 		dbuf_read_impl(db, zio, &flags);
561 
562 		/* dbuf_read_impl has dropped db_mtx for us */
563 
564 		if (prefetch)
565 			dmu_zfetch(&db->db_dnode->dn_zfetch, db->db.db_offset,
566 			    db->db.db_size, flags & DB_RF_CACHED);
567 
568 		if ((flags & DB_RF_HAVESTRUCT) == 0)
569 			rw_exit(&db->db_dnode->dn_struct_rwlock);
570 
571 		if (!havepzio)
572 			err = zio_wait(zio);
573 	} else {
574 		mutex_exit(&db->db_mtx);
575 		if (prefetch)
576 			dmu_zfetch(&db->db_dnode->dn_zfetch, db->db.db_offset,
577 			    db->db.db_size, TRUE);
578 		if ((flags & DB_RF_HAVESTRUCT) == 0)
579 			rw_exit(&db->db_dnode->dn_struct_rwlock);
580 
581 		mutex_enter(&db->db_mtx);
582 		if ((flags & DB_RF_NEVERWAIT) == 0) {
583 			while (db->db_state == DB_READ ||
584 			    db->db_state == DB_FILL) {
585 				ASSERT(db->db_state == DB_READ ||
586 				    (flags & DB_RF_HAVESTRUCT) == 0);
587 				cv_wait(&db->db_changed, &db->db_mtx);
588 			}
589 			if (db->db_state == DB_UNCACHED)
590 				err = EIO;
591 		}
592 		mutex_exit(&db->db_mtx);
593 	}
594 
595 	ASSERT(err || havepzio || db->db_state == DB_CACHED);
596 	return (err);
597 }
598 
599 static void
600 dbuf_noread(dmu_buf_impl_t *db)
601 {
602 	ASSERT(!refcount_is_zero(&db->db_holds));
603 	ASSERT(db->db_blkid != DB_BONUS_BLKID);
604 	mutex_enter(&db->db_mtx);
605 	while (db->db_state == DB_READ || db->db_state == DB_FILL)
606 		cv_wait(&db->db_changed, &db->db_mtx);
607 	if (db->db_state == DB_UNCACHED) {
608 		arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
609 
610 		ASSERT(db->db_buf == NULL);
611 		ASSERT(db->db.db_data == NULL);
612 		dbuf_set_data(db, arc_buf_alloc(db->db_dnode->dn_objset->os_spa,
613 		    db->db.db_size, db, type));
614 		db->db_state = DB_FILL;
615 	} else {
616 		ASSERT3U(db->db_state, ==, DB_CACHED);
617 	}
618 	mutex_exit(&db->db_mtx);
619 }
620 
621 /*
622  * This is our just-in-time copy function.  It makes a copy of
623  * buffers, that have been modified in a previous transaction
624  * group, before we modify them in the current active group.
625  *
626  * This function is used in two places: when we are dirtying a
627  * buffer for the first time in a txg, and when we are freeing
628  * a range in a dnode that includes this buffer.
629  *
630  * Note that when we are called from dbuf_free_range() we do
631  * not put a hold on the buffer, we just traverse the active
632  * dbuf list for the dnode.
633  */
634 static void
635 dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg)
636 {
637 	dbuf_dirty_record_t *dr = db->db_last_dirty;
638 
639 	ASSERT(MUTEX_HELD(&db->db_mtx));
640 	ASSERT(db->db.db_data != NULL);
641 	ASSERT(db->db_level == 0);
642 	ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT);
643 
644 	if (dr == NULL ||
645 	    (dr->dt.dl.dr_data !=
646 	    ((db->db_blkid  == DB_BONUS_BLKID) ? db->db.db_data : db->db_buf)))
647 		return;
648 
649 	/*
650 	 * If the last dirty record for this dbuf has not yet synced
651 	 * and its referencing the dbuf data, either:
652 	 * 	reset the reference to point to a new copy,
653 	 * or (if there a no active holders)
654 	 *	just null out the current db_data pointer.
655 	 */
656 	ASSERT(dr->dr_txg >= txg - 2);
657 	if (db->db_blkid == DB_BONUS_BLKID) {
658 		/* Note that the data bufs here are zio_bufs */
659 		dr->dt.dl.dr_data = zio_buf_alloc(DN_MAX_BONUSLEN);
660 		bcopy(db->db.db_data, dr->dt.dl.dr_data, DN_MAX_BONUSLEN);
661 	} else if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
662 		int size = db->db.db_size;
663 		arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
664 		dr->dt.dl.dr_data = arc_buf_alloc(
665 		    db->db_dnode->dn_objset->os_spa, size, db, type);
666 		bcopy(db->db.db_data, dr->dt.dl.dr_data->b_data, size);
667 	} else {
668 		dbuf_set_data(db, NULL);
669 	}
670 }
671 
672 void
673 dbuf_unoverride(dbuf_dirty_record_t *dr)
674 {
675 	dmu_buf_impl_t *db = dr->dr_dbuf;
676 	uint64_t txg = dr->dr_txg;
677 
678 	ASSERT(MUTEX_HELD(&db->db_mtx));
679 	ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC);
680 	ASSERT(db->db_level == 0);
681 
682 	if (db->db_blkid == DB_BONUS_BLKID ||
683 	    dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN)
684 		return;
685 
686 	/* free this block */
687 	if (!BP_IS_HOLE(&dr->dt.dl.dr_overridden_by)) {
688 		/* XXX can get silent EIO here */
689 		(void) arc_free(NULL, db->db_dnode->dn_objset->os_spa,
690 		    txg, &dr->dt.dl.dr_overridden_by, NULL, NULL, ARC_WAIT);
691 	}
692 	dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
693 	/*
694 	 * Release the already-written buffer, so we leave it in
695 	 * a consistent dirty state.  Note that all callers are
696 	 * modifying the buffer, so they will immediately do
697 	 * another (redundant) arc_release().  Therefore, leave
698 	 * the buf thawed to save the effort of freezing &
699 	 * immediately re-thawing it.
700 	 */
701 	arc_release(dr->dt.dl.dr_data, db);
702 }
703 
704 void
705 dbuf_free_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
706 {
707 	dmu_buf_impl_t *db, *db_next;
708 	uint64_t txg = tx->tx_txg;
709 
710 	dprintf_dnode(dn, "blkid=%llu nblks=%llu\n", blkid, nblks);
711 	mutex_enter(&dn->dn_dbufs_mtx);
712 	for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
713 		db_next = list_next(&dn->dn_dbufs, db);
714 		ASSERT(db->db_blkid != DB_BONUS_BLKID);
715 		if (db->db_level != 0)
716 			continue;
717 		dprintf_dbuf(db, "found buf %s\n", "");
718 		if (db->db_blkid < blkid ||
719 		    db->db_blkid >= blkid+nblks)
720 			continue;
721 
722 		/* found a level 0 buffer in the range */
723 		if (dbuf_undirty(db, tx))
724 			continue;
725 
726 		mutex_enter(&db->db_mtx);
727 		if (db->db_state == DB_UNCACHED ||
728 		    db->db_state == DB_EVICTING) {
729 			ASSERT(db->db.db_data == NULL);
730 			mutex_exit(&db->db_mtx);
731 			continue;
732 		}
733 		if (db->db_state == DB_READ || db->db_state == DB_FILL) {
734 			/* will be handled in dbuf_read_done or dbuf_rele */
735 			db->db_freed_in_flight = TRUE;
736 			mutex_exit(&db->db_mtx);
737 			continue;
738 		}
739 		if (refcount_count(&db->db_holds) == 0) {
740 			ASSERT(db->db_buf);
741 			dbuf_clear(db);
742 			continue;
743 		}
744 		/* The dbuf is referenced */
745 
746 		if (db->db_last_dirty != NULL) {
747 			dbuf_dirty_record_t *dr = db->db_last_dirty;
748 
749 			if (dr->dr_txg == txg) {
750 				/*
751 				 * This buffer is "in-use", re-adjust the file
752 				 * size to reflect that this buffer may
753 				 * contain new data when we sync.
754 				 */
755 				if (db->db_blkid > dn->dn_maxblkid)
756 					dn->dn_maxblkid = db->db_blkid;
757 				dbuf_unoverride(dr);
758 			} else {
759 				/*
760 				 * This dbuf is not dirty in the open context.
761 				 * Either uncache it (if its not referenced in
762 				 * the open context) or reset its contents to
763 				 * empty.
764 				 */
765 				dbuf_fix_old_data(db, txg);
766 			}
767 		}
768 		/* clear the contents if its cached */
769 		if (db->db_state == DB_CACHED) {
770 			ASSERT(db->db.db_data != NULL);
771 			arc_release(db->db_buf, db);
772 			bzero(db->db.db_data, db->db.db_size);
773 			arc_buf_freeze(db->db_buf);
774 		}
775 
776 		mutex_exit(&db->db_mtx);
777 	}
778 	mutex_exit(&dn->dn_dbufs_mtx);
779 }
780 
781 static int
782 dbuf_new_block(dmu_buf_impl_t *db)
783 {
784 	dsl_dataset_t *ds = db->db_objset->os_dsl_dataset;
785 	uint64_t birth_txg = 0;
786 
787 	/* Don't count meta-objects */
788 	if (ds == NULL)
789 		return (FALSE);
790 
791 	/*
792 	 * We don't need any locking to protect db_blkptr:
793 	 * If it's syncing, then db_last_dirty will be set
794 	 * so we'll ignore db_blkptr.
795 	 */
796 	ASSERT(MUTEX_HELD(&db->db_mtx));
797 	/* If we have been dirtied since the last snapshot, its not new */
798 	if (db->db_last_dirty)
799 		birth_txg = db->db_last_dirty->dr_txg;
800 	else if (db->db_blkptr)
801 		birth_txg = db->db_blkptr->blk_birth;
802 
803 	if (birth_txg)
804 		return (!dsl_dataset_block_freeable(ds, birth_txg));
805 	else
806 		return (TRUE);
807 }
808 
809 void
810 dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx)
811 {
812 	arc_buf_t *buf, *obuf;
813 	int osize = db->db.db_size;
814 	arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
815 
816 	ASSERT(db->db_blkid != DB_BONUS_BLKID);
817 
818 	/* XXX does *this* func really need the lock? */
819 	ASSERT(RW_WRITE_HELD(&db->db_dnode->dn_struct_rwlock));
820 
821 	/*
822 	 * This call to dbuf_will_dirty() with the dn_struct_rwlock held
823 	 * is OK, because there can be no other references to the db
824 	 * when we are changing its size, so no concurrent DB_FILL can
825 	 * be happening.
826 	 */
827 	/*
828 	 * XXX we should be doing a dbuf_read, checking the return
829 	 * value and returning that up to our callers
830 	 */
831 	dbuf_will_dirty(db, tx);
832 
833 	/* create the data buffer for the new block */
834 	buf = arc_buf_alloc(db->db_dnode->dn_objset->os_spa, size, db, type);
835 
836 	/* copy old block data to the new block */
837 	obuf = db->db_buf;
838 	bcopy(obuf->b_data, buf->b_data, MIN(osize, size));
839 	/* zero the remainder */
840 	if (size > osize)
841 		bzero((uint8_t *)buf->b_data + osize, size - osize);
842 
843 	mutex_enter(&db->db_mtx);
844 	dbuf_set_data(db, buf);
845 	VERIFY(arc_buf_remove_ref(obuf, db) == 1);
846 	db->db.db_size = size;
847 
848 	if (db->db_level == 0) {
849 		ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
850 		db->db_last_dirty->dt.dl.dr_data = buf;
851 	}
852 	mutex_exit(&db->db_mtx);
853 
854 	dnode_willuse_space(db->db_dnode, size-osize, tx);
855 }
856 
857 dbuf_dirty_record_t *
858 dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
859 {
860 	dnode_t *dn = db->db_dnode;
861 	objset_impl_t *os = dn->dn_objset;
862 	dbuf_dirty_record_t **drp, *dr;
863 	int drop_struct_lock = FALSE;
864 	int txgoff = tx->tx_txg & TXG_MASK;
865 
866 	ASSERT(tx->tx_txg != 0);
867 	ASSERT(!refcount_is_zero(&db->db_holds));
868 	DMU_TX_DIRTY_BUF(tx, db);
869 
870 	/*
871 	 * Shouldn't dirty a regular buffer in syncing context.  Private
872 	 * objects may be dirtied in syncing context, but only if they
873 	 * were already pre-dirtied in open context.
874 	 * XXX We may want to prohibit dirtying in syncing context even
875 	 * if they did pre-dirty.
876 	 */
877 	ASSERT(!dmu_tx_is_syncing(tx) ||
878 	    BP_IS_HOLE(dn->dn_objset->os_rootbp) ||
879 	    dn->dn_object == DMU_META_DNODE_OBJECT ||
880 	    dn->dn_objset->os_dsl_dataset == NULL ||
881 	    dsl_dir_is_private(dn->dn_objset->os_dsl_dataset->ds_dir));
882 
883 	/*
884 	 * We make this assert for private objects as well, but after we
885 	 * check if we're already dirty.  They are allowed to re-dirty
886 	 * in syncing context.
887 	 */
888 	ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
889 	    dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
890 	    (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
891 
892 	mutex_enter(&db->db_mtx);
893 	/*
894 	 * XXX make this true for indirects too?  The problem is that
895 	 * transactions created with dmu_tx_create_assigned() from
896 	 * syncing context don't bother holding ahead.
897 	 */
898 	ASSERT(db->db_level != 0 ||
899 	    db->db_state == DB_CACHED || db->db_state == DB_FILL);
900 
901 	mutex_enter(&dn->dn_mtx);
902 	/*
903 	 * Don't set dirtyctx to SYNC if we're just modifying this as we
904 	 * initialize the objset.
905 	 */
906 	if (dn->dn_dirtyctx == DN_UNDIRTIED &&
907 	    !BP_IS_HOLE(dn->dn_objset->os_rootbp)) {
908 		dn->dn_dirtyctx =
909 		    (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN);
910 		ASSERT(dn->dn_dirtyctx_firstset == NULL);
911 		dn->dn_dirtyctx_firstset = kmem_alloc(1, KM_SLEEP);
912 	}
913 	mutex_exit(&dn->dn_mtx);
914 
915 	/*
916 	 * If this buffer is already dirty, we're done.
917 	 */
918 	drp = &db->db_last_dirty;
919 	ASSERT(*drp == NULL || (*drp)->dr_txg <= tx->tx_txg ||
920 	    db->db.db_object == DMU_META_DNODE_OBJECT);
921 	while (*drp && (*drp)->dr_txg > tx->tx_txg)
922 		drp = &(*drp)->dr_next;
923 	if (*drp && (*drp)->dr_txg == tx->tx_txg) {
924 		if (db->db_level == 0 && db->db_blkid != DB_BONUS_BLKID) {
925 			/*
926 			 * If this buffer has already been written out,
927 			 * we now need to reset its state.
928 			 */
929 			dbuf_unoverride(*drp);
930 			if (db->db.db_object != DMU_META_DNODE_OBJECT)
931 				arc_buf_thaw(db->db_buf);
932 		}
933 		mutex_exit(&db->db_mtx);
934 		return (*drp);
935 	}
936 
937 	/*
938 	 * Only valid if not already dirty.
939 	 */
940 	ASSERT(dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
941 	    (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
942 
943 	ASSERT3U(dn->dn_nlevels, >, db->db_level);
944 	ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) ||
945 	    dn->dn_phys->dn_nlevels > db->db_level ||
946 	    dn->dn_next_nlevels[txgoff] > db->db_level ||
947 	    dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level ||
948 	    dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level);
949 
950 	/*
951 	 * We should only be dirtying in syncing context if it's the
952 	 * mos, a spa os, or we're initializing the os.  However, we are
953 	 * allowed to dirty in syncing context provided we already
954 	 * dirtied it in open context.  Hence we must make this
955 	 * assertion only if we're not already dirty.
956 	 */
957 	ASSERT(!dmu_tx_is_syncing(tx) ||
958 	    os->os_dsl_dataset == NULL ||
959 	    !dsl_dir_is_private(os->os_dsl_dataset->ds_dir) ||
960 	    !BP_IS_HOLE(os->os_rootbp));
961 	ASSERT(db->db.db_size != 0);
962 
963 	dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
964 
965 	/*
966 	 * If this buffer is dirty in an old transaction group we need
967 	 * to make a copy of it so that the changes we make in this
968 	 * transaction group won't leak out when we sync the older txg.
969 	 */
970 	dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP);
971 	if (db->db_level == 0) {
972 		void *data_old = db->db_buf;
973 
974 		if (db->db_blkid == DB_BONUS_BLKID) {
975 			dbuf_fix_old_data(db, tx->tx_txg);
976 			data_old = db->db.db_data;
977 		} else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
978 			/*
979 			 * Release the data buffer from the cache so that we
980 			 * can modify it without impacting possible other users
981 			 * of this cached data block.  Note that indirect
982 			 * blocks and private objects are not released until the
983 			 * syncing state (since they are only modified then).
984 			 */
985 			arc_release(db->db_buf, db);
986 			dbuf_fix_old_data(db, tx->tx_txg);
987 			data_old = db->db_buf;
988 		}
989 		ASSERT(data_old != NULL);
990 		dr->dt.dl.dr_data = data_old;
991 	} else {
992 		mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_DEFAULT, NULL);
993 		list_create(&dr->dt.di.dr_children,
994 		    sizeof (dbuf_dirty_record_t),
995 		    offsetof(dbuf_dirty_record_t, dr_dirty_node));
996 	}
997 	dr->dr_dbuf = db;
998 	dr->dr_txg = tx->tx_txg;
999 	dr->dr_next = *drp;
1000 	*drp = dr;
1001 
1002 	/*
1003 	 * We could have been freed_in_flight between the dbuf_noread
1004 	 * and dbuf_dirty.  We win, as though the dbuf_noread() had
1005 	 * happened after the free.
1006 	 */
1007 	if (db->db_level == 0 && db->db_blkid != DB_BONUS_BLKID) {
1008 		mutex_enter(&dn->dn_mtx);
1009 		dnode_clear_range(dn, db->db_blkid, 1, tx);
1010 		mutex_exit(&dn->dn_mtx);
1011 		db->db_freed_in_flight = FALSE;
1012 	}
1013 
1014 	if (db->db_blkid != DB_BONUS_BLKID) {
1015 		/*
1016 		 * Update the accounting.
1017 		 */
1018 		if (!dbuf_new_block(db) && db->db_blkptr) {
1019 			/*
1020 			 * This is only a guess -- if the dbuf is dirty
1021 			 * in a previous txg, we don't know how much
1022 			 * space it will use on disk yet.  We should
1023 			 * really have the struct_rwlock to access
1024 			 * db_blkptr, but since this is just a guess,
1025 			 * it's OK if we get an odd answer.
1026 			 */
1027 			dnode_willuse_space(dn,
1028 			    -bp_get_dasize(os->os_spa, db->db_blkptr), tx);
1029 		}
1030 		dnode_willuse_space(dn, db->db.db_size, tx);
1031 	}
1032 
1033 	/*
1034 	 * This buffer is now part of this txg
1035 	 */
1036 	dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg);
1037 	db->db_dirtycnt += 1;
1038 	ASSERT3U(db->db_dirtycnt, <=, 3);
1039 
1040 	mutex_exit(&db->db_mtx);
1041 
1042 	if (db->db_blkid == DB_BONUS_BLKID) {
1043 		mutex_enter(&dn->dn_mtx);
1044 		ASSERT(!list_link_active(&dr->dr_dirty_node));
1045 		list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1046 		mutex_exit(&dn->dn_mtx);
1047 		dnode_setdirty(dn, tx);
1048 		return (dr);
1049 	}
1050 
1051 	if (db->db_level == 0) {
1052 		dnode_new_blkid(dn, db->db_blkid, tx);
1053 		ASSERT(dn->dn_maxblkid >= db->db_blkid);
1054 	}
1055 
1056 	if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
1057 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
1058 		drop_struct_lock = TRUE;
1059 	}
1060 
1061 	if (db->db_level+1 < dn->dn_nlevels) {
1062 		dmu_buf_impl_t *parent = db->db_parent;
1063 		dbuf_dirty_record_t *di;
1064 		int parent_held = FALSE;
1065 
1066 		if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
1067 			int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1068 
1069 			parent = dbuf_hold_level(dn, db->db_level+1,
1070 			    db->db_blkid >> epbs, FTAG);
1071 			parent_held = TRUE;
1072 		}
1073 		if (drop_struct_lock)
1074 			rw_exit(&dn->dn_struct_rwlock);
1075 		ASSERT3U(db->db_level+1, ==, parent->db_level);
1076 		di = dbuf_dirty(parent, tx);
1077 		if (parent_held)
1078 			dbuf_rele(parent, FTAG);
1079 
1080 		mutex_enter(&db->db_mtx);
1081 		/*  possible race with dbuf_undirty() */
1082 		if (db->db_last_dirty == dr ||
1083 		    dn->dn_object == DMU_META_DNODE_OBJECT) {
1084 			mutex_enter(&di->dt.di.dr_mtx);
1085 			ASSERT3U(di->dr_txg, ==, tx->tx_txg);
1086 			ASSERT(!list_link_active(&dr->dr_dirty_node));
1087 			list_insert_tail(&di->dt.di.dr_children, dr);
1088 			mutex_exit(&di->dt.di.dr_mtx);
1089 			dr->dr_parent = di;
1090 		}
1091 		mutex_exit(&db->db_mtx);
1092 	} else {
1093 		ASSERT(db->db_level+1 == dn->dn_nlevels);
1094 		ASSERT(db->db_blkid < dn->dn_nblkptr);
1095 		ASSERT(db->db_parent == NULL ||
1096 		    db->db_parent == db->db_dnode->dn_dbuf);
1097 		mutex_enter(&dn->dn_mtx);
1098 		ASSERT(!list_link_active(&dr->dr_dirty_node));
1099 		list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1100 		mutex_exit(&dn->dn_mtx);
1101 		if (drop_struct_lock)
1102 			rw_exit(&dn->dn_struct_rwlock);
1103 	}
1104 
1105 	dnode_setdirty(dn, tx);
1106 	return (dr);
1107 }
1108 
1109 static int
1110 dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1111 {
1112 	dnode_t *dn = db->db_dnode;
1113 	uint64_t txg = tx->tx_txg;
1114 	dbuf_dirty_record_t *dr;
1115 
1116 	ASSERT(txg != 0);
1117 	ASSERT(db->db_blkid != DB_BONUS_BLKID);
1118 
1119 	mutex_enter(&db->db_mtx);
1120 
1121 	/*
1122 	 * If this buffer is not dirty, we're done.
1123 	 */
1124 	for (dr = db->db_last_dirty; dr; dr = dr->dr_next)
1125 		if (dr->dr_txg <= txg)
1126 			break;
1127 	if (dr == NULL || dr->dr_txg < txg) {
1128 		mutex_exit(&db->db_mtx);
1129 		return (0);
1130 	}
1131 	ASSERT(dr->dr_txg == txg);
1132 
1133 	/*
1134 	 * If this buffer is currently held, we cannot undirty
1135 	 * it, since one of the current holders may be in the
1136 	 * middle of an update.  Note that users of dbuf_undirty()
1137 	 * should not place a hold on the dbuf before the call.
1138 	 */
1139 	if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
1140 		mutex_exit(&db->db_mtx);
1141 		/* Make sure we don't toss this buffer at sync phase */
1142 		mutex_enter(&dn->dn_mtx);
1143 		dnode_clear_range(dn, db->db_blkid, 1, tx);
1144 		mutex_exit(&dn->dn_mtx);
1145 		return (0);
1146 	}
1147 
1148 	dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1149 
1150 	ASSERT(db->db.db_size != 0);
1151 
1152 	/* XXX would be nice to fix up dn_towrite_space[] */
1153 
1154 	db->db_last_dirty = dr->dr_next;
1155 
1156 	if (dr->dr_parent) {
1157 		mutex_enter(&dr->dr_parent->dt.di.dr_mtx);
1158 		list_remove(&dr->dr_parent->dt.di.dr_children, dr);
1159 		mutex_exit(&dr->dr_parent->dt.di.dr_mtx);
1160 	} else if (db->db_level+1 == dn->dn_nlevels) {
1161 		ASSERT3P(db->db_parent, ==, dn->dn_dbuf);
1162 		mutex_enter(&dn->dn_mtx);
1163 		list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr);
1164 		mutex_exit(&dn->dn_mtx);
1165 	}
1166 
1167 	if (db->db_level == 0) {
1168 		dbuf_unoverride(dr);
1169 
1170 		ASSERT(db->db_buf != NULL);
1171 		ASSERT(dr->dt.dl.dr_data != NULL);
1172 		if (dr->dt.dl.dr_data != db->db_buf)
1173 			VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data, db) == 1);
1174 	} else {
1175 		ASSERT(db->db_buf != NULL);
1176 		ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
1177 		/* XXX - mutex and list destroy? */
1178 	}
1179 	kmem_free(dr, sizeof (dbuf_dirty_record_t));
1180 
1181 	ASSERT(db->db_dirtycnt > 0);
1182 	db->db_dirtycnt -= 1;
1183 
1184 	if (refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) {
1185 		arc_buf_t *buf = db->db_buf;
1186 
1187 		ASSERT(arc_released(buf));
1188 		dbuf_set_data(db, NULL);
1189 		VERIFY(arc_buf_remove_ref(buf, db) == 1);
1190 		dbuf_evict(db);
1191 		return (1);
1192 	}
1193 
1194 	mutex_exit(&db->db_mtx);
1195 	return (0);
1196 }
1197 
1198 #pragma weak dmu_buf_will_dirty = dbuf_will_dirty
1199 void
1200 dbuf_will_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1201 {
1202 	int rf = DB_RF_MUST_SUCCEED;
1203 
1204 	ASSERT(tx->tx_txg != 0);
1205 	ASSERT(!refcount_is_zero(&db->db_holds));
1206 
1207 	if (RW_WRITE_HELD(&db->db_dnode->dn_struct_rwlock))
1208 		rf |= DB_RF_HAVESTRUCT;
1209 	(void) dbuf_read(db, NULL, rf);
1210 	(void) dbuf_dirty(db, tx);
1211 }
1212 
1213 void
1214 dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1215 {
1216 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1217 
1218 	ASSERT(db->db_blkid != DB_BONUS_BLKID);
1219 	ASSERT(tx->tx_txg != 0);
1220 	ASSERT(db->db_level == 0);
1221 	ASSERT(!refcount_is_zero(&db->db_holds));
1222 
1223 	ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
1224 	    dmu_tx_private_ok(tx));
1225 
1226 	dbuf_noread(db);
1227 	(void) dbuf_dirty(db, tx);
1228 }
1229 
1230 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1231 /* ARGSUSED */
1232 void
1233 dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx)
1234 {
1235 	mutex_enter(&db->db_mtx);
1236 	DBUF_VERIFY(db);
1237 
1238 	if (db->db_state == DB_FILL) {
1239 		if (db->db_level == 0 && db->db_freed_in_flight) {
1240 			ASSERT(db->db_blkid != DB_BONUS_BLKID);
1241 			/* we were freed while filling */
1242 			/* XXX dbuf_undirty? */
1243 			bzero(db->db.db_data, db->db.db_size);
1244 			db->db_freed_in_flight = FALSE;
1245 		}
1246 		db->db_state = DB_CACHED;
1247 		cv_broadcast(&db->db_changed);
1248 	}
1249 	mutex_exit(&db->db_mtx);
1250 }
1251 
1252 /*
1253  * "Clear" the contents of this dbuf.  This will mark the dbuf
1254  * EVICTING and clear *most* of its references.  Unfortunetely,
1255  * when we are not holding the dn_dbufs_mtx, we can't clear the
1256  * entry in the dn_dbufs list.  We have to wait until dbuf_destroy()
1257  * in this case.  For callers from the DMU we will usually see:
1258  *	dbuf_clear()->arc_buf_evict()->dbuf_do_evict()->dbuf_destroy()
1259  * For the arc callback, we will usually see:
1260  * 	dbuf_do_evict()->dbuf_clear();dbuf_destroy()
1261  * Sometimes, though, we will get a mix of these two:
1262  *	DMU: dbuf_clear()->arc_buf_evict()
1263  *	ARC: dbuf_do_evict()->dbuf_destroy()
1264  */
1265 void
1266 dbuf_clear(dmu_buf_impl_t *db)
1267 {
1268 	dnode_t *dn = db->db_dnode;
1269 	dmu_buf_impl_t *parent = db->db_parent;
1270 	dmu_buf_impl_t *dndb = dn->dn_dbuf;
1271 	int dbuf_gone = FALSE;
1272 
1273 	ASSERT(MUTEX_HELD(&db->db_mtx));
1274 	ASSERT(refcount_is_zero(&db->db_holds));
1275 
1276 	dbuf_evict_user(db);
1277 
1278 	if (db->db_state == DB_CACHED) {
1279 		ASSERT(db->db.db_data != NULL);
1280 		if (db->db_blkid == DB_BONUS_BLKID)
1281 			zio_buf_free(db->db.db_data, DN_MAX_BONUSLEN);
1282 		db->db.db_data = NULL;
1283 		db->db_state = DB_UNCACHED;
1284 	}
1285 
1286 	ASSERT3U(db->db_state, ==, DB_UNCACHED);
1287 	ASSERT(db->db_data_pending == NULL);
1288 
1289 	db->db_state = DB_EVICTING;
1290 	db->db_blkptr = NULL;
1291 
1292 	if (db->db_blkid != DB_BONUS_BLKID && MUTEX_HELD(&dn->dn_dbufs_mtx)) {
1293 		list_remove(&dn->dn_dbufs, db);
1294 		dnode_rele(dn, db);
1295 	}
1296 
1297 	if (db->db_buf)
1298 		dbuf_gone = arc_buf_evict(db->db_buf);
1299 
1300 	if (!dbuf_gone)
1301 		mutex_exit(&db->db_mtx);
1302 
1303 	/*
1304 	 * If this dbuf is referened from an indirect dbuf,
1305 	 * decrement the ref count on the indirect dbuf.
1306 	 */
1307 	if (parent && parent != dndb)
1308 		dbuf_rele(parent, db);
1309 }
1310 
1311 static int
1312 dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse,
1313     dmu_buf_impl_t **parentp, blkptr_t **bpp)
1314 {
1315 	int nlevels, epbs;
1316 
1317 	*parentp = NULL;
1318 	*bpp = NULL;
1319 
1320 	ASSERT(blkid != DB_BONUS_BLKID);
1321 
1322 	if (dn->dn_phys->dn_nlevels == 0)
1323 		nlevels = 1;
1324 	else
1325 		nlevels = dn->dn_phys->dn_nlevels;
1326 
1327 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1328 
1329 	ASSERT3U(level * epbs, <, 64);
1330 	ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1331 	if (level >= nlevels ||
1332 	    (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) {
1333 		/* the buffer has no parent yet */
1334 		return (ENOENT);
1335 	} else if (level < nlevels-1) {
1336 		/* this block is referenced from an indirect block */
1337 		int err = dbuf_hold_impl(dn, level+1,
1338 		    blkid >> epbs, fail_sparse, NULL, parentp);
1339 		if (err)
1340 			return (err);
1341 		err = dbuf_read(*parentp, NULL,
1342 		    (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL));
1343 		if (err) {
1344 			dbuf_rele(*parentp, NULL);
1345 			*parentp = NULL;
1346 			return (err);
1347 		}
1348 		*bpp = ((blkptr_t *)(*parentp)->db.db_data) +
1349 		    (blkid & ((1ULL << epbs) - 1));
1350 		return (0);
1351 	} else {
1352 		/* the block is referenced from the dnode */
1353 		ASSERT3U(level, ==, nlevels-1);
1354 		ASSERT(dn->dn_phys->dn_nblkptr == 0 ||
1355 		    blkid < dn->dn_phys->dn_nblkptr);
1356 		if (dn->dn_dbuf) {
1357 			dbuf_add_ref(dn->dn_dbuf, NULL);
1358 			*parentp = dn->dn_dbuf;
1359 		}
1360 		*bpp = &dn->dn_phys->dn_blkptr[blkid];
1361 		return (0);
1362 	}
1363 }
1364 
1365 static dmu_buf_impl_t *
1366 dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid,
1367     dmu_buf_impl_t *parent, blkptr_t *blkptr)
1368 {
1369 	objset_impl_t *os = dn->dn_objset;
1370 	dmu_buf_impl_t *db, *odb;
1371 
1372 	ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1373 	ASSERT(dn->dn_type != DMU_OT_NONE);
1374 
1375 	db = kmem_cache_alloc(dbuf_cache, KM_SLEEP);
1376 
1377 	db->db_objset = os;
1378 	db->db.db_object = dn->dn_object;
1379 	db->db_level = level;
1380 	db->db_blkid = blkid;
1381 	db->db_last_dirty = NULL;
1382 	db->db_dirtycnt = 0;
1383 	db->db_dnode = dn;
1384 	db->db_parent = parent;
1385 	db->db_blkptr = blkptr;
1386 
1387 	db->db_user_ptr = NULL;
1388 	db->db_user_data_ptr_ptr = NULL;
1389 	db->db_evict_func = NULL;
1390 	db->db_immediate_evict = 0;
1391 	db->db_freed_in_flight = 0;
1392 
1393 	if (blkid == DB_BONUS_BLKID) {
1394 		ASSERT3P(parent, ==, dn->dn_dbuf);
1395 		db->db.db_size = dn->dn_bonuslen;
1396 		db->db.db_offset = DB_BONUS_BLKID;
1397 		db->db_state = DB_UNCACHED;
1398 		/* the bonus dbuf is not placed in the hash table */
1399 		return (db);
1400 	} else {
1401 		int blocksize =
1402 		    db->db_level ? 1<<dn->dn_indblkshift :  dn->dn_datablksz;
1403 		db->db.db_size = blocksize;
1404 		db->db.db_offset = db->db_blkid * blocksize;
1405 	}
1406 
1407 	/*
1408 	 * Hold the dn_dbufs_mtx while we get the new dbuf
1409 	 * in the hash table *and* added to the dbufs list.
1410 	 * This prevents a possible deadlock with someone
1411 	 * trying to look up this dbuf before its added to the
1412 	 * dn_dbufs list.
1413 	 */
1414 	mutex_enter(&dn->dn_dbufs_mtx);
1415 	db->db_state = DB_EVICTING;
1416 	if ((odb = dbuf_hash_insert(db)) != NULL) {
1417 		/* someone else inserted it first */
1418 		kmem_cache_free(dbuf_cache, db);
1419 		mutex_exit(&dn->dn_dbufs_mtx);
1420 		return (odb);
1421 	}
1422 	list_insert_head(&dn->dn_dbufs, db);
1423 	db->db_state = DB_UNCACHED;
1424 	mutex_exit(&dn->dn_dbufs_mtx);
1425 
1426 	if (parent && parent != dn->dn_dbuf)
1427 		dbuf_add_ref(parent, db);
1428 
1429 	ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1430 	    refcount_count(&dn->dn_holds) > 0);
1431 	(void) refcount_add(&dn->dn_holds, db);
1432 
1433 	dprintf_dbuf(db, "db=%p\n", db);
1434 
1435 	return (db);
1436 }
1437 
1438 static int
1439 dbuf_do_evict(void *private)
1440 {
1441 	arc_buf_t *buf = private;
1442 	dmu_buf_impl_t *db = buf->b_private;
1443 
1444 	if (!MUTEX_HELD(&db->db_mtx))
1445 		mutex_enter(&db->db_mtx);
1446 
1447 	ASSERT(refcount_is_zero(&db->db_holds));
1448 
1449 	if (db->db_state != DB_EVICTING) {
1450 		ASSERT(db->db_state == DB_CACHED);
1451 		DBUF_VERIFY(db);
1452 		db->db_buf = NULL;
1453 		dbuf_evict(db);
1454 	} else {
1455 		mutex_exit(&db->db_mtx);
1456 		dbuf_destroy(db);
1457 	}
1458 	return (0);
1459 }
1460 
1461 static void
1462 dbuf_destroy(dmu_buf_impl_t *db)
1463 {
1464 	ASSERT(refcount_is_zero(&db->db_holds));
1465 
1466 	if (db->db_blkid != DB_BONUS_BLKID) {
1467 		dnode_t *dn = db->db_dnode;
1468 
1469 		/*
1470 		 * If this dbuf is still on the dn_dbufs list,
1471 		 * remove it from that list.
1472 		 */
1473 		if (list_link_active(&db->db_link)) {
1474 			mutex_enter(&dn->dn_dbufs_mtx);
1475 			list_remove(&dn->dn_dbufs, db);
1476 			mutex_exit(&dn->dn_dbufs_mtx);
1477 
1478 			dnode_rele(dn, db);
1479 		}
1480 		dbuf_hash_remove(db);
1481 	}
1482 	db->db_parent = NULL;
1483 	db->db_dnode = NULL;
1484 	db->db_buf = NULL;
1485 
1486 	ASSERT(db->db.db_data == NULL);
1487 	ASSERT(db->db_hash_next == NULL);
1488 	ASSERT(db->db_blkptr == NULL);
1489 	ASSERT(db->db_data_pending == NULL);
1490 
1491 	kmem_cache_free(dbuf_cache, db);
1492 }
1493 
1494 void
1495 dbuf_prefetch(dnode_t *dn, uint64_t blkid)
1496 {
1497 	dmu_buf_impl_t *db = NULL;
1498 	blkptr_t *bp = NULL;
1499 
1500 	ASSERT(blkid != DB_BONUS_BLKID);
1501 	ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1502 
1503 	if (dnode_block_freed(dn, blkid))
1504 		return;
1505 
1506 	/* dbuf_find() returns with db_mtx held */
1507 	if (db = dbuf_find(dn, 0, blkid)) {
1508 		if (refcount_count(&db->db_holds) > 0) {
1509 			/*
1510 			 * This dbuf is active.  We assume that it is
1511 			 * already CACHED, or else about to be either
1512 			 * read or filled.
1513 			 */
1514 			mutex_exit(&db->db_mtx);
1515 			return;
1516 		}
1517 		mutex_exit(&db->db_mtx);
1518 		db = NULL;
1519 	}
1520 
1521 	if (dbuf_findbp(dn, 0, blkid, TRUE, &db, &bp) == 0) {
1522 		if (bp && !BP_IS_HOLE(bp)) {
1523 			uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
1524 			zbookmark_t zb;
1525 			zb.zb_objset = dn->dn_objset->os_dsl_dataset ?
1526 			    dn->dn_objset->os_dsl_dataset->ds_object : 0;
1527 			zb.zb_object = dn->dn_object;
1528 			zb.zb_level = 0;
1529 			zb.zb_blkid = blkid;
1530 
1531 			(void) arc_read(NULL, dn->dn_objset->os_spa, bp,
1532 			    dmu_ot[dn->dn_type].ot_byteswap,
1533 			    NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
1534 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
1535 			    &aflags, &zb);
1536 		}
1537 		if (db)
1538 			dbuf_rele(db, NULL);
1539 	}
1540 }
1541 
1542 /*
1543  * Returns with db_holds incremented, and db_mtx not held.
1544  * Note: dn_struct_rwlock must be held.
1545  */
1546 int
1547 dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid, int fail_sparse,
1548     void *tag, dmu_buf_impl_t **dbp)
1549 {
1550 	dmu_buf_impl_t *db, *parent = NULL;
1551 
1552 	ASSERT(blkid != DB_BONUS_BLKID);
1553 	ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1554 	ASSERT3U(dn->dn_nlevels, >, level);
1555 
1556 	*dbp = NULL;
1557 top:
1558 	/* dbuf_find() returns with db_mtx held */
1559 	db = dbuf_find(dn, level, blkid);
1560 
1561 	if (db == NULL) {
1562 		blkptr_t *bp = NULL;
1563 		int err;
1564 
1565 		ASSERT3P(parent, ==, NULL);
1566 		err = dbuf_findbp(dn, level, blkid, fail_sparse, &parent, &bp);
1567 		if (fail_sparse) {
1568 			if (err == 0 && bp && BP_IS_HOLE(bp))
1569 				err = ENOENT;
1570 			if (err) {
1571 				if (parent)
1572 					dbuf_rele(parent, NULL);
1573 				return (err);
1574 			}
1575 		}
1576 		if (err && err != ENOENT)
1577 			return (err);
1578 		db = dbuf_create(dn, level, blkid, parent, bp);
1579 	}
1580 
1581 	if (db->db_buf && refcount_is_zero(&db->db_holds)) {
1582 		arc_buf_add_ref(db->db_buf, db);
1583 		if (db->db_buf->b_data == NULL) {
1584 			dbuf_clear(db);
1585 			if (parent) {
1586 				dbuf_rele(parent, NULL);
1587 				parent = NULL;
1588 			}
1589 			goto top;
1590 		}
1591 		ASSERT3P(db->db.db_data, ==, db->db_buf->b_data);
1592 	}
1593 
1594 	ASSERT(db->db_buf == NULL || arc_referenced(db->db_buf));
1595 
1596 	/*
1597 	 * If this buffer is currently syncing out, and we are are
1598 	 * still referencing it from db_data, we need to make a copy
1599 	 * of it in case we decide we want to dirty it again in this txg.
1600 	 */
1601 	if (db->db_level == 0 && db->db_blkid != DB_BONUS_BLKID &&
1602 	    dn->dn_object != DMU_META_DNODE_OBJECT &&
1603 	    db->db_state == DB_CACHED && db->db_data_pending) {
1604 		dbuf_dirty_record_t *dr = db->db_data_pending;
1605 
1606 		if (dr->dt.dl.dr_data == db->db_buf) {
1607 			arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1608 
1609 			dbuf_set_data(db,
1610 			    arc_buf_alloc(db->db_dnode->dn_objset->os_spa,
1611 			    db->db.db_size, db, type));
1612 			bcopy(dr->dt.dl.dr_data->b_data, db->db.db_data,
1613 			    db->db.db_size);
1614 		}
1615 	}
1616 
1617 	(void) refcount_add(&db->db_holds, tag);
1618 	dbuf_update_data(db);
1619 	DBUF_VERIFY(db);
1620 	mutex_exit(&db->db_mtx);
1621 
1622 	/* NOTE: we can't rele the parent until after we drop the db_mtx */
1623 	if (parent)
1624 		dbuf_rele(parent, NULL);
1625 
1626 	ASSERT3P(db->db_dnode, ==, dn);
1627 	ASSERT3U(db->db_blkid, ==, blkid);
1628 	ASSERT3U(db->db_level, ==, level);
1629 	*dbp = db;
1630 
1631 	return (0);
1632 }
1633 
1634 dmu_buf_impl_t *
1635 dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag)
1636 {
1637 	dmu_buf_impl_t *db;
1638 	int err = dbuf_hold_impl(dn, 0, blkid, FALSE, tag, &db);
1639 	return (err ? NULL : db);
1640 }
1641 
1642 dmu_buf_impl_t *
1643 dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag)
1644 {
1645 	dmu_buf_impl_t *db;
1646 	int err = dbuf_hold_impl(dn, level, blkid, FALSE, tag, &db);
1647 	return (err ? NULL : db);
1648 }
1649 
1650 dmu_buf_impl_t *
1651 dbuf_create_bonus(dnode_t *dn)
1652 {
1653 	dmu_buf_impl_t *db = dn->dn_bonus;
1654 
1655 	ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
1656 
1657 	ASSERT(dn->dn_bonus == NULL);
1658 	db = dbuf_create(dn, 0, DB_BONUS_BLKID, dn->dn_dbuf, NULL);
1659 	return (db);
1660 }
1661 
1662 #pragma weak dmu_buf_add_ref = dbuf_add_ref
1663 void
1664 dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
1665 {
1666 	int64_t holds = refcount_add(&db->db_holds, tag);
1667 	ASSERT(holds > 1);
1668 }
1669 
1670 #pragma weak dmu_buf_rele = dbuf_rele
1671 void
1672 dbuf_rele(dmu_buf_impl_t *db, void *tag)
1673 {
1674 	int64_t holds;
1675 
1676 	mutex_enter(&db->db_mtx);
1677 	DBUF_VERIFY(db);
1678 
1679 	holds = refcount_remove(&db->db_holds, tag);
1680 	ASSERT(holds >= 0);
1681 
1682 	/*
1683 	 * We can't freeze indirects if there is a possibility that they
1684 	 * may be modified in the current syncing context.
1685 	 */
1686 	if (db->db_buf && holds == (db->db_level == 0 ? db->db_dirtycnt : 0))
1687 		arc_buf_freeze(db->db_buf);
1688 
1689 	if (holds == db->db_dirtycnt &&
1690 	    db->db_level == 0 && db->db_immediate_evict)
1691 		dbuf_evict_user(db);
1692 
1693 	if (holds == 0) {
1694 		if (db->db_blkid == DB_BONUS_BLKID) {
1695 			mutex_exit(&db->db_mtx);
1696 			dnode_rele(db->db_dnode, db);
1697 		} else if (db->db_buf == NULL) {
1698 			/*
1699 			 * This is a special case: we never associated this
1700 			 * dbuf with any data allocated from the ARC.
1701 			 */
1702 			ASSERT3U(db->db_state, ==, DB_UNCACHED);
1703 			dbuf_evict(db);
1704 		} else if (arc_released(db->db_buf)) {
1705 			arc_buf_t *buf = db->db_buf;
1706 			/*
1707 			 * This dbuf has anonymous data associated with it.
1708 			 */
1709 			dbuf_set_data(db, NULL);
1710 			VERIFY(arc_buf_remove_ref(buf, db) == 1);
1711 			dbuf_evict(db);
1712 		} else {
1713 			VERIFY(arc_buf_remove_ref(db->db_buf, db) == 0);
1714 			mutex_exit(&db->db_mtx);
1715 		}
1716 	} else {
1717 		mutex_exit(&db->db_mtx);
1718 	}
1719 }
1720 
1721 #pragma weak dmu_buf_refcount = dbuf_refcount
1722 uint64_t
1723 dbuf_refcount(dmu_buf_impl_t *db)
1724 {
1725 	return (refcount_count(&db->db_holds));
1726 }
1727 
1728 void *
1729 dmu_buf_set_user(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr,
1730     dmu_buf_evict_func_t *evict_func)
1731 {
1732 	return (dmu_buf_update_user(db_fake, NULL, user_ptr,
1733 	    user_data_ptr_ptr, evict_func));
1734 }
1735 
1736 void *
1737 dmu_buf_set_user_ie(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr,
1738     dmu_buf_evict_func_t *evict_func)
1739 {
1740 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1741 
1742 	db->db_immediate_evict = TRUE;
1743 	return (dmu_buf_update_user(db_fake, NULL, user_ptr,
1744 	    user_data_ptr_ptr, evict_func));
1745 }
1746 
1747 void *
1748 dmu_buf_update_user(dmu_buf_t *db_fake, void *old_user_ptr, void *user_ptr,
1749     void *user_data_ptr_ptr, dmu_buf_evict_func_t *evict_func)
1750 {
1751 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1752 	ASSERT(db->db_level == 0);
1753 
1754 	ASSERT((user_ptr == NULL) == (evict_func == NULL));
1755 
1756 	mutex_enter(&db->db_mtx);
1757 
1758 	if (db->db_user_ptr == old_user_ptr) {
1759 		db->db_user_ptr = user_ptr;
1760 		db->db_user_data_ptr_ptr = user_data_ptr_ptr;
1761 		db->db_evict_func = evict_func;
1762 
1763 		dbuf_update_data(db);
1764 	} else {
1765 		old_user_ptr = db->db_user_ptr;
1766 	}
1767 
1768 	mutex_exit(&db->db_mtx);
1769 	return (old_user_ptr);
1770 }
1771 
1772 void *
1773 dmu_buf_get_user(dmu_buf_t *db_fake)
1774 {
1775 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1776 	ASSERT(!refcount_is_zero(&db->db_holds));
1777 
1778 	return (db->db_user_ptr);
1779 }
1780 
1781 static void
1782 dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db)
1783 {
1784 	/* ASSERT(dmu_tx_is_syncing(tx) */
1785 	ASSERT(MUTEX_HELD(&db->db_mtx));
1786 
1787 	if (db->db_blkptr != NULL)
1788 		return;
1789 
1790 	if (db->db_level == dn->dn_phys->dn_nlevels-1) {
1791 		/*
1792 		 * This buffer was allocated at a time when there was
1793 		 * no available blkptrs from the dnode, or it was
1794 		 * inappropriate to hook it in (i.e., nlevels mis-match).
1795 		 */
1796 		ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr);
1797 		ASSERT(db->db_parent == NULL);
1798 		db->db_parent = dn->dn_dbuf;
1799 		db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
1800 		DBUF_VERIFY(db);
1801 	} else {
1802 		dmu_buf_impl_t *parent = db->db_parent;
1803 		int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
1804 
1805 		ASSERT(dn->dn_phys->dn_nlevels > 1);
1806 		if (parent == NULL) {
1807 			mutex_exit(&db->db_mtx);
1808 			rw_enter(&dn->dn_struct_rwlock, RW_READER);
1809 			(void) dbuf_hold_impl(dn, db->db_level+1,
1810 			    db->db_blkid >> epbs, FALSE, db, &parent);
1811 			rw_exit(&dn->dn_struct_rwlock);
1812 			mutex_enter(&db->db_mtx);
1813 			db->db_parent = parent;
1814 		}
1815 		db->db_blkptr = (blkptr_t *)parent->db.db_data +
1816 		    (db->db_blkid & ((1ULL << epbs) - 1));
1817 		DBUF_VERIFY(db);
1818 	}
1819 }
1820 
1821 static void
1822 dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
1823 {
1824 	dmu_buf_impl_t *db = dr->dr_dbuf;
1825 	dnode_t *dn = db->db_dnode;
1826 	zio_t *zio;
1827 
1828 	ASSERT(dmu_tx_is_syncing(tx));
1829 
1830 	dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
1831 
1832 	mutex_enter(&db->db_mtx);
1833 
1834 	ASSERT(db->db_level > 0);
1835 	DBUF_VERIFY(db);
1836 
1837 	if (db->db_buf == NULL) {
1838 		mutex_exit(&db->db_mtx);
1839 		(void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
1840 		mutex_enter(&db->db_mtx);
1841 	}
1842 	ASSERT3U(db->db_state, ==, DB_CACHED);
1843 	ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
1844 	ASSERT(db->db_buf != NULL);
1845 
1846 	dbuf_check_blkptr(dn, db);
1847 
1848 	db->db_data_pending = dr;
1849 
1850 	arc_release(db->db_buf, db);
1851 	mutex_exit(&db->db_mtx);
1852 
1853 	/*
1854 	 * XXX -- we should design a compression algorithm
1855 	 * that specializes in arrays of bps.
1856 	 */
1857 	dbuf_write(dr, db->db_buf, ZIO_CHECKSUM_FLETCHER_4,
1858 	    zfs_mdcomp_disable ? ZIO_COMPRESS_EMPTY : ZIO_COMPRESS_LZJB, tx);
1859 
1860 	zio = dr->dr_zio;
1861 	mutex_enter(&dr->dt.di.dr_mtx);
1862 	dbuf_sync_list(&dr->dt.di.dr_children, tx);
1863 	ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
1864 	mutex_exit(&dr->dt.di.dr_mtx);
1865 	zio_nowait(zio);
1866 }
1867 
1868 static void
1869 dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
1870 {
1871 	arc_buf_t **datap = &dr->dt.dl.dr_data;
1872 	dmu_buf_impl_t *db = dr->dr_dbuf;
1873 	dnode_t *dn = db->db_dnode;
1874 	objset_impl_t *os = dn->dn_objset;
1875 	uint64_t txg = tx->tx_txg;
1876 	int checksum, compress;
1877 	int blksz;
1878 
1879 	ASSERT(dmu_tx_is_syncing(tx));
1880 
1881 	dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
1882 
1883 	mutex_enter(&db->db_mtx);
1884 	/*
1885 	 * To be synced, we must be dirtied.  But we
1886 	 * might have been freed after the dirty.
1887 	 */
1888 	if (db->db_state == DB_UNCACHED) {
1889 		/* This buffer has been freed since it was dirtied */
1890 		ASSERT(db->db.db_data == NULL);
1891 	} else if (db->db_state == DB_FILL) {
1892 		/* This buffer was freed and is now being re-filled */
1893 		ASSERT(db->db.db_data != dr->dt.dl.dr_data);
1894 	} else {
1895 		ASSERT3U(db->db_state, ==, DB_CACHED);
1896 	}
1897 	DBUF_VERIFY(db);
1898 
1899 	/*
1900 	 * If this is a bonus buffer, simply copy the bonus data into the
1901 	 * dnode.  It will be written out when the dnode is synced (and it
1902 	 * will be synced, since it must have been dirty for dbuf_sync to
1903 	 * be called).
1904 	 */
1905 	if (db->db_blkid == DB_BONUS_BLKID) {
1906 		dbuf_dirty_record_t **drp;
1907 		/*
1908 		 * Use dn_phys->dn_bonuslen since db.db_size is the length
1909 		 * of the bonus buffer in the open transaction rather than
1910 		 * the syncing transaction.
1911 		 */
1912 		ASSERT(*datap != NULL);
1913 		ASSERT3U(db->db_level, ==, 0);
1914 		ASSERT3U(dn->dn_phys->dn_bonuslen, <=, DN_MAX_BONUSLEN);
1915 		bcopy(*datap, DN_BONUS(dn->dn_phys), dn->dn_phys->dn_bonuslen);
1916 		if (*datap != db->db.db_data)
1917 			zio_buf_free(*datap, DN_MAX_BONUSLEN);
1918 		db->db_data_pending = NULL;
1919 		drp = &db->db_last_dirty;
1920 		while (*drp != dr)
1921 			drp = &(*drp)->dr_next;
1922 		ASSERT((*drp)->dr_next == NULL);
1923 		*drp = NULL;
1924 		kmem_free(dr, sizeof (dbuf_dirty_record_t));
1925 		ASSERT(db->db_dirtycnt > 0);
1926 		db->db_dirtycnt -= 1;
1927 		mutex_exit(&db->db_mtx);
1928 		dbuf_rele(db, (void *)(uintptr_t)txg);
1929 		return;
1930 	}
1931 
1932 	/*
1933 	 * If this buffer is in the middle of an immdiate write,
1934 	 * wait for the synchronous IO to complete.
1935 	 */
1936 	while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
1937 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
1938 		cv_wait(&db->db_changed, &db->db_mtx);
1939 		ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN);
1940 	}
1941 
1942 	dbuf_check_blkptr(dn, db);
1943 
1944 	/*
1945 	 * If this dbuf has already been written out via an immediate write,
1946 	 * just complete the write by copying over the new block pointer and
1947 	 * updating the accounting via the write-completion functions.
1948 	 */
1949 	if (dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
1950 		zio_t zio_fake;
1951 
1952 		zio_fake.io_private = &db;
1953 		zio_fake.io_error = 0;
1954 		zio_fake.io_bp = db->db_blkptr;
1955 		zio_fake.io_bp_orig = *db->db_blkptr;
1956 		zio_fake.io_txg = txg;
1957 
1958 		*db->db_blkptr = dr->dt.dl.dr_overridden_by;
1959 		dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
1960 		db->db_data_pending = dr;
1961 		dr->dr_zio = &zio_fake;
1962 		mutex_exit(&db->db_mtx);
1963 
1964 		if (BP_IS_OLDER(&zio_fake.io_bp_orig, txg))
1965 			dsl_dataset_block_kill(os->os_dsl_dataset,
1966 			    &zio_fake.io_bp_orig, dn->dn_zio, tx);
1967 
1968 		dbuf_write_ready(&zio_fake, db->db_buf, db);
1969 		dbuf_write_done(&zio_fake, db->db_buf, db);
1970 
1971 		return;
1972 	}
1973 
1974 	blksz = arc_buf_size(*datap);
1975 
1976 	if (dn->dn_object != DMU_META_DNODE_OBJECT) {
1977 		/*
1978 		 * If this buffer is currently "in use" (i.e., there are
1979 		 * active holds and db_data still references it), then make
1980 		 * a copy before we start the write so that any modifications
1981 		 * from the open txg will not leak into this write.
1982 		 *
1983 		 * NOTE: this copy does not need to be made for objects only
1984 		 * modified in the syncing context (e.g. DNONE_DNODE blocks).
1985 		 */
1986 		if (refcount_count(&db->db_holds) > 1 && *datap == db->db_buf) {
1987 			arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1988 			*datap = arc_buf_alloc(os->os_spa, blksz, db, type);
1989 			bcopy(db->db.db_data, (*datap)->b_data, blksz);
1990 		}
1991 	} else {
1992 		/*
1993 		 * Private object buffers are released here rather
1994 		 * than in dbuf_dirty() since they are only modified
1995 		 * in the syncing context and we don't want the
1996 		 * overhead of making multiple copies of the data.
1997 		 */
1998 		arc_release(db->db_buf, db);
1999 	}
2000 
2001 	ASSERT(*datap != NULL);
2002 	db->db_data_pending = dr;
2003 
2004 	mutex_exit(&db->db_mtx);
2005 
2006 	/*
2007 	 * Allow dnode settings to override objset settings,
2008 	 * except for metadata checksums.
2009 	 */
2010 	if (dmu_ot[dn->dn_type].ot_metadata) {
2011 		checksum = os->os_md_checksum;
2012 		compress = zio_compress_select(dn->dn_compress,
2013 		    os->os_md_compress);
2014 	} else {
2015 		checksum = zio_checksum_select(dn->dn_checksum,
2016 		    os->os_checksum);
2017 		compress = zio_compress_select(dn->dn_compress,
2018 		    os->os_compress);
2019 	}
2020 
2021 	dbuf_write(dr, *datap, checksum, compress, tx);
2022 
2023 	ASSERT(!list_link_active(&dr->dr_dirty_node));
2024 	if (dn->dn_object == DMU_META_DNODE_OBJECT)
2025 		list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr);
2026 	else
2027 		zio_nowait(dr->dr_zio);
2028 }
2029 
2030 void
2031 dbuf_sync_list(list_t *list, dmu_tx_t *tx)
2032 {
2033 	dbuf_dirty_record_t *dr;
2034 
2035 	while (dr = list_head(list)) {
2036 		if (dr->dr_zio != NULL) {
2037 			/*
2038 			 * If we find an already initialized zio then we
2039 			 * are processing the meta-dnode, and we have finished.
2040 			 * The dbufs for all dnodes are put back on the list
2041 			 * during processing, so that we can zio_wait()
2042 			 * these IOs after initiating all child IOs.
2043 			 */
2044 			ASSERT3U(dr->dr_dbuf->db.db_object, ==,
2045 			    DMU_META_DNODE_OBJECT);
2046 			break;
2047 		}
2048 		list_remove(list, dr);
2049 		if (dr->dr_dbuf->db_level > 0)
2050 			dbuf_sync_indirect(dr, tx);
2051 		else
2052 			dbuf_sync_leaf(dr, tx);
2053 	}
2054 }
2055 
2056 static void
2057 dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, int checksum,
2058     int compress, dmu_tx_t *tx)
2059 {
2060 	dmu_buf_impl_t *db = dr->dr_dbuf;
2061 	dnode_t *dn = db->db_dnode;
2062 	objset_impl_t *os = dn->dn_objset;
2063 	dmu_buf_impl_t *parent = db->db_parent;
2064 	uint64_t txg = tx->tx_txg;
2065 	zbookmark_t zb;
2066 	zio_t *zio;
2067 	int zio_flags;
2068 
2069 	if (parent != dn->dn_dbuf) {
2070 		ASSERT(parent && parent->db_data_pending);
2071 		ASSERT(db->db_level == parent->db_level-1);
2072 		ASSERT(arc_released(parent->db_buf));
2073 		zio = parent->db_data_pending->dr_zio;
2074 	} else {
2075 		ASSERT(db->db_level == dn->dn_phys->dn_nlevels-1);
2076 		ASSERT3P(db->db_blkptr, ==,
2077 		    &dn->dn_phys->dn_blkptr[db->db_blkid]);
2078 		zio = dn->dn_zio;
2079 	}
2080 
2081 	ASSERT(db->db_level == 0 || data == db->db_buf);
2082 	ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
2083 	ASSERT(zio);
2084 
2085 	zb.zb_objset = os->os_dsl_dataset ? os->os_dsl_dataset->ds_object : 0;
2086 	zb.zb_object = db->db.db_object;
2087 	zb.zb_level = db->db_level;
2088 	zb.zb_blkid = db->db_blkid;
2089 
2090 	zio_flags = ZIO_FLAG_MUSTSUCCEED;
2091 	if (dmu_ot[dn->dn_type].ot_metadata || zb.zb_level != 0)
2092 		zio_flags |= ZIO_FLAG_METADATA;
2093 	if (BP_IS_OLDER(db->db_blkptr, txg))
2094 		dsl_dataset_block_kill(
2095 		    os->os_dsl_dataset, db->db_blkptr, zio, tx);
2096 
2097 	dr->dr_zio = arc_write(zio, os->os_spa, checksum, compress,
2098 	    dmu_get_replication_level(os, &zb, dn->dn_type), txg,
2099 	    db->db_blkptr, data, dbuf_write_ready, dbuf_write_done, db,
2100 	    ZIO_PRIORITY_ASYNC_WRITE, zio_flags, &zb);
2101 }
2102 
2103 /* ARGSUSED */
2104 static void
2105 dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
2106 {
2107 	dmu_buf_impl_t *db = vdb;
2108 	dnode_t *dn = db->db_dnode;
2109 	objset_impl_t *os = dn->dn_objset;
2110 	blkptr_t *bp_orig = &zio->io_bp_orig;
2111 	uint64_t fill = 0;
2112 	int old_size, new_size, i;
2113 
2114 	dprintf_dbuf_bp(db, bp_orig, "bp_orig: %s", "");
2115 
2116 	old_size = bp_get_dasize(os->os_spa, bp_orig);
2117 	new_size = bp_get_dasize(os->os_spa, zio->io_bp);
2118 
2119 	dnode_diduse_space(dn, new_size-old_size);
2120 
2121 	if (BP_IS_HOLE(zio->io_bp)) {
2122 		dsl_dataset_t *ds = os->os_dsl_dataset;
2123 		dmu_tx_t *tx = os->os_synctx;
2124 
2125 		if (bp_orig->blk_birth == tx->tx_txg)
2126 			dsl_dataset_block_kill(ds, bp_orig, NULL, tx);
2127 		ASSERT3U(db->db_blkptr->blk_fill, ==, 0);
2128 		return;
2129 	}
2130 
2131 	mutex_enter(&db->db_mtx);
2132 
2133 	if (db->db_level == 0) {
2134 		mutex_enter(&dn->dn_mtx);
2135 		if (db->db_blkid > dn->dn_phys->dn_maxblkid)
2136 			dn->dn_phys->dn_maxblkid = db->db_blkid;
2137 		mutex_exit(&dn->dn_mtx);
2138 
2139 		if (dn->dn_type == DMU_OT_DNODE) {
2140 			dnode_phys_t *dnp = db->db.db_data;
2141 			for (i = db->db.db_size >> DNODE_SHIFT; i > 0;
2142 			    i--, dnp++) {
2143 				if (dnp->dn_type != DMU_OT_NONE)
2144 					fill++;
2145 			}
2146 		} else {
2147 			fill = 1;
2148 		}
2149 	} else {
2150 		blkptr_t *bp = db->db.db_data;
2151 		ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2152 		for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, bp++) {
2153 			if (BP_IS_HOLE(bp))
2154 				continue;
2155 			ASSERT3U(BP_GET_LSIZE(bp), ==,
2156 			    db->db_level == 1 ? dn->dn_datablksz :
2157 			    (1<<dn->dn_phys->dn_indblkshift));
2158 			fill += bp->blk_fill;
2159 		}
2160 	}
2161 
2162 	db->db_blkptr->blk_fill = fill;
2163 	BP_SET_TYPE(db->db_blkptr, dn->dn_type);
2164 	BP_SET_LEVEL(db->db_blkptr, db->db_level);
2165 
2166 	mutex_exit(&db->db_mtx);
2167 
2168 	/* We must do this after we've set the bp's type and level */
2169 	if (!DVA_EQUAL(BP_IDENTITY(zio->io_bp), BP_IDENTITY(bp_orig))) {
2170 		dsl_dataset_t *ds = os->os_dsl_dataset;
2171 		dmu_tx_t *tx = os->os_synctx;
2172 
2173 		if (bp_orig->blk_birth == tx->tx_txg)
2174 			dsl_dataset_block_kill(ds, bp_orig, NULL, tx);
2175 		dsl_dataset_block_born(ds, zio->io_bp, tx);
2176 	}
2177 }
2178 
2179 /* ARGSUSED */
2180 static void
2181 dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
2182 {
2183 	dmu_buf_impl_t *db = vdb;
2184 	uint64_t txg = zio->io_txg;
2185 	dbuf_dirty_record_t **drp, *dr;
2186 
2187 	ASSERT3U(zio->io_error, ==, 0);
2188 
2189 	mutex_enter(&db->db_mtx);
2190 
2191 	drp = &db->db_last_dirty;
2192 	while (*drp != db->db_data_pending)
2193 		drp = &(*drp)->dr_next;
2194 	ASSERT(!list_link_active(&(*drp)->dr_dirty_node));
2195 	ASSERT((*drp)->dr_txg == txg);
2196 	ASSERT((*drp)->dr_next == NULL);
2197 	dr = *drp;
2198 	*drp = NULL;
2199 
2200 	if (db->db_level == 0) {
2201 		ASSERT(db->db_blkid != DB_BONUS_BLKID);
2202 		ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
2203 
2204 		if (dr->dt.dl.dr_data != db->db_buf)
2205 			VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data, db) == 1);
2206 		else if (!BP_IS_HOLE(db->db_blkptr))
2207 			arc_set_callback(db->db_buf, dbuf_do_evict, db);
2208 		else
2209 			ASSERT(arc_released(db->db_buf));
2210 	} else {
2211 		dnode_t *dn = db->db_dnode;
2212 
2213 		ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
2214 		ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2215 		if (!BP_IS_HOLE(db->db_blkptr)) {
2216 			int epbs =
2217 			    dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2218 			ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
2219 			    db->db.db_size);
2220 			ASSERT3U(dn->dn_phys->dn_maxblkid
2221 			    >> (db->db_level * epbs), >=, db->db_blkid);
2222 			arc_set_callback(db->db_buf, dbuf_do_evict, db);
2223 		}
2224 	}
2225 	kmem_free(dr, sizeof (dbuf_dirty_record_t));
2226 
2227 	cv_broadcast(&db->db_changed);
2228 	ASSERT(db->db_dirtycnt > 0);
2229 	db->db_dirtycnt -= 1;
2230 	db->db_data_pending = NULL;
2231 	mutex_exit(&db->db_mtx);
2232 
2233 	dprintf_dbuf_bp(db, zio->io_bp, "bp: %s", "");
2234 
2235 	dbuf_rele(db, (void *)(uintptr_t)txg);
2236 }
2237