]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - zfs/module/zfs/dmu_object.c
UBUNTU: SAUCE: (noup) Update spl to 0.6.5.9-1, zfs to 0.6.5.9-2
[mirror_ubuntu-bionic-kernel.git] / zfs / module / zfs / dmu_object.c
1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21 /*
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.
25 */
26
27 #include <sys/dmu.h>
28 #include <sys/dmu_objset.h>
29 #include <sys/dmu_tx.h>
30 #include <sys/dnode.h>
31 #include <sys/zap.h>
32 #include <sys/zfeature.h>
33
34 uint64_t
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)
37 {
38 uint64_t object;
39 uint64_t L2_dnode_count = DNODES_PER_BLOCK <<
40 (DMU_META_DNODE(os)->dn_indblkshift - SPA_BLKPTRSHIFT);
41 dnode_t *dn = NULL;
42 int restarted = B_FALSE;
43
44 mutex_enter(&os->os_obj_lock);
45 for (;;) {
46 object = os->os_obj_next;
47 /*
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.
53 *
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.
59 */
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),
63 DNODE_FIND_HOLE,
64 &offset, 2, DNODES_PER_BLOCK >> 2, 0);
65 restarted = B_TRUE;
66 if (error == 0)
67 object = offset >> DNODE_SHIFT;
68 }
69 os->os_obj_next = ++object;
70
71 /*
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
75 * to do so.
76 */
77 (void) dnode_hold_impl(os, object, DNODE_MUST_BE_FREE,
78 FTAG, &dn);
79 if (dn)
80 break;
81
82 if (dmu_object_next(os, &object, B_TRUE, 0) == 0)
83 os->os_obj_next = object - 1;
84 }
85
86 dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
87 dnode_rele(dn, FTAG);
88
89 mutex_exit(&os->os_obj_lock);
90
91 dmu_tx_add_new_object(tx, os, object);
92 return (object);
93 }
94
95 int
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)
98 {
99 dnode_t *dn;
100 int err;
101
102 if (object == DMU_META_DNODE_OBJECT && !dmu_tx_private_ok(tx))
103 return (SET_ERROR(EBADF));
104
105 err = dnode_hold_impl(os, object, DNODE_MUST_BE_FREE, FTAG, &dn);
106 if (err)
107 return (err);
108 dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
109 dnode_rele(dn, FTAG);
110
111 dmu_tx_add_new_object(tx, os, object);
112 return (0);
113 }
114
115 int
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)
118 {
119 dnode_t *dn;
120 int err;
121
122 if (object == DMU_META_DNODE_OBJECT)
123 return (SET_ERROR(EBADF));
124
125 err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
126 FTAG, &dn);
127 if (err)
128 return (err);
129
130 dnode_reallocate(dn, ot, blocksize, bonustype, bonuslen, tx);
131
132 dnode_rele(dn, FTAG);
133 return (err);
134 }
135
136 int
137 dmu_object_free(objset_t *os, uint64_t object, dmu_tx_t *tx)
138 {
139 dnode_t *dn;
140 int err;
141
142 ASSERT(object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
143
144 err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
145 FTAG, &dn);
146 if (err)
147 return (err);
148
149 ASSERT(dn->dn_type != DMU_OT_NONE);
150 dnode_free_range(dn, 0, DMU_OBJECT_END, tx);
151 dnode_free(dn, tx);
152 dnode_rele(dn, FTAG);
153
154 return (0);
155 }
156
157 int
158 dmu_object_next(objset_t *os, uint64_t *objectp, boolean_t hole, uint64_t txg)
159 {
160 uint64_t offset = (*objectp + 1) << DNODE_SHIFT;
161 int error;
162
163 error = dnode_next_offset(DMU_META_DNODE(os),
164 (hole ? DNODE_FIND_HOLE : 0), &offset, 0, DNODES_PER_BLOCK, txg);
165
166 *objectp = offset >> DNODE_SHIFT;
167
168 return (error);
169 }
170
171 /*
172 * Turn this object from old_type into DMU_OTN_ZAP_METADATA, and bump the
173 * refcount on SPA_FEATURE_EXTENSIBLE_DATASET.
174 *
175 * Only for use from syncing context, on MOS objects.
176 */
177 void
178 dmu_object_zapify(objset_t *mos, uint64_t object, dmu_object_type_t old_type,
179 dmu_tx_t *tx)
180 {
181 dnode_t *dn;
182
183 ASSERT(dmu_tx_is_syncing(tx));
184
185 VERIFY0(dnode_hold(mos, object, FTAG, &dn));
186 if (dn->dn_type == DMU_OTN_ZAP_METADATA) {
187 dnode_rele(dn, FTAG);
188 return;
189 }
190 ASSERT3U(dn->dn_type, ==, old_type);
191 ASSERT0(dn->dn_maxblkid);
192 dn->dn_next_type[tx->tx_txg & TXG_MASK] = dn->dn_type =
193 DMU_OTN_ZAP_METADATA;
194 dnode_setdirty(dn, tx);
195 dnode_rele(dn, FTAG);
196
197 mzap_create_impl(mos, object, 0, 0, tx);
198
199 spa_feature_incr(dmu_objset_spa(mos),
200 SPA_FEATURE_EXTENSIBLE_DATASET, tx);
201 }
202
203 void
204 dmu_object_free_zapified(objset_t *mos, uint64_t object, dmu_tx_t *tx)
205 {
206 dnode_t *dn;
207 dmu_object_type_t t;
208
209 ASSERT(dmu_tx_is_syncing(tx));
210
211 VERIFY0(dnode_hold(mos, object, FTAG, &dn));
212 t = dn->dn_type;
213 dnode_rele(dn, FTAG);
214
215 if (t == DMU_OTN_ZAP_METADATA) {
216 spa_feature_decr(dmu_objset_spa(mos),
217 SPA_FEATURE_EXTENSIBLE_DATASET, tx);
218 }
219 VERIFY0(dmu_object_free(mos, object, tx));
220 }
221
222 #if defined(_KERNEL) && defined(HAVE_SPL)
223 EXPORT_SYMBOL(dmu_object_alloc);
224 EXPORT_SYMBOL(dmu_object_claim);
225 EXPORT_SYMBOL(dmu_object_reclaim);
226 EXPORT_SYMBOL(dmu_object_free);
227 EXPORT_SYMBOL(dmu_object_next);
228 EXPORT_SYMBOL(dmu_object_zapify);
229 EXPORT_SYMBOL(dmu_object_free_zapified);
230 #endif