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, 2021 by Delphix. All rights reserved.
24 */
25
26#include <sys/zfs_context.h>
27#include <sys/zfs_refcount.h>
28
29/*
30 * Reference count tracking is disabled by default.  It's memory requirements
31 * are reasonable, however as implemented it consumes a significant amount of
32 * cpu time.  Until its performance is improved it should be manually enabled.
33 */
34int reference_tracking_enable = FALSE;
35int reference_history = 3; /* tunable */
36
37#ifdef	ZFS_DEBUG
38static kmem_cache_t *reference_cache;
39static kmem_cache_t *reference_history_cache;
40
41void
42zfs_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
51void
52zfs_refcount_fini(void)
53{
54	kmem_cache_destroy(reference_cache);
55	kmem_cache_destroy(reference_history_cache);
56}
57
58void
59zfs_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
71void
72zfs_refcount_create_tracked(zfs_refcount_t *rc)
73{
74	zfs_refcount_create(rc);
75	rc->rc_tracked = B_TRUE;
76}
77
78void
79zfs_refcount_create_untracked(zfs_refcount_t *rc)
80{
81	zfs_refcount_create(rc);
82	rc->rc_tracked = B_FALSE;
83}
84
85void
86zfs_refcount_destroy_many(zfs_refcount_t *rc, uint64_t number)
87{
88	reference_t *ref;
89
90	ASSERT3U(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
106void
107zfs_refcount_destroy(zfs_refcount_t *rc)
108{
109	zfs_refcount_destroy_many(rc, 0);
110}
111
112int
113zfs_refcount_is_zero(zfs_refcount_t *rc)
114{
115	return (rc->rc_count == 0);
116}
117
118int64_t
119zfs_refcount_count(zfs_refcount_t *rc)
120{
121	return (rc->rc_count);
122}
123
124int64_t
125zfs_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	ASSERT3U(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
146int64_t
147zfs_refcount_add(zfs_refcount_t *rc, const void *holder)
148{
149	return (zfs_refcount_add_many(rc, 1, holder));
150}
151
152int64_t
153zfs_refcount_remove_many(zfs_refcount_t *rc, uint64_t number,
154    const void *holder)
155{
156	reference_t *ref;
157	int64_t count;
158
159	mutex_enter(&rc->rc_mtx);
160	ASSERT3U(rc->rc_count, >=, number);
161
162	if (!rc->rc_tracked) {
163		rc->rc_count -= number;
164		count = rc->rc_count;
165		mutex_exit(&rc->rc_mtx);
166		return (count);
167	}
168
169	for (ref = list_head(&rc->rc_list); ref;
170	    ref = list_next(&rc->rc_list, ref)) {
171		if (ref->ref_holder == holder && ref->ref_number == number) {
172			list_remove(&rc->rc_list, ref);
173			if (reference_history > 0) {
174				ref->ref_removed =
175				    kmem_cache_alloc(reference_history_cache,
176				    KM_SLEEP);
177				list_insert_head(&rc->rc_removed, ref);
178				rc->rc_removed_count++;
179				if (rc->rc_removed_count > reference_history) {
180					ref = list_tail(&rc->rc_removed);
181					list_remove(&rc->rc_removed, ref);
182					kmem_cache_free(reference_history_cache,
183					    ref->ref_removed);
184					kmem_cache_free(reference_cache, ref);
185					rc->rc_removed_count--;
186				}
187			} else {
188				kmem_cache_free(reference_cache, ref);
189			}
190			rc->rc_count -= number;
191			count = rc->rc_count;
192			mutex_exit(&rc->rc_mtx);
193			return (count);
194		}
195	}
196	panic("No such hold %p on refcount %llx", holder,
197	    (u_longlong_t)(uintptr_t)rc);
198	return (-1);
199}
200
201int64_t
202zfs_refcount_remove(zfs_refcount_t *rc, const void *holder)
203{
204	return (zfs_refcount_remove_many(rc, 1, holder));
205}
206
207void
208zfs_refcount_transfer(zfs_refcount_t *dst, zfs_refcount_t *src)
209{
210	int64_t count, removed_count;
211	list_t list, removed;
212
213	list_create(&list, sizeof (reference_t),
214	    offsetof(reference_t, ref_link));
215	list_create(&removed, sizeof (reference_t),
216	    offsetof(reference_t, ref_link));
217
218	mutex_enter(&src->rc_mtx);
219	count = src->rc_count;
220	removed_count = src->rc_removed_count;
221	src->rc_count = 0;
222	src->rc_removed_count = 0;
223	list_move_tail(&list, &src->rc_list);
224	list_move_tail(&removed, &src->rc_removed);
225	mutex_exit(&src->rc_mtx);
226
227	mutex_enter(&dst->rc_mtx);
228	dst->rc_count += count;
229	dst->rc_removed_count += removed_count;
230	list_move_tail(&dst->rc_list, &list);
231	list_move_tail(&dst->rc_removed, &removed);
232	mutex_exit(&dst->rc_mtx);
233
234	list_destroy(&list);
235	list_destroy(&removed);
236}
237
238void
239zfs_refcount_transfer_ownership_many(zfs_refcount_t *rc, uint64_t number,
240    const void *current_holder, const void *new_holder)
241{
242	reference_t *ref;
243	boolean_t found = B_FALSE;
244
245	mutex_enter(&rc->rc_mtx);
246	if (!rc->rc_tracked) {
247		mutex_exit(&rc->rc_mtx);
248		return;
249	}
250
251	for (ref = list_head(&rc->rc_list); ref;
252	    ref = list_next(&rc->rc_list, ref)) {
253		if (ref->ref_holder == current_holder &&
254		    ref->ref_number == number) {
255			ref->ref_holder = new_holder;
256			found = B_TRUE;
257			break;
258		}
259	}
260	ASSERT(found);
261	mutex_exit(&rc->rc_mtx);
262}
263
264void
265zfs_refcount_transfer_ownership(zfs_refcount_t *rc, const void *current_holder,
266    const void *new_holder)
267{
268	return (zfs_refcount_transfer_ownership_many(rc, 1, current_holder,
269	    new_holder));
270}
271
272/*
273 * If tracking is enabled, return true if a reference exists that matches
274 * the "holder" tag. If tracking is disabled, then return true if a reference
275 * might be held.
276 */
277boolean_t
278zfs_refcount_held(zfs_refcount_t *rc, const void *holder)
279{
280	reference_t *ref;
281
282	mutex_enter(&rc->rc_mtx);
283
284	if (!rc->rc_tracked) {
285		mutex_exit(&rc->rc_mtx);
286		return (rc->rc_count > 0);
287	}
288
289	for (ref = list_head(&rc->rc_list); ref;
290	    ref = list_next(&rc->rc_list, ref)) {
291		if (ref->ref_holder == holder) {
292			mutex_exit(&rc->rc_mtx);
293			return (B_TRUE);
294		}
295	}
296	mutex_exit(&rc->rc_mtx);
297	return (B_FALSE);
298}
299
300/*
301 * If tracking is enabled, return true if a reference does not exist that
302 * matches the "holder" tag. If tracking is disabled, always return true
303 * since the reference might not be held.
304 */
305boolean_t
306zfs_refcount_not_held(zfs_refcount_t *rc, const void *holder)
307{
308	reference_t *ref;
309
310	mutex_enter(&rc->rc_mtx);
311
312	if (!rc->rc_tracked) {
313		mutex_exit(&rc->rc_mtx);
314		return (B_TRUE);
315	}
316
317	for (ref = list_head(&rc->rc_list); ref;
318	    ref = list_next(&rc->rc_list, ref)) {
319		if (ref->ref_holder == holder) {
320			mutex_exit(&rc->rc_mtx);
321			return (B_FALSE);
322		}
323	}
324	mutex_exit(&rc->rc_mtx);
325	return (B_TRUE);
326}
327
328/* BEGIN CSTYLED */
329ZFS_MODULE_PARAM(zfs, ,reference_tracking_enable, INT, ZMOD_RW,
330	"Track reference holders to refcount_t objects");
331
332ZFS_MODULE_PARAM(zfs, ,reference_history, INT, ZMOD_RW,
333	"Maximum reference holders being tracked");
334/* END CSTYLED */
335#endif	/* ZFS_DEBUG */
336