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 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <sys/zfs_context.h>
29 #include <sys/refcount.h>
31 #if defined(DEBUG) || !defined(_KERNEL)
34 int reference_tracking_enable
= FALSE
; /* runs out of memory too easily */
36 int reference_tracking_enable
= TRUE
;
38 int reference_history
= 4; /* tunable */
40 static kmem_cache_t
*reference_cache
;
41 static kmem_cache_t
*reference_history_cache
;
46 reference_cache
= kmem_cache_create("reference_cache",
47 sizeof (reference_t
), 0, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
49 reference_history_cache
= kmem_cache_create("reference_history_cache",
50 sizeof (uint64_t), 0, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
56 kmem_cache_destroy(reference_cache
);
57 kmem_cache_destroy(reference_history_cache
);
61 refcount_create(refcount_t
*rc
)
63 mutex_init(&rc
->rc_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
64 list_create(&rc
->rc_list
, sizeof (reference_t
),
65 offsetof(reference_t
, ref_link
));
66 list_create(&rc
->rc_removed
, sizeof (reference_t
),
67 offsetof(reference_t
, ref_link
));
69 rc
->rc_removed_count
= 0;
73 refcount_destroy_many(refcount_t
*rc
, uint64_t number
)
77 ASSERT(rc
->rc_count
== number
);
78 while (ref
= list_head(&rc
->rc_list
)) {
79 list_remove(&rc
->rc_list
, ref
);
80 kmem_cache_free(reference_cache
, ref
);
82 list_destroy(&rc
->rc_list
);
84 while (ref
= list_head(&rc
->rc_removed
)) {
85 list_remove(&rc
->rc_removed
, ref
);
86 kmem_cache_free(reference_history_cache
, ref
->ref_removed
);
87 kmem_cache_free(reference_cache
, ref
);
89 list_destroy(&rc
->rc_removed
);
90 mutex_destroy(&rc
->rc_mtx
);
94 refcount_destroy(refcount_t
*rc
)
96 refcount_destroy_many(rc
, 0);
100 refcount_is_zero(refcount_t
*rc
)
102 ASSERT(rc
->rc_count
>= 0);
103 return (rc
->rc_count
== 0);
107 refcount_count(refcount_t
*rc
)
109 ASSERT(rc
->rc_count
>= 0);
110 return (rc
->rc_count
);
114 refcount_add_many(refcount_t
*rc
, uint64_t number
, void *holder
)
119 if (reference_tracking_enable
) {
120 ref
= kmem_cache_alloc(reference_cache
, KM_SLEEP
);
121 ref
->ref_holder
= holder
;
122 ref
->ref_number
= number
;
124 mutex_enter(&rc
->rc_mtx
);
125 ASSERT(rc
->rc_count
>= 0);
126 if (reference_tracking_enable
)
127 list_insert_head(&rc
->rc_list
, ref
);
128 rc
->rc_count
+= number
;
129 count
= rc
->rc_count
;
130 mutex_exit(&rc
->rc_mtx
);
136 refcount_add(refcount_t
*rc
, void *holder
)
138 return (refcount_add_many(rc
, 1, holder
));
142 refcount_remove_many(refcount_t
*rc
, uint64_t number
, void *holder
)
147 mutex_enter(&rc
->rc_mtx
);
148 ASSERT(rc
->rc_count
>= number
);
150 if (!reference_tracking_enable
) {
151 rc
->rc_count
-= number
;
152 count
= rc
->rc_count
;
153 mutex_exit(&rc
->rc_mtx
);
157 for (ref
= list_head(&rc
->rc_list
); ref
;
158 ref
= list_next(&rc
->rc_list
, ref
)) {
159 if (ref
->ref_holder
== holder
&& ref
->ref_number
== number
) {
160 list_remove(&rc
->rc_list
, ref
);
161 if (reference_history
> 0) {
163 kmem_cache_alloc(reference_history_cache
,
165 list_insert_head(&rc
->rc_removed
, ref
);
166 rc
->rc_removed_count
++;
167 if (rc
->rc_removed_count
>= reference_history
) {
168 ref
= list_tail(&rc
->rc_removed
);
169 list_remove(&rc
->rc_removed
, ref
);
170 kmem_cache_free(reference_history_cache
,
172 kmem_cache_free(reference_cache
, ref
);
173 rc
->rc_removed_count
--;
176 kmem_cache_free(reference_cache
, ref
);
178 rc
->rc_count
-= number
;
179 count
= rc
->rc_count
;
180 mutex_exit(&rc
->rc_mtx
);
184 panic("No such hold %p on refcount %llx", holder
,
185 (u_longlong_t
)(uintptr_t)rc
);
190 refcount_remove(refcount_t
*rc
, void *holder
)
192 return (refcount_remove_many(rc
, 1, holder
));