]> git.proxmox.com Git - mirror_zfs-debian.git/blob - module/zfs/zrlock.c
Imported Upstream version 0.6.4.2
[mirror_zfs-debian.git] / module / zfs / zrlock.c
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) 2010, Oracle and/or its affiliates. All rights reserved.
23 */
24
25 /*
26 * A Zero Reference Lock (ZRL) is a reference count that can lock out new
27 * references only when the count is zero and only without waiting if the count
28 * is not already zero. It is similar to a read-write lock in that it allows
29 * multiple readers and only a single writer, but it does not allow a writer to
30 * block while waiting for readers to exit, and therefore the question of
31 * reader/writer priority is moot (no WRWANT bit). Since the equivalent of
32 * rw_enter(&lock, RW_WRITER) is disallowed and only tryenter() is allowed, it
33 * is perfectly safe for the same reader to acquire the same lock multiple
34 * times. The fact that a ZRL is reentrant for readers (through multiple calls
35 * to zrl_add()) makes it convenient for determining whether something is
36 * actively referenced without the fuss of flagging lock ownership across
37 * function calls.
38 */
39 #include <sys/zrlock.h>
40 #include <sys/trace_zrlock.h>
41
42 /*
43 * A ZRL can be locked only while there are zero references, so ZRL_LOCKED is
44 * treated as zero references.
45 */
46 #define ZRL_LOCKED ((uint32_t)-1)
47 #define ZRL_DESTROYED -2
48
49 void
50 zrl_init(zrlock_t *zrl)
51 {
52 mutex_init(&zrl->zr_mtx, NULL, MUTEX_DEFAULT, NULL);
53 zrl->zr_refcount = 0;
54 cv_init(&zrl->zr_cv, NULL, CV_DEFAULT, NULL);
55 #ifdef ZFS_DEBUG
56 zrl->zr_owner = NULL;
57 zrl->zr_caller = NULL;
58 #endif
59 }
60
61 void
62 zrl_destroy(zrlock_t *zrl)
63 {
64 ASSERT(zrl->zr_refcount == 0);
65
66 mutex_destroy(&zrl->zr_mtx);
67 zrl->zr_refcount = ZRL_DESTROYED;
68 cv_destroy(&zrl->zr_cv);
69 }
70
71 void
72 #ifdef ZFS_DEBUG
73 zrl_add_debug(zrlock_t *zrl, const char *zc)
74 #else
75 zrl_add(zrlock_t *zrl)
76 #endif
77 {
78 uint32_t n = (uint32_t)zrl->zr_refcount;
79
80 while (n != ZRL_LOCKED) {
81 uint32_t cas = atomic_cas_32(
82 (uint32_t *)&zrl->zr_refcount, n, n + 1);
83 if (cas == n) {
84 ASSERT((int32_t)n >= 0);
85 #ifdef ZFS_DEBUG
86 if (zrl->zr_owner == curthread) {
87 DTRACE_PROBE2(zrlock__reentry,
88 zrlock_t *, zrl, uint32_t, n);
89 }
90 zrl->zr_owner = curthread;
91 zrl->zr_caller = zc;
92 #endif
93 return;
94 }
95 n = cas;
96 }
97
98 mutex_enter(&zrl->zr_mtx);
99 while (zrl->zr_refcount == ZRL_LOCKED) {
100 cv_wait(&zrl->zr_cv, &zrl->zr_mtx);
101 }
102 ASSERT(zrl->zr_refcount >= 0);
103 zrl->zr_refcount++;
104 #ifdef ZFS_DEBUG
105 zrl->zr_owner = curthread;
106 zrl->zr_caller = zc;
107 #endif
108 mutex_exit(&zrl->zr_mtx);
109 }
110
111 void
112 zrl_remove(zrlock_t *zrl)
113 {
114 uint32_t n;
115
116 n = atomic_dec_32_nv((uint32_t *)&zrl->zr_refcount);
117 ASSERT((int32_t)n >= 0);
118 #ifdef ZFS_DEBUG
119 if (zrl->zr_owner == curthread) {
120 zrl->zr_owner = NULL;
121 zrl->zr_caller = NULL;
122 }
123 #endif
124 }
125
126 int
127 zrl_tryenter(zrlock_t *zrl)
128 {
129 uint32_t n = (uint32_t)zrl->zr_refcount;
130
131 if (n == 0) {
132 uint32_t cas = atomic_cas_32(
133 (uint32_t *)&zrl->zr_refcount, 0, ZRL_LOCKED);
134 if (cas == 0) {
135 #ifdef ZFS_DEBUG
136 ASSERT(zrl->zr_owner == NULL);
137 zrl->zr_owner = curthread;
138 #endif
139 return (1);
140 }
141 }
142
143 ASSERT((int32_t)n > ZRL_DESTROYED);
144
145 return (0);
146 }
147
148 void
149 zrl_exit(zrlock_t *zrl)
150 {
151 ASSERT(zrl->zr_refcount == ZRL_LOCKED);
152
153 mutex_enter(&zrl->zr_mtx);
154 #ifdef ZFS_DEBUG
155 ASSERT(zrl->zr_owner == curthread);
156 zrl->zr_owner = NULL;
157 membar_producer(); /* make sure the owner store happens first */
158 #endif
159 zrl->zr_refcount = 0;
160 cv_broadcast(&zrl->zr_cv);
161 mutex_exit(&zrl->zr_mtx);
162 }
163
164 int
165 zrl_refcount(zrlock_t *zrl)
166 {
167 int n;
168
169 ASSERT(zrl->zr_refcount > ZRL_DESTROYED);
170
171 n = (int)zrl->zr_refcount;
172 return (n <= 0 ? 0 : n);
173 }
174
175 int
176 zrl_is_zero(zrlock_t *zrl)
177 {
178 ASSERT(zrl->zr_refcount > ZRL_DESTROYED);
179
180 return (zrl->zr_refcount <= 0);
181 }
182
183 int
184 zrl_is_locked(zrlock_t *zrl)
185 {
186 ASSERT(zrl->zr_refcount > ZRL_DESTROYED);
187
188 return (zrl->zr_refcount == ZRL_LOCKED);
189 }
190
191 #ifdef ZFS_DEBUG
192 kthread_t *
193 zrl_owner(zrlock_t *zrl)
194 {
195 return (zrl->zr_owner);
196 }
197 #endif
198
199 #if defined(_KERNEL) && defined(HAVE_SPL)
200
201 #ifdef ZFS_DEBUG
202 EXPORT_SYMBOL(zrl_add_debug);
203 #else
204 EXPORT_SYMBOL(zrl_add);
205 #endif
206 EXPORT_SYMBOL(zrl_remove);
207
208 #endif