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.
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.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
26 #include <sys/zfs_context.h>
27 #include <sys/refcount.h>
32 int reference_tracking_enable
= FALSE
; /* runs out of memory too easily */
34 int reference_tracking_enable
= TRUE
;
36 int reference_history
= 3; /* tunable */
38 static kmem_cache_t
*reference_cache
;
39 static kmem_cache_t
*reference_history_cache
;
44 reference_cache
= kmem_cache_create("reference_cache",
45 sizeof (reference_t
), 0, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
47 reference_history_cache
= kmem_cache_create("reference_history_cache",
48 sizeof (uint64_t), 0, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
54 kmem_cache_destroy(reference_cache
);
55 kmem_cache_destroy(reference_history_cache
);
59 refcount_create(refcount_t
*rc
)
61 mutex_init(&rc
->rc_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
62 list_create(&rc
->rc_list
, sizeof (reference_t
),
63 offsetof(reference_t
, ref_link
));
64 list_create(&rc
->rc_removed
, sizeof (reference_t
),
65 offsetof(reference_t
, ref_link
));
67 rc
->rc_removed_count
= 0;
68 rc
->rc_tracked
= reference_tracking_enable
;
72 refcount_create_tracked(refcount_t
*rc
)
75 rc
->rc_tracked
= B_TRUE
;
79 refcount_create_untracked(refcount_t
*rc
)
82 rc
->rc_tracked
= B_FALSE
;
86 refcount_destroy_many(refcount_t
*rc
, uint64_t number
)
90 ASSERT(rc
->rc_count
== number
);
91 while (ref
= list_head(&rc
->rc_list
)) {
92 list_remove(&rc
->rc_list
, ref
);
93 kmem_cache_free(reference_cache
, ref
);
95 list_destroy(&rc
->rc_list
);
97 while (ref
= list_head(&rc
->rc_removed
)) {
98 list_remove(&rc
->rc_removed
, ref
);
99 kmem_cache_free(reference_history_cache
, ref
->ref_removed
);
100 kmem_cache_free(reference_cache
, ref
);
102 list_destroy(&rc
->rc_removed
);
103 mutex_destroy(&rc
->rc_mtx
);
107 refcount_destroy(refcount_t
*rc
)
109 refcount_destroy_many(rc
, 0);
113 refcount_is_zero(refcount_t
*rc
)
115 return (rc
->rc_count
== 0);
119 refcount_count(refcount_t
*rc
)
121 return (rc
->rc_count
);
125 refcount_add_many(refcount_t
*rc
, uint64_t number
, void *holder
)
127 reference_t
*ref
= NULL
;
130 if (rc
->rc_tracked
) {
131 ref
= kmem_cache_alloc(reference_cache
, KM_SLEEP
);
132 ref
->ref_holder
= holder
;
133 ref
->ref_number
= number
;
135 mutex_enter(&rc
->rc_mtx
);
136 ASSERT(rc
->rc_count
>= 0);
138 list_insert_head(&rc
->rc_list
, ref
);
139 rc
->rc_count
+= number
;
140 count
= rc
->rc_count
;
141 mutex_exit(&rc
->rc_mtx
);
147 refcount_add(refcount_t
*rc
, void *holder
)
149 return (refcount_add_many(rc
, 1, holder
));
153 refcount_remove_many(refcount_t
*rc
, uint64_t number
, void *holder
)
158 mutex_enter(&rc
->rc_mtx
);
159 ASSERT(rc
->rc_count
>= number
);
161 if (!rc
->rc_tracked
) {
162 rc
->rc_count
-= number
;
163 count
= rc
->rc_count
;
164 mutex_exit(&rc
->rc_mtx
);
168 for (ref
= list_head(&rc
->rc_list
); ref
;
169 ref
= list_next(&rc
->rc_list
, ref
)) {
170 if (ref
->ref_holder
== holder
&& ref
->ref_number
== number
) {
171 list_remove(&rc
->rc_list
, ref
);
172 if (reference_history
> 0) {
174 kmem_cache_alloc(reference_history_cache
,
176 list_insert_head(&rc
->rc_removed
, ref
);
177 rc
->rc_removed_count
++;
178 if (rc
->rc_removed_count
> reference_history
) {
179 ref
= list_tail(&rc
->rc_removed
);
180 list_remove(&rc
->rc_removed
, ref
);
181 kmem_cache_free(reference_history_cache
,
183 kmem_cache_free(reference_cache
, ref
);
184 rc
->rc_removed_count
--;
187 kmem_cache_free(reference_cache
, ref
);
189 rc
->rc_count
-= number
;
190 count
= rc
->rc_count
;
191 mutex_exit(&rc
->rc_mtx
);
195 panic("No such hold %p on refcount %llx", holder
,
196 (u_longlong_t
)(uintptr_t)rc
);
201 refcount_remove(refcount_t
*rc
, void *holder
)
203 return (refcount_remove_many(rc
, 1, holder
));
207 refcount_transfer(refcount_t
*dst
, refcount_t
*src
)
209 int64_t count
, removed_count
;
210 list_t list
, removed
;
212 list_create(&list
, sizeof (reference_t
),
213 offsetof(reference_t
, ref_link
));
214 list_create(&removed
, sizeof (reference_t
),
215 offsetof(reference_t
, ref_link
));
217 mutex_enter(&src
->rc_mtx
);
218 count
= src
->rc_count
;
219 removed_count
= src
->rc_removed_count
;
221 src
->rc_removed_count
= 0;
222 list_move_tail(&list
, &src
->rc_list
);
223 list_move_tail(&removed
, &src
->rc_removed
);
224 mutex_exit(&src
->rc_mtx
);
226 mutex_enter(&dst
->rc_mtx
);
227 dst
->rc_count
+= count
;
228 dst
->rc_removed_count
+= removed_count
;
229 list_move_tail(&dst
->rc_list
, &list
);
230 list_move_tail(&dst
->rc_removed
, &removed
);
231 mutex_exit(&dst
->rc_mtx
);
234 list_destroy(&removed
);
238 refcount_transfer_ownership(refcount_t
*rc
, void *current_holder
,
242 boolean_t found
= B_FALSE
;
244 mutex_enter(&rc
->rc_mtx
);
245 if (!rc
->rc_tracked
) {
246 mutex_exit(&rc
->rc_mtx
);
250 for (ref
= list_head(&rc
->rc_list
); ref
;
251 ref
= list_next(&rc
->rc_list
, ref
)) {
252 if (ref
->ref_holder
== current_holder
) {
253 ref
->ref_holder
= new_holder
;
259 mutex_exit(&rc
->rc_mtx
);
263 * If tracking is enabled, return true if a reference exists that matches
264 * the "holder" tag. If tracking is disabled, then return true if a reference
268 refcount_held(refcount_t
*rc
, void *holder
)
272 mutex_enter(&rc
->rc_mtx
);
274 if (!rc
->rc_tracked
) {
275 mutex_exit(&rc
->rc_mtx
);
276 return (rc
->rc_count
> 0);
279 for (ref
= list_head(&rc
->rc_list
); ref
;
280 ref
= list_next(&rc
->rc_list
, ref
)) {
281 if (ref
->ref_holder
== holder
) {
282 mutex_exit(&rc
->rc_mtx
);
286 mutex_exit(&rc
->rc_mtx
);
291 * If tracking is enabled, return true if a reference does not exist that
292 * matches the "holder" tag. If tracking is disabled, always return true
293 * since the reference might not be held.
296 refcount_not_held(refcount_t
*rc
, void *holder
)
300 mutex_enter(&rc
->rc_mtx
);
302 if (!rc
->rc_tracked
) {
303 mutex_exit(&rc
->rc_mtx
);
307 for (ref
= list_head(&rc
->rc_list
); ref
;
308 ref
= list_next(&rc
->rc_list
, ref
)) {
309 if (ref
->ref_holder
== holder
) {
310 mutex_exit(&rc
->rc_mtx
);
314 mutex_exit(&rc
->rc_mtx
);
317 #endif /* ZFS_DEBUG */