xref: /titanic_51/usr/src/uts/common/fs/zfs/dnode.c (revision 381a2a9a387f449fab7d0c7e97c4184c26963abf)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/zfs_context.h>
29 #include <sys/dbuf.h>
30 #include <sys/dnode.h>
31 #include <sys/dmu.h>
32 #include <sys/dmu_impl.h>
33 #include <sys/dmu_tx.h>
34 #include <sys/dmu_objset.h>
35 #include <sys/dsl_dir.h>
36 #include <sys/dsl_dataset.h>
37 #include <sys/spa.h>
38 #include <sys/zio.h>
39 #include <sys/dmu_zfetch.h>
40 
41 static int free_range_compar(const void *node1, const void *node2);
42 
43 static kmem_cache_t *dnode_cache;
44 
45 static dnode_phys_t dnode_phys_zero;
46 
47 int zfs_default_bs = SPA_MINBLOCKSHIFT;
48 int zfs_default_ibs = DN_MAX_INDBLKSHIFT;
49 
50 /* ARGSUSED */
51 static int
52 dnode_cons(void *arg, void *unused, int kmflag)
53 {
54 	int i;
55 	dnode_t *dn = arg;
56 	bzero(dn, sizeof (dnode_t));
57 
58 	rw_init(&dn->dn_struct_rwlock, NULL, RW_DEFAULT, NULL);
59 	mutex_init(&dn->dn_mtx, NULL, MUTEX_DEFAULT, NULL);
60 	mutex_init(&dn->dn_dbufs_mtx, NULL, MUTEX_DEFAULT, NULL);
61 	refcount_create(&dn->dn_holds);
62 	refcount_create(&dn->dn_tx_holds);
63 
64 	for (i = 0; i < TXG_SIZE; i++) {
65 		avl_create(&dn->dn_ranges[i], free_range_compar,
66 		    sizeof (free_range_t),
67 		    offsetof(struct free_range, fr_node));
68 		list_create(&dn->dn_dirty_dbufs[i],
69 		    sizeof (dmu_buf_impl_t),
70 		    offsetof(dmu_buf_impl_t, db_dirty_node[i]));
71 	}
72 
73 	list_create(&dn->dn_dbufs, sizeof (dmu_buf_impl_t),
74 	    offsetof(dmu_buf_impl_t, db_link));
75 
76 	return (0);
77 }
78 
79 /* ARGSUSED */
80 static void
81 dnode_dest(void *arg, void *unused)
82 {
83 	int i;
84 	dnode_t *dn = arg;
85 
86 	rw_destroy(&dn->dn_struct_rwlock);
87 	mutex_destroy(&dn->dn_mtx);
88 	mutex_destroy(&dn->dn_dbufs_mtx);
89 	refcount_destroy(&dn->dn_holds);
90 	refcount_destroy(&dn->dn_tx_holds);
91 
92 	for (i = 0; i < TXG_SIZE; i++) {
93 		avl_destroy(&dn->dn_ranges[i]);
94 		list_destroy(&dn->dn_dirty_dbufs[i]);
95 	}
96 
97 	list_destroy(&dn->dn_dbufs);
98 }
99 
100 void
101 dnode_init(void)
102 {
103 	dnode_cache = kmem_cache_create("dnode_t",
104 	    sizeof (dnode_t),
105 	    0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0);
106 }
107 
108 void
109 dnode_fini(void)
110 {
111 	kmem_cache_destroy(dnode_cache);
112 }
113 
114 
115 #ifdef ZFS_DEBUG
116 void
117 dnode_verify(dnode_t *dn)
118 {
119 	int drop_struct_lock = FALSE;
120 
121 	ASSERT(dn->dn_phys);
122 	ASSERT(dn->dn_objset);
123 
124 	ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES);
125 
126 	if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY))
127 		return;
128 
129 	if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
130 		rw_enter(&dn->dn_struct_rwlock, RW_READER);
131 		drop_struct_lock = TRUE;
132 	}
133 	if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) {
134 		int i;
135 		ASSERT3U(dn->dn_indblkshift, >=, 0);
136 		ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT);
137 		if (dn->dn_datablkshift) {
138 			ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT);
139 			ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT);
140 			ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz);
141 		}
142 		ASSERT3U(dn->dn_nlevels, <=, 30);
143 		ASSERT3U(dn->dn_type, <=, DMU_OT_NUMTYPES);
144 		ASSERT3U(dn->dn_nblkptr, >=, 1);
145 		ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
146 		ASSERT3U(dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
147 		ASSERT3U(dn->dn_datablksz, ==,
148 		    dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
149 		ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0);
150 		ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) +
151 		    dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
152 		for (i = 0; i < TXG_SIZE; i++) {
153 			ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels);
154 		}
155 	}
156 	if (dn->dn_phys->dn_type != DMU_OT_NONE)
157 		ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels);
158 	ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT || dn->dn_dbuf != NULL);
159 	if (dn->dn_dbuf != NULL) {
160 		ASSERT3P(dn->dn_phys, ==,
161 		    (dnode_phys_t *)dn->dn_dbuf->db.db_data +
162 		    (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT)));
163 	}
164 	if (drop_struct_lock)
165 		rw_exit(&dn->dn_struct_rwlock);
166 }
167 #endif
168 
169 void
170 dnode_byteswap(dnode_phys_t *dnp)
171 {
172 	uint64_t *buf64 = (void*)&dnp->dn_blkptr;
173 	int i;
174 
175 	if (dnp->dn_type == DMU_OT_NONE) {
176 		bzero(dnp, sizeof (dnode_phys_t));
177 		return;
178 	}
179 
180 	dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec);
181 	dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen);
182 	dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid);
183 	dnp->dn_used = BSWAP_64(dnp->dn_used);
184 
185 	/*
186 	 * dn_nblkptr is only one byte, so it's OK to read it in either
187 	 * byte order.  We can't read dn_bouslen.
188 	 */
189 	ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT);
190 	ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR);
191 	for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++)
192 		buf64[i] = BSWAP_64(buf64[i]);
193 
194 	/*
195 	 * OK to check dn_bonuslen for zero, because it won't matter if
196 	 * we have the wrong byte order.  This is necessary because the
197 	 * dnode dnode is smaller than a regular dnode.
198 	 */
199 	if (dnp->dn_bonuslen != 0) {
200 		/*
201 		 * Note that the bonus length calculated here may be
202 		 * longer than the actual bonus buffer.  This is because
203 		 * we always put the bonus buffer after the last block
204 		 * pointer (instead of packing it against the end of the
205 		 * dnode buffer).
206 		 */
207 		int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t);
208 		size_t len = DN_MAX_BONUSLEN - off;
209 		dmu_ot[dnp->dn_bonustype].ot_byteswap(dnp->dn_bonus + off, len);
210 	}
211 }
212 
213 void
214 dnode_buf_byteswap(void *vbuf, size_t size)
215 {
216 	dnode_phys_t *buf = vbuf;
217 	int i;
218 
219 	ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT));
220 	ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0);
221 
222 	size >>= DNODE_SHIFT;
223 	for (i = 0; i < size; i++) {
224 		dnode_byteswap(buf);
225 		buf++;
226 	}
227 }
228 
229 static int
230 free_range_compar(const void *node1, const void *node2)
231 {
232 	const free_range_t *rp1 = node1;
233 	const free_range_t *rp2 = node2;
234 
235 	if (rp1->fr_blkid < rp2->fr_blkid)
236 		return (-1);
237 	else if (rp1->fr_blkid > rp2->fr_blkid)
238 		return (1);
239 	else return (0);
240 }
241 
242 static void
243 dnode_setdblksz(dnode_t *dn, int size)
244 {
245 	ASSERT3U(P2PHASE(size, SPA_MINBLOCKSIZE), ==, 0);
246 	ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
247 	ASSERT3U(size, >=, SPA_MINBLOCKSIZE);
248 	ASSERT3U(size >> SPA_MINBLOCKSHIFT, <,
249 	    1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8));
250 	dn->dn_datablksz = size;
251 	dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT;
252 	dn->dn_datablkshift = ISP2(size) ? highbit(size - 1) : 0;
253 }
254 
255 static dnode_t *
256 dnode_create(objset_impl_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db,
257     uint64_t object)
258 {
259 	dnode_t *dn = kmem_cache_alloc(dnode_cache, KM_SLEEP);
260 	(void) dnode_cons(dn, NULL, 0); /* XXX */
261 
262 	dn->dn_objset = os;
263 	dn->dn_object = object;
264 	dn->dn_dbuf = db;
265 	dn->dn_phys = dnp;
266 
267 	if (dnp->dn_datablkszsec)
268 		dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
269 	dn->dn_indblkshift = dnp->dn_indblkshift;
270 	dn->dn_nlevels = dnp->dn_nlevels;
271 	dn->dn_type = dnp->dn_type;
272 	dn->dn_nblkptr = dnp->dn_nblkptr;
273 	dn->dn_checksum = dnp->dn_checksum;
274 	dn->dn_compress = dnp->dn_compress;
275 	dn->dn_bonustype = dnp->dn_bonustype;
276 	dn->dn_bonuslen = dnp->dn_bonuslen;
277 	dn->dn_maxblkid = dnp->dn_maxblkid;
278 
279 	dmu_zfetch_init(&dn->dn_zfetch, dn);
280 
281 	ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES);
282 	mutex_enter(&os->os_lock);
283 	list_insert_head(&os->os_dnodes, dn);
284 	mutex_exit(&os->os_lock);
285 
286 	return (dn);
287 }
288 
289 static void
290 dnode_destroy(dnode_t *dn)
291 {
292 	objset_impl_t *os = dn->dn_objset;
293 
294 #ifdef ZFS_DEBUG
295 	int i;
296 
297 	for (i = 0; i < TXG_SIZE; i++) {
298 		ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
299 		ASSERT(NULL == list_head(&dn->dn_dirty_dbufs[i]));
300 		ASSERT(0 == avl_numnodes(&dn->dn_ranges[i]));
301 	}
302 	ASSERT(NULL == list_head(&dn->dn_dbufs));
303 #endif
304 
305 	mutex_enter(&os->os_lock);
306 	list_remove(&os->os_dnodes, dn);
307 	mutex_exit(&os->os_lock);
308 
309 	if (dn->dn_dirtyctx_firstset) {
310 		kmem_free(dn->dn_dirtyctx_firstset, 1);
311 		dn->dn_dirtyctx_firstset = NULL;
312 	}
313 	dmu_zfetch_rele(&dn->dn_zfetch);
314 	if (dn->dn_bonus) {
315 		mutex_enter(&dn->dn_bonus->db_mtx);
316 		dbuf_evict(dn->dn_bonus);
317 		dn->dn_bonus = NULL;
318 	}
319 	kmem_cache_free(dnode_cache, dn);
320 }
321 
322 void
323 dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
324     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
325 {
326 	int i;
327 
328 	if (blocksize == 0)
329 		blocksize = 1 << zfs_default_bs;
330 	else if (blocksize > SPA_MAXBLOCKSIZE)
331 		blocksize = SPA_MAXBLOCKSIZE;
332 	else
333 		blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE);
334 
335 	if (ibs == 0)
336 		ibs = zfs_default_ibs;
337 
338 	ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT);
339 
340 	dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset,
341 	    dn->dn_object, tx->tx_txg, blocksize, ibs);
342 
343 	ASSERT(dn->dn_type == DMU_OT_NONE);
344 	ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0);
345 	ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE);
346 	ASSERT(ot != DMU_OT_NONE);
347 	ASSERT3U(ot, <, DMU_OT_NUMTYPES);
348 	ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
349 	    (bonustype != DMU_OT_NONE && bonuslen != 0));
350 	ASSERT3U(bonustype, <, DMU_OT_NUMTYPES);
351 	ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
352 	ASSERT(dn->dn_type == DMU_OT_NONE);
353 	ASSERT3U(dn->dn_maxblkid, ==, 0);
354 	ASSERT3U(dn->dn_allocated_txg, ==, 0);
355 	ASSERT3U(dn->dn_assigned_txg, ==, 0);
356 	ASSERT(refcount_is_zero(&dn->dn_tx_holds));
357 	ASSERT3U(refcount_count(&dn->dn_holds), <=, 1);
358 	ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
359 
360 	for (i = 0; i < TXG_SIZE; i++) {
361 		ASSERT3U(dn->dn_next_nlevels[i], ==, 0);
362 		ASSERT3U(dn->dn_next_indblkshift[i], ==, 0);
363 		ASSERT3U(dn->dn_next_blksz[i], ==, 0);
364 		ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
365 		ASSERT3P(list_head(&dn->dn_dirty_dbufs[i]), ==, NULL);
366 		ASSERT3U(avl_numnodes(&dn->dn_ranges[i]), ==, 0);
367 	}
368 
369 	dn->dn_type = ot;
370 	dnode_setdblksz(dn, blocksize);
371 	dn->dn_indblkshift = ibs;
372 	dn->dn_nlevels = 1;
373 	dn->dn_nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
374 	dn->dn_bonustype = bonustype;
375 	dn->dn_bonuslen = bonuslen;
376 	dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
377 	dn->dn_compress = ZIO_COMPRESS_INHERIT;
378 	dn->dn_dirtyctx = 0;
379 
380 	dn->dn_free_txg = 0;
381 	if (dn->dn_dirtyctx_firstset) {
382 		kmem_free(dn->dn_dirtyctx_firstset, 1);
383 		dn->dn_dirtyctx_firstset = NULL;
384 	}
385 
386 	dn->dn_allocated_txg = tx->tx_txg;
387 
388 	dnode_setdirty(dn, tx);
389 	dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs;
390 	dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz;
391 }
392 
393 void
394 dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
395     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
396 {
397 	int i;
398 
399 	ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE);
400 	ASSERT3U(blocksize, <=, SPA_MAXBLOCKSIZE);
401 	ASSERT3U(blocksize % SPA_MINBLOCKSIZE, ==, 0);
402 	ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
403 	ASSERT(tx->tx_txg != 0);
404 	ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
405 	    (bonustype != DMU_OT_NONE && bonuslen != 0));
406 	ASSERT3U(bonustype, <, DMU_OT_NUMTYPES);
407 	ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
408 
409 	for (i = 0; i < TXG_SIZE; i++)
410 		ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
411 
412 	/* clean up any unreferenced dbufs */
413 	(void) dnode_evict_dbufs(dn, 0);
414 	ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
415 
416 	/*
417 	 * XXX I should really have a generation number to tell if we
418 	 * need to do this...
419 	 */
420 	if (blocksize != dn->dn_datablksz ||
421 	    dn->dn_bonustype != bonustype || dn->dn_bonuslen != bonuslen) {
422 		/* free all old data */
423 		dnode_free_range(dn, 0, -1ULL, tx);
424 	}
425 
426 	/* change blocksize */
427 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
428 	dnode_setdblksz(dn, blocksize);
429 	dnode_setdirty(dn, tx);
430 	dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize;
431 	rw_exit(&dn->dn_struct_rwlock);
432 
433 	/* change type */
434 	dn->dn_type = ot;
435 
436 	if (dn->dn_bonuslen != bonuslen) {
437 		dmu_buf_impl_t *db = NULL;
438 
439 		/* change bonus size */
440 		if (bonuslen == 0)
441 			bonuslen = 1; /* XXX */
442 		rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
443 		if (dn->dn_bonus == NULL)
444 			dn->dn_bonus = dbuf_create_bonus(dn);
445 		db = dn->dn_bonus;
446 		rw_exit(&dn->dn_struct_rwlock);
447 		if (refcount_add(&db->db_holds, FTAG) == 1)
448 			dnode_add_ref(dn, db);
449 		VERIFY(0 == dbuf_read(db, NULL, DB_RF_MUST_SUCCEED));
450 		mutex_enter(&db->db_mtx);
451 		ASSERT3U(db->db.db_size, ==, dn->dn_bonuslen);
452 		ASSERT(db->db.db_data != NULL);
453 		db->db.db_size = bonuslen;
454 		mutex_exit(&db->db_mtx);
455 		dbuf_dirty(db, tx);
456 		dbuf_rele(db, FTAG);
457 	}
458 
459 	/* change bonus size and type */
460 	mutex_enter(&dn->dn_mtx);
461 	dn->dn_bonustype = bonustype;
462 	dn->dn_bonuslen = bonuslen;
463 	dn->dn_nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
464 	dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
465 	dn->dn_compress = ZIO_COMPRESS_INHERIT;
466 	ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
467 
468 	dn->dn_allocated_txg = tx->tx_txg;
469 	mutex_exit(&dn->dn_mtx);
470 }
471 
472 void
473 dnode_special_close(dnode_t *dn)
474 {
475 	/*
476 	 * Wait for final references to the dnode to clear.  This can
477 	 * only happen if the arc is asyncronously evicting state that
478 	 * has a hold on this dnode while we are trying to evict this
479 	 * dnode.
480 	 */
481 	while (refcount_count(&dn->dn_holds) > 0)
482 		delay(1);
483 	dnode_destroy(dn);
484 }
485 
486 dnode_t *
487 dnode_special_open(objset_impl_t *os, dnode_phys_t *dnp, uint64_t object)
488 {
489 	dnode_t *dn = dnode_create(os, dnp, NULL, object);
490 	DNODE_VERIFY(dn);
491 	return (dn);
492 }
493 
494 static void
495 dnode_buf_pageout(dmu_buf_t *db, void *arg)
496 {
497 	dnode_t **children_dnodes = arg;
498 	int i;
499 	int epb = db->db_size >> DNODE_SHIFT;
500 
501 	for (i = 0; i < epb; i++) {
502 		dnode_t *dn = children_dnodes[i];
503 		int n;
504 
505 		if (dn == NULL)
506 			continue;
507 #ifdef ZFS_DEBUG
508 		/*
509 		 * If there are holds on this dnode, then there should
510 		 * be holds on the dnode's containing dbuf as well; thus
511 		 * it wouldn't be eligable for eviction and this function
512 		 * would not have been called.
513 		 */
514 		ASSERT(refcount_is_zero(&dn->dn_holds));
515 		ASSERT(list_head(&dn->dn_dbufs) == NULL);
516 		ASSERT(refcount_is_zero(&dn->dn_tx_holds));
517 
518 		for (n = 0; n < TXG_SIZE; n++)
519 			ASSERT(!list_link_active(&dn->dn_dirty_link[n]));
520 #endif
521 		children_dnodes[i] = NULL;
522 		dnode_destroy(dn);
523 	}
524 	kmem_free(children_dnodes, epb * sizeof (dnode_t *));
525 }
526 
527 /*
528  * errors:
529  * EINVAL - invalid object number.
530  * EIO - i/o error.
531  * succeeds even for free dnodes.
532  */
533 int
534 dnode_hold_impl(objset_impl_t *os, uint64_t object, int flag,
535     void *tag, dnode_t **dnp)
536 {
537 	int epb, idx, err;
538 	int drop_struct_lock = FALSE;
539 	int type;
540 	uint64_t blk;
541 	dnode_t *mdn, *dn;
542 	dmu_buf_impl_t *db;
543 	dnode_t **children_dnodes;
544 
545 	if (object == 0 || object >= DN_MAX_OBJECT)
546 		return (EINVAL);
547 
548 	mdn = os->os_meta_dnode;
549 
550 	DNODE_VERIFY(mdn);
551 
552 	if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) {
553 		rw_enter(&mdn->dn_struct_rwlock, RW_READER);
554 		drop_struct_lock = TRUE;
555 	}
556 
557 	blk = dbuf_whichblock(mdn, object * sizeof (dnode_phys_t));
558 
559 	db = dbuf_hold(mdn, blk, FTAG);
560 	if (drop_struct_lock)
561 		rw_exit(&mdn->dn_struct_rwlock);
562 	if (db == NULL)
563 		return (EIO);
564 	err = dbuf_read(db, NULL, DB_RF_CANFAIL);
565 	if (err) {
566 		dbuf_rele(db, FTAG);
567 		return (err);
568 	}
569 
570 	ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT);
571 	epb = db->db.db_size >> DNODE_SHIFT;
572 
573 	idx = object & (epb-1);
574 
575 	children_dnodes = dmu_buf_get_user(&db->db);
576 	if (children_dnodes == NULL) {
577 		dnode_t **winner;
578 		children_dnodes = kmem_zalloc(epb * sizeof (dnode_t *),
579 		    KM_SLEEP);
580 		if (winner = dmu_buf_set_user(&db->db, children_dnodes, NULL,
581 		    dnode_buf_pageout)) {
582 			kmem_free(children_dnodes, epb * sizeof (dnode_t *));
583 			children_dnodes = winner;
584 		}
585 	}
586 
587 	if ((dn = children_dnodes[idx]) == NULL) {
588 		dnode_t *winner;
589 		dn = dnode_create(os, (dnode_phys_t *)db->db.db_data+idx,
590 			db, object);
591 		winner = atomic_cas_ptr(&children_dnodes[idx], NULL, dn);
592 		if (winner != NULL) {
593 			dnode_destroy(dn);
594 			dn = winner;
595 		}
596 	}
597 
598 	mutex_enter(&dn->dn_mtx);
599 	type = dn->dn_type;
600 	if (dn->dn_free_txg ||
601 	    ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) ||
602 	    ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)) {
603 		mutex_exit(&dn->dn_mtx);
604 		dbuf_rele(db, FTAG);
605 		return (type == DMU_OT_NONE ? ENOENT : EEXIST);
606 	}
607 	mutex_exit(&dn->dn_mtx);
608 
609 	if (refcount_add(&dn->dn_holds, tag) == 1)
610 		dbuf_add_ref(db, dn);
611 
612 	DNODE_VERIFY(dn);
613 	ASSERT3P(dn->dn_dbuf, ==, db);
614 	ASSERT3U(dn->dn_object, ==, object);
615 	dbuf_rele(db, FTAG);
616 
617 	*dnp = dn;
618 	return (0);
619 }
620 
621 /*
622  * Return held dnode if the object is allocated, NULL if not.
623  */
624 int
625 dnode_hold(objset_impl_t *os, uint64_t object, void *tag, dnode_t **dnp)
626 {
627 	return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, tag, dnp));
628 }
629 
630 void
631 dnode_add_ref(dnode_t *dn, void *tag)
632 {
633 	ASSERT(refcount_count(&dn->dn_holds) > 0);
634 	(void) refcount_add(&dn->dn_holds, tag);
635 }
636 
637 void
638 dnode_rele(dnode_t *dn, void *tag)
639 {
640 	uint64_t refs;
641 
642 	refs = refcount_remove(&dn->dn_holds, tag);
643 	/* NOTE: the DNODE_DNODE does not have a dn_dbuf */
644 	if (refs == 0 && dn->dn_dbuf)
645 		dbuf_rele(dn->dn_dbuf, dn);
646 }
647 
648 void
649 dnode_setdirty(dnode_t *dn, dmu_tx_t *tx)
650 {
651 	objset_impl_t *os = dn->dn_objset;
652 	uint64_t txg = tx->tx_txg;
653 
654 	if (dn->dn_object == DMU_META_DNODE_OBJECT)
655 		return;
656 
657 	DNODE_VERIFY(dn);
658 
659 #ifdef ZFS_DEBUG
660 	mutex_enter(&dn->dn_mtx);
661 	ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg);
662 	/* ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg); */
663 	mutex_exit(&dn->dn_mtx);
664 #endif
665 
666 	mutex_enter(&os->os_lock);
667 
668 	/*
669 	 * If we are already marked dirty, we're done.
670 	 */
671 	if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) {
672 		mutex_exit(&os->os_lock);
673 		return;
674 	}
675 
676 	ASSERT(!refcount_is_zero(&dn->dn_holds) || list_head(&dn->dn_dbufs));
677 	ASSERT(dn->dn_datablksz != 0);
678 	ASSERT3U(dn->dn_next_blksz[txg&TXG_MASK], ==, 0);
679 
680 	dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
681 	    dn->dn_object, txg);
682 
683 	if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) {
684 		list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn);
685 	} else {
686 		list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn);
687 	}
688 
689 	mutex_exit(&os->os_lock);
690 
691 	/*
692 	 * The dnode maintains a hold on its containing dbuf as
693 	 * long as there are holds on it.  Each instantiated child
694 	 * dbuf maintaines a hold on the dnode.  When the last child
695 	 * drops its hold, the dnode will drop its hold on the
696 	 * containing dbuf. We add a "dirty hold" here so that the
697 	 * dnode will hang around after we finish processing its
698 	 * children.
699 	 */
700 	dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg);
701 
702 	dbuf_dirty(dn->dn_dbuf, tx);
703 
704 	dsl_dataset_dirty(os->os_dsl_dataset, tx);
705 }
706 
707 void
708 dnode_free(dnode_t *dn, dmu_tx_t *tx)
709 {
710 	int txgoff = tx->tx_txg & TXG_MASK;
711 
712 	dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg);
713 
714 	/* we should be the only holder... hopefully */
715 	/* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */
716 
717 	mutex_enter(&dn->dn_mtx);
718 	if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) {
719 		mutex_exit(&dn->dn_mtx);
720 		return;
721 	}
722 	dn->dn_free_txg = tx->tx_txg;
723 	mutex_exit(&dn->dn_mtx);
724 
725 	/*
726 	 * If the dnode is already dirty, it needs to be moved from
727 	 * the dirty list to the free list.
728 	 */
729 	mutex_enter(&dn->dn_objset->os_lock);
730 	if (list_link_active(&dn->dn_dirty_link[txgoff])) {
731 		list_remove(&dn->dn_objset->os_dirty_dnodes[txgoff], dn);
732 		list_insert_tail(&dn->dn_objset->os_free_dnodes[txgoff], dn);
733 		mutex_exit(&dn->dn_objset->os_lock);
734 	} else {
735 		mutex_exit(&dn->dn_objset->os_lock);
736 		dnode_setdirty(dn, tx);
737 	}
738 }
739 
740 /*
741  * Try to change the block size for the indicated dnode.  This can only
742  * succeed if there are no blocks allocated or dirty beyond first block
743  */
744 int
745 dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx)
746 {
747 	dmu_buf_impl_t *db, *db_next;
748 	int have_db0 = FALSE;
749 
750 	if (size == 0)
751 		size = SPA_MINBLOCKSIZE;
752 	if (size > SPA_MAXBLOCKSIZE)
753 		size = SPA_MAXBLOCKSIZE;
754 	else
755 		size = P2ROUNDUP(size, SPA_MINBLOCKSIZE);
756 
757 	if (ibs == dn->dn_indblkshift)
758 		ibs = 0;
759 
760 	if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0)
761 		return (0);
762 
763 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
764 
765 	/* Check for any allocated blocks beyond the first */
766 	if (dn->dn_phys->dn_maxblkid != 0)
767 		goto fail;
768 
769 	mutex_enter(&dn->dn_dbufs_mtx);
770 	for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
771 		db_next = list_next(&dn->dn_dbufs, db);
772 
773 		if (db->db_blkid == 0) {
774 			have_db0 = TRUE;
775 		} else if (db->db_blkid != DB_BONUS_BLKID) {
776 			mutex_exit(&dn->dn_dbufs_mtx);
777 			goto fail;
778 		}
779 	}
780 	mutex_exit(&dn->dn_dbufs_mtx);
781 
782 	if (ibs && dn->dn_nlevels != 1)
783 		goto fail;
784 
785 	db = NULL;
786 	if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) || have_db0) {
787 		/* obtain the old block */
788 		db = dbuf_hold(dn, 0, FTAG);
789 		dbuf_new_size(db, size, tx);
790 	}
791 
792 	dnode_setdblksz(dn, size);
793 	dnode_setdirty(dn, tx);
794 	dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size;
795 	if (ibs) {
796 		dn->dn_indblkshift = ibs;
797 		dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs;
798 	}
799 
800 	if (db)
801 		dbuf_rele(db, FTAG);
802 
803 	rw_exit(&dn->dn_struct_rwlock);
804 	return (0);
805 
806 fail:
807 	rw_exit(&dn->dn_struct_rwlock);
808 	return (ENOTSUP);
809 }
810 
811 void
812 dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx)
813 {
814 	uint64_t txgoff = tx->tx_txg & TXG_MASK;
815 	int drop_struct_lock = FALSE;
816 	int epbs, new_nlevels;
817 	uint64_t sz;
818 
819 	ASSERT(blkid != DB_BONUS_BLKID);
820 
821 	if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
822 		rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
823 		drop_struct_lock = TRUE;
824 	}
825 
826 	if (blkid <= dn->dn_maxblkid)
827 		goto out;
828 
829 	dn->dn_maxblkid = blkid;
830 
831 	/*
832 	 * Compute the number of levels necessary to support the new maxblkid.
833 	 */
834 	new_nlevels = 1;
835 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
836 	for (sz = dn->dn_nblkptr;
837 	    sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs)
838 		new_nlevels++;
839 
840 	if (new_nlevels > dn->dn_nlevels) {
841 		int old_nlevels = dn->dn_nlevels;
842 		dmu_buf_impl_t *db;
843 
844 		dn->dn_nlevels = new_nlevels;
845 
846 		ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]);
847 		dn->dn_next_nlevels[txgoff] = new_nlevels;
848 
849 		/* Dirty the left indirects.  */
850 		db = dbuf_hold_level(dn, old_nlevels, 0, FTAG);
851 		dbuf_dirty(db, tx);
852 		dbuf_rele(db, FTAG);
853 
854 	}
855 
856 out:
857 	if (drop_struct_lock)
858 		rw_exit(&dn->dn_struct_rwlock);
859 }
860 
861 void
862 dnode_clear_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
863 {
864 	avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
865 	avl_index_t where;
866 	free_range_t *rp;
867 	free_range_t rp_tofind;
868 	uint64_t endblk = blkid + nblks;
869 
870 	ASSERT(MUTEX_HELD(&dn->dn_mtx));
871 	ASSERT(nblks <= UINT64_MAX - blkid); /* no overflow */
872 
873 	dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
874 	    blkid, nblks, tx->tx_txg);
875 	rp_tofind.fr_blkid = blkid;
876 	rp = avl_find(tree, &rp_tofind, &where);
877 	if (rp == NULL)
878 		rp = avl_nearest(tree, where, AVL_BEFORE);
879 	if (rp == NULL)
880 		rp = avl_nearest(tree, where, AVL_AFTER);
881 
882 	while (rp && (rp->fr_blkid <= blkid + nblks)) {
883 		uint64_t fr_endblk = rp->fr_blkid + rp->fr_nblks;
884 		free_range_t *nrp = AVL_NEXT(tree, rp);
885 
886 		if (blkid <= rp->fr_blkid && endblk >= fr_endblk) {
887 			/* clear this entire range */
888 			avl_remove(tree, rp);
889 			kmem_free(rp, sizeof (free_range_t));
890 		} else if (blkid <= rp->fr_blkid &&
891 		    endblk > rp->fr_blkid && endblk < fr_endblk) {
892 			/* clear the beginning of this range */
893 			rp->fr_blkid = endblk;
894 			rp->fr_nblks = fr_endblk - endblk;
895 		} else if (blkid > rp->fr_blkid && blkid < fr_endblk &&
896 		    endblk >= fr_endblk) {
897 			/* clear the end of this range */
898 			rp->fr_nblks = blkid - rp->fr_blkid;
899 		} else if (blkid > rp->fr_blkid && endblk < fr_endblk) {
900 			/* clear a chunk out of this range */
901 			free_range_t *new_rp =
902 			    kmem_alloc(sizeof (free_range_t), KM_SLEEP);
903 
904 			new_rp->fr_blkid = endblk;
905 			new_rp->fr_nblks = fr_endblk - endblk;
906 			avl_insert_here(tree, new_rp, rp, AVL_AFTER);
907 			rp->fr_nblks = blkid - rp->fr_blkid;
908 		}
909 		/* there may be no overlap */
910 		rp = nrp;
911 	}
912 }
913 
914 void
915 dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx)
916 {
917 	dmu_buf_impl_t *db;
918 	uint64_t blkoff, blkid, nblks;
919 	int blksz, head;
920 	int trunc = FALSE;
921 
922 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
923 	blksz = dn->dn_datablksz;
924 
925 	/* If the range is past the end of the file, this is a no-op */
926 	if (off >= blksz * (dn->dn_maxblkid+1))
927 		goto out;
928 	if (len == -1ULL) {
929 		len = UINT64_MAX - off;
930 		trunc = TRUE;
931 	}
932 
933 	/*
934 	 * First, block align the region to free:
935 	 */
936 	if (ISP2(blksz)) {
937 		head = P2NPHASE(off, blksz);
938 		blkoff = P2PHASE(off, blksz);
939 	} else {
940 		ASSERT(dn->dn_maxblkid == 0);
941 		if (off == 0 && len >= blksz) {
942 			/* Freeing the whole block; don't do any head. */
943 			head = 0;
944 		} else {
945 			/* Freeing part of the block. */
946 			head = blksz - off;
947 			ASSERT3U(head, >, 0);
948 		}
949 		blkoff = off;
950 	}
951 	/* zero out any partial block data at the start of the range */
952 	if (head) {
953 		ASSERT3U(blkoff + head, ==, blksz);
954 		if (len < head)
955 			head = len;
956 		if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off), TRUE,
957 		    FTAG, &db) == 0) {
958 			caddr_t data;
959 
960 			/* don't dirty if it isn't on disk and isn't dirty */
961 			if (db->db_dirtied ||
962 			    (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
963 				rw_exit(&dn->dn_struct_rwlock);
964 				dbuf_will_dirty(db, tx);
965 				rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
966 				data = db->db.db_data;
967 				bzero(data + blkoff, head);
968 			}
969 			dbuf_rele(db, FTAG);
970 		}
971 		off += head;
972 		len -= head;
973 	}
974 
975 	/* If the range was less than one block, we're done */
976 	if (len == 0 || off >= blksz * (dn->dn_maxblkid+1))
977 		goto out;
978 
979 	if (!ISP2(blksz)) {
980 		/*
981 		 * They are freeing the whole block of a
982 		 * non-power-of-two blocksize file.  Skip all the messy
983 		 * math.
984 		 */
985 		ASSERT3U(off, ==, 0);
986 		ASSERT3U(len, >=, blksz);
987 		blkid = 0;
988 		nblks = 1;
989 	} else {
990 		int tail;
991 		int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
992 		int blkshift = dn->dn_datablkshift;
993 
994 		/* If the remaining range is past end of file, we're done */
995 		if (off > dn->dn_maxblkid << blkshift)
996 			goto out;
997 
998 		if (off + len == UINT64_MAX)
999 			tail = 0;
1000 		else
1001 			tail = P2PHASE(len, blksz);
1002 
1003 		ASSERT3U(P2PHASE(off, blksz), ==, 0);
1004 		/* zero out any partial block data at the end of the range */
1005 		if (tail) {
1006 			if (len < tail)
1007 				tail = len;
1008 			if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len),
1009 			    TRUE, FTAG, &db) == 0) {
1010 				/* don't dirty if not on disk and not dirty */
1011 				if (db->db_dirtied ||
1012 				    (db->db_blkptr &&
1013 				    !BP_IS_HOLE(db->db_blkptr))) {
1014 					rw_exit(&dn->dn_struct_rwlock);
1015 					dbuf_will_dirty(db, tx);
1016 					rw_enter(&dn->dn_struct_rwlock,
1017 					    RW_WRITER);
1018 					bzero(db->db.db_data, tail);
1019 				}
1020 				dbuf_rele(db, FTAG);
1021 			}
1022 			len -= tail;
1023 		}
1024 		/* If the range did not include a full block, we are done */
1025 		if (len == 0)
1026 			goto out;
1027 
1028 		/* dirty the left indirects */
1029 		if (dn->dn_nlevels > 1 && off != 0) {
1030 			db = dbuf_hold_level(dn, 1,
1031 			    (off - head) >> (blkshift + epbs), FTAG);
1032 			dbuf_will_dirty(db, tx);
1033 			dbuf_rele(db, FTAG);
1034 		}
1035 
1036 		/* dirty the right indirects */
1037 		if (dn->dn_nlevels > 1 && !trunc) {
1038 			db = dbuf_hold_level(dn, 1,
1039 			    (off + len + tail - 1) >> (blkshift + epbs), FTAG);
1040 			dbuf_will_dirty(db, tx);
1041 			dbuf_rele(db, FTAG);
1042 		}
1043 
1044 		/*
1045 		 * Finally, add this range to the dnode range list, we
1046 		 * will finish up this free operation in the syncing phase.
1047 		 */
1048 		ASSERT(IS_P2ALIGNED(off, 1<<blkshift));
1049 		ASSERT(off + len == UINT64_MAX ||
1050 		    IS_P2ALIGNED(len, 1<<blkshift));
1051 		blkid = off >> blkshift;
1052 		nblks = len >> blkshift;
1053 
1054 		if (trunc)
1055 			dn->dn_maxblkid = (blkid ? blkid - 1 : 0);
1056 	}
1057 
1058 	mutex_enter(&dn->dn_mtx);
1059 	dnode_clear_range(dn, blkid, nblks, tx);
1060 	{
1061 		free_range_t *rp, *found;
1062 		avl_index_t where;
1063 		avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
1064 
1065 		/* Add new range to dn_ranges */
1066 		rp = kmem_alloc(sizeof (free_range_t), KM_SLEEP);
1067 		rp->fr_blkid = blkid;
1068 		rp->fr_nblks = nblks;
1069 		found = avl_find(tree, rp, &where);
1070 		ASSERT(found == NULL);
1071 		avl_insert(tree, rp, where);
1072 		dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1073 		    blkid, nblks, tx->tx_txg);
1074 	}
1075 	mutex_exit(&dn->dn_mtx);
1076 
1077 	dbuf_free_range(dn, blkid, nblks, tx);
1078 	dnode_setdirty(dn, tx);
1079 out:
1080 	rw_exit(&dn->dn_struct_rwlock);
1081 }
1082 
1083 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
1084 uint64_t
1085 dnode_block_freed(dnode_t *dn, uint64_t blkid)
1086 {
1087 	free_range_t range_tofind;
1088 	void *dp = spa_get_dsl(dn->dn_objset->os_spa);
1089 	int i;
1090 
1091 	if (blkid == DB_BONUS_BLKID)
1092 		return (FALSE);
1093 
1094 	/*
1095 	 * If we're in the process of opening the pool, dp will not be
1096 	 * set yet, but there shouldn't be anything dirty.
1097 	 */
1098 	if (dp == NULL)
1099 		return (FALSE);
1100 
1101 	if (dn->dn_free_txg)
1102 		return (TRUE);
1103 
1104 	/*
1105 	 * If dn_datablkshift is not set, then there's only a single
1106 	 * block, in which case there will never be a free range so it
1107 	 * won't matter.
1108 	 */
1109 	range_tofind.fr_blkid = blkid;
1110 	mutex_enter(&dn->dn_mtx);
1111 	for (i = 0; i < TXG_SIZE; i++) {
1112 		free_range_t *range_found;
1113 		avl_index_t idx;
1114 
1115 		range_found = avl_find(&dn->dn_ranges[i], &range_tofind, &idx);
1116 		if (range_found) {
1117 			ASSERT(range_found->fr_nblks > 0);
1118 			break;
1119 		}
1120 		range_found = avl_nearest(&dn->dn_ranges[i], idx, AVL_BEFORE);
1121 		if (range_found &&
1122 		    range_found->fr_blkid + range_found->fr_nblks > blkid)
1123 			break;
1124 	}
1125 	mutex_exit(&dn->dn_mtx);
1126 	return (i < TXG_SIZE);
1127 }
1128 
1129 /* call from syncing context when we actually write/free space for this dnode */
1130 void
1131 dnode_diduse_space(dnode_t *dn, int64_t delta)
1132 {
1133 	uint64_t space;
1134 	dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n",
1135 	    dn, dn->dn_phys,
1136 	    (u_longlong_t)dn->dn_phys->dn_used,
1137 	    (longlong_t)delta);
1138 
1139 	mutex_enter(&dn->dn_mtx);
1140 	space = DN_USED_BYTES(dn->dn_phys);
1141 	if (delta > 0) {
1142 		ASSERT3U(space + delta, >=, space); /* no overflow */
1143 	} else {
1144 		ASSERT3U(space, >=, -delta); /* no underflow */
1145 	}
1146 	space += delta;
1147 	if (spa_version(dn->dn_objset->os_spa) < ZFS_VERSION_DNODE_BYTES) {
1148 		ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0);
1149 		ASSERT3U(P2PHASE(space, 1<<DEV_BSHIFT), ==, 0);
1150 		dn->dn_phys->dn_used = space >> DEV_BSHIFT;
1151 	} else {
1152 		dn->dn_phys->dn_used = space;
1153 		dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES;
1154 	}
1155 	mutex_exit(&dn->dn_mtx);
1156 }
1157 
1158 /*
1159  * Call when we think we're going to write/free space in open context.
1160  * Be conservative (ie. OK to write less than this or free more than
1161  * this, but don't write more or free less).
1162  */
1163 void
1164 dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx)
1165 {
1166 	objset_impl_t *os = dn->dn_objset;
1167 	dsl_dataset_t *ds = os->os_dsl_dataset;
1168 
1169 	if (space > 0)
1170 		space = spa_get_asize(os->os_spa, space);
1171 
1172 	if (ds)
1173 		dsl_dir_willuse_space(ds->ds_dir, space, tx);
1174 
1175 	dmu_tx_willuse_space(tx, space);
1176 }
1177 
1178 static int
1179 dnode_next_offset_level(dnode_t *dn, boolean_t hole, uint64_t *offset,
1180 	int lvl, uint64_t blkfill)
1181 {
1182 	dmu_buf_impl_t *db = NULL;
1183 	void *data = NULL;
1184 	uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
1185 	uint64_t epb = 1ULL << epbs;
1186 	uint64_t minfill, maxfill;
1187 	int i, error, span;
1188 
1189 	dprintf("probing object %llu offset %llx level %d of %u\n",
1190 	    dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels);
1191 
1192 	if (lvl == dn->dn_phys->dn_nlevels) {
1193 		error = 0;
1194 		epb = dn->dn_phys->dn_nblkptr;
1195 		data = dn->dn_phys->dn_blkptr;
1196 	} else {
1197 		uint64_t blkid = dbuf_whichblock(dn, *offset) >> (epbs * lvl);
1198 		error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FTAG, &db);
1199 		if (error) {
1200 			if (error == ENOENT)
1201 				return (hole ? 0 : ESRCH);
1202 			return (error);
1203 		}
1204 		error = dbuf_read(db, NULL, DB_RF_CANFAIL | DB_RF_HAVESTRUCT);
1205 		if (error) {
1206 			dbuf_rele(db, FTAG);
1207 			return (error);
1208 		}
1209 		data = db->db.db_data;
1210 	}
1211 
1212 	if (lvl == 0) {
1213 		dnode_phys_t *dnp = data;
1214 		span = DNODE_SHIFT;
1215 		ASSERT(dn->dn_type == DMU_OT_DNODE);
1216 
1217 		for (i = (*offset >> span) & (blkfill - 1); i < blkfill; i++) {
1218 			if (!dnp[i].dn_type == hole)
1219 				break;
1220 			*offset += 1ULL << span;
1221 		}
1222 		if (i == blkfill)
1223 			error = ESRCH;
1224 	} else {
1225 		blkptr_t *bp = data;
1226 		span = (lvl - 1) * epbs + dn->dn_datablkshift;
1227 		minfill = 0;
1228 		maxfill = blkfill << ((lvl - 1) * epbs);
1229 
1230 		if (hole)
1231 			maxfill--;
1232 		else
1233 			minfill++;
1234 
1235 		for (i = (*offset >> span) & ((1ULL << epbs) - 1);
1236 		    i < epb; i++) {
1237 			if (bp[i].blk_fill >= minfill &&
1238 			    bp[i].blk_fill <= maxfill)
1239 				break;
1240 			*offset += 1ULL << span;
1241 		}
1242 		if (i >= epb)
1243 			error = ESRCH;
1244 	}
1245 
1246 	if (db)
1247 		dbuf_rele(db, FTAG);
1248 
1249 	return (error);
1250 }
1251 
1252 /*
1253  * Find the next hole, data, or sparse region at or after *offset.
1254  * The value 'blkfill' tells us how many items we expect to find
1255  * in an L0 data block; this value is 1 for normal objects,
1256  * DNODES_PER_BLOCK for the meta dnode, and some fraction of
1257  * DNODES_PER_BLOCK when searching for sparse regions thereof.
1258  * Examples:
1259  *
1260  * dnode_next_offset(dn, hole, offset, 1, 1);
1261  *	Finds the next hole/data in a file.
1262  *	Used in dmu_offset_next().
1263  *
1264  * dnode_next_offset(mdn, hole, offset, 0, DNODES_PER_BLOCK);
1265  *	Finds the next free/allocated dnode an objset's meta-dnode.
1266  *	Used in dmu_object_next().
1267  *
1268  * dnode_next_offset(mdn, TRUE, offset, 2, DNODES_PER_BLOCK >> 2);
1269  *	Finds the next L2 meta-dnode bp that's at most 1/4 full.
1270  *	Used in dmu_object_alloc().
1271  */
1272 int
1273 dnode_next_offset(dnode_t *dn, boolean_t hole, uint64_t *offset,
1274     int minlvl, uint64_t blkfill)
1275 {
1276 	int lvl, maxlvl;
1277 	int error = 0;
1278 	uint64_t initial_offset = *offset;
1279 
1280 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
1281 
1282 	if (dn->dn_phys->dn_nlevels == 0) {
1283 		rw_exit(&dn->dn_struct_rwlock);
1284 		return (ESRCH);
1285 	}
1286 
1287 	if (dn->dn_datablkshift == 0) {
1288 		if (*offset < dn->dn_datablksz) {
1289 			if (hole)
1290 				*offset = dn->dn_datablksz;
1291 		} else {
1292 			error = ESRCH;
1293 		}
1294 		rw_exit(&dn->dn_struct_rwlock);
1295 		return (error);
1296 	}
1297 
1298 	maxlvl = dn->dn_phys->dn_nlevels;
1299 
1300 	for (lvl = minlvl; lvl <= maxlvl; lvl++) {
1301 		error = dnode_next_offset_level(dn, hole, offset, lvl, blkfill);
1302 		if (error != ESRCH)
1303 			break;
1304 	}
1305 
1306 	while (--lvl >= minlvl && error == 0)
1307 		error = dnode_next_offset_level(dn, hole, offset, lvl, blkfill);
1308 
1309 	rw_exit(&dn->dn_struct_rwlock);
1310 
1311 	if (error == 0 && initial_offset > *offset)
1312 		error = ESRCH;
1313 
1314 	return (error);
1315 }
1316