4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
24 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 #include <sys/zfs_context.h>
29 #include <sys/dnode.h>
31 #include <sys/dmu_impl.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/dmu_objset.h>
34 #include <sys/dsl_dir.h>
35 #include <sys/dsl_dataset.h>
38 #include <sys/dmu_zfetch.h>
39 #include <sys/range_tree.h>
40 #include <sys/trace_dnode.h>
42 dnode_stats_t dnode_stats
= {
43 { "dnode_hold_dbuf_hold", KSTAT_DATA_UINT64
},
44 { "dnode_hold_dbuf_read", KSTAT_DATA_UINT64
},
45 { "dnode_hold_alloc_hits", KSTAT_DATA_UINT64
},
46 { "dnode_hold_alloc_misses", KSTAT_DATA_UINT64
},
47 { "dnode_hold_alloc_interior", KSTAT_DATA_UINT64
},
48 { "dnode_hold_alloc_lock_retry", KSTAT_DATA_UINT64
},
49 { "dnode_hold_alloc_lock_misses", KSTAT_DATA_UINT64
},
50 { "dnode_hold_alloc_type_none", KSTAT_DATA_UINT64
},
51 { "dnode_hold_free_hits", KSTAT_DATA_UINT64
},
52 { "dnode_hold_free_misses", KSTAT_DATA_UINT64
},
53 { "dnode_hold_free_lock_misses", KSTAT_DATA_UINT64
},
54 { "dnode_hold_free_lock_retry", KSTAT_DATA_UINT64
},
55 { "dnode_hold_free_overflow", KSTAT_DATA_UINT64
},
56 { "dnode_hold_free_refcount", KSTAT_DATA_UINT64
},
57 { "dnode_hold_free_txg", KSTAT_DATA_UINT64
},
58 { "dnode_allocate", KSTAT_DATA_UINT64
},
59 { "dnode_reallocate", KSTAT_DATA_UINT64
},
60 { "dnode_buf_evict", KSTAT_DATA_UINT64
},
61 { "dnode_alloc_next_chunk", KSTAT_DATA_UINT64
},
62 { "dnode_alloc_race", KSTAT_DATA_UINT64
},
63 { "dnode_alloc_next_block", KSTAT_DATA_UINT64
},
64 { "dnode_move_invalid", KSTAT_DATA_UINT64
},
65 { "dnode_move_recheck1", KSTAT_DATA_UINT64
},
66 { "dnode_move_recheck2", KSTAT_DATA_UINT64
},
67 { "dnode_move_special", KSTAT_DATA_UINT64
},
68 { "dnode_move_handle", KSTAT_DATA_UINT64
},
69 { "dnode_move_rwlock", KSTAT_DATA_UINT64
},
70 { "dnode_move_active", KSTAT_DATA_UINT64
},
73 static kstat_t
*dnode_ksp
;
74 static kmem_cache_t
*dnode_cache
;
76 ASSERTV(static dnode_phys_t dnode_phys_zero
);
78 int zfs_default_bs
= SPA_MINBLOCKSHIFT
;
79 int zfs_default_ibs
= DN_MAX_INDBLKSHIFT
;
82 static kmem_cbrc_t
dnode_move(void *, void *, size_t, void *);
86 dbuf_compare(const void *x1
, const void *x2
)
88 const dmu_buf_impl_t
*d1
= x1
;
89 const dmu_buf_impl_t
*d2
= x2
;
91 int cmp
= AVL_CMP(d1
->db_level
, d2
->db_level
);
95 cmp
= AVL_CMP(d1
->db_blkid
, d2
->db_blkid
);
99 if (d1
->db_state
== DB_SEARCH
) {
100 ASSERT3S(d2
->db_state
, !=, DB_SEARCH
);
102 } else if (d2
->db_state
== DB_SEARCH
) {
103 ASSERT3S(d1
->db_state
, !=, DB_SEARCH
);
107 return (AVL_PCMP(d1
, d2
));
112 dnode_cons(void *arg
, void *unused
, int kmflag
)
117 rw_init(&dn
->dn_struct_rwlock
, NULL
, RW_NOLOCKDEP
, NULL
);
118 mutex_init(&dn
->dn_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
119 mutex_init(&dn
->dn_dbufs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
120 cv_init(&dn
->dn_notxholds
, NULL
, CV_DEFAULT
, NULL
);
123 * Every dbuf has a reference, and dropping a tracked reference is
124 * O(number of references), so don't track dn_holds.
126 refcount_create_untracked(&dn
->dn_holds
);
127 refcount_create(&dn
->dn_tx_holds
);
128 list_link_init(&dn
->dn_link
);
130 bzero(&dn
->dn_next_nblkptr
[0], sizeof (dn
->dn_next_nblkptr
));
131 bzero(&dn
->dn_next_nlevels
[0], sizeof (dn
->dn_next_nlevels
));
132 bzero(&dn
->dn_next_indblkshift
[0], sizeof (dn
->dn_next_indblkshift
));
133 bzero(&dn
->dn_next_bonustype
[0], sizeof (dn
->dn_next_bonustype
));
134 bzero(&dn
->dn_rm_spillblk
[0], sizeof (dn
->dn_rm_spillblk
));
135 bzero(&dn
->dn_next_bonuslen
[0], sizeof (dn
->dn_next_bonuslen
));
136 bzero(&dn
->dn_next_blksz
[0], sizeof (dn
->dn_next_blksz
));
138 for (i
= 0; i
< TXG_SIZE
; i
++) {
139 list_link_init(&dn
->dn_dirty_link
[i
]);
140 dn
->dn_free_ranges
[i
] = NULL
;
141 list_create(&dn
->dn_dirty_records
[i
],
142 sizeof (dbuf_dirty_record_t
),
143 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
146 dn
->dn_allocated_txg
= 0;
148 dn
->dn_assigned_txg
= 0;
150 dn
->dn_dirtyctx_firstset
= NULL
;
152 dn
->dn_have_spill
= B_FALSE
;
162 dn
->dn_dbufs_count
= 0;
163 avl_create(&dn
->dn_dbufs
, dbuf_compare
, sizeof (dmu_buf_impl_t
),
164 offsetof(dmu_buf_impl_t
, db_link
));
172 dnode_dest(void *arg
, void *unused
)
177 rw_destroy(&dn
->dn_struct_rwlock
);
178 mutex_destroy(&dn
->dn_mtx
);
179 mutex_destroy(&dn
->dn_dbufs_mtx
);
180 cv_destroy(&dn
->dn_notxholds
);
181 refcount_destroy(&dn
->dn_holds
);
182 refcount_destroy(&dn
->dn_tx_holds
);
183 ASSERT(!list_link_active(&dn
->dn_link
));
185 for (i
= 0; i
< TXG_SIZE
; i
++) {
186 ASSERT(!list_link_active(&dn
->dn_dirty_link
[i
]));
187 ASSERT3P(dn
->dn_free_ranges
[i
], ==, NULL
);
188 list_destroy(&dn
->dn_dirty_records
[i
]);
189 ASSERT0(dn
->dn_next_nblkptr
[i
]);
190 ASSERT0(dn
->dn_next_nlevels
[i
]);
191 ASSERT0(dn
->dn_next_indblkshift
[i
]);
192 ASSERT0(dn
->dn_next_bonustype
[i
]);
193 ASSERT0(dn
->dn_rm_spillblk
[i
]);
194 ASSERT0(dn
->dn_next_bonuslen
[i
]);
195 ASSERT0(dn
->dn_next_blksz
[i
]);
198 ASSERT0(dn
->dn_allocated_txg
);
199 ASSERT0(dn
->dn_free_txg
);
200 ASSERT0(dn
->dn_assigned_txg
);
201 ASSERT0(dn
->dn_dirtyctx
);
202 ASSERT3P(dn
->dn_dirtyctx_firstset
, ==, NULL
);
203 ASSERT3P(dn
->dn_bonus
, ==, NULL
);
204 ASSERT(!dn
->dn_have_spill
);
205 ASSERT3P(dn
->dn_zio
, ==, NULL
);
206 ASSERT0(dn
->dn_oldused
);
207 ASSERT0(dn
->dn_oldflags
);
208 ASSERT0(dn
->dn_olduid
);
209 ASSERT0(dn
->dn_oldgid
);
210 ASSERT0(dn
->dn_newuid
);
211 ASSERT0(dn
->dn_newgid
);
212 ASSERT0(dn
->dn_id_flags
);
214 ASSERT0(dn
->dn_dbufs_count
);
215 avl_destroy(&dn
->dn_dbufs
);
221 ASSERT(dnode_cache
== NULL
);
222 dnode_cache
= kmem_cache_create("dnode_t", sizeof (dnode_t
),
223 0, dnode_cons
, dnode_dest
, NULL
, NULL
, NULL
, 0);
224 kmem_cache_set_move(dnode_cache
, dnode_move
);
226 dnode_ksp
= kstat_create("zfs", 0, "dnodestats", "misc",
227 KSTAT_TYPE_NAMED
, sizeof (dnode_stats
) / sizeof (kstat_named_t
),
229 if (dnode_ksp
!= NULL
) {
230 dnode_ksp
->ks_data
= &dnode_stats
;
231 kstat_install(dnode_ksp
);
238 if (dnode_ksp
!= NULL
) {
239 kstat_delete(dnode_ksp
);
243 kmem_cache_destroy(dnode_cache
);
250 dnode_verify(dnode_t
*dn
)
252 int drop_struct_lock
= FALSE
;
255 ASSERT(dn
->dn_objset
);
256 ASSERT(dn
->dn_handle
->dnh_dnode
== dn
);
258 ASSERT(DMU_OT_IS_VALID(dn
->dn_phys
->dn_type
));
260 if (!(zfs_flags
& ZFS_DEBUG_DNODE_VERIFY
))
263 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
264 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
265 drop_struct_lock
= TRUE
;
267 if (dn
->dn_phys
->dn_type
!= DMU_OT_NONE
|| dn
->dn_allocated_txg
!= 0) {
269 int max_bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
270 ASSERT3U(dn
->dn_indblkshift
, <=, SPA_MAXBLOCKSHIFT
);
271 if (dn
->dn_datablkshift
) {
272 ASSERT3U(dn
->dn_datablkshift
, >=, SPA_MINBLOCKSHIFT
);
273 ASSERT3U(dn
->dn_datablkshift
, <=, SPA_MAXBLOCKSHIFT
);
274 ASSERT3U(1<<dn
->dn_datablkshift
, ==, dn
->dn_datablksz
);
276 ASSERT3U(dn
->dn_nlevels
, <=, 30);
277 ASSERT(DMU_OT_IS_VALID(dn
->dn_type
));
278 ASSERT3U(dn
->dn_nblkptr
, >=, 1);
279 ASSERT3U(dn
->dn_nblkptr
, <=, DN_MAX_NBLKPTR
);
280 ASSERT3U(dn
->dn_bonuslen
, <=, max_bonuslen
);
281 ASSERT3U(dn
->dn_datablksz
, ==,
282 dn
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
);
283 ASSERT3U(ISP2(dn
->dn_datablksz
), ==, dn
->dn_datablkshift
!= 0);
284 ASSERT3U((dn
->dn_nblkptr
- 1) * sizeof (blkptr_t
) +
285 dn
->dn_bonuslen
, <=, max_bonuslen
);
286 for (i
= 0; i
< TXG_SIZE
; i
++) {
287 ASSERT3U(dn
->dn_next_nlevels
[i
], <=, dn
->dn_nlevels
);
290 if (dn
->dn_phys
->dn_type
!= DMU_OT_NONE
)
291 ASSERT3U(dn
->dn_phys
->dn_nlevels
, <=, dn
->dn_nlevels
);
292 ASSERT(DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) || dn
->dn_dbuf
!= NULL
);
293 if (dn
->dn_dbuf
!= NULL
) {
294 ASSERT3P(dn
->dn_phys
, ==,
295 (dnode_phys_t
*)dn
->dn_dbuf
->db
.db_data
+
296 (dn
->dn_object
% (dn
->dn_dbuf
->db
.db_size
>> DNODE_SHIFT
)));
298 if (drop_struct_lock
)
299 rw_exit(&dn
->dn_struct_rwlock
);
304 dnode_byteswap(dnode_phys_t
*dnp
)
306 uint64_t *buf64
= (void*)&dnp
->dn_blkptr
;
309 if (dnp
->dn_type
== DMU_OT_NONE
) {
310 bzero(dnp
, sizeof (dnode_phys_t
));
314 dnp
->dn_datablkszsec
= BSWAP_16(dnp
->dn_datablkszsec
);
315 dnp
->dn_bonuslen
= BSWAP_16(dnp
->dn_bonuslen
);
316 dnp
->dn_extra_slots
= BSWAP_8(dnp
->dn_extra_slots
);
317 dnp
->dn_maxblkid
= BSWAP_64(dnp
->dn_maxblkid
);
318 dnp
->dn_used
= BSWAP_64(dnp
->dn_used
);
321 * dn_nblkptr is only one byte, so it's OK to read it in either
322 * byte order. We can't read dn_bouslen.
324 ASSERT(dnp
->dn_indblkshift
<= SPA_MAXBLOCKSHIFT
);
325 ASSERT(dnp
->dn_nblkptr
<= DN_MAX_NBLKPTR
);
326 for (i
= 0; i
< dnp
->dn_nblkptr
* sizeof (blkptr_t
)/8; i
++)
327 buf64
[i
] = BSWAP_64(buf64
[i
]);
330 * OK to check dn_bonuslen for zero, because it won't matter if
331 * we have the wrong byte order. This is necessary because the
332 * dnode dnode is smaller than a regular dnode.
334 if (dnp
->dn_bonuslen
!= 0) {
336 * Note that the bonus length calculated here may be
337 * longer than the actual bonus buffer. This is because
338 * we always put the bonus buffer after the last block
339 * pointer (instead of packing it against the end of the
342 int off
= (dnp
->dn_nblkptr
-1) * sizeof (blkptr_t
);
343 int slots
= dnp
->dn_extra_slots
+ 1;
344 size_t len
= DN_SLOTS_TO_BONUSLEN(slots
) - off
;
345 dmu_object_byteswap_t byteswap
;
346 ASSERT(DMU_OT_IS_VALID(dnp
->dn_bonustype
));
347 byteswap
= DMU_OT_BYTESWAP(dnp
->dn_bonustype
);
348 dmu_ot_byteswap
[byteswap
].ob_func(dnp
->dn_bonus
+ off
, len
);
351 /* Swap SPILL block if we have one */
352 if (dnp
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
)
353 byteswap_uint64_array(DN_SPILL_BLKPTR(dnp
), sizeof (blkptr_t
));
357 dnode_buf_byteswap(void *vbuf
, size_t size
)
361 ASSERT3U(sizeof (dnode_phys_t
), ==, (1<<DNODE_SHIFT
));
362 ASSERT((size
& (sizeof (dnode_phys_t
)-1)) == 0);
365 dnode_phys_t
*dnp
= (void *)(((char *)vbuf
) + i
);
369 if (dnp
->dn_type
!= DMU_OT_NONE
)
370 i
+= dnp
->dn_extra_slots
* DNODE_MIN_SIZE
;
375 dnode_setbonuslen(dnode_t
*dn
, int newsize
, dmu_tx_t
*tx
)
377 ASSERT3U(refcount_count(&dn
->dn_holds
), >=, 1);
379 dnode_setdirty(dn
, tx
);
380 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
381 ASSERT3U(newsize
, <=, DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
382 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
));
383 dn
->dn_bonuslen
= newsize
;
385 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = DN_ZERO_BONUSLEN
;
387 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonuslen
;
388 rw_exit(&dn
->dn_struct_rwlock
);
392 dnode_setbonus_type(dnode_t
*dn
, dmu_object_type_t newtype
, dmu_tx_t
*tx
)
394 ASSERT3U(refcount_count(&dn
->dn_holds
), >=, 1);
395 dnode_setdirty(dn
, tx
);
396 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
397 dn
->dn_bonustype
= newtype
;
398 dn
->dn_next_bonustype
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonustype
;
399 rw_exit(&dn
->dn_struct_rwlock
);
403 dnode_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
405 ASSERT3U(refcount_count(&dn
->dn_holds
), >=, 1);
406 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
407 dnode_setdirty(dn
, tx
);
408 dn
->dn_rm_spillblk
[tx
->tx_txg
&TXG_MASK
] = DN_KILL_SPILLBLK
;
409 dn
->dn_have_spill
= B_FALSE
;
413 dnode_setdblksz(dnode_t
*dn
, int size
)
415 ASSERT0(P2PHASE(size
, SPA_MINBLOCKSIZE
));
416 ASSERT3U(size
, <=, SPA_MAXBLOCKSIZE
);
417 ASSERT3U(size
, >=, SPA_MINBLOCKSIZE
);
418 ASSERT3U(size
>> SPA_MINBLOCKSHIFT
, <,
419 1<<(sizeof (dn
->dn_phys
->dn_datablkszsec
) * 8));
420 dn
->dn_datablksz
= size
;
421 dn
->dn_datablkszsec
= size
>> SPA_MINBLOCKSHIFT
;
422 dn
->dn_datablkshift
= ISP2(size
) ? highbit64(size
- 1) : 0;
426 dnode_create(objset_t
*os
, dnode_phys_t
*dnp
, dmu_buf_impl_t
*db
,
427 uint64_t object
, dnode_handle_t
*dnh
)
431 dn
= kmem_cache_alloc(dnode_cache
, KM_SLEEP
);
432 ASSERT(!POINTER_IS_VALID(dn
->dn_objset
));
436 * Defer setting dn_objset until the dnode is ready to be a candidate
437 * for the dnode_move() callback.
439 dn
->dn_object
= object
;
444 if (dnp
->dn_datablkszsec
) {
445 dnode_setdblksz(dn
, dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
);
447 dn
->dn_datablksz
= 0;
448 dn
->dn_datablkszsec
= 0;
449 dn
->dn_datablkshift
= 0;
451 dn
->dn_indblkshift
= dnp
->dn_indblkshift
;
452 dn
->dn_nlevels
= dnp
->dn_nlevels
;
453 dn
->dn_type
= dnp
->dn_type
;
454 dn
->dn_nblkptr
= dnp
->dn_nblkptr
;
455 dn
->dn_checksum
= dnp
->dn_checksum
;
456 dn
->dn_compress
= dnp
->dn_compress
;
457 dn
->dn_bonustype
= dnp
->dn_bonustype
;
458 dn
->dn_bonuslen
= dnp
->dn_bonuslen
;
459 dn
->dn_num_slots
= dnp
->dn_extra_slots
+ 1;
460 dn
->dn_maxblkid
= dnp
->dn_maxblkid
;
461 dn
->dn_have_spill
= ((dnp
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
) != 0);
464 dmu_zfetch_init(&dn
->dn_zfetch
, dn
);
466 ASSERT(DMU_OT_IS_VALID(dn
->dn_phys
->dn_type
));
467 ASSERT(zrl_is_locked(&dnh
->dnh_zrlock
));
468 ASSERT(!DN_SLOT_IS_PTR(dnh
->dnh_dnode
));
470 mutex_enter(&os
->os_lock
);
473 * Exclude special dnodes from os_dnodes so an empty os_dnodes
474 * signifies that the special dnodes have no references from
475 * their children (the entries in os_dnodes). This allows
476 * dnode_destroy() to easily determine if the last child has
477 * been removed and then complete eviction of the objset.
479 if (!DMU_OBJECT_IS_SPECIAL(object
))
480 list_insert_head(&os
->os_dnodes
, dn
);
484 * Everything else must be valid before assigning dn_objset
485 * makes the dnode eligible for dnode_move().
490 mutex_exit(&os
->os_lock
);
492 arc_space_consume(sizeof (dnode_t
), ARC_SPACE_DNODE
);
498 * Caller must be holding the dnode handle, which is released upon return.
501 dnode_destroy(dnode_t
*dn
)
503 objset_t
*os
= dn
->dn_objset
;
504 boolean_t complete_os_eviction
= B_FALSE
;
506 ASSERT((dn
->dn_id_flags
& DN_ID_NEW_EXIST
) == 0);
508 mutex_enter(&os
->os_lock
);
509 POINTER_INVALIDATE(&dn
->dn_objset
);
510 if (!DMU_OBJECT_IS_SPECIAL(dn
->dn_object
)) {
511 list_remove(&os
->os_dnodes
, dn
);
512 complete_os_eviction
=
513 list_is_empty(&os
->os_dnodes
) &&
514 list_link_active(&os
->os_evicting_node
);
516 mutex_exit(&os
->os_lock
);
518 /* the dnode can no longer move, so we can release the handle */
519 zrl_remove(&dn
->dn_handle
->dnh_zrlock
);
521 dn
->dn_allocated_txg
= 0;
523 dn
->dn_assigned_txg
= 0;
526 if (dn
->dn_dirtyctx_firstset
!= NULL
) {
527 kmem_free(dn
->dn_dirtyctx_firstset
, 1);
528 dn
->dn_dirtyctx_firstset
= NULL
;
530 if (dn
->dn_bonus
!= NULL
) {
531 mutex_enter(&dn
->dn_bonus
->db_mtx
);
532 dbuf_destroy(dn
->dn_bonus
);
537 dn
->dn_have_spill
= B_FALSE
;
546 dmu_zfetch_fini(&dn
->dn_zfetch
);
547 kmem_cache_free(dnode_cache
, dn
);
548 arc_space_return(sizeof (dnode_t
), ARC_SPACE_DNODE
);
550 if (complete_os_eviction
)
551 dmu_objset_evict_done(os
);
555 dnode_allocate(dnode_t
*dn
, dmu_object_type_t ot
, int blocksize
, int ibs
,
556 dmu_object_type_t bonustype
, int bonuslen
, int dn_slots
, dmu_tx_t
*tx
)
560 ASSERT3U(dn_slots
, >, 0);
561 ASSERT3U(dn_slots
<< DNODE_SHIFT
, <=,
562 spa_maxdnodesize(dmu_objset_spa(dn
->dn_objset
)));
563 ASSERT3U(blocksize
, <=,
564 spa_maxblocksize(dmu_objset_spa(dn
->dn_objset
)));
566 blocksize
= 1 << zfs_default_bs
;
568 blocksize
= P2ROUNDUP(blocksize
, SPA_MINBLOCKSIZE
);
571 ibs
= zfs_default_ibs
;
573 ibs
= MIN(MAX(ibs
, DN_MIN_INDBLKSHIFT
), DN_MAX_INDBLKSHIFT
);
575 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d dn_slots=%d\n",
576 dn
->dn_objset
, dn
->dn_object
, tx
->tx_txg
, blocksize
, ibs
, dn_slots
);
577 DNODE_STAT_BUMP(dnode_allocate
);
579 ASSERT(dn
->dn_type
== DMU_OT_NONE
);
580 ASSERT(bcmp(dn
->dn_phys
, &dnode_phys_zero
, sizeof (dnode_phys_t
)) == 0);
581 ASSERT(dn
->dn_phys
->dn_type
== DMU_OT_NONE
);
582 ASSERT(ot
!= DMU_OT_NONE
);
583 ASSERT(DMU_OT_IS_VALID(ot
));
584 ASSERT((bonustype
== DMU_OT_NONE
&& bonuslen
== 0) ||
585 (bonustype
== DMU_OT_SA
&& bonuslen
== 0) ||
586 (bonustype
!= DMU_OT_NONE
&& bonuslen
!= 0));
587 ASSERT(DMU_OT_IS_VALID(bonustype
));
588 ASSERT3U(bonuslen
, <=, DN_SLOTS_TO_BONUSLEN(dn_slots
));
589 ASSERT(dn
->dn_type
== DMU_OT_NONE
);
590 ASSERT0(dn
->dn_maxblkid
);
591 ASSERT0(dn
->dn_allocated_txg
);
592 ASSERT0(dn
->dn_assigned_txg
);
593 ASSERT(refcount_is_zero(&dn
->dn_tx_holds
));
594 ASSERT3U(refcount_count(&dn
->dn_holds
), <=, 1);
595 ASSERT(avl_is_empty(&dn
->dn_dbufs
));
597 for (i
= 0; i
< TXG_SIZE
; i
++) {
598 ASSERT0(dn
->dn_next_nblkptr
[i
]);
599 ASSERT0(dn
->dn_next_nlevels
[i
]);
600 ASSERT0(dn
->dn_next_indblkshift
[i
]);
601 ASSERT0(dn
->dn_next_bonuslen
[i
]);
602 ASSERT0(dn
->dn_next_bonustype
[i
]);
603 ASSERT0(dn
->dn_rm_spillblk
[i
]);
604 ASSERT0(dn
->dn_next_blksz
[i
]);
605 ASSERT(!list_link_active(&dn
->dn_dirty_link
[i
]));
606 ASSERT3P(list_head(&dn
->dn_dirty_records
[i
]), ==, NULL
);
607 ASSERT3P(dn
->dn_free_ranges
[i
], ==, NULL
);
611 dnode_setdblksz(dn
, blocksize
);
612 dn
->dn_indblkshift
= ibs
;
614 dn
->dn_num_slots
= dn_slots
;
615 if (bonustype
== DMU_OT_SA
) /* Maximize bonus space for SA */
618 dn
->dn_nblkptr
= MIN(DN_MAX_NBLKPTR
,
619 1 + ((DN_SLOTS_TO_BONUSLEN(dn_slots
) - bonuslen
) >>
623 dn
->dn_bonustype
= bonustype
;
624 dn
->dn_bonuslen
= bonuslen
;
625 dn
->dn_checksum
= ZIO_CHECKSUM_INHERIT
;
626 dn
->dn_compress
= ZIO_COMPRESS_INHERIT
;
630 if (dn
->dn_dirtyctx_firstset
) {
631 kmem_free(dn
->dn_dirtyctx_firstset
, 1);
632 dn
->dn_dirtyctx_firstset
= NULL
;
635 dn
->dn_allocated_txg
= tx
->tx_txg
;
638 dnode_setdirty(dn
, tx
);
639 dn
->dn_next_indblkshift
[tx
->tx_txg
& TXG_MASK
] = ibs
;
640 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonuslen
;
641 dn
->dn_next_bonustype
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonustype
;
642 dn
->dn_next_blksz
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_datablksz
;
646 dnode_reallocate(dnode_t
*dn
, dmu_object_type_t ot
, int blocksize
,
647 dmu_object_type_t bonustype
, int bonuslen
, int dn_slots
, dmu_tx_t
*tx
)
651 ASSERT3U(blocksize
, >=, SPA_MINBLOCKSIZE
);
652 ASSERT3U(blocksize
, <=,
653 spa_maxblocksize(dmu_objset_spa(dn
->dn_objset
)));
654 ASSERT0(blocksize
% SPA_MINBLOCKSIZE
);
655 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
|| dmu_tx_private_ok(tx
));
656 ASSERT(tx
->tx_txg
!= 0);
657 ASSERT((bonustype
== DMU_OT_NONE
&& bonuslen
== 0) ||
658 (bonustype
!= DMU_OT_NONE
&& bonuslen
!= 0) ||
659 (bonustype
== DMU_OT_SA
&& bonuslen
== 0));
660 ASSERT(DMU_OT_IS_VALID(bonustype
));
661 ASSERT3U(bonuslen
, <=,
662 DN_BONUS_SIZE(spa_maxdnodesize(dmu_objset_spa(dn
->dn_objset
))));
664 dn_slots
= dn_slots
> 0 ? dn_slots
: DNODE_MIN_SLOTS
;
665 DNODE_STAT_BUMP(dnode_reallocate
);
667 /* clean up any unreferenced dbufs */
668 dnode_evict_dbufs(dn
);
672 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
673 dnode_setdirty(dn
, tx
);
674 if (dn
->dn_datablksz
!= blocksize
) {
675 /* change blocksize */
676 ASSERT(dn
->dn_maxblkid
== 0 &&
677 (BP_IS_HOLE(&dn
->dn_phys
->dn_blkptr
[0]) ||
678 dnode_block_freed(dn
, 0)));
679 dnode_setdblksz(dn
, blocksize
);
680 dn
->dn_next_blksz
[tx
->tx_txg
&TXG_MASK
] = blocksize
;
682 if (dn
->dn_bonuslen
!= bonuslen
)
683 dn
->dn_next_bonuslen
[tx
->tx_txg
&TXG_MASK
] = bonuslen
;
685 if (bonustype
== DMU_OT_SA
) /* Maximize bonus space for SA */
688 nblkptr
= MIN(DN_MAX_NBLKPTR
,
689 1 + ((DN_SLOTS_TO_BONUSLEN(dn_slots
) - bonuslen
) >>
691 if (dn
->dn_bonustype
!= bonustype
)
692 dn
->dn_next_bonustype
[tx
->tx_txg
&TXG_MASK
] = bonustype
;
693 if (dn
->dn_nblkptr
!= nblkptr
)
694 dn
->dn_next_nblkptr
[tx
->tx_txg
&TXG_MASK
] = nblkptr
;
695 if (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
) {
696 dbuf_rm_spill(dn
, tx
);
697 dnode_rm_spill(dn
, tx
);
699 rw_exit(&dn
->dn_struct_rwlock
);
704 /* change bonus size and type */
705 mutex_enter(&dn
->dn_mtx
);
706 dn
->dn_bonustype
= bonustype
;
707 dn
->dn_bonuslen
= bonuslen
;
708 dn
->dn_num_slots
= dn_slots
;
709 dn
->dn_nblkptr
= nblkptr
;
710 dn
->dn_checksum
= ZIO_CHECKSUM_INHERIT
;
711 dn
->dn_compress
= ZIO_COMPRESS_INHERIT
;
712 ASSERT3U(dn
->dn_nblkptr
, <=, DN_MAX_NBLKPTR
);
714 /* fix up the bonus db_size */
716 dn
->dn_bonus
->db
.db_size
=
717 DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
718 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
719 ASSERT(dn
->dn_bonuslen
<= dn
->dn_bonus
->db
.db_size
);
722 dn
->dn_allocated_txg
= tx
->tx_txg
;
723 mutex_exit(&dn
->dn_mtx
);
728 dnode_move_impl(dnode_t
*odn
, dnode_t
*ndn
)
732 ASSERT(!RW_LOCK_HELD(&odn
->dn_struct_rwlock
));
733 ASSERT(MUTEX_NOT_HELD(&odn
->dn_mtx
));
734 ASSERT(MUTEX_NOT_HELD(&odn
->dn_dbufs_mtx
));
735 ASSERT(!RW_LOCK_HELD(&odn
->dn_zfetch
.zf_rwlock
));
738 ndn
->dn_objset
= odn
->dn_objset
;
739 ndn
->dn_object
= odn
->dn_object
;
740 ndn
->dn_dbuf
= odn
->dn_dbuf
;
741 ndn
->dn_handle
= odn
->dn_handle
;
742 ndn
->dn_phys
= odn
->dn_phys
;
743 ndn
->dn_type
= odn
->dn_type
;
744 ndn
->dn_bonuslen
= odn
->dn_bonuslen
;
745 ndn
->dn_bonustype
= odn
->dn_bonustype
;
746 ndn
->dn_nblkptr
= odn
->dn_nblkptr
;
747 ndn
->dn_checksum
= odn
->dn_checksum
;
748 ndn
->dn_compress
= odn
->dn_compress
;
749 ndn
->dn_nlevels
= odn
->dn_nlevels
;
750 ndn
->dn_indblkshift
= odn
->dn_indblkshift
;
751 ndn
->dn_datablkshift
= odn
->dn_datablkshift
;
752 ndn
->dn_datablkszsec
= odn
->dn_datablkszsec
;
753 ndn
->dn_datablksz
= odn
->dn_datablksz
;
754 ndn
->dn_maxblkid
= odn
->dn_maxblkid
;
755 ndn
->dn_num_slots
= odn
->dn_num_slots
;
756 bcopy(&odn
->dn_next_nblkptr
[0], &ndn
->dn_next_nblkptr
[0],
757 sizeof (odn
->dn_next_nblkptr
));
758 bcopy(&odn
->dn_next_nlevels
[0], &ndn
->dn_next_nlevels
[0],
759 sizeof (odn
->dn_next_nlevels
));
760 bcopy(&odn
->dn_next_indblkshift
[0], &ndn
->dn_next_indblkshift
[0],
761 sizeof (odn
->dn_next_indblkshift
));
762 bcopy(&odn
->dn_next_bonustype
[0], &ndn
->dn_next_bonustype
[0],
763 sizeof (odn
->dn_next_bonustype
));
764 bcopy(&odn
->dn_rm_spillblk
[0], &ndn
->dn_rm_spillblk
[0],
765 sizeof (odn
->dn_rm_spillblk
));
766 bcopy(&odn
->dn_next_bonuslen
[0], &ndn
->dn_next_bonuslen
[0],
767 sizeof (odn
->dn_next_bonuslen
));
768 bcopy(&odn
->dn_next_blksz
[0], &ndn
->dn_next_blksz
[0],
769 sizeof (odn
->dn_next_blksz
));
770 for (i
= 0; i
< TXG_SIZE
; i
++) {
771 list_move_tail(&ndn
->dn_dirty_records
[i
],
772 &odn
->dn_dirty_records
[i
]);
774 bcopy(&odn
->dn_free_ranges
[0], &ndn
->dn_free_ranges
[0],
775 sizeof (odn
->dn_free_ranges
));
776 ndn
->dn_allocated_txg
= odn
->dn_allocated_txg
;
777 ndn
->dn_free_txg
= odn
->dn_free_txg
;
778 ndn
->dn_assigned_txg
= odn
->dn_assigned_txg
;
779 ndn
->dn_dirtyctx
= odn
->dn_dirtyctx
;
780 ndn
->dn_dirtyctx_firstset
= odn
->dn_dirtyctx_firstset
;
781 ASSERT(refcount_count(&odn
->dn_tx_holds
) == 0);
782 refcount_transfer(&ndn
->dn_holds
, &odn
->dn_holds
);
783 ASSERT(avl_is_empty(&ndn
->dn_dbufs
));
784 avl_swap(&ndn
->dn_dbufs
, &odn
->dn_dbufs
);
785 ndn
->dn_dbufs_count
= odn
->dn_dbufs_count
;
786 ndn
->dn_bonus
= odn
->dn_bonus
;
787 ndn
->dn_have_spill
= odn
->dn_have_spill
;
788 ndn
->dn_zio
= odn
->dn_zio
;
789 ndn
->dn_oldused
= odn
->dn_oldused
;
790 ndn
->dn_oldflags
= odn
->dn_oldflags
;
791 ndn
->dn_olduid
= odn
->dn_olduid
;
792 ndn
->dn_oldgid
= odn
->dn_oldgid
;
793 ndn
->dn_newuid
= odn
->dn_newuid
;
794 ndn
->dn_newgid
= odn
->dn_newgid
;
795 ndn
->dn_id_flags
= odn
->dn_id_flags
;
796 dmu_zfetch_init(&ndn
->dn_zfetch
, NULL
);
797 list_move_tail(&ndn
->dn_zfetch
.zf_stream
, &odn
->dn_zfetch
.zf_stream
);
798 ndn
->dn_zfetch
.zf_dnode
= odn
->dn_zfetch
.zf_dnode
;
801 * Update back pointers. Updating the handle fixes the back pointer of
802 * every descendant dbuf as well as the bonus dbuf.
804 ASSERT(ndn
->dn_handle
->dnh_dnode
== odn
);
805 ndn
->dn_handle
->dnh_dnode
= ndn
;
806 if (ndn
->dn_zfetch
.zf_dnode
== odn
) {
807 ndn
->dn_zfetch
.zf_dnode
= ndn
;
811 * Invalidate the original dnode by clearing all of its back pointers.
814 odn
->dn_handle
= NULL
;
815 avl_create(&odn
->dn_dbufs
, dbuf_compare
, sizeof (dmu_buf_impl_t
),
816 offsetof(dmu_buf_impl_t
, db_link
));
817 odn
->dn_dbufs_count
= 0;
818 odn
->dn_bonus
= NULL
;
819 odn
->dn_zfetch
.zf_dnode
= NULL
;
822 * Set the low bit of the objset pointer to ensure that dnode_move()
823 * recognizes the dnode as invalid in any subsequent callback.
825 POINTER_INVALIDATE(&odn
->dn_objset
);
828 * Satisfy the destructor.
830 for (i
= 0; i
< TXG_SIZE
; i
++) {
831 list_create(&odn
->dn_dirty_records
[i
],
832 sizeof (dbuf_dirty_record_t
),
833 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
834 odn
->dn_free_ranges
[i
] = NULL
;
835 odn
->dn_next_nlevels
[i
] = 0;
836 odn
->dn_next_indblkshift
[i
] = 0;
837 odn
->dn_next_bonustype
[i
] = 0;
838 odn
->dn_rm_spillblk
[i
] = 0;
839 odn
->dn_next_bonuslen
[i
] = 0;
840 odn
->dn_next_blksz
[i
] = 0;
842 odn
->dn_allocated_txg
= 0;
843 odn
->dn_free_txg
= 0;
844 odn
->dn_assigned_txg
= 0;
845 odn
->dn_dirtyctx
= 0;
846 odn
->dn_dirtyctx_firstset
= NULL
;
847 odn
->dn_have_spill
= B_FALSE
;
850 odn
->dn_oldflags
= 0;
855 odn
->dn_id_flags
= 0;
861 odn
->dn_moved
= (uint8_t)-1;
866 dnode_move(void *buf
, void *newbuf
, size_t size
, void *arg
)
868 dnode_t
*odn
= buf
, *ndn
= newbuf
;
874 * The dnode is on the objset's list of known dnodes if the objset
875 * pointer is valid. We set the low bit of the objset pointer when
876 * freeing the dnode to invalidate it, and the memory patterns written
877 * by kmem (baddcafe and deadbeef) set at least one of the two low bits.
878 * A newly created dnode sets the objset pointer last of all to indicate
879 * that the dnode is known and in a valid state to be moved by this
883 if (!POINTER_IS_VALID(os
)) {
884 DNODE_STAT_BUMP(dnode_move_invalid
);
885 return (KMEM_CBRC_DONT_KNOW
);
889 * Ensure that the objset does not go away during the move.
891 rw_enter(&os_lock
, RW_WRITER
);
892 if (os
!= odn
->dn_objset
) {
894 DNODE_STAT_BUMP(dnode_move_recheck1
);
895 return (KMEM_CBRC_DONT_KNOW
);
899 * If the dnode is still valid, then so is the objset. We know that no
900 * valid objset can be freed while we hold os_lock, so we can safely
901 * ensure that the objset remains in use.
903 mutex_enter(&os
->os_lock
);
906 * Recheck the objset pointer in case the dnode was removed just before
907 * acquiring the lock.
909 if (os
!= odn
->dn_objset
) {
910 mutex_exit(&os
->os_lock
);
912 DNODE_STAT_BUMP(dnode_move_recheck2
);
913 return (KMEM_CBRC_DONT_KNOW
);
917 * At this point we know that as long as we hold os->os_lock, the dnode
918 * cannot be freed and fields within the dnode can be safely accessed.
919 * The objset listing this dnode cannot go away as long as this dnode is
923 if (DMU_OBJECT_IS_SPECIAL(odn
->dn_object
)) {
924 mutex_exit(&os
->os_lock
);
925 DNODE_STAT_BUMP(dnode_move_special
);
926 return (KMEM_CBRC_NO
);
928 ASSERT(odn
->dn_dbuf
!= NULL
); /* only "special" dnodes have no parent */
931 * Lock the dnode handle to prevent the dnode from obtaining any new
932 * holds. This also prevents the descendant dbufs and the bonus dbuf
933 * from accessing the dnode, so that we can discount their holds. The
934 * handle is safe to access because we know that while the dnode cannot
935 * go away, neither can its handle. Once we hold dnh_zrlock, we can
936 * safely move any dnode referenced only by dbufs.
938 if (!zrl_tryenter(&odn
->dn_handle
->dnh_zrlock
)) {
939 mutex_exit(&os
->os_lock
);
940 DNODE_STAT_BUMP(dnode_move_handle
);
941 return (KMEM_CBRC_LATER
);
945 * Ensure a consistent view of the dnode's holds and the dnode's dbufs.
946 * We need to guarantee that there is a hold for every dbuf in order to
947 * determine whether the dnode is actively referenced. Falsely matching
948 * a dbuf to an active hold would lead to an unsafe move. It's possible
949 * that a thread already having an active dnode hold is about to add a
950 * dbuf, and we can't compare hold and dbuf counts while the add is in
953 if (!rw_tryenter(&odn
->dn_struct_rwlock
, RW_WRITER
)) {
954 zrl_exit(&odn
->dn_handle
->dnh_zrlock
);
955 mutex_exit(&os
->os_lock
);
956 DNODE_STAT_BUMP(dnode_move_rwlock
);
957 return (KMEM_CBRC_LATER
);
961 * A dbuf may be removed (evicted) without an active dnode hold. In that
962 * case, the dbuf count is decremented under the handle lock before the
963 * dbuf's hold is released. This order ensures that if we count the hold
964 * after the dbuf is removed but before its hold is released, we will
965 * treat the unmatched hold as active and exit safely. If we count the
966 * hold before the dbuf is removed, the hold is discounted, and the
967 * removal is blocked until the move completes.
969 refcount
= refcount_count(&odn
->dn_holds
);
970 ASSERT(refcount
>= 0);
971 dbufs
= odn
->dn_dbufs_count
;
973 /* We can't have more dbufs than dnode holds. */
974 ASSERT3U(dbufs
, <=, refcount
);
975 DTRACE_PROBE3(dnode__move
, dnode_t
*, odn
, int64_t, refcount
,
978 if (refcount
> dbufs
) {
979 rw_exit(&odn
->dn_struct_rwlock
);
980 zrl_exit(&odn
->dn_handle
->dnh_zrlock
);
981 mutex_exit(&os
->os_lock
);
982 DNODE_STAT_BUMP(dnode_move_active
);
983 return (KMEM_CBRC_LATER
);
986 rw_exit(&odn
->dn_struct_rwlock
);
989 * At this point we know that anyone with a hold on the dnode is not
990 * actively referencing it. The dnode is known and in a valid state to
991 * move. We're holding the locks needed to execute the critical section.
993 dnode_move_impl(odn
, ndn
);
995 list_link_replace(&odn
->dn_link
, &ndn
->dn_link
);
996 /* If the dnode was safe to move, the refcount cannot have changed. */
997 ASSERT(refcount
== refcount_count(&ndn
->dn_holds
));
998 ASSERT(dbufs
== ndn
->dn_dbufs_count
);
999 zrl_exit(&ndn
->dn_handle
->dnh_zrlock
); /* handle has moved */
1000 mutex_exit(&os
->os_lock
);
1002 return (KMEM_CBRC_YES
);
1004 #endif /* _KERNEL */
1007 dnode_slots_hold(dnode_children_t
*children
, int idx
, int slots
)
1009 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1011 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1012 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1013 zrl_add(&dnh
->dnh_zrlock
);
1018 dnode_slots_rele(dnode_children_t
*children
, int idx
, int slots
)
1020 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1022 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1023 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1025 if (zrl_is_locked(&dnh
->dnh_zrlock
))
1026 zrl_exit(&dnh
->dnh_zrlock
);
1028 zrl_remove(&dnh
->dnh_zrlock
);
1033 dnode_slots_tryenter(dnode_children_t
*children
, int idx
, int slots
)
1035 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1037 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1038 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1040 if (!zrl_tryenter(&dnh
->dnh_zrlock
)) {
1041 for (int j
= idx
; j
< i
; j
++) {
1042 dnh
= &children
->dnc_children
[j
];
1043 zrl_exit(&dnh
->dnh_zrlock
);
1054 dnode_set_slots(dnode_children_t
*children
, int idx
, int slots
, void *ptr
)
1056 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1058 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1059 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1060 dnh
->dnh_dnode
= ptr
;
1065 dnode_check_slots(dnode_children_t
*children
, int idx
, int slots
, void *ptr
)
1067 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1069 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1070 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1071 if (dnh
->dnh_dnode
!= ptr
)
1079 dnode_special_close(dnode_handle_t
*dnh
)
1081 dnode_t
*dn
= dnh
->dnh_dnode
;
1084 * Wait for final references to the dnode to clear. This can
1085 * only happen if the arc is asynchronously evicting state that
1086 * has a hold on this dnode while we are trying to evict this
1089 while (refcount_count(&dn
->dn_holds
) > 0)
1091 ASSERT(dn
->dn_dbuf
== NULL
||
1092 dmu_buf_get_user(&dn
->dn_dbuf
->db
) == NULL
);
1093 zrl_add(&dnh
->dnh_zrlock
);
1094 dnode_destroy(dn
); /* implicit zrl_remove() */
1095 zrl_destroy(&dnh
->dnh_zrlock
);
1096 dnh
->dnh_dnode
= NULL
;
1100 dnode_special_open(objset_t
*os
, dnode_phys_t
*dnp
, uint64_t object
,
1101 dnode_handle_t
*dnh
)
1105 zrl_init(&dnh
->dnh_zrlock
);
1106 zrl_tryenter(&dnh
->dnh_zrlock
);
1108 dn
= dnode_create(os
, dnp
, NULL
, object
, dnh
);
1111 zrl_exit(&dnh
->dnh_zrlock
);
1115 dnode_buf_evict_async(void *dbu
)
1117 dnode_children_t
*dnc
= dbu
;
1119 DNODE_STAT_BUMP(dnode_buf_evict
);
1121 for (int i
= 0; i
< dnc
->dnc_count
; i
++) {
1122 dnode_handle_t
*dnh
= &dnc
->dnc_children
[i
];
1126 * The dnode handle lock guards against the dnode moving to
1127 * another valid address, so there is no need here to guard
1128 * against changes to or from NULL.
1130 if (!DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1131 zrl_destroy(&dnh
->dnh_zrlock
);
1132 dnh
->dnh_dnode
= DN_SLOT_UNINIT
;
1136 zrl_add(&dnh
->dnh_zrlock
);
1137 dn
= dnh
->dnh_dnode
;
1139 * If there are holds on this dnode, then there should
1140 * be holds on the dnode's containing dbuf as well; thus
1141 * it wouldn't be eligible for eviction and this function
1142 * would not have been called.
1144 ASSERT(refcount_is_zero(&dn
->dn_holds
));
1145 ASSERT(refcount_is_zero(&dn
->dn_tx_holds
));
1147 dnode_destroy(dn
); /* implicit zrl_remove() for first slot */
1148 zrl_destroy(&dnh
->dnh_zrlock
);
1149 dnh
->dnh_dnode
= DN_SLOT_UNINIT
;
1151 kmem_free(dnc
, sizeof (dnode_children_t
) +
1152 dnc
->dnc_count
* sizeof (dnode_handle_t
));
1156 * When the DNODE_MUST_BE_FREE flag is set, the "slots" parameter is used
1157 * to ensure the hole at the specified object offset is large enough to
1158 * hold the dnode being created. The slots parameter is also used to ensure
1159 * a dnode does not span multiple dnode blocks. In both of these cases, if
1160 * a failure occurs, ENOSPC is returned. Keep in mind, these failure cases
1161 * are only possible when using DNODE_MUST_BE_FREE.
1163 * If the DNODE_MUST_BE_ALLOCATED flag is set, "slots" must be 0.
1164 * dnode_hold_impl() will check if the requested dnode is already consumed
1165 * as an extra dnode slot by an large dnode, in which case it returns
1169 * EINVAL - Invalid object number or flags.
1170 * ENOSPC - Hole too small to fulfill "slots" request (DNODE_MUST_BE_FREE)
1171 * EEXIST - Refers to an allocated dnode (DNODE_MUST_BE_FREE)
1172 * - Refers to an interior dnode slot (DNODE_MUST_BE_ALLOCATED)
1173 * ENOENT - The requested dnode is not allocated (DNODE_MUST_BE_ALLOCATED)
1174 * EIO - I/O error when reading the meta dnode dbuf.
1176 * succeeds even for free dnodes.
1179 dnode_hold_impl(objset_t
*os
, uint64_t object
, int flag
, int slots
,
1180 void *tag
, dnode_t
**dnp
)
1183 int drop_struct_lock
= FALSE
;
1188 dnode_children_t
*dnc
;
1189 dnode_phys_t
*dn_block
;
1190 dnode_handle_t
*dnh
;
1192 ASSERT(!(flag
& DNODE_MUST_BE_ALLOCATED
) || (slots
== 0));
1193 ASSERT(!(flag
& DNODE_MUST_BE_FREE
) || (slots
> 0));
1196 * If you are holding the spa config lock as writer, you shouldn't
1197 * be asking the DMU to do *anything* unless it's the root pool
1198 * which may require us to read from the root filesystem while
1199 * holding some (not all) of the locks as writer.
1201 ASSERT(spa_config_held(os
->os_spa
, SCL_ALL
, RW_WRITER
) == 0 ||
1202 (spa_is_root(os
->os_spa
) &&
1203 spa_config_held(os
->os_spa
, SCL_STATE
, RW_WRITER
)));
1205 if (object
== DMU_USERUSED_OBJECT
|| object
== DMU_GROUPUSED_OBJECT
) {
1206 dn
= (object
== DMU_USERUSED_OBJECT
) ?
1207 DMU_USERUSED_DNODE(os
) : DMU_GROUPUSED_DNODE(os
);
1209 return (SET_ERROR(ENOENT
));
1211 if ((flag
& DNODE_MUST_BE_ALLOCATED
) && type
== DMU_OT_NONE
)
1212 return (SET_ERROR(ENOENT
));
1213 if ((flag
& DNODE_MUST_BE_FREE
) && type
!= DMU_OT_NONE
)
1214 return (SET_ERROR(EEXIST
));
1216 (void) refcount_add(&dn
->dn_holds
, tag
);
1221 if (object
== 0 || object
>= DN_MAX_OBJECT
)
1222 return (SET_ERROR(EINVAL
));
1224 mdn
= DMU_META_DNODE(os
);
1225 ASSERT(mdn
->dn_object
== DMU_META_DNODE_OBJECT
);
1229 if (!RW_WRITE_HELD(&mdn
->dn_struct_rwlock
)) {
1230 rw_enter(&mdn
->dn_struct_rwlock
, RW_READER
);
1231 drop_struct_lock
= TRUE
;
1234 blk
= dbuf_whichblock(mdn
, 0, object
* sizeof (dnode_phys_t
));
1236 db
= dbuf_hold(mdn
, blk
, FTAG
);
1237 if (drop_struct_lock
)
1238 rw_exit(&mdn
->dn_struct_rwlock
);
1240 DNODE_STAT_BUMP(dnode_hold_dbuf_hold
);
1241 return (SET_ERROR(EIO
));
1245 * We do not need to decrypt to read the dnode so it doesn't matter
1246 * if we get the encrypted or decrypted version.
1248 err
= dbuf_read(db
, NULL
, DB_RF_CANFAIL
| DB_RF_NO_DECRYPT
);
1250 DNODE_STAT_BUMP(dnode_hold_dbuf_read
);
1251 dbuf_rele(db
, FTAG
);
1255 ASSERT3U(db
->db
.db_size
, >=, 1<<DNODE_SHIFT
);
1256 epb
= db
->db
.db_size
>> DNODE_SHIFT
;
1258 idx
= object
& (epb
- 1);
1259 dn_block
= (dnode_phys_t
*)db
->db
.db_data
;
1261 ASSERT(DB_DNODE(db
)->dn_type
== DMU_OT_DNODE
);
1262 dnc
= dmu_buf_get_user(&db
->db
);
1265 dnode_children_t
*winner
;
1268 dnc
= kmem_zalloc(sizeof (dnode_children_t
) +
1269 epb
* sizeof (dnode_handle_t
), KM_SLEEP
);
1270 dnc
->dnc_count
= epb
;
1271 dnh
= &dnc
->dnc_children
[0];
1273 /* Initialize dnode slot status from dnode_phys_t */
1274 for (int i
= 0; i
< epb
; i
++) {
1275 zrl_init(&dnh
[i
].dnh_zrlock
);
1282 if (dn_block
[i
].dn_type
!= DMU_OT_NONE
) {
1283 int interior
= dn_block
[i
].dn_extra_slots
;
1285 dnode_set_slots(dnc
, i
, 1, DN_SLOT_ALLOCATED
);
1286 dnode_set_slots(dnc
, i
+ 1, interior
,
1290 dnh
[i
].dnh_dnode
= DN_SLOT_FREE
;
1295 dmu_buf_init_user(&dnc
->dnc_dbu
, NULL
,
1296 dnode_buf_evict_async
, NULL
);
1297 winner
= dmu_buf_set_user(&db
->db
, &dnc
->dnc_dbu
);
1298 if (winner
!= NULL
) {
1300 for (int i
= 0; i
< epb
; i
++)
1301 zrl_destroy(&dnh
[i
].dnh_zrlock
);
1303 kmem_free(dnc
, sizeof (dnode_children_t
) +
1304 epb
* sizeof (dnode_handle_t
));
1309 ASSERT(dnc
->dnc_count
== epb
);
1310 dn
= DN_SLOT_UNINIT
;
1312 if (flag
& DNODE_MUST_BE_ALLOCATED
) {
1315 while (dn
== DN_SLOT_UNINIT
) {
1316 dnode_slots_hold(dnc
, idx
, slots
);
1317 dnh
= &dnc
->dnc_children
[idx
];
1319 if (DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1320 dn
= dnh
->dnh_dnode
;
1322 } else if (dnh
->dnh_dnode
== DN_SLOT_INTERIOR
) {
1323 DNODE_STAT_BUMP(dnode_hold_alloc_interior
);
1324 dnode_slots_rele(dnc
, idx
, slots
);
1325 dbuf_rele(db
, FTAG
);
1326 return (SET_ERROR(EEXIST
));
1327 } else if (dnh
->dnh_dnode
!= DN_SLOT_ALLOCATED
) {
1328 DNODE_STAT_BUMP(dnode_hold_alloc_misses
);
1329 dnode_slots_rele(dnc
, idx
, slots
);
1330 dbuf_rele(db
, FTAG
);
1331 return (SET_ERROR(ENOENT
));
1334 dnode_slots_rele(dnc
, idx
, slots
);
1335 if (!dnode_slots_tryenter(dnc
, idx
, slots
)) {
1336 DNODE_STAT_BUMP(dnode_hold_alloc_lock_retry
);
1341 * Someone else won the race and called dnode_create()
1342 * after we checked DN_SLOT_IS_PTR() above but before
1343 * we acquired the lock.
1345 if (DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1346 DNODE_STAT_BUMP(dnode_hold_alloc_lock_misses
);
1347 dn
= dnh
->dnh_dnode
;
1349 dn
= dnode_create(os
, dn_block
+ idx
, db
,
1354 mutex_enter(&dn
->dn_mtx
);
1355 if (dn
->dn_type
== DMU_OT_NONE
) {
1356 DNODE_STAT_BUMP(dnode_hold_alloc_type_none
);
1357 mutex_exit(&dn
->dn_mtx
);
1358 dnode_slots_rele(dnc
, idx
, slots
);
1359 dbuf_rele(db
, FTAG
);
1360 return (SET_ERROR(ENOENT
));
1363 DNODE_STAT_BUMP(dnode_hold_alloc_hits
);
1364 } else if (flag
& DNODE_MUST_BE_FREE
) {
1366 if (idx
+ slots
- 1 >= DNODES_PER_BLOCK
) {
1367 DNODE_STAT_BUMP(dnode_hold_free_overflow
);
1368 dbuf_rele(db
, FTAG
);
1369 return (SET_ERROR(ENOSPC
));
1372 while (dn
== DN_SLOT_UNINIT
) {
1373 dnode_slots_hold(dnc
, idx
, slots
);
1375 if (!dnode_check_slots(dnc
, idx
, slots
, DN_SLOT_FREE
)) {
1376 DNODE_STAT_BUMP(dnode_hold_free_misses
);
1377 dnode_slots_rele(dnc
, idx
, slots
);
1378 dbuf_rele(db
, FTAG
);
1379 return (SET_ERROR(ENOSPC
));
1382 dnode_slots_rele(dnc
, idx
, slots
);
1383 if (!dnode_slots_tryenter(dnc
, idx
, slots
)) {
1384 DNODE_STAT_BUMP(dnode_hold_free_lock_retry
);
1388 if (!dnode_check_slots(dnc
, idx
, slots
, DN_SLOT_FREE
)) {
1389 DNODE_STAT_BUMP(dnode_hold_free_lock_misses
);
1390 dnode_slots_rele(dnc
, idx
, slots
);
1391 dbuf_rele(db
, FTAG
);
1392 return (SET_ERROR(ENOSPC
));
1395 dnh
= &dnc
->dnc_children
[idx
];
1396 dn
= dnode_create(os
, dn_block
+ idx
, db
, object
, dnh
);
1399 mutex_enter(&dn
->dn_mtx
);
1400 if (!refcount_is_zero(&dn
->dn_holds
)) {
1401 DNODE_STAT_BUMP(dnode_hold_free_refcount
);
1402 mutex_exit(&dn
->dn_mtx
);
1403 dnode_slots_rele(dnc
, idx
, slots
);
1404 dbuf_rele(db
, FTAG
);
1405 return (SET_ERROR(EEXIST
));
1408 dnode_set_slots(dnc
, idx
+ 1, slots
- 1, DN_SLOT_INTERIOR
);
1409 DNODE_STAT_BUMP(dnode_hold_free_hits
);
1411 dbuf_rele(db
, FTAG
);
1412 return (SET_ERROR(EINVAL
));
1415 if (dn
->dn_free_txg
) {
1416 DNODE_STAT_BUMP(dnode_hold_free_txg
);
1418 mutex_exit(&dn
->dn_mtx
);
1419 dnode_slots_rele(dnc
, idx
, slots
);
1420 dbuf_rele(db
, FTAG
);
1421 return (SET_ERROR(type
== DMU_OT_NONE
? ENOENT
: EEXIST
));
1424 if (refcount_add(&dn
->dn_holds
, tag
) == 1)
1425 dbuf_add_ref(db
, dnh
);
1427 mutex_exit(&dn
->dn_mtx
);
1429 /* Now we can rely on the hold to prevent the dnode from moving. */
1430 dnode_slots_rele(dnc
, idx
, slots
);
1433 ASSERT3P(dn
->dn_dbuf
, ==, db
);
1434 ASSERT3U(dn
->dn_object
, ==, object
);
1435 dbuf_rele(db
, FTAG
);
1442 * Return held dnode if the object is allocated, NULL if not.
1445 dnode_hold(objset_t
*os
, uint64_t object
, void *tag
, dnode_t
**dnp
)
1447 return (dnode_hold_impl(os
, object
, DNODE_MUST_BE_ALLOCATED
, 0, tag
,
1452 * Can only add a reference if there is already at least one
1453 * reference on the dnode. Returns FALSE if unable to add a
1457 dnode_add_ref(dnode_t
*dn
, void *tag
)
1459 mutex_enter(&dn
->dn_mtx
);
1460 if (refcount_is_zero(&dn
->dn_holds
)) {
1461 mutex_exit(&dn
->dn_mtx
);
1464 VERIFY(1 < refcount_add(&dn
->dn_holds
, tag
));
1465 mutex_exit(&dn
->dn_mtx
);
1470 dnode_rele(dnode_t
*dn
, void *tag
)
1472 mutex_enter(&dn
->dn_mtx
);
1473 dnode_rele_and_unlock(dn
, tag
);
1477 dnode_rele_and_unlock(dnode_t
*dn
, void *tag
)
1480 /* Get while the hold prevents the dnode from moving. */
1481 dmu_buf_impl_t
*db
= dn
->dn_dbuf
;
1482 dnode_handle_t
*dnh
= dn
->dn_handle
;
1484 refs
= refcount_remove(&dn
->dn_holds
, tag
);
1485 mutex_exit(&dn
->dn_mtx
);
1488 * It's unsafe to release the last hold on a dnode by dnode_rele() or
1489 * indirectly by dbuf_rele() while relying on the dnode handle to
1490 * prevent the dnode from moving, since releasing the last hold could
1491 * result in the dnode's parent dbuf evicting its dnode handles. For
1492 * that reason anyone calling dnode_rele() or dbuf_rele() without some
1493 * other direct or indirect hold on the dnode must first drop the dnode
1496 ASSERT(refs
> 0 || dnh
->dnh_zrlock
.zr_owner
!= curthread
);
1498 /* NOTE: the DNODE_DNODE does not have a dn_dbuf */
1499 if (refs
== 0 && db
!= NULL
) {
1501 * Another thread could add a hold to the dnode handle in
1502 * dnode_hold_impl() while holding the parent dbuf. Since the
1503 * hold on the parent dbuf prevents the handle from being
1504 * destroyed, the hold on the handle is OK. We can't yet assert
1505 * that the handle has zero references, but that will be
1506 * asserted anyway when the handle gets destroyed.
1513 dnode_setdirty(dnode_t
*dn
, dmu_tx_t
*tx
)
1515 objset_t
*os
= dn
->dn_objset
;
1516 uint64_t txg
= tx
->tx_txg
;
1518 if (DMU_OBJECT_IS_SPECIAL(dn
->dn_object
)) {
1519 dsl_dataset_dirty(os
->os_dsl_dataset
, tx
);
1526 mutex_enter(&dn
->dn_mtx
);
1527 ASSERT(dn
->dn_phys
->dn_type
|| dn
->dn_allocated_txg
);
1528 ASSERT(dn
->dn_free_txg
== 0 || dn
->dn_free_txg
>= txg
);
1529 mutex_exit(&dn
->dn_mtx
);
1533 * Determine old uid/gid when necessary
1535 dmu_objset_userquota_get_ids(dn
, B_TRUE
, tx
);
1537 multilist_t
*dirtylist
= os
->os_dirty_dnodes
[txg
& TXG_MASK
];
1538 multilist_sublist_t
*mls
= multilist_sublist_lock_obj(dirtylist
, dn
);
1541 * If we are already marked dirty, we're done.
1543 if (list_link_active(&dn
->dn_dirty_link
[txg
& TXG_MASK
])) {
1544 multilist_sublist_unlock(mls
);
1548 ASSERT(!refcount_is_zero(&dn
->dn_holds
) ||
1549 !avl_is_empty(&dn
->dn_dbufs
));
1550 ASSERT(dn
->dn_datablksz
!= 0);
1551 ASSERT0(dn
->dn_next_bonuslen
[txg
&TXG_MASK
]);
1552 ASSERT0(dn
->dn_next_blksz
[txg
&TXG_MASK
]);
1553 ASSERT0(dn
->dn_next_bonustype
[txg
&TXG_MASK
]);
1555 dprintf_ds(os
->os_dsl_dataset
, "obj=%llu txg=%llu\n",
1556 dn
->dn_object
, txg
);
1558 multilist_sublist_insert_head(mls
, dn
);
1560 multilist_sublist_unlock(mls
);
1563 * The dnode maintains a hold on its containing dbuf as
1564 * long as there are holds on it. Each instantiated child
1565 * dbuf maintains a hold on the dnode. When the last child
1566 * drops its hold, the dnode will drop its hold on the
1567 * containing dbuf. We add a "dirty hold" here so that the
1568 * dnode will hang around after we finish processing its
1571 VERIFY(dnode_add_ref(dn
, (void *)(uintptr_t)tx
->tx_txg
));
1573 (void) dbuf_dirty(dn
->dn_dbuf
, tx
);
1575 dsl_dataset_dirty(os
->os_dsl_dataset
, tx
);
1579 dnode_free(dnode_t
*dn
, dmu_tx_t
*tx
)
1581 mutex_enter(&dn
->dn_mtx
);
1582 if (dn
->dn_type
== DMU_OT_NONE
|| dn
->dn_free_txg
) {
1583 mutex_exit(&dn
->dn_mtx
);
1586 dn
->dn_free_txg
= tx
->tx_txg
;
1587 mutex_exit(&dn
->dn_mtx
);
1589 dnode_setdirty(dn
, tx
);
1593 * Try to change the block size for the indicated dnode. This can only
1594 * succeed if there are no blocks allocated or dirty beyond first block
1597 dnode_set_blksz(dnode_t
*dn
, uint64_t size
, int ibs
, dmu_tx_t
*tx
)
1602 ASSERT3U(size
, <=, spa_maxblocksize(dmu_objset_spa(dn
->dn_objset
)));
1604 size
= SPA_MINBLOCKSIZE
;
1606 size
= P2ROUNDUP(size
, SPA_MINBLOCKSIZE
);
1608 if (ibs
== dn
->dn_indblkshift
)
1611 if (size
>> SPA_MINBLOCKSHIFT
== dn
->dn_datablkszsec
&& ibs
== 0)
1614 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1616 /* Check for any allocated blocks beyond the first */
1617 if (dn
->dn_maxblkid
!= 0)
1620 mutex_enter(&dn
->dn_dbufs_mtx
);
1621 for (db
= avl_first(&dn
->dn_dbufs
); db
!= NULL
;
1622 db
= AVL_NEXT(&dn
->dn_dbufs
, db
)) {
1623 if (db
->db_blkid
!= 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1624 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1625 mutex_exit(&dn
->dn_dbufs_mtx
);
1629 mutex_exit(&dn
->dn_dbufs_mtx
);
1631 if (ibs
&& dn
->dn_nlevels
!= 1)
1634 /* resize the old block */
1635 err
= dbuf_hold_impl(dn
, 0, 0, TRUE
, FALSE
, FTAG
, &db
);
1637 dbuf_new_size(db
, size
, tx
);
1638 else if (err
!= ENOENT
)
1641 dnode_setdblksz(dn
, size
);
1642 dnode_setdirty(dn
, tx
);
1643 dn
->dn_next_blksz
[tx
->tx_txg
&TXG_MASK
] = size
;
1645 dn
->dn_indblkshift
= ibs
;
1646 dn
->dn_next_indblkshift
[tx
->tx_txg
&TXG_MASK
] = ibs
;
1648 /* rele after we have fixed the blocksize in the dnode */
1650 dbuf_rele(db
, FTAG
);
1652 rw_exit(&dn
->dn_struct_rwlock
);
1656 rw_exit(&dn
->dn_struct_rwlock
);
1657 return (SET_ERROR(ENOTSUP
));
1661 dnode_set_nlevels_impl(dnode_t
*dn
, int new_nlevels
, dmu_tx_t
*tx
)
1663 uint64_t txgoff
= tx
->tx_txg
& TXG_MASK
;
1664 int old_nlevels
= dn
->dn_nlevels
;
1667 dbuf_dirty_record_t
*new, *dr
, *dr_next
;
1669 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1671 dn
->dn_nlevels
= new_nlevels
;
1673 ASSERT3U(new_nlevels
, >, dn
->dn_next_nlevels
[txgoff
]);
1674 dn
->dn_next_nlevels
[txgoff
] = new_nlevels
;
1676 /* dirty the left indirects */
1677 db
= dbuf_hold_level(dn
, old_nlevels
, 0, FTAG
);
1679 new = dbuf_dirty(db
, tx
);
1680 dbuf_rele(db
, FTAG
);
1682 /* transfer the dirty records to the new indirect */
1683 mutex_enter(&dn
->dn_mtx
);
1684 mutex_enter(&new->dt
.di
.dr_mtx
);
1685 list
= &dn
->dn_dirty_records
[txgoff
];
1686 for (dr
= list_head(list
); dr
; dr
= dr_next
) {
1687 dr_next
= list_next(&dn
->dn_dirty_records
[txgoff
], dr
);
1688 if (dr
->dr_dbuf
->db_level
!= new_nlevels
-1 &&
1689 dr
->dr_dbuf
->db_blkid
!= DMU_BONUS_BLKID
&&
1690 dr
->dr_dbuf
->db_blkid
!= DMU_SPILL_BLKID
) {
1691 ASSERT(dr
->dr_dbuf
->db_level
== old_nlevels
-1);
1692 list_remove(&dn
->dn_dirty_records
[txgoff
], dr
);
1693 list_insert_tail(&new->dt
.di
.dr_children
, dr
);
1694 dr
->dr_parent
= new;
1697 mutex_exit(&new->dt
.di
.dr_mtx
);
1698 mutex_exit(&dn
->dn_mtx
);
1702 dnode_set_nlevels(dnode_t
*dn
, int nlevels
, dmu_tx_t
*tx
)
1706 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1708 if (dn
->dn_nlevels
== nlevels
) {
1711 } else if (nlevels
< dn
->dn_nlevels
) {
1712 ret
= SET_ERROR(EINVAL
);
1716 dnode_set_nlevels_impl(dn
, nlevels
, tx
);
1719 rw_exit(&dn
->dn_struct_rwlock
);
1723 /* read-holding callers must not rely on the lock being continuously held */
1725 dnode_new_blkid(dnode_t
*dn
, uint64_t blkid
, dmu_tx_t
*tx
, boolean_t have_read
)
1727 int epbs
, new_nlevels
;
1730 ASSERT(blkid
!= DMU_BONUS_BLKID
);
1733 RW_READ_HELD(&dn
->dn_struct_rwlock
) :
1734 RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1737 * if we have a read-lock, check to see if we need to do any work
1738 * before upgrading to a write-lock.
1741 if (blkid
<= dn
->dn_maxblkid
)
1744 if (!rw_tryupgrade(&dn
->dn_struct_rwlock
)) {
1745 rw_exit(&dn
->dn_struct_rwlock
);
1746 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1750 if (blkid
<= dn
->dn_maxblkid
)
1753 dn
->dn_maxblkid
= blkid
;
1756 * Compute the number of levels necessary to support the new maxblkid.
1759 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1760 for (sz
= dn
->dn_nblkptr
;
1761 sz
<= blkid
&& sz
>= dn
->dn_nblkptr
; sz
<<= epbs
)
1764 ASSERT3U(new_nlevels
, <=, DN_MAX_LEVELS
);
1766 if (new_nlevels
> dn
->dn_nlevels
)
1767 dnode_set_nlevels_impl(dn
, new_nlevels
, tx
);
1771 rw_downgrade(&dn
->dn_struct_rwlock
);
1775 dnode_dirty_l1(dnode_t
*dn
, uint64_t l1blkid
, dmu_tx_t
*tx
)
1777 dmu_buf_impl_t
*db
= dbuf_hold_level(dn
, 1, l1blkid
, FTAG
);
1779 dmu_buf_will_dirty(&db
->db
, tx
);
1780 dbuf_rele(db
, FTAG
);
1785 dnode_free_range(dnode_t
*dn
, uint64_t off
, uint64_t len
, dmu_tx_t
*tx
)
1788 uint64_t blkoff
, blkid
, nblks
;
1789 int blksz
, blkshift
, head
, tail
;
1793 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1794 blksz
= dn
->dn_datablksz
;
1795 blkshift
= dn
->dn_datablkshift
;
1796 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1798 if (len
== DMU_OBJECT_END
) {
1799 len
= UINT64_MAX
- off
;
1804 * First, block align the region to free:
1807 head
= P2NPHASE(off
, blksz
);
1808 blkoff
= P2PHASE(off
, blksz
);
1809 if ((off
>> blkshift
) > dn
->dn_maxblkid
)
1812 ASSERT(dn
->dn_maxblkid
== 0);
1813 if (off
== 0 && len
>= blksz
) {
1815 * Freeing the whole block; fast-track this request.
1816 * Note that we won't dirty any indirect blocks,
1817 * which is fine because we will be freeing the entire
1818 * file and thus all indirect blocks will be freed
1819 * by free_children().
1824 } else if (off
>= blksz
) {
1825 /* Freeing past end-of-data */
1828 /* Freeing part of the block. */
1830 ASSERT3U(head
, >, 0);
1834 /* zero out any partial block data at the start of the range */
1836 ASSERT3U(blkoff
+ head
, ==, blksz
);
1839 if (dbuf_hold_impl(dn
, 0, dbuf_whichblock(dn
, 0, off
),
1840 TRUE
, FALSE
, FTAG
, &db
) == 0) {
1843 /* don't dirty if it isn't on disk and isn't dirty */
1844 if (db
->db_last_dirty
||
1845 (db
->db_blkptr
&& !BP_IS_HOLE(db
->db_blkptr
))) {
1846 rw_exit(&dn
->dn_struct_rwlock
);
1847 dmu_buf_will_dirty(&db
->db
, tx
);
1848 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1849 data
= db
->db
.db_data
;
1850 bzero(data
+ blkoff
, head
);
1852 dbuf_rele(db
, FTAG
);
1858 /* If the range was less than one block, we're done */
1862 /* If the remaining range is past end of file, we're done */
1863 if ((off
>> blkshift
) > dn
->dn_maxblkid
)
1866 ASSERT(ISP2(blksz
));
1870 tail
= P2PHASE(len
, blksz
);
1872 ASSERT0(P2PHASE(off
, blksz
));
1873 /* zero out any partial block data at the end of the range */
1877 if (dbuf_hold_impl(dn
, 0, dbuf_whichblock(dn
, 0, off
+len
),
1878 TRUE
, FALSE
, FTAG
, &db
) == 0) {
1879 /* don't dirty if not on disk and not dirty */
1880 if (db
->db_last_dirty
||
1881 (db
->db_blkptr
&& !BP_IS_HOLE(db
->db_blkptr
))) {
1882 rw_exit(&dn
->dn_struct_rwlock
);
1883 dmu_buf_will_dirty(&db
->db
, tx
);
1884 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1885 bzero(db
->db
.db_data
, tail
);
1887 dbuf_rele(db
, FTAG
);
1892 /* If the range did not include a full block, we are done */
1896 ASSERT(IS_P2ALIGNED(off
, blksz
));
1897 ASSERT(trunc
|| IS_P2ALIGNED(len
, blksz
));
1898 blkid
= off
>> blkshift
;
1899 nblks
= len
>> blkshift
;
1904 * Dirty all the indirect blocks in this range. Note that only
1905 * the first and last indirect blocks can actually be written
1906 * (if they were partially freed) -- they must be dirtied, even if
1907 * they do not exist on disk yet. The interior blocks will
1908 * be freed by free_children(), so they will not actually be written.
1909 * Even though these interior blocks will not be written, we
1910 * dirty them for two reasons:
1912 * - It ensures that the indirect blocks remain in memory until
1913 * syncing context. (They have already been prefetched by
1914 * dmu_tx_hold_free(), so we don't have to worry about reading
1915 * them serially here.)
1917 * - The dirty space accounting will put pressure on the txg sync
1918 * mechanism to begin syncing, and to delay transactions if there
1919 * is a large amount of freeing. Even though these indirect
1920 * blocks will not be written, we could need to write the same
1921 * amount of space if we copy the freed BPs into deadlists.
1923 if (dn
->dn_nlevels
> 1) {
1924 uint64_t first
, last
;
1926 first
= blkid
>> epbs
;
1927 dnode_dirty_l1(dn
, first
, tx
);
1929 last
= dn
->dn_maxblkid
>> epbs
;
1931 last
= (blkid
+ nblks
- 1) >> epbs
;
1933 dnode_dirty_l1(dn
, last
, tx
);
1935 int shift
= dn
->dn_datablkshift
+ dn
->dn_indblkshift
-
1937 for (uint64_t i
= first
+ 1; i
< last
; i
++) {
1939 * Set i to the blockid of the next non-hole
1940 * level-1 indirect block at or after i. Note
1941 * that dnode_next_offset() operates in terms of
1942 * level-0-equivalent bytes.
1944 uint64_t ibyte
= i
<< shift
;
1945 int err
= dnode_next_offset(dn
, DNODE_FIND_HAVELOCK
,
1952 * Normally we should not see an error, either
1953 * from dnode_next_offset() or dbuf_hold_level()
1954 * (except for ESRCH from dnode_next_offset).
1955 * If there is an i/o error, then when we read
1956 * this block in syncing context, it will use
1957 * ZIO_FLAG_MUSTSUCCEED, and thus hang/panic according
1958 * to the "failmode" property. dnode_next_offset()
1959 * doesn't have a flag to indicate MUSTSUCCEED.
1964 dnode_dirty_l1(dn
, i
, tx
);
1970 * Add this range to the dnode range list.
1971 * We will finish up this free operation in the syncing phase.
1973 mutex_enter(&dn
->dn_mtx
);
1975 int txgoff
= tx
->tx_txg
& TXG_MASK
;
1976 if (dn
->dn_free_ranges
[txgoff
] == NULL
) {
1977 dn
->dn_free_ranges
[txgoff
] =
1978 range_tree_create(NULL
, NULL
, &dn
->dn_mtx
);
1980 range_tree_clear(dn
->dn_free_ranges
[txgoff
], blkid
, nblks
);
1981 range_tree_add(dn
->dn_free_ranges
[txgoff
], blkid
, nblks
);
1983 dprintf_dnode(dn
, "blkid=%llu nblks=%llu txg=%llu\n",
1984 blkid
, nblks
, tx
->tx_txg
);
1985 mutex_exit(&dn
->dn_mtx
);
1987 dbuf_free_range(dn
, blkid
, blkid
+ nblks
- 1, tx
);
1988 dnode_setdirty(dn
, tx
);
1991 rw_exit(&dn
->dn_struct_rwlock
);
1995 dnode_spill_freed(dnode_t
*dn
)
1999 mutex_enter(&dn
->dn_mtx
);
2000 for (i
= 0; i
< TXG_SIZE
; i
++) {
2001 if (dn
->dn_rm_spillblk
[i
] == DN_KILL_SPILLBLK
)
2004 mutex_exit(&dn
->dn_mtx
);
2005 return (i
< TXG_SIZE
);
2008 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
2010 dnode_block_freed(dnode_t
*dn
, uint64_t blkid
)
2012 void *dp
= spa_get_dsl(dn
->dn_objset
->os_spa
);
2015 if (blkid
== DMU_BONUS_BLKID
)
2019 * If we're in the process of opening the pool, dp will not be
2020 * set yet, but there shouldn't be anything dirty.
2025 if (dn
->dn_free_txg
)
2028 if (blkid
== DMU_SPILL_BLKID
)
2029 return (dnode_spill_freed(dn
));
2031 mutex_enter(&dn
->dn_mtx
);
2032 for (i
= 0; i
< TXG_SIZE
; i
++) {
2033 if (dn
->dn_free_ranges
[i
] != NULL
&&
2034 range_tree_contains(dn
->dn_free_ranges
[i
], blkid
, 1))
2037 mutex_exit(&dn
->dn_mtx
);
2038 return (i
< TXG_SIZE
);
2041 /* call from syncing context when we actually write/free space for this dnode */
2043 dnode_diduse_space(dnode_t
*dn
, int64_t delta
)
2046 dprintf_dnode(dn
, "dn=%p dnp=%p used=%llu delta=%lld\n",
2048 (u_longlong_t
)dn
->dn_phys
->dn_used
,
2051 mutex_enter(&dn
->dn_mtx
);
2052 space
= DN_USED_BYTES(dn
->dn_phys
);
2054 ASSERT3U(space
+ delta
, >=, space
); /* no overflow */
2056 ASSERT3U(space
, >=, -delta
); /* no underflow */
2059 if (spa_version(dn
->dn_objset
->os_spa
) < SPA_VERSION_DNODE_BYTES
) {
2060 ASSERT((dn
->dn_phys
->dn_flags
& DNODE_FLAG_USED_BYTES
) == 0);
2061 ASSERT0(P2PHASE(space
, 1<<DEV_BSHIFT
));
2062 dn
->dn_phys
->dn_used
= space
>> DEV_BSHIFT
;
2064 dn
->dn_phys
->dn_used
= space
;
2065 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_USED_BYTES
;
2067 mutex_exit(&dn
->dn_mtx
);
2071 * Scans a block at the indicated "level" looking for a hole or data,
2072 * depending on 'flags'.
2074 * If level > 0, then we are scanning an indirect block looking at its
2075 * pointers. If level == 0, then we are looking at a block of dnodes.
2077 * If we don't find what we are looking for in the block, we return ESRCH.
2078 * Otherwise, return with *offset pointing to the beginning (if searching
2079 * forwards) or end (if searching backwards) of the range covered by the
2080 * block pointer we matched on (or dnode).
2082 * The basic search algorithm used below by dnode_next_offset() is to
2083 * use this function to search up the block tree (widen the search) until
2084 * we find something (i.e., we don't return ESRCH) and then search back
2085 * down the tree (narrow the search) until we reach our original search
2089 dnode_next_offset_level(dnode_t
*dn
, int flags
, uint64_t *offset
,
2090 int lvl
, uint64_t blkfill
, uint64_t txg
)
2092 dmu_buf_impl_t
*db
= NULL
;
2094 uint64_t epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2095 uint64_t epb
= 1ULL << epbs
;
2096 uint64_t minfill
, maxfill
;
2098 int i
, inc
, error
, span
;
2100 hole
= ((flags
& DNODE_FIND_HOLE
) != 0);
2101 inc
= (flags
& DNODE_FIND_BACKWARDS
) ? -1 : 1;
2102 ASSERT(txg
== 0 || !hole
);
2104 if (lvl
== dn
->dn_phys
->dn_nlevels
) {
2106 epb
= dn
->dn_phys
->dn_nblkptr
;
2107 data
= dn
->dn_phys
->dn_blkptr
;
2109 uint64_t blkid
= dbuf_whichblock(dn
, lvl
, *offset
);
2110 error
= dbuf_hold_impl(dn
, lvl
, blkid
, TRUE
, FALSE
, FTAG
, &db
);
2112 if (error
!= ENOENT
)
2117 * This can only happen when we are searching up
2118 * the block tree for data. We don't really need to
2119 * adjust the offset, as we will just end up looking
2120 * at the pointer to this block in its parent, and its
2121 * going to be unallocated, so we will skip over it.
2123 return (SET_ERROR(ESRCH
));
2125 error
= dbuf_read(db
, NULL
,
2126 DB_RF_CANFAIL
| DB_RF_HAVESTRUCT
| DB_RF_NO_DECRYPT
);
2128 dbuf_rele(db
, FTAG
);
2131 data
= db
->db
.db_data
;
2135 if (db
!= NULL
&& txg
!= 0 && (db
->db_blkptr
== NULL
||
2136 db
->db_blkptr
->blk_birth
<= txg
||
2137 BP_IS_HOLE(db
->db_blkptr
))) {
2139 * This can only happen when we are searching up the tree
2140 * and these conditions mean that we need to keep climbing.
2142 error
= SET_ERROR(ESRCH
);
2143 } else if (lvl
== 0) {
2144 dnode_phys_t
*dnp
= data
;
2146 ASSERT(dn
->dn_type
== DMU_OT_DNODE
);
2147 ASSERT(!(flags
& DNODE_FIND_BACKWARDS
));
2149 for (i
= (*offset
>> DNODE_SHIFT
) & (blkfill
- 1);
2150 i
< blkfill
; i
+= dnp
[i
].dn_extra_slots
+ 1) {
2151 if ((dnp
[i
].dn_type
== DMU_OT_NONE
) == hole
)
2156 error
= SET_ERROR(ESRCH
);
2158 *offset
= (*offset
& ~(DNODE_BLOCK_SIZE
- 1)) +
2161 blkptr_t
*bp
= data
;
2162 uint64_t start
= *offset
;
2163 span
= (lvl
- 1) * epbs
+ dn
->dn_datablkshift
;
2165 maxfill
= blkfill
<< ((lvl
- 1) * epbs
);
2172 if (span
>= 8 * sizeof (*offset
)) {
2173 /* This only happens on the highest indirection level */
2174 ASSERT3U((lvl
- 1), ==, dn
->dn_phys
->dn_nlevels
- 1);
2177 *offset
= *offset
>> span
;
2180 for (i
= BF64_GET(*offset
, 0, epbs
);
2181 i
>= 0 && i
< epb
; i
+= inc
) {
2182 if (BP_GET_FILL(&bp
[i
]) >= minfill
&&
2183 BP_GET_FILL(&bp
[i
]) <= maxfill
&&
2184 (hole
|| bp
[i
].blk_birth
> txg
))
2186 if (inc
> 0 || *offset
> 0)
2190 if (span
>= 8 * sizeof (*offset
)) {
2193 *offset
= *offset
<< span
;
2197 /* traversing backwards; position offset at the end */
2198 ASSERT3U(*offset
, <=, start
);
2199 *offset
= MIN(*offset
+ (1ULL << span
) - 1, start
);
2200 } else if (*offset
< start
) {
2203 if (i
< 0 || i
>= epb
)
2204 error
= SET_ERROR(ESRCH
);
2208 dbuf_rele(db
, FTAG
);
2214 * Find the next hole, data, or sparse region at or after *offset.
2215 * The value 'blkfill' tells us how many items we expect to find
2216 * in an L0 data block; this value is 1 for normal objects,
2217 * DNODES_PER_BLOCK for the meta dnode, and some fraction of
2218 * DNODES_PER_BLOCK when searching for sparse regions thereof.
2222 * dnode_next_offset(dn, flags, offset, 1, 1, 0);
2223 * Finds the next/previous hole/data in a file.
2224 * Used in dmu_offset_next().
2226 * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
2227 * Finds the next free/allocated dnode an objset's meta-dnode.
2228 * Only finds objects that have new contents since txg (ie.
2229 * bonus buffer changes and content removal are ignored).
2230 * Used in dmu_object_next().
2232 * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
2233 * Finds the next L2 meta-dnode bp that's at most 1/4 full.
2234 * Used in dmu_object_alloc().
2237 dnode_next_offset(dnode_t
*dn
, int flags
, uint64_t *offset
,
2238 int minlvl
, uint64_t blkfill
, uint64_t txg
)
2240 uint64_t initial_offset
= *offset
;
2244 if (!(flags
& DNODE_FIND_HAVELOCK
))
2245 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2247 if (dn
->dn_phys
->dn_nlevels
== 0) {
2248 error
= SET_ERROR(ESRCH
);
2252 if (dn
->dn_datablkshift
== 0) {
2253 if (*offset
< dn
->dn_datablksz
) {
2254 if (flags
& DNODE_FIND_HOLE
)
2255 *offset
= dn
->dn_datablksz
;
2257 error
= SET_ERROR(ESRCH
);
2262 maxlvl
= dn
->dn_phys
->dn_nlevels
;
2264 for (lvl
= minlvl
; lvl
<= maxlvl
; lvl
++) {
2265 error
= dnode_next_offset_level(dn
,
2266 flags
, offset
, lvl
, blkfill
, txg
);
2271 while (error
== 0 && --lvl
>= minlvl
) {
2272 error
= dnode_next_offset_level(dn
,
2273 flags
, offset
, lvl
, blkfill
, txg
);
2277 * There's always a "virtual hole" at the end of the object, even
2278 * if all BP's which physically exist are non-holes.
2280 if ((flags
& DNODE_FIND_HOLE
) && error
== ESRCH
&& txg
== 0 &&
2281 minlvl
== 1 && blkfill
== 1 && !(flags
& DNODE_FIND_BACKWARDS
)) {
2285 if (error
== 0 && (flags
& DNODE_FIND_BACKWARDS
?
2286 initial_offset
< *offset
: initial_offset
> *offset
))
2287 error
= SET_ERROR(ESRCH
);
2289 if (!(flags
& DNODE_FIND_HAVELOCK
))
2290 rw_exit(&dn
->dn_struct_rwlock
);