1168404Spjd/*
2168404Spjd * CDDL HEADER START
3168404Spjd *
4168404Spjd * The contents of this file are subject to the terms of the
5168404Spjd * Common Development and Distribution License (the "License").
6168404Spjd * You may not use this file except in compliance with the License.
7168404Spjd *
8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9168404Spjd * or http://www.opensolaris.org/os/licensing.
10168404Spjd * See the License for the specific language governing permissions
11168404Spjd * and limitations under the License.
12168404Spjd *
13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each
14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15168404Spjd * If applicable, add the following below this CDDL HEADER, with the
16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18168404Spjd *
19168404Spjd * CDDL HEADER END
20168404Spjd */
21168404Spjd/*
22219089Spjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23249195Smm * Copyright (c) 2013 by Delphix. All rights reserved.
24168404Spjd */
25168404Spjd
26168404Spjd#include <sys/dmu.h>
27168404Spjd#include <sys/dmu_objset.h>
28168404Spjd#include <sys/dmu_tx.h>
29168404Spjd#include <sys/dnode.h>
30263390Sdelphij#include <sys/zap.h>
31263390Sdelphij#include <sys/zfeature.h>
32168404Spjd
33168404Spjduint64_t
34168404Spjddmu_object_alloc(objset_t *os, dmu_object_type_t ot, int blocksize,
35168404Spjd    dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
36168404Spjd{
37168404Spjd	uint64_t object;
38168404Spjd	uint64_t L2_dnode_count = DNODES_PER_BLOCK <<
39219089Spjd	    (DMU_META_DNODE(os)->dn_indblkshift - SPA_BLKPTRSHIFT);
40168404Spjd	dnode_t *dn = NULL;
41168404Spjd	int restarted = B_FALSE;
42168404Spjd
43219089Spjd	mutex_enter(&os->os_obj_lock);
44168404Spjd	for (;;) {
45219089Spjd		object = os->os_obj_next;
46168404Spjd		/*
47168404Spjd		 * Each time we polish off an L2 bp worth of dnodes
48168404Spjd		 * (2^13 objects), move to another L2 bp that's still
49168404Spjd		 * reasonably sparse (at most 1/4 full).  Look from the
50168404Spjd		 * beginning once, but after that keep looking from here.
51168404Spjd		 * If we can't find one, just keep going from here.
52168404Spjd		 */
53168404Spjd		if (P2PHASE(object, L2_dnode_count) == 0) {
54168404Spjd			uint64_t offset = restarted ? object << DNODE_SHIFT : 0;
55219089Spjd			int error = dnode_next_offset(DMU_META_DNODE(os),
56185029Spjd			    DNODE_FIND_HOLE,
57185029Spjd			    &offset, 2, DNODES_PER_BLOCK >> 2, 0);
58168404Spjd			restarted = B_TRUE;
59168404Spjd			if (error == 0)
60168404Spjd				object = offset >> DNODE_SHIFT;
61168404Spjd		}
62219089Spjd		os->os_obj_next = ++object;
63168404Spjd
64168404Spjd		/*
65168404Spjd		 * XXX We should check for an i/o error here and return
66168404Spjd		 * up to our caller.  Actually we should pre-read it in
67168404Spjd		 * dmu_tx_assign(), but there is currently no mechanism
68168404Spjd		 * to do so.
69168404Spjd		 */
70219089Spjd		(void) dnode_hold_impl(os, object, DNODE_MUST_BE_FREE,
71168404Spjd		    FTAG, &dn);
72168404Spjd		if (dn)
73168404Spjd			break;
74168404Spjd
75168404Spjd		if (dmu_object_next(os, &object, B_TRUE, 0) == 0)
76219089Spjd			os->os_obj_next = object - 1;
77168404Spjd	}
78168404Spjd
79168404Spjd	dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
80168404Spjd	dnode_rele(dn, FTAG);
81168404Spjd
82219089Spjd	mutex_exit(&os->os_obj_lock);
83168404Spjd
84168404Spjd	dmu_tx_add_new_object(tx, os, object);
85168404Spjd	return (object);
86168404Spjd}
87168404Spjd
88168404Spjdint
89168404Spjddmu_object_claim(objset_t *os, uint64_t object, dmu_object_type_t ot,
90168404Spjd    int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
91168404Spjd{
92168404Spjd	dnode_t *dn;
93168404Spjd	int err;
94168404Spjd
95168404Spjd	if (object == DMU_META_DNODE_OBJECT && !dmu_tx_private_ok(tx))
96249195Smm		return (SET_ERROR(EBADF));
97168404Spjd
98219089Spjd	err = dnode_hold_impl(os, object, DNODE_MUST_BE_FREE, FTAG, &dn);
99168404Spjd	if (err)
100168404Spjd		return (err);
101168404Spjd	dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
102168404Spjd	dnode_rele(dn, FTAG);
103168404Spjd
104168404Spjd	dmu_tx_add_new_object(tx, os, object);
105168404Spjd	return (0);
106168404Spjd}
107168404Spjd
108168404Spjdint
109168404Spjddmu_object_reclaim(objset_t *os, uint64_t object, dmu_object_type_t ot,
110200726Sdelphij    int blocksize, dmu_object_type_t bonustype, int bonuslen)
111168404Spjd{
112168404Spjd	dnode_t *dn;
113200726Sdelphij	dmu_tx_t *tx;
114200726Sdelphij	int nblkptr;
115168404Spjd	int err;
116168404Spjd
117200726Sdelphij	if (object == DMU_META_DNODE_OBJECT)
118249195Smm		return (SET_ERROR(EBADF));
119168404Spjd
120219089Spjd	err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
121168404Spjd	    FTAG, &dn);
122168404Spjd	if (err)
123168404Spjd		return (err);
124200726Sdelphij
125200726Sdelphij	if (dn->dn_type == ot && dn->dn_datablksz == blocksize &&
126200726Sdelphij	    dn->dn_bonustype == bonustype && dn->dn_bonuslen == bonuslen) {
127200726Sdelphij		/* nothing is changing, this is a noop */
128200726Sdelphij		dnode_rele(dn, FTAG);
129200726Sdelphij		return (0);
130200726Sdelphij	}
131200726Sdelphij
132219089Spjd	if (bonustype == DMU_OT_SA) {
133219089Spjd		nblkptr = 1;
134219089Spjd	} else {
135219089Spjd		nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
136219089Spjd	}
137200726Sdelphij
138200726Sdelphij	/*
139200726Sdelphij	 * If we are losing blkptrs or changing the block size this must
140200726Sdelphij	 * be a new file instance.   We must clear out the previous file
141200726Sdelphij	 * contents before we can change this type of metadata in the dnode.
142200726Sdelphij	 */
143207624Smm	if (dn->dn_nblkptr > nblkptr || dn->dn_datablksz != blocksize) {
144207624Smm		err = dmu_free_long_range(os, object, 0, DMU_OBJECT_END);
145207624Smm		if (err)
146207624Smm			goto out;
147207624Smm	}
148200726Sdelphij
149207624Smm	tx = dmu_tx_create(os);
150207624Smm	dmu_tx_hold_bonus(tx, object);
151207624Smm	err = dmu_tx_assign(tx, TXG_WAIT);
152207624Smm	if (err) {
153207624Smm		dmu_tx_abort(tx);
154207624Smm		goto out;
155207624Smm	}
156207624Smm
157168404Spjd	dnode_reallocate(dn, ot, blocksize, bonustype, bonuslen, tx);
158200726Sdelphij
159200726Sdelphij	dmu_tx_commit(tx);
160207624Smmout:
161168404Spjd	dnode_rele(dn, FTAG);
162168404Spjd
163207624Smm	return (err);
164168404Spjd}
165168404Spjd
166168404Spjdint
167168404Spjddmu_object_free(objset_t *os, uint64_t object, dmu_tx_t *tx)
168168404Spjd{
169168404Spjd	dnode_t *dn;
170168404Spjd	int err;
171168404Spjd
172168404Spjd	ASSERT(object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
173168404Spjd
174219089Spjd	err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
175168404Spjd	    FTAG, &dn);
176168404Spjd	if (err)
177168404Spjd		return (err);
178168404Spjd
179168404Spjd	ASSERT(dn->dn_type != DMU_OT_NONE);
180185029Spjd	dnode_free_range(dn, 0, DMU_OBJECT_END, tx);
181168404Spjd	dnode_free(dn, tx);
182168404Spjd	dnode_rele(dn, FTAG);
183168404Spjd
184168404Spjd	return (0);
185168404Spjd}
186168404Spjd
187168404Spjdint
188168404Spjddmu_object_next(objset_t *os, uint64_t *objectp, boolean_t hole, uint64_t txg)
189168404Spjd{
190168404Spjd	uint64_t offset = (*objectp + 1) << DNODE_SHIFT;
191168404Spjd	int error;
192168404Spjd
193219089Spjd	error = dnode_next_offset(DMU_META_DNODE(os),
194185029Spjd	    (hole ? DNODE_FIND_HOLE : 0), &offset, 0, DNODES_PER_BLOCK, txg);
195168404Spjd
196168404Spjd	*objectp = offset >> DNODE_SHIFT;
197168404Spjd
198168404Spjd	return (error);
199168404Spjd}
200263390Sdelphij
201263390Sdelphij/*
202263390Sdelphij * Turn this object from old_type into DMU_OTN_ZAP_METADATA, and bump the
203263390Sdelphij * refcount on SPA_FEATURE_EXTENSIBLE_DATASET.
204263390Sdelphij *
205263390Sdelphij * Only for use from syncing context, on MOS objects.
206263390Sdelphij */
207263390Sdelphijvoid
208263390Sdelphijdmu_object_zapify(objset_t *mos, uint64_t object, dmu_object_type_t old_type,
209263390Sdelphij    dmu_tx_t *tx)
210263390Sdelphij{
211263390Sdelphij	dnode_t *dn;
212263390Sdelphij
213263390Sdelphij	ASSERT(dmu_tx_is_syncing(tx));
214263390Sdelphij
215263390Sdelphij	VERIFY0(dnode_hold(mos, object, FTAG, &dn));
216263390Sdelphij	if (dn->dn_type == DMU_OTN_ZAP_METADATA) {
217263390Sdelphij		dnode_rele(dn, FTAG);
218263390Sdelphij		return;
219263390Sdelphij	}
220263390Sdelphij	ASSERT3U(dn->dn_type, ==, old_type);
221263390Sdelphij	ASSERT0(dn->dn_maxblkid);
222263390Sdelphij	dn->dn_next_type[tx->tx_txg & TXG_MASK] = dn->dn_type =
223263390Sdelphij	    DMU_OTN_ZAP_METADATA;
224263390Sdelphij	dnode_setdirty(dn, tx);
225263390Sdelphij	dnode_rele(dn, FTAG);
226263390Sdelphij
227263390Sdelphij	mzap_create_impl(mos, object, 0, 0, tx);
228263390Sdelphij
229263390Sdelphij	spa_feature_incr(dmu_objset_spa(mos),
230263390Sdelphij	    SPA_FEATURE_EXTENSIBLE_DATASET, tx);
231263390Sdelphij}
232263390Sdelphij
233263390Sdelphijvoid
234263390Sdelphijdmu_object_free_zapified(objset_t *mos, uint64_t object, dmu_tx_t *tx)
235263390Sdelphij{
236263390Sdelphij	dnode_t *dn;
237263390Sdelphij	dmu_object_type_t t;
238263390Sdelphij
239263390Sdelphij	ASSERT(dmu_tx_is_syncing(tx));
240263390Sdelphij
241263390Sdelphij	VERIFY0(dnode_hold(mos, object, FTAG, &dn));
242263390Sdelphij	t = dn->dn_type;
243263390Sdelphij	dnode_rele(dn, FTAG);
244263390Sdelphij
245263390Sdelphij	if (t == DMU_OTN_ZAP_METADATA) {
246263390Sdelphij		spa_feature_decr(dmu_objset_spa(mos),
247263390Sdelphij		    SPA_FEATURE_EXTENSIBLE_DATASET, tx);
248263390Sdelphij	}
249263390Sdelphij	VERIFY0(dmu_object_free(mos, object, tx));
250263390Sdelphij}
251