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