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