rf_stripelocks.c revision 1.23 1 1.23 oster /* $NetBSD: rf_stripelocks.c,v 1.23 2004/02/29 04:03:50 oster Exp $ */
2 1.1 oster /*
3 1.1 oster * Copyright (c) 1995 Carnegie-Mellon University.
4 1.1 oster * All rights reserved.
5 1.1 oster *
6 1.1 oster * Authors: Mark Holland, Jim Zelenka
7 1.1 oster *
8 1.1 oster * Permission to use, copy, modify and distribute this software and
9 1.1 oster * its documentation is hereby granted, provided that both the copyright
10 1.1 oster * notice and this permission notice appear in all copies of the
11 1.1 oster * software, derivative works or modified versions, and any portions
12 1.1 oster * thereof, and that both notices appear in supporting documentation.
13 1.1 oster *
14 1.1 oster * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 1.1 oster * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
16 1.1 oster * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 1.1 oster *
18 1.1 oster * Carnegie Mellon requests users of this software to return to
19 1.1 oster *
20 1.1 oster * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
21 1.1 oster * School of Computer Science
22 1.1 oster * Carnegie Mellon University
23 1.1 oster * Pittsburgh PA 15213-3890
24 1.1 oster *
25 1.1 oster * any improvements or extensions that they make and grant Carnegie the
26 1.1 oster * rights to redistribute these changes.
27 1.1 oster */
28 1.1 oster
29 1.1 oster /*
30 1.1 oster * stripelocks.c -- code to lock stripes for read and write access
31 1.1 oster *
32 1.1 oster * The code distinguishes between read locks and write locks. There can be
33 1.1 oster * as many readers to given stripe as desired. When a write request comes
34 1.1 oster * in, no further readers are allowed to enter, and all subsequent requests
35 1.1 oster * are queued in FIFO order. When a the number of readers goes to zero, the
36 1.1 oster * writer is given the lock. When a writer releases the lock, the list of
37 1.1 oster * queued requests is scanned, and all readersq up to the next writer are
38 1.1 oster * given the lock.
39 1.1 oster *
40 1.1 oster * The lock table size must be one less than a power of two, but HASH_STRIPEID
41 1.1 oster * is the only function that requires this.
42 1.1 oster *
43 1.1 oster * The code now supports "range locks". When you ask to lock a stripe, you
44 1.1 oster * specify a range of addresses in that stripe that you want to lock. When
45 1.1 oster * you acquire the lock, you've locked only this range of addresses, and
46 1.1 oster * other threads can concurrently read/write any non-overlapping portions
47 1.1 oster * of the stripe. The "addresses" that you lock are abstract in that you
48 1.1 oster * can pass in anything you like. The expectation is that you'll pass in
49 1.1 oster * the range of physical disk offsets of the parity bits you're planning
50 1.1 oster * to update. The idea behind this, of course, is to allow sub-stripe
51 1.1 oster * locking. The implementation is perhaps not the best imaginable; in the
52 1.1 oster * worst case a lock release is O(n^2) in the total number of outstanding
53 1.1 oster * requests to a given stripe. Note that if you're striping with a
54 1.1 oster * stripe unit size equal to an entire disk (i.e. not striping), there will
55 1.1 oster * be only one stripe and you may spend some significant number of cycles
56 1.1 oster * searching through stripe lock descriptors.
57 1.1 oster */
58 1.10 lukem
59 1.10 lukem #include <sys/cdefs.h>
60 1.23 oster __KERNEL_RCSID(0, "$NetBSD: rf_stripelocks.c,v 1.23 2004/02/29 04:03:50 oster Exp $");
61 1.1 oster
62 1.8 oster #include <dev/raidframe/raidframevar.h>
63 1.8 oster
64 1.1 oster #include "rf_raid.h"
65 1.1 oster #include "rf_stripelocks.h"
66 1.1 oster #include "rf_alloclist.h"
67 1.13 oster #include "rf_debugprint.h"
68 1.1 oster #include "rf_general.h"
69 1.1 oster #include "rf_driver.h"
70 1.1 oster #include "rf_shutdown.h"
71 1.1 oster
72 1.11 oster #ifdef DEBUG
73 1.11 oster
74 1.1 oster #define Dprintf1(s,a) rf_debug_printf(s,(void *)((unsigned long)a),NULL,NULL,NULL,NULL,NULL,NULL,NULL)
75 1.1 oster #define Dprintf2(s,a,b) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),NULL,NULL,NULL,NULL,NULL,NULL)
76 1.1 oster #define Dprintf3(s,a,b,c) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),NULL,NULL,NULL,NULL,NULL)
77 1.1 oster #define Dprintf4(s,a,b,c,d) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),(void *)((unsigned long)d),NULL,NULL,NULL,NULL)
78 1.1 oster #define Dprintf5(s,a,b,c,d,e) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),(void *)((unsigned long)d),(void *)((unsigned long)e),NULL,NULL,NULL)
79 1.1 oster #define Dprintf6(s,a,b,c,d,e,f) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),(void *)((unsigned long)d),(void *)((unsigned long)e),(void *)((unsigned long)f),NULL,NULL)
80 1.1 oster #define Dprintf7(s,a,b,c,d,e,f,g) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),(void *)((unsigned long)d),(void *)((unsigned long)e),(void *)((unsigned long)f),(void *)((unsigned long)g),NULL)
81 1.1 oster #define Dprintf8(s,a,b,c,d,e,f,g,h) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),(void *)((unsigned long)d),(void *)((unsigned long)e),(void *)((unsigned long)f),(void *)((unsigned long)g),(void *)((unsigned long)h))
82 1.1 oster
83 1.11 oster #else /* DEBUG */
84 1.11 oster
85 1.11 oster #define Dprintf1(s,a) {}
86 1.11 oster #define Dprintf2(s,a,b) {}
87 1.11 oster #define Dprintf3(s,a,b,c) {}
88 1.11 oster #define Dprintf4(s,a,b,c,d) {}
89 1.11 oster #define Dprintf5(s,a,b,c,d,e) {}
90 1.11 oster #define Dprintf6(s,a,b,c,d,e,f) {}
91 1.11 oster #define Dprintf7(s,a,b,c,d,e,f,g) {}
92 1.11 oster #define Dprintf8(s,a,b,c,d,e,f,g,h) {}
93 1.11 oster
94 1.11 oster #endif /* DEBUG */
95 1.11 oster
96 1.1 oster #define FLUSH
97 1.1 oster
98 1.1 oster #define HASH_STRIPEID(_sid_) ( (_sid_) & (rf_lockTableSize-1) )
99 1.1 oster
100 1.20 oster static void AddToWaitersQueue(RF_StripeLockDesc_t * lockDesc,
101 1.20 oster RF_LockReqDesc_t * lockReqDesc);
102 1.1 oster static RF_StripeLockDesc_t *AllocStripeLockDesc(RF_StripeNum_t stripeID);
103 1.3 oster static void FreeStripeLockDesc(RF_StripeLockDesc_t * p);
104 1.16 simonb static RF_LockTableEntry_t *rf_MakeLockTable(void);
105 1.13 oster #if RF_DEBUG_STRIPELOCK
106 1.3 oster static void PrintLockedStripes(RF_LockTableEntry_t * lockTable);
107 1.13 oster #endif
108 1.1 oster
109 1.20 oster /* determines if two ranges overlap. always yields false if either
110 1.20 oster start value is negative */
111 1.20 oster #define SINGLE_RANGE_OVERLAP(_strt1, _stop1, _strt2, _stop2) \
112 1.20 oster ( (_strt1 >= 0) && (_strt2 >= 0) && \
113 1.20 oster (RF_MAX(_strt1, _strt2) <= RF_MIN(_stop1, _stop2)) )
114 1.20 oster
115 1.20 oster /* determines if any of the ranges specified in the two lock
116 1.20 oster descriptors overlap each other */
117 1.20 oster
118 1.20 oster #define RANGE_OVERLAP(_cand, _pred) \
119 1.20 oster ( SINGLE_RANGE_OVERLAP((_cand)->start, (_cand)->stop, \
120 1.20 oster (_pred)->start, (_pred)->stop ) || \
121 1.20 oster SINGLE_RANGE_OVERLAP((_cand)->start2, (_cand)->stop2, \
122 1.20 oster (_pred)->start, (_pred)->stop ) || \
123 1.20 oster SINGLE_RANGE_OVERLAP((_cand)->start, (_cand)->stop, \
124 1.20 oster (_pred)->start2, (_pred)->stop2) || \
125 1.20 oster SINGLE_RANGE_OVERLAP((_cand)->start2, (_cand)->stop2, \
126 1.20 oster (_pred)->start2, (_pred)->stop2) )
127 1.1 oster
128 1.20 oster /* Determines if a candidate lock request conflicts with a predecessor
129 1.20 oster * lock req. Note that the arguments are not interchangeable.
130 1.20 oster *
131 1.1 oster * The rules are:
132 1.20 oster *
133 1.20 oster * a candidate read conflicts with a predecessor write if any
134 1.20 oster * ranges overlap
135 1.20 oster *
136 1.20 oster * a candidate write conflicts with a predecessor read if any
137 1.20 oster * ranges overlap
138 1.20 oster *
139 1.20 oster * a candidate write conflicts with a predecessor write if any
140 1.20 oster * ranges overlap */
141 1.20 oster
142 1.20 oster #define STRIPELOCK_CONFLICT(_cand, _pred) \
143 1.20 oster RANGE_OVERLAP((_cand), (_pred)) && \
144 1.20 oster ( ( (((_cand)->type == RF_IO_TYPE_READ) && \
145 1.20 oster ((_pred)->type == RF_IO_TYPE_WRITE)) || \
146 1.20 oster (((_cand)->type == RF_IO_TYPE_WRITE) && \
147 1.20 oster ((_pred)->type == RF_IO_TYPE_READ)) || \
148 1.20 oster (((_cand)->type == RF_IO_TYPE_WRITE) && \
149 1.20 oster ((_pred)->type == RF_IO_TYPE_WRITE)) \
150 1.20 oster ) \
151 1.20 oster )
152 1.1 oster
153 1.17 oster static struct pool rf_stripelock_pool;
154 1.1 oster #define RF_MAX_FREE_STRIPELOCK 128
155 1.1 oster #define RF_STRIPELOCK_INC 8
156 1.1 oster #define RF_STRIPELOCK_INITIAL 32
157 1.1 oster
158 1.16 simonb static void rf_ShutdownStripeLocks(RF_LockTableEntry_t * lockTable);
159 1.1 oster static void rf_ShutdownStripeLockFreeList(void *);
160 1.1 oster static void rf_RaidShutdownStripeLocks(void *);
161 1.1 oster
162 1.3 oster static void
163 1.21 oster rf_ShutdownStripeLockFreeList(void *ignored)
164 1.1 oster {
165 1.17 oster pool_destroy(&rf_stripelock_pool);
166 1.1 oster }
167 1.1 oster
168 1.3 oster int
169 1.21 oster rf_ConfigureStripeLockFreeList(RF_ShutdownList_t **listp)
170 1.1 oster {
171 1.1 oster unsigned mask;
172 1.1 oster
173 1.17 oster pool_init(&rf_stripelock_pool, sizeof(RF_StripeLockDesc_t),
174 1.17 oster 0, 0, 0, "rf_stripelock_pl", NULL);
175 1.17 oster pool_sethiwat(&rf_stripelock_pool, RF_MAX_FREE_STRIPELOCK);
176 1.17 oster pool_prime(&rf_stripelock_pool, RF_STRIPELOCK_INITIAL);
177 1.17 oster
178 1.23 oster rf_ShutdownCreate(listp, rf_ShutdownStripeLockFreeList, NULL);
179 1.17 oster
180 1.3 oster for (mask = 0x1; mask; mask <<= 1)
181 1.3 oster if (rf_lockTableSize == mask)
182 1.1 oster break;
183 1.1 oster if (!mask) {
184 1.3 oster printf("[WARNING: lock table size must be a power of two. Setting to %d.]\n", RF_DEFAULT_LOCK_TABLE_SIZE);
185 1.1 oster rf_lockTableSize = RF_DEFAULT_LOCK_TABLE_SIZE;
186 1.1 oster }
187 1.3 oster return (0);
188 1.1 oster }
189 1.1 oster
190 1.16 simonb static RF_LockTableEntry_t *
191 1.3 oster rf_MakeLockTable()
192 1.1 oster {
193 1.1 oster RF_LockTableEntry_t *lockTable;
194 1.18 oster int i;
195 1.1 oster
196 1.17 oster RF_Malloc(lockTable,
197 1.17 oster ((int) rf_lockTableSize) * sizeof(RF_LockTableEntry_t),
198 1.17 oster (RF_LockTableEntry_t *));
199 1.1 oster if (lockTable == NULL)
200 1.3 oster return (NULL);
201 1.3 oster for (i = 0; i < rf_lockTableSize; i++) {
202 1.18 oster rf_mutex_init(&lockTable[i].mutex);
203 1.1 oster }
204 1.3 oster return (lockTable);
205 1.1 oster }
206 1.1 oster
207 1.16 simonb static void
208 1.3 oster rf_ShutdownStripeLocks(RF_LockTableEntry_t * lockTable)
209 1.1 oster {
210 1.1 oster
211 1.13 oster #if RF_DEBUG_STRIPELOCK
212 1.1 oster if (rf_stripeLockDebug) {
213 1.1 oster PrintLockedStripes(lockTable);
214 1.1 oster }
215 1.13 oster #endif
216 1.3 oster RF_Free(lockTable, rf_lockTableSize * sizeof(RF_LockTableEntry_t));
217 1.1 oster }
218 1.1 oster
219 1.3 oster static void
220 1.21 oster rf_RaidShutdownStripeLocks(void *arg)
221 1.1 oster {
222 1.3 oster RF_Raid_t *raidPtr = (RF_Raid_t *) arg;
223 1.1 oster rf_ShutdownStripeLocks(raidPtr->lockTable);
224 1.1 oster }
225 1.1 oster
226 1.3 oster int
227 1.21 oster rf_ConfigureStripeLocks(RF_ShutdownList_t **listp, RF_Raid_t *raidPtr,
228 1.21 oster RF_Config_t *cfgPtr)
229 1.1 oster {
230 1.1 oster
231 1.1 oster raidPtr->lockTable = rf_MakeLockTable();
232 1.1 oster if (raidPtr->lockTable == NULL)
233 1.3 oster return (ENOMEM);
234 1.23 oster rf_ShutdownCreate(listp, rf_RaidShutdownStripeLocks, raidPtr);
235 1.23 oster
236 1.3 oster return (0);
237 1.1 oster }
238 1.1 oster /* returns 0 if you've got the lock, and non-zero if you have to wait.
239 1.1 oster * if and only if you have to wait, we'll cause cbFunc to get invoked
240 1.20 oster * with cbArg when you are granted the lock. We store a tag in
241 1.20 oster * *releaseTag that you need to give back to us when you release the
242 1.20 oster * lock. */
243 1.3 oster int
244 1.21 oster rf_AcquireStripeLock(RF_LockTableEntry_t *lockTable, RF_StripeNum_t stripeID,
245 1.21 oster RF_LockReqDesc_t *lockReqDesc)
246 1.3 oster {
247 1.3 oster RF_StripeLockDesc_t *lockDesc;
248 1.22 oster RF_StripeLockDesc_t *newlockDesc;
249 1.3 oster RF_LockReqDesc_t *p;
250 1.13 oster #if defined(DEBUG) && (RF_DEBUG_STRIPELOCK > 0)
251 1.12 oster int tid = 0;
252 1.11 oster #endif
253 1.11 oster int hashval = HASH_STRIPEID(stripeID);
254 1.3 oster int retcode = 0;
255 1.3 oster
256 1.3 oster RF_ASSERT(RF_IO_IS_R_OR_W(lockReqDesc->type));
257 1.3 oster
258 1.13 oster #if RF_DEBUG_STRIPELOCK
259 1.1 oster if (rf_stripeLockDebug) {
260 1.11 oster if (stripeID == -1) {
261 1.3 oster Dprintf1("[%d] Lock acquisition supressed (stripeID == -1)\n", tid);
262 1.11 oster } else {
263 1.3 oster Dprintf8("[%d] Trying to acquire stripe lock table 0x%lx SID %ld type %c range %ld-%ld, range2 %ld-%ld hashval %d\n",
264 1.3 oster tid, (unsigned long) lockTable, stripeID, lockReqDesc->type, lockReqDesc->start,
265 1.3 oster lockReqDesc->stop, lockReqDesc->start2, lockReqDesc->stop2);
266 1.3 oster Dprintf3("[%d] lock %ld hashval %d\n", tid, stripeID, hashval);
267 1.3 oster FLUSH;
268 1.3 oster }
269 1.3 oster }
270 1.13 oster #endif
271 1.3 oster if (stripeID == -1)
272 1.3 oster return (0);
273 1.3 oster lockReqDesc->next = NULL; /* just to be sure */
274 1.22 oster newlockDesc = AllocStripeLockDesc(stripeID);
275 1.3 oster
276 1.3 oster RF_LOCK_MUTEX(lockTable[hashval].mutex);
277 1.20 oster for (lockDesc = lockTable[hashval].descList; lockDesc;
278 1.20 oster lockDesc = lockDesc->next) {
279 1.3 oster if (lockDesc->stripeID == stripeID)
280 1.3 oster break;
281 1.3 oster }
282 1.3 oster
283 1.20 oster if (!lockDesc) {
284 1.20 oster /* no entry in table => no one reading or writing */
285 1.22 oster lockDesc = newlockDesc;
286 1.3 oster lockDesc->next = lockTable[hashval].descList;
287 1.3 oster lockTable[hashval].descList = lockDesc;
288 1.3 oster if (lockReqDesc->type == RF_IO_TYPE_WRITE)
289 1.3 oster lockDesc->nWriters++;
290 1.3 oster lockDesc->granted = lockReqDesc;
291 1.13 oster #if RF_DEBUG_STRIPELOCK
292 1.3 oster if (rf_stripeLockDebug) {
293 1.3 oster Dprintf7("[%d] no one waiting: lock %ld %c %ld-%ld %ld-%ld granted\n",
294 1.3 oster tid, stripeID, lockReqDesc->type, lockReqDesc->start, lockReqDesc->stop, lockReqDesc->start2, lockReqDesc->stop2);
295 1.3 oster FLUSH;
296 1.3 oster }
297 1.13 oster #endif
298 1.3 oster } else {
299 1.22 oster /* we won't be needing newlockDesc after all.. pity.. */
300 1.22 oster FreeStripeLockDesc(newlockDesc);
301 1.3 oster
302 1.3 oster if (lockReqDesc->type == RF_IO_TYPE_WRITE)
303 1.3 oster lockDesc->nWriters++;
304 1.3 oster
305 1.20 oster if (lockDesc->nWriters == 0) {
306 1.20 oster /* no need to search any lists if there are no
307 1.20 oster * writers anywhere */
308 1.3 oster lockReqDesc->next = lockDesc->granted;
309 1.3 oster lockDesc->granted = lockReqDesc;
310 1.13 oster #if RF_DEBUG_STRIPELOCK
311 1.3 oster if (rf_stripeLockDebug) {
312 1.3 oster Dprintf7("[%d] no writers: lock %ld %c %ld-%ld %ld-%ld granted\n",
313 1.3 oster tid, stripeID, lockReqDesc->type, lockReqDesc->start, lockReqDesc->stop, lockReqDesc->start2, lockReqDesc->stop2);
314 1.3 oster FLUSH;
315 1.3 oster }
316 1.13 oster #endif
317 1.3 oster } else {
318 1.3 oster
319 1.20 oster /* search the granted & waiting lists for a
320 1.20 oster * conflict. stop searching as soon as we
321 1.20 oster * find one */
322 1.3 oster retcode = 0;
323 1.3 oster for (p = lockDesc->granted; p; p = p->next)
324 1.3 oster if (STRIPELOCK_CONFLICT(lockReqDesc, p)) {
325 1.3 oster retcode = 1;
326 1.3 oster break;
327 1.3 oster }
328 1.3 oster if (!retcode)
329 1.3 oster for (p = lockDesc->waitersH; p; p = p->next)
330 1.3 oster if (STRIPELOCK_CONFLICT(lockReqDesc, p)) {
331 1.3 oster retcode = 2;
332 1.3 oster break;
333 1.3 oster }
334 1.3 oster if (!retcode) {
335 1.20 oster /* no conflicts found => grant lock */
336 1.20 oster lockReqDesc->next = lockDesc->granted;
337 1.3 oster lockDesc->granted = lockReqDesc;
338 1.13 oster #if RF_DEBUG_STRIPELOCK
339 1.3 oster if (rf_stripeLockDebug) {
340 1.3 oster Dprintf7("[%d] no conflicts: lock %ld %c %ld-%ld %ld-%ld granted\n",
341 1.3 oster tid, stripeID, lockReqDesc->type, lockReqDesc->start, lockReqDesc->stop,
342 1.3 oster lockReqDesc->start2, lockReqDesc->stop2);
343 1.3 oster FLUSH;
344 1.3 oster }
345 1.13 oster #endif
346 1.3 oster } else {
347 1.13 oster #if RF_DEBUG_STRIPELOCK
348 1.3 oster if (rf_stripeLockDebug) {
349 1.3 oster Dprintf6("[%d] conflict: lock %ld %c %ld-%ld hashval=%d not granted\n",
350 1.3 oster tid, stripeID, lockReqDesc->type, lockReqDesc->start, lockReqDesc->stop,
351 1.3 oster hashval);
352 1.3 oster Dprintf3("[%d] lock %ld retcode=%d\n", tid, stripeID, retcode);
353 1.3 oster FLUSH;
354 1.3 oster }
355 1.13 oster #endif
356 1.15 oster AddToWaitersQueue(lockDesc, lockReqDesc);
357 1.15 oster /* conflict => the current access must wait */
358 1.3 oster }
359 1.3 oster }
360 1.1 oster }
361 1.3 oster
362 1.3 oster RF_UNLOCK_MUTEX(lockTable[hashval].mutex);
363 1.3 oster return (retcode);
364 1.3 oster }
365 1.3 oster
366 1.3 oster void
367 1.21 oster rf_ReleaseStripeLock(RF_LockTableEntry_t *lockTable, RF_StripeNum_t stripeID,
368 1.21 oster RF_LockReqDesc_t *lockReqDesc)
369 1.3 oster {
370 1.3 oster RF_StripeLockDesc_t *lockDesc, *ld_t;
371 1.3 oster RF_LockReqDesc_t *lr, *lr_t, *callbacklist, *t;
372 1.13 oster #if defined(DEBUG) && (RF_DEBUG_STRIPELOCK > 0)
373 1.12 oster int tid = 0;
374 1.11 oster #endif
375 1.11 oster int hashval = HASH_STRIPEID(stripeID);
376 1.3 oster int release_it, consider_it;
377 1.3 oster RF_LockReqDesc_t *candidate, *candidate_t, *predecessor;
378 1.3 oster
379 1.7 thorpej RF_ASSERT(RF_IO_IS_R_OR_W(lockReqDesc->type));
380 1.3 oster
381 1.13 oster #if RF_DEBUG_STRIPELOCK
382 1.1 oster if (rf_stripeLockDebug) {
383 1.11 oster if (stripeID == -1) {
384 1.3 oster Dprintf1("[%d] Lock release supressed (stripeID == -1)\n", tid);
385 1.11 oster } else {
386 1.3 oster Dprintf8("[%d] Releasing stripe lock on stripe ID %ld, type %c range %ld-%ld %ld-%ld table 0x%lx\n",
387 1.3 oster tid, stripeID, lockReqDesc->type, lockReqDesc->start, lockReqDesc->stop, lockReqDesc->start2, lockReqDesc->stop2, lockTable);
388 1.3 oster FLUSH;
389 1.3 oster }
390 1.3 oster }
391 1.13 oster #endif
392 1.3 oster if (stripeID == -1)
393 1.3 oster return;
394 1.3 oster
395 1.3 oster RF_LOCK_MUTEX(lockTable[hashval].mutex);
396 1.3 oster
397 1.3 oster /* find the stripe lock descriptor */
398 1.20 oster for (ld_t = NULL, lockDesc = lockTable[hashval].descList;
399 1.20 oster lockDesc; ld_t = lockDesc, lockDesc = lockDesc->next) {
400 1.3 oster if (lockDesc->stripeID == stripeID)
401 1.3 oster break;
402 1.3 oster }
403 1.3 oster RF_ASSERT(lockDesc); /* major error to release a lock that doesn't
404 1.3 oster * exist */
405 1.3 oster
406 1.3 oster /* find the stripe lock request descriptor & delete it from the list */
407 1.3 oster for (lr_t = NULL, lr = lockDesc->granted; lr; lr_t = lr, lr = lr->next)
408 1.3 oster if (lr == lockReqDesc)
409 1.3 oster break;
410 1.3 oster
411 1.3 oster RF_ASSERT(lr && (lr == lockReqDesc)); /* major error to release a
412 1.3 oster * lock that hasn't been
413 1.3 oster * granted */
414 1.3 oster if (lr_t)
415 1.3 oster lr_t->next = lr->next;
416 1.3 oster else {
417 1.3 oster RF_ASSERT(lr == lockDesc->granted);
418 1.3 oster lockDesc->granted = lr->next;
419 1.3 oster }
420 1.3 oster lr->next = NULL;
421 1.3 oster
422 1.3 oster if (lockReqDesc->type == RF_IO_TYPE_WRITE)
423 1.3 oster lockDesc->nWriters--;
424 1.3 oster
425 1.20 oster /* search through the waiters list to see if anyone needs to
426 1.20 oster * be woken up. for each such descriptor in the wait list, we
427 1.20 oster * check it against everything granted and against everything
428 1.20 oster * _in front_ of it in the waiters queue. If it conflicts
429 1.20 oster * with none of these, we release it.
430 1.3 oster *
431 1.20 oster * DON'T TOUCH THE TEMPLINK POINTER OF ANYTHING IN THE GRANTED
432 1.20 oster * LIST HERE.
433 1.20 oster *
434 1.20 oster * This will roach the case where the callback tries to
435 1.20 oster * acquire a new lock in the same stripe. There are some
436 1.20 oster * asserts to try and detect this.
437 1.3 oster *
438 1.20 oster * We apply 2 performance optimizations: (1) if releasing this
439 1.20 oster * lock results in no more writers to this stripe, we just
440 1.20 oster * release everybody waiting, since we place no restrictions
441 1.20 oster * on the number of concurrent reads. (2) we consider as
442 1.20 oster * candidates for wakeup only those waiters that have a range
443 1.20 oster * overlap with either the descriptor being woken up or with
444 1.20 oster * something in the callbacklist (i.e. something we've just
445 1.20 oster * now woken up). This allows us to avoid the long evaluation
446 1.20 oster * for some descriptors. */
447 1.3 oster
448 1.3 oster callbacklist = NULL;
449 1.3 oster if (lockDesc->nWriters == 0) { /* performance tweak (1) */
450 1.3 oster while (lockDesc->waitersH) {
451 1.20 oster /* delete from waiters list */
452 1.20 oster lr = lockDesc->waitersH;
453 1.3 oster lockDesc->waitersH = lr->next;
454 1.3 oster
455 1.3 oster RF_ASSERT(lr->type == RF_IO_TYPE_READ);
456 1.3 oster
457 1.20 oster /* add to granted list */
458 1.20 oster lr->next = lockDesc->granted;
459 1.3 oster lockDesc->granted = lr;
460 1.3 oster
461 1.3 oster RF_ASSERT(!lr->templink);
462 1.20 oster /* put on callback list so that we'll invoke
463 1.20 oster callback below */
464 1.20 oster lr->templink = callbacklist;
465 1.3 oster callbacklist = lr;
466 1.13 oster #if RF_DEBUG_STRIPELOCK
467 1.3 oster if (rf_stripeLockDebug) {
468 1.3 oster Dprintf8("[%d] No writers: granting lock stripe ID %ld, type %c range %ld-%ld %ld-%ld table 0x%lx\n",
469 1.3 oster tid, stripeID, lr->type, lr->start, lr->stop, lr->start2, lr->stop2, (unsigned long) lockTable);
470 1.3 oster FLUSH;
471 1.3 oster }
472 1.13 oster #endif
473 1.3 oster }
474 1.20 oster lockDesc->waitersT = NULL;
475 1.20 oster /* we've purged the whole waiters list */
476 1.3 oster
477 1.3 oster } else
478 1.20 oster for (candidate_t = NULL, candidate = lockDesc->waitersH;
479 1.20 oster candidate;) {
480 1.3 oster
481 1.3 oster /* performance tweak (2) */
482 1.3 oster consider_it = 0;
483 1.3 oster if (RANGE_OVERLAP(lockReqDesc, candidate))
484 1.3 oster consider_it = 1;
485 1.3 oster else
486 1.3 oster for (t = callbacklist; t; t = t->templink)
487 1.3 oster if (RANGE_OVERLAP(t, candidate)) {
488 1.3 oster consider_it = 1;
489 1.3 oster break;
490 1.3 oster }
491 1.3 oster if (!consider_it) {
492 1.13 oster #if RF_DEBUG_STRIPELOCK
493 1.3 oster if (rf_stripeLockDebug) {
494 1.3 oster Dprintf8("[%d] No overlap: rejecting candidate stripeID %ld, type %c range %ld-%ld %ld-%ld table 0x%lx\n",
495 1.3 oster tid, stripeID, candidate->type, candidate->start, candidate->stop, candidate->start2, candidate->stop2,
496 1.3 oster (unsigned long) lockTable);
497 1.3 oster FLUSH;
498 1.3 oster }
499 1.13 oster #endif
500 1.3 oster candidate_t = candidate;
501 1.3 oster candidate = candidate->next;
502 1.3 oster continue;
503 1.3 oster }
504 1.20 oster /* we have a candidate for release. check to
505 1.20 oster * make sure it is not blocked by any granted
506 1.20 oster * locks */
507 1.3 oster release_it = 1;
508 1.20 oster for (predecessor = lockDesc->granted; predecessor;
509 1.20 oster predecessor = predecessor->next) {
510 1.20 oster if (STRIPELOCK_CONFLICT(candidate,
511 1.20 oster predecessor)) {
512 1.13 oster #if RF_DEBUG_STRIPELOCK
513 1.3 oster if (rf_stripeLockDebug) {
514 1.3 oster Dprintf8("[%d] Conflicts with granted lock: rejecting candidate stripeID %ld, type %c range %ld-%ld %ld-%ld table 0x%lx\n",
515 1.3 oster tid, stripeID, candidate->type, candidate->start, candidate->stop, candidate->start2, candidate->stop2,
516 1.3 oster (unsigned long) lockTable);
517 1.3 oster FLUSH;
518 1.3 oster }
519 1.13 oster #endif
520 1.3 oster release_it = 0;
521 1.3 oster break;
522 1.3 oster }
523 1.3 oster }
524 1.3 oster
525 1.20 oster /* now check to see if the candidate is
526 1.20 oster * blocked by any waiters that occur before it
527 1.20 oster * it the wait queue */
528 1.3 oster if (release_it)
529 1.20 oster for (predecessor = lockDesc->waitersH;
530 1.20 oster predecessor != candidate;
531 1.20 oster predecessor = predecessor->next) {
532 1.20 oster if (STRIPELOCK_CONFLICT(candidate,
533 1.20 oster predecessor)) {
534 1.13 oster #if RF_DEBUG_STRIPELOCK
535 1.3 oster if (rf_stripeLockDebug) {
536 1.3 oster Dprintf8("[%d] Conflicts with waiting lock: rejecting candidate stripeID %ld, type %c range %ld-%ld %ld-%ld table 0x%lx\n",
537 1.3 oster tid, stripeID, candidate->type, candidate->start, candidate->stop, candidate->start2, candidate->stop2,
538 1.3 oster (unsigned long) lockTable);
539 1.3 oster FLUSH;
540 1.3 oster }
541 1.13 oster #endif
542 1.3 oster release_it = 0;
543 1.3 oster break;
544 1.3 oster }
545 1.3 oster }
546 1.3 oster
547 1.3 oster /* release it if indicated */
548 1.3 oster if (release_it) {
549 1.13 oster #if RF_DEBUG_STRIPELOCK
550 1.3 oster if (rf_stripeLockDebug) {
551 1.3 oster Dprintf8("[%d] Granting lock to candidate stripeID %ld, type %c range %ld-%ld %ld-%ld table 0x%lx\n",
552 1.3 oster tid, stripeID, candidate->type, candidate->start, candidate->stop, candidate->start2, candidate->stop2,
553 1.3 oster (unsigned long) lockTable);
554 1.3 oster FLUSH;
555 1.3 oster }
556 1.13 oster #endif
557 1.3 oster if (candidate_t) {
558 1.3 oster candidate_t->next = candidate->next;
559 1.3 oster if (lockDesc->waitersT == candidate)
560 1.15 oster lockDesc->waitersT = candidate_t; /* cannot be waitersH since candidate_t is not NULL */
561 1.3 oster } else {
562 1.3 oster RF_ASSERT(candidate == lockDesc->waitersH);
563 1.3 oster lockDesc->waitersH = lockDesc->waitersH->next;
564 1.3 oster if (!lockDesc->waitersH)
565 1.3 oster lockDesc->waitersT = NULL;
566 1.3 oster }
567 1.20 oster /* move it to the granted list */
568 1.20 oster candidate->next = lockDesc->granted;
569 1.3 oster lockDesc->granted = candidate;
570 1.3 oster
571 1.3 oster RF_ASSERT(!candidate->templink);
572 1.20 oster /* put it on the list of things to be
573 1.20 oster called after we release the mutex */
574 1.20 oster candidate->templink = callbacklist;
575 1.20 oster
576 1.3 oster callbacklist = candidate;
577 1.3 oster
578 1.3 oster if (!candidate_t)
579 1.3 oster candidate = lockDesc->waitersH;
580 1.3 oster else
581 1.20 oster candidate = candidate_t->next;
582 1.20 oster /* continue with the rest of the list */
583 1.3 oster } else {
584 1.3 oster candidate_t = candidate;
585 1.20 oster /* continue with the rest of the list */
586 1.20 oster candidate = candidate->next;
587 1.3 oster }
588 1.3 oster }
589 1.3 oster
590 1.3 oster /* delete the descriptor if no one is waiting or active */
591 1.3 oster if (!lockDesc->granted && !lockDesc->waitersH) {
592 1.3 oster RF_ASSERT(lockDesc->nWriters == 0);
593 1.13 oster #if RF_DEBUG_STRIPELOCK
594 1.3 oster if (rf_stripeLockDebug) {
595 1.3 oster Dprintf3("[%d] Last lock released (table 0x%lx): deleting desc for stripeID %ld\n", tid, (unsigned long) lockTable, stripeID);
596 1.3 oster FLUSH;
597 1.3 oster }
598 1.13 oster #endif
599 1.3 oster if (ld_t)
600 1.3 oster ld_t->next = lockDesc->next;
601 1.3 oster else {
602 1.3 oster RF_ASSERT(lockDesc == lockTable[hashval].descList);
603 1.3 oster lockTable[hashval].descList = lockDesc->next;
604 1.3 oster }
605 1.3 oster FreeStripeLockDesc(lockDesc);
606 1.3 oster lockDesc = NULL;/* only for the ASSERT below */
607 1.3 oster }
608 1.3 oster RF_UNLOCK_MUTEX(lockTable[hashval].mutex);
609 1.3 oster
610 1.20 oster /* now that we've unlocked the mutex, invoke the callback on
611 1.20 oster * all the descriptors in the list */
612 1.20 oster
613 1.20 oster /* if we deleted the descriptor, we should have no callbacks
614 1.20 oster * to do */
615 1.20 oster RF_ASSERT(!((callbacklist) && (!lockDesc)));
616 1.3 oster for (candidate = callbacklist; candidate;) {
617 1.3 oster t = candidate;
618 1.3 oster candidate = candidate->templink;
619 1.3 oster t->templink = NULL;
620 1.3 oster (t->cbFunc) (t->cbArg);
621 1.1 oster }
622 1.1 oster }
623 1.3 oster /* must have the indicated lock table mutex upon entry */
624 1.3 oster static void
625 1.21 oster AddToWaitersQueue(RF_StripeLockDesc_t *lockDesc, RF_LockReqDesc_t *lockReqDesc)
626 1.3 oster {
627 1.3 oster if (!lockDesc->waitersH) {
628 1.3 oster lockDesc->waitersH = lockDesc->waitersT = lockReqDesc;
629 1.3 oster } else {
630 1.3 oster lockDesc->waitersT->next = lockReqDesc;
631 1.3 oster lockDesc->waitersT = lockReqDesc;
632 1.3 oster }
633 1.1 oster }
634 1.1 oster
635 1.3 oster static RF_StripeLockDesc_t *
636 1.3 oster AllocStripeLockDesc(RF_StripeNum_t stripeID)
637 1.1 oster {
638 1.1 oster RF_StripeLockDesc_t *p;
639 1.1 oster
640 1.17 oster p = pool_get(&rf_stripelock_pool, PR_WAITOK);
641 1.1 oster if (p) {
642 1.1 oster p->stripeID = stripeID;
643 1.17 oster p->granted = NULL;
644 1.17 oster p->waitersH = NULL;
645 1.17 oster p->waitersT = NULL;
646 1.17 oster p->nWriters = 0;
647 1.17 oster p->next = NULL;
648 1.1 oster }
649 1.3 oster return (p);
650 1.1 oster }
651 1.1 oster
652 1.3 oster static void
653 1.21 oster FreeStripeLockDesc(RF_StripeLockDesc_t *p)
654 1.1 oster {
655 1.17 oster pool_put(&rf_stripelock_pool, p);
656 1.1 oster }
657 1.1 oster
658 1.13 oster #if RF_DEBUG_STRIPELOCK
659 1.3 oster static void
660 1.21 oster PrintLockedStripes(RF_LockTableEntry_t *lockTable)
661 1.3 oster {
662 1.3 oster int i, j, foundone = 0, did;
663 1.3 oster RF_StripeLockDesc_t *p;
664 1.3 oster RF_LockReqDesc_t *q;
665 1.3 oster
666 1.3 oster RF_LOCK_MUTEX(rf_printf_mutex);
667 1.3 oster printf("Locked stripes:\n");
668 1.3 oster for (i = 0; i < rf_lockTableSize; i++)
669 1.3 oster if (lockTable[i].descList) {
670 1.3 oster foundone = 1;
671 1.3 oster for (p = lockTable[i].descList; p; p = p->next) {
672 1.3 oster printf("Stripe ID 0x%lx (%d) nWriters %d\n",
673 1.20 oster (long) p->stripeID, (int) p->stripeID,
674 1.20 oster p->nWriters);
675 1.3 oster
676 1.3 oster if (!(p->granted))
677 1.3 oster printf("Granted: (none)\n");
678 1.3 oster else
679 1.3 oster printf("Granted:\n");
680 1.20 oster for (did = 1, j = 0, q = p->granted; q;
681 1.20 oster j++, q = q->next) {
682 1.3 oster printf(" %c(%ld-%ld", q->type, (long) q->start, (long) q->stop);
683 1.3 oster if (q->start2 != -1)
684 1.3 oster printf(",%ld-%ld) ", (long) q->start2,
685 1.3 oster (long) q->stop2);
686 1.3 oster else
687 1.3 oster printf(") ");
688 1.3 oster if (j && !(j % 4)) {
689 1.3 oster printf("\n");
690 1.3 oster did = 1;
691 1.3 oster } else
692 1.3 oster did = 0;
693 1.3 oster }
694 1.3 oster if (!did)
695 1.3 oster printf("\n");
696 1.3 oster
697 1.3 oster if (!(p->waitersH))
698 1.3 oster printf("Waiting: (none)\n");
699 1.3 oster else
700 1.3 oster printf("Waiting:\n");
701 1.20 oster for (did = 1, j = 0, q = p->waitersH; q;
702 1.20 oster j++, q = q->next) {
703 1.3 oster printf("%c(%ld-%ld", q->type, (long) q->start, (long) q->stop);
704 1.3 oster if (q->start2 != -1)
705 1.3 oster printf(",%ld-%ld) ", (long) q->start2, (long) q->stop2);
706 1.3 oster else
707 1.3 oster printf(") ");
708 1.3 oster if (j && !(j % 4)) {
709 1.3 oster printf("\n ");
710 1.3 oster did = 1;
711 1.3 oster } else
712 1.3 oster did = 0;
713 1.3 oster }
714 1.3 oster if (!did)
715 1.3 oster printf("\n");
716 1.3 oster }
717 1.3 oster }
718 1.3 oster if (!foundone)
719 1.3 oster printf("(none)\n");
720 1.3 oster else
721 1.3 oster printf("\n");
722 1.3 oster RF_UNLOCK_MUTEX(rf_printf_mutex);
723 1.1 oster }
724 1.13 oster #endif
725