xref: /onnv-gate/usr/src/uts/common/fs/zfs/dnode.c (revision 7754)
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 /*
226992Smaybee  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #include <sys/zfs_context.h>
27789Sahrens #include <sys/dbuf.h>
28789Sahrens #include <sys/dnode.h>
29789Sahrens #include <sys/dmu.h>
30789Sahrens #include <sys/dmu_impl.h>
31789Sahrens #include <sys/dmu_tx.h>
32789Sahrens #include <sys/dmu_objset.h>
33789Sahrens #include <sys/dsl_dir.h>
34789Sahrens #include <sys/dsl_dataset.h>
35789Sahrens #include <sys/spa.h>
36789Sahrens #include <sys/zio.h>
37789Sahrens #include <sys/dmu_zfetch.h>
38789Sahrens 
39789Sahrens static int free_range_compar(const void *node1, const void *node2);
40789Sahrens 
41789Sahrens static kmem_cache_t *dnode_cache;
42789Sahrens 
43789Sahrens static dnode_phys_t dnode_phys_zero;
44789Sahrens 
45789Sahrens int zfs_default_bs = SPA_MINBLOCKSHIFT;
46789Sahrens int zfs_default_ibs = DN_MAX_INDBLKSHIFT;
47789Sahrens 
48789Sahrens /* ARGSUSED */
49789Sahrens static int
50789Sahrens dnode_cons(void *arg, void *unused, int kmflag)
51789Sahrens {
52789Sahrens 	int i;
53789Sahrens 	dnode_t *dn = arg;
54789Sahrens 	bzero(dn, sizeof (dnode_t));
55789Sahrens 
56789Sahrens 	rw_init(&dn->dn_struct_rwlock, NULL, RW_DEFAULT, NULL);
57789Sahrens 	mutex_init(&dn->dn_mtx, NULL, MUTEX_DEFAULT, NULL);
58789Sahrens 	mutex_init(&dn->dn_dbufs_mtx, NULL, MUTEX_DEFAULT, NULL);
59789Sahrens 	refcount_create(&dn->dn_holds);
60789Sahrens 	refcount_create(&dn->dn_tx_holds);
61789Sahrens 
62789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
63789Sahrens 		avl_create(&dn->dn_ranges[i], free_range_compar,
64789Sahrens 		    sizeof (free_range_t),
65789Sahrens 		    offsetof(struct free_range, fr_node));
663547Smaybee 		list_create(&dn->dn_dirty_records[i],
673547Smaybee 		    sizeof (dbuf_dirty_record_t),
683547Smaybee 		    offsetof(dbuf_dirty_record_t, dr_dirty_node));
69789Sahrens 	}
70789Sahrens 
71789Sahrens 	list_create(&dn->dn_dbufs, sizeof (dmu_buf_impl_t),
72789Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
73789Sahrens 
74789Sahrens 	return (0);
75789Sahrens }
76789Sahrens 
77789Sahrens /* ARGSUSED */
78789Sahrens static void
79789Sahrens dnode_dest(void *arg, void *unused)
80789Sahrens {
81789Sahrens 	int i;
82789Sahrens 	dnode_t *dn = arg;
83789Sahrens 
84789Sahrens 	rw_destroy(&dn->dn_struct_rwlock);
85789Sahrens 	mutex_destroy(&dn->dn_mtx);
86789Sahrens 	mutex_destroy(&dn->dn_dbufs_mtx);
87789Sahrens 	refcount_destroy(&dn->dn_holds);
88789Sahrens 	refcount_destroy(&dn->dn_tx_holds);
89789Sahrens 
90789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
91789Sahrens 		avl_destroy(&dn->dn_ranges[i]);
923547Smaybee 		list_destroy(&dn->dn_dirty_records[i]);
93789Sahrens 	}
94789Sahrens 
95789Sahrens 	list_destroy(&dn->dn_dbufs);
96789Sahrens }
97789Sahrens 
98789Sahrens void
99789Sahrens dnode_init(void)
100789Sahrens {
101789Sahrens 	dnode_cache = kmem_cache_create("dnode_t",
102789Sahrens 	    sizeof (dnode_t),
103789Sahrens 	    0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0);
104789Sahrens }
105789Sahrens 
106789Sahrens void
107789Sahrens dnode_fini(void)
108789Sahrens {
109789Sahrens 	kmem_cache_destroy(dnode_cache);
110789Sahrens }
111789Sahrens 
112789Sahrens 
113873Sek110237 #ifdef ZFS_DEBUG
114789Sahrens void
115789Sahrens dnode_verify(dnode_t *dn)
116789Sahrens {
117789Sahrens 	int drop_struct_lock = FALSE;
118789Sahrens 
119789Sahrens 	ASSERT(dn->dn_phys);
120789Sahrens 	ASSERT(dn->dn_objset);
121789Sahrens 
122789Sahrens 	ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES);
123789Sahrens 
124789Sahrens 	if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY))
125789Sahrens 		return;
126789Sahrens 
127789Sahrens 	if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
128789Sahrens 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
129789Sahrens 		drop_struct_lock = TRUE;
130789Sahrens 	}
131789Sahrens 	if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) {
132789Sahrens 		int i;
133789Sahrens 		ASSERT3U(dn->dn_indblkshift, >=, 0);
134789Sahrens 		ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT);
135789Sahrens 		if (dn->dn_datablkshift) {
136789Sahrens 			ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT);
137789Sahrens 			ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT);
138789Sahrens 			ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz);
139789Sahrens 		}
140789Sahrens 		ASSERT3U(dn->dn_nlevels, <=, 30);
141789Sahrens 		ASSERT3U(dn->dn_type, <=, DMU_OT_NUMTYPES);
142789Sahrens 		ASSERT3U(dn->dn_nblkptr, >=, 1);
143789Sahrens 		ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
144789Sahrens 		ASSERT3U(dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
145789Sahrens 		ASSERT3U(dn->dn_datablksz, ==,
146789Sahrens 		    dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
147789Sahrens 		ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0);
148789Sahrens 		ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) +
149789Sahrens 		    dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
150789Sahrens 		for (i = 0; i < TXG_SIZE; i++) {
151789Sahrens 			ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels);
152789Sahrens 		}
153789Sahrens 	}
154789Sahrens 	if (dn->dn_phys->dn_type != DMU_OT_NONE)
155789Sahrens 		ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels);
1561544Seschrock 	ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT || dn->dn_dbuf != NULL);
157789Sahrens 	if (dn->dn_dbuf != NULL) {
158789Sahrens 		ASSERT3P(dn->dn_phys, ==,
159789Sahrens 		    (dnode_phys_t *)dn->dn_dbuf->db.db_data +
160789Sahrens 		    (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT)));
161789Sahrens 	}
162789Sahrens 	if (drop_struct_lock)
163789Sahrens 		rw_exit(&dn->dn_struct_rwlock);
164873Sek110237 }
165789Sahrens #endif
166789Sahrens 
167789Sahrens void
168789Sahrens dnode_byteswap(dnode_phys_t *dnp)
169789Sahrens {
170789Sahrens 	uint64_t *buf64 = (void*)&dnp->dn_blkptr;
171789Sahrens 	int i;
172789Sahrens 
173789Sahrens 	if (dnp->dn_type == DMU_OT_NONE) {
174789Sahrens 		bzero(dnp, sizeof (dnode_phys_t));
175789Sahrens 		return;
176789Sahrens 	}
177789Sahrens 
178789Sahrens 	dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec);
179789Sahrens 	dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen);
180789Sahrens 	dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid);
1812082Seschrock 	dnp->dn_used = BSWAP_64(dnp->dn_used);
182789Sahrens 
183789Sahrens 	/*
184789Sahrens 	 * dn_nblkptr is only one byte, so it's OK to read it in either
185789Sahrens 	 * byte order.  We can't read dn_bouslen.
186789Sahrens 	 */
187789Sahrens 	ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT);
188789Sahrens 	ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR);
189789Sahrens 	for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++)
190789Sahrens 		buf64[i] = BSWAP_64(buf64[i]);
191789Sahrens 
192789Sahrens 	/*
193789Sahrens 	 * OK to check dn_bonuslen for zero, because it won't matter if
194789Sahrens 	 * we have the wrong byte order.  This is necessary because the
195789Sahrens 	 * dnode dnode is smaller than a regular dnode.
196789Sahrens 	 */
197789Sahrens 	if (dnp->dn_bonuslen != 0) {
198789Sahrens 		/*
199789Sahrens 		 * Note that the bonus length calculated here may be
200789Sahrens 		 * longer than the actual bonus buffer.  This is because
201789Sahrens 		 * we always put the bonus buffer after the last block
202789Sahrens 		 * pointer (instead of packing it against the end of the
203789Sahrens 		 * dnode buffer).
204789Sahrens 		 */
205789Sahrens 		int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t);
206789Sahrens 		size_t len = DN_MAX_BONUSLEN - off;
2073882Sahrens 		ASSERT3U(dnp->dn_bonustype, <, DMU_OT_NUMTYPES);
208789Sahrens 		dmu_ot[dnp->dn_bonustype].ot_byteswap(dnp->dn_bonus + off, len);
209789Sahrens 	}
210789Sahrens }
211789Sahrens 
212789Sahrens void
213789Sahrens dnode_buf_byteswap(void *vbuf, size_t size)
214789Sahrens {
215789Sahrens 	dnode_phys_t *buf = vbuf;
216789Sahrens 	int i;
217789Sahrens 
218789Sahrens 	ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT));
219789Sahrens 	ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0);
220789Sahrens 
221789Sahrens 	size >>= DNODE_SHIFT;
222789Sahrens 	for (i = 0; i < size; i++) {
223789Sahrens 		dnode_byteswap(buf);
224789Sahrens 		buf++;
225789Sahrens 	}
226789Sahrens }
227789Sahrens 
228789Sahrens static int
229789Sahrens free_range_compar(const void *node1, const void *node2)
230789Sahrens {
231789Sahrens 	const free_range_t *rp1 = node1;
232789Sahrens 	const free_range_t *rp2 = node2;
233789Sahrens 
234789Sahrens 	if (rp1->fr_blkid < rp2->fr_blkid)
235789Sahrens 		return (-1);
236789Sahrens 	else if (rp1->fr_blkid > rp2->fr_blkid)
237789Sahrens 		return (1);
238789Sahrens 	else return (0);
239789Sahrens }
240789Sahrens 
2414944Smaybee void
2424944Smaybee dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx)
2434944Smaybee {
2444944Smaybee 	ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
2454944Smaybee 
2464944Smaybee 	dnode_setdirty(dn, tx);
2474944Smaybee 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2484944Smaybee 	ASSERT3U(newsize, <=, DN_MAX_BONUSLEN -
2494944Smaybee 	    (dn->dn_nblkptr-1) * sizeof (blkptr_t));
2504944Smaybee 	dn->dn_bonuslen = newsize;
2514944Smaybee 	if (newsize == 0)
2524944Smaybee 		dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = DN_ZERO_BONUSLEN;
2534944Smaybee 	else
2544944Smaybee 		dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
2554944Smaybee 	rw_exit(&dn->dn_struct_rwlock);
2564944Smaybee }
2574944Smaybee 
258789Sahrens static void
259789Sahrens dnode_setdblksz(dnode_t *dn, int size)
260789Sahrens {
261789Sahrens 	ASSERT3U(P2PHASE(size, SPA_MINBLOCKSIZE), ==, 0);
262789Sahrens 	ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
263789Sahrens 	ASSERT3U(size, >=, SPA_MINBLOCKSIZE);
264789Sahrens 	ASSERT3U(size >> SPA_MINBLOCKSHIFT, <,
265789Sahrens 	    1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8));
266789Sahrens 	dn->dn_datablksz = size;
267789Sahrens 	dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT;
268789Sahrens 	dn->dn_datablkshift = ISP2(size) ? highbit(size - 1) : 0;
269789Sahrens }
270789Sahrens 
271789Sahrens static dnode_t *
272789Sahrens dnode_create(objset_impl_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db,
273789Sahrens     uint64_t object)
274789Sahrens {
275789Sahrens 	dnode_t *dn = kmem_cache_alloc(dnode_cache, KM_SLEEP);
276789Sahrens 	(void) dnode_cons(dn, NULL, 0); /* XXX */
277789Sahrens 
278789Sahrens 	dn->dn_objset = os;
279789Sahrens 	dn->dn_object = object;
280789Sahrens 	dn->dn_dbuf = db;
281789Sahrens 	dn->dn_phys = dnp;
282789Sahrens 
283789Sahrens 	if (dnp->dn_datablkszsec)
284789Sahrens 		dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
285789Sahrens 	dn->dn_indblkshift = dnp->dn_indblkshift;
286789Sahrens 	dn->dn_nlevels = dnp->dn_nlevels;
287789Sahrens 	dn->dn_type = dnp->dn_type;
288789Sahrens 	dn->dn_nblkptr = dnp->dn_nblkptr;
289789Sahrens 	dn->dn_checksum = dnp->dn_checksum;
290789Sahrens 	dn->dn_compress = dnp->dn_compress;
291789Sahrens 	dn->dn_bonustype = dnp->dn_bonustype;
292789Sahrens 	dn->dn_bonuslen = dnp->dn_bonuslen;
293789Sahrens 	dn->dn_maxblkid = dnp->dn_maxblkid;
294789Sahrens 
295789Sahrens 	dmu_zfetch_init(&dn->dn_zfetch, dn);
296789Sahrens 
297789Sahrens 	ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES);
298789Sahrens 	mutex_enter(&os->os_lock);
299789Sahrens 	list_insert_head(&os->os_dnodes, dn);
300789Sahrens 	mutex_exit(&os->os_lock);
301789Sahrens 
3024309Smaybee 	arc_space_consume(sizeof (dnode_t));
303789Sahrens 	return (dn);
304789Sahrens }
305789Sahrens 
306789Sahrens static void
307789Sahrens dnode_destroy(dnode_t *dn)
308789Sahrens {
309789Sahrens 	objset_impl_t *os = dn->dn_objset;
310789Sahrens 
3112885Sahrens #ifdef ZFS_DEBUG
3122885Sahrens 	int i;
3132885Sahrens 
3142885Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
3152885Sahrens 		ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
3163547Smaybee 		ASSERT(NULL == list_head(&dn->dn_dirty_records[i]));
3172885Sahrens 		ASSERT(0 == avl_numnodes(&dn->dn_ranges[i]));
3182885Sahrens 	}
3192885Sahrens 	ASSERT(NULL == list_head(&dn->dn_dbufs));
3202885Sahrens #endif
3212885Sahrens 
322789Sahrens 	mutex_enter(&os->os_lock);
323789Sahrens 	list_remove(&os->os_dnodes, dn);
324789Sahrens 	mutex_exit(&os->os_lock);
325789Sahrens 
326789Sahrens 	if (dn->dn_dirtyctx_firstset) {
327789Sahrens 		kmem_free(dn->dn_dirtyctx_firstset, 1);
328789Sahrens 		dn->dn_dirtyctx_firstset = NULL;
329789Sahrens 	}
330789Sahrens 	dmu_zfetch_rele(&dn->dn_zfetch);
3311544Seschrock 	if (dn->dn_bonus) {
3321544Seschrock 		mutex_enter(&dn->dn_bonus->db_mtx);
3331544Seschrock 		dbuf_evict(dn->dn_bonus);
3341544Seschrock 		dn->dn_bonus = NULL;
3351544Seschrock 	}
336789Sahrens 	kmem_cache_free(dnode_cache, dn);
3374309Smaybee 	arc_space_return(sizeof (dnode_t));
338789Sahrens }
339789Sahrens 
340789Sahrens void
341789Sahrens dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
3421599Sahrens     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
343789Sahrens {
344789Sahrens 	int i;
345789Sahrens 
346789Sahrens 	if (blocksize == 0)
347789Sahrens 		blocksize = 1 << zfs_default_bs;
3481402Sahrens 	else if (blocksize > SPA_MAXBLOCKSIZE)
3491402Sahrens 		blocksize = SPA_MAXBLOCKSIZE;
3501402Sahrens 	else
3511402Sahrens 		blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE);
352789Sahrens 
353789Sahrens 	if (ibs == 0)
354789Sahrens 		ibs = zfs_default_ibs;
355789Sahrens 
356789Sahrens 	ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT);
357789Sahrens 
358789Sahrens 	dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset,
359789Sahrens 	    dn->dn_object, tx->tx_txg, blocksize, ibs);
360789Sahrens 
361789Sahrens 	ASSERT(dn->dn_type == DMU_OT_NONE);
362789Sahrens 	ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0);
363789Sahrens 	ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE);
364789Sahrens 	ASSERT(ot != DMU_OT_NONE);
365789Sahrens 	ASSERT3U(ot, <, DMU_OT_NUMTYPES);
366789Sahrens 	ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
367789Sahrens 	    (bonustype != DMU_OT_NONE && bonuslen != 0));
368789Sahrens 	ASSERT3U(bonustype, <, DMU_OT_NUMTYPES);
369789Sahrens 	ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
370789Sahrens 	ASSERT(dn->dn_type == DMU_OT_NONE);
371789Sahrens 	ASSERT3U(dn->dn_maxblkid, ==, 0);
372789Sahrens 	ASSERT3U(dn->dn_allocated_txg, ==, 0);
373789Sahrens 	ASSERT3U(dn->dn_assigned_txg, ==, 0);
374789Sahrens 	ASSERT(refcount_is_zero(&dn->dn_tx_holds));
375789Sahrens 	ASSERT3U(refcount_count(&dn->dn_holds), <=, 1);
376789Sahrens 	ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
377789Sahrens 
378789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
379789Sahrens 		ASSERT3U(dn->dn_next_nlevels[i], ==, 0);
380789Sahrens 		ASSERT3U(dn->dn_next_indblkshift[i], ==, 0);
3814944Smaybee 		ASSERT3U(dn->dn_next_bonuslen[i], ==, 0);
3821596Sahrens 		ASSERT3U(dn->dn_next_blksz[i], ==, 0);
3831596Sahrens 		ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
3843547Smaybee 		ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL);
385789Sahrens 		ASSERT3U(avl_numnodes(&dn->dn_ranges[i]), ==, 0);
386789Sahrens 	}
387789Sahrens 
388789Sahrens 	dn->dn_type = ot;
389789Sahrens 	dnode_setdblksz(dn, blocksize);
390789Sahrens 	dn->dn_indblkshift = ibs;
391789Sahrens 	dn->dn_nlevels = 1;
392789Sahrens 	dn->dn_nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
393789Sahrens 	dn->dn_bonustype = bonustype;
394789Sahrens 	dn->dn_bonuslen = bonuslen;
395789Sahrens 	dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
396789Sahrens 	dn->dn_compress = ZIO_COMPRESS_INHERIT;
397789Sahrens 	dn->dn_dirtyctx = 0;
398789Sahrens 
399789Sahrens 	dn->dn_free_txg = 0;
400789Sahrens 	if (dn->dn_dirtyctx_firstset) {
401789Sahrens 		kmem_free(dn->dn_dirtyctx_firstset, 1);
402789Sahrens 		dn->dn_dirtyctx_firstset = NULL;
403789Sahrens 	}
404789Sahrens 
405789Sahrens 	dn->dn_allocated_txg = tx->tx_txg;
4061599Sahrens 
407789Sahrens 	dnode_setdirty(dn, tx);
4081599Sahrens 	dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs;
4094944Smaybee 	dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
4101599Sahrens 	dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz;
411789Sahrens }
412789Sahrens 
413789Sahrens void
414789Sahrens dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
415789Sahrens     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
416789Sahrens {
4174944Smaybee 	int i, old_nblkptr;
4183087Sahrens 	dmu_buf_impl_t *db = NULL;
4191596Sahrens 
420789Sahrens 	ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE);
421789Sahrens 	ASSERT3U(blocksize, <=, SPA_MAXBLOCKSIZE);
422789Sahrens 	ASSERT3U(blocksize % SPA_MINBLOCKSIZE, ==, 0);
4231544Seschrock 	ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
424789Sahrens 	ASSERT(tx->tx_txg != 0);
425789Sahrens 	ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
426789Sahrens 	    (bonustype != DMU_OT_NONE && bonuslen != 0));
427789Sahrens 	ASSERT3U(bonustype, <, DMU_OT_NUMTYPES);
428789Sahrens 	ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
4291596Sahrens 
4301596Sahrens 	for (i = 0; i < TXG_SIZE; i++)
4311596Sahrens 		ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
432789Sahrens 
4331544Seschrock 	/* clean up any unreferenced dbufs */
4344944Smaybee 	dnode_evict_dbufs(dn);
4351544Seschrock 	ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
4361544Seschrock 
437789Sahrens 	/*
438789Sahrens 	 * XXX I should really have a generation number to tell if we
439789Sahrens 	 * need to do this...
440789Sahrens 	 */
441789Sahrens 	if (blocksize != dn->dn_datablksz ||
442789Sahrens 	    dn->dn_bonustype != bonustype || dn->dn_bonuslen != bonuslen) {
443789Sahrens 		/* free all old data */
444789Sahrens 		dnode_free_range(dn, 0, -1ULL, tx);
445789Sahrens 	}
446789Sahrens 
447789Sahrens 	/* change blocksize */
448789Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
4493087Sahrens 	if (blocksize != dn->dn_datablksz &&
4503087Sahrens 	    (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) ||
4513087Sahrens 	    list_head(&dn->dn_dbufs) != NULL)) {
4523087Sahrens 		db = dbuf_hold(dn, 0, FTAG);
4533087Sahrens 		dbuf_new_size(db, blocksize, tx);
4543087Sahrens 	}
455789Sahrens 	dnode_setdblksz(dn, blocksize);
456789Sahrens 	dnode_setdirty(dn, tx);
4574944Smaybee 	dn->dn_next_bonuslen[tx->tx_txg&TXG_MASK] = bonuslen;
4581596Sahrens 	dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize;
459789Sahrens 	rw_exit(&dn->dn_struct_rwlock);
4604944Smaybee 	if (db)
4613087Sahrens 		dbuf_rele(db, FTAG);
462789Sahrens 
463789Sahrens 	/* change type */
464789Sahrens 	dn->dn_type = ot;
465789Sahrens 
466789Sahrens 	/* change bonus size and type */
467789Sahrens 	mutex_enter(&dn->dn_mtx);
4684944Smaybee 	old_nblkptr = dn->dn_nblkptr;
469789Sahrens 	dn->dn_bonustype = bonustype;
470789Sahrens 	dn->dn_bonuslen = bonuslen;
471789Sahrens 	dn->dn_nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
472789Sahrens 	dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
473789Sahrens 	dn->dn_compress = ZIO_COMPRESS_INHERIT;
474789Sahrens 	ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
475789Sahrens 
4764944Smaybee 	/* XXX - for now, we can't make nblkptr smaller */
4774944Smaybee 	ASSERT3U(dn->dn_nblkptr, >=, old_nblkptr);
4784944Smaybee 
4794944Smaybee 	/* fix up the bonus db_size if dn_nblkptr has changed */
4804944Smaybee 	if (dn->dn_bonus && dn->dn_bonuslen != old_nblkptr) {
4814944Smaybee 		dn->dn_bonus->db.db_size =
4824944Smaybee 		    DN_MAX_BONUSLEN - (dn->dn_nblkptr-1) * sizeof (blkptr_t);
4834944Smaybee 		ASSERT(dn->dn_bonuslen <= dn->dn_bonus->db.db_size);
4844944Smaybee 	}
4853087Sahrens 
486789Sahrens 	dn->dn_allocated_txg = tx->tx_txg;
487789Sahrens 	mutex_exit(&dn->dn_mtx);
488789Sahrens }
489789Sahrens 
490789Sahrens void
491789Sahrens dnode_special_close(dnode_t *dn)
492789Sahrens {
4931544Seschrock 	/*
4941544Seschrock 	 * Wait for final references to the dnode to clear.  This can
4951544Seschrock 	 * only happen if the arc is asyncronously evicting state that
4961544Seschrock 	 * has a hold on this dnode while we are trying to evict this
4971544Seschrock 	 * dnode.
4981544Seschrock 	 */
4991544Seschrock 	while (refcount_count(&dn->dn_holds) > 0)
5001544Seschrock 		delay(1);
501789Sahrens 	dnode_destroy(dn);
502789Sahrens }
503789Sahrens 
504789Sahrens dnode_t *
505789Sahrens dnode_special_open(objset_impl_t *os, dnode_phys_t *dnp, uint64_t object)
506789Sahrens {
507789Sahrens 	dnode_t *dn = dnode_create(os, dnp, NULL, object);
508873Sek110237 	DNODE_VERIFY(dn);
509789Sahrens 	return (dn);
510789Sahrens }
511789Sahrens 
512789Sahrens static void
513789Sahrens dnode_buf_pageout(dmu_buf_t *db, void *arg)
514789Sahrens {
515789Sahrens 	dnode_t **children_dnodes = arg;
516789Sahrens 	int i;
517789Sahrens 	int epb = db->db_size >> DNODE_SHIFT;
518789Sahrens 
519789Sahrens 	for (i = 0; i < epb; i++) {
520789Sahrens 		dnode_t *dn = children_dnodes[i];
521789Sahrens 		int n;
522789Sahrens 
523789Sahrens 		if (dn == NULL)
524789Sahrens 			continue;
525789Sahrens #ifdef ZFS_DEBUG
526789Sahrens 		/*
527789Sahrens 		 * If there are holds on this dnode, then there should
528789Sahrens 		 * be holds on the dnode's containing dbuf as well; thus
529789Sahrens 		 * it wouldn't be eligable for eviction and this function
530789Sahrens 		 * would not have been called.
531789Sahrens 		 */
532789Sahrens 		ASSERT(refcount_is_zero(&dn->dn_holds));
533789Sahrens 		ASSERT(list_head(&dn->dn_dbufs) == NULL);
534789Sahrens 		ASSERT(refcount_is_zero(&dn->dn_tx_holds));
535789Sahrens 
536789Sahrens 		for (n = 0; n < TXG_SIZE; n++)
5371596Sahrens 			ASSERT(!list_link_active(&dn->dn_dirty_link[n]));
538789Sahrens #endif
539789Sahrens 		children_dnodes[i] = NULL;
540789Sahrens 		dnode_destroy(dn);
541789Sahrens 	}
542789Sahrens 	kmem_free(children_dnodes, epb * sizeof (dnode_t *));
543789Sahrens }
544789Sahrens 
545789Sahrens /*
5461544Seschrock  * errors:
5471544Seschrock  * EINVAL - invalid object number.
5481544Seschrock  * EIO - i/o error.
5491544Seschrock  * succeeds even for free dnodes.
550789Sahrens  */
5511544Seschrock int
5521544Seschrock dnode_hold_impl(objset_impl_t *os, uint64_t object, int flag,
5531544Seschrock     void *tag, dnode_t **dnp)
554789Sahrens {
5551544Seschrock 	int epb, idx, err;
556789Sahrens 	int drop_struct_lock = FALSE;
5571544Seschrock 	int type;
558789Sahrens 	uint64_t blk;
559789Sahrens 	dnode_t *mdn, *dn;
560789Sahrens 	dmu_buf_impl_t *db;
561789Sahrens 	dnode_t **children_dnodes;
562789Sahrens 
563*7754SJeff.Bonwick@Sun.COM 	/*
564*7754SJeff.Bonwick@Sun.COM 	 * If you are holding the spa config lock as writer, you shouldn't
565*7754SJeff.Bonwick@Sun.COM 	 * be asking the DMU to do *anything*.
566*7754SJeff.Bonwick@Sun.COM 	 */
567*7754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(os->os_spa, SCL_ALL, RW_WRITER) == 0);
568*7754SJeff.Bonwick@Sun.COM 
569789Sahrens 	if (object == 0 || object >= DN_MAX_OBJECT)
5701544Seschrock 		return (EINVAL);
571789Sahrens 
572789Sahrens 	mdn = os->os_meta_dnode;
573789Sahrens 
574873Sek110237 	DNODE_VERIFY(mdn);
575789Sahrens 
576789Sahrens 	if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) {
577789Sahrens 		rw_enter(&mdn->dn_struct_rwlock, RW_READER);
578789Sahrens 		drop_struct_lock = TRUE;
579789Sahrens 	}
580789Sahrens 
581789Sahrens 	blk = dbuf_whichblock(mdn, object * sizeof (dnode_phys_t));
582789Sahrens 
5831544Seschrock 	db = dbuf_hold(mdn, blk, FTAG);
584789Sahrens 	if (drop_struct_lock)
585789Sahrens 		rw_exit(&mdn->dn_struct_rwlock);
5861544Seschrock 	if (db == NULL)
5871544Seschrock 		return (EIO);
5881544Seschrock 	err = dbuf_read(db, NULL, DB_RF_CANFAIL);
5891544Seschrock 	if (err) {
5901544Seschrock 		dbuf_rele(db, FTAG);
5911544Seschrock 		return (err);
5921544Seschrock 	}
593789Sahrens 
594789Sahrens 	ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT);
595789Sahrens 	epb = db->db.db_size >> DNODE_SHIFT;
596789Sahrens 
597789Sahrens 	idx = object & (epb-1);
598789Sahrens 
599789Sahrens 	children_dnodes = dmu_buf_get_user(&db->db);
600789Sahrens 	if (children_dnodes == NULL) {
601789Sahrens 		dnode_t **winner;
602789Sahrens 		children_dnodes = kmem_zalloc(epb * sizeof (dnode_t *),
603789Sahrens 		    KM_SLEEP);
604789Sahrens 		if (winner = dmu_buf_set_user(&db->db, children_dnodes, NULL,
605789Sahrens 		    dnode_buf_pageout)) {
606789Sahrens 			kmem_free(children_dnodes, epb * sizeof (dnode_t *));
607789Sahrens 			children_dnodes = winner;
608789Sahrens 		}
609789Sahrens 	}
610789Sahrens 
611789Sahrens 	if ((dn = children_dnodes[idx]) == NULL) {
6124309Smaybee 		dnode_phys_t *dnp = (dnode_phys_t *)db->db.db_data+idx;
613789Sahrens 		dnode_t *winner;
6144309Smaybee 
6154309Smaybee 		dn = dnode_create(os, dnp, db, object);
616789Sahrens 		winner = atomic_cas_ptr(&children_dnodes[idx], NULL, dn);
617789Sahrens 		if (winner != NULL) {
618789Sahrens 			dnode_destroy(dn);
619789Sahrens 			dn = winner;
620789Sahrens 		}
621789Sahrens 	}
622789Sahrens 
623789Sahrens 	mutex_enter(&dn->dn_mtx);
6241544Seschrock 	type = dn->dn_type;
625789Sahrens 	if (dn->dn_free_txg ||
6261544Seschrock 	    ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) ||
6271544Seschrock 	    ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)) {
628789Sahrens 		mutex_exit(&dn->dn_mtx);
6291544Seschrock 		dbuf_rele(db, FTAG);
6301544Seschrock 		return (type == DMU_OT_NONE ? ENOENT : EEXIST);
631789Sahrens 	}
632789Sahrens 	mutex_exit(&dn->dn_mtx);
633789Sahrens 
6341544Seschrock 	if (refcount_add(&dn->dn_holds, tag) == 1)
635789Sahrens 		dbuf_add_ref(db, dn);
636789Sahrens 
637873Sek110237 	DNODE_VERIFY(dn);
638789Sahrens 	ASSERT3P(dn->dn_dbuf, ==, db);
639789Sahrens 	ASSERT3U(dn->dn_object, ==, object);
6401544Seschrock 	dbuf_rele(db, FTAG);
641789Sahrens 
6421544Seschrock 	*dnp = dn;
6431544Seschrock 	return (0);
644789Sahrens }
645789Sahrens 
646789Sahrens /*
647789Sahrens  * Return held dnode if the object is allocated, NULL if not.
648789Sahrens  */
6491544Seschrock int
6501544Seschrock dnode_hold(objset_impl_t *os, uint64_t object, void *tag, dnode_t **dnp)
651789Sahrens {
6521544Seschrock 	return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, tag, dnp));
653789Sahrens }
654789Sahrens 
6554944Smaybee /*
6564944Smaybee  * Can only add a reference if there is already at least one
6574944Smaybee  * reference on the dnode.  Returns FALSE if unable to add a
6584944Smaybee  * new reference.
6594944Smaybee  */
6604944Smaybee boolean_t
6611544Seschrock dnode_add_ref(dnode_t *dn, void *tag)
662789Sahrens {
6634944Smaybee 	mutex_enter(&dn->dn_mtx);
6644944Smaybee 	if (refcount_is_zero(&dn->dn_holds)) {
6654944Smaybee 		mutex_exit(&dn->dn_mtx);
6664944Smaybee 		return (FALSE);
6674944Smaybee 	}
6684944Smaybee 	VERIFY(1 < refcount_add(&dn->dn_holds, tag));
6694944Smaybee 	mutex_exit(&dn->dn_mtx);
6704944Smaybee 	return (TRUE);
671789Sahrens }
672789Sahrens 
673789Sahrens void
6741544Seschrock dnode_rele(dnode_t *dn, void *tag)
675789Sahrens {
676789Sahrens 	uint64_t refs;
677789Sahrens 
6784944Smaybee 	mutex_enter(&dn->dn_mtx);
6791544Seschrock 	refs = refcount_remove(&dn->dn_holds, tag);
6804944Smaybee 	mutex_exit(&dn->dn_mtx);
681789Sahrens 	/* NOTE: the DNODE_DNODE does not have a dn_dbuf */
682789Sahrens 	if (refs == 0 && dn->dn_dbuf)
6831544Seschrock 		dbuf_rele(dn->dn_dbuf, dn);
684789Sahrens }
685789Sahrens 
686789Sahrens void
687789Sahrens dnode_setdirty(dnode_t *dn, dmu_tx_t *tx)
688789Sahrens {
689789Sahrens 	objset_impl_t *os = dn->dn_objset;
690789Sahrens 	uint64_t txg = tx->tx_txg;
691789Sahrens 
6921544Seschrock 	if (dn->dn_object == DMU_META_DNODE_OBJECT)
693789Sahrens 		return;
694789Sahrens 
695873Sek110237 	DNODE_VERIFY(dn);
696789Sahrens 
697789Sahrens #ifdef ZFS_DEBUG
698789Sahrens 	mutex_enter(&dn->dn_mtx);
699789Sahrens 	ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg);
700789Sahrens 	/* ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg); */
701789Sahrens 	mutex_exit(&dn->dn_mtx);
702789Sahrens #endif
703789Sahrens 
704789Sahrens 	mutex_enter(&os->os_lock);
705789Sahrens 
706789Sahrens 	/*
707789Sahrens 	 * If we are already marked dirty, we're done.
708789Sahrens 	 */
7091596Sahrens 	if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) {
710789Sahrens 		mutex_exit(&os->os_lock);
711789Sahrens 		return;
712789Sahrens 	}
713789Sahrens 
714789Sahrens 	ASSERT(!refcount_is_zero(&dn->dn_holds) || list_head(&dn->dn_dbufs));
715789Sahrens 	ASSERT(dn->dn_datablksz != 0);
7164944Smaybee 	ASSERT3U(dn->dn_next_bonuslen[txg&TXG_MASK], ==, 0);
7171599Sahrens 	ASSERT3U(dn->dn_next_blksz[txg&TXG_MASK], ==, 0);
718789Sahrens 
719789Sahrens 	dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
720789Sahrens 	    dn->dn_object, txg);
721789Sahrens 
722789Sahrens 	if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) {
723789Sahrens 		list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn);
724789Sahrens 	} else {
725789Sahrens 		list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn);
726789Sahrens 	}
727789Sahrens 
728789Sahrens 	mutex_exit(&os->os_lock);
729789Sahrens 
730789Sahrens 	/*
731789Sahrens 	 * The dnode maintains a hold on its containing dbuf as
732789Sahrens 	 * long as there are holds on it.  Each instantiated child
733789Sahrens 	 * dbuf maintaines a hold on the dnode.  When the last child
734789Sahrens 	 * drops its hold, the dnode will drop its hold on the
735789Sahrens 	 * containing dbuf. We add a "dirty hold" here so that the
736789Sahrens 	 * dnode will hang around after we finish processing its
737789Sahrens 	 * children.
738789Sahrens 	 */
7394944Smaybee 	VERIFY(dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg));
740789Sahrens 
7413547Smaybee 	(void) dbuf_dirty(dn->dn_dbuf, tx);
742789Sahrens 
743789Sahrens 	dsl_dataset_dirty(os->os_dsl_dataset, tx);
744789Sahrens }
745789Sahrens 
746789Sahrens void
747789Sahrens dnode_free(dnode_t *dn, dmu_tx_t *tx)
748789Sahrens {
7491596Sahrens 	int txgoff = tx->tx_txg & TXG_MASK;
7501596Sahrens 
751789Sahrens 	dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg);
752789Sahrens 
753789Sahrens 	/* we should be the only holder... hopefully */
754789Sahrens 	/* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */
755789Sahrens 
756789Sahrens 	mutex_enter(&dn->dn_mtx);
757789Sahrens 	if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) {
758789Sahrens 		mutex_exit(&dn->dn_mtx);
759789Sahrens 		return;
760789Sahrens 	}
761789Sahrens 	dn->dn_free_txg = tx->tx_txg;
762789Sahrens 	mutex_exit(&dn->dn_mtx);
763789Sahrens 
764789Sahrens 	/*
765789Sahrens 	 * If the dnode is already dirty, it needs to be moved from
766789Sahrens 	 * the dirty list to the free list.
767789Sahrens 	 */
768789Sahrens 	mutex_enter(&dn->dn_objset->os_lock);
7691596Sahrens 	if (list_link_active(&dn->dn_dirty_link[txgoff])) {
7701596Sahrens 		list_remove(&dn->dn_objset->os_dirty_dnodes[txgoff], dn);
7711596Sahrens 		list_insert_tail(&dn->dn_objset->os_free_dnodes[txgoff], dn);
772789Sahrens 		mutex_exit(&dn->dn_objset->os_lock);
773789Sahrens 	} else {
774789Sahrens 		mutex_exit(&dn->dn_objset->os_lock);
775789Sahrens 		dnode_setdirty(dn, tx);
776789Sahrens 	}
777789Sahrens }
778789Sahrens 
779789Sahrens /*
780789Sahrens  * Try to change the block size for the indicated dnode.  This can only
781789Sahrens  * succeed if there are no blocks allocated or dirty beyond first block
782789Sahrens  */
783789Sahrens int
784789Sahrens dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx)
785789Sahrens {
786789Sahrens 	dmu_buf_impl_t *db, *db_next;
7876992Smaybee 	int err;
788789Sahrens 
789789Sahrens 	if (size == 0)
790789Sahrens 		size = SPA_MINBLOCKSIZE;
791789Sahrens 	if (size > SPA_MAXBLOCKSIZE)
792789Sahrens 		size = SPA_MAXBLOCKSIZE;
793789Sahrens 	else
794789Sahrens 		size = P2ROUNDUP(size, SPA_MINBLOCKSIZE);
795789Sahrens 
7962445Sahrens 	if (ibs == dn->dn_indblkshift)
7972445Sahrens 		ibs = 0;
798789Sahrens 
7992445Sahrens 	if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0)
800789Sahrens 		return (0);
801789Sahrens 
802789Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
803789Sahrens 
804789Sahrens 	/* Check for any allocated blocks beyond the first */
805789Sahrens 	if (dn->dn_phys->dn_maxblkid != 0)
8062445Sahrens 		goto fail;
807789Sahrens 
808789Sahrens 	mutex_enter(&dn->dn_dbufs_mtx);
809789Sahrens 	for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
810789Sahrens 		db_next = list_next(&dn->dn_dbufs, db);
811789Sahrens 
8126992Smaybee 		if (db->db_blkid != 0 && db->db_blkid != DB_BONUS_BLKID) {
813789Sahrens 			mutex_exit(&dn->dn_dbufs_mtx);
8142445Sahrens 			goto fail;
815789Sahrens 		}
816789Sahrens 	}
817789Sahrens 	mutex_exit(&dn->dn_dbufs_mtx);
818789Sahrens 
8192445Sahrens 	if (ibs && dn->dn_nlevels != 1)
8202445Sahrens 		goto fail;
8212445Sahrens 
8226992Smaybee 	/* resize the old block */
8236992Smaybee 	err = dbuf_hold_impl(dn, 0, 0, TRUE, FTAG, &db);
8246992Smaybee 	if (err == 0)
8251596Sahrens 		dbuf_new_size(db, size, tx);
8266992Smaybee 	else if (err != ENOENT)
8276992Smaybee 		goto fail;
828789Sahrens 
829789Sahrens 	dnode_setdblksz(dn, size);
8301596Sahrens 	dnode_setdirty(dn, tx);
8311596Sahrens 	dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size;
8322445Sahrens 	if (ibs) {
8332445Sahrens 		dn->dn_indblkshift = ibs;
8342445Sahrens 		dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs;
8352445Sahrens 	}
8366992Smaybee 	/* rele after we have fixed the blocksize in the dnode */
8371596Sahrens 	if (db)
8381596Sahrens 		dbuf_rele(db, FTAG);
839789Sahrens 
840789Sahrens 	rw_exit(&dn->dn_struct_rwlock);
8412445Sahrens 	return (0);
8422445Sahrens 
8432445Sahrens fail:
8442445Sahrens 	rw_exit(&dn->dn_struct_rwlock);
8452445Sahrens 	return (ENOTSUP);
846789Sahrens }
847789Sahrens 
8487332SJonathan.Adams@Sun.COM /* read-holding callers must not rely on the lock being continuously held */
849789Sahrens void
8507332SJonathan.Adams@Sun.COM dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t have_read)
851789Sahrens {
852789Sahrens 	uint64_t txgoff = tx->tx_txg & TXG_MASK;
8531596Sahrens 	int epbs, new_nlevels;
854789Sahrens 	uint64_t sz;
855789Sahrens 
8561596Sahrens 	ASSERT(blkid != DB_BONUS_BLKID);
857789Sahrens 
8587332SJonathan.Adams@Sun.COM 	ASSERT(have_read ?
8597332SJonathan.Adams@Sun.COM 	    RW_READ_HELD(&dn->dn_struct_rwlock) :
8607332SJonathan.Adams@Sun.COM 	    RW_WRITE_HELD(&dn->dn_struct_rwlock));
8617332SJonathan.Adams@Sun.COM 
8627332SJonathan.Adams@Sun.COM 	/*
8637332SJonathan.Adams@Sun.COM 	 * if we have a read-lock, check to see if we need to do any work
8647332SJonathan.Adams@Sun.COM 	 * before upgrading to a write-lock.
8657332SJonathan.Adams@Sun.COM 	 */
8667332SJonathan.Adams@Sun.COM 	if (have_read) {
8677332SJonathan.Adams@Sun.COM 		if (blkid <= dn->dn_maxblkid)
8687332SJonathan.Adams@Sun.COM 			return;
8697332SJonathan.Adams@Sun.COM 
8707332SJonathan.Adams@Sun.COM 		if (!rw_tryupgrade(&dn->dn_struct_rwlock)) {
8717332SJonathan.Adams@Sun.COM 			rw_exit(&dn->dn_struct_rwlock);
8727332SJonathan.Adams@Sun.COM 			rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
8737332SJonathan.Adams@Sun.COM 		}
874789Sahrens 	}
875789Sahrens 
8761596Sahrens 	if (blkid <= dn->dn_maxblkid)
8771596Sahrens 		goto out;
8781596Sahrens 
8791596Sahrens 	dn->dn_maxblkid = blkid;
880789Sahrens 
881789Sahrens 	/*
8821596Sahrens 	 * Compute the number of levels necessary to support the new maxblkid.
883789Sahrens 	 */
884789Sahrens 	new_nlevels = 1;
885789Sahrens 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
8861596Sahrens 	for (sz = dn->dn_nblkptr;
8871596Sahrens 	    sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs)
888789Sahrens 		new_nlevels++;
889789Sahrens 
8901596Sahrens 	if (new_nlevels > dn->dn_nlevels) {
8911596Sahrens 		int old_nlevels = dn->dn_nlevels;
8921596Sahrens 		dmu_buf_impl_t *db;
8933547Smaybee 		list_t *list;
8943547Smaybee 		dbuf_dirty_record_t *new, *dr, *dr_next;
8951596Sahrens 
8961596Sahrens 		dn->dn_nlevels = new_nlevels;
8971596Sahrens 
8981596Sahrens 		ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]);
899789Sahrens 		dn->dn_next_nlevels[txgoff] = new_nlevels;
900789Sahrens 
9013547Smaybee 		/* dirty the left indirects */
9021596Sahrens 		db = dbuf_hold_level(dn, old_nlevels, 0, FTAG);
9033547Smaybee 		new = dbuf_dirty(db, tx);
9041544Seschrock 		dbuf_rele(db, FTAG);
9051596Sahrens 
9063547Smaybee 		/* transfer the dirty records to the new indirect */
9073547Smaybee 		mutex_enter(&dn->dn_mtx);
9083547Smaybee 		mutex_enter(&new->dt.di.dr_mtx);
9093547Smaybee 		list = &dn->dn_dirty_records[txgoff];
9103547Smaybee 		for (dr = list_head(list); dr; dr = dr_next) {
9113547Smaybee 			dr_next = list_next(&dn->dn_dirty_records[txgoff], dr);
9123547Smaybee 			if (dr->dr_dbuf->db_level != new_nlevels-1 &&
9133547Smaybee 			    dr->dr_dbuf->db_blkid != DB_BONUS_BLKID) {
9143547Smaybee 				ASSERT(dr->dr_dbuf->db_level == old_nlevels-1);
9153547Smaybee 				list_remove(&dn->dn_dirty_records[txgoff], dr);
9163547Smaybee 				list_insert_tail(&new->dt.di.dr_children, dr);
9173547Smaybee 				dr->dr_parent = new;
9183547Smaybee 			}
9193547Smaybee 		}
9203547Smaybee 		mutex_exit(&new->dt.di.dr_mtx);
9213547Smaybee 		mutex_exit(&dn->dn_mtx);
922789Sahrens 	}
923789Sahrens 
924789Sahrens out:
9257332SJonathan.Adams@Sun.COM 	if (have_read)
9267332SJonathan.Adams@Sun.COM 		rw_downgrade(&dn->dn_struct_rwlock);
927789Sahrens }
928789Sahrens 
929789Sahrens void
930789Sahrens dnode_clear_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
931789Sahrens {
932789Sahrens 	avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
933789Sahrens 	avl_index_t where;
934789Sahrens 	free_range_t *rp;
935789Sahrens 	free_range_t rp_tofind;
936789Sahrens 	uint64_t endblk = blkid + nblks;
937789Sahrens 
938789Sahrens 	ASSERT(MUTEX_HELD(&dn->dn_mtx));
939789Sahrens 	ASSERT(nblks <= UINT64_MAX - blkid); /* no overflow */
940789Sahrens 
941789Sahrens 	dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
942789Sahrens 	    blkid, nblks, tx->tx_txg);
943789Sahrens 	rp_tofind.fr_blkid = blkid;
944789Sahrens 	rp = avl_find(tree, &rp_tofind, &where);
945789Sahrens 	if (rp == NULL)
946789Sahrens 		rp = avl_nearest(tree, where, AVL_BEFORE);
947789Sahrens 	if (rp == NULL)
948789Sahrens 		rp = avl_nearest(tree, where, AVL_AFTER);
949789Sahrens 
950789Sahrens 	while (rp && (rp->fr_blkid <= blkid + nblks)) {
951789Sahrens 		uint64_t fr_endblk = rp->fr_blkid + rp->fr_nblks;
952789Sahrens 		free_range_t *nrp = AVL_NEXT(tree, rp);
953789Sahrens 
954789Sahrens 		if (blkid <= rp->fr_blkid && endblk >= fr_endblk) {
955789Sahrens 			/* clear this entire range */
956789Sahrens 			avl_remove(tree, rp);
957789Sahrens 			kmem_free(rp, sizeof (free_range_t));
958789Sahrens 		} else if (blkid <= rp->fr_blkid &&
959789Sahrens 		    endblk > rp->fr_blkid && endblk < fr_endblk) {
960789Sahrens 			/* clear the beginning of this range */
961789Sahrens 			rp->fr_blkid = endblk;
962789Sahrens 			rp->fr_nblks = fr_endblk - endblk;
963789Sahrens 		} else if (blkid > rp->fr_blkid && blkid < fr_endblk &&
964789Sahrens 		    endblk >= fr_endblk) {
965789Sahrens 			/* clear the end of this range */
966789Sahrens 			rp->fr_nblks = blkid - rp->fr_blkid;
967789Sahrens 		} else if (blkid > rp->fr_blkid && endblk < fr_endblk) {
968789Sahrens 			/* clear a chunk out of this range */
969789Sahrens 			free_range_t *new_rp =
970789Sahrens 			    kmem_alloc(sizeof (free_range_t), KM_SLEEP);
971789Sahrens 
972789Sahrens 			new_rp->fr_blkid = endblk;
973789Sahrens 			new_rp->fr_nblks = fr_endblk - endblk;
974789Sahrens 			avl_insert_here(tree, new_rp, rp, AVL_AFTER);
975789Sahrens 			rp->fr_nblks = blkid - rp->fr_blkid;
976789Sahrens 		}
977789Sahrens 		/* there may be no overlap */
978789Sahrens 		rp = nrp;
979789Sahrens 	}
980789Sahrens }
981789Sahrens 
982789Sahrens void
983789Sahrens dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx)
984789Sahrens {
985789Sahrens 	dmu_buf_impl_t *db;
9862445Sahrens 	uint64_t blkoff, blkid, nblks;
9876992Smaybee 	int blksz, blkshift, head, tail;
988789Sahrens 	int trunc = FALSE;
9896992Smaybee 	int epbs;
990789Sahrens 
991789Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
992789Sahrens 	blksz = dn->dn_datablksz;
9936992Smaybee 	blkshift = dn->dn_datablkshift;
9946992Smaybee 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
995789Sahrens 
996789Sahrens 	if (len == -1ULL) {
997789Sahrens 		len = UINT64_MAX - off;
998789Sahrens 		trunc = TRUE;
999789Sahrens 	}
1000789Sahrens 
1001789Sahrens 	/*
1002789Sahrens 	 * First, block align the region to free:
1003789Sahrens 	 */
10042445Sahrens 	if (ISP2(blksz)) {
10052445Sahrens 		head = P2NPHASE(off, blksz);
10062445Sahrens 		blkoff = P2PHASE(off, blksz);
10076992Smaybee 		if ((off >> blkshift) > dn->dn_maxblkid)
10086992Smaybee 			goto out;
10092445Sahrens 	} else {
10102445Sahrens 		ASSERT(dn->dn_maxblkid == 0);
10112445Sahrens 		if (off == 0 && len >= blksz) {
10126992Smaybee 			/* Freeing the whole block; fast-track this request */
10136992Smaybee 			blkid = 0;
10146992Smaybee 			nblks = 1;
10156992Smaybee 			goto done;
10167385SMark.Maybee@Sun.COM 		} else if (off >= blksz) {
10176992Smaybee 			/* Freeing past end-of-data */
10186992Smaybee 			goto out;
1019789Sahrens 		} else {
10202445Sahrens 			/* Freeing part of the block. */
1021789Sahrens 			head = blksz - off;
1022789Sahrens 			ASSERT3U(head, >, 0);
1023789Sahrens 		}
10242445Sahrens 		blkoff = off;
1025789Sahrens 	}
1026789Sahrens 	/* zero out any partial block data at the start of the range */
1027789Sahrens 	if (head) {
10282445Sahrens 		ASSERT3U(blkoff + head, ==, blksz);
1029789Sahrens 		if (len < head)
1030789Sahrens 			head = len;
1031789Sahrens 		if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off), TRUE,
1032789Sahrens 		    FTAG, &db) == 0) {
1033789Sahrens 			caddr_t data;
1034789Sahrens 
1035789Sahrens 			/* don't dirty if it isn't on disk and isn't dirty */
10363547Smaybee 			if (db->db_last_dirty ||
1037789Sahrens 			    (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1038789Sahrens 				rw_exit(&dn->dn_struct_rwlock);
1039789Sahrens 				dbuf_will_dirty(db, tx);
1040789Sahrens 				rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1041789Sahrens 				data = db->db.db_data;
10422445Sahrens 				bzero(data + blkoff, head);
1043789Sahrens 			}
10441544Seschrock 			dbuf_rele(db, FTAG);
1045789Sahrens 		}
1046789Sahrens 		off += head;
1047789Sahrens 		len -= head;
1048789Sahrens 	}
1049789Sahrens 
10502445Sahrens 	/* If the range was less than one block, we're done */
10516992Smaybee 	if (len == 0)
10526992Smaybee 		goto out;
10536992Smaybee 
10546992Smaybee 	/* If the remaining range is past end of file, we're done */
10556992Smaybee 	if ((off >> blkshift) > dn->dn_maxblkid)
10566992Smaybee 		goto out;
10576992Smaybee 
10587385SMark.Maybee@Sun.COM 	ASSERT(ISP2(blksz));
10596992Smaybee 	if (trunc)
10606992Smaybee 		tail = 0;
10616992Smaybee 	else
10626992Smaybee 		tail = P2PHASE(len, blksz);
10636992Smaybee 
10646992Smaybee 	ASSERT3U(P2PHASE(off, blksz), ==, 0);
10656992Smaybee 	/* zero out any partial block data at the end of the range */
10666992Smaybee 	if (tail) {
10676992Smaybee 		if (len < tail)
10686992Smaybee 			tail = len;
10696992Smaybee 		if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len),
10706992Smaybee 		    TRUE, FTAG, &db) == 0) {
10716992Smaybee 			/* don't dirty if not on disk and not dirty */
10726992Smaybee 			if (db->db_last_dirty ||
10736992Smaybee 			    (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
10746992Smaybee 				rw_exit(&dn->dn_struct_rwlock);
10756992Smaybee 				dbuf_will_dirty(db, tx);
10766992Smaybee 				rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
10776992Smaybee 				bzero(db->db.db_data, tail);
10786992Smaybee 			}
10796992Smaybee 			dbuf_rele(db, FTAG);
10806992Smaybee 		}
10816992Smaybee 		len -= tail;
10826992Smaybee 	}
10836992Smaybee 
10846992Smaybee 	/* If the range did not include a full block, we are done */
10856992Smaybee 	if (len == 0)
1086789Sahrens 		goto out;
1087789Sahrens 
10886992Smaybee 	ASSERT(IS_P2ALIGNED(off, blksz));
10896992Smaybee 	ASSERT(trunc || IS_P2ALIGNED(len, blksz));
10906992Smaybee 	blkid = off >> blkshift;
10916992Smaybee 	nblks = len >> blkshift;
10926992Smaybee 	if (trunc)
10936992Smaybee 		nblks += 1;
10942445Sahrens 
10956992Smaybee 	/*
10966992Smaybee 	 * Read in and mark all the level-1 indirects dirty,
10976992Smaybee 	 * so that they will stay in memory until syncing phase.
10987049Smaybee 	 * Always dirty the first and last indirect to make sure
10997049Smaybee 	 * we dirty all the partial indirects.
11006992Smaybee 	 */
11016992Smaybee 	if (dn->dn_nlevels > 1) {
11026992Smaybee 		uint64_t i, first, last;
11036992Smaybee 		int shift = epbs + dn->dn_datablkshift;
11042445Sahrens 
11056992Smaybee 		first = blkid >> epbs;
11067049Smaybee 		if (db = dbuf_hold_level(dn, 1, first, FTAG)) {
11077049Smaybee 			dbuf_will_dirty(db, tx);
11087049Smaybee 			dbuf_rele(db, FTAG);
11097049Smaybee 		}
11106992Smaybee 		if (trunc)
11116992Smaybee 			last = dn->dn_maxblkid >> epbs;
11122445Sahrens 		else
11136992Smaybee 			last = (blkid + nblks - 1) >> epbs;
11147049Smaybee 		if (last > first && (db = dbuf_hold_level(dn, 1, last, FTAG))) {
11157049Smaybee 			dbuf_will_dirty(db, tx);
11167049Smaybee 			dbuf_rele(db, FTAG);
11177049Smaybee 		}
11187049Smaybee 		for (i = first + 1; i < last; i++) {
11196992Smaybee 			uint64_t ibyte = i << shift;
11206992Smaybee 			int err;
1121789Sahrens 
11226992Smaybee 			err = dnode_next_offset(dn,
11236992Smaybee 			    DNODE_FIND_HAVELOCK, &ibyte, 1, 1, 0);
11246992Smaybee 			i = ibyte >> shift;
11257049Smaybee 			if (err == ESRCH || i >= last)
11266992Smaybee 				break;
11276992Smaybee 			ASSERT(err == 0);
11286992Smaybee 			db = dbuf_hold_level(dn, 1, i, FTAG);
11296992Smaybee 			if (db) {
11306992Smaybee 				dbuf_will_dirty(db, tx);
11312445Sahrens 				dbuf_rele(db, FTAG);
1132789Sahrens 			}
11332445Sahrens 		}
11342445Sahrens 	}
11356992Smaybee done:
11366992Smaybee 	/*
11376992Smaybee 	 * Add this range to the dnode range list.
11386992Smaybee 	 * We will finish up this free operation in the syncing phase.
11396992Smaybee 	 */
1140789Sahrens 	mutex_enter(&dn->dn_mtx);
1141789Sahrens 	dnode_clear_range(dn, blkid, nblks, tx);
1142789Sahrens 	{
1143789Sahrens 		free_range_t *rp, *found;
1144789Sahrens 		avl_index_t where;
1145789Sahrens 		avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
1146789Sahrens 
1147789Sahrens 		/* Add new range to dn_ranges */
1148789Sahrens 		rp = kmem_alloc(sizeof (free_range_t), KM_SLEEP);
1149789Sahrens 		rp->fr_blkid = blkid;
1150789Sahrens 		rp->fr_nblks = nblks;
1151789Sahrens 		found = avl_find(tree, rp, &where);
1152789Sahrens 		ASSERT(found == NULL);
1153789Sahrens 		avl_insert(tree, rp, where);
1154789Sahrens 		dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1155789Sahrens 		    blkid, nblks, tx->tx_txg);
1156789Sahrens 	}
1157789Sahrens 	mutex_exit(&dn->dn_mtx);
1158789Sahrens 
11596992Smaybee 	dbuf_free_range(dn, blkid, blkid + nblks - 1, tx);
1160789Sahrens 	dnode_setdirty(dn, tx);
1161789Sahrens out:
11626992Smaybee 	if (trunc && dn->dn_maxblkid >= (off >> blkshift))
11636992Smaybee 		dn->dn_maxblkid = (off >> blkshift ? (off >> blkshift) - 1 : 0);
11646992Smaybee 
1165789Sahrens 	rw_exit(&dn->dn_struct_rwlock);
1166789Sahrens }
1167789Sahrens 
1168789Sahrens /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
1169789Sahrens uint64_t
1170789Sahrens dnode_block_freed(dnode_t *dn, uint64_t blkid)
1171789Sahrens {
1172789Sahrens 	free_range_t range_tofind;
1173789Sahrens 	void *dp = spa_get_dsl(dn->dn_objset->os_spa);
1174789Sahrens 	int i;
1175789Sahrens 
1176789Sahrens 	if (blkid == DB_BONUS_BLKID)
1177789Sahrens 		return (FALSE);
1178789Sahrens 
1179789Sahrens 	/*
1180789Sahrens 	 * If we're in the process of opening the pool, dp will not be
1181789Sahrens 	 * set yet, but there shouldn't be anything dirty.
1182789Sahrens 	 */
1183789Sahrens 	if (dp == NULL)
1184789Sahrens 		return (FALSE);
1185789Sahrens 
1186789Sahrens 	if (dn->dn_free_txg)
1187789Sahrens 		return (TRUE);
1188789Sahrens 
1189789Sahrens 	/*
1190789Sahrens 	 * If dn_datablkshift is not set, then there's only a single
1191789Sahrens 	 * block, in which case there will never be a free range so it
1192789Sahrens 	 * won't matter.
1193789Sahrens 	 */
1194789Sahrens 	range_tofind.fr_blkid = blkid;
1195789Sahrens 	mutex_enter(&dn->dn_mtx);
1196789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
1197789Sahrens 		free_range_t *range_found;
1198789Sahrens 		avl_index_t idx;
1199789Sahrens 
1200789Sahrens 		range_found = avl_find(&dn->dn_ranges[i], &range_tofind, &idx);
1201789Sahrens 		if (range_found) {
1202789Sahrens 			ASSERT(range_found->fr_nblks > 0);
1203789Sahrens 			break;
1204789Sahrens 		}
1205789Sahrens 		range_found = avl_nearest(&dn->dn_ranges[i], idx, AVL_BEFORE);
1206789Sahrens 		if (range_found &&
1207789Sahrens 		    range_found->fr_blkid + range_found->fr_nblks > blkid)
1208789Sahrens 			break;
1209789Sahrens 	}
1210789Sahrens 	mutex_exit(&dn->dn_mtx);
1211789Sahrens 	return (i < TXG_SIZE);
1212789Sahrens }
1213789Sahrens 
1214789Sahrens /* call from syncing context when we actually write/free space for this dnode */
1215789Sahrens void
12162082Seschrock dnode_diduse_space(dnode_t *dn, int64_t delta)
1217789Sahrens {
12182082Seschrock 	uint64_t space;
12192082Seschrock 	dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n",
1220789Sahrens 	    dn, dn->dn_phys,
12212082Seschrock 	    (u_longlong_t)dn->dn_phys->dn_used,
12222082Seschrock 	    (longlong_t)delta);
1223789Sahrens 
1224789Sahrens 	mutex_enter(&dn->dn_mtx);
12252082Seschrock 	space = DN_USED_BYTES(dn->dn_phys);
12262082Seschrock 	if (delta > 0) {
12272082Seschrock 		ASSERT3U(space + delta, >=, space); /* no overflow */
1228789Sahrens 	} else {
12292082Seschrock 		ASSERT3U(space, >=, -delta); /* no underflow */
12302082Seschrock 	}
12312082Seschrock 	space += delta;
12324577Sahrens 	if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_DNODE_BYTES) {
12332082Seschrock 		ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0);
12342082Seschrock 		ASSERT3U(P2PHASE(space, 1<<DEV_BSHIFT), ==, 0);
12352082Seschrock 		dn->dn_phys->dn_used = space >> DEV_BSHIFT;
12362082Seschrock 	} else {
12372082Seschrock 		dn->dn_phys->dn_used = space;
12382082Seschrock 		dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES;
1239789Sahrens 	}
1240789Sahrens 	mutex_exit(&dn->dn_mtx);
1241789Sahrens }
1242789Sahrens 
1243789Sahrens /*
1244789Sahrens  * Call when we think we're going to write/free space in open context.
1245789Sahrens  * Be conservative (ie. OK to write less than this or free more than
1246789Sahrens  * this, but don't write more or free less).
1247789Sahrens  */
1248789Sahrens void
1249789Sahrens dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx)
1250789Sahrens {
1251789Sahrens 	objset_impl_t *os = dn->dn_objset;
1252789Sahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1253789Sahrens 
1254789Sahrens 	if (space > 0)
1255789Sahrens 		space = spa_get_asize(os->os_spa, space);
1256789Sahrens 
1257789Sahrens 	if (ds)
1258789Sahrens 		dsl_dir_willuse_space(ds->ds_dir, space, tx);
1259789Sahrens 
1260789Sahrens 	dmu_tx_willuse_space(tx, space);
1261789Sahrens }
1262789Sahrens 
1263789Sahrens static int
12646992Smaybee dnode_next_offset_level(dnode_t *dn, int flags, uint64_t *offset,
12653025Sahrens 	int lvl, uint64_t blkfill, uint64_t txg)
1266789Sahrens {
1267789Sahrens 	dmu_buf_impl_t *db = NULL;
1268789Sahrens 	void *data = NULL;
1269789Sahrens 	uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
1270789Sahrens 	uint64_t epb = 1ULL << epbs;
1271789Sahrens 	uint64_t minfill, maxfill;
12726992Smaybee 	boolean_t hole;
12736992Smaybee 	int i, inc, error, span;
1274789Sahrens 
1275789Sahrens 	dprintf("probing object %llu offset %llx level %d of %u\n",
1276789Sahrens 	    dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels);
1277789Sahrens 
12786992Smaybee 	hole = flags & DNODE_FIND_HOLE;
12796992Smaybee 	inc = (flags & DNODE_FIND_BACKWARDS) ? -1 : 1;
12807385SMark.Maybee@Sun.COM 	ASSERT(txg == 0 || !hole);
12816992Smaybee 
1282789Sahrens 	if (lvl == dn->dn_phys->dn_nlevels) {
1283789Sahrens 		error = 0;
1284789Sahrens 		epb = dn->dn_phys->dn_nblkptr;
1285789Sahrens 		data = dn->dn_phys->dn_blkptr;
1286789Sahrens 	} else {
1287789Sahrens 		uint64_t blkid = dbuf_whichblock(dn, *offset) >> (epbs * lvl);
1288789Sahrens 		error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FTAG, &db);
1289789Sahrens 		if (error) {
12907385SMark.Maybee@Sun.COM 			if (error != ENOENT)
12917385SMark.Maybee@Sun.COM 				return (error);
12927385SMark.Maybee@Sun.COM 			if (hole)
12937385SMark.Maybee@Sun.COM 				return (0);
12947385SMark.Maybee@Sun.COM 			/*
12957385SMark.Maybee@Sun.COM 			 * This can only happen when we are searching up
12967385SMark.Maybee@Sun.COM 			 * the block tree for data.  We don't really need to
12977385SMark.Maybee@Sun.COM 			 * adjust the offset, as we will just end up looking
12987385SMark.Maybee@Sun.COM 			 * at the pointer to this block in its parent, and its
12997385SMark.Maybee@Sun.COM 			 * going to be unallocated, so we will skip over it.
13007385SMark.Maybee@Sun.COM 			 */
13017385SMark.Maybee@Sun.COM 			return (ESRCH);
1302789Sahrens 		}
13031793Sahrens 		error = dbuf_read(db, NULL, DB_RF_CANFAIL | DB_RF_HAVESTRUCT);
13041793Sahrens 		if (error) {
13051793Sahrens 			dbuf_rele(db, FTAG);
13061793Sahrens 			return (error);
13071793Sahrens 		}
1308789Sahrens 		data = db->db.db_data;
1309789Sahrens 	}
1310789Sahrens 
13113025Sahrens 	if (db && txg &&
13123025Sahrens 	    (db->db_blkptr == NULL || db->db_blkptr->blk_birth <= txg)) {
13137385SMark.Maybee@Sun.COM 		/*
13147385SMark.Maybee@Sun.COM 		 * This can only happen when we are searching up the tree
13157385SMark.Maybee@Sun.COM 		 * and these conditions mean that we need to keep climbing.
13167385SMark.Maybee@Sun.COM 		 */
13173025Sahrens 		error = ESRCH;
13183025Sahrens 	} else if (lvl == 0) {
1319789Sahrens 		dnode_phys_t *dnp = data;
1320789Sahrens 		span = DNODE_SHIFT;
1321789Sahrens 		ASSERT(dn->dn_type == DMU_OT_DNODE);
1322789Sahrens 
13236992Smaybee 		for (i = (*offset >> span) & (blkfill - 1);
13246992Smaybee 		    i >= 0 && i < blkfill; i += inc) {
13253025Sahrens 			boolean_t newcontents = B_TRUE;
13263025Sahrens 			if (txg) {
13273025Sahrens 				int j;
13283025Sahrens 				newcontents = B_FALSE;
13293025Sahrens 				for (j = 0; j < dnp[i].dn_nblkptr; j++) {
13303025Sahrens 					if (dnp[i].dn_blkptr[j].blk_birth > txg)
13313025Sahrens 						newcontents = B_TRUE;
13323025Sahrens 				}
13333025Sahrens 			}
13343025Sahrens 			if (!dnp[i].dn_type == hole && newcontents)
1335789Sahrens 				break;
13366992Smaybee 			*offset += (1ULL << span) * inc;
1337789Sahrens 		}
13386992Smaybee 		if (i < 0 || i == blkfill)
1339789Sahrens 			error = ESRCH;
1340789Sahrens 	} else {
1341789Sahrens 		blkptr_t *bp = data;
1342789Sahrens 		span = (lvl - 1) * epbs + dn->dn_datablkshift;
1343789Sahrens 		minfill = 0;
1344789Sahrens 		maxfill = blkfill << ((lvl - 1) * epbs);
1345789Sahrens 
1346789Sahrens 		if (hole)
1347789Sahrens 			maxfill--;
1348789Sahrens 		else
1349789Sahrens 			minfill++;
1350789Sahrens 
1351789Sahrens 		for (i = (*offset >> span) & ((1ULL << epbs) - 1);
13526992Smaybee 		    i >= 0 && i < epb; i += inc) {
1353789Sahrens 			if (bp[i].blk_fill >= minfill &&
13543025Sahrens 			    bp[i].blk_fill <= maxfill &&
13557385SMark.Maybee@Sun.COM 			    (hole || bp[i].blk_birth > txg))
1356789Sahrens 				break;
13577385SMark.Maybee@Sun.COM 			if (inc < 0 && *offset < (1ULL << span))
13587385SMark.Maybee@Sun.COM 				*offset = 0;
13597385SMark.Maybee@Sun.COM 			else
13607385SMark.Maybee@Sun.COM 				*offset += (1ULL << span) * inc;
1361789Sahrens 		}
13626992Smaybee 		if (i < 0 || i == epb)
1363789Sahrens 			error = ESRCH;
1364789Sahrens 	}
1365789Sahrens 
1366789Sahrens 	if (db)
13671544Seschrock 		dbuf_rele(db, FTAG);
1368789Sahrens 
1369789Sahrens 	return (error);
1370789Sahrens }
1371789Sahrens 
1372789Sahrens /*
1373789Sahrens  * Find the next hole, data, or sparse region at or after *offset.
1374789Sahrens  * The value 'blkfill' tells us how many items we expect to find
1375789Sahrens  * in an L0 data block; this value is 1 for normal objects,
1376789Sahrens  * DNODES_PER_BLOCK for the meta dnode, and some fraction of
1377789Sahrens  * DNODES_PER_BLOCK when searching for sparse regions thereof.
13783025Sahrens  *
1379789Sahrens  * Examples:
1380789Sahrens  *
13816992Smaybee  * dnode_next_offset(dn, flags, offset, 1, 1, 0);
13826992Smaybee  *	Finds the next/previous hole/data in a file.
1383789Sahrens  *	Used in dmu_offset_next().
1384789Sahrens  *
13856992Smaybee  * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
1386789Sahrens  *	Finds the next free/allocated dnode an objset's meta-dnode.
13873025Sahrens  *	Only finds objects that have new contents since txg (ie.
13883025Sahrens  *	bonus buffer changes and content removal are ignored).
1389789Sahrens  *	Used in dmu_object_next().
1390789Sahrens  *
13916992Smaybee  * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
1392789Sahrens  *	Finds the next L2 meta-dnode bp that's at most 1/4 full.
1393789Sahrens  *	Used in dmu_object_alloc().
1394789Sahrens  */
1395789Sahrens int
13966992Smaybee dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset,
13973025Sahrens     int minlvl, uint64_t blkfill, uint64_t txg)
1398789Sahrens {
13996992Smaybee 	uint64_t initial_offset = *offset;
1400789Sahrens 	int lvl, maxlvl;
1401789Sahrens 	int error = 0;
1402789Sahrens 
14036992Smaybee 	if (!(flags & DNODE_FIND_HAVELOCK))
14046992Smaybee 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
1405789Sahrens 
1406789Sahrens 	if (dn->dn_phys->dn_nlevels == 0) {
14076992Smaybee 		error = ESRCH;
14086992Smaybee 		goto out;
1409789Sahrens 	}
1410789Sahrens 
1411789Sahrens 	if (dn->dn_datablkshift == 0) {
1412789Sahrens 		if (*offset < dn->dn_datablksz) {
14136992Smaybee 			if (flags & DNODE_FIND_HOLE)
1414789Sahrens 				*offset = dn->dn_datablksz;
1415789Sahrens 		} else {
1416789Sahrens 			error = ESRCH;
1417789Sahrens 		}
14186992Smaybee 		goto out;
1419789Sahrens 	}
1420789Sahrens 
1421789Sahrens 	maxlvl = dn->dn_phys->dn_nlevels;
1422789Sahrens 
1423789Sahrens 	for (lvl = minlvl; lvl <= maxlvl; lvl++) {
14243025Sahrens 		error = dnode_next_offset_level(dn,
14256992Smaybee 		    flags, offset, lvl, blkfill, txg);
14261793Sahrens 		if (error != ESRCH)
1427789Sahrens 			break;
1428789Sahrens 	}
1429789Sahrens 
14306992Smaybee 	while (error == 0 && --lvl >= minlvl) {
14313025Sahrens 		error = dnode_next_offset_level(dn,
14326992Smaybee 		    flags, offset, lvl, blkfill, txg);
14333025Sahrens 	}
1434789Sahrens 
14356992Smaybee 	if (error == 0 && (flags & DNODE_FIND_BACKWARDS ?
14366992Smaybee 	    initial_offset < *offset : initial_offset > *offset))
14371793Sahrens 		error = ESRCH;
14386992Smaybee out:
14396992Smaybee 	if (!(flags & DNODE_FIND_HAVELOCK))
14406992Smaybee 		rw_exit(&dn->dn_struct_rwlock);
1441789Sahrens 
1442789Sahrens 	return (error);
1443789Sahrens }
1444