1168404Spjd/*
2168404Spjd * CDDL HEADER START
3168404Spjd *
4168404Spjd * The contents of this file are subject to the terms of the
5168404Spjd * Common Development and Distribution License (the "License").
6168404Spjd * 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/*
22168404Spjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23219089Spjd * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24307122Smav */
25297112Smav
26168404Spjd#include <sys/zfs_context.h>
27168404Spjd#include <sys/refcount.h>
28185029Spjd
29168404Spjd#ifdef	ZFS_DEBUG
30168404Spjd
31168404Spjd#ifdef _KERNEL
32185029Spjdint reference_tracking_enable = FALSE; /* runs out of memory too easily */
33185029SpjdSYSCTL_DECL(_vfs_zfs);
34248571SmmTUNABLE_INT("vfs.zfs.reference_tracking_enable", &reference_tracking_enable);
35248571SmmSYSCTL_INT(_vfs_zfs, OID_AUTO, reference_tracking_enable, CTLFLAG_RDTUN,
36185029Spjd    &reference_tracking_enable, 0,
37185029Spjd    "Track reference holders to refcount_t objects, used mostly by ZFS");
38248571Smm#else
39219089Spjdint reference_tracking_enable = TRUE;
40185029Spjd#endif
41185029Spjdint reference_history = 3; /* tunable */
42185029Spjd
43185029Spjdstatic kmem_cache_t *reference_cache;
44168404Spjdstatic kmem_cache_t *reference_history_cache;
45168404Spjd
46168404Spjdvoid
47168404Spjdrefcount_sysinit(void)
48168404Spjd{
49168404Spjd	reference_cache = kmem_cache_create("reference_cache",
50168404Spjd	    sizeof (reference_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
51168404Spjd
52168404Spjd	reference_history_cache = kmem_cache_create("reference_history_cache",
53168404Spjd	    sizeof (uint64_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
54168404Spjd}
55168404Spjd
56168404Spjdvoid
57168404Spjdrefcount_fini(void)
58168404Spjd{
59168404Spjd	kmem_cache_destroy(reference_cache);
60168404Spjd	kmem_cache_destroy(reference_history_cache);
61168404Spjd}
62168404Spjd
63168404Spjdvoid
64168404Spjdrefcount_create(refcount_t *rc)
65168404Spjd{
66168404Spjd	mutex_init(&rc->rc_mtx, NULL, MUTEX_DEFAULT, NULL);
67168404Spjd	list_create(&rc->rc_list, sizeof (reference_t),
68168404Spjd	    offsetof(reference_t, ref_link));
69168404Spjd	list_create(&rc->rc_removed, sizeof (reference_t),
70168404Spjd	    offsetof(reference_t, ref_link));
71168404Spjd	rc->rc_count = 0;
72168404Spjd	rc->rc_removed_count = 0;
73168404Spjd	rc->rc_tracked = reference_tracking_enable;
74168404Spjd}
75168404Spjd
76168404Spjdvoid
77168404Spjdrefcount_create_tracked(refcount_t *rc)
78168404Spjd{
79168404Spjd	refcount_create(rc);
80168404Spjd	rc->rc_tracked = B_TRUE;
81168404Spjd}
82168404Spjd
83168404Spjdvoid
84168404Spjdrefcount_create_untracked(refcount_t *rc)
85168404Spjd{
86168404Spjd	refcount_create(rc);
87168404Spjd	rc->rc_tracked = B_FALSE;
88168404Spjd}
89168404Spjd
90168404Spjdvoid
91168404Spjdrefcount_destroy_many(refcount_t *rc, uint64_t number)
92168404Spjd{
93168404Spjd	reference_t *ref;
94276081Sdelphij
95168404Spjd	ASSERT(rc->rc_count == number);
96168404Spjd	while (ref = list_head(&rc->rc_list)) {
97168404Spjd		list_remove(&rc->rc_list, ref);
98168404Spjd		kmem_cache_free(reference_cache, ref);
99168404Spjd	}
100168404Spjd	list_destroy(&rc->rc_list);
101168404Spjd
102168404Spjd	while (ref = list_head(&rc->rc_removed)) {
103168404Spjd		list_remove(&rc->rc_removed, ref);
104168404Spjd		kmem_cache_free(reference_history_cache, ref->ref_removed);
105168404Spjd		kmem_cache_free(reference_cache, ref);
106168404Spjd	}
107168404Spjd	list_destroy(&rc->rc_removed);
108168404Spjd	mutex_destroy(&rc->rc_mtx);
109228103Smm}
110168404Spjd
111219089Spjdvoid
112228103Smmrefcount_destroy(refcount_t *rc)
113228103Smm{
114168404Spjd	refcount_destroy_many(rc, 0);
115168404Spjd}
116168404Spjd
117168404Spjdint
118168404Spjdrefcount_is_zero(refcount_t *rc)
119168404Spjd{
120168404Spjd	return (rc->rc_count == 0);
121168404Spjd}
122168404Spjd
123168404Spjdint64_t
124168404Spjdrefcount_count(refcount_t *rc)
125168404Spjd{
126168404Spjd	return (rc->rc_count);
127168404Spjd}
128168404Spjd
129168404Spjdint64_t
130168404Spjdrefcount_add_many(refcount_t *rc, uint64_t number, void *holder)
131168404Spjd{
132168404Spjd	reference_t *ref = NULL;
133168404Spjd	int64_t count;
134209962Smm
135168404Spjd	if (rc->rc_tracked) {
136168404Spjd		ref = kmem_cache_alloc(reference_cache, KM_SLEEP);
137168404Spjd		ref->ref_holder = holder;
138168404Spjd		ref->ref_number = number;
139209962Smm	}
140168404Spjd	mutex_enter(&rc->rc_mtx);
141168404Spjd	ASSERT(rc->rc_count >= 0);
142168404Spjd	if (rc->rc_tracked)
143168404Spjd		list_insert_head(&rc->rc_list, ref);
144168404Spjd	rc->rc_count += number;
145168404Spjd	count = rc->rc_count;
146168404Spjd	mutex_exit(&rc->rc_mtx);
147168404Spjd
148168404Spjd	return (count);
149168404Spjd}
150168404Spjd
151168404Spjdint64_t
152168404Spjdrefcount_add(refcount_t *rc, void *holder)
153168404Spjd{
154168404Spjd	return (refcount_add_many(rc, 1, holder));
155168404Spjd}
156168404Spjd
157168404Spjdint64_t
158168404Spjdrefcount_remove_many(refcount_t *rc, uint64_t number, void *holder)
159168404Spjd{
160168404Spjd	reference_t *ref;
161168404Spjd	int64_t count;
162185029Spjd
163168404Spjd	mutex_enter(&rc->rc_mtx);
164185029Spjd	ASSERT(rc->rc_count >= number);
165168404Spjd
166168404Spjd	if (!rc->rc_tracked) {
167168404Spjd		rc->rc_count -= number;
168168404Spjd		count = rc->rc_count;
169168404Spjd		mutex_exit(&rc->rc_mtx);
170168404Spjd		return (count);
171168404Spjd	}
172168404Spjd
173168404Spjd	for (ref = list_head(&rc->rc_list); ref;
174168404Spjd	    ref = list_next(&rc->rc_list, ref)) {
175168404Spjd		if (ref->ref_holder == holder && ref->ref_number == number) {
176168404Spjd			list_remove(&rc->rc_list, ref);
177168404Spjd			if (reference_history > 0) {
178168404Spjd				ref->ref_removed =
179168404Spjd				    kmem_cache_alloc(reference_history_cache,
180168404Spjd				    KM_SLEEP);
181168404Spjd				list_insert_head(&rc->rc_removed, ref);
182185029Spjd				rc->rc_removed_count++;
183248571Smm				if (rc->rc_removed_count > reference_history) {
184185029Spjd					ref = list_tail(&rc->rc_removed);
185185029Spjd					list_remove(&rc->rc_removed, ref);
186194118Sjamie					kmem_cache_free(reference_history_cache,
187185029Spjd					    ref->ref_removed);
188194118Sjamie					kmem_cache_free(reference_cache, ref);
189185029Spjd					rc->rc_removed_count--;
190248571Smm				}
191185029Spjd			} else {
192185029Spjd				kmem_cache_free(reference_cache, ref);
193168404Spjd			}
194168404Spjd			rc->rc_count -= number;
195168404Spjd			count = rc->rc_count;
196219089Spjd			mutex_exit(&rc->rc_mtx);
197185029Spjd			return (count);
198248571Smm		}
199168404Spjd	}
200248571Smm	panic("No such hold %p on refcount %llx", holder,
201248571Smm	    (u_longlong_t)(uintptr_t)rc);
202168404Spjd	return (-1);
203168404Spjd}
204168404Spjd
205168404Spjdint64_t
206168404Spjdrefcount_remove(refcount_t *rc, void *holder)
207168404Spjd{
208168404Spjd	return (refcount_remove_many(rc, 1, holder));
209168404Spjd}
210168404Spjd
211168404Spjdvoid
212168404Spjdrefcount_transfer(refcount_t *dst, refcount_t *src)
213168404Spjd{
214168404Spjd	int64_t count, removed_count;
215168404Spjd	list_t list, removed;
216168404Spjd
217168404Spjd	list_create(&list, sizeof (reference_t),
218168404Spjd	    offsetof(reference_t, ref_link));
219168404Spjd	list_create(&removed, sizeof (reference_t),
220168404Spjd	    offsetof(reference_t, ref_link));
221168404Spjd
222168404Spjd	mutex_enter(&src->rc_mtx);
223248571Smm	count = src->rc_count;
224168404Spjd	removed_count = src->rc_removed_count;
225168404Spjd	src->rc_count = 0;
226168404Spjd	src->rc_removed_count = 0;
227168404Spjd	list_move_tail(&list, &src->rc_list);
228168404Spjd	list_move_tail(&removed, &src->rc_removed);
229168404Spjd	mutex_exit(&src->rc_mtx);
230168404Spjd
231168404Spjd	mutex_enter(&dst->rc_mtx);
232168404Spjd	dst->rc_count += count;
233168404Spjd	dst->rc_removed_count += removed_count;
234168404Spjd	list_move_tail(&dst->rc_list, &list);
235168404Spjd	list_move_tail(&dst->rc_removed, &removed);
236248571Smm	mutex_exit(&dst->rc_mtx);
237185029Spjd
238248571Smm	list_destroy(&list);
239185029Spjd	list_destroy(&removed);
240248571Smm}
241248571Smm
242248571Smmvoid
243248571Smmrefcount_transfer_ownership(refcount_t *rc, void *current_holder,
244248571Smm    void *new_holder)
245248571Smm{
246248571Smm	reference_t *ref;
247248571Smm	boolean_t found = B_FALSE;
248248571Smm
249248571Smm	mutex_enter(&rc->rc_mtx);
250248571Smm	if (!rc->rc_tracked) {
251248571Smm		mutex_exit(&rc->rc_mtx);
252248571Smm		return;
253248571Smm	}
254248571Smm
255248571Smm	for (ref = list_head(&rc->rc_list); ref;
256248571Smm	    ref = list_next(&rc->rc_list, ref)) {
257248571Smm		if (ref->ref_holder == current_holder) {
258248571Smm			ref->ref_holder = new_holder;
259248571Smm			found = B_TRUE;
260185029Spjd			break;
261185029Spjd		}
262248571Smm	}
263185029Spjd	ASSERT(found);
264168404Spjd	mutex_exit(&rc->rc_mtx);
265168404Spjd}
266168404Spjd
267168404Spjd/*
268168404Spjd * If tracking is enabled, return true if a reference exists that matches
269168404Spjd * the "holder" tag. If tracking is disabled, then return true if a reference
270168404Spjd * might be held.
271168404Spjd */
272248571Smmboolean_t
273248571Smmrefcount_held(refcount_t *rc, void *holder)
274248571Smm{
275248571Smm	reference_t *ref;
276168404Spjd
277168404Spjd	mutex_enter(&rc->rc_mtx);
278168404Spjd
279248571Smm	if (!rc->rc_tracked) {
280168404Spjd		mutex_exit(&rc->rc_mtx);
281248571Smm		return (rc->rc_count > 0);
282168404Spjd	}
283168404Spjd
284168404Spjd	for (ref = list_head(&rc->rc_list); ref;
285168404Spjd	    ref = list_next(&rc->rc_list, ref)) {
286168404Spjd		if (ref->ref_holder == holder) {
287168404Spjd			mutex_exit(&rc->rc_mtx);
288248571Smm			return (B_TRUE);
289168404Spjd		}
290248571Smm	}
291248571Smm	mutex_exit(&rc->rc_mtx);
292248571Smm	return (B_FALSE);
293248571Smm}
294248571Smm
295248571Smm/*
296248571Smm * If tracking is enabled, return true if a reference does not exist that
297248571Smm * matches the "holder" tag. If tracking is disabled, always return true
298248571Smm * since the reference might not be held.
299248571Smm */
300248571Smmboolean_t
301248571Smmrefcount_not_held(refcount_t *rc, void *holder)
302219089Spjd{
303219089Spjd	reference_t *ref;
304248571Smm
305168404Spjd	mutex_enter(&rc->rc_mtx);
306248571Smm
307248571Smm	if (!rc->rc_tracked) {
308185029Spjd		mutex_exit(&rc->rc_mtx);
309240133Smm		return (B_TRUE);
310249195Smm	}
311240133Smm
312219089Spjd	for (ref = list_head(&rc->rc_list); ref;
313219089Spjd	    ref = list_next(&rc->rc_list, ref)) {
314219089Spjd		if (ref->ref_holder == holder) {
315219089Spjd			mutex_exit(&rc->rc_mtx);
316219089Spjd			return (B_FALSE);
317219089Spjd		}
318219089Spjd	}
319248571Smm	mutex_exit(&rc->rc_mtx);
320248571Smm	return (B_TRUE);
321248571Smm}
322248571Smm#endif	/* ZFS_DEBUG */
323248571Smm