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_free_interior_lock_retry", KSTAT_DATA_UINT64
},
59 { "dnode_allocate", KSTAT_DATA_UINT64
},
60 { "dnode_reallocate", KSTAT_DATA_UINT64
},
61 { "dnode_buf_evict", KSTAT_DATA_UINT64
},
62 { "dnode_alloc_next_chunk", KSTAT_DATA_UINT64
},
63 { "dnode_alloc_race", KSTAT_DATA_UINT64
},
64 { "dnode_alloc_next_block", KSTAT_DATA_UINT64
},
65 { "dnode_move_invalid", KSTAT_DATA_UINT64
},
66 { "dnode_move_recheck1", KSTAT_DATA_UINT64
},
67 { "dnode_move_recheck2", KSTAT_DATA_UINT64
},
68 { "dnode_move_special", KSTAT_DATA_UINT64
},
69 { "dnode_move_handle", KSTAT_DATA_UINT64
},
70 { "dnode_move_rwlock", KSTAT_DATA_UINT64
},
71 { "dnode_move_active", KSTAT_DATA_UINT64
},
74 static kstat_t
*dnode_ksp
;
75 static kmem_cache_t
*dnode_cache
;
77 ASSERTV(static dnode_phys_t dnode_phys_zero
);
79 int zfs_default_bs
= SPA_MINBLOCKSHIFT
;
80 int zfs_default_ibs
= DN_MAX_INDBLKSHIFT
;
83 static kmem_cbrc_t
dnode_move(void *, void *, size_t, void *);
87 dbuf_compare(const void *x1
, const void *x2
)
89 const dmu_buf_impl_t
*d1
= x1
;
90 const dmu_buf_impl_t
*d2
= x2
;
92 int cmp
= AVL_CMP(d1
->db_level
, d2
->db_level
);
96 cmp
= AVL_CMP(d1
->db_blkid
, d2
->db_blkid
);
100 if (d1
->db_state
== DB_SEARCH
) {
101 ASSERT3S(d2
->db_state
, !=, DB_SEARCH
);
103 } else if (d2
->db_state
== DB_SEARCH
) {
104 ASSERT3S(d1
->db_state
, !=, DB_SEARCH
);
108 return (AVL_PCMP(d1
, d2
));
113 dnode_cons(void *arg
, void *unused
, int kmflag
)
118 rw_init(&dn
->dn_struct_rwlock
, NULL
, RW_NOLOCKDEP
, NULL
);
119 mutex_init(&dn
->dn_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
120 mutex_init(&dn
->dn_dbufs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
121 cv_init(&dn
->dn_notxholds
, NULL
, CV_DEFAULT
, NULL
);
124 * Every dbuf has a reference, and dropping a tracked reference is
125 * O(number of references), so don't track dn_holds.
127 zfs_refcount_create_untracked(&dn
->dn_holds
);
128 zfs_refcount_create(&dn
->dn_tx_holds
);
129 list_link_init(&dn
->dn_link
);
131 bzero(&dn
->dn_next_nblkptr
[0], sizeof (dn
->dn_next_nblkptr
));
132 bzero(&dn
->dn_next_nlevels
[0], sizeof (dn
->dn_next_nlevels
));
133 bzero(&dn
->dn_next_indblkshift
[0], sizeof (dn
->dn_next_indblkshift
));
134 bzero(&dn
->dn_next_bonustype
[0], sizeof (dn
->dn_next_bonustype
));
135 bzero(&dn
->dn_rm_spillblk
[0], sizeof (dn
->dn_rm_spillblk
));
136 bzero(&dn
->dn_next_bonuslen
[0], sizeof (dn
->dn_next_bonuslen
));
137 bzero(&dn
->dn_next_blksz
[0], sizeof (dn
->dn_next_blksz
));
138 bzero(&dn
->dn_next_maxblkid
[0], sizeof (dn
->dn_next_maxblkid
));
140 for (i
= 0; i
< TXG_SIZE
; i
++) {
141 multilist_link_init(&dn
->dn_dirty_link
[i
]);
142 dn
->dn_free_ranges
[i
] = NULL
;
143 list_create(&dn
->dn_dirty_records
[i
],
144 sizeof (dbuf_dirty_record_t
),
145 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
148 dn
->dn_allocated_txg
= 0;
150 dn
->dn_assigned_txg
= 0;
151 dn
->dn_dirty_txg
= 0;
153 dn
->dn_dirtyctx_firstset
= NULL
;
155 dn
->dn_have_spill
= B_FALSE
;
161 dn
->dn_oldprojid
= ZFS_DEFAULT_PROJID
;
164 dn
->dn_newprojid
= ZFS_DEFAULT_PROJID
;
167 dn
->dn_dbufs_count
= 0;
168 avl_create(&dn
->dn_dbufs
, dbuf_compare
, sizeof (dmu_buf_impl_t
),
169 offsetof(dmu_buf_impl_t
, db_link
));
177 dnode_dest(void *arg
, void *unused
)
182 rw_destroy(&dn
->dn_struct_rwlock
);
183 mutex_destroy(&dn
->dn_mtx
);
184 mutex_destroy(&dn
->dn_dbufs_mtx
);
185 cv_destroy(&dn
->dn_notxholds
);
186 zfs_refcount_destroy(&dn
->dn_holds
);
187 zfs_refcount_destroy(&dn
->dn_tx_holds
);
188 ASSERT(!list_link_active(&dn
->dn_link
));
190 for (i
= 0; i
< TXG_SIZE
; i
++) {
191 ASSERT(!multilist_link_active(&dn
->dn_dirty_link
[i
]));
192 ASSERT3P(dn
->dn_free_ranges
[i
], ==, NULL
);
193 list_destroy(&dn
->dn_dirty_records
[i
]);
194 ASSERT0(dn
->dn_next_nblkptr
[i
]);
195 ASSERT0(dn
->dn_next_nlevels
[i
]);
196 ASSERT0(dn
->dn_next_indblkshift
[i
]);
197 ASSERT0(dn
->dn_next_bonustype
[i
]);
198 ASSERT0(dn
->dn_rm_spillblk
[i
]);
199 ASSERT0(dn
->dn_next_bonuslen
[i
]);
200 ASSERT0(dn
->dn_next_blksz
[i
]);
201 ASSERT0(dn
->dn_next_maxblkid
[i
]);
204 ASSERT0(dn
->dn_allocated_txg
);
205 ASSERT0(dn
->dn_free_txg
);
206 ASSERT0(dn
->dn_assigned_txg
);
207 ASSERT0(dn
->dn_dirty_txg
);
208 ASSERT0(dn
->dn_dirtyctx
);
209 ASSERT3P(dn
->dn_dirtyctx_firstset
, ==, NULL
);
210 ASSERT3P(dn
->dn_bonus
, ==, NULL
);
211 ASSERT(!dn
->dn_have_spill
);
212 ASSERT3P(dn
->dn_zio
, ==, NULL
);
213 ASSERT0(dn
->dn_oldused
);
214 ASSERT0(dn
->dn_oldflags
);
215 ASSERT0(dn
->dn_olduid
);
216 ASSERT0(dn
->dn_oldgid
);
217 ASSERT0(dn
->dn_oldprojid
);
218 ASSERT0(dn
->dn_newuid
);
219 ASSERT0(dn
->dn_newgid
);
220 ASSERT0(dn
->dn_newprojid
);
221 ASSERT0(dn
->dn_id_flags
);
223 ASSERT0(dn
->dn_dbufs_count
);
224 avl_destroy(&dn
->dn_dbufs
);
230 ASSERT(dnode_cache
== NULL
);
231 dnode_cache
= kmem_cache_create("dnode_t", sizeof (dnode_t
),
232 0, dnode_cons
, dnode_dest
, NULL
, NULL
, NULL
, 0);
233 kmem_cache_set_move(dnode_cache
, dnode_move
);
235 dnode_ksp
= kstat_create("zfs", 0, "dnodestats", "misc",
236 KSTAT_TYPE_NAMED
, sizeof (dnode_stats
) / sizeof (kstat_named_t
),
238 if (dnode_ksp
!= NULL
) {
239 dnode_ksp
->ks_data
= &dnode_stats
;
240 kstat_install(dnode_ksp
);
247 if (dnode_ksp
!= NULL
) {
248 kstat_delete(dnode_ksp
);
252 kmem_cache_destroy(dnode_cache
);
259 dnode_verify(dnode_t
*dn
)
261 int drop_struct_lock
= FALSE
;
264 ASSERT(dn
->dn_objset
);
265 ASSERT(dn
->dn_handle
->dnh_dnode
== dn
);
267 ASSERT(DMU_OT_IS_VALID(dn
->dn_phys
->dn_type
));
269 if (!(zfs_flags
& ZFS_DEBUG_DNODE_VERIFY
))
272 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
273 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
274 drop_struct_lock
= TRUE
;
276 if (dn
->dn_phys
->dn_type
!= DMU_OT_NONE
|| dn
->dn_allocated_txg
!= 0) {
278 int max_bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
279 ASSERT3U(dn
->dn_indblkshift
, <=, SPA_MAXBLOCKSHIFT
);
280 if (dn
->dn_datablkshift
) {
281 ASSERT3U(dn
->dn_datablkshift
, >=, SPA_MINBLOCKSHIFT
);
282 ASSERT3U(dn
->dn_datablkshift
, <=, SPA_MAXBLOCKSHIFT
);
283 ASSERT3U(1<<dn
->dn_datablkshift
, ==, dn
->dn_datablksz
);
285 ASSERT3U(dn
->dn_nlevels
, <=, 30);
286 ASSERT(DMU_OT_IS_VALID(dn
->dn_type
));
287 ASSERT3U(dn
->dn_nblkptr
, >=, 1);
288 ASSERT3U(dn
->dn_nblkptr
, <=, DN_MAX_NBLKPTR
);
289 ASSERT3U(dn
->dn_bonuslen
, <=, max_bonuslen
);
290 ASSERT3U(dn
->dn_datablksz
, ==,
291 dn
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
);
292 ASSERT3U(ISP2(dn
->dn_datablksz
), ==, dn
->dn_datablkshift
!= 0);
293 ASSERT3U((dn
->dn_nblkptr
- 1) * sizeof (blkptr_t
) +
294 dn
->dn_bonuslen
, <=, max_bonuslen
);
295 for (i
= 0; i
< TXG_SIZE
; i
++) {
296 ASSERT3U(dn
->dn_next_nlevels
[i
], <=, dn
->dn_nlevels
);
299 if (dn
->dn_phys
->dn_type
!= DMU_OT_NONE
)
300 ASSERT3U(dn
->dn_phys
->dn_nlevels
, <=, dn
->dn_nlevels
);
301 ASSERT(DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) || dn
->dn_dbuf
!= NULL
);
302 if (dn
->dn_dbuf
!= NULL
) {
303 ASSERT3P(dn
->dn_phys
, ==,
304 (dnode_phys_t
*)dn
->dn_dbuf
->db
.db_data
+
305 (dn
->dn_object
% (dn
->dn_dbuf
->db
.db_size
>> DNODE_SHIFT
)));
307 if (drop_struct_lock
)
308 rw_exit(&dn
->dn_struct_rwlock
);
313 dnode_byteswap(dnode_phys_t
*dnp
)
315 uint64_t *buf64
= (void*)&dnp
->dn_blkptr
;
318 if (dnp
->dn_type
== DMU_OT_NONE
) {
319 bzero(dnp
, sizeof (dnode_phys_t
));
323 dnp
->dn_datablkszsec
= BSWAP_16(dnp
->dn_datablkszsec
);
324 dnp
->dn_bonuslen
= BSWAP_16(dnp
->dn_bonuslen
);
325 dnp
->dn_extra_slots
= BSWAP_8(dnp
->dn_extra_slots
);
326 dnp
->dn_maxblkid
= BSWAP_64(dnp
->dn_maxblkid
);
327 dnp
->dn_used
= BSWAP_64(dnp
->dn_used
);
330 * dn_nblkptr is only one byte, so it's OK to read it in either
331 * byte order. We can't read dn_bouslen.
333 ASSERT(dnp
->dn_indblkshift
<= SPA_MAXBLOCKSHIFT
);
334 ASSERT(dnp
->dn_nblkptr
<= DN_MAX_NBLKPTR
);
335 for (i
= 0; i
< dnp
->dn_nblkptr
* sizeof (blkptr_t
)/8; i
++)
336 buf64
[i
] = BSWAP_64(buf64
[i
]);
339 * OK to check dn_bonuslen for zero, because it won't matter if
340 * we have the wrong byte order. This is necessary because the
341 * dnode dnode is smaller than a regular dnode.
343 if (dnp
->dn_bonuslen
!= 0) {
345 * Note that the bonus length calculated here may be
346 * longer than the actual bonus buffer. This is because
347 * we always put the bonus buffer after the last block
348 * pointer (instead of packing it against the end of the
351 int off
= (dnp
->dn_nblkptr
-1) * sizeof (blkptr_t
);
352 int slots
= dnp
->dn_extra_slots
+ 1;
353 size_t len
= DN_SLOTS_TO_BONUSLEN(slots
) - off
;
354 dmu_object_byteswap_t byteswap
;
355 ASSERT(DMU_OT_IS_VALID(dnp
->dn_bonustype
));
356 byteswap
= DMU_OT_BYTESWAP(dnp
->dn_bonustype
);
357 dmu_ot_byteswap
[byteswap
].ob_func(dnp
->dn_bonus
+ off
, len
);
360 /* Swap SPILL block if we have one */
361 if (dnp
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
)
362 byteswap_uint64_array(DN_SPILL_BLKPTR(dnp
), sizeof (blkptr_t
));
366 dnode_buf_byteswap(void *vbuf
, size_t size
)
370 ASSERT3U(sizeof (dnode_phys_t
), ==, (1<<DNODE_SHIFT
));
371 ASSERT((size
& (sizeof (dnode_phys_t
)-1)) == 0);
374 dnode_phys_t
*dnp
= (void *)(((char *)vbuf
) + i
);
378 if (dnp
->dn_type
!= DMU_OT_NONE
)
379 i
+= dnp
->dn_extra_slots
* DNODE_MIN_SIZE
;
384 dnode_setbonuslen(dnode_t
*dn
, int newsize
, dmu_tx_t
*tx
)
386 ASSERT3U(zfs_refcount_count(&dn
->dn_holds
), >=, 1);
388 dnode_setdirty(dn
, tx
);
389 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
390 ASSERT3U(newsize
, <=, DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
391 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
));
393 if (newsize
< dn
->dn_bonuslen
) {
394 /* clear any data after the end of the new size */
395 size_t diff
= dn
->dn_bonuslen
- newsize
;
396 char *data_end
= ((char *)dn
->dn_bonus
->db
.db_data
) + newsize
;
397 bzero(data_end
, diff
);
400 dn
->dn_bonuslen
= newsize
;
402 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = DN_ZERO_BONUSLEN
;
404 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonuslen
;
405 rw_exit(&dn
->dn_struct_rwlock
);
409 dnode_setbonus_type(dnode_t
*dn
, dmu_object_type_t newtype
, dmu_tx_t
*tx
)
411 ASSERT3U(zfs_refcount_count(&dn
->dn_holds
), >=, 1);
412 dnode_setdirty(dn
, tx
);
413 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
414 dn
->dn_bonustype
= newtype
;
415 dn
->dn_next_bonustype
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonustype
;
416 rw_exit(&dn
->dn_struct_rwlock
);
420 dnode_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
422 ASSERT3U(zfs_refcount_count(&dn
->dn_holds
), >=, 1);
423 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
424 dnode_setdirty(dn
, tx
);
425 dn
->dn_rm_spillblk
[tx
->tx_txg
& TXG_MASK
] = DN_KILL_SPILLBLK
;
426 dn
->dn_have_spill
= B_FALSE
;
430 dnode_setdblksz(dnode_t
*dn
, int size
)
432 ASSERT0(P2PHASE(size
, SPA_MINBLOCKSIZE
));
433 ASSERT3U(size
, <=, SPA_MAXBLOCKSIZE
);
434 ASSERT3U(size
, >=, SPA_MINBLOCKSIZE
);
435 ASSERT3U(size
>> SPA_MINBLOCKSHIFT
, <,
436 1<<(sizeof (dn
->dn_phys
->dn_datablkszsec
) * 8));
437 dn
->dn_datablksz
= size
;
438 dn
->dn_datablkszsec
= size
>> SPA_MINBLOCKSHIFT
;
439 dn
->dn_datablkshift
= ISP2(size
) ? highbit64(size
- 1) : 0;
443 dnode_create(objset_t
*os
, dnode_phys_t
*dnp
, dmu_buf_impl_t
*db
,
444 uint64_t object
, dnode_handle_t
*dnh
)
448 dn
= kmem_cache_alloc(dnode_cache
, KM_SLEEP
);
449 ASSERT(!POINTER_IS_VALID(dn
->dn_objset
));
453 * Defer setting dn_objset until the dnode is ready to be a candidate
454 * for the dnode_move() callback.
456 dn
->dn_object
= object
;
461 if (dnp
->dn_datablkszsec
) {
462 dnode_setdblksz(dn
, dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
);
464 dn
->dn_datablksz
= 0;
465 dn
->dn_datablkszsec
= 0;
466 dn
->dn_datablkshift
= 0;
468 dn
->dn_indblkshift
= dnp
->dn_indblkshift
;
469 dn
->dn_nlevels
= dnp
->dn_nlevels
;
470 dn
->dn_type
= dnp
->dn_type
;
471 dn
->dn_nblkptr
= dnp
->dn_nblkptr
;
472 dn
->dn_checksum
= dnp
->dn_checksum
;
473 dn
->dn_compress
= dnp
->dn_compress
;
474 dn
->dn_bonustype
= dnp
->dn_bonustype
;
475 dn
->dn_bonuslen
= dnp
->dn_bonuslen
;
476 dn
->dn_num_slots
= dnp
->dn_extra_slots
+ 1;
477 dn
->dn_maxblkid
= dnp
->dn_maxblkid
;
478 dn
->dn_have_spill
= ((dnp
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
) != 0);
481 dmu_zfetch_init(&dn
->dn_zfetch
, dn
);
483 ASSERT(DMU_OT_IS_VALID(dn
->dn_phys
->dn_type
));
484 ASSERT(zrl_is_locked(&dnh
->dnh_zrlock
));
485 ASSERT(!DN_SLOT_IS_PTR(dnh
->dnh_dnode
));
487 mutex_enter(&os
->os_lock
);
490 * Exclude special dnodes from os_dnodes so an empty os_dnodes
491 * signifies that the special dnodes have no references from
492 * their children (the entries in os_dnodes). This allows
493 * dnode_destroy() to easily determine if the last child has
494 * been removed and then complete eviction of the objset.
496 if (!DMU_OBJECT_IS_SPECIAL(object
))
497 list_insert_head(&os
->os_dnodes
, dn
);
501 * Everything else must be valid before assigning dn_objset
502 * makes the dnode eligible for dnode_move().
507 mutex_exit(&os
->os_lock
);
509 arc_space_consume(sizeof (dnode_t
), ARC_SPACE_DNODE
);
515 * Caller must be holding the dnode handle, which is released upon return.
518 dnode_destroy(dnode_t
*dn
)
520 objset_t
*os
= dn
->dn_objset
;
521 boolean_t complete_os_eviction
= B_FALSE
;
523 ASSERT((dn
->dn_id_flags
& DN_ID_NEW_EXIST
) == 0);
525 mutex_enter(&os
->os_lock
);
526 POINTER_INVALIDATE(&dn
->dn_objset
);
527 if (!DMU_OBJECT_IS_SPECIAL(dn
->dn_object
)) {
528 list_remove(&os
->os_dnodes
, dn
);
529 complete_os_eviction
=
530 list_is_empty(&os
->os_dnodes
) &&
531 list_link_active(&os
->os_evicting_node
);
533 mutex_exit(&os
->os_lock
);
535 /* the dnode can no longer move, so we can release the handle */
536 if (!zrl_is_locked(&dn
->dn_handle
->dnh_zrlock
))
537 zrl_remove(&dn
->dn_handle
->dnh_zrlock
);
539 dn
->dn_allocated_txg
= 0;
541 dn
->dn_assigned_txg
= 0;
542 dn
->dn_dirty_txg
= 0;
545 if (dn
->dn_dirtyctx_firstset
!= NULL
) {
546 kmem_free(dn
->dn_dirtyctx_firstset
, 1);
547 dn
->dn_dirtyctx_firstset
= NULL
;
549 if (dn
->dn_bonus
!= NULL
) {
550 mutex_enter(&dn
->dn_bonus
->db_mtx
);
551 dbuf_destroy(dn
->dn_bonus
);
556 dn
->dn_have_spill
= B_FALSE
;
561 dn
->dn_oldprojid
= ZFS_DEFAULT_PROJID
;
564 dn
->dn_newprojid
= ZFS_DEFAULT_PROJID
;
567 dmu_zfetch_fini(&dn
->dn_zfetch
);
568 kmem_cache_free(dnode_cache
, dn
);
569 arc_space_return(sizeof (dnode_t
), ARC_SPACE_DNODE
);
571 if (complete_os_eviction
)
572 dmu_objset_evict_done(os
);
576 dnode_allocate(dnode_t
*dn
, dmu_object_type_t ot
, int blocksize
, int ibs
,
577 dmu_object_type_t bonustype
, int bonuslen
, int dn_slots
, dmu_tx_t
*tx
)
581 ASSERT3U(dn_slots
, >, 0);
582 ASSERT3U(dn_slots
<< DNODE_SHIFT
, <=,
583 spa_maxdnodesize(dmu_objset_spa(dn
->dn_objset
)));
584 ASSERT3U(blocksize
, <=,
585 spa_maxblocksize(dmu_objset_spa(dn
->dn_objset
)));
587 blocksize
= 1 << zfs_default_bs
;
589 blocksize
= P2ROUNDUP(blocksize
, SPA_MINBLOCKSIZE
);
592 ibs
= zfs_default_ibs
;
594 ibs
= MIN(MAX(ibs
, DN_MIN_INDBLKSHIFT
), DN_MAX_INDBLKSHIFT
);
596 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d dn_slots=%d\n",
597 dn
->dn_objset
, dn
->dn_object
, tx
->tx_txg
, blocksize
, ibs
, dn_slots
);
598 DNODE_STAT_BUMP(dnode_allocate
);
600 ASSERT(dn
->dn_type
== DMU_OT_NONE
);
601 ASSERT(bcmp(dn
->dn_phys
, &dnode_phys_zero
, sizeof (dnode_phys_t
)) == 0);
602 ASSERT(dn
->dn_phys
->dn_type
== DMU_OT_NONE
);
603 ASSERT(ot
!= DMU_OT_NONE
);
604 ASSERT(DMU_OT_IS_VALID(ot
));
605 ASSERT((bonustype
== DMU_OT_NONE
&& bonuslen
== 0) ||
606 (bonustype
== DMU_OT_SA
&& bonuslen
== 0) ||
607 (bonustype
!= DMU_OT_NONE
&& bonuslen
!= 0));
608 ASSERT(DMU_OT_IS_VALID(bonustype
));
609 ASSERT3U(bonuslen
, <=, DN_SLOTS_TO_BONUSLEN(dn_slots
));
610 ASSERT(dn
->dn_type
== DMU_OT_NONE
);
611 ASSERT0(dn
->dn_maxblkid
);
612 ASSERT0(dn
->dn_allocated_txg
);
613 ASSERT0(dn
->dn_assigned_txg
);
614 ASSERT0(dn
->dn_dirty_txg
);
615 ASSERT(zfs_refcount_is_zero(&dn
->dn_tx_holds
));
616 ASSERT3U(zfs_refcount_count(&dn
->dn_holds
), <=, 1);
617 ASSERT(avl_is_empty(&dn
->dn_dbufs
));
619 for (i
= 0; i
< TXG_SIZE
; i
++) {
620 ASSERT0(dn
->dn_next_nblkptr
[i
]);
621 ASSERT0(dn
->dn_next_nlevels
[i
]);
622 ASSERT0(dn
->dn_next_indblkshift
[i
]);
623 ASSERT0(dn
->dn_next_bonuslen
[i
]);
624 ASSERT0(dn
->dn_next_bonustype
[i
]);
625 ASSERT0(dn
->dn_rm_spillblk
[i
]);
626 ASSERT0(dn
->dn_next_blksz
[i
]);
627 ASSERT0(dn
->dn_next_maxblkid
[i
]);
628 ASSERT(!multilist_link_active(&dn
->dn_dirty_link
[i
]));
629 ASSERT3P(list_head(&dn
->dn_dirty_records
[i
]), ==, NULL
);
630 ASSERT3P(dn
->dn_free_ranges
[i
], ==, NULL
);
634 dnode_setdblksz(dn
, blocksize
);
635 dn
->dn_indblkshift
= ibs
;
637 dn
->dn_num_slots
= dn_slots
;
638 if (bonustype
== DMU_OT_SA
) /* Maximize bonus space for SA */
641 dn
->dn_nblkptr
= MIN(DN_MAX_NBLKPTR
,
642 1 + ((DN_SLOTS_TO_BONUSLEN(dn_slots
) - bonuslen
) >>
646 dn
->dn_bonustype
= bonustype
;
647 dn
->dn_bonuslen
= bonuslen
;
648 dn
->dn_checksum
= ZIO_CHECKSUM_INHERIT
;
649 dn
->dn_compress
= ZIO_COMPRESS_INHERIT
;
653 if (dn
->dn_dirtyctx_firstset
) {
654 kmem_free(dn
->dn_dirtyctx_firstset
, 1);
655 dn
->dn_dirtyctx_firstset
= NULL
;
658 dn
->dn_allocated_txg
= tx
->tx_txg
;
661 dnode_setdirty(dn
, tx
);
662 dn
->dn_next_indblkshift
[tx
->tx_txg
& TXG_MASK
] = ibs
;
663 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonuslen
;
664 dn
->dn_next_bonustype
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonustype
;
665 dn
->dn_next_blksz
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_datablksz
;
669 dnode_reallocate(dnode_t
*dn
, dmu_object_type_t ot
, int blocksize
,
670 dmu_object_type_t bonustype
, int bonuslen
, int dn_slots
,
671 boolean_t keep_spill
, dmu_tx_t
*tx
)
675 ASSERT3U(blocksize
, >=, SPA_MINBLOCKSIZE
);
676 ASSERT3U(blocksize
, <=,
677 spa_maxblocksize(dmu_objset_spa(dn
->dn_objset
)));
678 ASSERT0(blocksize
% SPA_MINBLOCKSIZE
);
679 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
|| dmu_tx_private_ok(tx
));
680 ASSERT(tx
->tx_txg
!= 0);
681 ASSERT((bonustype
== DMU_OT_NONE
&& bonuslen
== 0) ||
682 (bonustype
!= DMU_OT_NONE
&& bonuslen
!= 0) ||
683 (bonustype
== DMU_OT_SA
&& bonuslen
== 0));
684 ASSERT(DMU_OT_IS_VALID(bonustype
));
685 ASSERT3U(bonuslen
, <=,
686 DN_BONUS_SIZE(spa_maxdnodesize(dmu_objset_spa(dn
->dn_objset
))));
687 ASSERT3U(bonuslen
, <=, DN_BONUS_SIZE(dn_slots
<< DNODE_SHIFT
));
689 dnode_free_interior_slots(dn
);
690 DNODE_STAT_BUMP(dnode_reallocate
);
692 /* clean up any unreferenced dbufs */
693 dnode_evict_dbufs(dn
);
697 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
698 dnode_setdirty(dn
, tx
);
699 if (dn
->dn_datablksz
!= blocksize
) {
700 /* change blocksize */
701 ASSERT0(dn
->dn_maxblkid
);
702 ASSERT(BP_IS_HOLE(&dn
->dn_phys
->dn_blkptr
[0]) ||
703 dnode_block_freed(dn
, 0));
705 dnode_setdblksz(dn
, blocksize
);
706 dn
->dn_next_blksz
[tx
->tx_txg
& TXG_MASK
] = blocksize
;
708 if (dn
->dn_bonuslen
!= bonuslen
)
709 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = bonuslen
;
711 if (bonustype
== DMU_OT_SA
) /* Maximize bonus space for SA */
714 nblkptr
= MIN(DN_MAX_NBLKPTR
,
715 1 + ((DN_SLOTS_TO_BONUSLEN(dn_slots
) - bonuslen
) >>
717 if (dn
->dn_bonustype
!= bonustype
)
718 dn
->dn_next_bonustype
[tx
->tx_txg
& TXG_MASK
] = bonustype
;
719 if (dn
->dn_nblkptr
!= nblkptr
)
720 dn
->dn_next_nblkptr
[tx
->tx_txg
& TXG_MASK
] = nblkptr
;
721 if (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
&& !keep_spill
) {
722 dbuf_rm_spill(dn
, tx
);
723 dnode_rm_spill(dn
, tx
);
726 rw_exit(&dn
->dn_struct_rwlock
);
731 /* change bonus size and type */
732 mutex_enter(&dn
->dn_mtx
);
733 dn
->dn_bonustype
= bonustype
;
734 dn
->dn_bonuslen
= bonuslen
;
735 dn
->dn_num_slots
= dn_slots
;
736 dn
->dn_nblkptr
= nblkptr
;
737 dn
->dn_checksum
= ZIO_CHECKSUM_INHERIT
;
738 dn
->dn_compress
= ZIO_COMPRESS_INHERIT
;
739 ASSERT3U(dn
->dn_nblkptr
, <=, DN_MAX_NBLKPTR
);
741 /* fix up the bonus db_size */
743 dn
->dn_bonus
->db
.db_size
=
744 DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
745 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
746 ASSERT(dn
->dn_bonuslen
<= dn
->dn_bonus
->db
.db_size
);
749 dn
->dn_allocated_txg
= tx
->tx_txg
;
750 mutex_exit(&dn
->dn_mtx
);
755 dnode_move_impl(dnode_t
*odn
, dnode_t
*ndn
)
759 ASSERT(!RW_LOCK_HELD(&odn
->dn_struct_rwlock
));
760 ASSERT(MUTEX_NOT_HELD(&odn
->dn_mtx
));
761 ASSERT(MUTEX_NOT_HELD(&odn
->dn_dbufs_mtx
));
762 ASSERT(!MUTEX_HELD(&odn
->dn_zfetch
.zf_lock
));
765 ndn
->dn_objset
= odn
->dn_objset
;
766 ndn
->dn_object
= odn
->dn_object
;
767 ndn
->dn_dbuf
= odn
->dn_dbuf
;
768 ndn
->dn_handle
= odn
->dn_handle
;
769 ndn
->dn_phys
= odn
->dn_phys
;
770 ndn
->dn_type
= odn
->dn_type
;
771 ndn
->dn_bonuslen
= odn
->dn_bonuslen
;
772 ndn
->dn_bonustype
= odn
->dn_bonustype
;
773 ndn
->dn_nblkptr
= odn
->dn_nblkptr
;
774 ndn
->dn_checksum
= odn
->dn_checksum
;
775 ndn
->dn_compress
= odn
->dn_compress
;
776 ndn
->dn_nlevels
= odn
->dn_nlevels
;
777 ndn
->dn_indblkshift
= odn
->dn_indblkshift
;
778 ndn
->dn_datablkshift
= odn
->dn_datablkshift
;
779 ndn
->dn_datablkszsec
= odn
->dn_datablkszsec
;
780 ndn
->dn_datablksz
= odn
->dn_datablksz
;
781 ndn
->dn_maxblkid
= odn
->dn_maxblkid
;
782 ndn
->dn_num_slots
= odn
->dn_num_slots
;
783 bcopy(&odn
->dn_next_type
[0], &ndn
->dn_next_type
[0],
784 sizeof (odn
->dn_next_type
));
785 bcopy(&odn
->dn_next_nblkptr
[0], &ndn
->dn_next_nblkptr
[0],
786 sizeof (odn
->dn_next_nblkptr
));
787 bcopy(&odn
->dn_next_nlevels
[0], &ndn
->dn_next_nlevels
[0],
788 sizeof (odn
->dn_next_nlevels
));
789 bcopy(&odn
->dn_next_indblkshift
[0], &ndn
->dn_next_indblkshift
[0],
790 sizeof (odn
->dn_next_indblkshift
));
791 bcopy(&odn
->dn_next_bonustype
[0], &ndn
->dn_next_bonustype
[0],
792 sizeof (odn
->dn_next_bonustype
));
793 bcopy(&odn
->dn_rm_spillblk
[0], &ndn
->dn_rm_spillblk
[0],
794 sizeof (odn
->dn_rm_spillblk
));
795 bcopy(&odn
->dn_next_bonuslen
[0], &ndn
->dn_next_bonuslen
[0],
796 sizeof (odn
->dn_next_bonuslen
));
797 bcopy(&odn
->dn_next_blksz
[0], &ndn
->dn_next_blksz
[0],
798 sizeof (odn
->dn_next_blksz
));
799 bcopy(&odn
->dn_next_maxblkid
[0], &ndn
->dn_next_maxblkid
[0],
800 sizeof (odn
->dn_next_maxblkid
));
801 for (i
= 0; i
< TXG_SIZE
; i
++) {
802 list_move_tail(&ndn
->dn_dirty_records
[i
],
803 &odn
->dn_dirty_records
[i
]);
805 bcopy(&odn
->dn_free_ranges
[0], &ndn
->dn_free_ranges
[0],
806 sizeof (odn
->dn_free_ranges
));
807 ndn
->dn_allocated_txg
= odn
->dn_allocated_txg
;
808 ndn
->dn_free_txg
= odn
->dn_free_txg
;
809 ndn
->dn_assigned_txg
= odn
->dn_assigned_txg
;
810 ndn
->dn_dirty_txg
= odn
->dn_dirty_txg
;
811 ndn
->dn_dirtyctx
= odn
->dn_dirtyctx
;
812 ndn
->dn_dirtyctx_firstset
= odn
->dn_dirtyctx_firstset
;
813 ASSERT(zfs_refcount_count(&odn
->dn_tx_holds
) == 0);
814 zfs_refcount_transfer(&ndn
->dn_holds
, &odn
->dn_holds
);
815 ASSERT(avl_is_empty(&ndn
->dn_dbufs
));
816 avl_swap(&ndn
->dn_dbufs
, &odn
->dn_dbufs
);
817 ndn
->dn_dbufs_count
= odn
->dn_dbufs_count
;
818 ndn
->dn_bonus
= odn
->dn_bonus
;
819 ndn
->dn_have_spill
= odn
->dn_have_spill
;
820 ndn
->dn_zio
= odn
->dn_zio
;
821 ndn
->dn_oldused
= odn
->dn_oldused
;
822 ndn
->dn_oldflags
= odn
->dn_oldflags
;
823 ndn
->dn_olduid
= odn
->dn_olduid
;
824 ndn
->dn_oldgid
= odn
->dn_oldgid
;
825 ndn
->dn_oldprojid
= odn
->dn_oldprojid
;
826 ndn
->dn_newuid
= odn
->dn_newuid
;
827 ndn
->dn_newgid
= odn
->dn_newgid
;
828 ndn
->dn_newprojid
= odn
->dn_newprojid
;
829 ndn
->dn_id_flags
= odn
->dn_id_flags
;
830 dmu_zfetch_init(&ndn
->dn_zfetch
, NULL
);
831 list_move_tail(&ndn
->dn_zfetch
.zf_stream
, &odn
->dn_zfetch
.zf_stream
);
832 ndn
->dn_zfetch
.zf_dnode
= odn
->dn_zfetch
.zf_dnode
;
835 * Update back pointers. Updating the handle fixes the back pointer of
836 * every descendant dbuf as well as the bonus dbuf.
838 ASSERT(ndn
->dn_handle
->dnh_dnode
== odn
);
839 ndn
->dn_handle
->dnh_dnode
= ndn
;
840 if (ndn
->dn_zfetch
.zf_dnode
== odn
) {
841 ndn
->dn_zfetch
.zf_dnode
= ndn
;
845 * Invalidate the original dnode by clearing all of its back pointers.
848 odn
->dn_handle
= NULL
;
849 avl_create(&odn
->dn_dbufs
, dbuf_compare
, sizeof (dmu_buf_impl_t
),
850 offsetof(dmu_buf_impl_t
, db_link
));
851 odn
->dn_dbufs_count
= 0;
852 odn
->dn_bonus
= NULL
;
853 dmu_zfetch_fini(&odn
->dn_zfetch
);
856 * Set the low bit of the objset pointer to ensure that dnode_move()
857 * recognizes the dnode as invalid in any subsequent callback.
859 POINTER_INVALIDATE(&odn
->dn_objset
);
862 * Satisfy the destructor.
864 for (i
= 0; i
< TXG_SIZE
; i
++) {
865 list_create(&odn
->dn_dirty_records
[i
],
866 sizeof (dbuf_dirty_record_t
),
867 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
868 odn
->dn_free_ranges
[i
] = NULL
;
869 odn
->dn_next_nlevels
[i
] = 0;
870 odn
->dn_next_indblkshift
[i
] = 0;
871 odn
->dn_next_bonustype
[i
] = 0;
872 odn
->dn_rm_spillblk
[i
] = 0;
873 odn
->dn_next_bonuslen
[i
] = 0;
874 odn
->dn_next_blksz
[i
] = 0;
876 odn
->dn_allocated_txg
= 0;
877 odn
->dn_free_txg
= 0;
878 odn
->dn_assigned_txg
= 0;
879 odn
->dn_dirty_txg
= 0;
880 odn
->dn_dirtyctx
= 0;
881 odn
->dn_dirtyctx_firstset
= NULL
;
882 odn
->dn_have_spill
= B_FALSE
;
885 odn
->dn_oldflags
= 0;
888 odn
->dn_oldprojid
= ZFS_DEFAULT_PROJID
;
891 odn
->dn_newprojid
= ZFS_DEFAULT_PROJID
;
892 odn
->dn_id_flags
= 0;
898 odn
->dn_moved
= (uint8_t)-1;
903 dnode_move(void *buf
, void *newbuf
, size_t size
, void *arg
)
905 dnode_t
*odn
= buf
, *ndn
= newbuf
;
911 * The dnode is on the objset's list of known dnodes if the objset
912 * pointer is valid. We set the low bit of the objset pointer when
913 * freeing the dnode to invalidate it, and the memory patterns written
914 * by kmem (baddcafe and deadbeef) set at least one of the two low bits.
915 * A newly created dnode sets the objset pointer last of all to indicate
916 * that the dnode is known and in a valid state to be moved by this
920 if (!POINTER_IS_VALID(os
)) {
921 DNODE_STAT_BUMP(dnode_move_invalid
);
922 return (KMEM_CBRC_DONT_KNOW
);
926 * Ensure that the objset does not go away during the move.
928 rw_enter(&os_lock
, RW_WRITER
);
929 if (os
!= odn
->dn_objset
) {
931 DNODE_STAT_BUMP(dnode_move_recheck1
);
932 return (KMEM_CBRC_DONT_KNOW
);
936 * If the dnode is still valid, then so is the objset. We know that no
937 * valid objset can be freed while we hold os_lock, so we can safely
938 * ensure that the objset remains in use.
940 mutex_enter(&os
->os_lock
);
943 * Recheck the objset pointer in case the dnode was removed just before
944 * acquiring the lock.
946 if (os
!= odn
->dn_objset
) {
947 mutex_exit(&os
->os_lock
);
949 DNODE_STAT_BUMP(dnode_move_recheck2
);
950 return (KMEM_CBRC_DONT_KNOW
);
954 * At this point we know that as long as we hold os->os_lock, the dnode
955 * cannot be freed and fields within the dnode can be safely accessed.
956 * The objset listing this dnode cannot go away as long as this dnode is
960 if (DMU_OBJECT_IS_SPECIAL(odn
->dn_object
)) {
961 mutex_exit(&os
->os_lock
);
962 DNODE_STAT_BUMP(dnode_move_special
);
963 return (KMEM_CBRC_NO
);
965 ASSERT(odn
->dn_dbuf
!= NULL
); /* only "special" dnodes have no parent */
968 * Lock the dnode handle to prevent the dnode from obtaining any new
969 * holds. This also prevents the descendant dbufs and the bonus dbuf
970 * from accessing the dnode, so that we can discount their holds. The
971 * handle is safe to access because we know that while the dnode cannot
972 * go away, neither can its handle. Once we hold dnh_zrlock, we can
973 * safely move any dnode referenced only by dbufs.
975 if (!zrl_tryenter(&odn
->dn_handle
->dnh_zrlock
)) {
976 mutex_exit(&os
->os_lock
);
977 DNODE_STAT_BUMP(dnode_move_handle
);
978 return (KMEM_CBRC_LATER
);
982 * Ensure a consistent view of the dnode's holds and the dnode's dbufs.
983 * We need to guarantee that there is a hold for every dbuf in order to
984 * determine whether the dnode is actively referenced. Falsely matching
985 * a dbuf to an active hold would lead to an unsafe move. It's possible
986 * that a thread already having an active dnode hold is about to add a
987 * dbuf, and we can't compare hold and dbuf counts while the add is in
990 if (!rw_tryenter(&odn
->dn_struct_rwlock
, RW_WRITER
)) {
991 zrl_exit(&odn
->dn_handle
->dnh_zrlock
);
992 mutex_exit(&os
->os_lock
);
993 DNODE_STAT_BUMP(dnode_move_rwlock
);
994 return (KMEM_CBRC_LATER
);
998 * A dbuf may be removed (evicted) without an active dnode hold. In that
999 * case, the dbuf count is decremented under the handle lock before the
1000 * dbuf's hold is released. This order ensures that if we count the hold
1001 * after the dbuf is removed but before its hold is released, we will
1002 * treat the unmatched hold as active and exit safely. If we count the
1003 * hold before the dbuf is removed, the hold is discounted, and the
1004 * removal is blocked until the move completes.
1006 refcount
= zfs_refcount_count(&odn
->dn_holds
);
1007 ASSERT(refcount
>= 0);
1008 dbufs
= odn
->dn_dbufs_count
;
1010 /* We can't have more dbufs than dnode holds. */
1011 ASSERT3U(dbufs
, <=, refcount
);
1012 DTRACE_PROBE3(dnode__move
, dnode_t
*, odn
, int64_t, refcount
,
1015 if (refcount
> dbufs
) {
1016 rw_exit(&odn
->dn_struct_rwlock
);
1017 zrl_exit(&odn
->dn_handle
->dnh_zrlock
);
1018 mutex_exit(&os
->os_lock
);
1019 DNODE_STAT_BUMP(dnode_move_active
);
1020 return (KMEM_CBRC_LATER
);
1023 rw_exit(&odn
->dn_struct_rwlock
);
1026 * At this point we know that anyone with a hold on the dnode is not
1027 * actively referencing it. The dnode is known and in a valid state to
1028 * move. We're holding the locks needed to execute the critical section.
1030 dnode_move_impl(odn
, ndn
);
1032 list_link_replace(&odn
->dn_link
, &ndn
->dn_link
);
1033 /* If the dnode was safe to move, the refcount cannot have changed. */
1034 ASSERT(refcount
== zfs_refcount_count(&ndn
->dn_holds
));
1035 ASSERT(dbufs
== ndn
->dn_dbufs_count
);
1036 zrl_exit(&ndn
->dn_handle
->dnh_zrlock
); /* handle has moved */
1037 mutex_exit(&os
->os_lock
);
1039 return (KMEM_CBRC_YES
);
1041 #endif /* _KERNEL */
1044 dnode_slots_hold(dnode_children_t
*children
, int idx
, int slots
)
1046 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1048 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1049 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1050 zrl_add(&dnh
->dnh_zrlock
);
1055 dnode_slots_rele(dnode_children_t
*children
, int idx
, int slots
)
1057 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1059 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1060 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1062 if (zrl_is_locked(&dnh
->dnh_zrlock
))
1063 zrl_exit(&dnh
->dnh_zrlock
);
1065 zrl_remove(&dnh
->dnh_zrlock
);
1070 dnode_slots_tryenter(dnode_children_t
*children
, int idx
, int slots
)
1072 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1074 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1075 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1077 if (!zrl_tryenter(&dnh
->dnh_zrlock
)) {
1078 for (int j
= idx
; j
< i
; j
++) {
1079 dnh
= &children
->dnc_children
[j
];
1080 zrl_exit(&dnh
->dnh_zrlock
);
1091 dnode_set_slots(dnode_children_t
*children
, int idx
, int slots
, void *ptr
)
1093 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1095 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1096 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1097 dnh
->dnh_dnode
= ptr
;
1102 dnode_check_slots_free(dnode_children_t
*children
, int idx
, int slots
)
1104 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1107 * If all dnode slots are either already free or
1108 * evictable return B_TRUE.
1110 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1111 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1112 dnode_t
*dn
= dnh
->dnh_dnode
;
1114 if (dn
== DN_SLOT_FREE
) {
1116 } else if (DN_SLOT_IS_PTR(dn
)) {
1117 mutex_enter(&dn
->dn_mtx
);
1118 boolean_t can_free
= (dn
->dn_type
== DMU_OT_NONE
&&
1119 zfs_refcount_is_zero(&dn
->dn_holds
) &&
1120 !DNODE_IS_DIRTY(dn
));
1121 mutex_exit(&dn
->dn_mtx
);
1136 dnode_reclaim_slots(dnode_children_t
*children
, int idx
, int slots
)
1138 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1140 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1141 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1143 ASSERT(zrl_is_locked(&dnh
->dnh_zrlock
));
1145 if (DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1146 ASSERT3S(dnh
->dnh_dnode
->dn_type
, ==, DMU_OT_NONE
);
1147 dnode_destroy(dnh
->dnh_dnode
);
1148 dnh
->dnh_dnode
= DN_SLOT_FREE
;
1154 dnode_free_interior_slots(dnode_t
*dn
)
1156 dnode_children_t
*children
= dmu_buf_get_user(&dn
->dn_dbuf
->db
);
1157 int epb
= dn
->dn_dbuf
->db
.db_size
>> DNODE_SHIFT
;
1158 int idx
= (dn
->dn_object
& (epb
- 1)) + 1;
1159 int slots
= dn
->dn_num_slots
- 1;
1164 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1166 while (!dnode_slots_tryenter(children
, idx
, slots
)) {
1167 DNODE_STAT_BUMP(dnode_free_interior_lock_retry
);
1171 dnode_set_slots(children
, idx
, slots
, DN_SLOT_FREE
);
1172 dnode_slots_rele(children
, idx
, slots
);
1176 dnode_special_close(dnode_handle_t
*dnh
)
1178 dnode_t
*dn
= dnh
->dnh_dnode
;
1181 * Wait for final references to the dnode to clear. This can
1182 * only happen if the arc is asynchronously evicting state that
1183 * has a hold on this dnode while we are trying to evict this
1186 while (zfs_refcount_count(&dn
->dn_holds
) > 0)
1188 ASSERT(dn
->dn_dbuf
== NULL
||
1189 dmu_buf_get_user(&dn
->dn_dbuf
->db
) == NULL
);
1190 zrl_add(&dnh
->dnh_zrlock
);
1191 dnode_destroy(dn
); /* implicit zrl_remove() */
1192 zrl_destroy(&dnh
->dnh_zrlock
);
1193 dnh
->dnh_dnode
= NULL
;
1197 dnode_special_open(objset_t
*os
, dnode_phys_t
*dnp
, uint64_t object
,
1198 dnode_handle_t
*dnh
)
1202 zrl_init(&dnh
->dnh_zrlock
);
1203 zrl_tryenter(&dnh
->dnh_zrlock
);
1205 dn
= dnode_create(os
, dnp
, NULL
, object
, dnh
);
1208 zrl_exit(&dnh
->dnh_zrlock
);
1212 dnode_buf_evict_async(void *dbu
)
1214 dnode_children_t
*dnc
= dbu
;
1216 DNODE_STAT_BUMP(dnode_buf_evict
);
1218 for (int i
= 0; i
< dnc
->dnc_count
; i
++) {
1219 dnode_handle_t
*dnh
= &dnc
->dnc_children
[i
];
1223 * The dnode handle lock guards against the dnode moving to
1224 * another valid address, so there is no need here to guard
1225 * against changes to or from NULL.
1227 if (!DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1228 zrl_destroy(&dnh
->dnh_zrlock
);
1229 dnh
->dnh_dnode
= DN_SLOT_UNINIT
;
1233 zrl_add(&dnh
->dnh_zrlock
);
1234 dn
= dnh
->dnh_dnode
;
1236 * If there are holds on this dnode, then there should
1237 * be holds on the dnode's containing dbuf as well; thus
1238 * it wouldn't be eligible for eviction and this function
1239 * would not have been called.
1241 ASSERT(zfs_refcount_is_zero(&dn
->dn_holds
));
1242 ASSERT(zfs_refcount_is_zero(&dn
->dn_tx_holds
));
1244 dnode_destroy(dn
); /* implicit zrl_remove() for first slot */
1245 zrl_destroy(&dnh
->dnh_zrlock
);
1246 dnh
->dnh_dnode
= DN_SLOT_UNINIT
;
1248 kmem_free(dnc
, sizeof (dnode_children_t
) +
1249 dnc
->dnc_count
* sizeof (dnode_handle_t
));
1253 * When the DNODE_MUST_BE_FREE flag is set, the "slots" parameter is used
1254 * to ensure the hole at the specified object offset is large enough to
1255 * hold the dnode being created. The slots parameter is also used to ensure
1256 * a dnode does not span multiple dnode blocks. In both of these cases, if
1257 * a failure occurs, ENOSPC is returned. Keep in mind, these failure cases
1258 * are only possible when using DNODE_MUST_BE_FREE.
1260 * If the DNODE_MUST_BE_ALLOCATED flag is set, "slots" must be 0.
1261 * dnode_hold_impl() will check if the requested dnode is already consumed
1262 * as an extra dnode slot by an large dnode, in which case it returns
1265 * If the DNODE_DRY_RUN flag is set, we don't actually hold the dnode, just
1266 * return whether the hold would succeed or not. tag and dnp should set to
1267 * NULL in this case.
1270 * EINVAL - Invalid object number or flags.
1271 * ENOSPC - Hole too small to fulfill "slots" request (DNODE_MUST_BE_FREE)
1272 * EEXIST - Refers to an allocated dnode (DNODE_MUST_BE_FREE)
1273 * - Refers to a freeing dnode (DNODE_MUST_BE_FREE)
1274 * - Refers to an interior dnode slot (DNODE_MUST_BE_ALLOCATED)
1275 * ENOENT - The requested dnode is not allocated (DNODE_MUST_BE_ALLOCATED)
1276 * - The requested dnode is being freed (DNODE_MUST_BE_ALLOCATED)
1277 * EIO - I/O error when reading the meta dnode dbuf.
1279 * succeeds even for free dnodes.
1282 dnode_hold_impl(objset_t
*os
, uint64_t object
, int flag
, int slots
,
1283 void *tag
, dnode_t
**dnp
)
1286 int drop_struct_lock
= FALSE
;
1291 dnode_children_t
*dnc
;
1292 dnode_phys_t
*dn_block
;
1293 dnode_handle_t
*dnh
;
1295 ASSERT(!(flag
& DNODE_MUST_BE_ALLOCATED
) || (slots
== 0));
1296 ASSERT(!(flag
& DNODE_MUST_BE_FREE
) || (slots
> 0));
1297 IMPLY(flag
& DNODE_DRY_RUN
, (tag
== NULL
) && (dnp
== NULL
));
1300 * If you are holding the spa config lock as writer, you shouldn't
1301 * be asking the DMU to do *anything* unless it's the root pool
1302 * which may require us to read from the root filesystem while
1303 * holding some (not all) of the locks as writer.
1305 ASSERT(spa_config_held(os
->os_spa
, SCL_ALL
, RW_WRITER
) == 0 ||
1306 (spa_is_root(os
->os_spa
) &&
1307 spa_config_held(os
->os_spa
, SCL_STATE
, RW_WRITER
)));
1309 ASSERT((flag
& DNODE_MUST_BE_ALLOCATED
) || (flag
& DNODE_MUST_BE_FREE
));
1311 if (object
== DMU_USERUSED_OBJECT
|| object
== DMU_GROUPUSED_OBJECT
||
1312 object
== DMU_PROJECTUSED_OBJECT
) {
1313 if (object
== DMU_USERUSED_OBJECT
)
1314 dn
= DMU_USERUSED_DNODE(os
);
1315 else if (object
== DMU_GROUPUSED_OBJECT
)
1316 dn
= DMU_GROUPUSED_DNODE(os
);
1318 dn
= DMU_PROJECTUSED_DNODE(os
);
1320 return (SET_ERROR(ENOENT
));
1322 if ((flag
& DNODE_MUST_BE_ALLOCATED
) && type
== DMU_OT_NONE
)
1323 return (SET_ERROR(ENOENT
));
1324 if ((flag
& DNODE_MUST_BE_FREE
) && type
!= DMU_OT_NONE
)
1325 return (SET_ERROR(EEXIST
));
1327 /* Don't actually hold if dry run, just return 0 */
1328 if (!(flag
& DNODE_DRY_RUN
)) {
1329 (void) zfs_refcount_add(&dn
->dn_holds
, tag
);
1335 if (object
== 0 || object
>= DN_MAX_OBJECT
)
1336 return (SET_ERROR(EINVAL
));
1338 mdn
= DMU_META_DNODE(os
);
1339 ASSERT(mdn
->dn_object
== DMU_META_DNODE_OBJECT
);
1343 if (!RW_WRITE_HELD(&mdn
->dn_struct_rwlock
)) {
1344 rw_enter(&mdn
->dn_struct_rwlock
, RW_READER
);
1345 drop_struct_lock
= TRUE
;
1348 blk
= dbuf_whichblock(mdn
, 0, object
* sizeof (dnode_phys_t
));
1349 db
= dbuf_hold(mdn
, blk
, FTAG
);
1350 if (drop_struct_lock
)
1351 rw_exit(&mdn
->dn_struct_rwlock
);
1353 DNODE_STAT_BUMP(dnode_hold_dbuf_hold
);
1354 return (SET_ERROR(EIO
));
1358 * We do not need to decrypt to read the dnode so it doesn't matter
1359 * if we get the encrypted or decrypted version.
1361 err
= dbuf_read(db
, NULL
, DB_RF_CANFAIL
| DB_RF_NO_DECRYPT
);
1363 DNODE_STAT_BUMP(dnode_hold_dbuf_read
);
1364 dbuf_rele(db
, FTAG
);
1368 ASSERT3U(db
->db
.db_size
, >=, 1<<DNODE_SHIFT
);
1369 epb
= db
->db
.db_size
>> DNODE_SHIFT
;
1371 idx
= object
& (epb
- 1);
1372 dn_block
= (dnode_phys_t
*)db
->db
.db_data
;
1374 ASSERT(DB_DNODE(db
)->dn_type
== DMU_OT_DNODE
);
1375 dnc
= dmu_buf_get_user(&db
->db
);
1378 dnode_children_t
*winner
;
1381 dnc
= kmem_zalloc(sizeof (dnode_children_t
) +
1382 epb
* sizeof (dnode_handle_t
), KM_SLEEP
);
1383 dnc
->dnc_count
= epb
;
1384 dnh
= &dnc
->dnc_children
[0];
1386 /* Initialize dnode slot status from dnode_phys_t */
1387 for (int i
= 0; i
< epb
; i
++) {
1388 zrl_init(&dnh
[i
].dnh_zrlock
);
1395 if (dn_block
[i
].dn_type
!= DMU_OT_NONE
) {
1396 int interior
= dn_block
[i
].dn_extra_slots
;
1398 dnode_set_slots(dnc
, i
, 1, DN_SLOT_ALLOCATED
);
1399 dnode_set_slots(dnc
, i
+ 1, interior
,
1403 dnh
[i
].dnh_dnode
= DN_SLOT_FREE
;
1408 dmu_buf_init_user(&dnc
->dnc_dbu
, NULL
,
1409 dnode_buf_evict_async
, NULL
);
1410 winner
= dmu_buf_set_user(&db
->db
, &dnc
->dnc_dbu
);
1411 if (winner
!= NULL
) {
1413 for (int i
= 0; i
< epb
; i
++)
1414 zrl_destroy(&dnh
[i
].dnh_zrlock
);
1416 kmem_free(dnc
, sizeof (dnode_children_t
) +
1417 epb
* sizeof (dnode_handle_t
));
1422 ASSERT(dnc
->dnc_count
== epb
);
1424 if (flag
& DNODE_MUST_BE_ALLOCATED
) {
1427 dnode_slots_hold(dnc
, idx
, slots
);
1428 dnh
= &dnc
->dnc_children
[idx
];
1430 if (DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1431 dn
= dnh
->dnh_dnode
;
1432 } else if (dnh
->dnh_dnode
== DN_SLOT_INTERIOR
) {
1433 DNODE_STAT_BUMP(dnode_hold_alloc_interior
);
1434 dnode_slots_rele(dnc
, idx
, slots
);
1435 dbuf_rele(db
, FTAG
);
1436 return (SET_ERROR(EEXIST
));
1437 } else if (dnh
->dnh_dnode
!= DN_SLOT_ALLOCATED
) {
1438 DNODE_STAT_BUMP(dnode_hold_alloc_misses
);
1439 dnode_slots_rele(dnc
, idx
, slots
);
1440 dbuf_rele(db
, FTAG
);
1441 return (SET_ERROR(ENOENT
));
1443 dnode_slots_rele(dnc
, idx
, slots
);
1444 while (!dnode_slots_tryenter(dnc
, idx
, slots
)) {
1445 DNODE_STAT_BUMP(dnode_hold_alloc_lock_retry
);
1450 * Someone else won the race and called dnode_create()
1451 * after we checked DN_SLOT_IS_PTR() above but before
1452 * we acquired the lock.
1454 if (DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1455 DNODE_STAT_BUMP(dnode_hold_alloc_lock_misses
);
1456 dn
= dnh
->dnh_dnode
;
1458 dn
= dnode_create(os
, dn_block
+ idx
, db
,
1463 mutex_enter(&dn
->dn_mtx
);
1464 if (dn
->dn_type
== DMU_OT_NONE
|| dn
->dn_free_txg
!= 0) {
1465 DNODE_STAT_BUMP(dnode_hold_alloc_type_none
);
1466 mutex_exit(&dn
->dn_mtx
);
1467 dnode_slots_rele(dnc
, idx
, slots
);
1468 dbuf_rele(db
, FTAG
);
1469 return (SET_ERROR(ENOENT
));
1472 /* Don't actually hold if dry run, just return 0 */
1473 if (flag
& DNODE_DRY_RUN
) {
1474 mutex_exit(&dn
->dn_mtx
);
1475 dnode_slots_rele(dnc
, idx
, slots
);
1476 dbuf_rele(db
, FTAG
);
1480 DNODE_STAT_BUMP(dnode_hold_alloc_hits
);
1481 } else if (flag
& DNODE_MUST_BE_FREE
) {
1483 if (idx
+ slots
- 1 >= DNODES_PER_BLOCK
) {
1484 DNODE_STAT_BUMP(dnode_hold_free_overflow
);
1485 dbuf_rele(db
, FTAG
);
1486 return (SET_ERROR(ENOSPC
));
1489 dnode_slots_hold(dnc
, idx
, slots
);
1491 if (!dnode_check_slots_free(dnc
, idx
, slots
)) {
1492 DNODE_STAT_BUMP(dnode_hold_free_misses
);
1493 dnode_slots_rele(dnc
, idx
, slots
);
1494 dbuf_rele(db
, FTAG
);
1495 return (SET_ERROR(ENOSPC
));
1498 dnode_slots_rele(dnc
, idx
, slots
);
1499 while (!dnode_slots_tryenter(dnc
, idx
, slots
)) {
1500 DNODE_STAT_BUMP(dnode_hold_free_lock_retry
);
1504 if (!dnode_check_slots_free(dnc
, idx
, slots
)) {
1505 DNODE_STAT_BUMP(dnode_hold_free_lock_misses
);
1506 dnode_slots_rele(dnc
, idx
, slots
);
1507 dbuf_rele(db
, FTAG
);
1508 return (SET_ERROR(ENOSPC
));
1512 * Allocated but otherwise free dnodes which would
1513 * be in the interior of a multi-slot dnodes need
1514 * to be freed. Single slot dnodes can be safely
1515 * re-purposed as a performance optimization.
1518 dnode_reclaim_slots(dnc
, idx
+ 1, slots
- 1);
1520 dnh
= &dnc
->dnc_children
[idx
];
1521 if (DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1522 dn
= dnh
->dnh_dnode
;
1524 dn
= dnode_create(os
, dn_block
+ idx
, db
,
1528 mutex_enter(&dn
->dn_mtx
);
1529 if (!zfs_refcount_is_zero(&dn
->dn_holds
) || dn
->dn_free_txg
) {
1530 DNODE_STAT_BUMP(dnode_hold_free_refcount
);
1531 mutex_exit(&dn
->dn_mtx
);
1532 dnode_slots_rele(dnc
, idx
, slots
);
1533 dbuf_rele(db
, FTAG
);
1534 return (SET_ERROR(EEXIST
));
1537 /* Don't actually hold if dry run, just return 0 */
1538 if (flag
& DNODE_DRY_RUN
) {
1539 mutex_exit(&dn
->dn_mtx
);
1540 dnode_slots_rele(dnc
, idx
, slots
);
1541 dbuf_rele(db
, FTAG
);
1545 dnode_set_slots(dnc
, idx
+ 1, slots
- 1, DN_SLOT_INTERIOR
);
1546 DNODE_STAT_BUMP(dnode_hold_free_hits
);
1548 dbuf_rele(db
, FTAG
);
1549 return (SET_ERROR(EINVAL
));
1552 ASSERT0(dn
->dn_free_txg
);
1554 if (zfs_refcount_add(&dn
->dn_holds
, tag
) == 1)
1555 dbuf_add_ref(db
, dnh
);
1557 mutex_exit(&dn
->dn_mtx
);
1559 /* Now we can rely on the hold to prevent the dnode from moving. */
1560 dnode_slots_rele(dnc
, idx
, slots
);
1563 ASSERT3P(dn
->dn_dbuf
, ==, db
);
1564 ASSERT3U(dn
->dn_object
, ==, object
);
1565 dbuf_rele(db
, FTAG
);
1572 * Return held dnode if the object is allocated, NULL if not.
1575 dnode_hold(objset_t
*os
, uint64_t object
, void *tag
, dnode_t
**dnp
)
1577 return (dnode_hold_impl(os
, object
, DNODE_MUST_BE_ALLOCATED
, 0, tag
,
1582 * Can only add a reference if there is already at least one
1583 * reference on the dnode. Returns FALSE if unable to add a
1587 dnode_add_ref(dnode_t
*dn
, void *tag
)
1589 mutex_enter(&dn
->dn_mtx
);
1590 if (zfs_refcount_is_zero(&dn
->dn_holds
)) {
1591 mutex_exit(&dn
->dn_mtx
);
1594 VERIFY(1 < zfs_refcount_add(&dn
->dn_holds
, tag
));
1595 mutex_exit(&dn
->dn_mtx
);
1600 dnode_rele(dnode_t
*dn
, void *tag
)
1602 mutex_enter(&dn
->dn_mtx
);
1603 dnode_rele_and_unlock(dn
, tag
, B_FALSE
);
1607 dnode_rele_and_unlock(dnode_t
*dn
, void *tag
, boolean_t evicting
)
1610 /* Get while the hold prevents the dnode from moving. */
1611 dmu_buf_impl_t
*db
= dn
->dn_dbuf
;
1612 dnode_handle_t
*dnh
= dn
->dn_handle
;
1614 refs
= zfs_refcount_remove(&dn
->dn_holds
, tag
);
1615 mutex_exit(&dn
->dn_mtx
);
1618 * It's unsafe to release the last hold on a dnode by dnode_rele() or
1619 * indirectly by dbuf_rele() while relying on the dnode handle to
1620 * prevent the dnode from moving, since releasing the last hold could
1621 * result in the dnode's parent dbuf evicting its dnode handles. For
1622 * that reason anyone calling dnode_rele() or dbuf_rele() without some
1623 * other direct or indirect hold on the dnode must first drop the dnode
1626 ASSERT(refs
> 0 || dnh
->dnh_zrlock
.zr_owner
!= curthread
);
1628 /* NOTE: the DNODE_DNODE does not have a dn_dbuf */
1629 if (refs
== 0 && db
!= NULL
) {
1631 * Another thread could add a hold to the dnode handle in
1632 * dnode_hold_impl() while holding the parent dbuf. Since the
1633 * hold on the parent dbuf prevents the handle from being
1634 * destroyed, the hold on the handle is OK. We can't yet assert
1635 * that the handle has zero references, but that will be
1636 * asserted anyway when the handle gets destroyed.
1638 mutex_enter(&db
->db_mtx
);
1639 dbuf_rele_and_unlock(db
, dnh
, evicting
);
1644 * Test whether we can create a dnode at the specified location.
1647 dnode_try_claim(objset_t
*os
, uint64_t object
, int slots
)
1649 return (dnode_hold_impl(os
, object
, DNODE_MUST_BE_FREE
| DNODE_DRY_RUN
,
1650 slots
, NULL
, NULL
));
1654 dnode_setdirty(dnode_t
*dn
, dmu_tx_t
*tx
)
1656 objset_t
*os
= dn
->dn_objset
;
1657 uint64_t txg
= tx
->tx_txg
;
1659 if (DMU_OBJECT_IS_SPECIAL(dn
->dn_object
)) {
1660 dsl_dataset_dirty(os
->os_dsl_dataset
, tx
);
1667 mutex_enter(&dn
->dn_mtx
);
1668 ASSERT(dn
->dn_phys
->dn_type
|| dn
->dn_allocated_txg
);
1669 ASSERT(dn
->dn_free_txg
== 0 || dn
->dn_free_txg
>= txg
);
1670 mutex_exit(&dn
->dn_mtx
);
1674 * Determine old uid/gid when necessary
1676 dmu_objset_userquota_get_ids(dn
, B_TRUE
, tx
);
1678 multilist_t
*dirtylist
= os
->os_dirty_dnodes
[txg
& TXG_MASK
];
1679 multilist_sublist_t
*mls
= multilist_sublist_lock_obj(dirtylist
, dn
);
1682 * If we are already marked dirty, we're done.
1684 if (multilist_link_active(&dn
->dn_dirty_link
[txg
& TXG_MASK
])) {
1685 multilist_sublist_unlock(mls
);
1689 ASSERT(!zfs_refcount_is_zero(&dn
->dn_holds
) ||
1690 !avl_is_empty(&dn
->dn_dbufs
));
1691 ASSERT(dn
->dn_datablksz
!= 0);
1692 ASSERT0(dn
->dn_next_bonuslen
[txg
& TXG_MASK
]);
1693 ASSERT0(dn
->dn_next_blksz
[txg
& TXG_MASK
]);
1694 ASSERT0(dn
->dn_next_bonustype
[txg
& TXG_MASK
]);
1696 dprintf_ds(os
->os_dsl_dataset
, "obj=%llu txg=%llu\n",
1697 dn
->dn_object
, txg
);
1699 multilist_sublist_insert_head(mls
, dn
);
1701 multilist_sublist_unlock(mls
);
1704 * The dnode maintains a hold on its containing dbuf as
1705 * long as there are holds on it. Each instantiated child
1706 * dbuf maintains a hold on the dnode. When the last child
1707 * drops its hold, the dnode will drop its hold on the
1708 * containing dbuf. We add a "dirty hold" here so that the
1709 * dnode will hang around after we finish processing its
1712 VERIFY(dnode_add_ref(dn
, (void *)(uintptr_t)tx
->tx_txg
));
1714 (void) dbuf_dirty(dn
->dn_dbuf
, tx
);
1716 dsl_dataset_dirty(os
->os_dsl_dataset
, tx
);
1720 dnode_free(dnode_t
*dn
, dmu_tx_t
*tx
)
1722 mutex_enter(&dn
->dn_mtx
);
1723 if (dn
->dn_type
== DMU_OT_NONE
|| dn
->dn_free_txg
) {
1724 mutex_exit(&dn
->dn_mtx
);
1727 dn
->dn_free_txg
= tx
->tx_txg
;
1728 mutex_exit(&dn
->dn_mtx
);
1730 dnode_setdirty(dn
, tx
);
1734 * Try to change the block size for the indicated dnode. This can only
1735 * succeed if there are no blocks allocated or dirty beyond first block
1738 dnode_set_blksz(dnode_t
*dn
, uint64_t size
, int ibs
, dmu_tx_t
*tx
)
1743 ASSERT3U(size
, <=, spa_maxblocksize(dmu_objset_spa(dn
->dn_objset
)));
1745 size
= SPA_MINBLOCKSIZE
;
1747 size
= P2ROUNDUP(size
, SPA_MINBLOCKSIZE
);
1749 if (ibs
== dn
->dn_indblkshift
)
1752 if (size
>> SPA_MINBLOCKSHIFT
== dn
->dn_datablkszsec
&& ibs
== 0)
1755 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1757 /* Check for any allocated blocks beyond the first */
1758 if (dn
->dn_maxblkid
!= 0)
1761 mutex_enter(&dn
->dn_dbufs_mtx
);
1762 for (db
= avl_first(&dn
->dn_dbufs
); db
!= NULL
;
1763 db
= AVL_NEXT(&dn
->dn_dbufs
, db
)) {
1764 if (db
->db_blkid
!= 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1765 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1766 mutex_exit(&dn
->dn_dbufs_mtx
);
1770 mutex_exit(&dn
->dn_dbufs_mtx
);
1772 if (ibs
&& dn
->dn_nlevels
!= 1)
1775 /* resize the old block */
1776 err
= dbuf_hold_impl(dn
, 0, 0, TRUE
, FALSE
, FTAG
, &db
);
1778 dbuf_new_size(db
, size
, tx
);
1779 } else if (err
!= ENOENT
) {
1783 dnode_setdblksz(dn
, size
);
1784 dnode_setdirty(dn
, tx
);
1785 dn
->dn_next_blksz
[tx
->tx_txg
&TXG_MASK
] = size
;
1787 dn
->dn_indblkshift
= ibs
;
1788 dn
->dn_next_indblkshift
[tx
->tx_txg
&TXG_MASK
] = ibs
;
1790 /* release after we have fixed the blocksize in the dnode */
1792 dbuf_rele(db
, FTAG
);
1794 rw_exit(&dn
->dn_struct_rwlock
);
1798 rw_exit(&dn
->dn_struct_rwlock
);
1799 return (SET_ERROR(ENOTSUP
));
1803 dnode_set_nlevels_impl(dnode_t
*dn
, int new_nlevels
, dmu_tx_t
*tx
)
1805 uint64_t txgoff
= tx
->tx_txg
& TXG_MASK
;
1806 int old_nlevels
= dn
->dn_nlevels
;
1809 dbuf_dirty_record_t
*new, *dr
, *dr_next
;
1811 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1813 dn
->dn_nlevels
= new_nlevels
;
1815 ASSERT3U(new_nlevels
, >, dn
->dn_next_nlevels
[txgoff
]);
1816 dn
->dn_next_nlevels
[txgoff
] = new_nlevels
;
1818 /* dirty the left indirects */
1819 db
= dbuf_hold_level(dn
, old_nlevels
, 0, FTAG
);
1821 new = dbuf_dirty(db
, tx
);
1822 dbuf_rele(db
, FTAG
);
1824 /* transfer the dirty records to the new indirect */
1825 mutex_enter(&dn
->dn_mtx
);
1826 mutex_enter(&new->dt
.di
.dr_mtx
);
1827 list
= &dn
->dn_dirty_records
[txgoff
];
1828 for (dr
= list_head(list
); dr
; dr
= dr_next
) {
1829 dr_next
= list_next(&dn
->dn_dirty_records
[txgoff
], dr
);
1830 if (dr
->dr_dbuf
->db_level
!= new_nlevels
-1 &&
1831 dr
->dr_dbuf
->db_blkid
!= DMU_BONUS_BLKID
&&
1832 dr
->dr_dbuf
->db_blkid
!= DMU_SPILL_BLKID
) {
1833 ASSERT(dr
->dr_dbuf
->db_level
== old_nlevels
-1);
1834 list_remove(&dn
->dn_dirty_records
[txgoff
], dr
);
1835 list_insert_tail(&new->dt
.di
.dr_children
, dr
);
1836 dr
->dr_parent
= new;
1839 mutex_exit(&new->dt
.di
.dr_mtx
);
1840 mutex_exit(&dn
->dn_mtx
);
1844 dnode_set_nlevels(dnode_t
*dn
, int nlevels
, dmu_tx_t
*tx
)
1848 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1850 if (dn
->dn_nlevels
== nlevels
) {
1853 } else if (nlevels
< dn
->dn_nlevels
) {
1854 ret
= SET_ERROR(EINVAL
);
1858 dnode_set_nlevels_impl(dn
, nlevels
, tx
);
1861 rw_exit(&dn
->dn_struct_rwlock
);
1865 /* read-holding callers must not rely on the lock being continuously held */
1867 dnode_new_blkid(dnode_t
*dn
, uint64_t blkid
, dmu_tx_t
*tx
, boolean_t have_read
,
1870 int epbs
, new_nlevels
;
1873 ASSERT(blkid
!= DMU_BONUS_BLKID
);
1876 RW_READ_HELD(&dn
->dn_struct_rwlock
) :
1877 RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1880 * if we have a read-lock, check to see if we need to do any work
1881 * before upgrading to a write-lock.
1884 if (blkid
<= dn
->dn_maxblkid
)
1887 if (!rw_tryupgrade(&dn
->dn_struct_rwlock
)) {
1888 rw_exit(&dn
->dn_struct_rwlock
);
1889 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1894 * Raw sends (indicated by the force flag) require that we take the
1895 * given blkid even if the value is lower than the current value.
1897 if (!force
&& blkid
<= dn
->dn_maxblkid
)
1901 * We use the (otherwise unused) top bit of dn_next_maxblkid[txgoff]
1902 * to indicate that this field is set. This allows us to set the
1903 * maxblkid to 0 on an existing object in dnode_sync().
1905 dn
->dn_maxblkid
= blkid
;
1906 dn
->dn_next_maxblkid
[tx
->tx_txg
& TXG_MASK
] =
1907 blkid
| DMU_NEXT_MAXBLKID_SET
;
1910 * Compute the number of levels necessary to support the new maxblkid.
1911 * Raw sends will ensure nlevels is set correctly for us.
1914 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1915 for (sz
= dn
->dn_nblkptr
;
1916 sz
<= blkid
&& sz
>= dn
->dn_nblkptr
; sz
<<= epbs
)
1919 ASSERT3U(new_nlevels
, <=, DN_MAX_LEVELS
);
1922 if (new_nlevels
> dn
->dn_nlevels
)
1923 dnode_set_nlevels_impl(dn
, new_nlevels
, tx
);
1925 ASSERT3U(dn
->dn_nlevels
, >=, new_nlevels
);
1930 rw_downgrade(&dn
->dn_struct_rwlock
);
1934 dnode_dirty_l1(dnode_t
*dn
, uint64_t l1blkid
, dmu_tx_t
*tx
)
1936 dmu_buf_impl_t
*db
= dbuf_hold_level(dn
, 1, l1blkid
, FTAG
);
1938 dmu_buf_will_dirty(&db
->db
, tx
);
1939 dbuf_rele(db
, FTAG
);
1944 * Dirty all the in-core level-1 dbufs in the range specified by start_blkid
1948 dnode_dirty_l1range(dnode_t
*dn
, uint64_t start_blkid
, uint64_t end_blkid
,
1951 dmu_buf_impl_t db_search
;
1955 mutex_enter(&dn
->dn_dbufs_mtx
);
1957 db_search
.db_level
= 1;
1958 db_search
.db_blkid
= start_blkid
+ 1;
1959 db_search
.db_state
= DB_SEARCH
;
1962 db
= avl_find(&dn
->dn_dbufs
, &db_search
, &where
);
1964 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
1966 if (db
== NULL
|| db
->db_level
!= 1 ||
1967 db
->db_blkid
>= end_blkid
) {
1972 * Setup the next blkid we want to search for.
1974 db_search
.db_blkid
= db
->db_blkid
+ 1;
1975 ASSERT3U(db
->db_blkid
, >=, start_blkid
);
1978 * If the dbuf transitions to DB_EVICTING while we're trying
1979 * to dirty it, then we will be unable to discover it in
1980 * the dbuf hash table. This will result in a call to
1981 * dbuf_create() which needs to acquire the dn_dbufs_mtx
1982 * lock. To avoid a deadlock, we drop the lock before
1983 * dirtying the level-1 dbuf.
1985 mutex_exit(&dn
->dn_dbufs_mtx
);
1986 dnode_dirty_l1(dn
, db
->db_blkid
, tx
);
1987 mutex_enter(&dn
->dn_dbufs_mtx
);
1992 * Walk all the in-core level-1 dbufs and verify they have been dirtied.
1994 db_search
.db_level
= 1;
1995 db_search
.db_blkid
= start_blkid
+ 1;
1996 db_search
.db_state
= DB_SEARCH
;
1997 db
= avl_find(&dn
->dn_dbufs
, &db_search
, &where
);
1999 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
2000 for (; db
!= NULL
; db
= AVL_NEXT(&dn
->dn_dbufs
, db
)) {
2001 if (db
->db_level
!= 1 || db
->db_blkid
>= end_blkid
)
2003 if (db
->db_state
!= DB_EVICTING
)
2004 ASSERT(db
->db_dirtycnt
> 0);
2007 mutex_exit(&dn
->dn_dbufs_mtx
);
2011 dnode_free_range(dnode_t
*dn
, uint64_t off
, uint64_t len
, dmu_tx_t
*tx
)
2014 uint64_t blkoff
, blkid
, nblks
;
2015 int blksz
, blkshift
, head
, tail
;
2019 blksz
= dn
->dn_datablksz
;
2020 blkshift
= dn
->dn_datablkshift
;
2021 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2023 if (len
== DMU_OBJECT_END
) {
2024 len
= UINT64_MAX
- off
;
2029 * First, block align the region to free:
2032 head
= P2NPHASE(off
, blksz
);
2033 blkoff
= P2PHASE(off
, blksz
);
2034 if ((off
>> blkshift
) > dn
->dn_maxblkid
)
2037 ASSERT(dn
->dn_maxblkid
== 0);
2038 if (off
== 0 && len
>= blksz
) {
2040 * Freeing the whole block; fast-track this request.
2044 if (dn
->dn_nlevels
> 1) {
2045 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
2046 dnode_dirty_l1(dn
, 0, tx
);
2047 rw_exit(&dn
->dn_struct_rwlock
);
2050 } else if (off
>= blksz
) {
2051 /* Freeing past end-of-data */
2054 /* Freeing part of the block. */
2056 ASSERT3U(head
, >, 0);
2060 /* zero out any partial block data at the start of the range */
2063 ASSERT3U(blkoff
+ head
, ==, blksz
);
2066 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2067 res
= dbuf_hold_impl(dn
, 0, dbuf_whichblock(dn
, 0, off
),
2068 TRUE
, FALSE
, FTAG
, &db
);
2069 rw_exit(&dn
->dn_struct_rwlock
);
2074 db_lock_type_t dblt
= dmu_buf_lock_parent(db
, RW_READER
,
2076 /* don't dirty if it isn't on disk and isn't dirty */
2077 dirty
= db
->db_last_dirty
||
2078 (db
->db_blkptr
&& !BP_IS_HOLE(db
->db_blkptr
));
2079 dmu_buf_unlock_parent(db
, dblt
, FTAG
);
2081 dmu_buf_will_dirty(&db
->db
, tx
);
2082 data
= db
->db
.db_data
;
2083 bzero(data
+ blkoff
, head
);
2085 dbuf_rele(db
, FTAG
);
2091 /* If the range was less than one block, we're done */
2095 /* If the remaining range is past end of file, we're done */
2096 if ((off
>> blkshift
) > dn
->dn_maxblkid
)
2099 ASSERT(ISP2(blksz
));
2103 tail
= P2PHASE(len
, blksz
);
2105 ASSERT0(P2PHASE(off
, blksz
));
2106 /* zero out any partial block data at the end of the range */
2111 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2112 res
= dbuf_hold_impl(dn
, 0, dbuf_whichblock(dn
, 0, off
+len
),
2113 TRUE
, FALSE
, FTAG
, &db
);
2114 rw_exit(&dn
->dn_struct_rwlock
);
2117 /* don't dirty if not on disk and not dirty */
2118 db_lock_type_t type
= dmu_buf_lock_parent(db
, RW_READER
,
2120 dirty
= db
->db_last_dirty
||
2121 (db
->db_blkptr
&& !BP_IS_HOLE(db
->db_blkptr
));
2122 dmu_buf_unlock_parent(db
, type
, FTAG
);
2124 dmu_buf_will_dirty(&db
->db
, tx
);
2125 bzero(db
->db
.db_data
, tail
);
2127 dbuf_rele(db
, FTAG
);
2132 /* If the range did not include a full block, we are done */
2136 ASSERT(IS_P2ALIGNED(off
, blksz
));
2137 ASSERT(trunc
|| IS_P2ALIGNED(len
, blksz
));
2138 blkid
= off
>> blkshift
;
2139 nblks
= len
>> blkshift
;
2144 * Dirty all the indirect blocks in this range. Note that only
2145 * the first and last indirect blocks can actually be written
2146 * (if they were partially freed) -- they must be dirtied, even if
2147 * they do not exist on disk yet. The interior blocks will
2148 * be freed by free_children(), so they will not actually be written.
2149 * Even though these interior blocks will not be written, we
2150 * dirty them for two reasons:
2152 * - It ensures that the indirect blocks remain in memory until
2153 * syncing context. (They have already been prefetched by
2154 * dmu_tx_hold_free(), so we don't have to worry about reading
2155 * them serially here.)
2157 * - The dirty space accounting will put pressure on the txg sync
2158 * mechanism to begin syncing, and to delay transactions if there
2159 * is a large amount of freeing. Even though these indirect
2160 * blocks will not be written, we could need to write the same
2161 * amount of space if we copy the freed BPs into deadlists.
2163 if (dn
->dn_nlevels
> 1) {
2164 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
2165 uint64_t first
, last
;
2167 first
= blkid
>> epbs
;
2168 dnode_dirty_l1(dn
, first
, tx
);
2170 last
= dn
->dn_maxblkid
>> epbs
;
2172 last
= (blkid
+ nblks
- 1) >> epbs
;
2174 dnode_dirty_l1(dn
, last
, tx
);
2176 dnode_dirty_l1range(dn
, first
, last
, tx
);
2178 int shift
= dn
->dn_datablkshift
+ dn
->dn_indblkshift
-
2180 for (uint64_t i
= first
+ 1; i
< last
; i
++) {
2182 * Set i to the blockid of the next non-hole
2183 * level-1 indirect block at or after i. Note
2184 * that dnode_next_offset() operates in terms of
2185 * level-0-equivalent bytes.
2187 uint64_t ibyte
= i
<< shift
;
2188 int err
= dnode_next_offset(dn
, DNODE_FIND_HAVELOCK
,
2195 * Normally we should not see an error, either
2196 * from dnode_next_offset() or dbuf_hold_level()
2197 * (except for ESRCH from dnode_next_offset).
2198 * If there is an i/o error, then when we read
2199 * this block in syncing context, it will use
2200 * ZIO_FLAG_MUSTSUCCEED, and thus hang/panic according
2201 * to the "failmode" property. dnode_next_offset()
2202 * doesn't have a flag to indicate MUSTSUCCEED.
2207 dnode_dirty_l1(dn
, i
, tx
);
2209 rw_exit(&dn
->dn_struct_rwlock
);
2214 * Add this range to the dnode range list.
2215 * We will finish up this free operation in the syncing phase.
2217 mutex_enter(&dn
->dn_mtx
);
2219 int txgoff
= tx
->tx_txg
& TXG_MASK
;
2220 if (dn
->dn_free_ranges
[txgoff
] == NULL
) {
2221 dn
->dn_free_ranges
[txgoff
] = range_tree_create(NULL
, NULL
);
2223 range_tree_clear(dn
->dn_free_ranges
[txgoff
], blkid
, nblks
);
2224 range_tree_add(dn
->dn_free_ranges
[txgoff
], blkid
, nblks
);
2226 dprintf_dnode(dn
, "blkid=%llu nblks=%llu txg=%llu\n",
2227 blkid
, nblks
, tx
->tx_txg
);
2228 mutex_exit(&dn
->dn_mtx
);
2230 dbuf_free_range(dn
, blkid
, blkid
+ nblks
- 1, tx
);
2231 dnode_setdirty(dn
, tx
);
2235 dnode_spill_freed(dnode_t
*dn
)
2239 mutex_enter(&dn
->dn_mtx
);
2240 for (i
= 0; i
< TXG_SIZE
; i
++) {
2241 if (dn
->dn_rm_spillblk
[i
] == DN_KILL_SPILLBLK
)
2244 mutex_exit(&dn
->dn_mtx
);
2245 return (i
< TXG_SIZE
);
2248 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
2250 dnode_block_freed(dnode_t
*dn
, uint64_t blkid
)
2252 void *dp
= spa_get_dsl(dn
->dn_objset
->os_spa
);
2255 if (blkid
== DMU_BONUS_BLKID
)
2259 * If we're in the process of opening the pool, dp will not be
2260 * set yet, but there shouldn't be anything dirty.
2265 if (dn
->dn_free_txg
)
2268 if (blkid
== DMU_SPILL_BLKID
)
2269 return (dnode_spill_freed(dn
));
2271 mutex_enter(&dn
->dn_mtx
);
2272 for (i
= 0; i
< TXG_SIZE
; i
++) {
2273 if (dn
->dn_free_ranges
[i
] != NULL
&&
2274 range_tree_contains(dn
->dn_free_ranges
[i
], blkid
, 1))
2277 mutex_exit(&dn
->dn_mtx
);
2278 return (i
< TXG_SIZE
);
2281 /* call from syncing context when we actually write/free space for this dnode */
2283 dnode_diduse_space(dnode_t
*dn
, int64_t delta
)
2286 dprintf_dnode(dn
, "dn=%p dnp=%p used=%llu delta=%lld\n",
2288 (u_longlong_t
)dn
->dn_phys
->dn_used
,
2291 mutex_enter(&dn
->dn_mtx
);
2292 space
= DN_USED_BYTES(dn
->dn_phys
);
2294 ASSERT3U(space
+ delta
, >=, space
); /* no overflow */
2296 ASSERT3U(space
, >=, -delta
); /* no underflow */
2299 if (spa_version(dn
->dn_objset
->os_spa
) < SPA_VERSION_DNODE_BYTES
) {
2300 ASSERT((dn
->dn_phys
->dn_flags
& DNODE_FLAG_USED_BYTES
) == 0);
2301 ASSERT0(P2PHASE(space
, 1<<DEV_BSHIFT
));
2302 dn
->dn_phys
->dn_used
= space
>> DEV_BSHIFT
;
2304 dn
->dn_phys
->dn_used
= space
;
2305 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_USED_BYTES
;
2307 mutex_exit(&dn
->dn_mtx
);
2311 * Scans a block at the indicated "level" looking for a hole or data,
2312 * depending on 'flags'.
2314 * If level > 0, then we are scanning an indirect block looking at its
2315 * pointers. If level == 0, then we are looking at a block of dnodes.
2317 * If we don't find what we are looking for in the block, we return ESRCH.
2318 * Otherwise, return with *offset pointing to the beginning (if searching
2319 * forwards) or end (if searching backwards) of the range covered by the
2320 * block pointer we matched on (or dnode).
2322 * The basic search algorithm used below by dnode_next_offset() is to
2323 * use this function to search up the block tree (widen the search) until
2324 * we find something (i.e., we don't return ESRCH) and then search back
2325 * down the tree (narrow the search) until we reach our original search
2329 dnode_next_offset_level(dnode_t
*dn
, int flags
, uint64_t *offset
,
2330 int lvl
, uint64_t blkfill
, uint64_t txg
)
2332 dmu_buf_impl_t
*db
= NULL
;
2334 uint64_t epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2335 uint64_t epb
= 1ULL << epbs
;
2336 uint64_t minfill
, maxfill
;
2338 int i
, inc
, error
, span
;
2340 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2342 hole
= ((flags
& DNODE_FIND_HOLE
) != 0);
2343 inc
= (flags
& DNODE_FIND_BACKWARDS
) ? -1 : 1;
2344 ASSERT(txg
== 0 || !hole
);
2346 if (lvl
== dn
->dn_phys
->dn_nlevels
) {
2348 epb
= dn
->dn_phys
->dn_nblkptr
;
2349 data
= dn
->dn_phys
->dn_blkptr
;
2351 uint64_t blkid
= dbuf_whichblock(dn
, lvl
, *offset
);
2352 error
= dbuf_hold_impl(dn
, lvl
, blkid
, TRUE
, FALSE
, FTAG
, &db
);
2354 if (error
!= ENOENT
)
2359 * This can only happen when we are searching up
2360 * the block tree for data. We don't really need to
2361 * adjust the offset, as we will just end up looking
2362 * at the pointer to this block in its parent, and its
2363 * going to be unallocated, so we will skip over it.
2365 return (SET_ERROR(ESRCH
));
2367 error
= dbuf_read(db
, NULL
,
2368 DB_RF_CANFAIL
| DB_RF_HAVESTRUCT
| DB_RF_NO_DECRYPT
);
2370 dbuf_rele(db
, FTAG
);
2373 data
= db
->db
.db_data
;
2374 rw_enter(&db
->db_rwlock
, RW_READER
);
2377 if (db
!= NULL
&& txg
!= 0 && (db
->db_blkptr
== NULL
||
2378 db
->db_blkptr
->blk_birth
<= txg
||
2379 BP_IS_HOLE(db
->db_blkptr
))) {
2381 * This can only happen when we are searching up the tree
2382 * and these conditions mean that we need to keep climbing.
2384 error
= SET_ERROR(ESRCH
);
2385 } else if (lvl
== 0) {
2386 dnode_phys_t
*dnp
= data
;
2388 ASSERT(dn
->dn_type
== DMU_OT_DNODE
);
2389 ASSERT(!(flags
& DNODE_FIND_BACKWARDS
));
2391 for (i
= (*offset
>> DNODE_SHIFT
) & (blkfill
- 1);
2392 i
< blkfill
; i
+= dnp
[i
].dn_extra_slots
+ 1) {
2393 if ((dnp
[i
].dn_type
== DMU_OT_NONE
) == hole
)
2398 error
= SET_ERROR(ESRCH
);
2400 *offset
= (*offset
& ~(DNODE_BLOCK_SIZE
- 1)) +
2403 blkptr_t
*bp
= data
;
2404 uint64_t start
= *offset
;
2405 span
= (lvl
- 1) * epbs
+ dn
->dn_datablkshift
;
2407 maxfill
= blkfill
<< ((lvl
- 1) * epbs
);
2414 if (span
>= 8 * sizeof (*offset
)) {
2415 /* This only happens on the highest indirection level */
2416 ASSERT3U((lvl
- 1), ==, dn
->dn_phys
->dn_nlevels
- 1);
2419 *offset
= *offset
>> span
;
2422 for (i
= BF64_GET(*offset
, 0, epbs
);
2423 i
>= 0 && i
< epb
; i
+= inc
) {
2424 if (BP_GET_FILL(&bp
[i
]) >= minfill
&&
2425 BP_GET_FILL(&bp
[i
]) <= maxfill
&&
2426 (hole
|| bp
[i
].blk_birth
> txg
))
2428 if (inc
> 0 || *offset
> 0)
2432 if (span
>= 8 * sizeof (*offset
)) {
2435 *offset
= *offset
<< span
;
2439 /* traversing backwards; position offset at the end */
2440 ASSERT3U(*offset
, <=, start
);
2441 *offset
= MIN(*offset
+ (1ULL << span
) - 1, start
);
2442 } else if (*offset
< start
) {
2445 if (i
< 0 || i
>= epb
)
2446 error
= SET_ERROR(ESRCH
);
2450 rw_exit(&db
->db_rwlock
);
2451 dbuf_rele(db
, FTAG
);
2458 * Find the next hole, data, or sparse region at or after *offset.
2459 * The value 'blkfill' tells us how many items we expect to find
2460 * in an L0 data block; this value is 1 for normal objects,
2461 * DNODES_PER_BLOCK for the meta dnode, and some fraction of
2462 * DNODES_PER_BLOCK when searching for sparse regions thereof.
2466 * dnode_next_offset(dn, flags, offset, 1, 1, 0);
2467 * Finds the next/previous hole/data in a file.
2468 * Used in dmu_offset_next().
2470 * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
2471 * Finds the next free/allocated dnode an objset's meta-dnode.
2472 * Only finds objects that have new contents since txg (ie.
2473 * bonus buffer changes and content removal are ignored).
2474 * Used in dmu_object_next().
2476 * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
2477 * Finds the next L2 meta-dnode bp that's at most 1/4 full.
2478 * Used in dmu_object_alloc().
2481 dnode_next_offset(dnode_t
*dn
, int flags
, uint64_t *offset
,
2482 int minlvl
, uint64_t blkfill
, uint64_t txg
)
2484 uint64_t initial_offset
= *offset
;
2488 if (!(flags
& DNODE_FIND_HAVELOCK
))
2489 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2491 if (dn
->dn_phys
->dn_nlevels
== 0) {
2492 error
= SET_ERROR(ESRCH
);
2496 if (dn
->dn_datablkshift
== 0) {
2497 if (*offset
< dn
->dn_datablksz
) {
2498 if (flags
& DNODE_FIND_HOLE
)
2499 *offset
= dn
->dn_datablksz
;
2501 error
= SET_ERROR(ESRCH
);
2506 maxlvl
= dn
->dn_phys
->dn_nlevels
;
2508 for (lvl
= minlvl
; lvl
<= maxlvl
; lvl
++) {
2509 error
= dnode_next_offset_level(dn
,
2510 flags
, offset
, lvl
, blkfill
, txg
);
2515 while (error
== 0 && --lvl
>= minlvl
) {
2516 error
= dnode_next_offset_level(dn
,
2517 flags
, offset
, lvl
, blkfill
, txg
);
2521 * There's always a "virtual hole" at the end of the object, even
2522 * if all BP's which physically exist are non-holes.
2524 if ((flags
& DNODE_FIND_HOLE
) && error
== ESRCH
&& txg
== 0 &&
2525 minlvl
== 1 && blkfill
== 1 && !(flags
& DNODE_FIND_BACKWARDS
)) {
2529 if (error
== 0 && (flags
& DNODE_FIND_BACKWARDS
?
2530 initial_offset
< *offset
: initial_offset
> *offset
))
2531 error
= SET_ERROR(ESRCH
);
2533 if (!(flags
& DNODE_FIND_HAVELOCK
))
2534 rw_exit(&dn
->dn_struct_rwlock
);
2539 #if defined(_KERNEL)
2540 EXPORT_SYMBOL(dnode_hold
);
2541 EXPORT_SYMBOL(dnode_rele
);
2542 EXPORT_SYMBOL(dnode_set_nlevels
);
2543 EXPORT_SYMBOL(dnode_set_blksz
);
2544 EXPORT_SYMBOL(dnode_free_range
);
2545 EXPORT_SYMBOL(dnode_evict_dbufs
);
2546 EXPORT_SYMBOL(dnode_evict_bonus
);