xref: /onnv-gate/usr/src/uts/common/fs/zfs/dnode.c (revision 9409)
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 /*
228582SBrendan.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);
1599396SMatthew.Ahrens@Sun.COM 	ASSERT(DMU_OBJECT_IS_SPECIAL(dn->dn_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 
3058582SBrendan.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
3249396SMatthew.Ahrens@Sun.COM 	ASSERT(dn->dn_oldphys == NULL);
3252885Sahrens 
326789Sahrens 	mutex_enter(&os->os_lock);
327789Sahrens 	list_remove(&os->os_dnodes, dn);
328789Sahrens 	mutex_exit(&os->os_lock);
329789Sahrens 
330789Sahrens 	if (dn->dn_dirtyctx_firstset) {
331789Sahrens 		kmem_free(dn->dn_dirtyctx_firstset, 1);
332789Sahrens 		dn->dn_dirtyctx_firstset = NULL;
333789Sahrens 	}
334789Sahrens 	dmu_zfetch_rele(&dn->dn_zfetch);
3351544Seschrock 	if (dn->dn_bonus) {
3361544Seschrock 		mutex_enter(&dn->dn_bonus->db_mtx);
3371544Seschrock 		dbuf_evict(dn->dn_bonus);
3381544Seschrock 		dn->dn_bonus = NULL;
3391544Seschrock 	}
340789Sahrens 	kmem_cache_free(dnode_cache, dn);
3418582SBrendan.Gregg@Sun.COM 	arc_space_return(sizeof (dnode_t), ARC_SPACE_OTHER);
342789Sahrens }
343789Sahrens 
344789Sahrens void
345789Sahrens dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
3461599Sahrens     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
347789Sahrens {
348789Sahrens 	int i;
349789Sahrens 
350789Sahrens 	if (blocksize == 0)
351789Sahrens 		blocksize = 1 << zfs_default_bs;
3521402Sahrens 	else if (blocksize > SPA_MAXBLOCKSIZE)
3531402Sahrens 		blocksize = SPA_MAXBLOCKSIZE;
3541402Sahrens 	else
3551402Sahrens 		blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE);
356789Sahrens 
357789Sahrens 	if (ibs == 0)
358789Sahrens 		ibs = zfs_default_ibs;
359789Sahrens 
360789Sahrens 	ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT);
361789Sahrens 
362789Sahrens 	dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset,
363789Sahrens 	    dn->dn_object, tx->tx_txg, blocksize, ibs);
364789Sahrens 
365789Sahrens 	ASSERT(dn->dn_type == DMU_OT_NONE);
366789Sahrens 	ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0);
367789Sahrens 	ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE);
368789Sahrens 	ASSERT(ot != DMU_OT_NONE);
369789Sahrens 	ASSERT3U(ot, <, DMU_OT_NUMTYPES);
370789Sahrens 	ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
371789Sahrens 	    (bonustype != DMU_OT_NONE && bonuslen != 0));
372789Sahrens 	ASSERT3U(bonustype, <, DMU_OT_NUMTYPES);
373789Sahrens 	ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
374789Sahrens 	ASSERT(dn->dn_type == DMU_OT_NONE);
375789Sahrens 	ASSERT3U(dn->dn_maxblkid, ==, 0);
376789Sahrens 	ASSERT3U(dn->dn_allocated_txg, ==, 0);
377789Sahrens 	ASSERT3U(dn->dn_assigned_txg, ==, 0);
378789Sahrens 	ASSERT(refcount_is_zero(&dn->dn_tx_holds));
379789Sahrens 	ASSERT3U(refcount_count(&dn->dn_holds), <=, 1);
380789Sahrens 	ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
381789Sahrens 
382789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
383789Sahrens 		ASSERT3U(dn->dn_next_nlevels[i], ==, 0);
384789Sahrens 		ASSERT3U(dn->dn_next_indblkshift[i], ==, 0);
3854944Smaybee 		ASSERT3U(dn->dn_next_bonuslen[i], ==, 0);
3861596Sahrens 		ASSERT3U(dn->dn_next_blksz[i], ==, 0);
3871596Sahrens 		ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
3883547Smaybee 		ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL);
389789Sahrens 		ASSERT3U(avl_numnodes(&dn->dn_ranges[i]), ==, 0);
390789Sahrens 	}
391789Sahrens 
392789Sahrens 	dn->dn_type = ot;
393789Sahrens 	dnode_setdblksz(dn, blocksize);
394789Sahrens 	dn->dn_indblkshift = ibs;
395789Sahrens 	dn->dn_nlevels = 1;
396789Sahrens 	dn->dn_nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
397789Sahrens 	dn->dn_bonustype = bonustype;
398789Sahrens 	dn->dn_bonuslen = bonuslen;
399789Sahrens 	dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
400789Sahrens 	dn->dn_compress = ZIO_COMPRESS_INHERIT;
401789Sahrens 	dn->dn_dirtyctx = 0;
402789Sahrens 
403789Sahrens 	dn->dn_free_txg = 0;
404789Sahrens 	if (dn->dn_dirtyctx_firstset) {
405789Sahrens 		kmem_free(dn->dn_dirtyctx_firstset, 1);
406789Sahrens 		dn->dn_dirtyctx_firstset = NULL;
407789Sahrens 	}
408789Sahrens 
409789Sahrens 	dn->dn_allocated_txg = tx->tx_txg;
4101599Sahrens 
411789Sahrens 	dnode_setdirty(dn, tx);
4121599Sahrens 	dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs;
4134944Smaybee 	dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
4141599Sahrens 	dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz;
415789Sahrens }
416789Sahrens 
417789Sahrens void
418789Sahrens dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
419789Sahrens     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
420789Sahrens {
4218986SMark.Maybee@Sun.COM 	int nblkptr;
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 
4331544Seschrock 	/* clean up any unreferenced dbufs */
4344944Smaybee 	dnode_evict_dbufs(dn);
4351544Seschrock 
4368986SMark.Maybee@Sun.COM 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
4378986SMark.Maybee@Sun.COM 	dnode_setdirty(dn, tx);
4388986SMark.Maybee@Sun.COM 	if (dn->dn_datablksz != blocksize) {
4398986SMark.Maybee@Sun.COM 		/* change blocksize */
4408986SMark.Maybee@Sun.COM 		ASSERT(dn->dn_maxblkid == 0 &&
4418986SMark.Maybee@Sun.COM 		    (BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) ||
4428986SMark.Maybee@Sun.COM 		    dnode_block_freed(dn, 0)));
4438986SMark.Maybee@Sun.COM 		dnode_setdblksz(dn, blocksize);
4448986SMark.Maybee@Sun.COM 		dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize;
445789Sahrens 	}
4468986SMark.Maybee@Sun.COM 	if (dn->dn_bonuslen != bonuslen)
4478986SMark.Maybee@Sun.COM 		dn->dn_next_bonuslen[tx->tx_txg&TXG_MASK] = bonuslen;
4488644SMark.Maybee@Sun.COM 	nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
4498644SMark.Maybee@Sun.COM 	if (dn->dn_nblkptr != nblkptr)
4508644SMark.Maybee@Sun.COM 		dn->dn_next_nblkptr[tx->tx_txg&TXG_MASK] = nblkptr;
451789Sahrens 	rw_exit(&dn->dn_struct_rwlock);
452789Sahrens 
453789Sahrens 	/* change type */
454789Sahrens 	dn->dn_type = ot;
455789Sahrens 
456789Sahrens 	/* change bonus size and type */
457789Sahrens 	mutex_enter(&dn->dn_mtx);
458789Sahrens 	dn->dn_bonustype = bonustype;
459789Sahrens 	dn->dn_bonuslen = bonuslen;
4608644SMark.Maybee@Sun.COM 	dn->dn_nblkptr = nblkptr;
461789Sahrens 	dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
462789Sahrens 	dn->dn_compress = ZIO_COMPRESS_INHERIT;
463789Sahrens 	ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
464789Sahrens 
4658644SMark.Maybee@Sun.COM 	/* fix up the bonus db_size */
4668644SMark.Maybee@Sun.COM 	if (dn->dn_bonus) {
4674944Smaybee 		dn->dn_bonus->db.db_size =
4684944Smaybee 		    DN_MAX_BONUSLEN - (dn->dn_nblkptr-1) * sizeof (blkptr_t);
4694944Smaybee 		ASSERT(dn->dn_bonuslen <= dn->dn_bonus->db.db_size);
4704944Smaybee 	}
4713087Sahrens 
472789Sahrens 	dn->dn_allocated_txg = tx->tx_txg;
473789Sahrens 	mutex_exit(&dn->dn_mtx);
474789Sahrens }
475789Sahrens 
476789Sahrens void
477789Sahrens dnode_special_close(dnode_t *dn)
478789Sahrens {
4791544Seschrock 	/*
4801544Seschrock 	 * Wait for final references to the dnode to clear.  This can
4811544Seschrock 	 * only happen if the arc is asyncronously evicting state that
4821544Seschrock 	 * has a hold on this dnode while we are trying to evict this
4831544Seschrock 	 * dnode.
4841544Seschrock 	 */
4851544Seschrock 	while (refcount_count(&dn->dn_holds) > 0)
4861544Seschrock 		delay(1);
487789Sahrens 	dnode_destroy(dn);
488789Sahrens }
489789Sahrens 
490789Sahrens dnode_t *
491789Sahrens dnode_special_open(objset_impl_t *os, dnode_phys_t *dnp, uint64_t object)
492789Sahrens {
493789Sahrens 	dnode_t *dn = dnode_create(os, dnp, NULL, object);
494873Sek110237 	DNODE_VERIFY(dn);
495789Sahrens 	return (dn);
496789Sahrens }
497789Sahrens 
498789Sahrens static void
499789Sahrens dnode_buf_pageout(dmu_buf_t *db, void *arg)
500789Sahrens {
501789Sahrens 	dnode_t **children_dnodes = arg;
502789Sahrens 	int i;
503789Sahrens 	int epb = db->db_size >> DNODE_SHIFT;
504789Sahrens 
505789Sahrens 	for (i = 0; i < epb; i++) {
506789Sahrens 		dnode_t *dn = children_dnodes[i];
507789Sahrens 		int n;
508789Sahrens 
509789Sahrens 		if (dn == NULL)
510789Sahrens 			continue;
511789Sahrens #ifdef ZFS_DEBUG
512789Sahrens 		/*
513789Sahrens 		 * If there are holds on this dnode, then there should
514789Sahrens 		 * be holds on the dnode's containing dbuf as well; thus
515789Sahrens 		 * it wouldn't be eligable for eviction and this function
516789Sahrens 		 * would not have been called.
517789Sahrens 		 */
518789Sahrens 		ASSERT(refcount_is_zero(&dn->dn_holds));
519789Sahrens 		ASSERT(list_head(&dn->dn_dbufs) == NULL);
520789Sahrens 		ASSERT(refcount_is_zero(&dn->dn_tx_holds));
521789Sahrens 
522789Sahrens 		for (n = 0; n < TXG_SIZE; n++)
5231596Sahrens 			ASSERT(!list_link_active(&dn->dn_dirty_link[n]));
524789Sahrens #endif
525789Sahrens 		children_dnodes[i] = NULL;
526789Sahrens 		dnode_destroy(dn);
527789Sahrens 	}
528789Sahrens 	kmem_free(children_dnodes, epb * sizeof (dnode_t *));
529789Sahrens }
530789Sahrens 
531789Sahrens /*
5321544Seschrock  * errors:
5331544Seschrock  * EINVAL - invalid object number.
5341544Seschrock  * EIO - i/o error.
5351544Seschrock  * succeeds even for free dnodes.
536789Sahrens  */
5371544Seschrock int
5381544Seschrock dnode_hold_impl(objset_impl_t *os, uint64_t object, int flag,
5391544Seschrock     void *tag, dnode_t **dnp)
540789Sahrens {
5411544Seschrock 	int epb, idx, err;
542789Sahrens 	int drop_struct_lock = FALSE;
5431544Seschrock 	int type;
544789Sahrens 	uint64_t blk;
545789Sahrens 	dnode_t *mdn, *dn;
546789Sahrens 	dmu_buf_impl_t *db;
547789Sahrens 	dnode_t **children_dnodes;
548789Sahrens 
5497754SJeff.Bonwick@Sun.COM 	/*
5507754SJeff.Bonwick@Sun.COM 	 * If you are holding the spa config lock as writer, you shouldn't
5517754SJeff.Bonwick@Sun.COM 	 * be asking the DMU to do *anything*.
5527754SJeff.Bonwick@Sun.COM 	 */
5537754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(os->os_spa, SCL_ALL, RW_WRITER) == 0);
5547754SJeff.Bonwick@Sun.COM 
5559396SMatthew.Ahrens@Sun.COM 	if (object == DMU_USERUSED_OBJECT || object == DMU_GROUPUSED_OBJECT) {
5569396SMatthew.Ahrens@Sun.COM 		dn = (object == DMU_USERUSED_OBJECT) ?
5579396SMatthew.Ahrens@Sun.COM 		    os->os_userused_dnode : os->os_groupused_dnode;
5589396SMatthew.Ahrens@Sun.COM 		if (dn == NULL)
5599396SMatthew.Ahrens@Sun.COM 			return (ENOENT);
5609396SMatthew.Ahrens@Sun.COM 		type = dn->dn_type;
5619396SMatthew.Ahrens@Sun.COM 		if ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE)
5629396SMatthew.Ahrens@Sun.COM 			return (ENOENT);
5639396SMatthew.Ahrens@Sun.COM 		if ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)
5649396SMatthew.Ahrens@Sun.COM 			return (EEXIST);
5659396SMatthew.Ahrens@Sun.COM 		DNODE_VERIFY(dn);
5669396SMatthew.Ahrens@Sun.COM 		(void) refcount_add(&dn->dn_holds, tag);
5679396SMatthew.Ahrens@Sun.COM 		*dnp = dn;
5689396SMatthew.Ahrens@Sun.COM 		return (0);
5699396SMatthew.Ahrens@Sun.COM 	}
5709396SMatthew.Ahrens@Sun.COM 
571789Sahrens 	if (object == 0 || object >= DN_MAX_OBJECT)
5721544Seschrock 		return (EINVAL);
573789Sahrens 
574789Sahrens 	mdn = os->os_meta_dnode;
575789Sahrens 
576873Sek110237 	DNODE_VERIFY(mdn);
577789Sahrens 
578789Sahrens 	if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) {
579789Sahrens 		rw_enter(&mdn->dn_struct_rwlock, RW_READER);
580789Sahrens 		drop_struct_lock = TRUE;
581789Sahrens 	}
582789Sahrens 
583789Sahrens 	blk = dbuf_whichblock(mdn, object * sizeof (dnode_phys_t));
584789Sahrens 
5851544Seschrock 	db = dbuf_hold(mdn, blk, FTAG);
586789Sahrens 	if (drop_struct_lock)
587789Sahrens 		rw_exit(&mdn->dn_struct_rwlock);
5881544Seschrock 	if (db == NULL)
5891544Seschrock 		return (EIO);
5901544Seschrock 	err = dbuf_read(db, NULL, DB_RF_CANFAIL);
5911544Seschrock 	if (err) {
5921544Seschrock 		dbuf_rele(db, FTAG);
5931544Seschrock 		return (err);
5941544Seschrock 	}
595789Sahrens 
596789Sahrens 	ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT);
597789Sahrens 	epb = db->db.db_size >> DNODE_SHIFT;
598789Sahrens 
599789Sahrens 	idx = object & (epb-1);
600789Sahrens 
601789Sahrens 	children_dnodes = dmu_buf_get_user(&db->db);
602789Sahrens 	if (children_dnodes == NULL) {
603789Sahrens 		dnode_t **winner;
604789Sahrens 		children_dnodes = kmem_zalloc(epb * sizeof (dnode_t *),
605789Sahrens 		    KM_SLEEP);
606789Sahrens 		if (winner = dmu_buf_set_user(&db->db, children_dnodes, NULL,
607789Sahrens 		    dnode_buf_pageout)) {
608789Sahrens 			kmem_free(children_dnodes, epb * sizeof (dnode_t *));
609789Sahrens 			children_dnodes = winner;
610789Sahrens 		}
611789Sahrens 	}
612789Sahrens 
613789Sahrens 	if ((dn = children_dnodes[idx]) == NULL) {
6144309Smaybee 		dnode_phys_t *dnp = (dnode_phys_t *)db->db.db_data+idx;
615789Sahrens 		dnode_t *winner;
6164309Smaybee 
6174309Smaybee 		dn = dnode_create(os, dnp, db, object);
618789Sahrens 		winner = atomic_cas_ptr(&children_dnodes[idx], NULL, dn);
619789Sahrens 		if (winner != NULL) {
620789Sahrens 			dnode_destroy(dn);
621789Sahrens 			dn = winner;
622789Sahrens 		}
623789Sahrens 	}
624789Sahrens 
625789Sahrens 	mutex_enter(&dn->dn_mtx);
6261544Seschrock 	type = dn->dn_type;
627789Sahrens 	if (dn->dn_free_txg ||
6281544Seschrock 	    ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) ||
6299396SMatthew.Ahrens@Sun.COM 	    ((flag & DNODE_MUST_BE_FREE) &&
6309396SMatthew.Ahrens@Sun.COM 	    (type != DMU_OT_NONE || dn->dn_oldphys))) {
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 
6959396SMatthew.Ahrens@Sun.COM 	if (DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
6969396SMatthew.Ahrens@Sun.COM 		dsl_dataset_dirty(os->os_dsl_dataset, tx);
697789Sahrens 		return;
6989396SMatthew.Ahrens@Sun.COM 	}
699789Sahrens 
700873Sek110237 	DNODE_VERIFY(dn);
701789Sahrens 
702789Sahrens #ifdef ZFS_DEBUG
703789Sahrens 	mutex_enter(&dn->dn_mtx);
704789Sahrens 	ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg);
705789Sahrens 	/* ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg); */
706789Sahrens 	mutex_exit(&dn->dn_mtx);
707789Sahrens #endif
708789Sahrens 
709789Sahrens 	mutex_enter(&os->os_lock);
710789Sahrens 
711789Sahrens 	/*
712789Sahrens 	 * If we are already marked dirty, we're done.
713789Sahrens 	 */
7141596Sahrens 	if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) {
715789Sahrens 		mutex_exit(&os->os_lock);
716789Sahrens 		return;
717789Sahrens 	}
718789Sahrens 
719789Sahrens 	ASSERT(!refcount_is_zero(&dn->dn_holds) || list_head(&dn->dn_dbufs));
720789Sahrens 	ASSERT(dn->dn_datablksz != 0);
7214944Smaybee 	ASSERT3U(dn->dn_next_bonuslen[txg&TXG_MASK], ==, 0);
7221599Sahrens 	ASSERT3U(dn->dn_next_blksz[txg&TXG_MASK], ==, 0);
723789Sahrens 
724789Sahrens 	dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
725789Sahrens 	    dn->dn_object, txg);
726789Sahrens 
727789Sahrens 	if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) {
728789Sahrens 		list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn);
729789Sahrens 	} else {
730789Sahrens 		list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn);
731789Sahrens 	}
732789Sahrens 
733789Sahrens 	mutex_exit(&os->os_lock);
734789Sahrens 
735789Sahrens 	/*
736789Sahrens 	 * The dnode maintains a hold on its containing dbuf as
737789Sahrens 	 * long as there are holds on it.  Each instantiated child
738789Sahrens 	 * dbuf maintaines a hold on the dnode.  When the last child
739789Sahrens 	 * drops its hold, the dnode will drop its hold on the
740789Sahrens 	 * containing dbuf. We add a "dirty hold" here so that the
741789Sahrens 	 * dnode will hang around after we finish processing its
742789Sahrens 	 * children.
743789Sahrens 	 */
7444944Smaybee 	VERIFY(dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg));
745789Sahrens 
7463547Smaybee 	(void) dbuf_dirty(dn->dn_dbuf, tx);
747789Sahrens 
748789Sahrens 	dsl_dataset_dirty(os->os_dsl_dataset, tx);
749789Sahrens }
750789Sahrens 
751789Sahrens void
752789Sahrens dnode_free(dnode_t *dn, dmu_tx_t *tx)
753789Sahrens {
7541596Sahrens 	int txgoff = tx->tx_txg & TXG_MASK;
7551596Sahrens 
756789Sahrens 	dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg);
757789Sahrens 
758789Sahrens 	/* we should be the only holder... hopefully */
759789Sahrens 	/* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */
760789Sahrens 
761789Sahrens 	mutex_enter(&dn->dn_mtx);
762789Sahrens 	if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) {
763789Sahrens 		mutex_exit(&dn->dn_mtx);
764789Sahrens 		return;
765789Sahrens 	}
766789Sahrens 	dn->dn_free_txg = tx->tx_txg;
767789Sahrens 	mutex_exit(&dn->dn_mtx);
768789Sahrens 
769789Sahrens 	/*
770789Sahrens 	 * If the dnode is already dirty, it needs to be moved from
771789Sahrens 	 * the dirty list to the free list.
772789Sahrens 	 */
773789Sahrens 	mutex_enter(&dn->dn_objset->os_lock);
7741596Sahrens 	if (list_link_active(&dn->dn_dirty_link[txgoff])) {
7751596Sahrens 		list_remove(&dn->dn_objset->os_dirty_dnodes[txgoff], dn);
7761596Sahrens 		list_insert_tail(&dn->dn_objset->os_free_dnodes[txgoff], dn);
777789Sahrens 		mutex_exit(&dn->dn_objset->os_lock);
778789Sahrens 	} else {
779789Sahrens 		mutex_exit(&dn->dn_objset->os_lock);
780789Sahrens 		dnode_setdirty(dn, tx);
781789Sahrens 	}
782789Sahrens }
783789Sahrens 
784789Sahrens /*
785789Sahrens  * Try to change the block size for the indicated dnode.  This can only
786789Sahrens  * succeed if there are no blocks allocated or dirty beyond first block
787789Sahrens  */
788789Sahrens int
789789Sahrens dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx)
790789Sahrens {
791789Sahrens 	dmu_buf_impl_t *db, *db_next;
7926992Smaybee 	int err;
793789Sahrens 
794789Sahrens 	if (size == 0)
795789Sahrens 		size = SPA_MINBLOCKSIZE;
796789Sahrens 	if (size > SPA_MAXBLOCKSIZE)
797789Sahrens 		size = SPA_MAXBLOCKSIZE;
798789Sahrens 	else
799789Sahrens 		size = P2ROUNDUP(size, SPA_MINBLOCKSIZE);
800789Sahrens 
8012445Sahrens 	if (ibs == dn->dn_indblkshift)
8022445Sahrens 		ibs = 0;
803789Sahrens 
8042445Sahrens 	if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0)
805789Sahrens 		return (0);
806789Sahrens 
807789Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
808789Sahrens 
809789Sahrens 	/* Check for any allocated blocks beyond the first */
810789Sahrens 	if (dn->dn_phys->dn_maxblkid != 0)
8112445Sahrens 		goto fail;
812789Sahrens 
813789Sahrens 	mutex_enter(&dn->dn_dbufs_mtx);
814789Sahrens 	for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
815789Sahrens 		db_next = list_next(&dn->dn_dbufs, db);
816789Sahrens 
8176992Smaybee 		if (db->db_blkid != 0 && db->db_blkid != DB_BONUS_BLKID) {
818789Sahrens 			mutex_exit(&dn->dn_dbufs_mtx);
8192445Sahrens 			goto fail;
820789Sahrens 		}
821789Sahrens 	}
822789Sahrens 	mutex_exit(&dn->dn_dbufs_mtx);
823789Sahrens 
8242445Sahrens 	if (ibs && dn->dn_nlevels != 1)
8252445Sahrens 		goto fail;
8262445Sahrens 
8276992Smaybee 	/* resize the old block */
8286992Smaybee 	err = dbuf_hold_impl(dn, 0, 0, TRUE, FTAG, &db);
8296992Smaybee 	if (err == 0)
8301596Sahrens 		dbuf_new_size(db, size, tx);
8316992Smaybee 	else if (err != ENOENT)
8326992Smaybee 		goto fail;
833789Sahrens 
834789Sahrens 	dnode_setdblksz(dn, size);
8351596Sahrens 	dnode_setdirty(dn, tx);
8361596Sahrens 	dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size;
8372445Sahrens 	if (ibs) {
8382445Sahrens 		dn->dn_indblkshift = ibs;
8392445Sahrens 		dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs;
8402445Sahrens 	}
8416992Smaybee 	/* rele after we have fixed the blocksize in the dnode */
8421596Sahrens 	if (db)
8431596Sahrens 		dbuf_rele(db, FTAG);
844789Sahrens 
845789Sahrens 	rw_exit(&dn->dn_struct_rwlock);
8462445Sahrens 	return (0);
8472445Sahrens 
8482445Sahrens fail:
8492445Sahrens 	rw_exit(&dn->dn_struct_rwlock);
8502445Sahrens 	return (ENOTSUP);
851789Sahrens }
852789Sahrens 
8537332SJonathan.Adams@Sun.COM /* read-holding callers must not rely on the lock being continuously held */
854789Sahrens void
8557332SJonathan.Adams@Sun.COM dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t have_read)
856789Sahrens {
857789Sahrens 	uint64_t txgoff = tx->tx_txg & TXG_MASK;
8581596Sahrens 	int epbs, new_nlevels;
859789Sahrens 	uint64_t sz;
860789Sahrens 
8611596Sahrens 	ASSERT(blkid != DB_BONUS_BLKID);
862789Sahrens 
8637332SJonathan.Adams@Sun.COM 	ASSERT(have_read ?
8647332SJonathan.Adams@Sun.COM 	    RW_READ_HELD(&dn->dn_struct_rwlock) :
8657332SJonathan.Adams@Sun.COM 	    RW_WRITE_HELD(&dn->dn_struct_rwlock));
8667332SJonathan.Adams@Sun.COM 
8677332SJonathan.Adams@Sun.COM 	/*
8687332SJonathan.Adams@Sun.COM 	 * if we have a read-lock, check to see if we need to do any work
8697332SJonathan.Adams@Sun.COM 	 * before upgrading to a write-lock.
8707332SJonathan.Adams@Sun.COM 	 */
8717332SJonathan.Adams@Sun.COM 	if (have_read) {
8727332SJonathan.Adams@Sun.COM 		if (blkid <= dn->dn_maxblkid)
8737332SJonathan.Adams@Sun.COM 			return;
8747332SJonathan.Adams@Sun.COM 
8757332SJonathan.Adams@Sun.COM 		if (!rw_tryupgrade(&dn->dn_struct_rwlock)) {
8767332SJonathan.Adams@Sun.COM 			rw_exit(&dn->dn_struct_rwlock);
8777332SJonathan.Adams@Sun.COM 			rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
8787332SJonathan.Adams@Sun.COM 		}
879789Sahrens 	}
880789Sahrens 
8811596Sahrens 	if (blkid <= dn->dn_maxblkid)
8821596Sahrens 		goto out;
8831596Sahrens 
8841596Sahrens 	dn->dn_maxblkid = blkid;
885789Sahrens 
886789Sahrens 	/*
8871596Sahrens 	 * Compute the number of levels necessary to support the new maxblkid.
888789Sahrens 	 */
889789Sahrens 	new_nlevels = 1;
890789Sahrens 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
8911596Sahrens 	for (sz = dn->dn_nblkptr;
8921596Sahrens 	    sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs)
893789Sahrens 		new_nlevels++;
894789Sahrens 
8951596Sahrens 	if (new_nlevels > dn->dn_nlevels) {
8961596Sahrens 		int old_nlevels = dn->dn_nlevels;
8971596Sahrens 		dmu_buf_impl_t *db;
8983547Smaybee 		list_t *list;
8993547Smaybee 		dbuf_dirty_record_t *new, *dr, *dr_next;
9001596Sahrens 
9011596Sahrens 		dn->dn_nlevels = new_nlevels;
9021596Sahrens 
9031596Sahrens 		ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]);
904789Sahrens 		dn->dn_next_nlevels[txgoff] = new_nlevels;
905789Sahrens 
9063547Smaybee 		/* dirty the left indirects */
9071596Sahrens 		db = dbuf_hold_level(dn, old_nlevels, 0, FTAG);
9083547Smaybee 		new = dbuf_dirty(db, tx);
9091544Seschrock 		dbuf_rele(db, FTAG);
9101596Sahrens 
9113547Smaybee 		/* transfer the dirty records to the new indirect */
9123547Smaybee 		mutex_enter(&dn->dn_mtx);
9133547Smaybee 		mutex_enter(&new->dt.di.dr_mtx);
9143547Smaybee 		list = &dn->dn_dirty_records[txgoff];
9153547Smaybee 		for (dr = list_head(list); dr; dr = dr_next) {
9163547Smaybee 			dr_next = list_next(&dn->dn_dirty_records[txgoff], dr);
9173547Smaybee 			if (dr->dr_dbuf->db_level != new_nlevels-1 &&
9183547Smaybee 			    dr->dr_dbuf->db_blkid != DB_BONUS_BLKID) {
9193547Smaybee 				ASSERT(dr->dr_dbuf->db_level == old_nlevels-1);
9203547Smaybee 				list_remove(&dn->dn_dirty_records[txgoff], dr);
9213547Smaybee 				list_insert_tail(&new->dt.di.dr_children, dr);
9223547Smaybee 				dr->dr_parent = new;
9233547Smaybee 			}
9243547Smaybee 		}
9253547Smaybee 		mutex_exit(&new->dt.di.dr_mtx);
9263547Smaybee 		mutex_exit(&dn->dn_mtx);
927789Sahrens 	}
928789Sahrens 
929789Sahrens out:
9307332SJonathan.Adams@Sun.COM 	if (have_read)
9317332SJonathan.Adams@Sun.COM 		rw_downgrade(&dn->dn_struct_rwlock);
932789Sahrens }
933789Sahrens 
934789Sahrens void
935789Sahrens dnode_clear_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
936789Sahrens {
937789Sahrens 	avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
938789Sahrens 	avl_index_t where;
939789Sahrens 	free_range_t *rp;
940789Sahrens 	free_range_t rp_tofind;
941789Sahrens 	uint64_t endblk = blkid + nblks;
942789Sahrens 
943789Sahrens 	ASSERT(MUTEX_HELD(&dn->dn_mtx));
944789Sahrens 	ASSERT(nblks <= UINT64_MAX - blkid); /* no overflow */
945789Sahrens 
946789Sahrens 	dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
947789Sahrens 	    blkid, nblks, tx->tx_txg);
948789Sahrens 	rp_tofind.fr_blkid = blkid;
949789Sahrens 	rp = avl_find(tree, &rp_tofind, &where);
950789Sahrens 	if (rp == NULL)
951789Sahrens 		rp = avl_nearest(tree, where, AVL_BEFORE);
952789Sahrens 	if (rp == NULL)
953789Sahrens 		rp = avl_nearest(tree, where, AVL_AFTER);
954789Sahrens 
955789Sahrens 	while (rp && (rp->fr_blkid <= blkid + nblks)) {
956789Sahrens 		uint64_t fr_endblk = rp->fr_blkid + rp->fr_nblks;
957789Sahrens 		free_range_t *nrp = AVL_NEXT(tree, rp);
958789Sahrens 
959789Sahrens 		if (blkid <= rp->fr_blkid && endblk >= fr_endblk) {
960789Sahrens 			/* clear this entire range */
961789Sahrens 			avl_remove(tree, rp);
962789Sahrens 			kmem_free(rp, sizeof (free_range_t));
963789Sahrens 		} else if (blkid <= rp->fr_blkid &&
964789Sahrens 		    endblk > rp->fr_blkid && endblk < fr_endblk) {
965789Sahrens 			/* clear the beginning of this range */
966789Sahrens 			rp->fr_blkid = endblk;
967789Sahrens 			rp->fr_nblks = fr_endblk - endblk;
968789Sahrens 		} else if (blkid > rp->fr_blkid && blkid < fr_endblk &&
969789Sahrens 		    endblk >= fr_endblk) {
970789Sahrens 			/* clear the end of this range */
971789Sahrens 			rp->fr_nblks = blkid - rp->fr_blkid;
972789Sahrens 		} else if (blkid > rp->fr_blkid && endblk < fr_endblk) {
973789Sahrens 			/* clear a chunk out of this range */
974789Sahrens 			free_range_t *new_rp =
975789Sahrens 			    kmem_alloc(sizeof (free_range_t), KM_SLEEP);
976789Sahrens 
977789Sahrens 			new_rp->fr_blkid = endblk;
978789Sahrens 			new_rp->fr_nblks = fr_endblk - endblk;
979789Sahrens 			avl_insert_here(tree, new_rp, rp, AVL_AFTER);
980789Sahrens 			rp->fr_nblks = blkid - rp->fr_blkid;
981789Sahrens 		}
982789Sahrens 		/* there may be no overlap */
983789Sahrens 		rp = nrp;
984789Sahrens 	}
985789Sahrens }
986789Sahrens 
987789Sahrens void
988789Sahrens dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx)
989789Sahrens {
990789Sahrens 	dmu_buf_impl_t *db;
9912445Sahrens 	uint64_t blkoff, blkid, nblks;
9926992Smaybee 	int blksz, blkshift, head, tail;
993789Sahrens 	int trunc = FALSE;
9946992Smaybee 	int epbs;
995789Sahrens 
996789Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
997789Sahrens 	blksz = dn->dn_datablksz;
9986992Smaybee 	blkshift = dn->dn_datablkshift;
9996992Smaybee 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1000789Sahrens 
1001789Sahrens 	if (len == -1ULL) {
1002789Sahrens 		len = UINT64_MAX - off;
1003789Sahrens 		trunc = TRUE;
1004789Sahrens 	}
1005789Sahrens 
1006789Sahrens 	/*
1007789Sahrens 	 * First, block align the region to free:
1008789Sahrens 	 */
10092445Sahrens 	if (ISP2(blksz)) {
10102445Sahrens 		head = P2NPHASE(off, blksz);
10112445Sahrens 		blkoff = P2PHASE(off, blksz);
10126992Smaybee 		if ((off >> blkshift) > dn->dn_maxblkid)
10136992Smaybee 			goto out;
10142445Sahrens 	} else {
10152445Sahrens 		ASSERT(dn->dn_maxblkid == 0);
10162445Sahrens 		if (off == 0 && len >= blksz) {
10176992Smaybee 			/* Freeing the whole block; fast-track this request */
10186992Smaybee 			blkid = 0;
10196992Smaybee 			nblks = 1;
10206992Smaybee 			goto done;
10217385SMark.Maybee@Sun.COM 		} else if (off >= blksz) {
10226992Smaybee 			/* Freeing past end-of-data */
10236992Smaybee 			goto out;
1024789Sahrens 		} else {
10252445Sahrens 			/* Freeing part of the block. */
1026789Sahrens 			head = blksz - off;
1027789Sahrens 			ASSERT3U(head, >, 0);
1028789Sahrens 		}
10292445Sahrens 		blkoff = off;
1030789Sahrens 	}
1031789Sahrens 	/* zero out any partial block data at the start of the range */
1032789Sahrens 	if (head) {
10332445Sahrens 		ASSERT3U(blkoff + head, ==, blksz);
1034789Sahrens 		if (len < head)
1035789Sahrens 			head = len;
1036789Sahrens 		if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off), TRUE,
1037789Sahrens 		    FTAG, &db) == 0) {
1038789Sahrens 			caddr_t data;
1039789Sahrens 
1040789Sahrens 			/* don't dirty if it isn't on disk and isn't dirty */
10413547Smaybee 			if (db->db_last_dirty ||
1042789Sahrens 			    (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1043789Sahrens 				rw_exit(&dn->dn_struct_rwlock);
1044789Sahrens 				dbuf_will_dirty(db, tx);
1045789Sahrens 				rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1046789Sahrens 				data = db->db.db_data;
10472445Sahrens 				bzero(data + blkoff, head);
1048789Sahrens 			}
10491544Seschrock 			dbuf_rele(db, FTAG);
1050789Sahrens 		}
1051789Sahrens 		off += head;
1052789Sahrens 		len -= head;
1053789Sahrens 	}
1054789Sahrens 
10552445Sahrens 	/* If the range was less than one block, we're done */
10566992Smaybee 	if (len == 0)
10576992Smaybee 		goto out;
10586992Smaybee 
10596992Smaybee 	/* If the remaining range is past end of file, we're done */
10606992Smaybee 	if ((off >> blkshift) > dn->dn_maxblkid)
10616992Smaybee 		goto out;
10626992Smaybee 
10637385SMark.Maybee@Sun.COM 	ASSERT(ISP2(blksz));
10646992Smaybee 	if (trunc)
10656992Smaybee 		tail = 0;
10666992Smaybee 	else
10676992Smaybee 		tail = P2PHASE(len, blksz);
10686992Smaybee 
10696992Smaybee 	ASSERT3U(P2PHASE(off, blksz), ==, 0);
10706992Smaybee 	/* zero out any partial block data at the end of the range */
10716992Smaybee 	if (tail) {
10726992Smaybee 		if (len < tail)
10736992Smaybee 			tail = len;
10746992Smaybee 		if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len),
10756992Smaybee 		    TRUE, FTAG, &db) == 0) {
10766992Smaybee 			/* don't dirty if not on disk and not dirty */
10776992Smaybee 			if (db->db_last_dirty ||
10786992Smaybee 			    (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
10796992Smaybee 				rw_exit(&dn->dn_struct_rwlock);
10806992Smaybee 				dbuf_will_dirty(db, tx);
10816992Smaybee 				rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
10826992Smaybee 				bzero(db->db.db_data, tail);
10836992Smaybee 			}
10846992Smaybee 			dbuf_rele(db, FTAG);
10856992Smaybee 		}
10866992Smaybee 		len -= tail;
10876992Smaybee 	}
10886992Smaybee 
10896992Smaybee 	/* If the range did not include a full block, we are done */
10906992Smaybee 	if (len == 0)
1091789Sahrens 		goto out;
1092789Sahrens 
10936992Smaybee 	ASSERT(IS_P2ALIGNED(off, blksz));
10946992Smaybee 	ASSERT(trunc || IS_P2ALIGNED(len, blksz));
10956992Smaybee 	blkid = off >> blkshift;
10966992Smaybee 	nblks = len >> blkshift;
10976992Smaybee 	if (trunc)
10986992Smaybee 		nblks += 1;
10992445Sahrens 
11006992Smaybee 	/*
11016992Smaybee 	 * Read in and mark all the level-1 indirects dirty,
11026992Smaybee 	 * so that they will stay in memory until syncing phase.
11037049Smaybee 	 * Always dirty the first and last indirect to make sure
11047049Smaybee 	 * we dirty all the partial indirects.
11056992Smaybee 	 */
11066992Smaybee 	if (dn->dn_nlevels > 1) {
11076992Smaybee 		uint64_t i, first, last;
11086992Smaybee 		int shift = epbs + dn->dn_datablkshift;
11092445Sahrens 
11106992Smaybee 		first = blkid >> epbs;
11117049Smaybee 		if (db = dbuf_hold_level(dn, 1, first, FTAG)) {
11127049Smaybee 			dbuf_will_dirty(db, tx);
11137049Smaybee 			dbuf_rele(db, FTAG);
11147049Smaybee 		}
11156992Smaybee 		if (trunc)
11166992Smaybee 			last = dn->dn_maxblkid >> epbs;
11172445Sahrens 		else
11186992Smaybee 			last = (blkid + nblks - 1) >> epbs;
11197049Smaybee 		if (last > first && (db = dbuf_hold_level(dn, 1, last, FTAG))) {
11207049Smaybee 			dbuf_will_dirty(db, tx);
11217049Smaybee 			dbuf_rele(db, FTAG);
11227049Smaybee 		}
11237049Smaybee 		for (i = first + 1; i < last; i++) {
11246992Smaybee 			uint64_t ibyte = i << shift;
11256992Smaybee 			int err;
1126789Sahrens 
11276992Smaybee 			err = dnode_next_offset(dn,
11286992Smaybee 			    DNODE_FIND_HAVELOCK, &ibyte, 1, 1, 0);
11296992Smaybee 			i = ibyte >> shift;
11307049Smaybee 			if (err == ESRCH || i >= last)
11316992Smaybee 				break;
11326992Smaybee 			ASSERT(err == 0);
11336992Smaybee 			db = dbuf_hold_level(dn, 1, i, FTAG);
11346992Smaybee 			if (db) {
11356992Smaybee 				dbuf_will_dirty(db, tx);
11362445Sahrens 				dbuf_rele(db, FTAG);
1137789Sahrens 			}
11382445Sahrens 		}
11392445Sahrens 	}
11406992Smaybee done:
11416992Smaybee 	/*
11426992Smaybee 	 * Add this range to the dnode range list.
11436992Smaybee 	 * We will finish up this free operation in the syncing phase.
11446992Smaybee 	 */
1145789Sahrens 	mutex_enter(&dn->dn_mtx);
1146789Sahrens 	dnode_clear_range(dn, blkid, nblks, tx);
1147789Sahrens 	{
1148789Sahrens 		free_range_t *rp, *found;
1149789Sahrens 		avl_index_t where;
1150789Sahrens 		avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
1151789Sahrens 
1152789Sahrens 		/* Add new range to dn_ranges */
1153789Sahrens 		rp = kmem_alloc(sizeof (free_range_t), KM_SLEEP);
1154789Sahrens 		rp->fr_blkid = blkid;
1155789Sahrens 		rp->fr_nblks = nblks;
1156789Sahrens 		found = avl_find(tree, rp, &where);
1157789Sahrens 		ASSERT(found == NULL);
1158789Sahrens 		avl_insert(tree, rp, where);
1159789Sahrens 		dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1160789Sahrens 		    blkid, nblks, tx->tx_txg);
1161789Sahrens 	}
1162789Sahrens 	mutex_exit(&dn->dn_mtx);
1163789Sahrens 
11646992Smaybee 	dbuf_free_range(dn, blkid, blkid + nblks - 1, tx);
1165789Sahrens 	dnode_setdirty(dn, tx);
1166789Sahrens out:
11676992Smaybee 	if (trunc && dn->dn_maxblkid >= (off >> blkshift))
11686992Smaybee 		dn->dn_maxblkid = (off >> blkshift ? (off >> blkshift) - 1 : 0);
11696992Smaybee 
1170789Sahrens 	rw_exit(&dn->dn_struct_rwlock);
1171789Sahrens }
1172789Sahrens 
1173789Sahrens /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
1174789Sahrens uint64_t
1175789Sahrens dnode_block_freed(dnode_t *dn, uint64_t blkid)
1176789Sahrens {
1177789Sahrens 	free_range_t range_tofind;
1178789Sahrens 	void *dp = spa_get_dsl(dn->dn_objset->os_spa);
1179789Sahrens 	int i;
1180789Sahrens 
1181789Sahrens 	if (blkid == DB_BONUS_BLKID)
1182789Sahrens 		return (FALSE);
1183789Sahrens 
1184789Sahrens 	/*
1185789Sahrens 	 * If we're in the process of opening the pool, dp will not be
1186789Sahrens 	 * set yet, but there shouldn't be anything dirty.
1187789Sahrens 	 */
1188789Sahrens 	if (dp == NULL)
1189789Sahrens 		return (FALSE);
1190789Sahrens 
1191789Sahrens 	if (dn->dn_free_txg)
1192789Sahrens 		return (TRUE);
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 
12789396SMatthew.Ahrens@Sun.COM 	hole = ((flags & DNODE_FIND_HOLE) != 0);
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) {
1325*9409SJonathan.Adams@Sun.COM 			if ((dnp[i].dn_type == DMU_OT_NONE) == hole)
1326789Sahrens 				break;
13276992Smaybee 			*offset += (1ULL << span) * inc;
1328789Sahrens 		}
13296992Smaybee 		if (i < 0 || i == blkfill)
1330789Sahrens 			error = ESRCH;
1331789Sahrens 	} else {
1332789Sahrens 		blkptr_t *bp = data;
1333789Sahrens 		span = (lvl - 1) * epbs + dn->dn_datablkshift;
1334789Sahrens 		minfill = 0;
1335789Sahrens 		maxfill = blkfill << ((lvl - 1) * epbs);
1336789Sahrens 
1337789Sahrens 		if (hole)
1338789Sahrens 			maxfill--;
1339789Sahrens 		else
1340789Sahrens 			minfill++;
1341789Sahrens 
1342789Sahrens 		for (i = (*offset >> span) & ((1ULL << epbs) - 1);
13436992Smaybee 		    i >= 0 && i < epb; i += inc) {
1344789Sahrens 			if (bp[i].blk_fill >= minfill &&
13453025Sahrens 			    bp[i].blk_fill <= maxfill &&
13467385SMark.Maybee@Sun.COM 			    (hole || bp[i].blk_birth > txg))
1347789Sahrens 				break;
13487385SMark.Maybee@Sun.COM 			if (inc < 0 && *offset < (1ULL << span))
13497385SMark.Maybee@Sun.COM 				*offset = 0;
13507385SMark.Maybee@Sun.COM 			else
13517385SMark.Maybee@Sun.COM 				*offset += (1ULL << span) * inc;
1352789Sahrens 		}
13536992Smaybee 		if (i < 0 || i == epb)
1354789Sahrens 			error = ESRCH;
1355789Sahrens 	}
1356789Sahrens 
1357789Sahrens 	if (db)
13581544Seschrock 		dbuf_rele(db, FTAG);
1359789Sahrens 
1360789Sahrens 	return (error);
1361789Sahrens }
1362789Sahrens 
1363789Sahrens /*
1364789Sahrens  * Find the next hole, data, or sparse region at or after *offset.
1365789Sahrens  * The value 'blkfill' tells us how many items we expect to find
1366789Sahrens  * in an L0 data block; this value is 1 for normal objects,
1367789Sahrens  * DNODES_PER_BLOCK for the meta dnode, and some fraction of
1368789Sahrens  * DNODES_PER_BLOCK when searching for sparse regions thereof.
13693025Sahrens  *
1370789Sahrens  * Examples:
1371789Sahrens  *
13726992Smaybee  * dnode_next_offset(dn, flags, offset, 1, 1, 0);
13736992Smaybee  *	Finds the next/previous hole/data in a file.
1374789Sahrens  *	Used in dmu_offset_next().
1375789Sahrens  *
13766992Smaybee  * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
1377789Sahrens  *	Finds the next free/allocated dnode an objset's meta-dnode.
13783025Sahrens  *	Only finds objects that have new contents since txg (ie.
13793025Sahrens  *	bonus buffer changes and content removal are ignored).
1380789Sahrens  *	Used in dmu_object_next().
1381789Sahrens  *
13826992Smaybee  * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
1383789Sahrens  *	Finds the next L2 meta-dnode bp that's at most 1/4 full.
1384789Sahrens  *	Used in dmu_object_alloc().
1385789Sahrens  */
1386789Sahrens int
13876992Smaybee dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset,
13883025Sahrens     int minlvl, uint64_t blkfill, uint64_t txg)
1389789Sahrens {
13906992Smaybee 	uint64_t initial_offset = *offset;
1391789Sahrens 	int lvl, maxlvl;
1392789Sahrens 	int error = 0;
1393789Sahrens 
13946992Smaybee 	if (!(flags & DNODE_FIND_HAVELOCK))
13956992Smaybee 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
1396789Sahrens 
1397789Sahrens 	if (dn->dn_phys->dn_nlevels == 0) {
13986992Smaybee 		error = ESRCH;
13996992Smaybee 		goto out;
1400789Sahrens 	}
1401789Sahrens 
1402789Sahrens 	if (dn->dn_datablkshift == 0) {
1403789Sahrens 		if (*offset < dn->dn_datablksz) {
14046992Smaybee 			if (flags & DNODE_FIND_HOLE)
1405789Sahrens 				*offset = dn->dn_datablksz;
1406789Sahrens 		} else {
1407789Sahrens 			error = ESRCH;
1408789Sahrens 		}
14096992Smaybee 		goto out;
1410789Sahrens 	}
1411789Sahrens 
1412789Sahrens 	maxlvl = dn->dn_phys->dn_nlevels;
1413789Sahrens 
1414789Sahrens 	for (lvl = minlvl; lvl <= maxlvl; lvl++) {
14153025Sahrens 		error = dnode_next_offset_level(dn,
14166992Smaybee 		    flags, offset, lvl, blkfill, txg);
14171793Sahrens 		if (error != ESRCH)
1418789Sahrens 			break;
1419789Sahrens 	}
1420789Sahrens 
14216992Smaybee 	while (error == 0 && --lvl >= minlvl) {
14223025Sahrens 		error = dnode_next_offset_level(dn,
14236992Smaybee 		    flags, offset, lvl, blkfill, txg);
14243025Sahrens 	}
1425789Sahrens 
14266992Smaybee 	if (error == 0 && (flags & DNODE_FIND_BACKWARDS ?
14276992Smaybee 	    initial_offset < *offset : initial_offset > *offset))
14281793Sahrens 		error = ESRCH;
14296992Smaybee out:
14306992Smaybee 	if (!(flags & DNODE_FIND_HAVELOCK))
14316992Smaybee 		rw_exit(&dn->dn_struct_rwlock);
1432789Sahrens 
1433789Sahrens 	return (error);
1434789Sahrens }
1435