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.
23*ee88b7a2SPaul 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
dmu_object_alloc(objset_t * os,dmu_object_type_t ot,int blocksize,dmu_object_type_t bonustype,int bonuslen,dmu_tx_t * tx)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;
39fa9e4066Sahrens uint64_t L2_dnode_count = DNODES_PER_BLOCK <<
40744947dcSTom Erickson (DMU_META_DNODE(os)->dn_indblkshift - SPA_BLKPTRSHIFT);
41ea8dc4b6Seschrock dnode_t *dn = NULL;
42fa9e4066Sahrens int restarted = B_FALSE;
43fa9e4066Sahrens
44503ad85cSMatthew Ahrens mutex_enter(&os->os_obj_lock);
45fa9e4066Sahrens for (;;) {
46503ad85cSMatthew Ahrens object = os->os_obj_next;
47fa9e4066Sahrens /*
48fa9e4066Sahrens * Each time we polish off an L2 bp worth of dnodes
49fa9e4066Sahrens * (2^13 objects), move to another L2 bp that's still
50fa9e4066Sahrens * reasonably sparse (at most 1/4 full). Look from the
51fa9e4066Sahrens * beginning once, but after that keep looking from here.
52fa9e4066Sahrens * If we can't find one, just keep going from here.
53*ee88b7a2SPaul Dagnelie *
54*ee88b7a2SPaul Dagnelie * Note that dmu_traverse depends on the behavior that we use
55*ee88b7a2SPaul Dagnelie * multiple blocks of the dnode object before going back to
56*ee88b7a2SPaul Dagnelie * reuse objects. Any change to this algorithm should preserve
57*ee88b7a2SPaul Dagnelie * that property or find another solution to the issues described
58*ee88b7a2SPaul Dagnelie * in traverse_visitbp.
59fa9e4066Sahrens */
60fa9e4066Sahrens if (P2PHASE(object, L2_dnode_count) == 0) {
61fa9e4066Sahrens uint64_t offset = restarted ? object << DNODE_SHIFT : 0;
62744947dcSTom Erickson int error = dnode_next_offset(DMU_META_DNODE(os),
63cdb0ab79Smaybee DNODE_FIND_HOLE,
64cdb0ab79Smaybee &offset, 2, DNODES_PER_BLOCK >> 2, 0);
65fa9e4066Sahrens restarted = B_TRUE;
66fa9e4066Sahrens if (error == 0)
67fa9e4066Sahrens object = offset >> DNODE_SHIFT;
68fa9e4066Sahrens }
69503ad85cSMatthew Ahrens os->os_obj_next = ++object;
70fa9e4066Sahrens
71ea8dc4b6Seschrock /*
72ea8dc4b6Seschrock * XXX We should check for an i/o error here and return
73ea8dc4b6Seschrock * up to our caller. Actually we should pre-read it in
74ea8dc4b6Seschrock * dmu_tx_assign(), but there is currently no mechanism
75ea8dc4b6Seschrock * to do so.
76ea8dc4b6Seschrock */
77503ad85cSMatthew Ahrens (void) dnode_hold_impl(os, object, DNODE_MUST_BE_FREE,
78ea8dc4b6Seschrock FTAG, &dn);
79fa9e4066Sahrens if (dn)
80fa9e4066Sahrens break;
81fa9e4066Sahrens
826754306eSahrens if (dmu_object_next(os, &object, B_TRUE, 0) == 0)
83503ad85cSMatthew Ahrens os->os_obj_next = object - 1;
84fa9e4066Sahrens }
85fa9e4066Sahrens
86fa9e4066Sahrens dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
87fa9e4066Sahrens dnode_rele(dn, FTAG);
88fa9e4066Sahrens
89503ad85cSMatthew Ahrens mutex_exit(&os->os_obj_lock);
90fa9e4066Sahrens
91fa9e4066Sahrens dmu_tx_add_new_object(tx, os, object);
92fa9e4066Sahrens return (object);
93fa9e4066Sahrens }
94fa9e4066Sahrens
95fa9e4066Sahrens int
dmu_object_claim(objset_t * os,uint64_t object,dmu_object_type_t ot,int blocksize,dmu_object_type_t bonustype,int bonuslen,dmu_tx_t * tx)96fa9e4066Sahrens dmu_object_claim(objset_t *os, uint64_t object, dmu_object_type_t ot,
97fa9e4066Sahrens int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
98fa9e4066Sahrens {
99fa9e4066Sahrens dnode_t *dn;
100ea8dc4b6Seschrock int err;
101fa9e4066Sahrens
102ea8dc4b6Seschrock if (object == DMU_META_DNODE_OBJECT && !dmu_tx_private_ok(tx))
103be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF));
104fa9e4066Sahrens
105503ad85cSMatthew Ahrens err = dnode_hold_impl(os, object, DNODE_MUST_BE_FREE, FTAG, &dn);
106ea8dc4b6Seschrock if (err)
107ea8dc4b6Seschrock return (err);
108fa9e4066Sahrens dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
109fa9e4066Sahrens dnode_rele(dn, FTAG);
110fa9e4066Sahrens
111fa9e4066Sahrens dmu_tx_add_new_object(tx, os, object);
112fa9e4066Sahrens return (0);
113fa9e4066Sahrens }
114fa9e4066Sahrens
115fa9e4066Sahrens int
dmu_object_reclaim(objset_t * os,uint64_t object,dmu_object_type_t ot,int blocksize,dmu_object_type_t bonustype,int bonuslen,dmu_tx_t * tx)116fa9e4066Sahrens dmu_object_reclaim(objset_t *os, uint64_t object, dmu_object_type_t ot,
117e77d42eaSMatthew Ahrens int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
118fa9e4066Sahrens {
119fa9e4066Sahrens dnode_t *dn;
120ea8dc4b6Seschrock int err;
121fa9e4066Sahrens
1222bf405a2SMark Maybee if (object == DMU_META_DNODE_OBJECT)
123be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF));
124fa9e4066Sahrens
125503ad85cSMatthew Ahrens err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
126ea8dc4b6Seschrock FTAG, &dn);
127ea8dc4b6Seschrock if (err)
128ea8dc4b6Seschrock return (err);
1292bf405a2SMark Maybee
130fa9e4066Sahrens dnode_reallocate(dn, ot, blocksize, bonustype, bonuslen, tx);
1312bf405a2SMark Maybee
132fa9e4066Sahrens dnode_rele(dn, FTAG);
133cf04dda1SMark Maybee return (err);
134fa9e4066Sahrens }
135fa9e4066Sahrens
136fa9e4066Sahrens int
dmu_object_free(objset_t * os,uint64_t object,dmu_tx_t * tx)137fa9e4066Sahrens dmu_object_free(objset_t *os, uint64_t object, dmu_tx_t *tx)
138fa9e4066Sahrens {
139fa9e4066Sahrens dnode_t *dn;
140ea8dc4b6Seschrock int err;
141fa9e4066Sahrens
142ea8dc4b6Seschrock ASSERT(object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
143fa9e4066Sahrens
144503ad85cSMatthew Ahrens err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
145ea8dc4b6Seschrock FTAG, &dn);
146ea8dc4b6Seschrock if (err)
147ea8dc4b6Seschrock return (err);
148fa9e4066Sahrens
149fa9e4066Sahrens ASSERT(dn->dn_type != DMU_OT_NONE);
150cdb0ab79Smaybee dnode_free_range(dn, 0, DMU_OBJECT_END, tx);
151fa9e4066Sahrens dnode_free(dn, tx);
152fa9e4066Sahrens dnode_rele(dn, FTAG);
153fa9e4066Sahrens
154fa9e4066Sahrens return (0);
155fa9e4066Sahrens }
156fa9e4066Sahrens
157a2cdcdd2SPaul Dagnelie /*
158a2cdcdd2SPaul Dagnelie * Return (in *objectp) the next object which is allocated (or a hole)
159a2cdcdd2SPaul Dagnelie * after *object, taking into account only objects that may have been modified
160a2cdcdd2SPaul Dagnelie * after the specified txg.
161a2cdcdd2SPaul Dagnelie */
162fa9e4066Sahrens int
dmu_object_next(objset_t * os,uint64_t * objectp,boolean_t hole,uint64_t txg)1636754306eSahrens dmu_object_next(objset_t *os, uint64_t *objectp, boolean_t hole, uint64_t txg)
164fa9e4066Sahrens {
165fa9e4066Sahrens uint64_t offset = (*objectp + 1) << DNODE_SHIFT;
166fa9e4066Sahrens int error;
167fa9e4066Sahrens
168744947dcSTom Erickson error = dnode_next_offset(DMU_META_DNODE(os),
169cdb0ab79Smaybee (hole ? DNODE_FIND_HOLE : 0), &offset, 0, DNODES_PER_BLOCK, txg);
170fa9e4066Sahrens
171fa9e4066Sahrens *objectp = offset >> DNODE_SHIFT;
172fa9e4066Sahrens
173fa9e4066Sahrens return (error);
174fa9e4066Sahrens }
1752acef22dSMatthew Ahrens
1762acef22dSMatthew Ahrens /*
1772acef22dSMatthew Ahrens * Turn this object from old_type into DMU_OTN_ZAP_METADATA, and bump the
1782acef22dSMatthew Ahrens * refcount on SPA_FEATURE_EXTENSIBLE_DATASET.
1792acef22dSMatthew Ahrens *
1802acef22dSMatthew Ahrens * Only for use from syncing context, on MOS objects.
1812acef22dSMatthew Ahrens */
1822acef22dSMatthew Ahrens void
dmu_object_zapify(objset_t * mos,uint64_t object,dmu_object_type_t old_type,dmu_tx_t * tx)1832acef22dSMatthew Ahrens dmu_object_zapify(objset_t *mos, uint64_t object, dmu_object_type_t old_type,
1842acef22dSMatthew Ahrens dmu_tx_t *tx)
1852acef22dSMatthew Ahrens {
1862acef22dSMatthew Ahrens dnode_t *dn;
1872acef22dSMatthew Ahrens
1882acef22dSMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx));
1892acef22dSMatthew Ahrens
1902acef22dSMatthew Ahrens VERIFY0(dnode_hold(mos, object, FTAG, &dn));
1912acef22dSMatthew Ahrens if (dn->dn_type == DMU_OTN_ZAP_METADATA) {
1922acef22dSMatthew Ahrens dnode_rele(dn, FTAG);
1932acef22dSMatthew Ahrens return;
1942acef22dSMatthew Ahrens }
1952acef22dSMatthew Ahrens ASSERT3U(dn->dn_type, ==, old_type);
1962acef22dSMatthew Ahrens ASSERT0(dn->dn_maxblkid);
1972acef22dSMatthew Ahrens dn->dn_next_type[tx->tx_txg & TXG_MASK] = dn->dn_type =
1982acef22dSMatthew Ahrens DMU_OTN_ZAP_METADATA;
1992acef22dSMatthew Ahrens dnode_setdirty(dn, tx);
2002acef22dSMatthew Ahrens dnode_rele(dn, FTAG);
2012acef22dSMatthew Ahrens
2022acef22dSMatthew Ahrens mzap_create_impl(mos, object, 0, 0, tx);
2032acef22dSMatthew Ahrens
2042acef22dSMatthew Ahrens spa_feature_incr(dmu_objset_spa(mos),
2052acef22dSMatthew Ahrens SPA_FEATURE_EXTENSIBLE_DATASET, tx);
2062acef22dSMatthew Ahrens }
2072acef22dSMatthew Ahrens
2082acef22dSMatthew Ahrens void
dmu_object_free_zapified(objset_t * mos,uint64_t object,dmu_tx_t * tx)2092acef22dSMatthew Ahrens dmu_object_free_zapified(objset_t *mos, uint64_t object, dmu_tx_t *tx)
2102acef22dSMatthew Ahrens {
2112acef22dSMatthew Ahrens dnode_t *dn;
2122acef22dSMatthew Ahrens dmu_object_type_t t;
2132acef22dSMatthew Ahrens
2142acef22dSMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx));
2152acef22dSMatthew Ahrens
2162acef22dSMatthew Ahrens VERIFY0(dnode_hold(mos, object, FTAG, &dn));
2172acef22dSMatthew Ahrens t = dn->dn_type;
2182acef22dSMatthew Ahrens dnode_rele(dn, FTAG);
2192acef22dSMatthew Ahrens
2202acef22dSMatthew Ahrens if (t == DMU_OTN_ZAP_METADATA) {
2212acef22dSMatthew Ahrens spa_feature_decr(dmu_objset_spa(mos),
2222acef22dSMatthew Ahrens SPA_FEATURE_EXTENSIBLE_DATASET, tx);
2232acef22dSMatthew Ahrens }
2242acef22dSMatthew Ahrens VERIFY0(dmu_object_free(mos, object, tx));
2252acef22dSMatthew Ahrens }
226