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.
25 #include <sys/zfs_context.h>
26 #include <sys/refcount.h>
31 int reference_tracking_enable
= FALSE
; /* runs out of memory too easily */
33 int reference_tracking_enable
= TRUE
;
35 int reference_history
= 4; /* 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;
70 refcount_destroy_many(refcount_t
*rc
, uint64_t number
)
74 ASSERT(rc
->rc_count
== number
);
75 while (ref
= list_head(&rc
->rc_list
)) {
76 list_remove(&rc
->rc_list
, ref
);
77 kmem_cache_free(reference_cache
, ref
);
79 list_destroy(&rc
->rc_list
);
81 while (ref
= list_head(&rc
->rc_removed
)) {
82 list_remove(&rc
->rc_removed
, ref
);
83 kmem_cache_free(reference_history_cache
, ref
->ref_removed
);
84 kmem_cache_free(reference_cache
, ref
);
86 list_destroy(&rc
->rc_removed
);
87 mutex_destroy(&rc
->rc_mtx
);
91 refcount_destroy(refcount_t
*rc
)
93 refcount_destroy_many(rc
, 0);
97 refcount_is_zero(refcount_t
*rc
)
99 ASSERT(rc
->rc_count
>= 0);
100 return (rc
->rc_count
== 0);
104 refcount_count(refcount_t
*rc
)
106 ASSERT(rc
->rc_count
>= 0);
107 return (rc
->rc_count
);
111 refcount_add_many(refcount_t
*rc
, uint64_t number
, void *holder
)
116 if (reference_tracking_enable
) {
117 ref
= kmem_cache_alloc(reference_cache
, KM_SLEEP
);
118 ref
->ref_holder
= holder
;
119 ref
->ref_number
= number
;
121 mutex_enter(&rc
->rc_mtx
);
122 ASSERT(rc
->rc_count
>= 0);
123 if (reference_tracking_enable
)
124 list_insert_head(&rc
->rc_list
, ref
);
125 rc
->rc_count
+= number
;
126 count
= rc
->rc_count
;
127 mutex_exit(&rc
->rc_mtx
);
133 refcount_add(refcount_t
*rc
, void *holder
)
135 return (refcount_add_many(rc
, 1, holder
));
139 refcount_remove_many(refcount_t
*rc
, uint64_t number
, void *holder
)
144 mutex_enter(&rc
->rc_mtx
);
145 ASSERT(rc
->rc_count
>= number
);
147 if (!reference_tracking_enable
) {
148 rc
->rc_count
-= number
;
149 count
= rc
->rc_count
;
150 mutex_exit(&rc
->rc_mtx
);
154 for (ref
= list_head(&rc
->rc_list
); ref
;
155 ref
= list_next(&rc
->rc_list
, ref
)) {
156 if (ref
->ref_holder
== holder
&& ref
->ref_number
== number
) {
157 list_remove(&rc
->rc_list
, ref
);
158 if (reference_history
> 0) {
160 kmem_cache_alloc(reference_history_cache
,
162 list_insert_head(&rc
->rc_removed
, ref
);
163 rc
->rc_removed_count
++;
164 if (rc
->rc_removed_count
>= reference_history
) {
165 ref
= list_tail(&rc
->rc_removed
);
166 list_remove(&rc
->rc_removed
, ref
);
167 kmem_cache_free(reference_history_cache
,
169 kmem_cache_free(reference_cache
, ref
);
170 rc
->rc_removed_count
--;
173 kmem_cache_free(reference_cache
, ref
);
175 rc
->rc_count
-= number
;
176 count
= rc
->rc_count
;
177 mutex_exit(&rc
->rc_mtx
);
181 panic("No such hold %p on refcount %llx", holder
,
182 (u_longlong_t
)(uintptr_t)rc
);
187 refcount_remove(refcount_t
*rc
, void *holder
)
189 return (refcount_remove_many(rc
, 1, holder
));
193 refcount_transfer(refcount_t
*dst
, refcount_t
*src
)
195 int64_t count
, removed_count
;
196 list_t list
, removed
;
198 list_create(&list
, sizeof (reference_t
),
199 offsetof(reference_t
, ref_link
));
200 list_create(&removed
, sizeof (reference_t
),
201 offsetof(reference_t
, ref_link
));
203 mutex_enter(&src
->rc_mtx
);
204 count
= src
->rc_count
;
205 removed_count
= src
->rc_removed_count
;
207 src
->rc_removed_count
= 0;
208 list_move_tail(&list
, &src
->rc_list
);
209 list_move_tail(&removed
, &src
->rc_removed
);
210 mutex_exit(&src
->rc_mtx
);
212 mutex_enter(&dst
->rc_mtx
);
213 dst
->rc_count
+= count
;
214 dst
->rc_removed_count
+= removed_count
;
215 list_move_tail(&dst
->rc_list
, &list
);
216 list_move_tail(&dst
->rc_removed
, &removed
);
217 mutex_exit(&dst
->rc_mtx
);
220 list_destroy(&removed
);
223 #endif /* ZFS_DEBUG */