Home | History | Annotate | Line # | Download | only in raidframe
rf_diskqueue.c revision 1.3
      1  1.3  oster /*	$NetBSD: rf_diskqueue.c,v 1.3 1999/01/14 20:29:38 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  * Author: Mark Holland
      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  *
     31  1.1  oster  * rf_diskqueue.c -- higher-level disk queue code
     32  1.1  oster  *
     33  1.1  oster  * the routines here are a generic wrapper around the actual queueing
     34  1.1  oster  * routines.  The code here implements thread scheduling, synchronization,
     35  1.1  oster  * and locking ops (see below) on top of the lower-level queueing code.
     36  1.1  oster  *
     37  1.1  oster  * to support atomic RMW, we implement "locking operations".  When a locking op
     38  1.1  oster  * is dispatched to the lower levels of the driver, the queue is locked, and no further
     39  1.1  oster  * I/Os are dispatched until the queue receives & completes a corresponding "unlocking
     40  1.1  oster  * operation".  This code relies on the higher layers to guarantee that a locking
     41  1.1  oster  * op will always be eventually followed by an unlocking op.  The model is that
     42  1.1  oster  * the higher layers are structured so locking and unlocking ops occur in pairs, i.e.
     43  1.1  oster  * an unlocking op cannot be generated until after a locking op reports completion.
     44  1.1  oster  * There is no good way to check to see that an unlocking op "corresponds" to the
     45  1.1  oster  * op that currently has the queue locked, so we make no such attempt.  Since by
     46  1.1  oster  * definition there can be only one locking op outstanding on a disk, this should
     47  1.1  oster  * not be a problem.
     48  1.1  oster  *
     49  1.1  oster  * In the kernel, we allow multiple I/Os to be concurrently dispatched to the disk
     50  1.1  oster  * driver.  In order to support locking ops in this environment, when we decide to
     51  1.1  oster  * do a locking op, we stop dispatching new I/Os and wait until all dispatched I/Os
     52  1.1  oster  * have completed before dispatching the locking op.
     53  1.1  oster  *
     54  1.1  oster  * Unfortunately, the code is different in the 3 different operating states
     55  1.1  oster  * (user level, kernel, simulator).  In the kernel, I/O is non-blocking, and
     56  1.1  oster  * we have no disk threads to dispatch for us.  Therefore, we have to dispatch
     57  1.1  oster  * new I/Os to the scsi driver at the time of enqueue, and also at the time
     58  1.1  oster  * of completion.  At user level, I/O is blocking, and so only the disk threads
     59  1.1  oster  * may dispatch I/Os.  Thus at user level, all we can do at enqueue time is
     60  1.1  oster  * enqueue and wake up the disk thread to do the dispatch.
     61  1.1  oster  *
     62  1.1  oster  ***************************************************************************************/
     63  1.1  oster 
     64  1.1  oster /*
     65  1.1  oster  * :
     66  1.1  oster  *
     67  1.1  oster  * Log: rf_diskqueue.c,v
     68  1.1  oster  * Revision 1.50  1996/08/07 21:08:38  jimz
     69  1.1  oster  * b_proc -> kb_proc
     70  1.1  oster  *
     71  1.1  oster  * Revision 1.49  1996/07/05  20:36:14  jimz
     72  1.1  oster  * make rf_ConfigureDiskQueueSystem return 0
     73  1.1  oster  *
     74  1.1  oster  * Revision 1.48  1996/06/18  20:53:11  jimz
     75  1.1  oster  * fix up disk queueing (remove configure routine,
     76  1.1  oster  * add shutdown list arg to create routines)
     77  1.1  oster  *
     78  1.1  oster  * Revision 1.47  1996/06/14  14:16:36  jimz
     79  1.1  oster  * fix handling of bogus queue type
     80  1.1  oster  *
     81  1.1  oster  * Revision 1.46  1996/06/13  20:41:44  jimz
     82  1.1  oster  * add scan, cscan, random queueing
     83  1.1  oster  *
     84  1.1  oster  * Revision 1.45  1996/06/11  01:27:50  jimz
     85  1.1  oster  * Fixed bug where diskthread shutdown would crash or hang. This
     86  1.1  oster  * turned out to be two distinct bugs:
     87  1.1  oster  * (1) [crash] The thread shutdown code wasn't properly waiting for
     88  1.1  oster  * all the diskthreads to complete. This caused diskthreads that were
     89  1.1  oster  * exiting+cleaning up to unlock a destroyed mutex.
     90  1.1  oster  * (2) [hang] TerminateDiskQueues wasn't locking, and DiskIODequeue
     91  1.1  oster  * only checked for termination _after_ a wakeup if the queues were
     92  1.1  oster  * empty. This was a race where the termination wakeup could be lost
     93  1.1  oster  * by the dequeueing thread, and the system would hang waiting for the
     94  1.1  oster  * thread to exit, while the thread waited for an I/O or a signal to
     95  1.1  oster  * check the termination flag.
     96  1.1  oster  *
     97  1.1  oster  * Revision 1.44  1996/06/10  11:55:47  jimz
     98  1.1  oster  * Straightened out some per-array/not-per-array distinctions, fixed
     99  1.1  oster  * a couple bugs related to confusion. Added shutdown lists. Removed
    100  1.1  oster  * layout shutdown function (now subsumed by shutdown lists).
    101  1.1  oster  *
    102  1.1  oster  * Revision 1.43  1996/06/09  02:36:46  jimz
    103  1.1  oster  * lots of little crufty cleanup- fixup whitespace
    104  1.1  oster  * issues, comment #ifdefs, improve typing in some
    105  1.1  oster  * places (esp size-related)
    106  1.1  oster  *
    107  1.1  oster  * Revision 1.42  1996/06/07  22:26:27  jimz
    108  1.1  oster  * type-ify which_ru (RF_ReconUnitNum_t)
    109  1.1  oster  *
    110  1.1  oster  * Revision 1.41  1996/06/07  21:33:04  jimz
    111  1.1  oster  * begin using consistent types for sector numbers,
    112  1.1  oster  * stripe numbers, row+col numbers, recon unit numbers
    113  1.1  oster  *
    114  1.1  oster  * Revision 1.40  1996/06/06  17:28:04  jimz
    115  1.1  oster  * track sector number of last I/O dequeued
    116  1.1  oster  *
    117  1.1  oster  * Revision 1.39  1996/06/06  01:14:13  jimz
    118  1.1  oster  * fix crashing bug when tracerec is NULL (ie, from copyback)
    119  1.1  oster  * initialize req->queue
    120  1.1  oster  *
    121  1.1  oster  * Revision 1.38  1996/06/05  19:38:32  jimz
    122  1.1  oster  * fixed up disk queueing types config
    123  1.1  oster  * added sstf disk queueing
    124  1.1  oster  * fixed exit bug on diskthreads (ref-ing bad mem)
    125  1.1  oster  *
    126  1.1  oster  * Revision 1.37  1996/06/05  18:06:02  jimz
    127  1.1  oster  * Major code cleanup. The Great Renaming is now done.
    128  1.1  oster  * Better modularity. Better typing. Fixed a bunch of
    129  1.1  oster  * synchronization bugs. Made a lot of global stuff
    130  1.1  oster  * per-desc or per-array. Removed dead code.
    131  1.1  oster  *
    132  1.1  oster  * Revision 1.36  1996/05/30  23:22:16  jimz
    133  1.1  oster  * bugfixes of serialization, timing problems
    134  1.1  oster  * more cleanup
    135  1.1  oster  *
    136  1.1  oster  * Revision 1.35  1996/05/30  12:59:18  jimz
    137  1.1  oster  * make etimer happier, more portable
    138  1.1  oster  *
    139  1.1  oster  * Revision 1.34  1996/05/30  11:29:41  jimz
    140  1.1  oster  * Numerous bug fixes. Stripe lock release code disagreed with the taking code
    141  1.1  oster  * about when stripes should be locked (I made it consistent: no parity, no lock)
    142  1.1  oster  * There was a lot of extra serialization of I/Os which I've removed- a lot of
    143  1.1  oster  * it was to calculate values for the cache code, which is no longer with us.
    144  1.1  oster  * More types, function, macro cleanup. Added code to properly quiesce the array
    145  1.1  oster  * on shutdown. Made a lot of stuff array-specific which was (bogusly) general
    146  1.1  oster  * before. Fixed memory allocation, freeing bugs.
    147  1.1  oster  *
    148  1.1  oster  * Revision 1.33  1996/05/27  18:56:37  jimz
    149  1.1  oster  * more code cleanup
    150  1.1  oster  * better typing
    151  1.1  oster  * compiles in all 3 environments
    152  1.1  oster  *
    153  1.1  oster  * Revision 1.32  1996/05/24  22:17:04  jimz
    154  1.1  oster  * continue code + namespace cleanup
    155  1.1  oster  * typed a bunch of flags
    156  1.1  oster  *
    157  1.1  oster  * Revision 1.31  1996/05/24  01:59:45  jimz
    158  1.1  oster  * another checkpoint in code cleanup for release
    159  1.1  oster  * time to sync kernel tree
    160  1.1  oster  *
    161  1.1  oster  * Revision 1.30  1996/05/23  21:46:35  jimz
    162  1.1  oster  * checkpoint in code cleanup (release prep)
    163  1.1  oster  * lots of types, function names have been fixed
    164  1.1  oster  *
    165  1.1  oster  * Revision 1.29  1996/05/23  00:33:23  jimz
    166  1.1  oster  * code cleanup: move all debug decls to rf_options.c, all extern
    167  1.1  oster  * debug decls to rf_options.h, all debug vars preceded by rf_
    168  1.1  oster  *
    169  1.1  oster  * Revision 1.28  1996/05/20  16:14:29  jimz
    170  1.1  oster  * switch to rf_{mutex,cond}_{init,destroy}
    171  1.1  oster  *
    172  1.1  oster  * Revision 1.27  1996/05/18  19:51:34  jimz
    173  1.1  oster  * major code cleanup- fix syntax, make some types consistent,
    174  1.1  oster  * add prototypes, clean out dead code, et cetera
    175  1.1  oster  *
    176  1.1  oster  * Revision 1.26  1996/05/16  19:21:49  wvcii
    177  1.1  oster  * fixed typo in init_dqd
    178  1.1  oster  *
    179  1.1  oster  * Revision 1.25  1996/05/16  16:02:51  jimz
    180  1.1  oster  * switch to RF_FREELIST stuff for DiskQueueData
    181  1.1  oster  *
    182  1.1  oster  * Revision 1.24  1996/05/10  16:24:14  jimz
    183  1.1  oster  * new cvscan function names
    184  1.1  oster  *
    185  1.1  oster  * Revision 1.23  1996/05/01  16:27:54  jimz
    186  1.1  oster  * don't use ccmn bp management
    187  1.1  oster  *
    188  1.1  oster  * Revision 1.22  1995/12/12  18:10:06  jimz
    189  1.1  oster  * MIN -> RF_MIN, MAX -> RF_MAX, ASSERT -> RF_ASSERT
    190  1.1  oster  * fix 80-column brain damage in comments
    191  1.1  oster  *
    192  1.1  oster  * Revision 1.21  1995/12/01  15:59:59  root
    193  1.1  oster  * added copyright info
    194  1.1  oster  *
    195  1.1  oster  * Revision 1.20  1995/11/07  16:27:20  wvcii
    196  1.1  oster  * added Peek() function to diskqueuesw
    197  1.1  oster  * non-locking accesses are never blocked (assume clients enforce proper
    198  1.1  oster  * respect for lock acquisition)
    199  1.1  oster  *
    200  1.1  oster  * Revision 1.19  1995/10/05  18:56:52  jimz
    201  1.1  oster  * fix req handling in IOComplete
    202  1.1  oster  *
    203  1.1  oster  * Revision 1.18  1995/10/04  20:13:50  wvcii
    204  1.1  oster  * added asserts to monitor numOutstanding queueLength
    205  1.1  oster  *
    206  1.1  oster  * Revision 1.17  1995/10/04  07:43:52  wvcii
    207  1.1  oster  * queue->numOutstanding now valid for user & sim
    208  1.1  oster  * added queue->queueLength
    209  1.1  oster  * user tested & verified, sim untested
    210  1.1  oster  *
    211  1.1  oster  * Revision 1.16  1995/09/12  00:21:19  wvcii
    212  1.1  oster  * added support for tracing disk queue time
    213  1.1  oster  *
    214  1.1  oster  */
    215  1.1  oster 
    216  1.1  oster #include "rf_types.h"
    217  1.1  oster #include "rf_threadstuff.h"
    218  1.1  oster #include "rf_threadid.h"
    219  1.1  oster #include "rf_raid.h"
    220  1.1  oster #include "rf_diskqueue.h"
    221  1.1  oster #include "rf_alloclist.h"
    222  1.1  oster #include "rf_acctrace.h"
    223  1.1  oster #include "rf_etimer.h"
    224  1.1  oster #include "rf_configure.h"
    225  1.1  oster #include "rf_general.h"
    226  1.1  oster #include "rf_freelist.h"
    227  1.1  oster #include "rf_debugprint.h"
    228  1.1  oster #include "rf_shutdown.h"
    229  1.1  oster #include "rf_cvscan.h"
    230  1.1  oster #include "rf_sstf.h"
    231  1.1  oster #include "rf_fifo.h"
    232  1.1  oster 
    233  1.1  oster #ifdef SIMULATE
    234  1.1  oster #include "rf_diskevent.h"
    235  1.1  oster #endif /* SIMULATE */
    236  1.1  oster 
    237  1.1  oster #if !defined(__NetBSD__)
    238  1.1  oster extern struct buf *ubc_bufget();
    239  1.1  oster #endif
    240  1.1  oster 
    241  1.1  oster static int init_dqd(RF_DiskQueueData_t *);
    242  1.1  oster static void clean_dqd(RF_DiskQueueData_t *);
    243  1.1  oster static void rf_ShutdownDiskQueueSystem(void *);
    244  1.1  oster /* From rf_kintf.c */
    245  1.1  oster int rf_DispatchKernelIO(RF_DiskQueue_t *,RF_DiskQueueData_t *);
    246  1.1  oster 
    247  1.1  oster 
    248  1.1  oster #define Dprintf1(s,a)         if (rf_queueDebug) rf_debug_printf(s,(void *)((unsigned long)a),NULL,NULL,NULL,NULL,NULL,NULL,NULL)
    249  1.1  oster #define Dprintf2(s,a,b)       if (rf_queueDebug) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),NULL,NULL,NULL,NULL,NULL,NULL)
    250  1.1  oster #define Dprintf3(s,a,b,c)     if (rf_queueDebug) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),NULL,NULL,NULL,NULL,NULL)
    251  1.1  oster #define Dprintf4(s,a,b,c,d)   if (rf_queueDebug) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),(void *)((unsigned long)d),NULL,NULL,NULL,NULL)
    252  1.1  oster #define Dprintf5(s,a,b,c,d,e) if (rf_queueDebug) 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)
    253  1.1  oster 
    254  1.1  oster #if !defined(KERNEL) && !defined(SIMULATE)
    255  1.1  oster 
    256  1.1  oster /* queue must be locked before invoking this */
    257  1.1  oster #define SIGNAL_DISK_QUEUE(_q_,_wh_)  \
    258  1.1  oster {                                    \
    259  1.1  oster   if ( (_q_)->numWaiting > 0) {      \
    260  1.1  oster     (_q_)->numWaiting--;             \
    261  1.1  oster     RF_SIGNAL_COND( ((_q_)->cond) );    \
    262  1.1  oster   }                                  \
    263  1.1  oster }
    264  1.1  oster 
    265  1.1  oster /* queue must be locked before invoking this */
    266  1.1  oster #define WAIT_DISK_QUEUE(_q_,_wh_)                                         \
    267  1.1  oster {                                                                         \
    268  1.1  oster   (_q_)->numWaiting++;                                                    \
    269  1.1  oster   RF_WAIT_COND( ((_q_)->cond), ((_q_)->mutex) );                             \
    270  1.1  oster }
    271  1.1  oster 
    272  1.1  oster #else /* !defined(KERNEL) && !defined(SIMULATE) */
    273  1.1  oster 
    274  1.1  oster #define SIGNAL_DISK_QUEUE(_q_,_wh_)
    275  1.1  oster #define WAIT_DISK_QUEUE(_q_,_wh_)
    276  1.1  oster 
    277  1.1  oster #endif /* !defined(KERNEL) && !defined(SIMULATE) */
    278  1.1  oster 
    279  1.1  oster /*****************************************************************************************
    280  1.1  oster  *
    281  1.1  oster  * the disk queue switch defines all the functions used in the different queueing
    282  1.1  oster  * disciplines
    283  1.1  oster  *    queue ID, init routine, enqueue routine, dequeue routine
    284  1.1  oster  *
    285  1.1  oster  ****************************************************************************************/
    286  1.1  oster 
    287  1.1  oster static RF_DiskQueueSW_t diskqueuesw[] = {
    288  1.1  oster 	{"fifo", /* FIFO */
    289  1.1  oster 	rf_FifoCreate,
    290  1.1  oster 	rf_FifoEnqueue,
    291  1.1  oster 	rf_FifoDequeue,
    292  1.1  oster 	rf_FifoPeek,
    293  1.1  oster 	rf_FifoPromote},
    294  1.1  oster 
    295  1.1  oster 	{"cvscan", /* cvscan */
    296  1.1  oster 	rf_CvscanCreate,
    297  1.1  oster 	rf_CvscanEnqueue,
    298  1.1  oster 	rf_CvscanDequeue,
    299  1.1  oster 	rf_CvscanPeek,
    300  1.1  oster 	rf_CvscanPromote },
    301  1.1  oster 
    302  1.1  oster 	{"sstf", /* shortest seek time first */
    303  1.1  oster 	rf_SstfCreate,
    304  1.1  oster 	rf_SstfEnqueue,
    305  1.1  oster 	rf_SstfDequeue,
    306  1.1  oster 	rf_SstfPeek,
    307  1.1  oster 	rf_SstfPromote},
    308  1.1  oster 
    309  1.1  oster 	{"scan", /* SCAN (two-way elevator) */
    310  1.1  oster 	rf_ScanCreate,
    311  1.1  oster 	rf_SstfEnqueue,
    312  1.1  oster 	rf_ScanDequeue,
    313  1.1  oster 	rf_ScanPeek,
    314  1.1  oster 	rf_SstfPromote},
    315  1.1  oster 
    316  1.1  oster 	{"cscan", /* CSCAN (one-way elevator) */
    317  1.1  oster 	rf_CscanCreate,
    318  1.1  oster 	rf_SstfEnqueue,
    319  1.1  oster 	rf_CscanDequeue,
    320  1.1  oster 	rf_CscanPeek,
    321  1.1  oster 	rf_SstfPromote},
    322  1.1  oster 
    323  1.1  oster #if !defined(KERNEL) && RF_INCLUDE_QUEUE_RANDOM > 0
    324  1.1  oster 	/* to make a point to Chris :-> */
    325  1.1  oster 	{"random", /* random */
    326  1.1  oster 	rf_FifoCreate,
    327  1.1  oster 	rf_FifoEnqueue,
    328  1.1  oster 	rf_RandomDequeue,
    329  1.1  oster 	rf_RandomPeek,
    330  1.1  oster 	rf_FifoPromote},
    331  1.1  oster #endif /* !KERNEL && RF_INCLUDE_QUEUE_RANDOM > 0 */
    332  1.1  oster };
    333  1.1  oster #define NUM_DISK_QUEUE_TYPES (sizeof(diskqueuesw)/sizeof(RF_DiskQueueSW_t))
    334  1.1  oster 
    335  1.1  oster static RF_FreeList_t *rf_dqd_freelist;
    336  1.1  oster 
    337  1.1  oster #define RF_MAX_FREE_DQD 256
    338  1.1  oster #define RF_DQD_INC       16
    339  1.1  oster #define RF_DQD_INITIAL   64
    340  1.1  oster 
    341  1.1  oster #ifdef __NetBSD__
    342  1.1  oster #ifdef _KERNEL
    343  1.1  oster #include <sys/buf.h>
    344  1.1  oster #endif
    345  1.1  oster #endif
    346  1.1  oster 
    347  1.1  oster static int init_dqd(dqd)
    348  1.1  oster   RF_DiskQueueData_t  *dqd;
    349  1.1  oster {
    350  1.1  oster #ifdef KERNEL
    351  1.1  oster #ifdef __NetBSD__
    352  1.1  oster 	/* XXX not sure if the following malloc is appropriate... probably not quite... */
    353  1.1  oster 	dqd->bp = (struct buf *) malloc( sizeof(struct buf), M_DEVBUF, M_NOWAIT);
    354  1.1  oster 	/* XXX */
    355  1.1  oster 	/* printf("NEED TO IMPLEMENT THIS BETTER!\n"); */
    356  1.1  oster #else
    357  1.1  oster 	dqd->bp = ubc_bufget();
    358  1.1  oster #endif
    359  1.1  oster 	if (dqd->bp == NULL) {
    360  1.1  oster 		return(ENOMEM);
    361  1.1  oster 	}
    362  1.3  oster #ifdef __NetBSD__
    363  1.3  oster 	memset(dqd->bp,0,sizeof(struct buf)); /* if you don't do it, nobody else will.. */
    364  1.3  oster #endif
    365  1.1  oster #endif /* KERNEL */
    366  1.1  oster 	return(0);
    367  1.1  oster }
    368  1.1  oster 
    369  1.1  oster static void clean_dqd(dqd)
    370  1.1  oster   RF_DiskQueueData_t  *dqd;
    371  1.1  oster {
    372  1.1  oster #ifdef KERNEL
    373  1.1  oster #ifdef __NetBSD__
    374  1.1  oster 	/* printf("NEED TO IMPLEMENT THIS BETTER(2)!\n"); */
    375  1.1  oster 	/* XXX ? */
    376  1.1  oster 	free( dqd->bp, M_DEVBUF );
    377  1.1  oster #else
    378  1.1  oster     ubc_buffree(dqd->bp);
    379  1.1  oster #endif
    380  1.1  oster 
    381  1.1  oster #endif /* KERNEL */
    382  1.1  oster }
    383  1.1  oster 
    384  1.1  oster /* configures a single disk queue */
    385  1.1  oster static int config_disk_queue(
    386  1.1  oster   RF_Raid_t            *raidPtr,
    387  1.1  oster   RF_DiskQueue_t       *diskqueue,
    388  1.1  oster   RF_RowCol_t           r, /* row & col -- debug only.  BZZT not any more... */
    389  1.1  oster   RF_RowCol_t           c,
    390  1.1  oster   RF_DiskQueueSW_t     *p,
    391  1.1  oster   RF_SectorCount_t      sectPerDisk,
    392  1.1  oster   dev_t                 dev,
    393  1.1  oster   int                   maxOutstanding,
    394  1.1  oster   RF_ShutdownList_t   **listp,
    395  1.1  oster   RF_AllocListElem_t   *clList)
    396  1.1  oster {
    397  1.1  oster   int rc;
    398  1.1  oster 
    399  1.1  oster   diskqueue->row = r;
    400  1.1  oster   diskqueue->col = c;
    401  1.1  oster   diskqueue->qPtr = p;
    402  1.1  oster   diskqueue->qHdr = (p->Create)(sectPerDisk, clList, listp);
    403  1.1  oster   diskqueue->dev  = dev;
    404  1.1  oster   diskqueue->numOutstanding = 0;
    405  1.1  oster   diskqueue->queueLength = 0;
    406  1.1  oster   diskqueue->maxOutstanding = maxOutstanding;
    407  1.1  oster   diskqueue->curPriority    = RF_IO_NORMAL_PRIORITY;
    408  1.1  oster   diskqueue->nextLockingOp  = NULL;
    409  1.1  oster   diskqueue->unlockingOp    = NULL;
    410  1.1  oster   diskqueue->numWaiting=0;
    411  1.1  oster   diskqueue->flags = 0;
    412  1.1  oster   diskqueue->raidPtr = raidPtr;
    413  1.1  oster #if defined(__NetBSD__) && defined(_KERNEL)
    414  1.1  oster   diskqueue->rf_cinfo = &raidPtr->raid_cinfo[r][c];
    415  1.1  oster #endif
    416  1.1  oster   rc = rf_create_managed_mutex(listp, &diskqueue->mutex);
    417  1.1  oster   if (rc) {
    418  1.1  oster     RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    419  1.1  oster       __LINE__, rc);
    420  1.1  oster     return(rc);
    421  1.1  oster   }
    422  1.1  oster   rc = rf_create_managed_cond(listp, &diskqueue->cond);
    423  1.1  oster   if (rc) {
    424  1.1  oster     RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", __FILE__,
    425  1.1  oster       __LINE__, rc);
    426  1.1  oster     return(rc);
    427  1.1  oster   }
    428  1.1  oster   return(0);
    429  1.1  oster }
    430  1.1  oster 
    431  1.1  oster static void rf_ShutdownDiskQueueSystem(ignored)
    432  1.1  oster   void  *ignored;
    433  1.1  oster {
    434  1.1  oster   RF_FREELIST_DESTROY_CLEAN(rf_dqd_freelist,next,(RF_DiskQueueData_t *),clean_dqd);
    435  1.1  oster }
    436  1.1  oster 
    437  1.1  oster int rf_ConfigureDiskQueueSystem(listp)
    438  1.1  oster   RF_ShutdownList_t  **listp;
    439  1.1  oster {
    440  1.1  oster   int rc;
    441  1.1  oster 
    442  1.1  oster   RF_FREELIST_CREATE(rf_dqd_freelist, RF_MAX_FREE_DQD,
    443  1.1  oster     RF_DQD_INC, sizeof(RF_DiskQueueData_t));
    444  1.1  oster   if (rf_dqd_freelist == NULL)
    445  1.1  oster     return(ENOMEM);
    446  1.1  oster   rc = rf_ShutdownCreate(listp, rf_ShutdownDiskQueueSystem, NULL);
    447  1.1  oster   if (rc) {
    448  1.1  oster     RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
    449  1.1  oster       __FILE__, __LINE__, rc);
    450  1.1  oster     rf_ShutdownDiskQueueSystem(NULL);
    451  1.1  oster     return(rc);
    452  1.1  oster   }
    453  1.1  oster   RF_FREELIST_PRIME_INIT(rf_dqd_freelist, RF_DQD_INITIAL,next,
    454  1.1  oster     (RF_DiskQueueData_t *),init_dqd);
    455  1.1  oster   return(0);
    456  1.1  oster }
    457  1.1  oster 
    458  1.1  oster #ifndef KERNEL
    459  1.1  oster /* this is called prior to shutdown to wakeup everyone waiting on a disk queue
    460  1.1  oster  * and tell them to exit
    461  1.1  oster  */
    462  1.1  oster void rf_TerminateDiskQueues(raidPtr)
    463  1.1  oster   RF_Raid_t  *raidPtr;
    464  1.1  oster {
    465  1.1  oster   RF_RowCol_t r, c;
    466  1.1  oster 
    467  1.1  oster   raidPtr->terminate_disk_queues = 1;
    468  1.1  oster   for (r=0; r<raidPtr->numRow; r++) {
    469  1.1  oster     for (c=0; c<raidPtr->numCol + ((r==0) ? raidPtr->numSpare : 0); c++) {
    470  1.1  oster       RF_LOCK_QUEUE_MUTEX(&raidPtr->Queues[r][c], "TerminateDiskQueues");
    471  1.1  oster       RF_BROADCAST_COND(raidPtr->Queues[r][c].cond);
    472  1.1  oster       RF_UNLOCK_QUEUE_MUTEX(&raidPtr->Queues[r][c], "TerminateDiskQueues");
    473  1.1  oster     }
    474  1.1  oster   }
    475  1.1  oster }
    476  1.1  oster #endif /* !KERNEL */
    477  1.1  oster 
    478  1.1  oster int rf_ConfigureDiskQueues(
    479  1.1  oster   RF_ShutdownList_t  **listp,
    480  1.1  oster   RF_Raid_t           *raidPtr,
    481  1.1  oster   RF_Config_t         *cfgPtr)
    482  1.1  oster {
    483  1.1  oster   RF_DiskQueue_t **diskQueues, *spareQueues;
    484  1.1  oster   RF_DiskQueueSW_t *p;
    485  1.1  oster   RF_RowCol_t r, c;
    486  1.1  oster   int rc, i;
    487  1.1  oster 
    488  1.1  oster   raidPtr->maxQueueDepth = cfgPtr->maxOutstandingDiskReqs;
    489  1.1  oster 
    490  1.1  oster   for(p=NULL,i=0;i<NUM_DISK_QUEUE_TYPES;i++) {
    491  1.1  oster     if (!strcmp(diskqueuesw[i].queueType, cfgPtr->diskQueueType)) {
    492  1.1  oster       p = &diskqueuesw[i];
    493  1.1  oster       break;
    494  1.1  oster     }
    495  1.1  oster   }
    496  1.1  oster   if (p == NULL) {
    497  1.1  oster     RF_ERRORMSG2("Unknown queue type \"%s\".  Using %s\n",cfgPtr->diskQueueType, diskqueuesw[0].queueType);
    498  1.1  oster     p = &diskqueuesw[0];
    499  1.1  oster   }
    500  1.1  oster 
    501  1.1  oster   RF_CallocAndAdd(diskQueues, raidPtr->numRow, sizeof(RF_DiskQueue_t *), (RF_DiskQueue_t **), raidPtr->cleanupList);
    502  1.1  oster   if (diskQueues == NULL) {
    503  1.1  oster     return(ENOMEM);
    504  1.1  oster   }
    505  1.1  oster   raidPtr->Queues = diskQueues;
    506  1.1  oster   for (r=0; r<raidPtr->numRow; r++) {
    507  1.1  oster     RF_CallocAndAdd(diskQueues[r], raidPtr->numCol + ((r==0) ? raidPtr->numSpare : 0), sizeof(RF_DiskQueue_t), (RF_DiskQueue_t *), raidPtr->cleanupList);
    508  1.1  oster     if (diskQueues[r] == NULL)
    509  1.1  oster       return(ENOMEM);
    510  1.1  oster     for (c=0; c<raidPtr->numCol; c++) {
    511  1.1  oster       rc = config_disk_queue(raidPtr, &diskQueues[r][c], r, c, p,
    512  1.1  oster         raidPtr->sectorsPerDisk, raidPtr->Disks[r][c].dev,
    513  1.1  oster         cfgPtr->maxOutstandingDiskReqs, listp, raidPtr->cleanupList);
    514  1.1  oster       if (rc)
    515  1.1  oster         return(rc);
    516  1.1  oster     }
    517  1.1  oster   }
    518  1.1  oster 
    519  1.1  oster   spareQueues = &raidPtr->Queues[0][raidPtr->numCol];
    520  1.1  oster   for (r=0; r<raidPtr->numSpare; r++) {
    521  1.1  oster 	  rc = config_disk_queue(raidPtr, &spareQueues[r],
    522  1.1  oster 				 0, raidPtr->numCol+r, p,
    523  1.1  oster 				 raidPtr->sectorsPerDisk,
    524  1.1  oster 				 raidPtr->Disks[0][raidPtr->numCol+r].dev,
    525  1.1  oster 				 cfgPtr->maxOutstandingDiskReqs, listp,
    526  1.1  oster 				 raidPtr->cleanupList);
    527  1.1  oster     if (rc)
    528  1.1  oster       return(rc);
    529  1.1  oster   }
    530  1.1  oster   return(0);
    531  1.1  oster }
    532  1.1  oster 
    533  1.1  oster /* Enqueue a disk I/O
    534  1.1  oster  *
    535  1.1  oster  * Unfortunately, we have to do things differently in the different
    536  1.1  oster  * environments (simulator, user-level, kernel).
    537  1.1  oster  * At user level, all I/O is blocking, so we have 1 or more threads/disk
    538  1.1  oster  * and the thread that enqueues is different from the thread that dequeues.
    539  1.1  oster  * In the kernel, I/O is non-blocking and so we'd like to have multiple
    540  1.1  oster  * I/Os outstanding on the physical disks when possible.
    541  1.1  oster  *
    542  1.1  oster  * when any request arrives at a queue, we have two choices:
    543  1.1  oster  *    dispatch it to the lower levels
    544  1.1  oster  *    queue it up
    545  1.1  oster  *
    546  1.1  oster  * kernel rules for when to do what:
    547  1.1  oster  *    locking request:  queue empty => dispatch and lock queue,
    548  1.1  oster  *                      else queue it
    549  1.1  oster  *    unlocking req  :  always dispatch it
    550  1.1  oster  *    normal req     :  queue empty => dispatch it & set priority
    551  1.1  oster  *                      queue not full & priority is ok => dispatch it
    552  1.1  oster  *                      else queue it
    553  1.1  oster  *
    554  1.1  oster  * user-level rules:
    555  1.1  oster  *    always enqueue.  In the special case of an unlocking op, enqueue
    556  1.1  oster  *    in a special way that will cause the unlocking op to be the next
    557  1.1  oster  *    thing dequeued.
    558  1.1  oster  *
    559  1.1  oster  * simulator rules:
    560  1.1  oster  *    Do the same as at user level, with the sleeps and wakeups suppressed.
    561  1.1  oster  */
    562  1.1  oster void rf_DiskIOEnqueue(queue, req, pri)
    563  1.1  oster   RF_DiskQueue_t      *queue;
    564  1.1  oster   RF_DiskQueueData_t  *req;
    565  1.1  oster   int                  pri;
    566  1.1  oster {
    567  1.1  oster   int tid;
    568  1.1  oster 
    569  1.1  oster   RF_ETIMER_START(req->qtime);
    570  1.1  oster   rf_get_threadid(tid);
    571  1.1  oster   RF_ASSERT(req->type == RF_IO_TYPE_NOP || req->numSector);
    572  1.1  oster   req->priority = pri;
    573  1.1  oster 
    574  1.1  oster   if (rf_queueDebug && (req->numSector == 0)) {
    575  1.1  oster     printf("Warning: Enqueueing zero-sector access\n");
    576  1.1  oster   }
    577  1.1  oster 
    578  1.1  oster #ifdef KERNEL
    579  1.1  oster   /*
    580  1.1  oster    * kernel
    581  1.1  oster    */
    582  1.1  oster   RF_LOCK_QUEUE_MUTEX( queue, "DiskIOEnqueue" );
    583  1.1  oster   /* locking request */
    584  1.1  oster   if (RF_LOCKING_REQ(req)) {
    585  1.1  oster     if (RF_QUEUE_EMPTY(queue)) {
    586  1.1  oster       Dprintf3("Dispatching pri %d locking op to r %d c %d (queue empty)\n",pri,queue->row, queue->col);
    587  1.1  oster       RF_LOCK_QUEUE(queue);
    588  1.1  oster       rf_DispatchKernelIO(queue, req);
    589  1.1  oster     } else {
    590  1.1  oster       queue->queueLength++;  /* increment count of number of requests waiting in this queue */
    591  1.1  oster       Dprintf3("Enqueueing pri %d locking op to r %d c %d (queue not empty)\n",pri,queue->row, queue->col);
    592  1.1  oster       req->queue = (void *)queue;
    593  1.1  oster       (queue->qPtr->Enqueue)(queue->qHdr, req, pri);
    594  1.1  oster     }
    595  1.1  oster   }
    596  1.1  oster   /* unlocking request */
    597  1.1  oster   else if (RF_UNLOCKING_REQ(req)) {           /* we'll do the actual unlock when this I/O completes */
    598  1.1  oster     Dprintf3("Dispatching pri %d unlocking op to r %d c %d\n",pri,queue->row, queue->col);
    599  1.1  oster     RF_ASSERT(RF_QUEUE_LOCKED(queue));
    600  1.1  oster     rf_DispatchKernelIO(queue, req);
    601  1.1  oster   }
    602  1.1  oster   /* normal request */
    603  1.1  oster   else if (RF_OK_TO_DISPATCH(queue, req)) {
    604  1.1  oster     Dprintf3("Dispatching pri %d regular op to r %d c %d (ok to dispatch)\n",pri,queue->row, queue->col);
    605  1.1  oster     rf_DispatchKernelIO(queue, req);
    606  1.1  oster   } else {
    607  1.1  oster     queue->queueLength++;  /* increment count of number of requests waiting in this queue */
    608  1.1  oster     Dprintf3("Enqueueing pri %d regular op to r %d c %d (not ok to dispatch)\n",pri,queue->row, queue->col);
    609  1.1  oster     req->queue = (void *)queue;
    610  1.1  oster     (queue->qPtr->Enqueue)(queue->qHdr, req, pri);
    611  1.1  oster   }
    612  1.1  oster   RF_UNLOCK_QUEUE_MUTEX( queue, "DiskIOEnqueue" );
    613  1.1  oster 
    614  1.1  oster #else /* KERNEL */
    615  1.1  oster   /*
    616  1.1  oster    * user-level
    617  1.1  oster    */
    618  1.1  oster   RF_LOCK_QUEUE_MUTEX( queue, "DiskIOEnqueue" );
    619  1.1  oster   queue->queueLength++;  /* increment count of number of requests waiting in this queue */
    620  1.1  oster   /* unlocking request */
    621  1.1  oster   if (RF_UNLOCKING_REQ(req)) {
    622  1.1  oster     Dprintf4("[%d] enqueueing pri %d unlocking op & signalling r %d c %d\n", tid, pri, queue->row, queue->col);
    623  1.1  oster     RF_ASSERT(RF_QUEUE_LOCKED(queue) && queue->unlockingOp == NULL);
    624  1.1  oster     queue->unlockingOp = req;
    625  1.1  oster   }
    626  1.1  oster   /* locking and normal requests */
    627  1.1  oster   else {
    628  1.1  oster     req->queue = (void *)queue;
    629  1.1  oster     Dprintf5("[%d] enqueueing pri %d %s op & signalling r %d c %d\n", tid, pri,
    630  1.1  oster 	     (RF_LOCKING_REQ(req)) ? "locking" : "regular",queue->row,queue->col);
    631  1.1  oster     (queue->qPtr->Enqueue)(queue->qHdr, req, pri);
    632  1.1  oster   }
    633  1.1  oster   SIGNAL_DISK_QUEUE( queue, "DiskIOEnqueue");
    634  1.1  oster   RF_UNLOCK_QUEUE_MUTEX( queue, "DiskIOEnqueue" );
    635  1.1  oster #endif /* KERNEL */
    636  1.1  oster }
    637  1.1  oster 
    638  1.1  oster #if !defined(KERNEL) && !defined(SIMULATE)
    639  1.1  oster /* user-level only: tell all threads to wake up & recheck the queue */
    640  1.1  oster void rf_BroadcastOnQueue(queue)
    641  1.1  oster   RF_DiskQueue_t *queue;
    642  1.1  oster {
    643  1.1  oster   int i;
    644  1.1  oster 
    645  1.1  oster   if (queue->maxOutstanding > 1) for (i=0; i<queue->maxOutstanding; i++) {
    646  1.1  oster     SIGNAL_DISK_QUEUE(queue, "BroadcastOnQueue" );
    647  1.1  oster   }
    648  1.1  oster }
    649  1.1  oster #endif /* !KERNEL && !SIMULATE */
    650  1.1  oster 
    651  1.1  oster #ifndef KERNEL /* not used in kernel */
    652  1.1  oster 
    653  1.1  oster RF_DiskQueueData_t *rf_DiskIODequeue(queue)
    654  1.1  oster   RF_DiskQueue_t *queue;
    655  1.1  oster {
    656  1.1  oster   RF_DiskQueueData_t *p, *headItem;
    657  1.1  oster   int tid;
    658  1.1  oster 
    659  1.1  oster   rf_get_threadid(tid);
    660  1.1  oster   RF_LOCK_QUEUE_MUTEX( queue, "DiskIODequeue" );
    661  1.1  oster   for (p=NULL; !p; ) {
    662  1.1  oster     if (queue->unlockingOp) {
    663  1.1  oster       /* unlocking request */
    664  1.1  oster       RF_ASSERT(RF_QUEUE_LOCKED(queue));
    665  1.1  oster       p = queue->unlockingOp;
    666  1.1  oster       queue->unlockingOp = NULL;
    667  1.1  oster       Dprintf4("[%d] dequeueing pri %d unlocking op r %d c %d\n", tid, p->priority, queue->row,queue->col);
    668  1.1  oster     }
    669  1.1  oster     else {
    670  1.1  oster       headItem = (queue->qPtr->Peek)(queue->qHdr);
    671  1.1  oster       if (headItem) {
    672  1.1  oster         if (RF_LOCKING_REQ(headItem)) {
    673  1.1  oster           /* locking request */
    674  1.1  oster           if (!RF_QUEUE_LOCKED(queue)) {
    675  1.1  oster             /* queue isn't locked, so dequeue the request & lock the queue */
    676  1.1  oster             p = (queue->qPtr->Dequeue)( queue->qHdr );
    677  1.1  oster             if (p)
    678  1.1  oster               Dprintf4("[%d] dequeueing pri %d locking op r %d c %d\n", tid, p->priority, queue->row, queue->col);
    679  1.1  oster             else
    680  1.1  oster               Dprintf3("[%d] no dequeue -- raw queue empty r %d c %d\n", tid, queue->row, queue->col);
    681  1.1  oster           }
    682  1.1  oster           else {
    683  1.1  oster             /* queue already locked, no dequeue occurs */
    684  1.1  oster             Dprintf3("[%d] no dequeue -- queue is locked r %d c %d\n", tid, queue->row, queue->col);
    685  1.1  oster             p = NULL;
    686  1.1  oster           }
    687  1.1  oster         }
    688  1.1  oster         else {
    689  1.1  oster           /* normal request, always dequeue and assume caller already has lock (if needed) */
    690  1.1  oster           p = (queue->qPtr->Dequeue)( queue->qHdr );
    691  1.1  oster           if (p)
    692  1.1  oster             Dprintf4("[%d] dequeueing pri %d regular op r %d c %d\n", tid, p->priority, queue->row, queue->col);
    693  1.1  oster           else
    694  1.1  oster             Dprintf3("[%d] no dequeue -- raw queue empty r %d c %d\n", tid, queue->row, queue->col);
    695  1.1  oster         }
    696  1.1  oster       }
    697  1.1  oster       else {
    698  1.1  oster         Dprintf3("[%d] no dequeue -- raw queue empty r %d c %d\n", tid, queue->row, queue->col);
    699  1.1  oster       }
    700  1.1  oster     }
    701  1.1  oster 
    702  1.1  oster     if (queue->raidPtr->terminate_disk_queues) {
    703  1.1  oster       p = NULL;
    704  1.1  oster       break;
    705  1.1  oster     }
    706  1.1  oster #ifdef SIMULATE
    707  1.1  oster     break;		/* in simulator, return NULL on empty queue instead of blocking */
    708  1.1  oster #else /* SIMULATE */
    709  1.1  oster     if (!p) {
    710  1.1  oster       Dprintf3("[%d] nothing to dequeue: waiting r %d c %d\n", tid, queue->row, queue->col);
    711  1.1  oster       WAIT_DISK_QUEUE( queue, "DiskIODequeue" );
    712  1.1  oster     }
    713  1.1  oster #endif /* SIMULATE */
    714  1.1  oster   }
    715  1.1  oster 
    716  1.1  oster   if (p) {
    717  1.1  oster     queue->queueLength--;  /* decrement count of number of requests waiting in this queue */
    718  1.1  oster     RF_ASSERT(queue->queueLength >= 0);
    719  1.1  oster     queue->numOutstanding++;
    720  1.1  oster     queue->last_deq_sector = p->sectorOffset;
    721  1.1  oster     /* record the amount of time this request spent in the disk queue */
    722  1.1  oster     RF_ETIMER_STOP(p->qtime);
    723  1.1  oster     RF_ETIMER_EVAL(p->qtime);
    724  1.1  oster     if (p->tracerec)
    725  1.1  oster       p->tracerec->diskqueue_us += RF_ETIMER_VAL_US(p->qtime);
    726  1.1  oster   }
    727  1.1  oster 
    728  1.1  oster   if (p && RF_LOCKING_REQ(p)) {
    729  1.1  oster     RF_ASSERT(!RF_QUEUE_LOCKED(queue));
    730  1.1  oster     Dprintf3("[%d] locking queue r %d c %d\n",tid,queue->row,queue->col);
    731  1.1  oster     RF_LOCK_QUEUE(queue);
    732  1.1  oster   }
    733  1.1  oster   RF_UNLOCK_QUEUE_MUTEX( queue, "DiskIODequeue" );
    734  1.1  oster 
    735  1.1  oster   return(p);
    736  1.1  oster }
    737  1.1  oster 
    738  1.1  oster #else /* !KERNEL */
    739  1.1  oster 
    740  1.1  oster /* get the next set of I/Os started, kernel version only */
    741  1.1  oster void rf_DiskIOComplete(queue, req, status)
    742  1.1  oster   RF_DiskQueue_t      *queue;
    743  1.1  oster   RF_DiskQueueData_t  *req;
    744  1.1  oster   int                  status;
    745  1.1  oster {
    746  1.1  oster   int done=0;
    747  1.1  oster 
    748  1.1  oster   RF_LOCK_QUEUE_MUTEX( queue, "DiskIOComplete" );
    749  1.1  oster 
    750  1.1  oster   /* unlock the queue:
    751  1.1  oster      (1) after an unlocking req completes
    752  1.1  oster      (2) after a locking req fails
    753  1.1  oster   */
    754  1.1  oster   if (RF_UNLOCKING_REQ(req) || (RF_LOCKING_REQ(req) && status)) {
    755  1.1  oster     Dprintf2("DiskIOComplete: unlocking queue at r %d c %d\n", queue->row, queue->col);
    756  1.1  oster     RF_ASSERT(RF_QUEUE_LOCKED(queue) && (queue->unlockingOp == NULL));
    757  1.1  oster     RF_UNLOCK_QUEUE(queue);
    758  1.1  oster   }
    759  1.1  oster 
    760  1.1  oster   queue->numOutstanding--;
    761  1.1  oster   RF_ASSERT(queue->numOutstanding >= 0);
    762  1.1  oster 
    763  1.1  oster   /* dispatch requests to the disk until we find one that we can't. */
    764  1.1  oster   /* no reason to continue once we've filled up the queue */
    765  1.1  oster   /* no reason to even start if the queue is locked */
    766  1.1  oster 
    767  1.1  oster   while (!done && !RF_QUEUE_FULL(queue) && !RF_QUEUE_LOCKED(queue)) {
    768  1.1  oster     if (queue->nextLockingOp) {
    769  1.1  oster       req = queue->nextLockingOp; queue->nextLockingOp = NULL;
    770  1.1  oster       Dprintf3("DiskIOComplete: a pri %d locking req was pending at r %d c %d\n",req->priority,queue->row, queue->col);
    771  1.1  oster     } else {
    772  1.1  oster       req = (queue->qPtr->Dequeue)( queue->qHdr );
    773  1.2  oster       if (req != NULL) {
    774  1.2  oster 	      Dprintf3("DiskIOComplete: extracting pri %d req from queue at r %d c %d\n",req->priority,queue->row, queue->col);
    775  1.2  oster       } else {
    776  1.2  oster 	      Dprintf1("DiskIOComplete: no more requests to extract.\n","");
    777  1.2  oster       }
    778  1.1  oster     }
    779  1.1  oster     if (req) {
    780  1.1  oster 	queue->queueLength--;  /* decrement count of number of requests waiting in this queue */
    781  1.1  oster 	RF_ASSERT(queue->queueLength >= 0);
    782  1.1  oster     }
    783  1.1  oster     if (!req) done=1;
    784  1.1  oster     else if (RF_LOCKING_REQ(req)) {
    785  1.1  oster       if (RF_QUEUE_EMPTY(queue)) {                   					/* dispatch it */
    786  1.1  oster 	Dprintf3("DiskIOComplete: dispatching pri %d locking req to r %d c %d (queue empty)\n",req->priority,queue->row, queue->col);
    787  1.1  oster 	RF_LOCK_QUEUE(queue);
    788  1.1  oster 	rf_DispatchKernelIO(queue, req);
    789  1.1  oster 	done = 1;
    790  1.1  oster       } else {                         		           /* put it aside to wait for the queue to drain */
    791  1.1  oster 	Dprintf3("DiskIOComplete: postponing pri %d locking req to r %d c %d\n",req->priority,queue->row, queue->col);
    792  1.1  oster 	RF_ASSERT(queue->nextLockingOp == NULL);
    793  1.1  oster 	queue->nextLockingOp = req;
    794  1.1  oster 	done = 1;
    795  1.1  oster       }
    796  1.1  oster     } else if (RF_UNLOCKING_REQ(req)) {      	/* should not happen: unlocking ops should not get queued */
    797  1.1  oster       RF_ASSERT(RF_QUEUE_LOCKED(queue)); 			               /* support it anyway for the future */
    798  1.1  oster       Dprintf3("DiskIOComplete: dispatching pri %d unl req to r %d c %d (SHOULD NOT SEE THIS)\n",req->priority,queue->row, queue->col);
    799  1.1  oster       rf_DispatchKernelIO(queue, req);
    800  1.1  oster       done = 1;
    801  1.1  oster     } else if (RF_OK_TO_DISPATCH(queue, req)) {
    802  1.1  oster       Dprintf3("DiskIOComplete: dispatching pri %d regular req to r %d c %d (ok to dispatch)\n",req->priority,queue->row, queue->col);
    803  1.1  oster       rf_DispatchKernelIO(queue, req);
    804  1.1  oster     } else {                                   		  /* we can't dispatch it, so just re-enqueue it.  */
    805  1.1  oster       /* potential trouble here if disk queues batch reqs */
    806  1.1  oster       Dprintf3("DiskIOComplete: re-enqueueing pri %d regular req to r %d c %d\n",req->priority,queue->row, queue->col);
    807  1.1  oster       queue->queueLength++;
    808  1.1  oster       (queue->qPtr->Enqueue)(queue->qHdr, req, req->priority);
    809  1.1  oster       done = 1;
    810  1.1  oster     }
    811  1.1  oster   }
    812  1.1  oster 
    813  1.1  oster   RF_UNLOCK_QUEUE_MUTEX( queue, "DiskIOComplete" );
    814  1.1  oster }
    815  1.1  oster #endif /* !KERNEL */
    816  1.1  oster 
    817  1.1  oster /* promotes accesses tagged with the given parityStripeID from low priority
    818  1.1  oster  * to normal priority.  This promotion is optional, meaning that a queue
    819  1.1  oster  * need not implement it.  If there is no promotion routine associated with
    820  1.1  oster  * a queue, this routine does nothing and returns -1.
    821  1.1  oster  */
    822  1.1  oster int rf_DiskIOPromote(queue, parityStripeID, which_ru)
    823  1.1  oster   RF_DiskQueue_t     *queue;
    824  1.1  oster   RF_StripeNum_t      parityStripeID;
    825  1.1  oster   RF_ReconUnitNum_t   which_ru;
    826  1.1  oster {
    827  1.1  oster   int retval;
    828  1.1  oster 
    829  1.1  oster   if (!queue->qPtr->Promote)
    830  1.1  oster     return(-1);
    831  1.1  oster   RF_LOCK_QUEUE_MUTEX( queue, "DiskIOPromote" );
    832  1.1  oster   retval = (queue->qPtr->Promote)( queue->qHdr, parityStripeID, which_ru );
    833  1.1  oster   RF_UNLOCK_QUEUE_MUTEX( queue, "DiskIOPromote" );
    834  1.1  oster   return(retval);
    835  1.1  oster }
    836  1.1  oster 
    837  1.1  oster RF_DiskQueueData_t *rf_CreateDiskQueueData(
    838  1.1  oster   RF_IoType_t                typ,
    839  1.1  oster   RF_SectorNum_t             ssect,
    840  1.1  oster   RF_SectorCount_t           nsect,
    841  1.1  oster   caddr_t                    buf,
    842  1.1  oster   RF_StripeNum_t             parityStripeID,
    843  1.1  oster   RF_ReconUnitNum_t          which_ru,
    844  1.1  oster   int                      (*wakeF)(void *,int),
    845  1.1  oster   void                      *arg,
    846  1.1  oster   RF_DiskQueueData_t        *next,
    847  1.1  oster   RF_AccTraceEntry_t        *tracerec,
    848  1.1  oster   void                      *raidPtr,
    849  1.1  oster   RF_DiskQueueDataFlags_t    flags,
    850  1.1  oster   void                      *kb_proc)
    851  1.1  oster {
    852  1.1  oster   RF_DiskQueueData_t *p;
    853  1.1  oster 
    854  1.1  oster   RF_FREELIST_GET_INIT(rf_dqd_freelist,p,next,(RF_DiskQueueData_t *),init_dqd);
    855  1.1  oster 
    856  1.1  oster   p->sectorOffset  = ssect + rf_protectedSectors;
    857  1.1  oster   p->numSector     = nsect;
    858  1.1  oster   p->type          = typ;
    859  1.1  oster   p->buf           = buf;
    860  1.1  oster   p->parityStripeID= parityStripeID;
    861  1.1  oster   p->which_ru      = which_ru;
    862  1.1  oster   p->CompleteFunc  = wakeF;
    863  1.1  oster   p->argument      = arg;
    864  1.1  oster   p->next          = next;
    865  1.1  oster   p->tracerec      = tracerec;
    866  1.1  oster   p->priority      = RF_IO_NORMAL_PRIORITY;
    867  1.1  oster   p->AuxFunc       = NULL;
    868  1.1  oster   p->buf2          = NULL;
    869  1.1  oster #ifdef SIMULATE
    870  1.1  oster   p->owner         = rf_GetCurrentOwner();
    871  1.1  oster #endif /* SIMULATE */
    872  1.1  oster   p->raidPtr       = raidPtr;
    873  1.1  oster   p->flags         = flags;
    874  1.1  oster #ifdef KERNEL
    875  1.1  oster   p->b_proc        = kb_proc;
    876  1.1  oster #endif /* KERNEL */
    877  1.1  oster   return(p);
    878  1.1  oster }
    879  1.1  oster 
    880  1.1  oster RF_DiskQueueData_t *rf_CreateDiskQueueDataFull(
    881  1.1  oster   RF_IoType_t                typ,
    882  1.1  oster   RF_SectorNum_t             ssect,
    883  1.1  oster   RF_SectorCount_t           nsect,
    884  1.1  oster   caddr_t                    buf,
    885  1.1  oster   RF_StripeNum_t             parityStripeID,
    886  1.1  oster   RF_ReconUnitNum_t          which_ru,
    887  1.1  oster   int                      (*wakeF)(void *,int),
    888  1.1  oster   void                      *arg,
    889  1.1  oster   RF_DiskQueueData_t        *next,
    890  1.1  oster   RF_AccTraceEntry_t        *tracerec,
    891  1.1  oster   int                        priority,
    892  1.1  oster   int                      (*AuxFunc)(void *,...),
    893  1.1  oster   caddr_t                    buf2,
    894  1.1  oster   void                      *raidPtr,
    895  1.1  oster   RF_DiskQueueDataFlags_t    flags,
    896  1.1  oster   void                      *kb_proc)
    897  1.1  oster {
    898  1.1  oster   RF_DiskQueueData_t *p;
    899  1.1  oster 
    900  1.1  oster   RF_FREELIST_GET_INIT(rf_dqd_freelist,p,next,(RF_DiskQueueData_t *),init_dqd);
    901  1.1  oster 
    902  1.1  oster   p->sectorOffset  = ssect + rf_protectedSectors;
    903  1.1  oster   p->numSector     = nsect;
    904  1.1  oster   p->type          = typ;
    905  1.1  oster   p->buf           = buf;
    906  1.1  oster   p->parityStripeID= parityStripeID;
    907  1.1  oster   p->which_ru      = which_ru;
    908  1.1  oster   p->CompleteFunc  = wakeF;
    909  1.1  oster   p->argument      = arg;
    910  1.1  oster   p->next          = next;
    911  1.1  oster   p->tracerec      = tracerec;
    912  1.1  oster   p->priority      = priority;
    913  1.1  oster   p->AuxFunc       = AuxFunc;
    914  1.1  oster   p->buf2          = buf2;
    915  1.1  oster #ifdef SIMULATE
    916  1.1  oster   p->owner         = rf_GetCurrentOwner();
    917  1.1  oster #endif /* SIMULATE */
    918  1.1  oster   p->raidPtr       = raidPtr;
    919  1.1  oster   p->flags         = flags;
    920  1.1  oster #ifdef KERNEL
    921  1.1  oster   p->b_proc        = kb_proc;
    922  1.1  oster #endif /* KERNEL */
    923  1.1  oster   return(p);
    924  1.1  oster }
    925  1.1  oster 
    926  1.1  oster void rf_FreeDiskQueueData(p)
    927  1.1  oster   RF_DiskQueueData_t  *p;
    928  1.1  oster {
    929  1.1  oster 	RF_FREELIST_FREE_CLEAN(rf_dqd_freelist,p,next,clean_dqd);
    930  1.1  oster }
    931