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