xref: /onnv-gate/usr/src/uts/common/fs/zfs/dnode.c (revision 12586)
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;
41789Sahrens 
42789Sahrens static dnode_phys_t dnode_phys_zero;
43789Sahrens 
44789Sahrens int zfs_default_bs = SPA_MINBLOCKSHIFT;
45789Sahrens int zfs_default_ibs = DN_MAX_INDBLKSHIFT;
46789Sahrens 
47789Sahrens /* ARGSUSED */
48789Sahrens static int
49789Sahrens dnode_cons(void *arg, void *unused, int kmflag)
50789Sahrens {
51789Sahrens 	int i;
52789Sahrens 	dnode_t *dn = arg;
53789Sahrens 	bzero(dn, sizeof (dnode_t));
54789Sahrens 
55789Sahrens 	rw_init(&dn->dn_struct_rwlock, NULL, RW_DEFAULT, NULL);
56789Sahrens 	mutex_init(&dn->dn_mtx, NULL, MUTEX_DEFAULT, NULL);
57789Sahrens 	mutex_init(&dn->dn_dbufs_mtx, NULL, MUTEX_DEFAULT, NULL);
588214SRicardo.M.Correia@Sun.COM 	cv_init(&dn->dn_notxholds, NULL, CV_DEFAULT, NULL);
598214SRicardo.M.Correia@Sun.COM 
60789Sahrens 	refcount_create(&dn->dn_holds);
61789Sahrens 	refcount_create(&dn->dn_tx_holds);
62789Sahrens 
63789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
64789Sahrens 		avl_create(&dn->dn_ranges[i], free_range_compar,
65789Sahrens 		    sizeof (free_range_t),
66789Sahrens 		    offsetof(struct free_range, fr_node));
673547Smaybee 		list_create(&dn->dn_dirty_records[i],
683547Smaybee 		    sizeof (dbuf_dirty_record_t),
693547Smaybee 		    offsetof(dbuf_dirty_record_t, dr_dirty_node));
70789Sahrens 	}
71789Sahrens 
72789Sahrens 	list_create(&dn->dn_dbufs, sizeof (dmu_buf_impl_t),
73789Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
74789Sahrens 
75789Sahrens 	return (0);
76789Sahrens }
77789Sahrens 
78789Sahrens /* ARGSUSED */
79789Sahrens static void
80789Sahrens dnode_dest(void *arg, void *unused)
81789Sahrens {
82789Sahrens 	int i;
83789Sahrens 	dnode_t *dn = arg;
84789Sahrens 
85789Sahrens 	rw_destroy(&dn->dn_struct_rwlock);
86789Sahrens 	mutex_destroy(&dn->dn_mtx);
87789Sahrens 	mutex_destroy(&dn->dn_dbufs_mtx);
888214SRicardo.M.Correia@Sun.COM 	cv_destroy(&dn->dn_notxholds);
89789Sahrens 	refcount_destroy(&dn->dn_holds);
90789Sahrens 	refcount_destroy(&dn->dn_tx_holds);
91789Sahrens 
92789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
93789Sahrens 		avl_destroy(&dn->dn_ranges[i]);
943547Smaybee 		list_destroy(&dn->dn_dirty_records[i]);
95789Sahrens 	}
96789Sahrens 
97789Sahrens 	list_destroy(&dn->dn_dbufs);
98789Sahrens }
99789Sahrens 
100789Sahrens void
101789Sahrens dnode_init(void)
102789Sahrens {
103789Sahrens 	dnode_cache = kmem_cache_create("dnode_t",
104789Sahrens 	    sizeof (dnode_t),
105789Sahrens 	    0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0);
106789Sahrens }
107789Sahrens 
108789Sahrens void
109789Sahrens dnode_fini(void)
110789Sahrens {
111789Sahrens 	kmem_cache_destroy(dnode_cache);
112789Sahrens }
113789Sahrens 
114789Sahrens 
115873Sek110237 #ifdef ZFS_DEBUG
116789Sahrens void
117789Sahrens dnode_verify(dnode_t *dn)
118789Sahrens {
119789Sahrens 	int drop_struct_lock = FALSE;
120789Sahrens 
121789Sahrens 	ASSERT(dn->dn_phys);
122789Sahrens 	ASSERT(dn->dn_objset);
123789Sahrens 
124789Sahrens 	ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES);
125789Sahrens 
126789Sahrens 	if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY))
127789Sahrens 		return;
128789Sahrens 
129789Sahrens 	if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
130789Sahrens 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
131789Sahrens 		drop_struct_lock = TRUE;
132789Sahrens 	}
133789Sahrens 	if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) {
134789Sahrens 		int i;
135789Sahrens 		ASSERT3U(dn->dn_indblkshift, >=, 0);
136789Sahrens 		ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT);
137789Sahrens 		if (dn->dn_datablkshift) {
138789Sahrens 			ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT);
139789Sahrens 			ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT);
140789Sahrens 			ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz);
141789Sahrens 		}
142789Sahrens 		ASSERT3U(dn->dn_nlevels, <=, 30);
143789Sahrens 		ASSERT3U(dn->dn_type, <=, DMU_OT_NUMTYPES);
144789Sahrens 		ASSERT3U(dn->dn_nblkptr, >=, 1);
145789Sahrens 		ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
146789Sahrens 		ASSERT3U(dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
147789Sahrens 		ASSERT3U(dn->dn_datablksz, ==,
148789Sahrens 		    dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
149789Sahrens 		ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0);
150789Sahrens 		ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) +
151789Sahrens 		    dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
152789Sahrens 		for (i = 0; i < TXG_SIZE; i++) {
153789Sahrens 			ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels);
154789Sahrens 		}
155789Sahrens 	}
156789Sahrens 	if (dn->dn_phys->dn_type != DMU_OT_NONE)
157789Sahrens 		ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels);
1589396SMatthew.Ahrens@Sun.COM 	ASSERT(DMU_OBJECT_IS_SPECIAL(dn->dn_object) || dn->dn_dbuf != NULL);
159789Sahrens 	if (dn->dn_dbuf != NULL) {
160789Sahrens 		ASSERT3P(dn->dn_phys, ==,
161789Sahrens 		    (dnode_phys_t *)dn->dn_dbuf->db.db_data +
162789Sahrens 		    (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT)));
163789Sahrens 	}
164789Sahrens 	if (drop_struct_lock)
165789Sahrens 		rw_exit(&dn->dn_struct_rwlock);
166873Sek110237 }
167789Sahrens #endif
168789Sahrens 
169789Sahrens void
170789Sahrens dnode_byteswap(dnode_phys_t *dnp)
171789Sahrens {
172789Sahrens 	uint64_t *buf64 = (void*)&dnp->dn_blkptr;
173789Sahrens 	int i;
174789Sahrens 
175789Sahrens 	if (dnp->dn_type == DMU_OT_NONE) {
176789Sahrens 		bzero(dnp, sizeof (dnode_phys_t));
177789Sahrens 		return;
178789Sahrens 	}
179789Sahrens 
180789Sahrens 	dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec);
181789Sahrens 	dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen);
182789Sahrens 	dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid);
1832082Seschrock 	dnp->dn_used = BSWAP_64(dnp->dn_used);
184789Sahrens 
185789Sahrens 	/*
186789Sahrens 	 * dn_nblkptr is only one byte, so it's OK to read it in either
187789Sahrens 	 * byte order.  We can't read dn_bouslen.
188789Sahrens 	 */
189789Sahrens 	ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT);
190789Sahrens 	ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR);
191789Sahrens 	for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++)
192789Sahrens 		buf64[i] = BSWAP_64(buf64[i]);
193789Sahrens 
194789Sahrens 	/*
195789Sahrens 	 * OK to check dn_bonuslen for zero, because it won't matter if
196789Sahrens 	 * we have the wrong byte order.  This is necessary because the
197789Sahrens 	 * dnode dnode is smaller than a regular dnode.
198789Sahrens 	 */
199789Sahrens 	if (dnp->dn_bonuslen != 0) {
200789Sahrens 		/*
201789Sahrens 		 * Note that the bonus length calculated here may be
202789Sahrens 		 * longer than the actual bonus buffer.  This is because
203789Sahrens 		 * we always put the bonus buffer after the last block
204789Sahrens 		 * pointer (instead of packing it against the end of the
205789Sahrens 		 * dnode buffer).
206789Sahrens 		 */
207789Sahrens 		int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t);
208789Sahrens 		size_t len = DN_MAX_BONUSLEN - off;
2093882Sahrens 		ASSERT3U(dnp->dn_bonustype, <, DMU_OT_NUMTYPES);
210789Sahrens 		dmu_ot[dnp->dn_bonustype].ot_byteswap(dnp->dn_bonus + off, len);
211789Sahrens 	}
21211935SMark.Shellenbaum@Sun.COM 
21311935SMark.Shellenbaum@Sun.COM 	/* Swap SPILL block if we have one */
21411935SMark.Shellenbaum@Sun.COM 	if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)
21511935SMark.Shellenbaum@Sun.COM 		byteswap_uint64_array(&dnp->dn_spill, sizeof (blkptr_t));
21611935SMark.Shellenbaum@Sun.COM 
217789Sahrens }
218789Sahrens 
219789Sahrens void
220789Sahrens dnode_buf_byteswap(void *vbuf, size_t size)
221789Sahrens {
222789Sahrens 	dnode_phys_t *buf = vbuf;
223789Sahrens 	int i;
224789Sahrens 
225789Sahrens 	ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT));
226789Sahrens 	ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0);
227789Sahrens 
228789Sahrens 	size >>= DNODE_SHIFT;
229789Sahrens 	for (i = 0; i < size; i++) {
230789Sahrens 		dnode_byteswap(buf);
231789Sahrens 		buf++;
232789Sahrens 	}
233789Sahrens }
234789Sahrens 
235789Sahrens static int
236789Sahrens free_range_compar(const void *node1, const void *node2)
237789Sahrens {
238789Sahrens 	const free_range_t *rp1 = node1;
239789Sahrens 	const free_range_t *rp2 = node2;
240789Sahrens 
241789Sahrens 	if (rp1->fr_blkid < rp2->fr_blkid)
242789Sahrens 		return (-1);
243789Sahrens 	else if (rp1->fr_blkid > rp2->fr_blkid)
244789Sahrens 		return (1);
245789Sahrens 	else return (0);
246789Sahrens }
247789Sahrens 
2484944Smaybee void
2494944Smaybee dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx)
2504944Smaybee {
2514944Smaybee 	ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
2524944Smaybee 
2534944Smaybee 	dnode_setdirty(dn, tx);
2544944Smaybee 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2554944Smaybee 	ASSERT3U(newsize, <=, DN_MAX_BONUSLEN -
2564944Smaybee 	    (dn->dn_nblkptr-1) * sizeof (blkptr_t));
2574944Smaybee 	dn->dn_bonuslen = newsize;
2584944Smaybee 	if (newsize == 0)
2594944Smaybee 		dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = DN_ZERO_BONUSLEN;
2604944Smaybee 	else
2614944Smaybee 		dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
2624944Smaybee 	rw_exit(&dn->dn_struct_rwlock);
2634944Smaybee }
2644944Smaybee 
26511935SMark.Shellenbaum@Sun.COM void
26611935SMark.Shellenbaum@Sun.COM dnode_setbonus_type(dnode_t *dn, dmu_object_type_t newtype, dmu_tx_t *tx)
26711935SMark.Shellenbaum@Sun.COM {
26811935SMark.Shellenbaum@Sun.COM 	ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
26911935SMark.Shellenbaum@Sun.COM 	dnode_setdirty(dn, tx);
27011935SMark.Shellenbaum@Sun.COM 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
27111935SMark.Shellenbaum@Sun.COM 	dn->dn_bonustype = newtype;
27211935SMark.Shellenbaum@Sun.COM 	dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
27311935SMark.Shellenbaum@Sun.COM 	rw_exit(&dn->dn_struct_rwlock);
27411935SMark.Shellenbaum@Sun.COM }
27511935SMark.Shellenbaum@Sun.COM 
27611935SMark.Shellenbaum@Sun.COM void
27711935SMark.Shellenbaum@Sun.COM dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx)
27811935SMark.Shellenbaum@Sun.COM {
27911935SMark.Shellenbaum@Sun.COM 	ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
28012178SMark.Shellenbaum@Sun.COM 	ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
28111935SMark.Shellenbaum@Sun.COM 	dnode_setdirty(dn, tx);
28211935SMark.Shellenbaum@Sun.COM 	dn->dn_rm_spillblk[tx->tx_txg&TXG_MASK] = DN_KILL_SPILLBLK;
28311935SMark.Shellenbaum@Sun.COM 	dn->dn_have_spill = B_FALSE;
28411935SMark.Shellenbaum@Sun.COM }
28511935SMark.Shellenbaum@Sun.COM 
286789Sahrens static void
287789Sahrens dnode_setdblksz(dnode_t *dn, int size)
288789Sahrens {
289789Sahrens 	ASSERT3U(P2PHASE(size, SPA_MINBLOCKSIZE), ==, 0);
290789Sahrens 	ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
291789Sahrens 	ASSERT3U(size, >=, SPA_MINBLOCKSIZE);
292789Sahrens 	ASSERT3U(size >> SPA_MINBLOCKSHIFT, <,
293789Sahrens 	    1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8));
294789Sahrens 	dn->dn_datablksz = size;
295789Sahrens 	dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT;
296789Sahrens 	dn->dn_datablkshift = ISP2(size) ? highbit(size - 1) : 0;
297789Sahrens }
298789Sahrens 
299789Sahrens static dnode_t *
30010298SMatthew.Ahrens@Sun.COM dnode_create(objset_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db,
301789Sahrens     uint64_t object)
302789Sahrens {
303789Sahrens 	dnode_t *dn = kmem_cache_alloc(dnode_cache, KM_SLEEP);
304789Sahrens 	(void) dnode_cons(dn, NULL, 0); /* XXX */
305789Sahrens 
306789Sahrens 	dn->dn_objset = os;
307789Sahrens 	dn->dn_object = object;
308789Sahrens 	dn->dn_dbuf = db;
309789Sahrens 	dn->dn_phys = dnp;
310789Sahrens 
311789Sahrens 	if (dnp->dn_datablkszsec)
312789Sahrens 		dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
313789Sahrens 	dn->dn_indblkshift = dnp->dn_indblkshift;
314789Sahrens 	dn->dn_nlevels = dnp->dn_nlevels;
315789Sahrens 	dn->dn_type = dnp->dn_type;
316789Sahrens 	dn->dn_nblkptr = dnp->dn_nblkptr;
317789Sahrens 	dn->dn_checksum = dnp->dn_checksum;
318789Sahrens 	dn->dn_compress = dnp->dn_compress;
319789Sahrens 	dn->dn_bonustype = dnp->dn_bonustype;
320789Sahrens 	dn->dn_bonuslen = dnp->dn_bonuslen;
321789Sahrens 	dn->dn_maxblkid = dnp->dn_maxblkid;
32211935SMark.Shellenbaum@Sun.COM 	dn->dn_have_spill = ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0);
32312178SMark.Shellenbaum@Sun.COM 	dn->dn_id_flags = 0;
324789Sahrens 
325789Sahrens 	dmu_zfetch_init(&dn->dn_zfetch, dn);
326789Sahrens 
327789Sahrens 	ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES);
328789Sahrens 	mutex_enter(&os->os_lock);
329789Sahrens 	list_insert_head(&os->os_dnodes, dn);
330789Sahrens 	mutex_exit(&os->os_lock);
331789Sahrens 
3328582SBrendan.Gregg@Sun.COM 	arc_space_consume(sizeof (dnode_t), ARC_SPACE_OTHER);
333789Sahrens 	return (dn);
334789Sahrens }
335789Sahrens 
336789Sahrens static void
337789Sahrens dnode_destroy(dnode_t *dn)
338789Sahrens {
33910298SMatthew.Ahrens@Sun.COM 	objset_t *os = dn->dn_objset;
340789Sahrens 
3412885Sahrens #ifdef ZFS_DEBUG
3422885Sahrens 	int i;
3432885Sahrens 
3442885Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
3452885Sahrens 		ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
3463547Smaybee 		ASSERT(NULL == list_head(&dn->dn_dirty_records[i]));
3472885Sahrens 		ASSERT(0 == avl_numnodes(&dn->dn_ranges[i]));
3482885Sahrens 	}
3492885Sahrens 	ASSERT(NULL == list_head(&dn->dn_dbufs));
3502885Sahrens #endif
35111935SMark.Shellenbaum@Sun.COM 	ASSERT((dn->dn_id_flags & DN_ID_NEW_EXIST) == 0);
3522885Sahrens 
353789Sahrens 	mutex_enter(&os->os_lock);
354789Sahrens 	list_remove(&os->os_dnodes, dn);
355789Sahrens 	mutex_exit(&os->os_lock);
356789Sahrens 
357789Sahrens 	if (dn->dn_dirtyctx_firstset) {
358789Sahrens 		kmem_free(dn->dn_dirtyctx_firstset, 1);
359789Sahrens 		dn->dn_dirtyctx_firstset = NULL;
360789Sahrens 	}
361789Sahrens 	dmu_zfetch_rele(&dn->dn_zfetch);
3621544Seschrock 	if (dn->dn_bonus) {
3631544Seschrock 		mutex_enter(&dn->dn_bonus->db_mtx);
3641544Seschrock 		dbuf_evict(dn->dn_bonus);
3651544Seschrock 		dn->dn_bonus = NULL;
3661544Seschrock 	}
367789Sahrens 	kmem_cache_free(dnode_cache, dn);
3688582SBrendan.Gregg@Sun.COM 	arc_space_return(sizeof (dnode_t), ARC_SPACE_OTHER);
369789Sahrens }
370789Sahrens 
371789Sahrens void
372789Sahrens dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
3731599Sahrens     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
374789Sahrens {
375789Sahrens 	int i;
376789Sahrens 
377789Sahrens 	if (blocksize == 0)
378789Sahrens 		blocksize = 1 << zfs_default_bs;
3791402Sahrens 	else if (blocksize > SPA_MAXBLOCKSIZE)
3801402Sahrens 		blocksize = SPA_MAXBLOCKSIZE;
3811402Sahrens 	else
3821402Sahrens 		blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE);
383789Sahrens 
384789Sahrens 	if (ibs == 0)
385789Sahrens 		ibs = zfs_default_ibs;
386789Sahrens 
387789Sahrens 	ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT);
388789Sahrens 
389789Sahrens 	dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset,
390789Sahrens 	    dn->dn_object, tx->tx_txg, blocksize, ibs);
391789Sahrens 
392789Sahrens 	ASSERT(dn->dn_type == DMU_OT_NONE);
393789Sahrens 	ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0);
394789Sahrens 	ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE);
395789Sahrens 	ASSERT(ot != DMU_OT_NONE);
396789Sahrens 	ASSERT3U(ot, <, DMU_OT_NUMTYPES);
397789Sahrens 	ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
39811935SMark.Shellenbaum@Sun.COM 	    (bonustype == DMU_OT_SA && bonuslen == 0) ||
399789Sahrens 	    (bonustype != DMU_OT_NONE && bonuslen != 0));
400789Sahrens 	ASSERT3U(bonustype, <, DMU_OT_NUMTYPES);
401789Sahrens 	ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
402789Sahrens 	ASSERT(dn->dn_type == DMU_OT_NONE);
403789Sahrens 	ASSERT3U(dn->dn_maxblkid, ==, 0);
404789Sahrens 	ASSERT3U(dn->dn_allocated_txg, ==, 0);
405789Sahrens 	ASSERT3U(dn->dn_assigned_txg, ==, 0);
406789Sahrens 	ASSERT(refcount_is_zero(&dn->dn_tx_holds));
407789Sahrens 	ASSERT3U(refcount_count(&dn->dn_holds), <=, 1);
408789Sahrens 	ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
409789Sahrens 
410789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
411789Sahrens 		ASSERT3U(dn->dn_next_nlevels[i], ==, 0);
412789Sahrens 		ASSERT3U(dn->dn_next_indblkshift[i], ==, 0);
4134944Smaybee 		ASSERT3U(dn->dn_next_bonuslen[i], ==, 0);
41411935SMark.Shellenbaum@Sun.COM 		ASSERT3U(dn->dn_next_bonustype[i], ==, 0);
41511935SMark.Shellenbaum@Sun.COM 		ASSERT3U(dn->dn_rm_spillblk[i], ==, 0);
4161596Sahrens 		ASSERT3U(dn->dn_next_blksz[i], ==, 0);
4171596Sahrens 		ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
4183547Smaybee 		ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL);
419789Sahrens 		ASSERT3U(avl_numnodes(&dn->dn_ranges[i]), ==, 0);
420789Sahrens 	}
421789Sahrens 
422789Sahrens 	dn->dn_type = ot;
423789Sahrens 	dnode_setdblksz(dn, blocksize);
424789Sahrens 	dn->dn_indblkshift = ibs;
425789Sahrens 	dn->dn_nlevels = 1;
42611935SMark.Shellenbaum@Sun.COM 	if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
42711935SMark.Shellenbaum@Sun.COM 		dn->dn_nblkptr = 1;
42811935SMark.Shellenbaum@Sun.COM 	else
42911935SMark.Shellenbaum@Sun.COM 		dn->dn_nblkptr = 1 +
43011935SMark.Shellenbaum@Sun.COM 		    ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
431789Sahrens 	dn->dn_bonustype = bonustype;
432789Sahrens 	dn->dn_bonuslen = bonuslen;
433789Sahrens 	dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
434789Sahrens 	dn->dn_compress = ZIO_COMPRESS_INHERIT;
435789Sahrens 	dn->dn_dirtyctx = 0;
436789Sahrens 
437789Sahrens 	dn->dn_free_txg = 0;
438789Sahrens 	if (dn->dn_dirtyctx_firstset) {
439789Sahrens 		kmem_free(dn->dn_dirtyctx_firstset, 1);
440789Sahrens 		dn->dn_dirtyctx_firstset = NULL;
441789Sahrens 	}
442789Sahrens 
443789Sahrens 	dn->dn_allocated_txg = tx->tx_txg;
44411935SMark.Shellenbaum@Sun.COM 	dn->dn_id_flags = 0;
4451599Sahrens 
446789Sahrens 	dnode_setdirty(dn, tx);
4471599Sahrens 	dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs;
4484944Smaybee 	dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
44911935SMark.Shellenbaum@Sun.COM 	dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
4501599Sahrens 	dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz;
451789Sahrens }
452789Sahrens 
453789Sahrens void
454789Sahrens dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
455789Sahrens     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
456789Sahrens {
4578986SMark.Maybee@Sun.COM 	int nblkptr;
4581596Sahrens 
459789Sahrens 	ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE);
460789Sahrens 	ASSERT3U(blocksize, <=, SPA_MAXBLOCKSIZE);
461789Sahrens 	ASSERT3U(blocksize % SPA_MINBLOCKSIZE, ==, 0);
4621544Seschrock 	ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
463789Sahrens 	ASSERT(tx->tx_txg != 0);
464789Sahrens 	ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
46512178SMark.Shellenbaum@Sun.COM 	    (bonustype != DMU_OT_NONE && bonuslen != 0) ||
46612178SMark.Shellenbaum@Sun.COM 	    (bonustype == DMU_OT_SA && bonuslen == 0));
467789Sahrens 	ASSERT3U(bonustype, <, DMU_OT_NUMTYPES);
468789Sahrens 	ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
4691596Sahrens 
4701544Seschrock 	/* clean up any unreferenced dbufs */
4714944Smaybee 	dnode_evict_dbufs(dn);
4721544Seschrock 
47312432SMark.Shellenbaum@Sun.COM 	dn->dn_id_flags = 0;
47412432SMark.Shellenbaum@Sun.COM 
4758986SMark.Maybee@Sun.COM 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
4768986SMark.Maybee@Sun.COM 	dnode_setdirty(dn, tx);
4778986SMark.Maybee@Sun.COM 	if (dn->dn_datablksz != blocksize) {
4788986SMark.Maybee@Sun.COM 		/* change blocksize */
4798986SMark.Maybee@Sun.COM 		ASSERT(dn->dn_maxblkid == 0 &&
4808986SMark.Maybee@Sun.COM 		    (BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) ||
4818986SMark.Maybee@Sun.COM 		    dnode_block_freed(dn, 0)));
4828986SMark.Maybee@Sun.COM 		dnode_setdblksz(dn, blocksize);
4838986SMark.Maybee@Sun.COM 		dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize;
484789Sahrens 	}
4858986SMark.Maybee@Sun.COM 	if (dn->dn_bonuslen != bonuslen)
4868986SMark.Maybee@Sun.COM 		dn->dn_next_bonuslen[tx->tx_txg&TXG_MASK] = bonuslen;
48712178SMark.Shellenbaum@Sun.COM 
48812178SMark.Shellenbaum@Sun.COM 	if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
48912178SMark.Shellenbaum@Sun.COM 		nblkptr = 1;
49012178SMark.Shellenbaum@Sun.COM 	else
49112178SMark.Shellenbaum@Sun.COM 		nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
49211935SMark.Shellenbaum@Sun.COM 	if (dn->dn_bonustype != bonustype)
49311935SMark.Shellenbaum@Sun.COM 		dn->dn_next_bonustype[tx->tx_txg&TXG_MASK] = bonustype;
4948644SMark.Maybee@Sun.COM 	if (dn->dn_nblkptr != nblkptr)
4958644SMark.Maybee@Sun.COM 		dn->dn_next_nblkptr[tx->tx_txg&TXG_MASK] = nblkptr;
49611935SMark.Shellenbaum@Sun.COM 	if (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
49712178SMark.Shellenbaum@Sun.COM 		dbuf_rm_spill(dn, tx);
49812178SMark.Shellenbaum@Sun.COM 		dnode_rm_spill(dn, tx);
49911935SMark.Shellenbaum@Sun.COM 	}
500789Sahrens 	rw_exit(&dn->dn_struct_rwlock);
501789Sahrens 
502789Sahrens 	/* change type */
503789Sahrens 	dn->dn_type = ot;
504789Sahrens 
505789Sahrens 	/* change bonus size and type */
506789Sahrens 	mutex_enter(&dn->dn_mtx);
507789Sahrens 	dn->dn_bonustype = bonustype;
508789Sahrens 	dn->dn_bonuslen = bonuslen;
5098644SMark.Maybee@Sun.COM 	dn->dn_nblkptr = nblkptr;
510789Sahrens 	dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
511789Sahrens 	dn->dn_compress = ZIO_COMPRESS_INHERIT;
512789Sahrens 	ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
513789Sahrens 
5148644SMark.Maybee@Sun.COM 	/* fix up the bonus db_size */
5158644SMark.Maybee@Sun.COM 	if (dn->dn_bonus) {
5164944Smaybee 		dn->dn_bonus->db.db_size =
5174944Smaybee 		    DN_MAX_BONUSLEN - (dn->dn_nblkptr-1) * sizeof (blkptr_t);
5184944Smaybee 		ASSERT(dn->dn_bonuslen <= dn->dn_bonus->db.db_size);
5194944Smaybee 	}
5203087Sahrens 
521789Sahrens 	dn->dn_allocated_txg = tx->tx_txg;
522789Sahrens 	mutex_exit(&dn->dn_mtx);
523789Sahrens }
524789Sahrens 
525789Sahrens void
526789Sahrens dnode_special_close(dnode_t *dn)
527789Sahrens {
5281544Seschrock 	/*
5291544Seschrock 	 * Wait for final references to the dnode to clear.  This can
5301544Seschrock 	 * only happen if the arc is asyncronously evicting state that
5311544Seschrock 	 * has a hold on this dnode while we are trying to evict this
5321544Seschrock 	 * dnode.
5331544Seschrock 	 */
5341544Seschrock 	while (refcount_count(&dn->dn_holds) > 0)
5351544Seschrock 		delay(1);
536789Sahrens 	dnode_destroy(dn);
537789Sahrens }
538789Sahrens 
539789Sahrens dnode_t *
54010298SMatthew.Ahrens@Sun.COM dnode_special_open(objset_t *os, dnode_phys_t *dnp, uint64_t object)
541789Sahrens {
542789Sahrens 	dnode_t *dn = dnode_create(os, dnp, NULL, object);
543873Sek110237 	DNODE_VERIFY(dn);
544789Sahrens 	return (dn);
545789Sahrens }
546789Sahrens 
547789Sahrens static void
548789Sahrens dnode_buf_pageout(dmu_buf_t *db, void *arg)
549789Sahrens {
550789Sahrens 	dnode_t **children_dnodes = arg;
551789Sahrens 	int i;
552789Sahrens 	int epb = db->db_size >> DNODE_SHIFT;
553789Sahrens 
554789Sahrens 	for (i = 0; i < epb; i++) {
555789Sahrens 		dnode_t *dn = children_dnodes[i];
556789Sahrens 		int n;
557789Sahrens 
558789Sahrens 		if (dn == NULL)
559789Sahrens 			continue;
560789Sahrens #ifdef ZFS_DEBUG
561789Sahrens 		/*
562789Sahrens 		 * If there are holds on this dnode, then there should
563789Sahrens 		 * be holds on the dnode's containing dbuf as well; thus
564789Sahrens 		 * it wouldn't be eligable for eviction and this function
565789Sahrens 		 * would not have been called.
566789Sahrens 		 */
567789Sahrens 		ASSERT(refcount_is_zero(&dn->dn_holds));
568789Sahrens 		ASSERT(list_head(&dn->dn_dbufs) == NULL);
569789Sahrens 		ASSERT(refcount_is_zero(&dn->dn_tx_holds));
570789Sahrens 
571789Sahrens 		for (n = 0; n < TXG_SIZE; n++)
5721596Sahrens 			ASSERT(!list_link_active(&dn->dn_dirty_link[n]));
573789Sahrens #endif
574789Sahrens 		children_dnodes[i] = NULL;
575789Sahrens 		dnode_destroy(dn);
576789Sahrens 	}
577789Sahrens 	kmem_free(children_dnodes, epb * sizeof (dnode_t *));
578789Sahrens }
579789Sahrens 
580789Sahrens /*
5811544Seschrock  * errors:
5821544Seschrock  * EINVAL - invalid object number.
5831544Seschrock  * EIO - i/o error.
5841544Seschrock  * succeeds even for free dnodes.
585789Sahrens  */
5861544Seschrock int
58710298SMatthew.Ahrens@Sun.COM dnode_hold_impl(objset_t *os, uint64_t object, int flag,
5881544Seschrock     void *tag, dnode_t **dnp)
589789Sahrens {
5901544Seschrock 	int epb, idx, err;
591789Sahrens 	int drop_struct_lock = FALSE;
5921544Seschrock 	int type;
593789Sahrens 	uint64_t blk;
594789Sahrens 	dnode_t *mdn, *dn;
595789Sahrens 	dmu_buf_impl_t *db;
596789Sahrens 	dnode_t **children_dnodes;
597789Sahrens 
5987754SJeff.Bonwick@Sun.COM 	/*
5997754SJeff.Bonwick@Sun.COM 	 * If you are holding the spa config lock as writer, you shouldn't
60011958SGeorge.Wilson@Sun.COM 	 * be asking the DMU to do *anything* unless it's the root pool
60111958SGeorge.Wilson@Sun.COM 	 * which may require us to read from the root filesystem while
60211958SGeorge.Wilson@Sun.COM 	 * holding some (not all) of the locks as writer.
6037754SJeff.Bonwick@Sun.COM 	 */
60411958SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(os->os_spa, SCL_ALL, RW_WRITER) == 0 ||
60511958SGeorge.Wilson@Sun.COM 	    (spa_is_root(os->os_spa) &&
606*12586SGeorge.Wilson@Sun.COM 	    spa_config_held(os->os_spa, SCL_STATE, RW_WRITER)));
6077754SJeff.Bonwick@Sun.COM 
6089396SMatthew.Ahrens@Sun.COM 	if (object == DMU_USERUSED_OBJECT || object == DMU_GROUPUSED_OBJECT) {
6099396SMatthew.Ahrens@Sun.COM 		dn = (object == DMU_USERUSED_OBJECT) ?
6109396SMatthew.Ahrens@Sun.COM 		    os->os_userused_dnode : os->os_groupused_dnode;
6119396SMatthew.Ahrens@Sun.COM 		if (dn == NULL)
6129396SMatthew.Ahrens@Sun.COM 			return (ENOENT);
6139396SMatthew.Ahrens@Sun.COM 		type = dn->dn_type;
6149396SMatthew.Ahrens@Sun.COM 		if ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE)
6159396SMatthew.Ahrens@Sun.COM 			return (ENOENT);
6169396SMatthew.Ahrens@Sun.COM 		if ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)
6179396SMatthew.Ahrens@Sun.COM 			return (EEXIST);
6189396SMatthew.Ahrens@Sun.COM 		DNODE_VERIFY(dn);
6199396SMatthew.Ahrens@Sun.COM 		(void) refcount_add(&dn->dn_holds, tag);
6209396SMatthew.Ahrens@Sun.COM 		*dnp = dn;
6219396SMatthew.Ahrens@Sun.COM 		return (0);
6229396SMatthew.Ahrens@Sun.COM 	}
6239396SMatthew.Ahrens@Sun.COM 
624789Sahrens 	if (object == 0 || object >= DN_MAX_OBJECT)
6251544Seschrock 		return (EINVAL);
626789Sahrens 
627789Sahrens 	mdn = os->os_meta_dnode;
628789Sahrens 
629873Sek110237 	DNODE_VERIFY(mdn);
630789Sahrens 
631789Sahrens 	if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) {
632789Sahrens 		rw_enter(&mdn->dn_struct_rwlock, RW_READER);
633789Sahrens 		drop_struct_lock = TRUE;
634789Sahrens 	}
635789Sahrens 
636789Sahrens 	blk = dbuf_whichblock(mdn, object * sizeof (dnode_phys_t));
637789Sahrens 
6381544Seschrock 	db = dbuf_hold(mdn, blk, FTAG);
639789Sahrens 	if (drop_struct_lock)
640789Sahrens 		rw_exit(&mdn->dn_struct_rwlock);
6411544Seschrock 	if (db == NULL)
6421544Seschrock 		return (EIO);
6431544Seschrock 	err = dbuf_read(db, NULL, DB_RF_CANFAIL);
6441544Seschrock 	if (err) {
6451544Seschrock 		dbuf_rele(db, FTAG);
6461544Seschrock 		return (err);
6471544Seschrock 	}
648789Sahrens 
649789Sahrens 	ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT);
650789Sahrens 	epb = db->db.db_size >> DNODE_SHIFT;
651789Sahrens 
652789Sahrens 	idx = object & (epb-1);
653789Sahrens 
654789Sahrens 	children_dnodes = dmu_buf_get_user(&db->db);
655789Sahrens 	if (children_dnodes == NULL) {
656789Sahrens 		dnode_t **winner;
657789Sahrens 		children_dnodes = kmem_zalloc(epb * sizeof (dnode_t *),
658789Sahrens 		    KM_SLEEP);
659789Sahrens 		if (winner = dmu_buf_set_user(&db->db, children_dnodes, NULL,
660789Sahrens 		    dnode_buf_pageout)) {
661789Sahrens 			kmem_free(children_dnodes, epb * sizeof (dnode_t *));
662789Sahrens 			children_dnodes = winner;
663789Sahrens 		}
664789Sahrens 	}
665789Sahrens 
666789Sahrens 	if ((dn = children_dnodes[idx]) == NULL) {
6674309Smaybee 		dnode_phys_t *dnp = (dnode_phys_t *)db->db.db_data+idx;
668789Sahrens 		dnode_t *winner;
6694309Smaybee 
6704309Smaybee 		dn = dnode_create(os, dnp, db, object);
671789Sahrens 		winner = atomic_cas_ptr(&children_dnodes[idx], NULL, dn);
672789Sahrens 		if (winner != NULL) {
673789Sahrens 			dnode_destroy(dn);
674789Sahrens 			dn = winner;
675789Sahrens 		}
676789Sahrens 	}
677789Sahrens 
678789Sahrens 	mutex_enter(&dn->dn_mtx);
6791544Seschrock 	type = dn->dn_type;
680789Sahrens 	if (dn->dn_free_txg ||
6811544Seschrock 	    ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) ||
6829396SMatthew.Ahrens@Sun.COM 	    ((flag & DNODE_MUST_BE_FREE) &&
68312432SMark.Shellenbaum@Sun.COM 	    (type != DMU_OT_NONE || !refcount_is_zero(&dn->dn_holds)))) {
684789Sahrens 		mutex_exit(&dn->dn_mtx);
6851544Seschrock 		dbuf_rele(db, FTAG);
6861544Seschrock 		return (type == DMU_OT_NONE ? ENOENT : EEXIST);
687789Sahrens 	}
688789Sahrens 	mutex_exit(&dn->dn_mtx);
689789Sahrens 
6901544Seschrock 	if (refcount_add(&dn->dn_holds, tag) == 1)
691789Sahrens 		dbuf_add_ref(db, dn);
692789Sahrens 
693873Sek110237 	DNODE_VERIFY(dn);
694789Sahrens 	ASSERT3P(dn->dn_dbuf, ==, db);
695789Sahrens 	ASSERT3U(dn->dn_object, ==, object);
6961544Seschrock 	dbuf_rele(db, FTAG);
697789Sahrens 
6981544Seschrock 	*dnp = dn;
6991544Seschrock 	return (0);
700789Sahrens }
701789Sahrens 
702789Sahrens /*
703789Sahrens  * Return held dnode if the object is allocated, NULL if not.
704789Sahrens  */
7051544Seschrock int
70610298SMatthew.Ahrens@Sun.COM dnode_hold(objset_t *os, uint64_t object, void *tag, dnode_t **dnp)
707789Sahrens {
7081544Seschrock 	return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, tag, dnp));
709789Sahrens }
710789Sahrens 
7114944Smaybee /*
7124944Smaybee  * Can only add a reference if there is already at least one
7134944Smaybee  * reference on the dnode.  Returns FALSE if unable to add a
7144944Smaybee  * new reference.
7154944Smaybee  */
7164944Smaybee boolean_t
7171544Seschrock dnode_add_ref(dnode_t *dn, void *tag)
718789Sahrens {
7194944Smaybee 	mutex_enter(&dn->dn_mtx);
7204944Smaybee 	if (refcount_is_zero(&dn->dn_holds)) {
7214944Smaybee 		mutex_exit(&dn->dn_mtx);
7224944Smaybee 		return (FALSE);
7234944Smaybee 	}
7244944Smaybee 	VERIFY(1 < refcount_add(&dn->dn_holds, tag));
7254944Smaybee 	mutex_exit(&dn->dn_mtx);
7264944Smaybee 	return (TRUE);
727789Sahrens }
728789Sahrens 
729789Sahrens void
7301544Seschrock dnode_rele(dnode_t *dn, void *tag)
731789Sahrens {
732789Sahrens 	uint64_t refs;
733789Sahrens 
7344944Smaybee 	mutex_enter(&dn->dn_mtx);
7351544Seschrock 	refs = refcount_remove(&dn->dn_holds, tag);
7364944Smaybee 	mutex_exit(&dn->dn_mtx);
737789Sahrens 	/* NOTE: the DNODE_DNODE does not have a dn_dbuf */
738789Sahrens 	if (refs == 0 && dn->dn_dbuf)
7391544Seschrock 		dbuf_rele(dn->dn_dbuf, dn);
740789Sahrens }
741789Sahrens 
742789Sahrens void
743789Sahrens dnode_setdirty(dnode_t *dn, dmu_tx_t *tx)
744789Sahrens {
74510298SMatthew.Ahrens@Sun.COM 	objset_t *os = dn->dn_objset;
746789Sahrens 	uint64_t txg = tx->tx_txg;
747789Sahrens 
7489396SMatthew.Ahrens@Sun.COM 	if (DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
7499396SMatthew.Ahrens@Sun.COM 		dsl_dataset_dirty(os->os_dsl_dataset, tx);
750789Sahrens 		return;
7519396SMatthew.Ahrens@Sun.COM 	}
752789Sahrens 
753873Sek110237 	DNODE_VERIFY(dn);
754789Sahrens 
755789Sahrens #ifdef ZFS_DEBUG
756789Sahrens 	mutex_enter(&dn->dn_mtx);
757789Sahrens 	ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg);
758789Sahrens 	/* ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg); */
759789Sahrens 	mutex_exit(&dn->dn_mtx);
760789Sahrens #endif
761789Sahrens 
76211935SMark.Shellenbaum@Sun.COM 	/*
76311935SMark.Shellenbaum@Sun.COM 	 * Determine old uid/gid when necessary
76411935SMark.Shellenbaum@Sun.COM 	 */
76512178SMark.Shellenbaum@Sun.COM 	dmu_objset_userquota_get_ids(dn, B_TRUE, tx);
76611935SMark.Shellenbaum@Sun.COM 
767789Sahrens 	mutex_enter(&os->os_lock);
768789Sahrens 
769789Sahrens 	/*
770789Sahrens 	 * If we are already marked dirty, we're done.
771789Sahrens 	 */
7721596Sahrens 	if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) {
773789Sahrens 		mutex_exit(&os->os_lock);
774789Sahrens 		return;
775789Sahrens 	}
776789Sahrens 
777789Sahrens 	ASSERT(!refcount_is_zero(&dn->dn_holds) || list_head(&dn->dn_dbufs));
778789Sahrens 	ASSERT(dn->dn_datablksz != 0);
7794944Smaybee 	ASSERT3U(dn->dn_next_bonuslen[txg&TXG_MASK], ==, 0);
7801599Sahrens 	ASSERT3U(dn->dn_next_blksz[txg&TXG_MASK], ==, 0);
78111935SMark.Shellenbaum@Sun.COM 	ASSERT3U(dn->dn_next_bonustype[txg&TXG_MASK], ==, 0);
782789Sahrens 
783789Sahrens 	dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
784789Sahrens 	    dn->dn_object, txg);
785789Sahrens 
786789Sahrens 	if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) {
787789Sahrens 		list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn);
788789Sahrens 	} else {
789789Sahrens 		list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn);
790789Sahrens 	}
791789Sahrens 
792789Sahrens 	mutex_exit(&os->os_lock);
793789Sahrens 
794789Sahrens 	/*
795789Sahrens 	 * The dnode maintains a hold on its containing dbuf as
796789Sahrens 	 * long as there are holds on it.  Each instantiated child
797789Sahrens 	 * dbuf maintaines a hold on the dnode.  When the last child
798789Sahrens 	 * drops its hold, the dnode will drop its hold on the
799789Sahrens 	 * containing dbuf. We add a "dirty hold" here so that the
800789Sahrens 	 * dnode will hang around after we finish processing its
801789Sahrens 	 * children.
802789Sahrens 	 */
8034944Smaybee 	VERIFY(dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg));
804789Sahrens 
8053547Smaybee 	(void) dbuf_dirty(dn->dn_dbuf, tx);
806789Sahrens 
807789Sahrens 	dsl_dataset_dirty(os->os_dsl_dataset, tx);
808789Sahrens }
809789Sahrens 
810789Sahrens void
811789Sahrens dnode_free(dnode_t *dn, dmu_tx_t *tx)
812789Sahrens {
8131596Sahrens 	int txgoff = tx->tx_txg & TXG_MASK;
8141596Sahrens 
815789Sahrens 	dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg);
816789Sahrens 
817789Sahrens 	/* we should be the only holder... hopefully */
818789Sahrens 	/* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */
819789Sahrens 
820789Sahrens 	mutex_enter(&dn->dn_mtx);
821789Sahrens 	if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) {
822789Sahrens 		mutex_exit(&dn->dn_mtx);
823789Sahrens 		return;
824789Sahrens 	}
825789Sahrens 	dn->dn_free_txg = tx->tx_txg;
826789Sahrens 	mutex_exit(&dn->dn_mtx);
827789Sahrens 
828789Sahrens 	/*
829789Sahrens 	 * If the dnode is already dirty, it needs to be moved from
830789Sahrens 	 * the dirty list to the free list.
831789Sahrens 	 */
832789Sahrens 	mutex_enter(&dn->dn_objset->os_lock);
8331596Sahrens 	if (list_link_active(&dn->dn_dirty_link[txgoff])) {
8341596Sahrens 		list_remove(&dn->dn_objset->os_dirty_dnodes[txgoff], dn);
8351596Sahrens 		list_insert_tail(&dn->dn_objset->os_free_dnodes[txgoff], dn);
836789Sahrens 		mutex_exit(&dn->dn_objset->os_lock);
837789Sahrens 	} else {
838789Sahrens 		mutex_exit(&dn->dn_objset->os_lock);
839789Sahrens 		dnode_setdirty(dn, tx);
840789Sahrens 	}
841789Sahrens }
842789Sahrens 
843789Sahrens /*
844789Sahrens  * Try to change the block size for the indicated dnode.  This can only
845789Sahrens  * succeed if there are no blocks allocated or dirty beyond first block
846789Sahrens  */
847789Sahrens int
848789Sahrens dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx)
849789Sahrens {
850789Sahrens 	dmu_buf_impl_t *db, *db_next;
8516992Smaybee 	int err;
852789Sahrens 
853789Sahrens 	if (size == 0)
854789Sahrens 		size = SPA_MINBLOCKSIZE;
855789Sahrens 	if (size > SPA_MAXBLOCKSIZE)
856789Sahrens 		size = SPA_MAXBLOCKSIZE;
857789Sahrens 	else
858789Sahrens 		size = P2ROUNDUP(size, SPA_MINBLOCKSIZE);
859789Sahrens 
8602445Sahrens 	if (ibs == dn->dn_indblkshift)
8612445Sahrens 		ibs = 0;
862789Sahrens 
8632445Sahrens 	if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0)
864789Sahrens 		return (0);
865789Sahrens 
866789Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
867789Sahrens 
868789Sahrens 	/* Check for any allocated blocks beyond the first */
869789Sahrens 	if (dn->dn_phys->dn_maxblkid != 0)
8702445Sahrens 		goto fail;
871789Sahrens 
872789Sahrens 	mutex_enter(&dn->dn_dbufs_mtx);
873789Sahrens 	for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
874789Sahrens 		db_next = list_next(&dn->dn_dbufs, db);
875789Sahrens 
87611935SMark.Shellenbaum@Sun.COM 		if (db->db_blkid != 0 && db->db_blkid != DMU_BONUS_BLKID &&
87711935SMark.Shellenbaum@Sun.COM 		    db->db_blkid != DMU_SPILL_BLKID) {
878789Sahrens 			mutex_exit(&dn->dn_dbufs_mtx);
8792445Sahrens 			goto fail;
880789Sahrens 		}
881789Sahrens 	}
882789Sahrens 	mutex_exit(&dn->dn_dbufs_mtx);
883789Sahrens 
8842445Sahrens 	if (ibs && dn->dn_nlevels != 1)
8852445Sahrens 		goto fail;
8862445Sahrens 
8876992Smaybee 	/* resize the old block */
8886992Smaybee 	err = dbuf_hold_impl(dn, 0, 0, TRUE, FTAG, &db);
8896992Smaybee 	if (err == 0)
8901596Sahrens 		dbuf_new_size(db, size, tx);
8916992Smaybee 	else if (err != ENOENT)
8926992Smaybee 		goto fail;
893789Sahrens 
894789Sahrens 	dnode_setdblksz(dn, size);
8951596Sahrens 	dnode_setdirty(dn, tx);
8961596Sahrens 	dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size;
8972445Sahrens 	if (ibs) {
8982445Sahrens 		dn->dn_indblkshift = ibs;
8992445Sahrens 		dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs;
9002445Sahrens 	}
9016992Smaybee 	/* rele after we have fixed the blocksize in the dnode */
9021596Sahrens 	if (db)
9031596Sahrens 		dbuf_rele(db, FTAG);
904789Sahrens 
905789Sahrens 	rw_exit(&dn->dn_struct_rwlock);
9062445Sahrens 	return (0);
9072445Sahrens 
9082445Sahrens fail:
9092445Sahrens 	rw_exit(&dn->dn_struct_rwlock);
9102445Sahrens 	return (ENOTSUP);
911789Sahrens }
912789Sahrens 
9137332SJonathan.Adams@Sun.COM /* read-holding callers must not rely on the lock being continuously held */
914789Sahrens void
9157332SJonathan.Adams@Sun.COM dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t have_read)
916789Sahrens {
917789Sahrens 	uint64_t txgoff = tx->tx_txg & TXG_MASK;
9181596Sahrens 	int epbs, new_nlevels;
919789Sahrens 	uint64_t sz;
920789Sahrens 
92111935SMark.Shellenbaum@Sun.COM 	ASSERT(blkid != DMU_BONUS_BLKID);
922789Sahrens 
9237332SJonathan.Adams@Sun.COM 	ASSERT(have_read ?
9247332SJonathan.Adams@Sun.COM 	    RW_READ_HELD(&dn->dn_struct_rwlock) :
9257332SJonathan.Adams@Sun.COM 	    RW_WRITE_HELD(&dn->dn_struct_rwlock));
9267332SJonathan.Adams@Sun.COM 
9277332SJonathan.Adams@Sun.COM 	/*
9287332SJonathan.Adams@Sun.COM 	 * if we have a read-lock, check to see if we need to do any work
9297332SJonathan.Adams@Sun.COM 	 * before upgrading to a write-lock.
9307332SJonathan.Adams@Sun.COM 	 */
9317332SJonathan.Adams@Sun.COM 	if (have_read) {
9327332SJonathan.Adams@Sun.COM 		if (blkid <= dn->dn_maxblkid)
9337332SJonathan.Adams@Sun.COM 			return;
9347332SJonathan.Adams@Sun.COM 
9357332SJonathan.Adams@Sun.COM 		if (!rw_tryupgrade(&dn->dn_struct_rwlock)) {
9367332SJonathan.Adams@Sun.COM 			rw_exit(&dn->dn_struct_rwlock);
9377332SJonathan.Adams@Sun.COM 			rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
9387332SJonathan.Adams@Sun.COM 		}
939789Sahrens 	}
940789Sahrens 
9411596Sahrens 	if (blkid <= dn->dn_maxblkid)
9421596Sahrens 		goto out;
9431596Sahrens 
9441596Sahrens 	dn->dn_maxblkid = blkid;
945789Sahrens 
946789Sahrens 	/*
9471596Sahrens 	 * Compute the number of levels necessary to support the new maxblkid.
948789Sahrens 	 */
949789Sahrens 	new_nlevels = 1;
950789Sahrens 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
9511596Sahrens 	for (sz = dn->dn_nblkptr;
9521596Sahrens 	    sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs)
953789Sahrens 		new_nlevels++;
954789Sahrens 
9551596Sahrens 	if (new_nlevels > dn->dn_nlevels) {
9561596Sahrens 		int old_nlevels = dn->dn_nlevels;
9571596Sahrens 		dmu_buf_impl_t *db;
9583547Smaybee 		list_t *list;
9593547Smaybee 		dbuf_dirty_record_t *new, *dr, *dr_next;
9601596Sahrens 
9611596Sahrens 		dn->dn_nlevels = new_nlevels;
9621596Sahrens 
9631596Sahrens 		ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]);
964789Sahrens 		dn->dn_next_nlevels[txgoff] = new_nlevels;
965789Sahrens 
9663547Smaybee 		/* dirty the left indirects */
9671596Sahrens 		db = dbuf_hold_level(dn, old_nlevels, 0, FTAG);
96812411SJohn.Harres@Sun.COM 		ASSERT(db != NULL);
9693547Smaybee 		new = dbuf_dirty(db, tx);
9701544Seschrock 		dbuf_rele(db, FTAG);
9711596Sahrens 
9723547Smaybee 		/* transfer the dirty records to the new indirect */
9733547Smaybee 		mutex_enter(&dn->dn_mtx);
9743547Smaybee 		mutex_enter(&new->dt.di.dr_mtx);
9753547Smaybee 		list = &dn->dn_dirty_records[txgoff];
9763547Smaybee 		for (dr = list_head(list); dr; dr = dr_next) {
9773547Smaybee 			dr_next = list_next(&dn->dn_dirty_records[txgoff], dr);
9783547Smaybee 			if (dr->dr_dbuf->db_level != new_nlevels-1 &&
97911935SMark.Shellenbaum@Sun.COM 			    dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
98011935SMark.Shellenbaum@Sun.COM 			    dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
9813547Smaybee 				ASSERT(dr->dr_dbuf->db_level == old_nlevels-1);
9823547Smaybee 				list_remove(&dn->dn_dirty_records[txgoff], dr);
9833547Smaybee 				list_insert_tail(&new->dt.di.dr_children, dr);
9843547Smaybee 				dr->dr_parent = new;
9853547Smaybee 			}
9863547Smaybee 		}
9873547Smaybee 		mutex_exit(&new->dt.di.dr_mtx);
9883547Smaybee 		mutex_exit(&dn->dn_mtx);
989789Sahrens 	}
990789Sahrens 
991789Sahrens out:
9927332SJonathan.Adams@Sun.COM 	if (have_read)
9937332SJonathan.Adams@Sun.COM 		rw_downgrade(&dn->dn_struct_rwlock);
994789Sahrens }
995789Sahrens 
996789Sahrens void
997789Sahrens dnode_clear_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
998789Sahrens {
999789Sahrens 	avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
1000789Sahrens 	avl_index_t where;
1001789Sahrens 	free_range_t *rp;
1002789Sahrens 	free_range_t rp_tofind;
1003789Sahrens 	uint64_t endblk = blkid + nblks;
1004789Sahrens 
1005789Sahrens 	ASSERT(MUTEX_HELD(&dn->dn_mtx));
1006789Sahrens 	ASSERT(nblks <= UINT64_MAX - blkid); /* no overflow */
1007789Sahrens 
1008789Sahrens 	dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1009789Sahrens 	    blkid, nblks, tx->tx_txg);
1010789Sahrens 	rp_tofind.fr_blkid = blkid;
1011789Sahrens 	rp = avl_find(tree, &rp_tofind, &where);
1012789Sahrens 	if (rp == NULL)
1013789Sahrens 		rp = avl_nearest(tree, where, AVL_BEFORE);
1014789Sahrens 	if (rp == NULL)
1015789Sahrens 		rp = avl_nearest(tree, where, AVL_AFTER);
1016789Sahrens 
1017789Sahrens 	while (rp && (rp->fr_blkid <= blkid + nblks)) {
1018789Sahrens 		uint64_t fr_endblk = rp->fr_blkid + rp->fr_nblks;
1019789Sahrens 		free_range_t *nrp = AVL_NEXT(tree, rp);
1020789Sahrens 
1021789Sahrens 		if (blkid <= rp->fr_blkid && endblk >= fr_endblk) {
1022789Sahrens 			/* clear this entire range */
1023789Sahrens 			avl_remove(tree, rp);
1024789Sahrens 			kmem_free(rp, sizeof (free_range_t));
1025789Sahrens 		} else if (blkid <= rp->fr_blkid &&
1026789Sahrens 		    endblk > rp->fr_blkid && endblk < fr_endblk) {
1027789Sahrens 			/* clear the beginning of this range */
1028789Sahrens 			rp->fr_blkid = endblk;
1029789Sahrens 			rp->fr_nblks = fr_endblk - endblk;
1030789Sahrens 		} else if (blkid > rp->fr_blkid && blkid < fr_endblk &&
1031789Sahrens 		    endblk >= fr_endblk) {
1032789Sahrens 			/* clear the end of this range */
1033789Sahrens 			rp->fr_nblks = blkid - rp->fr_blkid;
1034789Sahrens 		} else if (blkid > rp->fr_blkid && endblk < fr_endblk) {
1035789Sahrens 			/* clear a chunk out of this range */
1036789Sahrens 			free_range_t *new_rp =
1037789Sahrens 			    kmem_alloc(sizeof (free_range_t), KM_SLEEP);
1038789Sahrens 
1039789Sahrens 			new_rp->fr_blkid = endblk;
1040789Sahrens 			new_rp->fr_nblks = fr_endblk - endblk;
1041789Sahrens 			avl_insert_here(tree, new_rp, rp, AVL_AFTER);
1042789Sahrens 			rp->fr_nblks = blkid - rp->fr_blkid;
1043789Sahrens 		}
1044789Sahrens 		/* there may be no overlap */
1045789Sahrens 		rp = nrp;
1046789Sahrens 	}
1047789Sahrens }
1048789Sahrens 
1049789Sahrens void
1050789Sahrens dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx)
1051789Sahrens {
1052789Sahrens 	dmu_buf_impl_t *db;
10532445Sahrens 	uint64_t blkoff, blkid, nblks;
10546992Smaybee 	int blksz, blkshift, head, tail;
1055789Sahrens 	int trunc = FALSE;
10566992Smaybee 	int epbs;
1057789Sahrens 
1058789Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1059789Sahrens 	blksz = dn->dn_datablksz;
10606992Smaybee 	blkshift = dn->dn_datablkshift;
10616992Smaybee 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1062789Sahrens 
1063789Sahrens 	if (len == -1ULL) {
1064789Sahrens 		len = UINT64_MAX - off;
1065789Sahrens 		trunc = TRUE;
1066789Sahrens 	}
1067789Sahrens 
1068789Sahrens 	/*
1069789Sahrens 	 * First, block align the region to free:
1070789Sahrens 	 */
10712445Sahrens 	if (ISP2(blksz)) {
10722445Sahrens 		head = P2NPHASE(off, blksz);
10732445Sahrens 		blkoff = P2PHASE(off, blksz);
10746992Smaybee 		if ((off >> blkshift) > dn->dn_maxblkid)
10756992Smaybee 			goto out;
10762445Sahrens 	} else {
10772445Sahrens 		ASSERT(dn->dn_maxblkid == 0);
10782445Sahrens 		if (off == 0 && len >= blksz) {
10796992Smaybee 			/* Freeing the whole block; fast-track this request */
10806992Smaybee 			blkid = 0;
10816992Smaybee 			nblks = 1;
10826992Smaybee 			goto done;
10837385SMark.Maybee@Sun.COM 		} else if (off >= blksz) {
10846992Smaybee 			/* Freeing past end-of-data */
10856992Smaybee 			goto out;
1086789Sahrens 		} else {
10872445Sahrens 			/* Freeing part of the block. */
1088789Sahrens 			head = blksz - off;
1089789Sahrens 			ASSERT3U(head, >, 0);
1090789Sahrens 		}
10912445Sahrens 		blkoff = off;
1092789Sahrens 	}
1093789Sahrens 	/* zero out any partial block data at the start of the range */
1094789Sahrens 	if (head) {
10952445Sahrens 		ASSERT3U(blkoff + head, ==, blksz);
1096789Sahrens 		if (len < head)
1097789Sahrens 			head = len;
1098789Sahrens 		if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off), TRUE,
1099789Sahrens 		    FTAG, &db) == 0) {
1100789Sahrens 			caddr_t data;
1101789Sahrens 
1102789Sahrens 			/* don't dirty if it isn't on disk and isn't dirty */
11033547Smaybee 			if (db->db_last_dirty ||
1104789Sahrens 			    (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1105789Sahrens 				rw_exit(&dn->dn_struct_rwlock);
1106789Sahrens 				dbuf_will_dirty(db, tx);
1107789Sahrens 				rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1108789Sahrens 				data = db->db.db_data;
11092445Sahrens 				bzero(data + blkoff, head);
1110789Sahrens 			}
11111544Seschrock 			dbuf_rele(db, FTAG);
1112789Sahrens 		}
1113789Sahrens 		off += head;
1114789Sahrens 		len -= head;
1115789Sahrens 	}
1116789Sahrens 
11172445Sahrens 	/* If the range was less than one block, we're done */
11186992Smaybee 	if (len == 0)
11196992Smaybee 		goto out;
11206992Smaybee 
11216992Smaybee 	/* If the remaining range is past end of file, we're done */
11226992Smaybee 	if ((off >> blkshift) > dn->dn_maxblkid)
11236992Smaybee 		goto out;
11246992Smaybee 
11257385SMark.Maybee@Sun.COM 	ASSERT(ISP2(blksz));
11266992Smaybee 	if (trunc)
11276992Smaybee 		tail = 0;
11286992Smaybee 	else
11296992Smaybee 		tail = P2PHASE(len, blksz);
11306992Smaybee 
11316992Smaybee 	ASSERT3U(P2PHASE(off, blksz), ==, 0);
11326992Smaybee 	/* zero out any partial block data at the end of the range */
11336992Smaybee 	if (tail) {
11346992Smaybee 		if (len < tail)
11356992Smaybee 			tail = len;
11366992Smaybee 		if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len),
11376992Smaybee 		    TRUE, FTAG, &db) == 0) {
11386992Smaybee 			/* don't dirty if not on disk and not dirty */
11396992Smaybee 			if (db->db_last_dirty ||
11406992Smaybee 			    (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
11416992Smaybee 				rw_exit(&dn->dn_struct_rwlock);
11426992Smaybee 				dbuf_will_dirty(db, tx);
11436992Smaybee 				rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
11446992Smaybee 				bzero(db->db.db_data, tail);
11456992Smaybee 			}
11466992Smaybee 			dbuf_rele(db, FTAG);
11476992Smaybee 		}
11486992Smaybee 		len -= tail;
11496992Smaybee 	}
11506992Smaybee 
11516992Smaybee 	/* If the range did not include a full block, we are done */
11526992Smaybee 	if (len == 0)
1153789Sahrens 		goto out;
1154789Sahrens 
11556992Smaybee 	ASSERT(IS_P2ALIGNED(off, blksz));
11566992Smaybee 	ASSERT(trunc || IS_P2ALIGNED(len, blksz));
11576992Smaybee 	blkid = off >> blkshift;
11586992Smaybee 	nblks = len >> blkshift;
11596992Smaybee 	if (trunc)
11606992Smaybee 		nblks += 1;
11612445Sahrens 
11626992Smaybee 	/*
11636992Smaybee 	 * Read in and mark all the level-1 indirects dirty,
11646992Smaybee 	 * so that they will stay in memory until syncing phase.
11657049Smaybee 	 * Always dirty the first and last indirect to make sure
11667049Smaybee 	 * we dirty all the partial indirects.
11676992Smaybee 	 */
11686992Smaybee 	if (dn->dn_nlevels > 1) {
11696992Smaybee 		uint64_t i, first, last;
11706992Smaybee 		int shift = epbs + dn->dn_datablkshift;
11712445Sahrens 
11726992Smaybee 		first = blkid >> epbs;
11737049Smaybee 		if (db = dbuf_hold_level(dn, 1, first, FTAG)) {
11747049Smaybee 			dbuf_will_dirty(db, tx);
11757049Smaybee 			dbuf_rele(db, FTAG);
11767049Smaybee 		}
11776992Smaybee 		if (trunc)
11786992Smaybee 			last = dn->dn_maxblkid >> epbs;
11792445Sahrens 		else
11806992Smaybee 			last = (blkid + nblks - 1) >> epbs;
11817049Smaybee 		if (last > first && (db = dbuf_hold_level(dn, 1, last, FTAG))) {
11827049Smaybee 			dbuf_will_dirty(db, tx);
11837049Smaybee 			dbuf_rele(db, FTAG);
11847049Smaybee 		}
11857049Smaybee 		for (i = first + 1; i < last; i++) {
11866992Smaybee 			uint64_t ibyte = i << shift;
11876992Smaybee 			int err;
1188789Sahrens 
11896992Smaybee 			err = dnode_next_offset(dn,
11906992Smaybee 			    DNODE_FIND_HAVELOCK, &ibyte, 1, 1, 0);
11916992Smaybee 			i = ibyte >> shift;
11927049Smaybee 			if (err == ESRCH || i >= last)
11936992Smaybee 				break;
11946992Smaybee 			ASSERT(err == 0);
11956992Smaybee 			db = dbuf_hold_level(dn, 1, i, FTAG);
11966992Smaybee 			if (db) {
11976992Smaybee 				dbuf_will_dirty(db, tx);
11982445Sahrens 				dbuf_rele(db, FTAG);
1199789Sahrens 			}
12002445Sahrens 		}
12012445Sahrens 	}
12026992Smaybee done:
12036992Smaybee 	/*
12046992Smaybee 	 * Add this range to the dnode range list.
12056992Smaybee 	 * We will finish up this free operation in the syncing phase.
12066992Smaybee 	 */
1207789Sahrens 	mutex_enter(&dn->dn_mtx);
1208789Sahrens 	dnode_clear_range(dn, blkid, nblks, tx);
1209789Sahrens 	{
1210789Sahrens 		free_range_t *rp, *found;
1211789Sahrens 		avl_index_t where;
1212789Sahrens 		avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
1213789Sahrens 
1214789Sahrens 		/* Add new range to dn_ranges */
1215789Sahrens 		rp = kmem_alloc(sizeof (free_range_t), KM_SLEEP);
1216789Sahrens 		rp->fr_blkid = blkid;
1217789Sahrens 		rp->fr_nblks = nblks;
1218789Sahrens 		found = avl_find(tree, rp, &where);
1219789Sahrens 		ASSERT(found == NULL);
1220789Sahrens 		avl_insert(tree, rp, where);
1221789Sahrens 		dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1222789Sahrens 		    blkid, nblks, tx->tx_txg);
1223789Sahrens 	}
1224789Sahrens 	mutex_exit(&dn->dn_mtx);
1225789Sahrens 
12266992Smaybee 	dbuf_free_range(dn, blkid, blkid + nblks - 1, tx);
1227789Sahrens 	dnode_setdirty(dn, tx);
1228789Sahrens out:
12296992Smaybee 	if (trunc && dn->dn_maxblkid >= (off >> blkshift))
12306992Smaybee 		dn->dn_maxblkid = (off >> blkshift ? (off >> blkshift) - 1 : 0);
12316992Smaybee 
1232789Sahrens 	rw_exit(&dn->dn_struct_rwlock);
1233789Sahrens }
1234789Sahrens 
123511935SMark.Shellenbaum@Sun.COM static boolean_t
123611935SMark.Shellenbaum@Sun.COM dnode_spill_freed(dnode_t *dn)
123711935SMark.Shellenbaum@Sun.COM {
123811935SMark.Shellenbaum@Sun.COM 	int i;
123911935SMark.Shellenbaum@Sun.COM 
124011935SMark.Shellenbaum@Sun.COM 	mutex_enter(&dn->dn_mtx);
124111935SMark.Shellenbaum@Sun.COM 	for (i = 0; i < TXG_SIZE; i++) {
124211935SMark.Shellenbaum@Sun.COM 		if (dn->dn_rm_spillblk[i] == DN_KILL_SPILLBLK)
124311935SMark.Shellenbaum@Sun.COM 			break;
124411935SMark.Shellenbaum@Sun.COM 	}
124511935SMark.Shellenbaum@Sun.COM 	mutex_exit(&dn->dn_mtx);
124611935SMark.Shellenbaum@Sun.COM 	return (i < TXG_SIZE);
124711935SMark.Shellenbaum@Sun.COM }
124811935SMark.Shellenbaum@Sun.COM 
1249789Sahrens /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
1250789Sahrens uint64_t
1251789Sahrens dnode_block_freed(dnode_t *dn, uint64_t blkid)
1252789Sahrens {
1253789Sahrens 	free_range_t range_tofind;
1254789Sahrens 	void *dp = spa_get_dsl(dn->dn_objset->os_spa);
1255789Sahrens 	int i;
1256789Sahrens 
125711935SMark.Shellenbaum@Sun.COM 	if (blkid == DMU_BONUS_BLKID)
1258789Sahrens 		return (FALSE);
1259789Sahrens 
1260789Sahrens 	/*
1261789Sahrens 	 * If we're in the process of opening the pool, dp will not be
1262789Sahrens 	 * set yet, but there shouldn't be anything dirty.
1263789Sahrens 	 */
1264789Sahrens 	if (dp == NULL)
1265789Sahrens 		return (FALSE);
1266789Sahrens 
1267789Sahrens 	if (dn->dn_free_txg)
1268789Sahrens 		return (TRUE);
1269789Sahrens 
127011935SMark.Shellenbaum@Sun.COM 	if (blkid == DMU_SPILL_BLKID)
127111935SMark.Shellenbaum@Sun.COM 		return (dnode_spill_freed(dn));
127211935SMark.Shellenbaum@Sun.COM 
1273789Sahrens 	range_tofind.fr_blkid = blkid;
1274789Sahrens 	mutex_enter(&dn->dn_mtx);
1275789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
1276789Sahrens 		free_range_t *range_found;
1277789Sahrens 		avl_index_t idx;
1278789Sahrens 
1279789Sahrens 		range_found = avl_find(&dn->dn_ranges[i], &range_tofind, &idx);
1280789Sahrens 		if (range_found) {
1281789Sahrens 			ASSERT(range_found->fr_nblks > 0);
1282789Sahrens 			break;
1283789Sahrens 		}
1284789Sahrens 		range_found = avl_nearest(&dn->dn_ranges[i], idx, AVL_BEFORE);
1285789Sahrens 		if (range_found &&
1286789Sahrens 		    range_found->fr_blkid + range_found->fr_nblks > blkid)
1287789Sahrens 			break;
1288789Sahrens 	}
1289789Sahrens 	mutex_exit(&dn->dn_mtx);
1290789Sahrens 	return (i < TXG_SIZE);
1291789Sahrens }
1292789Sahrens 
1293789Sahrens /* call from syncing context when we actually write/free space for this dnode */
1294789Sahrens void
12952082Seschrock dnode_diduse_space(dnode_t *dn, int64_t delta)
1296789Sahrens {
12972082Seschrock 	uint64_t space;
12982082Seschrock 	dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n",
1299789Sahrens 	    dn, dn->dn_phys,
13002082Seschrock 	    (u_longlong_t)dn->dn_phys->dn_used,
13012082Seschrock 	    (longlong_t)delta);
1302789Sahrens 
1303789Sahrens 	mutex_enter(&dn->dn_mtx);
13042082Seschrock 	space = DN_USED_BYTES(dn->dn_phys);
13052082Seschrock 	if (delta > 0) {
13062082Seschrock 		ASSERT3U(space + delta, >=, space); /* no overflow */
1307789Sahrens 	} else {
13082082Seschrock 		ASSERT3U(space, >=, -delta); /* no underflow */
13092082Seschrock 	}
13102082Seschrock 	space += delta;
13114577Sahrens 	if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_DNODE_BYTES) {
13122082Seschrock 		ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0);
13132082Seschrock 		ASSERT3U(P2PHASE(space, 1<<DEV_BSHIFT), ==, 0);
13142082Seschrock 		dn->dn_phys->dn_used = space >> DEV_BSHIFT;
13152082Seschrock 	} else {
13162082Seschrock 		dn->dn_phys->dn_used = space;
13172082Seschrock 		dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES;
1318789Sahrens 	}
1319789Sahrens 	mutex_exit(&dn->dn_mtx);
1320789Sahrens }
1321789Sahrens 
1322789Sahrens /*
1323789Sahrens  * Call when we think we're going to write/free space in open context.
1324789Sahrens  * Be conservative (ie. OK to write less than this or free more than
1325789Sahrens  * this, but don't write more or free less).
1326789Sahrens  */
1327789Sahrens void
1328789Sahrens dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx)
1329789Sahrens {
133010298SMatthew.Ahrens@Sun.COM 	objset_t *os = dn->dn_objset;
1331789Sahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1332789Sahrens 
1333789Sahrens 	if (space > 0)
1334789Sahrens 		space = spa_get_asize(os->os_spa, space);
1335789Sahrens 
1336789Sahrens 	if (ds)
1337789Sahrens 		dsl_dir_willuse_space(ds->ds_dir, space, tx);
1338789Sahrens 
1339789Sahrens 	dmu_tx_willuse_space(tx, space);
1340789Sahrens }
1341789Sahrens 
13429950SMark.Maybee@Sun.COM /*
13439950SMark.Maybee@Sun.COM  * This function scans a block at the indicated "level" looking for
13449950SMark.Maybee@Sun.COM  * a hole or data (depending on 'flags').  If level > 0, then we are
13459950SMark.Maybee@Sun.COM  * scanning an indirect block looking at its pointers.  If level == 0,
13469950SMark.Maybee@Sun.COM  * then we are looking at a block of dnodes.  If we don't find what we
13479950SMark.Maybee@Sun.COM  * are looking for in the block, we return ESRCH.  Otherwise, return
13489950SMark.Maybee@Sun.COM  * with *offset pointing to the beginning (if searching forwards) or
13499950SMark.Maybee@Sun.COM  * end (if searching backwards) of the range covered by the block
13509950SMark.Maybee@Sun.COM  * pointer we matched on (or dnode).
13519950SMark.Maybee@Sun.COM  *
13529950SMark.Maybee@Sun.COM  * The basic search algorithm used below by dnode_next_offset() is to
13539950SMark.Maybee@Sun.COM  * use this function to search up the block tree (widen the search) until
13549950SMark.Maybee@Sun.COM  * we find something (i.e., we don't return ESRCH) and then search back
13559950SMark.Maybee@Sun.COM  * down the tree (narrow the search) until we reach our original search
13569950SMark.Maybee@Sun.COM  * level.
13579950SMark.Maybee@Sun.COM  */
1358789Sahrens static int
13596992Smaybee dnode_next_offset_level(dnode_t *dn, int flags, uint64_t *offset,
13603025Sahrens 	int lvl, uint64_t blkfill, uint64_t txg)
1361789Sahrens {
1362789Sahrens 	dmu_buf_impl_t *db = NULL;
1363789Sahrens 	void *data = NULL;
1364789Sahrens 	uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
1365789Sahrens 	uint64_t epb = 1ULL << epbs;
1366789Sahrens 	uint64_t minfill, maxfill;
13676992Smaybee 	boolean_t hole;
13686992Smaybee 	int i, inc, error, span;
1369789Sahrens 
1370789Sahrens 	dprintf("probing object %llu offset %llx level %d of %u\n",
1371789Sahrens 	    dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels);
1372789Sahrens 
13739396SMatthew.Ahrens@Sun.COM 	hole = ((flags & DNODE_FIND_HOLE) != 0);
13746992Smaybee 	inc = (flags & DNODE_FIND_BACKWARDS) ? -1 : 1;
13757385SMark.Maybee@Sun.COM 	ASSERT(txg == 0 || !hole);
13766992Smaybee 
1377789Sahrens 	if (lvl == dn->dn_phys->dn_nlevels) {
1378789Sahrens 		error = 0;
1379789Sahrens 		epb = dn->dn_phys->dn_nblkptr;
1380789Sahrens 		data = dn->dn_phys->dn_blkptr;
1381789Sahrens 	} else {
1382789Sahrens 		uint64_t blkid = dbuf_whichblock(dn, *offset) >> (epbs * lvl);
1383789Sahrens 		error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FTAG, &db);
1384789Sahrens 		if (error) {
13857385SMark.Maybee@Sun.COM 			if (error != ENOENT)
13867385SMark.Maybee@Sun.COM 				return (error);
13877385SMark.Maybee@Sun.COM 			if (hole)
13887385SMark.Maybee@Sun.COM 				return (0);
13897385SMark.Maybee@Sun.COM 			/*
13907385SMark.Maybee@Sun.COM 			 * This can only happen when we are searching up
13917385SMark.Maybee@Sun.COM 			 * the block tree for data.  We don't really need to
13927385SMark.Maybee@Sun.COM 			 * adjust the offset, as we will just end up looking
13937385SMark.Maybee@Sun.COM 			 * at the pointer to this block in its parent, and its
13947385SMark.Maybee@Sun.COM 			 * going to be unallocated, so we will skip over it.
13957385SMark.Maybee@Sun.COM 			 */
13967385SMark.Maybee@Sun.COM 			return (ESRCH);
1397789Sahrens 		}
13981793Sahrens 		error = dbuf_read(db, NULL, DB_RF_CANFAIL | DB_RF_HAVESTRUCT);
13991793Sahrens 		if (error) {
14001793Sahrens 			dbuf_rele(db, FTAG);
14011793Sahrens 			return (error);
14021793Sahrens 		}
1403789Sahrens 		data = db->db.db_data;
1404789Sahrens 	}
1405789Sahrens 
14063025Sahrens 	if (db && txg &&
14073025Sahrens 	    (db->db_blkptr == NULL || db->db_blkptr->blk_birth <= txg)) {
14087385SMark.Maybee@Sun.COM 		/*
14097385SMark.Maybee@Sun.COM 		 * This can only happen when we are searching up the tree
14107385SMark.Maybee@Sun.COM 		 * and these conditions mean that we need to keep climbing.
14117385SMark.Maybee@Sun.COM 		 */
14123025Sahrens 		error = ESRCH;
14133025Sahrens 	} else if (lvl == 0) {
1414789Sahrens 		dnode_phys_t *dnp = data;
1415789Sahrens 		span = DNODE_SHIFT;
1416789Sahrens 		ASSERT(dn->dn_type == DMU_OT_DNODE);
1417789Sahrens 
14186992Smaybee 		for (i = (*offset >> span) & (blkfill - 1);
14196992Smaybee 		    i >= 0 && i < blkfill; i += inc) {
14209409SJonathan.Adams@Sun.COM 			if ((dnp[i].dn_type == DMU_OT_NONE) == hole)
1421789Sahrens 				break;
14226992Smaybee 			*offset += (1ULL << span) * inc;
1423789Sahrens 		}
14246992Smaybee 		if (i < 0 || i == blkfill)
1425789Sahrens 			error = ESRCH;
1426789Sahrens 	} else {
1427789Sahrens 		blkptr_t *bp = data;
14289950SMark.Maybee@Sun.COM 		uint64_t start = *offset;
1429789Sahrens 		span = (lvl - 1) * epbs + dn->dn_datablkshift;
1430789Sahrens 		minfill = 0;
1431789Sahrens 		maxfill = blkfill << ((lvl - 1) * epbs);
1432789Sahrens 
1433789Sahrens 		if (hole)
1434789Sahrens 			maxfill--;
1435789Sahrens 		else
1436789Sahrens 			minfill++;
1437789Sahrens 
14389950SMark.Maybee@Sun.COM 		*offset = *offset >> span;
14399950SMark.Maybee@Sun.COM 		for (i = BF64_GET(*offset, 0, epbs);
14406992Smaybee 		    i >= 0 && i < epb; i += inc) {
1441789Sahrens 			if (bp[i].blk_fill >= minfill &&
14423025Sahrens 			    bp[i].blk_fill <= maxfill &&
14437385SMark.Maybee@Sun.COM 			    (hole || bp[i].blk_birth > txg))
1444789Sahrens 				break;
14459950SMark.Maybee@Sun.COM 			if (inc > 0 || *offset > 0)
14469950SMark.Maybee@Sun.COM 				*offset += inc;
1447789Sahrens 		}
14489950SMark.Maybee@Sun.COM 		*offset = *offset << span;
14499950SMark.Maybee@Sun.COM 		if (inc < 0) {
14509950SMark.Maybee@Sun.COM 			/* traversing backwards; position offset at the end */
14519950SMark.Maybee@Sun.COM 			ASSERT3U(*offset, <=, start);
14529950SMark.Maybee@Sun.COM 			*offset = MIN(*offset + (1ULL << span) - 1, start);
14539950SMark.Maybee@Sun.COM 		} else if (*offset < start) {
14549950SMark.Maybee@Sun.COM 			*offset = start;
14559950SMark.Maybee@Sun.COM 		}
14569950SMark.Maybee@Sun.COM 		if (i < 0 || i >= epb)
1457789Sahrens 			error = ESRCH;
1458789Sahrens 	}
1459789Sahrens 
1460789Sahrens 	if (db)
14611544Seschrock 		dbuf_rele(db, FTAG);
1462789Sahrens 
1463789Sahrens 	return (error);
1464789Sahrens }
1465789Sahrens 
1466789Sahrens /*
1467789Sahrens  * Find the next hole, data, or sparse region at or after *offset.
1468789Sahrens  * The value 'blkfill' tells us how many items we expect to find
1469789Sahrens  * in an L0 data block; this value is 1 for normal objects,
1470789Sahrens  * DNODES_PER_BLOCK for the meta dnode, and some fraction of
1471789Sahrens  * DNODES_PER_BLOCK when searching for sparse regions thereof.
14723025Sahrens  *
1473789Sahrens  * Examples:
1474789Sahrens  *
14756992Smaybee  * dnode_next_offset(dn, flags, offset, 1, 1, 0);
14766992Smaybee  *	Finds the next/previous hole/data in a file.
1477789Sahrens  *	Used in dmu_offset_next().
1478789Sahrens  *
14796992Smaybee  * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
1480789Sahrens  *	Finds the next free/allocated dnode an objset's meta-dnode.
14813025Sahrens  *	Only finds objects that have new contents since txg (ie.
14823025Sahrens  *	bonus buffer changes and content removal are ignored).
1483789Sahrens  *	Used in dmu_object_next().
1484789Sahrens  *
14856992Smaybee  * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
1486789Sahrens  *	Finds the next L2 meta-dnode bp that's at most 1/4 full.
1487789Sahrens  *	Used in dmu_object_alloc().
1488789Sahrens  */
1489789Sahrens int
14906992Smaybee dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset,
14913025Sahrens     int minlvl, uint64_t blkfill, uint64_t txg)
1492789Sahrens {
14936992Smaybee 	uint64_t initial_offset = *offset;
1494789Sahrens 	int lvl, maxlvl;
1495789Sahrens 	int error = 0;
1496789Sahrens 
14976992Smaybee 	if (!(flags & DNODE_FIND_HAVELOCK))
14986992Smaybee 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
1499789Sahrens 
1500789Sahrens 	if (dn->dn_phys->dn_nlevels == 0) {
15016992Smaybee 		error = ESRCH;
15026992Smaybee 		goto out;
1503789Sahrens 	}
1504789Sahrens 
1505789Sahrens 	if (dn->dn_datablkshift == 0) {
1506789Sahrens 		if (*offset < dn->dn_datablksz) {
15076992Smaybee 			if (flags & DNODE_FIND_HOLE)
1508789Sahrens 				*offset = dn->dn_datablksz;
1509789Sahrens 		} else {
1510789Sahrens 			error = ESRCH;
1511789Sahrens 		}
15126992Smaybee 		goto out;
1513789Sahrens 	}
1514789Sahrens 
1515789Sahrens 	maxlvl = dn->dn_phys->dn_nlevels;
1516789Sahrens 
1517789Sahrens 	for (lvl = minlvl; lvl <= maxlvl; lvl++) {
15183025Sahrens 		error = dnode_next_offset_level(dn,
15196992Smaybee 		    flags, offset, lvl, blkfill, txg);
15201793Sahrens 		if (error != ESRCH)
1521789Sahrens 			break;
1522789Sahrens 	}
1523789Sahrens 
15246992Smaybee 	while (error == 0 && --lvl >= minlvl) {
15253025Sahrens 		error = dnode_next_offset_level(dn,
15266992Smaybee 		    flags, offset, lvl, blkfill, txg);
15273025Sahrens 	}
1528789Sahrens 
15296992Smaybee 	if (error == 0 && (flags & DNODE_FIND_BACKWARDS ?
15306992Smaybee 	    initial_offset < *offset : initial_offset > *offset))
15311793Sahrens 		error = ESRCH;
15326992Smaybee out:
15336992Smaybee 	if (!(flags & DNODE_FIND_HAVELOCK))
15346992Smaybee 		rw_exit(&dn->dn_struct_rwlock);
1535789Sahrens 
1536789Sahrens 	return (error);
1537789Sahrens }
1538