Print this page
2882 implement libzfs_core
2883 changing "canmount" property to "on" should not always remount dataset
2900 "zfs snapshot" should be able to create multiple, arbitrary snapshots at once
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Chris Siden <christopher.siden@delphix.com>
Reviewed by: Garrett D'Amore <garrett@damore.org>
Reviewed by: Bill Pijewski <wdp@joyent.com>
Reviewed by: Dan Kruchinin <dan.kruchinin@gmail.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/zfs/rrwlock.c
+++ new/usr/src/uts/common/fs/zfs/rrwlock.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 +/*
26 + * Copyright (c) 2012 by Delphix. All rights reserved.
27 + */
25 28
26 29 #include <sys/refcount.h>
27 30 #include <sys/rrwlock.h>
28 31
29 32 /*
30 33 * This file contains the implementation of a re-entrant read
31 34 * reader/writer lock (aka "rrwlock").
32 35 *
33 36 * This is a normal reader/writer lock with the additional feature
34 37 * of allowing threads who have already obtained a read lock to
35 38 * re-enter another read lock (re-entrant read) - even if there are
36 39 * waiting writers.
37 40 *
38 41 * Callers who have not obtained a read lock give waiting writers priority.
39 42 *
40 43 * The rrwlock_t lock does not allow re-entrant writers, nor does it
41 44 * allow a re-entrant mix of reads and writes (that is, it does not
42 45 * allow a caller who has already obtained a read lock to be able to
43 46 * then grab a write lock without first dropping all read locks, and
44 47 * vice versa).
45 48 *
46 49 * The rrwlock_t uses tsd (thread specific data) to keep a list of
47 50 * nodes (rrw_node_t), where each node keeps track of which specific
48 51 * lock (rrw_node_t::rn_rrl) the thread has grabbed. Since re-entering
49 52 * should be rare, a thread that grabs multiple reads on the same rrwlock_t
50 53 * will store multiple rrw_node_ts of the same 'rrn_rrl'. Nodes on the
51 54 * tsd list can represent a different rrwlock_t. This allows a thread
52 55 * to enter multiple and unique rrwlock_ts for read locks at the same time.
53 56 *
54 57 * Since using tsd exposes some overhead, the rrwlock_t only needs to
55 58 * keep tsd data when writers are waiting. If no writers are waiting, then
56 59 * a reader just bumps the anonymous read count (rr_anon_rcount) - no tsd
57 60 * is needed. Once a writer attempts to grab the lock, readers then
58 61 * keep tsd data and bump the linked readers count (rr_linked_rcount).
59 62 *
60 63 * If there are waiting writers and there are anonymous readers, then a
61 64 * reader doesn't know if it is a re-entrant lock. But since it may be one,
62 65 * we allow the read to proceed (otherwise it could deadlock). Since once
63 66 * waiting writers are active, readers no longer bump the anonymous count,
64 67 * the anonymous readers will eventually flush themselves out. At this point,
65 68 * readers will be able to tell if they are a re-entrant lock (have a
66 69 * rrw_node_t entry for the lock) or not. If they are a re-entrant lock, then
67 70 * we must let the proceed. If they are not, then the reader blocks for the
68 71 * waiting writers. Hence, we do not starve writers.
69 72 */
70 73
71 74 /* global key for TSD */
72 75 uint_t rrw_tsd_key;
73 76
74 77 typedef struct rrw_node {
75 78 struct rrw_node *rn_next;
76 79 rrwlock_t *rn_rrl;
77 80 } rrw_node_t;
78 81
79 82 static rrw_node_t *
80 83 rrn_find(rrwlock_t *rrl)
81 84 {
82 85 rrw_node_t *rn;
83 86
84 87 if (refcount_count(&rrl->rr_linked_rcount) == 0)
85 88 return (NULL);
86 89
87 90 for (rn = tsd_get(rrw_tsd_key); rn != NULL; rn = rn->rn_next) {
88 91 if (rn->rn_rrl == rrl)
89 92 return (rn);
90 93 }
91 94 return (NULL);
92 95 }
93 96
94 97 /*
95 98 * Add a node to the head of the singly linked list.
96 99 */
97 100 static void
98 101 rrn_add(rrwlock_t *rrl)
99 102 {
100 103 rrw_node_t *rn;
101 104
102 105 rn = kmem_alloc(sizeof (*rn), KM_SLEEP);
103 106 rn->rn_rrl = rrl;
104 107 rn->rn_next = tsd_get(rrw_tsd_key);
105 108 VERIFY(tsd_set(rrw_tsd_key, rn) == 0);
106 109 }
107 110
108 111 /*
109 112 * If a node is found for 'rrl', then remove the node from this
110 113 * thread's list and return TRUE; otherwise return FALSE.
111 114 */
112 115 static boolean_t
113 116 rrn_find_and_remove(rrwlock_t *rrl)
114 117 {
115 118 rrw_node_t *rn;
116 119 rrw_node_t *prev = NULL;
117 120
118 121 if (refcount_count(&rrl->rr_linked_rcount) == 0)
119 122 return (B_FALSE);
120 123
121 124 for (rn = tsd_get(rrw_tsd_key); rn != NULL; rn = rn->rn_next) {
122 125 if (rn->rn_rrl == rrl) {
123 126 if (prev)
124 127 prev->rn_next = rn->rn_next;
125 128 else
126 129 VERIFY(tsd_set(rrw_tsd_key, rn->rn_next) == 0);
127 130 kmem_free(rn, sizeof (*rn));
128 131 return (B_TRUE);
129 132 }
130 133 prev = rn;
131 134 }
132 135 return (B_FALSE);
133 136 }
134 137
135 138 void
136 139 rrw_init(rrwlock_t *rrl)
137 140 {
138 141 mutex_init(&rrl->rr_lock, NULL, MUTEX_DEFAULT, NULL);
139 142 cv_init(&rrl->rr_cv, NULL, CV_DEFAULT, NULL);
140 143 rrl->rr_writer = NULL;
141 144 refcount_create(&rrl->rr_anon_rcount);
142 145 refcount_create(&rrl->rr_linked_rcount);
143 146 rrl->rr_writer_wanted = B_FALSE;
144 147 }
145 148
146 149 void
147 150 rrw_destroy(rrwlock_t *rrl)
148 151 {
149 152 mutex_destroy(&rrl->rr_lock);
150 153 cv_destroy(&rrl->rr_cv);
151 154 ASSERT(rrl->rr_writer == NULL);
152 155 refcount_destroy(&rrl->rr_anon_rcount);
153 156 refcount_destroy(&rrl->rr_linked_rcount);
154 157 }
155 158
156 159 static void
157 160 rrw_enter_read(rrwlock_t *rrl, void *tag)
158 161 {
159 162 mutex_enter(&rrl->rr_lock);
160 163 #if !defined(DEBUG) && defined(_KERNEL)
161 164 if (!rrl->rr_writer && !rrl->rr_writer_wanted) {
162 165 rrl->rr_anon_rcount.rc_count++;
163 166 mutex_exit(&rrl->rr_lock);
164 167 return;
165 168 }
166 169 DTRACE_PROBE(zfs__rrwfastpath__rdmiss);
167 170 #endif
168 171 ASSERT(rrl->rr_writer != curthread);
169 172 ASSERT(refcount_count(&rrl->rr_anon_rcount) >= 0);
170 173
171 174 while (rrl->rr_writer || (rrl->rr_writer_wanted &&
172 175 refcount_is_zero(&rrl->rr_anon_rcount) &&
173 176 rrn_find(rrl) == NULL))
174 177 cv_wait(&rrl->rr_cv, &rrl->rr_lock);
175 178
176 179 if (rrl->rr_writer_wanted) {
177 180 /* may or may not be a re-entrant enter */
178 181 rrn_add(rrl);
179 182 (void) refcount_add(&rrl->rr_linked_rcount, tag);
180 183 } else {
181 184 (void) refcount_add(&rrl->rr_anon_rcount, tag);
182 185 }
183 186 ASSERT(rrl->rr_writer == NULL);
184 187 mutex_exit(&rrl->rr_lock);
185 188 }
186 189
187 190 static void
188 191 rrw_enter_write(rrwlock_t *rrl)
189 192 {
190 193 mutex_enter(&rrl->rr_lock);
191 194 ASSERT(rrl->rr_writer != curthread);
192 195
193 196 while (refcount_count(&rrl->rr_anon_rcount) > 0 ||
194 197 refcount_count(&rrl->rr_linked_rcount) > 0 ||
195 198 rrl->rr_writer != NULL) {
196 199 rrl->rr_writer_wanted = B_TRUE;
197 200 cv_wait(&rrl->rr_cv, &rrl->rr_lock);
198 201 }
199 202 rrl->rr_writer_wanted = B_FALSE;
200 203 rrl->rr_writer = curthread;
201 204 mutex_exit(&rrl->rr_lock);
202 205 }
203 206
204 207 void
205 208 rrw_enter(rrwlock_t *rrl, krw_t rw, void *tag)
206 209 {
207 210 if (rw == RW_READER)
208 211 rrw_enter_read(rrl, tag);
209 212 else
210 213 rrw_enter_write(rrl);
211 214 }
212 215
213 216 void
214 217 rrw_exit(rrwlock_t *rrl, void *tag)
215 218 {
216 219 mutex_enter(&rrl->rr_lock);
217 220 #if !defined(DEBUG) && defined(_KERNEL)
218 221 if (!rrl->rr_writer && rrl->rr_linked_rcount.rc_count == 0) {
219 222 rrl->rr_anon_rcount.rc_count--;
220 223 if (rrl->rr_anon_rcount.rc_count == 0)
221 224 cv_broadcast(&rrl->rr_cv);
222 225 mutex_exit(&rrl->rr_lock);
223 226 return;
224 227 }
225 228 DTRACE_PROBE(zfs__rrwfastpath__exitmiss);
226 229 #endif
227 230 ASSERT(!refcount_is_zero(&rrl->rr_anon_rcount) ||
228 231 !refcount_is_zero(&rrl->rr_linked_rcount) ||
229 232 rrl->rr_writer != NULL);
230 233
231 234 if (rrl->rr_writer == NULL) {
232 235 int64_t count;
233 236 if (rrn_find_and_remove(rrl))
234 237 count = refcount_remove(&rrl->rr_linked_rcount, tag);
235 238 else
236 239 count = refcount_remove(&rrl->rr_anon_rcount, tag);
237 240 if (count == 0)
238 241 cv_broadcast(&rrl->rr_cv);
239 242 } else {
240 243 ASSERT(rrl->rr_writer == curthread);
241 244 ASSERT(refcount_is_zero(&rrl->rr_anon_rcount) &&
242 245 refcount_is_zero(&rrl->rr_linked_rcount));
243 246 rrl->rr_writer = NULL;
244 247 cv_broadcast(&rrl->rr_cv);
245 248 }
246 249 mutex_exit(&rrl->rr_lock);
247 250 }
248 251
249 252 boolean_t
250 253 rrw_held(rrwlock_t *rrl, krw_t rw)
251 254 {
252 255 boolean_t held;
253 256
254 257 mutex_enter(&rrl->rr_lock);
↓ open down ↓ |
220 lines elided |
↑ open up ↑ |
255 258 if (rw == RW_WRITER) {
256 259 held = (rrl->rr_writer == curthread);
257 260 } else {
258 261 held = (!refcount_is_zero(&rrl->rr_anon_rcount) ||
259 262 !refcount_is_zero(&rrl->rr_linked_rcount));
260 263 }
261 264 mutex_exit(&rrl->rr_lock);
262 265
263 266 return (held);
264 267 }
268 +
269 +void
270 +rrw_tsd_destroy(void *arg)
271 +{
272 + rrw_node_t *rn = arg;
273 + if (rn != NULL) {
274 + panic("thread %p terminating with rrw lock %p held",
275 + (void *)curthread, (void *)rn->rn_rrl);
276 + }
277 +}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX