xref: /illumos-gate/usr/src/uts/common/fs/zfs/dbuf.c (revision 0e8c6158)
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);
310fa9e4066Sahrens 		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) {
471fa9e4066Sahrens 		ASSERT3U(db->db_dnode->dn_bonuslen, ==, db->db.db_size);
472ea8dc4b6Seschrock 		db->db.db_data = zio_buf_alloc(DN_MAX_BONUSLEN);
473*0e8c6158Smaybee 		arc_space_consume(512);
474fa9e4066Sahrens 		if (db->db.db_size < DN_MAX_BONUSLEN)
475ea8dc4b6Seschrock 			bzero(db->db.db_data, DN_MAX_BONUSLEN);
476ea8dc4b6Seschrock 		bcopy(DN_BONUS(db->db_dnode->dn_phys), db->db.db_data,
477fa9e4066Sahrens 		    db->db.db_size);
478ea8dc4b6Seschrock 		dbuf_update_data(db);
479fa9e4066Sahrens 		db->db_state = DB_CACHED;
480fa9e4066Sahrens 		mutex_exit(&db->db_mtx);
481fa9e4066Sahrens 		return;
482fa9e4066Sahrens 	}
483fa9e4066Sahrens 
484fa9e4066Sahrens 	if (db->db_level == 0 && dnode_block_freed(db->db_dnode, db->db_blkid))
485fa9e4066Sahrens 		bp = NULL;
486fa9e4066Sahrens 	else
487fa9e4066Sahrens 		bp = db->db_blkptr;
488fa9e4066Sahrens 
489fa9e4066Sahrens 	if (bp == NULL)
490fa9e4066Sahrens 		dprintf_dbuf(db, "blkptr: %s\n", "NULL");
491fa9e4066Sahrens 	else
492fa9e4066Sahrens 		dprintf_dbuf_bp(db, bp, "%s", "blkptr:");
493fa9e4066Sahrens 
494fa9e4066Sahrens 	if (bp == NULL || BP_IS_HOLE(bp)) {
495ad23a2dbSjohansen 		arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
496ad23a2dbSjohansen 
497fa9e4066Sahrens 		ASSERT(bp == NULL || BP_IS_HOLE(bp));
498fa9e4066Sahrens 		dbuf_set_data(db, arc_buf_alloc(db->db_dnode->dn_objset->os_spa,
499ad23a2dbSjohansen 		    db->db.db_size, db, type));
500fa9e4066Sahrens 		bzero(db->db.db_data, db->db.db_size);
501fa9e4066Sahrens 		db->db_state = DB_CACHED;
50213506d1eSmaybee 		*flags |= DB_RF_CACHED;
503fa9e4066Sahrens 		mutex_exit(&db->db_mtx);
504fa9e4066Sahrens 		return;
505fa9e4066Sahrens 	}
506fa9e4066Sahrens 
507fa9e4066Sahrens 	db->db_state = DB_READ;
508fa9e4066Sahrens 	mutex_exit(&db->db_mtx);
509fa9e4066Sahrens 
510ea8dc4b6Seschrock 	zb.zb_objset = db->db_objset->os_dsl_dataset ?
511ea8dc4b6Seschrock 	    db->db_objset->os_dsl_dataset->ds_object : 0;
512ea8dc4b6Seschrock 	zb.zb_object = db->db.db_object;
513ea8dc4b6Seschrock 	zb.zb_level = db->db_level;
514ea8dc4b6Seschrock 	zb.zb_blkid = db->db_blkid;
515ea8dc4b6Seschrock 
516ea8dc4b6Seschrock 	dbuf_add_ref(db, NULL);
517fa9e4066Sahrens 	/* ZIO_FLAG_CANFAIL callers have to check the parent zio's error */
51887bd5c1eSahrens 	ASSERT3U(db->db_dnode->dn_type, <, DMU_OT_NUMTYPES);
519fa9e4066Sahrens 	(void) arc_read(zio, db->db_dnode->dn_objset->os_spa, bp,
520fa9e4066Sahrens 	    db->db_level > 0 ? byteswap_uint64_array :
521fa9e4066Sahrens 	    dmu_ot[db->db_dnode->dn_type].ot_byteswap,
522fa9e4066Sahrens 	    dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ,
52313506d1eSmaybee 	    (*flags & DB_RF_CANFAIL) ? ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED,
52413506d1eSmaybee 	    &aflags, &zb);
52513506d1eSmaybee 	if (aflags & ARC_CACHED)
52613506d1eSmaybee 		*flags |= DB_RF_CACHED;
527fa9e4066Sahrens }
528fa9e4066Sahrens 
529ea8dc4b6Seschrock int
530ea8dc4b6Seschrock dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
531fa9e4066Sahrens {
532ea8dc4b6Seschrock 	int err = 0;
533ea8dc4b6Seschrock 	int havepzio = (zio != NULL);
53413506d1eSmaybee 	int prefetch;
535fa9e4066Sahrens 
536fa9e4066Sahrens 	/*
537fa9e4066Sahrens 	 * We don't have to hold the mutex to check db_state because it
538fa9e4066Sahrens 	 * can't be freed while we have a hold on the buffer.
539fa9e4066Sahrens 	 */
540fa9e4066Sahrens 	ASSERT(!refcount_is_zero(&db->db_holds));
541fa9e4066Sahrens 
542ea8dc4b6Seschrock 	if ((flags & DB_RF_HAVESTRUCT) == 0)
543ea8dc4b6Seschrock 		rw_enter(&db->db_dnode->dn_struct_rwlock, RW_READER);
544ea8dc4b6Seschrock 
54513506d1eSmaybee 	prefetch = db->db_level == 0 && db->db_blkid != DB_BONUS_BLKID &&
54613506d1eSmaybee 	    (flags & DB_RF_NOPREFETCH) == 0 && db->db_dnode != NULL;
54713506d1eSmaybee 
548ea8dc4b6Seschrock 	mutex_enter(&db->db_mtx);
549ea8dc4b6Seschrock 	if (db->db_state == DB_CACHED) {
550ea8dc4b6Seschrock 		mutex_exit(&db->db_mtx);
55113506d1eSmaybee 		if (prefetch)
55213506d1eSmaybee 			dmu_zfetch(&db->db_dnode->dn_zfetch, db->db.db_offset,
55313506d1eSmaybee 			    db->db.db_size, TRUE);
554fa9e4066Sahrens 		if ((flags & DB_RF_HAVESTRUCT) == 0)
555ea8dc4b6Seschrock 			rw_exit(&db->db_dnode->dn_struct_rwlock);
556ea8dc4b6Seschrock 	} else if (db->db_state == DB_UNCACHED) {
557ea8dc4b6Seschrock 		if (zio == NULL) {
558ea8dc4b6Seschrock 			zio = zio_root(db->db_dnode->dn_objset->os_spa,
559ea8dc4b6Seschrock 			    NULL, NULL, ZIO_FLAG_CANFAIL);
560ea8dc4b6Seschrock 		}
56113506d1eSmaybee 		dbuf_read_impl(db, zio, &flags);
56213506d1eSmaybee 
563ea8dc4b6Seschrock 		/* dbuf_read_impl has dropped db_mtx for us */
564ea8dc4b6Seschrock 
56513506d1eSmaybee 		if (prefetch)
566ea8dc4b6Seschrock 			dmu_zfetch(&db->db_dnode->dn_zfetch, db->db.db_offset,
56713506d1eSmaybee 			    db->db.db_size, flags & DB_RF_CACHED);
568ea8dc4b6Seschrock 
569fa9e4066Sahrens 		if ((flags & DB_RF_HAVESTRUCT) == 0)
570fa9e4066Sahrens 			rw_exit(&db->db_dnode->dn_struct_rwlock);
571fa9e4066Sahrens 
572ea8dc4b6Seschrock 		if (!havepzio)
573ea8dc4b6Seschrock 			err = zio_wait(zio);
574ea8dc4b6Seschrock 	} else {
57513506d1eSmaybee 		mutex_exit(&db->db_mtx);
57613506d1eSmaybee 		if (prefetch)
57713506d1eSmaybee 			dmu_zfetch(&db->db_dnode->dn_zfetch, db->db.db_offset,
57813506d1eSmaybee 			    db->db.db_size, TRUE);
579ea8dc4b6Seschrock 		if ((flags & DB_RF_HAVESTRUCT) == 0)
580ea8dc4b6Seschrock 			rw_exit(&db->db_dnode->dn_struct_rwlock);
58113506d1eSmaybee 
58213506d1eSmaybee 		mutex_enter(&db->db_mtx);
583ea8dc4b6Seschrock 		if ((flags & DB_RF_NEVERWAIT) == 0) {
584ea8dc4b6Seschrock 			while (db->db_state == DB_READ ||
585ea8dc4b6Seschrock 			    db->db_state == DB_FILL) {
586ea8dc4b6Seschrock 				ASSERT(db->db_state == DB_READ ||
587ea8dc4b6Seschrock 				    (flags & DB_RF_HAVESTRUCT) == 0);
588ea8dc4b6Seschrock 				cv_wait(&db->db_changed, &db->db_mtx);
589ea8dc4b6Seschrock 			}
590ea8dc4b6Seschrock 			if (db->db_state == DB_UNCACHED)
591ea8dc4b6Seschrock 				err = EIO;
592ea8dc4b6Seschrock 		}
593ea8dc4b6Seschrock 		mutex_exit(&db->db_mtx);
594fa9e4066Sahrens 	}
595fa9e4066Sahrens 
596ea8dc4b6Seschrock 	ASSERT(err || havepzio || db->db_state == DB_CACHED);
597ea8dc4b6Seschrock 	return (err);
598fa9e4066Sahrens }
599fa9e4066Sahrens 
600fa9e4066Sahrens static void
601fa9e4066Sahrens dbuf_noread(dmu_buf_impl_t *db)
602fa9e4066Sahrens {
603fa9e4066Sahrens 	ASSERT(!refcount_is_zero(&db->db_holds));
604ea8dc4b6Seschrock 	ASSERT(db->db_blkid != DB_BONUS_BLKID);
605fa9e4066Sahrens 	mutex_enter(&db->db_mtx);
606fa9e4066Sahrens 	while (db->db_state == DB_READ || db->db_state == DB_FILL)
607fa9e4066Sahrens 		cv_wait(&db->db_changed, &db->db_mtx);
608fa9e4066Sahrens 	if (db->db_state == DB_UNCACHED) {
609ad23a2dbSjohansen 		arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
610ad23a2dbSjohansen 
611ea8dc4b6Seschrock 		ASSERT(db->db_buf == NULL);
612fa9e4066Sahrens 		ASSERT(db->db.db_data == NULL);
613fa9e4066Sahrens 		dbuf_set_data(db, arc_buf_alloc(db->db_dnode->dn_objset->os_spa,
614ad23a2dbSjohansen 		    db->db.db_size, db, type));
615fa9e4066Sahrens 		db->db_state = DB_FILL;
616fa9e4066Sahrens 	} else {
617fa9e4066Sahrens 		ASSERT3U(db->db_state, ==, DB_CACHED);
618fa9e4066Sahrens 	}
619fa9e4066Sahrens 	mutex_exit(&db->db_mtx);
620fa9e4066Sahrens }
621fa9e4066Sahrens 
622fa9e4066Sahrens /*
623fa9e4066Sahrens  * This is our just-in-time copy function.  It makes a copy of
624fa9e4066Sahrens  * buffers, that have been modified in a previous transaction
625fa9e4066Sahrens  * group, before we modify them in the current active group.
626fa9e4066Sahrens  *
627fa9e4066Sahrens  * This function is used in two places: when we are dirtying a
628fa9e4066Sahrens  * buffer for the first time in a txg, and when we are freeing
629fa9e4066Sahrens  * a range in a dnode that includes this buffer.
630fa9e4066Sahrens  *
631fa9e4066Sahrens  * Note that when we are called from dbuf_free_range() we do
632fa9e4066Sahrens  * not put a hold on the buffer, we just traverse the active
633fa9e4066Sahrens  * dbuf list for the dnode.
634fa9e4066Sahrens  */
635fa9e4066Sahrens static void
636fa9e4066Sahrens dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg)
637fa9e4066Sahrens {
638c717a561Smaybee 	dbuf_dirty_record_t *dr = db->db_last_dirty;
639fa9e4066Sahrens 
640fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&db->db_mtx));
641fa9e4066Sahrens 	ASSERT(db->db.db_data != NULL);
642c717a561Smaybee 	ASSERT(db->db_level == 0);
643c717a561Smaybee 	ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT);
644fa9e4066Sahrens 
6454d31c452Smaybee 	if (dr == NULL ||
6464d31c452Smaybee 	    (dr->dt.dl.dr_data !=
6474d31c452Smaybee 	    ((db->db_blkid  == DB_BONUS_BLKID) ? db->db.db_data : db->db_buf)))
648fa9e4066Sahrens 		return;
649fa9e4066Sahrens 
650fa9e4066Sahrens 	/*
651c717a561Smaybee 	 * If the last dirty record for this dbuf has not yet synced
652c717a561Smaybee 	 * and its referencing the dbuf data, either:
653c717a561Smaybee 	 * 	reset the reference to point to a new copy,
654c717a561Smaybee 	 * or (if there a no active holders)
655c717a561Smaybee 	 *	just null out the current db_data pointer.
656fa9e4066Sahrens 	 */
657c717a561Smaybee 	ASSERT(dr->dr_txg >= txg - 2);
658c717a561Smaybee 	if (db->db_blkid == DB_BONUS_BLKID) {
659c717a561Smaybee 		/* Note that the data bufs here are zio_bufs */
660c717a561Smaybee 		dr->dt.dl.dr_data = zio_buf_alloc(DN_MAX_BONUSLEN);
661*0e8c6158Smaybee 		arc_space_consume(DN_MAX_BONUSLEN);
662c717a561Smaybee 		bcopy(db->db.db_data, dr->dt.dl.dr_data, DN_MAX_BONUSLEN);
663c717a561Smaybee 	} else if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
664c717a561Smaybee 		int size = db->db.db_size;
665c717a561Smaybee 		arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
666c717a561Smaybee 		dr->dt.dl.dr_data = arc_buf_alloc(
667c717a561Smaybee 		    db->db_dnode->dn_objset->os_spa, size, db, type);
668c717a561Smaybee 		bcopy(db->db.db_data, dr->dt.dl.dr_data->b_data, size);
669c717a561Smaybee 	} else {
670c717a561Smaybee 		dbuf_set_data(db, NULL);
671fa9e4066Sahrens 	}
672fa9e4066Sahrens }
673fa9e4066Sahrens 
674c717a561Smaybee void
675c717a561Smaybee dbuf_unoverride(dbuf_dirty_record_t *dr)
676ea8dc4b6Seschrock {
677c717a561Smaybee 	dmu_buf_impl_t *db = dr->dr_dbuf;
678c717a561Smaybee 	uint64_t txg = dr->dr_txg;
679ea8dc4b6Seschrock 
680ea8dc4b6Seschrock 	ASSERT(MUTEX_HELD(&db->db_mtx));
681c717a561Smaybee 	ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC);
682c717a561Smaybee 	ASSERT(db->db_level == 0);
683ea8dc4b6Seschrock 
684c717a561Smaybee 	if (db->db_blkid == DB_BONUS_BLKID ||
685c717a561Smaybee 	    dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN)
686c717a561Smaybee 		return;
687ea8dc4b6Seschrock 
688c717a561Smaybee 	/* free this block */
689c717a561Smaybee 	if (!BP_IS_HOLE(&dr->dt.dl.dr_overridden_by)) {
690c717a561Smaybee 		/* XXX can get silent EIO here */
691c717a561Smaybee 		(void) arc_free(NULL, db->db_dnode->dn_objset->os_spa,
692c717a561Smaybee 		    txg, &dr->dt.dl.dr_overridden_by, NULL, NULL, ARC_WAIT);
693fa9e4066Sahrens 	}
694c717a561Smaybee 	dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
695c717a561Smaybee 	/*
696c717a561Smaybee 	 * Release the already-written buffer, so we leave it in
697c717a561Smaybee 	 * a consistent dirty state.  Note that all callers are
698c717a561Smaybee 	 * modifying the buffer, so they will immediately do
699c717a561Smaybee 	 * another (redundant) arc_release().  Therefore, leave
700c717a561Smaybee 	 * the buf thawed to save the effort of freezing &
701c717a561Smaybee 	 * immediately re-thawing it.
702c717a561Smaybee 	 */
703c717a561Smaybee 	arc_release(dr->dt.dl.dr_data, db);
704fa9e4066Sahrens }
705fa9e4066Sahrens 
706fa9e4066Sahrens void
707fa9e4066Sahrens dbuf_free_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
708fa9e4066Sahrens {
709fa9e4066Sahrens 	dmu_buf_impl_t *db, *db_next;
710fa9e4066Sahrens 	uint64_t txg = tx->tx_txg;
711fa9e4066Sahrens 
712fa9e4066Sahrens 	dprintf_dnode(dn, "blkid=%llu nblks=%llu\n", blkid, nblks);
713fa9e4066Sahrens 	mutex_enter(&dn->dn_dbufs_mtx);
714fa9e4066Sahrens 	for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
715fa9e4066Sahrens 		db_next = list_next(&dn->dn_dbufs, db);
716ea8dc4b6Seschrock 		ASSERT(db->db_blkid != DB_BONUS_BLKID);
717ea8dc4b6Seschrock 		if (db->db_level != 0)
718fa9e4066Sahrens 			continue;
719fa9e4066Sahrens 		dprintf_dbuf(db, "found buf %s\n", "");
720fa9e4066Sahrens 		if (db->db_blkid < blkid ||
721fa9e4066Sahrens 		    db->db_blkid >= blkid+nblks)
722fa9e4066Sahrens 			continue;
723fa9e4066Sahrens 
724fa9e4066Sahrens 		/* found a level 0 buffer in the range */
725fa9e4066Sahrens 		if (dbuf_undirty(db, tx))
726fa9e4066Sahrens 			continue;
727fa9e4066Sahrens 
728fa9e4066Sahrens 		mutex_enter(&db->db_mtx);
729ea8dc4b6Seschrock 		if (db->db_state == DB_UNCACHED ||
730ea8dc4b6Seschrock 		    db->db_state == DB_EVICTING) {
731fa9e4066Sahrens 			ASSERT(db->db.db_data == NULL);
732fa9e4066Sahrens 			mutex_exit(&db->db_mtx);
733fa9e4066Sahrens 			continue;
734fa9e4066Sahrens 		}
735c543ec06Sahrens 		if (db->db_state == DB_READ || db->db_state == DB_FILL) {
736c543ec06Sahrens 			/* will be handled in dbuf_read_done or dbuf_rele */
737c717a561Smaybee 			db->db_freed_in_flight = TRUE;
738fa9e4066Sahrens 			mutex_exit(&db->db_mtx);
739fa9e4066Sahrens 			continue;
740fa9e4066Sahrens 		}
741ea8dc4b6Seschrock 		if (refcount_count(&db->db_holds) == 0) {
742ea8dc4b6Seschrock 			ASSERT(db->db_buf);
743ea8dc4b6Seschrock 			dbuf_clear(db);
744ea8dc4b6Seschrock 			continue;
745ea8dc4b6Seschrock 		}
746c717a561Smaybee 		/* The dbuf is referenced */
747fa9e4066Sahrens 
748c717a561Smaybee 		if (db->db_last_dirty != NULL) {
749c717a561Smaybee 			dbuf_dirty_record_t *dr = db->db_last_dirty;
750c717a561Smaybee 
751c717a561Smaybee 			if (dr->dr_txg == txg) {
75244eda4d7Smaybee 				/*
753c717a561Smaybee 				 * This buffer is "in-use", re-adjust the file
754c717a561Smaybee 				 * size to reflect that this buffer may
755c717a561Smaybee 				 * contain new data when we sync.
75644eda4d7Smaybee 				 */
757c717a561Smaybee 				if (db->db_blkid > dn->dn_maxblkid)
758c717a561Smaybee 					dn->dn_maxblkid = db->db_blkid;
759c717a561Smaybee 				dbuf_unoverride(dr);
760c717a561Smaybee 			} else {
761c717a561Smaybee 				/*
762c717a561Smaybee 				 * This dbuf is not dirty in the open context.
763c717a561Smaybee 				 * Either uncache it (if its not referenced in
764c717a561Smaybee 				 * the open context) or reset its contents to
765c717a561Smaybee 				 * empty.
766c717a561Smaybee 				 */
767c717a561Smaybee 				dbuf_fix_old_data(db, txg);
76844eda4d7Smaybee 			}
769ea8dc4b6Seschrock 		}
770c717a561Smaybee 		/* clear the contents if its cached */
771ea8dc4b6Seschrock 		if (db->db_state == DB_CACHED) {
772ea8dc4b6Seschrock 			ASSERT(db->db.db_data != NULL);
773fa9e4066Sahrens 			arc_release(db->db_buf, db);
774fa9e4066Sahrens 			bzero(db->db.db_data, db->db.db_size);
7756b4acc8bSahrens 			arc_buf_freeze(db->db_buf);
776fa9e4066Sahrens 		}
777ea8dc4b6Seschrock 
778fa9e4066Sahrens 		mutex_exit(&db->db_mtx);
779fa9e4066Sahrens 	}
780fa9e4066Sahrens 	mutex_exit(&dn->dn_dbufs_mtx);
781fa9e4066Sahrens }
782fa9e4066Sahrens 
783fa9e4066Sahrens static int
784ea8dc4b6Seschrock dbuf_new_block(dmu_buf_impl_t *db)
785fa9e4066Sahrens {
786fa9e4066Sahrens 	dsl_dataset_t *ds = db->db_objset->os_dsl_dataset;
787fa9e4066Sahrens 	uint64_t birth_txg = 0;
788fa9e4066Sahrens 
789fa9e4066Sahrens 	/* Don't count meta-objects */
790fa9e4066Sahrens 	if (ds == NULL)
791fa9e4066Sahrens 		return (FALSE);
792fa9e4066Sahrens 
793fa9e4066Sahrens 	/*
794fa9e4066Sahrens 	 * We don't need any locking to protect db_blkptr:
795c717a561Smaybee 	 * If it's syncing, then db_last_dirty will be set
796c717a561Smaybee 	 * so we'll ignore db_blkptr.
797fa9e4066Sahrens 	 */
798c717a561Smaybee 	ASSERT(MUTEX_HELD(&db->db_mtx));
799fa9e4066Sahrens 	/* If we have been dirtied since the last snapshot, its not new */
800c717a561Smaybee 	if (db->db_last_dirty)
801c717a561Smaybee 		birth_txg = db->db_last_dirty->dr_txg;
802fa9e4066Sahrens 	else if (db->db_blkptr)
803fa9e4066Sahrens 		birth_txg = db->db_blkptr->blk_birth;
804fa9e4066Sahrens 
805fa9e4066Sahrens 	if (birth_txg)
806ea8dc4b6Seschrock 		return (!dsl_dataset_block_freeable(ds, birth_txg));
807fa9e4066Sahrens 	else
808fa9e4066Sahrens 		return (TRUE);
809fa9e4066Sahrens }
810fa9e4066Sahrens 
811fa9e4066Sahrens void
812fa9e4066Sahrens dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx)
813fa9e4066Sahrens {
814fa9e4066Sahrens 	arc_buf_t *buf, *obuf;
815fa9e4066Sahrens 	int osize = db->db.db_size;
816ad23a2dbSjohansen 	arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
817fa9e4066Sahrens 
818ea8dc4b6Seschrock 	ASSERT(db->db_blkid != DB_BONUS_BLKID);
819ea8dc4b6Seschrock 
820fa9e4066Sahrens 	/* XXX does *this* func really need the lock? */
821fa9e4066Sahrens 	ASSERT(RW_WRITE_HELD(&db->db_dnode->dn_struct_rwlock));
822fa9e4066Sahrens 
823fa9e4066Sahrens 	/*
824fa9e4066Sahrens 	 * This call to dbuf_will_dirty() with the dn_struct_rwlock held
825fa9e4066Sahrens 	 * is OK, because there can be no other references to the db
826fa9e4066Sahrens 	 * when we are changing its size, so no concurrent DB_FILL can
827fa9e4066Sahrens 	 * be happening.
828fa9e4066Sahrens 	 */
829ea8dc4b6Seschrock 	/*
830ea8dc4b6Seschrock 	 * XXX we should be doing a dbuf_read, checking the return
831ea8dc4b6Seschrock 	 * value and returning that up to our callers
832ea8dc4b6Seschrock 	 */
833fa9e4066Sahrens 	dbuf_will_dirty(db, tx);
834fa9e4066Sahrens 
835fa9e4066Sahrens 	/* create the data buffer for the new block */
836ad23a2dbSjohansen 	buf = arc_buf_alloc(db->db_dnode->dn_objset->os_spa, size, db, type);
837fa9e4066Sahrens 
838fa9e4066Sahrens 	/* copy old block data to the new block */
839fa9e4066Sahrens 	obuf = db->db_buf;
840f65e61c0Sahrens 	bcopy(obuf->b_data, buf->b_data, MIN(osize, size));
841fa9e4066Sahrens 	/* zero the remainder */
842f65e61c0Sahrens 	if (size > osize)
843f65e61c0Sahrens 		bzero((uint8_t *)buf->b_data + osize, size - osize);
844fa9e4066Sahrens 
845fa9e4066Sahrens 	mutex_enter(&db->db_mtx);
846fa9e4066Sahrens 	dbuf_set_data(db, buf);
847ea8dc4b6Seschrock 	VERIFY(arc_buf_remove_ref(obuf, db) == 1);
848fa9e4066Sahrens 	db->db.db_size = size;
849fa9e4066Sahrens 
850c717a561Smaybee 	if (db->db_level == 0) {
851c717a561Smaybee 		ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
852c717a561Smaybee 		db->db_last_dirty->dt.dl.dr_data = buf;
853c717a561Smaybee 	}
854fa9e4066Sahrens 	mutex_exit(&db->db_mtx);
855fa9e4066Sahrens 
856fa9e4066Sahrens 	dnode_willuse_space(db->db_dnode, size-osize, tx);
857fa9e4066Sahrens }
858fa9e4066Sahrens 
859c717a561Smaybee dbuf_dirty_record_t *
860fa9e4066Sahrens dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
861fa9e4066Sahrens {
862fa9e4066Sahrens 	dnode_t *dn = db->db_dnode;
863fa9e4066Sahrens 	objset_impl_t *os = dn->dn_objset;
864c717a561Smaybee 	dbuf_dirty_record_t **drp, *dr;
865fa9e4066Sahrens 	int drop_struct_lock = FALSE;
866fa9e4066Sahrens 	int txgoff = tx->tx_txg & TXG_MASK;
867fa9e4066Sahrens 
868fa9e4066Sahrens 	ASSERT(tx->tx_txg != 0);
869fa9e4066Sahrens 	ASSERT(!refcount_is_zero(&db->db_holds));
8709c9dc39aSek 	DMU_TX_DIRTY_BUF(tx, db);
871fa9e4066Sahrens 
872fa9e4066Sahrens 	/*
873fa9e4066Sahrens 	 * Shouldn't dirty a regular buffer in syncing context.  Private
874fa9e4066Sahrens 	 * objects may be dirtied in syncing context, but only if they
875fa9e4066Sahrens 	 * were already pre-dirtied in open context.
876fa9e4066Sahrens 	 * XXX We may want to prohibit dirtying in syncing context even
877fa9e4066Sahrens 	 * if they did pre-dirty.
878fa9e4066Sahrens 	 */
879c717a561Smaybee 	ASSERT(!dmu_tx_is_syncing(tx) ||
880c717a561Smaybee 	    BP_IS_HOLE(dn->dn_objset->os_rootbp) ||
881c717a561Smaybee 	    dn->dn_object == DMU_META_DNODE_OBJECT ||
882c717a561Smaybee 	    dn->dn_objset->os_dsl_dataset == NULL ||
883c717a561Smaybee 	    dsl_dir_is_private(dn->dn_objset->os_dsl_dataset->ds_dir));
884fa9e4066Sahrens 
885fa9e4066Sahrens 	/*
886fa9e4066Sahrens 	 * We make this assert for private objects as well, but after we
887fa9e4066Sahrens 	 * check if we're already dirty.  They are allowed to re-dirty
888fa9e4066Sahrens 	 * in syncing context.
889fa9e4066Sahrens 	 */
890ea8dc4b6Seschrock 	ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
891c717a561Smaybee 	    dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
892fa9e4066Sahrens 	    (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
893fa9e4066Sahrens 
894fa9e4066Sahrens 	mutex_enter(&db->db_mtx);
895fa9e4066Sahrens 	/*
896c717a561Smaybee 	 * XXX make this true for indirects too?  The problem is that
897c717a561Smaybee 	 * transactions created with dmu_tx_create_assigned() from
898c717a561Smaybee 	 * syncing context don't bother holding ahead.
899fa9e4066Sahrens 	 */
900c717a561Smaybee 	ASSERT(db->db_level != 0 ||
901c717a561Smaybee 	    db->db_state == DB_CACHED || db->db_state == DB_FILL);
902fa9e4066Sahrens 
903fa9e4066Sahrens 	mutex_enter(&dn->dn_mtx);
904fa9e4066Sahrens 	/*
905fa9e4066Sahrens 	 * Don't set dirtyctx to SYNC if we're just modifying this as we
906fa9e4066Sahrens 	 * initialize the objset.
907fa9e4066Sahrens 	 */
908fa9e4066Sahrens 	if (dn->dn_dirtyctx == DN_UNDIRTIED &&
909c717a561Smaybee 	    !BP_IS_HOLE(dn->dn_objset->os_rootbp)) {
910fa9e4066Sahrens 		dn->dn_dirtyctx =
911fa9e4066Sahrens 		    (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN);
912fa9e4066Sahrens 		ASSERT(dn->dn_dirtyctx_firstset == NULL);
913fa9e4066Sahrens 		dn->dn_dirtyctx_firstset = kmem_alloc(1, KM_SLEEP);
914fa9e4066Sahrens 	}
915fa9e4066Sahrens 	mutex_exit(&dn->dn_mtx);
916fa9e4066Sahrens 
917fa9e4066Sahrens 	/*
918fa9e4066Sahrens 	 * If this buffer is already dirty, we're done.
919fa9e4066Sahrens 	 */
920c717a561Smaybee 	drp = &db->db_last_dirty;
921c717a561Smaybee 	ASSERT(*drp == NULL || (*drp)->dr_txg <= tx->tx_txg ||
922c717a561Smaybee 	    db->db.db_object == DMU_META_DNODE_OBJECT);
923c717a561Smaybee 	while (*drp && (*drp)->dr_txg > tx->tx_txg)
924c717a561Smaybee 		drp = &(*drp)->dr_next;
925c717a561Smaybee 	if (*drp && (*drp)->dr_txg == tx->tx_txg) {
926c717a561Smaybee 		if (db->db_level == 0 && db->db_blkid != DB_BONUS_BLKID) {
927c717a561Smaybee 			/*
928c717a561Smaybee 			 * If this buffer has already been written out,
929c717a561Smaybee 			 * we now need to reset its state.
930c717a561Smaybee 			 */
931c717a561Smaybee 			dbuf_unoverride(*drp);
932c717a561Smaybee 			if (db->db.db_object != DMU_META_DNODE_OBJECT)
933c717a561Smaybee 				arc_buf_thaw(db->db_buf);
934c717a561Smaybee 		}
935fa9e4066Sahrens 		mutex_exit(&db->db_mtx);
936c717a561Smaybee 		return (*drp);
937fa9e4066Sahrens 	}
938fa9e4066Sahrens 
939fa9e4066Sahrens 	/*
940fa9e4066Sahrens 	 * Only valid if not already dirty.
941fa9e4066Sahrens 	 */
942fa9e4066Sahrens 	ASSERT(dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
943fa9e4066Sahrens 	    (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
944fa9e4066Sahrens 
945fa9e4066Sahrens 	ASSERT3U(dn->dn_nlevels, >, db->db_level);
946fa9e4066Sahrens 	ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) ||
947fa9e4066Sahrens 	    dn->dn_phys->dn_nlevels > db->db_level ||
948fa9e4066Sahrens 	    dn->dn_next_nlevels[txgoff] > db->db_level ||
949fa9e4066Sahrens 	    dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level ||
950fa9e4066Sahrens 	    dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level);
951fa9e4066Sahrens 
952fa9e4066Sahrens 	/*
953fa9e4066Sahrens 	 * We should only be dirtying in syncing context if it's the
954fa9e4066Sahrens 	 * mos, a spa os, or we're initializing the os.  However, we are
955fa9e4066Sahrens 	 * allowed to dirty in syncing context provided we already
956fa9e4066Sahrens 	 * dirtied it in open context.  Hence we must make this
957fa9e4066Sahrens 	 * assertion only if we're not already dirty.
958fa9e4066Sahrens 	 */
959fa9e4066Sahrens 	ASSERT(!dmu_tx_is_syncing(tx) ||
960fa9e4066Sahrens 	    os->os_dsl_dataset == NULL ||
961fa9e4066Sahrens 	    !dsl_dir_is_private(os->os_dsl_dataset->ds_dir) ||
962c717a561Smaybee 	    !BP_IS_HOLE(os->os_rootbp));
963fa9e4066Sahrens 	ASSERT(db->db.db_size != 0);
964fa9e4066Sahrens 
965fa9e4066Sahrens 	dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
966fa9e4066Sahrens 
967ea8dc4b6Seschrock 	/*
968ea8dc4b6Seschrock 	 * If this buffer is dirty in an old transaction group we need
969ea8dc4b6Seschrock 	 * to make a copy of it so that the changes we make in this
970ea8dc4b6Seschrock 	 * transaction group won't leak out when we sync the older txg.
971ea8dc4b6Seschrock 	 */
972c717a561Smaybee 	dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP);
973c717a561Smaybee 	if (db->db_level == 0) {
974c717a561Smaybee 		void *data_old = db->db_buf;
975c717a561Smaybee 
976c717a561Smaybee 		if (db->db_blkid == DB_BONUS_BLKID) {
977c717a561Smaybee 			dbuf_fix_old_data(db, tx->tx_txg);
978c717a561Smaybee 			data_old = db->db.db_data;
979c717a561Smaybee 		} else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
980c717a561Smaybee 			/*
981c717a561Smaybee 			 * Release the data buffer from the cache so that we
982c717a561Smaybee 			 * can modify it without impacting possible other users
983c717a561Smaybee 			 * of this cached data block.  Note that indirect
984c717a561Smaybee 			 * blocks and private objects are not released until the
985c717a561Smaybee 			 * syncing state (since they are only modified then).
986c717a561Smaybee 			 */
987fa9e4066Sahrens 			arc_release(db->db_buf, db);
988fa9e4066Sahrens 			dbuf_fix_old_data(db, tx->tx_txg);
989c717a561Smaybee 			data_old = db->db_buf;
990fa9e4066Sahrens 		}
991c717a561Smaybee 		ASSERT(data_old != NULL);
992c717a561Smaybee 		dr->dt.dl.dr_data = data_old;
993c717a561Smaybee 	} else {
994c717a561Smaybee 		mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_DEFAULT, NULL);
995c717a561Smaybee 		list_create(&dr->dt.di.dr_children,
996c717a561Smaybee 		    sizeof (dbuf_dirty_record_t),
997c717a561Smaybee 		    offsetof(dbuf_dirty_record_t, dr_dirty_node));
998fa9e4066Sahrens 	}
999c717a561Smaybee 	dr->dr_dbuf = db;
1000c717a561Smaybee 	dr->dr_txg = tx->tx_txg;
1001c717a561Smaybee 	dr->dr_next = *drp;
1002c717a561Smaybee 	*drp = dr;
1003fa9e4066Sahrens 
1004fa9e4066Sahrens 	/*
1005fa9e4066Sahrens 	 * We could have been freed_in_flight between the dbuf_noread
1006fa9e4066Sahrens 	 * and dbuf_dirty.  We win, as though the dbuf_noread() had
1007fa9e4066Sahrens 	 * happened after the free.
1008fa9e4066Sahrens 	 */
1009fa9e4066Sahrens 	if (db->db_level == 0 && db->db_blkid != DB_BONUS_BLKID) {
1010c717a561Smaybee 		mutex_enter(&dn->dn_mtx);
1011fa9e4066Sahrens 		dnode_clear_range(dn, db->db_blkid, 1, tx);
1012c717a561Smaybee 		mutex_exit(&dn->dn_mtx);
1013c717a561Smaybee 		db->db_freed_in_flight = FALSE;
1014fa9e4066Sahrens 	}
1015fa9e4066Sahrens 
1016fa9e4066Sahrens 	if (db->db_blkid != DB_BONUS_BLKID) {
1017ea8dc4b6Seschrock 		/*
1018ea8dc4b6Seschrock 		 * Update the accounting.
1019ea8dc4b6Seschrock 		 */
1020ea8dc4b6Seschrock 		if (!dbuf_new_block(db) && db->db_blkptr) {
1021fa9e4066Sahrens 			/*
1022fa9e4066Sahrens 			 * This is only a guess -- if the dbuf is dirty
1023fa9e4066Sahrens 			 * in a previous txg, we don't know how much
1024fa9e4066Sahrens 			 * space it will use on disk yet.  We should
1025fa9e4066Sahrens 			 * really have the struct_rwlock to access
1026fa9e4066Sahrens 			 * db_blkptr, but since this is just a guess,
1027fa9e4066Sahrens 			 * it's OK if we get an odd answer.
1028fa9e4066Sahrens 			 */
1029fa9e4066Sahrens 			dnode_willuse_space(dn,
103099653d4eSeschrock 			    -bp_get_dasize(os->os_spa, db->db_blkptr), tx);
1031fa9e4066Sahrens 		}
1032fa9e4066Sahrens 		dnode_willuse_space(dn, db->db.db_size, tx);
1033fa9e4066Sahrens 	}
1034fa9e4066Sahrens 
1035fa9e4066Sahrens 	/*
1036fa9e4066Sahrens 	 * This buffer is now part of this txg
1037fa9e4066Sahrens 	 */
1038fa9e4066Sahrens 	dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg);
1039fa9e4066Sahrens 	db->db_dirtycnt += 1;
1040fa9e4066Sahrens 	ASSERT3U(db->db_dirtycnt, <=, 3);
1041fa9e4066Sahrens 
1042fa9e4066Sahrens 	mutex_exit(&db->db_mtx);
1043fa9e4066Sahrens 
1044fa9e4066Sahrens 	if (db->db_blkid == DB_BONUS_BLKID) {
1045c717a561Smaybee 		mutex_enter(&dn->dn_mtx);
1046c717a561Smaybee 		ASSERT(!list_link_active(&dr->dr_dirty_node));
1047c717a561Smaybee 		list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1048c717a561Smaybee 		mutex_exit(&dn->dn_mtx);
1049fa9e4066Sahrens 		dnode_setdirty(dn, tx);
1050c717a561Smaybee 		return (dr);
1051fa9e4066Sahrens 	}
1052fa9e4066Sahrens 
105344eda4d7Smaybee 	if (db->db_level == 0) {
1054fa9e4066Sahrens 		dnode_new_blkid(dn, db->db_blkid, tx);
105544eda4d7Smaybee 		ASSERT(dn->dn_maxblkid >= db->db_blkid);
105644eda4d7Smaybee 	}
1057fa9e4066Sahrens 
1058fa9e4066Sahrens 	if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
1059fa9e4066Sahrens 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
1060fa9e4066Sahrens 		drop_struct_lock = TRUE;
1061fa9e4066Sahrens 	}
1062fa9e4066Sahrens 
106344eda4d7Smaybee 	if (db->db_level+1 < dn->dn_nlevels) {
1064c717a561Smaybee 		dmu_buf_impl_t *parent = db->db_parent;
1065c717a561Smaybee 		dbuf_dirty_record_t *di;
1066c717a561Smaybee 		int parent_held = FALSE;
1067c717a561Smaybee 
1068c717a561Smaybee 		if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
1069c717a561Smaybee 			int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1070c717a561Smaybee 
1071c717a561Smaybee 			parent = dbuf_hold_level(dn, db->db_level+1,
1072c717a561Smaybee 			    db->db_blkid >> epbs, FTAG);
1073c717a561Smaybee 			parent_held = TRUE;
1074c717a561Smaybee 		}
1075fa9e4066Sahrens 		if (drop_struct_lock)
1076fa9e4066Sahrens 			rw_exit(&dn->dn_struct_rwlock);
1077c717a561Smaybee 		ASSERT3U(db->db_level+1, ==, parent->db_level);
1078c717a561Smaybee 		di = dbuf_dirty(parent, tx);
1079c717a561Smaybee 		if (parent_held)
1080c717a561Smaybee 			dbuf_rele(parent, FTAG);
1081c717a561Smaybee 
1082c717a561Smaybee 		mutex_enter(&db->db_mtx);
1083c717a561Smaybee 		/*  possible race with dbuf_undirty() */
1084c717a561Smaybee 		if (db->db_last_dirty == dr ||
1085c717a561Smaybee 		    dn->dn_object == DMU_META_DNODE_OBJECT) {
1086c717a561Smaybee 			mutex_enter(&di->dt.di.dr_mtx);
1087c717a561Smaybee 			ASSERT3U(di->dr_txg, ==, tx->tx_txg);
1088c717a561Smaybee 			ASSERT(!list_link_active(&dr->dr_dirty_node));
1089c717a561Smaybee 			list_insert_tail(&di->dt.di.dr_children, dr);
1090c717a561Smaybee 			mutex_exit(&di->dt.di.dr_mtx);
1091c717a561Smaybee 			dr->dr_parent = di;
1092c717a561Smaybee 		}
1093c717a561Smaybee 		mutex_exit(&db->db_mtx);
1094fa9e4066Sahrens 	} else {
1095c717a561Smaybee 		ASSERT(db->db_level+1 == dn->dn_nlevels);
1096c717a561Smaybee 		ASSERT(db->db_blkid < dn->dn_nblkptr);
1097c717a561Smaybee 		ASSERT(db->db_parent == NULL ||
1098c717a561Smaybee 		    db->db_parent == db->db_dnode->dn_dbuf);
1099c717a561Smaybee 		mutex_enter(&dn->dn_mtx);
1100c717a561Smaybee 		ASSERT(!list_link_active(&dr->dr_dirty_node));
1101c717a561Smaybee 		list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1102c717a561Smaybee 		mutex_exit(&dn->dn_mtx);
1103fa9e4066Sahrens 		if (drop_struct_lock)
1104fa9e4066Sahrens 			rw_exit(&dn->dn_struct_rwlock);
1105fa9e4066Sahrens 	}
1106fa9e4066Sahrens 
1107fa9e4066Sahrens 	dnode_setdirty(dn, tx);
1108c717a561Smaybee 	return (dr);
1109fa9e4066Sahrens }
1110fa9e4066Sahrens 
1111fa9e4066Sahrens static int
1112fa9e4066Sahrens dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1113fa9e4066Sahrens {
1114fa9e4066Sahrens 	dnode_t *dn = db->db_dnode;
1115c717a561Smaybee 	uint64_t txg = tx->tx_txg;
1116c717a561Smaybee 	dbuf_dirty_record_t *dr;
1117fa9e4066Sahrens 
1118c717a561Smaybee 	ASSERT(txg != 0);
1119ea8dc4b6Seschrock 	ASSERT(db->db_blkid != DB_BONUS_BLKID);
1120fa9e4066Sahrens 
1121fa9e4066Sahrens 	mutex_enter(&db->db_mtx);
1122fa9e4066Sahrens 
1123fa9e4066Sahrens 	/*
1124fa9e4066Sahrens 	 * If this buffer is not dirty, we're done.
1125fa9e4066Sahrens 	 */
1126c717a561Smaybee 	for (dr = db->db_last_dirty; dr; dr = dr->dr_next)
1127c717a561Smaybee 		if (dr->dr_txg <= txg)
1128c717a561Smaybee 			break;
1129c717a561Smaybee 	if (dr == NULL || dr->dr_txg < txg) {
1130fa9e4066Sahrens 		mutex_exit(&db->db_mtx);
1131fa9e4066Sahrens 		return (0);
1132fa9e4066Sahrens 	}
1133c717a561Smaybee 	ASSERT(dr->dr_txg == txg);
1134fa9e4066Sahrens 
1135fa9e4066Sahrens 	/*
1136fa9e4066Sahrens 	 * If this buffer is currently held, we cannot undirty
1137fa9e4066Sahrens 	 * it, since one of the current holders may be in the
1138fa9e4066Sahrens 	 * middle of an update.  Note that users of dbuf_undirty()
1139fa9e4066Sahrens 	 * should not place a hold on the dbuf before the call.
1140fa9e4066Sahrens 	 */
1141fa9e4066Sahrens 	if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
1142fa9e4066Sahrens 		mutex_exit(&db->db_mtx);
114344eda4d7Smaybee 		/* Make sure we don't toss this buffer at sync phase */
1144fa9e4066Sahrens 		mutex_enter(&dn->dn_mtx);
1145fa9e4066Sahrens 		dnode_clear_range(dn, db->db_blkid, 1, tx);
1146fa9e4066Sahrens 		mutex_exit(&dn->dn_mtx);
1147fa9e4066Sahrens 		return (0);
1148fa9e4066Sahrens 	}
1149fa9e4066Sahrens 
1150fa9e4066Sahrens 	dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1151fa9e4066Sahrens 
1152fa9e4066Sahrens 	ASSERT(db->db.db_size != 0);
1153fa9e4066Sahrens 
1154fa9e4066Sahrens 	/* XXX would be nice to fix up dn_towrite_space[] */
1155fa9e4066Sahrens 
1156c717a561Smaybee 	db->db_last_dirty = dr->dr_next;
1157c717a561Smaybee 
1158c717a561Smaybee 	if (dr->dr_parent) {
1159c717a561Smaybee 		mutex_enter(&dr->dr_parent->dt.di.dr_mtx);
1160c717a561Smaybee 		list_remove(&dr->dr_parent->dt.di.dr_children, dr);
1161c717a561Smaybee 		mutex_exit(&dr->dr_parent->dt.di.dr_mtx);
1162c717a561Smaybee 	} else if (db->db_level+1 == dn->dn_nlevels) {
1163c717a561Smaybee 		ASSERT3P(db->db_parent, ==, dn->dn_dbuf);
1164c717a561Smaybee 		mutex_enter(&dn->dn_mtx);
1165c717a561Smaybee 		list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr);
1166c717a561Smaybee 		mutex_exit(&dn->dn_mtx);
1167c717a561Smaybee 	}
1168c717a561Smaybee 
1169c717a561Smaybee 	if (db->db_level == 0) {
1170c717a561Smaybee 		dbuf_unoverride(dr);
1171c717a561Smaybee 
1172c717a561Smaybee 		ASSERT(db->db_buf != NULL);
1173c717a561Smaybee 		ASSERT(dr->dt.dl.dr_data != NULL);
1174c717a561Smaybee 		if (dr->dt.dl.dr_data != db->db_buf)
1175c717a561Smaybee 			VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data, db) == 1);
1176c717a561Smaybee 	} else {
1177c717a561Smaybee 		ASSERT(db->db_buf != NULL);
1178c717a561Smaybee 		ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
1179c717a561Smaybee 		/* XXX - mutex and list destroy? */
1180c717a561Smaybee 	}
1181c717a561Smaybee 	kmem_free(dr, sizeof (dbuf_dirty_record_t));
1182fa9e4066Sahrens 
1183fa9e4066Sahrens 	ASSERT(db->db_dirtycnt > 0);
1184fa9e4066Sahrens 	db->db_dirtycnt -= 1;
1185fa9e4066Sahrens 
1186c717a561Smaybee 	if (refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) {
1187ea8dc4b6Seschrock 		arc_buf_t *buf = db->db_buf;
1188fa9e4066Sahrens 
1189ea8dc4b6Seschrock 		ASSERT(arc_released(buf));
1190ea8dc4b6Seschrock 		dbuf_set_data(db, NULL);
1191ea8dc4b6Seschrock 		VERIFY(arc_buf_remove_ref(buf, db) == 1);
1192fa9e4066Sahrens 		dbuf_evict(db);
1193fa9e4066Sahrens 		return (1);
1194fa9e4066Sahrens 	}
1195fa9e4066Sahrens 
1196fa9e4066Sahrens 	mutex_exit(&db->db_mtx);
1197fa9e4066Sahrens 	return (0);
1198fa9e4066Sahrens }
1199fa9e4066Sahrens 
1200fa9e4066Sahrens #pragma weak dmu_buf_will_dirty = dbuf_will_dirty
1201fa9e4066Sahrens void
1202fa9e4066Sahrens dbuf_will_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1203fa9e4066Sahrens {
1204fa9e4066Sahrens 	int rf = DB_RF_MUST_SUCCEED;
1205fa9e4066Sahrens 
1206fa9e4066Sahrens 	ASSERT(tx->tx_txg != 0);
1207fa9e4066Sahrens 	ASSERT(!refcount_is_zero(&db->db_holds));
1208fa9e4066Sahrens 
1209fa9e4066Sahrens 	if (RW_WRITE_HELD(&db->db_dnode->dn_struct_rwlock))
1210fa9e4066Sahrens 		rf |= DB_RF_HAVESTRUCT;
1211ea8dc4b6Seschrock 	(void) dbuf_read(db, NULL, rf);
1212c717a561Smaybee 	(void) dbuf_dirty(db, tx);
1213fa9e4066Sahrens }
1214fa9e4066Sahrens 
1215fa9e4066Sahrens void
1216ea8dc4b6Seschrock dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1217fa9e4066Sahrens {
1218ea8dc4b6Seschrock 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1219ea8dc4b6Seschrock 
1220ea8dc4b6Seschrock 	ASSERT(db->db_blkid != DB_BONUS_BLKID);
1221fa9e4066Sahrens 	ASSERT(tx->tx_txg != 0);
1222fa9e4066Sahrens 	ASSERT(db->db_level == 0);
1223fa9e4066Sahrens 	ASSERT(!refcount_is_zero(&db->db_holds));
1224fa9e4066Sahrens 
1225ea8dc4b6Seschrock 	ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
1226fa9e4066Sahrens 	    dmu_tx_private_ok(tx));
1227fa9e4066Sahrens 
1228fa9e4066Sahrens 	dbuf_noread(db);
1229c717a561Smaybee 	(void) dbuf_dirty(db, tx);
1230fa9e4066Sahrens }
1231fa9e4066Sahrens 
1232fa9e4066Sahrens #pragma weak dmu_buf_fill_done = dbuf_fill_done
1233fa9e4066Sahrens /* ARGSUSED */
1234fa9e4066Sahrens void
1235fa9e4066Sahrens dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx)
1236fa9e4066Sahrens {
1237fa9e4066Sahrens 	mutex_enter(&db->db_mtx);
12389c9dc39aSek 	DBUF_VERIFY(db);
1239fa9e4066Sahrens 
1240fa9e4066Sahrens 	if (db->db_state == DB_FILL) {
1241c717a561Smaybee 		if (db->db_level == 0 && db->db_freed_in_flight) {
1242ea8dc4b6Seschrock 			ASSERT(db->db_blkid != DB_BONUS_BLKID);
1243fa9e4066Sahrens 			/* we were freed while filling */
1244fa9e4066Sahrens 			/* XXX dbuf_undirty? */
1245fa9e4066Sahrens 			bzero(db->db.db_data, db->db.db_size);
1246c717a561Smaybee 			db->db_freed_in_flight = FALSE;
1247fa9e4066Sahrens 		}
1248fa9e4066Sahrens 		db->db_state = DB_CACHED;
1249fa9e4066Sahrens 		cv_broadcast(&db->db_changed);
1250fa9e4066Sahrens 	}
1251fa9e4066Sahrens 	mutex_exit(&db->db_mtx);
1252fa9e4066Sahrens }
1253fa9e4066Sahrens 
1254ea8dc4b6Seschrock /*
1255ea8dc4b6Seschrock  * "Clear" the contents of this dbuf.  This will mark the dbuf
1256ea8dc4b6Seschrock  * EVICTING and clear *most* of its references.  Unfortunetely,
1257ea8dc4b6Seschrock  * when we are not holding the dn_dbufs_mtx, we can't clear the
1258ea8dc4b6Seschrock  * entry in the dn_dbufs list.  We have to wait until dbuf_destroy()
1259ea8dc4b6Seschrock  * in this case.  For callers from the DMU we will usually see:
1260ea8dc4b6Seschrock  *	dbuf_clear()->arc_buf_evict()->dbuf_do_evict()->dbuf_destroy()
1261ea8dc4b6Seschrock  * For the arc callback, we will usually see:
1262ea8dc4b6Seschrock  * 	dbuf_do_evict()->dbuf_clear();dbuf_destroy()
1263ea8dc4b6Seschrock  * Sometimes, though, we will get a mix of these two:
1264ea8dc4b6Seschrock  *	DMU: dbuf_clear()->arc_buf_evict()
1265ea8dc4b6Seschrock  *	ARC: dbuf_do_evict()->dbuf_destroy()
1266ea8dc4b6Seschrock  */
1267ea8dc4b6Seschrock void
1268fa9e4066Sahrens dbuf_clear(dmu_buf_impl_t *db)
1269fa9e4066Sahrens {
1270fa9e4066Sahrens 	dnode_t *dn = db->db_dnode;
1271ea8dc4b6Seschrock 	dmu_buf_impl_t *parent = db->db_parent;
1272c543ec06Sahrens 	dmu_buf_impl_t *dndb = dn->dn_dbuf;
1273ea8dc4b6Seschrock 	int dbuf_gone = FALSE;
1274fa9e4066Sahrens 
1275fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&db->db_mtx));
1276fa9e4066Sahrens 	ASSERT(refcount_is_zero(&db->db_holds));
1277fa9e4066Sahrens 
1278ea8dc4b6Seschrock 	dbuf_evict_user(db);
1279ea8dc4b6Seschrock 
1280fa9e4066Sahrens 	if (db->db_state == DB_CACHED) {
1281ea8dc4b6Seschrock 		ASSERT(db->db.db_data != NULL);
1282*0e8c6158Smaybee 		if (db->db_blkid == DB_BONUS_BLKID) {
1283ea8dc4b6Seschrock 			zio_buf_free(db->db.db_data, DN_MAX_BONUSLEN);
1284*0e8c6158Smaybee 			arc_space_return(DN_MAX_BONUSLEN);
1285*0e8c6158Smaybee 		}
1286fa9e4066Sahrens 		db->db.db_data = NULL;
1287fa9e4066Sahrens 		db->db_state = DB_UNCACHED;
1288fa9e4066Sahrens 	}
1289fa9e4066Sahrens 
1290fa9e4066Sahrens 	ASSERT3U(db->db_state, ==, DB_UNCACHED);
1291fa9e4066Sahrens 	ASSERT(db->db_data_pending == NULL);
1292fa9e4066Sahrens 
1293ea8dc4b6Seschrock 	db->db_state = DB_EVICTING;
1294ea8dc4b6Seschrock 	db->db_blkptr = NULL;
1295ea8dc4b6Seschrock 
1296ea8dc4b6Seschrock 	if (db->db_blkid != DB_BONUS_BLKID && MUTEX_HELD(&dn->dn_dbufs_mtx)) {
1297ea8dc4b6Seschrock 		list_remove(&dn->dn_dbufs, db);
1298ea8dc4b6Seschrock 		dnode_rele(dn, db);
1299ea8dc4b6Seschrock 	}
1300ea8dc4b6Seschrock 
1301ea8dc4b6Seschrock 	if (db->db_buf)
1302ea8dc4b6Seschrock 		dbuf_gone = arc_buf_evict(db->db_buf);
1303ea8dc4b6Seschrock 
1304ea8dc4b6Seschrock 	if (!dbuf_gone)
1305ea8dc4b6Seschrock 		mutex_exit(&db->db_mtx);
1306fa9e4066Sahrens 
1307fa9e4066Sahrens 	/*
1308fa9e4066Sahrens 	 * If this dbuf is referened from an indirect dbuf,
1309fa9e4066Sahrens 	 * decrement the ref count on the indirect dbuf.
1310fa9e4066Sahrens 	 */
1311c543ec06Sahrens 	if (parent && parent != dndb)
1312ea8dc4b6Seschrock 		dbuf_rele(parent, db);
1313fa9e4066Sahrens }
1314fa9e4066Sahrens 
1315fa9e4066Sahrens static int
1316fa9e4066Sahrens dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse,
1317fa9e4066Sahrens     dmu_buf_impl_t **parentp, blkptr_t **bpp)
1318fa9e4066Sahrens {
1319fa9e4066Sahrens 	int nlevels, epbs;
1320fa9e4066Sahrens 
13210b69c2f0Sahrens 	*parentp = NULL;
13220b69c2f0Sahrens 	*bpp = NULL;
13230b69c2f0Sahrens 
1324ea8dc4b6Seschrock 	ASSERT(blkid != DB_BONUS_BLKID);
1325ea8dc4b6Seschrock 
1326fa9e4066Sahrens 	if (dn->dn_phys->dn_nlevels == 0)
1327fa9e4066Sahrens 		nlevels = 1;
1328fa9e4066Sahrens 	else
1329fa9e4066Sahrens 		nlevels = dn->dn_phys->dn_nlevels;
1330fa9e4066Sahrens 
1331fa9e4066Sahrens 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1332fa9e4066Sahrens 
1333fa9e4066Sahrens 	ASSERT3U(level * epbs, <, 64);
1334fa9e4066Sahrens 	ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1335ea8dc4b6Seschrock 	if (level >= nlevels ||
1336fa9e4066Sahrens 	    (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) {
1337fa9e4066Sahrens 		/* the buffer has no parent yet */
1338fa9e4066Sahrens 		return (ENOENT);
1339fa9e4066Sahrens 	} else if (level < nlevels-1) {
1340fa9e4066Sahrens 		/* this block is referenced from an indirect block */
1341fa9e4066Sahrens 		int err = dbuf_hold_impl(dn, level+1,
1342fa9e4066Sahrens 		    blkid >> epbs, fail_sparse, NULL, parentp);
1343fa9e4066Sahrens 		if (err)
1344fa9e4066Sahrens 			return (err);
1345ea8dc4b6Seschrock 		err = dbuf_read(*parentp, NULL,
1346ea8dc4b6Seschrock 		    (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL));
1347c543ec06Sahrens 		if (err) {
1348c543ec06Sahrens 			dbuf_rele(*parentp, NULL);
1349c543ec06Sahrens 			*parentp = NULL;
1350c543ec06Sahrens 			return (err);
1351ea8dc4b6Seschrock 		}
1352c543ec06Sahrens 		*bpp = ((blkptr_t *)(*parentp)->db.db_data) +
1353c543ec06Sahrens 		    (blkid & ((1ULL << epbs) - 1));
1354c543ec06Sahrens 		return (0);
1355fa9e4066Sahrens 	} else {
1356fa9e4066Sahrens 		/* the block is referenced from the dnode */
1357fa9e4066Sahrens 		ASSERT3U(level, ==, nlevels-1);
1358fa9e4066Sahrens 		ASSERT(dn->dn_phys->dn_nblkptr == 0 ||
1359fa9e4066Sahrens 		    blkid < dn->dn_phys->dn_nblkptr);
1360c543ec06Sahrens 		if (dn->dn_dbuf) {
1361c543ec06Sahrens 			dbuf_add_ref(dn->dn_dbuf, NULL);
1362c543ec06Sahrens 			*parentp = dn->dn_dbuf;
1363c543ec06Sahrens 		}
1364fa9e4066Sahrens 		*bpp = &dn->dn_phys->dn_blkptr[blkid];
1365fa9e4066Sahrens 		return (0);
1366fa9e4066Sahrens 	}
1367fa9e4066Sahrens }
1368fa9e4066Sahrens 
1369fa9e4066Sahrens static dmu_buf_impl_t *
1370fa9e4066Sahrens dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid,
1371fa9e4066Sahrens     dmu_buf_impl_t *parent, blkptr_t *blkptr)
1372fa9e4066Sahrens {
1373fa9e4066Sahrens 	objset_impl_t *os = dn->dn_objset;
1374fa9e4066Sahrens 	dmu_buf_impl_t *db, *odb;
1375fa9e4066Sahrens 
1376fa9e4066Sahrens 	ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1377fa9e4066Sahrens 	ASSERT(dn->dn_type != DMU_OT_NONE);
1378fa9e4066Sahrens 
1379fa9e4066Sahrens 	db = kmem_cache_alloc(dbuf_cache, KM_SLEEP);
1380fa9e4066Sahrens 
1381fa9e4066Sahrens 	db->db_objset = os;
1382fa9e4066Sahrens 	db->db.db_object = dn->dn_object;
1383fa9e4066Sahrens 	db->db_level = level;
1384fa9e4066Sahrens 	db->db_blkid = blkid;
1385c717a561Smaybee 	db->db_last_dirty = NULL;
1386ea8dc4b6Seschrock 	db->db_dirtycnt = 0;
1387ea8dc4b6Seschrock 	db->db_dnode = dn;
1388ea8dc4b6Seschrock 	db->db_parent = parent;
1389ea8dc4b6Seschrock 	db->db_blkptr = blkptr;
1390fa9e4066Sahrens 
1391c717a561Smaybee 	db->db_user_ptr = NULL;
1392c717a561Smaybee 	db->db_user_data_ptr_ptr = NULL;
1393c717a561Smaybee 	db->db_evict_func = NULL;
1394c717a561Smaybee 	db->db_immediate_evict = 0;
1395c717a561Smaybee 	db->db_freed_in_flight = 0;
1396ea8dc4b6Seschrock 
1397ea8dc4b6Seschrock 	if (blkid == DB_BONUS_BLKID) {
1398ea8dc4b6Seschrock 		ASSERT3P(parent, ==, dn->dn_dbuf);
1399fa9e4066Sahrens 		db->db.db_size = dn->dn_bonuslen;
1400fa9e4066Sahrens 		db->db.db_offset = DB_BONUS_BLKID;
1401ea8dc4b6Seschrock 		db->db_state = DB_UNCACHED;
1402ea8dc4b6Seschrock 		/* the bonus dbuf is not placed in the hash table */
1403*0e8c6158Smaybee 		arc_space_consume(sizeof (dmu_buf_impl_t));
1404ea8dc4b6Seschrock 		return (db);
1405fa9e4066Sahrens 	} else {
1406fa9e4066Sahrens 		int blocksize =
1407fa9e4066Sahrens 		    db->db_level ? 1<<dn->dn_indblkshift :  dn->dn_datablksz;
1408fa9e4066Sahrens 		db->db.db_size = blocksize;
1409fa9e4066Sahrens 		db->db.db_offset = db->db_blkid * blocksize;
1410fa9e4066Sahrens 	}
1411fa9e4066Sahrens 
1412fa9e4066Sahrens 	/*
1413fa9e4066Sahrens 	 * Hold the dn_dbufs_mtx while we get the new dbuf
1414fa9e4066Sahrens 	 * in the hash table *and* added to the dbufs list.
1415fa9e4066Sahrens 	 * This prevents a possible deadlock with someone
1416fa9e4066Sahrens 	 * trying to look up this dbuf before its added to the
1417fa9e4066Sahrens 	 * dn_dbufs list.
1418fa9e4066Sahrens 	 */
1419fa9e4066Sahrens 	mutex_enter(&dn->dn_dbufs_mtx);
1420ea8dc4b6Seschrock 	db->db_state = DB_EVICTING;
1421fa9e4066Sahrens 	if ((odb = dbuf_hash_insert(db)) != NULL) {
1422fa9e4066Sahrens 		/* someone else inserted it first */
1423fa9e4066Sahrens 		kmem_cache_free(dbuf_cache, db);
1424fa9e4066Sahrens 		mutex_exit(&dn->dn_dbufs_mtx);
1425fa9e4066Sahrens 		return (odb);
1426fa9e4066Sahrens 	}
1427fa9e4066Sahrens 	list_insert_head(&dn->dn_dbufs, db);
1428ea8dc4b6Seschrock 	db->db_state = DB_UNCACHED;
1429fa9e4066Sahrens 	mutex_exit(&dn->dn_dbufs_mtx);
1430*0e8c6158Smaybee 	arc_space_consume(sizeof (dmu_buf_impl_t));
1431fa9e4066Sahrens 
1432fa9e4066Sahrens 	if (parent && parent != dn->dn_dbuf)
1433fa9e4066Sahrens 		dbuf_add_ref(parent, db);
1434fa9e4066Sahrens 
1435ea8dc4b6Seschrock 	ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1436ea8dc4b6Seschrock 	    refcount_count(&dn->dn_holds) > 0);
1437fa9e4066Sahrens 	(void) refcount_add(&dn->dn_holds, db);
1438fa9e4066Sahrens 
1439fa9e4066Sahrens 	dprintf_dbuf(db, "db=%p\n", db);
1440fa9e4066Sahrens 
1441fa9e4066Sahrens 	return (db);
1442fa9e4066Sahrens }
1443fa9e4066Sahrens 
1444fa9e4066Sahrens static int
1445ea8dc4b6Seschrock dbuf_do_evict(void *private)
1446fa9e4066Sahrens {
1447ea8dc4b6Seschrock 	arc_buf_t *buf = private;
1448ea8dc4b6Seschrock 	dmu_buf_impl_t *db = buf->b_private;
1449fa9e4066Sahrens 
1450ea8dc4b6Seschrock 	if (!MUTEX_HELD(&db->db_mtx))
1451ea8dc4b6Seschrock 		mutex_enter(&db->db_mtx);
1452fa9e4066Sahrens 
1453ea8dc4b6Seschrock 	ASSERT(refcount_is_zero(&db->db_holds));
1454fa9e4066Sahrens 
1455ea8dc4b6Seschrock 	if (db->db_state != DB_EVICTING) {
1456ea8dc4b6Seschrock 		ASSERT(db->db_state == DB_CACHED);
1457ea8dc4b6Seschrock 		DBUF_VERIFY(db);
1458ea8dc4b6Seschrock 		db->db_buf = NULL;
1459ea8dc4b6Seschrock 		dbuf_evict(db);
1460ea8dc4b6Seschrock 	} else {
1461ea8dc4b6Seschrock 		mutex_exit(&db->db_mtx);
1462ea8dc4b6Seschrock 		dbuf_destroy(db);
1463fa9e4066Sahrens 	}
1464ea8dc4b6Seschrock 	return (0);
1465fa9e4066Sahrens }
1466fa9e4066Sahrens 
1467fa9e4066Sahrens static void
1468fa9e4066Sahrens dbuf_destroy(dmu_buf_impl_t *db)
1469fa9e4066Sahrens {
1470fa9e4066Sahrens 	ASSERT(refcount_is_zero(&db->db_holds));
1471fa9e4066Sahrens 
1472ea8dc4b6Seschrock 	if (db->db_blkid != DB_BONUS_BLKID) {
1473ea8dc4b6Seschrock 		dnode_t *dn = db->db_dnode;
1474ea8dc4b6Seschrock 
1475ea8dc4b6Seschrock 		/*
1476ea8dc4b6Seschrock 		 * If this dbuf is still on the dn_dbufs list,
1477ea8dc4b6Seschrock 		 * remove it from that list.
1478ea8dc4b6Seschrock 		 */
1479ea8dc4b6Seschrock 		if (list_link_active(&db->db_link)) {
1480c543ec06Sahrens 			mutex_enter(&dn->dn_dbufs_mtx);
1481ea8dc4b6Seschrock 			list_remove(&dn->dn_dbufs, db);
1482c543ec06Sahrens 			mutex_exit(&dn->dn_dbufs_mtx);
1483ea8dc4b6Seschrock 
1484ea8dc4b6Seschrock 			dnode_rele(dn, db);
1485ea8dc4b6Seschrock 		}
1486ea8dc4b6Seschrock 		dbuf_hash_remove(db);
1487ea8dc4b6Seschrock 	}
1488ea8dc4b6Seschrock 	db->db_parent = NULL;
1489ea8dc4b6Seschrock 	db->db_dnode = NULL;
1490ea8dc4b6Seschrock 	db->db_buf = NULL;
1491ea8dc4b6Seschrock 
1492fa9e4066Sahrens 	ASSERT(db->db.db_data == NULL);
1493fa9e4066Sahrens 	ASSERT(db->db_hash_next == NULL);
1494fa9e4066Sahrens 	ASSERT(db->db_blkptr == NULL);
1495fa9e4066Sahrens 	ASSERT(db->db_data_pending == NULL);
1496fa9e4066Sahrens 
1497fa9e4066Sahrens 	kmem_cache_free(dbuf_cache, db);
1498*0e8c6158Smaybee 	arc_space_return(sizeof (dmu_buf_impl_t));
1499fa9e4066Sahrens }
1500fa9e4066Sahrens 
1501fa9e4066Sahrens void
1502fa9e4066Sahrens dbuf_prefetch(dnode_t *dn, uint64_t blkid)
1503fa9e4066Sahrens {
150413506d1eSmaybee 	dmu_buf_impl_t *db = NULL;
1505fa9e4066Sahrens 	blkptr_t *bp = NULL;
1506fa9e4066Sahrens 
1507fa9e4066Sahrens 	ASSERT(blkid != DB_BONUS_BLKID);
1508fa9e4066Sahrens 	ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1509fa9e4066Sahrens 
1510fa9e4066Sahrens 	if (dnode_block_freed(dn, blkid))
1511fa9e4066Sahrens 		return;
1512fa9e4066Sahrens 
1513fa9e4066Sahrens 	/* dbuf_find() returns with db_mtx held */
1514fa9e4066Sahrens 	if (db = dbuf_find(dn, 0, blkid)) {
151513506d1eSmaybee 		if (refcount_count(&db->db_holds) > 0) {
151613506d1eSmaybee 			/*
151713506d1eSmaybee 			 * This dbuf is active.  We assume that it is
151813506d1eSmaybee 			 * already CACHED, or else about to be either
151913506d1eSmaybee 			 * read or filled.
152013506d1eSmaybee 			 */
152113506d1eSmaybee 			mutex_exit(&db->db_mtx);
152213506d1eSmaybee 			return;
152313506d1eSmaybee 		}
1524fa9e4066Sahrens 		mutex_exit(&db->db_mtx);
15250b69c2f0Sahrens 		db = NULL;
1526fa9e4066Sahrens 	}
1527fa9e4066Sahrens 
152813506d1eSmaybee 	if (dbuf_findbp(dn, 0, blkid, TRUE, &db, &bp) == 0) {
1529fa9e4066Sahrens 		if (bp && !BP_IS_HOLE(bp)) {
153013506d1eSmaybee 			uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
1531ea8dc4b6Seschrock 			zbookmark_t zb;
1532ea8dc4b6Seschrock 			zb.zb_objset = dn->dn_objset->os_dsl_dataset ?
1533ea8dc4b6Seschrock 			    dn->dn_objset->os_dsl_dataset->ds_object : 0;
1534ea8dc4b6Seschrock 			zb.zb_object = dn->dn_object;
1535ea8dc4b6Seschrock 			zb.zb_level = 0;
1536ea8dc4b6Seschrock 			zb.zb_blkid = blkid;
1537ea8dc4b6Seschrock 
1538fa9e4066Sahrens 			(void) arc_read(NULL, dn->dn_objset->os_spa, bp,
1539fa9e4066Sahrens 			    dmu_ot[dn->dn_type].ot_byteswap,
1540fa9e4066Sahrens 			    NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
1541fa9e4066Sahrens 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
154213506d1eSmaybee 			    &aflags, &zb);
1543fa9e4066Sahrens 		}
154413506d1eSmaybee 		if (db)
154513506d1eSmaybee 			dbuf_rele(db, NULL);
1546fa9e4066Sahrens 	}
1547fa9e4066Sahrens }
1548fa9e4066Sahrens 
1549fa9e4066Sahrens /*
1550fa9e4066Sahrens  * Returns with db_holds incremented, and db_mtx not held.
1551fa9e4066Sahrens  * Note: dn_struct_rwlock must be held.
1552fa9e4066Sahrens  */
1553fa9e4066Sahrens int
1554fa9e4066Sahrens dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid, int fail_sparse,
1555fa9e4066Sahrens     void *tag, dmu_buf_impl_t **dbp)
1556fa9e4066Sahrens {
1557fa9e4066Sahrens 	dmu_buf_impl_t *db, *parent = NULL;
1558fa9e4066Sahrens 
1559ea8dc4b6Seschrock 	ASSERT(blkid != DB_BONUS_BLKID);
1560fa9e4066Sahrens 	ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1561fa9e4066Sahrens 	ASSERT3U(dn->dn_nlevels, >, level);
1562fa9e4066Sahrens 
1563fa9e4066Sahrens 	*dbp = NULL;
1564ea8dc4b6Seschrock top:
1565fa9e4066Sahrens 	/* dbuf_find() returns with db_mtx held */
1566fa9e4066Sahrens 	db = dbuf_find(dn, level, blkid);
1567fa9e4066Sahrens 
1568fa9e4066Sahrens 	if (db == NULL) {
1569fa9e4066Sahrens 		blkptr_t *bp = NULL;
1570fa9e4066Sahrens 		int err;
1571fa9e4066Sahrens 
1572c543ec06Sahrens 		ASSERT3P(parent, ==, NULL);
1573fa9e4066Sahrens 		err = dbuf_findbp(dn, level, blkid, fail_sparse, &parent, &bp);
1574fa9e4066Sahrens 		if (fail_sparse) {
1575fa9e4066Sahrens 			if (err == 0 && bp && BP_IS_HOLE(bp))
1576fa9e4066Sahrens 				err = ENOENT;
1577fa9e4066Sahrens 			if (err) {
1578c543ec06Sahrens 				if (parent)
1579ea8dc4b6Seschrock 					dbuf_rele(parent, NULL);
1580fa9e4066Sahrens 				return (err);
1581fa9e4066Sahrens 			}
1582fa9e4066Sahrens 		}
1583ea8dc4b6Seschrock 		if (err && err != ENOENT)
1584ea8dc4b6Seschrock 			return (err);
1585fa9e4066Sahrens 		db = dbuf_create(dn, level, blkid, parent, bp);
1586fa9e4066Sahrens 	}
1587fa9e4066Sahrens 
1588ea8dc4b6Seschrock 	if (db->db_buf && refcount_is_zero(&db->db_holds)) {
1589ea8dc4b6Seschrock 		arc_buf_add_ref(db->db_buf, db);
1590ea8dc4b6Seschrock 		if (db->db_buf->b_data == NULL) {
1591ea8dc4b6Seschrock 			dbuf_clear(db);
1592c543ec06Sahrens 			if (parent) {
1593c543ec06Sahrens 				dbuf_rele(parent, NULL);
1594c543ec06Sahrens 				parent = NULL;
1595c543ec06Sahrens 			}
1596ea8dc4b6Seschrock 			goto top;
1597ea8dc4b6Seschrock 		}
1598ea8dc4b6Seschrock 		ASSERT3P(db->db.db_data, ==, db->db_buf->b_data);
1599ea8dc4b6Seschrock 	}
1600ea8dc4b6Seschrock 
1601ea8dc4b6Seschrock 	ASSERT(db->db_buf == NULL || arc_referenced(db->db_buf));
1602ea8dc4b6Seschrock 
1603fa9e4066Sahrens 	/*
1604c717a561Smaybee 	 * If this buffer is currently syncing out, and we are are
1605c717a561Smaybee 	 * still referencing it from db_data, we need to make a copy
1606c717a561Smaybee 	 * of it in case we decide we want to dirty it again in this txg.
1607fa9e4066Sahrens 	 */
1608c717a561Smaybee 	if (db->db_level == 0 && db->db_blkid != DB_BONUS_BLKID &&
1609ea8dc4b6Seschrock 	    dn->dn_object != DMU_META_DNODE_OBJECT &&
1610c717a561Smaybee 	    db->db_state == DB_CACHED && db->db_data_pending) {
1611c717a561Smaybee 		dbuf_dirty_record_t *dr = db->db_data_pending;
1612fa9e4066Sahrens 
1613c717a561Smaybee 		if (dr->dt.dl.dr_data == db->db_buf) {
1614c717a561Smaybee 			arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1615c717a561Smaybee 
1616c717a561Smaybee 			dbuf_set_data(db,
1617c717a561Smaybee 			    arc_buf_alloc(db->db_dnode->dn_objset->os_spa,
1618c717a561Smaybee 			    db->db.db_size, db, type));
1619c717a561Smaybee 			bcopy(dr->dt.dl.dr_data->b_data, db->db.db_data,
1620c717a561Smaybee 			    db->db.db_size);
1621c717a561Smaybee 		}
1622fa9e4066Sahrens 	}
1623fa9e4066Sahrens 
1624ea8dc4b6Seschrock 	(void) refcount_add(&db->db_holds, tag);
1625fa9e4066Sahrens 	dbuf_update_data(db);
16269c9dc39aSek 	DBUF_VERIFY(db);
1627fa9e4066Sahrens 	mutex_exit(&db->db_mtx);
1628fa9e4066Sahrens 
1629fa9e4066Sahrens 	/* NOTE: we can't rele the parent until after we drop the db_mtx */
1630c543ec06Sahrens 	if (parent)
1631ea8dc4b6Seschrock 		dbuf_rele(parent, NULL);
1632fa9e4066Sahrens 
1633fa9e4066Sahrens 	ASSERT3P(db->db_dnode, ==, dn);
1634fa9e4066Sahrens 	ASSERT3U(db->db_blkid, ==, blkid);
1635fa9e4066Sahrens 	ASSERT3U(db->db_level, ==, level);
1636fa9e4066Sahrens 	*dbp = db;
1637fa9e4066Sahrens 
1638fa9e4066Sahrens 	return (0);
1639fa9e4066Sahrens }
1640fa9e4066Sahrens 
1641fa9e4066Sahrens dmu_buf_impl_t *
1642ea8dc4b6Seschrock dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag)
1643fa9e4066Sahrens {
1644fa9e4066Sahrens 	dmu_buf_impl_t *db;
1645ea8dc4b6Seschrock 	int err = dbuf_hold_impl(dn, 0, blkid, FALSE, tag, &db);
1646ea8dc4b6Seschrock 	return (err ? NULL : db);
1647fa9e4066Sahrens }
1648fa9e4066Sahrens 
1649fa9e4066Sahrens dmu_buf_impl_t *
1650fa9e4066Sahrens dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag)
1651fa9e4066Sahrens {
1652fa9e4066Sahrens 	dmu_buf_impl_t *db;
1653ea8dc4b6Seschrock 	int err = dbuf_hold_impl(dn, level, blkid, FALSE, tag, &db);
1654ea8dc4b6Seschrock 	return (err ? NULL : db);
1655fa9e4066Sahrens }
1656fa9e4066Sahrens 
1657fa9e4066Sahrens dmu_buf_impl_t *
1658ea8dc4b6Seschrock dbuf_create_bonus(dnode_t *dn)
1659fa9e4066Sahrens {
1660ea8dc4b6Seschrock 	dmu_buf_impl_t *db = dn->dn_bonus;
1661ea8dc4b6Seschrock 
1662ea8dc4b6Seschrock 	ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
1663ea8dc4b6Seschrock 
1664ea8dc4b6Seschrock 	ASSERT(dn->dn_bonus == NULL);
1665ea8dc4b6Seschrock 	db = dbuf_create(dn, 0, DB_BONUS_BLKID, dn->dn_dbuf, NULL);
1666fa9e4066Sahrens 	return (db);
1667fa9e4066Sahrens }
1668fa9e4066Sahrens 
1669ea8dc4b6Seschrock #pragma weak dmu_buf_add_ref = dbuf_add_ref
1670fa9e4066Sahrens void
1671fa9e4066Sahrens dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
1672fa9e4066Sahrens {
1673ea8dc4b6Seschrock 	int64_t holds = refcount_add(&db->db_holds, tag);
1674ea8dc4b6Seschrock 	ASSERT(holds > 1);
1675fa9e4066Sahrens }
1676fa9e4066Sahrens 
1677ea8dc4b6Seschrock #pragma weak dmu_buf_rele = dbuf_rele
1678fa9e4066Sahrens void
1679ea8dc4b6Seschrock dbuf_rele(dmu_buf_impl_t *db, void *tag)
1680fa9e4066Sahrens {
1681fa9e4066Sahrens 	int64_t holds;
1682fa9e4066Sahrens 
1683fa9e4066Sahrens 	mutex_enter(&db->db_mtx);
16849c9dc39aSek 	DBUF_VERIFY(db);
1685fa9e4066Sahrens 
1686fa9e4066Sahrens 	holds = refcount_remove(&db->db_holds, tag);
1687ea8dc4b6Seschrock 	ASSERT(holds >= 0);
1688ea8dc4b6Seschrock 
1689c717a561Smaybee 	/*
1690c717a561Smaybee 	 * We can't freeze indirects if there is a possibility that they
1691c717a561Smaybee 	 * may be modified in the current syncing context.
1692c717a561Smaybee 	 */
1693c717a561Smaybee 	if (db->db_buf && holds == (db->db_level == 0 ? db->db_dirtycnt : 0))
16946b4acc8bSahrens 		arc_buf_freeze(db->db_buf);
16956b4acc8bSahrens 
1696ea8dc4b6Seschrock 	if (holds == db->db_dirtycnt &&
1697c717a561Smaybee 	    db->db_level == 0 && db->db_immediate_evict)
1698ea8dc4b6Seschrock 		dbuf_evict_user(db);
1699fa9e4066Sahrens 
1700fa9e4066Sahrens 	if (holds == 0) {
1701ea8dc4b6Seschrock 		if (db->db_blkid == DB_BONUS_BLKID) {
1702ea8dc4b6Seschrock 			mutex_exit(&db->db_mtx);
1703ea8dc4b6Seschrock 			dnode_rele(db->db_dnode, db);
1704ea8dc4b6Seschrock 		} else if (db->db_buf == NULL) {
1705ea8dc4b6Seschrock 			/*
1706ea8dc4b6Seschrock 			 * This is a special case: we never associated this
1707ea8dc4b6Seschrock 			 * dbuf with any data allocated from the ARC.
1708ea8dc4b6Seschrock 			 */
1709ea8dc4b6Seschrock 			ASSERT3U(db->db_state, ==, DB_UNCACHED);
1710ea8dc4b6Seschrock 			dbuf_evict(db);
17116b4acc8bSahrens 		} else if (arc_released(db->db_buf)) {
1712ea8dc4b6Seschrock 			arc_buf_t *buf = db->db_buf;
1713ea8dc4b6Seschrock 			/*
1714ea8dc4b6Seschrock 			 * This dbuf has anonymous data associated with it.
1715ea8dc4b6Seschrock 			 */
1716ea8dc4b6Seschrock 			dbuf_set_data(db, NULL);
1717ea8dc4b6Seschrock 			VERIFY(arc_buf_remove_ref(buf, db) == 1);
1718ea8dc4b6Seschrock 			dbuf_evict(db);
1719ea8dc4b6Seschrock 		} else {
1720ea8dc4b6Seschrock 			VERIFY(arc_buf_remove_ref(db->db_buf, db) == 0);
1721ea8dc4b6Seschrock 			mutex_exit(&db->db_mtx);
1722ea8dc4b6Seschrock 		}
1723fa9e4066Sahrens 	} else {
1724fa9e4066Sahrens 		mutex_exit(&db->db_mtx);
1725fa9e4066Sahrens 	}
1726fa9e4066Sahrens }
1727fa9e4066Sahrens 
1728fa9e4066Sahrens #pragma weak dmu_buf_refcount = dbuf_refcount
1729fa9e4066Sahrens uint64_t
1730fa9e4066Sahrens dbuf_refcount(dmu_buf_impl_t *db)
1731fa9e4066Sahrens {
1732fa9e4066Sahrens 	return (refcount_count(&db->db_holds));
1733fa9e4066Sahrens }
1734fa9e4066Sahrens 
1735fa9e4066Sahrens void *
1736fa9e4066Sahrens dmu_buf_set_user(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr,
1737fa9e4066Sahrens     dmu_buf_evict_func_t *evict_func)
1738fa9e4066Sahrens {
1739fa9e4066Sahrens 	return (dmu_buf_update_user(db_fake, NULL, user_ptr,
1740fa9e4066Sahrens 	    user_data_ptr_ptr, evict_func));
1741fa9e4066Sahrens }
1742fa9e4066Sahrens 
1743fa9e4066Sahrens void *
1744fa9e4066Sahrens dmu_buf_set_user_ie(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr,
1745fa9e4066Sahrens     dmu_buf_evict_func_t *evict_func)
1746fa9e4066Sahrens {
1747fa9e4066Sahrens 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1748fa9e4066Sahrens 
1749c717a561Smaybee 	db->db_immediate_evict = TRUE;
1750fa9e4066Sahrens 	return (dmu_buf_update_user(db_fake, NULL, user_ptr,
1751fa9e4066Sahrens 	    user_data_ptr_ptr, evict_func));
1752fa9e4066Sahrens }
1753fa9e4066Sahrens 
1754fa9e4066Sahrens void *
1755fa9e4066Sahrens dmu_buf_update_user(dmu_buf_t *db_fake, void *old_user_ptr, void *user_ptr,
1756fa9e4066Sahrens     void *user_data_ptr_ptr, dmu_buf_evict_func_t *evict_func)
1757fa9e4066Sahrens {
1758fa9e4066Sahrens 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1759fa9e4066Sahrens 	ASSERT(db->db_level == 0);
1760fa9e4066Sahrens 
1761fa9e4066Sahrens 	ASSERT((user_ptr == NULL) == (evict_func == NULL));
1762fa9e4066Sahrens 
1763fa9e4066Sahrens 	mutex_enter(&db->db_mtx);
1764fa9e4066Sahrens 
1765c717a561Smaybee 	if (db->db_user_ptr == old_user_ptr) {
1766c717a561Smaybee 		db->db_user_ptr = user_ptr;
1767c717a561Smaybee 		db->db_user_data_ptr_ptr = user_data_ptr_ptr;
1768c717a561Smaybee 		db->db_evict_func = evict_func;
1769fa9e4066Sahrens 
1770fa9e4066Sahrens 		dbuf_update_data(db);
1771fa9e4066Sahrens 	} else {
1772c717a561Smaybee 		old_user_ptr = db->db_user_ptr;
1773fa9e4066Sahrens 	}
1774fa9e4066Sahrens 
1775fa9e4066Sahrens 	mutex_exit(&db->db_mtx);
1776fa9e4066Sahrens 	return (old_user_ptr);
1777fa9e4066Sahrens }
1778fa9e4066Sahrens 
1779fa9e4066Sahrens void *
1780fa9e4066Sahrens dmu_buf_get_user(dmu_buf_t *db_fake)
1781fa9e4066Sahrens {
1782fa9e4066Sahrens 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1783fa9e4066Sahrens 	ASSERT(!refcount_is_zero(&db->db_holds));
1784fa9e4066Sahrens 
1785c717a561Smaybee 	return (db->db_user_ptr);
1786fa9e4066Sahrens }
1787fa9e4066Sahrens 
1788c717a561Smaybee static void
1789c717a561Smaybee dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db)
1790fa9e4066Sahrens {
1791c717a561Smaybee 	/* ASSERT(dmu_tx_is_syncing(tx) */
1792c717a561Smaybee 	ASSERT(MUTEX_HELD(&db->db_mtx));
1793c717a561Smaybee 
1794c717a561Smaybee 	if (db->db_blkptr != NULL)
1795c717a561Smaybee 		return;
1796c717a561Smaybee 
1797c717a561Smaybee 	if (db->db_level == dn->dn_phys->dn_nlevels-1) {
1798c717a561Smaybee 		/*
1799c717a561Smaybee 		 * This buffer was allocated at a time when there was
1800c717a561Smaybee 		 * no available blkptrs from the dnode, or it was
1801c717a561Smaybee 		 * inappropriate to hook it in (i.e., nlevels mis-match).
1802c717a561Smaybee 		 */
1803c717a561Smaybee 		ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr);
1804c717a561Smaybee 		ASSERT(db->db_parent == NULL);
1805c717a561Smaybee 		db->db_parent = dn->dn_dbuf;
1806c717a561Smaybee 		db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
1807c717a561Smaybee 		DBUF_VERIFY(db);
1808c717a561Smaybee 	} else {
1809c717a561Smaybee 		dmu_buf_impl_t *parent = db->db_parent;
1810c717a561Smaybee 		int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
1811c717a561Smaybee 
1812c717a561Smaybee 		ASSERT(dn->dn_phys->dn_nlevels > 1);
1813c717a561Smaybee 		if (parent == NULL) {
1814c717a561Smaybee 			mutex_exit(&db->db_mtx);
1815c717a561Smaybee 			rw_enter(&dn->dn_struct_rwlock, RW_READER);
1816c717a561Smaybee 			(void) dbuf_hold_impl(dn, db->db_level+1,
1817c717a561Smaybee 			    db->db_blkid >> epbs, FALSE, db, &parent);
1818c717a561Smaybee 			rw_exit(&dn->dn_struct_rwlock);
1819c717a561Smaybee 			mutex_enter(&db->db_mtx);
1820c717a561Smaybee 			db->db_parent = parent;
1821c717a561Smaybee 		}
1822c717a561Smaybee 		db->db_blkptr = (blkptr_t *)parent->db.db_data +
1823c717a561Smaybee 		    (db->db_blkid & ((1ULL << epbs) - 1));
1824c717a561Smaybee 		DBUF_VERIFY(db);
1825c717a561Smaybee 	}
1826c717a561Smaybee }
1827c717a561Smaybee 
1828c717a561Smaybee static void
1829c717a561Smaybee dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
1830c717a561Smaybee {
1831c717a561Smaybee 	dmu_buf_impl_t *db = dr->dr_dbuf;
1832c717a561Smaybee 	dnode_t *dn = db->db_dnode;
1833c717a561Smaybee 	zio_t *zio;
1834c717a561Smaybee 
1835c717a561Smaybee 	ASSERT(dmu_tx_is_syncing(tx));
1836c717a561Smaybee 
1837c717a561Smaybee 	dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
1838c717a561Smaybee 
1839c717a561Smaybee 	mutex_enter(&db->db_mtx);
1840c717a561Smaybee 
1841c717a561Smaybee 	ASSERT(db->db_level > 0);
1842c717a561Smaybee 	DBUF_VERIFY(db);
1843c717a561Smaybee 
1844c717a561Smaybee 	if (db->db_buf == NULL) {
1845c717a561Smaybee 		mutex_exit(&db->db_mtx);
1846c717a561Smaybee 		(void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
1847c717a561Smaybee 		mutex_enter(&db->db_mtx);
1848c717a561Smaybee 	}
1849c717a561Smaybee 	ASSERT3U(db->db_state, ==, DB_CACHED);
1850c717a561Smaybee 	ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
1851c717a561Smaybee 	ASSERT(db->db_buf != NULL);
1852c717a561Smaybee 
1853c717a561Smaybee 	dbuf_check_blkptr(dn, db);
1854c717a561Smaybee 
1855c717a561Smaybee 	db->db_data_pending = dr;
1856c717a561Smaybee 
1857c717a561Smaybee 	arc_release(db->db_buf, db);
1858af2c4821Smaybee 	mutex_exit(&db->db_mtx);
1859c717a561Smaybee 
1860c717a561Smaybee 	/*
1861c717a561Smaybee 	 * XXX -- we should design a compression algorithm
1862c717a561Smaybee 	 * that specializes in arrays of bps.
1863c717a561Smaybee 	 */
1864c717a561Smaybee 	dbuf_write(dr, db->db_buf, ZIO_CHECKSUM_FLETCHER_4,
1865c717a561Smaybee 	    zfs_mdcomp_disable ? ZIO_COMPRESS_EMPTY : ZIO_COMPRESS_LZJB, tx);
1866c717a561Smaybee 
1867c717a561Smaybee 	zio = dr->dr_zio;
1868c717a561Smaybee 	mutex_enter(&dr->dt.di.dr_mtx);
1869c717a561Smaybee 	dbuf_sync_list(&dr->dt.di.dr_children, tx);
1870c717a561Smaybee 	ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
1871c717a561Smaybee 	mutex_exit(&dr->dt.di.dr_mtx);
1872c717a561Smaybee 	zio_nowait(zio);
1873c717a561Smaybee }
1874c717a561Smaybee 
1875c717a561Smaybee static void
1876c717a561Smaybee dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
1877c717a561Smaybee {
1878c717a561Smaybee 	arc_buf_t **datap = &dr->dt.dl.dr_data;
1879c717a561Smaybee 	dmu_buf_impl_t *db = dr->dr_dbuf;
1880fa9e4066Sahrens 	dnode_t *dn = db->db_dnode;
1881fa9e4066Sahrens 	objset_impl_t *os = dn->dn_objset;
1882c717a561Smaybee 	uint64_t txg = tx->tx_txg;
1883ea8dc4b6Seschrock 	int checksum, compress;
1884fa9e4066Sahrens 	int blksz;
1885fa9e4066Sahrens 
1886fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1887fa9e4066Sahrens 
1888fa9e4066Sahrens 	dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
1889fa9e4066Sahrens 
1890fa9e4066Sahrens 	mutex_enter(&db->db_mtx);
1891fa9e4066Sahrens 	/*
1892fa9e4066Sahrens 	 * To be synced, we must be dirtied.  But we
1893fa9e4066Sahrens 	 * might have been freed after the dirty.
1894fa9e4066Sahrens 	 */
1895fa9e4066Sahrens 	if (db->db_state == DB_UNCACHED) {
1896fa9e4066Sahrens 		/* This buffer has been freed since it was dirtied */
1897fa9e4066Sahrens 		ASSERT(db->db.db_data == NULL);
1898fa9e4066Sahrens 	} else if (db->db_state == DB_FILL) {
1899fa9e4066Sahrens 		/* This buffer was freed and is now being re-filled */
1900c717a561Smaybee 		ASSERT(db->db.db_data != dr->dt.dl.dr_data);
1901fa9e4066Sahrens 	} else {
1902fa9e4066Sahrens 		ASSERT3U(db->db_state, ==, DB_CACHED);
1903fa9e4066Sahrens 	}
19049c9dc39aSek 	DBUF_VERIFY(db);
1905fa9e4066Sahrens 
1906fa9e4066Sahrens 	/*
1907c717a561Smaybee 	 * If this is a bonus buffer, simply copy the bonus data into the
1908c717a561Smaybee 	 * dnode.  It will be written out when the dnode is synced (and it
1909c717a561Smaybee 	 * will be synced, since it must have been dirty for dbuf_sync to
1910c717a561Smaybee 	 * be called).
1911fa9e4066Sahrens 	 */
1912ea8dc4b6Seschrock 	if (db->db_blkid == DB_BONUS_BLKID) {
1913c717a561Smaybee 		dbuf_dirty_record_t **drp;
1914ea8dc4b6Seschrock 		/*
1915ea8dc4b6Seschrock 		 * Use dn_phys->dn_bonuslen since db.db_size is the length
1916ea8dc4b6Seschrock 		 * of the bonus buffer in the open transaction rather than
1917ea8dc4b6Seschrock 		 * the syncing transaction.
1918ea8dc4b6Seschrock 		 */
1919ea8dc4b6Seschrock 		ASSERT(*datap != NULL);
1920ea8dc4b6Seschrock 		ASSERT3U(db->db_level, ==, 0);
1921ea8dc4b6Seschrock 		ASSERT3U(dn->dn_phys->dn_bonuslen, <=, DN_MAX_BONUSLEN);
1922ea8dc4b6Seschrock 		bcopy(*datap, DN_BONUS(dn->dn_phys), dn->dn_phys->dn_bonuslen);
1923*0e8c6158Smaybee 		if (*datap != db->db.db_data) {
1924ea8dc4b6Seschrock 			zio_buf_free(*datap, DN_MAX_BONUSLEN);
1925*0e8c6158Smaybee 			arc_space_return(DN_MAX_BONUSLEN);
1926*0e8c6158Smaybee 		}
1927ea8dc4b6Seschrock 		db->db_data_pending = NULL;
1928c717a561Smaybee 		drp = &db->db_last_dirty;
1929c717a561Smaybee 		while (*drp != dr)
1930c717a561Smaybee 			drp = &(*drp)->dr_next;
1931c717a561Smaybee 		ASSERT((*drp)->dr_next == NULL);
1932c717a561Smaybee 		*drp = NULL;
1933c717a561Smaybee 		kmem_free(dr, sizeof (dbuf_dirty_record_t));
1934ea8dc4b6Seschrock 		ASSERT(db->db_dirtycnt > 0);
1935ea8dc4b6Seschrock 		db->db_dirtycnt -= 1;
1936ea8dc4b6Seschrock 		mutex_exit(&db->db_mtx);
1937ea8dc4b6Seschrock 		dbuf_rele(db, (void *)(uintptr_t)txg);
1938ea8dc4b6Seschrock 		return;
1939ea8dc4b6Seschrock 	}
1940ea8dc4b6Seschrock 
1941c717a561Smaybee 	/*
1942c717a561Smaybee 	 * If this buffer is in the middle of an immdiate write,
1943c717a561Smaybee 	 * wait for the synchronous IO to complete.
1944c717a561Smaybee 	 */
1945c717a561Smaybee 	while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
1946c717a561Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
1947c717a561Smaybee 		cv_wait(&db->db_changed, &db->db_mtx);
1948c717a561Smaybee 		ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN);
1949c717a561Smaybee 	}
1950c5c6ffa0Smaybee 
1951c717a561Smaybee 	dbuf_check_blkptr(dn, db);
1952c717a561Smaybee 
1953c717a561Smaybee 	/*
1954c717a561Smaybee 	 * If this dbuf has already been written out via an immediate write,
1955c717a561Smaybee 	 * just complete the write by copying over the new block pointer and
1956c717a561Smaybee 	 * updating the accounting via the write-completion functions.
1957c717a561Smaybee 	 */
1958c717a561Smaybee 	if (dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
1959c717a561Smaybee 		zio_t zio_fake;
1960c717a561Smaybee 
1961c717a561Smaybee 		zio_fake.io_private = &db;
1962c717a561Smaybee 		zio_fake.io_error = 0;
1963c717a561Smaybee 		zio_fake.io_bp = db->db_blkptr;
1964c717a561Smaybee 		zio_fake.io_bp_orig = *db->db_blkptr;
1965c717a561Smaybee 		zio_fake.io_txg = txg;
1966c717a561Smaybee 
1967c717a561Smaybee 		*db->db_blkptr = dr->dt.dl.dr_overridden_by;
1968c717a561Smaybee 		dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
1969c717a561Smaybee 		db->db_data_pending = dr;
1970c717a561Smaybee 		dr->dr_zio = &zio_fake;
1971c717a561Smaybee 		mutex_exit(&db->db_mtx);
1972c717a561Smaybee 
1973c717a561Smaybee 		if (BP_IS_OLDER(&zio_fake.io_bp_orig, txg))
1974c717a561Smaybee 			dsl_dataset_block_kill(os->os_dsl_dataset,
1975c717a561Smaybee 			    &zio_fake.io_bp_orig, dn->dn_zio, tx);
1976c717a561Smaybee 
1977c717a561Smaybee 		dbuf_write_ready(&zio_fake, db->db_buf, db);
1978c717a561Smaybee 		dbuf_write_done(&zio_fake, db->db_buf, db);
1979c717a561Smaybee 
1980c717a561Smaybee 		return;
1981c717a561Smaybee 	}
1982c717a561Smaybee 
1983c717a561Smaybee 	blksz = arc_buf_size(*datap);
1984c717a561Smaybee 
1985c717a561Smaybee 	if (dn->dn_object != DMU_META_DNODE_OBJECT) {
1986fa9e4066Sahrens 		/*
1987fa9e4066Sahrens 		 * If this buffer is currently "in use" (i.e., there are
1988fa9e4066Sahrens 		 * active holds and db_data still references it), then make
1989fa9e4066Sahrens 		 * a copy before we start the write so that any modifications
1990fa9e4066Sahrens 		 * from the open txg will not leak into this write.
1991fa9e4066Sahrens 		 *
1992fa9e4066Sahrens 		 * NOTE: this copy does not need to be made for objects only
1993c717a561Smaybee 		 * modified in the syncing context (e.g. DNONE_DNODE blocks).
1994fa9e4066Sahrens 		 */
1995c717a561Smaybee 		if (refcount_count(&db->db_holds) > 1 && *datap == db->db_buf) {
1996c717a561Smaybee 			arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1997c717a561Smaybee 			*datap = arc_buf_alloc(os->os_spa, blksz, db, type);
1998c717a561Smaybee 			bcopy(db->db.db_data, (*datap)->b_data, blksz);
1999fa9e4066Sahrens 		}
2000fa9e4066Sahrens 	} else {
2001c717a561Smaybee 		/*
2002c717a561Smaybee 		 * Private object buffers are released here rather
2003c717a561Smaybee 		 * than in dbuf_dirty() since they are only modified
2004c717a561Smaybee 		 * in the syncing context and we don't want the
2005c717a561Smaybee 		 * overhead of making multiple copies of the data.
2006c717a561Smaybee 		 */
2007c717a561Smaybee 		arc_release(db->db_buf, db);
2008fa9e4066Sahrens 	}
2009fa9e4066Sahrens 
2010c717a561Smaybee 	ASSERT(*datap != NULL);
2011c717a561Smaybee 	db->db_data_pending = dr;
2012fa9e4066Sahrens 
2013c717a561Smaybee 	mutex_exit(&db->db_mtx);
2014fa9e4066Sahrens 
2015c717a561Smaybee 	/*
2016c717a561Smaybee 	 * Allow dnode settings to override objset settings,
2017c717a561Smaybee 	 * except for metadata checksums.
2018c717a561Smaybee 	 */
2019c717a561Smaybee 	if (dmu_ot[dn->dn_type].ot_metadata) {
2020c717a561Smaybee 		checksum = os->os_md_checksum;
2021c717a561Smaybee 		compress = zio_compress_select(dn->dn_compress,
2022c717a561Smaybee 		    os->os_md_compress);
2023fa9e4066Sahrens 	} else {
2024c717a561Smaybee 		checksum = zio_checksum_select(dn->dn_checksum,
2025c717a561Smaybee 		    os->os_checksum);
2026c717a561Smaybee 		compress = zio_compress_select(dn->dn_compress,
2027c717a561Smaybee 		    os->os_compress);
2028fa9e4066Sahrens 	}
2029fa9e4066Sahrens 
2030c717a561Smaybee 	dbuf_write(dr, *datap, checksum, compress, tx);
2031fa9e4066Sahrens 
2032c717a561Smaybee 	ASSERT(!list_link_active(&dr->dr_dirty_node));
2033c717a561Smaybee 	if (dn->dn_object == DMU_META_DNODE_OBJECT)
2034c717a561Smaybee 		list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr);
2035c717a561Smaybee 	else
2036c717a561Smaybee 		zio_nowait(dr->dr_zio);
2037c717a561Smaybee }
203823b11526Smaybee 
2039c717a561Smaybee void
2040c717a561Smaybee dbuf_sync_list(list_t *list, dmu_tx_t *tx)
2041c717a561Smaybee {
2042c717a561Smaybee 	dbuf_dirty_record_t *dr;
2043c717a561Smaybee 
2044c717a561Smaybee 	while (dr = list_head(list)) {
2045c717a561Smaybee 		if (dr->dr_zio != NULL) {
2046c717a561Smaybee 			/*
2047c717a561Smaybee 			 * If we find an already initialized zio then we
2048c717a561Smaybee 			 * are processing the meta-dnode, and we have finished.
2049c717a561Smaybee 			 * The dbufs for all dnodes are put back on the list
2050c717a561Smaybee 			 * during processing, so that we can zio_wait()
2051c717a561Smaybee 			 * these IOs after initiating all child IOs.
2052c717a561Smaybee 			 */
2053c717a561Smaybee 			ASSERT3U(dr->dr_dbuf->db.db_object, ==,
2054c717a561Smaybee 			    DMU_META_DNODE_OBJECT);
2055c717a561Smaybee 			break;
205623b11526Smaybee 		}
2057c717a561Smaybee 		list_remove(list, dr);
2058c717a561Smaybee 		if (dr->dr_dbuf->db_level > 0)
2059c717a561Smaybee 			dbuf_sync_indirect(dr, tx);
2060c717a561Smaybee 		else
2061c717a561Smaybee 			dbuf_sync_leaf(dr, tx);
206223b11526Smaybee 	}
2063c717a561Smaybee }
206423b11526Smaybee 
2065c717a561Smaybee static void
2066c717a561Smaybee dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, int checksum,
2067c717a561Smaybee     int compress, dmu_tx_t *tx)
2068c717a561Smaybee {
2069c717a561Smaybee 	dmu_buf_impl_t *db = dr->dr_dbuf;
2070c717a561Smaybee 	dnode_t *dn = db->db_dnode;
2071c717a561Smaybee 	objset_impl_t *os = dn->dn_objset;
2072c717a561Smaybee 	dmu_buf_impl_t *parent = db->db_parent;
2073c717a561Smaybee 	uint64_t txg = tx->tx_txg;
2074c717a561Smaybee 	zbookmark_t zb;
2075c717a561Smaybee 	zio_t *zio;
20769bc11082Sek 	int zio_flags;
2077fa9e4066Sahrens 
2078c717a561Smaybee 	if (parent != dn->dn_dbuf) {
2079c717a561Smaybee 		ASSERT(parent && parent->db_data_pending);
2080fa9e4066Sahrens 		ASSERT(db->db_level == parent->db_level-1);
2081c717a561Smaybee 		ASSERT(arc_released(parent->db_buf));
2082c717a561Smaybee 		zio = parent->db_data_pending->dr_zio;
2083ea8dc4b6Seschrock 	} else {
2084fa9e4066Sahrens 		ASSERT(db->db_level == dn->dn_phys->dn_nlevels-1);
2085fa9e4066Sahrens 		ASSERT3P(db->db_blkptr, ==,
2086fa9e4066Sahrens 		    &dn->dn_phys->dn_blkptr[db->db_blkid]);
2087c717a561Smaybee 		zio = dn->dn_zio;
2088ea8dc4b6Seschrock 	}
2089fa9e4066Sahrens 
2090c717a561Smaybee 	ASSERT(db->db_level == 0 || data == db->db_buf);
2091c717a561Smaybee 	ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
2092c717a561Smaybee 	ASSERT(zio);
2093fa9e4066Sahrens 
2094ea8dc4b6Seschrock 	zb.zb_objset = os->os_dsl_dataset ? os->os_dsl_dataset->ds_object : 0;
2095ea8dc4b6Seschrock 	zb.zb_object = db->db.db_object;
2096ea8dc4b6Seschrock 	zb.zb_level = db->db_level;
2097ea8dc4b6Seschrock 	zb.zb_blkid = db->db_blkid;
209844cd46caSbillm 
20999bc11082Sek 	zio_flags = ZIO_FLAG_MUSTSUCCEED;
21009bc11082Sek 	if (dmu_ot[dn->dn_type].ot_metadata || zb.zb_level != 0)
21019bc11082Sek 		zio_flags |= ZIO_FLAG_METADATA;
2102c717a561Smaybee 	if (BP_IS_OLDER(db->db_blkptr, txg))
2103c717a561Smaybee 		dsl_dataset_block_kill(
2104c717a561Smaybee 		    os->os_dsl_dataset, db->db_blkptr, zio, tx);
2105fa9e4066Sahrens 
2106c717a561Smaybee 	dr->dr_zio = arc_write(zio, os->os_spa, checksum, compress,
2107d0ad202dSahrens 	    dmu_get_replication_level(os, &zb, dn->dn_type), txg,
2108c717a561Smaybee 	    db->db_blkptr, data, dbuf_write_ready, dbuf_write_done, db,
21099bc11082Sek 	    ZIO_PRIORITY_ASYNC_WRITE, zio_flags, &zb);
2110fa9e4066Sahrens }
2111fa9e4066Sahrens 
2112fa9e4066Sahrens /* ARGSUSED */
2113fa9e4066Sahrens static void
2114c717a561Smaybee dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
2115fa9e4066Sahrens {
2116fa9e4066Sahrens 	dmu_buf_impl_t *db = vdb;
2117fa9e4066Sahrens 	dnode_t *dn = db->db_dnode;
2118fa9e4066Sahrens 	objset_impl_t *os = dn->dn_objset;
2119c717a561Smaybee 	blkptr_t *bp_orig = &zio->io_bp_orig;
2120fa9e4066Sahrens 	uint64_t fill = 0;
2121c717a561Smaybee 	int old_size, new_size, i;
2122fa9e4066Sahrens 
2123c717a561Smaybee 	dprintf_dbuf_bp(db, bp_orig, "bp_orig: %s", "");
2124fa9e4066Sahrens 
2125c717a561Smaybee 	old_size = bp_get_dasize(os->os_spa, bp_orig);
212699653d4eSeschrock 	new_size = bp_get_dasize(os->os_spa, zio->io_bp);
2127fa9e4066Sahrens 
2128fa9e4066Sahrens 	dnode_diduse_space(dn, new_size-old_size);
2129fa9e4066Sahrens 
2130c717a561Smaybee 	if (BP_IS_HOLE(zio->io_bp)) {
2131c717a561Smaybee 		dsl_dataset_t *ds = os->os_dsl_dataset;
2132c717a561Smaybee 		dmu_tx_t *tx = os->os_synctx;
2133fa9e4066Sahrens 
2134c717a561Smaybee 		if (bp_orig->blk_birth == tx->tx_txg)
2135c717a561Smaybee 			dsl_dataset_block_kill(ds, bp_orig, NULL, tx);
2136c717a561Smaybee 		ASSERT3U(db->db_blkptr->blk_fill, ==, 0);
2137c717a561Smaybee 		return;
2138c717a561Smaybee 	}
2139c5c6ffa0Smaybee 
2140c717a561Smaybee 	mutex_enter(&db->db_mtx);
2141fa9e4066Sahrens 
2142fa9e4066Sahrens 	if (db->db_level == 0) {
2143fa9e4066Sahrens 		mutex_enter(&dn->dn_mtx);
2144c717a561Smaybee 		if (db->db_blkid > dn->dn_phys->dn_maxblkid)
2145fa9e4066Sahrens 			dn->dn_phys->dn_maxblkid = db->db_blkid;
2146fa9e4066Sahrens 		mutex_exit(&dn->dn_mtx);
2147fa9e4066Sahrens 
2148fa9e4066Sahrens 		if (dn->dn_type == DMU_OT_DNODE) {
2149fa9e4066Sahrens 			dnode_phys_t *dnp = db->db.db_data;
2150fa9e4066Sahrens 			for (i = db->db.db_size >> DNODE_SHIFT; i > 0;
2151fa9e4066Sahrens 			    i--, dnp++) {
2152fa9e4066Sahrens 				if (dnp->dn_type != DMU_OT_NONE)
2153fa9e4066Sahrens 					fill++;
2154fa9e4066Sahrens 			}
2155fa9e4066Sahrens 		} else {
2156c717a561Smaybee 			fill = 1;
2157fa9e4066Sahrens 		}
2158fa9e4066Sahrens 	} else {
2159fa9e4066Sahrens 		blkptr_t *bp = db->db.db_data;
2160fa9e4066Sahrens 		ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2161fa9e4066Sahrens 		for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, bp++) {
2162fa9e4066Sahrens 			if (BP_IS_HOLE(bp))
2163fa9e4066Sahrens 				continue;
2164fa9e4066Sahrens 			ASSERT3U(BP_GET_LSIZE(bp), ==,
2165fa9e4066Sahrens 			    db->db_level == 1 ? dn->dn_datablksz :
2166fa9e4066Sahrens 			    (1<<dn->dn_phys->dn_indblkshift));
2167fa9e4066Sahrens 			fill += bp->blk_fill;
2168fa9e4066Sahrens 		}
2169fa9e4066Sahrens 	}
2170fa9e4066Sahrens 
2171c717a561Smaybee 	db->db_blkptr->blk_fill = fill;
2172c717a561Smaybee 	BP_SET_TYPE(db->db_blkptr, dn->dn_type);
2173c717a561Smaybee 	BP_SET_LEVEL(db->db_blkptr, db->db_level);
2174c717a561Smaybee 
2175c717a561Smaybee 	mutex_exit(&db->db_mtx);
2176c717a561Smaybee 
2177c717a561Smaybee 	/* We must do this after we've set the bp's type and level */
2178c717a561Smaybee 	if (!DVA_EQUAL(BP_IDENTITY(zio->io_bp), BP_IDENTITY(bp_orig))) {
2179c717a561Smaybee 		dsl_dataset_t *ds = os->os_dsl_dataset;
2180c717a561Smaybee 		dmu_tx_t *tx = os->os_synctx;
2181c717a561Smaybee 
2182c717a561Smaybee 		if (bp_orig->blk_birth == tx->tx_txg)
2183c717a561Smaybee 			dsl_dataset_block_kill(ds, bp_orig, NULL, tx);
2184c717a561Smaybee 		dsl_dataset_block_born(ds, zio->io_bp, tx);
2185fa9e4066Sahrens 	}
2186c717a561Smaybee }
2187fa9e4066Sahrens 
2188c717a561Smaybee /* ARGSUSED */
2189c717a561Smaybee static void
2190c717a561Smaybee dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
2191c717a561Smaybee {
2192c717a561Smaybee 	dmu_buf_impl_t *db = vdb;
2193c717a561Smaybee 	uint64_t txg = zio->io_txg;
2194c717a561Smaybee 	dbuf_dirty_record_t **drp, *dr;
2195c717a561Smaybee 
2196c717a561Smaybee 	ASSERT3U(zio->io_error, ==, 0);
2197c717a561Smaybee 
2198c717a561Smaybee 	mutex_enter(&db->db_mtx);
2199c717a561Smaybee 
2200c717a561Smaybee 	drp = &db->db_last_dirty;
2201c717a561Smaybee 	while (*drp != db->db_data_pending)
2202c717a561Smaybee 		drp = &(*drp)->dr_next;
2203c717a561Smaybee 	ASSERT(!list_link_active(&(*drp)->dr_dirty_node));
2204c717a561Smaybee 	ASSERT((*drp)->dr_txg == txg);
2205c717a561Smaybee 	ASSERT((*drp)->dr_next == NULL);
2206c717a561Smaybee 	dr = *drp;
2207c717a561Smaybee 	*drp = NULL;
2208c717a561Smaybee 
2209c717a561Smaybee 	if (db->db_level == 0) {
2210c717a561Smaybee 		ASSERT(db->db_blkid != DB_BONUS_BLKID);
2211c717a561Smaybee 		ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
2212c717a561Smaybee 
2213c717a561Smaybee 		if (dr->dt.dl.dr_data != db->db_buf)
2214c717a561Smaybee 			VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data, db) == 1);
2215c717a561Smaybee 		else if (!BP_IS_HOLE(db->db_blkptr))
2216c717a561Smaybee 			arc_set_callback(db->db_buf, dbuf_do_evict, db);
2217c717a561Smaybee 		else
2218c717a561Smaybee 			ASSERT(arc_released(db->db_buf));
2219c717a561Smaybee 	} else {
2220c717a561Smaybee 		dnode_t *dn = db->db_dnode;
2221c717a561Smaybee 
2222c717a561Smaybee 		ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
2223c717a561Smaybee 		ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2224c717a561Smaybee 		if (!BP_IS_HOLE(db->db_blkptr)) {
2225c717a561Smaybee 			int epbs =
2226c717a561Smaybee 			    dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2227c717a561Smaybee 			ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
2228c717a561Smaybee 			    db->db.db_size);
2229c717a561Smaybee 			ASSERT3U(dn->dn_phys->dn_maxblkid
2230c717a561Smaybee 			    >> (db->db_level * epbs), >=, db->db_blkid);
2231c717a561Smaybee 			arc_set_callback(db->db_buf, dbuf_do_evict, db);
2232c717a561Smaybee 		}
2233c717a561Smaybee 	}
2234c717a561Smaybee 	kmem_free(dr, sizeof (dbuf_dirty_record_t));
2235fa9e4066Sahrens 
2236fa9e4066Sahrens 	cv_broadcast(&db->db_changed);
2237fa9e4066Sahrens 	ASSERT(db->db_dirtycnt > 0);
2238fa9e4066Sahrens 	db->db_dirtycnt -= 1;
2239c717a561Smaybee 	db->db_data_pending = NULL;
2240fa9e4066Sahrens 	mutex_exit(&db->db_mtx);
2241fa9e4066Sahrens 
2242c717a561Smaybee 	dprintf_dbuf_bp(db, zio->io_bp, "bp: %s", "");
2243fa9e4066Sahrens 
2244ea8dc4b6Seschrock 	dbuf_rele(db, (void *)(uintptr_t)txg);
2245fa9e4066Sahrens }
2246