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.
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.
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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/zfs_context.h>
28 #include <sys/dnode.h>
30 #include <sys/dmu_impl.h>
31 #include <sys/dmu_tx.h>
32 #include <sys/dmu_objset.h>
33 #include <sys/dsl_dir.h>
34 #include <sys/dsl_dataset.h>
37 #include <sys/dmu_zfetch.h>
39 static int free_range_compar(const void *node1, const void *node2);
41 static kmem_cache_t *dnode_cache;
43 static dnode_phys_t dnode_phys_zero;
45 int zfs_default_bs = SPA_MINBLOCKSHIFT;
46 int zfs_default_ibs = DN_MAX_INDBLKSHIFT;
50 dnode_cons(void *arg, void *unused, int kmflag)
54 bzero(dn, sizeof (dnode_t));
56 rw_init(&dn->dn_struct_rwlock, NULL, RW_DEFAULT, NULL);
57 mutex_init(&dn->dn_mtx, NULL, MUTEX_DEFAULT, NULL);
58 mutex_init(&dn->dn_dbufs_mtx, NULL, MUTEX_DEFAULT, NULL);
59 cv_init(&dn->dn_notxholds, NULL, CV_DEFAULT, NULL);
61 refcount_create(&dn->dn_holds);
62 refcount_create(&dn->dn_tx_holds);
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_records[i],
69 sizeof (dbuf_dirty_record_t),
70 offsetof(dbuf_dirty_record_t, dr_dirty_node));
73 list_create(&dn->dn_dbufs, sizeof (dmu_buf_impl_t),
74 offsetof(dmu_buf_impl_t, db_link));
81 dnode_dest(void *arg, void *unused)
86 rw_destroy(&dn->dn_struct_rwlock);
87 mutex_destroy(&dn->dn_mtx);
88 mutex_destroy(&dn->dn_dbufs_mtx);
89 cv_destroy(&dn->dn_notxholds);
90 refcount_destroy(&dn->dn_holds);
91 refcount_destroy(&dn->dn_tx_holds);
93 for (i = 0; i < TXG_SIZE; i++) {
94 avl_destroy(&dn->dn_ranges[i]);
95 list_destroy(&dn->dn_dirty_records[i]);
98 list_destroy(&dn->dn_dbufs);
104 dnode_cache = kmem_cache_create("dnode_t",
106 0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0);
112 kmem_cache_destroy(dnode_cache);
118 dnode_verify(dnode_t *dn)
120 int drop_struct_lock = FALSE;
123 ASSERT(dn->dn_objset);
125 ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES);
127 if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY))
130 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
131 rw_enter(&dn->dn_struct_rwlock, RW_READER);
132 drop_struct_lock = TRUE;
134 if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) {
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);
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);
157 if (dn->dn_phys->dn_type != DMU_OT_NONE)
158 ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels);
159 ASSERT(DMU_OBJECT_IS_SPECIAL(dn->dn_object) || dn->dn_dbuf != NULL);
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)));
165 if (drop_struct_lock)
166 rw_exit(&dn->dn_struct_rwlock);
171 dnode_byteswap(dnode_phys_t *dnp)
173 uint64_t *buf64 = (void*)&dnp->dn_blkptr;
176 if (dnp->dn_type == DMU_OT_NONE) {
177 bzero(dnp, sizeof (dnode_phys_t));
181 dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec);
182 dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen);
183 dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid);
184 dnp->dn_used = BSWAP_64(dnp->dn_used);
187 * dn_nblkptr is only one byte, so it's OK to read it in either
188 * byte order. We can't read dn_bouslen.
190 ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT);
191 ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR);
192 for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++)
193 buf64[i] = BSWAP_64(buf64[i]);
196 * OK to check dn_bonuslen for zero, because it won't matter if
197 * we have the wrong byte order. This is necessary because the
198 * dnode dnode is smaller than a regular dnode.
200 if (dnp->dn_bonuslen != 0) {
202 * Note that the bonus length calculated here may be
203 * longer than the actual bonus buffer. This is because
204 * we always put the bonus buffer after the last block
205 * pointer (instead of packing it against the end of the
208 int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t);
209 size_t len = DN_MAX_BONUSLEN - off;
210 ASSERT3U(dnp->dn_bonustype, <, DMU_OT_NUMTYPES);
211 dmu_ot[dnp->dn_bonustype].ot_byteswap(dnp->dn_bonus + off, len);
216 dnode_buf_byteswap(void *vbuf, size_t size)
218 dnode_phys_t *buf = vbuf;
221 ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT));
222 ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0);
224 size >>= DNODE_SHIFT;
225 for (i = 0; i < size; i++) {
232 free_range_compar(const void *node1, const void *node2)
234 const free_range_t *rp1 = node1;
235 const free_range_t *rp2 = node2;
237 if (rp1->fr_blkid < rp2->fr_blkid)
239 else if (rp1->fr_blkid > rp2->fr_blkid)
245 dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx)
247 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
249 dnode_setdirty(dn, tx);
250 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
251 ASSERT3U(newsize, <=, DN_MAX_BONUSLEN -
252 (dn->dn_nblkptr-1) * sizeof (blkptr_t));
253 dn->dn_bonuslen = newsize;
255 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = DN_ZERO_BONUSLEN;
257 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
258 rw_exit(&dn->dn_struct_rwlock);
262 dnode_setdblksz(dnode_t *dn, int size)
264 ASSERT3U(P2PHASE(size, SPA_MINBLOCKSIZE), ==, 0);
265 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
266 ASSERT3U(size, >=, SPA_MINBLOCKSIZE);
267 ASSERT3U(size >> SPA_MINBLOCKSHIFT, <,
268 1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8));
269 dn->dn_datablksz = size;
270 dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT;
271 dn->dn_datablkshift = ISP2(size) ? highbit(size - 1) : 0;
275 dnode_create(objset_impl_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db,
278 dnode_t *dn = kmem_cache_alloc(dnode_cache, KM_SLEEP);
279 (void) dnode_cons(dn, NULL, 0); /* XXX */
282 dn->dn_object = object;
286 if (dnp->dn_datablkszsec)
287 dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
288 dn->dn_indblkshift = dnp->dn_indblkshift;
289 dn->dn_nlevels = dnp->dn_nlevels;
290 dn->dn_type = dnp->dn_type;
291 dn->dn_nblkptr = dnp->dn_nblkptr;
292 dn->dn_checksum = dnp->dn_checksum;
293 dn->dn_compress = dnp->dn_compress;
294 dn->dn_bonustype = dnp->dn_bonustype;
295 dn->dn_bonuslen = dnp->dn_bonuslen;
296 dn->dn_maxblkid = dnp->dn_maxblkid;
298 dmu_zfetch_init(&dn->dn_zfetch, dn);
300 ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES);
301 mutex_enter(&os->os_lock);
302 list_insert_head(&os->os_dnodes, dn);
303 mutex_exit(&os->os_lock);
305 arc_space_consume(sizeof (dnode_t), ARC_SPACE_OTHER);
310 dnode_destroy(dnode_t *dn)
312 objset_impl_t *os = dn->dn_objset;
317 for (i = 0; i < TXG_SIZE; i++) {
318 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
319 ASSERT(NULL == list_head(&dn->dn_dirty_records[i]));
320 ASSERT(0 == avl_numnodes(&dn->dn_ranges[i]));
322 ASSERT(NULL == list_head(&dn->dn_dbufs));
324 ASSERT(dn->dn_oldphys == NULL);
326 mutex_enter(&os->os_lock);
327 list_remove(&os->os_dnodes, dn);
328 mutex_exit(&os->os_lock);
330 if (dn->dn_dirtyctx_firstset) {
331 kmem_free(dn->dn_dirtyctx_firstset, 1);
332 dn->dn_dirtyctx_firstset = NULL;
334 dmu_zfetch_rele(&dn->dn_zfetch);
336 mutex_enter(&dn->dn_bonus->db_mtx);
337 dbuf_evict(dn->dn_bonus);
340 kmem_cache_free(dnode_cache, dn);
341 arc_space_return(sizeof (dnode_t), ARC_SPACE_OTHER);
345 dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
346 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
351 blocksize = 1 << zfs_default_bs;
352 else if (blocksize > SPA_MAXBLOCKSIZE)
353 blocksize = SPA_MAXBLOCKSIZE;
355 blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE);
358 ibs = zfs_default_ibs;
360 ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT);
362 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset,
363 dn->dn_object, tx->tx_txg, blocksize, ibs);
365 ASSERT(dn->dn_type == DMU_OT_NONE);
366 ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0);
367 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE);
368 ASSERT(ot != DMU_OT_NONE);
369 ASSERT3U(ot, <, DMU_OT_NUMTYPES);
370 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
371 (bonustype != DMU_OT_NONE && bonuslen != 0));
372 ASSERT3U(bonustype, <, DMU_OT_NUMTYPES);
373 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
374 ASSERT(dn->dn_type == DMU_OT_NONE);
375 ASSERT3U(dn->dn_maxblkid, ==, 0);
376 ASSERT3U(dn->dn_allocated_txg, ==, 0);
377 ASSERT3U(dn->dn_assigned_txg, ==, 0);
378 ASSERT(refcount_is_zero(&dn->dn_tx_holds));
379 ASSERT3U(refcount_count(&dn->dn_holds), <=, 1);
380 ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
382 for (i = 0; i < TXG_SIZE; i++) {
383 ASSERT3U(dn->dn_next_nlevels[i], ==, 0);
384 ASSERT3U(dn->dn_next_indblkshift[i], ==, 0);
385 ASSERT3U(dn->dn_next_bonuslen[i], ==, 0);
386 ASSERT3U(dn->dn_next_blksz[i], ==, 0);
387 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
388 ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL);
389 ASSERT3U(avl_numnodes(&dn->dn_ranges[i]), ==, 0);
393 dnode_setdblksz(dn, blocksize);
394 dn->dn_indblkshift = ibs;
396 dn->dn_nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
397 dn->dn_bonustype = bonustype;
398 dn->dn_bonuslen = bonuslen;
399 dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
400 dn->dn_compress = ZIO_COMPRESS_INHERIT;
404 if (dn->dn_dirtyctx_firstset) {
405 kmem_free(dn->dn_dirtyctx_firstset, 1);
406 dn->dn_dirtyctx_firstset = NULL;
409 dn->dn_allocated_txg = tx->tx_txg;
411 dnode_setdirty(dn, tx);
412 dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs;
413 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
414 dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz;
418 dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
419 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
423 ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE);
424 ASSERT3U(blocksize, <=, SPA_MAXBLOCKSIZE);
425 ASSERT3U(blocksize % SPA_MINBLOCKSIZE, ==, 0);
426 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
427 ASSERT(tx->tx_txg != 0);
428 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
429 (bonustype != DMU_OT_NONE && bonuslen != 0));
430 ASSERT3U(bonustype, <, DMU_OT_NUMTYPES);
431 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
433 /* clean up any unreferenced dbufs */
434 dnode_evict_dbufs(dn);
436 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
437 dnode_setdirty(dn, tx);
438 if (dn->dn_datablksz != blocksize) {
439 /* change blocksize */
440 ASSERT(dn->dn_maxblkid == 0 &&
441 (BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) ||
442 dnode_block_freed(dn, 0)));
443 dnode_setdblksz(dn, blocksize);
444 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize;
446 if (dn->dn_bonuslen != bonuslen)
447 dn->dn_next_bonuslen[tx->tx_txg&TXG_MASK] = bonuslen;
448 nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
449 if (dn->dn_nblkptr != nblkptr)
450 dn->dn_next_nblkptr[tx->tx_txg&TXG_MASK] = nblkptr;
451 rw_exit(&dn->dn_struct_rwlock);
456 /* change bonus size and type */
457 mutex_enter(&dn->dn_mtx);
458 dn->dn_bonustype = bonustype;
459 dn->dn_bonuslen = bonuslen;
460 dn->dn_nblkptr = nblkptr;
461 dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
462 dn->dn_compress = ZIO_COMPRESS_INHERIT;
463 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
465 /* fix up the bonus db_size */
467 dn->dn_bonus->db.db_size =
468 DN_MAX_BONUSLEN - (dn->dn_nblkptr-1) * sizeof (blkptr_t);
469 ASSERT(dn->dn_bonuslen <= dn->dn_bonus->db.db_size);
472 dn->dn_allocated_txg = tx->tx_txg;
473 mutex_exit(&dn->dn_mtx);
477 dnode_special_close(dnode_t *dn)
480 * Wait for final references to the dnode to clear. This can
481 * only happen if the arc is asyncronously evicting state that
482 * has a hold on this dnode while we are trying to evict this
485 while (refcount_count(&dn->dn_holds) > 0)
491 dnode_special_open(objset_impl_t *os, dnode_phys_t *dnp, uint64_t object)
493 dnode_t *dn = dnode_create(os, dnp, NULL, object);
499 dnode_buf_pageout(dmu_buf_t *db, void *arg)
501 dnode_t **children_dnodes = arg;
503 int epb = db->db_size >> DNODE_SHIFT;
505 for (i = 0; i < epb; i++) {
506 dnode_t *dn = children_dnodes[i];
513 * If there are holds on this dnode, then there should
514 * be holds on the dnode's containing dbuf as well; thus
515 * it wouldn't be eligable for eviction and this function
516 * would not have been called.
518 ASSERT(refcount_is_zero(&dn->dn_holds));
519 ASSERT(list_head(&dn->dn_dbufs) == NULL);
520 ASSERT(refcount_is_zero(&dn->dn_tx_holds));
522 for (n = 0; n < TXG_SIZE; n++)
523 ASSERT(!list_link_active(&dn->dn_dirty_link[n]));
525 children_dnodes[i] = NULL;
528 kmem_free(children_dnodes, epb * sizeof (dnode_t *));
533 * EINVAL - invalid object number.
535 * succeeds even for free dnodes.
538 dnode_hold_impl(objset_impl_t *os, uint64_t object, int flag,
539 void *tag, dnode_t **dnp)
542 int drop_struct_lock = FALSE;
547 dnode_t **children_dnodes;
550 * If you are holding the spa config lock as writer, you shouldn't
551 * be asking the DMU to do *anything*.
553 ASSERT(spa_config_held(os->os_spa, SCL_ALL, RW_WRITER) == 0);
555 if (object == DMU_USERUSED_OBJECT || object == DMU_GROUPUSED_OBJECT) {
556 dn = (object == DMU_USERUSED_OBJECT) ?
557 os->os_userused_dnode : os->os_groupused_dnode;
561 if ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE)
563 if ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)
566 (void) refcount_add(&dn->dn_holds, tag);
571 if (object == 0 || object >= DN_MAX_OBJECT)
574 mdn = os->os_meta_dnode;
578 if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) {
579 rw_enter(&mdn->dn_struct_rwlock, RW_READER);
580 drop_struct_lock = TRUE;
583 blk = dbuf_whichblock(mdn, object * sizeof (dnode_phys_t));
585 db = dbuf_hold(mdn, blk, FTAG);
586 if (drop_struct_lock)
587 rw_exit(&mdn->dn_struct_rwlock);
590 err = dbuf_read(db, NULL, DB_RF_CANFAIL);
596 ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT);
597 epb = db->db.db_size >> DNODE_SHIFT;
599 idx = object & (epb-1);
601 children_dnodes = dmu_buf_get_user(&db->db);
602 if (children_dnodes == NULL) {
604 children_dnodes = kmem_zalloc(epb * sizeof (dnode_t *),
606 if (winner = dmu_buf_set_user(&db->db, children_dnodes, NULL,
607 dnode_buf_pageout)) {
608 kmem_free(children_dnodes, epb * sizeof (dnode_t *));
609 children_dnodes = winner;
613 if ((dn = children_dnodes[idx]) == NULL) {
614 dnode_phys_t *dnp = (dnode_phys_t *)db->db.db_data+idx;
617 dn = dnode_create(os, dnp, db, object);
618 winner = atomic_cas_ptr(&children_dnodes[idx], NULL, dn);
619 if (winner != NULL) {
625 mutex_enter(&dn->dn_mtx);
627 if (dn->dn_free_txg ||
628 ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) ||
629 ((flag & DNODE_MUST_BE_FREE) &&
630 (type != DMU_OT_NONE || dn->dn_oldphys))) {
631 mutex_exit(&dn->dn_mtx);
633 return (type == DMU_OT_NONE ? ENOENT : EEXIST);
635 mutex_exit(&dn->dn_mtx);
637 if (refcount_add(&dn->dn_holds, tag) == 1)
638 dbuf_add_ref(db, dn);
641 ASSERT3P(dn->dn_dbuf, ==, db);
642 ASSERT3U(dn->dn_object, ==, object);
650 * Return held dnode if the object is allocated, NULL if not.
653 dnode_hold(objset_impl_t *os, uint64_t object, void *tag, dnode_t **dnp)
655 return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, tag, dnp));
659 * Can only add a reference if there is already at least one
660 * reference on the dnode. Returns FALSE if unable to add a
664 dnode_add_ref(dnode_t *dn, void *tag)
666 mutex_enter(&dn->dn_mtx);
667 if (refcount_is_zero(&dn->dn_holds)) {
668 mutex_exit(&dn->dn_mtx);
671 VERIFY(1 < refcount_add(&dn->dn_holds, tag));
672 mutex_exit(&dn->dn_mtx);
677 dnode_rele(dnode_t *dn, void *tag)
681 mutex_enter(&dn->dn_mtx);
682 refs = refcount_remove(&dn->dn_holds, tag);
683 mutex_exit(&dn->dn_mtx);
684 /* NOTE: the DNODE_DNODE does not have a dn_dbuf */
685 if (refs == 0 && dn->dn_dbuf)
686 dbuf_rele(dn->dn_dbuf, dn);
690 dnode_setdirty(dnode_t *dn, dmu_tx_t *tx)
692 objset_impl_t *os = dn->dn_objset;
693 uint64_t txg = tx->tx_txg;
695 if (DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
696 dsl_dataset_dirty(os->os_dsl_dataset, tx);
703 mutex_enter(&dn->dn_mtx);
704 ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg);
705 /* ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg); */
706 mutex_exit(&dn->dn_mtx);
709 mutex_enter(&os->os_lock);
712 * If we are already marked dirty, we're done.
714 if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) {
715 mutex_exit(&os->os_lock);
719 ASSERT(!refcount_is_zero(&dn->dn_holds) || list_head(&dn->dn_dbufs));
720 ASSERT(dn->dn_datablksz != 0);
721 ASSERT3U(dn->dn_next_bonuslen[txg&TXG_MASK], ==, 0);
722 ASSERT3U(dn->dn_next_blksz[txg&TXG_MASK], ==, 0);
724 dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
727 if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) {
728 list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn);
730 list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn);
733 mutex_exit(&os->os_lock);
736 * The dnode maintains a hold on its containing dbuf as
737 * long as there are holds on it. Each instantiated child
738 * dbuf maintaines a hold on the dnode. When the last child
739 * drops its hold, the dnode will drop its hold on the
740 * containing dbuf. We add a "dirty hold" here so that the
741 * dnode will hang around after we finish processing its
744 VERIFY(dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg));
746 (void) dbuf_dirty(dn->dn_dbuf, tx);
748 dsl_dataset_dirty(os->os_dsl_dataset, tx);
752 dnode_free(dnode_t *dn, dmu_tx_t *tx)
754 int txgoff = tx->tx_txg & TXG_MASK;
756 dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg);
758 /* we should be the only holder... hopefully */
759 /* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */
761 mutex_enter(&dn->dn_mtx);
762 if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) {
763 mutex_exit(&dn->dn_mtx);
766 dn->dn_free_txg = tx->tx_txg;
767 mutex_exit(&dn->dn_mtx);
770 * If the dnode is already dirty, it needs to be moved from
771 * the dirty list to the free list.
773 mutex_enter(&dn->dn_objset->os_lock);
774 if (list_link_active(&dn->dn_dirty_link[txgoff])) {
775 list_remove(&dn->dn_objset->os_dirty_dnodes[txgoff], dn);
776 list_insert_tail(&dn->dn_objset->os_free_dnodes[txgoff], dn);
777 mutex_exit(&dn->dn_objset->os_lock);
779 mutex_exit(&dn->dn_objset->os_lock);
780 dnode_setdirty(dn, tx);
785 * Try to change the block size for the indicated dnode. This can only
786 * succeed if there are no blocks allocated or dirty beyond first block
789 dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx)
791 dmu_buf_impl_t *db, *db_next;
795 size = SPA_MINBLOCKSIZE;
796 if (size > SPA_MAXBLOCKSIZE)
797 size = SPA_MAXBLOCKSIZE;
799 size = P2ROUNDUP(size, SPA_MINBLOCKSIZE);
801 if (ibs == dn->dn_indblkshift)
804 if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0)
807 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
809 /* Check for any allocated blocks beyond the first */
810 if (dn->dn_phys->dn_maxblkid != 0)
813 mutex_enter(&dn->dn_dbufs_mtx);
814 for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
815 db_next = list_next(&dn->dn_dbufs, db);
817 if (db->db_blkid != 0 && db->db_blkid != DB_BONUS_BLKID) {
818 mutex_exit(&dn->dn_dbufs_mtx);
822 mutex_exit(&dn->dn_dbufs_mtx);
824 if (ibs && dn->dn_nlevels != 1)
827 /* resize the old block */
828 err = dbuf_hold_impl(dn, 0, 0, TRUE, FTAG, &db);
830 dbuf_new_size(db, size, tx);
831 else if (err != ENOENT)
834 dnode_setdblksz(dn, size);
835 dnode_setdirty(dn, tx);
836 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size;
838 dn->dn_indblkshift = ibs;
839 dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs;
841 /* rele after we have fixed the blocksize in the dnode */
845 rw_exit(&dn->dn_struct_rwlock);
849 rw_exit(&dn->dn_struct_rwlock);
853 /* read-holding callers must not rely on the lock being continuously held */
855 dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t have_read)
857 uint64_t txgoff = tx->tx_txg & TXG_MASK;
858 int epbs, new_nlevels;
861 ASSERT(blkid != DB_BONUS_BLKID);
864 RW_READ_HELD(&dn->dn_struct_rwlock) :
865 RW_WRITE_HELD(&dn->dn_struct_rwlock));
868 * if we have a read-lock, check to see if we need to do any work
869 * before upgrading to a write-lock.
872 if (blkid <= dn->dn_maxblkid)
875 if (!rw_tryupgrade(&dn->dn_struct_rwlock)) {
876 rw_exit(&dn->dn_struct_rwlock);
877 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
881 if (blkid <= dn->dn_maxblkid)
884 dn->dn_maxblkid = blkid;
887 * Compute the number of levels necessary to support the new maxblkid.
890 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
891 for (sz = dn->dn_nblkptr;
892 sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs)
895 if (new_nlevels > dn->dn_nlevels) {
896 int old_nlevels = dn->dn_nlevels;
899 dbuf_dirty_record_t *new, *dr, *dr_next;
901 dn->dn_nlevels = new_nlevels;
903 ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]);
904 dn->dn_next_nlevels[txgoff] = new_nlevels;
906 /* dirty the left indirects */
907 db = dbuf_hold_level(dn, old_nlevels, 0, FTAG);
908 new = dbuf_dirty(db, tx);
911 /* transfer the dirty records to the new indirect */
912 mutex_enter(&dn->dn_mtx);
913 mutex_enter(&new->dt.di.dr_mtx);
914 list = &dn->dn_dirty_records[txgoff];
915 for (dr = list_head(list); dr; dr = dr_next) {
916 dr_next = list_next(&dn->dn_dirty_records[txgoff], dr);
917 if (dr->dr_dbuf->db_level != new_nlevels-1 &&
918 dr->dr_dbuf->db_blkid != DB_BONUS_BLKID) {
919 ASSERT(dr->dr_dbuf->db_level == old_nlevels-1);
920 list_remove(&dn->dn_dirty_records[txgoff], dr);
921 list_insert_tail(&new->dt.di.dr_children, dr);
925 mutex_exit(&new->dt.di.dr_mtx);
926 mutex_exit(&dn->dn_mtx);
931 rw_downgrade(&dn->dn_struct_rwlock);
935 dnode_clear_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
937 avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
940 free_range_t rp_tofind;
941 uint64_t endblk = blkid + nblks;
943 ASSERT(MUTEX_HELD(&dn->dn_mtx));
944 ASSERT(nblks <= UINT64_MAX - blkid); /* no overflow */
946 dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
947 blkid, nblks, tx->tx_txg);
948 rp_tofind.fr_blkid = blkid;
949 rp = avl_find(tree, &rp_tofind, &where);
951 rp = avl_nearest(tree, where, AVL_BEFORE);
953 rp = avl_nearest(tree, where, AVL_AFTER);
955 while (rp && (rp->fr_blkid <= blkid + nblks)) {
956 uint64_t fr_endblk = rp->fr_blkid + rp->fr_nblks;
957 free_range_t *nrp = AVL_NEXT(tree, rp);
959 if (blkid <= rp->fr_blkid && endblk >= fr_endblk) {
960 /* clear this entire range */
961 avl_remove(tree, rp);
962 kmem_free(rp, sizeof (free_range_t));
963 } else if (blkid <= rp->fr_blkid &&
964 endblk > rp->fr_blkid && endblk < fr_endblk) {
965 /* clear the beginning of this range */
966 rp->fr_blkid = endblk;
967 rp->fr_nblks = fr_endblk - endblk;
968 } else if (blkid > rp->fr_blkid && blkid < fr_endblk &&
969 endblk >= fr_endblk) {
970 /* clear the end of this range */
971 rp->fr_nblks = blkid - rp->fr_blkid;
972 } else if (blkid > rp->fr_blkid && endblk < fr_endblk) {
973 /* clear a chunk out of this range */
974 free_range_t *new_rp =
975 kmem_alloc(sizeof (free_range_t), KM_SLEEP);
977 new_rp->fr_blkid = endblk;
978 new_rp->fr_nblks = fr_endblk - endblk;
979 avl_insert_here(tree, new_rp, rp, AVL_AFTER);
980 rp->fr_nblks = blkid - rp->fr_blkid;
982 /* there may be no overlap */
988 dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx)
991 uint64_t blkoff, blkid, nblks;
992 int blksz, blkshift, head, tail;
996 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
997 blksz = dn->dn_datablksz;
998 blkshift = dn->dn_datablkshift;
999 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1002 len = UINT64_MAX - off;
1007 * First, block align the region to free:
1010 head = P2NPHASE(off, blksz);
1011 blkoff = P2PHASE(off, blksz);
1012 if ((off >> blkshift) > dn->dn_maxblkid)
1015 ASSERT(dn->dn_maxblkid == 0);
1016 if (off == 0 && len >= blksz) {
1017 /* Freeing the whole block; fast-track this request */
1021 } else if (off >= blksz) {
1022 /* Freeing past end-of-data */
1025 /* Freeing part of the block. */
1027 ASSERT3U(head, >, 0);
1031 /* zero out any partial block data at the start of the range */
1033 ASSERT3U(blkoff + head, ==, blksz);
1036 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off), TRUE,
1040 /* don't dirty if it isn't on disk and isn't dirty */
1041 if (db->db_last_dirty ||
1042 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1043 rw_exit(&dn->dn_struct_rwlock);
1044 dbuf_will_dirty(db, tx);
1045 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1046 data = db->db.db_data;
1047 bzero(data + blkoff, head);
1049 dbuf_rele(db, FTAG);
1055 /* If the range was less than one block, we're done */
1059 /* If the remaining range is past end of file, we're done */
1060 if ((off >> blkshift) > dn->dn_maxblkid)
1063 ASSERT(ISP2(blksz));
1067 tail = P2PHASE(len, blksz);
1069 ASSERT3U(P2PHASE(off, blksz), ==, 0);
1070 /* zero out any partial block data at the end of the range */
1074 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len),
1075 TRUE, FTAG, &db) == 0) {
1076 /* don't dirty if not on disk and not dirty */
1077 if (db->db_last_dirty ||
1078 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1079 rw_exit(&dn->dn_struct_rwlock);
1080 dbuf_will_dirty(db, tx);
1081 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1082 bzero(db->db.db_data, tail);
1084 dbuf_rele(db, FTAG);
1089 /* If the range did not include a full block, we are done */
1093 ASSERT(IS_P2ALIGNED(off, blksz));
1094 ASSERT(trunc || IS_P2ALIGNED(len, blksz));
1095 blkid = off >> blkshift;
1096 nblks = len >> blkshift;
1101 * Read in and mark all the level-1 indirects dirty,
1102 * so that they will stay in memory until syncing phase.
1103 * Always dirty the first and last indirect to make sure
1104 * we dirty all the partial indirects.
1106 if (dn->dn_nlevels > 1) {
1107 uint64_t i, first, last;
1108 int shift = epbs + dn->dn_datablkshift;
1110 first = blkid >> epbs;
1111 if (db = dbuf_hold_level(dn, 1, first, FTAG)) {
1112 dbuf_will_dirty(db, tx);
1113 dbuf_rele(db, FTAG);
1116 last = dn->dn_maxblkid >> epbs;
1118 last = (blkid + nblks - 1) >> epbs;
1119 if (last > first && (db = dbuf_hold_level(dn, 1, last, FTAG))) {
1120 dbuf_will_dirty(db, tx);
1121 dbuf_rele(db, FTAG);
1123 for (i = first + 1; i < last; i++) {
1124 uint64_t ibyte = i << shift;
1127 err = dnode_next_offset(dn,
1128 DNODE_FIND_HAVELOCK, &ibyte, 1, 1, 0);
1130 if (err == ESRCH || i >= last)
1133 db = dbuf_hold_level(dn, 1, i, FTAG);
1135 dbuf_will_dirty(db, tx);
1136 dbuf_rele(db, FTAG);
1142 * Add this range to the dnode range list.
1143 * We will finish up this free operation in the syncing phase.
1145 mutex_enter(&dn->dn_mtx);
1146 dnode_clear_range(dn, blkid, nblks, tx);
1148 free_range_t *rp, *found;
1150 avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
1152 /* Add new range to dn_ranges */
1153 rp = kmem_alloc(sizeof (free_range_t), KM_SLEEP);
1154 rp->fr_blkid = blkid;
1155 rp->fr_nblks = nblks;
1156 found = avl_find(tree, rp, &where);
1157 ASSERT(found == NULL);
1158 avl_insert(tree, rp, where);
1159 dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1160 blkid, nblks, tx->tx_txg);
1162 mutex_exit(&dn->dn_mtx);
1164 dbuf_free_range(dn, blkid, blkid + nblks - 1, tx);
1165 dnode_setdirty(dn, tx);
1167 if (trunc && dn->dn_maxblkid >= (off >> blkshift))
1168 dn->dn_maxblkid = (off >> blkshift ? (off >> blkshift) - 1 : 0);
1170 rw_exit(&dn->dn_struct_rwlock);
1173 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
1175 dnode_block_freed(dnode_t *dn, uint64_t blkid)
1177 free_range_t range_tofind;
1178 void *dp = spa_get_dsl(dn->dn_objset->os_spa);
1181 if (blkid == DB_BONUS_BLKID)
1185 * If we're in the process of opening the pool, dp will not be
1186 * set yet, but there shouldn't be anything dirty.
1191 if (dn->dn_free_txg)
1194 range_tofind.fr_blkid = blkid;
1195 mutex_enter(&dn->dn_mtx);
1196 for (i = 0; i < TXG_SIZE; i++) {
1197 free_range_t *range_found;
1200 range_found = avl_find(&dn->dn_ranges[i], &range_tofind, &idx);
1202 ASSERT(range_found->fr_nblks > 0);
1205 range_found = avl_nearest(&dn->dn_ranges[i], idx, AVL_BEFORE);
1207 range_found->fr_blkid + range_found->fr_nblks > blkid)
1210 mutex_exit(&dn->dn_mtx);
1211 return (i < TXG_SIZE);
1214 /* call from syncing context when we actually write/free space for this dnode */
1216 dnode_diduse_space(dnode_t *dn, int64_t delta)
1219 dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n",
1221 (u_longlong_t)dn->dn_phys->dn_used,
1224 mutex_enter(&dn->dn_mtx);
1225 space = DN_USED_BYTES(dn->dn_phys);
1227 ASSERT3U(space + delta, >=, space); /* no overflow */
1229 ASSERT3U(space, >=, -delta); /* no underflow */
1232 if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_DNODE_BYTES) {
1233 ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0);
1234 ASSERT3U(P2PHASE(space, 1<<DEV_BSHIFT), ==, 0);
1235 dn->dn_phys->dn_used = space >> DEV_BSHIFT;
1237 dn->dn_phys->dn_used = space;
1238 dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES;
1240 mutex_exit(&dn->dn_mtx);
1244 * Call when we think we're going to write/free space in open context.
1245 * Be conservative (ie. OK to write less than this or free more than
1246 * this, but don't write more or free less).
1249 dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx)
1251 objset_impl_t *os = dn->dn_objset;
1252 dsl_dataset_t *ds = os->os_dsl_dataset;
1255 space = spa_get_asize(os->os_spa, space);
1258 dsl_dir_willuse_space(ds->ds_dir, space, tx);
1260 dmu_tx_willuse_space(tx, space);
1264 * This function scans a block at the indicated "level" looking for
1265 * a hole or data (depending on 'flags'). If level > 0, then we are
1266 * scanning an indirect block looking at its pointers. If level == 0,
1267 * then we are looking at a block of dnodes. If we don't find what we
1268 * are looking for in the block, we return ESRCH. Otherwise, return
1269 * with *offset pointing to the beginning (if searching forwards) or
1270 * end (if searching backwards) of the range covered by the block
1271 * pointer we matched on (or dnode).
1273 * The basic search algorithm used below by dnode_next_offset() is to
1274 * use this function to search up the block tree (widen the search) until
1275 * we find something (i.e., we don't return ESRCH) and then search back
1276 * down the tree (narrow the search) until we reach our original search
1280 dnode_next_offset_level(dnode_t *dn, int flags, uint64_t *offset,
1281 int lvl, uint64_t blkfill, uint64_t txg)
1283 dmu_buf_impl_t *db = NULL;
1285 uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
1286 uint64_t epb = 1ULL << epbs;
1287 uint64_t minfill, maxfill;
1289 int i, inc, error, span;
1291 dprintf("probing object %llu offset %llx level %d of %u\n",
1292 dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels);
1294 hole = ((flags & DNODE_FIND_HOLE) != 0);
1295 inc = (flags & DNODE_FIND_BACKWARDS) ? -1 : 1;
1296 ASSERT(txg == 0 || !hole);
1298 if (lvl == dn->dn_phys->dn_nlevels) {
1300 epb = dn->dn_phys->dn_nblkptr;
1301 data = dn->dn_phys->dn_blkptr;
1303 uint64_t blkid = dbuf_whichblock(dn, *offset) >> (epbs * lvl);
1304 error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FTAG, &db);
1306 if (error != ENOENT)
1311 * This can only happen when we are searching up
1312 * the block tree for data. We don't really need to
1313 * adjust the offset, as we will just end up looking
1314 * at the pointer to this block in its parent, and its
1315 * going to be unallocated, so we will skip over it.
1319 error = dbuf_read(db, NULL, DB_RF_CANFAIL | DB_RF_HAVESTRUCT);
1321 dbuf_rele(db, FTAG);
1324 data = db->db.db_data;
1328 (db->db_blkptr == NULL || db->db_blkptr->blk_birth <= txg)) {
1330 * This can only happen when we are searching up the tree
1331 * and these conditions mean that we need to keep climbing.
1334 } else if (lvl == 0) {
1335 dnode_phys_t *dnp = data;
1337 ASSERT(dn->dn_type == DMU_OT_DNODE);
1339 for (i = (*offset >> span) & (blkfill - 1);
1340 i >= 0 && i < blkfill; i += inc) {
1341 if ((dnp[i].dn_type == DMU_OT_NONE) == hole)
1343 *offset += (1ULL << span) * inc;
1345 if (i < 0 || i == blkfill)
1348 blkptr_t *bp = data;
1349 uint64_t start = *offset;
1350 span = (lvl - 1) * epbs + dn->dn_datablkshift;
1352 maxfill = blkfill << ((lvl - 1) * epbs);
1359 *offset = *offset >> span;
1360 for (i = BF64_GET(*offset, 0, epbs);
1361 i >= 0 && i < epb; i += inc) {
1362 if (bp[i].blk_fill >= minfill &&
1363 bp[i].blk_fill <= maxfill &&
1364 (hole || bp[i].blk_birth > txg))
1366 if (inc > 0 || *offset > 0)
1369 *offset = *offset << span;
1371 /* traversing backwards; position offset at the end */
1372 ASSERT3U(*offset, <=, start);
1373 *offset = MIN(*offset + (1ULL << span) - 1, start);
1374 } else if (*offset < start) {
1377 if (i < 0 || i >= epb)
1382 dbuf_rele(db, FTAG);
1388 * Find the next hole, data, or sparse region at or after *offset.
1389 * The value 'blkfill' tells us how many items we expect to find
1390 * in an L0 data block; this value is 1 for normal objects,
1391 * DNODES_PER_BLOCK for the meta dnode, and some fraction of
1392 * DNODES_PER_BLOCK when searching for sparse regions thereof.
1396 * dnode_next_offset(dn, flags, offset, 1, 1, 0);
1397 * Finds the next/previous hole/data in a file.
1398 * Used in dmu_offset_next().
1400 * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
1401 * Finds the next free/allocated dnode an objset's meta-dnode.
1402 * Only finds objects that have new contents since txg (ie.
1403 * bonus buffer changes and content removal are ignored).
1404 * Used in dmu_object_next().
1406 * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
1407 * Finds the next L2 meta-dnode bp that's at most 1/4 full.
1408 * Used in dmu_object_alloc().
1411 dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset,
1412 int minlvl, uint64_t blkfill, uint64_t txg)
1414 uint64_t initial_offset = *offset;
1418 if (!(flags & DNODE_FIND_HAVELOCK))
1419 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1421 if (dn->dn_phys->dn_nlevels == 0) {
1426 if (dn->dn_datablkshift == 0) {
1427 if (*offset < dn->dn_datablksz) {
1428 if (flags & DNODE_FIND_HOLE)
1429 *offset = dn->dn_datablksz;
1436 maxlvl = dn->dn_phys->dn_nlevels;
1438 for (lvl = minlvl; lvl <= maxlvl; lvl++) {
1439 error = dnode_next_offset_level(dn,
1440 flags, offset, lvl, blkfill, txg);
1445 while (error == 0 && --lvl >= minlvl) {
1446 error = dnode_next_offset_level(dn,
1447 flags, offset, lvl, blkfill, txg);
1450 if (error == 0 && (flags & DNODE_FIND_BACKWARDS ?
1451 initial_offset < *offset : initial_offset > *offset))
1454 if (!(flags & DNODE_FIND_HAVELOCK))
1455 rw_exit(&dn->dn_struct_rwlock);