xref: /onnv-gate/usr/src/uts/common/fs/zfs/dnode.c (revision 12178)
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*12178SMark.Shellenbaum@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23789Sahrens  */
24789Sahrens 
25789Sahrens #include <sys/zfs_context.h>
26789Sahrens #include <sys/dbuf.h>
27789Sahrens #include <sys/dnode.h>
28789Sahrens #include <sys/dmu.h>
29789Sahrens #include <sys/dmu_impl.h>
30789Sahrens #include <sys/dmu_tx.h>
31789Sahrens #include <sys/dmu_objset.h>
32789Sahrens #include <sys/dsl_dir.h>
33789Sahrens #include <sys/dsl_dataset.h>
34789Sahrens #include <sys/spa.h>
35789Sahrens #include <sys/zio.h>
36789Sahrens #include <sys/dmu_zfetch.h>
37789Sahrens 
38789Sahrens static int free_range_compar(const void *node1, const void *node2);
39789Sahrens 
40789Sahrens static kmem_cache_t *dnode_cache;
41789Sahrens 
42789Sahrens static dnode_phys_t dnode_phys_zero;
43789Sahrens 
44789Sahrens int zfs_default_bs = SPA_MINBLOCKSHIFT;
45789Sahrens int zfs_default_ibs = DN_MAX_INDBLKSHIFT;
46789Sahrens 
47789Sahrens /* ARGSUSED */
48789Sahrens static int
49789Sahrens dnode_cons(void *arg, void *unused, int kmflag)
50789Sahrens {
51789Sahrens 	int i;
52789Sahrens 	dnode_t *dn = arg;
53789Sahrens 	bzero(dn, sizeof (dnode_t));
54789Sahrens 
55789Sahrens 	rw_init(&dn->dn_struct_rwlock, NULL, RW_DEFAULT, NULL);
56789Sahrens 	mutex_init(&dn->dn_mtx, NULL, MUTEX_DEFAULT, NULL);
57789Sahrens 	mutex_init(&dn->dn_dbufs_mtx, NULL, MUTEX_DEFAULT, NULL);
588214SRicardo.M.Correia@Sun.COM 	cv_init(&dn->dn_notxholds, NULL, CV_DEFAULT, NULL);
598214SRicardo.M.Correia@Sun.COM 
60789Sahrens 	refcount_create(&dn->dn_holds);
61789Sahrens 	refcount_create(&dn->dn_tx_holds);
62789Sahrens 
63789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
64789Sahrens 		avl_create(&dn->dn_ranges[i], free_range_compar,
65789Sahrens 		    sizeof (free_range_t),
66789Sahrens 		    offsetof(struct free_range, fr_node));
673547Smaybee 		list_create(&dn->dn_dirty_records[i],
683547Smaybee 		    sizeof (dbuf_dirty_record_t),
693547Smaybee 		    offsetof(dbuf_dirty_record_t, dr_dirty_node));
70789Sahrens 	}
71789Sahrens 
72789Sahrens 	list_create(&dn->dn_dbufs, sizeof (dmu_buf_impl_t),
73789Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
74789Sahrens 
75789Sahrens 	return (0);
76789Sahrens }
77789Sahrens 
78789Sahrens /* ARGSUSED */
79789Sahrens static void
80789Sahrens dnode_dest(void *arg, void *unused)
81789Sahrens {
82789Sahrens 	int i;
83789Sahrens 	dnode_t *dn = arg;
84789Sahrens 
85789Sahrens 	rw_destroy(&dn->dn_struct_rwlock);
86789Sahrens 	mutex_destroy(&dn->dn_mtx);
87789Sahrens 	mutex_destroy(&dn->dn_dbufs_mtx);
888214SRicardo.M.Correia@Sun.COM 	cv_destroy(&dn->dn_notxholds);
89789Sahrens 	refcount_destroy(&dn->dn_holds);
90789Sahrens 	refcount_destroy(&dn->dn_tx_holds);
91789Sahrens 
92789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
93789Sahrens 		avl_destroy(&dn->dn_ranges[i]);
943547Smaybee 		list_destroy(&dn->dn_dirty_records[i]);
95789Sahrens 	}
96789Sahrens 
97789Sahrens 	list_destroy(&dn->dn_dbufs);
98789Sahrens }
99789Sahrens 
100789Sahrens void
101789Sahrens dnode_init(void)
102789Sahrens {
103789Sahrens 	dnode_cache = kmem_cache_create("dnode_t",
104789Sahrens 	    sizeof (dnode_t),
105789Sahrens 	    0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0);
106789Sahrens }
107789Sahrens 
108789Sahrens void
109789Sahrens dnode_fini(void)
110789Sahrens {
111789Sahrens 	kmem_cache_destroy(dnode_cache);
112789Sahrens }
113789Sahrens 
114789Sahrens 
115873Sek110237 #ifdef ZFS_DEBUG
116789Sahrens void
117789Sahrens dnode_verify(dnode_t *dn)
118789Sahrens {
119789Sahrens 	int drop_struct_lock = FALSE;
120789Sahrens 
121789Sahrens 	ASSERT(dn->dn_phys);
122789Sahrens 	ASSERT(dn->dn_objset);
123789Sahrens 
124789Sahrens 	ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES);
125789Sahrens 
126789Sahrens 	if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY))
127789Sahrens 		return;
128789Sahrens 
129789Sahrens 	if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
130789Sahrens 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
131789Sahrens 		drop_struct_lock = TRUE;
132789Sahrens 	}
133789Sahrens 	if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) {
134789Sahrens 		int i;
135789Sahrens 		ASSERT3U(dn->dn_indblkshift, >=, 0);
136789Sahrens 		ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT);
137789Sahrens 		if (dn->dn_datablkshift) {
138789Sahrens 			ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT);
139789Sahrens 			ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT);
140789Sahrens 			ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz);
141789Sahrens 		}
142789Sahrens 		ASSERT3U(dn->dn_nlevels, <=, 30);
143789Sahrens 		ASSERT3U(dn->dn_type, <=, DMU_OT_NUMTYPES);
144789Sahrens 		ASSERT3U(dn->dn_nblkptr, >=, 1);
145789Sahrens 		ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
146789Sahrens 		ASSERT3U(dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
147789Sahrens 		ASSERT3U(dn->dn_datablksz, ==,
148789Sahrens 		    dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
149789Sahrens 		ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0);
150789Sahrens 		ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) +
151789Sahrens 		    dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
152789Sahrens 		for (i = 0; i < TXG_SIZE; i++) {
153789Sahrens 			ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels);
154789Sahrens 		}
155789Sahrens 	}
156789Sahrens 	if (dn->dn_phys->dn_type != DMU_OT_NONE)
157789Sahrens 		ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels);
1589396SMatthew.Ahrens@Sun.COM 	ASSERT(DMU_OBJECT_IS_SPECIAL(dn->dn_object) || dn->dn_dbuf != NULL);
159789Sahrens 	if (dn->dn_dbuf != NULL) {
160789Sahrens 		ASSERT3P(dn->dn_phys, ==,
161789Sahrens 		    (dnode_phys_t *)dn->dn_dbuf->db.db_data +
162789Sahrens 		    (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT)));
163789Sahrens 	}
164789Sahrens 	if (drop_struct_lock)
165789Sahrens 		rw_exit(&dn->dn_struct_rwlock);
166873Sek110237 }
167789Sahrens #endif
168789Sahrens 
169789Sahrens void
170789Sahrens dnode_byteswap(dnode_phys_t *dnp)
171789Sahrens {
172789Sahrens 	uint64_t *buf64 = (void*)&dnp->dn_blkptr;
173789Sahrens 	int i;
174789Sahrens 
175789Sahrens 	if (dnp->dn_type == DMU_OT_NONE) {
176789Sahrens 		bzero(dnp, sizeof (dnode_phys_t));
177789Sahrens 		return;
178789Sahrens 	}
179789Sahrens 
180789Sahrens 	dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec);
181789Sahrens 	dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen);
182789Sahrens 	dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid);
1832082Seschrock 	dnp->dn_used = BSWAP_64(dnp->dn_used);
184789Sahrens 
185789Sahrens 	/*
186789Sahrens 	 * dn_nblkptr is only one byte, so it's OK to read it in either
187789Sahrens 	 * byte order.  We can't read dn_bouslen.
188789Sahrens 	 */
189789Sahrens 	ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT);
190789Sahrens 	ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR);
191789Sahrens 	for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++)
192789Sahrens 		buf64[i] = BSWAP_64(buf64[i]);
193789Sahrens 
194789Sahrens 	/*
195789Sahrens 	 * OK to check dn_bonuslen for zero, because it won't matter if
196789Sahrens 	 * we have the wrong byte order.  This is necessary because the
197789Sahrens 	 * dnode dnode is smaller than a regular dnode.
198789Sahrens 	 */
199789Sahrens 	if (dnp->dn_bonuslen != 0) {
200789Sahrens 		/*
201789Sahrens 		 * Note that the bonus length calculated here may be
202789Sahrens 		 * longer than the actual bonus buffer.  This is because
203789Sahrens 		 * we always put the bonus buffer after the last block
204789Sahrens 		 * pointer (instead of packing it against the end of the
205789Sahrens 		 * dnode buffer).
206789Sahrens 		 */
207789Sahrens 		int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t);
208789Sahrens 		size_t len = DN_MAX_BONUSLEN - off;
2093882Sahrens 		ASSERT3U(dnp->dn_bonustype, <, DMU_OT_NUMTYPES);
210789Sahrens 		dmu_ot[dnp->dn_bonustype].ot_byteswap(dnp->dn_bonus + off, len);
211789Sahrens 	}
21211935SMark.Shellenbaum@Sun.COM 
21311935SMark.Shellenbaum@Sun.COM 	/* Swap SPILL block if we have one */
21411935SMark.Shellenbaum@Sun.COM 	if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)
21511935SMark.Shellenbaum@Sun.COM 		byteswap_uint64_array(&dnp->dn_spill, sizeof (blkptr_t));
21611935SMark.Shellenbaum@Sun.COM 
217789Sahrens }
218789Sahrens 
219789Sahrens void
220789Sahrens dnode_buf_byteswap(void *vbuf, size_t size)
221789Sahrens {
222789Sahrens 	dnode_phys_t *buf = vbuf;
223789Sahrens 	int i;
224789Sahrens 
225789Sahrens 	ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT));
226789Sahrens 	ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0);
227789Sahrens 
228789Sahrens 	size >>= DNODE_SHIFT;
229789Sahrens 	for (i = 0; i < size; i++) {
230789Sahrens 		dnode_byteswap(buf);
231789Sahrens 		buf++;
232789Sahrens 	}
233789Sahrens }
234789Sahrens 
235789Sahrens static int
236789Sahrens free_range_compar(const void *node1, const void *node2)
237789Sahrens {
238789Sahrens 	const free_range_t *rp1 = node1;
239789Sahrens 	const free_range_t *rp2 = node2;
240789Sahrens 
241789Sahrens 	if (rp1->fr_blkid < rp2->fr_blkid)
242789Sahrens 		return (-1);
243789Sahrens 	else if (rp1->fr_blkid > rp2->fr_blkid)
244789Sahrens 		return (1);
245789Sahrens 	else return (0);
246789Sahrens }
247789Sahrens 
2484944Smaybee void
2494944Smaybee dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx)
2504944Smaybee {
2514944Smaybee 	ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
2524944Smaybee 
2534944Smaybee 	dnode_setdirty(dn, tx);
2544944Smaybee 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2554944Smaybee 	ASSERT3U(newsize, <=, DN_MAX_BONUSLEN -
2564944Smaybee 	    (dn->dn_nblkptr-1) * sizeof (blkptr_t));
2574944Smaybee 	dn->dn_bonuslen = newsize;
2584944Smaybee 	if (newsize == 0)
2594944Smaybee 		dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = DN_ZERO_BONUSLEN;
2604944Smaybee 	else
2614944Smaybee 		dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
2624944Smaybee 	rw_exit(&dn->dn_struct_rwlock);
2634944Smaybee }
2644944Smaybee 
26511935SMark.Shellenbaum@Sun.COM void
26611935SMark.Shellenbaum@Sun.COM dnode_setbonus_type(dnode_t *dn, dmu_object_type_t newtype, dmu_tx_t *tx)
26711935SMark.Shellenbaum@Sun.COM {
26811935SMark.Shellenbaum@Sun.COM 	ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
26911935SMark.Shellenbaum@Sun.COM 	dnode_setdirty(dn, tx);
27011935SMark.Shellenbaum@Sun.COM 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
27111935SMark.Shellenbaum@Sun.COM 	dn->dn_bonustype = newtype;
27211935SMark.Shellenbaum@Sun.COM 	dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
27311935SMark.Shellenbaum@Sun.COM 	rw_exit(&dn->dn_struct_rwlock);
27411935SMark.Shellenbaum@Sun.COM }
27511935SMark.Shellenbaum@Sun.COM 
27611935SMark.Shellenbaum@Sun.COM void
27711935SMark.Shellenbaum@Sun.COM dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx)
27811935SMark.Shellenbaum@Sun.COM {
27911935SMark.Shellenbaum@Sun.COM 	ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
280*12178SMark.Shellenbaum@Sun.COM 	ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
28111935SMark.Shellenbaum@Sun.COM 	dnode_setdirty(dn, tx);
28211935SMark.Shellenbaum@Sun.COM 	dn->dn_rm_spillblk[tx->tx_txg&TXG_MASK] = DN_KILL_SPILLBLK;
28311935SMark.Shellenbaum@Sun.COM 	dn->dn_have_spill = B_FALSE;
28411935SMark.Shellenbaum@Sun.COM }
28511935SMark.Shellenbaum@Sun.COM 
286789Sahrens static void
287789Sahrens dnode_setdblksz(dnode_t *dn, int size)
288789Sahrens {
289789Sahrens 	ASSERT3U(P2PHASE(size, SPA_MINBLOCKSIZE), ==, 0);
290789Sahrens 	ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
291789Sahrens 	ASSERT3U(size, >=, SPA_MINBLOCKSIZE);
292789Sahrens 	ASSERT3U(size >> SPA_MINBLOCKSHIFT, <,
293789Sahrens 	    1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8));
294789Sahrens 	dn->dn_datablksz = size;
295789Sahrens 	dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT;
296789Sahrens 	dn->dn_datablkshift = ISP2(size) ? highbit(size - 1) : 0;
297789Sahrens }
298789Sahrens 
299789Sahrens static dnode_t *
30010298SMatthew.Ahrens@Sun.COM dnode_create(objset_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db,
301789Sahrens     uint64_t object)
302789Sahrens {
303789Sahrens 	dnode_t *dn = kmem_cache_alloc(dnode_cache, KM_SLEEP);
304789Sahrens 	(void) dnode_cons(dn, NULL, 0); /* XXX */
305789Sahrens 
306789Sahrens 	dn->dn_objset = os;
307789Sahrens 	dn->dn_object = object;
308789Sahrens 	dn->dn_dbuf = db;
309789Sahrens 	dn->dn_phys = dnp;
310789Sahrens 
311789Sahrens 	if (dnp->dn_datablkszsec)
312789Sahrens 		dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
313789Sahrens 	dn->dn_indblkshift = dnp->dn_indblkshift;
314789Sahrens 	dn->dn_nlevels = dnp->dn_nlevels;
315789Sahrens 	dn->dn_type = dnp->dn_type;
316789Sahrens 	dn->dn_nblkptr = dnp->dn_nblkptr;
317789Sahrens 	dn->dn_checksum = dnp->dn_checksum;
318789Sahrens 	dn->dn_compress = dnp->dn_compress;
319789Sahrens 	dn->dn_bonustype = dnp->dn_bonustype;
320789Sahrens 	dn->dn_bonuslen = dnp->dn_bonuslen;
321789Sahrens 	dn->dn_maxblkid = dnp->dn_maxblkid;
32211935SMark.Shellenbaum@Sun.COM 	dn->dn_have_spill = ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0);
323*12178SMark.Shellenbaum@Sun.COM 	dn->dn_id_flags = 0;
324789Sahrens 
325789Sahrens 	dmu_zfetch_init(&dn->dn_zfetch, dn);
326789Sahrens 
327789Sahrens 	ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES);
328789Sahrens 	mutex_enter(&os->os_lock);
329789Sahrens 	list_insert_head(&os->os_dnodes, dn);
330789Sahrens 	mutex_exit(&os->os_lock);
331789Sahrens 
3328582SBrendan.Gregg@Sun.COM 	arc_space_consume(sizeof (dnode_t), ARC_SPACE_OTHER);
333789Sahrens 	return (dn);
334789Sahrens }
335789Sahrens 
336789Sahrens static void
337789Sahrens dnode_destroy(dnode_t *dn)
338789Sahrens {
33910298SMatthew.Ahrens@Sun.COM 	objset_t *os = dn->dn_objset;
340789Sahrens 
3412885Sahrens #ifdef ZFS_DEBUG
3422885Sahrens 	int i;
3432885Sahrens 
3442885Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
3452885Sahrens 		ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
3463547Smaybee 		ASSERT(NULL == list_head(&dn->dn_dirty_records[i]));
3472885Sahrens 		ASSERT(0 == avl_numnodes(&dn->dn_ranges[i]));
3482885Sahrens 	}
3492885Sahrens 	ASSERT(NULL == list_head(&dn->dn_dbufs));
3502885Sahrens #endif
35111935SMark.Shellenbaum@Sun.COM 	ASSERT((dn->dn_id_flags & DN_ID_NEW_EXIST) == 0);
3522885Sahrens 
353789Sahrens 	mutex_enter(&os->os_lock);
354789Sahrens 	list_remove(&os->os_dnodes, dn);
355789Sahrens 	mutex_exit(&os->os_lock);
356789Sahrens 
357789Sahrens 	if (dn->dn_dirtyctx_firstset) {
358789Sahrens 		kmem_free(dn->dn_dirtyctx_firstset, 1);
359789Sahrens 		dn->dn_dirtyctx_firstset = NULL;
360789Sahrens 	}
361789Sahrens 	dmu_zfetch_rele(&dn->dn_zfetch);
3621544Seschrock 	if (dn->dn_bonus) {
3631544Seschrock 		mutex_enter(&dn->dn_bonus->db_mtx);
3641544Seschrock 		dbuf_evict(dn->dn_bonus);
3651544Seschrock 		dn->dn_bonus = NULL;
3661544Seschrock 	}
367789Sahrens 	kmem_cache_free(dnode_cache, dn);
3688582SBrendan.Gregg@Sun.COM 	arc_space_return(sizeof (dnode_t), ARC_SPACE_OTHER);
369789Sahrens }
370789Sahrens 
371789Sahrens void
372789Sahrens dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
3731599Sahrens     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
374789Sahrens {
375789Sahrens 	int i;
376789Sahrens 
377789Sahrens 	if (blocksize == 0)
378789Sahrens 		blocksize = 1 << zfs_default_bs;
3791402Sahrens 	else if (blocksize > SPA_MAXBLOCKSIZE)
3801402Sahrens 		blocksize = SPA_MAXBLOCKSIZE;
3811402Sahrens 	else
3821402Sahrens 		blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE);
383789Sahrens 
384789Sahrens 	if (ibs == 0)
385789Sahrens 		ibs = zfs_default_ibs;
386789Sahrens 
387789Sahrens 	ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT);
388789Sahrens 
389789Sahrens 	dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset,
390789Sahrens 	    dn->dn_object, tx->tx_txg, blocksize, ibs);
391789Sahrens 
392789Sahrens 	ASSERT(dn->dn_type == DMU_OT_NONE);
393789Sahrens 	ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0);
394789Sahrens 	ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE);
395789Sahrens 	ASSERT(ot != DMU_OT_NONE);
396789Sahrens 	ASSERT3U(ot, <, DMU_OT_NUMTYPES);
397789Sahrens 	ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
39811935SMark.Shellenbaum@Sun.COM 	    (bonustype == DMU_OT_SA && bonuslen == 0) ||
399789Sahrens 	    (bonustype != DMU_OT_NONE && bonuslen != 0));
400789Sahrens 	ASSERT3U(bonustype, <, DMU_OT_NUMTYPES);
401789Sahrens 	ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
402789Sahrens 	ASSERT(dn->dn_type == DMU_OT_NONE);
403789Sahrens 	ASSERT3U(dn->dn_maxblkid, ==, 0);
404789Sahrens 	ASSERT3U(dn->dn_allocated_txg, ==, 0);
405789Sahrens 	ASSERT3U(dn->dn_assigned_txg, ==, 0);
406789Sahrens 	ASSERT(refcount_is_zero(&dn->dn_tx_holds));
407789Sahrens 	ASSERT3U(refcount_count(&dn->dn_holds), <=, 1);
408789Sahrens 	ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
409789Sahrens 
410789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
411789Sahrens 		ASSERT3U(dn->dn_next_nlevels[i], ==, 0);
412789Sahrens 		ASSERT3U(dn->dn_next_indblkshift[i], ==, 0);
4134944Smaybee 		ASSERT3U(dn->dn_next_bonuslen[i], ==, 0);
41411935SMark.Shellenbaum@Sun.COM 		ASSERT3U(dn->dn_next_bonustype[i], ==, 0);
41511935SMark.Shellenbaum@Sun.COM 		ASSERT3U(dn->dn_rm_spillblk[i], ==, 0);
4161596Sahrens 		ASSERT3U(dn->dn_next_blksz[i], ==, 0);
4171596Sahrens 		ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
4183547Smaybee 		ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL);
419789Sahrens 		ASSERT3U(avl_numnodes(&dn->dn_ranges[i]), ==, 0);
420789Sahrens 	}
421789Sahrens 
422789Sahrens 	dn->dn_type = ot;
423789Sahrens 	dnode_setdblksz(dn, blocksize);
424789Sahrens 	dn->dn_indblkshift = ibs;
425789Sahrens 	dn->dn_nlevels = 1;
42611935SMark.Shellenbaum@Sun.COM 	if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
42711935SMark.Shellenbaum@Sun.COM 		dn->dn_nblkptr = 1;
42811935SMark.Shellenbaum@Sun.COM 	else
42911935SMark.Shellenbaum@Sun.COM 		dn->dn_nblkptr = 1 +
43011935SMark.Shellenbaum@Sun.COM 		    ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
431789Sahrens 	dn->dn_bonustype = bonustype;
432789Sahrens 	dn->dn_bonuslen = bonuslen;
433789Sahrens 	dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
434789Sahrens 	dn->dn_compress = ZIO_COMPRESS_INHERIT;
435789Sahrens 	dn->dn_dirtyctx = 0;
436789Sahrens 
437789Sahrens 	dn->dn_free_txg = 0;
438789Sahrens 	if (dn->dn_dirtyctx_firstset) {
439789Sahrens 		kmem_free(dn->dn_dirtyctx_firstset, 1);
440789Sahrens 		dn->dn_dirtyctx_firstset = NULL;
441789Sahrens 	}
442789Sahrens 
443789Sahrens 	dn->dn_allocated_txg = tx->tx_txg;
44411935SMark.Shellenbaum@Sun.COM 	dn->dn_id_flags = 0;
4451599Sahrens 
446789Sahrens 	dnode_setdirty(dn, tx);
4471599Sahrens 	dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs;
4484944Smaybee 	dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
44911935SMark.Shellenbaum@Sun.COM 	dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
4501599Sahrens 	dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz;
451789Sahrens }
452789Sahrens 
453789Sahrens void
454789Sahrens dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
455789Sahrens     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
456789Sahrens {
4578986SMark.Maybee@Sun.COM 	int nblkptr;
4581596Sahrens 
459789Sahrens 	ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE);
460789Sahrens 	ASSERT3U(blocksize, <=, SPA_MAXBLOCKSIZE);
461789Sahrens 	ASSERT3U(blocksize % SPA_MINBLOCKSIZE, ==, 0);
4621544Seschrock 	ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
463789Sahrens 	ASSERT(tx->tx_txg != 0);
464789Sahrens 	ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
465*12178SMark.Shellenbaum@Sun.COM 	    (bonustype != DMU_OT_NONE && bonuslen != 0) ||
466*12178SMark.Shellenbaum@Sun.COM 	    (bonustype == DMU_OT_SA && bonuslen == 0));
467789Sahrens 	ASSERT3U(bonustype, <, DMU_OT_NUMTYPES);
468789Sahrens 	ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
4691596Sahrens 
4701544Seschrock 	/* clean up any unreferenced dbufs */
4714944Smaybee 	dnode_evict_dbufs(dn);
4721544Seschrock 
4738986SMark.Maybee@Sun.COM 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
4748986SMark.Maybee@Sun.COM 	dnode_setdirty(dn, tx);
4758986SMark.Maybee@Sun.COM 	if (dn->dn_datablksz != blocksize) {
4768986SMark.Maybee@Sun.COM 		/* change blocksize */
4778986SMark.Maybee@Sun.COM 		ASSERT(dn->dn_maxblkid == 0 &&
4788986SMark.Maybee@Sun.COM 		    (BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) ||
4798986SMark.Maybee@Sun.COM 		    dnode_block_freed(dn, 0)));
4808986SMark.Maybee@Sun.COM 		dnode_setdblksz(dn, blocksize);
4818986SMark.Maybee@Sun.COM 		dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize;
482789Sahrens 	}
4838986SMark.Maybee@Sun.COM 	if (dn->dn_bonuslen != bonuslen)
4848986SMark.Maybee@Sun.COM 		dn->dn_next_bonuslen[tx->tx_txg&TXG_MASK] = bonuslen;
485*12178SMark.Shellenbaum@Sun.COM 
486*12178SMark.Shellenbaum@Sun.COM 	if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
487*12178SMark.Shellenbaum@Sun.COM 		nblkptr = 1;
488*12178SMark.Shellenbaum@Sun.COM 	else
489*12178SMark.Shellenbaum@Sun.COM 		nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
49011935SMark.Shellenbaum@Sun.COM 	if (dn->dn_bonustype != bonustype)
49111935SMark.Shellenbaum@Sun.COM 		dn->dn_next_bonustype[tx->tx_txg&TXG_MASK] = bonustype;
4928644SMark.Maybee@Sun.COM 	if (dn->dn_nblkptr != nblkptr)
4938644SMark.Maybee@Sun.COM 		dn->dn_next_nblkptr[tx->tx_txg&TXG_MASK] = nblkptr;
49411935SMark.Shellenbaum@Sun.COM 	if (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
495*12178SMark.Shellenbaum@Sun.COM 		dbuf_rm_spill(dn, tx);
496*12178SMark.Shellenbaum@Sun.COM 		dnode_rm_spill(dn, tx);
49711935SMark.Shellenbaum@Sun.COM 	}
498789Sahrens 	rw_exit(&dn->dn_struct_rwlock);
499789Sahrens 
500789Sahrens 	/* change type */
501789Sahrens 	dn->dn_type = ot;
502789Sahrens 
503789Sahrens 	/* change bonus size and type */
504789Sahrens 	mutex_enter(&dn->dn_mtx);
505789Sahrens 	dn->dn_bonustype = bonustype;
506789Sahrens 	dn->dn_bonuslen = bonuslen;
5078644SMark.Maybee@Sun.COM 	dn->dn_nblkptr = nblkptr;
508789Sahrens 	dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
509789Sahrens 	dn->dn_compress = ZIO_COMPRESS_INHERIT;
510789Sahrens 	ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
511789Sahrens 
5128644SMark.Maybee@Sun.COM 	/* fix up the bonus db_size */
5138644SMark.Maybee@Sun.COM 	if (dn->dn_bonus) {
5144944Smaybee 		dn->dn_bonus->db.db_size =
5154944Smaybee 		    DN_MAX_BONUSLEN - (dn->dn_nblkptr-1) * sizeof (blkptr_t);
5164944Smaybee 		ASSERT(dn->dn_bonuslen <= dn->dn_bonus->db.db_size);
5174944Smaybee 	}
5183087Sahrens 
519789Sahrens 	dn->dn_allocated_txg = tx->tx_txg;
520789Sahrens 	mutex_exit(&dn->dn_mtx);
521789Sahrens }
522789Sahrens 
523789Sahrens void
524789Sahrens dnode_special_close(dnode_t *dn)
525789Sahrens {
5261544Seschrock 	/*
5271544Seschrock 	 * Wait for final references to the dnode to clear.  This can
5281544Seschrock 	 * only happen if the arc is asyncronously evicting state that
5291544Seschrock 	 * has a hold on this dnode while we are trying to evict this
5301544Seschrock 	 * dnode.
5311544Seschrock 	 */
5321544Seschrock 	while (refcount_count(&dn->dn_holds) > 0)
5331544Seschrock 		delay(1);
534789Sahrens 	dnode_destroy(dn);
535789Sahrens }
536789Sahrens 
537789Sahrens dnode_t *
53810298SMatthew.Ahrens@Sun.COM dnode_special_open(objset_t *os, dnode_phys_t *dnp, uint64_t object)
539789Sahrens {
540789Sahrens 	dnode_t *dn = dnode_create(os, dnp, NULL, object);
541873Sek110237 	DNODE_VERIFY(dn);
542789Sahrens 	return (dn);
543789Sahrens }
544789Sahrens 
545789Sahrens static void
546789Sahrens dnode_buf_pageout(dmu_buf_t *db, void *arg)
547789Sahrens {
548789Sahrens 	dnode_t **children_dnodes = arg;
549789Sahrens 	int i;
550789Sahrens 	int epb = db->db_size >> DNODE_SHIFT;
551789Sahrens 
552789Sahrens 	for (i = 0; i < epb; i++) {
553789Sahrens 		dnode_t *dn = children_dnodes[i];
554789Sahrens 		int n;
555789Sahrens 
556789Sahrens 		if (dn == NULL)
557789Sahrens 			continue;
558789Sahrens #ifdef ZFS_DEBUG
559789Sahrens 		/*
560789Sahrens 		 * If there are holds on this dnode, then there should
561789Sahrens 		 * be holds on the dnode's containing dbuf as well; thus
562789Sahrens 		 * it wouldn't be eligable for eviction and this function
563789Sahrens 		 * would not have been called.
564789Sahrens 		 */
565789Sahrens 		ASSERT(refcount_is_zero(&dn->dn_holds));
566789Sahrens 		ASSERT(list_head(&dn->dn_dbufs) == NULL);
567789Sahrens 		ASSERT(refcount_is_zero(&dn->dn_tx_holds));
568789Sahrens 
569789Sahrens 		for (n = 0; n < TXG_SIZE; n++)
5701596Sahrens 			ASSERT(!list_link_active(&dn->dn_dirty_link[n]));
571789Sahrens #endif
572789Sahrens 		children_dnodes[i] = NULL;
573789Sahrens 		dnode_destroy(dn);
574789Sahrens 	}
575789Sahrens 	kmem_free(children_dnodes, epb * sizeof (dnode_t *));
576789Sahrens }
577789Sahrens 
578789Sahrens /*
5791544Seschrock  * errors:
5801544Seschrock  * EINVAL - invalid object number.
5811544Seschrock  * EIO - i/o error.
5821544Seschrock  * succeeds even for free dnodes.
583789Sahrens  */
5841544Seschrock int
58510298SMatthew.Ahrens@Sun.COM dnode_hold_impl(objset_t *os, uint64_t object, int flag,
5861544Seschrock     void *tag, dnode_t **dnp)
587789Sahrens {
5881544Seschrock 	int epb, idx, err;
589789Sahrens 	int drop_struct_lock = FALSE;
5901544Seschrock 	int type;
591789Sahrens 	uint64_t blk;
592789Sahrens 	dnode_t *mdn, *dn;
593789Sahrens 	dmu_buf_impl_t *db;
594789Sahrens 	dnode_t **children_dnodes;
595789Sahrens 
5967754SJeff.Bonwick@Sun.COM 	/*
5977754SJeff.Bonwick@Sun.COM 	 * If you are holding the spa config lock as writer, you shouldn't
59811958SGeorge.Wilson@Sun.COM 	 * be asking the DMU to do *anything* unless it's the root pool
59911958SGeorge.Wilson@Sun.COM 	 * which may require us to read from the root filesystem while
60011958SGeorge.Wilson@Sun.COM 	 * holding some (not all) of the locks as writer.
6017754SJeff.Bonwick@Sun.COM 	 */
60211958SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(os->os_spa, SCL_ALL, RW_WRITER) == 0 ||
60311958SGeorge.Wilson@Sun.COM 	    (spa_is_root(os->os_spa) &&
60411958SGeorge.Wilson@Sun.COM 	    spa_config_held(os->os_spa, SCL_STATE, RW_WRITER) &&
60511958SGeorge.Wilson@Sun.COM 	    !spa_config_held(os->os_spa, SCL_ZIO, RW_WRITER)));
6067754SJeff.Bonwick@Sun.COM 
6079396SMatthew.Ahrens@Sun.COM 	if (object == DMU_USERUSED_OBJECT || object == DMU_GROUPUSED_OBJECT) {
6089396SMatthew.Ahrens@Sun.COM 		dn = (object == DMU_USERUSED_OBJECT) ?
6099396SMatthew.Ahrens@Sun.COM 		    os->os_userused_dnode : os->os_groupused_dnode;
6109396SMatthew.Ahrens@Sun.COM 		if (dn == NULL)
6119396SMatthew.Ahrens@Sun.COM 			return (ENOENT);
6129396SMatthew.Ahrens@Sun.COM 		type = dn->dn_type;
6139396SMatthew.Ahrens@Sun.COM 		if ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE)
6149396SMatthew.Ahrens@Sun.COM 			return (ENOENT);
6159396SMatthew.Ahrens@Sun.COM 		if ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)
6169396SMatthew.Ahrens@Sun.COM 			return (EEXIST);
6179396SMatthew.Ahrens@Sun.COM 		DNODE_VERIFY(dn);
6189396SMatthew.Ahrens@Sun.COM 		(void) refcount_add(&dn->dn_holds, tag);
6199396SMatthew.Ahrens@Sun.COM 		*dnp = dn;
6209396SMatthew.Ahrens@Sun.COM 		return (0);
6219396SMatthew.Ahrens@Sun.COM 	}
6229396SMatthew.Ahrens@Sun.COM 
623789Sahrens 	if (object == 0 || object >= DN_MAX_OBJECT)
6241544Seschrock 		return (EINVAL);
625789Sahrens 
626789Sahrens 	mdn = os->os_meta_dnode;
627789Sahrens 
628873Sek110237 	DNODE_VERIFY(mdn);
629789Sahrens 
630789Sahrens 	if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) {
631789Sahrens 		rw_enter(&mdn->dn_struct_rwlock, RW_READER);
632789Sahrens 		drop_struct_lock = TRUE;
633789Sahrens 	}
634789Sahrens 
635789Sahrens 	blk = dbuf_whichblock(mdn, object * sizeof (dnode_phys_t));
636789Sahrens 
6371544Seschrock 	db = dbuf_hold(mdn, blk, FTAG);
638789Sahrens 	if (drop_struct_lock)
639789Sahrens 		rw_exit(&mdn->dn_struct_rwlock);
6401544Seschrock 	if (db == NULL)
6411544Seschrock 		return (EIO);
6421544Seschrock 	err = dbuf_read(db, NULL, DB_RF_CANFAIL);
6431544Seschrock 	if (err) {
6441544Seschrock 		dbuf_rele(db, FTAG);
6451544Seschrock 		return (err);
6461544Seschrock 	}
647789Sahrens 
648789Sahrens 	ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT);
649789Sahrens 	epb = db->db.db_size >> DNODE_SHIFT;
650789Sahrens 
651789Sahrens 	idx = object & (epb-1);
652789Sahrens 
653789Sahrens 	children_dnodes = dmu_buf_get_user(&db->db);
654789Sahrens 	if (children_dnodes == NULL) {
655789Sahrens 		dnode_t **winner;
656789Sahrens 		children_dnodes = kmem_zalloc(epb * sizeof (dnode_t *),
657789Sahrens 		    KM_SLEEP);
658789Sahrens 		if (winner = dmu_buf_set_user(&db->db, children_dnodes, NULL,
659789Sahrens 		    dnode_buf_pageout)) {
660789Sahrens 			kmem_free(children_dnodes, epb * sizeof (dnode_t *));
661789Sahrens 			children_dnodes = winner;
662789Sahrens 		}
663789Sahrens 	}
664789Sahrens 
665789Sahrens 	if ((dn = children_dnodes[idx]) == NULL) {
6664309Smaybee 		dnode_phys_t *dnp = (dnode_phys_t *)db->db.db_data+idx;
667789Sahrens 		dnode_t *winner;
6684309Smaybee 
6694309Smaybee 		dn = dnode_create(os, dnp, db, object);
670789Sahrens 		winner = atomic_cas_ptr(&children_dnodes[idx], NULL, dn);
671789Sahrens 		if (winner != NULL) {
672789Sahrens 			dnode_destroy(dn);
673789Sahrens 			dn = winner;
674789Sahrens 		}
675789Sahrens 	}
676789Sahrens 
677789Sahrens 	mutex_enter(&dn->dn_mtx);
6781544Seschrock 	type = dn->dn_type;
679789Sahrens 	if (dn->dn_free_txg ||
6801544Seschrock 	    ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) ||
6819396SMatthew.Ahrens@Sun.COM 	    ((flag & DNODE_MUST_BE_FREE) &&
68211935SMark.Shellenbaum@Sun.COM 	    (type != DMU_OT_NONE || (dn->dn_id_flags & DN_ID_SYNC)))) {
683789Sahrens 		mutex_exit(&dn->dn_mtx);
6841544Seschrock 		dbuf_rele(db, FTAG);
6851544Seschrock 		return (type == DMU_OT_NONE ? ENOENT : EEXIST);
686789Sahrens 	}
68711935SMark.Shellenbaum@Sun.COM 	if (flag & DNODE_MUST_BE_FREE) {
68811935SMark.Shellenbaum@Sun.COM 		ASSERT(refcount_is_zero(&dn->dn_holds));
68911935SMark.Shellenbaum@Sun.COM 		ASSERT(!(dn->dn_id_flags & DN_ID_SYNC));
69011935SMark.Shellenbaum@Sun.COM 	}
691789Sahrens 	mutex_exit(&dn->dn_mtx);
692789Sahrens 
6931544Seschrock 	if (refcount_add(&dn->dn_holds, tag) == 1)
694789Sahrens 		dbuf_add_ref(db, dn);
695789Sahrens 
696873Sek110237 	DNODE_VERIFY(dn);
697789Sahrens 	ASSERT3P(dn->dn_dbuf, ==, db);
698789Sahrens 	ASSERT3U(dn->dn_object, ==, object);
6991544Seschrock 	dbuf_rele(db, FTAG);
700789Sahrens 
7011544Seschrock 	*dnp = dn;
7021544Seschrock 	return (0);
703789Sahrens }
704789Sahrens 
705789Sahrens /*
706789Sahrens  * Return held dnode if the object is allocated, NULL if not.
707789Sahrens  */
7081544Seschrock int
70910298SMatthew.Ahrens@Sun.COM dnode_hold(objset_t *os, uint64_t object, void *tag, dnode_t **dnp)
710789Sahrens {
7111544Seschrock 	return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, tag, dnp));
712789Sahrens }
713789Sahrens 
7144944Smaybee /*
7154944Smaybee  * Can only add a reference if there is already at least one
7164944Smaybee  * reference on the dnode.  Returns FALSE if unable to add a
7174944Smaybee  * new reference.
7184944Smaybee  */
7194944Smaybee boolean_t
7201544Seschrock dnode_add_ref(dnode_t *dn, void *tag)
721789Sahrens {
7224944Smaybee 	mutex_enter(&dn->dn_mtx);
7234944Smaybee 	if (refcount_is_zero(&dn->dn_holds)) {
7244944Smaybee 		mutex_exit(&dn->dn_mtx);
7254944Smaybee 		return (FALSE);
7264944Smaybee 	}
7274944Smaybee 	VERIFY(1 < refcount_add(&dn->dn_holds, tag));
7284944Smaybee 	mutex_exit(&dn->dn_mtx);
7294944Smaybee 	return (TRUE);
730789Sahrens }
731789Sahrens 
732789Sahrens void
7331544Seschrock dnode_rele(dnode_t *dn, void *tag)
734789Sahrens {
735789Sahrens 	uint64_t refs;
736789Sahrens 
7374944Smaybee 	mutex_enter(&dn->dn_mtx);
7381544Seschrock 	refs = refcount_remove(&dn->dn_holds, tag);
7394944Smaybee 	mutex_exit(&dn->dn_mtx);
740789Sahrens 	/* NOTE: the DNODE_DNODE does not have a dn_dbuf */
741789Sahrens 	if (refs == 0 && dn->dn_dbuf)
7421544Seschrock 		dbuf_rele(dn->dn_dbuf, dn);
743789Sahrens }
744789Sahrens 
745789Sahrens void
746789Sahrens dnode_setdirty(dnode_t *dn, dmu_tx_t *tx)
747789Sahrens {
74810298SMatthew.Ahrens@Sun.COM 	objset_t *os = dn->dn_objset;
749789Sahrens 	uint64_t txg = tx->tx_txg;
750789Sahrens 
7519396SMatthew.Ahrens@Sun.COM 	if (DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
7529396SMatthew.Ahrens@Sun.COM 		dsl_dataset_dirty(os->os_dsl_dataset, tx);
753789Sahrens 		return;
7549396SMatthew.Ahrens@Sun.COM 	}
755789Sahrens 
756873Sek110237 	DNODE_VERIFY(dn);
757789Sahrens 
758789Sahrens #ifdef ZFS_DEBUG
759789Sahrens 	mutex_enter(&dn->dn_mtx);
760789Sahrens 	ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg);
761789Sahrens 	/* ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg); */
762789Sahrens 	mutex_exit(&dn->dn_mtx);
763789Sahrens #endif
764789Sahrens 
76511935SMark.Shellenbaum@Sun.COM 	/*
76611935SMark.Shellenbaum@Sun.COM 	 * Determine old uid/gid when necessary
76711935SMark.Shellenbaum@Sun.COM 	 */
768*12178SMark.Shellenbaum@Sun.COM 	dmu_objset_userquota_get_ids(dn, B_TRUE, tx);
76911935SMark.Shellenbaum@Sun.COM 
770789Sahrens 	mutex_enter(&os->os_lock);
771789Sahrens 
772789Sahrens 	/*
773789Sahrens 	 * If we are already marked dirty, we're done.
774789Sahrens 	 */
7751596Sahrens 	if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) {
776789Sahrens 		mutex_exit(&os->os_lock);
777789Sahrens 		return;
778789Sahrens 	}
779789Sahrens 
780789Sahrens 	ASSERT(!refcount_is_zero(&dn->dn_holds) || list_head(&dn->dn_dbufs));
781789Sahrens 	ASSERT(dn->dn_datablksz != 0);
7824944Smaybee 	ASSERT3U(dn->dn_next_bonuslen[txg&TXG_MASK], ==, 0);
7831599Sahrens 	ASSERT3U(dn->dn_next_blksz[txg&TXG_MASK], ==, 0);
78411935SMark.Shellenbaum@Sun.COM 	ASSERT3U(dn->dn_next_bonustype[txg&TXG_MASK], ==, 0);
785789Sahrens 
786789Sahrens 	dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
787789Sahrens 	    dn->dn_object, txg);
788789Sahrens 
789789Sahrens 	if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) {
790789Sahrens 		list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn);
791789Sahrens 	} else {
792789Sahrens 		list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn);
793789Sahrens 	}
794789Sahrens 
795789Sahrens 	mutex_exit(&os->os_lock);
796789Sahrens 
797789Sahrens 	/*
798789Sahrens 	 * The dnode maintains a hold on its containing dbuf as
799789Sahrens 	 * long as there are holds on it.  Each instantiated child
800789Sahrens 	 * dbuf maintaines a hold on the dnode.  When the last child
801789Sahrens 	 * drops its hold, the dnode will drop its hold on the
802789Sahrens 	 * containing dbuf. We add a "dirty hold" here so that the
803789Sahrens 	 * dnode will hang around after we finish processing its
804789Sahrens 	 * children.
805789Sahrens 	 */
8064944Smaybee 	VERIFY(dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg));
807789Sahrens 
8083547Smaybee 	(void) dbuf_dirty(dn->dn_dbuf, tx);
809789Sahrens 
810789Sahrens 	dsl_dataset_dirty(os->os_dsl_dataset, tx);
811789Sahrens }
812789Sahrens 
813789Sahrens void
814789Sahrens dnode_free(dnode_t *dn, dmu_tx_t *tx)
815789Sahrens {
8161596Sahrens 	int txgoff = tx->tx_txg & TXG_MASK;
8171596Sahrens 
818789Sahrens 	dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg);
819789Sahrens 
820789Sahrens 	/* we should be the only holder... hopefully */
821789Sahrens 	/* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */
822789Sahrens 
823789Sahrens 	mutex_enter(&dn->dn_mtx);
824789Sahrens 	if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) {
825789Sahrens 		mutex_exit(&dn->dn_mtx);
826789Sahrens 		return;
827789Sahrens 	}
828789Sahrens 	dn->dn_free_txg = tx->tx_txg;
829789Sahrens 	mutex_exit(&dn->dn_mtx);
830789Sahrens 
831789Sahrens 	/*
832789Sahrens 	 * If the dnode is already dirty, it needs to be moved from
833789Sahrens 	 * the dirty list to the free list.
834789Sahrens 	 */
835789Sahrens 	mutex_enter(&dn->dn_objset->os_lock);
8361596Sahrens 	if (list_link_active(&dn->dn_dirty_link[txgoff])) {
8371596Sahrens 		list_remove(&dn->dn_objset->os_dirty_dnodes[txgoff], dn);
8381596Sahrens 		list_insert_tail(&dn->dn_objset->os_free_dnodes[txgoff], dn);
839789Sahrens 		mutex_exit(&dn->dn_objset->os_lock);
840789Sahrens 	} else {
841789Sahrens 		mutex_exit(&dn->dn_objset->os_lock);
842789Sahrens 		dnode_setdirty(dn, tx);
843789Sahrens 	}
844789Sahrens }
845789Sahrens 
846789Sahrens /*
847789Sahrens  * Try to change the block size for the indicated dnode.  This can only
848789Sahrens  * succeed if there are no blocks allocated or dirty beyond first block
849789Sahrens  */
850789Sahrens int
851789Sahrens dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx)
852789Sahrens {
853789Sahrens 	dmu_buf_impl_t *db, *db_next;
8546992Smaybee 	int err;
855789Sahrens 
856789Sahrens 	if (size == 0)
857789Sahrens 		size = SPA_MINBLOCKSIZE;
858789Sahrens 	if (size > SPA_MAXBLOCKSIZE)
859789Sahrens 		size = SPA_MAXBLOCKSIZE;
860789Sahrens 	else
861789Sahrens 		size = P2ROUNDUP(size, SPA_MINBLOCKSIZE);
862789Sahrens 
8632445Sahrens 	if (ibs == dn->dn_indblkshift)
8642445Sahrens 		ibs = 0;
865789Sahrens 
8662445Sahrens 	if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0)
867789Sahrens 		return (0);
868789Sahrens 
869789Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
870789Sahrens 
871789Sahrens 	/* Check for any allocated blocks beyond the first */
872789Sahrens 	if (dn->dn_phys->dn_maxblkid != 0)
8732445Sahrens 		goto fail;
874789Sahrens 
875789Sahrens 	mutex_enter(&dn->dn_dbufs_mtx);
876789Sahrens 	for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
877789Sahrens 		db_next = list_next(&dn->dn_dbufs, db);
878789Sahrens 
87911935SMark.Shellenbaum@Sun.COM 		if (db->db_blkid != 0 && db->db_blkid != DMU_BONUS_BLKID &&
88011935SMark.Shellenbaum@Sun.COM 		    db->db_blkid != DMU_SPILL_BLKID) {
881789Sahrens 			mutex_exit(&dn->dn_dbufs_mtx);
8822445Sahrens 			goto fail;
883789Sahrens 		}
884789Sahrens 	}
885789Sahrens 	mutex_exit(&dn->dn_dbufs_mtx);
886789Sahrens 
8872445Sahrens 	if (ibs && dn->dn_nlevels != 1)
8882445Sahrens 		goto fail;
8892445Sahrens 
8906992Smaybee 	/* resize the old block */
8916992Smaybee 	err = dbuf_hold_impl(dn, 0, 0, TRUE, FTAG, &db);
8926992Smaybee 	if (err == 0)
8931596Sahrens 		dbuf_new_size(db, size, tx);
8946992Smaybee 	else if (err != ENOENT)
8956992Smaybee 		goto fail;
896789Sahrens 
897789Sahrens 	dnode_setdblksz(dn, size);
8981596Sahrens 	dnode_setdirty(dn, tx);
8991596Sahrens 	dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size;
9002445Sahrens 	if (ibs) {
9012445Sahrens 		dn->dn_indblkshift = ibs;
9022445Sahrens 		dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs;
9032445Sahrens 	}
9046992Smaybee 	/* rele after we have fixed the blocksize in the dnode */
9051596Sahrens 	if (db)
9061596Sahrens 		dbuf_rele(db, FTAG);
907789Sahrens 
908789Sahrens 	rw_exit(&dn->dn_struct_rwlock);
9092445Sahrens 	return (0);
9102445Sahrens 
9112445Sahrens fail:
9122445Sahrens 	rw_exit(&dn->dn_struct_rwlock);
9132445Sahrens 	return (ENOTSUP);
914789Sahrens }
915789Sahrens 
9167332SJonathan.Adams@Sun.COM /* read-holding callers must not rely on the lock being continuously held */
917789Sahrens void
9187332SJonathan.Adams@Sun.COM dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t have_read)
919789Sahrens {
920789Sahrens 	uint64_t txgoff = tx->tx_txg & TXG_MASK;
9211596Sahrens 	int epbs, new_nlevels;
922789Sahrens 	uint64_t sz;
923789Sahrens 
92411935SMark.Shellenbaum@Sun.COM 	ASSERT(blkid != DMU_BONUS_BLKID);
925789Sahrens 
9267332SJonathan.Adams@Sun.COM 	ASSERT(have_read ?
9277332SJonathan.Adams@Sun.COM 	    RW_READ_HELD(&dn->dn_struct_rwlock) :
9287332SJonathan.Adams@Sun.COM 	    RW_WRITE_HELD(&dn->dn_struct_rwlock));
9297332SJonathan.Adams@Sun.COM 
9307332SJonathan.Adams@Sun.COM 	/*
9317332SJonathan.Adams@Sun.COM 	 * if we have a read-lock, check to see if we need to do any work
9327332SJonathan.Adams@Sun.COM 	 * before upgrading to a write-lock.
9337332SJonathan.Adams@Sun.COM 	 */
9347332SJonathan.Adams@Sun.COM 	if (have_read) {
9357332SJonathan.Adams@Sun.COM 		if (blkid <= dn->dn_maxblkid)
9367332SJonathan.Adams@Sun.COM 			return;
9377332SJonathan.Adams@Sun.COM 
9387332SJonathan.Adams@Sun.COM 		if (!rw_tryupgrade(&dn->dn_struct_rwlock)) {
9397332SJonathan.Adams@Sun.COM 			rw_exit(&dn->dn_struct_rwlock);
9407332SJonathan.Adams@Sun.COM 			rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
9417332SJonathan.Adams@Sun.COM 		}
942789Sahrens 	}
943789Sahrens 
9441596Sahrens 	if (blkid <= dn->dn_maxblkid)
9451596Sahrens 		goto out;
9461596Sahrens 
9471596Sahrens 	dn->dn_maxblkid = blkid;
948789Sahrens 
949789Sahrens 	/*
9501596Sahrens 	 * Compute the number of levels necessary to support the new maxblkid.
951789Sahrens 	 */
952789Sahrens 	new_nlevels = 1;
953789Sahrens 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
9541596Sahrens 	for (sz = dn->dn_nblkptr;
9551596Sahrens 	    sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs)
956789Sahrens 		new_nlevels++;
957789Sahrens 
9581596Sahrens 	if (new_nlevels > dn->dn_nlevels) {
9591596Sahrens 		int old_nlevels = dn->dn_nlevels;
9601596Sahrens 		dmu_buf_impl_t *db;
9613547Smaybee 		list_t *list;
9623547Smaybee 		dbuf_dirty_record_t *new, *dr, *dr_next;
9631596Sahrens 
9641596Sahrens 		dn->dn_nlevels = new_nlevels;
9651596Sahrens 
9661596Sahrens 		ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]);
967789Sahrens 		dn->dn_next_nlevels[txgoff] = new_nlevels;
968789Sahrens 
9693547Smaybee 		/* dirty the left indirects */
9701596Sahrens 		db = dbuf_hold_level(dn, old_nlevels, 0, FTAG);
9713547Smaybee 		new = dbuf_dirty(db, tx);
9721544Seschrock 		dbuf_rele(db, FTAG);
9731596Sahrens 
9743547Smaybee 		/* transfer the dirty records to the new indirect */
9753547Smaybee 		mutex_enter(&dn->dn_mtx);
9763547Smaybee 		mutex_enter(&new->dt.di.dr_mtx);
9773547Smaybee 		list = &dn->dn_dirty_records[txgoff];
9783547Smaybee 		for (dr = list_head(list); dr; dr = dr_next) {
9793547Smaybee 			dr_next = list_next(&dn->dn_dirty_records[txgoff], dr);
9803547Smaybee 			if (dr->dr_dbuf->db_level != new_nlevels-1 &&
98111935SMark.Shellenbaum@Sun.COM 			    dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
98211935SMark.Shellenbaum@Sun.COM 			    dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
9833547Smaybee 				ASSERT(dr->dr_dbuf->db_level == old_nlevels-1);
9843547Smaybee 				list_remove(&dn->dn_dirty_records[txgoff], dr);
9853547Smaybee 				list_insert_tail(&new->dt.di.dr_children, dr);
9863547Smaybee 				dr->dr_parent = new;
9873547Smaybee 			}
9883547Smaybee 		}
9893547Smaybee 		mutex_exit(&new->dt.di.dr_mtx);
9903547Smaybee 		mutex_exit(&dn->dn_mtx);
991789Sahrens 	}
992789Sahrens 
993789Sahrens out:
9947332SJonathan.Adams@Sun.COM 	if (have_read)
9957332SJonathan.Adams@Sun.COM 		rw_downgrade(&dn->dn_struct_rwlock);
996789Sahrens }
997789Sahrens 
998789Sahrens void
999789Sahrens dnode_clear_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
1000789Sahrens {
1001789Sahrens 	avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
1002789Sahrens 	avl_index_t where;
1003789Sahrens 	free_range_t *rp;
1004789Sahrens 	free_range_t rp_tofind;
1005789Sahrens 	uint64_t endblk = blkid + nblks;
1006789Sahrens 
1007789Sahrens 	ASSERT(MUTEX_HELD(&dn->dn_mtx));
1008789Sahrens 	ASSERT(nblks <= UINT64_MAX - blkid); /* no overflow */
1009789Sahrens 
1010789Sahrens 	dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1011789Sahrens 	    blkid, nblks, tx->tx_txg);
1012789Sahrens 	rp_tofind.fr_blkid = blkid;
1013789Sahrens 	rp = avl_find(tree, &rp_tofind, &where);
1014789Sahrens 	if (rp == NULL)
1015789Sahrens 		rp = avl_nearest(tree, where, AVL_BEFORE);
1016789Sahrens 	if (rp == NULL)
1017789Sahrens 		rp = avl_nearest(tree, where, AVL_AFTER);
1018789Sahrens 
1019789Sahrens 	while (rp && (rp->fr_blkid <= blkid + nblks)) {
1020789Sahrens 		uint64_t fr_endblk = rp->fr_blkid + rp->fr_nblks;
1021789Sahrens 		free_range_t *nrp = AVL_NEXT(tree, rp);
1022789Sahrens 
1023789Sahrens 		if (blkid <= rp->fr_blkid && endblk >= fr_endblk) {
1024789Sahrens 			/* clear this entire range */
1025789Sahrens 			avl_remove(tree, rp);
1026789Sahrens 			kmem_free(rp, sizeof (free_range_t));
1027789Sahrens 		} else if (blkid <= rp->fr_blkid &&
1028789Sahrens 		    endblk > rp->fr_blkid && endblk < fr_endblk) {
1029789Sahrens 			/* clear the beginning of this range */
1030789Sahrens 			rp->fr_blkid = endblk;
1031789Sahrens 			rp->fr_nblks = fr_endblk - endblk;
1032789Sahrens 		} else if (blkid > rp->fr_blkid && blkid < fr_endblk &&
1033789Sahrens 		    endblk >= fr_endblk) {
1034789Sahrens 			/* clear the end of this range */
1035789Sahrens 			rp->fr_nblks = blkid - rp->fr_blkid;
1036789Sahrens 		} else if (blkid > rp->fr_blkid && endblk < fr_endblk) {
1037789Sahrens 			/* clear a chunk out of this range */
1038789Sahrens 			free_range_t *new_rp =
1039789Sahrens 			    kmem_alloc(sizeof (free_range_t), KM_SLEEP);
1040789Sahrens 
1041789Sahrens 			new_rp->fr_blkid = endblk;
1042789Sahrens 			new_rp->fr_nblks = fr_endblk - endblk;
1043789Sahrens 			avl_insert_here(tree, new_rp, rp, AVL_AFTER);
1044789Sahrens 			rp->fr_nblks = blkid - rp->fr_blkid;
1045789Sahrens 		}
1046789Sahrens 		/* there may be no overlap */
1047789Sahrens 		rp = nrp;
1048789Sahrens 	}
1049789Sahrens }
1050789Sahrens 
1051789Sahrens void
1052789Sahrens dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx)
1053789Sahrens {
1054789Sahrens 	dmu_buf_impl_t *db;
10552445Sahrens 	uint64_t blkoff, blkid, nblks;
10566992Smaybee 	int blksz, blkshift, head, tail;
1057789Sahrens 	int trunc = FALSE;
10586992Smaybee 	int epbs;
1059789Sahrens 
1060789Sahrens 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1061789Sahrens 	blksz = dn->dn_datablksz;
10626992Smaybee 	blkshift = dn->dn_datablkshift;
10636992Smaybee 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1064789Sahrens 
1065789Sahrens 	if (len == -1ULL) {
1066789Sahrens 		len = UINT64_MAX - off;
1067789Sahrens 		trunc = TRUE;
1068789Sahrens 	}
1069789Sahrens 
1070789Sahrens 	/*
1071789Sahrens 	 * First, block align the region to free:
1072789Sahrens 	 */
10732445Sahrens 	if (ISP2(blksz)) {
10742445Sahrens 		head = P2NPHASE(off, blksz);
10752445Sahrens 		blkoff = P2PHASE(off, blksz);
10766992Smaybee 		if ((off >> blkshift) > dn->dn_maxblkid)
10776992Smaybee 			goto out;
10782445Sahrens 	} else {
10792445Sahrens 		ASSERT(dn->dn_maxblkid == 0);
10802445Sahrens 		if (off == 0 && len >= blksz) {
10816992Smaybee 			/* Freeing the whole block; fast-track this request */
10826992Smaybee 			blkid = 0;
10836992Smaybee 			nblks = 1;
10846992Smaybee 			goto done;
10857385SMark.Maybee@Sun.COM 		} else if (off >= blksz) {
10866992Smaybee 			/* Freeing past end-of-data */
10876992Smaybee 			goto out;
1088789Sahrens 		} else {
10892445Sahrens 			/* Freeing part of the block. */
1090789Sahrens 			head = blksz - off;
1091789Sahrens 			ASSERT3U(head, >, 0);
1092789Sahrens 		}
10932445Sahrens 		blkoff = off;
1094789Sahrens 	}
1095789Sahrens 	/* zero out any partial block data at the start of the range */
1096789Sahrens 	if (head) {
10972445Sahrens 		ASSERT3U(blkoff + head, ==, blksz);
1098789Sahrens 		if (len < head)
1099789Sahrens 			head = len;
1100789Sahrens 		if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off), TRUE,
1101789Sahrens 		    FTAG, &db) == 0) {
1102789Sahrens 			caddr_t data;
1103789Sahrens 
1104789Sahrens 			/* don't dirty if it isn't on disk and isn't dirty */
11053547Smaybee 			if (db->db_last_dirty ||
1106789Sahrens 			    (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1107789Sahrens 				rw_exit(&dn->dn_struct_rwlock);
1108789Sahrens 				dbuf_will_dirty(db, tx);
1109789Sahrens 				rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1110789Sahrens 				data = db->db.db_data;
11112445Sahrens 				bzero(data + blkoff, head);
1112789Sahrens 			}
11131544Seschrock 			dbuf_rele(db, FTAG);
1114789Sahrens 		}
1115789Sahrens 		off += head;
1116789Sahrens 		len -= head;
1117789Sahrens 	}
1118789Sahrens 
11192445Sahrens 	/* If the range was less than one block, we're done */
11206992Smaybee 	if (len == 0)
11216992Smaybee 		goto out;
11226992Smaybee 
11236992Smaybee 	/* If the remaining range is past end of file, we're done */
11246992Smaybee 	if ((off >> blkshift) > dn->dn_maxblkid)
11256992Smaybee 		goto out;
11266992Smaybee 
11277385SMark.Maybee@Sun.COM 	ASSERT(ISP2(blksz));
11286992Smaybee 	if (trunc)
11296992Smaybee 		tail = 0;
11306992Smaybee 	else
11316992Smaybee 		tail = P2PHASE(len, blksz);
11326992Smaybee 
11336992Smaybee 	ASSERT3U(P2PHASE(off, blksz), ==, 0);
11346992Smaybee 	/* zero out any partial block data at the end of the range */
11356992Smaybee 	if (tail) {
11366992Smaybee 		if (len < tail)
11376992Smaybee 			tail = len;
11386992Smaybee 		if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len),
11396992Smaybee 		    TRUE, FTAG, &db) == 0) {
11406992Smaybee 			/* don't dirty if not on disk and not dirty */
11416992Smaybee 			if (db->db_last_dirty ||
11426992Smaybee 			    (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
11436992Smaybee 				rw_exit(&dn->dn_struct_rwlock);
11446992Smaybee 				dbuf_will_dirty(db, tx);
11456992Smaybee 				rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
11466992Smaybee 				bzero(db->db.db_data, tail);
11476992Smaybee 			}
11486992Smaybee 			dbuf_rele(db, FTAG);
11496992Smaybee 		}
11506992Smaybee 		len -= tail;
11516992Smaybee 	}
11526992Smaybee 
11536992Smaybee 	/* If the range did not include a full block, we are done */
11546992Smaybee 	if (len == 0)
1155789Sahrens 		goto out;
1156789Sahrens 
11576992Smaybee 	ASSERT(IS_P2ALIGNED(off, blksz));
11586992Smaybee 	ASSERT(trunc || IS_P2ALIGNED(len, blksz));
11596992Smaybee 	blkid = off >> blkshift;
11606992Smaybee 	nblks = len >> blkshift;
11616992Smaybee 	if (trunc)
11626992Smaybee 		nblks += 1;
11632445Sahrens 
11646992Smaybee 	/*
11656992Smaybee 	 * Read in and mark all the level-1 indirects dirty,
11666992Smaybee 	 * so that they will stay in memory until syncing phase.
11677049Smaybee 	 * Always dirty the first and last indirect to make sure
11687049Smaybee 	 * we dirty all the partial indirects.
11696992Smaybee 	 */
11706992Smaybee 	if (dn->dn_nlevels > 1) {
11716992Smaybee 		uint64_t i, first, last;
11726992Smaybee 		int shift = epbs + dn->dn_datablkshift;
11732445Sahrens 
11746992Smaybee 		first = blkid >> epbs;
11757049Smaybee 		if (db = dbuf_hold_level(dn, 1, first, FTAG)) {
11767049Smaybee 			dbuf_will_dirty(db, tx);
11777049Smaybee 			dbuf_rele(db, FTAG);
11787049Smaybee 		}
11796992Smaybee 		if (trunc)
11806992Smaybee 			last = dn->dn_maxblkid >> epbs;
11812445Sahrens 		else
11826992Smaybee 			last = (blkid + nblks - 1) >> epbs;
11837049Smaybee 		if (last > first && (db = dbuf_hold_level(dn, 1, last, FTAG))) {
11847049Smaybee 			dbuf_will_dirty(db, tx);
11857049Smaybee 			dbuf_rele(db, FTAG);
11867049Smaybee 		}
11877049Smaybee 		for (i = first + 1; i < last; i++) {
11886992Smaybee 			uint64_t ibyte = i << shift;
11896992Smaybee 			int err;
1190789Sahrens 
11916992Smaybee 			err = dnode_next_offset(dn,
11926992Smaybee 			    DNODE_FIND_HAVELOCK, &ibyte, 1, 1, 0);
11936992Smaybee 			i = ibyte >> shift;
11947049Smaybee 			if (err == ESRCH || i >= last)
11956992Smaybee 				break;
11966992Smaybee 			ASSERT(err == 0);
11976992Smaybee 			db = dbuf_hold_level(dn, 1, i, FTAG);
11986992Smaybee 			if (db) {
11996992Smaybee 				dbuf_will_dirty(db, tx);
12002445Sahrens 				dbuf_rele(db, FTAG);
1201789Sahrens 			}
12022445Sahrens 		}
12032445Sahrens 	}
12046992Smaybee done:
12056992Smaybee 	/*
12066992Smaybee 	 * Add this range to the dnode range list.
12076992Smaybee 	 * We will finish up this free operation in the syncing phase.
12086992Smaybee 	 */
1209789Sahrens 	mutex_enter(&dn->dn_mtx);
1210789Sahrens 	dnode_clear_range(dn, blkid, nblks, tx);
1211789Sahrens 	{
1212789Sahrens 		free_range_t *rp, *found;
1213789Sahrens 		avl_index_t where;
1214789Sahrens 		avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
1215789Sahrens 
1216789Sahrens 		/* Add new range to dn_ranges */
1217789Sahrens 		rp = kmem_alloc(sizeof (free_range_t), KM_SLEEP);
1218789Sahrens 		rp->fr_blkid = blkid;
1219789Sahrens 		rp->fr_nblks = nblks;
1220789Sahrens 		found = avl_find(tree, rp, &where);
1221789Sahrens 		ASSERT(found == NULL);
1222789Sahrens 		avl_insert(tree, rp, where);
1223789Sahrens 		dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1224789Sahrens 		    blkid, nblks, tx->tx_txg);
1225789Sahrens 	}
1226789Sahrens 	mutex_exit(&dn->dn_mtx);
1227789Sahrens 
12286992Smaybee 	dbuf_free_range(dn, blkid, blkid + nblks - 1, tx);
1229789Sahrens 	dnode_setdirty(dn, tx);
1230789Sahrens out:
12316992Smaybee 	if (trunc && dn->dn_maxblkid >= (off >> blkshift))
12326992Smaybee 		dn->dn_maxblkid = (off >> blkshift ? (off >> blkshift) - 1 : 0);
12336992Smaybee 
1234789Sahrens 	rw_exit(&dn->dn_struct_rwlock);
1235789Sahrens }
1236789Sahrens 
123711935SMark.Shellenbaum@Sun.COM static boolean_t
123811935SMark.Shellenbaum@Sun.COM dnode_spill_freed(dnode_t *dn)
123911935SMark.Shellenbaum@Sun.COM {
124011935SMark.Shellenbaum@Sun.COM 	int i;
124111935SMark.Shellenbaum@Sun.COM 
124211935SMark.Shellenbaum@Sun.COM 	mutex_enter(&dn->dn_mtx);
124311935SMark.Shellenbaum@Sun.COM 	for (i = 0; i < TXG_SIZE; i++) {
124411935SMark.Shellenbaum@Sun.COM 		if (dn->dn_rm_spillblk[i] == DN_KILL_SPILLBLK)
124511935SMark.Shellenbaum@Sun.COM 			break;
124611935SMark.Shellenbaum@Sun.COM 	}
124711935SMark.Shellenbaum@Sun.COM 	mutex_exit(&dn->dn_mtx);
124811935SMark.Shellenbaum@Sun.COM 	return (i < TXG_SIZE);
124911935SMark.Shellenbaum@Sun.COM }
125011935SMark.Shellenbaum@Sun.COM 
1251789Sahrens /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
1252789Sahrens uint64_t
1253789Sahrens dnode_block_freed(dnode_t *dn, uint64_t blkid)
1254789Sahrens {
1255789Sahrens 	free_range_t range_tofind;
1256789Sahrens 	void *dp = spa_get_dsl(dn->dn_objset->os_spa);
1257789Sahrens 	int i;
1258789Sahrens 
125911935SMark.Shellenbaum@Sun.COM 	if (blkid == DMU_BONUS_BLKID)
1260789Sahrens 		return (FALSE);
1261789Sahrens 
1262789Sahrens 	/*
1263789Sahrens 	 * If we're in the process of opening the pool, dp will not be
1264789Sahrens 	 * set yet, but there shouldn't be anything dirty.
1265789Sahrens 	 */
1266789Sahrens 	if (dp == NULL)
1267789Sahrens 		return (FALSE);
1268789Sahrens 
1269789Sahrens 	if (dn->dn_free_txg)
1270789Sahrens 		return (TRUE);
1271789Sahrens 
127211935SMark.Shellenbaum@Sun.COM 	if (blkid == DMU_SPILL_BLKID)
127311935SMark.Shellenbaum@Sun.COM 		return (dnode_spill_freed(dn));
127411935SMark.Shellenbaum@Sun.COM 
1275789Sahrens 	range_tofind.fr_blkid = blkid;
1276789Sahrens 	mutex_enter(&dn->dn_mtx);
1277789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
1278789Sahrens 		free_range_t *range_found;
1279789Sahrens 		avl_index_t idx;
1280789Sahrens 
1281789Sahrens 		range_found = avl_find(&dn->dn_ranges[i], &range_tofind, &idx);
1282789Sahrens 		if (range_found) {
1283789Sahrens 			ASSERT(range_found->fr_nblks > 0);
1284789Sahrens 			break;
1285789Sahrens 		}
1286789Sahrens 		range_found = avl_nearest(&dn->dn_ranges[i], idx, AVL_BEFORE);
1287789Sahrens 		if (range_found &&
1288789Sahrens 		    range_found->fr_blkid + range_found->fr_nblks > blkid)
1289789Sahrens 			break;
1290789Sahrens 	}
1291789Sahrens 	mutex_exit(&dn->dn_mtx);
1292789Sahrens 	return (i < TXG_SIZE);
1293789Sahrens }
1294789Sahrens 
1295789Sahrens /* call from syncing context when we actually write/free space for this dnode */
1296789Sahrens void
12972082Seschrock dnode_diduse_space(dnode_t *dn, int64_t delta)
1298789Sahrens {
12992082Seschrock 	uint64_t space;
13002082Seschrock 	dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n",
1301789Sahrens 	    dn, dn->dn_phys,
13022082Seschrock 	    (u_longlong_t)dn->dn_phys->dn_used,
13032082Seschrock 	    (longlong_t)delta);
1304789Sahrens 
1305789Sahrens 	mutex_enter(&dn->dn_mtx);
13062082Seschrock 	space = DN_USED_BYTES(dn->dn_phys);
13072082Seschrock 	if (delta > 0) {
13082082Seschrock 		ASSERT3U(space + delta, >=, space); /* no overflow */
1309789Sahrens 	} else {
13102082Seschrock 		ASSERT3U(space, >=, -delta); /* no underflow */
13112082Seschrock 	}
13122082Seschrock 	space += delta;
13134577Sahrens 	if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_DNODE_BYTES) {
13142082Seschrock 		ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0);
13152082Seschrock 		ASSERT3U(P2PHASE(space, 1<<DEV_BSHIFT), ==, 0);
13162082Seschrock 		dn->dn_phys->dn_used = space >> DEV_BSHIFT;
13172082Seschrock 	} else {
13182082Seschrock 		dn->dn_phys->dn_used = space;
13192082Seschrock 		dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES;
1320789Sahrens 	}
1321789Sahrens 	mutex_exit(&dn->dn_mtx);
1322789Sahrens }
1323789Sahrens 
1324789Sahrens /*
1325789Sahrens  * Call when we think we're going to write/free space in open context.
1326789Sahrens  * Be conservative (ie. OK to write less than this or free more than
1327789Sahrens  * this, but don't write more or free less).
1328789Sahrens  */
1329789Sahrens void
1330789Sahrens dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx)
1331789Sahrens {
133210298SMatthew.Ahrens@Sun.COM 	objset_t *os = dn->dn_objset;
1333789Sahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1334789Sahrens 
1335789Sahrens 	if (space > 0)
1336789Sahrens 		space = spa_get_asize(os->os_spa, space);
1337789Sahrens 
1338789Sahrens 	if (ds)
1339789Sahrens 		dsl_dir_willuse_space(ds->ds_dir, space, tx);
1340789Sahrens 
1341789Sahrens 	dmu_tx_willuse_space(tx, space);
1342789Sahrens }
1343789Sahrens 
13449950SMark.Maybee@Sun.COM /*
13459950SMark.Maybee@Sun.COM  * This function scans a block at the indicated "level" looking for
13469950SMark.Maybee@Sun.COM  * a hole or data (depending on 'flags').  If level > 0, then we are
13479950SMark.Maybee@Sun.COM  * scanning an indirect block looking at its pointers.  If level == 0,
13489950SMark.Maybee@Sun.COM  * then we are looking at a block of dnodes.  If we don't find what we
13499950SMark.Maybee@Sun.COM  * are looking for in the block, we return ESRCH.  Otherwise, return
13509950SMark.Maybee@Sun.COM  * with *offset pointing to the beginning (if searching forwards) or
13519950SMark.Maybee@Sun.COM  * end (if searching backwards) of the range covered by the block
13529950SMark.Maybee@Sun.COM  * pointer we matched on (or dnode).
13539950SMark.Maybee@Sun.COM  *
13549950SMark.Maybee@Sun.COM  * The basic search algorithm used below by dnode_next_offset() is to
13559950SMark.Maybee@Sun.COM  * use this function to search up the block tree (widen the search) until
13569950SMark.Maybee@Sun.COM  * we find something (i.e., we don't return ESRCH) and then search back
13579950SMark.Maybee@Sun.COM  * down the tree (narrow the search) until we reach our original search
13589950SMark.Maybee@Sun.COM  * level.
13599950SMark.Maybee@Sun.COM  */
1360789Sahrens static int
13616992Smaybee dnode_next_offset_level(dnode_t *dn, int flags, uint64_t *offset,
13623025Sahrens 	int lvl, uint64_t blkfill, uint64_t txg)
1363789Sahrens {
1364789Sahrens 	dmu_buf_impl_t *db = NULL;
1365789Sahrens 	void *data = NULL;
1366789Sahrens 	uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
1367789Sahrens 	uint64_t epb = 1ULL << epbs;
1368789Sahrens 	uint64_t minfill, maxfill;
13696992Smaybee 	boolean_t hole;
13706992Smaybee 	int i, inc, error, span;
1371789Sahrens 
1372789Sahrens 	dprintf("probing object %llu offset %llx level %d of %u\n",
1373789Sahrens 	    dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels);
1374789Sahrens 
13759396SMatthew.Ahrens@Sun.COM 	hole = ((flags & DNODE_FIND_HOLE) != 0);
13766992Smaybee 	inc = (flags & DNODE_FIND_BACKWARDS) ? -1 : 1;
13777385SMark.Maybee@Sun.COM 	ASSERT(txg == 0 || !hole);
13786992Smaybee 
1379789Sahrens 	if (lvl == dn->dn_phys->dn_nlevels) {
1380789Sahrens 		error = 0;
1381789Sahrens 		epb = dn->dn_phys->dn_nblkptr;
1382789Sahrens 		data = dn->dn_phys->dn_blkptr;
1383789Sahrens 	} else {
1384789Sahrens 		uint64_t blkid = dbuf_whichblock(dn, *offset) >> (epbs * lvl);
1385789Sahrens 		error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FTAG, &db);
1386789Sahrens 		if (error) {
13877385SMark.Maybee@Sun.COM 			if (error != ENOENT)
13887385SMark.Maybee@Sun.COM 				return (error);
13897385SMark.Maybee@Sun.COM 			if (hole)
13907385SMark.Maybee@Sun.COM 				return (0);
13917385SMark.Maybee@Sun.COM 			/*
13927385SMark.Maybee@Sun.COM 			 * This can only happen when we are searching up
13937385SMark.Maybee@Sun.COM 			 * the block tree for data.  We don't really need to
13947385SMark.Maybee@Sun.COM 			 * adjust the offset, as we will just end up looking
13957385SMark.Maybee@Sun.COM 			 * at the pointer to this block in its parent, and its
13967385SMark.Maybee@Sun.COM 			 * going to be unallocated, so we will skip over it.
13977385SMark.Maybee@Sun.COM 			 */
13987385SMark.Maybee@Sun.COM 			return (ESRCH);
1399789Sahrens 		}
14001793Sahrens 		error = dbuf_read(db, NULL, DB_RF_CANFAIL | DB_RF_HAVESTRUCT);
14011793Sahrens 		if (error) {
14021793Sahrens 			dbuf_rele(db, FTAG);
14031793Sahrens 			return (error);
14041793Sahrens 		}
1405789Sahrens 		data = db->db.db_data;
1406789Sahrens 	}
1407789Sahrens 
14083025Sahrens 	if (db && txg &&
14093025Sahrens 	    (db->db_blkptr == NULL || db->db_blkptr->blk_birth <= txg)) {
14107385SMark.Maybee@Sun.COM 		/*
14117385SMark.Maybee@Sun.COM 		 * This can only happen when we are searching up the tree
14127385SMark.Maybee@Sun.COM 		 * and these conditions mean that we need to keep climbing.
14137385SMark.Maybee@Sun.COM 		 */
14143025Sahrens 		error = ESRCH;
14153025Sahrens 	} else if (lvl == 0) {
1416789Sahrens 		dnode_phys_t *dnp = data;
1417789Sahrens 		span = DNODE_SHIFT;
1418789Sahrens 		ASSERT(dn->dn_type == DMU_OT_DNODE);
1419789Sahrens 
14206992Smaybee 		for (i = (*offset >> span) & (blkfill - 1);
14216992Smaybee 		    i >= 0 && i < blkfill; i += inc) {
14229409SJonathan.Adams@Sun.COM 			if ((dnp[i].dn_type == DMU_OT_NONE) == hole)
1423789Sahrens 				break;
14246992Smaybee 			*offset += (1ULL << span) * inc;
1425789Sahrens 		}
14266992Smaybee 		if (i < 0 || i == blkfill)
1427789Sahrens 			error = ESRCH;
1428789Sahrens 	} else {
1429789Sahrens 		blkptr_t *bp = data;
14309950SMark.Maybee@Sun.COM 		uint64_t start = *offset;
1431789Sahrens 		span = (lvl - 1) * epbs + dn->dn_datablkshift;
1432789Sahrens 		minfill = 0;
1433789Sahrens 		maxfill = blkfill << ((lvl - 1) * epbs);
1434789Sahrens 
1435789Sahrens 		if (hole)
1436789Sahrens 			maxfill--;
1437789Sahrens 		else
1438789Sahrens 			minfill++;
1439789Sahrens 
14409950SMark.Maybee@Sun.COM 		*offset = *offset >> span;
14419950SMark.Maybee@Sun.COM 		for (i = BF64_GET(*offset, 0, epbs);
14426992Smaybee 		    i >= 0 && i < epb; i += inc) {
1443789Sahrens 			if (bp[i].blk_fill >= minfill &&
14443025Sahrens 			    bp[i].blk_fill <= maxfill &&
14457385SMark.Maybee@Sun.COM 			    (hole || bp[i].blk_birth > txg))
1446789Sahrens 				break;
14479950SMark.Maybee@Sun.COM 			if (inc > 0 || *offset > 0)
14489950SMark.Maybee@Sun.COM 				*offset += inc;
1449789Sahrens 		}
14509950SMark.Maybee@Sun.COM 		*offset = *offset << span;
14519950SMark.Maybee@Sun.COM 		if (inc < 0) {
14529950SMark.Maybee@Sun.COM 			/* traversing backwards; position offset at the end */
14539950SMark.Maybee@Sun.COM 			ASSERT3U(*offset, <=, start);
14549950SMark.Maybee@Sun.COM 			*offset = MIN(*offset + (1ULL << span) - 1, start);
14559950SMark.Maybee@Sun.COM 		} else if (*offset < start) {
14569950SMark.Maybee@Sun.COM 			*offset = start;
14579950SMark.Maybee@Sun.COM 		}
14589950SMark.Maybee@Sun.COM 		if (i < 0 || i >= epb)
1459789Sahrens 			error = ESRCH;
1460789Sahrens 	}
1461789Sahrens 
1462789Sahrens 	if (db)
14631544Seschrock 		dbuf_rele(db, FTAG);
1464789Sahrens 
1465789Sahrens 	return (error);
1466789Sahrens }
1467789Sahrens 
1468789Sahrens /*
1469789Sahrens  * Find the next hole, data, or sparse region at or after *offset.
1470789Sahrens  * The value 'blkfill' tells us how many items we expect to find
1471789Sahrens  * in an L0 data block; this value is 1 for normal objects,
1472789Sahrens  * DNODES_PER_BLOCK for the meta dnode, and some fraction of
1473789Sahrens  * DNODES_PER_BLOCK when searching for sparse regions thereof.
14743025Sahrens  *
1475789Sahrens  * Examples:
1476789Sahrens  *
14776992Smaybee  * dnode_next_offset(dn, flags, offset, 1, 1, 0);
14786992Smaybee  *	Finds the next/previous hole/data in a file.
1479789Sahrens  *	Used in dmu_offset_next().
1480789Sahrens  *
14816992Smaybee  * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
1482789Sahrens  *	Finds the next free/allocated dnode an objset's meta-dnode.
14833025Sahrens  *	Only finds objects that have new contents since txg (ie.
14843025Sahrens  *	bonus buffer changes and content removal are ignored).
1485789Sahrens  *	Used in dmu_object_next().
1486789Sahrens  *
14876992Smaybee  * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
1488789Sahrens  *	Finds the next L2 meta-dnode bp that's at most 1/4 full.
1489789Sahrens  *	Used in dmu_object_alloc().
1490789Sahrens  */
1491789Sahrens int
14926992Smaybee dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset,
14933025Sahrens     int minlvl, uint64_t blkfill, uint64_t txg)
1494789Sahrens {
14956992Smaybee 	uint64_t initial_offset = *offset;
1496789Sahrens 	int lvl, maxlvl;
1497789Sahrens 	int error = 0;
1498789Sahrens 
14996992Smaybee 	if (!(flags & DNODE_FIND_HAVELOCK))
15006992Smaybee 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
1501789Sahrens 
1502789Sahrens 	if (dn->dn_phys->dn_nlevels == 0) {
15036992Smaybee 		error = ESRCH;
15046992Smaybee 		goto out;
1505789Sahrens 	}
1506789Sahrens 
1507789Sahrens 	if (dn->dn_datablkshift == 0) {
1508789Sahrens 		if (*offset < dn->dn_datablksz) {
15096992Smaybee 			if (flags & DNODE_FIND_HOLE)
1510789Sahrens 				*offset = dn->dn_datablksz;
1511789Sahrens 		} else {
1512789Sahrens 			error = ESRCH;
1513789Sahrens 		}
15146992Smaybee 		goto out;
1515789Sahrens 	}
1516789Sahrens 
1517789Sahrens 	maxlvl = dn->dn_phys->dn_nlevels;
1518789Sahrens 
1519789Sahrens 	for (lvl = minlvl; lvl <= maxlvl; lvl++) {
15203025Sahrens 		error = dnode_next_offset_level(dn,
15216992Smaybee 		    flags, offset, lvl, blkfill, txg);
15221793Sahrens 		if (error != ESRCH)
1523789Sahrens 			break;
1524789Sahrens 	}
1525789Sahrens 
15266992Smaybee 	while (error == 0 && --lvl >= minlvl) {
15273025Sahrens 		error = dnode_next_offset_level(dn,
15286992Smaybee 		    flags, offset, lvl, blkfill, txg);
15293025Sahrens 	}
1530789Sahrens 
15316992Smaybee 	if (error == 0 && (flags & DNODE_FIND_BACKWARDS ?
15326992Smaybee 	    initial_offset < *offset : initial_offset > *offset))
15331793Sahrens 		error = ESRCH;
15346992Smaybee out:
15356992Smaybee 	if (!(flags & DNODE_FIND_HAVELOCK))
15366992Smaybee 		rw_exit(&dn->dn_struct_rwlock);
1537789Sahrens 
1538789Sahrens 	return (error);
1539789Sahrens }
1540