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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 #include <sys/zfs_context.h>
29 #include <sys/dnode.h>
31 #include <sys/dmu_impl.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/dmu_objset.h>
34 #include <sys/dsl_dir.h>
35 #include <sys/dsl_dataset.h>
38 #include <sys/dmu_zfetch.h>
39 #include <sys/range_tree.h>
40 #include <sys/trace_dnode.h>
42 static kmem_cache_t *dnode_cache;
44 * Define DNODE_STATS to turn on statistic gathering. By default, it is only
45 * turned on when DEBUG is also defined.
52 #define DNODE_STAT_ADD(stat) ((stat)++)
54 #define DNODE_STAT_ADD(stat) /* nothing */
55 #endif /* DNODE_STATS */
57 ASSERTV(static dnode_phys_t dnode_phys_zero);
59 int zfs_default_bs = SPA_MINBLOCKSHIFT;
60 int zfs_default_ibs = DN_MAX_INDBLKSHIFT;
63 static kmem_cbrc_t dnode_move(void *, void *, size_t, void *);
67 dbuf_compare(const void *x1, const void *x2)
69 const dmu_buf_impl_t *d1 = x1;
70 const dmu_buf_impl_t *d2 = x2;
72 if (d1->db_level < d2->db_level) {
75 if (d1->db_level > d2->db_level) {
79 if (d1->db_blkid < d2->db_blkid) {
82 if (d1->db_blkid > d2->db_blkid) {
86 if (d1->db_state == DB_SEARCH) {
87 ASSERT3S(d2->db_state, !=, DB_SEARCH);
89 } else if (d2->db_state == DB_SEARCH) {
90 ASSERT3S(d1->db_state, !=, DB_SEARCH);
94 if ((uintptr_t)d1 < (uintptr_t)d2) {
97 if ((uintptr_t)d1 > (uintptr_t)d2) {
105 dnode_cons(void *arg, void *unused, int kmflag)
110 rw_init(&dn->dn_struct_rwlock, NULL, RW_NOLOCKDEP, NULL);
111 mutex_init(&dn->dn_mtx, NULL, MUTEX_DEFAULT, NULL);
112 mutex_init(&dn->dn_dbufs_mtx, NULL, MUTEX_DEFAULT, NULL);
113 cv_init(&dn->dn_notxholds, NULL, CV_DEFAULT, NULL);
116 * Every dbuf has a reference, and dropping a tracked reference is
117 * O(number of references), so don't track dn_holds.
119 refcount_create_untracked(&dn->dn_holds);
120 refcount_create(&dn->dn_tx_holds);
121 list_link_init(&dn->dn_link);
123 bzero(&dn->dn_next_nblkptr[0], sizeof (dn->dn_next_nblkptr));
124 bzero(&dn->dn_next_nlevels[0], sizeof (dn->dn_next_nlevels));
125 bzero(&dn->dn_next_indblkshift[0], sizeof (dn->dn_next_indblkshift));
126 bzero(&dn->dn_next_bonustype[0], sizeof (dn->dn_next_bonustype));
127 bzero(&dn->dn_rm_spillblk[0], sizeof (dn->dn_rm_spillblk));
128 bzero(&dn->dn_next_bonuslen[0], sizeof (dn->dn_next_bonuslen));
129 bzero(&dn->dn_next_blksz[0], sizeof (dn->dn_next_blksz));
131 for (i = 0; i < TXG_SIZE; i++) {
132 list_link_init(&dn->dn_dirty_link[i]);
133 dn->dn_free_ranges[i] = NULL;
134 list_create(&dn->dn_dirty_records[i],
135 sizeof (dbuf_dirty_record_t),
136 offsetof(dbuf_dirty_record_t, dr_dirty_node));
139 dn->dn_allocated_txg = 0;
141 dn->dn_assigned_txg = 0;
143 dn->dn_dirtyctx_firstset = NULL;
145 dn->dn_have_spill = B_FALSE;
155 dn->dn_dbufs_count = 0;
156 dn->dn_unlisted_l0_blkid = 0;
157 avl_create(&dn->dn_dbufs, dbuf_compare, sizeof (dmu_buf_impl_t),
158 offsetof(dmu_buf_impl_t, db_link));
166 dnode_dest(void *arg, void *unused)
171 rw_destroy(&dn->dn_struct_rwlock);
172 mutex_destroy(&dn->dn_mtx);
173 mutex_destroy(&dn->dn_dbufs_mtx);
174 cv_destroy(&dn->dn_notxholds);
175 refcount_destroy(&dn->dn_holds);
176 refcount_destroy(&dn->dn_tx_holds);
177 ASSERT(!list_link_active(&dn->dn_link));
179 for (i = 0; i < TXG_SIZE; i++) {
180 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
181 ASSERT3P(dn->dn_free_ranges[i], ==, NULL);
182 list_destroy(&dn->dn_dirty_records[i]);
183 ASSERT0(dn->dn_next_nblkptr[i]);
184 ASSERT0(dn->dn_next_nlevels[i]);
185 ASSERT0(dn->dn_next_indblkshift[i]);
186 ASSERT0(dn->dn_next_bonustype[i]);
187 ASSERT0(dn->dn_rm_spillblk[i]);
188 ASSERT0(dn->dn_next_bonuslen[i]);
189 ASSERT0(dn->dn_next_blksz[i]);
192 ASSERT0(dn->dn_allocated_txg);
193 ASSERT0(dn->dn_free_txg);
194 ASSERT0(dn->dn_assigned_txg);
195 ASSERT0(dn->dn_dirtyctx);
196 ASSERT3P(dn->dn_dirtyctx_firstset, ==, NULL);
197 ASSERT3P(dn->dn_bonus, ==, NULL);
198 ASSERT(!dn->dn_have_spill);
199 ASSERT3P(dn->dn_zio, ==, NULL);
200 ASSERT0(dn->dn_oldused);
201 ASSERT0(dn->dn_oldflags);
202 ASSERT0(dn->dn_olduid);
203 ASSERT0(dn->dn_oldgid);
204 ASSERT0(dn->dn_newuid);
205 ASSERT0(dn->dn_newgid);
206 ASSERT0(dn->dn_id_flags);
208 ASSERT0(dn->dn_dbufs_count);
209 ASSERT0(dn->dn_unlisted_l0_blkid);
210 avl_destroy(&dn->dn_dbufs);
216 ASSERT(dnode_cache == NULL);
217 dnode_cache = kmem_cache_create("dnode_t", sizeof (dnode_t),
218 0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0);
219 kmem_cache_set_move(dnode_cache, dnode_move);
225 kmem_cache_destroy(dnode_cache);
232 dnode_verify(dnode_t *dn)
234 int drop_struct_lock = FALSE;
237 ASSERT(dn->dn_objset);
238 ASSERT(dn->dn_handle->dnh_dnode == dn);
240 ASSERT(DMU_OT_IS_VALID(dn->dn_phys->dn_type));
242 if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY))
245 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
246 rw_enter(&dn->dn_struct_rwlock, RW_READER);
247 drop_struct_lock = TRUE;
249 if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) {
251 ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT);
252 if (dn->dn_datablkshift) {
253 ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT);
254 ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT);
255 ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz);
257 ASSERT3U(dn->dn_nlevels, <=, 30);
258 ASSERT(DMU_OT_IS_VALID(dn->dn_type));
259 ASSERT3U(dn->dn_nblkptr, >=, 1);
260 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
261 ASSERT3U(dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
262 ASSERT3U(dn->dn_datablksz, ==,
263 dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
264 ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0);
265 ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) +
266 dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
267 for (i = 0; i < TXG_SIZE; i++) {
268 ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels);
271 if (dn->dn_phys->dn_type != DMU_OT_NONE)
272 ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels);
273 ASSERT(DMU_OBJECT_IS_SPECIAL(dn->dn_object) || dn->dn_dbuf != NULL);
274 if (dn->dn_dbuf != NULL) {
275 ASSERT3P(dn->dn_phys, ==,
276 (dnode_phys_t *)dn->dn_dbuf->db.db_data +
277 (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT)));
279 if (drop_struct_lock)
280 rw_exit(&dn->dn_struct_rwlock);
285 dnode_byteswap(dnode_phys_t *dnp)
287 uint64_t *buf64 = (void*)&dnp->dn_blkptr;
290 if (dnp->dn_type == DMU_OT_NONE) {
291 bzero(dnp, sizeof (dnode_phys_t));
295 dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec);
296 dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen);
297 dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid);
298 dnp->dn_used = BSWAP_64(dnp->dn_used);
301 * dn_nblkptr is only one byte, so it's OK to read it in either
302 * byte order. We can't read dn_bouslen.
304 ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT);
305 ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR);
306 for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++)
307 buf64[i] = BSWAP_64(buf64[i]);
310 * OK to check dn_bonuslen for zero, because it won't matter if
311 * we have the wrong byte order. This is necessary because the
312 * dnode dnode is smaller than a regular dnode.
314 if (dnp->dn_bonuslen != 0) {
316 * Note that the bonus length calculated here may be
317 * longer than the actual bonus buffer. This is because
318 * we always put the bonus buffer after the last block
319 * pointer (instead of packing it against the end of the
322 int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t);
323 size_t len = DN_MAX_BONUSLEN - off;
324 dmu_object_byteswap_t byteswap;
325 ASSERT(DMU_OT_IS_VALID(dnp->dn_bonustype));
326 byteswap = DMU_OT_BYTESWAP(dnp->dn_bonustype);
327 dmu_ot_byteswap[byteswap].ob_func(dnp->dn_bonus + off, len);
330 /* Swap SPILL block if we have one */
331 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)
332 byteswap_uint64_array(&dnp->dn_spill, sizeof (blkptr_t));
337 dnode_buf_byteswap(void *vbuf, size_t size)
339 dnode_phys_t *buf = vbuf;
342 ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT));
343 ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0);
345 size >>= DNODE_SHIFT;
346 for (i = 0; i < size; i++) {
353 dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx)
355 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
357 dnode_setdirty(dn, tx);
358 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
359 ASSERT3U(newsize, <=, DN_MAX_BONUSLEN -
360 (dn->dn_nblkptr-1) * sizeof (blkptr_t));
361 dn->dn_bonuslen = newsize;
363 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = DN_ZERO_BONUSLEN;
365 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
366 rw_exit(&dn->dn_struct_rwlock);
370 dnode_setbonus_type(dnode_t *dn, dmu_object_type_t newtype, dmu_tx_t *tx)
372 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
373 dnode_setdirty(dn, tx);
374 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
375 dn->dn_bonustype = newtype;
376 dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
377 rw_exit(&dn->dn_struct_rwlock);
381 dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx)
383 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
384 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
385 dnode_setdirty(dn, tx);
386 dn->dn_rm_spillblk[tx->tx_txg&TXG_MASK] = DN_KILL_SPILLBLK;
387 dn->dn_have_spill = B_FALSE;
391 dnode_setdblksz(dnode_t *dn, int size)
393 ASSERT0(P2PHASE(size, SPA_MINBLOCKSIZE));
394 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
395 ASSERT3U(size, >=, SPA_MINBLOCKSIZE);
396 ASSERT3U(size >> SPA_MINBLOCKSHIFT, <,
397 1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8));
398 dn->dn_datablksz = size;
399 dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT;
400 dn->dn_datablkshift = ISP2(size) ? highbit64(size - 1) : 0;
404 dnode_create(objset_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db,
405 uint64_t object, dnode_handle_t *dnh)
409 dn = kmem_cache_alloc(dnode_cache, KM_SLEEP);
410 ASSERT(!POINTER_IS_VALID(dn->dn_objset));
414 * Defer setting dn_objset until the dnode is ready to be a candidate
415 * for the dnode_move() callback.
417 dn->dn_object = object;
422 if (dnp->dn_datablkszsec) {
423 dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
425 dn->dn_datablksz = 0;
426 dn->dn_datablkszsec = 0;
427 dn->dn_datablkshift = 0;
429 dn->dn_indblkshift = dnp->dn_indblkshift;
430 dn->dn_nlevels = dnp->dn_nlevels;
431 dn->dn_type = dnp->dn_type;
432 dn->dn_nblkptr = dnp->dn_nblkptr;
433 dn->dn_checksum = dnp->dn_checksum;
434 dn->dn_compress = dnp->dn_compress;
435 dn->dn_bonustype = dnp->dn_bonustype;
436 dn->dn_bonuslen = dnp->dn_bonuslen;
437 dn->dn_maxblkid = dnp->dn_maxblkid;
438 dn->dn_have_spill = ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0);
441 dmu_zfetch_init(&dn->dn_zfetch, dn);
443 ASSERT(DMU_OT_IS_VALID(dn->dn_phys->dn_type));
445 mutex_enter(&os->os_lock);
446 if (dnh->dnh_dnode != NULL) {
447 /* Lost the allocation race. */
448 mutex_exit(&os->os_lock);
449 kmem_cache_free(dnode_cache, dn);
450 return (dnh->dnh_dnode);
454 * Exclude special dnodes from os_dnodes so an empty os_dnodes
455 * signifies that the special dnodes have no references from
456 * their children (the entries in os_dnodes). This allows
457 * dnode_destroy() to easily determine if the last child has
458 * been removed and then complete eviction of the objset.
460 if (!DMU_OBJECT_IS_SPECIAL(object))
461 list_insert_head(&os->os_dnodes, dn);
465 * Everything else must be valid before assigning dn_objset
466 * makes the dnode eligible for dnode_move().
471 mutex_exit(&os->os_lock);
473 arc_space_consume(sizeof (dnode_t), ARC_SPACE_OTHER);
478 * Caller must be holding the dnode handle, which is released upon return.
481 dnode_destroy(dnode_t *dn)
483 objset_t *os = dn->dn_objset;
484 boolean_t complete_os_eviction = B_FALSE;
486 ASSERT((dn->dn_id_flags & DN_ID_NEW_EXIST) == 0);
488 mutex_enter(&os->os_lock);
489 POINTER_INVALIDATE(&dn->dn_objset);
490 if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
491 list_remove(&os->os_dnodes, dn);
492 complete_os_eviction =
493 list_is_empty(&os->os_dnodes) &&
494 list_link_active(&os->os_evicting_node);
496 mutex_exit(&os->os_lock);
498 /* the dnode can no longer move, so we can release the handle */
499 zrl_remove(&dn->dn_handle->dnh_zrlock);
501 dn->dn_allocated_txg = 0;
503 dn->dn_assigned_txg = 0;
506 if (dn->dn_dirtyctx_firstset != NULL) {
507 kmem_free(dn->dn_dirtyctx_firstset, 1);
508 dn->dn_dirtyctx_firstset = NULL;
510 if (dn->dn_bonus != NULL) {
511 mutex_enter(&dn->dn_bonus->db_mtx);
512 dbuf_evict(dn->dn_bonus);
517 dn->dn_have_spill = B_FALSE;
525 dn->dn_unlisted_l0_blkid = 0;
527 dmu_zfetch_rele(&dn->dn_zfetch);
528 kmem_cache_free(dnode_cache, dn);
529 arc_space_return(sizeof (dnode_t), ARC_SPACE_OTHER);
531 if (complete_os_eviction)
532 dmu_objset_evict_done(os);
536 dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
537 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
541 ASSERT3U(blocksize, <=,
542 spa_maxblocksize(dmu_objset_spa(dn->dn_objset)));
544 blocksize = 1 << zfs_default_bs;
546 blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE);
549 ibs = zfs_default_ibs;
551 ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT);
553 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset,
554 dn->dn_object, tx->tx_txg, blocksize, ibs);
556 ASSERT(dn->dn_type == DMU_OT_NONE);
557 ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0);
558 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE);
559 ASSERT(ot != DMU_OT_NONE);
560 ASSERT(DMU_OT_IS_VALID(ot));
561 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
562 (bonustype == DMU_OT_SA && bonuslen == 0) ||
563 (bonustype != DMU_OT_NONE && bonuslen != 0));
564 ASSERT(DMU_OT_IS_VALID(bonustype));
565 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
566 ASSERT(dn->dn_type == DMU_OT_NONE);
567 ASSERT0(dn->dn_maxblkid);
568 ASSERT0(dn->dn_allocated_txg);
569 ASSERT0(dn->dn_assigned_txg);
570 ASSERT(refcount_is_zero(&dn->dn_tx_holds));
571 ASSERT3U(refcount_count(&dn->dn_holds), <=, 1);
572 ASSERT(avl_is_empty(&dn->dn_dbufs));
574 for (i = 0; i < TXG_SIZE; i++) {
575 ASSERT0(dn->dn_next_nblkptr[i]);
576 ASSERT0(dn->dn_next_nlevels[i]);
577 ASSERT0(dn->dn_next_indblkshift[i]);
578 ASSERT0(dn->dn_next_bonuslen[i]);
579 ASSERT0(dn->dn_next_bonustype[i]);
580 ASSERT0(dn->dn_rm_spillblk[i]);
581 ASSERT0(dn->dn_next_blksz[i]);
582 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
583 ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL);
584 ASSERT3P(dn->dn_free_ranges[i], ==, NULL);
588 dnode_setdblksz(dn, blocksize);
589 dn->dn_indblkshift = ibs;
591 if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
595 ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
596 dn->dn_bonustype = bonustype;
597 dn->dn_bonuslen = bonuslen;
598 dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
599 dn->dn_compress = ZIO_COMPRESS_INHERIT;
603 if (dn->dn_dirtyctx_firstset) {
604 kmem_free(dn->dn_dirtyctx_firstset, 1);
605 dn->dn_dirtyctx_firstset = NULL;
608 dn->dn_allocated_txg = tx->tx_txg;
611 dnode_setdirty(dn, tx);
612 dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs;
613 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
614 dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
615 dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz;
619 dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
620 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
624 ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE);
625 ASSERT3U(blocksize, <=,
626 spa_maxblocksize(dmu_objset_spa(dn->dn_objset)));
627 ASSERT0(blocksize % SPA_MINBLOCKSIZE);
628 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
629 ASSERT(tx->tx_txg != 0);
630 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
631 (bonustype != DMU_OT_NONE && bonuslen != 0) ||
632 (bonustype == DMU_OT_SA && bonuslen == 0));
633 ASSERT(DMU_OT_IS_VALID(bonustype));
634 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
636 /* clean up any unreferenced dbufs */
637 dnode_evict_dbufs(dn);
641 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
642 dnode_setdirty(dn, tx);
643 if (dn->dn_datablksz != blocksize) {
644 /* change blocksize */
645 ASSERT(dn->dn_maxblkid == 0 &&
646 (BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) ||
647 dnode_block_freed(dn, 0)));
648 dnode_setdblksz(dn, blocksize);
649 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize;
651 if (dn->dn_bonuslen != bonuslen)
652 dn->dn_next_bonuslen[tx->tx_txg&TXG_MASK] = bonuslen;
654 if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
657 nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
658 if (dn->dn_bonustype != bonustype)
659 dn->dn_next_bonustype[tx->tx_txg&TXG_MASK] = bonustype;
660 if (dn->dn_nblkptr != nblkptr)
661 dn->dn_next_nblkptr[tx->tx_txg&TXG_MASK] = nblkptr;
662 if (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
663 dbuf_rm_spill(dn, tx);
664 dnode_rm_spill(dn, tx);
666 rw_exit(&dn->dn_struct_rwlock);
671 /* change bonus size and type */
672 mutex_enter(&dn->dn_mtx);
673 dn->dn_bonustype = bonustype;
674 dn->dn_bonuslen = bonuslen;
675 dn->dn_nblkptr = nblkptr;
676 dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
677 dn->dn_compress = ZIO_COMPRESS_INHERIT;
678 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
680 /* fix up the bonus db_size */
682 dn->dn_bonus->db.db_size =
683 DN_MAX_BONUSLEN - (dn->dn_nblkptr-1) * sizeof (blkptr_t);
684 ASSERT(dn->dn_bonuslen <= dn->dn_bonus->db.db_size);
687 dn->dn_allocated_txg = tx->tx_txg;
688 mutex_exit(&dn->dn_mtx);
694 uint64_t dms_dnode_invalid;
695 uint64_t dms_dnode_recheck1;
696 uint64_t dms_dnode_recheck2;
697 uint64_t dms_dnode_special;
698 uint64_t dms_dnode_handle;
699 uint64_t dms_dnode_rwlock;
700 uint64_t dms_dnode_active;
702 #endif /* DNODE_STATS */
705 dnode_move_impl(dnode_t *odn, dnode_t *ndn)
709 ASSERT(!RW_LOCK_HELD(&odn->dn_struct_rwlock));
710 ASSERT(MUTEX_NOT_HELD(&odn->dn_mtx));
711 ASSERT(MUTEX_NOT_HELD(&odn->dn_dbufs_mtx));
712 ASSERT(!RW_LOCK_HELD(&odn->dn_zfetch.zf_rwlock));
715 ndn->dn_objset = odn->dn_objset;
716 ndn->dn_object = odn->dn_object;
717 ndn->dn_dbuf = odn->dn_dbuf;
718 ndn->dn_handle = odn->dn_handle;
719 ndn->dn_phys = odn->dn_phys;
720 ndn->dn_type = odn->dn_type;
721 ndn->dn_bonuslen = odn->dn_bonuslen;
722 ndn->dn_bonustype = odn->dn_bonustype;
723 ndn->dn_nblkptr = odn->dn_nblkptr;
724 ndn->dn_checksum = odn->dn_checksum;
725 ndn->dn_compress = odn->dn_compress;
726 ndn->dn_nlevels = odn->dn_nlevels;
727 ndn->dn_indblkshift = odn->dn_indblkshift;
728 ndn->dn_datablkshift = odn->dn_datablkshift;
729 ndn->dn_datablkszsec = odn->dn_datablkszsec;
730 ndn->dn_datablksz = odn->dn_datablksz;
731 ndn->dn_maxblkid = odn->dn_maxblkid;
732 bcopy(&odn->dn_next_nblkptr[0], &ndn->dn_next_nblkptr[0],
733 sizeof (odn->dn_next_nblkptr));
734 bcopy(&odn->dn_next_nlevels[0], &ndn->dn_next_nlevels[0],
735 sizeof (odn->dn_next_nlevels));
736 bcopy(&odn->dn_next_indblkshift[0], &ndn->dn_next_indblkshift[0],
737 sizeof (odn->dn_next_indblkshift));
738 bcopy(&odn->dn_next_bonustype[0], &ndn->dn_next_bonustype[0],
739 sizeof (odn->dn_next_bonustype));
740 bcopy(&odn->dn_rm_spillblk[0], &ndn->dn_rm_spillblk[0],
741 sizeof (odn->dn_rm_spillblk));
742 bcopy(&odn->dn_next_bonuslen[0], &ndn->dn_next_bonuslen[0],
743 sizeof (odn->dn_next_bonuslen));
744 bcopy(&odn->dn_next_blksz[0], &ndn->dn_next_blksz[0],
745 sizeof (odn->dn_next_blksz));
746 for (i = 0; i < TXG_SIZE; i++) {
747 list_move_tail(&ndn->dn_dirty_records[i],
748 &odn->dn_dirty_records[i]);
750 bcopy(&odn->dn_free_ranges[0], &ndn->dn_free_ranges[0],
751 sizeof (odn->dn_free_ranges));
752 ndn->dn_allocated_txg = odn->dn_allocated_txg;
753 ndn->dn_free_txg = odn->dn_free_txg;
754 ndn->dn_assigned_txg = odn->dn_assigned_txg;
755 ndn->dn_dirtyctx = odn->dn_dirtyctx;
756 ndn->dn_dirtyctx_firstset = odn->dn_dirtyctx_firstset;
757 ASSERT(refcount_count(&odn->dn_tx_holds) == 0);
758 refcount_transfer(&ndn->dn_holds, &odn->dn_holds);
759 ASSERT(avl_is_empty(&ndn->dn_dbufs));
760 avl_swap(&ndn->dn_dbufs, &odn->dn_dbufs);
761 ndn->dn_dbufs_count = odn->dn_dbufs_count;
762 ndn->dn_unlisted_l0_blkid = odn->dn_unlisted_l0_blkid;
763 ndn->dn_bonus = odn->dn_bonus;
764 ndn->dn_have_spill = odn->dn_have_spill;
765 ndn->dn_zio = odn->dn_zio;
766 ndn->dn_oldused = odn->dn_oldused;
767 ndn->dn_oldflags = odn->dn_oldflags;
768 ndn->dn_olduid = odn->dn_olduid;
769 ndn->dn_oldgid = odn->dn_oldgid;
770 ndn->dn_newuid = odn->dn_newuid;
771 ndn->dn_newgid = odn->dn_newgid;
772 ndn->dn_id_flags = odn->dn_id_flags;
773 dmu_zfetch_init(&ndn->dn_zfetch, NULL);
774 list_move_tail(&ndn->dn_zfetch.zf_stream, &odn->dn_zfetch.zf_stream);
775 ndn->dn_zfetch.zf_dnode = odn->dn_zfetch.zf_dnode;
776 ndn->dn_zfetch.zf_stream_cnt = odn->dn_zfetch.zf_stream_cnt;
777 ndn->dn_zfetch.zf_alloc_fail = odn->dn_zfetch.zf_alloc_fail;
780 * Update back pointers. Updating the handle fixes the back pointer of
781 * every descendant dbuf as well as the bonus dbuf.
783 ASSERT(ndn->dn_handle->dnh_dnode == odn);
784 ndn->dn_handle->dnh_dnode = ndn;
785 if (ndn->dn_zfetch.zf_dnode == odn) {
786 ndn->dn_zfetch.zf_dnode = ndn;
790 * Invalidate the original dnode by clearing all of its back pointers.
793 odn->dn_handle = NULL;
794 avl_create(&odn->dn_dbufs, dbuf_compare, sizeof (dmu_buf_impl_t),
795 offsetof(dmu_buf_impl_t, db_link));
796 odn->dn_dbufs_count = 0;
797 odn->dn_unlisted_l0_blkid = 0;
798 odn->dn_bonus = NULL;
799 odn->dn_zfetch.zf_dnode = NULL;
802 * Set the low bit of the objset pointer to ensure that dnode_move()
803 * recognizes the dnode as invalid in any subsequent callback.
805 POINTER_INVALIDATE(&odn->dn_objset);
808 * Satisfy the destructor.
810 for (i = 0; i < TXG_SIZE; i++) {
811 list_create(&odn->dn_dirty_records[i],
812 sizeof (dbuf_dirty_record_t),
813 offsetof(dbuf_dirty_record_t, dr_dirty_node));
814 odn->dn_free_ranges[i] = NULL;
815 odn->dn_next_nlevels[i] = 0;
816 odn->dn_next_indblkshift[i] = 0;
817 odn->dn_next_bonustype[i] = 0;
818 odn->dn_rm_spillblk[i] = 0;
819 odn->dn_next_bonuslen[i] = 0;
820 odn->dn_next_blksz[i] = 0;
822 odn->dn_allocated_txg = 0;
823 odn->dn_free_txg = 0;
824 odn->dn_assigned_txg = 0;
825 odn->dn_dirtyctx = 0;
826 odn->dn_dirtyctx_firstset = NULL;
827 odn->dn_have_spill = B_FALSE;
830 odn->dn_oldflags = 0;
835 odn->dn_id_flags = 0;
841 odn->dn_moved = (uint8_t)-1;
846 dnode_move(void *buf, void *newbuf, size_t size, void *arg)
848 dnode_t *odn = buf, *ndn = newbuf;
854 * The dnode is on the objset's list of known dnodes if the objset
855 * pointer is valid. We set the low bit of the objset pointer when
856 * freeing the dnode to invalidate it, and the memory patterns written
857 * by kmem (baddcafe and deadbeef) set at least one of the two low bits.
858 * A newly created dnode sets the objset pointer last of all to indicate
859 * that the dnode is known and in a valid state to be moved by this
863 if (!POINTER_IS_VALID(os)) {
864 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_invalid);
865 return (KMEM_CBRC_DONT_KNOW);
869 * Ensure that the objset does not go away during the move.
871 rw_enter(&os_lock, RW_WRITER);
872 if (os != odn->dn_objset) {
874 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_recheck1);
875 return (KMEM_CBRC_DONT_KNOW);
879 * If the dnode is still valid, then so is the objset. We know that no
880 * valid objset can be freed while we hold os_lock, so we can safely
881 * ensure that the objset remains in use.
883 mutex_enter(&os->os_lock);
886 * Recheck the objset pointer in case the dnode was removed just before
887 * acquiring the lock.
889 if (os != odn->dn_objset) {
890 mutex_exit(&os->os_lock);
892 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_recheck2);
893 return (KMEM_CBRC_DONT_KNOW);
897 * At this point we know that as long as we hold os->os_lock, the dnode
898 * cannot be freed and fields within the dnode can be safely accessed.
899 * The objset listing this dnode cannot go away as long as this dnode is
903 if (DMU_OBJECT_IS_SPECIAL(odn->dn_object)) {
904 mutex_exit(&os->os_lock);
905 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_special);
906 return (KMEM_CBRC_NO);
908 ASSERT(odn->dn_dbuf != NULL); /* only "special" dnodes have no parent */
911 * Lock the dnode handle to prevent the dnode from obtaining any new
912 * holds. This also prevents the descendant dbufs and the bonus dbuf
913 * from accessing the dnode, so that we can discount their holds. The
914 * handle is safe to access because we know that while the dnode cannot
915 * go away, neither can its handle. Once we hold dnh_zrlock, we can
916 * safely move any dnode referenced only by dbufs.
918 if (!zrl_tryenter(&odn->dn_handle->dnh_zrlock)) {
919 mutex_exit(&os->os_lock);
920 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_handle);
921 return (KMEM_CBRC_LATER);
925 * Ensure a consistent view of the dnode's holds and the dnode's dbufs.
926 * We need to guarantee that there is a hold for every dbuf in order to
927 * determine whether the dnode is actively referenced. Falsely matching
928 * a dbuf to an active hold would lead to an unsafe move. It's possible
929 * that a thread already having an active dnode hold is about to add a
930 * dbuf, and we can't compare hold and dbuf counts while the add is in
933 if (!rw_tryenter(&odn->dn_struct_rwlock, RW_WRITER)) {
934 zrl_exit(&odn->dn_handle->dnh_zrlock);
935 mutex_exit(&os->os_lock);
936 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_rwlock);
937 return (KMEM_CBRC_LATER);
941 * A dbuf may be removed (evicted) without an active dnode hold. In that
942 * case, the dbuf count is decremented under the handle lock before the
943 * dbuf's hold is released. This order ensures that if we count the hold
944 * after the dbuf is removed but before its hold is released, we will
945 * treat the unmatched hold as active and exit safely. If we count the
946 * hold before the dbuf is removed, the hold is discounted, and the
947 * removal is blocked until the move completes.
949 refcount = refcount_count(&odn->dn_holds);
950 ASSERT(refcount >= 0);
951 dbufs = odn->dn_dbufs_count;
953 /* We can't have more dbufs than dnode holds. */
954 ASSERT3U(dbufs, <=, refcount);
955 DTRACE_PROBE3(dnode__move, dnode_t *, odn, int64_t, refcount,
958 if (refcount > dbufs) {
959 rw_exit(&odn->dn_struct_rwlock);
960 zrl_exit(&odn->dn_handle->dnh_zrlock);
961 mutex_exit(&os->os_lock);
962 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_active);
963 return (KMEM_CBRC_LATER);
966 rw_exit(&odn->dn_struct_rwlock);
969 * At this point we know that anyone with a hold on the dnode is not
970 * actively referencing it. The dnode is known and in a valid state to
971 * move. We're holding the locks needed to execute the critical section.
973 dnode_move_impl(odn, ndn);
975 list_link_replace(&odn->dn_link, &ndn->dn_link);
976 /* If the dnode was safe to move, the refcount cannot have changed. */
977 ASSERT(refcount == refcount_count(&ndn->dn_holds));
978 ASSERT(dbufs == ndn->dn_dbufs_count);
979 zrl_exit(&ndn->dn_handle->dnh_zrlock); /* handle has moved */
980 mutex_exit(&os->os_lock);
982 return (KMEM_CBRC_YES);
987 dnode_special_close(dnode_handle_t *dnh)
989 dnode_t *dn = dnh->dnh_dnode;
992 * Wait for final references to the dnode to clear. This can
993 * only happen if the arc is asyncronously evicting state that
994 * has a hold on this dnode while we are trying to evict this
997 while (refcount_count(&dn->dn_holds) > 0)
999 ASSERT(dn->dn_dbuf == NULL ||
1000 dmu_buf_get_user(&dn->dn_dbuf->db) == NULL);
1001 zrl_add(&dnh->dnh_zrlock);
1002 dnode_destroy(dn); /* implicit zrl_remove() */
1003 zrl_destroy(&dnh->dnh_zrlock);
1004 dnh->dnh_dnode = NULL;
1008 dnode_special_open(objset_t *os, dnode_phys_t *dnp, uint64_t object,
1009 dnode_handle_t *dnh)
1013 dn = dnode_create(os, dnp, NULL, object, dnh);
1014 zrl_init(&dnh->dnh_zrlock);
1019 dnode_buf_pageout(void *dbu)
1021 dnode_children_t *children_dnodes = dbu;
1024 for (i = 0; i < children_dnodes->dnc_count; i++) {
1025 dnode_handle_t *dnh = &children_dnodes->dnc_children[i];
1029 * The dnode handle lock guards against the dnode moving to
1030 * another valid address, so there is no need here to guard
1031 * against changes to or from NULL.
1033 if (dnh->dnh_dnode == NULL) {
1034 zrl_destroy(&dnh->dnh_zrlock);
1038 zrl_add(&dnh->dnh_zrlock);
1039 dn = dnh->dnh_dnode;
1041 * If there are holds on this dnode, then there should
1042 * be holds on the dnode's containing dbuf as well; thus
1043 * it wouldn't be eligible for eviction and this function
1044 * would not have been called.
1046 ASSERT(refcount_is_zero(&dn->dn_holds));
1047 ASSERT(refcount_is_zero(&dn->dn_tx_holds));
1049 dnode_destroy(dn); /* implicit zrl_remove() */
1050 zrl_destroy(&dnh->dnh_zrlock);
1051 dnh->dnh_dnode = NULL;
1053 kmem_free(children_dnodes, sizeof (dnode_children_t) +
1054 children_dnodes->dnc_count * sizeof (dnode_handle_t));
1059 * EINVAL - invalid object number.
1061 * succeeds even for free dnodes.
1064 dnode_hold_impl(objset_t *os, uint64_t object, int flag,
1065 void *tag, dnode_t **dnp)
1068 int drop_struct_lock = FALSE;
1073 dnode_children_t *children_dnodes;
1074 dnode_handle_t *dnh;
1077 * If you are holding the spa config lock as writer, you shouldn't
1078 * be asking the DMU to do *anything* unless it's the root pool
1079 * which may require us to read from the root filesystem while
1080 * holding some (not all) of the locks as writer.
1082 ASSERT(spa_config_held(os->os_spa, SCL_ALL, RW_WRITER) == 0 ||
1083 (spa_is_root(os->os_spa) &&
1084 spa_config_held(os->os_spa, SCL_STATE, RW_WRITER)));
1086 if (object == DMU_USERUSED_OBJECT || object == DMU_GROUPUSED_OBJECT) {
1087 dn = (object == DMU_USERUSED_OBJECT) ?
1088 DMU_USERUSED_DNODE(os) : DMU_GROUPUSED_DNODE(os);
1090 return (SET_ERROR(ENOENT));
1092 if ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE)
1093 return (SET_ERROR(ENOENT));
1094 if ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)
1095 return (SET_ERROR(EEXIST));
1097 (void) refcount_add(&dn->dn_holds, tag);
1102 if (object == 0 || object >= DN_MAX_OBJECT)
1103 return (SET_ERROR(EINVAL));
1105 mdn = DMU_META_DNODE(os);
1106 ASSERT(mdn->dn_object == DMU_META_DNODE_OBJECT);
1110 if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) {
1111 rw_enter(&mdn->dn_struct_rwlock, RW_READER);
1112 drop_struct_lock = TRUE;
1115 blk = dbuf_whichblock(mdn, 0, object * sizeof (dnode_phys_t));
1117 db = dbuf_hold(mdn, blk, FTAG);
1118 if (drop_struct_lock)
1119 rw_exit(&mdn->dn_struct_rwlock);
1121 return (SET_ERROR(EIO));
1122 err = dbuf_read(db, NULL, DB_RF_CANFAIL);
1124 dbuf_rele(db, FTAG);
1128 ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT);
1129 epb = db->db.db_size >> DNODE_SHIFT;
1131 idx = object & (epb-1);
1133 ASSERT(DB_DNODE(db)->dn_type == DMU_OT_DNODE);
1134 children_dnodes = dmu_buf_get_user(&db->db);
1135 if (children_dnodes == NULL) {
1137 dnode_children_t *winner;
1138 children_dnodes = kmem_zalloc(sizeof (dnode_children_t) +
1139 epb * sizeof (dnode_handle_t), KM_SLEEP);
1140 children_dnodes->dnc_count = epb;
1141 dnh = &children_dnodes->dnc_children[0];
1142 for (i = 0; i < epb; i++) {
1143 zrl_init(&dnh[i].dnh_zrlock);
1145 dmu_buf_init_user(&children_dnodes->dnc_dbu,
1146 dnode_buf_pageout, NULL);
1147 winner = dmu_buf_set_user(&db->db, &children_dnodes->dnc_dbu);
1148 if (winner != NULL) {
1150 for (i = 0; i < epb; i++) {
1151 zrl_destroy(&dnh[i].dnh_zrlock);
1154 kmem_free(children_dnodes, sizeof (dnode_children_t) +
1155 epb * sizeof (dnode_handle_t));
1156 children_dnodes = winner;
1159 ASSERT(children_dnodes->dnc_count == epb);
1161 dnh = &children_dnodes->dnc_children[idx];
1162 zrl_add(&dnh->dnh_zrlock);
1163 dn = dnh->dnh_dnode;
1165 dnode_phys_t *phys = (dnode_phys_t *)db->db.db_data+idx;
1167 dn = dnode_create(os, phys, db, object, dnh);
1170 mutex_enter(&dn->dn_mtx);
1172 if (dn->dn_free_txg ||
1173 ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) ||
1174 ((flag & DNODE_MUST_BE_FREE) &&
1175 (type != DMU_OT_NONE || !refcount_is_zero(&dn->dn_holds)))) {
1176 mutex_exit(&dn->dn_mtx);
1177 zrl_remove(&dnh->dnh_zrlock);
1178 dbuf_rele(db, FTAG);
1179 return (type == DMU_OT_NONE ? ENOENT : EEXIST);
1181 if (refcount_add(&dn->dn_holds, tag) == 1)
1182 dbuf_add_ref(db, dnh);
1183 mutex_exit(&dn->dn_mtx);
1185 /* Now we can rely on the hold to prevent the dnode from moving. */
1186 zrl_remove(&dnh->dnh_zrlock);
1189 ASSERT3P(dn->dn_dbuf, ==, db);
1190 ASSERT3U(dn->dn_object, ==, object);
1191 dbuf_rele(db, FTAG);
1198 * Return held dnode if the object is allocated, NULL if not.
1201 dnode_hold(objset_t *os, uint64_t object, void *tag, dnode_t **dnp)
1203 return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, tag, dnp));
1207 * Can only add a reference if there is already at least one
1208 * reference on the dnode. Returns FALSE if unable to add a
1212 dnode_add_ref(dnode_t *dn, void *tag)
1214 mutex_enter(&dn->dn_mtx);
1215 if (refcount_is_zero(&dn->dn_holds)) {
1216 mutex_exit(&dn->dn_mtx);
1219 VERIFY(1 < refcount_add(&dn->dn_holds, tag));
1220 mutex_exit(&dn->dn_mtx);
1225 dnode_rele(dnode_t *dn, void *tag)
1227 mutex_enter(&dn->dn_mtx);
1228 dnode_rele_and_unlock(dn, tag);
1232 dnode_rele_and_unlock(dnode_t *dn, void *tag)
1235 /* Get while the hold prevents the dnode from moving. */
1236 dmu_buf_impl_t *db = dn->dn_dbuf;
1237 dnode_handle_t *dnh = dn->dn_handle;
1239 refs = refcount_remove(&dn->dn_holds, tag);
1240 mutex_exit(&dn->dn_mtx);
1243 * It's unsafe to release the last hold on a dnode by dnode_rele() or
1244 * indirectly by dbuf_rele() while relying on the dnode handle to
1245 * prevent the dnode from moving, since releasing the last hold could
1246 * result in the dnode's parent dbuf evicting its dnode handles. For
1247 * that reason anyone calling dnode_rele() or dbuf_rele() without some
1248 * other direct or indirect hold on the dnode must first drop the dnode
1251 ASSERT(refs > 0 || dnh->dnh_zrlock.zr_owner != curthread);
1253 /* NOTE: the DNODE_DNODE does not have a dn_dbuf */
1254 if (refs == 0 && db != NULL) {
1256 * Another thread could add a hold to the dnode handle in
1257 * dnode_hold_impl() while holding the parent dbuf. Since the
1258 * hold on the parent dbuf prevents the handle from being
1259 * destroyed, the hold on the handle is OK. We can't yet assert
1260 * that the handle has zero references, but that will be
1261 * asserted anyway when the handle gets destroyed.
1268 dnode_setdirty(dnode_t *dn, dmu_tx_t *tx)
1270 objset_t *os = dn->dn_objset;
1271 uint64_t txg = tx->tx_txg;
1273 if (DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
1274 dsl_dataset_dirty(os->os_dsl_dataset, tx);
1281 mutex_enter(&dn->dn_mtx);
1282 ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg);
1283 ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg);
1284 mutex_exit(&dn->dn_mtx);
1288 * Determine old uid/gid when necessary
1290 dmu_objset_userquota_get_ids(dn, B_TRUE, tx);
1292 mutex_enter(&os->os_lock);
1295 * If we are already marked dirty, we're done.
1297 if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) {
1298 mutex_exit(&os->os_lock);
1302 ASSERT(!refcount_is_zero(&dn->dn_holds) ||
1303 !avl_is_empty(&dn->dn_dbufs));
1304 ASSERT(dn->dn_datablksz != 0);
1305 ASSERT0(dn->dn_next_bonuslen[txg&TXG_MASK]);
1306 ASSERT0(dn->dn_next_blksz[txg&TXG_MASK]);
1307 ASSERT0(dn->dn_next_bonustype[txg&TXG_MASK]);
1309 dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
1310 dn->dn_object, txg);
1312 if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) {
1313 list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn);
1315 list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn);
1318 mutex_exit(&os->os_lock);
1321 * The dnode maintains a hold on its containing dbuf as
1322 * long as there are holds on it. Each instantiated child
1323 * dbuf maintains a hold on the dnode. When the last child
1324 * drops its hold, the dnode will drop its hold on the
1325 * containing dbuf. We add a "dirty hold" here so that the
1326 * dnode will hang around after we finish processing its
1329 VERIFY(dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg));
1331 (void) dbuf_dirty(dn->dn_dbuf, tx);
1333 dsl_dataset_dirty(os->os_dsl_dataset, tx);
1337 dnode_free(dnode_t *dn, dmu_tx_t *tx)
1339 int txgoff = tx->tx_txg & TXG_MASK;
1341 dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg);
1343 /* we should be the only holder... hopefully */
1344 /* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */
1346 mutex_enter(&dn->dn_mtx);
1347 if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) {
1348 mutex_exit(&dn->dn_mtx);
1351 dn->dn_free_txg = tx->tx_txg;
1352 mutex_exit(&dn->dn_mtx);
1355 * If the dnode is already dirty, it needs to be moved from
1356 * the dirty list to the free list.
1358 mutex_enter(&dn->dn_objset->os_lock);
1359 if (list_link_active(&dn->dn_dirty_link[txgoff])) {
1360 list_remove(&dn->dn_objset->os_dirty_dnodes[txgoff], dn);
1361 list_insert_tail(&dn->dn_objset->os_free_dnodes[txgoff], dn);
1362 mutex_exit(&dn->dn_objset->os_lock);
1364 mutex_exit(&dn->dn_objset->os_lock);
1365 dnode_setdirty(dn, tx);
1370 * Try to change the block size for the indicated dnode. This can only
1371 * succeed if there are no blocks allocated or dirty beyond first block
1374 dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx)
1379 ASSERT3U(size, <=, spa_maxblocksize(dmu_objset_spa(dn->dn_objset)));
1381 size = SPA_MINBLOCKSIZE;
1383 size = P2ROUNDUP(size, SPA_MINBLOCKSIZE);
1385 if (ibs == dn->dn_indblkshift)
1388 if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0)
1391 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1393 /* Check for any allocated blocks beyond the first */
1394 if (dn->dn_maxblkid != 0)
1397 mutex_enter(&dn->dn_dbufs_mtx);
1398 for (db = avl_first(&dn->dn_dbufs); db != NULL;
1399 db = AVL_NEXT(&dn->dn_dbufs, db)) {
1400 if (db->db_blkid != 0 && db->db_blkid != DMU_BONUS_BLKID &&
1401 db->db_blkid != DMU_SPILL_BLKID) {
1402 mutex_exit(&dn->dn_dbufs_mtx);
1406 mutex_exit(&dn->dn_dbufs_mtx);
1408 if (ibs && dn->dn_nlevels != 1)
1411 /* resize the old block */
1412 err = dbuf_hold_impl(dn, 0, 0, TRUE, FALSE, FTAG, &db);
1414 dbuf_new_size(db, size, tx);
1415 else if (err != ENOENT)
1418 dnode_setdblksz(dn, size);
1419 dnode_setdirty(dn, tx);
1420 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size;
1422 dn->dn_indblkshift = ibs;
1423 dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs;
1425 /* rele after we have fixed the blocksize in the dnode */
1427 dbuf_rele(db, FTAG);
1429 rw_exit(&dn->dn_struct_rwlock);
1433 rw_exit(&dn->dn_struct_rwlock);
1434 return (SET_ERROR(ENOTSUP));
1437 /* read-holding callers must not rely on the lock being continuously held */
1439 dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t have_read)
1441 uint64_t txgoff = tx->tx_txg & TXG_MASK;
1442 int epbs, new_nlevels;
1445 ASSERT(blkid != DMU_BONUS_BLKID);
1448 RW_READ_HELD(&dn->dn_struct_rwlock) :
1449 RW_WRITE_HELD(&dn->dn_struct_rwlock));
1452 * if we have a read-lock, check to see if we need to do any work
1453 * before upgrading to a write-lock.
1456 if (blkid <= dn->dn_maxblkid)
1459 if (!rw_tryupgrade(&dn->dn_struct_rwlock)) {
1460 rw_exit(&dn->dn_struct_rwlock);
1461 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1465 if (blkid <= dn->dn_maxblkid)
1468 dn->dn_maxblkid = blkid;
1471 * Compute the number of levels necessary to support the new maxblkid.
1474 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1475 for (sz = dn->dn_nblkptr;
1476 sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs)
1479 if (new_nlevels > dn->dn_nlevels) {
1480 int old_nlevels = dn->dn_nlevels;
1483 dbuf_dirty_record_t *new, *dr, *dr_next;
1485 dn->dn_nlevels = new_nlevels;
1487 ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]);
1488 dn->dn_next_nlevels[txgoff] = new_nlevels;
1490 /* dirty the left indirects */
1491 db = dbuf_hold_level(dn, old_nlevels, 0, FTAG);
1493 new = dbuf_dirty(db, tx);
1494 dbuf_rele(db, FTAG);
1496 /* transfer the dirty records to the new indirect */
1497 mutex_enter(&dn->dn_mtx);
1498 mutex_enter(&new->dt.di.dr_mtx);
1499 list = &dn->dn_dirty_records[txgoff];
1500 for (dr = list_head(list); dr; dr = dr_next) {
1501 dr_next = list_next(&dn->dn_dirty_records[txgoff], dr);
1502 if (dr->dr_dbuf->db_level != new_nlevels-1 &&
1503 dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
1504 dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
1505 ASSERT(dr->dr_dbuf->db_level == old_nlevels-1);
1506 list_remove(&dn->dn_dirty_records[txgoff], dr);
1507 list_insert_tail(&new->dt.di.dr_children, dr);
1508 dr->dr_parent = new;
1511 mutex_exit(&new->dt.di.dr_mtx);
1512 mutex_exit(&dn->dn_mtx);
1517 rw_downgrade(&dn->dn_struct_rwlock);
1521 dnode_dirty_l1(dnode_t *dn, uint64_t l1blkid, dmu_tx_t *tx)
1523 dmu_buf_impl_t *db = dbuf_hold_level(dn, 1, l1blkid, FTAG);
1525 dmu_buf_will_dirty(&db->db, tx);
1526 dbuf_rele(db, FTAG);
1531 dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx)
1534 uint64_t blkoff, blkid, nblks;
1535 int blksz, blkshift, head, tail;
1539 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1540 blksz = dn->dn_datablksz;
1541 blkshift = dn->dn_datablkshift;
1542 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1544 if (len == DMU_OBJECT_END) {
1545 len = UINT64_MAX - off;
1550 * First, block align the region to free:
1553 head = P2NPHASE(off, blksz);
1554 blkoff = P2PHASE(off, blksz);
1555 if ((off >> blkshift) > dn->dn_maxblkid)
1558 ASSERT(dn->dn_maxblkid == 0);
1559 if (off == 0 && len >= blksz) {
1561 * Freeing the whole block; fast-track this request.
1562 * Note that we won't dirty any indirect blocks,
1563 * which is fine because we will be freeing the entire
1564 * file and thus all indirect blocks will be freed
1565 * by free_children().
1570 } else if (off >= blksz) {
1571 /* Freeing past end-of-data */
1574 /* Freeing part of the block. */
1576 ASSERT3U(head, >, 0);
1580 /* zero out any partial block data at the start of the range */
1582 ASSERT3U(blkoff + head, ==, blksz);
1585 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, 0, off),
1586 TRUE, FALSE, FTAG, &db) == 0) {
1589 /* don't dirty if it isn't on disk and isn't dirty */
1590 if (db->db_last_dirty ||
1591 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1592 rw_exit(&dn->dn_struct_rwlock);
1593 dmu_buf_will_dirty(&db->db, tx);
1594 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1595 data = db->db.db_data;
1596 bzero(data + blkoff, head);
1598 dbuf_rele(db, FTAG);
1604 /* If the range was less than one block, we're done */
1608 /* If the remaining range is past end of file, we're done */
1609 if ((off >> blkshift) > dn->dn_maxblkid)
1612 ASSERT(ISP2(blksz));
1616 tail = P2PHASE(len, blksz);
1618 ASSERT0(P2PHASE(off, blksz));
1619 /* zero out any partial block data at the end of the range */
1623 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, 0, off+len),
1624 TRUE, FALSE, FTAG, &db) == 0) {
1625 /* don't dirty if not on disk and not dirty */
1626 if (db->db_last_dirty ||
1627 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1628 rw_exit(&dn->dn_struct_rwlock);
1629 dmu_buf_will_dirty(&db->db, tx);
1630 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1631 bzero(db->db.db_data, tail);
1633 dbuf_rele(db, FTAG);
1638 /* If the range did not include a full block, we are done */
1642 ASSERT(IS_P2ALIGNED(off, blksz));
1643 ASSERT(trunc || IS_P2ALIGNED(len, blksz));
1644 blkid = off >> blkshift;
1645 nblks = len >> blkshift;
1650 * Dirty all the indirect blocks in this range. Note that only
1651 * the first and last indirect blocks can actually be written
1652 * (if they were partially freed) -- they must be dirtied, even if
1653 * they do not exist on disk yet. The interior blocks will
1654 * be freed by free_children(), so they will not actually be written.
1655 * Even though these interior blocks will not be written, we
1656 * dirty them for two reasons:
1658 * - It ensures that the indirect blocks remain in memory until
1659 * syncing context. (They have already been prefetched by
1660 * dmu_tx_hold_free(), so we don't have to worry about reading
1661 * them serially here.)
1663 * - The dirty space accounting will put pressure on the txg sync
1664 * mechanism to begin syncing, and to delay transactions if there
1665 * is a large amount of freeing. Even though these indirect
1666 * blocks will not be written, we could need to write the same
1667 * amount of space if we copy the freed BPs into deadlists.
1669 if (dn->dn_nlevels > 1) {
1670 uint64_t first, last, i, ibyte;
1673 first = blkid >> epbs;
1674 dnode_dirty_l1(dn, first, tx);
1676 last = dn->dn_maxblkid >> epbs;
1678 last = (blkid + nblks - 1) >> epbs;
1680 dnode_dirty_l1(dn, last, tx);
1682 shift = dn->dn_datablkshift + dn->dn_indblkshift -
1684 for (i = first + 1; i < last; i++) {
1686 * Set i to the blockid of the next non-hole
1687 * level-1 indirect block at or after i. Note
1688 * that dnode_next_offset() operates in terms of
1689 * level-0-equivalent bytes.
1692 err = dnode_next_offset(dn, DNODE_FIND_HAVELOCK,
1699 * Normally we should not see an error, either
1700 * from dnode_next_offset() or dbuf_hold_level()
1701 * (except for ESRCH from dnode_next_offset).
1702 * If there is an i/o error, then when we read
1703 * this block in syncing context, it will use
1704 * ZIO_FLAG_MUSTSUCCEED, and thus hang/panic according
1705 * to the "failmode" property. dnode_next_offset()
1706 * doesn't have a flag to indicate MUSTSUCCEED.
1711 dnode_dirty_l1(dn, i, tx);
1717 * Add this range to the dnode range list.
1718 * We will finish up this free operation in the syncing phase.
1720 mutex_enter(&dn->dn_mtx);
1722 int txgoff = tx->tx_txg & TXG_MASK;
1723 if (dn->dn_free_ranges[txgoff] == NULL) {
1724 dn->dn_free_ranges[txgoff] =
1725 range_tree_create(NULL, NULL, &dn->dn_mtx);
1727 range_tree_clear(dn->dn_free_ranges[txgoff], blkid, nblks);
1728 range_tree_add(dn->dn_free_ranges[txgoff], blkid, nblks);
1730 dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1731 blkid, nblks, tx->tx_txg);
1732 mutex_exit(&dn->dn_mtx);
1734 dbuf_free_range(dn, blkid, blkid + nblks - 1, tx);
1735 dnode_setdirty(dn, tx);
1738 rw_exit(&dn->dn_struct_rwlock);
1742 dnode_spill_freed(dnode_t *dn)
1746 mutex_enter(&dn->dn_mtx);
1747 for (i = 0; i < TXG_SIZE; i++) {
1748 if (dn->dn_rm_spillblk[i] == DN_KILL_SPILLBLK)
1751 mutex_exit(&dn->dn_mtx);
1752 return (i < TXG_SIZE);
1755 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
1757 dnode_block_freed(dnode_t *dn, uint64_t blkid)
1759 void *dp = spa_get_dsl(dn->dn_objset->os_spa);
1762 if (blkid == DMU_BONUS_BLKID)
1766 * If we're in the process of opening the pool, dp will not be
1767 * set yet, but there shouldn't be anything dirty.
1772 if (dn->dn_free_txg)
1775 if (blkid == DMU_SPILL_BLKID)
1776 return (dnode_spill_freed(dn));
1778 mutex_enter(&dn->dn_mtx);
1779 for (i = 0; i < TXG_SIZE; i++) {
1780 if (dn->dn_free_ranges[i] != NULL &&
1781 range_tree_contains(dn->dn_free_ranges[i], blkid, 1))
1784 mutex_exit(&dn->dn_mtx);
1785 return (i < TXG_SIZE);
1788 /* call from syncing context when we actually write/free space for this dnode */
1790 dnode_diduse_space(dnode_t *dn, int64_t delta)
1793 dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n",
1795 (u_longlong_t)dn->dn_phys->dn_used,
1798 mutex_enter(&dn->dn_mtx);
1799 space = DN_USED_BYTES(dn->dn_phys);
1801 ASSERT3U(space + delta, >=, space); /* no overflow */
1803 ASSERT3U(space, >=, -delta); /* no underflow */
1806 if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_DNODE_BYTES) {
1807 ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0);
1808 ASSERT0(P2PHASE(space, 1<<DEV_BSHIFT));
1809 dn->dn_phys->dn_used = space >> DEV_BSHIFT;
1811 dn->dn_phys->dn_used = space;
1812 dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES;
1814 mutex_exit(&dn->dn_mtx);
1818 * Call when we think we're going to write/free space in open context to track
1819 * the amount of memory in use by the currently open txg.
1822 dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx)
1824 objset_t *os = dn->dn_objset;
1825 dsl_dataset_t *ds = os->os_dsl_dataset;
1826 int64_t aspace = spa_get_asize(os->os_spa, space);
1829 dsl_dir_willuse_space(ds->ds_dir, aspace, tx);
1830 dsl_pool_dirty_space(dmu_tx_pool(tx), space, tx);
1833 dmu_tx_willuse_space(tx, aspace);
1837 * Scans a block at the indicated "level" looking for a hole or data,
1838 * depending on 'flags'.
1840 * If level > 0, then we are scanning an indirect block looking at its
1841 * pointers. If level == 0, then we are looking at a block of dnodes.
1843 * If we don't find what we are looking for in the block, we return ESRCH.
1844 * Otherwise, return with *offset pointing to the beginning (if searching
1845 * forwards) or end (if searching backwards) of the range covered by the
1846 * block pointer we matched on (or dnode).
1848 * The basic search algorithm used below by dnode_next_offset() is to
1849 * use this function to search up the block tree (widen the search) until
1850 * we find something (i.e., we don't return ESRCH) and then search back
1851 * down the tree (narrow the search) until we reach our original search
1855 dnode_next_offset_level(dnode_t *dn, int flags, uint64_t *offset,
1856 int lvl, uint64_t blkfill, uint64_t txg)
1858 dmu_buf_impl_t *db = NULL;
1860 uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
1861 uint64_t epb = 1ULL << epbs;
1862 uint64_t minfill, maxfill;
1864 int i, inc, error, span;
1866 dprintf("probing object %llu offset %llx level %d of %u\n",
1867 dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels);
1869 hole = ((flags & DNODE_FIND_HOLE) != 0);
1870 inc = (flags & DNODE_FIND_BACKWARDS) ? -1 : 1;
1871 ASSERT(txg == 0 || !hole);
1873 if (lvl == dn->dn_phys->dn_nlevels) {
1875 epb = dn->dn_phys->dn_nblkptr;
1876 data = dn->dn_phys->dn_blkptr;
1878 uint64_t blkid = dbuf_whichblock(dn, lvl, *offset);
1879 error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FALSE, FTAG, &db);
1881 if (error != ENOENT)
1886 * This can only happen when we are searching up
1887 * the block tree for data. We don't really need to
1888 * adjust the offset, as we will just end up looking
1889 * at the pointer to this block in its parent, and its
1890 * going to be unallocated, so we will skip over it.
1892 return (SET_ERROR(ESRCH));
1894 error = dbuf_read(db, NULL, DB_RF_CANFAIL | DB_RF_HAVESTRUCT);
1896 dbuf_rele(db, FTAG);
1899 data = db->db.db_data;
1903 if (db != NULL && txg != 0 && (db->db_blkptr == NULL ||
1904 db->db_blkptr->blk_birth <= txg ||
1905 BP_IS_HOLE(db->db_blkptr))) {
1907 * This can only happen when we are searching up the tree
1908 * and these conditions mean that we need to keep climbing.
1910 error = SET_ERROR(ESRCH);
1911 } else if (lvl == 0) {
1912 dnode_phys_t *dnp = data;
1914 ASSERT(dn->dn_type == DMU_OT_DNODE);
1916 for (i = (*offset >> span) & (blkfill - 1);
1917 i >= 0 && i < blkfill; i += inc) {
1918 if ((dnp[i].dn_type == DMU_OT_NONE) == hole)
1920 *offset += (1ULL << span) * inc;
1922 if (i < 0 || i == blkfill)
1923 error = SET_ERROR(ESRCH);
1925 blkptr_t *bp = data;
1926 uint64_t start = *offset;
1927 span = (lvl - 1) * epbs + dn->dn_datablkshift;
1929 maxfill = blkfill << ((lvl - 1) * epbs);
1936 *offset = *offset >> span;
1937 for (i = BF64_GET(*offset, 0, epbs);
1938 i >= 0 && i < epb; i += inc) {
1939 if (BP_GET_FILL(&bp[i]) >= minfill &&
1940 BP_GET_FILL(&bp[i]) <= maxfill &&
1941 (hole || bp[i].blk_birth > txg))
1943 if (inc > 0 || *offset > 0)
1946 *offset = *offset << span;
1948 /* traversing backwards; position offset at the end */
1949 ASSERT3U(*offset, <=, start);
1950 *offset = MIN(*offset + (1ULL << span) - 1, start);
1951 } else if (*offset < start) {
1954 if (i < 0 || i >= epb)
1955 error = SET_ERROR(ESRCH);
1959 dbuf_rele(db, FTAG);
1965 * Find the next hole, data, or sparse region at or after *offset.
1966 * The value 'blkfill' tells us how many items we expect to find
1967 * in an L0 data block; this value is 1 for normal objects,
1968 * DNODES_PER_BLOCK for the meta dnode, and some fraction of
1969 * DNODES_PER_BLOCK when searching for sparse regions thereof.
1973 * dnode_next_offset(dn, flags, offset, 1, 1, 0);
1974 * Finds the next/previous hole/data in a file.
1975 * Used in dmu_offset_next().
1977 * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
1978 * Finds the next free/allocated dnode an objset's meta-dnode.
1979 * Only finds objects that have new contents since txg (ie.
1980 * bonus buffer changes and content removal are ignored).
1981 * Used in dmu_object_next().
1983 * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
1984 * Finds the next L2 meta-dnode bp that's at most 1/4 full.
1985 * Used in dmu_object_alloc().
1988 dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset,
1989 int minlvl, uint64_t blkfill, uint64_t txg)
1991 uint64_t initial_offset = *offset;
1995 if (!(flags & DNODE_FIND_HAVELOCK))
1996 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1998 if (dn->dn_phys->dn_nlevels == 0) {
1999 error = SET_ERROR(ESRCH);
2003 if (dn->dn_datablkshift == 0) {
2004 if (*offset < dn->dn_datablksz) {
2005 if (flags & DNODE_FIND_HOLE)
2006 *offset = dn->dn_datablksz;
2008 error = SET_ERROR(ESRCH);
2013 maxlvl = dn->dn_phys->dn_nlevels;
2015 for (lvl = minlvl; lvl <= maxlvl; lvl++) {
2016 error = dnode_next_offset_level(dn,
2017 flags, offset, lvl, blkfill, txg);
2022 while (error == 0 && --lvl >= minlvl) {
2023 error = dnode_next_offset_level(dn,
2024 flags, offset, lvl, blkfill, txg);
2028 * There's always a "virtual hole" at the end of the object, even
2029 * if all BP's which physically exist are non-holes.
2031 if ((flags & DNODE_FIND_HOLE) && error == ESRCH && txg == 0 &&
2032 minlvl == 1 && blkfill == 1 && !(flags & DNODE_FIND_BACKWARDS)) {
2036 if (error == 0 && (flags & DNODE_FIND_BACKWARDS ?
2037 initial_offset < *offset : initial_offset > *offset))
2038 error = SET_ERROR(ESRCH);
2040 if (!(flags & DNODE_FIND_HAVELOCK))
2041 rw_exit(&dn->dn_struct_rwlock);