1168404Spjd/*
2168404Spjd * CDDL HEADER START
3168404Spjd *
4168404Spjd * The contents of this file are subject to the terms of the
5185029Spjd * Common Development and Distribution License (the "License").
6185029Spjd * You may not use this file except in compliance with the License.
7168404Spjd *
8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9168404Spjd * or http://www.opensolaris.org/os/licensing.
10168404Spjd * See the License for the specific language governing permissions
11168404Spjd * and limitations under the License.
12168404Spjd *
13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each
14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15168404Spjd * If applicable, add the following below this CDDL HEADER, with the
16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18168404Spjd *
19168404Spjd * CDDL HEADER END
20168404Spjd */
21168404Spjd/*
22219089Spjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23307277Smav * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24168404Spjd */
25168404Spjd
26168404Spjd#include <sys/zfs_context.h>
27168404Spjd#include <sys/refcount.h>
28168404Spjd
29219089Spjd#ifdef	ZFS_DEBUG
30168404Spjd
31168404Spjd#ifdef _KERNEL
32168404Spjdint reference_tracking_enable = FALSE; /* runs out of memory too easily */
33277492SwillSYSCTL_DECL(_vfs_zfs);
34277492SwillSYSCTL_INT(_vfs_zfs, OID_AUTO, reference_tracking_enable, CTLFLAG_RDTUN,
35277492Swill    &reference_tracking_enable, 0,
36277492Swill    "Track reference holders to refcount_t objects, used mostly by ZFS");
37168404Spjd#else
38168404Spjdint reference_tracking_enable = TRUE;
39168404Spjd#endif
40248571Smmint reference_history = 3; /* tunable */
41168404Spjd
42168404Spjdstatic kmem_cache_t *reference_cache;
43168404Spjdstatic kmem_cache_t *reference_history_cache;
44168404Spjd
45168404Spjdvoid
46179310Spjdrefcount_sysinit(void)
47168404Spjd{
48168404Spjd	reference_cache = kmem_cache_create("reference_cache",
49168404Spjd	    sizeof (reference_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
50168404Spjd
51168404Spjd	reference_history_cache = kmem_cache_create("reference_history_cache",
52168404Spjd	    sizeof (uint64_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
53168404Spjd}
54168404Spjd
55168404Spjdvoid
56168404Spjdrefcount_fini(void)
57168404Spjd{
58168404Spjd	kmem_cache_destroy(reference_cache);
59168404Spjd	kmem_cache_destroy(reference_history_cache);
60168404Spjd}
61168404Spjd
62168404Spjdvoid
63168404Spjdrefcount_create(refcount_t *rc)
64168404Spjd{
65185029Spjd	mutex_init(&rc->rc_mtx, NULL, MUTEX_DEFAULT, NULL);
66168404Spjd	list_create(&rc->rc_list, sizeof (reference_t),
67168404Spjd	    offsetof(reference_t, ref_link));
68168404Spjd	list_create(&rc->rc_removed, sizeof (reference_t),
69168404Spjd	    offsetof(reference_t, ref_link));
70185029Spjd	rc->rc_count = 0;
71185029Spjd	rc->rc_removed_count = 0;
72248571Smm	rc->rc_tracked = reference_tracking_enable;
73168404Spjd}
74168404Spjd
75168404Spjdvoid
76307277Smavrefcount_create_tracked(refcount_t *rc)
77307277Smav{
78307277Smav	refcount_create(rc);
79307277Smav	rc->rc_tracked = B_TRUE;
80307277Smav}
81307277Smav
82307277Smavvoid
83248571Smmrefcount_create_untracked(refcount_t *rc)
84248571Smm{
85248571Smm	refcount_create(rc);
86248571Smm	rc->rc_tracked = B_FALSE;
87248571Smm}
88248571Smm
89248571Smmvoid
90168404Spjdrefcount_destroy_many(refcount_t *rc, uint64_t number)
91168404Spjd{
92168404Spjd	reference_t *ref;
93168404Spjd
94168404Spjd	ASSERT(rc->rc_count == number);
95168404Spjd	while (ref = list_head(&rc->rc_list)) {
96168404Spjd		list_remove(&rc->rc_list, ref);
97168404Spjd		kmem_cache_free(reference_cache, ref);
98168404Spjd	}
99168404Spjd	list_destroy(&rc->rc_list);
100168404Spjd
101168404Spjd	while (ref = list_head(&rc->rc_removed)) {
102168404Spjd		list_remove(&rc->rc_removed, ref);
103168404Spjd		kmem_cache_free(reference_history_cache, ref->ref_removed);
104168404Spjd		kmem_cache_free(reference_cache, ref);
105168404Spjd	}
106168404Spjd	list_destroy(&rc->rc_removed);
107168404Spjd	mutex_destroy(&rc->rc_mtx);
108168404Spjd}
109168404Spjd
110168404Spjdvoid
111168404Spjdrefcount_destroy(refcount_t *rc)
112168404Spjd{
113168404Spjd	refcount_destroy_many(rc, 0);
114168404Spjd}
115168404Spjd
116168404Spjdint
117168404Spjdrefcount_is_zero(refcount_t *rc)
118168404Spjd{
119168404Spjd	return (rc->rc_count == 0);
120168404Spjd}
121168404Spjd
122168404Spjdint64_t
123168404Spjdrefcount_count(refcount_t *rc)
124168404Spjd{
125168404Spjd	return (rc->rc_count);
126168404Spjd}
127168404Spjd
128168404Spjdint64_t
129168404Spjdrefcount_add_many(refcount_t *rc, uint64_t number, void *holder)
130168404Spjd{
131247187Smm	reference_t *ref = NULL;
132168404Spjd	int64_t count;
133168404Spjd
134248571Smm	if (rc->rc_tracked) {
135168404Spjd		ref = kmem_cache_alloc(reference_cache, KM_SLEEP);
136168404Spjd		ref->ref_holder = holder;
137168404Spjd		ref->ref_number = number;
138168404Spjd	}
139168404Spjd	mutex_enter(&rc->rc_mtx);
140168404Spjd	ASSERT(rc->rc_count >= 0);
141248571Smm	if (rc->rc_tracked)
142168404Spjd		list_insert_head(&rc->rc_list, ref);
143168404Spjd	rc->rc_count += number;
144168404Spjd	count = rc->rc_count;
145168404Spjd	mutex_exit(&rc->rc_mtx);
146168404Spjd
147168404Spjd	return (count);
148168404Spjd}
149168404Spjd
150168404Spjdint64_t
151168404Spjdrefcount_add(refcount_t *rc, void *holder)
152168404Spjd{
153168404Spjd	return (refcount_add_many(rc, 1, holder));
154168404Spjd}
155168404Spjd
156168404Spjdint64_t
157168404Spjdrefcount_remove_many(refcount_t *rc, uint64_t number, void *holder)
158168404Spjd{
159168404Spjd	reference_t *ref;
160168404Spjd	int64_t count;
161168404Spjd
162168404Spjd	mutex_enter(&rc->rc_mtx);
163168404Spjd	ASSERT(rc->rc_count >= number);
164168404Spjd
165248571Smm	if (!rc->rc_tracked) {
166168404Spjd		rc->rc_count -= number;
167168404Spjd		count = rc->rc_count;
168168404Spjd		mutex_exit(&rc->rc_mtx);
169168404Spjd		return (count);
170168404Spjd	}
171168404Spjd
172168404Spjd	for (ref = list_head(&rc->rc_list); ref;
173168404Spjd	    ref = list_next(&rc->rc_list, ref)) {
174168404Spjd		if (ref->ref_holder == holder && ref->ref_number == number) {
175168404Spjd			list_remove(&rc->rc_list, ref);
176168404Spjd			if (reference_history > 0) {
177168404Spjd				ref->ref_removed =
178168404Spjd				    kmem_cache_alloc(reference_history_cache,
179168404Spjd				    KM_SLEEP);
180168404Spjd				list_insert_head(&rc->rc_removed, ref);
181168404Spjd				rc->rc_removed_count++;
182248571Smm				if (rc->rc_removed_count > reference_history) {
183168404Spjd					ref = list_tail(&rc->rc_removed);
184168404Spjd					list_remove(&rc->rc_removed, ref);
185168404Spjd					kmem_cache_free(reference_history_cache,
186168404Spjd					    ref->ref_removed);
187168404Spjd					kmem_cache_free(reference_cache, ref);
188168404Spjd					rc->rc_removed_count--;
189168404Spjd				}
190168404Spjd			} else {
191168404Spjd				kmem_cache_free(reference_cache, ref);
192168404Spjd			}
193168404Spjd			rc->rc_count -= number;
194168404Spjd			count = rc->rc_count;
195168404Spjd			mutex_exit(&rc->rc_mtx);
196168404Spjd			return (count);
197168404Spjd		}
198168404Spjd	}
199168404Spjd	panic("No such hold %p on refcount %llx", holder,
200168404Spjd	    (u_longlong_t)(uintptr_t)rc);
201168404Spjd	return (-1);
202168404Spjd}
203168404Spjd
204168404Spjdint64_t
205168404Spjdrefcount_remove(refcount_t *rc, void *holder)
206168404Spjd{
207168404Spjd	return (refcount_remove_many(rc, 1, holder));
208168404Spjd}
209168404Spjd
210219089Spjdvoid
211219089Spjdrefcount_transfer(refcount_t *dst, refcount_t *src)
212219089Spjd{
213219089Spjd	int64_t count, removed_count;
214219089Spjd	list_t list, removed;
215219089Spjd
216219089Spjd	list_create(&list, sizeof (reference_t),
217219089Spjd	    offsetof(reference_t, ref_link));
218219089Spjd	list_create(&removed, sizeof (reference_t),
219219089Spjd	    offsetof(reference_t, ref_link));
220219089Spjd
221219089Spjd	mutex_enter(&src->rc_mtx);
222219089Spjd	count = src->rc_count;
223219089Spjd	removed_count = src->rc_removed_count;
224219089Spjd	src->rc_count = 0;
225219089Spjd	src->rc_removed_count = 0;
226219089Spjd	list_move_tail(&list, &src->rc_list);
227219089Spjd	list_move_tail(&removed, &src->rc_removed);
228219089Spjd	mutex_exit(&src->rc_mtx);
229219089Spjd
230219089Spjd	mutex_enter(&dst->rc_mtx);
231219089Spjd	dst->rc_count += count;
232219089Spjd	dst->rc_removed_count += removed_count;
233219089Spjd	list_move_tail(&dst->rc_list, &list);
234219089Spjd	list_move_tail(&dst->rc_removed, &removed);
235219089Spjd	mutex_exit(&dst->rc_mtx);
236219089Spjd
237219089Spjd	list_destroy(&list);
238219089Spjd	list_destroy(&removed);
239219089Spjd}
240219089Spjd
241307265Smavvoid
242307265Smavrefcount_transfer_ownership(refcount_t *rc, void *current_holder,
243307265Smav    void *new_holder)
244307265Smav{
245307265Smav	reference_t *ref;
246307265Smav	boolean_t found = B_FALSE;
247307265Smav
248307265Smav	mutex_enter(&rc->rc_mtx);
249307265Smav	if (!rc->rc_tracked) {
250307265Smav		mutex_exit(&rc->rc_mtx);
251307265Smav		return;
252307265Smav	}
253307265Smav
254307265Smav	for (ref = list_head(&rc->rc_list); ref;
255307265Smav	    ref = list_next(&rc->rc_list, ref)) {
256307265Smav		if (ref->ref_holder == current_holder) {
257307265Smav			ref->ref_holder = new_holder;
258307265Smav			found = B_TRUE;
259307265Smav			break;
260307265Smav		}
261307265Smav	}
262307265Smav	ASSERT(found);
263307265Smav	mutex_exit(&rc->rc_mtx);
264307265Smav}
265307277Smav
266307277Smav/*
267307277Smav * If tracking is enabled, return true if a reference exists that matches
268307277Smav * the "holder" tag. If tracking is disabled, then return true if a reference
269307277Smav * might be held.
270307277Smav */
271307277Smavboolean_t
272307277Smavrefcount_held(refcount_t *rc, void *holder)
273307277Smav{
274307277Smav	reference_t *ref;
275307277Smav
276307277Smav	mutex_enter(&rc->rc_mtx);
277307277Smav
278307277Smav	if (!rc->rc_tracked) {
279307277Smav		mutex_exit(&rc->rc_mtx);
280307277Smav		return (rc->rc_count > 0);
281307277Smav	}
282307277Smav
283307277Smav	for (ref = list_head(&rc->rc_list); ref;
284307277Smav	    ref = list_next(&rc->rc_list, ref)) {
285307277Smav		if (ref->ref_holder == holder) {
286307277Smav			mutex_exit(&rc->rc_mtx);
287307277Smav			return (B_TRUE);
288307277Smav		}
289307277Smav	}
290307277Smav	mutex_exit(&rc->rc_mtx);
291307277Smav	return (B_FALSE);
292307277Smav}
293307277Smav
294307277Smav/*
295307277Smav * If tracking is enabled, return true if a reference does not exist that
296307277Smav * matches the "holder" tag. If tracking is disabled, always return true
297307277Smav * since the reference might not be held.
298307277Smav */
299307277Smavboolean_t
300307277Smavrefcount_not_held(refcount_t *rc, void *holder)
301307277Smav{
302307277Smav	reference_t *ref;
303307277Smav
304307277Smav	mutex_enter(&rc->rc_mtx);
305307277Smav
306307277Smav	if (!rc->rc_tracked) {
307307277Smav		mutex_exit(&rc->rc_mtx);
308307277Smav		return (B_TRUE);
309307277Smav	}
310307277Smav
311307277Smav	for (ref = list_head(&rc->rc_list); ref;
312307277Smav	    ref = list_next(&rc->rc_list, ref)) {
313307277Smav		if (ref->ref_holder == holder) {
314307277Smav			mutex_exit(&rc->rc_mtx);
315307277Smav			return (B_FALSE);
316307277Smav		}
317307277Smav	}
318307277Smav	mutex_exit(&rc->rc_mtx);
319307277Smav	return (B_TRUE);
320307277Smav}
321219089Spjd#endif	/* ZFS_DEBUG */
322