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, 2014 by Delphix. All rights reserved.
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>
38 #include <sys/range_tree.h>
39 #include <sys/trace_dnode.h>
41 static kmem_cache_t
*dnode_cache
;
43 * Define DNODE_STATS to turn on statistic gathering. By default, it is only
44 * turned on when DEBUG is also defined.
51 #define DNODE_STAT_ADD(stat) ((stat)++)
53 #define DNODE_STAT_ADD(stat) /* nothing */
54 #endif /* DNODE_STATS */
56 ASSERTV(static dnode_phys_t dnode_phys_zero
);
58 int zfs_default_bs
= SPA_MINBLOCKSHIFT
;
59 int zfs_default_ibs
= DN_MAX_INDBLKSHIFT
;
62 static kmem_cbrc_t
dnode_move(void *, void *, size_t, void *);
66 dbuf_compare(const void *x1
, const void *x2
)
68 const dmu_buf_impl_t
*d1
= x1
;
69 const dmu_buf_impl_t
*d2
= x2
;
71 if (d1
->db_level
< d2
->db_level
) {
74 if (d1
->db_level
> d2
->db_level
) {
78 if (d1
->db_blkid
< d2
->db_blkid
) {
81 if (d1
->db_blkid
> d2
->db_blkid
) {
85 if (d1
->db_state
< d2
->db_state
) {
88 if (d1
->db_state
> d2
->db_state
) {
92 ASSERT3S(d1
->db_state
, !=, DB_SEARCH
);
93 ASSERT3S(d2
->db_state
, !=, DB_SEARCH
);
95 if ((uintptr_t)d1
< (uintptr_t)d2
) {
98 if ((uintptr_t)d1
> (uintptr_t)d2
) {
106 dnode_cons(void *arg
, void *unused
, int kmflag
)
111 rw_init(&dn
->dn_struct_rwlock
, NULL
, RW_DEFAULT
, NULL
);
112 mutex_init(&dn
->dn_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
113 mutex_init(&dn
->dn_dbufs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
114 cv_init(&dn
->dn_notxholds
, NULL
, CV_DEFAULT
, NULL
);
117 * Every dbuf has a reference, and dropping a tracked reference is
118 * O(number of references), so don't track dn_holds.
120 refcount_create_untracked(&dn
->dn_holds
);
121 refcount_create(&dn
->dn_tx_holds
);
122 list_link_init(&dn
->dn_link
);
124 bzero(&dn
->dn_next_nblkptr
[0], sizeof (dn
->dn_next_nblkptr
));
125 bzero(&dn
->dn_next_nlevels
[0], sizeof (dn
->dn_next_nlevels
));
126 bzero(&dn
->dn_next_indblkshift
[0], sizeof (dn
->dn_next_indblkshift
));
127 bzero(&dn
->dn_next_bonustype
[0], sizeof (dn
->dn_next_bonustype
));
128 bzero(&dn
->dn_rm_spillblk
[0], sizeof (dn
->dn_rm_spillblk
));
129 bzero(&dn
->dn_next_bonuslen
[0], sizeof (dn
->dn_next_bonuslen
));
130 bzero(&dn
->dn_next_blksz
[0], sizeof (dn
->dn_next_blksz
));
132 for (i
= 0; i
< TXG_SIZE
; i
++) {
133 list_link_init(&dn
->dn_dirty_link
[i
]);
134 dn
->dn_free_ranges
[i
] = NULL
;
135 list_create(&dn
->dn_dirty_records
[i
],
136 sizeof (dbuf_dirty_record_t
),
137 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
140 dn
->dn_allocated_txg
= 0;
142 dn
->dn_assigned_txg
= 0;
144 dn
->dn_dirtyctx_firstset
= NULL
;
146 dn
->dn_have_spill
= B_FALSE
;
156 dn
->dn_dbufs_count
= 0;
157 dn
->dn_unlisted_l0_blkid
= 0;
158 avl_create(&dn
->dn_dbufs
, dbuf_compare
, sizeof (dmu_buf_impl_t
),
159 offsetof(dmu_buf_impl_t
, db_link
));
167 dnode_dest(void *arg
, void *unused
)
172 rw_destroy(&dn
->dn_struct_rwlock
);
173 mutex_destroy(&dn
->dn_mtx
);
174 mutex_destroy(&dn
->dn_dbufs_mtx
);
175 cv_destroy(&dn
->dn_notxholds
);
176 refcount_destroy(&dn
->dn_holds
);
177 refcount_destroy(&dn
->dn_tx_holds
);
178 ASSERT(!list_link_active(&dn
->dn_link
));
180 for (i
= 0; i
< TXG_SIZE
; i
++) {
181 ASSERT(!list_link_active(&dn
->dn_dirty_link
[i
]));
182 ASSERT3P(dn
->dn_free_ranges
[i
], ==, NULL
);
183 list_destroy(&dn
->dn_dirty_records
[i
]);
184 ASSERT0(dn
->dn_next_nblkptr
[i
]);
185 ASSERT0(dn
->dn_next_nlevels
[i
]);
186 ASSERT0(dn
->dn_next_indblkshift
[i
]);
187 ASSERT0(dn
->dn_next_bonustype
[i
]);
188 ASSERT0(dn
->dn_rm_spillblk
[i
]);
189 ASSERT0(dn
->dn_next_bonuslen
[i
]);
190 ASSERT0(dn
->dn_next_blksz
[i
]);
193 ASSERT0(dn
->dn_allocated_txg
);
194 ASSERT0(dn
->dn_free_txg
);
195 ASSERT0(dn
->dn_assigned_txg
);
196 ASSERT0(dn
->dn_dirtyctx
);
197 ASSERT3P(dn
->dn_dirtyctx_firstset
, ==, NULL
);
198 ASSERT3P(dn
->dn_bonus
, ==, NULL
);
199 ASSERT(!dn
->dn_have_spill
);
200 ASSERT3P(dn
->dn_zio
, ==, NULL
);
201 ASSERT0(dn
->dn_oldused
);
202 ASSERT0(dn
->dn_oldflags
);
203 ASSERT0(dn
->dn_olduid
);
204 ASSERT0(dn
->dn_oldgid
);
205 ASSERT0(dn
->dn_newuid
);
206 ASSERT0(dn
->dn_newgid
);
207 ASSERT0(dn
->dn_id_flags
);
209 ASSERT0(dn
->dn_dbufs_count
);
210 ASSERT0(dn
->dn_unlisted_l0_blkid
);
211 avl_destroy(&dn
->dn_dbufs
);
217 ASSERT(dnode_cache
== NULL
);
218 dnode_cache
= kmem_cache_create("dnode_t", sizeof (dnode_t
),
219 0, dnode_cons
, dnode_dest
, NULL
, NULL
, NULL
, 0);
220 kmem_cache_set_move(dnode_cache
, dnode_move
);
226 kmem_cache_destroy(dnode_cache
);
233 dnode_verify(dnode_t
*dn
)
235 int drop_struct_lock
= FALSE
;
238 ASSERT(dn
->dn_objset
);
239 ASSERT(dn
->dn_handle
->dnh_dnode
== dn
);
241 ASSERT(DMU_OT_IS_VALID(dn
->dn_phys
->dn_type
));
243 if (!(zfs_flags
& ZFS_DEBUG_DNODE_VERIFY
))
246 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
247 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
248 drop_struct_lock
= TRUE
;
250 if (dn
->dn_phys
->dn_type
!= DMU_OT_NONE
|| dn
->dn_allocated_txg
!= 0) {
252 ASSERT3U(dn
->dn_indblkshift
, <=, SPA_MAXBLOCKSHIFT
);
253 if (dn
->dn_datablkshift
) {
254 ASSERT3U(dn
->dn_datablkshift
, >=, SPA_MINBLOCKSHIFT
);
255 ASSERT3U(dn
->dn_datablkshift
, <=, SPA_MAXBLOCKSHIFT
);
256 ASSERT3U(1<<dn
->dn_datablkshift
, ==, dn
->dn_datablksz
);
258 ASSERT3U(dn
->dn_nlevels
, <=, 30);
259 ASSERT(DMU_OT_IS_VALID(dn
->dn_type
));
260 ASSERT3U(dn
->dn_nblkptr
, >=, 1);
261 ASSERT3U(dn
->dn_nblkptr
, <=, DN_MAX_NBLKPTR
);
262 ASSERT3U(dn
->dn_bonuslen
, <=, DN_MAX_BONUSLEN
);
263 ASSERT3U(dn
->dn_datablksz
, ==,
264 dn
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
);
265 ASSERT3U(ISP2(dn
->dn_datablksz
), ==, dn
->dn_datablkshift
!= 0);
266 ASSERT3U((dn
->dn_nblkptr
- 1) * sizeof (blkptr_t
) +
267 dn
->dn_bonuslen
, <=, DN_MAX_BONUSLEN
);
268 for (i
= 0; i
< TXG_SIZE
; i
++) {
269 ASSERT3U(dn
->dn_next_nlevels
[i
], <=, dn
->dn_nlevels
);
272 if (dn
->dn_phys
->dn_type
!= DMU_OT_NONE
)
273 ASSERT3U(dn
->dn_phys
->dn_nlevels
, <=, dn
->dn_nlevels
);
274 ASSERT(DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) || dn
->dn_dbuf
!= NULL
);
275 if (dn
->dn_dbuf
!= NULL
) {
276 ASSERT3P(dn
->dn_phys
, ==,
277 (dnode_phys_t
*)dn
->dn_dbuf
->db
.db_data
+
278 (dn
->dn_object
% (dn
->dn_dbuf
->db
.db_size
>> DNODE_SHIFT
)));
280 if (drop_struct_lock
)
281 rw_exit(&dn
->dn_struct_rwlock
);
286 dnode_byteswap(dnode_phys_t
*dnp
)
288 uint64_t *buf64
= (void*)&dnp
->dn_blkptr
;
291 if (dnp
->dn_type
== DMU_OT_NONE
) {
292 bzero(dnp
, sizeof (dnode_phys_t
));
296 dnp
->dn_datablkszsec
= BSWAP_16(dnp
->dn_datablkszsec
);
297 dnp
->dn_bonuslen
= BSWAP_16(dnp
->dn_bonuslen
);
298 dnp
->dn_maxblkid
= BSWAP_64(dnp
->dn_maxblkid
);
299 dnp
->dn_used
= BSWAP_64(dnp
->dn_used
);
302 * dn_nblkptr is only one byte, so it's OK to read it in either
303 * byte order. We can't read dn_bouslen.
305 ASSERT(dnp
->dn_indblkshift
<= SPA_MAXBLOCKSHIFT
);
306 ASSERT(dnp
->dn_nblkptr
<= DN_MAX_NBLKPTR
);
307 for (i
= 0; i
< dnp
->dn_nblkptr
* sizeof (blkptr_t
)/8; i
++)
308 buf64
[i
] = BSWAP_64(buf64
[i
]);
311 * OK to check dn_bonuslen for zero, because it won't matter if
312 * we have the wrong byte order. This is necessary because the
313 * dnode dnode is smaller than a regular dnode.
315 if (dnp
->dn_bonuslen
!= 0) {
317 * Note that the bonus length calculated here may be
318 * longer than the actual bonus buffer. This is because
319 * we always put the bonus buffer after the last block
320 * pointer (instead of packing it against the end of the
323 int off
= (dnp
->dn_nblkptr
-1) * sizeof (blkptr_t
);
324 size_t len
= DN_MAX_BONUSLEN
- off
;
325 dmu_object_byteswap_t byteswap
;
326 ASSERT(DMU_OT_IS_VALID(dnp
->dn_bonustype
));
327 byteswap
= DMU_OT_BYTESWAP(dnp
->dn_bonustype
);
328 dmu_ot_byteswap
[byteswap
].ob_func(dnp
->dn_bonus
+ off
, len
);
331 /* Swap SPILL block if we have one */
332 if (dnp
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
)
333 byteswap_uint64_array(&dnp
->dn_spill
, sizeof (blkptr_t
));
338 dnode_buf_byteswap(void *vbuf
, size_t size
)
340 dnode_phys_t
*buf
= vbuf
;
343 ASSERT3U(sizeof (dnode_phys_t
), ==, (1<<DNODE_SHIFT
));
344 ASSERT((size
& (sizeof (dnode_phys_t
)-1)) == 0);
346 size
>>= DNODE_SHIFT
;
347 for (i
= 0; i
< size
; i
++) {
354 dnode_setbonuslen(dnode_t
*dn
, int newsize
, dmu_tx_t
*tx
)
356 ASSERT3U(refcount_count(&dn
->dn_holds
), >=, 1);
358 dnode_setdirty(dn
, tx
);
359 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
360 ASSERT3U(newsize
, <=, DN_MAX_BONUSLEN
-
361 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
));
362 dn
->dn_bonuslen
= newsize
;
364 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = DN_ZERO_BONUSLEN
;
366 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonuslen
;
367 rw_exit(&dn
->dn_struct_rwlock
);
371 dnode_setbonus_type(dnode_t
*dn
, dmu_object_type_t newtype
, dmu_tx_t
*tx
)
373 ASSERT3U(refcount_count(&dn
->dn_holds
), >=, 1);
374 dnode_setdirty(dn
, tx
);
375 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
376 dn
->dn_bonustype
= newtype
;
377 dn
->dn_next_bonustype
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonustype
;
378 rw_exit(&dn
->dn_struct_rwlock
);
382 dnode_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
384 ASSERT3U(refcount_count(&dn
->dn_holds
), >=, 1);
385 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
386 dnode_setdirty(dn
, tx
);
387 dn
->dn_rm_spillblk
[tx
->tx_txg
&TXG_MASK
] = DN_KILL_SPILLBLK
;
388 dn
->dn_have_spill
= B_FALSE
;
392 dnode_setdblksz(dnode_t
*dn
, int size
)
394 ASSERT0(P2PHASE(size
, SPA_MINBLOCKSIZE
));
395 ASSERT3U(size
, <=, SPA_MAXBLOCKSIZE
);
396 ASSERT3U(size
, >=, SPA_MINBLOCKSIZE
);
397 ASSERT3U(size
>> SPA_MINBLOCKSHIFT
, <,
398 1<<(sizeof (dn
->dn_phys
->dn_datablkszsec
) * 8));
399 dn
->dn_datablksz
= size
;
400 dn
->dn_datablkszsec
= size
>> SPA_MINBLOCKSHIFT
;
401 dn
->dn_datablkshift
= ISP2(size
) ? highbit64(size
- 1) : 0;
405 dnode_create(objset_t
*os
, dnode_phys_t
*dnp
, dmu_buf_impl_t
*db
,
406 uint64_t object
, dnode_handle_t
*dnh
)
408 dnode_t
*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 list_insert_head(&os
->os_dnodes
, dn
);
449 * Everything else must be valid before assigning dn_objset makes the
450 * dnode eligible for dnode_move().
453 mutex_exit(&os
->os_lock
);
455 arc_space_consume(sizeof (dnode_t
), ARC_SPACE_OTHER
);
460 * Caller must be holding the dnode handle, which is released upon return.
463 dnode_destroy(dnode_t
*dn
)
465 objset_t
*os
= dn
->dn_objset
;
467 ASSERT((dn
->dn_id_flags
& DN_ID_NEW_EXIST
) == 0);
469 mutex_enter(&os
->os_lock
);
470 POINTER_INVALIDATE(&dn
->dn_objset
);
471 list_remove(&os
->os_dnodes
, dn
);
472 mutex_exit(&os
->os_lock
);
474 /* the dnode can no longer move, so we can release the handle */
475 zrl_remove(&dn
->dn_handle
->dnh_zrlock
);
477 dn
->dn_allocated_txg
= 0;
479 dn
->dn_assigned_txg
= 0;
482 if (dn
->dn_dirtyctx_firstset
!= NULL
) {
483 kmem_free(dn
->dn_dirtyctx_firstset
, 1);
484 dn
->dn_dirtyctx_firstset
= NULL
;
486 if (dn
->dn_bonus
!= NULL
) {
487 mutex_enter(&dn
->dn_bonus
->db_mtx
);
488 dbuf_evict(dn
->dn_bonus
);
493 dn
->dn_have_spill
= B_FALSE
;
501 dn
->dn_unlisted_l0_blkid
= 0;
503 dmu_zfetch_rele(&dn
->dn_zfetch
);
504 kmem_cache_free(dnode_cache
, dn
);
505 arc_space_return(sizeof (dnode_t
), ARC_SPACE_OTHER
);
509 dnode_allocate(dnode_t
*dn
, dmu_object_type_t ot
, int blocksize
, int ibs
,
510 dmu_object_type_t bonustype
, int bonuslen
, dmu_tx_t
*tx
)
515 blocksize
= 1 << zfs_default_bs
;
516 else if (blocksize
> SPA_MAXBLOCKSIZE
)
517 blocksize
= SPA_MAXBLOCKSIZE
;
519 blocksize
= P2ROUNDUP(blocksize
, SPA_MINBLOCKSIZE
);
522 ibs
= zfs_default_ibs
;
524 ibs
= MIN(MAX(ibs
, DN_MIN_INDBLKSHIFT
), DN_MAX_INDBLKSHIFT
);
526 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn
->dn_objset
,
527 dn
->dn_object
, tx
->tx_txg
, blocksize
, ibs
);
529 ASSERT(dn
->dn_type
== DMU_OT_NONE
);
530 ASSERT(bcmp(dn
->dn_phys
, &dnode_phys_zero
, sizeof (dnode_phys_t
)) == 0);
531 ASSERT(dn
->dn_phys
->dn_type
== DMU_OT_NONE
);
532 ASSERT(ot
!= DMU_OT_NONE
);
533 ASSERT(DMU_OT_IS_VALID(ot
));
534 ASSERT((bonustype
== DMU_OT_NONE
&& bonuslen
== 0) ||
535 (bonustype
== DMU_OT_SA
&& bonuslen
== 0) ||
536 (bonustype
!= DMU_OT_NONE
&& bonuslen
!= 0));
537 ASSERT(DMU_OT_IS_VALID(bonustype
));
538 ASSERT3U(bonuslen
, <=, DN_MAX_BONUSLEN
);
539 ASSERT(dn
->dn_type
== DMU_OT_NONE
);
540 ASSERT0(dn
->dn_maxblkid
);
541 ASSERT0(dn
->dn_allocated_txg
);
542 ASSERT0(dn
->dn_assigned_txg
);
543 ASSERT(refcount_is_zero(&dn
->dn_tx_holds
));
544 ASSERT3U(refcount_count(&dn
->dn_holds
), <=, 1);
545 ASSERT(avl_is_empty(&dn
->dn_dbufs
));
547 for (i
= 0; i
< TXG_SIZE
; i
++) {
548 ASSERT0(dn
->dn_next_nblkptr
[i
]);
549 ASSERT0(dn
->dn_next_nlevels
[i
]);
550 ASSERT0(dn
->dn_next_indblkshift
[i
]);
551 ASSERT0(dn
->dn_next_bonuslen
[i
]);
552 ASSERT0(dn
->dn_next_bonustype
[i
]);
553 ASSERT0(dn
->dn_rm_spillblk
[i
]);
554 ASSERT0(dn
->dn_next_blksz
[i
]);
555 ASSERT(!list_link_active(&dn
->dn_dirty_link
[i
]));
556 ASSERT3P(list_head(&dn
->dn_dirty_records
[i
]), ==, NULL
);
557 ASSERT3P(dn
->dn_free_ranges
[i
], ==, NULL
);
561 dnode_setdblksz(dn
, blocksize
);
562 dn
->dn_indblkshift
= ibs
;
564 if (bonustype
== DMU_OT_SA
) /* Maximize bonus space for SA */
568 ((DN_MAX_BONUSLEN
- bonuslen
) >> SPA_BLKPTRSHIFT
);
569 dn
->dn_bonustype
= bonustype
;
570 dn
->dn_bonuslen
= bonuslen
;
571 dn
->dn_checksum
= ZIO_CHECKSUM_INHERIT
;
572 dn
->dn_compress
= ZIO_COMPRESS_INHERIT
;
576 if (dn
->dn_dirtyctx_firstset
) {
577 kmem_free(dn
->dn_dirtyctx_firstset
, 1);
578 dn
->dn_dirtyctx_firstset
= NULL
;
581 dn
->dn_allocated_txg
= tx
->tx_txg
;
584 dnode_setdirty(dn
, tx
);
585 dn
->dn_next_indblkshift
[tx
->tx_txg
& TXG_MASK
] = ibs
;
586 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonuslen
;
587 dn
->dn_next_bonustype
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonustype
;
588 dn
->dn_next_blksz
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_datablksz
;
592 dnode_reallocate(dnode_t
*dn
, dmu_object_type_t ot
, int blocksize
,
593 dmu_object_type_t bonustype
, int bonuslen
, dmu_tx_t
*tx
)
597 ASSERT3U(blocksize
, >=, SPA_MINBLOCKSIZE
);
598 ASSERT3U(blocksize
, <=, SPA_MAXBLOCKSIZE
);
599 ASSERT0(blocksize
% SPA_MINBLOCKSIZE
);
600 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
|| dmu_tx_private_ok(tx
));
601 ASSERT(tx
->tx_txg
!= 0);
602 ASSERT((bonustype
== DMU_OT_NONE
&& bonuslen
== 0) ||
603 (bonustype
!= DMU_OT_NONE
&& bonuslen
!= 0) ||
604 (bonustype
== DMU_OT_SA
&& bonuslen
== 0));
605 ASSERT(DMU_OT_IS_VALID(bonustype
));
606 ASSERT3U(bonuslen
, <=, DN_MAX_BONUSLEN
);
608 /* clean up any unreferenced dbufs */
609 dnode_evict_dbufs(dn
);
613 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
614 dnode_setdirty(dn
, tx
);
615 if (dn
->dn_datablksz
!= blocksize
) {
616 /* change blocksize */
617 ASSERT(dn
->dn_maxblkid
== 0 &&
618 (BP_IS_HOLE(&dn
->dn_phys
->dn_blkptr
[0]) ||
619 dnode_block_freed(dn
, 0)));
620 dnode_setdblksz(dn
, blocksize
);
621 dn
->dn_next_blksz
[tx
->tx_txg
&TXG_MASK
] = blocksize
;
623 if (dn
->dn_bonuslen
!= bonuslen
)
624 dn
->dn_next_bonuslen
[tx
->tx_txg
&TXG_MASK
] = bonuslen
;
626 if (bonustype
== DMU_OT_SA
) /* Maximize bonus space for SA */
629 nblkptr
= 1 + ((DN_MAX_BONUSLEN
- bonuslen
) >> SPA_BLKPTRSHIFT
);
630 if (dn
->dn_bonustype
!= bonustype
)
631 dn
->dn_next_bonustype
[tx
->tx_txg
&TXG_MASK
] = bonustype
;
632 if (dn
->dn_nblkptr
!= nblkptr
)
633 dn
->dn_next_nblkptr
[tx
->tx_txg
&TXG_MASK
] = nblkptr
;
634 if (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
) {
635 dbuf_rm_spill(dn
, tx
);
636 dnode_rm_spill(dn
, tx
);
638 rw_exit(&dn
->dn_struct_rwlock
);
643 /* change bonus size and type */
644 mutex_enter(&dn
->dn_mtx
);
645 dn
->dn_bonustype
= bonustype
;
646 dn
->dn_bonuslen
= bonuslen
;
647 dn
->dn_nblkptr
= nblkptr
;
648 dn
->dn_checksum
= ZIO_CHECKSUM_INHERIT
;
649 dn
->dn_compress
= ZIO_COMPRESS_INHERIT
;
650 ASSERT3U(dn
->dn_nblkptr
, <=, DN_MAX_NBLKPTR
);
652 /* fix up the bonus db_size */
654 dn
->dn_bonus
->db
.db_size
=
655 DN_MAX_BONUSLEN
- (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
656 ASSERT(dn
->dn_bonuslen
<= dn
->dn_bonus
->db
.db_size
);
659 dn
->dn_allocated_txg
= tx
->tx_txg
;
660 mutex_exit(&dn
->dn_mtx
);
666 uint64_t dms_dnode_invalid
;
667 uint64_t dms_dnode_recheck1
;
668 uint64_t dms_dnode_recheck2
;
669 uint64_t dms_dnode_special
;
670 uint64_t dms_dnode_handle
;
671 uint64_t dms_dnode_rwlock
;
672 uint64_t dms_dnode_active
;
674 #endif /* DNODE_STATS */
677 dnode_move_impl(dnode_t
*odn
, dnode_t
*ndn
)
681 ASSERT(!RW_LOCK_HELD(&odn
->dn_struct_rwlock
));
682 ASSERT(MUTEX_NOT_HELD(&odn
->dn_mtx
));
683 ASSERT(MUTEX_NOT_HELD(&odn
->dn_dbufs_mtx
));
684 ASSERT(!RW_LOCK_HELD(&odn
->dn_zfetch
.zf_rwlock
));
687 ndn
->dn_objset
= odn
->dn_objset
;
688 ndn
->dn_object
= odn
->dn_object
;
689 ndn
->dn_dbuf
= odn
->dn_dbuf
;
690 ndn
->dn_handle
= odn
->dn_handle
;
691 ndn
->dn_phys
= odn
->dn_phys
;
692 ndn
->dn_type
= odn
->dn_type
;
693 ndn
->dn_bonuslen
= odn
->dn_bonuslen
;
694 ndn
->dn_bonustype
= odn
->dn_bonustype
;
695 ndn
->dn_nblkptr
= odn
->dn_nblkptr
;
696 ndn
->dn_checksum
= odn
->dn_checksum
;
697 ndn
->dn_compress
= odn
->dn_compress
;
698 ndn
->dn_nlevels
= odn
->dn_nlevels
;
699 ndn
->dn_indblkshift
= odn
->dn_indblkshift
;
700 ndn
->dn_datablkshift
= odn
->dn_datablkshift
;
701 ndn
->dn_datablkszsec
= odn
->dn_datablkszsec
;
702 ndn
->dn_datablksz
= odn
->dn_datablksz
;
703 ndn
->dn_maxblkid
= odn
->dn_maxblkid
;
704 bcopy(&odn
->dn_next_nblkptr
[0], &ndn
->dn_next_nblkptr
[0],
705 sizeof (odn
->dn_next_nblkptr
));
706 bcopy(&odn
->dn_next_nlevels
[0], &ndn
->dn_next_nlevels
[0],
707 sizeof (odn
->dn_next_nlevels
));
708 bcopy(&odn
->dn_next_indblkshift
[0], &ndn
->dn_next_indblkshift
[0],
709 sizeof (odn
->dn_next_indblkshift
));
710 bcopy(&odn
->dn_next_bonustype
[0], &ndn
->dn_next_bonustype
[0],
711 sizeof (odn
->dn_next_bonustype
));
712 bcopy(&odn
->dn_rm_spillblk
[0], &ndn
->dn_rm_spillblk
[0],
713 sizeof (odn
->dn_rm_spillblk
));
714 bcopy(&odn
->dn_next_bonuslen
[0], &ndn
->dn_next_bonuslen
[0],
715 sizeof (odn
->dn_next_bonuslen
));
716 bcopy(&odn
->dn_next_blksz
[0], &ndn
->dn_next_blksz
[0],
717 sizeof (odn
->dn_next_blksz
));
718 for (i
= 0; i
< TXG_SIZE
; i
++) {
719 list_move_tail(&ndn
->dn_dirty_records
[i
],
720 &odn
->dn_dirty_records
[i
]);
722 bcopy(&odn
->dn_free_ranges
[0], &ndn
->dn_free_ranges
[0],
723 sizeof (odn
->dn_free_ranges
));
724 ndn
->dn_allocated_txg
= odn
->dn_allocated_txg
;
725 ndn
->dn_free_txg
= odn
->dn_free_txg
;
726 ndn
->dn_assigned_txg
= odn
->dn_assigned_txg
;
727 ndn
->dn_dirtyctx
= odn
->dn_dirtyctx
;
728 ndn
->dn_dirtyctx_firstset
= odn
->dn_dirtyctx_firstset
;
729 ASSERT(refcount_count(&odn
->dn_tx_holds
) == 0);
730 refcount_transfer(&ndn
->dn_holds
, &odn
->dn_holds
);
731 ASSERT(avl_is_empty(&ndn
->dn_dbufs
));
732 avl_swap(&ndn
->dn_dbufs
, &odn
->dn_dbufs
);
733 ndn
->dn_dbufs_count
= odn
->dn_dbufs_count
;
734 ndn
->dn_unlisted_l0_blkid
= odn
->dn_unlisted_l0_blkid
;
735 ndn
->dn_bonus
= odn
->dn_bonus
;
736 ndn
->dn_have_spill
= odn
->dn_have_spill
;
737 ndn
->dn_zio
= odn
->dn_zio
;
738 ndn
->dn_oldused
= odn
->dn_oldused
;
739 ndn
->dn_oldflags
= odn
->dn_oldflags
;
740 ndn
->dn_olduid
= odn
->dn_olduid
;
741 ndn
->dn_oldgid
= odn
->dn_oldgid
;
742 ndn
->dn_newuid
= odn
->dn_newuid
;
743 ndn
->dn_newgid
= odn
->dn_newgid
;
744 ndn
->dn_id_flags
= odn
->dn_id_flags
;
745 dmu_zfetch_init(&ndn
->dn_zfetch
, NULL
);
746 list_move_tail(&ndn
->dn_zfetch
.zf_stream
, &odn
->dn_zfetch
.zf_stream
);
747 ndn
->dn_zfetch
.zf_dnode
= odn
->dn_zfetch
.zf_dnode
;
748 ndn
->dn_zfetch
.zf_stream_cnt
= odn
->dn_zfetch
.zf_stream_cnt
;
749 ndn
->dn_zfetch
.zf_alloc_fail
= odn
->dn_zfetch
.zf_alloc_fail
;
752 * Update back pointers. Updating the handle fixes the back pointer of
753 * every descendant dbuf as well as the bonus dbuf.
755 ASSERT(ndn
->dn_handle
->dnh_dnode
== odn
);
756 ndn
->dn_handle
->dnh_dnode
= ndn
;
757 if (ndn
->dn_zfetch
.zf_dnode
== odn
) {
758 ndn
->dn_zfetch
.zf_dnode
= ndn
;
762 * Invalidate the original dnode by clearing all of its back pointers.
765 odn
->dn_handle
= NULL
;
766 avl_create(&odn
->dn_dbufs
, dbuf_compare
, sizeof (dmu_buf_impl_t
),
767 offsetof(dmu_buf_impl_t
, db_link
));
768 odn
->dn_dbufs_count
= 0;
769 odn
->dn_unlisted_l0_blkid
= 0;
770 odn
->dn_bonus
= NULL
;
771 odn
->dn_zfetch
.zf_dnode
= NULL
;
774 * Set the low bit of the objset pointer to ensure that dnode_move()
775 * recognizes the dnode as invalid in any subsequent callback.
777 POINTER_INVALIDATE(&odn
->dn_objset
);
780 * Satisfy the destructor.
782 for (i
= 0; i
< TXG_SIZE
; i
++) {
783 list_create(&odn
->dn_dirty_records
[i
],
784 sizeof (dbuf_dirty_record_t
),
785 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
786 odn
->dn_free_ranges
[i
] = NULL
;
787 odn
->dn_next_nlevels
[i
] = 0;
788 odn
->dn_next_indblkshift
[i
] = 0;
789 odn
->dn_next_bonustype
[i
] = 0;
790 odn
->dn_rm_spillblk
[i
] = 0;
791 odn
->dn_next_bonuslen
[i
] = 0;
792 odn
->dn_next_blksz
[i
] = 0;
794 odn
->dn_allocated_txg
= 0;
795 odn
->dn_free_txg
= 0;
796 odn
->dn_assigned_txg
= 0;
797 odn
->dn_dirtyctx
= 0;
798 odn
->dn_dirtyctx_firstset
= NULL
;
799 odn
->dn_have_spill
= B_FALSE
;
802 odn
->dn_oldflags
= 0;
807 odn
->dn_id_flags
= 0;
813 odn
->dn_moved
= (uint8_t)-1;
818 dnode_move(void *buf
, void *newbuf
, size_t size
, void *arg
)
820 dnode_t
*odn
= buf
, *ndn
= newbuf
;
826 * The dnode is on the objset's list of known dnodes if the objset
827 * pointer is valid. We set the low bit of the objset pointer when
828 * freeing the dnode to invalidate it, and the memory patterns written
829 * by kmem (baddcafe and deadbeef) set at least one of the two low bits.
830 * A newly created dnode sets the objset pointer last of all to indicate
831 * that the dnode is known and in a valid state to be moved by this
835 if (!POINTER_IS_VALID(os
)) {
836 DNODE_STAT_ADD(dnode_move_stats
.dms_dnode_invalid
);
837 return (KMEM_CBRC_DONT_KNOW
);
841 * Ensure that the objset does not go away during the move.
843 rw_enter(&os_lock
, RW_WRITER
);
844 if (os
!= odn
->dn_objset
) {
846 DNODE_STAT_ADD(dnode_move_stats
.dms_dnode_recheck1
);
847 return (KMEM_CBRC_DONT_KNOW
);
851 * If the dnode is still valid, then so is the objset. We know that no
852 * valid objset can be freed while we hold os_lock, so we can safely
853 * ensure that the objset remains in use.
855 mutex_enter(&os
->os_lock
);
858 * Recheck the objset pointer in case the dnode was removed just before
859 * acquiring the lock.
861 if (os
!= odn
->dn_objset
) {
862 mutex_exit(&os
->os_lock
);
864 DNODE_STAT_ADD(dnode_move_stats
.dms_dnode_recheck2
);
865 return (KMEM_CBRC_DONT_KNOW
);
869 * At this point we know that as long as we hold os->os_lock, the dnode
870 * cannot be freed and fields within the dnode can be safely accessed.
871 * The objset listing this dnode cannot go away as long as this dnode is
875 if (DMU_OBJECT_IS_SPECIAL(odn
->dn_object
)) {
876 mutex_exit(&os
->os_lock
);
877 DNODE_STAT_ADD(dnode_move_stats
.dms_dnode_special
);
878 return (KMEM_CBRC_NO
);
880 ASSERT(odn
->dn_dbuf
!= NULL
); /* only "special" dnodes have no parent */
883 * Lock the dnode handle to prevent the dnode from obtaining any new
884 * holds. This also prevents the descendant dbufs and the bonus dbuf
885 * from accessing the dnode, so that we can discount their holds. The
886 * handle is safe to access because we know that while the dnode cannot
887 * go away, neither can its handle. Once we hold dnh_zrlock, we can
888 * safely move any dnode referenced only by dbufs.
890 if (!zrl_tryenter(&odn
->dn_handle
->dnh_zrlock
)) {
891 mutex_exit(&os
->os_lock
);
892 DNODE_STAT_ADD(dnode_move_stats
.dms_dnode_handle
);
893 return (KMEM_CBRC_LATER
);
897 * Ensure a consistent view of the dnode's holds and the dnode's dbufs.
898 * We need to guarantee that there is a hold for every dbuf in order to
899 * determine whether the dnode is actively referenced. Falsely matching
900 * a dbuf to an active hold would lead to an unsafe move. It's possible
901 * that a thread already having an active dnode hold is about to add a
902 * dbuf, and we can't compare hold and dbuf counts while the add is in
905 if (!rw_tryenter(&odn
->dn_struct_rwlock
, RW_WRITER
)) {
906 zrl_exit(&odn
->dn_handle
->dnh_zrlock
);
907 mutex_exit(&os
->os_lock
);
908 DNODE_STAT_ADD(dnode_move_stats
.dms_dnode_rwlock
);
909 return (KMEM_CBRC_LATER
);
913 * A dbuf may be removed (evicted) without an active dnode hold. In that
914 * case, the dbuf count is decremented under the handle lock before the
915 * dbuf's hold is released. This order ensures that if we count the hold
916 * after the dbuf is removed but before its hold is released, we will
917 * treat the unmatched hold as active and exit safely. If we count the
918 * hold before the dbuf is removed, the hold is discounted, and the
919 * removal is blocked until the move completes.
921 refcount
= refcount_count(&odn
->dn_holds
);
922 ASSERT(refcount
>= 0);
923 dbufs
= odn
->dn_dbufs_count
;
925 /* We can't have more dbufs than dnode holds. */
926 ASSERT3U(dbufs
, <=, refcount
);
927 DTRACE_PROBE3(dnode__move
, dnode_t
*, odn
, int64_t, refcount
,
930 if (refcount
> dbufs
) {
931 rw_exit(&odn
->dn_struct_rwlock
);
932 zrl_exit(&odn
->dn_handle
->dnh_zrlock
);
933 mutex_exit(&os
->os_lock
);
934 DNODE_STAT_ADD(dnode_move_stats
.dms_dnode_active
);
935 return (KMEM_CBRC_LATER
);
938 rw_exit(&odn
->dn_struct_rwlock
);
941 * At this point we know that anyone with a hold on the dnode is not
942 * actively referencing it. The dnode is known and in a valid state to
943 * move. We're holding the locks needed to execute the critical section.
945 dnode_move_impl(odn
, ndn
);
947 list_link_replace(&odn
->dn_link
, &ndn
->dn_link
);
948 /* If the dnode was safe to move, the refcount cannot have changed. */
949 ASSERT(refcount
== refcount_count(&ndn
->dn_holds
));
950 ASSERT(dbufs
== ndn
->dn_dbufs_count
);
951 zrl_exit(&ndn
->dn_handle
->dnh_zrlock
); /* handle has moved */
952 mutex_exit(&os
->os_lock
);
954 return (KMEM_CBRC_YES
);
959 dnode_special_close(dnode_handle_t
*dnh
)
961 dnode_t
*dn
= dnh
->dnh_dnode
;
964 * Wait for final references to the dnode to clear. This can
965 * only happen if the arc is asyncronously evicting state that
966 * has a hold on this dnode while we are trying to evict this
969 while (refcount_count(&dn
->dn_holds
) > 0)
971 zrl_add(&dnh
->dnh_zrlock
);
972 dnode_destroy(dn
); /* implicit zrl_remove() */
973 zrl_destroy(&dnh
->dnh_zrlock
);
974 dnh
->dnh_dnode
= NULL
;
978 dnode_special_open(objset_t
*os
, dnode_phys_t
*dnp
, uint64_t object
,
981 dnode_t
*dn
= dnode_create(os
, dnp
, NULL
, object
, dnh
);
983 zrl_init(&dnh
->dnh_zrlock
);
989 dnode_buf_pageout(dmu_buf_t
*db
, void *arg
)
991 dnode_children_t
*children_dnodes
= arg
;
993 int epb
= db
->db_size
>> DNODE_SHIFT
;
995 ASSERT(epb
== children_dnodes
->dnc_count
);
997 for (i
= 0; i
< epb
; i
++) {
998 dnode_handle_t
*dnh
= &children_dnodes
->dnc_children
[i
];
1002 * The dnode handle lock guards against the dnode moving to
1003 * another valid address, so there is no need here to guard
1004 * against changes to or from NULL.
1006 if (dnh
->dnh_dnode
== NULL
) {
1007 zrl_destroy(&dnh
->dnh_zrlock
);
1011 zrl_add(&dnh
->dnh_zrlock
);
1012 dn
= dnh
->dnh_dnode
;
1014 * If there are holds on this dnode, then there should
1015 * be holds on the dnode's containing dbuf as well; thus
1016 * it wouldn't be eligible for eviction and this function
1017 * would not have been called.
1019 ASSERT(refcount_is_zero(&dn
->dn_holds
));
1020 ASSERT(refcount_is_zero(&dn
->dn_tx_holds
));
1022 dnode_destroy(dn
); /* implicit zrl_remove() */
1023 zrl_destroy(&dnh
->dnh_zrlock
);
1024 dnh
->dnh_dnode
= NULL
;
1026 kmem_free(children_dnodes
, sizeof (dnode_children_t
) +
1027 epb
* sizeof (dnode_handle_t
));
1032 * EINVAL - invalid object number.
1034 * succeeds even for free dnodes.
1037 dnode_hold_impl(objset_t
*os
, uint64_t object
, int flag
,
1038 void *tag
, dnode_t
**dnp
)
1041 int drop_struct_lock
= FALSE
;
1046 dnode_children_t
*children_dnodes
;
1047 dnode_handle_t
*dnh
;
1050 * If you are holding the spa config lock as writer, you shouldn't
1051 * be asking the DMU to do *anything* unless it's the root pool
1052 * which may require us to read from the root filesystem while
1053 * holding some (not all) of the locks as writer.
1055 ASSERT(spa_config_held(os
->os_spa
, SCL_ALL
, RW_WRITER
) == 0 ||
1056 (spa_is_root(os
->os_spa
) &&
1057 spa_config_held(os
->os_spa
, SCL_STATE
, RW_WRITER
)));
1059 if (object
== DMU_USERUSED_OBJECT
|| object
== DMU_GROUPUSED_OBJECT
) {
1060 dn
= (object
== DMU_USERUSED_OBJECT
) ?
1061 DMU_USERUSED_DNODE(os
) : DMU_GROUPUSED_DNODE(os
);
1063 return (SET_ERROR(ENOENT
));
1065 if ((flag
& DNODE_MUST_BE_ALLOCATED
) && type
== DMU_OT_NONE
)
1066 return (SET_ERROR(ENOENT
));
1067 if ((flag
& DNODE_MUST_BE_FREE
) && type
!= DMU_OT_NONE
)
1068 return (SET_ERROR(EEXIST
));
1070 (void) refcount_add(&dn
->dn_holds
, tag
);
1075 if (object
== 0 || object
>= DN_MAX_OBJECT
)
1076 return (SET_ERROR(EINVAL
));
1078 mdn
= DMU_META_DNODE(os
);
1079 ASSERT(mdn
->dn_object
== DMU_META_DNODE_OBJECT
);
1083 if (!RW_WRITE_HELD(&mdn
->dn_struct_rwlock
)) {
1084 rw_enter(&mdn
->dn_struct_rwlock
, RW_READER
);
1085 drop_struct_lock
= TRUE
;
1088 blk
= dbuf_whichblock(mdn
, object
* sizeof (dnode_phys_t
));
1090 db
= dbuf_hold(mdn
, blk
, FTAG
);
1091 if (drop_struct_lock
)
1092 rw_exit(&mdn
->dn_struct_rwlock
);
1094 return (SET_ERROR(EIO
));
1095 err
= dbuf_read(db
, NULL
, DB_RF_CANFAIL
);
1097 dbuf_rele(db
, FTAG
);
1101 ASSERT3U(db
->db
.db_size
, >=, 1<<DNODE_SHIFT
);
1102 epb
= db
->db
.db_size
>> DNODE_SHIFT
;
1104 idx
= object
& (epb
-1);
1106 ASSERT(DB_DNODE(db
)->dn_type
== DMU_OT_DNODE
);
1107 children_dnodes
= dmu_buf_get_user(&db
->db
);
1108 if (children_dnodes
== NULL
) {
1110 dnode_children_t
*winner
;
1111 children_dnodes
= kmem_alloc(sizeof (dnode_children_t
) +
1112 epb
* sizeof (dnode_handle_t
), KM_SLEEP
);
1113 children_dnodes
->dnc_count
= epb
;
1114 dnh
= &children_dnodes
->dnc_children
[0];
1115 for (i
= 0; i
< epb
; i
++) {
1116 zrl_init(&dnh
[i
].dnh_zrlock
);
1117 dnh
[i
].dnh_dnode
= NULL
;
1119 if ((winner
= dmu_buf_set_user(&db
->db
, children_dnodes
, NULL
,
1120 dnode_buf_pageout
))) {
1122 for (i
= 0; i
< epb
; i
++) {
1123 zrl_destroy(&dnh
[i
].dnh_zrlock
);
1126 kmem_free(children_dnodes
, sizeof (dnode_children_t
) +
1127 epb
* sizeof (dnode_handle_t
));
1128 children_dnodes
= winner
;
1131 ASSERT(children_dnodes
->dnc_count
== epb
);
1133 dnh
= &children_dnodes
->dnc_children
[idx
];
1134 zrl_add(&dnh
->dnh_zrlock
);
1135 if ((dn
= dnh
->dnh_dnode
) == NULL
) {
1136 dnode_phys_t
*phys
= (dnode_phys_t
*)db
->db
.db_data
+idx
;
1139 dn
= dnode_create(os
, phys
, db
, object
, dnh
);
1140 winner
= atomic_cas_ptr(&dnh
->dnh_dnode
, NULL
, dn
);
1141 if (winner
!= NULL
) {
1142 zrl_add(&dnh
->dnh_zrlock
);
1143 dnode_destroy(dn
); /* implicit zrl_remove() */
1148 mutex_enter(&dn
->dn_mtx
);
1150 if (dn
->dn_free_txg
||
1151 ((flag
& DNODE_MUST_BE_ALLOCATED
) && type
== DMU_OT_NONE
) ||
1152 ((flag
& DNODE_MUST_BE_FREE
) &&
1153 (type
!= DMU_OT_NONE
|| !refcount_is_zero(&dn
->dn_holds
)))) {
1154 mutex_exit(&dn
->dn_mtx
);
1155 zrl_remove(&dnh
->dnh_zrlock
);
1156 dbuf_rele(db
, FTAG
);
1157 return (type
== DMU_OT_NONE
? ENOENT
: EEXIST
);
1159 mutex_exit(&dn
->dn_mtx
);
1161 if (refcount_add(&dn
->dn_holds
, tag
) == 1)
1162 dbuf_add_ref(db
, dnh
);
1163 /* Now we can rely on the hold to prevent the dnode from moving. */
1164 zrl_remove(&dnh
->dnh_zrlock
);
1167 ASSERT3P(dn
->dn_dbuf
, ==, db
);
1168 ASSERT3U(dn
->dn_object
, ==, object
);
1169 dbuf_rele(db
, FTAG
);
1176 * Return held dnode if the object is allocated, NULL if not.
1179 dnode_hold(objset_t
*os
, uint64_t object
, void *tag
, dnode_t
**dnp
)
1181 return (dnode_hold_impl(os
, object
, DNODE_MUST_BE_ALLOCATED
, tag
, dnp
));
1185 * Can only add a reference if there is already at least one
1186 * reference on the dnode. Returns FALSE if unable to add a
1190 dnode_add_ref(dnode_t
*dn
, void *tag
)
1192 mutex_enter(&dn
->dn_mtx
);
1193 if (refcount_is_zero(&dn
->dn_holds
)) {
1194 mutex_exit(&dn
->dn_mtx
);
1197 VERIFY(1 < refcount_add(&dn
->dn_holds
, tag
));
1198 mutex_exit(&dn
->dn_mtx
);
1203 dnode_rele(dnode_t
*dn
, void *tag
)
1205 mutex_enter(&dn
->dn_mtx
);
1206 dnode_rele_and_unlock(dn
, tag
);
1210 dnode_rele_and_unlock(dnode_t
*dn
, void *tag
)
1213 /* Get while the hold prevents the dnode from moving. */
1214 dmu_buf_impl_t
*db
= dn
->dn_dbuf
;
1215 dnode_handle_t
*dnh
= dn
->dn_handle
;
1217 refs
= refcount_remove(&dn
->dn_holds
, tag
);
1218 mutex_exit(&dn
->dn_mtx
);
1221 * It's unsafe to release the last hold on a dnode by dnode_rele() or
1222 * indirectly by dbuf_rele() while relying on the dnode handle to
1223 * prevent the dnode from moving, since releasing the last hold could
1224 * result in the dnode's parent dbuf evicting its dnode handles. For
1225 * that reason anyone calling dnode_rele() or dbuf_rele() without some
1226 * other direct or indirect hold on the dnode must first drop the dnode
1229 ASSERT(refs
> 0 || dnh
->dnh_zrlock
.zr_owner
!= curthread
);
1231 /* NOTE: the DNODE_DNODE does not have a dn_dbuf */
1232 if (refs
== 0 && db
!= NULL
) {
1234 * Another thread could add a hold to the dnode handle in
1235 * dnode_hold_impl() while holding the parent dbuf. Since the
1236 * hold on the parent dbuf prevents the handle from being
1237 * destroyed, the hold on the handle is OK. We can't yet assert
1238 * that the handle has zero references, but that will be
1239 * asserted anyway when the handle gets destroyed.
1246 dnode_setdirty(dnode_t
*dn
, dmu_tx_t
*tx
)
1248 objset_t
*os
= dn
->dn_objset
;
1249 uint64_t txg
= tx
->tx_txg
;
1251 if (DMU_OBJECT_IS_SPECIAL(dn
->dn_object
)) {
1252 dsl_dataset_dirty(os
->os_dsl_dataset
, tx
);
1259 mutex_enter(&dn
->dn_mtx
);
1260 ASSERT(dn
->dn_phys
->dn_type
|| dn
->dn_allocated_txg
);
1261 ASSERT(dn
->dn_free_txg
== 0 || dn
->dn_free_txg
>= txg
);
1262 mutex_exit(&dn
->dn_mtx
);
1266 * Determine old uid/gid when necessary
1268 dmu_objset_userquota_get_ids(dn
, B_TRUE
, tx
);
1270 mutex_enter(&os
->os_lock
);
1273 * If we are already marked dirty, we're done.
1275 if (list_link_active(&dn
->dn_dirty_link
[txg
& TXG_MASK
])) {
1276 mutex_exit(&os
->os_lock
);
1280 ASSERT(!refcount_is_zero(&dn
->dn_holds
) ||
1281 !avl_is_empty(&dn
->dn_dbufs
));
1282 ASSERT(dn
->dn_datablksz
!= 0);
1283 ASSERT0(dn
->dn_next_bonuslen
[txg
&TXG_MASK
]);
1284 ASSERT0(dn
->dn_next_blksz
[txg
&TXG_MASK
]);
1285 ASSERT0(dn
->dn_next_bonustype
[txg
&TXG_MASK
]);
1287 dprintf_ds(os
->os_dsl_dataset
, "obj=%llu txg=%llu\n",
1288 dn
->dn_object
, txg
);
1290 if (dn
->dn_free_txg
> 0 && dn
->dn_free_txg
<= txg
) {
1291 list_insert_tail(&os
->os_free_dnodes
[txg
&TXG_MASK
], dn
);
1293 list_insert_tail(&os
->os_dirty_dnodes
[txg
&TXG_MASK
], dn
);
1296 mutex_exit(&os
->os_lock
);
1299 * The dnode maintains a hold on its containing dbuf as
1300 * long as there are holds on it. Each instantiated child
1301 * dbuf maintains a hold on the dnode. When the last child
1302 * drops its hold, the dnode will drop its hold on the
1303 * containing dbuf. We add a "dirty hold" here so that the
1304 * dnode will hang around after we finish processing its
1307 VERIFY(dnode_add_ref(dn
, (void *)(uintptr_t)tx
->tx_txg
));
1309 (void) dbuf_dirty(dn
->dn_dbuf
, tx
);
1311 dsl_dataset_dirty(os
->os_dsl_dataset
, tx
);
1315 dnode_free(dnode_t
*dn
, dmu_tx_t
*tx
)
1317 int txgoff
= tx
->tx_txg
& TXG_MASK
;
1319 dprintf("dn=%p txg=%llu\n", dn
, tx
->tx_txg
);
1321 /* we should be the only holder... hopefully */
1322 /* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */
1324 mutex_enter(&dn
->dn_mtx
);
1325 if (dn
->dn_type
== DMU_OT_NONE
|| dn
->dn_free_txg
) {
1326 mutex_exit(&dn
->dn_mtx
);
1329 dn
->dn_free_txg
= tx
->tx_txg
;
1330 mutex_exit(&dn
->dn_mtx
);
1333 * If the dnode is already dirty, it needs to be moved from
1334 * the dirty list to the free list.
1336 mutex_enter(&dn
->dn_objset
->os_lock
);
1337 if (list_link_active(&dn
->dn_dirty_link
[txgoff
])) {
1338 list_remove(&dn
->dn_objset
->os_dirty_dnodes
[txgoff
], dn
);
1339 list_insert_tail(&dn
->dn_objset
->os_free_dnodes
[txgoff
], dn
);
1340 mutex_exit(&dn
->dn_objset
->os_lock
);
1342 mutex_exit(&dn
->dn_objset
->os_lock
);
1343 dnode_setdirty(dn
, tx
);
1348 * Try to change the block size for the indicated dnode. This can only
1349 * succeed if there are no blocks allocated or dirty beyond first block
1352 dnode_set_blksz(dnode_t
*dn
, uint64_t size
, int ibs
, dmu_tx_t
*tx
)
1358 size
= SPA_MINBLOCKSIZE
;
1359 if (size
> SPA_MAXBLOCKSIZE
)
1360 size
= SPA_MAXBLOCKSIZE
;
1362 size
= P2ROUNDUP(size
, SPA_MINBLOCKSIZE
);
1364 if (ibs
== dn
->dn_indblkshift
)
1367 if (size
>> SPA_MINBLOCKSHIFT
== dn
->dn_datablkszsec
&& ibs
== 0)
1370 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1372 /* Check for any allocated blocks beyond the first */
1373 if (dn
->dn_maxblkid
!= 0)
1376 mutex_enter(&dn
->dn_dbufs_mtx
);
1377 for (db
= avl_first(&dn
->dn_dbufs
); db
!= NULL
;
1378 db
= AVL_NEXT(&dn
->dn_dbufs
, db
)) {
1379 if (db
->db_blkid
!= 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1380 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1381 mutex_exit(&dn
->dn_dbufs_mtx
);
1385 mutex_exit(&dn
->dn_dbufs_mtx
);
1387 if (ibs
&& dn
->dn_nlevels
!= 1)
1390 /* resize the old block */
1391 err
= dbuf_hold_impl(dn
, 0, 0, TRUE
, FTAG
, &db
);
1393 dbuf_new_size(db
, size
, tx
);
1394 else if (err
!= ENOENT
)
1397 dnode_setdblksz(dn
, size
);
1398 dnode_setdirty(dn
, tx
);
1399 dn
->dn_next_blksz
[tx
->tx_txg
&TXG_MASK
] = size
;
1401 dn
->dn_indblkshift
= ibs
;
1402 dn
->dn_next_indblkshift
[tx
->tx_txg
&TXG_MASK
] = ibs
;
1404 /* rele after we have fixed the blocksize in the dnode */
1406 dbuf_rele(db
, FTAG
);
1408 rw_exit(&dn
->dn_struct_rwlock
);
1412 rw_exit(&dn
->dn_struct_rwlock
);
1413 return (SET_ERROR(ENOTSUP
));
1416 /* read-holding callers must not rely on the lock being continuously held */
1418 dnode_new_blkid(dnode_t
*dn
, uint64_t blkid
, dmu_tx_t
*tx
, boolean_t have_read
)
1420 uint64_t txgoff
= tx
->tx_txg
& TXG_MASK
;
1421 int epbs
, new_nlevels
;
1424 ASSERT(blkid
!= DMU_BONUS_BLKID
);
1427 RW_READ_HELD(&dn
->dn_struct_rwlock
) :
1428 RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1431 * if we have a read-lock, check to see if we need to do any work
1432 * before upgrading to a write-lock.
1435 if (blkid
<= dn
->dn_maxblkid
)
1438 if (!rw_tryupgrade(&dn
->dn_struct_rwlock
)) {
1439 rw_exit(&dn
->dn_struct_rwlock
);
1440 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1444 if (blkid
<= dn
->dn_maxblkid
)
1447 dn
->dn_maxblkid
= blkid
;
1450 * Compute the number of levels necessary to support the new maxblkid.
1453 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1454 for (sz
= dn
->dn_nblkptr
;
1455 sz
<= blkid
&& sz
>= dn
->dn_nblkptr
; sz
<<= epbs
)
1458 if (new_nlevels
> dn
->dn_nlevels
) {
1459 int old_nlevels
= dn
->dn_nlevels
;
1462 dbuf_dirty_record_t
*new, *dr
, *dr_next
;
1464 dn
->dn_nlevels
= new_nlevels
;
1466 ASSERT3U(new_nlevels
, >, dn
->dn_next_nlevels
[txgoff
]);
1467 dn
->dn_next_nlevels
[txgoff
] = new_nlevels
;
1469 /* dirty the left indirects */
1470 db
= dbuf_hold_level(dn
, old_nlevels
, 0, FTAG
);
1472 new = dbuf_dirty(db
, tx
);
1473 dbuf_rele(db
, FTAG
);
1475 /* transfer the dirty records to the new indirect */
1476 mutex_enter(&dn
->dn_mtx
);
1477 mutex_enter(&new->dt
.di
.dr_mtx
);
1478 list
= &dn
->dn_dirty_records
[txgoff
];
1479 for (dr
= list_head(list
); dr
; dr
= dr_next
) {
1480 dr_next
= list_next(&dn
->dn_dirty_records
[txgoff
], dr
);
1481 if (dr
->dr_dbuf
->db_level
!= new_nlevels
-1 &&
1482 dr
->dr_dbuf
->db_blkid
!= DMU_BONUS_BLKID
&&
1483 dr
->dr_dbuf
->db_blkid
!= DMU_SPILL_BLKID
) {
1484 ASSERT(dr
->dr_dbuf
->db_level
== old_nlevels
-1);
1485 list_remove(&dn
->dn_dirty_records
[txgoff
], dr
);
1486 list_insert_tail(&new->dt
.di
.dr_children
, dr
);
1487 dr
->dr_parent
= new;
1490 mutex_exit(&new->dt
.di
.dr_mtx
);
1491 mutex_exit(&dn
->dn_mtx
);
1496 rw_downgrade(&dn
->dn_struct_rwlock
);
1500 dnode_free_range(dnode_t
*dn
, uint64_t off
, uint64_t len
, dmu_tx_t
*tx
)
1503 uint64_t blkoff
, blkid
, nblks
;
1504 int blksz
, blkshift
, head
, tail
;
1508 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1509 blksz
= dn
->dn_datablksz
;
1510 blkshift
= dn
->dn_datablkshift
;
1511 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1513 if (len
== DMU_OBJECT_END
) {
1514 len
= UINT64_MAX
- off
;
1519 * First, block align the region to free:
1522 head
= P2NPHASE(off
, blksz
);
1523 blkoff
= P2PHASE(off
, blksz
);
1524 if ((off
>> blkshift
) > dn
->dn_maxblkid
)
1527 ASSERT(dn
->dn_maxblkid
== 0);
1528 if (off
== 0 && len
>= blksz
) {
1530 * Freeing the whole block; fast-track this request.
1531 * Note that we won't dirty any indirect blocks,
1532 * which is fine because we will be freeing the entire
1533 * file and thus all indirect blocks will be freed
1534 * by free_children().
1539 } else if (off
>= blksz
) {
1540 /* Freeing past end-of-data */
1543 /* Freeing part of the block. */
1545 ASSERT3U(head
, >, 0);
1549 /* zero out any partial block data at the start of the range */
1551 ASSERT3U(blkoff
+ head
, ==, blksz
);
1554 if (dbuf_hold_impl(dn
, 0, dbuf_whichblock(dn
, off
), TRUE
,
1558 /* don't dirty if it isn't on disk and isn't dirty */
1559 if (db
->db_last_dirty
||
1560 (db
->db_blkptr
&& !BP_IS_HOLE(db
->db_blkptr
))) {
1561 rw_exit(&dn
->dn_struct_rwlock
);
1562 dmu_buf_will_dirty(&db
->db
, tx
);
1563 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1564 data
= db
->db
.db_data
;
1565 bzero(data
+ blkoff
, head
);
1567 dbuf_rele(db
, FTAG
);
1573 /* If the range was less than one block, we're done */
1577 /* If the remaining range is past end of file, we're done */
1578 if ((off
>> blkshift
) > dn
->dn_maxblkid
)
1581 ASSERT(ISP2(blksz
));
1585 tail
= P2PHASE(len
, blksz
);
1587 ASSERT0(P2PHASE(off
, blksz
));
1588 /* zero out any partial block data at the end of the range */
1592 if (dbuf_hold_impl(dn
, 0, dbuf_whichblock(dn
, off
+len
),
1593 TRUE
, FTAG
, &db
) == 0) {
1594 /* don't dirty if not on disk and not dirty */
1595 if (db
->db_last_dirty
||
1596 (db
->db_blkptr
&& !BP_IS_HOLE(db
->db_blkptr
))) {
1597 rw_exit(&dn
->dn_struct_rwlock
);
1598 dmu_buf_will_dirty(&db
->db
, tx
);
1599 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1600 bzero(db
->db
.db_data
, tail
);
1602 dbuf_rele(db
, FTAG
);
1607 /* If the range did not include a full block, we are done */
1611 ASSERT(IS_P2ALIGNED(off
, blksz
));
1612 ASSERT(trunc
|| IS_P2ALIGNED(len
, blksz
));
1613 blkid
= off
>> blkshift
;
1614 nblks
= len
>> blkshift
;
1619 * Dirty the first and last indirect blocks, as they (and/or their
1620 * parents) will need to be written out if they were only
1621 * partially freed. Interior indirect blocks will be themselves freed,
1622 * by free_children(), so they need not be dirtied. Note that these
1623 * interior blocks have already been prefetched by dmu_tx_hold_free().
1625 if (dn
->dn_nlevels
> 1) {
1626 uint64_t first
, last
;
1628 first
= blkid
>> epbs
;
1629 if ((db
= dbuf_hold_level(dn
, 1, first
, FTAG
))) {
1630 dmu_buf_will_dirty(&db
->db
, tx
);
1631 dbuf_rele(db
, FTAG
);
1634 last
= dn
->dn_maxblkid
>> epbs
;
1636 last
= (blkid
+ nblks
- 1) >> epbs
;
1637 if (last
> first
&& (db
= dbuf_hold_level(dn
, 1, last
, FTAG
))) {
1638 dmu_buf_will_dirty(&db
->db
, tx
);
1639 dbuf_rele(db
, FTAG
);
1645 * Add this range to the dnode range list.
1646 * We will finish up this free operation in the syncing phase.
1648 mutex_enter(&dn
->dn_mtx
);
1650 int txgoff
= tx
->tx_txg
& TXG_MASK
;
1651 if (dn
->dn_free_ranges
[txgoff
] == NULL
) {
1652 dn
->dn_free_ranges
[txgoff
] =
1653 range_tree_create(NULL
, NULL
, &dn
->dn_mtx
);
1655 range_tree_clear(dn
->dn_free_ranges
[txgoff
], blkid
, nblks
);
1656 range_tree_add(dn
->dn_free_ranges
[txgoff
], blkid
, nblks
);
1658 dprintf_dnode(dn
, "blkid=%llu nblks=%llu txg=%llu\n",
1659 blkid
, nblks
, tx
->tx_txg
);
1660 mutex_exit(&dn
->dn_mtx
);
1662 dbuf_free_range(dn
, blkid
, blkid
+ nblks
- 1, tx
);
1663 dnode_setdirty(dn
, tx
);
1666 rw_exit(&dn
->dn_struct_rwlock
);
1670 dnode_spill_freed(dnode_t
*dn
)
1674 mutex_enter(&dn
->dn_mtx
);
1675 for (i
= 0; i
< TXG_SIZE
; i
++) {
1676 if (dn
->dn_rm_spillblk
[i
] == DN_KILL_SPILLBLK
)
1679 mutex_exit(&dn
->dn_mtx
);
1680 return (i
< TXG_SIZE
);
1683 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
1685 dnode_block_freed(dnode_t
*dn
, uint64_t blkid
)
1687 void *dp
= spa_get_dsl(dn
->dn_objset
->os_spa
);
1690 if (blkid
== DMU_BONUS_BLKID
)
1694 * If we're in the process of opening the pool, dp will not be
1695 * set yet, but there shouldn't be anything dirty.
1700 if (dn
->dn_free_txg
)
1703 if (blkid
== DMU_SPILL_BLKID
)
1704 return (dnode_spill_freed(dn
));
1706 mutex_enter(&dn
->dn_mtx
);
1707 for (i
= 0; i
< TXG_SIZE
; i
++) {
1708 if (dn
->dn_free_ranges
[i
] != NULL
&&
1709 range_tree_contains(dn
->dn_free_ranges
[i
], blkid
, 1))
1712 mutex_exit(&dn
->dn_mtx
);
1713 return (i
< TXG_SIZE
);
1716 /* call from syncing context when we actually write/free space for this dnode */
1718 dnode_diduse_space(dnode_t
*dn
, int64_t delta
)
1721 dprintf_dnode(dn
, "dn=%p dnp=%p used=%llu delta=%lld\n",
1723 (u_longlong_t
)dn
->dn_phys
->dn_used
,
1726 mutex_enter(&dn
->dn_mtx
);
1727 space
= DN_USED_BYTES(dn
->dn_phys
);
1729 ASSERT3U(space
+ delta
, >=, space
); /* no overflow */
1731 ASSERT3U(space
, >=, -delta
); /* no underflow */
1734 if (spa_version(dn
->dn_objset
->os_spa
) < SPA_VERSION_DNODE_BYTES
) {
1735 ASSERT((dn
->dn_phys
->dn_flags
& DNODE_FLAG_USED_BYTES
) == 0);
1736 ASSERT0(P2PHASE(space
, 1<<DEV_BSHIFT
));
1737 dn
->dn_phys
->dn_used
= space
>> DEV_BSHIFT
;
1739 dn
->dn_phys
->dn_used
= space
;
1740 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_USED_BYTES
;
1742 mutex_exit(&dn
->dn_mtx
);
1746 * Call when we think we're going to write/free space in open context to track
1747 * the amount of memory in use by the currently open txg.
1750 dnode_willuse_space(dnode_t
*dn
, int64_t space
, dmu_tx_t
*tx
)
1752 objset_t
*os
= dn
->dn_objset
;
1753 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1754 int64_t aspace
= spa_get_asize(os
->os_spa
, space
);
1757 dsl_dir_willuse_space(ds
->ds_dir
, aspace
, tx
);
1758 dsl_pool_dirty_space(dmu_tx_pool(tx
), space
, tx
);
1761 dmu_tx_willuse_space(tx
, aspace
);
1765 * Scans a block at the indicated "level" looking for a hole or data,
1766 * depending on 'flags'.
1768 * If level > 0, then we are scanning an indirect block looking at its
1769 * pointers. If level == 0, then we are looking at a block of dnodes.
1771 * If we don't find what we are looking for in the block, we return ESRCH.
1772 * Otherwise, return with *offset pointing to the beginning (if searching
1773 * forwards) or end (if searching backwards) of the range covered by the
1774 * block pointer we matched on (or dnode).
1776 * The basic search algorithm used below by dnode_next_offset() is to
1777 * use this function to search up the block tree (widen the search) until
1778 * we find something (i.e., we don't return ESRCH) and then search back
1779 * down the tree (narrow the search) until we reach our original search
1783 dnode_next_offset_level(dnode_t
*dn
, int flags
, uint64_t *offset
,
1784 int lvl
, uint64_t blkfill
, uint64_t txg
)
1786 dmu_buf_impl_t
*db
= NULL
;
1788 uint64_t epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1789 uint64_t epb
= 1ULL << epbs
;
1790 uint64_t minfill
, maxfill
;
1792 int i
, inc
, error
, span
;
1794 dprintf("probing object %llu offset %llx level %d of %u\n",
1795 dn
->dn_object
, *offset
, lvl
, dn
->dn_phys
->dn_nlevels
);
1797 hole
= ((flags
& DNODE_FIND_HOLE
) != 0);
1798 inc
= (flags
& DNODE_FIND_BACKWARDS
) ? -1 : 1;
1799 ASSERT(txg
== 0 || !hole
);
1801 if (lvl
== dn
->dn_phys
->dn_nlevels
) {
1803 epb
= dn
->dn_phys
->dn_nblkptr
;
1804 data
= dn
->dn_phys
->dn_blkptr
;
1806 uint64_t blkid
= dbuf_whichblock(dn
, *offset
) >> (epbs
* lvl
);
1807 error
= dbuf_hold_impl(dn
, lvl
, blkid
, TRUE
, FTAG
, &db
);
1809 if (error
!= ENOENT
)
1814 * This can only happen when we are searching up
1815 * the block tree for data. We don't really need to
1816 * adjust the offset, as we will just end up looking
1817 * at the pointer to this block in its parent, and its
1818 * going to be unallocated, so we will skip over it.
1820 return (SET_ERROR(ESRCH
));
1822 error
= dbuf_read(db
, NULL
, DB_RF_CANFAIL
| DB_RF_HAVESTRUCT
);
1824 dbuf_rele(db
, FTAG
);
1827 data
= db
->db
.db_data
;
1831 if (db
!= NULL
&& txg
!= 0 && (db
->db_blkptr
== NULL
||
1832 db
->db_blkptr
->blk_birth
<= txg
||
1833 BP_IS_HOLE(db
->db_blkptr
))) {
1835 * This can only happen when we are searching up the tree
1836 * and these conditions mean that we need to keep climbing.
1838 error
= SET_ERROR(ESRCH
);
1839 } else if (lvl
== 0) {
1840 dnode_phys_t
*dnp
= data
;
1842 ASSERT(dn
->dn_type
== DMU_OT_DNODE
);
1844 for (i
= (*offset
>> span
) & (blkfill
- 1);
1845 i
>= 0 && i
< blkfill
; i
+= inc
) {
1846 if ((dnp
[i
].dn_type
== DMU_OT_NONE
) == hole
)
1848 *offset
+= (1ULL << span
) * inc
;
1850 if (i
< 0 || i
== blkfill
)
1851 error
= SET_ERROR(ESRCH
);
1853 blkptr_t
*bp
= data
;
1854 uint64_t start
= *offset
;
1855 span
= (lvl
- 1) * epbs
+ dn
->dn_datablkshift
;
1857 maxfill
= blkfill
<< ((lvl
- 1) * epbs
);
1864 *offset
= *offset
>> span
;
1865 for (i
= BF64_GET(*offset
, 0, epbs
);
1866 i
>= 0 && i
< epb
; i
+= inc
) {
1867 if (BP_GET_FILL(&bp
[i
]) >= minfill
&&
1868 BP_GET_FILL(&bp
[i
]) <= maxfill
&&
1869 (hole
|| bp
[i
].blk_birth
> txg
))
1871 if (inc
> 0 || *offset
> 0)
1874 *offset
= *offset
<< span
;
1876 /* traversing backwards; position offset at the end */
1877 ASSERT3U(*offset
, <=, start
);
1878 *offset
= MIN(*offset
+ (1ULL << span
) - 1, start
);
1879 } else if (*offset
< start
) {
1882 if (i
< 0 || i
>= epb
)
1883 error
= SET_ERROR(ESRCH
);
1887 dbuf_rele(db
, FTAG
);
1893 * Find the next hole, data, or sparse region at or after *offset.
1894 * The value 'blkfill' tells us how many items we expect to find
1895 * in an L0 data block; this value is 1 for normal objects,
1896 * DNODES_PER_BLOCK for the meta dnode, and some fraction of
1897 * DNODES_PER_BLOCK when searching for sparse regions thereof.
1901 * dnode_next_offset(dn, flags, offset, 1, 1, 0);
1902 * Finds the next/previous hole/data in a file.
1903 * Used in dmu_offset_next().
1905 * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
1906 * Finds the next free/allocated dnode an objset's meta-dnode.
1907 * Only finds objects that have new contents since txg (ie.
1908 * bonus buffer changes and content removal are ignored).
1909 * Used in dmu_object_next().
1911 * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
1912 * Finds the next L2 meta-dnode bp that's at most 1/4 full.
1913 * Used in dmu_object_alloc().
1916 dnode_next_offset(dnode_t
*dn
, int flags
, uint64_t *offset
,
1917 int minlvl
, uint64_t blkfill
, uint64_t txg
)
1919 uint64_t initial_offset
= *offset
;
1923 if (!(flags
& DNODE_FIND_HAVELOCK
))
1924 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1926 if (dn
->dn_phys
->dn_nlevels
== 0) {
1927 error
= SET_ERROR(ESRCH
);
1931 if (dn
->dn_datablkshift
== 0) {
1932 if (*offset
< dn
->dn_datablksz
) {
1933 if (flags
& DNODE_FIND_HOLE
)
1934 *offset
= dn
->dn_datablksz
;
1936 error
= SET_ERROR(ESRCH
);
1941 maxlvl
= dn
->dn_phys
->dn_nlevels
;
1943 for (lvl
= minlvl
; lvl
<= maxlvl
; lvl
++) {
1944 error
= dnode_next_offset_level(dn
,
1945 flags
, offset
, lvl
, blkfill
, txg
);
1950 while (error
== 0 && --lvl
>= minlvl
) {
1951 error
= dnode_next_offset_level(dn
,
1952 flags
, offset
, lvl
, blkfill
, txg
);
1956 * There's always a "virtual hole" at the end of the object, even
1957 * if all BP's which physically exist are non-holes.
1959 if ((flags
& DNODE_FIND_HOLE
) && error
== ESRCH
&& txg
== 0 &&
1960 minlvl
== 1 && blkfill
== 1 && !(flags
& DNODE_FIND_BACKWARDS
)) {
1964 if (error
== 0 && (flags
& DNODE_FIND_BACKWARDS
?
1965 initial_offset
< *offset
: initial_offset
> *offset
))
1966 error
= SET_ERROR(ESRCH
);
1968 if (!(flags
& DNODE_FIND_HAVELOCK
))
1969 rw_exit(&dn
->dn_struct_rwlock
);