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 https://opensource.org/licenses/CDDL-1.0.
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, 2020 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_zfs.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 dnode_sums_t dnode_sums
;
76 static kstat_t
*dnode_ksp
;
77 static kmem_cache_t
*dnode_cache
;
79 static dnode_phys_t dnode_phys_zero __maybe_unused
;
81 int zfs_default_bs
= SPA_MINBLOCKSHIFT
;
82 int zfs_default_ibs
= DN_MAX_INDBLKSHIFT
;
85 static kmem_cbrc_t
dnode_move(void *, void *, size_t, void *);
89 dbuf_compare(const void *x1
, const void *x2
)
91 const dmu_buf_impl_t
*d1
= x1
;
92 const dmu_buf_impl_t
*d2
= x2
;
94 int cmp
= TREE_CMP(d1
->db_level
, d2
->db_level
);
98 cmp
= TREE_CMP(d1
->db_blkid
, d2
->db_blkid
);
102 if (d1
->db_state
== DB_SEARCH
) {
103 ASSERT3S(d2
->db_state
, !=, DB_SEARCH
);
105 } else if (d2
->db_state
== DB_SEARCH
) {
106 ASSERT3S(d1
->db_state
, !=, DB_SEARCH
);
110 return (TREE_PCMP(d1
, d2
));
114 dnode_cons(void *arg
, void *unused
, int kmflag
)
116 (void) unused
, (void) 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
);
123 cv_init(&dn
->dn_nodnholds
, NULL
, CV_DEFAULT
, NULL
);
126 * Every dbuf has a reference, and dropping a tracked reference is
127 * O(number of references), so don't track dn_holds.
129 zfs_refcount_create_untracked(&dn
->dn_holds
);
130 zfs_refcount_create(&dn
->dn_tx_holds
);
131 list_link_init(&dn
->dn_link
);
133 memset(dn
->dn_next_type
, 0, sizeof (dn
->dn_next_type
));
134 memset(dn
->dn_next_nblkptr
, 0, sizeof (dn
->dn_next_nblkptr
));
135 memset(dn
->dn_next_nlevels
, 0, sizeof (dn
->dn_next_nlevels
));
136 memset(dn
->dn_next_indblkshift
, 0, sizeof (dn
->dn_next_indblkshift
));
137 memset(dn
->dn_next_bonustype
, 0, sizeof (dn
->dn_next_bonustype
));
138 memset(dn
->dn_rm_spillblk
, 0, sizeof (dn
->dn_rm_spillblk
));
139 memset(dn
->dn_next_bonuslen
, 0, sizeof (dn
->dn_next_bonuslen
));
140 memset(dn
->dn_next_blksz
, 0, sizeof (dn
->dn_next_blksz
));
141 memset(dn
->dn_next_maxblkid
, 0, sizeof (dn
->dn_next_maxblkid
));
143 for (int i
= 0; i
< TXG_SIZE
; i
++) {
144 multilist_link_init(&dn
->dn_dirty_link
[i
]);
145 dn
->dn_free_ranges
[i
] = NULL
;
146 list_create(&dn
->dn_dirty_records
[i
],
147 sizeof (dbuf_dirty_record_t
),
148 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
151 dn
->dn_allocated_txg
= 0;
153 dn
->dn_assigned_txg
= 0;
154 dn
->dn_dirty_txg
= 0;
156 dn
->dn_dirtyctx_firstset
= NULL
;
158 dn
->dn_have_spill
= B_FALSE
;
164 dn
->dn_oldprojid
= ZFS_DEFAULT_PROJID
;
167 dn
->dn_newprojid
= ZFS_DEFAULT_PROJID
;
170 dn
->dn_dbufs_count
= 0;
171 avl_create(&dn
->dn_dbufs
, dbuf_compare
, sizeof (dmu_buf_impl_t
),
172 offsetof(dmu_buf_impl_t
, db_link
));
179 dnode_dest(void *arg
, void *unused
)
184 rw_destroy(&dn
->dn_struct_rwlock
);
185 mutex_destroy(&dn
->dn_mtx
);
186 mutex_destroy(&dn
->dn_dbufs_mtx
);
187 cv_destroy(&dn
->dn_notxholds
);
188 cv_destroy(&dn
->dn_nodnholds
);
189 zfs_refcount_destroy(&dn
->dn_holds
);
190 zfs_refcount_destroy(&dn
->dn_tx_holds
);
191 ASSERT(!list_link_active(&dn
->dn_link
));
193 for (int i
= 0; i
< TXG_SIZE
; i
++) {
194 ASSERT(!multilist_link_active(&dn
->dn_dirty_link
[i
]));
195 ASSERT3P(dn
->dn_free_ranges
[i
], ==, NULL
);
196 list_destroy(&dn
->dn_dirty_records
[i
]);
197 ASSERT0(dn
->dn_next_nblkptr
[i
]);
198 ASSERT0(dn
->dn_next_nlevels
[i
]);
199 ASSERT0(dn
->dn_next_indblkshift
[i
]);
200 ASSERT0(dn
->dn_next_bonustype
[i
]);
201 ASSERT0(dn
->dn_rm_spillblk
[i
]);
202 ASSERT0(dn
->dn_next_bonuslen
[i
]);
203 ASSERT0(dn
->dn_next_blksz
[i
]);
204 ASSERT0(dn
->dn_next_maxblkid
[i
]);
207 ASSERT0(dn
->dn_allocated_txg
);
208 ASSERT0(dn
->dn_free_txg
);
209 ASSERT0(dn
->dn_assigned_txg
);
210 ASSERT0(dn
->dn_dirty_txg
);
211 ASSERT0(dn
->dn_dirtyctx
);
212 ASSERT3P(dn
->dn_dirtyctx_firstset
, ==, NULL
);
213 ASSERT3P(dn
->dn_bonus
, ==, NULL
);
214 ASSERT(!dn
->dn_have_spill
);
215 ASSERT3P(dn
->dn_zio
, ==, NULL
);
216 ASSERT0(dn
->dn_oldused
);
217 ASSERT0(dn
->dn_oldflags
);
218 ASSERT0(dn
->dn_olduid
);
219 ASSERT0(dn
->dn_oldgid
);
220 ASSERT0(dn
->dn_oldprojid
);
221 ASSERT0(dn
->dn_newuid
);
222 ASSERT0(dn
->dn_newgid
);
223 ASSERT0(dn
->dn_newprojid
);
224 ASSERT0(dn
->dn_id_flags
);
226 ASSERT0(dn
->dn_dbufs_count
);
227 avl_destroy(&dn
->dn_dbufs
);
231 dnode_kstats_update(kstat_t
*ksp
, int rw
)
233 dnode_stats_t
*ds
= ksp
->ks_data
;
235 if (rw
== KSTAT_WRITE
)
237 ds
->dnode_hold_dbuf_hold
.value
.ui64
=
238 wmsum_value(&dnode_sums
.dnode_hold_dbuf_hold
);
239 ds
->dnode_hold_dbuf_read
.value
.ui64
=
240 wmsum_value(&dnode_sums
.dnode_hold_dbuf_read
);
241 ds
->dnode_hold_alloc_hits
.value
.ui64
=
242 wmsum_value(&dnode_sums
.dnode_hold_alloc_hits
);
243 ds
->dnode_hold_alloc_misses
.value
.ui64
=
244 wmsum_value(&dnode_sums
.dnode_hold_alloc_misses
);
245 ds
->dnode_hold_alloc_interior
.value
.ui64
=
246 wmsum_value(&dnode_sums
.dnode_hold_alloc_interior
);
247 ds
->dnode_hold_alloc_lock_retry
.value
.ui64
=
248 wmsum_value(&dnode_sums
.dnode_hold_alloc_lock_retry
);
249 ds
->dnode_hold_alloc_lock_misses
.value
.ui64
=
250 wmsum_value(&dnode_sums
.dnode_hold_alloc_lock_misses
);
251 ds
->dnode_hold_alloc_type_none
.value
.ui64
=
252 wmsum_value(&dnode_sums
.dnode_hold_alloc_type_none
);
253 ds
->dnode_hold_free_hits
.value
.ui64
=
254 wmsum_value(&dnode_sums
.dnode_hold_free_hits
);
255 ds
->dnode_hold_free_misses
.value
.ui64
=
256 wmsum_value(&dnode_sums
.dnode_hold_free_misses
);
257 ds
->dnode_hold_free_lock_misses
.value
.ui64
=
258 wmsum_value(&dnode_sums
.dnode_hold_free_lock_misses
);
259 ds
->dnode_hold_free_lock_retry
.value
.ui64
=
260 wmsum_value(&dnode_sums
.dnode_hold_free_lock_retry
);
261 ds
->dnode_hold_free_refcount
.value
.ui64
=
262 wmsum_value(&dnode_sums
.dnode_hold_free_refcount
);
263 ds
->dnode_hold_free_overflow
.value
.ui64
=
264 wmsum_value(&dnode_sums
.dnode_hold_free_overflow
);
265 ds
->dnode_free_interior_lock_retry
.value
.ui64
=
266 wmsum_value(&dnode_sums
.dnode_free_interior_lock_retry
);
267 ds
->dnode_allocate
.value
.ui64
=
268 wmsum_value(&dnode_sums
.dnode_allocate
);
269 ds
->dnode_reallocate
.value
.ui64
=
270 wmsum_value(&dnode_sums
.dnode_reallocate
);
271 ds
->dnode_buf_evict
.value
.ui64
=
272 wmsum_value(&dnode_sums
.dnode_buf_evict
);
273 ds
->dnode_alloc_next_chunk
.value
.ui64
=
274 wmsum_value(&dnode_sums
.dnode_alloc_next_chunk
);
275 ds
->dnode_alloc_race
.value
.ui64
=
276 wmsum_value(&dnode_sums
.dnode_alloc_race
);
277 ds
->dnode_alloc_next_block
.value
.ui64
=
278 wmsum_value(&dnode_sums
.dnode_alloc_next_block
);
279 ds
->dnode_move_invalid
.value
.ui64
=
280 wmsum_value(&dnode_sums
.dnode_move_invalid
);
281 ds
->dnode_move_recheck1
.value
.ui64
=
282 wmsum_value(&dnode_sums
.dnode_move_recheck1
);
283 ds
->dnode_move_recheck2
.value
.ui64
=
284 wmsum_value(&dnode_sums
.dnode_move_recheck2
);
285 ds
->dnode_move_special
.value
.ui64
=
286 wmsum_value(&dnode_sums
.dnode_move_special
);
287 ds
->dnode_move_handle
.value
.ui64
=
288 wmsum_value(&dnode_sums
.dnode_move_handle
);
289 ds
->dnode_move_rwlock
.value
.ui64
=
290 wmsum_value(&dnode_sums
.dnode_move_rwlock
);
291 ds
->dnode_move_active
.value
.ui64
=
292 wmsum_value(&dnode_sums
.dnode_move_active
);
299 ASSERT(dnode_cache
== NULL
);
300 dnode_cache
= kmem_cache_create("dnode_t", sizeof (dnode_t
),
301 0, dnode_cons
, dnode_dest
, NULL
, NULL
, NULL
, 0);
302 kmem_cache_set_move(dnode_cache
, dnode_move
);
304 wmsum_init(&dnode_sums
.dnode_hold_dbuf_hold
, 0);
305 wmsum_init(&dnode_sums
.dnode_hold_dbuf_read
, 0);
306 wmsum_init(&dnode_sums
.dnode_hold_alloc_hits
, 0);
307 wmsum_init(&dnode_sums
.dnode_hold_alloc_misses
, 0);
308 wmsum_init(&dnode_sums
.dnode_hold_alloc_interior
, 0);
309 wmsum_init(&dnode_sums
.dnode_hold_alloc_lock_retry
, 0);
310 wmsum_init(&dnode_sums
.dnode_hold_alloc_lock_misses
, 0);
311 wmsum_init(&dnode_sums
.dnode_hold_alloc_type_none
, 0);
312 wmsum_init(&dnode_sums
.dnode_hold_free_hits
, 0);
313 wmsum_init(&dnode_sums
.dnode_hold_free_misses
, 0);
314 wmsum_init(&dnode_sums
.dnode_hold_free_lock_misses
, 0);
315 wmsum_init(&dnode_sums
.dnode_hold_free_lock_retry
, 0);
316 wmsum_init(&dnode_sums
.dnode_hold_free_refcount
, 0);
317 wmsum_init(&dnode_sums
.dnode_hold_free_overflow
, 0);
318 wmsum_init(&dnode_sums
.dnode_free_interior_lock_retry
, 0);
319 wmsum_init(&dnode_sums
.dnode_allocate
, 0);
320 wmsum_init(&dnode_sums
.dnode_reallocate
, 0);
321 wmsum_init(&dnode_sums
.dnode_buf_evict
, 0);
322 wmsum_init(&dnode_sums
.dnode_alloc_next_chunk
, 0);
323 wmsum_init(&dnode_sums
.dnode_alloc_race
, 0);
324 wmsum_init(&dnode_sums
.dnode_alloc_next_block
, 0);
325 wmsum_init(&dnode_sums
.dnode_move_invalid
, 0);
326 wmsum_init(&dnode_sums
.dnode_move_recheck1
, 0);
327 wmsum_init(&dnode_sums
.dnode_move_recheck2
, 0);
328 wmsum_init(&dnode_sums
.dnode_move_special
, 0);
329 wmsum_init(&dnode_sums
.dnode_move_handle
, 0);
330 wmsum_init(&dnode_sums
.dnode_move_rwlock
, 0);
331 wmsum_init(&dnode_sums
.dnode_move_active
, 0);
333 dnode_ksp
= kstat_create("zfs", 0, "dnodestats", "misc",
334 KSTAT_TYPE_NAMED
, sizeof (dnode_stats
) / sizeof (kstat_named_t
),
336 if (dnode_ksp
!= NULL
) {
337 dnode_ksp
->ks_data
= &dnode_stats
;
338 dnode_ksp
->ks_update
= dnode_kstats_update
;
339 kstat_install(dnode_ksp
);
346 if (dnode_ksp
!= NULL
) {
347 kstat_delete(dnode_ksp
);
351 wmsum_fini(&dnode_sums
.dnode_hold_dbuf_hold
);
352 wmsum_fini(&dnode_sums
.dnode_hold_dbuf_read
);
353 wmsum_fini(&dnode_sums
.dnode_hold_alloc_hits
);
354 wmsum_fini(&dnode_sums
.dnode_hold_alloc_misses
);
355 wmsum_fini(&dnode_sums
.dnode_hold_alloc_interior
);
356 wmsum_fini(&dnode_sums
.dnode_hold_alloc_lock_retry
);
357 wmsum_fini(&dnode_sums
.dnode_hold_alloc_lock_misses
);
358 wmsum_fini(&dnode_sums
.dnode_hold_alloc_type_none
);
359 wmsum_fini(&dnode_sums
.dnode_hold_free_hits
);
360 wmsum_fini(&dnode_sums
.dnode_hold_free_misses
);
361 wmsum_fini(&dnode_sums
.dnode_hold_free_lock_misses
);
362 wmsum_fini(&dnode_sums
.dnode_hold_free_lock_retry
);
363 wmsum_fini(&dnode_sums
.dnode_hold_free_refcount
);
364 wmsum_fini(&dnode_sums
.dnode_hold_free_overflow
);
365 wmsum_fini(&dnode_sums
.dnode_free_interior_lock_retry
);
366 wmsum_fini(&dnode_sums
.dnode_allocate
);
367 wmsum_fini(&dnode_sums
.dnode_reallocate
);
368 wmsum_fini(&dnode_sums
.dnode_buf_evict
);
369 wmsum_fini(&dnode_sums
.dnode_alloc_next_chunk
);
370 wmsum_fini(&dnode_sums
.dnode_alloc_race
);
371 wmsum_fini(&dnode_sums
.dnode_alloc_next_block
);
372 wmsum_fini(&dnode_sums
.dnode_move_invalid
);
373 wmsum_fini(&dnode_sums
.dnode_move_recheck1
);
374 wmsum_fini(&dnode_sums
.dnode_move_recheck2
);
375 wmsum_fini(&dnode_sums
.dnode_move_special
);
376 wmsum_fini(&dnode_sums
.dnode_move_handle
);
377 wmsum_fini(&dnode_sums
.dnode_move_rwlock
);
378 wmsum_fini(&dnode_sums
.dnode_move_active
);
380 kmem_cache_destroy(dnode_cache
);
387 dnode_verify(dnode_t
*dn
)
389 int drop_struct_lock
= FALSE
;
392 ASSERT(dn
->dn_objset
);
393 ASSERT(dn
->dn_handle
->dnh_dnode
== dn
);
395 ASSERT(DMU_OT_IS_VALID(dn
->dn_phys
->dn_type
));
397 if (!(zfs_flags
& ZFS_DEBUG_DNODE_VERIFY
))
400 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
401 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
402 drop_struct_lock
= TRUE
;
404 if (dn
->dn_phys
->dn_type
!= DMU_OT_NONE
|| dn
->dn_allocated_txg
!= 0) {
406 int max_bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
407 ASSERT3U(dn
->dn_indblkshift
, <=, SPA_MAXBLOCKSHIFT
);
408 if (dn
->dn_datablkshift
) {
409 ASSERT3U(dn
->dn_datablkshift
, >=, SPA_MINBLOCKSHIFT
);
410 ASSERT3U(dn
->dn_datablkshift
, <=, SPA_MAXBLOCKSHIFT
);
411 ASSERT3U(1<<dn
->dn_datablkshift
, ==, dn
->dn_datablksz
);
413 ASSERT3U(dn
->dn_nlevels
, <=, 30);
414 ASSERT(DMU_OT_IS_VALID(dn
->dn_type
));
415 ASSERT3U(dn
->dn_nblkptr
, >=, 1);
416 ASSERT3U(dn
->dn_nblkptr
, <=, DN_MAX_NBLKPTR
);
417 ASSERT3U(dn
->dn_bonuslen
, <=, max_bonuslen
);
418 ASSERT3U(dn
->dn_datablksz
, ==,
419 dn
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
);
420 ASSERT3U(ISP2(dn
->dn_datablksz
), ==, dn
->dn_datablkshift
!= 0);
421 ASSERT3U((dn
->dn_nblkptr
- 1) * sizeof (blkptr_t
) +
422 dn
->dn_bonuslen
, <=, max_bonuslen
);
423 for (i
= 0; i
< TXG_SIZE
; i
++) {
424 ASSERT3U(dn
->dn_next_nlevels
[i
], <=, dn
->dn_nlevels
);
427 if (dn
->dn_phys
->dn_type
!= DMU_OT_NONE
)
428 ASSERT3U(dn
->dn_phys
->dn_nlevels
, <=, dn
->dn_nlevels
);
429 ASSERT(DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) || dn
->dn_dbuf
!= NULL
);
430 if (dn
->dn_dbuf
!= NULL
) {
431 ASSERT3P(dn
->dn_phys
, ==,
432 (dnode_phys_t
*)dn
->dn_dbuf
->db
.db_data
+
433 (dn
->dn_object
% (dn
->dn_dbuf
->db
.db_size
>> DNODE_SHIFT
)));
435 if (drop_struct_lock
)
436 rw_exit(&dn
->dn_struct_rwlock
);
441 dnode_byteswap(dnode_phys_t
*dnp
)
443 uint64_t *buf64
= (void*)&dnp
->dn_blkptr
;
446 if (dnp
->dn_type
== DMU_OT_NONE
) {
447 memset(dnp
, 0, sizeof (dnode_phys_t
));
451 dnp
->dn_datablkszsec
= BSWAP_16(dnp
->dn_datablkszsec
);
452 dnp
->dn_bonuslen
= BSWAP_16(dnp
->dn_bonuslen
);
453 dnp
->dn_extra_slots
= BSWAP_8(dnp
->dn_extra_slots
);
454 dnp
->dn_maxblkid
= BSWAP_64(dnp
->dn_maxblkid
);
455 dnp
->dn_used
= BSWAP_64(dnp
->dn_used
);
458 * dn_nblkptr is only one byte, so it's OK to read it in either
459 * byte order. We can't read dn_bouslen.
461 ASSERT(dnp
->dn_indblkshift
<= SPA_MAXBLOCKSHIFT
);
462 ASSERT(dnp
->dn_nblkptr
<= DN_MAX_NBLKPTR
);
463 for (i
= 0; i
< dnp
->dn_nblkptr
* sizeof (blkptr_t
)/8; i
++)
464 buf64
[i
] = BSWAP_64(buf64
[i
]);
467 * OK to check dn_bonuslen for zero, because it won't matter if
468 * we have the wrong byte order. This is necessary because the
469 * dnode dnode is smaller than a regular dnode.
471 if (dnp
->dn_bonuslen
!= 0) {
472 dmu_object_byteswap_t byteswap
;
473 ASSERT(DMU_OT_IS_VALID(dnp
->dn_bonustype
));
474 byteswap
= DMU_OT_BYTESWAP(dnp
->dn_bonustype
);
475 dmu_ot_byteswap
[byteswap
].ob_func(DN_BONUS(dnp
),
476 DN_MAX_BONUS_LEN(dnp
));
479 /* Swap SPILL block if we have one */
480 if (dnp
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
)
481 byteswap_uint64_array(DN_SPILL_BLKPTR(dnp
), sizeof (blkptr_t
));
485 dnode_buf_byteswap(void *vbuf
, size_t size
)
489 ASSERT3U(sizeof (dnode_phys_t
), ==, (1<<DNODE_SHIFT
));
490 ASSERT((size
& (sizeof (dnode_phys_t
)-1)) == 0);
493 dnode_phys_t
*dnp
= (void *)(((char *)vbuf
) + i
);
497 if (dnp
->dn_type
!= DMU_OT_NONE
)
498 i
+= dnp
->dn_extra_slots
* DNODE_MIN_SIZE
;
503 dnode_setbonuslen(dnode_t
*dn
, int newsize
, dmu_tx_t
*tx
)
505 ASSERT3U(zfs_refcount_count(&dn
->dn_holds
), >=, 1);
507 dnode_setdirty(dn
, tx
);
508 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
509 ASSERT3U(newsize
, <=, DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
510 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
));
512 if (newsize
< dn
->dn_bonuslen
) {
513 /* clear any data after the end of the new size */
514 size_t diff
= dn
->dn_bonuslen
- newsize
;
515 char *data_end
= ((char *)dn
->dn_bonus
->db
.db_data
) + newsize
;
516 memset(data_end
, 0, diff
);
519 dn
->dn_bonuslen
= newsize
;
521 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = DN_ZERO_BONUSLEN
;
523 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonuslen
;
524 rw_exit(&dn
->dn_struct_rwlock
);
528 dnode_setbonus_type(dnode_t
*dn
, dmu_object_type_t newtype
, dmu_tx_t
*tx
)
530 ASSERT3U(zfs_refcount_count(&dn
->dn_holds
), >=, 1);
531 dnode_setdirty(dn
, tx
);
532 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
533 dn
->dn_bonustype
= newtype
;
534 dn
->dn_next_bonustype
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonustype
;
535 rw_exit(&dn
->dn_struct_rwlock
);
539 dnode_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
541 ASSERT3U(zfs_refcount_count(&dn
->dn_holds
), >=, 1);
542 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
543 dnode_setdirty(dn
, tx
);
544 dn
->dn_rm_spillblk
[tx
->tx_txg
& TXG_MASK
] = DN_KILL_SPILLBLK
;
545 dn
->dn_have_spill
= B_FALSE
;
549 dnode_setdblksz(dnode_t
*dn
, int size
)
551 ASSERT0(P2PHASE(size
, SPA_MINBLOCKSIZE
));
552 ASSERT3U(size
, <=, SPA_MAXBLOCKSIZE
);
553 ASSERT3U(size
, >=, SPA_MINBLOCKSIZE
);
554 ASSERT3U(size
>> SPA_MINBLOCKSHIFT
, <,
555 1<<(sizeof (dn
->dn_phys
->dn_datablkszsec
) * 8));
556 dn
->dn_datablksz
= size
;
557 dn
->dn_datablkszsec
= size
>> SPA_MINBLOCKSHIFT
;
558 dn
->dn_datablkshift
= ISP2(size
) ? highbit64(size
- 1) : 0;
562 dnode_create(objset_t
*os
, dnode_phys_t
*dnp
, dmu_buf_impl_t
*db
,
563 uint64_t object
, dnode_handle_t
*dnh
)
567 dn
= kmem_cache_alloc(dnode_cache
, KM_SLEEP
);
571 * Defer setting dn_objset until the dnode is ready to be a candidate
572 * for the dnode_move() callback.
574 dn
->dn_object
= object
;
579 if (dnp
->dn_datablkszsec
) {
580 dnode_setdblksz(dn
, dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
);
582 dn
->dn_datablksz
= 0;
583 dn
->dn_datablkszsec
= 0;
584 dn
->dn_datablkshift
= 0;
586 dn
->dn_indblkshift
= dnp
->dn_indblkshift
;
587 dn
->dn_nlevels
= dnp
->dn_nlevels
;
588 dn
->dn_type
= dnp
->dn_type
;
589 dn
->dn_nblkptr
= dnp
->dn_nblkptr
;
590 dn
->dn_checksum
= dnp
->dn_checksum
;
591 dn
->dn_compress
= dnp
->dn_compress
;
592 dn
->dn_bonustype
= dnp
->dn_bonustype
;
593 dn
->dn_bonuslen
= dnp
->dn_bonuslen
;
594 dn
->dn_num_slots
= dnp
->dn_extra_slots
+ 1;
595 dn
->dn_maxblkid
= dnp
->dn_maxblkid
;
596 dn
->dn_have_spill
= ((dnp
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
) != 0);
599 dmu_zfetch_init(&dn
->dn_zfetch
, dn
);
601 ASSERT(DMU_OT_IS_VALID(dn
->dn_phys
->dn_type
));
602 ASSERT(zrl_is_locked(&dnh
->dnh_zrlock
));
603 ASSERT(!DN_SLOT_IS_PTR(dnh
->dnh_dnode
));
605 mutex_enter(&os
->os_lock
);
608 * Exclude special dnodes from os_dnodes so an empty os_dnodes
609 * signifies that the special dnodes have no references from
610 * their children (the entries in os_dnodes). This allows
611 * dnode_destroy() to easily determine if the last child has
612 * been removed and then complete eviction of the objset.
614 if (!DMU_OBJECT_IS_SPECIAL(object
))
615 list_insert_head(&os
->os_dnodes
, dn
);
619 * Everything else must be valid before assigning dn_objset
620 * makes the dnode eligible for dnode_move().
625 mutex_exit(&os
->os_lock
);
627 arc_space_consume(sizeof (dnode_t
), ARC_SPACE_DNODE
);
633 * Caller must be holding the dnode handle, which is released upon return.
636 dnode_destroy(dnode_t
*dn
)
638 objset_t
*os
= dn
->dn_objset
;
639 boolean_t complete_os_eviction
= B_FALSE
;
641 ASSERT((dn
->dn_id_flags
& DN_ID_NEW_EXIST
) == 0);
643 mutex_enter(&os
->os_lock
);
644 POINTER_INVALIDATE(&dn
->dn_objset
);
645 if (!DMU_OBJECT_IS_SPECIAL(dn
->dn_object
)) {
646 list_remove(&os
->os_dnodes
, dn
);
647 complete_os_eviction
=
648 list_is_empty(&os
->os_dnodes
) &&
649 list_link_active(&os
->os_evicting_node
);
651 mutex_exit(&os
->os_lock
);
653 /* the dnode can no longer move, so we can release the handle */
654 if (!zrl_is_locked(&dn
->dn_handle
->dnh_zrlock
))
655 zrl_remove(&dn
->dn_handle
->dnh_zrlock
);
657 dn
->dn_allocated_txg
= 0;
659 dn
->dn_assigned_txg
= 0;
660 dn
->dn_dirty_txg
= 0;
663 dn
->dn_dirtyctx_firstset
= NULL
;
664 if (dn
->dn_bonus
!= NULL
) {
665 mutex_enter(&dn
->dn_bonus
->db_mtx
);
666 dbuf_destroy(dn
->dn_bonus
);
671 dn
->dn_have_spill
= B_FALSE
;
676 dn
->dn_oldprojid
= ZFS_DEFAULT_PROJID
;
679 dn
->dn_newprojid
= ZFS_DEFAULT_PROJID
;
682 dmu_zfetch_fini(&dn
->dn_zfetch
);
683 kmem_cache_free(dnode_cache
, dn
);
684 arc_space_return(sizeof (dnode_t
), ARC_SPACE_DNODE
);
686 if (complete_os_eviction
)
687 dmu_objset_evict_done(os
);
691 dnode_allocate(dnode_t
*dn
, dmu_object_type_t ot
, int blocksize
, int ibs
,
692 dmu_object_type_t bonustype
, int bonuslen
, int dn_slots
, dmu_tx_t
*tx
)
696 ASSERT3U(dn_slots
, >, 0);
697 ASSERT3U(dn_slots
<< DNODE_SHIFT
, <=,
698 spa_maxdnodesize(dmu_objset_spa(dn
->dn_objset
)));
699 ASSERT3U(blocksize
, <=,
700 spa_maxblocksize(dmu_objset_spa(dn
->dn_objset
)));
702 blocksize
= 1 << zfs_default_bs
;
704 blocksize
= P2ROUNDUP(blocksize
, SPA_MINBLOCKSIZE
);
707 ibs
= zfs_default_ibs
;
709 ibs
= MIN(MAX(ibs
, DN_MIN_INDBLKSHIFT
), DN_MAX_INDBLKSHIFT
);
711 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d dn_slots=%d\n",
712 dn
->dn_objset
, (u_longlong_t
)dn
->dn_object
,
713 (u_longlong_t
)tx
->tx_txg
, blocksize
, ibs
, dn_slots
);
714 DNODE_STAT_BUMP(dnode_allocate
);
716 ASSERT(dn
->dn_type
== DMU_OT_NONE
);
717 ASSERT0(memcmp(dn
->dn_phys
, &dnode_phys_zero
, sizeof (dnode_phys_t
)));
718 ASSERT(dn
->dn_phys
->dn_type
== DMU_OT_NONE
);
719 ASSERT(ot
!= DMU_OT_NONE
);
720 ASSERT(DMU_OT_IS_VALID(ot
));
721 ASSERT((bonustype
== DMU_OT_NONE
&& bonuslen
== 0) ||
722 (bonustype
== DMU_OT_SA
&& bonuslen
== 0) ||
723 (bonustype
!= DMU_OT_NONE
&& bonuslen
!= 0));
724 ASSERT(DMU_OT_IS_VALID(bonustype
));
725 ASSERT3U(bonuslen
, <=, DN_SLOTS_TO_BONUSLEN(dn_slots
));
726 ASSERT(dn
->dn_type
== DMU_OT_NONE
);
727 ASSERT0(dn
->dn_maxblkid
);
728 ASSERT0(dn
->dn_allocated_txg
);
729 ASSERT0(dn
->dn_assigned_txg
);
730 ASSERT(zfs_refcount_is_zero(&dn
->dn_tx_holds
));
731 ASSERT3U(zfs_refcount_count(&dn
->dn_holds
), <=, 1);
732 ASSERT(avl_is_empty(&dn
->dn_dbufs
));
734 for (i
= 0; i
< TXG_SIZE
; i
++) {
735 ASSERT0(dn
->dn_next_nblkptr
[i
]);
736 ASSERT0(dn
->dn_next_nlevels
[i
]);
737 ASSERT0(dn
->dn_next_indblkshift
[i
]);
738 ASSERT0(dn
->dn_next_bonuslen
[i
]);
739 ASSERT0(dn
->dn_next_bonustype
[i
]);
740 ASSERT0(dn
->dn_rm_spillblk
[i
]);
741 ASSERT0(dn
->dn_next_blksz
[i
]);
742 ASSERT0(dn
->dn_next_maxblkid
[i
]);
743 ASSERT(!multilist_link_active(&dn
->dn_dirty_link
[i
]));
744 ASSERT3P(list_head(&dn
->dn_dirty_records
[i
]), ==, NULL
);
745 ASSERT3P(dn
->dn_free_ranges
[i
], ==, NULL
);
749 dnode_setdblksz(dn
, blocksize
);
750 dn
->dn_indblkshift
= ibs
;
752 dn
->dn_num_slots
= dn_slots
;
753 if (bonustype
== DMU_OT_SA
) /* Maximize bonus space for SA */
756 dn
->dn_nblkptr
= MIN(DN_MAX_NBLKPTR
,
757 1 + ((DN_SLOTS_TO_BONUSLEN(dn_slots
) - bonuslen
) >>
761 dn
->dn_bonustype
= bonustype
;
762 dn
->dn_bonuslen
= bonuslen
;
763 dn
->dn_checksum
= ZIO_CHECKSUM_INHERIT
;
764 dn
->dn_compress
= ZIO_COMPRESS_INHERIT
;
768 dn
->dn_dirtyctx_firstset
= NULL
;
769 dn
->dn_dirty_txg
= 0;
771 dn
->dn_allocated_txg
= tx
->tx_txg
;
774 dnode_setdirty(dn
, tx
);
775 dn
->dn_next_indblkshift
[tx
->tx_txg
& TXG_MASK
] = ibs
;
776 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonuslen
;
777 dn
->dn_next_bonustype
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonustype
;
778 dn
->dn_next_blksz
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_datablksz
;
782 dnode_reallocate(dnode_t
*dn
, dmu_object_type_t ot
, int blocksize
,
783 dmu_object_type_t bonustype
, int bonuslen
, int dn_slots
,
784 boolean_t keep_spill
, dmu_tx_t
*tx
)
788 ASSERT3U(blocksize
, >=, SPA_MINBLOCKSIZE
);
789 ASSERT3U(blocksize
, <=,
790 spa_maxblocksize(dmu_objset_spa(dn
->dn_objset
)));
791 ASSERT0(blocksize
% SPA_MINBLOCKSIZE
);
792 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
|| dmu_tx_private_ok(tx
));
793 ASSERT(tx
->tx_txg
!= 0);
794 ASSERT((bonustype
== DMU_OT_NONE
&& bonuslen
== 0) ||
795 (bonustype
!= DMU_OT_NONE
&& bonuslen
!= 0) ||
796 (bonustype
== DMU_OT_SA
&& bonuslen
== 0));
797 ASSERT(DMU_OT_IS_VALID(bonustype
));
798 ASSERT3U(bonuslen
, <=,
799 DN_BONUS_SIZE(spa_maxdnodesize(dmu_objset_spa(dn
->dn_objset
))));
800 ASSERT3U(bonuslen
, <=, DN_BONUS_SIZE(dn_slots
<< DNODE_SHIFT
));
802 dnode_free_interior_slots(dn
);
803 DNODE_STAT_BUMP(dnode_reallocate
);
805 /* clean up any unreferenced dbufs */
806 dnode_evict_dbufs(dn
);
810 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
811 dnode_setdirty(dn
, tx
);
812 if (dn
->dn_datablksz
!= blocksize
) {
813 /* change blocksize */
814 ASSERT0(dn
->dn_maxblkid
);
815 ASSERT(BP_IS_HOLE(&dn
->dn_phys
->dn_blkptr
[0]) ||
816 dnode_block_freed(dn
, 0));
818 dnode_setdblksz(dn
, blocksize
);
819 dn
->dn_next_blksz
[tx
->tx_txg
& TXG_MASK
] = blocksize
;
821 if (dn
->dn_bonuslen
!= bonuslen
)
822 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = bonuslen
;
824 if (bonustype
== DMU_OT_SA
) /* Maximize bonus space for SA */
827 nblkptr
= MIN(DN_MAX_NBLKPTR
,
828 1 + ((DN_SLOTS_TO_BONUSLEN(dn_slots
) - bonuslen
) >>
830 if (dn
->dn_bonustype
!= bonustype
)
831 dn
->dn_next_bonustype
[tx
->tx_txg
& TXG_MASK
] = bonustype
;
832 if (dn
->dn_nblkptr
!= nblkptr
)
833 dn
->dn_next_nblkptr
[tx
->tx_txg
& TXG_MASK
] = nblkptr
;
834 if (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
&& !keep_spill
) {
835 dbuf_rm_spill(dn
, tx
);
836 dnode_rm_spill(dn
, tx
);
839 rw_exit(&dn
->dn_struct_rwlock
);
844 /* change bonus size and type */
845 mutex_enter(&dn
->dn_mtx
);
846 dn
->dn_bonustype
= bonustype
;
847 dn
->dn_bonuslen
= bonuslen
;
848 dn
->dn_num_slots
= dn_slots
;
849 dn
->dn_nblkptr
= nblkptr
;
850 dn
->dn_checksum
= ZIO_CHECKSUM_INHERIT
;
851 dn
->dn_compress
= ZIO_COMPRESS_INHERIT
;
852 ASSERT3U(dn
->dn_nblkptr
, <=, DN_MAX_NBLKPTR
);
854 /* fix up the bonus db_size */
856 dn
->dn_bonus
->db
.db_size
=
857 DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
858 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
859 ASSERT(dn
->dn_bonuslen
<= dn
->dn_bonus
->db
.db_size
);
862 dn
->dn_allocated_txg
= tx
->tx_txg
;
863 mutex_exit(&dn
->dn_mtx
);
868 dnode_move_impl(dnode_t
*odn
, dnode_t
*ndn
)
870 ASSERT(!RW_LOCK_HELD(&odn
->dn_struct_rwlock
));
871 ASSERT(MUTEX_NOT_HELD(&odn
->dn_mtx
));
872 ASSERT(MUTEX_NOT_HELD(&odn
->dn_dbufs_mtx
));
875 ndn
->dn_objset
= odn
->dn_objset
;
876 ndn
->dn_object
= odn
->dn_object
;
877 ndn
->dn_dbuf
= odn
->dn_dbuf
;
878 ndn
->dn_handle
= odn
->dn_handle
;
879 ndn
->dn_phys
= odn
->dn_phys
;
880 ndn
->dn_type
= odn
->dn_type
;
881 ndn
->dn_bonuslen
= odn
->dn_bonuslen
;
882 ndn
->dn_bonustype
= odn
->dn_bonustype
;
883 ndn
->dn_nblkptr
= odn
->dn_nblkptr
;
884 ndn
->dn_checksum
= odn
->dn_checksum
;
885 ndn
->dn_compress
= odn
->dn_compress
;
886 ndn
->dn_nlevels
= odn
->dn_nlevels
;
887 ndn
->dn_indblkshift
= odn
->dn_indblkshift
;
888 ndn
->dn_datablkshift
= odn
->dn_datablkshift
;
889 ndn
->dn_datablkszsec
= odn
->dn_datablkszsec
;
890 ndn
->dn_datablksz
= odn
->dn_datablksz
;
891 ndn
->dn_maxblkid
= odn
->dn_maxblkid
;
892 ndn
->dn_num_slots
= odn
->dn_num_slots
;
893 memcpy(ndn
->dn_next_type
, odn
->dn_next_type
,
894 sizeof (odn
->dn_next_type
));
895 memcpy(ndn
->dn_next_nblkptr
, odn
->dn_next_nblkptr
,
896 sizeof (odn
->dn_next_nblkptr
));
897 memcpy(ndn
->dn_next_nlevels
, odn
->dn_next_nlevels
,
898 sizeof (odn
->dn_next_nlevels
));
899 memcpy(ndn
->dn_next_indblkshift
, odn
->dn_next_indblkshift
,
900 sizeof (odn
->dn_next_indblkshift
));
901 memcpy(ndn
->dn_next_bonustype
, odn
->dn_next_bonustype
,
902 sizeof (odn
->dn_next_bonustype
));
903 memcpy(ndn
->dn_rm_spillblk
, odn
->dn_rm_spillblk
,
904 sizeof (odn
->dn_rm_spillblk
));
905 memcpy(ndn
->dn_next_bonuslen
, odn
->dn_next_bonuslen
,
906 sizeof (odn
->dn_next_bonuslen
));
907 memcpy(ndn
->dn_next_blksz
, odn
->dn_next_blksz
,
908 sizeof (odn
->dn_next_blksz
));
909 memcpy(ndn
->dn_next_maxblkid
, odn
->dn_next_maxblkid
,
910 sizeof (odn
->dn_next_maxblkid
));
911 for (int i
= 0; i
< TXG_SIZE
; i
++) {
912 list_move_tail(&ndn
->dn_dirty_records
[i
],
913 &odn
->dn_dirty_records
[i
]);
915 memcpy(ndn
->dn_free_ranges
, odn
->dn_free_ranges
,
916 sizeof (odn
->dn_free_ranges
));
917 ndn
->dn_allocated_txg
= odn
->dn_allocated_txg
;
918 ndn
->dn_free_txg
= odn
->dn_free_txg
;
919 ndn
->dn_assigned_txg
= odn
->dn_assigned_txg
;
920 ndn
->dn_dirty_txg
= odn
->dn_dirty_txg
;
921 ndn
->dn_dirtyctx
= odn
->dn_dirtyctx
;
922 ndn
->dn_dirtyctx_firstset
= odn
->dn_dirtyctx_firstset
;
923 ASSERT(zfs_refcount_count(&odn
->dn_tx_holds
) == 0);
924 zfs_refcount_transfer(&ndn
->dn_holds
, &odn
->dn_holds
);
925 ASSERT(avl_is_empty(&ndn
->dn_dbufs
));
926 avl_swap(&ndn
->dn_dbufs
, &odn
->dn_dbufs
);
927 ndn
->dn_dbufs_count
= odn
->dn_dbufs_count
;
928 ndn
->dn_bonus
= odn
->dn_bonus
;
929 ndn
->dn_have_spill
= odn
->dn_have_spill
;
930 ndn
->dn_zio
= odn
->dn_zio
;
931 ndn
->dn_oldused
= odn
->dn_oldused
;
932 ndn
->dn_oldflags
= odn
->dn_oldflags
;
933 ndn
->dn_olduid
= odn
->dn_olduid
;
934 ndn
->dn_oldgid
= odn
->dn_oldgid
;
935 ndn
->dn_oldprojid
= odn
->dn_oldprojid
;
936 ndn
->dn_newuid
= odn
->dn_newuid
;
937 ndn
->dn_newgid
= odn
->dn_newgid
;
938 ndn
->dn_newprojid
= odn
->dn_newprojid
;
939 ndn
->dn_id_flags
= odn
->dn_id_flags
;
940 dmu_zfetch_init(&ndn
->dn_zfetch
, ndn
);
943 * Update back pointers. Updating the handle fixes the back pointer of
944 * every descendant dbuf as well as the bonus dbuf.
946 ASSERT(ndn
->dn_handle
->dnh_dnode
== odn
);
947 ndn
->dn_handle
->dnh_dnode
= ndn
;
950 * Invalidate the original dnode by clearing all of its back pointers.
953 odn
->dn_handle
= NULL
;
954 avl_create(&odn
->dn_dbufs
, dbuf_compare
, sizeof (dmu_buf_impl_t
),
955 offsetof(dmu_buf_impl_t
, db_link
));
956 odn
->dn_dbufs_count
= 0;
957 odn
->dn_bonus
= NULL
;
958 dmu_zfetch_fini(&odn
->dn_zfetch
);
961 * Set the low bit of the objset pointer to ensure that dnode_move()
962 * recognizes the dnode as invalid in any subsequent callback.
964 POINTER_INVALIDATE(&odn
->dn_objset
);
967 * Satisfy the destructor.
969 for (int i
= 0; i
< TXG_SIZE
; i
++) {
970 list_create(&odn
->dn_dirty_records
[i
],
971 sizeof (dbuf_dirty_record_t
),
972 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
973 odn
->dn_free_ranges
[i
] = NULL
;
974 odn
->dn_next_nlevels
[i
] = 0;
975 odn
->dn_next_indblkshift
[i
] = 0;
976 odn
->dn_next_bonustype
[i
] = 0;
977 odn
->dn_rm_spillblk
[i
] = 0;
978 odn
->dn_next_bonuslen
[i
] = 0;
979 odn
->dn_next_blksz
[i
] = 0;
981 odn
->dn_allocated_txg
= 0;
982 odn
->dn_free_txg
= 0;
983 odn
->dn_assigned_txg
= 0;
984 odn
->dn_dirty_txg
= 0;
985 odn
->dn_dirtyctx
= 0;
986 odn
->dn_dirtyctx_firstset
= NULL
;
987 odn
->dn_have_spill
= B_FALSE
;
990 odn
->dn_oldflags
= 0;
993 odn
->dn_oldprojid
= ZFS_DEFAULT_PROJID
;
996 odn
->dn_newprojid
= ZFS_DEFAULT_PROJID
;
997 odn
->dn_id_flags
= 0;
1003 odn
->dn_moved
= (uint8_t)-1;
1007 dnode_move(void *buf
, void *newbuf
, size_t size
, void *arg
)
1009 dnode_t
*odn
= buf
, *ndn
= newbuf
;
1015 * The dnode is on the objset's list of known dnodes if the objset
1016 * pointer is valid. We set the low bit of the objset pointer when
1017 * freeing the dnode to invalidate it, and the memory patterns written
1018 * by kmem (baddcafe and deadbeef) set at least one of the two low bits.
1019 * A newly created dnode sets the objset pointer last of all to indicate
1020 * that the dnode is known and in a valid state to be moved by this
1023 os
= odn
->dn_objset
;
1024 if (!POINTER_IS_VALID(os
)) {
1025 DNODE_STAT_BUMP(dnode_move_invalid
);
1026 return (KMEM_CBRC_DONT_KNOW
);
1030 * Ensure that the objset does not go away during the move.
1032 rw_enter(&os_lock
, RW_WRITER
);
1033 if (os
!= odn
->dn_objset
) {
1035 DNODE_STAT_BUMP(dnode_move_recheck1
);
1036 return (KMEM_CBRC_DONT_KNOW
);
1040 * If the dnode is still valid, then so is the objset. We know that no
1041 * valid objset can be freed while we hold os_lock, so we can safely
1042 * ensure that the objset remains in use.
1044 mutex_enter(&os
->os_lock
);
1047 * Recheck the objset pointer in case the dnode was removed just before
1048 * acquiring the lock.
1050 if (os
!= odn
->dn_objset
) {
1051 mutex_exit(&os
->os_lock
);
1053 DNODE_STAT_BUMP(dnode_move_recheck2
);
1054 return (KMEM_CBRC_DONT_KNOW
);
1058 * At this point we know that as long as we hold os->os_lock, the dnode
1059 * cannot be freed and fields within the dnode can be safely accessed.
1060 * The objset listing this dnode cannot go away as long as this dnode is
1064 if (DMU_OBJECT_IS_SPECIAL(odn
->dn_object
)) {
1065 mutex_exit(&os
->os_lock
);
1066 DNODE_STAT_BUMP(dnode_move_special
);
1067 return (KMEM_CBRC_NO
);
1069 ASSERT(odn
->dn_dbuf
!= NULL
); /* only "special" dnodes have no parent */
1072 * Lock the dnode handle to prevent the dnode from obtaining any new
1073 * holds. This also prevents the descendant dbufs and the bonus dbuf
1074 * from accessing the dnode, so that we can discount their holds. The
1075 * handle is safe to access because we know that while the dnode cannot
1076 * go away, neither can its handle. Once we hold dnh_zrlock, we can
1077 * safely move any dnode referenced only by dbufs.
1079 if (!zrl_tryenter(&odn
->dn_handle
->dnh_zrlock
)) {
1080 mutex_exit(&os
->os_lock
);
1081 DNODE_STAT_BUMP(dnode_move_handle
);
1082 return (KMEM_CBRC_LATER
);
1086 * Ensure a consistent view of the dnode's holds and the dnode's dbufs.
1087 * We need to guarantee that there is a hold for every dbuf in order to
1088 * determine whether the dnode is actively referenced. Falsely matching
1089 * a dbuf to an active hold would lead to an unsafe move. It's possible
1090 * that a thread already having an active dnode hold is about to add a
1091 * dbuf, and we can't compare hold and dbuf counts while the add is in
1094 if (!rw_tryenter(&odn
->dn_struct_rwlock
, RW_WRITER
)) {
1095 zrl_exit(&odn
->dn_handle
->dnh_zrlock
);
1096 mutex_exit(&os
->os_lock
);
1097 DNODE_STAT_BUMP(dnode_move_rwlock
);
1098 return (KMEM_CBRC_LATER
);
1102 * A dbuf may be removed (evicted) without an active dnode hold. In that
1103 * case, the dbuf count is decremented under the handle lock before the
1104 * dbuf's hold is released. This order ensures that if we count the hold
1105 * after the dbuf is removed but before its hold is released, we will
1106 * treat the unmatched hold as active and exit safely. If we count the
1107 * hold before the dbuf is removed, the hold is discounted, and the
1108 * removal is blocked until the move completes.
1110 refcount
= zfs_refcount_count(&odn
->dn_holds
);
1111 ASSERT(refcount
>= 0);
1112 dbufs
= DN_DBUFS_COUNT(odn
);
1114 /* We can't have more dbufs than dnode holds. */
1115 ASSERT3U(dbufs
, <=, refcount
);
1116 DTRACE_PROBE3(dnode__move
, dnode_t
*, odn
, int64_t, refcount
,
1119 if (refcount
> dbufs
) {
1120 rw_exit(&odn
->dn_struct_rwlock
);
1121 zrl_exit(&odn
->dn_handle
->dnh_zrlock
);
1122 mutex_exit(&os
->os_lock
);
1123 DNODE_STAT_BUMP(dnode_move_active
);
1124 return (KMEM_CBRC_LATER
);
1127 rw_exit(&odn
->dn_struct_rwlock
);
1130 * At this point we know that anyone with a hold on the dnode is not
1131 * actively referencing it. The dnode is known and in a valid state to
1132 * move. We're holding the locks needed to execute the critical section.
1134 dnode_move_impl(odn
, ndn
);
1136 list_link_replace(&odn
->dn_link
, &ndn
->dn_link
);
1137 /* If the dnode was safe to move, the refcount cannot have changed. */
1138 ASSERT(refcount
== zfs_refcount_count(&ndn
->dn_holds
));
1139 ASSERT(dbufs
== DN_DBUFS_COUNT(ndn
));
1140 zrl_exit(&ndn
->dn_handle
->dnh_zrlock
); /* handle has moved */
1141 mutex_exit(&os
->os_lock
);
1143 return (KMEM_CBRC_YES
);
1145 #endif /* _KERNEL */
1148 dnode_slots_hold(dnode_children_t
*children
, int idx
, int slots
)
1150 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1152 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1153 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1154 zrl_add(&dnh
->dnh_zrlock
);
1159 dnode_slots_rele(dnode_children_t
*children
, int idx
, int slots
)
1161 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1163 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1164 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1166 if (zrl_is_locked(&dnh
->dnh_zrlock
))
1167 zrl_exit(&dnh
->dnh_zrlock
);
1169 zrl_remove(&dnh
->dnh_zrlock
);
1174 dnode_slots_tryenter(dnode_children_t
*children
, int idx
, int slots
)
1176 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1178 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1179 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1181 if (!zrl_tryenter(&dnh
->dnh_zrlock
)) {
1182 for (int j
= idx
; j
< i
; j
++) {
1183 dnh
= &children
->dnc_children
[j
];
1184 zrl_exit(&dnh
->dnh_zrlock
);
1195 dnode_set_slots(dnode_children_t
*children
, int idx
, int slots
, void *ptr
)
1197 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1199 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1200 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1201 dnh
->dnh_dnode
= ptr
;
1206 dnode_check_slots_free(dnode_children_t
*children
, int idx
, int slots
)
1208 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1211 * If all dnode slots are either already free or
1212 * evictable return B_TRUE.
1214 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1215 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1216 dnode_t
*dn
= dnh
->dnh_dnode
;
1218 if (dn
== DN_SLOT_FREE
) {
1220 } else if (DN_SLOT_IS_PTR(dn
)) {
1221 mutex_enter(&dn
->dn_mtx
);
1222 boolean_t can_free
= (dn
->dn_type
== DMU_OT_NONE
&&
1223 zfs_refcount_is_zero(&dn
->dn_holds
) &&
1224 !DNODE_IS_DIRTY(dn
));
1225 mutex_exit(&dn
->dn_mtx
);
1240 dnode_reclaim_slots(dnode_children_t
*children
, int idx
, int slots
)
1242 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1244 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1245 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1247 ASSERT(zrl_is_locked(&dnh
->dnh_zrlock
));
1249 if (DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1250 ASSERT3S(dnh
->dnh_dnode
->dn_type
, ==, DMU_OT_NONE
);
1251 dnode_destroy(dnh
->dnh_dnode
);
1252 dnh
->dnh_dnode
= DN_SLOT_FREE
;
1258 dnode_free_interior_slots(dnode_t
*dn
)
1260 dnode_children_t
*children
= dmu_buf_get_user(&dn
->dn_dbuf
->db
);
1261 int epb
= dn
->dn_dbuf
->db
.db_size
>> DNODE_SHIFT
;
1262 int idx
= (dn
->dn_object
& (epb
- 1)) + 1;
1263 int slots
= dn
->dn_num_slots
- 1;
1268 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1270 while (!dnode_slots_tryenter(children
, idx
, slots
)) {
1271 DNODE_STAT_BUMP(dnode_free_interior_lock_retry
);
1272 kpreempt(KPREEMPT_SYNC
);
1275 dnode_set_slots(children
, idx
, slots
, DN_SLOT_FREE
);
1276 dnode_slots_rele(children
, idx
, slots
);
1280 dnode_special_close(dnode_handle_t
*dnh
)
1282 dnode_t
*dn
= dnh
->dnh_dnode
;
1285 * Ensure dnode_rele_and_unlock() has released dn_mtx, after final
1286 * zfs_refcount_remove()
1288 mutex_enter(&dn
->dn_mtx
);
1289 if (zfs_refcount_count(&dn
->dn_holds
) > 0)
1290 cv_wait(&dn
->dn_nodnholds
, &dn
->dn_mtx
);
1291 mutex_exit(&dn
->dn_mtx
);
1292 ASSERT3U(zfs_refcount_count(&dn
->dn_holds
), ==, 0);
1294 ASSERT(dn
->dn_dbuf
== NULL
||
1295 dmu_buf_get_user(&dn
->dn_dbuf
->db
) == NULL
);
1296 zrl_add(&dnh
->dnh_zrlock
);
1297 dnode_destroy(dn
); /* implicit zrl_remove() */
1298 zrl_destroy(&dnh
->dnh_zrlock
);
1299 dnh
->dnh_dnode
= NULL
;
1303 dnode_special_open(objset_t
*os
, dnode_phys_t
*dnp
, uint64_t object
,
1304 dnode_handle_t
*dnh
)
1308 zrl_init(&dnh
->dnh_zrlock
);
1309 VERIFY3U(1, ==, zrl_tryenter(&dnh
->dnh_zrlock
));
1311 dn
= dnode_create(os
, dnp
, NULL
, object
, dnh
);
1314 zrl_exit(&dnh
->dnh_zrlock
);
1318 dnode_buf_evict_async(void *dbu
)
1320 dnode_children_t
*dnc
= dbu
;
1322 DNODE_STAT_BUMP(dnode_buf_evict
);
1324 for (int i
= 0; i
< dnc
->dnc_count
; i
++) {
1325 dnode_handle_t
*dnh
= &dnc
->dnc_children
[i
];
1329 * The dnode handle lock guards against the dnode moving to
1330 * another valid address, so there is no need here to guard
1331 * against changes to or from NULL.
1333 if (!DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1334 zrl_destroy(&dnh
->dnh_zrlock
);
1335 dnh
->dnh_dnode
= DN_SLOT_UNINIT
;
1339 zrl_add(&dnh
->dnh_zrlock
);
1340 dn
= dnh
->dnh_dnode
;
1342 * If there are holds on this dnode, then there should
1343 * be holds on the dnode's containing dbuf as well; thus
1344 * it wouldn't be eligible for eviction and this function
1345 * would not have been called.
1347 ASSERT(zfs_refcount_is_zero(&dn
->dn_holds
));
1348 ASSERT(zfs_refcount_is_zero(&dn
->dn_tx_holds
));
1350 dnode_destroy(dn
); /* implicit zrl_remove() for first slot */
1351 zrl_destroy(&dnh
->dnh_zrlock
);
1352 dnh
->dnh_dnode
= DN_SLOT_UNINIT
;
1354 kmem_free(dnc
, sizeof (dnode_children_t
) +
1355 dnc
->dnc_count
* sizeof (dnode_handle_t
));
1359 * When the DNODE_MUST_BE_FREE flag is set, the "slots" parameter is used
1360 * to ensure the hole at the specified object offset is large enough to
1361 * hold the dnode being created. The slots parameter is also used to ensure
1362 * a dnode does not span multiple dnode blocks. In both of these cases, if
1363 * a failure occurs, ENOSPC is returned. Keep in mind, these failure cases
1364 * are only possible when using DNODE_MUST_BE_FREE.
1366 * If the DNODE_MUST_BE_ALLOCATED flag is set, "slots" must be 0.
1367 * dnode_hold_impl() will check if the requested dnode is already consumed
1368 * as an extra dnode slot by an large dnode, in which case it returns
1371 * If the DNODE_DRY_RUN flag is set, we don't actually hold the dnode, just
1372 * return whether the hold would succeed or not. tag and dnp should set to
1373 * NULL in this case.
1376 * EINVAL - Invalid object number or flags.
1377 * ENOSPC - Hole too small to fulfill "slots" request (DNODE_MUST_BE_FREE)
1378 * EEXIST - Refers to an allocated dnode (DNODE_MUST_BE_FREE)
1379 * - Refers to a freeing dnode (DNODE_MUST_BE_FREE)
1380 * - Refers to an interior dnode slot (DNODE_MUST_BE_ALLOCATED)
1381 * ENOENT - The requested dnode is not allocated (DNODE_MUST_BE_ALLOCATED)
1382 * - The requested dnode is being freed (DNODE_MUST_BE_ALLOCATED)
1383 * EIO - I/O error when reading the meta dnode dbuf.
1385 * succeeds even for free dnodes.
1388 dnode_hold_impl(objset_t
*os
, uint64_t object
, int flag
, int slots
,
1389 const void *tag
, dnode_t
**dnp
)
1392 int drop_struct_lock
= FALSE
;
1397 dnode_children_t
*dnc
;
1398 dnode_phys_t
*dn_block
;
1399 dnode_handle_t
*dnh
;
1401 ASSERT(!(flag
& DNODE_MUST_BE_ALLOCATED
) || (slots
== 0));
1402 ASSERT(!(flag
& DNODE_MUST_BE_FREE
) || (slots
> 0));
1403 IMPLY(flag
& DNODE_DRY_RUN
, (tag
== NULL
) && (dnp
== NULL
));
1406 * If you are holding the spa config lock as writer, you shouldn't
1407 * be asking the DMU to do *anything* unless it's the root pool
1408 * which may require us to read from the root filesystem while
1409 * holding some (not all) of the locks as writer.
1411 ASSERT(spa_config_held(os
->os_spa
, SCL_ALL
, RW_WRITER
) == 0 ||
1412 (spa_is_root(os
->os_spa
) &&
1413 spa_config_held(os
->os_spa
, SCL_STATE
, RW_WRITER
)));
1415 ASSERT((flag
& DNODE_MUST_BE_ALLOCATED
) || (flag
& DNODE_MUST_BE_FREE
));
1417 if (object
== DMU_USERUSED_OBJECT
|| object
== DMU_GROUPUSED_OBJECT
||
1418 object
== DMU_PROJECTUSED_OBJECT
) {
1419 if (object
== DMU_USERUSED_OBJECT
)
1420 dn
= DMU_USERUSED_DNODE(os
);
1421 else if (object
== DMU_GROUPUSED_OBJECT
)
1422 dn
= DMU_GROUPUSED_DNODE(os
);
1424 dn
= DMU_PROJECTUSED_DNODE(os
);
1426 return (SET_ERROR(ENOENT
));
1428 if ((flag
& DNODE_MUST_BE_ALLOCATED
) && type
== DMU_OT_NONE
)
1429 return (SET_ERROR(ENOENT
));
1430 if ((flag
& DNODE_MUST_BE_FREE
) && type
!= DMU_OT_NONE
)
1431 return (SET_ERROR(EEXIST
));
1433 /* Don't actually hold if dry run, just return 0 */
1434 if (!(flag
& DNODE_DRY_RUN
)) {
1435 (void) zfs_refcount_add(&dn
->dn_holds
, tag
);
1441 if (object
== 0 || object
>= DN_MAX_OBJECT
)
1442 return (SET_ERROR(EINVAL
));
1444 mdn
= DMU_META_DNODE(os
);
1445 ASSERT(mdn
->dn_object
== DMU_META_DNODE_OBJECT
);
1449 if (!RW_WRITE_HELD(&mdn
->dn_struct_rwlock
)) {
1450 rw_enter(&mdn
->dn_struct_rwlock
, RW_READER
);
1451 drop_struct_lock
= TRUE
;
1454 blk
= dbuf_whichblock(mdn
, 0, object
* sizeof (dnode_phys_t
));
1455 db
= dbuf_hold(mdn
, blk
, FTAG
);
1456 if (drop_struct_lock
)
1457 rw_exit(&mdn
->dn_struct_rwlock
);
1459 DNODE_STAT_BUMP(dnode_hold_dbuf_hold
);
1460 return (SET_ERROR(EIO
));
1464 * We do not need to decrypt to read the dnode so it doesn't matter
1465 * if we get the encrypted or decrypted version.
1467 err
= dbuf_read(db
, NULL
, DB_RF_CANFAIL
|
1468 DB_RF_NO_DECRYPT
| DB_RF_NOPREFETCH
);
1470 DNODE_STAT_BUMP(dnode_hold_dbuf_read
);
1471 dbuf_rele(db
, FTAG
);
1475 ASSERT3U(db
->db
.db_size
, >=, 1<<DNODE_SHIFT
);
1476 epb
= db
->db
.db_size
>> DNODE_SHIFT
;
1478 idx
= object
& (epb
- 1);
1479 dn_block
= (dnode_phys_t
*)db
->db
.db_data
;
1481 ASSERT(DB_DNODE(db
)->dn_type
== DMU_OT_DNODE
);
1482 dnc
= dmu_buf_get_user(&db
->db
);
1485 dnode_children_t
*winner
;
1488 dnc
= kmem_zalloc(sizeof (dnode_children_t
) +
1489 epb
* sizeof (dnode_handle_t
), KM_SLEEP
);
1490 dnc
->dnc_count
= epb
;
1491 dnh
= &dnc
->dnc_children
[0];
1493 /* Initialize dnode slot status from dnode_phys_t */
1494 for (int i
= 0; i
< epb
; i
++) {
1495 zrl_init(&dnh
[i
].dnh_zrlock
);
1502 if (dn_block
[i
].dn_type
!= DMU_OT_NONE
) {
1503 int interior
= dn_block
[i
].dn_extra_slots
;
1505 dnode_set_slots(dnc
, i
, 1, DN_SLOT_ALLOCATED
);
1506 dnode_set_slots(dnc
, i
+ 1, interior
,
1510 dnh
[i
].dnh_dnode
= DN_SLOT_FREE
;
1515 dmu_buf_init_user(&dnc
->dnc_dbu
, NULL
,
1516 dnode_buf_evict_async
, NULL
);
1517 winner
= dmu_buf_set_user(&db
->db
, &dnc
->dnc_dbu
);
1518 if (winner
!= NULL
) {
1520 for (int i
= 0; i
< epb
; i
++)
1521 zrl_destroy(&dnh
[i
].dnh_zrlock
);
1523 kmem_free(dnc
, sizeof (dnode_children_t
) +
1524 epb
* sizeof (dnode_handle_t
));
1529 ASSERT(dnc
->dnc_count
== epb
);
1531 if (flag
& DNODE_MUST_BE_ALLOCATED
) {
1534 dnode_slots_hold(dnc
, idx
, slots
);
1535 dnh
= &dnc
->dnc_children
[idx
];
1537 if (DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1538 dn
= dnh
->dnh_dnode
;
1539 } else if (dnh
->dnh_dnode
== DN_SLOT_INTERIOR
) {
1540 DNODE_STAT_BUMP(dnode_hold_alloc_interior
);
1541 dnode_slots_rele(dnc
, idx
, slots
);
1542 dbuf_rele(db
, FTAG
);
1543 return (SET_ERROR(EEXIST
));
1544 } else if (dnh
->dnh_dnode
!= DN_SLOT_ALLOCATED
) {
1545 DNODE_STAT_BUMP(dnode_hold_alloc_misses
);
1546 dnode_slots_rele(dnc
, idx
, slots
);
1547 dbuf_rele(db
, FTAG
);
1548 return (SET_ERROR(ENOENT
));
1550 dnode_slots_rele(dnc
, idx
, slots
);
1551 while (!dnode_slots_tryenter(dnc
, idx
, slots
)) {
1552 DNODE_STAT_BUMP(dnode_hold_alloc_lock_retry
);
1553 kpreempt(KPREEMPT_SYNC
);
1557 * Someone else won the race and called dnode_create()
1558 * after we checked DN_SLOT_IS_PTR() above but before
1559 * we acquired the lock.
1561 if (DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1562 DNODE_STAT_BUMP(dnode_hold_alloc_lock_misses
);
1563 dn
= dnh
->dnh_dnode
;
1565 dn
= dnode_create(os
, dn_block
+ idx
, db
,
1570 mutex_enter(&dn
->dn_mtx
);
1571 if (dn
->dn_type
== DMU_OT_NONE
|| dn
->dn_free_txg
!= 0) {
1572 DNODE_STAT_BUMP(dnode_hold_alloc_type_none
);
1573 mutex_exit(&dn
->dn_mtx
);
1574 dnode_slots_rele(dnc
, idx
, slots
);
1575 dbuf_rele(db
, FTAG
);
1576 return (SET_ERROR(ENOENT
));
1579 /* Don't actually hold if dry run, just return 0 */
1580 if (flag
& DNODE_DRY_RUN
) {
1581 mutex_exit(&dn
->dn_mtx
);
1582 dnode_slots_rele(dnc
, idx
, slots
);
1583 dbuf_rele(db
, FTAG
);
1587 DNODE_STAT_BUMP(dnode_hold_alloc_hits
);
1588 } else if (flag
& DNODE_MUST_BE_FREE
) {
1590 if (idx
+ slots
- 1 >= DNODES_PER_BLOCK
) {
1591 DNODE_STAT_BUMP(dnode_hold_free_overflow
);
1592 dbuf_rele(db
, FTAG
);
1593 return (SET_ERROR(ENOSPC
));
1596 dnode_slots_hold(dnc
, idx
, slots
);
1598 if (!dnode_check_slots_free(dnc
, idx
, slots
)) {
1599 DNODE_STAT_BUMP(dnode_hold_free_misses
);
1600 dnode_slots_rele(dnc
, idx
, slots
);
1601 dbuf_rele(db
, FTAG
);
1602 return (SET_ERROR(ENOSPC
));
1605 dnode_slots_rele(dnc
, idx
, slots
);
1606 while (!dnode_slots_tryenter(dnc
, idx
, slots
)) {
1607 DNODE_STAT_BUMP(dnode_hold_free_lock_retry
);
1608 kpreempt(KPREEMPT_SYNC
);
1611 if (!dnode_check_slots_free(dnc
, idx
, slots
)) {
1612 DNODE_STAT_BUMP(dnode_hold_free_lock_misses
);
1613 dnode_slots_rele(dnc
, idx
, slots
);
1614 dbuf_rele(db
, FTAG
);
1615 return (SET_ERROR(ENOSPC
));
1619 * Allocated but otherwise free dnodes which would
1620 * be in the interior of a multi-slot dnodes need
1621 * to be freed. Single slot dnodes can be safely
1622 * re-purposed as a performance optimization.
1625 dnode_reclaim_slots(dnc
, idx
+ 1, slots
- 1);
1627 dnh
= &dnc
->dnc_children
[idx
];
1628 if (DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1629 dn
= dnh
->dnh_dnode
;
1631 dn
= dnode_create(os
, dn_block
+ idx
, db
,
1635 mutex_enter(&dn
->dn_mtx
);
1636 if (!zfs_refcount_is_zero(&dn
->dn_holds
) || dn
->dn_free_txg
) {
1637 DNODE_STAT_BUMP(dnode_hold_free_refcount
);
1638 mutex_exit(&dn
->dn_mtx
);
1639 dnode_slots_rele(dnc
, idx
, slots
);
1640 dbuf_rele(db
, FTAG
);
1641 return (SET_ERROR(EEXIST
));
1644 /* Don't actually hold if dry run, just return 0 */
1645 if (flag
& DNODE_DRY_RUN
) {
1646 mutex_exit(&dn
->dn_mtx
);
1647 dnode_slots_rele(dnc
, idx
, slots
);
1648 dbuf_rele(db
, FTAG
);
1652 dnode_set_slots(dnc
, idx
+ 1, slots
- 1, DN_SLOT_INTERIOR
);
1653 DNODE_STAT_BUMP(dnode_hold_free_hits
);
1655 dbuf_rele(db
, FTAG
);
1656 return (SET_ERROR(EINVAL
));
1659 ASSERT0(dn
->dn_free_txg
);
1661 if (zfs_refcount_add(&dn
->dn_holds
, tag
) == 1)
1662 dbuf_add_ref(db
, dnh
);
1664 mutex_exit(&dn
->dn_mtx
);
1666 /* Now we can rely on the hold to prevent the dnode from moving. */
1667 dnode_slots_rele(dnc
, idx
, slots
);
1670 ASSERT3P(dnp
, !=, NULL
);
1671 ASSERT3P(dn
->dn_dbuf
, ==, db
);
1672 ASSERT3U(dn
->dn_object
, ==, object
);
1673 dbuf_rele(db
, FTAG
);
1680 * Return held dnode if the object is allocated, NULL if not.
1683 dnode_hold(objset_t
*os
, uint64_t object
, const void *tag
, dnode_t
**dnp
)
1685 return (dnode_hold_impl(os
, object
, DNODE_MUST_BE_ALLOCATED
, 0, tag
,
1690 * Can only add a reference if there is already at least one
1691 * reference on the dnode. Returns FALSE if unable to add a
1695 dnode_add_ref(dnode_t
*dn
, const void *tag
)
1697 mutex_enter(&dn
->dn_mtx
);
1698 if (zfs_refcount_is_zero(&dn
->dn_holds
)) {
1699 mutex_exit(&dn
->dn_mtx
);
1702 VERIFY(1 < zfs_refcount_add(&dn
->dn_holds
, tag
));
1703 mutex_exit(&dn
->dn_mtx
);
1708 dnode_rele(dnode_t
*dn
, const void *tag
)
1710 mutex_enter(&dn
->dn_mtx
);
1711 dnode_rele_and_unlock(dn
, tag
, B_FALSE
);
1715 dnode_rele_and_unlock(dnode_t
*dn
, const void *tag
, boolean_t evicting
)
1718 /* Get while the hold prevents the dnode from moving. */
1719 dmu_buf_impl_t
*db
= dn
->dn_dbuf
;
1720 dnode_handle_t
*dnh
= dn
->dn_handle
;
1722 refs
= zfs_refcount_remove(&dn
->dn_holds
, tag
);
1724 cv_broadcast(&dn
->dn_nodnholds
);
1725 mutex_exit(&dn
->dn_mtx
);
1726 /* dnode could get destroyed at this point, so don't use it anymore */
1729 * It's unsafe to release the last hold on a dnode by dnode_rele() or
1730 * indirectly by dbuf_rele() while relying on the dnode handle to
1731 * prevent the dnode from moving, since releasing the last hold could
1732 * result in the dnode's parent dbuf evicting its dnode handles. For
1733 * that reason anyone calling dnode_rele() or dbuf_rele() without some
1734 * other direct or indirect hold on the dnode must first drop the dnode
1738 ASSERT(refs
> 0 || dnh
->dnh_zrlock
.zr_owner
!= curthread
);
1741 /* NOTE: the DNODE_DNODE does not have a dn_dbuf */
1742 if (refs
== 0 && db
!= NULL
) {
1744 * Another thread could add a hold to the dnode handle in
1745 * dnode_hold_impl() while holding the parent dbuf. Since the
1746 * hold on the parent dbuf prevents the handle from being
1747 * destroyed, the hold on the handle is OK. We can't yet assert
1748 * that the handle has zero references, but that will be
1749 * asserted anyway when the handle gets destroyed.
1751 mutex_enter(&db
->db_mtx
);
1752 dbuf_rele_and_unlock(db
, dnh
, evicting
);
1757 * Test whether we can create a dnode at the specified location.
1760 dnode_try_claim(objset_t
*os
, uint64_t object
, int slots
)
1762 return (dnode_hold_impl(os
, object
, DNODE_MUST_BE_FREE
| DNODE_DRY_RUN
,
1763 slots
, NULL
, NULL
));
1767 * Checks if the dnode contains any uncommitted dirty records.
1770 dnode_is_dirty(dnode_t
*dn
)
1772 mutex_enter(&dn
->dn_mtx
);
1774 for (int i
= 0; i
< TXG_SIZE
; i
++) {
1775 if (multilist_link_active(&dn
->dn_dirty_link
[i
])) {
1776 mutex_exit(&dn
->dn_mtx
);
1781 mutex_exit(&dn
->dn_mtx
);
1787 dnode_setdirty(dnode_t
*dn
, dmu_tx_t
*tx
)
1789 objset_t
*os
= dn
->dn_objset
;
1790 uint64_t txg
= tx
->tx_txg
;
1792 if (DMU_OBJECT_IS_SPECIAL(dn
->dn_object
)) {
1793 dsl_dataset_dirty(os
->os_dsl_dataset
, tx
);
1800 mutex_enter(&dn
->dn_mtx
);
1801 ASSERT(dn
->dn_phys
->dn_type
|| dn
->dn_allocated_txg
);
1802 ASSERT(dn
->dn_free_txg
== 0 || dn
->dn_free_txg
>= txg
);
1803 mutex_exit(&dn
->dn_mtx
);
1807 * Determine old uid/gid when necessary
1809 dmu_objset_userquota_get_ids(dn
, B_TRUE
, tx
);
1811 multilist_t
*dirtylist
= &os
->os_dirty_dnodes
[txg
& TXG_MASK
];
1812 multilist_sublist_t
*mls
= multilist_sublist_lock_obj(dirtylist
, dn
);
1815 * If we are already marked dirty, we're done.
1817 if (multilist_link_active(&dn
->dn_dirty_link
[txg
& TXG_MASK
])) {
1818 multilist_sublist_unlock(mls
);
1822 ASSERT(!zfs_refcount_is_zero(&dn
->dn_holds
) ||
1823 !avl_is_empty(&dn
->dn_dbufs
));
1824 ASSERT(dn
->dn_datablksz
!= 0);
1825 ASSERT0(dn
->dn_next_bonuslen
[txg
& TXG_MASK
]);
1826 ASSERT0(dn
->dn_next_blksz
[txg
& TXG_MASK
]);
1827 ASSERT0(dn
->dn_next_bonustype
[txg
& TXG_MASK
]);
1829 dprintf_ds(os
->os_dsl_dataset
, "obj=%llu txg=%llu\n",
1830 (u_longlong_t
)dn
->dn_object
, (u_longlong_t
)txg
);
1832 multilist_sublist_insert_head(mls
, dn
);
1834 multilist_sublist_unlock(mls
);
1837 * The dnode maintains a hold on its containing dbuf as
1838 * long as there are holds on it. Each instantiated child
1839 * dbuf maintains a hold on the dnode. When the last child
1840 * drops its hold, the dnode will drop its hold on the
1841 * containing dbuf. We add a "dirty hold" here so that the
1842 * dnode will hang around after we finish processing its
1845 VERIFY(dnode_add_ref(dn
, (void *)(uintptr_t)tx
->tx_txg
));
1847 (void) dbuf_dirty(dn
->dn_dbuf
, tx
);
1849 dsl_dataset_dirty(os
->os_dsl_dataset
, tx
);
1853 dnode_free(dnode_t
*dn
, dmu_tx_t
*tx
)
1855 mutex_enter(&dn
->dn_mtx
);
1856 if (dn
->dn_type
== DMU_OT_NONE
|| dn
->dn_free_txg
) {
1857 mutex_exit(&dn
->dn_mtx
);
1860 dn
->dn_free_txg
= tx
->tx_txg
;
1861 mutex_exit(&dn
->dn_mtx
);
1863 dnode_setdirty(dn
, tx
);
1867 * Try to change the block size for the indicated dnode. This can only
1868 * succeed if there are no blocks allocated or dirty beyond first block
1871 dnode_set_blksz(dnode_t
*dn
, uint64_t size
, int ibs
, dmu_tx_t
*tx
)
1876 ASSERT3U(size
, <=, spa_maxblocksize(dmu_objset_spa(dn
->dn_objset
)));
1878 size
= SPA_MINBLOCKSIZE
;
1880 size
= P2ROUNDUP(size
, SPA_MINBLOCKSIZE
);
1882 if (ibs
== dn
->dn_indblkshift
)
1885 if (size
== dn
->dn_datablksz
&& ibs
== 0)
1888 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1890 /* Check for any allocated blocks beyond the first */
1891 if (dn
->dn_maxblkid
!= 0)
1894 mutex_enter(&dn
->dn_dbufs_mtx
);
1895 for (db
= avl_first(&dn
->dn_dbufs
); db
!= NULL
;
1896 db
= AVL_NEXT(&dn
->dn_dbufs
, db
)) {
1897 if (db
->db_blkid
!= 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1898 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1899 mutex_exit(&dn
->dn_dbufs_mtx
);
1903 mutex_exit(&dn
->dn_dbufs_mtx
);
1905 if (ibs
&& dn
->dn_nlevels
!= 1)
1908 dnode_setdirty(dn
, tx
);
1909 if (size
!= dn
->dn_datablksz
) {
1910 /* resize the old block */
1911 err
= dbuf_hold_impl(dn
, 0, 0, TRUE
, FALSE
, FTAG
, &db
);
1913 dbuf_new_size(db
, size
, tx
);
1914 } else if (err
!= ENOENT
) {
1918 dnode_setdblksz(dn
, size
);
1919 dn
->dn_next_blksz
[tx
->tx_txg
& TXG_MASK
] = size
;
1921 dbuf_rele(db
, FTAG
);
1924 dn
->dn_indblkshift
= ibs
;
1925 dn
->dn_next_indblkshift
[tx
->tx_txg
& TXG_MASK
] = ibs
;
1928 rw_exit(&dn
->dn_struct_rwlock
);
1932 rw_exit(&dn
->dn_struct_rwlock
);
1933 return (SET_ERROR(ENOTSUP
));
1937 dnode_set_nlevels_impl(dnode_t
*dn
, int new_nlevels
, dmu_tx_t
*tx
)
1939 uint64_t txgoff
= tx
->tx_txg
& TXG_MASK
;
1940 int old_nlevels
= dn
->dn_nlevels
;
1943 dbuf_dirty_record_t
*new, *dr
, *dr_next
;
1945 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1947 ASSERT3U(new_nlevels
, >, dn
->dn_nlevels
);
1948 dn
->dn_nlevels
= new_nlevels
;
1950 ASSERT3U(new_nlevels
, >, dn
->dn_next_nlevels
[txgoff
]);
1951 dn
->dn_next_nlevels
[txgoff
] = new_nlevels
;
1953 /* dirty the left indirects */
1954 db
= dbuf_hold_level(dn
, old_nlevels
, 0, FTAG
);
1956 new = dbuf_dirty(db
, tx
);
1957 dbuf_rele(db
, FTAG
);
1959 /* transfer the dirty records to the new indirect */
1960 mutex_enter(&dn
->dn_mtx
);
1961 mutex_enter(&new->dt
.di
.dr_mtx
);
1962 list
= &dn
->dn_dirty_records
[txgoff
];
1963 for (dr
= list_head(list
); dr
; dr
= dr_next
) {
1964 dr_next
= list_next(&dn
->dn_dirty_records
[txgoff
], dr
);
1966 IMPLY(dr
->dr_dbuf
== NULL
, old_nlevels
== 1);
1967 if (dr
->dr_dbuf
== NULL
||
1968 (dr
->dr_dbuf
->db_level
== old_nlevels
- 1 &&
1969 dr
->dr_dbuf
->db_blkid
!= DMU_BONUS_BLKID
&&
1970 dr
->dr_dbuf
->db_blkid
!= DMU_SPILL_BLKID
)) {
1971 list_remove(&dn
->dn_dirty_records
[txgoff
], dr
);
1972 list_insert_tail(&new->dt
.di
.dr_children
, dr
);
1973 dr
->dr_parent
= new;
1976 mutex_exit(&new->dt
.di
.dr_mtx
);
1977 mutex_exit(&dn
->dn_mtx
);
1981 dnode_set_nlevels(dnode_t
*dn
, int nlevels
, dmu_tx_t
*tx
)
1985 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1987 if (dn
->dn_nlevels
== nlevels
) {
1990 } else if (nlevels
< dn
->dn_nlevels
) {
1991 ret
= SET_ERROR(EINVAL
);
1995 dnode_set_nlevels_impl(dn
, nlevels
, tx
);
1998 rw_exit(&dn
->dn_struct_rwlock
);
2002 /* read-holding callers must not rely on the lock being continuously held */
2004 dnode_new_blkid(dnode_t
*dn
, uint64_t blkid
, dmu_tx_t
*tx
, boolean_t have_read
,
2007 int epbs
, new_nlevels
;
2010 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2013 RW_READ_HELD(&dn
->dn_struct_rwlock
) :
2014 RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
2017 * if we have a read-lock, check to see if we need to do any work
2018 * before upgrading to a write-lock.
2021 if (blkid
<= dn
->dn_maxblkid
)
2024 if (!rw_tryupgrade(&dn
->dn_struct_rwlock
)) {
2025 rw_exit(&dn
->dn_struct_rwlock
);
2026 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
2031 * Raw sends (indicated by the force flag) require that we take the
2032 * given blkid even if the value is lower than the current value.
2034 if (!force
&& blkid
<= dn
->dn_maxblkid
)
2038 * We use the (otherwise unused) top bit of dn_next_maxblkid[txgoff]
2039 * to indicate that this field is set. This allows us to set the
2040 * maxblkid to 0 on an existing object in dnode_sync().
2042 dn
->dn_maxblkid
= blkid
;
2043 dn
->dn_next_maxblkid
[tx
->tx_txg
& TXG_MASK
] =
2044 blkid
| DMU_NEXT_MAXBLKID_SET
;
2047 * Compute the number of levels necessary to support the new maxblkid.
2048 * Raw sends will ensure nlevels is set correctly for us.
2051 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2052 for (sz
= dn
->dn_nblkptr
;
2053 sz
<= blkid
&& sz
>= dn
->dn_nblkptr
; sz
<<= epbs
)
2056 ASSERT3U(new_nlevels
, <=, DN_MAX_LEVELS
);
2059 if (new_nlevels
> dn
->dn_nlevels
)
2060 dnode_set_nlevels_impl(dn
, new_nlevels
, tx
);
2062 ASSERT3U(dn
->dn_nlevels
, >=, new_nlevels
);
2067 rw_downgrade(&dn
->dn_struct_rwlock
);
2071 dnode_dirty_l1(dnode_t
*dn
, uint64_t l1blkid
, dmu_tx_t
*tx
)
2073 dmu_buf_impl_t
*db
= dbuf_hold_level(dn
, 1, l1blkid
, FTAG
);
2075 dmu_buf_will_dirty(&db
->db
, tx
);
2076 dbuf_rele(db
, FTAG
);
2081 * Dirty all the in-core level-1 dbufs in the range specified by start_blkid
2085 dnode_dirty_l1range(dnode_t
*dn
, uint64_t start_blkid
, uint64_t end_blkid
,
2088 dmu_buf_impl_t
*db_search
;
2092 db_search
= kmem_zalloc(sizeof (dmu_buf_impl_t
), KM_SLEEP
);
2094 mutex_enter(&dn
->dn_dbufs_mtx
);
2096 db_search
->db_level
= 1;
2097 db_search
->db_blkid
= start_blkid
+ 1;
2098 db_search
->db_state
= DB_SEARCH
;
2101 db
= avl_find(&dn
->dn_dbufs
, db_search
, &where
);
2103 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
2105 if (db
== NULL
|| db
->db_level
!= 1 ||
2106 db
->db_blkid
>= end_blkid
) {
2111 * Setup the next blkid we want to search for.
2113 db_search
->db_blkid
= db
->db_blkid
+ 1;
2114 ASSERT3U(db
->db_blkid
, >=, start_blkid
);
2117 * If the dbuf transitions to DB_EVICTING while we're trying
2118 * to dirty it, then we will be unable to discover it in
2119 * the dbuf hash table. This will result in a call to
2120 * dbuf_create() which needs to acquire the dn_dbufs_mtx
2121 * lock. To avoid a deadlock, we drop the lock before
2122 * dirtying the level-1 dbuf.
2124 mutex_exit(&dn
->dn_dbufs_mtx
);
2125 dnode_dirty_l1(dn
, db
->db_blkid
, tx
);
2126 mutex_enter(&dn
->dn_dbufs_mtx
);
2131 * Walk all the in-core level-1 dbufs and verify they have been dirtied.
2133 db_search
->db_level
= 1;
2134 db_search
->db_blkid
= start_blkid
+ 1;
2135 db_search
->db_state
= DB_SEARCH
;
2136 db
= avl_find(&dn
->dn_dbufs
, db_search
, &where
);
2138 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
2139 for (; db
!= NULL
; db
= AVL_NEXT(&dn
->dn_dbufs
, db
)) {
2140 if (db
->db_level
!= 1 || db
->db_blkid
>= end_blkid
)
2142 if (db
->db_state
!= DB_EVICTING
)
2143 ASSERT(db
->db_dirtycnt
> 0);
2146 kmem_free(db_search
, sizeof (dmu_buf_impl_t
));
2147 mutex_exit(&dn
->dn_dbufs_mtx
);
2151 dnode_set_dirtyctx(dnode_t
*dn
, dmu_tx_t
*tx
, const void *tag
)
2154 * Don't set dirtyctx to SYNC if we're just modifying this as we
2155 * initialize the objset.
2157 if (dn
->dn_dirtyctx
== DN_UNDIRTIED
) {
2158 dsl_dataset_t
*ds
= dn
->dn_objset
->os_dsl_dataset
;
2161 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, tag
);
2163 if (!BP_IS_HOLE(dn
->dn_objset
->os_rootbp
)) {
2164 if (dmu_tx_is_syncing(tx
))
2165 dn
->dn_dirtyctx
= DN_DIRTY_SYNC
;
2167 dn
->dn_dirtyctx
= DN_DIRTY_OPEN
;
2168 dn
->dn_dirtyctx_firstset
= tag
;
2171 rrw_exit(&ds
->ds_bp_rwlock
, tag
);
2177 dnode_partial_zero(dnode_t
*dn
, uint64_t off
, uint64_t blkoff
, uint64_t len
,
2183 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2184 res
= dbuf_hold_impl(dn
, 0, dbuf_whichblock(dn
, 0, off
), TRUE
, FALSE
,
2186 rw_exit(&dn
->dn_struct_rwlock
);
2188 db_lock_type_t dblt
;
2191 dblt
= dmu_buf_lock_parent(db
, RW_READER
, FTAG
);
2192 /* don't dirty if not on disk and not dirty */
2193 dirty
= !list_is_empty(&db
->db_dirty_records
) ||
2194 (db
->db_blkptr
&& !BP_IS_HOLE(db
->db_blkptr
));
2195 dmu_buf_unlock_parent(db
, dblt
, FTAG
);
2199 dmu_buf_will_dirty(&db
->db
, tx
);
2200 data
= db
->db
.db_data
;
2201 memset(data
+ blkoff
, 0, len
);
2203 dbuf_rele(db
, FTAG
);
2208 dnode_free_range(dnode_t
*dn
, uint64_t off
, uint64_t len
, dmu_tx_t
*tx
)
2210 uint64_t blkoff
, blkid
, nblks
;
2211 int blksz
, blkshift
, head
, tail
;
2215 blksz
= dn
->dn_datablksz
;
2216 blkshift
= dn
->dn_datablkshift
;
2217 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2219 if (len
== DMU_OBJECT_END
) {
2220 len
= UINT64_MAX
- off
;
2225 * First, block align the region to free:
2228 head
= P2NPHASE(off
, blksz
);
2229 blkoff
= P2PHASE(off
, blksz
);
2230 if ((off
>> blkshift
) > dn
->dn_maxblkid
)
2233 ASSERT(dn
->dn_maxblkid
== 0);
2234 if (off
== 0 && len
>= blksz
) {
2236 * Freeing the whole block; fast-track this request.
2240 if (dn
->dn_nlevels
> 1) {
2241 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
2242 dnode_dirty_l1(dn
, 0, tx
);
2243 rw_exit(&dn
->dn_struct_rwlock
);
2246 } else if (off
>= blksz
) {
2247 /* Freeing past end-of-data */
2250 /* Freeing part of the block. */
2252 ASSERT3U(head
, >, 0);
2256 /* zero out any partial block data at the start of the range */
2258 ASSERT3U(blkoff
+ head
, ==, blksz
);
2261 dnode_partial_zero(dn
, off
, blkoff
, head
, tx
);
2266 /* If the range was less than one block, we're done */
2270 /* If the remaining range is past end of file, we're done */
2271 if ((off
>> blkshift
) > dn
->dn_maxblkid
)
2274 ASSERT(ISP2(blksz
));
2278 tail
= P2PHASE(len
, blksz
);
2280 ASSERT0(P2PHASE(off
, blksz
));
2281 /* zero out any partial block data at the end of the range */
2285 dnode_partial_zero(dn
, off
+ len
, 0, tail
, tx
);
2289 /* If the range did not include a full block, we are done */
2293 ASSERT(IS_P2ALIGNED(off
, blksz
));
2294 ASSERT(trunc
|| IS_P2ALIGNED(len
, blksz
));
2295 blkid
= off
>> blkshift
;
2296 nblks
= len
>> blkshift
;
2301 * Dirty all the indirect blocks in this range. Note that only
2302 * the first and last indirect blocks can actually be written
2303 * (if they were partially freed) -- they must be dirtied, even if
2304 * they do not exist on disk yet. The interior blocks will
2305 * be freed by free_children(), so they will not actually be written.
2306 * Even though these interior blocks will not be written, we
2307 * dirty them for two reasons:
2309 * - It ensures that the indirect blocks remain in memory until
2310 * syncing context. (They have already been prefetched by
2311 * dmu_tx_hold_free(), so we don't have to worry about reading
2312 * them serially here.)
2314 * - The dirty space accounting will put pressure on the txg sync
2315 * mechanism to begin syncing, and to delay transactions if there
2316 * is a large amount of freeing. Even though these indirect
2317 * blocks will not be written, we could need to write the same
2318 * amount of space if we copy the freed BPs into deadlists.
2320 if (dn
->dn_nlevels
> 1) {
2321 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
2322 uint64_t first
, last
;
2324 first
= blkid
>> epbs
;
2325 dnode_dirty_l1(dn
, first
, tx
);
2327 last
= dn
->dn_maxblkid
>> epbs
;
2329 last
= (blkid
+ nblks
- 1) >> epbs
;
2331 dnode_dirty_l1(dn
, last
, tx
);
2333 dnode_dirty_l1range(dn
, first
, last
, tx
);
2335 int shift
= dn
->dn_datablkshift
+ dn
->dn_indblkshift
-
2337 for (uint64_t i
= first
+ 1; i
< last
; i
++) {
2339 * Set i to the blockid of the next non-hole
2340 * level-1 indirect block at or after i. Note
2341 * that dnode_next_offset() operates in terms of
2342 * level-0-equivalent bytes.
2344 uint64_t ibyte
= i
<< shift
;
2345 int err
= dnode_next_offset(dn
, DNODE_FIND_HAVELOCK
,
2352 * Normally we should not see an error, either
2353 * from dnode_next_offset() or dbuf_hold_level()
2354 * (except for ESRCH from dnode_next_offset).
2355 * If there is an i/o error, then when we read
2356 * this block in syncing context, it will use
2357 * ZIO_FLAG_MUSTSUCCEED, and thus hang/panic according
2358 * to the "failmode" property. dnode_next_offset()
2359 * doesn't have a flag to indicate MUSTSUCCEED.
2364 dnode_dirty_l1(dn
, i
, tx
);
2366 rw_exit(&dn
->dn_struct_rwlock
);
2371 * Add this range to the dnode range list.
2372 * We will finish up this free operation in the syncing phase.
2374 mutex_enter(&dn
->dn_mtx
);
2376 int txgoff
= tx
->tx_txg
& TXG_MASK
;
2377 if (dn
->dn_free_ranges
[txgoff
] == NULL
) {
2378 dn
->dn_free_ranges
[txgoff
] = range_tree_create(NULL
,
2379 RANGE_SEG64
, NULL
, 0, 0);
2381 range_tree_clear(dn
->dn_free_ranges
[txgoff
], blkid
, nblks
);
2382 range_tree_add(dn
->dn_free_ranges
[txgoff
], blkid
, nblks
);
2384 dprintf_dnode(dn
, "blkid=%llu nblks=%llu txg=%llu\n",
2385 (u_longlong_t
)blkid
, (u_longlong_t
)nblks
,
2386 (u_longlong_t
)tx
->tx_txg
);
2387 mutex_exit(&dn
->dn_mtx
);
2389 dbuf_free_range(dn
, blkid
, blkid
+ nblks
- 1, tx
);
2390 dnode_setdirty(dn
, tx
);
2394 dnode_spill_freed(dnode_t
*dn
)
2398 mutex_enter(&dn
->dn_mtx
);
2399 for (i
= 0; i
< TXG_SIZE
; i
++) {
2400 if (dn
->dn_rm_spillblk
[i
] == DN_KILL_SPILLBLK
)
2403 mutex_exit(&dn
->dn_mtx
);
2404 return (i
< TXG_SIZE
);
2407 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
2409 dnode_block_freed(dnode_t
*dn
, uint64_t blkid
)
2413 if (blkid
== DMU_BONUS_BLKID
)
2416 if (dn
->dn_free_txg
)
2419 if (blkid
== DMU_SPILL_BLKID
)
2420 return (dnode_spill_freed(dn
));
2422 mutex_enter(&dn
->dn_mtx
);
2423 for (i
= 0; i
< TXG_SIZE
; i
++) {
2424 if (dn
->dn_free_ranges
[i
] != NULL
&&
2425 range_tree_contains(dn
->dn_free_ranges
[i
], blkid
, 1))
2428 mutex_exit(&dn
->dn_mtx
);
2429 return (i
< TXG_SIZE
);
2432 /* call from syncing context when we actually write/free space for this dnode */
2434 dnode_diduse_space(dnode_t
*dn
, int64_t delta
)
2437 dprintf_dnode(dn
, "dn=%p dnp=%p used=%llu delta=%lld\n",
2439 (u_longlong_t
)dn
->dn_phys
->dn_used
,
2442 mutex_enter(&dn
->dn_mtx
);
2443 space
= DN_USED_BYTES(dn
->dn_phys
);
2445 ASSERT3U(space
+ delta
, >=, space
); /* no overflow */
2447 ASSERT3U(space
, >=, -delta
); /* no underflow */
2450 if (spa_version(dn
->dn_objset
->os_spa
) < SPA_VERSION_DNODE_BYTES
) {
2451 ASSERT((dn
->dn_phys
->dn_flags
& DNODE_FLAG_USED_BYTES
) == 0);
2452 ASSERT0(P2PHASE(space
, 1<<DEV_BSHIFT
));
2453 dn
->dn_phys
->dn_used
= space
>> DEV_BSHIFT
;
2455 dn
->dn_phys
->dn_used
= space
;
2456 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_USED_BYTES
;
2458 mutex_exit(&dn
->dn_mtx
);
2462 * Scans a block at the indicated "level" looking for a hole or data,
2463 * depending on 'flags'.
2465 * If level > 0, then we are scanning an indirect block looking at its
2466 * pointers. If level == 0, then we are looking at a block of dnodes.
2468 * If we don't find what we are looking for in the block, we return ESRCH.
2469 * Otherwise, return with *offset pointing to the beginning (if searching
2470 * forwards) or end (if searching backwards) of the range covered by the
2471 * block pointer we matched on (or dnode).
2473 * The basic search algorithm used below by dnode_next_offset() is to
2474 * use this function to search up the block tree (widen the search) until
2475 * we find something (i.e., we don't return ESRCH) and then search back
2476 * down the tree (narrow the search) until we reach our original search
2480 dnode_next_offset_level(dnode_t
*dn
, int flags
, uint64_t *offset
,
2481 int lvl
, uint64_t blkfill
, uint64_t txg
)
2483 dmu_buf_impl_t
*db
= NULL
;
2485 uint64_t epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2486 uint64_t epb
= 1ULL << epbs
;
2487 uint64_t minfill
, maxfill
;
2489 int i
, inc
, error
, span
;
2491 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2493 hole
= ((flags
& DNODE_FIND_HOLE
) != 0);
2494 inc
= (flags
& DNODE_FIND_BACKWARDS
) ? -1 : 1;
2495 ASSERT(txg
== 0 || !hole
);
2497 if (lvl
== dn
->dn_phys
->dn_nlevels
) {
2499 epb
= dn
->dn_phys
->dn_nblkptr
;
2500 data
= dn
->dn_phys
->dn_blkptr
;
2502 uint64_t blkid
= dbuf_whichblock(dn
, lvl
, *offset
);
2503 error
= dbuf_hold_impl(dn
, lvl
, blkid
, TRUE
, FALSE
, FTAG
, &db
);
2505 if (error
!= ENOENT
)
2510 * This can only happen when we are searching up
2511 * the block tree for data. We don't really need to
2512 * adjust the offset, as we will just end up looking
2513 * at the pointer to this block in its parent, and its
2514 * going to be unallocated, so we will skip over it.
2516 return (SET_ERROR(ESRCH
));
2518 error
= dbuf_read(db
, NULL
,
2519 DB_RF_CANFAIL
| DB_RF_HAVESTRUCT
|
2520 DB_RF_NO_DECRYPT
| DB_RF_NOPREFETCH
);
2522 dbuf_rele(db
, FTAG
);
2525 data
= db
->db
.db_data
;
2526 rw_enter(&db
->db_rwlock
, RW_READER
);
2529 if (db
!= NULL
&& txg
!= 0 && (db
->db_blkptr
== NULL
||
2530 db
->db_blkptr
->blk_birth
<= txg
||
2531 BP_IS_HOLE(db
->db_blkptr
))) {
2533 * This can only happen when we are searching up the tree
2534 * and these conditions mean that we need to keep climbing.
2536 error
= SET_ERROR(ESRCH
);
2537 } else if (lvl
== 0) {
2538 dnode_phys_t
*dnp
= data
;
2540 ASSERT(dn
->dn_type
== DMU_OT_DNODE
);
2541 ASSERT(!(flags
& DNODE_FIND_BACKWARDS
));
2543 for (i
= (*offset
>> DNODE_SHIFT
) & (blkfill
- 1);
2544 i
< blkfill
; i
+= dnp
[i
].dn_extra_slots
+ 1) {
2545 if ((dnp
[i
].dn_type
== DMU_OT_NONE
) == hole
)
2550 error
= SET_ERROR(ESRCH
);
2552 *offset
= (*offset
& ~(DNODE_BLOCK_SIZE
- 1)) +
2555 blkptr_t
*bp
= data
;
2556 uint64_t start
= *offset
;
2557 span
= (lvl
- 1) * epbs
+ dn
->dn_datablkshift
;
2559 maxfill
= blkfill
<< ((lvl
- 1) * epbs
);
2566 if (span
>= 8 * sizeof (*offset
)) {
2567 /* This only happens on the highest indirection level */
2568 ASSERT3U((lvl
- 1), ==, dn
->dn_phys
->dn_nlevels
- 1);
2571 *offset
= *offset
>> span
;
2574 for (i
= BF64_GET(*offset
, 0, epbs
);
2575 i
>= 0 && i
< epb
; i
+= inc
) {
2576 if (BP_GET_FILL(&bp
[i
]) >= minfill
&&
2577 BP_GET_FILL(&bp
[i
]) <= maxfill
&&
2578 (hole
|| bp
[i
].blk_birth
> txg
))
2580 if (inc
> 0 || *offset
> 0)
2584 if (span
>= 8 * sizeof (*offset
)) {
2587 *offset
= *offset
<< span
;
2591 /* traversing backwards; position offset at the end */
2592 if (span
< 8 * sizeof (*offset
))
2593 *offset
= MIN(*offset
+ (1ULL << span
) - 1,
2595 } else if (*offset
< start
) {
2598 if (i
< 0 || i
>= epb
)
2599 error
= SET_ERROR(ESRCH
);
2603 rw_exit(&db
->db_rwlock
);
2604 dbuf_rele(db
, FTAG
);
2611 * Find the next hole, data, or sparse region at or after *offset.
2612 * The value 'blkfill' tells us how many items we expect to find
2613 * in an L0 data block; this value is 1 for normal objects,
2614 * DNODES_PER_BLOCK for the meta dnode, and some fraction of
2615 * DNODES_PER_BLOCK when searching for sparse regions thereof.
2619 * dnode_next_offset(dn, flags, offset, 1, 1, 0);
2620 * Finds the next/previous hole/data in a file.
2621 * Used in dmu_offset_next().
2623 * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
2624 * Finds the next free/allocated dnode an objset's meta-dnode.
2625 * Only finds objects that have new contents since txg (ie.
2626 * bonus buffer changes and content removal are ignored).
2627 * Used in dmu_object_next().
2629 * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
2630 * Finds the next L2 meta-dnode bp that's at most 1/4 full.
2631 * Used in dmu_object_alloc().
2634 dnode_next_offset(dnode_t
*dn
, int flags
, uint64_t *offset
,
2635 int minlvl
, uint64_t blkfill
, uint64_t txg
)
2637 uint64_t initial_offset
= *offset
;
2641 if (!(flags
& DNODE_FIND_HAVELOCK
))
2642 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2644 if (dn
->dn_phys
->dn_nlevels
== 0) {
2645 error
= SET_ERROR(ESRCH
);
2649 if (dn
->dn_datablkshift
== 0) {
2650 if (*offset
< dn
->dn_datablksz
) {
2651 if (flags
& DNODE_FIND_HOLE
)
2652 *offset
= dn
->dn_datablksz
;
2654 error
= SET_ERROR(ESRCH
);
2659 maxlvl
= dn
->dn_phys
->dn_nlevels
;
2661 for (lvl
= minlvl
; lvl
<= maxlvl
; lvl
++) {
2662 error
= dnode_next_offset_level(dn
,
2663 flags
, offset
, lvl
, blkfill
, txg
);
2668 while (error
== 0 && --lvl
>= minlvl
) {
2669 error
= dnode_next_offset_level(dn
,
2670 flags
, offset
, lvl
, blkfill
, txg
);
2674 * There's always a "virtual hole" at the end of the object, even
2675 * if all BP's which physically exist are non-holes.
2677 if ((flags
& DNODE_FIND_HOLE
) && error
== ESRCH
&& txg
== 0 &&
2678 minlvl
== 1 && blkfill
== 1 && !(flags
& DNODE_FIND_BACKWARDS
)) {
2682 if (error
== 0 && (flags
& DNODE_FIND_BACKWARDS
?
2683 initial_offset
< *offset
: initial_offset
> *offset
))
2684 error
= SET_ERROR(ESRCH
);
2686 if (!(flags
& DNODE_FIND_HAVELOCK
))
2687 rw_exit(&dn
->dn_struct_rwlock
);
2692 #if defined(_KERNEL)
2693 EXPORT_SYMBOL(dnode_hold
);
2694 EXPORT_SYMBOL(dnode_rele
);
2695 EXPORT_SYMBOL(dnode_set_nlevels
);
2696 EXPORT_SYMBOL(dnode_set_blksz
);
2697 EXPORT_SYMBOL(dnode_free_range
);
2698 EXPORT_SYMBOL(dnode_evict_dbufs
);
2699 EXPORT_SYMBOL(dnode_evict_bonus
);
2702 ZFS_MODULE_PARAM(zfs
, zfs_
, default_bs
, INT
, ZMOD_RW
,
2703 "Default dnode block shift");
2704 ZFS_MODULE_PARAM(zfs
, zfs_
, default_ibs
, INT
, ZMOD_RW
,
2705 "Default dnode indirect block shift");