1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24 */
25
26 #include <sys/zfs_context.h>
27 #include <sys/refcount.h>
28
29 #ifdef ZFS_DEBUG
30
31 #ifdef _KERNEL
32 int reference_tracking_enable = FALSE; /* runs out of memory too easily */
33 #else
34 int reference_tracking_enable = TRUE;
35 #endif
36 int reference_history = 3; /* tunable */
37
38 static kmem_cache_t *reference_cache;
39 static kmem_cache_t *reference_history_cache;
40
41 void
zfs_refcount_init(void)42 zfs_refcount_init(void)
43 {
44 reference_cache = kmem_cache_create("reference_cache",
45 sizeof (reference_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
46
47 reference_history_cache = kmem_cache_create("reference_history_cache",
48 sizeof (uint64_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
49 }
50
51 void
zfs_refcount_fini(void)52 zfs_refcount_fini(void)
53 {
54 kmem_cache_destroy(reference_cache);
55 kmem_cache_destroy(reference_history_cache);
56 }
57
58 void
zfs_refcount_create(zfs_refcount_t * rc)59 zfs_refcount_create(zfs_refcount_t *rc)
60 {
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));
66 rc->rc_count = 0;
67 rc->rc_removed_count = 0;
68 rc->rc_tracked = reference_tracking_enable;
69 }
70
71 void
zfs_refcount_create_tracked(zfs_refcount_t * rc)72 zfs_refcount_create_tracked(zfs_refcount_t *rc)
73 {
74 zfs_refcount_create(rc);
75 rc->rc_tracked = B_TRUE;
76 }
77
78 void
zfs_refcount_create_untracked(zfs_refcount_t * rc)79 zfs_refcount_create_untracked(zfs_refcount_t *rc)
80 {
81 zfs_refcount_create(rc);
82 rc->rc_tracked = B_FALSE;
83 }
84
85 void
zfs_refcount_destroy_many(zfs_refcount_t * rc,uint64_t number)86 zfs_refcount_destroy_many(zfs_refcount_t *rc, uint64_t number)
87 {
88 reference_t *ref;
89
90 ASSERT(rc->rc_count == number);
91 while (ref = list_head(&rc->rc_list)) {
92 list_remove(&rc->rc_list, ref);
93 kmem_cache_free(reference_cache, ref);
94 }
95 list_destroy(&rc->rc_list);
96
97 while (ref = list_head(&rc->rc_removed)) {
98 list_remove(&rc->rc_removed, ref);
99 kmem_cache_free(reference_history_cache, ref->ref_removed);
100 kmem_cache_free(reference_cache, ref);
101 }
102 list_destroy(&rc->rc_removed);
103 mutex_destroy(&rc->rc_mtx);
104 }
105
106 void
zfs_refcount_destroy(zfs_refcount_t * rc)107 zfs_refcount_destroy(zfs_refcount_t *rc)
108 {
109 zfs_refcount_destroy_many(rc, 0);
110 }
111
112 int
zfs_refcount_is_zero(zfs_refcount_t * rc)113 zfs_refcount_is_zero(zfs_refcount_t *rc)
114 {
115 return (rc->rc_count == 0);
116 }
117
118 int64_t
zfs_refcount_count(zfs_refcount_t * rc)119 zfs_refcount_count(zfs_refcount_t *rc)
120 {
121 return (rc->rc_count);
122 }
123
124 int64_t
zfs_refcount_add_many(zfs_refcount_t * rc,uint64_t number,const void * holder)125 zfs_refcount_add_many(zfs_refcount_t *rc, uint64_t number, const void *holder)
126 {
127 reference_t *ref = NULL;
128 int64_t count;
129
130 if (rc->rc_tracked) {
131 ref = kmem_cache_alloc(reference_cache, KM_SLEEP);
132 ref->ref_holder = holder;
133 ref->ref_number = number;
134 }
135 mutex_enter(&rc->rc_mtx);
136 ASSERT(rc->rc_count >= 0);
137 if (rc->rc_tracked)
138 list_insert_head(&rc->rc_list, ref);
139 rc->rc_count += number;
140 count = rc->rc_count;
141 mutex_exit(&rc->rc_mtx);
142
143 return (count);
144 }
145
146 int64_t
zfs_refcount_add(zfs_refcount_t * rc,const void * holder)147 zfs_refcount_add(zfs_refcount_t *rc, const void *holder)
148 {
149 return (zfs_refcount_add_many(rc, 1, holder));
150 }
151
152 void
zfs_refcount_add_few(zfs_refcount_t * rc,uint64_t number,const void * holder)153 zfs_refcount_add_few(zfs_refcount_t *rc, uint64_t number, const void *holder)
154 {
155 if (!rc->rc_tracked) {
156 (void) zfs_refcount_add_many(rc, number, holder);
157 } else {
158 for (; number > 0; number--)
159 (void) zfs_refcount_add(rc, holder);
160 }
161 }
162
163 int64_t
zfs_refcount_remove_many(zfs_refcount_t * rc,uint64_t number,const void * holder)164 zfs_refcount_remove_many(zfs_refcount_t *rc, uint64_t number,
165 const void *holder)
166 {
167 reference_t *ref;
168 int64_t count;
169
170 mutex_enter(&rc->rc_mtx);
171 ASSERT(rc->rc_count >= number);
172
173 if (!rc->rc_tracked) {
174 rc->rc_count -= number;
175 count = rc->rc_count;
176 mutex_exit(&rc->rc_mtx);
177 return (count);
178 }
179
180 for (ref = list_head(&rc->rc_list); ref;
181 ref = list_next(&rc->rc_list, ref)) {
182 if (ref->ref_holder == holder && ref->ref_number == number) {
183 list_remove(&rc->rc_list, ref);
184 if (reference_history > 0) {
185 ref->ref_removed =
186 kmem_cache_alloc(reference_history_cache,
187 KM_SLEEP);
188 list_insert_head(&rc->rc_removed, ref);
189 rc->rc_removed_count++;
190 if (rc->rc_removed_count > reference_history) {
191 ref = list_tail(&rc->rc_removed);
192 list_remove(&rc->rc_removed, ref);
193 kmem_cache_free(reference_history_cache,
194 ref->ref_removed);
195 kmem_cache_free(reference_cache, ref);
196 rc->rc_removed_count--;
197 }
198 } else {
199 kmem_cache_free(reference_cache, ref);
200 }
201 rc->rc_count -= number;
202 count = rc->rc_count;
203 mutex_exit(&rc->rc_mtx);
204 return (count);
205 }
206 }
207 panic("No such hold %p with count %" PRIu64 " on refcount %llx",
208 holder, number, (u_longlong_t)(uintptr_t)rc);
209 return (-1);
210 }
211
212 int64_t
zfs_refcount_remove(zfs_refcount_t * rc,const void * holder)213 zfs_refcount_remove(zfs_refcount_t *rc, const void *holder)
214 {
215 return (zfs_refcount_remove_many(rc, 1, holder));
216 }
217
218 void
zfs_refcount_remove_few(zfs_refcount_t * rc,uint64_t number,const void * holder)219 zfs_refcount_remove_few(zfs_refcount_t *rc, uint64_t number, const void *holder)
220 {
221 if (!rc->rc_tracked) {
222 (void) zfs_refcount_remove_many(rc, number, holder);
223 } else {
224 for (; number > 0; number--)
225 (void) zfs_refcount_remove(rc, holder);
226 }
227 }
228
229 void
zfs_refcount_transfer(zfs_refcount_t * dst,zfs_refcount_t * src)230 zfs_refcount_transfer(zfs_refcount_t *dst, zfs_refcount_t *src)
231 {
232 int64_t count, removed_count;
233 list_t list, removed;
234
235 list_create(&list, sizeof (reference_t),
236 offsetof(reference_t, ref_link));
237 list_create(&removed, sizeof (reference_t),
238 offsetof(reference_t, ref_link));
239
240 mutex_enter(&src->rc_mtx);
241 count = src->rc_count;
242 removed_count = src->rc_removed_count;
243 src->rc_count = 0;
244 src->rc_removed_count = 0;
245 list_move_tail(&list, &src->rc_list);
246 list_move_tail(&removed, &src->rc_removed);
247 mutex_exit(&src->rc_mtx);
248
249 mutex_enter(&dst->rc_mtx);
250 dst->rc_count += count;
251 dst->rc_removed_count += removed_count;
252 list_move_tail(&dst->rc_list, &list);
253 list_move_tail(&dst->rc_removed, &removed);
254 mutex_exit(&dst->rc_mtx);
255
256 list_destroy(&list);
257 list_destroy(&removed);
258 }
259
260 /* ARGSUSED */
261 void
zfs_refcount_transfer_ownership_many(zfs_refcount_t * rc,uint64_t number,void * current_holder,void * new_holder)262 zfs_refcount_transfer_ownership_many(zfs_refcount_t *rc, uint64_t number,
263 void *current_holder, void *new_holder)
264 {
265 reference_t *ref;
266 boolean_t found = B_FALSE;
267
268 mutex_enter(&rc->rc_mtx);
269 if (!rc->rc_tracked) {
270 mutex_exit(&rc->rc_mtx);
271 return;
272 }
273
274 for (ref = list_head(&rc->rc_list); ref;
275 ref = list_next(&rc->rc_list, ref)) {
276 if (ref->ref_holder == current_holder &&
277 ref->ref_number == number) {
278 ref->ref_holder = new_holder;
279 found = B_TRUE;
280 break;
281 }
282 }
283 ASSERT(found);
284 mutex_exit(&rc->rc_mtx);
285 }
286
287 void
zfs_refcount_transfer_ownership(zfs_refcount_t * rc,void * current_holder,void * new_holder)288 zfs_refcount_transfer_ownership(zfs_refcount_t *rc, void *current_holder,
289 void *new_holder)
290 {
291 zfs_refcount_transfer_ownership_many(rc, 1, current_holder,
292 new_holder);
293 }
294
295 /*
296 * If tracking is enabled, return true if a reference exists that matches
297 * the "holder" tag. If tracking is disabled, then return true if a reference
298 * might be held.
299 */
300 boolean_t
zfs_refcount_held(zfs_refcount_t * rc,void * holder)301 zfs_refcount_held(zfs_refcount_t *rc, void *holder)
302 {
303 reference_t *ref;
304
305 mutex_enter(&rc->rc_mtx);
306
307 if (!rc->rc_tracked) {
308 mutex_exit(&rc->rc_mtx);
309 return (rc->rc_count > 0);
310 }
311
312 for (ref = list_head(&rc->rc_list); ref;
313 ref = list_next(&rc->rc_list, ref)) {
314 if (ref->ref_holder == holder) {
315 mutex_exit(&rc->rc_mtx);
316 return (B_TRUE);
317 }
318 }
319 mutex_exit(&rc->rc_mtx);
320 return (B_FALSE);
321 }
322
323 /*
324 * If tracking is enabled, return true if a reference does not exist that
325 * matches the "holder" tag. If tracking is disabled, always return true
326 * since the reference might not be held.
327 */
328 boolean_t
zfs_refcount_not_held(zfs_refcount_t * rc,void * holder)329 zfs_refcount_not_held(zfs_refcount_t *rc, void *holder)
330 {
331 reference_t *ref;
332
333 mutex_enter(&rc->rc_mtx);
334
335 if (!rc->rc_tracked) {
336 mutex_exit(&rc->rc_mtx);
337 return (B_TRUE);
338 }
339
340 for (ref = list_head(&rc->rc_list); ref;
341 ref = list_next(&rc->rc_list, ref)) {
342 if (ref->ref_holder == holder) {
343 mutex_exit(&rc->rc_mtx);
344 return (B_FALSE);
345 }
346 }
347 mutex_exit(&rc->rc_mtx);
348 return (B_TRUE);
349 }
350 #endif /* ZFS_DEBUG */
351