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