Annotation of src/sys/dev/raidframe/rf_diskqueue.c, revision 1.65

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

CVSweb <webmaster@jp.NetBSD.org>