xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_object.c (revision d1aea6f139360e9e7f1504facb24f8521047b15c)
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 L1_dnode_count = DNODES_PER_BLOCK <<
40 	    (DMU_META_DNODE(os)->dn_indblkshift - SPA_BLKPTRSHIFT);
41 	dnode_t *dn = NULL;
42 
43 	mutex_enter(&os->os_obj_lock);
44 	for (;;) {
45 		object = os->os_obj_next;
46 		/*
47 		 * Each time we polish off a L1 bp worth of dnodes (2^12
48 		 * objects), move to another L1 bp that's still reasonably
49 		 * sparse (at most 1/4 full). Look from the beginning at most
50 		 * once per txg, but after that keep looking from here.
51 		 * os_scan_dnodes is set during txg sync if enough objects
52 		 * have been freed since the previous rescan to justify
53 		 * backfilling again. If we can't find a suitable block, just
54 		 * keep going from here.
55 		 *
56 		 * Note that dmu_traverse depends on the behavior that we use
57 		 * multiple blocks of the dnode object before going back to
58 		 * reuse objects.  Any change to this algorithm should preserve
59 		 * that property or find another solution to the issues
60 		 * described in traverse_visitbp.
61 		 */
62 
63 		if (P2PHASE(object, L1_dnode_count) == 0) {
64 			uint64_t offset;
65 			int error;
66 			if (os->os_rescan_dnodes) {
67 				offset = 0;
68 				os->os_rescan_dnodes = B_FALSE;
69 			} else {
70 				offset = object << DNODE_SHIFT;
71 			}
72 			error = dnode_next_offset(DMU_META_DNODE(os),
73 			    DNODE_FIND_HOLE,
74 			    &offset, 2, DNODES_PER_BLOCK >> 2, 0);
75 			if (error == 0)
76 				object = offset >> DNODE_SHIFT;
77 		}
78 		os->os_obj_next = ++object;
79 
80 		/*
81 		 * XXX We should check for an i/o error here and return
82 		 * up to our caller.  Actually we should pre-read it in
83 		 * dmu_tx_assign(), but there is currently no mechanism
84 		 * to do so.
85 		 */
86 		(void) dnode_hold_impl(os, object, DNODE_MUST_BE_FREE,
87 		    FTAG, &dn);
88 		if (dn)
89 			break;
90 
91 		if (dmu_object_next(os, &object, B_TRUE, 0) == 0)
92 			os->os_obj_next = object - 1;
93 	}
94 
95 	dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
96 	mutex_exit(&os->os_obj_lock);
97 
98 	dmu_tx_add_new_object(tx, dn);
99 	dnode_rele(dn, FTAG);
100 
101 	return (object);
102 }
103 
104 int
105 dmu_object_claim(objset_t *os, uint64_t object, dmu_object_type_t ot,
106     int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
107 {
108 	dnode_t *dn;
109 	int err;
110 
111 	if (object == DMU_META_DNODE_OBJECT && !dmu_tx_private_ok(tx))
112 		return (SET_ERROR(EBADF));
113 
114 	err = dnode_hold_impl(os, object, DNODE_MUST_BE_FREE, FTAG, &dn);
115 	if (err)
116 		return (err);
117 	dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
118 	dmu_tx_add_new_object(tx, dn);
119 
120 	dnode_rele(dn, FTAG);
121 
122 	return (0);
123 }
124 
125 int
126 dmu_object_reclaim(objset_t *os, uint64_t object, dmu_object_type_t ot,
127     int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
128 {
129 	dnode_t *dn;
130 	int err;
131 
132 	if (object == DMU_META_DNODE_OBJECT)
133 		return (SET_ERROR(EBADF));
134 
135 	err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
136 	    FTAG, &dn);
137 	if (err)
138 		return (err);
139 
140 	dnode_reallocate(dn, ot, blocksize, bonustype, bonuslen, tx);
141 
142 	dnode_rele(dn, FTAG);
143 	return (err);
144 }
145 
146 int
147 dmu_object_free(objset_t *os, uint64_t object, dmu_tx_t *tx)
148 {
149 	dnode_t *dn;
150 	int err;
151 
152 	ASSERT(object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
153 
154 	err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
155 	    FTAG, &dn);
156 	if (err)
157 		return (err);
158 
159 	ASSERT(dn->dn_type != DMU_OT_NONE);
160 	dnode_free_range(dn, 0, DMU_OBJECT_END, tx);
161 	dnode_free(dn, tx);
162 	dnode_rele(dn, FTAG);
163 
164 	return (0);
165 }
166 
167 /*
168  * Return (in *objectp) the next object which is allocated (or a hole)
169  * after *object, taking into account only objects that may have been modified
170  * after the specified txg.
171  */
172 int
173 dmu_object_next(objset_t *os, uint64_t *objectp, boolean_t hole, uint64_t txg)
174 {
175 	uint64_t offset = (*objectp + 1) << DNODE_SHIFT;
176 	int error;
177 
178 	error = dnode_next_offset(DMU_META_DNODE(os),
179 	    (hole ? DNODE_FIND_HOLE : 0), &offset, 0, DNODES_PER_BLOCK, txg);
180 
181 	*objectp = offset >> DNODE_SHIFT;
182 
183 	return (error);
184 }
185 
186 /*
187  * Turn this object from old_type into DMU_OTN_ZAP_METADATA, and bump the
188  * refcount on SPA_FEATURE_EXTENSIBLE_DATASET.
189  *
190  * Only for use from syncing context, on MOS objects.
191  */
192 void
193 dmu_object_zapify(objset_t *mos, uint64_t object, dmu_object_type_t old_type,
194     dmu_tx_t *tx)
195 {
196 	dnode_t *dn;
197 
198 	ASSERT(dmu_tx_is_syncing(tx));
199 
200 	VERIFY0(dnode_hold(mos, object, FTAG, &dn));
201 	if (dn->dn_type == DMU_OTN_ZAP_METADATA) {
202 		dnode_rele(dn, FTAG);
203 		return;
204 	}
205 	ASSERT3U(dn->dn_type, ==, old_type);
206 	ASSERT0(dn->dn_maxblkid);
207 	dn->dn_next_type[tx->tx_txg & TXG_MASK] = dn->dn_type =
208 	    DMU_OTN_ZAP_METADATA;
209 	dnode_setdirty(dn, tx);
210 	dnode_rele(dn, FTAG);
211 
212 	mzap_create_impl(mos, object, 0, 0, tx);
213 
214 	spa_feature_incr(dmu_objset_spa(mos),
215 	    SPA_FEATURE_EXTENSIBLE_DATASET, tx);
216 }
217 
218 void
219 dmu_object_free_zapified(objset_t *mos, uint64_t object, dmu_tx_t *tx)
220 {
221 	dnode_t *dn;
222 	dmu_object_type_t t;
223 
224 	ASSERT(dmu_tx_is_syncing(tx));
225 
226 	VERIFY0(dnode_hold(mos, object, FTAG, &dn));
227 	t = dn->dn_type;
228 	dnode_rele(dn, FTAG);
229 
230 	if (t == DMU_OTN_ZAP_METADATA) {
231 		spa_feature_decr(dmu_objset_spa(mos),
232 		    SPA_FEATURE_EXTENSIBLE_DATASET, tx);
233 	}
234 	VERIFY0(dmu_object_free(mos, object, tx));
235 }
236