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