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