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, 2017 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>
41 #include <sys/zfs_project.h>
43 dnode_stats_t dnode_stats
= {
44 { "dnode_hold_dbuf_hold", KSTAT_DATA_UINT64
},
45 { "dnode_hold_dbuf_read", KSTAT_DATA_UINT64
},
46 { "dnode_hold_alloc_hits", KSTAT_DATA_UINT64
},
47 { "dnode_hold_alloc_misses", KSTAT_DATA_UINT64
},
48 { "dnode_hold_alloc_interior", KSTAT_DATA_UINT64
},
49 { "dnode_hold_alloc_lock_retry", KSTAT_DATA_UINT64
},
50 { "dnode_hold_alloc_lock_misses", KSTAT_DATA_UINT64
},
51 { "dnode_hold_alloc_type_none", KSTAT_DATA_UINT64
},
52 { "dnode_hold_free_hits", KSTAT_DATA_UINT64
},
53 { "dnode_hold_free_misses", KSTAT_DATA_UINT64
},
54 { "dnode_hold_free_lock_misses", KSTAT_DATA_UINT64
},
55 { "dnode_hold_free_lock_retry", KSTAT_DATA_UINT64
},
56 { "dnode_hold_free_overflow", KSTAT_DATA_UINT64
},
57 { "dnode_hold_free_refcount", KSTAT_DATA_UINT64
},
58 { "dnode_hold_free_txg", KSTAT_DATA_UINT64
},
59 { "dnode_free_interior_lock_retry", KSTAT_DATA_UINT64
},
60 { "dnode_allocate", KSTAT_DATA_UINT64
},
61 { "dnode_reallocate", KSTAT_DATA_UINT64
},
62 { "dnode_buf_evict", KSTAT_DATA_UINT64
},
63 { "dnode_alloc_next_chunk", KSTAT_DATA_UINT64
},
64 { "dnode_alloc_race", KSTAT_DATA_UINT64
},
65 { "dnode_alloc_next_block", KSTAT_DATA_UINT64
},
66 { "dnode_move_invalid", KSTAT_DATA_UINT64
},
67 { "dnode_move_recheck1", KSTAT_DATA_UINT64
},
68 { "dnode_move_recheck2", KSTAT_DATA_UINT64
},
69 { "dnode_move_special", KSTAT_DATA_UINT64
},
70 { "dnode_move_handle", KSTAT_DATA_UINT64
},
71 { "dnode_move_rwlock", KSTAT_DATA_UINT64
},
72 { "dnode_move_active", KSTAT_DATA_UINT64
},
75 static kstat_t
*dnode_ksp
;
76 static kmem_cache_t
*dnode_cache
;
78 ASSERTV(static dnode_phys_t dnode_phys_zero
);
80 int zfs_default_bs
= SPA_MINBLOCKSHIFT
;
81 int zfs_default_ibs
= DN_MAX_INDBLKSHIFT
;
84 static kmem_cbrc_t
dnode_move(void *, void *, size_t, void *);
88 dbuf_compare(const void *x1
, const void *x2
)
90 const dmu_buf_impl_t
*d1
= x1
;
91 const dmu_buf_impl_t
*d2
= x2
;
93 int cmp
= AVL_CMP(d1
->db_level
, d2
->db_level
);
97 cmp
= AVL_CMP(d1
->db_blkid
, d2
->db_blkid
);
101 if (d1
->db_state
== DB_SEARCH
) {
102 ASSERT3S(d2
->db_state
, !=, DB_SEARCH
);
104 } else if (d2
->db_state
== DB_SEARCH
) {
105 ASSERT3S(d1
->db_state
, !=, DB_SEARCH
);
109 return (AVL_PCMP(d1
, d2
));
114 dnode_cons(void *arg
, void *unused
, int kmflag
)
119 rw_init(&dn
->dn_struct_rwlock
, NULL
, RW_NOLOCKDEP
, NULL
);
120 mutex_init(&dn
->dn_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
121 mutex_init(&dn
->dn_dbufs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
122 cv_init(&dn
->dn_notxholds
, NULL
, CV_DEFAULT
, NULL
);
125 * Every dbuf has a reference, and dropping a tracked reference is
126 * O(number of references), so don't track dn_holds.
128 zfs_refcount_create_untracked(&dn
->dn_holds
);
129 zfs_refcount_create(&dn
->dn_tx_holds
);
130 list_link_init(&dn
->dn_link
);
132 bzero(&dn
->dn_next_nblkptr
[0], sizeof (dn
->dn_next_nblkptr
));
133 bzero(&dn
->dn_next_nlevels
[0], sizeof (dn
->dn_next_nlevels
));
134 bzero(&dn
->dn_next_indblkshift
[0], sizeof (dn
->dn_next_indblkshift
));
135 bzero(&dn
->dn_next_bonustype
[0], sizeof (dn
->dn_next_bonustype
));
136 bzero(&dn
->dn_rm_spillblk
[0], sizeof (dn
->dn_rm_spillblk
));
137 bzero(&dn
->dn_next_bonuslen
[0], sizeof (dn
->dn_next_bonuslen
));
138 bzero(&dn
->dn_next_blksz
[0], sizeof (dn
->dn_next_blksz
));
139 bzero(&dn
->dn_next_maxblkid
[0], sizeof (dn
->dn_next_maxblkid
));
141 for (i
= 0; i
< TXG_SIZE
; i
++) {
142 multilist_link_init(&dn
->dn_dirty_link
[i
]);
143 dn
->dn_free_ranges
[i
] = NULL
;
144 list_create(&dn
->dn_dirty_records
[i
],
145 sizeof (dbuf_dirty_record_t
),
146 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
149 dn
->dn_allocated_txg
= 0;
151 dn
->dn_assigned_txg
= 0;
152 dn
->dn_dirty_txg
= 0;
154 dn
->dn_dirtyctx_firstset
= NULL
;
156 dn
->dn_have_spill
= B_FALSE
;
162 dn
->dn_oldprojid
= ZFS_DEFAULT_PROJID
;
165 dn
->dn_newprojid
= ZFS_DEFAULT_PROJID
;
168 dn
->dn_dbufs_count
= 0;
169 avl_create(&dn
->dn_dbufs
, dbuf_compare
, sizeof (dmu_buf_impl_t
),
170 offsetof(dmu_buf_impl_t
, db_link
));
178 dnode_dest(void *arg
, void *unused
)
183 rw_destroy(&dn
->dn_struct_rwlock
);
184 mutex_destroy(&dn
->dn_mtx
);
185 mutex_destroy(&dn
->dn_dbufs_mtx
);
186 cv_destroy(&dn
->dn_notxholds
);
187 zfs_refcount_destroy(&dn
->dn_holds
);
188 zfs_refcount_destroy(&dn
->dn_tx_holds
);
189 ASSERT(!list_link_active(&dn
->dn_link
));
191 for (i
= 0; i
< TXG_SIZE
; i
++) {
192 ASSERT(!multilist_link_active(&dn
->dn_dirty_link
[i
]));
193 ASSERT3P(dn
->dn_free_ranges
[i
], ==, NULL
);
194 list_destroy(&dn
->dn_dirty_records
[i
]);
195 ASSERT0(dn
->dn_next_nblkptr
[i
]);
196 ASSERT0(dn
->dn_next_nlevels
[i
]);
197 ASSERT0(dn
->dn_next_indblkshift
[i
]);
198 ASSERT0(dn
->dn_next_bonustype
[i
]);
199 ASSERT0(dn
->dn_rm_spillblk
[i
]);
200 ASSERT0(dn
->dn_next_bonuslen
[i
]);
201 ASSERT0(dn
->dn_next_blksz
[i
]);
202 ASSERT0(dn
->dn_next_maxblkid
[i
]);
205 ASSERT0(dn
->dn_allocated_txg
);
206 ASSERT0(dn
->dn_free_txg
);
207 ASSERT0(dn
->dn_assigned_txg
);
208 ASSERT0(dn
->dn_dirty_txg
);
209 ASSERT0(dn
->dn_dirtyctx
);
210 ASSERT3P(dn
->dn_dirtyctx_firstset
, ==, NULL
);
211 ASSERT3P(dn
->dn_bonus
, ==, NULL
);
212 ASSERT(!dn
->dn_have_spill
);
213 ASSERT3P(dn
->dn_zio
, ==, NULL
);
214 ASSERT0(dn
->dn_oldused
);
215 ASSERT0(dn
->dn_oldflags
);
216 ASSERT0(dn
->dn_olduid
);
217 ASSERT0(dn
->dn_oldgid
);
218 ASSERT0(dn
->dn_oldprojid
);
219 ASSERT0(dn
->dn_newuid
);
220 ASSERT0(dn
->dn_newgid
);
221 ASSERT0(dn
->dn_newprojid
);
222 ASSERT0(dn
->dn_id_flags
);
224 ASSERT0(dn
->dn_dbufs_count
);
225 avl_destroy(&dn
->dn_dbufs
);
231 ASSERT(dnode_cache
== NULL
);
232 dnode_cache
= kmem_cache_create("dnode_t", sizeof (dnode_t
),
233 0, dnode_cons
, dnode_dest
, NULL
, NULL
, NULL
, 0);
234 kmem_cache_set_move(dnode_cache
, dnode_move
);
236 dnode_ksp
= kstat_create("zfs", 0, "dnodestats", "misc",
237 KSTAT_TYPE_NAMED
, sizeof (dnode_stats
) / sizeof (kstat_named_t
),
239 if (dnode_ksp
!= NULL
) {
240 dnode_ksp
->ks_data
= &dnode_stats
;
241 kstat_install(dnode_ksp
);
248 if (dnode_ksp
!= NULL
) {
249 kstat_delete(dnode_ksp
);
253 kmem_cache_destroy(dnode_cache
);
260 dnode_verify(dnode_t
*dn
)
262 int drop_struct_lock
= FALSE
;
265 ASSERT(dn
->dn_objset
);
266 ASSERT(dn
->dn_handle
->dnh_dnode
== dn
);
268 ASSERT(DMU_OT_IS_VALID(dn
->dn_phys
->dn_type
));
270 if (!(zfs_flags
& ZFS_DEBUG_DNODE_VERIFY
))
273 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
274 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
275 drop_struct_lock
= TRUE
;
277 if (dn
->dn_phys
->dn_type
!= DMU_OT_NONE
|| dn
->dn_allocated_txg
!= 0) {
279 int max_bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
280 ASSERT3U(dn
->dn_indblkshift
, <=, SPA_MAXBLOCKSHIFT
);
281 if (dn
->dn_datablkshift
) {
282 ASSERT3U(dn
->dn_datablkshift
, >=, SPA_MINBLOCKSHIFT
);
283 ASSERT3U(dn
->dn_datablkshift
, <=, SPA_MAXBLOCKSHIFT
);
284 ASSERT3U(1<<dn
->dn_datablkshift
, ==, dn
->dn_datablksz
);
286 ASSERT3U(dn
->dn_nlevels
, <=, 30);
287 ASSERT(DMU_OT_IS_VALID(dn
->dn_type
));
288 ASSERT3U(dn
->dn_nblkptr
, >=, 1);
289 ASSERT3U(dn
->dn_nblkptr
, <=, DN_MAX_NBLKPTR
);
290 ASSERT3U(dn
->dn_bonuslen
, <=, max_bonuslen
);
291 ASSERT3U(dn
->dn_datablksz
, ==,
292 dn
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
);
293 ASSERT3U(ISP2(dn
->dn_datablksz
), ==, dn
->dn_datablkshift
!= 0);
294 ASSERT3U((dn
->dn_nblkptr
- 1) * sizeof (blkptr_t
) +
295 dn
->dn_bonuslen
, <=, max_bonuslen
);
296 for (i
= 0; i
< TXG_SIZE
; i
++) {
297 ASSERT3U(dn
->dn_next_nlevels
[i
], <=, dn
->dn_nlevels
);
300 if (dn
->dn_phys
->dn_type
!= DMU_OT_NONE
)
301 ASSERT3U(dn
->dn_phys
->dn_nlevels
, <=, dn
->dn_nlevels
);
302 ASSERT(DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) || dn
->dn_dbuf
!= NULL
);
303 if (dn
->dn_dbuf
!= NULL
) {
304 ASSERT3P(dn
->dn_phys
, ==,
305 (dnode_phys_t
*)dn
->dn_dbuf
->db
.db_data
+
306 (dn
->dn_object
% (dn
->dn_dbuf
->db
.db_size
>> DNODE_SHIFT
)));
308 if (drop_struct_lock
)
309 rw_exit(&dn
->dn_struct_rwlock
);
314 dnode_byteswap(dnode_phys_t
*dnp
)
316 uint64_t *buf64
= (void*)&dnp
->dn_blkptr
;
319 if (dnp
->dn_type
== DMU_OT_NONE
) {
320 bzero(dnp
, sizeof (dnode_phys_t
));
324 dnp
->dn_datablkszsec
= BSWAP_16(dnp
->dn_datablkszsec
);
325 dnp
->dn_bonuslen
= BSWAP_16(dnp
->dn_bonuslen
);
326 dnp
->dn_extra_slots
= BSWAP_8(dnp
->dn_extra_slots
);
327 dnp
->dn_maxblkid
= BSWAP_64(dnp
->dn_maxblkid
);
328 dnp
->dn_used
= BSWAP_64(dnp
->dn_used
);
331 * dn_nblkptr is only one byte, so it's OK to read it in either
332 * byte order. We can't read dn_bouslen.
334 ASSERT(dnp
->dn_indblkshift
<= SPA_MAXBLOCKSHIFT
);
335 ASSERT(dnp
->dn_nblkptr
<= DN_MAX_NBLKPTR
);
336 for (i
= 0; i
< dnp
->dn_nblkptr
* sizeof (blkptr_t
)/8; i
++)
337 buf64
[i
] = BSWAP_64(buf64
[i
]);
340 * OK to check dn_bonuslen for zero, because it won't matter if
341 * we have the wrong byte order. This is necessary because the
342 * dnode dnode is smaller than a regular dnode.
344 if (dnp
->dn_bonuslen
!= 0) {
346 * Note that the bonus length calculated here may be
347 * longer than the actual bonus buffer. This is because
348 * we always put the bonus buffer after the last block
349 * pointer (instead of packing it against the end of the
352 int off
= (dnp
->dn_nblkptr
-1) * sizeof (blkptr_t
);
353 int slots
= dnp
->dn_extra_slots
+ 1;
354 size_t len
= DN_SLOTS_TO_BONUSLEN(slots
) - off
;
355 dmu_object_byteswap_t byteswap
;
356 ASSERT(DMU_OT_IS_VALID(dnp
->dn_bonustype
));
357 byteswap
= DMU_OT_BYTESWAP(dnp
->dn_bonustype
);
358 dmu_ot_byteswap
[byteswap
].ob_func(dnp
->dn_bonus
+ off
, len
);
361 /* Swap SPILL block if we have one */
362 if (dnp
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
)
363 byteswap_uint64_array(DN_SPILL_BLKPTR(dnp
), sizeof (blkptr_t
));
367 dnode_buf_byteswap(void *vbuf
, size_t size
)
371 ASSERT3U(sizeof (dnode_phys_t
), ==, (1<<DNODE_SHIFT
));
372 ASSERT((size
& (sizeof (dnode_phys_t
)-1)) == 0);
375 dnode_phys_t
*dnp
= (void *)(((char *)vbuf
) + i
);
379 if (dnp
->dn_type
!= DMU_OT_NONE
)
380 i
+= dnp
->dn_extra_slots
* DNODE_MIN_SIZE
;
385 dnode_setbonuslen(dnode_t
*dn
, int newsize
, dmu_tx_t
*tx
)
387 ASSERT3U(zfs_refcount_count(&dn
->dn_holds
), >=, 1);
389 dnode_setdirty(dn
, tx
);
390 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
391 ASSERT3U(newsize
, <=, DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
392 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
));
394 if (newsize
< dn
->dn_bonuslen
) {
395 /* clear any data after the end of the new size */
396 size_t diff
= dn
->dn_bonuslen
- newsize
;
397 char *data_end
= ((char *)dn
->dn_bonus
->db
.db_data
) + newsize
;
398 bzero(data_end
, diff
);
401 dn
->dn_bonuslen
= newsize
;
403 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = DN_ZERO_BONUSLEN
;
405 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonuslen
;
406 rw_exit(&dn
->dn_struct_rwlock
);
410 dnode_setbonus_type(dnode_t
*dn
, dmu_object_type_t newtype
, dmu_tx_t
*tx
)
412 ASSERT3U(zfs_refcount_count(&dn
->dn_holds
), >=, 1);
413 dnode_setdirty(dn
, tx
);
414 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
415 dn
->dn_bonustype
= newtype
;
416 dn
->dn_next_bonustype
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonustype
;
417 rw_exit(&dn
->dn_struct_rwlock
);
421 dnode_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
423 ASSERT3U(zfs_refcount_count(&dn
->dn_holds
), >=, 1);
424 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
425 dnode_setdirty(dn
, tx
);
426 dn
->dn_rm_spillblk
[tx
->tx_txg
& TXG_MASK
] = DN_KILL_SPILLBLK
;
427 dn
->dn_have_spill
= B_FALSE
;
431 dnode_setdblksz(dnode_t
*dn
, int size
)
433 ASSERT0(P2PHASE(size
, SPA_MINBLOCKSIZE
));
434 ASSERT3U(size
, <=, SPA_MAXBLOCKSIZE
);
435 ASSERT3U(size
, >=, SPA_MINBLOCKSIZE
);
436 ASSERT3U(size
>> SPA_MINBLOCKSHIFT
, <,
437 1<<(sizeof (dn
->dn_phys
->dn_datablkszsec
) * 8));
438 dn
->dn_datablksz
= size
;
439 dn
->dn_datablkszsec
= size
>> SPA_MINBLOCKSHIFT
;
440 dn
->dn_datablkshift
= ISP2(size
) ? highbit64(size
- 1) : 0;
444 dnode_create(objset_t
*os
, dnode_phys_t
*dnp
, dmu_buf_impl_t
*db
,
445 uint64_t object
, dnode_handle_t
*dnh
)
449 dn
= kmem_cache_alloc(dnode_cache
, KM_SLEEP
);
450 ASSERT(!POINTER_IS_VALID(dn
->dn_objset
));
454 * Defer setting dn_objset until the dnode is ready to be a candidate
455 * for the dnode_move() callback.
457 dn
->dn_object
= object
;
462 if (dnp
->dn_datablkszsec
) {
463 dnode_setdblksz(dn
, dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
);
465 dn
->dn_datablksz
= 0;
466 dn
->dn_datablkszsec
= 0;
467 dn
->dn_datablkshift
= 0;
469 dn
->dn_indblkshift
= dnp
->dn_indblkshift
;
470 dn
->dn_nlevels
= dnp
->dn_nlevels
;
471 dn
->dn_type
= dnp
->dn_type
;
472 dn
->dn_nblkptr
= dnp
->dn_nblkptr
;
473 dn
->dn_checksum
= dnp
->dn_checksum
;
474 dn
->dn_compress
= dnp
->dn_compress
;
475 dn
->dn_bonustype
= dnp
->dn_bonustype
;
476 dn
->dn_bonuslen
= dnp
->dn_bonuslen
;
477 dn
->dn_num_slots
= dnp
->dn_extra_slots
+ 1;
478 dn
->dn_maxblkid
= dnp
->dn_maxblkid
;
479 dn
->dn_have_spill
= ((dnp
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
) != 0);
482 dmu_zfetch_init(&dn
->dn_zfetch
, dn
);
484 ASSERT(DMU_OT_IS_VALID(dn
->dn_phys
->dn_type
));
485 ASSERT(zrl_is_locked(&dnh
->dnh_zrlock
));
486 ASSERT(!DN_SLOT_IS_PTR(dnh
->dnh_dnode
));
488 mutex_enter(&os
->os_lock
);
491 * Exclude special dnodes from os_dnodes so an empty os_dnodes
492 * signifies that the special dnodes have no references from
493 * their children (the entries in os_dnodes). This allows
494 * dnode_destroy() to easily determine if the last child has
495 * been removed and then complete eviction of the objset.
497 if (!DMU_OBJECT_IS_SPECIAL(object
))
498 list_insert_head(&os
->os_dnodes
, dn
);
502 * Everything else must be valid before assigning dn_objset
503 * makes the dnode eligible for dnode_move().
508 mutex_exit(&os
->os_lock
);
510 arc_space_consume(sizeof (dnode_t
), ARC_SPACE_DNODE
);
516 * Caller must be holding the dnode handle, which is released upon return.
519 dnode_destroy(dnode_t
*dn
)
521 objset_t
*os
= dn
->dn_objset
;
522 boolean_t complete_os_eviction
= B_FALSE
;
524 ASSERT((dn
->dn_id_flags
& DN_ID_NEW_EXIST
) == 0);
526 mutex_enter(&os
->os_lock
);
527 POINTER_INVALIDATE(&dn
->dn_objset
);
528 if (!DMU_OBJECT_IS_SPECIAL(dn
->dn_object
)) {
529 list_remove(&os
->os_dnodes
, dn
);
530 complete_os_eviction
=
531 list_is_empty(&os
->os_dnodes
) &&
532 list_link_active(&os
->os_evicting_node
);
534 mutex_exit(&os
->os_lock
);
536 /* the dnode can no longer move, so we can release the handle */
537 if (!zrl_is_locked(&dn
->dn_handle
->dnh_zrlock
))
538 zrl_remove(&dn
->dn_handle
->dnh_zrlock
);
540 dn
->dn_allocated_txg
= 0;
542 dn
->dn_assigned_txg
= 0;
543 dn
->dn_dirty_txg
= 0;
546 if (dn
->dn_dirtyctx_firstset
!= NULL
) {
547 kmem_free(dn
->dn_dirtyctx_firstset
, 1);
548 dn
->dn_dirtyctx_firstset
= NULL
;
550 if (dn
->dn_bonus
!= NULL
) {
551 mutex_enter(&dn
->dn_bonus
->db_mtx
);
552 dbuf_destroy(dn
->dn_bonus
);
557 dn
->dn_have_spill
= B_FALSE
;
562 dn
->dn_oldprojid
= ZFS_DEFAULT_PROJID
;
565 dn
->dn_newprojid
= ZFS_DEFAULT_PROJID
;
568 dmu_zfetch_fini(&dn
->dn_zfetch
);
569 kmem_cache_free(dnode_cache
, dn
);
570 arc_space_return(sizeof (dnode_t
), ARC_SPACE_DNODE
);
572 if (complete_os_eviction
)
573 dmu_objset_evict_done(os
);
577 dnode_allocate(dnode_t
*dn
, dmu_object_type_t ot
, int blocksize
, int ibs
,
578 dmu_object_type_t bonustype
, int bonuslen
, int dn_slots
, dmu_tx_t
*tx
)
582 ASSERT3U(dn_slots
, >, 0);
583 ASSERT3U(dn_slots
<< DNODE_SHIFT
, <=,
584 spa_maxdnodesize(dmu_objset_spa(dn
->dn_objset
)));
585 ASSERT3U(blocksize
, <=,
586 spa_maxblocksize(dmu_objset_spa(dn
->dn_objset
)));
588 blocksize
= 1 << zfs_default_bs
;
590 blocksize
= P2ROUNDUP(blocksize
, SPA_MINBLOCKSIZE
);
593 ibs
= zfs_default_ibs
;
595 ibs
= MIN(MAX(ibs
, DN_MIN_INDBLKSHIFT
), DN_MAX_INDBLKSHIFT
);
597 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d dn_slots=%d\n",
598 dn
->dn_objset
, dn
->dn_object
, tx
->tx_txg
, blocksize
, ibs
, dn_slots
);
599 DNODE_STAT_BUMP(dnode_allocate
);
601 ASSERT(dn
->dn_type
== DMU_OT_NONE
);
602 ASSERT(bcmp(dn
->dn_phys
, &dnode_phys_zero
, sizeof (dnode_phys_t
)) == 0);
603 ASSERT(dn
->dn_phys
->dn_type
== DMU_OT_NONE
);
604 ASSERT(ot
!= DMU_OT_NONE
);
605 ASSERT(DMU_OT_IS_VALID(ot
));
606 ASSERT((bonustype
== DMU_OT_NONE
&& bonuslen
== 0) ||
607 (bonustype
== DMU_OT_SA
&& bonuslen
== 0) ||
608 (bonustype
!= DMU_OT_NONE
&& bonuslen
!= 0));
609 ASSERT(DMU_OT_IS_VALID(bonustype
));
610 ASSERT3U(bonuslen
, <=, DN_SLOTS_TO_BONUSLEN(dn_slots
));
611 ASSERT(dn
->dn_type
== DMU_OT_NONE
);
612 ASSERT0(dn
->dn_maxblkid
);
613 ASSERT0(dn
->dn_allocated_txg
);
614 ASSERT0(dn
->dn_assigned_txg
);
615 ASSERT0(dn
->dn_dirty_txg
);
616 ASSERT(zfs_refcount_is_zero(&dn
->dn_tx_holds
));
617 ASSERT3U(zfs_refcount_count(&dn
->dn_holds
), <=, 1);
618 ASSERT(avl_is_empty(&dn
->dn_dbufs
));
620 for (i
= 0; i
< TXG_SIZE
; i
++) {
621 ASSERT0(dn
->dn_next_nblkptr
[i
]);
622 ASSERT0(dn
->dn_next_nlevels
[i
]);
623 ASSERT0(dn
->dn_next_indblkshift
[i
]);
624 ASSERT0(dn
->dn_next_bonuslen
[i
]);
625 ASSERT0(dn
->dn_next_bonustype
[i
]);
626 ASSERT0(dn
->dn_rm_spillblk
[i
]);
627 ASSERT0(dn
->dn_next_blksz
[i
]);
628 ASSERT0(dn
->dn_next_maxblkid
[i
]);
629 ASSERT(!multilist_link_active(&dn
->dn_dirty_link
[i
]));
630 ASSERT3P(list_head(&dn
->dn_dirty_records
[i
]), ==, NULL
);
631 ASSERT3P(dn
->dn_free_ranges
[i
], ==, NULL
);
635 dnode_setdblksz(dn
, blocksize
);
636 dn
->dn_indblkshift
= ibs
;
638 dn
->dn_num_slots
= dn_slots
;
639 if (bonustype
== DMU_OT_SA
) /* Maximize bonus space for SA */
642 dn
->dn_nblkptr
= MIN(DN_MAX_NBLKPTR
,
643 1 + ((DN_SLOTS_TO_BONUSLEN(dn_slots
) - bonuslen
) >>
647 dn
->dn_bonustype
= bonustype
;
648 dn
->dn_bonuslen
= bonuslen
;
649 dn
->dn_checksum
= ZIO_CHECKSUM_INHERIT
;
650 dn
->dn_compress
= ZIO_COMPRESS_INHERIT
;
654 if (dn
->dn_dirtyctx_firstset
) {
655 kmem_free(dn
->dn_dirtyctx_firstset
, 1);
656 dn
->dn_dirtyctx_firstset
= NULL
;
659 dn
->dn_allocated_txg
= tx
->tx_txg
;
662 dnode_setdirty(dn
, tx
);
663 dn
->dn_next_indblkshift
[tx
->tx_txg
& TXG_MASK
] = ibs
;
664 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonuslen
;
665 dn
->dn_next_bonustype
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonustype
;
666 dn
->dn_next_blksz
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_datablksz
;
670 dnode_reallocate(dnode_t
*dn
, dmu_object_type_t ot
, int blocksize
,
671 dmu_object_type_t bonustype
, int bonuslen
, int dn_slots
,
672 boolean_t keep_spill
, dmu_tx_t
*tx
)
676 ASSERT3U(blocksize
, >=, SPA_MINBLOCKSIZE
);
677 ASSERT3U(blocksize
, <=,
678 spa_maxblocksize(dmu_objset_spa(dn
->dn_objset
)));
679 ASSERT0(blocksize
% SPA_MINBLOCKSIZE
);
680 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
|| dmu_tx_private_ok(tx
));
681 ASSERT(tx
->tx_txg
!= 0);
682 ASSERT((bonustype
== DMU_OT_NONE
&& bonuslen
== 0) ||
683 (bonustype
!= DMU_OT_NONE
&& bonuslen
!= 0) ||
684 (bonustype
== DMU_OT_SA
&& bonuslen
== 0));
685 ASSERT(DMU_OT_IS_VALID(bonustype
));
686 ASSERT3U(bonuslen
, <=,
687 DN_BONUS_SIZE(spa_maxdnodesize(dmu_objset_spa(dn
->dn_objset
))));
688 ASSERT3U(bonuslen
, <=, DN_BONUS_SIZE(dn_slots
<< DNODE_SHIFT
));
690 dnode_free_interior_slots(dn
);
691 DNODE_STAT_BUMP(dnode_reallocate
);
693 /* clean up any unreferenced dbufs */
694 dnode_evict_dbufs(dn
);
698 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
699 dnode_setdirty(dn
, tx
);
700 if (dn
->dn_datablksz
!= blocksize
) {
701 /* change blocksize */
702 ASSERT0(dn
->dn_maxblkid
);
703 ASSERT(BP_IS_HOLE(&dn
->dn_phys
->dn_blkptr
[0]) ||
704 dnode_block_freed(dn
, 0));
706 dnode_setdblksz(dn
, blocksize
);
707 dn
->dn_next_blksz
[tx
->tx_txg
& TXG_MASK
] = blocksize
;
709 if (dn
->dn_bonuslen
!= bonuslen
)
710 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = bonuslen
;
712 if (bonustype
== DMU_OT_SA
) /* Maximize bonus space for SA */
715 nblkptr
= MIN(DN_MAX_NBLKPTR
,
716 1 + ((DN_SLOTS_TO_BONUSLEN(dn_slots
) - bonuslen
) >>
718 if (dn
->dn_bonustype
!= bonustype
)
719 dn
->dn_next_bonustype
[tx
->tx_txg
& TXG_MASK
] = bonustype
;
720 if (dn
->dn_nblkptr
!= nblkptr
)
721 dn
->dn_next_nblkptr
[tx
->tx_txg
& TXG_MASK
] = nblkptr
;
722 if (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
&& !keep_spill
) {
723 dbuf_rm_spill(dn
, tx
);
724 dnode_rm_spill(dn
, tx
);
727 rw_exit(&dn
->dn_struct_rwlock
);
732 /* change bonus size and type */
733 mutex_enter(&dn
->dn_mtx
);
734 dn
->dn_bonustype
= bonustype
;
735 dn
->dn_bonuslen
= bonuslen
;
736 dn
->dn_num_slots
= dn_slots
;
737 dn
->dn_nblkptr
= nblkptr
;
738 dn
->dn_checksum
= ZIO_CHECKSUM_INHERIT
;
739 dn
->dn_compress
= ZIO_COMPRESS_INHERIT
;
740 ASSERT3U(dn
->dn_nblkptr
, <=, DN_MAX_NBLKPTR
);
742 /* fix up the bonus db_size */
744 dn
->dn_bonus
->db
.db_size
=
745 DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
746 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
747 ASSERT(dn
->dn_bonuslen
<= dn
->dn_bonus
->db
.db_size
);
750 dn
->dn_allocated_txg
= tx
->tx_txg
;
751 mutex_exit(&dn
->dn_mtx
);
756 dnode_move_impl(dnode_t
*odn
, dnode_t
*ndn
)
760 ASSERT(!RW_LOCK_HELD(&odn
->dn_struct_rwlock
));
761 ASSERT(MUTEX_NOT_HELD(&odn
->dn_mtx
));
762 ASSERT(MUTEX_NOT_HELD(&odn
->dn_dbufs_mtx
));
763 ASSERT(!MUTEX_HELD(&odn
->dn_zfetch
.zf_lock
));
766 ndn
->dn_objset
= odn
->dn_objset
;
767 ndn
->dn_object
= odn
->dn_object
;
768 ndn
->dn_dbuf
= odn
->dn_dbuf
;
769 ndn
->dn_handle
= odn
->dn_handle
;
770 ndn
->dn_phys
= odn
->dn_phys
;
771 ndn
->dn_type
= odn
->dn_type
;
772 ndn
->dn_bonuslen
= odn
->dn_bonuslen
;
773 ndn
->dn_bonustype
= odn
->dn_bonustype
;
774 ndn
->dn_nblkptr
= odn
->dn_nblkptr
;
775 ndn
->dn_checksum
= odn
->dn_checksum
;
776 ndn
->dn_compress
= odn
->dn_compress
;
777 ndn
->dn_nlevels
= odn
->dn_nlevels
;
778 ndn
->dn_indblkshift
= odn
->dn_indblkshift
;
779 ndn
->dn_datablkshift
= odn
->dn_datablkshift
;
780 ndn
->dn_datablkszsec
= odn
->dn_datablkszsec
;
781 ndn
->dn_datablksz
= odn
->dn_datablksz
;
782 ndn
->dn_maxblkid
= odn
->dn_maxblkid
;
783 ndn
->dn_num_slots
= odn
->dn_num_slots
;
784 bcopy(&odn
->dn_next_type
[0], &ndn
->dn_next_type
[0],
785 sizeof (odn
->dn_next_type
));
786 bcopy(&odn
->dn_next_nblkptr
[0], &ndn
->dn_next_nblkptr
[0],
787 sizeof (odn
->dn_next_nblkptr
));
788 bcopy(&odn
->dn_next_nlevels
[0], &ndn
->dn_next_nlevels
[0],
789 sizeof (odn
->dn_next_nlevels
));
790 bcopy(&odn
->dn_next_indblkshift
[0], &ndn
->dn_next_indblkshift
[0],
791 sizeof (odn
->dn_next_indblkshift
));
792 bcopy(&odn
->dn_next_bonustype
[0], &ndn
->dn_next_bonustype
[0],
793 sizeof (odn
->dn_next_bonustype
));
794 bcopy(&odn
->dn_rm_spillblk
[0], &ndn
->dn_rm_spillblk
[0],
795 sizeof (odn
->dn_rm_spillblk
));
796 bcopy(&odn
->dn_next_bonuslen
[0], &ndn
->dn_next_bonuslen
[0],
797 sizeof (odn
->dn_next_bonuslen
));
798 bcopy(&odn
->dn_next_blksz
[0], &ndn
->dn_next_blksz
[0],
799 sizeof (odn
->dn_next_blksz
));
800 bcopy(&odn
->dn_next_maxblkid
[0], &ndn
->dn_next_maxblkid
[0],
801 sizeof (odn
->dn_next_maxblkid
));
802 for (i
= 0; i
< TXG_SIZE
; i
++) {
803 list_move_tail(&ndn
->dn_dirty_records
[i
],
804 &odn
->dn_dirty_records
[i
]);
806 bcopy(&odn
->dn_free_ranges
[0], &ndn
->dn_free_ranges
[0],
807 sizeof (odn
->dn_free_ranges
));
808 ndn
->dn_allocated_txg
= odn
->dn_allocated_txg
;
809 ndn
->dn_free_txg
= odn
->dn_free_txg
;
810 ndn
->dn_assigned_txg
= odn
->dn_assigned_txg
;
811 ndn
->dn_dirty_txg
= odn
->dn_dirty_txg
;
812 ndn
->dn_dirtyctx
= odn
->dn_dirtyctx
;
813 ndn
->dn_dirtyctx_firstset
= odn
->dn_dirtyctx_firstset
;
814 ASSERT(zfs_refcount_count(&odn
->dn_tx_holds
) == 0);
815 zfs_refcount_transfer(&ndn
->dn_holds
, &odn
->dn_holds
);
816 ASSERT(avl_is_empty(&ndn
->dn_dbufs
));
817 avl_swap(&ndn
->dn_dbufs
, &odn
->dn_dbufs
);
818 ndn
->dn_dbufs_count
= odn
->dn_dbufs_count
;
819 ndn
->dn_bonus
= odn
->dn_bonus
;
820 ndn
->dn_have_spill
= odn
->dn_have_spill
;
821 ndn
->dn_zio
= odn
->dn_zio
;
822 ndn
->dn_oldused
= odn
->dn_oldused
;
823 ndn
->dn_oldflags
= odn
->dn_oldflags
;
824 ndn
->dn_olduid
= odn
->dn_olduid
;
825 ndn
->dn_oldgid
= odn
->dn_oldgid
;
826 ndn
->dn_oldprojid
= odn
->dn_oldprojid
;
827 ndn
->dn_newuid
= odn
->dn_newuid
;
828 ndn
->dn_newgid
= odn
->dn_newgid
;
829 ndn
->dn_newprojid
= odn
->dn_newprojid
;
830 ndn
->dn_id_flags
= odn
->dn_id_flags
;
831 dmu_zfetch_init(&ndn
->dn_zfetch
, NULL
);
832 list_move_tail(&ndn
->dn_zfetch
.zf_stream
, &odn
->dn_zfetch
.zf_stream
);
833 ndn
->dn_zfetch
.zf_dnode
= odn
->dn_zfetch
.zf_dnode
;
836 * Update back pointers. Updating the handle fixes the back pointer of
837 * every descendant dbuf as well as the bonus dbuf.
839 ASSERT(ndn
->dn_handle
->dnh_dnode
== odn
);
840 ndn
->dn_handle
->dnh_dnode
= ndn
;
841 if (ndn
->dn_zfetch
.zf_dnode
== odn
) {
842 ndn
->dn_zfetch
.zf_dnode
= ndn
;
846 * Invalidate the original dnode by clearing all of its back pointers.
849 odn
->dn_handle
= NULL
;
850 avl_create(&odn
->dn_dbufs
, dbuf_compare
, sizeof (dmu_buf_impl_t
),
851 offsetof(dmu_buf_impl_t
, db_link
));
852 odn
->dn_dbufs_count
= 0;
853 odn
->dn_bonus
= NULL
;
854 dmu_zfetch_fini(&odn
->dn_zfetch
);
857 * Set the low bit of the objset pointer to ensure that dnode_move()
858 * recognizes the dnode as invalid in any subsequent callback.
860 POINTER_INVALIDATE(&odn
->dn_objset
);
863 * Satisfy the destructor.
865 for (i
= 0; i
< TXG_SIZE
; i
++) {
866 list_create(&odn
->dn_dirty_records
[i
],
867 sizeof (dbuf_dirty_record_t
),
868 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
869 odn
->dn_free_ranges
[i
] = NULL
;
870 odn
->dn_next_nlevels
[i
] = 0;
871 odn
->dn_next_indblkshift
[i
] = 0;
872 odn
->dn_next_bonustype
[i
] = 0;
873 odn
->dn_rm_spillblk
[i
] = 0;
874 odn
->dn_next_bonuslen
[i
] = 0;
875 odn
->dn_next_blksz
[i
] = 0;
877 odn
->dn_allocated_txg
= 0;
878 odn
->dn_free_txg
= 0;
879 odn
->dn_assigned_txg
= 0;
880 odn
->dn_dirty_txg
= 0;
881 odn
->dn_dirtyctx
= 0;
882 odn
->dn_dirtyctx_firstset
= NULL
;
883 odn
->dn_have_spill
= B_FALSE
;
886 odn
->dn_oldflags
= 0;
889 odn
->dn_oldprojid
= ZFS_DEFAULT_PROJID
;
892 odn
->dn_newprojid
= ZFS_DEFAULT_PROJID
;
893 odn
->dn_id_flags
= 0;
899 odn
->dn_moved
= (uint8_t)-1;
904 dnode_move(void *buf
, void *newbuf
, size_t size
, void *arg
)
906 dnode_t
*odn
= buf
, *ndn
= newbuf
;
912 * The dnode is on the objset's list of known dnodes if the objset
913 * pointer is valid. We set the low bit of the objset pointer when
914 * freeing the dnode to invalidate it, and the memory patterns written
915 * by kmem (baddcafe and deadbeef) set at least one of the two low bits.
916 * A newly created dnode sets the objset pointer last of all to indicate
917 * that the dnode is known and in a valid state to be moved by this
921 if (!POINTER_IS_VALID(os
)) {
922 DNODE_STAT_BUMP(dnode_move_invalid
);
923 return (KMEM_CBRC_DONT_KNOW
);
927 * Ensure that the objset does not go away during the move.
929 rw_enter(&os_lock
, RW_WRITER
);
930 if (os
!= odn
->dn_objset
) {
932 DNODE_STAT_BUMP(dnode_move_recheck1
);
933 return (KMEM_CBRC_DONT_KNOW
);
937 * If the dnode is still valid, then so is the objset. We know that no
938 * valid objset can be freed while we hold os_lock, so we can safely
939 * ensure that the objset remains in use.
941 mutex_enter(&os
->os_lock
);
944 * Recheck the objset pointer in case the dnode was removed just before
945 * acquiring the lock.
947 if (os
!= odn
->dn_objset
) {
948 mutex_exit(&os
->os_lock
);
950 DNODE_STAT_BUMP(dnode_move_recheck2
);
951 return (KMEM_CBRC_DONT_KNOW
);
955 * At this point we know that as long as we hold os->os_lock, the dnode
956 * cannot be freed and fields within the dnode can be safely accessed.
957 * The objset listing this dnode cannot go away as long as this dnode is
961 if (DMU_OBJECT_IS_SPECIAL(odn
->dn_object
)) {
962 mutex_exit(&os
->os_lock
);
963 DNODE_STAT_BUMP(dnode_move_special
);
964 return (KMEM_CBRC_NO
);
966 ASSERT(odn
->dn_dbuf
!= NULL
); /* only "special" dnodes have no parent */
969 * Lock the dnode handle to prevent the dnode from obtaining any new
970 * holds. This also prevents the descendant dbufs and the bonus dbuf
971 * from accessing the dnode, so that we can discount their holds. The
972 * handle is safe to access because we know that while the dnode cannot
973 * go away, neither can its handle. Once we hold dnh_zrlock, we can
974 * safely move any dnode referenced only by dbufs.
976 if (!zrl_tryenter(&odn
->dn_handle
->dnh_zrlock
)) {
977 mutex_exit(&os
->os_lock
);
978 DNODE_STAT_BUMP(dnode_move_handle
);
979 return (KMEM_CBRC_LATER
);
983 * Ensure a consistent view of the dnode's holds and the dnode's dbufs.
984 * We need to guarantee that there is a hold for every dbuf in order to
985 * determine whether the dnode is actively referenced. Falsely matching
986 * a dbuf to an active hold would lead to an unsafe move. It's possible
987 * that a thread already having an active dnode hold is about to add a
988 * dbuf, and we can't compare hold and dbuf counts while the add is in
991 if (!rw_tryenter(&odn
->dn_struct_rwlock
, RW_WRITER
)) {
992 zrl_exit(&odn
->dn_handle
->dnh_zrlock
);
993 mutex_exit(&os
->os_lock
);
994 DNODE_STAT_BUMP(dnode_move_rwlock
);
995 return (KMEM_CBRC_LATER
);
999 * A dbuf may be removed (evicted) without an active dnode hold. In that
1000 * case, the dbuf count is decremented under the handle lock before the
1001 * dbuf's hold is released. This order ensures that if we count the hold
1002 * after the dbuf is removed but before its hold is released, we will
1003 * treat the unmatched hold as active and exit safely. If we count the
1004 * hold before the dbuf is removed, the hold is discounted, and the
1005 * removal is blocked until the move completes.
1007 refcount
= zfs_refcount_count(&odn
->dn_holds
);
1008 ASSERT(refcount
>= 0);
1009 dbufs
= odn
->dn_dbufs_count
;
1011 /* We can't have more dbufs than dnode holds. */
1012 ASSERT3U(dbufs
, <=, refcount
);
1013 DTRACE_PROBE3(dnode__move
, dnode_t
*, odn
, int64_t, refcount
,
1016 if (refcount
> dbufs
) {
1017 rw_exit(&odn
->dn_struct_rwlock
);
1018 zrl_exit(&odn
->dn_handle
->dnh_zrlock
);
1019 mutex_exit(&os
->os_lock
);
1020 DNODE_STAT_BUMP(dnode_move_active
);
1021 return (KMEM_CBRC_LATER
);
1024 rw_exit(&odn
->dn_struct_rwlock
);
1027 * At this point we know that anyone with a hold on the dnode is not
1028 * actively referencing it. The dnode is known and in a valid state to
1029 * move. We're holding the locks needed to execute the critical section.
1031 dnode_move_impl(odn
, ndn
);
1033 list_link_replace(&odn
->dn_link
, &ndn
->dn_link
);
1034 /* If the dnode was safe to move, the refcount cannot have changed. */
1035 ASSERT(refcount
== zfs_refcount_count(&ndn
->dn_holds
));
1036 ASSERT(dbufs
== ndn
->dn_dbufs_count
);
1037 zrl_exit(&ndn
->dn_handle
->dnh_zrlock
); /* handle has moved */
1038 mutex_exit(&os
->os_lock
);
1040 return (KMEM_CBRC_YES
);
1042 #endif /* _KERNEL */
1045 dnode_slots_hold(dnode_children_t
*children
, int idx
, int slots
)
1047 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1049 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1050 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1051 zrl_add(&dnh
->dnh_zrlock
);
1056 dnode_slots_rele(dnode_children_t
*children
, int idx
, int slots
)
1058 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1060 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1061 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1063 if (zrl_is_locked(&dnh
->dnh_zrlock
))
1064 zrl_exit(&dnh
->dnh_zrlock
);
1066 zrl_remove(&dnh
->dnh_zrlock
);
1071 dnode_slots_tryenter(dnode_children_t
*children
, int idx
, int slots
)
1073 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1075 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1076 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1078 if (!zrl_tryenter(&dnh
->dnh_zrlock
)) {
1079 for (int j
= idx
; j
< i
; j
++) {
1080 dnh
= &children
->dnc_children
[j
];
1081 zrl_exit(&dnh
->dnh_zrlock
);
1092 dnode_set_slots(dnode_children_t
*children
, int idx
, int slots
, void *ptr
)
1094 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1096 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1097 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1098 dnh
->dnh_dnode
= ptr
;
1103 dnode_check_slots_free(dnode_children_t
*children
, int idx
, int slots
)
1105 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1108 * If all dnode slots are either already free or
1109 * evictable return B_TRUE.
1111 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1112 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1113 dnode_t
*dn
= dnh
->dnh_dnode
;
1115 if (dn
== DN_SLOT_FREE
) {
1117 } else if (DN_SLOT_IS_PTR(dn
)) {
1118 mutex_enter(&dn
->dn_mtx
);
1119 boolean_t can_free
= (dn
->dn_type
== DMU_OT_NONE
&&
1120 zfs_refcount_is_zero(&dn
->dn_holds
) &&
1121 !DNODE_IS_DIRTY(dn
));
1122 mutex_exit(&dn
->dn_mtx
);
1137 dnode_reclaim_slots(dnode_children_t
*children
, int idx
, int slots
)
1139 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1141 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1142 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1144 ASSERT(zrl_is_locked(&dnh
->dnh_zrlock
));
1146 if (DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1147 ASSERT3S(dnh
->dnh_dnode
->dn_type
, ==, DMU_OT_NONE
);
1148 dnode_destroy(dnh
->dnh_dnode
);
1149 dnh
->dnh_dnode
= DN_SLOT_FREE
;
1155 dnode_free_interior_slots(dnode_t
*dn
)
1157 dnode_children_t
*children
= dmu_buf_get_user(&dn
->dn_dbuf
->db
);
1158 int epb
= dn
->dn_dbuf
->db
.db_size
>> DNODE_SHIFT
;
1159 int idx
= (dn
->dn_object
& (epb
- 1)) + 1;
1160 int slots
= dn
->dn_num_slots
- 1;
1165 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1167 while (!dnode_slots_tryenter(children
, idx
, slots
)) {
1168 DNODE_STAT_BUMP(dnode_free_interior_lock_retry
);
1172 dnode_set_slots(children
, idx
, slots
, DN_SLOT_FREE
);
1173 dnode_slots_rele(children
, idx
, slots
);
1177 dnode_special_close(dnode_handle_t
*dnh
)
1179 dnode_t
*dn
= dnh
->dnh_dnode
;
1182 * Wait for final references to the dnode to clear. This can
1183 * only happen if the arc is asynchronously evicting state that
1184 * has a hold on this dnode while we are trying to evict this
1187 while (zfs_refcount_count(&dn
->dn_holds
) > 0)
1189 ASSERT(dn
->dn_dbuf
== NULL
||
1190 dmu_buf_get_user(&dn
->dn_dbuf
->db
) == NULL
);
1191 zrl_add(&dnh
->dnh_zrlock
);
1192 dnode_destroy(dn
); /* implicit zrl_remove() */
1193 zrl_destroy(&dnh
->dnh_zrlock
);
1194 dnh
->dnh_dnode
= NULL
;
1198 dnode_special_open(objset_t
*os
, dnode_phys_t
*dnp
, uint64_t object
,
1199 dnode_handle_t
*dnh
)
1203 zrl_init(&dnh
->dnh_zrlock
);
1204 zrl_tryenter(&dnh
->dnh_zrlock
);
1206 dn
= dnode_create(os
, dnp
, NULL
, object
, dnh
);
1209 zrl_exit(&dnh
->dnh_zrlock
);
1213 dnode_buf_evict_async(void *dbu
)
1215 dnode_children_t
*dnc
= dbu
;
1217 DNODE_STAT_BUMP(dnode_buf_evict
);
1219 for (int i
= 0; i
< dnc
->dnc_count
; i
++) {
1220 dnode_handle_t
*dnh
= &dnc
->dnc_children
[i
];
1224 * The dnode handle lock guards against the dnode moving to
1225 * another valid address, so there is no need here to guard
1226 * against changes to or from NULL.
1228 if (!DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1229 zrl_destroy(&dnh
->dnh_zrlock
);
1230 dnh
->dnh_dnode
= DN_SLOT_UNINIT
;
1234 zrl_add(&dnh
->dnh_zrlock
);
1235 dn
= dnh
->dnh_dnode
;
1237 * If there are holds on this dnode, then there should
1238 * be holds on the dnode's containing dbuf as well; thus
1239 * it wouldn't be eligible for eviction and this function
1240 * would not have been called.
1242 ASSERT(zfs_refcount_is_zero(&dn
->dn_holds
));
1243 ASSERT(zfs_refcount_is_zero(&dn
->dn_tx_holds
));
1245 dnode_destroy(dn
); /* implicit zrl_remove() for first slot */
1246 zrl_destroy(&dnh
->dnh_zrlock
);
1247 dnh
->dnh_dnode
= DN_SLOT_UNINIT
;
1249 kmem_free(dnc
, sizeof (dnode_children_t
) +
1250 dnc
->dnc_count
* sizeof (dnode_handle_t
));
1254 * When the DNODE_MUST_BE_FREE flag is set, the "slots" parameter is used
1255 * to ensure the hole at the specified object offset is large enough to
1256 * hold the dnode being created. The slots parameter is also used to ensure
1257 * a dnode does not span multiple dnode blocks. In both of these cases, if
1258 * a failure occurs, ENOSPC is returned. Keep in mind, these failure cases
1259 * are only possible when using DNODE_MUST_BE_FREE.
1261 * If the DNODE_MUST_BE_ALLOCATED flag is set, "slots" must be 0.
1262 * dnode_hold_impl() will check if the requested dnode is already consumed
1263 * as an extra dnode slot by an large dnode, in which case it returns
1267 * EINVAL - Invalid object number or flags.
1268 * ENOSPC - Hole too small to fulfill "slots" request (DNODE_MUST_BE_FREE)
1269 * EEXIST - Refers to an allocated dnode (DNODE_MUST_BE_FREE)
1270 * - Refers to a freeing dnode (DNODE_MUST_BE_FREE)
1271 * - Refers to an interior dnode slot (DNODE_MUST_BE_ALLOCATED)
1272 * ENOENT - The requested dnode is not allocated (DNODE_MUST_BE_ALLOCATED)
1273 * - The requested dnode is being freed (DNODE_MUST_BE_ALLOCATED)
1274 * EIO - I/O error when reading the meta dnode dbuf.
1276 * succeeds even for free dnodes.
1279 dnode_hold_impl(objset_t
*os
, uint64_t object
, int flag
, int slots
,
1280 void *tag
, dnode_t
**dnp
)
1283 int drop_struct_lock
= FALSE
;
1288 dnode_children_t
*dnc
;
1289 dnode_phys_t
*dn_block
;
1290 dnode_handle_t
*dnh
;
1292 ASSERT(!(flag
& DNODE_MUST_BE_ALLOCATED
) || (slots
== 0));
1293 ASSERT(!(flag
& DNODE_MUST_BE_FREE
) || (slots
> 0));
1296 * If you are holding the spa config lock as writer, you shouldn't
1297 * be asking the DMU to do *anything* unless it's the root pool
1298 * which may require us to read from the root filesystem while
1299 * holding some (not all) of the locks as writer.
1301 ASSERT(spa_config_held(os
->os_spa
, SCL_ALL
, RW_WRITER
) == 0 ||
1302 (spa_is_root(os
->os_spa
) &&
1303 spa_config_held(os
->os_spa
, SCL_STATE
, RW_WRITER
)));
1305 ASSERT((flag
& DNODE_MUST_BE_ALLOCATED
) || (flag
& DNODE_MUST_BE_FREE
));
1307 if (object
== DMU_USERUSED_OBJECT
|| object
== DMU_GROUPUSED_OBJECT
||
1308 object
== DMU_PROJECTUSED_OBJECT
) {
1309 if (object
== DMU_USERUSED_OBJECT
)
1310 dn
= DMU_USERUSED_DNODE(os
);
1311 else if (object
== DMU_GROUPUSED_OBJECT
)
1312 dn
= DMU_GROUPUSED_DNODE(os
);
1314 dn
= DMU_PROJECTUSED_DNODE(os
);
1316 return (SET_ERROR(ENOENT
));
1318 if ((flag
& DNODE_MUST_BE_ALLOCATED
) && type
== DMU_OT_NONE
)
1319 return (SET_ERROR(ENOENT
));
1320 if ((flag
& DNODE_MUST_BE_FREE
) && type
!= DMU_OT_NONE
)
1321 return (SET_ERROR(EEXIST
));
1323 (void) zfs_refcount_add(&dn
->dn_holds
, tag
);
1328 if (object
== 0 || object
>= DN_MAX_OBJECT
)
1329 return (SET_ERROR(EINVAL
));
1331 mdn
= DMU_META_DNODE(os
);
1332 ASSERT(mdn
->dn_object
== DMU_META_DNODE_OBJECT
);
1336 if (!RW_WRITE_HELD(&mdn
->dn_struct_rwlock
)) {
1337 rw_enter(&mdn
->dn_struct_rwlock
, RW_READER
);
1338 drop_struct_lock
= TRUE
;
1341 blk
= dbuf_whichblock(mdn
, 0, object
* sizeof (dnode_phys_t
));
1342 db
= dbuf_hold(mdn
, blk
, FTAG
);
1343 if (drop_struct_lock
)
1344 rw_exit(&mdn
->dn_struct_rwlock
);
1346 DNODE_STAT_BUMP(dnode_hold_dbuf_hold
);
1347 return (SET_ERROR(EIO
));
1351 * We do not need to decrypt to read the dnode so it doesn't matter
1352 * if we get the encrypted or decrypted version.
1354 err
= dbuf_read(db
, NULL
, DB_RF_CANFAIL
| DB_RF_NO_DECRYPT
);
1356 DNODE_STAT_BUMP(dnode_hold_dbuf_read
);
1357 dbuf_rele(db
, FTAG
);
1361 ASSERT3U(db
->db
.db_size
, >=, 1<<DNODE_SHIFT
);
1362 epb
= db
->db
.db_size
>> DNODE_SHIFT
;
1364 idx
= object
& (epb
- 1);
1365 dn_block
= (dnode_phys_t
*)db
->db
.db_data
;
1367 ASSERT(DB_DNODE(db
)->dn_type
== DMU_OT_DNODE
);
1368 dnc
= dmu_buf_get_user(&db
->db
);
1371 dnode_children_t
*winner
;
1374 dnc
= kmem_zalloc(sizeof (dnode_children_t
) +
1375 epb
* sizeof (dnode_handle_t
), KM_SLEEP
);
1376 dnc
->dnc_count
= epb
;
1377 dnh
= &dnc
->dnc_children
[0];
1379 /* Initialize dnode slot status from dnode_phys_t */
1380 for (int i
= 0; i
< epb
; i
++) {
1381 zrl_init(&dnh
[i
].dnh_zrlock
);
1388 if (dn_block
[i
].dn_type
!= DMU_OT_NONE
) {
1389 int interior
= dn_block
[i
].dn_extra_slots
;
1391 dnode_set_slots(dnc
, i
, 1, DN_SLOT_ALLOCATED
);
1392 dnode_set_slots(dnc
, i
+ 1, interior
,
1396 dnh
[i
].dnh_dnode
= DN_SLOT_FREE
;
1401 dmu_buf_init_user(&dnc
->dnc_dbu
, NULL
,
1402 dnode_buf_evict_async
, NULL
);
1403 winner
= dmu_buf_set_user(&db
->db
, &dnc
->dnc_dbu
);
1404 if (winner
!= NULL
) {
1406 for (int i
= 0; i
< epb
; i
++)
1407 zrl_destroy(&dnh
[i
].dnh_zrlock
);
1409 kmem_free(dnc
, sizeof (dnode_children_t
) +
1410 epb
* sizeof (dnode_handle_t
));
1415 ASSERT(dnc
->dnc_count
== epb
);
1417 if (flag
& DNODE_MUST_BE_ALLOCATED
) {
1420 dnode_slots_hold(dnc
, idx
, slots
);
1421 dnh
= &dnc
->dnc_children
[idx
];
1423 if (DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1424 dn
= dnh
->dnh_dnode
;
1425 } else if (dnh
->dnh_dnode
== DN_SLOT_INTERIOR
) {
1426 DNODE_STAT_BUMP(dnode_hold_alloc_interior
);
1427 dnode_slots_rele(dnc
, idx
, slots
);
1428 dbuf_rele(db
, FTAG
);
1429 return (SET_ERROR(EEXIST
));
1430 } else if (dnh
->dnh_dnode
!= DN_SLOT_ALLOCATED
) {
1431 DNODE_STAT_BUMP(dnode_hold_alloc_misses
);
1432 dnode_slots_rele(dnc
, idx
, slots
);
1433 dbuf_rele(db
, FTAG
);
1434 return (SET_ERROR(ENOENT
));
1436 dnode_slots_rele(dnc
, idx
, slots
);
1437 while (!dnode_slots_tryenter(dnc
, idx
, slots
)) {
1438 DNODE_STAT_BUMP(dnode_hold_alloc_lock_retry
);
1443 * Someone else won the race and called dnode_create()
1444 * after we checked DN_SLOT_IS_PTR() above but before
1445 * we acquired the lock.
1447 if (DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1448 DNODE_STAT_BUMP(dnode_hold_alloc_lock_misses
);
1449 dn
= dnh
->dnh_dnode
;
1451 dn
= dnode_create(os
, dn_block
+ idx
, db
,
1456 mutex_enter(&dn
->dn_mtx
);
1457 if (dn
->dn_type
== DMU_OT_NONE
|| dn
->dn_free_txg
!= 0) {
1458 DNODE_STAT_BUMP(dnode_hold_alloc_type_none
);
1459 mutex_exit(&dn
->dn_mtx
);
1460 dnode_slots_rele(dnc
, idx
, slots
);
1461 dbuf_rele(db
, FTAG
);
1462 return (SET_ERROR(ENOENT
));
1465 DNODE_STAT_BUMP(dnode_hold_alloc_hits
);
1466 } else if (flag
& DNODE_MUST_BE_FREE
) {
1468 if (idx
+ slots
- 1 >= DNODES_PER_BLOCK
) {
1469 DNODE_STAT_BUMP(dnode_hold_free_overflow
);
1470 dbuf_rele(db
, FTAG
);
1471 return (SET_ERROR(ENOSPC
));
1474 dnode_slots_hold(dnc
, idx
, slots
);
1476 if (!dnode_check_slots_free(dnc
, idx
, slots
)) {
1477 DNODE_STAT_BUMP(dnode_hold_free_misses
);
1478 dnode_slots_rele(dnc
, idx
, slots
);
1479 dbuf_rele(db
, FTAG
);
1480 return (SET_ERROR(ENOSPC
));
1483 dnode_slots_rele(dnc
, idx
, slots
);
1484 while (!dnode_slots_tryenter(dnc
, idx
, slots
)) {
1485 DNODE_STAT_BUMP(dnode_hold_free_lock_retry
);
1489 if (!dnode_check_slots_free(dnc
, idx
, slots
)) {
1490 DNODE_STAT_BUMP(dnode_hold_free_lock_misses
);
1491 dnode_slots_rele(dnc
, idx
, slots
);
1492 dbuf_rele(db
, FTAG
);
1493 return (SET_ERROR(ENOSPC
));
1497 * Allocated but otherwise free dnodes which would
1498 * be in the interior of a multi-slot dnodes need
1499 * to be freed. Single slot dnodes can be safely
1500 * re-purposed as a performance optimization.
1503 dnode_reclaim_slots(dnc
, idx
+ 1, slots
- 1);
1505 dnh
= &dnc
->dnc_children
[idx
];
1506 if (DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1507 dn
= dnh
->dnh_dnode
;
1509 dn
= dnode_create(os
, dn_block
+ idx
, db
,
1513 mutex_enter(&dn
->dn_mtx
);
1514 if (!zfs_refcount_is_zero(&dn
->dn_holds
) || dn
->dn_free_txg
) {
1515 DNODE_STAT_BUMP(dnode_hold_free_refcount
);
1516 mutex_exit(&dn
->dn_mtx
);
1517 dnode_slots_rele(dnc
, idx
, slots
);
1518 dbuf_rele(db
, FTAG
);
1519 return (SET_ERROR(EEXIST
));
1522 dnode_set_slots(dnc
, idx
+ 1, slots
- 1, DN_SLOT_INTERIOR
);
1523 DNODE_STAT_BUMP(dnode_hold_free_hits
);
1525 dbuf_rele(db
, FTAG
);
1526 return (SET_ERROR(EINVAL
));
1529 if (dn
->dn_free_txg
) {
1530 DNODE_STAT_BUMP(dnode_hold_free_txg
);
1532 mutex_exit(&dn
->dn_mtx
);
1533 dnode_slots_rele(dnc
, idx
, slots
);
1534 dbuf_rele(db
, FTAG
);
1535 return (SET_ERROR((flag
& DNODE_MUST_BE_ALLOCATED
) ?
1539 if (zfs_refcount_add(&dn
->dn_holds
, tag
) == 1)
1540 dbuf_add_ref(db
, dnh
);
1542 mutex_exit(&dn
->dn_mtx
);
1544 /* Now we can rely on the hold to prevent the dnode from moving. */
1545 dnode_slots_rele(dnc
, idx
, slots
);
1548 ASSERT3P(dn
->dn_dbuf
, ==, db
);
1549 ASSERT3U(dn
->dn_object
, ==, object
);
1550 dbuf_rele(db
, FTAG
);
1557 * Return held dnode if the object is allocated, NULL if not.
1560 dnode_hold(objset_t
*os
, uint64_t object
, void *tag
, dnode_t
**dnp
)
1562 return (dnode_hold_impl(os
, object
, DNODE_MUST_BE_ALLOCATED
, 0, tag
,
1567 * Can only add a reference if there is already at least one
1568 * reference on the dnode. Returns FALSE if unable to add a
1572 dnode_add_ref(dnode_t
*dn
, void *tag
)
1574 mutex_enter(&dn
->dn_mtx
);
1575 if (zfs_refcount_is_zero(&dn
->dn_holds
)) {
1576 mutex_exit(&dn
->dn_mtx
);
1579 VERIFY(1 < zfs_refcount_add(&dn
->dn_holds
, tag
));
1580 mutex_exit(&dn
->dn_mtx
);
1585 dnode_rele(dnode_t
*dn
, void *tag
)
1587 mutex_enter(&dn
->dn_mtx
);
1588 dnode_rele_and_unlock(dn
, tag
, B_FALSE
);
1592 dnode_rele_and_unlock(dnode_t
*dn
, void *tag
, boolean_t evicting
)
1595 /* Get while the hold prevents the dnode from moving. */
1596 dmu_buf_impl_t
*db
= dn
->dn_dbuf
;
1597 dnode_handle_t
*dnh
= dn
->dn_handle
;
1599 refs
= zfs_refcount_remove(&dn
->dn_holds
, tag
);
1600 mutex_exit(&dn
->dn_mtx
);
1603 * It's unsafe to release the last hold on a dnode by dnode_rele() or
1604 * indirectly by dbuf_rele() while relying on the dnode handle to
1605 * prevent the dnode from moving, since releasing the last hold could
1606 * result in the dnode's parent dbuf evicting its dnode handles. For
1607 * that reason anyone calling dnode_rele() or dbuf_rele() without some
1608 * other direct or indirect hold on the dnode must first drop the dnode
1611 ASSERT(refs
> 0 || dnh
->dnh_zrlock
.zr_owner
!= curthread
);
1613 /* NOTE: the DNODE_DNODE does not have a dn_dbuf */
1614 if (refs
== 0 && db
!= NULL
) {
1616 * Another thread could add a hold to the dnode handle in
1617 * dnode_hold_impl() while holding the parent dbuf. Since the
1618 * hold on the parent dbuf prevents the handle from being
1619 * destroyed, the hold on the handle is OK. We can't yet assert
1620 * that the handle has zero references, but that will be
1621 * asserted anyway when the handle gets destroyed.
1623 mutex_enter(&db
->db_mtx
);
1624 dbuf_rele_and_unlock(db
, dnh
, evicting
);
1629 dnode_setdirty(dnode_t
*dn
, dmu_tx_t
*tx
)
1631 objset_t
*os
= dn
->dn_objset
;
1632 uint64_t txg
= tx
->tx_txg
;
1634 if (DMU_OBJECT_IS_SPECIAL(dn
->dn_object
)) {
1635 dsl_dataset_dirty(os
->os_dsl_dataset
, tx
);
1642 mutex_enter(&dn
->dn_mtx
);
1643 ASSERT(dn
->dn_phys
->dn_type
|| dn
->dn_allocated_txg
);
1644 ASSERT(dn
->dn_free_txg
== 0 || dn
->dn_free_txg
>= txg
);
1645 mutex_exit(&dn
->dn_mtx
);
1649 * Determine old uid/gid when necessary
1651 dmu_objset_userquota_get_ids(dn
, B_TRUE
, tx
);
1653 multilist_t
*dirtylist
= os
->os_dirty_dnodes
[txg
& TXG_MASK
];
1654 multilist_sublist_t
*mls
= multilist_sublist_lock_obj(dirtylist
, dn
);
1657 * If we are already marked dirty, we're done.
1659 if (multilist_link_active(&dn
->dn_dirty_link
[txg
& TXG_MASK
])) {
1660 multilist_sublist_unlock(mls
);
1664 ASSERT(!zfs_refcount_is_zero(&dn
->dn_holds
) ||
1665 !avl_is_empty(&dn
->dn_dbufs
));
1666 ASSERT(dn
->dn_datablksz
!= 0);
1667 ASSERT0(dn
->dn_next_bonuslen
[txg
& TXG_MASK
]);
1668 ASSERT0(dn
->dn_next_blksz
[txg
& TXG_MASK
]);
1669 ASSERT0(dn
->dn_next_bonustype
[txg
& TXG_MASK
]);
1671 dprintf_ds(os
->os_dsl_dataset
, "obj=%llu txg=%llu\n",
1672 dn
->dn_object
, txg
);
1674 multilist_sublist_insert_head(mls
, dn
);
1676 multilist_sublist_unlock(mls
);
1679 * The dnode maintains a hold on its containing dbuf as
1680 * long as there are holds on it. Each instantiated child
1681 * dbuf maintains a hold on the dnode. When the last child
1682 * drops its hold, the dnode will drop its hold on the
1683 * containing dbuf. We add a "dirty hold" here so that the
1684 * dnode will hang around after we finish processing its
1687 VERIFY(dnode_add_ref(dn
, (void *)(uintptr_t)tx
->tx_txg
));
1689 (void) dbuf_dirty(dn
->dn_dbuf
, tx
);
1691 dsl_dataset_dirty(os
->os_dsl_dataset
, tx
);
1695 dnode_free(dnode_t
*dn
, dmu_tx_t
*tx
)
1697 mutex_enter(&dn
->dn_mtx
);
1698 if (dn
->dn_type
== DMU_OT_NONE
|| dn
->dn_free_txg
) {
1699 mutex_exit(&dn
->dn_mtx
);
1702 dn
->dn_free_txg
= tx
->tx_txg
;
1703 mutex_exit(&dn
->dn_mtx
);
1705 dnode_setdirty(dn
, tx
);
1709 * Try to change the block size for the indicated dnode. This can only
1710 * succeed if there are no blocks allocated or dirty beyond first block
1713 dnode_set_blksz(dnode_t
*dn
, uint64_t size
, int ibs
, dmu_tx_t
*tx
)
1718 ASSERT3U(size
, <=, spa_maxblocksize(dmu_objset_spa(dn
->dn_objset
)));
1720 size
= SPA_MINBLOCKSIZE
;
1722 size
= P2ROUNDUP(size
, SPA_MINBLOCKSIZE
);
1724 if (ibs
== dn
->dn_indblkshift
)
1727 if (size
>> SPA_MINBLOCKSHIFT
== dn
->dn_datablkszsec
&& ibs
== 0)
1730 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1732 /* Check for any allocated blocks beyond the first */
1733 if (dn
->dn_maxblkid
!= 0)
1736 mutex_enter(&dn
->dn_dbufs_mtx
);
1737 for (db
= avl_first(&dn
->dn_dbufs
); db
!= NULL
;
1738 db
= AVL_NEXT(&dn
->dn_dbufs
, db
)) {
1739 if (db
->db_blkid
!= 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1740 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1741 mutex_exit(&dn
->dn_dbufs_mtx
);
1745 mutex_exit(&dn
->dn_dbufs_mtx
);
1747 if (ibs
&& dn
->dn_nlevels
!= 1)
1750 /* resize the old block */
1751 err
= dbuf_hold_impl(dn
, 0, 0, TRUE
, FALSE
, FTAG
, &db
);
1753 dbuf_new_size(db
, size
, tx
);
1754 } else if (err
!= ENOENT
) {
1758 dnode_setdblksz(dn
, size
);
1759 dnode_setdirty(dn
, tx
);
1760 dn
->dn_next_blksz
[tx
->tx_txg
&TXG_MASK
] = size
;
1762 dn
->dn_indblkshift
= ibs
;
1763 dn
->dn_next_indblkshift
[tx
->tx_txg
&TXG_MASK
] = ibs
;
1765 /* rele after we have fixed the blocksize in the dnode */
1767 dbuf_rele(db
, FTAG
);
1769 rw_exit(&dn
->dn_struct_rwlock
);
1773 rw_exit(&dn
->dn_struct_rwlock
);
1774 return (SET_ERROR(ENOTSUP
));
1778 dnode_set_nlevels_impl(dnode_t
*dn
, int new_nlevels
, dmu_tx_t
*tx
)
1780 uint64_t txgoff
= tx
->tx_txg
& TXG_MASK
;
1781 int old_nlevels
= dn
->dn_nlevels
;
1784 dbuf_dirty_record_t
*new, *dr
, *dr_next
;
1786 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1788 dn
->dn_nlevels
= new_nlevels
;
1790 ASSERT3U(new_nlevels
, >, dn
->dn_next_nlevels
[txgoff
]);
1791 dn
->dn_next_nlevels
[txgoff
] = new_nlevels
;
1793 /* dirty the left indirects */
1794 db
= dbuf_hold_level(dn
, old_nlevels
, 0, FTAG
);
1796 new = dbuf_dirty(db
, tx
);
1797 dbuf_rele(db
, FTAG
);
1799 /* transfer the dirty records to the new indirect */
1800 mutex_enter(&dn
->dn_mtx
);
1801 mutex_enter(&new->dt
.di
.dr_mtx
);
1802 list
= &dn
->dn_dirty_records
[txgoff
];
1803 for (dr
= list_head(list
); dr
; dr
= dr_next
) {
1804 dr_next
= list_next(&dn
->dn_dirty_records
[txgoff
], dr
);
1805 if (dr
->dr_dbuf
->db_level
!= new_nlevels
-1 &&
1806 dr
->dr_dbuf
->db_blkid
!= DMU_BONUS_BLKID
&&
1807 dr
->dr_dbuf
->db_blkid
!= DMU_SPILL_BLKID
) {
1808 ASSERT(dr
->dr_dbuf
->db_level
== old_nlevels
-1);
1809 list_remove(&dn
->dn_dirty_records
[txgoff
], dr
);
1810 list_insert_tail(&new->dt
.di
.dr_children
, dr
);
1811 dr
->dr_parent
= new;
1814 mutex_exit(&new->dt
.di
.dr_mtx
);
1815 mutex_exit(&dn
->dn_mtx
);
1819 dnode_set_nlevels(dnode_t
*dn
, int nlevels
, dmu_tx_t
*tx
)
1823 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1825 if (dn
->dn_nlevels
== nlevels
) {
1828 } else if (nlevels
< dn
->dn_nlevels
) {
1829 ret
= SET_ERROR(EINVAL
);
1833 dnode_set_nlevels_impl(dn
, nlevels
, tx
);
1836 rw_exit(&dn
->dn_struct_rwlock
);
1840 /* read-holding callers must not rely on the lock being continuously held */
1842 dnode_new_blkid(dnode_t
*dn
, uint64_t blkid
, dmu_tx_t
*tx
, boolean_t have_read
,
1845 int epbs
, new_nlevels
;
1848 ASSERT(blkid
!= DMU_BONUS_BLKID
);
1851 RW_READ_HELD(&dn
->dn_struct_rwlock
) :
1852 RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1855 * if we have a read-lock, check to see if we need to do any work
1856 * before upgrading to a write-lock.
1859 if (blkid
<= dn
->dn_maxblkid
)
1862 if (!rw_tryupgrade(&dn
->dn_struct_rwlock
)) {
1863 rw_exit(&dn
->dn_struct_rwlock
);
1864 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1869 * Raw sends (indicated by the force flag) require that we take the
1870 * given blkid even if the value is lower than the current value.
1872 if (!force
&& blkid
<= dn
->dn_maxblkid
)
1876 * We use the (otherwise unused) top bit of dn_next_maxblkid[txgoff]
1877 * to indicate that this field is set. This allows us to set the
1878 * maxblkid to 0 on an existing object in dnode_sync().
1880 dn
->dn_maxblkid
= blkid
;
1881 dn
->dn_next_maxblkid
[tx
->tx_txg
& TXG_MASK
] =
1882 blkid
| DMU_NEXT_MAXBLKID_SET
;
1885 * Compute the number of levels necessary to support the new maxblkid.
1886 * Raw sends will ensure nlevels is set correctly for us.
1889 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1890 for (sz
= dn
->dn_nblkptr
;
1891 sz
<= blkid
&& sz
>= dn
->dn_nblkptr
; sz
<<= epbs
)
1894 ASSERT3U(new_nlevels
, <=, DN_MAX_LEVELS
);
1897 if (new_nlevels
> dn
->dn_nlevels
)
1898 dnode_set_nlevels_impl(dn
, new_nlevels
, tx
);
1900 ASSERT3U(dn
->dn_nlevels
, >=, new_nlevels
);
1905 rw_downgrade(&dn
->dn_struct_rwlock
);
1909 dnode_dirty_l1(dnode_t
*dn
, uint64_t l1blkid
, dmu_tx_t
*tx
)
1911 dmu_buf_impl_t
*db
= dbuf_hold_level(dn
, 1, l1blkid
, FTAG
);
1913 dmu_buf_will_dirty(&db
->db
, tx
);
1914 dbuf_rele(db
, FTAG
);
1919 * Dirty all the in-core level-1 dbufs in the range specified by start_blkid
1923 dnode_dirty_l1range(dnode_t
*dn
, uint64_t start_blkid
, uint64_t end_blkid
,
1926 dmu_buf_impl_t db_search
;
1930 mutex_enter(&dn
->dn_dbufs_mtx
);
1932 db_search
.db_level
= 1;
1933 db_search
.db_blkid
= start_blkid
+ 1;
1934 db_search
.db_state
= DB_SEARCH
;
1937 db
= avl_find(&dn
->dn_dbufs
, &db_search
, &where
);
1939 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
1941 if (db
== NULL
|| db
->db_level
!= 1 ||
1942 db
->db_blkid
>= end_blkid
) {
1947 * Setup the next blkid we want to search for.
1949 db_search
.db_blkid
= db
->db_blkid
+ 1;
1950 ASSERT3U(db
->db_blkid
, >=, start_blkid
);
1953 * If the dbuf transitions to DB_EVICTING while we're trying
1954 * to dirty it, then we will be unable to discover it in
1955 * the dbuf hash table. This will result in a call to
1956 * dbuf_create() which needs to acquire the dn_dbufs_mtx
1957 * lock. To avoid a deadlock, we drop the lock before
1958 * dirtying the level-1 dbuf.
1960 mutex_exit(&dn
->dn_dbufs_mtx
);
1961 dnode_dirty_l1(dn
, db
->db_blkid
, tx
);
1962 mutex_enter(&dn
->dn_dbufs_mtx
);
1967 * Walk all the in-core level-1 dbufs and verify they have been dirtied.
1969 db_search
.db_level
= 1;
1970 db_search
.db_blkid
= start_blkid
+ 1;
1971 db_search
.db_state
= DB_SEARCH
;
1972 db
= avl_find(&dn
->dn_dbufs
, &db_search
, &where
);
1974 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
1975 for (; db
!= NULL
; db
= AVL_NEXT(&dn
->dn_dbufs
, db
)) {
1976 if (db
->db_level
!= 1 || db
->db_blkid
>= end_blkid
)
1978 if (db
->db_state
!= DB_EVICTING
)
1979 ASSERT(db
->db_dirtycnt
> 0);
1982 mutex_exit(&dn
->dn_dbufs_mtx
);
1986 dnode_free_range(dnode_t
*dn
, uint64_t off
, uint64_t len
, dmu_tx_t
*tx
)
1989 uint64_t blkoff
, blkid
, nblks
;
1990 int blksz
, blkshift
, head
, tail
;
1994 blksz
= dn
->dn_datablksz
;
1995 blkshift
= dn
->dn_datablkshift
;
1996 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1998 if (len
== DMU_OBJECT_END
) {
1999 len
= UINT64_MAX
- off
;
2004 * First, block align the region to free:
2007 head
= P2NPHASE(off
, blksz
);
2008 blkoff
= P2PHASE(off
, blksz
);
2009 if ((off
>> blkshift
) > dn
->dn_maxblkid
)
2012 ASSERT(dn
->dn_maxblkid
== 0);
2013 if (off
== 0 && len
>= blksz
) {
2015 * Freeing the whole block; fast-track this request.
2019 if (dn
->dn_nlevels
> 1) {
2020 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
2021 dnode_dirty_l1(dn
, 0, tx
);
2022 rw_exit(&dn
->dn_struct_rwlock
);
2025 } else if (off
>= blksz
) {
2026 /* Freeing past end-of-data */
2029 /* Freeing part of the block. */
2031 ASSERT3U(head
, >, 0);
2035 /* zero out any partial block data at the start of the range */
2038 ASSERT3U(blkoff
+ head
, ==, blksz
);
2041 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2042 res
= dbuf_hold_impl(dn
, 0, dbuf_whichblock(dn
, 0, off
),
2043 TRUE
, FALSE
, FTAG
, &db
);
2044 rw_exit(&dn
->dn_struct_rwlock
);
2049 db_lock_type_t dblt
= dmu_buf_lock_parent(db
, RW_READER
,
2051 /* don't dirty if it isn't on disk and isn't dirty */
2052 dirty
= db
->db_last_dirty
||
2053 (db
->db_blkptr
&& !BP_IS_HOLE(db
->db_blkptr
));
2054 dmu_buf_unlock_parent(db
, dblt
, FTAG
);
2056 dmu_buf_will_dirty(&db
->db
, tx
);
2057 data
= db
->db
.db_data
;
2058 bzero(data
+ blkoff
, head
);
2060 dbuf_rele(db
, FTAG
);
2066 /* If the range was less than one block, we're done */
2070 /* If the remaining range is past end of file, we're done */
2071 if ((off
>> blkshift
) > dn
->dn_maxblkid
)
2074 ASSERT(ISP2(blksz
));
2078 tail
= P2PHASE(len
, blksz
);
2080 ASSERT0(P2PHASE(off
, blksz
));
2081 /* zero out any partial block data at the end of the range */
2086 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2087 res
= dbuf_hold_impl(dn
, 0, dbuf_whichblock(dn
, 0, off
+len
),
2088 TRUE
, FALSE
, FTAG
, &db
);
2089 rw_exit(&dn
->dn_struct_rwlock
);
2092 /* don't dirty if not on disk and not dirty */
2093 db_lock_type_t type
= dmu_buf_lock_parent(db
, RW_READER
,
2095 dirty
= db
->db_last_dirty
||
2096 (db
->db_blkptr
&& !BP_IS_HOLE(db
->db_blkptr
));
2097 dmu_buf_unlock_parent(db
, type
, FTAG
);
2099 dmu_buf_will_dirty(&db
->db
, tx
);
2100 bzero(db
->db
.db_data
, tail
);
2102 dbuf_rele(db
, FTAG
);
2107 /* If the range did not include a full block, we are done */
2111 ASSERT(IS_P2ALIGNED(off
, blksz
));
2112 ASSERT(trunc
|| IS_P2ALIGNED(len
, blksz
));
2113 blkid
= off
>> blkshift
;
2114 nblks
= len
>> blkshift
;
2119 * Dirty all the indirect blocks in this range. Note that only
2120 * the first and last indirect blocks can actually be written
2121 * (if they were partially freed) -- they must be dirtied, even if
2122 * they do not exist on disk yet. The interior blocks will
2123 * be freed by free_children(), so they will not actually be written.
2124 * Even though these interior blocks will not be written, we
2125 * dirty them for two reasons:
2127 * - It ensures that the indirect blocks remain in memory until
2128 * syncing context. (They have already been prefetched by
2129 * dmu_tx_hold_free(), so we don't have to worry about reading
2130 * them serially here.)
2132 * - The dirty space accounting will put pressure on the txg sync
2133 * mechanism to begin syncing, and to delay transactions if there
2134 * is a large amount of freeing. Even though these indirect
2135 * blocks will not be written, we could need to write the same
2136 * amount of space if we copy the freed BPs into deadlists.
2138 if (dn
->dn_nlevels
> 1) {
2139 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
2140 uint64_t first
, last
;
2142 first
= blkid
>> epbs
;
2143 dnode_dirty_l1(dn
, first
, tx
);
2145 last
= dn
->dn_maxblkid
>> epbs
;
2147 last
= (blkid
+ nblks
- 1) >> epbs
;
2149 dnode_dirty_l1(dn
, last
, tx
);
2151 dnode_dirty_l1range(dn
, first
, last
, tx
);
2153 int shift
= dn
->dn_datablkshift
+ dn
->dn_indblkshift
-
2155 for (uint64_t i
= first
+ 1; i
< last
; i
++) {
2157 * Set i to the blockid of the next non-hole
2158 * level-1 indirect block at or after i. Note
2159 * that dnode_next_offset() operates in terms of
2160 * level-0-equivalent bytes.
2162 uint64_t ibyte
= i
<< shift
;
2163 int err
= dnode_next_offset(dn
, DNODE_FIND_HAVELOCK
,
2170 * Normally we should not see an error, either
2171 * from dnode_next_offset() or dbuf_hold_level()
2172 * (except for ESRCH from dnode_next_offset).
2173 * If there is an i/o error, then when we read
2174 * this block in syncing context, it will use
2175 * ZIO_FLAG_MUSTSUCCEED, and thus hang/panic according
2176 * to the "failmode" property. dnode_next_offset()
2177 * doesn't have a flag to indicate MUSTSUCCEED.
2182 dnode_dirty_l1(dn
, i
, tx
);
2184 rw_exit(&dn
->dn_struct_rwlock
);
2189 * Add this range to the dnode range list.
2190 * We will finish up this free operation in the syncing phase.
2192 mutex_enter(&dn
->dn_mtx
);
2194 int txgoff
= tx
->tx_txg
& TXG_MASK
;
2195 if (dn
->dn_free_ranges
[txgoff
] == NULL
) {
2196 dn
->dn_free_ranges
[txgoff
] = range_tree_create(NULL
, NULL
);
2198 range_tree_clear(dn
->dn_free_ranges
[txgoff
], blkid
, nblks
);
2199 range_tree_add(dn
->dn_free_ranges
[txgoff
], blkid
, nblks
);
2201 dprintf_dnode(dn
, "blkid=%llu nblks=%llu txg=%llu\n",
2202 blkid
, nblks
, tx
->tx_txg
);
2203 mutex_exit(&dn
->dn_mtx
);
2205 dbuf_free_range(dn
, blkid
, blkid
+ nblks
- 1, tx
);
2206 dnode_setdirty(dn
, tx
);
2210 dnode_spill_freed(dnode_t
*dn
)
2214 mutex_enter(&dn
->dn_mtx
);
2215 for (i
= 0; i
< TXG_SIZE
; i
++) {
2216 if (dn
->dn_rm_spillblk
[i
] == DN_KILL_SPILLBLK
)
2219 mutex_exit(&dn
->dn_mtx
);
2220 return (i
< TXG_SIZE
);
2223 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
2225 dnode_block_freed(dnode_t
*dn
, uint64_t blkid
)
2227 void *dp
= spa_get_dsl(dn
->dn_objset
->os_spa
);
2230 if (blkid
== DMU_BONUS_BLKID
)
2234 * If we're in the process of opening the pool, dp will not be
2235 * set yet, but there shouldn't be anything dirty.
2240 if (dn
->dn_free_txg
)
2243 if (blkid
== DMU_SPILL_BLKID
)
2244 return (dnode_spill_freed(dn
));
2246 mutex_enter(&dn
->dn_mtx
);
2247 for (i
= 0; i
< TXG_SIZE
; i
++) {
2248 if (dn
->dn_free_ranges
[i
] != NULL
&&
2249 range_tree_contains(dn
->dn_free_ranges
[i
], blkid
, 1))
2252 mutex_exit(&dn
->dn_mtx
);
2253 return (i
< TXG_SIZE
);
2256 /* call from syncing context when we actually write/free space for this dnode */
2258 dnode_diduse_space(dnode_t
*dn
, int64_t delta
)
2261 dprintf_dnode(dn
, "dn=%p dnp=%p used=%llu delta=%lld\n",
2263 (u_longlong_t
)dn
->dn_phys
->dn_used
,
2266 mutex_enter(&dn
->dn_mtx
);
2267 space
= DN_USED_BYTES(dn
->dn_phys
);
2269 ASSERT3U(space
+ delta
, >=, space
); /* no overflow */
2271 ASSERT3U(space
, >=, -delta
); /* no underflow */
2274 if (spa_version(dn
->dn_objset
->os_spa
) < SPA_VERSION_DNODE_BYTES
) {
2275 ASSERT((dn
->dn_phys
->dn_flags
& DNODE_FLAG_USED_BYTES
) == 0);
2276 ASSERT0(P2PHASE(space
, 1<<DEV_BSHIFT
));
2277 dn
->dn_phys
->dn_used
= space
>> DEV_BSHIFT
;
2279 dn
->dn_phys
->dn_used
= space
;
2280 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_USED_BYTES
;
2282 mutex_exit(&dn
->dn_mtx
);
2286 * Scans a block at the indicated "level" looking for a hole or data,
2287 * depending on 'flags'.
2289 * If level > 0, then we are scanning an indirect block looking at its
2290 * pointers. If level == 0, then we are looking at a block of dnodes.
2292 * If we don't find what we are looking for in the block, we return ESRCH.
2293 * Otherwise, return with *offset pointing to the beginning (if searching
2294 * forwards) or end (if searching backwards) of the range covered by the
2295 * block pointer we matched on (or dnode).
2297 * The basic search algorithm used below by dnode_next_offset() is to
2298 * use this function to search up the block tree (widen the search) until
2299 * we find something (i.e., we don't return ESRCH) and then search back
2300 * down the tree (narrow the search) until we reach our original search
2304 dnode_next_offset_level(dnode_t
*dn
, int flags
, uint64_t *offset
,
2305 int lvl
, uint64_t blkfill
, uint64_t txg
)
2307 dmu_buf_impl_t
*db
= NULL
;
2309 uint64_t epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2310 uint64_t epb
= 1ULL << epbs
;
2311 uint64_t minfill
, maxfill
;
2313 int i
, inc
, error
, span
;
2315 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2317 hole
= ((flags
& DNODE_FIND_HOLE
) != 0);
2318 inc
= (flags
& DNODE_FIND_BACKWARDS
) ? -1 : 1;
2319 ASSERT(txg
== 0 || !hole
);
2321 if (lvl
== dn
->dn_phys
->dn_nlevels
) {
2323 epb
= dn
->dn_phys
->dn_nblkptr
;
2324 data
= dn
->dn_phys
->dn_blkptr
;
2326 uint64_t blkid
= dbuf_whichblock(dn
, lvl
, *offset
);
2327 error
= dbuf_hold_impl(dn
, lvl
, blkid
, TRUE
, FALSE
, FTAG
, &db
);
2329 if (error
!= ENOENT
)
2334 * This can only happen when we are searching up
2335 * the block tree for data. We don't really need to
2336 * adjust the offset, as we will just end up looking
2337 * at the pointer to this block in its parent, and its
2338 * going to be unallocated, so we will skip over it.
2340 return (SET_ERROR(ESRCH
));
2342 error
= dbuf_read(db
, NULL
,
2343 DB_RF_CANFAIL
| DB_RF_HAVESTRUCT
| DB_RF_NO_DECRYPT
);
2345 dbuf_rele(db
, FTAG
);
2348 data
= db
->db
.db_data
;
2349 rw_enter(&db
->db_rwlock
, RW_READER
);
2352 if (db
!= NULL
&& txg
!= 0 && (db
->db_blkptr
== NULL
||
2353 db
->db_blkptr
->blk_birth
<= txg
||
2354 BP_IS_HOLE(db
->db_blkptr
))) {
2356 * This can only happen when we are searching up the tree
2357 * and these conditions mean that we need to keep climbing.
2359 error
= SET_ERROR(ESRCH
);
2360 } else if (lvl
== 0) {
2361 dnode_phys_t
*dnp
= data
;
2363 ASSERT(dn
->dn_type
== DMU_OT_DNODE
);
2364 ASSERT(!(flags
& DNODE_FIND_BACKWARDS
));
2366 for (i
= (*offset
>> DNODE_SHIFT
) & (blkfill
- 1);
2367 i
< blkfill
; i
+= dnp
[i
].dn_extra_slots
+ 1) {
2368 if ((dnp
[i
].dn_type
== DMU_OT_NONE
) == hole
)
2373 error
= SET_ERROR(ESRCH
);
2375 *offset
= (*offset
& ~(DNODE_BLOCK_SIZE
- 1)) +
2378 blkptr_t
*bp
= data
;
2379 uint64_t start
= *offset
;
2380 span
= (lvl
- 1) * epbs
+ dn
->dn_datablkshift
;
2382 maxfill
= blkfill
<< ((lvl
- 1) * epbs
);
2389 if (span
>= 8 * sizeof (*offset
)) {
2390 /* This only happens on the highest indirection level */
2391 ASSERT3U((lvl
- 1), ==, dn
->dn_phys
->dn_nlevels
- 1);
2394 *offset
= *offset
>> span
;
2397 for (i
= BF64_GET(*offset
, 0, epbs
);
2398 i
>= 0 && i
< epb
; i
+= inc
) {
2399 if (BP_GET_FILL(&bp
[i
]) >= minfill
&&
2400 BP_GET_FILL(&bp
[i
]) <= maxfill
&&
2401 (hole
|| bp
[i
].blk_birth
> txg
))
2403 if (inc
> 0 || *offset
> 0)
2407 if (span
>= 8 * sizeof (*offset
)) {
2410 *offset
= *offset
<< span
;
2414 /* traversing backwards; position offset at the end */
2415 ASSERT3U(*offset
, <=, start
);
2416 *offset
= MIN(*offset
+ (1ULL << span
) - 1, start
);
2417 } else if (*offset
< start
) {
2420 if (i
< 0 || i
>= epb
)
2421 error
= SET_ERROR(ESRCH
);
2425 rw_exit(&db
->db_rwlock
);
2426 dbuf_rele(db
, FTAG
);
2433 * Find the next hole, data, or sparse region at or after *offset.
2434 * The value 'blkfill' tells us how many items we expect to find
2435 * in an L0 data block; this value is 1 for normal objects,
2436 * DNODES_PER_BLOCK for the meta dnode, and some fraction of
2437 * DNODES_PER_BLOCK when searching for sparse regions thereof.
2441 * dnode_next_offset(dn, flags, offset, 1, 1, 0);
2442 * Finds the next/previous hole/data in a file.
2443 * Used in dmu_offset_next().
2445 * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
2446 * Finds the next free/allocated dnode an objset's meta-dnode.
2447 * Only finds objects that have new contents since txg (ie.
2448 * bonus buffer changes and content removal are ignored).
2449 * Used in dmu_object_next().
2451 * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
2452 * Finds the next L2 meta-dnode bp that's at most 1/4 full.
2453 * Used in dmu_object_alloc().
2456 dnode_next_offset(dnode_t
*dn
, int flags
, uint64_t *offset
,
2457 int minlvl
, uint64_t blkfill
, uint64_t txg
)
2459 uint64_t initial_offset
= *offset
;
2463 if (!(flags
& DNODE_FIND_HAVELOCK
))
2464 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2466 if (dn
->dn_phys
->dn_nlevels
== 0) {
2467 error
= SET_ERROR(ESRCH
);
2471 if (dn
->dn_datablkshift
== 0) {
2472 if (*offset
< dn
->dn_datablksz
) {
2473 if (flags
& DNODE_FIND_HOLE
)
2474 *offset
= dn
->dn_datablksz
;
2476 error
= SET_ERROR(ESRCH
);
2481 maxlvl
= dn
->dn_phys
->dn_nlevels
;
2483 for (lvl
= minlvl
; lvl
<= maxlvl
; lvl
++) {
2484 error
= dnode_next_offset_level(dn
,
2485 flags
, offset
, lvl
, blkfill
, txg
);
2490 while (error
== 0 && --lvl
>= minlvl
) {
2491 error
= dnode_next_offset_level(dn
,
2492 flags
, offset
, lvl
, blkfill
, txg
);
2496 * There's always a "virtual hole" at the end of the object, even
2497 * if all BP's which physically exist are non-holes.
2499 if ((flags
& DNODE_FIND_HOLE
) && error
== ESRCH
&& txg
== 0 &&
2500 minlvl
== 1 && blkfill
== 1 && !(flags
& DNODE_FIND_BACKWARDS
)) {
2504 if (error
== 0 && (flags
& DNODE_FIND_BACKWARDS
?
2505 initial_offset
< *offset
: initial_offset
> *offset
))
2506 error
= SET_ERROR(ESRCH
);
2508 if (!(flags
& DNODE_FIND_HAVELOCK
))
2509 rw_exit(&dn
->dn_struct_rwlock
);
2514 #if defined(_KERNEL)
2515 EXPORT_SYMBOL(dnode_hold
);
2516 EXPORT_SYMBOL(dnode_rele
);
2517 EXPORT_SYMBOL(dnode_set_nlevels
);
2518 EXPORT_SYMBOL(dnode_set_blksz
);
2519 EXPORT_SYMBOL(dnode_free_range
);
2520 EXPORT_SYMBOL(dnode_evict_dbufs
);
2521 EXPORT_SYMBOL(dnode_evict_bonus
);