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 */
33 SYSCTL_DECL(_vfs_zfs);
34 SYSCTL_INT(_vfs_zfs, OID_AUTO, reference_tracking_enable, CTLFLAG_RDTUN,
35 &reference_tracking_enable, 0,
36 "Track reference holders to refcount_t objects, used mostly by ZFS");
38 int reference_tracking_enable = TRUE;
40 int reference_history = 3; /* tunable */
42 static kmem_cache_t *reference_cache;
43 static kmem_cache_t *reference_history_cache;
46 zfs_refcount_init(void)
48 reference_cache = kmem_cache_create("reference_cache",
49 sizeof (reference_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
51 reference_history_cache = kmem_cache_create("reference_history_cache",
52 sizeof (uint64_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
56 zfs_refcount_fini(void)
58 kmem_cache_destroy(reference_cache);
59 kmem_cache_destroy(reference_history_cache);
63 zfs_refcount_create(zfs_refcount_t *rc)
65 mutex_init(&rc->rc_mtx, NULL, MUTEX_DEFAULT, NULL);
66 list_create(&rc->rc_list, sizeof (reference_t),
67 offsetof(reference_t, ref_link));
68 list_create(&rc->rc_removed, sizeof (reference_t),
69 offsetof(reference_t, ref_link));
71 rc->rc_removed_count = 0;
72 rc->rc_tracked = reference_tracking_enable;
76 zfs_refcount_create_tracked(zfs_refcount_t *rc)
78 zfs_refcount_create(rc);
79 rc->rc_tracked = B_TRUE;
83 zfs_refcount_create_untracked(zfs_refcount_t *rc)
85 zfs_refcount_create(rc);
86 rc->rc_tracked = B_FALSE;
90 zfs_refcount_destroy_many(zfs_refcount_t *rc, uint64_t number)
94 ASSERT(rc->rc_count == number);
95 while (ref = list_head(&rc->rc_list)) {
96 list_remove(&rc->rc_list, ref);
97 kmem_cache_free(reference_cache, ref);
99 list_destroy(&rc->rc_list);
101 while (ref = list_head(&rc->rc_removed)) {
102 list_remove(&rc->rc_removed, ref);
103 kmem_cache_free(reference_history_cache, ref->ref_removed);
104 kmem_cache_free(reference_cache, ref);
106 list_destroy(&rc->rc_removed);
107 mutex_destroy(&rc->rc_mtx);
111 zfs_refcount_destroy(zfs_refcount_t *rc)
113 zfs_refcount_destroy_many(rc, 0);
117 zfs_refcount_is_zero(zfs_refcount_t *rc)
119 return (rc->rc_count == 0);
123 zfs_refcount_count(zfs_refcount_t *rc)
125 return (rc->rc_count);
129 zfs_refcount_add_many(zfs_refcount_t *rc, uint64_t number, void *holder)
131 reference_t *ref = NULL;
134 if (rc->rc_tracked) {
135 ref = kmem_cache_alloc(reference_cache, KM_SLEEP);
136 ref->ref_holder = holder;
137 ref->ref_number = number;
139 mutex_enter(&rc->rc_mtx);
140 ASSERT(rc->rc_count >= 0);
142 list_insert_head(&rc->rc_list, ref);
143 rc->rc_count += number;
144 count = rc->rc_count;
145 mutex_exit(&rc->rc_mtx);
151 zfs_refcount_add(zfs_refcount_t *rc, void *holder)
153 return (zfs_refcount_add_many(rc, 1, holder));
157 zfs_refcount_remove_many(zfs_refcount_t *rc, uint64_t number, void *holder)
162 mutex_enter(&rc->rc_mtx);
163 ASSERT(rc->rc_count >= number);
165 if (!rc->rc_tracked) {
166 rc->rc_count -= number;
167 count = rc->rc_count;
168 mutex_exit(&rc->rc_mtx);
172 for (ref = list_head(&rc->rc_list); ref;
173 ref = list_next(&rc->rc_list, ref)) {
174 if (ref->ref_holder == holder && ref->ref_number == number) {
175 list_remove(&rc->rc_list, ref);
176 if (reference_history > 0) {
178 kmem_cache_alloc(reference_history_cache,
180 list_insert_head(&rc->rc_removed, ref);
181 rc->rc_removed_count++;
182 if (rc->rc_removed_count > reference_history) {
183 ref = list_tail(&rc->rc_removed);
184 list_remove(&rc->rc_removed, ref);
185 kmem_cache_free(reference_history_cache,
187 kmem_cache_free(reference_cache, ref);
188 rc->rc_removed_count--;
191 kmem_cache_free(reference_cache, ref);
193 rc->rc_count -= number;
194 count = rc->rc_count;
195 mutex_exit(&rc->rc_mtx);
199 panic("No such hold %p on refcount %llx", holder,
200 (u_longlong_t)(uintptr_t)rc);
205 zfs_refcount_remove(zfs_refcount_t *rc, void *holder)
207 return (zfs_refcount_remove_many(rc, 1, holder));
211 zfs_refcount_transfer(zfs_refcount_t *dst, zfs_refcount_t *src)
213 int64_t count, removed_count;
214 list_t list, removed;
216 list_create(&list, sizeof (reference_t),
217 offsetof(reference_t, ref_link));
218 list_create(&removed, sizeof (reference_t),
219 offsetof(reference_t, ref_link));
221 mutex_enter(&src->rc_mtx);
222 count = src->rc_count;
223 removed_count = src->rc_removed_count;
225 src->rc_removed_count = 0;
226 list_move_tail(&list, &src->rc_list);
227 list_move_tail(&removed, &src->rc_removed);
228 mutex_exit(&src->rc_mtx);
230 mutex_enter(&dst->rc_mtx);
231 dst->rc_count += count;
232 dst->rc_removed_count += removed_count;
233 list_move_tail(&dst->rc_list, &list);
234 list_move_tail(&dst->rc_removed, &removed);
235 mutex_exit(&dst->rc_mtx);
238 list_destroy(&removed);
242 zfs_refcount_transfer_ownership(zfs_refcount_t *rc, void *current_holder,
246 boolean_t found = B_FALSE;
248 mutex_enter(&rc->rc_mtx);
249 if (!rc->rc_tracked) {
250 mutex_exit(&rc->rc_mtx);
254 for (ref = list_head(&rc->rc_list); ref;
255 ref = list_next(&rc->rc_list, ref)) {
256 if (ref->ref_holder == current_holder) {
257 ref->ref_holder = new_holder;
263 mutex_exit(&rc->rc_mtx);
267 * If tracking is enabled, return true if a reference exists that matches
268 * the "holder" tag. If tracking is disabled, then return true if a reference
272 zfs_refcount_held(zfs_refcount_t *rc, void *holder)
276 mutex_enter(&rc->rc_mtx);
278 if (!rc->rc_tracked) {
279 mutex_exit(&rc->rc_mtx);
280 return (rc->rc_count > 0);
283 for (ref = list_head(&rc->rc_list); ref;
284 ref = list_next(&rc->rc_list, ref)) {
285 if (ref->ref_holder == holder) {
286 mutex_exit(&rc->rc_mtx);
290 mutex_exit(&rc->rc_mtx);
295 * If tracking is enabled, return true if a reference does not exist that
296 * matches the "holder" tag. If tracking is disabled, always return true
297 * since the reference might not be held.
300 zfs_refcount_not_held(zfs_refcount_t *rc, void *holder)
304 mutex_enter(&rc->rc_mtx);
306 if (!rc->rc_tracked) {
307 mutex_exit(&rc->rc_mtx);
311 for (ref = list_head(&rc->rc_list); ref;
312 ref = list_next(&rc->rc_list, ref)) {
313 if (ref->ref_holder == holder) {
314 mutex_exit(&rc->rc_mtx);
318 mutex_exit(&rc->rc_mtx);
321 #endif /* ZFS_DEBUG */