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