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