rf_stripelocks.c revision 1.21 1 /* $NetBSD: rf_stripelocks.c,v 1.21 2003/12/30 21:59:03 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.21 2003/12/30 21:59:03 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,
101 RF_LockReqDesc_t * lockReqDesc);
102 static RF_StripeLockDesc_t *AllocStripeLockDesc(RF_StripeNum_t stripeID);
103 static void FreeStripeLockDesc(RF_StripeLockDesc_t * p);
104 static RF_LockTableEntry_t *rf_MakeLockTable(void);
105 #if RF_DEBUG_STRIPELOCK
106 static void PrintLockedStripes(RF_LockTableEntry_t * lockTable);
107 #endif
108
109 /* determines if two ranges overlap. always yields false if either
110 start value is negative */
111 #define SINGLE_RANGE_OVERLAP(_strt1, _stop1, _strt2, _stop2) \
112 ( (_strt1 >= 0) && (_strt2 >= 0) && \
113 (RF_MAX(_strt1, _strt2) <= RF_MIN(_stop1, _stop2)) )
114
115 /* determines if any of the ranges specified in the two lock
116 descriptors overlap each other */
117
118 #define RANGE_OVERLAP(_cand, _pred) \
119 ( SINGLE_RANGE_OVERLAP((_cand)->start, (_cand)->stop, \
120 (_pred)->start, (_pred)->stop ) || \
121 SINGLE_RANGE_OVERLAP((_cand)->start2, (_cand)->stop2, \
122 (_pred)->start, (_pred)->stop ) || \
123 SINGLE_RANGE_OVERLAP((_cand)->start, (_cand)->stop, \
124 (_pred)->start2, (_pred)->stop2) || \
125 SINGLE_RANGE_OVERLAP((_cand)->start2, (_cand)->stop2, \
126 (_pred)->start2, (_pred)->stop2) )
127
128 /* Determines if a candidate lock request conflicts with a predecessor
129 * lock req. Note that the arguments are not interchangeable.
130 *
131 * The rules are:
132 *
133 * a candidate read conflicts with a predecessor write if any
134 * ranges overlap
135 *
136 * a candidate write conflicts with a predecessor read if any
137 * ranges overlap
138 *
139 * a candidate write conflicts with a predecessor write if any
140 * ranges overlap */
141
142 #define STRIPELOCK_CONFLICT(_cand, _pred) \
143 RANGE_OVERLAP((_cand), (_pred)) && \
144 ( ( (((_cand)->type == RF_IO_TYPE_READ) && \
145 ((_pred)->type == RF_IO_TYPE_WRITE)) || \
146 (((_cand)->type == RF_IO_TYPE_WRITE) && \
147 ((_pred)->type == RF_IO_TYPE_READ)) || \
148 (((_cand)->type == RF_IO_TYPE_WRITE) && \
149 ((_pred)->type == RF_IO_TYPE_WRITE)) \
150 ) \
151 )
152
153 static struct pool rf_stripelock_pool;
154 #define RF_MAX_FREE_STRIPELOCK 128
155 #define RF_STRIPELOCK_INC 8
156 #define RF_STRIPELOCK_INITIAL 32
157
158 static void rf_ShutdownStripeLocks(RF_LockTableEntry_t * lockTable);
159 static void rf_ShutdownStripeLockFreeList(void *);
160 static void rf_RaidShutdownStripeLocks(void *);
161
162 static void
163 rf_ShutdownStripeLockFreeList(void *ignored)
164 {
165 pool_destroy(&rf_stripelock_pool);
166 }
167
168 int
169 rf_ConfigureStripeLockFreeList(RF_ShutdownList_t **listp)
170 {
171 unsigned mask;
172 int rc;
173
174 pool_init(&rf_stripelock_pool, sizeof(RF_StripeLockDesc_t),
175 0, 0, 0, "rf_stripelock_pl", NULL);
176 pool_sethiwat(&rf_stripelock_pool, RF_MAX_FREE_STRIPELOCK);
177 pool_prime(&rf_stripelock_pool, RF_STRIPELOCK_INITIAL);
178
179 rc = rf_ShutdownCreate(listp, rf_ShutdownStripeLockFreeList, NULL);
180 if (rc) {
181 rf_print_unable_to_add_shutdown(__FILE__, __LINE__, rc);
182 rf_ShutdownStripeLockFreeList(NULL);
183 return (rc);
184 }
185
186 for (mask = 0x1; mask; mask <<= 1)
187 if (rf_lockTableSize == mask)
188 break;
189 if (!mask) {
190 printf("[WARNING: lock table size must be a power of two. Setting to %d.]\n", RF_DEFAULT_LOCK_TABLE_SIZE);
191 rf_lockTableSize = RF_DEFAULT_LOCK_TABLE_SIZE;
192 }
193 return (0);
194 }
195
196 static RF_LockTableEntry_t *
197 rf_MakeLockTable()
198 {
199 RF_LockTableEntry_t *lockTable;
200 int i;
201
202 RF_Malloc(lockTable,
203 ((int) rf_lockTableSize) * sizeof(RF_LockTableEntry_t),
204 (RF_LockTableEntry_t *));
205 if (lockTable == NULL)
206 return (NULL);
207 for (i = 0; i < rf_lockTableSize; i++) {
208 rf_mutex_init(&lockTable[i].mutex);
209 }
210 return (lockTable);
211 }
212
213 static void
214 rf_ShutdownStripeLocks(RF_LockTableEntry_t * lockTable)
215 {
216
217 #if RF_DEBUG_STRIPELOCK
218 if (rf_stripeLockDebug) {
219 PrintLockedStripes(lockTable);
220 }
221 #endif
222 RF_Free(lockTable, rf_lockTableSize * sizeof(RF_LockTableEntry_t));
223 }
224
225 static void
226 rf_RaidShutdownStripeLocks(void *arg)
227 {
228 RF_Raid_t *raidPtr = (RF_Raid_t *) arg;
229 rf_ShutdownStripeLocks(raidPtr->lockTable);
230 }
231
232 int
233 rf_ConfigureStripeLocks(RF_ShutdownList_t **listp, RF_Raid_t *raidPtr,
234 RF_Config_t *cfgPtr)
235 {
236 int rc;
237
238 raidPtr->lockTable = rf_MakeLockTable();
239 if (raidPtr->lockTable == NULL)
240 return (ENOMEM);
241 rc = rf_ShutdownCreate(listp, rf_RaidShutdownStripeLocks, raidPtr);
242 if (rc) {
243 rf_print_unable_to_add_shutdown(__FILE__, __LINE__, rc);
244 rf_ShutdownStripeLocks(raidPtr->lockTable);
245 return (rc);
246 }
247 return (0);
248 }
249 /* returns 0 if you've got the lock, and non-zero if you have to wait.
250 * if and only if you have to wait, we'll cause cbFunc to get invoked
251 * with cbArg when you are granted the lock. We store a tag in
252 * *releaseTag that you need to give back to us when you release the
253 * lock. */
254 int
255 rf_AcquireStripeLock(RF_LockTableEntry_t *lockTable, RF_StripeNum_t stripeID,
256 RF_LockReqDesc_t *lockReqDesc)
257 {
258 RF_StripeLockDesc_t *lockDesc;
259 RF_LockReqDesc_t *p;
260 #if defined(DEBUG) && (RF_DEBUG_STRIPELOCK > 0)
261 int tid = 0;
262 #endif
263 int hashval = HASH_STRIPEID(stripeID);
264 int retcode = 0;
265
266 RF_ASSERT(RF_IO_IS_R_OR_W(lockReqDesc->type));
267
268 #if RF_DEBUG_STRIPELOCK
269 if (rf_stripeLockDebug) {
270 if (stripeID == -1) {
271 Dprintf1("[%d] Lock acquisition supressed (stripeID == -1)\n", tid);
272 } else {
273 Dprintf8("[%d] Trying to acquire stripe lock table 0x%lx SID %ld type %c range %ld-%ld, range2 %ld-%ld hashval %d\n",
274 tid, (unsigned long) lockTable, stripeID, lockReqDesc->type, lockReqDesc->start,
275 lockReqDesc->stop, lockReqDesc->start2, lockReqDesc->stop2);
276 Dprintf3("[%d] lock %ld hashval %d\n", tid, stripeID, hashval);
277 FLUSH;
278 }
279 }
280 #endif
281 if (stripeID == -1)
282 return (0);
283 lockReqDesc->next = NULL; /* just to be sure */
284
285 RF_LOCK_MUTEX(lockTable[hashval].mutex);
286 for (lockDesc = lockTable[hashval].descList; lockDesc;
287 lockDesc = lockDesc->next) {
288 if (lockDesc->stripeID == stripeID)
289 break;
290 }
291
292 if (!lockDesc) {
293 /* no entry in table => no one reading or writing */
294 lockDesc = AllocStripeLockDesc(stripeID);
295 lockDesc->next = lockTable[hashval].descList;
296 lockTable[hashval].descList = lockDesc;
297 if (lockReqDesc->type == RF_IO_TYPE_WRITE)
298 lockDesc->nWriters++;
299 lockDesc->granted = lockReqDesc;
300 #if RF_DEBUG_STRIPELOCK
301 if (rf_stripeLockDebug) {
302 Dprintf7("[%d] no one waiting: lock %ld %c %ld-%ld %ld-%ld granted\n",
303 tid, stripeID, lockReqDesc->type, lockReqDesc->start, lockReqDesc->stop, lockReqDesc->start2, lockReqDesc->stop2);
304 FLUSH;
305 }
306 #endif
307 } else {
308
309 if (lockReqDesc->type == RF_IO_TYPE_WRITE)
310 lockDesc->nWriters++;
311
312 if (lockDesc->nWriters == 0) {
313 /* no need to search any lists if there are no
314 * writers anywhere */
315 lockReqDesc->next = lockDesc->granted;
316 lockDesc->granted = lockReqDesc;
317 #if RF_DEBUG_STRIPELOCK
318 if (rf_stripeLockDebug) {
319 Dprintf7("[%d] no writers: lock %ld %c %ld-%ld %ld-%ld granted\n",
320 tid, stripeID, lockReqDesc->type, lockReqDesc->start, lockReqDesc->stop, lockReqDesc->start2, lockReqDesc->stop2);
321 FLUSH;
322 }
323 #endif
324 } else {
325
326 /* search the granted & waiting lists for a
327 * conflict. stop searching as soon as we
328 * find one */
329 retcode = 0;
330 for (p = lockDesc->granted; p; p = p->next)
331 if (STRIPELOCK_CONFLICT(lockReqDesc, p)) {
332 retcode = 1;
333 break;
334 }
335 if (!retcode)
336 for (p = lockDesc->waitersH; p; p = p->next)
337 if (STRIPELOCK_CONFLICT(lockReqDesc, p)) {
338 retcode = 2;
339 break;
340 }
341 if (!retcode) {
342 /* no conflicts found => grant lock */
343 lockReqDesc->next = lockDesc->granted;
344 lockDesc->granted = lockReqDesc;
345 #if RF_DEBUG_STRIPELOCK
346 if (rf_stripeLockDebug) {
347 Dprintf7("[%d] no conflicts: lock %ld %c %ld-%ld %ld-%ld granted\n",
348 tid, stripeID, lockReqDesc->type, lockReqDesc->start, lockReqDesc->stop,
349 lockReqDesc->start2, lockReqDesc->stop2);
350 FLUSH;
351 }
352 #endif
353 } else {
354 #if RF_DEBUG_STRIPELOCK
355 if (rf_stripeLockDebug) {
356 Dprintf6("[%d] conflict: lock %ld %c %ld-%ld hashval=%d not granted\n",
357 tid, stripeID, lockReqDesc->type, lockReqDesc->start, lockReqDesc->stop,
358 hashval);
359 Dprintf3("[%d] lock %ld retcode=%d\n", tid, stripeID, retcode);
360 FLUSH;
361 }
362 #endif
363 AddToWaitersQueue(lockDesc, lockReqDesc);
364 /* conflict => the current access must wait */
365 }
366 }
367 }
368
369 RF_UNLOCK_MUTEX(lockTable[hashval].mutex);
370 return (retcode);
371 }
372
373 void
374 rf_ReleaseStripeLock(RF_LockTableEntry_t *lockTable, RF_StripeNum_t stripeID,
375 RF_LockReqDesc_t *lockReqDesc)
376 {
377 RF_StripeLockDesc_t *lockDesc, *ld_t;
378 RF_LockReqDesc_t *lr, *lr_t, *callbacklist, *t;
379 #if defined(DEBUG) && (RF_DEBUG_STRIPELOCK > 0)
380 int tid = 0;
381 #endif
382 int hashval = HASH_STRIPEID(stripeID);
383 int release_it, consider_it;
384 RF_LockReqDesc_t *candidate, *candidate_t, *predecessor;
385
386 RF_ASSERT(RF_IO_IS_R_OR_W(lockReqDesc->type));
387
388 #if RF_DEBUG_STRIPELOCK
389 if (rf_stripeLockDebug) {
390 if (stripeID == -1) {
391 Dprintf1("[%d] Lock release supressed (stripeID == -1)\n", tid);
392 } else {
393 Dprintf8("[%d] Releasing stripe lock on stripe ID %ld, type %c range %ld-%ld %ld-%ld table 0x%lx\n",
394 tid, stripeID, lockReqDesc->type, lockReqDesc->start, lockReqDesc->stop, lockReqDesc->start2, lockReqDesc->stop2, lockTable);
395 FLUSH;
396 }
397 }
398 #endif
399 if (stripeID == -1)
400 return;
401
402 RF_LOCK_MUTEX(lockTable[hashval].mutex);
403
404 /* find the stripe lock descriptor */
405 for (ld_t = NULL, lockDesc = lockTable[hashval].descList;
406 lockDesc; ld_t = lockDesc, lockDesc = lockDesc->next) {
407 if (lockDesc->stripeID == stripeID)
408 break;
409 }
410 RF_ASSERT(lockDesc); /* major error to release a lock that doesn't
411 * exist */
412
413 /* find the stripe lock request descriptor & delete it from the list */
414 for (lr_t = NULL, lr = lockDesc->granted; lr; lr_t = lr, lr = lr->next)
415 if (lr == lockReqDesc)
416 break;
417
418 RF_ASSERT(lr && (lr == lockReqDesc)); /* major error to release a
419 * lock that hasn't been
420 * granted */
421 if (lr_t)
422 lr_t->next = lr->next;
423 else {
424 RF_ASSERT(lr == lockDesc->granted);
425 lockDesc->granted = lr->next;
426 }
427 lr->next = NULL;
428
429 if (lockReqDesc->type == RF_IO_TYPE_WRITE)
430 lockDesc->nWriters--;
431
432 /* search through the waiters list to see if anyone needs to
433 * be woken up. for each such descriptor in the wait list, we
434 * check it against everything granted and against everything
435 * _in front_ of it in the waiters queue. If it conflicts
436 * with none of these, we release it.
437 *
438 * DON'T TOUCH THE TEMPLINK POINTER OF ANYTHING IN THE GRANTED
439 * LIST HERE.
440 *
441 * This will roach the case where the callback tries to
442 * acquire a new lock in the same stripe. There are some
443 * asserts to try and detect this.
444 *
445 * We apply 2 performance optimizations: (1) if releasing this
446 * lock results in no more writers to this stripe, we just
447 * release everybody waiting, since we place no restrictions
448 * on the number of concurrent reads. (2) we consider as
449 * candidates for wakeup only those waiters that have a range
450 * overlap with either the descriptor being woken up or with
451 * something in the callbacklist (i.e. something we've just
452 * now woken up). This allows us to avoid the long evaluation
453 * for some descriptors. */
454
455 callbacklist = NULL;
456 if (lockDesc->nWriters == 0) { /* performance tweak (1) */
457 while (lockDesc->waitersH) {
458 /* delete from waiters list */
459 lr = lockDesc->waitersH;
460 lockDesc->waitersH = lr->next;
461
462 RF_ASSERT(lr->type == RF_IO_TYPE_READ);
463
464 /* add to granted list */
465 lr->next = lockDesc->granted;
466 lockDesc->granted = lr;
467
468 RF_ASSERT(!lr->templink);
469 /* put on callback list so that we'll invoke
470 callback below */
471 lr->templink = callbacklist;
472 callbacklist = lr;
473 #if RF_DEBUG_STRIPELOCK
474 if (rf_stripeLockDebug) {
475 Dprintf8("[%d] No writers: granting lock stripe ID %ld, type %c range %ld-%ld %ld-%ld table 0x%lx\n",
476 tid, stripeID, lr->type, lr->start, lr->stop, lr->start2, lr->stop2, (unsigned long) lockTable);
477 FLUSH;
478 }
479 #endif
480 }
481 lockDesc->waitersT = NULL;
482 /* we've purged the whole waiters list */
483
484 } else
485 for (candidate_t = NULL, candidate = lockDesc->waitersH;
486 candidate;) {
487
488 /* performance tweak (2) */
489 consider_it = 0;
490 if (RANGE_OVERLAP(lockReqDesc, candidate))
491 consider_it = 1;
492 else
493 for (t = callbacklist; t; t = t->templink)
494 if (RANGE_OVERLAP(t, candidate)) {
495 consider_it = 1;
496 break;
497 }
498 if (!consider_it) {
499 #if RF_DEBUG_STRIPELOCK
500 if (rf_stripeLockDebug) {
501 Dprintf8("[%d] No overlap: rejecting candidate stripeID %ld, type %c range %ld-%ld %ld-%ld table 0x%lx\n",
502 tid, stripeID, candidate->type, candidate->start, candidate->stop, candidate->start2, candidate->stop2,
503 (unsigned long) lockTable);
504 FLUSH;
505 }
506 #endif
507 candidate_t = candidate;
508 candidate = candidate->next;
509 continue;
510 }
511 /* we have a candidate for release. check to
512 * make sure it is not blocked by any granted
513 * locks */
514 release_it = 1;
515 for (predecessor = lockDesc->granted; predecessor;
516 predecessor = predecessor->next) {
517 if (STRIPELOCK_CONFLICT(candidate,
518 predecessor)) {
519 #if RF_DEBUG_STRIPELOCK
520 if (rf_stripeLockDebug) {
521 Dprintf8("[%d] Conflicts with granted lock: rejecting candidate stripeID %ld, type %c range %ld-%ld %ld-%ld table 0x%lx\n",
522 tid, stripeID, candidate->type, candidate->start, candidate->stop, candidate->start2, candidate->stop2,
523 (unsigned long) lockTable);
524 FLUSH;
525 }
526 #endif
527 release_it = 0;
528 break;
529 }
530 }
531
532 /* now check to see if the candidate is
533 * blocked by any waiters that occur before it
534 * it the wait queue */
535 if (release_it)
536 for (predecessor = lockDesc->waitersH;
537 predecessor != candidate;
538 predecessor = predecessor->next) {
539 if (STRIPELOCK_CONFLICT(candidate,
540 predecessor)) {
541 #if RF_DEBUG_STRIPELOCK
542 if (rf_stripeLockDebug) {
543 Dprintf8("[%d] Conflicts with waiting lock: rejecting candidate stripeID %ld, type %c range %ld-%ld %ld-%ld table 0x%lx\n",
544 tid, stripeID, candidate->type, candidate->start, candidate->stop, candidate->start2, candidate->stop2,
545 (unsigned long) lockTable);
546 FLUSH;
547 }
548 #endif
549 release_it = 0;
550 break;
551 }
552 }
553
554 /* release it if indicated */
555 if (release_it) {
556 #if RF_DEBUG_STRIPELOCK
557 if (rf_stripeLockDebug) {
558 Dprintf8("[%d] Granting lock to candidate stripeID %ld, type %c range %ld-%ld %ld-%ld table 0x%lx\n",
559 tid, stripeID, candidate->type, candidate->start, candidate->stop, candidate->start2, candidate->stop2,
560 (unsigned long) lockTable);
561 FLUSH;
562 }
563 #endif
564 if (candidate_t) {
565 candidate_t->next = candidate->next;
566 if (lockDesc->waitersT == candidate)
567 lockDesc->waitersT = candidate_t; /* cannot be waitersH since candidate_t is not NULL */
568 } else {
569 RF_ASSERT(candidate == lockDesc->waitersH);
570 lockDesc->waitersH = lockDesc->waitersH->next;
571 if (!lockDesc->waitersH)
572 lockDesc->waitersT = NULL;
573 }
574 /* move it to the granted list */
575 candidate->next = lockDesc->granted;
576 lockDesc->granted = candidate;
577
578 RF_ASSERT(!candidate->templink);
579 /* put it on the list of things to be
580 called after we release the mutex */
581 candidate->templink = callbacklist;
582
583 callbacklist = candidate;
584
585 if (!candidate_t)
586 candidate = lockDesc->waitersH;
587 else
588 candidate = candidate_t->next;
589 /* continue with the rest of the list */
590 } else {
591 candidate_t = candidate;
592 /* continue with the rest of the list */
593 candidate = candidate->next;
594 }
595 }
596
597 /* delete the descriptor if no one is waiting or active */
598 if (!lockDesc->granted && !lockDesc->waitersH) {
599 RF_ASSERT(lockDesc->nWriters == 0);
600 #if RF_DEBUG_STRIPELOCK
601 if (rf_stripeLockDebug) {
602 Dprintf3("[%d] Last lock released (table 0x%lx): deleting desc for stripeID %ld\n", tid, (unsigned long) lockTable, stripeID);
603 FLUSH;
604 }
605 #endif
606 if (ld_t)
607 ld_t->next = lockDesc->next;
608 else {
609 RF_ASSERT(lockDesc == lockTable[hashval].descList);
610 lockTable[hashval].descList = lockDesc->next;
611 }
612 FreeStripeLockDesc(lockDesc);
613 lockDesc = NULL;/* only for the ASSERT below */
614 }
615 RF_UNLOCK_MUTEX(lockTable[hashval].mutex);
616
617 /* now that we've unlocked the mutex, invoke the callback on
618 * all the descriptors in the list */
619
620 /* if we deleted the descriptor, we should have no callbacks
621 * to do */
622 RF_ASSERT(!((callbacklist) && (!lockDesc)));
623 for (candidate = callbacklist; candidate;) {
624 t = candidate;
625 candidate = candidate->templink;
626 t->templink = NULL;
627 (t->cbFunc) (t->cbArg);
628 }
629 }
630 /* must have the indicated lock table mutex upon entry */
631 static void
632 AddToWaitersQueue(RF_StripeLockDesc_t *lockDesc, RF_LockReqDesc_t *lockReqDesc)
633 {
634 if (!lockDesc->waitersH) {
635 lockDesc->waitersH = lockDesc->waitersT = lockReqDesc;
636 } else {
637 lockDesc->waitersT->next = lockReqDesc;
638 lockDesc->waitersT = lockReqDesc;
639 }
640 }
641
642 static RF_StripeLockDesc_t *
643 AllocStripeLockDesc(RF_StripeNum_t stripeID)
644 {
645 RF_StripeLockDesc_t *p;
646
647 p = pool_get(&rf_stripelock_pool, PR_WAITOK);
648 if (p) {
649 p->stripeID = stripeID;
650 p->granted = NULL;
651 p->waitersH = NULL;
652 p->waitersT = NULL;
653 p->nWriters = 0;
654 p->next = NULL;
655 }
656 return (p);
657 }
658
659 static void
660 FreeStripeLockDesc(RF_StripeLockDesc_t *p)
661 {
662 pool_put(&rf_stripelock_pool, p);
663 }
664
665 #if RF_DEBUG_STRIPELOCK
666 static void
667 PrintLockedStripes(RF_LockTableEntry_t *lockTable)
668 {
669 int i, j, foundone = 0, did;
670 RF_StripeLockDesc_t *p;
671 RF_LockReqDesc_t *q;
672
673 RF_LOCK_MUTEX(rf_printf_mutex);
674 printf("Locked stripes:\n");
675 for (i = 0; i < rf_lockTableSize; i++)
676 if (lockTable[i].descList) {
677 foundone = 1;
678 for (p = lockTable[i].descList; p; p = p->next) {
679 printf("Stripe ID 0x%lx (%d) nWriters %d\n",
680 (long) p->stripeID, (int) p->stripeID,
681 p->nWriters);
682
683 if (!(p->granted))
684 printf("Granted: (none)\n");
685 else
686 printf("Granted:\n");
687 for (did = 1, j = 0, q = p->granted; q;
688 j++, q = q->next) {
689 printf(" %c(%ld-%ld", q->type, (long) q->start, (long) q->stop);
690 if (q->start2 != -1)
691 printf(",%ld-%ld) ", (long) q->start2,
692 (long) q->stop2);
693 else
694 printf(") ");
695 if (j && !(j % 4)) {
696 printf("\n");
697 did = 1;
698 } else
699 did = 0;
700 }
701 if (!did)
702 printf("\n");
703
704 if (!(p->waitersH))
705 printf("Waiting: (none)\n");
706 else
707 printf("Waiting:\n");
708 for (did = 1, j = 0, q = p->waitersH; q;
709 j++, q = q->next) {
710 printf("%c(%ld-%ld", q->type, (long) q->start, (long) q->stop);
711 if (q->start2 != -1)
712 printf(",%ld-%ld) ", (long) q->start2, (long) q->stop2);
713 else
714 printf(") ");
715 if (j && !(j % 4)) {
716 printf("\n ");
717 did = 1;
718 } else
719 did = 0;
720 }
721 if (!did)
722 printf("\n");
723 }
724 }
725 if (!foundone)
726 printf("(none)\n");
727 else
728 printf("\n");
729 RF_UNLOCK_MUTEX(rf_printf_mutex);
730 }
731 #endif
732