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 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 TUNABLE_INT("vfs.zfs.reference_tracking_enable", &reference_tracking_enable);
35 SYSCTL_INT(_vfs_zfs, OID_AUTO, reference_tracking_enable, CTLFLAG_RDTUN,
36 &reference_tracking_enable, 0,
37 "Track reference holders to refcount_t objects, used mostly by ZFS");
39 int reference_tracking_enable = TRUE;
41 int reference_history = 3; /* tunable */
43 static kmem_cache_t *reference_cache;
44 static kmem_cache_t *reference_history_cache;
47 refcount_sysinit(void)
49 reference_cache = kmem_cache_create("reference_cache",
50 sizeof (reference_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
52 reference_history_cache = kmem_cache_create("reference_history_cache",
53 sizeof (uint64_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
59 kmem_cache_destroy(reference_cache);
60 kmem_cache_destroy(reference_history_cache);
64 refcount_create(refcount_t *rc)
66 mutex_init(&rc->rc_mtx, NULL, MUTEX_DEFAULT, NULL);
67 list_create(&rc->rc_list, sizeof (reference_t),
68 offsetof(reference_t, ref_link));
69 list_create(&rc->rc_removed, sizeof (reference_t),
70 offsetof(reference_t, ref_link));
72 rc->rc_removed_count = 0;
73 rc->rc_tracked = reference_tracking_enable;
77 refcount_create_untracked(refcount_t *rc)
80 rc->rc_tracked = B_FALSE;
84 refcount_destroy_many(refcount_t *rc, uint64_t number)
88 ASSERT(rc->rc_count == number);
89 while (ref = list_head(&rc->rc_list)) {
90 list_remove(&rc->rc_list, ref);
91 kmem_cache_free(reference_cache, ref);
93 list_destroy(&rc->rc_list);
95 while (ref = list_head(&rc->rc_removed)) {
96 list_remove(&rc->rc_removed, ref);
97 kmem_cache_free(reference_history_cache, ref->ref_removed);
98 kmem_cache_free(reference_cache, ref);
100 list_destroy(&rc->rc_removed);
101 mutex_destroy(&rc->rc_mtx);
105 refcount_destroy(refcount_t *rc)
107 refcount_destroy_many(rc, 0);
111 refcount_is_zero(refcount_t *rc)
113 return (rc->rc_count == 0);
117 refcount_count(refcount_t *rc)
119 return (rc->rc_count);
123 refcount_add_many(refcount_t *rc, uint64_t number, void *holder)
125 reference_t *ref = NULL;
128 if (rc->rc_tracked) {
129 ref = kmem_cache_alloc(reference_cache, KM_SLEEP);
130 ref->ref_holder = holder;
131 ref->ref_number = number;
133 mutex_enter(&rc->rc_mtx);
134 ASSERT(rc->rc_count >= 0);
136 list_insert_head(&rc->rc_list, ref);
137 rc->rc_count += number;
138 count = rc->rc_count;
139 mutex_exit(&rc->rc_mtx);
145 refcount_add(refcount_t *rc, void *holder)
147 return (refcount_add_many(rc, 1, holder));
151 refcount_remove_many(refcount_t *rc, uint64_t number, void *holder)
156 mutex_enter(&rc->rc_mtx);
157 ASSERT(rc->rc_count >= number);
159 if (!rc->rc_tracked) {
160 rc->rc_count -= number;
161 count = rc->rc_count;
162 mutex_exit(&rc->rc_mtx);
166 for (ref = list_head(&rc->rc_list); ref;
167 ref = list_next(&rc->rc_list, ref)) {
168 if (ref->ref_holder == holder && ref->ref_number == number) {
169 list_remove(&rc->rc_list, ref);
170 if (reference_history > 0) {
172 kmem_cache_alloc(reference_history_cache,
174 list_insert_head(&rc->rc_removed, ref);
175 rc->rc_removed_count++;
176 if (rc->rc_removed_count > reference_history) {
177 ref = list_tail(&rc->rc_removed);
178 list_remove(&rc->rc_removed, ref);
179 kmem_cache_free(reference_history_cache,
181 kmem_cache_free(reference_cache, ref);
182 rc->rc_removed_count--;
185 kmem_cache_free(reference_cache, ref);
187 rc->rc_count -= number;
188 count = rc->rc_count;
189 mutex_exit(&rc->rc_mtx);
193 panic("No such hold %p on refcount %llx", holder,
194 (u_longlong_t)(uintptr_t)rc);
199 refcount_remove(refcount_t *rc, void *holder)
201 return (refcount_remove_many(rc, 1, holder));
205 refcount_transfer(refcount_t *dst, refcount_t *src)
207 int64_t count, removed_count;
208 list_t list, removed;
210 list_create(&list, sizeof (reference_t),
211 offsetof(reference_t, ref_link));
212 list_create(&removed, sizeof (reference_t),
213 offsetof(reference_t, ref_link));
215 mutex_enter(&src->rc_mtx);
216 count = src->rc_count;
217 removed_count = src->rc_removed_count;
219 src->rc_removed_count = 0;
220 list_move_tail(&list, &src->rc_list);
221 list_move_tail(&removed, &src->rc_removed);
222 mutex_exit(&src->rc_mtx);
224 mutex_enter(&dst->rc_mtx);
225 dst->rc_count += count;
226 dst->rc_removed_count += removed_count;
227 list_move_tail(&dst->rc_list, &list);
228 list_move_tail(&dst->rc_removed, &removed);
229 mutex_exit(&dst->rc_mtx);
232 list_destroy(&removed);
236 refcount_transfer_ownership(refcount_t *rc, void *current_holder,
240 boolean_t found = B_FALSE;
242 mutex_enter(&rc->rc_mtx);
243 if (!rc->rc_tracked) {
244 mutex_exit(&rc->rc_mtx);
248 for (ref = list_head(&rc->rc_list); ref;
249 ref = list_next(&rc->rc_list, ref)) {
250 if (ref->ref_holder == current_holder) {
251 ref->ref_holder = new_holder;
257 mutex_exit(&rc->rc_mtx);
259 #endif /* ZFS_DEBUG */