1 /* 2 * CDDL HEADER START 3 * 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. 7 * 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. 12 * 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] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright (c) 2013, 2017 by Delphix. All rights reserved. 24 * Copyright 2014 HybridCluster. All rights reserved. 25 */ 26 27 #include <sys/dmu.h> 28 #include <sys/dmu_objset.h> 29 #include <sys/dmu_tx.h> 30 #include <sys/dnode.h> 31 #include <sys/zap.h> 32 #include <sys/zfeature.h> 33 34 uint64_t 35 dmu_object_alloc_ibs(objset_t *os, dmu_object_type_t ot, int blocksize, 36 int indirect_blockshift, 37 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 38 { 39 uint64_t object; 40 uint64_t L1_dnode_count = DNODES_PER_BLOCK << 41 (DMU_META_DNODE(os)->dn_indblkshift - SPA_BLKPTRSHIFT); 42 dnode_t *dn = NULL; 43 44 mutex_enter(&os->os_obj_lock); 45 for (;;) { 46 object = os->os_obj_next; 47 /* 48 * Each time we polish off a L1 bp worth of dnodes (2^12 49 * objects), move to another L1 bp that's still reasonably 50 * sparse (at most 1/4 full). Look from the beginning at most 51 * once per txg, but after that keep looking from here. 52 * os_scan_dnodes is set during txg sync if enough objects 53 * have been freed since the previous rescan to justify 54 * backfilling again. If we can't find a suitable block, just 55 * keep going from here. 56 * 57 * Note that dmu_traverse depends on the behavior that we use 58 * multiple blocks of the dnode object before going back to 59 * reuse objects. Any change to this algorithm should preserve 60 * that property or find another solution to the issues 61 * described in traverse_visitbp. 62 */ 63 64 if (P2PHASE(object, L1_dnode_count) == 0) { 65 uint64_t offset; 66 int error; 67 if (os->os_rescan_dnodes) { 68 offset = 0; 69 os->os_rescan_dnodes = B_FALSE; 70 } else { 71 offset = object << DNODE_SHIFT; 72 } 73 error = dnode_next_offset(DMU_META_DNODE(os), 74 DNODE_FIND_HOLE, 75 &offset, 2, DNODES_PER_BLOCK >> 2, 0); 76 if (error == 0) 77 object = offset >> DNODE_SHIFT; 78 } 79 os->os_obj_next = ++object; 80 81 /* 82 * XXX We should check for an i/o error here and return 83 * up to our caller. Actually we should pre-read it in 84 * dmu_tx_assign(), but there is currently no mechanism 85 * to do so. 86 */ 87 (void) dnode_hold_impl(os, object, DNODE_MUST_BE_FREE, 88 FTAG, &dn); 89 if (dn) 90 break; 91 92 if (dmu_object_next(os, &object, B_TRUE, 0) == 0) 93 os->os_obj_next = object - 1; 94 } 95 96 dnode_allocate(dn, ot, blocksize, indirect_blockshift, 97 bonustype, bonuslen, tx); 98 mutex_exit(&os->os_obj_lock); 99 100 dmu_tx_add_new_object(tx, dn); 101 dnode_rele(dn, FTAG); 102 103 return (object); 104 } 105 106 uint64_t 107 dmu_object_alloc(objset_t *os, dmu_object_type_t ot, int blocksize, 108 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 109 { 110 return (dmu_object_alloc_ibs(os, ot, blocksize, 0, 111 bonustype, bonuslen, tx)); 112 } 113 114 int 115 dmu_object_claim(objset_t *os, uint64_t object, dmu_object_type_t ot, 116 int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 117 { 118 dnode_t *dn; 119 int err; 120 121 if (object == DMU_META_DNODE_OBJECT && !dmu_tx_private_ok(tx)) 122 return (SET_ERROR(EBADF)); 123 124 err = dnode_hold_impl(os, object, DNODE_MUST_BE_FREE, FTAG, &dn); 125 if (err) 126 return (err); 127 dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx); 128 dmu_tx_add_new_object(tx, dn); 129 130 dnode_rele(dn, FTAG); 131 132 return (0); 133 } 134 135 int 136 dmu_object_reclaim(objset_t *os, uint64_t object, dmu_object_type_t ot, 137 int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 138 { 139 dnode_t *dn; 140 int err; 141 142 if (object == DMU_META_DNODE_OBJECT) 143 return (SET_ERROR(EBADF)); 144 145 err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, 146 FTAG, &dn); 147 if (err) 148 return (err); 149 150 dnode_reallocate(dn, ot, blocksize, bonustype, bonuslen, tx); 151 152 dnode_rele(dn, FTAG); 153 return (err); 154 } 155 156 int 157 dmu_object_free(objset_t *os, uint64_t object, dmu_tx_t *tx) 158 { 159 dnode_t *dn; 160 int err; 161 162 ASSERT(object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx)); 163 164 err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, 165 FTAG, &dn); 166 if (err) 167 return (err); 168 169 ASSERT(dn->dn_type != DMU_OT_NONE); 170 /* 171 * If we don't create this free range, we'll leak indirect blocks when 172 * we get to freeing the dnode in syncing context. 173 */ 174 dnode_free_range(dn, 0, DMU_OBJECT_END, tx); 175 dnode_free(dn, tx); 176 dnode_rele(dn, FTAG); 177 178 return (0); 179 } 180 181 /* 182 * Return (in *objectp) the next object which is allocated (or a hole) 183 * after *object, taking into account only objects that may have been modified 184 * after the specified txg. 185 */ 186 int 187 dmu_object_next(objset_t *os, uint64_t *objectp, boolean_t hole, uint64_t txg) 188 { 189 uint64_t offset = (*objectp + 1) << DNODE_SHIFT; 190 int error; 191 192 error = dnode_next_offset(DMU_META_DNODE(os), 193 (hole ? DNODE_FIND_HOLE : 0), &offset, 0, DNODES_PER_BLOCK, txg); 194 195 *objectp = offset >> DNODE_SHIFT; 196 197 return (error); 198 } 199 200 /* 201 * Turn this object from old_type into DMU_OTN_ZAP_METADATA, and bump the 202 * refcount on SPA_FEATURE_EXTENSIBLE_DATASET. 203 * 204 * Only for use from syncing context, on MOS objects. 205 */ 206 void 207 dmu_object_zapify(objset_t *mos, uint64_t object, dmu_object_type_t old_type, 208 dmu_tx_t *tx) 209 { 210 dnode_t *dn; 211 212 ASSERT(dmu_tx_is_syncing(tx)); 213 214 VERIFY0(dnode_hold(mos, object, FTAG, &dn)); 215 if (dn->dn_type == DMU_OTN_ZAP_METADATA) { 216 dnode_rele(dn, FTAG); 217 return; 218 } 219 ASSERT3U(dn->dn_type, ==, old_type); 220 ASSERT0(dn->dn_maxblkid); 221 222 /* 223 * We must initialize the ZAP data before changing the type, 224 * so that concurrent calls to *_is_zapified() can determine if 225 * the object has been completely zapified by checking the type. 226 */ 227 mzap_create_impl(mos, object, 0, 0, tx); 228 229 dn->dn_next_type[tx->tx_txg & TXG_MASK] = dn->dn_type = 230 DMU_OTN_ZAP_METADATA; 231 dnode_setdirty(dn, tx); 232 dnode_rele(dn, FTAG); 233 234 spa_feature_incr(dmu_objset_spa(mos), 235 SPA_FEATURE_EXTENSIBLE_DATASET, tx); 236 } 237 238 void 239 dmu_object_free_zapified(objset_t *mos, uint64_t object, dmu_tx_t *tx) 240 { 241 dnode_t *dn; 242 dmu_object_type_t t; 243 244 ASSERT(dmu_tx_is_syncing(tx)); 245 246 VERIFY0(dnode_hold(mos, object, FTAG, &dn)); 247 t = dn->dn_type; 248 dnode_rele(dn, FTAG); 249 250 if (t == DMU_OTN_ZAP_METADATA) { 251 spa_feature_decr(dmu_objset_spa(mos), 252 SPA_FEATURE_EXTENSIBLE_DATASET, tx); 253 } 254 VERIFY0(dmu_object_free(mos, object, tx)); 255 } 256