xref: /titanic_52/usr/src/uts/common/fs/zfs/dmu_object.c (revision 36e852a172cba914383d7341c988128b2c667fbd)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/dmu.h>
27 #include <sys/dmu_objset.h>
28 #include <sys/dmu_tx.h>
29 #include <sys/dnode.h>
30 
31 uint64_t
32 dmu_object_alloc(objset_t *os, dmu_object_type_t ot, int blocksize,
33     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
34 {
35 	uint64_t object;
36 	uint64_t L2_dnode_count = DNODES_PER_BLOCK <<
37 	    (os->os_meta_dnode->dn_indblkshift - SPA_BLKPTRSHIFT);
38 	dnode_t *dn = NULL;
39 	int restarted = B_FALSE;
40 
41 	mutex_enter(&os->os_obj_lock);
42 	for (;;) {
43 		object = os->os_obj_next;
44 		/*
45 		 * Each time we polish off an L2 bp worth of dnodes
46 		 * (2^13 objects), move to another L2 bp that's still
47 		 * reasonably sparse (at most 1/4 full).  Look from the
48 		 * beginning once, but after that keep looking from here.
49 		 * If we can't find one, just keep going from here.
50 		 */
51 		if (P2PHASE(object, L2_dnode_count) == 0) {
52 			uint64_t offset = restarted ? object << DNODE_SHIFT : 0;
53 			int error = dnode_next_offset(os->os_meta_dnode,
54 			    DNODE_FIND_HOLE,
55 			    &offset, 2, DNODES_PER_BLOCK >> 2, 0);
56 			restarted = B_TRUE;
57 			if (error == 0)
58 				object = offset >> DNODE_SHIFT;
59 		}
60 		os->os_obj_next = ++object;
61 
62 		/*
63 		 * XXX We should check for an i/o error here and return
64 		 * up to our caller.  Actually we should pre-read it in
65 		 * dmu_tx_assign(), but there is currently no mechanism
66 		 * to do so.
67 		 */
68 		(void) dnode_hold_impl(os, object, DNODE_MUST_BE_FREE,
69 		    FTAG, &dn);
70 		if (dn)
71 			break;
72 
73 		if (dmu_object_next(os, &object, B_TRUE, 0) == 0)
74 			os->os_obj_next = object - 1;
75 	}
76 
77 	dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
78 	dnode_rele(dn, FTAG);
79 
80 	mutex_exit(&os->os_obj_lock);
81 
82 	dmu_tx_add_new_object(tx, os, object);
83 	return (object);
84 }
85 
86 int
87 dmu_object_claim(objset_t *os, uint64_t object, dmu_object_type_t ot,
88     int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
89 {
90 	dnode_t *dn;
91 	int err;
92 
93 	if (object == DMU_META_DNODE_OBJECT && !dmu_tx_private_ok(tx))
94 		return (EBADF);
95 
96 	err = dnode_hold_impl(os, object, DNODE_MUST_BE_FREE, FTAG, &dn);
97 	if (err)
98 		return (err);
99 	dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
100 	dnode_rele(dn, FTAG);
101 
102 	dmu_tx_add_new_object(tx, os, object);
103 	return (0);
104 }
105 
106 int
107 dmu_object_reclaim(objset_t *os, uint64_t object, dmu_object_type_t ot,
108     int blocksize, dmu_object_type_t bonustype, int bonuslen)
109 {
110 	dnode_t *dn;
111 	dmu_tx_t *tx;
112 	int nblkptr;
113 	int err;
114 
115 	if (object == DMU_META_DNODE_OBJECT)
116 		return (EBADF);
117 
118 	err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
119 	    FTAG, &dn);
120 	if (err)
121 		return (err);
122 
123 	if (dn->dn_type == ot && dn->dn_datablksz == blocksize &&
124 	    dn->dn_bonustype == bonustype && dn->dn_bonuslen == bonuslen) {
125 		/* nothing is changing, this is a noop */
126 		dnode_rele(dn, FTAG);
127 		return (0);
128 	}
129 
130 	nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
131 
132 	/*
133 	 * If we are losing blkptrs or changing the block size this must
134 	 * be a new file instance.   We must clear out the previous file
135 	 * contents before we can change this type of metadata in the dnode.
136 	 */
137 	if (dn->dn_nblkptr > nblkptr || dn->dn_datablksz != blocksize) {
138 		err = dmu_free_long_range(os, object, 0, DMU_OBJECT_END);
139 		if (err)
140 			goto out;
141 	}
142 
143 	tx = dmu_tx_create(os);
144 	dmu_tx_hold_bonus(tx, object);
145 	err = dmu_tx_assign(tx, TXG_WAIT);
146 	if (err) {
147 		dmu_tx_abort(tx);
148 		goto out;
149 	}
150 
151 	dnode_reallocate(dn, ot, blocksize, bonustype, bonuslen, tx);
152 
153 	dmu_tx_commit(tx);
154 out:
155 	dnode_rele(dn, FTAG);
156 
157 	return (err);
158 }
159 
160 int
161 dmu_object_free(objset_t *os, uint64_t object, dmu_tx_t *tx)
162 {
163 	dnode_t *dn;
164 	int err;
165 
166 	ASSERT(object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
167 
168 	err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
169 	    FTAG, &dn);
170 	if (err)
171 		return (err);
172 
173 	ASSERT(dn->dn_type != DMU_OT_NONE);
174 	dnode_free_range(dn, 0, DMU_OBJECT_END, tx);
175 	dnode_free(dn, tx);
176 	dnode_rele(dn, FTAG);
177 
178 	return (0);
179 }
180 
181 int
182 dmu_object_next(objset_t *os, uint64_t *objectp, boolean_t hole, uint64_t txg)
183 {
184 	uint64_t offset = (*objectp + 1) << DNODE_SHIFT;
185 	int error;
186 
187 	error = dnode_next_offset(os->os_meta_dnode,
188 	    (hole ? DNODE_FIND_HOLE : 0), &offset, 0, DNODES_PER_BLOCK, txg);
189 
190 	*objectp = offset >> DNODE_SHIFT;
191 
192 	return (error);
193 }
194