Annotation of src/sys/dev/raidframe/rf_diskqueue.c, revision 1.58
1.58 ! jdolecek 1: /* $NetBSD: rf_diskqueue.c,v 1.57 2020/06/19 19:29:39 jdolecek Exp $ */
1.1 oster 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@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:
1.13 oster 29: /****************************************************************************
1.1 oster 30: *
31: * rf_diskqueue.c -- higher-level disk queue code
32: *
33: * the routines here are a generic wrapper around the actual queueing
1.6 oster 34: * routines. The code here implements thread scheduling, synchronization,
1.1 oster 35: * and locking ops (see below) on top of the lower-level queueing code.
36: *
1.13 oster 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.
1.1 oster 65: *
1.13 oster 66: ****************************************************************************/
1.15 lukem 67:
68: #include <sys/cdefs.h>
1.58 ! jdolecek 69: __KERNEL_RCSID(0, "$NetBSD: rf_diskqueue.c,v 1.57 2020/06/19 19:29:39 jdolecek Exp $");
1.1 oster 70:
1.14 oster 71: #include <dev/raidframe/raidframevar.h>
72:
1.1 oster 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"
1.11 oster 85: #include "rf_kintf.h"
1.1 oster 86:
87: static void rf_ShutdownDiskQueueSystem(void *);
88:
1.21 oster 89: #ifndef RF_DEBUG_DISKQUEUE
90: #define RF_DEBUG_DISKQUEUE 0
91: #endif
92:
93: #if RF_DEBUG_DISKQUEUE
1.1 oster 94: #define Dprintf1(s,a) if (rf_queueDebug) rf_debug_printf(s,(void *)((unsigned long)a),NULL,NULL,NULL,NULL,NULL,NULL,NULL)
95: #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)
96: #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)
1.21 oster 97: #else
98: #define Dprintf1(s,a)
99: #define Dprintf2(s,a,b)
100: #define Dprintf3(s,a,b,c)
101: #endif
1.1 oster 102:
1.13 oster 103: /*****************************************************************************
1.1 oster 104: *
1.13 oster 105: * the disk queue switch defines all the functions used in the
106: * different queueing disciplines queue ID, init routine, enqueue
107: * routine, dequeue routine
1.1 oster 108: *
1.13 oster 109: ****************************************************************************/
1.1 oster 110:
1.22 jdolecek 111: static const RF_DiskQueueSW_t diskqueuesw[] = {
1.6 oster 112: {"fifo", /* FIFO */
113: rf_FifoCreate,
114: rf_FifoEnqueue,
115: rf_FifoDequeue,
116: rf_FifoPeek,
1.1 oster 117: rf_FifoPromote},
118:
1.6 oster 119: {"cvscan", /* cvscan */
120: rf_CvscanCreate,
121: rf_CvscanEnqueue,
122: rf_CvscanDequeue,
123: rf_CvscanPeek,
124: rf_CvscanPromote},
125:
126: {"sstf", /* shortest seek time first */
127: rf_SstfCreate,
128: rf_SstfEnqueue,
129: rf_SstfDequeue,
130: rf_SstfPeek,
1.1 oster 131: rf_SstfPromote},
132:
1.6 oster 133: {"scan", /* SCAN (two-way elevator) */
134: rf_ScanCreate,
135: rf_SstfEnqueue,
136: rf_ScanDequeue,
137: rf_ScanPeek,
1.1 oster 138: rf_SstfPromote},
139:
1.6 oster 140: {"cscan", /* CSCAN (one-way elevator) */
141: rf_CscanCreate,
142: rf_SstfEnqueue,
143: rf_CscanDequeue,
144: rf_CscanPeek,
1.1 oster 145: rf_SstfPromote},
146:
147: };
148: #define NUM_DISK_QUEUE_TYPES (sizeof(diskqueuesw)/sizeof(RF_DiskQueueSW_t))
149:
150: #define RF_MAX_FREE_DQD 256
1.31 oster 151: #define RF_MIN_FREE_DQD 64
1.1 oster 152:
153: #include <sys/buf.h>
154:
1.6 oster 155: /* configures a single disk queue */
1.9 oster 156:
1.53 mrg 157: static void
158: rf_ShutdownDiskQueue(void *arg)
159: {
160: RF_DiskQueue_t *diskqueue = arg;
161:
162: rf_destroy_mutex2(diskqueue->mutex);
163: }
164:
1.40 perry 165: int
1.27 oster 166: rf_ConfigureDiskQueue(RF_Raid_t *raidPtr, RF_DiskQueue_t *diskqueue,
167: RF_RowCol_t c, const RF_DiskQueueSW_t *p,
168: RF_SectorCount_t sectPerDisk, dev_t dev,
169: int maxOutstanding, RF_ShutdownList_t **listp,
170: RF_AllocListElem_t *clList)
1.6 oster 171: {
172: diskqueue->col = c;
173: diskqueue->qPtr = p;
174: diskqueue->qHdr = (p->Create) (sectPerDisk, clList, listp);
175: diskqueue->dev = dev;
176: diskqueue->numOutstanding = 0;
177: diskqueue->queueLength = 0;
178: diskqueue->maxOutstanding = maxOutstanding;
179: diskqueue->curPriority = RF_IO_NORMAL_PRIORITY;
180: diskqueue->flags = 0;
181: diskqueue->raidPtr = raidPtr;
1.23 oster 182: diskqueue->rf_cinfo = &raidPtr->raid_cinfo[c];
1.53 mrg 183: rf_init_mutex2(diskqueue->mutex, IPL_VM);
184: rf_ShutdownCreate(listp, rf_ShutdownDiskQueue, diskqueue);
1.6 oster 185: return (0);
1.1 oster 186: }
187:
1.40 perry 188: static void
1.48 christos 189: rf_ShutdownDiskQueueSystem(void *ignored)
1.6 oster 190: {
1.32 oster 191: pool_destroy(&rf_pools.dqd);
1.1 oster 192: }
193:
1.30 oster 194: int
1.27 oster 195: rf_ConfigureDiskQueueSystem(RF_ShutdownList_t **listp)
1.6 oster 196: {
197:
1.32 oster 198: rf_pool_init(&rf_pools.dqd, sizeof(RF_DiskQueueData_t),
199: "rf_dqd_pl", RF_MIN_FREE_DQD, RF_MAX_FREE_DQD);
1.30 oster 200: rf_ShutdownCreate(listp, rf_ShutdownDiskQueueSystem, NULL);
1.24 oster 201:
1.6 oster 202: return (0);
203: }
204:
1.40 perry 205: int
1.27 oster 206: rf_ConfigureDiskQueues(RF_ShutdownList_t **listp, RF_Raid_t *raidPtr,
207: RF_Config_t *cfgPtr)
1.6 oster 208: {
1.23 oster 209: RF_DiskQueue_t *diskQueues, *spareQueues;
1.22 jdolecek 210: const RF_DiskQueueSW_t *p;
1.23 oster 211: RF_RowCol_t r,c;
1.6 oster 212: int rc, i;
213:
214: raidPtr->maxQueueDepth = cfgPtr->maxOutstandingDiskReqs;
215:
216: for (p = NULL, i = 0; i < NUM_DISK_QUEUE_TYPES; i++) {
217: if (!strcmp(diskqueuesw[i].queueType, cfgPtr->diskQueueType)) {
218: p = &diskqueuesw[i];
219: break;
220: }
221: }
222: if (p == NULL) {
223: RF_ERRORMSG2("Unknown queue type \"%s\". Using %s\n", cfgPtr->diskQueueType, diskqueuesw[0].queueType);
224: p = &diskqueuesw[0];
225: }
1.10 oster 226: raidPtr->qType = p;
1.23 oster 227:
1.54 christos 228: diskQueues = RF_MallocAndAdd(
229: (raidPtr->numCol + RF_MAXSPARE) * sizeof(*diskQueues),
230: raidPtr->cleanupList);
1.23 oster 231: if (diskQueues == NULL)
1.6 oster 232: return (ENOMEM);
233: raidPtr->Queues = diskQueues;
1.23 oster 234:
235: for (c = 0; c < raidPtr->numCol; c++) {
236: rc = rf_ConfigureDiskQueue(raidPtr, &diskQueues[c],
237: c, p,
1.40 perry 238: raidPtr->sectorsPerDisk,
1.23 oster 239: raidPtr->Disks[c].dev,
1.40 perry 240: cfgPtr->maxOutstandingDiskReqs,
1.23 oster 241: listp, raidPtr->cleanupList);
242: if (rc)
243: return (rc);
1.6 oster 244: }
245:
1.23 oster 246: spareQueues = &raidPtr->Queues[raidPtr->numCol];
1.6 oster 247: for (r = 0; r < raidPtr->numSpare; r++) {
1.9 oster 248: rc = rf_ConfigureDiskQueue(raidPtr, &spareQueues[r],
1.23 oster 249: raidPtr->numCol + r, p,
250: raidPtr->sectorsPerDisk,
251: raidPtr->Disks[raidPtr->numCol + r].dev,
252: cfgPtr->maxOutstandingDiskReqs, listp,
253: raidPtr->cleanupList);
1.6 oster 254: if (rc)
255: return (rc);
256: }
257: return (0);
258: }
1.1 oster 259: /* Enqueue a disk I/O
260: *
261: * In the kernel, I/O is non-blocking and so we'd like to have multiple
262: * I/Os outstanding on the physical disks when possible.
263: *
264: * when any request arrives at a queue, we have two choices:
265: * dispatch it to the lower levels
266: * queue it up
267: *
268: * kernel rules for when to do what:
269: * unlocking req : always dispatch it
270: * normal req : queue empty => dispatch it & set priority
271: * queue not full & priority is ok => dispatch it
272: * else queue it
273: */
1.40 perry 274: void
1.27 oster 275: rf_DiskIOEnqueue(RF_DiskQueue_t *queue, RF_DiskQueueData_t *req, int pri)
1.6 oster 276: {
277: RF_ETIMER_START(req->qtime);
278: RF_ASSERT(req->type == RF_IO_TYPE_NOP || req->numSector);
279: req->priority = pri;
280:
1.21 oster 281: #if RF_DEBUG_DISKQUEUE
1.6 oster 282: if (rf_queueDebug && (req->numSector == 0)) {
283: printf("Warning: Enqueueing zero-sector access\n");
284: }
1.21 oster 285: #endif
1.6 oster 286: RF_LOCK_QUEUE_MUTEX(queue, "DiskIOEnqueue");
1.52 oster 287: if (RF_OK_TO_DISPATCH(queue, req)) {
288: Dprintf2("Dispatching pri %d regular op to c %d (ok to dispatch)\n", pri, queue->col);
289: rf_DispatchKernelIO(queue, req);
290: } else {
291: queue->queueLength++; /* increment count of number of requests waiting in this queue */
292: Dprintf2("Enqueueing pri %d regular op to c %d (not ok to dispatch)\n", pri, queue->col);
293: req->queue = (void *) queue;
294: (queue->qPtr->Enqueue) (queue->qHdr, req, pri);
1.6 oster 295: }
296: RF_UNLOCK_QUEUE_MUTEX(queue, "DiskIOEnqueue");
1.1 oster 297: }
1.6 oster 298:
1.1 oster 299:
1.52 oster 300: /* get the next set of I/Os started */
1.40 perry 301: void
1.27 oster 302: rf_DiskIOComplete(RF_DiskQueue_t *queue, RF_DiskQueueData_t *req, int status)
1.6 oster 303: {
304: int done = 0;
305:
306: RF_LOCK_QUEUE_MUTEX(queue, "DiskIOComplete");
307: queue->numOutstanding--;
308: RF_ASSERT(queue->numOutstanding >= 0);
309:
310: /* dispatch requests to the disk until we find one that we can't. */
311: /* no reason to continue once we've filled up the queue */
312: /* no reason to even start if the queue is locked */
313:
1.52 oster 314: while (!done && !RF_QUEUE_FULL(queue)) {
315: req = (queue->qPtr->Dequeue) (queue->qHdr);
1.6 oster 316: if (req) {
1.52 oster 317: Dprintf2("DiskIOComplete: extracting pri %d req from queue at c %d\n", req->priority, queue->col);
318: queue->queueLength--; /* decrement count of number of requests waiting in this queue */
1.6 oster 319: RF_ASSERT(queue->queueLength >= 0);
1.52 oster 320: if (RF_OK_TO_DISPATCH(queue, req)) {
321: Dprintf2("DiskIOComplete: dispatching pri %d regular req to c %d (ok to dispatch)\n", req->priority, queue->col);
322: rf_DispatchKernelIO(queue, req);
323: } else {
324: /* we can't dispatch it, so just re-enqueue it.
325: potential trouble here if disk queues batch reqs */
326: Dprintf2("DiskIOComplete: re-enqueueing pri %d regular req to c %d\n", req->priority, queue->col);
327: queue->queueLength++;
328: (queue->qPtr->Enqueue) (queue->qHdr, req, req->priority);
329: done = 1;
330: }
331: } else {
332: Dprintf1("DiskIOComplete: no more requests to extract.\n", "");
333: done = 1;
1.6 oster 334: }
335: }
336:
337: RF_UNLOCK_QUEUE_MUTEX(queue, "DiskIOComplete");
1.1 oster 338: }
339: /* promotes accesses tagged with the given parityStripeID from low priority
340: * to normal priority. This promotion is optional, meaning that a queue
341: * need not implement it. If there is no promotion routine associated with
342: * a queue, this routine does nothing and returns -1.
343: */
1.40 perry 344: int
1.27 oster 345: rf_DiskIOPromote(RF_DiskQueue_t *queue, RF_StripeNum_t parityStripeID,
346: RF_ReconUnitNum_t which_ru)
1.6 oster 347: {
348: int retval;
349:
350: if (!queue->qPtr->Promote)
351: return (-1);
352: RF_LOCK_QUEUE_MUTEX(queue, "DiskIOPromote");
353: retval = (queue->qPtr->Promote) (queue->qHdr, parityStripeID, which_ru);
354: RF_UNLOCK_QUEUE_MUTEX(queue, "DiskIOPromote");
355: return (retval);
356: }
357:
358: RF_DiskQueueData_t *
1.27 oster 359: rf_CreateDiskQueueData(RF_IoType_t typ, RF_SectorNum_t ssect,
1.49 christos 360: RF_SectorCount_t nsect, void *bf,
1.27 oster 361: RF_StripeNum_t parityStripeID,
362: RF_ReconUnitNum_t which_ru,
1.56 christos 363: void (*wakeF) (void *, int), void *arg,
1.37 oster 364: RF_AccTraceEntry_t *tracerec, RF_Raid_t *raidPtr,
1.57 jdolecek 365: RF_DiskQueueDataFlags_t flags, const struct buf *mbp,
1.38 oster 366: int waitflag)
1.6 oster 367: {
368: RF_DiskQueueData_t *p;
369:
1.55 christos 370: p = pool_get(&rf_pools.dqd, waitflag | PR_ZERO);
1.38 oster 371: if (p == NULL)
372: return (NULL);
373:
1.43 yamt 374: if (waitflag == PR_WAITOK) {
1.50 ad 375: p->bp = getiobuf(NULL, true);
1.43 yamt 376: } else {
1.50 ad 377: p->bp = getiobuf(NULL, false);
1.43 yamt 378: }
1.28 oster 379: if (p->bp == NULL) {
1.32 oster 380: pool_put(&rf_pools.dqd, p);
1.38 oster 381: return (NULL);
1.24 oster 382: }
1.51 reinoud 383: SET(p->bp->b_cflags, BC_BUSY); /* mark buffer busy */
1.57 jdolecek 384: if (mbp) {
385: SET(p->bp->b_flags, mbp->b_flags & rf_b_pass);
386: p->bp->b_proc = mbp->b_proc;
387: }
1.6 oster 388:
389: p->sectorOffset = ssect + rf_protectedSectors;
390: p->numSector = nsect;
391: p->type = typ;
1.41 christos 392: p->buf = bf;
1.6 oster 393: p->parityStripeID = parityStripeID;
394: p->which_ru = which_ru;
395: p->CompleteFunc = wakeF;
396: p->argument = arg;
1.39 oster 397: p->next = NULL;
1.6 oster 398: p->tracerec = tracerec;
399: p->priority = RF_IO_NORMAL_PRIORITY;
400: p->raidPtr = raidPtr;
401: p->flags = flags;
402: return (p);
403: }
404:
1.40 perry 405: void
1.27 oster 406: rf_FreeDiskQueueData(RF_DiskQueueData_t *p)
1.1 oster 407: {
1.43 yamt 408: putiobuf(p->bp);
1.32 oster 409: pool_put(&rf_pools.dqd, p);
1.1 oster 410: }
CVSweb <webmaster@jp.NetBSD.org>