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