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