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_MARKER
) {
103 ASSERT3S(d2
->db_state
, !=, DB_MARKER
);
104 return (TREE_PCMP(d1
->db_parent
, d2
));
105 } else if (d2
->db_state
== DB_MARKER
) {
106 ASSERT3S(d1
->db_state
, !=, DB_MARKER
);
107 return (TREE_PCMP(d1
, d2
->db_parent
));
110 if (d1
->db_state
== DB_SEARCH
) {
111 ASSERT3S(d2
->db_state
, !=, DB_SEARCH
);
113 } else if (d2
->db_state
== DB_SEARCH
) {
114 ASSERT3S(d1
->db_state
, !=, DB_SEARCH
);
118 return (TREE_PCMP(d1
, d2
));
122 dnode_cons(void *arg
, void *unused
, int kmflag
)
124 (void) unused
, (void) kmflag
;
127 rw_init(&dn
->dn_struct_rwlock
, NULL
, RW_NOLOCKDEP
, NULL
);
128 mutex_init(&dn
->dn_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
129 mutex_init(&dn
->dn_dbufs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
130 cv_init(&dn
->dn_notxholds
, NULL
, CV_DEFAULT
, NULL
);
131 cv_init(&dn
->dn_nodnholds
, NULL
, CV_DEFAULT
, NULL
);
134 * Every dbuf has a reference, and dropping a tracked reference is
135 * O(number of references), so don't track dn_holds.
137 zfs_refcount_create_untracked(&dn
->dn_holds
);
138 zfs_refcount_create(&dn
->dn_tx_holds
);
139 list_link_init(&dn
->dn_link
);
141 memset(dn
->dn_next_type
, 0, sizeof (dn
->dn_next_type
));
142 memset(dn
->dn_next_nblkptr
, 0, sizeof (dn
->dn_next_nblkptr
));
143 memset(dn
->dn_next_nlevels
, 0, sizeof (dn
->dn_next_nlevels
));
144 memset(dn
->dn_next_indblkshift
, 0, sizeof (dn
->dn_next_indblkshift
));
145 memset(dn
->dn_next_bonustype
, 0, sizeof (dn
->dn_next_bonustype
));
146 memset(dn
->dn_rm_spillblk
, 0, sizeof (dn
->dn_rm_spillblk
));
147 memset(dn
->dn_next_bonuslen
, 0, sizeof (dn
->dn_next_bonuslen
));
148 memset(dn
->dn_next_blksz
, 0, sizeof (dn
->dn_next_blksz
));
149 memset(dn
->dn_next_maxblkid
, 0, sizeof (dn
->dn_next_maxblkid
));
151 for (int i
= 0; i
< TXG_SIZE
; i
++) {
152 multilist_link_init(&dn
->dn_dirty_link
[i
]);
153 dn
->dn_free_ranges
[i
] = NULL
;
154 list_create(&dn
->dn_dirty_records
[i
],
155 sizeof (dbuf_dirty_record_t
),
156 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
159 dn
->dn_allocated_txg
= 0;
161 dn
->dn_assigned_txg
= 0;
162 dn
->dn_dirty_txg
= 0;
164 dn
->dn_dirtyctx_firstset
= NULL
;
166 dn
->dn_have_spill
= B_FALSE
;
172 dn
->dn_oldprojid
= ZFS_DEFAULT_PROJID
;
175 dn
->dn_newprojid
= ZFS_DEFAULT_PROJID
;
178 dn
->dn_dbufs_count
= 0;
179 avl_create(&dn
->dn_dbufs
, dbuf_compare
, sizeof (dmu_buf_impl_t
),
180 offsetof(dmu_buf_impl_t
, db_link
));
187 dnode_dest(void *arg
, void *unused
)
192 rw_destroy(&dn
->dn_struct_rwlock
);
193 mutex_destroy(&dn
->dn_mtx
);
194 mutex_destroy(&dn
->dn_dbufs_mtx
);
195 cv_destroy(&dn
->dn_notxholds
);
196 cv_destroy(&dn
->dn_nodnholds
);
197 zfs_refcount_destroy(&dn
->dn_holds
);
198 zfs_refcount_destroy(&dn
->dn_tx_holds
);
199 ASSERT(!list_link_active(&dn
->dn_link
));
201 for (int i
= 0; i
< TXG_SIZE
; i
++) {
202 ASSERT(!multilist_link_active(&dn
->dn_dirty_link
[i
]));
203 ASSERT3P(dn
->dn_free_ranges
[i
], ==, NULL
);
204 list_destroy(&dn
->dn_dirty_records
[i
]);
205 ASSERT0(dn
->dn_next_nblkptr
[i
]);
206 ASSERT0(dn
->dn_next_nlevels
[i
]);
207 ASSERT0(dn
->dn_next_indblkshift
[i
]);
208 ASSERT0(dn
->dn_next_bonustype
[i
]);
209 ASSERT0(dn
->dn_rm_spillblk
[i
]);
210 ASSERT0(dn
->dn_next_bonuslen
[i
]);
211 ASSERT0(dn
->dn_next_blksz
[i
]);
212 ASSERT0(dn
->dn_next_maxblkid
[i
]);
215 ASSERT0(dn
->dn_allocated_txg
);
216 ASSERT0(dn
->dn_free_txg
);
217 ASSERT0(dn
->dn_assigned_txg
);
218 ASSERT0(dn
->dn_dirty_txg
);
219 ASSERT0(dn
->dn_dirtyctx
);
220 ASSERT3P(dn
->dn_dirtyctx_firstset
, ==, NULL
);
221 ASSERT3P(dn
->dn_bonus
, ==, NULL
);
222 ASSERT(!dn
->dn_have_spill
);
223 ASSERT3P(dn
->dn_zio
, ==, NULL
);
224 ASSERT0(dn
->dn_oldused
);
225 ASSERT0(dn
->dn_oldflags
);
226 ASSERT0(dn
->dn_olduid
);
227 ASSERT0(dn
->dn_oldgid
);
228 ASSERT0(dn
->dn_oldprojid
);
229 ASSERT0(dn
->dn_newuid
);
230 ASSERT0(dn
->dn_newgid
);
231 ASSERT0(dn
->dn_newprojid
);
232 ASSERT0(dn
->dn_id_flags
);
234 ASSERT0(dn
->dn_dbufs_count
);
235 avl_destroy(&dn
->dn_dbufs
);
239 dnode_kstats_update(kstat_t
*ksp
, int rw
)
241 dnode_stats_t
*ds
= ksp
->ks_data
;
243 if (rw
== KSTAT_WRITE
)
245 ds
->dnode_hold_dbuf_hold
.value
.ui64
=
246 wmsum_value(&dnode_sums
.dnode_hold_dbuf_hold
);
247 ds
->dnode_hold_dbuf_read
.value
.ui64
=
248 wmsum_value(&dnode_sums
.dnode_hold_dbuf_read
);
249 ds
->dnode_hold_alloc_hits
.value
.ui64
=
250 wmsum_value(&dnode_sums
.dnode_hold_alloc_hits
);
251 ds
->dnode_hold_alloc_misses
.value
.ui64
=
252 wmsum_value(&dnode_sums
.dnode_hold_alloc_misses
);
253 ds
->dnode_hold_alloc_interior
.value
.ui64
=
254 wmsum_value(&dnode_sums
.dnode_hold_alloc_interior
);
255 ds
->dnode_hold_alloc_lock_retry
.value
.ui64
=
256 wmsum_value(&dnode_sums
.dnode_hold_alloc_lock_retry
);
257 ds
->dnode_hold_alloc_lock_misses
.value
.ui64
=
258 wmsum_value(&dnode_sums
.dnode_hold_alloc_lock_misses
);
259 ds
->dnode_hold_alloc_type_none
.value
.ui64
=
260 wmsum_value(&dnode_sums
.dnode_hold_alloc_type_none
);
261 ds
->dnode_hold_free_hits
.value
.ui64
=
262 wmsum_value(&dnode_sums
.dnode_hold_free_hits
);
263 ds
->dnode_hold_free_misses
.value
.ui64
=
264 wmsum_value(&dnode_sums
.dnode_hold_free_misses
);
265 ds
->dnode_hold_free_lock_misses
.value
.ui64
=
266 wmsum_value(&dnode_sums
.dnode_hold_free_lock_misses
);
267 ds
->dnode_hold_free_lock_retry
.value
.ui64
=
268 wmsum_value(&dnode_sums
.dnode_hold_free_lock_retry
);
269 ds
->dnode_hold_free_refcount
.value
.ui64
=
270 wmsum_value(&dnode_sums
.dnode_hold_free_refcount
);
271 ds
->dnode_hold_free_overflow
.value
.ui64
=
272 wmsum_value(&dnode_sums
.dnode_hold_free_overflow
);
273 ds
->dnode_free_interior_lock_retry
.value
.ui64
=
274 wmsum_value(&dnode_sums
.dnode_free_interior_lock_retry
);
275 ds
->dnode_allocate
.value
.ui64
=
276 wmsum_value(&dnode_sums
.dnode_allocate
);
277 ds
->dnode_reallocate
.value
.ui64
=
278 wmsum_value(&dnode_sums
.dnode_reallocate
);
279 ds
->dnode_buf_evict
.value
.ui64
=
280 wmsum_value(&dnode_sums
.dnode_buf_evict
);
281 ds
->dnode_alloc_next_chunk
.value
.ui64
=
282 wmsum_value(&dnode_sums
.dnode_alloc_next_chunk
);
283 ds
->dnode_alloc_race
.value
.ui64
=
284 wmsum_value(&dnode_sums
.dnode_alloc_race
);
285 ds
->dnode_alloc_next_block
.value
.ui64
=
286 wmsum_value(&dnode_sums
.dnode_alloc_next_block
);
287 ds
->dnode_move_invalid
.value
.ui64
=
288 wmsum_value(&dnode_sums
.dnode_move_invalid
);
289 ds
->dnode_move_recheck1
.value
.ui64
=
290 wmsum_value(&dnode_sums
.dnode_move_recheck1
);
291 ds
->dnode_move_recheck2
.value
.ui64
=
292 wmsum_value(&dnode_sums
.dnode_move_recheck2
);
293 ds
->dnode_move_special
.value
.ui64
=
294 wmsum_value(&dnode_sums
.dnode_move_special
);
295 ds
->dnode_move_handle
.value
.ui64
=
296 wmsum_value(&dnode_sums
.dnode_move_handle
);
297 ds
->dnode_move_rwlock
.value
.ui64
=
298 wmsum_value(&dnode_sums
.dnode_move_rwlock
);
299 ds
->dnode_move_active
.value
.ui64
=
300 wmsum_value(&dnode_sums
.dnode_move_active
);
307 ASSERT(dnode_cache
== NULL
);
308 dnode_cache
= kmem_cache_create("dnode_t", sizeof (dnode_t
),
309 0, dnode_cons
, dnode_dest
, NULL
, NULL
, NULL
, 0);
310 kmem_cache_set_move(dnode_cache
, dnode_move
);
312 wmsum_init(&dnode_sums
.dnode_hold_dbuf_hold
, 0);
313 wmsum_init(&dnode_sums
.dnode_hold_dbuf_read
, 0);
314 wmsum_init(&dnode_sums
.dnode_hold_alloc_hits
, 0);
315 wmsum_init(&dnode_sums
.dnode_hold_alloc_misses
, 0);
316 wmsum_init(&dnode_sums
.dnode_hold_alloc_interior
, 0);
317 wmsum_init(&dnode_sums
.dnode_hold_alloc_lock_retry
, 0);
318 wmsum_init(&dnode_sums
.dnode_hold_alloc_lock_misses
, 0);
319 wmsum_init(&dnode_sums
.dnode_hold_alloc_type_none
, 0);
320 wmsum_init(&dnode_sums
.dnode_hold_free_hits
, 0);
321 wmsum_init(&dnode_sums
.dnode_hold_free_misses
, 0);
322 wmsum_init(&dnode_sums
.dnode_hold_free_lock_misses
, 0);
323 wmsum_init(&dnode_sums
.dnode_hold_free_lock_retry
, 0);
324 wmsum_init(&dnode_sums
.dnode_hold_free_refcount
, 0);
325 wmsum_init(&dnode_sums
.dnode_hold_free_overflow
, 0);
326 wmsum_init(&dnode_sums
.dnode_free_interior_lock_retry
, 0);
327 wmsum_init(&dnode_sums
.dnode_allocate
, 0);
328 wmsum_init(&dnode_sums
.dnode_reallocate
, 0);
329 wmsum_init(&dnode_sums
.dnode_buf_evict
, 0);
330 wmsum_init(&dnode_sums
.dnode_alloc_next_chunk
, 0);
331 wmsum_init(&dnode_sums
.dnode_alloc_race
, 0);
332 wmsum_init(&dnode_sums
.dnode_alloc_next_block
, 0);
333 wmsum_init(&dnode_sums
.dnode_move_invalid
, 0);
334 wmsum_init(&dnode_sums
.dnode_move_recheck1
, 0);
335 wmsum_init(&dnode_sums
.dnode_move_recheck2
, 0);
336 wmsum_init(&dnode_sums
.dnode_move_special
, 0);
337 wmsum_init(&dnode_sums
.dnode_move_handle
, 0);
338 wmsum_init(&dnode_sums
.dnode_move_rwlock
, 0);
339 wmsum_init(&dnode_sums
.dnode_move_active
, 0);
341 dnode_ksp
= kstat_create("zfs", 0, "dnodestats", "misc",
342 KSTAT_TYPE_NAMED
, sizeof (dnode_stats
) / sizeof (kstat_named_t
),
344 if (dnode_ksp
!= NULL
) {
345 dnode_ksp
->ks_data
= &dnode_stats
;
346 dnode_ksp
->ks_update
= dnode_kstats_update
;
347 kstat_install(dnode_ksp
);
354 if (dnode_ksp
!= NULL
) {
355 kstat_delete(dnode_ksp
);
359 wmsum_fini(&dnode_sums
.dnode_hold_dbuf_hold
);
360 wmsum_fini(&dnode_sums
.dnode_hold_dbuf_read
);
361 wmsum_fini(&dnode_sums
.dnode_hold_alloc_hits
);
362 wmsum_fini(&dnode_sums
.dnode_hold_alloc_misses
);
363 wmsum_fini(&dnode_sums
.dnode_hold_alloc_interior
);
364 wmsum_fini(&dnode_sums
.dnode_hold_alloc_lock_retry
);
365 wmsum_fini(&dnode_sums
.dnode_hold_alloc_lock_misses
);
366 wmsum_fini(&dnode_sums
.dnode_hold_alloc_type_none
);
367 wmsum_fini(&dnode_sums
.dnode_hold_free_hits
);
368 wmsum_fini(&dnode_sums
.dnode_hold_free_misses
);
369 wmsum_fini(&dnode_sums
.dnode_hold_free_lock_misses
);
370 wmsum_fini(&dnode_sums
.dnode_hold_free_lock_retry
);
371 wmsum_fini(&dnode_sums
.dnode_hold_free_refcount
);
372 wmsum_fini(&dnode_sums
.dnode_hold_free_overflow
);
373 wmsum_fini(&dnode_sums
.dnode_free_interior_lock_retry
);
374 wmsum_fini(&dnode_sums
.dnode_allocate
);
375 wmsum_fini(&dnode_sums
.dnode_reallocate
);
376 wmsum_fini(&dnode_sums
.dnode_buf_evict
);
377 wmsum_fini(&dnode_sums
.dnode_alloc_next_chunk
);
378 wmsum_fini(&dnode_sums
.dnode_alloc_race
);
379 wmsum_fini(&dnode_sums
.dnode_alloc_next_block
);
380 wmsum_fini(&dnode_sums
.dnode_move_invalid
);
381 wmsum_fini(&dnode_sums
.dnode_move_recheck1
);
382 wmsum_fini(&dnode_sums
.dnode_move_recheck2
);
383 wmsum_fini(&dnode_sums
.dnode_move_special
);
384 wmsum_fini(&dnode_sums
.dnode_move_handle
);
385 wmsum_fini(&dnode_sums
.dnode_move_rwlock
);
386 wmsum_fini(&dnode_sums
.dnode_move_active
);
388 kmem_cache_destroy(dnode_cache
);
395 dnode_verify(dnode_t
*dn
)
397 int drop_struct_lock
= FALSE
;
400 ASSERT(dn
->dn_objset
);
401 ASSERT(dn
->dn_handle
->dnh_dnode
== dn
);
403 ASSERT(DMU_OT_IS_VALID(dn
->dn_phys
->dn_type
));
405 if (!(zfs_flags
& ZFS_DEBUG_DNODE_VERIFY
))
408 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
409 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
410 drop_struct_lock
= TRUE
;
412 if (dn
->dn_phys
->dn_type
!= DMU_OT_NONE
|| dn
->dn_allocated_txg
!= 0) {
414 int max_bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
415 ASSERT3U(dn
->dn_indblkshift
, <=, SPA_MAXBLOCKSHIFT
);
416 if (dn
->dn_datablkshift
) {
417 ASSERT3U(dn
->dn_datablkshift
, >=, SPA_MINBLOCKSHIFT
);
418 ASSERT3U(dn
->dn_datablkshift
, <=, SPA_MAXBLOCKSHIFT
);
419 ASSERT3U(1<<dn
->dn_datablkshift
, ==, dn
->dn_datablksz
);
421 ASSERT3U(dn
->dn_nlevels
, <=, 30);
422 ASSERT(DMU_OT_IS_VALID(dn
->dn_type
));
423 ASSERT3U(dn
->dn_nblkptr
, >=, 1);
424 ASSERT3U(dn
->dn_nblkptr
, <=, DN_MAX_NBLKPTR
);
425 ASSERT3U(dn
->dn_bonuslen
, <=, max_bonuslen
);
426 ASSERT3U(dn
->dn_datablksz
, ==,
427 dn
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
);
428 ASSERT3U(ISP2(dn
->dn_datablksz
), ==, dn
->dn_datablkshift
!= 0);
429 ASSERT3U((dn
->dn_nblkptr
- 1) * sizeof (blkptr_t
) +
430 dn
->dn_bonuslen
, <=, max_bonuslen
);
431 for (i
= 0; i
< TXG_SIZE
; i
++) {
432 ASSERT3U(dn
->dn_next_nlevels
[i
], <=, dn
->dn_nlevels
);
435 if (dn
->dn_phys
->dn_type
!= DMU_OT_NONE
)
436 ASSERT3U(dn
->dn_phys
->dn_nlevels
, <=, dn
->dn_nlevels
);
437 ASSERT(DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) || dn
->dn_dbuf
!= NULL
);
438 if (dn
->dn_dbuf
!= NULL
) {
439 ASSERT3P(dn
->dn_phys
, ==,
440 (dnode_phys_t
*)dn
->dn_dbuf
->db
.db_data
+
441 (dn
->dn_object
% (dn
->dn_dbuf
->db
.db_size
>> DNODE_SHIFT
)));
443 if (drop_struct_lock
)
444 rw_exit(&dn
->dn_struct_rwlock
);
449 dnode_byteswap(dnode_phys_t
*dnp
)
451 uint64_t *buf64
= (void*)&dnp
->dn_blkptr
;
454 if (dnp
->dn_type
== DMU_OT_NONE
) {
455 memset(dnp
, 0, sizeof (dnode_phys_t
));
459 dnp
->dn_datablkszsec
= BSWAP_16(dnp
->dn_datablkszsec
);
460 dnp
->dn_bonuslen
= BSWAP_16(dnp
->dn_bonuslen
);
461 dnp
->dn_extra_slots
= BSWAP_8(dnp
->dn_extra_slots
);
462 dnp
->dn_maxblkid
= BSWAP_64(dnp
->dn_maxblkid
);
463 dnp
->dn_used
= BSWAP_64(dnp
->dn_used
);
466 * dn_nblkptr is only one byte, so it's OK to read it in either
467 * byte order. We can't read dn_bouslen.
469 ASSERT(dnp
->dn_indblkshift
<= SPA_MAXBLOCKSHIFT
);
470 ASSERT(dnp
->dn_nblkptr
<= DN_MAX_NBLKPTR
);
471 for (i
= 0; i
< dnp
->dn_nblkptr
* sizeof (blkptr_t
)/8; i
++)
472 buf64
[i
] = BSWAP_64(buf64
[i
]);
475 * OK to check dn_bonuslen for zero, because it won't matter if
476 * we have the wrong byte order. This is necessary because the
477 * dnode dnode is smaller than a regular dnode.
479 if (dnp
->dn_bonuslen
!= 0) {
480 dmu_object_byteswap_t byteswap
;
481 ASSERT(DMU_OT_IS_VALID(dnp
->dn_bonustype
));
482 byteswap
= DMU_OT_BYTESWAP(dnp
->dn_bonustype
);
483 dmu_ot_byteswap
[byteswap
].ob_func(DN_BONUS(dnp
),
484 DN_MAX_BONUS_LEN(dnp
));
487 /* Swap SPILL block if we have one */
488 if (dnp
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
)
489 byteswap_uint64_array(DN_SPILL_BLKPTR(dnp
), sizeof (blkptr_t
));
493 dnode_buf_byteswap(void *vbuf
, size_t size
)
497 ASSERT3U(sizeof (dnode_phys_t
), ==, (1<<DNODE_SHIFT
));
498 ASSERT((size
& (sizeof (dnode_phys_t
)-1)) == 0);
501 dnode_phys_t
*dnp
= (void *)(((char *)vbuf
) + i
);
505 if (dnp
->dn_type
!= DMU_OT_NONE
)
506 i
+= dnp
->dn_extra_slots
* DNODE_MIN_SIZE
;
511 dnode_setbonuslen(dnode_t
*dn
, int newsize
, dmu_tx_t
*tx
)
513 ASSERT3U(zfs_refcount_count(&dn
->dn_holds
), >=, 1);
515 dnode_setdirty(dn
, tx
);
516 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
517 ASSERT3U(newsize
, <=, DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
518 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
));
520 if (newsize
< dn
->dn_bonuslen
) {
521 /* clear any data after the end of the new size */
522 size_t diff
= dn
->dn_bonuslen
- newsize
;
523 char *data_end
= ((char *)dn
->dn_bonus
->db
.db_data
) + newsize
;
524 memset(data_end
, 0, diff
);
527 dn
->dn_bonuslen
= newsize
;
529 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = DN_ZERO_BONUSLEN
;
531 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonuslen
;
532 rw_exit(&dn
->dn_struct_rwlock
);
536 dnode_setbonus_type(dnode_t
*dn
, dmu_object_type_t newtype
, dmu_tx_t
*tx
)
538 ASSERT3U(zfs_refcount_count(&dn
->dn_holds
), >=, 1);
539 dnode_setdirty(dn
, tx
);
540 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
541 dn
->dn_bonustype
= newtype
;
542 dn
->dn_next_bonustype
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonustype
;
543 rw_exit(&dn
->dn_struct_rwlock
);
547 dnode_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
549 ASSERT3U(zfs_refcount_count(&dn
->dn_holds
), >=, 1);
550 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
551 dnode_setdirty(dn
, tx
);
552 dn
->dn_rm_spillblk
[tx
->tx_txg
& TXG_MASK
] = DN_KILL_SPILLBLK
;
553 dn
->dn_have_spill
= B_FALSE
;
557 dnode_setdblksz(dnode_t
*dn
, int size
)
559 ASSERT0(P2PHASE(size
, SPA_MINBLOCKSIZE
));
560 ASSERT3U(size
, <=, SPA_MAXBLOCKSIZE
);
561 ASSERT3U(size
, >=, SPA_MINBLOCKSIZE
);
562 ASSERT3U(size
>> SPA_MINBLOCKSHIFT
, <,
563 1<<(sizeof (dn
->dn_phys
->dn_datablkszsec
) * 8));
564 dn
->dn_datablksz
= size
;
565 dn
->dn_datablkszsec
= size
>> SPA_MINBLOCKSHIFT
;
566 dn
->dn_datablkshift
= ISP2(size
) ? highbit64(size
- 1) : 0;
570 dnode_create(objset_t
*os
, dnode_phys_t
*dnp
, dmu_buf_impl_t
*db
,
571 uint64_t object
, dnode_handle_t
*dnh
)
575 dn
= kmem_cache_alloc(dnode_cache
, KM_SLEEP
);
579 * Defer setting dn_objset until the dnode is ready to be a candidate
580 * for the dnode_move() callback.
582 dn
->dn_object
= object
;
587 if (dnp
->dn_datablkszsec
) {
588 dnode_setdblksz(dn
, dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
);
590 dn
->dn_datablksz
= 0;
591 dn
->dn_datablkszsec
= 0;
592 dn
->dn_datablkshift
= 0;
594 dn
->dn_indblkshift
= dnp
->dn_indblkshift
;
595 dn
->dn_nlevels
= dnp
->dn_nlevels
;
596 dn
->dn_type
= dnp
->dn_type
;
597 dn
->dn_nblkptr
= dnp
->dn_nblkptr
;
598 dn
->dn_checksum
= dnp
->dn_checksum
;
599 dn
->dn_compress
= dnp
->dn_compress
;
600 dn
->dn_bonustype
= dnp
->dn_bonustype
;
601 dn
->dn_bonuslen
= dnp
->dn_bonuslen
;
602 dn
->dn_num_slots
= dnp
->dn_extra_slots
+ 1;
603 dn
->dn_maxblkid
= dnp
->dn_maxblkid
;
604 dn
->dn_have_spill
= ((dnp
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
) != 0);
607 dmu_zfetch_init(&dn
->dn_zfetch
, dn
);
609 ASSERT(DMU_OT_IS_VALID(dn
->dn_phys
->dn_type
));
610 ASSERT(zrl_is_locked(&dnh
->dnh_zrlock
));
611 ASSERT(!DN_SLOT_IS_PTR(dnh
->dnh_dnode
));
613 mutex_enter(&os
->os_lock
);
616 * Exclude special dnodes from os_dnodes so an empty os_dnodes
617 * signifies that the special dnodes have no references from
618 * their children (the entries in os_dnodes). This allows
619 * dnode_destroy() to easily determine if the last child has
620 * been removed and then complete eviction of the objset.
622 if (!DMU_OBJECT_IS_SPECIAL(object
))
623 list_insert_head(&os
->os_dnodes
, dn
);
627 * Everything else must be valid before assigning dn_objset
628 * makes the dnode eligible for dnode_move().
633 mutex_exit(&os
->os_lock
);
635 arc_space_consume(sizeof (dnode_t
), ARC_SPACE_DNODE
);
641 * Caller must be holding the dnode handle, which is released upon return.
644 dnode_destroy(dnode_t
*dn
)
646 objset_t
*os
= dn
->dn_objset
;
647 boolean_t complete_os_eviction
= B_FALSE
;
649 ASSERT((dn
->dn_id_flags
& DN_ID_NEW_EXIST
) == 0);
651 mutex_enter(&os
->os_lock
);
652 POINTER_INVALIDATE(&dn
->dn_objset
);
653 if (!DMU_OBJECT_IS_SPECIAL(dn
->dn_object
)) {
654 list_remove(&os
->os_dnodes
, dn
);
655 complete_os_eviction
=
656 list_is_empty(&os
->os_dnodes
) &&
657 list_link_active(&os
->os_evicting_node
);
659 mutex_exit(&os
->os_lock
);
661 /* the dnode can no longer move, so we can release the handle */
662 if (!zrl_is_locked(&dn
->dn_handle
->dnh_zrlock
))
663 zrl_remove(&dn
->dn_handle
->dnh_zrlock
);
665 dn
->dn_allocated_txg
= 0;
667 dn
->dn_assigned_txg
= 0;
668 dn
->dn_dirty_txg
= 0;
671 dn
->dn_dirtyctx_firstset
= NULL
;
672 if (dn
->dn_bonus
!= NULL
) {
673 mutex_enter(&dn
->dn_bonus
->db_mtx
);
674 dbuf_destroy(dn
->dn_bonus
);
679 dn
->dn_have_spill
= B_FALSE
;
684 dn
->dn_oldprojid
= ZFS_DEFAULT_PROJID
;
687 dn
->dn_newprojid
= ZFS_DEFAULT_PROJID
;
690 dmu_zfetch_fini(&dn
->dn_zfetch
);
691 kmem_cache_free(dnode_cache
, dn
);
692 arc_space_return(sizeof (dnode_t
), ARC_SPACE_DNODE
);
694 if (complete_os_eviction
)
695 dmu_objset_evict_done(os
);
699 dnode_allocate(dnode_t
*dn
, dmu_object_type_t ot
, int blocksize
, int ibs
,
700 dmu_object_type_t bonustype
, int bonuslen
, int dn_slots
, dmu_tx_t
*tx
)
704 ASSERT3U(dn_slots
, >, 0);
705 ASSERT3U(dn_slots
<< DNODE_SHIFT
, <=,
706 spa_maxdnodesize(dmu_objset_spa(dn
->dn_objset
)));
707 ASSERT3U(blocksize
, <=,
708 spa_maxblocksize(dmu_objset_spa(dn
->dn_objset
)));
710 blocksize
= 1 << zfs_default_bs
;
712 blocksize
= P2ROUNDUP(blocksize
, SPA_MINBLOCKSIZE
);
715 ibs
= zfs_default_ibs
;
717 ibs
= MIN(MAX(ibs
, DN_MIN_INDBLKSHIFT
), DN_MAX_INDBLKSHIFT
);
719 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d dn_slots=%d\n",
720 dn
->dn_objset
, (u_longlong_t
)dn
->dn_object
,
721 (u_longlong_t
)tx
->tx_txg
, blocksize
, ibs
, dn_slots
);
722 DNODE_STAT_BUMP(dnode_allocate
);
724 ASSERT(dn
->dn_type
== DMU_OT_NONE
);
725 ASSERT0(memcmp(dn
->dn_phys
, &dnode_phys_zero
, sizeof (dnode_phys_t
)));
726 ASSERT(dn
->dn_phys
->dn_type
== DMU_OT_NONE
);
727 ASSERT(ot
!= DMU_OT_NONE
);
728 ASSERT(DMU_OT_IS_VALID(ot
));
729 ASSERT((bonustype
== DMU_OT_NONE
&& bonuslen
== 0) ||
730 (bonustype
== DMU_OT_SA
&& bonuslen
== 0) ||
731 (bonustype
== DMU_OTN_UINT64_METADATA
&& bonuslen
== 0) ||
732 (bonustype
!= DMU_OT_NONE
&& bonuslen
!= 0));
733 ASSERT(DMU_OT_IS_VALID(bonustype
));
734 ASSERT3U(bonuslen
, <=, DN_SLOTS_TO_BONUSLEN(dn_slots
));
735 ASSERT(dn
->dn_type
== DMU_OT_NONE
);
736 ASSERT0(dn
->dn_maxblkid
);
737 ASSERT0(dn
->dn_allocated_txg
);
738 ASSERT0(dn
->dn_assigned_txg
);
739 ASSERT(zfs_refcount_is_zero(&dn
->dn_tx_holds
));
740 ASSERT3U(zfs_refcount_count(&dn
->dn_holds
), <=, 1);
741 ASSERT(avl_is_empty(&dn
->dn_dbufs
));
743 for (i
= 0; i
< TXG_SIZE
; i
++) {
744 ASSERT0(dn
->dn_next_nblkptr
[i
]);
745 ASSERT0(dn
->dn_next_nlevels
[i
]);
746 ASSERT0(dn
->dn_next_indblkshift
[i
]);
747 ASSERT0(dn
->dn_next_bonuslen
[i
]);
748 ASSERT0(dn
->dn_next_bonustype
[i
]);
749 ASSERT0(dn
->dn_rm_spillblk
[i
]);
750 ASSERT0(dn
->dn_next_blksz
[i
]);
751 ASSERT0(dn
->dn_next_maxblkid
[i
]);
752 ASSERT(!multilist_link_active(&dn
->dn_dirty_link
[i
]));
753 ASSERT3P(list_head(&dn
->dn_dirty_records
[i
]), ==, NULL
);
754 ASSERT3P(dn
->dn_free_ranges
[i
], ==, NULL
);
758 dnode_setdblksz(dn
, blocksize
);
759 dn
->dn_indblkshift
= ibs
;
761 dn
->dn_num_slots
= dn_slots
;
762 if (bonustype
== DMU_OT_SA
) /* Maximize bonus space for SA */
765 dn
->dn_nblkptr
= MIN(DN_MAX_NBLKPTR
,
766 1 + ((DN_SLOTS_TO_BONUSLEN(dn_slots
) - bonuslen
) >>
770 dn
->dn_bonustype
= bonustype
;
771 dn
->dn_bonuslen
= bonuslen
;
772 dn
->dn_checksum
= ZIO_CHECKSUM_INHERIT
;
773 dn
->dn_compress
= ZIO_COMPRESS_INHERIT
;
777 dn
->dn_dirtyctx_firstset
= NULL
;
778 dn
->dn_dirty_txg
= 0;
780 dn
->dn_allocated_txg
= tx
->tx_txg
;
783 dnode_setdirty(dn
, tx
);
784 dn
->dn_next_indblkshift
[tx
->tx_txg
& TXG_MASK
] = ibs
;
785 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonuslen
;
786 dn
->dn_next_bonustype
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonustype
;
787 dn
->dn_next_blksz
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_datablksz
;
791 dnode_reallocate(dnode_t
*dn
, dmu_object_type_t ot
, int blocksize
,
792 dmu_object_type_t bonustype
, int bonuslen
, int dn_slots
,
793 boolean_t keep_spill
, dmu_tx_t
*tx
)
797 ASSERT3U(blocksize
, >=, SPA_MINBLOCKSIZE
);
798 ASSERT3U(blocksize
, <=,
799 spa_maxblocksize(dmu_objset_spa(dn
->dn_objset
)));
800 ASSERT0(blocksize
% SPA_MINBLOCKSIZE
);
801 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
|| dmu_tx_private_ok(tx
));
802 ASSERT(tx
->tx_txg
!= 0);
803 ASSERT((bonustype
== DMU_OT_NONE
&& bonuslen
== 0) ||
804 (bonustype
!= DMU_OT_NONE
&& bonuslen
!= 0) ||
805 (bonustype
== DMU_OT_SA
&& bonuslen
== 0));
806 ASSERT(DMU_OT_IS_VALID(bonustype
));
807 ASSERT3U(bonuslen
, <=,
808 DN_BONUS_SIZE(spa_maxdnodesize(dmu_objset_spa(dn
->dn_objset
))));
809 ASSERT3U(bonuslen
, <=, DN_BONUS_SIZE(dn_slots
<< DNODE_SHIFT
));
811 dnode_free_interior_slots(dn
);
812 DNODE_STAT_BUMP(dnode_reallocate
);
814 /* clean up any unreferenced dbufs */
815 dnode_evict_dbufs(dn
);
819 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
820 dnode_setdirty(dn
, tx
);
821 if (dn
->dn_datablksz
!= blocksize
) {
822 /* change blocksize */
823 ASSERT0(dn
->dn_maxblkid
);
824 ASSERT(BP_IS_HOLE(&dn
->dn_phys
->dn_blkptr
[0]) ||
825 dnode_block_freed(dn
, 0));
827 dnode_setdblksz(dn
, blocksize
);
828 dn
->dn_next_blksz
[tx
->tx_txg
& TXG_MASK
] = blocksize
;
830 if (dn
->dn_bonuslen
!= bonuslen
)
831 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = bonuslen
;
833 if (bonustype
== DMU_OT_SA
) /* Maximize bonus space for SA */
836 nblkptr
= MIN(DN_MAX_NBLKPTR
,
837 1 + ((DN_SLOTS_TO_BONUSLEN(dn_slots
) - bonuslen
) >>
839 if (dn
->dn_bonustype
!= bonustype
)
840 dn
->dn_next_bonustype
[tx
->tx_txg
& TXG_MASK
] = bonustype
;
841 if (dn
->dn_nblkptr
!= nblkptr
)
842 dn
->dn_next_nblkptr
[tx
->tx_txg
& TXG_MASK
] = nblkptr
;
843 if (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
&& !keep_spill
) {
844 dbuf_rm_spill(dn
, tx
);
845 dnode_rm_spill(dn
, tx
);
848 rw_exit(&dn
->dn_struct_rwlock
);
853 /* change bonus size and type */
854 mutex_enter(&dn
->dn_mtx
);
855 dn
->dn_bonustype
= bonustype
;
856 dn
->dn_bonuslen
= bonuslen
;
857 dn
->dn_num_slots
= dn_slots
;
858 dn
->dn_nblkptr
= nblkptr
;
859 dn
->dn_checksum
= ZIO_CHECKSUM_INHERIT
;
860 dn
->dn_compress
= ZIO_COMPRESS_INHERIT
;
861 ASSERT3U(dn
->dn_nblkptr
, <=, DN_MAX_NBLKPTR
);
863 /* fix up the bonus db_size */
865 dn
->dn_bonus
->db
.db_size
=
866 DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
867 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
868 ASSERT(dn
->dn_bonuslen
<= dn
->dn_bonus
->db
.db_size
);
871 dn
->dn_allocated_txg
= tx
->tx_txg
;
872 mutex_exit(&dn
->dn_mtx
);
877 dnode_move_impl(dnode_t
*odn
, dnode_t
*ndn
)
879 ASSERT(!RW_LOCK_HELD(&odn
->dn_struct_rwlock
));
880 ASSERT(MUTEX_NOT_HELD(&odn
->dn_mtx
));
881 ASSERT(MUTEX_NOT_HELD(&odn
->dn_dbufs_mtx
));
884 ndn
->dn_objset
= odn
->dn_objset
;
885 ndn
->dn_object
= odn
->dn_object
;
886 ndn
->dn_dbuf
= odn
->dn_dbuf
;
887 ndn
->dn_handle
= odn
->dn_handle
;
888 ndn
->dn_phys
= odn
->dn_phys
;
889 ndn
->dn_type
= odn
->dn_type
;
890 ndn
->dn_bonuslen
= odn
->dn_bonuslen
;
891 ndn
->dn_bonustype
= odn
->dn_bonustype
;
892 ndn
->dn_nblkptr
= odn
->dn_nblkptr
;
893 ndn
->dn_checksum
= odn
->dn_checksum
;
894 ndn
->dn_compress
= odn
->dn_compress
;
895 ndn
->dn_nlevels
= odn
->dn_nlevels
;
896 ndn
->dn_indblkshift
= odn
->dn_indblkshift
;
897 ndn
->dn_datablkshift
= odn
->dn_datablkshift
;
898 ndn
->dn_datablkszsec
= odn
->dn_datablkszsec
;
899 ndn
->dn_datablksz
= odn
->dn_datablksz
;
900 ndn
->dn_maxblkid
= odn
->dn_maxblkid
;
901 ndn
->dn_num_slots
= odn
->dn_num_slots
;
902 memcpy(ndn
->dn_next_type
, odn
->dn_next_type
,
903 sizeof (odn
->dn_next_type
));
904 memcpy(ndn
->dn_next_nblkptr
, odn
->dn_next_nblkptr
,
905 sizeof (odn
->dn_next_nblkptr
));
906 memcpy(ndn
->dn_next_nlevels
, odn
->dn_next_nlevels
,
907 sizeof (odn
->dn_next_nlevels
));
908 memcpy(ndn
->dn_next_indblkshift
, odn
->dn_next_indblkshift
,
909 sizeof (odn
->dn_next_indblkshift
));
910 memcpy(ndn
->dn_next_bonustype
, odn
->dn_next_bonustype
,
911 sizeof (odn
->dn_next_bonustype
));
912 memcpy(ndn
->dn_rm_spillblk
, odn
->dn_rm_spillblk
,
913 sizeof (odn
->dn_rm_spillblk
));
914 memcpy(ndn
->dn_next_bonuslen
, odn
->dn_next_bonuslen
,
915 sizeof (odn
->dn_next_bonuslen
));
916 memcpy(ndn
->dn_next_blksz
, odn
->dn_next_blksz
,
917 sizeof (odn
->dn_next_blksz
));
918 memcpy(ndn
->dn_next_maxblkid
, odn
->dn_next_maxblkid
,
919 sizeof (odn
->dn_next_maxblkid
));
920 for (int i
= 0; i
< TXG_SIZE
; i
++) {
921 list_move_tail(&ndn
->dn_dirty_records
[i
],
922 &odn
->dn_dirty_records
[i
]);
924 memcpy(ndn
->dn_free_ranges
, odn
->dn_free_ranges
,
925 sizeof (odn
->dn_free_ranges
));
926 ndn
->dn_allocated_txg
= odn
->dn_allocated_txg
;
927 ndn
->dn_free_txg
= odn
->dn_free_txg
;
928 ndn
->dn_assigned_txg
= odn
->dn_assigned_txg
;
929 ndn
->dn_dirty_txg
= odn
->dn_dirty_txg
;
930 ndn
->dn_dirtyctx
= odn
->dn_dirtyctx
;
931 ndn
->dn_dirtyctx_firstset
= odn
->dn_dirtyctx_firstset
;
932 ASSERT(zfs_refcount_count(&odn
->dn_tx_holds
) == 0);
933 zfs_refcount_transfer(&ndn
->dn_holds
, &odn
->dn_holds
);
934 ASSERT(avl_is_empty(&ndn
->dn_dbufs
));
935 avl_swap(&ndn
->dn_dbufs
, &odn
->dn_dbufs
);
936 ndn
->dn_dbufs_count
= odn
->dn_dbufs_count
;
937 ndn
->dn_bonus
= odn
->dn_bonus
;
938 ndn
->dn_have_spill
= odn
->dn_have_spill
;
939 ndn
->dn_zio
= odn
->dn_zio
;
940 ndn
->dn_oldused
= odn
->dn_oldused
;
941 ndn
->dn_oldflags
= odn
->dn_oldflags
;
942 ndn
->dn_olduid
= odn
->dn_olduid
;
943 ndn
->dn_oldgid
= odn
->dn_oldgid
;
944 ndn
->dn_oldprojid
= odn
->dn_oldprojid
;
945 ndn
->dn_newuid
= odn
->dn_newuid
;
946 ndn
->dn_newgid
= odn
->dn_newgid
;
947 ndn
->dn_newprojid
= odn
->dn_newprojid
;
948 ndn
->dn_id_flags
= odn
->dn_id_flags
;
949 dmu_zfetch_init(&ndn
->dn_zfetch
, ndn
);
952 * Update back pointers. Updating the handle fixes the back pointer of
953 * every descendant dbuf as well as the bonus dbuf.
955 ASSERT(ndn
->dn_handle
->dnh_dnode
== odn
);
956 ndn
->dn_handle
->dnh_dnode
= ndn
;
959 * Invalidate the original dnode by clearing all of its back pointers.
962 odn
->dn_handle
= NULL
;
963 avl_create(&odn
->dn_dbufs
, dbuf_compare
, sizeof (dmu_buf_impl_t
),
964 offsetof(dmu_buf_impl_t
, db_link
));
965 odn
->dn_dbufs_count
= 0;
966 odn
->dn_bonus
= NULL
;
967 dmu_zfetch_fini(&odn
->dn_zfetch
);
970 * Set the low bit of the objset pointer to ensure that dnode_move()
971 * recognizes the dnode as invalid in any subsequent callback.
973 POINTER_INVALIDATE(&odn
->dn_objset
);
976 * Satisfy the destructor.
978 for (int i
= 0; i
< TXG_SIZE
; i
++) {
979 list_create(&odn
->dn_dirty_records
[i
],
980 sizeof (dbuf_dirty_record_t
),
981 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
982 odn
->dn_free_ranges
[i
] = NULL
;
983 odn
->dn_next_nlevels
[i
] = 0;
984 odn
->dn_next_indblkshift
[i
] = 0;
985 odn
->dn_next_bonustype
[i
] = 0;
986 odn
->dn_rm_spillblk
[i
] = 0;
987 odn
->dn_next_bonuslen
[i
] = 0;
988 odn
->dn_next_blksz
[i
] = 0;
990 odn
->dn_allocated_txg
= 0;
991 odn
->dn_free_txg
= 0;
992 odn
->dn_assigned_txg
= 0;
993 odn
->dn_dirty_txg
= 0;
994 odn
->dn_dirtyctx
= 0;
995 odn
->dn_dirtyctx_firstset
= NULL
;
996 odn
->dn_have_spill
= B_FALSE
;
999 odn
->dn_oldflags
= 0;
1002 odn
->dn_oldprojid
= ZFS_DEFAULT_PROJID
;
1005 odn
->dn_newprojid
= ZFS_DEFAULT_PROJID
;
1006 odn
->dn_id_flags
= 0;
1012 odn
->dn_moved
= (uint8_t)-1;
1016 dnode_move(void *buf
, void *newbuf
, size_t size
, void *arg
)
1018 dnode_t
*odn
= buf
, *ndn
= newbuf
;
1024 * The dnode is on the objset's list of known dnodes if the objset
1025 * pointer is valid. We set the low bit of the objset pointer when
1026 * freeing the dnode to invalidate it, and the memory patterns written
1027 * by kmem (baddcafe and deadbeef) set at least one of the two low bits.
1028 * A newly created dnode sets the objset pointer last of all to indicate
1029 * that the dnode is known and in a valid state to be moved by this
1032 os
= odn
->dn_objset
;
1033 if (!POINTER_IS_VALID(os
)) {
1034 DNODE_STAT_BUMP(dnode_move_invalid
);
1035 return (KMEM_CBRC_DONT_KNOW
);
1039 * Ensure that the objset does not go away during the move.
1041 rw_enter(&os_lock
, RW_WRITER
);
1042 if (os
!= odn
->dn_objset
) {
1044 DNODE_STAT_BUMP(dnode_move_recheck1
);
1045 return (KMEM_CBRC_DONT_KNOW
);
1049 * If the dnode is still valid, then so is the objset. We know that no
1050 * valid objset can be freed while we hold os_lock, so we can safely
1051 * ensure that the objset remains in use.
1053 mutex_enter(&os
->os_lock
);
1056 * Recheck the objset pointer in case the dnode was removed just before
1057 * acquiring the lock.
1059 if (os
!= odn
->dn_objset
) {
1060 mutex_exit(&os
->os_lock
);
1062 DNODE_STAT_BUMP(dnode_move_recheck2
);
1063 return (KMEM_CBRC_DONT_KNOW
);
1067 * At this point we know that as long as we hold os->os_lock, the dnode
1068 * cannot be freed and fields within the dnode can be safely accessed.
1069 * The objset listing this dnode cannot go away as long as this dnode is
1073 if (DMU_OBJECT_IS_SPECIAL(odn
->dn_object
)) {
1074 mutex_exit(&os
->os_lock
);
1075 DNODE_STAT_BUMP(dnode_move_special
);
1076 return (KMEM_CBRC_NO
);
1078 ASSERT(odn
->dn_dbuf
!= NULL
); /* only "special" dnodes have no parent */
1081 * Lock the dnode handle to prevent the dnode from obtaining any new
1082 * holds. This also prevents the descendant dbufs and the bonus dbuf
1083 * from accessing the dnode, so that we can discount their holds. The
1084 * handle is safe to access because we know that while the dnode cannot
1085 * go away, neither can its handle. Once we hold dnh_zrlock, we can
1086 * safely move any dnode referenced only by dbufs.
1088 if (!zrl_tryenter(&odn
->dn_handle
->dnh_zrlock
)) {
1089 mutex_exit(&os
->os_lock
);
1090 DNODE_STAT_BUMP(dnode_move_handle
);
1091 return (KMEM_CBRC_LATER
);
1095 * Ensure a consistent view of the dnode's holds and the dnode's dbufs.
1096 * We need to guarantee that there is a hold for every dbuf in order to
1097 * determine whether the dnode is actively referenced. Falsely matching
1098 * a dbuf to an active hold would lead to an unsafe move. It's possible
1099 * that a thread already having an active dnode hold is about to add a
1100 * dbuf, and we can't compare hold and dbuf counts while the add is in
1103 if (!rw_tryenter(&odn
->dn_struct_rwlock
, RW_WRITER
)) {
1104 zrl_exit(&odn
->dn_handle
->dnh_zrlock
);
1105 mutex_exit(&os
->os_lock
);
1106 DNODE_STAT_BUMP(dnode_move_rwlock
);
1107 return (KMEM_CBRC_LATER
);
1111 * A dbuf may be removed (evicted) without an active dnode hold. In that
1112 * case, the dbuf count is decremented under the handle lock before the
1113 * dbuf's hold is released. This order ensures that if we count the hold
1114 * after the dbuf is removed but before its hold is released, we will
1115 * treat the unmatched hold as active and exit safely. If we count the
1116 * hold before the dbuf is removed, the hold is discounted, and the
1117 * removal is blocked until the move completes.
1119 refcount
= zfs_refcount_count(&odn
->dn_holds
);
1120 ASSERT(refcount
>= 0);
1121 dbufs
= DN_DBUFS_COUNT(odn
);
1123 /* We can't have more dbufs than dnode holds. */
1124 ASSERT3U(dbufs
, <=, refcount
);
1125 DTRACE_PROBE3(dnode__move
, dnode_t
*, odn
, int64_t, refcount
,
1128 if (refcount
> dbufs
) {
1129 rw_exit(&odn
->dn_struct_rwlock
);
1130 zrl_exit(&odn
->dn_handle
->dnh_zrlock
);
1131 mutex_exit(&os
->os_lock
);
1132 DNODE_STAT_BUMP(dnode_move_active
);
1133 return (KMEM_CBRC_LATER
);
1136 rw_exit(&odn
->dn_struct_rwlock
);
1139 * At this point we know that anyone with a hold on the dnode is not
1140 * actively referencing it. The dnode is known and in a valid state to
1141 * move. We're holding the locks needed to execute the critical section.
1143 dnode_move_impl(odn
, ndn
);
1145 list_link_replace(&odn
->dn_link
, &ndn
->dn_link
);
1146 /* If the dnode was safe to move, the refcount cannot have changed. */
1147 ASSERT(refcount
== zfs_refcount_count(&ndn
->dn_holds
));
1148 ASSERT(dbufs
== DN_DBUFS_COUNT(ndn
));
1149 zrl_exit(&ndn
->dn_handle
->dnh_zrlock
); /* handle has moved */
1150 mutex_exit(&os
->os_lock
);
1152 return (KMEM_CBRC_YES
);
1154 #endif /* _KERNEL */
1157 dnode_slots_hold(dnode_children_t
*children
, int idx
, int slots
)
1159 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1161 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1162 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1163 zrl_add(&dnh
->dnh_zrlock
);
1168 dnode_slots_rele(dnode_children_t
*children
, int idx
, int slots
)
1170 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1172 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1173 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1175 if (zrl_is_locked(&dnh
->dnh_zrlock
))
1176 zrl_exit(&dnh
->dnh_zrlock
);
1178 zrl_remove(&dnh
->dnh_zrlock
);
1183 dnode_slots_tryenter(dnode_children_t
*children
, int idx
, int slots
)
1185 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1187 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1188 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1190 if (!zrl_tryenter(&dnh
->dnh_zrlock
)) {
1191 for (int j
= idx
; j
< i
; j
++) {
1192 dnh
= &children
->dnc_children
[j
];
1193 zrl_exit(&dnh
->dnh_zrlock
);
1204 dnode_set_slots(dnode_children_t
*children
, int idx
, int slots
, void *ptr
)
1206 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1208 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1209 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1210 dnh
->dnh_dnode
= ptr
;
1215 dnode_check_slots_free(dnode_children_t
*children
, int idx
, int slots
)
1217 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1220 * If all dnode slots are either already free or
1221 * evictable return B_TRUE.
1223 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1224 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1225 dnode_t
*dn
= dnh
->dnh_dnode
;
1227 if (dn
== DN_SLOT_FREE
) {
1229 } else if (DN_SLOT_IS_PTR(dn
)) {
1230 mutex_enter(&dn
->dn_mtx
);
1231 boolean_t can_free
= (dn
->dn_type
== DMU_OT_NONE
&&
1232 zfs_refcount_is_zero(&dn
->dn_holds
) &&
1233 !DNODE_IS_DIRTY(dn
));
1234 mutex_exit(&dn
->dn_mtx
);
1249 dnode_reclaim_slots(dnode_children_t
*children
, int idx
, int slots
)
1251 uint_t reclaimed
= 0;
1253 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1255 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1256 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1258 ASSERT(zrl_is_locked(&dnh
->dnh_zrlock
));
1260 if (DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1261 ASSERT3S(dnh
->dnh_dnode
->dn_type
, ==, DMU_OT_NONE
);
1262 dnode_destroy(dnh
->dnh_dnode
);
1263 dnh
->dnh_dnode
= DN_SLOT_FREE
;
1272 dnode_free_interior_slots(dnode_t
*dn
)
1274 dnode_children_t
*children
= dmu_buf_get_user(&dn
->dn_dbuf
->db
);
1275 int epb
= dn
->dn_dbuf
->db
.db_size
>> DNODE_SHIFT
;
1276 int idx
= (dn
->dn_object
& (epb
- 1)) + 1;
1277 int slots
= dn
->dn_num_slots
- 1;
1282 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1284 while (!dnode_slots_tryenter(children
, idx
, slots
)) {
1285 DNODE_STAT_BUMP(dnode_free_interior_lock_retry
);
1286 kpreempt(KPREEMPT_SYNC
);
1289 dnode_set_slots(children
, idx
, slots
, DN_SLOT_FREE
);
1290 dnode_slots_rele(children
, idx
, slots
);
1294 dnode_special_close(dnode_handle_t
*dnh
)
1296 dnode_t
*dn
= dnh
->dnh_dnode
;
1299 * Ensure dnode_rele_and_unlock() has released dn_mtx, after final
1300 * zfs_refcount_remove()
1302 mutex_enter(&dn
->dn_mtx
);
1303 if (zfs_refcount_count(&dn
->dn_holds
) > 0)
1304 cv_wait(&dn
->dn_nodnholds
, &dn
->dn_mtx
);
1305 mutex_exit(&dn
->dn_mtx
);
1306 ASSERT3U(zfs_refcount_count(&dn
->dn_holds
), ==, 0);
1308 ASSERT(dn
->dn_dbuf
== NULL
||
1309 dmu_buf_get_user(&dn
->dn_dbuf
->db
) == NULL
);
1310 zrl_add(&dnh
->dnh_zrlock
);
1311 dnode_destroy(dn
); /* implicit zrl_remove() */
1312 zrl_destroy(&dnh
->dnh_zrlock
);
1313 dnh
->dnh_dnode
= NULL
;
1317 dnode_special_open(objset_t
*os
, dnode_phys_t
*dnp
, uint64_t object
,
1318 dnode_handle_t
*dnh
)
1322 zrl_init(&dnh
->dnh_zrlock
);
1323 VERIFY3U(1, ==, zrl_tryenter(&dnh
->dnh_zrlock
));
1325 dn
= dnode_create(os
, dnp
, NULL
, object
, dnh
);
1328 zrl_exit(&dnh
->dnh_zrlock
);
1332 dnode_buf_evict_async(void *dbu
)
1334 dnode_children_t
*dnc
= dbu
;
1336 DNODE_STAT_BUMP(dnode_buf_evict
);
1338 for (int i
= 0; i
< dnc
->dnc_count
; i
++) {
1339 dnode_handle_t
*dnh
= &dnc
->dnc_children
[i
];
1343 * The dnode handle lock guards against the dnode moving to
1344 * another valid address, so there is no need here to guard
1345 * against changes to or from NULL.
1347 if (!DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1348 zrl_destroy(&dnh
->dnh_zrlock
);
1349 dnh
->dnh_dnode
= DN_SLOT_UNINIT
;
1353 zrl_add(&dnh
->dnh_zrlock
);
1354 dn
= dnh
->dnh_dnode
;
1356 * If there are holds on this dnode, then there should
1357 * be holds on the dnode's containing dbuf as well; thus
1358 * it wouldn't be eligible for eviction and this function
1359 * would not have been called.
1361 ASSERT(zfs_refcount_is_zero(&dn
->dn_holds
));
1362 ASSERT(zfs_refcount_is_zero(&dn
->dn_tx_holds
));
1364 dnode_destroy(dn
); /* implicit zrl_remove() for first slot */
1365 zrl_destroy(&dnh
->dnh_zrlock
);
1366 dnh
->dnh_dnode
= DN_SLOT_UNINIT
;
1368 kmem_free(dnc
, sizeof (dnode_children_t
) +
1369 dnc
->dnc_count
* sizeof (dnode_handle_t
));
1373 * When the DNODE_MUST_BE_FREE flag is set, the "slots" parameter is used
1374 * to ensure the hole at the specified object offset is large enough to
1375 * hold the dnode being created. The slots parameter is also used to ensure
1376 * a dnode does not span multiple dnode blocks. In both of these cases, if
1377 * a failure occurs, ENOSPC is returned. Keep in mind, these failure cases
1378 * are only possible when using DNODE_MUST_BE_FREE.
1380 * If the DNODE_MUST_BE_ALLOCATED flag is set, "slots" must be 0.
1381 * dnode_hold_impl() will check if the requested dnode is already consumed
1382 * as an extra dnode slot by an large dnode, in which case it returns
1385 * If the DNODE_DRY_RUN flag is set, we don't actually hold the dnode, just
1386 * return whether the hold would succeed or not. tag and dnp should set to
1387 * NULL in this case.
1390 * EINVAL - Invalid object number or flags.
1391 * ENOSPC - Hole too small to fulfill "slots" request (DNODE_MUST_BE_FREE)
1392 * EEXIST - Refers to an allocated dnode (DNODE_MUST_BE_FREE)
1393 * - Refers to a freeing dnode (DNODE_MUST_BE_FREE)
1394 * - Refers to an interior dnode slot (DNODE_MUST_BE_ALLOCATED)
1395 * ENOENT - The requested dnode is not allocated (DNODE_MUST_BE_ALLOCATED)
1396 * - The requested dnode is being freed (DNODE_MUST_BE_ALLOCATED)
1397 * EIO - I/O error when reading the meta dnode dbuf.
1399 * succeeds even for free dnodes.
1402 dnode_hold_impl(objset_t
*os
, uint64_t object
, int flag
, int slots
,
1403 const void *tag
, dnode_t
**dnp
)
1406 int drop_struct_lock
= FALSE
;
1411 dnode_children_t
*dnc
;
1412 dnode_phys_t
*dn_block
;
1413 dnode_handle_t
*dnh
;
1415 ASSERT(!(flag
& DNODE_MUST_BE_ALLOCATED
) || (slots
== 0));
1416 ASSERT(!(flag
& DNODE_MUST_BE_FREE
) || (slots
> 0));
1417 IMPLY(flag
& DNODE_DRY_RUN
, (tag
== NULL
) && (dnp
== NULL
));
1420 * If you are holding the spa config lock as writer, you shouldn't
1421 * be asking the DMU to do *anything* unless it's the root pool
1422 * which may require us to read from the root filesystem while
1423 * holding some (not all) of the locks as writer.
1425 ASSERT(spa_config_held(os
->os_spa
, SCL_ALL
, RW_WRITER
) == 0 ||
1426 (spa_is_root(os
->os_spa
) &&
1427 spa_config_held(os
->os_spa
, SCL_STATE
, RW_WRITER
)));
1429 ASSERT((flag
& DNODE_MUST_BE_ALLOCATED
) || (flag
& DNODE_MUST_BE_FREE
));
1431 if (object
== DMU_USERUSED_OBJECT
|| object
== DMU_GROUPUSED_OBJECT
||
1432 object
== DMU_PROJECTUSED_OBJECT
) {
1433 if (object
== DMU_USERUSED_OBJECT
)
1434 dn
= DMU_USERUSED_DNODE(os
);
1435 else if (object
== DMU_GROUPUSED_OBJECT
)
1436 dn
= DMU_GROUPUSED_DNODE(os
);
1438 dn
= DMU_PROJECTUSED_DNODE(os
);
1440 return (SET_ERROR(ENOENT
));
1442 if ((flag
& DNODE_MUST_BE_ALLOCATED
) && type
== DMU_OT_NONE
)
1443 return (SET_ERROR(ENOENT
));
1444 if ((flag
& DNODE_MUST_BE_FREE
) && type
!= DMU_OT_NONE
)
1445 return (SET_ERROR(EEXIST
));
1447 /* Don't actually hold if dry run, just return 0 */
1448 if (!(flag
& DNODE_DRY_RUN
)) {
1449 (void) zfs_refcount_add(&dn
->dn_holds
, tag
);
1455 if (object
== 0 || object
>= DN_MAX_OBJECT
)
1456 return (SET_ERROR(EINVAL
));
1458 mdn
= DMU_META_DNODE(os
);
1459 ASSERT(mdn
->dn_object
== DMU_META_DNODE_OBJECT
);
1463 if (!RW_WRITE_HELD(&mdn
->dn_struct_rwlock
)) {
1464 rw_enter(&mdn
->dn_struct_rwlock
, RW_READER
);
1465 drop_struct_lock
= TRUE
;
1468 blk
= dbuf_whichblock(mdn
, 0, object
* sizeof (dnode_phys_t
));
1469 db
= dbuf_hold(mdn
, blk
, FTAG
);
1470 if (drop_struct_lock
)
1471 rw_exit(&mdn
->dn_struct_rwlock
);
1473 DNODE_STAT_BUMP(dnode_hold_dbuf_hold
);
1474 return (SET_ERROR(EIO
));
1478 * We do not need to decrypt to read the dnode so it doesn't matter
1479 * if we get the encrypted or decrypted version.
1481 err
= dbuf_read(db
, NULL
, DB_RF_CANFAIL
|
1482 DB_RF_NO_DECRYPT
| DB_RF_NOPREFETCH
);
1484 DNODE_STAT_BUMP(dnode_hold_dbuf_read
);
1485 dbuf_rele(db
, FTAG
);
1489 ASSERT3U(db
->db
.db_size
, >=, 1<<DNODE_SHIFT
);
1490 epb
= db
->db
.db_size
>> DNODE_SHIFT
;
1492 idx
= object
& (epb
- 1);
1493 dn_block
= (dnode_phys_t
*)db
->db
.db_data
;
1495 ASSERT(DB_DNODE(db
)->dn_type
== DMU_OT_DNODE
);
1496 dnc
= dmu_buf_get_user(&db
->db
);
1499 dnode_children_t
*winner
;
1502 dnc
= kmem_zalloc(sizeof (dnode_children_t
) +
1503 epb
* sizeof (dnode_handle_t
), KM_SLEEP
);
1504 dnc
->dnc_count
= epb
;
1505 dnh
= &dnc
->dnc_children
[0];
1507 /* Initialize dnode slot status from dnode_phys_t */
1508 for (int i
= 0; i
< epb
; i
++) {
1509 zrl_init(&dnh
[i
].dnh_zrlock
);
1516 if (dn_block
[i
].dn_type
!= DMU_OT_NONE
) {
1517 int interior
= dn_block
[i
].dn_extra_slots
;
1519 dnode_set_slots(dnc
, i
, 1, DN_SLOT_ALLOCATED
);
1520 dnode_set_slots(dnc
, i
+ 1, interior
,
1524 dnh
[i
].dnh_dnode
= DN_SLOT_FREE
;
1529 dmu_buf_init_user(&dnc
->dnc_dbu
, NULL
,
1530 dnode_buf_evict_async
, NULL
);
1531 winner
= dmu_buf_set_user(&db
->db
, &dnc
->dnc_dbu
);
1532 if (winner
!= NULL
) {
1534 for (int i
= 0; i
< epb
; i
++)
1535 zrl_destroy(&dnh
[i
].dnh_zrlock
);
1537 kmem_free(dnc
, sizeof (dnode_children_t
) +
1538 epb
* sizeof (dnode_handle_t
));
1543 ASSERT(dnc
->dnc_count
== epb
);
1545 if (flag
& DNODE_MUST_BE_ALLOCATED
) {
1548 dnode_slots_hold(dnc
, idx
, slots
);
1549 dnh
= &dnc
->dnc_children
[idx
];
1551 if (DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1552 dn
= dnh
->dnh_dnode
;
1553 } else if (dnh
->dnh_dnode
== DN_SLOT_INTERIOR
) {
1554 DNODE_STAT_BUMP(dnode_hold_alloc_interior
);
1555 dnode_slots_rele(dnc
, idx
, slots
);
1556 dbuf_rele(db
, FTAG
);
1557 return (SET_ERROR(EEXIST
));
1558 } else if (dnh
->dnh_dnode
!= DN_SLOT_ALLOCATED
) {
1559 DNODE_STAT_BUMP(dnode_hold_alloc_misses
);
1560 dnode_slots_rele(dnc
, idx
, slots
);
1561 dbuf_rele(db
, FTAG
);
1562 return (SET_ERROR(ENOENT
));
1564 dnode_slots_rele(dnc
, idx
, slots
);
1565 while (!dnode_slots_tryenter(dnc
, idx
, slots
)) {
1566 DNODE_STAT_BUMP(dnode_hold_alloc_lock_retry
);
1567 kpreempt(KPREEMPT_SYNC
);
1571 * Someone else won the race and called dnode_create()
1572 * after we checked DN_SLOT_IS_PTR() above but before
1573 * we acquired the lock.
1575 if (DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1576 DNODE_STAT_BUMP(dnode_hold_alloc_lock_misses
);
1577 dn
= dnh
->dnh_dnode
;
1579 dn
= dnode_create(os
, dn_block
+ idx
, db
,
1581 dmu_buf_add_user_size(&db
->db
,
1586 mutex_enter(&dn
->dn_mtx
);
1587 if (dn
->dn_type
== DMU_OT_NONE
|| dn
->dn_free_txg
!= 0) {
1588 DNODE_STAT_BUMP(dnode_hold_alloc_type_none
);
1589 mutex_exit(&dn
->dn_mtx
);
1590 dnode_slots_rele(dnc
, idx
, slots
);
1591 dbuf_rele(db
, FTAG
);
1592 return (SET_ERROR(ENOENT
));
1595 /* Don't actually hold if dry run, just return 0 */
1596 if (flag
& DNODE_DRY_RUN
) {
1597 mutex_exit(&dn
->dn_mtx
);
1598 dnode_slots_rele(dnc
, idx
, slots
);
1599 dbuf_rele(db
, FTAG
);
1603 DNODE_STAT_BUMP(dnode_hold_alloc_hits
);
1604 } else if (flag
& DNODE_MUST_BE_FREE
) {
1606 if (idx
+ slots
- 1 >= DNODES_PER_BLOCK
) {
1607 DNODE_STAT_BUMP(dnode_hold_free_overflow
);
1608 dbuf_rele(db
, FTAG
);
1609 return (SET_ERROR(ENOSPC
));
1612 dnode_slots_hold(dnc
, idx
, slots
);
1614 if (!dnode_check_slots_free(dnc
, idx
, slots
)) {
1615 DNODE_STAT_BUMP(dnode_hold_free_misses
);
1616 dnode_slots_rele(dnc
, idx
, slots
);
1617 dbuf_rele(db
, FTAG
);
1618 return (SET_ERROR(ENOSPC
));
1621 dnode_slots_rele(dnc
, idx
, slots
);
1622 while (!dnode_slots_tryenter(dnc
, idx
, slots
)) {
1623 DNODE_STAT_BUMP(dnode_hold_free_lock_retry
);
1624 kpreempt(KPREEMPT_SYNC
);
1627 if (!dnode_check_slots_free(dnc
, idx
, slots
)) {
1628 DNODE_STAT_BUMP(dnode_hold_free_lock_misses
);
1629 dnode_slots_rele(dnc
, idx
, slots
);
1630 dbuf_rele(db
, FTAG
);
1631 return (SET_ERROR(ENOSPC
));
1635 * Allocated but otherwise free dnodes which would
1636 * be in the interior of a multi-slot dnodes need
1637 * to be freed. Single slot dnodes can be safely
1638 * re-purposed as a performance optimization.
1642 dnode_reclaim_slots(dnc
, idx
+ 1, slots
- 1);
1644 dmu_buf_sub_user_size(&db
->db
,
1645 reclaimed
* sizeof (dnode_t
));
1648 dnh
= &dnc
->dnc_children
[idx
];
1649 if (DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1650 dn
= dnh
->dnh_dnode
;
1652 dn
= dnode_create(os
, dn_block
+ idx
, db
,
1654 dmu_buf_add_user_size(&db
->db
, sizeof (dnode_t
));
1657 mutex_enter(&dn
->dn_mtx
);
1658 if (!zfs_refcount_is_zero(&dn
->dn_holds
) || dn
->dn_free_txg
) {
1659 DNODE_STAT_BUMP(dnode_hold_free_refcount
);
1660 mutex_exit(&dn
->dn_mtx
);
1661 dnode_slots_rele(dnc
, idx
, slots
);
1662 dbuf_rele(db
, FTAG
);
1663 return (SET_ERROR(EEXIST
));
1666 /* Don't actually hold if dry run, just return 0 */
1667 if (flag
& DNODE_DRY_RUN
) {
1668 mutex_exit(&dn
->dn_mtx
);
1669 dnode_slots_rele(dnc
, idx
, slots
);
1670 dbuf_rele(db
, FTAG
);
1674 dnode_set_slots(dnc
, idx
+ 1, slots
- 1, DN_SLOT_INTERIOR
);
1675 DNODE_STAT_BUMP(dnode_hold_free_hits
);
1677 dbuf_rele(db
, FTAG
);
1678 return (SET_ERROR(EINVAL
));
1681 ASSERT0(dn
->dn_free_txg
);
1683 if (zfs_refcount_add(&dn
->dn_holds
, tag
) == 1)
1684 dbuf_add_ref(db
, dnh
);
1686 mutex_exit(&dn
->dn_mtx
);
1688 /* Now we can rely on the hold to prevent the dnode from moving. */
1689 dnode_slots_rele(dnc
, idx
, slots
);
1692 ASSERT3P(dnp
, !=, NULL
);
1693 ASSERT3P(dn
->dn_dbuf
, ==, db
);
1694 ASSERT3U(dn
->dn_object
, ==, object
);
1695 dbuf_rele(db
, FTAG
);
1702 * Return held dnode if the object is allocated, NULL if not.
1705 dnode_hold(objset_t
*os
, uint64_t object
, const void *tag
, dnode_t
**dnp
)
1707 return (dnode_hold_impl(os
, object
, DNODE_MUST_BE_ALLOCATED
, 0, tag
,
1712 * Can only add a reference if there is already at least one
1713 * reference on the dnode. Returns FALSE if unable to add a
1717 dnode_add_ref(dnode_t
*dn
, const void *tag
)
1719 mutex_enter(&dn
->dn_mtx
);
1720 if (zfs_refcount_is_zero(&dn
->dn_holds
)) {
1721 mutex_exit(&dn
->dn_mtx
);
1724 VERIFY(1 < zfs_refcount_add(&dn
->dn_holds
, tag
));
1725 mutex_exit(&dn
->dn_mtx
);
1730 dnode_rele(dnode_t
*dn
, const void *tag
)
1732 mutex_enter(&dn
->dn_mtx
);
1733 dnode_rele_and_unlock(dn
, tag
, B_FALSE
);
1737 dnode_rele_and_unlock(dnode_t
*dn
, const void *tag
, boolean_t evicting
)
1740 /* Get while the hold prevents the dnode from moving. */
1741 dmu_buf_impl_t
*db
= dn
->dn_dbuf
;
1742 dnode_handle_t
*dnh
= dn
->dn_handle
;
1744 refs
= zfs_refcount_remove(&dn
->dn_holds
, tag
);
1746 cv_broadcast(&dn
->dn_nodnholds
);
1747 mutex_exit(&dn
->dn_mtx
);
1748 /* dnode could get destroyed at this point, so don't use it anymore */
1751 * It's unsafe to release the last hold on a dnode by dnode_rele() or
1752 * indirectly by dbuf_rele() while relying on the dnode handle to
1753 * prevent the dnode from moving, since releasing the last hold could
1754 * result in the dnode's parent dbuf evicting its dnode handles. For
1755 * that reason anyone calling dnode_rele() or dbuf_rele() without some
1756 * other direct or indirect hold on the dnode must first drop the dnode
1760 ASSERT(refs
> 0 || dnh
->dnh_zrlock
.zr_owner
!= curthread
);
1763 /* NOTE: the DNODE_DNODE does not have a dn_dbuf */
1764 if (refs
== 0 && db
!= NULL
) {
1766 * Another thread could add a hold to the dnode handle in
1767 * dnode_hold_impl() while holding the parent dbuf. Since the
1768 * hold on the parent dbuf prevents the handle from being
1769 * destroyed, the hold on the handle is OK. We can't yet assert
1770 * that the handle has zero references, but that will be
1771 * asserted anyway when the handle gets destroyed.
1773 mutex_enter(&db
->db_mtx
);
1774 dbuf_rele_and_unlock(db
, dnh
, evicting
);
1779 * Test whether we can create a dnode at the specified location.
1782 dnode_try_claim(objset_t
*os
, uint64_t object
, int slots
)
1784 return (dnode_hold_impl(os
, object
, DNODE_MUST_BE_FREE
| DNODE_DRY_RUN
,
1785 slots
, NULL
, NULL
));
1789 * Checks if the dnode itself is dirty, or is carrying any uncommitted records.
1790 * It is important to check both conditions, as some operations (eg appending
1791 * to a file) can dirty both as a single logical unit, but they are not synced
1792 * out atomically, so checking one and not the other can result in an object
1793 * appearing to be clean mid-way through a commit.
1795 * Do not change this lightly! If you get it wrong, dmu_offset_next() can
1796 * detect a hole where there is really data, leading to silent corruption.
1799 dnode_is_dirty(dnode_t
*dn
)
1801 mutex_enter(&dn
->dn_mtx
);
1803 for (int i
= 0; i
< TXG_SIZE
; i
++) {
1804 if (multilist_link_active(&dn
->dn_dirty_link
[i
]) ||
1805 !list_is_empty(&dn
->dn_dirty_records
[i
])) {
1806 mutex_exit(&dn
->dn_mtx
);
1811 mutex_exit(&dn
->dn_mtx
);
1817 dnode_setdirty(dnode_t
*dn
, dmu_tx_t
*tx
)
1819 objset_t
*os
= dn
->dn_objset
;
1820 uint64_t txg
= tx
->tx_txg
;
1822 if (DMU_OBJECT_IS_SPECIAL(dn
->dn_object
)) {
1823 dsl_dataset_dirty(os
->os_dsl_dataset
, tx
);
1830 mutex_enter(&dn
->dn_mtx
);
1831 ASSERT(dn
->dn_phys
->dn_type
|| dn
->dn_allocated_txg
);
1832 ASSERT(dn
->dn_free_txg
== 0 || dn
->dn_free_txg
>= txg
);
1833 mutex_exit(&dn
->dn_mtx
);
1837 * Determine old uid/gid when necessary
1839 dmu_objset_userquota_get_ids(dn
, B_TRUE
, tx
);
1841 multilist_t
*dirtylist
= &os
->os_dirty_dnodes
[txg
& TXG_MASK
];
1842 multilist_sublist_t
*mls
= multilist_sublist_lock_obj(dirtylist
, dn
);
1845 * If we are already marked dirty, we're done.
1847 if (multilist_link_active(&dn
->dn_dirty_link
[txg
& TXG_MASK
])) {
1848 multilist_sublist_unlock(mls
);
1852 ASSERT(!zfs_refcount_is_zero(&dn
->dn_holds
) ||
1853 !avl_is_empty(&dn
->dn_dbufs
));
1854 ASSERT(dn
->dn_datablksz
!= 0);
1855 ASSERT0(dn
->dn_next_bonuslen
[txg
& TXG_MASK
]);
1856 ASSERT0(dn
->dn_next_blksz
[txg
& TXG_MASK
]);
1857 ASSERT0(dn
->dn_next_bonustype
[txg
& TXG_MASK
]);
1859 dprintf_ds(os
->os_dsl_dataset
, "obj=%llu txg=%llu\n",
1860 (u_longlong_t
)dn
->dn_object
, (u_longlong_t
)txg
);
1862 multilist_sublist_insert_head(mls
, dn
);
1864 multilist_sublist_unlock(mls
);
1867 * The dnode maintains a hold on its containing dbuf as
1868 * long as there are holds on it. Each instantiated child
1869 * dbuf maintains a hold on the dnode. When the last child
1870 * drops its hold, the dnode will drop its hold on the
1871 * containing dbuf. We add a "dirty hold" here so that the
1872 * dnode will hang around after we finish processing its
1875 VERIFY(dnode_add_ref(dn
, (void *)(uintptr_t)tx
->tx_txg
));
1877 (void) dbuf_dirty(dn
->dn_dbuf
, tx
);
1879 dsl_dataset_dirty(os
->os_dsl_dataset
, tx
);
1883 dnode_free(dnode_t
*dn
, dmu_tx_t
*tx
)
1885 mutex_enter(&dn
->dn_mtx
);
1886 if (dn
->dn_type
== DMU_OT_NONE
|| dn
->dn_free_txg
) {
1887 mutex_exit(&dn
->dn_mtx
);
1890 dn
->dn_free_txg
= tx
->tx_txg
;
1891 mutex_exit(&dn
->dn_mtx
);
1893 dnode_setdirty(dn
, tx
);
1897 * Try to change the block size for the indicated dnode. This can only
1898 * succeed if there are no blocks allocated or dirty beyond first block
1901 dnode_set_blksz(dnode_t
*dn
, uint64_t size
, int ibs
, dmu_tx_t
*tx
)
1906 ASSERT3U(size
, <=, spa_maxblocksize(dmu_objset_spa(dn
->dn_objset
)));
1908 size
= SPA_MINBLOCKSIZE
;
1910 size
= P2ROUNDUP(size
, SPA_MINBLOCKSIZE
);
1912 if (ibs
== dn
->dn_indblkshift
)
1915 if (size
== dn
->dn_datablksz
&& ibs
== 0)
1918 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1920 /* Check for any allocated blocks beyond the first */
1921 if (dn
->dn_maxblkid
!= 0)
1924 mutex_enter(&dn
->dn_dbufs_mtx
);
1925 for (db
= avl_first(&dn
->dn_dbufs
); db
!= NULL
;
1926 db
= AVL_NEXT(&dn
->dn_dbufs
, db
)) {
1927 if (db
->db_blkid
!= 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1928 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1929 mutex_exit(&dn
->dn_dbufs_mtx
);
1933 mutex_exit(&dn
->dn_dbufs_mtx
);
1935 if (ibs
&& dn
->dn_nlevels
!= 1)
1938 dnode_setdirty(dn
, tx
);
1939 if (size
!= dn
->dn_datablksz
) {
1940 /* resize the old block */
1941 err
= dbuf_hold_impl(dn
, 0, 0, TRUE
, FALSE
, FTAG
, &db
);
1943 dbuf_new_size(db
, size
, tx
);
1944 } else if (err
!= ENOENT
) {
1948 dnode_setdblksz(dn
, size
);
1949 dn
->dn_next_blksz
[tx
->tx_txg
& TXG_MASK
] = size
;
1951 dbuf_rele(db
, FTAG
);
1954 dn
->dn_indblkshift
= ibs
;
1955 dn
->dn_next_indblkshift
[tx
->tx_txg
& TXG_MASK
] = ibs
;
1958 rw_exit(&dn
->dn_struct_rwlock
);
1962 rw_exit(&dn
->dn_struct_rwlock
);
1963 return (SET_ERROR(ENOTSUP
));
1967 dnode_set_nlevels_impl(dnode_t
*dn
, int new_nlevels
, dmu_tx_t
*tx
)
1969 uint64_t txgoff
= tx
->tx_txg
& TXG_MASK
;
1970 int old_nlevels
= dn
->dn_nlevels
;
1973 dbuf_dirty_record_t
*new, *dr
, *dr_next
;
1975 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1977 ASSERT3U(new_nlevels
, >, dn
->dn_nlevels
);
1978 dn
->dn_nlevels
= new_nlevels
;
1980 ASSERT3U(new_nlevels
, >, dn
->dn_next_nlevels
[txgoff
]);
1981 dn
->dn_next_nlevels
[txgoff
] = new_nlevels
;
1983 /* dirty the left indirects */
1984 db
= dbuf_hold_level(dn
, old_nlevels
, 0, FTAG
);
1986 new = dbuf_dirty(db
, tx
);
1987 dbuf_rele(db
, FTAG
);
1989 /* transfer the dirty records to the new indirect */
1990 mutex_enter(&dn
->dn_mtx
);
1991 mutex_enter(&new->dt
.di
.dr_mtx
);
1992 list
= &dn
->dn_dirty_records
[txgoff
];
1993 for (dr
= list_head(list
); dr
; dr
= dr_next
) {
1994 dr_next
= list_next(&dn
->dn_dirty_records
[txgoff
], dr
);
1996 IMPLY(dr
->dr_dbuf
== NULL
, old_nlevels
== 1);
1997 if (dr
->dr_dbuf
== NULL
||
1998 (dr
->dr_dbuf
->db_level
== old_nlevels
- 1 &&
1999 dr
->dr_dbuf
->db_blkid
!= DMU_BONUS_BLKID
&&
2000 dr
->dr_dbuf
->db_blkid
!= DMU_SPILL_BLKID
)) {
2001 list_remove(&dn
->dn_dirty_records
[txgoff
], dr
);
2002 list_insert_tail(&new->dt
.di
.dr_children
, dr
);
2003 dr
->dr_parent
= new;
2006 mutex_exit(&new->dt
.di
.dr_mtx
);
2007 mutex_exit(&dn
->dn_mtx
);
2011 dnode_set_nlevels(dnode_t
*dn
, int nlevels
, dmu_tx_t
*tx
)
2015 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
2017 if (dn
->dn_nlevels
== nlevels
) {
2020 } else if (nlevels
< dn
->dn_nlevels
) {
2021 ret
= SET_ERROR(EINVAL
);
2025 dnode_set_nlevels_impl(dn
, nlevels
, tx
);
2028 rw_exit(&dn
->dn_struct_rwlock
);
2032 /* read-holding callers must not rely on the lock being continuously held */
2034 dnode_new_blkid(dnode_t
*dn
, uint64_t blkid
, dmu_tx_t
*tx
, boolean_t have_read
,
2037 int epbs
, new_nlevels
;
2040 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2043 RW_READ_HELD(&dn
->dn_struct_rwlock
) :
2044 RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
2047 * if we have a read-lock, check to see if we need to do any work
2048 * before upgrading to a write-lock.
2051 if (blkid
<= dn
->dn_maxblkid
)
2054 if (!rw_tryupgrade(&dn
->dn_struct_rwlock
)) {
2055 rw_exit(&dn
->dn_struct_rwlock
);
2056 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
2061 * Raw sends (indicated by the force flag) require that we take the
2062 * given blkid even if the value is lower than the current value.
2064 if (!force
&& blkid
<= dn
->dn_maxblkid
)
2068 * We use the (otherwise unused) top bit of dn_next_maxblkid[txgoff]
2069 * to indicate that this field is set. This allows us to set the
2070 * maxblkid to 0 on an existing object in dnode_sync().
2072 dn
->dn_maxblkid
= blkid
;
2073 dn
->dn_next_maxblkid
[tx
->tx_txg
& TXG_MASK
] =
2074 blkid
| DMU_NEXT_MAXBLKID_SET
;
2077 * Compute the number of levels necessary to support the new maxblkid.
2078 * Raw sends will ensure nlevels is set correctly for us.
2081 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2082 for (sz
= dn
->dn_nblkptr
;
2083 sz
<= blkid
&& sz
>= dn
->dn_nblkptr
; sz
<<= epbs
)
2086 ASSERT3U(new_nlevels
, <=, DN_MAX_LEVELS
);
2089 if (new_nlevels
> dn
->dn_nlevels
)
2090 dnode_set_nlevels_impl(dn
, new_nlevels
, tx
);
2092 ASSERT3U(dn
->dn_nlevels
, >=, new_nlevels
);
2097 rw_downgrade(&dn
->dn_struct_rwlock
);
2101 dnode_dirty_l1(dnode_t
*dn
, uint64_t l1blkid
, dmu_tx_t
*tx
)
2103 dmu_buf_impl_t
*db
= dbuf_hold_level(dn
, 1, l1blkid
, FTAG
);
2105 dmu_buf_will_dirty(&db
->db
, tx
);
2106 dbuf_rele(db
, FTAG
);
2111 * Dirty all the in-core level-1 dbufs in the range specified by start_blkid
2115 dnode_dirty_l1range(dnode_t
*dn
, uint64_t start_blkid
, uint64_t end_blkid
,
2118 dmu_buf_impl_t
*db_search
;
2122 db_search
= kmem_zalloc(sizeof (dmu_buf_impl_t
), KM_SLEEP
);
2124 mutex_enter(&dn
->dn_dbufs_mtx
);
2126 db_search
->db_level
= 1;
2127 db_search
->db_blkid
= start_blkid
+ 1;
2128 db_search
->db_state
= DB_SEARCH
;
2131 db
= avl_find(&dn
->dn_dbufs
, db_search
, &where
);
2133 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
2135 if (db
== NULL
|| db
->db_level
!= 1 ||
2136 db
->db_blkid
>= end_blkid
) {
2141 * Setup the next blkid we want to search for.
2143 db_search
->db_blkid
= db
->db_blkid
+ 1;
2144 ASSERT3U(db
->db_blkid
, >=, start_blkid
);
2147 * If the dbuf transitions to DB_EVICTING while we're trying
2148 * to dirty it, then we will be unable to discover it in
2149 * the dbuf hash table. This will result in a call to
2150 * dbuf_create() which needs to acquire the dn_dbufs_mtx
2151 * lock. To avoid a deadlock, we drop the lock before
2152 * dirtying the level-1 dbuf.
2154 mutex_exit(&dn
->dn_dbufs_mtx
);
2155 dnode_dirty_l1(dn
, db
->db_blkid
, tx
);
2156 mutex_enter(&dn
->dn_dbufs_mtx
);
2161 * Walk all the in-core level-1 dbufs and verify they have been dirtied.
2163 db_search
->db_level
= 1;
2164 db_search
->db_blkid
= start_blkid
+ 1;
2165 db_search
->db_state
= DB_SEARCH
;
2166 db
= avl_find(&dn
->dn_dbufs
, db_search
, &where
);
2168 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
2169 for (; db
!= NULL
; db
= AVL_NEXT(&dn
->dn_dbufs
, db
)) {
2170 if (db
->db_level
!= 1 || db
->db_blkid
>= end_blkid
)
2172 if (db
->db_state
!= DB_EVICTING
)
2173 ASSERT(db
->db_dirtycnt
> 0);
2176 kmem_free(db_search
, sizeof (dmu_buf_impl_t
));
2177 mutex_exit(&dn
->dn_dbufs_mtx
);
2181 dnode_set_dirtyctx(dnode_t
*dn
, dmu_tx_t
*tx
, const void *tag
)
2184 * Don't set dirtyctx to SYNC if we're just modifying this as we
2185 * initialize the objset.
2187 if (dn
->dn_dirtyctx
== DN_UNDIRTIED
) {
2188 dsl_dataset_t
*ds
= dn
->dn_objset
->os_dsl_dataset
;
2191 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, tag
);
2193 if (!BP_IS_HOLE(dn
->dn_objset
->os_rootbp
)) {
2194 if (dmu_tx_is_syncing(tx
))
2195 dn
->dn_dirtyctx
= DN_DIRTY_SYNC
;
2197 dn
->dn_dirtyctx
= DN_DIRTY_OPEN
;
2198 dn
->dn_dirtyctx_firstset
= tag
;
2201 rrw_exit(&ds
->ds_bp_rwlock
, tag
);
2207 dnode_partial_zero(dnode_t
*dn
, uint64_t off
, uint64_t blkoff
, uint64_t len
,
2213 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2214 res
= dbuf_hold_impl(dn
, 0, dbuf_whichblock(dn
, 0, off
), TRUE
, FALSE
,
2216 rw_exit(&dn
->dn_struct_rwlock
);
2218 db_lock_type_t dblt
;
2221 dblt
= dmu_buf_lock_parent(db
, RW_READER
, FTAG
);
2222 /* don't dirty if not on disk and not dirty */
2223 dirty
= !list_is_empty(&db
->db_dirty_records
) ||
2224 (db
->db_blkptr
&& !BP_IS_HOLE(db
->db_blkptr
));
2225 dmu_buf_unlock_parent(db
, dblt
, FTAG
);
2229 dmu_buf_will_dirty(&db
->db
, tx
);
2230 data
= db
->db
.db_data
;
2231 memset(data
+ blkoff
, 0, len
);
2233 dbuf_rele(db
, FTAG
);
2238 dnode_free_range(dnode_t
*dn
, uint64_t off
, uint64_t len
, dmu_tx_t
*tx
)
2240 uint64_t blkoff
, blkid
, nblks
;
2241 int blksz
, blkshift
, head
, tail
;
2245 blksz
= dn
->dn_datablksz
;
2246 blkshift
= dn
->dn_datablkshift
;
2247 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2249 if (len
== DMU_OBJECT_END
) {
2250 len
= UINT64_MAX
- off
;
2255 * First, block align the region to free:
2258 head
= P2NPHASE(off
, blksz
);
2259 blkoff
= P2PHASE(off
, blksz
);
2260 if ((off
>> blkshift
) > dn
->dn_maxblkid
)
2263 ASSERT(dn
->dn_maxblkid
== 0);
2264 if (off
== 0 && len
>= blksz
) {
2266 * Freeing the whole block; fast-track this request.
2270 if (dn
->dn_nlevels
> 1) {
2271 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
2272 dnode_dirty_l1(dn
, 0, tx
);
2273 rw_exit(&dn
->dn_struct_rwlock
);
2276 } else if (off
>= blksz
) {
2277 /* Freeing past end-of-data */
2280 /* Freeing part of the block. */
2282 ASSERT3U(head
, >, 0);
2286 /* zero out any partial block data at the start of the range */
2288 ASSERT3U(blkoff
+ head
, ==, blksz
);
2291 dnode_partial_zero(dn
, off
, blkoff
, head
, tx
);
2296 /* If the range was less than one block, we're done */
2300 /* If the remaining range is past end of file, we're done */
2301 if ((off
>> blkshift
) > dn
->dn_maxblkid
)
2304 ASSERT(ISP2(blksz
));
2308 tail
= P2PHASE(len
, blksz
);
2310 ASSERT0(P2PHASE(off
, blksz
));
2311 /* zero out any partial block data at the end of the range */
2315 dnode_partial_zero(dn
, off
+ len
, 0, tail
, tx
);
2319 /* If the range did not include a full block, we are done */
2323 ASSERT(IS_P2ALIGNED(off
, blksz
));
2324 ASSERT(trunc
|| IS_P2ALIGNED(len
, blksz
));
2325 blkid
= off
>> blkshift
;
2326 nblks
= len
>> blkshift
;
2331 * Dirty all the indirect blocks in this range. Note that only
2332 * the first and last indirect blocks can actually be written
2333 * (if they were partially freed) -- they must be dirtied, even if
2334 * they do not exist on disk yet. The interior blocks will
2335 * be freed by free_children(), so they will not actually be written.
2336 * Even though these interior blocks will not be written, we
2337 * dirty them for two reasons:
2339 * - It ensures that the indirect blocks remain in memory until
2340 * syncing context. (They have already been prefetched by
2341 * dmu_tx_hold_free(), so we don't have to worry about reading
2342 * them serially here.)
2344 * - The dirty space accounting will put pressure on the txg sync
2345 * mechanism to begin syncing, and to delay transactions if there
2346 * is a large amount of freeing. Even though these indirect
2347 * blocks will not be written, we could need to write the same
2348 * amount of space if we copy the freed BPs into deadlists.
2350 if (dn
->dn_nlevels
> 1) {
2351 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
2352 uint64_t first
, last
;
2354 first
= blkid
>> epbs
;
2355 dnode_dirty_l1(dn
, first
, tx
);
2357 last
= dn
->dn_maxblkid
>> epbs
;
2359 last
= (blkid
+ nblks
- 1) >> epbs
;
2361 dnode_dirty_l1(dn
, last
, tx
);
2363 dnode_dirty_l1range(dn
, first
, last
, tx
);
2365 int shift
= dn
->dn_datablkshift
+ dn
->dn_indblkshift
-
2367 for (uint64_t i
= first
+ 1; i
< last
; i
++) {
2369 * Set i to the blockid of the next non-hole
2370 * level-1 indirect block at or after i. Note
2371 * that dnode_next_offset() operates in terms of
2372 * level-0-equivalent bytes.
2374 uint64_t ibyte
= i
<< shift
;
2375 int err
= dnode_next_offset(dn
, DNODE_FIND_HAVELOCK
,
2382 * Normally we should not see an error, either
2383 * from dnode_next_offset() or dbuf_hold_level()
2384 * (except for ESRCH from dnode_next_offset).
2385 * If there is an i/o error, then when we read
2386 * this block in syncing context, it will use
2387 * ZIO_FLAG_MUSTSUCCEED, and thus hang/panic according
2388 * to the "failmode" property. dnode_next_offset()
2389 * doesn't have a flag to indicate MUSTSUCCEED.
2394 dnode_dirty_l1(dn
, i
, tx
);
2396 rw_exit(&dn
->dn_struct_rwlock
);
2401 * Add this range to the dnode range list.
2402 * We will finish up this free operation in the syncing phase.
2404 mutex_enter(&dn
->dn_mtx
);
2406 int txgoff
= tx
->tx_txg
& TXG_MASK
;
2407 if (dn
->dn_free_ranges
[txgoff
] == NULL
) {
2408 dn
->dn_free_ranges
[txgoff
] = range_tree_create(NULL
,
2409 RANGE_SEG64
, NULL
, 0, 0);
2411 range_tree_clear(dn
->dn_free_ranges
[txgoff
], blkid
, nblks
);
2412 range_tree_add(dn
->dn_free_ranges
[txgoff
], blkid
, nblks
);
2414 dprintf_dnode(dn
, "blkid=%llu nblks=%llu txg=%llu\n",
2415 (u_longlong_t
)blkid
, (u_longlong_t
)nblks
,
2416 (u_longlong_t
)tx
->tx_txg
);
2417 mutex_exit(&dn
->dn_mtx
);
2419 dbuf_free_range(dn
, blkid
, blkid
+ nblks
- 1, tx
);
2420 dnode_setdirty(dn
, tx
);
2424 dnode_spill_freed(dnode_t
*dn
)
2428 mutex_enter(&dn
->dn_mtx
);
2429 for (i
= 0; i
< TXG_SIZE
; i
++) {
2430 if (dn
->dn_rm_spillblk
[i
] == DN_KILL_SPILLBLK
)
2433 mutex_exit(&dn
->dn_mtx
);
2434 return (i
< TXG_SIZE
);
2437 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
2439 dnode_block_freed(dnode_t
*dn
, uint64_t blkid
)
2443 if (blkid
== DMU_BONUS_BLKID
)
2446 if (dn
->dn_free_txg
)
2449 if (blkid
== DMU_SPILL_BLKID
)
2450 return (dnode_spill_freed(dn
));
2452 mutex_enter(&dn
->dn_mtx
);
2453 for (i
= 0; i
< TXG_SIZE
; i
++) {
2454 if (dn
->dn_free_ranges
[i
] != NULL
&&
2455 range_tree_contains(dn
->dn_free_ranges
[i
], blkid
, 1))
2458 mutex_exit(&dn
->dn_mtx
);
2459 return (i
< TXG_SIZE
);
2462 /* call from syncing context when we actually write/free space for this dnode */
2464 dnode_diduse_space(dnode_t
*dn
, int64_t delta
)
2467 dprintf_dnode(dn
, "dn=%p dnp=%p used=%llu delta=%lld\n",
2469 (u_longlong_t
)dn
->dn_phys
->dn_used
,
2472 mutex_enter(&dn
->dn_mtx
);
2473 space
= DN_USED_BYTES(dn
->dn_phys
);
2475 ASSERT3U(space
+ delta
, >=, space
); /* no overflow */
2477 ASSERT3U(space
, >=, -delta
); /* no underflow */
2480 if (spa_version(dn
->dn_objset
->os_spa
) < SPA_VERSION_DNODE_BYTES
) {
2481 ASSERT((dn
->dn_phys
->dn_flags
& DNODE_FLAG_USED_BYTES
) == 0);
2482 ASSERT0(P2PHASE(space
, 1<<DEV_BSHIFT
));
2483 dn
->dn_phys
->dn_used
= space
>> DEV_BSHIFT
;
2485 dn
->dn_phys
->dn_used
= space
;
2486 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_USED_BYTES
;
2488 mutex_exit(&dn
->dn_mtx
);
2492 * Scans a block at the indicated "level" looking for a hole or data,
2493 * depending on 'flags'.
2495 * If level > 0, then we are scanning an indirect block looking at its
2496 * pointers. If level == 0, then we are looking at a block of dnodes.
2498 * If we don't find what we are looking for in the block, we return ESRCH.
2499 * Otherwise, return with *offset pointing to the beginning (if searching
2500 * forwards) or end (if searching backwards) of the range covered by the
2501 * block pointer we matched on (or dnode).
2503 * The basic search algorithm used below by dnode_next_offset() is to
2504 * use this function to search up the block tree (widen the search) until
2505 * we find something (i.e., we don't return ESRCH) and then search back
2506 * down the tree (narrow the search) until we reach our original search
2510 dnode_next_offset_level(dnode_t
*dn
, int flags
, uint64_t *offset
,
2511 int lvl
, uint64_t blkfill
, uint64_t txg
)
2513 dmu_buf_impl_t
*db
= NULL
;
2515 uint64_t epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2516 uint64_t epb
= 1ULL << epbs
;
2517 uint64_t minfill
, maxfill
;
2519 int i
, inc
, error
, span
;
2521 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2523 hole
= ((flags
& DNODE_FIND_HOLE
) != 0);
2524 inc
= (flags
& DNODE_FIND_BACKWARDS
) ? -1 : 1;
2525 ASSERT(txg
== 0 || !hole
);
2527 if (lvl
== dn
->dn_phys
->dn_nlevels
) {
2529 epb
= dn
->dn_phys
->dn_nblkptr
;
2530 data
= dn
->dn_phys
->dn_blkptr
;
2532 uint64_t blkid
= dbuf_whichblock(dn
, lvl
, *offset
);
2533 error
= dbuf_hold_impl(dn
, lvl
, blkid
, TRUE
, FALSE
, FTAG
, &db
);
2535 if (error
!= ENOENT
)
2540 * This can only happen when we are searching up
2541 * the block tree for data. We don't really need to
2542 * adjust the offset, as we will just end up looking
2543 * at the pointer to this block in its parent, and its
2544 * going to be unallocated, so we will skip over it.
2546 return (SET_ERROR(ESRCH
));
2548 error
= dbuf_read(db
, NULL
,
2549 DB_RF_CANFAIL
| DB_RF_HAVESTRUCT
|
2550 DB_RF_NO_DECRYPT
| DB_RF_NOPREFETCH
);
2552 dbuf_rele(db
, FTAG
);
2555 data
= db
->db
.db_data
;
2556 rw_enter(&db
->db_rwlock
, RW_READER
);
2559 if (db
!= NULL
&& txg
!= 0 && (db
->db_blkptr
== NULL
||
2560 db
->db_blkptr
->blk_birth
<= txg
||
2561 BP_IS_HOLE(db
->db_blkptr
))) {
2563 * This can only happen when we are searching up the tree
2564 * and these conditions mean that we need to keep climbing.
2566 error
= SET_ERROR(ESRCH
);
2567 } else if (lvl
== 0) {
2568 dnode_phys_t
*dnp
= data
;
2570 ASSERT(dn
->dn_type
== DMU_OT_DNODE
);
2571 ASSERT(!(flags
& DNODE_FIND_BACKWARDS
));
2573 for (i
= (*offset
>> DNODE_SHIFT
) & (blkfill
- 1);
2574 i
< blkfill
; i
+= dnp
[i
].dn_extra_slots
+ 1) {
2575 if ((dnp
[i
].dn_type
== DMU_OT_NONE
) == hole
)
2580 error
= SET_ERROR(ESRCH
);
2582 *offset
= (*offset
& ~(DNODE_BLOCK_SIZE
- 1)) +
2585 blkptr_t
*bp
= data
;
2586 uint64_t start
= *offset
;
2587 span
= (lvl
- 1) * epbs
+ dn
->dn_datablkshift
;
2589 maxfill
= blkfill
<< ((lvl
- 1) * epbs
);
2596 if (span
>= 8 * sizeof (*offset
)) {
2597 /* This only happens on the highest indirection level */
2598 ASSERT3U((lvl
- 1), ==, dn
->dn_phys
->dn_nlevels
- 1);
2601 *offset
= *offset
>> span
;
2604 for (i
= BF64_GET(*offset
, 0, epbs
);
2605 i
>= 0 && i
< epb
; i
+= inc
) {
2606 if (BP_GET_FILL(&bp
[i
]) >= minfill
&&
2607 BP_GET_FILL(&bp
[i
]) <= maxfill
&&
2608 (hole
|| bp
[i
].blk_birth
> txg
))
2610 if (inc
> 0 || *offset
> 0)
2614 if (span
>= 8 * sizeof (*offset
)) {
2617 *offset
= *offset
<< span
;
2621 /* traversing backwards; position offset at the end */
2622 if (span
< 8 * sizeof (*offset
))
2623 *offset
= MIN(*offset
+ (1ULL << span
) - 1,
2625 } else if (*offset
< start
) {
2628 if (i
< 0 || i
>= epb
)
2629 error
= SET_ERROR(ESRCH
);
2633 rw_exit(&db
->db_rwlock
);
2634 dbuf_rele(db
, FTAG
);
2641 * Find the next hole, data, or sparse region at or after *offset.
2642 * The value 'blkfill' tells us how many items we expect to find
2643 * in an L0 data block; this value is 1 for normal objects,
2644 * DNODES_PER_BLOCK for the meta dnode, and some fraction of
2645 * DNODES_PER_BLOCK when searching for sparse regions thereof.
2649 * dnode_next_offset(dn, flags, offset, 1, 1, 0);
2650 * Finds the next/previous hole/data in a file.
2651 * Used in dmu_offset_next().
2653 * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
2654 * Finds the next free/allocated dnode an objset's meta-dnode.
2655 * Only finds objects that have new contents since txg (ie.
2656 * bonus buffer changes and content removal are ignored).
2657 * Used in dmu_object_next().
2659 * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
2660 * Finds the next L2 meta-dnode bp that's at most 1/4 full.
2661 * Used in dmu_object_alloc().
2664 dnode_next_offset(dnode_t
*dn
, int flags
, uint64_t *offset
,
2665 int minlvl
, uint64_t blkfill
, uint64_t txg
)
2667 uint64_t initial_offset
= *offset
;
2671 if (!(flags
& DNODE_FIND_HAVELOCK
))
2672 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2674 if (dn
->dn_phys
->dn_nlevels
== 0) {
2675 error
= SET_ERROR(ESRCH
);
2679 if (dn
->dn_datablkshift
== 0) {
2680 if (*offset
< dn
->dn_datablksz
) {
2681 if (flags
& DNODE_FIND_HOLE
)
2682 *offset
= dn
->dn_datablksz
;
2684 error
= SET_ERROR(ESRCH
);
2689 maxlvl
= dn
->dn_phys
->dn_nlevels
;
2691 for (lvl
= minlvl
; lvl
<= maxlvl
; lvl
++) {
2692 error
= dnode_next_offset_level(dn
,
2693 flags
, offset
, lvl
, blkfill
, txg
);
2698 while (error
== 0 && --lvl
>= minlvl
) {
2699 error
= dnode_next_offset_level(dn
,
2700 flags
, offset
, lvl
, blkfill
, txg
);
2704 * There's always a "virtual hole" at the end of the object, even
2705 * if all BP's which physically exist are non-holes.
2707 if ((flags
& DNODE_FIND_HOLE
) && error
== ESRCH
&& txg
== 0 &&
2708 minlvl
== 1 && blkfill
== 1 && !(flags
& DNODE_FIND_BACKWARDS
)) {
2712 if (error
== 0 && (flags
& DNODE_FIND_BACKWARDS
?
2713 initial_offset
< *offset
: initial_offset
> *offset
))
2714 error
= SET_ERROR(ESRCH
);
2716 if (!(flags
& DNODE_FIND_HAVELOCK
))
2717 rw_exit(&dn
->dn_struct_rwlock
);
2722 #if defined(_KERNEL)
2723 EXPORT_SYMBOL(dnode_hold
);
2724 EXPORT_SYMBOL(dnode_rele
);
2725 EXPORT_SYMBOL(dnode_set_nlevels
);
2726 EXPORT_SYMBOL(dnode_set_blksz
);
2727 EXPORT_SYMBOL(dnode_free_range
);
2728 EXPORT_SYMBOL(dnode_evict_dbufs
);
2729 EXPORT_SYMBOL(dnode_evict_bonus
);
2732 ZFS_MODULE_PARAM(zfs
, zfs_
, default_bs
, INT
, ZMOD_RW
,
2733 "Default dnode block shift");
2734 ZFS_MODULE_PARAM(zfs
, zfs_
, default_ibs
, INT
, ZMOD_RW
,
2735 "Default dnode indirect block shift");