1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51544Seschrock * Common Development and Distribution License (the "License"). 61544Seschrock * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 22*6992Smaybee * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27789Sahrens 28789Sahrens #include <sys/dmu.h> 29789Sahrens #include <sys/dmu_objset.h> 30789Sahrens #include <sys/dmu_tx.h> 31789Sahrens #include <sys/dnode.h> 32789Sahrens 33789Sahrens uint64_t 34789Sahrens dmu_object_alloc(objset_t *os, dmu_object_type_t ot, int blocksize, 35789Sahrens dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 36789Sahrens { 37789Sahrens objset_impl_t *osi = os->os; 38789Sahrens uint64_t object; 39789Sahrens uint64_t L2_dnode_count = DNODES_PER_BLOCK << 40789Sahrens (osi->os_meta_dnode->dn_indblkshift - SPA_BLKPTRSHIFT); 411544Seschrock dnode_t *dn = NULL; 42789Sahrens int restarted = B_FALSE; 43789Sahrens 44789Sahrens mutex_enter(&osi->os_obj_lock); 45789Sahrens for (;;) { 46789Sahrens object = osi->os_obj_next; 47789Sahrens /* 48789Sahrens * Each time we polish off an L2 bp worth of dnodes 49789Sahrens * (2^13 objects), move to another L2 bp that's still 50789Sahrens * reasonably sparse (at most 1/4 full). Look from the 51789Sahrens * beginning once, but after that keep looking from here. 52789Sahrens * If we can't find one, just keep going from here. 53789Sahrens */ 54789Sahrens if (P2PHASE(object, L2_dnode_count) == 0) { 55789Sahrens uint64_t offset = restarted ? object << DNODE_SHIFT : 0; 56789Sahrens int error = dnode_next_offset(osi->os_meta_dnode, 57*6992Smaybee DNODE_FIND_HOLE, 58*6992Smaybee &offset, 2, DNODES_PER_BLOCK >> 2, 0); 59789Sahrens restarted = B_TRUE; 60789Sahrens if (error == 0) 61789Sahrens object = offset >> DNODE_SHIFT; 62789Sahrens } 63789Sahrens osi->os_obj_next = ++object; 64789Sahrens 651544Seschrock /* 661544Seschrock * XXX We should check for an i/o error here and return 671544Seschrock * up to our caller. Actually we should pre-read it in 681544Seschrock * dmu_tx_assign(), but there is currently no mechanism 691544Seschrock * to do so. 701544Seschrock */ 711544Seschrock (void) dnode_hold_impl(os->os, object, DNODE_MUST_BE_FREE, 721544Seschrock FTAG, &dn); 73789Sahrens if (dn) 74789Sahrens break; 75789Sahrens 763025Sahrens if (dmu_object_next(os, &object, B_TRUE, 0) == 0) 77789Sahrens osi->os_obj_next = object - 1; 78789Sahrens } 79789Sahrens 80789Sahrens dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx); 81789Sahrens dnode_rele(dn, FTAG); 82789Sahrens 83789Sahrens mutex_exit(&osi->os_obj_lock); 84789Sahrens 85789Sahrens dmu_tx_add_new_object(tx, os, object); 86789Sahrens return (object); 87789Sahrens } 88789Sahrens 89789Sahrens int 90789Sahrens dmu_object_claim(objset_t *os, uint64_t object, dmu_object_type_t ot, 91789Sahrens int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 92789Sahrens { 93789Sahrens dnode_t *dn; 941544Seschrock int err; 95789Sahrens 961544Seschrock if (object == DMU_META_DNODE_OBJECT && !dmu_tx_private_ok(tx)) 97789Sahrens return (EBADF); 98789Sahrens 991544Seschrock err = dnode_hold_impl(os->os, object, DNODE_MUST_BE_FREE, FTAG, &dn); 1001544Seschrock if (err) 1011544Seschrock return (err); 102789Sahrens dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx); 103789Sahrens dnode_rele(dn, FTAG); 104789Sahrens 105789Sahrens dmu_tx_add_new_object(tx, os, object); 106789Sahrens return (0); 107789Sahrens } 108789Sahrens 109789Sahrens int 110789Sahrens dmu_object_reclaim(objset_t *os, uint64_t object, dmu_object_type_t ot, 111789Sahrens int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 112789Sahrens { 113789Sahrens dnode_t *dn; 1141544Seschrock int err; 115789Sahrens 1161544Seschrock if (object == DMU_META_DNODE_OBJECT && !dmu_tx_private_ok(tx)) 117789Sahrens return (EBADF); 118789Sahrens 1191544Seschrock err = dnode_hold_impl(os->os, object, DNODE_MUST_BE_ALLOCATED, 1201544Seschrock FTAG, &dn); 1211544Seschrock if (err) 1221544Seschrock return (err); 123789Sahrens dnode_reallocate(dn, ot, blocksize, bonustype, bonuslen, tx); 124789Sahrens dnode_rele(dn, FTAG); 125789Sahrens 126789Sahrens return (0); 127789Sahrens } 128789Sahrens 129789Sahrens int 130789Sahrens dmu_object_free(objset_t *os, uint64_t object, dmu_tx_t *tx) 131789Sahrens { 132789Sahrens dnode_t *dn; 1331544Seschrock int err; 134789Sahrens 1351544Seschrock ASSERT(object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx)); 136789Sahrens 1371544Seschrock err = dnode_hold_impl(os->os, object, DNODE_MUST_BE_ALLOCATED, 1381544Seschrock FTAG, &dn); 1391544Seschrock if (err) 1401544Seschrock return (err); 141789Sahrens 142789Sahrens ASSERT(dn->dn_type != DMU_OT_NONE); 143*6992Smaybee dnode_free_range(dn, 0, DMU_OBJECT_END, tx); 144789Sahrens dnode_free(dn, tx); 145789Sahrens dnode_rele(dn, FTAG); 146789Sahrens 147789Sahrens return (0); 148789Sahrens } 149789Sahrens 150789Sahrens int 1513025Sahrens dmu_object_next(objset_t *os, uint64_t *objectp, boolean_t hole, uint64_t txg) 152789Sahrens { 153789Sahrens uint64_t offset = (*objectp + 1) << DNODE_SHIFT; 154789Sahrens int error; 155789Sahrens 156789Sahrens error = dnode_next_offset(os->os->os_meta_dnode, 157*6992Smaybee (hole ? DNODE_FIND_HOLE : 0), &offset, 0, DNODES_PER_BLOCK, txg); 158789Sahrens 159789Sahrens *objectp = offset >> DNODE_SHIFT; 160789Sahrens 161789Sahrens return (error); 162789Sahrens } 163