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. 23286ef713SPaul Dagnelie * Copyright (c) 2013, 2015 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 35fa9e4066Sahrens dmu_object_alloc(objset_t *os, dmu_object_type_t ot, int blocksize, 36fa9e4066Sahrens dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 37fa9e4066Sahrens { 38fa9e4066Sahrens uint64_t object; 39af346df5SNed Bass uint64_t L1_dnode_count = DNODES_PER_BLOCK << 40744947dcSTom Erickson (DMU_META_DNODE(os)->dn_indblkshift - SPA_BLKPTRSHIFT); 41ea8dc4b6Seschrock dnode_t *dn = NULL; 42fa9e4066Sahrens 43503ad85cSMatthew Ahrens mutex_enter(&os->os_obj_lock); 44fa9e4066Sahrens for (;;) { 45503ad85cSMatthew Ahrens object = os->os_obj_next; 46fa9e4066Sahrens /* 47af346df5SNed Bass * Each time we polish off a L1 bp worth of dnodes (2^12 48af346df5SNed Bass * objects), move to another L1 bp that's still reasonably 49af346df5SNed Bass * sparse (at most 1/4 full). Look from the beginning at most 50af346df5SNed Bass * once per txg, but after that keep looking from here. 51af346df5SNed Bass * os_scan_dnodes is set during txg sync if enough objects 52af346df5SNed Bass * have been freed since the previous rescan to justify 53af346df5SNed Bass * backfilling again. If we can't find a suitable block, just 54af346df5SNed Bass * keep going from here. 55286ef713SPaul Dagnelie * 56286ef713SPaul Dagnelie * Note that dmu_traverse depends on the behavior that we use 57286ef713SPaul Dagnelie * multiple blocks of the dnode object before going back to 58286ef713SPaul Dagnelie * reuse objects. Any change to this algorithm should preserve 59286ef713SPaul Dagnelie * that property or find another solution to the issues 60286ef713SPaul Dagnelie * described in traverse_visitbp. 61fa9e4066Sahrens */ 62af346df5SNed Bass 63af346df5SNed Bass if (P2PHASE(object, L1_dnode_count) == 0) { 64af346df5SNed Bass uint64_t offset; 65af346df5SNed Bass int error; 66af346df5SNed Bass if (os->os_rescan_dnodes) { 67af346df5SNed Bass offset = 0; 68af346df5SNed Bass os->os_rescan_dnodes = B_FALSE; 69af346df5SNed Bass } else { 70af346df5SNed Bass offset = object << DNODE_SHIFT; 71af346df5SNed Bass } 72af346df5SNed Bass error = dnode_next_offset(DMU_META_DNODE(os), 73cdb0ab79Smaybee DNODE_FIND_HOLE, 74cdb0ab79Smaybee &offset, 2, DNODES_PER_BLOCK >> 2, 0); 75fa9e4066Sahrens if (error == 0) 76fa9e4066Sahrens object = offset >> DNODE_SHIFT; 77fa9e4066Sahrens } 78503ad85cSMatthew Ahrens os->os_obj_next = ++object; 79fa9e4066Sahrens 80ea8dc4b6Seschrock /* 81ea8dc4b6Seschrock * XXX We should check for an i/o error here and return 82ea8dc4b6Seschrock * up to our caller. Actually we should pre-read it in 83ea8dc4b6Seschrock * dmu_tx_assign(), but there is currently no mechanism 84ea8dc4b6Seschrock * to do so. 85ea8dc4b6Seschrock */ 86503ad85cSMatthew Ahrens (void) dnode_hold_impl(os, object, DNODE_MUST_BE_FREE, 87ea8dc4b6Seschrock FTAG, &dn); 88fa9e4066Sahrens if (dn) 89fa9e4066Sahrens break; 90fa9e4066Sahrens 916754306eSahrens if (dmu_object_next(os, &object, B_TRUE, 0) == 0) 92503ad85cSMatthew Ahrens os->os_obj_next = object - 1; 93fa9e4066Sahrens } 94fa9e4066Sahrens 95fa9e4066Sahrens dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx); 96503ad85cSMatthew Ahrens mutex_exit(&os->os_obj_lock); 97fa9e4066Sahrens 98*b0c42cd4Sbzzz dmu_tx_add_new_object(tx, dn); 99*b0c42cd4Sbzzz dnode_rele(dn, FTAG); 100*b0c42cd4Sbzzz 101fa9e4066Sahrens return (object); 102fa9e4066Sahrens } 103fa9e4066Sahrens 104fa9e4066Sahrens int 105fa9e4066Sahrens dmu_object_claim(objset_t *os, uint64_t object, dmu_object_type_t ot, 106fa9e4066Sahrens int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 107fa9e4066Sahrens { 108fa9e4066Sahrens dnode_t *dn; 109ea8dc4b6Seschrock int err; 110fa9e4066Sahrens 111ea8dc4b6Seschrock if (object == DMU_META_DNODE_OBJECT && !dmu_tx_private_ok(tx)) 112be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF)); 113fa9e4066Sahrens 114503ad85cSMatthew Ahrens err = dnode_hold_impl(os, object, DNODE_MUST_BE_FREE, FTAG, &dn); 115ea8dc4b6Seschrock if (err) 116ea8dc4b6Seschrock return (err); 117fa9e4066Sahrens dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx); 118*b0c42cd4Sbzzz dmu_tx_add_new_object(tx, dn); 119*b0c42cd4Sbzzz 120fa9e4066Sahrens dnode_rele(dn, FTAG); 121fa9e4066Sahrens 122fa9e4066Sahrens return (0); 123fa9e4066Sahrens } 124fa9e4066Sahrens 125fa9e4066Sahrens int 126fa9e4066Sahrens dmu_object_reclaim(objset_t *os, uint64_t object, dmu_object_type_t ot, 127e77d42eaSMatthew Ahrens int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 128fa9e4066Sahrens { 129fa9e4066Sahrens dnode_t *dn; 130ea8dc4b6Seschrock int err; 131fa9e4066Sahrens 1322bf405a2SMark Maybee if (object == DMU_META_DNODE_OBJECT) 133be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF)); 134fa9e4066Sahrens 135503ad85cSMatthew Ahrens err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, 136ea8dc4b6Seschrock FTAG, &dn); 137ea8dc4b6Seschrock if (err) 138ea8dc4b6Seschrock return (err); 1392bf405a2SMark Maybee 140fa9e4066Sahrens dnode_reallocate(dn, ot, blocksize, bonustype, bonuslen, tx); 1412bf405a2SMark Maybee 142fa9e4066Sahrens dnode_rele(dn, FTAG); 143cf04dda1SMark Maybee return (err); 144fa9e4066Sahrens } 145fa9e4066Sahrens 146fa9e4066Sahrens int 147fa9e4066Sahrens dmu_object_free(objset_t *os, uint64_t object, dmu_tx_t *tx) 148fa9e4066Sahrens { 149fa9e4066Sahrens dnode_t *dn; 150ea8dc4b6Seschrock int err; 151fa9e4066Sahrens 152ea8dc4b6Seschrock ASSERT(object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx)); 153fa9e4066Sahrens 154503ad85cSMatthew Ahrens err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, 155ea8dc4b6Seschrock FTAG, &dn); 156ea8dc4b6Seschrock if (err) 157ea8dc4b6Seschrock return (err); 158fa9e4066Sahrens 159fa9e4066Sahrens ASSERT(dn->dn_type != DMU_OT_NONE); 160cdb0ab79Smaybee dnode_free_range(dn, 0, DMU_OBJECT_END, tx); 161fa9e4066Sahrens dnode_free(dn, tx); 162fa9e4066Sahrens dnode_rele(dn, FTAG); 163fa9e4066Sahrens 164fa9e4066Sahrens return (0); 165fa9e4066Sahrens } 166fa9e4066Sahrens 167a2cdcdd2SPaul Dagnelie /* 168a2cdcdd2SPaul Dagnelie * Return (in *objectp) the next object which is allocated (or a hole) 169a2cdcdd2SPaul Dagnelie * after *object, taking into account only objects that may have been modified 170a2cdcdd2SPaul Dagnelie * after the specified txg. 171a2cdcdd2SPaul Dagnelie */ 172fa9e4066Sahrens int 1736754306eSahrens dmu_object_next(objset_t *os, uint64_t *objectp, boolean_t hole, uint64_t txg) 174fa9e4066Sahrens { 175fa9e4066Sahrens uint64_t offset = (*objectp + 1) << DNODE_SHIFT; 176fa9e4066Sahrens int error; 177fa9e4066Sahrens 178744947dcSTom Erickson error = dnode_next_offset(DMU_META_DNODE(os), 179cdb0ab79Smaybee (hole ? DNODE_FIND_HOLE : 0), &offset, 0, DNODES_PER_BLOCK, txg); 180fa9e4066Sahrens 181fa9e4066Sahrens *objectp = offset >> DNODE_SHIFT; 182fa9e4066Sahrens 183fa9e4066Sahrens return (error); 184fa9e4066Sahrens } 1852acef22dSMatthew Ahrens 1862acef22dSMatthew Ahrens /* 1872acef22dSMatthew Ahrens * Turn this object from old_type into DMU_OTN_ZAP_METADATA, and bump the 1882acef22dSMatthew Ahrens * refcount on SPA_FEATURE_EXTENSIBLE_DATASET. 1892acef22dSMatthew Ahrens * 1902acef22dSMatthew Ahrens * Only for use from syncing context, on MOS objects. 1912acef22dSMatthew Ahrens */ 1922acef22dSMatthew Ahrens void 1932acef22dSMatthew Ahrens dmu_object_zapify(objset_t *mos, uint64_t object, dmu_object_type_t old_type, 1942acef22dSMatthew Ahrens dmu_tx_t *tx) 1952acef22dSMatthew Ahrens { 1962acef22dSMatthew Ahrens dnode_t *dn; 1972acef22dSMatthew Ahrens 1982acef22dSMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 1992acef22dSMatthew Ahrens 2002acef22dSMatthew Ahrens VERIFY0(dnode_hold(mos, object, FTAG, &dn)); 2012acef22dSMatthew Ahrens if (dn->dn_type == DMU_OTN_ZAP_METADATA) { 2022acef22dSMatthew Ahrens dnode_rele(dn, FTAG); 2032acef22dSMatthew Ahrens return; 2042acef22dSMatthew Ahrens } 2052acef22dSMatthew Ahrens ASSERT3U(dn->dn_type, ==, old_type); 2062acef22dSMatthew Ahrens ASSERT0(dn->dn_maxblkid); 2072acef22dSMatthew Ahrens dn->dn_next_type[tx->tx_txg & TXG_MASK] = dn->dn_type = 2082acef22dSMatthew Ahrens DMU_OTN_ZAP_METADATA; 2092acef22dSMatthew Ahrens dnode_setdirty(dn, tx); 2102acef22dSMatthew Ahrens dnode_rele(dn, FTAG); 2112acef22dSMatthew Ahrens 2122acef22dSMatthew Ahrens mzap_create_impl(mos, object, 0, 0, tx); 2132acef22dSMatthew Ahrens 2142acef22dSMatthew Ahrens spa_feature_incr(dmu_objset_spa(mos), 2152acef22dSMatthew Ahrens SPA_FEATURE_EXTENSIBLE_DATASET, tx); 2162acef22dSMatthew Ahrens } 2172acef22dSMatthew Ahrens 2182acef22dSMatthew Ahrens void 2192acef22dSMatthew Ahrens dmu_object_free_zapified(objset_t *mos, uint64_t object, dmu_tx_t *tx) 2202acef22dSMatthew Ahrens { 2212acef22dSMatthew Ahrens dnode_t *dn; 2222acef22dSMatthew Ahrens dmu_object_type_t t; 2232acef22dSMatthew Ahrens 2242acef22dSMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 2252acef22dSMatthew Ahrens 2262acef22dSMatthew Ahrens VERIFY0(dnode_hold(mos, object, FTAG, &dn)); 2272acef22dSMatthew Ahrens t = dn->dn_type; 2282acef22dSMatthew Ahrens dnode_rele(dn, FTAG); 2292acef22dSMatthew Ahrens 2302acef22dSMatthew Ahrens if (t == DMU_OTN_ZAP_METADATA) { 2312acef22dSMatthew Ahrens spa_feature_decr(dmu_objset_spa(mos), 2322acef22dSMatthew Ahrens SPA_FEATURE_EXTENSIBLE_DATASET, tx); 2332acef22dSMatthew Ahrens } 2342acef22dSMatthew Ahrens VERIFY0(dmu_object_free(mos, object, tx)); 2352acef22dSMatthew Ahrens } 236