1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 2206e0070dSMark Shellenbaum * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23bf26014cSMatthew Ahrens * Copyright (c) 2013, 2017 by Delphix. All rights reserved. 24e77d42eaSMatthew Ahrens * Copyright 2014 HybridCluster. All rights reserved. 25fa9e4066Sahrens */ 26fa9e4066Sahrens 27fa9e4066Sahrens #include <sys/dmu.h> 28fa9e4066Sahrens #include <sys/dmu_objset.h> 29fa9e4066Sahrens #include <sys/dmu_tx.h> 30fa9e4066Sahrens #include <sys/dnode.h> 312acef22dSMatthew Ahrens #include <sys/zap.h> 322acef22dSMatthew Ahrens #include <sys/zfeature.h> 33fa9e4066Sahrens 34fa9e4066Sahrens uint64_t 35*221813c1SMatthew Ahrens dmu_object_alloc_ibs(objset_t *os, dmu_object_type_t ot, int blocksize, 36*221813c1SMatthew Ahrens int indirect_blockshift, 37fa9e4066Sahrens dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 38fa9e4066Sahrens { 39fa9e4066Sahrens uint64_t object; 40af346df5SNed Bass uint64_t L1_dnode_count = DNODES_PER_BLOCK << 41744947dcSTom Erickson (DMU_META_DNODE(os)->dn_indblkshift - SPA_BLKPTRSHIFT); 42ea8dc4b6Seschrock dnode_t *dn = NULL; 43fa9e4066Sahrens 44503ad85cSMatthew Ahrens mutex_enter(&os->os_obj_lock); 45fa9e4066Sahrens for (;;) { 46503ad85cSMatthew Ahrens object = os->os_obj_next; 47fa9e4066Sahrens /* 48af346df5SNed Bass * Each time we polish off a L1 bp worth of dnodes (2^12 49af346df5SNed Bass * objects), move to another L1 bp that's still reasonably 50af346df5SNed Bass * sparse (at most 1/4 full). Look from the beginning at most 51af346df5SNed Bass * once per txg, but after that keep looking from here. 52af346df5SNed Bass * os_scan_dnodes is set during txg sync if enough objects 53af346df5SNed Bass * have been freed since the previous rescan to justify 54af346df5SNed Bass * backfilling again. If we can't find a suitable block, just 55af346df5SNed Bass * keep going from here. 56286ef713SPaul Dagnelie * 57286ef713SPaul Dagnelie * Note that dmu_traverse depends on the behavior that we use 58286ef713SPaul Dagnelie * multiple blocks of the dnode object before going back to 59286ef713SPaul Dagnelie * reuse objects. Any change to this algorithm should preserve 60286ef713SPaul Dagnelie * that property or find another solution to the issues 61286ef713SPaul Dagnelie * described in traverse_visitbp. 62fa9e4066Sahrens */ 63af346df5SNed Bass 64af346df5SNed Bass if (P2PHASE(object, L1_dnode_count) == 0) { 65af346df5SNed Bass uint64_t offset; 66af346df5SNed Bass int error; 67af346df5SNed Bass if (os->os_rescan_dnodes) { 68af346df5SNed Bass offset = 0; 69af346df5SNed Bass os->os_rescan_dnodes = B_FALSE; 70af346df5SNed Bass } else { 71af346df5SNed Bass offset = object << DNODE_SHIFT; 72af346df5SNed Bass } 73af346df5SNed Bass error = dnode_next_offset(DMU_META_DNODE(os), 74cdb0ab79Smaybee DNODE_FIND_HOLE, 75cdb0ab79Smaybee &offset, 2, DNODES_PER_BLOCK >> 2, 0); 76fa9e4066Sahrens if (error == 0) 77fa9e4066Sahrens object = offset >> DNODE_SHIFT; 78fa9e4066Sahrens } 79503ad85cSMatthew Ahrens os->os_obj_next = ++object; 80fa9e4066Sahrens 81ea8dc4b6Seschrock /* 82ea8dc4b6Seschrock * XXX We should check for an i/o error here and return 83ea8dc4b6Seschrock * up to our caller. Actually we should pre-read it in 84ea8dc4b6Seschrock * dmu_tx_assign(), but there is currently no mechanism 85ea8dc4b6Seschrock * to do so. 86ea8dc4b6Seschrock */ 87503ad85cSMatthew Ahrens (void) dnode_hold_impl(os, object, DNODE_MUST_BE_FREE, 88ea8dc4b6Seschrock FTAG, &dn); 89fa9e4066Sahrens if (dn) 90fa9e4066Sahrens break; 91fa9e4066Sahrens 926754306eSahrens if (dmu_object_next(os, &object, B_TRUE, 0) == 0) 93503ad85cSMatthew Ahrens os->os_obj_next = object - 1; 94fa9e4066Sahrens } 95fa9e4066Sahrens 96*221813c1SMatthew Ahrens dnode_allocate(dn, ot, blocksize, indirect_blockshift, 97*221813c1SMatthew Ahrens bonustype, bonuslen, tx); 98503ad85cSMatthew Ahrens mutex_exit(&os->os_obj_lock); 99fa9e4066Sahrens 100b0c42cd4Sbzzz dmu_tx_add_new_object(tx, dn); 101b0c42cd4Sbzzz dnode_rele(dn, FTAG); 102b0c42cd4Sbzzz 103fa9e4066Sahrens return (object); 104fa9e4066Sahrens } 105fa9e4066Sahrens 106*221813c1SMatthew Ahrens uint64_t 107*221813c1SMatthew Ahrens dmu_object_alloc(objset_t *os, dmu_object_type_t ot, int blocksize, 108*221813c1SMatthew Ahrens dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 109*221813c1SMatthew Ahrens { 110*221813c1SMatthew Ahrens return (dmu_object_alloc_ibs(os, ot, blocksize, 0, 111*221813c1SMatthew Ahrens bonustype, bonuslen, tx)); 112*221813c1SMatthew Ahrens } 113*221813c1SMatthew Ahrens 114fa9e4066Sahrens int 115fa9e4066Sahrens dmu_object_claim(objset_t *os, uint64_t object, dmu_object_type_t ot, 116fa9e4066Sahrens int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 117fa9e4066Sahrens { 118fa9e4066Sahrens dnode_t *dn; 119ea8dc4b6Seschrock int err; 120fa9e4066Sahrens 121ea8dc4b6Seschrock if (object == DMU_META_DNODE_OBJECT && !dmu_tx_private_ok(tx)) 122be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF)); 123fa9e4066Sahrens 124503ad85cSMatthew Ahrens err = dnode_hold_impl(os, object, DNODE_MUST_BE_FREE, FTAG, &dn); 125ea8dc4b6Seschrock if (err) 126ea8dc4b6Seschrock return (err); 127fa9e4066Sahrens dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx); 128b0c42cd4Sbzzz dmu_tx_add_new_object(tx, dn); 129b0c42cd4Sbzzz 130fa9e4066Sahrens dnode_rele(dn, FTAG); 131fa9e4066Sahrens 132fa9e4066Sahrens return (0); 133fa9e4066Sahrens } 134fa9e4066Sahrens 135fa9e4066Sahrens int 136fa9e4066Sahrens dmu_object_reclaim(objset_t *os, uint64_t object, dmu_object_type_t ot, 137e77d42eaSMatthew Ahrens int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 138fa9e4066Sahrens { 139fa9e4066Sahrens dnode_t *dn; 140ea8dc4b6Seschrock int err; 141fa9e4066Sahrens 1422bf405a2SMark Maybee if (object == DMU_META_DNODE_OBJECT) 143be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF)); 144fa9e4066Sahrens 145503ad85cSMatthew Ahrens err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, 146ea8dc4b6Seschrock FTAG, &dn); 147ea8dc4b6Seschrock if (err) 148ea8dc4b6Seschrock return (err); 1492bf405a2SMark Maybee 150fa9e4066Sahrens dnode_reallocate(dn, ot, blocksize, bonustype, bonuslen, tx); 1512bf405a2SMark Maybee 152fa9e4066Sahrens dnode_rele(dn, FTAG); 153cf04dda1SMark Maybee return (err); 154fa9e4066Sahrens } 155fa9e4066Sahrens 156fa9e4066Sahrens int 157fa9e4066Sahrens dmu_object_free(objset_t *os, uint64_t object, dmu_tx_t *tx) 158fa9e4066Sahrens { 159fa9e4066Sahrens dnode_t *dn; 160ea8dc4b6Seschrock int err; 161fa9e4066Sahrens 162ea8dc4b6Seschrock ASSERT(object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx)); 163fa9e4066Sahrens 164503ad85cSMatthew Ahrens err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, 165ea8dc4b6Seschrock FTAG, &dn); 166ea8dc4b6Seschrock if (err) 167ea8dc4b6Seschrock return (err); 168fa9e4066Sahrens 169fa9e4066Sahrens ASSERT(dn->dn_type != DMU_OT_NONE); 170cdb0ab79Smaybee dnode_free_range(dn, 0, DMU_OBJECT_END, tx); 171fa9e4066Sahrens dnode_free(dn, tx); 172fa9e4066Sahrens dnode_rele(dn, FTAG); 173fa9e4066Sahrens 174fa9e4066Sahrens return (0); 175fa9e4066Sahrens } 176fa9e4066Sahrens 177a2cdcdd2SPaul Dagnelie /* 178a2cdcdd2SPaul Dagnelie * Return (in *objectp) the next object which is allocated (or a hole) 179a2cdcdd2SPaul Dagnelie * after *object, taking into account only objects that may have been modified 180a2cdcdd2SPaul Dagnelie * after the specified txg. 181a2cdcdd2SPaul Dagnelie */ 182fa9e4066Sahrens int 1836754306eSahrens dmu_object_next(objset_t *os, uint64_t *objectp, boolean_t hole, uint64_t txg) 184fa9e4066Sahrens { 185fa9e4066Sahrens uint64_t offset = (*objectp + 1) << DNODE_SHIFT; 186fa9e4066Sahrens int error; 187fa9e4066Sahrens 188744947dcSTom Erickson error = dnode_next_offset(DMU_META_DNODE(os), 189cdb0ab79Smaybee (hole ? DNODE_FIND_HOLE : 0), &offset, 0, DNODES_PER_BLOCK, txg); 190fa9e4066Sahrens 191fa9e4066Sahrens *objectp = offset >> DNODE_SHIFT; 192fa9e4066Sahrens 193fa9e4066Sahrens return (error); 194fa9e4066Sahrens } 1952acef22dSMatthew Ahrens 1962acef22dSMatthew Ahrens /* 1972acef22dSMatthew Ahrens * Turn this object from old_type into DMU_OTN_ZAP_METADATA, and bump the 1982acef22dSMatthew Ahrens * refcount on SPA_FEATURE_EXTENSIBLE_DATASET. 1992acef22dSMatthew Ahrens * 2002acef22dSMatthew Ahrens * Only for use from syncing context, on MOS objects. 2012acef22dSMatthew Ahrens */ 2022acef22dSMatthew Ahrens void 2032acef22dSMatthew Ahrens dmu_object_zapify(objset_t *mos, uint64_t object, dmu_object_type_t old_type, 2042acef22dSMatthew Ahrens dmu_tx_t *tx) 2052acef22dSMatthew Ahrens { 2062acef22dSMatthew Ahrens dnode_t *dn; 2072acef22dSMatthew Ahrens 2082acef22dSMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 2092acef22dSMatthew Ahrens 2102acef22dSMatthew Ahrens VERIFY0(dnode_hold(mos, object, FTAG, &dn)); 2112acef22dSMatthew Ahrens if (dn->dn_type == DMU_OTN_ZAP_METADATA) { 2122acef22dSMatthew Ahrens dnode_rele(dn, FTAG); 2132acef22dSMatthew Ahrens return; 2142acef22dSMatthew Ahrens } 2152acef22dSMatthew Ahrens ASSERT3U(dn->dn_type, ==, old_type); 2162acef22dSMatthew Ahrens ASSERT0(dn->dn_maxblkid); 217bf26014cSMatthew Ahrens 218bf26014cSMatthew Ahrens /* 219bf26014cSMatthew Ahrens * We must initialize the ZAP data before changing the type, 220bf26014cSMatthew Ahrens * so that concurrent calls to *_is_zapified() can determine if 221bf26014cSMatthew Ahrens * the object has been completely zapified by checking the type. 222bf26014cSMatthew Ahrens */ 223bf26014cSMatthew Ahrens mzap_create_impl(mos, object, 0, 0, tx); 224bf26014cSMatthew Ahrens 2252acef22dSMatthew Ahrens dn->dn_next_type[tx->tx_txg & TXG_MASK] = dn->dn_type = 2262acef22dSMatthew Ahrens DMU_OTN_ZAP_METADATA; 2272acef22dSMatthew Ahrens dnode_setdirty(dn, tx); 2282acef22dSMatthew Ahrens dnode_rele(dn, FTAG); 2292acef22dSMatthew Ahrens 2302acef22dSMatthew Ahrens spa_feature_incr(dmu_objset_spa(mos), 2312acef22dSMatthew Ahrens SPA_FEATURE_EXTENSIBLE_DATASET, tx); 2322acef22dSMatthew Ahrens } 2332acef22dSMatthew Ahrens 2342acef22dSMatthew Ahrens void 2352acef22dSMatthew Ahrens dmu_object_free_zapified(objset_t *mos, uint64_t object, dmu_tx_t *tx) 2362acef22dSMatthew Ahrens { 2372acef22dSMatthew Ahrens dnode_t *dn; 2382acef22dSMatthew Ahrens dmu_object_type_t t; 2392acef22dSMatthew Ahrens 2402acef22dSMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 2412acef22dSMatthew Ahrens 2422acef22dSMatthew Ahrens VERIFY0(dnode_hold(mos, object, FTAG, &dn)); 2432acef22dSMatthew Ahrens t = dn->dn_type; 2442acef22dSMatthew Ahrens dnode_rele(dn, FTAG); 2452acef22dSMatthew Ahrens 2462acef22dSMatthew Ahrens if (t == DMU_OTN_ZAP_METADATA) { 2472acef22dSMatthew Ahrens spa_feature_decr(dmu_objset_spa(mos), 2482acef22dSMatthew Ahrens SPA_FEATURE_EXTENSIBLE_DATASET, tx); 2492acef22dSMatthew Ahrens } 2502acef22dSMatthew Ahrens VERIFY0(dmu_object_free(mos, object, tx)); 2512acef22dSMatthew Ahrens } 252