Home | History | Annotate | Line # | Download | only in raidframe
      1 /*	$NetBSD: rf_stripelocks.h,v 1.10 2021/07/23 00:54:45 oster Exp $	*/
      2 /*
      3  * Copyright (c) 1995 Carnegie-Mellon University.
      4  * All rights reserved.
      5  *
      6  * Author: Mark Holland
      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  *
     31  * stripelocks.h -- header file for locking stripes
     32  *
     33  * Note that these functions are called from the execution routines of certain
     34  * DAG Nodes, and so they must be NON-BLOCKING to assure maximum parallelism
     35  * in the DAG.  Accordingly, when a node wants to acquire a lock, it calls
     36  * AcquireStripeLock, supplying a pointer to a callback function.  If the lock
     37  * is free at the time of the call, 0 is returned, indicating that the lock
     38  * has been acquired.  If the lock is not free, 1 is returned, and a copy of
     39  * the function pointer and argument are held in the lock table.  When the
     40  * lock becomes free, the callback function is invoked.
     41  *
     42  *****************************************************************************/
     43 
     44 #ifndef _RF__RF_STRIPELOCKS_H_
     45 #define _RF__RF_STRIPELOCKS_H_
     46 
     47 #include <sys/buf.h>
     48 #include <dev/raidframe/raidframevar.h>
     49 
     50 #include "rf_threadstuff.h"
     51 #include "rf_general.h"
     52 
     53 struct RF_LockReqDesc_s {
     54 	RF_IoType_t type;	/* read or write */
     55 	RF_int64 start, stop;	/* start and end of range to be locked */
     56 	RF_int64 start2, stop2;	/* start and end of 2nd range to be locked */
     57 	void    (*cbFunc) (void *);	/* callback function */
     58 	void   *cbArg;		/* argument to callback function */
     59 	RF_LockReqDesc_t *next;	/* next element in chain */
     60 	RF_LockReqDesc_t *templink;	/* for making short-lived lists of
     61 					 * request descriptors */
     62 };
     63 #define RF_ASSERT_VALID_LOCKREQ(_lr_) { \
     64 	RF_ASSERT(RF_IO_IS_R_OR_W((_lr_)->type)); \
     65 }
     66 
     67 struct RF_StripeLockDesc_s {
     68 	RF_StripeNum_t stripeID;/* the stripe ID */
     69 	RF_LockReqDesc_t *granted;	/* unordered list of granted requests */
     70 	RF_LockReqDesc_t *waitersH;	/* FIFO queue of all waiting reqs,
     71 					 * both read and write (Head and Tail) */
     72 	RF_LockReqDesc_t *waitersT;
     73 	int     nWriters;	/* number of writers either granted or waiting */
     74 	RF_StripeLockDesc_t *next;	/* for hash table collision resolution */
     75 };
     76 
     77 struct RF_LockTableEntry_s {
     78 	rf_declare_mutex2(mutex);	/* mutex on this hash chain */
     79 	RF_StripeLockDesc_t *descList;	/* hash chain of lock descriptors */
     80 };
     81 /*
     82  * Initializes a stripe lock descriptor.  _defSize is the number of sectors
     83  * that we lock when there is no parity information in the ASM (e.g. RAID0).
     84  */
     85 
     86 #define RF_INIT_LOCK_REQ_DESC(_lrd, _typ, _cbf, _cba, _asm, _defSize)                                           \
     87   {                                                                                                          \
     88     (_lrd).type    = _typ;                                                                                   \
     89     (_lrd).start2  = -1;                                                                                     \
     90     (_lrd).stop2   = -1;                                                                                     \
     91     if ((_asm)->parityInfo) {                                                                                \
     92       (_lrd).start = (_asm)->parityInfo->startSector;                                                        \
     93       (_lrd).stop  = (_asm)->parityInfo->startSector + (_asm)->parityInfo->numSector-1;                      \
     94       if ((_asm)->parityInfo->next) {                                                                        \
     95         (_lrd).start2  = (_asm)->parityInfo->next->startSector;                                              \
     96         (_lrd).stop2   = (_asm)->parityInfo->next->startSector + (_asm)->parityInfo->next->numSector-1;      \
     97       }                                                                                                      \
     98     } else {                                                                                                 \
     99       (_lrd).start = 0;                                                                                      \
    100       (_lrd).stop  = (_defSize);                                                                             \
    101     }													     \
    102     (_lrd).templink= NULL;                                                                                   \
    103     (_lrd).cbFunc  = (_cbf);                                                                                 \
    104     (_lrd).cbArg   = (void *) (_cba);                                                                        \
    105   }
    106 
    107 int rf_ConfigureStripeLockFreeList(RF_ShutdownList_t **, RF_Raid_t *, RF_Config_t *);
    108 int rf_ConfigureStripeLocks(RF_ShutdownList_t **, RF_Raid_t *, RF_Config_t *);
    109 int rf_AcquireStripeLock(RF_Raid_t *, RF_LockTableEntry_t *, RF_StripeNum_t, RF_LockReqDesc_t *);
    110 void rf_ReleaseStripeLock(RF_Raid_t *, RF_LockTableEntry_t *, RF_StripeNum_t, RF_LockReqDesc_t *);
    111 
    112 #endif				/* !_RF__RF_STRIPELOCKS_H_ */
    113