fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER START
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * The contents of this file are subject to the terms of the
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * Common Development and Distribution License (the "License").
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * You may not use this file except in compliance with the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fa9e4066f08beec538e775443c5be79dd423fcabahrens * or http://www.opensolaris.org/os/licensing.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * See the License for the specific language governing permissions
fa9e4066f08beec538e775443c5be79dd423fcabahrens * and limitations under the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * When distributing Covered Code, include this CDDL HEADER in each
fa9e4066f08beec538e775443c5be79dd423fcabahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If applicable, add the following below this CDDL HEADER, with the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * fields enclosed by brackets "[]" replaced with your own identifying
fa9e4066f08beec538e775443c5be79dd423fcabahrens * information: Portions Copyright [yyyy] [name of copyright owner]
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER END
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
06e0070d70ba2ee95f5aa2645423eb2cf1546788Mark Shellenbaum * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
286ef71398fb54b1d5007d6f45aa4320a9e0ede2Paul Dagnelie * Copyright (c) 2013, 2015 by Delphix. All rights reserved.
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens * Copyright 2014 HybridCluster. All rights reserved.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/dmu.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/dmu_objset.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/dmu_tx.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/dnode.h>
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens#include <sys/zap.h>
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens#include <sys/zfeature.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensuint64_t
fa9e4066f08beec538e775443c5be79dd423fcabahrensdmu_object_alloc(objset_t *os, dmu_object_type_t ot, int blocksize,
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens uint64_t object;
af346df58864e8fe897b1ff1a3a4c12f9294391bNed Bass uint64_t L1_dnode_count = DNODES_PER_BLOCK <<
744947dc83c634d985ed3ad79ac9c5e28d1865fdTom Erickson (DMU_META_DNODE(os)->dn_indblkshift - SPA_BLKPTRSHIFT);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock dnode_t *dn = NULL;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens mutex_enter(&os->os_obj_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens for (;;) {
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens object = os->os_obj_next;
fa9e4066f08beec538e775443c5be79dd423fcabahrens /*
af346df58864e8fe897b1ff1a3a4c12f9294391bNed Bass * Each time we polish off a L1 bp worth of dnodes (2^12
af346df58864e8fe897b1ff1a3a4c12f9294391bNed Bass * objects), move to another L1 bp that's still reasonably
af346df58864e8fe897b1ff1a3a4c12f9294391bNed Bass * sparse (at most 1/4 full). Look from the beginning at most
af346df58864e8fe897b1ff1a3a4c12f9294391bNed Bass * once per txg, but after that keep looking from here.
af346df58864e8fe897b1ff1a3a4c12f9294391bNed Bass * os_scan_dnodes is set during txg sync if enough objects
af346df58864e8fe897b1ff1a3a4c12f9294391bNed Bass * have been freed since the previous rescan to justify
af346df58864e8fe897b1ff1a3a4c12f9294391bNed Bass * backfilling again. If we can't find a suitable block, just
af346df58864e8fe897b1ff1a3a4c12f9294391bNed Bass * keep going from here.
286ef71398fb54b1d5007d6f45aa4320a9e0ede2Paul Dagnelie *
286ef71398fb54b1d5007d6f45aa4320a9e0ede2Paul Dagnelie * Note that dmu_traverse depends on the behavior that we use
286ef71398fb54b1d5007d6f45aa4320a9e0ede2Paul Dagnelie * multiple blocks of the dnode object before going back to
286ef71398fb54b1d5007d6f45aa4320a9e0ede2Paul Dagnelie * reuse objects. Any change to this algorithm should preserve
286ef71398fb54b1d5007d6f45aa4320a9e0ede2Paul Dagnelie * that property or find another solution to the issues
286ef71398fb54b1d5007d6f45aa4320a9e0ede2Paul Dagnelie * described in traverse_visitbp.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
af346df58864e8fe897b1ff1a3a4c12f9294391bNed Bass
af346df58864e8fe897b1ff1a3a4c12f9294391bNed Bass if (P2PHASE(object, L1_dnode_count) == 0) {
af346df58864e8fe897b1ff1a3a4c12f9294391bNed Bass uint64_t offset;
af346df58864e8fe897b1ff1a3a4c12f9294391bNed Bass int error;
af346df58864e8fe897b1ff1a3a4c12f9294391bNed Bass if (os->os_rescan_dnodes) {
af346df58864e8fe897b1ff1a3a4c12f9294391bNed Bass offset = 0;
af346df58864e8fe897b1ff1a3a4c12f9294391bNed Bass os->os_rescan_dnodes = B_FALSE;
af346df58864e8fe897b1ff1a3a4c12f9294391bNed Bass } else {
af346df58864e8fe897b1ff1a3a4c12f9294391bNed Bass offset = object << DNODE_SHIFT;
af346df58864e8fe897b1ff1a3a4c12f9294391bNed Bass }
af346df58864e8fe897b1ff1a3a4c12f9294391bNed Bass error = dnode_next_offset(DMU_META_DNODE(os),
cdb0ab79ea1af7b8fc339a04d4bf7426dc77ec4emaybee DNODE_FIND_HOLE,
cdb0ab79ea1af7b8fc339a04d4bf7426dc77ec4emaybee &offset, 2, DNODES_PER_BLOCK >> 2, 0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (error == 0)
fa9e4066f08beec538e775443c5be79dd423fcabahrens object = offset >> DNODE_SHIFT;
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens os->os_obj_next = ++object;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock /*
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * XXX We should check for an i/o error here and return
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * up to our caller. Actually we should pre-read it in
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * dmu_tx_assign(), but there is currently no mechanism
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * to do so.
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock */
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens (void) dnode_hold_impl(os, object, DNODE_MUST_BE_FREE,
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock FTAG, &dn);
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (dn)
fa9e4066f08beec538e775443c5be79dd423fcabahrens break;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
6754306ec9a89fd28806908d10c76141e8fbba3fahrens if (dmu_object_next(os, &object, B_TRUE, 0) == 0)
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens os->os_obj_next = object - 1;
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
fa9e4066f08beec538e775443c5be79dd423fcabahrens dnode_rele(dn, FTAG);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens mutex_exit(&os->os_obj_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_tx_add_new_object(tx, os, object);
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (object);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensint
fa9e4066f08beec538e775443c5be79dd423fcabahrensdmu_object_claim(objset_t *os, uint64_t object, dmu_object_type_t ot,
fa9e4066f08beec538e775443c5be79dd423fcabahrens int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens dnode_t *dn;
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock int err;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock if (object == DMU_META_DNODE_OBJECT && !dmu_tx_private_ok(tx))
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EBADF));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens err = dnode_hold_impl(os, object, DNODE_MUST_BE_FREE, FTAG, &dn);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock if (err)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock return (err);
fa9e4066f08beec538e775443c5be79dd423fcabahrens dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
fa9e4066f08beec538e775443c5be79dd423fcabahrens dnode_rele(dn, FTAG);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_tx_add_new_object(tx, os, object);
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensint
fa9e4066f08beec538e775443c5be79dd423fcabahrensdmu_object_reclaim(objset_t *os, uint64_t object, dmu_object_type_t ot,
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens dnode_t *dn;
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock int err;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
2bf405a25eb25f79638fc951ff8d8857ad384417Mark Maybee if (object == DMU_META_DNODE_OBJECT)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EBADF));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock FTAG, &dn);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock if (err)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock return (err);
2bf405a25eb25f79638fc951ff8d8857ad384417Mark Maybee
fa9e4066f08beec538e775443c5be79dd423fcabahrens dnode_reallocate(dn, ot, blocksize, bonustype, bonuslen, tx);
2bf405a25eb25f79638fc951ff8d8857ad384417Mark Maybee
fa9e4066f08beec538e775443c5be79dd423fcabahrens dnode_rele(dn, FTAG);
cf04dda189f8e7f1d3245be8e387757e07c78e66Mark Maybee return (err);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensint
fa9e4066f08beec538e775443c5be79dd423fcabahrensdmu_object_free(objset_t *os, uint64_t object, dmu_tx_t *tx)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens dnode_t *dn;
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock int err;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock ASSERT(object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock FTAG, &dn);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock if (err)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock return (err);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(dn->dn_type != DMU_OT_NONE);
cdb0ab79ea1af7b8fc339a04d4bf7426dc77ec4emaybee dnode_free_range(dn, 0, DMU_OBJECT_END, tx);
fa9e4066f08beec538e775443c5be79dd423fcabahrens dnode_free(dn, tx);
fa9e4066f08beec538e775443c5be79dd423fcabahrens dnode_rele(dn, FTAG);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie/*
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * Return (in *objectp) the next object which is allocated (or a hole)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * after *object, taking into account only objects that may have been modified
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * after the specified txg.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie */
fa9e4066f08beec538e775443c5be79dd423fcabahrensint
6754306ec9a89fd28806908d10c76141e8fbba3fahrensdmu_object_next(objset_t *os, uint64_t *objectp, boolean_t hole, uint64_t txg)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens uint64_t offset = (*objectp + 1) << DNODE_SHIFT;
fa9e4066f08beec538e775443c5be79dd423fcabahrens int error;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
744947dc83c634d985ed3ad79ac9c5e28d1865fdTom Erickson error = dnode_next_offset(DMU_META_DNODE(os),
cdb0ab79ea1af7b8fc339a04d4bf7426dc77ec4emaybee (hole ? DNODE_FIND_HOLE : 0), &offset, 0, DNODES_PER_BLOCK, txg);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens *objectp = offset >> DNODE_SHIFT;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (error);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens/*
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens * Turn this object from old_type into DMU_OTN_ZAP_METADATA, and bump the
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens * refcount on SPA_FEATURE_EXTENSIBLE_DATASET.
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens *
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens * Only for use from syncing context, on MOS objects.
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens */
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrensvoid
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrensdmu_object_zapify(objset_t *mos, uint64_t object, dmu_object_type_t old_type,
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens dmu_tx_t *tx)
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens{
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens dnode_t *dn;
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens ASSERT(dmu_tx_is_syncing(tx));
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens VERIFY0(dnode_hold(mos, object, FTAG, &dn));
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens if (dn->dn_type == DMU_OTN_ZAP_METADATA) {
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens dnode_rele(dn, FTAG);
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens return;
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens }
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens ASSERT3U(dn->dn_type, ==, old_type);
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens ASSERT0(dn->dn_maxblkid);
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens dn->dn_next_type[tx->tx_txg & TXG_MASK] = dn->dn_type =
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens DMU_OTN_ZAP_METADATA;
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens dnode_setdirty(dn, tx);
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens dnode_rele(dn, FTAG);
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens mzap_create_impl(mos, object, 0, 0, tx);
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens spa_feature_incr(dmu_objset_spa(mos),
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens SPA_FEATURE_EXTENSIBLE_DATASET, tx);
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens}
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrensvoid
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrensdmu_object_free_zapified(objset_t *mos, uint64_t object, dmu_tx_t *tx)
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens{
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens dnode_t *dn;
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens dmu_object_type_t t;
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens ASSERT(dmu_tx_is_syncing(tx));
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens VERIFY0(dnode_hold(mos, object, FTAG, &dn));
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens t = dn->dn_type;
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens dnode_rele(dn, FTAG);
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens if (t == DMU_OTN_ZAP_METADATA) {
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens spa_feature_decr(dmu_objset_spa(mos),
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens SPA_FEATURE_EXTENSIBLE_DATASET, tx);
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens }
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens VERIFY0(dmu_object_free(mos, object, tx));
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens}