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) 2013 by Delphix. All rights reserved.
26 #include <sys/zfs_context.h>
28 #include <sys/dnode.h>
30 #include <sys/dmu_impl.h>
31 #include <sys/dmu_tx.h>
32 #include <sys/dmu_objset.h>
33 #include <sys/dsl_dir.h>
34 #include <sys/dsl_dataset.h>
37 #include <sys/dmu_zfetch.h>
39 static int free_range_compar(const void *node1
, const void *node2
);
41 static kmem_cache_t
*dnode_cache
;
43 * Define DNODE_STATS to turn on statistic gathering. By default, it is only
44 * turned on when DEBUG is also defined.
51 #define DNODE_STAT_ADD(stat) ((stat)++)
53 #define DNODE_STAT_ADD(stat) /* nothing */
54 #endif /* DNODE_STATS */
56 ASSERTV(static dnode_phys_t dnode_phys_zero
);
58 int zfs_default_bs
= SPA_MINBLOCKSHIFT
;
59 int zfs_default_ibs
= DN_MAX_INDBLKSHIFT
;
62 static kmem_cbrc_t
dnode_move(void *, void *, size_t, void *);
67 dnode_cons(void *arg
, void *unused
, int kmflag
)
72 rw_init(&dn
->dn_struct_rwlock
, NULL
, RW_DEFAULT
, NULL
);
73 mutex_init(&dn
->dn_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
74 mutex_init(&dn
->dn_dbufs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
75 cv_init(&dn
->dn_notxholds
, NULL
, CV_DEFAULT
, NULL
);
78 * Every dbuf has a reference, and dropping a tracked reference is
79 * O(number of references), so don't track dn_holds.
81 refcount_create_untracked(&dn
->dn_holds
);
82 refcount_create(&dn
->dn_tx_holds
);
83 list_link_init(&dn
->dn_link
);
85 bzero(&dn
->dn_next_nblkptr
[0], sizeof (dn
->dn_next_nblkptr
));
86 bzero(&dn
->dn_next_nlevels
[0], sizeof (dn
->dn_next_nlevels
));
87 bzero(&dn
->dn_next_indblkshift
[0], sizeof (dn
->dn_next_indblkshift
));
88 bzero(&dn
->dn_next_bonustype
[0], sizeof (dn
->dn_next_bonustype
));
89 bzero(&dn
->dn_rm_spillblk
[0], sizeof (dn
->dn_rm_spillblk
));
90 bzero(&dn
->dn_next_bonuslen
[0], sizeof (dn
->dn_next_bonuslen
));
91 bzero(&dn
->dn_next_blksz
[0], sizeof (dn
->dn_next_blksz
));
93 for (i
= 0; i
< TXG_SIZE
; i
++) {
94 list_link_init(&dn
->dn_dirty_link
[i
]);
95 avl_create(&dn
->dn_ranges
[i
], free_range_compar
,
96 sizeof (free_range_t
),
97 offsetof(struct free_range
, fr_node
));
98 list_create(&dn
->dn_dirty_records
[i
],
99 sizeof (dbuf_dirty_record_t
),
100 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
103 dn
->dn_allocated_txg
= 0;
105 dn
->dn_assigned_txg
= 0;
107 dn
->dn_dirtyctx_firstset
= NULL
;
109 dn
->dn_have_spill
= B_FALSE
;
119 dn
->dn_dbufs_count
= 0;
120 dn
->dn_unlisted_l0_blkid
= 0;
121 list_create(&dn
->dn_dbufs
, sizeof (dmu_buf_impl_t
),
122 offsetof(dmu_buf_impl_t
, db_link
));
130 dnode_dest(void *arg
, void *unused
)
135 rw_destroy(&dn
->dn_struct_rwlock
);
136 mutex_destroy(&dn
->dn_mtx
);
137 mutex_destroy(&dn
->dn_dbufs_mtx
);
138 cv_destroy(&dn
->dn_notxholds
);
139 refcount_destroy(&dn
->dn_holds
);
140 refcount_destroy(&dn
->dn_tx_holds
);
141 ASSERT(!list_link_active(&dn
->dn_link
));
143 for (i
= 0; i
< TXG_SIZE
; i
++) {
144 ASSERT(!list_link_active(&dn
->dn_dirty_link
[i
]));
145 avl_destroy(&dn
->dn_ranges
[i
]);
146 list_destroy(&dn
->dn_dirty_records
[i
]);
147 ASSERT0(dn
->dn_next_nblkptr
[i
]);
148 ASSERT0(dn
->dn_next_nlevels
[i
]);
149 ASSERT0(dn
->dn_next_indblkshift
[i
]);
150 ASSERT0(dn
->dn_next_bonustype
[i
]);
151 ASSERT0(dn
->dn_rm_spillblk
[i
]);
152 ASSERT0(dn
->dn_next_bonuslen
[i
]);
153 ASSERT0(dn
->dn_next_blksz
[i
]);
156 ASSERT0(dn
->dn_allocated_txg
);
157 ASSERT0(dn
->dn_free_txg
);
158 ASSERT0(dn
->dn_assigned_txg
);
159 ASSERT0(dn
->dn_dirtyctx
);
160 ASSERT3P(dn
->dn_dirtyctx_firstset
, ==, NULL
);
161 ASSERT3P(dn
->dn_bonus
, ==, NULL
);
162 ASSERT(!dn
->dn_have_spill
);
163 ASSERT3P(dn
->dn_zio
, ==, NULL
);
164 ASSERT0(dn
->dn_oldused
);
165 ASSERT0(dn
->dn_oldflags
);
166 ASSERT0(dn
->dn_olduid
);
167 ASSERT0(dn
->dn_oldgid
);
168 ASSERT0(dn
->dn_newuid
);
169 ASSERT0(dn
->dn_newgid
);
170 ASSERT0(dn
->dn_id_flags
);
172 ASSERT0(dn
->dn_dbufs_count
);
173 ASSERT0(dn
->dn_unlisted_l0_blkid
);
174 list_destroy(&dn
->dn_dbufs
);
180 ASSERT(dnode_cache
== NULL
);
181 dnode_cache
= kmem_cache_create("dnode_t", sizeof (dnode_t
),
182 0, dnode_cons
, dnode_dest
, NULL
, NULL
, NULL
, KMC_KMEM
);
183 kmem_cache_set_move(dnode_cache
, dnode_move
);
189 kmem_cache_destroy(dnode_cache
);
196 dnode_verify(dnode_t
*dn
)
198 int drop_struct_lock
= FALSE
;
201 ASSERT(dn
->dn_objset
);
202 ASSERT(dn
->dn_handle
->dnh_dnode
== dn
);
204 ASSERT(DMU_OT_IS_VALID(dn
->dn_phys
->dn_type
));
206 if (!(zfs_flags
& ZFS_DEBUG_DNODE_VERIFY
))
209 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
210 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
211 drop_struct_lock
= TRUE
;
213 if (dn
->dn_phys
->dn_type
!= DMU_OT_NONE
|| dn
->dn_allocated_txg
!= 0) {
215 ASSERT3U(dn
->dn_indblkshift
, <=, SPA_MAXBLOCKSHIFT
);
216 if (dn
->dn_datablkshift
) {
217 ASSERT3U(dn
->dn_datablkshift
, >=, SPA_MINBLOCKSHIFT
);
218 ASSERT3U(dn
->dn_datablkshift
, <=, SPA_MAXBLOCKSHIFT
);
219 ASSERT3U(1<<dn
->dn_datablkshift
, ==, dn
->dn_datablksz
);
221 ASSERT3U(dn
->dn_nlevels
, <=, 30);
222 ASSERT(DMU_OT_IS_VALID(dn
->dn_type
));
223 ASSERT3U(dn
->dn_nblkptr
, >=, 1);
224 ASSERT3U(dn
->dn_nblkptr
, <=, DN_MAX_NBLKPTR
);
225 ASSERT3U(dn
->dn_bonuslen
, <=, DN_MAX_BONUSLEN
);
226 ASSERT3U(dn
->dn_datablksz
, ==,
227 dn
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
);
228 ASSERT3U(ISP2(dn
->dn_datablksz
), ==, dn
->dn_datablkshift
!= 0);
229 ASSERT3U((dn
->dn_nblkptr
- 1) * sizeof (blkptr_t
) +
230 dn
->dn_bonuslen
, <=, DN_MAX_BONUSLEN
);
231 for (i
= 0; i
< TXG_SIZE
; i
++) {
232 ASSERT3U(dn
->dn_next_nlevels
[i
], <=, dn
->dn_nlevels
);
235 if (dn
->dn_phys
->dn_type
!= DMU_OT_NONE
)
236 ASSERT3U(dn
->dn_phys
->dn_nlevels
, <=, dn
->dn_nlevels
);
237 ASSERT(DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) || dn
->dn_dbuf
!= NULL
);
238 if (dn
->dn_dbuf
!= NULL
) {
239 ASSERT3P(dn
->dn_phys
, ==,
240 (dnode_phys_t
*)dn
->dn_dbuf
->db
.db_data
+
241 (dn
->dn_object
% (dn
->dn_dbuf
->db
.db_size
>> DNODE_SHIFT
)));
243 if (drop_struct_lock
)
244 rw_exit(&dn
->dn_struct_rwlock
);
249 dnode_byteswap(dnode_phys_t
*dnp
)
251 uint64_t *buf64
= (void*)&dnp
->dn_blkptr
;
254 if (dnp
->dn_type
== DMU_OT_NONE
) {
255 bzero(dnp
, sizeof (dnode_phys_t
));
259 dnp
->dn_datablkszsec
= BSWAP_16(dnp
->dn_datablkszsec
);
260 dnp
->dn_bonuslen
= BSWAP_16(dnp
->dn_bonuslen
);
261 dnp
->dn_maxblkid
= BSWAP_64(dnp
->dn_maxblkid
);
262 dnp
->dn_used
= BSWAP_64(dnp
->dn_used
);
265 * dn_nblkptr is only one byte, so it's OK to read it in either
266 * byte order. We can't read dn_bouslen.
268 ASSERT(dnp
->dn_indblkshift
<= SPA_MAXBLOCKSHIFT
);
269 ASSERT(dnp
->dn_nblkptr
<= DN_MAX_NBLKPTR
);
270 for (i
= 0; i
< dnp
->dn_nblkptr
* sizeof (blkptr_t
)/8; i
++)
271 buf64
[i
] = BSWAP_64(buf64
[i
]);
274 * OK to check dn_bonuslen for zero, because it won't matter if
275 * we have the wrong byte order. This is necessary because the
276 * dnode dnode is smaller than a regular dnode.
278 if (dnp
->dn_bonuslen
!= 0) {
280 * Note that the bonus length calculated here may be
281 * longer than the actual bonus buffer. This is because
282 * we always put the bonus buffer after the last block
283 * pointer (instead of packing it against the end of the
286 int off
= (dnp
->dn_nblkptr
-1) * sizeof (blkptr_t
);
287 size_t len
= DN_MAX_BONUSLEN
- off
;
288 dmu_object_byteswap_t byteswap
;
289 ASSERT(DMU_OT_IS_VALID(dnp
->dn_bonustype
));
290 byteswap
= DMU_OT_BYTESWAP(dnp
->dn_bonustype
);
291 dmu_ot_byteswap
[byteswap
].ob_func(dnp
->dn_bonus
+ off
, len
);
294 /* Swap SPILL block if we have one */
295 if (dnp
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
)
296 byteswap_uint64_array(&dnp
->dn_spill
, sizeof (blkptr_t
));
301 dnode_buf_byteswap(void *vbuf
, size_t size
)
303 dnode_phys_t
*buf
= vbuf
;
306 ASSERT3U(sizeof (dnode_phys_t
), ==, (1<<DNODE_SHIFT
));
307 ASSERT((size
& (sizeof (dnode_phys_t
)-1)) == 0);
309 size
>>= DNODE_SHIFT
;
310 for (i
= 0; i
< size
; i
++) {
317 free_range_compar(const void *node1
, const void *node2
)
319 const free_range_t
*rp1
= node1
;
320 const free_range_t
*rp2
= node2
;
322 if (rp1
->fr_blkid
< rp2
->fr_blkid
)
324 else if (rp1
->fr_blkid
> rp2
->fr_blkid
)
330 dnode_setbonuslen(dnode_t
*dn
, int newsize
, dmu_tx_t
*tx
)
332 ASSERT3U(refcount_count(&dn
->dn_holds
), >=, 1);
334 dnode_setdirty(dn
, tx
);
335 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
336 ASSERT3U(newsize
, <=, DN_MAX_BONUSLEN
-
337 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
));
338 dn
->dn_bonuslen
= newsize
;
340 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = DN_ZERO_BONUSLEN
;
342 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonuslen
;
343 rw_exit(&dn
->dn_struct_rwlock
);
347 dnode_setbonus_type(dnode_t
*dn
, dmu_object_type_t newtype
, dmu_tx_t
*tx
)
349 ASSERT3U(refcount_count(&dn
->dn_holds
), >=, 1);
350 dnode_setdirty(dn
, tx
);
351 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
352 dn
->dn_bonustype
= newtype
;
353 dn
->dn_next_bonustype
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonustype
;
354 rw_exit(&dn
->dn_struct_rwlock
);
358 dnode_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
360 ASSERT3U(refcount_count(&dn
->dn_holds
), >=, 1);
361 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
362 dnode_setdirty(dn
, tx
);
363 dn
->dn_rm_spillblk
[tx
->tx_txg
&TXG_MASK
] = DN_KILL_SPILLBLK
;
364 dn
->dn_have_spill
= B_FALSE
;
368 dnode_setdblksz(dnode_t
*dn
, int size
)
370 ASSERT0(P2PHASE(size
, SPA_MINBLOCKSIZE
));
371 ASSERT3U(size
, <=, SPA_MAXBLOCKSIZE
);
372 ASSERT3U(size
, >=, SPA_MINBLOCKSIZE
);
373 ASSERT3U(size
>> SPA_MINBLOCKSHIFT
, <,
374 1<<(sizeof (dn
->dn_phys
->dn_datablkszsec
) * 8));
375 dn
->dn_datablksz
= size
;
376 dn
->dn_datablkszsec
= size
>> SPA_MINBLOCKSHIFT
;
377 dn
->dn_datablkshift
= ISP2(size
) ? highbit(size
- 1) : 0;
381 dnode_create(objset_t
*os
, dnode_phys_t
*dnp
, dmu_buf_impl_t
*db
,
382 uint64_t object
, dnode_handle_t
*dnh
)
384 dnode_t
*dn
= kmem_cache_alloc(dnode_cache
, KM_PUSHPAGE
);
386 ASSERT(!POINTER_IS_VALID(dn
->dn_objset
));
390 * Defer setting dn_objset until the dnode is ready to be a candidate
391 * for the dnode_move() callback.
393 dn
->dn_object
= object
;
398 if (dnp
->dn_datablkszsec
) {
399 dnode_setdblksz(dn
, dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
);
401 dn
->dn_datablksz
= 0;
402 dn
->dn_datablkszsec
= 0;
403 dn
->dn_datablkshift
= 0;
405 dn
->dn_indblkshift
= dnp
->dn_indblkshift
;
406 dn
->dn_nlevels
= dnp
->dn_nlevels
;
407 dn
->dn_type
= dnp
->dn_type
;
408 dn
->dn_nblkptr
= dnp
->dn_nblkptr
;
409 dn
->dn_checksum
= dnp
->dn_checksum
;
410 dn
->dn_compress
= dnp
->dn_compress
;
411 dn
->dn_bonustype
= dnp
->dn_bonustype
;
412 dn
->dn_bonuslen
= dnp
->dn_bonuslen
;
413 dn
->dn_maxblkid
= dnp
->dn_maxblkid
;
414 dn
->dn_have_spill
= ((dnp
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
) != 0);
417 dmu_zfetch_init(&dn
->dn_zfetch
, dn
);
419 ASSERT(DMU_OT_IS_VALID(dn
->dn_phys
->dn_type
));
421 mutex_enter(&os
->os_lock
);
422 list_insert_head(&os
->os_dnodes
, dn
);
425 * Everything else must be valid before assigning dn_objset makes the
426 * dnode eligible for dnode_move().
429 mutex_exit(&os
->os_lock
);
431 arc_space_consume(sizeof (dnode_t
), ARC_SPACE_OTHER
);
436 * Caller must be holding the dnode handle, which is released upon return.
439 dnode_destroy(dnode_t
*dn
)
441 objset_t
*os
= dn
->dn_objset
;
443 ASSERT((dn
->dn_id_flags
& DN_ID_NEW_EXIST
) == 0);
445 mutex_enter(&os
->os_lock
);
446 POINTER_INVALIDATE(&dn
->dn_objset
);
447 list_remove(&os
->os_dnodes
, dn
);
448 mutex_exit(&os
->os_lock
);
450 /* the dnode can no longer move, so we can release the handle */
451 zrl_remove(&dn
->dn_handle
->dnh_zrlock
);
453 dn
->dn_allocated_txg
= 0;
455 dn
->dn_assigned_txg
= 0;
458 if (dn
->dn_dirtyctx_firstset
!= NULL
) {
459 kmem_free(dn
->dn_dirtyctx_firstset
, 1);
460 dn
->dn_dirtyctx_firstset
= NULL
;
462 if (dn
->dn_bonus
!= NULL
) {
463 mutex_enter(&dn
->dn_bonus
->db_mtx
);
464 dbuf_evict(dn
->dn_bonus
);
469 dn
->dn_have_spill
= B_FALSE
;
477 dn
->dn_unlisted_l0_blkid
= 0;
479 dmu_zfetch_rele(&dn
->dn_zfetch
);
480 kmem_cache_free(dnode_cache
, dn
);
481 arc_space_return(sizeof (dnode_t
), ARC_SPACE_OTHER
);
485 dnode_allocate(dnode_t
*dn
, dmu_object_type_t ot
, int blocksize
, int ibs
,
486 dmu_object_type_t bonustype
, int bonuslen
, dmu_tx_t
*tx
)
491 blocksize
= 1 << zfs_default_bs
;
492 else if (blocksize
> SPA_MAXBLOCKSIZE
)
493 blocksize
= SPA_MAXBLOCKSIZE
;
495 blocksize
= P2ROUNDUP(blocksize
, SPA_MINBLOCKSIZE
);
498 ibs
= zfs_default_ibs
;
500 ibs
= MIN(MAX(ibs
, DN_MIN_INDBLKSHIFT
), DN_MAX_INDBLKSHIFT
);
502 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn
->dn_objset
,
503 dn
->dn_object
, tx
->tx_txg
, blocksize
, ibs
);
505 ASSERT(dn
->dn_type
== DMU_OT_NONE
);
506 ASSERT(bcmp(dn
->dn_phys
, &dnode_phys_zero
, sizeof (dnode_phys_t
)) == 0);
507 ASSERT(dn
->dn_phys
->dn_type
== DMU_OT_NONE
);
508 ASSERT(ot
!= DMU_OT_NONE
);
509 ASSERT(DMU_OT_IS_VALID(ot
));
510 ASSERT((bonustype
== DMU_OT_NONE
&& bonuslen
== 0) ||
511 (bonustype
== DMU_OT_SA
&& bonuslen
== 0) ||
512 (bonustype
!= DMU_OT_NONE
&& bonuslen
!= 0));
513 ASSERT(DMU_OT_IS_VALID(bonustype
));
514 ASSERT3U(bonuslen
, <=, DN_MAX_BONUSLEN
);
515 ASSERT(dn
->dn_type
== DMU_OT_NONE
);
516 ASSERT0(dn
->dn_maxblkid
);
517 ASSERT0(dn
->dn_allocated_txg
);
518 ASSERT0(dn
->dn_assigned_txg
);
519 ASSERT(refcount_is_zero(&dn
->dn_tx_holds
));
520 ASSERT3U(refcount_count(&dn
->dn_holds
), <=, 1);
521 ASSERT3P(list_head(&dn
->dn_dbufs
), ==, NULL
);
523 for (i
= 0; i
< TXG_SIZE
; i
++) {
524 ASSERT0(dn
->dn_next_nblkptr
[i
]);
525 ASSERT0(dn
->dn_next_nlevels
[i
]);
526 ASSERT0(dn
->dn_next_indblkshift
[i
]);
527 ASSERT0(dn
->dn_next_bonuslen
[i
]);
528 ASSERT0(dn
->dn_next_bonustype
[i
]);
529 ASSERT0(dn
->dn_rm_spillblk
[i
]);
530 ASSERT0(dn
->dn_next_blksz
[i
]);
531 ASSERT(!list_link_active(&dn
->dn_dirty_link
[i
]));
532 ASSERT3P(list_head(&dn
->dn_dirty_records
[i
]), ==, NULL
);
533 ASSERT0(avl_numnodes(&dn
->dn_ranges
[i
]));
537 dnode_setdblksz(dn
, blocksize
);
538 dn
->dn_indblkshift
= ibs
;
540 if (bonustype
== DMU_OT_SA
) /* Maximize bonus space for SA */
544 ((DN_MAX_BONUSLEN
- bonuslen
) >> SPA_BLKPTRSHIFT
);
545 dn
->dn_bonustype
= bonustype
;
546 dn
->dn_bonuslen
= bonuslen
;
547 dn
->dn_checksum
= ZIO_CHECKSUM_INHERIT
;
548 dn
->dn_compress
= ZIO_COMPRESS_INHERIT
;
552 if (dn
->dn_dirtyctx_firstset
) {
553 kmem_free(dn
->dn_dirtyctx_firstset
, 1);
554 dn
->dn_dirtyctx_firstset
= NULL
;
557 dn
->dn_allocated_txg
= tx
->tx_txg
;
560 dnode_setdirty(dn
, tx
);
561 dn
->dn_next_indblkshift
[tx
->tx_txg
& TXG_MASK
] = ibs
;
562 dn
->dn_next_bonuslen
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonuslen
;
563 dn
->dn_next_bonustype
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_bonustype
;
564 dn
->dn_next_blksz
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_datablksz
;
568 dnode_reallocate(dnode_t
*dn
, dmu_object_type_t ot
, int blocksize
,
569 dmu_object_type_t bonustype
, int bonuslen
, dmu_tx_t
*tx
)
573 ASSERT3U(blocksize
, >=, SPA_MINBLOCKSIZE
);
574 ASSERT3U(blocksize
, <=, SPA_MAXBLOCKSIZE
);
575 ASSERT0(blocksize
% SPA_MINBLOCKSIZE
);
576 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
|| dmu_tx_private_ok(tx
));
577 ASSERT(tx
->tx_txg
!= 0);
578 ASSERT((bonustype
== DMU_OT_NONE
&& bonuslen
== 0) ||
579 (bonustype
!= DMU_OT_NONE
&& bonuslen
!= 0) ||
580 (bonustype
== DMU_OT_SA
&& bonuslen
== 0));
581 ASSERT(DMU_OT_IS_VALID(bonustype
));
582 ASSERT3U(bonuslen
, <=, DN_MAX_BONUSLEN
);
584 /* clean up any unreferenced dbufs */
585 dnode_evict_dbufs(dn
);
589 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
590 dnode_setdirty(dn
, tx
);
591 if (dn
->dn_datablksz
!= blocksize
) {
592 /* change blocksize */
593 ASSERT(dn
->dn_maxblkid
== 0 &&
594 (BP_IS_HOLE(&dn
->dn_phys
->dn_blkptr
[0]) ||
595 dnode_block_freed(dn
, 0)));
596 dnode_setdblksz(dn
, blocksize
);
597 dn
->dn_next_blksz
[tx
->tx_txg
&TXG_MASK
] = blocksize
;
599 if (dn
->dn_bonuslen
!= bonuslen
)
600 dn
->dn_next_bonuslen
[tx
->tx_txg
&TXG_MASK
] = bonuslen
;
602 if (bonustype
== DMU_OT_SA
) /* Maximize bonus space for SA */
605 nblkptr
= 1 + ((DN_MAX_BONUSLEN
- bonuslen
) >> SPA_BLKPTRSHIFT
);
606 if (dn
->dn_bonustype
!= bonustype
)
607 dn
->dn_next_bonustype
[tx
->tx_txg
&TXG_MASK
] = bonustype
;
608 if (dn
->dn_nblkptr
!= nblkptr
)
609 dn
->dn_next_nblkptr
[tx
->tx_txg
&TXG_MASK
] = nblkptr
;
610 if (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
) {
611 dbuf_rm_spill(dn
, tx
);
612 dnode_rm_spill(dn
, tx
);
614 rw_exit(&dn
->dn_struct_rwlock
);
619 /* change bonus size and type */
620 mutex_enter(&dn
->dn_mtx
);
621 dn
->dn_bonustype
= bonustype
;
622 dn
->dn_bonuslen
= bonuslen
;
623 dn
->dn_nblkptr
= nblkptr
;
624 dn
->dn_checksum
= ZIO_CHECKSUM_INHERIT
;
625 dn
->dn_compress
= ZIO_COMPRESS_INHERIT
;
626 ASSERT3U(dn
->dn_nblkptr
, <=, DN_MAX_NBLKPTR
);
628 /* fix up the bonus db_size */
630 dn
->dn_bonus
->db
.db_size
=
631 DN_MAX_BONUSLEN
- (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
632 ASSERT(dn
->dn_bonuslen
<= dn
->dn_bonus
->db
.db_size
);
635 dn
->dn_allocated_txg
= tx
->tx_txg
;
636 mutex_exit(&dn
->dn_mtx
);
642 uint64_t dms_dnode_invalid
;
643 uint64_t dms_dnode_recheck1
;
644 uint64_t dms_dnode_recheck2
;
645 uint64_t dms_dnode_special
;
646 uint64_t dms_dnode_handle
;
647 uint64_t dms_dnode_rwlock
;
648 uint64_t dms_dnode_active
;
650 #endif /* DNODE_STATS */
653 dnode_move_impl(dnode_t
*odn
, dnode_t
*ndn
)
657 ASSERT(!RW_LOCK_HELD(&odn
->dn_struct_rwlock
));
658 ASSERT(MUTEX_NOT_HELD(&odn
->dn_mtx
));
659 ASSERT(MUTEX_NOT_HELD(&odn
->dn_dbufs_mtx
));
660 ASSERT(!RW_LOCK_HELD(&odn
->dn_zfetch
.zf_rwlock
));
663 ndn
->dn_objset
= odn
->dn_objset
;
664 ndn
->dn_object
= odn
->dn_object
;
665 ndn
->dn_dbuf
= odn
->dn_dbuf
;
666 ndn
->dn_handle
= odn
->dn_handle
;
667 ndn
->dn_phys
= odn
->dn_phys
;
668 ndn
->dn_type
= odn
->dn_type
;
669 ndn
->dn_bonuslen
= odn
->dn_bonuslen
;
670 ndn
->dn_bonustype
= odn
->dn_bonustype
;
671 ndn
->dn_nblkptr
= odn
->dn_nblkptr
;
672 ndn
->dn_checksum
= odn
->dn_checksum
;
673 ndn
->dn_compress
= odn
->dn_compress
;
674 ndn
->dn_nlevels
= odn
->dn_nlevels
;
675 ndn
->dn_indblkshift
= odn
->dn_indblkshift
;
676 ndn
->dn_datablkshift
= odn
->dn_datablkshift
;
677 ndn
->dn_datablkszsec
= odn
->dn_datablkszsec
;
678 ndn
->dn_datablksz
= odn
->dn_datablksz
;
679 ndn
->dn_maxblkid
= odn
->dn_maxblkid
;
680 bcopy(&odn
->dn_next_nblkptr
[0], &ndn
->dn_next_nblkptr
[0],
681 sizeof (odn
->dn_next_nblkptr
));
682 bcopy(&odn
->dn_next_nlevels
[0], &ndn
->dn_next_nlevels
[0],
683 sizeof (odn
->dn_next_nlevels
));
684 bcopy(&odn
->dn_next_indblkshift
[0], &ndn
->dn_next_indblkshift
[0],
685 sizeof (odn
->dn_next_indblkshift
));
686 bcopy(&odn
->dn_next_bonustype
[0], &ndn
->dn_next_bonustype
[0],
687 sizeof (odn
->dn_next_bonustype
));
688 bcopy(&odn
->dn_rm_spillblk
[0], &ndn
->dn_rm_spillblk
[0],
689 sizeof (odn
->dn_rm_spillblk
));
690 bcopy(&odn
->dn_next_bonuslen
[0], &ndn
->dn_next_bonuslen
[0],
691 sizeof (odn
->dn_next_bonuslen
));
692 bcopy(&odn
->dn_next_blksz
[0], &ndn
->dn_next_blksz
[0],
693 sizeof (odn
->dn_next_blksz
));
694 for (i
= 0; i
< TXG_SIZE
; i
++) {
695 list_move_tail(&ndn
->dn_dirty_records
[i
],
696 &odn
->dn_dirty_records
[i
]);
698 bcopy(&odn
->dn_ranges
[0], &ndn
->dn_ranges
[0], sizeof (odn
->dn_ranges
));
699 ndn
->dn_allocated_txg
= odn
->dn_allocated_txg
;
700 ndn
->dn_free_txg
= odn
->dn_free_txg
;
701 ndn
->dn_assigned_txg
= odn
->dn_assigned_txg
;
702 ndn
->dn_dirtyctx
= odn
->dn_dirtyctx
;
703 ndn
->dn_dirtyctx_firstset
= odn
->dn_dirtyctx_firstset
;
704 ASSERT(refcount_count(&odn
->dn_tx_holds
) == 0);
705 refcount_transfer(&ndn
->dn_holds
, &odn
->dn_holds
);
706 ASSERT(list_is_empty(&ndn
->dn_dbufs
));
707 list_move_tail(&ndn
->dn_dbufs
, &odn
->dn_dbufs
);
708 ndn
->dn_dbufs_count
= odn
->dn_dbufs_count
;
709 ndn
->dn_unlisted_l0_blkid
= odn
->dn_unlisted_l0_blkid
;
710 ndn
->dn_bonus
= odn
->dn_bonus
;
711 ndn
->dn_have_spill
= odn
->dn_have_spill
;
712 ndn
->dn_zio
= odn
->dn_zio
;
713 ndn
->dn_oldused
= odn
->dn_oldused
;
714 ndn
->dn_oldflags
= odn
->dn_oldflags
;
715 ndn
->dn_olduid
= odn
->dn_olduid
;
716 ndn
->dn_oldgid
= odn
->dn_oldgid
;
717 ndn
->dn_newuid
= odn
->dn_newuid
;
718 ndn
->dn_newgid
= odn
->dn_newgid
;
719 ndn
->dn_id_flags
= odn
->dn_id_flags
;
720 dmu_zfetch_init(&ndn
->dn_zfetch
, NULL
);
721 list_move_tail(&ndn
->dn_zfetch
.zf_stream
, &odn
->dn_zfetch
.zf_stream
);
722 ndn
->dn_zfetch
.zf_dnode
= odn
->dn_zfetch
.zf_dnode
;
723 ndn
->dn_zfetch
.zf_stream_cnt
= odn
->dn_zfetch
.zf_stream_cnt
;
724 ndn
->dn_zfetch
.zf_alloc_fail
= odn
->dn_zfetch
.zf_alloc_fail
;
727 * Update back pointers. Updating the handle fixes the back pointer of
728 * every descendant dbuf as well as the bonus dbuf.
730 ASSERT(ndn
->dn_handle
->dnh_dnode
== odn
);
731 ndn
->dn_handle
->dnh_dnode
= ndn
;
732 if (ndn
->dn_zfetch
.zf_dnode
== odn
) {
733 ndn
->dn_zfetch
.zf_dnode
= ndn
;
737 * Invalidate the original dnode by clearing all of its back pointers.
740 odn
->dn_handle
= NULL
;
741 list_create(&odn
->dn_dbufs
, sizeof (dmu_buf_impl_t
),
742 offsetof(dmu_buf_impl_t
, db_link
));
743 odn
->dn_dbufs_count
= 0;
744 odn
->dn_unlisted_l0_blkid
= 0;
745 odn
->dn_bonus
= NULL
;
746 odn
->dn_zfetch
.zf_dnode
= NULL
;
749 * Set the low bit of the objset pointer to ensure that dnode_move()
750 * recognizes the dnode as invalid in any subsequent callback.
752 POINTER_INVALIDATE(&odn
->dn_objset
);
755 * Satisfy the destructor.
757 for (i
= 0; i
< TXG_SIZE
; i
++) {
758 list_create(&odn
->dn_dirty_records
[i
],
759 sizeof (dbuf_dirty_record_t
),
760 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
761 odn
->dn_ranges
[i
].avl_root
= NULL
;
762 odn
->dn_ranges
[i
].avl_numnodes
= 0;
763 odn
->dn_next_nlevels
[i
] = 0;
764 odn
->dn_next_indblkshift
[i
] = 0;
765 odn
->dn_next_bonustype
[i
] = 0;
766 odn
->dn_rm_spillblk
[i
] = 0;
767 odn
->dn_next_bonuslen
[i
] = 0;
768 odn
->dn_next_blksz
[i
] = 0;
770 odn
->dn_allocated_txg
= 0;
771 odn
->dn_free_txg
= 0;
772 odn
->dn_assigned_txg
= 0;
773 odn
->dn_dirtyctx
= 0;
774 odn
->dn_dirtyctx_firstset
= NULL
;
775 odn
->dn_have_spill
= B_FALSE
;
778 odn
->dn_oldflags
= 0;
783 odn
->dn_id_flags
= 0;
789 odn
->dn_moved
= (uint8_t)-1;
794 dnode_move(void *buf
, void *newbuf
, size_t size
, void *arg
)
796 dnode_t
*odn
= buf
, *ndn
= newbuf
;
802 * The dnode is on the objset's list of known dnodes if the objset
803 * pointer is valid. We set the low bit of the objset pointer when
804 * freeing the dnode to invalidate it, and the memory patterns written
805 * by kmem (baddcafe and deadbeef) set at least one of the two low bits.
806 * A newly created dnode sets the objset pointer last of all to indicate
807 * that the dnode is known and in a valid state to be moved by this
811 if (!POINTER_IS_VALID(os
)) {
812 DNODE_STAT_ADD(dnode_move_stats
.dms_dnode_invalid
);
813 return (KMEM_CBRC_DONT_KNOW
);
817 * Ensure that the objset does not go away during the move.
819 rw_enter(&os_lock
, RW_WRITER
);
820 if (os
!= odn
->dn_objset
) {
822 DNODE_STAT_ADD(dnode_move_stats
.dms_dnode_recheck1
);
823 return (KMEM_CBRC_DONT_KNOW
);
827 * If the dnode is still valid, then so is the objset. We know that no
828 * valid objset can be freed while we hold os_lock, so we can safely
829 * ensure that the objset remains in use.
831 mutex_enter(&os
->os_lock
);
834 * Recheck the objset pointer in case the dnode was removed just before
835 * acquiring the lock.
837 if (os
!= odn
->dn_objset
) {
838 mutex_exit(&os
->os_lock
);
840 DNODE_STAT_ADD(dnode_move_stats
.dms_dnode_recheck2
);
841 return (KMEM_CBRC_DONT_KNOW
);
845 * At this point we know that as long as we hold os->os_lock, the dnode
846 * cannot be freed and fields within the dnode can be safely accessed.
847 * The objset listing this dnode cannot go away as long as this dnode is
851 if (DMU_OBJECT_IS_SPECIAL(odn
->dn_object
)) {
852 mutex_exit(&os
->os_lock
);
853 DNODE_STAT_ADD(dnode_move_stats
.dms_dnode_special
);
854 return (KMEM_CBRC_NO
);
856 ASSERT(odn
->dn_dbuf
!= NULL
); /* only "special" dnodes have no parent */
859 * Lock the dnode handle to prevent the dnode from obtaining any new
860 * holds. This also prevents the descendant dbufs and the bonus dbuf
861 * from accessing the dnode, so that we can discount their holds. The
862 * handle is safe to access because we know that while the dnode cannot
863 * go away, neither can its handle. Once we hold dnh_zrlock, we can
864 * safely move any dnode referenced only by dbufs.
866 if (!zrl_tryenter(&odn
->dn_handle
->dnh_zrlock
)) {
867 mutex_exit(&os
->os_lock
);
868 DNODE_STAT_ADD(dnode_move_stats
.dms_dnode_handle
);
869 return (KMEM_CBRC_LATER
);
873 * Ensure a consistent view of the dnode's holds and the dnode's dbufs.
874 * We need to guarantee that there is a hold for every dbuf in order to
875 * determine whether the dnode is actively referenced. Falsely matching
876 * a dbuf to an active hold would lead to an unsafe move. It's possible
877 * that a thread already having an active dnode hold is about to add a
878 * dbuf, and we can't compare hold and dbuf counts while the add is in
881 if (!rw_tryenter(&odn
->dn_struct_rwlock
, RW_WRITER
)) {
882 zrl_exit(&odn
->dn_handle
->dnh_zrlock
);
883 mutex_exit(&os
->os_lock
);
884 DNODE_STAT_ADD(dnode_move_stats
.dms_dnode_rwlock
);
885 return (KMEM_CBRC_LATER
);
889 * A dbuf may be removed (evicted) without an active dnode hold. In that
890 * case, the dbuf count is decremented under the handle lock before the
891 * dbuf's hold is released. This order ensures that if we count the hold
892 * after the dbuf is removed but before its hold is released, we will
893 * treat the unmatched hold as active and exit safely. If we count the
894 * hold before the dbuf is removed, the hold is discounted, and the
895 * removal is blocked until the move completes.
897 refcount
= refcount_count(&odn
->dn_holds
);
898 ASSERT(refcount
>= 0);
899 dbufs
= odn
->dn_dbufs_count
;
901 /* We can't have more dbufs than dnode holds. */
902 ASSERT3U(dbufs
, <=, refcount
);
903 DTRACE_PROBE3(dnode__move
, dnode_t
*, odn
, int64_t, refcount
,
906 if (refcount
> dbufs
) {
907 rw_exit(&odn
->dn_struct_rwlock
);
908 zrl_exit(&odn
->dn_handle
->dnh_zrlock
);
909 mutex_exit(&os
->os_lock
);
910 DNODE_STAT_ADD(dnode_move_stats
.dms_dnode_active
);
911 return (KMEM_CBRC_LATER
);
914 rw_exit(&odn
->dn_struct_rwlock
);
917 * At this point we know that anyone with a hold on the dnode is not
918 * actively referencing it. The dnode is known and in a valid state to
919 * move. We're holding the locks needed to execute the critical section.
921 dnode_move_impl(odn
, ndn
);
923 list_link_replace(&odn
->dn_link
, &ndn
->dn_link
);
924 /* If the dnode was safe to move, the refcount cannot have changed. */
925 ASSERT(refcount
== refcount_count(&ndn
->dn_holds
));
926 ASSERT(dbufs
== ndn
->dn_dbufs_count
);
927 zrl_exit(&ndn
->dn_handle
->dnh_zrlock
); /* handle has moved */
928 mutex_exit(&os
->os_lock
);
930 return (KMEM_CBRC_YES
);
935 dnode_special_close(dnode_handle_t
*dnh
)
937 dnode_t
*dn
= dnh
->dnh_dnode
;
940 * Wait for final references to the dnode to clear. This can
941 * only happen if the arc is asyncronously evicting state that
942 * has a hold on this dnode while we are trying to evict this
945 while (refcount_count(&dn
->dn_holds
) > 0)
947 zrl_add(&dnh
->dnh_zrlock
);
948 dnode_destroy(dn
); /* implicit zrl_remove() */
949 zrl_destroy(&dnh
->dnh_zrlock
);
950 dnh
->dnh_dnode
= NULL
;
954 dnode_special_open(objset_t
*os
, dnode_phys_t
*dnp
, uint64_t object
,
957 dnode_t
*dn
= dnode_create(os
, dnp
, NULL
, object
, dnh
);
959 zrl_init(&dnh
->dnh_zrlock
);
965 dnode_buf_pageout(dmu_buf_t
*db
, void *arg
)
967 dnode_children_t
*children_dnodes
= arg
;
969 int epb
= db
->db_size
>> DNODE_SHIFT
;
971 ASSERT(epb
== children_dnodes
->dnc_count
);
973 for (i
= 0; i
< epb
; i
++) {
974 dnode_handle_t
*dnh
= &children_dnodes
->dnc_children
[i
];
978 * The dnode handle lock guards against the dnode moving to
979 * another valid address, so there is no need here to guard
980 * against changes to or from NULL.
982 if (dnh
->dnh_dnode
== NULL
) {
983 zrl_destroy(&dnh
->dnh_zrlock
);
987 zrl_add(&dnh
->dnh_zrlock
);
990 * If there are holds on this dnode, then there should
991 * be holds on the dnode's containing dbuf as well; thus
992 * it wouldn't be eligible for eviction and this function
993 * would not have been called.
995 ASSERT(refcount_is_zero(&dn
->dn_holds
));
996 ASSERT(refcount_is_zero(&dn
->dn_tx_holds
));
998 dnode_destroy(dn
); /* implicit zrl_remove() */
999 zrl_destroy(&dnh
->dnh_zrlock
);
1000 dnh
->dnh_dnode
= NULL
;
1002 kmem_free(children_dnodes
, sizeof (dnode_children_t
) +
1003 (epb
- 1) * sizeof (dnode_handle_t
));
1008 * EINVAL - invalid object number.
1010 * succeeds even for free dnodes.
1013 dnode_hold_impl(objset_t
*os
, uint64_t object
, int flag
,
1014 void *tag
, dnode_t
**dnp
)
1017 int drop_struct_lock
= FALSE
;
1022 dnode_children_t
*children_dnodes
;
1023 dnode_handle_t
*dnh
;
1026 * If you are holding the spa config lock as writer, you shouldn't
1027 * be asking the DMU to do *anything* unless it's the root pool
1028 * which may require us to read from the root filesystem while
1029 * holding some (not all) of the locks as writer.
1031 ASSERT(spa_config_held(os
->os_spa
, SCL_ALL
, RW_WRITER
) == 0 ||
1032 (spa_is_root(os
->os_spa
) &&
1033 spa_config_held(os
->os_spa
, SCL_STATE
, RW_WRITER
)));
1035 if (object
== DMU_USERUSED_OBJECT
|| object
== DMU_GROUPUSED_OBJECT
) {
1036 dn
= (object
== DMU_USERUSED_OBJECT
) ?
1037 DMU_USERUSED_DNODE(os
) : DMU_GROUPUSED_DNODE(os
);
1039 return (SET_ERROR(ENOENT
));
1041 if ((flag
& DNODE_MUST_BE_ALLOCATED
) && type
== DMU_OT_NONE
)
1042 return (SET_ERROR(ENOENT
));
1043 if ((flag
& DNODE_MUST_BE_FREE
) && type
!= DMU_OT_NONE
)
1044 return (SET_ERROR(EEXIST
));
1046 (void) refcount_add(&dn
->dn_holds
, tag
);
1051 if (object
== 0 || object
>= DN_MAX_OBJECT
)
1052 return (SET_ERROR(EINVAL
));
1054 mdn
= DMU_META_DNODE(os
);
1055 ASSERT(mdn
->dn_object
== DMU_META_DNODE_OBJECT
);
1059 if (!RW_WRITE_HELD(&mdn
->dn_struct_rwlock
)) {
1060 rw_enter(&mdn
->dn_struct_rwlock
, RW_READER
);
1061 drop_struct_lock
= TRUE
;
1064 blk
= dbuf_whichblock(mdn
, object
* sizeof (dnode_phys_t
));
1066 db
= dbuf_hold(mdn
, blk
, FTAG
);
1067 if (drop_struct_lock
)
1068 rw_exit(&mdn
->dn_struct_rwlock
);
1070 return (SET_ERROR(EIO
));
1071 err
= dbuf_read(db
, NULL
, DB_RF_CANFAIL
);
1073 dbuf_rele(db
, FTAG
);
1077 ASSERT3U(db
->db
.db_size
, >=, 1<<DNODE_SHIFT
);
1078 epb
= db
->db
.db_size
>> DNODE_SHIFT
;
1080 idx
= object
& (epb
-1);
1082 ASSERT(DB_DNODE(db
)->dn_type
== DMU_OT_DNODE
);
1083 children_dnodes
= dmu_buf_get_user(&db
->db
);
1084 if (children_dnodes
== NULL
) {
1086 dnode_children_t
*winner
;
1087 children_dnodes
= kmem_alloc(sizeof (dnode_children_t
) +
1088 (epb
- 1) * sizeof (dnode_handle_t
),
1089 KM_PUSHPAGE
| KM_NODEBUG
);
1090 children_dnodes
->dnc_count
= epb
;
1091 dnh
= &children_dnodes
->dnc_children
[0];
1092 for (i
= 0; i
< epb
; i
++) {
1093 zrl_init(&dnh
[i
].dnh_zrlock
);
1094 dnh
[i
].dnh_dnode
= NULL
;
1096 if ((winner
= dmu_buf_set_user(&db
->db
, children_dnodes
, NULL
,
1097 dnode_buf_pageout
))) {
1098 kmem_free(children_dnodes
, sizeof (dnode_children_t
) +
1099 (epb
- 1) * sizeof (dnode_handle_t
));
1100 children_dnodes
= winner
;
1103 ASSERT(children_dnodes
->dnc_count
== epb
);
1105 dnh
= &children_dnodes
->dnc_children
[idx
];
1106 zrl_add(&dnh
->dnh_zrlock
);
1107 if ((dn
= dnh
->dnh_dnode
) == NULL
) {
1108 dnode_phys_t
*phys
= (dnode_phys_t
*)db
->db
.db_data
+idx
;
1111 dn
= dnode_create(os
, phys
, db
, object
, dnh
);
1112 winner
= atomic_cas_ptr(&dnh
->dnh_dnode
, NULL
, dn
);
1113 if (winner
!= NULL
) {
1114 zrl_add(&dnh
->dnh_zrlock
);
1115 dnode_destroy(dn
); /* implicit zrl_remove() */
1120 mutex_enter(&dn
->dn_mtx
);
1122 if (dn
->dn_free_txg
||
1123 ((flag
& DNODE_MUST_BE_ALLOCATED
) && type
== DMU_OT_NONE
) ||
1124 ((flag
& DNODE_MUST_BE_FREE
) &&
1125 (type
!= DMU_OT_NONE
|| !refcount_is_zero(&dn
->dn_holds
)))) {
1126 mutex_exit(&dn
->dn_mtx
);
1127 zrl_remove(&dnh
->dnh_zrlock
);
1128 dbuf_rele(db
, FTAG
);
1129 return (type
== DMU_OT_NONE
? ENOENT
: EEXIST
);
1131 mutex_exit(&dn
->dn_mtx
);
1133 if (refcount_add(&dn
->dn_holds
, tag
) == 1)
1134 dbuf_add_ref(db
, dnh
);
1135 /* Now we can rely on the hold to prevent the dnode from moving. */
1136 zrl_remove(&dnh
->dnh_zrlock
);
1139 ASSERT3P(dn
->dn_dbuf
, ==, db
);
1140 ASSERT3U(dn
->dn_object
, ==, object
);
1141 dbuf_rele(db
, FTAG
);
1148 * Return held dnode if the object is allocated, NULL if not.
1151 dnode_hold(objset_t
*os
, uint64_t object
, void *tag
, dnode_t
**dnp
)
1153 return (dnode_hold_impl(os
, object
, DNODE_MUST_BE_ALLOCATED
, tag
, dnp
));
1157 * Can only add a reference if there is already at least one
1158 * reference on the dnode. Returns FALSE if unable to add a
1162 dnode_add_ref(dnode_t
*dn
, void *tag
)
1164 mutex_enter(&dn
->dn_mtx
);
1165 if (refcount_is_zero(&dn
->dn_holds
)) {
1166 mutex_exit(&dn
->dn_mtx
);
1169 VERIFY(1 < refcount_add(&dn
->dn_holds
, tag
));
1170 mutex_exit(&dn
->dn_mtx
);
1175 dnode_rele(dnode_t
*dn
, void *tag
)
1178 /* Get while the hold prevents the dnode from moving. */
1179 dmu_buf_impl_t
*db
= dn
->dn_dbuf
;
1180 dnode_handle_t
*dnh
= dn
->dn_handle
;
1182 mutex_enter(&dn
->dn_mtx
);
1183 refs
= refcount_remove(&dn
->dn_holds
, tag
);
1184 mutex_exit(&dn
->dn_mtx
);
1187 * It's unsafe to release the last hold on a dnode by dnode_rele() or
1188 * indirectly by dbuf_rele() while relying on the dnode handle to
1189 * prevent the dnode from moving, since releasing the last hold could
1190 * result in the dnode's parent dbuf evicting its dnode handles. For
1191 * that reason anyone calling dnode_rele() or dbuf_rele() without some
1192 * other direct or indirect hold on the dnode must first drop the dnode
1195 ASSERT(refs
> 0 || dnh
->dnh_zrlock
.zr_owner
!= curthread
);
1197 /* NOTE: the DNODE_DNODE does not have a dn_dbuf */
1198 if (refs
== 0 && db
!= NULL
) {
1200 * Another thread could add a hold to the dnode handle in
1201 * dnode_hold_impl() while holding the parent dbuf. Since the
1202 * hold on the parent dbuf prevents the handle from being
1203 * destroyed, the hold on the handle is OK. We can't yet assert
1204 * that the handle has zero references, but that will be
1205 * asserted anyway when the handle gets destroyed.
1212 dnode_setdirty(dnode_t
*dn
, dmu_tx_t
*tx
)
1214 objset_t
*os
= dn
->dn_objset
;
1215 uint64_t txg
= tx
->tx_txg
;
1217 if (DMU_OBJECT_IS_SPECIAL(dn
->dn_object
)) {
1218 dsl_dataset_dirty(os
->os_dsl_dataset
, tx
);
1225 mutex_enter(&dn
->dn_mtx
);
1226 ASSERT(dn
->dn_phys
->dn_type
|| dn
->dn_allocated_txg
);
1227 ASSERT(dn
->dn_free_txg
== 0 || dn
->dn_free_txg
>= txg
);
1228 mutex_exit(&dn
->dn_mtx
);
1232 * Determine old uid/gid when necessary
1234 dmu_objset_userquota_get_ids(dn
, B_TRUE
, tx
);
1236 mutex_enter(&os
->os_lock
);
1239 * If we are already marked dirty, we're done.
1241 if (list_link_active(&dn
->dn_dirty_link
[txg
& TXG_MASK
])) {
1242 mutex_exit(&os
->os_lock
);
1246 ASSERT(!refcount_is_zero(&dn
->dn_holds
) || list_head(&dn
->dn_dbufs
));
1247 ASSERT(dn
->dn_datablksz
!= 0);
1248 ASSERT0(dn
->dn_next_bonuslen
[txg
&TXG_MASK
]);
1249 ASSERT0(dn
->dn_next_blksz
[txg
&TXG_MASK
]);
1250 ASSERT0(dn
->dn_next_bonustype
[txg
&TXG_MASK
]);
1252 dprintf_ds(os
->os_dsl_dataset
, "obj=%llu txg=%llu\n",
1253 dn
->dn_object
, txg
);
1255 if (dn
->dn_free_txg
> 0 && dn
->dn_free_txg
<= txg
) {
1256 list_insert_tail(&os
->os_free_dnodes
[txg
&TXG_MASK
], dn
);
1258 list_insert_tail(&os
->os_dirty_dnodes
[txg
&TXG_MASK
], dn
);
1261 mutex_exit(&os
->os_lock
);
1264 * The dnode maintains a hold on its containing dbuf as
1265 * long as there are holds on it. Each instantiated child
1266 * dbuf maintains a hold on the dnode. When the last child
1267 * drops its hold, the dnode will drop its hold on the
1268 * containing dbuf. We add a "dirty hold" here so that the
1269 * dnode will hang around after we finish processing its
1272 VERIFY(dnode_add_ref(dn
, (void *)(uintptr_t)tx
->tx_txg
));
1274 (void) dbuf_dirty(dn
->dn_dbuf
, tx
);
1276 dsl_dataset_dirty(os
->os_dsl_dataset
, tx
);
1280 dnode_free(dnode_t
*dn
, dmu_tx_t
*tx
)
1282 int txgoff
= tx
->tx_txg
& TXG_MASK
;
1284 dprintf("dn=%p txg=%llu\n", dn
, tx
->tx_txg
);
1286 /* we should be the only holder... hopefully */
1287 /* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */
1289 mutex_enter(&dn
->dn_mtx
);
1290 if (dn
->dn_type
== DMU_OT_NONE
|| dn
->dn_free_txg
) {
1291 mutex_exit(&dn
->dn_mtx
);
1294 dn
->dn_free_txg
= tx
->tx_txg
;
1295 mutex_exit(&dn
->dn_mtx
);
1298 * If the dnode is already dirty, it needs to be moved from
1299 * the dirty list to the free list.
1301 mutex_enter(&dn
->dn_objset
->os_lock
);
1302 if (list_link_active(&dn
->dn_dirty_link
[txgoff
])) {
1303 list_remove(&dn
->dn_objset
->os_dirty_dnodes
[txgoff
], dn
);
1304 list_insert_tail(&dn
->dn_objset
->os_free_dnodes
[txgoff
], dn
);
1305 mutex_exit(&dn
->dn_objset
->os_lock
);
1307 mutex_exit(&dn
->dn_objset
->os_lock
);
1308 dnode_setdirty(dn
, tx
);
1313 * Try to change the block size for the indicated dnode. This can only
1314 * succeed if there are no blocks allocated or dirty beyond first block
1317 dnode_set_blksz(dnode_t
*dn
, uint64_t size
, int ibs
, dmu_tx_t
*tx
)
1319 dmu_buf_impl_t
*db
, *db_next
;
1323 size
= SPA_MINBLOCKSIZE
;
1324 if (size
> SPA_MAXBLOCKSIZE
)
1325 size
= SPA_MAXBLOCKSIZE
;
1327 size
= P2ROUNDUP(size
, SPA_MINBLOCKSIZE
);
1329 if (ibs
== dn
->dn_indblkshift
)
1332 if (size
>> SPA_MINBLOCKSHIFT
== dn
->dn_datablkszsec
&& ibs
== 0)
1335 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1337 /* Check for any allocated blocks beyond the first */
1338 if (dn
->dn_phys
->dn_maxblkid
!= 0)
1341 mutex_enter(&dn
->dn_dbufs_mtx
);
1342 for (db
= list_head(&dn
->dn_dbufs
); db
; db
= db_next
) {
1343 db_next
= list_next(&dn
->dn_dbufs
, db
);
1345 if (db
->db_blkid
!= 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1346 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1347 mutex_exit(&dn
->dn_dbufs_mtx
);
1351 mutex_exit(&dn
->dn_dbufs_mtx
);
1353 if (ibs
&& dn
->dn_nlevels
!= 1)
1356 /* resize the old block */
1357 err
= dbuf_hold_impl(dn
, 0, 0, TRUE
, FTAG
, &db
);
1359 dbuf_new_size(db
, size
, tx
);
1360 else if (err
!= ENOENT
)
1363 dnode_setdblksz(dn
, size
);
1364 dnode_setdirty(dn
, tx
);
1365 dn
->dn_next_blksz
[tx
->tx_txg
&TXG_MASK
] = size
;
1367 dn
->dn_indblkshift
= ibs
;
1368 dn
->dn_next_indblkshift
[tx
->tx_txg
&TXG_MASK
] = ibs
;
1370 /* rele after we have fixed the blocksize in the dnode */
1372 dbuf_rele(db
, FTAG
);
1374 rw_exit(&dn
->dn_struct_rwlock
);
1378 rw_exit(&dn
->dn_struct_rwlock
);
1379 return (SET_ERROR(ENOTSUP
));
1382 /* read-holding callers must not rely on the lock being continuously held */
1384 dnode_new_blkid(dnode_t
*dn
, uint64_t blkid
, dmu_tx_t
*tx
, boolean_t have_read
)
1386 uint64_t txgoff
= tx
->tx_txg
& TXG_MASK
;
1387 int epbs
, new_nlevels
;
1390 ASSERT(blkid
!= DMU_BONUS_BLKID
);
1393 RW_READ_HELD(&dn
->dn_struct_rwlock
) :
1394 RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1397 * if we have a read-lock, check to see if we need to do any work
1398 * before upgrading to a write-lock.
1401 if (blkid
<= dn
->dn_maxblkid
)
1404 if (!rw_tryupgrade(&dn
->dn_struct_rwlock
)) {
1405 rw_exit(&dn
->dn_struct_rwlock
);
1406 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1410 if (blkid
<= dn
->dn_maxblkid
)
1413 dn
->dn_maxblkid
= blkid
;
1416 * Compute the number of levels necessary to support the new maxblkid.
1419 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1420 for (sz
= dn
->dn_nblkptr
;
1421 sz
<= blkid
&& sz
>= dn
->dn_nblkptr
; sz
<<= epbs
)
1424 if (new_nlevels
> dn
->dn_nlevels
) {
1425 int old_nlevels
= dn
->dn_nlevels
;
1428 dbuf_dirty_record_t
*new, *dr
, *dr_next
;
1430 dn
->dn_nlevels
= new_nlevels
;
1432 ASSERT3U(new_nlevels
, >, dn
->dn_next_nlevels
[txgoff
]);
1433 dn
->dn_next_nlevels
[txgoff
] = new_nlevels
;
1435 /* dirty the left indirects */
1436 db
= dbuf_hold_level(dn
, old_nlevels
, 0, FTAG
);
1438 new = dbuf_dirty(db
, tx
);
1439 dbuf_rele(db
, FTAG
);
1441 /* transfer the dirty records to the new indirect */
1442 mutex_enter(&dn
->dn_mtx
);
1443 mutex_enter(&new->dt
.di
.dr_mtx
);
1444 list
= &dn
->dn_dirty_records
[txgoff
];
1445 for (dr
= list_head(list
); dr
; dr
= dr_next
) {
1446 dr_next
= list_next(&dn
->dn_dirty_records
[txgoff
], dr
);
1447 if (dr
->dr_dbuf
->db_level
!= new_nlevels
-1 &&
1448 dr
->dr_dbuf
->db_blkid
!= DMU_BONUS_BLKID
&&
1449 dr
->dr_dbuf
->db_blkid
!= DMU_SPILL_BLKID
) {
1450 ASSERT(dr
->dr_dbuf
->db_level
== old_nlevels
-1);
1451 list_remove(&dn
->dn_dirty_records
[txgoff
], dr
);
1452 list_insert_tail(&new->dt
.di
.dr_children
, dr
);
1453 dr
->dr_parent
= new;
1456 mutex_exit(&new->dt
.di
.dr_mtx
);
1457 mutex_exit(&dn
->dn_mtx
);
1462 rw_downgrade(&dn
->dn_struct_rwlock
);
1466 dnode_clear_range(dnode_t
*dn
, uint64_t blkid
, uint64_t nblks
, dmu_tx_t
*tx
)
1468 avl_tree_t
*tree
= &dn
->dn_ranges
[tx
->tx_txg
&TXG_MASK
];
1471 free_range_t rp_tofind
;
1472 uint64_t endblk
= blkid
+ nblks
;
1474 ASSERT(MUTEX_HELD(&dn
->dn_mtx
));
1475 ASSERT(nblks
<= UINT64_MAX
- blkid
); /* no overflow */
1477 dprintf_dnode(dn
, "blkid=%llu nblks=%llu txg=%llu\n",
1478 blkid
, nblks
, tx
->tx_txg
);
1479 rp_tofind
.fr_blkid
= blkid
;
1480 rp
= avl_find(tree
, &rp_tofind
, &where
);
1482 rp
= avl_nearest(tree
, where
, AVL_BEFORE
);
1484 rp
= avl_nearest(tree
, where
, AVL_AFTER
);
1486 while (rp
&& (rp
->fr_blkid
<= blkid
+ nblks
)) {
1487 uint64_t fr_endblk
= rp
->fr_blkid
+ rp
->fr_nblks
;
1488 free_range_t
*nrp
= AVL_NEXT(tree
, rp
);
1490 if (blkid
<= rp
->fr_blkid
&& endblk
>= fr_endblk
) {
1491 /* clear this entire range */
1492 avl_remove(tree
, rp
);
1493 kmem_free(rp
, sizeof (free_range_t
));
1494 } else if (blkid
<= rp
->fr_blkid
&&
1495 endblk
> rp
->fr_blkid
&& endblk
< fr_endblk
) {
1496 /* clear the beginning of this range */
1497 rp
->fr_blkid
= endblk
;
1498 rp
->fr_nblks
= fr_endblk
- endblk
;
1499 } else if (blkid
> rp
->fr_blkid
&& blkid
< fr_endblk
&&
1500 endblk
>= fr_endblk
) {
1501 /* clear the end of this range */
1502 rp
->fr_nblks
= blkid
- rp
->fr_blkid
;
1503 } else if (blkid
> rp
->fr_blkid
&& endblk
< fr_endblk
) {
1504 /* clear a chunk out of this range */
1505 free_range_t
*new_rp
=
1506 kmem_alloc(sizeof (free_range_t
), KM_PUSHPAGE
);
1508 new_rp
->fr_blkid
= endblk
;
1509 new_rp
->fr_nblks
= fr_endblk
- endblk
;
1510 avl_insert_here(tree
, new_rp
, rp
, AVL_AFTER
);
1511 rp
->fr_nblks
= blkid
- rp
->fr_blkid
;
1513 /* there may be no overlap */
1519 dnode_free_range(dnode_t
*dn
, uint64_t off
, uint64_t len
, dmu_tx_t
*tx
)
1522 uint64_t blkoff
, blkid
, nblks
;
1523 int blksz
, blkshift
, head
, tail
;
1527 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1528 blksz
= dn
->dn_datablksz
;
1529 blkshift
= dn
->dn_datablkshift
;
1530 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1532 if (len
== DMU_OBJECT_END
) {
1533 len
= UINT64_MAX
- off
;
1538 * First, block align the region to free:
1541 head
= P2NPHASE(off
, blksz
);
1542 blkoff
= P2PHASE(off
, blksz
);
1543 if ((off
>> blkshift
) > dn
->dn_maxblkid
)
1546 ASSERT(dn
->dn_maxblkid
== 0);
1547 if (off
== 0 && len
>= blksz
) {
1548 /* Freeing the whole block; fast-track this request */
1552 } else if (off
>= blksz
) {
1553 /* Freeing past end-of-data */
1556 /* Freeing part of the block. */
1558 ASSERT3U(head
, >, 0);
1562 /* zero out any partial block data at the start of the range */
1564 ASSERT3U(blkoff
+ head
, ==, blksz
);
1567 if (dbuf_hold_impl(dn
, 0, dbuf_whichblock(dn
, off
), TRUE
,
1571 /* don't dirty if it isn't on disk and isn't dirty */
1572 if (db
->db_last_dirty
||
1573 (db
->db_blkptr
&& !BP_IS_HOLE(db
->db_blkptr
))) {
1574 rw_exit(&dn
->dn_struct_rwlock
);
1575 dbuf_will_dirty(db
, tx
);
1576 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1577 data
= db
->db
.db_data
;
1578 bzero(data
+ blkoff
, head
);
1580 dbuf_rele(db
, FTAG
);
1586 /* If the range was less than one block, we're done */
1590 /* If the remaining range is past end of file, we're done */
1591 if ((off
>> blkshift
) > dn
->dn_maxblkid
)
1594 ASSERT(ISP2(blksz
));
1598 tail
= P2PHASE(len
, blksz
);
1600 ASSERT0(P2PHASE(off
, blksz
));
1601 /* zero out any partial block data at the end of the range */
1605 if (dbuf_hold_impl(dn
, 0, dbuf_whichblock(dn
, off
+len
),
1606 TRUE
, FTAG
, &db
) == 0) {
1607 /* don't dirty if not on disk and not dirty */
1608 if (db
->db_last_dirty
||
1609 (db
->db_blkptr
&& !BP_IS_HOLE(db
->db_blkptr
))) {
1610 rw_exit(&dn
->dn_struct_rwlock
);
1611 dbuf_will_dirty(db
, tx
);
1612 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
1613 bzero(db
->db
.db_data
, tail
);
1615 dbuf_rele(db
, FTAG
);
1620 /* If the range did not include a full block, we are done */
1624 ASSERT(IS_P2ALIGNED(off
, blksz
));
1625 ASSERT(trunc
|| IS_P2ALIGNED(len
, blksz
));
1626 blkid
= off
>> blkshift
;
1627 nblks
= len
>> blkshift
;
1632 * Read in and mark all the level-1 indirects dirty,
1633 * so that they will stay in memory until syncing phase.
1634 * Always dirty the first and last indirect to make sure
1635 * we dirty all the partial indirects.
1637 if (dn
->dn_nlevels
> 1) {
1638 uint64_t i
, first
, last
;
1639 int shift
= epbs
+ dn
->dn_datablkshift
;
1641 first
= blkid
>> epbs
;
1642 if ((db
= dbuf_hold_level(dn
, 1, first
, FTAG
))) {
1643 dbuf_will_dirty(db
, tx
);
1644 dbuf_rele(db
, FTAG
);
1647 last
= dn
->dn_maxblkid
>> epbs
;
1649 last
= (blkid
+ nblks
- 1) >> epbs
;
1650 if (last
> first
&& (db
= dbuf_hold_level(dn
, 1, last
, FTAG
))) {
1651 dbuf_will_dirty(db
, tx
);
1652 dbuf_rele(db
, FTAG
);
1654 for (i
= first
+ 1; i
< last
; i
++) {
1655 uint64_t ibyte
= i
<< shift
;
1658 err
= dnode_next_offset(dn
,
1659 DNODE_FIND_HAVELOCK
, &ibyte
, 1, 1, 0);
1661 if (err
== ESRCH
|| i
>= last
)
1664 db
= dbuf_hold_level(dn
, 1, i
, FTAG
);
1666 dbuf_will_dirty(db
, tx
);
1667 dbuf_rele(db
, FTAG
);
1673 * Add this range to the dnode range list.
1674 * We will finish up this free operation in the syncing phase.
1676 mutex_enter(&dn
->dn_mtx
);
1677 dnode_clear_range(dn
, blkid
, nblks
, tx
);
1679 free_range_t
*rp
, *found
;
1681 avl_tree_t
*tree
= &dn
->dn_ranges
[tx
->tx_txg
&TXG_MASK
];
1683 /* Add new range to dn_ranges */
1684 rp
= kmem_alloc(sizeof (free_range_t
), KM_PUSHPAGE
);
1685 rp
->fr_blkid
= blkid
;
1686 rp
->fr_nblks
= nblks
;
1687 found
= avl_find(tree
, rp
, &where
);
1688 ASSERT(found
== NULL
);
1689 avl_insert(tree
, rp
, where
);
1690 dprintf_dnode(dn
, "blkid=%llu nblks=%llu txg=%llu\n",
1691 blkid
, nblks
, tx
->tx_txg
);
1693 mutex_exit(&dn
->dn_mtx
);
1695 dbuf_free_range(dn
, blkid
, blkid
+ nblks
- 1, tx
);
1696 dnode_setdirty(dn
, tx
);
1698 if (trunc
&& dn
->dn_maxblkid
>= (off
>> blkshift
))
1699 dn
->dn_maxblkid
= (off
>> blkshift
? (off
>> blkshift
) - 1 : 0);
1701 rw_exit(&dn
->dn_struct_rwlock
);
1705 dnode_spill_freed(dnode_t
*dn
)
1709 mutex_enter(&dn
->dn_mtx
);
1710 for (i
= 0; i
< TXG_SIZE
; i
++) {
1711 if (dn
->dn_rm_spillblk
[i
] == DN_KILL_SPILLBLK
)
1714 mutex_exit(&dn
->dn_mtx
);
1715 return (i
< TXG_SIZE
);
1718 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
1720 dnode_block_freed(dnode_t
*dn
, uint64_t blkid
)
1722 free_range_t range_tofind
;
1723 void *dp
= spa_get_dsl(dn
->dn_objset
->os_spa
);
1726 if (blkid
== DMU_BONUS_BLKID
)
1730 * If we're in the process of opening the pool, dp will not be
1731 * set yet, but there shouldn't be anything dirty.
1736 if (dn
->dn_free_txg
)
1739 if (blkid
== DMU_SPILL_BLKID
)
1740 return (dnode_spill_freed(dn
));
1742 range_tofind
.fr_blkid
= blkid
;
1743 mutex_enter(&dn
->dn_mtx
);
1744 for (i
= 0; i
< TXG_SIZE
; i
++) {
1745 free_range_t
*range_found
;
1748 range_found
= avl_find(&dn
->dn_ranges
[i
], &range_tofind
, &idx
);
1750 ASSERT(range_found
->fr_nblks
> 0);
1753 range_found
= avl_nearest(&dn
->dn_ranges
[i
], idx
, AVL_BEFORE
);
1755 range_found
->fr_blkid
+ range_found
->fr_nblks
> blkid
)
1758 mutex_exit(&dn
->dn_mtx
);
1759 return (i
< TXG_SIZE
);
1762 /* call from syncing context when we actually write/free space for this dnode */
1764 dnode_diduse_space(dnode_t
*dn
, int64_t delta
)
1767 dprintf_dnode(dn
, "dn=%p dnp=%p used=%llu delta=%lld\n",
1769 (u_longlong_t
)dn
->dn_phys
->dn_used
,
1772 mutex_enter(&dn
->dn_mtx
);
1773 space
= DN_USED_BYTES(dn
->dn_phys
);
1775 ASSERT3U(space
+ delta
, >=, space
); /* no overflow */
1777 ASSERT3U(space
, >=, -delta
); /* no underflow */
1780 if (spa_version(dn
->dn_objset
->os_spa
) < SPA_VERSION_DNODE_BYTES
) {
1781 ASSERT((dn
->dn_phys
->dn_flags
& DNODE_FLAG_USED_BYTES
) == 0);
1782 ASSERT0(P2PHASE(space
, 1<<DEV_BSHIFT
));
1783 dn
->dn_phys
->dn_used
= space
>> DEV_BSHIFT
;
1785 dn
->dn_phys
->dn_used
= space
;
1786 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_USED_BYTES
;
1788 mutex_exit(&dn
->dn_mtx
);
1792 * Call when we think we're going to write/free space in open context to track
1793 * the amount of memory in use by the currently open txg.
1796 dnode_willuse_space(dnode_t
*dn
, int64_t space
, dmu_tx_t
*tx
)
1798 objset_t
*os
= dn
->dn_objset
;
1799 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1800 int64_t aspace
= spa_get_asize(os
->os_spa
, space
);
1803 dsl_dir_willuse_space(ds
->ds_dir
, aspace
, tx
);
1804 dsl_pool_dirty_space(dmu_tx_pool(tx
), space
, tx
);
1807 dmu_tx_willuse_space(tx
, aspace
);
1811 * Scans a block at the indicated "level" looking for a hole or data,
1812 * depending on 'flags'.
1814 * If level > 0, then we are scanning an indirect block looking at its
1815 * pointers. If level == 0, then we are looking at a block of dnodes.
1817 * If we don't find what we are looking for in the block, we return ESRCH.
1818 * Otherwise, return with *offset pointing to the beginning (if searching
1819 * forwards) or end (if searching backwards) of the range covered by the
1820 * block pointer we matched on (or dnode).
1822 * The basic search algorithm used below by dnode_next_offset() is to
1823 * use this function to search up the block tree (widen the search) until
1824 * we find something (i.e., we don't return ESRCH) and then search back
1825 * down the tree (narrow the search) until we reach our original search
1829 dnode_next_offset_level(dnode_t
*dn
, int flags
, uint64_t *offset
,
1830 int lvl
, uint64_t blkfill
, uint64_t txg
)
1832 dmu_buf_impl_t
*db
= NULL
;
1834 uint64_t epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1835 uint64_t epb
= 1ULL << epbs
;
1836 uint64_t minfill
, maxfill
;
1838 int i
, inc
, error
, span
;
1840 dprintf("probing object %llu offset %llx level %d of %u\n",
1841 dn
->dn_object
, *offset
, lvl
, dn
->dn_phys
->dn_nlevels
);
1843 hole
= ((flags
& DNODE_FIND_HOLE
) != 0);
1844 inc
= (flags
& DNODE_FIND_BACKWARDS
) ? -1 : 1;
1845 ASSERT(txg
== 0 || !hole
);
1847 if (lvl
== dn
->dn_phys
->dn_nlevels
) {
1849 epb
= dn
->dn_phys
->dn_nblkptr
;
1850 data
= dn
->dn_phys
->dn_blkptr
;
1852 uint64_t blkid
= dbuf_whichblock(dn
, *offset
) >> (epbs
* lvl
);
1853 error
= dbuf_hold_impl(dn
, lvl
, blkid
, TRUE
, FTAG
, &db
);
1855 if (error
!= ENOENT
)
1860 * This can only happen when we are searching up
1861 * the block tree for data. We don't really need to
1862 * adjust the offset, as we will just end up looking
1863 * at the pointer to this block in its parent, and its
1864 * going to be unallocated, so we will skip over it.
1866 return (SET_ERROR(ESRCH
));
1868 error
= dbuf_read(db
, NULL
, DB_RF_CANFAIL
| DB_RF_HAVESTRUCT
);
1870 dbuf_rele(db
, FTAG
);
1873 data
= db
->db
.db_data
;
1877 (db
->db_blkptr
== NULL
|| db
->db_blkptr
->blk_birth
<= txg
)) {
1879 * This can only happen when we are searching up the tree
1880 * and these conditions mean that we need to keep climbing.
1882 error
= SET_ERROR(ESRCH
);
1883 } else if (lvl
== 0) {
1884 dnode_phys_t
*dnp
= data
;
1886 ASSERT(dn
->dn_type
== DMU_OT_DNODE
);
1888 for (i
= (*offset
>> span
) & (blkfill
- 1);
1889 i
>= 0 && i
< blkfill
; i
+= inc
) {
1890 if ((dnp
[i
].dn_type
== DMU_OT_NONE
) == hole
)
1892 *offset
+= (1ULL << span
) * inc
;
1894 if (i
< 0 || i
== blkfill
)
1895 error
= SET_ERROR(ESRCH
);
1897 blkptr_t
*bp
= data
;
1898 uint64_t start
= *offset
;
1899 span
= (lvl
- 1) * epbs
+ dn
->dn_datablkshift
;
1901 maxfill
= blkfill
<< ((lvl
- 1) * epbs
);
1908 *offset
= *offset
>> span
;
1909 for (i
= BF64_GET(*offset
, 0, epbs
);
1910 i
>= 0 && i
< epb
; i
+= inc
) {
1911 if (bp
[i
].blk_fill
>= minfill
&&
1912 bp
[i
].blk_fill
<= maxfill
&&
1913 (hole
|| bp
[i
].blk_birth
> txg
))
1915 if (inc
> 0 || *offset
> 0)
1918 *offset
= *offset
<< span
;
1920 /* traversing backwards; position offset at the end */
1921 ASSERT3U(*offset
, <=, start
);
1922 *offset
= MIN(*offset
+ (1ULL << span
) - 1, start
);
1923 } else if (*offset
< start
) {
1926 if (i
< 0 || i
>= epb
)
1927 error
= SET_ERROR(ESRCH
);
1931 dbuf_rele(db
, FTAG
);
1937 * Find the next hole, data, or sparse region at or after *offset.
1938 * The value 'blkfill' tells us how many items we expect to find
1939 * in an L0 data block; this value is 1 for normal objects,
1940 * DNODES_PER_BLOCK for the meta dnode, and some fraction of
1941 * DNODES_PER_BLOCK when searching for sparse regions thereof.
1945 * dnode_next_offset(dn, flags, offset, 1, 1, 0);
1946 * Finds the next/previous hole/data in a file.
1947 * Used in dmu_offset_next().
1949 * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
1950 * Finds the next free/allocated dnode an objset's meta-dnode.
1951 * Only finds objects that have new contents since txg (ie.
1952 * bonus buffer changes and content removal are ignored).
1953 * Used in dmu_object_next().
1955 * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
1956 * Finds the next L2 meta-dnode bp that's at most 1/4 full.
1957 * Used in dmu_object_alloc().
1960 dnode_next_offset(dnode_t
*dn
, int flags
, uint64_t *offset
,
1961 int minlvl
, uint64_t blkfill
, uint64_t txg
)
1963 uint64_t initial_offset
= *offset
;
1967 if (!(flags
& DNODE_FIND_HAVELOCK
))
1968 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1970 if (dn
->dn_phys
->dn_nlevels
== 0) {
1971 error
= SET_ERROR(ESRCH
);
1975 if (dn
->dn_datablkshift
== 0) {
1976 if (*offset
< dn
->dn_datablksz
) {
1977 if (flags
& DNODE_FIND_HOLE
)
1978 *offset
= dn
->dn_datablksz
;
1980 error
= SET_ERROR(ESRCH
);
1985 maxlvl
= dn
->dn_phys
->dn_nlevels
;
1987 for (lvl
= minlvl
; lvl
<= maxlvl
; lvl
++) {
1988 error
= dnode_next_offset_level(dn
,
1989 flags
, offset
, lvl
, blkfill
, txg
);
1994 while (error
== 0 && --lvl
>= minlvl
) {
1995 error
= dnode_next_offset_level(dn
,
1996 flags
, offset
, lvl
, blkfill
, txg
);
1999 if (error
== 0 && (flags
& DNODE_FIND_BACKWARDS
?
2000 initial_offset
< *offset
: initial_offset
> *offset
))
2001 error
= SET_ERROR(ESRCH
);
2003 if (!(flags
& DNODE_FIND_HAVELOCK
))
2004 rw_exit(&dn
->dn_struct_rwlock
);