4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/zfs_context.h>
30 #include <sys/refcount.h>
32 #if defined(DEBUG) || !defined(_KERNEL)
35 int reference_tracking_enable = FALSE; /* runs out of memory too easily */
37 int reference_tracking_enable = TRUE;
39 int reference_history = 4; /* tunable */
41 static kmem_cache_t *reference_cache;
42 static kmem_cache_t *reference_history_cache;
45 refcount_sysinit(void)
47 reference_cache = kmem_cache_create("reference_cache",
48 sizeof (reference_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
50 reference_history_cache = kmem_cache_create("reference_history_cache",
51 sizeof (uint64_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
57 kmem_cache_destroy(reference_cache);
58 kmem_cache_destroy(reference_history_cache);
62 refcount_create(refcount_t *rc)
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));
68 mutex_init(&rc->rc_mtx, NULL, MUTEX_DEFAULT, NULL);
72 refcount_destroy_many(refcount_t *rc, uint64_t number)
76 ASSERT(rc->rc_count == number);
77 while (ref = list_head(&rc->rc_list)) {
78 list_remove(&rc->rc_list, ref);
79 kmem_cache_free(reference_cache, ref);
81 list_destroy(&rc->rc_list);
83 while (ref = list_head(&rc->rc_removed)) {
84 list_remove(&rc->rc_removed, ref);
85 kmem_cache_free(reference_history_cache, ref->ref_removed);
86 kmem_cache_free(reference_cache, ref);
88 list_destroy(&rc->rc_removed);
89 mutex_destroy(&rc->rc_mtx);
93 refcount_destroy(refcount_t *rc)
95 refcount_destroy_many(rc, 0);
99 refcount_is_zero(refcount_t *rc)
101 ASSERT(rc->rc_count >= 0);
102 return (rc->rc_count == 0);
106 refcount_count(refcount_t *rc)
108 ASSERT(rc->rc_count >= 0);
109 return (rc->rc_count);
113 refcount_add_many(refcount_t *rc, uint64_t number, void *holder)
118 if (reference_tracking_enable) {
119 ref = kmem_cache_alloc(reference_cache, KM_SLEEP);
120 ref->ref_holder = holder;
121 ref->ref_number = number;
123 mutex_enter(&rc->rc_mtx);
124 ASSERT(rc->rc_count >= 0);
125 if (reference_tracking_enable)
126 list_insert_head(&rc->rc_list, ref);
127 rc->rc_count += number;
128 count = rc->rc_count;
129 mutex_exit(&rc->rc_mtx);
135 refcount_add(refcount_t *rc, void *holder)
137 return (refcount_add_many(rc, 1, holder));
141 refcount_remove_many(refcount_t *rc, uint64_t number, void *holder)
146 mutex_enter(&rc->rc_mtx);
147 ASSERT(rc->rc_count >= number);
149 if (!reference_tracking_enable) {
150 rc->rc_count -= number;
151 count = rc->rc_count;
152 mutex_exit(&rc->rc_mtx);
156 for (ref = list_head(&rc->rc_list); ref;
157 ref = list_next(&rc->rc_list, ref)) {
158 if (ref->ref_holder == holder && ref->ref_number == number) {
159 list_remove(&rc->rc_list, ref);
160 if (reference_history > 0) {
162 kmem_cache_alloc(reference_history_cache,
164 list_insert_head(&rc->rc_removed, ref);
165 rc->rc_removed_count++;
166 if (rc->rc_removed_count >= reference_history) {
167 ref = list_tail(&rc->rc_removed);
168 list_remove(&rc->rc_removed, ref);
169 kmem_cache_free(reference_history_cache,
171 kmem_cache_free(reference_cache, ref);
172 rc->rc_removed_count--;
175 kmem_cache_free(reference_cache, ref);
177 rc->rc_count -= number;
178 count = rc->rc_count;
179 mutex_exit(&rc->rc_mtx);
183 panic("No such hold %p on refcount %llx", holder,
184 (u_longlong_t)(uintptr_t)rc);
189 refcount_remove(refcount_t *rc, void *holder)
191 return (refcount_remove_many(rc, 1, holder));