xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_object.c (revision e77d42eaa49fe55bfae1e0e0065c6e99affc001b)
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 /*
2206e0070dSMark Shellenbaum  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23*e77d42eaSMatthew Ahrens  * Copyright (c) 2013, 2014 by Delphix. All rights reserved.
24*e77d42eaSMatthew Ahrens  * Copyright 2014 HybridCluster. All rights reserved.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #include <sys/dmu.h>
28fa9e4066Sahrens #include <sys/dmu_objset.h>
29fa9e4066Sahrens #include <sys/dmu_tx.h>
30fa9e4066Sahrens #include <sys/dnode.h>
312acef22dSMatthew Ahrens #include <sys/zap.h>
322acef22dSMatthew Ahrens #include <sys/zfeature.h>
33fa9e4066Sahrens 
34fa9e4066Sahrens uint64_t
35fa9e4066Sahrens dmu_object_alloc(objset_t *os, dmu_object_type_t ot, int blocksize,
36fa9e4066Sahrens     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
37fa9e4066Sahrens {
38fa9e4066Sahrens 	uint64_t object;
39fa9e4066Sahrens 	uint64_t L2_dnode_count = DNODES_PER_BLOCK <<
40744947dcSTom Erickson 	    (DMU_META_DNODE(os)->dn_indblkshift - SPA_BLKPTRSHIFT);
41ea8dc4b6Seschrock 	dnode_t *dn = NULL;
42fa9e4066Sahrens 	int restarted = B_FALSE;
43fa9e4066Sahrens 
44503ad85cSMatthew Ahrens 	mutex_enter(&os->os_obj_lock);
45fa9e4066Sahrens 	for (;;) {
46503ad85cSMatthew Ahrens 		object = os->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;
56744947dcSTom Erickson 			int error = dnode_next_offset(DMU_META_DNODE(os),
57cdb0ab79Smaybee 			    DNODE_FIND_HOLE,
58cdb0ab79Smaybee 			    &offset, 2, DNODES_PER_BLOCK >> 2, 0);
59fa9e4066Sahrens 			restarted = B_TRUE;
60fa9e4066Sahrens 			if (error == 0)
61fa9e4066Sahrens 				object = offset >> DNODE_SHIFT;
62fa9e4066Sahrens 		}
63503ad85cSMatthew Ahrens 		os->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 		 */
71503ad85cSMatthew Ahrens 		(void) dnode_hold_impl(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)
77503ad85cSMatthew Ahrens 			os->os_obj_next = object - 1;
78fa9e4066Sahrens 	}
79fa9e4066Sahrens 
80fa9e4066Sahrens 	dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
81fa9e4066Sahrens 	dnode_rele(dn, FTAG);
82fa9e4066Sahrens 
83503ad85cSMatthew Ahrens 	mutex_exit(&os->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))
97be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
98fa9e4066Sahrens 
99503ad85cSMatthew Ahrens 	err = dnode_hold_impl(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,
111*e77d42eaSMatthew Ahrens     int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
112fa9e4066Sahrens {
113fa9e4066Sahrens 	dnode_t *dn;
114ea8dc4b6Seschrock 	int err;
115fa9e4066Sahrens 
1162bf405a2SMark Maybee 	if (object == DMU_META_DNODE_OBJECT)
117be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
118fa9e4066Sahrens 
119503ad85cSMatthew Ahrens 	err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
120ea8dc4b6Seschrock 	    FTAG, &dn);
121ea8dc4b6Seschrock 	if (err)
122ea8dc4b6Seschrock 		return (err);
1232bf405a2SMark Maybee 
124fa9e4066Sahrens 	dnode_reallocate(dn, ot, blocksize, bonustype, bonuslen, tx);
1252bf405a2SMark Maybee 
126fa9e4066Sahrens 	dnode_rele(dn, FTAG);
127cf04dda1SMark Maybee 	return (err);
128fa9e4066Sahrens }
129fa9e4066Sahrens 
130fa9e4066Sahrens int
131fa9e4066Sahrens dmu_object_free(objset_t *os, uint64_t object, dmu_tx_t *tx)
132fa9e4066Sahrens {
133fa9e4066Sahrens 	dnode_t *dn;
134ea8dc4b6Seschrock 	int err;
135fa9e4066Sahrens 
136ea8dc4b6Seschrock 	ASSERT(object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
137fa9e4066Sahrens 
138503ad85cSMatthew Ahrens 	err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
139ea8dc4b6Seschrock 	    FTAG, &dn);
140ea8dc4b6Seschrock 	if (err)
141ea8dc4b6Seschrock 		return (err);
142fa9e4066Sahrens 
143fa9e4066Sahrens 	ASSERT(dn->dn_type != DMU_OT_NONE);
144cdb0ab79Smaybee 	dnode_free_range(dn, 0, DMU_OBJECT_END, tx);
145fa9e4066Sahrens 	dnode_free(dn, tx);
146fa9e4066Sahrens 	dnode_rele(dn, FTAG);
147fa9e4066Sahrens 
148fa9e4066Sahrens 	return (0);
149fa9e4066Sahrens }
150fa9e4066Sahrens 
151fa9e4066Sahrens int
1526754306eSahrens dmu_object_next(objset_t *os, uint64_t *objectp, boolean_t hole, uint64_t txg)
153fa9e4066Sahrens {
154fa9e4066Sahrens 	uint64_t offset = (*objectp + 1) << DNODE_SHIFT;
155fa9e4066Sahrens 	int error;
156fa9e4066Sahrens 
157744947dcSTom Erickson 	error = dnode_next_offset(DMU_META_DNODE(os),
158cdb0ab79Smaybee 	    (hole ? DNODE_FIND_HOLE : 0), &offset, 0, DNODES_PER_BLOCK, txg);
159fa9e4066Sahrens 
160fa9e4066Sahrens 	*objectp = offset >> DNODE_SHIFT;
161fa9e4066Sahrens 
162fa9e4066Sahrens 	return (error);
163fa9e4066Sahrens }
1642acef22dSMatthew Ahrens 
1652acef22dSMatthew Ahrens /*
1662acef22dSMatthew Ahrens  * Turn this object from old_type into DMU_OTN_ZAP_METADATA, and bump the
1672acef22dSMatthew Ahrens  * refcount on SPA_FEATURE_EXTENSIBLE_DATASET.
1682acef22dSMatthew Ahrens  *
1692acef22dSMatthew Ahrens  * Only for use from syncing context, on MOS objects.
1702acef22dSMatthew Ahrens  */
1712acef22dSMatthew Ahrens void
1722acef22dSMatthew Ahrens dmu_object_zapify(objset_t *mos, uint64_t object, dmu_object_type_t old_type,
1732acef22dSMatthew Ahrens     dmu_tx_t *tx)
1742acef22dSMatthew Ahrens {
1752acef22dSMatthew Ahrens 	dnode_t *dn;
1762acef22dSMatthew Ahrens 
1772acef22dSMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
1782acef22dSMatthew Ahrens 
1792acef22dSMatthew Ahrens 	VERIFY0(dnode_hold(mos, object, FTAG, &dn));
1802acef22dSMatthew Ahrens 	if (dn->dn_type == DMU_OTN_ZAP_METADATA) {
1812acef22dSMatthew Ahrens 		dnode_rele(dn, FTAG);
1822acef22dSMatthew Ahrens 		return;
1832acef22dSMatthew Ahrens 	}
1842acef22dSMatthew Ahrens 	ASSERT3U(dn->dn_type, ==, old_type);
1852acef22dSMatthew Ahrens 	ASSERT0(dn->dn_maxblkid);
1862acef22dSMatthew Ahrens 	dn->dn_next_type[tx->tx_txg & TXG_MASK] = dn->dn_type =
1872acef22dSMatthew Ahrens 	    DMU_OTN_ZAP_METADATA;
1882acef22dSMatthew Ahrens 	dnode_setdirty(dn, tx);
1892acef22dSMatthew Ahrens 	dnode_rele(dn, FTAG);
1902acef22dSMatthew Ahrens 
1912acef22dSMatthew Ahrens 	mzap_create_impl(mos, object, 0, 0, tx);
1922acef22dSMatthew Ahrens 
1932acef22dSMatthew Ahrens 	spa_feature_incr(dmu_objset_spa(mos),
1942acef22dSMatthew Ahrens 	    SPA_FEATURE_EXTENSIBLE_DATASET, tx);
1952acef22dSMatthew Ahrens }
1962acef22dSMatthew Ahrens 
1972acef22dSMatthew Ahrens void
1982acef22dSMatthew Ahrens dmu_object_free_zapified(objset_t *mos, uint64_t object, dmu_tx_t *tx)
1992acef22dSMatthew Ahrens {
2002acef22dSMatthew Ahrens 	dnode_t *dn;
2012acef22dSMatthew Ahrens 	dmu_object_type_t t;
2022acef22dSMatthew Ahrens 
2032acef22dSMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
2042acef22dSMatthew Ahrens 
2052acef22dSMatthew Ahrens 	VERIFY0(dnode_hold(mos, object, FTAG, &dn));
2062acef22dSMatthew Ahrens 	t = dn->dn_type;
2072acef22dSMatthew Ahrens 	dnode_rele(dn, FTAG);
2082acef22dSMatthew Ahrens 
2092acef22dSMatthew Ahrens 	if (t == DMU_OTN_ZAP_METADATA) {
2102acef22dSMatthew Ahrens 		spa_feature_decr(dmu_objset_spa(mos),
2112acef22dSMatthew Ahrens 		    SPA_FEATURE_EXTENSIBLE_DATASET, tx);
2122acef22dSMatthew Ahrens 	}
2132acef22dSMatthew Ahrens 	VERIFY0(dmu_object_free(mos, object, tx));
2142acef22dSMatthew Ahrens }
215