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, 2015 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 static kmem_cache_t
*dnode_cache
;
44 * Define DNODE_STATS to turn on statistic gathering. By default, it is only
45 * turned on when DEBUG is also defined.
52 #define DNODE_STAT_ADD(stat) ((stat)++)
54 #define DNODE_STAT_ADD(stat) /* nothing */
55 #endif /* DNODE_STATS */
57 ASSERTV(static dnode_phys_t dnode_phys_zero
);
59 int zfs_default_bs
= SPA_MINBLOCKSHIFT
;
60 int zfs_default_ibs
= DN_MAX_INDBLKSHIFT
;
63 static kmem_cbrc_t
dnode_move(void *, void *, size_t, void *);
67 dbuf_compare(const void *x1
, const void *x2
)
69 const dmu_buf_impl_t
*d1
= x1
;
70 const dmu_buf_impl_t
*d2
= x2
;
72 int cmp
= AVL_CMP(d1
->db_level
, d2
->db_level
);
76 cmp
= AVL_CMP(d1
->db_blkid
, d2
->db_blkid
);
80 if (d1
->db_state
== DB_SEARCH
) {
81 ASSERT3S(d2
->db_state
, !=, DB_SEARCH
);
83 } else if (d2
->db_state
== DB_SEARCH
) {
84 ASSERT3S(d1
->db_state
, !=, DB_SEARCH
);
88 return (AVL_PCMP(d1
, d2
));
93 dnode_cons(void *arg
, void *unused
, int kmflag
)
98 rw_init(&dn
->dn_struct_rwlock
, NULL
, RW_NOLOCKDEP
, NULL
);
99 mutex_init(&dn
->dn_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
100 mutex_init(&dn
->dn_dbufs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
101 cv_init(&dn
->dn_notxholds
, NULL
, CV_DEFAULT
, NULL
);
104 * Every dbuf has a reference, and dropping a tracked reference is
105 * O(number of references), so don't track dn_holds.
107 refcount_create_untracked(&dn
->dn_holds
);
108 refcount_create(&dn
->dn_tx_holds
);
109 list_link_init(&dn
->dn_link
);
111 bzero(&dn
->dn_next_nblkptr
[0], sizeof (dn
->dn_next_nblkptr
));
112 bzero(&dn
->dn_next_nlevels
[0], sizeof (dn
->dn_next_nlevels
));
113 bzero(&dn
->dn_next_indblkshift
[0], sizeof (dn
->dn_next_indblkshift
));
114 bzero(&dn
->dn_next_bonustype
[0], sizeof (dn
->dn_next_bonustype
));
115 bzero(&dn
->dn_rm_spillblk
[0], sizeof (dn
->dn_rm_spillblk
));
116 bzero(&dn
->dn_next_bonuslen
[0], sizeof (dn
->dn_next_bonuslen
));
117 bzero(&dn
->dn_next_blksz
[0], sizeof (dn
->dn_next_blksz
));
119 for (i
= 0; i
< TXG_SIZE
; i
++) {
120 list_link_init(&dn
->dn_dirty_link
[i
]);
121 dn
->dn_free_ranges
[i
] = NULL
;
122 list_create(&dn
->dn_dirty_records
[i
],
123 sizeof (dbuf_dirty_record_t
),
124 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
127 dn
->dn_allocated_txg
= 0;
129 dn
->dn_assigned_txg
= 0;
131 dn
->dn_dirtyctx_firstset
= NULL
;
133 dn
->dn_have_spill
= B_FALSE
;
143 dn
->dn_dbufs_count
= 0;
144 dn
->dn_unlisted_l0_blkid
= 0;
145 avl_create(&dn
->dn_dbufs
, dbuf_compare
, sizeof (dmu_buf_impl_t
),
146 offsetof(dmu_buf_impl_t
, db_link
));
154 dnode_dest(void *arg
, void *unused
)
159 rw_destroy(&dn
->dn_struct_rwlock
);
160 mutex_destroy(&dn
->dn_mtx
);
161 mutex_destroy(&dn
->dn_dbufs_mtx
);
162 cv_destroy(&dn
->dn_notxholds
);
163 refcount_destroy(&dn
->dn_holds
);
164 refcount_destroy(&dn
->dn_tx_holds
);
165 ASSERT(!list_link_active(&dn
->dn_link
));
167 for (i
= 0; i
< TXG_SIZE
; i
++) {
168 ASSERT(!list_link_active(&dn
->dn_dirty_link
[i
]));
169 ASSERT3P(dn
->dn_free_ranges
[i
], ==, NULL
);
170 list_destroy(&dn
->dn_dirty_records
[i
]);
171 ASSERT0(dn
->dn_next_nblkptr
[i
]);
172 ASSERT0(dn
->dn_next_nlevels
[i
]);
173 ASSERT0(dn
->dn_next_indblkshift
[i
]);
174 ASSERT0(dn
->dn_next_bonustype
[i
]);
175 ASSERT0(dn
->dn_rm_spillblk
[i
]);
176 ASSERT0(dn
->dn_next_bonuslen
[i
]);
177 ASSERT0(dn
->dn_next_blksz
[i
]);
180 ASSERT0(dn
->dn_allocated_txg
);
181 ASSERT0(dn
->dn_free_txg
);
182 ASSERT0(dn
->dn_assigned_txg
);
183 ASSERT0(dn
->dn_dirtyctx
);
184 ASSERT3P(dn
->dn_dirtyctx_firstset
, ==, NULL
);
185 ASSERT3P(dn
->dn_bonus
, ==, NULL
);
186 ASSERT(!dn
->dn_have_spill
);
187 ASSERT3P(dn
->dn_zio
, ==, NULL
);
188 ASSERT0(dn
->dn_oldused
);
189 ASSERT0(dn
->dn_oldflags
);
190 ASSERT0(dn
->dn_olduid
);
191 ASSERT0(dn
->dn_oldgid
);
192 ASSERT0(dn
->dn_newuid
);
193 ASSERT0(dn
->dn_newgid
);
194 ASSERT0(dn
->dn_id_flags
);
196 ASSERT0(dn
->dn_dbufs_count
);
197 ASSERT0(dn
->dn_unlisted_l0_blkid
);
198 avl_destroy(&dn
->dn_dbufs
);
204 ASSERT(dnode_cache
== NULL
);
205 dnode_cache
= kmem_cache_create("dnode_t", sizeof (dnode_t
),
206 0, dnode_cons
, dnode_dest
, NULL
, NULL
, NULL
, 0);
207 kmem_cache_set_move(dnode_cache
, dnode_move
);
213 kmem_cache_destroy(dnode_cache
);
220 dnode_verify(dnode_t
*dn
)
222 int drop_struct_lock
= FALSE
;
225 ASSERT(dn
->dn_objset
);
226 ASSERT(dn
->dn_handle
->dnh_dnode
== dn
);
228 ASSERT(DMU_OT_IS_VALID(dn
->dn_phys
->dn_type
));
230 if (!(zfs_flags
& ZFS_DEBUG_DNODE_VERIFY
))
233 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
234 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
235 drop_struct_lock
= TRUE
;
237 if (dn
->dn_phys
->dn_type
!= DMU_OT_NONE
|| dn
->dn_allocated_txg
!= 0) {
239 int max_bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
240 ASSERT3U(dn
->dn_indblkshift
, <=, SPA_MAXBLOCKSHIFT
);
241 if (dn
->dn_datablkshift
) {
242 ASSERT3U(dn
->dn_datablkshift
, >=, SPA_MINBLOCKSHIFT
);
243 ASSERT3U(dn
->dn_datablkshift
, <=, SPA_MAXBLOCKSHIFT
);
244 ASSERT3U(1<<dn
->dn_datablkshift
, ==, dn
->dn_datablksz
);
246 ASSERT3U(dn
->dn_nlevels
, <=, 30);
247 ASSERT(DMU_OT_IS_VALID(dn
->dn_type
));
248 ASSERT3U(dn
->dn_nblkptr
, >=, 1);
249 ASSERT3U(dn
->dn_nblkptr
, <=, DN_MAX_NBLKPTR
);
250 ASSERT3U(dn
->dn_bonuslen
, <=, max_bonuslen
);
251 ASSERT3U(dn
->dn_datablksz
, ==,
252 dn
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
);
253 ASSERT3U(ISP2(dn
->dn_datablksz
), ==, dn
->dn_datablkshift
!= 0);
254 ASSERT3U((dn
->dn_nblkptr
- 1) * sizeof (blkptr_t
) +
255 dn
->dn_bonuslen
, <=, max_bonuslen
);
256 for (i
= 0; i
< TXG_SIZE
; i
++) {
257 ASSERT3U(dn
->dn_next_nlevels
[i
], <=, dn
->dn_nlevels
);
260 if (dn
->dn_phys
->dn_type
!= DMU_OT_NONE
)
261 ASSERT3U(dn
->dn_phys
->dn_nlevels
, <=, dn
->dn_nlevels
);
262 ASSERT(DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) || dn
->dn_dbuf
!= NULL
);
263 if (dn
->dn_dbuf
!= NULL
) {
264 ASSERT3P(dn
->dn_phys
, ==,
265 (dnode_phys_t
*)dn
->dn_dbuf
->db
.db_data
+
266 (dn
->dn_object
% (dn
->dn_dbuf
->db
.db_size
>> DNODE_SHIFT
)));
268 if (drop_struct_lock
)
269 rw_exit(&dn
->dn_struct_rwlock
);
274 dnode_byteswap(dnode_phys_t
*dnp
)
276 uint64_t *buf64
= (void*)&dnp
->dn_blkptr
;
279 if (dnp
->dn_type
== DMU_OT_NONE
) {
280 bzero(dnp
, sizeof (dnode_phys_t
));
284 dnp
->dn_datablkszsec
= BSWAP_16(dnp
->dn_datablkszsec
);
285 dnp
->dn_bonuslen
= BSWAP_16(dnp
->dn_bonuslen
);
286 dnp
->dn_extra_slots
= BSWAP_8(dnp
->dn_extra_slots
);
287 dnp
->dn_maxblkid
= BSWAP_64(dnp
->dn_maxblkid
);
288 dnp
->dn_used
= BSWAP_64(dnp
->dn_used
);
291 * dn_nblkptr is only one byte, so it's OK to read it in either
292 * byte order. We can't read dn_bouslen.
294 ASSERT(dnp
->dn_indblkshift
<= SPA_MAXBLOCKSHIFT
);
295 ASSERT(dnp
->dn_nblkptr
<= DN_MAX_NBLKPTR
);
296 for (i
= 0; i
< dnp
->dn_nblkptr
* sizeof (blkptr_t
)/8; i
++)
297 buf64
[i
] = BSWAP_64(buf64
[i
]);
300 * OK to check dn_bonuslen for zero, because it won't matter if
301 * we have the wrong byte order. This is necessary because the
302 * dnode dnode is smaller than a regular dnode.
304 if (dnp
->dn_bonuslen
!= 0) {
306 * Note that the bonus length calculated here may be
307 * longer than the actual bonus buffer. This is because
308 * we always put the bonus buffer after the last block
309 * pointer (instead of packing it against the end of the
312 int off
= (dnp
->dn_nblkptr
-1) * sizeof (blkptr_t
);
313 int slots
= dnp
->dn_extra_slots
+ 1;
314 size_t len
= DN_SLOTS_TO_BONUSLEN(slots
) - off
;
315 dmu_object_byteswap_t byteswap
;
316 ASSERT(DMU_OT_IS_VALID(dnp
->dn_bonustype
));
317 byteswap
= DMU_OT_BYTESWAP(dnp
->dn_bonustype
);
318 dmu_ot_byteswap
[byteswap
].ob_func(dnp
->dn_bonus
+ off
, len
);
321 /* Swap SPILL block if we have one */
322 if (dnp
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
)
323 byteswap_uint64_array(DN_SPILL_BLKPTR(dnp
), sizeof (blkptr_t
));
327 dnode_buf_byteswap(void *vbuf
, size_t size
)
331 ASSERT3U(sizeof (dnode_phys_t
), ==, (1<<DNODE_SHIFT
));
332 ASSERT((size
& (sizeof (dnode_phys_t
)-1)) == 0);
335 dnode_phys_t
*dnp
= vbuf
+ i
;
339 if (dnp
->dn_type
!= DMU_OT_NONE
)
340 i
+= dnp
->dn_extra_slots
* DNODE_MIN_SIZE
;
345 dnode_setbonuslen(dnode_t
*dn
, int newsize
, dmu_tx_t
*tx
)
347 ASSERT3U(refcount_count(&dn
->dn_holds
), >=, 1);
349 dnode_setdirty(dn
, tx
);
350 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
351 ASSERT3U(newsize
, <=, DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
352 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
));
353 dn
->dn_bonuslen
= newsize
;
355 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = DN_ZERO_BONUSLEN
;
357 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonuslen
;
358 rw_exit(&dn
->dn_struct_rwlock
);
362 dnode_setbonus_type(dnode_t
*dn
, dmu_object_type_t newtype
, dmu_tx_t
*tx
)
364 ASSERT3U(refcount_count(&dn
->dn_holds
), >=, 1);
365 dnode_setdirty(dn
, tx
);
366 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
367 dn
->dn_bonustype
= newtype
;
368 dn
->dn_next_bonustype
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonustype
;
369 rw_exit(&dn
->dn_struct_rwlock
);
373 dnode_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
375 ASSERT3U(refcount_count(&dn
->dn_holds
), >=, 1);
376 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
377 dnode_setdirty(dn
, tx
);
378 dn
->dn_rm_spillblk
[tx
->tx_txg
&TXG_MASK
] = DN_KILL_SPILLBLK
;
379 dn
->dn_have_spill
= B_FALSE
;
383 dnode_setdblksz(dnode_t
*dn
, int size
)
385 ASSERT0(P2PHASE(size
, SPA_MINBLOCKSIZE
));
386 ASSERT3U(size
, <=, SPA_MAXBLOCKSIZE
);
387 ASSERT3U(size
, >=, SPA_MINBLOCKSIZE
);
388 ASSERT3U(size
>> SPA_MINBLOCKSHIFT
, <,
389 1<<(sizeof (dn
->dn_phys
->dn_datablkszsec
) * 8));
390 dn
->dn_datablksz
= size
;
391 dn
->dn_datablkszsec
= size
>> SPA_MINBLOCKSHIFT
;
392 dn
->dn_datablkshift
= ISP2(size
) ? highbit64(size
- 1) : 0;
396 dnode_create(objset_t
*os
, dnode_phys_t
*dnp
, dmu_buf_impl_t
*db
,
397 uint64_t object
, dnode_handle_t
*dnh
)
401 dn
= kmem_cache_alloc(dnode_cache
, KM_SLEEP
);
402 ASSERT(!POINTER_IS_VALID(dn
->dn_objset
));
406 * Defer setting dn_objset until the dnode is ready to be a candidate
407 * for the dnode_move() callback.
409 dn
->dn_object
= object
;
414 if (dnp
->dn_datablkszsec
) {
415 dnode_setdblksz(dn
, dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
);
417 dn
->dn_datablksz
= 0;
418 dn
->dn_datablkszsec
= 0;
419 dn
->dn_datablkshift
= 0;
421 dn
->dn_indblkshift
= dnp
->dn_indblkshift
;
422 dn
->dn_nlevels
= dnp
->dn_nlevels
;
423 dn
->dn_type
= dnp
->dn_type
;
424 dn
->dn_nblkptr
= dnp
->dn_nblkptr
;
425 dn
->dn_checksum
= dnp
->dn_checksum
;
426 dn
->dn_compress
= dnp
->dn_compress
;
427 dn
->dn_bonustype
= dnp
->dn_bonustype
;
428 dn
->dn_bonuslen
= dnp
->dn_bonuslen
;
429 dn
->dn_num_slots
= dnp
->dn_extra_slots
+ 1;
430 dn
->dn_maxblkid
= dnp
->dn_maxblkid
;
431 dn
->dn_have_spill
= ((dnp
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
) != 0);
434 dmu_zfetch_init(&dn
->dn_zfetch
, dn
);
436 ASSERT(DMU_OT_IS_VALID(dn
->dn_phys
->dn_type
));
438 mutex_enter(&os
->os_lock
);
439 if (dnh
->dnh_dnode
!= NULL
) {
440 /* Lost the allocation race. */
441 mutex_exit(&os
->os_lock
);
442 kmem_cache_free(dnode_cache
, dn
);
443 return (dnh
->dnh_dnode
);
447 * Exclude special dnodes from os_dnodes so an empty os_dnodes
448 * signifies that the special dnodes have no references from
449 * their children (the entries in os_dnodes). This allows
450 * dnode_destroy() to easily determine if the last child has
451 * been removed and then complete eviction of the objset.
453 if (!DMU_OBJECT_IS_SPECIAL(object
))
454 list_insert_head(&os
->os_dnodes
, dn
);
458 * Everything else must be valid before assigning dn_objset
459 * makes the dnode eligible for dnode_move().
464 mutex_exit(&os
->os_lock
);
466 arc_space_consume(sizeof (dnode_t
), ARC_SPACE_DNODE
);
471 * Caller must be holding the dnode handle, which is released upon return.
474 dnode_destroy(dnode_t
*dn
)
476 objset_t
*os
= dn
->dn_objset
;
477 boolean_t complete_os_eviction
= B_FALSE
;
479 ASSERT((dn
->dn_id_flags
& DN_ID_NEW_EXIST
) == 0);
481 mutex_enter(&os
->os_lock
);
482 POINTER_INVALIDATE(&dn
->dn_objset
);
483 if (!DMU_OBJECT_IS_SPECIAL(dn
->dn_object
)) {
484 list_remove(&os
->os_dnodes
, dn
);
485 complete_os_eviction
=
486 list_is_empty(&os
->os_dnodes
) &&
487 list_link_active(&os
->os_evicting_node
);
489 mutex_exit(&os
->os_lock
);
491 /* the dnode can no longer move, so we can release the handle */
492 zrl_remove(&dn
->dn_handle
->dnh_zrlock
);
494 dn
->dn_allocated_txg
= 0;
496 dn
->dn_assigned_txg
= 0;
499 if (dn
->dn_dirtyctx_firstset
!= NULL
) {
500 kmem_free(dn
->dn_dirtyctx_firstset
, 1);
501 dn
->dn_dirtyctx_firstset
= NULL
;
503 if (dn
->dn_bonus
!= NULL
) {
504 mutex_enter(&dn
->dn_bonus
->db_mtx
);
505 dbuf_destroy(dn
->dn_bonus
);
510 dn
->dn_have_spill
= B_FALSE
;
518 dn
->dn_unlisted_l0_blkid
= 0;
520 dmu_zfetch_fini(&dn
->dn_zfetch
);
521 kmem_cache_free(dnode_cache
, dn
);
522 arc_space_return(sizeof (dnode_t
), ARC_SPACE_DNODE
);
524 if (complete_os_eviction
)
525 dmu_objset_evict_done(os
);
529 dnode_allocate(dnode_t
*dn
, dmu_object_type_t ot
, int blocksize
, int ibs
,
530 dmu_object_type_t bonustype
, int bonuslen
, int dn_slots
, dmu_tx_t
*tx
)
534 ASSERT3U(dn_slots
, >, 0);
535 ASSERT3U(dn_slots
<< DNODE_SHIFT
, <=,
536 spa_maxdnodesize(dmu_objset_spa(dn
->dn_objset
)));
537 ASSERT3U(blocksize
, <=,
538 spa_maxblocksize(dmu_objset_spa(dn
->dn_objset
)));
540 blocksize
= 1 << zfs_default_bs
;
542 blocksize
= P2ROUNDUP(blocksize
, SPA_MINBLOCKSIZE
);
545 ibs
= zfs_default_ibs
;
547 ibs
= MIN(MAX(ibs
, DN_MIN_INDBLKSHIFT
), DN_MAX_INDBLKSHIFT
);
549 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d dn_slots=%d\n",
550 dn
->dn_objset
, dn
->dn_object
, tx
->tx_txg
, blocksize
, ibs
, dn_slots
);
552 ASSERT(dn
->dn_type
== DMU_OT_NONE
);
553 ASSERT(bcmp(dn
->dn_phys
, &dnode_phys_zero
, sizeof (dnode_phys_t
)) == 0);
554 ASSERT(dn
->dn_phys
->dn_type
== DMU_OT_NONE
);
555 ASSERT(ot
!= DMU_OT_NONE
);
556 ASSERT(DMU_OT_IS_VALID(ot
));
557 ASSERT((bonustype
== DMU_OT_NONE
&& bonuslen
== 0) ||
558 (bonustype
== DMU_OT_SA
&& bonuslen
== 0) ||
559 (bonustype
!= DMU_OT_NONE
&& bonuslen
!= 0));
560 ASSERT(DMU_OT_IS_VALID(bonustype
));
561 ASSERT3U(bonuslen
, <=, DN_SLOTS_TO_BONUSLEN(dn_slots
));
562 ASSERT(dn
->dn_type
== DMU_OT_NONE
);
563 ASSERT0(dn
->dn_maxblkid
);
564 ASSERT0(dn
->dn_allocated_txg
);
565 ASSERT0(dn
->dn_assigned_txg
);
566 ASSERT(refcount_is_zero(&dn
->dn_tx_holds
));
567 ASSERT3U(refcount_count(&dn
->dn_holds
), <=, 1);
568 ASSERT(avl_is_empty(&dn
->dn_dbufs
));
570 for (i
= 0; i
< TXG_SIZE
; i
++) {
571 ASSERT0(dn
->dn_next_nblkptr
[i
]);
572 ASSERT0(dn
->dn_next_nlevels
[i
]);
573 ASSERT0(dn
->dn_next_indblkshift
[i
]);
574 ASSERT0(dn
->dn_next_bonuslen
[i
]);
575 ASSERT0(dn
->dn_next_bonustype
[i
]);
576 ASSERT0(dn
->dn_rm_spillblk
[i
]);
577 ASSERT0(dn
->dn_next_blksz
[i
]);
578 ASSERT(!list_link_active(&dn
->dn_dirty_link
[i
]));
579 ASSERT3P(list_head(&dn
->dn_dirty_records
[i
]), ==, NULL
);
580 ASSERT3P(dn
->dn_free_ranges
[i
], ==, NULL
);
584 dnode_setdblksz(dn
, blocksize
);
585 dn
->dn_indblkshift
= ibs
;
587 dn
->dn_num_slots
= dn_slots
;
588 if (bonustype
== DMU_OT_SA
) /* Maximize bonus space for SA */
591 dn
->dn_nblkptr
= MIN(DN_MAX_NBLKPTR
,
592 1 + ((DN_SLOTS_TO_BONUSLEN(dn_slots
) - bonuslen
) >>
596 dn
->dn_bonustype
= bonustype
;
597 dn
->dn_bonuslen
= bonuslen
;
598 dn
->dn_checksum
= ZIO_CHECKSUM_INHERIT
;
599 dn
->dn_compress
= ZIO_COMPRESS_INHERIT
;
603 if (dn
->dn_dirtyctx_firstset
) {
604 kmem_free(dn
->dn_dirtyctx_firstset
, 1);
605 dn
->dn_dirtyctx_firstset
= NULL
;
608 dn
->dn_allocated_txg
= tx
->tx_txg
;
611 dnode_setdirty(dn
, tx
);
612 dn
->dn_next_indblkshift
[tx
->tx_txg
& TXG_MASK
] = ibs
;
613 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonuslen
;
614 dn
->dn_next_bonustype
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonustype
;
615 dn
->dn_next_blksz
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_datablksz
;
619 dnode_reallocate(dnode_t
*dn
, dmu_object_type_t ot
, int blocksize
,
620 dmu_object_type_t bonustype
, int bonuslen
, int dn_slots
, dmu_tx_t
*tx
)
624 ASSERT3U(blocksize
, >=, SPA_MINBLOCKSIZE
);
625 ASSERT3U(blocksize
, <=,
626 spa_maxblocksize(dmu_objset_spa(dn
->dn_objset
)));
627 ASSERT0(blocksize
% SPA_MINBLOCKSIZE
);
628 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
|| dmu_tx_private_ok(tx
));
629 ASSERT(tx
->tx_txg
!= 0);
630 ASSERT((bonustype
== DMU_OT_NONE
&& bonuslen
== 0) ||
631 (bonustype
!= DMU_OT_NONE
&& bonuslen
!= 0) ||
632 (bonustype
== DMU_OT_SA
&& bonuslen
== 0));
633 ASSERT(DMU_OT_IS_VALID(bonustype
));
634 ASSERT3U(bonuslen
, <=,
635 DN_BONUS_SIZE(spa_maxdnodesize(dmu_objset_spa(dn
->dn_objset
))));
637 dn_slots
= dn_slots
> 0 ? dn_slots
: DNODE_MIN_SLOTS
;
639 /* clean up any unreferenced dbufs */
640 dnode_evict_dbufs(dn
);
644 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
645 dnode_setdirty(dn
, tx
);
646 if (dn
->dn_datablksz
!= blocksize
) {
647 /* change blocksize */
648 ASSERT(dn
->dn_maxblkid
== 0 &&
649 (BP_IS_HOLE(&dn
->dn_phys
->dn_blkptr
[0]) ||
650 dnode_block_freed(dn
, 0)));
651 dnode_setdblksz(dn
, blocksize
);
652 dn
->dn_next_blksz
[tx
->tx_txg
&TXG_MASK
] = blocksize
;
654 if (dn
->dn_bonuslen
!= bonuslen
)
655 dn
->dn_next_bonuslen
[tx
->tx_txg
&TXG_MASK
] = bonuslen
;
657 if (bonustype
== DMU_OT_SA
) /* Maximize bonus space for SA */
660 nblkptr
= MIN(DN_MAX_NBLKPTR
,
661 1 + ((DN_SLOTS_TO_BONUSLEN(dn_slots
) - bonuslen
) >>
663 if (dn
->dn_bonustype
!= bonustype
)
664 dn
->dn_next_bonustype
[tx
->tx_txg
&TXG_MASK
] = bonustype
;
665 if (dn
->dn_nblkptr
!= nblkptr
)
666 dn
->dn_next_nblkptr
[tx
->tx_txg
&TXG_MASK
] = nblkptr
;
667 if (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
) {
668 dbuf_rm_spill(dn
, tx
);
669 dnode_rm_spill(dn
, tx
);
671 rw_exit(&dn
->dn_struct_rwlock
);
676 /* change bonus size and type */
677 mutex_enter(&dn
->dn_mtx
);
678 dn
->dn_bonustype
= bonustype
;
679 dn
->dn_bonuslen
= bonuslen
;
680 dn
->dn_num_slots
= dn_slots
;
681 dn
->dn_nblkptr
= nblkptr
;
682 dn
->dn_checksum
= ZIO_CHECKSUM_INHERIT
;
683 dn
->dn_compress
= ZIO_COMPRESS_INHERIT
;
684 ASSERT3U(dn
->dn_nblkptr
, <=, DN_MAX_NBLKPTR
);
686 /* fix up the bonus db_size */
688 dn
->dn_bonus
->db
.db_size
=
689 DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
690 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
691 ASSERT(dn
->dn_bonuslen
<= dn
->dn_bonus
->db
.db_size
);
694 dn
->dn_allocated_txg
= tx
->tx_txg
;
695 mutex_exit(&dn
->dn_mtx
);
701 uint64_t dms_dnode_invalid
;
702 uint64_t dms_dnode_recheck1
;
703 uint64_t dms_dnode_recheck2
;
704 uint64_t dms_dnode_special
;
705 uint64_t dms_dnode_handle
;
706 uint64_t dms_dnode_rwlock
;
707 uint64_t dms_dnode_active
;
709 #endif /* DNODE_STATS */
712 dnode_move_impl(dnode_t
*odn
, dnode_t
*ndn
)
716 ASSERT(!RW_LOCK_HELD(&odn
->dn_struct_rwlock
));
717 ASSERT(MUTEX_NOT_HELD(&odn
->dn_mtx
));
718 ASSERT(MUTEX_NOT_HELD(&odn
->dn_dbufs_mtx
));
719 ASSERT(!RW_LOCK_HELD(&odn
->dn_zfetch
.zf_rwlock
));
722 ndn
->dn_objset
= odn
->dn_objset
;
723 ndn
->dn_object
= odn
->dn_object
;
724 ndn
->dn_dbuf
= odn
->dn_dbuf
;
725 ndn
->dn_handle
= odn
->dn_handle
;
726 ndn
->dn_phys
= odn
->dn_phys
;
727 ndn
->dn_type
= odn
->dn_type
;
728 ndn
->dn_bonuslen
= odn
->dn_bonuslen
;
729 ndn
->dn_bonustype
= odn
->dn_bonustype
;
730 ndn
->dn_nblkptr
= odn
->dn_nblkptr
;
731 ndn
->dn_checksum
= odn
->dn_checksum
;
732 ndn
->dn_compress
= odn
->dn_compress
;
733 ndn
->dn_nlevels
= odn
->dn_nlevels
;
734 ndn
->dn_indblkshift
= odn
->dn_indblkshift
;
735 ndn
->dn_datablkshift
= odn
->dn_datablkshift
;
736 ndn
->dn_datablkszsec
= odn
->dn_datablkszsec
;
737 ndn
->dn_datablksz
= odn
->dn_datablksz
;
738 ndn
->dn_maxblkid
= odn
->dn_maxblkid
;
739 bcopy(&odn
->dn_next_nblkptr
[0], &ndn
->dn_next_nblkptr
[0],
740 sizeof (odn
->dn_next_nblkptr
));
741 bcopy(&odn
->dn_next_nlevels
[0], &ndn
->dn_next_nlevels
[0],
742 sizeof (odn
->dn_next_nlevels
));
743 bcopy(&odn
->dn_next_indblkshift
[0], &ndn
->dn_next_indblkshift
[0],
744 sizeof (odn
->dn_next_indblkshift
));
745 bcopy(&odn
->dn_next_bonustype
[0], &ndn
->dn_next_bonustype
[0],
746 sizeof (odn
->dn_next_bonustype
));
747 bcopy(&odn
->dn_rm_spillblk
[0], &ndn
->dn_rm_spillblk
[0],
748 sizeof (odn
->dn_rm_spillblk
));
749 bcopy(&odn
->dn_next_bonuslen
[0], &ndn
->dn_next_bonuslen
[0],
750 sizeof (odn
->dn_next_bonuslen
));
751 bcopy(&odn
->dn_next_blksz
[0], &ndn
->dn_next_blksz
[0],
752 sizeof (odn
->dn_next_blksz
));
753 for (i
= 0; i
< TXG_SIZE
; i
++) {
754 list_move_tail(&ndn
->dn_dirty_records
[i
],
755 &odn
->dn_dirty_records
[i
]);
757 bcopy(&odn
->dn_free_ranges
[0], &ndn
->dn_free_ranges
[0],
758 sizeof (odn
->dn_free_ranges
));
759 ndn
->dn_allocated_txg
= odn
->dn_allocated_txg
;
760 ndn
->dn_free_txg
= odn
->dn_free_txg
;
761 ndn
->dn_assigned_txg
= odn
->dn_assigned_txg
;
762 ndn
->dn_dirtyctx
= odn
->dn_dirtyctx
;
763 ndn
->dn_dirtyctx_firstset
= odn
->dn_dirtyctx_firstset
;
764 ASSERT(refcount_count(&odn
->dn_tx_holds
) == 0);
765 refcount_transfer(&ndn
->dn_holds
, &odn
->dn_holds
);
766 ASSERT(avl_is_empty(&ndn
->dn_dbufs
));
767 avl_swap(&ndn
->dn_dbufs
, &odn
->dn_dbufs
);
768 ndn
->dn_dbufs_count
= odn
->dn_dbufs_count
;
769 ndn
->dn_unlisted_l0_blkid
= odn
->dn_unlisted_l0_blkid
;
770 ndn
->dn_bonus
= odn
->dn_bonus
;
771 ndn
->dn_have_spill
= odn
->dn_have_spill
;
772 ndn
->dn_zio
= odn
->dn_zio
;
773 ndn
->dn_oldused
= odn
->dn_oldused
;
774 ndn
->dn_oldflags
= odn
->dn_oldflags
;
775 ndn
->dn_olduid
= odn
->dn_olduid
;
776 ndn
->dn_oldgid
= odn
->dn_oldgid
;
777 ndn
->dn_newuid
= odn
->dn_newuid
;
778 ndn
->dn_newgid
= odn
->dn_newgid
;
779 ndn
->dn_id_flags
= odn
->dn_id_flags
;
780 dmu_zfetch_init(&ndn
->dn_zfetch
, NULL
);
781 list_move_tail(&ndn
->dn_zfetch
.zf_stream
, &odn
->dn_zfetch
.zf_stream
);
782 ndn
->dn_zfetch
.zf_dnode
= odn
->dn_zfetch
.zf_dnode
;
785 * Update back pointers. Updating the handle fixes the back pointer of
786 * every descendant dbuf as well as the bonus dbuf.
788 ASSERT(ndn
->dn_handle
->dnh_dnode
== odn
);
789 ndn
->dn_handle
->dnh_dnode
= ndn
;
790 if (ndn
->dn_zfetch
.zf_dnode
== odn
) {
791 ndn
->dn_zfetch
.zf_dnode
= ndn
;
795 * Invalidate the original dnode by clearing all of its back pointers.
798 odn
->dn_handle
= NULL
;
799 avl_create(&odn
->dn_dbufs
, dbuf_compare
, sizeof (dmu_buf_impl_t
),
800 offsetof(dmu_buf_impl_t
, db_link
));
801 odn
->dn_dbufs_count
= 0;
802 odn
->dn_unlisted_l0_blkid
= 0;
803 odn
->dn_bonus
= NULL
;
804 odn
->dn_zfetch
.zf_dnode
= NULL
;
807 * Set the low bit of the objset pointer to ensure that dnode_move()
808 * recognizes the dnode as invalid in any subsequent callback.
810 POINTER_INVALIDATE(&odn
->dn_objset
);
813 * Satisfy the destructor.
815 for (i
= 0; i
< TXG_SIZE
; i
++) {
816 list_create(&odn
->dn_dirty_records
[i
],
817 sizeof (dbuf_dirty_record_t
),
818 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
819 odn
->dn_free_ranges
[i
] = NULL
;
820 odn
->dn_next_nlevels
[i
] = 0;
821 odn
->dn_next_indblkshift
[i
] = 0;
822 odn
->dn_next_bonustype
[i
] = 0;
823 odn
->dn_rm_spillblk
[i
] = 0;
824 odn
->dn_next_bonuslen
[i
] = 0;
825 odn
->dn_next_blksz
[i
] = 0;
827 odn
->dn_allocated_txg
= 0;
828 odn
->dn_free_txg
= 0;
829 odn
->dn_assigned_txg
= 0;
830 odn
->dn_dirtyctx
= 0;
831 odn
->dn_dirtyctx_firstset
= NULL
;
832 odn
->dn_have_spill
= B_FALSE
;
835 odn
->dn_oldflags
= 0;
840 odn
->dn_id_flags
= 0;
846 odn
->dn_moved
= (uint8_t)-1;
851 dnode_move(void *buf
, void *newbuf
, size_t size
, void *arg
)
853 dnode_t
*odn
= buf
, *ndn
= newbuf
;
859 * The dnode is on the objset's list of known dnodes if the objset
860 * pointer is valid. We set the low bit of the objset pointer when
861 * freeing the dnode to invalidate it, and the memory patterns written
862 * by kmem (baddcafe and deadbeef) set at least one of the two low bits.
863 * A newly created dnode sets the objset pointer last of all to indicate
864 * that the dnode is known and in a valid state to be moved by this
868 if (!POINTER_IS_VALID(os
)) {
869 DNODE_STAT_ADD(dnode_move_stats
.dms_dnode_invalid
);
870 return (KMEM_CBRC_DONT_KNOW
);
874 * Ensure that the objset does not go away during the move.
876 rw_enter(&os_lock
, RW_WRITER
);
877 if (os
!= odn
->dn_objset
) {
879 DNODE_STAT_ADD(dnode_move_stats
.dms_dnode_recheck1
);
880 return (KMEM_CBRC_DONT_KNOW
);
884 * If the dnode is still valid, then so is the objset. We know that no
885 * valid objset can be freed while we hold os_lock, so we can safely
886 * ensure that the objset remains in use.
888 mutex_enter(&os
->os_lock
);
891 * Recheck the objset pointer in case the dnode was removed just before
892 * acquiring the lock.
894 if (os
!= odn
->dn_objset
) {
895 mutex_exit(&os
->os_lock
);
897 DNODE_STAT_ADD(dnode_move_stats
.dms_dnode_recheck2
);
898 return (KMEM_CBRC_DONT_KNOW
);
902 * At this point we know that as long as we hold os->os_lock, the dnode
903 * cannot be freed and fields within the dnode can be safely accessed.
904 * The objset listing this dnode cannot go away as long as this dnode is
908 if (DMU_OBJECT_IS_SPECIAL(odn
->dn_object
)) {
909 mutex_exit(&os
->os_lock
);
910 DNODE_STAT_ADD(dnode_move_stats
.dms_dnode_special
);
911 return (KMEM_CBRC_NO
);
913 ASSERT(odn
->dn_dbuf
!= NULL
); /* only "special" dnodes have no parent */
916 * Lock the dnode handle to prevent the dnode from obtaining any new
917 * holds. This also prevents the descendant dbufs and the bonus dbuf
918 * from accessing the dnode, so that we can discount their holds. The
919 * handle is safe to access because we know that while the dnode cannot
920 * go away, neither can its handle. Once we hold dnh_zrlock, we can
921 * safely move any dnode referenced only by dbufs.
923 if (!zrl_tryenter(&odn
->dn_handle
->dnh_zrlock
)) {
924 mutex_exit(&os
->os_lock
);
925 DNODE_STAT_ADD(dnode_move_stats
.dms_dnode_handle
);
926 return (KMEM_CBRC_LATER
);
930 * Ensure a consistent view of the dnode's holds and the dnode's dbufs.
931 * We need to guarantee that there is a hold for every dbuf in order to
932 * determine whether the dnode is actively referenced. Falsely matching
933 * a dbuf to an active hold would lead to an unsafe move. It's possible
934 * that a thread already having an active dnode hold is about to add a
935 * dbuf, and we can't compare hold and dbuf counts while the add is in
938 if (!rw_tryenter(&odn
->dn_struct_rwlock
, RW_WRITER
)) {
939 zrl_exit(&odn
->dn_handle
->dnh_zrlock
);
940 mutex_exit(&os
->os_lock
);
941 DNODE_STAT_ADD(dnode_move_stats
.dms_dnode_rwlock
);
942 return (KMEM_CBRC_LATER
);
946 * A dbuf may be removed (evicted) without an active dnode hold. In that
947 * case, the dbuf count is decremented under the handle lock before the
948 * dbuf's hold is released. This order ensures that if we count the hold
949 * after the dbuf is removed but before its hold is released, we will
950 * treat the unmatched hold as active and exit safely. If we count the
951 * hold before the dbuf is removed, the hold is discounted, and the
952 * removal is blocked until the move completes.
954 refcount
= refcount_count(&odn
->dn_holds
);
955 ASSERT(refcount
>= 0);
956 dbufs
= odn
->dn_dbufs_count
;
958 /* We can't have more dbufs than dnode holds. */
959 ASSERT3U(dbufs
, <=, refcount
);
960 DTRACE_PROBE3(dnode__move
, dnode_t
*, odn
, int64_t, refcount
,
963 if (refcount
> dbufs
) {
964 rw_exit(&odn
->dn_struct_rwlock
);
965 zrl_exit(&odn
->dn_handle
->dnh_zrlock
);
966 mutex_exit(&os
->os_lock
);
967 DNODE_STAT_ADD(dnode_move_stats
.dms_dnode_active
);
968 return (KMEM_CBRC_LATER
);
971 rw_exit(&odn
->dn_struct_rwlock
);
974 * At this point we know that anyone with a hold on the dnode is not
975 * actively referencing it. The dnode is known and in a valid state to
976 * move. We're holding the locks needed to execute the critical section.
978 dnode_move_impl(odn
, ndn
);
980 list_link_replace(&odn
->dn_link
, &ndn
->dn_link
);
981 /* If the dnode was safe to move, the refcount cannot have changed. */
982 ASSERT(refcount
== refcount_count(&ndn
->dn_holds
));
983 ASSERT(dbufs
== ndn
->dn_dbufs_count
);
984 zrl_exit(&ndn
->dn_handle
->dnh_zrlock
); /* handle has moved */
985 mutex_exit(&os
->os_lock
);
987 return (KMEM_CBRC_YES
);
992 dnode_special_close(dnode_handle_t
*dnh
)
994 dnode_t
*dn
= dnh
->dnh_dnode
;
997 * Wait for final references to the dnode to clear. This can
998 * only happen if the arc is asyncronously evicting state that
999 * has a hold on this dnode while we are trying to evict this
1002 while (refcount_count(&dn
->dn_holds
) > 0)
1004 ASSERT(dn
->dn_dbuf
== NULL
||
1005 dmu_buf_get_user(&dn
->dn_dbuf
->db
) == NULL
);
1006 zrl_add(&dnh
->dnh_zrlock
);
1007 dnode_destroy(dn
); /* implicit zrl_remove() */
1008 zrl_destroy(&dnh
->dnh_zrlock
);
1009 dnh
->dnh_dnode
= NULL
;
1013 dnode_special_open(objset_t
*os
, dnode_phys_t
*dnp
, uint64_t object
,
1014 dnode_handle_t
*dnh
)
1018 dn
= dnode_create(os
, dnp
, NULL
, object
, dnh
);
1019 zrl_init(&dnh
->dnh_zrlock
);
1024 dnode_buf_evict_async(void *dbu
)
1026 dnode_children_t
*children_dnodes
= dbu
;
1029 for (i
= 0; i
< children_dnodes
->dnc_count
; i
++) {
1030 dnode_handle_t
*dnh
= &children_dnodes
->dnc_children
[i
];
1034 * The dnode handle lock guards against the dnode moving to
1035 * another valid address, so there is no need here to guard
1036 * against changes to or from NULL.
1038 if (dnh
->dnh_dnode
== NULL
) {
1039 zrl_destroy(&dnh
->dnh_zrlock
);
1043 zrl_add(&dnh
->dnh_zrlock
);
1044 dn
= dnh
->dnh_dnode
;
1046 * If there are holds on this dnode, then there should
1047 * be holds on the dnode's containing dbuf as well; thus
1048 * it wouldn't be eligible for eviction and this function
1049 * would not have been called.
1051 ASSERT(refcount_is_zero(&dn
->dn_holds
));
1052 ASSERT(refcount_is_zero(&dn
->dn_tx_holds
));
1054 dnode_destroy(dn
); /* implicit zrl_remove() */
1055 zrl_destroy(&dnh
->dnh_zrlock
);
1056 dnh
->dnh_dnode
= NULL
;
1058 kmem_free(children_dnodes
, sizeof (dnode_children_t
) +
1059 children_dnodes
->dnc_count
* sizeof (dnode_handle_t
));
1063 * Return true if the given index is interior to a dnode already
1064 * allocated in the block. That is, the index is neither free nor
1065 * allocated, but is consumed by a large dnode.
1067 * The dnode_phys_t buffer may not be in sync with the in-core dnode
1068 * structure, so we try to check the dnode structure first and fall back
1069 * to the dnode_phys_t buffer it doesn't exist.
1072 dnode_is_consumed(dmu_buf_impl_t
*db
, int idx
)
1074 dnode_handle_t
*dnh
;
1075 dmu_object_type_t ot
;
1076 dnode_children_t
*children_dnodes
;
1077 dnode_phys_t
*dn_block
;
1081 children_dnodes
= dmu_buf_get_user(&db
->db
);
1082 dn_block
= (dnode_phys_t
*)db
->db
.db_data
;
1084 for (i
= 0; i
< idx
; i
+= skip
) {
1085 dnh
= &children_dnodes
->dnc_children
[i
];
1087 zrl_add(&dnh
->dnh_zrlock
);
1088 if (dnh
->dnh_dnode
!= NULL
) {
1089 ot
= dnh
->dnh_dnode
->dn_type
;
1090 skip
= dnh
->dnh_dnode
->dn_num_slots
;
1092 ot
= dn_block
[i
].dn_type
;
1093 skip
= dn_block
[i
].dn_extra_slots
+ 1;
1095 zrl_remove(&dnh
->dnh_zrlock
);
1097 if (ot
== DMU_OT_NONE
)
1105 * Return true if the given index in the dnode block is a valid
1106 * allocated dnode. That is, the index is not consumed by a large
1107 * dnode and is not free.
1109 * The dnode_phys_t buffer may not be in sync with the in-core dnode
1110 * structure, so we try to check the dnode structure first and fall back
1111 * to the dnode_phys_t buffer it doesn't exist.
1114 dnode_is_allocated(dmu_buf_impl_t
*db
, int idx
)
1116 dnode_handle_t
*dnh
;
1117 dmu_object_type_t ot
;
1118 dnode_children_t
*children_dnodes
;
1119 dnode_phys_t
*dn_block
;
1121 if (dnode_is_consumed(db
, idx
))
1124 children_dnodes
= dmu_buf_get_user(&db
->db
);
1125 dn_block
= (dnode_phys_t
*)db
->db
.db_data
;
1127 dnh
= &children_dnodes
->dnc_children
[idx
];
1129 zrl_add(&dnh
->dnh_zrlock
);
1130 if (dnh
->dnh_dnode
!= NULL
)
1131 ot
= dnh
->dnh_dnode
->dn_type
;
1133 ot
= dn_block
[idx
].dn_type
;
1134 zrl_remove(&dnh
->dnh_zrlock
);
1136 return (ot
!= DMU_OT_NONE
);
1140 * Return true if the given range of indices in the dnode block are
1141 * free. That is, the starting index is not consumed by a large dnode
1142 * and none of the indices are allocated.
1144 * The dnode_phys_t buffer may not be in sync with the in-core dnode
1145 * structure, so we try to check the dnode structure first and fall back
1146 * to the dnode_phys_t buffer it doesn't exist.
1149 dnode_is_free(dmu_buf_impl_t
*db
, int idx
, int slots
)
1151 dnode_handle_t
*dnh
;
1152 dmu_object_type_t ot
;
1153 dnode_children_t
*children_dnodes
;
1154 dnode_phys_t
*dn_block
;
1157 if (idx
+ slots
> DNODES_PER_BLOCK
)
1160 children_dnodes
= dmu_buf_get_user(&db
->db
);
1161 dn_block
= (dnode_phys_t
*)db
->db
.db_data
;
1163 if (dnode_is_consumed(db
, idx
))
1166 for (i
= idx
; i
< idx
+ slots
; i
++) {
1167 dnh
= &children_dnodes
->dnc_children
[i
];
1169 zrl_add(&dnh
->dnh_zrlock
);
1170 if (dnh
->dnh_dnode
!= NULL
)
1171 ot
= dnh
->dnh_dnode
->dn_type
;
1173 ot
= dn_block
[i
].dn_type
;
1174 zrl_remove(&dnh
->dnh_zrlock
);
1176 if (ot
!= DMU_OT_NONE
)
1185 * EINVAL - invalid object number.
1186 * ENOSPC - hole too small to fulfill "slots" request
1187 * ENOENT - the requested dnode is not allocated
1189 * succeeds even for free dnodes.
1192 dnode_hold_impl(objset_t
*os
, uint64_t object
, int flag
, int slots
,
1193 void *tag
, dnode_t
**dnp
)
1195 int epb
, idx
, err
, i
;
1196 int drop_struct_lock
= FALSE
;
1201 dnode_children_t
*children_dnodes
;
1202 dnode_phys_t
*dn_block_begin
;
1203 dnode_handle_t
*dnh
;
1205 ASSERT(!(flag
& DNODE_MUST_BE_ALLOCATED
) || (slots
== 0));
1206 ASSERT(!(flag
& DNODE_MUST_BE_FREE
) || (slots
> 0));
1209 * If you are holding the spa config lock as writer, you shouldn't
1210 * be asking the DMU to do *anything* unless it's the root pool
1211 * which may require us to read from the root filesystem while
1212 * holding some (not all) of the locks as writer.
1214 ASSERT(spa_config_held(os
->os_spa
, SCL_ALL
, RW_WRITER
) == 0 ||
1215 (spa_is_root(os
->os_spa
) &&
1216 spa_config_held(os
->os_spa
, SCL_STATE
, RW_WRITER
)));
1218 if (object
== DMU_USERUSED_OBJECT
|| object
== DMU_GROUPUSED_OBJECT
) {
1219 dn
= (object
== DMU_USERUSED_OBJECT
) ?
1220 DMU_USERUSED_DNODE(os
) : DMU_GROUPUSED_DNODE(os
);
1222 return (SET_ERROR(ENOENT
));
1224 if ((flag
& DNODE_MUST_BE_ALLOCATED
) && type
== DMU_OT_NONE
)
1225 return (SET_ERROR(ENOENT
));
1226 if ((flag
& DNODE_MUST_BE_FREE
) && type
!= DMU_OT_NONE
)
1227 return (SET_ERROR(EEXIST
));
1229 (void) refcount_add(&dn
->dn_holds
, tag
);
1234 if (object
== 0 || object
>= DN_MAX_OBJECT
)
1235 return (SET_ERROR(EINVAL
));
1237 mdn
= DMU_META_DNODE(os
);
1238 ASSERT(mdn
->dn_object
== DMU_META_DNODE_OBJECT
);
1242 if (!RW_WRITE_HELD(&mdn
->dn_struct_rwlock
)) {
1243 rw_enter(&mdn
->dn_struct_rwlock
, RW_READER
);
1244 drop_struct_lock
= TRUE
;
1247 blk
= dbuf_whichblock(mdn
, 0, object
* sizeof (dnode_phys_t
));
1249 db
= dbuf_hold(mdn
, blk
, FTAG
);
1250 if (drop_struct_lock
)
1251 rw_exit(&mdn
->dn_struct_rwlock
);
1253 return (SET_ERROR(EIO
));
1254 err
= dbuf_read(db
, NULL
, DB_RF_CANFAIL
);
1256 dbuf_rele(db
, FTAG
);
1260 ASSERT3U(db
->db
.db_size
, >=, 1<<DNODE_SHIFT
);
1261 epb
= db
->db
.db_size
>> DNODE_SHIFT
;
1263 ASSERT(DB_DNODE(db
)->dn_type
== DMU_OT_DNODE
);
1264 children_dnodes
= dmu_buf_get_user(&db
->db
);
1265 if (children_dnodes
== NULL
) {
1266 dnode_children_t
*winner
;
1267 children_dnodes
= kmem_zalloc(sizeof (dnode_children_t
) +
1268 epb
* sizeof (dnode_handle_t
), KM_SLEEP
);
1269 children_dnodes
->dnc_count
= epb
;
1270 dnh
= &children_dnodes
->dnc_children
[0];
1271 for (i
= 0; i
< epb
; i
++) {
1272 zrl_init(&dnh
[i
].dnh_zrlock
);
1274 dmu_buf_init_user(&children_dnodes
->dnc_dbu
, NULL
,
1275 dnode_buf_evict_async
, NULL
);
1276 winner
= dmu_buf_set_user(&db
->db
, &children_dnodes
->dnc_dbu
);
1277 if (winner
!= NULL
) {
1279 for (i
= 0; i
< epb
; i
++) {
1280 zrl_destroy(&dnh
[i
].dnh_zrlock
);
1283 kmem_free(children_dnodes
, sizeof (dnode_children_t
) +
1284 epb
* sizeof (dnode_handle_t
));
1285 children_dnodes
= winner
;
1288 ASSERT(children_dnodes
->dnc_count
== epb
);
1290 idx
= object
& (epb
- 1);
1291 dn_block_begin
= (dnode_phys_t
*)db
->db
.db_data
;
1293 if ((flag
& DNODE_MUST_BE_FREE
) && !dnode_is_free(db
, idx
, slots
)) {
1294 dbuf_rele(db
, FTAG
);
1296 } else if ((flag
& DNODE_MUST_BE_ALLOCATED
) &&
1297 !dnode_is_allocated(db
, idx
)) {
1298 dbuf_rele(db
, FTAG
);
1302 dnh
= &children_dnodes
->dnc_children
[idx
];
1303 zrl_add(&dnh
->dnh_zrlock
);
1304 dn
= dnh
->dnh_dnode
;
1306 dn
= dnode_create(os
, dn_block_begin
+ idx
, db
, object
, dnh
);
1308 mutex_enter(&dn
->dn_mtx
);
1310 if (dn
->dn_free_txg
||
1311 ((flag
& DNODE_MUST_BE_FREE
) && !refcount_is_zero(&dn
->dn_holds
))) {
1312 mutex_exit(&dn
->dn_mtx
);
1313 zrl_remove(&dnh
->dnh_zrlock
);
1314 dbuf_rele(db
, FTAG
);
1315 return (type
== DMU_OT_NONE
? ENOENT
: EEXIST
);
1317 if (refcount_add(&dn
->dn_holds
, tag
) == 1)
1318 dbuf_add_ref(db
, dnh
);
1319 mutex_exit(&dn
->dn_mtx
);
1321 /* Now we can rely on the hold to prevent the dnode from moving. */
1322 zrl_remove(&dnh
->dnh_zrlock
);
1325 ASSERT3P(dn
->dn_dbuf
, ==, db
);
1326 ASSERT3U(dn
->dn_object
, ==, object
);
1327 dbuf_rele(db
, FTAG
);
1334 * Return held dnode if the object is allocated, NULL if not.
1337 dnode_hold(objset_t
*os
, uint64_t object
, void *tag
, dnode_t
**dnp
)
1339 return (dnode_hold_impl(os
, object
, DNODE_MUST_BE_ALLOCATED
, 0, tag
,
1344 * Can only add a reference if there is already at least one
1345 * reference on the dnode. Returns FALSE if unable to add a
1349 dnode_add_ref(dnode_t
*dn
, void *tag
)
1351 mutex_enter(&dn
->dn_mtx
);
1352 if (refcount_is_zero(&dn
->dn_holds
)) {
1353 mutex_exit(&dn
->dn_mtx
);
1356 VERIFY(1 < refcount_add(&dn
->dn_holds
, tag
));
1357 mutex_exit(&dn
->dn_mtx
);
1362 dnode_rele(dnode_t
*dn
, void *tag
)
1364 mutex_enter(&dn
->dn_mtx
);
1365 dnode_rele_and_unlock(dn
, tag
);
1369 dnode_rele_and_unlock(dnode_t
*dn
, void *tag
)
1372 /* Get while the hold prevents the dnode from moving. */
1373 dmu_buf_impl_t
*db
= dn
->dn_dbuf
;
1374 dnode_handle_t
*dnh
= dn
->dn_handle
;
1376 refs
= refcount_remove(&dn
->dn_holds
, tag
);
1377 mutex_exit(&dn
->dn_mtx
);
1380 * It's unsafe to release the last hold on a dnode by dnode_rele() or
1381 * indirectly by dbuf_rele() while relying on the dnode handle to
1382 * prevent the dnode from moving, since releasing the last hold could
1383 * result in the dnode's parent dbuf evicting its dnode handles. For
1384 * that reason anyone calling dnode_rele() or dbuf_rele() without some
1385 * other direct or indirect hold on the dnode must first drop the dnode
1388 ASSERT(refs
> 0 || dnh
->dnh_zrlock
.zr_owner
!= curthread
);
1390 /* NOTE: the DNODE_DNODE does not have a dn_dbuf */
1391 if (refs
== 0 && db
!= NULL
) {
1393 * Another thread could add a hold to the dnode handle in
1394 * dnode_hold_impl() while holding the parent dbuf. Since the
1395 * hold on the parent dbuf prevents the handle from being
1396 * destroyed, the hold on the handle is OK. We can't yet assert
1397 * that the handle has zero references, but that will be
1398 * asserted anyway when the handle gets destroyed.
1405 dnode_setdirty(dnode_t
*dn
, dmu_tx_t
*tx
)
1407 objset_t
*os
= dn
->dn_objset
;
1408 uint64_t txg
= tx
->tx_txg
;
1410 if (DMU_OBJECT_IS_SPECIAL(dn
->dn_object
)) {
1411 dsl_dataset_dirty(os
->os_dsl_dataset
, tx
);
1418 mutex_enter(&dn
->dn_mtx
);
1419 ASSERT(dn
->dn_phys
->dn_type
|| dn
->dn_allocated_txg
);
1420 ASSERT(dn
->dn_free_txg
== 0 || dn
->dn_free_txg
>= txg
);
1421 mutex_exit(&dn
->dn_mtx
);
1425 * Determine old uid/gid when necessary
1427 dmu_objset_userquota_get_ids(dn
, B_TRUE
, tx
);
1429 mutex_enter(&os
->os_lock
);
1432 * If we are already marked dirty, we're done.
1434 if (list_link_active(&dn
->dn_dirty_link
[txg
& TXG_MASK
])) {
1435 mutex_exit(&os
->os_lock
);
1439 ASSERT(!refcount_is_zero(&dn
->dn_holds
) ||
1440 !avl_is_empty(&dn
->dn_dbufs
));
1441 ASSERT(dn
->dn_datablksz
!= 0);
1442 ASSERT0(dn
->dn_next_bonuslen
[txg
&TXG_MASK
]);
1443 ASSERT0(dn
->dn_next_blksz
[txg
&TXG_MASK
]);
1444 ASSERT0(dn
->dn_next_bonustype
[txg
&TXG_MASK
]);
1446 dprintf_ds(os
->os_dsl_dataset
, "obj=%llu txg=%llu\n",
1447 dn
->dn_object
, txg
);
1449 if (dn
->dn_free_txg
> 0 && dn
->dn_free_txg
<= txg
) {
1450 list_insert_tail(&os
->os_free_dnodes
[txg
&TXG_MASK
], dn
);
1452 list_insert_tail(&os
->os_dirty_dnodes
[txg
&TXG_MASK
], dn
);
1455 mutex_exit(&os
->os_lock
);
1458 * The dnode maintains a hold on its containing dbuf as
1459 * long as there are holds on it. Each instantiated child
1460 * dbuf maintains a hold on the dnode. When the last child
1461 * drops its hold, the dnode will drop its hold on the
1462 * containing dbuf. We add a "dirty hold" here so that the
1463 * dnode will hang around after we finish processing its
1466 VERIFY(dnode_add_ref(dn
, (void *)(uintptr_t)tx
->tx_txg
));
1468 (void) dbuf_dirty(dn
->dn_dbuf
, tx
);
1470 dsl_dataset_dirty(os
->os_dsl_dataset
, tx
);
1474 dnode_free(dnode_t
*dn
, dmu_tx_t
*tx
)
1476 int txgoff
= tx
->tx_txg
& TXG_MASK
;
1478 dprintf("dn=%p txg=%llu\n", dn
, tx
->tx_txg
);
1480 /* we should be the only holder... hopefully */
1481 /* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */
1483 mutex_enter(&dn
->dn_mtx
);
1484 if (dn
->dn_type
== DMU_OT_NONE
|| dn
->dn_free_txg
) {
1485 mutex_exit(&dn
->dn_mtx
);
1488 dn
->dn_free_txg
= tx
->tx_txg
;
1489 mutex_exit(&dn
->dn_mtx
);
1492 * If the dnode is already dirty, it needs to be moved from
1493 * the dirty list to the free list.
1495 mutex_enter(&dn
->dn_objset
->os_lock
);
1496 if (list_link_active(&dn
->dn_dirty_link
[txgoff
])) {
1497 list_remove(&dn
->dn_objset
->os_dirty_dnodes
[txgoff
], dn
);
1498 list_insert_tail(&dn
->dn_objset
->os_free_dnodes
[txgoff
], dn
);
1499 mutex_exit(&dn
->dn_objset
->os_lock
);
1501 mutex_exit(&dn
->dn_objset
->os_lock
);
1502 dnode_setdirty(dn
, tx
);
1507 * Try to change the block size for the indicated dnode. This can only
1508 * succeed if there are no blocks allocated or dirty beyond first block
1511 dnode_set_blksz(dnode_t
*dn
, uint64_t size
, int ibs
, dmu_tx_t
*tx
)
1516 ASSERT3U(size
, <=, spa_maxblocksize(dmu_objset_spa(dn
->dn_objset
)));
1518 size
= SPA_MINBLOCKSIZE
;
1520 size
= P2ROUNDUP(size
, SPA_MINBLOCKSIZE
);
1522 if (ibs
== dn
->dn_indblkshift
)
1525 if (size
>> SPA_MINBLOCKSHIFT
== dn
->dn_datablkszsec
&& ibs
== 0)
1528 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1530 /* Check for any allocated blocks beyond the first */
1531 if (dn
->dn_maxblkid
!= 0)
1534 mutex_enter(&dn
->dn_dbufs_mtx
);
1535 for (db
= avl_first(&dn
->dn_dbufs
); db
!= NULL
;
1536 db
= AVL_NEXT(&dn
->dn_dbufs
, db
)) {
1537 if (db
->db_blkid
!= 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1538 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1539 mutex_exit(&dn
->dn_dbufs_mtx
);
1543 mutex_exit(&dn
->dn_dbufs_mtx
);
1545 if (ibs
&& dn
->dn_nlevels
!= 1)
1548 /* resize the old block */
1549 err
= dbuf_hold_impl(dn
, 0, 0, TRUE
, FALSE
, FTAG
, &db
);
1551 dbuf_new_size(db
, size
, tx
);
1552 else if (err
!= ENOENT
)
1555 dnode_setdblksz(dn
, size
);
1556 dnode_setdirty(dn
, tx
);
1557 dn
->dn_next_blksz
[tx
->tx_txg
&TXG_MASK
] = size
;
1559 dn
->dn_indblkshift
= ibs
;
1560 dn
->dn_next_indblkshift
[tx
->tx_txg
&TXG_MASK
] = ibs
;
1562 /* rele after we have fixed the blocksize in the dnode */
1564 dbuf_rele(db
, FTAG
);
1566 rw_exit(&dn
->dn_struct_rwlock
);
1570 rw_exit(&dn
->dn_struct_rwlock
);
1571 return (SET_ERROR(ENOTSUP
));
1574 /* read-holding callers must not rely on the lock being continuously held */
1576 dnode_new_blkid(dnode_t
*dn
, uint64_t blkid
, dmu_tx_t
*tx
, boolean_t have_read
)
1578 uint64_t txgoff
= tx
->tx_txg
& TXG_MASK
;
1579 int epbs
, new_nlevels
;
1582 ASSERT(blkid
!= DMU_BONUS_BLKID
);
1585 RW_READ_HELD(&dn
->dn_struct_rwlock
) :
1586 RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1589 * if we have a read-lock, check to see if we need to do any work
1590 * before upgrading to a write-lock.
1593 if (blkid
<= dn
->dn_maxblkid
)
1596 if (!rw_tryupgrade(&dn
->dn_struct_rwlock
)) {
1597 rw_exit(&dn
->dn_struct_rwlock
);
1598 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1602 if (blkid
<= dn
->dn_maxblkid
)
1605 dn
->dn_maxblkid
= blkid
;
1608 * Compute the number of levels necessary to support the new maxblkid.
1611 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1612 for (sz
= dn
->dn_nblkptr
;
1613 sz
<= blkid
&& sz
>= dn
->dn_nblkptr
; sz
<<= epbs
)
1616 ASSERT3U(new_nlevels
, <=, DN_MAX_LEVELS
);
1618 if (new_nlevels
> dn
->dn_nlevels
) {
1619 int old_nlevels
= dn
->dn_nlevels
;
1622 dbuf_dirty_record_t
*new, *dr
, *dr_next
;
1624 dn
->dn_nlevels
= new_nlevels
;
1626 ASSERT3U(new_nlevels
, >, dn
->dn_next_nlevels
[txgoff
]);
1627 dn
->dn_next_nlevels
[txgoff
] = new_nlevels
;
1629 /* dirty the left indirects */
1630 db
= dbuf_hold_level(dn
, old_nlevels
, 0, FTAG
);
1632 new = dbuf_dirty(db
, tx
);
1633 dbuf_rele(db
, FTAG
);
1635 /* transfer the dirty records to the new indirect */
1636 mutex_enter(&dn
->dn_mtx
);
1637 mutex_enter(&new->dt
.di
.dr_mtx
);
1638 list
= &dn
->dn_dirty_records
[txgoff
];
1639 for (dr
= list_head(list
); dr
; dr
= dr_next
) {
1640 dr_next
= list_next(&dn
->dn_dirty_records
[txgoff
], dr
);
1641 if (dr
->dr_dbuf
->db_level
!= new_nlevels
-1 &&
1642 dr
->dr_dbuf
->db_blkid
!= DMU_BONUS_BLKID
&&
1643 dr
->dr_dbuf
->db_blkid
!= DMU_SPILL_BLKID
) {
1644 ASSERT(dr
->dr_dbuf
->db_level
== old_nlevels
-1);
1645 list_remove(&dn
->dn_dirty_records
[txgoff
], dr
);
1646 list_insert_tail(&new->dt
.di
.dr_children
, dr
);
1647 dr
->dr_parent
= new;
1650 mutex_exit(&new->dt
.di
.dr_mtx
);
1651 mutex_exit(&dn
->dn_mtx
);
1656 rw_downgrade(&dn
->dn_struct_rwlock
);
1660 dnode_dirty_l1(dnode_t
*dn
, uint64_t l1blkid
, dmu_tx_t
*tx
)
1662 dmu_buf_impl_t
*db
= dbuf_hold_level(dn
, 1, l1blkid
, FTAG
);
1664 dmu_buf_will_dirty(&db
->db
, tx
);
1665 dbuf_rele(db
, FTAG
);
1670 dnode_free_range(dnode_t
*dn
, uint64_t off
, uint64_t len
, dmu_tx_t
*tx
)
1673 uint64_t blkoff
, blkid
, nblks
;
1674 int blksz
, blkshift
, head
, tail
;
1678 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1679 blksz
= dn
->dn_datablksz
;
1680 blkshift
= dn
->dn_datablkshift
;
1681 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1683 if (len
== DMU_OBJECT_END
) {
1684 len
= UINT64_MAX
- off
;
1689 * First, block align the region to free:
1692 head
= P2NPHASE(off
, blksz
);
1693 blkoff
= P2PHASE(off
, blksz
);
1694 if ((off
>> blkshift
) > dn
->dn_maxblkid
)
1697 ASSERT(dn
->dn_maxblkid
== 0);
1698 if (off
== 0 && len
>= blksz
) {
1700 * Freeing the whole block; fast-track this request.
1701 * Note that we won't dirty any indirect blocks,
1702 * which is fine because we will be freeing the entire
1703 * file and thus all indirect blocks will be freed
1704 * by free_children().
1709 } else if (off
>= blksz
) {
1710 /* Freeing past end-of-data */
1713 /* Freeing part of the block. */
1715 ASSERT3U(head
, >, 0);
1719 /* zero out any partial block data at the start of the range */
1721 ASSERT3U(blkoff
+ head
, ==, blksz
);
1724 if (dbuf_hold_impl(dn
, 0, dbuf_whichblock(dn
, 0, off
),
1725 TRUE
, FALSE
, FTAG
, &db
) == 0) {
1728 /* don't dirty if it isn't on disk and isn't dirty */
1729 if (db
->db_last_dirty
||
1730 (db
->db_blkptr
&& !BP_IS_HOLE(db
->db_blkptr
))) {
1731 rw_exit(&dn
->dn_struct_rwlock
);
1732 dmu_buf_will_dirty(&db
->db
, tx
);
1733 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1734 data
= db
->db
.db_data
;
1735 bzero(data
+ blkoff
, head
);
1737 dbuf_rele(db
, FTAG
);
1743 /* If the range was less than one block, we're done */
1747 /* If the remaining range is past end of file, we're done */
1748 if ((off
>> blkshift
) > dn
->dn_maxblkid
)
1751 ASSERT(ISP2(blksz
));
1755 tail
= P2PHASE(len
, blksz
);
1757 ASSERT0(P2PHASE(off
, blksz
));
1758 /* zero out any partial block data at the end of the range */
1762 if (dbuf_hold_impl(dn
, 0, dbuf_whichblock(dn
, 0, off
+len
),
1763 TRUE
, FALSE
, FTAG
, &db
) == 0) {
1764 /* don't dirty if not on disk and not dirty */
1765 if (db
->db_last_dirty
||
1766 (db
->db_blkptr
&& !BP_IS_HOLE(db
->db_blkptr
))) {
1767 rw_exit(&dn
->dn_struct_rwlock
);
1768 dmu_buf_will_dirty(&db
->db
, tx
);
1769 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1770 bzero(db
->db
.db_data
, tail
);
1772 dbuf_rele(db
, FTAG
);
1777 /* If the range did not include a full block, we are done */
1781 ASSERT(IS_P2ALIGNED(off
, blksz
));
1782 ASSERT(trunc
|| IS_P2ALIGNED(len
, blksz
));
1783 blkid
= off
>> blkshift
;
1784 nblks
= len
>> blkshift
;
1789 * Dirty all the indirect blocks in this range. Note that only
1790 * the first and last indirect blocks can actually be written
1791 * (if they were partially freed) -- they must be dirtied, even if
1792 * they do not exist on disk yet. The interior blocks will
1793 * be freed by free_children(), so they will not actually be written.
1794 * Even though these interior blocks will not be written, we
1795 * dirty them for two reasons:
1797 * - It ensures that the indirect blocks remain in memory until
1798 * syncing context. (They have already been prefetched by
1799 * dmu_tx_hold_free(), so we don't have to worry about reading
1800 * them serially here.)
1802 * - The dirty space accounting will put pressure on the txg sync
1803 * mechanism to begin syncing, and to delay transactions if there
1804 * is a large amount of freeing. Even though these indirect
1805 * blocks will not be written, we could need to write the same
1806 * amount of space if we copy the freed BPs into deadlists.
1808 if (dn
->dn_nlevels
> 1) {
1809 uint64_t first
, last
, i
, ibyte
;
1812 first
= blkid
>> epbs
;
1813 dnode_dirty_l1(dn
, first
, tx
);
1815 last
= dn
->dn_maxblkid
>> epbs
;
1817 last
= (blkid
+ nblks
- 1) >> epbs
;
1819 dnode_dirty_l1(dn
, last
, tx
);
1821 shift
= dn
->dn_datablkshift
+ dn
->dn_indblkshift
-
1823 for (i
= first
+ 1; i
< last
; i
++) {
1825 * Set i to the blockid of the next non-hole
1826 * level-1 indirect block at or after i. Note
1827 * that dnode_next_offset() operates in terms of
1828 * level-0-equivalent bytes.
1831 err
= dnode_next_offset(dn
, DNODE_FIND_HAVELOCK
,
1838 * Normally we should not see an error, either
1839 * from dnode_next_offset() or dbuf_hold_level()
1840 * (except for ESRCH from dnode_next_offset).
1841 * If there is an i/o error, then when we read
1842 * this block in syncing context, it will use
1843 * ZIO_FLAG_MUSTSUCCEED, and thus hang/panic according
1844 * to the "failmode" property. dnode_next_offset()
1845 * doesn't have a flag to indicate MUSTSUCCEED.
1850 dnode_dirty_l1(dn
, i
, tx
);
1856 * Add this range to the dnode range list.
1857 * We will finish up this free operation in the syncing phase.
1859 mutex_enter(&dn
->dn_mtx
);
1861 int txgoff
= tx
->tx_txg
& TXG_MASK
;
1862 if (dn
->dn_free_ranges
[txgoff
] == NULL
) {
1863 dn
->dn_free_ranges
[txgoff
] =
1864 range_tree_create(NULL
, NULL
, &dn
->dn_mtx
);
1866 range_tree_clear(dn
->dn_free_ranges
[txgoff
], blkid
, nblks
);
1867 range_tree_add(dn
->dn_free_ranges
[txgoff
], blkid
, nblks
);
1869 dprintf_dnode(dn
, "blkid=%llu nblks=%llu txg=%llu\n",
1870 blkid
, nblks
, tx
->tx_txg
);
1871 mutex_exit(&dn
->dn_mtx
);
1873 dbuf_free_range(dn
, blkid
, blkid
+ nblks
- 1, tx
);
1874 dnode_setdirty(dn
, tx
);
1877 rw_exit(&dn
->dn_struct_rwlock
);
1881 dnode_spill_freed(dnode_t
*dn
)
1885 mutex_enter(&dn
->dn_mtx
);
1886 for (i
= 0; i
< TXG_SIZE
; i
++) {
1887 if (dn
->dn_rm_spillblk
[i
] == DN_KILL_SPILLBLK
)
1890 mutex_exit(&dn
->dn_mtx
);
1891 return (i
< TXG_SIZE
);
1894 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
1896 dnode_block_freed(dnode_t
*dn
, uint64_t blkid
)
1898 void *dp
= spa_get_dsl(dn
->dn_objset
->os_spa
);
1901 if (blkid
== DMU_BONUS_BLKID
)
1905 * If we're in the process of opening the pool, dp will not be
1906 * set yet, but there shouldn't be anything dirty.
1911 if (dn
->dn_free_txg
)
1914 if (blkid
== DMU_SPILL_BLKID
)
1915 return (dnode_spill_freed(dn
));
1917 mutex_enter(&dn
->dn_mtx
);
1918 for (i
= 0; i
< TXG_SIZE
; i
++) {
1919 if (dn
->dn_free_ranges
[i
] != NULL
&&
1920 range_tree_contains(dn
->dn_free_ranges
[i
], blkid
, 1))
1923 mutex_exit(&dn
->dn_mtx
);
1924 return (i
< TXG_SIZE
);
1927 /* call from syncing context when we actually write/free space for this dnode */
1929 dnode_diduse_space(dnode_t
*dn
, int64_t delta
)
1932 dprintf_dnode(dn
, "dn=%p dnp=%p used=%llu delta=%lld\n",
1934 (u_longlong_t
)dn
->dn_phys
->dn_used
,
1937 mutex_enter(&dn
->dn_mtx
);
1938 space
= DN_USED_BYTES(dn
->dn_phys
);
1940 ASSERT3U(space
+ delta
, >=, space
); /* no overflow */
1942 ASSERT3U(space
, >=, -delta
); /* no underflow */
1945 if (spa_version(dn
->dn_objset
->os_spa
) < SPA_VERSION_DNODE_BYTES
) {
1946 ASSERT((dn
->dn_phys
->dn_flags
& DNODE_FLAG_USED_BYTES
) == 0);
1947 ASSERT0(P2PHASE(space
, 1<<DEV_BSHIFT
));
1948 dn
->dn_phys
->dn_used
= space
>> DEV_BSHIFT
;
1950 dn
->dn_phys
->dn_used
= space
;
1951 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_USED_BYTES
;
1953 mutex_exit(&dn
->dn_mtx
);
1957 * Call when we think we're going to write/free space in open context to track
1958 * the amount of memory in use by the currently open txg.
1961 dnode_willuse_space(dnode_t
*dn
, int64_t space
, dmu_tx_t
*tx
)
1963 objset_t
*os
= dn
->dn_objset
;
1964 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1965 int64_t aspace
= spa_get_asize(os
->os_spa
, space
);
1968 dsl_dir_willuse_space(ds
->ds_dir
, aspace
, tx
);
1969 dsl_pool_dirty_space(dmu_tx_pool(tx
), space
, tx
);
1972 dmu_tx_willuse_space(tx
, aspace
);
1976 * Scans a block at the indicated "level" looking for a hole or data,
1977 * depending on 'flags'.
1979 * If level > 0, then we are scanning an indirect block looking at its
1980 * pointers. If level == 0, then we are looking at a block of dnodes.
1982 * If we don't find what we are looking for in the block, we return ESRCH.
1983 * Otherwise, return with *offset pointing to the beginning (if searching
1984 * forwards) or end (if searching backwards) of the range covered by the
1985 * block pointer we matched on (or dnode).
1987 * The basic search algorithm used below by dnode_next_offset() is to
1988 * use this function to search up the block tree (widen the search) until
1989 * we find something (i.e., we don't return ESRCH) and then search back
1990 * down the tree (narrow the search) until we reach our original search
1994 dnode_next_offset_level(dnode_t
*dn
, int flags
, uint64_t *offset
,
1995 int lvl
, uint64_t blkfill
, uint64_t txg
)
1997 dmu_buf_impl_t
*db
= NULL
;
1999 uint64_t epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2000 uint64_t epb
= 1ULL << epbs
;
2001 uint64_t minfill
, maxfill
;
2003 int i
, inc
, error
, span
;
2005 hole
= ((flags
& DNODE_FIND_HOLE
) != 0);
2006 inc
= (flags
& DNODE_FIND_BACKWARDS
) ? -1 : 1;
2007 ASSERT(txg
== 0 || !hole
);
2009 if (lvl
== dn
->dn_phys
->dn_nlevels
) {
2011 epb
= dn
->dn_phys
->dn_nblkptr
;
2012 data
= dn
->dn_phys
->dn_blkptr
;
2014 uint64_t blkid
= dbuf_whichblock(dn
, lvl
, *offset
);
2015 error
= dbuf_hold_impl(dn
, lvl
, blkid
, TRUE
, FALSE
, FTAG
, &db
);
2017 if (error
!= ENOENT
)
2022 * This can only happen when we are searching up
2023 * the block tree for data. We don't really need to
2024 * adjust the offset, as we will just end up looking
2025 * at the pointer to this block in its parent, and its
2026 * going to be unallocated, so we will skip over it.
2028 return (SET_ERROR(ESRCH
));
2030 error
= dbuf_read(db
, NULL
, DB_RF_CANFAIL
| DB_RF_HAVESTRUCT
);
2032 dbuf_rele(db
, FTAG
);
2035 data
= db
->db
.db_data
;
2039 if (db
!= NULL
&& txg
!= 0 && (db
->db_blkptr
== NULL
||
2040 db
->db_blkptr
->blk_birth
<= txg
||
2041 BP_IS_HOLE(db
->db_blkptr
))) {
2043 * This can only happen when we are searching up the tree
2044 * and these conditions mean that we need to keep climbing.
2046 error
= SET_ERROR(ESRCH
);
2047 } else if (lvl
== 0) {
2048 dnode_phys_t
*dnp
= data
;
2050 ASSERT(dn
->dn_type
== DMU_OT_DNODE
);
2051 ASSERT(!(flags
& DNODE_FIND_BACKWARDS
));
2053 for (i
= (*offset
>> DNODE_SHIFT
) & (blkfill
- 1);
2054 i
< blkfill
; i
+= dnp
[i
].dn_extra_slots
+ 1) {
2055 if ((dnp
[i
].dn_type
== DMU_OT_NONE
) == hole
)
2060 error
= SET_ERROR(ESRCH
);
2062 *offset
= (*offset
& ~(DNODE_BLOCK_SIZE
- 1)) +
2065 blkptr_t
*bp
= data
;
2066 uint64_t start
= *offset
;
2067 span
= (lvl
- 1) * epbs
+ dn
->dn_datablkshift
;
2069 maxfill
= blkfill
<< ((lvl
- 1) * epbs
);
2076 if (span
>= 8 * sizeof (*offset
)) {
2077 /* This only happens on the highest indirection level */
2078 ASSERT3U((lvl
- 1), ==, dn
->dn_phys
->dn_nlevels
- 1);
2081 *offset
= *offset
>> span
;
2084 for (i
= BF64_GET(*offset
, 0, epbs
);
2085 i
>= 0 && i
< epb
; i
+= inc
) {
2086 if (BP_GET_FILL(&bp
[i
]) >= minfill
&&
2087 BP_GET_FILL(&bp
[i
]) <= maxfill
&&
2088 (hole
|| bp
[i
].blk_birth
> txg
))
2090 if (inc
> 0 || *offset
> 0)
2094 if (span
>= 8 * sizeof (*offset
)) {
2097 *offset
= *offset
<< span
;
2101 /* traversing backwards; position offset at the end */
2102 ASSERT3U(*offset
, <=, start
);
2103 *offset
= MIN(*offset
+ (1ULL << span
) - 1, start
);
2104 } else if (*offset
< start
) {
2107 if (i
< 0 || i
>= epb
)
2108 error
= SET_ERROR(ESRCH
);
2112 dbuf_rele(db
, FTAG
);
2118 * Find the next hole, data, or sparse region at or after *offset.
2119 * The value 'blkfill' tells us how many items we expect to find
2120 * in an L0 data block; this value is 1 for normal objects,
2121 * DNODES_PER_BLOCK for the meta dnode, and some fraction of
2122 * DNODES_PER_BLOCK when searching for sparse regions thereof.
2126 * dnode_next_offset(dn, flags, offset, 1, 1, 0);
2127 * Finds the next/previous hole/data in a file.
2128 * Used in dmu_offset_next().
2130 * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
2131 * Finds the next free/allocated dnode an objset's meta-dnode.
2132 * Only finds objects that have new contents since txg (ie.
2133 * bonus buffer changes and content removal are ignored).
2134 * Used in dmu_object_next().
2136 * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
2137 * Finds the next L2 meta-dnode bp that's at most 1/4 full.
2138 * Used in dmu_object_alloc().
2141 dnode_next_offset(dnode_t
*dn
, int flags
, uint64_t *offset
,
2142 int minlvl
, uint64_t blkfill
, uint64_t txg
)
2144 uint64_t initial_offset
= *offset
;
2148 if (!(flags
& DNODE_FIND_HAVELOCK
))
2149 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2151 if (dn
->dn_phys
->dn_nlevels
== 0) {
2152 error
= SET_ERROR(ESRCH
);
2156 if (dn
->dn_datablkshift
== 0) {
2157 if (*offset
< dn
->dn_datablksz
) {
2158 if (flags
& DNODE_FIND_HOLE
)
2159 *offset
= dn
->dn_datablksz
;
2161 error
= SET_ERROR(ESRCH
);
2166 maxlvl
= dn
->dn_phys
->dn_nlevels
;
2168 for (lvl
= minlvl
; lvl
<= maxlvl
; lvl
++) {
2169 error
= dnode_next_offset_level(dn
,
2170 flags
, offset
, lvl
, blkfill
, txg
);
2175 while (error
== 0 && --lvl
>= minlvl
) {
2176 error
= dnode_next_offset_level(dn
,
2177 flags
, offset
, lvl
, blkfill
, txg
);
2181 * There's always a "virtual hole" at the end of the object, even
2182 * if all BP's which physically exist are non-holes.
2184 if ((flags
& DNODE_FIND_HOLE
) && error
== ESRCH
&& txg
== 0 &&
2185 minlvl
== 1 && blkfill
== 1 && !(flags
& DNODE_FIND_BACKWARDS
)) {
2189 if (error
== 0 && (flags
& DNODE_FIND_BACKWARDS
?
2190 initial_offset
< *offset
: initial_offset
> *offset
))
2191 error
= SET_ERROR(ESRCH
);
2193 if (!(flags
& DNODE_FIND_HAVELOCK
))
2194 rw_exit(&dn
->dn_struct_rwlock
);