xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_object.c (revision cdb0ab79ea1af7b8fc339a04d4bf7426dc77ec4e)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
22*cdb0ab79Smaybee  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27fa9e4066Sahrens 
28fa9e4066Sahrens #include <sys/dmu.h>
29fa9e4066Sahrens #include <sys/dmu_objset.h>
30fa9e4066Sahrens #include <sys/dmu_tx.h>
31fa9e4066Sahrens #include <sys/dnode.h>
32fa9e4066Sahrens 
33fa9e4066Sahrens uint64_t
34fa9e4066Sahrens dmu_object_alloc(objset_t *os, dmu_object_type_t ot, int blocksize,
35fa9e4066Sahrens     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
36fa9e4066Sahrens {
37fa9e4066Sahrens 	objset_impl_t *osi = os->os;
38fa9e4066Sahrens 	uint64_t object;
39fa9e4066Sahrens 	uint64_t L2_dnode_count = DNODES_PER_BLOCK <<
40fa9e4066Sahrens 	    (osi->os_meta_dnode->dn_indblkshift - SPA_BLKPTRSHIFT);
41ea8dc4b6Seschrock 	dnode_t *dn = NULL;
42fa9e4066Sahrens 	int restarted = B_FALSE;
43fa9e4066Sahrens 
44fa9e4066Sahrens 	mutex_enter(&osi->os_obj_lock);
45fa9e4066Sahrens 	for (;;) {
46fa9e4066Sahrens 		object = osi->os_obj_next;
47fa9e4066Sahrens 		/*
48fa9e4066Sahrens 		 * Each time we polish off an L2 bp worth of dnodes
49fa9e4066Sahrens 		 * (2^13 objects), move to another L2 bp that's still
50fa9e4066Sahrens 		 * reasonably sparse (at most 1/4 full).  Look from the
51fa9e4066Sahrens 		 * beginning once, but after that keep looking from here.
52fa9e4066Sahrens 		 * If we can't find one, just keep going from here.
53fa9e4066Sahrens 		 */
54fa9e4066Sahrens 		if (P2PHASE(object, L2_dnode_count) == 0) {
55fa9e4066Sahrens 			uint64_t offset = restarted ? object << DNODE_SHIFT : 0;
56fa9e4066Sahrens 			int error = dnode_next_offset(osi->os_meta_dnode,
57*cdb0ab79Smaybee 			    DNODE_FIND_HOLE,
58*cdb0ab79Smaybee 			    &offset, 2, DNODES_PER_BLOCK >> 2, 0);
59fa9e4066Sahrens 			restarted = B_TRUE;
60fa9e4066Sahrens 			if (error == 0)
61fa9e4066Sahrens 				object = offset >> DNODE_SHIFT;
62fa9e4066Sahrens 		}
63fa9e4066Sahrens 		osi->os_obj_next = ++object;
64fa9e4066Sahrens 
65ea8dc4b6Seschrock 		/*
66ea8dc4b6Seschrock 		 * XXX We should check for an i/o error here and return
67ea8dc4b6Seschrock 		 * up to our caller.  Actually we should pre-read it in
68ea8dc4b6Seschrock 		 * dmu_tx_assign(), but there is currently no mechanism
69ea8dc4b6Seschrock 		 * to do so.
70ea8dc4b6Seschrock 		 */
71ea8dc4b6Seschrock 		(void) dnode_hold_impl(os->os, object, DNODE_MUST_BE_FREE,
72ea8dc4b6Seschrock 		    FTAG, &dn);
73fa9e4066Sahrens 		if (dn)
74fa9e4066Sahrens 			break;
75fa9e4066Sahrens 
766754306eSahrens 		if (dmu_object_next(os, &object, B_TRUE, 0) == 0)
77fa9e4066Sahrens 			osi->os_obj_next = object - 1;
78fa9e4066Sahrens 	}
79fa9e4066Sahrens 
80fa9e4066Sahrens 	dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
81fa9e4066Sahrens 	dnode_rele(dn, FTAG);
82fa9e4066Sahrens 
83fa9e4066Sahrens 	mutex_exit(&osi->os_obj_lock);
84fa9e4066Sahrens 
85fa9e4066Sahrens 	dmu_tx_add_new_object(tx, os, object);
86fa9e4066Sahrens 	return (object);
87fa9e4066Sahrens }
88fa9e4066Sahrens 
89fa9e4066Sahrens int
90fa9e4066Sahrens dmu_object_claim(objset_t *os, uint64_t object, dmu_object_type_t ot,
91fa9e4066Sahrens     int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
92fa9e4066Sahrens {
93fa9e4066Sahrens 	dnode_t *dn;
94ea8dc4b6Seschrock 	int err;
95fa9e4066Sahrens 
96ea8dc4b6Seschrock 	if (object == DMU_META_DNODE_OBJECT && !dmu_tx_private_ok(tx))
97fa9e4066Sahrens 		return (EBADF);
98fa9e4066Sahrens 
99ea8dc4b6Seschrock 	err = dnode_hold_impl(os->os, object, DNODE_MUST_BE_FREE, FTAG, &dn);
100ea8dc4b6Seschrock 	if (err)
101ea8dc4b6Seschrock 		return (err);
102fa9e4066Sahrens 	dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
103fa9e4066Sahrens 	dnode_rele(dn, FTAG);
104fa9e4066Sahrens 
105fa9e4066Sahrens 	dmu_tx_add_new_object(tx, os, object);
106fa9e4066Sahrens 	return (0);
107fa9e4066Sahrens }
108fa9e4066Sahrens 
109fa9e4066Sahrens int
110fa9e4066Sahrens dmu_object_reclaim(objset_t *os, uint64_t object, dmu_object_type_t ot,
111fa9e4066Sahrens     int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
112fa9e4066Sahrens {
113fa9e4066Sahrens 	dnode_t *dn;
114ea8dc4b6Seschrock 	int err;
115fa9e4066Sahrens 
116ea8dc4b6Seschrock 	if (object == DMU_META_DNODE_OBJECT && !dmu_tx_private_ok(tx))
117fa9e4066Sahrens 		return (EBADF);
118fa9e4066Sahrens 
119ea8dc4b6Seschrock 	err = dnode_hold_impl(os->os, object, DNODE_MUST_BE_ALLOCATED,
120ea8dc4b6Seschrock 	    FTAG, &dn);
121ea8dc4b6Seschrock 	if (err)
122ea8dc4b6Seschrock 		return (err);
123fa9e4066Sahrens 	dnode_reallocate(dn, ot, blocksize, bonustype, bonuslen, tx);
124fa9e4066Sahrens 	dnode_rele(dn, FTAG);
125fa9e4066Sahrens 
126fa9e4066Sahrens 	return (0);
127fa9e4066Sahrens }
128fa9e4066Sahrens 
129fa9e4066Sahrens int
130fa9e4066Sahrens dmu_object_free(objset_t *os, uint64_t object, dmu_tx_t *tx)
131fa9e4066Sahrens {
132fa9e4066Sahrens 	dnode_t *dn;
133ea8dc4b6Seschrock 	int err;
134fa9e4066Sahrens 
135ea8dc4b6Seschrock 	ASSERT(object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
136fa9e4066Sahrens 
137ea8dc4b6Seschrock 	err = dnode_hold_impl(os->os, object, DNODE_MUST_BE_ALLOCATED,
138ea8dc4b6Seschrock 	    FTAG, &dn);
139ea8dc4b6Seschrock 	if (err)
140ea8dc4b6Seschrock 		return (err);
141fa9e4066Sahrens 
142fa9e4066Sahrens 	ASSERT(dn->dn_type != DMU_OT_NONE);
143*cdb0ab79Smaybee 	dnode_free_range(dn, 0, DMU_OBJECT_END, tx);
144fa9e4066Sahrens 	dnode_free(dn, tx);
145fa9e4066Sahrens 	dnode_rele(dn, FTAG);
146fa9e4066Sahrens 
147fa9e4066Sahrens 	return (0);
148fa9e4066Sahrens }
149fa9e4066Sahrens 
150fa9e4066Sahrens int
1516754306eSahrens dmu_object_next(objset_t *os, uint64_t *objectp, boolean_t hole, uint64_t txg)
152fa9e4066Sahrens {
153fa9e4066Sahrens 	uint64_t offset = (*objectp + 1) << DNODE_SHIFT;
154fa9e4066Sahrens 	int error;
155fa9e4066Sahrens 
156fa9e4066Sahrens 	error = dnode_next_offset(os->os->os_meta_dnode,
157*cdb0ab79Smaybee 	    (hole ? DNODE_FIND_HOLE : 0), &offset, 0, DNODES_PER_BLOCK, txg);
158fa9e4066Sahrens 
159fa9e4066Sahrens 	*objectp = offset >> DNODE_SHIFT;
160fa9e4066Sahrens 
161fa9e4066Sahrens 	return (error);
162fa9e4066Sahrens }
163