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