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