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, 2014 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>
38 #include <sys/range_tree.h>
40 static kmem_cache_t *dnode_cache;
42 * Define DNODE_STATS to turn on statistic gathering. By default, it is only
43 * turned on when DEBUG is also defined.
50 #define DNODE_STAT_ADD(stat) ((stat)++)
52 #define DNODE_STAT_ADD(stat) /* nothing */
53 #endif /* DNODE_STATS */
55 static dnode_phys_t dnode_phys_zero;
57 int zfs_default_bs = SPA_MINBLOCKSHIFT;
58 int zfs_default_ibs = DN_MAX_INDBLKSHIFT;
61 static kmem_cbrc_t dnode_move(void *, void *, size_t, void *);
65 dbuf_compare(const void *x1, const void *x2)
67 const dmu_buf_impl_t *d1 = x1;
68 const dmu_buf_impl_t *d2 = x2;
70 if (d1->db_level < d2->db_level) {
73 if (d1->db_level > d2->db_level) {
77 if (d1->db_blkid < d2->db_blkid) {
80 if (d1->db_blkid > d2->db_blkid) {
84 if (d1->db_state < d2->db_state) {
87 if (d1->db_state > d2->db_state) {
91 ASSERT3S(d1->db_state, !=, DB_SEARCH);
92 ASSERT3S(d2->db_state, !=, DB_SEARCH);
94 if ((uintptr_t)d1 < (uintptr_t)d2) {
97 if ((uintptr_t)d1 > (uintptr_t)d2) {
105 dnode_cons(void *arg, void *unused, int kmflag)
110 rw_init(&dn->dn_struct_rwlock, NULL, RW_DEFAULT, NULL);
111 mutex_init(&dn->dn_mtx, NULL, MUTEX_DEFAULT, NULL);
112 mutex_init(&dn->dn_dbufs_mtx, NULL, MUTEX_DEFAULT, NULL);
113 cv_init(&dn->dn_notxholds, NULL, CV_DEFAULT, NULL);
116 * Every dbuf has a reference, and dropping a tracked reference is
117 * O(number of references), so don't track dn_holds.
119 refcount_create_untracked(&dn->dn_holds);
120 refcount_create(&dn->dn_tx_holds);
121 list_link_init(&dn->dn_link);
123 bzero(&dn->dn_next_nblkptr[0], sizeof (dn->dn_next_nblkptr));
124 bzero(&dn->dn_next_nlevels[0], sizeof (dn->dn_next_nlevels));
125 bzero(&dn->dn_next_indblkshift[0], sizeof (dn->dn_next_indblkshift));
126 bzero(&dn->dn_next_bonustype[0], sizeof (dn->dn_next_bonustype));
127 bzero(&dn->dn_rm_spillblk[0], sizeof (dn->dn_rm_spillblk));
128 bzero(&dn->dn_next_bonuslen[0], sizeof (dn->dn_next_bonuslen));
129 bzero(&dn->dn_next_blksz[0], sizeof (dn->dn_next_blksz));
131 for (i = 0; i < TXG_SIZE; i++) {
132 list_link_init(&dn->dn_dirty_link[i]);
133 dn->dn_free_ranges[i] = NULL;
134 list_create(&dn->dn_dirty_records[i],
135 sizeof (dbuf_dirty_record_t),
136 offsetof(dbuf_dirty_record_t, dr_dirty_node));
139 dn->dn_allocated_txg = 0;
141 dn->dn_assigned_txg = 0;
143 dn->dn_dirtyctx_firstset = NULL;
145 dn->dn_have_spill = B_FALSE;
155 dn->dn_dbufs_count = 0;
156 dn->dn_unlisted_l0_blkid = 0;
157 avl_create(&dn->dn_dbufs, dbuf_compare, sizeof (dmu_buf_impl_t),
158 offsetof(dmu_buf_impl_t, db_link));
161 POINTER_INVALIDATE(&dn->dn_objset);
167 dnode_dest(void *arg, void *unused)
172 rw_destroy(&dn->dn_struct_rwlock);
173 mutex_destroy(&dn->dn_mtx);
174 mutex_destroy(&dn->dn_dbufs_mtx);
175 cv_destroy(&dn->dn_notxholds);
176 refcount_destroy(&dn->dn_holds);
177 refcount_destroy(&dn->dn_tx_holds);
178 ASSERT(!list_link_active(&dn->dn_link));
180 for (i = 0; i < TXG_SIZE; i++) {
181 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
182 ASSERT3P(dn->dn_free_ranges[i], ==, NULL);
183 list_destroy(&dn->dn_dirty_records[i]);
184 ASSERT0(dn->dn_next_nblkptr[i]);
185 ASSERT0(dn->dn_next_nlevels[i]);
186 ASSERT0(dn->dn_next_indblkshift[i]);
187 ASSERT0(dn->dn_next_bonustype[i]);
188 ASSERT0(dn->dn_rm_spillblk[i]);
189 ASSERT0(dn->dn_next_bonuslen[i]);
190 ASSERT0(dn->dn_next_blksz[i]);
193 ASSERT0(dn->dn_allocated_txg);
194 ASSERT0(dn->dn_free_txg);
195 ASSERT0(dn->dn_assigned_txg);
196 ASSERT0(dn->dn_dirtyctx);
197 ASSERT3P(dn->dn_dirtyctx_firstset, ==, NULL);
198 ASSERT3P(dn->dn_bonus, ==, NULL);
199 ASSERT(!dn->dn_have_spill);
200 ASSERT3P(dn->dn_zio, ==, NULL);
201 ASSERT0(dn->dn_oldused);
202 ASSERT0(dn->dn_oldflags);
203 ASSERT0(dn->dn_olduid);
204 ASSERT0(dn->dn_oldgid);
205 ASSERT0(dn->dn_newuid);
206 ASSERT0(dn->dn_newgid);
207 ASSERT0(dn->dn_id_flags);
209 ASSERT0(dn->dn_dbufs_count);
210 ASSERT0(dn->dn_unlisted_l0_blkid);
211 avl_destroy(&dn->dn_dbufs);
217 ASSERT(dnode_cache == NULL);
218 dnode_cache = kmem_cache_create("dnode_t",
220 0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0);
221 kmem_cache_set_move(dnode_cache, dnode_move);
227 kmem_cache_destroy(dnode_cache);
234 dnode_verify(dnode_t *dn)
236 int drop_struct_lock = FALSE;
239 ASSERT(dn->dn_objset);
240 ASSERT(dn->dn_handle->dnh_dnode == dn);
242 ASSERT(DMU_OT_IS_VALID(dn->dn_phys->dn_type));
244 if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY))
247 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
248 rw_enter(&dn->dn_struct_rwlock, RW_READER);
249 drop_struct_lock = TRUE;
251 if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) {
253 ASSERT3U(dn->dn_indblkshift, >=, 0);
254 ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT);
255 if (dn->dn_datablkshift) {
256 ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT);
257 ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT);
258 ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz);
260 ASSERT3U(dn->dn_nlevels, <=, 30);
261 ASSERT(DMU_OT_IS_VALID(dn->dn_type));
262 ASSERT3U(dn->dn_nblkptr, >=, 1);
263 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
264 ASSERT3U(dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
265 ASSERT3U(dn->dn_datablksz, ==,
266 dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
267 ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0);
268 ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) +
269 dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
270 for (i = 0; i < TXG_SIZE; i++) {
271 ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels);
274 if (dn->dn_phys->dn_type != DMU_OT_NONE)
275 ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels);
276 ASSERT(DMU_OBJECT_IS_SPECIAL(dn->dn_object) || dn->dn_dbuf != NULL);
277 if (dn->dn_dbuf != NULL) {
278 ASSERT3P(dn->dn_phys, ==,
279 (dnode_phys_t *)dn->dn_dbuf->db.db_data +
280 (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT)));
282 if (drop_struct_lock)
283 rw_exit(&dn->dn_struct_rwlock);
288 dnode_byteswap(dnode_phys_t *dnp)
290 uint64_t *buf64 = (void*)&dnp->dn_blkptr;
293 if (dnp->dn_type == DMU_OT_NONE) {
294 bzero(dnp, sizeof (dnode_phys_t));
298 dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec);
299 dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen);
300 dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid);
301 dnp->dn_used = BSWAP_64(dnp->dn_used);
304 * dn_nblkptr is only one byte, so it's OK to read it in either
305 * byte order. We can't read dn_bouslen.
307 ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT);
308 ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR);
309 for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++)
310 buf64[i] = BSWAP_64(buf64[i]);
313 * OK to check dn_bonuslen for zero, because it won't matter if
314 * we have the wrong byte order. This is necessary because the
315 * dnode dnode is smaller than a regular dnode.
317 if (dnp->dn_bonuslen != 0) {
319 * Note that the bonus length calculated here may be
320 * longer than the actual bonus buffer. This is because
321 * we always put the bonus buffer after the last block
322 * pointer (instead of packing it against the end of the
325 int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t);
326 size_t len = DN_MAX_BONUSLEN - off;
327 ASSERT(DMU_OT_IS_VALID(dnp->dn_bonustype));
328 dmu_object_byteswap_t byteswap =
329 DMU_OT_BYTESWAP(dnp->dn_bonustype);
330 dmu_ot_byteswap[byteswap].ob_func(dnp->dn_bonus + off, len);
333 /* Swap SPILL block if we have one */
334 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)
335 byteswap_uint64_array(&dnp->dn_spill, sizeof (blkptr_t));
340 dnode_buf_byteswap(void *vbuf, size_t size)
342 dnode_phys_t *buf = vbuf;
345 ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT));
346 ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0);
348 size >>= DNODE_SHIFT;
349 for (i = 0; i < size; i++) {
356 dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx)
358 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
360 dnode_setdirty(dn, tx);
361 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
362 ASSERT3U(newsize, <=, DN_MAX_BONUSLEN -
363 (dn->dn_nblkptr-1) * sizeof (blkptr_t));
364 dn->dn_bonuslen = newsize;
366 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = DN_ZERO_BONUSLEN;
368 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
369 rw_exit(&dn->dn_struct_rwlock);
373 dnode_setbonus_type(dnode_t *dn, dmu_object_type_t newtype, dmu_tx_t *tx)
375 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
376 dnode_setdirty(dn, tx);
377 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
378 dn->dn_bonustype = newtype;
379 dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
380 rw_exit(&dn->dn_struct_rwlock);
384 dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx)
386 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
387 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
388 dnode_setdirty(dn, tx);
389 dn->dn_rm_spillblk[tx->tx_txg&TXG_MASK] = DN_KILL_SPILLBLK;
390 dn->dn_have_spill = B_FALSE;
394 dnode_setdblksz(dnode_t *dn, int size)
396 ASSERT0(P2PHASE(size, SPA_MINBLOCKSIZE));
397 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
398 ASSERT3U(size, >=, SPA_MINBLOCKSIZE);
399 ASSERT3U(size >> SPA_MINBLOCKSHIFT, <,
400 1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8));
401 dn->dn_datablksz = size;
402 dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT;
403 dn->dn_datablkshift = ISP2(size) ? highbit64(size - 1) : 0;
407 dnode_create(objset_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db,
408 uint64_t object, dnode_handle_t *dnh)
410 dnode_t *dn = kmem_cache_alloc(dnode_cache, KM_SLEEP);
412 ASSERT(!POINTER_IS_VALID(dn->dn_objset));
416 * Defer setting dn_objset until the dnode is ready to be a candidate
417 * for the dnode_move() callback.
419 dn->dn_object = object;
424 if (dnp->dn_datablkszsec) {
425 dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
427 dn->dn_datablksz = 0;
428 dn->dn_datablkszsec = 0;
429 dn->dn_datablkshift = 0;
431 dn->dn_indblkshift = dnp->dn_indblkshift;
432 dn->dn_nlevels = dnp->dn_nlevels;
433 dn->dn_type = dnp->dn_type;
434 dn->dn_nblkptr = dnp->dn_nblkptr;
435 dn->dn_checksum = dnp->dn_checksum;
436 dn->dn_compress = dnp->dn_compress;
437 dn->dn_bonustype = dnp->dn_bonustype;
438 dn->dn_bonuslen = dnp->dn_bonuslen;
439 dn->dn_maxblkid = dnp->dn_maxblkid;
440 dn->dn_have_spill = ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0);
443 dmu_zfetch_init(&dn->dn_zfetch, dn);
445 ASSERT(DMU_OT_IS_VALID(dn->dn_phys->dn_type));
447 mutex_enter(&os->os_lock);
448 list_insert_head(&os->os_dnodes, dn);
451 * Everything else must be valid before assigning dn_objset makes the
452 * dnode eligible for dnode_move().
455 mutex_exit(&os->os_lock);
457 arc_space_consume(sizeof (dnode_t), ARC_SPACE_OTHER);
462 * Caller must be holding the dnode handle, which is released upon return.
465 dnode_destroy(dnode_t *dn)
467 objset_t *os = dn->dn_objset;
469 ASSERT((dn->dn_id_flags & DN_ID_NEW_EXIST) == 0);
471 mutex_enter(&os->os_lock);
472 POINTER_INVALIDATE(&dn->dn_objset);
473 list_remove(&os->os_dnodes, dn);
474 mutex_exit(&os->os_lock);
476 /* the dnode can no longer move, so we can release the handle */
477 zrl_remove(&dn->dn_handle->dnh_zrlock);
479 dn->dn_allocated_txg = 0;
481 dn->dn_assigned_txg = 0;
484 if (dn->dn_dirtyctx_firstset != NULL) {
485 kmem_free(dn->dn_dirtyctx_firstset, 1);
486 dn->dn_dirtyctx_firstset = NULL;
488 if (dn->dn_bonus != NULL) {
489 mutex_enter(&dn->dn_bonus->db_mtx);
490 dbuf_evict(dn->dn_bonus);
495 dn->dn_have_spill = B_FALSE;
503 dn->dn_unlisted_l0_blkid = 0;
505 dmu_zfetch_rele(&dn->dn_zfetch);
506 kmem_cache_free(dnode_cache, dn);
507 arc_space_return(sizeof (dnode_t), ARC_SPACE_OTHER);
511 dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
512 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
517 blocksize = 1 << zfs_default_bs;
518 else if (blocksize > SPA_MAXBLOCKSIZE)
519 blocksize = SPA_MAXBLOCKSIZE;
521 blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE);
524 ibs = zfs_default_ibs;
526 ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT);
528 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset,
529 dn->dn_object, tx->tx_txg, blocksize, ibs);
531 ASSERT(dn->dn_type == DMU_OT_NONE);
532 ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0);
533 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE);
534 ASSERT(ot != DMU_OT_NONE);
535 ASSERT(DMU_OT_IS_VALID(ot));
536 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
537 (bonustype == DMU_OT_SA && bonuslen == 0) ||
538 (bonustype != DMU_OT_NONE && bonuslen != 0));
539 ASSERT(DMU_OT_IS_VALID(bonustype));
540 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
541 ASSERT(dn->dn_type == DMU_OT_NONE);
542 ASSERT0(dn->dn_maxblkid);
543 ASSERT0(dn->dn_allocated_txg);
544 ASSERT0(dn->dn_assigned_txg);
545 ASSERT(refcount_is_zero(&dn->dn_tx_holds));
546 ASSERT3U(refcount_count(&dn->dn_holds), <=, 1);
547 ASSERT(avl_is_empty(&dn->dn_dbufs));
549 for (i = 0; i < TXG_SIZE; i++) {
550 ASSERT0(dn->dn_next_nblkptr[i]);
551 ASSERT0(dn->dn_next_nlevels[i]);
552 ASSERT0(dn->dn_next_indblkshift[i]);
553 ASSERT0(dn->dn_next_bonuslen[i]);
554 ASSERT0(dn->dn_next_bonustype[i]);
555 ASSERT0(dn->dn_rm_spillblk[i]);
556 ASSERT0(dn->dn_next_blksz[i]);
557 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
558 ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL);
559 ASSERT3P(dn->dn_free_ranges[i], ==, NULL);
563 dnode_setdblksz(dn, blocksize);
564 dn->dn_indblkshift = ibs;
566 if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
570 ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
571 dn->dn_bonustype = bonustype;
572 dn->dn_bonuslen = bonuslen;
573 dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
574 dn->dn_compress = ZIO_COMPRESS_INHERIT;
578 if (dn->dn_dirtyctx_firstset) {
579 kmem_free(dn->dn_dirtyctx_firstset, 1);
580 dn->dn_dirtyctx_firstset = NULL;
583 dn->dn_allocated_txg = tx->tx_txg;
586 dnode_setdirty(dn, tx);
587 dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs;
588 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
589 dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
590 dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz;
594 dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
595 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
599 ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE);
600 ASSERT3U(blocksize, <=, SPA_MAXBLOCKSIZE);
601 ASSERT0(blocksize % SPA_MINBLOCKSIZE);
602 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
603 ASSERT(tx->tx_txg != 0);
604 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
605 (bonustype != DMU_OT_NONE && bonuslen != 0) ||
606 (bonustype == DMU_OT_SA && bonuslen == 0));
607 ASSERT(DMU_OT_IS_VALID(bonustype));
608 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
610 /* clean up any unreferenced dbufs */
611 dnode_evict_dbufs(dn);
615 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
616 dnode_setdirty(dn, tx);
617 if (dn->dn_datablksz != blocksize) {
618 /* change blocksize */
619 ASSERT(dn->dn_maxblkid == 0 &&
620 (BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) ||
621 dnode_block_freed(dn, 0)));
622 dnode_setdblksz(dn, blocksize);
623 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize;
625 if (dn->dn_bonuslen != bonuslen)
626 dn->dn_next_bonuslen[tx->tx_txg&TXG_MASK] = bonuslen;
628 if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
631 nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
632 if (dn->dn_bonustype != bonustype)
633 dn->dn_next_bonustype[tx->tx_txg&TXG_MASK] = bonustype;
634 if (dn->dn_nblkptr != nblkptr)
635 dn->dn_next_nblkptr[tx->tx_txg&TXG_MASK] = nblkptr;
636 if (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
637 dbuf_rm_spill(dn, tx);
638 dnode_rm_spill(dn, tx);
640 rw_exit(&dn->dn_struct_rwlock);
645 /* change bonus size and type */
646 mutex_enter(&dn->dn_mtx);
647 dn->dn_bonustype = bonustype;
648 dn->dn_bonuslen = bonuslen;
649 dn->dn_nblkptr = nblkptr;
650 dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
651 dn->dn_compress = ZIO_COMPRESS_INHERIT;
652 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
654 /* fix up the bonus db_size */
656 dn->dn_bonus->db.db_size =
657 DN_MAX_BONUSLEN - (dn->dn_nblkptr-1) * sizeof (blkptr_t);
658 ASSERT(dn->dn_bonuslen <= dn->dn_bonus->db.db_size);
661 dn->dn_allocated_txg = tx->tx_txg;
662 mutex_exit(&dn->dn_mtx);
667 uint64_t dms_dnode_invalid;
668 uint64_t dms_dnode_recheck1;
669 uint64_t dms_dnode_recheck2;
670 uint64_t dms_dnode_special;
671 uint64_t dms_dnode_handle;
672 uint64_t dms_dnode_rwlock;
673 uint64_t dms_dnode_active;
675 #endif /* DNODE_STATS */
678 dnode_move_impl(dnode_t *odn, dnode_t *ndn)
682 ASSERT(!RW_LOCK_HELD(&odn->dn_struct_rwlock));
683 ASSERT(MUTEX_NOT_HELD(&odn->dn_mtx));
684 ASSERT(MUTEX_NOT_HELD(&odn->dn_dbufs_mtx));
685 ASSERT(!RW_LOCK_HELD(&odn->dn_zfetch.zf_rwlock));
688 ndn->dn_objset = odn->dn_objset;
689 ndn->dn_object = odn->dn_object;
690 ndn->dn_dbuf = odn->dn_dbuf;
691 ndn->dn_handle = odn->dn_handle;
692 ndn->dn_phys = odn->dn_phys;
693 ndn->dn_type = odn->dn_type;
694 ndn->dn_bonuslen = odn->dn_bonuslen;
695 ndn->dn_bonustype = odn->dn_bonustype;
696 ndn->dn_nblkptr = odn->dn_nblkptr;
697 ndn->dn_checksum = odn->dn_checksum;
698 ndn->dn_compress = odn->dn_compress;
699 ndn->dn_nlevels = odn->dn_nlevels;
700 ndn->dn_indblkshift = odn->dn_indblkshift;
701 ndn->dn_datablkshift = odn->dn_datablkshift;
702 ndn->dn_datablkszsec = odn->dn_datablkszsec;
703 ndn->dn_datablksz = odn->dn_datablksz;
704 ndn->dn_maxblkid = odn->dn_maxblkid;
705 bcopy(&odn->dn_next_nblkptr[0], &ndn->dn_next_nblkptr[0],
706 sizeof (odn->dn_next_nblkptr));
707 bcopy(&odn->dn_next_nlevels[0], &ndn->dn_next_nlevels[0],
708 sizeof (odn->dn_next_nlevels));
709 bcopy(&odn->dn_next_indblkshift[0], &ndn->dn_next_indblkshift[0],
710 sizeof (odn->dn_next_indblkshift));
711 bcopy(&odn->dn_next_bonustype[0], &ndn->dn_next_bonustype[0],
712 sizeof (odn->dn_next_bonustype));
713 bcopy(&odn->dn_rm_spillblk[0], &ndn->dn_rm_spillblk[0],
714 sizeof (odn->dn_rm_spillblk));
715 bcopy(&odn->dn_next_bonuslen[0], &ndn->dn_next_bonuslen[0],
716 sizeof (odn->dn_next_bonuslen));
717 bcopy(&odn->dn_next_blksz[0], &ndn->dn_next_blksz[0],
718 sizeof (odn->dn_next_blksz));
719 for (i = 0; i < TXG_SIZE; i++) {
720 list_move_tail(&ndn->dn_dirty_records[i],
721 &odn->dn_dirty_records[i]);
723 bcopy(&odn->dn_free_ranges[0], &ndn->dn_free_ranges[0],
724 sizeof (odn->dn_free_ranges));
725 ndn->dn_allocated_txg = odn->dn_allocated_txg;
726 ndn->dn_free_txg = odn->dn_free_txg;
727 ndn->dn_assigned_txg = odn->dn_assigned_txg;
728 ndn->dn_dirtyctx = odn->dn_dirtyctx;
729 ndn->dn_dirtyctx_firstset = odn->dn_dirtyctx_firstset;
730 ASSERT(refcount_count(&odn->dn_tx_holds) == 0);
731 refcount_transfer(&ndn->dn_holds, &odn->dn_holds);
732 ASSERT(avl_is_empty(&ndn->dn_dbufs));
733 avl_swap(&ndn->dn_dbufs, &odn->dn_dbufs);
734 ndn->dn_dbufs_count = odn->dn_dbufs_count;
735 ndn->dn_unlisted_l0_blkid = odn->dn_unlisted_l0_blkid;
736 ndn->dn_bonus = odn->dn_bonus;
737 ndn->dn_have_spill = odn->dn_have_spill;
738 ndn->dn_zio = odn->dn_zio;
739 ndn->dn_oldused = odn->dn_oldused;
740 ndn->dn_oldflags = odn->dn_oldflags;
741 ndn->dn_olduid = odn->dn_olduid;
742 ndn->dn_oldgid = odn->dn_oldgid;
743 ndn->dn_newuid = odn->dn_newuid;
744 ndn->dn_newgid = odn->dn_newgid;
745 ndn->dn_id_flags = odn->dn_id_flags;
746 dmu_zfetch_init(&ndn->dn_zfetch, NULL);
747 list_move_tail(&ndn->dn_zfetch.zf_stream, &odn->dn_zfetch.zf_stream);
748 ndn->dn_zfetch.zf_dnode = odn->dn_zfetch.zf_dnode;
749 ndn->dn_zfetch.zf_stream_cnt = odn->dn_zfetch.zf_stream_cnt;
750 ndn->dn_zfetch.zf_alloc_fail = odn->dn_zfetch.zf_alloc_fail;
753 * Update back pointers. Updating the handle fixes the back pointer of
754 * every descendant dbuf as well as the bonus dbuf.
756 ASSERT(ndn->dn_handle->dnh_dnode == odn);
757 ndn->dn_handle->dnh_dnode = ndn;
758 if (ndn->dn_zfetch.zf_dnode == odn) {
759 ndn->dn_zfetch.zf_dnode = ndn;
763 * Invalidate the original dnode by clearing all of its back pointers.
766 odn->dn_handle = NULL;
767 avl_create(&odn->dn_dbufs, dbuf_compare, sizeof (dmu_buf_impl_t),
768 offsetof(dmu_buf_impl_t, db_link));
769 odn->dn_dbufs_count = 0;
770 odn->dn_unlisted_l0_blkid = 0;
771 odn->dn_bonus = NULL;
772 odn->dn_zfetch.zf_dnode = NULL;
775 * Set the low bit of the objset pointer to ensure that dnode_move()
776 * recognizes the dnode as invalid in any subsequent callback.
778 POINTER_INVALIDATE(&odn->dn_objset);
781 * Satisfy the destructor.
783 for (i = 0; i < TXG_SIZE; i++) {
784 list_create(&odn->dn_dirty_records[i],
785 sizeof (dbuf_dirty_record_t),
786 offsetof(dbuf_dirty_record_t, dr_dirty_node));
787 odn->dn_free_ranges[i] = NULL;
788 odn->dn_next_nlevels[i] = 0;
789 odn->dn_next_indblkshift[i] = 0;
790 odn->dn_next_bonustype[i] = 0;
791 odn->dn_rm_spillblk[i] = 0;
792 odn->dn_next_bonuslen[i] = 0;
793 odn->dn_next_blksz[i] = 0;
795 odn->dn_allocated_txg = 0;
796 odn->dn_free_txg = 0;
797 odn->dn_assigned_txg = 0;
798 odn->dn_dirtyctx = 0;
799 odn->dn_dirtyctx_firstset = NULL;
800 odn->dn_have_spill = B_FALSE;
803 odn->dn_oldflags = 0;
808 odn->dn_id_flags = 0;
814 odn->dn_moved = (uint8_t)-1;
821 dnode_move(void *buf, void *newbuf, size_t size, void *arg)
823 dnode_t *odn = buf, *ndn = newbuf;
829 * The dnode is on the objset's list of known dnodes if the objset
830 * pointer is valid. We set the low bit of the objset pointer when
831 * freeing the dnode to invalidate it, and the memory patterns written
832 * by kmem (baddcafe and deadbeef) set at least one of the two low bits.
833 * A newly created dnode sets the objset pointer last of all to indicate
834 * that the dnode is known and in a valid state to be moved by this
838 if (!POINTER_IS_VALID(os)) {
839 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_invalid);
840 return (KMEM_CBRC_DONT_KNOW);
844 * Ensure that the objset does not go away during the move.
846 rw_enter(&os_lock, RW_WRITER);
847 if (os != odn->dn_objset) {
849 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_recheck1);
850 return (KMEM_CBRC_DONT_KNOW);
854 * If the dnode is still valid, then so is the objset. We know that no
855 * valid objset can be freed while we hold os_lock, so we can safely
856 * ensure that the objset remains in use.
858 mutex_enter(&os->os_lock);
861 * Recheck the objset pointer in case the dnode was removed just before
862 * acquiring the lock.
864 if (os != odn->dn_objset) {
865 mutex_exit(&os->os_lock);
867 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_recheck2);
868 return (KMEM_CBRC_DONT_KNOW);
872 * At this point we know that as long as we hold os->os_lock, the dnode
873 * cannot be freed and fields within the dnode can be safely accessed.
874 * The objset listing this dnode cannot go away as long as this dnode is
878 if (DMU_OBJECT_IS_SPECIAL(odn->dn_object)) {
879 mutex_exit(&os->os_lock);
880 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_special);
881 return (KMEM_CBRC_NO);
883 ASSERT(odn->dn_dbuf != NULL); /* only "special" dnodes have no parent */
886 * Lock the dnode handle to prevent the dnode from obtaining any new
887 * holds. This also prevents the descendant dbufs and the bonus dbuf
888 * from accessing the dnode, so that we can discount their holds. The
889 * handle is safe to access because we know that while the dnode cannot
890 * go away, neither can its handle. Once we hold dnh_zrlock, we can
891 * safely move any dnode referenced only by dbufs.
893 if (!zrl_tryenter(&odn->dn_handle->dnh_zrlock)) {
894 mutex_exit(&os->os_lock);
895 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_handle);
896 return (KMEM_CBRC_LATER);
900 * Ensure a consistent view of the dnode's holds and the dnode's dbufs.
901 * We need to guarantee that there is a hold for every dbuf in order to
902 * determine whether the dnode is actively referenced. Falsely matching
903 * a dbuf to an active hold would lead to an unsafe move. It's possible
904 * that a thread already having an active dnode hold is about to add a
905 * dbuf, and we can't compare hold and dbuf counts while the add is in
908 if (!rw_tryenter(&odn->dn_struct_rwlock, RW_WRITER)) {
909 zrl_exit(&odn->dn_handle->dnh_zrlock);
910 mutex_exit(&os->os_lock);
911 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_rwlock);
912 return (KMEM_CBRC_LATER);
916 * A dbuf may be removed (evicted) without an active dnode hold. In that
917 * case, the dbuf count is decremented under the handle lock before the
918 * dbuf's hold is released. This order ensures that if we count the hold
919 * after the dbuf is removed but before its hold is released, we will
920 * treat the unmatched hold as active and exit safely. If we count the
921 * hold before the dbuf is removed, the hold is discounted, and the
922 * removal is blocked until the move completes.
924 refcount = refcount_count(&odn->dn_holds);
925 ASSERT(refcount >= 0);
926 dbufs = odn->dn_dbufs_count;
928 /* We can't have more dbufs than dnode holds. */
929 ASSERT3U(dbufs, <=, refcount);
930 DTRACE_PROBE3(dnode__move, dnode_t *, odn, int64_t, refcount,
933 if (refcount > dbufs) {
934 rw_exit(&odn->dn_struct_rwlock);
935 zrl_exit(&odn->dn_handle->dnh_zrlock);
936 mutex_exit(&os->os_lock);
937 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_active);
938 return (KMEM_CBRC_LATER);
941 rw_exit(&odn->dn_struct_rwlock);
944 * At this point we know that anyone with a hold on the dnode is not
945 * actively referencing it. The dnode is known and in a valid state to
946 * move. We're holding the locks needed to execute the critical section.
948 dnode_move_impl(odn, ndn);
950 list_link_replace(&odn->dn_link, &ndn->dn_link);
951 /* If the dnode was safe to move, the refcount cannot have changed. */
952 ASSERT(refcount == refcount_count(&ndn->dn_holds));
953 ASSERT(dbufs == ndn->dn_dbufs_count);
954 zrl_exit(&ndn->dn_handle->dnh_zrlock); /* handle has moved */
955 mutex_exit(&os->os_lock);
957 return (KMEM_CBRC_YES);
963 dnode_special_close(dnode_handle_t *dnh)
965 dnode_t *dn = dnh->dnh_dnode;
968 * Wait for final references to the dnode to clear. This can
969 * only happen if the arc is asyncronously evicting state that
970 * has a hold on this dnode while we are trying to evict this
973 while (refcount_count(&dn->dn_holds) > 0)
975 zrl_add(&dnh->dnh_zrlock);
976 dnode_destroy(dn); /* implicit zrl_remove() */
977 zrl_destroy(&dnh->dnh_zrlock);
978 dnh->dnh_dnode = NULL;
982 dnode_special_open(objset_t *os, dnode_phys_t *dnp, uint64_t object,
985 dnode_t *dn = dnode_create(os, dnp, NULL, object, dnh);
987 zrl_init(&dnh->dnh_zrlock);
993 dnode_buf_pageout(dmu_buf_t *db, void *arg)
995 dnode_children_t *children_dnodes = arg;
997 int epb = db->db_size >> DNODE_SHIFT;
999 ASSERT(epb == children_dnodes->dnc_count);
1001 for (i = 0; i < epb; i++) {
1002 dnode_handle_t *dnh = &children_dnodes->dnc_children[i];
1006 * The dnode handle lock guards against the dnode moving to
1007 * another valid address, so there is no need here to guard
1008 * against changes to or from NULL.
1010 if (dnh->dnh_dnode == NULL) {
1011 zrl_destroy(&dnh->dnh_zrlock);
1015 zrl_add(&dnh->dnh_zrlock);
1016 dn = dnh->dnh_dnode;
1018 * If there are holds on this dnode, then there should
1019 * be holds on the dnode's containing dbuf as well; thus
1020 * it wouldn't be eligible for eviction and this function
1021 * would not have been called.
1023 ASSERT(refcount_is_zero(&dn->dn_holds));
1024 ASSERT(refcount_is_zero(&dn->dn_tx_holds));
1026 dnode_destroy(dn); /* implicit zrl_remove() */
1027 zrl_destroy(&dnh->dnh_zrlock);
1028 dnh->dnh_dnode = NULL;
1030 kmem_free(children_dnodes, sizeof (dnode_children_t) +
1031 epb * sizeof (dnode_handle_t));
1036 * EINVAL - invalid object number.
1038 * succeeds even for free dnodes.
1041 dnode_hold_impl(objset_t *os, uint64_t object, int flag,
1042 void *tag, dnode_t **dnp)
1045 int drop_struct_lock = FALSE;
1050 dnode_children_t *children_dnodes;
1051 dnode_handle_t *dnh;
1054 * If you are holding the spa config lock as writer, you shouldn't
1055 * be asking the DMU to do *anything* unless it's the root pool
1056 * which may require us to read from the root filesystem while
1057 * holding some (not all) of the locks as writer.
1059 ASSERT(spa_config_held(os->os_spa, SCL_ALL, RW_WRITER) == 0 ||
1060 (spa_is_root(os->os_spa) &&
1061 spa_config_held(os->os_spa, SCL_STATE, RW_WRITER)));
1063 if (object == DMU_USERUSED_OBJECT || object == DMU_GROUPUSED_OBJECT) {
1064 dn = (object == DMU_USERUSED_OBJECT) ?
1065 DMU_USERUSED_DNODE(os) : DMU_GROUPUSED_DNODE(os);
1067 return (SET_ERROR(ENOENT));
1069 if ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE)
1070 return (SET_ERROR(ENOENT));
1071 if ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)
1072 return (SET_ERROR(EEXIST));
1074 (void) refcount_add(&dn->dn_holds, tag);
1079 if (object == 0 || object >= DN_MAX_OBJECT)
1080 return (SET_ERROR(EINVAL));
1082 mdn = DMU_META_DNODE(os);
1083 ASSERT(mdn->dn_object == DMU_META_DNODE_OBJECT);
1087 if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) {
1088 rw_enter(&mdn->dn_struct_rwlock, RW_READER);
1089 drop_struct_lock = TRUE;
1092 blk = dbuf_whichblock(mdn, object * sizeof (dnode_phys_t));
1094 db = dbuf_hold(mdn, blk, FTAG);
1095 if (drop_struct_lock)
1096 rw_exit(&mdn->dn_struct_rwlock);
1098 return (SET_ERROR(EIO));
1099 err = dbuf_read(db, NULL, DB_RF_CANFAIL);
1101 dbuf_rele(db, FTAG);
1105 ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT);
1106 epb = db->db.db_size >> DNODE_SHIFT;
1108 idx = object & (epb-1);
1110 ASSERT(DB_DNODE(db)->dn_type == DMU_OT_DNODE);
1111 children_dnodes = dmu_buf_get_user(&db->db);
1112 if (children_dnodes == NULL) {
1114 dnode_children_t *winner;
1115 children_dnodes = kmem_zalloc(sizeof (dnode_children_t) +
1116 epb * sizeof (dnode_handle_t), KM_SLEEP);
1117 children_dnodes->dnc_count = epb;
1118 dnh = &children_dnodes->dnc_children[0];
1119 for (i = 0; i < epb; i++) {
1120 zrl_init(&dnh[i].dnh_zrlock);
1121 dnh[i].dnh_dnode = NULL;
1123 if (winner = dmu_buf_set_user(&db->db, children_dnodes, NULL,
1124 dnode_buf_pageout)) {
1126 for (i = 0; i < epb; i++) {
1127 zrl_destroy(&dnh[i].dnh_zrlock);
1130 kmem_free(children_dnodes, sizeof (dnode_children_t) +
1131 epb * sizeof (dnode_handle_t));
1132 children_dnodes = winner;
1135 ASSERT(children_dnodes->dnc_count == epb);
1137 dnh = &children_dnodes->dnc_children[idx];
1138 zrl_add(&dnh->dnh_zrlock);
1139 if ((dn = dnh->dnh_dnode) == NULL) {
1140 dnode_phys_t *phys = (dnode_phys_t *)db->db.db_data+idx;
1143 dn = dnode_create(os, phys, db, object, dnh);
1144 winner = atomic_cas_ptr(&dnh->dnh_dnode, NULL, dn);
1145 if (winner != NULL) {
1146 zrl_add(&dnh->dnh_zrlock);
1147 dnode_destroy(dn); /* implicit zrl_remove() */
1152 mutex_enter(&dn->dn_mtx);
1154 if (dn->dn_free_txg ||
1155 ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) ||
1156 ((flag & DNODE_MUST_BE_FREE) &&
1157 (type != DMU_OT_NONE || !refcount_is_zero(&dn->dn_holds)))) {
1158 mutex_exit(&dn->dn_mtx);
1159 zrl_remove(&dnh->dnh_zrlock);
1160 dbuf_rele(db, FTAG);
1161 return (type == DMU_OT_NONE ? ENOENT : EEXIST);
1163 mutex_exit(&dn->dn_mtx);
1165 if (refcount_add(&dn->dn_holds, tag) == 1)
1166 dbuf_add_ref(db, dnh);
1167 /* Now we can rely on the hold to prevent the dnode from moving. */
1168 zrl_remove(&dnh->dnh_zrlock);
1171 ASSERT3P(dn->dn_dbuf, ==, db);
1172 ASSERT3U(dn->dn_object, ==, object);
1173 dbuf_rele(db, FTAG);
1180 * Return held dnode if the object is allocated, NULL if not.
1183 dnode_hold(objset_t *os, uint64_t object, void *tag, dnode_t **dnp)
1185 return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, tag, dnp));
1189 * Can only add a reference if there is already at least one
1190 * reference on the dnode. Returns FALSE if unable to add a
1194 dnode_add_ref(dnode_t *dn, void *tag)
1196 mutex_enter(&dn->dn_mtx);
1197 if (refcount_is_zero(&dn->dn_holds)) {
1198 mutex_exit(&dn->dn_mtx);
1201 VERIFY(1 < refcount_add(&dn->dn_holds, tag));
1202 mutex_exit(&dn->dn_mtx);
1207 dnode_rele(dnode_t *dn, void *tag)
1210 /* Get while the hold prevents the dnode from moving. */
1211 dmu_buf_impl_t *db = dn->dn_dbuf;
1212 dnode_handle_t *dnh = dn->dn_handle;
1214 mutex_enter(&dn->dn_mtx);
1215 refs = refcount_remove(&dn->dn_holds, tag);
1216 mutex_exit(&dn->dn_mtx);
1219 * It's unsafe to release the last hold on a dnode by dnode_rele() or
1220 * indirectly by dbuf_rele() while relying on the dnode handle to
1221 * prevent the dnode from moving, since releasing the last hold could
1222 * result in the dnode's parent dbuf evicting its dnode handles. For
1223 * that reason anyone calling dnode_rele() or dbuf_rele() without some
1224 * other direct or indirect hold on the dnode must first drop the dnode
1227 ASSERT(refs > 0 || dnh->dnh_zrlock.zr_owner != curthread);
1229 /* NOTE: the DNODE_DNODE does not have a dn_dbuf */
1230 if (refs == 0 && db != NULL) {
1232 * Another thread could add a hold to the dnode handle in
1233 * dnode_hold_impl() while holding the parent dbuf. Since the
1234 * hold on the parent dbuf prevents the handle from being
1235 * destroyed, the hold on the handle is OK. We can't yet assert
1236 * that the handle has zero references, but that will be
1237 * asserted anyway when the handle gets destroyed.
1244 dnode_setdirty(dnode_t *dn, dmu_tx_t *tx)
1246 objset_t *os = dn->dn_objset;
1247 uint64_t txg = tx->tx_txg;
1249 if (DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
1250 dsl_dataset_dirty(os->os_dsl_dataset, tx);
1257 mutex_enter(&dn->dn_mtx);
1258 ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg);
1259 ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg);
1260 mutex_exit(&dn->dn_mtx);
1264 * Determine old uid/gid when necessary
1266 dmu_objset_userquota_get_ids(dn, B_TRUE, tx);
1268 mutex_enter(&os->os_lock);
1271 * If we are already marked dirty, we're done.
1273 if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) {
1274 mutex_exit(&os->os_lock);
1278 ASSERT(!refcount_is_zero(&dn->dn_holds) ||
1279 !avl_is_empty(&dn->dn_dbufs));
1280 ASSERT(dn->dn_datablksz != 0);
1281 ASSERT0(dn->dn_next_bonuslen[txg&TXG_MASK]);
1282 ASSERT0(dn->dn_next_blksz[txg&TXG_MASK]);
1283 ASSERT0(dn->dn_next_bonustype[txg&TXG_MASK]);
1285 dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
1286 dn->dn_object, txg);
1288 if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) {
1289 list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn);
1291 list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn);
1294 mutex_exit(&os->os_lock);
1297 * The dnode maintains a hold on its containing dbuf as
1298 * long as there are holds on it. Each instantiated child
1299 * dbuf maintains a hold on the dnode. When the last child
1300 * drops its hold, the dnode will drop its hold on the
1301 * containing dbuf. We add a "dirty hold" here so that the
1302 * dnode will hang around after we finish processing its
1305 VERIFY(dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg));
1307 (void) dbuf_dirty(dn->dn_dbuf, tx);
1309 dsl_dataset_dirty(os->os_dsl_dataset, tx);
1313 dnode_free(dnode_t *dn, dmu_tx_t *tx)
1315 int txgoff = tx->tx_txg & TXG_MASK;
1317 dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg);
1319 /* we should be the only holder... hopefully */
1320 /* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */
1322 mutex_enter(&dn->dn_mtx);
1323 if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) {
1324 mutex_exit(&dn->dn_mtx);
1327 dn->dn_free_txg = tx->tx_txg;
1328 mutex_exit(&dn->dn_mtx);
1331 * If the dnode is already dirty, it needs to be moved from
1332 * the dirty list to the free list.
1334 mutex_enter(&dn->dn_objset->os_lock);
1335 if (list_link_active(&dn->dn_dirty_link[txgoff])) {
1336 list_remove(&dn->dn_objset->os_dirty_dnodes[txgoff], dn);
1337 list_insert_tail(&dn->dn_objset->os_free_dnodes[txgoff], dn);
1338 mutex_exit(&dn->dn_objset->os_lock);
1340 mutex_exit(&dn->dn_objset->os_lock);
1341 dnode_setdirty(dn, tx);
1346 * Try to change the block size for the indicated dnode. This can only
1347 * succeed if there are no blocks allocated or dirty beyond first block
1350 dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx)
1356 size = SPA_MINBLOCKSIZE;
1357 if (size > SPA_MAXBLOCKSIZE)
1358 size = SPA_MAXBLOCKSIZE;
1360 size = P2ROUNDUP(size, SPA_MINBLOCKSIZE);
1362 if (ibs == dn->dn_indblkshift)
1365 if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0)
1368 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1370 /* Check for any allocated blocks beyond the first */
1371 if (dn->dn_maxblkid != 0)
1374 mutex_enter(&dn->dn_dbufs_mtx);
1375 for (db = avl_first(&dn->dn_dbufs); db != NULL;
1376 db = AVL_NEXT(&dn->dn_dbufs, db)) {
1377 if (db->db_blkid != 0 && db->db_blkid != DMU_BONUS_BLKID &&
1378 db->db_blkid != DMU_SPILL_BLKID) {
1379 mutex_exit(&dn->dn_dbufs_mtx);
1383 mutex_exit(&dn->dn_dbufs_mtx);
1385 if (ibs && dn->dn_nlevels != 1)
1388 /* resize the old block */
1389 err = dbuf_hold_impl(dn, 0, 0, TRUE, FTAG, &db);
1391 dbuf_new_size(db, size, tx);
1392 else if (err != ENOENT)
1395 dnode_setdblksz(dn, size);
1396 dnode_setdirty(dn, tx);
1397 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size;
1399 dn->dn_indblkshift = ibs;
1400 dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs;
1402 /* rele after we have fixed the blocksize in the dnode */
1404 dbuf_rele(db, FTAG);
1406 rw_exit(&dn->dn_struct_rwlock);
1410 rw_exit(&dn->dn_struct_rwlock);
1411 return (SET_ERROR(ENOTSUP));
1414 /* read-holding callers must not rely on the lock being continuously held */
1416 dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t have_read)
1418 uint64_t txgoff = tx->tx_txg & TXG_MASK;
1419 int epbs, new_nlevels;
1422 ASSERT(blkid != DMU_BONUS_BLKID);
1425 RW_READ_HELD(&dn->dn_struct_rwlock) :
1426 RW_WRITE_HELD(&dn->dn_struct_rwlock));
1429 * if we have a read-lock, check to see if we need to do any work
1430 * before upgrading to a write-lock.
1433 if (blkid <= dn->dn_maxblkid)
1436 if (!rw_tryupgrade(&dn->dn_struct_rwlock)) {
1437 rw_exit(&dn->dn_struct_rwlock);
1438 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1442 if (blkid <= dn->dn_maxblkid)
1445 dn->dn_maxblkid = blkid;
1448 * Compute the number of levels necessary to support the new maxblkid.
1451 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1452 for (sz = dn->dn_nblkptr;
1453 sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs)
1456 if (new_nlevels > dn->dn_nlevels) {
1457 int old_nlevels = dn->dn_nlevels;
1460 dbuf_dirty_record_t *new, *dr, *dr_next;
1462 dn->dn_nlevels = new_nlevels;
1464 ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]);
1465 dn->dn_next_nlevels[txgoff] = new_nlevels;
1467 /* dirty the left indirects */
1468 db = dbuf_hold_level(dn, old_nlevels, 0, FTAG);
1470 new = dbuf_dirty(db, tx);
1471 dbuf_rele(db, FTAG);
1473 /* transfer the dirty records to the new indirect */
1474 mutex_enter(&dn->dn_mtx);
1475 mutex_enter(&new->dt.di.dr_mtx);
1476 list = &dn->dn_dirty_records[txgoff];
1477 for (dr = list_head(list); dr; dr = dr_next) {
1478 dr_next = list_next(&dn->dn_dirty_records[txgoff], dr);
1479 if (dr->dr_dbuf->db_level != new_nlevels-1 &&
1480 dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
1481 dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
1482 ASSERT(dr->dr_dbuf->db_level == old_nlevels-1);
1483 list_remove(&dn->dn_dirty_records[txgoff], dr);
1484 list_insert_tail(&new->dt.di.dr_children, dr);
1485 dr->dr_parent = new;
1488 mutex_exit(&new->dt.di.dr_mtx);
1489 mutex_exit(&dn->dn_mtx);
1494 rw_downgrade(&dn->dn_struct_rwlock);
1498 dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx)
1501 uint64_t blkoff, blkid, nblks;
1502 int blksz, blkshift, head, tail;
1506 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1507 blksz = dn->dn_datablksz;
1508 blkshift = dn->dn_datablkshift;
1509 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1511 if (len == DMU_OBJECT_END) {
1512 len = UINT64_MAX - off;
1517 * First, block align the region to free:
1520 head = P2NPHASE(off, blksz);
1521 blkoff = P2PHASE(off, blksz);
1522 if ((off >> blkshift) > dn->dn_maxblkid)
1525 ASSERT(dn->dn_maxblkid == 0);
1526 if (off == 0 && len >= blksz) {
1528 * Freeing the whole block; fast-track this request.
1529 * Note that we won't dirty any indirect blocks,
1530 * which is fine because we will be freeing the entire
1531 * file and thus all indirect blocks will be freed
1532 * by free_children().
1537 } else if (off >= blksz) {
1538 /* Freeing past end-of-data */
1541 /* Freeing part of the block. */
1543 ASSERT3U(head, >, 0);
1547 /* zero out any partial block data at the start of the range */
1549 ASSERT3U(blkoff + head, ==, blksz);
1552 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off), TRUE,
1556 /* don't dirty if it isn't on disk and isn't dirty */
1557 if (db->db_last_dirty ||
1558 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1559 rw_exit(&dn->dn_struct_rwlock);
1560 dmu_buf_will_dirty(&db->db, tx);
1561 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1562 data = db->db.db_data;
1563 bzero(data + blkoff, head);
1565 dbuf_rele(db, FTAG);
1571 /* If the range was less than one block, we're done */
1575 /* If the remaining range is past end of file, we're done */
1576 if ((off >> blkshift) > dn->dn_maxblkid)
1579 ASSERT(ISP2(blksz));
1583 tail = P2PHASE(len, blksz);
1585 ASSERT0(P2PHASE(off, blksz));
1586 /* zero out any partial block data at the end of the range */
1590 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len),
1591 TRUE, FTAG, &db) == 0) {
1592 /* don't dirty if not on disk and not dirty */
1593 if (db->db_last_dirty ||
1594 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1595 rw_exit(&dn->dn_struct_rwlock);
1596 dmu_buf_will_dirty(&db->db, tx);
1597 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1598 bzero(db->db.db_data, tail);
1600 dbuf_rele(db, FTAG);
1605 /* If the range did not include a full block, we are done */
1609 ASSERT(IS_P2ALIGNED(off, blksz));
1610 ASSERT(trunc || IS_P2ALIGNED(len, blksz));
1611 blkid = off >> blkshift;
1612 nblks = len >> blkshift;
1617 * Dirty the first and last indirect blocks, as they (and/or their
1618 * parents) will need to be written out if they were only
1619 * partially freed. Interior indirect blocks will be themselves freed,
1620 * by free_children(), so they need not be dirtied. Note that these
1621 * interior blocks have already been prefetched by dmu_tx_hold_free().
1623 if (dn->dn_nlevels > 1) {
1624 uint64_t first, last;
1626 first = blkid >> epbs;
1627 if (db = dbuf_hold_level(dn, 1, first, FTAG)) {
1628 dmu_buf_will_dirty(&db->db, tx);
1629 dbuf_rele(db, FTAG);
1632 last = dn->dn_maxblkid >> epbs;
1634 last = (blkid + nblks - 1) >> epbs;
1635 if (last > first && (db = dbuf_hold_level(dn, 1, last, FTAG))) {
1636 dmu_buf_will_dirty(&db->db, tx);
1637 dbuf_rele(db, FTAG);
1643 * Add this range to the dnode range list.
1644 * We will finish up this free operation in the syncing phase.
1646 mutex_enter(&dn->dn_mtx);
1647 int txgoff = tx->tx_txg & TXG_MASK;
1648 if (dn->dn_free_ranges[txgoff] == NULL) {
1649 dn->dn_free_ranges[txgoff] =
1650 range_tree_create(NULL, NULL, &dn->dn_mtx);
1652 range_tree_clear(dn->dn_free_ranges[txgoff], blkid, nblks);
1653 range_tree_add(dn->dn_free_ranges[txgoff], blkid, nblks);
1654 dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1655 blkid, nblks, tx->tx_txg);
1656 mutex_exit(&dn->dn_mtx);
1658 dbuf_free_range(dn, blkid, blkid + nblks - 1, tx);
1659 dnode_setdirty(dn, tx);
1662 rw_exit(&dn->dn_struct_rwlock);
1666 dnode_spill_freed(dnode_t *dn)
1670 mutex_enter(&dn->dn_mtx);
1671 for (i = 0; i < TXG_SIZE; i++) {
1672 if (dn->dn_rm_spillblk[i] == DN_KILL_SPILLBLK)
1675 mutex_exit(&dn->dn_mtx);
1676 return (i < TXG_SIZE);
1679 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
1681 dnode_block_freed(dnode_t *dn, uint64_t blkid)
1683 void *dp = spa_get_dsl(dn->dn_objset->os_spa);
1686 if (blkid == DMU_BONUS_BLKID)
1690 * If we're in the process of opening the pool, dp will not be
1691 * set yet, but there shouldn't be anything dirty.
1696 if (dn->dn_free_txg)
1699 if (blkid == DMU_SPILL_BLKID)
1700 return (dnode_spill_freed(dn));
1702 mutex_enter(&dn->dn_mtx);
1703 for (i = 0; i < TXG_SIZE; i++) {
1704 if (dn->dn_free_ranges[i] != NULL &&
1705 range_tree_contains(dn->dn_free_ranges[i], blkid, 1))
1708 mutex_exit(&dn->dn_mtx);
1709 return (i < TXG_SIZE);
1712 /* call from syncing context when we actually write/free space for this dnode */
1714 dnode_diduse_space(dnode_t *dn, int64_t delta)
1717 dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n",
1719 (u_longlong_t)dn->dn_phys->dn_used,
1722 mutex_enter(&dn->dn_mtx);
1723 space = DN_USED_BYTES(dn->dn_phys);
1725 ASSERT3U(space + delta, >=, space); /* no overflow */
1727 ASSERT3U(space, >=, -delta); /* no underflow */
1730 if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_DNODE_BYTES) {
1731 ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0);
1732 ASSERT0(P2PHASE(space, 1<<DEV_BSHIFT));
1733 dn->dn_phys->dn_used = space >> DEV_BSHIFT;
1735 dn->dn_phys->dn_used = space;
1736 dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES;
1738 mutex_exit(&dn->dn_mtx);
1742 * Call when we think we're going to write/free space in open context to track
1743 * the amount of memory in use by the currently open txg.
1746 dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx)
1748 objset_t *os = dn->dn_objset;
1749 dsl_dataset_t *ds = os->os_dsl_dataset;
1750 int64_t aspace = spa_get_asize(os->os_spa, space);
1753 dsl_dir_willuse_space(ds->ds_dir, aspace, tx);
1754 dsl_pool_dirty_space(dmu_tx_pool(tx), space, tx);
1757 dmu_tx_willuse_space(tx, aspace);
1761 * Scans a block at the indicated "level" looking for a hole or data,
1762 * depending on 'flags'.
1764 * If level > 0, then we are scanning an indirect block looking at its
1765 * pointers. If level == 0, then we are looking at a block of dnodes.
1767 * If we don't find what we are looking for in the block, we return ESRCH.
1768 * Otherwise, return with *offset pointing to the beginning (if searching
1769 * forwards) or end (if searching backwards) of the range covered by the
1770 * block pointer we matched on (or dnode).
1772 * The basic search algorithm used below by dnode_next_offset() is to
1773 * use this function to search up the block tree (widen the search) until
1774 * we find something (i.e., we don't return ESRCH) and then search back
1775 * down the tree (narrow the search) until we reach our original search
1779 dnode_next_offset_level(dnode_t *dn, int flags, uint64_t *offset,
1780 int lvl, uint64_t blkfill, uint64_t txg)
1782 dmu_buf_impl_t *db = NULL;
1784 uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
1785 uint64_t epb = 1ULL << epbs;
1786 uint64_t minfill, maxfill;
1788 int i, inc, error, span;
1790 dprintf("probing object %llu offset %llx level %d of %u\n",
1791 dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels);
1793 hole = ((flags & DNODE_FIND_HOLE) != 0);
1794 inc = (flags & DNODE_FIND_BACKWARDS) ? -1 : 1;
1795 ASSERT(txg == 0 || !hole);
1797 if (lvl == dn->dn_phys->dn_nlevels) {
1799 epb = dn->dn_phys->dn_nblkptr;
1800 data = dn->dn_phys->dn_blkptr;
1802 uint64_t blkid = dbuf_whichblock(dn, *offset) >> (epbs * lvl);
1803 error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FTAG, &db);
1805 if (error != ENOENT)
1810 * This can only happen when we are searching up
1811 * the block tree for data. We don't really need to
1812 * adjust the offset, as we will just end up looking
1813 * at the pointer to this block in its parent, and its
1814 * going to be unallocated, so we will skip over it.
1816 return (SET_ERROR(ESRCH));
1818 error = dbuf_read(db, NULL, DB_RF_CANFAIL | DB_RF_HAVESTRUCT);
1820 dbuf_rele(db, FTAG);
1823 data = db->db.db_data;
1827 if (db != NULL && txg != 0 && (db->db_blkptr == NULL ||
1828 db->db_blkptr->blk_birth <= txg ||
1829 BP_IS_HOLE(db->db_blkptr))) {
1831 * This can only happen when we are searching up the tree
1832 * and these conditions mean that we need to keep climbing.
1834 error = SET_ERROR(ESRCH);
1835 } else if (lvl == 0) {
1836 dnode_phys_t *dnp = data;
1838 ASSERT(dn->dn_type == DMU_OT_DNODE);
1840 for (i = (*offset >> span) & (blkfill - 1);
1841 i >= 0 && i < blkfill; i += inc) {
1842 if ((dnp[i].dn_type == DMU_OT_NONE) == hole)
1844 *offset += (1ULL << span) * inc;
1846 if (i < 0 || i == blkfill)
1847 error = SET_ERROR(ESRCH);
1849 blkptr_t *bp = data;
1850 uint64_t start = *offset;
1851 span = (lvl - 1) * epbs + dn->dn_datablkshift;
1853 maxfill = blkfill << ((lvl - 1) * epbs);
1860 *offset = *offset >> span;
1861 for (i = BF64_GET(*offset, 0, epbs);
1862 i >= 0 && i < epb; i += inc) {
1863 if (BP_GET_FILL(&bp[i]) >= minfill &&
1864 BP_GET_FILL(&bp[i]) <= maxfill &&
1865 (hole || bp[i].blk_birth > txg))
1867 if (inc > 0 || *offset > 0)
1870 *offset = *offset << span;
1872 /* traversing backwards; position offset at the end */
1873 ASSERT3U(*offset, <=, start);
1874 *offset = MIN(*offset + (1ULL << span) - 1, start);
1875 } else if (*offset < start) {
1878 if (i < 0 || i >= epb)
1879 error = SET_ERROR(ESRCH);
1883 dbuf_rele(db, FTAG);
1889 * Find the next hole, data, or sparse region at or after *offset.
1890 * The value 'blkfill' tells us how many items we expect to find
1891 * in an L0 data block; this value is 1 for normal objects,
1892 * DNODES_PER_BLOCK for the meta dnode, and some fraction of
1893 * DNODES_PER_BLOCK when searching for sparse regions thereof.
1897 * dnode_next_offset(dn, flags, offset, 1, 1, 0);
1898 * Finds the next/previous hole/data in a file.
1899 * Used in dmu_offset_next().
1901 * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
1902 * Finds the next free/allocated dnode an objset's meta-dnode.
1903 * Only finds objects that have new contents since txg (ie.
1904 * bonus buffer changes and content removal are ignored).
1905 * Used in dmu_object_next().
1907 * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
1908 * Finds the next L2 meta-dnode bp that's at most 1/4 full.
1909 * Used in dmu_object_alloc().
1912 dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset,
1913 int minlvl, uint64_t blkfill, uint64_t txg)
1915 uint64_t initial_offset = *offset;
1919 if (!(flags & DNODE_FIND_HAVELOCK))
1920 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1922 if (dn->dn_phys->dn_nlevels == 0) {
1923 error = SET_ERROR(ESRCH);
1927 if (dn->dn_datablkshift == 0) {
1928 if (*offset < dn->dn_datablksz) {
1929 if (flags & DNODE_FIND_HOLE)
1930 *offset = dn->dn_datablksz;
1932 error = SET_ERROR(ESRCH);
1937 maxlvl = dn->dn_phys->dn_nlevels;
1939 for (lvl = minlvl; lvl <= maxlvl; lvl++) {
1940 error = dnode_next_offset_level(dn,
1941 flags, offset, lvl, blkfill, txg);
1946 while (error == 0 && --lvl >= minlvl) {
1947 error = dnode_next_offset_level(dn,
1948 flags, offset, lvl, blkfill, txg);
1951 if (error == 0 && (flags & DNODE_FIND_BACKWARDS ?
1952 initial_offset < *offset : initial_offset > *offset))
1953 error = SET_ERROR(ESRCH);
1955 if (!(flags & DNODE_FIND_HAVELOCK))
1956 rw_exit(&dn->dn_struct_rwlock);