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