xref: /titanic_44/usr/src/uts/common/fs/zfs/dnode.c (revision 9df12a23948bd40cbe37ce88d84e272c3894e675)
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_type = BSWAP_8(dnp->dn_type);
181 	dnp->dn_indblkshift = BSWAP_8(dnp->dn_indblkshift);
182 	dnp->dn_nlevels = BSWAP_8(dnp->dn_nlevels);
183 	dnp->dn_nblkptr = BSWAP_8(dnp->dn_nblkptr);
184 	dnp->dn_bonustype = BSWAP_8(dnp->dn_bonustype);
185 	dnp->dn_checksum = BSWAP_8(dnp->dn_checksum);
186 	dnp->dn_compress = BSWAP_8(dnp->dn_compress);
187 	dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec);
188 	dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen);
189 	dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid);
190 	dnp->dn_secphys = BSWAP_64(dnp->dn_secphys);
191 
192 	/*
193 	 * dn_nblkptr is only one byte, so it's OK to read it in either
194 	 * byte order.  We can't read dn_bouslen.
195 	 */
196 	ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT);
197 	ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR);
198 	for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++)
199 		buf64[i] = BSWAP_64(buf64[i]);
200 
201 	/*
202 	 * OK to check dn_bonuslen for zero, because it won't matter if
203 	 * we have the wrong byte order.  This is necessary because the
204 	 * dnode dnode is smaller than a regular dnode.
205 	 */
206 	if (dnp->dn_bonuslen != 0) {
207 		/*
208 		 * Note that the bonus length calculated here may be
209 		 * longer than the actual bonus buffer.  This is because
210 		 * we always put the bonus buffer after the last block
211 		 * pointer (instead of packing it against the end of the
212 		 * dnode buffer).
213 		 */
214 		int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t);
215 		size_t len = DN_MAX_BONUSLEN - off;
216 		dmu_ot[dnp->dn_bonustype].ot_byteswap(dnp->dn_bonus + off, len);
217 	}
218 }
219 
220 void
221 dnode_buf_byteswap(void *vbuf, size_t size)
222 {
223 	dnode_phys_t *buf = vbuf;
224 	int i;
225 
226 	ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT));
227 	ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0);
228 
229 	size >>= DNODE_SHIFT;
230 	for (i = 0; i < size; i++) {
231 		dnode_byteswap(buf);
232 		buf++;
233 	}
234 }
235 
236 static int
237 free_range_compar(const void *node1, const void *node2)
238 {
239 	const free_range_t *rp1 = node1;
240 	const free_range_t *rp2 = node2;
241 
242 	if (rp1->fr_blkid < rp2->fr_blkid)
243 		return (-1);
244 	else if (rp1->fr_blkid > rp2->fr_blkid)
245 		return (1);
246 	else return (0);
247 }
248 
249 static void
250 dnode_setdblksz(dnode_t *dn, int size)
251 {
252 	ASSERT3U(P2PHASE(size, SPA_MINBLOCKSIZE), ==, 0);
253 	ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
254 	ASSERT3U(size, >=, SPA_MINBLOCKSIZE);
255 	ASSERT3U(size >> SPA_MINBLOCKSHIFT, <,
256 	    1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8));
257 	dn->dn_datablksz = size;
258 	dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT;
259 	dn->dn_datablkshift = ISP2(size) ? highbit(size - 1) : 0;
260 }
261 
262 static dnode_t *
263 dnode_create(objset_impl_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db,
264     uint64_t object)
265 {
266 	dnode_t *dn = kmem_cache_alloc(dnode_cache, KM_SLEEP);
267 	(void) dnode_cons(dn, NULL, 0); /* XXX */
268 
269 	dn->dn_objset = os;
270 	dn->dn_object = object;
271 	dn->dn_dbuf = db;
272 	dn->dn_phys = dnp;
273 
274 	if (dnp->dn_datablkszsec)
275 		dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
276 	dn->dn_indblkshift = dnp->dn_indblkshift;
277 	dn->dn_nlevels = dnp->dn_nlevels;
278 	dn->dn_type = dnp->dn_type;
279 	dn->dn_nblkptr = dnp->dn_nblkptr;
280 	dn->dn_checksum = dnp->dn_checksum;
281 	dn->dn_compress = dnp->dn_compress;
282 	dn->dn_bonustype = dnp->dn_bonustype;
283 	dn->dn_bonuslen = dnp->dn_bonuslen;
284 	dn->dn_maxblkid = dnp->dn_maxblkid;
285 
286 	dmu_zfetch_init(&dn->dn_zfetch, dn);
287 
288 	ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES);
289 	mutex_enter(&os->os_lock);
290 	list_insert_head(&os->os_dnodes, dn);
291 	mutex_exit(&os->os_lock);
292 
293 	return (dn);
294 }
295 
296 static void
297 dnode_destroy(dnode_t *dn)
298 {
299 	objset_impl_t *os = dn->dn_objset;
300 
301 	mutex_enter(&os->os_lock);
302 	list_remove(&os->os_dnodes, dn);
303 	mutex_exit(&os->os_lock);
304 
305 	if (dn->dn_dirtyctx_firstset) {
306 		kmem_free(dn->dn_dirtyctx_firstset, 1);
307 		dn->dn_dirtyctx_firstset = NULL;
308 	}
309 	dmu_zfetch_rele(&dn->dn_zfetch);
310 	if (dn->dn_bonus) {
311 		mutex_enter(&dn->dn_bonus->db_mtx);
312 		dbuf_evict(dn->dn_bonus);
313 		dn->dn_bonus = NULL;
314 	}
315 	kmem_cache_free(dnode_cache, dn);
316 }
317 
318 void
319 dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
320     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
321 {
322 	int i;
323 
324 	if (blocksize == 0)
325 		blocksize = 1 << zfs_default_bs;
326 	else if (blocksize > SPA_MAXBLOCKSIZE)
327 		blocksize = SPA_MAXBLOCKSIZE;
328 	else
329 		blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE);
330 
331 	if (ibs == 0)
332 		ibs = zfs_default_ibs;
333 
334 	ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT);
335 
336 	dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset,
337 	    dn->dn_object, tx->tx_txg, blocksize, ibs);
338 
339 	ASSERT(dn->dn_type == DMU_OT_NONE);
340 	ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0);
341 	ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE);
342 	ASSERT(ot != DMU_OT_NONE);
343 	ASSERT3U(ot, <, DMU_OT_NUMTYPES);
344 	ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
345 	    (bonustype != DMU_OT_NONE && bonuslen != 0));
346 	ASSERT3U(bonustype, <, DMU_OT_NUMTYPES);
347 	ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
348 	ASSERT(dn->dn_type == DMU_OT_NONE);
349 	ASSERT3U(dn->dn_maxblkid, ==, 0);
350 	ASSERT3U(dn->dn_allocated_txg, ==, 0);
351 	ASSERT3U(dn->dn_assigned_txg, ==, 0);
352 	ASSERT(refcount_is_zero(&dn->dn_tx_holds));
353 	ASSERT3U(refcount_count(&dn->dn_holds), <=, 1);
354 	ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
355 
356 	for (i = 0; i < TXG_SIZE; i++) {
357 		ASSERT3U(dn->dn_next_nlevels[i], ==, 0);
358 		ASSERT3U(dn->dn_next_indblkshift[i], ==, 0);
359 		ASSERT3U(dn->dn_next_blksz[i], ==, 0);
360 		ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
361 		ASSERT3P(list_head(&dn->dn_dirty_dbufs[i]), ==, NULL);
362 		ASSERT3U(avl_numnodes(&dn->dn_ranges[i]), ==, 0);
363 	}
364 
365 	dn->dn_type = ot;
366 	dnode_setdblksz(dn, blocksize);
367 	dn->dn_indblkshift = ibs;
368 	dn->dn_nlevels = 1;
369 	dn->dn_nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
370 	dn->dn_bonustype = bonustype;
371 	dn->dn_bonuslen = bonuslen;
372 	dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
373 	dn->dn_compress = ZIO_COMPRESS_INHERIT;
374 	dn->dn_dirtyctx = 0;
375 
376 	dn->dn_free_txg = 0;
377 	if (dn->dn_dirtyctx_firstset) {
378 		kmem_free(dn->dn_dirtyctx_firstset, 1);
379 		dn->dn_dirtyctx_firstset = NULL;
380 	}
381 
382 	dn->dn_allocated_txg = tx->tx_txg;
383 
384 	dnode_setdirty(dn, tx);
385 	dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs;
386 	dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz;
387 }
388 
389 void
390 dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
391     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
392 {
393 	int i;
394 
395 	ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE);
396 	ASSERT3U(blocksize, <=, SPA_MAXBLOCKSIZE);
397 	ASSERT3U(blocksize % SPA_MINBLOCKSIZE, ==, 0);
398 	ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
399 	ASSERT(tx->tx_txg != 0);
400 	ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
401 	    (bonustype != DMU_OT_NONE && bonuslen != 0));
402 	ASSERT3U(bonustype, <, DMU_OT_NUMTYPES);
403 	ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
404 
405 	for (i = 0; i < TXG_SIZE; i++)
406 		ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
407 
408 	/* clean up any unreferenced dbufs */
409 	dnode_evict_dbufs(dn);
410 	ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
411 
412 	/*
413 	 * XXX I should really have a generation number to tell if we
414 	 * need to do this...
415 	 */
416 	if (blocksize != dn->dn_datablksz ||
417 	    dn->dn_bonustype != bonustype || dn->dn_bonuslen != bonuslen) {
418 		/* free all old data */
419 		dnode_free_range(dn, 0, -1ULL, tx);
420 	}
421 
422 	/* change blocksize */
423 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
424 	dnode_setdblksz(dn, blocksize);
425 	dnode_setdirty(dn, tx);
426 	dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize;
427 	rw_exit(&dn->dn_struct_rwlock);
428 
429 	/* change type */
430 	dn->dn_type = ot;
431 
432 	if (dn->dn_bonuslen != bonuslen) {
433 		dmu_buf_impl_t *db = NULL;
434 
435 		/* change bonus size */
436 		if (bonuslen == 0)
437 			bonuslen = 1; /* XXX */
438 		rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
439 		if (dn->dn_bonus == NULL)
440 			dn->dn_bonus = dbuf_create_bonus(dn);
441 		db = dn->dn_bonus;
442 		rw_exit(&dn->dn_struct_rwlock);
443 		if (refcount_add(&db->db_holds, FTAG) == 1)
444 			dnode_add_ref(dn, db);
445 		mutex_enter(&db->db_mtx);
446 		ASSERT3U(db->db.db_size, ==, dn->dn_bonuslen);
447 		ASSERT(db->db.db_data != NULL);
448 		db->db.db_size = bonuslen;
449 		mutex_exit(&db->db_mtx);
450 		dbuf_dirty(db, tx);
451 		dbuf_rele(db, FTAG);
452 	}
453 
454 	/* change bonus size and type */
455 	mutex_enter(&dn->dn_mtx);
456 	dn->dn_bonustype = bonustype;
457 	dn->dn_bonuslen = bonuslen;
458 	dn->dn_nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
459 	dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
460 	dn->dn_compress = ZIO_COMPRESS_INHERIT;
461 	ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
462 
463 	dn->dn_allocated_txg = tx->tx_txg;
464 	mutex_exit(&dn->dn_mtx);
465 }
466 
467 void
468 dnode_special_close(dnode_t *dn)
469 {
470 	/*
471 	 * Wait for final references to the dnode to clear.  This can
472 	 * only happen if the arc is asyncronously evicting state that
473 	 * has a hold on this dnode while we are trying to evict this
474 	 * dnode.
475 	 */
476 	while (refcount_count(&dn->dn_holds) > 0)
477 		delay(1);
478 	dnode_destroy(dn);
479 }
480 
481 dnode_t *
482 dnode_special_open(objset_impl_t *os, dnode_phys_t *dnp, uint64_t object)
483 {
484 	dnode_t *dn = dnode_create(os, dnp, NULL, object);
485 	DNODE_VERIFY(dn);
486 	return (dn);
487 }
488 
489 static void
490 dnode_buf_pageout(dmu_buf_t *db, void *arg)
491 {
492 	dnode_t **children_dnodes = arg;
493 	int i;
494 	int epb = db->db_size >> DNODE_SHIFT;
495 
496 	for (i = 0; i < epb; i++) {
497 		dnode_t *dn = children_dnodes[i];
498 		int n;
499 
500 		if (dn == NULL)
501 			continue;
502 #ifdef ZFS_DEBUG
503 		/*
504 		 * If there are holds on this dnode, then there should
505 		 * be holds on the dnode's containing dbuf as well; thus
506 		 * it wouldn't be eligable for eviction and this function
507 		 * would not have been called.
508 		 */
509 		ASSERT(refcount_is_zero(&dn->dn_holds));
510 		ASSERT(list_head(&dn->dn_dbufs) == NULL);
511 		ASSERT(refcount_is_zero(&dn->dn_tx_holds));
512 
513 		for (n = 0; n < TXG_SIZE; n++)
514 			ASSERT(!list_link_active(&dn->dn_dirty_link[n]));
515 #endif
516 		children_dnodes[i] = NULL;
517 		dnode_destroy(dn);
518 	}
519 	kmem_free(children_dnodes, epb * sizeof (dnode_t *));
520 }
521 
522 /*
523  * errors:
524  * EINVAL - invalid object number.
525  * EIO - i/o error.
526  * succeeds even for free dnodes.
527  */
528 int
529 dnode_hold_impl(objset_impl_t *os, uint64_t object, int flag,
530     void *tag, dnode_t **dnp)
531 {
532 	int epb, idx, err;
533 	int drop_struct_lock = FALSE;
534 	int type;
535 	uint64_t blk;
536 	dnode_t *mdn, *dn;
537 	dmu_buf_impl_t *db;
538 	dnode_t **children_dnodes;
539 
540 	if (object == 0 || object >= DN_MAX_OBJECT)
541 		return (EINVAL);
542 
543 	mdn = os->os_meta_dnode;
544 
545 	DNODE_VERIFY(mdn);
546 
547 	if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) {
548 		rw_enter(&mdn->dn_struct_rwlock, RW_READER);
549 		drop_struct_lock = TRUE;
550 	}
551 
552 	blk = dbuf_whichblock(mdn, object * sizeof (dnode_phys_t));
553 
554 	db = dbuf_hold(mdn, blk, FTAG);
555 	if (drop_struct_lock)
556 		rw_exit(&mdn->dn_struct_rwlock);
557 	if (db == NULL)
558 		return (EIO);
559 	err = dbuf_read(db, NULL, DB_RF_CANFAIL);
560 	if (err) {
561 		dbuf_rele(db, FTAG);
562 		return (err);
563 	}
564 
565 	ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT);
566 	epb = db->db.db_size >> DNODE_SHIFT;
567 
568 	idx = object & (epb-1);
569 
570 	children_dnodes = dmu_buf_get_user(&db->db);
571 	if (children_dnodes == NULL) {
572 		dnode_t **winner;
573 		children_dnodes = kmem_zalloc(epb * sizeof (dnode_t *),
574 		    KM_SLEEP);
575 		if (winner = dmu_buf_set_user(&db->db, children_dnodes, NULL,
576 		    dnode_buf_pageout)) {
577 			kmem_free(children_dnodes, epb * sizeof (dnode_t *));
578 			children_dnodes = winner;
579 		}
580 	}
581 
582 	if ((dn = children_dnodes[idx]) == NULL) {
583 		dnode_t *winner;
584 		dn = dnode_create(os, (dnode_phys_t *)db->db.db_data+idx,
585 			db, object);
586 		winner = atomic_cas_ptr(&children_dnodes[idx], NULL, dn);
587 		if (winner != NULL) {
588 			dnode_destroy(dn);
589 			dn = winner;
590 		}
591 	}
592 
593 	mutex_enter(&dn->dn_mtx);
594 	type = dn->dn_type;
595 	if (dn->dn_free_txg ||
596 	    ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) ||
597 	    ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)) {
598 		mutex_exit(&dn->dn_mtx);
599 		dbuf_rele(db, FTAG);
600 		return (type == DMU_OT_NONE ? ENOENT : EEXIST);
601 	}
602 	mutex_exit(&dn->dn_mtx);
603 
604 	if (refcount_add(&dn->dn_holds, tag) == 1)
605 		dbuf_add_ref(db, dn);
606 
607 	DNODE_VERIFY(dn);
608 	ASSERT3P(dn->dn_dbuf, ==, db);
609 	ASSERT3U(dn->dn_object, ==, object);
610 	dbuf_rele(db, FTAG);
611 
612 	*dnp = dn;
613 	return (0);
614 }
615 
616 /*
617  * Return held dnode if the object is allocated, NULL if not.
618  */
619 int
620 dnode_hold(objset_impl_t *os, uint64_t object, void *tag, dnode_t **dnp)
621 {
622 	return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, tag, dnp));
623 }
624 
625 void
626 dnode_add_ref(dnode_t *dn, void *tag)
627 {
628 	ASSERT(refcount_count(&dn->dn_holds) > 0);
629 	(void) refcount_add(&dn->dn_holds, tag);
630 }
631 
632 void
633 dnode_rele(dnode_t *dn, void *tag)
634 {
635 	uint64_t refs;
636 
637 	refs = refcount_remove(&dn->dn_holds, tag);
638 	/* NOTE: the DNODE_DNODE does not have a dn_dbuf */
639 	if (refs == 0 && dn->dn_dbuf)
640 		dbuf_rele(dn->dn_dbuf, dn);
641 }
642 
643 void
644 dnode_setdirty(dnode_t *dn, dmu_tx_t *tx)
645 {
646 	objset_impl_t *os = dn->dn_objset;
647 	uint64_t txg = tx->tx_txg;
648 
649 	if (dn->dn_object == DMU_META_DNODE_OBJECT)
650 		return;
651 
652 	DNODE_VERIFY(dn);
653 
654 #ifdef ZFS_DEBUG
655 	mutex_enter(&dn->dn_mtx);
656 	ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg);
657 	/* ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg); */
658 	mutex_exit(&dn->dn_mtx);
659 #endif
660 
661 	mutex_enter(&os->os_lock);
662 
663 	/*
664 	 * If we are already marked dirty, we're done.
665 	 */
666 	if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) {
667 		mutex_exit(&os->os_lock);
668 		return;
669 	}
670 
671 	ASSERT(!refcount_is_zero(&dn->dn_holds) || list_head(&dn->dn_dbufs));
672 	ASSERT(dn->dn_datablksz != 0);
673 	ASSERT3U(dn->dn_next_blksz[txg&TXG_MASK], ==, 0);
674 
675 	dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
676 	    dn->dn_object, txg);
677 
678 	if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) {
679 		list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn);
680 	} else {
681 		list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn);
682 	}
683 
684 	mutex_exit(&os->os_lock);
685 
686 	/*
687 	 * The dnode maintains a hold on its containing dbuf as
688 	 * long as there are holds on it.  Each instantiated child
689 	 * dbuf maintaines a hold on the dnode.  When the last child
690 	 * drops its hold, the dnode will drop its hold on the
691 	 * containing dbuf. We add a "dirty hold" here so that the
692 	 * dnode will hang around after we finish processing its
693 	 * children.
694 	 */
695 	dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg);
696 
697 	dbuf_dirty(dn->dn_dbuf, tx);
698 
699 	dsl_dataset_dirty(os->os_dsl_dataset, tx);
700 }
701 
702 void
703 dnode_free(dnode_t *dn, dmu_tx_t *tx)
704 {
705 	int txgoff = tx->tx_txg & TXG_MASK;
706 
707 	dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg);
708 
709 	/* we should be the only holder... hopefully */
710 	/* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */
711 
712 	mutex_enter(&dn->dn_mtx);
713 	if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) {
714 		mutex_exit(&dn->dn_mtx);
715 		return;
716 	}
717 	dn->dn_free_txg = tx->tx_txg;
718 	mutex_exit(&dn->dn_mtx);
719 
720 	/*
721 	 * If the dnode is already dirty, it needs to be moved from
722 	 * the dirty list to the free list.
723 	 */
724 	mutex_enter(&dn->dn_objset->os_lock);
725 	if (list_link_active(&dn->dn_dirty_link[txgoff])) {
726 		list_remove(&dn->dn_objset->os_dirty_dnodes[txgoff], dn);
727 		list_insert_tail(&dn->dn_objset->os_free_dnodes[txgoff], dn);
728 		mutex_exit(&dn->dn_objset->os_lock);
729 	} else {
730 		mutex_exit(&dn->dn_objset->os_lock);
731 		dnode_setdirty(dn, tx);
732 	}
733 }
734 
735 /*
736  * Try to change the block size for the indicated dnode.  This can only
737  * succeed if there are no blocks allocated or dirty beyond first block
738  */
739 int
740 dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx)
741 {
742 	dmu_buf_impl_t *db, *db_next;
743 	int have_db0 = FALSE;
744 	int err = ENOTSUP;
745 
746 	if (size == 0)
747 		size = SPA_MINBLOCKSIZE;
748 	if (size > SPA_MAXBLOCKSIZE)
749 		size = SPA_MAXBLOCKSIZE;
750 	else
751 		size = P2ROUNDUP(size, SPA_MINBLOCKSIZE);
752 
753 	if (ibs == 0)
754 		ibs = dn->dn_indblkshift;
755 
756 	if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec &&
757 	    ibs == dn->dn_indblkshift)
758 		return (0);
759 
760 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
761 
762 	/* Check for any allocated blocks beyond the first */
763 	if (dn->dn_phys->dn_maxblkid != 0)
764 		goto end;
765 
766 	mutex_enter(&dn->dn_dbufs_mtx);
767 	for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
768 		db_next = list_next(&dn->dn_dbufs, db);
769 
770 		if (db->db_blkid == 0) {
771 			have_db0 = TRUE;
772 		} else if (db->db_blkid != DB_BONUS_BLKID) {
773 			mutex_exit(&dn->dn_dbufs_mtx);
774 			goto end;
775 		}
776 	}
777 	mutex_exit(&dn->dn_dbufs_mtx);
778 
779 	db = NULL;
780 	if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) || have_db0) {
781 		/* obtain the old block */
782 		db = dbuf_hold(dn, 0, FTAG);
783 		dbuf_new_size(db, size, tx);
784 	}
785 
786 	dnode_setdblksz(dn, size);
787 	dn->dn_indblkshift = ibs;
788 	dnode_setdirty(dn, tx);
789 	dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size;
790 	dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs;
791 
792 	if (db)
793 		dbuf_rele(db, FTAG);
794 
795 	err = 0;
796 end:
797 	rw_exit(&dn->dn_struct_rwlock);
798 	return (err);
799 }
800 
801 uint64_t
802 dnode_max_nonzero_offset(dnode_t *dn)
803 {
804 	if (dn->dn_phys->dn_maxblkid == 0 &&
805 	    BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]))
806 		return (0);
807 	else
808 		return ((dn->dn_phys->dn_maxblkid+1) * dn->dn_datablksz);
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 start, objsize, blkid, nblks;
919 	int blkshift, blksz, tail, head, epbs;
920 	int trunc = FALSE;
921 
922 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
923 	blksz = dn->dn_datablksz;
924 	blkshift = dn->dn_datablkshift;
925 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
926 
927 	/* If the range is past the end of the file, this is a no-op */
928 	objsize = blksz * (dn->dn_maxblkid+1);
929 	if (off >= objsize)
930 		goto out;
931 	if (len == -1ULL) {
932 		len = UINT64_MAX - off;
933 		trunc = TRUE;
934 	}
935 
936 	/*
937 	 * First, block align the region to free:
938 	 */
939 	if (dn->dn_maxblkid == 0) {
940 		if (off == 0) {
941 			head = 0;
942 		} else {
943 			head = blksz - off;
944 			ASSERT3U(head, >, 0);
945 		}
946 		start = off;
947 	} else {
948 		ASSERT(ISP2(blksz));
949 		head = P2NPHASE(off, blksz);
950 		start = P2PHASE(off, blksz);
951 	}
952 	/* zero out any partial block data at the start of the range */
953 	if (head) {
954 		ASSERT3U(start + head, ==, blksz);
955 		if (len < head)
956 			head = len;
957 		if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off), TRUE,
958 		    FTAG, &db) == 0) {
959 			caddr_t data;
960 
961 			/* don't dirty if it isn't on disk and isn't dirty */
962 			if (db->db_dirtied ||
963 			    (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
964 				rw_exit(&dn->dn_struct_rwlock);
965 				dbuf_will_dirty(db, tx);
966 				rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
967 				data = db->db.db_data;
968 				bzero(data + start, head);
969 			}
970 			dbuf_rele(db, FTAG);
971 		}
972 		off += head;
973 		len -= head;
974 	}
975 	/* If the range was less than one block, we are done */
976 	if (len == 0)
977 		goto out;
978 
979 	/* If the remaining range is past the end of the file, we are done */
980 	if (off > dn->dn_maxblkid << blkshift)
981 		goto out;
982 
983 	if (off + len == UINT64_MAX)
984 		tail = 0;
985 	else
986 		tail = P2PHASE(len, blksz);
987 
988 	ASSERT3U(P2PHASE(off, blksz), ==, 0);
989 	/* zero out any partial block data at the end of the range */
990 	if (tail) {
991 		if (len < tail)
992 			tail = len;
993 		if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len),
994 		    TRUE, FTAG, &db) == 0) {
995 			/* don't dirty if it isn't on disk and isn't dirty */
996 			if (db->db_dirtied ||
997 			    (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
998 				rw_exit(&dn->dn_struct_rwlock);
999 				dbuf_will_dirty(db, tx);
1000 				rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1001 				bzero(db->db.db_data, tail);
1002 			}
1003 			dbuf_rele(db, FTAG);
1004 		}
1005 		len -= tail;
1006 	}
1007 	/* If the range did not include a full block, we are done */
1008 	if (len == 0)
1009 		goto out;
1010 
1011 	/* dirty the left indirects */
1012 	if (dn->dn_nlevels > 1 && off != 0) {
1013 		db = dbuf_hold_level(dn, 1,
1014 		    (off - head) >> (blkshift + epbs), FTAG);
1015 		dbuf_will_dirty(db, tx);
1016 		dbuf_rele(db, FTAG);
1017 	}
1018 
1019 	/* dirty the right indirects */
1020 	if (dn->dn_nlevels > 1 && !trunc) {
1021 		db = dbuf_hold_level(dn, 1,
1022 		    (off + len + tail - 1) >> (blkshift + epbs), FTAG);
1023 		dbuf_will_dirty(db, tx);
1024 		dbuf_rele(db, FTAG);
1025 	}
1026 
1027 	/*
1028 	 * Finally, add this range to the dnode range list, we
1029 	 * will finish up this free operation in the syncing phase.
1030 	 */
1031 	ASSERT(IS_P2ALIGNED(off, 1<<blkshift));
1032 	ASSERT(off + len == UINT64_MAX || IS_P2ALIGNED(len, 1<<blkshift));
1033 	blkid = off >> blkshift;
1034 	nblks = len >> blkshift;
1035 
1036 	if (trunc)
1037 		dn->dn_maxblkid = (blkid ? blkid - 1 : 0);
1038 
1039 	mutex_enter(&dn->dn_mtx);
1040 	dnode_clear_range(dn, blkid, nblks, tx);
1041 	{
1042 		free_range_t *rp, *found;
1043 		avl_index_t where;
1044 		avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
1045 
1046 		/* Add new range to dn_ranges */
1047 		rp = kmem_alloc(sizeof (free_range_t), KM_SLEEP);
1048 		rp->fr_blkid = blkid;
1049 		rp->fr_nblks = nblks;
1050 		found = avl_find(tree, rp, &where);
1051 		ASSERT(found == NULL);
1052 		avl_insert(tree, rp, where);
1053 		dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1054 		    blkid, nblks, tx->tx_txg);
1055 	}
1056 	mutex_exit(&dn->dn_mtx);
1057 
1058 	dbuf_free_range(dn, blkid, nblks, tx);
1059 	dnode_setdirty(dn, tx);
1060 out:
1061 	rw_exit(&dn->dn_struct_rwlock);
1062 }
1063 
1064 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
1065 uint64_t
1066 dnode_block_freed(dnode_t *dn, uint64_t blkid)
1067 {
1068 	free_range_t range_tofind;
1069 	void *dp = spa_get_dsl(dn->dn_objset->os_spa);
1070 	int i;
1071 
1072 	if (blkid == DB_BONUS_BLKID)
1073 		return (FALSE);
1074 
1075 	/*
1076 	 * If we're in the process of opening the pool, dp will not be
1077 	 * set yet, but there shouldn't be anything dirty.
1078 	 */
1079 	if (dp == NULL)
1080 		return (FALSE);
1081 
1082 	if (dn->dn_free_txg)
1083 		return (TRUE);
1084 
1085 	/*
1086 	 * If dn_datablkshift is not set, then there's only a single
1087 	 * block, in which case there will never be a free range so it
1088 	 * won't matter.
1089 	 */
1090 	range_tofind.fr_blkid = blkid;
1091 	mutex_enter(&dn->dn_mtx);
1092 	for (i = 0; i < TXG_SIZE; i++) {
1093 		free_range_t *range_found;
1094 		avl_index_t idx;
1095 
1096 		range_found = avl_find(&dn->dn_ranges[i], &range_tofind, &idx);
1097 		if (range_found) {
1098 			ASSERT(range_found->fr_nblks > 0);
1099 			break;
1100 		}
1101 		range_found = avl_nearest(&dn->dn_ranges[i], idx, AVL_BEFORE);
1102 		if (range_found &&
1103 		    range_found->fr_blkid + range_found->fr_nblks > blkid)
1104 			break;
1105 	}
1106 	mutex_exit(&dn->dn_mtx);
1107 	return (i < TXG_SIZE);
1108 }
1109 
1110 /* call from syncing context when we actually write/free space for this dnode */
1111 void
1112 dnode_diduse_space(dnode_t *dn, int64_t space)
1113 {
1114 	uint64_t sectors;
1115 
1116 	dprintf_dnode(dn, "dn=%p dnp=%p secphys=%llu space=%lld\n",
1117 	    dn, dn->dn_phys,
1118 	    (u_longlong_t)dn->dn_phys->dn_secphys,
1119 	    (longlong_t)space);
1120 
1121 	ASSERT(P2PHASE(space, 1<<DEV_BSHIFT) == 0);
1122 
1123 	mutex_enter(&dn->dn_mtx);
1124 	if (space > 0) {
1125 		sectors = space >> DEV_BSHIFT;
1126 		ASSERT3U(dn->dn_phys->dn_secphys + sectors, >=,
1127 		    dn->dn_phys->dn_secphys);
1128 		dn->dn_phys->dn_secphys += sectors;
1129 	} else {
1130 		sectors = -space >> DEV_BSHIFT;
1131 		ASSERT3U(dn->dn_phys->dn_secphys, >=, sectors);
1132 		dn->dn_phys->dn_secphys -= sectors;
1133 	}
1134 	mutex_exit(&dn->dn_mtx);
1135 }
1136 
1137 /*
1138  * Call when we think we're going to write/free space in open context.
1139  * Be conservative (ie. OK to write less than this or free more than
1140  * this, but don't write more or free less).
1141  */
1142 void
1143 dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx)
1144 {
1145 	objset_impl_t *os = dn->dn_objset;
1146 	dsl_dataset_t *ds = os->os_dsl_dataset;
1147 
1148 	if (space > 0)
1149 		space = spa_get_asize(os->os_spa, space);
1150 
1151 	if (ds)
1152 		dsl_dir_willuse_space(ds->ds_dir, space, tx);
1153 
1154 	dmu_tx_willuse_space(tx, space);
1155 }
1156 
1157 static int
1158 dnode_next_offset_level(dnode_t *dn, boolean_t hole, uint64_t *offset,
1159 	int lvl, uint64_t blkfill)
1160 {
1161 	dmu_buf_impl_t *db = NULL;
1162 	void *data = NULL;
1163 	uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
1164 	uint64_t epb = 1ULL << epbs;
1165 	uint64_t minfill, maxfill;
1166 	int i, error, span;
1167 
1168 	dprintf("probing object %llu offset %llx level %d of %u\n",
1169 	    dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels);
1170 
1171 	if (lvl == dn->dn_phys->dn_nlevels) {
1172 		error = 0;
1173 		epb = dn->dn_phys->dn_nblkptr;
1174 		data = dn->dn_phys->dn_blkptr;
1175 	} else {
1176 		uint64_t blkid = dbuf_whichblock(dn, *offset) >> (epbs * lvl);
1177 		error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FTAG, &db);
1178 		if (error) {
1179 			if (error == ENOENT)
1180 				return (hole ? 0 : ESRCH);
1181 			return (error);
1182 		}
1183 		(void) dbuf_read(db, NULL,
1184 		    DB_RF_MUST_SUCCEED | DB_RF_HAVESTRUCT);
1185 		data = db->db.db_data;
1186 	}
1187 
1188 	if (lvl == 0) {
1189 		dnode_phys_t *dnp = data;
1190 		span = DNODE_SHIFT;
1191 		ASSERT(dn->dn_type == DMU_OT_DNODE);
1192 
1193 		for (i = (*offset >> span) & (blkfill - 1); i < blkfill; i++) {
1194 			if (!dnp[i].dn_type == hole)
1195 				break;
1196 			*offset += 1ULL << span;
1197 		}
1198 		if (i == blkfill)
1199 			error = ESRCH;
1200 	} else {
1201 		blkptr_t *bp = data;
1202 		span = (lvl - 1) * epbs + dn->dn_datablkshift;
1203 		minfill = 0;
1204 		maxfill = blkfill << ((lvl - 1) * epbs);
1205 
1206 		if (hole)
1207 			maxfill--;
1208 		else
1209 			minfill++;
1210 
1211 		for (i = (*offset >> span) & ((1ULL << epbs) - 1);
1212 		    i < epb; i++) {
1213 			if (bp[i].blk_fill >= minfill &&
1214 			    bp[i].blk_fill <= maxfill)
1215 				break;
1216 			*offset += 1ULL << span;
1217 		}
1218 		if (i >= epb)
1219 			error = ESRCH;
1220 	}
1221 
1222 	if (db)
1223 		dbuf_rele(db, FTAG);
1224 
1225 	return (error);
1226 }
1227 
1228 /*
1229  * Find the next hole, data, or sparse region at or after *offset.
1230  * The value 'blkfill' tells us how many items we expect to find
1231  * in an L0 data block; this value is 1 for normal objects,
1232  * DNODES_PER_BLOCK for the meta dnode, and some fraction of
1233  * DNODES_PER_BLOCK when searching for sparse regions thereof.
1234  * Examples:
1235  *
1236  * dnode_next_offset(dn, hole, offset, 1, 1);
1237  *	Finds the next hole/data in a file.
1238  *	Used in dmu_offset_next().
1239  *
1240  * dnode_next_offset(mdn, hole, offset, 0, DNODES_PER_BLOCK);
1241  *	Finds the next free/allocated dnode an objset's meta-dnode.
1242  *	Used in dmu_object_next().
1243  *
1244  * dnode_next_offset(mdn, TRUE, offset, 2, DNODES_PER_BLOCK >> 2);
1245  *	Finds the next L2 meta-dnode bp that's at most 1/4 full.
1246  *	Used in dmu_object_alloc().
1247  */
1248 int
1249 dnode_next_offset(dnode_t *dn, boolean_t hole, uint64_t *offset,
1250     int minlvl, uint64_t blkfill)
1251 {
1252 	int lvl, maxlvl;
1253 	int error = 0;
1254 	uint64_t initial_offset = *offset;
1255 
1256 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
1257 
1258 	if (dn->dn_phys->dn_nlevels == 0) {
1259 		rw_exit(&dn->dn_struct_rwlock);
1260 		return (ESRCH);
1261 	}
1262 
1263 	if (dn->dn_datablkshift == 0) {
1264 		if (*offset < dn->dn_datablksz) {
1265 			if (hole)
1266 				*offset = dn->dn_datablksz;
1267 		} else {
1268 			error = ESRCH;
1269 		}
1270 		rw_exit(&dn->dn_struct_rwlock);
1271 		return (error);
1272 	}
1273 
1274 	maxlvl = dn->dn_phys->dn_nlevels;
1275 
1276 	for (lvl = minlvl; lvl <= maxlvl; lvl++) {
1277 		error = dnode_next_offset_level(dn, hole, offset, lvl, blkfill);
1278 		if (error == 0)
1279 			break;
1280 	}
1281 
1282 	while (--lvl >= minlvl && error == 0)
1283 		error = dnode_next_offset_level(dn, hole, offset, lvl, blkfill);
1284 
1285 	rw_exit(&dn->dn_struct_rwlock);
1286 
1287 	if (initial_offset > *offset)
1288 		return (ESRCH);
1289 
1290 	return (error);
1291 }
1292