xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_object.c (revision 221813c13b43ef48330b03725e00edee85108cf1)
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, 2017 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_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)
38 {
39 	uint64_t object;
40 	uint64_t L1_dnode_count = DNODES_PER_BLOCK <<
41 	    (DMU_META_DNODE(os)->dn_indblkshift - SPA_BLKPTRSHIFT);
42 	dnode_t *dn = NULL;
43 
44 	mutex_enter(&os->os_obj_lock);
45 	for (;;) {
46 		object = os->os_obj_next;
47 		/*
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.
56 		 *
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.
62 		 */
63 
64 		if (P2PHASE(object, L1_dnode_count) == 0) {
65 			uint64_t offset;
66 			int error;
67 			if (os->os_rescan_dnodes) {
68 				offset = 0;
69 				os->os_rescan_dnodes = B_FALSE;
70 			} else {
71 				offset = object << DNODE_SHIFT;
72 			}
73 			error = dnode_next_offset(DMU_META_DNODE(os),
74 			    DNODE_FIND_HOLE,
75 			    &offset, 2, DNODES_PER_BLOCK >> 2, 0);
76 			if (error == 0)
77 				object = offset >> DNODE_SHIFT;
78 		}
79 		os->os_obj_next = ++object;
80 
81 		/*
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
85 		 * to do so.
86 		 */
87 		(void) dnode_hold_impl(os, object, DNODE_MUST_BE_FREE,
88 		    FTAG, &dn);
89 		if (dn)
90 			break;
91 
92 		if (dmu_object_next(os, &object, B_TRUE, 0) == 0)
93 			os->os_obj_next = object - 1;
94 	}
95 
96 	dnode_allocate(dn, ot, blocksize, indirect_blockshift,
97 	    bonustype, bonuslen, tx);
98 	mutex_exit(&os->os_obj_lock);
99 
100 	dmu_tx_add_new_object(tx, dn);
101 	dnode_rele(dn, FTAG);
102 
103 	return (object);
104 }
105 
106 uint64_t
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)
109 {
110 	return (dmu_object_alloc_ibs(os, ot, blocksize, 0,
111 	    bonustype, bonuslen, tx));
112 }
113 
114 int
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)
117 {
118 	dnode_t *dn;
119 	int err;
120 
121 	if (object == DMU_META_DNODE_OBJECT && !dmu_tx_private_ok(tx))
122 		return (SET_ERROR(EBADF));
123 
124 	err = dnode_hold_impl(os, object, DNODE_MUST_BE_FREE, FTAG, &dn);
125 	if (err)
126 		return (err);
127 	dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
128 	dmu_tx_add_new_object(tx, dn);
129 
130 	dnode_rele(dn, FTAG);
131 
132 	return (0);
133 }
134 
135 int
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)
138 {
139 	dnode_t *dn;
140 	int err;
141 
142 	if (object == DMU_META_DNODE_OBJECT)
143 		return (SET_ERROR(EBADF));
144 
145 	err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
146 	    FTAG, &dn);
147 	if (err)
148 		return (err);
149 
150 	dnode_reallocate(dn, ot, blocksize, bonustype, bonuslen, tx);
151 
152 	dnode_rele(dn, FTAG);
153 	return (err);
154 }
155 
156 int
157 dmu_object_free(objset_t *os, uint64_t object, dmu_tx_t *tx)
158 {
159 	dnode_t *dn;
160 	int err;
161 
162 	ASSERT(object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
163 
164 	err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
165 	    FTAG, &dn);
166 	if (err)
167 		return (err);
168 
169 	ASSERT(dn->dn_type != DMU_OT_NONE);
170 	dnode_free_range(dn, 0, DMU_OBJECT_END, tx);
171 	dnode_free(dn, tx);
172 	dnode_rele(dn, FTAG);
173 
174 	return (0);
175 }
176 
177 /*
178  * Return (in *objectp) the next object which is allocated (or a hole)
179  * after *object, taking into account only objects that may have been modified
180  * after the specified txg.
181  */
182 int
183 dmu_object_next(objset_t *os, uint64_t *objectp, boolean_t hole, uint64_t txg)
184 {
185 	uint64_t offset = (*objectp + 1) << DNODE_SHIFT;
186 	int error;
187 
188 	error = dnode_next_offset(DMU_META_DNODE(os),
189 	    (hole ? DNODE_FIND_HOLE : 0), &offset, 0, DNODES_PER_BLOCK, txg);
190 
191 	*objectp = offset >> DNODE_SHIFT;
192 
193 	return (error);
194 }
195 
196 /*
197  * Turn this object from old_type into DMU_OTN_ZAP_METADATA, and bump the
198  * refcount on SPA_FEATURE_EXTENSIBLE_DATASET.
199  *
200  * Only for use from syncing context, on MOS objects.
201  */
202 void
203 dmu_object_zapify(objset_t *mos, uint64_t object, dmu_object_type_t old_type,
204     dmu_tx_t *tx)
205 {
206 	dnode_t *dn;
207 
208 	ASSERT(dmu_tx_is_syncing(tx));
209 
210 	VERIFY0(dnode_hold(mos, object, FTAG, &dn));
211 	if (dn->dn_type == DMU_OTN_ZAP_METADATA) {
212 		dnode_rele(dn, FTAG);
213 		return;
214 	}
215 	ASSERT3U(dn->dn_type, ==, old_type);
216 	ASSERT0(dn->dn_maxblkid);
217 
218 	/*
219 	 * We must initialize the ZAP data before changing the type,
220 	 * so that concurrent calls to *_is_zapified() can determine if
221 	 * the object has been completely zapified by checking the type.
222 	 */
223 	mzap_create_impl(mos, object, 0, 0, tx);
224 
225 	dn->dn_next_type[tx->tx_txg & TXG_MASK] = dn->dn_type =
226 	    DMU_OTN_ZAP_METADATA;
227 	dnode_setdirty(dn, tx);
228 	dnode_rele(dn, FTAG);
229 
230 	spa_feature_incr(dmu_objset_spa(mos),
231 	    SPA_FEATURE_EXTENSIBLE_DATASET, tx);
232 }
233 
234 void
235 dmu_object_free_zapified(objset_t *mos, uint64_t object, dmu_tx_t *tx)
236 {
237 	dnode_t *dn;
238 	dmu_object_type_t t;
239 
240 	ASSERT(dmu_tx_is_syncing(tx));
241 
242 	VERIFY0(dnode_hold(mos, object, FTAG, &dn));
243 	t = dn->dn_type;
244 	dnode_rele(dn, FTAG);
245 
246 	if (t == DMU_OTN_ZAP_METADATA) {
247 		spa_feature_decr(dmu_objset_spa(mos),
248 		    SPA_FEATURE_EXTENSIBLE_DATASET, tx);
249 	}
250 	VERIFY0(dmu_object_free(mos, object, tx));
251 }
252