xref: /titanic_50/usr/src/uts/common/fs/zfs/dnode.c (revision ea8dc4b6d2251b437950c0056bc626b311c73c27)
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_dirtyblksz[i], ==, 0);
360 		ASSERT3P(list_head(&dn->dn_dirty_dbufs[i]), ==, NULL);
361 		ASSERT3U(avl_numnodes(&dn->dn_ranges[i]), ==, 0);
362 	}
363 
364 	dn->dn_type = ot;
365 	dnode_setdblksz(dn, blocksize);
366 	dn->dn_indblkshift = ibs;
367 	dn->dn_nlevels = 1;
368 	dn->dn_nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
369 	dn->dn_bonustype = bonustype;
370 	dn->dn_bonuslen = bonuslen;
371 	dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
372 	dn->dn_compress = ZIO_COMPRESS_INHERIT;
373 	dn->dn_dirtyctx = 0;
374 
375 	dn->dn_free_txg = 0;
376 	if (dn->dn_dirtyctx_firstset) {
377 		kmem_free(dn->dn_dirtyctx_firstset, 1);
378 		dn->dn_dirtyctx_firstset = NULL;
379 	}
380 
381 	dn->dn_allocated_txg = tx->tx_txg;
382 	dnode_setdirty(dn, tx);
383 }
384 
385 void
386 dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
387     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
388 {
389 	ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE);
390 	ASSERT3U(blocksize, <=, SPA_MAXBLOCKSIZE);
391 	ASSERT3U(blocksize % SPA_MINBLOCKSIZE, ==, 0);
392 	ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
393 	ASSERT(tx->tx_txg != 0);
394 	ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
395 	    (bonustype != DMU_OT_NONE && bonuslen != 0));
396 	ASSERT3U(bonustype, <, DMU_OT_NUMTYPES);
397 	ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
398 	ASSERT(dn->dn_dirtyblksz[0] == 0);
399 	ASSERT(dn->dn_dirtyblksz[1] == 0);
400 	ASSERT(dn->dn_dirtyblksz[2] == 0);
401 	ASSERT(dn->dn_dirtyblksz[3] == 0);
402 
403 	/* clean up any unreferenced dbufs */
404 	dnode_evict_dbufs(dn);
405 	ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
406 
407 	/*
408 	 * XXX I should really have a generation number to tell if we
409 	 * need to do this...
410 	 */
411 	if (blocksize != dn->dn_datablksz ||
412 	    dn->dn_bonustype != bonustype || dn->dn_bonuslen != bonuslen) {
413 		/* free all old data */
414 		dnode_free_range(dn, 0, -1ULL, tx);
415 	}
416 
417 	/* change blocksize */
418 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
419 	dnode_setdblksz(dn, blocksize);
420 	dnode_setdirty(dn, tx);
421 	/* don't need dd_dirty_mtx, dnode is already dirty */
422 	ASSERT(dn->dn_dirtyblksz[tx->tx_txg&TXG_MASK] != 0);
423 	dn->dn_dirtyblksz[tx->tx_txg&TXG_MASK] = blocksize;
424 	rw_exit(&dn->dn_struct_rwlock);
425 
426 	/* change type */
427 	dn->dn_type = ot;
428 
429 	if (dn->dn_bonuslen != bonuslen) {
430 		dmu_buf_impl_t *db = NULL;
431 
432 		/* change bonus size */
433 		if (bonuslen == 0)
434 			bonuslen = 1; /* XXX */
435 		rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
436 		if (dn->dn_bonus == NULL)
437 			dn->dn_bonus = dbuf_create_bonus(dn);
438 		db = dn->dn_bonus;
439 		rw_exit(&dn->dn_struct_rwlock);
440 		if (refcount_add(&db->db_holds, FTAG) == 1)
441 			dnode_add_ref(dn, db);
442 		mutex_enter(&db->db_mtx);
443 		ASSERT3U(db->db.db_size, ==, dn->dn_bonuslen);
444 		ASSERT(db->db.db_data != NULL);
445 		db->db.db_size = bonuslen;
446 		mutex_exit(&db->db_mtx);
447 		dbuf_dirty(db, tx);
448 		dbuf_rele(db, FTAG);
449 	}
450 
451 	/* change bonus size and type */
452 	mutex_enter(&dn->dn_mtx);
453 	dn->dn_bonustype = bonustype;
454 	dn->dn_bonuslen = bonuslen;
455 	dn->dn_nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
456 	dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
457 	dn->dn_compress = ZIO_COMPRESS_INHERIT;
458 	ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
459 
460 	dn->dn_allocated_txg = tx->tx_txg;
461 	mutex_exit(&dn->dn_mtx);
462 }
463 
464 void
465 dnode_special_close(dnode_t *dn)
466 {
467 	/*
468 	 * Wait for final references to the dnode to clear.  This can
469 	 * only happen if the arc is asyncronously evicting state that
470 	 * has a hold on this dnode while we are trying to evict this
471 	 * dnode.
472 	 */
473 	while (refcount_count(&dn->dn_holds) > 0)
474 		delay(1);
475 	dnode_destroy(dn);
476 }
477 
478 dnode_t *
479 dnode_special_open(objset_impl_t *os, dnode_phys_t *dnp, uint64_t object)
480 {
481 	dnode_t *dn = dnode_create(os, dnp, NULL, object);
482 	DNODE_VERIFY(dn);
483 	return (dn);
484 }
485 
486 static void
487 dnode_buf_pageout(dmu_buf_t *db, void *arg)
488 {
489 	dnode_t **children_dnodes = arg;
490 	int i;
491 	int epb = db->db_size >> DNODE_SHIFT;
492 
493 	for (i = 0; i < epb; i++) {
494 		dnode_t *dn = children_dnodes[i];
495 		int n;
496 
497 		if (dn == NULL)
498 			continue;
499 #ifdef ZFS_DEBUG
500 		/*
501 		 * If there are holds on this dnode, then there should
502 		 * be holds on the dnode's containing dbuf as well; thus
503 		 * it wouldn't be eligable for eviction and this function
504 		 * would not have been called.
505 		 */
506 		ASSERT(refcount_is_zero(&dn->dn_holds));
507 		ASSERT(list_head(&dn->dn_dbufs) == NULL);
508 		ASSERT(refcount_is_zero(&dn->dn_tx_holds));
509 
510 		for (n = 0; n < TXG_SIZE; n++)
511 			ASSERT(dn->dn_dirtyblksz[n] == 0);
512 #endif
513 		children_dnodes[i] = NULL;
514 		dnode_destroy(dn);
515 	}
516 	kmem_free(children_dnodes, epb * sizeof (dnode_t *));
517 }
518 
519 /*
520  * errors:
521  * EINVAL - invalid object number.
522  * EIO - i/o error.
523  * succeeds even for free dnodes.
524  */
525 int
526 dnode_hold_impl(objset_impl_t *os, uint64_t object, int flag,
527     void *tag, dnode_t **dnp)
528 {
529 	int epb, idx, err;
530 	int drop_struct_lock = FALSE;
531 	int type;
532 	uint64_t blk;
533 	dnode_t *mdn, *dn;
534 	dmu_buf_impl_t *db;
535 	dnode_t **children_dnodes;
536 
537 	if (object == 0 || object >= DN_MAX_OBJECT)
538 		return (EINVAL);
539 
540 	mdn = os->os_meta_dnode;
541 
542 	DNODE_VERIFY(mdn);
543 
544 	if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) {
545 		rw_enter(&mdn->dn_struct_rwlock, RW_READER);
546 		drop_struct_lock = TRUE;
547 	}
548 
549 	blk = dbuf_whichblock(mdn, object * sizeof (dnode_phys_t));
550 
551 	db = dbuf_hold(mdn, blk, FTAG);
552 	if (drop_struct_lock)
553 		rw_exit(&mdn->dn_struct_rwlock);
554 	if (db == NULL)
555 		return (EIO);
556 	err = dbuf_read(db, NULL, DB_RF_CANFAIL);
557 	if (err) {
558 		dbuf_rele(db, FTAG);
559 		return (err);
560 	}
561 
562 	ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT);
563 	epb = db->db.db_size >> DNODE_SHIFT;
564 
565 	idx = object & (epb-1);
566 
567 	children_dnodes = dmu_buf_get_user(&db->db);
568 	if (children_dnodes == NULL) {
569 		dnode_t **winner;
570 		children_dnodes = kmem_zalloc(epb * sizeof (dnode_t *),
571 		    KM_SLEEP);
572 		if (winner = dmu_buf_set_user(&db->db, children_dnodes, NULL,
573 		    dnode_buf_pageout)) {
574 			kmem_free(children_dnodes, epb * sizeof (dnode_t *));
575 			children_dnodes = winner;
576 		}
577 	}
578 
579 	if ((dn = children_dnodes[idx]) == NULL) {
580 		dnode_t *winner;
581 		dn = dnode_create(os, (dnode_phys_t *)db->db.db_data+idx,
582 			db, object);
583 		winner = atomic_cas_ptr(&children_dnodes[idx], NULL, dn);
584 		if (winner != NULL) {
585 			dnode_destroy(dn);
586 			dn = winner;
587 		}
588 	}
589 
590 	mutex_enter(&dn->dn_mtx);
591 	type = dn->dn_type;
592 	if (dn->dn_free_txg ||
593 	    ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) ||
594 	    ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)) {
595 		mutex_exit(&dn->dn_mtx);
596 		dbuf_rele(db, FTAG);
597 		return (type == DMU_OT_NONE ? ENOENT : EEXIST);
598 	}
599 	mutex_exit(&dn->dn_mtx);
600 
601 	if (refcount_add(&dn->dn_holds, tag) == 1)
602 		dbuf_add_ref(db, dn);
603 
604 	DNODE_VERIFY(dn);
605 	ASSERT3P(dn->dn_dbuf, ==, db);
606 	ASSERT3U(dn->dn_object, ==, object);
607 	dbuf_rele(db, FTAG);
608 
609 	*dnp = dn;
610 	return (0);
611 }
612 
613 /*
614  * Return held dnode if the object is allocated, NULL if not.
615  */
616 int
617 dnode_hold(objset_impl_t *os, uint64_t object, void *tag, dnode_t **dnp)
618 {
619 	return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, tag, dnp));
620 }
621 
622 void
623 dnode_add_ref(dnode_t *dn, void *tag)
624 {
625 	ASSERT(refcount_count(&dn->dn_holds) > 0);
626 	(void) refcount_add(&dn->dn_holds, tag);
627 }
628 
629 void
630 dnode_rele(dnode_t *dn, void *tag)
631 {
632 	uint64_t refs;
633 
634 	refs = refcount_remove(&dn->dn_holds, tag);
635 	/* NOTE: the DNODE_DNODE does not have a dn_dbuf */
636 	if (refs == 0 && dn->dn_dbuf)
637 		dbuf_rele(dn->dn_dbuf, dn);
638 }
639 
640 void
641 dnode_setdirty(dnode_t *dn, dmu_tx_t *tx)
642 {
643 	objset_impl_t *os = dn->dn_objset;
644 	uint64_t txg = tx->tx_txg;
645 
646 	if (dn->dn_object == DMU_META_DNODE_OBJECT)
647 		return;
648 
649 	DNODE_VERIFY(dn);
650 
651 #ifdef ZFS_DEBUG
652 	mutex_enter(&dn->dn_mtx);
653 	ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg);
654 	/* ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg); */
655 	mutex_exit(&dn->dn_mtx);
656 #endif
657 
658 	mutex_enter(&os->os_lock);
659 
660 	/*
661 	 * If we are already marked dirty, we're done.
662 	 */
663 	if (dn->dn_dirtyblksz[txg&TXG_MASK] > 0) {
664 		mutex_exit(&os->os_lock);
665 		return;
666 	}
667 
668 	ASSERT(!refcount_is_zero(&dn->dn_holds) || list_head(&dn->dn_dbufs));
669 	ASSERT(dn->dn_datablksz != 0);
670 	dn->dn_dirtyblksz[txg&TXG_MASK] = dn->dn_datablksz;
671 
672 	dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
673 	    dn->dn_object, txg);
674 
675 	if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) {
676 		list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn);
677 	} else {
678 		list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn);
679 	}
680 
681 	mutex_exit(&os->os_lock);
682 
683 	/*
684 	 * The dnode maintains a hold on its containing dbuf as
685 	 * long as there are holds on it.  Each instantiated child
686 	 * dbuf maintaines a hold on the dnode.  When the last child
687 	 * drops its hold, the dnode will drop its hold on the
688 	 * containing dbuf. We add a "dirty hold" here so that the
689 	 * dnode will hang around after we finish processing its
690 	 * children.
691 	 */
692 	dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg);
693 
694 	dbuf_dirty(dn->dn_dbuf, tx);
695 
696 	dsl_dataset_dirty(os->os_dsl_dataset, tx);
697 }
698 
699 void
700 dnode_free(dnode_t *dn, dmu_tx_t *tx)
701 {
702 	dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg);
703 
704 	/* we should be the only holder... hopefully */
705 	/* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */
706 
707 	mutex_enter(&dn->dn_mtx);
708 	if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) {
709 		mutex_exit(&dn->dn_mtx);
710 		return;
711 	}
712 	dn->dn_free_txg = tx->tx_txg;
713 	mutex_exit(&dn->dn_mtx);
714 
715 	/*
716 	 * If the dnode is already dirty, it needs to be moved from
717 	 * the dirty list to the free list.
718 	 */
719 	mutex_enter(&dn->dn_objset->os_lock);
720 	if (dn->dn_dirtyblksz[tx->tx_txg&TXG_MASK] > 0) {
721 		list_remove(
722 		    &dn->dn_objset->os_dirty_dnodes[tx->tx_txg&TXG_MASK], dn);
723 		list_insert_tail(
724 		    &dn->dn_objset->os_free_dnodes[tx->tx_txg&TXG_MASK], dn);
725 		mutex_exit(&dn->dn_objset->os_lock);
726 	} else {
727 		mutex_exit(&dn->dn_objset->os_lock);
728 		dnode_setdirty(dn, tx);
729 	}
730 }
731 
732 /*
733  * Try to change the block size for the indicated dnode.  This can only
734  * succeed if there are no blocks allocated or dirty beyond first block
735  */
736 int
737 dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx)
738 {
739 	dmu_buf_impl_t *db, *db_next;
740 	int have_db0 = FALSE;
741 	int err = ENOTSUP;
742 
743 	if (size == 0)
744 		size = SPA_MINBLOCKSIZE;
745 	if (size > SPA_MAXBLOCKSIZE)
746 		size = SPA_MAXBLOCKSIZE;
747 	else
748 		size = P2ROUNDUP(size, SPA_MINBLOCKSIZE);
749 
750 	if (ibs == 0)
751 		ibs = dn->dn_indblkshift;
752 
753 	if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec &&
754 	    ibs == dn->dn_indblkshift)
755 		return (0);
756 
757 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
758 
759 	/* Check for any allocated blocks beyond the first */
760 	if (dn->dn_phys->dn_maxblkid != 0)
761 		goto end;
762 
763 	/*
764 	 * Any buffers allocated for blocks beyond the first
765 	 * must be evictable/evicted, because they're the wrong size.
766 	 */
767 	mutex_enter(&dn->dn_dbufs_mtx);
768 	/*
769 	 * Since we have the dn_dbufs_mtx, nothing can be
770 	 * removed from dn_dbufs.  Since we have dn_struct_rwlock/w,
771 	 * nothing can be added to dn_dbufs.
772 	 */
773 	for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
774 		db_next = list_next(&dn->dn_dbufs, db);
775 
776 		if (db->db_blkid == 0) {
777 			have_db0 = TRUE;
778 		} else if (db->db_blkid != DB_BONUS_BLKID) {
779 			mutex_exit(&dn->dn_dbufs_mtx);
780 			goto end;
781 		}
782 	}
783 	mutex_exit(&dn->dn_dbufs_mtx);
784 
785 	/* Fast-track if there is no data in the file */
786 	if (BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) && !have_db0) {
787 		dnode_setdblksz(dn, size);
788 		dn->dn_indblkshift = ibs;
789 		dnode_setdirty(dn, tx);
790 		/* don't need dd_dirty_mtx, dnode is already dirty */
791 		dn->dn_dirtyblksz[tx->tx_txg&TXG_MASK] = size;
792 		dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs;
793 		rw_exit(&dn->dn_struct_rwlock);
794 		return (0);
795 	}
796 
797 	/* obtain the old block */
798 	db = dbuf_hold(dn, 0, FTAG);
799 
800 	dbuf_new_size(db, size, tx);
801 
802 	dnode_setdblksz(dn, size);
803 	dn->dn_indblkshift = ibs;
804 	/* don't need dd_dirty_mtx, dnode is already dirty */
805 	dn->dn_dirtyblksz[tx->tx_txg&TXG_MASK] = size;
806 	dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs;
807 	dbuf_rele(db, FTAG);
808 
809 	err = 0;
810 end:
811 	rw_exit(&dn->dn_struct_rwlock);
812 	return (err);
813 }
814 
815 uint64_t
816 dnode_max_nonzero_offset(dnode_t *dn)
817 {
818 	if (dn->dn_phys->dn_maxblkid == 0 &&
819 	    BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]))
820 		return (0);
821 	else
822 		return ((dn->dn_phys->dn_maxblkid+1) * dn->dn_datablksz);
823 }
824 
825 void
826 dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx)
827 {
828 	uint64_t txgoff = tx->tx_txg & TXG_MASK;
829 	int drop_struct_lock = FALSE;
830 	int epbs, old_nlevels, new_nlevels;
831 	uint64_t sz;
832 
833 	if (blkid == DB_BONUS_BLKID)
834 		return;
835 
836 	if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
837 		rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
838 		drop_struct_lock = TRUE;
839 	}
840 
841 	if (blkid > dn->dn_maxblkid)
842 		dn->dn_maxblkid = blkid;
843 
844 	/*
845 	 * Compute the number of levels necessary to support the
846 	 * new blkid.
847 	 */
848 	new_nlevels = 1;
849 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
850 
851 	for (sz = dn->dn_nblkptr; sz <= blkid && sz >= dn->dn_nblkptr;
852 	    sz <<= epbs)
853 		new_nlevels++;
854 	old_nlevels = dn->dn_nlevels;
855 
856 	if (new_nlevels > dn->dn_next_nlevels[txgoff])
857 		dn->dn_next_nlevels[txgoff] = new_nlevels;
858 
859 	if (new_nlevels > old_nlevels) {
860 		dprintf("dn %p increasing nlevels from %u to %u\n",
861 		    dn, dn->dn_nlevels, new_nlevels);
862 		dn->dn_nlevels = new_nlevels;
863 	}
864 
865 	/*
866 	 * Dirty the left indirects.
867 	 * Note: the caller should have just dnode_use_space()'d one
868 	 * data block's worth, so we could subtract that out of
869 	 * dn_inflight_data to determine if there is any dirty data
870 	 * besides this block.
871 	 * We don't strictly need to dirty them unless there's
872 	 * *something* in the object (eg. on disk or dirty)...
873 	 */
874 	if (new_nlevels > old_nlevels) {
875 		dmu_buf_impl_t *db = dbuf_hold_level(dn, old_nlevels, 0, FTAG);
876 		dprintf("dn %p dirtying left indirects\n", dn);
877 		dbuf_dirty(db, tx);
878 		dbuf_rele(db, FTAG);
879 	}
880 #ifdef ZFS_DEBUG
881 	else if (old_nlevels > 1 && new_nlevels > old_nlevels) {
882 		dmu_buf_impl_t *db;
883 		int i;
884 
885 		for (i = 0; i < dn->dn_nblkptr; i++) {
886 			db = dbuf_hold_level(dn, old_nlevels-1, i, FTAG);
887 			ASSERT(!
888 			    list_link_active(&db->db_dirty_node[txgoff]));
889 			dbuf_rele(db, FTAG);
890 		}
891 	}
892 #endif
893 
894 	dprintf("dn %p done\n", dn);
895 
896 out:
897 	if (drop_struct_lock)
898 		rw_exit(&dn->dn_struct_rwlock);
899 }
900 
901 void
902 dnode_clear_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
903 {
904 	avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
905 	avl_index_t where;
906 	free_range_t *rp;
907 	free_range_t rp_tofind;
908 	uint64_t endblk = blkid + nblks;
909 
910 	ASSERT(MUTEX_HELD(&dn->dn_mtx));
911 	ASSERT(nblks <= UINT64_MAX - blkid); /* no overflow */
912 
913 	dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
914 	    blkid, nblks, tx->tx_txg);
915 	rp_tofind.fr_blkid = blkid;
916 	rp = avl_find(tree, &rp_tofind, &where);
917 	if (rp == NULL)
918 		rp = avl_nearest(tree, where, AVL_BEFORE);
919 	if (rp == NULL)
920 		rp = avl_nearest(tree, where, AVL_AFTER);
921 
922 	while (rp && (rp->fr_blkid <= blkid + nblks)) {
923 		uint64_t fr_endblk = rp->fr_blkid + rp->fr_nblks;
924 		free_range_t *nrp = AVL_NEXT(tree, rp);
925 
926 		if (blkid <= rp->fr_blkid && endblk >= fr_endblk) {
927 			/* clear this entire range */
928 			avl_remove(tree, rp);
929 			kmem_free(rp, sizeof (free_range_t));
930 		} else if (blkid <= rp->fr_blkid &&
931 		    endblk > rp->fr_blkid && endblk < fr_endblk) {
932 			/* clear the beginning of this range */
933 			rp->fr_blkid = endblk;
934 			rp->fr_nblks = fr_endblk - endblk;
935 		} else if (blkid > rp->fr_blkid && blkid < fr_endblk &&
936 		    endblk >= fr_endblk) {
937 			/* clear the end of this range */
938 			rp->fr_nblks = blkid - rp->fr_blkid;
939 		} else if (blkid > rp->fr_blkid && endblk < fr_endblk) {
940 			/* clear a chunk out of this range */
941 			free_range_t *new_rp =
942 			    kmem_alloc(sizeof (free_range_t), KM_SLEEP);
943 
944 			new_rp->fr_blkid = endblk;
945 			new_rp->fr_nblks = fr_endblk - endblk;
946 			avl_insert_here(tree, new_rp, rp, AVL_AFTER);
947 			rp->fr_nblks = blkid - rp->fr_blkid;
948 		}
949 		/* there may be no overlap */
950 		rp = nrp;
951 	}
952 }
953 
954 void
955 dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx)
956 {
957 	dmu_buf_impl_t *db;
958 	uint64_t start, objsize, blkid, nblks;
959 	int blkshift, blksz, tail, head, epbs;
960 	int trunc = FALSE;
961 
962 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
963 	blksz = dn->dn_datablksz;
964 	blkshift = dn->dn_datablkshift;
965 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
966 
967 	/* If the range is past the end of the file, this is a no-op */
968 	objsize = blksz * (dn->dn_maxblkid+1);
969 	if (off >= objsize)
970 		goto out;
971 	if (len == -1ULL) {
972 		len = UINT64_MAX - off;
973 		trunc = TRUE;
974 	}
975 
976 	/*
977 	 * First, block align the region to free:
978 	 */
979 	if (dn->dn_maxblkid == 0) {
980 		if (off == 0) {
981 			head = 0;
982 		} else {
983 			head = blksz - off;
984 			ASSERT3U(head, >, 0);
985 		}
986 		start = off;
987 	} else {
988 		ASSERT(ISP2(blksz));
989 		head = P2NPHASE(off, blksz);
990 		start = P2PHASE(off, blksz);
991 	}
992 	/* zero out any partial block data at the start of the range */
993 	if (head) {
994 		ASSERT3U(start + head, ==, blksz);
995 		if (len < head)
996 			head = len;
997 		if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off), TRUE,
998 		    FTAG, &db) == 0) {
999 			caddr_t data;
1000 
1001 			/* don't dirty if it isn't on disk and isn't dirty */
1002 			if (db->db_dirtied ||
1003 			    (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1004 				rw_exit(&dn->dn_struct_rwlock);
1005 				dbuf_will_dirty(db, tx);
1006 				rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1007 				data = db->db.db_data;
1008 				bzero(data + start, head);
1009 			}
1010 			dbuf_rele(db, FTAG);
1011 		}
1012 		off += head;
1013 		len -= head;
1014 	}
1015 	/* If the range was less than one block, we are done */
1016 	if (len == 0)
1017 		goto out;
1018 
1019 	/* If the remaining range is past the end of the file, we are done */
1020 	if (off > dn->dn_maxblkid << blkshift)
1021 		goto out;
1022 
1023 	if (off + len == UINT64_MAX)
1024 		tail = 0;
1025 	else
1026 		tail = P2PHASE(len, blksz);
1027 
1028 	ASSERT3U(P2PHASE(off, blksz), ==, 0);
1029 	/* zero out any partial block data at the end of the range */
1030 	if (tail) {
1031 		if (len < tail)
1032 			tail = len;
1033 		if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len),
1034 		    TRUE, FTAG, &db) == 0) {
1035 			/* don't dirty if it isn't on disk and isn't dirty */
1036 			if (db->db_dirtied ||
1037 			    (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1038 				rw_exit(&dn->dn_struct_rwlock);
1039 				dbuf_will_dirty(db, tx);
1040 				rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1041 				bzero(db->db.db_data, tail);
1042 			}
1043 			dbuf_rele(db, FTAG);
1044 		}
1045 		len -= tail;
1046 	}
1047 	/* If the range did not include a full block, we are done */
1048 	if (len == 0)
1049 		goto out;
1050 
1051 	/* dirty the left indirects */
1052 	if (dn->dn_nlevels > 1 && off != 0) {
1053 		db = dbuf_hold_level(dn, 1,
1054 		    (off - head) >> (blkshift + epbs), FTAG);
1055 		dbuf_will_dirty(db, tx);
1056 		dbuf_rele(db, FTAG);
1057 	}
1058 
1059 	/* dirty the right indirects */
1060 	if (dn->dn_nlevels > 1 && !trunc) {
1061 		db = dbuf_hold_level(dn, 1,
1062 		    (off + len + tail - 1) >> (blkshift + epbs), FTAG);
1063 		dbuf_will_dirty(db, tx);
1064 		dbuf_rele(db, FTAG);
1065 	}
1066 
1067 	/*
1068 	 * Finally, add this range to the dnode range list, we
1069 	 * will finish up this free operation in the syncing phase.
1070 	 */
1071 	ASSERT(IS_P2ALIGNED(off, 1<<blkshift));
1072 	ASSERT(off + len == UINT64_MAX || IS_P2ALIGNED(len, 1<<blkshift));
1073 	blkid = off >> blkshift;
1074 	nblks = len >> blkshift;
1075 
1076 	if (trunc)
1077 		dn->dn_maxblkid = (blkid ? blkid - 1 : 0);
1078 
1079 	mutex_enter(&dn->dn_mtx);
1080 	dnode_clear_range(dn, blkid, nblks, tx);
1081 	{
1082 		free_range_t *rp, *found;
1083 		avl_index_t where;
1084 		avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
1085 
1086 		/* Add new range to dn_ranges */
1087 		rp = kmem_alloc(sizeof (free_range_t), KM_SLEEP);
1088 		rp->fr_blkid = blkid;
1089 		rp->fr_nblks = nblks;
1090 		found = avl_find(tree, rp, &where);
1091 		ASSERT(found == NULL);
1092 		avl_insert(tree, rp, where);
1093 		dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1094 		    blkid, nblks, tx->tx_txg);
1095 	}
1096 	mutex_exit(&dn->dn_mtx);
1097 
1098 	dbuf_free_range(dn, blkid, nblks, tx);
1099 	dnode_setdirty(dn, tx);
1100 out:
1101 	rw_exit(&dn->dn_struct_rwlock);
1102 }
1103 
1104 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
1105 uint64_t
1106 dnode_block_freed(dnode_t *dn, uint64_t blkid)
1107 {
1108 	free_range_t range_tofind;
1109 	void *dp = spa_get_dsl(dn->dn_objset->os_spa);
1110 	int i;
1111 
1112 	if (blkid == DB_BONUS_BLKID)
1113 		return (FALSE);
1114 
1115 	/*
1116 	 * If we're in the process of opening the pool, dp will not be
1117 	 * set yet, but there shouldn't be anything dirty.
1118 	 */
1119 	if (dp == NULL)
1120 		return (FALSE);
1121 
1122 	if (dn->dn_free_txg)
1123 		return (TRUE);
1124 
1125 	/*
1126 	 * If dn_datablkshift is not set, then there's only a single
1127 	 * block, in which case there will never be a free range so it
1128 	 * won't matter.
1129 	 */
1130 	range_tofind.fr_blkid = blkid;
1131 	mutex_enter(&dn->dn_mtx);
1132 	for (i = 0; i < TXG_SIZE; i++) {
1133 		free_range_t *range_found;
1134 		avl_index_t idx;
1135 
1136 		range_found = avl_find(&dn->dn_ranges[i], &range_tofind, &idx);
1137 		if (range_found) {
1138 			ASSERT(range_found->fr_nblks > 0);
1139 			break;
1140 		}
1141 		range_found = avl_nearest(&dn->dn_ranges[i], idx, AVL_BEFORE);
1142 		if (range_found &&
1143 		    range_found->fr_blkid + range_found->fr_nblks > blkid)
1144 			break;
1145 	}
1146 	mutex_exit(&dn->dn_mtx);
1147 	return (i < TXG_SIZE);
1148 }
1149 
1150 /* call from syncing context when we actually write/free space for this dnode */
1151 void
1152 dnode_diduse_space(dnode_t *dn, int64_t space)
1153 {
1154 	uint64_t sectors;
1155 
1156 	dprintf_dnode(dn, "dn=%p dnp=%p secphys=%llu space=%lld\n",
1157 	    dn, dn->dn_phys,
1158 	    (u_longlong_t)dn->dn_phys->dn_secphys,
1159 	    (longlong_t)space);
1160 
1161 	ASSERT(P2PHASE(space, 1<<DEV_BSHIFT) == 0);
1162 
1163 	mutex_enter(&dn->dn_mtx);
1164 	if (space > 0) {
1165 		sectors = space >> DEV_BSHIFT;
1166 		ASSERT3U(dn->dn_phys->dn_secphys + sectors, >=,
1167 		    dn->dn_phys->dn_secphys);
1168 		dn->dn_phys->dn_secphys += sectors;
1169 	} else {
1170 		sectors = -space >> DEV_BSHIFT;
1171 		ASSERT3U(dn->dn_phys->dn_secphys, >=, sectors);
1172 		dn->dn_phys->dn_secphys -= sectors;
1173 	}
1174 	mutex_exit(&dn->dn_mtx);
1175 }
1176 
1177 /*
1178  * Call when we think we're going to write/free space in open context.
1179  * Be conservative (ie. OK to write less than this or free more than
1180  * this, but don't write more or free less).
1181  */
1182 void
1183 dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx)
1184 {
1185 	objset_impl_t *os = dn->dn_objset;
1186 	dsl_dataset_t *ds = os->os_dsl_dataset;
1187 
1188 	if (space > 0)
1189 		space = spa_get_asize(os->os_spa, space);
1190 
1191 	if (ds)
1192 		dsl_dir_willuse_space(ds->ds_dir, space, tx);
1193 
1194 	dmu_tx_willuse_space(tx, space);
1195 }
1196 
1197 static int
1198 dnode_next_offset_level(dnode_t *dn, boolean_t hole, uint64_t *offset,
1199 	int lvl, uint64_t blkfill)
1200 {
1201 	dmu_buf_impl_t *db = NULL;
1202 	void *data = NULL;
1203 	uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
1204 	uint64_t epb = 1ULL << epbs;
1205 	uint64_t minfill, maxfill;
1206 	int i, error, span;
1207 
1208 	dprintf("probing object %llu offset %llx level %d of %u\n",
1209 	    dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels);
1210 
1211 	if (lvl == dn->dn_phys->dn_nlevels) {
1212 		error = 0;
1213 		epb = dn->dn_phys->dn_nblkptr;
1214 		data = dn->dn_phys->dn_blkptr;
1215 	} else {
1216 		uint64_t blkid = dbuf_whichblock(dn, *offset) >> (epbs * lvl);
1217 		error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FTAG, &db);
1218 		if (error) {
1219 			if (error == ENOENT)
1220 				return (hole ? 0 : ESRCH);
1221 			return (error);
1222 		}
1223 		(void) dbuf_read(db, NULL,
1224 		    DB_RF_MUST_SUCCEED | DB_RF_HAVESTRUCT);
1225 		data = db->db.db_data;
1226 	}
1227 
1228 	if (lvl == 0) {
1229 		dnode_phys_t *dnp = data;
1230 		span = DNODE_SHIFT;
1231 		ASSERT(dn->dn_type == DMU_OT_DNODE);
1232 
1233 		for (i = (*offset >> span) & (blkfill - 1); i < blkfill; i++) {
1234 			if (!dnp[i].dn_type == hole)
1235 				break;
1236 			*offset += 1ULL << span;
1237 		}
1238 		if (i == blkfill)
1239 			error = ESRCH;
1240 	} else {
1241 		blkptr_t *bp = data;
1242 		span = (lvl - 1) * epbs + dn->dn_datablkshift;
1243 		minfill = 0;
1244 		maxfill = blkfill << ((lvl - 1) * epbs);
1245 
1246 		if (hole)
1247 			maxfill--;
1248 		else
1249 			minfill++;
1250 
1251 		for (i = (*offset >> span) & ((1ULL << epbs) - 1);
1252 		    i < epb; i++) {
1253 			if (bp[i].blk_fill >= minfill &&
1254 			    bp[i].blk_fill <= maxfill)
1255 				break;
1256 			*offset += 1ULL << span;
1257 		}
1258 		if (i >= epb)
1259 			error = ESRCH;
1260 	}
1261 
1262 	if (db)
1263 		dbuf_rele(db, FTAG);
1264 
1265 	return (error);
1266 }
1267 
1268 /*
1269  * Find the next hole, data, or sparse region at or after *offset.
1270  * The value 'blkfill' tells us how many items we expect to find
1271  * in an L0 data block; this value is 1 for normal objects,
1272  * DNODES_PER_BLOCK for the meta dnode, and some fraction of
1273  * DNODES_PER_BLOCK when searching for sparse regions thereof.
1274  * Examples:
1275  *
1276  * dnode_next_offset(dn, hole, offset, 1, 1);
1277  *	Finds the next hole/data in a file.
1278  *	Used in dmu_offset_next().
1279  *
1280  * dnode_next_offset(mdn, hole, offset, 0, DNODES_PER_BLOCK);
1281  *	Finds the next free/allocated dnode an objset's meta-dnode.
1282  *	Used in dmu_object_next().
1283  *
1284  * dnode_next_offset(mdn, TRUE, offset, 2, DNODES_PER_BLOCK >> 2);
1285  *	Finds the next L2 meta-dnode bp that's at most 1/4 full.
1286  *	Used in dmu_object_alloc().
1287  */
1288 int
1289 dnode_next_offset(dnode_t *dn, boolean_t hole, uint64_t *offset,
1290     int minlvl, uint64_t blkfill)
1291 {
1292 	int lvl, maxlvl;
1293 	int error = 0;
1294 	uint64_t initial_offset = *offset;
1295 
1296 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
1297 
1298 	if (dn->dn_phys->dn_nlevels == 0) {
1299 		rw_exit(&dn->dn_struct_rwlock);
1300 		return (ESRCH);
1301 	}
1302 
1303 	if (dn->dn_datablkshift == 0) {
1304 		if (*offset < dn->dn_datablksz) {
1305 			if (hole)
1306 				*offset = dn->dn_datablksz;
1307 		} else {
1308 			error = ESRCH;
1309 		}
1310 		rw_exit(&dn->dn_struct_rwlock);
1311 		return (error);
1312 	}
1313 
1314 	maxlvl = dn->dn_phys->dn_nlevels;
1315 
1316 	for (lvl = minlvl; lvl <= maxlvl; lvl++) {
1317 		error = dnode_next_offset_level(dn, hole, offset, lvl, blkfill);
1318 		if (error == 0)
1319 			break;
1320 	}
1321 
1322 	while (--lvl >= minlvl && error == 0)
1323 		error = dnode_next_offset_level(dn, hole, offset, lvl, blkfill);
1324 
1325 	rw_exit(&dn->dn_struct_rwlock);
1326 
1327 	if (initial_offset > *offset)
1328 		return (ESRCH);
1329 
1330 	return (error);
1331 }
1332