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.
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.
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]
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.
28 #include <sys/dmu_objset.h>
29 #include <sys/dmu_tx.h>
30 #include <sys/dnode.h>
32 #include <sys/zfeature.h>
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)
40 uint64_t L1_dnode_count = DNODES_PER_BLOCK <<
41 (DMU_META_DNODE(os)->dn_indblkshift - SPA_BLKPTRSHIFT);
44 mutex_enter(&os->os_obj_lock);
46 object = os->os_obj_next;
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.
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.
64 if (P2PHASE(object, L1_dnode_count) == 0) {
67 if (os->os_rescan_dnodes) {
69 os->os_rescan_dnodes = B_FALSE;
71 offset = object << DNODE_SHIFT;
73 error = dnode_next_offset(DMU_META_DNODE(os),
75 &offset, 2, DNODES_PER_BLOCK >> 2, 0);
77 object = offset >> DNODE_SHIFT;
79 os->os_obj_next = ++object;
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
87 (void) dnode_hold_impl(os, object, DNODE_MUST_BE_FREE,
92 if (dmu_object_next(os, &object, B_TRUE, 0) == 0)
93 os->os_obj_next = object - 1;
96 dnode_allocate(dn, ot, blocksize, indirect_blockshift,
97 bonustype, bonuslen, tx);
98 mutex_exit(&os->os_obj_lock);
100 dmu_tx_add_new_object(tx, dn);
101 dnode_rele(dn, FTAG);
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)
110 return (dmu_object_alloc_ibs(os, ot, blocksize, 0,
111 bonustype, bonuslen, tx));
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)
121 if (object == DMU_META_DNODE_OBJECT && !dmu_tx_private_ok(tx))
122 return (SET_ERROR(EBADF));
124 err = dnode_hold_impl(os, object, DNODE_MUST_BE_FREE, FTAG, &dn);
127 dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
128 dmu_tx_add_new_object(tx, dn);
130 dnode_rele(dn, FTAG);
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)
142 if (object == DMU_META_DNODE_OBJECT)
143 return (SET_ERROR(EBADF));
145 err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
150 dnode_reallocate(dn, ot, blocksize, bonustype, bonuslen, tx);
152 dnode_rele(dn, FTAG);
157 dmu_object_free(objset_t *os, uint64_t object, dmu_tx_t *tx)
162 ASSERT(object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
164 err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
169 ASSERT(dn->dn_type != DMU_OT_NONE);
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.
174 dnode_free_range(dn, 0, DMU_OBJECT_END, tx);
176 dnode_rele(dn, FTAG);
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.
187 dmu_object_next(objset_t *os, uint64_t *objectp, boolean_t hole, uint64_t txg)
189 uint64_t offset = (*objectp + 1) << DNODE_SHIFT;
192 error = dnode_next_offset(DMU_META_DNODE(os),
193 (hole ? DNODE_FIND_HOLE : 0), &offset, 0, DNODES_PER_BLOCK, txg);
195 *objectp = offset >> DNODE_SHIFT;
201 * Turn this object from old_type into DMU_OTN_ZAP_METADATA, and bump the
202 * refcount on SPA_FEATURE_EXTENSIBLE_DATASET.
204 * Only for use from syncing context, on MOS objects.
207 dmu_object_zapify(objset_t *mos, uint64_t object, dmu_object_type_t old_type,
212 ASSERT(dmu_tx_is_syncing(tx));
214 VERIFY0(dnode_hold(mos, object, FTAG, &dn));
215 if (dn->dn_type == DMU_OTN_ZAP_METADATA) {
216 dnode_rele(dn, FTAG);
219 ASSERT3U(dn->dn_type, ==, old_type);
220 ASSERT0(dn->dn_maxblkid);
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.
227 mzap_create_impl(mos, object, 0, 0, tx);
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);
234 spa_feature_incr(dmu_objset_spa(mos),
235 SPA_FEATURE_EXTENSIBLE_DATASET, tx);
239 dmu_object_free_zapified(objset_t *mos, uint64_t object, dmu_tx_t *tx)
244 ASSERT(dmu_tx_is_syncing(tx));
246 VERIFY0(dnode_hold(mos, object, FTAG, &dn));
248 dnode_rele(dn, FTAG);
250 if (t == DMU_OTN_ZAP_METADATA) {
251 spa_feature_decr(dmu_objset_spa(mos),
252 SPA_FEATURE_EXTENSIBLE_DATASET, tx);
254 VERIFY0(dmu_object_free(mos, object, tx));