[21496] | 1 | /* $Id: PDMAsyncCompletionFileFailsafe.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * PDM Async I/O - Transport data asynchronous in R3 using EMT.
|
---|
[27299] | 4 | * Simple File I/O manager.
|
---|
[21496] | 5 | */
|
---|
| 6 |
|
---|
| 7 | /*
|
---|
[82968] | 8 | * Copyright (C) 2006-2020 Oracle Corporation
|
---|
[21496] | 9 | *
|
---|
| 10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
| 11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
| 12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
| 13 | * General Public License (GPL) as published by the Free Software
|
---|
| 14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
| 15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
| 16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
| 17 | */
|
---|
[29250] | 18 |
|
---|
[57358] | 19 |
|
---|
| 20 | /*********************************************************************************************************************************
|
---|
| 21 | * Header Files *
|
---|
| 22 | *********************************************************************************************************************************/
|
---|
[21496] | 23 | #define LOG_GROUP LOG_GROUP_PDM_ASYNC_COMPLETION
|
---|
[29250] | 24 | #include <iprt/asm.h>
|
---|
[23956] | 25 | #include <iprt/assert.h>
|
---|
[21496] | 26 | #include <VBox/log.h>
|
---|
| 27 |
|
---|
| 28 | #include "PDMAsyncCompletionFileInternal.h"
|
---|
| 29 |
|
---|
[80191] | 30 |
|
---|
| 31 |
|
---|
[36001] | 32 | /**
|
---|
| 33 | * Put a list of tasks in the pending request list of an endpoint.
|
---|
| 34 | */
|
---|
| 35 | DECLINLINE(void) pdmacFileAioMgrEpAddTaskList(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint, PPDMACTASKFILE pTaskHead)
|
---|
| 36 | {
|
---|
| 37 | /* Add the rest of the tasks to the pending list */
|
---|
| 38 | if (!pEndpoint->AioMgr.pReqsPendingHead)
|
---|
| 39 | {
|
---|
| 40 | Assert(!pEndpoint->AioMgr.pReqsPendingTail);
|
---|
| 41 | pEndpoint->AioMgr.pReqsPendingHead = pTaskHead;
|
---|
| 42 | }
|
---|
| 43 | else
|
---|
| 44 | {
|
---|
| 45 | Assert(pEndpoint->AioMgr.pReqsPendingTail);
|
---|
| 46 | pEndpoint->AioMgr.pReqsPendingTail->pNext = pTaskHead;
|
---|
| 47 | }
|
---|
[29250] | 48 |
|
---|
[36001] | 49 | /* Update the tail. */
|
---|
| 50 | while (pTaskHead->pNext)
|
---|
| 51 | pTaskHead = pTaskHead->pNext;
|
---|
| 52 |
|
---|
| 53 | pEndpoint->AioMgr.pReqsPendingTail = pTaskHead;
|
---|
| 54 | pTaskHead->pNext = NULL;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | /**
|
---|
| 58 | * Processes a given task list for assigned to the given endpoint.
|
---|
| 59 | */
|
---|
| 60 | static int pdmacFileAioMgrFailsafeProcessEndpointTaskList(PPDMACEPFILEMGR pAioMgr,
|
---|
| 61 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint,
|
---|
[23956] | 62 | PPDMACTASKFILE pTasks)
|
---|
[21496] | 63 | {
|
---|
| 64 | int rc = VINF_SUCCESS;
|
---|
| 65 |
|
---|
| 66 | while (pTasks)
|
---|
| 67 | {
|
---|
[36001] | 68 | RTMSINTERVAL msWhenNext;
|
---|
[22309] | 69 | PPDMACTASKFILE pCurr = pTasks;
|
---|
[21496] | 70 |
|
---|
[36001] | 71 | if (!pdmacEpIsTransferAllowed(&pEndpoint->Core, (uint32_t)pCurr->DataSeg.cbSeg, &msWhenNext))
|
---|
| 72 | {
|
---|
| 73 | pAioMgr->msBwLimitExpired = RT_MIN(pAioMgr->msBwLimitExpired, msWhenNext);
|
---|
| 74 | break;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[22978] | 77 | pTasks = pTasks->pNext;
|
---|
| 78 |
|
---|
[22309] | 79 | switch (pCurr->enmTransferType)
|
---|
[21496] | 80 | {
|
---|
| 81 | case PDMACTASKFILETRANSFER_FLUSH:
|
---|
| 82 | {
|
---|
[37596] | 83 | rc = RTFileFlush(pEndpoint->hFile);
|
---|
[21496] | 84 | break;
|
---|
| 85 | }
|
---|
| 86 | case PDMACTASKFILETRANSFER_READ:
|
---|
| 87 | case PDMACTASKFILETRANSFER_WRITE:
|
---|
| 88 | {
|
---|
[22309] | 89 | if (pCurr->enmTransferType == PDMACTASKFILETRANSFER_READ)
|
---|
[21496] | 90 | {
|
---|
[37596] | 91 | rc = RTFileReadAt(pEndpoint->hFile, pCurr->Off,
|
---|
[22309] | 92 | pCurr->DataSeg.pvSeg,
|
---|
| 93 | pCurr->DataSeg.cbSeg,
|
---|
| 94 | NULL);
|
---|
| 95 | }
|
---|
| 96 | else
|
---|
| 97 | {
|
---|
[25270] | 98 | if (RT_UNLIKELY((uint64_t)pCurr->Off + pCurr->DataSeg.cbSeg > pEndpoint->cbFile))
|
---|
[22978] | 99 | {
|
---|
| 100 | ASMAtomicWriteU64(&pEndpoint->cbFile, pCurr->Off + pCurr->DataSeg.cbSeg);
|
---|
[37596] | 101 | RTFileSetSize(pEndpoint->hFile, pCurr->Off + pCurr->DataSeg.cbSeg);
|
---|
[22978] | 102 | }
|
---|
| 103 |
|
---|
[37596] | 104 | rc = RTFileWriteAt(pEndpoint->hFile, pCurr->Off,
|
---|
[22309] | 105 | pCurr->DataSeg.pvSeg,
|
---|
| 106 | pCurr->DataSeg.cbSeg,
|
---|
[24222] | 107 | NULL);
|
---|
[22309] | 108 | }
|
---|
[21496] | 109 |
|
---|
| 110 | break;
|
---|
| 111 | }
|
---|
| 112 | default:
|
---|
[22309] | 113 | AssertMsgFailed(("Invalid transfer type %d\n", pTasks->enmTransferType));
|
---|
[21496] | 114 | }
|
---|
| 115 |
|
---|
[27920] | 116 | pCurr->pfnCompleted(pCurr, pCurr->pvUser, rc);
|
---|
[22309] | 117 | pdmacFileTaskFree(pEndpoint, pCurr);
|
---|
[21496] | 118 | }
|
---|
| 119 |
|
---|
[36001] | 120 | if (pTasks)
|
---|
| 121 | {
|
---|
| 122 | /* Add the rest of the tasks to the pending list */
|
---|
| 123 | pdmacFileAioMgrEpAddTaskList(pEndpoint, pTasks);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[27920] | 126 | return VINF_SUCCESS;
|
---|
[21496] | 127 | }
|
---|
| 128 |
|
---|
[36001] | 129 | static int pdmacFileAioMgrFailsafeProcessEndpoint(PPDMACEPFILEMGR pAioMgr,
|
---|
| 130 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
|
---|
[23956] | 131 | {
|
---|
| 132 | int rc = VINF_SUCCESS;
|
---|
| 133 | PPDMACTASKFILE pTasks = pEndpoint->AioMgr.pReqsPendingHead;
|
---|
| 134 |
|
---|
| 135 | pEndpoint->AioMgr.pReqsPendingHead = NULL;
|
---|
| 136 | pEndpoint->AioMgr.pReqsPendingTail = NULL;
|
---|
| 137 |
|
---|
| 138 | /* Process the request pending list first in case the endpoint was migrated due to an error. */
|
---|
| 139 | if (pTasks)
|
---|
[36001] | 140 | rc = pdmacFileAioMgrFailsafeProcessEndpointTaskList(pAioMgr, pEndpoint, pTasks);
|
---|
[23956] | 141 |
|
---|
| 142 | if (RT_SUCCESS(rc))
|
---|
| 143 | {
|
---|
| 144 | pTasks = pdmacFileEpGetNewTasks(pEndpoint);
|
---|
| 145 |
|
---|
[23978] | 146 | if (pTasks)
|
---|
[36001] | 147 | rc = pdmacFileAioMgrFailsafeProcessEndpointTaskList(pAioMgr, pEndpoint, pTasks);
|
---|
[23956] | 148 | }
|
---|
| 149 |
|
---|
| 150 | return rc;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[21496] | 153 | /**
|
---|
| 154 | * A fallback method in case something goes wrong with the normal
|
---|
| 155 | * I/O manager.
|
---|
| 156 | */
|
---|
[39078] | 157 | DECLCALLBACK(int) pdmacFileAioMgrFailsafe(RTTHREAD hThreadSelf, void *pvUser)
|
---|
[21496] | 158 | {
|
---|
[39078] | 159 | int rc = VINF_SUCCESS;
|
---|
[21496] | 160 | PPDMACEPFILEMGR pAioMgr = (PPDMACEPFILEMGR)pvUser;
|
---|
[39078] | 161 | NOREF(hThreadSelf);
|
---|
[21496] | 162 |
|
---|
[22309] | 163 | while ( (pAioMgr->enmState == PDMACEPFILEMGRSTATE_RUNNING)
|
---|
| 164 | || (pAioMgr->enmState == PDMACEPFILEMGRSTATE_SUSPENDING))
|
---|
[21496] | 165 | {
|
---|
[30147] | 166 | ASMAtomicWriteBool(&pAioMgr->fWaitingEventSem, true);
|
---|
[21496] | 167 | if (!ASMAtomicReadBool(&pAioMgr->fWokenUp))
|
---|
[36001] | 168 | rc = RTSemEventWait(pAioMgr->EventSem, pAioMgr->msBwLimitExpired);
|
---|
[30147] | 169 | ASMAtomicWriteBool(&pAioMgr->fWaitingEventSem, false);
|
---|
[36001] | 170 | Assert(RT_SUCCESS(rc) || rc == VERR_TIMEOUT);
|
---|
[21496] | 171 |
|
---|
[30147] | 172 | LogFlow(("Got woken up\n"));
|
---|
| 173 | ASMAtomicWriteBool(&pAioMgr->fWokenUp, false);
|
---|
| 174 |
|
---|
[21496] | 175 | /* Process endpoint events first. */
|
---|
| 176 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint = pAioMgr->pEndpointsHead;
|
---|
| 177 | while (pEndpoint)
|
---|
| 178 | {
|
---|
[36001] | 179 | pAioMgr->msBwLimitExpired = RT_INDEFINITE_WAIT;
|
---|
| 180 | rc = pdmacFileAioMgrFailsafeProcessEndpoint(pAioMgr, pEndpoint);
|
---|
[21496] | 181 | AssertRC(rc);
|
---|
| 182 | pEndpoint = pEndpoint->AioMgr.pEndpointNext;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | /* Now check for an external blocking event. */
|
---|
| 186 | if (pAioMgr->fBlockingEventPending)
|
---|
| 187 | {
|
---|
| 188 | switch (pAioMgr->enmBlockingEvent)
|
---|
| 189 | {
|
---|
| 190 | case PDMACEPFILEAIOMGRBLOCKINGEVENT_ADD_ENDPOINT:
|
---|
| 191 | {
|
---|
| 192 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpointNew = pAioMgr->BlockingEventData.AddEndpoint.pEndpoint;
|
---|
| 193 | AssertMsg(VALID_PTR(pEndpointNew), ("Adding endpoint event without a endpoint to add\n"));
|
---|
| 194 |
|
---|
[22309] | 195 | pEndpointNew->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE;
|
---|
| 196 |
|
---|
[21496] | 197 | pEndpointNew->AioMgr.pEndpointNext = pAioMgr->pEndpointsHead;
|
---|
| 198 | pEndpointNew->AioMgr.pEndpointPrev = NULL;
|
---|
| 199 | if (pAioMgr->pEndpointsHead)
|
---|
| 200 | pAioMgr->pEndpointsHead->AioMgr.pEndpointPrev = pEndpointNew;
|
---|
| 201 | pAioMgr->pEndpointsHead = pEndpointNew;
|
---|
[24121] | 202 |
|
---|
[24358] | 203 | pAioMgr->cEndpoints++;
|
---|
| 204 |
|
---|
[24121] | 205 | /*
|
---|
| 206 | * Process the task list the first time. There might be pending requests
|
---|
| 207 | * if the endpoint was migrated from another endpoint.
|
---|
| 208 | */
|
---|
[36001] | 209 | rc = pdmacFileAioMgrFailsafeProcessEndpoint(pAioMgr, pEndpointNew);
|
---|
[24121] | 210 | AssertRC(rc);
|
---|
[21496] | 211 | break;
|
---|
| 212 | }
|
---|
| 213 | case PDMACEPFILEAIOMGRBLOCKINGEVENT_REMOVE_ENDPOINT:
|
---|
| 214 | {
|
---|
| 215 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpointRemove = pAioMgr->BlockingEventData.RemoveEndpoint.pEndpoint;
|
---|
| 216 | AssertMsg(VALID_PTR(pEndpointRemove), ("Removing endpoint event without a endpoint to remove\n"));
|
---|
| 217 |
|
---|
[22309] | 218 | pEndpointRemove->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_REMOVING;
|
---|
| 219 |
|
---|
[21496] | 220 | PPDMASYNCCOMPLETIONENDPOINTFILE pPrev = pEndpointRemove->AioMgr.pEndpointPrev;
|
---|
| 221 | PPDMASYNCCOMPLETIONENDPOINTFILE pNext = pEndpointRemove->AioMgr.pEndpointNext;
|
---|
| 222 |
|
---|
| 223 | if (pPrev)
|
---|
| 224 | pPrev->AioMgr.pEndpointNext = pNext;
|
---|
| 225 | else
|
---|
| 226 | pAioMgr->pEndpointsHead = pNext;
|
---|
| 227 |
|
---|
| 228 | if (pNext)
|
---|
| 229 | pNext->AioMgr.pEndpointPrev = pPrev;
|
---|
[24358] | 230 |
|
---|
| 231 | pAioMgr->cEndpoints--;
|
---|
[21496] | 232 | break;
|
---|
| 233 | }
|
---|
| 234 | case PDMACEPFILEAIOMGRBLOCKINGEVENT_CLOSE_ENDPOINT:
|
---|
| 235 | {
|
---|
| 236 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpointClose = pAioMgr->BlockingEventData.CloseEndpoint.pEndpoint;
|
---|
| 237 | AssertMsg(VALID_PTR(pEndpointClose), ("Close endpoint event without a endpoint to Close\n"));
|
---|
| 238 |
|
---|
[22309] | 239 | pEndpointClose->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_CLOSING;
|
---|
| 240 |
|
---|
[21496] | 241 | /* Make sure all tasks finished. */
|
---|
[36001] | 242 | rc = pdmacFileAioMgrFailsafeProcessEndpoint(pAioMgr, pEndpointClose);
|
---|
[21496] | 243 | AssertRC(rc);
|
---|
[22979] | 244 | break;
|
---|
[21496] | 245 | }
|
---|
| 246 | case PDMACEPFILEAIOMGRBLOCKINGEVENT_SHUTDOWN:
|
---|
[22979] | 247 | pAioMgr->enmState = PDMACEPFILEMGRSTATE_SHUTDOWN;
|
---|
[21496] | 248 | break;
|
---|
| 249 | case PDMACEPFILEAIOMGRBLOCKINGEVENT_SUSPEND:
|
---|
[22979] | 250 | pAioMgr->enmState = PDMACEPFILEMGRSTATE_SUSPENDING;
|
---|
[21496] | 251 | break;
|
---|
[22979] | 252 | case PDMACEPFILEAIOMGRBLOCKINGEVENT_RESUME:
|
---|
| 253 | pAioMgr->enmState = PDMACEPFILEMGRSTATE_RUNNING;
|
---|
| 254 | break;
|
---|
[21496] | 255 | default:
|
---|
| 256 | AssertMsgFailed(("Invalid event type %d\n", pAioMgr->enmBlockingEvent));
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[30131] | 259 | ASMAtomicWriteBool(&pAioMgr->fBlockingEventPending, false);
|
---|
| 260 | pAioMgr->enmBlockingEvent = PDMACEPFILEAIOMGRBLOCKINGEVENT_INVALID;
|
---|
| 261 |
|
---|
[21496] | 262 | /* Release the waiting thread. */
|
---|
| 263 | rc = RTSemEventSignal(pAioMgr->EventSemBlock);
|
---|
| 264 | AssertRC(rc);
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | return rc;
|
---|
| 269 | }
|
---|
| 270 |
|
---|