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 refcount_create_untracked(&dn
->dn_holds
);
129 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 refcount_destroy(&dn
->dn_holds
);
188 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(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
));
393 dn
->dn_bonuslen
= newsize
;
395 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = DN_ZERO_BONUSLEN
;
397 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonuslen
;
398 rw_exit(&dn
->dn_struct_rwlock
);
402 dnode_setbonus_type(dnode_t
*dn
, dmu_object_type_t newtype
, dmu_tx_t
*tx
)
404 ASSERT3U(refcount_count(&dn
->dn_holds
), >=, 1);
405 dnode_setdirty(dn
, tx
);
406 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
407 dn
->dn_bonustype
= newtype
;
408 dn
->dn_next_bonustype
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonustype
;
409 rw_exit(&dn
->dn_struct_rwlock
);
413 dnode_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
415 ASSERT3U(refcount_count(&dn
->dn_holds
), >=, 1);
416 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
417 dnode_setdirty(dn
, tx
);
418 dn
->dn_rm_spillblk
[tx
->tx_txg
&TXG_MASK
] = DN_KILL_SPILLBLK
;
419 dn
->dn_have_spill
= B_FALSE
;
423 dnode_setdblksz(dnode_t
*dn
, int size
)
425 ASSERT0(P2PHASE(size
, SPA_MINBLOCKSIZE
));
426 ASSERT3U(size
, <=, SPA_MAXBLOCKSIZE
);
427 ASSERT3U(size
, >=, SPA_MINBLOCKSIZE
);
428 ASSERT3U(size
>> SPA_MINBLOCKSHIFT
, <,
429 1<<(sizeof (dn
->dn_phys
->dn_datablkszsec
) * 8));
430 dn
->dn_datablksz
= size
;
431 dn
->dn_datablkszsec
= size
>> SPA_MINBLOCKSHIFT
;
432 dn
->dn_datablkshift
= ISP2(size
) ? highbit64(size
- 1) : 0;
436 dnode_create(objset_t
*os
, dnode_phys_t
*dnp
, dmu_buf_impl_t
*db
,
437 uint64_t object
, dnode_handle_t
*dnh
)
441 dn
= kmem_cache_alloc(dnode_cache
, KM_SLEEP
);
442 ASSERT(!POINTER_IS_VALID(dn
->dn_objset
));
446 * Defer setting dn_objset until the dnode is ready to be a candidate
447 * for the dnode_move() callback.
449 dn
->dn_object
= object
;
454 if (dnp
->dn_datablkszsec
) {
455 dnode_setdblksz(dn
, dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
);
457 dn
->dn_datablksz
= 0;
458 dn
->dn_datablkszsec
= 0;
459 dn
->dn_datablkshift
= 0;
461 dn
->dn_indblkshift
= dnp
->dn_indblkshift
;
462 dn
->dn_nlevels
= dnp
->dn_nlevels
;
463 dn
->dn_type
= dnp
->dn_type
;
464 dn
->dn_nblkptr
= dnp
->dn_nblkptr
;
465 dn
->dn_checksum
= dnp
->dn_checksum
;
466 dn
->dn_compress
= dnp
->dn_compress
;
467 dn
->dn_bonustype
= dnp
->dn_bonustype
;
468 dn
->dn_bonuslen
= dnp
->dn_bonuslen
;
469 dn
->dn_num_slots
= dnp
->dn_extra_slots
+ 1;
470 dn
->dn_maxblkid
= dnp
->dn_maxblkid
;
471 dn
->dn_have_spill
= ((dnp
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
) != 0);
474 dmu_zfetch_init(&dn
->dn_zfetch
, dn
);
476 ASSERT(DMU_OT_IS_VALID(dn
->dn_phys
->dn_type
));
477 ASSERT(zrl_is_locked(&dnh
->dnh_zrlock
));
478 ASSERT(!DN_SLOT_IS_PTR(dnh
->dnh_dnode
));
480 mutex_enter(&os
->os_lock
);
483 * Exclude special dnodes from os_dnodes so an empty os_dnodes
484 * signifies that the special dnodes have no references from
485 * their children (the entries in os_dnodes). This allows
486 * dnode_destroy() to easily determine if the last child has
487 * been removed and then complete eviction of the objset.
489 if (!DMU_OBJECT_IS_SPECIAL(object
))
490 list_insert_head(&os
->os_dnodes
, dn
);
494 * Everything else must be valid before assigning dn_objset
495 * makes the dnode eligible for dnode_move().
500 mutex_exit(&os
->os_lock
);
502 arc_space_consume(sizeof (dnode_t
), ARC_SPACE_DNODE
);
508 * Caller must be holding the dnode handle, which is released upon return.
511 dnode_destroy(dnode_t
*dn
)
513 objset_t
*os
= dn
->dn_objset
;
514 boolean_t complete_os_eviction
= B_FALSE
;
516 ASSERT((dn
->dn_id_flags
& DN_ID_NEW_EXIST
) == 0);
518 mutex_enter(&os
->os_lock
);
519 POINTER_INVALIDATE(&dn
->dn_objset
);
520 if (!DMU_OBJECT_IS_SPECIAL(dn
->dn_object
)) {
521 list_remove(&os
->os_dnodes
, dn
);
522 complete_os_eviction
=
523 list_is_empty(&os
->os_dnodes
) &&
524 list_link_active(&os
->os_evicting_node
);
526 mutex_exit(&os
->os_lock
);
528 /* the dnode can no longer move, so we can release the handle */
529 if (!zrl_is_locked(&dn
->dn_handle
->dnh_zrlock
))
530 zrl_remove(&dn
->dn_handle
->dnh_zrlock
);
532 dn
->dn_allocated_txg
= 0;
534 dn
->dn_assigned_txg
= 0;
535 dn
->dn_dirty_txg
= 0;
538 if (dn
->dn_dirtyctx_firstset
!= NULL
) {
539 kmem_free(dn
->dn_dirtyctx_firstset
, 1);
540 dn
->dn_dirtyctx_firstset
= NULL
;
542 if (dn
->dn_bonus
!= NULL
) {
543 mutex_enter(&dn
->dn_bonus
->db_mtx
);
544 dbuf_destroy(dn
->dn_bonus
);
549 dn
->dn_have_spill
= B_FALSE
;
554 dn
->dn_oldprojid
= ZFS_DEFAULT_PROJID
;
557 dn
->dn_newprojid
= ZFS_DEFAULT_PROJID
;
560 dmu_zfetch_fini(&dn
->dn_zfetch
);
561 kmem_cache_free(dnode_cache
, dn
);
562 arc_space_return(sizeof (dnode_t
), ARC_SPACE_DNODE
);
564 if (complete_os_eviction
)
565 dmu_objset_evict_done(os
);
569 dnode_allocate(dnode_t
*dn
, dmu_object_type_t ot
, int blocksize
, int ibs
,
570 dmu_object_type_t bonustype
, int bonuslen
, int dn_slots
, dmu_tx_t
*tx
)
574 ASSERT3U(dn_slots
, >, 0);
575 ASSERT3U(dn_slots
<< DNODE_SHIFT
, <=,
576 spa_maxdnodesize(dmu_objset_spa(dn
->dn_objset
)));
577 ASSERT3U(blocksize
, <=,
578 spa_maxblocksize(dmu_objset_spa(dn
->dn_objset
)));
580 blocksize
= 1 << zfs_default_bs
;
582 blocksize
= P2ROUNDUP(blocksize
, SPA_MINBLOCKSIZE
);
585 ibs
= zfs_default_ibs
;
587 ibs
= MIN(MAX(ibs
, DN_MIN_INDBLKSHIFT
), DN_MAX_INDBLKSHIFT
);
589 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d dn_slots=%d\n",
590 dn
->dn_objset
, dn
->dn_object
, tx
->tx_txg
, blocksize
, ibs
, dn_slots
);
591 DNODE_STAT_BUMP(dnode_allocate
);
593 ASSERT(dn
->dn_type
== DMU_OT_NONE
);
594 ASSERT(bcmp(dn
->dn_phys
, &dnode_phys_zero
, sizeof (dnode_phys_t
)) == 0);
595 ASSERT(dn
->dn_phys
->dn_type
== DMU_OT_NONE
);
596 ASSERT(ot
!= DMU_OT_NONE
);
597 ASSERT(DMU_OT_IS_VALID(ot
));
598 ASSERT((bonustype
== DMU_OT_NONE
&& bonuslen
== 0) ||
599 (bonustype
== DMU_OT_SA
&& bonuslen
== 0) ||
600 (bonustype
!= DMU_OT_NONE
&& bonuslen
!= 0));
601 ASSERT(DMU_OT_IS_VALID(bonustype
));
602 ASSERT3U(bonuslen
, <=, DN_SLOTS_TO_BONUSLEN(dn_slots
));
603 ASSERT(dn
->dn_type
== DMU_OT_NONE
);
604 ASSERT0(dn
->dn_maxblkid
);
605 ASSERT0(dn
->dn_allocated_txg
);
606 ASSERT0(dn
->dn_assigned_txg
);
607 ASSERT0(dn
->dn_dirty_txg
);
608 ASSERT(refcount_is_zero(&dn
->dn_tx_holds
));
609 ASSERT3U(refcount_count(&dn
->dn_holds
), <=, 1);
610 ASSERT(avl_is_empty(&dn
->dn_dbufs
));
612 for (i
= 0; i
< TXG_SIZE
; i
++) {
613 ASSERT0(dn
->dn_next_nblkptr
[i
]);
614 ASSERT0(dn
->dn_next_nlevels
[i
]);
615 ASSERT0(dn
->dn_next_indblkshift
[i
]);
616 ASSERT0(dn
->dn_next_bonuslen
[i
]);
617 ASSERT0(dn
->dn_next_bonustype
[i
]);
618 ASSERT0(dn
->dn_rm_spillblk
[i
]);
619 ASSERT0(dn
->dn_next_blksz
[i
]);
620 ASSERT0(dn
->dn_next_maxblkid
[i
]);
621 ASSERT(!multilist_link_active(&dn
->dn_dirty_link
[i
]));
622 ASSERT3P(list_head(&dn
->dn_dirty_records
[i
]), ==, NULL
);
623 ASSERT3P(dn
->dn_free_ranges
[i
], ==, NULL
);
627 dnode_setdblksz(dn
, blocksize
);
628 dn
->dn_indblkshift
= ibs
;
630 dn
->dn_num_slots
= dn_slots
;
631 if (bonustype
== DMU_OT_SA
) /* Maximize bonus space for SA */
634 dn
->dn_nblkptr
= MIN(DN_MAX_NBLKPTR
,
635 1 + ((DN_SLOTS_TO_BONUSLEN(dn_slots
) - bonuslen
) >>
639 dn
->dn_bonustype
= bonustype
;
640 dn
->dn_bonuslen
= bonuslen
;
641 dn
->dn_checksum
= ZIO_CHECKSUM_INHERIT
;
642 dn
->dn_compress
= ZIO_COMPRESS_INHERIT
;
646 if (dn
->dn_dirtyctx_firstset
) {
647 kmem_free(dn
->dn_dirtyctx_firstset
, 1);
648 dn
->dn_dirtyctx_firstset
= NULL
;
651 dn
->dn_allocated_txg
= tx
->tx_txg
;
654 dnode_setdirty(dn
, tx
);
655 dn
->dn_next_indblkshift
[tx
->tx_txg
& TXG_MASK
] = ibs
;
656 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonuslen
;
657 dn
->dn_next_bonustype
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonustype
;
658 dn
->dn_next_blksz
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_datablksz
;
662 dnode_reallocate(dnode_t
*dn
, dmu_object_type_t ot
, int blocksize
,
663 dmu_object_type_t bonustype
, int bonuslen
, int dn_slots
, dmu_tx_t
*tx
)
667 ASSERT3U(blocksize
, >=, SPA_MINBLOCKSIZE
);
668 ASSERT3U(blocksize
, <=,
669 spa_maxblocksize(dmu_objset_spa(dn
->dn_objset
)));
670 ASSERT0(blocksize
% SPA_MINBLOCKSIZE
);
671 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
|| dmu_tx_private_ok(tx
));
672 ASSERT(tx
->tx_txg
!= 0);
673 ASSERT((bonustype
== DMU_OT_NONE
&& bonuslen
== 0) ||
674 (bonustype
!= DMU_OT_NONE
&& bonuslen
!= 0) ||
675 (bonustype
== DMU_OT_SA
&& bonuslen
== 0));
676 ASSERT(DMU_OT_IS_VALID(bonustype
));
677 ASSERT3U(bonuslen
, <=,
678 DN_BONUS_SIZE(spa_maxdnodesize(dmu_objset_spa(dn
->dn_objset
))));
680 dn_slots
= dn_slots
> 0 ? dn_slots
: DNODE_MIN_SLOTS
;
682 dnode_free_interior_slots(dn
);
683 DNODE_STAT_BUMP(dnode_reallocate
);
685 /* clean up any unreferenced dbufs */
686 dnode_evict_dbufs(dn
);
690 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
691 dnode_setdirty(dn
, tx
);
692 if (dn
->dn_datablksz
!= blocksize
) {
693 /* change blocksize */
694 ASSERT(dn
->dn_maxblkid
== 0 &&
695 (BP_IS_HOLE(&dn
->dn_phys
->dn_blkptr
[0]) ||
696 dnode_block_freed(dn
, 0)));
697 dnode_setdblksz(dn
, blocksize
);
698 dn
->dn_next_blksz
[tx
->tx_txg
&TXG_MASK
] = blocksize
;
700 if (dn
->dn_bonuslen
!= bonuslen
)
701 dn
->dn_next_bonuslen
[tx
->tx_txg
&TXG_MASK
] = bonuslen
;
703 if (bonustype
== DMU_OT_SA
) /* Maximize bonus space for SA */
706 nblkptr
= MIN(DN_MAX_NBLKPTR
,
707 1 + ((DN_SLOTS_TO_BONUSLEN(dn_slots
) - bonuslen
) >>
709 if (dn
->dn_bonustype
!= bonustype
)
710 dn
->dn_next_bonustype
[tx
->tx_txg
&TXG_MASK
] = bonustype
;
711 if (dn
->dn_nblkptr
!= nblkptr
)
712 dn
->dn_next_nblkptr
[tx
->tx_txg
&TXG_MASK
] = nblkptr
;
713 if (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
) {
714 dbuf_rm_spill(dn
, tx
);
715 dnode_rm_spill(dn
, tx
);
717 rw_exit(&dn
->dn_struct_rwlock
);
722 /* change bonus size and type */
723 mutex_enter(&dn
->dn_mtx
);
724 dn
->dn_bonustype
= bonustype
;
725 dn
->dn_bonuslen
= bonuslen
;
726 dn
->dn_num_slots
= dn_slots
;
727 dn
->dn_nblkptr
= nblkptr
;
728 dn
->dn_checksum
= ZIO_CHECKSUM_INHERIT
;
729 dn
->dn_compress
= ZIO_COMPRESS_INHERIT
;
730 ASSERT3U(dn
->dn_nblkptr
, <=, DN_MAX_NBLKPTR
);
732 /* fix up the bonus db_size */
734 dn
->dn_bonus
->db
.db_size
=
735 DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
736 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
737 ASSERT(dn
->dn_bonuslen
<= dn
->dn_bonus
->db
.db_size
);
740 dn
->dn_allocated_txg
= tx
->tx_txg
;
741 mutex_exit(&dn
->dn_mtx
);
746 dnode_move_impl(dnode_t
*odn
, dnode_t
*ndn
)
750 ASSERT(!RW_LOCK_HELD(&odn
->dn_struct_rwlock
));
751 ASSERT(MUTEX_NOT_HELD(&odn
->dn_mtx
));
752 ASSERT(MUTEX_NOT_HELD(&odn
->dn_dbufs_mtx
));
753 ASSERT(!RW_LOCK_HELD(&odn
->dn_zfetch
.zf_rwlock
));
756 ndn
->dn_objset
= odn
->dn_objset
;
757 ndn
->dn_object
= odn
->dn_object
;
758 ndn
->dn_dbuf
= odn
->dn_dbuf
;
759 ndn
->dn_handle
= odn
->dn_handle
;
760 ndn
->dn_phys
= odn
->dn_phys
;
761 ndn
->dn_type
= odn
->dn_type
;
762 ndn
->dn_bonuslen
= odn
->dn_bonuslen
;
763 ndn
->dn_bonustype
= odn
->dn_bonustype
;
764 ndn
->dn_nblkptr
= odn
->dn_nblkptr
;
765 ndn
->dn_checksum
= odn
->dn_checksum
;
766 ndn
->dn_compress
= odn
->dn_compress
;
767 ndn
->dn_nlevels
= odn
->dn_nlevels
;
768 ndn
->dn_indblkshift
= odn
->dn_indblkshift
;
769 ndn
->dn_datablkshift
= odn
->dn_datablkshift
;
770 ndn
->dn_datablkszsec
= odn
->dn_datablkszsec
;
771 ndn
->dn_datablksz
= odn
->dn_datablksz
;
772 ndn
->dn_maxblkid
= odn
->dn_maxblkid
;
773 ndn
->dn_num_slots
= odn
->dn_num_slots
;
774 bcopy(&odn
->dn_next_nblkptr
[0], &ndn
->dn_next_nblkptr
[0],
775 sizeof (odn
->dn_next_nblkptr
));
776 bcopy(&odn
->dn_next_nlevels
[0], &ndn
->dn_next_nlevels
[0],
777 sizeof (odn
->dn_next_nlevels
));
778 bcopy(&odn
->dn_next_indblkshift
[0], &ndn
->dn_next_indblkshift
[0],
779 sizeof (odn
->dn_next_indblkshift
));
780 bcopy(&odn
->dn_next_bonustype
[0], &ndn
->dn_next_bonustype
[0],
781 sizeof (odn
->dn_next_bonustype
));
782 bcopy(&odn
->dn_rm_spillblk
[0], &ndn
->dn_rm_spillblk
[0],
783 sizeof (odn
->dn_rm_spillblk
));
784 bcopy(&odn
->dn_next_bonuslen
[0], &ndn
->dn_next_bonuslen
[0],
785 sizeof (odn
->dn_next_bonuslen
));
786 bcopy(&odn
->dn_next_blksz
[0], &ndn
->dn_next_blksz
[0],
787 sizeof (odn
->dn_next_blksz
));
788 bcopy(&odn
->dn_next_maxblkid
[0], &ndn
->dn_next_maxblkid
[0],
789 sizeof (odn
->dn_next_maxblkid
));
790 for (i
= 0; i
< TXG_SIZE
; i
++) {
791 list_move_tail(&ndn
->dn_dirty_records
[i
],
792 &odn
->dn_dirty_records
[i
]);
794 bcopy(&odn
->dn_free_ranges
[0], &ndn
->dn_free_ranges
[0],
795 sizeof (odn
->dn_free_ranges
));
796 ndn
->dn_allocated_txg
= odn
->dn_allocated_txg
;
797 ndn
->dn_free_txg
= odn
->dn_free_txg
;
798 ndn
->dn_assigned_txg
= odn
->dn_assigned_txg
;
799 ndn
->dn_dirty_txg
= odn
->dn_dirty_txg
;
800 ndn
->dn_dirtyctx
= odn
->dn_dirtyctx
;
801 ndn
->dn_dirtyctx_firstset
= odn
->dn_dirtyctx_firstset
;
802 ASSERT(refcount_count(&odn
->dn_tx_holds
) == 0);
803 refcount_transfer(&ndn
->dn_holds
, &odn
->dn_holds
);
804 ASSERT(avl_is_empty(&ndn
->dn_dbufs
));
805 avl_swap(&ndn
->dn_dbufs
, &odn
->dn_dbufs
);
806 ndn
->dn_dbufs_count
= odn
->dn_dbufs_count
;
807 ndn
->dn_bonus
= odn
->dn_bonus
;
808 ndn
->dn_have_spill
= odn
->dn_have_spill
;
809 ndn
->dn_zio
= odn
->dn_zio
;
810 ndn
->dn_oldused
= odn
->dn_oldused
;
811 ndn
->dn_oldflags
= odn
->dn_oldflags
;
812 ndn
->dn_olduid
= odn
->dn_olduid
;
813 ndn
->dn_oldgid
= odn
->dn_oldgid
;
814 ndn
->dn_oldprojid
= odn
->dn_oldprojid
;
815 ndn
->dn_newuid
= odn
->dn_newuid
;
816 ndn
->dn_newgid
= odn
->dn_newgid
;
817 ndn
->dn_newprojid
= odn
->dn_newprojid
;
818 ndn
->dn_id_flags
= odn
->dn_id_flags
;
819 dmu_zfetch_init(&ndn
->dn_zfetch
, NULL
);
820 list_move_tail(&ndn
->dn_zfetch
.zf_stream
, &odn
->dn_zfetch
.zf_stream
);
821 ndn
->dn_zfetch
.zf_dnode
= odn
->dn_zfetch
.zf_dnode
;
824 * Update back pointers. Updating the handle fixes the back pointer of
825 * every descendant dbuf as well as the bonus dbuf.
827 ASSERT(ndn
->dn_handle
->dnh_dnode
== odn
);
828 ndn
->dn_handle
->dnh_dnode
= ndn
;
829 if (ndn
->dn_zfetch
.zf_dnode
== odn
) {
830 ndn
->dn_zfetch
.zf_dnode
= ndn
;
834 * Invalidate the original dnode by clearing all of its back pointers.
837 odn
->dn_handle
= NULL
;
838 avl_create(&odn
->dn_dbufs
, dbuf_compare
, sizeof (dmu_buf_impl_t
),
839 offsetof(dmu_buf_impl_t
, db_link
));
840 odn
->dn_dbufs_count
= 0;
841 odn
->dn_bonus
= NULL
;
842 odn
->dn_zfetch
.zf_dnode
= NULL
;
845 * Set the low bit of the objset pointer to ensure that dnode_move()
846 * recognizes the dnode as invalid in any subsequent callback.
848 POINTER_INVALIDATE(&odn
->dn_objset
);
851 * Satisfy the destructor.
853 for (i
= 0; i
< TXG_SIZE
; i
++) {
854 list_create(&odn
->dn_dirty_records
[i
],
855 sizeof (dbuf_dirty_record_t
),
856 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
857 odn
->dn_free_ranges
[i
] = NULL
;
858 odn
->dn_next_nlevels
[i
] = 0;
859 odn
->dn_next_indblkshift
[i
] = 0;
860 odn
->dn_next_bonustype
[i
] = 0;
861 odn
->dn_rm_spillblk
[i
] = 0;
862 odn
->dn_next_bonuslen
[i
] = 0;
863 odn
->dn_next_blksz
[i
] = 0;
865 odn
->dn_allocated_txg
= 0;
866 odn
->dn_free_txg
= 0;
867 odn
->dn_assigned_txg
= 0;
868 odn
->dn_dirty_txg
= 0;
869 odn
->dn_dirtyctx
= 0;
870 odn
->dn_dirtyctx_firstset
= NULL
;
871 odn
->dn_have_spill
= B_FALSE
;
874 odn
->dn_oldflags
= 0;
877 odn
->dn_oldprojid
= ZFS_DEFAULT_PROJID
;
880 odn
->dn_newprojid
= ZFS_DEFAULT_PROJID
;
881 odn
->dn_id_flags
= 0;
887 odn
->dn_moved
= (uint8_t)-1;
892 dnode_move(void *buf
, void *newbuf
, size_t size
, void *arg
)
894 dnode_t
*odn
= buf
, *ndn
= newbuf
;
900 * The dnode is on the objset's list of known dnodes if the objset
901 * pointer is valid. We set the low bit of the objset pointer when
902 * freeing the dnode to invalidate it, and the memory patterns written
903 * by kmem (baddcafe and deadbeef) set at least one of the two low bits.
904 * A newly created dnode sets the objset pointer last of all to indicate
905 * that the dnode is known and in a valid state to be moved by this
909 if (!POINTER_IS_VALID(os
)) {
910 DNODE_STAT_BUMP(dnode_move_invalid
);
911 return (KMEM_CBRC_DONT_KNOW
);
915 * Ensure that the objset does not go away during the move.
917 rw_enter(&os_lock
, RW_WRITER
);
918 if (os
!= odn
->dn_objset
) {
920 DNODE_STAT_BUMP(dnode_move_recheck1
);
921 return (KMEM_CBRC_DONT_KNOW
);
925 * If the dnode is still valid, then so is the objset. We know that no
926 * valid objset can be freed while we hold os_lock, so we can safely
927 * ensure that the objset remains in use.
929 mutex_enter(&os
->os_lock
);
932 * Recheck the objset pointer in case the dnode was removed just before
933 * acquiring the lock.
935 if (os
!= odn
->dn_objset
) {
936 mutex_exit(&os
->os_lock
);
938 DNODE_STAT_BUMP(dnode_move_recheck2
);
939 return (KMEM_CBRC_DONT_KNOW
);
943 * At this point we know that as long as we hold os->os_lock, the dnode
944 * cannot be freed and fields within the dnode can be safely accessed.
945 * The objset listing this dnode cannot go away as long as this dnode is
949 if (DMU_OBJECT_IS_SPECIAL(odn
->dn_object
)) {
950 mutex_exit(&os
->os_lock
);
951 DNODE_STAT_BUMP(dnode_move_special
);
952 return (KMEM_CBRC_NO
);
954 ASSERT(odn
->dn_dbuf
!= NULL
); /* only "special" dnodes have no parent */
957 * Lock the dnode handle to prevent the dnode from obtaining any new
958 * holds. This also prevents the descendant dbufs and the bonus dbuf
959 * from accessing the dnode, so that we can discount their holds. The
960 * handle is safe to access because we know that while the dnode cannot
961 * go away, neither can its handle. Once we hold dnh_zrlock, we can
962 * safely move any dnode referenced only by dbufs.
964 if (!zrl_tryenter(&odn
->dn_handle
->dnh_zrlock
)) {
965 mutex_exit(&os
->os_lock
);
966 DNODE_STAT_BUMP(dnode_move_handle
);
967 return (KMEM_CBRC_LATER
);
971 * Ensure a consistent view of the dnode's holds and the dnode's dbufs.
972 * We need to guarantee that there is a hold for every dbuf in order to
973 * determine whether the dnode is actively referenced. Falsely matching
974 * a dbuf to an active hold would lead to an unsafe move. It's possible
975 * that a thread already having an active dnode hold is about to add a
976 * dbuf, and we can't compare hold and dbuf counts while the add is in
979 if (!rw_tryenter(&odn
->dn_struct_rwlock
, RW_WRITER
)) {
980 zrl_exit(&odn
->dn_handle
->dnh_zrlock
);
981 mutex_exit(&os
->os_lock
);
982 DNODE_STAT_BUMP(dnode_move_rwlock
);
983 return (KMEM_CBRC_LATER
);
987 * A dbuf may be removed (evicted) without an active dnode hold. In that
988 * case, the dbuf count is decremented under the handle lock before the
989 * dbuf's hold is released. This order ensures that if we count the hold
990 * after the dbuf is removed but before its hold is released, we will
991 * treat the unmatched hold as active and exit safely. If we count the
992 * hold before the dbuf is removed, the hold is discounted, and the
993 * removal is blocked until the move completes.
995 refcount
= refcount_count(&odn
->dn_holds
);
996 ASSERT(refcount
>= 0);
997 dbufs
= odn
->dn_dbufs_count
;
999 /* We can't have more dbufs than dnode holds. */
1000 ASSERT3U(dbufs
, <=, refcount
);
1001 DTRACE_PROBE3(dnode__move
, dnode_t
*, odn
, int64_t, refcount
,
1004 if (refcount
> dbufs
) {
1005 rw_exit(&odn
->dn_struct_rwlock
);
1006 zrl_exit(&odn
->dn_handle
->dnh_zrlock
);
1007 mutex_exit(&os
->os_lock
);
1008 DNODE_STAT_BUMP(dnode_move_active
);
1009 return (KMEM_CBRC_LATER
);
1012 rw_exit(&odn
->dn_struct_rwlock
);
1015 * At this point we know that anyone with a hold on the dnode is not
1016 * actively referencing it. The dnode is known and in a valid state to
1017 * move. We're holding the locks needed to execute the critical section.
1019 dnode_move_impl(odn
, ndn
);
1021 list_link_replace(&odn
->dn_link
, &ndn
->dn_link
);
1022 /* If the dnode was safe to move, the refcount cannot have changed. */
1023 ASSERT(refcount
== refcount_count(&ndn
->dn_holds
));
1024 ASSERT(dbufs
== ndn
->dn_dbufs_count
);
1025 zrl_exit(&ndn
->dn_handle
->dnh_zrlock
); /* handle has moved */
1026 mutex_exit(&os
->os_lock
);
1028 return (KMEM_CBRC_YES
);
1030 #endif /* _KERNEL */
1033 dnode_slots_hold(dnode_children_t
*children
, int idx
, int slots
)
1035 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1037 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1038 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1039 zrl_add(&dnh
->dnh_zrlock
);
1044 dnode_slots_rele(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
];
1051 if (zrl_is_locked(&dnh
->dnh_zrlock
))
1052 zrl_exit(&dnh
->dnh_zrlock
);
1054 zrl_remove(&dnh
->dnh_zrlock
);
1059 dnode_slots_tryenter(dnode_children_t
*children
, int idx
, int slots
)
1061 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1063 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1064 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1066 if (!zrl_tryenter(&dnh
->dnh_zrlock
)) {
1067 for (int j
= idx
; j
< i
; j
++) {
1068 dnh
= &children
->dnc_children
[j
];
1069 zrl_exit(&dnh
->dnh_zrlock
);
1080 dnode_set_slots(dnode_children_t
*children
, int idx
, int slots
, void *ptr
)
1082 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1084 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1085 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1086 dnh
->dnh_dnode
= ptr
;
1091 dnode_check_slots_free(dnode_children_t
*children
, int idx
, int slots
)
1093 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1096 * If all dnode slots are either already free or
1097 * evictable return B_TRUE.
1099 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1100 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1101 dnode_t
*dn
= dnh
->dnh_dnode
;
1103 if (dn
== DN_SLOT_FREE
) {
1105 } else if (DN_SLOT_IS_PTR(dn
)) {
1106 mutex_enter(&dn
->dn_mtx
);
1107 boolean_t can_free
= (dn
->dn_type
== DMU_OT_NONE
&&
1108 !DNODE_IS_DIRTY(dn
));
1109 mutex_exit(&dn
->dn_mtx
);
1124 dnode_reclaim_slots(dnode_children_t
*children
, int idx
, int slots
)
1126 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1128 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1129 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1131 ASSERT(zrl_is_locked(&dnh
->dnh_zrlock
));
1133 if (DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1134 ASSERT3S(dnh
->dnh_dnode
->dn_type
, ==, DMU_OT_NONE
);
1135 dnode_destroy(dnh
->dnh_dnode
);
1136 dnh
->dnh_dnode
= DN_SLOT_FREE
;
1142 dnode_free_interior_slots(dnode_t
*dn
)
1144 dnode_children_t
*children
= dmu_buf_get_user(&dn
->dn_dbuf
->db
);
1145 int epb
= dn
->dn_dbuf
->db
.db_size
>> DNODE_SHIFT
;
1146 int idx
= (dn
->dn_object
& (epb
- 1)) + 1;
1147 int slots
= dn
->dn_num_slots
- 1;
1152 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1154 while (!dnode_slots_tryenter(children
, idx
, slots
))
1155 DNODE_STAT_BUMP(dnode_free_interior_lock_retry
);
1157 dnode_set_slots(children
, idx
, slots
, DN_SLOT_FREE
);
1158 dnode_slots_rele(children
, idx
, slots
);
1162 dnode_special_close(dnode_handle_t
*dnh
)
1164 dnode_t
*dn
= dnh
->dnh_dnode
;
1167 * Wait for final references to the dnode to clear. This can
1168 * only happen if the arc is asynchronously evicting state that
1169 * has a hold on this dnode while we are trying to evict this
1172 while (refcount_count(&dn
->dn_holds
) > 0)
1174 ASSERT(dn
->dn_dbuf
== NULL
||
1175 dmu_buf_get_user(&dn
->dn_dbuf
->db
) == NULL
);
1176 zrl_add(&dnh
->dnh_zrlock
);
1177 dnode_destroy(dn
); /* implicit zrl_remove() */
1178 zrl_destroy(&dnh
->dnh_zrlock
);
1179 dnh
->dnh_dnode
= NULL
;
1183 dnode_special_open(objset_t
*os
, dnode_phys_t
*dnp
, uint64_t object
,
1184 dnode_handle_t
*dnh
)
1188 zrl_init(&dnh
->dnh_zrlock
);
1189 zrl_tryenter(&dnh
->dnh_zrlock
);
1191 dn
= dnode_create(os
, dnp
, NULL
, object
, dnh
);
1194 zrl_exit(&dnh
->dnh_zrlock
);
1198 dnode_buf_evict_async(void *dbu
)
1200 dnode_children_t
*dnc
= dbu
;
1202 DNODE_STAT_BUMP(dnode_buf_evict
);
1204 for (int i
= 0; i
< dnc
->dnc_count
; i
++) {
1205 dnode_handle_t
*dnh
= &dnc
->dnc_children
[i
];
1209 * The dnode handle lock guards against the dnode moving to
1210 * another valid address, so there is no need here to guard
1211 * against changes to or from NULL.
1213 if (!DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1214 zrl_destroy(&dnh
->dnh_zrlock
);
1215 dnh
->dnh_dnode
= DN_SLOT_UNINIT
;
1219 zrl_add(&dnh
->dnh_zrlock
);
1220 dn
= dnh
->dnh_dnode
;
1222 * If there are holds on this dnode, then there should
1223 * be holds on the dnode's containing dbuf as well; thus
1224 * it wouldn't be eligible for eviction and this function
1225 * would not have been called.
1227 ASSERT(refcount_is_zero(&dn
->dn_holds
));
1228 ASSERT(refcount_is_zero(&dn
->dn_tx_holds
));
1230 dnode_destroy(dn
); /* implicit zrl_remove() for first slot */
1231 zrl_destroy(&dnh
->dnh_zrlock
);
1232 dnh
->dnh_dnode
= DN_SLOT_UNINIT
;
1234 kmem_free(dnc
, sizeof (dnode_children_t
) +
1235 dnc
->dnc_count
* sizeof (dnode_handle_t
));
1239 * When the DNODE_MUST_BE_FREE flag is set, the "slots" parameter is used
1240 * to ensure the hole at the specified object offset is large enough to
1241 * hold the dnode being created. The slots parameter is also used to ensure
1242 * a dnode does not span multiple dnode blocks. In both of these cases, if
1243 * a failure occurs, ENOSPC is returned. Keep in mind, these failure cases
1244 * are only possible when using DNODE_MUST_BE_FREE.
1246 * If the DNODE_MUST_BE_ALLOCATED flag is set, "slots" must be 0.
1247 * dnode_hold_impl() will check if the requested dnode is already consumed
1248 * as an extra dnode slot by an large dnode, in which case it returns
1252 * EINVAL - Invalid object number or flags.
1253 * ENOSPC - Hole too small to fulfill "slots" request (DNODE_MUST_BE_FREE)
1254 * EEXIST - Refers to an allocated dnode (DNODE_MUST_BE_FREE)
1255 * - Refers to an interior dnode slot (DNODE_MUST_BE_ALLOCATED)
1256 * ENOENT - The requested dnode is not allocated (DNODE_MUST_BE_ALLOCATED)
1257 * EIO - I/O error when reading the meta dnode dbuf.
1259 * succeeds even for free dnodes.
1262 dnode_hold_impl(objset_t
*os
, uint64_t object
, int flag
, int slots
,
1263 void *tag
, dnode_t
**dnp
)
1266 int drop_struct_lock
= FALSE
;
1271 dnode_children_t
*dnc
;
1272 dnode_phys_t
*dn_block
;
1273 dnode_handle_t
*dnh
;
1275 ASSERT(!(flag
& DNODE_MUST_BE_ALLOCATED
) || (slots
== 0));
1276 ASSERT(!(flag
& DNODE_MUST_BE_FREE
) || (slots
> 0));
1279 * If you are holding the spa config lock as writer, you shouldn't
1280 * be asking the DMU to do *anything* unless it's the root pool
1281 * which may require us to read from the root filesystem while
1282 * holding some (not all) of the locks as writer.
1284 ASSERT(spa_config_held(os
->os_spa
, SCL_ALL
, RW_WRITER
) == 0 ||
1285 (spa_is_root(os
->os_spa
) &&
1286 spa_config_held(os
->os_spa
, SCL_STATE
, RW_WRITER
)));
1288 if (object
== DMU_USERUSED_OBJECT
|| object
== DMU_GROUPUSED_OBJECT
||
1289 object
== DMU_PROJECTUSED_OBJECT
) {
1290 if (object
== DMU_USERUSED_OBJECT
)
1291 dn
= DMU_USERUSED_DNODE(os
);
1292 else if (object
== DMU_GROUPUSED_OBJECT
)
1293 dn
= DMU_GROUPUSED_DNODE(os
);
1295 dn
= DMU_PROJECTUSED_DNODE(os
);
1297 return (SET_ERROR(ENOENT
));
1299 if ((flag
& DNODE_MUST_BE_ALLOCATED
) && type
== DMU_OT_NONE
)
1300 return (SET_ERROR(ENOENT
));
1301 if ((flag
& DNODE_MUST_BE_FREE
) && type
!= DMU_OT_NONE
)
1302 return (SET_ERROR(EEXIST
));
1304 (void) refcount_add(&dn
->dn_holds
, tag
);
1309 if (object
== 0 || object
>= DN_MAX_OBJECT
)
1310 return (SET_ERROR(EINVAL
));
1312 mdn
= DMU_META_DNODE(os
);
1313 ASSERT(mdn
->dn_object
== DMU_META_DNODE_OBJECT
);
1317 if (!RW_WRITE_HELD(&mdn
->dn_struct_rwlock
)) {
1318 rw_enter(&mdn
->dn_struct_rwlock
, RW_READER
);
1319 drop_struct_lock
= TRUE
;
1322 blk
= dbuf_whichblock(mdn
, 0, object
* sizeof (dnode_phys_t
));
1324 db
= dbuf_hold(mdn
, blk
, FTAG
);
1325 if (drop_struct_lock
)
1326 rw_exit(&mdn
->dn_struct_rwlock
);
1328 DNODE_STAT_BUMP(dnode_hold_dbuf_hold
);
1329 return (SET_ERROR(EIO
));
1333 * We do not need to decrypt to read the dnode so it doesn't matter
1334 * if we get the encrypted or decrypted version.
1336 err
= dbuf_read(db
, NULL
, DB_RF_CANFAIL
| DB_RF_NO_DECRYPT
);
1338 DNODE_STAT_BUMP(dnode_hold_dbuf_read
);
1339 dbuf_rele(db
, FTAG
);
1343 ASSERT3U(db
->db
.db_size
, >=, 1<<DNODE_SHIFT
);
1344 epb
= db
->db
.db_size
>> DNODE_SHIFT
;
1346 idx
= object
& (epb
- 1);
1347 dn_block
= (dnode_phys_t
*)db
->db
.db_data
;
1349 ASSERT(DB_DNODE(db
)->dn_type
== DMU_OT_DNODE
);
1350 dnc
= dmu_buf_get_user(&db
->db
);
1353 dnode_children_t
*winner
;
1356 dnc
= kmem_zalloc(sizeof (dnode_children_t
) +
1357 epb
* sizeof (dnode_handle_t
), KM_SLEEP
);
1358 dnc
->dnc_count
= epb
;
1359 dnh
= &dnc
->dnc_children
[0];
1361 /* Initialize dnode slot status from dnode_phys_t */
1362 for (int i
= 0; i
< epb
; i
++) {
1363 zrl_init(&dnh
[i
].dnh_zrlock
);
1370 if (dn_block
[i
].dn_type
!= DMU_OT_NONE
) {
1371 int interior
= dn_block
[i
].dn_extra_slots
;
1373 dnode_set_slots(dnc
, i
, 1, DN_SLOT_ALLOCATED
);
1374 dnode_set_slots(dnc
, i
+ 1, interior
,
1378 dnh
[i
].dnh_dnode
= DN_SLOT_FREE
;
1383 dmu_buf_init_user(&dnc
->dnc_dbu
, NULL
,
1384 dnode_buf_evict_async
, NULL
);
1385 winner
= dmu_buf_set_user(&db
->db
, &dnc
->dnc_dbu
);
1386 if (winner
!= NULL
) {
1388 for (int i
= 0; i
< epb
; i
++)
1389 zrl_destroy(&dnh
[i
].dnh_zrlock
);
1391 kmem_free(dnc
, sizeof (dnode_children_t
) +
1392 epb
* sizeof (dnode_handle_t
));
1397 ASSERT(dnc
->dnc_count
== epb
);
1398 dn
= DN_SLOT_UNINIT
;
1400 if (flag
& DNODE_MUST_BE_ALLOCATED
) {
1403 while (dn
== DN_SLOT_UNINIT
) {
1404 dnode_slots_hold(dnc
, idx
, slots
);
1405 dnh
= &dnc
->dnc_children
[idx
];
1407 if (DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1408 dn
= dnh
->dnh_dnode
;
1410 } else if (dnh
->dnh_dnode
== DN_SLOT_INTERIOR
) {
1411 DNODE_STAT_BUMP(dnode_hold_alloc_interior
);
1412 dnode_slots_rele(dnc
, idx
, slots
);
1413 dbuf_rele(db
, FTAG
);
1414 return (SET_ERROR(EEXIST
));
1415 } else if (dnh
->dnh_dnode
!= DN_SLOT_ALLOCATED
) {
1416 DNODE_STAT_BUMP(dnode_hold_alloc_misses
);
1417 dnode_slots_rele(dnc
, idx
, slots
);
1418 dbuf_rele(db
, FTAG
);
1419 return (SET_ERROR(ENOENT
));
1422 dnode_slots_rele(dnc
, idx
, slots
);
1423 if (!dnode_slots_tryenter(dnc
, idx
, slots
)) {
1424 DNODE_STAT_BUMP(dnode_hold_alloc_lock_retry
);
1429 * Someone else won the race and called dnode_create()
1430 * after we checked DN_SLOT_IS_PTR() above but before
1431 * we acquired the lock.
1433 if (DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1434 DNODE_STAT_BUMP(dnode_hold_alloc_lock_misses
);
1435 dn
= dnh
->dnh_dnode
;
1437 dn
= dnode_create(os
, dn_block
+ idx
, db
,
1442 mutex_enter(&dn
->dn_mtx
);
1443 if (dn
->dn_type
== DMU_OT_NONE
) {
1444 DNODE_STAT_BUMP(dnode_hold_alloc_type_none
);
1445 mutex_exit(&dn
->dn_mtx
);
1446 dnode_slots_rele(dnc
, idx
, slots
);
1447 dbuf_rele(db
, FTAG
);
1448 return (SET_ERROR(ENOENT
));
1451 DNODE_STAT_BUMP(dnode_hold_alloc_hits
);
1452 } else if (flag
& DNODE_MUST_BE_FREE
) {
1454 if (idx
+ slots
- 1 >= DNODES_PER_BLOCK
) {
1455 DNODE_STAT_BUMP(dnode_hold_free_overflow
);
1456 dbuf_rele(db
, FTAG
);
1457 return (SET_ERROR(ENOSPC
));
1460 while (dn
== DN_SLOT_UNINIT
) {
1461 dnode_slots_hold(dnc
, idx
, slots
);
1463 if (!dnode_check_slots_free(dnc
, idx
, slots
)) {
1464 DNODE_STAT_BUMP(dnode_hold_free_misses
);
1465 dnode_slots_rele(dnc
, idx
, slots
);
1466 dbuf_rele(db
, FTAG
);
1467 return (SET_ERROR(ENOSPC
));
1470 dnode_slots_rele(dnc
, idx
, slots
);
1471 if (!dnode_slots_tryenter(dnc
, idx
, slots
)) {
1472 DNODE_STAT_BUMP(dnode_hold_free_lock_retry
);
1476 if (!dnode_check_slots_free(dnc
, idx
, slots
)) {
1477 DNODE_STAT_BUMP(dnode_hold_free_lock_misses
);
1478 dnode_slots_rele(dnc
, idx
, slots
);
1479 dbuf_rele(db
, FTAG
);
1480 return (SET_ERROR(ENOSPC
));
1484 * Allocated but otherwise free dnodes which would
1485 * be in the interior of a multi-slot dnodes need
1486 * to be freed. Single slot dnodes can be safely
1487 * re-purposed as a performance optimization.
1490 dnode_reclaim_slots(dnc
, idx
+ 1, slots
- 1);
1492 dnh
= &dnc
->dnc_children
[idx
];
1493 if (DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1494 dn
= dnh
->dnh_dnode
;
1496 dn
= dnode_create(os
, dn_block
+ idx
, db
,
1501 mutex_enter(&dn
->dn_mtx
);
1502 if (!refcount_is_zero(&dn
->dn_holds
)) {
1503 DNODE_STAT_BUMP(dnode_hold_free_refcount
);
1504 mutex_exit(&dn
->dn_mtx
);
1505 dnode_slots_rele(dnc
, idx
, slots
);
1506 dbuf_rele(db
, FTAG
);
1507 return (SET_ERROR(EEXIST
));
1510 dnode_set_slots(dnc
, idx
+ 1, slots
- 1, DN_SLOT_INTERIOR
);
1511 DNODE_STAT_BUMP(dnode_hold_free_hits
);
1513 dbuf_rele(db
, FTAG
);
1514 return (SET_ERROR(EINVAL
));
1517 if (dn
->dn_free_txg
) {
1518 DNODE_STAT_BUMP(dnode_hold_free_txg
);
1520 mutex_exit(&dn
->dn_mtx
);
1521 dnode_slots_rele(dnc
, idx
, slots
);
1522 dbuf_rele(db
, FTAG
);
1523 return (SET_ERROR(type
== DMU_OT_NONE
? ENOENT
: EEXIST
));
1526 if (refcount_add(&dn
->dn_holds
, tag
) == 1)
1527 dbuf_add_ref(db
, dnh
);
1529 mutex_exit(&dn
->dn_mtx
);
1531 /* Now we can rely on the hold to prevent the dnode from moving. */
1532 dnode_slots_rele(dnc
, idx
, slots
);
1535 ASSERT3P(dn
->dn_dbuf
, ==, db
);
1536 ASSERT3U(dn
->dn_object
, ==, object
);
1537 dbuf_rele(db
, FTAG
);
1544 * Return held dnode if the object is allocated, NULL if not.
1547 dnode_hold(objset_t
*os
, uint64_t object
, void *tag
, dnode_t
**dnp
)
1549 return (dnode_hold_impl(os
, object
, DNODE_MUST_BE_ALLOCATED
, 0, tag
,
1554 * Can only add a reference if there is already at least one
1555 * reference on the dnode. Returns FALSE if unable to add a
1559 dnode_add_ref(dnode_t
*dn
, void *tag
)
1561 mutex_enter(&dn
->dn_mtx
);
1562 if (refcount_is_zero(&dn
->dn_holds
)) {
1563 mutex_exit(&dn
->dn_mtx
);
1566 VERIFY(1 < refcount_add(&dn
->dn_holds
, tag
));
1567 mutex_exit(&dn
->dn_mtx
);
1572 dnode_rele(dnode_t
*dn
, void *tag
)
1574 mutex_enter(&dn
->dn_mtx
);
1575 dnode_rele_and_unlock(dn
, tag
);
1579 dnode_rele_and_unlock(dnode_t
*dn
, void *tag
)
1582 /* Get while the hold prevents the dnode from moving. */
1583 dmu_buf_impl_t
*db
= dn
->dn_dbuf
;
1584 dnode_handle_t
*dnh
= dn
->dn_handle
;
1586 refs
= refcount_remove(&dn
->dn_holds
, tag
);
1587 mutex_exit(&dn
->dn_mtx
);
1590 * It's unsafe to release the last hold on a dnode by dnode_rele() or
1591 * indirectly by dbuf_rele() while relying on the dnode handle to
1592 * prevent the dnode from moving, since releasing the last hold could
1593 * result in the dnode's parent dbuf evicting its dnode handles. For
1594 * that reason anyone calling dnode_rele() or dbuf_rele() without some
1595 * other direct or indirect hold on the dnode must first drop the dnode
1598 ASSERT(refs
> 0 || dnh
->dnh_zrlock
.zr_owner
!= curthread
);
1600 /* NOTE: the DNODE_DNODE does not have a dn_dbuf */
1601 if (refs
== 0 && db
!= NULL
) {
1603 * Another thread could add a hold to the dnode handle in
1604 * dnode_hold_impl() while holding the parent dbuf. Since the
1605 * hold on the parent dbuf prevents the handle from being
1606 * destroyed, the hold on the handle is OK. We can't yet assert
1607 * that the handle has zero references, but that will be
1608 * asserted anyway when the handle gets destroyed.
1615 dnode_setdirty(dnode_t
*dn
, dmu_tx_t
*tx
)
1617 objset_t
*os
= dn
->dn_objset
;
1618 uint64_t txg
= tx
->tx_txg
;
1620 if (DMU_OBJECT_IS_SPECIAL(dn
->dn_object
)) {
1621 dsl_dataset_dirty(os
->os_dsl_dataset
, tx
);
1628 mutex_enter(&dn
->dn_mtx
);
1629 ASSERT(dn
->dn_phys
->dn_type
|| dn
->dn_allocated_txg
);
1630 ASSERT(dn
->dn_free_txg
== 0 || dn
->dn_free_txg
>= txg
);
1631 mutex_exit(&dn
->dn_mtx
);
1635 * Determine old uid/gid when necessary
1637 dmu_objset_userquota_get_ids(dn
, B_TRUE
, tx
);
1639 multilist_t
*dirtylist
= os
->os_dirty_dnodes
[txg
& TXG_MASK
];
1640 multilist_sublist_t
*mls
= multilist_sublist_lock_obj(dirtylist
, dn
);
1643 * If we are already marked dirty, we're done.
1645 if (multilist_link_active(&dn
->dn_dirty_link
[txg
& TXG_MASK
])) {
1646 multilist_sublist_unlock(mls
);
1650 ASSERT(!refcount_is_zero(&dn
->dn_holds
) ||
1651 !avl_is_empty(&dn
->dn_dbufs
));
1652 ASSERT(dn
->dn_datablksz
!= 0);
1653 ASSERT0(dn
->dn_next_bonuslen
[txg
&TXG_MASK
]);
1654 ASSERT0(dn
->dn_next_blksz
[txg
&TXG_MASK
]);
1655 ASSERT0(dn
->dn_next_bonustype
[txg
&TXG_MASK
]);
1657 dprintf_ds(os
->os_dsl_dataset
, "obj=%llu txg=%llu\n",
1658 dn
->dn_object
, txg
);
1660 multilist_sublist_insert_head(mls
, dn
);
1662 multilist_sublist_unlock(mls
);
1665 * The dnode maintains a hold on its containing dbuf as
1666 * long as there are holds on it. Each instantiated child
1667 * dbuf maintains a hold on the dnode. When the last child
1668 * drops its hold, the dnode will drop its hold on the
1669 * containing dbuf. We add a "dirty hold" here so that the
1670 * dnode will hang around after we finish processing its
1673 VERIFY(dnode_add_ref(dn
, (void *)(uintptr_t)tx
->tx_txg
));
1675 (void) dbuf_dirty(dn
->dn_dbuf
, tx
);
1677 dsl_dataset_dirty(os
->os_dsl_dataset
, tx
);
1681 dnode_free(dnode_t
*dn
, dmu_tx_t
*tx
)
1683 mutex_enter(&dn
->dn_mtx
);
1684 if (dn
->dn_type
== DMU_OT_NONE
|| dn
->dn_free_txg
) {
1685 mutex_exit(&dn
->dn_mtx
);
1688 dn
->dn_free_txg
= tx
->tx_txg
;
1689 mutex_exit(&dn
->dn_mtx
);
1691 dnode_setdirty(dn
, tx
);
1695 * Try to change the block size for the indicated dnode. This can only
1696 * succeed if there are no blocks allocated or dirty beyond first block
1699 dnode_set_blksz(dnode_t
*dn
, uint64_t size
, int ibs
, dmu_tx_t
*tx
)
1704 ASSERT3U(size
, <=, spa_maxblocksize(dmu_objset_spa(dn
->dn_objset
)));
1706 size
= SPA_MINBLOCKSIZE
;
1708 size
= P2ROUNDUP(size
, SPA_MINBLOCKSIZE
);
1710 if (ibs
== dn
->dn_indblkshift
)
1713 if (size
>> SPA_MINBLOCKSHIFT
== dn
->dn_datablkszsec
&& ibs
== 0)
1716 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1718 /* Check for any allocated blocks beyond the first */
1719 if (dn
->dn_maxblkid
!= 0)
1722 mutex_enter(&dn
->dn_dbufs_mtx
);
1723 for (db
= avl_first(&dn
->dn_dbufs
); db
!= NULL
;
1724 db
= AVL_NEXT(&dn
->dn_dbufs
, db
)) {
1725 if (db
->db_blkid
!= 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1726 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1727 mutex_exit(&dn
->dn_dbufs_mtx
);
1731 mutex_exit(&dn
->dn_dbufs_mtx
);
1733 if (ibs
&& dn
->dn_nlevels
!= 1)
1736 /* resize the old block */
1737 err
= dbuf_hold_impl(dn
, 0, 0, TRUE
, FALSE
, FTAG
, &db
);
1739 dbuf_new_size(db
, size
, tx
);
1740 else if (err
!= ENOENT
)
1743 dnode_setdblksz(dn
, size
);
1744 dnode_setdirty(dn
, tx
);
1745 dn
->dn_next_blksz
[tx
->tx_txg
&TXG_MASK
] = size
;
1747 dn
->dn_indblkshift
= ibs
;
1748 dn
->dn_next_indblkshift
[tx
->tx_txg
&TXG_MASK
] = ibs
;
1750 /* rele after we have fixed the blocksize in the dnode */
1752 dbuf_rele(db
, FTAG
);
1754 rw_exit(&dn
->dn_struct_rwlock
);
1758 rw_exit(&dn
->dn_struct_rwlock
);
1759 return (SET_ERROR(ENOTSUP
));
1763 dnode_set_nlevels_impl(dnode_t
*dn
, int new_nlevels
, dmu_tx_t
*tx
)
1765 uint64_t txgoff
= tx
->tx_txg
& TXG_MASK
;
1766 int old_nlevels
= dn
->dn_nlevels
;
1769 dbuf_dirty_record_t
*new, *dr
, *dr_next
;
1771 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1773 dn
->dn_nlevels
= new_nlevels
;
1775 ASSERT3U(new_nlevels
, >, dn
->dn_next_nlevels
[txgoff
]);
1776 dn
->dn_next_nlevels
[txgoff
] = new_nlevels
;
1778 /* dirty the left indirects */
1779 db
= dbuf_hold_level(dn
, old_nlevels
, 0, FTAG
);
1781 new = dbuf_dirty(db
, tx
);
1782 dbuf_rele(db
, FTAG
);
1784 /* transfer the dirty records to the new indirect */
1785 mutex_enter(&dn
->dn_mtx
);
1786 mutex_enter(&new->dt
.di
.dr_mtx
);
1787 list
= &dn
->dn_dirty_records
[txgoff
];
1788 for (dr
= list_head(list
); dr
; dr
= dr_next
) {
1789 dr_next
= list_next(&dn
->dn_dirty_records
[txgoff
], dr
);
1790 if (dr
->dr_dbuf
->db_level
!= new_nlevels
-1 &&
1791 dr
->dr_dbuf
->db_blkid
!= DMU_BONUS_BLKID
&&
1792 dr
->dr_dbuf
->db_blkid
!= DMU_SPILL_BLKID
) {
1793 ASSERT(dr
->dr_dbuf
->db_level
== old_nlevels
-1);
1794 list_remove(&dn
->dn_dirty_records
[txgoff
], dr
);
1795 list_insert_tail(&new->dt
.di
.dr_children
, dr
);
1796 dr
->dr_parent
= new;
1799 mutex_exit(&new->dt
.di
.dr_mtx
);
1800 mutex_exit(&dn
->dn_mtx
);
1804 dnode_set_nlevels(dnode_t
*dn
, int nlevels
, dmu_tx_t
*tx
)
1808 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1810 if (dn
->dn_nlevels
== nlevels
) {
1813 } else if (nlevels
< dn
->dn_nlevels
) {
1814 ret
= SET_ERROR(EINVAL
);
1818 dnode_set_nlevels_impl(dn
, nlevels
, tx
);
1821 rw_exit(&dn
->dn_struct_rwlock
);
1825 /* read-holding callers must not rely on the lock being continuously held */
1827 dnode_new_blkid(dnode_t
*dn
, uint64_t blkid
, dmu_tx_t
*tx
, boolean_t have_read
)
1829 int epbs
, new_nlevels
;
1832 ASSERT(blkid
!= DMU_BONUS_BLKID
);
1835 RW_READ_HELD(&dn
->dn_struct_rwlock
) :
1836 RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1839 * if we have a read-lock, check to see if we need to do any work
1840 * before upgrading to a write-lock.
1843 if (blkid
<= dn
->dn_maxblkid
)
1846 if (!rw_tryupgrade(&dn
->dn_struct_rwlock
)) {
1847 rw_exit(&dn
->dn_struct_rwlock
);
1848 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1852 if (blkid
<= dn
->dn_maxblkid
)
1855 dn
->dn_maxblkid
= blkid
;
1856 dn
->dn_next_maxblkid
[tx
->tx_txg
& TXG_MASK
] = blkid
;
1859 * Compute the number of levels necessary to support the new maxblkid.
1862 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1863 for (sz
= dn
->dn_nblkptr
;
1864 sz
<= blkid
&& sz
>= dn
->dn_nblkptr
; sz
<<= epbs
)
1867 ASSERT3U(new_nlevels
, <=, DN_MAX_LEVELS
);
1869 if (new_nlevels
> dn
->dn_nlevels
)
1870 dnode_set_nlevels_impl(dn
, new_nlevels
, tx
);
1874 rw_downgrade(&dn
->dn_struct_rwlock
);
1878 dnode_dirty_l1(dnode_t
*dn
, uint64_t l1blkid
, dmu_tx_t
*tx
)
1880 dmu_buf_impl_t
*db
= dbuf_hold_level(dn
, 1, l1blkid
, FTAG
);
1882 dmu_buf_will_dirty(&db
->db
, tx
);
1883 dbuf_rele(db
, FTAG
);
1888 dnode_free_range(dnode_t
*dn
, uint64_t off
, uint64_t len
, dmu_tx_t
*tx
)
1891 uint64_t blkoff
, blkid
, nblks
;
1892 int blksz
, blkshift
, head
, tail
;
1896 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1897 blksz
= dn
->dn_datablksz
;
1898 blkshift
= dn
->dn_datablkshift
;
1899 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1901 if (len
== DMU_OBJECT_END
) {
1902 len
= UINT64_MAX
- off
;
1907 * First, block align the region to free:
1910 head
= P2NPHASE(off
, blksz
);
1911 blkoff
= P2PHASE(off
, blksz
);
1912 if ((off
>> blkshift
) > dn
->dn_maxblkid
)
1915 ASSERT(dn
->dn_maxblkid
== 0);
1916 if (off
== 0 && len
>= blksz
) {
1918 * Freeing the whole block; fast-track this request.
1919 * Note that we won't dirty any indirect blocks,
1920 * which is fine because we will be freeing the entire
1921 * file and thus all indirect blocks will be freed
1922 * by free_children().
1927 } else if (off
>= blksz
) {
1928 /* Freeing past end-of-data */
1931 /* Freeing part of the block. */
1933 ASSERT3U(head
, >, 0);
1937 /* zero out any partial block data at the start of the range */
1939 ASSERT3U(blkoff
+ head
, ==, blksz
);
1942 if (dbuf_hold_impl(dn
, 0, dbuf_whichblock(dn
, 0, off
),
1943 TRUE
, FALSE
, FTAG
, &db
) == 0) {
1946 /* don't dirty if it isn't on disk and isn't dirty */
1947 if (db
->db_last_dirty
||
1948 (db
->db_blkptr
&& !BP_IS_HOLE(db
->db_blkptr
))) {
1949 rw_exit(&dn
->dn_struct_rwlock
);
1950 dmu_buf_will_dirty(&db
->db
, tx
);
1951 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1952 data
= db
->db
.db_data
;
1953 bzero(data
+ blkoff
, head
);
1955 dbuf_rele(db
, FTAG
);
1961 /* If the range was less than one block, we're done */
1965 /* If the remaining range is past end of file, we're done */
1966 if ((off
>> blkshift
) > dn
->dn_maxblkid
)
1969 ASSERT(ISP2(blksz
));
1973 tail
= P2PHASE(len
, blksz
);
1975 ASSERT0(P2PHASE(off
, blksz
));
1976 /* zero out any partial block data at the end of the range */
1980 if (dbuf_hold_impl(dn
, 0, dbuf_whichblock(dn
, 0, off
+len
),
1981 TRUE
, FALSE
, FTAG
, &db
) == 0) {
1982 /* don't dirty if not on disk and not dirty */
1983 if (db
->db_last_dirty
||
1984 (db
->db_blkptr
&& !BP_IS_HOLE(db
->db_blkptr
))) {
1985 rw_exit(&dn
->dn_struct_rwlock
);
1986 dmu_buf_will_dirty(&db
->db
, tx
);
1987 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1988 bzero(db
->db
.db_data
, tail
);
1990 dbuf_rele(db
, FTAG
);
1995 /* If the range did not include a full block, we are done */
1999 ASSERT(IS_P2ALIGNED(off
, blksz
));
2000 ASSERT(trunc
|| IS_P2ALIGNED(len
, blksz
));
2001 blkid
= off
>> blkshift
;
2002 nblks
= len
>> blkshift
;
2007 * Dirty all the indirect blocks in this range. Note that only
2008 * the first and last indirect blocks can actually be written
2009 * (if they were partially freed) -- they must be dirtied, even if
2010 * they do not exist on disk yet. The interior blocks will
2011 * be freed by free_children(), so they will not actually be written.
2012 * Even though these interior blocks will not be written, we
2013 * dirty them for two reasons:
2015 * - It ensures that the indirect blocks remain in memory until
2016 * syncing context. (They have already been prefetched by
2017 * dmu_tx_hold_free(), so we don't have to worry about reading
2018 * them serially here.)
2020 * - The dirty space accounting will put pressure on the txg sync
2021 * mechanism to begin syncing, and to delay transactions if there
2022 * is a large amount of freeing. Even though these indirect
2023 * blocks will not be written, we could need to write the same
2024 * amount of space if we copy the freed BPs into deadlists.
2026 if (dn
->dn_nlevels
> 1) {
2027 uint64_t first
, last
;
2029 first
= blkid
>> epbs
;
2030 dnode_dirty_l1(dn
, first
, tx
);
2032 last
= dn
->dn_maxblkid
>> epbs
;
2034 last
= (blkid
+ nblks
- 1) >> epbs
;
2036 dnode_dirty_l1(dn
, last
, tx
);
2038 int shift
= dn
->dn_datablkshift
+ dn
->dn_indblkshift
-
2040 for (uint64_t i
= first
+ 1; i
< last
; i
++) {
2042 * Set i to the blockid of the next non-hole
2043 * level-1 indirect block at or after i. Note
2044 * that dnode_next_offset() operates in terms of
2045 * level-0-equivalent bytes.
2047 uint64_t ibyte
= i
<< shift
;
2048 int err
= dnode_next_offset(dn
, DNODE_FIND_HAVELOCK
,
2055 * Normally we should not see an error, either
2056 * from dnode_next_offset() or dbuf_hold_level()
2057 * (except for ESRCH from dnode_next_offset).
2058 * If there is an i/o error, then when we read
2059 * this block in syncing context, it will use
2060 * ZIO_FLAG_MUSTSUCCEED, and thus hang/panic according
2061 * to the "failmode" property. dnode_next_offset()
2062 * doesn't have a flag to indicate MUSTSUCCEED.
2067 dnode_dirty_l1(dn
, i
, tx
);
2073 * Add this range to the dnode range list.
2074 * We will finish up this free operation in the syncing phase.
2076 mutex_enter(&dn
->dn_mtx
);
2078 int txgoff
= tx
->tx_txg
& TXG_MASK
;
2079 if (dn
->dn_free_ranges
[txgoff
] == NULL
) {
2080 dn
->dn_free_ranges
[txgoff
] = range_tree_create(NULL
, NULL
);
2082 range_tree_clear(dn
->dn_free_ranges
[txgoff
], blkid
, nblks
);
2083 range_tree_add(dn
->dn_free_ranges
[txgoff
], blkid
, nblks
);
2085 dprintf_dnode(dn
, "blkid=%llu nblks=%llu txg=%llu\n",
2086 blkid
, nblks
, tx
->tx_txg
);
2087 mutex_exit(&dn
->dn_mtx
);
2089 dbuf_free_range(dn
, blkid
, blkid
+ nblks
- 1, tx
);
2090 dnode_setdirty(dn
, tx
);
2093 rw_exit(&dn
->dn_struct_rwlock
);
2097 dnode_spill_freed(dnode_t
*dn
)
2101 mutex_enter(&dn
->dn_mtx
);
2102 for (i
= 0; i
< TXG_SIZE
; i
++) {
2103 if (dn
->dn_rm_spillblk
[i
] == DN_KILL_SPILLBLK
)
2106 mutex_exit(&dn
->dn_mtx
);
2107 return (i
< TXG_SIZE
);
2110 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
2112 dnode_block_freed(dnode_t
*dn
, uint64_t blkid
)
2114 void *dp
= spa_get_dsl(dn
->dn_objset
->os_spa
);
2117 if (blkid
== DMU_BONUS_BLKID
)
2121 * If we're in the process of opening the pool, dp will not be
2122 * set yet, but there shouldn't be anything dirty.
2127 if (dn
->dn_free_txg
)
2130 if (blkid
== DMU_SPILL_BLKID
)
2131 return (dnode_spill_freed(dn
));
2133 mutex_enter(&dn
->dn_mtx
);
2134 for (i
= 0; i
< TXG_SIZE
; i
++) {
2135 if (dn
->dn_free_ranges
[i
] != NULL
&&
2136 range_tree_contains(dn
->dn_free_ranges
[i
], blkid
, 1))
2139 mutex_exit(&dn
->dn_mtx
);
2140 return (i
< TXG_SIZE
);
2143 /* call from syncing context when we actually write/free space for this dnode */
2145 dnode_diduse_space(dnode_t
*dn
, int64_t delta
)
2148 dprintf_dnode(dn
, "dn=%p dnp=%p used=%llu delta=%lld\n",
2150 (u_longlong_t
)dn
->dn_phys
->dn_used
,
2153 mutex_enter(&dn
->dn_mtx
);
2154 space
= DN_USED_BYTES(dn
->dn_phys
);
2156 ASSERT3U(space
+ delta
, >=, space
); /* no overflow */
2158 ASSERT3U(space
, >=, -delta
); /* no underflow */
2161 if (spa_version(dn
->dn_objset
->os_spa
) < SPA_VERSION_DNODE_BYTES
) {
2162 ASSERT((dn
->dn_phys
->dn_flags
& DNODE_FLAG_USED_BYTES
) == 0);
2163 ASSERT0(P2PHASE(space
, 1<<DEV_BSHIFT
));
2164 dn
->dn_phys
->dn_used
= space
>> DEV_BSHIFT
;
2166 dn
->dn_phys
->dn_used
= space
;
2167 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_USED_BYTES
;
2169 mutex_exit(&dn
->dn_mtx
);
2173 * Scans a block at the indicated "level" looking for a hole or data,
2174 * depending on 'flags'.
2176 * If level > 0, then we are scanning an indirect block looking at its
2177 * pointers. If level == 0, then we are looking at a block of dnodes.
2179 * If we don't find what we are looking for in the block, we return ESRCH.
2180 * Otherwise, return with *offset pointing to the beginning (if searching
2181 * forwards) or end (if searching backwards) of the range covered by the
2182 * block pointer we matched on (or dnode).
2184 * The basic search algorithm used below by dnode_next_offset() is to
2185 * use this function to search up the block tree (widen the search) until
2186 * we find something (i.e., we don't return ESRCH) and then search back
2187 * down the tree (narrow the search) until we reach our original search
2191 dnode_next_offset_level(dnode_t
*dn
, int flags
, uint64_t *offset
,
2192 int lvl
, uint64_t blkfill
, uint64_t txg
)
2194 dmu_buf_impl_t
*db
= NULL
;
2196 uint64_t epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2197 uint64_t epb
= 1ULL << epbs
;
2198 uint64_t minfill
, maxfill
;
2200 int i
, inc
, error
, span
;
2202 hole
= ((flags
& DNODE_FIND_HOLE
) != 0);
2203 inc
= (flags
& DNODE_FIND_BACKWARDS
) ? -1 : 1;
2204 ASSERT(txg
== 0 || !hole
);
2206 if (lvl
== dn
->dn_phys
->dn_nlevels
) {
2208 epb
= dn
->dn_phys
->dn_nblkptr
;
2209 data
= dn
->dn_phys
->dn_blkptr
;
2211 uint64_t blkid
= dbuf_whichblock(dn
, lvl
, *offset
);
2212 error
= dbuf_hold_impl(dn
, lvl
, blkid
, TRUE
, FALSE
, FTAG
, &db
);
2214 if (error
!= ENOENT
)
2219 * This can only happen when we are searching up
2220 * the block tree for data. We don't really need to
2221 * adjust the offset, as we will just end up looking
2222 * at the pointer to this block in its parent, and its
2223 * going to be unallocated, so we will skip over it.
2225 return (SET_ERROR(ESRCH
));
2227 error
= dbuf_read(db
, NULL
,
2228 DB_RF_CANFAIL
| DB_RF_HAVESTRUCT
| DB_RF_NO_DECRYPT
);
2230 dbuf_rele(db
, FTAG
);
2233 data
= db
->db
.db_data
;
2237 if (db
!= NULL
&& txg
!= 0 && (db
->db_blkptr
== NULL
||
2238 db
->db_blkptr
->blk_birth
<= txg
||
2239 BP_IS_HOLE(db
->db_blkptr
))) {
2241 * This can only happen when we are searching up the tree
2242 * and these conditions mean that we need to keep climbing.
2244 error
= SET_ERROR(ESRCH
);
2245 } else if (lvl
== 0) {
2246 dnode_phys_t
*dnp
= data
;
2248 ASSERT(dn
->dn_type
== DMU_OT_DNODE
);
2249 ASSERT(!(flags
& DNODE_FIND_BACKWARDS
));
2251 for (i
= (*offset
>> DNODE_SHIFT
) & (blkfill
- 1);
2252 i
< blkfill
; i
+= dnp
[i
].dn_extra_slots
+ 1) {
2253 if ((dnp
[i
].dn_type
== DMU_OT_NONE
) == hole
)
2258 error
= SET_ERROR(ESRCH
);
2260 *offset
= (*offset
& ~(DNODE_BLOCK_SIZE
- 1)) +
2263 blkptr_t
*bp
= data
;
2264 uint64_t start
= *offset
;
2265 span
= (lvl
- 1) * epbs
+ dn
->dn_datablkshift
;
2267 maxfill
= blkfill
<< ((lvl
- 1) * epbs
);
2274 if (span
>= 8 * sizeof (*offset
)) {
2275 /* This only happens on the highest indirection level */
2276 ASSERT3U((lvl
- 1), ==, dn
->dn_phys
->dn_nlevels
- 1);
2279 *offset
= *offset
>> span
;
2282 for (i
= BF64_GET(*offset
, 0, epbs
);
2283 i
>= 0 && i
< epb
; i
+= inc
) {
2284 if (BP_GET_FILL(&bp
[i
]) >= minfill
&&
2285 BP_GET_FILL(&bp
[i
]) <= maxfill
&&
2286 (hole
|| bp
[i
].blk_birth
> txg
))
2288 if (inc
> 0 || *offset
> 0)
2292 if (span
>= 8 * sizeof (*offset
)) {
2295 *offset
= *offset
<< span
;
2299 /* traversing backwards; position offset at the end */
2300 ASSERT3U(*offset
, <=, start
);
2301 *offset
= MIN(*offset
+ (1ULL << span
) - 1, start
);
2302 } else if (*offset
< start
) {
2305 if (i
< 0 || i
>= epb
)
2306 error
= SET_ERROR(ESRCH
);
2310 dbuf_rele(db
, FTAG
);
2316 * Find the next hole, data, or sparse region at or after *offset.
2317 * The value 'blkfill' tells us how many items we expect to find
2318 * in an L0 data block; this value is 1 for normal objects,
2319 * DNODES_PER_BLOCK for the meta dnode, and some fraction of
2320 * DNODES_PER_BLOCK when searching for sparse regions thereof.
2324 * dnode_next_offset(dn, flags, offset, 1, 1, 0);
2325 * Finds the next/previous hole/data in a file.
2326 * Used in dmu_offset_next().
2328 * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
2329 * Finds the next free/allocated dnode an objset's meta-dnode.
2330 * Only finds objects that have new contents since txg (ie.
2331 * bonus buffer changes and content removal are ignored).
2332 * Used in dmu_object_next().
2334 * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
2335 * Finds the next L2 meta-dnode bp that's at most 1/4 full.
2336 * Used in dmu_object_alloc().
2339 dnode_next_offset(dnode_t
*dn
, int flags
, uint64_t *offset
,
2340 int minlvl
, uint64_t blkfill
, uint64_t txg
)
2342 uint64_t initial_offset
= *offset
;
2346 if (!(flags
& DNODE_FIND_HAVELOCK
))
2347 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2349 if (dn
->dn_phys
->dn_nlevels
== 0) {
2350 error
= SET_ERROR(ESRCH
);
2354 if (dn
->dn_datablkshift
== 0) {
2355 if (*offset
< dn
->dn_datablksz
) {
2356 if (flags
& DNODE_FIND_HOLE
)
2357 *offset
= dn
->dn_datablksz
;
2359 error
= SET_ERROR(ESRCH
);
2364 maxlvl
= dn
->dn_phys
->dn_nlevels
;
2366 for (lvl
= minlvl
; lvl
<= maxlvl
; lvl
++) {
2367 error
= dnode_next_offset_level(dn
,
2368 flags
, offset
, lvl
, blkfill
, txg
);
2373 while (error
== 0 && --lvl
>= minlvl
) {
2374 error
= dnode_next_offset_level(dn
,
2375 flags
, offset
, lvl
, blkfill
, txg
);
2379 * There's always a "virtual hole" at the end of the object, even
2380 * if all BP's which physically exist are non-holes.
2382 if ((flags
& DNODE_FIND_HOLE
) && error
== ESRCH
&& txg
== 0 &&
2383 minlvl
== 1 && blkfill
== 1 && !(flags
& DNODE_FIND_BACKWARDS
)) {
2387 if (error
== 0 && (flags
& DNODE_FIND_BACKWARDS
?
2388 initial_offset
< *offset
: initial_offset
> *offset
))
2389 error
= SET_ERROR(ESRCH
);
2391 if (!(flags
& DNODE_FIND_HAVELOCK
))
2392 rw_exit(&dn
->dn_struct_rwlock
);