xref: /titanic_44/usr/src/uts/common/fs/zfs/refcount.c (revision 40e5e17b3361b3eea56a9723071c406894a20b78)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/zfs_context.h>
30 #include <sys/refcount.h>
31 
32 #if defined(DEBUG) || !defined(_KERNEL)
33 
34 #ifdef _KERNEL
35 int reference_tracking_enable = FALSE; /* runs out of memory too easily */
36 #else
37 int reference_tracking_enable = TRUE;
38 #endif
39 int reference_history = 4; /* tunable */
40 
41 static kmem_cache_t *reference_cache;
42 static kmem_cache_t *reference_history_cache;
43 
44 void
45 refcount_init(void)
46 {
47 	reference_cache = kmem_cache_create("reference_cache",
48 	    sizeof (reference_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
49 
50 	reference_history_cache = kmem_cache_create("reference_history_cache",
51 	    sizeof (uint64_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
52 }
53 
54 void
55 refcount_fini(void)
56 {
57 	kmem_cache_destroy(reference_cache);
58 	kmem_cache_destroy(reference_history_cache);
59 }
60 
61 void
62 refcount_create(refcount_t *rc)
63 {
64 	list_create(&rc->rc_list, sizeof (reference_t),
65 	    offsetof(reference_t, ref_link));
66 	list_create(&rc->rc_removed, sizeof (reference_t),
67 	    offsetof(reference_t, ref_link));
68 	mutex_init(&rc->rc_mtx, NULL, MUTEX_DEFAULT, NULL);
69 }
70 
71 void
72 refcount_destroy_many(refcount_t *rc, uint64_t number)
73 {
74 	reference_t *ref;
75 
76 	ASSERT(rc->rc_count == number);
77 	while (ref = list_head(&rc->rc_list)) {
78 		list_remove(&rc->rc_list, ref);
79 		kmem_cache_free(reference_cache, ref);
80 	}
81 	list_destroy(&rc->rc_list);
82 
83 	while (ref = list_head(&rc->rc_removed)) {
84 		list_remove(&rc->rc_removed, ref);
85 		kmem_cache_free(reference_history_cache, ref->ref_removed);
86 		kmem_cache_free(reference_cache, ref);
87 	}
88 	list_destroy(&rc->rc_removed);
89 	mutex_destroy(&rc->rc_mtx);
90 }
91 
92 void
93 refcount_destroy(refcount_t *rc)
94 {
95 	refcount_destroy_many(rc, 0);
96 }
97 
98 int
99 refcount_is_zero(refcount_t *rc)
100 {
101 	ASSERT(rc->rc_count >= 0);
102 	return (rc->rc_count == 0);
103 }
104 
105 int64_t
106 refcount_count(refcount_t *rc)
107 {
108 	ASSERT(rc->rc_count >= 0);
109 	return (rc->rc_count);
110 }
111 
112 int64_t
113 refcount_add_many(refcount_t *rc, uint64_t number, void *holder)
114 {
115 	reference_t *ref;
116 	int64_t count;
117 
118 	if (reference_tracking_enable) {
119 		ref = kmem_cache_alloc(reference_cache, KM_SLEEP);
120 		ref->ref_holder = holder;
121 		ref->ref_number = number;
122 	}
123 	mutex_enter(&rc->rc_mtx);
124 	ASSERT(rc->rc_count >= 0);
125 	if (reference_tracking_enable)
126 		list_insert_head(&rc->rc_list, ref);
127 	rc->rc_count += number;
128 	count = rc->rc_count;
129 	mutex_exit(&rc->rc_mtx);
130 
131 	return (count);
132 }
133 
134 int64_t
135 refcount_add(refcount_t *rc, void *holder)
136 {
137 	return (refcount_add_many(rc, 1, holder));
138 }
139 
140 int64_t
141 refcount_remove_many(refcount_t *rc, uint64_t number, void *holder)
142 {
143 	reference_t *ref;
144 	int64_t count;
145 
146 	mutex_enter(&rc->rc_mtx);
147 	ASSERT(rc->rc_count >= number);
148 
149 	if (!reference_tracking_enable) {
150 		rc->rc_count -= number;
151 		count = rc->rc_count;
152 		mutex_exit(&rc->rc_mtx);
153 		return (count);
154 	}
155 
156 	for (ref = list_head(&rc->rc_list); ref;
157 	    ref = list_next(&rc->rc_list, ref)) {
158 		if (ref->ref_holder == holder && ref->ref_number == number) {
159 			list_remove(&rc->rc_list, ref);
160 			if (reference_history > 0) {
161 				ref->ref_removed =
162 				    kmem_cache_alloc(reference_history_cache,
163 				    KM_SLEEP);
164 				list_insert_head(&rc->rc_removed, ref);
165 				rc->rc_removed_count++;
166 				if (rc->rc_removed_count >= reference_history) {
167 					ref = list_tail(&rc->rc_removed);
168 					list_remove(&rc->rc_removed, ref);
169 					kmem_cache_free(reference_history_cache,
170 					    ref->ref_removed);
171 					kmem_cache_free(reference_cache, ref);
172 					rc->rc_removed_count--;
173 				}
174 			} else {
175 				kmem_cache_free(reference_cache, ref);
176 			}
177 			rc->rc_count -= number;
178 			count = rc->rc_count;
179 			mutex_exit(&rc->rc_mtx);
180 			return (count);
181 		}
182 	}
183 	panic("No such hold %p on refcount %llx", holder,
184 	    (u_longlong_t)(uintptr_t)rc);
185 	return (-1);
186 }
187 
188 int64_t
189 refcount_remove(refcount_t *rc, void *holder)
190 {
191 	return (refcount_remove_many(rc, 1, holder));
192 }
193 
194 #endif
195