]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/dmu_object.c
8ca699ebff4a1b7383e7c3b8b190042c3d122622
[mirror_zfs.git] / 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 #include <sys/dsl_dataset.h>
34
35 uint64_t
36 dmu_object_alloc(objset_t *os, dmu_object_type_t ot, int blocksize,
37 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
38 {
39 return dmu_object_alloc_dnsize(os, ot, blocksize, bonustype, bonuslen,
40 0, tx);
41 }
42
43 uint64_t
44 dmu_object_alloc_dnsize(objset_t *os, dmu_object_type_t ot, int blocksize,
45 dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx)
46 {
47 uint64_t object;
48 uint64_t L1_dnode_count = DNODES_PER_BLOCK <<
49 (DMU_META_DNODE(os)->dn_indblkshift - SPA_BLKPTRSHIFT);
50 dnode_t *dn = NULL;
51 int dn_slots = dnodesize >> DNODE_SHIFT;
52 boolean_t restarted = B_FALSE;
53
54 if (dn_slots == 0) {
55 dn_slots = DNODE_MIN_SLOTS;
56 } else {
57 ASSERT3S(dn_slots, >=, DNODE_MIN_SLOTS);
58 ASSERT3S(dn_slots, <=, DNODE_MAX_SLOTS);
59 }
60
61 mutex_enter(&os->os_obj_lock);
62 for (;;) {
63 object = os->os_obj_next;
64 /*
65 * Each time we polish off a L1 bp worth of dnodes (2^12
66 * objects), move to another L1 bp that's still
67 * reasonably sparse (at most 1/4 full). Look from the
68 * beginning at most once per txg. If we still can't
69 * allocate from that L1 block, search for an empty L0
70 * block, which will quickly skip to the end of the
71 * metadnode if the no nearby L0 blocks are empty. This
72 * fallback avoids a pathology where full dnode blocks
73 * containing large dnodes appear sparse because they
74 * have a low blk_fill, leading to many failed
75 * allocation attempts. In the long term a better
76 * mechanism to search for sparse metadnode regions,
77 * such as spacemaps, could be implemented.
78 *
79 * os_scan_dnodes is set during txg sync if enough objects
80 * have been freed since the previous rescan to justify
81 * backfilling again.
82 *
83 * Note that dmu_traverse depends on the behavior that we use
84 * multiple blocks of the dnode object before going back to
85 * reuse objects. Any change to this algorithm should preserve
86 * that property or find another solution to the issues
87 * described in traverse_visitbp.
88 */
89 if (P2PHASE(object, L1_dnode_count) == 0) {
90 uint64_t offset;
91 uint64_t blkfill;
92 int minlvl;
93 int error;
94 if (os->os_rescan_dnodes) {
95 offset = 0;
96 os->os_rescan_dnodes = B_FALSE;
97 } else {
98 offset = object << DNODE_SHIFT;
99 }
100 blkfill = restarted ? 1 : DNODES_PER_BLOCK >> 2;
101 minlvl = restarted ? 1 : 2;
102 restarted = B_TRUE;
103 error = dnode_next_offset(DMU_META_DNODE(os),
104 DNODE_FIND_HOLE, &offset, minlvl, blkfill, 0);
105 if (error == 0)
106 object = offset >> DNODE_SHIFT;
107 }
108 os->os_obj_next = object + dn_slots;
109
110 /*
111 * XXX We should check for an i/o error here and return
112 * up to our caller. Actually we should pre-read it in
113 * dmu_tx_assign(), but there is currently no mechanism
114 * to do so.
115 */
116 (void) dnode_hold_impl(os, object, DNODE_MUST_BE_FREE, dn_slots,
117 FTAG, &dn);
118 if (dn)
119 break;
120
121 if (dmu_object_next(os, &object, B_TRUE, 0) == 0)
122 os->os_obj_next = object;
123 else
124 /*
125 * Skip to next known valid starting point for a dnode.
126 */
127 os->os_obj_next = P2ROUNDUP(object + 1,
128 DNODES_PER_BLOCK);
129 }
130
131 dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, dn_slots, tx);
132 mutex_exit(&os->os_obj_lock);
133
134 dmu_tx_add_new_object(tx, dn);
135 dnode_rele(dn, FTAG);
136
137 return (object);
138 }
139
140 int
141 dmu_object_claim(objset_t *os, uint64_t object, dmu_object_type_t ot,
142 int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
143 {
144 return (dmu_object_claim_dnsize(os, object, ot, blocksize, bonustype,
145 bonuslen, 0, tx));
146 }
147
148 int
149 dmu_object_claim_dnsize(objset_t *os, uint64_t object, dmu_object_type_t ot,
150 int blocksize, dmu_object_type_t bonustype, int bonuslen,
151 int dnodesize, dmu_tx_t *tx)
152 {
153 dnode_t *dn;
154 int dn_slots = dnodesize >> DNODE_SHIFT;
155 int err;
156
157 if (dn_slots == 0)
158 dn_slots = DNODE_MIN_SLOTS;
159 ASSERT3S(dn_slots, >=, DNODE_MIN_SLOTS);
160 ASSERT3S(dn_slots, <=, DNODE_MAX_SLOTS);
161
162 if (object == DMU_META_DNODE_OBJECT && !dmu_tx_private_ok(tx))
163 return (SET_ERROR(EBADF));
164
165 err = dnode_hold_impl(os, object, DNODE_MUST_BE_FREE, dn_slots,
166 FTAG, &dn);
167 if (err)
168 return (err);
169
170 dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, dn_slots, tx);
171 dmu_tx_add_new_object(tx, dn);
172
173 dnode_rele(dn, FTAG);
174
175 return (0);
176 }
177
178 int
179 dmu_object_reclaim(objset_t *os, uint64_t object, dmu_object_type_t ot,
180 int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
181 {
182 return (dmu_object_reclaim_dnsize(os, object, ot, blocksize, bonustype,
183 bonuslen, 0, tx));
184 }
185
186 int
187 dmu_object_reclaim_dnsize(objset_t *os, uint64_t object, dmu_object_type_t ot,
188 int blocksize, dmu_object_type_t bonustype, int bonuslen, int dnodesize,
189 dmu_tx_t *tx)
190 {
191 dnode_t *dn;
192 int dn_slots = dnodesize >> DNODE_SHIFT;
193 int err;
194
195 if (object == DMU_META_DNODE_OBJECT)
196 return (SET_ERROR(EBADF));
197
198 err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, 0,
199 FTAG, &dn);
200 if (err)
201 return (err);
202
203 dnode_reallocate(dn, ot, blocksize, bonustype, bonuslen, dn_slots, tx);
204
205 dnode_rele(dn, FTAG);
206 return (err);
207 }
208
209
210 int
211 dmu_object_free(objset_t *os, uint64_t object, dmu_tx_t *tx)
212 {
213 dnode_t *dn;
214 int err;
215
216 ASSERT(object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
217
218 err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, 0,
219 FTAG, &dn);
220 if (err)
221 return (err);
222
223 ASSERT(dn->dn_type != DMU_OT_NONE);
224 dnode_free_range(dn, 0, DMU_OBJECT_END, tx);
225 dnode_free(dn, tx);
226 dnode_rele(dn, FTAG);
227
228 return (0);
229 }
230
231 /*
232 * Return (in *objectp) the next object which is allocated (or a hole)
233 * after *object, taking into account only objects that may have been modified
234 * after the specified txg.
235 */
236 int
237 dmu_object_next(objset_t *os, uint64_t *objectp, boolean_t hole, uint64_t txg)
238 {
239 uint64_t offset;
240 uint64_t start_obj;
241 struct dsl_dataset *ds = os->os_dsl_dataset;
242 int error;
243
244 if (*objectp == 0) {
245 start_obj = 1;
246 } else if (ds && ds->ds_feature_inuse[SPA_FEATURE_LARGE_DNODE]) {
247 /*
248 * For large_dnode datasets, scan from the beginning of the
249 * dnode block to find the starting offset. This is needed
250 * because objectp could be part of a large dnode so we can't
251 * assume it's a hole even if dmu_object_info() returns ENOENT.
252 */
253 int epb = DNODE_BLOCK_SIZE >> DNODE_SHIFT;
254 int skip;
255 uint64_t i;
256
257 for (i = *objectp & ~(epb - 1); i <= *objectp; i += skip) {
258 dmu_object_info_t doi;
259
260 error = dmu_object_info(os, i, &doi);
261 if (error)
262 skip = 1;
263 else
264 skip = doi.doi_dnodesize >> DNODE_SHIFT;
265 }
266
267 start_obj = i;
268 } else {
269 start_obj = *objectp + 1;
270 }
271
272 offset = start_obj << DNODE_SHIFT;
273
274 error = dnode_next_offset(DMU_META_DNODE(os),
275 (hole ? DNODE_FIND_HOLE : 0), &offset, 0, DNODES_PER_BLOCK, txg);
276
277 *objectp = offset >> DNODE_SHIFT;
278
279 return (error);
280 }
281
282 /*
283 * Turn this object from old_type into DMU_OTN_ZAP_METADATA, and bump the
284 * refcount on SPA_FEATURE_EXTENSIBLE_DATASET.
285 *
286 * Only for use from syncing context, on MOS objects.
287 */
288 void
289 dmu_object_zapify(objset_t *mos, uint64_t object, dmu_object_type_t old_type,
290 dmu_tx_t *tx)
291 {
292 dnode_t *dn;
293
294 ASSERT(dmu_tx_is_syncing(tx));
295
296 VERIFY0(dnode_hold(mos, object, FTAG, &dn));
297 if (dn->dn_type == DMU_OTN_ZAP_METADATA) {
298 dnode_rele(dn, FTAG);
299 return;
300 }
301 ASSERT3U(dn->dn_type, ==, old_type);
302 ASSERT0(dn->dn_maxblkid);
303 dn->dn_next_type[tx->tx_txg & TXG_MASK] = dn->dn_type =
304 DMU_OTN_ZAP_METADATA;
305 dnode_setdirty(dn, tx);
306 dnode_rele(dn, FTAG);
307
308 mzap_create_impl(mos, object, 0, 0, tx);
309
310 spa_feature_incr(dmu_objset_spa(mos),
311 SPA_FEATURE_EXTENSIBLE_DATASET, tx);
312 }
313
314 void
315 dmu_object_free_zapified(objset_t *mos, uint64_t object, dmu_tx_t *tx)
316 {
317 dnode_t *dn;
318 dmu_object_type_t t;
319
320 ASSERT(dmu_tx_is_syncing(tx));
321
322 VERIFY0(dnode_hold(mos, object, FTAG, &dn));
323 t = dn->dn_type;
324 dnode_rele(dn, FTAG);
325
326 if (t == DMU_OTN_ZAP_METADATA) {
327 spa_feature_decr(dmu_objset_spa(mos),
328 SPA_FEATURE_EXTENSIBLE_DATASET, tx);
329 }
330 VERIFY0(dmu_object_free(mos, object, tx));
331 }
332
333 #if defined(_KERNEL) && defined(HAVE_SPL)
334 EXPORT_SYMBOL(dmu_object_alloc);
335 EXPORT_SYMBOL(dmu_object_alloc_dnsize);
336 EXPORT_SYMBOL(dmu_object_claim);
337 EXPORT_SYMBOL(dmu_object_claim_dnsize);
338 EXPORT_SYMBOL(dmu_object_reclaim);
339 EXPORT_SYMBOL(dmu_object_reclaim_dnsize);
340 EXPORT_SYMBOL(dmu_object_free);
341 EXPORT_SYMBOL(dmu_object_next);
342 EXPORT_SYMBOL(dmu_object_zapify);
343 EXPORT_SYMBOL(dmu_object_free_zapified);
344 #endif