dnode.c revision 0fbc0cd0e52a11f6c4397a1714f94412cbf98b60
0N/A/*
1472N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms of the
0N/A * Common Development and Distribution License (the "License").
0N/A * You may not use this file except in compliance with the License.
0N/A *
0N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
0N/A * or http://www.opensolaris.org/os/licensing.
0N/A * See the License for the specific language governing permissions
0N/A * and limitations under the License.
0N/A *
0N/A * When distributing Covered Code, include this CDDL HEADER in each
0N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
0N/A * If applicable, add the following below this CDDL HEADER, with the
0N/A * fields enclosed by brackets "[]" replaced with your own identifying
0N/A * information: Portions Copyright [yyyy] [name of copyright owner]
0N/A *
1472N/A * CDDL HEADER END
1472N/A */
1472N/A/*
0N/A * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
0N/A */
1879N/A
1879N/A#include <sys/zfs_context.h>
1879N/A#include <sys/dbuf.h>
1879N/A#include <sys/dnode.h>
1879N/A#include <sys/dmu.h>
1879N/A#include <sys/dmu_impl.h>
1879N/A#include <sys/dmu_tx.h>
1879N/A#include <sys/dmu_objset.h>
1879N/A#include <sys/dsl_dir.h>
0N/A#include <sys/dsl_dataset.h>
0N/A#include <sys/spa.h>
0N/A#include <sys/zio.h>
0N/A#include <sys/dmu_zfetch.h>
0N/A#include <sys/range_tree.h>
0N/A
0N/Astatic kmem_cache_t *dnode_cache;
0N/A/*
0N/A * Define DNODE_STATS to turn on statistic gathering. By default, it is only
0N/A * turned on when DEBUG is also defined.
0N/A */
0N/A#ifdef DEBUG
0N/A#define DNODE_STATS
0N/A#endif /* DEBUG */
0N/A
0N/A#ifdef DNODE_STATS
0N/A#define DNODE_STAT_ADD(stat) ((stat)++)
0N/A#else
0N/A#define DNODE_STAT_ADD(stat) /* nothing */
0N/A#endif /* DNODE_STATS */
0N/A
0N/Astatic dnode_phys_t dnode_phys_zero;
0N/A
0N/Aint zfs_default_bs = SPA_MINBLOCKSHIFT;
0N/Aint zfs_default_ibs = DN_MAX_INDBLKSHIFT;
0N/A
0N/Astatic kmem_cbrc_t dnode_move(void *, void *, size_t, void *);
0N/A
0N/Astatic int
0N/Adbuf_compare(const void *x1, const void *x2)
0N/A{
0N/A const dmu_buf_impl_t *d1 = x1;
0N/A const dmu_buf_impl_t *d2 = x2;
0N/A
0N/A if (d1->db_level < d2->db_level) {
0N/A return (-1);
0N/A }
0N/A if (d1->db_level > d2->db_level) {
0N/A return (1);
0N/A }
0N/A
0N/A if (d1->db_blkid < d2->db_blkid) {
0N/A return (-1);
0N/A }
0N/A if (d1->db_blkid > d2->db_blkid) {
0N/A return (1);
0N/A }
0N/A
0N/A if (d1->db_state < d2->db_state) {
0N/A return (-1);
0N/A }
0N/A if (d1->db_state > d2->db_state) {
0N/A return (1);
0N/A }
0N/A
0N/A ASSERT3S(d1->db_state, !=, DB_SEARCH);
0N/A ASSERT3S(d2->db_state, !=, DB_SEARCH);
0N/A
0N/A if ((uintptr_t)d1 < (uintptr_t)d2) {
0N/A return (-1);
0N/A }
0N/A if ((uintptr_t)d1 > (uintptr_t)d2) {
0N/A return (1);
0N/A }
0N/A return (0);
0N/A}
0N/A
0N/A/* ARGSUSED */
0N/Astatic int
0N/Adnode_cons(void *arg, void *unused, int kmflag)
0N/A{
0N/A dnode_t *dn = arg;
0N/A int i;
0N/A
0N/A rw_init(&dn->dn_struct_rwlock, NULL, RW_DEFAULT, NULL);
0N/A mutex_init(&dn->dn_mtx, NULL, MUTEX_DEFAULT, NULL);
0N/A mutex_init(&dn->dn_dbufs_mtx, NULL, MUTEX_DEFAULT, NULL);
0N/A cv_init(&dn->dn_notxholds, NULL, CV_DEFAULT, NULL);
0N/A
0N/A /*
0N/A * Every dbuf has a reference, and dropping a tracked reference is
0N/A * O(number of references), so don't track dn_holds.
0N/A */
0N/A refcount_create_untracked(&dn->dn_holds);
0N/A refcount_create(&dn->dn_tx_holds);
0N/A list_link_init(&dn->dn_link);
0N/A
0N/A bzero(&dn->dn_next_nblkptr[0], sizeof (dn->dn_next_nblkptr));
0N/A bzero(&dn->dn_next_nlevels[0], sizeof (dn->dn_next_nlevels));
0N/A bzero(&dn->dn_next_indblkshift[0], sizeof (dn->dn_next_indblkshift));
0N/A bzero(&dn->dn_next_bonustype[0], sizeof (dn->dn_next_bonustype));
0N/A bzero(&dn->dn_rm_spillblk[0], sizeof (dn->dn_rm_spillblk));
0N/A bzero(&dn->dn_next_bonuslen[0], sizeof (dn->dn_next_bonuslen));
0N/A bzero(&dn->dn_next_blksz[0], sizeof (dn->dn_next_blksz));
0N/A
0N/A for (i = 0; i < TXG_SIZE; i++) {
0N/A list_link_init(&dn->dn_dirty_link[i]);
0N/A dn->dn_free_ranges[i] = NULL;
0N/A list_create(&dn->dn_dirty_records[i],
0N/A sizeof (dbuf_dirty_record_t),
0N/A offsetof(dbuf_dirty_record_t, dr_dirty_node));
0N/A }
0N/A
0N/A dn->dn_allocated_txg = 0;
0N/A dn->dn_free_txg = 0;
0N/A dn->dn_assigned_txg = 0;
0N/A dn->dn_dirtyctx = 0;
0N/A dn->dn_dirtyctx_firstset = NULL;
0N/A dn->dn_bonus = NULL;
0N/A dn->dn_have_spill = B_FALSE;
0N/A dn->dn_zio = NULL;
0N/A dn->dn_oldused = 0;
0N/A dn->dn_oldflags = 0;
0N/A dn->dn_olduid = 0;
0N/A dn->dn_oldgid = 0;
0N/A dn->dn_newuid = 0;
0N/A dn->dn_newgid = 0;
0N/A dn->dn_id_flags = 0;
0N/A
0N/A dn->dn_dbufs_count = 0;
0N/A dn->dn_unlisted_l0_blkid = 0;
0N/A avl_create(&dn->dn_dbufs, dbuf_compare, sizeof (dmu_buf_impl_t),
0N/A offsetof(dmu_buf_impl_t, db_link));
0N/A
0N/A dn->dn_moved = 0;
0N/A return (0);
0N/A}
0N/A
0N/A/* ARGSUSED */
0N/Astatic void
0N/Adnode_dest(void *arg, void *unused)
0N/A{
0N/A int i;
0N/A dnode_t *dn = arg;
0N/A
0N/A rw_destroy(&dn->dn_struct_rwlock);
0N/A mutex_destroy(&dn->dn_mtx);
0N/A mutex_destroy(&dn->dn_dbufs_mtx);
0N/A cv_destroy(&dn->dn_notxholds);
0N/A refcount_destroy(&dn->dn_holds);
0N/A refcount_destroy(&dn->dn_tx_holds);
0N/A ASSERT(!list_link_active(&dn->dn_link));
0N/A
0N/A for (i = 0; i < TXG_SIZE; i++) {
0N/A ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
0N/A ASSERT3P(dn->dn_free_ranges[i], ==, NULL);
0N/A list_destroy(&dn->dn_dirty_records[i]);
0N/A ASSERT0(dn->dn_next_nblkptr[i]);
0N/A ASSERT0(dn->dn_next_nlevels[i]);
0N/A ASSERT0(dn->dn_next_indblkshift[i]);
0N/A ASSERT0(dn->dn_next_bonustype[i]);
0N/A ASSERT0(dn->dn_rm_spillblk[i]);
0N/A ASSERT0(dn->dn_next_bonuslen[i]);
0N/A ASSERT0(dn->dn_next_blksz[i]);
0N/A }
0N/A
2676N/A ASSERT0(dn->dn_allocated_txg);
2676N/A ASSERT0(dn->dn_free_txg);
2676N/A ASSERT0(dn->dn_assigned_txg);
2963N/A ASSERT0(dn->dn_dirtyctx);
2963N/A ASSERT3P(dn->dn_dirtyctx_firstset, ==, NULL);
2963N/A ASSERT3P(dn->dn_bonus, ==, NULL);
0N/A ASSERT(!dn->dn_have_spill);
0N/A ASSERT3P(dn->dn_zio, ==, NULL);
0N/A ASSERT0(dn->dn_oldused);
0N/A ASSERT0(dn->dn_oldflags);
0N/A ASSERT0(dn->dn_olduid);
0N/A ASSERT0(dn->dn_oldgid);
0N/A ASSERT0(dn->dn_newuid);
0N/A ASSERT0(dn->dn_newgid);
0N/A ASSERT0(dn->dn_id_flags);
0N/A
0N/A ASSERT0(dn->dn_dbufs_count);
0N/A ASSERT0(dn->dn_unlisted_l0_blkid);
0N/A avl_destroy(&dn->dn_dbufs);
0N/A}
0N/A
0N/Avoid
0N/Adnode_init(void)
0N/A{
0N/A ASSERT(dnode_cache == NULL);
0N/A dnode_cache = kmem_cache_create("dnode_t",
0N/A sizeof (dnode_t),
0N/A 0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0);
0N/A kmem_cache_set_move(dnode_cache, dnode_move);
0N/A}
0N/A
0N/Avoid
0N/Adnode_fini(void)
0N/A{
0N/A kmem_cache_destroy(dnode_cache);
0N/A dnode_cache = NULL;
0N/A}
0N/A
0N/A
0N/A#ifdef ZFS_DEBUG
0N/Avoid
0N/Adnode_verify(dnode_t *dn)
0N/A{
0N/A int drop_struct_lock = FALSE;
0N/A
0N/A ASSERT(dn->dn_phys);
0N/A ASSERT(dn->dn_objset);
0N/A ASSERT(dn->dn_handle->dnh_dnode == dn);
0N/A
0N/A ASSERT(DMU_OT_IS_VALID(dn->dn_phys->dn_type));
0N/A
0N/A if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY))
0N/A return;
0N/A
0N/A if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
0N/A rw_enter(&dn->dn_struct_rwlock, RW_READER);
1203N/A drop_struct_lock = TRUE;
0N/A }
0N/A if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) {
0N/A int i;
0N/A ASSERT3U(dn->dn_indblkshift, >=, 0);
0N/A ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT);
0N/A if (dn->dn_datablkshift) {
0N/A ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT);
0N/A ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT);
0N/A ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz);
0N/A }
0N/A ASSERT3U(dn->dn_nlevels, <=, 30);
0N/A ASSERT(DMU_OT_IS_VALID(dn->dn_type));
0N/A ASSERT3U(dn->dn_nblkptr, >=, 1);
0N/A ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
0N/A ASSERT3U(dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
0N/A ASSERT3U(dn->dn_datablksz, ==,
0N/A dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
0N/A ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0);
0N/A ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) +
0N/A dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
0N/A for (i = 0; i < TXG_SIZE; i++) {
0N/A ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels);
0N/A }
0N/A }
0N/A if (dn->dn_phys->dn_type != DMU_OT_NONE)
0N/A ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels);
0N/A ASSERT(DMU_OBJECT_IS_SPECIAL(dn->dn_object) || dn->dn_dbuf != NULL);
0N/A if (dn->dn_dbuf != NULL) {
0N/A ASSERT3P(dn->dn_phys, ==,
0N/A (dnode_phys_t *)dn->dn_dbuf->db.db_data +
0N/A (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT)));
0N/A }
0N/A if (drop_struct_lock)
0N/A rw_exit(&dn->dn_struct_rwlock);
0N/A}
0N/A#endif
0N/A
0N/Avoid
0N/Adnode_byteswap(dnode_phys_t *dnp)
0N/A{
0N/A uint64_t *buf64 = (void*)&dnp->dn_blkptr;
0N/A int i;
0N/A
0N/A if (dnp->dn_type == DMU_OT_NONE) {
0N/A bzero(dnp, sizeof (dnode_phys_t));
0N/A return;
0N/A }
0N/A
0N/A dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec);
0N/A dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen);
0N/A dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid);
0N/A dnp->dn_used = BSWAP_64(dnp->dn_used);
0N/A
0N/A /*
0N/A * dn_nblkptr is only one byte, so it's OK to read it in either
0N/A * byte order. We can't read dn_bouslen.
0N/A */
0N/A ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT);
0N/A ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR);
0N/A for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++)
0N/A buf64[i] = BSWAP_64(buf64[i]);
0N/A
0N/A /*
0N/A * OK to check dn_bonuslen for zero, because it won't matter if
0N/A * we have the wrong byte order. This is necessary because the
0N/A * dnode dnode is smaller than a regular dnode.
0N/A */
0N/A if (dnp->dn_bonuslen != 0) {
0N/A /*
0N/A * Note that the bonus length calculated here may be
0N/A * longer than the actual bonus buffer. This is because
0N/A * we always put the bonus buffer after the last block
0N/A * pointer (instead of packing it against the end of the
0N/A * dnode buffer).
0N/A */
0N/A int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t);
0N/A size_t len = DN_MAX_BONUSLEN - off;
0N/A ASSERT(DMU_OT_IS_VALID(dnp->dn_bonustype));
0N/A dmu_object_byteswap_t byteswap =
0N/A DMU_OT_BYTESWAP(dnp->dn_bonustype);
0N/A dmu_ot_byteswap[byteswap].ob_func(dnp->dn_bonus + off, len);
0N/A }
0N/A
0N/A /* Swap SPILL block if we have one */
0N/A if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)
0N/A byteswap_uint64_array(&dnp->dn_spill, sizeof (blkptr_t));
0N/A
0N/A}
0N/A
0N/Avoid
0N/Adnode_buf_byteswap(void *vbuf, size_t size)
0N/A{
0N/A dnode_phys_t *buf = vbuf;
0N/A int i;
0N/A
0N/A ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT));
0N/A ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0);
1915N/A
1915N/A size >>= DNODE_SHIFT;
1915N/A for (i = 0; i < size; i++) {
1915N/A dnode_byteswap(buf);
1915N/A buf++;
1915N/A }
1915N/A}
1915N/A
1915N/Avoid
1915N/Adnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx)
1915N/A{
1915N/A ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
1915N/A
1915N/A dnode_setdirty(dn, tx);
1915N/A rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1915N/A ASSERT3U(newsize, <=, DN_MAX_BONUSLEN -
1915N/A (dn->dn_nblkptr-1) * sizeof (blkptr_t));
1915N/A dn->dn_bonuslen = newsize;
1915N/A if (newsize == 0)
1915N/A dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = DN_ZERO_BONUSLEN;
1915N/A else
1915N/A dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
1915N/A rw_exit(&dn->dn_struct_rwlock);
1915N/A}
1915N/A
1915N/Avoid
1915N/Adnode_setbonus_type(dnode_t *dn, dmu_object_type_t newtype, dmu_tx_t *tx)
1915N/A{
1915N/A ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
1915N/A dnode_setdirty(dn, tx);
1915N/A rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1915N/A dn->dn_bonustype = newtype;
1915N/A dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
1915N/A rw_exit(&dn->dn_struct_rwlock);
1915N/A}
1915N/A
1915N/Avoid
1915N/Adnode_rm_spill(dnode_t *dn, dmu_tx_t *tx)
1915N/A{
1915N/A ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
1915N/A ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
1915N/A dnode_setdirty(dn, tx);
1915N/A dn->dn_rm_spillblk[tx->tx_txg&TXG_MASK] = DN_KILL_SPILLBLK;
1915N/A dn->dn_have_spill = B_FALSE;
1915N/A}
1915N/A
1915N/Astatic void
1915N/Adnode_setdblksz(dnode_t *dn, int size)
1915N/A{
1915N/A ASSERT0(P2PHASE(size, SPA_MINBLOCKSIZE));
1915N/A ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
1915N/A ASSERT3U(size, >=, SPA_MINBLOCKSIZE);
1915N/A ASSERT3U(size >> SPA_MINBLOCKSHIFT, <,
1915N/A 1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8));
1915N/A dn->dn_datablksz = size;
1915N/A dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT;
1915N/A dn->dn_datablkshift = ISP2(size) ? highbit64(size - 1) : 0;
1915N/A}
1915N/A
0N/Astatic dnode_t *
0N/Adnode_create(objset_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db,
0N/A uint64_t object, dnode_handle_t *dnh)
0N/A{
0N/A dnode_t *dn = kmem_cache_alloc(dnode_cache, KM_SLEEP);
0N/A
0N/A ASSERT(!POINTER_IS_VALID(dn->dn_objset));
0N/A dn->dn_moved = 0;
0N/A
0N/A /*
0N/A * Defer setting dn_objset until the dnode is ready to be a candidate
0N/A * for the dnode_move() callback.
0N/A */
0N/A dn->dn_object = object;
0N/A dn->dn_dbuf = db;
0N/A dn->dn_handle = dnh;
0N/A dn->dn_phys = dnp;
0N/A
0N/A if (dnp->dn_datablkszsec) {
0N/A dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
0N/A } else {
0N/A dn->dn_datablksz = 0;
0N/A dn->dn_datablkszsec = 0;
0N/A dn->dn_datablkshift = 0;
0N/A }
0N/A dn->dn_indblkshift = dnp->dn_indblkshift;
0N/A dn->dn_nlevels = dnp->dn_nlevels;
0N/A dn->dn_type = dnp->dn_type;
0N/A dn->dn_nblkptr = dnp->dn_nblkptr;
0N/A dn->dn_checksum = dnp->dn_checksum;
0N/A dn->dn_compress = dnp->dn_compress;
0N/A dn->dn_bonustype = dnp->dn_bonustype;
0N/A dn->dn_bonuslen = dnp->dn_bonuslen;
0N/A dn->dn_maxblkid = dnp->dn_maxblkid;
0N/A dn->dn_have_spill = ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0);
0N/A dn->dn_id_flags = 0;
0N/A
0N/A dmu_zfetch_init(&dn->dn_zfetch, dn);
0N/A
0N/A ASSERT(DMU_OT_IS_VALID(dn->dn_phys->dn_type));
0N/A
0N/A mutex_enter(&os->os_lock);
0N/A list_insert_head(&os->os_dnodes, dn);
0N/A membar_producer();
0N/A /*
0N/A * Everything else must be valid before assigning dn_objset makes the
0N/A * dnode eligible for dnode_move().
0N/A */
0N/A dn->dn_objset = os;
0N/A mutex_exit(&os->os_lock);
0N/A
0N/A arc_space_consume(sizeof (dnode_t), ARC_SPACE_OTHER);
0N/A return (dn);
0N/A}
0N/A
0N/A/*
0N/A * Caller must be holding the dnode handle, which is released upon return.
0N/A */
0N/Astatic void
0N/Adnode_destroy(dnode_t *dn)
0N/A{
0N/A objset_t *os = dn->dn_objset;
0N/A
0N/A ASSERT((dn->dn_id_flags & DN_ID_NEW_EXIST) == 0);
0N/A
0N/A mutex_enter(&os->os_lock);
0N/A POINTER_INVALIDATE(&dn->dn_objset);
0N/A list_remove(&os->os_dnodes, dn);
0N/A mutex_exit(&os->os_lock);
0N/A
0N/A /* the dnode can no longer move, so we can release the handle */
0N/A zrl_remove(&dn->dn_handle->dnh_zrlock);
0N/A
0N/A dn->dn_allocated_txg = 0;
0N/A dn->dn_free_txg = 0;
0N/A dn->dn_assigned_txg = 0;
0N/A
0N/A dn->dn_dirtyctx = 0;
0N/A if (dn->dn_dirtyctx_firstset != NULL) {
0N/A kmem_free(dn->dn_dirtyctx_firstset, 1);
0N/A dn->dn_dirtyctx_firstset = NULL;
0N/A }
0N/A if (dn->dn_bonus != NULL) {
0N/A mutex_enter(&dn->dn_bonus->db_mtx);
0N/A dbuf_evict(dn->dn_bonus);
0N/A dn->dn_bonus = NULL;
0N/A }
0N/A dn->dn_zio = NULL;
0N/A
0N/A dn->dn_have_spill = B_FALSE;
0N/A dn->dn_oldused = 0;
0N/A dn->dn_oldflags = 0;
0N/A dn->dn_olduid = 0;
0N/A dn->dn_oldgid = 0;
0N/A dn->dn_newuid = 0;
0N/A dn->dn_newgid = 0;
0N/A dn->dn_id_flags = 0;
0N/A dn->dn_unlisted_l0_blkid = 0;
0N/A
0N/A dmu_zfetch_rele(&dn->dn_zfetch);
0N/A kmem_cache_free(dnode_cache, dn);
0N/A arc_space_return(sizeof (dnode_t), ARC_SPACE_OTHER);
0N/A}
0N/A
0N/Avoid
0N/Adnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
0N/A dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
0N/A{
0N/A int i;
0N/A
2678N/A if (blocksize == 0)
2678N/A blocksize = 1 << zfs_default_bs;
2678N/A else if (blocksize > SPA_MAXBLOCKSIZE)
2678N/A blocksize = SPA_MAXBLOCKSIZE;
2678N/A else
2678N/A blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE);
2678N/A
2678N/A if (ibs == 0)
2678N/A ibs = zfs_default_ibs;
2678N/A
2678N/A ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT);
2678N/A
2678N/A dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset,
2678N/A dn->dn_object, tx->tx_txg, blocksize, ibs);
2678N/A
2678N/A ASSERT(dn->dn_type == DMU_OT_NONE);
0N/A ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0);
0N/A ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE);
0N/A ASSERT(ot != DMU_OT_NONE);
0N/A ASSERT(DMU_OT_IS_VALID(ot));
0N/A ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
0N/A (bonustype == DMU_OT_SA && bonuslen == 0) ||
2678N/A (bonustype != DMU_OT_NONE && bonuslen != 0));
0N/A ASSERT(DMU_OT_IS_VALID(bonustype));
0N/A ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
2678N/A ASSERT(dn->dn_type == DMU_OT_NONE);
0N/A ASSERT0(dn->dn_maxblkid);
0N/A ASSERT0(dn->dn_allocated_txg);
0N/A ASSERT0(dn->dn_assigned_txg);
0N/A ASSERT(refcount_is_zero(&dn->dn_tx_holds));
2678N/A ASSERT3U(refcount_count(&dn->dn_holds), <=, 1);
0N/A ASSERT(avl_is_empty(&dn->dn_dbufs));
0N/A
2664N/A for (i = 0; i < TXG_SIZE; i++) {
2678N/A ASSERT0(dn->dn_next_nblkptr[i]);
0N/A ASSERT0(dn->dn_next_nlevels[i]);
0N/A ASSERT0(dn->dn_next_indblkshift[i]);
0N/A ASSERT0(dn->dn_next_bonuslen[i]);
0N/A ASSERT0(dn->dn_next_bonustype[i]);
0N/A ASSERT0(dn->dn_rm_spillblk[i]);
0N/A ASSERT0(dn->dn_next_blksz[i]);
0N/A ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
0N/A ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL);
0N/A ASSERT3P(dn->dn_free_ranges[i], ==, NULL);
0N/A }
0N/A
0N/A dn->dn_type = ot;
0N/A dnode_setdblksz(dn, blocksize);
0N/A dn->dn_indblkshift = ibs;
0N/A dn->dn_nlevels = 1;
0N/A if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
0N/A dn->dn_nblkptr = 1;
0N/A else
0N/A dn->dn_nblkptr = 1 +
0N/A ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
0N/A dn->dn_bonustype = bonustype;
2667N/A dn->dn_bonuslen = bonuslen;
2667N/A dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
2667N/A dn->dn_compress = ZIO_COMPRESS_INHERIT;
0N/A dn->dn_dirtyctx = 0;
0N/A
0N/A dn->dn_free_txg = 0;
0N/A if (dn->dn_dirtyctx_firstset) {
0N/A kmem_free(dn->dn_dirtyctx_firstset, 1);
0N/A dn->dn_dirtyctx_firstset = NULL;
0N/A }
0N/A
0N/A dn->dn_allocated_txg = tx->tx_txg;
0N/A dn->dn_id_flags = 0;
0N/A
0N/A dnode_setdirty(dn, tx);
0N/A dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs;
0N/A dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
0N/A dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
0N/A dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz;
0N/A}
0N/A
0N/Avoid
0N/Adnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
0N/A dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
2678N/A{
0N/A int nblkptr;
0N/A
0N/A ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE);
0N/A ASSERT3U(blocksize, <=, SPA_MAXBLOCKSIZE);
2678N/A ASSERT0(blocksize % SPA_MINBLOCKSIZE);
0N/A ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
0N/A ASSERT(tx->tx_txg != 0);
2678N/A ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
2678N/A (bonustype != DMU_OT_NONE && bonuslen != 0) ||
0N/A (bonustype == DMU_OT_SA && bonuslen == 0));
0N/A ASSERT(DMU_OT_IS_VALID(bonustype));
0N/A ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
0N/A
0N/A /* clean up any unreferenced dbufs */
2667N/A dnode_evict_dbufs(dn);
2667N/A
2678N/A dn->dn_id_flags = 0;
2667N/A
2678N/A rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2667N/A dnode_setdirty(dn, tx);
2667N/A if (dn->dn_datablksz != blocksize) {
2667N/A /* change blocksize */
2667N/A ASSERT(dn->dn_maxblkid == 0 &&
0N/A (BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) ||
0N/A dnode_block_freed(dn, 0)));
0N/A dnode_setdblksz(dn, blocksize);
0N/A dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize;
0N/A }
0N/A if (dn->dn_bonuslen != bonuslen)
0N/A dn->dn_next_bonuslen[tx->tx_txg&TXG_MASK] = bonuslen;
0N/A
0N/A if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
0N/A nblkptr = 1;
0N/A else
0N/A nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
0N/A if (dn->dn_bonustype != bonustype)
0N/A dn->dn_next_bonustype[tx->tx_txg&TXG_MASK] = bonustype;
0N/A if (dn->dn_nblkptr != nblkptr)
0N/A dn->dn_next_nblkptr[tx->tx_txg&TXG_MASK] = nblkptr;
0N/A if (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
0N/A dbuf_rm_spill(dn, tx);
0N/A dnode_rm_spill(dn, tx);
0N/A }
0N/A rw_exit(&dn->dn_struct_rwlock);
0N/A
0N/A /* change type */
0N/A dn->dn_type = ot;
0N/A
0N/A /* change bonus size and type */
0N/A mutex_enter(&dn->dn_mtx);
0N/A dn->dn_bonustype = bonustype;
0N/A dn->dn_bonuslen = bonuslen;
0N/A dn->dn_nblkptr = nblkptr;
0N/A dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
0N/A dn->dn_compress = ZIO_COMPRESS_INHERIT;
0N/A ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
0N/A
0N/A /* fix up the bonus db_size */
0N/A if (dn->dn_bonus) {
0N/A dn->dn_bonus->db.db_size =
0N/A DN_MAX_BONUSLEN - (dn->dn_nblkptr-1) * sizeof (blkptr_t);
0N/A ASSERT(dn->dn_bonuslen <= dn->dn_bonus->db.db_size);
0N/A }
0N/A
0N/A dn->dn_allocated_txg = tx->tx_txg;
0N/A mutex_exit(&dn->dn_mtx);
0N/A}
0N/A
0N/A#ifdef DNODE_STATS
0N/Astatic struct {
0N/A uint64_t dms_dnode_invalid;
0N/A uint64_t dms_dnode_recheck1;
0N/A uint64_t dms_dnode_recheck2;
0N/A uint64_t dms_dnode_special;
0N/A uint64_t dms_dnode_handle;
0N/A uint64_t dms_dnode_rwlock;
0N/A uint64_t dms_dnode_active;
0N/A} dnode_move_stats;
0N/A#endif /* DNODE_STATS */
0N/A
0N/Astatic void
0N/Adnode_move_impl(dnode_t *odn, dnode_t *ndn)
0N/A{
0N/A int i;
0N/A
0N/A ASSERT(!RW_LOCK_HELD(&odn->dn_struct_rwlock));
0N/A ASSERT(MUTEX_NOT_HELD(&odn->dn_mtx));
0N/A ASSERT(MUTEX_NOT_HELD(&odn->dn_dbufs_mtx));
0N/A ASSERT(!RW_LOCK_HELD(&odn->dn_zfetch.zf_rwlock));
0N/A
0N/A /* Copy fields. */
0N/A ndn->dn_objset = odn->dn_objset;
0N/A ndn->dn_object = odn->dn_object;
0N/A ndn->dn_dbuf = odn->dn_dbuf;
0N/A ndn->dn_handle = odn->dn_handle;
0N/A ndn->dn_phys = odn->dn_phys;
0N/A ndn->dn_type = odn->dn_type;
0N/A ndn->dn_bonuslen = odn->dn_bonuslen;
0N/A ndn->dn_bonustype = odn->dn_bonustype;
0N/A ndn->dn_nblkptr = odn->dn_nblkptr;
0N/A ndn->dn_checksum = odn->dn_checksum;
0N/A ndn->dn_compress = odn->dn_compress;
0N/A ndn->dn_nlevels = odn->dn_nlevels;
0N/A ndn->dn_indblkshift = odn->dn_indblkshift;
0N/A ndn->dn_datablkshift = odn->dn_datablkshift;
0N/A ndn->dn_datablkszsec = odn->dn_datablkszsec;
0N/A ndn->dn_datablksz = odn->dn_datablksz;
0N/A ndn->dn_maxblkid = odn->dn_maxblkid;
0N/A bcopy(&odn->dn_next_nblkptr[0], &ndn->dn_next_nblkptr[0],
0N/A sizeof (odn->dn_next_nblkptr));
0N/A bcopy(&odn->dn_next_nlevels[0], &ndn->dn_next_nlevels[0],
0N/A sizeof (odn->dn_next_nlevels));
0N/A bcopy(&odn->dn_next_indblkshift[0], &ndn->dn_next_indblkshift[0],
0N/A sizeof (odn->dn_next_indblkshift));
0N/A bcopy(&odn->dn_next_bonustype[0], &ndn->dn_next_bonustype[0],
0N/A sizeof (odn->dn_next_bonustype));
0N/A bcopy(&odn->dn_rm_spillblk[0], &ndn->dn_rm_spillblk[0],
0N/A sizeof (odn->dn_rm_spillblk));
0N/A bcopy(&odn->dn_next_bonuslen[0], &ndn->dn_next_bonuslen[0],
0N/A sizeof (odn->dn_next_bonuslen));
0N/A bcopy(&odn->dn_next_blksz[0], &ndn->dn_next_blksz[0],
0N/A sizeof (odn->dn_next_blksz));
0N/A for (i = 0; i < TXG_SIZE; i++) {
0N/A list_move_tail(&ndn->dn_dirty_records[i],
0N/A &odn->dn_dirty_records[i]);
0N/A }
0N/A bcopy(&odn->dn_free_ranges[0], &ndn->dn_free_ranges[0],
0N/A sizeof (odn->dn_free_ranges));
0N/A ndn->dn_allocated_txg = odn->dn_allocated_txg;
0N/A ndn->dn_free_txg = odn->dn_free_txg;
0N/A ndn->dn_assigned_txg = odn->dn_assigned_txg;
0N/A ndn->dn_dirtyctx = odn->dn_dirtyctx;
0N/A ndn->dn_dirtyctx_firstset = odn->dn_dirtyctx_firstset;
0N/A ASSERT(refcount_count(&odn->dn_tx_holds) == 0);
0N/A refcount_transfer(&ndn->dn_holds, &odn->dn_holds);
0N/A ASSERT(avl_is_empty(&ndn->dn_dbufs));
0N/A avl_swap(&ndn->dn_dbufs, &odn->dn_dbufs);
0N/A ndn->dn_dbufs_count = odn->dn_dbufs_count;
0N/A ndn->dn_unlisted_l0_blkid = odn->dn_unlisted_l0_blkid;
0N/A ndn->dn_bonus = odn->dn_bonus;
0N/A ndn->dn_have_spill = odn->dn_have_spill;
0N/A ndn->dn_zio = odn->dn_zio;
0N/A ndn->dn_oldused = odn->dn_oldused;
0N/A ndn->dn_oldflags = odn->dn_oldflags;
0N/A ndn->dn_olduid = odn->dn_olduid;
0N/A ndn->dn_oldgid = odn->dn_oldgid;
0N/A ndn->dn_newuid = odn->dn_newuid;
0N/A ndn->dn_newgid = odn->dn_newgid;
0N/A ndn->dn_id_flags = odn->dn_id_flags;
0N/A dmu_zfetch_init(&ndn->dn_zfetch, NULL);
0N/A list_move_tail(&ndn->dn_zfetch.zf_stream, &odn->dn_zfetch.zf_stream);
0N/A ndn->dn_zfetch.zf_dnode = odn->dn_zfetch.zf_dnode;
0N/A ndn->dn_zfetch.zf_stream_cnt = odn->dn_zfetch.zf_stream_cnt;
0N/A ndn->dn_zfetch.zf_alloc_fail = odn->dn_zfetch.zf_alloc_fail;
0N/A
0N/A /*
0N/A * Update back pointers. Updating the handle fixes the back pointer of
0N/A * every descendant dbuf as well as the bonus dbuf.
0N/A */
1137N/A ASSERT(ndn->dn_handle->dnh_dnode == odn);
0N/A ndn->dn_handle->dnh_dnode = ndn;
0N/A if (ndn->dn_zfetch.zf_dnode == odn) {
0N/A ndn->dn_zfetch.zf_dnode = ndn;
1137N/A }
1137N/A
1137N/A /*
0N/A * Invalidate the original dnode by clearing all of its back pointers.
0N/A */
0N/A odn->dn_dbuf = NULL;
0N/A odn->dn_handle = NULL;
0N/A avl_create(&odn->dn_dbufs, dbuf_compare, sizeof (dmu_buf_impl_t),
0N/A offsetof(dmu_buf_impl_t, db_link));
0N/A odn->dn_dbufs_count = 0;
0N/A odn->dn_unlisted_l0_blkid = 0;
0N/A odn->dn_bonus = NULL;
0N/A odn->dn_zfetch.zf_dnode = NULL;
0N/A
0N/A /*
0N/A * Set the low bit of the objset pointer to ensure that dnode_move()
0N/A * recognizes the dnode as invalid in any subsequent callback.
0N/A */
0N/A POINTER_INVALIDATE(&odn->dn_objset);
0N/A
0N/A /*
0N/A * Satisfy the destructor.
0N/A */
0N/A for (i = 0; i < TXG_SIZE; i++) {
0N/A list_create(&odn->dn_dirty_records[i],
0N/A sizeof (dbuf_dirty_record_t),
0N/A offsetof(dbuf_dirty_record_t, dr_dirty_node));
0N/A odn->dn_free_ranges[i] = NULL;
0N/A odn->dn_next_nlevels[i] = 0;
0N/A odn->dn_next_indblkshift[i] = 0;
0N/A odn->dn_next_bonustype[i] = 0;
0N/A odn->dn_rm_spillblk[i] = 0;
0N/A odn->dn_next_bonuslen[i] = 0;
0N/A odn->dn_next_blksz[i] = 0;
0N/A }
0N/A odn->dn_allocated_txg = 0;
0N/A odn->dn_free_txg = 0;
0N/A odn->dn_assigned_txg = 0;
0N/A odn->dn_dirtyctx = 0;
0N/A odn->dn_dirtyctx_firstset = NULL;
0N/A odn->dn_have_spill = B_FALSE;
0N/A odn->dn_zio = NULL;
0N/A odn->dn_oldused = 0;
0N/A odn->dn_oldflags = 0;
0N/A odn->dn_olduid = 0;
0N/A odn->dn_oldgid = 0;
0N/A odn->dn_newuid = 0;
0N/A odn->dn_newgid = 0;
0N/A odn->dn_id_flags = 0;
0N/A
0N/A /*
0N/A * Mark the dnode.
0N/A */
0N/A ndn->dn_moved = 1;
0N/A odn->dn_moved = (uint8_t)-1;
0N/A}
0N/A
0N/A#ifdef _KERNEL
0N/A/*ARGSUSED*/
0N/Astatic kmem_cbrc_t
0N/Adnode_move(void *buf, void *newbuf, size_t size, void *arg)
0N/A{
0N/A dnode_t *odn = buf, *ndn = newbuf;
0N/A objset_t *os;
0N/A int64_t refcount;
0N/A uint32_t dbufs;
0N/A
0N/A /*
0N/A * The dnode is on the objset's list of known dnodes if the objset
0N/A * pointer is valid. We set the low bit of the objset pointer when
0N/A * freeing the dnode to invalidate it, and the memory patterns written
0N/A * by kmem (baddcafe and deadbeef) set at least one of the two low bits.
0N/A * A newly created dnode sets the objset pointer last of all to indicate
0N/A * that the dnode is known and in a valid state to be moved by this
0N/A * function.
0N/A */
0N/A os = odn->dn_objset;
0N/A if (!POINTER_IS_VALID(os)) {
0N/A DNODE_STAT_ADD(dnode_move_stats.dms_dnode_invalid);
0N/A return (KMEM_CBRC_DONT_KNOW);
0N/A }
0N/A
0N/A /*
0N/A * Ensure that the objset does not go away during the move.
0N/A */
0N/A rw_enter(&os_lock, RW_WRITER);
0N/A if (os != odn->dn_objset) {
0N/A rw_exit(&os_lock);
0N/A DNODE_STAT_ADD(dnode_move_stats.dms_dnode_recheck1);
0N/A return (KMEM_CBRC_DONT_KNOW);
0N/A }
0N/A
0N/A /*
0N/A * If the dnode is still valid, then so is the objset. We know that no
0N/A * valid objset can be freed while we hold os_lock, so we can safely
0N/A * ensure that the objset remains in use.
0N/A */
0N/A mutex_enter(&os->os_lock);
0N/A
0N/A /*
0N/A * Recheck the objset pointer in case the dnode was removed just before
0N/A * acquiring the lock.
0N/A */
0N/A if (os != odn->dn_objset) {
0N/A mutex_exit(&os->os_lock);
0N/A rw_exit(&os_lock);
0N/A DNODE_STAT_ADD(dnode_move_stats.dms_dnode_recheck2);
0N/A return (KMEM_CBRC_DONT_KNOW);
0N/A }
0N/A
0N/A /*
0N/A * At this point we know that as long as we hold os->os_lock, the dnode
0N/A * cannot be freed and fields within the dnode can be safely accessed.
0N/A * The objset listing this dnode cannot go away as long as this dnode is
0N/A * on its list.
0N/A */
0N/A rw_exit(&os_lock);
0N/A if (DMU_OBJECT_IS_SPECIAL(odn->dn_object)) {
0N/A mutex_exit(&os->os_lock);
0N/A DNODE_STAT_ADD(dnode_move_stats.dms_dnode_special);
0N/A return (KMEM_CBRC_NO);
0N/A }
2664N/A ASSERT(odn->dn_dbuf != NULL); /* only "special" dnodes have no parent */
0N/A
0N/A /*
0N/A * Lock the dnode handle to prevent the dnode from obtaining any new
0N/A * holds. This also prevents the descendant dbufs and the bonus dbuf
0N/A * from accessing the dnode, so that we can discount their holds. The
0N/A * handle is safe to access because we know that while the dnode cannot
0N/A * go away, neither can its handle. Once we hold dnh_zrlock, we can
0N/A * safely move any dnode referenced only by dbufs.
0N/A */
0N/A if (!zrl_tryenter(&odn->dn_handle->dnh_zrlock)) {
0N/A mutex_exit(&os->os_lock);
0N/A DNODE_STAT_ADD(dnode_move_stats.dms_dnode_handle);
0N/A return (KMEM_CBRC_LATER);
0N/A }
0N/A
0N/A /*
0N/A * Ensure a consistent view of the dnode's holds and the dnode's dbufs.
0N/A * We need to guarantee that there is a hold for every dbuf in order to
0N/A * determine whether the dnode is actively referenced. Falsely matching
0N/A * a dbuf to an active hold would lead to an unsafe move. It's possible
0N/A * that a thread already having an active dnode hold is about to add a
0N/A * dbuf, and we can't compare hold and dbuf counts while the add is in
0N/A * progress.
0N/A */
0N/A if (!rw_tryenter(&odn->dn_struct_rwlock, RW_WRITER)) {
0N/A zrl_exit(&odn->dn_handle->dnh_zrlock);
0N/A mutex_exit(&os->os_lock);
0N/A DNODE_STAT_ADD(dnode_move_stats.dms_dnode_rwlock);
0N/A return (KMEM_CBRC_LATER);
0N/A }
0N/A
0N/A /*
0N/A * A dbuf may be removed (evicted) without an active dnode hold. In that
0N/A * case, the dbuf count is decremented under the handle lock before the
0N/A * dbuf's hold is released. This order ensures that if we count the hold
0N/A * after the dbuf is removed but before its hold is released, we will
0N/A * treat the unmatched hold as active and exit safely. If we count the
0N/A * hold before the dbuf is removed, the hold is discounted, and the
0N/A * removal is blocked until the move completes.
1879N/A */
1879N/A refcount = refcount_count(&odn->dn_holds);
ASSERT(refcount >= 0);
dbufs = odn->dn_dbufs_count;
/* We can't have more dbufs than dnode holds. */
ASSERT3U(dbufs, <=, refcount);
DTRACE_PROBE3(dnode__move, dnode_t *, odn, int64_t, refcount,
uint32_t, dbufs);
if (refcount > dbufs) {
rw_exit(&odn->dn_struct_rwlock);
zrl_exit(&odn->dn_handle->dnh_zrlock);
mutex_exit(&os->os_lock);
DNODE_STAT_ADD(dnode_move_stats.dms_dnode_active);
return (KMEM_CBRC_LATER);
}
rw_exit(&odn->dn_struct_rwlock);
/*
* At this point we know that anyone with a hold on the dnode is not
* actively referencing it. The dnode is known and in a valid state to
* move. We're holding the locks needed to execute the critical section.
*/
dnode_move_impl(odn, ndn);
list_link_replace(&odn->dn_link, &ndn->dn_link);
/* If the dnode was safe to move, the refcount cannot have changed. */
ASSERT(refcount == refcount_count(&ndn->dn_holds));
ASSERT(dbufs == ndn->dn_dbufs_count);
zrl_exit(&ndn->dn_handle->dnh_zrlock); /* handle has moved */
mutex_exit(&os->os_lock);
return (KMEM_CBRC_YES);
}
#endif /* _KERNEL */
void
dnode_special_close(dnode_handle_t *dnh)
{
dnode_t *dn = dnh->dnh_dnode;
/*
* Wait for final references to the dnode to clear. This can
* only happen if the arc is asyncronously evicting state that
* has a hold on this dnode while we are trying to evict this
* dnode.
*/
while (refcount_count(&dn->dn_holds) > 0)
delay(1);
zrl_add(&dnh->dnh_zrlock);
dnode_destroy(dn); /* implicit zrl_remove() */
zrl_destroy(&dnh->dnh_zrlock);
dnh->dnh_dnode = NULL;
}
dnode_t *
dnode_special_open(objset_t *os, dnode_phys_t *dnp, uint64_t object,
dnode_handle_t *dnh)
{
dnode_t *dn = dnode_create(os, dnp, NULL, object, dnh);
dnh->dnh_dnode = dn;
zrl_init(&dnh->dnh_zrlock);
DNODE_VERIFY(dn);
return (dn);
}
static void
dnode_buf_pageout(dmu_buf_t *db, void *arg)
{
dnode_children_t *children_dnodes = arg;
int i;
int epb = db->db_size >> DNODE_SHIFT;
ASSERT(epb == children_dnodes->dnc_count);
for (i = 0; i < epb; i++) {
dnode_handle_t *dnh = &children_dnodes->dnc_children[i];
dnode_t *dn;
/*
* The dnode handle lock guards against the dnode moving to
* another valid address, so there is no need here to guard
* against changes to or from NULL.
*/
if (dnh->dnh_dnode == NULL) {
zrl_destroy(&dnh->dnh_zrlock);
continue;
}
zrl_add(&dnh->dnh_zrlock);
dn = dnh->dnh_dnode;
/*
* If there are holds on this dnode, then there should
* be holds on the dnode's containing dbuf as well; thus
* it wouldn't be eligible for eviction and this function
* would not have been called.
*/
ASSERT(refcount_is_zero(&dn->dn_holds));
ASSERT(refcount_is_zero(&dn->dn_tx_holds));
dnode_destroy(dn); /* implicit zrl_remove() */
zrl_destroy(&dnh->dnh_zrlock);
dnh->dnh_dnode = NULL;
}
kmem_free(children_dnodes, sizeof (dnode_children_t) +
epb * sizeof (dnode_handle_t));
}
/*
* errors:
* EINVAL - invalid object number.
* EIO - i/o error.
* succeeds even for free dnodes.
*/
int
dnode_hold_impl(objset_t *os, uint64_t object, int flag,
void *tag, dnode_t **dnp)
{
int epb, idx, err;
int drop_struct_lock = FALSE;
int type;
uint64_t blk;
dnode_t *mdn, *dn;
dmu_buf_impl_t *db;
dnode_children_t *children_dnodes;
dnode_handle_t *dnh;
/*
* If you are holding the spa config lock as writer, you shouldn't
* be asking the DMU to do *anything* unless it's the root pool
* which may require us to read from the root filesystem while
* holding some (not all) of the locks as writer.
*/
ASSERT(spa_config_held(os->os_spa, SCL_ALL, RW_WRITER) == 0 ||
(spa_is_root(os->os_spa) &&
spa_config_held(os->os_spa, SCL_STATE, RW_WRITER)));
if (object == DMU_USERUSED_OBJECT || object == DMU_GROUPUSED_OBJECT) {
dn = (object == DMU_USERUSED_OBJECT) ?
DMU_USERUSED_DNODE(os) : DMU_GROUPUSED_DNODE(os);
if (dn == NULL)
return (SET_ERROR(ENOENT));
type = dn->dn_type;
if ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE)
return (SET_ERROR(ENOENT));
if ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)
return (SET_ERROR(EEXIST));
DNODE_VERIFY(dn);
(void) refcount_add(&dn->dn_holds, tag);
*dnp = dn;
return (0);
}
if (object == 0 || object >= DN_MAX_OBJECT)
return (SET_ERROR(EINVAL));
mdn = DMU_META_DNODE(os);
ASSERT(mdn->dn_object == DMU_META_DNODE_OBJECT);
DNODE_VERIFY(mdn);
if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) {
rw_enter(&mdn->dn_struct_rwlock, RW_READER);
drop_struct_lock = TRUE;
}
blk = dbuf_whichblock(mdn, object * sizeof (dnode_phys_t));
db = dbuf_hold(mdn, blk, FTAG);
if (drop_struct_lock)
rw_exit(&mdn->dn_struct_rwlock);
if (db == NULL)
return (SET_ERROR(EIO));
err = dbuf_read(db, NULL, DB_RF_CANFAIL);
if (err) {
dbuf_rele(db, FTAG);
return (err);
}
ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT);
epb = db->db.db_size >> DNODE_SHIFT;
idx = object & (epb-1);
ASSERT(DB_DNODE(db)->dn_type == DMU_OT_DNODE);
children_dnodes = dmu_buf_get_user(&db->db);
if (children_dnodes == NULL) {
int i;
dnode_children_t *winner;
children_dnodes = kmem_alloc(sizeof (dnode_children_t) +
epb * sizeof (dnode_handle_t), KM_SLEEP);
children_dnodes->dnc_count = epb;
dnh = &children_dnodes->dnc_children[0];
for (i = 0; i < epb; i++) {
zrl_init(&dnh[i].dnh_zrlock);
dnh[i].dnh_dnode = NULL;
}
if (winner = dmu_buf_set_user(&db->db, children_dnodes, NULL,
dnode_buf_pageout)) {
for (i = 0; i < epb; i++) {
zrl_destroy(&dnh[i].dnh_zrlock);
}
kmem_free(children_dnodes, sizeof (dnode_children_t) +
epb * sizeof (dnode_handle_t));
children_dnodes = winner;
}
}
ASSERT(children_dnodes->dnc_count == epb);
dnh = &children_dnodes->dnc_children[idx];
zrl_add(&dnh->dnh_zrlock);
if ((dn = dnh->dnh_dnode) == NULL) {
dnode_phys_t *phys = (dnode_phys_t *)db->db.db_data+idx;
dnode_t *winner;
dn = dnode_create(os, phys, db, object, dnh);
winner = atomic_cas_ptr(&dnh->dnh_dnode, NULL, dn);
if (winner != NULL) {
zrl_add(&dnh->dnh_zrlock);
dnode_destroy(dn); /* implicit zrl_remove() */
dn = winner;
}
}
mutex_enter(&dn->dn_mtx);
type = dn->dn_type;
if (dn->dn_free_txg ||
((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) ||
((flag & DNODE_MUST_BE_FREE) &&
(type != DMU_OT_NONE || !refcount_is_zero(&dn->dn_holds)))) {
mutex_exit(&dn->dn_mtx);
zrl_remove(&dnh->dnh_zrlock);
dbuf_rele(db, FTAG);
return (type == DMU_OT_NONE ? ENOENT : EEXIST);
}
mutex_exit(&dn->dn_mtx);
if (refcount_add(&dn->dn_holds, tag) == 1)
dbuf_add_ref(db, dnh);
/* Now we can rely on the hold to prevent the dnode from moving. */
zrl_remove(&dnh->dnh_zrlock);
DNODE_VERIFY(dn);
ASSERT3P(dn->dn_dbuf, ==, db);
ASSERT3U(dn->dn_object, ==, object);
dbuf_rele(db, FTAG);
*dnp = dn;
return (0);
}
/*
* Return held dnode if the object is allocated, NULL if not.
*/
int
dnode_hold(objset_t *os, uint64_t object, void *tag, dnode_t **dnp)
{
return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, tag, dnp));
}
/*
* Can only add a reference if there is already at least one
* reference on the dnode. Returns FALSE if unable to add a
* new reference.
*/
boolean_t
dnode_add_ref(dnode_t *dn, void *tag)
{
mutex_enter(&dn->dn_mtx);
if (refcount_is_zero(&dn->dn_holds)) {
mutex_exit(&dn->dn_mtx);
return (FALSE);
}
VERIFY(1 < refcount_add(&dn->dn_holds, tag));
mutex_exit(&dn->dn_mtx);
return (TRUE);
}
void
dnode_rele(dnode_t *dn, void *tag)
{
uint64_t refs;
/* Get while the hold prevents the dnode from moving. */
dmu_buf_impl_t *db = dn->dn_dbuf;
dnode_handle_t *dnh = dn->dn_handle;
mutex_enter(&dn->dn_mtx);
refs = refcount_remove(&dn->dn_holds, tag);
mutex_exit(&dn->dn_mtx);
/*
* It's unsafe to release the last hold on a dnode by dnode_rele() or
* indirectly by dbuf_rele() while relying on the dnode handle to
* prevent the dnode from moving, since releasing the last hold could
* result in the dnode's parent dbuf evicting its dnode handles. For
* that reason anyone calling dnode_rele() or dbuf_rele() without some
* other direct or indirect hold on the dnode must first drop the dnode
* handle.
*/
ASSERT(refs > 0 || dnh->dnh_zrlock.zr_owner != curthread);
/* NOTE: the DNODE_DNODE does not have a dn_dbuf */
if (refs == 0 && db != NULL) {
/*
* Another thread could add a hold to the dnode handle in
* dnode_hold_impl() while holding the parent dbuf. Since the
* hold on the parent dbuf prevents the handle from being
* destroyed, the hold on the handle is OK. We can't yet assert
* that the handle has zero references, but that will be
* asserted anyway when the handle gets destroyed.
*/
dbuf_rele(db, dnh);
}
}
void
dnode_setdirty(dnode_t *dn, dmu_tx_t *tx)
{
objset_t *os = dn->dn_objset;
uint64_t txg = tx->tx_txg;
if (DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
dsl_dataset_dirty(os->os_dsl_dataset, tx);
return;
}
DNODE_VERIFY(dn);
#ifdef ZFS_DEBUG
mutex_enter(&dn->dn_mtx);
ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg);
ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg);
mutex_exit(&dn->dn_mtx);
#endif
/*
* Determine old uid/gid when necessary
*/
dmu_objset_userquota_get_ids(dn, B_TRUE, tx);
mutex_enter(&os->os_lock);
/*
* If we are already marked dirty, we're done.
*/
if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) {
mutex_exit(&os->os_lock);
return;
}
ASSERT(!refcount_is_zero(&dn->dn_holds) ||
!avl_is_empty(&dn->dn_dbufs));
ASSERT(dn->dn_datablksz != 0);
ASSERT0(dn->dn_next_bonuslen[txg&TXG_MASK]);
ASSERT0(dn->dn_next_blksz[txg&TXG_MASK]);
ASSERT0(dn->dn_next_bonustype[txg&TXG_MASK]);
dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
dn->dn_object, txg);
if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) {
list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn);
} else {
list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn);
}
mutex_exit(&os->os_lock);
/*
* The dnode maintains a hold on its containing dbuf as
* long as there are holds on it. Each instantiated child
* dbuf maintains a hold on the dnode. When the last child
* drops its hold, the dnode will drop its hold on the
* containing dbuf. We add a "dirty hold" here so that the
* dnode will hang around after we finish processing its
* children.
*/
VERIFY(dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg));
(void) dbuf_dirty(dn->dn_dbuf, tx);
dsl_dataset_dirty(os->os_dsl_dataset, tx);
}
void
dnode_free(dnode_t *dn, dmu_tx_t *tx)
{
int txgoff = tx->tx_txg & TXG_MASK;
dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg);
/* we should be the only holder... hopefully */
/* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */
mutex_enter(&dn->dn_mtx);
if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) {
mutex_exit(&dn->dn_mtx);
return;
}
dn->dn_free_txg = tx->tx_txg;
mutex_exit(&dn->dn_mtx);
/*
* If the dnode is already dirty, it needs to be moved from
* the dirty list to the free list.
*/
mutex_enter(&dn->dn_objset->os_lock);
if (list_link_active(&dn->dn_dirty_link[txgoff])) {
list_remove(&dn->dn_objset->os_dirty_dnodes[txgoff], dn);
list_insert_tail(&dn->dn_objset->os_free_dnodes[txgoff], dn);
mutex_exit(&dn->dn_objset->os_lock);
} else {
mutex_exit(&dn->dn_objset->os_lock);
dnode_setdirty(dn, tx);
}
}
/*
* Try to change the block size for the indicated dnode. This can only
* succeed if there are no blocks allocated or dirty beyond first block
*/
int
dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx)
{
dmu_buf_impl_t *db;
int err;
if (size == 0)
size = SPA_MINBLOCKSIZE;
if (size > SPA_MAXBLOCKSIZE)
size = SPA_MAXBLOCKSIZE;
else
size = P2ROUNDUP(size, SPA_MINBLOCKSIZE);
if (ibs == dn->dn_indblkshift)
ibs = 0;
if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0)
return (0);
rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
/* Check for any allocated blocks beyond the first */
if (dn->dn_maxblkid != 0)
goto fail;
mutex_enter(&dn->dn_dbufs_mtx);
for (db = avl_first(&dn->dn_dbufs); db != NULL;
db = AVL_NEXT(&dn->dn_dbufs, db)) {
if (db->db_blkid != 0 && db->db_blkid != DMU_BONUS_BLKID &&
db->db_blkid != DMU_SPILL_BLKID) {
mutex_exit(&dn->dn_dbufs_mtx);
goto fail;
}
}
mutex_exit(&dn->dn_dbufs_mtx);
if (ibs && dn->dn_nlevels != 1)
goto fail;
/* resize the old block */
err = dbuf_hold_impl(dn, 0, 0, TRUE, FTAG, &db);
if (err == 0)
dbuf_new_size(db, size, tx);
else if (err != ENOENT)
goto fail;
dnode_setdblksz(dn, size);
dnode_setdirty(dn, tx);
dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size;
if (ibs) {
dn->dn_indblkshift = ibs;
dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs;
}
/* rele after we have fixed the blocksize in the dnode */
if (db)
dbuf_rele(db, FTAG);
rw_exit(&dn->dn_struct_rwlock);
return (0);
fail:
rw_exit(&dn->dn_struct_rwlock);
return (SET_ERROR(ENOTSUP));
}
/* read-holding callers must not rely on the lock being continuously held */
void
dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t have_read)
{
uint64_t txgoff = tx->tx_txg & TXG_MASK;
int epbs, new_nlevels;
uint64_t sz;
ASSERT(blkid != DMU_BONUS_BLKID);
ASSERT(have_read ?
RW_READ_HELD(&dn->dn_struct_rwlock) :
RW_WRITE_HELD(&dn->dn_struct_rwlock));
/*
* if we have a read-lock, check to see if we need to do any work
* before upgrading to a write-lock.
*/
if (have_read) {
if (blkid <= dn->dn_maxblkid)
return;
if (!rw_tryupgrade(&dn->dn_struct_rwlock)) {
rw_exit(&dn->dn_struct_rwlock);
rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
}
}
if (blkid <= dn->dn_maxblkid)
goto out;
dn->dn_maxblkid = blkid;
/*
* Compute the number of levels necessary to support the new maxblkid.
*/
new_nlevels = 1;
epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
for (sz = dn->dn_nblkptr;
sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs)
new_nlevels++;
if (new_nlevels > dn->dn_nlevels) {
int old_nlevels = dn->dn_nlevels;
dmu_buf_impl_t *db;
list_t *list;
dbuf_dirty_record_t *new, *dr, *dr_next;
dn->dn_nlevels = new_nlevels;
ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]);
dn->dn_next_nlevels[txgoff] = new_nlevels;
/* dirty the left indirects */
db = dbuf_hold_level(dn, old_nlevels, 0, FTAG);
ASSERT(db != NULL);
new = dbuf_dirty(db, tx);
dbuf_rele(db, FTAG);
/* transfer the dirty records to the new indirect */
mutex_enter(&dn->dn_mtx);
mutex_enter(&new->dt.di.dr_mtx);
list = &dn->dn_dirty_records[txgoff];
for (dr = list_head(list); dr; dr = dr_next) {
dr_next = list_next(&dn->dn_dirty_records[txgoff], dr);
if (dr->dr_dbuf->db_level != new_nlevels-1 &&
dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
ASSERT(dr->dr_dbuf->db_level == old_nlevels-1);
list_remove(&dn->dn_dirty_records[txgoff], dr);
list_insert_tail(&new->dt.di.dr_children, dr);
dr->dr_parent = new;
}
}
mutex_exit(&new->dt.di.dr_mtx);
mutex_exit(&dn->dn_mtx);
}
out:
if (have_read)
rw_downgrade(&dn->dn_struct_rwlock);
}
void
dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx)
{
dmu_buf_impl_t *db;
uint64_t blkoff, blkid, nblks;
int blksz, blkshift, head, tail;
int trunc = FALSE;
int epbs;
rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
blksz = dn->dn_datablksz;
blkshift = dn->dn_datablkshift;
epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
if (len == DMU_OBJECT_END) {
len = UINT64_MAX - off;
trunc = TRUE;
}
/*
* First, block align the region to free:
*/
if (ISP2(blksz)) {
head = P2NPHASE(off, blksz);
blkoff = P2PHASE(off, blksz);
if ((off >> blkshift) > dn->dn_maxblkid)
goto out;
} else {
ASSERT(dn->dn_maxblkid == 0);
if (off == 0 && len >= blksz) {
/*
* Freeing the whole block; fast-track this request.
* Note that we won't dirty any indirect blocks,
* which is fine because we will be freeing the entire
* file and thus all indirect blocks will be freed
* by free_children().
*/
blkid = 0;
nblks = 1;
goto done;
} else if (off >= blksz) {
/* Freeing past end-of-data */
goto out;
} else {
/* Freeing part of the block. */
head = blksz - off;
ASSERT3U(head, >, 0);
}
blkoff = off;
}
/* zero out any partial block data at the start of the range */
if (head) {
ASSERT3U(blkoff + head, ==, blksz);
if (len < head)
head = len;
if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off), TRUE,
FTAG, &db) == 0) {
caddr_t data;
/* don't dirty if it isn't on disk and isn't dirty */
if (db->db_last_dirty ||
(db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
rw_exit(&dn->dn_struct_rwlock);
dmu_buf_will_dirty(&db->db, tx);
rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
data = db->db.db_data;
bzero(data + blkoff, head);
}
dbuf_rele(db, FTAG);
}
off += head;
len -= head;
}
/* If the range was less than one block, we're done */
if (len == 0)
goto out;
/* If the remaining range is past end of file, we're done */
if ((off >> blkshift) > dn->dn_maxblkid)
goto out;
ASSERT(ISP2(blksz));
if (trunc)
tail = 0;
else
tail = P2PHASE(len, blksz);
ASSERT0(P2PHASE(off, blksz));
/* zero out any partial block data at the end of the range */
if (tail) {
if (len < tail)
tail = len;
if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len),
TRUE, FTAG, &db) == 0) {
/* don't dirty if not on disk and not dirty */
if (db->db_last_dirty ||
(db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
rw_exit(&dn->dn_struct_rwlock);
dmu_buf_will_dirty(&db->db, tx);
rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
bzero(db->db.db_data, tail);
}
dbuf_rele(db, FTAG);
}
len -= tail;
}
/* If the range did not include a full block, we are done */
if (len == 0)
goto out;
ASSERT(IS_P2ALIGNED(off, blksz));
ASSERT(trunc || IS_P2ALIGNED(len, blksz));
blkid = off >> blkshift;
nblks = len >> blkshift;
if (trunc)
nblks += 1;
/*
* Dirty the first and last indirect blocks, as they (and/or their
* parents) will need to be written out if they were only
* partially freed. Interior indirect blocks will be themselves freed,
* by free_children(), so they need not be dirtied. Note that these
* interior blocks have already been prefetched by dmu_tx_hold_free().
*/
if (dn->dn_nlevels > 1) {
uint64_t first, last;
first = blkid >> epbs;
if (db = dbuf_hold_level(dn, 1, first, FTAG)) {
dmu_buf_will_dirty(&db->db, tx);
dbuf_rele(db, FTAG);
}
if (trunc)
last = dn->dn_maxblkid >> epbs;
else
last = (blkid + nblks - 1) >> epbs;
if (last > first && (db = dbuf_hold_level(dn, 1, last, FTAG))) {
dmu_buf_will_dirty(&db->db, tx);
dbuf_rele(db, FTAG);
}
}
done:
/*
* Add this range to the dnode range list.
* We will finish up this free operation in the syncing phase.
*/
mutex_enter(&dn->dn_mtx);
int txgoff = tx->tx_txg & TXG_MASK;
if (dn->dn_free_ranges[txgoff] == NULL) {
dn->dn_free_ranges[txgoff] =
range_tree_create(NULL, NULL, &dn->dn_mtx);
}
range_tree_clear(dn->dn_free_ranges[txgoff], blkid, nblks);
range_tree_add(dn->dn_free_ranges[txgoff], blkid, nblks);
dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
blkid, nblks, tx->tx_txg);
mutex_exit(&dn->dn_mtx);
dbuf_free_range(dn, blkid, blkid + nblks - 1, tx);
dnode_setdirty(dn, tx);
out:
rw_exit(&dn->dn_struct_rwlock);
}
static boolean_t
dnode_spill_freed(dnode_t *dn)
{
int i;
mutex_enter(&dn->dn_mtx);
for (i = 0; i < TXG_SIZE; i++) {
if (dn->dn_rm_spillblk[i] == DN_KILL_SPILLBLK)
break;
}
mutex_exit(&dn->dn_mtx);
return (i < TXG_SIZE);
}
/* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
uint64_t
dnode_block_freed(dnode_t *dn, uint64_t blkid)
{
void *dp = spa_get_dsl(dn->dn_objset->os_spa);
int i;
if (blkid == DMU_BONUS_BLKID)
return (FALSE);
/*
* If we're in the process of opening the pool, dp will not be
* set yet, but there shouldn't be anything dirty.
*/
if (dp == NULL)
return (FALSE);
if (dn->dn_free_txg)
return (TRUE);
if (blkid == DMU_SPILL_BLKID)
return (dnode_spill_freed(dn));
mutex_enter(&dn->dn_mtx);
for (i = 0; i < TXG_SIZE; i++) {
if (dn->dn_free_ranges[i] != NULL &&
range_tree_contains(dn->dn_free_ranges[i], blkid, 1))
break;
}
mutex_exit(&dn->dn_mtx);
return (i < TXG_SIZE);
}
/* call from syncing context when we actually write/free space for this dnode */
void
dnode_diduse_space(dnode_t *dn, int64_t delta)
{
uint64_t space;
dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n",
dn, dn->dn_phys,
(u_longlong_t)dn->dn_phys->dn_used,
(longlong_t)delta);
mutex_enter(&dn->dn_mtx);
space = DN_USED_BYTES(dn->dn_phys);
if (delta > 0) {
ASSERT3U(space + delta, >=, space); /* no overflow */
} else {
ASSERT3U(space, >=, -delta); /* no underflow */
}
space += delta;
if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_DNODE_BYTES) {
ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0);
ASSERT0(P2PHASE(space, 1<<DEV_BSHIFT));
dn->dn_phys->dn_used = space >> DEV_BSHIFT;
} else {
dn->dn_phys->dn_used = space;
dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES;
}
mutex_exit(&dn->dn_mtx);
}
/*
* Call when we think we're going to write/free space in open context to track
* the amount of memory in use by the currently open txg.
*/
void
dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx)
{
objset_t *os = dn->dn_objset;
dsl_dataset_t *ds = os->os_dsl_dataset;
int64_t aspace = spa_get_asize(os->os_spa, space);
if (ds != NULL) {
dsl_dir_willuse_space(ds->ds_dir, aspace, tx);
dsl_pool_dirty_space(dmu_tx_pool(tx), space, tx);
}
dmu_tx_willuse_space(tx, aspace);
}
/*
* Scans a block at the indicated "level" looking for a hole or data,
* depending on 'flags'.
*
* If level > 0, then we are scanning an indirect block looking at its
* pointers. If level == 0, then we are looking at a block of dnodes.
*
* If we don't find what we are looking for in the block, we return ESRCH.
* Otherwise, return with *offset pointing to the beginning (if searching
* forwards) or end (if searching backwards) of the range covered by the
* block pointer we matched on (or dnode).
*
* The basic search algorithm used below by dnode_next_offset() is to
* use this function to search up the block tree (widen the search) until
* we find something (i.e., we don't return ESRCH) and then search back
* down the tree (narrow the search) until we reach our original search
* level.
*/
static int
dnode_next_offset_level(dnode_t *dn, int flags, uint64_t *offset,
int lvl, uint64_t blkfill, uint64_t txg)
{
dmu_buf_impl_t *db = NULL;
void *data = NULL;
uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
uint64_t epb = 1ULL << epbs;
uint64_t minfill, maxfill;
boolean_t hole;
int i, inc, error, span;
dprintf("probing object %llu offset %llx level %d of %u\n",
dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels);
hole = ((flags & DNODE_FIND_HOLE) != 0);
inc = (flags & DNODE_FIND_BACKWARDS) ? -1 : 1;
ASSERT(txg == 0 || !hole);
if (lvl == dn->dn_phys->dn_nlevels) {
error = 0;
epb = dn->dn_phys->dn_nblkptr;
data = dn->dn_phys->dn_blkptr;
} else {
uint64_t blkid = dbuf_whichblock(dn, *offset) >> (epbs * lvl);
error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FTAG, &db);
if (error) {
if (error != ENOENT)
return (error);
if (hole)
return (0);
/*
* This can only happen when we are searching up
* the block tree for data. We don't really need to
* adjust the offset, as we will just end up looking
* at the pointer to this block in its parent, and its
* going to be unallocated, so we will skip over it.
*/
return (SET_ERROR(ESRCH));
}
error = dbuf_read(db, NULL, DB_RF_CANFAIL | DB_RF_HAVESTRUCT);
if (error) {
dbuf_rele(db, FTAG);
return (error);
}
data = db->db.db_data;
}
if (db != NULL && txg != 0 && (db->db_blkptr == NULL ||
db->db_blkptr->blk_birth <= txg ||
BP_IS_HOLE(db->db_blkptr))) {
/*
* This can only happen when we are searching up the tree
* and these conditions mean that we need to keep climbing.
*/
error = SET_ERROR(ESRCH);
} else if (lvl == 0) {
dnode_phys_t *dnp = data;
span = DNODE_SHIFT;
ASSERT(dn->dn_type == DMU_OT_DNODE);
for (i = (*offset >> span) & (blkfill - 1);
i >= 0 && i < blkfill; i += inc) {
if ((dnp[i].dn_type == DMU_OT_NONE) == hole)
break;
*offset += (1ULL << span) * inc;
}
if (i < 0 || i == blkfill)
error = SET_ERROR(ESRCH);
} else {
blkptr_t *bp = data;
uint64_t start = *offset;
span = (lvl - 1) * epbs + dn->dn_datablkshift;
minfill = 0;
maxfill = blkfill << ((lvl - 1) * epbs);
if (hole)
maxfill--;
else
minfill++;
*offset = *offset >> span;
for (i = BF64_GET(*offset, 0, epbs);
i >= 0 && i < epb; i += inc) {
if (BP_GET_FILL(&bp[i]) >= minfill &&
BP_GET_FILL(&bp[i]) <= maxfill &&
(hole || bp[i].blk_birth > txg))
break;
if (inc > 0 || *offset > 0)
*offset += inc;
}
*offset = *offset << span;
if (inc < 0) {
/* traversing backwards; position offset at the end */
ASSERT3U(*offset, <=, start);
*offset = MIN(*offset + (1ULL << span) - 1, start);
} else if (*offset < start) {
*offset = start;
}
if (i < 0 || i >= epb)
error = SET_ERROR(ESRCH);
}
if (db)
dbuf_rele(db, FTAG);
return (error);
}
/*
* Find the next hole, data, or sparse region at or after *offset.
* The value 'blkfill' tells us how many items we expect to find
* in an L0 data block; this value is 1 for normal objects,
* DNODES_PER_BLOCK for the meta dnode, and some fraction of
* DNODES_PER_BLOCK when searching for sparse regions thereof.
*
* Examples:
*
* dnode_next_offset(dn, flags, offset, 1, 1, 0);
* Finds the next/previous hole/data in a file.
* Used in dmu_offset_next().
*
* dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
* Finds the next free/allocated dnode an objset's meta-dnode.
* Only finds objects that have new contents since txg (ie.
* bonus buffer changes and content removal are ignored).
* Used in dmu_object_next().
*
* dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
* Finds the next L2 meta-dnode bp that's at most 1/4 full.
* Used in dmu_object_alloc().
*/
int
dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset,
int minlvl, uint64_t blkfill, uint64_t txg)
{
uint64_t initial_offset = *offset;
int lvl, maxlvl;
int error = 0;
if (!(flags & DNODE_FIND_HAVELOCK))
rw_enter(&dn->dn_struct_rwlock, RW_READER);
if (dn->dn_phys->dn_nlevels == 0) {
error = SET_ERROR(ESRCH);
goto out;
}
if (dn->dn_datablkshift == 0) {
if (*offset < dn->dn_datablksz) {
if (flags & DNODE_FIND_HOLE)
*offset = dn->dn_datablksz;
} else {
error = SET_ERROR(ESRCH);
}
goto out;
}
maxlvl = dn->dn_phys->dn_nlevels;
for (lvl = minlvl; lvl <= maxlvl; lvl++) {
error = dnode_next_offset_level(dn,
flags, offset, lvl, blkfill, txg);
if (error != ESRCH)
break;
}
while (error == 0 && --lvl >= minlvl) {
error = dnode_next_offset_level(dn,
flags, offset, lvl, blkfill, txg);
}
/*
* There's always a "virtual hole" at the end of the object, even
* if all BP's which physically exist are non-holes.
*/
if ((flags & DNODE_FIND_HOLE) && error == ESRCH && txg == 0 &&
minlvl == 1 && blkfill == 1 && !(flags & DNODE_FIND_BACKWARDS)) {
error = 0;
}
if (error == 0 && (flags & DNODE_FIND_BACKWARDS ?
initial_offset < *offset : initial_offset > *offset))
error = SET_ERROR(ESRCH);
out:
if (!(flags & DNODE_FIND_HAVELOCK))
rw_exit(&dn->dn_struct_rwlock);
return (error);
}