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>
30168404Spjd
31168404Spjduint64_t
32168404Spjddmu_object_alloc(objset_t *os, dmu_object_type_t ot, int blocksize,
33168404Spjd    dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
34168404Spjd{
35168404Spjd	uint64_t object;
36168404Spjd	uint64_t L2_dnode_count = DNODES_PER_BLOCK <<
37219089Spjd	    (DMU_META_DNODE(os)->dn_indblkshift - SPA_BLKPTRSHIFT);
38168404Spjd	dnode_t *dn = NULL;
39168404Spjd	int restarted = B_FALSE;
40168404Spjd
41219089Spjd	mutex_enter(&os->os_obj_lock);
42168404Spjd	for (;;) {
43219089Spjd		object = os->os_obj_next;
44168404Spjd		/*
45168404Spjd		 * Each time we polish off an L2 bp worth of dnodes
46168404Spjd		 * (2^13 objects), move to another L2 bp that's still
47168404Spjd		 * reasonably sparse (at most 1/4 full).  Look from the
48168404Spjd		 * beginning once, but after that keep looking from here.
49168404Spjd		 * If we can't find one, just keep going from here.
50168404Spjd		 */
51168404Spjd		if (P2PHASE(object, L2_dnode_count) == 0) {
52168404Spjd			uint64_t offset = restarted ? object << DNODE_SHIFT : 0;
53219089Spjd			int error = dnode_next_offset(DMU_META_DNODE(os),
54185029Spjd			    DNODE_FIND_HOLE,
55185029Spjd			    &offset, 2, DNODES_PER_BLOCK >> 2, 0);
56168404Spjd			restarted = B_TRUE;
57168404Spjd			if (error == 0)
58168404Spjd				object = offset >> DNODE_SHIFT;
59168404Spjd		}
60219089Spjd		os->os_obj_next = ++object;
61168404Spjd
62168404Spjd		/*
63168404Spjd		 * XXX We should check for an i/o error here and return
64168404Spjd		 * up to our caller.  Actually we should pre-read it in
65168404Spjd		 * dmu_tx_assign(), but there is currently no mechanism
66168404Spjd		 * to do so.
67168404Spjd		 */
68219089Spjd		(void) dnode_hold_impl(os, object, DNODE_MUST_BE_FREE,
69168404Spjd		    FTAG, &dn);
70168404Spjd		if (dn)
71168404Spjd			break;
72168404Spjd
73168404Spjd		if (dmu_object_next(os, &object, B_TRUE, 0) == 0)
74219089Spjd			os->os_obj_next = object - 1;
75168404Spjd	}
76168404Spjd
77168404Spjd	dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
78168404Spjd	dnode_rele(dn, FTAG);
79168404Spjd
80219089Spjd	mutex_exit(&os->os_obj_lock);
81168404Spjd
82168404Spjd	dmu_tx_add_new_object(tx, os, object);
83168404Spjd	return (object);
84168404Spjd}
85168404Spjd
86168404Spjdint
87168404Spjddmu_object_claim(objset_t *os, uint64_t object, dmu_object_type_t ot,
88168404Spjd    int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
89168404Spjd{
90168404Spjd	dnode_t *dn;
91168404Spjd	int err;
92168404Spjd
93168404Spjd	if (object == DMU_META_DNODE_OBJECT && !dmu_tx_private_ok(tx))
94249195Smm		return (SET_ERROR(EBADF));
95168404Spjd
96219089Spjd	err = dnode_hold_impl(os, object, DNODE_MUST_BE_FREE, FTAG, &dn);
97168404Spjd	if (err)
98168404Spjd		return (err);
99168404Spjd	dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
100168404Spjd	dnode_rele(dn, FTAG);
101168404Spjd
102168404Spjd	dmu_tx_add_new_object(tx, os, object);
103168404Spjd	return (0);
104168404Spjd}
105168404Spjd
106168404Spjdint
107168404Spjddmu_object_reclaim(objset_t *os, uint64_t object, dmu_object_type_t ot,
108200726Sdelphij    int blocksize, dmu_object_type_t bonustype, int bonuslen)
109168404Spjd{
110168404Spjd	dnode_t *dn;
111200726Sdelphij	dmu_tx_t *tx;
112200726Sdelphij	int nblkptr;
113168404Spjd	int err;
114168404Spjd
115200726Sdelphij	if (object == DMU_META_DNODE_OBJECT)
116249195Smm		return (SET_ERROR(EBADF));
117168404Spjd
118219089Spjd	err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
119168404Spjd	    FTAG, &dn);
120168404Spjd	if (err)
121168404Spjd		return (err);
122200726Sdelphij
123200726Sdelphij	if (dn->dn_type == ot && dn->dn_datablksz == blocksize &&
124200726Sdelphij	    dn->dn_bonustype == bonustype && dn->dn_bonuslen == bonuslen) {
125200726Sdelphij		/* nothing is changing, this is a noop */
126200726Sdelphij		dnode_rele(dn, FTAG);
127200726Sdelphij		return (0);
128200726Sdelphij	}
129200726Sdelphij
130219089Spjd	if (bonustype == DMU_OT_SA) {
131219089Spjd		nblkptr = 1;
132219089Spjd	} else {
133219089Spjd		nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
134219089Spjd	}
135200726Sdelphij
136200726Sdelphij	/*
137200726Sdelphij	 * If we are losing blkptrs or changing the block size this must
138200726Sdelphij	 * be a new file instance.   We must clear out the previous file
139200726Sdelphij	 * contents before we can change this type of metadata in the dnode.
140200726Sdelphij	 */
141207624Smm	if (dn->dn_nblkptr > nblkptr || dn->dn_datablksz != blocksize) {
142207624Smm		err = dmu_free_long_range(os, object, 0, DMU_OBJECT_END);
143207624Smm		if (err)
144207624Smm			goto out;
145207624Smm	}
146200726Sdelphij
147207624Smm	tx = dmu_tx_create(os);
148207624Smm	dmu_tx_hold_bonus(tx, object);
149207624Smm	err = dmu_tx_assign(tx, TXG_WAIT);
150207624Smm	if (err) {
151207624Smm		dmu_tx_abort(tx);
152207624Smm		goto out;
153207624Smm	}
154207624Smm
155168404Spjd	dnode_reallocate(dn, ot, blocksize, bonustype, bonuslen, tx);
156200726Sdelphij
157200726Sdelphij	dmu_tx_commit(tx);
158207624Smmout:
159168404Spjd	dnode_rele(dn, FTAG);
160168404Spjd
161207624Smm	return (err);
162168404Spjd}
163168404Spjd
164168404Spjdint
165168404Spjddmu_object_free(objset_t *os, uint64_t object, dmu_tx_t *tx)
166168404Spjd{
167168404Spjd	dnode_t *dn;
168168404Spjd	int err;
169168404Spjd
170168404Spjd	ASSERT(object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
171168404Spjd
172219089Spjd	err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
173168404Spjd	    FTAG, &dn);
174168404Spjd	if (err)
175168404Spjd		return (err);
176168404Spjd
177168404Spjd	ASSERT(dn->dn_type != DMU_OT_NONE);
178185029Spjd	dnode_free_range(dn, 0, DMU_OBJECT_END, tx);
179168404Spjd	dnode_free(dn, tx);
180168404Spjd	dnode_rele(dn, FTAG);
181168404Spjd
182168404Spjd	return (0);
183168404Spjd}
184168404Spjd
185168404Spjdint
186168404Spjddmu_object_next(objset_t *os, uint64_t *objectp, boolean_t hole, uint64_t txg)
187168404Spjd{
188168404Spjd	uint64_t offset = (*objectp + 1) << DNODE_SHIFT;
189168404Spjd	int error;
190168404Spjd
191219089Spjd	error = dnode_next_offset(DMU_META_DNODE(os),
192185029Spjd	    (hole ? DNODE_FIND_HOLE : 0), &offset, 0, DNODES_PER_BLOCK, txg);
193168404Spjd
194168404Spjd	*objectp = offset >> DNODE_SHIFT;
195168404Spjd
196168404Spjd	return (error);
197168404Spjd}
198