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 */
24
25#include <sys/zfs_context.h>
26#include <sys/refcount.h>
27
28#ifdef	ZFS_DEBUG
29
30#ifdef _KERNEL
31int reference_tracking_enable = FALSE; /* runs out of memory too easily */
32#else
33int reference_tracking_enable = TRUE;
34#endif
35int reference_history = 4; /* tunable */
36
37static kmem_cache_t *reference_cache;
38static kmem_cache_t *reference_history_cache;
39
40void
41refcount_init(void)
42{
43	reference_cache = kmem_cache_create("reference_cache",
44	    sizeof (reference_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
45
46	reference_history_cache = kmem_cache_create("reference_history_cache",
47	    sizeof (uint64_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
48}
49
50void
51refcount_fini(void)
52{
53	kmem_cache_destroy(reference_cache);
54	kmem_cache_destroy(reference_history_cache);
55}
56
57void
58refcount_create(refcount_t *rc)
59{
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;
67}
68
69void
70refcount_destroy_many(refcount_t *rc, uint64_t number)
71{
72	reference_t *ref;
73
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);
78	}
79	list_destroy(&rc->rc_list);
80
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);
85	}
86	list_destroy(&rc->rc_removed);
87	mutex_destroy(&rc->rc_mtx);
88}
89
90void
91refcount_destroy(refcount_t *rc)
92{
93	refcount_destroy_many(rc, 0);
94}
95
96int
97refcount_is_zero(refcount_t *rc)
98{
99	ASSERT(rc->rc_count >= 0);
100	return (rc->rc_count == 0);
101}
102
103int64_t
104refcount_count(refcount_t *rc)
105{
106	ASSERT(rc->rc_count >= 0);
107	return (rc->rc_count);
108}
109
110int64_t
111refcount_add_many(refcount_t *rc, uint64_t number, void *holder)
112{
113	reference_t *ref;
114	int64_t count;
115
116	if (reference_tracking_enable) {
117		ref = kmem_cache_alloc(reference_cache, KM_SLEEP);
118		ref->ref_holder = holder;
119		ref->ref_number = number;
120	}
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);
128
129	return (count);
130}
131
132int64_t
133refcount_add(refcount_t *rc, void *holder)
134{
135	return (refcount_add_many(rc, 1, holder));
136}
137
138int64_t
139refcount_remove_many(refcount_t *rc, uint64_t number, void *holder)
140{
141	reference_t *ref;
142	int64_t count;
143
144	mutex_enter(&rc->rc_mtx);
145	ASSERT(rc->rc_count >= number);
146
147	if (!reference_tracking_enable) {
148		rc->rc_count -= number;
149		count = rc->rc_count;
150		mutex_exit(&rc->rc_mtx);
151		return (count);
152	}
153
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--;
171				}
172			} else {
173				kmem_cache_free(reference_cache, ref);
174			}
175			rc->rc_count -= number;
176			count = rc->rc_count;
177			mutex_exit(&rc->rc_mtx);
178			return (count);
179		}
180	}
181	panic("No such hold %p on refcount %llx", holder,
182	    (u_longlong_t)(uintptr_t)rc);
183	return (-1);
184}
185
186int64_t
187refcount_remove(refcount_t *rc, void *holder)
188{
189	return (refcount_remove_many(rc, 1, holder));
190}
191
192void
193refcount_transfer(refcount_t *dst, refcount_t *src)
194{
195	int64_t count, removed_count;
196	list_t list, removed;
197
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));
202
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);
211
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);
218
219	list_destroy(&list);
220	list_destroy(&removed);
221}
222
223#endif	/* ZFS_DEBUG */
224