4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
24 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 #include <sys/zfs_context.h>
29 #include <sys/dnode.h>
31 #include <sys/dmu_impl.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/dmu_objset.h>
34 #include <sys/dsl_dir.h>
35 #include <sys/dsl_dataset.h>
38 #include <sys/dmu_zfetch.h>
39 #include <sys/range_tree.h>
40 #include <sys/trace_dnode.h>
41 #include <sys/zfs_project.h>
43 dnode_stats_t dnode_stats = {
44 { "dnode_hold_dbuf_hold", KSTAT_DATA_UINT64 },
45 { "dnode_hold_dbuf_read", KSTAT_DATA_UINT64 },
46 { "dnode_hold_alloc_hits", KSTAT_DATA_UINT64 },
47 { "dnode_hold_alloc_misses", KSTAT_DATA_UINT64 },
48 { "dnode_hold_alloc_interior", KSTAT_DATA_UINT64 },
49 { "dnode_hold_alloc_lock_retry", KSTAT_DATA_UINT64 },
50 { "dnode_hold_alloc_lock_misses", KSTAT_DATA_UINT64 },
51 { "dnode_hold_alloc_type_none", KSTAT_DATA_UINT64 },
52 { "dnode_hold_free_hits", KSTAT_DATA_UINT64 },
53 { "dnode_hold_free_misses", KSTAT_DATA_UINT64 },
54 { "dnode_hold_free_lock_misses", KSTAT_DATA_UINT64 },
55 { "dnode_hold_free_lock_retry", KSTAT_DATA_UINT64 },
56 { "dnode_hold_free_overflow", KSTAT_DATA_UINT64 },
57 { "dnode_hold_free_refcount", KSTAT_DATA_UINT64 },
58 { "dnode_hold_free_txg", KSTAT_DATA_UINT64 },
59 { "dnode_free_interior_lock_retry", KSTAT_DATA_UINT64 },
60 { "dnode_allocate", KSTAT_DATA_UINT64 },
61 { "dnode_reallocate", KSTAT_DATA_UINT64 },
62 { "dnode_buf_evict", KSTAT_DATA_UINT64 },
63 { "dnode_alloc_next_chunk", KSTAT_DATA_UINT64 },
64 { "dnode_alloc_race", KSTAT_DATA_UINT64 },
65 { "dnode_alloc_next_block", KSTAT_DATA_UINT64 },
66 { "dnode_move_invalid", KSTAT_DATA_UINT64 },
67 { "dnode_move_recheck1", KSTAT_DATA_UINT64 },
68 { "dnode_move_recheck2", KSTAT_DATA_UINT64 },
69 { "dnode_move_special", KSTAT_DATA_UINT64 },
70 { "dnode_move_handle", KSTAT_DATA_UINT64 },
71 { "dnode_move_rwlock", KSTAT_DATA_UINT64 },
72 { "dnode_move_active", KSTAT_DATA_UINT64 },
75 static kstat_t *dnode_ksp;
76 static kmem_cache_t *dnode_cache;
78 ASSERTV(static dnode_phys_t dnode_phys_zero);
80 int zfs_default_bs = SPA_MINBLOCKSHIFT;
81 int zfs_default_ibs = DN_MAX_INDBLKSHIFT;
84 static kmem_cbrc_t dnode_move(void *, void *, size_t, void *);
88 dbuf_compare(const void *x1, const void *x2)
90 const dmu_buf_impl_t *d1 = x1;
91 const dmu_buf_impl_t *d2 = x2;
93 int cmp = AVL_CMP(d1->db_level, d2->db_level);
97 cmp = AVL_CMP(d1->db_blkid, d2->db_blkid);
101 if (d1->db_state == DB_SEARCH) {
102 ASSERT3S(d2->db_state, !=, DB_SEARCH);
104 } else if (d2->db_state == DB_SEARCH) {
105 ASSERT3S(d1->db_state, !=, DB_SEARCH);
109 return (AVL_PCMP(d1, d2));
114 dnode_cons(void *arg, void *unused, int kmflag)
119 rw_init(&dn->dn_struct_rwlock, NULL, RW_NOLOCKDEP, NULL);
120 mutex_init(&dn->dn_mtx, NULL, MUTEX_DEFAULT, NULL);
121 mutex_init(&dn->dn_dbufs_mtx, NULL, MUTEX_DEFAULT, NULL);
122 cv_init(&dn->dn_notxholds, NULL, CV_DEFAULT, NULL);
125 * Every dbuf has a reference, and dropping a tracked reference is
126 * O(number of references), so don't track dn_holds.
128 refcount_create_untracked(&dn->dn_holds);
129 refcount_create(&dn->dn_tx_holds);
130 list_link_init(&dn->dn_link);
132 bzero(&dn->dn_next_nblkptr[0], sizeof (dn->dn_next_nblkptr));
133 bzero(&dn->dn_next_nlevels[0], sizeof (dn->dn_next_nlevels));
134 bzero(&dn->dn_next_indblkshift[0], sizeof (dn->dn_next_indblkshift));
135 bzero(&dn->dn_next_bonustype[0], sizeof (dn->dn_next_bonustype));
136 bzero(&dn->dn_rm_spillblk[0], sizeof (dn->dn_rm_spillblk));
137 bzero(&dn->dn_next_bonuslen[0], sizeof (dn->dn_next_bonuslen));
138 bzero(&dn->dn_next_blksz[0], sizeof (dn->dn_next_blksz));
139 bzero(&dn->dn_next_maxblkid[0], sizeof (dn->dn_next_maxblkid));
141 for (i = 0; i < TXG_SIZE; i++) {
142 list_link_init(&dn->dn_dirty_link[i]);
143 dn->dn_free_ranges[i] = NULL;
144 list_create(&dn->dn_dirty_records[i],
145 sizeof (dbuf_dirty_record_t),
146 offsetof(dbuf_dirty_record_t, dr_dirty_node));
149 dn->dn_allocated_txg = 0;
151 dn->dn_assigned_txg = 0;
153 dn->dn_dirtyctx_firstset = NULL;
155 dn->dn_have_spill = B_FALSE;
161 dn->dn_oldprojid = ZFS_DEFAULT_PROJID;
164 dn->dn_newprojid = ZFS_DEFAULT_PROJID;
167 dn->dn_dbufs_count = 0;
168 avl_create(&dn->dn_dbufs, dbuf_compare, sizeof (dmu_buf_impl_t),
169 offsetof(dmu_buf_impl_t, db_link));
177 dnode_dest(void *arg, void *unused)
182 rw_destroy(&dn->dn_struct_rwlock);
183 mutex_destroy(&dn->dn_mtx);
184 mutex_destroy(&dn->dn_dbufs_mtx);
185 cv_destroy(&dn->dn_notxholds);
186 refcount_destroy(&dn->dn_holds);
187 refcount_destroy(&dn->dn_tx_holds);
188 ASSERT(!list_link_active(&dn->dn_link));
190 for (i = 0; i < TXG_SIZE; i++) {
191 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
192 ASSERT3P(dn->dn_free_ranges[i], ==, NULL);
193 list_destroy(&dn->dn_dirty_records[i]);
194 ASSERT0(dn->dn_next_nblkptr[i]);
195 ASSERT0(dn->dn_next_nlevels[i]);
196 ASSERT0(dn->dn_next_indblkshift[i]);
197 ASSERT0(dn->dn_next_bonustype[i]);
198 ASSERT0(dn->dn_rm_spillblk[i]);
199 ASSERT0(dn->dn_next_bonuslen[i]);
200 ASSERT0(dn->dn_next_blksz[i]);
201 ASSERT0(dn->dn_next_maxblkid[i]);
204 ASSERT0(dn->dn_allocated_txg);
205 ASSERT0(dn->dn_free_txg);
206 ASSERT0(dn->dn_assigned_txg);
207 ASSERT0(dn->dn_dirtyctx);
208 ASSERT3P(dn->dn_dirtyctx_firstset, ==, NULL);
209 ASSERT3P(dn->dn_bonus, ==, NULL);
210 ASSERT(!dn->dn_have_spill);
211 ASSERT3P(dn->dn_zio, ==, NULL);
212 ASSERT0(dn->dn_oldused);
213 ASSERT0(dn->dn_oldflags);
214 ASSERT0(dn->dn_olduid);
215 ASSERT0(dn->dn_oldgid);
216 ASSERT0(dn->dn_oldprojid);
217 ASSERT0(dn->dn_newuid);
218 ASSERT0(dn->dn_newgid);
219 ASSERT0(dn->dn_newprojid);
220 ASSERT0(dn->dn_id_flags);
222 ASSERT0(dn->dn_dbufs_count);
223 avl_destroy(&dn->dn_dbufs);
229 ASSERT(dnode_cache == NULL);
230 dnode_cache = kmem_cache_create("dnode_t", sizeof (dnode_t),
231 0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0);
232 kmem_cache_set_move(dnode_cache, dnode_move);
234 dnode_ksp = kstat_create("zfs", 0, "dnodestats", "misc",
235 KSTAT_TYPE_NAMED, sizeof (dnode_stats) / sizeof (kstat_named_t),
237 if (dnode_ksp != NULL) {
238 dnode_ksp->ks_data = &dnode_stats;
239 kstat_install(dnode_ksp);
246 if (dnode_ksp != NULL) {
247 kstat_delete(dnode_ksp);
251 kmem_cache_destroy(dnode_cache);
258 dnode_verify(dnode_t *dn)
260 int drop_struct_lock = FALSE;
263 ASSERT(dn->dn_objset);
264 ASSERT(dn->dn_handle->dnh_dnode == dn);
266 ASSERT(DMU_OT_IS_VALID(dn->dn_phys->dn_type));
268 if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY))
271 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
272 rw_enter(&dn->dn_struct_rwlock, RW_READER);
273 drop_struct_lock = TRUE;
275 if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) {
277 int max_bonuslen = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots);
278 ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT);
279 if (dn->dn_datablkshift) {
280 ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT);
281 ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT);
282 ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz);
284 ASSERT3U(dn->dn_nlevels, <=, 30);
285 ASSERT(DMU_OT_IS_VALID(dn->dn_type));
286 ASSERT3U(dn->dn_nblkptr, >=, 1);
287 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
288 ASSERT3U(dn->dn_bonuslen, <=, max_bonuslen);
289 ASSERT3U(dn->dn_datablksz, ==,
290 dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
291 ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0);
292 ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) +
293 dn->dn_bonuslen, <=, max_bonuslen);
294 for (i = 0; i < TXG_SIZE; i++) {
295 ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels);
298 if (dn->dn_phys->dn_type != DMU_OT_NONE)
299 ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels);
300 ASSERT(DMU_OBJECT_IS_SPECIAL(dn->dn_object) || dn->dn_dbuf != NULL);
301 if (dn->dn_dbuf != NULL) {
302 ASSERT3P(dn->dn_phys, ==,
303 (dnode_phys_t *)dn->dn_dbuf->db.db_data +
304 (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT)));
306 if (drop_struct_lock)
307 rw_exit(&dn->dn_struct_rwlock);
312 dnode_byteswap(dnode_phys_t *dnp)
314 uint64_t *buf64 = (void*)&dnp->dn_blkptr;
317 if (dnp->dn_type == DMU_OT_NONE) {
318 bzero(dnp, sizeof (dnode_phys_t));
322 dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec);
323 dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen);
324 dnp->dn_extra_slots = BSWAP_8(dnp->dn_extra_slots);
325 dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid);
326 dnp->dn_used = BSWAP_64(dnp->dn_used);
329 * dn_nblkptr is only one byte, so it's OK to read it in either
330 * byte order. We can't read dn_bouslen.
332 ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT);
333 ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR);
334 for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++)
335 buf64[i] = BSWAP_64(buf64[i]);
338 * OK to check dn_bonuslen for zero, because it won't matter if
339 * we have the wrong byte order. This is necessary because the
340 * dnode dnode is smaller than a regular dnode.
342 if (dnp->dn_bonuslen != 0) {
344 * Note that the bonus length calculated here may be
345 * longer than the actual bonus buffer. This is because
346 * we always put the bonus buffer after the last block
347 * pointer (instead of packing it against the end of the
350 int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t);
351 int slots = dnp->dn_extra_slots + 1;
352 size_t len = DN_SLOTS_TO_BONUSLEN(slots) - off;
353 dmu_object_byteswap_t byteswap;
354 ASSERT(DMU_OT_IS_VALID(dnp->dn_bonustype));
355 byteswap = DMU_OT_BYTESWAP(dnp->dn_bonustype);
356 dmu_ot_byteswap[byteswap].ob_func(dnp->dn_bonus + off, len);
359 /* Swap SPILL block if we have one */
360 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)
361 byteswap_uint64_array(DN_SPILL_BLKPTR(dnp), sizeof (blkptr_t));
365 dnode_buf_byteswap(void *vbuf, size_t size)
369 ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT));
370 ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0);
373 dnode_phys_t *dnp = (void *)(((char *)vbuf) + i);
377 if (dnp->dn_type != DMU_OT_NONE)
378 i += dnp->dn_extra_slots * DNODE_MIN_SIZE;
383 dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx)
385 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
387 dnode_setdirty(dn, tx);
388 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
389 ASSERT3U(newsize, <=, DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots) -
390 (dn->dn_nblkptr-1) * sizeof (blkptr_t));
391 dn->dn_bonuslen = newsize;
393 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = DN_ZERO_BONUSLEN;
395 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
396 rw_exit(&dn->dn_struct_rwlock);
400 dnode_setbonus_type(dnode_t *dn, dmu_object_type_t newtype, dmu_tx_t *tx)
402 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
403 dnode_setdirty(dn, tx);
404 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
405 dn->dn_bonustype = newtype;
406 dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
407 rw_exit(&dn->dn_struct_rwlock);
411 dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx)
413 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
414 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
415 dnode_setdirty(dn, tx);
416 dn->dn_rm_spillblk[tx->tx_txg&TXG_MASK] = DN_KILL_SPILLBLK;
417 dn->dn_have_spill = B_FALSE;
421 dnode_setdblksz(dnode_t *dn, int size)
423 ASSERT0(P2PHASE(size, SPA_MINBLOCKSIZE));
424 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
425 ASSERT3U(size, >=, SPA_MINBLOCKSIZE);
426 ASSERT3U(size >> SPA_MINBLOCKSHIFT, <,
427 1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8));
428 dn->dn_datablksz = size;
429 dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT;
430 dn->dn_datablkshift = ISP2(size) ? highbit64(size - 1) : 0;
434 dnode_create(objset_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db,
435 uint64_t object, dnode_handle_t *dnh)
439 dn = kmem_cache_alloc(dnode_cache, KM_SLEEP);
440 ASSERT(!POINTER_IS_VALID(dn->dn_objset));
444 * Defer setting dn_objset until the dnode is ready to be a candidate
445 * for the dnode_move() callback.
447 dn->dn_object = object;
452 if (dnp->dn_datablkszsec) {
453 dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
455 dn->dn_datablksz = 0;
456 dn->dn_datablkszsec = 0;
457 dn->dn_datablkshift = 0;
459 dn->dn_indblkshift = dnp->dn_indblkshift;
460 dn->dn_nlevels = dnp->dn_nlevels;
461 dn->dn_type = dnp->dn_type;
462 dn->dn_nblkptr = dnp->dn_nblkptr;
463 dn->dn_checksum = dnp->dn_checksum;
464 dn->dn_compress = dnp->dn_compress;
465 dn->dn_bonustype = dnp->dn_bonustype;
466 dn->dn_bonuslen = dnp->dn_bonuslen;
467 dn->dn_num_slots = dnp->dn_extra_slots + 1;
468 dn->dn_maxblkid = dnp->dn_maxblkid;
469 dn->dn_have_spill = ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0);
472 dmu_zfetch_init(&dn->dn_zfetch, dn);
474 ASSERT(DMU_OT_IS_VALID(dn->dn_phys->dn_type));
475 ASSERT(zrl_is_locked(&dnh->dnh_zrlock));
476 ASSERT(!DN_SLOT_IS_PTR(dnh->dnh_dnode));
478 mutex_enter(&os->os_lock);
481 * Exclude special dnodes from os_dnodes so an empty os_dnodes
482 * signifies that the special dnodes have no references from
483 * their children (the entries in os_dnodes). This allows
484 * dnode_destroy() to easily determine if the last child has
485 * been removed and then complete eviction of the objset.
487 if (!DMU_OBJECT_IS_SPECIAL(object))
488 list_insert_head(&os->os_dnodes, dn);
492 * Everything else must be valid before assigning dn_objset
493 * makes the dnode eligible for dnode_move().
498 mutex_exit(&os->os_lock);
500 arc_space_consume(sizeof (dnode_t), ARC_SPACE_DNODE);
506 * Caller must be holding the dnode handle, which is released upon return.
509 dnode_destroy(dnode_t *dn)
511 objset_t *os = dn->dn_objset;
512 boolean_t complete_os_eviction = B_FALSE;
514 ASSERT((dn->dn_id_flags & DN_ID_NEW_EXIST) == 0);
516 mutex_enter(&os->os_lock);
517 POINTER_INVALIDATE(&dn->dn_objset);
518 if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
519 list_remove(&os->os_dnodes, dn);
520 complete_os_eviction =
521 list_is_empty(&os->os_dnodes) &&
522 list_link_active(&os->os_evicting_node);
524 mutex_exit(&os->os_lock);
526 /* the dnode can no longer move, so we can release the handle */
527 if (!zrl_is_locked(&dn->dn_handle->dnh_zrlock))
528 zrl_remove(&dn->dn_handle->dnh_zrlock);
530 dn->dn_allocated_txg = 0;
532 dn->dn_assigned_txg = 0;
535 if (dn->dn_dirtyctx_firstset != NULL) {
536 kmem_free(dn->dn_dirtyctx_firstset, 1);
537 dn->dn_dirtyctx_firstset = NULL;
539 if (dn->dn_bonus != NULL) {
540 mutex_enter(&dn->dn_bonus->db_mtx);
541 dbuf_destroy(dn->dn_bonus);
546 dn->dn_have_spill = B_FALSE;
551 dn->dn_oldprojid = ZFS_DEFAULT_PROJID;
554 dn->dn_newprojid = ZFS_DEFAULT_PROJID;
557 dmu_zfetch_fini(&dn->dn_zfetch);
558 kmem_cache_free(dnode_cache, dn);
559 arc_space_return(sizeof (dnode_t), ARC_SPACE_DNODE);
561 if (complete_os_eviction)
562 dmu_objset_evict_done(os);
566 dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
567 dmu_object_type_t bonustype, int bonuslen, int dn_slots, dmu_tx_t *tx)
571 ASSERT3U(dn_slots, >, 0);
572 ASSERT3U(dn_slots << DNODE_SHIFT, <=,
573 spa_maxdnodesize(dmu_objset_spa(dn->dn_objset)));
574 ASSERT3U(blocksize, <=,
575 spa_maxblocksize(dmu_objset_spa(dn->dn_objset)));
577 blocksize = 1 << zfs_default_bs;
579 blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE);
582 ibs = zfs_default_ibs;
584 ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT);
586 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d dn_slots=%d\n",
587 dn->dn_objset, dn->dn_object, tx->tx_txg, blocksize, ibs, dn_slots);
588 DNODE_STAT_BUMP(dnode_allocate);
590 ASSERT(dn->dn_type == DMU_OT_NONE);
591 ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0);
592 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE);
593 ASSERT(ot != DMU_OT_NONE);
594 ASSERT(DMU_OT_IS_VALID(ot));
595 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
596 (bonustype == DMU_OT_SA && bonuslen == 0) ||
597 (bonustype != DMU_OT_NONE && bonuslen != 0));
598 ASSERT(DMU_OT_IS_VALID(bonustype));
599 ASSERT3U(bonuslen, <=, DN_SLOTS_TO_BONUSLEN(dn_slots));
600 ASSERT(dn->dn_type == DMU_OT_NONE);
601 ASSERT0(dn->dn_maxblkid);
602 ASSERT0(dn->dn_allocated_txg);
603 ASSERT0(dn->dn_assigned_txg);
604 ASSERT(refcount_is_zero(&dn->dn_tx_holds));
605 ASSERT3U(refcount_count(&dn->dn_holds), <=, 1);
606 ASSERT(avl_is_empty(&dn->dn_dbufs));
608 for (i = 0; i < TXG_SIZE; i++) {
609 ASSERT0(dn->dn_next_nblkptr[i]);
610 ASSERT0(dn->dn_next_nlevels[i]);
611 ASSERT0(dn->dn_next_indblkshift[i]);
612 ASSERT0(dn->dn_next_bonuslen[i]);
613 ASSERT0(dn->dn_next_bonustype[i]);
614 ASSERT0(dn->dn_rm_spillblk[i]);
615 ASSERT0(dn->dn_next_blksz[i]);
616 ASSERT0(dn->dn_next_maxblkid[i]);
617 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
618 ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL);
619 ASSERT3P(dn->dn_free_ranges[i], ==, NULL);
623 dnode_setdblksz(dn, blocksize);
624 dn->dn_indblkshift = ibs;
626 dn->dn_num_slots = dn_slots;
627 if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
630 dn->dn_nblkptr = MIN(DN_MAX_NBLKPTR,
631 1 + ((DN_SLOTS_TO_BONUSLEN(dn_slots) - bonuslen) >>
635 dn->dn_bonustype = bonustype;
636 dn->dn_bonuslen = bonuslen;
637 dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
638 dn->dn_compress = ZIO_COMPRESS_INHERIT;
642 if (dn->dn_dirtyctx_firstset) {
643 kmem_free(dn->dn_dirtyctx_firstset, 1);
644 dn->dn_dirtyctx_firstset = NULL;
647 dn->dn_allocated_txg = tx->tx_txg;
650 dnode_setdirty(dn, tx);
651 dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs;
652 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
653 dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
654 dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz;
658 dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
659 dmu_object_type_t bonustype, int bonuslen, int dn_slots, dmu_tx_t *tx)
663 ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE);
664 ASSERT3U(blocksize, <=,
665 spa_maxblocksize(dmu_objset_spa(dn->dn_objset)));
666 ASSERT0(blocksize % SPA_MINBLOCKSIZE);
667 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
668 ASSERT(tx->tx_txg != 0);
669 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
670 (bonustype != DMU_OT_NONE && bonuslen != 0) ||
671 (bonustype == DMU_OT_SA && bonuslen == 0));
672 ASSERT(DMU_OT_IS_VALID(bonustype));
673 ASSERT3U(bonuslen, <=,
674 DN_BONUS_SIZE(spa_maxdnodesize(dmu_objset_spa(dn->dn_objset))));
676 dn_slots = dn_slots > 0 ? dn_slots : DNODE_MIN_SLOTS;
678 dnode_free_interior_slots(dn);
679 DNODE_STAT_BUMP(dnode_reallocate);
681 /* clean up any unreferenced dbufs */
682 dnode_evict_dbufs(dn);
686 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
687 dnode_setdirty(dn, tx);
688 if (dn->dn_datablksz != blocksize) {
689 /* change blocksize */
690 ASSERT(dn->dn_maxblkid == 0 &&
691 (BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) ||
692 dnode_block_freed(dn, 0)));
693 dnode_setdblksz(dn, blocksize);
694 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize;
696 if (dn->dn_bonuslen != bonuslen)
697 dn->dn_next_bonuslen[tx->tx_txg&TXG_MASK] = bonuslen;
699 if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
702 nblkptr = MIN(DN_MAX_NBLKPTR,
703 1 + ((DN_SLOTS_TO_BONUSLEN(dn_slots) - bonuslen) >>
705 if (dn->dn_bonustype != bonustype)
706 dn->dn_next_bonustype[tx->tx_txg&TXG_MASK] = bonustype;
707 if (dn->dn_nblkptr != nblkptr)
708 dn->dn_next_nblkptr[tx->tx_txg&TXG_MASK] = nblkptr;
709 if (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
710 dbuf_rm_spill(dn, tx);
711 dnode_rm_spill(dn, tx);
713 rw_exit(&dn->dn_struct_rwlock);
718 /* change bonus size and type */
719 mutex_enter(&dn->dn_mtx);
720 dn->dn_bonustype = bonustype;
721 dn->dn_bonuslen = bonuslen;
722 dn->dn_num_slots = dn_slots;
723 dn->dn_nblkptr = nblkptr;
724 dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
725 dn->dn_compress = ZIO_COMPRESS_INHERIT;
726 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
728 /* fix up the bonus db_size */
730 dn->dn_bonus->db.db_size =
731 DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots) -
732 (dn->dn_nblkptr-1) * sizeof (blkptr_t);
733 ASSERT(dn->dn_bonuslen <= dn->dn_bonus->db.db_size);
736 dn->dn_allocated_txg = tx->tx_txg;
737 mutex_exit(&dn->dn_mtx);
742 dnode_move_impl(dnode_t *odn, dnode_t *ndn)
746 ASSERT(!RW_LOCK_HELD(&odn->dn_struct_rwlock));
747 ASSERT(MUTEX_NOT_HELD(&odn->dn_mtx));
748 ASSERT(MUTEX_NOT_HELD(&odn->dn_dbufs_mtx));
749 ASSERT(!RW_LOCK_HELD(&odn->dn_zfetch.zf_rwlock));
752 ndn->dn_objset = odn->dn_objset;
753 ndn->dn_object = odn->dn_object;
754 ndn->dn_dbuf = odn->dn_dbuf;
755 ndn->dn_handle = odn->dn_handle;
756 ndn->dn_phys = odn->dn_phys;
757 ndn->dn_type = odn->dn_type;
758 ndn->dn_bonuslen = odn->dn_bonuslen;
759 ndn->dn_bonustype = odn->dn_bonustype;
760 ndn->dn_nblkptr = odn->dn_nblkptr;
761 ndn->dn_checksum = odn->dn_checksum;
762 ndn->dn_compress = odn->dn_compress;
763 ndn->dn_nlevels = odn->dn_nlevels;
764 ndn->dn_indblkshift = odn->dn_indblkshift;
765 ndn->dn_datablkshift = odn->dn_datablkshift;
766 ndn->dn_datablkszsec = odn->dn_datablkszsec;
767 ndn->dn_datablksz = odn->dn_datablksz;
768 ndn->dn_maxblkid = odn->dn_maxblkid;
769 ndn->dn_num_slots = odn->dn_num_slots;
770 bcopy(&odn->dn_next_nblkptr[0], &ndn->dn_next_nblkptr[0],
771 sizeof (odn->dn_next_nblkptr));
772 bcopy(&odn->dn_next_nlevels[0], &ndn->dn_next_nlevels[0],
773 sizeof (odn->dn_next_nlevels));
774 bcopy(&odn->dn_next_indblkshift[0], &ndn->dn_next_indblkshift[0],
775 sizeof (odn->dn_next_indblkshift));
776 bcopy(&odn->dn_next_bonustype[0], &ndn->dn_next_bonustype[0],
777 sizeof (odn->dn_next_bonustype));
778 bcopy(&odn->dn_rm_spillblk[0], &ndn->dn_rm_spillblk[0],
779 sizeof (odn->dn_rm_spillblk));
780 bcopy(&odn->dn_next_bonuslen[0], &ndn->dn_next_bonuslen[0],
781 sizeof (odn->dn_next_bonuslen));
782 bcopy(&odn->dn_next_blksz[0], &ndn->dn_next_blksz[0],
783 sizeof (odn->dn_next_blksz));
784 bcopy(&odn->dn_next_maxblkid[0], &ndn->dn_next_maxblkid[0],
785 sizeof (odn->dn_next_maxblkid));
786 for (i = 0; i < TXG_SIZE; i++) {
787 list_move_tail(&ndn->dn_dirty_records[i],
788 &odn->dn_dirty_records[i]);
790 bcopy(&odn->dn_free_ranges[0], &ndn->dn_free_ranges[0],
791 sizeof (odn->dn_free_ranges));
792 ndn->dn_allocated_txg = odn->dn_allocated_txg;
793 ndn->dn_free_txg = odn->dn_free_txg;
794 ndn->dn_assigned_txg = odn->dn_assigned_txg;
795 ndn->dn_dirtyctx = odn->dn_dirtyctx;
796 ndn->dn_dirtyctx_firstset = odn->dn_dirtyctx_firstset;
797 ASSERT(refcount_count(&odn->dn_tx_holds) == 0);
798 refcount_transfer(&ndn->dn_holds, &odn->dn_holds);
799 ASSERT(avl_is_empty(&ndn->dn_dbufs));
800 avl_swap(&ndn->dn_dbufs, &odn->dn_dbufs);
801 ndn->dn_dbufs_count = odn->dn_dbufs_count;
802 ndn->dn_bonus = odn->dn_bonus;
803 ndn->dn_have_spill = odn->dn_have_spill;
804 ndn->dn_zio = odn->dn_zio;
805 ndn->dn_oldused = odn->dn_oldused;
806 ndn->dn_oldflags = odn->dn_oldflags;
807 ndn->dn_olduid = odn->dn_olduid;
808 ndn->dn_oldgid = odn->dn_oldgid;
809 ndn->dn_oldprojid = odn->dn_oldprojid;
810 ndn->dn_newuid = odn->dn_newuid;
811 ndn->dn_newgid = odn->dn_newgid;
812 ndn->dn_newprojid = odn->dn_newprojid;
813 ndn->dn_id_flags = odn->dn_id_flags;
814 dmu_zfetch_init(&ndn->dn_zfetch, NULL);
815 list_move_tail(&ndn->dn_zfetch.zf_stream, &odn->dn_zfetch.zf_stream);
816 ndn->dn_zfetch.zf_dnode = odn->dn_zfetch.zf_dnode;
819 * Update back pointers. Updating the handle fixes the back pointer of
820 * every descendant dbuf as well as the bonus dbuf.
822 ASSERT(ndn->dn_handle->dnh_dnode == odn);
823 ndn->dn_handle->dnh_dnode = ndn;
824 if (ndn->dn_zfetch.zf_dnode == odn) {
825 ndn->dn_zfetch.zf_dnode = ndn;
829 * Invalidate the original dnode by clearing all of its back pointers.
832 odn->dn_handle = NULL;
833 avl_create(&odn->dn_dbufs, dbuf_compare, sizeof (dmu_buf_impl_t),
834 offsetof(dmu_buf_impl_t, db_link));
835 odn->dn_dbufs_count = 0;
836 odn->dn_bonus = NULL;
837 odn->dn_zfetch.zf_dnode = NULL;
840 * Set the low bit of the objset pointer to ensure that dnode_move()
841 * recognizes the dnode as invalid in any subsequent callback.
843 POINTER_INVALIDATE(&odn->dn_objset);
846 * Satisfy the destructor.
848 for (i = 0; i < TXG_SIZE; i++) {
849 list_create(&odn->dn_dirty_records[i],
850 sizeof (dbuf_dirty_record_t),
851 offsetof(dbuf_dirty_record_t, dr_dirty_node));
852 odn->dn_free_ranges[i] = NULL;
853 odn->dn_next_nlevels[i] = 0;
854 odn->dn_next_indblkshift[i] = 0;
855 odn->dn_next_bonustype[i] = 0;
856 odn->dn_rm_spillblk[i] = 0;
857 odn->dn_next_bonuslen[i] = 0;
858 odn->dn_next_blksz[i] = 0;
860 odn->dn_allocated_txg = 0;
861 odn->dn_free_txg = 0;
862 odn->dn_assigned_txg = 0;
863 odn->dn_dirtyctx = 0;
864 odn->dn_dirtyctx_firstset = NULL;
865 odn->dn_have_spill = B_FALSE;
868 odn->dn_oldflags = 0;
871 odn->dn_oldprojid = ZFS_DEFAULT_PROJID;
874 odn->dn_newprojid = ZFS_DEFAULT_PROJID;
875 odn->dn_id_flags = 0;
881 odn->dn_moved = (uint8_t)-1;
886 dnode_move(void *buf, void *newbuf, size_t size, void *arg)
888 dnode_t *odn = buf, *ndn = newbuf;
894 * The dnode is on the objset's list of known dnodes if the objset
895 * pointer is valid. We set the low bit of the objset pointer when
896 * freeing the dnode to invalidate it, and the memory patterns written
897 * by kmem (baddcafe and deadbeef) set at least one of the two low bits.
898 * A newly created dnode sets the objset pointer last of all to indicate
899 * that the dnode is known and in a valid state to be moved by this
903 if (!POINTER_IS_VALID(os)) {
904 DNODE_STAT_BUMP(dnode_move_invalid);
905 return (KMEM_CBRC_DONT_KNOW);
909 * Ensure that the objset does not go away during the move.
911 rw_enter(&os_lock, RW_WRITER);
912 if (os != odn->dn_objset) {
914 DNODE_STAT_BUMP(dnode_move_recheck1);
915 return (KMEM_CBRC_DONT_KNOW);
919 * If the dnode is still valid, then so is the objset. We know that no
920 * valid objset can be freed while we hold os_lock, so we can safely
921 * ensure that the objset remains in use.
923 mutex_enter(&os->os_lock);
926 * Recheck the objset pointer in case the dnode was removed just before
927 * acquiring the lock.
929 if (os != odn->dn_objset) {
930 mutex_exit(&os->os_lock);
932 DNODE_STAT_BUMP(dnode_move_recheck2);
933 return (KMEM_CBRC_DONT_KNOW);
937 * At this point we know that as long as we hold os->os_lock, the dnode
938 * cannot be freed and fields within the dnode can be safely accessed.
939 * The objset listing this dnode cannot go away as long as this dnode is
943 if (DMU_OBJECT_IS_SPECIAL(odn->dn_object)) {
944 mutex_exit(&os->os_lock);
945 DNODE_STAT_BUMP(dnode_move_special);
946 return (KMEM_CBRC_NO);
948 ASSERT(odn->dn_dbuf != NULL); /* only "special" dnodes have no parent */
951 * Lock the dnode handle to prevent the dnode from obtaining any new
952 * holds. This also prevents the descendant dbufs and the bonus dbuf
953 * from accessing the dnode, so that we can discount their holds. The
954 * handle is safe to access because we know that while the dnode cannot
955 * go away, neither can its handle. Once we hold dnh_zrlock, we can
956 * safely move any dnode referenced only by dbufs.
958 if (!zrl_tryenter(&odn->dn_handle->dnh_zrlock)) {
959 mutex_exit(&os->os_lock);
960 DNODE_STAT_BUMP(dnode_move_handle);
961 return (KMEM_CBRC_LATER);
965 * Ensure a consistent view of the dnode's holds and the dnode's dbufs.
966 * We need to guarantee that there is a hold for every dbuf in order to
967 * determine whether the dnode is actively referenced. Falsely matching
968 * a dbuf to an active hold would lead to an unsafe move. It's possible
969 * that a thread already having an active dnode hold is about to add a
970 * dbuf, and we can't compare hold and dbuf counts while the add is in
973 if (!rw_tryenter(&odn->dn_struct_rwlock, RW_WRITER)) {
974 zrl_exit(&odn->dn_handle->dnh_zrlock);
975 mutex_exit(&os->os_lock);
976 DNODE_STAT_BUMP(dnode_move_rwlock);
977 return (KMEM_CBRC_LATER);
981 * A dbuf may be removed (evicted) without an active dnode hold. In that
982 * case, the dbuf count is decremented under the handle lock before the
983 * dbuf's hold is released. This order ensures that if we count the hold
984 * after the dbuf is removed but before its hold is released, we will
985 * treat the unmatched hold as active and exit safely. If we count the
986 * hold before the dbuf is removed, the hold is discounted, and the
987 * removal is blocked until the move completes.
989 refcount = refcount_count(&odn->dn_holds);
990 ASSERT(refcount >= 0);
991 dbufs = odn->dn_dbufs_count;
993 /* We can't have more dbufs than dnode holds. */
994 ASSERT3U(dbufs, <=, refcount);
995 DTRACE_PROBE3(dnode__move, dnode_t *, odn, int64_t, refcount,
998 if (refcount > dbufs) {
999 rw_exit(&odn->dn_struct_rwlock);
1000 zrl_exit(&odn->dn_handle->dnh_zrlock);
1001 mutex_exit(&os->os_lock);
1002 DNODE_STAT_BUMP(dnode_move_active);
1003 return (KMEM_CBRC_LATER);
1006 rw_exit(&odn->dn_struct_rwlock);
1009 * At this point we know that anyone with a hold on the dnode is not
1010 * actively referencing it. The dnode is known and in a valid state to
1011 * move. We're holding the locks needed to execute the critical section.
1013 dnode_move_impl(odn, ndn);
1015 list_link_replace(&odn->dn_link, &ndn->dn_link);
1016 /* If the dnode was safe to move, the refcount cannot have changed. */
1017 ASSERT(refcount == refcount_count(&ndn->dn_holds));
1018 ASSERT(dbufs == ndn->dn_dbufs_count);
1019 zrl_exit(&ndn->dn_handle->dnh_zrlock); /* handle has moved */
1020 mutex_exit(&os->os_lock);
1022 return (KMEM_CBRC_YES);
1024 #endif /* _KERNEL */
1027 dnode_slots_hold(dnode_children_t *children, int idx, int slots)
1029 ASSERT3S(idx + slots, <=, DNODES_PER_BLOCK);
1031 for (int i = idx; i < idx + slots; i++) {
1032 dnode_handle_t *dnh = &children->dnc_children[i];
1033 zrl_add(&dnh->dnh_zrlock);
1038 dnode_slots_rele(dnode_children_t *children, int idx, int slots)
1040 ASSERT3S(idx + slots, <=, DNODES_PER_BLOCK);
1042 for (int i = idx; i < idx + slots; i++) {
1043 dnode_handle_t *dnh = &children->dnc_children[i];
1045 if (zrl_is_locked(&dnh->dnh_zrlock))
1046 zrl_exit(&dnh->dnh_zrlock);
1048 zrl_remove(&dnh->dnh_zrlock);
1053 dnode_slots_tryenter(dnode_children_t *children, int idx, int slots)
1055 ASSERT3S(idx + slots, <=, DNODES_PER_BLOCK);
1057 for (int i = idx; i < idx + slots; i++) {
1058 dnode_handle_t *dnh = &children->dnc_children[i];
1060 if (!zrl_tryenter(&dnh->dnh_zrlock)) {
1061 for (int j = idx; j < i; j++) {
1062 dnh = &children->dnc_children[j];
1063 zrl_exit(&dnh->dnh_zrlock);
1074 dnode_set_slots(dnode_children_t *children, int idx, int slots, void *ptr)
1076 ASSERT3S(idx + slots, <=, DNODES_PER_BLOCK);
1078 for (int i = idx; i < idx + slots; i++) {
1079 dnode_handle_t *dnh = &children->dnc_children[i];
1080 dnh->dnh_dnode = ptr;
1085 dnode_check_slots_free(dnode_children_t *children, int idx, int slots)
1087 ASSERT3S(idx + slots, <=, DNODES_PER_BLOCK);
1089 for (int i = idx; i < idx + slots; i++) {
1090 dnode_handle_t *dnh = &children->dnc_children[i];
1091 dnode_t *dn = dnh->dnh_dnode;
1093 if (dn == DN_SLOT_FREE) {
1095 } else if (DN_SLOT_IS_PTR(dn)) {
1096 mutex_enter(&dn->dn_mtx);
1097 dmu_object_type_t type = dn->dn_type;
1098 mutex_exit(&dn->dn_mtx);
1100 if (type != DMU_OT_NONE)
1115 dnode_reclaim_slots(dnode_children_t *children, int idx, int slots)
1117 ASSERT3S(idx + slots, <=, DNODES_PER_BLOCK);
1119 for (int i = idx; i < idx + slots; i++) {
1120 dnode_handle_t *dnh = &children->dnc_children[i];
1122 ASSERT(zrl_is_locked(&dnh->dnh_zrlock));
1124 if (DN_SLOT_IS_PTR(dnh->dnh_dnode)) {
1125 ASSERT3S(dnh->dnh_dnode->dn_type, ==, DMU_OT_NONE);
1126 dnode_destroy(dnh->dnh_dnode);
1127 dnh->dnh_dnode = DN_SLOT_FREE;
1133 dnode_free_interior_slots(dnode_t *dn)
1135 dnode_children_t *children = dmu_buf_get_user(&dn->dn_dbuf->db);
1136 int epb = dn->dn_dbuf->db.db_size >> DNODE_SHIFT;
1137 int idx = (dn->dn_object & (epb - 1)) + 1;
1138 int slots = dn->dn_num_slots - 1;
1143 ASSERT3S(idx + slots, <=, DNODES_PER_BLOCK);
1145 while (!dnode_slots_tryenter(children, idx, slots))
1146 DNODE_STAT_BUMP(dnode_free_interior_lock_retry);
1148 dnode_set_slots(children, idx, slots, DN_SLOT_FREE);
1149 dnode_slots_rele(children, idx, slots);
1153 dnode_special_close(dnode_handle_t *dnh)
1155 dnode_t *dn = dnh->dnh_dnode;
1158 * Wait for final references to the dnode to clear. This can
1159 * only happen if the arc is asynchronously evicting state that
1160 * has a hold on this dnode while we are trying to evict this
1163 while (refcount_count(&dn->dn_holds) > 0)
1165 ASSERT(dn->dn_dbuf == NULL ||
1166 dmu_buf_get_user(&dn->dn_dbuf->db) == NULL);
1167 zrl_add(&dnh->dnh_zrlock);
1168 dnode_destroy(dn); /* implicit zrl_remove() */
1169 zrl_destroy(&dnh->dnh_zrlock);
1170 dnh->dnh_dnode = NULL;
1174 dnode_special_open(objset_t *os, dnode_phys_t *dnp, uint64_t object,
1175 dnode_handle_t *dnh)
1179 zrl_init(&dnh->dnh_zrlock);
1180 zrl_tryenter(&dnh->dnh_zrlock);
1182 dn = dnode_create(os, dnp, NULL, object, dnh);
1185 zrl_exit(&dnh->dnh_zrlock);
1189 dnode_buf_evict_async(void *dbu)
1191 dnode_children_t *dnc = dbu;
1193 DNODE_STAT_BUMP(dnode_buf_evict);
1195 for (int i = 0; i < dnc->dnc_count; i++) {
1196 dnode_handle_t *dnh = &dnc->dnc_children[i];
1200 * The dnode handle lock guards against the dnode moving to
1201 * another valid address, so there is no need here to guard
1202 * against changes to or from NULL.
1204 if (!DN_SLOT_IS_PTR(dnh->dnh_dnode)) {
1205 zrl_destroy(&dnh->dnh_zrlock);
1206 dnh->dnh_dnode = DN_SLOT_UNINIT;
1210 zrl_add(&dnh->dnh_zrlock);
1211 dn = dnh->dnh_dnode;
1213 * If there are holds on this dnode, then there should
1214 * be holds on the dnode's containing dbuf as well; thus
1215 * it wouldn't be eligible for eviction and this function
1216 * would not have been called.
1218 ASSERT(refcount_is_zero(&dn->dn_holds));
1219 ASSERT(refcount_is_zero(&dn->dn_tx_holds));
1221 dnode_destroy(dn); /* implicit zrl_remove() for first slot */
1222 zrl_destroy(&dnh->dnh_zrlock);
1223 dnh->dnh_dnode = DN_SLOT_UNINIT;
1225 kmem_free(dnc, sizeof (dnode_children_t) +
1226 dnc->dnc_count * sizeof (dnode_handle_t));
1230 * When the DNODE_MUST_BE_FREE flag is set, the "slots" parameter is used
1231 * to ensure the hole at the specified object offset is large enough to
1232 * hold the dnode being created. The slots parameter is also used to ensure
1233 * a dnode does not span multiple dnode blocks. In both of these cases, if
1234 * a failure occurs, ENOSPC is returned. Keep in mind, these failure cases
1235 * are only possible when using DNODE_MUST_BE_FREE.
1237 * If the DNODE_MUST_BE_ALLOCATED flag is set, "slots" must be 0.
1238 * dnode_hold_impl() will check if the requested dnode is already consumed
1239 * as an extra dnode slot by an large dnode, in which case it returns
1243 * EINVAL - Invalid object number or flags.
1244 * ENOSPC - Hole too small to fulfill "slots" request (DNODE_MUST_BE_FREE)
1245 * EEXIST - Refers to an allocated dnode (DNODE_MUST_BE_FREE)
1246 * - Refers to an interior dnode slot (DNODE_MUST_BE_ALLOCATED)
1247 * ENOENT - The requested dnode is not allocated (DNODE_MUST_BE_ALLOCATED)
1248 * EIO - I/O error when reading the meta dnode dbuf.
1250 * succeeds even for free dnodes.
1253 dnode_hold_impl(objset_t *os, uint64_t object, int flag, int slots,
1254 void *tag, dnode_t **dnp)
1257 int drop_struct_lock = FALSE;
1262 dnode_children_t *dnc;
1263 dnode_phys_t *dn_block;
1264 dnode_handle_t *dnh;
1266 ASSERT(!(flag & DNODE_MUST_BE_ALLOCATED) || (slots == 0));
1267 ASSERT(!(flag & DNODE_MUST_BE_FREE) || (slots > 0));
1270 * If you are holding the spa config lock as writer, you shouldn't
1271 * be asking the DMU to do *anything* unless it's the root pool
1272 * which may require us to read from the root filesystem while
1273 * holding some (not all) of the locks as writer.
1275 ASSERT(spa_config_held(os->os_spa, SCL_ALL, RW_WRITER) == 0 ||
1276 (spa_is_root(os->os_spa) &&
1277 spa_config_held(os->os_spa, SCL_STATE, RW_WRITER)));
1279 if (object == DMU_USERUSED_OBJECT || object == DMU_GROUPUSED_OBJECT ||
1280 object == DMU_PROJECTUSED_OBJECT) {
1281 if (object == DMU_USERUSED_OBJECT)
1282 dn = DMU_USERUSED_DNODE(os);
1283 else if (object == DMU_GROUPUSED_OBJECT)
1284 dn = DMU_GROUPUSED_DNODE(os);
1286 dn = DMU_PROJECTUSED_DNODE(os);
1288 return (SET_ERROR(ENOENT));
1290 if ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE)
1291 return (SET_ERROR(ENOENT));
1292 if ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)
1293 return (SET_ERROR(EEXIST));
1295 (void) refcount_add(&dn->dn_holds, tag);
1300 if (object == 0 || object >= DN_MAX_OBJECT)
1301 return (SET_ERROR(EINVAL));
1303 mdn = DMU_META_DNODE(os);
1304 ASSERT(mdn->dn_object == DMU_META_DNODE_OBJECT);
1308 if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) {
1309 rw_enter(&mdn->dn_struct_rwlock, RW_READER);
1310 drop_struct_lock = TRUE;
1313 blk = dbuf_whichblock(mdn, 0, object * sizeof (dnode_phys_t));
1315 db = dbuf_hold(mdn, blk, FTAG);
1316 if (drop_struct_lock)
1317 rw_exit(&mdn->dn_struct_rwlock);
1319 DNODE_STAT_BUMP(dnode_hold_dbuf_hold);
1320 return (SET_ERROR(EIO));
1324 * We do not need to decrypt to read the dnode so it doesn't matter
1325 * if we get the encrypted or decrypted version.
1327 err = dbuf_read(db, NULL, DB_RF_CANFAIL | DB_RF_NO_DECRYPT);
1329 DNODE_STAT_BUMP(dnode_hold_dbuf_read);
1330 dbuf_rele(db, FTAG);
1334 ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT);
1335 epb = db->db.db_size >> DNODE_SHIFT;
1337 idx = object & (epb - 1);
1338 dn_block = (dnode_phys_t *)db->db.db_data;
1340 ASSERT(DB_DNODE(db)->dn_type == DMU_OT_DNODE);
1341 dnc = dmu_buf_get_user(&db->db);
1344 dnode_children_t *winner;
1347 dnc = kmem_zalloc(sizeof (dnode_children_t) +
1348 epb * sizeof (dnode_handle_t), KM_SLEEP);
1349 dnc->dnc_count = epb;
1350 dnh = &dnc->dnc_children[0];
1352 /* Initialize dnode slot status from dnode_phys_t */
1353 for (int i = 0; i < epb; i++) {
1354 zrl_init(&dnh[i].dnh_zrlock);
1361 if (dn_block[i].dn_type != DMU_OT_NONE) {
1362 int interior = dn_block[i].dn_extra_slots;
1364 dnode_set_slots(dnc, i, 1, DN_SLOT_ALLOCATED);
1365 dnode_set_slots(dnc, i + 1, interior,
1369 dnh[i].dnh_dnode = DN_SLOT_FREE;
1374 dmu_buf_init_user(&dnc->dnc_dbu, NULL,
1375 dnode_buf_evict_async, NULL);
1376 winner = dmu_buf_set_user(&db->db, &dnc->dnc_dbu);
1377 if (winner != NULL) {
1379 for (int i = 0; i < epb; i++)
1380 zrl_destroy(&dnh[i].dnh_zrlock);
1382 kmem_free(dnc, sizeof (dnode_children_t) +
1383 epb * sizeof (dnode_handle_t));
1388 ASSERT(dnc->dnc_count == epb);
1389 dn = DN_SLOT_UNINIT;
1391 if (flag & DNODE_MUST_BE_ALLOCATED) {
1394 while (dn == DN_SLOT_UNINIT) {
1395 dnode_slots_hold(dnc, idx, slots);
1396 dnh = &dnc->dnc_children[idx];
1398 if (DN_SLOT_IS_PTR(dnh->dnh_dnode)) {
1399 dn = dnh->dnh_dnode;
1401 } else if (dnh->dnh_dnode == DN_SLOT_INTERIOR) {
1402 DNODE_STAT_BUMP(dnode_hold_alloc_interior);
1403 dnode_slots_rele(dnc, idx, slots);
1404 dbuf_rele(db, FTAG);
1405 return (SET_ERROR(EEXIST));
1406 } else if (dnh->dnh_dnode != DN_SLOT_ALLOCATED) {
1407 DNODE_STAT_BUMP(dnode_hold_alloc_misses);
1408 dnode_slots_rele(dnc, idx, slots);
1409 dbuf_rele(db, FTAG);
1410 return (SET_ERROR(ENOENT));
1413 dnode_slots_rele(dnc, idx, slots);
1414 if (!dnode_slots_tryenter(dnc, idx, slots)) {
1415 DNODE_STAT_BUMP(dnode_hold_alloc_lock_retry);
1420 * Someone else won the race and called dnode_create()
1421 * after we checked DN_SLOT_IS_PTR() above but before
1422 * we acquired the lock.
1424 if (DN_SLOT_IS_PTR(dnh->dnh_dnode)) {
1425 DNODE_STAT_BUMP(dnode_hold_alloc_lock_misses);
1426 dn = dnh->dnh_dnode;
1428 dn = dnode_create(os, dn_block + idx, db,
1433 mutex_enter(&dn->dn_mtx);
1434 if (dn->dn_type == DMU_OT_NONE) {
1435 DNODE_STAT_BUMP(dnode_hold_alloc_type_none);
1436 mutex_exit(&dn->dn_mtx);
1437 dnode_slots_rele(dnc, idx, slots);
1438 dbuf_rele(db, FTAG);
1439 return (SET_ERROR(ENOENT));
1442 DNODE_STAT_BUMP(dnode_hold_alloc_hits);
1443 } else if (flag & DNODE_MUST_BE_FREE) {
1445 if (idx + slots - 1 >= DNODES_PER_BLOCK) {
1446 DNODE_STAT_BUMP(dnode_hold_free_overflow);
1447 dbuf_rele(db, FTAG);
1448 return (SET_ERROR(ENOSPC));
1451 while (dn == DN_SLOT_UNINIT) {
1452 dnode_slots_hold(dnc, idx, slots);
1454 if (!dnode_check_slots_free(dnc, idx, slots)) {
1455 DNODE_STAT_BUMP(dnode_hold_free_misses);
1456 dnode_slots_rele(dnc, idx, slots);
1457 dbuf_rele(db, FTAG);
1458 return (SET_ERROR(ENOSPC));
1461 dnode_slots_rele(dnc, idx, slots);
1462 if (!dnode_slots_tryenter(dnc, idx, slots)) {
1463 DNODE_STAT_BUMP(dnode_hold_free_lock_retry);
1467 if (!dnode_check_slots_free(dnc, idx, slots)) {
1468 DNODE_STAT_BUMP(dnode_hold_free_lock_misses);
1469 dnode_slots_rele(dnc, idx, slots);
1470 dbuf_rele(db, FTAG);
1471 return (SET_ERROR(ENOSPC));
1475 * Allocated but otherwise free dnodes which would
1476 * be in the interior of a multi-slot dnodes need
1477 * to be freed. Single slot dnodes can be safely
1478 * re-purposed as a performance optimization.
1481 dnode_reclaim_slots(dnc, idx + 1, slots - 1);
1483 dnh = &dnc->dnc_children[idx];
1484 if (DN_SLOT_IS_PTR(dnh->dnh_dnode)) {
1485 dn = dnh->dnh_dnode;
1487 dn = dnode_create(os, dn_block + idx, db,
1492 mutex_enter(&dn->dn_mtx);
1493 if (!refcount_is_zero(&dn->dn_holds)) {
1494 DNODE_STAT_BUMP(dnode_hold_free_refcount);
1495 mutex_exit(&dn->dn_mtx);
1496 dnode_slots_rele(dnc, idx, slots);
1497 dbuf_rele(db, FTAG);
1498 return (SET_ERROR(EEXIST));
1501 dnode_set_slots(dnc, idx + 1, slots - 1, DN_SLOT_INTERIOR);
1502 DNODE_STAT_BUMP(dnode_hold_free_hits);
1504 dbuf_rele(db, FTAG);
1505 return (SET_ERROR(EINVAL));
1508 if (dn->dn_free_txg) {
1509 DNODE_STAT_BUMP(dnode_hold_free_txg);
1511 mutex_exit(&dn->dn_mtx);
1512 dnode_slots_rele(dnc, idx, slots);
1513 dbuf_rele(db, FTAG);
1514 return (SET_ERROR(type == DMU_OT_NONE ? ENOENT : EEXIST));
1517 if (refcount_add(&dn->dn_holds, tag) == 1)
1518 dbuf_add_ref(db, dnh);
1520 mutex_exit(&dn->dn_mtx);
1522 /* Now we can rely on the hold to prevent the dnode from moving. */
1523 dnode_slots_rele(dnc, idx, slots);
1526 ASSERT3P(dn->dn_dbuf, ==, db);
1527 ASSERT3U(dn->dn_object, ==, object);
1528 dbuf_rele(db, FTAG);
1535 * Return held dnode if the object is allocated, NULL if not.
1538 dnode_hold(objset_t *os, uint64_t object, void *tag, dnode_t **dnp)
1540 return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, 0, tag,
1545 * Can only add a reference if there is already at least one
1546 * reference on the dnode. Returns FALSE if unable to add a
1550 dnode_add_ref(dnode_t *dn, void *tag)
1552 mutex_enter(&dn->dn_mtx);
1553 if (refcount_is_zero(&dn->dn_holds)) {
1554 mutex_exit(&dn->dn_mtx);
1557 VERIFY(1 < refcount_add(&dn->dn_holds, tag));
1558 mutex_exit(&dn->dn_mtx);
1563 dnode_rele(dnode_t *dn, void *tag)
1565 mutex_enter(&dn->dn_mtx);
1566 dnode_rele_and_unlock(dn, tag);
1570 dnode_rele_and_unlock(dnode_t *dn, void *tag)
1573 /* Get while the hold prevents the dnode from moving. */
1574 dmu_buf_impl_t *db = dn->dn_dbuf;
1575 dnode_handle_t *dnh = dn->dn_handle;
1577 refs = refcount_remove(&dn->dn_holds, tag);
1578 mutex_exit(&dn->dn_mtx);
1581 * It's unsafe to release the last hold on a dnode by dnode_rele() or
1582 * indirectly by dbuf_rele() while relying on the dnode handle to
1583 * prevent the dnode from moving, since releasing the last hold could
1584 * result in the dnode's parent dbuf evicting its dnode handles. For
1585 * that reason anyone calling dnode_rele() or dbuf_rele() without some
1586 * other direct or indirect hold on the dnode must first drop the dnode
1589 ASSERT(refs > 0 || dnh->dnh_zrlock.zr_owner != curthread);
1591 /* NOTE: the DNODE_DNODE does not have a dn_dbuf */
1592 if (refs == 0 && db != NULL) {
1594 * Another thread could add a hold to the dnode handle in
1595 * dnode_hold_impl() while holding the parent dbuf. Since the
1596 * hold on the parent dbuf prevents the handle from being
1597 * destroyed, the hold on the handle is OK. We can't yet assert
1598 * that the handle has zero references, but that will be
1599 * asserted anyway when the handle gets destroyed.
1606 dnode_setdirty(dnode_t *dn, dmu_tx_t *tx)
1608 objset_t *os = dn->dn_objset;
1609 uint64_t txg = tx->tx_txg;
1611 if (DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
1612 dsl_dataset_dirty(os->os_dsl_dataset, tx);
1619 mutex_enter(&dn->dn_mtx);
1620 ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg);
1621 ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg);
1622 mutex_exit(&dn->dn_mtx);
1626 * Determine old uid/gid when necessary
1628 dmu_objset_userquota_get_ids(dn, B_TRUE, tx);
1630 multilist_t *dirtylist = os->os_dirty_dnodes[txg & TXG_MASK];
1631 multilist_sublist_t *mls = multilist_sublist_lock_obj(dirtylist, dn);
1634 * If we are already marked dirty, we're done.
1636 if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) {
1637 multilist_sublist_unlock(mls);
1641 ASSERT(!refcount_is_zero(&dn->dn_holds) ||
1642 !avl_is_empty(&dn->dn_dbufs));
1643 ASSERT(dn->dn_datablksz != 0);
1644 ASSERT0(dn->dn_next_bonuslen[txg&TXG_MASK]);
1645 ASSERT0(dn->dn_next_blksz[txg&TXG_MASK]);
1646 ASSERT0(dn->dn_next_bonustype[txg&TXG_MASK]);
1648 dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
1649 dn->dn_object, txg);
1651 multilist_sublist_insert_head(mls, dn);
1653 multilist_sublist_unlock(mls);
1656 * The dnode maintains a hold on its containing dbuf as
1657 * long as there are holds on it. Each instantiated child
1658 * dbuf maintains a hold on the dnode. When the last child
1659 * drops its hold, the dnode will drop its hold on the
1660 * containing dbuf. We add a "dirty hold" here so that the
1661 * dnode will hang around after we finish processing its
1664 VERIFY(dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg));
1666 (void) dbuf_dirty(dn->dn_dbuf, tx);
1668 dsl_dataset_dirty(os->os_dsl_dataset, tx);
1672 dnode_free(dnode_t *dn, dmu_tx_t *tx)
1674 mutex_enter(&dn->dn_mtx);
1675 if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) {
1676 mutex_exit(&dn->dn_mtx);
1679 dn->dn_free_txg = tx->tx_txg;
1680 mutex_exit(&dn->dn_mtx);
1682 dnode_setdirty(dn, tx);
1686 * Try to change the block size for the indicated dnode. This can only
1687 * succeed if there are no blocks allocated or dirty beyond first block
1690 dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx)
1695 ASSERT3U(size, <=, spa_maxblocksize(dmu_objset_spa(dn->dn_objset)));
1697 size = SPA_MINBLOCKSIZE;
1699 size = P2ROUNDUP(size, SPA_MINBLOCKSIZE);
1701 if (ibs == dn->dn_indblkshift)
1704 if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0)
1707 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1709 /* Check for any allocated blocks beyond the first */
1710 if (dn->dn_maxblkid != 0)
1713 mutex_enter(&dn->dn_dbufs_mtx);
1714 for (db = avl_first(&dn->dn_dbufs); db != NULL;
1715 db = AVL_NEXT(&dn->dn_dbufs, db)) {
1716 if (db->db_blkid != 0 && db->db_blkid != DMU_BONUS_BLKID &&
1717 db->db_blkid != DMU_SPILL_BLKID) {
1718 mutex_exit(&dn->dn_dbufs_mtx);
1722 mutex_exit(&dn->dn_dbufs_mtx);
1724 if (ibs && dn->dn_nlevels != 1)
1727 /* resize the old block */
1728 err = dbuf_hold_impl(dn, 0, 0, TRUE, FALSE, FTAG, &db);
1730 dbuf_new_size(db, size, tx);
1731 else if (err != ENOENT)
1734 dnode_setdblksz(dn, size);
1735 dnode_setdirty(dn, tx);
1736 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size;
1738 dn->dn_indblkshift = ibs;
1739 dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs;
1741 /* rele after we have fixed the blocksize in the dnode */
1743 dbuf_rele(db, FTAG);
1745 rw_exit(&dn->dn_struct_rwlock);
1749 rw_exit(&dn->dn_struct_rwlock);
1750 return (SET_ERROR(ENOTSUP));
1754 dnode_set_nlevels_impl(dnode_t *dn, int new_nlevels, dmu_tx_t *tx)
1756 uint64_t txgoff = tx->tx_txg & TXG_MASK;
1757 int old_nlevels = dn->dn_nlevels;
1760 dbuf_dirty_record_t *new, *dr, *dr_next;
1762 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
1764 dn->dn_nlevels = new_nlevels;
1766 ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]);
1767 dn->dn_next_nlevels[txgoff] = new_nlevels;
1769 /* dirty the left indirects */
1770 db = dbuf_hold_level(dn, old_nlevels, 0, FTAG);
1772 new = dbuf_dirty(db, tx);
1773 dbuf_rele(db, FTAG);
1775 /* transfer the dirty records to the new indirect */
1776 mutex_enter(&dn->dn_mtx);
1777 mutex_enter(&new->dt.di.dr_mtx);
1778 list = &dn->dn_dirty_records[txgoff];
1779 for (dr = list_head(list); dr; dr = dr_next) {
1780 dr_next = list_next(&dn->dn_dirty_records[txgoff], dr);
1781 if (dr->dr_dbuf->db_level != new_nlevels-1 &&
1782 dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
1783 dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
1784 ASSERT(dr->dr_dbuf->db_level == old_nlevels-1);
1785 list_remove(&dn->dn_dirty_records[txgoff], dr);
1786 list_insert_tail(&new->dt.di.dr_children, dr);
1787 dr->dr_parent = new;
1790 mutex_exit(&new->dt.di.dr_mtx);
1791 mutex_exit(&dn->dn_mtx);
1795 dnode_set_nlevels(dnode_t *dn, int nlevels, dmu_tx_t *tx)
1799 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1801 if (dn->dn_nlevels == nlevels) {
1804 } else if (nlevels < dn->dn_nlevels) {
1805 ret = SET_ERROR(EINVAL);
1809 dnode_set_nlevels_impl(dn, nlevels, tx);
1812 rw_exit(&dn->dn_struct_rwlock);
1816 /* read-holding callers must not rely on the lock being continuously held */
1818 dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t have_read)
1820 int epbs, new_nlevels;
1823 ASSERT(blkid != DMU_BONUS_BLKID);
1826 RW_READ_HELD(&dn->dn_struct_rwlock) :
1827 RW_WRITE_HELD(&dn->dn_struct_rwlock));
1830 * if we have a read-lock, check to see if we need to do any work
1831 * before upgrading to a write-lock.
1834 if (blkid <= dn->dn_maxblkid)
1837 if (!rw_tryupgrade(&dn->dn_struct_rwlock)) {
1838 rw_exit(&dn->dn_struct_rwlock);
1839 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1843 if (blkid <= dn->dn_maxblkid)
1846 dn->dn_maxblkid = blkid;
1847 dn->dn_next_maxblkid[tx->tx_txg & TXG_MASK] = blkid;
1850 * Compute the number of levels necessary to support the new maxblkid.
1853 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1854 for (sz = dn->dn_nblkptr;
1855 sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs)
1858 ASSERT3U(new_nlevels, <=, DN_MAX_LEVELS);
1860 if (new_nlevels > dn->dn_nlevels)
1861 dnode_set_nlevels_impl(dn, new_nlevels, tx);
1865 rw_downgrade(&dn->dn_struct_rwlock);
1869 dnode_dirty_l1(dnode_t *dn, uint64_t l1blkid, dmu_tx_t *tx)
1871 dmu_buf_impl_t *db = dbuf_hold_level(dn, 1, l1blkid, FTAG);
1873 dmu_buf_will_dirty(&db->db, tx);
1874 dbuf_rele(db, FTAG);
1879 dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx)
1882 uint64_t blkoff, blkid, nblks;
1883 int blksz, blkshift, head, tail;
1887 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1888 blksz = dn->dn_datablksz;
1889 blkshift = dn->dn_datablkshift;
1890 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1892 if (len == DMU_OBJECT_END) {
1893 len = UINT64_MAX - off;
1898 * First, block align the region to free:
1901 head = P2NPHASE(off, blksz);
1902 blkoff = P2PHASE(off, blksz);
1903 if ((off >> blkshift) > dn->dn_maxblkid)
1906 ASSERT(dn->dn_maxblkid == 0);
1907 if (off == 0 && len >= blksz) {
1909 * Freeing the whole block; fast-track this request.
1910 * Note that we won't dirty any indirect blocks,
1911 * which is fine because we will be freeing the entire
1912 * file and thus all indirect blocks will be freed
1913 * by free_children().
1918 } else if (off >= blksz) {
1919 /* Freeing past end-of-data */
1922 /* Freeing part of the block. */
1924 ASSERT3U(head, >, 0);
1928 /* zero out any partial block data at the start of the range */
1930 ASSERT3U(blkoff + head, ==, blksz);
1933 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, 0, off),
1934 TRUE, FALSE, FTAG, &db) == 0) {
1937 /* don't dirty if it isn't on disk and isn't dirty */
1938 if (db->db_last_dirty ||
1939 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1940 rw_exit(&dn->dn_struct_rwlock);
1941 dmu_buf_will_dirty(&db->db, tx);
1942 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1943 data = db->db.db_data;
1944 bzero(data + blkoff, head);
1946 dbuf_rele(db, FTAG);
1952 /* If the range was less than one block, we're done */
1956 /* If the remaining range is past end of file, we're done */
1957 if ((off >> blkshift) > dn->dn_maxblkid)
1960 ASSERT(ISP2(blksz));
1964 tail = P2PHASE(len, blksz);
1966 ASSERT0(P2PHASE(off, blksz));
1967 /* zero out any partial block data at the end of the range */
1971 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, 0, off+len),
1972 TRUE, FALSE, FTAG, &db) == 0) {
1973 /* don't dirty if not on disk and not dirty */
1974 if (db->db_last_dirty ||
1975 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1976 rw_exit(&dn->dn_struct_rwlock);
1977 dmu_buf_will_dirty(&db->db, tx);
1978 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1979 bzero(db->db.db_data, tail);
1981 dbuf_rele(db, FTAG);
1986 /* If the range did not include a full block, we are done */
1990 ASSERT(IS_P2ALIGNED(off, blksz));
1991 ASSERT(trunc || IS_P2ALIGNED(len, blksz));
1992 blkid = off >> blkshift;
1993 nblks = len >> blkshift;
1998 * Dirty all the indirect blocks in this range. Note that only
1999 * the first and last indirect blocks can actually be written
2000 * (if they were partially freed) -- they must be dirtied, even if
2001 * they do not exist on disk yet. The interior blocks will
2002 * be freed by free_children(), so they will not actually be written.
2003 * Even though these interior blocks will not be written, we
2004 * dirty them for two reasons:
2006 * - It ensures that the indirect blocks remain in memory until
2007 * syncing context. (They have already been prefetched by
2008 * dmu_tx_hold_free(), so we don't have to worry about reading
2009 * them serially here.)
2011 * - The dirty space accounting will put pressure on the txg sync
2012 * mechanism to begin syncing, and to delay transactions if there
2013 * is a large amount of freeing. Even though these indirect
2014 * blocks will not be written, we could need to write the same
2015 * amount of space if we copy the freed BPs into deadlists.
2017 if (dn->dn_nlevels > 1) {
2018 uint64_t first, last;
2020 first = blkid >> epbs;
2021 dnode_dirty_l1(dn, first, tx);
2023 last = dn->dn_maxblkid >> epbs;
2025 last = (blkid + nblks - 1) >> epbs;
2027 dnode_dirty_l1(dn, last, tx);
2029 int shift = dn->dn_datablkshift + dn->dn_indblkshift -
2031 for (uint64_t i = first + 1; i < last; i++) {
2033 * Set i to the blockid of the next non-hole
2034 * level-1 indirect block at or after i. Note
2035 * that dnode_next_offset() operates in terms of
2036 * level-0-equivalent bytes.
2038 uint64_t ibyte = i << shift;
2039 int err = dnode_next_offset(dn, DNODE_FIND_HAVELOCK,
2046 * Normally we should not see an error, either
2047 * from dnode_next_offset() or dbuf_hold_level()
2048 * (except for ESRCH from dnode_next_offset).
2049 * If there is an i/o error, then when we read
2050 * this block in syncing context, it will use
2051 * ZIO_FLAG_MUSTSUCCEED, and thus hang/panic according
2052 * to the "failmode" property. dnode_next_offset()
2053 * doesn't have a flag to indicate MUSTSUCCEED.
2058 dnode_dirty_l1(dn, i, tx);
2064 * Add this range to the dnode range list.
2065 * We will finish up this free operation in the syncing phase.
2067 mutex_enter(&dn->dn_mtx);
2069 int txgoff = tx->tx_txg & TXG_MASK;
2070 if (dn->dn_free_ranges[txgoff] == NULL) {
2071 dn->dn_free_ranges[txgoff] =
2072 range_tree_create(NULL, NULL, &dn->dn_mtx);
2074 range_tree_clear(dn->dn_free_ranges[txgoff], blkid, nblks);
2075 range_tree_add(dn->dn_free_ranges[txgoff], blkid, nblks);
2077 dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
2078 blkid, nblks, tx->tx_txg);
2079 mutex_exit(&dn->dn_mtx);
2081 dbuf_free_range(dn, blkid, blkid + nblks - 1, tx);
2082 dnode_setdirty(dn, tx);
2085 rw_exit(&dn->dn_struct_rwlock);
2089 dnode_spill_freed(dnode_t *dn)
2093 mutex_enter(&dn->dn_mtx);
2094 for (i = 0; i < TXG_SIZE; i++) {
2095 if (dn->dn_rm_spillblk[i] == DN_KILL_SPILLBLK)
2098 mutex_exit(&dn->dn_mtx);
2099 return (i < TXG_SIZE);
2102 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
2104 dnode_block_freed(dnode_t *dn, uint64_t blkid)
2106 void *dp = spa_get_dsl(dn->dn_objset->os_spa);
2109 if (blkid == DMU_BONUS_BLKID)
2113 * If we're in the process of opening the pool, dp will not be
2114 * set yet, but there shouldn't be anything dirty.
2119 if (dn->dn_free_txg)
2122 if (blkid == DMU_SPILL_BLKID)
2123 return (dnode_spill_freed(dn));
2125 mutex_enter(&dn->dn_mtx);
2126 for (i = 0; i < TXG_SIZE; i++) {
2127 if (dn->dn_free_ranges[i] != NULL &&
2128 range_tree_contains(dn->dn_free_ranges[i], blkid, 1))
2131 mutex_exit(&dn->dn_mtx);
2132 return (i < TXG_SIZE);
2135 /* call from syncing context when we actually write/free space for this dnode */
2137 dnode_diduse_space(dnode_t *dn, int64_t delta)
2140 dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n",
2142 (u_longlong_t)dn->dn_phys->dn_used,
2145 mutex_enter(&dn->dn_mtx);
2146 space = DN_USED_BYTES(dn->dn_phys);
2148 ASSERT3U(space + delta, >=, space); /* no overflow */
2150 ASSERT3U(space, >=, -delta); /* no underflow */
2153 if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_DNODE_BYTES) {
2154 ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0);
2155 ASSERT0(P2PHASE(space, 1<<DEV_BSHIFT));
2156 dn->dn_phys->dn_used = space >> DEV_BSHIFT;
2158 dn->dn_phys->dn_used = space;
2159 dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES;
2161 mutex_exit(&dn->dn_mtx);
2165 * Scans a block at the indicated "level" looking for a hole or data,
2166 * depending on 'flags'.
2168 * If level > 0, then we are scanning an indirect block looking at its
2169 * pointers. If level == 0, then we are looking at a block of dnodes.
2171 * If we don't find what we are looking for in the block, we return ESRCH.
2172 * Otherwise, return with *offset pointing to the beginning (if searching
2173 * forwards) or end (if searching backwards) of the range covered by the
2174 * block pointer we matched on (or dnode).
2176 * The basic search algorithm used below by dnode_next_offset() is to
2177 * use this function to search up the block tree (widen the search) until
2178 * we find something (i.e., we don't return ESRCH) and then search back
2179 * down the tree (narrow the search) until we reach our original search
2183 dnode_next_offset_level(dnode_t *dn, int flags, uint64_t *offset,
2184 int lvl, uint64_t blkfill, uint64_t txg)
2186 dmu_buf_impl_t *db = NULL;
2188 uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2189 uint64_t epb = 1ULL << epbs;
2190 uint64_t minfill, maxfill;
2192 int i, inc, error, span;
2194 hole = ((flags & DNODE_FIND_HOLE) != 0);
2195 inc = (flags & DNODE_FIND_BACKWARDS) ? -1 : 1;
2196 ASSERT(txg == 0 || !hole);
2198 if (lvl == dn->dn_phys->dn_nlevels) {
2200 epb = dn->dn_phys->dn_nblkptr;
2201 data = dn->dn_phys->dn_blkptr;
2203 uint64_t blkid = dbuf_whichblock(dn, lvl, *offset);
2204 error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FALSE, FTAG, &db);
2206 if (error != ENOENT)
2211 * This can only happen when we are searching up
2212 * the block tree for data. We don't really need to
2213 * adjust the offset, as we will just end up looking
2214 * at the pointer to this block in its parent, and its
2215 * going to be unallocated, so we will skip over it.
2217 return (SET_ERROR(ESRCH));
2219 error = dbuf_read(db, NULL,
2220 DB_RF_CANFAIL | DB_RF_HAVESTRUCT | DB_RF_NO_DECRYPT);
2222 dbuf_rele(db, FTAG);
2225 data = db->db.db_data;
2229 if (db != NULL && txg != 0 && (db->db_blkptr == NULL ||
2230 db->db_blkptr->blk_birth <= txg ||
2231 BP_IS_HOLE(db->db_blkptr))) {
2233 * This can only happen when we are searching up the tree
2234 * and these conditions mean that we need to keep climbing.
2236 error = SET_ERROR(ESRCH);
2237 } else if (lvl == 0) {
2238 dnode_phys_t *dnp = data;
2240 ASSERT(dn->dn_type == DMU_OT_DNODE);
2241 ASSERT(!(flags & DNODE_FIND_BACKWARDS));
2243 for (i = (*offset >> DNODE_SHIFT) & (blkfill - 1);
2244 i < blkfill; i += dnp[i].dn_extra_slots + 1) {
2245 if ((dnp[i].dn_type == DMU_OT_NONE) == hole)
2250 error = SET_ERROR(ESRCH);
2252 *offset = (*offset & ~(DNODE_BLOCK_SIZE - 1)) +
2255 blkptr_t *bp = data;
2256 uint64_t start = *offset;
2257 span = (lvl - 1) * epbs + dn->dn_datablkshift;
2259 maxfill = blkfill << ((lvl - 1) * epbs);
2266 if (span >= 8 * sizeof (*offset)) {
2267 /* This only happens on the highest indirection level */
2268 ASSERT3U((lvl - 1), ==, dn->dn_phys->dn_nlevels - 1);
2271 *offset = *offset >> span;
2274 for (i = BF64_GET(*offset, 0, epbs);
2275 i >= 0 && i < epb; i += inc) {
2276 if (BP_GET_FILL(&bp[i]) >= minfill &&
2277 BP_GET_FILL(&bp[i]) <= maxfill &&
2278 (hole || bp[i].blk_birth > txg))
2280 if (inc > 0 || *offset > 0)
2284 if (span >= 8 * sizeof (*offset)) {
2287 *offset = *offset << span;
2291 /* traversing backwards; position offset at the end */
2292 ASSERT3U(*offset, <=, start);
2293 *offset = MIN(*offset + (1ULL << span) - 1, start);
2294 } else if (*offset < start) {
2297 if (i < 0 || i >= epb)
2298 error = SET_ERROR(ESRCH);
2302 dbuf_rele(db, FTAG);
2308 * Find the next hole, data, or sparse region at or after *offset.
2309 * The value 'blkfill' tells us how many items we expect to find
2310 * in an L0 data block; this value is 1 for normal objects,
2311 * DNODES_PER_BLOCK for the meta dnode, and some fraction of
2312 * DNODES_PER_BLOCK when searching for sparse regions thereof.
2316 * dnode_next_offset(dn, flags, offset, 1, 1, 0);
2317 * Finds the next/previous hole/data in a file.
2318 * Used in dmu_offset_next().
2320 * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
2321 * Finds the next free/allocated dnode an objset's meta-dnode.
2322 * Only finds objects that have new contents since txg (ie.
2323 * bonus buffer changes and content removal are ignored).
2324 * Used in dmu_object_next().
2326 * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
2327 * Finds the next L2 meta-dnode bp that's at most 1/4 full.
2328 * Used in dmu_object_alloc().
2331 dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset,
2332 int minlvl, uint64_t blkfill, uint64_t txg)
2334 uint64_t initial_offset = *offset;
2338 if (!(flags & DNODE_FIND_HAVELOCK))
2339 rw_enter(&dn->dn_struct_rwlock, RW_READER);
2341 if (dn->dn_phys->dn_nlevels == 0) {
2342 error = SET_ERROR(ESRCH);
2346 if (dn->dn_datablkshift == 0) {
2347 if (*offset < dn->dn_datablksz) {
2348 if (flags & DNODE_FIND_HOLE)
2349 *offset = dn->dn_datablksz;
2351 error = SET_ERROR(ESRCH);
2356 maxlvl = dn->dn_phys->dn_nlevels;
2358 for (lvl = minlvl; lvl <= maxlvl; lvl++) {
2359 error = dnode_next_offset_level(dn,
2360 flags, offset, lvl, blkfill, txg);
2365 while (error == 0 && --lvl >= minlvl) {
2366 error = dnode_next_offset_level(dn,
2367 flags, offset, lvl, blkfill, txg);
2371 * There's always a "virtual hole" at the end of the object, even
2372 * if all BP's which physically exist are non-holes.
2374 if ((flags & DNODE_FIND_HOLE) && error == ESRCH && txg == 0 &&
2375 minlvl == 1 && blkfill == 1 && !(flags & DNODE_FIND_BACKWARDS)) {
2379 if (error == 0 && (flags & DNODE_FIND_BACKWARDS ?
2380 initial_offset < *offset : initial_offset > *offset))
2381 error = SET_ERROR(ESRCH);
2383 if (!(flags & DNODE_FIND_HAVELOCK))
2384 rw_exit(&dn->dn_struct_rwlock);