xref: /NextBSD/sys/cddl/contrib/opensolaris/uts/common/fs/zfs/refcount.c (revision 287e3b14e9552995def1802ec9c5034f4adf28ec)
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 by Delphix. All rights reserved.
24  */
25 
26 #include <sys/zfs_context.h>
27 #include <sys/refcount.h>
28 
29 #ifdef	ZFS_DEBUG
30 
31 #ifdef _KERNEL
32 int reference_tracking_enable = FALSE; /* runs out of memory too easily */
33 SYSCTL_DECL(_vfs_zfs);
34 SYSCTL_INT(_vfs_zfs, OID_AUTO, reference_tracking_enable, CTLFLAG_RDTUN,
35     &reference_tracking_enable, 0,
36     "Track reference holders to refcount_t objects, used mostly by ZFS");
37 #else
38 int reference_tracking_enable = TRUE;
39 #endif
40 int reference_history = 3; /* tunable */
41 
42 static kmem_cache_t *reference_cache;
43 static kmem_cache_t *reference_history_cache;
44 
45 void
refcount_sysinit(void)46 refcount_sysinit(void)
47 {
48 	reference_cache = kmem_cache_create("reference_cache",
49 	    sizeof (reference_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
50 
51 	reference_history_cache = kmem_cache_create("reference_history_cache",
52 	    sizeof (uint64_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
53 }
54 
55 void
refcount_fini(void)56 refcount_fini(void)
57 {
58 	kmem_cache_destroy(reference_cache);
59 	kmem_cache_destroy(reference_history_cache);
60 }
61 
62 void
refcount_create(refcount_t * rc)63 refcount_create(refcount_t *rc)
64 {
65 	mutex_init(&rc->rc_mtx, NULL, MUTEX_DEFAULT, NULL);
66 	list_create(&rc->rc_list, sizeof (reference_t),
67 	    offsetof(reference_t, ref_link));
68 	list_create(&rc->rc_removed, sizeof (reference_t),
69 	    offsetof(reference_t, ref_link));
70 	rc->rc_count = 0;
71 	rc->rc_removed_count = 0;
72 	rc->rc_tracked = reference_tracking_enable;
73 }
74 
75 void
refcount_create_untracked(refcount_t * rc)76 refcount_create_untracked(refcount_t *rc)
77 {
78 	refcount_create(rc);
79 	rc->rc_tracked = B_FALSE;
80 }
81 
82 void
refcount_destroy_many(refcount_t * rc,uint64_t number)83 refcount_destroy_many(refcount_t *rc, uint64_t number)
84 {
85 	reference_t *ref;
86 
87 	ASSERT(rc->rc_count == number);
88 	while (ref = list_head(&rc->rc_list)) {
89 		list_remove(&rc->rc_list, ref);
90 		kmem_cache_free(reference_cache, ref);
91 	}
92 	list_destroy(&rc->rc_list);
93 
94 	while (ref = list_head(&rc->rc_removed)) {
95 		list_remove(&rc->rc_removed, ref);
96 		kmem_cache_free(reference_history_cache, ref->ref_removed);
97 		kmem_cache_free(reference_cache, ref);
98 	}
99 	list_destroy(&rc->rc_removed);
100 	mutex_destroy(&rc->rc_mtx);
101 }
102 
103 void
refcount_destroy(refcount_t * rc)104 refcount_destroy(refcount_t *rc)
105 {
106 	refcount_destroy_many(rc, 0);
107 }
108 
109 int
refcount_is_zero(refcount_t * rc)110 refcount_is_zero(refcount_t *rc)
111 {
112 	return (rc->rc_count == 0);
113 }
114 
115 int64_t
refcount_count(refcount_t * rc)116 refcount_count(refcount_t *rc)
117 {
118 	return (rc->rc_count);
119 }
120 
121 int64_t
refcount_add_many(refcount_t * rc,uint64_t number,void * holder)122 refcount_add_many(refcount_t *rc, uint64_t number, void *holder)
123 {
124 	reference_t *ref = NULL;
125 	int64_t count;
126 
127 	if (rc->rc_tracked) {
128 		ref = kmem_cache_alloc(reference_cache, KM_SLEEP);
129 		ref->ref_holder = holder;
130 		ref->ref_number = number;
131 	}
132 	mutex_enter(&rc->rc_mtx);
133 	ASSERT(rc->rc_count >= 0);
134 	if (rc->rc_tracked)
135 		list_insert_head(&rc->rc_list, ref);
136 	rc->rc_count += number;
137 	count = rc->rc_count;
138 	mutex_exit(&rc->rc_mtx);
139 
140 	return (count);
141 }
142 
143 int64_t
refcount_add(refcount_t * rc,void * holder)144 refcount_add(refcount_t *rc, void *holder)
145 {
146 	return (refcount_add_many(rc, 1, holder));
147 }
148 
149 int64_t
refcount_remove_many(refcount_t * rc,uint64_t number,void * holder)150 refcount_remove_many(refcount_t *rc, uint64_t number, void *holder)
151 {
152 	reference_t *ref;
153 	int64_t count;
154 
155 	mutex_enter(&rc->rc_mtx);
156 	ASSERT(rc->rc_count >= number);
157 
158 	if (!rc->rc_tracked) {
159 		rc->rc_count -= number;
160 		count = rc->rc_count;
161 		mutex_exit(&rc->rc_mtx);
162 		return (count);
163 	}
164 
165 	for (ref = list_head(&rc->rc_list); ref;
166 	    ref = list_next(&rc->rc_list, ref)) {
167 		if (ref->ref_holder == holder && ref->ref_number == number) {
168 			list_remove(&rc->rc_list, ref);
169 			if (reference_history > 0) {
170 				ref->ref_removed =
171 				    kmem_cache_alloc(reference_history_cache,
172 				    KM_SLEEP);
173 				list_insert_head(&rc->rc_removed, ref);
174 				rc->rc_removed_count++;
175 				if (rc->rc_removed_count > reference_history) {
176 					ref = list_tail(&rc->rc_removed);
177 					list_remove(&rc->rc_removed, ref);
178 					kmem_cache_free(reference_history_cache,
179 					    ref->ref_removed);
180 					kmem_cache_free(reference_cache, ref);
181 					rc->rc_removed_count--;
182 				}
183 			} else {
184 				kmem_cache_free(reference_cache, ref);
185 			}
186 			rc->rc_count -= number;
187 			count = rc->rc_count;
188 			mutex_exit(&rc->rc_mtx);
189 			return (count);
190 		}
191 	}
192 	panic("No such hold %p on refcount %llx", holder,
193 	    (u_longlong_t)(uintptr_t)rc);
194 	return (-1);
195 }
196 
197 int64_t
refcount_remove(refcount_t * rc,void * holder)198 refcount_remove(refcount_t *rc, void *holder)
199 {
200 	return (refcount_remove_many(rc, 1, holder));
201 }
202 
203 void
refcount_transfer(refcount_t * dst,refcount_t * src)204 refcount_transfer(refcount_t *dst, refcount_t *src)
205 {
206 	int64_t count, removed_count;
207 	list_t list, removed;
208 
209 	list_create(&list, sizeof (reference_t),
210 	    offsetof(reference_t, ref_link));
211 	list_create(&removed, sizeof (reference_t),
212 	    offsetof(reference_t, ref_link));
213 
214 	mutex_enter(&src->rc_mtx);
215 	count = src->rc_count;
216 	removed_count = src->rc_removed_count;
217 	src->rc_count = 0;
218 	src->rc_removed_count = 0;
219 	list_move_tail(&list, &src->rc_list);
220 	list_move_tail(&removed, &src->rc_removed);
221 	mutex_exit(&src->rc_mtx);
222 
223 	mutex_enter(&dst->rc_mtx);
224 	dst->rc_count += count;
225 	dst->rc_removed_count += removed_count;
226 	list_move_tail(&dst->rc_list, &list);
227 	list_move_tail(&dst->rc_removed, &removed);
228 	mutex_exit(&dst->rc_mtx);
229 
230 	list_destroy(&list);
231 	list_destroy(&removed);
232 }
233 
234 #endif	/* ZFS_DEBUG */
235