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