xref: /titanic_41/usr/src/uts/common/fs/zfs/dnode.c (revision c4cb95767ae6931f185dc5c02edb165c2ff75b10)
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 /*
2206e0070dSMark Shellenbaum  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23238fb8bbSMatthew Ahrens  * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
240521c5ebSJustin Gibbs  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #include <sys/zfs_context.h>
28fa9e4066Sahrens #include <sys/dbuf.h>
29fa9e4066Sahrens #include <sys/dnode.h>
30fa9e4066Sahrens #include <sys/dmu.h>
31fa9e4066Sahrens #include <sys/dmu_impl.h>
32fa9e4066Sahrens #include <sys/dmu_tx.h>
33fa9e4066Sahrens #include <sys/dmu_objset.h>
34fa9e4066Sahrens #include <sys/dsl_dir.h>
35fa9e4066Sahrens #include <sys/dsl_dataset.h>
36fa9e4066Sahrens #include <sys/spa.h>
37fa9e4066Sahrens #include <sys/zio.h>
38fa9e4066Sahrens #include <sys/dmu_zfetch.h>
39887cdfc7SMatthew Ahrens #include <sys/range_tree.h>
40fa9e4066Sahrens 
41fa9e4066Sahrens static kmem_cache_t *dnode_cache;
42744947dcSTom Erickson /*
43744947dcSTom Erickson  * Define DNODE_STATS to turn on statistic gathering. By default, it is only
44744947dcSTom Erickson  * turned on when DEBUG is also defined.
45744947dcSTom Erickson  */
46744947dcSTom Erickson #ifdef	DEBUG
47744947dcSTom Erickson #define	DNODE_STATS
48744947dcSTom Erickson #endif	/* DEBUG */
49744947dcSTom Erickson 
50744947dcSTom Erickson #ifdef	DNODE_STATS
51744947dcSTom Erickson #define	DNODE_STAT_ADD(stat)			((stat)++)
52744947dcSTom Erickson #else
53744947dcSTom Erickson #define	DNODE_STAT_ADD(stat)			/* nothing */
54744947dcSTom Erickson #endif	/* DNODE_STATS */
55fa9e4066Sahrens 
56fa9e4066Sahrens static dnode_phys_t dnode_phys_zero;
57fa9e4066Sahrens 
58fa9e4066Sahrens int zfs_default_bs = SPA_MINBLOCKSHIFT;
59fa9e4066Sahrens int zfs_default_ibs = DN_MAX_INDBLKSHIFT;
60fa9e4066Sahrens 
61744947dcSTom Erickson static kmem_cbrc_t dnode_move(void *, void *, size_t, void *);
62744947dcSTom Erickson 
63d080a709SAlex Reece static int
dbuf_compare(const void * x1,const void * x2)64d080a709SAlex Reece dbuf_compare(const void *x1, const void *x2)
65d080a709SAlex Reece {
66d080a709SAlex Reece 	const dmu_buf_impl_t *d1 = x1;
67d080a709SAlex Reece 	const dmu_buf_impl_t *d2 = x2;
68d080a709SAlex Reece 
69d080a709SAlex Reece 	if (d1->db_level < d2->db_level) {
70d080a709SAlex Reece 		return (-1);
7180ff00d7SAlex Reece 	}
7280ff00d7SAlex Reece 	if (d1->db_level > d2->db_level) {
73d080a709SAlex Reece 		return (1);
74d080a709SAlex Reece 	}
75d080a709SAlex Reece 
76d080a709SAlex Reece 	if (d1->db_blkid < d2->db_blkid) {
77d080a709SAlex Reece 		return (-1);
7880ff00d7SAlex Reece 	}
7980ff00d7SAlex Reece 	if (d1->db_blkid > d2->db_blkid) {
80d080a709SAlex Reece 		return (1);
81d080a709SAlex Reece 	}
82d080a709SAlex Reece 
836d84e0deSAlex Reece 	if (d1->db_state == DB_SEARCH) {
846d84e0deSAlex Reece 		ASSERT3S(d2->db_state, !=, DB_SEARCH);
85d080a709SAlex Reece 		return (-1);
866d84e0deSAlex Reece 	} else if (d2->db_state == DB_SEARCH) {
876d84e0deSAlex Reece 		ASSERT3S(d1->db_state, !=, DB_SEARCH);
8880ff00d7SAlex Reece 		return (1);
8980ff00d7SAlex Reece 	}
9080ff00d7SAlex Reece 
9180ff00d7SAlex Reece 	if ((uintptr_t)d1 < (uintptr_t)d2) {
9280ff00d7SAlex Reece 		return (-1);
9380ff00d7SAlex Reece 	}
9480ff00d7SAlex Reece 	if ((uintptr_t)d1 > (uintptr_t)d2) {
9580ff00d7SAlex Reece 		return (1);
9680ff00d7SAlex Reece 	}
9780ff00d7SAlex Reece 	return (0);
98d080a709SAlex Reece }
99d080a709SAlex Reece 
100fa9e4066Sahrens /* ARGSUSED */
101fa9e4066Sahrens static int
dnode_cons(void * arg,void * unused,int kmflag)102fa9e4066Sahrens dnode_cons(void *arg, void *unused, int kmflag)
103fa9e4066Sahrens {
104fa9e4066Sahrens 	dnode_t *dn = arg;
105744947dcSTom Erickson 	int i;
106fa9e4066Sahrens 
107fa9e4066Sahrens 	rw_init(&dn->dn_struct_rwlock, NULL, RW_DEFAULT, NULL);
108fa9e4066Sahrens 	mutex_init(&dn->dn_mtx, NULL, MUTEX_DEFAULT, NULL);
109fa9e4066Sahrens 	mutex_init(&dn->dn_dbufs_mtx, NULL, MUTEX_DEFAULT, NULL);
110b5e70f97SRicardo M. Correia 	cv_init(&dn->dn_notxholds, NULL, CV_DEFAULT, NULL);
111b5e70f97SRicardo M. Correia 
1123b2aab18SMatthew Ahrens 	/*
1133b2aab18SMatthew Ahrens 	 * Every dbuf has a reference, and dropping a tracked reference is
1143b2aab18SMatthew Ahrens 	 * O(number of references), so don't track dn_holds.
1153b2aab18SMatthew Ahrens 	 */
1163b2aab18SMatthew Ahrens 	refcount_create_untracked(&dn->dn_holds);
117fa9e4066Sahrens 	refcount_create(&dn->dn_tx_holds);
118744947dcSTom Erickson 	list_link_init(&dn->dn_link);
119744947dcSTom Erickson 
120744947dcSTom Erickson 	bzero(&dn->dn_next_nblkptr[0], sizeof (dn->dn_next_nblkptr));
121744947dcSTom Erickson 	bzero(&dn->dn_next_nlevels[0], sizeof (dn->dn_next_nlevels));
122744947dcSTom Erickson 	bzero(&dn->dn_next_indblkshift[0], sizeof (dn->dn_next_indblkshift));
123744947dcSTom Erickson 	bzero(&dn->dn_next_bonustype[0], sizeof (dn->dn_next_bonustype));
124744947dcSTom Erickson 	bzero(&dn->dn_rm_spillblk[0], sizeof (dn->dn_rm_spillblk));
125744947dcSTom Erickson 	bzero(&dn->dn_next_bonuslen[0], sizeof (dn->dn_next_bonuslen));
126744947dcSTom Erickson 	bzero(&dn->dn_next_blksz[0], sizeof (dn->dn_next_blksz));
127fa9e4066Sahrens 
128fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
129744947dcSTom Erickson 		list_link_init(&dn->dn_dirty_link[i]);
130887cdfc7SMatthew Ahrens 		dn->dn_free_ranges[i] = NULL;
131c717a561Smaybee 		list_create(&dn->dn_dirty_records[i],
132c717a561Smaybee 		    sizeof (dbuf_dirty_record_t),
133c717a561Smaybee 		    offsetof(dbuf_dirty_record_t, dr_dirty_node));
134fa9e4066Sahrens 	}
135fa9e4066Sahrens 
136744947dcSTom Erickson 	dn->dn_allocated_txg = 0;
137744947dcSTom Erickson 	dn->dn_free_txg = 0;
138744947dcSTom Erickson 	dn->dn_assigned_txg = 0;
139744947dcSTom Erickson 	dn->dn_dirtyctx = 0;
140744947dcSTom Erickson 	dn->dn_dirtyctx_firstset = NULL;
141744947dcSTom Erickson 	dn->dn_bonus = NULL;
142744947dcSTom Erickson 	dn->dn_have_spill = B_FALSE;
143744947dcSTom Erickson 	dn->dn_zio = NULL;
144744947dcSTom Erickson 	dn->dn_oldused = 0;
145744947dcSTom Erickson 	dn->dn_oldflags = 0;
146744947dcSTom Erickson 	dn->dn_olduid = 0;
147744947dcSTom Erickson 	dn->dn_oldgid = 0;
148744947dcSTom Erickson 	dn->dn_newuid = 0;
149744947dcSTom Erickson 	dn->dn_newgid = 0;
150744947dcSTom Erickson 	dn->dn_id_flags = 0;
151744947dcSTom Erickson 
152744947dcSTom Erickson 	dn->dn_dbufs_count = 0;
153713d6c20SMatthew Ahrens 	dn->dn_unlisted_l0_blkid = 0;
154d080a709SAlex Reece 	avl_create(&dn->dn_dbufs, dbuf_compare, sizeof (dmu_buf_impl_t),
155fa9e4066Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
156fa9e4066Sahrens 
157744947dcSTom Erickson 	dn->dn_moved = 0;
158fa9e4066Sahrens 	return (0);
159fa9e4066Sahrens }
160fa9e4066Sahrens 
161fa9e4066Sahrens /* ARGSUSED */
162fa9e4066Sahrens static void
dnode_dest(void * arg,void * unused)163fa9e4066Sahrens dnode_dest(void *arg, void *unused)
164fa9e4066Sahrens {
165fa9e4066Sahrens 	int i;
166fa9e4066Sahrens 	dnode_t *dn = arg;
167fa9e4066Sahrens 
168fa9e4066Sahrens 	rw_destroy(&dn->dn_struct_rwlock);
169fa9e4066Sahrens 	mutex_destroy(&dn->dn_mtx);
170fa9e4066Sahrens 	mutex_destroy(&dn->dn_dbufs_mtx);
171b5e70f97SRicardo M. Correia 	cv_destroy(&dn->dn_notxholds);
172fa9e4066Sahrens 	refcount_destroy(&dn->dn_holds);
173fa9e4066Sahrens 	refcount_destroy(&dn->dn_tx_holds);
174744947dcSTom Erickson 	ASSERT(!list_link_active(&dn->dn_link));
175fa9e4066Sahrens 
176fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
177744947dcSTom Erickson 		ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
178887cdfc7SMatthew Ahrens 		ASSERT3P(dn->dn_free_ranges[i], ==, NULL);
179c717a561Smaybee 		list_destroy(&dn->dn_dirty_records[i]);
180fb09f5aaSMadhav Suresh 		ASSERT0(dn->dn_next_nblkptr[i]);
181fb09f5aaSMadhav Suresh 		ASSERT0(dn->dn_next_nlevels[i]);
182fb09f5aaSMadhav Suresh 		ASSERT0(dn->dn_next_indblkshift[i]);
183fb09f5aaSMadhav Suresh 		ASSERT0(dn->dn_next_bonustype[i]);
184fb09f5aaSMadhav Suresh 		ASSERT0(dn->dn_rm_spillblk[i]);
185fb09f5aaSMadhav Suresh 		ASSERT0(dn->dn_next_bonuslen[i]);
186fb09f5aaSMadhav Suresh 		ASSERT0(dn->dn_next_blksz[i]);
187fa9e4066Sahrens 	}
188fa9e4066Sahrens 
189fb09f5aaSMadhav Suresh 	ASSERT0(dn->dn_allocated_txg);
190fb09f5aaSMadhav Suresh 	ASSERT0(dn->dn_free_txg);
191fb09f5aaSMadhav Suresh 	ASSERT0(dn->dn_assigned_txg);
192fb09f5aaSMadhav Suresh 	ASSERT0(dn->dn_dirtyctx);
193744947dcSTom Erickson 	ASSERT3P(dn->dn_dirtyctx_firstset, ==, NULL);
194744947dcSTom Erickson 	ASSERT3P(dn->dn_bonus, ==, NULL);
195744947dcSTom Erickson 	ASSERT(!dn->dn_have_spill);
196744947dcSTom Erickson 	ASSERT3P(dn->dn_zio, ==, NULL);
197fb09f5aaSMadhav Suresh 	ASSERT0(dn->dn_oldused);
198fb09f5aaSMadhav Suresh 	ASSERT0(dn->dn_oldflags);
199fb09f5aaSMadhav Suresh 	ASSERT0(dn->dn_olduid);
200fb09f5aaSMadhav Suresh 	ASSERT0(dn->dn_oldgid);
201fb09f5aaSMadhav Suresh 	ASSERT0(dn->dn_newuid);
202fb09f5aaSMadhav Suresh 	ASSERT0(dn->dn_newgid);
203fb09f5aaSMadhav Suresh 	ASSERT0(dn->dn_id_flags);
204744947dcSTom Erickson 
205fb09f5aaSMadhav Suresh 	ASSERT0(dn->dn_dbufs_count);
206713d6c20SMatthew Ahrens 	ASSERT0(dn->dn_unlisted_l0_blkid);
207d080a709SAlex Reece 	avl_destroy(&dn->dn_dbufs);
208fa9e4066Sahrens }
209fa9e4066Sahrens 
210fa9e4066Sahrens void
dnode_init(void)211fa9e4066Sahrens dnode_init(void)
212fa9e4066Sahrens {
213744947dcSTom Erickson 	ASSERT(dnode_cache == NULL);
214fa9e4066Sahrens 	dnode_cache = kmem_cache_create("dnode_t",
215fa9e4066Sahrens 	    sizeof (dnode_t),
216fa9e4066Sahrens 	    0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0);
217744947dcSTom Erickson 	kmem_cache_set_move(dnode_cache, dnode_move);
218fa9e4066Sahrens }
219fa9e4066Sahrens 
220fa9e4066Sahrens void
dnode_fini(void)221fa9e4066Sahrens dnode_fini(void)
222fa9e4066Sahrens {
223fa9e4066Sahrens 	kmem_cache_destroy(dnode_cache);
224744947dcSTom Erickson 	dnode_cache = NULL;
225fa9e4066Sahrens }
226fa9e4066Sahrens 
227fa9e4066Sahrens 
2289c9dc39aSek110237 #ifdef ZFS_DEBUG
229fa9e4066Sahrens void
dnode_verify(dnode_t * dn)230fa9e4066Sahrens dnode_verify(dnode_t *dn)
231fa9e4066Sahrens {
232fa9e4066Sahrens 	int drop_struct_lock = FALSE;
233fa9e4066Sahrens 
234fa9e4066Sahrens 	ASSERT(dn->dn_phys);
235fa9e4066Sahrens 	ASSERT(dn->dn_objset);
236744947dcSTom Erickson 	ASSERT(dn->dn_handle->dnh_dnode == dn);
237fa9e4066Sahrens 
238ad135b5dSChristopher Siden 	ASSERT(DMU_OT_IS_VALID(dn->dn_phys->dn_type));
239fa9e4066Sahrens 
240fa9e4066Sahrens 	if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY))
241fa9e4066Sahrens 		return;
242fa9e4066Sahrens 
243fa9e4066Sahrens 	if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
244fa9e4066Sahrens 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
245fa9e4066Sahrens 		drop_struct_lock = TRUE;
246fa9e4066Sahrens 	}
247fa9e4066Sahrens 	if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) {
248fa9e4066Sahrens 		int i;
249fa9e4066Sahrens 		ASSERT3U(dn->dn_indblkshift, >=, 0);
250fa9e4066Sahrens 		ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT);
251fa9e4066Sahrens 		if (dn->dn_datablkshift) {
252fa9e4066Sahrens 			ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT);
253fa9e4066Sahrens 			ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT);
254fa9e4066Sahrens 			ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz);
255fa9e4066Sahrens 		}
256fa9e4066Sahrens 		ASSERT3U(dn->dn_nlevels, <=, 30);
257ad135b5dSChristopher Siden 		ASSERT(DMU_OT_IS_VALID(dn->dn_type));
258fa9e4066Sahrens 		ASSERT3U(dn->dn_nblkptr, >=, 1);
259fa9e4066Sahrens 		ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
260fa9e4066Sahrens 		ASSERT3U(dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
261fa9e4066Sahrens 		ASSERT3U(dn->dn_datablksz, ==,
262fa9e4066Sahrens 		    dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
263fa9e4066Sahrens 		ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0);
264fa9e4066Sahrens 		ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) +
265fa9e4066Sahrens 		    dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
266fa9e4066Sahrens 		for (i = 0; i < TXG_SIZE; i++) {
267fa9e4066Sahrens 			ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels);
268fa9e4066Sahrens 		}
269fa9e4066Sahrens 	}
270fa9e4066Sahrens 	if (dn->dn_phys->dn_type != DMU_OT_NONE)
271fa9e4066Sahrens 		ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels);
27214843421SMatthew Ahrens 	ASSERT(DMU_OBJECT_IS_SPECIAL(dn->dn_object) || dn->dn_dbuf != NULL);
273fa9e4066Sahrens 	if (dn->dn_dbuf != NULL) {
274fa9e4066Sahrens 		ASSERT3P(dn->dn_phys, ==,
275fa9e4066Sahrens 		    (dnode_phys_t *)dn->dn_dbuf->db.db_data +
276fa9e4066Sahrens 		    (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT)));
277fa9e4066Sahrens 	}
278fa9e4066Sahrens 	if (drop_struct_lock)
279fa9e4066Sahrens 		rw_exit(&dn->dn_struct_rwlock);
280fa9e4066Sahrens }
2819c9dc39aSek110237 #endif
282fa9e4066Sahrens 
283fa9e4066Sahrens void
dnode_byteswap(dnode_phys_t * dnp)284fa9e4066Sahrens dnode_byteswap(dnode_phys_t *dnp)
285fa9e4066Sahrens {
286fa9e4066Sahrens 	uint64_t *buf64 = (void*)&dnp->dn_blkptr;
287fa9e4066Sahrens 	int i;
288fa9e4066Sahrens 
289fa9e4066Sahrens 	if (dnp->dn_type == DMU_OT_NONE) {
290fa9e4066Sahrens 		bzero(dnp, sizeof (dnode_phys_t));
291fa9e4066Sahrens 		return;
292fa9e4066Sahrens 	}
293fa9e4066Sahrens 
294fa9e4066Sahrens 	dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec);
295fa9e4066Sahrens 	dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen);
296fa9e4066Sahrens 	dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid);
29799653d4eSeschrock 	dnp->dn_used = BSWAP_64(dnp->dn_used);
298fa9e4066Sahrens 
299fa9e4066Sahrens 	/*
300fa9e4066Sahrens 	 * dn_nblkptr is only one byte, so it's OK to read it in either
301fa9e4066Sahrens 	 * byte order.  We can't read dn_bouslen.
302fa9e4066Sahrens 	 */
303fa9e4066Sahrens 	ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT);
304fa9e4066Sahrens 	ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR);
305fa9e4066Sahrens 	for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++)
306fa9e4066Sahrens 		buf64[i] = BSWAP_64(buf64[i]);
307fa9e4066Sahrens 
308fa9e4066Sahrens 	/*
309fa9e4066Sahrens 	 * OK to check dn_bonuslen for zero, because it won't matter if
310fa9e4066Sahrens 	 * we have the wrong byte order.  This is necessary because the
311fa9e4066Sahrens 	 * dnode dnode is smaller than a regular dnode.
312fa9e4066Sahrens 	 */
313fa9e4066Sahrens 	if (dnp->dn_bonuslen != 0) {
314fa9e4066Sahrens 		/*
315fa9e4066Sahrens 		 * Note that the bonus length calculated here may be
316fa9e4066Sahrens 		 * longer than the actual bonus buffer.  This is because
317fa9e4066Sahrens 		 * we always put the bonus buffer after the last block
318fa9e4066Sahrens 		 * pointer (instead of packing it against the end of the
319fa9e4066Sahrens 		 * dnode buffer).
320fa9e4066Sahrens 		 */
321fa9e4066Sahrens 		int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t);
322fa9e4066Sahrens 		size_t len = DN_MAX_BONUSLEN - off;
323ad135b5dSChristopher Siden 		ASSERT(DMU_OT_IS_VALID(dnp->dn_bonustype));
324ad135b5dSChristopher Siden 		dmu_object_byteswap_t byteswap =
325ad135b5dSChristopher Siden 		    DMU_OT_BYTESWAP(dnp->dn_bonustype);
326ad135b5dSChristopher Siden 		dmu_ot_byteswap[byteswap].ob_func(dnp->dn_bonus + off, len);
327fa9e4066Sahrens 	}
3280a586ceaSMark Shellenbaum 
3290a586ceaSMark Shellenbaum 	/* Swap SPILL block if we have one */
3300a586ceaSMark Shellenbaum 	if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)
3310a586ceaSMark Shellenbaum 		byteswap_uint64_array(&dnp->dn_spill, sizeof (blkptr_t));
3320a586ceaSMark Shellenbaum 
333fa9e4066Sahrens }
334fa9e4066Sahrens 
335fa9e4066Sahrens void
dnode_buf_byteswap(void * vbuf,size_t size)336fa9e4066Sahrens dnode_buf_byteswap(void *vbuf, size_t size)
337fa9e4066Sahrens {
338fa9e4066Sahrens 	dnode_phys_t *buf = vbuf;
339fa9e4066Sahrens 	int i;
340fa9e4066Sahrens 
341fa9e4066Sahrens 	ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT));
342fa9e4066Sahrens 	ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0);
343fa9e4066Sahrens 
344fa9e4066Sahrens 	size >>= DNODE_SHIFT;
345fa9e4066Sahrens 	for (i = 0; i < size; i++) {
346fa9e4066Sahrens 		dnode_byteswap(buf);
347fa9e4066Sahrens 		buf++;
348fa9e4066Sahrens 	}
349fa9e4066Sahrens }
350fa9e4066Sahrens 
3511934e92fSmaybee void
dnode_setbonuslen(dnode_t * dn,int newsize,dmu_tx_t * tx)3521934e92fSmaybee dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx)
3531934e92fSmaybee {
3541934e92fSmaybee 	ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
3551934e92fSmaybee 
3561934e92fSmaybee 	dnode_setdirty(dn, tx);
3571934e92fSmaybee 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
3581934e92fSmaybee 	ASSERT3U(newsize, <=, DN_MAX_BONUSLEN -
3591934e92fSmaybee 	    (dn->dn_nblkptr-1) * sizeof (blkptr_t));
3601934e92fSmaybee 	dn->dn_bonuslen = newsize;
3611934e92fSmaybee 	if (newsize == 0)
3621934e92fSmaybee 		dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = DN_ZERO_BONUSLEN;
3631934e92fSmaybee 	else
3641934e92fSmaybee 		dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
3651934e92fSmaybee 	rw_exit(&dn->dn_struct_rwlock);
3661934e92fSmaybee }
3671934e92fSmaybee 
3680a586ceaSMark Shellenbaum void
dnode_setbonus_type(dnode_t * dn,dmu_object_type_t newtype,dmu_tx_t * tx)3690a586ceaSMark Shellenbaum dnode_setbonus_type(dnode_t *dn, dmu_object_type_t newtype, dmu_tx_t *tx)
3700a586ceaSMark Shellenbaum {
3710a586ceaSMark Shellenbaum 	ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
3720a586ceaSMark Shellenbaum 	dnode_setdirty(dn, tx);
3730a586ceaSMark Shellenbaum 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
3740a586ceaSMark Shellenbaum 	dn->dn_bonustype = newtype;
3750a586ceaSMark Shellenbaum 	dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
3760a586ceaSMark Shellenbaum 	rw_exit(&dn->dn_struct_rwlock);
3770a586ceaSMark Shellenbaum }
3780a586ceaSMark Shellenbaum 
3790a586ceaSMark Shellenbaum void
dnode_rm_spill(dnode_t * dn,dmu_tx_t * tx)3800a586ceaSMark Shellenbaum dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx)
3810a586ceaSMark Shellenbaum {
3820a586ceaSMark Shellenbaum 	ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
38306e0070dSMark Shellenbaum 	ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
3840a586ceaSMark Shellenbaum 	dnode_setdirty(dn, tx);
3850a586ceaSMark Shellenbaum 	dn->dn_rm_spillblk[tx->tx_txg&TXG_MASK] = DN_KILL_SPILLBLK;
3860a586ceaSMark Shellenbaum 	dn->dn_have_spill = B_FALSE;
3870a586ceaSMark Shellenbaum }
3880a586ceaSMark Shellenbaum 
389fa9e4066Sahrens static void
dnode_setdblksz(dnode_t * dn,int size)390fa9e4066Sahrens dnode_setdblksz(dnode_t *dn, int size)
391fa9e4066Sahrens {
392fb09f5aaSMadhav Suresh 	ASSERT0(P2PHASE(size, SPA_MINBLOCKSIZE));
393fa9e4066Sahrens 	ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
394fa9e4066Sahrens 	ASSERT3U(size, >=, SPA_MINBLOCKSIZE);
395fa9e4066Sahrens 	ASSERT3U(size >> SPA_MINBLOCKSHIFT, <,
396fa9e4066Sahrens 	    1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8));
397fa9e4066Sahrens 	dn->dn_datablksz = size;
398fa9e4066Sahrens 	dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT;
399887cdfc7SMatthew Ahrens 	dn->dn_datablkshift = ISP2(size) ? highbit64(size - 1) : 0;
400fa9e4066Sahrens }
401fa9e4066Sahrens 
402fa9e4066Sahrens static dnode_t *
dnode_create(objset_t * os,dnode_phys_t * dnp,dmu_buf_impl_t * db,uint64_t object,dnode_handle_t * dnh)403503ad85cSMatthew Ahrens dnode_create(objset_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db,
404744947dcSTom Erickson     uint64_t object, dnode_handle_t *dnh)
405fa9e4066Sahrens {
4060521c5ebSJustin Gibbs 	dnode_t *dn;
407fa9e4066Sahrens 
4080521c5ebSJustin Gibbs 	dn = kmem_cache_alloc(dnode_cache, KM_SLEEP);
409744947dcSTom Erickson 	ASSERT(!POINTER_IS_VALID(dn->dn_objset));
410744947dcSTom Erickson 	dn->dn_moved = 0;
411744947dcSTom Erickson 
412744947dcSTom Erickson 	/*
413744947dcSTom Erickson 	 * Defer setting dn_objset until the dnode is ready to be a candidate
414744947dcSTom Erickson 	 * for the dnode_move() callback.
415744947dcSTom Erickson 	 */
416fa9e4066Sahrens 	dn->dn_object = object;
417fa9e4066Sahrens 	dn->dn_dbuf = db;
418744947dcSTom Erickson 	dn->dn_handle = dnh;
419fa9e4066Sahrens 	dn->dn_phys = dnp;
420fa9e4066Sahrens 
421744947dcSTom Erickson 	if (dnp->dn_datablkszsec) {
422fa9e4066Sahrens 		dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
423744947dcSTom Erickson 	} else {
424744947dcSTom Erickson 		dn->dn_datablksz = 0;
425744947dcSTom Erickson 		dn->dn_datablkszsec = 0;
426744947dcSTom Erickson 		dn->dn_datablkshift = 0;
427744947dcSTom Erickson 	}
428fa9e4066Sahrens 	dn->dn_indblkshift = dnp->dn_indblkshift;
429fa9e4066Sahrens 	dn->dn_nlevels = dnp->dn_nlevels;
430fa9e4066Sahrens 	dn->dn_type = dnp->dn_type;
431fa9e4066Sahrens 	dn->dn_nblkptr = dnp->dn_nblkptr;
432fa9e4066Sahrens 	dn->dn_checksum = dnp->dn_checksum;
433fa9e4066Sahrens 	dn->dn_compress = dnp->dn_compress;
434fa9e4066Sahrens 	dn->dn_bonustype = dnp->dn_bonustype;
435fa9e4066Sahrens 	dn->dn_bonuslen = dnp->dn_bonuslen;
436fa9e4066Sahrens 	dn->dn_maxblkid = dnp->dn_maxblkid;
4370a586ceaSMark Shellenbaum 	dn->dn_have_spill = ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0);
43806e0070dSMark Shellenbaum 	dn->dn_id_flags = 0;
439fa9e4066Sahrens 
440fa9e4066Sahrens 	dmu_zfetch_init(&dn->dn_zfetch, dn);
441fa9e4066Sahrens 
442ad135b5dSChristopher Siden 	ASSERT(DMU_OT_IS_VALID(dn->dn_phys->dn_type));
443744947dcSTom Erickson 
444fa9e4066Sahrens 	mutex_enter(&os->os_lock);
4450521c5ebSJustin Gibbs 	if (dnh->dnh_dnode != NULL) {
4460521c5ebSJustin Gibbs 		/* Lost the allocation race. */
4470521c5ebSJustin Gibbs 		mutex_exit(&os->os_lock);
4480521c5ebSJustin Gibbs 		kmem_cache_free(dnode_cache, dn);
4490521c5ebSJustin Gibbs 		return (dnh->dnh_dnode);
4500521c5ebSJustin Gibbs 	}
4510521c5ebSJustin Gibbs 
4520521c5ebSJustin Gibbs 	/*
4530521c5ebSJustin Gibbs 	 * Exclude special dnodes from os_dnodes so an empty os_dnodes
4540521c5ebSJustin Gibbs 	 * signifies that the special dnodes have no references from
4550521c5ebSJustin Gibbs 	 * their children (the entries in os_dnodes).  This allows
4560521c5ebSJustin Gibbs 	 * dnode_destroy() to easily determine if the last child has
4570521c5ebSJustin Gibbs 	 * been removed and then complete eviction of the objset.
4580521c5ebSJustin Gibbs 	 */
4590521c5ebSJustin Gibbs 	if (!DMU_OBJECT_IS_SPECIAL(object))
460fa9e4066Sahrens 		list_insert_head(&os->os_dnodes, dn);
461744947dcSTom Erickson 	membar_producer();
4620521c5ebSJustin Gibbs 
463744947dcSTom Erickson 	/*
4640521c5ebSJustin Gibbs 	 * Everything else must be valid before assigning dn_objset
4650521c5ebSJustin Gibbs 	 * makes the dnode eligible for dnode_move().
466744947dcSTom Erickson 	 */
467744947dcSTom Erickson 	dn->dn_objset = os;
4680521c5ebSJustin Gibbs 
4690521c5ebSJustin Gibbs 	dnh->dnh_dnode = dn;
470fa9e4066Sahrens 	mutex_exit(&os->os_lock);
471fa9e4066Sahrens 
4725a98e54bSBrendan Gregg - Sun Microsystems 	arc_space_consume(sizeof (dnode_t), ARC_SPACE_OTHER);
473fa9e4066Sahrens 	return (dn);
474fa9e4066Sahrens }
475fa9e4066Sahrens 
476744947dcSTom Erickson /*
477744947dcSTom Erickson  * Caller must be holding the dnode handle, which is released upon return.
478744947dcSTom Erickson  */
479fa9e4066Sahrens static void
dnode_destroy(dnode_t * dn)480fa9e4066Sahrens dnode_destroy(dnode_t *dn)
481fa9e4066Sahrens {
482503ad85cSMatthew Ahrens 	objset_t *os = dn->dn_objset;
4830521c5ebSJustin Gibbs 	boolean_t complete_os_eviction = B_FALSE;
484fa9e4066Sahrens 
4850a586ceaSMark Shellenbaum 	ASSERT((dn->dn_id_flags & DN_ID_NEW_EXIST) == 0);
486a2eea2e1Sahrens 
487fa9e4066Sahrens 	mutex_enter(&os->os_lock);
488744947dcSTom Erickson 	POINTER_INVALIDATE(&dn->dn_objset);
4890521c5ebSJustin Gibbs 	if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
490fa9e4066Sahrens 		list_remove(&os->os_dnodes, dn);
4910521c5ebSJustin Gibbs 		complete_os_eviction =
4920521c5ebSJustin Gibbs 		    list_is_empty(&os->os_dnodes) &&
4930521c5ebSJustin Gibbs 		    list_link_active(&os->os_evicting_node);
4940521c5ebSJustin Gibbs 	}
495fa9e4066Sahrens 	mutex_exit(&os->os_lock);
496fa9e4066Sahrens 
497744947dcSTom Erickson 	/* the dnode can no longer move, so we can release the handle */
498744947dcSTom Erickson 	zrl_remove(&dn->dn_handle->dnh_zrlock);
499744947dcSTom Erickson 
500744947dcSTom Erickson 	dn->dn_allocated_txg = 0;
501744947dcSTom Erickson 	dn->dn_free_txg = 0;
502744947dcSTom Erickson 	dn->dn_assigned_txg = 0;
503744947dcSTom Erickson 
504744947dcSTom Erickson 	dn->dn_dirtyctx = 0;
505744947dcSTom Erickson 	if (dn->dn_dirtyctx_firstset != NULL) {
506fa9e4066Sahrens 		kmem_free(dn->dn_dirtyctx_firstset, 1);
507fa9e4066Sahrens 		dn->dn_dirtyctx_firstset = NULL;
508fa9e4066Sahrens 	}
509744947dcSTom Erickson 	if (dn->dn_bonus != NULL) {
510ea8dc4b6Seschrock 		mutex_enter(&dn->dn_bonus->db_mtx);
511ea8dc4b6Seschrock 		dbuf_evict(dn->dn_bonus);
512ea8dc4b6Seschrock 		dn->dn_bonus = NULL;
513ea8dc4b6Seschrock 	}
514744947dcSTom Erickson 	dn->dn_zio = NULL;
515744947dcSTom Erickson 
516744947dcSTom Erickson 	dn->dn_have_spill = B_FALSE;
517744947dcSTom Erickson 	dn->dn_oldused = 0;
518744947dcSTom Erickson 	dn->dn_oldflags = 0;
519744947dcSTom Erickson 	dn->dn_olduid = 0;
520744947dcSTom Erickson 	dn->dn_oldgid = 0;
521744947dcSTom Erickson 	dn->dn_newuid = 0;
522744947dcSTom Erickson 	dn->dn_newgid = 0;
523744947dcSTom Erickson 	dn->dn_id_flags = 0;
524713d6c20SMatthew Ahrens 	dn->dn_unlisted_l0_blkid = 0;
525744947dcSTom Erickson 
526744947dcSTom Erickson 	dmu_zfetch_rele(&dn->dn_zfetch);
527fa9e4066Sahrens 	kmem_cache_free(dnode_cache, dn);
5285a98e54bSBrendan Gregg - Sun Microsystems 	arc_space_return(sizeof (dnode_t), ARC_SPACE_OTHER);
5290521c5ebSJustin Gibbs 
5300521c5ebSJustin Gibbs 	if (complete_os_eviction)
5310521c5ebSJustin Gibbs 		dmu_objset_evict_done(os);
532fa9e4066Sahrens }
533fa9e4066Sahrens 
534fa9e4066Sahrens void
dnode_allocate(dnode_t * dn,dmu_object_type_t ot,int blocksize,int ibs,dmu_object_type_t bonustype,int bonuslen,dmu_tx_t * tx)535fa9e4066Sahrens dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
536fa9e4066Sahrens     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
537fa9e4066Sahrens {
538fa9e4066Sahrens 	int i;
539fa9e4066Sahrens 
540d1a98260SMatthew Ahrens 	ASSERT3U(blocksize, <=,
541d1a98260SMatthew Ahrens 	    spa_maxblocksize(dmu_objset_spa(dn->dn_objset)));
542fa9e4066Sahrens 	if (blocksize == 0)
543fa9e4066Sahrens 		blocksize = 1 << zfs_default_bs;
5443b83abddSahrens 	else
5453b83abddSahrens 		blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE);
546fa9e4066Sahrens 
547fa9e4066Sahrens 	if (ibs == 0)
548fa9e4066Sahrens 		ibs = zfs_default_ibs;
549fa9e4066Sahrens 
550fa9e4066Sahrens 	ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT);
551fa9e4066Sahrens 
552fa9e4066Sahrens 	dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset,
553fa9e4066Sahrens 	    dn->dn_object, tx->tx_txg, blocksize, ibs);
554fa9e4066Sahrens 
555fa9e4066Sahrens 	ASSERT(dn->dn_type == DMU_OT_NONE);
556fa9e4066Sahrens 	ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0);
557fa9e4066Sahrens 	ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE);
558fa9e4066Sahrens 	ASSERT(ot != DMU_OT_NONE);
559ad135b5dSChristopher Siden 	ASSERT(DMU_OT_IS_VALID(ot));
560fa9e4066Sahrens 	ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
5610a586ceaSMark Shellenbaum 	    (bonustype == DMU_OT_SA && bonuslen == 0) ||
562fa9e4066Sahrens 	    (bonustype != DMU_OT_NONE && bonuslen != 0));
563ad135b5dSChristopher Siden 	ASSERT(DMU_OT_IS_VALID(bonustype));
564fa9e4066Sahrens 	ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
565fa9e4066Sahrens 	ASSERT(dn->dn_type == DMU_OT_NONE);
566fb09f5aaSMadhav Suresh 	ASSERT0(dn->dn_maxblkid);
567fb09f5aaSMadhav Suresh 	ASSERT0(dn->dn_allocated_txg);
568fb09f5aaSMadhav Suresh 	ASSERT0(dn->dn_assigned_txg);
569fa9e4066Sahrens 	ASSERT(refcount_is_zero(&dn->dn_tx_holds));
570fa9e4066Sahrens 	ASSERT3U(refcount_count(&dn->dn_holds), <=, 1);
571d080a709SAlex Reece 	ASSERT(avl_is_empty(&dn->dn_dbufs));
572fa9e4066Sahrens 
573fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
574fb09f5aaSMadhav Suresh 		ASSERT0(dn->dn_next_nblkptr[i]);
575fb09f5aaSMadhav Suresh 		ASSERT0(dn->dn_next_nlevels[i]);
576fb09f5aaSMadhav Suresh 		ASSERT0(dn->dn_next_indblkshift[i]);
577fb09f5aaSMadhav Suresh 		ASSERT0(dn->dn_next_bonuslen[i]);
578fb09f5aaSMadhav Suresh 		ASSERT0(dn->dn_next_bonustype[i]);
579fb09f5aaSMadhav Suresh 		ASSERT0(dn->dn_rm_spillblk[i]);
580fb09f5aaSMadhav Suresh 		ASSERT0(dn->dn_next_blksz[i]);
581c543ec06Sahrens 		ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
582c717a561Smaybee 		ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL);
583887cdfc7SMatthew Ahrens 		ASSERT3P(dn->dn_free_ranges[i], ==, NULL);
584fa9e4066Sahrens 	}
585fa9e4066Sahrens 
586fa9e4066Sahrens 	dn->dn_type = ot;
587fa9e4066Sahrens 	dnode_setdblksz(dn, blocksize);
588fa9e4066Sahrens 	dn->dn_indblkshift = ibs;
589fa9e4066Sahrens 	dn->dn_nlevels = 1;
5900a586ceaSMark Shellenbaum 	if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
5910a586ceaSMark Shellenbaum 		dn->dn_nblkptr = 1;
5920a586ceaSMark Shellenbaum 	else
5930a586ceaSMark Shellenbaum 		dn->dn_nblkptr = 1 +
5940a586ceaSMark Shellenbaum 		    ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
595fa9e4066Sahrens 	dn->dn_bonustype = bonustype;
596fa9e4066Sahrens 	dn->dn_bonuslen = bonuslen;
597fa9e4066Sahrens 	dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
598fa9e4066Sahrens 	dn->dn_compress = ZIO_COMPRESS_INHERIT;
599fa9e4066Sahrens 	dn->dn_dirtyctx = 0;
600fa9e4066Sahrens 
601fa9e4066Sahrens 	dn->dn_free_txg = 0;
602fa9e4066Sahrens 	if (dn->dn_dirtyctx_firstset) {
603fa9e4066Sahrens 		kmem_free(dn->dn_dirtyctx_firstset, 1);
604fa9e4066Sahrens 		dn->dn_dirtyctx_firstset = NULL;
605fa9e4066Sahrens 	}
606fa9e4066Sahrens 
607fa9e4066Sahrens 	dn->dn_allocated_txg = tx->tx_txg;
6080a586ceaSMark Shellenbaum 	dn->dn_id_flags = 0;
609f676ed34Sahrens 
610fa9e4066Sahrens 	dnode_setdirty(dn, tx);
611f676ed34Sahrens 	dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs;
6121934e92fSmaybee 	dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
6130a586ceaSMark Shellenbaum 	dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
614f676ed34Sahrens 	dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz;
615fa9e4066Sahrens }
616fa9e4066Sahrens 
617fa9e4066Sahrens void
dnode_reallocate(dnode_t * dn,dmu_object_type_t ot,int blocksize,dmu_object_type_t bonustype,int bonuslen,dmu_tx_t * tx)618fa9e4066Sahrens dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
619fa9e4066Sahrens     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
620fa9e4066Sahrens {
6212bf405a2SMark Maybee 	int nblkptr;
622c543ec06Sahrens 
623fa9e4066Sahrens 	ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE);
624d1a98260SMatthew Ahrens 	ASSERT3U(blocksize, <=,
625d1a98260SMatthew Ahrens 	    spa_maxblocksize(dmu_objset_spa(dn->dn_objset)));
626fb09f5aaSMadhav Suresh 	ASSERT0(blocksize % SPA_MINBLOCKSIZE);
627ea8dc4b6Seschrock 	ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
628fa9e4066Sahrens 	ASSERT(tx->tx_txg != 0);
629fa9e4066Sahrens 	ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
63006e0070dSMark Shellenbaum 	    (bonustype != DMU_OT_NONE && bonuslen != 0) ||
63106e0070dSMark Shellenbaum 	    (bonustype == DMU_OT_SA && bonuslen == 0));
632ad135b5dSChristopher Siden 	ASSERT(DMU_OT_IS_VALID(bonustype));
633fa9e4066Sahrens 	ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
634c543ec06Sahrens 
635ea8dc4b6Seschrock 	/* clean up any unreferenced dbufs */
6361934e92fSmaybee 	dnode_evict_dbufs(dn);
637ea8dc4b6Seschrock 
63828d97a71SMark Shellenbaum 	dn->dn_id_flags = 0;
63928d97a71SMark Shellenbaum 
640fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
641fa9e4066Sahrens 	dnode_setdirty(dn, tx);
6422bf405a2SMark Maybee 	if (dn->dn_datablksz != blocksize) {
6432bf405a2SMark Maybee 		/* change blocksize */
6442bf405a2SMark Maybee 		ASSERT(dn->dn_maxblkid == 0 &&
6452bf405a2SMark Maybee 		    (BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) ||
6462bf405a2SMark Maybee 		    dnode_block_freed(dn, 0)));
6472bf405a2SMark Maybee 		dnode_setdblksz(dn, blocksize);
648c543ec06Sahrens 		dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize;
6492bf405a2SMark Maybee 	}
6502bf405a2SMark Maybee 	if (dn->dn_bonuslen != bonuslen)
6512bf405a2SMark Maybee 		dn->dn_next_bonuslen[tx->tx_txg&TXG_MASK] = bonuslen;
65206e0070dSMark Shellenbaum 
65306e0070dSMark Shellenbaum 	if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
65406e0070dSMark Shellenbaum 		nblkptr = 1;
65506e0070dSMark Shellenbaum 	else
6562bf405a2SMark Maybee 		nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
6570a586ceaSMark Shellenbaum 	if (dn->dn_bonustype != bonustype)
6580a586ceaSMark Shellenbaum 		dn->dn_next_bonustype[tx->tx_txg&TXG_MASK] = bonustype;
659da03de99SMark Maybee 	if (dn->dn_nblkptr != nblkptr)
660da03de99SMark Maybee 		dn->dn_next_nblkptr[tx->tx_txg&TXG_MASK] = nblkptr;
6610a586ceaSMark Shellenbaum 	if (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
66206e0070dSMark Shellenbaum 		dbuf_rm_spill(dn, tx);
66306e0070dSMark Shellenbaum 		dnode_rm_spill(dn, tx);
6640a586ceaSMark Shellenbaum 	}
665fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
666fa9e4066Sahrens 
667fa9e4066Sahrens 	/* change type */
668fa9e4066Sahrens 	dn->dn_type = ot;
669fa9e4066Sahrens 
670fa9e4066Sahrens 	/* change bonus size and type */
671fa9e4066Sahrens 	mutex_enter(&dn->dn_mtx);
672fa9e4066Sahrens 	dn->dn_bonustype = bonustype;
673fa9e4066Sahrens 	dn->dn_bonuslen = bonuslen;
674da03de99SMark Maybee 	dn->dn_nblkptr = nblkptr;
675fa9e4066Sahrens 	dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
676fa9e4066Sahrens 	dn->dn_compress = ZIO_COMPRESS_INHERIT;
677fa9e4066Sahrens 	ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
678fa9e4066Sahrens 
679da03de99SMark Maybee 	/* fix up the bonus db_size */
680da03de99SMark Maybee 	if (dn->dn_bonus) {
6811934e92fSmaybee 		dn->dn_bonus->db.db_size =
6821934e92fSmaybee 		    DN_MAX_BONUSLEN - (dn->dn_nblkptr-1) * sizeof (blkptr_t);
6831934e92fSmaybee 		ASSERT(dn->dn_bonuslen <= dn->dn_bonus->db.db_size);
6841934e92fSmaybee 	}
685432f72fdSahrens 
686fa9e4066Sahrens 	dn->dn_allocated_txg = tx->tx_txg;
687fa9e4066Sahrens 	mutex_exit(&dn->dn_mtx);
688fa9e4066Sahrens }
689fa9e4066Sahrens 
690744947dcSTom Erickson #ifdef	DNODE_STATS
691744947dcSTom Erickson static struct {
692744947dcSTom Erickson 	uint64_t dms_dnode_invalid;
693744947dcSTom Erickson 	uint64_t dms_dnode_recheck1;
694744947dcSTom Erickson 	uint64_t dms_dnode_recheck2;
695744947dcSTom Erickson 	uint64_t dms_dnode_special;
696744947dcSTom Erickson 	uint64_t dms_dnode_handle;
697744947dcSTom Erickson 	uint64_t dms_dnode_rwlock;
698744947dcSTom Erickson 	uint64_t dms_dnode_active;
699744947dcSTom Erickson } dnode_move_stats;
700744947dcSTom Erickson #endif	/* DNODE_STATS */
701744947dcSTom Erickson 
702744947dcSTom Erickson static void
dnode_move_impl(dnode_t * odn,dnode_t * ndn)703744947dcSTom Erickson dnode_move_impl(dnode_t *odn, dnode_t *ndn)
704fa9e4066Sahrens {
705744947dcSTom Erickson 	int i;
706744947dcSTom Erickson 
707744947dcSTom Erickson 	ASSERT(!RW_LOCK_HELD(&odn->dn_struct_rwlock));
708744947dcSTom Erickson 	ASSERT(MUTEX_NOT_HELD(&odn->dn_mtx));
709744947dcSTom Erickson 	ASSERT(MUTEX_NOT_HELD(&odn->dn_dbufs_mtx));
710744947dcSTom Erickson 	ASSERT(!RW_LOCK_HELD(&odn->dn_zfetch.zf_rwlock));
711744947dcSTom Erickson 
712744947dcSTom Erickson 	/* Copy fields. */
713744947dcSTom Erickson 	ndn->dn_objset = odn->dn_objset;
714744947dcSTom Erickson 	ndn->dn_object = odn->dn_object;
715744947dcSTom Erickson 	ndn->dn_dbuf = odn->dn_dbuf;
716744947dcSTom Erickson 	ndn->dn_handle = odn->dn_handle;
717744947dcSTom Erickson 	ndn->dn_phys = odn->dn_phys;
718744947dcSTom Erickson 	ndn->dn_type = odn->dn_type;
719744947dcSTom Erickson 	ndn->dn_bonuslen = odn->dn_bonuslen;
720744947dcSTom Erickson 	ndn->dn_bonustype = odn->dn_bonustype;
721744947dcSTom Erickson 	ndn->dn_nblkptr = odn->dn_nblkptr;
722744947dcSTom Erickson 	ndn->dn_checksum = odn->dn_checksum;
723744947dcSTom Erickson 	ndn->dn_compress = odn->dn_compress;
724744947dcSTom Erickson 	ndn->dn_nlevels = odn->dn_nlevels;
725744947dcSTom Erickson 	ndn->dn_indblkshift = odn->dn_indblkshift;
726744947dcSTom Erickson 	ndn->dn_datablkshift = odn->dn_datablkshift;
727744947dcSTom Erickson 	ndn->dn_datablkszsec = odn->dn_datablkszsec;
728744947dcSTom Erickson 	ndn->dn_datablksz = odn->dn_datablksz;
729744947dcSTom Erickson 	ndn->dn_maxblkid = odn->dn_maxblkid;
730744947dcSTom Erickson 	bcopy(&odn->dn_next_nblkptr[0], &ndn->dn_next_nblkptr[0],
731744947dcSTom Erickson 	    sizeof (odn->dn_next_nblkptr));
732744947dcSTom Erickson 	bcopy(&odn->dn_next_nlevels[0], &ndn->dn_next_nlevels[0],
733744947dcSTom Erickson 	    sizeof (odn->dn_next_nlevels));
734744947dcSTom Erickson 	bcopy(&odn->dn_next_indblkshift[0], &ndn->dn_next_indblkshift[0],
735744947dcSTom Erickson 	    sizeof (odn->dn_next_indblkshift));
736744947dcSTom Erickson 	bcopy(&odn->dn_next_bonustype[0], &ndn->dn_next_bonustype[0],
737744947dcSTom Erickson 	    sizeof (odn->dn_next_bonustype));
738744947dcSTom Erickson 	bcopy(&odn->dn_rm_spillblk[0], &ndn->dn_rm_spillblk[0],
739744947dcSTom Erickson 	    sizeof (odn->dn_rm_spillblk));
740744947dcSTom Erickson 	bcopy(&odn->dn_next_bonuslen[0], &ndn->dn_next_bonuslen[0],
741744947dcSTom Erickson 	    sizeof (odn->dn_next_bonuslen));
742744947dcSTom Erickson 	bcopy(&odn->dn_next_blksz[0], &ndn->dn_next_blksz[0],
743744947dcSTom Erickson 	    sizeof (odn->dn_next_blksz));
744744947dcSTom Erickson 	for (i = 0; i < TXG_SIZE; i++) {
745744947dcSTom Erickson 		list_move_tail(&ndn->dn_dirty_records[i],
746744947dcSTom Erickson 		    &odn->dn_dirty_records[i]);
747744947dcSTom Erickson 	}
748887cdfc7SMatthew Ahrens 	bcopy(&odn->dn_free_ranges[0], &ndn->dn_free_ranges[0],
749887cdfc7SMatthew Ahrens 	    sizeof (odn->dn_free_ranges));
750744947dcSTom Erickson 	ndn->dn_allocated_txg = odn->dn_allocated_txg;
751744947dcSTom Erickson 	ndn->dn_free_txg = odn->dn_free_txg;
752744947dcSTom Erickson 	ndn->dn_assigned_txg = odn->dn_assigned_txg;
753744947dcSTom Erickson 	ndn->dn_dirtyctx = odn->dn_dirtyctx;
754744947dcSTom Erickson 	ndn->dn_dirtyctx_firstset = odn->dn_dirtyctx_firstset;
755744947dcSTom Erickson 	ASSERT(refcount_count(&odn->dn_tx_holds) == 0);
756744947dcSTom Erickson 	refcount_transfer(&ndn->dn_holds, &odn->dn_holds);
757d080a709SAlex Reece 	ASSERT(avl_is_empty(&ndn->dn_dbufs));
758d080a709SAlex Reece 	avl_swap(&ndn->dn_dbufs, &odn->dn_dbufs);
759744947dcSTom Erickson 	ndn->dn_dbufs_count = odn->dn_dbufs_count;
760713d6c20SMatthew Ahrens 	ndn->dn_unlisted_l0_blkid = odn->dn_unlisted_l0_blkid;
761744947dcSTom Erickson 	ndn->dn_bonus = odn->dn_bonus;
762744947dcSTom Erickson 	ndn->dn_have_spill = odn->dn_have_spill;
763744947dcSTom Erickson 	ndn->dn_zio = odn->dn_zio;
764744947dcSTom Erickson 	ndn->dn_oldused = odn->dn_oldused;
765744947dcSTom Erickson 	ndn->dn_oldflags = odn->dn_oldflags;
766744947dcSTom Erickson 	ndn->dn_olduid = odn->dn_olduid;
767744947dcSTom Erickson 	ndn->dn_oldgid = odn->dn_oldgid;
768744947dcSTom Erickson 	ndn->dn_newuid = odn->dn_newuid;
769744947dcSTom Erickson 	ndn->dn_newgid = odn->dn_newgid;
770744947dcSTom Erickson 	ndn->dn_id_flags = odn->dn_id_flags;
771744947dcSTom Erickson 	dmu_zfetch_init(&ndn->dn_zfetch, NULL);
772744947dcSTom Erickson 	list_move_tail(&ndn->dn_zfetch.zf_stream, &odn->dn_zfetch.zf_stream);
773744947dcSTom Erickson 	ndn->dn_zfetch.zf_dnode = odn->dn_zfetch.zf_dnode;
774744947dcSTom Erickson 	ndn->dn_zfetch.zf_stream_cnt = odn->dn_zfetch.zf_stream_cnt;
775744947dcSTom Erickson 	ndn->dn_zfetch.zf_alloc_fail = odn->dn_zfetch.zf_alloc_fail;
776744947dcSTom Erickson 
777744947dcSTom Erickson 	/*
778744947dcSTom Erickson 	 * Update back pointers. Updating the handle fixes the back pointer of
779744947dcSTom Erickson 	 * every descendant dbuf as well as the bonus dbuf.
780744947dcSTom Erickson 	 */
781744947dcSTom Erickson 	ASSERT(ndn->dn_handle->dnh_dnode == odn);
782744947dcSTom Erickson 	ndn->dn_handle->dnh_dnode = ndn;
783744947dcSTom Erickson 	if (ndn->dn_zfetch.zf_dnode == odn) {
784744947dcSTom Erickson 		ndn->dn_zfetch.zf_dnode = ndn;
785744947dcSTom Erickson 	}
786744947dcSTom Erickson 
787744947dcSTom Erickson 	/*
788744947dcSTom Erickson 	 * Invalidate the original dnode by clearing all of its back pointers.
789744947dcSTom Erickson 	 */
790744947dcSTom Erickson 	odn->dn_dbuf = NULL;
791744947dcSTom Erickson 	odn->dn_handle = NULL;
792d080a709SAlex Reece 	avl_create(&odn->dn_dbufs, dbuf_compare, sizeof (dmu_buf_impl_t),
793744947dcSTom Erickson 	    offsetof(dmu_buf_impl_t, db_link));
794744947dcSTom Erickson 	odn->dn_dbufs_count = 0;
795713d6c20SMatthew Ahrens 	odn->dn_unlisted_l0_blkid = 0;
796744947dcSTom Erickson 	odn->dn_bonus = NULL;
797744947dcSTom Erickson 	odn->dn_zfetch.zf_dnode = NULL;
798744947dcSTom Erickson 
799744947dcSTom Erickson 	/*
800744947dcSTom Erickson 	 * Set the low bit of the objset pointer to ensure that dnode_move()
801744947dcSTom Erickson 	 * recognizes the dnode as invalid in any subsequent callback.
802744947dcSTom Erickson 	 */
803744947dcSTom Erickson 	POINTER_INVALIDATE(&odn->dn_objset);
804744947dcSTom Erickson 
805744947dcSTom Erickson 	/*
806744947dcSTom Erickson 	 * Satisfy the destructor.
807744947dcSTom Erickson 	 */
808744947dcSTom Erickson 	for (i = 0; i < TXG_SIZE; i++) {
809744947dcSTom Erickson 		list_create(&odn->dn_dirty_records[i],
810744947dcSTom Erickson 		    sizeof (dbuf_dirty_record_t),
811744947dcSTom Erickson 		    offsetof(dbuf_dirty_record_t, dr_dirty_node));
812887cdfc7SMatthew Ahrens 		odn->dn_free_ranges[i] = NULL;
813744947dcSTom Erickson 		odn->dn_next_nlevels[i] = 0;
814744947dcSTom Erickson 		odn->dn_next_indblkshift[i] = 0;
815744947dcSTom Erickson 		odn->dn_next_bonustype[i] = 0;
816744947dcSTom Erickson 		odn->dn_rm_spillblk[i] = 0;
817744947dcSTom Erickson 		odn->dn_next_bonuslen[i] = 0;
818744947dcSTom Erickson 		odn->dn_next_blksz[i] = 0;
819744947dcSTom Erickson 	}
820744947dcSTom Erickson 	odn->dn_allocated_txg = 0;
821744947dcSTom Erickson 	odn->dn_free_txg = 0;
822744947dcSTom Erickson 	odn->dn_assigned_txg = 0;
823744947dcSTom Erickson 	odn->dn_dirtyctx = 0;
824744947dcSTom Erickson 	odn->dn_dirtyctx_firstset = NULL;
825744947dcSTom Erickson 	odn->dn_have_spill = B_FALSE;
826744947dcSTom Erickson 	odn->dn_zio = NULL;
827744947dcSTom Erickson 	odn->dn_oldused = 0;
828744947dcSTom Erickson 	odn->dn_oldflags = 0;
829744947dcSTom Erickson 	odn->dn_olduid = 0;
830744947dcSTom Erickson 	odn->dn_oldgid = 0;
831744947dcSTom Erickson 	odn->dn_newuid = 0;
832744947dcSTom Erickson 	odn->dn_newgid = 0;
833744947dcSTom Erickson 	odn->dn_id_flags = 0;
834744947dcSTom Erickson 
835744947dcSTom Erickson 	/*
836744947dcSTom Erickson 	 * Mark the dnode.
837744947dcSTom Erickson 	 */
838744947dcSTom Erickson 	ndn->dn_moved = 1;
839744947dcSTom Erickson 	odn->dn_moved = (uint8_t)-1;
840744947dcSTom Erickson }
841744947dcSTom Erickson 
842744947dcSTom Erickson #ifdef	_KERNEL
843744947dcSTom Erickson /*ARGSUSED*/
844744947dcSTom Erickson static kmem_cbrc_t
dnode_move(void * buf,void * newbuf,size_t size,void * arg)845744947dcSTom Erickson dnode_move(void *buf, void *newbuf, size_t size, void *arg)
846744947dcSTom Erickson {
847744947dcSTom Erickson 	dnode_t *odn = buf, *ndn = newbuf;
848744947dcSTom Erickson 	objset_t *os;
849744947dcSTom Erickson 	int64_t refcount;
850744947dcSTom Erickson 	uint32_t dbufs;
851744947dcSTom Erickson 
852744947dcSTom Erickson 	/*
853744947dcSTom Erickson 	 * The dnode is on the objset's list of known dnodes if the objset
854744947dcSTom Erickson 	 * pointer is valid. We set the low bit of the objset pointer when
855744947dcSTom Erickson 	 * freeing the dnode to invalidate it, and the memory patterns written
856744947dcSTom Erickson 	 * by kmem (baddcafe and deadbeef) set at least one of the two low bits.
857744947dcSTom Erickson 	 * A newly created dnode sets the objset pointer last of all to indicate
858744947dcSTom Erickson 	 * that the dnode is known and in a valid state to be moved by this
859744947dcSTom Erickson 	 * function.
860744947dcSTom Erickson 	 */
861744947dcSTom Erickson 	os = odn->dn_objset;
862744947dcSTom Erickson 	if (!POINTER_IS_VALID(os)) {
863744947dcSTom Erickson 		DNODE_STAT_ADD(dnode_move_stats.dms_dnode_invalid);
864744947dcSTom Erickson 		return (KMEM_CBRC_DONT_KNOW);
865744947dcSTom Erickson 	}
866744947dcSTom Erickson 
867744947dcSTom Erickson 	/*
868744947dcSTom Erickson 	 * Ensure that the objset does not go away during the move.
869744947dcSTom Erickson 	 */
870744947dcSTom Erickson 	rw_enter(&os_lock, RW_WRITER);
871744947dcSTom Erickson 	if (os != odn->dn_objset) {
872744947dcSTom Erickson 		rw_exit(&os_lock);
873744947dcSTom Erickson 		DNODE_STAT_ADD(dnode_move_stats.dms_dnode_recheck1);
874744947dcSTom Erickson 		return (KMEM_CBRC_DONT_KNOW);
875744947dcSTom Erickson 	}
876744947dcSTom Erickson 
877744947dcSTom Erickson 	/*
878744947dcSTom Erickson 	 * If the dnode is still valid, then so is the objset. We know that no
879744947dcSTom Erickson 	 * valid objset can be freed while we hold os_lock, so we can safely
880744947dcSTom Erickson 	 * ensure that the objset remains in use.
881744947dcSTom Erickson 	 */
882744947dcSTom Erickson 	mutex_enter(&os->os_lock);
883744947dcSTom Erickson 
884744947dcSTom Erickson 	/*
885744947dcSTom Erickson 	 * Recheck the objset pointer in case the dnode was removed just before
886744947dcSTom Erickson 	 * acquiring the lock.
887744947dcSTom Erickson 	 */
888744947dcSTom Erickson 	if (os != odn->dn_objset) {
889744947dcSTom Erickson 		mutex_exit(&os->os_lock);
890744947dcSTom Erickson 		rw_exit(&os_lock);
891744947dcSTom Erickson 		DNODE_STAT_ADD(dnode_move_stats.dms_dnode_recheck2);
892744947dcSTom Erickson 		return (KMEM_CBRC_DONT_KNOW);
893744947dcSTom Erickson 	}
894744947dcSTom Erickson 
895744947dcSTom Erickson 	/*
896744947dcSTom Erickson 	 * At this point we know that as long as we hold os->os_lock, the dnode
897744947dcSTom Erickson 	 * cannot be freed and fields within the dnode can be safely accessed.
898744947dcSTom Erickson 	 * The objset listing this dnode cannot go away as long as this dnode is
899744947dcSTom Erickson 	 * on its list.
900744947dcSTom Erickson 	 */
901744947dcSTom Erickson 	rw_exit(&os_lock);
902744947dcSTom Erickson 	if (DMU_OBJECT_IS_SPECIAL(odn->dn_object)) {
903744947dcSTom Erickson 		mutex_exit(&os->os_lock);
904744947dcSTom Erickson 		DNODE_STAT_ADD(dnode_move_stats.dms_dnode_special);
905744947dcSTom Erickson 		return (KMEM_CBRC_NO);
906744947dcSTom Erickson 	}
907744947dcSTom Erickson 	ASSERT(odn->dn_dbuf != NULL); /* only "special" dnodes have no parent */
908744947dcSTom Erickson 
909744947dcSTom Erickson 	/*
910744947dcSTom Erickson 	 * Lock the dnode handle to prevent the dnode from obtaining any new
911744947dcSTom Erickson 	 * holds. This also prevents the descendant dbufs and the bonus dbuf
912744947dcSTom Erickson 	 * from accessing the dnode, so that we can discount their holds. The
913744947dcSTom Erickson 	 * handle is safe to access because we know that while the dnode cannot
914744947dcSTom Erickson 	 * go away, neither can its handle. Once we hold dnh_zrlock, we can
915744947dcSTom Erickson 	 * safely move any dnode referenced only by dbufs.
916744947dcSTom Erickson 	 */
917744947dcSTom Erickson 	if (!zrl_tryenter(&odn->dn_handle->dnh_zrlock)) {
918744947dcSTom Erickson 		mutex_exit(&os->os_lock);
919744947dcSTom Erickson 		DNODE_STAT_ADD(dnode_move_stats.dms_dnode_handle);
920744947dcSTom Erickson 		return (KMEM_CBRC_LATER);
921744947dcSTom Erickson 	}
922744947dcSTom Erickson 
923744947dcSTom Erickson 	/*
924744947dcSTom Erickson 	 * Ensure a consistent view of the dnode's holds and the dnode's dbufs.
925744947dcSTom Erickson 	 * We need to guarantee that there is a hold for every dbuf in order to
926744947dcSTom Erickson 	 * determine whether the dnode is actively referenced. Falsely matching
927744947dcSTom Erickson 	 * a dbuf to an active hold would lead to an unsafe move. It's possible
928744947dcSTom Erickson 	 * that a thread already having an active dnode hold is about to add a
929744947dcSTom Erickson 	 * dbuf, and we can't compare hold and dbuf counts while the add is in
930744947dcSTom Erickson 	 * progress.
931744947dcSTom Erickson 	 */
932744947dcSTom Erickson 	if (!rw_tryenter(&odn->dn_struct_rwlock, RW_WRITER)) {
933744947dcSTom Erickson 		zrl_exit(&odn->dn_handle->dnh_zrlock);
934744947dcSTom Erickson 		mutex_exit(&os->os_lock);
935744947dcSTom Erickson 		DNODE_STAT_ADD(dnode_move_stats.dms_dnode_rwlock);
936744947dcSTom Erickson 		return (KMEM_CBRC_LATER);
937744947dcSTom Erickson 	}
938744947dcSTom Erickson 
939744947dcSTom Erickson 	/*
940744947dcSTom Erickson 	 * A dbuf may be removed (evicted) without an active dnode hold. In that
941744947dcSTom Erickson 	 * case, the dbuf count is decremented under the handle lock before the
942744947dcSTom Erickson 	 * dbuf's hold is released. This order ensures that if we count the hold
943744947dcSTom Erickson 	 * after the dbuf is removed but before its hold is released, we will
944744947dcSTom Erickson 	 * treat the unmatched hold as active and exit safely. If we count the
945744947dcSTom Erickson 	 * hold before the dbuf is removed, the hold is discounted, and the
946744947dcSTom Erickson 	 * removal is blocked until the move completes.
947744947dcSTom Erickson 	 */
948744947dcSTom Erickson 	refcount = refcount_count(&odn->dn_holds);
949744947dcSTom Erickson 	ASSERT(refcount >= 0);
950744947dcSTom Erickson 	dbufs = odn->dn_dbufs_count;
951744947dcSTom Erickson 
952744947dcSTom Erickson 	/* We can't have more dbufs than dnode holds. */
953744947dcSTom Erickson 	ASSERT3U(dbufs, <=, refcount);
954744947dcSTom Erickson 	DTRACE_PROBE3(dnode__move, dnode_t *, odn, int64_t, refcount,
955744947dcSTom Erickson 	    uint32_t, dbufs);
956744947dcSTom Erickson 
957744947dcSTom Erickson 	if (refcount > dbufs) {
958744947dcSTom Erickson 		rw_exit(&odn->dn_struct_rwlock);
959744947dcSTom Erickson 		zrl_exit(&odn->dn_handle->dnh_zrlock);
960744947dcSTom Erickson 		mutex_exit(&os->os_lock);
961744947dcSTom Erickson 		DNODE_STAT_ADD(dnode_move_stats.dms_dnode_active);
962744947dcSTom Erickson 		return (KMEM_CBRC_LATER);
963744947dcSTom Erickson 	}
964744947dcSTom Erickson 
965744947dcSTom Erickson 	rw_exit(&odn->dn_struct_rwlock);
966744947dcSTom Erickson 
967744947dcSTom Erickson 	/*
968744947dcSTom Erickson 	 * At this point we know that anyone with a hold on the dnode is not
969744947dcSTom Erickson 	 * actively referencing it. The dnode is known and in a valid state to
970744947dcSTom Erickson 	 * move. We're holding the locks needed to execute the critical section.
971744947dcSTom Erickson 	 */
972744947dcSTom Erickson 	dnode_move_impl(odn, ndn);
973744947dcSTom Erickson 
974744947dcSTom Erickson 	list_link_replace(&odn->dn_link, &ndn->dn_link);
975744947dcSTom Erickson 	/* If the dnode was safe to move, the refcount cannot have changed. */
976744947dcSTom Erickson 	ASSERT(refcount == refcount_count(&ndn->dn_holds));
977744947dcSTom Erickson 	ASSERT(dbufs == ndn->dn_dbufs_count);
978744947dcSTom Erickson 	zrl_exit(&ndn->dn_handle->dnh_zrlock); /* handle has moved */
979744947dcSTom Erickson 	mutex_exit(&os->os_lock);
980744947dcSTom Erickson 
981744947dcSTom Erickson 	return (KMEM_CBRC_YES);
982744947dcSTom Erickson }
983744947dcSTom Erickson #endif	/* _KERNEL */
984744947dcSTom Erickson 
985744947dcSTom Erickson void
dnode_special_close(dnode_handle_t * dnh)986744947dcSTom Erickson dnode_special_close(dnode_handle_t *dnh)
987744947dcSTom Erickson {
988744947dcSTom Erickson 	dnode_t *dn = dnh->dnh_dnode;
989744947dcSTom Erickson 
990ea8dc4b6Seschrock 	/*
991ea8dc4b6Seschrock 	 * Wait for final references to the dnode to clear.  This can
992ea8dc4b6Seschrock 	 * only happen if the arc is asyncronously evicting state that
993ea8dc4b6Seschrock 	 * has a hold on this dnode while we are trying to evict this
994ea8dc4b6Seschrock 	 * dnode.
995ea8dc4b6Seschrock 	 */
996ea8dc4b6Seschrock 	while (refcount_count(&dn->dn_holds) > 0)
997ea8dc4b6Seschrock 		delay(1);
9980521c5ebSJustin Gibbs 	ASSERT(dn->dn_dbuf == NULL ||
9990521c5ebSJustin Gibbs 	    dmu_buf_get_user(&dn->dn_dbuf->db) == NULL);
1000744947dcSTom Erickson 	zrl_add(&dnh->dnh_zrlock);
1001744947dcSTom Erickson 	dnode_destroy(dn); /* implicit zrl_remove() */
1002744947dcSTom Erickson 	zrl_destroy(&dnh->dnh_zrlock);
1003744947dcSTom Erickson 	dnh->dnh_dnode = NULL;
1004fa9e4066Sahrens }
1005fa9e4066Sahrens 
10060521c5ebSJustin Gibbs void
dnode_special_open(objset_t * os,dnode_phys_t * dnp,uint64_t object,dnode_handle_t * dnh)1007744947dcSTom Erickson dnode_special_open(objset_t *os, dnode_phys_t *dnp, uint64_t object,
1008744947dcSTom Erickson     dnode_handle_t *dnh)
1009fa9e4066Sahrens {
10100521c5ebSJustin Gibbs 	dnode_t *dn;
10110521c5ebSJustin Gibbs 
10120521c5ebSJustin Gibbs 	dn = dnode_create(os, dnp, NULL, object, dnh);
1013744947dcSTom Erickson 	zrl_init(&dnh->dnh_zrlock);
10149c9dc39aSek110237 	DNODE_VERIFY(dn);
1015fa9e4066Sahrens }
1016fa9e4066Sahrens 
1017fa9e4066Sahrens static void
dnode_buf_evict_async(void * dbu)1018*c4cb9576SJosef 'Jeff' Sipek dnode_buf_evict_async(void *dbu)
1019fa9e4066Sahrens {
10200521c5ebSJustin Gibbs 	dnode_children_t *children_dnodes = dbu;
1021fa9e4066Sahrens 	int i;
1022fa9e4066Sahrens 
10230521c5ebSJustin Gibbs 	for (i = 0; i < children_dnodes->dnc_count; i++) {
1024744947dcSTom Erickson 		dnode_handle_t *dnh = &children_dnodes->dnc_children[i];
1025744947dcSTom Erickson 		dnode_t *dn;
1026744947dcSTom Erickson 
1027744947dcSTom Erickson 		/*
1028744947dcSTom Erickson 		 * The dnode handle lock guards against the dnode moving to
1029744947dcSTom Erickson 		 * another valid address, so there is no need here to guard
1030744947dcSTom Erickson 		 * against changes to or from NULL.
1031744947dcSTom Erickson 		 */
1032744947dcSTom Erickson 		if (dnh->dnh_dnode == NULL) {
1033744947dcSTom Erickson 			zrl_destroy(&dnh->dnh_zrlock);
1034fa9e4066Sahrens 			continue;
1035744947dcSTom Erickson 		}
1036744947dcSTom Erickson 
1037744947dcSTom Erickson 		zrl_add(&dnh->dnh_zrlock);
1038744947dcSTom Erickson 		dn = dnh->dnh_dnode;
1039fa9e4066Sahrens 		/*
1040fa9e4066Sahrens 		 * If there are holds on this dnode, then there should
1041fa9e4066Sahrens 		 * be holds on the dnode's containing dbuf as well; thus
1042744947dcSTom Erickson 		 * it wouldn't be eligible for eviction and this function
1043fa9e4066Sahrens 		 * would not have been called.
1044fa9e4066Sahrens 		 */
1045fa9e4066Sahrens 		ASSERT(refcount_is_zero(&dn->dn_holds));
1046fa9e4066Sahrens 		ASSERT(refcount_is_zero(&dn->dn_tx_holds));
1047fa9e4066Sahrens 
1048744947dcSTom Erickson 		dnode_destroy(dn); /* implicit zrl_remove() */
1049744947dcSTom Erickson 		zrl_destroy(&dnh->dnh_zrlock);
1050744947dcSTom Erickson 		dnh->dnh_dnode = NULL;
1051fa9e4066Sahrens 	}
1052744947dcSTom Erickson 	kmem_free(children_dnodes, sizeof (dnode_children_t) +
10530521c5ebSJustin Gibbs 	    children_dnodes->dnc_count * sizeof (dnode_handle_t));
1054fa9e4066Sahrens }
1055fa9e4066Sahrens 
1056fa9e4066Sahrens /*
1057ea8dc4b6Seschrock  * errors:
1058ea8dc4b6Seschrock  * EINVAL - invalid object number.
1059ea8dc4b6Seschrock  * EIO - i/o error.
1060ea8dc4b6Seschrock  * succeeds even for free dnodes.
1061fa9e4066Sahrens  */
1062ea8dc4b6Seschrock int
dnode_hold_impl(objset_t * os,uint64_t object,int flag,void * tag,dnode_t ** dnp)1063503ad85cSMatthew Ahrens dnode_hold_impl(objset_t *os, uint64_t object, int flag,
1064ea8dc4b6Seschrock     void *tag, dnode_t **dnp)
1065fa9e4066Sahrens {
1066ea8dc4b6Seschrock 	int epb, idx, err;
1067fa9e4066Sahrens 	int drop_struct_lock = FALSE;
1068ea8dc4b6Seschrock 	int type;
1069fa9e4066Sahrens 	uint64_t blk;
1070fa9e4066Sahrens 	dnode_t *mdn, *dn;
1071fa9e4066Sahrens 	dmu_buf_impl_t *db;
1072744947dcSTom Erickson 	dnode_children_t *children_dnodes;
1073744947dcSTom Erickson 	dnode_handle_t *dnh;
1074fa9e4066Sahrens 
1075e14bb325SJeff Bonwick 	/*
1076e14bb325SJeff Bonwick 	 * If you are holding the spa config lock as writer, you shouldn't
1077dcba9f3fSGeorge Wilson 	 * be asking the DMU to do *anything* unless it's the root pool
1078dcba9f3fSGeorge Wilson 	 * which may require us to read from the root filesystem while
1079dcba9f3fSGeorge Wilson 	 * holding some (not all) of the locks as writer.
1080e14bb325SJeff Bonwick 	 */
1081dcba9f3fSGeorge Wilson 	ASSERT(spa_config_held(os->os_spa, SCL_ALL, RW_WRITER) == 0 ||
1082dcba9f3fSGeorge Wilson 	    (spa_is_root(os->os_spa) &&
108344ecc532SGeorge Wilson 	    spa_config_held(os->os_spa, SCL_STATE, RW_WRITER)));
1084e14bb325SJeff Bonwick 
108514843421SMatthew Ahrens 	if (object == DMU_USERUSED_OBJECT || object == DMU_GROUPUSED_OBJECT) {
108614843421SMatthew Ahrens 		dn = (object == DMU_USERUSED_OBJECT) ?
1087744947dcSTom Erickson 		    DMU_USERUSED_DNODE(os) : DMU_GROUPUSED_DNODE(os);
108814843421SMatthew Ahrens 		if (dn == NULL)
1089be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
109014843421SMatthew Ahrens 		type = dn->dn_type;
109114843421SMatthew Ahrens 		if ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE)
1092be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
109314843421SMatthew Ahrens 		if ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)
1094be6fd75aSMatthew Ahrens 			return (SET_ERROR(EEXIST));
109514843421SMatthew Ahrens 		DNODE_VERIFY(dn);
109614843421SMatthew Ahrens 		(void) refcount_add(&dn->dn_holds, tag);
109714843421SMatthew Ahrens 		*dnp = dn;
109814843421SMatthew Ahrens 		return (0);
109914843421SMatthew Ahrens 	}
110014843421SMatthew Ahrens 
1101fa9e4066Sahrens 	if (object == 0 || object >= DN_MAX_OBJECT)
1102be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1103fa9e4066Sahrens 
1104744947dcSTom Erickson 	mdn = DMU_META_DNODE(os);
1105744947dcSTom Erickson 	ASSERT(mdn->dn_object == DMU_META_DNODE_OBJECT);
1106fa9e4066Sahrens 
11079c9dc39aSek110237 	DNODE_VERIFY(mdn);
1108fa9e4066Sahrens 
1109fa9e4066Sahrens 	if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) {
1110fa9e4066Sahrens 		rw_enter(&mdn->dn_struct_rwlock, RW_READER);
1111fa9e4066Sahrens 		drop_struct_lock = TRUE;
1112fa9e4066Sahrens 	}
1113fa9e4066Sahrens 
11145f9bb2f3SPaul Dagnelie 	blk = dbuf_whichblock(mdn, 0, object * sizeof (dnode_phys_t));
1115fa9e4066Sahrens 
1116ea8dc4b6Seschrock 	db = dbuf_hold(mdn, blk, FTAG);
1117fa9e4066Sahrens 	if (drop_struct_lock)
1118fa9e4066Sahrens 		rw_exit(&mdn->dn_struct_rwlock);
1119ea8dc4b6Seschrock 	if (db == NULL)
1120be6fd75aSMatthew Ahrens 		return (SET_ERROR(EIO));
1121ea8dc4b6Seschrock 	err = dbuf_read(db, NULL, DB_RF_CANFAIL);
1122ea8dc4b6Seschrock 	if (err) {
1123ea8dc4b6Seschrock 		dbuf_rele(db, FTAG);
1124ea8dc4b6Seschrock 		return (err);
1125ea8dc4b6Seschrock 	}
1126fa9e4066Sahrens 
1127fa9e4066Sahrens 	ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT);
1128fa9e4066Sahrens 	epb = db->db.db_size >> DNODE_SHIFT;
1129fa9e4066Sahrens 
1130fa9e4066Sahrens 	idx = object & (epb-1);
1131fa9e4066Sahrens 
1132744947dcSTom Erickson 	ASSERT(DB_DNODE(db)->dn_type == DMU_OT_DNODE);
1133fa9e4066Sahrens 	children_dnodes = dmu_buf_get_user(&db->db);
1134fa9e4066Sahrens 	if (children_dnodes == NULL) {
1135744947dcSTom Erickson 		int i;
1136744947dcSTom Erickson 		dnode_children_t *winner;
11370521c5ebSJustin Gibbs 		children_dnodes = kmem_zalloc(sizeof (dnode_children_t) +
1138891ff7fbSJustin T. Gibbs 		    epb * sizeof (dnode_handle_t), KM_SLEEP);
1139744947dcSTom Erickson 		children_dnodes->dnc_count = epb;
1140744947dcSTom Erickson 		dnh = &children_dnodes->dnc_children[0];
1141744947dcSTom Erickson 		for (i = 0; i < epb; i++) {
1142744947dcSTom Erickson 			zrl_init(&dnh[i].dnh_zrlock);
1143744947dcSTom Erickson 		}
1144*c4cb9576SJosef 'Jeff' Sipek 		dmu_buf_init_user(&children_dnodes->dnc_dbu, NULL,
1145*c4cb9576SJosef 'Jeff' Sipek 		    dnode_buf_evict_async, NULL);
11460521c5ebSJustin Gibbs 		winner = dmu_buf_set_user(&db->db, &children_dnodes->dnc_dbu);
11470521c5ebSJustin Gibbs 		if (winner != NULL) {
1148fa565b39SJorgen Lundman 
1149fa565b39SJorgen Lundman 			for (i = 0; i < epb; i++) {
1150fa565b39SJorgen Lundman 				zrl_destroy(&dnh[i].dnh_zrlock);
1151fa565b39SJorgen Lundman 			}
1152fa565b39SJorgen Lundman 
1153744947dcSTom Erickson 			kmem_free(children_dnodes, sizeof (dnode_children_t) +
1154891ff7fbSJustin T. Gibbs 			    epb * sizeof (dnode_handle_t));
1155fa9e4066Sahrens 			children_dnodes = winner;
1156fa9e4066Sahrens 		}
1157fa9e4066Sahrens 	}
1158744947dcSTom Erickson 	ASSERT(children_dnodes->dnc_count == epb);
1159fa9e4066Sahrens 
1160744947dcSTom Erickson 	dnh = &children_dnodes->dnc_children[idx];
1161744947dcSTom Erickson 	zrl_add(&dnh->dnh_zrlock);
11620521c5ebSJustin Gibbs 	dn = dnh->dnh_dnode;
11630521c5ebSJustin Gibbs 	if (dn == NULL) {
1164744947dcSTom Erickson 		dnode_phys_t *phys = (dnode_phys_t *)db->db.db_data+idx;
11650e8c6158Smaybee 
1166744947dcSTom Erickson 		dn = dnode_create(os, phys, db, object, dnh);
1167fa9e4066Sahrens 	}
1168fa9e4066Sahrens 
1169fa9e4066Sahrens 	mutex_enter(&dn->dn_mtx);
1170ea8dc4b6Seschrock 	type = dn->dn_type;
1171fa9e4066Sahrens 	if (dn->dn_free_txg ||
1172ea8dc4b6Seschrock 	    ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) ||
117314843421SMatthew Ahrens 	    ((flag & DNODE_MUST_BE_FREE) &&
117428d97a71SMark Shellenbaum 	    (type != DMU_OT_NONE || !refcount_is_zero(&dn->dn_holds)))) {
1175fa9e4066Sahrens 		mutex_exit(&dn->dn_mtx);
1176744947dcSTom Erickson 		zrl_remove(&dnh->dnh_zrlock);
1177ea8dc4b6Seschrock 		dbuf_rele(db, FTAG);
1178ea8dc4b6Seschrock 		return (type == DMU_OT_NONE ? ENOENT : EEXIST);
1179fa9e4066Sahrens 	}
1180ea8dc4b6Seschrock 	if (refcount_add(&dn->dn_holds, tag) == 1)
1181744947dcSTom Erickson 		dbuf_add_ref(db, dnh);
11820521c5ebSJustin Gibbs 	mutex_exit(&dn->dn_mtx);
11830521c5ebSJustin Gibbs 
1184744947dcSTom Erickson 	/* Now we can rely on the hold to prevent the dnode from moving. */
1185744947dcSTom Erickson 	zrl_remove(&dnh->dnh_zrlock);
1186fa9e4066Sahrens 
11879c9dc39aSek110237 	DNODE_VERIFY(dn);
1188fa9e4066Sahrens 	ASSERT3P(dn->dn_dbuf, ==, db);
1189fa9e4066Sahrens 	ASSERT3U(dn->dn_object, ==, object);
1190ea8dc4b6Seschrock 	dbuf_rele(db, FTAG);
1191fa9e4066Sahrens 
1192ea8dc4b6Seschrock 	*dnp = dn;
1193ea8dc4b6Seschrock 	return (0);
1194fa9e4066Sahrens }
1195fa9e4066Sahrens 
1196fa9e4066Sahrens /*
1197fa9e4066Sahrens  * Return held dnode if the object is allocated, NULL if not.
1198fa9e4066Sahrens  */
1199ea8dc4b6Seschrock int
dnode_hold(objset_t * os,uint64_t object,void * tag,dnode_t ** dnp)1200503ad85cSMatthew Ahrens dnode_hold(objset_t *os, uint64_t object, void *tag, dnode_t **dnp)
1201fa9e4066Sahrens {
1202ea8dc4b6Seschrock 	return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, tag, dnp));
1203fa9e4066Sahrens }
1204fa9e4066Sahrens 
12051934e92fSmaybee /*
12061934e92fSmaybee  * Can only add a reference if there is already at least one
12071934e92fSmaybee  * reference on the dnode.  Returns FALSE if unable to add a
12081934e92fSmaybee  * new reference.
12091934e92fSmaybee  */
12101934e92fSmaybee boolean_t
dnode_add_ref(dnode_t * dn,void * tag)1211ea8dc4b6Seschrock dnode_add_ref(dnode_t *dn, void *tag)
1212fa9e4066Sahrens {
12131934e92fSmaybee 	mutex_enter(&dn->dn_mtx);
12141934e92fSmaybee 	if (refcount_is_zero(&dn->dn_holds)) {
12151934e92fSmaybee 		mutex_exit(&dn->dn_mtx);
12161934e92fSmaybee 		return (FALSE);
12171934e92fSmaybee 	}
12181934e92fSmaybee 	VERIFY(1 < refcount_add(&dn->dn_holds, tag));
12191934e92fSmaybee 	mutex_exit(&dn->dn_mtx);
12201934e92fSmaybee 	return (TRUE);
1221fa9e4066Sahrens }
1222fa9e4066Sahrens 
1223fa9e4066Sahrens void
dnode_rele(dnode_t * dn,void * tag)1224ea8dc4b6Seschrock dnode_rele(dnode_t *dn, void *tag)
1225fa9e4066Sahrens {
1226300ba082SJustin T. Gibbs 	mutex_enter(&dn->dn_mtx);
1227300ba082SJustin T. Gibbs 	dnode_rele_and_unlock(dn, tag);
1228300ba082SJustin T. Gibbs }
1229300ba082SJustin T. Gibbs 
1230300ba082SJustin T. Gibbs void
dnode_rele_and_unlock(dnode_t * dn,void * tag)1231300ba082SJustin T. Gibbs dnode_rele_and_unlock(dnode_t *dn, void *tag)
1232300ba082SJustin T. Gibbs {
1233fa9e4066Sahrens 	uint64_t refs;
1234744947dcSTom Erickson 	/* Get while the hold prevents the dnode from moving. */
1235744947dcSTom Erickson 	dmu_buf_impl_t *db = dn->dn_dbuf;
1236744947dcSTom Erickson 	dnode_handle_t *dnh = dn->dn_handle;
1237fa9e4066Sahrens 
1238ea8dc4b6Seschrock 	refs = refcount_remove(&dn->dn_holds, tag);
12391934e92fSmaybee 	mutex_exit(&dn->dn_mtx);
1240744947dcSTom Erickson 
1241744947dcSTom Erickson 	/*
1242744947dcSTom Erickson 	 * It's unsafe to release the last hold on a dnode by dnode_rele() or
1243744947dcSTom Erickson 	 * indirectly by dbuf_rele() while relying on the dnode handle to
1244744947dcSTom Erickson 	 * prevent the dnode from moving, since releasing the last hold could
1245744947dcSTom Erickson 	 * result in the dnode's parent dbuf evicting its dnode handles. For
1246744947dcSTom Erickson 	 * that reason anyone calling dnode_rele() or dbuf_rele() without some
1247744947dcSTom Erickson 	 * other direct or indirect hold on the dnode must first drop the dnode
1248744947dcSTom Erickson 	 * handle.
1249744947dcSTom Erickson 	 */
1250744947dcSTom Erickson 	ASSERT(refs > 0 || dnh->dnh_zrlock.zr_owner != curthread);
1251744947dcSTom Erickson 
1252fa9e4066Sahrens 	/* NOTE: the DNODE_DNODE does not have a dn_dbuf */
1253744947dcSTom Erickson 	if (refs == 0 && db != NULL) {
1254744947dcSTom Erickson 		/*
1255744947dcSTom Erickson 		 * Another thread could add a hold to the dnode handle in
1256744947dcSTom Erickson 		 * dnode_hold_impl() while holding the parent dbuf. Since the
1257744947dcSTom Erickson 		 * hold on the parent dbuf prevents the handle from being
1258744947dcSTom Erickson 		 * destroyed, the hold on the handle is OK. We can't yet assert
1259744947dcSTom Erickson 		 * that the handle has zero references, but that will be
1260744947dcSTom Erickson 		 * asserted anyway when the handle gets destroyed.
1261744947dcSTom Erickson 		 */
1262744947dcSTom Erickson 		dbuf_rele(db, dnh);
1263744947dcSTom Erickson 	}
1264fa9e4066Sahrens }
1265fa9e4066Sahrens 
1266fa9e4066Sahrens void
dnode_setdirty(dnode_t * dn,dmu_tx_t * tx)1267fa9e4066Sahrens dnode_setdirty(dnode_t *dn, dmu_tx_t *tx)
1268fa9e4066Sahrens {
1269503ad85cSMatthew Ahrens 	objset_t *os = dn->dn_objset;
1270fa9e4066Sahrens 	uint64_t txg = tx->tx_txg;
1271fa9e4066Sahrens 
127214843421SMatthew Ahrens 	if (DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
127314843421SMatthew Ahrens 		dsl_dataset_dirty(os->os_dsl_dataset, tx);
1274fa9e4066Sahrens 		return;
127514843421SMatthew Ahrens 	}
1276fa9e4066Sahrens 
12779c9dc39aSek110237 	DNODE_VERIFY(dn);
1278fa9e4066Sahrens 
1279fa9e4066Sahrens #ifdef ZFS_DEBUG
1280fa9e4066Sahrens 	mutex_enter(&dn->dn_mtx);
1281fa9e4066Sahrens 	ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg);
1282744947dcSTom Erickson 	ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg);
1283fa9e4066Sahrens 	mutex_exit(&dn->dn_mtx);
1284fa9e4066Sahrens #endif
1285fa9e4066Sahrens 
12860a586ceaSMark Shellenbaum 	/*
12870a586ceaSMark Shellenbaum 	 * Determine old uid/gid when necessary
12880a586ceaSMark Shellenbaum 	 */
128906e0070dSMark Shellenbaum 	dmu_objset_userquota_get_ids(dn, B_TRUE, tx);
12900a586ceaSMark Shellenbaum 
1291fa9e4066Sahrens 	mutex_enter(&os->os_lock);
1292fa9e4066Sahrens 
1293fa9e4066Sahrens 	/*
1294fa9e4066Sahrens 	 * If we are already marked dirty, we're done.
1295fa9e4066Sahrens 	 */
1296c543ec06Sahrens 	if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) {
1297fa9e4066Sahrens 		mutex_exit(&os->os_lock);
1298fa9e4066Sahrens 		return;
1299fa9e4066Sahrens 	}
1300fa9e4066Sahrens 
1301d080a709SAlex Reece 	ASSERT(!refcount_is_zero(&dn->dn_holds) ||
1302d080a709SAlex Reece 	    !avl_is_empty(&dn->dn_dbufs));
1303fa9e4066Sahrens 	ASSERT(dn->dn_datablksz != 0);
1304fb09f5aaSMadhav Suresh 	ASSERT0(dn->dn_next_bonuslen[txg&TXG_MASK]);
1305fb09f5aaSMadhav Suresh 	ASSERT0(dn->dn_next_blksz[txg&TXG_MASK]);
1306fb09f5aaSMadhav Suresh 	ASSERT0(dn->dn_next_bonustype[txg&TXG_MASK]);
1307fa9e4066Sahrens 
1308fa9e4066Sahrens 	dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
1309fa9e4066Sahrens 	    dn->dn_object, txg);
1310fa9e4066Sahrens 
1311fa9e4066Sahrens 	if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) {
1312fa9e4066Sahrens 		list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn);
1313fa9e4066Sahrens 	} else {
1314fa9e4066Sahrens 		list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn);
1315fa9e4066Sahrens 	}
1316fa9e4066Sahrens 
1317fa9e4066Sahrens 	mutex_exit(&os->os_lock);
1318fa9e4066Sahrens 
1319fa9e4066Sahrens 	/*
1320fa9e4066Sahrens 	 * The dnode maintains a hold on its containing dbuf as
1321fa9e4066Sahrens 	 * long as there are holds on it.  Each instantiated child
1322744947dcSTom Erickson 	 * dbuf maintains a hold on the dnode.  When the last child
1323fa9e4066Sahrens 	 * drops its hold, the dnode will drop its hold on the
1324fa9e4066Sahrens 	 * containing dbuf. We add a "dirty hold" here so that the
1325fa9e4066Sahrens 	 * dnode will hang around after we finish processing its
1326fa9e4066Sahrens 	 * children.
1327fa9e4066Sahrens 	 */
13281934e92fSmaybee 	VERIFY(dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg));
1329fa9e4066Sahrens 
1330c717a561Smaybee 	(void) dbuf_dirty(dn->dn_dbuf, tx);
1331fa9e4066Sahrens 
1332fa9e4066Sahrens 	dsl_dataset_dirty(os->os_dsl_dataset, tx);
1333fa9e4066Sahrens }
1334fa9e4066Sahrens 
1335fa9e4066Sahrens void
dnode_free(dnode_t * dn,dmu_tx_t * tx)1336fa9e4066Sahrens dnode_free(dnode_t *dn, dmu_tx_t *tx)
1337fa9e4066Sahrens {
1338c543ec06Sahrens 	int txgoff = tx->tx_txg & TXG_MASK;
1339c543ec06Sahrens 
1340fa9e4066Sahrens 	dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg);
1341fa9e4066Sahrens 
1342fa9e4066Sahrens 	/* we should be the only holder... hopefully */
1343fa9e4066Sahrens 	/* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */
1344fa9e4066Sahrens 
1345fa9e4066Sahrens 	mutex_enter(&dn->dn_mtx);
1346fa9e4066Sahrens 	if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) {
1347fa9e4066Sahrens 		mutex_exit(&dn->dn_mtx);
1348fa9e4066Sahrens 		return;
1349fa9e4066Sahrens 	}
1350fa9e4066Sahrens 	dn->dn_free_txg = tx->tx_txg;
1351fa9e4066Sahrens 	mutex_exit(&dn->dn_mtx);
1352fa9e4066Sahrens 
1353fa9e4066Sahrens 	/*
1354fa9e4066Sahrens 	 * If the dnode is already dirty, it needs to be moved from
1355fa9e4066Sahrens 	 * the dirty list to the free list.
1356fa9e4066Sahrens 	 */
1357fa9e4066Sahrens 	mutex_enter(&dn->dn_objset->os_lock);
1358c543ec06Sahrens 	if (list_link_active(&dn->dn_dirty_link[txgoff])) {
1359c543ec06Sahrens 		list_remove(&dn->dn_objset->os_dirty_dnodes[txgoff], dn);
1360c543ec06Sahrens 		list_insert_tail(&dn->dn_objset->os_free_dnodes[txgoff], dn);
1361fa9e4066Sahrens 		mutex_exit(&dn->dn_objset->os_lock);
1362fa9e4066Sahrens 	} else {
1363fa9e4066Sahrens 		mutex_exit(&dn->dn_objset->os_lock);
1364fa9e4066Sahrens 		dnode_setdirty(dn, tx);
1365fa9e4066Sahrens 	}
1366fa9e4066Sahrens }
1367fa9e4066Sahrens 
1368fa9e4066Sahrens /*
1369fa9e4066Sahrens  * Try to change the block size for the indicated dnode.  This can only
1370fa9e4066Sahrens  * succeed if there are no blocks allocated or dirty beyond first block
1371fa9e4066Sahrens  */
1372fa9e4066Sahrens int
dnode_set_blksz(dnode_t * dn,uint64_t size,int ibs,dmu_tx_t * tx)1373fa9e4066Sahrens dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx)
1374fa9e4066Sahrens {
1375d080a709SAlex Reece 	dmu_buf_impl_t *db;
1376cdb0ab79Smaybee 	int err;
1377fa9e4066Sahrens 
1378d1a98260SMatthew Ahrens 	ASSERT3U(size, <=, spa_maxblocksize(dmu_objset_spa(dn->dn_objset)));
1379fa9e4066Sahrens 	if (size == 0)
1380fa9e4066Sahrens 		size = SPA_MINBLOCKSIZE;
1381fa9e4066Sahrens 	else
1382fa9e4066Sahrens 		size = P2ROUNDUP(size, SPA_MINBLOCKSIZE);
1383fa9e4066Sahrens 
1384b143e04bSahrens 	if (ibs == dn->dn_indblkshift)
1385b143e04bSahrens 		ibs = 0;
1386fa9e4066Sahrens 
1387b143e04bSahrens 	if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0)
1388fa9e4066Sahrens 		return (0);
1389fa9e4066Sahrens 
1390fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1391fa9e4066Sahrens 
1392fa9e4066Sahrens 	/* Check for any allocated blocks beyond the first */
13930713e232SGeorge Wilson 	if (dn->dn_maxblkid != 0)
1394b143e04bSahrens 		goto fail;
1395fa9e4066Sahrens 
1396fa9e4066Sahrens 	mutex_enter(&dn->dn_dbufs_mtx);
1397d080a709SAlex Reece 	for (db = avl_first(&dn->dn_dbufs); db != NULL;
1398d080a709SAlex Reece 	    db = AVL_NEXT(&dn->dn_dbufs, db)) {
13990a586ceaSMark Shellenbaum 		if (db->db_blkid != 0 && db->db_blkid != DMU_BONUS_BLKID &&
14000a586ceaSMark Shellenbaum 		    db->db_blkid != DMU_SPILL_BLKID) {
1401fa9e4066Sahrens 			mutex_exit(&dn->dn_dbufs_mtx);
1402b143e04bSahrens 			goto fail;
1403fa9e4066Sahrens 		}
1404fa9e4066Sahrens 	}
1405fa9e4066Sahrens 	mutex_exit(&dn->dn_dbufs_mtx);
1406fa9e4066Sahrens 
1407b143e04bSahrens 	if (ibs && dn->dn_nlevels != 1)
1408b143e04bSahrens 		goto fail;
1409b143e04bSahrens 
1410cdb0ab79Smaybee 	/* resize the old block */
14115f9bb2f3SPaul Dagnelie 	err = dbuf_hold_impl(dn, 0, 0, TRUE, FALSE, FTAG, &db);
1412cdb0ab79Smaybee 	if (err == 0)
1413c543ec06Sahrens 		dbuf_new_size(db, size, tx);
1414cdb0ab79Smaybee 	else if (err != ENOENT)
1415cdb0ab79Smaybee 		goto fail;
1416c543ec06Sahrens 
1417fa9e4066Sahrens 	dnode_setdblksz(dn, size);
1418fa9e4066Sahrens 	dnode_setdirty(dn, tx);
1419c543ec06Sahrens 	dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size;
1420b143e04bSahrens 	if (ibs) {
1421b143e04bSahrens 		dn->dn_indblkshift = ibs;
1422fa9e4066Sahrens 		dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs;
1423b143e04bSahrens 	}
1424cdb0ab79Smaybee 	/* rele after we have fixed the blocksize in the dnode */
1425c543ec06Sahrens 	if (db)
1426ea8dc4b6Seschrock 		dbuf_rele(db, FTAG);
1427fa9e4066Sahrens 
1428fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
1429b143e04bSahrens 	return (0);
1430b143e04bSahrens 
1431b143e04bSahrens fail:
1432b143e04bSahrens 	rw_exit(&dn->dn_struct_rwlock);
1433be6fd75aSMatthew Ahrens 	return (SET_ERROR(ENOTSUP));
1434fa9e4066Sahrens }
1435fa9e4066Sahrens 
14368346f03fSJonathan W Adams /* read-holding callers must not rely on the lock being continuously held */
1437fa9e4066Sahrens void
dnode_new_blkid(dnode_t * dn,uint64_t blkid,dmu_tx_t * tx,boolean_t have_read)14388346f03fSJonathan W Adams dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t have_read)
1439fa9e4066Sahrens {
1440fa9e4066Sahrens 	uint64_t txgoff = tx->tx_txg & TXG_MASK;
1441c543ec06Sahrens 	int epbs, new_nlevels;
1442fa9e4066Sahrens 	uint64_t sz;
1443fa9e4066Sahrens 
14440a586ceaSMark Shellenbaum 	ASSERT(blkid != DMU_BONUS_BLKID);
1445fa9e4066Sahrens 
14468346f03fSJonathan W Adams 	ASSERT(have_read ?
14478346f03fSJonathan W Adams 	    RW_READ_HELD(&dn->dn_struct_rwlock) :
14488346f03fSJonathan W Adams 	    RW_WRITE_HELD(&dn->dn_struct_rwlock));
14498346f03fSJonathan W Adams 
14508346f03fSJonathan W Adams 	/*
14518346f03fSJonathan W Adams 	 * if we have a read-lock, check to see if we need to do any work
14528346f03fSJonathan W Adams 	 * before upgrading to a write-lock.
14538346f03fSJonathan W Adams 	 */
14548346f03fSJonathan W Adams 	if (have_read) {
14558346f03fSJonathan W Adams 		if (blkid <= dn->dn_maxblkid)
14568346f03fSJonathan W Adams 			return;
14578346f03fSJonathan W Adams 
14588346f03fSJonathan W Adams 		if (!rw_tryupgrade(&dn->dn_struct_rwlock)) {
14598346f03fSJonathan W Adams 			rw_exit(&dn->dn_struct_rwlock);
1460fa9e4066Sahrens 			rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
14618346f03fSJonathan W Adams 		}
1462fa9e4066Sahrens 	}
1463fa9e4066Sahrens 
1464c543ec06Sahrens 	if (blkid <= dn->dn_maxblkid)
1465c543ec06Sahrens 		goto out;
1466c543ec06Sahrens 
1467fa9e4066Sahrens 	dn->dn_maxblkid = blkid;
1468fa9e4066Sahrens 
1469fa9e4066Sahrens 	/*
1470c543ec06Sahrens 	 * Compute the number of levels necessary to support the new maxblkid.
1471fa9e4066Sahrens 	 */
1472fa9e4066Sahrens 	new_nlevels = 1;
1473fa9e4066Sahrens 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1474c543ec06Sahrens 	for (sz = dn->dn_nblkptr;
1475c543ec06Sahrens 	    sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs)
1476fa9e4066Sahrens 		new_nlevels++;
1477fa9e4066Sahrens 
1478c543ec06Sahrens 	if (new_nlevels > dn->dn_nlevels) {
1479c543ec06Sahrens 		int old_nlevels = dn->dn_nlevels;
1480c543ec06Sahrens 		dmu_buf_impl_t *db;
1481c717a561Smaybee 		list_t *list;
1482c717a561Smaybee 		dbuf_dirty_record_t *new, *dr, *dr_next;
1483c543ec06Sahrens 
1484c543ec06Sahrens 		dn->dn_nlevels = new_nlevels;
1485c543ec06Sahrens 
1486c543ec06Sahrens 		ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]);
1487fa9e4066Sahrens 		dn->dn_next_nlevels[txgoff] = new_nlevels;
1488fa9e4066Sahrens 
1489c717a561Smaybee 		/* dirty the left indirects */
1490c543ec06Sahrens 		db = dbuf_hold_level(dn, old_nlevels, 0, FTAG);
149101025c89SJohn Harres 		ASSERT(db != NULL);
1492c717a561Smaybee 		new = dbuf_dirty(db, tx);
1493ea8dc4b6Seschrock 		dbuf_rele(db, FTAG);
1494fa9e4066Sahrens 
1495c717a561Smaybee 		/* transfer the dirty records to the new indirect */
1496c717a561Smaybee 		mutex_enter(&dn->dn_mtx);
1497c717a561Smaybee 		mutex_enter(&new->dt.di.dr_mtx);
1498c717a561Smaybee 		list = &dn->dn_dirty_records[txgoff];
1499c717a561Smaybee 		for (dr = list_head(list); dr; dr = dr_next) {
1500c717a561Smaybee 			dr_next = list_next(&dn->dn_dirty_records[txgoff], dr);
1501c717a561Smaybee 			if (dr->dr_dbuf->db_level != new_nlevels-1 &&
15020a586ceaSMark Shellenbaum 			    dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
15030a586ceaSMark Shellenbaum 			    dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
1504c717a561Smaybee 				ASSERT(dr->dr_dbuf->db_level == old_nlevels-1);
1505c717a561Smaybee 				list_remove(&dn->dn_dirty_records[txgoff], dr);
1506c717a561Smaybee 				list_insert_tail(&new->dt.di.dr_children, dr);
1507c717a561Smaybee 				dr->dr_parent = new;
1508c717a561Smaybee 			}
1509c717a561Smaybee 		}
1510c717a561Smaybee 		mutex_exit(&new->dt.di.dr_mtx);
1511c717a561Smaybee 		mutex_exit(&dn->dn_mtx);
1512fa9e4066Sahrens 	}
1513fa9e4066Sahrens 
1514fa9e4066Sahrens out:
15158346f03fSJonathan W Adams 	if (have_read)
15168346f03fSJonathan W Adams 		rw_downgrade(&dn->dn_struct_rwlock);
1517fa9e4066Sahrens }
1518fa9e4066Sahrens 
1519238fb8bbSMatthew Ahrens static void
dnode_dirty_l1(dnode_t * dn,uint64_t l1blkid,dmu_tx_t * tx)1520238fb8bbSMatthew Ahrens dnode_dirty_l1(dnode_t *dn, uint64_t l1blkid, dmu_tx_t *tx)
1521238fb8bbSMatthew Ahrens {
1522238fb8bbSMatthew Ahrens 	dmu_buf_impl_t *db = dbuf_hold_level(dn, 1, l1blkid, FTAG);
1523238fb8bbSMatthew Ahrens 	if (db != NULL) {
1524238fb8bbSMatthew Ahrens 		dmu_buf_will_dirty(&db->db, tx);
1525238fb8bbSMatthew Ahrens 		dbuf_rele(db, FTAG);
1526238fb8bbSMatthew Ahrens 	}
1527238fb8bbSMatthew Ahrens }
1528238fb8bbSMatthew Ahrens 
1529fa9e4066Sahrens void
dnode_free_range(dnode_t * dn,uint64_t off,uint64_t len,dmu_tx_t * tx)1530fa9e4066Sahrens dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx)
1531fa9e4066Sahrens {
1532fa9e4066Sahrens 	dmu_buf_impl_t *db;
1533b143e04bSahrens 	uint64_t blkoff, blkid, nblks;
1534cdb0ab79Smaybee 	int blksz, blkshift, head, tail;
1535fa9e4066Sahrens 	int trunc = FALSE;
1536cdb0ab79Smaybee 	int epbs;
1537fa9e4066Sahrens 
1538fa9e4066Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1539fa9e4066Sahrens 	blksz = dn->dn_datablksz;
1540cdb0ab79Smaybee 	blkshift = dn->dn_datablkshift;
1541cdb0ab79Smaybee 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1542fa9e4066Sahrens 
1543713d6c20SMatthew Ahrens 	if (len == DMU_OBJECT_END) {
1544fa9e4066Sahrens 		len = UINT64_MAX - off;
1545fa9e4066Sahrens 		trunc = TRUE;
1546fa9e4066Sahrens 	}
1547fa9e4066Sahrens 
1548fa9e4066Sahrens 	/*
1549fa9e4066Sahrens 	 * First, block align the region to free:
1550fa9e4066Sahrens 	 */
1551b143e04bSahrens 	if (ISP2(blksz)) {
1552b143e04bSahrens 		head = P2NPHASE(off, blksz);
1553b143e04bSahrens 		blkoff = P2PHASE(off, blksz);
1554cdb0ab79Smaybee 		if ((off >> blkshift) > dn->dn_maxblkid)
1555cdb0ab79Smaybee 			goto out;
1556b143e04bSahrens 	} else {
1557b143e04bSahrens 		ASSERT(dn->dn_maxblkid == 0);
1558b143e04bSahrens 		if (off == 0 && len >= blksz) {
155949064978SMax Grossman 			/*
156049064978SMax Grossman 			 * Freeing the whole block; fast-track this request.
156149064978SMax Grossman 			 * Note that we won't dirty any indirect blocks,
156249064978SMax Grossman 			 * which is fine because we will be freeing the entire
156349064978SMax Grossman 			 * file and thus all indirect blocks will be freed
156449064978SMax Grossman 			 * by free_children().
156549064978SMax Grossman 			 */
1566cdb0ab79Smaybee 			blkid = 0;
1567cdb0ab79Smaybee 			nblks = 1;
1568cdb0ab79Smaybee 			goto done;
15691c8564a7SMark Maybee 		} else if (off >= blksz) {
1570cdb0ab79Smaybee 			/* Freeing past end-of-data */
1571cdb0ab79Smaybee 			goto out;
1572fa9e4066Sahrens 		} else {
1573b143e04bSahrens 			/* Freeing part of the block. */
1574fa9e4066Sahrens 			head = blksz - off;
1575fa9e4066Sahrens 			ASSERT3U(head, >, 0);
1576fa9e4066Sahrens 		}
1577b143e04bSahrens 		blkoff = off;
1578fa9e4066Sahrens 	}
1579fa9e4066Sahrens 	/* zero out any partial block data at the start of the range */
1580fa9e4066Sahrens 	if (head) {
1581b143e04bSahrens 		ASSERT3U(blkoff + head, ==, blksz);
1582fa9e4066Sahrens 		if (len < head)
1583fa9e4066Sahrens 			head = len;
15845f9bb2f3SPaul Dagnelie 		if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, 0, off),
15855f9bb2f3SPaul Dagnelie 		    TRUE, FALSE, FTAG, &db) == 0) {
1586fa9e4066Sahrens 			caddr_t data;
1587fa9e4066Sahrens 
1588fa9e4066Sahrens 			/* don't dirty if it isn't on disk and isn't dirty */
1589c717a561Smaybee 			if (db->db_last_dirty ||
1590fa9e4066Sahrens 			    (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1591fa9e4066Sahrens 				rw_exit(&dn->dn_struct_rwlock);
159249064978SMax Grossman 				dmu_buf_will_dirty(&db->db, tx);
1593fa9e4066Sahrens 				rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1594fa9e4066Sahrens 				data = db->db.db_data;
1595b143e04bSahrens 				bzero(data + blkoff, head);
1596fa9e4066Sahrens 			}
1597ea8dc4b6Seschrock 			dbuf_rele(db, FTAG);
1598fa9e4066Sahrens 		}
1599fa9e4066Sahrens 		off += head;
1600fa9e4066Sahrens 		len -= head;
1601fa9e4066Sahrens 	}
1602b143e04bSahrens 
1603b143e04bSahrens 	/* If the range was less than one block, we're done */
1604cdb0ab79Smaybee 	if (len == 0)
1605fa9e4066Sahrens 		goto out;
1606fa9e4066Sahrens 
1607b143e04bSahrens 	/* If the remaining range is past end of file, we're done */
1608cdb0ab79Smaybee 	if ((off >> blkshift) > dn->dn_maxblkid)
1609fa9e4066Sahrens 		goto out;
1610fa9e4066Sahrens 
16111c8564a7SMark Maybee 	ASSERT(ISP2(blksz));
1612cdb0ab79Smaybee 	if (trunc)
1613fa9e4066Sahrens 		tail = 0;
1614fa9e4066Sahrens 	else
1615fa9e4066Sahrens 		tail = P2PHASE(len, blksz);
1616fa9e4066Sahrens 
1617fb09f5aaSMadhav Suresh 	ASSERT0(P2PHASE(off, blksz));
1618fa9e4066Sahrens 	/* zero out any partial block data at the end of the range */
1619fa9e4066Sahrens 	if (tail) {
1620fa9e4066Sahrens 		if (len < tail)
1621fa9e4066Sahrens 			tail = len;
16225f9bb2f3SPaul Dagnelie 		if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, 0, off+len),
16235f9bb2f3SPaul Dagnelie 		    TRUE, FALSE, FTAG, &db) == 0) {
1624b143e04bSahrens 			/* don't dirty if not on disk and not dirty */
1625c717a561Smaybee 			if (db->db_last_dirty ||
1626cdb0ab79Smaybee 			    (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1627fa9e4066Sahrens 				rw_exit(&dn->dn_struct_rwlock);
162849064978SMax Grossman 				dmu_buf_will_dirty(&db->db, tx);
1629cdb0ab79Smaybee 				rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1630fa9e4066Sahrens 				bzero(db->db.db_data, tail);
1631fa9e4066Sahrens 			}
1632ea8dc4b6Seschrock 			dbuf_rele(db, FTAG);
1633fa9e4066Sahrens 		}
1634fa9e4066Sahrens 		len -= tail;
1635fa9e4066Sahrens 	}
1636cdb0ab79Smaybee 
1637fa9e4066Sahrens 	/* If the range did not include a full block, we are done */
1638fa9e4066Sahrens 	if (len == 0)
1639fa9e4066Sahrens 		goto out;
1640fa9e4066Sahrens 
1641cdb0ab79Smaybee 	ASSERT(IS_P2ALIGNED(off, blksz));
1642cdb0ab79Smaybee 	ASSERT(trunc || IS_P2ALIGNED(len, blksz));
1643fa9e4066Sahrens 	blkid = off >> blkshift;
1644fa9e4066Sahrens 	nblks = len >> blkshift;
1645fa9e4066Sahrens 	if (trunc)
1646cdb0ab79Smaybee 		nblks += 1;
1647fa9e4066Sahrens 
1648cdb0ab79Smaybee 	/*
1649238fb8bbSMatthew Ahrens 	 * Dirty all the indirect blocks in this range.  Note that only
1650238fb8bbSMatthew Ahrens 	 * the first and last indirect blocks can actually be written
1651238fb8bbSMatthew Ahrens 	 * (if they were partially freed) -- they must be dirtied, even if
1652238fb8bbSMatthew Ahrens 	 * they do not exist on disk yet.  The interior blocks will
1653238fb8bbSMatthew Ahrens 	 * be freed by free_children(), so they will not actually be written.
1654238fb8bbSMatthew Ahrens 	 * Even though these interior blocks will not be written, we
1655238fb8bbSMatthew Ahrens 	 * dirty them for two reasons:
1656238fb8bbSMatthew Ahrens 	 *
1657238fb8bbSMatthew Ahrens 	 *  - It ensures that the indirect blocks remain in memory until
1658238fb8bbSMatthew Ahrens 	 *    syncing context.  (They have already been prefetched by
1659238fb8bbSMatthew Ahrens 	 *    dmu_tx_hold_free(), so we don't have to worry about reading
1660238fb8bbSMatthew Ahrens 	 *    them serially here.)
1661238fb8bbSMatthew Ahrens 	 *
1662238fb8bbSMatthew Ahrens 	 *  - The dirty space accounting will put pressure on the txg sync
1663238fb8bbSMatthew Ahrens 	 *    mechanism to begin syncing, and to delay transactions if there
1664238fb8bbSMatthew Ahrens 	 *    is a large amount of freeing.  Even though these indirect
1665238fb8bbSMatthew Ahrens 	 *    blocks will not be written, we could need to write the same
1666238fb8bbSMatthew Ahrens 	 *    amount of space if we copy the freed BPs into deadlists.
1667cdb0ab79Smaybee 	 */
1668cdb0ab79Smaybee 	if (dn->dn_nlevels > 1) {
166949064978SMax Grossman 		uint64_t first, last;
1670cdb0ab79Smaybee 
1671cdb0ab79Smaybee 		first = blkid >> epbs;
1672238fb8bbSMatthew Ahrens 		dnode_dirty_l1(dn, first, tx);
1673cdb0ab79Smaybee 		if (trunc)
1674cdb0ab79Smaybee 			last = dn->dn_maxblkid >> epbs;
1675cdb0ab79Smaybee 		else
1676cdb0ab79Smaybee 			last = (blkid + nblks - 1) >> epbs;
1677238fb8bbSMatthew Ahrens 		if (last != first)
1678238fb8bbSMatthew Ahrens 			dnode_dirty_l1(dn, last, tx);
1679238fb8bbSMatthew Ahrens 
1680238fb8bbSMatthew Ahrens 		int shift = dn->dn_datablkshift + dn->dn_indblkshift -
1681238fb8bbSMatthew Ahrens 		    SPA_BLKPTRSHIFT;
1682238fb8bbSMatthew Ahrens 		for (uint64_t i = first + 1; i < last; i++) {
1683238fb8bbSMatthew Ahrens 			/*
1684238fb8bbSMatthew Ahrens 			 * Set i to the blockid of the next non-hole
1685238fb8bbSMatthew Ahrens 			 * level-1 indirect block at or after i.  Note
1686238fb8bbSMatthew Ahrens 			 * that dnode_next_offset() operates in terms of
1687238fb8bbSMatthew Ahrens 			 * level-0-equivalent bytes.
1688238fb8bbSMatthew Ahrens 			 */
1689238fb8bbSMatthew Ahrens 			uint64_t ibyte = i << shift;
1690238fb8bbSMatthew Ahrens 			int err = dnode_next_offset(dn, DNODE_FIND_HAVELOCK,
1691238fb8bbSMatthew Ahrens 			    &ibyte, 2, 1, 0);
1692238fb8bbSMatthew Ahrens 			i = ibyte >> shift;
1693238fb8bbSMatthew Ahrens 			if (i >= last)
1694238fb8bbSMatthew Ahrens 				break;
1695238fb8bbSMatthew Ahrens 
1696238fb8bbSMatthew Ahrens 			/*
1697238fb8bbSMatthew Ahrens 			 * Normally we should not see an error, either
1698238fb8bbSMatthew Ahrens 			 * from dnode_next_offset() or dbuf_hold_level()
1699238fb8bbSMatthew Ahrens 			 * (except for ESRCH from dnode_next_offset).
1700238fb8bbSMatthew Ahrens 			 * If there is an i/o error, then when we read
1701238fb8bbSMatthew Ahrens 			 * this block in syncing context, it will use
1702238fb8bbSMatthew Ahrens 			 * ZIO_FLAG_MUSTSUCCEED, and thus hang/panic according
1703238fb8bbSMatthew Ahrens 			 * to the "failmode" property.  dnode_next_offset()
1704238fb8bbSMatthew Ahrens 			 * doesn't have a flag to indicate MUSTSUCCEED.
1705238fb8bbSMatthew Ahrens 			 */
1706238fb8bbSMatthew Ahrens 			if (err != 0)
1707238fb8bbSMatthew Ahrens 				break;
1708238fb8bbSMatthew Ahrens 
1709238fb8bbSMatthew Ahrens 			dnode_dirty_l1(dn, i, tx);
171056d55a53Smaybee 		}
171149064978SMax Grossman 	}
1712cdb0ab79Smaybee 
1713cdb0ab79Smaybee done:
1714cdb0ab79Smaybee 	/*
1715cdb0ab79Smaybee 	 * Add this range to the dnode range list.
1716cdb0ab79Smaybee 	 * We will finish up this free operation in the syncing phase.
1717cdb0ab79Smaybee 	 */
1718fa9e4066Sahrens 	mutex_enter(&dn->dn_mtx);
1719887cdfc7SMatthew Ahrens 	int txgoff = tx->tx_txg & TXG_MASK;
1720887cdfc7SMatthew Ahrens 	if (dn->dn_free_ranges[txgoff] == NULL) {
1721887cdfc7SMatthew Ahrens 		dn->dn_free_ranges[txgoff] =
1722887cdfc7SMatthew Ahrens 		    range_tree_create(NULL, NULL, &dn->dn_mtx);
1723887cdfc7SMatthew Ahrens 	}
1724887cdfc7SMatthew Ahrens 	range_tree_clear(dn->dn_free_ranges[txgoff], blkid, nblks);
1725887cdfc7SMatthew Ahrens 	range_tree_add(dn->dn_free_ranges[txgoff], blkid, nblks);
1726fa9e4066Sahrens 	dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1727fa9e4066Sahrens 	    blkid, nblks, tx->tx_txg);
1728fa9e4066Sahrens 	mutex_exit(&dn->dn_mtx);
1729fa9e4066Sahrens 
1730cdb0ab79Smaybee 	dbuf_free_range(dn, blkid, blkid + nblks - 1, tx);
1731fa9e4066Sahrens 	dnode_setdirty(dn, tx);
1732fa9e4066Sahrens out:
1733cdb0ab79Smaybee 
1734fa9e4066Sahrens 	rw_exit(&dn->dn_struct_rwlock);
1735fa9e4066Sahrens }
1736fa9e4066Sahrens 
17370a586ceaSMark Shellenbaum static boolean_t
dnode_spill_freed(dnode_t * dn)17380a586ceaSMark Shellenbaum dnode_spill_freed(dnode_t *dn)
17390a586ceaSMark Shellenbaum {
17400a586ceaSMark Shellenbaum 	int i;
17410a586ceaSMark Shellenbaum 
17420a586ceaSMark Shellenbaum 	mutex_enter(&dn->dn_mtx);
17430a586ceaSMark Shellenbaum 	for (i = 0; i < TXG_SIZE; i++) {
17440a586ceaSMark Shellenbaum 		if (dn->dn_rm_spillblk[i] == DN_KILL_SPILLBLK)
17450a586ceaSMark Shellenbaum 			break;
17460a586ceaSMark Shellenbaum 	}
17470a586ceaSMark Shellenbaum 	mutex_exit(&dn->dn_mtx);
17480a586ceaSMark Shellenbaum 	return (i < TXG_SIZE);
17490a586ceaSMark Shellenbaum }
17500a586ceaSMark Shellenbaum 
1751fa9e4066Sahrens /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
1752fa9e4066Sahrens uint64_t
dnode_block_freed(dnode_t * dn,uint64_t blkid)1753fa9e4066Sahrens dnode_block_freed(dnode_t *dn, uint64_t blkid)
1754fa9e4066Sahrens {
1755fa9e4066Sahrens 	void *dp = spa_get_dsl(dn->dn_objset->os_spa);
1756fa9e4066Sahrens 	int i;
1757fa9e4066Sahrens 
17580a586ceaSMark Shellenbaum 	if (blkid == DMU_BONUS_BLKID)
1759fa9e4066Sahrens 		return (FALSE);
1760fa9e4066Sahrens 
1761fa9e4066Sahrens 	/*
1762fa9e4066Sahrens 	 * If we're in the process of opening the pool, dp will not be
1763fa9e4066Sahrens 	 * set yet, but there shouldn't be anything dirty.
1764fa9e4066Sahrens 	 */
1765fa9e4066Sahrens 	if (dp == NULL)
1766fa9e4066Sahrens 		return (FALSE);
1767fa9e4066Sahrens 
1768fa9e4066Sahrens 	if (dn->dn_free_txg)
1769fa9e4066Sahrens 		return (TRUE);
1770fa9e4066Sahrens 
17710a586ceaSMark Shellenbaum 	if (blkid == DMU_SPILL_BLKID)
17720a586ceaSMark Shellenbaum 		return (dnode_spill_freed(dn));
17730a586ceaSMark Shellenbaum 
1774fa9e4066Sahrens 	mutex_enter(&dn->dn_mtx);
1775fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
1776887cdfc7SMatthew Ahrens 		if (dn->dn_free_ranges[i] != NULL &&
1777887cdfc7SMatthew Ahrens 		    range_tree_contains(dn->dn_free_ranges[i], blkid, 1))
1778fa9e4066Sahrens 			break;
1779fa9e4066Sahrens 	}
1780fa9e4066Sahrens 	mutex_exit(&dn->dn_mtx);
1781fa9e4066Sahrens 	return (i < TXG_SIZE);
1782fa9e4066Sahrens }
1783fa9e4066Sahrens 
1784fa9e4066Sahrens /* call from syncing context when we actually write/free space for this dnode */
1785fa9e4066Sahrens void
dnode_diduse_space(dnode_t * dn,int64_t delta)178699653d4eSeschrock dnode_diduse_space(dnode_t *dn, int64_t delta)
1787fa9e4066Sahrens {
178899653d4eSeschrock 	uint64_t space;
178999653d4eSeschrock 	dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n",
1790fa9e4066Sahrens 	    dn, dn->dn_phys,
179199653d4eSeschrock 	    (u_longlong_t)dn->dn_phys->dn_used,
179299653d4eSeschrock 	    (longlong_t)delta);
1793fa9e4066Sahrens 
1794fa9e4066Sahrens 	mutex_enter(&dn->dn_mtx);
179599653d4eSeschrock 	space = DN_USED_BYTES(dn->dn_phys);
179699653d4eSeschrock 	if (delta > 0) {
179799653d4eSeschrock 		ASSERT3U(space + delta, >=, space); /* no overflow */
1798fa9e4066Sahrens 	} else {
179999653d4eSeschrock 		ASSERT3U(space, >=, -delta); /* no underflow */
180099653d4eSeschrock 	}
180199653d4eSeschrock 	space += delta;
1802e7437265Sahrens 	if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_DNODE_BYTES) {
180399653d4eSeschrock 		ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0);
1804fb09f5aaSMadhav Suresh 		ASSERT0(P2PHASE(space, 1<<DEV_BSHIFT));
180599653d4eSeschrock 		dn->dn_phys->dn_used = space >> DEV_BSHIFT;
180699653d4eSeschrock 	} else {
180799653d4eSeschrock 		dn->dn_phys->dn_used = space;
180899653d4eSeschrock 		dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES;
1809fa9e4066Sahrens 	}
1810fa9e4066Sahrens 	mutex_exit(&dn->dn_mtx);
1811fa9e4066Sahrens }
1812fa9e4066Sahrens 
1813fa9e4066Sahrens /*
181469962b56SMatthew Ahrens  * Call when we think we're going to write/free space in open context to track
181569962b56SMatthew Ahrens  * the amount of memory in use by the currently open txg.
1816fa9e4066Sahrens  */
1817fa9e4066Sahrens void
dnode_willuse_space(dnode_t * dn,int64_t space,dmu_tx_t * tx)1818fa9e4066Sahrens dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx)
1819fa9e4066Sahrens {
1820503ad85cSMatthew Ahrens 	objset_t *os = dn->dn_objset;
1821fa9e4066Sahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
182269962b56SMatthew Ahrens 	int64_t aspace = spa_get_asize(os->os_spa, space);
1823fa9e4066Sahrens 
182469962b56SMatthew Ahrens 	if (ds != NULL) {
182569962b56SMatthew Ahrens 		dsl_dir_willuse_space(ds->ds_dir, aspace, tx);
182669962b56SMatthew Ahrens 		dsl_pool_dirty_space(dmu_tx_pool(tx), space, tx);
182769962b56SMatthew Ahrens 	}
1828fa9e4066Sahrens 
182969962b56SMatthew Ahrens 	dmu_tx_willuse_space(tx, aspace);
1830fa9e4066Sahrens }
1831fa9e4066Sahrens 
183276256205SMark Maybee /*
1833f7170741SWill Andrews  * Scans a block at the indicated "level" looking for a hole or data,
1834f7170741SWill Andrews  * depending on 'flags'.
1835f7170741SWill Andrews  *
1836f7170741SWill Andrews  * If level > 0, then we are scanning an indirect block looking at its
1837f7170741SWill Andrews  * pointers.  If level == 0, then we are looking at a block of dnodes.
1838f7170741SWill Andrews  *
1839f7170741SWill Andrews  * If we don't find what we are looking for in the block, we return ESRCH.
1840f7170741SWill Andrews  * Otherwise, return with *offset pointing to the beginning (if searching
1841f7170741SWill Andrews  * forwards) or end (if searching backwards) of the range covered by the
1842f7170741SWill Andrews  * block pointer we matched on (or dnode).
184376256205SMark Maybee  *
184476256205SMark Maybee  * The basic search algorithm used below by dnode_next_offset() is to
184576256205SMark Maybee  * use this function to search up the block tree (widen the search) until
184676256205SMark Maybee  * we find something (i.e., we don't return ESRCH) and then search back
184776256205SMark Maybee  * down the tree (narrow the search) until we reach our original search
184876256205SMark Maybee  * level.
184976256205SMark Maybee  */
1850fa9e4066Sahrens static int
dnode_next_offset_level(dnode_t * dn,int flags,uint64_t * offset,int lvl,uint64_t blkfill,uint64_t txg)1851cdb0ab79Smaybee dnode_next_offset_level(dnode_t *dn, int flags, uint64_t *offset,
18526754306eSahrens     int lvl, uint64_t blkfill, uint64_t txg)
1853fa9e4066Sahrens {
1854fa9e4066Sahrens 	dmu_buf_impl_t *db = NULL;
1855fa9e4066Sahrens 	void *data = NULL;
1856fa9e4066Sahrens 	uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
1857fa9e4066Sahrens 	uint64_t epb = 1ULL << epbs;
1858fa9e4066Sahrens 	uint64_t minfill, maxfill;
1859cdb0ab79Smaybee 	boolean_t hole;
1860cdb0ab79Smaybee 	int i, inc, error, span;
1861fa9e4066Sahrens 
1862fa9e4066Sahrens 	dprintf("probing object %llu offset %llx level %d of %u\n",
1863fa9e4066Sahrens 	    dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels);
1864fa9e4066Sahrens 
186514843421SMatthew Ahrens 	hole = ((flags & DNODE_FIND_HOLE) != 0);
1866cdb0ab79Smaybee 	inc = (flags & DNODE_FIND_BACKWARDS) ? -1 : 1;
18671c8564a7SMark Maybee 	ASSERT(txg == 0 || !hole);
1868cdb0ab79Smaybee 
1869fa9e4066Sahrens 	if (lvl == dn->dn_phys->dn_nlevels) {
1870fa9e4066Sahrens 		error = 0;
1871fa9e4066Sahrens 		epb = dn->dn_phys->dn_nblkptr;
1872fa9e4066Sahrens 		data = dn->dn_phys->dn_blkptr;
1873fa9e4066Sahrens 	} else {
18745f9bb2f3SPaul Dagnelie 		uint64_t blkid = dbuf_whichblock(dn, lvl, *offset);
18755f9bb2f3SPaul Dagnelie 		error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FALSE, FTAG, &db);
1876fa9e4066Sahrens 		if (error) {
18771c8564a7SMark Maybee 			if (error != ENOENT)
1878fa9e4066Sahrens 				return (error);
18791c8564a7SMark Maybee 			if (hole)
18801c8564a7SMark Maybee 				return (0);
18811c8564a7SMark Maybee 			/*
18821c8564a7SMark Maybee 			 * This can only happen when we are searching up
18831c8564a7SMark Maybee 			 * the block tree for data.  We don't really need to
18841c8564a7SMark Maybee 			 * adjust the offset, as we will just end up looking
18851c8564a7SMark Maybee 			 * at the pointer to this block in its parent, and its
18861c8564a7SMark Maybee 			 * going to be unallocated, so we will skip over it.
18871c8564a7SMark Maybee 			 */
1888be6fd75aSMatthew Ahrens 			return (SET_ERROR(ESRCH));
1889fa9e4066Sahrens 		}
189098572ac1Sahrens 		error = dbuf_read(db, NULL, DB_RF_CANFAIL | DB_RF_HAVESTRUCT);
189198572ac1Sahrens 		if (error) {
189298572ac1Sahrens 			dbuf_rele(db, FTAG);
189398572ac1Sahrens 			return (error);
189498572ac1Sahrens 		}
1895fa9e4066Sahrens 		data = db->db.db_data;
1896fa9e4066Sahrens 	}
1897fa9e4066Sahrens 
189849064978SMax Grossman 
189949064978SMax Grossman 	if (db != NULL && txg != 0 && (db->db_blkptr == NULL ||
190049064978SMax Grossman 	    db->db_blkptr->blk_birth <= txg ||
190149064978SMax Grossman 	    BP_IS_HOLE(db->db_blkptr))) {
19021c8564a7SMark Maybee 		/*
19031c8564a7SMark Maybee 		 * This can only happen when we are searching up the tree
19041c8564a7SMark Maybee 		 * and these conditions mean that we need to keep climbing.
19051c8564a7SMark Maybee 		 */
1906be6fd75aSMatthew Ahrens 		error = SET_ERROR(ESRCH);
19076754306eSahrens 	} else if (lvl == 0) {
1908fa9e4066Sahrens 		dnode_phys_t *dnp = data;
1909fa9e4066Sahrens 		span = DNODE_SHIFT;
1910fa9e4066Sahrens 		ASSERT(dn->dn_type == DMU_OT_DNODE);
1911fa9e4066Sahrens 
1912cdb0ab79Smaybee 		for (i = (*offset >> span) & (blkfill - 1);
1913cdb0ab79Smaybee 		    i >= 0 && i < blkfill; i += inc) {
191408f3f137SJonathan W Adams 			if ((dnp[i].dn_type == DMU_OT_NONE) == hole)
1915fa9e4066Sahrens 				break;
1916cdb0ab79Smaybee 			*offset += (1ULL << span) * inc;
1917fa9e4066Sahrens 		}
1918cdb0ab79Smaybee 		if (i < 0 || i == blkfill)
1919be6fd75aSMatthew Ahrens 			error = SET_ERROR(ESRCH);
1920fa9e4066Sahrens 	} else {
1921fa9e4066Sahrens 		blkptr_t *bp = data;
192276256205SMark Maybee 		uint64_t start = *offset;
1923fa9e4066Sahrens 		span = (lvl - 1) * epbs + dn->dn_datablkshift;
1924fa9e4066Sahrens 		minfill = 0;
1925fa9e4066Sahrens 		maxfill = blkfill << ((lvl - 1) * epbs);
1926fa9e4066Sahrens 
1927fa9e4066Sahrens 		if (hole)
1928fa9e4066Sahrens 			maxfill--;
1929fa9e4066Sahrens 		else
1930fa9e4066Sahrens 			minfill++;
1931fa9e4066Sahrens 
193276256205SMark Maybee 		*offset = *offset >> span;
193376256205SMark Maybee 		for (i = BF64_GET(*offset, 0, epbs);
1934cdb0ab79Smaybee 		    i >= 0 && i < epb; i += inc) {
1935e54e0be9SMatthew Ahrens 			if (BP_GET_FILL(&bp[i]) >= minfill &&
1936e54e0be9SMatthew Ahrens 			    BP_GET_FILL(&bp[i]) <= maxfill &&
19371c8564a7SMark Maybee 			    (hole || bp[i].blk_birth > txg))
1938fa9e4066Sahrens 				break;
193976256205SMark Maybee 			if (inc > 0 || *offset > 0)
194076256205SMark Maybee 				*offset += inc;
1941fa9e4066Sahrens 		}
194276256205SMark Maybee 		*offset = *offset << span;
194376256205SMark Maybee 		if (inc < 0) {
194476256205SMark Maybee 			/* traversing backwards; position offset at the end */
194576256205SMark Maybee 			ASSERT3U(*offset, <=, start);
194676256205SMark Maybee 			*offset = MIN(*offset + (1ULL << span) - 1, start);
194776256205SMark Maybee 		} else if (*offset < start) {
194876256205SMark Maybee 			*offset = start;
194976256205SMark Maybee 		}
195076256205SMark Maybee 		if (i < 0 || i >= epb)
1951be6fd75aSMatthew Ahrens 			error = SET_ERROR(ESRCH);
1952fa9e4066Sahrens 	}
1953fa9e4066Sahrens 
1954fa9e4066Sahrens 	if (db)
1955ea8dc4b6Seschrock 		dbuf_rele(db, FTAG);
1956fa9e4066Sahrens 
1957fa9e4066Sahrens 	return (error);
1958fa9e4066Sahrens }
1959fa9e4066Sahrens 
1960fa9e4066Sahrens /*
1961fa9e4066Sahrens  * Find the next hole, data, or sparse region at or after *offset.
1962fa9e4066Sahrens  * The value 'blkfill' tells us how many items we expect to find
1963fa9e4066Sahrens  * in an L0 data block; this value is 1 for normal objects,
1964fa9e4066Sahrens  * DNODES_PER_BLOCK for the meta dnode, and some fraction of
1965fa9e4066Sahrens  * DNODES_PER_BLOCK when searching for sparse regions thereof.
19666754306eSahrens  *
1967fa9e4066Sahrens  * Examples:
1968fa9e4066Sahrens  *
1969cdb0ab79Smaybee  * dnode_next_offset(dn, flags, offset, 1, 1, 0);
1970cdb0ab79Smaybee  *	Finds the next/previous hole/data in a file.
1971fa9e4066Sahrens  *	Used in dmu_offset_next().
1972fa9e4066Sahrens  *
1973cdb0ab79Smaybee  * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
1974fa9e4066Sahrens  *	Finds the next free/allocated dnode an objset's meta-dnode.
19756754306eSahrens  *	Only finds objects that have new contents since txg (ie.
19766754306eSahrens  *	bonus buffer changes and content removal are ignored).
1977fa9e4066Sahrens  *	Used in dmu_object_next().
1978fa9e4066Sahrens  *
1979cdb0ab79Smaybee  * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
1980fa9e4066Sahrens  *	Finds the next L2 meta-dnode bp that's at most 1/4 full.
1981fa9e4066Sahrens  *	Used in dmu_object_alloc().
1982fa9e4066Sahrens  */
1983fa9e4066Sahrens int
dnode_next_offset(dnode_t * dn,int flags,uint64_t * offset,int minlvl,uint64_t blkfill,uint64_t txg)1984cdb0ab79Smaybee dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset,
19856754306eSahrens     int minlvl, uint64_t blkfill, uint64_t txg)
1986fa9e4066Sahrens {
1987cdb0ab79Smaybee 	uint64_t initial_offset = *offset;
1988fa9e4066Sahrens 	int lvl, maxlvl;
1989fa9e4066Sahrens 	int error = 0;
1990fa9e4066Sahrens 
1991cdb0ab79Smaybee 	if (!(flags & DNODE_FIND_HAVELOCK))
1992fa9e4066Sahrens 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
1993fa9e4066Sahrens 
1994fa9e4066Sahrens 	if (dn->dn_phys->dn_nlevels == 0) {
1995be6fd75aSMatthew Ahrens 		error = SET_ERROR(ESRCH);
1996cdb0ab79Smaybee 		goto out;
1997fa9e4066Sahrens 	}
1998fa9e4066Sahrens 
1999fa9e4066Sahrens 	if (dn->dn_datablkshift == 0) {
2000fa9e4066Sahrens 		if (*offset < dn->dn_datablksz) {
2001cdb0ab79Smaybee 			if (flags & DNODE_FIND_HOLE)
2002fa9e4066Sahrens 				*offset = dn->dn_datablksz;
2003fa9e4066Sahrens 		} else {
2004be6fd75aSMatthew Ahrens 			error = SET_ERROR(ESRCH);
2005fa9e4066Sahrens 		}
2006cdb0ab79Smaybee 		goto out;
2007fa9e4066Sahrens 	}
2008fa9e4066Sahrens 
2009fa9e4066Sahrens 	maxlvl = dn->dn_phys->dn_nlevels;
2010fa9e4066Sahrens 
2011fa9e4066Sahrens 	for (lvl = minlvl; lvl <= maxlvl; lvl++) {
20126754306eSahrens 		error = dnode_next_offset_level(dn,
2013cdb0ab79Smaybee 		    flags, offset, lvl, blkfill, txg);
201498572ac1Sahrens 		if (error != ESRCH)
2015fa9e4066Sahrens 			break;
2016fa9e4066Sahrens 	}
2017fa9e4066Sahrens 
2018cdb0ab79Smaybee 	while (error == 0 && --lvl >= minlvl) {
20196754306eSahrens 		error = dnode_next_offset_level(dn,
2020cdb0ab79Smaybee 		    flags, offset, lvl, blkfill, txg);
20216754306eSahrens 	}
2022fa9e4066Sahrens 
2023958b8692SMatthew Ahrens 	/*
2024958b8692SMatthew Ahrens 	 * There's always a "virtual hole" at the end of the object, even
2025958b8692SMatthew Ahrens 	 * if all BP's which physically exist are non-holes.
2026958b8692SMatthew Ahrens 	 */
2027958b8692SMatthew Ahrens 	if ((flags & DNODE_FIND_HOLE) && error == ESRCH && txg == 0 &&
2028958b8692SMatthew Ahrens 	    minlvl == 1 && blkfill == 1 && !(flags & DNODE_FIND_BACKWARDS)) {
2029958b8692SMatthew Ahrens 		error = 0;
2030958b8692SMatthew Ahrens 	}
2031958b8692SMatthew Ahrens 
2032cdb0ab79Smaybee 	if (error == 0 && (flags & DNODE_FIND_BACKWARDS ?
2033cdb0ab79Smaybee 	    initial_offset < *offset : initial_offset > *offset))
2034be6fd75aSMatthew Ahrens 		error = SET_ERROR(ESRCH);
2035cdb0ab79Smaybee out:
2036cdb0ab79Smaybee 	if (!(flags & DNODE_FIND_HAVELOCK))
2037cdb0ab79Smaybee 		rw_exit(&dn->dn_struct_rwlock);
2038fa9e4066Sahrens 
2039fa9e4066Sahrens 	return (error);
2040fa9e4066Sahrens }
2041