xref: /onnv-gate/usr/src/uts/common/fs/zfs/dnode.c (revision 12684:397e44ebb8a9)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51491Sahrens  * Common Development and Distribution License (the "License").
61491Sahrens  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
2212178SMark.Shellenbaum@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23789Sahrens  */
24789Sahrens 
25789Sahrens #include <sys/zfs_context.h>
26789Sahrens #include <sys/dbuf.h>
27789Sahrens #include <sys/dnode.h>
28789Sahrens #include <sys/dmu.h>
29789Sahrens #include <sys/dmu_impl.h>
30789Sahrens #include <sys/dmu_tx.h>
31789Sahrens #include <sys/dmu_objset.h>
32789Sahrens #include <sys/dsl_dir.h>
33789Sahrens #include <sys/dsl_dataset.h>
34789Sahrens #include <sys/spa.h>
35789Sahrens #include <sys/zio.h>
36789Sahrens #include <sys/dmu_zfetch.h>
37789Sahrens 
38789Sahrens static int free_range_compar(const void *node1, const void *node2);
39789Sahrens 
40789Sahrens static kmem_cache_t *dnode_cache;
41*12684STom.Erickson@Sun.COM /*
42*12684STom.Erickson@Sun.COM  * Define DNODE_STATS to turn on statistic gathering. By default, it is only
43*12684STom.Erickson@Sun.COM  * turned on when DEBUG is also defined.
44*12684STom.Erickson@Sun.COM  */
45*12684STom.Erickson@Sun.COM #ifdef	DEBUG
46*12684STom.Erickson@Sun.COM #define	DNODE_STATS
47*12684STom.Erickson@Sun.COM #endif	/* DEBUG */
48*12684STom.Erickson@Sun.COM 
49*12684STom.Erickson@Sun.COM #ifdef	DNODE_STATS
50*12684STom.Erickson@Sun.COM #define	DNODE_STAT_ADD(stat)			((stat)++)
51*12684STom.Erickson@Sun.COM #else
52*12684STom.Erickson@Sun.COM #define	DNODE_STAT_ADD(stat)			/* nothing */
53*12684STom.Erickson@Sun.COM #endif	/* DNODE_STATS */
54789Sahrens 
55789Sahrens static dnode_phys_t dnode_phys_zero;
56789Sahrens 
57789Sahrens int zfs_default_bs = SPA_MINBLOCKSHIFT;
58789Sahrens int zfs_default_ibs = DN_MAX_INDBLKSHIFT;
59789Sahrens 
60*12684STom.Erickson@Sun.COM static kmem_cbrc_t dnode_move(void *, void *, size_t, void *);
61*12684STom.Erickson@Sun.COM 
62789Sahrens /* ARGSUSED */
63789Sahrens static int
dnode_cons(void * arg,void * unused,int kmflag)64789Sahrens dnode_cons(void *arg, void *unused, int kmflag)
65789Sahrens {
66*12684STom.Erickson@Sun.COM 	dnode_t *dn = arg;
67789Sahrens 	int i;
68789Sahrens 
69789Sahrens 	rw_init(&dn->dn_struct_rwlock, NULL, RW_DEFAULT, NULL);
70789Sahrens 	mutex_init(&dn->dn_mtx, NULL, MUTEX_DEFAULT, NULL);
71789Sahrens 	mutex_init(&dn->dn_dbufs_mtx, NULL, MUTEX_DEFAULT, NULL);
728214SRicardo.M.Correia@Sun.COM 	cv_init(&dn->dn_notxholds, NULL, CV_DEFAULT, NULL);
738214SRicardo.M.Correia@Sun.COM 
74789Sahrens 	refcount_create(&dn->dn_holds);
75789Sahrens 	refcount_create(&dn->dn_tx_holds);
76*12684STom.Erickson@Sun.COM 	list_link_init(&dn->dn_link);
77*12684STom.Erickson@Sun.COM 
78*12684STom.Erickson@Sun.COM 	bzero(&dn->dn_next_nblkptr[0], sizeof (dn->dn_next_nblkptr));
79*12684STom.Erickson@Sun.COM 	bzero(&dn->dn_next_nlevels[0], sizeof (dn->dn_next_nlevels));
80*12684STom.Erickson@Sun.COM 	bzero(&dn->dn_next_indblkshift[0], sizeof (dn->dn_next_indblkshift));
81*12684STom.Erickson@Sun.COM 	bzero(&dn->dn_next_bonustype[0], sizeof (dn->dn_next_bonustype));
82*12684STom.Erickson@Sun.COM 	bzero(&dn->dn_rm_spillblk[0], sizeof (dn->dn_rm_spillblk));
83*12684STom.Erickson@Sun.COM 	bzero(&dn->dn_next_bonuslen[0], sizeof (dn->dn_next_bonuslen));
84*12684STom.Erickson@Sun.COM 	bzero(&dn->dn_next_blksz[0], sizeof (dn->dn_next_blksz));
85789Sahrens 
86789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
87*12684STom.Erickson@Sun.COM 		list_link_init(&dn->dn_dirty_link[i]);
88789Sahrens 		avl_create(&dn->dn_ranges[i], free_range_compar,
89789Sahrens 		    sizeof (free_range_t),
90789Sahrens 		    offsetof(struct free_range, fr_node));
913547Smaybee 		list_create(&dn->dn_dirty_records[i],
923547Smaybee 		    sizeof (dbuf_dirty_record_t),
933547Smaybee 		    offsetof(dbuf_dirty_record_t, dr_dirty_node));
94789Sahrens 	}
95789Sahrens 
96*12684STom.Erickson@Sun.COM 	dn->dn_allocated_txg = 0;
97*12684STom.Erickson@Sun.COM 	dn->dn_free_txg = 0;
98*12684STom.Erickson@Sun.COM 	dn->dn_assigned_txg = 0;
99*12684STom.Erickson@Sun.COM 	dn->dn_dirtyctx = 0;
100*12684STom.Erickson@Sun.COM 	dn->dn_dirtyctx_firstset = NULL;
101*12684STom.Erickson@Sun.COM 	dn->dn_bonus = NULL;
102*12684STom.Erickson@Sun.COM 	dn->dn_have_spill = B_FALSE;
103*12684STom.Erickson@Sun.COM 	dn->dn_zio = NULL;
104*12684STom.Erickson@Sun.COM 	dn->dn_oldused = 0;
105*12684STom.Erickson@Sun.COM 	dn->dn_oldflags = 0;
106*12684STom.Erickson@Sun.COM 	dn->dn_olduid = 0;
107*12684STom.Erickson@Sun.COM 	dn->dn_oldgid = 0;
108*12684STom.Erickson@Sun.COM 	dn->dn_newuid = 0;
109*12684STom.Erickson@Sun.COM 	dn->dn_newgid = 0;
110*12684STom.Erickson@Sun.COM 	dn->dn_id_flags = 0;
111*12684STom.Erickson@Sun.COM 
112*12684STom.Erickson@Sun.COM 	dn->dn_dbufs_count = 0;
113789Sahrens 	list_create(&dn->dn_dbufs, sizeof (dmu_buf_impl_t),
114789Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
115789Sahrens 
116*12684STom.Erickson@Sun.COM 	dn->dn_moved = 0;
117789Sahrens 	return (0);
118789Sahrens }
119789Sahrens 
120789Sahrens /* ARGSUSED */
121789Sahrens static void
dnode_dest(void * arg,void * unused)122789Sahrens dnode_dest(void *arg, void *unused)
123789Sahrens {
124789Sahrens 	int i;
125789Sahrens 	dnode_t *dn = arg;
126789Sahrens 
127789Sahrens 	rw_destroy(&dn->dn_struct_rwlock);
128789Sahrens 	mutex_destroy(&dn->dn_mtx);
129789Sahrens 	mutex_destroy(&dn->dn_dbufs_mtx);
1308214SRicardo.M.Correia@Sun.COM 	cv_destroy(&dn->dn_notxholds);
131789Sahrens 	refcount_destroy(&dn->dn_holds);
132789Sahrens 	refcount_destroy(&dn->dn_tx_holds);
133*12684STom.Erickson@Sun.COM 	ASSERT(!list_link_active(&dn->dn_link));
134789Sahrens 
135789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
136*12684STom.Erickson@Sun.COM 		ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
137789Sahrens 		avl_destroy(&dn->dn_ranges[i]);
1383547Smaybee 		list_destroy(&dn->dn_dirty_records[i]);
139*12684STom.Erickson@Sun.COM 		ASSERT3U(dn->dn_next_nblkptr[i], ==, 0);
140*12684STom.Erickson@Sun.COM 		ASSERT3U(dn->dn_next_nlevels[i], ==, 0);
141*12684STom.Erickson@Sun.COM 		ASSERT3U(dn->dn_next_indblkshift[i], ==, 0);
142*12684STom.Erickson@Sun.COM 		ASSERT3U(dn->dn_next_bonustype[i], ==, 0);
143*12684STom.Erickson@Sun.COM 		ASSERT3U(dn->dn_rm_spillblk[i], ==, 0);
144*12684STom.Erickson@Sun.COM 		ASSERT3U(dn->dn_next_bonuslen[i], ==, 0);
145*12684STom.Erickson@Sun.COM 		ASSERT3U(dn->dn_next_blksz[i], ==, 0);
146789Sahrens 	}
147789Sahrens 
148*12684STom.Erickson@Sun.COM 	ASSERT3U(dn->dn_allocated_txg, ==, 0);
149*12684STom.Erickson@Sun.COM 	ASSERT3U(dn->dn_free_txg, ==, 0);
150*12684STom.Erickson@Sun.COM 	ASSERT3U(dn->dn_assigned_txg, ==, 0);
151*12684STom.Erickson@Sun.COM 	ASSERT3U(dn->dn_dirtyctx, ==, 0);
152*12684STom.Erickson@Sun.COM 	ASSERT3P(dn->dn_dirtyctx_firstset, ==, NULL);
153*12684STom.Erickson@Sun.COM 	ASSERT3P(dn->dn_bonus, ==, NULL);
154*12684STom.Erickson@Sun.COM 	ASSERT(!dn->dn_have_spill);
155*12684STom.Erickson@Sun.COM 	ASSERT3P(dn->dn_zio, ==, NULL);
156*12684STom.Erickson@Sun.COM 	ASSERT3U(dn->dn_oldused, ==, 0);
157*12684STom.Erickson@Sun.COM 	ASSERT3U(dn->dn_oldflags, ==, 0);
158*12684STom.Erickson@Sun.COM 	ASSERT3U(dn->dn_olduid, ==, 0);
159*12684STom.Erickson@Sun.COM 	ASSERT3U(dn->dn_oldgid, ==, 0);
160*12684STom.Erickson@Sun.COM 	ASSERT3U(dn->dn_newuid, ==, 0);
161*12684STom.Erickson@Sun.COM 	ASSERT3U(dn->dn_newgid, ==, 0);
162*12684STom.Erickson@Sun.COM 	ASSERT3U(dn->dn_id_flags, ==, 0);
163*12684STom.Erickson@Sun.COM 
164*12684STom.Erickson@Sun.COM 	ASSERT3U(dn->dn_dbufs_count, ==, 0);
165789Sahrens 	list_destroy(&dn->dn_dbufs);
166789Sahrens }
167789Sahrens 
168789Sahrens void
dnode_init(void)169789Sahrens dnode_init(void)
170789Sahrens {
171*12684STom.Erickson@Sun.COM 	ASSERT(dnode_cache == NULL);
172789Sahrens 	dnode_cache = kmem_cache_create("dnode_t",
173789Sahrens 	    sizeof (dnode_t),
174789Sahrens 	    0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0);
175*12684STom.Erickson@Sun.COM 	kmem_cache_set_move(dnode_cache, dnode_move);
176789Sahrens }
177789Sahrens 
178789Sahrens void
dnode_fini(void)179789Sahrens dnode_fini(void)
180789Sahrens {
181789Sahrens 	kmem_cache_destroy(dnode_cache);
182*12684STom.Erickson@Sun.COM 	dnode_cache = NULL;
183789Sahrens }
184789Sahrens 
185789Sahrens 
186873Sek110237 #ifdef ZFS_DEBUG
187789Sahrens void
dnode_verify(dnode_t * dn)188789Sahrens dnode_verify(dnode_t *dn)
189789Sahrens {
190789Sahrens 	int drop_struct_lock = FALSE;
191789Sahrens 
192789Sahrens 	ASSERT(dn->dn_phys);
193789Sahrens 	ASSERT(dn->dn_objset);
194*12684STom.Erickson@Sun.COM 	ASSERT(dn->dn_handle->dnh_dnode == dn);
195789Sahrens 
196789Sahrens 	ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES);
197789Sahrens 
198789Sahrens 	if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY))
199789Sahrens 		return;
200789Sahrens 
201789Sahrens 	if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
202789Sahrens 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
203789Sahrens 		drop_struct_lock = TRUE;
204789Sahrens 	}
205789Sahrens 	if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) {
206789Sahrens 		int i;
207789Sahrens 		ASSERT3U(dn->dn_indblkshift, >=, 0);
208789Sahrens 		ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT);
209789Sahrens 		if (dn->dn_datablkshift) {
210789Sahrens 			ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT);
211789Sahrens 			ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT);
212789Sahrens 			ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz);
213789Sahrens 		}
214789Sahrens 		ASSERT3U(dn->dn_nlevels, <=, 30);
215789Sahrens 		ASSERT3U(dn->dn_type, <=, DMU_OT_NUMTYPES);
216789Sahrens 		ASSERT3U(dn->dn_nblkptr, >=, 1);
217789Sahrens 		ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
218789Sahrens 		ASSERT3U(dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
219789Sahrens 		ASSERT3U(dn->dn_datablksz, ==,
220789Sahrens 		    dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
221789Sahrens 		ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0);
222789Sahrens 		ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) +
223789Sahrens 		    dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
224789Sahrens 		for (i = 0; i < TXG_SIZE; i++) {
225789Sahrens 			ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels);
226789Sahrens 		}
227789Sahrens 	}
228789Sahrens 	if (dn->dn_phys->dn_type != DMU_OT_NONE)
229789Sahrens 		ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels);
2309396SMatthew.Ahrens@Sun.COM 	ASSERT(DMU_OBJECT_IS_SPECIAL(dn->dn_object) || dn->dn_dbuf != NULL);
231789Sahrens 	if (dn->dn_dbuf != NULL) {
232789Sahrens 		ASSERT3P(dn->dn_phys, ==,
233789Sahrens 		    (dnode_phys_t *)dn->dn_dbuf->db.db_data +
234789Sahrens 		    (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT)));
235789Sahrens 	}
236789Sahrens 	if (drop_struct_lock)
237789Sahrens 		rw_exit(&dn->dn_struct_rwlock);
238873Sek110237 }
239789Sahrens #endif
240789Sahrens 
241789Sahrens void
dnode_byteswap(dnode_phys_t * dnp)242789Sahrens dnode_byteswap(dnode_phys_t *dnp)
243789Sahrens {
244789Sahrens 	uint64_t *buf64 = (void*)&dnp->dn_blkptr;
245789Sahrens 	int i;
246789Sahrens 
247789Sahrens 	if (dnp->dn_type == DMU_OT_NONE) {
248789Sahrens 		bzero(dnp, sizeof (dnode_phys_t));
249789Sahrens 		return;
250789Sahrens 	}
251789Sahrens 
252789Sahrens 	dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec);
253789Sahrens 	dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen);
254789Sahrens 	dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid);
2552082Seschrock 	dnp->dn_used = BSWAP_64(dnp->dn_used);
256789Sahrens 
257789Sahrens 	/*
258789Sahrens 	 * dn_nblkptr is only one byte, so it's OK to read it in either
259789Sahrens 	 * byte order.  We can't read dn_bouslen.
260789Sahrens 	 */
261789Sahrens 	ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT);
262789Sahrens 	ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR);
263789Sahrens 	for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++)
264789Sahrens 		buf64[i] = BSWAP_64(buf64[i]);
265789Sahrens 
266789Sahrens 	/*
267789Sahrens 	 * OK to check dn_bonuslen for zero, because it won't matter if
268789Sahrens 	 * we have the wrong byte order.  This is necessary because the
269789Sahrens 	 * dnode dnode is smaller than a regular dnode.
270789Sahrens 	 */
271789Sahrens 	if (dnp->dn_bonuslen != 0) {
272789Sahrens 		/*
273789Sahrens 		 * Note that the bonus length calculated here may be
274789Sahrens 		 * longer than the actual bonus buffer.  This is because
275789Sahrens 		 * we always put the bonus buffer after the last block
276789Sahrens 		 * pointer (instead of packing it against the end of the
277789Sahrens 		 * dnode buffer).
278789Sahrens 		 */
279789Sahrens 		int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t);
280789Sahrens 		size_t len = DN_MAX_BONUSLEN - off;
2813882Sahrens 		ASSERT3U(dnp->dn_bonustype, <, DMU_OT_NUMTYPES);
282789Sahrens 		dmu_ot[dnp->dn_bonustype].ot_byteswap(dnp->dn_bonus + off, len);
283789Sahrens 	}
28411935SMark.Shellenbaum@Sun.COM 
28511935SMark.Shellenbaum@Sun.COM 	/* Swap SPILL block if we have one */
28611935SMark.Shellenbaum@Sun.COM 	if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)
28711935SMark.Shellenbaum@Sun.COM 		byteswap_uint64_array(&dnp->dn_spill, sizeof (blkptr_t));
28811935SMark.Shellenbaum@Sun.COM 
289789Sahrens }
290789Sahrens 
291789Sahrens void
dnode_buf_byteswap(void * vbuf,size_t size)292789Sahrens dnode_buf_byteswap(void *vbuf, size_t size)
293789Sahrens {
294789Sahrens 	dnode_phys_t *buf = vbuf;
295789Sahrens 	int i;
296789Sahrens 
297789Sahrens 	ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT));
298789Sahrens 	ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0);
299789Sahrens 
300789Sahrens 	size >>= DNODE_SHIFT;
301789Sahrens 	for (i = 0; i < size; i++) {
302789Sahrens 		dnode_byteswap(buf);
303789Sahrens 		buf++;
304789Sahrens 	}
305789Sahrens }
306789Sahrens 
307789Sahrens static int
free_range_compar(const void * node1,const void * node2)308789Sahrens free_range_compar(const void *node1, const void *node2)
309789Sahrens {
310789Sahrens 	const free_range_t *rp1 = node1;
311789Sahrens 	const free_range_t *rp2 = node2;
312789Sahrens 
313789Sahrens 	if (rp1->fr_blkid < rp2->fr_blkid)
314789Sahrens 		return (-1);
315789Sahrens 	else if (rp1->fr_blkid > rp2->fr_blkid)
316789Sahrens 		return (1);
317789Sahrens 	else return (0);
318789Sahrens }
319789Sahrens 
3204944Smaybee void
dnode_setbonuslen(dnode_t * dn,int newsize,dmu_tx_t * tx)3214944Smaybee dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx)
3224944Smaybee {
3234944Smaybee 	ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
3244944Smaybee 
3254944Smaybee 	dnode_setdirty(dn, tx);
3264944Smaybee 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
3274944Smaybee 	ASSERT3U(newsize, <=, DN_MAX_BONUSLEN -
3284944Smaybee 	    (dn->dn_nblkptr-1) * sizeof (blkptr_t));
3294944Smaybee 	dn->dn_bonuslen = newsize;
3304944Smaybee 	if (newsize == 0)
3314944Smaybee 		dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = DN_ZERO_BONUSLEN;
3324944Smaybee 	else
3334944Smaybee 		dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
3344944Smaybee 	rw_exit(&dn->dn_struct_rwlock);
3354944Smaybee }
3364944Smaybee 
33711935SMark.Shellenbaum@Sun.COM void
dnode_setbonus_type(dnode_t * dn,dmu_object_type_t newtype,dmu_tx_t * tx)33811935SMark.Shellenbaum@Sun.COM dnode_setbonus_type(dnode_t *dn, dmu_object_type_t newtype, dmu_tx_t *tx)
33911935SMark.Shellenbaum@Sun.COM {
34011935SMark.Shellenbaum@Sun.COM 	ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
34111935SMark.Shellenbaum@Sun.COM 	dnode_setdirty(dn, tx);
34211935SMark.Shellenbaum@Sun.COM 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
34311935SMark.Shellenbaum@Sun.COM 	dn->dn_bonustype = newtype;
34411935SMark.Shellenbaum@Sun.COM 	dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
34511935SMark.Shellenbaum@Sun.COM 	rw_exit(&dn->dn_struct_rwlock);
34611935SMark.Shellenbaum@Sun.COM }
34711935SMark.Shellenbaum@Sun.COM 
34811935SMark.Shellenbaum@Sun.COM void
dnode_rm_spill(dnode_t * dn,dmu_tx_t * tx)34911935SMark.Shellenbaum@Sun.COM dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx)
35011935SMark.Shellenbaum@Sun.COM {
35111935SMark.Shellenbaum@Sun.COM 	ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
35212178SMark.Shellenbaum@Sun.COM 	ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
35311935SMark.Shellenbaum@Sun.COM 	dnode_setdirty(dn, tx);
35411935SMark.Shellenbaum@Sun.COM 	dn->dn_rm_spillblk[tx->tx_txg&TXG_MASK] = DN_KILL_SPILLBLK;
35511935SMark.Shellenbaum@Sun.COM 	dn->dn_have_spill = B_FALSE;
35611935SMark.Shellenbaum@Sun.COM }
35711935SMark.Shellenbaum@Sun.COM 
358789Sahrens static void
dnode_setdblksz(dnode_t * dn,int size)359789Sahrens dnode_setdblksz(dnode_t *dn, int size)
360789Sahrens {
361789Sahrens 	ASSERT3U(P2PHASE(size, SPA_MINBLOCKSIZE), ==, 0);
362789Sahrens 	ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
363789Sahrens 	ASSERT3U(size, >=, SPA_MINBLOCKSIZE);
364789Sahrens 	ASSERT3U(size >> SPA_MINBLOCKSHIFT, <,
365789Sahrens 	    1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8));
366789Sahrens 	dn->dn_datablksz = size;
367789Sahrens 	dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT;
368789Sahrens 	dn->dn_datablkshift = ISP2(size) ? highbit(size - 1) : 0;
369789Sahrens }
370789Sahrens 
371789Sahrens static dnode_t *
dnode_create(objset_t * os,dnode_phys_t * dnp,dmu_buf_impl_t * db,uint64_t object,dnode_handle_t * dnh)37210298SMatthew.Ahrens@Sun.COM dnode_create(objset_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db,
373*12684STom.Erickson@Sun.COM     uint64_t object, dnode_handle_t *dnh)
374789Sahrens {
375789Sahrens 	dnode_t *dn = kmem_cache_alloc(dnode_cache, KM_SLEEP);
376*12684STom.Erickson@Sun.COM 
377*12684STom.Erickson@Sun.COM 	ASSERT(!POINTER_IS_VALID(dn->dn_objset));
378*12684STom.Erickson@Sun.COM 	dn->dn_moved = 0;
379789Sahrens 
380*12684STom.Erickson@Sun.COM 	/*
381*12684STom.Erickson@Sun.COM 	 * Defer setting dn_objset until the dnode is ready to be a candidate
382*12684STom.Erickson@Sun.COM 	 * for the dnode_move() callback.
383*12684STom.Erickson@Sun.COM 	 */
384789Sahrens 	dn->dn_object = object;
385789Sahrens 	dn->dn_dbuf = db;
386*12684STom.Erickson@Sun.COM 	dn->dn_handle = dnh;
387789Sahrens 	dn->dn_phys = dnp;
388789Sahrens 
389*12684STom.Erickson@Sun.COM 	if (dnp->dn_datablkszsec) {
390789Sahrens 		dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
391*12684STom.Erickson@Sun.COM 	} else {
392*12684STom.Erickson@Sun.COM 		dn->dn_datablksz = 0;
393*12684STom.Erickson@Sun.COM 		dn->dn_datablkszsec = 0;
394*12684STom.Erickson@Sun.COM 		dn->dn_datablkshift = 0;
395*12684STom.Erickson@Sun.COM 	}
396789Sahrens 	dn->dn_indblkshift = dnp->dn_indblkshift;
397789Sahrens 	dn->dn_nlevels = dnp->dn_nlevels;
398789Sahrens 	dn->dn_type = dnp->dn_type;
399789Sahrens 	dn->dn_nblkptr = dnp->dn_nblkptr;
400789Sahrens 	dn->dn_checksum = dnp->dn_checksum;
401789Sahrens 	dn->dn_compress = dnp->dn_compress;
402789Sahrens 	dn->dn_bonustype = dnp->dn_bonustype;
403789Sahrens 	dn->dn_bonuslen = dnp->dn_bonuslen;
404789Sahrens 	dn->dn_maxblkid = dnp->dn_maxblkid;
40511935SMark.Shellenbaum@Sun.COM 	dn->dn_have_spill = ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0);
40612178SMark.Shellenbaum@Sun.COM 	dn->dn_id_flags = 0;
407789Sahrens 
408789Sahrens 	dmu_zfetch_init(&dn->dn_zfetch, dn);
409789Sahrens 
410789Sahrens 	ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES);
411*12684STom.Erickson@Sun.COM 
412789Sahrens 	mutex_enter(&os->os_lock);
413789Sahrens 	list_insert_head(&os->os_dnodes, dn);
414*12684STom.Erickson@Sun.COM 	membar_producer();
415*12684STom.Erickson@Sun.COM 	/*
416*12684STom.Erickson@Sun.COM 	 * Everything else must be valid before assigning dn_objset makes the
417*12684STom.Erickson@Sun.COM 	 * dnode eligible for dnode_move().
418*12684STom.Erickson@Sun.COM 	 */
419*12684STom.Erickson@Sun.COM 	dn->dn_objset = os;
420789Sahrens 	mutex_exit(&os->os_lock);
421789Sahrens 
4228582SBrendan.Gregg@Sun.COM 	arc_space_consume(sizeof (dnode_t), ARC_SPACE_OTHER);
423789Sahrens 	return (dn);
424789Sahrens }
425789Sahrens 
426*12684STom.Erickson@Sun.COM /*
427*12684STom.Erickson@Sun.COM  * Caller must be holding the dnode handle, which is released upon return.
428*12684STom.Erickson@Sun.COM  */
429789Sahrens static void
dnode_destroy(dnode_t * dn)430789Sahrens dnode_destroy(dnode_t *dn)
431789Sahrens {
43210298SMatthew.Ahrens@Sun.COM 	objset_t *os = dn->dn_objset;
433789Sahrens 
43411935SMark.Shellenbaum@Sun.COM 	ASSERT((dn->dn_id_flags & DN_ID_NEW_EXIST) == 0);
4352885Sahrens 
436789Sahrens 	mutex_enter(&os->os_lock);
437*12684STom.Erickson@Sun.COM 	POINTER_INVALIDATE(&dn->dn_objset);
438789Sahrens 	list_remove(&os->os_dnodes, dn);
439789Sahrens 	mutex_exit(&os->os_lock);
440789Sahrens 
441*12684STom.Erickson@Sun.COM 	/* the dnode can no longer move, so we can release the handle */
442*12684STom.Erickson@Sun.COM 	zrl_remove(&dn->dn_handle->dnh_zrlock);
443*12684STom.Erickson@Sun.COM 
444*12684STom.Erickson@Sun.COM 	dn->dn_allocated_txg = 0;
445*12684STom.Erickson@Sun.COM 	dn->dn_free_txg = 0;
446*12684STom.Erickson@Sun.COM 	dn->dn_assigned_txg = 0;
447*12684STom.Erickson@Sun.COM 
448*12684STom.Erickson@Sun.COM 	dn->dn_dirtyctx = 0;
449*12684STom.Erickson@Sun.COM 	if (dn->dn_dirtyctx_firstset != NULL) {
450789Sahrens 		kmem_free(dn->dn_dirtyctx_firstset, 1);
451789Sahrens 		dn->dn_dirtyctx_firstset = NULL;
452789Sahrens 	}
453*12684STom.Erickson@Sun.COM 	if (dn->dn_bonus != NULL) {
4541544Seschrock 		mutex_enter(&dn->dn_bonus->db_mtx);
4551544Seschrock 		dbuf_evict(dn->dn_bonus);
4561544Seschrock 		dn->dn_bonus = NULL;
4571544Seschrock 	}
458*12684STom.Erickson@Sun.COM 	dn->dn_zio = NULL;
459*12684STom.Erickson@Sun.COM 
460*12684STom.Erickson@Sun.COM 	dn->dn_have_spill = B_FALSE;
461*12684STom.Erickson@Sun.COM 	dn->dn_oldused = 0;
462*12684STom.Erickson@Sun.COM 	dn->dn_oldflags = 0;
463*12684STom.Erickson@Sun.COM 	dn->dn_olduid = 0;
464*12684STom.Erickson@Sun.COM 	dn->dn_oldgid = 0;
465*12684STom.Erickson@Sun.COM 	dn->dn_newuid = 0;
466*12684STom.Erickson@Sun.COM 	dn->dn_newgid = 0;
467*12684STom.Erickson@Sun.COM 	dn->dn_id_flags = 0;
468*12684STom.Erickson@Sun.COM 
469*12684STom.Erickson@Sun.COM 	dmu_zfetch_rele(&dn->dn_zfetch);
470789Sahrens 	kmem_cache_free(dnode_cache, dn);
4718582SBrendan.Gregg@Sun.COM 	arc_space_return(sizeof (dnode_t), ARC_SPACE_OTHER);
472789Sahrens }
473789Sahrens 
474789Sahrens 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)475789Sahrens dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
4761599Sahrens     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
477789Sahrens {
478789Sahrens 	int i;
479789Sahrens 
480789Sahrens 	if (blocksize == 0)
481789Sahrens 		blocksize = 1 << zfs_default_bs;
4821402Sahrens 	else if (blocksize > SPA_MAXBLOCKSIZE)
4831402Sahrens 		blocksize = SPA_MAXBLOCKSIZE;
4841402Sahrens 	else
4851402Sahrens 		blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE);
486789Sahrens 
487789Sahrens 	if (ibs == 0)
488789Sahrens 		ibs = zfs_default_ibs;
489789Sahrens 
490789Sahrens 	ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT);
491789Sahrens 
492789Sahrens 	dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset,
493789Sahrens 	    dn->dn_object, tx->tx_txg, blocksize, ibs);
494789Sahrens 
495789Sahrens 	ASSERT(dn->dn_type == DMU_OT_NONE);
496789Sahrens 	ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0);
497789Sahrens 	ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE);
498789Sahrens 	ASSERT(ot != DMU_OT_NONE);
499789Sahrens 	ASSERT3U(ot, <, DMU_OT_NUMTYPES);
500789Sahrens 	ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
50111935SMark.Shellenbaum@Sun.COM 	    (bonustype == DMU_OT_SA && bonuslen == 0) ||
502789Sahrens 	    (bonustype != DMU_OT_NONE && bonuslen != 0));
503789Sahrens 	ASSERT3U(bonustype, <, DMU_OT_NUMTYPES);
504789Sahrens 	ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
505789Sahrens 	ASSERT(dn->dn_type == DMU_OT_NONE);
506789Sahrens 	ASSERT3U(dn->dn_maxblkid, ==, 0);
507789Sahrens 	ASSERT3U(dn->dn_allocated_txg, ==, 0);
508789Sahrens 	ASSERT3U(dn->dn_assigned_txg, ==, 0);
509789Sahrens 	ASSERT(refcount_is_zero(&dn->dn_tx_holds));
510789Sahrens 	ASSERT3U(refcount_count(&dn->dn_holds), <=, 1);
511789Sahrens 	ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
512789Sahrens 
513789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
514*12684STom.Erickson@Sun.COM 		ASSERT3U(dn->dn_next_nblkptr[i], ==, 0);
515789Sahrens 		ASSERT3U(dn->dn_next_nlevels[i], ==, 0);
516789Sahrens 		ASSERT3U(dn->dn_next_indblkshift[i], ==, 0);
5174944Smaybee 		ASSERT3U(dn->dn_next_bonuslen[i], ==, 0);
51811935SMark.Shellenbaum@Sun.COM 		ASSERT3U(dn->dn_next_bonustype[i], ==, 0);
51911935SMark.Shellenbaum@Sun.COM 		ASSERT3U(dn->dn_rm_spillblk[i], ==, 0);
5201596Sahrens 		ASSERT3U(dn->dn_next_blksz[i], ==, 0);
5211596Sahrens 		ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
5223547Smaybee 		ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL);
523789Sahrens 		ASSERT3U(avl_numnodes(&dn->dn_ranges[i]), ==, 0);
524789Sahrens 	}
525789Sahrens 
526789Sahrens 	dn->dn_type = ot;
527789Sahrens 	dnode_setdblksz(dn, blocksize);
528789Sahrens 	dn->dn_indblkshift = ibs;
529789Sahrens 	dn->dn_nlevels = 1;
53011935SMark.Shellenbaum@Sun.COM 	if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
53111935SMark.Shellenbaum@Sun.COM 		dn->dn_nblkptr = 1;
53211935SMark.Shellenbaum@Sun.COM 	else
53311935SMark.Shellenbaum@Sun.COM 		dn->dn_nblkptr = 1 +
53411935SMark.Shellenbaum@Sun.COM 		    ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
535789Sahrens 	dn->dn_bonustype = bonustype;
536789Sahrens 	dn->dn_bonuslen = bonuslen;
537789Sahrens 	dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
538789Sahrens 	dn->dn_compress = ZIO_COMPRESS_INHERIT;
539789Sahrens 	dn->dn_dirtyctx = 0;
540789Sahrens 
541789Sahrens 	dn->dn_free_txg = 0;
542789Sahrens 	if (dn->dn_dirtyctx_firstset) {
543789Sahrens 		kmem_free(dn->dn_dirtyctx_firstset, 1);
544789Sahrens 		dn->dn_dirtyctx_firstset = NULL;
545789Sahrens 	}
546789Sahrens 
547789Sahrens 	dn->dn_allocated_txg = tx->tx_txg;
54811935SMark.Shellenbaum@Sun.COM 	dn->dn_id_flags = 0;
5491599Sahrens 
550789Sahrens 	dnode_setdirty(dn, tx);
5511599Sahrens 	dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs;
5524944Smaybee 	dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
55311935SMark.Shellenbaum@Sun.COM 	dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
5541599Sahrens 	dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz;
555789Sahrens }
556789Sahrens 
557789Sahrens void
dnode_reallocate(dnode_t * dn,dmu_object_type_t ot,int blocksize,dmu_object_type_t bonustype,int bonuslen,dmu_tx_t * tx)558789Sahrens dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
559789Sahrens     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
560789Sahrens {
5618986SMark.Maybee@Sun.COM 	int nblkptr;
5621596Sahrens 
563789Sahrens 	ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE);
564789Sahrens 	ASSERT3U(blocksize, <=, SPA_MAXBLOCKSIZE);
565789Sahrens 	ASSERT3U(blocksize % SPA_MINBLOCKSIZE, ==, 0);
5661544Seschrock 	ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
567789Sahrens 	ASSERT(tx->tx_txg != 0);
568789Sahrens 	ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
56912178SMark.Shellenbaum@Sun.COM 	    (bonustype != DMU_OT_NONE && bonuslen != 0) ||
57012178SMark.Shellenbaum@Sun.COM 	    (bonustype == DMU_OT_SA && bonuslen == 0));
571789Sahrens 	ASSERT3U(bonustype, <, DMU_OT_NUMTYPES);
572789Sahrens 	ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
5731596Sahrens 
5741544Seschrock 	/* clean up any unreferenced dbufs */
5754944Smaybee 	dnode_evict_dbufs(dn);
5761544Seschrock 
57712432SMark.Shellenbaum@Sun.COM 	dn->dn_id_flags = 0;
57812432SMark.Shellenbaum@Sun.COM 
5798986SMark.Maybee@Sun.COM 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
5808986SMark.Maybee@Sun.COM 	dnode_setdirty(dn, tx);
5818986SMark.Maybee@Sun.COM 	if (dn->dn_datablksz != blocksize) {
5828986SMark.Maybee@Sun.COM 		/* change blocksize */
5838986SMark.Maybee@Sun.COM 		ASSERT(dn->dn_maxblkid == 0 &&
5848986SMark.Maybee@Sun.COM 		    (BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) ||
5858986SMark.Maybee@Sun.COM 		    dnode_block_freed(dn, 0)));
5868986SMark.Maybee@Sun.COM 		dnode_setdblksz(dn, blocksize);
5878986SMark.Maybee@Sun.COM 		dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize;
588789Sahrens 	}
5898986SMark.Maybee@Sun.COM 	if (dn->dn_bonuslen != bonuslen)
5908986SMark.Maybee@Sun.COM 		dn->dn_next_bonuslen[tx->tx_txg&TXG_MASK] = bonuslen;
59112178SMark.Shellenbaum@Sun.COM 
59212178SMark.Shellenbaum@Sun.COM 	if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
59312178SMark.Shellenbaum@Sun.COM 		nblkptr = 1;
59412178SMark.Shellenbaum@Sun.COM 	else
59512178SMark.Shellenbaum@Sun.COM 		nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
59611935SMark.Shellenbaum@Sun.COM 	if (dn->dn_bonustype != bonustype)
59711935SMark.Shellenbaum@Sun.COM 		dn->dn_next_bonustype[tx->tx_txg&TXG_MASK] = bonustype;
5988644SMark.Maybee@Sun.COM 	if (dn->dn_nblkptr != nblkptr)
5998644SMark.Maybee@Sun.COM 		dn->dn_next_nblkptr[tx->tx_txg&TXG_MASK] = nblkptr;
60011935SMark.Shellenbaum@Sun.COM 	if (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
60112178SMark.Shellenbaum@Sun.COM 		dbuf_rm_spill(dn, tx);
60212178SMark.Shellenbaum@Sun.COM 		dnode_rm_spill(dn, tx);
60311935SMark.Shellenbaum@Sun.COM 	}
604789Sahrens 	rw_exit(&dn->dn_struct_rwlock);
605789Sahrens 
606789Sahrens 	/* change type */
607789Sahrens 	dn->dn_type = ot;
608789Sahrens 
609789Sahrens 	/* change bonus size and type */
610789Sahrens 	mutex_enter(&dn->dn_mtx);
611789Sahrens 	dn->dn_bonustype = bonustype;
612789Sahrens 	dn->dn_bonuslen = bonuslen;
6138644SMark.Maybee@Sun.COM 	dn->dn_nblkptr = nblkptr;
614789Sahrens 	dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
615789Sahrens 	dn->dn_compress = ZIO_COMPRESS_INHERIT;
616789Sahrens 	ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
617789Sahrens 
6188644SMark.Maybee@Sun.COM 	/* fix up the bonus db_size */
6198644SMark.Maybee@Sun.COM 	if (dn->dn_bonus) {
6204944Smaybee 		dn->dn_bonus->db.db_size =
6214944Smaybee 		    DN_MAX_BONUSLEN - (dn->dn_nblkptr-1) * sizeof (blkptr_t);
6224944Smaybee 		ASSERT(dn->dn_bonuslen <= dn->dn_bonus->db.db_size);
6234944Smaybee 	}
6243087Sahrens 
625789Sahrens 	dn->dn_allocated_txg = tx->tx_txg;
626789Sahrens 	mutex_exit(&dn->dn_mtx);
627789Sahrens }
628789Sahrens 
629*12684STom.Erickson@Sun.COM #ifdef	DNODE_STATS
630*12684STom.Erickson@Sun.COM static struct {
631*12684STom.Erickson@Sun.COM 	uint64_t dms_dnode_invalid;
632*12684STom.Erickson@Sun.COM 	uint64_t dms_dnode_recheck1;
633*12684STom.Erickson@Sun.COM 	uint64_t dms_dnode_recheck2;
634*12684STom.Erickson@Sun.COM 	uint64_t dms_dnode_special;
635*12684STom.Erickson@Sun.COM 	uint64_t dms_dnode_handle;
636*12684STom.Erickson@Sun.COM 	uint64_t dms_dnode_rwlock;
637*12684STom.Erickson@Sun.COM 	uint64_t dms_dnode_active;
638*12684STom.Erickson@Sun.COM } dnode_move_stats;
639*12684STom.Erickson@Sun.COM #endif	/* DNODE_STATS */
640*12684STom.Erickson@Sun.COM 
641*12684STom.Erickson@Sun.COM static void
dnode_move_impl(dnode_t * odn,dnode_t * ndn)642*12684STom.Erickson@Sun.COM dnode_move_impl(dnode_t *odn, dnode_t *ndn)
643*12684STom.Erickson@Sun.COM {
644*12684STom.Erickson@Sun.COM 	int i;
645*12684STom.Erickson@Sun.COM 
646*12684STom.Erickson@Sun.COM 	ASSERT(!RW_LOCK_HELD(&odn->dn_struct_rwlock));
647*12684STom.Erickson@Sun.COM 	ASSERT(MUTEX_NOT_HELD(&odn->dn_mtx));
648*12684STom.Erickson@Sun.COM 	ASSERT(MUTEX_NOT_HELD(&odn->dn_dbufs_mtx));
649*12684STom.Erickson@Sun.COM 	ASSERT(!RW_LOCK_HELD(&odn->dn_zfetch.zf_rwlock));
650*12684STom.Erickson@Sun.COM 
651*12684STom.Erickson@Sun.COM 	/* Copy fields. */
652*12684STom.Erickson@Sun.COM 	ndn->dn_objset = odn->dn_objset;
653*12684STom.Erickson@Sun.COM 	ndn->dn_object = odn->dn_object;
654*12684STom.Erickson@Sun.COM 	ndn->dn_dbuf = odn->dn_dbuf;
655*12684STom.Erickson@Sun.COM 	ndn->dn_handle = odn->dn_handle;
656*12684STom.Erickson@Sun.COM 	ndn->dn_phys = odn->dn_phys;
657*12684STom.Erickson@Sun.COM 	ndn->dn_type = odn->dn_type;
658*12684STom.Erickson@Sun.COM 	ndn->dn_bonuslen = odn->dn_bonuslen;
659*12684STom.Erickson@Sun.COM 	ndn->dn_bonustype = odn->dn_bonustype;
660*12684STom.Erickson@Sun.COM 	ndn->dn_nblkptr = odn->dn_nblkptr;
661*12684STom.Erickson@Sun.COM 	ndn->dn_checksum = odn->dn_checksum;
662*12684STom.Erickson@Sun.COM 	ndn->dn_compress = odn->dn_compress;
663*12684STom.Erickson@Sun.COM 	ndn->dn_nlevels = odn->dn_nlevels;
664*12684STom.Erickson@Sun.COM 	ndn->dn_indblkshift = odn->dn_indblkshift;
665*12684STom.Erickson@Sun.COM 	ndn->dn_datablkshift = odn->dn_datablkshift;
666*12684STom.Erickson@Sun.COM 	ndn->dn_datablkszsec = odn->dn_datablkszsec;
667*12684STom.Erickson@Sun.COM 	ndn->dn_datablksz = odn->dn_datablksz;
668*12684STom.Erickson@Sun.COM 	ndn->dn_maxblkid = odn->dn_maxblkid;
669*12684STom.Erickson@Sun.COM 	bcopy(&odn->dn_next_nblkptr[0], &ndn->dn_next_nblkptr[0],
670*12684STom.Erickson@Sun.COM 	    sizeof (odn->dn_next_nblkptr));
671*12684STom.Erickson@Sun.COM 	bcopy(&odn->dn_next_nlevels[0], &ndn->dn_next_nlevels[0],
672*12684STom.Erickson@Sun.COM 	    sizeof (odn->dn_next_nlevels));
673*12684STom.Erickson@Sun.COM 	bcopy(&odn->dn_next_indblkshift[0], &ndn->dn_next_indblkshift[0],
674*12684STom.Erickson@Sun.COM 	    sizeof (odn->dn_next_indblkshift));
675*12684STom.Erickson@Sun.COM 	bcopy(&odn->dn_next_bonustype[0], &ndn->dn_next_bonustype[0],
676*12684STom.Erickson@Sun.COM 	    sizeof (odn->dn_next_bonustype));
677*12684STom.Erickson@Sun.COM 	bcopy(&odn->dn_rm_spillblk[0], &ndn->dn_rm_spillblk[0],
678*12684STom.Erickson@Sun.COM 	    sizeof (odn->dn_rm_spillblk));
679*12684STom.Erickson@Sun.COM 	bcopy(&odn->dn_next_bonuslen[0], &ndn->dn_next_bonuslen[0],
680*12684STom.Erickson@Sun.COM 	    sizeof (odn->dn_next_bonuslen));
681*12684STom.Erickson@Sun.COM 	bcopy(&odn->dn_next_blksz[0], &ndn->dn_next_blksz[0],
682*12684STom.Erickson@Sun.COM 	    sizeof (odn->dn_next_blksz));
683*12684STom.Erickson@Sun.COM 	for (i = 0; i < TXG_SIZE; i++) {
684*12684STom.Erickson@Sun.COM 		list_move_tail(&ndn->dn_dirty_records[i],
685*12684STom.Erickson@Sun.COM 		    &odn->dn_dirty_records[i]);
686*12684STom.Erickson@Sun.COM 	}
687*12684STom.Erickson@Sun.COM 	bcopy(&odn->dn_ranges[0], &ndn->dn_ranges[0], sizeof (odn->dn_ranges));
688*12684STom.Erickson@Sun.COM 	ndn->dn_allocated_txg = odn->dn_allocated_txg;
689*12684STom.Erickson@Sun.COM 	ndn->dn_free_txg = odn->dn_free_txg;
690*12684STom.Erickson@Sun.COM 	ndn->dn_assigned_txg = odn->dn_assigned_txg;
691*12684STom.Erickson@Sun.COM 	ndn->dn_dirtyctx = odn->dn_dirtyctx;
692*12684STom.Erickson@Sun.COM 	ndn->dn_dirtyctx_firstset = odn->dn_dirtyctx_firstset;
693*12684STom.Erickson@Sun.COM 	ASSERT(refcount_count(&odn->dn_tx_holds) == 0);
694*12684STom.Erickson@Sun.COM 	refcount_transfer(&ndn->dn_holds, &odn->dn_holds);
695*12684STom.Erickson@Sun.COM 	ASSERT(list_is_empty(&ndn->dn_dbufs));
696*12684STom.Erickson@Sun.COM 	list_move_tail(&ndn->dn_dbufs, &odn->dn_dbufs);
697*12684STom.Erickson@Sun.COM 	ndn->dn_dbufs_count = odn->dn_dbufs_count;
698*12684STom.Erickson@Sun.COM 	ndn->dn_bonus = odn->dn_bonus;
699*12684STom.Erickson@Sun.COM 	ndn->dn_have_spill = odn->dn_have_spill;
700*12684STom.Erickson@Sun.COM 	ndn->dn_zio = odn->dn_zio;
701*12684STom.Erickson@Sun.COM 	ndn->dn_oldused = odn->dn_oldused;
702*12684STom.Erickson@Sun.COM 	ndn->dn_oldflags = odn->dn_oldflags;
703*12684STom.Erickson@Sun.COM 	ndn->dn_olduid = odn->dn_olduid;
704*12684STom.Erickson@Sun.COM 	ndn->dn_oldgid = odn->dn_oldgid;
705*12684STom.Erickson@Sun.COM 	ndn->dn_newuid = odn->dn_newuid;
706*12684STom.Erickson@Sun.COM 	ndn->dn_newgid = odn->dn_newgid;
707*12684STom.Erickson@Sun.COM 	ndn->dn_id_flags = odn->dn_id_flags;
708*12684STom.Erickson@Sun.COM 	dmu_zfetch_init(&ndn->dn_zfetch, NULL);
709*12684STom.Erickson@Sun.COM 	list_move_tail(&ndn->dn_zfetch.zf_stream, &odn->dn_zfetch.zf_stream);
710*12684STom.Erickson@Sun.COM 	ndn->dn_zfetch.zf_dnode = odn->dn_zfetch.zf_dnode;
711*12684STom.Erickson@Sun.COM 	ndn->dn_zfetch.zf_stream_cnt = odn->dn_zfetch.zf_stream_cnt;
712*12684STom.Erickson@Sun.COM 	ndn->dn_zfetch.zf_alloc_fail = odn->dn_zfetch.zf_alloc_fail;
713*12684STom.Erickson@Sun.COM 
714*12684STom.Erickson@Sun.COM 	/*
715*12684STom.Erickson@Sun.COM 	 * Update back pointers. Updating the handle fixes the back pointer of
716*12684STom.Erickson@Sun.COM 	 * every descendant dbuf as well as the bonus dbuf.
717*12684STom.Erickson@Sun.COM 	 */
718*12684STom.Erickson@Sun.COM 	ASSERT(ndn->dn_handle->dnh_dnode == odn);
719*12684STom.Erickson@Sun.COM 	ndn->dn_handle->dnh_dnode = ndn;
720*12684STom.Erickson@Sun.COM 	if (ndn->dn_zfetch.zf_dnode == odn) {
721*12684STom.Erickson@Sun.COM 		ndn->dn_zfetch.zf_dnode = ndn;
722*12684STom.Erickson@Sun.COM 	}
723*12684STom.Erickson@Sun.COM 
724*12684STom.Erickson@Sun.COM 	/*
725*12684STom.Erickson@Sun.COM 	 * Invalidate the original dnode by clearing all of its back pointers.
726*12684STom.Erickson@Sun.COM 	 */
727*12684STom.Erickson@Sun.COM 	odn->dn_dbuf = NULL;
728*12684STom.Erickson@Sun.COM 	odn->dn_handle = NULL;
729*12684STom.Erickson@Sun.COM 	list_create(&odn->dn_dbufs, sizeof (dmu_buf_impl_t),
730*12684STom.Erickson@Sun.COM 	    offsetof(dmu_buf_impl_t, db_link));
731*12684STom.Erickson@Sun.COM 	odn->dn_dbufs_count = 0;
732*12684STom.Erickson@Sun.COM 	odn->dn_bonus = NULL;
733*12684STom.Erickson@Sun.COM 	odn->dn_zfetch.zf_dnode = NULL;
734*12684STom.Erickson@Sun.COM 
735*12684STom.Erickson@Sun.COM 	/*
736*12684STom.Erickson@Sun.COM 	 * Set the low bit of the objset pointer to ensure that dnode_move()
737*12684STom.Erickson@Sun.COM 	 * recognizes the dnode as invalid in any subsequent callback.
738*12684STom.Erickson@Sun.COM 	 */
739*12684STom.Erickson@Sun.COM 	POINTER_INVALIDATE(&odn->dn_objset);
740*12684STom.Erickson@Sun.COM 
741*12684STom.Erickson@Sun.COM 	/*
742*12684STom.Erickson@Sun.COM 	 * Satisfy the destructor.
743*12684STom.Erickson@Sun.COM 	 */
744*12684STom.Erickson@Sun.COM 	for (i = 0; i < TXG_SIZE; i++) {
745*12684STom.Erickson@Sun.COM 		list_create(&odn->dn_dirty_records[i],
746*12684STom.Erickson@Sun.COM 		    sizeof (dbuf_dirty_record_t),
747*12684STom.Erickson@Sun.COM 		    offsetof(dbuf_dirty_record_t, dr_dirty_node));
748*12684STom.Erickson@Sun.COM 		odn->dn_ranges[i].avl_root = NULL;
749*12684STom.Erickson@Sun.COM 		odn->dn_ranges[i].avl_numnodes = 0;
750*12684STom.Erickson@Sun.COM 		odn->dn_next_nlevels[i] = 0;
751*12684STom.Erickson@Sun.COM 		odn->dn_next_indblkshift[i] = 0;
752*12684STom.Erickson@Sun.COM 		odn->dn_next_bonustype[i] = 0;
753*12684STom.Erickson@Sun.COM 		odn->dn_rm_spillblk[i] = 0;
754*12684STom.Erickson@Sun.COM 		odn->dn_next_bonuslen[i] = 0;
755*12684STom.Erickson@Sun.COM 		odn->dn_next_blksz[i] = 0;
756*12684STom.Erickson@Sun.COM 	}
757*12684STom.Erickson@Sun.COM 	odn->dn_allocated_txg = 0;
758*12684STom.Erickson@Sun.COM 	odn->dn_free_txg = 0;
759*12684STom.Erickson@Sun.COM 	odn->dn_assigned_txg = 0;
760*12684STom.Erickson@Sun.COM 	odn->dn_dirtyctx = 0;
761*12684STom.Erickson@Sun.COM 	odn->dn_dirtyctx_firstset = NULL;
762*12684STom.Erickson@Sun.COM 	odn->dn_have_spill = B_FALSE;
763*12684STom.Erickson@Sun.COM 	odn->dn_zio = NULL;
764*12684STom.Erickson@Sun.COM 	odn->dn_oldused = 0;
765*12684STom.Erickson@Sun.COM 	odn->dn_oldflags = 0;
766*12684STom.Erickson@Sun.COM 	odn->dn_olduid = 0;
767*12684STom.Erickson@Sun.COM 	odn->dn_oldgid = 0;
768*12684STom.Erickson@Sun.COM 	odn->dn_newuid = 0;
769*12684STom.Erickson@Sun.COM 	odn->dn_newgid = 0;
770*12684STom.Erickson@Sun.COM 	odn->dn_id_flags = 0;
771*12684STom.Erickson@Sun.COM 
772*12684STom.Erickson@Sun.COM 	/*
773*12684STom.Erickson@Sun.COM 	 * Mark the dnode.
774*12684STom.Erickson@Sun.COM 	 */
775*12684STom.Erickson@Sun.COM 	ndn->dn_moved = 1;
776*12684STom.Erickson@Sun.COM 	odn->dn_moved = (uint8_t)-1;
777*12684STom.Erickson@Sun.COM }
778*12684STom.Erickson@Sun.COM 
779*12684STom.Erickson@Sun.COM #ifdef	_KERNEL
780*12684STom.Erickson@Sun.COM /*ARGSUSED*/
781*12684STom.Erickson@Sun.COM static kmem_cbrc_t
dnode_move(void * buf,void * newbuf,size_t size,void * arg)782*12684STom.Erickson@Sun.COM dnode_move(void *buf, void *newbuf, size_t size, void *arg)
783*12684STom.Erickson@Sun.COM {
784*12684STom.Erickson@Sun.COM 	dnode_t *odn = buf, *ndn = newbuf;
785*12684STom.Erickson@Sun.COM 	objset_t *os;
786*12684STom.Erickson@Sun.COM 	int64_t refcount;
787*12684STom.Erickson@Sun.COM 	uint32_t dbufs;
788*12684STom.Erickson@Sun.COM 
789*12684STom.Erickson@Sun.COM 	/*
790*12684STom.Erickson@Sun.COM 	 * The dnode is on the objset's list of known dnodes if the objset
791*12684STom.Erickson@Sun.COM 	 * pointer is valid. We set the low bit of the objset pointer when
792*12684STom.Erickson@Sun.COM 	 * freeing the dnode to invalidate it, and the memory patterns written
793*12684STom.Erickson@Sun.COM 	 * by kmem (baddcafe and deadbeef) set at least one of the two low bits.
794*12684STom.Erickson@Sun.COM 	 * A newly created dnode sets the objset pointer last of all to indicate
795*12684STom.Erickson@Sun.COM 	 * that the dnode is known and in a valid state to be moved by this
796*12684STom.Erickson@Sun.COM 	 * function.
797*12684STom.Erickson@Sun.COM 	 */
798*12684STom.Erickson@Sun.COM 	os = odn->dn_objset;
799*12684STom.Erickson@Sun.COM 	if (!POINTER_IS_VALID(os)) {
800*12684STom.Erickson@Sun.COM 		DNODE_STAT_ADD(dnode_move_stats.dms_dnode_invalid);
801*12684STom.Erickson@Sun.COM 		return (KMEM_CBRC_DONT_KNOW);
802*12684STom.Erickson@Sun.COM 	}
803*12684STom.Erickson@Sun.COM 
804*12684STom.Erickson@Sun.COM 	/*
805*12684STom.Erickson@Sun.COM 	 * Ensure that the objset does not go away during the move.
806*12684STom.Erickson@Sun.COM 	 */
807*12684STom.Erickson@Sun.COM 	rw_enter(&os_lock, RW_WRITER);
808*12684STom.Erickson@Sun.COM 	if (os != odn->dn_objset) {
809*12684STom.Erickson@Sun.COM 		rw_exit(&os_lock);
810*12684STom.Erickson@Sun.COM 		DNODE_STAT_ADD(dnode_move_stats.dms_dnode_recheck1);
811*12684STom.Erickson@Sun.COM 		return (KMEM_CBRC_DONT_KNOW);
812*12684STom.Erickson@Sun.COM 	}
813*12684STom.Erickson@Sun.COM 
814*12684STom.Erickson@Sun.COM 	/*
815*12684STom.Erickson@Sun.COM 	 * If the dnode is still valid, then so is the objset. We know that no
816*12684STom.Erickson@Sun.COM 	 * valid objset can be freed while we hold os_lock, so we can safely
817*12684STom.Erickson@Sun.COM 	 * ensure that the objset remains in use.
818*12684STom.Erickson@Sun.COM 	 */
819*12684STom.Erickson@Sun.COM 	mutex_enter(&os->os_lock);
820*12684STom.Erickson@Sun.COM 
821*12684STom.Erickson@Sun.COM 	/*
822*12684STom.Erickson@Sun.COM 	 * Recheck the objset pointer in case the dnode was removed just before
823*12684STom.Erickson@Sun.COM 	 * acquiring the lock.
824*12684STom.Erickson@Sun.COM 	 */
825*12684STom.Erickson@Sun.COM 	if (os != odn->dn_objset) {
826*12684STom.Erickson@Sun.COM 		mutex_exit(&os->os_lock);
827*12684STom.Erickson@Sun.COM 		rw_exit(&os_lock);
828*12684STom.Erickson@Sun.COM 		DNODE_STAT_ADD(dnode_move_stats.dms_dnode_recheck2);
829*12684STom.Erickson@Sun.COM 		return (KMEM_CBRC_DONT_KNOW);
830*12684STom.Erickson@Sun.COM 	}
831*12684STom.Erickson@Sun.COM 
832*12684STom.Erickson@Sun.COM 	/*
833*12684STom.Erickson@Sun.COM 	 * At this point we know that as long as we hold os->os_lock, the dnode
834*12684STom.Erickson@Sun.COM 	 * cannot be freed and fields within the dnode can be safely accessed.
835*12684STom.Erickson@Sun.COM 	 * The objset listing this dnode cannot go away as long as this dnode is
836*12684STom.Erickson@Sun.COM 	 * on its list.
837*12684STom.Erickson@Sun.COM 	 */
838*12684STom.Erickson@Sun.COM 	rw_exit(&os_lock);
839*12684STom.Erickson@Sun.COM 	if (DMU_OBJECT_IS_SPECIAL(odn->dn_object)) {
840*12684STom.Erickson@Sun.COM 		mutex_exit(&os->os_lock);
841*12684STom.Erickson@Sun.COM 		DNODE_STAT_ADD(dnode_move_stats.dms_dnode_special);
842*12684STom.Erickson@Sun.COM 		return (KMEM_CBRC_NO);
843*12684STom.Erickson@Sun.COM 	}
844*12684STom.Erickson@Sun.COM 	ASSERT(odn->dn_dbuf != NULL); /* only "special" dnodes have no parent */
845*12684STom.Erickson@Sun.COM 
846*12684STom.Erickson@Sun.COM 	/*
847*12684STom.Erickson@Sun.COM 	 * Lock the dnode handle to prevent the dnode from obtaining any new
848*12684STom.Erickson@Sun.COM 	 * holds. This also prevents the descendant dbufs and the bonus dbuf
849*12684STom.Erickson@Sun.COM 	 * from accessing the dnode, so that we can discount their holds. The
850*12684STom.Erickson@Sun.COM 	 * handle is safe to access because we know that while the dnode cannot
851*12684STom.Erickson@Sun.COM 	 * go away, neither can its handle. Once we hold dnh_zrlock, we can
852*12684STom.Erickson@Sun.COM 	 * safely move any dnode referenced only by dbufs.
853*12684STom.Erickson@Sun.COM 	 */
854*12684STom.Erickson@Sun.COM 	if (!zrl_tryenter(&odn->dn_handle->dnh_zrlock)) {
855*12684STom.Erickson@Sun.COM 		mutex_exit(&os->os_lock);
856*12684STom.Erickson@Sun.COM 		DNODE_STAT_ADD(dnode_move_stats.dms_dnode_handle);
857*12684STom.Erickson@Sun.COM 		return (KMEM_CBRC_LATER);
858*12684STom.Erickson@Sun.COM 	}
859*12684STom.Erickson@Sun.COM 
860*12684STom.Erickson@Sun.COM 	/*
861*12684STom.Erickson@Sun.COM 	 * Ensure a consistent view of the dnode's holds and the dnode's dbufs.
862*12684STom.Erickson@Sun.COM 	 * We need to guarantee that there is a hold for every dbuf in order to
863*12684STom.Erickson@Sun.COM 	 * determine whether the dnode is actively referenced. Falsely matching
864*12684STom.Erickson@Sun.COM 	 * a dbuf to an active hold would lead to an unsafe move. It's possible
865*12684STom.Erickson@Sun.COM 	 * that a thread already having an active dnode hold is about to add a
866*12684STom.Erickson@Sun.COM 	 * dbuf, and we can't compare hold and dbuf counts while the add is in
867*12684STom.Erickson@Sun.COM 	 * progress.
868*12684STom.Erickson@Sun.COM 	 */
869*12684STom.Erickson@Sun.COM 	if (!rw_tryenter(&odn->dn_struct_rwlock, RW_WRITER)) {
870*12684STom.Erickson@Sun.COM 		zrl_exit(&odn->dn_handle->dnh_zrlock);
871*12684STom.Erickson@Sun.COM 		mutex_exit(&os->os_lock);
872*12684STom.Erickson@Sun.COM 		DNODE_STAT_ADD(dnode_move_stats.dms_dnode_rwlock);
873*12684STom.Erickson@Sun.COM 		return (KMEM_CBRC_LATER);
874*12684STom.Erickson@Sun.COM 	}
875*12684STom.Erickson@Sun.COM 
876*12684STom.Erickson@Sun.COM 	/*
877*12684STom.Erickson@Sun.COM 	 * A dbuf may be removed (evicted) without an active dnode hold. In that
878*12684STom.Erickson@Sun.COM 	 * case, the dbuf count is decremented under the handle lock before the
879*12684STom.Erickson@Sun.COM 	 * dbuf's hold is released. This order ensures that if we count the hold
880*12684STom.Erickson@Sun.COM 	 * after the dbuf is removed but before its hold is released, we will
881*12684STom.Erickson@Sun.COM 	 * treat the unmatched hold as active and exit safely. If we count the
882*12684STom.Erickson@Sun.COM 	 * hold before the dbuf is removed, the hold is discounted, and the
883*12684STom.Erickson@Sun.COM 	 * removal is blocked until the move completes.
884*12684STom.Erickson@Sun.COM 	 */
885*12684STom.Erickson@Sun.COM 	refcount = refcount_count(&odn->dn_holds);
886*12684STom.Erickson@Sun.COM 	ASSERT(refcount >= 0);
887*12684STom.Erickson@Sun.COM 	dbufs = odn->dn_dbufs_count;
888*12684STom.Erickson@Sun.COM 
889*12684STom.Erickson@Sun.COM 	/* We can't have more dbufs than dnode holds. */
890*12684STom.Erickson@Sun.COM 	ASSERT3U(dbufs, <=, refcount);
891*12684STom.Erickson@Sun.COM 	DTRACE_PROBE3(dnode__move, dnode_t *, odn, int64_t, refcount,
892*12684STom.Erickson@Sun.COM 	    uint32_t, dbufs);
893*12684STom.Erickson@Sun.COM 
894*12684STom.Erickson@Sun.COM 	if (refcount > dbufs) {
895*12684STom.Erickson@Sun.COM 		rw_exit(&odn->dn_struct_rwlock);
896*12684STom.Erickson@Sun.COM 		zrl_exit(&odn->dn_handle->dnh_zrlock);
897*12684STom.Erickson@Sun.COM 		mutex_exit(&os->os_lock);
898*12684STom.Erickson@Sun.COM 		DNODE_STAT_ADD(dnode_move_stats.dms_dnode_active);
899*12684STom.Erickson@Sun.COM 		return (KMEM_CBRC_LATER);
900*12684STom.Erickson@Sun.COM 	}
901*12684STom.Erickson@Sun.COM 
902*12684STom.Erickson@Sun.COM 	rw_exit(&odn->dn_struct_rwlock);
903*12684STom.Erickson@Sun.COM 
904*12684STom.Erickson@Sun.COM 	/*
905*12684STom.Erickson@Sun.COM 	 * At this point we know that anyone with a hold on the dnode is not
906*12684STom.Erickson@Sun.COM 	 * actively referencing it. The dnode is known and in a valid state to
907*12684STom.Erickson@Sun.COM 	 * move. We're holding the locks needed to execute the critical section.
908*12684STom.Erickson@Sun.COM 	 */
909*12684STom.Erickson@Sun.COM 	dnode_move_impl(odn, ndn);
910*12684STom.Erickson@Sun.COM 
911*12684STom.Erickson@Sun.COM 	list_link_replace(&odn->dn_link, &ndn->dn_link);
912*12684STom.Erickson@Sun.COM 	/* If the dnode was safe to move, the refcount cannot have changed. */
913*12684STom.Erickson@Sun.COM 	ASSERT(refcount == refcount_count(&ndn->dn_holds));
914*12684STom.Erickson@Sun.COM 	ASSERT(dbufs == ndn->dn_dbufs_count);
915*12684STom.Erickson@Sun.COM 	zrl_exit(&ndn->dn_handle->dnh_zrlock); /* handle has moved */
916*12684STom.Erickson@Sun.COM 	mutex_exit(&os->os_lock);
917*12684STom.Erickson@Sun.COM 
918*12684STom.Erickson@Sun.COM 	return (KMEM_CBRC_YES);
919*12684STom.Erickson@Sun.COM }
920*12684STom.Erickson@Sun.COM #endif	/* _KERNEL */
921*12684STom.Erickson@Sun.COM 
922789Sahrens void
dnode_special_close(dnode_handle_t * dnh)923*12684STom.Erickson@Sun.COM dnode_special_close(dnode_handle_t *dnh)
924789Sahrens {
925*12684STom.Erickson@Sun.COM 	dnode_t *dn = dnh->dnh_dnode;
926*12684STom.Erickson@Sun.COM 
9271544Seschrock 	/*
9281544Seschrock 	 * Wait for final references to the dnode to clear.  This can
9291544Seschrock 	 * only happen if the arc is asyncronously evicting state that
9301544Seschrock 	 * has a hold on this dnode while we are trying to evict this
9311544Seschrock 	 * dnode.
9321544Seschrock 	 */
9331544Seschrock 	while (refcount_count(&dn->dn_holds) > 0)
9341544Seschrock 		delay(1);
935*12684STom.Erickson@Sun.COM 	zrl_add(&dnh->dnh_zrlock);
936*12684STom.Erickson@Sun.COM 	dnode_destroy(dn); /* implicit zrl_remove() */
937*12684STom.Erickson@Sun.COM 	zrl_destroy(&dnh->dnh_zrlock);
938*12684STom.Erickson@Sun.COM 	dnh->dnh_dnode = NULL;
939789Sahrens }
940789Sahrens 
941789Sahrens dnode_t *
dnode_special_open(objset_t * os,dnode_phys_t * dnp,uint64_t object,dnode_handle_t * dnh)942*12684STom.Erickson@Sun.COM dnode_special_open(objset_t *os, dnode_phys_t *dnp, uint64_t object,
943*12684STom.Erickson@Sun.COM     dnode_handle_t *dnh)
944789Sahrens {
945*12684STom.Erickson@Sun.COM 	dnode_t *dn = dnode_create(os, dnp, NULL, object, dnh);
946*12684STom.Erickson@Sun.COM 	dnh->dnh_dnode = dn;
947*12684STom.Erickson@Sun.COM 	zrl_init(&dnh->dnh_zrlock);
948873Sek110237 	DNODE_VERIFY(dn);
949789Sahrens 	return (dn);
950789Sahrens }
951789Sahrens 
952789Sahrens static void
dnode_buf_pageout(dmu_buf_t * db,void * arg)953789Sahrens dnode_buf_pageout(dmu_buf_t *db, void *arg)
954789Sahrens {
955*12684STom.Erickson@Sun.COM 	dnode_children_t *children_dnodes = arg;
956789Sahrens 	int i;
957789Sahrens 	int epb = db->db_size >> DNODE_SHIFT;
958789Sahrens 
959*12684STom.Erickson@Sun.COM 	ASSERT(epb == children_dnodes->dnc_count);
960*12684STom.Erickson@Sun.COM 
961789Sahrens 	for (i = 0; i < epb; i++) {
962*12684STom.Erickson@Sun.COM 		dnode_handle_t *dnh = &children_dnodes->dnc_children[i];
963*12684STom.Erickson@Sun.COM 		dnode_t *dn;
964789Sahrens 
965*12684STom.Erickson@Sun.COM 		/*
966*12684STom.Erickson@Sun.COM 		 * The dnode handle lock guards against the dnode moving to
967*12684STom.Erickson@Sun.COM 		 * another valid address, so there is no need here to guard
968*12684STom.Erickson@Sun.COM 		 * against changes to or from NULL.
969*12684STom.Erickson@Sun.COM 		 */
970*12684STom.Erickson@Sun.COM 		if (dnh->dnh_dnode == NULL) {
971*12684STom.Erickson@Sun.COM 			zrl_destroy(&dnh->dnh_zrlock);
972789Sahrens 			continue;
973*12684STom.Erickson@Sun.COM 		}
974*12684STom.Erickson@Sun.COM 
975*12684STom.Erickson@Sun.COM 		zrl_add(&dnh->dnh_zrlock);
976*12684STom.Erickson@Sun.COM 		dn = dnh->dnh_dnode;
977789Sahrens 		/*
978789Sahrens 		 * If there are holds on this dnode, then there should
979789Sahrens 		 * be holds on the dnode's containing dbuf as well; thus
980*12684STom.Erickson@Sun.COM 		 * it wouldn't be eligible for eviction and this function
981789Sahrens 		 * would not have been called.
982789Sahrens 		 */
983789Sahrens 		ASSERT(refcount_is_zero(&dn->dn_holds));
984789Sahrens 		ASSERT(refcount_is_zero(&dn->dn_tx_holds));
985789Sahrens 
986*12684STom.Erickson@Sun.COM 		dnode_destroy(dn); /* implicit zrl_remove() */
987*12684STom.Erickson@Sun.COM 		zrl_destroy(&dnh->dnh_zrlock);
988*12684STom.Erickson@Sun.COM 		dnh->dnh_dnode = NULL;
989789Sahrens 	}
990*12684STom.Erickson@Sun.COM 	kmem_free(children_dnodes, sizeof (dnode_children_t) +
991*12684STom.Erickson@Sun.COM 	    (epb - 1) * sizeof (dnode_handle_t));
992789Sahrens }
993789Sahrens 
994789Sahrens /*
9951544Seschrock  * errors:
9961544Seschrock  * EINVAL - invalid object number.
9971544Seschrock  * EIO - i/o error.
9981544Seschrock  * succeeds even for free dnodes.
999789Sahrens  */
10001544Seschrock int
dnode_hold_impl(objset_t * os,uint64_t object,int flag,void * tag,dnode_t ** dnp)100110298SMatthew.Ahrens@Sun.COM dnode_hold_impl(objset_t *os, uint64_t object, int flag,
10021544Seschrock     void *tag, dnode_t **dnp)
1003789Sahrens {
10041544Seschrock 	int epb, idx, err;
1005789Sahrens 	int drop_struct_lock = FALSE;
10061544Seschrock 	int type;
1007789Sahrens 	uint64_t blk;
1008789Sahrens 	dnode_t *mdn, *dn;
1009789Sahrens 	dmu_buf_impl_t *db;
1010*12684STom.Erickson@Sun.COM 	dnode_children_t *children_dnodes;
1011*12684STom.Erickson@Sun.COM 	dnode_handle_t *dnh;
1012789Sahrens 
10137754SJeff.Bonwick@Sun.COM 	/*
10147754SJeff.Bonwick@Sun.COM 	 * If you are holding the spa config lock as writer, you shouldn't
101511958SGeorge.Wilson@Sun.COM 	 * be asking the DMU to do *anything* unless it's the root pool
101611958SGeorge.Wilson@Sun.COM 	 * which may require us to read from the root filesystem while
101711958SGeorge.Wilson@Sun.COM 	 * holding some (not all) of the locks as writer.
10187754SJeff.Bonwick@Sun.COM 	 */
101911958SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(os->os_spa, SCL_ALL, RW_WRITER) == 0 ||
102011958SGeorge.Wilson@Sun.COM 	    (spa_is_root(os->os_spa) &&
102112586SGeorge.Wilson@Sun.COM 	    spa_config_held(os->os_spa, SCL_STATE, RW_WRITER)));
10227754SJeff.Bonwick@Sun.COM 
10239396SMatthew.Ahrens@Sun.COM 	if (object == DMU_USERUSED_OBJECT || object == DMU_GROUPUSED_OBJECT) {
10249396SMatthew.Ahrens@Sun.COM 		dn = (object == DMU_USERUSED_OBJECT) ?
1025*12684STom.Erickson@Sun.COM 		    DMU_USERUSED_DNODE(os) : DMU_GROUPUSED_DNODE(os);
10269396SMatthew.Ahrens@Sun.COM 		if (dn == NULL)
10279396SMatthew.Ahrens@Sun.COM 			return (ENOENT);
10289396SMatthew.Ahrens@Sun.COM 		type = dn->dn_type;
10299396SMatthew.Ahrens@Sun.COM 		if ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE)
10309396SMatthew.Ahrens@Sun.COM 			return (ENOENT);
10319396SMatthew.Ahrens@Sun.COM 		if ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)
10329396SMatthew.Ahrens@Sun.COM 			return (EEXIST);
10339396SMatthew.Ahrens@Sun.COM 		DNODE_VERIFY(dn);
10349396SMatthew.Ahrens@Sun.COM 		(void) refcount_add(&dn->dn_holds, tag);
10359396SMatthew.Ahrens@Sun.COM 		*dnp = dn;
10369396SMatthew.Ahrens@Sun.COM 		return (0);
10379396SMatthew.Ahrens@Sun.COM 	}
10389396SMatthew.Ahrens@Sun.COM 
1039789Sahrens 	if (object == 0 || object >= DN_MAX_OBJECT)
10401544Seschrock 		return (EINVAL);
1041789Sahrens 
1042*12684STom.Erickson@Sun.COM 	mdn = DMU_META_DNODE(os);
1043*12684STom.Erickson@Sun.COM 	ASSERT(mdn->dn_object == DMU_META_DNODE_OBJECT);
1044789Sahrens 
1045873Sek110237 	DNODE_VERIFY(mdn);
1046789Sahrens 
1047789Sahrens 	if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) {
1048789Sahrens 		rw_enter(&mdn->dn_struct_rwlock, RW_READER);
1049789Sahrens 		drop_struct_lock = TRUE;
1050789Sahrens 	}
1051789Sahrens 
1052789Sahrens 	blk = dbuf_whichblock(mdn, object * sizeof (dnode_phys_t));
1053789Sahrens 
10541544Seschrock 	db = dbuf_hold(mdn, blk, FTAG);
1055789Sahrens 	if (drop_struct_lock)
1056789Sahrens 		rw_exit(&mdn->dn_struct_rwlock);
10571544Seschrock 	if (db == NULL)
10581544Seschrock 		return (EIO);
10591544Seschrock 	err = dbuf_read(db, NULL, DB_RF_CANFAIL);
10601544Seschrock 	if (err) {
10611544Seschrock 		dbuf_rele(db, FTAG);
10621544Seschrock 		return (err);
10631544Seschrock 	}
1064789Sahrens 
1065789Sahrens 	ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT);
1066789Sahrens 	epb = db->db.db_size >> DNODE_SHIFT;
1067789Sahrens 
1068789Sahrens 	idx = object & (epb-1);
1069789Sahrens 
1070*12684STom.Erickson@Sun.COM 	ASSERT(DB_DNODE(db)->dn_type == DMU_OT_DNODE);
1071789Sahrens 	children_dnodes = dmu_buf_get_user(&db->db);
1072789Sahrens 	if (children_dnodes == NULL) {
1073*12684STom.Erickson@Sun.COM 		int i;
1074*12684STom.Erickson@Sun.COM 		dnode_children_t *winner;
1075*12684STom.Erickson@Sun.COM 		children_dnodes = kmem_alloc(sizeof (dnode_children_t) +
1076*12684STom.Erickson@Sun.COM 		    (epb - 1) * sizeof (dnode_handle_t), KM_SLEEP);
1077*12684STom.Erickson@Sun.COM 		children_dnodes->dnc_count = epb;
1078*12684STom.Erickson@Sun.COM 		dnh = &children_dnodes->dnc_children[0];
1079*12684STom.Erickson@Sun.COM 		for (i = 0; i < epb; i++) {
1080*12684STom.Erickson@Sun.COM 			zrl_init(&dnh[i].dnh_zrlock);
1081*12684STom.Erickson@Sun.COM 			dnh[i].dnh_dnode = NULL;
1082*12684STom.Erickson@Sun.COM 		}
1083789Sahrens 		if (winner = dmu_buf_set_user(&db->db, children_dnodes, NULL,
1084789Sahrens 		    dnode_buf_pageout)) {
1085*12684STom.Erickson@Sun.COM 			kmem_free(children_dnodes, sizeof (dnode_children_t) +
1086*12684STom.Erickson@Sun.COM 			    (epb - 1) * sizeof (dnode_handle_t));
1087789Sahrens 			children_dnodes = winner;
1088789Sahrens 		}
1089789Sahrens 	}
1090*12684STom.Erickson@Sun.COM 	ASSERT(children_dnodes->dnc_count == epb);
1091789Sahrens 
1092*12684STom.Erickson@Sun.COM 	dnh = &children_dnodes->dnc_children[idx];
1093*12684STom.Erickson@Sun.COM 	zrl_add(&dnh->dnh_zrlock);
1094*12684STom.Erickson@Sun.COM 	if ((dn = dnh->dnh_dnode) == NULL) {
1095*12684STom.Erickson@Sun.COM 		dnode_phys_t *phys = (dnode_phys_t *)db->db.db_data+idx;
1096789Sahrens 		dnode_t *winner;
10974309Smaybee 
1098*12684STom.Erickson@Sun.COM 		dn = dnode_create(os, phys, db, object, dnh);
1099*12684STom.Erickson@Sun.COM 		winner = atomic_cas_ptr(&dnh->dnh_dnode, NULL, dn);
1100789Sahrens 		if (winner != NULL) {
1101*12684STom.Erickson@Sun.COM 			zrl_add(&dnh->dnh_zrlock);
1102*12684STom.Erickson@Sun.COM 			dnode_destroy(dn); /* implicit zrl_remove() */
1103789Sahrens 			dn = winner;
1104789Sahrens 		}
1105789Sahrens 	}
1106789Sahrens 
1107789Sahrens 	mutex_enter(&dn->dn_mtx);
11081544Seschrock 	type = dn->dn_type;
1109789Sahrens 	if (dn->dn_free_txg ||
11101544Seschrock 	    ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) ||
11119396SMatthew.Ahrens@Sun.COM 	    ((flag & DNODE_MUST_BE_FREE) &&
111212432SMark.Shellenbaum@Sun.COM 	    (type != DMU_OT_NONE || !refcount_is_zero(&dn->dn_holds)))) {
1113789Sahrens 		mutex_exit(&dn->dn_mtx);
1114*12684STom.Erickson@Sun.COM 		zrl_remove(&dnh->dnh_zrlock);
11151544Seschrock 		dbuf_rele(db, FTAG);
11161544Seschrock 		return (type == DMU_OT_NONE ? ENOENT : EEXIST);
1117789Sahrens 	}
1118789Sahrens 	mutex_exit(&dn->dn_mtx);
1119789Sahrens 
11201544Seschrock 	if (refcount_add(&dn->dn_holds, tag) == 1)
1121*12684STom.Erickson@Sun.COM 		dbuf_add_ref(db, dnh);
1122*12684STom.Erickson@Sun.COM 	/* Now we can rely on the hold to prevent the dnode from moving. */
1123*12684STom.Erickson@Sun.COM 	zrl_remove(&dnh->dnh_zrlock);
1124789Sahrens 
1125873Sek110237 	DNODE_VERIFY(dn);
1126789Sahrens 	ASSERT3P(dn->dn_dbuf, ==, db);
1127789Sahrens 	ASSERT3U(dn->dn_object, ==, object);
11281544Seschrock 	dbuf_rele(db, FTAG);
1129789Sahrens 
11301544Seschrock 	*dnp = dn;
11311544Seschrock 	return (0);
1132789Sahrens }
1133789Sahrens 
1134789Sahrens /*
1135789Sahrens  * Return held dnode if the object is allocated, NULL if not.
1136789Sahrens  */
11371544Seschrock int
dnode_hold(objset_t * os,uint64_t object,void * tag,dnode_t ** dnp)113810298SMatthew.Ahrens@Sun.COM dnode_hold(objset_t *os, uint64_t object, void *tag, dnode_t **dnp)
1139789Sahrens {
11401544Seschrock 	return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, tag, dnp));
1141789Sahrens }
1142789Sahrens 
11434944Smaybee /*
11444944Smaybee  * Can only add a reference if there is already at least one
11454944Smaybee  * reference on the dnode.  Returns FALSE if unable to add a
11464944Smaybee  * new reference.
11474944Smaybee  */
11484944Smaybee boolean_t
dnode_add_ref(dnode_t * dn,void * tag)11491544Seschrock dnode_add_ref(dnode_t *dn, void *tag)
1150789Sahrens {
11514944Smaybee 	mutex_enter(&dn->dn_mtx);
11524944Smaybee 	if (refcount_is_zero(&dn->dn_holds)) {
11534944Smaybee 		mutex_exit(&dn->dn_mtx);
11544944Smaybee 		return (FALSE);
11554944Smaybee 	}
11564944Smaybee 	VERIFY(1 < refcount_add(&dn->dn_holds, tag));
11574944Smaybee 	mutex_exit(&dn->dn_mtx);
11584944Smaybee 	return (TRUE);
1159789Sahrens }
1160789Sahrens 
1161789Sahrens void
dnode_rele(dnode_t * dn,void * tag)11621544Seschrock dnode_rele(dnode_t *dn, void *tag)
1163789Sahrens {
1164789Sahrens 	uint64_t refs;
1165*12684STom.Erickson@Sun.COM 	/* Get while the hold prevents the dnode from moving. */
1166*12684STom.Erickson@Sun.COM 	dmu_buf_impl_t *db = dn->dn_dbuf;
1167*12684STom.Erickson@Sun.COM 	dnode_handle_t *dnh = dn->dn_handle;
1168789Sahrens 
11694944Smaybee 	mutex_enter(&dn->dn_mtx);
11701544Seschrock 	refs = refcount_remove(&dn->dn_holds, tag);
11714944Smaybee 	mutex_exit(&dn->dn_mtx);
1172*12684STom.Erickson@Sun.COM 
1173*12684STom.Erickson@Sun.COM 	/*
1174*12684STom.Erickson@Sun.COM 	 * It's unsafe to release the last hold on a dnode by dnode_rele() or
1175*12684STom.Erickson@Sun.COM 	 * indirectly by dbuf_rele() while relying on the dnode handle to
1176*12684STom.Erickson@Sun.COM 	 * prevent the dnode from moving, since releasing the last hold could
1177*12684STom.Erickson@Sun.COM 	 * result in the dnode's parent dbuf evicting its dnode handles. For
1178*12684STom.Erickson@Sun.COM 	 * that reason anyone calling dnode_rele() or dbuf_rele() without some
1179*12684STom.Erickson@Sun.COM 	 * other direct or indirect hold on the dnode must first drop the dnode
1180*12684STom.Erickson@Sun.COM 	 * handle.
1181*12684STom.Erickson@Sun.COM 	 */
1182*12684STom.Erickson@Sun.COM 	ASSERT(refs > 0 || dnh->dnh_zrlock.zr_owner != curthread);
1183*12684STom.Erickson@Sun.COM 
1184789Sahrens 	/* NOTE: the DNODE_DNODE does not have a dn_dbuf */
1185*12684STom.Erickson@Sun.COM 	if (refs == 0 && db != NULL) {
1186*12684STom.Erickson@Sun.COM 		/*
1187*12684STom.Erickson@Sun.COM 		 * Another thread could add a hold to the dnode handle in
1188*12684STom.Erickson@Sun.COM 		 * dnode_hold_impl() while holding the parent dbuf. Since the
1189*12684STom.Erickson@Sun.COM 		 * hold on the parent dbuf prevents the handle from being
1190*12684STom.Erickson@Sun.COM 		 * destroyed, the hold on the handle is OK. We can't yet assert
1191*12684STom.Erickson@Sun.COM 		 * that the handle has zero references, but that will be
1192*12684STom.Erickson@Sun.COM 		 * asserted anyway when the handle gets destroyed.
1193*12684STom.Erickson@Sun.COM 		 */
1194*12684STom.Erickson@Sun.COM 		dbuf_rele(db, dnh);
1195*12684STom.Erickson@Sun.COM 	}
1196789Sahrens }
1197789Sahrens 
1198789Sahrens void
dnode_setdirty(dnode_t * dn,dmu_tx_t * tx)1199789Sahrens dnode_setdirty(dnode_t *dn, dmu_tx_t *tx)
1200789Sahrens {
120110298SMatthew.Ahrens@Sun.COM 	objset_t *os = dn->dn_objset;
1202789Sahrens 	uint64_t txg = tx->tx_txg;
1203789Sahrens 
12049396SMatthew.Ahrens@Sun.COM 	if (DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
12059396SMatthew.Ahrens@Sun.COM 		dsl_dataset_dirty(os->os_dsl_dataset, tx);
1206789Sahrens 		return;
12079396SMatthew.Ahrens@Sun.COM 	}
1208789Sahrens 
1209873Sek110237 	DNODE_VERIFY(dn);
1210789Sahrens 
1211789Sahrens #ifdef ZFS_DEBUG
1212789Sahrens 	mutex_enter(&dn->dn_mtx);
1213789Sahrens 	ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg);
1214*12684STom.Erickson@Sun.COM 	ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg);
1215789Sahrens 	mutex_exit(&dn->dn_mtx);
1216789Sahrens #endif
1217789Sahrens 
121811935SMark.Shellenbaum@Sun.COM 	/*
121911935SMark.Shellenbaum@Sun.COM 	 * Determine old uid/gid when necessary
122011935SMark.Shellenbaum@Sun.COM 	 */
122112178SMark.Shellenbaum@Sun.COM 	dmu_objset_userquota_get_ids(dn, B_TRUE, tx);
122211935SMark.Shellenbaum@Sun.COM 
1223789Sahrens 	mutex_enter(&os->os_lock);
1224789Sahrens 
1225789Sahrens 	/*
1226789Sahrens 	 * If we are already marked dirty, we're done.
1227789Sahrens 	 */
12281596Sahrens 	if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) {
1229789Sahrens 		mutex_exit(&os->os_lock);
1230789Sahrens 		return;
1231789Sahrens 	}
1232789Sahrens 
1233789Sahrens 	ASSERT(!refcount_is_zero(&dn->dn_holds) || list_head(&dn->dn_dbufs));
1234789Sahrens 	ASSERT(dn->dn_datablksz != 0);
12354944Smaybee 	ASSERT3U(dn->dn_next_bonuslen[txg&TXG_MASK], ==, 0);
12361599Sahrens 	ASSERT3U(dn->dn_next_blksz[txg&TXG_MASK], ==, 0);
123711935SMark.Shellenbaum@Sun.COM 	ASSERT3U(dn->dn_next_bonustype[txg&TXG_MASK], ==, 0);
1238789Sahrens 
1239789Sahrens 	dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
1240789Sahrens 	    dn->dn_object, txg);
1241789Sahrens 
1242789Sahrens 	if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) {
1243789Sahrens 		list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn);
1244789Sahrens 	} else {
1245789Sahrens 		list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn);
1246789Sahrens 	}
1247789Sahrens 
1248789Sahrens 	mutex_exit(&os->os_lock);
1249789Sahrens 
1250789Sahrens 	/*
1251789Sahrens 	 * The dnode maintains a hold on its containing dbuf as
1252789Sahrens 	 * long as there are holds on it.  Each instantiated child
1253*12684STom.Erickson@Sun.COM 	 * dbuf maintains a hold on the dnode.  When the last child
1254789Sahrens 	 * drops its hold, the dnode will drop its hold on the
1255789Sahrens 	 * containing dbuf. We add a "dirty hold" here so that the
1256789Sahrens 	 * dnode will hang around after we finish processing its
1257789Sahrens 	 * children.
1258789Sahrens 	 */
12594944Smaybee 	VERIFY(dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg));
1260789Sahrens 
12613547Smaybee 	(void) dbuf_dirty(dn->dn_dbuf, tx);
1262789Sahrens 
1263789Sahrens 	dsl_dataset_dirty(os->os_dsl_dataset, tx);
1264789Sahrens }
1265789Sahrens 
1266789Sahrens void
dnode_free(dnode_t * dn,dmu_tx_t * tx)1267789Sahrens dnode_free(dnode_t *dn, dmu_tx_t *tx)
1268789Sahrens {
12691596Sahrens 	int txgoff = tx->tx_txg & TXG_MASK;
12701596Sahrens 
1271789Sahrens 	dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg);
1272789Sahrens 
1273789Sahrens 	/* we should be the only holder... hopefully */
1274789Sahrens 	/* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */
1275789Sahrens 
1276789Sahrens 	mutex_enter(&dn->dn_mtx);
1277789Sahrens 	if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) {
1278789Sahrens 		mutex_exit(&dn->dn_mtx);
1279789Sahrens 		return;
1280789Sahrens 	}
1281789Sahrens 	dn->dn_free_txg = tx->tx_txg;
1282789Sahrens 	mutex_exit(&dn->dn_mtx);
1283789Sahrens 
1284789Sahrens 	/*
1285789Sahrens 	 * If the dnode is already dirty, it needs to be moved from
1286789Sahrens 	 * the dirty list to the free list.
1287789Sahrens 	 */
1288789Sahrens 	mutex_enter(&dn->dn_objset->os_lock);
12891596Sahrens 	if (list_link_active(&dn->dn_dirty_link[txgoff])) {
12901596Sahrens 		list_remove(&dn->dn_objset->os_dirty_dnodes[txgoff], dn);
12911596Sahrens 		list_insert_tail(&dn->dn_objset->os_free_dnodes[txgoff], dn);
1292789Sahrens 		mutex_exit(&dn->dn_objset->os_lock);
1293789Sahrens 	} else {
1294789Sahrens 		mutex_exit(&dn->dn_objset->os_lock);
1295789Sahrens 		dnode_setdirty(dn, tx);
1296789Sahrens 	}
1297789Sahrens }
1298789Sahrens 
1299789Sahrens /*
1300789Sahrens  * Try to change the block size for the indicated dnode.  This can only
1301789Sahrens  * succeed if there are no blocks allocated or dirty beyond first block
1302789Sahrens  */
1303789Sahrens int
dnode_set_blksz(dnode_t * dn,uint64_t size,int ibs,dmu_tx_t * tx)1304789Sahrens dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx)
1305789Sahrens {
1306789Sahrens 	dmu_buf_impl_t *db, *db_next;
13076992Smaybee 	int err;
1308789Sahrens 
1309789Sahrens 	if (size == 0)
1310789Sahrens 		size = SPA_MINBLOCKSIZE;
1311789Sahrens 	if (size > SPA_MAXBLOCKSIZE)
1312789Sahrens 		size = SPA_MAXBLOCKSIZE;
1313789Sahrens 	else
1314789Sahrens 		size = P2ROUNDUP(size, SPA_MINBLOCKSIZE);
1315789Sahrens 
13162445Sahrens 	if (ibs == dn->dn_indblkshift)
13172445Sahrens 		ibs = 0;
1318789Sahrens 
13192445Sahrens 	if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0)
1320789Sahrens 		return (0);
1321789Sahrens 
1322789Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1323789Sahrens 
1324789Sahrens 	/* Check for any allocated blocks beyond the first */
1325789Sahrens 	if (dn->dn_phys->dn_maxblkid != 0)
13262445Sahrens 		goto fail;
1327789Sahrens 
1328789Sahrens 	mutex_enter(&dn->dn_dbufs_mtx);
1329789Sahrens 	for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
1330789Sahrens 		db_next = list_next(&dn->dn_dbufs, db);
1331789Sahrens 
133211935SMark.Shellenbaum@Sun.COM 		if (db->db_blkid != 0 && db->db_blkid != DMU_BONUS_BLKID &&
133311935SMark.Shellenbaum@Sun.COM 		    db->db_blkid != DMU_SPILL_BLKID) {
1334789Sahrens 			mutex_exit(&dn->dn_dbufs_mtx);
13352445Sahrens 			goto fail;
1336789Sahrens 		}
1337789Sahrens 	}
1338789Sahrens 	mutex_exit(&dn->dn_dbufs_mtx);
1339789Sahrens 
13402445Sahrens 	if (ibs && dn->dn_nlevels != 1)
13412445Sahrens 		goto fail;
13422445Sahrens 
13436992Smaybee 	/* resize the old block */
13446992Smaybee 	err = dbuf_hold_impl(dn, 0, 0, TRUE, FTAG, &db);
13456992Smaybee 	if (err == 0)
13461596Sahrens 		dbuf_new_size(db, size, tx);
13476992Smaybee 	else if (err != ENOENT)
13486992Smaybee 		goto fail;
1349789Sahrens 
1350789Sahrens 	dnode_setdblksz(dn, size);
13511596Sahrens 	dnode_setdirty(dn, tx);
13521596Sahrens 	dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size;
13532445Sahrens 	if (ibs) {
13542445Sahrens 		dn->dn_indblkshift = ibs;
13552445Sahrens 		dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs;
13562445Sahrens 	}
13576992Smaybee 	/* rele after we have fixed the blocksize in the dnode */
13581596Sahrens 	if (db)
13591596Sahrens 		dbuf_rele(db, FTAG);
1360789Sahrens 
1361789Sahrens 	rw_exit(&dn->dn_struct_rwlock);
13622445Sahrens 	return (0);
13632445Sahrens 
13642445Sahrens fail:
13652445Sahrens 	rw_exit(&dn->dn_struct_rwlock);
13662445Sahrens 	return (ENOTSUP);
1367789Sahrens }
1368789Sahrens 
13697332SJonathan.Adams@Sun.COM /* read-holding callers must not rely on the lock being continuously held */
1370789Sahrens void
dnode_new_blkid(dnode_t * dn,uint64_t blkid,dmu_tx_t * tx,boolean_t have_read)13717332SJonathan.Adams@Sun.COM dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t have_read)
1372789Sahrens {
1373789Sahrens 	uint64_t txgoff = tx->tx_txg & TXG_MASK;
13741596Sahrens 	int epbs, new_nlevels;
1375789Sahrens 	uint64_t sz;
1376789Sahrens 
137711935SMark.Shellenbaum@Sun.COM 	ASSERT(blkid != DMU_BONUS_BLKID);
1378789Sahrens 
13797332SJonathan.Adams@Sun.COM 	ASSERT(have_read ?
13807332SJonathan.Adams@Sun.COM 	    RW_READ_HELD(&dn->dn_struct_rwlock) :
13817332SJonathan.Adams@Sun.COM 	    RW_WRITE_HELD(&dn->dn_struct_rwlock));
13827332SJonathan.Adams@Sun.COM 
13837332SJonathan.Adams@Sun.COM 	/*
13847332SJonathan.Adams@Sun.COM 	 * if we have a read-lock, check to see if we need to do any work
13857332SJonathan.Adams@Sun.COM 	 * before upgrading to a write-lock.
13867332SJonathan.Adams@Sun.COM 	 */
13877332SJonathan.Adams@Sun.COM 	if (have_read) {
13887332SJonathan.Adams@Sun.COM 		if (blkid <= dn->dn_maxblkid)
13897332SJonathan.Adams@Sun.COM 			return;
13907332SJonathan.Adams@Sun.COM 
13917332SJonathan.Adams@Sun.COM 		if (!rw_tryupgrade(&dn->dn_struct_rwlock)) {
13927332SJonathan.Adams@Sun.COM 			rw_exit(&dn->dn_struct_rwlock);
13937332SJonathan.Adams@Sun.COM 			rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
13947332SJonathan.Adams@Sun.COM 		}
1395789Sahrens 	}
1396789Sahrens 
13971596Sahrens 	if (blkid <= dn->dn_maxblkid)
13981596Sahrens 		goto out;
13991596Sahrens 
14001596Sahrens 	dn->dn_maxblkid = blkid;
1401789Sahrens 
1402789Sahrens 	/*
14031596Sahrens 	 * Compute the number of levels necessary to support the new maxblkid.
1404789Sahrens 	 */
1405789Sahrens 	new_nlevels = 1;
1406789Sahrens 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
14071596Sahrens 	for (sz = dn->dn_nblkptr;
14081596Sahrens 	    sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs)
1409789Sahrens 		new_nlevels++;
1410789Sahrens 
14111596Sahrens 	if (new_nlevels > dn->dn_nlevels) {
14121596Sahrens 		int old_nlevels = dn->dn_nlevels;
14131596Sahrens 		dmu_buf_impl_t *db;
14143547Smaybee 		list_t *list;
14153547Smaybee 		dbuf_dirty_record_t *new, *dr, *dr_next;
14161596Sahrens 
14171596Sahrens 		dn->dn_nlevels = new_nlevels;
14181596Sahrens 
14191596Sahrens 		ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]);
1420789Sahrens 		dn->dn_next_nlevels[txgoff] = new_nlevels;
1421789Sahrens 
14223547Smaybee 		/* dirty the left indirects */
14231596Sahrens 		db = dbuf_hold_level(dn, old_nlevels, 0, FTAG);
142412411SJohn.Harres@Sun.COM 		ASSERT(db != NULL);
14253547Smaybee 		new = dbuf_dirty(db, tx);
14261544Seschrock 		dbuf_rele(db, FTAG);
14271596Sahrens 
14283547Smaybee 		/* transfer the dirty records to the new indirect */
14293547Smaybee 		mutex_enter(&dn->dn_mtx);
14303547Smaybee 		mutex_enter(&new->dt.di.dr_mtx);
14313547Smaybee 		list = &dn->dn_dirty_records[txgoff];
14323547Smaybee 		for (dr = list_head(list); dr; dr = dr_next) {
14333547Smaybee 			dr_next = list_next(&dn->dn_dirty_records[txgoff], dr);
14343547Smaybee 			if (dr->dr_dbuf->db_level != new_nlevels-1 &&
143511935SMark.Shellenbaum@Sun.COM 			    dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
143611935SMark.Shellenbaum@Sun.COM 			    dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
14373547Smaybee 				ASSERT(dr->dr_dbuf->db_level == old_nlevels-1);
14383547Smaybee 				list_remove(&dn->dn_dirty_records[txgoff], dr);
14393547Smaybee 				list_insert_tail(&new->dt.di.dr_children, dr);
14403547Smaybee 				dr->dr_parent = new;
14413547Smaybee 			}
14423547Smaybee 		}
14433547Smaybee 		mutex_exit(&new->dt.di.dr_mtx);
14443547Smaybee 		mutex_exit(&dn->dn_mtx);
1445789Sahrens 	}
1446789Sahrens 
1447789Sahrens out:
14487332SJonathan.Adams@Sun.COM 	if (have_read)
14497332SJonathan.Adams@Sun.COM 		rw_downgrade(&dn->dn_struct_rwlock);
1450789Sahrens }
1451789Sahrens 
1452789Sahrens void
dnode_clear_range(dnode_t * dn,uint64_t blkid,uint64_t nblks,dmu_tx_t * tx)1453789Sahrens dnode_clear_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
1454789Sahrens {
1455789Sahrens 	avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
1456789Sahrens 	avl_index_t where;
1457789Sahrens 	free_range_t *rp;
1458789Sahrens 	free_range_t rp_tofind;
1459789Sahrens 	uint64_t endblk = blkid + nblks;
1460789Sahrens 
1461789Sahrens 	ASSERT(MUTEX_HELD(&dn->dn_mtx));
1462789Sahrens 	ASSERT(nblks <= UINT64_MAX - blkid); /* no overflow */
1463789Sahrens 
1464789Sahrens 	dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1465789Sahrens 	    blkid, nblks, tx->tx_txg);
1466789Sahrens 	rp_tofind.fr_blkid = blkid;
1467789Sahrens 	rp = avl_find(tree, &rp_tofind, &where);
1468789Sahrens 	if (rp == NULL)
1469789Sahrens 		rp = avl_nearest(tree, where, AVL_BEFORE);
1470789Sahrens 	if (rp == NULL)
1471789Sahrens 		rp = avl_nearest(tree, where, AVL_AFTER);
1472789Sahrens 
1473789Sahrens 	while (rp && (rp->fr_blkid <= blkid + nblks)) {
1474789Sahrens 		uint64_t fr_endblk = rp->fr_blkid + rp->fr_nblks;
1475789Sahrens 		free_range_t *nrp = AVL_NEXT(tree, rp);
1476789Sahrens 
1477789Sahrens 		if (blkid <= rp->fr_blkid && endblk >= fr_endblk) {
1478789Sahrens 			/* clear this entire range */
1479789Sahrens 			avl_remove(tree, rp);
1480789Sahrens 			kmem_free(rp, sizeof (free_range_t));
1481789Sahrens 		} else if (blkid <= rp->fr_blkid &&
1482789Sahrens 		    endblk > rp->fr_blkid && endblk < fr_endblk) {
1483789Sahrens 			/* clear the beginning of this range */
1484789Sahrens 			rp->fr_blkid = endblk;
1485789Sahrens 			rp->fr_nblks = fr_endblk - endblk;
1486789Sahrens 		} else if (blkid > rp->fr_blkid && blkid < fr_endblk &&
1487789Sahrens 		    endblk >= fr_endblk) {
1488789Sahrens 			/* clear the end of this range */
1489789Sahrens 			rp->fr_nblks = blkid - rp->fr_blkid;
1490789Sahrens 		} else if (blkid > rp->fr_blkid && endblk < fr_endblk) {
1491789Sahrens 			/* clear a chunk out of this range */
1492789Sahrens 			free_range_t *new_rp =
1493789Sahrens 			    kmem_alloc(sizeof (free_range_t), KM_SLEEP);
1494789Sahrens 
1495789Sahrens 			new_rp->fr_blkid = endblk;
1496789Sahrens 			new_rp->fr_nblks = fr_endblk - endblk;
1497789Sahrens 			avl_insert_here(tree, new_rp, rp, AVL_AFTER);
1498789Sahrens 			rp->fr_nblks = blkid - rp->fr_blkid;
1499789Sahrens 		}
1500789Sahrens 		/* there may be no overlap */
1501789Sahrens 		rp = nrp;
1502789Sahrens 	}
1503789Sahrens }
1504789Sahrens 
1505789Sahrens void
dnode_free_range(dnode_t * dn,uint64_t off,uint64_t len,dmu_tx_t * tx)1506789Sahrens dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx)
1507789Sahrens {
1508789Sahrens 	dmu_buf_impl_t *db;
15092445Sahrens 	uint64_t blkoff, blkid, nblks;
15106992Smaybee 	int blksz, blkshift, head, tail;
1511789Sahrens 	int trunc = FALSE;
15126992Smaybee 	int epbs;
1513789Sahrens 
1514789Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1515789Sahrens 	blksz = dn->dn_datablksz;
15166992Smaybee 	blkshift = dn->dn_datablkshift;
15176992Smaybee 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1518789Sahrens 
1519789Sahrens 	if (len == -1ULL) {
1520789Sahrens 		len = UINT64_MAX - off;
1521789Sahrens 		trunc = TRUE;
1522789Sahrens 	}
1523789Sahrens 
1524789Sahrens 	/*
1525789Sahrens 	 * First, block align the region to free:
1526789Sahrens 	 */
15272445Sahrens 	if (ISP2(blksz)) {
15282445Sahrens 		head = P2NPHASE(off, blksz);
15292445Sahrens 		blkoff = P2PHASE(off, blksz);
15306992Smaybee 		if ((off >> blkshift) > dn->dn_maxblkid)
15316992Smaybee 			goto out;
15322445Sahrens 	} else {
15332445Sahrens 		ASSERT(dn->dn_maxblkid == 0);
15342445Sahrens 		if (off == 0 && len >= blksz) {
15356992Smaybee 			/* Freeing the whole block; fast-track this request */
15366992Smaybee 			blkid = 0;
15376992Smaybee 			nblks = 1;
15386992Smaybee 			goto done;
15397385SMark.Maybee@Sun.COM 		} else if (off >= blksz) {
15406992Smaybee 			/* Freeing past end-of-data */
15416992Smaybee 			goto out;
1542789Sahrens 		} else {
15432445Sahrens 			/* Freeing part of the block. */
1544789Sahrens 			head = blksz - off;
1545789Sahrens 			ASSERT3U(head, >, 0);
1546789Sahrens 		}
15472445Sahrens 		blkoff = off;
1548789Sahrens 	}
1549789Sahrens 	/* zero out any partial block data at the start of the range */
1550789Sahrens 	if (head) {
15512445Sahrens 		ASSERT3U(blkoff + head, ==, blksz);
1552789Sahrens 		if (len < head)
1553789Sahrens 			head = len;
1554789Sahrens 		if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off), TRUE,
1555789Sahrens 		    FTAG, &db) == 0) {
1556789Sahrens 			caddr_t data;
1557789Sahrens 
1558789Sahrens 			/* don't dirty if it isn't on disk and isn't dirty */
15593547Smaybee 			if (db->db_last_dirty ||
1560789Sahrens 			    (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1561789Sahrens 				rw_exit(&dn->dn_struct_rwlock);
1562789Sahrens 				dbuf_will_dirty(db, tx);
1563789Sahrens 				rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1564789Sahrens 				data = db->db.db_data;
15652445Sahrens 				bzero(data + blkoff, head);
1566789Sahrens 			}
15671544Seschrock 			dbuf_rele(db, FTAG);
1568789Sahrens 		}
1569789Sahrens 		off += head;
1570789Sahrens 		len -= head;
1571789Sahrens 	}
1572789Sahrens 
15732445Sahrens 	/* If the range was less than one block, we're done */
15746992Smaybee 	if (len == 0)
15756992Smaybee 		goto out;
15766992Smaybee 
15776992Smaybee 	/* If the remaining range is past end of file, we're done */
15786992Smaybee 	if ((off >> blkshift) > dn->dn_maxblkid)
15796992Smaybee 		goto out;
15806992Smaybee 
15817385SMark.Maybee@Sun.COM 	ASSERT(ISP2(blksz));
15826992Smaybee 	if (trunc)
15836992Smaybee 		tail = 0;
15846992Smaybee 	else
15856992Smaybee 		tail = P2PHASE(len, blksz);
15866992Smaybee 
15876992Smaybee 	ASSERT3U(P2PHASE(off, blksz), ==, 0);
15886992Smaybee 	/* zero out any partial block data at the end of the range */
15896992Smaybee 	if (tail) {
15906992Smaybee 		if (len < tail)
15916992Smaybee 			tail = len;
15926992Smaybee 		if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len),
15936992Smaybee 		    TRUE, FTAG, &db) == 0) {
15946992Smaybee 			/* don't dirty if not on disk and not dirty */
15956992Smaybee 			if (db->db_last_dirty ||
15966992Smaybee 			    (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
15976992Smaybee 				rw_exit(&dn->dn_struct_rwlock);
15986992Smaybee 				dbuf_will_dirty(db, tx);
15996992Smaybee 				rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
16006992Smaybee 				bzero(db->db.db_data, tail);
16016992Smaybee 			}
16026992Smaybee 			dbuf_rele(db, FTAG);
16036992Smaybee 		}
16046992Smaybee 		len -= tail;
16056992Smaybee 	}
16066992Smaybee 
16076992Smaybee 	/* If the range did not include a full block, we are done */
16086992Smaybee 	if (len == 0)
1609789Sahrens 		goto out;
1610789Sahrens 
16116992Smaybee 	ASSERT(IS_P2ALIGNED(off, blksz));
16126992Smaybee 	ASSERT(trunc || IS_P2ALIGNED(len, blksz));
16136992Smaybee 	blkid = off >> blkshift;
16146992Smaybee 	nblks = len >> blkshift;
16156992Smaybee 	if (trunc)
16166992Smaybee 		nblks += 1;
16172445Sahrens 
16186992Smaybee 	/*
16196992Smaybee 	 * Read in and mark all the level-1 indirects dirty,
16206992Smaybee 	 * so that they will stay in memory until syncing phase.
16217049Smaybee 	 * Always dirty the first and last indirect to make sure
16227049Smaybee 	 * we dirty all the partial indirects.
16236992Smaybee 	 */
16246992Smaybee 	if (dn->dn_nlevels > 1) {
16256992Smaybee 		uint64_t i, first, last;
16266992Smaybee 		int shift = epbs + dn->dn_datablkshift;
16272445Sahrens 
16286992Smaybee 		first = blkid >> epbs;
16297049Smaybee 		if (db = dbuf_hold_level(dn, 1, first, FTAG)) {
16307049Smaybee 			dbuf_will_dirty(db, tx);
16317049Smaybee 			dbuf_rele(db, FTAG);
16327049Smaybee 		}
16336992Smaybee 		if (trunc)
16346992Smaybee 			last = dn->dn_maxblkid >> epbs;
16352445Sahrens 		else
16366992Smaybee 			last = (blkid + nblks - 1) >> epbs;
16377049Smaybee 		if (last > first && (db = dbuf_hold_level(dn, 1, last, FTAG))) {
16387049Smaybee 			dbuf_will_dirty(db, tx);
16397049Smaybee 			dbuf_rele(db, FTAG);
16407049Smaybee 		}
16417049Smaybee 		for (i = first + 1; i < last; i++) {
16426992Smaybee 			uint64_t ibyte = i << shift;
16436992Smaybee 			int err;
1644789Sahrens 
16456992Smaybee 			err = dnode_next_offset(dn,
16466992Smaybee 			    DNODE_FIND_HAVELOCK, &ibyte, 1, 1, 0);
16476992Smaybee 			i = ibyte >> shift;
16487049Smaybee 			if (err == ESRCH || i >= last)
16496992Smaybee 				break;
16506992Smaybee 			ASSERT(err == 0);
16516992Smaybee 			db = dbuf_hold_level(dn, 1, i, FTAG);
16526992Smaybee 			if (db) {
16536992Smaybee 				dbuf_will_dirty(db, tx);
16542445Sahrens 				dbuf_rele(db, FTAG);
1655789Sahrens 			}
16562445Sahrens 		}
16572445Sahrens 	}
16586992Smaybee done:
16596992Smaybee 	/*
16606992Smaybee 	 * Add this range to the dnode range list.
16616992Smaybee 	 * We will finish up this free operation in the syncing phase.
16626992Smaybee 	 */
1663789Sahrens 	mutex_enter(&dn->dn_mtx);
1664789Sahrens 	dnode_clear_range(dn, blkid, nblks, tx);
1665789Sahrens 	{
1666789Sahrens 		free_range_t *rp, *found;
1667789Sahrens 		avl_index_t where;
1668789Sahrens 		avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
1669789Sahrens 
1670789Sahrens 		/* Add new range to dn_ranges */
1671789Sahrens 		rp = kmem_alloc(sizeof (free_range_t), KM_SLEEP);
1672789Sahrens 		rp->fr_blkid = blkid;
1673789Sahrens 		rp->fr_nblks = nblks;
1674789Sahrens 		found = avl_find(tree, rp, &where);
1675789Sahrens 		ASSERT(found == NULL);
1676789Sahrens 		avl_insert(tree, rp, where);
1677789Sahrens 		dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1678789Sahrens 		    blkid, nblks, tx->tx_txg);
1679789Sahrens 	}
1680789Sahrens 	mutex_exit(&dn->dn_mtx);
1681789Sahrens 
16826992Smaybee 	dbuf_free_range(dn, blkid, blkid + nblks - 1, tx);
1683789Sahrens 	dnode_setdirty(dn, tx);
1684789Sahrens out:
16856992Smaybee 	if (trunc && dn->dn_maxblkid >= (off >> blkshift))
16866992Smaybee 		dn->dn_maxblkid = (off >> blkshift ? (off >> blkshift) - 1 : 0);
16876992Smaybee 
1688789Sahrens 	rw_exit(&dn->dn_struct_rwlock);
1689789Sahrens }
1690789Sahrens 
169111935SMark.Shellenbaum@Sun.COM static boolean_t
dnode_spill_freed(dnode_t * dn)169211935SMark.Shellenbaum@Sun.COM dnode_spill_freed(dnode_t *dn)
169311935SMark.Shellenbaum@Sun.COM {
169411935SMark.Shellenbaum@Sun.COM 	int i;
169511935SMark.Shellenbaum@Sun.COM 
169611935SMark.Shellenbaum@Sun.COM 	mutex_enter(&dn->dn_mtx);
169711935SMark.Shellenbaum@Sun.COM 	for (i = 0; i < TXG_SIZE; i++) {
169811935SMark.Shellenbaum@Sun.COM 		if (dn->dn_rm_spillblk[i] == DN_KILL_SPILLBLK)
169911935SMark.Shellenbaum@Sun.COM 			break;
170011935SMark.Shellenbaum@Sun.COM 	}
170111935SMark.Shellenbaum@Sun.COM 	mutex_exit(&dn->dn_mtx);
170211935SMark.Shellenbaum@Sun.COM 	return (i < TXG_SIZE);
170311935SMark.Shellenbaum@Sun.COM }
170411935SMark.Shellenbaum@Sun.COM 
1705789Sahrens /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
1706789Sahrens uint64_t
dnode_block_freed(dnode_t * dn,uint64_t blkid)1707789Sahrens dnode_block_freed(dnode_t *dn, uint64_t blkid)
1708789Sahrens {
1709789Sahrens 	free_range_t range_tofind;
1710789Sahrens 	void *dp = spa_get_dsl(dn->dn_objset->os_spa);
1711789Sahrens 	int i;
1712789Sahrens 
171311935SMark.Shellenbaum@Sun.COM 	if (blkid == DMU_BONUS_BLKID)
1714789Sahrens 		return (FALSE);
1715789Sahrens 
1716789Sahrens 	/*
1717789Sahrens 	 * If we're in the process of opening the pool, dp will not be
1718789Sahrens 	 * set yet, but there shouldn't be anything dirty.
1719789Sahrens 	 */
1720789Sahrens 	if (dp == NULL)
1721789Sahrens 		return (FALSE);
1722789Sahrens 
1723789Sahrens 	if (dn->dn_free_txg)
1724789Sahrens 		return (TRUE);
1725789Sahrens 
172611935SMark.Shellenbaum@Sun.COM 	if (blkid == DMU_SPILL_BLKID)
172711935SMark.Shellenbaum@Sun.COM 		return (dnode_spill_freed(dn));
172811935SMark.Shellenbaum@Sun.COM 
1729789Sahrens 	range_tofind.fr_blkid = blkid;
1730789Sahrens 	mutex_enter(&dn->dn_mtx);
1731789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
1732789Sahrens 		free_range_t *range_found;
1733789Sahrens 		avl_index_t idx;
1734789Sahrens 
1735789Sahrens 		range_found = avl_find(&dn->dn_ranges[i], &range_tofind, &idx);
1736789Sahrens 		if (range_found) {
1737789Sahrens 			ASSERT(range_found->fr_nblks > 0);
1738789Sahrens 			break;
1739789Sahrens 		}
1740789Sahrens 		range_found = avl_nearest(&dn->dn_ranges[i], idx, AVL_BEFORE);
1741789Sahrens 		if (range_found &&
1742789Sahrens 		    range_found->fr_blkid + range_found->fr_nblks > blkid)
1743789Sahrens 			break;
1744789Sahrens 	}
1745789Sahrens 	mutex_exit(&dn->dn_mtx);
1746789Sahrens 	return (i < TXG_SIZE);
1747789Sahrens }
1748789Sahrens 
1749789Sahrens /* call from syncing context when we actually write/free space for this dnode */
1750789Sahrens void
dnode_diduse_space(dnode_t * dn,int64_t delta)17512082Seschrock dnode_diduse_space(dnode_t *dn, int64_t delta)
1752789Sahrens {
17532082Seschrock 	uint64_t space;
17542082Seschrock 	dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n",
1755789Sahrens 	    dn, dn->dn_phys,
17562082Seschrock 	    (u_longlong_t)dn->dn_phys->dn_used,
17572082Seschrock 	    (longlong_t)delta);
1758789Sahrens 
1759789Sahrens 	mutex_enter(&dn->dn_mtx);
17602082Seschrock 	space = DN_USED_BYTES(dn->dn_phys);
17612082Seschrock 	if (delta > 0) {
17622082Seschrock 		ASSERT3U(space + delta, >=, space); /* no overflow */
1763789Sahrens 	} else {
17642082Seschrock 		ASSERT3U(space, >=, -delta); /* no underflow */
17652082Seschrock 	}
17662082Seschrock 	space += delta;
17674577Sahrens 	if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_DNODE_BYTES) {
17682082Seschrock 		ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0);
17692082Seschrock 		ASSERT3U(P2PHASE(space, 1<<DEV_BSHIFT), ==, 0);
17702082Seschrock 		dn->dn_phys->dn_used = space >> DEV_BSHIFT;
17712082Seschrock 	} else {
17722082Seschrock 		dn->dn_phys->dn_used = space;
17732082Seschrock 		dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES;
1774789Sahrens 	}
1775789Sahrens 	mutex_exit(&dn->dn_mtx);
1776789Sahrens }
1777789Sahrens 
1778789Sahrens /*
1779789Sahrens  * Call when we think we're going to write/free space in open context.
1780789Sahrens  * Be conservative (ie. OK to write less than this or free more than
1781789Sahrens  * this, but don't write more or free less).
1782789Sahrens  */
1783789Sahrens void
dnode_willuse_space(dnode_t * dn,int64_t space,dmu_tx_t * tx)1784789Sahrens dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx)
1785789Sahrens {
178610298SMatthew.Ahrens@Sun.COM 	objset_t *os = dn->dn_objset;
1787789Sahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1788789Sahrens 
1789789Sahrens 	if (space > 0)
1790789Sahrens 		space = spa_get_asize(os->os_spa, space);
1791789Sahrens 
1792789Sahrens 	if (ds)
1793789Sahrens 		dsl_dir_willuse_space(ds->ds_dir, space, tx);
1794789Sahrens 
1795789Sahrens 	dmu_tx_willuse_space(tx, space);
1796789Sahrens }
1797789Sahrens 
17989950SMark.Maybee@Sun.COM /*
17999950SMark.Maybee@Sun.COM  * This function scans a block at the indicated "level" looking for
18009950SMark.Maybee@Sun.COM  * a hole or data (depending on 'flags').  If level > 0, then we are
18019950SMark.Maybee@Sun.COM  * scanning an indirect block looking at its pointers.  If level == 0,
18029950SMark.Maybee@Sun.COM  * then we are looking at a block of dnodes.  If we don't find what we
18039950SMark.Maybee@Sun.COM  * are looking for in the block, we return ESRCH.  Otherwise, return
18049950SMark.Maybee@Sun.COM  * with *offset pointing to the beginning (if searching forwards) or
18059950SMark.Maybee@Sun.COM  * end (if searching backwards) of the range covered by the block
18069950SMark.Maybee@Sun.COM  * pointer we matched on (or dnode).
18079950SMark.Maybee@Sun.COM  *
18089950SMark.Maybee@Sun.COM  * The basic search algorithm used below by dnode_next_offset() is to
18099950SMark.Maybee@Sun.COM  * use this function to search up the block tree (widen the search) until
18109950SMark.Maybee@Sun.COM  * we find something (i.e., we don't return ESRCH) and then search back
18119950SMark.Maybee@Sun.COM  * down the tree (narrow the search) until we reach our original search
18129950SMark.Maybee@Sun.COM  * level.
18139950SMark.Maybee@Sun.COM  */
1814789Sahrens static int
dnode_next_offset_level(dnode_t * dn,int flags,uint64_t * offset,int lvl,uint64_t blkfill,uint64_t txg)18156992Smaybee dnode_next_offset_level(dnode_t *dn, int flags, uint64_t *offset,
18163025Sahrens 	int lvl, uint64_t blkfill, uint64_t txg)
1817789Sahrens {
1818789Sahrens 	dmu_buf_impl_t *db = NULL;
1819789Sahrens 	void *data = NULL;
1820789Sahrens 	uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
1821789Sahrens 	uint64_t epb = 1ULL << epbs;
1822789Sahrens 	uint64_t minfill, maxfill;
18236992Smaybee 	boolean_t hole;
18246992Smaybee 	int i, inc, error, span;
1825789Sahrens 
1826789Sahrens 	dprintf("probing object %llu offset %llx level %d of %u\n",
1827789Sahrens 	    dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels);
1828789Sahrens 
18299396SMatthew.Ahrens@Sun.COM 	hole = ((flags & DNODE_FIND_HOLE) != 0);
18306992Smaybee 	inc = (flags & DNODE_FIND_BACKWARDS) ? -1 : 1;
18317385SMark.Maybee@Sun.COM 	ASSERT(txg == 0 || !hole);
18326992Smaybee 
1833789Sahrens 	if (lvl == dn->dn_phys->dn_nlevels) {
1834789Sahrens 		error = 0;
1835789Sahrens 		epb = dn->dn_phys->dn_nblkptr;
1836789Sahrens 		data = dn->dn_phys->dn_blkptr;
1837789Sahrens 	} else {
1838789Sahrens 		uint64_t blkid = dbuf_whichblock(dn, *offset) >> (epbs * lvl);
1839789Sahrens 		error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FTAG, &db);
1840789Sahrens 		if (error) {
18417385SMark.Maybee@Sun.COM 			if (error != ENOENT)
18427385SMark.Maybee@Sun.COM 				return (error);
18437385SMark.Maybee@Sun.COM 			if (hole)
18447385SMark.Maybee@Sun.COM 				return (0);
18457385SMark.Maybee@Sun.COM 			/*
18467385SMark.Maybee@Sun.COM 			 * This can only happen when we are searching up
18477385SMark.Maybee@Sun.COM 			 * the block tree for data.  We don't really need to
18487385SMark.Maybee@Sun.COM 			 * adjust the offset, as we will just end up looking
18497385SMark.Maybee@Sun.COM 			 * at the pointer to this block in its parent, and its
18507385SMark.Maybee@Sun.COM 			 * going to be unallocated, so we will skip over it.
18517385SMark.Maybee@Sun.COM 			 */
18527385SMark.Maybee@Sun.COM 			return (ESRCH);
1853789Sahrens 		}
18541793Sahrens 		error = dbuf_read(db, NULL, DB_RF_CANFAIL | DB_RF_HAVESTRUCT);
18551793Sahrens 		if (error) {
18561793Sahrens 			dbuf_rele(db, FTAG);
18571793Sahrens 			return (error);
18581793Sahrens 		}
1859789Sahrens 		data = db->db.db_data;
1860789Sahrens 	}
1861789Sahrens 
18623025Sahrens 	if (db && txg &&
18633025Sahrens 	    (db->db_blkptr == NULL || db->db_blkptr->blk_birth <= txg)) {
18647385SMark.Maybee@Sun.COM 		/*
18657385SMark.Maybee@Sun.COM 		 * This can only happen when we are searching up the tree
18667385SMark.Maybee@Sun.COM 		 * and these conditions mean that we need to keep climbing.
18677385SMark.Maybee@Sun.COM 		 */
18683025Sahrens 		error = ESRCH;
18693025Sahrens 	} else if (lvl == 0) {
1870789Sahrens 		dnode_phys_t *dnp = data;
1871789Sahrens 		span = DNODE_SHIFT;
1872789Sahrens 		ASSERT(dn->dn_type == DMU_OT_DNODE);
1873789Sahrens 
18746992Smaybee 		for (i = (*offset >> span) & (blkfill - 1);
18756992Smaybee 		    i >= 0 && i < blkfill; i += inc) {
18769409SJonathan.Adams@Sun.COM 			if ((dnp[i].dn_type == DMU_OT_NONE) == hole)
1877789Sahrens 				break;
18786992Smaybee 			*offset += (1ULL << span) * inc;
1879789Sahrens 		}
18806992Smaybee 		if (i < 0 || i == blkfill)
1881789Sahrens 			error = ESRCH;
1882789Sahrens 	} else {
1883789Sahrens 		blkptr_t *bp = data;
18849950SMark.Maybee@Sun.COM 		uint64_t start = *offset;
1885789Sahrens 		span = (lvl - 1) * epbs + dn->dn_datablkshift;
1886789Sahrens 		minfill = 0;
1887789Sahrens 		maxfill = blkfill << ((lvl - 1) * epbs);
1888789Sahrens 
1889789Sahrens 		if (hole)
1890789Sahrens 			maxfill--;
1891789Sahrens 		else
1892789Sahrens 			minfill++;
1893789Sahrens 
18949950SMark.Maybee@Sun.COM 		*offset = *offset >> span;
18959950SMark.Maybee@Sun.COM 		for (i = BF64_GET(*offset, 0, epbs);
18966992Smaybee 		    i >= 0 && i < epb; i += inc) {
1897789Sahrens 			if (bp[i].blk_fill >= minfill &&
18983025Sahrens 			    bp[i].blk_fill <= maxfill &&
18997385SMark.Maybee@Sun.COM 			    (hole || bp[i].blk_birth > txg))
1900789Sahrens 				break;
19019950SMark.Maybee@Sun.COM 			if (inc > 0 || *offset > 0)
19029950SMark.Maybee@Sun.COM 				*offset += inc;
1903789Sahrens 		}
19049950SMark.Maybee@Sun.COM 		*offset = *offset << span;
19059950SMark.Maybee@Sun.COM 		if (inc < 0) {
19069950SMark.Maybee@Sun.COM 			/* traversing backwards; position offset at the end */
19079950SMark.Maybee@Sun.COM 			ASSERT3U(*offset, <=, start);
19089950SMark.Maybee@Sun.COM 			*offset = MIN(*offset + (1ULL << span) - 1, start);
19099950SMark.Maybee@Sun.COM 		} else if (*offset < start) {
19109950SMark.Maybee@Sun.COM 			*offset = start;
19119950SMark.Maybee@Sun.COM 		}
19129950SMark.Maybee@Sun.COM 		if (i < 0 || i >= epb)
1913789Sahrens 			error = ESRCH;
1914789Sahrens 	}
1915789Sahrens 
1916789Sahrens 	if (db)
19171544Seschrock 		dbuf_rele(db, FTAG);
1918789Sahrens 
1919789Sahrens 	return (error);
1920789Sahrens }
1921789Sahrens 
1922789Sahrens /*
1923789Sahrens  * Find the next hole, data, or sparse region at or after *offset.
1924789Sahrens  * The value 'blkfill' tells us how many items we expect to find
1925789Sahrens  * in an L0 data block; this value is 1 for normal objects,
1926789Sahrens  * DNODES_PER_BLOCK for the meta dnode, and some fraction of
1927789Sahrens  * DNODES_PER_BLOCK when searching for sparse regions thereof.
19283025Sahrens  *
1929789Sahrens  * Examples:
1930789Sahrens  *
19316992Smaybee  * dnode_next_offset(dn, flags, offset, 1, 1, 0);
19326992Smaybee  *	Finds the next/previous hole/data in a file.
1933789Sahrens  *	Used in dmu_offset_next().
1934789Sahrens  *
19356992Smaybee  * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
1936789Sahrens  *	Finds the next free/allocated dnode an objset's meta-dnode.
19373025Sahrens  *	Only finds objects that have new contents since txg (ie.
19383025Sahrens  *	bonus buffer changes and content removal are ignored).
1939789Sahrens  *	Used in dmu_object_next().
1940789Sahrens  *
19416992Smaybee  * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
1942789Sahrens  *	Finds the next L2 meta-dnode bp that's at most 1/4 full.
1943789Sahrens  *	Used in dmu_object_alloc().
1944789Sahrens  */
1945789Sahrens int
dnode_next_offset(dnode_t * dn,int flags,uint64_t * offset,int minlvl,uint64_t blkfill,uint64_t txg)19466992Smaybee dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset,
19473025Sahrens     int minlvl, uint64_t blkfill, uint64_t txg)
1948789Sahrens {
19496992Smaybee 	uint64_t initial_offset = *offset;
1950789Sahrens 	int lvl, maxlvl;
1951789Sahrens 	int error = 0;
1952789Sahrens 
19536992Smaybee 	if (!(flags & DNODE_FIND_HAVELOCK))
19546992Smaybee 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
1955789Sahrens 
1956789Sahrens 	if (dn->dn_phys->dn_nlevels == 0) {
19576992Smaybee 		error = ESRCH;
19586992Smaybee 		goto out;
1959789Sahrens 	}
1960789Sahrens 
1961789Sahrens 	if (dn->dn_datablkshift == 0) {
1962789Sahrens 		if (*offset < dn->dn_datablksz) {
19636992Smaybee 			if (flags & DNODE_FIND_HOLE)
1964789Sahrens 				*offset = dn->dn_datablksz;
1965789Sahrens 		} else {
1966789Sahrens 			error = ESRCH;
1967789Sahrens 		}
19686992Smaybee 		goto out;
1969789Sahrens 	}
1970789Sahrens 
1971789Sahrens 	maxlvl = dn->dn_phys->dn_nlevels;
1972789Sahrens 
1973789Sahrens 	for (lvl = minlvl; lvl <= maxlvl; lvl++) {
19743025Sahrens 		error = dnode_next_offset_level(dn,
19756992Smaybee 		    flags, offset, lvl, blkfill, txg);
19761793Sahrens 		if (error != ESRCH)
1977789Sahrens 			break;
1978789Sahrens 	}
1979789Sahrens 
19806992Smaybee 	while (error == 0 && --lvl >= minlvl) {
19813025Sahrens 		error = dnode_next_offset_level(dn,
19826992Smaybee 		    flags, offset, lvl, blkfill, txg);
19833025Sahrens 	}
1984789Sahrens 
19856992Smaybee 	if (error == 0 && (flags & DNODE_FIND_BACKWARDS ?
19866992Smaybee 	    initial_offset < *offset : initial_offset > *offset))
19871793Sahrens 		error = ESRCH;
19886992Smaybee out:
19896992Smaybee 	if (!(flags & DNODE_FIND_HAVELOCK))
19906992Smaybee 		rw_exit(&dn->dn_struct_rwlock);
1991789Sahrens 
1992789Sahrens 	return (error);
1993789Sahrens }
1994