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.
23248571Smm * Copyright (c) 2012 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 */
33168404Spjd#else
34168404Spjdint reference_tracking_enable = TRUE;
35168404Spjd#endif
36248571Smmint reference_history = 3; /* tunable */
37168404Spjd
38168404Spjdstatic kmem_cache_t *reference_cache;
39168404Spjdstatic kmem_cache_t *reference_history_cache;
40168404Spjd
41168404Spjdvoid
42179310Spjdrefcount_sysinit(void)
43168404Spjd{
44168404Spjd	reference_cache = kmem_cache_create("reference_cache",
45168404Spjd	    sizeof (reference_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
46168404Spjd
47168404Spjd	reference_history_cache = kmem_cache_create("reference_history_cache",
48168404Spjd	    sizeof (uint64_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
49168404Spjd}
50168404Spjd
51168404Spjdvoid
52168404Spjdrefcount_fini(void)
53168404Spjd{
54168404Spjd	kmem_cache_destroy(reference_cache);
55168404Spjd	kmem_cache_destroy(reference_history_cache);
56168404Spjd}
57168404Spjd
58168404Spjdvoid
59168404Spjdrefcount_create(refcount_t *rc)
60168404Spjd{
61185029Spjd	mutex_init(&rc->rc_mtx, NULL, MUTEX_DEFAULT, NULL);
62168404Spjd	list_create(&rc->rc_list, sizeof (reference_t),
63168404Spjd	    offsetof(reference_t, ref_link));
64168404Spjd	list_create(&rc->rc_removed, sizeof (reference_t),
65168404Spjd	    offsetof(reference_t, ref_link));
66185029Spjd	rc->rc_count = 0;
67185029Spjd	rc->rc_removed_count = 0;
68248571Smm	rc->rc_tracked = reference_tracking_enable;
69168404Spjd}
70168404Spjd
71168404Spjdvoid
72248571Smmrefcount_create_untracked(refcount_t *rc)
73248571Smm{
74248571Smm	refcount_create(rc);
75248571Smm	rc->rc_tracked = B_FALSE;
76248571Smm}
77248571Smm
78248571Smmvoid
79168404Spjdrefcount_destroy_many(refcount_t *rc, uint64_t number)
80168404Spjd{
81168404Spjd	reference_t *ref;
82168404Spjd
83168404Spjd	ASSERT(rc->rc_count == number);
84168404Spjd	while (ref = list_head(&rc->rc_list)) {
85168404Spjd		list_remove(&rc->rc_list, ref);
86168404Spjd		kmem_cache_free(reference_cache, ref);
87168404Spjd	}
88168404Spjd	list_destroy(&rc->rc_list);
89168404Spjd
90168404Spjd	while (ref = list_head(&rc->rc_removed)) {
91168404Spjd		list_remove(&rc->rc_removed, ref);
92168404Spjd		kmem_cache_free(reference_history_cache, ref->ref_removed);
93168404Spjd		kmem_cache_free(reference_cache, ref);
94168404Spjd	}
95168404Spjd	list_destroy(&rc->rc_removed);
96168404Spjd	mutex_destroy(&rc->rc_mtx);
97168404Spjd}
98168404Spjd
99168404Spjdvoid
100168404Spjdrefcount_destroy(refcount_t *rc)
101168404Spjd{
102168404Spjd	refcount_destroy_many(rc, 0);
103168404Spjd}
104168404Spjd
105168404Spjdint
106168404Spjdrefcount_is_zero(refcount_t *rc)
107168404Spjd{
108168404Spjd	return (rc->rc_count == 0);
109168404Spjd}
110168404Spjd
111168404Spjdint64_t
112168404Spjdrefcount_count(refcount_t *rc)
113168404Spjd{
114168404Spjd	return (rc->rc_count);
115168404Spjd}
116168404Spjd
117168404Spjdint64_t
118168404Spjdrefcount_add_many(refcount_t *rc, uint64_t number, void *holder)
119168404Spjd{
120247187Smm	reference_t *ref = NULL;
121168404Spjd	int64_t count;
122168404Spjd
123248571Smm	if (rc->rc_tracked) {
124168404Spjd		ref = kmem_cache_alloc(reference_cache, KM_SLEEP);
125168404Spjd		ref->ref_holder = holder;
126168404Spjd		ref->ref_number = number;
127168404Spjd	}
128168404Spjd	mutex_enter(&rc->rc_mtx);
129168404Spjd	ASSERT(rc->rc_count >= 0);
130248571Smm	if (rc->rc_tracked)
131168404Spjd		list_insert_head(&rc->rc_list, ref);
132168404Spjd	rc->rc_count += number;
133168404Spjd	count = rc->rc_count;
134168404Spjd	mutex_exit(&rc->rc_mtx);
135168404Spjd
136168404Spjd	return (count);
137168404Spjd}
138168404Spjd
139168404Spjdint64_t
140168404Spjdrefcount_add(refcount_t *rc, void *holder)
141168404Spjd{
142168404Spjd	return (refcount_add_many(rc, 1, holder));
143168404Spjd}
144168404Spjd
145168404Spjdint64_t
146168404Spjdrefcount_remove_many(refcount_t *rc, uint64_t number, void *holder)
147168404Spjd{
148168404Spjd	reference_t *ref;
149168404Spjd	int64_t count;
150168404Spjd
151168404Spjd	mutex_enter(&rc->rc_mtx);
152168404Spjd	ASSERT(rc->rc_count >= number);
153168404Spjd
154248571Smm	if (!rc->rc_tracked) {
155168404Spjd		rc->rc_count -= number;
156168404Spjd		count = rc->rc_count;
157168404Spjd		mutex_exit(&rc->rc_mtx);
158168404Spjd		return (count);
159168404Spjd	}
160168404Spjd
161168404Spjd	for (ref = list_head(&rc->rc_list); ref;
162168404Spjd	    ref = list_next(&rc->rc_list, ref)) {
163168404Spjd		if (ref->ref_holder == holder && ref->ref_number == number) {
164168404Spjd			list_remove(&rc->rc_list, ref);
165168404Spjd			if (reference_history > 0) {
166168404Spjd				ref->ref_removed =
167168404Spjd				    kmem_cache_alloc(reference_history_cache,
168168404Spjd				    KM_SLEEP);
169168404Spjd				list_insert_head(&rc->rc_removed, ref);
170168404Spjd				rc->rc_removed_count++;
171248571Smm				if (rc->rc_removed_count > reference_history) {
172168404Spjd					ref = list_tail(&rc->rc_removed);
173168404Spjd					list_remove(&rc->rc_removed, ref);
174168404Spjd					kmem_cache_free(reference_history_cache,
175168404Spjd					    ref->ref_removed);
176168404Spjd					kmem_cache_free(reference_cache, ref);
177168404Spjd					rc->rc_removed_count--;
178168404Spjd				}
179168404Spjd			} else {
180168404Spjd				kmem_cache_free(reference_cache, ref);
181168404Spjd			}
182168404Spjd			rc->rc_count -= number;
183168404Spjd			count = rc->rc_count;
184168404Spjd			mutex_exit(&rc->rc_mtx);
185168404Spjd			return (count);
186168404Spjd		}
187168404Spjd	}
188168404Spjd	panic("No such hold %p on refcount %llx", holder,
189168404Spjd	    (u_longlong_t)(uintptr_t)rc);
190168404Spjd	return (-1);
191168404Spjd}
192168404Spjd
193168404Spjdint64_t
194168404Spjdrefcount_remove(refcount_t *rc, void *holder)
195168404Spjd{
196168404Spjd	return (refcount_remove_many(rc, 1, holder));
197168404Spjd}
198168404Spjd
199219089Spjdvoid
200219089Spjdrefcount_transfer(refcount_t *dst, refcount_t *src)
201219089Spjd{
202219089Spjd	int64_t count, removed_count;
203219089Spjd	list_t list, removed;
204219089Spjd
205219089Spjd	list_create(&list, sizeof (reference_t),
206219089Spjd	    offsetof(reference_t, ref_link));
207219089Spjd	list_create(&removed, sizeof (reference_t),
208219089Spjd	    offsetof(reference_t, ref_link));
209219089Spjd
210219089Spjd	mutex_enter(&src->rc_mtx);
211219089Spjd	count = src->rc_count;
212219089Spjd	removed_count = src->rc_removed_count;
213219089Spjd	src->rc_count = 0;
214219089Spjd	src->rc_removed_count = 0;
215219089Spjd	list_move_tail(&list, &src->rc_list);
216219089Spjd	list_move_tail(&removed, &src->rc_removed);
217219089Spjd	mutex_exit(&src->rc_mtx);
218219089Spjd
219219089Spjd	mutex_enter(&dst->rc_mtx);
220219089Spjd	dst->rc_count += count;
221219089Spjd	dst->rc_removed_count += removed_count;
222219089Spjd	list_move_tail(&dst->rc_list, &list);
223219089Spjd	list_move_tail(&dst->rc_removed, &removed);
224219089Spjd	mutex_exit(&dst->rc_mtx);
225219089Spjd
226219089Spjd	list_destroy(&list);
227219089Spjd	list_destroy(&removed);
228219089Spjd}
229219089Spjd
230219089Spjd#endif	/* ZFS_DEBUG */
231