xref: /onnv-gate/usr/src/uts/common/fs/zfs/dnode.c (revision 8582)
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 /*
22*8582SBrendan.Gregg@Sun.COM  * Copyright 2009 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);
598214SRicardo.M.Correia@Sun.COM 	cv_init(&dn->dn_notxholds, NULL, CV_DEFAULT, NULL);
608214SRicardo.M.Correia@Sun.COM 
61789Sahrens 	refcount_create(&dn->dn_holds);
62789Sahrens 	refcount_create(&dn->dn_tx_holds);
63789Sahrens 
64789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
65789Sahrens 		avl_create(&dn->dn_ranges[i], free_range_compar,
66789Sahrens 		    sizeof (free_range_t),
67789Sahrens 		    offsetof(struct free_range, fr_node));
683547Smaybee 		list_create(&dn->dn_dirty_records[i],
693547Smaybee 		    sizeof (dbuf_dirty_record_t),
703547Smaybee 		    offsetof(dbuf_dirty_record_t, dr_dirty_node));
71789Sahrens 	}
72789Sahrens 
73789Sahrens 	list_create(&dn->dn_dbufs, sizeof (dmu_buf_impl_t),
74789Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
75789Sahrens 
76789Sahrens 	return (0);
77789Sahrens }
78789Sahrens 
79789Sahrens /* ARGSUSED */
80789Sahrens static void
81789Sahrens dnode_dest(void *arg, void *unused)
82789Sahrens {
83789Sahrens 	int i;
84789Sahrens 	dnode_t *dn = arg;
85789Sahrens 
86789Sahrens 	rw_destroy(&dn->dn_struct_rwlock);
87789Sahrens 	mutex_destroy(&dn->dn_mtx);
88789Sahrens 	mutex_destroy(&dn->dn_dbufs_mtx);
898214SRicardo.M.Correia@Sun.COM 	cv_destroy(&dn->dn_notxholds);
90789Sahrens 	refcount_destroy(&dn->dn_holds);
91789Sahrens 	refcount_destroy(&dn->dn_tx_holds);
92789Sahrens 
93789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
94789Sahrens 		avl_destroy(&dn->dn_ranges[i]);
953547Smaybee 		list_destroy(&dn->dn_dirty_records[i]);
96789Sahrens 	}
97789Sahrens 
98789Sahrens 	list_destroy(&dn->dn_dbufs);
99789Sahrens }
100789Sahrens 
101789Sahrens void
102789Sahrens dnode_init(void)
103789Sahrens {
104789Sahrens 	dnode_cache = kmem_cache_create("dnode_t",
105789Sahrens 	    sizeof (dnode_t),
106789Sahrens 	    0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0);
107789Sahrens }
108789Sahrens 
109789Sahrens void
110789Sahrens dnode_fini(void)
111789Sahrens {
112789Sahrens 	kmem_cache_destroy(dnode_cache);
113789Sahrens }
114789Sahrens 
115789Sahrens 
116873Sek110237 #ifdef ZFS_DEBUG
117789Sahrens void
118789Sahrens dnode_verify(dnode_t *dn)
119789Sahrens {
120789Sahrens 	int drop_struct_lock = FALSE;
121789Sahrens 
122789Sahrens 	ASSERT(dn->dn_phys);
123789Sahrens 	ASSERT(dn->dn_objset);
124789Sahrens 
125789Sahrens 	ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES);
126789Sahrens 
127789Sahrens 	if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY))
128789Sahrens 		return;
129789Sahrens 
130789Sahrens 	if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
131789Sahrens 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
132789Sahrens 		drop_struct_lock = TRUE;
133789Sahrens 	}
134789Sahrens 	if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) {
135789Sahrens 		int i;
136789Sahrens 		ASSERT3U(dn->dn_indblkshift, >=, 0);
137789Sahrens 		ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT);
138789Sahrens 		if (dn->dn_datablkshift) {
139789Sahrens 			ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT);
140789Sahrens 			ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT);
141789Sahrens 			ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz);
142789Sahrens 		}
143789Sahrens 		ASSERT3U(dn->dn_nlevels, <=, 30);
144789Sahrens 		ASSERT3U(dn->dn_type, <=, DMU_OT_NUMTYPES);
145789Sahrens 		ASSERT3U(dn->dn_nblkptr, >=, 1);
146789Sahrens 		ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
147789Sahrens 		ASSERT3U(dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
148789Sahrens 		ASSERT3U(dn->dn_datablksz, ==,
149789Sahrens 		    dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
150789Sahrens 		ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0);
151789Sahrens 		ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) +
152789Sahrens 		    dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
153789Sahrens 		for (i = 0; i < TXG_SIZE; i++) {
154789Sahrens 			ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels);
155789Sahrens 		}
156789Sahrens 	}
157789Sahrens 	if (dn->dn_phys->dn_type != DMU_OT_NONE)
158789Sahrens 		ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels);
1591544Seschrock 	ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT || dn->dn_dbuf != NULL);
160789Sahrens 	if (dn->dn_dbuf != NULL) {
161789Sahrens 		ASSERT3P(dn->dn_phys, ==,
162789Sahrens 		    (dnode_phys_t *)dn->dn_dbuf->db.db_data +
163789Sahrens 		    (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT)));
164789Sahrens 	}
165789Sahrens 	if (drop_struct_lock)
166789Sahrens 		rw_exit(&dn->dn_struct_rwlock);
167873Sek110237 }
168789Sahrens #endif
169789Sahrens 
170789Sahrens void
171789Sahrens dnode_byteswap(dnode_phys_t *dnp)
172789Sahrens {
173789Sahrens 	uint64_t *buf64 = (void*)&dnp->dn_blkptr;
174789Sahrens 	int i;
175789Sahrens 
176789Sahrens 	if (dnp->dn_type == DMU_OT_NONE) {
177789Sahrens 		bzero(dnp, sizeof (dnode_phys_t));
178789Sahrens 		return;
179789Sahrens 	}
180789Sahrens 
181789Sahrens 	dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec);
182789Sahrens 	dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen);
183789Sahrens 	dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid);
1842082Seschrock 	dnp->dn_used = BSWAP_64(dnp->dn_used);
185789Sahrens 
186789Sahrens 	/*
187789Sahrens 	 * dn_nblkptr is only one byte, so it's OK to read it in either
188789Sahrens 	 * byte order.  We can't read dn_bouslen.
189789Sahrens 	 */
190789Sahrens 	ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT);
191789Sahrens 	ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR);
192789Sahrens 	for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++)
193789Sahrens 		buf64[i] = BSWAP_64(buf64[i]);
194789Sahrens 
195789Sahrens 	/*
196789Sahrens 	 * OK to check dn_bonuslen for zero, because it won't matter if
197789Sahrens 	 * we have the wrong byte order.  This is necessary because the
198789Sahrens 	 * dnode dnode is smaller than a regular dnode.
199789Sahrens 	 */
200789Sahrens 	if (dnp->dn_bonuslen != 0) {
201789Sahrens 		/*
202789Sahrens 		 * Note that the bonus length calculated here may be
203789Sahrens 		 * longer than the actual bonus buffer.  This is because
204789Sahrens 		 * we always put the bonus buffer after the last block
205789Sahrens 		 * pointer (instead of packing it against the end of the
206789Sahrens 		 * dnode buffer).
207789Sahrens 		 */
208789Sahrens 		int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t);
209789Sahrens 		size_t len = DN_MAX_BONUSLEN - off;
2103882Sahrens 		ASSERT3U(dnp->dn_bonustype, <, DMU_OT_NUMTYPES);
211789Sahrens 		dmu_ot[dnp->dn_bonustype].ot_byteswap(dnp->dn_bonus + off, len);
212789Sahrens 	}
213789Sahrens }
214789Sahrens 
215789Sahrens void
216789Sahrens dnode_buf_byteswap(void *vbuf, size_t size)
217789Sahrens {
218789Sahrens 	dnode_phys_t *buf = vbuf;
219789Sahrens 	int i;
220789Sahrens 
221789Sahrens 	ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT));
222789Sahrens 	ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0);
223789Sahrens 
224789Sahrens 	size >>= DNODE_SHIFT;
225789Sahrens 	for (i = 0; i < size; i++) {
226789Sahrens 		dnode_byteswap(buf);
227789Sahrens 		buf++;
228789Sahrens 	}
229789Sahrens }
230789Sahrens 
231789Sahrens static int
232789Sahrens free_range_compar(const void *node1, const void *node2)
233789Sahrens {
234789Sahrens 	const free_range_t *rp1 = node1;
235789Sahrens 	const free_range_t *rp2 = node2;
236789Sahrens 
237789Sahrens 	if (rp1->fr_blkid < rp2->fr_blkid)
238789Sahrens 		return (-1);
239789Sahrens 	else if (rp1->fr_blkid > rp2->fr_blkid)
240789Sahrens 		return (1);
241789Sahrens 	else return (0);
242789Sahrens }
243789Sahrens 
2444944Smaybee void
2454944Smaybee dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx)
2464944Smaybee {
2474944Smaybee 	ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
2484944Smaybee 
2494944Smaybee 	dnode_setdirty(dn, tx);
2504944Smaybee 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2514944Smaybee 	ASSERT3U(newsize, <=, DN_MAX_BONUSLEN -
2524944Smaybee 	    (dn->dn_nblkptr-1) * sizeof (blkptr_t));
2534944Smaybee 	dn->dn_bonuslen = newsize;
2544944Smaybee 	if (newsize == 0)
2554944Smaybee 		dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = DN_ZERO_BONUSLEN;
2564944Smaybee 	else
2574944Smaybee 		dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
2584944Smaybee 	rw_exit(&dn->dn_struct_rwlock);
2594944Smaybee }
2604944Smaybee 
261789Sahrens static void
262789Sahrens dnode_setdblksz(dnode_t *dn, int size)
263789Sahrens {
264789Sahrens 	ASSERT3U(P2PHASE(size, SPA_MINBLOCKSIZE), ==, 0);
265789Sahrens 	ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
266789Sahrens 	ASSERT3U(size, >=, SPA_MINBLOCKSIZE);
267789Sahrens 	ASSERT3U(size >> SPA_MINBLOCKSHIFT, <,
268789Sahrens 	    1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8));
269789Sahrens 	dn->dn_datablksz = size;
270789Sahrens 	dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT;
271789Sahrens 	dn->dn_datablkshift = ISP2(size) ? highbit(size - 1) : 0;
272789Sahrens }
273789Sahrens 
274789Sahrens static dnode_t *
275789Sahrens dnode_create(objset_impl_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db,
276789Sahrens     uint64_t object)
277789Sahrens {
278789Sahrens 	dnode_t *dn = kmem_cache_alloc(dnode_cache, KM_SLEEP);
279789Sahrens 	(void) dnode_cons(dn, NULL, 0); /* XXX */
280789Sahrens 
281789Sahrens 	dn->dn_objset = os;
282789Sahrens 	dn->dn_object = object;
283789Sahrens 	dn->dn_dbuf = db;
284789Sahrens 	dn->dn_phys = dnp;
285789Sahrens 
286789Sahrens 	if (dnp->dn_datablkszsec)
287789Sahrens 		dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
288789Sahrens 	dn->dn_indblkshift = dnp->dn_indblkshift;
289789Sahrens 	dn->dn_nlevels = dnp->dn_nlevels;
290789Sahrens 	dn->dn_type = dnp->dn_type;
291789Sahrens 	dn->dn_nblkptr = dnp->dn_nblkptr;
292789Sahrens 	dn->dn_checksum = dnp->dn_checksum;
293789Sahrens 	dn->dn_compress = dnp->dn_compress;
294789Sahrens 	dn->dn_bonustype = dnp->dn_bonustype;
295789Sahrens 	dn->dn_bonuslen = dnp->dn_bonuslen;
296789Sahrens 	dn->dn_maxblkid = dnp->dn_maxblkid;
297789Sahrens 
298789Sahrens 	dmu_zfetch_init(&dn->dn_zfetch, dn);
299789Sahrens 
300789Sahrens 	ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES);
301789Sahrens 	mutex_enter(&os->os_lock);
302789Sahrens 	list_insert_head(&os->os_dnodes, dn);
303789Sahrens 	mutex_exit(&os->os_lock);
304789Sahrens 
305*8582SBrendan.Gregg@Sun.COM 	arc_space_consume(sizeof (dnode_t), ARC_SPACE_OTHER);
306789Sahrens 	return (dn);
307789Sahrens }
308789Sahrens 
309789Sahrens static void
310789Sahrens dnode_destroy(dnode_t *dn)
311789Sahrens {
312789Sahrens 	objset_impl_t *os = dn->dn_objset;
313789Sahrens 
3142885Sahrens #ifdef ZFS_DEBUG
3152885Sahrens 	int i;
3162885Sahrens 
3172885Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
3182885Sahrens 		ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
3193547Smaybee 		ASSERT(NULL == list_head(&dn->dn_dirty_records[i]));
3202885Sahrens 		ASSERT(0 == avl_numnodes(&dn->dn_ranges[i]));
3212885Sahrens 	}
3222885Sahrens 	ASSERT(NULL == list_head(&dn->dn_dbufs));
3232885Sahrens #endif
3242885Sahrens 
325789Sahrens 	mutex_enter(&os->os_lock);
326789Sahrens 	list_remove(&os->os_dnodes, dn);
327789Sahrens 	mutex_exit(&os->os_lock);
328789Sahrens 
329789Sahrens 	if (dn->dn_dirtyctx_firstset) {
330789Sahrens 		kmem_free(dn->dn_dirtyctx_firstset, 1);
331789Sahrens 		dn->dn_dirtyctx_firstset = NULL;
332789Sahrens 	}
333789Sahrens 	dmu_zfetch_rele(&dn->dn_zfetch);
3341544Seschrock 	if (dn->dn_bonus) {
3351544Seschrock 		mutex_enter(&dn->dn_bonus->db_mtx);
3361544Seschrock 		dbuf_evict(dn->dn_bonus);
3371544Seschrock 		dn->dn_bonus = NULL;
3381544Seschrock 	}
339789Sahrens 	kmem_cache_free(dnode_cache, dn);
340*8582SBrendan.Gregg@Sun.COM 	arc_space_return(sizeof (dnode_t), ARC_SPACE_OTHER);
341789Sahrens }
342789Sahrens 
343789Sahrens void
344789Sahrens dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
3451599Sahrens     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
346789Sahrens {
347789Sahrens 	int i;
348789Sahrens 
349789Sahrens 	if (blocksize == 0)
350789Sahrens 		blocksize = 1 << zfs_default_bs;
3511402Sahrens 	else if (blocksize > SPA_MAXBLOCKSIZE)
3521402Sahrens 		blocksize = SPA_MAXBLOCKSIZE;
3531402Sahrens 	else
3541402Sahrens 		blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE);
355789Sahrens 
356789Sahrens 	if (ibs == 0)
357789Sahrens 		ibs = zfs_default_ibs;
358789Sahrens 
359789Sahrens 	ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT);
360789Sahrens 
361789Sahrens 	dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset,
362789Sahrens 	    dn->dn_object, tx->tx_txg, blocksize, ibs);
363789Sahrens 
364789Sahrens 	ASSERT(dn->dn_type == DMU_OT_NONE);
365789Sahrens 	ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0);
366789Sahrens 	ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE);
367789Sahrens 	ASSERT(ot != DMU_OT_NONE);
368789Sahrens 	ASSERT3U(ot, <, DMU_OT_NUMTYPES);
369789Sahrens 	ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
370789Sahrens 	    (bonustype != DMU_OT_NONE && bonuslen != 0));
371789Sahrens 	ASSERT3U(bonustype, <, DMU_OT_NUMTYPES);
372789Sahrens 	ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
373789Sahrens 	ASSERT(dn->dn_type == DMU_OT_NONE);
374789Sahrens 	ASSERT3U(dn->dn_maxblkid, ==, 0);
375789Sahrens 	ASSERT3U(dn->dn_allocated_txg, ==, 0);
376789Sahrens 	ASSERT3U(dn->dn_assigned_txg, ==, 0);
377789Sahrens 	ASSERT(refcount_is_zero(&dn->dn_tx_holds));
378789Sahrens 	ASSERT3U(refcount_count(&dn->dn_holds), <=, 1);
379789Sahrens 	ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
380789Sahrens 
381789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
382789Sahrens 		ASSERT3U(dn->dn_next_nlevels[i], ==, 0);
383789Sahrens 		ASSERT3U(dn->dn_next_indblkshift[i], ==, 0);
3844944Smaybee 		ASSERT3U(dn->dn_next_bonuslen[i], ==, 0);
3851596Sahrens 		ASSERT3U(dn->dn_next_blksz[i], ==, 0);
3861596Sahrens 		ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
3873547Smaybee 		ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL);
388789Sahrens 		ASSERT3U(avl_numnodes(&dn->dn_ranges[i]), ==, 0);
389789Sahrens 	}
390789Sahrens 
391789Sahrens 	dn->dn_type = ot;
392789Sahrens 	dnode_setdblksz(dn, blocksize);
393789Sahrens 	dn->dn_indblkshift = ibs;
394789Sahrens 	dn->dn_nlevels = 1;
395789Sahrens 	dn->dn_nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
396789Sahrens 	dn->dn_bonustype = bonustype;
397789Sahrens 	dn->dn_bonuslen = bonuslen;
398789Sahrens 	dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
399789Sahrens 	dn->dn_compress = ZIO_COMPRESS_INHERIT;
400789Sahrens 	dn->dn_dirtyctx = 0;
401789Sahrens 
402789Sahrens 	dn->dn_free_txg = 0;
403789Sahrens 	if (dn->dn_dirtyctx_firstset) {
404789Sahrens 		kmem_free(dn->dn_dirtyctx_firstset, 1);
405789Sahrens 		dn->dn_dirtyctx_firstset = NULL;
406789Sahrens 	}
407789Sahrens 
408789Sahrens 	dn->dn_allocated_txg = tx->tx_txg;
4091599Sahrens 
410789Sahrens 	dnode_setdirty(dn, tx);
4111599Sahrens 	dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs;
4124944Smaybee 	dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
4131599Sahrens 	dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz;
414789Sahrens }
415789Sahrens 
416789Sahrens void
417789Sahrens dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
418789Sahrens     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
419789Sahrens {
4204944Smaybee 	int i, old_nblkptr;
4213087Sahrens 	dmu_buf_impl_t *db = NULL;
4221596Sahrens 
423789Sahrens 	ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE);
424789Sahrens 	ASSERT3U(blocksize, <=, SPA_MAXBLOCKSIZE);
425789Sahrens 	ASSERT3U(blocksize % SPA_MINBLOCKSIZE, ==, 0);
4261544Seschrock 	ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
427789Sahrens 	ASSERT(tx->tx_txg != 0);
428789Sahrens 	ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
429789Sahrens 	    (bonustype != DMU_OT_NONE && bonuslen != 0));
430789Sahrens 	ASSERT3U(bonustype, <, DMU_OT_NUMTYPES);
431789Sahrens 	ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
4321596Sahrens 
4331596Sahrens 	for (i = 0; i < TXG_SIZE; i++)
4341596Sahrens 		ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
435789Sahrens 
4361544Seschrock 	/* clean up any unreferenced dbufs */
4374944Smaybee 	dnode_evict_dbufs(dn);
4381544Seschrock 	ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
4391544Seschrock 
440789Sahrens 	/*
441789Sahrens 	 * XXX I should really have a generation number to tell if we
442789Sahrens 	 * need to do this...
443789Sahrens 	 */
444789Sahrens 	if (blocksize != dn->dn_datablksz ||
445789Sahrens 	    dn->dn_bonustype != bonustype || dn->dn_bonuslen != bonuslen) {
446789Sahrens 		/* free all old data */
447789Sahrens 		dnode_free_range(dn, 0, -1ULL, tx);
448789Sahrens 	}
449789Sahrens 
450789Sahrens 	/* change blocksize */
451789Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
4523087Sahrens 	if (blocksize != dn->dn_datablksz &&
4533087Sahrens 	    (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) ||
4543087Sahrens 	    list_head(&dn->dn_dbufs) != NULL)) {
4553087Sahrens 		db = dbuf_hold(dn, 0, FTAG);
4563087Sahrens 		dbuf_new_size(db, blocksize, tx);
4573087Sahrens 	}
458789Sahrens 	dnode_setdblksz(dn, blocksize);
459789Sahrens 	dnode_setdirty(dn, tx);
4604944Smaybee 	dn->dn_next_bonuslen[tx->tx_txg&TXG_MASK] = bonuslen;
4611596Sahrens 	dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize;
462789Sahrens 	rw_exit(&dn->dn_struct_rwlock);
4634944Smaybee 	if (db)
4643087Sahrens 		dbuf_rele(db, FTAG);
465789Sahrens 
466789Sahrens 	/* change type */
467789Sahrens 	dn->dn_type = ot;
468789Sahrens 
469789Sahrens 	/* change bonus size and type */
470789Sahrens 	mutex_enter(&dn->dn_mtx);
4714944Smaybee 	old_nblkptr = dn->dn_nblkptr;
472789Sahrens 	dn->dn_bonustype = bonustype;
473789Sahrens 	dn->dn_bonuslen = bonuslen;
474789Sahrens 	dn->dn_nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
475789Sahrens 	dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
476789Sahrens 	dn->dn_compress = ZIO_COMPRESS_INHERIT;
477789Sahrens 	ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
478789Sahrens 
4794944Smaybee 	/* XXX - for now, we can't make nblkptr smaller */
4804944Smaybee 	ASSERT3U(dn->dn_nblkptr, >=, old_nblkptr);
4814944Smaybee 
4824944Smaybee 	/* fix up the bonus db_size if dn_nblkptr has changed */
4834944Smaybee 	if (dn->dn_bonus && dn->dn_bonuslen != old_nblkptr) {
4844944Smaybee 		dn->dn_bonus->db.db_size =
4854944Smaybee 		    DN_MAX_BONUSLEN - (dn->dn_nblkptr-1) * sizeof (blkptr_t);
4864944Smaybee 		ASSERT(dn->dn_bonuslen <= dn->dn_bonus->db.db_size);
4874944Smaybee 	}
4883087Sahrens 
489789Sahrens 	dn->dn_allocated_txg = tx->tx_txg;
490789Sahrens 	mutex_exit(&dn->dn_mtx);
491789Sahrens }
492789Sahrens 
493789Sahrens void
494789Sahrens dnode_special_close(dnode_t *dn)
495789Sahrens {
4961544Seschrock 	/*
4971544Seschrock 	 * Wait for final references to the dnode to clear.  This can
4981544Seschrock 	 * only happen if the arc is asyncronously evicting state that
4991544Seschrock 	 * has a hold on this dnode while we are trying to evict this
5001544Seschrock 	 * dnode.
5011544Seschrock 	 */
5021544Seschrock 	while (refcount_count(&dn->dn_holds) > 0)
5031544Seschrock 		delay(1);
504789Sahrens 	dnode_destroy(dn);
505789Sahrens }
506789Sahrens 
507789Sahrens dnode_t *
508789Sahrens dnode_special_open(objset_impl_t *os, dnode_phys_t *dnp, uint64_t object)
509789Sahrens {
510789Sahrens 	dnode_t *dn = dnode_create(os, dnp, NULL, object);
511873Sek110237 	DNODE_VERIFY(dn);
512789Sahrens 	return (dn);
513789Sahrens }
514789Sahrens 
515789Sahrens static void
516789Sahrens dnode_buf_pageout(dmu_buf_t *db, void *arg)
517789Sahrens {
518789Sahrens 	dnode_t **children_dnodes = arg;
519789Sahrens 	int i;
520789Sahrens 	int epb = db->db_size >> DNODE_SHIFT;
521789Sahrens 
522789Sahrens 	for (i = 0; i < epb; i++) {
523789Sahrens 		dnode_t *dn = children_dnodes[i];
524789Sahrens 		int n;
525789Sahrens 
526789Sahrens 		if (dn == NULL)
527789Sahrens 			continue;
528789Sahrens #ifdef ZFS_DEBUG
529789Sahrens 		/*
530789Sahrens 		 * If there are holds on this dnode, then there should
531789Sahrens 		 * be holds on the dnode's containing dbuf as well; thus
532789Sahrens 		 * it wouldn't be eligable for eviction and this function
533789Sahrens 		 * would not have been called.
534789Sahrens 		 */
535789Sahrens 		ASSERT(refcount_is_zero(&dn->dn_holds));
536789Sahrens 		ASSERT(list_head(&dn->dn_dbufs) == NULL);
537789Sahrens 		ASSERT(refcount_is_zero(&dn->dn_tx_holds));
538789Sahrens 
539789Sahrens 		for (n = 0; n < TXG_SIZE; n++)
5401596Sahrens 			ASSERT(!list_link_active(&dn->dn_dirty_link[n]));
541789Sahrens #endif
542789Sahrens 		children_dnodes[i] = NULL;
543789Sahrens 		dnode_destroy(dn);
544789Sahrens 	}
545789Sahrens 	kmem_free(children_dnodes, epb * sizeof (dnode_t *));
546789Sahrens }
547789Sahrens 
548789Sahrens /*
5491544Seschrock  * errors:
5501544Seschrock  * EINVAL - invalid object number.
5511544Seschrock  * EIO - i/o error.
5521544Seschrock  * succeeds even for free dnodes.
553789Sahrens  */
5541544Seschrock int
5551544Seschrock dnode_hold_impl(objset_impl_t *os, uint64_t object, int flag,
5561544Seschrock     void *tag, dnode_t **dnp)
557789Sahrens {
5581544Seschrock 	int epb, idx, err;
559789Sahrens 	int drop_struct_lock = FALSE;
5601544Seschrock 	int type;
561789Sahrens 	uint64_t blk;
562789Sahrens 	dnode_t *mdn, *dn;
563789Sahrens 	dmu_buf_impl_t *db;
564789Sahrens 	dnode_t **children_dnodes;
565789Sahrens 
5667754SJeff.Bonwick@Sun.COM 	/*
5677754SJeff.Bonwick@Sun.COM 	 * If you are holding the spa config lock as writer, you shouldn't
5687754SJeff.Bonwick@Sun.COM 	 * be asking the DMU to do *anything*.
5697754SJeff.Bonwick@Sun.COM 	 */
5707754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(os->os_spa, SCL_ALL, RW_WRITER) == 0);
5717754SJeff.Bonwick@Sun.COM 
572789Sahrens 	if (object == 0 || object >= DN_MAX_OBJECT)
5731544Seschrock 		return (EINVAL);
574789Sahrens 
575789Sahrens 	mdn = os->os_meta_dnode;
576789Sahrens 
577873Sek110237 	DNODE_VERIFY(mdn);
578789Sahrens 
579789Sahrens 	if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) {
580789Sahrens 		rw_enter(&mdn->dn_struct_rwlock, RW_READER);
581789Sahrens 		drop_struct_lock = TRUE;
582789Sahrens 	}
583789Sahrens 
584789Sahrens 	blk = dbuf_whichblock(mdn, object * sizeof (dnode_phys_t));
585789Sahrens 
5861544Seschrock 	db = dbuf_hold(mdn, blk, FTAG);
587789Sahrens 	if (drop_struct_lock)
588789Sahrens 		rw_exit(&mdn->dn_struct_rwlock);
5891544Seschrock 	if (db == NULL)
5901544Seschrock 		return (EIO);
5911544Seschrock 	err = dbuf_read(db, NULL, DB_RF_CANFAIL);
5921544Seschrock 	if (err) {
5931544Seschrock 		dbuf_rele(db, FTAG);
5941544Seschrock 		return (err);
5951544Seschrock 	}
596789Sahrens 
597789Sahrens 	ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT);
598789Sahrens 	epb = db->db.db_size >> DNODE_SHIFT;
599789Sahrens 
600789Sahrens 	idx = object & (epb-1);
601789Sahrens 
602789Sahrens 	children_dnodes = dmu_buf_get_user(&db->db);
603789Sahrens 	if (children_dnodes == NULL) {
604789Sahrens 		dnode_t **winner;
605789Sahrens 		children_dnodes = kmem_zalloc(epb * sizeof (dnode_t *),
606789Sahrens 		    KM_SLEEP);
607789Sahrens 		if (winner = dmu_buf_set_user(&db->db, children_dnodes, NULL,
608789Sahrens 		    dnode_buf_pageout)) {
609789Sahrens 			kmem_free(children_dnodes, epb * sizeof (dnode_t *));
610789Sahrens 			children_dnodes = winner;
611789Sahrens 		}
612789Sahrens 	}
613789Sahrens 
614789Sahrens 	if ((dn = children_dnodes[idx]) == NULL) {
6154309Smaybee 		dnode_phys_t *dnp = (dnode_phys_t *)db->db.db_data+idx;
616789Sahrens 		dnode_t *winner;
6174309Smaybee 
6184309Smaybee 		dn = dnode_create(os, dnp, db, object);
619789Sahrens 		winner = atomic_cas_ptr(&children_dnodes[idx], NULL, dn);
620789Sahrens 		if (winner != NULL) {
621789Sahrens 			dnode_destroy(dn);
622789Sahrens 			dn = winner;
623789Sahrens 		}
624789Sahrens 	}
625789Sahrens 
626789Sahrens 	mutex_enter(&dn->dn_mtx);
6271544Seschrock 	type = dn->dn_type;
628789Sahrens 	if (dn->dn_free_txg ||
6291544Seschrock 	    ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) ||
6301544Seschrock 	    ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)) {
631789Sahrens 		mutex_exit(&dn->dn_mtx);
6321544Seschrock 		dbuf_rele(db, FTAG);
6331544Seschrock 		return (type == DMU_OT_NONE ? ENOENT : EEXIST);
634789Sahrens 	}
635789Sahrens 	mutex_exit(&dn->dn_mtx);
636789Sahrens 
6371544Seschrock 	if (refcount_add(&dn->dn_holds, tag) == 1)
638789Sahrens 		dbuf_add_ref(db, dn);
639789Sahrens 
640873Sek110237 	DNODE_VERIFY(dn);
641789Sahrens 	ASSERT3P(dn->dn_dbuf, ==, db);
642789Sahrens 	ASSERT3U(dn->dn_object, ==, object);
6431544Seschrock 	dbuf_rele(db, FTAG);
644789Sahrens 
6451544Seschrock 	*dnp = dn;
6461544Seschrock 	return (0);
647789Sahrens }
648789Sahrens 
649789Sahrens /*
650789Sahrens  * Return held dnode if the object is allocated, NULL if not.
651789Sahrens  */
6521544Seschrock int
6531544Seschrock dnode_hold(objset_impl_t *os, uint64_t object, void *tag, dnode_t **dnp)
654789Sahrens {
6551544Seschrock 	return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, tag, dnp));
656789Sahrens }
657789Sahrens 
6584944Smaybee /*
6594944Smaybee  * Can only add a reference if there is already at least one
6604944Smaybee  * reference on the dnode.  Returns FALSE if unable to add a
6614944Smaybee  * new reference.
6624944Smaybee  */
6634944Smaybee boolean_t
6641544Seschrock dnode_add_ref(dnode_t *dn, void *tag)
665789Sahrens {
6664944Smaybee 	mutex_enter(&dn->dn_mtx);
6674944Smaybee 	if (refcount_is_zero(&dn->dn_holds)) {
6684944Smaybee 		mutex_exit(&dn->dn_mtx);
6694944Smaybee 		return (FALSE);
6704944Smaybee 	}
6714944Smaybee 	VERIFY(1 < refcount_add(&dn->dn_holds, tag));
6724944Smaybee 	mutex_exit(&dn->dn_mtx);
6734944Smaybee 	return (TRUE);
674789Sahrens }
675789Sahrens 
676789Sahrens void
6771544Seschrock dnode_rele(dnode_t *dn, void *tag)
678789Sahrens {
679789Sahrens 	uint64_t refs;
680789Sahrens 
6814944Smaybee 	mutex_enter(&dn->dn_mtx);
6821544Seschrock 	refs = refcount_remove(&dn->dn_holds, tag);
6834944Smaybee 	mutex_exit(&dn->dn_mtx);
684789Sahrens 	/* NOTE: the DNODE_DNODE does not have a dn_dbuf */
685789Sahrens 	if (refs == 0 && dn->dn_dbuf)
6861544Seschrock 		dbuf_rele(dn->dn_dbuf, dn);
687789Sahrens }
688789Sahrens 
689789Sahrens void
690789Sahrens dnode_setdirty(dnode_t *dn, dmu_tx_t *tx)
691789Sahrens {
692789Sahrens 	objset_impl_t *os = dn->dn_objset;
693789Sahrens 	uint64_t txg = tx->tx_txg;
694789Sahrens 
6951544Seschrock 	if (dn->dn_object == DMU_META_DNODE_OBJECT)
696789Sahrens 		return;
697789Sahrens 
698873Sek110237 	DNODE_VERIFY(dn);
699789Sahrens 
700789Sahrens #ifdef ZFS_DEBUG
701789Sahrens 	mutex_enter(&dn->dn_mtx);
702789Sahrens 	ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg);
703789Sahrens 	/* ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg); */
704789Sahrens 	mutex_exit(&dn->dn_mtx);
705789Sahrens #endif
706789Sahrens 
707789Sahrens 	mutex_enter(&os->os_lock);
708789Sahrens 
709789Sahrens 	/*
710789Sahrens 	 * If we are already marked dirty, we're done.
711789Sahrens 	 */
7121596Sahrens 	if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) {
713789Sahrens 		mutex_exit(&os->os_lock);
714789Sahrens 		return;
715789Sahrens 	}
716789Sahrens 
717789Sahrens 	ASSERT(!refcount_is_zero(&dn->dn_holds) || list_head(&dn->dn_dbufs));
718789Sahrens 	ASSERT(dn->dn_datablksz != 0);
7194944Smaybee 	ASSERT3U(dn->dn_next_bonuslen[txg&TXG_MASK], ==, 0);
7201599Sahrens 	ASSERT3U(dn->dn_next_blksz[txg&TXG_MASK], ==, 0);
721789Sahrens 
722789Sahrens 	dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
723789Sahrens 	    dn->dn_object, txg);
724789Sahrens 
725789Sahrens 	if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) {
726789Sahrens 		list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn);
727789Sahrens 	} else {
728789Sahrens 		list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn);
729789Sahrens 	}
730789Sahrens 
731789Sahrens 	mutex_exit(&os->os_lock);
732789Sahrens 
733789Sahrens 	/*
734789Sahrens 	 * The dnode maintains a hold on its containing dbuf as
735789Sahrens 	 * long as there are holds on it.  Each instantiated child
736789Sahrens 	 * dbuf maintaines a hold on the dnode.  When the last child
737789Sahrens 	 * drops its hold, the dnode will drop its hold on the
738789Sahrens 	 * containing dbuf. We add a "dirty hold" here so that the
739789Sahrens 	 * dnode will hang around after we finish processing its
740789Sahrens 	 * children.
741789Sahrens 	 */
7424944Smaybee 	VERIFY(dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg));
743789Sahrens 
7443547Smaybee 	(void) dbuf_dirty(dn->dn_dbuf, tx);
745789Sahrens 
746789Sahrens 	dsl_dataset_dirty(os->os_dsl_dataset, tx);
747789Sahrens }
748789Sahrens 
749789Sahrens void
750789Sahrens dnode_free(dnode_t *dn, dmu_tx_t *tx)
751789Sahrens {
7521596Sahrens 	int txgoff = tx->tx_txg & TXG_MASK;
7531596Sahrens 
754789Sahrens 	dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg);
755789Sahrens 
756789Sahrens 	/* we should be the only holder... hopefully */
757789Sahrens 	/* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */
758789Sahrens 
759789Sahrens 	mutex_enter(&dn->dn_mtx);
760789Sahrens 	if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) {
761789Sahrens 		mutex_exit(&dn->dn_mtx);
762789Sahrens 		return;
763789Sahrens 	}
764789Sahrens 	dn->dn_free_txg = tx->tx_txg;
765789Sahrens 	mutex_exit(&dn->dn_mtx);
766789Sahrens 
767789Sahrens 	/*
768789Sahrens 	 * If the dnode is already dirty, it needs to be moved from
769789Sahrens 	 * the dirty list to the free list.
770789Sahrens 	 */
771789Sahrens 	mutex_enter(&dn->dn_objset->os_lock);
7721596Sahrens 	if (list_link_active(&dn->dn_dirty_link[txgoff])) {
7731596Sahrens 		list_remove(&dn->dn_objset->os_dirty_dnodes[txgoff], dn);
7741596Sahrens 		list_insert_tail(&dn->dn_objset->os_free_dnodes[txgoff], dn);
775789Sahrens 		mutex_exit(&dn->dn_objset->os_lock);
776789Sahrens 	} else {
777789Sahrens 		mutex_exit(&dn->dn_objset->os_lock);
778789Sahrens 		dnode_setdirty(dn, tx);
779789Sahrens 	}
780789Sahrens }
781789Sahrens 
782789Sahrens /*
783789Sahrens  * Try to change the block size for the indicated dnode.  This can only
784789Sahrens  * succeed if there are no blocks allocated or dirty beyond first block
785789Sahrens  */
786789Sahrens int
787789Sahrens dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx)
788789Sahrens {
789789Sahrens 	dmu_buf_impl_t *db, *db_next;
7906992Smaybee 	int err;
791789Sahrens 
792789Sahrens 	if (size == 0)
793789Sahrens 		size = SPA_MINBLOCKSIZE;
794789Sahrens 	if (size > SPA_MAXBLOCKSIZE)
795789Sahrens 		size = SPA_MAXBLOCKSIZE;
796789Sahrens 	else
797789Sahrens 		size = P2ROUNDUP(size, SPA_MINBLOCKSIZE);
798789Sahrens 
7992445Sahrens 	if (ibs == dn->dn_indblkshift)
8002445Sahrens 		ibs = 0;
801789Sahrens 
8022445Sahrens 	if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0)
803789Sahrens 		return (0);
804789Sahrens 
805789Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
806789Sahrens 
807789Sahrens 	/* Check for any allocated blocks beyond the first */
808789Sahrens 	if (dn->dn_phys->dn_maxblkid != 0)
8092445Sahrens 		goto fail;
810789Sahrens 
811789Sahrens 	mutex_enter(&dn->dn_dbufs_mtx);
812789Sahrens 	for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
813789Sahrens 		db_next = list_next(&dn->dn_dbufs, db);
814789Sahrens 
8156992Smaybee 		if (db->db_blkid != 0 && db->db_blkid != DB_BONUS_BLKID) {
816789Sahrens 			mutex_exit(&dn->dn_dbufs_mtx);
8172445Sahrens 			goto fail;
818789Sahrens 		}
819789Sahrens 	}
820789Sahrens 	mutex_exit(&dn->dn_dbufs_mtx);
821789Sahrens 
8222445Sahrens 	if (ibs && dn->dn_nlevels != 1)
8232445Sahrens 		goto fail;
8242445Sahrens 
8256992Smaybee 	/* resize the old block */
8266992Smaybee 	err = dbuf_hold_impl(dn, 0, 0, TRUE, FTAG, &db);
8276992Smaybee 	if (err == 0)
8281596Sahrens 		dbuf_new_size(db, size, tx);
8296992Smaybee 	else if (err != ENOENT)
8306992Smaybee 		goto fail;
831789Sahrens 
832789Sahrens 	dnode_setdblksz(dn, size);
8331596Sahrens 	dnode_setdirty(dn, tx);
8341596Sahrens 	dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size;
8352445Sahrens 	if (ibs) {
8362445Sahrens 		dn->dn_indblkshift = ibs;
8372445Sahrens 		dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs;
8382445Sahrens 	}
8396992Smaybee 	/* rele after we have fixed the blocksize in the dnode */
8401596Sahrens 	if (db)
8411596Sahrens 		dbuf_rele(db, FTAG);
842789Sahrens 
843789Sahrens 	rw_exit(&dn->dn_struct_rwlock);
8442445Sahrens 	return (0);
8452445Sahrens 
8462445Sahrens fail:
8472445Sahrens 	rw_exit(&dn->dn_struct_rwlock);
8482445Sahrens 	return (ENOTSUP);
849789Sahrens }
850789Sahrens 
8517332SJonathan.Adams@Sun.COM /* read-holding callers must not rely on the lock being continuously held */
852789Sahrens void
8537332SJonathan.Adams@Sun.COM dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t have_read)
854789Sahrens {
855789Sahrens 	uint64_t txgoff = tx->tx_txg & TXG_MASK;
8561596Sahrens 	int epbs, new_nlevels;
857789Sahrens 	uint64_t sz;
858789Sahrens 
8591596Sahrens 	ASSERT(blkid != DB_BONUS_BLKID);
860789Sahrens 
8617332SJonathan.Adams@Sun.COM 	ASSERT(have_read ?
8627332SJonathan.Adams@Sun.COM 	    RW_READ_HELD(&dn->dn_struct_rwlock) :
8637332SJonathan.Adams@Sun.COM 	    RW_WRITE_HELD(&dn->dn_struct_rwlock));
8647332SJonathan.Adams@Sun.COM 
8657332SJonathan.Adams@Sun.COM 	/*
8667332SJonathan.Adams@Sun.COM 	 * if we have a read-lock, check to see if we need to do any work
8677332SJonathan.Adams@Sun.COM 	 * before upgrading to a write-lock.
8687332SJonathan.Adams@Sun.COM 	 */
8697332SJonathan.Adams@Sun.COM 	if (have_read) {
8707332SJonathan.Adams@Sun.COM 		if (blkid <= dn->dn_maxblkid)
8717332SJonathan.Adams@Sun.COM 			return;
8727332SJonathan.Adams@Sun.COM 
8737332SJonathan.Adams@Sun.COM 		if (!rw_tryupgrade(&dn->dn_struct_rwlock)) {
8747332SJonathan.Adams@Sun.COM 			rw_exit(&dn->dn_struct_rwlock);
8757332SJonathan.Adams@Sun.COM 			rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
8767332SJonathan.Adams@Sun.COM 		}
877789Sahrens 	}
878789Sahrens 
8791596Sahrens 	if (blkid <= dn->dn_maxblkid)
8801596Sahrens 		goto out;
8811596Sahrens 
8821596Sahrens 	dn->dn_maxblkid = blkid;
883789Sahrens 
884789Sahrens 	/*
8851596Sahrens 	 * Compute the number of levels necessary to support the new maxblkid.
886789Sahrens 	 */
887789Sahrens 	new_nlevels = 1;
888789Sahrens 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
8891596Sahrens 	for (sz = dn->dn_nblkptr;
8901596Sahrens 	    sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs)
891789Sahrens 		new_nlevels++;
892789Sahrens 
8931596Sahrens 	if (new_nlevels > dn->dn_nlevels) {
8941596Sahrens 		int old_nlevels = dn->dn_nlevels;
8951596Sahrens 		dmu_buf_impl_t *db;
8963547Smaybee 		list_t *list;
8973547Smaybee 		dbuf_dirty_record_t *new, *dr, *dr_next;
8981596Sahrens 
8991596Sahrens 		dn->dn_nlevels = new_nlevels;
9001596Sahrens 
9011596Sahrens 		ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]);
902789Sahrens 		dn->dn_next_nlevels[txgoff] = new_nlevels;
903789Sahrens 
9043547Smaybee 		/* dirty the left indirects */
9051596Sahrens 		db = dbuf_hold_level(dn, old_nlevels, 0, FTAG);
9063547Smaybee 		new = dbuf_dirty(db, tx);
9071544Seschrock 		dbuf_rele(db, FTAG);
9081596Sahrens 
9093547Smaybee 		/* transfer the dirty records to the new indirect */
9103547Smaybee 		mutex_enter(&dn->dn_mtx);
9113547Smaybee 		mutex_enter(&new->dt.di.dr_mtx);
9123547Smaybee 		list = &dn->dn_dirty_records[txgoff];
9133547Smaybee 		for (dr = list_head(list); dr; dr = dr_next) {
9143547Smaybee 			dr_next = list_next(&dn->dn_dirty_records[txgoff], dr);
9153547Smaybee 			if (dr->dr_dbuf->db_level != new_nlevels-1 &&
9163547Smaybee 			    dr->dr_dbuf->db_blkid != DB_BONUS_BLKID) {
9173547Smaybee 				ASSERT(dr->dr_dbuf->db_level == old_nlevels-1);
9183547Smaybee 				list_remove(&dn->dn_dirty_records[txgoff], dr);
9193547Smaybee 				list_insert_tail(&new->dt.di.dr_children, dr);
9203547Smaybee 				dr->dr_parent = new;
9213547Smaybee 			}
9223547Smaybee 		}
9233547Smaybee 		mutex_exit(&new->dt.di.dr_mtx);
9243547Smaybee 		mutex_exit(&dn->dn_mtx);
925789Sahrens 	}
926789Sahrens 
927789Sahrens out:
9287332SJonathan.Adams@Sun.COM 	if (have_read)
9297332SJonathan.Adams@Sun.COM 		rw_downgrade(&dn->dn_struct_rwlock);
930789Sahrens }
931789Sahrens 
932789Sahrens void
933789Sahrens dnode_clear_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
934789Sahrens {
935789Sahrens 	avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
936789Sahrens 	avl_index_t where;
937789Sahrens 	free_range_t *rp;
938789Sahrens 	free_range_t rp_tofind;
939789Sahrens 	uint64_t endblk = blkid + nblks;
940789Sahrens 
941789Sahrens 	ASSERT(MUTEX_HELD(&dn->dn_mtx));
942789Sahrens 	ASSERT(nblks <= UINT64_MAX - blkid); /* no overflow */
943789Sahrens 
944789Sahrens 	dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
945789Sahrens 	    blkid, nblks, tx->tx_txg);
946789Sahrens 	rp_tofind.fr_blkid = blkid;
947789Sahrens 	rp = avl_find(tree, &rp_tofind, &where);
948789Sahrens 	if (rp == NULL)
949789Sahrens 		rp = avl_nearest(tree, where, AVL_BEFORE);
950789Sahrens 	if (rp == NULL)
951789Sahrens 		rp = avl_nearest(tree, where, AVL_AFTER);
952789Sahrens 
953789Sahrens 	while (rp && (rp->fr_blkid <= blkid + nblks)) {
954789Sahrens 		uint64_t fr_endblk = rp->fr_blkid + rp->fr_nblks;
955789Sahrens 		free_range_t *nrp = AVL_NEXT(tree, rp);
956789Sahrens 
957789Sahrens 		if (blkid <= rp->fr_blkid && endblk >= fr_endblk) {
958789Sahrens 			/* clear this entire range */
959789Sahrens 			avl_remove(tree, rp);
960789Sahrens 			kmem_free(rp, sizeof (free_range_t));
961789Sahrens 		} else if (blkid <= rp->fr_blkid &&
962789Sahrens 		    endblk > rp->fr_blkid && endblk < fr_endblk) {
963789Sahrens 			/* clear the beginning of this range */
964789Sahrens 			rp->fr_blkid = endblk;
965789Sahrens 			rp->fr_nblks = fr_endblk - endblk;
966789Sahrens 		} else if (blkid > rp->fr_blkid && blkid < fr_endblk &&
967789Sahrens 		    endblk >= fr_endblk) {
968789Sahrens 			/* clear the end of this range */
969789Sahrens 			rp->fr_nblks = blkid - rp->fr_blkid;
970789Sahrens 		} else if (blkid > rp->fr_blkid && endblk < fr_endblk) {
971789Sahrens 			/* clear a chunk out of this range */
972789Sahrens 			free_range_t *new_rp =
973789Sahrens 			    kmem_alloc(sizeof (free_range_t), KM_SLEEP);
974789Sahrens 
975789Sahrens 			new_rp->fr_blkid = endblk;
976789Sahrens 			new_rp->fr_nblks = fr_endblk - endblk;
977789Sahrens 			avl_insert_here(tree, new_rp, rp, AVL_AFTER);
978789Sahrens 			rp->fr_nblks = blkid - rp->fr_blkid;
979789Sahrens 		}
980789Sahrens 		/* there may be no overlap */
981789Sahrens 		rp = nrp;
982789Sahrens 	}
983789Sahrens }
984789Sahrens 
985789Sahrens void
986789Sahrens dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx)
987789Sahrens {
988789Sahrens 	dmu_buf_impl_t *db;
9892445Sahrens 	uint64_t blkoff, blkid, nblks;
9906992Smaybee 	int blksz, blkshift, head, tail;
991789Sahrens 	int trunc = FALSE;
9926992Smaybee 	int epbs;
993789Sahrens 
994789Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
995789Sahrens 	blksz = dn->dn_datablksz;
9966992Smaybee 	blkshift = dn->dn_datablkshift;
9976992Smaybee 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
998789Sahrens 
999789Sahrens 	if (len == -1ULL) {
1000789Sahrens 		len = UINT64_MAX - off;
1001789Sahrens 		trunc = TRUE;
1002789Sahrens 	}
1003789Sahrens 
1004789Sahrens 	/*
1005789Sahrens 	 * First, block align the region to free:
1006789Sahrens 	 */
10072445Sahrens 	if (ISP2(blksz)) {
10082445Sahrens 		head = P2NPHASE(off, blksz);
10092445Sahrens 		blkoff = P2PHASE(off, blksz);
10106992Smaybee 		if ((off >> blkshift) > dn->dn_maxblkid)
10116992Smaybee 			goto out;
10122445Sahrens 	} else {
10132445Sahrens 		ASSERT(dn->dn_maxblkid == 0);
10142445Sahrens 		if (off == 0 && len >= blksz) {
10156992Smaybee 			/* Freeing the whole block; fast-track this request */
10166992Smaybee 			blkid = 0;
10176992Smaybee 			nblks = 1;
10186992Smaybee 			goto done;
10197385SMark.Maybee@Sun.COM 		} else if (off >= blksz) {
10206992Smaybee 			/* Freeing past end-of-data */
10216992Smaybee 			goto out;
1022789Sahrens 		} else {
10232445Sahrens 			/* Freeing part of the block. */
1024789Sahrens 			head = blksz - off;
1025789Sahrens 			ASSERT3U(head, >, 0);
1026789Sahrens 		}
10272445Sahrens 		blkoff = off;
1028789Sahrens 	}
1029789Sahrens 	/* zero out any partial block data at the start of the range */
1030789Sahrens 	if (head) {
10312445Sahrens 		ASSERT3U(blkoff + head, ==, blksz);
1032789Sahrens 		if (len < head)
1033789Sahrens 			head = len;
1034789Sahrens 		if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off), TRUE,
1035789Sahrens 		    FTAG, &db) == 0) {
1036789Sahrens 			caddr_t data;
1037789Sahrens 
1038789Sahrens 			/* don't dirty if it isn't on disk and isn't dirty */
10393547Smaybee 			if (db->db_last_dirty ||
1040789Sahrens 			    (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1041789Sahrens 				rw_exit(&dn->dn_struct_rwlock);
1042789Sahrens 				dbuf_will_dirty(db, tx);
1043789Sahrens 				rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1044789Sahrens 				data = db->db.db_data;
10452445Sahrens 				bzero(data + blkoff, head);
1046789Sahrens 			}
10471544Seschrock 			dbuf_rele(db, FTAG);
1048789Sahrens 		}
1049789Sahrens 		off += head;
1050789Sahrens 		len -= head;
1051789Sahrens 	}
1052789Sahrens 
10532445Sahrens 	/* If the range was less than one block, we're done */
10546992Smaybee 	if (len == 0)
10556992Smaybee 		goto out;
10566992Smaybee 
10576992Smaybee 	/* If the remaining range is past end of file, we're done */
10586992Smaybee 	if ((off >> blkshift) > dn->dn_maxblkid)
10596992Smaybee 		goto out;
10606992Smaybee 
10617385SMark.Maybee@Sun.COM 	ASSERT(ISP2(blksz));
10626992Smaybee 	if (trunc)
10636992Smaybee 		tail = 0;
10646992Smaybee 	else
10656992Smaybee 		tail = P2PHASE(len, blksz);
10666992Smaybee 
10676992Smaybee 	ASSERT3U(P2PHASE(off, blksz), ==, 0);
10686992Smaybee 	/* zero out any partial block data at the end of the range */
10696992Smaybee 	if (tail) {
10706992Smaybee 		if (len < tail)
10716992Smaybee 			tail = len;
10726992Smaybee 		if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len),
10736992Smaybee 		    TRUE, FTAG, &db) == 0) {
10746992Smaybee 			/* don't dirty if not on disk and not dirty */
10756992Smaybee 			if (db->db_last_dirty ||
10766992Smaybee 			    (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
10776992Smaybee 				rw_exit(&dn->dn_struct_rwlock);
10786992Smaybee 				dbuf_will_dirty(db, tx);
10796992Smaybee 				rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
10806992Smaybee 				bzero(db->db.db_data, tail);
10816992Smaybee 			}
10826992Smaybee 			dbuf_rele(db, FTAG);
10836992Smaybee 		}
10846992Smaybee 		len -= tail;
10856992Smaybee 	}
10866992Smaybee 
10876992Smaybee 	/* If the range did not include a full block, we are done */
10886992Smaybee 	if (len == 0)
1089789Sahrens 		goto out;
1090789Sahrens 
10916992Smaybee 	ASSERT(IS_P2ALIGNED(off, blksz));
10926992Smaybee 	ASSERT(trunc || IS_P2ALIGNED(len, blksz));
10936992Smaybee 	blkid = off >> blkshift;
10946992Smaybee 	nblks = len >> blkshift;
10956992Smaybee 	if (trunc)
10966992Smaybee 		nblks += 1;
10972445Sahrens 
10986992Smaybee 	/*
10996992Smaybee 	 * Read in and mark all the level-1 indirects dirty,
11006992Smaybee 	 * so that they will stay in memory until syncing phase.
11017049Smaybee 	 * Always dirty the first and last indirect to make sure
11027049Smaybee 	 * we dirty all the partial indirects.
11036992Smaybee 	 */
11046992Smaybee 	if (dn->dn_nlevels > 1) {
11056992Smaybee 		uint64_t i, first, last;
11066992Smaybee 		int shift = epbs + dn->dn_datablkshift;
11072445Sahrens 
11086992Smaybee 		first = blkid >> epbs;
11097049Smaybee 		if (db = dbuf_hold_level(dn, 1, first, FTAG)) {
11107049Smaybee 			dbuf_will_dirty(db, tx);
11117049Smaybee 			dbuf_rele(db, FTAG);
11127049Smaybee 		}
11136992Smaybee 		if (trunc)
11146992Smaybee 			last = dn->dn_maxblkid >> epbs;
11152445Sahrens 		else
11166992Smaybee 			last = (blkid + nblks - 1) >> epbs;
11177049Smaybee 		if (last > first && (db = dbuf_hold_level(dn, 1, last, FTAG))) {
11187049Smaybee 			dbuf_will_dirty(db, tx);
11197049Smaybee 			dbuf_rele(db, FTAG);
11207049Smaybee 		}
11217049Smaybee 		for (i = first + 1; i < last; i++) {
11226992Smaybee 			uint64_t ibyte = i << shift;
11236992Smaybee 			int err;
1124789Sahrens 
11256992Smaybee 			err = dnode_next_offset(dn,
11266992Smaybee 			    DNODE_FIND_HAVELOCK, &ibyte, 1, 1, 0);
11276992Smaybee 			i = ibyte >> shift;
11287049Smaybee 			if (err == ESRCH || i >= last)
11296992Smaybee 				break;
11306992Smaybee 			ASSERT(err == 0);
11316992Smaybee 			db = dbuf_hold_level(dn, 1, i, FTAG);
11326992Smaybee 			if (db) {
11336992Smaybee 				dbuf_will_dirty(db, tx);
11342445Sahrens 				dbuf_rele(db, FTAG);
1135789Sahrens 			}
11362445Sahrens 		}
11372445Sahrens 	}
11386992Smaybee done:
11396992Smaybee 	/*
11406992Smaybee 	 * Add this range to the dnode range list.
11416992Smaybee 	 * We will finish up this free operation in the syncing phase.
11426992Smaybee 	 */
1143789Sahrens 	mutex_enter(&dn->dn_mtx);
1144789Sahrens 	dnode_clear_range(dn, blkid, nblks, tx);
1145789Sahrens 	{
1146789Sahrens 		free_range_t *rp, *found;
1147789Sahrens 		avl_index_t where;
1148789Sahrens 		avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
1149789Sahrens 
1150789Sahrens 		/* Add new range to dn_ranges */
1151789Sahrens 		rp = kmem_alloc(sizeof (free_range_t), KM_SLEEP);
1152789Sahrens 		rp->fr_blkid = blkid;
1153789Sahrens 		rp->fr_nblks = nblks;
1154789Sahrens 		found = avl_find(tree, rp, &where);
1155789Sahrens 		ASSERT(found == NULL);
1156789Sahrens 		avl_insert(tree, rp, where);
1157789Sahrens 		dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1158789Sahrens 		    blkid, nblks, tx->tx_txg);
1159789Sahrens 	}
1160789Sahrens 	mutex_exit(&dn->dn_mtx);
1161789Sahrens 
11626992Smaybee 	dbuf_free_range(dn, blkid, blkid + nblks - 1, tx);
1163789Sahrens 	dnode_setdirty(dn, tx);
1164789Sahrens out:
11656992Smaybee 	if (trunc && dn->dn_maxblkid >= (off >> blkshift))
11666992Smaybee 		dn->dn_maxblkid = (off >> blkshift ? (off >> blkshift) - 1 : 0);
11676992Smaybee 
1168789Sahrens 	rw_exit(&dn->dn_struct_rwlock);
1169789Sahrens }
1170789Sahrens 
1171789Sahrens /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
1172789Sahrens uint64_t
1173789Sahrens dnode_block_freed(dnode_t *dn, uint64_t blkid)
1174789Sahrens {
1175789Sahrens 	free_range_t range_tofind;
1176789Sahrens 	void *dp = spa_get_dsl(dn->dn_objset->os_spa);
1177789Sahrens 	int i;
1178789Sahrens 
1179789Sahrens 	if (blkid == DB_BONUS_BLKID)
1180789Sahrens 		return (FALSE);
1181789Sahrens 
1182789Sahrens 	/*
1183789Sahrens 	 * If we're in the process of opening the pool, dp will not be
1184789Sahrens 	 * set yet, but there shouldn't be anything dirty.
1185789Sahrens 	 */
1186789Sahrens 	if (dp == NULL)
1187789Sahrens 		return (FALSE);
1188789Sahrens 
1189789Sahrens 	if (dn->dn_free_txg)
1190789Sahrens 		return (TRUE);
1191789Sahrens 
1192789Sahrens 	/*
1193789Sahrens 	 * If dn_datablkshift is not set, then there's only a single
1194789Sahrens 	 * block, in which case there will never be a free range so it
1195789Sahrens 	 * won't matter.
1196789Sahrens 	 */
1197789Sahrens 	range_tofind.fr_blkid = blkid;
1198789Sahrens 	mutex_enter(&dn->dn_mtx);
1199789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
1200789Sahrens 		free_range_t *range_found;
1201789Sahrens 		avl_index_t idx;
1202789Sahrens 
1203789Sahrens 		range_found = avl_find(&dn->dn_ranges[i], &range_tofind, &idx);
1204789Sahrens 		if (range_found) {
1205789Sahrens 			ASSERT(range_found->fr_nblks > 0);
1206789Sahrens 			break;
1207789Sahrens 		}
1208789Sahrens 		range_found = avl_nearest(&dn->dn_ranges[i], idx, AVL_BEFORE);
1209789Sahrens 		if (range_found &&
1210789Sahrens 		    range_found->fr_blkid + range_found->fr_nblks > blkid)
1211789Sahrens 			break;
1212789Sahrens 	}
1213789Sahrens 	mutex_exit(&dn->dn_mtx);
1214789Sahrens 	return (i < TXG_SIZE);
1215789Sahrens }
1216789Sahrens 
1217789Sahrens /* call from syncing context when we actually write/free space for this dnode */
1218789Sahrens void
12192082Seschrock dnode_diduse_space(dnode_t *dn, int64_t delta)
1220789Sahrens {
12212082Seschrock 	uint64_t space;
12222082Seschrock 	dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n",
1223789Sahrens 	    dn, dn->dn_phys,
12242082Seschrock 	    (u_longlong_t)dn->dn_phys->dn_used,
12252082Seschrock 	    (longlong_t)delta);
1226789Sahrens 
1227789Sahrens 	mutex_enter(&dn->dn_mtx);
12282082Seschrock 	space = DN_USED_BYTES(dn->dn_phys);
12292082Seschrock 	if (delta > 0) {
12302082Seschrock 		ASSERT3U(space + delta, >=, space); /* no overflow */
1231789Sahrens 	} else {
12322082Seschrock 		ASSERT3U(space, >=, -delta); /* no underflow */
12332082Seschrock 	}
12342082Seschrock 	space += delta;
12354577Sahrens 	if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_DNODE_BYTES) {
12362082Seschrock 		ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0);
12372082Seschrock 		ASSERT3U(P2PHASE(space, 1<<DEV_BSHIFT), ==, 0);
12382082Seschrock 		dn->dn_phys->dn_used = space >> DEV_BSHIFT;
12392082Seschrock 	} else {
12402082Seschrock 		dn->dn_phys->dn_used = space;
12412082Seschrock 		dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES;
1242789Sahrens 	}
1243789Sahrens 	mutex_exit(&dn->dn_mtx);
1244789Sahrens }
1245789Sahrens 
1246789Sahrens /*
1247789Sahrens  * Call when we think we're going to write/free space in open context.
1248789Sahrens  * Be conservative (ie. OK to write less than this or free more than
1249789Sahrens  * this, but don't write more or free less).
1250789Sahrens  */
1251789Sahrens void
1252789Sahrens dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx)
1253789Sahrens {
1254789Sahrens 	objset_impl_t *os = dn->dn_objset;
1255789Sahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1256789Sahrens 
1257789Sahrens 	if (space > 0)
1258789Sahrens 		space = spa_get_asize(os->os_spa, space);
1259789Sahrens 
1260789Sahrens 	if (ds)
1261789Sahrens 		dsl_dir_willuse_space(ds->ds_dir, space, tx);
1262789Sahrens 
1263789Sahrens 	dmu_tx_willuse_space(tx, space);
1264789Sahrens }
1265789Sahrens 
1266789Sahrens static int
12676992Smaybee dnode_next_offset_level(dnode_t *dn, int flags, uint64_t *offset,
12683025Sahrens 	int lvl, uint64_t blkfill, uint64_t txg)
1269789Sahrens {
1270789Sahrens 	dmu_buf_impl_t *db = NULL;
1271789Sahrens 	void *data = NULL;
1272789Sahrens 	uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
1273789Sahrens 	uint64_t epb = 1ULL << epbs;
1274789Sahrens 	uint64_t minfill, maxfill;
12756992Smaybee 	boolean_t hole;
12766992Smaybee 	int i, inc, error, span;
1277789Sahrens 
1278789Sahrens 	dprintf("probing object %llu offset %llx level %d of %u\n",
1279789Sahrens 	    dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels);
1280789Sahrens 
12816992Smaybee 	hole = flags & DNODE_FIND_HOLE;
12826992Smaybee 	inc = (flags & DNODE_FIND_BACKWARDS) ? -1 : 1;
12837385SMark.Maybee@Sun.COM 	ASSERT(txg == 0 || !hole);
12846992Smaybee 
1285789Sahrens 	if (lvl == dn->dn_phys->dn_nlevels) {
1286789Sahrens 		error = 0;
1287789Sahrens 		epb = dn->dn_phys->dn_nblkptr;
1288789Sahrens 		data = dn->dn_phys->dn_blkptr;
1289789Sahrens 	} else {
1290789Sahrens 		uint64_t blkid = dbuf_whichblock(dn, *offset) >> (epbs * lvl);
1291789Sahrens 		error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FTAG, &db);
1292789Sahrens 		if (error) {
12937385SMark.Maybee@Sun.COM 			if (error != ENOENT)
12947385SMark.Maybee@Sun.COM 				return (error);
12957385SMark.Maybee@Sun.COM 			if (hole)
12967385SMark.Maybee@Sun.COM 				return (0);
12977385SMark.Maybee@Sun.COM 			/*
12987385SMark.Maybee@Sun.COM 			 * This can only happen when we are searching up
12997385SMark.Maybee@Sun.COM 			 * the block tree for data.  We don't really need to
13007385SMark.Maybee@Sun.COM 			 * adjust the offset, as we will just end up looking
13017385SMark.Maybee@Sun.COM 			 * at the pointer to this block in its parent, and its
13027385SMark.Maybee@Sun.COM 			 * going to be unallocated, so we will skip over it.
13037385SMark.Maybee@Sun.COM 			 */
13047385SMark.Maybee@Sun.COM 			return (ESRCH);
1305789Sahrens 		}
13061793Sahrens 		error = dbuf_read(db, NULL, DB_RF_CANFAIL | DB_RF_HAVESTRUCT);
13071793Sahrens 		if (error) {
13081793Sahrens 			dbuf_rele(db, FTAG);
13091793Sahrens 			return (error);
13101793Sahrens 		}
1311789Sahrens 		data = db->db.db_data;
1312789Sahrens 	}
1313789Sahrens 
13143025Sahrens 	if (db && txg &&
13153025Sahrens 	    (db->db_blkptr == NULL || db->db_blkptr->blk_birth <= txg)) {
13167385SMark.Maybee@Sun.COM 		/*
13177385SMark.Maybee@Sun.COM 		 * This can only happen when we are searching up the tree
13187385SMark.Maybee@Sun.COM 		 * and these conditions mean that we need to keep climbing.
13197385SMark.Maybee@Sun.COM 		 */
13203025Sahrens 		error = ESRCH;
13213025Sahrens 	} else if (lvl == 0) {
1322789Sahrens 		dnode_phys_t *dnp = data;
1323789Sahrens 		span = DNODE_SHIFT;
1324789Sahrens 		ASSERT(dn->dn_type == DMU_OT_DNODE);
1325789Sahrens 
13266992Smaybee 		for (i = (*offset >> span) & (blkfill - 1);
13276992Smaybee 		    i >= 0 && i < blkfill; i += inc) {
13283025Sahrens 			boolean_t newcontents = B_TRUE;
13293025Sahrens 			if (txg) {
13303025Sahrens 				int j;
13313025Sahrens 				newcontents = B_FALSE;
13323025Sahrens 				for (j = 0; j < dnp[i].dn_nblkptr; j++) {
13333025Sahrens 					if (dnp[i].dn_blkptr[j].blk_birth > txg)
13343025Sahrens 						newcontents = B_TRUE;
13353025Sahrens 				}
13363025Sahrens 			}
13373025Sahrens 			if (!dnp[i].dn_type == hole && newcontents)
1338789Sahrens 				break;
13396992Smaybee 			*offset += (1ULL << span) * inc;
1340789Sahrens 		}
13416992Smaybee 		if (i < 0 || i == blkfill)
1342789Sahrens 			error = ESRCH;
1343789Sahrens 	} else {
1344789Sahrens 		blkptr_t *bp = data;
1345789Sahrens 		span = (lvl - 1) * epbs + dn->dn_datablkshift;
1346789Sahrens 		minfill = 0;
1347789Sahrens 		maxfill = blkfill << ((lvl - 1) * epbs);
1348789Sahrens 
1349789Sahrens 		if (hole)
1350789Sahrens 			maxfill--;
1351789Sahrens 		else
1352789Sahrens 			minfill++;
1353789Sahrens 
1354789Sahrens 		for (i = (*offset >> span) & ((1ULL << epbs) - 1);
13556992Smaybee 		    i >= 0 && i < epb; i += inc) {
1356789Sahrens 			if (bp[i].blk_fill >= minfill &&
13573025Sahrens 			    bp[i].blk_fill <= maxfill &&
13587385SMark.Maybee@Sun.COM 			    (hole || bp[i].blk_birth > txg))
1359789Sahrens 				break;
13607385SMark.Maybee@Sun.COM 			if (inc < 0 && *offset < (1ULL << span))
13617385SMark.Maybee@Sun.COM 				*offset = 0;
13627385SMark.Maybee@Sun.COM 			else
13637385SMark.Maybee@Sun.COM 				*offset += (1ULL << span) * inc;
1364789Sahrens 		}
13656992Smaybee 		if (i < 0 || i == epb)
1366789Sahrens 			error = ESRCH;
1367789Sahrens 	}
1368789Sahrens 
1369789Sahrens 	if (db)
13701544Seschrock 		dbuf_rele(db, FTAG);
1371789Sahrens 
1372789Sahrens 	return (error);
1373789Sahrens }
1374789Sahrens 
1375789Sahrens /*
1376789Sahrens  * Find the next hole, data, or sparse region at or after *offset.
1377789Sahrens  * The value 'blkfill' tells us how many items we expect to find
1378789Sahrens  * in an L0 data block; this value is 1 for normal objects,
1379789Sahrens  * DNODES_PER_BLOCK for the meta dnode, and some fraction of
1380789Sahrens  * DNODES_PER_BLOCK when searching for sparse regions thereof.
13813025Sahrens  *
1382789Sahrens  * Examples:
1383789Sahrens  *
13846992Smaybee  * dnode_next_offset(dn, flags, offset, 1, 1, 0);
13856992Smaybee  *	Finds the next/previous hole/data in a file.
1386789Sahrens  *	Used in dmu_offset_next().
1387789Sahrens  *
13886992Smaybee  * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
1389789Sahrens  *	Finds the next free/allocated dnode an objset's meta-dnode.
13903025Sahrens  *	Only finds objects that have new contents since txg (ie.
13913025Sahrens  *	bonus buffer changes and content removal are ignored).
1392789Sahrens  *	Used in dmu_object_next().
1393789Sahrens  *
13946992Smaybee  * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
1395789Sahrens  *	Finds the next L2 meta-dnode bp that's at most 1/4 full.
1396789Sahrens  *	Used in dmu_object_alloc().
1397789Sahrens  */
1398789Sahrens int
13996992Smaybee dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset,
14003025Sahrens     int minlvl, uint64_t blkfill, uint64_t txg)
1401789Sahrens {
14026992Smaybee 	uint64_t initial_offset = *offset;
1403789Sahrens 	int lvl, maxlvl;
1404789Sahrens 	int error = 0;
1405789Sahrens 
14066992Smaybee 	if (!(flags & DNODE_FIND_HAVELOCK))
14076992Smaybee 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
1408789Sahrens 
1409789Sahrens 	if (dn->dn_phys->dn_nlevels == 0) {
14106992Smaybee 		error = ESRCH;
14116992Smaybee 		goto out;
1412789Sahrens 	}
1413789Sahrens 
1414789Sahrens 	if (dn->dn_datablkshift == 0) {
1415789Sahrens 		if (*offset < dn->dn_datablksz) {
14166992Smaybee 			if (flags & DNODE_FIND_HOLE)
1417789Sahrens 				*offset = dn->dn_datablksz;
1418789Sahrens 		} else {
1419789Sahrens 			error = ESRCH;
1420789Sahrens 		}
14216992Smaybee 		goto out;
1422789Sahrens 	}
1423789Sahrens 
1424789Sahrens 	maxlvl = dn->dn_phys->dn_nlevels;
1425789Sahrens 
1426789Sahrens 	for (lvl = minlvl; lvl <= maxlvl; lvl++) {
14273025Sahrens 		error = dnode_next_offset_level(dn,
14286992Smaybee 		    flags, offset, lvl, blkfill, txg);
14291793Sahrens 		if (error != ESRCH)
1430789Sahrens 			break;
1431789Sahrens 	}
1432789Sahrens 
14336992Smaybee 	while (error == 0 && --lvl >= minlvl) {
14343025Sahrens 		error = dnode_next_offset_level(dn,
14356992Smaybee 		    flags, offset, lvl, blkfill, txg);
14363025Sahrens 	}
1437789Sahrens 
14386992Smaybee 	if (error == 0 && (flags & DNODE_FIND_BACKWARDS ?
14396992Smaybee 	    initial_offset < *offset : initial_offset > *offset))
14401793Sahrens 		error = ESRCH;
14416992Smaybee out:
14426992Smaybee 	if (!(flags & DNODE_FIND_HAVELOCK))
14436992Smaybee 		rw_exit(&dn->dn_struct_rwlock);
1444789Sahrens 
1445789Sahrens 	return (error);
1446789Sahrens }
1447