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
));
137 bzero(&dn
->dn_next_maxblkid
[0], sizeof (dn
->dn_next_maxblkid
));
139 for (i
= 0; i
< TXG_SIZE
; i
++) {
140 list_link_init(&dn
->dn_dirty_link
[i
]);
141 dn
->dn_free_ranges
[i
] = NULL
;
142 list_create(&dn
->dn_dirty_records
[i
],
143 sizeof (dbuf_dirty_record_t
),
144 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
147 dn
->dn_allocated_txg
= 0;
149 dn
->dn_assigned_txg
= 0;
151 dn
->dn_dirtyctx_firstset
= NULL
;
153 dn
->dn_have_spill
= B_FALSE
;
163 dn
->dn_dbufs_count
= 0;
164 avl_create(&dn
->dn_dbufs
, dbuf_compare
, sizeof (dmu_buf_impl_t
),
165 offsetof(dmu_buf_impl_t
, db_link
));
173 dnode_dest(void *arg
, void *unused
)
178 rw_destroy(&dn
->dn_struct_rwlock
);
179 mutex_destroy(&dn
->dn_mtx
);
180 mutex_destroy(&dn
->dn_dbufs_mtx
);
181 cv_destroy(&dn
->dn_notxholds
);
182 refcount_destroy(&dn
->dn_holds
);
183 refcount_destroy(&dn
->dn_tx_holds
);
184 ASSERT(!list_link_active(&dn
->dn_link
));
186 for (i
= 0; i
< TXG_SIZE
; i
++) {
187 ASSERT(!list_link_active(&dn
->dn_dirty_link
[i
]));
188 ASSERT3P(dn
->dn_free_ranges
[i
], ==, NULL
);
189 list_destroy(&dn
->dn_dirty_records
[i
]);
190 ASSERT0(dn
->dn_next_nblkptr
[i
]);
191 ASSERT0(dn
->dn_next_nlevels
[i
]);
192 ASSERT0(dn
->dn_next_indblkshift
[i
]);
193 ASSERT0(dn
->dn_next_bonustype
[i
]);
194 ASSERT0(dn
->dn_rm_spillblk
[i
]);
195 ASSERT0(dn
->dn_next_bonuslen
[i
]);
196 ASSERT0(dn
->dn_next_blksz
[i
]);
197 ASSERT0(dn
->dn_next_maxblkid
[i
]);
200 ASSERT0(dn
->dn_allocated_txg
);
201 ASSERT0(dn
->dn_free_txg
);
202 ASSERT0(dn
->dn_assigned_txg
);
203 ASSERT0(dn
->dn_dirtyctx
);
204 ASSERT3P(dn
->dn_dirtyctx_firstset
, ==, NULL
);
205 ASSERT3P(dn
->dn_bonus
, ==, NULL
);
206 ASSERT(!dn
->dn_have_spill
);
207 ASSERT3P(dn
->dn_zio
, ==, NULL
);
208 ASSERT0(dn
->dn_oldused
);
209 ASSERT0(dn
->dn_oldflags
);
210 ASSERT0(dn
->dn_olduid
);
211 ASSERT0(dn
->dn_oldgid
);
212 ASSERT0(dn
->dn_newuid
);
213 ASSERT0(dn
->dn_newgid
);
214 ASSERT0(dn
->dn_id_flags
);
216 ASSERT0(dn
->dn_dbufs_count
);
217 avl_destroy(&dn
->dn_dbufs
);
223 ASSERT(dnode_cache
== NULL
);
224 dnode_cache
= kmem_cache_create("dnode_t", sizeof (dnode_t
),
225 0, dnode_cons
, dnode_dest
, NULL
, NULL
, NULL
, 0);
226 kmem_cache_set_move(dnode_cache
, dnode_move
);
228 dnode_ksp
= kstat_create("zfs", 0, "dnodestats", "misc",
229 KSTAT_TYPE_NAMED
, sizeof (dnode_stats
) / sizeof (kstat_named_t
),
231 if (dnode_ksp
!= NULL
) {
232 dnode_ksp
->ks_data
= &dnode_stats
;
233 kstat_install(dnode_ksp
);
240 if (dnode_ksp
!= NULL
) {
241 kstat_delete(dnode_ksp
);
245 kmem_cache_destroy(dnode_cache
);
252 dnode_verify(dnode_t
*dn
)
254 int drop_struct_lock
= FALSE
;
257 ASSERT(dn
->dn_objset
);
258 ASSERT(dn
->dn_handle
->dnh_dnode
== dn
);
260 ASSERT(DMU_OT_IS_VALID(dn
->dn_phys
->dn_type
));
262 if (!(zfs_flags
& ZFS_DEBUG_DNODE_VERIFY
))
265 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
266 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
267 drop_struct_lock
= TRUE
;
269 if (dn
->dn_phys
->dn_type
!= DMU_OT_NONE
|| dn
->dn_allocated_txg
!= 0) {
271 int max_bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
272 ASSERT3U(dn
->dn_indblkshift
, <=, SPA_MAXBLOCKSHIFT
);
273 if (dn
->dn_datablkshift
) {
274 ASSERT3U(dn
->dn_datablkshift
, >=, SPA_MINBLOCKSHIFT
);
275 ASSERT3U(dn
->dn_datablkshift
, <=, SPA_MAXBLOCKSHIFT
);
276 ASSERT3U(1<<dn
->dn_datablkshift
, ==, dn
->dn_datablksz
);
278 ASSERT3U(dn
->dn_nlevels
, <=, 30);
279 ASSERT(DMU_OT_IS_VALID(dn
->dn_type
));
280 ASSERT3U(dn
->dn_nblkptr
, >=, 1);
281 ASSERT3U(dn
->dn_nblkptr
, <=, DN_MAX_NBLKPTR
);
282 ASSERT3U(dn
->dn_bonuslen
, <=, max_bonuslen
);
283 ASSERT3U(dn
->dn_datablksz
, ==,
284 dn
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
);
285 ASSERT3U(ISP2(dn
->dn_datablksz
), ==, dn
->dn_datablkshift
!= 0);
286 ASSERT3U((dn
->dn_nblkptr
- 1) * sizeof (blkptr_t
) +
287 dn
->dn_bonuslen
, <=, max_bonuslen
);
288 for (i
= 0; i
< TXG_SIZE
; i
++) {
289 ASSERT3U(dn
->dn_next_nlevels
[i
], <=, dn
->dn_nlevels
);
292 if (dn
->dn_phys
->dn_type
!= DMU_OT_NONE
)
293 ASSERT3U(dn
->dn_phys
->dn_nlevels
, <=, dn
->dn_nlevels
);
294 ASSERT(DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) || dn
->dn_dbuf
!= NULL
);
295 if (dn
->dn_dbuf
!= NULL
) {
296 ASSERT3P(dn
->dn_phys
, ==,
297 (dnode_phys_t
*)dn
->dn_dbuf
->db
.db_data
+
298 (dn
->dn_object
% (dn
->dn_dbuf
->db
.db_size
>> DNODE_SHIFT
)));
300 if (drop_struct_lock
)
301 rw_exit(&dn
->dn_struct_rwlock
);
306 dnode_byteswap(dnode_phys_t
*dnp
)
308 uint64_t *buf64
= (void*)&dnp
->dn_blkptr
;
311 if (dnp
->dn_type
== DMU_OT_NONE
) {
312 bzero(dnp
, sizeof (dnode_phys_t
));
316 dnp
->dn_datablkszsec
= BSWAP_16(dnp
->dn_datablkszsec
);
317 dnp
->dn_bonuslen
= BSWAP_16(dnp
->dn_bonuslen
);
318 dnp
->dn_extra_slots
= BSWAP_8(dnp
->dn_extra_slots
);
319 dnp
->dn_maxblkid
= BSWAP_64(dnp
->dn_maxblkid
);
320 dnp
->dn_used
= BSWAP_64(dnp
->dn_used
);
323 * dn_nblkptr is only one byte, so it's OK to read it in either
324 * byte order. We can't read dn_bouslen.
326 ASSERT(dnp
->dn_indblkshift
<= SPA_MAXBLOCKSHIFT
);
327 ASSERT(dnp
->dn_nblkptr
<= DN_MAX_NBLKPTR
);
328 for (i
= 0; i
< dnp
->dn_nblkptr
* sizeof (blkptr_t
)/8; i
++)
329 buf64
[i
] = BSWAP_64(buf64
[i
]);
332 * OK to check dn_bonuslen for zero, because it won't matter if
333 * we have the wrong byte order. This is necessary because the
334 * dnode dnode is smaller than a regular dnode.
336 if (dnp
->dn_bonuslen
!= 0) {
338 * Note that the bonus length calculated here may be
339 * longer than the actual bonus buffer. This is because
340 * we always put the bonus buffer after the last block
341 * pointer (instead of packing it against the end of the
344 int off
= (dnp
->dn_nblkptr
-1) * sizeof (blkptr_t
);
345 int slots
= dnp
->dn_extra_slots
+ 1;
346 size_t len
= DN_SLOTS_TO_BONUSLEN(slots
) - off
;
347 dmu_object_byteswap_t byteswap
;
348 ASSERT(DMU_OT_IS_VALID(dnp
->dn_bonustype
));
349 byteswap
= DMU_OT_BYTESWAP(dnp
->dn_bonustype
);
350 dmu_ot_byteswap
[byteswap
].ob_func(dnp
->dn_bonus
+ off
, len
);
353 /* Swap SPILL block if we have one */
354 if (dnp
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
)
355 byteswap_uint64_array(DN_SPILL_BLKPTR(dnp
), sizeof (blkptr_t
));
359 dnode_buf_byteswap(void *vbuf
, size_t size
)
363 ASSERT3U(sizeof (dnode_phys_t
), ==, (1<<DNODE_SHIFT
));
364 ASSERT((size
& (sizeof (dnode_phys_t
)-1)) == 0);
367 dnode_phys_t
*dnp
= (void *)(((char *)vbuf
) + i
);
371 if (dnp
->dn_type
!= DMU_OT_NONE
)
372 i
+= dnp
->dn_extra_slots
* DNODE_MIN_SIZE
;
377 dnode_setbonuslen(dnode_t
*dn
, int newsize
, dmu_tx_t
*tx
)
379 ASSERT3U(refcount_count(&dn
->dn_holds
), >=, 1);
381 dnode_setdirty(dn
, tx
);
382 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
383 ASSERT3U(newsize
, <=, DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
384 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
));
385 dn
->dn_bonuslen
= newsize
;
387 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = DN_ZERO_BONUSLEN
;
389 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonuslen
;
390 rw_exit(&dn
->dn_struct_rwlock
);
394 dnode_setbonus_type(dnode_t
*dn
, dmu_object_type_t newtype
, dmu_tx_t
*tx
)
396 ASSERT3U(refcount_count(&dn
->dn_holds
), >=, 1);
397 dnode_setdirty(dn
, tx
);
398 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
399 dn
->dn_bonustype
= newtype
;
400 dn
->dn_next_bonustype
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonustype
;
401 rw_exit(&dn
->dn_struct_rwlock
);
405 dnode_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
407 ASSERT3U(refcount_count(&dn
->dn_holds
), >=, 1);
408 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
409 dnode_setdirty(dn
, tx
);
410 dn
->dn_rm_spillblk
[tx
->tx_txg
&TXG_MASK
] = DN_KILL_SPILLBLK
;
411 dn
->dn_have_spill
= B_FALSE
;
415 dnode_setdblksz(dnode_t
*dn
, int size
)
417 ASSERT0(P2PHASE(size
, SPA_MINBLOCKSIZE
));
418 ASSERT3U(size
, <=, SPA_MAXBLOCKSIZE
);
419 ASSERT3U(size
, >=, SPA_MINBLOCKSIZE
);
420 ASSERT3U(size
>> SPA_MINBLOCKSHIFT
, <,
421 1<<(sizeof (dn
->dn_phys
->dn_datablkszsec
) * 8));
422 dn
->dn_datablksz
= size
;
423 dn
->dn_datablkszsec
= size
>> SPA_MINBLOCKSHIFT
;
424 dn
->dn_datablkshift
= ISP2(size
) ? highbit64(size
- 1) : 0;
428 dnode_create(objset_t
*os
, dnode_phys_t
*dnp
, dmu_buf_impl_t
*db
,
429 uint64_t object
, dnode_handle_t
*dnh
)
433 dn
= kmem_cache_alloc(dnode_cache
, KM_SLEEP
);
434 ASSERT(!POINTER_IS_VALID(dn
->dn_objset
));
438 * Defer setting dn_objset until the dnode is ready to be a candidate
439 * for the dnode_move() callback.
441 dn
->dn_object
= object
;
446 if (dnp
->dn_datablkszsec
) {
447 dnode_setdblksz(dn
, dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
);
449 dn
->dn_datablksz
= 0;
450 dn
->dn_datablkszsec
= 0;
451 dn
->dn_datablkshift
= 0;
453 dn
->dn_indblkshift
= dnp
->dn_indblkshift
;
454 dn
->dn_nlevels
= dnp
->dn_nlevels
;
455 dn
->dn_type
= dnp
->dn_type
;
456 dn
->dn_nblkptr
= dnp
->dn_nblkptr
;
457 dn
->dn_checksum
= dnp
->dn_checksum
;
458 dn
->dn_compress
= dnp
->dn_compress
;
459 dn
->dn_bonustype
= dnp
->dn_bonustype
;
460 dn
->dn_bonuslen
= dnp
->dn_bonuslen
;
461 dn
->dn_num_slots
= dnp
->dn_extra_slots
+ 1;
462 dn
->dn_maxblkid
= dnp
->dn_maxblkid
;
463 dn
->dn_have_spill
= ((dnp
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
) != 0);
466 dmu_zfetch_init(&dn
->dn_zfetch
, dn
);
468 ASSERT(DMU_OT_IS_VALID(dn
->dn_phys
->dn_type
));
469 ASSERT(zrl_is_locked(&dnh
->dnh_zrlock
));
470 ASSERT(!DN_SLOT_IS_PTR(dnh
->dnh_dnode
));
472 mutex_enter(&os
->os_lock
);
475 * Exclude special dnodes from os_dnodes so an empty os_dnodes
476 * signifies that the special dnodes have no references from
477 * their children (the entries in os_dnodes). This allows
478 * dnode_destroy() to easily determine if the last child has
479 * been removed and then complete eviction of the objset.
481 if (!DMU_OBJECT_IS_SPECIAL(object
))
482 list_insert_head(&os
->os_dnodes
, dn
);
486 * Everything else must be valid before assigning dn_objset
487 * makes the dnode eligible for dnode_move().
492 mutex_exit(&os
->os_lock
);
494 arc_space_consume(sizeof (dnode_t
), ARC_SPACE_DNODE
);
500 * Caller must be holding the dnode handle, which is released upon return.
503 dnode_destroy(dnode_t
*dn
)
505 objset_t
*os
= dn
->dn_objset
;
506 boolean_t complete_os_eviction
= B_FALSE
;
508 ASSERT((dn
->dn_id_flags
& DN_ID_NEW_EXIST
) == 0);
510 mutex_enter(&os
->os_lock
);
511 POINTER_INVALIDATE(&dn
->dn_objset
);
512 if (!DMU_OBJECT_IS_SPECIAL(dn
->dn_object
)) {
513 list_remove(&os
->os_dnodes
, dn
);
514 complete_os_eviction
=
515 list_is_empty(&os
->os_dnodes
) &&
516 list_link_active(&os
->os_evicting_node
);
518 mutex_exit(&os
->os_lock
);
520 /* the dnode can no longer move, so we can release the handle */
521 zrl_remove(&dn
->dn_handle
->dnh_zrlock
);
523 dn
->dn_allocated_txg
= 0;
525 dn
->dn_assigned_txg
= 0;
528 if (dn
->dn_dirtyctx_firstset
!= NULL
) {
529 kmem_free(dn
->dn_dirtyctx_firstset
, 1);
530 dn
->dn_dirtyctx_firstset
= NULL
;
532 if (dn
->dn_bonus
!= NULL
) {
533 mutex_enter(&dn
->dn_bonus
->db_mtx
);
534 dbuf_destroy(dn
->dn_bonus
);
539 dn
->dn_have_spill
= B_FALSE
;
548 dmu_zfetch_fini(&dn
->dn_zfetch
);
549 kmem_cache_free(dnode_cache
, dn
);
550 arc_space_return(sizeof (dnode_t
), ARC_SPACE_DNODE
);
552 if (complete_os_eviction
)
553 dmu_objset_evict_done(os
);
557 dnode_allocate(dnode_t
*dn
, dmu_object_type_t ot
, int blocksize
, int ibs
,
558 dmu_object_type_t bonustype
, int bonuslen
, int dn_slots
, dmu_tx_t
*tx
)
562 ASSERT3U(dn_slots
, >, 0);
563 ASSERT3U(dn_slots
<< DNODE_SHIFT
, <=,
564 spa_maxdnodesize(dmu_objset_spa(dn
->dn_objset
)));
565 ASSERT3U(blocksize
, <=,
566 spa_maxblocksize(dmu_objset_spa(dn
->dn_objset
)));
568 blocksize
= 1 << zfs_default_bs
;
570 blocksize
= P2ROUNDUP(blocksize
, SPA_MINBLOCKSIZE
);
573 ibs
= zfs_default_ibs
;
575 ibs
= MIN(MAX(ibs
, DN_MIN_INDBLKSHIFT
), DN_MAX_INDBLKSHIFT
);
577 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d dn_slots=%d\n",
578 dn
->dn_objset
, dn
->dn_object
, tx
->tx_txg
, blocksize
, ibs
, dn_slots
);
579 DNODE_STAT_BUMP(dnode_allocate
);
581 ASSERT(dn
->dn_type
== DMU_OT_NONE
);
582 ASSERT(bcmp(dn
->dn_phys
, &dnode_phys_zero
, sizeof (dnode_phys_t
)) == 0);
583 ASSERT(dn
->dn_phys
->dn_type
== DMU_OT_NONE
);
584 ASSERT(ot
!= DMU_OT_NONE
);
585 ASSERT(DMU_OT_IS_VALID(ot
));
586 ASSERT((bonustype
== DMU_OT_NONE
&& bonuslen
== 0) ||
587 (bonustype
== DMU_OT_SA
&& bonuslen
== 0) ||
588 (bonustype
!= DMU_OT_NONE
&& bonuslen
!= 0));
589 ASSERT(DMU_OT_IS_VALID(bonustype
));
590 ASSERT3U(bonuslen
, <=, DN_SLOTS_TO_BONUSLEN(dn_slots
));
591 ASSERT(dn
->dn_type
== DMU_OT_NONE
);
592 ASSERT0(dn
->dn_maxblkid
);
593 ASSERT0(dn
->dn_allocated_txg
);
594 ASSERT0(dn
->dn_assigned_txg
);
595 ASSERT(refcount_is_zero(&dn
->dn_tx_holds
));
596 ASSERT3U(refcount_count(&dn
->dn_holds
), <=, 1);
597 ASSERT(avl_is_empty(&dn
->dn_dbufs
));
599 for (i
= 0; i
< TXG_SIZE
; i
++) {
600 ASSERT0(dn
->dn_next_nblkptr
[i
]);
601 ASSERT0(dn
->dn_next_nlevels
[i
]);
602 ASSERT0(dn
->dn_next_indblkshift
[i
]);
603 ASSERT0(dn
->dn_next_bonuslen
[i
]);
604 ASSERT0(dn
->dn_next_bonustype
[i
]);
605 ASSERT0(dn
->dn_rm_spillblk
[i
]);
606 ASSERT0(dn
->dn_next_blksz
[i
]);
607 ASSERT0(dn
->dn_next_maxblkid
[i
]);
608 ASSERT(!list_link_active(&dn
->dn_dirty_link
[i
]));
609 ASSERT3P(list_head(&dn
->dn_dirty_records
[i
]), ==, NULL
);
610 ASSERT3P(dn
->dn_free_ranges
[i
], ==, NULL
);
614 dnode_setdblksz(dn
, blocksize
);
615 dn
->dn_indblkshift
= ibs
;
617 dn
->dn_num_slots
= dn_slots
;
618 if (bonustype
== DMU_OT_SA
) /* Maximize bonus space for SA */
621 dn
->dn_nblkptr
= MIN(DN_MAX_NBLKPTR
,
622 1 + ((DN_SLOTS_TO_BONUSLEN(dn_slots
) - bonuslen
) >>
626 dn
->dn_bonustype
= bonustype
;
627 dn
->dn_bonuslen
= bonuslen
;
628 dn
->dn_checksum
= ZIO_CHECKSUM_INHERIT
;
629 dn
->dn_compress
= ZIO_COMPRESS_INHERIT
;
633 if (dn
->dn_dirtyctx_firstset
) {
634 kmem_free(dn
->dn_dirtyctx_firstset
, 1);
635 dn
->dn_dirtyctx_firstset
= NULL
;
638 dn
->dn_allocated_txg
= tx
->tx_txg
;
641 dnode_setdirty(dn
, tx
);
642 dn
->dn_next_indblkshift
[tx
->tx_txg
& TXG_MASK
] = ibs
;
643 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonuslen
;
644 dn
->dn_next_bonustype
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonustype
;
645 dn
->dn_next_blksz
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_datablksz
;
649 dnode_reallocate(dnode_t
*dn
, dmu_object_type_t ot
, int blocksize
,
650 dmu_object_type_t bonustype
, int bonuslen
, int dn_slots
, dmu_tx_t
*tx
)
654 ASSERT3U(blocksize
, >=, SPA_MINBLOCKSIZE
);
655 ASSERT3U(blocksize
, <=,
656 spa_maxblocksize(dmu_objset_spa(dn
->dn_objset
)));
657 ASSERT0(blocksize
% SPA_MINBLOCKSIZE
);
658 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
|| dmu_tx_private_ok(tx
));
659 ASSERT(tx
->tx_txg
!= 0);
660 ASSERT((bonustype
== DMU_OT_NONE
&& bonuslen
== 0) ||
661 (bonustype
!= DMU_OT_NONE
&& bonuslen
!= 0) ||
662 (bonustype
== DMU_OT_SA
&& bonuslen
== 0));
663 ASSERT(DMU_OT_IS_VALID(bonustype
));
664 ASSERT3U(bonuslen
, <=,
665 DN_BONUS_SIZE(spa_maxdnodesize(dmu_objset_spa(dn
->dn_objset
))));
667 dn_slots
= dn_slots
> 0 ? dn_slots
: DNODE_MIN_SLOTS
;
668 DNODE_STAT_BUMP(dnode_reallocate
);
670 /* clean up any unreferenced dbufs */
671 dnode_evict_dbufs(dn
);
675 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
676 dnode_setdirty(dn
, tx
);
677 if (dn
->dn_datablksz
!= blocksize
) {
678 /* change blocksize */
679 ASSERT(dn
->dn_maxblkid
== 0 &&
680 (BP_IS_HOLE(&dn
->dn_phys
->dn_blkptr
[0]) ||
681 dnode_block_freed(dn
, 0)));
682 dnode_setdblksz(dn
, blocksize
);
683 dn
->dn_next_blksz
[tx
->tx_txg
&TXG_MASK
] = blocksize
;
685 if (dn
->dn_bonuslen
!= bonuslen
)
686 dn
->dn_next_bonuslen
[tx
->tx_txg
&TXG_MASK
] = bonuslen
;
688 if (bonustype
== DMU_OT_SA
) /* Maximize bonus space for SA */
691 nblkptr
= MIN(DN_MAX_NBLKPTR
,
692 1 + ((DN_SLOTS_TO_BONUSLEN(dn_slots
) - bonuslen
) >>
694 if (dn
->dn_bonustype
!= bonustype
)
695 dn
->dn_next_bonustype
[tx
->tx_txg
&TXG_MASK
] = bonustype
;
696 if (dn
->dn_nblkptr
!= nblkptr
)
697 dn
->dn_next_nblkptr
[tx
->tx_txg
&TXG_MASK
] = nblkptr
;
698 if (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
) {
699 dbuf_rm_spill(dn
, tx
);
700 dnode_rm_spill(dn
, tx
);
702 rw_exit(&dn
->dn_struct_rwlock
);
707 /* change bonus size and type */
708 mutex_enter(&dn
->dn_mtx
);
709 dn
->dn_bonustype
= bonustype
;
710 dn
->dn_bonuslen
= bonuslen
;
711 dn
->dn_num_slots
= dn_slots
;
712 dn
->dn_nblkptr
= nblkptr
;
713 dn
->dn_checksum
= ZIO_CHECKSUM_INHERIT
;
714 dn
->dn_compress
= ZIO_COMPRESS_INHERIT
;
715 ASSERT3U(dn
->dn_nblkptr
, <=, DN_MAX_NBLKPTR
);
717 /* fix up the bonus db_size */
719 dn
->dn_bonus
->db
.db_size
=
720 DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
721 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
722 ASSERT(dn
->dn_bonuslen
<= dn
->dn_bonus
->db
.db_size
);
725 dn
->dn_allocated_txg
= tx
->tx_txg
;
726 mutex_exit(&dn
->dn_mtx
);
731 dnode_move_impl(dnode_t
*odn
, dnode_t
*ndn
)
735 ASSERT(!RW_LOCK_HELD(&odn
->dn_struct_rwlock
));
736 ASSERT(MUTEX_NOT_HELD(&odn
->dn_mtx
));
737 ASSERT(MUTEX_NOT_HELD(&odn
->dn_dbufs_mtx
));
738 ASSERT(!RW_LOCK_HELD(&odn
->dn_zfetch
.zf_rwlock
));
741 ndn
->dn_objset
= odn
->dn_objset
;
742 ndn
->dn_object
= odn
->dn_object
;
743 ndn
->dn_dbuf
= odn
->dn_dbuf
;
744 ndn
->dn_handle
= odn
->dn_handle
;
745 ndn
->dn_phys
= odn
->dn_phys
;
746 ndn
->dn_type
= odn
->dn_type
;
747 ndn
->dn_bonuslen
= odn
->dn_bonuslen
;
748 ndn
->dn_bonustype
= odn
->dn_bonustype
;
749 ndn
->dn_nblkptr
= odn
->dn_nblkptr
;
750 ndn
->dn_checksum
= odn
->dn_checksum
;
751 ndn
->dn_compress
= odn
->dn_compress
;
752 ndn
->dn_nlevels
= odn
->dn_nlevels
;
753 ndn
->dn_indblkshift
= odn
->dn_indblkshift
;
754 ndn
->dn_datablkshift
= odn
->dn_datablkshift
;
755 ndn
->dn_datablkszsec
= odn
->dn_datablkszsec
;
756 ndn
->dn_datablksz
= odn
->dn_datablksz
;
757 ndn
->dn_maxblkid
= odn
->dn_maxblkid
;
758 ndn
->dn_num_slots
= odn
->dn_num_slots
;
759 bcopy(&odn
->dn_next_nblkptr
[0], &ndn
->dn_next_nblkptr
[0],
760 sizeof (odn
->dn_next_nblkptr
));
761 bcopy(&odn
->dn_next_nlevels
[0], &ndn
->dn_next_nlevels
[0],
762 sizeof (odn
->dn_next_nlevels
));
763 bcopy(&odn
->dn_next_indblkshift
[0], &ndn
->dn_next_indblkshift
[0],
764 sizeof (odn
->dn_next_indblkshift
));
765 bcopy(&odn
->dn_next_bonustype
[0], &ndn
->dn_next_bonustype
[0],
766 sizeof (odn
->dn_next_bonustype
));
767 bcopy(&odn
->dn_rm_spillblk
[0], &ndn
->dn_rm_spillblk
[0],
768 sizeof (odn
->dn_rm_spillblk
));
769 bcopy(&odn
->dn_next_bonuslen
[0], &ndn
->dn_next_bonuslen
[0],
770 sizeof (odn
->dn_next_bonuslen
));
771 bcopy(&odn
->dn_next_blksz
[0], &ndn
->dn_next_blksz
[0],
772 sizeof (odn
->dn_next_blksz
));
773 bcopy(&odn
->dn_next_maxblkid
[0], &ndn
->dn_next_maxblkid
[0],
774 sizeof (odn
->dn_next_maxblkid
));
775 for (i
= 0; i
< TXG_SIZE
; i
++) {
776 list_move_tail(&ndn
->dn_dirty_records
[i
],
777 &odn
->dn_dirty_records
[i
]);
779 bcopy(&odn
->dn_free_ranges
[0], &ndn
->dn_free_ranges
[0],
780 sizeof (odn
->dn_free_ranges
));
781 ndn
->dn_allocated_txg
= odn
->dn_allocated_txg
;
782 ndn
->dn_free_txg
= odn
->dn_free_txg
;
783 ndn
->dn_assigned_txg
= odn
->dn_assigned_txg
;
784 ndn
->dn_dirtyctx
= odn
->dn_dirtyctx
;
785 ndn
->dn_dirtyctx_firstset
= odn
->dn_dirtyctx_firstset
;
786 ASSERT(refcount_count(&odn
->dn_tx_holds
) == 0);
787 refcount_transfer(&ndn
->dn_holds
, &odn
->dn_holds
);
788 ASSERT(avl_is_empty(&ndn
->dn_dbufs
));
789 avl_swap(&ndn
->dn_dbufs
, &odn
->dn_dbufs
);
790 ndn
->dn_dbufs_count
= odn
->dn_dbufs_count
;
791 ndn
->dn_bonus
= odn
->dn_bonus
;
792 ndn
->dn_have_spill
= odn
->dn_have_spill
;
793 ndn
->dn_zio
= odn
->dn_zio
;
794 ndn
->dn_oldused
= odn
->dn_oldused
;
795 ndn
->dn_oldflags
= odn
->dn_oldflags
;
796 ndn
->dn_olduid
= odn
->dn_olduid
;
797 ndn
->dn_oldgid
= odn
->dn_oldgid
;
798 ndn
->dn_newuid
= odn
->dn_newuid
;
799 ndn
->dn_newgid
= odn
->dn_newgid
;
800 ndn
->dn_id_flags
= odn
->dn_id_flags
;
801 dmu_zfetch_init(&ndn
->dn_zfetch
, NULL
);
802 list_move_tail(&ndn
->dn_zfetch
.zf_stream
, &odn
->dn_zfetch
.zf_stream
);
803 ndn
->dn_zfetch
.zf_dnode
= odn
->dn_zfetch
.zf_dnode
;
806 * Update back pointers. Updating the handle fixes the back pointer of
807 * every descendant dbuf as well as the bonus dbuf.
809 ASSERT(ndn
->dn_handle
->dnh_dnode
== odn
);
810 ndn
->dn_handle
->dnh_dnode
= ndn
;
811 if (ndn
->dn_zfetch
.zf_dnode
== odn
) {
812 ndn
->dn_zfetch
.zf_dnode
= ndn
;
816 * Invalidate the original dnode by clearing all of its back pointers.
819 odn
->dn_handle
= NULL
;
820 avl_create(&odn
->dn_dbufs
, dbuf_compare
, sizeof (dmu_buf_impl_t
),
821 offsetof(dmu_buf_impl_t
, db_link
));
822 odn
->dn_dbufs_count
= 0;
823 odn
->dn_bonus
= NULL
;
824 odn
->dn_zfetch
.zf_dnode
= NULL
;
827 * Set the low bit of the objset pointer to ensure that dnode_move()
828 * recognizes the dnode as invalid in any subsequent callback.
830 POINTER_INVALIDATE(&odn
->dn_objset
);
833 * Satisfy the destructor.
835 for (i
= 0; i
< TXG_SIZE
; i
++) {
836 list_create(&odn
->dn_dirty_records
[i
],
837 sizeof (dbuf_dirty_record_t
),
838 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
839 odn
->dn_free_ranges
[i
] = NULL
;
840 odn
->dn_next_nlevels
[i
] = 0;
841 odn
->dn_next_indblkshift
[i
] = 0;
842 odn
->dn_next_bonustype
[i
] = 0;
843 odn
->dn_rm_spillblk
[i
] = 0;
844 odn
->dn_next_bonuslen
[i
] = 0;
845 odn
->dn_next_blksz
[i
] = 0;
847 odn
->dn_allocated_txg
= 0;
848 odn
->dn_free_txg
= 0;
849 odn
->dn_assigned_txg
= 0;
850 odn
->dn_dirtyctx
= 0;
851 odn
->dn_dirtyctx_firstset
= NULL
;
852 odn
->dn_have_spill
= B_FALSE
;
855 odn
->dn_oldflags
= 0;
860 odn
->dn_id_flags
= 0;
866 odn
->dn_moved
= (uint8_t)-1;
871 dnode_move(void *buf
, void *newbuf
, size_t size
, void *arg
)
873 dnode_t
*odn
= buf
, *ndn
= newbuf
;
879 * The dnode is on the objset's list of known dnodes if the objset
880 * pointer is valid. We set the low bit of the objset pointer when
881 * freeing the dnode to invalidate it, and the memory patterns written
882 * by kmem (baddcafe and deadbeef) set at least one of the two low bits.
883 * A newly created dnode sets the objset pointer last of all to indicate
884 * that the dnode is known and in a valid state to be moved by this
888 if (!POINTER_IS_VALID(os
)) {
889 DNODE_STAT_BUMP(dnode_move_invalid
);
890 return (KMEM_CBRC_DONT_KNOW
);
894 * Ensure that the objset does not go away during the move.
896 rw_enter(&os_lock
, RW_WRITER
);
897 if (os
!= odn
->dn_objset
) {
899 DNODE_STAT_BUMP(dnode_move_recheck1
);
900 return (KMEM_CBRC_DONT_KNOW
);
904 * If the dnode is still valid, then so is the objset. We know that no
905 * valid objset can be freed while we hold os_lock, so we can safely
906 * ensure that the objset remains in use.
908 mutex_enter(&os
->os_lock
);
911 * Recheck the objset pointer in case the dnode was removed just before
912 * acquiring the lock.
914 if (os
!= odn
->dn_objset
) {
915 mutex_exit(&os
->os_lock
);
917 DNODE_STAT_BUMP(dnode_move_recheck2
);
918 return (KMEM_CBRC_DONT_KNOW
);
922 * At this point we know that as long as we hold os->os_lock, the dnode
923 * cannot be freed and fields within the dnode can be safely accessed.
924 * The objset listing this dnode cannot go away as long as this dnode is
928 if (DMU_OBJECT_IS_SPECIAL(odn
->dn_object
)) {
929 mutex_exit(&os
->os_lock
);
930 DNODE_STAT_BUMP(dnode_move_special
);
931 return (KMEM_CBRC_NO
);
933 ASSERT(odn
->dn_dbuf
!= NULL
); /* only "special" dnodes have no parent */
936 * Lock the dnode handle to prevent the dnode from obtaining any new
937 * holds. This also prevents the descendant dbufs and the bonus dbuf
938 * from accessing the dnode, so that we can discount their holds. The
939 * handle is safe to access because we know that while the dnode cannot
940 * go away, neither can its handle. Once we hold dnh_zrlock, we can
941 * safely move any dnode referenced only by dbufs.
943 if (!zrl_tryenter(&odn
->dn_handle
->dnh_zrlock
)) {
944 mutex_exit(&os
->os_lock
);
945 DNODE_STAT_BUMP(dnode_move_handle
);
946 return (KMEM_CBRC_LATER
);
950 * Ensure a consistent view of the dnode's holds and the dnode's dbufs.
951 * We need to guarantee that there is a hold for every dbuf in order to
952 * determine whether the dnode is actively referenced. Falsely matching
953 * a dbuf to an active hold would lead to an unsafe move. It's possible
954 * that a thread already having an active dnode hold is about to add a
955 * dbuf, and we can't compare hold and dbuf counts while the add is in
958 if (!rw_tryenter(&odn
->dn_struct_rwlock
, RW_WRITER
)) {
959 zrl_exit(&odn
->dn_handle
->dnh_zrlock
);
960 mutex_exit(&os
->os_lock
);
961 DNODE_STAT_BUMP(dnode_move_rwlock
);
962 return (KMEM_CBRC_LATER
);
966 * A dbuf may be removed (evicted) without an active dnode hold. In that
967 * case, the dbuf count is decremented under the handle lock before the
968 * dbuf's hold is released. This order ensures that if we count the hold
969 * after the dbuf is removed but before its hold is released, we will
970 * treat the unmatched hold as active and exit safely. If we count the
971 * hold before the dbuf is removed, the hold is discounted, and the
972 * removal is blocked until the move completes.
974 refcount
= refcount_count(&odn
->dn_holds
);
975 ASSERT(refcount
>= 0);
976 dbufs
= odn
->dn_dbufs_count
;
978 /* We can't have more dbufs than dnode holds. */
979 ASSERT3U(dbufs
, <=, refcount
);
980 DTRACE_PROBE3(dnode__move
, dnode_t
*, odn
, int64_t, refcount
,
983 if (refcount
> dbufs
) {
984 rw_exit(&odn
->dn_struct_rwlock
);
985 zrl_exit(&odn
->dn_handle
->dnh_zrlock
);
986 mutex_exit(&os
->os_lock
);
987 DNODE_STAT_BUMP(dnode_move_active
);
988 return (KMEM_CBRC_LATER
);
991 rw_exit(&odn
->dn_struct_rwlock
);
994 * At this point we know that anyone with a hold on the dnode is not
995 * actively referencing it. The dnode is known and in a valid state to
996 * move. We're holding the locks needed to execute the critical section.
998 dnode_move_impl(odn
, ndn
);
1000 list_link_replace(&odn
->dn_link
, &ndn
->dn_link
);
1001 /* If the dnode was safe to move, the refcount cannot have changed. */
1002 ASSERT(refcount
== refcount_count(&ndn
->dn_holds
));
1003 ASSERT(dbufs
== ndn
->dn_dbufs_count
);
1004 zrl_exit(&ndn
->dn_handle
->dnh_zrlock
); /* handle has moved */
1005 mutex_exit(&os
->os_lock
);
1007 return (KMEM_CBRC_YES
);
1009 #endif /* _KERNEL */
1012 dnode_slots_hold(dnode_children_t
*children
, int idx
, int slots
)
1014 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1016 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1017 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1018 zrl_add(&dnh
->dnh_zrlock
);
1023 dnode_slots_rele(dnode_children_t
*children
, int idx
, int slots
)
1025 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1027 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1028 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1030 if (zrl_is_locked(&dnh
->dnh_zrlock
))
1031 zrl_exit(&dnh
->dnh_zrlock
);
1033 zrl_remove(&dnh
->dnh_zrlock
);
1038 dnode_slots_tryenter(dnode_children_t
*children
, int idx
, int slots
)
1040 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1042 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1043 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1045 if (!zrl_tryenter(&dnh
->dnh_zrlock
)) {
1046 for (int j
= idx
; j
< i
; j
++) {
1047 dnh
= &children
->dnc_children
[j
];
1048 zrl_exit(&dnh
->dnh_zrlock
);
1059 dnode_set_slots(dnode_children_t
*children
, int idx
, int slots
, void *ptr
)
1061 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1063 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1064 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1065 dnh
->dnh_dnode
= ptr
;
1070 dnode_check_slots(dnode_children_t
*children
, int idx
, int slots
, void *ptr
)
1072 ASSERT3S(idx
+ slots
, <=, DNODES_PER_BLOCK
);
1074 for (int i
= idx
; i
< idx
+ slots
; i
++) {
1075 dnode_handle_t
*dnh
= &children
->dnc_children
[i
];
1076 if (dnh
->dnh_dnode
!= ptr
)
1084 dnode_special_close(dnode_handle_t
*dnh
)
1086 dnode_t
*dn
= dnh
->dnh_dnode
;
1089 * Wait for final references to the dnode to clear. This can
1090 * only happen if the arc is asynchronously evicting state that
1091 * has a hold on this dnode while we are trying to evict this
1094 while (refcount_count(&dn
->dn_holds
) > 0)
1096 ASSERT(dn
->dn_dbuf
== NULL
||
1097 dmu_buf_get_user(&dn
->dn_dbuf
->db
) == NULL
);
1098 zrl_add(&dnh
->dnh_zrlock
);
1099 dnode_destroy(dn
); /* implicit zrl_remove() */
1100 zrl_destroy(&dnh
->dnh_zrlock
);
1101 dnh
->dnh_dnode
= NULL
;
1105 dnode_special_open(objset_t
*os
, dnode_phys_t
*dnp
, uint64_t object
,
1106 dnode_handle_t
*dnh
)
1110 zrl_init(&dnh
->dnh_zrlock
);
1111 zrl_tryenter(&dnh
->dnh_zrlock
);
1113 dn
= dnode_create(os
, dnp
, NULL
, object
, dnh
);
1116 zrl_exit(&dnh
->dnh_zrlock
);
1120 dnode_buf_evict_async(void *dbu
)
1122 dnode_children_t
*dnc
= dbu
;
1124 DNODE_STAT_BUMP(dnode_buf_evict
);
1126 for (int i
= 0; i
< dnc
->dnc_count
; i
++) {
1127 dnode_handle_t
*dnh
= &dnc
->dnc_children
[i
];
1131 * The dnode handle lock guards against the dnode moving to
1132 * another valid address, so there is no need here to guard
1133 * against changes to or from NULL.
1135 if (!DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1136 zrl_destroy(&dnh
->dnh_zrlock
);
1137 dnh
->dnh_dnode
= DN_SLOT_UNINIT
;
1141 zrl_add(&dnh
->dnh_zrlock
);
1142 dn
= dnh
->dnh_dnode
;
1144 * If there are holds on this dnode, then there should
1145 * be holds on the dnode's containing dbuf as well; thus
1146 * it wouldn't be eligible for eviction and this function
1147 * would not have been called.
1149 ASSERT(refcount_is_zero(&dn
->dn_holds
));
1150 ASSERT(refcount_is_zero(&dn
->dn_tx_holds
));
1152 dnode_destroy(dn
); /* implicit zrl_remove() for first slot */
1153 zrl_destroy(&dnh
->dnh_zrlock
);
1154 dnh
->dnh_dnode
= DN_SLOT_UNINIT
;
1156 kmem_free(dnc
, sizeof (dnode_children_t
) +
1157 dnc
->dnc_count
* sizeof (dnode_handle_t
));
1161 * When the DNODE_MUST_BE_FREE flag is set, the "slots" parameter is used
1162 * to ensure the hole at the specified object offset is large enough to
1163 * hold the dnode being created. The slots parameter is also used to ensure
1164 * a dnode does not span multiple dnode blocks. In both of these cases, if
1165 * a failure occurs, ENOSPC is returned. Keep in mind, these failure cases
1166 * are only possible when using DNODE_MUST_BE_FREE.
1168 * If the DNODE_MUST_BE_ALLOCATED flag is set, "slots" must be 0.
1169 * dnode_hold_impl() will check if the requested dnode is already consumed
1170 * as an extra dnode slot by an large dnode, in which case it returns
1174 * EINVAL - Invalid object number or flags.
1175 * ENOSPC - Hole too small to fulfill "slots" request (DNODE_MUST_BE_FREE)
1176 * EEXIST - Refers to an allocated dnode (DNODE_MUST_BE_FREE)
1177 * - Refers to an interior dnode slot (DNODE_MUST_BE_ALLOCATED)
1178 * ENOENT - The requested dnode is not allocated (DNODE_MUST_BE_ALLOCATED)
1179 * EIO - I/O error when reading the meta dnode dbuf.
1181 * succeeds even for free dnodes.
1184 dnode_hold_impl(objset_t
*os
, uint64_t object
, int flag
, int slots
,
1185 void *tag
, dnode_t
**dnp
)
1188 int drop_struct_lock
= FALSE
;
1193 dnode_children_t
*dnc
;
1194 dnode_phys_t
*dn_block
;
1195 dnode_handle_t
*dnh
;
1197 ASSERT(!(flag
& DNODE_MUST_BE_ALLOCATED
) || (slots
== 0));
1198 ASSERT(!(flag
& DNODE_MUST_BE_FREE
) || (slots
> 0));
1201 * If you are holding the spa config lock as writer, you shouldn't
1202 * be asking the DMU to do *anything* unless it's the root pool
1203 * which may require us to read from the root filesystem while
1204 * holding some (not all) of the locks as writer.
1206 ASSERT(spa_config_held(os
->os_spa
, SCL_ALL
, RW_WRITER
) == 0 ||
1207 (spa_is_root(os
->os_spa
) &&
1208 spa_config_held(os
->os_spa
, SCL_STATE
, RW_WRITER
)));
1210 if (object
== DMU_USERUSED_OBJECT
|| object
== DMU_GROUPUSED_OBJECT
) {
1211 dn
= (object
== DMU_USERUSED_OBJECT
) ?
1212 DMU_USERUSED_DNODE(os
) : DMU_GROUPUSED_DNODE(os
);
1214 return (SET_ERROR(ENOENT
));
1216 if ((flag
& DNODE_MUST_BE_ALLOCATED
) && type
== DMU_OT_NONE
)
1217 return (SET_ERROR(ENOENT
));
1218 if ((flag
& DNODE_MUST_BE_FREE
) && type
!= DMU_OT_NONE
)
1219 return (SET_ERROR(EEXIST
));
1221 (void) refcount_add(&dn
->dn_holds
, tag
);
1226 if (object
== 0 || object
>= DN_MAX_OBJECT
)
1227 return (SET_ERROR(EINVAL
));
1229 mdn
= DMU_META_DNODE(os
);
1230 ASSERT(mdn
->dn_object
== DMU_META_DNODE_OBJECT
);
1234 if (!RW_WRITE_HELD(&mdn
->dn_struct_rwlock
)) {
1235 rw_enter(&mdn
->dn_struct_rwlock
, RW_READER
);
1236 drop_struct_lock
= TRUE
;
1239 blk
= dbuf_whichblock(mdn
, 0, object
* sizeof (dnode_phys_t
));
1241 db
= dbuf_hold(mdn
, blk
, FTAG
);
1242 if (drop_struct_lock
)
1243 rw_exit(&mdn
->dn_struct_rwlock
);
1245 DNODE_STAT_BUMP(dnode_hold_dbuf_hold
);
1246 return (SET_ERROR(EIO
));
1250 * We do not need to decrypt to read the dnode so it doesn't matter
1251 * if we get the encrypted or decrypted version.
1253 err
= dbuf_read(db
, NULL
, DB_RF_CANFAIL
| DB_RF_NO_DECRYPT
);
1255 DNODE_STAT_BUMP(dnode_hold_dbuf_read
);
1256 dbuf_rele(db
, FTAG
);
1260 ASSERT3U(db
->db
.db_size
, >=, 1<<DNODE_SHIFT
);
1261 epb
= db
->db
.db_size
>> DNODE_SHIFT
;
1263 idx
= object
& (epb
- 1);
1264 dn_block
= (dnode_phys_t
*)db
->db
.db_data
;
1266 ASSERT(DB_DNODE(db
)->dn_type
== DMU_OT_DNODE
);
1267 dnc
= dmu_buf_get_user(&db
->db
);
1270 dnode_children_t
*winner
;
1273 dnc
= kmem_zalloc(sizeof (dnode_children_t
) +
1274 epb
* sizeof (dnode_handle_t
), KM_SLEEP
);
1275 dnc
->dnc_count
= epb
;
1276 dnh
= &dnc
->dnc_children
[0];
1278 /* Initialize dnode slot status from dnode_phys_t */
1279 for (int i
= 0; i
< epb
; i
++) {
1280 zrl_init(&dnh
[i
].dnh_zrlock
);
1287 if (dn_block
[i
].dn_type
!= DMU_OT_NONE
) {
1288 int interior
= dn_block
[i
].dn_extra_slots
;
1290 dnode_set_slots(dnc
, i
, 1, DN_SLOT_ALLOCATED
);
1291 dnode_set_slots(dnc
, i
+ 1, interior
,
1295 dnh
[i
].dnh_dnode
= DN_SLOT_FREE
;
1300 dmu_buf_init_user(&dnc
->dnc_dbu
, NULL
,
1301 dnode_buf_evict_async
, NULL
);
1302 winner
= dmu_buf_set_user(&db
->db
, &dnc
->dnc_dbu
);
1303 if (winner
!= NULL
) {
1305 for (int i
= 0; i
< epb
; i
++)
1306 zrl_destroy(&dnh
[i
].dnh_zrlock
);
1308 kmem_free(dnc
, sizeof (dnode_children_t
) +
1309 epb
* sizeof (dnode_handle_t
));
1314 ASSERT(dnc
->dnc_count
== epb
);
1315 dn
= DN_SLOT_UNINIT
;
1317 if (flag
& DNODE_MUST_BE_ALLOCATED
) {
1320 while (dn
== DN_SLOT_UNINIT
) {
1321 dnode_slots_hold(dnc
, idx
, slots
);
1322 dnh
= &dnc
->dnc_children
[idx
];
1324 if (DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1325 dn
= dnh
->dnh_dnode
;
1327 } else if (dnh
->dnh_dnode
== DN_SLOT_INTERIOR
) {
1328 DNODE_STAT_BUMP(dnode_hold_alloc_interior
);
1329 dnode_slots_rele(dnc
, idx
, slots
);
1330 dbuf_rele(db
, FTAG
);
1331 return (SET_ERROR(EEXIST
));
1332 } else if (dnh
->dnh_dnode
!= DN_SLOT_ALLOCATED
) {
1333 DNODE_STAT_BUMP(dnode_hold_alloc_misses
);
1334 dnode_slots_rele(dnc
, idx
, slots
);
1335 dbuf_rele(db
, FTAG
);
1336 return (SET_ERROR(ENOENT
));
1339 dnode_slots_rele(dnc
, idx
, slots
);
1340 if (!dnode_slots_tryenter(dnc
, idx
, slots
)) {
1341 DNODE_STAT_BUMP(dnode_hold_alloc_lock_retry
);
1346 * Someone else won the race and called dnode_create()
1347 * after we checked DN_SLOT_IS_PTR() above but before
1348 * we acquired the lock.
1350 if (DN_SLOT_IS_PTR(dnh
->dnh_dnode
)) {
1351 DNODE_STAT_BUMP(dnode_hold_alloc_lock_misses
);
1352 dn
= dnh
->dnh_dnode
;
1354 dn
= dnode_create(os
, dn_block
+ idx
, db
,
1359 mutex_enter(&dn
->dn_mtx
);
1360 if (dn
->dn_type
== DMU_OT_NONE
) {
1361 DNODE_STAT_BUMP(dnode_hold_alloc_type_none
);
1362 mutex_exit(&dn
->dn_mtx
);
1363 dnode_slots_rele(dnc
, idx
, slots
);
1364 dbuf_rele(db
, FTAG
);
1365 return (SET_ERROR(ENOENT
));
1368 DNODE_STAT_BUMP(dnode_hold_alloc_hits
);
1369 } else if (flag
& DNODE_MUST_BE_FREE
) {
1371 if (idx
+ slots
- 1 >= DNODES_PER_BLOCK
) {
1372 DNODE_STAT_BUMP(dnode_hold_free_overflow
);
1373 dbuf_rele(db
, FTAG
);
1374 return (SET_ERROR(ENOSPC
));
1377 while (dn
== DN_SLOT_UNINIT
) {
1378 dnode_slots_hold(dnc
, idx
, slots
);
1380 if (!dnode_check_slots(dnc
, idx
, slots
, DN_SLOT_FREE
)) {
1381 DNODE_STAT_BUMP(dnode_hold_free_misses
);
1382 dnode_slots_rele(dnc
, idx
, slots
);
1383 dbuf_rele(db
, FTAG
);
1384 return (SET_ERROR(ENOSPC
));
1387 dnode_slots_rele(dnc
, idx
, slots
);
1388 if (!dnode_slots_tryenter(dnc
, idx
, slots
)) {
1389 DNODE_STAT_BUMP(dnode_hold_free_lock_retry
);
1393 if (!dnode_check_slots(dnc
, idx
, slots
, DN_SLOT_FREE
)) {
1394 DNODE_STAT_BUMP(dnode_hold_free_lock_misses
);
1395 dnode_slots_rele(dnc
, idx
, slots
);
1396 dbuf_rele(db
, FTAG
);
1397 return (SET_ERROR(ENOSPC
));
1400 dnh
= &dnc
->dnc_children
[idx
];
1401 dn
= dnode_create(os
, dn_block
+ idx
, db
, object
, dnh
);
1404 mutex_enter(&dn
->dn_mtx
);
1405 if (!refcount_is_zero(&dn
->dn_holds
)) {
1406 DNODE_STAT_BUMP(dnode_hold_free_refcount
);
1407 mutex_exit(&dn
->dn_mtx
);
1408 dnode_slots_rele(dnc
, idx
, slots
);
1409 dbuf_rele(db
, FTAG
);
1410 return (SET_ERROR(EEXIST
));
1413 dnode_set_slots(dnc
, idx
+ 1, slots
- 1, DN_SLOT_INTERIOR
);
1414 DNODE_STAT_BUMP(dnode_hold_free_hits
);
1416 dbuf_rele(db
, FTAG
);
1417 return (SET_ERROR(EINVAL
));
1420 if (dn
->dn_free_txg
) {
1421 DNODE_STAT_BUMP(dnode_hold_free_txg
);
1423 mutex_exit(&dn
->dn_mtx
);
1424 dnode_slots_rele(dnc
, idx
, slots
);
1425 dbuf_rele(db
, FTAG
);
1426 return (SET_ERROR(type
== DMU_OT_NONE
? ENOENT
: EEXIST
));
1429 if (refcount_add(&dn
->dn_holds
, tag
) == 1)
1430 dbuf_add_ref(db
, dnh
);
1432 mutex_exit(&dn
->dn_mtx
);
1434 /* Now we can rely on the hold to prevent the dnode from moving. */
1435 dnode_slots_rele(dnc
, idx
, slots
);
1438 ASSERT3P(dn
->dn_dbuf
, ==, db
);
1439 ASSERT3U(dn
->dn_object
, ==, object
);
1440 dbuf_rele(db
, FTAG
);
1447 * Return held dnode if the object is allocated, NULL if not.
1450 dnode_hold(objset_t
*os
, uint64_t object
, void *tag
, dnode_t
**dnp
)
1452 return (dnode_hold_impl(os
, object
, DNODE_MUST_BE_ALLOCATED
, 0, tag
,
1457 * Can only add a reference if there is already at least one
1458 * reference on the dnode. Returns FALSE if unable to add a
1462 dnode_add_ref(dnode_t
*dn
, void *tag
)
1464 mutex_enter(&dn
->dn_mtx
);
1465 if (refcount_is_zero(&dn
->dn_holds
)) {
1466 mutex_exit(&dn
->dn_mtx
);
1469 VERIFY(1 < refcount_add(&dn
->dn_holds
, tag
));
1470 mutex_exit(&dn
->dn_mtx
);
1475 dnode_rele(dnode_t
*dn
, void *tag
)
1477 mutex_enter(&dn
->dn_mtx
);
1478 dnode_rele_and_unlock(dn
, tag
);
1482 dnode_rele_and_unlock(dnode_t
*dn
, void *tag
)
1485 /* Get while the hold prevents the dnode from moving. */
1486 dmu_buf_impl_t
*db
= dn
->dn_dbuf
;
1487 dnode_handle_t
*dnh
= dn
->dn_handle
;
1489 refs
= refcount_remove(&dn
->dn_holds
, tag
);
1490 mutex_exit(&dn
->dn_mtx
);
1493 * It's unsafe to release the last hold on a dnode by dnode_rele() or
1494 * indirectly by dbuf_rele() while relying on the dnode handle to
1495 * prevent the dnode from moving, since releasing the last hold could
1496 * result in the dnode's parent dbuf evicting its dnode handles. For
1497 * that reason anyone calling dnode_rele() or dbuf_rele() without some
1498 * other direct or indirect hold on the dnode must first drop the dnode
1501 ASSERT(refs
> 0 || dnh
->dnh_zrlock
.zr_owner
!= curthread
);
1503 /* NOTE: the DNODE_DNODE does not have a dn_dbuf */
1504 if (refs
== 0 && db
!= NULL
) {
1506 * Another thread could add a hold to the dnode handle in
1507 * dnode_hold_impl() while holding the parent dbuf. Since the
1508 * hold on the parent dbuf prevents the handle from being
1509 * destroyed, the hold on the handle is OK. We can't yet assert
1510 * that the handle has zero references, but that will be
1511 * asserted anyway when the handle gets destroyed.
1518 dnode_setdirty(dnode_t
*dn
, dmu_tx_t
*tx
)
1520 objset_t
*os
= dn
->dn_objset
;
1521 uint64_t txg
= tx
->tx_txg
;
1523 if (DMU_OBJECT_IS_SPECIAL(dn
->dn_object
)) {
1524 dsl_dataset_dirty(os
->os_dsl_dataset
, tx
);
1531 mutex_enter(&dn
->dn_mtx
);
1532 ASSERT(dn
->dn_phys
->dn_type
|| dn
->dn_allocated_txg
);
1533 ASSERT(dn
->dn_free_txg
== 0 || dn
->dn_free_txg
>= txg
);
1534 mutex_exit(&dn
->dn_mtx
);
1538 * Determine old uid/gid when necessary
1540 dmu_objset_userquota_get_ids(dn
, B_TRUE
, tx
);
1542 multilist_t
*dirtylist
= os
->os_dirty_dnodes
[txg
& TXG_MASK
];
1543 multilist_sublist_t
*mls
= multilist_sublist_lock_obj(dirtylist
, dn
);
1546 * If we are already marked dirty, we're done.
1548 if (list_link_active(&dn
->dn_dirty_link
[txg
& TXG_MASK
])) {
1549 multilist_sublist_unlock(mls
);
1553 ASSERT(!refcount_is_zero(&dn
->dn_holds
) ||
1554 !avl_is_empty(&dn
->dn_dbufs
));
1555 ASSERT(dn
->dn_datablksz
!= 0);
1556 ASSERT0(dn
->dn_next_bonuslen
[txg
&TXG_MASK
]);
1557 ASSERT0(dn
->dn_next_blksz
[txg
&TXG_MASK
]);
1558 ASSERT0(dn
->dn_next_bonustype
[txg
&TXG_MASK
]);
1560 dprintf_ds(os
->os_dsl_dataset
, "obj=%llu txg=%llu\n",
1561 dn
->dn_object
, txg
);
1563 multilist_sublist_insert_head(mls
, dn
);
1565 multilist_sublist_unlock(mls
);
1568 * The dnode maintains a hold on its containing dbuf as
1569 * long as there are holds on it. Each instantiated child
1570 * dbuf maintains a hold on the dnode. When the last child
1571 * drops its hold, the dnode will drop its hold on the
1572 * containing dbuf. We add a "dirty hold" here so that the
1573 * dnode will hang around after we finish processing its
1576 VERIFY(dnode_add_ref(dn
, (void *)(uintptr_t)tx
->tx_txg
));
1578 (void) dbuf_dirty(dn
->dn_dbuf
, tx
);
1580 dsl_dataset_dirty(os
->os_dsl_dataset
, tx
);
1584 dnode_free(dnode_t
*dn
, dmu_tx_t
*tx
)
1586 mutex_enter(&dn
->dn_mtx
);
1587 if (dn
->dn_type
== DMU_OT_NONE
|| dn
->dn_free_txg
) {
1588 mutex_exit(&dn
->dn_mtx
);
1591 dn
->dn_free_txg
= tx
->tx_txg
;
1592 mutex_exit(&dn
->dn_mtx
);
1594 dnode_setdirty(dn
, tx
);
1598 * Try to change the block size for the indicated dnode. This can only
1599 * succeed if there are no blocks allocated or dirty beyond first block
1602 dnode_set_blksz(dnode_t
*dn
, uint64_t size
, int ibs
, dmu_tx_t
*tx
)
1607 ASSERT3U(size
, <=, spa_maxblocksize(dmu_objset_spa(dn
->dn_objset
)));
1609 size
= SPA_MINBLOCKSIZE
;
1611 size
= P2ROUNDUP(size
, SPA_MINBLOCKSIZE
);
1613 if (ibs
== dn
->dn_indblkshift
)
1616 if (size
>> SPA_MINBLOCKSHIFT
== dn
->dn_datablkszsec
&& ibs
== 0)
1619 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1621 /* Check for any allocated blocks beyond the first */
1622 if (dn
->dn_maxblkid
!= 0)
1625 mutex_enter(&dn
->dn_dbufs_mtx
);
1626 for (db
= avl_first(&dn
->dn_dbufs
); db
!= NULL
;
1627 db
= AVL_NEXT(&dn
->dn_dbufs
, db
)) {
1628 if (db
->db_blkid
!= 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1629 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1630 mutex_exit(&dn
->dn_dbufs_mtx
);
1634 mutex_exit(&dn
->dn_dbufs_mtx
);
1636 if (ibs
&& dn
->dn_nlevels
!= 1)
1639 /* resize the old block */
1640 err
= dbuf_hold_impl(dn
, 0, 0, TRUE
, FALSE
, FTAG
, &db
);
1642 dbuf_new_size(db
, size
, tx
);
1643 else if (err
!= ENOENT
)
1646 dnode_setdblksz(dn
, size
);
1647 dnode_setdirty(dn
, tx
);
1648 dn
->dn_next_blksz
[tx
->tx_txg
&TXG_MASK
] = size
;
1650 dn
->dn_indblkshift
= ibs
;
1651 dn
->dn_next_indblkshift
[tx
->tx_txg
&TXG_MASK
] = ibs
;
1653 /* rele after we have fixed the blocksize in the dnode */
1655 dbuf_rele(db
, FTAG
);
1657 rw_exit(&dn
->dn_struct_rwlock
);
1661 rw_exit(&dn
->dn_struct_rwlock
);
1662 return (SET_ERROR(ENOTSUP
));
1666 dnode_set_nlevels_impl(dnode_t
*dn
, int new_nlevels
, dmu_tx_t
*tx
)
1668 uint64_t txgoff
= tx
->tx_txg
& TXG_MASK
;
1669 int old_nlevels
= dn
->dn_nlevels
;
1672 dbuf_dirty_record_t
*new, *dr
, *dr_next
;
1674 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1676 dn
->dn_nlevels
= new_nlevels
;
1678 ASSERT3U(new_nlevels
, >, dn
->dn_next_nlevels
[txgoff
]);
1679 dn
->dn_next_nlevels
[txgoff
] = new_nlevels
;
1681 /* dirty the left indirects */
1682 db
= dbuf_hold_level(dn
, old_nlevels
, 0, FTAG
);
1684 new = dbuf_dirty(db
, tx
);
1685 dbuf_rele(db
, FTAG
);
1687 /* transfer the dirty records to the new indirect */
1688 mutex_enter(&dn
->dn_mtx
);
1689 mutex_enter(&new->dt
.di
.dr_mtx
);
1690 list
= &dn
->dn_dirty_records
[txgoff
];
1691 for (dr
= list_head(list
); dr
; dr
= dr_next
) {
1692 dr_next
= list_next(&dn
->dn_dirty_records
[txgoff
], dr
);
1693 if (dr
->dr_dbuf
->db_level
!= new_nlevels
-1 &&
1694 dr
->dr_dbuf
->db_blkid
!= DMU_BONUS_BLKID
&&
1695 dr
->dr_dbuf
->db_blkid
!= DMU_SPILL_BLKID
) {
1696 ASSERT(dr
->dr_dbuf
->db_level
== old_nlevels
-1);
1697 list_remove(&dn
->dn_dirty_records
[txgoff
], dr
);
1698 list_insert_tail(&new->dt
.di
.dr_children
, dr
);
1699 dr
->dr_parent
= new;
1702 mutex_exit(&new->dt
.di
.dr_mtx
);
1703 mutex_exit(&dn
->dn_mtx
);
1707 dnode_set_nlevels(dnode_t
*dn
, int nlevels
, dmu_tx_t
*tx
)
1711 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1713 if (dn
->dn_nlevels
== nlevels
) {
1716 } else if (nlevels
< dn
->dn_nlevels
) {
1717 ret
= SET_ERROR(EINVAL
);
1721 dnode_set_nlevels_impl(dn
, nlevels
, tx
);
1724 rw_exit(&dn
->dn_struct_rwlock
);
1728 /* read-holding callers must not rely on the lock being continuously held */
1730 dnode_new_blkid(dnode_t
*dn
, uint64_t blkid
, dmu_tx_t
*tx
, boolean_t have_read
)
1732 int epbs
, new_nlevels
;
1735 ASSERT(blkid
!= DMU_BONUS_BLKID
);
1738 RW_READ_HELD(&dn
->dn_struct_rwlock
) :
1739 RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1742 * if we have a read-lock, check to see if we need to do any work
1743 * before upgrading to a write-lock.
1746 if (blkid
<= dn
->dn_maxblkid
)
1749 if (!rw_tryupgrade(&dn
->dn_struct_rwlock
)) {
1750 rw_exit(&dn
->dn_struct_rwlock
);
1751 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1755 if (blkid
<= dn
->dn_maxblkid
)
1758 dn
->dn_maxblkid
= blkid
;
1759 dn
->dn_next_maxblkid
[tx
->tx_txg
& TXG_MASK
] = blkid
;
1762 * Compute the number of levels necessary to support the new maxblkid.
1765 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1766 for (sz
= dn
->dn_nblkptr
;
1767 sz
<= blkid
&& sz
>= dn
->dn_nblkptr
; sz
<<= epbs
)
1770 ASSERT3U(new_nlevels
, <=, DN_MAX_LEVELS
);
1772 if (new_nlevels
> dn
->dn_nlevels
)
1773 dnode_set_nlevels_impl(dn
, new_nlevels
, tx
);
1777 rw_downgrade(&dn
->dn_struct_rwlock
);
1781 dnode_dirty_l1(dnode_t
*dn
, uint64_t l1blkid
, dmu_tx_t
*tx
)
1783 dmu_buf_impl_t
*db
= dbuf_hold_level(dn
, 1, l1blkid
, FTAG
);
1785 dmu_buf_will_dirty(&db
->db
, tx
);
1786 dbuf_rele(db
, FTAG
);
1791 dnode_free_range(dnode_t
*dn
, uint64_t off
, uint64_t len
, dmu_tx_t
*tx
)
1794 uint64_t blkoff
, blkid
, nblks
;
1795 int blksz
, blkshift
, head
, tail
;
1799 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1800 blksz
= dn
->dn_datablksz
;
1801 blkshift
= dn
->dn_datablkshift
;
1802 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1804 if (len
== DMU_OBJECT_END
) {
1805 len
= UINT64_MAX
- off
;
1810 * First, block align the region to free:
1813 head
= P2NPHASE(off
, blksz
);
1814 blkoff
= P2PHASE(off
, blksz
);
1815 if ((off
>> blkshift
) > dn
->dn_maxblkid
)
1818 ASSERT(dn
->dn_maxblkid
== 0);
1819 if (off
== 0 && len
>= blksz
) {
1821 * Freeing the whole block; fast-track this request.
1822 * Note that we won't dirty any indirect blocks,
1823 * which is fine because we will be freeing the entire
1824 * file and thus all indirect blocks will be freed
1825 * by free_children().
1830 } else if (off
>= blksz
) {
1831 /* Freeing past end-of-data */
1834 /* Freeing part of the block. */
1836 ASSERT3U(head
, >, 0);
1840 /* zero out any partial block data at the start of the range */
1842 ASSERT3U(blkoff
+ head
, ==, blksz
);
1845 if (dbuf_hold_impl(dn
, 0, dbuf_whichblock(dn
, 0, off
),
1846 TRUE
, FALSE
, FTAG
, &db
) == 0) {
1849 /* don't dirty if it isn't on disk and isn't dirty */
1850 if (db
->db_last_dirty
||
1851 (db
->db_blkptr
&& !BP_IS_HOLE(db
->db_blkptr
))) {
1852 rw_exit(&dn
->dn_struct_rwlock
);
1853 dmu_buf_will_dirty(&db
->db
, tx
);
1854 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1855 data
= db
->db
.db_data
;
1856 bzero(data
+ blkoff
, head
);
1858 dbuf_rele(db
, FTAG
);
1864 /* If the range was less than one block, we're done */
1868 /* If the remaining range is past end of file, we're done */
1869 if ((off
>> blkshift
) > dn
->dn_maxblkid
)
1872 ASSERT(ISP2(blksz
));
1876 tail
= P2PHASE(len
, blksz
);
1878 ASSERT0(P2PHASE(off
, blksz
));
1879 /* zero out any partial block data at the end of the range */
1883 if (dbuf_hold_impl(dn
, 0, dbuf_whichblock(dn
, 0, off
+len
),
1884 TRUE
, FALSE
, FTAG
, &db
) == 0) {
1885 /* don't dirty if not on disk and not dirty */
1886 if (db
->db_last_dirty
||
1887 (db
->db_blkptr
&& !BP_IS_HOLE(db
->db_blkptr
))) {
1888 rw_exit(&dn
->dn_struct_rwlock
);
1889 dmu_buf_will_dirty(&db
->db
, tx
);
1890 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1891 bzero(db
->db
.db_data
, tail
);
1893 dbuf_rele(db
, FTAG
);
1898 /* If the range did not include a full block, we are done */
1902 ASSERT(IS_P2ALIGNED(off
, blksz
));
1903 ASSERT(trunc
|| IS_P2ALIGNED(len
, blksz
));
1904 blkid
= off
>> blkshift
;
1905 nblks
= len
>> blkshift
;
1910 * Dirty all the indirect blocks in this range. Note that only
1911 * the first and last indirect blocks can actually be written
1912 * (if they were partially freed) -- they must be dirtied, even if
1913 * they do not exist on disk yet. The interior blocks will
1914 * be freed by free_children(), so they will not actually be written.
1915 * Even though these interior blocks will not be written, we
1916 * dirty them for two reasons:
1918 * - It ensures that the indirect blocks remain in memory until
1919 * syncing context. (They have already been prefetched by
1920 * dmu_tx_hold_free(), so we don't have to worry about reading
1921 * them serially here.)
1923 * - The dirty space accounting will put pressure on the txg sync
1924 * mechanism to begin syncing, and to delay transactions if there
1925 * is a large amount of freeing. Even though these indirect
1926 * blocks will not be written, we could need to write the same
1927 * amount of space if we copy the freed BPs into deadlists.
1929 if (dn
->dn_nlevels
> 1) {
1930 uint64_t first
, last
;
1932 first
= blkid
>> epbs
;
1933 dnode_dirty_l1(dn
, first
, tx
);
1935 last
= dn
->dn_maxblkid
>> epbs
;
1937 last
= (blkid
+ nblks
- 1) >> epbs
;
1939 dnode_dirty_l1(dn
, last
, tx
);
1941 int shift
= dn
->dn_datablkshift
+ dn
->dn_indblkshift
-
1943 for (uint64_t i
= first
+ 1; i
< last
; i
++) {
1945 * Set i to the blockid of the next non-hole
1946 * level-1 indirect block at or after i. Note
1947 * that dnode_next_offset() operates in terms of
1948 * level-0-equivalent bytes.
1950 uint64_t ibyte
= i
<< shift
;
1951 int err
= dnode_next_offset(dn
, DNODE_FIND_HAVELOCK
,
1958 * Normally we should not see an error, either
1959 * from dnode_next_offset() or dbuf_hold_level()
1960 * (except for ESRCH from dnode_next_offset).
1961 * If there is an i/o error, then when we read
1962 * this block in syncing context, it will use
1963 * ZIO_FLAG_MUSTSUCCEED, and thus hang/panic according
1964 * to the "failmode" property. dnode_next_offset()
1965 * doesn't have a flag to indicate MUSTSUCCEED.
1970 dnode_dirty_l1(dn
, i
, tx
);
1976 * Add this range to the dnode range list.
1977 * We will finish up this free operation in the syncing phase.
1979 mutex_enter(&dn
->dn_mtx
);
1981 int txgoff
= tx
->tx_txg
& TXG_MASK
;
1982 if (dn
->dn_free_ranges
[txgoff
] == NULL
) {
1983 dn
->dn_free_ranges
[txgoff
] =
1984 range_tree_create(NULL
, NULL
, &dn
->dn_mtx
);
1986 range_tree_clear(dn
->dn_free_ranges
[txgoff
], blkid
, nblks
);
1987 range_tree_add(dn
->dn_free_ranges
[txgoff
], blkid
, nblks
);
1989 dprintf_dnode(dn
, "blkid=%llu nblks=%llu txg=%llu\n",
1990 blkid
, nblks
, tx
->tx_txg
);
1991 mutex_exit(&dn
->dn_mtx
);
1993 dbuf_free_range(dn
, blkid
, blkid
+ nblks
- 1, tx
);
1994 dnode_setdirty(dn
, tx
);
1997 rw_exit(&dn
->dn_struct_rwlock
);
2001 dnode_spill_freed(dnode_t
*dn
)
2005 mutex_enter(&dn
->dn_mtx
);
2006 for (i
= 0; i
< TXG_SIZE
; i
++) {
2007 if (dn
->dn_rm_spillblk
[i
] == DN_KILL_SPILLBLK
)
2010 mutex_exit(&dn
->dn_mtx
);
2011 return (i
< TXG_SIZE
);
2014 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
2016 dnode_block_freed(dnode_t
*dn
, uint64_t blkid
)
2018 void *dp
= spa_get_dsl(dn
->dn_objset
->os_spa
);
2021 if (blkid
== DMU_BONUS_BLKID
)
2025 * If we're in the process of opening the pool, dp will not be
2026 * set yet, but there shouldn't be anything dirty.
2031 if (dn
->dn_free_txg
)
2034 if (blkid
== DMU_SPILL_BLKID
)
2035 return (dnode_spill_freed(dn
));
2037 mutex_enter(&dn
->dn_mtx
);
2038 for (i
= 0; i
< TXG_SIZE
; i
++) {
2039 if (dn
->dn_free_ranges
[i
] != NULL
&&
2040 range_tree_contains(dn
->dn_free_ranges
[i
], blkid
, 1))
2043 mutex_exit(&dn
->dn_mtx
);
2044 return (i
< TXG_SIZE
);
2047 /* call from syncing context when we actually write/free space for this dnode */
2049 dnode_diduse_space(dnode_t
*dn
, int64_t delta
)
2052 dprintf_dnode(dn
, "dn=%p dnp=%p used=%llu delta=%lld\n",
2054 (u_longlong_t
)dn
->dn_phys
->dn_used
,
2057 mutex_enter(&dn
->dn_mtx
);
2058 space
= DN_USED_BYTES(dn
->dn_phys
);
2060 ASSERT3U(space
+ delta
, >=, space
); /* no overflow */
2062 ASSERT3U(space
, >=, -delta
); /* no underflow */
2065 if (spa_version(dn
->dn_objset
->os_spa
) < SPA_VERSION_DNODE_BYTES
) {
2066 ASSERT((dn
->dn_phys
->dn_flags
& DNODE_FLAG_USED_BYTES
) == 0);
2067 ASSERT0(P2PHASE(space
, 1<<DEV_BSHIFT
));
2068 dn
->dn_phys
->dn_used
= space
>> DEV_BSHIFT
;
2070 dn
->dn_phys
->dn_used
= space
;
2071 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_USED_BYTES
;
2073 mutex_exit(&dn
->dn_mtx
);
2077 * Scans a block at the indicated "level" looking for a hole or data,
2078 * depending on 'flags'.
2080 * If level > 0, then we are scanning an indirect block looking at its
2081 * pointers. If level == 0, then we are looking at a block of dnodes.
2083 * If we don't find what we are looking for in the block, we return ESRCH.
2084 * Otherwise, return with *offset pointing to the beginning (if searching
2085 * forwards) or end (if searching backwards) of the range covered by the
2086 * block pointer we matched on (or dnode).
2088 * The basic search algorithm used below by dnode_next_offset() is to
2089 * use this function to search up the block tree (widen the search) until
2090 * we find something (i.e., we don't return ESRCH) and then search back
2091 * down the tree (narrow the search) until we reach our original search
2095 dnode_next_offset_level(dnode_t
*dn
, int flags
, uint64_t *offset
,
2096 int lvl
, uint64_t blkfill
, uint64_t txg
)
2098 dmu_buf_impl_t
*db
= NULL
;
2100 uint64_t epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2101 uint64_t epb
= 1ULL << epbs
;
2102 uint64_t minfill
, maxfill
;
2104 int i
, inc
, error
, span
;
2106 hole
= ((flags
& DNODE_FIND_HOLE
) != 0);
2107 inc
= (flags
& DNODE_FIND_BACKWARDS
) ? -1 : 1;
2108 ASSERT(txg
== 0 || !hole
);
2110 if (lvl
== dn
->dn_phys
->dn_nlevels
) {
2112 epb
= dn
->dn_phys
->dn_nblkptr
;
2113 data
= dn
->dn_phys
->dn_blkptr
;
2115 uint64_t blkid
= dbuf_whichblock(dn
, lvl
, *offset
);
2116 error
= dbuf_hold_impl(dn
, lvl
, blkid
, TRUE
, FALSE
, FTAG
, &db
);
2118 if (error
!= ENOENT
)
2123 * This can only happen when we are searching up
2124 * the block tree for data. We don't really need to
2125 * adjust the offset, as we will just end up looking
2126 * at the pointer to this block in its parent, and its
2127 * going to be unallocated, so we will skip over it.
2129 return (SET_ERROR(ESRCH
));
2131 error
= dbuf_read(db
, NULL
,
2132 DB_RF_CANFAIL
| DB_RF_HAVESTRUCT
| DB_RF_NO_DECRYPT
);
2134 dbuf_rele(db
, FTAG
);
2137 data
= db
->db
.db_data
;
2141 if (db
!= NULL
&& txg
!= 0 && (db
->db_blkptr
== NULL
||
2142 db
->db_blkptr
->blk_birth
<= txg
||
2143 BP_IS_HOLE(db
->db_blkptr
))) {
2145 * This can only happen when we are searching up the tree
2146 * and these conditions mean that we need to keep climbing.
2148 error
= SET_ERROR(ESRCH
);
2149 } else if (lvl
== 0) {
2150 dnode_phys_t
*dnp
= data
;
2152 ASSERT(dn
->dn_type
== DMU_OT_DNODE
);
2153 ASSERT(!(flags
& DNODE_FIND_BACKWARDS
));
2155 for (i
= (*offset
>> DNODE_SHIFT
) & (blkfill
- 1);
2156 i
< blkfill
; i
+= dnp
[i
].dn_extra_slots
+ 1) {
2157 if ((dnp
[i
].dn_type
== DMU_OT_NONE
) == hole
)
2162 error
= SET_ERROR(ESRCH
);
2164 *offset
= (*offset
& ~(DNODE_BLOCK_SIZE
- 1)) +
2167 blkptr_t
*bp
= data
;
2168 uint64_t start
= *offset
;
2169 span
= (lvl
- 1) * epbs
+ dn
->dn_datablkshift
;
2171 maxfill
= blkfill
<< ((lvl
- 1) * epbs
);
2178 if (span
>= 8 * sizeof (*offset
)) {
2179 /* This only happens on the highest indirection level */
2180 ASSERT3U((lvl
- 1), ==, dn
->dn_phys
->dn_nlevels
- 1);
2183 *offset
= *offset
>> span
;
2186 for (i
= BF64_GET(*offset
, 0, epbs
);
2187 i
>= 0 && i
< epb
; i
+= inc
) {
2188 if (BP_GET_FILL(&bp
[i
]) >= minfill
&&
2189 BP_GET_FILL(&bp
[i
]) <= maxfill
&&
2190 (hole
|| bp
[i
].blk_birth
> txg
))
2192 if (inc
> 0 || *offset
> 0)
2196 if (span
>= 8 * sizeof (*offset
)) {
2199 *offset
= *offset
<< span
;
2203 /* traversing backwards; position offset at the end */
2204 ASSERT3U(*offset
, <=, start
);
2205 *offset
= MIN(*offset
+ (1ULL << span
) - 1, start
);
2206 } else if (*offset
< start
) {
2209 if (i
< 0 || i
>= epb
)
2210 error
= SET_ERROR(ESRCH
);
2214 dbuf_rele(db
, FTAG
);
2220 * Find the next hole, data, or sparse region at or after *offset.
2221 * The value 'blkfill' tells us how many items we expect to find
2222 * in an L0 data block; this value is 1 for normal objects,
2223 * DNODES_PER_BLOCK for the meta dnode, and some fraction of
2224 * DNODES_PER_BLOCK when searching for sparse regions thereof.
2228 * dnode_next_offset(dn, flags, offset, 1, 1, 0);
2229 * Finds the next/previous hole/data in a file.
2230 * Used in dmu_offset_next().
2232 * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
2233 * Finds the next free/allocated dnode an objset's meta-dnode.
2234 * Only finds objects that have new contents since txg (ie.
2235 * bonus buffer changes and content removal are ignored).
2236 * Used in dmu_object_next().
2238 * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
2239 * Finds the next L2 meta-dnode bp that's at most 1/4 full.
2240 * Used in dmu_object_alloc().
2243 dnode_next_offset(dnode_t
*dn
, int flags
, uint64_t *offset
,
2244 int minlvl
, uint64_t blkfill
, uint64_t txg
)
2246 uint64_t initial_offset
= *offset
;
2250 if (!(flags
& DNODE_FIND_HAVELOCK
))
2251 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2253 if (dn
->dn_phys
->dn_nlevels
== 0) {
2254 error
= SET_ERROR(ESRCH
);
2258 if (dn
->dn_datablkshift
== 0) {
2259 if (*offset
< dn
->dn_datablksz
) {
2260 if (flags
& DNODE_FIND_HOLE
)
2261 *offset
= dn
->dn_datablksz
;
2263 error
= SET_ERROR(ESRCH
);
2268 maxlvl
= dn
->dn_phys
->dn_nlevels
;
2270 for (lvl
= minlvl
; lvl
<= maxlvl
; lvl
++) {
2271 error
= dnode_next_offset_level(dn
,
2272 flags
, offset
, lvl
, blkfill
, txg
);
2277 while (error
== 0 && --lvl
>= minlvl
) {
2278 error
= dnode_next_offset_level(dn
,
2279 flags
, offset
, lvl
, blkfill
, txg
);
2283 * There's always a "virtual hole" at the end of the object, even
2284 * if all BP's which physically exist are non-holes.
2286 if ((flags
& DNODE_FIND_HOLE
) && error
== ESRCH
&& txg
== 0 &&
2287 minlvl
== 1 && blkfill
== 1 && !(flags
& DNODE_FIND_BACKWARDS
)) {
2291 if (error
== 0 && (flags
& DNODE_FIND_BACKWARDS
?
2292 initial_offset
< *offset
: initial_offset
> *offset
))
2293 error
= SET_ERROR(ESRCH
);
2295 if (!(flags
& DNODE_FIND_HAVELOCK
))
2296 rw_exit(&dn
->dn_struct_rwlock
);