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>
30 int reference_tracking_enable = FALSE; /* runs out of memory too easily */
32 int reference_tracking_enable = TRUE;
34 int reference_history = 3; /* tunable */
37 static kmem_cache_t *reference_cache;
38 static kmem_cache_t *reference_history_cache;
43 reference_cache = kmem_cache_create("reference_cache",
44 sizeof (reference_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
46 reference_history_cache = kmem_cache_create("reference_history_cache",
47 sizeof (uint64_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
53 kmem_cache_destroy(reference_cache);
54 kmem_cache_destroy(reference_history_cache);
58 refcount_create(refcount_t *rc)
60 mutex_init(&rc->rc_mtx, NULL, MUTEX_DEFAULT, NULL);
61 list_create(&rc->rc_list, sizeof (reference_t),
62 offsetof(reference_t, ref_link));
63 list_create(&rc->rc_removed, sizeof (reference_t),
64 offsetof(reference_t, ref_link));
66 rc->rc_removed_count = 0;
67 rc->rc_tracked = reference_tracking_enable;
71 refcount_create_tracked(refcount_t *rc)
74 rc->rc_tracked = B_TRUE;
78 refcount_create_untracked(refcount_t *rc)
81 rc->rc_tracked = B_FALSE;
85 refcount_destroy_many(refcount_t *rc, uint64_t number)
89 ASSERT(rc->rc_count == number);
90 while ((ref = list_head(&rc->rc_list))) {
91 list_remove(&rc->rc_list, ref);
92 kmem_cache_free(reference_cache, ref);
94 list_destroy(&rc->rc_list);
96 while ((ref = list_head(&rc->rc_removed))) {
97 list_remove(&rc->rc_removed, ref);
98 kmem_cache_free(reference_history_cache, ref->ref_removed);
99 kmem_cache_free(reference_cache, ref);
101 list_destroy(&rc->rc_removed);
102 mutex_destroy(&rc->rc_mtx);
106 refcount_destroy(refcount_t *rc)
108 refcount_destroy_many(rc, 0);
112 refcount_is_zero(refcount_t *rc)
114 return (rc->rc_count == 0);
118 refcount_count(refcount_t *rc)
120 return (rc->rc_count);
124 refcount_add_many(refcount_t *rc, uint64_t number, void *holder)
126 reference_t *ref = NULL;
129 if (rc->rc_tracked) {
130 ref = kmem_cache_alloc(reference_cache, KM_SLEEP);
131 ref->ref_holder = holder;
132 ref->ref_number = number;
134 mutex_enter(&rc->rc_mtx);
135 ASSERT(rc->rc_count >= 0);
137 list_insert_head(&rc->rc_list, ref);
138 rc->rc_count += number;
139 count = rc->rc_count;
140 mutex_exit(&rc->rc_mtx);
146 zfs_refcount_add(refcount_t *rc, void *holder)
148 return (refcount_add_many(rc, 1, holder));
152 refcount_remove_many(refcount_t *rc, uint64_t number, void *holder)
157 mutex_enter(&rc->rc_mtx);
158 ASSERT(rc->rc_count >= number);
160 if (!rc->rc_tracked) {
161 rc->rc_count -= number;
162 count = rc->rc_count;
163 mutex_exit(&rc->rc_mtx);
167 for (ref = list_head(&rc->rc_list); ref;
168 ref = list_next(&rc->rc_list, ref)) {
169 if (ref->ref_holder == holder && ref->ref_number == number) {
170 list_remove(&rc->rc_list, ref);
171 if (reference_history > 0) {
173 kmem_cache_alloc(reference_history_cache,
175 list_insert_head(&rc->rc_removed, ref);
176 rc->rc_removed_count++;
177 if (rc->rc_removed_count > reference_history) {
178 ref = list_tail(&rc->rc_removed);
179 list_remove(&rc->rc_removed, ref);
180 kmem_cache_free(reference_history_cache,
182 kmem_cache_free(reference_cache, ref);
183 rc->rc_removed_count--;
186 kmem_cache_free(reference_cache, ref);
188 rc->rc_count -= number;
189 count = rc->rc_count;
190 mutex_exit(&rc->rc_mtx);
194 panic("No such hold %p on refcount %llx", holder,
195 (u_longlong_t)(uintptr_t)rc);
200 refcount_remove(refcount_t *rc, void *holder)
202 return (refcount_remove_many(rc, 1, holder));
206 refcount_transfer(refcount_t *dst, refcount_t *src)
208 int64_t count, removed_count;
209 list_t list, removed;
211 list_create(&list, sizeof (reference_t),
212 offsetof(reference_t, ref_link));
213 list_create(&removed, sizeof (reference_t),
214 offsetof(reference_t, ref_link));
216 mutex_enter(&src->rc_mtx);
217 count = src->rc_count;
218 removed_count = src->rc_removed_count;
220 src->rc_removed_count = 0;
221 list_move_tail(&list, &src->rc_list);
222 list_move_tail(&removed, &src->rc_removed);
223 mutex_exit(&src->rc_mtx);
225 mutex_enter(&dst->rc_mtx);
226 dst->rc_count += count;
227 dst->rc_removed_count += removed_count;
228 list_move_tail(&dst->rc_list, &list);
229 list_move_tail(&dst->rc_removed, &removed);
230 mutex_exit(&dst->rc_mtx);
233 list_destroy(&removed);
237 refcount_transfer_ownership(refcount_t *rc, void *current_holder,
241 boolean_t found = B_FALSE;
243 mutex_enter(&rc->rc_mtx);
244 if (!rc->rc_tracked) {
245 mutex_exit(&rc->rc_mtx);
249 for (ref = list_head(&rc->rc_list); ref;
250 ref = list_next(&rc->rc_list, ref)) {
251 if (ref->ref_holder == current_holder) {
252 ref->ref_holder = new_holder;
258 mutex_exit(&rc->rc_mtx);
262 * If tracking is enabled, return true if a reference exists that matches
263 * the "holder" tag. If tracking is disabled, then return true if a reference
267 refcount_held(refcount_t *rc, void *holder)
271 mutex_enter(&rc->rc_mtx);
273 if (!rc->rc_tracked) {
274 mutex_exit(&rc->rc_mtx);
275 return (rc->rc_count > 0);
278 for (ref = list_head(&rc->rc_list); ref;
279 ref = list_next(&rc->rc_list, ref)) {
280 if (ref->ref_holder == holder) {
281 mutex_exit(&rc->rc_mtx);
285 mutex_exit(&rc->rc_mtx);
290 * If tracking is enabled, return true if a reference does not exist that
291 * matches the "holder" tag. If tracking is disabled, always return true
292 * since the reference might not be held.
295 refcount_not_held(refcount_t *rc, void *holder)
299 mutex_enter(&rc->rc_mtx);
301 if (!rc->rc_tracked) {
302 mutex_exit(&rc->rc_mtx);
306 for (ref = list_head(&rc->rc_list); ref;
307 ref = list_next(&rc->rc_list, ref)) {
308 if (ref->ref_holder == holder) {
309 mutex_exit(&rc->rc_mtx);
313 mutex_exit(&rc->rc_mtx);
316 #endif /* ZFS_DEBUG */