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 */
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_untracked(refcount_t
*rc
)
75 rc
->rc_tracked
= B_FALSE
;
79 refcount_destroy_many(refcount_t
*rc
, uint64_t number
)
83 ASSERT(rc
->rc_count
== number
);
84 while (ref
= list_head(&rc
->rc_list
)) {
85 list_remove(&rc
->rc_list
, ref
);
86 kmem_cache_free(reference_cache
, ref
);
88 list_destroy(&rc
->rc_list
);
90 while (ref
= list_head(&rc
->rc_removed
)) {
91 list_remove(&rc
->rc_removed
, ref
);
92 kmem_cache_free(reference_history_cache
, ref
->ref_removed
);
93 kmem_cache_free(reference_cache
, ref
);
95 list_destroy(&rc
->rc_removed
);
96 mutex_destroy(&rc
->rc_mtx
);
100 refcount_destroy(refcount_t
*rc
)
102 refcount_destroy_many(rc
, 0);
106 refcount_is_zero(refcount_t
*rc
)
108 return (rc
->rc_count
== 0);
112 refcount_count(refcount_t
*rc
)
114 return (rc
->rc_count
);
118 refcount_add_many(refcount_t
*rc
, uint64_t number
, void *holder
)
120 reference_t
*ref
= NULL
;
123 if (rc
->rc_tracked
) {
124 ref
= kmem_cache_alloc(reference_cache
, KM_SLEEP
);
125 ref
->ref_holder
= holder
;
126 ref
->ref_number
= number
;
128 mutex_enter(&rc
->rc_mtx
);
129 ASSERT(rc
->rc_count
>= 0);
131 list_insert_head(&rc
->rc_list
, ref
);
132 rc
->rc_count
+= number
;
133 count
= rc
->rc_count
;
134 mutex_exit(&rc
->rc_mtx
);
140 refcount_add(refcount_t
*rc
, void *holder
)
142 return (refcount_add_many(rc
, 1, holder
));
146 refcount_remove_many(refcount_t
*rc
, uint64_t number
, void *holder
)
151 mutex_enter(&rc
->rc_mtx
);
152 ASSERT(rc
->rc_count
>= number
);
154 if (!rc
->rc_tracked
) {
155 rc
->rc_count
-= number
;
156 count
= rc
->rc_count
;
157 mutex_exit(&rc
->rc_mtx
);
161 for (ref
= list_head(&rc
->rc_list
); ref
;
162 ref
= list_next(&rc
->rc_list
, ref
)) {
163 if (ref
->ref_holder
== holder
&& ref
->ref_number
== number
) {
164 list_remove(&rc
->rc_list
, ref
);
165 if (reference_history
> 0) {
167 kmem_cache_alloc(reference_history_cache
,
169 list_insert_head(&rc
->rc_removed
, ref
);
170 rc
->rc_removed_count
++;
171 if (rc
->rc_removed_count
> reference_history
) {
172 ref
= list_tail(&rc
->rc_removed
);
173 list_remove(&rc
->rc_removed
, ref
);
174 kmem_cache_free(reference_history_cache
,
176 kmem_cache_free(reference_cache
, ref
);
177 rc
->rc_removed_count
--;
180 kmem_cache_free(reference_cache
, ref
);
182 rc
->rc_count
-= number
;
183 count
= rc
->rc_count
;
184 mutex_exit(&rc
->rc_mtx
);
188 panic("No such hold %p on refcount %llx", holder
,
189 (u_longlong_t
)(uintptr_t)rc
);
194 refcount_remove(refcount_t
*rc
, void *holder
)
196 return (refcount_remove_many(rc
, 1, holder
));
200 refcount_transfer(refcount_t
*dst
, refcount_t
*src
)
202 int64_t count
, removed_count
;
203 list_t list
, removed
;
205 list_create(&list
, sizeof (reference_t
),
206 offsetof(reference_t
, ref_link
));
207 list_create(&removed
, sizeof (reference_t
),
208 offsetof(reference_t
, ref_link
));
210 mutex_enter(&src
->rc_mtx
);
211 count
= src
->rc_count
;
212 removed_count
= src
->rc_removed_count
;
214 src
->rc_removed_count
= 0;
215 list_move_tail(&list
, &src
->rc_list
);
216 list_move_tail(&removed
, &src
->rc_removed
);
217 mutex_exit(&src
->rc_mtx
);
219 mutex_enter(&dst
->rc_mtx
);
220 dst
->rc_count
+= count
;
221 dst
->rc_removed_count
+= removed_count
;
222 list_move_tail(&dst
->rc_list
, &list
);
223 list_move_tail(&dst
->rc_removed
, &removed
);
224 mutex_exit(&dst
->rc_mtx
);
227 list_destroy(&removed
);
230 #endif /* ZFS_DEBUG */