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, 2015 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(objset_t
*os
, dmu_object_type_t ot
, int blocksize
,
36 dmu_object_type_t bonustype
, int bonuslen
, dmu_tx_t
*tx
)
39 uint64_t L2_dnode_count
= DNODES_PER_BLOCK
<<
40 (DMU_META_DNODE(os
)->dn_indblkshift
- SPA_BLKPTRSHIFT
);
42 int restarted
= B_FALSE
;
44 mutex_enter(&os
->os_obj_lock
);
46 object
= os
->os_obj_next
;
48 * Each time we polish off an L2 bp worth of dnodes
49 * (2^13 objects), move to another L2 bp that's still
50 * reasonably sparse (at most 1/4 full). Look from the
51 * beginning once, but after that keep looking from here.
52 * If we can't find one, just keep going from here.
54 * Note that dmu_traverse depends on the behavior that we use
55 * multiple blocks of the dnode object before going back to
56 * reuse objects. Any change to this algorithm should preserve
57 * that property or find another solution to the issues
58 * described in traverse_visitbp.
60 if (P2PHASE(object
, L2_dnode_count
) == 0) {
61 uint64_t offset
= restarted
? object
<< DNODE_SHIFT
: 0;
62 int error
= dnode_next_offset(DMU_META_DNODE(os
),
64 &offset
, 2, DNODES_PER_BLOCK
>> 2, 0);
67 object
= offset
>> DNODE_SHIFT
;
69 os
->os_obj_next
= ++object
;
72 * XXX We should check for an i/o error here and return
73 * up to our caller. Actually we should pre-read it in
74 * dmu_tx_assign(), but there is currently no mechanism
77 (void) dnode_hold_impl(os
, object
, DNODE_MUST_BE_FREE
,
82 if (dmu_object_next(os
, &object
, B_TRUE
, 0) == 0)
83 os
->os_obj_next
= object
- 1;
86 dnode_allocate(dn
, ot
, blocksize
, 0, bonustype
, bonuslen
, tx
);
89 mutex_exit(&os
->os_obj_lock
);
91 dmu_tx_add_new_object(tx
, os
, object
);
96 dmu_object_claim(objset_t
*os
, uint64_t object
, dmu_object_type_t ot
,
97 int blocksize
, dmu_object_type_t bonustype
, int bonuslen
, dmu_tx_t
*tx
)
102 if (object
== DMU_META_DNODE_OBJECT
&& !dmu_tx_private_ok(tx
))
103 return (SET_ERROR(EBADF
));
105 err
= dnode_hold_impl(os
, object
, DNODE_MUST_BE_FREE
, FTAG
, &dn
);
108 dnode_allocate(dn
, ot
, blocksize
, 0, bonustype
, bonuslen
, tx
);
109 dnode_rele(dn
, FTAG
);
111 dmu_tx_add_new_object(tx
, os
, object
);
116 dmu_object_reclaim(objset_t
*os
, uint64_t object
, dmu_object_type_t ot
,
117 int blocksize
, dmu_object_type_t bonustype
, int bonuslen
, dmu_tx_t
*tx
)
122 if (object
== DMU_META_DNODE_OBJECT
)
123 return (SET_ERROR(EBADF
));
125 err
= dnode_hold_impl(os
, object
, DNODE_MUST_BE_ALLOCATED
,
130 dnode_reallocate(dn
, ot
, blocksize
, bonustype
, bonuslen
, tx
);
132 dnode_rele(dn
, FTAG
);
137 dmu_object_free(objset_t
*os
, uint64_t object
, dmu_tx_t
*tx
)
142 ASSERT(object
!= DMU_META_DNODE_OBJECT
|| dmu_tx_private_ok(tx
));
144 err
= dnode_hold_impl(os
, object
, DNODE_MUST_BE_ALLOCATED
,
149 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
150 dnode_free_range(dn
, 0, DMU_OBJECT_END
, tx
);
152 dnode_rele(dn
, FTAG
);
158 * Return (in *objectp) the next object which is allocated (or a hole)
159 * after *object, taking into account only objects that may have been modified
160 * after the specified txg.
163 dmu_object_next(objset_t
*os
, uint64_t *objectp
, boolean_t hole
, uint64_t txg
)
165 uint64_t offset
= (*objectp
+ 1) << DNODE_SHIFT
;
168 error
= dnode_next_offset(DMU_META_DNODE(os
),
169 (hole
? DNODE_FIND_HOLE
: 0), &offset
, 0, DNODES_PER_BLOCK
, txg
);
171 *objectp
= offset
>> DNODE_SHIFT
;
177 * Turn this object from old_type into DMU_OTN_ZAP_METADATA, and bump the
178 * refcount on SPA_FEATURE_EXTENSIBLE_DATASET.
180 * Only for use from syncing context, on MOS objects.
183 dmu_object_zapify(objset_t
*mos
, uint64_t object
, dmu_object_type_t old_type
,
188 ASSERT(dmu_tx_is_syncing(tx
));
190 VERIFY0(dnode_hold(mos
, object
, FTAG
, &dn
));
191 if (dn
->dn_type
== DMU_OTN_ZAP_METADATA
) {
192 dnode_rele(dn
, FTAG
);
195 ASSERT3U(dn
->dn_type
, ==, old_type
);
196 ASSERT0(dn
->dn_maxblkid
);
197 dn
->dn_next_type
[tx
->tx_txg
& TXG_MASK
] = dn
->dn_type
=
198 DMU_OTN_ZAP_METADATA
;
199 dnode_setdirty(dn
, tx
);
200 dnode_rele(dn
, FTAG
);
202 mzap_create_impl(mos
, object
, 0, 0, tx
);
204 spa_feature_incr(dmu_objset_spa(mos
),
205 SPA_FEATURE_EXTENSIBLE_DATASET
, tx
);
209 dmu_object_free_zapified(objset_t
*mos
, uint64_t object
, dmu_tx_t
*tx
)
214 ASSERT(dmu_tx_is_syncing(tx
));
216 VERIFY0(dnode_hold(mos
, object
, FTAG
, &dn
));
218 dnode_rele(dn
, FTAG
);
220 if (t
== DMU_OTN_ZAP_METADATA
) {
221 spa_feature_decr(dmu_objset_spa(mos
),
222 SPA_FEATURE_EXTENSIBLE_DATASET
, tx
);
224 VERIFY0(dmu_object_free(mos
, object
, tx
));