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