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