1 789 ahrens /* 2 789 ahrens * CDDL HEADER START 3 789 ahrens * 4 789 ahrens * The contents of this file are subject to the terms of the 5 1544 eschrock * Common Development and Distribution License (the "License"). 6 1544 eschrock * You may not use this file except in compliance with the License. 7 789 ahrens * 8 789 ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 789 ahrens * or http://www.opensolaris.org/os/licensing. 10 789 ahrens * See the License for the specific language governing permissions 11 789 ahrens * and limitations under the License. 12 789 ahrens * 13 789 ahrens * When distributing Covered Code, include this CDDL HEADER in each 14 789 ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 789 ahrens * If applicable, add the following below this CDDL HEADER, with the 16 789 ahrens * fields enclosed by brackets "[]" replaced with your own identifying 17 789 ahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18 789 ahrens * 19 789 ahrens * CDDL HEADER END 20 789 ahrens */ 21 789 ahrens /* 22 8986 Mark * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 789 ahrens * Use is subject to license terms. 24 789 ahrens */ 25 789 ahrens 26 789 ahrens #include <sys/dmu.h> 27 789 ahrens #include <sys/dmu_objset.h> 28 789 ahrens #include <sys/dmu_tx.h> 29 789 ahrens #include <sys/dnode.h> 30 789 ahrens 31 789 ahrens uint64_t 32 789 ahrens dmu_object_alloc(objset_t *os, dmu_object_type_t ot, int blocksize, 33 789 ahrens dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 34 789 ahrens { 35 789 ahrens uint64_t object; 36 789 ahrens uint64_t L2_dnode_count = DNODES_PER_BLOCK << 37 10298 Matthew (os->os_meta_dnode->dn_indblkshift - SPA_BLKPTRSHIFT); 38 1544 eschrock dnode_t *dn = NULL; 39 789 ahrens int restarted = B_FALSE; 40 789 ahrens 41 10298 Matthew mutex_enter(&os->os_obj_lock); 42 789 ahrens for (;;) { 43 10298 Matthew object = os->os_obj_next; 44 789 ahrens /* 45 789 ahrens * Each time we polish off an L2 bp worth of dnodes 46 789 ahrens * (2^13 objects), move to another L2 bp that's still 47 789 ahrens * reasonably sparse (at most 1/4 full). Look from the 48 789 ahrens * beginning once, but after that keep looking from here. 49 789 ahrens * If we can't find one, just keep going from here. 50 789 ahrens */ 51 789 ahrens if (P2PHASE(object, L2_dnode_count) == 0) { 52 789 ahrens uint64_t offset = restarted ? object << DNODE_SHIFT : 0; 53 10298 Matthew int error = dnode_next_offset(os->os_meta_dnode, 54 6992 maybee DNODE_FIND_HOLE, 55 6992 maybee &offset, 2, DNODES_PER_BLOCK >> 2, 0); 56 789 ahrens restarted = B_TRUE; 57 789 ahrens if (error == 0) 58 789 ahrens object = offset >> DNODE_SHIFT; 59 789 ahrens } 60 10298 Matthew os->os_obj_next = ++object; 61 789 ahrens 62 1544 eschrock /* 63 1544 eschrock * XXX We should check for an i/o error here and return 64 1544 eschrock * up to our caller. Actually we should pre-read it in 65 1544 eschrock * dmu_tx_assign(), but there is currently no mechanism 66 1544 eschrock * to do so. 67 1544 eschrock */ 68 10298 Matthew (void) dnode_hold_impl(os, object, DNODE_MUST_BE_FREE, 69 1544 eschrock FTAG, &dn); 70 789 ahrens if (dn) 71 789 ahrens break; 72 789 ahrens 73 3025 ahrens if (dmu_object_next(os, &object, B_TRUE, 0) == 0) 74 10298 Matthew os->os_obj_next = object - 1; 75 789 ahrens } 76 789 ahrens 77 789 ahrens dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx); 78 789 ahrens dnode_rele(dn, FTAG); 79 789 ahrens 80 10298 Matthew mutex_exit(&os->os_obj_lock); 81 789 ahrens 82 789 ahrens dmu_tx_add_new_object(tx, os, object); 83 789 ahrens return (object); 84 789 ahrens } 85 789 ahrens 86 789 ahrens int 87 789 ahrens dmu_object_claim(objset_t *os, uint64_t object, dmu_object_type_t ot, 88 789 ahrens int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 89 789 ahrens { 90 789 ahrens dnode_t *dn; 91 1544 eschrock int err; 92 789 ahrens 93 1544 eschrock if (object == DMU_META_DNODE_OBJECT && !dmu_tx_private_ok(tx)) 94 789 ahrens return (EBADF); 95 789 ahrens 96 10298 Matthew err = dnode_hold_impl(os, object, DNODE_MUST_BE_FREE, FTAG, &dn); 97 1544 eschrock if (err) 98 1544 eschrock return (err); 99 789 ahrens dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx); 100 789 ahrens dnode_rele(dn, FTAG); 101 789 ahrens 102 789 ahrens dmu_tx_add_new_object(tx, os, object); 103 789 ahrens return (0); 104 789 ahrens } 105 789 ahrens 106 789 ahrens int 107 789 ahrens dmu_object_reclaim(objset_t *os, uint64_t object, dmu_object_type_t ot, 108 8986 Mark int blocksize, dmu_object_type_t bonustype, int bonuslen) 109 789 ahrens { 110 789 ahrens dnode_t *dn; 111 8986 Mark dmu_tx_t *tx; 112 8986 Mark int nblkptr; 113 1544 eschrock int err; 114 789 ahrens 115 8986 Mark if (object == DMU_META_DNODE_OBJECT) 116 789 ahrens return (EBADF); 117 789 ahrens 118 10298 Matthew err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, 119 1544 eschrock FTAG, &dn); 120 1544 eschrock if (err) 121 1544 eschrock return (err); 122 8986 Mark 123 8986 Mark if (dn->dn_type == ot && dn->dn_datablksz == blocksize && 124 8986 Mark dn->dn_bonustype == bonustype && dn->dn_bonuslen == bonuslen) { 125 8986 Mark /* nothing is changing, this is a noop */ 126 8986 Mark dnode_rele(dn, FTAG); 127 8986 Mark return (0); 128 8986 Mark } 129 8986 Mark 130 8986 Mark nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT); 131 8986 Mark 132 8986 Mark /* 133 8986 Mark * If we are losing blkptrs or changing the block size this must 134 8986 Mark * be a new file instance. We must clear out the previous file 135 8986 Mark * contents before we can change this type of metadata in the dnode. 136 8986 Mark */ 137 9299 Mark if (dn->dn_nblkptr > nblkptr || dn->dn_datablksz != blocksize) { 138 9299 Mark err = dmu_free_long_range(os, object, 0, DMU_OBJECT_END); 139 9299 Mark if (err) 140 9299 Mark goto out; 141 9299 Mark } 142 9299 Mark 143 9299 Mark tx = dmu_tx_create(os); 144 9299 Mark dmu_tx_hold_bonus(tx, object); 145 9299 Mark err = dmu_tx_assign(tx, TXG_WAIT); 146 9299 Mark if (err) { 147 9299 Mark dmu_tx_abort(tx); 148 9299 Mark goto out; 149 9299 Mark } 150 8986 Mark 151 789 ahrens dnode_reallocate(dn, ot, blocksize, bonustype, bonuslen, tx); 152 8986 Mark 153 8986 Mark dmu_tx_commit(tx); 154 9299 Mark out: 155 789 ahrens dnode_rele(dn, FTAG); 156 789 ahrens 157 9299 Mark return (err); 158 789 ahrens } 159 789 ahrens 160 789 ahrens int 161 789 ahrens dmu_object_free(objset_t *os, uint64_t object, dmu_tx_t *tx) 162 789 ahrens { 163 789 ahrens dnode_t *dn; 164 1544 eschrock int err; 165 789 ahrens 166 1544 eschrock ASSERT(object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx)); 167 789 ahrens 168 10298 Matthew err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, 169 1544 eschrock FTAG, &dn); 170 1544 eschrock if (err) 171 1544 eschrock return (err); 172 789 ahrens 173 789 ahrens ASSERT(dn->dn_type != DMU_OT_NONE); 174 6992 maybee dnode_free_range(dn, 0, DMU_OBJECT_END, tx); 175 789 ahrens dnode_free(dn, tx); 176 789 ahrens dnode_rele(dn, FTAG); 177 789 ahrens 178 789 ahrens return (0); 179 789 ahrens } 180 789 ahrens 181 789 ahrens int 182 3025 ahrens dmu_object_next(objset_t *os, uint64_t *objectp, boolean_t hole, uint64_t txg) 183 789 ahrens { 184 789 ahrens uint64_t offset = (*objectp + 1) << DNODE_SHIFT; 185 789 ahrens int error; 186 789 ahrens 187 10298 Matthew error = dnode_next_offset(os->os_meta_dnode, 188 6992 maybee (hole ? DNODE_FIND_HOLE : 0), &offset, 0, DNODES_PER_BLOCK, txg); 189 789 ahrens 190 789 ahrens *objectp = offset >> DNODE_SHIFT; 191 789 ahrens 192 789 ahrens return (error); 193 789 ahrens } 194