xref: /titanic_44/usr/src/uts/common/fs/zfs/dmu_object.c (revision da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/dmu.h>
29 #include <sys/dmu_objset.h>
30 #include <sys/dmu_tx.h>
31 #include <sys/dnode.h>
32 
33 uint64_t
34 dmu_object_alloc(objset_t *os, dmu_object_type_t ot, int blocksize,
35     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
36 {
37 	objset_impl_t *osi = os->os;
38 	uint64_t object;
39 	uint64_t L2_dnode_count = DNODES_PER_BLOCK <<
40 	    (osi->os_meta_dnode->dn_indblkshift - SPA_BLKPTRSHIFT);
41 	dnode_t *dn = NULL;
42 	int restarted = B_FALSE;
43 
44 	mutex_enter(&osi->os_obj_lock);
45 	for (;;) {
46 		object = osi->os_obj_next;
47 		/*
48 		 * Each time we polish off an L2 bp worth of dnodes
49 		 * (2^13 objects), move to another L2 bp that's still
50 		 * reasonably sparse (at most 1/4 full).  Look from the
51 		 * beginning once, but after that keep looking from here.
52 		 * If we can't find one, just keep going from here.
53 		 */
54 		if (P2PHASE(object, L2_dnode_count) == 0) {
55 			uint64_t offset = restarted ? object << DNODE_SHIFT : 0;
56 			int error = dnode_next_offset(osi->os_meta_dnode,
57 			    B_TRUE, &offset, 2, DNODES_PER_BLOCK >> 2, 0);
58 			restarted = B_TRUE;
59 			if (error == 0)
60 				object = offset >> DNODE_SHIFT;
61 		}
62 		osi->os_obj_next = ++object;
63 
64 		/*
65 		 * XXX We should check for an i/o error here and return
66 		 * up to our caller.  Actually we should pre-read it in
67 		 * dmu_tx_assign(), but there is currently no mechanism
68 		 * to do so.
69 		 */
70 		(void) dnode_hold_impl(os->os, object, DNODE_MUST_BE_FREE,
71 		    FTAG, &dn);
72 		if (dn)
73 			break;
74 
75 		if (dmu_object_next(os, &object, B_TRUE, 0) == 0)
76 			osi->os_obj_next = object - 1;
77 	}
78 
79 	dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
80 	dnode_rele(dn, FTAG);
81 
82 	mutex_exit(&osi->os_obj_lock);
83 
84 	dmu_tx_add_new_object(tx, os, object);
85 	return (object);
86 }
87 
88 int
89 dmu_object_claim(objset_t *os, uint64_t object, dmu_object_type_t ot,
90     int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
91 {
92 	dnode_t *dn;
93 	int err;
94 
95 	if (object == DMU_META_DNODE_OBJECT && !dmu_tx_private_ok(tx))
96 		return (EBADF);
97 
98 	err = dnode_hold_impl(os->os, object, DNODE_MUST_BE_FREE, FTAG, &dn);
99 	if (err)
100 		return (err);
101 	dnode_allocate(dn, ot, blocksize, 0, bonustype, bonuslen, tx);
102 	dnode_rele(dn, FTAG);
103 
104 	dmu_tx_add_new_object(tx, os, object);
105 	return (0);
106 }
107 
108 int
109 dmu_object_reclaim(objset_t *os, uint64_t object, dmu_object_type_t ot,
110     int blocksize, dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
111 {
112 	dnode_t *dn;
113 	int err;
114 
115 	if (object == DMU_META_DNODE_OBJECT && !dmu_tx_private_ok(tx))
116 		return (EBADF);
117 
118 	err = dnode_hold_impl(os->os, object, DNODE_MUST_BE_ALLOCATED,
119 	    FTAG, &dn);
120 	if (err)
121 		return (err);
122 	dnode_reallocate(dn, ot, blocksize, bonustype, bonuslen, tx);
123 	dnode_rele(dn, FTAG);
124 
125 	return (0);
126 }
127 
128 int
129 dmu_object_free(objset_t *os, uint64_t object, dmu_tx_t *tx)
130 {
131 	dnode_t *dn;
132 	int err;
133 
134 	ASSERT(object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
135 
136 	err = dnode_hold_impl(os->os, object, DNODE_MUST_BE_ALLOCATED,
137 	    FTAG, &dn);
138 	if (err)
139 		return (err);
140 
141 	ASSERT(dn->dn_type != DMU_OT_NONE);
142 	dnode_free(dn, tx);
143 	dnode_rele(dn, FTAG);
144 
145 	return (0);
146 }
147 
148 int
149 dmu_object_next(objset_t *os, uint64_t *objectp, boolean_t hole, uint64_t txg)
150 {
151 	uint64_t offset = (*objectp + 1) << DNODE_SHIFT;
152 	int error;
153 
154 	error = dnode_next_offset(os->os->os_meta_dnode,
155 	    hole, &offset, 0, DNODES_PER_BLOCK, txg);
156 
157 	*objectp = offset >> DNODE_SHIFT;
158 
159 	return (error);
160 }
161