rf_diskqueue.c revision 1.5 1 1.5 oster /* $NetBSD: rf_diskqueue.c,v 1.5 1999/01/26 02:33:56 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 #include "rf_types.h"
65 1.1 oster #include "rf_threadstuff.h"
66 1.1 oster #include "rf_threadid.h"
67 1.1 oster #include "rf_raid.h"
68 1.1 oster #include "rf_diskqueue.h"
69 1.1 oster #include "rf_alloclist.h"
70 1.1 oster #include "rf_acctrace.h"
71 1.1 oster #include "rf_etimer.h"
72 1.1 oster #include "rf_configure.h"
73 1.1 oster #include "rf_general.h"
74 1.1 oster #include "rf_freelist.h"
75 1.1 oster #include "rf_debugprint.h"
76 1.1 oster #include "rf_shutdown.h"
77 1.1 oster #include "rf_cvscan.h"
78 1.1 oster #include "rf_sstf.h"
79 1.1 oster #include "rf_fifo.h"
80 1.1 oster
81 1.1 oster static int init_dqd(RF_DiskQueueData_t *);
82 1.1 oster static void clean_dqd(RF_DiskQueueData_t *);
83 1.1 oster static void rf_ShutdownDiskQueueSystem(void *);
84 1.1 oster /* From rf_kintf.c */
85 1.1 oster int rf_DispatchKernelIO(RF_DiskQueue_t *,RF_DiskQueueData_t *);
86 1.1 oster
87 1.1 oster
88 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)
89 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)
90 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)
91 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)
92 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)
93 1.1 oster
94 1.1 oster
95 1.1 oster #define SIGNAL_DISK_QUEUE(_q_,_wh_)
96 1.1 oster #define WAIT_DISK_QUEUE(_q_,_wh_)
97 1.1 oster
98 1.1 oster /*****************************************************************************************
99 1.1 oster *
100 1.1 oster * the disk queue switch defines all the functions used in the different queueing
101 1.1 oster * disciplines
102 1.1 oster * queue ID, init routine, enqueue routine, dequeue routine
103 1.1 oster *
104 1.1 oster ****************************************************************************************/
105 1.1 oster
106 1.1 oster static RF_DiskQueueSW_t diskqueuesw[] = {
107 1.1 oster {"fifo", /* FIFO */
108 1.1 oster rf_FifoCreate,
109 1.1 oster rf_FifoEnqueue,
110 1.1 oster rf_FifoDequeue,
111 1.1 oster rf_FifoPeek,
112 1.1 oster rf_FifoPromote},
113 1.1 oster
114 1.1 oster {"cvscan", /* cvscan */
115 1.1 oster rf_CvscanCreate,
116 1.1 oster rf_CvscanEnqueue,
117 1.1 oster rf_CvscanDequeue,
118 1.1 oster rf_CvscanPeek,
119 1.1 oster rf_CvscanPromote },
120 1.1 oster
121 1.1 oster {"sstf", /* shortest seek time first */
122 1.1 oster rf_SstfCreate,
123 1.1 oster rf_SstfEnqueue,
124 1.1 oster rf_SstfDequeue,
125 1.1 oster rf_SstfPeek,
126 1.1 oster rf_SstfPromote},
127 1.1 oster
128 1.1 oster {"scan", /* SCAN (two-way elevator) */
129 1.1 oster rf_ScanCreate,
130 1.1 oster rf_SstfEnqueue,
131 1.1 oster rf_ScanDequeue,
132 1.1 oster rf_ScanPeek,
133 1.1 oster rf_SstfPromote},
134 1.1 oster
135 1.1 oster {"cscan", /* CSCAN (one-way elevator) */
136 1.1 oster rf_CscanCreate,
137 1.1 oster rf_SstfEnqueue,
138 1.1 oster rf_CscanDequeue,
139 1.1 oster rf_CscanPeek,
140 1.1 oster rf_SstfPromote},
141 1.1 oster
142 1.5 oster #if !defined(_KERNEL) && RF_INCLUDE_QUEUE_RANDOM > 0
143 1.1 oster /* to make a point to Chris :-> */
144 1.1 oster {"random", /* random */
145 1.1 oster rf_FifoCreate,
146 1.1 oster rf_FifoEnqueue,
147 1.1 oster rf_RandomDequeue,
148 1.1 oster rf_RandomPeek,
149 1.1 oster rf_FifoPromote},
150 1.1 oster #endif /* !KERNEL && RF_INCLUDE_QUEUE_RANDOM > 0 */
151 1.1 oster };
152 1.1 oster #define NUM_DISK_QUEUE_TYPES (sizeof(diskqueuesw)/sizeof(RF_DiskQueueSW_t))
153 1.1 oster
154 1.1 oster static RF_FreeList_t *rf_dqd_freelist;
155 1.1 oster
156 1.1 oster #define RF_MAX_FREE_DQD 256
157 1.1 oster #define RF_DQD_INC 16
158 1.1 oster #define RF_DQD_INITIAL 64
159 1.1 oster
160 1.1 oster #include <sys/buf.h>
161 1.1 oster
162 1.1 oster static int init_dqd(dqd)
163 1.1 oster RF_DiskQueueData_t *dqd;
164 1.1 oster {
165 1.1 oster /* XXX not sure if the following malloc is appropriate... probably not quite... */
166 1.4 thorpej dqd->bp = (struct buf *) malloc( sizeof(struct buf), M_RAIDFRAME, M_NOWAIT);
167 1.1 oster if (dqd->bp == NULL) {
168 1.1 oster return(ENOMEM);
169 1.1 oster }
170 1.3 oster memset(dqd->bp,0,sizeof(struct buf)); /* if you don't do it, nobody else will.. */
171 1.1 oster return(0);
172 1.1 oster }
173 1.1 oster
174 1.1 oster static void clean_dqd(dqd)
175 1.1 oster RF_DiskQueueData_t *dqd;
176 1.1 oster {
177 1.4 thorpej free( dqd->bp, M_RAIDFRAME );
178 1.1 oster }
179 1.1 oster
180 1.1 oster /* configures a single disk queue */
181 1.1 oster static int config_disk_queue(
182 1.1 oster RF_Raid_t *raidPtr,
183 1.1 oster RF_DiskQueue_t *diskqueue,
184 1.1 oster RF_RowCol_t r, /* row & col -- debug only. BZZT not any more... */
185 1.1 oster RF_RowCol_t c,
186 1.1 oster RF_DiskQueueSW_t *p,
187 1.1 oster RF_SectorCount_t sectPerDisk,
188 1.1 oster dev_t dev,
189 1.1 oster int maxOutstanding,
190 1.1 oster RF_ShutdownList_t **listp,
191 1.1 oster RF_AllocListElem_t *clList)
192 1.1 oster {
193 1.1 oster int rc;
194 1.1 oster
195 1.1 oster diskqueue->row = r;
196 1.1 oster diskqueue->col = c;
197 1.1 oster diskqueue->qPtr = p;
198 1.1 oster diskqueue->qHdr = (p->Create)(sectPerDisk, clList, listp);
199 1.1 oster diskqueue->dev = dev;
200 1.1 oster diskqueue->numOutstanding = 0;
201 1.1 oster diskqueue->queueLength = 0;
202 1.1 oster diskqueue->maxOutstanding = maxOutstanding;
203 1.1 oster diskqueue->curPriority = RF_IO_NORMAL_PRIORITY;
204 1.1 oster diskqueue->nextLockingOp = NULL;
205 1.1 oster diskqueue->unlockingOp = NULL;
206 1.1 oster diskqueue->numWaiting=0;
207 1.1 oster diskqueue->flags = 0;
208 1.1 oster diskqueue->raidPtr = raidPtr;
209 1.1 oster diskqueue->rf_cinfo = &raidPtr->raid_cinfo[r][c];
210 1.1 oster rc = rf_create_managed_mutex(listp, &diskqueue->mutex);
211 1.1 oster if (rc) {
212 1.1 oster RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
213 1.1 oster __LINE__, rc);
214 1.1 oster return(rc);
215 1.1 oster }
216 1.1 oster rc = rf_create_managed_cond(listp, &diskqueue->cond);
217 1.1 oster if (rc) {
218 1.1 oster RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", __FILE__,
219 1.1 oster __LINE__, rc);
220 1.1 oster return(rc);
221 1.1 oster }
222 1.1 oster return(0);
223 1.1 oster }
224 1.1 oster
225 1.1 oster static void rf_ShutdownDiskQueueSystem(ignored)
226 1.1 oster void *ignored;
227 1.1 oster {
228 1.1 oster RF_FREELIST_DESTROY_CLEAN(rf_dqd_freelist,next,(RF_DiskQueueData_t *),clean_dqd);
229 1.1 oster }
230 1.1 oster
231 1.1 oster int rf_ConfigureDiskQueueSystem(listp)
232 1.1 oster RF_ShutdownList_t **listp;
233 1.1 oster {
234 1.1 oster int rc;
235 1.1 oster
236 1.1 oster RF_FREELIST_CREATE(rf_dqd_freelist, RF_MAX_FREE_DQD,
237 1.1 oster RF_DQD_INC, sizeof(RF_DiskQueueData_t));
238 1.1 oster if (rf_dqd_freelist == NULL)
239 1.1 oster return(ENOMEM);
240 1.1 oster rc = rf_ShutdownCreate(listp, rf_ShutdownDiskQueueSystem, NULL);
241 1.1 oster if (rc) {
242 1.1 oster RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
243 1.1 oster __FILE__, __LINE__, rc);
244 1.1 oster rf_ShutdownDiskQueueSystem(NULL);
245 1.1 oster return(rc);
246 1.1 oster }
247 1.1 oster RF_FREELIST_PRIME_INIT(rf_dqd_freelist, RF_DQD_INITIAL,next,
248 1.1 oster (RF_DiskQueueData_t *),init_dqd);
249 1.1 oster return(0);
250 1.1 oster }
251 1.1 oster
252 1.1 oster int rf_ConfigureDiskQueues(
253 1.1 oster RF_ShutdownList_t **listp,
254 1.1 oster RF_Raid_t *raidPtr,
255 1.1 oster RF_Config_t *cfgPtr)
256 1.1 oster {
257 1.1 oster RF_DiskQueue_t **diskQueues, *spareQueues;
258 1.1 oster RF_DiskQueueSW_t *p;
259 1.1 oster RF_RowCol_t r, c;
260 1.1 oster int rc, i;
261 1.1 oster
262 1.1 oster raidPtr->maxQueueDepth = cfgPtr->maxOutstandingDiskReqs;
263 1.1 oster
264 1.1 oster for(p=NULL,i=0;i<NUM_DISK_QUEUE_TYPES;i++) {
265 1.1 oster if (!strcmp(diskqueuesw[i].queueType, cfgPtr->diskQueueType)) {
266 1.1 oster p = &diskqueuesw[i];
267 1.1 oster break;
268 1.1 oster }
269 1.1 oster }
270 1.1 oster if (p == NULL) {
271 1.1 oster RF_ERRORMSG2("Unknown queue type \"%s\". Using %s\n",cfgPtr->diskQueueType, diskqueuesw[0].queueType);
272 1.1 oster p = &diskqueuesw[0];
273 1.1 oster }
274 1.1 oster
275 1.1 oster RF_CallocAndAdd(diskQueues, raidPtr->numRow, sizeof(RF_DiskQueue_t *), (RF_DiskQueue_t **), raidPtr->cleanupList);
276 1.1 oster if (diskQueues == NULL) {
277 1.1 oster return(ENOMEM);
278 1.1 oster }
279 1.1 oster raidPtr->Queues = diskQueues;
280 1.1 oster for (r=0; r<raidPtr->numRow; r++) {
281 1.1 oster RF_CallocAndAdd(diskQueues[r], raidPtr->numCol + ((r==0) ? raidPtr->numSpare : 0), sizeof(RF_DiskQueue_t), (RF_DiskQueue_t *), raidPtr->cleanupList);
282 1.1 oster if (diskQueues[r] == NULL)
283 1.1 oster return(ENOMEM);
284 1.1 oster for (c=0; c<raidPtr->numCol; c++) {
285 1.1 oster rc = config_disk_queue(raidPtr, &diskQueues[r][c], r, c, p,
286 1.1 oster raidPtr->sectorsPerDisk, raidPtr->Disks[r][c].dev,
287 1.1 oster cfgPtr->maxOutstandingDiskReqs, listp, raidPtr->cleanupList);
288 1.1 oster if (rc)
289 1.1 oster return(rc);
290 1.1 oster }
291 1.1 oster }
292 1.1 oster
293 1.1 oster spareQueues = &raidPtr->Queues[0][raidPtr->numCol];
294 1.1 oster for (r=0; r<raidPtr->numSpare; r++) {
295 1.1 oster rc = config_disk_queue(raidPtr, &spareQueues[r],
296 1.1 oster 0, raidPtr->numCol+r, p,
297 1.1 oster raidPtr->sectorsPerDisk,
298 1.1 oster raidPtr->Disks[0][raidPtr->numCol+r].dev,
299 1.1 oster cfgPtr->maxOutstandingDiskReqs, listp,
300 1.1 oster raidPtr->cleanupList);
301 1.1 oster if (rc)
302 1.1 oster return(rc);
303 1.1 oster }
304 1.1 oster return(0);
305 1.1 oster }
306 1.1 oster
307 1.1 oster /* Enqueue a disk I/O
308 1.1 oster *
309 1.1 oster * Unfortunately, we have to do things differently in the different
310 1.1 oster * environments (simulator, user-level, kernel).
311 1.1 oster * At user level, all I/O is blocking, so we have 1 or more threads/disk
312 1.1 oster * and the thread that enqueues is different from the thread that dequeues.
313 1.1 oster * In the kernel, I/O is non-blocking and so we'd like to have multiple
314 1.1 oster * I/Os outstanding on the physical disks when possible.
315 1.1 oster *
316 1.1 oster * when any request arrives at a queue, we have two choices:
317 1.1 oster * dispatch it to the lower levels
318 1.1 oster * queue it up
319 1.1 oster *
320 1.1 oster * kernel rules for when to do what:
321 1.1 oster * locking request: queue empty => dispatch and lock queue,
322 1.1 oster * else queue it
323 1.1 oster * unlocking req : always dispatch it
324 1.1 oster * normal req : queue empty => dispatch it & set priority
325 1.1 oster * queue not full & priority is ok => dispatch it
326 1.1 oster * else queue it
327 1.1 oster *
328 1.1 oster * user-level rules:
329 1.1 oster * always enqueue. In the special case of an unlocking op, enqueue
330 1.1 oster * in a special way that will cause the unlocking op to be the next
331 1.1 oster * thing dequeued.
332 1.1 oster *
333 1.1 oster * simulator rules:
334 1.1 oster * Do the same as at user level, with the sleeps and wakeups suppressed.
335 1.1 oster */
336 1.1 oster void rf_DiskIOEnqueue(queue, req, pri)
337 1.1 oster RF_DiskQueue_t *queue;
338 1.1 oster RF_DiskQueueData_t *req;
339 1.1 oster int pri;
340 1.1 oster {
341 1.1 oster int tid;
342 1.1 oster
343 1.1 oster RF_ETIMER_START(req->qtime);
344 1.1 oster rf_get_threadid(tid);
345 1.1 oster RF_ASSERT(req->type == RF_IO_TYPE_NOP || req->numSector);
346 1.1 oster req->priority = pri;
347 1.1 oster
348 1.1 oster if (rf_queueDebug && (req->numSector == 0)) {
349 1.1 oster printf("Warning: Enqueueing zero-sector access\n");
350 1.1 oster }
351 1.1 oster
352 1.1 oster /*
353 1.1 oster * kernel
354 1.1 oster */
355 1.1 oster RF_LOCK_QUEUE_MUTEX( queue, "DiskIOEnqueue" );
356 1.1 oster /* locking request */
357 1.1 oster if (RF_LOCKING_REQ(req)) {
358 1.1 oster if (RF_QUEUE_EMPTY(queue)) {
359 1.1 oster Dprintf3("Dispatching pri %d locking op to r %d c %d (queue empty)\n",pri,queue->row, queue->col);
360 1.1 oster RF_LOCK_QUEUE(queue);
361 1.1 oster rf_DispatchKernelIO(queue, req);
362 1.1 oster } else {
363 1.1 oster queue->queueLength++; /* increment count of number of requests waiting in this queue */
364 1.1 oster Dprintf3("Enqueueing pri %d locking op to r %d c %d (queue not empty)\n",pri,queue->row, queue->col);
365 1.1 oster req->queue = (void *)queue;
366 1.1 oster (queue->qPtr->Enqueue)(queue->qHdr, req, pri);
367 1.1 oster }
368 1.1 oster }
369 1.1 oster /* unlocking request */
370 1.1 oster else if (RF_UNLOCKING_REQ(req)) { /* we'll do the actual unlock when this I/O completes */
371 1.1 oster Dprintf3("Dispatching pri %d unlocking op to r %d c %d\n",pri,queue->row, queue->col);
372 1.1 oster RF_ASSERT(RF_QUEUE_LOCKED(queue));
373 1.1 oster rf_DispatchKernelIO(queue, req);
374 1.1 oster }
375 1.1 oster /* normal request */
376 1.1 oster else if (RF_OK_TO_DISPATCH(queue, req)) {
377 1.1 oster Dprintf3("Dispatching pri %d regular op to r %d c %d (ok to dispatch)\n",pri,queue->row, queue->col);
378 1.1 oster rf_DispatchKernelIO(queue, req);
379 1.1 oster } else {
380 1.1 oster queue->queueLength++; /* increment count of number of requests waiting in this queue */
381 1.1 oster Dprintf3("Enqueueing pri %d regular op to r %d c %d (not ok to dispatch)\n",pri,queue->row, queue->col);
382 1.1 oster req->queue = (void *)queue;
383 1.1 oster (queue->qPtr->Enqueue)(queue->qHdr, req, pri);
384 1.1 oster }
385 1.1 oster RF_UNLOCK_QUEUE_MUTEX( queue, "DiskIOEnqueue" );
386 1.1 oster }
387 1.1 oster
388 1.1 oster
389 1.1 oster /* get the next set of I/Os started, kernel version only */
390 1.1 oster void rf_DiskIOComplete(queue, req, status)
391 1.1 oster RF_DiskQueue_t *queue;
392 1.1 oster RF_DiskQueueData_t *req;
393 1.1 oster int status;
394 1.1 oster {
395 1.1 oster int done=0;
396 1.1 oster
397 1.1 oster RF_LOCK_QUEUE_MUTEX( queue, "DiskIOComplete" );
398 1.1 oster
399 1.1 oster /* unlock the queue:
400 1.1 oster (1) after an unlocking req completes
401 1.1 oster (2) after a locking req fails
402 1.1 oster */
403 1.1 oster if (RF_UNLOCKING_REQ(req) || (RF_LOCKING_REQ(req) && status)) {
404 1.1 oster Dprintf2("DiskIOComplete: unlocking queue at r %d c %d\n", queue->row, queue->col);
405 1.1 oster RF_ASSERT(RF_QUEUE_LOCKED(queue) && (queue->unlockingOp == NULL));
406 1.1 oster RF_UNLOCK_QUEUE(queue);
407 1.1 oster }
408 1.1 oster
409 1.1 oster queue->numOutstanding--;
410 1.1 oster RF_ASSERT(queue->numOutstanding >= 0);
411 1.1 oster
412 1.1 oster /* dispatch requests to the disk until we find one that we can't. */
413 1.1 oster /* no reason to continue once we've filled up the queue */
414 1.1 oster /* no reason to even start if the queue is locked */
415 1.1 oster
416 1.1 oster while (!done && !RF_QUEUE_FULL(queue) && !RF_QUEUE_LOCKED(queue)) {
417 1.1 oster if (queue->nextLockingOp) {
418 1.1 oster req = queue->nextLockingOp; queue->nextLockingOp = NULL;
419 1.1 oster Dprintf3("DiskIOComplete: a pri %d locking req was pending at r %d c %d\n",req->priority,queue->row, queue->col);
420 1.1 oster } else {
421 1.1 oster req = (queue->qPtr->Dequeue)( queue->qHdr );
422 1.2 oster if (req != NULL) {
423 1.2 oster Dprintf3("DiskIOComplete: extracting pri %d req from queue at r %d c %d\n",req->priority,queue->row, queue->col);
424 1.2 oster } else {
425 1.2 oster Dprintf1("DiskIOComplete: no more requests to extract.\n","");
426 1.2 oster }
427 1.1 oster }
428 1.1 oster if (req) {
429 1.1 oster queue->queueLength--; /* decrement count of number of requests waiting in this queue */
430 1.1 oster RF_ASSERT(queue->queueLength >= 0);
431 1.1 oster }
432 1.1 oster if (!req) done=1;
433 1.1 oster else if (RF_LOCKING_REQ(req)) {
434 1.1 oster if (RF_QUEUE_EMPTY(queue)) { /* dispatch it */
435 1.1 oster Dprintf3("DiskIOComplete: dispatching pri %d locking req to r %d c %d (queue empty)\n",req->priority,queue->row, queue->col);
436 1.1 oster RF_LOCK_QUEUE(queue);
437 1.1 oster rf_DispatchKernelIO(queue, req);
438 1.1 oster done = 1;
439 1.1 oster } else { /* put it aside to wait for the queue to drain */
440 1.1 oster Dprintf3("DiskIOComplete: postponing pri %d locking req to r %d c %d\n",req->priority,queue->row, queue->col);
441 1.1 oster RF_ASSERT(queue->nextLockingOp == NULL);
442 1.1 oster queue->nextLockingOp = req;
443 1.1 oster done = 1;
444 1.1 oster }
445 1.1 oster } else if (RF_UNLOCKING_REQ(req)) { /* should not happen: unlocking ops should not get queued */
446 1.1 oster RF_ASSERT(RF_QUEUE_LOCKED(queue)); /* support it anyway for the future */
447 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);
448 1.1 oster rf_DispatchKernelIO(queue, req);
449 1.1 oster done = 1;
450 1.1 oster } else if (RF_OK_TO_DISPATCH(queue, req)) {
451 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);
452 1.1 oster rf_DispatchKernelIO(queue, req);
453 1.1 oster } else { /* we can't dispatch it, so just re-enqueue it. */
454 1.1 oster /* potential trouble here if disk queues batch reqs */
455 1.1 oster Dprintf3("DiskIOComplete: re-enqueueing pri %d regular req to r %d c %d\n",req->priority,queue->row, queue->col);
456 1.1 oster queue->queueLength++;
457 1.1 oster (queue->qPtr->Enqueue)(queue->qHdr, req, req->priority);
458 1.1 oster done = 1;
459 1.1 oster }
460 1.1 oster }
461 1.1 oster
462 1.1 oster RF_UNLOCK_QUEUE_MUTEX( queue, "DiskIOComplete" );
463 1.1 oster }
464 1.1 oster
465 1.1 oster /* promotes accesses tagged with the given parityStripeID from low priority
466 1.1 oster * to normal priority. This promotion is optional, meaning that a queue
467 1.1 oster * need not implement it. If there is no promotion routine associated with
468 1.1 oster * a queue, this routine does nothing and returns -1.
469 1.1 oster */
470 1.1 oster int rf_DiskIOPromote(queue, parityStripeID, which_ru)
471 1.1 oster RF_DiskQueue_t *queue;
472 1.1 oster RF_StripeNum_t parityStripeID;
473 1.1 oster RF_ReconUnitNum_t which_ru;
474 1.1 oster {
475 1.1 oster int retval;
476 1.1 oster
477 1.1 oster if (!queue->qPtr->Promote)
478 1.1 oster return(-1);
479 1.1 oster RF_LOCK_QUEUE_MUTEX( queue, "DiskIOPromote" );
480 1.1 oster retval = (queue->qPtr->Promote)( queue->qHdr, parityStripeID, which_ru );
481 1.1 oster RF_UNLOCK_QUEUE_MUTEX( queue, "DiskIOPromote" );
482 1.1 oster return(retval);
483 1.1 oster }
484 1.1 oster
485 1.1 oster RF_DiskQueueData_t *rf_CreateDiskQueueData(
486 1.1 oster RF_IoType_t typ,
487 1.1 oster RF_SectorNum_t ssect,
488 1.1 oster RF_SectorCount_t nsect,
489 1.1 oster caddr_t buf,
490 1.1 oster RF_StripeNum_t parityStripeID,
491 1.1 oster RF_ReconUnitNum_t which_ru,
492 1.1 oster int (*wakeF)(void *,int),
493 1.1 oster void *arg,
494 1.1 oster RF_DiskQueueData_t *next,
495 1.1 oster RF_AccTraceEntry_t *tracerec,
496 1.1 oster void *raidPtr,
497 1.1 oster RF_DiskQueueDataFlags_t flags,
498 1.1 oster void *kb_proc)
499 1.1 oster {
500 1.1 oster RF_DiskQueueData_t *p;
501 1.1 oster
502 1.1 oster RF_FREELIST_GET_INIT(rf_dqd_freelist,p,next,(RF_DiskQueueData_t *),init_dqd);
503 1.1 oster
504 1.1 oster p->sectorOffset = ssect + rf_protectedSectors;
505 1.1 oster p->numSector = nsect;
506 1.1 oster p->type = typ;
507 1.1 oster p->buf = buf;
508 1.1 oster p->parityStripeID= parityStripeID;
509 1.1 oster p->which_ru = which_ru;
510 1.1 oster p->CompleteFunc = wakeF;
511 1.1 oster p->argument = arg;
512 1.1 oster p->next = next;
513 1.1 oster p->tracerec = tracerec;
514 1.1 oster p->priority = RF_IO_NORMAL_PRIORITY;
515 1.1 oster p->AuxFunc = NULL;
516 1.1 oster p->buf2 = NULL;
517 1.1 oster p->raidPtr = raidPtr;
518 1.1 oster p->flags = flags;
519 1.1 oster p->b_proc = kb_proc;
520 1.1 oster return(p);
521 1.1 oster }
522 1.1 oster
523 1.1 oster RF_DiskQueueData_t *rf_CreateDiskQueueDataFull(
524 1.1 oster RF_IoType_t typ,
525 1.1 oster RF_SectorNum_t ssect,
526 1.1 oster RF_SectorCount_t nsect,
527 1.1 oster caddr_t buf,
528 1.1 oster RF_StripeNum_t parityStripeID,
529 1.1 oster RF_ReconUnitNum_t which_ru,
530 1.1 oster int (*wakeF)(void *,int),
531 1.1 oster void *arg,
532 1.1 oster RF_DiskQueueData_t *next,
533 1.1 oster RF_AccTraceEntry_t *tracerec,
534 1.1 oster int priority,
535 1.1 oster int (*AuxFunc)(void *,...),
536 1.1 oster caddr_t buf2,
537 1.1 oster void *raidPtr,
538 1.1 oster RF_DiskQueueDataFlags_t flags,
539 1.1 oster void *kb_proc)
540 1.1 oster {
541 1.1 oster RF_DiskQueueData_t *p;
542 1.1 oster
543 1.1 oster RF_FREELIST_GET_INIT(rf_dqd_freelist,p,next,(RF_DiskQueueData_t *),init_dqd);
544 1.1 oster
545 1.1 oster p->sectorOffset = ssect + rf_protectedSectors;
546 1.1 oster p->numSector = nsect;
547 1.1 oster p->type = typ;
548 1.1 oster p->buf = buf;
549 1.1 oster p->parityStripeID= parityStripeID;
550 1.1 oster p->which_ru = which_ru;
551 1.1 oster p->CompleteFunc = wakeF;
552 1.1 oster p->argument = arg;
553 1.1 oster p->next = next;
554 1.1 oster p->tracerec = tracerec;
555 1.1 oster p->priority = priority;
556 1.1 oster p->AuxFunc = AuxFunc;
557 1.1 oster p->buf2 = buf2;
558 1.1 oster p->raidPtr = raidPtr;
559 1.1 oster p->flags = flags;
560 1.1 oster p->b_proc = kb_proc;
561 1.1 oster return(p);
562 1.1 oster }
563 1.1 oster
564 1.1 oster void rf_FreeDiskQueueData(p)
565 1.1 oster RF_DiskQueueData_t *p;
566 1.1 oster {
567 1.1 oster RF_FREELIST_FREE_CLEAN(rf_dqd_freelist,p,next,clean_dqd);
568 1.1 oster }
569