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