Home | History | Annotate | Line # | Download | only in raidframe
rf_stripelocks.c revision 1.1
      1  1.1  oster /*	$NetBSD: rf_stripelocks.c,v 1.1 1998/11/13 04:20:34 oster 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  * Log: rf_stripelocks.c,v
     31  1.1  oster  * Revision 1.35  1996/06/10 12:50:57  jimz
     32  1.1  oster  * Add counters to freelists to track number of allocations, frees,
     33  1.1  oster  * grows, max size, etc. Adjust a couple sets of PRIME params based
     34  1.1  oster  * on the results.
     35  1.1  oster  *
     36  1.1  oster  * Revision 1.34  1996/06/10  11:55:47  jimz
     37  1.1  oster  * Straightened out some per-array/not-per-array distinctions, fixed
     38  1.1  oster  * a couple bugs related to confusion. Added shutdown lists. Removed
     39  1.1  oster  * layout shutdown function (now subsumed by shutdown lists).
     40  1.1  oster  *
     41  1.1  oster  * Revision 1.33  1996/06/09  02:36:46  jimz
     42  1.1  oster  * lots of little crufty cleanup- fixup whitespace
     43  1.1  oster  * issues, comment #ifdefs, improve typing in some
     44  1.1  oster  * places (esp size-related)
     45  1.1  oster  *
     46  1.1  oster  * Revision 1.32  1996/06/07  21:33:04  jimz
     47  1.1  oster  * begin using consistent types for sector numbers,
     48  1.1  oster  * stripe numbers, row+col numbers, recon unit numbers
     49  1.1  oster  *
     50  1.1  oster  * Revision 1.31  1996/06/05  18:06:02  jimz
     51  1.1  oster  * Major code cleanup. The Great Renaming is now done.
     52  1.1  oster  * Better modularity. Better typing. Fixed a bunch of
     53  1.1  oster  * synchronization bugs. Made a lot of global stuff
     54  1.1  oster  * per-desc or per-array. Removed dead code.
     55  1.1  oster  *
     56  1.1  oster  * Revision 1.30  1996/06/03  23:28:26  jimz
     57  1.1  oster  * more bugfixes
     58  1.1  oster  * check in tree to sync for IPDS runs with current bugfixes
     59  1.1  oster  * there still may be a problem with threads in the script test
     60  1.1  oster  * getting I/Os stuck- not trivially reproducible (runs ~50 times
     61  1.1  oster  * in a row without getting stuck)
     62  1.1  oster  *
     63  1.1  oster  * Revision 1.29  1996/05/30  23:22:16  jimz
     64  1.1  oster  * bugfixes of serialization, timing problems
     65  1.1  oster  * more cleanup
     66  1.1  oster  *
     67  1.1  oster  * Revision 1.28  1996/05/30  11:29:41  jimz
     68  1.1  oster  * Numerous bug fixes. Stripe lock release code disagreed with the taking code
     69  1.1  oster  * about when stripes should be locked (I made it consistent: no parity, no lock)
     70  1.1  oster  * There was a lot of extra serialization of I/Os which I've removed- a lot of
     71  1.1  oster  * it was to calculate values for the cache code, which is no longer with us.
     72  1.1  oster  * More types, function, macro cleanup. Added code to properly quiesce the array
     73  1.1  oster  * on shutdown. Made a lot of stuff array-specific which was (bogusly) general
     74  1.1  oster  * before. Fixed memory allocation, freeing bugs.
     75  1.1  oster  *
     76  1.1  oster  * Revision 1.27  1996/05/27  18:56:37  jimz
     77  1.1  oster  * more code cleanup
     78  1.1  oster  * better typing
     79  1.1  oster  * compiles in all 3 environments
     80  1.1  oster  *
     81  1.1  oster  * Revision 1.26  1996/05/24  22:17:04  jimz
     82  1.1  oster  * continue code + namespace cleanup
     83  1.1  oster  * typed a bunch of flags
     84  1.1  oster  *
     85  1.1  oster  * Revision 1.25  1996/05/23  00:33:23  jimz
     86  1.1  oster  * code cleanup: move all debug decls to rf_options.c, all extern
     87  1.1  oster  * debug decls to rf_options.h, all debug vars preceded by rf_
     88  1.1  oster  *
     89  1.1  oster  * Revision 1.24  1996/05/20  16:15:00  jimz
     90  1.1  oster  * switch to rf_{mutex,cond}_{init,destroy}
     91  1.1  oster  *
     92  1.1  oster  * Revision 1.23  1996/05/18  19:51:34  jimz
     93  1.1  oster  * major code cleanup- fix syntax, make some types consistent,
     94  1.1  oster  * add prototypes, clean out dead code, et cetera
     95  1.1  oster  *
     96  1.1  oster  * Revision 1.22  1996/05/16  22:28:11  jimz
     97  1.1  oster  * misc cleanup
     98  1.1  oster  *
     99  1.1  oster  * Revision 1.21  1996/05/15  23:39:52  jimz
    100  1.1  oster  * remove #if 0 code
    101  1.1  oster  *
    102  1.1  oster  * Revision 1.20  1996/05/15  23:37:38  jimz
    103  1.1  oster  * convert to using RF_FREELIST stuff for StripeLockDesc allocation
    104  1.1  oster  *
    105  1.1  oster  * Revision 1.19  1996/05/08  18:00:53  jimz
    106  1.1  oster  * fix number of args to debug printf
    107  1.1  oster  *
    108  1.1  oster  * Revision 1.18  1996/05/06  22:33:07  jimz
    109  1.1  oster  * added better debug info
    110  1.1  oster  *
    111  1.1  oster  * Revision 1.17  1996/05/06  22:09:01  wvcii
    112  1.1  oster  * added copyright info and change log
    113  1.1  oster  *
    114  1.1  oster  */
    115  1.1  oster 
    116  1.1  oster /*
    117  1.1  oster  * stripelocks.c -- code to lock stripes for read and write access
    118  1.1  oster  *
    119  1.1  oster  * The code distinguishes between read locks and write locks. There can be
    120  1.1  oster  * as many readers to given stripe as desired. When a write request comes
    121  1.1  oster  * in, no further readers are allowed to enter, and all subsequent requests
    122  1.1  oster  * are queued in FIFO order. When a the number of readers goes to zero, the
    123  1.1  oster  * writer is given the lock. When a writer releases the lock, the list of
    124  1.1  oster  * queued requests is scanned, and all readersq up to the next writer are
    125  1.1  oster  * given the lock.
    126  1.1  oster  *
    127  1.1  oster  * The lock table size must be one less than a power of two, but HASH_STRIPEID
    128  1.1  oster  * is the only function that requires this.
    129  1.1  oster  *
    130  1.1  oster  * The code now supports "range locks". When you ask to lock a stripe, you
    131  1.1  oster  * specify a range of addresses in that stripe that you want to lock. When
    132  1.1  oster  * you acquire the lock, you've locked only this range of addresses, and
    133  1.1  oster  * other threads can concurrently read/write any non-overlapping portions
    134  1.1  oster  * of the stripe. The "addresses" that you lock are abstract in that you
    135  1.1  oster  * can pass in anything you like.  The expectation is that you'll pass in
    136  1.1  oster  * the range of physical disk offsets of the parity bits you're planning
    137  1.1  oster  * to update. The idea behind this, of course, is to allow sub-stripe
    138  1.1  oster  * locking. The implementation is perhaps not the best imaginable; in the
    139  1.1  oster  * worst case a lock release is O(n^2) in the total number of outstanding
    140  1.1  oster  * requests to a given stripe.  Note that if you're striping with a
    141  1.1  oster  * stripe unit size equal to an entire disk (i.e. not striping), there will
    142  1.1  oster  * be only one stripe and you may spend some significant number of cycles
    143  1.1  oster  * searching through stripe lock descriptors.
    144  1.1  oster  */
    145  1.1  oster 
    146  1.1  oster #ifdef _KERNEL
    147  1.1  oster #define KERNEL
    148  1.1  oster #endif
    149  1.1  oster 
    150  1.1  oster #include "rf_types.h"
    151  1.1  oster #include "rf_raid.h"
    152  1.1  oster #include "rf_stripelocks.h"
    153  1.1  oster #include "rf_alloclist.h"
    154  1.1  oster #include "rf_threadid.h"
    155  1.1  oster #include "rf_general.h"
    156  1.1  oster #include "rf_freelist.h"
    157  1.1  oster #include "rf_debugprint.h"
    158  1.1  oster #include "rf_driver.h"
    159  1.1  oster #include "rf_shutdown.h"
    160  1.1  oster 
    161  1.1  oster #define Dprintf1(s,a)         rf_debug_printf(s,(void *)((unsigned long)a),NULL,NULL,NULL,NULL,NULL,NULL,NULL)
    162  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)
    163  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)
    164  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)
    165  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)
    166  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)
    167  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)
    168  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))
    169  1.1  oster 
    170  1.1  oster #ifndef KERNEL
    171  1.1  oster #define FLUSH fflush(stdout)
    172  1.1  oster #else /* !KERNEL */
    173  1.1  oster #define FLUSH
    174  1.1  oster #endif /* !KERNEL */
    175  1.1  oster 
    176  1.1  oster #define HASH_STRIPEID(_sid_)  ( (_sid_) & (rf_lockTableSize-1) )
    177  1.1  oster #define MAX_FREELIST 100
    178  1.1  oster 
    179  1.1  oster static void AddToWaitersQueue(RF_LockTableEntry_t *lockTable, RF_StripeLockDesc_t *lockDesc, RF_LockReqDesc_t *lockReqDesc);
    180  1.1  oster static RF_StripeLockDesc_t *AllocStripeLockDesc(RF_StripeNum_t stripeID);
    181  1.1  oster static void FreeStripeLockDesc(RF_StripeLockDesc_t *p);
    182  1.1  oster static void PrintLockedStripes(RF_LockTableEntry_t *lockTable);
    183  1.1  oster 
    184  1.1  oster /* determines if two ranges overlap.  always yields false if either start value is negative  */
    185  1.1  oster #define SINGLE_RANGE_OVERLAP(_strt1, _stop1, _strt2, _stop2)                                     \
    186  1.1  oster   ( (_strt1 >= 0) && (_strt2 >= 0) && (RF_MAX(_strt1, _strt2) <= RF_MIN(_stop1, _stop2)) )
    187  1.1  oster 
    188  1.1  oster /* determines if any of the ranges specified in the two lock descriptors overlap each other */
    189  1.1  oster #define RANGE_OVERLAP(_cand, _pred)                                                  \
    190  1.1  oster   ( SINGLE_RANGE_OVERLAP((_cand)->start,  (_cand)->stop,  (_pred)->start,  (_pred)->stop ) ||    \
    191  1.1  oster     SINGLE_RANGE_OVERLAP((_cand)->start2, (_cand)->stop2, (_pred)->start,  (_pred)->stop ) ||    \
    192  1.1  oster     SINGLE_RANGE_OVERLAP((_cand)->start,  (_cand)->stop,  (_pred)->start2, (_pred)->stop2) ||    \
    193  1.1  oster     SINGLE_RANGE_OVERLAP((_cand)->start2, (_cand)->stop2, (_pred)->start2, (_pred)->stop2) )
    194  1.1  oster 
    195  1.1  oster /* Determines if a candidate lock request conflicts with a predecessor lock req.
    196  1.1  oster  * Note that the arguments are not interchangeable.
    197  1.1  oster  * The rules are:
    198  1.1  oster  *      a candidate read conflicts with a predecessor write if any ranges overlap
    199  1.1  oster  *      a candidate write conflicts with a predecessor read if any ranges overlap
    200  1.1  oster  *      a candidate write conflicts with a predecessor write if any ranges overlap
    201  1.1  oster  */
    202  1.1  oster #define STRIPELOCK_CONFLICT(_cand, _pred)                                        \
    203  1.1  oster   RANGE_OVERLAP((_cand), (_pred)) &&                                        \
    204  1.1  oster   ( ( (((_cand)->type == RF_IO_TYPE_READ) && ((_pred)->type == RF_IO_TYPE_WRITE)) ||                      \
    205  1.1  oster       (((_cand)->type == RF_IO_TYPE_WRITE) && ((_pred)->type == RF_IO_TYPE_READ)) ||                      \
    206  1.1  oster       (((_cand)->type == RF_IO_TYPE_WRITE) && ((_pred)->type == RF_IO_TYPE_WRITE))                         \
    207  1.1  oster     )                                                                            \
    208  1.1  oster   )
    209  1.1  oster 
    210  1.1  oster static RF_FreeList_t *rf_stripelock_freelist;
    211  1.1  oster #define RF_MAX_FREE_STRIPELOCK 128
    212  1.1  oster #define RF_STRIPELOCK_INC        8
    213  1.1  oster #define RF_STRIPELOCK_INITIAL   32
    214  1.1  oster 
    215  1.1  oster static void rf_ShutdownStripeLockFreeList(void *);
    216  1.1  oster static void rf_RaidShutdownStripeLocks(void *);
    217  1.1  oster 
    218  1.1  oster static void rf_ShutdownStripeLockFreeList(ignored)
    219  1.1  oster   void  *ignored;
    220  1.1  oster {
    221  1.1  oster 	RF_FREELIST_DESTROY(rf_stripelock_freelist,next,(RF_StripeLockDesc_t *));
    222  1.1  oster }
    223  1.1  oster 
    224  1.1  oster int rf_ConfigureStripeLockFreeList(listp)
    225  1.1  oster   RF_ShutdownList_t  **listp;
    226  1.1  oster {
    227  1.1  oster 	unsigned mask;
    228  1.1  oster 	int rc;
    229  1.1  oster 
    230  1.1  oster 	RF_FREELIST_CREATE(rf_stripelock_freelist, RF_MAX_FREE_STRIPELOCK,
    231  1.1  oster 		RF_STRIPELOCK_INITIAL,sizeof(RF_StripeLockDesc_t));
    232  1.1  oster 	rc = rf_ShutdownCreate(listp, rf_ShutdownStripeLockFreeList, NULL);
    233  1.1  oster 	if (rc) {
    234  1.1  oster 		RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
    235  1.1  oster 			__FILE__, __LINE__, rc);
    236  1.1  oster 		rf_ShutdownStripeLockFreeList(NULL);
    237  1.1  oster 		return(rc);
    238  1.1  oster 	}
    239  1.1  oster 	RF_FREELIST_PRIME(rf_stripelock_freelist,RF_STRIPELOCK_INITIAL,next,
    240  1.1  oster 		(RF_StripeLockDesc_t *));
    241  1.1  oster 	for (mask=0x1; mask; mask<<=1)
    242  1.1  oster 		if (rf_lockTableSize==mask)
    243  1.1  oster 			break;
    244  1.1  oster 	if (!mask) {
    245  1.1  oster 		printf("[WARNING:  lock table size must be a power of two.  Setting to %d.]\n",RF_DEFAULT_LOCK_TABLE_SIZE);
    246  1.1  oster 		rf_lockTableSize = RF_DEFAULT_LOCK_TABLE_SIZE;
    247  1.1  oster 	}
    248  1.1  oster 	return(0);
    249  1.1  oster }
    250  1.1  oster 
    251  1.1  oster RF_LockTableEntry_t *rf_MakeLockTable()
    252  1.1  oster {
    253  1.1  oster 	RF_LockTableEntry_t *lockTable;
    254  1.1  oster 	int i, rc;
    255  1.1  oster 
    256  1.1  oster 	RF_Calloc(lockTable, ((int) rf_lockTableSize), sizeof(RF_LockTableEntry_t), (RF_LockTableEntry_t *));
    257  1.1  oster 	if (lockTable == NULL)
    258  1.1  oster 		return(NULL);
    259  1.1  oster 	for (i=0; i<rf_lockTableSize; i++) {
    260  1.1  oster 		rc = rf_mutex_init(&lockTable[i].mutex);
    261  1.1  oster 		if (rc) {
    262  1.1  oster 		    RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    263  1.1  oster 		      __LINE__, rc);
    264  1.1  oster 			/* XXX clean up other mutexes */
    265  1.1  oster 			return(NULL);
    266  1.1  oster 		}
    267  1.1  oster 	}
    268  1.1  oster 	return(lockTable);
    269  1.1  oster }
    270  1.1  oster 
    271  1.1  oster void rf_ShutdownStripeLocks(RF_LockTableEntry_t *lockTable)
    272  1.1  oster {
    273  1.1  oster 	int i;
    274  1.1  oster 
    275  1.1  oster 	if (rf_stripeLockDebug) {
    276  1.1  oster 		PrintLockedStripes(lockTable);
    277  1.1  oster 	}
    278  1.1  oster 	for (i=0; i<rf_lockTableSize; i++) {
    279  1.1  oster 		rf_mutex_destroy(&lockTable[i].mutex);
    280  1.1  oster 	}
    281  1.1  oster 	RF_Free(lockTable, rf_lockTableSize*sizeof(RF_LockTableEntry_t));
    282  1.1  oster }
    283  1.1  oster 
    284  1.1  oster static void rf_RaidShutdownStripeLocks(arg)
    285  1.1  oster   void  *arg;
    286  1.1  oster {
    287  1.1  oster 	RF_Raid_t *raidPtr = (RF_Raid_t *)arg;
    288  1.1  oster 	rf_ShutdownStripeLocks(raidPtr->lockTable);
    289  1.1  oster }
    290  1.1  oster 
    291  1.1  oster int rf_ConfigureStripeLocks(
    292  1.1  oster   RF_ShutdownList_t  **listp,
    293  1.1  oster   RF_Raid_t           *raidPtr,
    294  1.1  oster   RF_Config_t         *cfgPtr)
    295  1.1  oster {
    296  1.1  oster 	int rc;
    297  1.1  oster 
    298  1.1  oster 	raidPtr->lockTable = rf_MakeLockTable();
    299  1.1  oster 	if (raidPtr->lockTable == NULL)
    300  1.1  oster 		return(ENOMEM);
    301  1.1  oster 	rc = rf_ShutdownCreate(listp, rf_RaidShutdownStripeLocks, raidPtr);
    302  1.1  oster 	if (rc) {
    303  1.1  oster 		RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
    304  1.1  oster 			__FILE__, __LINE__, rc);
    305  1.1  oster 		rf_ShutdownStripeLocks(raidPtr->lockTable);
    306  1.1  oster 		return(rc);
    307  1.1  oster 	}
    308  1.1  oster 	return(0);
    309  1.1  oster }
    310  1.1  oster 
    311  1.1  oster /* returns 0 if you've got the lock, and non-zero if you have to wait.
    312  1.1  oster  * if and only if you have to wait, we'll cause cbFunc to get invoked
    313  1.1  oster  * with cbArg when you are granted the lock.  We store a tag in *releaseTag
    314  1.1  oster  * that you need to give back to us when you release the lock.
    315  1.1  oster  */
    316  1.1  oster int rf_AcquireStripeLock(
    317  1.1  oster   RF_LockTableEntry_t  *lockTable,
    318  1.1  oster   RF_StripeNum_t        stripeID,
    319  1.1  oster   RF_LockReqDesc_t     *lockReqDesc)
    320  1.1  oster {
    321  1.1  oster   RF_StripeLockDesc_t *lockDesc;
    322  1.1  oster   RF_LockReqDesc_t    *p;
    323  1.1  oster   int tid=0, hashval = HASH_STRIPEID(stripeID);
    324  1.1  oster   int retcode = 0;
    325  1.1  oster 
    326  1.1  oster   RF_ASSERT(RF_IO_IS_R_OR_W(lockReqDesc->type));
    327  1.1  oster 
    328  1.1  oster   if (rf_stripeLockDebug) {
    329  1.1  oster     rf_get_threadid(tid);
    330  1.1  oster     if (stripeID == -1) Dprintf1("[%d] Lock acquisition supressed (stripeID == -1)\n",tid);
    331  1.1  oster     else {
    332  1.1  oster       Dprintf8("[%d] Trying to acquire stripe lock table 0x%lx SID %ld type %c range %ld-%ld, range2 %ld-%ld hashval %d\n",
    333  1.1  oster         tid, (unsigned long) lockTable, stripeID, lockReqDesc->type, lockReqDesc->start,
    334  1.1  oster         lockReqDesc->stop, lockReqDesc->start2, lockReqDesc->stop2);
    335  1.1  oster       Dprintf3("[%d] lock %ld hashval %d\n", tid, stripeID, hashval);
    336  1.1  oster       FLUSH;
    337  1.1  oster     }
    338  1.1  oster   }
    339  1.1  oster   if (stripeID == -1) return(0);
    340  1.1  oster   lockReqDesc->next = NULL;                       /* just to be sure */
    341  1.1  oster 
    342  1.1  oster   RF_LOCK_MUTEX(lockTable[hashval].mutex);
    343  1.1  oster   for (lockDesc = lockTable[hashval].descList; lockDesc; lockDesc=lockDesc->next) {
    344  1.1  oster     if (lockDesc->stripeID == stripeID) break;
    345  1.1  oster   }
    346  1.1  oster 
    347  1.1  oster   if (!lockDesc) {            /* no entry in table => no one reading or writing */
    348  1.1  oster     lockDesc = AllocStripeLockDesc(stripeID);
    349  1.1  oster     lockDesc->next = lockTable[hashval].descList;
    350  1.1  oster     lockTable[hashval].descList = lockDesc;
    351  1.1  oster     if (lockReqDesc->type == RF_IO_TYPE_WRITE) lockDesc->nWriters++;
    352  1.1  oster     lockDesc->granted = lockReqDesc;
    353  1.1  oster     if (rf_stripeLockDebug) {Dprintf7("[%d] no one waiting: lock %ld %c %ld-%ld %ld-%ld granted\n",
    354  1.1  oster 				tid,stripeID,lockReqDesc->type,lockReqDesc->start,lockReqDesc->stop,lockReqDesc->start2,lockReqDesc->stop2); FLUSH;}
    355  1.1  oster   } else {
    356  1.1  oster 
    357  1.1  oster     if (lockReqDesc->type == RF_IO_TYPE_WRITE) lockDesc->nWriters++;
    358  1.1  oster 
    359  1.1  oster     if (lockDesc->nWriters == 0) {                        /* no need to search any lists if there are no writers anywhere */
    360  1.1  oster       lockReqDesc->next = lockDesc->granted;
    361  1.1  oster       lockDesc->granted = lockReqDesc;
    362  1.1  oster       if (rf_stripeLockDebug) {Dprintf7("[%d] no writers: lock %ld %c %ld-%ld %ld-%ld granted\n",
    363  1.1  oster 				  tid,stripeID,lockReqDesc->type,lockReqDesc->start,lockReqDesc->stop,lockReqDesc->start2,lockReqDesc->stop2); FLUSH;}
    364  1.1  oster     } else {
    365  1.1  oster 
    366  1.1  oster       /* search the granted & waiting lists for a conflict.  stop searching as soon as we find one */
    367  1.1  oster       retcode = 0;
    368  1.1  oster       for (p = lockDesc->granted; p; p=p->next) if (STRIPELOCK_CONFLICT(lockReqDesc, p)) {retcode = 1; break;}
    369  1.1  oster       if (!retcode) for (p = lockDesc->waitersH; p; p=p->next) if (STRIPELOCK_CONFLICT(lockReqDesc, p)) {retcode = 2; break;}
    370  1.1  oster 
    371  1.1  oster       if (!retcode) {
    372  1.1  oster 	lockReqDesc->next = lockDesc->granted;                                   /* no conflicts found => grant lock */
    373  1.1  oster 	lockDesc->granted = lockReqDesc;
    374  1.1  oster 	if (rf_stripeLockDebug) {
    375  1.1  oster 		Dprintf7("[%d] no conflicts: lock %ld %c %ld-%ld %ld-%ld granted\n",
    376  1.1  oster 			tid,stripeID,lockReqDesc->type,lockReqDesc->start,lockReqDesc->stop,
    377  1.1  oster 			lockReqDesc->start2,lockReqDesc->stop2);
    378  1.1  oster 		FLUSH;
    379  1.1  oster 	}
    380  1.1  oster       } else {
    381  1.1  oster 	if (rf_stripeLockDebug) {
    382  1.1  oster 		Dprintf6("[%d] conflict: lock %ld %c %ld-%ld hashval=%d not granted\n",
    383  1.1  oster 				tid,stripeID,lockReqDesc->type,lockReqDesc->start,lockReqDesc->stop,
    384  1.1  oster 				hashval);
    385  1.1  oster 		Dprintf3("[%d] lock %ld retcode=%d\n", tid, stripeID, retcode);
    386  1.1  oster 		FLUSH;
    387  1.1  oster 	}
    388  1.1  oster 	AddToWaitersQueue(lockTable, lockDesc, lockReqDesc);                     /* conflict => the current access must wait */
    389  1.1  oster       }
    390  1.1  oster     }
    391  1.1  oster   }
    392  1.1  oster 
    393  1.1  oster   RF_UNLOCK_MUTEX(lockTable[hashval].mutex);
    394  1.1  oster   return(retcode);
    395  1.1  oster }
    396  1.1  oster 
    397  1.1  oster void rf_ReleaseStripeLock(
    398  1.1  oster   RF_LockTableEntry_t  *lockTable,
    399  1.1  oster   RF_StripeNum_t        stripeID,
    400  1.1  oster   RF_LockReqDesc_t     *lockReqDesc)
    401  1.1  oster {
    402  1.1  oster   RF_StripeLockDesc_t *lockDesc, *ld_t;
    403  1.1  oster   RF_LockReqDesc_t    *lr, *lr_t, *callbacklist, *t;
    404  1.1  oster   RF_IoType_t type = lockReqDesc->type;
    405  1.1  oster   int tid=0, hashval = HASH_STRIPEID(stripeID);
    406  1.1  oster   int release_it, consider_it;
    407  1.1  oster   RF_LockReqDesc_t *candidate, *candidate_t, *predecessor;
    408  1.1  oster 
    409  1.1  oster   RF_ASSERT(RF_IO_IS_R_OR_W(type));
    410  1.1  oster 
    411  1.1  oster   if (rf_stripeLockDebug) {
    412  1.1  oster     rf_get_threadid(tid);
    413  1.1  oster     if (stripeID == -1) Dprintf1("[%d] Lock release supressed (stripeID == -1)\n",tid);
    414  1.1  oster     else {Dprintf8("[%d] Releasing stripe lock on stripe ID %ld, type %c range %ld-%ld %ld-%ld table 0x%lx\n",
    415  1.1  oster 		 tid,stripeID,lockReqDesc->type,lockReqDesc->start,lockReqDesc->stop,lockReqDesc->start2,lockReqDesc->stop2, lockTable); FLUSH;}
    416  1.1  oster   }
    417  1.1  oster 
    418  1.1  oster   if (stripeID == -1) return;
    419  1.1  oster 
    420  1.1  oster   RF_LOCK_MUTEX(lockTable[hashval].mutex);
    421  1.1  oster 
    422  1.1  oster   /* find the stripe lock descriptor */
    423  1.1  oster   for (ld_t = NULL, lockDesc = lockTable[hashval].descList; lockDesc; ld_t = lockDesc, lockDesc=lockDesc->next) {
    424  1.1  oster     if (lockDesc->stripeID == stripeID) break;
    425  1.1  oster   }
    426  1.1  oster   RF_ASSERT(lockDesc);                                    /* major error to release a lock that doesn't exist */
    427  1.1  oster 
    428  1.1  oster   /* find the stripe lock request descriptor & delete it from the list */
    429  1.1  oster   for (lr_t = NULL, lr = lockDesc->granted; lr; lr_t = lr, lr=lr->next) if (lr == lockReqDesc) break;
    430  1.1  oster 
    431  1.1  oster   RF_ASSERT(lr && (lr == lockReqDesc));                            /* major error to release a lock that hasn't been granted */
    432  1.1  oster   if (lr_t) lr_t->next = lr->next; else {
    433  1.1  oster     RF_ASSERT(lr == lockDesc->granted);
    434  1.1  oster     lockDesc->granted = lr->next;
    435  1.1  oster   }
    436  1.1  oster   lr->next = NULL;
    437  1.1  oster 
    438  1.1  oster   if (lockReqDesc->type == RF_IO_TYPE_WRITE) lockDesc->nWriters--;
    439  1.1  oster 
    440  1.1  oster   /* search through the waiters list to see if anyone needs to be woken up.
    441  1.1  oster    * for each such descriptor in the wait list, we check it against everything granted and against
    442  1.1  oster    * everything _in front_ of it in the waiters queue.  If it conflicts with none of these, we release it.
    443  1.1  oster    *
    444  1.1  oster    * DON'T TOUCH THE TEMPLINK POINTER OF ANYTHING IN THE GRANTED LIST HERE.  This will roach the case where
    445  1.1  oster    * the callback tries to acquire a new lock in the same stripe.  There are some asserts to try and detect this.
    446  1.1  oster    *
    447  1.1  oster    * We apply 2 performance optimizations:
    448  1.1  oster    * (1) if releasing this lock results in no more writers to this stripe, we just release everybody waiting,
    449  1.1  oster    * since we place no restrictions on the number of concurrent reads.
    450  1.1  oster    * (2) we consider as candidates for wakeup only those waiters that have a range overlap with either
    451  1.1  oster    * the descriptor being woken up or with something in the callbacklist (i.e. something we've just now woken up).
    452  1.1  oster    * This allows us to avoid the long evaluation for some descriptors.
    453  1.1  oster    */
    454  1.1  oster 
    455  1.1  oster   callbacklist = NULL;
    456  1.1  oster   if (lockDesc->nWriters == 0) {                          /* performance tweak (1) */
    457  1.1  oster     while (lockDesc->waitersH) {
    458  1.1  oster 
    459  1.1  oster       lr = lockDesc->waitersH;                            /* delete from waiters list */
    460  1.1  oster       lockDesc->waitersH = lr->next;
    461  1.1  oster 
    462  1.1  oster       RF_ASSERT(lr->type == RF_IO_TYPE_READ);
    463  1.1  oster 
    464  1.1  oster       lr->next = lockDesc->granted;                       /* add to granted list */
    465  1.1  oster       lockDesc->granted = lr;
    466  1.1  oster 
    467  1.1  oster       RF_ASSERT(!lr->templink);
    468  1.1  oster       lr->templink = callbacklist;                        /* put on callback list so that we'll invoke callback below */
    469  1.1  oster       callbacklist = lr;
    470  1.1  oster       if (rf_stripeLockDebug) {Dprintf8("[%d] No writers: granting lock stripe ID %ld, type %c range %ld-%ld %ld-%ld table 0x%lx\n",
    471  1.1  oster 				     tid,stripeID,lr->type,lr->start,lr->stop,lr->start2,lr->stop2,(unsigned long) lockTable); FLUSH;}
    472  1.1  oster     }
    473  1.1  oster     lockDesc->waitersT = NULL;                            /* we've purged the whole waiters list */
    474  1.1  oster 
    475  1.1  oster   } else for (candidate_t = NULL, candidate = lockDesc->waitersH; candidate; ) {
    476  1.1  oster 
    477  1.1  oster     /* performance tweak (2) */
    478  1.1  oster     consider_it = 0;
    479  1.1  oster     if (RANGE_OVERLAP(lockReqDesc, candidate)) consider_it = 1;
    480  1.1  oster     else for (t = callbacklist; t; t=t->templink) if (RANGE_OVERLAP(t, candidate)) {
    481  1.1  oster       consider_it = 1;
    482  1.1  oster       break;
    483  1.1  oster     }
    484  1.1  oster     if (!consider_it) {
    485  1.1  oster       if (rf_stripeLockDebug) {Dprintf8("[%d] No overlap: rejecting candidate stripeID %ld, type %c range %ld-%ld %ld-%ld table 0x%lx\n",
    486  1.1  oster 				     tid, stripeID, candidate->type, candidate->start, candidate->stop, candidate->start2, candidate->stop2,
    487  1.1  oster 				     (unsigned long) lockTable); FLUSH;}
    488  1.1  oster       candidate_t = candidate; candidate = candidate->next;
    489  1.1  oster       continue;
    490  1.1  oster     }
    491  1.1  oster 
    492  1.1  oster 
    493  1.1  oster     /* we have a candidate for release.  check to make sure it is not blocked by any granted locks */
    494  1.1  oster     release_it = 1;
    495  1.1  oster     for (predecessor = lockDesc->granted; predecessor; predecessor = predecessor->next) {
    496  1.1  oster       if (STRIPELOCK_CONFLICT(candidate, predecessor)) {
    497  1.1  oster 	if (rf_stripeLockDebug) {
    498  1.1  oster 	  Dprintf8("[%d] Conflicts with granted lock: rejecting candidate stripeID %ld, type %c range %ld-%ld %ld-%ld table 0x%lx\n",
    499  1.1  oster 		   tid, stripeID, candidate->type, candidate->start, candidate->stop, candidate->start2, candidate->stop2,
    500  1.1  oster 		   (unsigned long) lockTable); FLUSH;
    501  1.1  oster 	}
    502  1.1  oster 	release_it = 0; break;
    503  1.1  oster       }
    504  1.1  oster     }
    505  1.1  oster 
    506  1.1  oster     /* now check to see if the candidate is blocked by any waiters that occur before it it the wait queue */
    507  1.1  oster     if (release_it) for (predecessor = lockDesc->waitersH; predecessor != candidate; predecessor = predecessor->next) {
    508  1.1  oster       if (STRIPELOCK_CONFLICT(candidate, predecessor)) {
    509  1.1  oster 	if (rf_stripeLockDebug) {
    510  1.1  oster 	  Dprintf8("[%d] Conflicts with waiting lock: rejecting candidate stripeID %ld, type %c range %ld-%ld %ld-%ld table 0x%lx\n",
    511  1.1  oster 		   tid, stripeID, candidate->type, candidate->start, candidate->stop, candidate->start2, candidate->stop2,
    512  1.1  oster 		   (unsigned long) lockTable); FLUSH;
    513  1.1  oster 	}
    514  1.1  oster 	release_it = 0; break;
    515  1.1  oster       }
    516  1.1  oster     }
    517  1.1  oster 
    518  1.1  oster     /* release it if indicated */
    519  1.1  oster     if (release_it) {
    520  1.1  oster       if (rf_stripeLockDebug) {Dprintf8("[%d] Granting lock to candidate stripeID %ld, type %c range %ld-%ld %ld-%ld table 0x%lx\n",
    521  1.1  oster 				     tid, stripeID, candidate->type, candidate->start, candidate->stop, candidate->start2, candidate->stop2,
    522  1.1  oster 				     (unsigned long) lockTable); FLUSH;}
    523  1.1  oster       if (candidate_t) {
    524  1.1  oster 	candidate_t->next = candidate->next;
    525  1.1  oster 	if (lockDesc->waitersT == candidate) lockDesc->waitersT = candidate_t;       /* cannot be waitersH since candidate_t is not NULL */
    526  1.1  oster       } else {
    527  1.1  oster 	RF_ASSERT(candidate == lockDesc->waitersH);
    528  1.1  oster 	lockDesc->waitersH = lockDesc->waitersH->next;
    529  1.1  oster 	if (!lockDesc->waitersH) lockDesc->waitersT = NULL;
    530  1.1  oster       }
    531  1.1  oster       candidate->next = lockDesc->granted;                    /* move it to the granted list */
    532  1.1  oster       lockDesc->granted = candidate;
    533  1.1  oster 
    534  1.1  oster       RF_ASSERT(!candidate->templink);
    535  1.1  oster       candidate->templink = callbacklist;                     /* put it on the list of things to be called after we release the mutex */
    536  1.1  oster       callbacklist = candidate;
    537  1.1  oster 
    538  1.1  oster       if (!candidate_t) candidate = lockDesc->waitersH; else candidate = candidate_t->next;     /* continue with the rest of the list */
    539  1.1  oster     } else {
    540  1.1  oster       candidate_t = candidate; candidate = candidate->next;                                     /* continue with the rest of the list */
    541  1.1  oster     }
    542  1.1  oster   }
    543  1.1  oster 
    544  1.1  oster   /* delete the descriptor if no one is waiting or active */
    545  1.1  oster   if (!lockDesc->granted && !lockDesc->waitersH) {
    546  1.1  oster     RF_ASSERT(lockDesc->nWriters == 0);
    547  1.1  oster     if (rf_stripeLockDebug) {
    548  1.1  oster       Dprintf3("[%d] Last lock released (table 0x%lx): deleting desc for stripeID %ld\n",tid,(unsigned long) lockTable, stripeID); FLUSH;
    549  1.1  oster     }
    550  1.1  oster     if (ld_t) ld_t->next = lockDesc->next; else {
    551  1.1  oster       RF_ASSERT(lockDesc == lockTable[hashval].descList);
    552  1.1  oster       lockTable[hashval].descList = lockDesc->next;
    553  1.1  oster     }
    554  1.1  oster     FreeStripeLockDesc(lockDesc);
    555  1.1  oster     lockDesc = NULL;                                                               /* only for the ASSERT below */
    556  1.1  oster   }
    557  1.1  oster 
    558  1.1  oster   RF_UNLOCK_MUTEX(lockTable[hashval].mutex);
    559  1.1  oster 
    560  1.1  oster   /* now that we've unlocked the mutex, invoke the callback on all the descriptors in the list */
    561  1.1  oster   RF_ASSERT(!( (callbacklist) && (!lockDesc) ));                   /* if we deleted the descriptor, we should have no callbacks to do */
    562  1.1  oster   for (candidate = callbacklist; candidate; ) {
    563  1.1  oster     t = candidate;
    564  1.1  oster     candidate = candidate->templink;
    565  1.1  oster     t->templink = NULL;
    566  1.1  oster     (t->cbFunc)(t->cbArg);
    567  1.1  oster   }
    568  1.1  oster }
    569  1.1  oster 
    570  1.1  oster /* must have the indicated lock table mutex upon entry */
    571  1.1  oster static void AddToWaitersQueue(
    572  1.1  oster   RF_LockTableEntry_t  *lockTable,
    573  1.1  oster   RF_StripeLockDesc_t  *lockDesc,
    574  1.1  oster   RF_LockReqDesc_t     *lockReqDesc)
    575  1.1  oster {
    576  1.1  oster   int tid;
    577  1.1  oster 
    578  1.1  oster   if (rf_stripeLockDebug) {
    579  1.1  oster     rf_get_threadid(tid);
    580  1.1  oster     Dprintf3("[%d] Waiting on lock for stripe %ld table 0x%lx\n", tid, lockDesc->stripeID, (unsigned long) lockTable); FLUSH;
    581  1.1  oster   }
    582  1.1  oster   if (!lockDesc->waitersH) {
    583  1.1  oster     lockDesc->waitersH = lockDesc->waitersT = lockReqDesc;
    584  1.1  oster   } else {
    585  1.1  oster     lockDesc->waitersT->next = lockReqDesc;
    586  1.1  oster     lockDesc->waitersT = lockReqDesc;
    587  1.1  oster   }
    588  1.1  oster }
    589  1.1  oster 
    590  1.1  oster static RF_StripeLockDesc_t *AllocStripeLockDesc(RF_StripeNum_t stripeID)
    591  1.1  oster {
    592  1.1  oster 	RF_StripeLockDesc_t *p;
    593  1.1  oster 
    594  1.1  oster 	RF_FREELIST_GET(rf_stripelock_freelist,p,next,(RF_StripeLockDesc_t *));
    595  1.1  oster 	if (p) {
    596  1.1  oster 		p->stripeID = stripeID;
    597  1.1  oster 	}
    598  1.1  oster 	return(p);
    599  1.1  oster }
    600  1.1  oster 
    601  1.1  oster static void FreeStripeLockDesc(RF_StripeLockDesc_t *p)
    602  1.1  oster {
    603  1.1  oster 	RF_FREELIST_FREE(rf_stripelock_freelist,p,next);
    604  1.1  oster }
    605  1.1  oster 
    606  1.1  oster static void PrintLockedStripes(lockTable)
    607  1.1  oster   RF_LockTableEntry_t  *lockTable;
    608  1.1  oster {
    609  1.1  oster   int i, j, foundone = 0, did;
    610  1.1  oster   RF_StripeLockDesc_t *p;
    611  1.1  oster   RF_LockReqDesc_t *q;
    612  1.1  oster 
    613  1.1  oster   RF_LOCK_MUTEX(rf_printf_mutex);
    614  1.1  oster   printf("Locked stripes:\n");
    615  1.1  oster   for (i=0; i<rf_lockTableSize; i++) if (lockTable[i].descList) {
    616  1.1  oster     foundone = 1;
    617  1.1  oster     for (p = lockTable[i].descList; p; p=p->next) {
    618  1.1  oster       printf("Stripe ID 0x%lx (%d) nWriters %d\n",
    619  1.1  oster 	     (long)p->stripeID, (int)p->stripeID, p->nWriters);
    620  1.1  oster 
    621  1.1  oster       if (! (p->granted) ) printf("Granted: (none)\n"); else printf("Granted:\n");
    622  1.1  oster       for (did=1,j=0,q = p->granted; q; j++,q=q->next) {
    623  1.1  oster 	printf("  %c(%ld-%ld",q->type,(long)q->start,(long)q->stop);
    624  1.1  oster 	if (q->start2 != -1) printf(",%ld-%ld) ",(long)q->start2,
    625  1.1  oster 				    (long)q->stop2); else printf(") ");
    626  1.1  oster 	if (j && !(j%4)) {printf("\n"); did=1;} else did=0;
    627  1.1  oster       }
    628  1.1  oster       if (!did) printf("\n");
    629  1.1  oster 
    630  1.1  oster       if (! (p->waitersH) ) printf("Waiting: (none)\n"); else printf("Waiting:\n");
    631  1.1  oster       for (did=1,j=0,q = p->waitersH; q; j++,q=q->next) {
    632  1.1  oster 	printf("%c(%ld-%ld",q->type,(long)q->start,(long)q->stop);
    633  1.1  oster 	if (q->start2 != -1) printf(",%ld-%ld) ",(long)q->start2,(long)q->stop2); else printf(") ");
    634  1.1  oster 	if (j && !(j%4)) {printf("\n         "); did=1;} else did=0;
    635  1.1  oster       }
    636  1.1  oster       if (!did) printf("\n");
    637  1.1  oster     }
    638  1.1  oster   }
    639  1.1  oster   if (!foundone) printf("(none)\n"); else printf("\n");
    640  1.1  oster   RF_UNLOCK_MUTEX(rf_printf_mutex);
    641  1.1  oster }
    642