1949 crash during reguid causes stale config
[unleashed.git] / usr / src / uts / common / fs / zfs / refcount.c
blob600132f080e70e70586e1e00d6683257d7cf53ae
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 #include <sys/zfs_context.h>
26 #include <sys/refcount.h>
28 #ifdef ZFS_DEBUG
30 #ifdef _KERNEL
31 int reference_tracking_enable = FALSE; /* runs out of memory too easily */
32 #else
33 int reference_tracking_enable = TRUE;
34 #endif
35 int reference_history = 4; /* tunable */
37 static kmem_cache_t *reference_cache;
38 static kmem_cache_t *reference_history_cache;
40 void
41 refcount_init(void)
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);
50 void
51 refcount_fini(void)
53 kmem_cache_destroy(reference_cache);
54 kmem_cache_destroy(reference_history_cache);
57 void
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));
65 rc->rc_count = 0;
66 rc->rc_removed_count = 0;
69 void
70 refcount_destroy_many(refcount_t *rc, uint64_t number)
72 reference_t *ref;
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);
90 void
91 refcount_destroy(refcount_t *rc)
93 refcount_destroy_many(rc, 0);
96 int
97 refcount_is_zero(refcount_t *rc)
99 ASSERT(rc->rc_count >= 0);
100 return (rc->rc_count == 0);
103 int64_t
104 refcount_count(refcount_t *rc)
106 ASSERT(rc->rc_count >= 0);
107 return (rc->rc_count);
110 int64_t
111 refcount_add_many(refcount_t *rc, uint64_t number, void *holder)
113 reference_t *ref;
114 int64_t count;
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);
129 return (count);
132 int64_t
133 refcount_add(refcount_t *rc, void *holder)
135 return (refcount_add_many(rc, 1, holder));
138 int64_t
139 refcount_remove_many(refcount_t *rc, uint64_t number, void *holder)
141 reference_t *ref;
142 int64_t count;
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);
151 return (count);
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) {
159 ref->ref_removed =
160 kmem_cache_alloc(reference_history_cache,
161 KM_SLEEP);
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,
168 ref->ref_removed);
169 kmem_cache_free(reference_cache, ref);
170 rc->rc_removed_count--;
172 } else {
173 kmem_cache_free(reference_cache, ref);
175 rc->rc_count -= number;
176 count = rc->rc_count;
177 mutex_exit(&rc->rc_mtx);
178 return (count);
181 panic("No such hold %p on refcount %llx", holder,
182 (u_longlong_t)(uintptr_t)rc);
183 return (-1);
186 int64_t
187 refcount_remove(refcount_t *rc, void *holder)
189 return (refcount_remove_many(rc, 1, holder));
192 void
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;
206 src->rc_count = 0;
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);
219 list_destroy(&list);
220 list_destroy(&removed);
223 #endif /* ZFS_DEBUG */