VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/PDMAsyncCompletionFileFailsafe.cpp@ 82968

Last change on this file since 82968 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.1 KB
Line 
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.
4 * Simple File I/O manager.
5 */
6
7/*
8 * Copyright (C) 2006-2020 Oracle Corporation
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 */
18
19
20/*********************************************************************************************************************************
21* Header Files *
22*********************************************************************************************************************************/
23#define LOG_GROUP LOG_GROUP_PDM_ASYNC_COMPLETION
24#include <iprt/asm.h>
25#include <iprt/assert.h>
26#include <VBox/log.h>
27
28#include "PDMAsyncCompletionFileInternal.h"
29
30
31
32/**
33 * Put a list of tasks in the pending request list of an endpoint.
34 */
35DECLINLINE(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 }
48
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 */
60static int pdmacFileAioMgrFailsafeProcessEndpointTaskList(PPDMACEPFILEMGR pAioMgr,
61 PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint,
62 PPDMACTASKFILE pTasks)
63{
64 int rc = VINF_SUCCESS;
65
66 while (pTasks)
67 {
68 RTMSINTERVAL msWhenNext;
69 PPDMACTASKFILE pCurr = pTasks;
70
71 if (!pdmacEpIsTransferAllowed(&pEndpoint->Core, (uint32_t)pCurr->DataSeg.cbSeg, &msWhenNext))
72 {
73 pAioMgr->msBwLimitExpired = RT_MIN(pAioMgr->msBwLimitExpired, msWhenNext);
74 break;
75 }
76
77 pTasks = pTasks->pNext;
78
79 switch (pCurr->enmTransferType)
80 {
81 case PDMACTASKFILETRANSFER_FLUSH:
82 {
83 rc = RTFileFlush(pEndpoint->hFile);
84 break;
85 }
86 case PDMACTASKFILETRANSFER_READ:
87 case PDMACTASKFILETRANSFER_WRITE:
88 {
89 if (pCurr->enmTransferType == PDMACTASKFILETRANSFER_READ)
90 {
91 rc = RTFileReadAt(pEndpoint->hFile, pCurr->Off,
92 pCurr->DataSeg.pvSeg,
93 pCurr->DataSeg.cbSeg,
94 NULL);
95 }
96 else
97 {
98 if (RT_UNLIKELY((uint64_t)pCurr->Off + pCurr->DataSeg.cbSeg > pEndpoint->cbFile))
99 {
100 ASMAtomicWriteU64(&pEndpoint->cbFile, pCurr->Off + pCurr->DataSeg.cbSeg);
101 RTFileSetSize(pEndpoint->hFile, pCurr->Off + pCurr->DataSeg.cbSeg);
102 }
103
104 rc = RTFileWriteAt(pEndpoint->hFile, pCurr->Off,
105 pCurr->DataSeg.pvSeg,
106 pCurr->DataSeg.cbSeg,
107 NULL);
108 }
109
110 break;
111 }
112 default:
113 AssertMsgFailed(("Invalid transfer type %d\n", pTasks->enmTransferType));
114 }
115
116 pCurr->pfnCompleted(pCurr, pCurr->pvUser, rc);
117 pdmacFileTaskFree(pEndpoint, pCurr);
118 }
119
120 if (pTasks)
121 {
122 /* Add the rest of the tasks to the pending list */
123 pdmacFileAioMgrEpAddTaskList(pEndpoint, pTasks);
124 }
125
126 return VINF_SUCCESS;
127}
128
129static int pdmacFileAioMgrFailsafeProcessEndpoint(PPDMACEPFILEMGR pAioMgr,
130 PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
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)
140 rc = pdmacFileAioMgrFailsafeProcessEndpointTaskList(pAioMgr, pEndpoint, pTasks);
141
142 if (RT_SUCCESS(rc))
143 {
144 pTasks = pdmacFileEpGetNewTasks(pEndpoint);
145
146 if (pTasks)
147 rc = pdmacFileAioMgrFailsafeProcessEndpointTaskList(pAioMgr, pEndpoint, pTasks);
148 }
149
150 return rc;
151}
152
153/**
154 * A fallback method in case something goes wrong with the normal
155 * I/O manager.
156 */
157DECLCALLBACK(int) pdmacFileAioMgrFailsafe(RTTHREAD hThreadSelf, void *pvUser)
158{
159 int rc = VINF_SUCCESS;
160 PPDMACEPFILEMGR pAioMgr = (PPDMACEPFILEMGR)pvUser;
161 NOREF(hThreadSelf);
162
163 while ( (pAioMgr->enmState == PDMACEPFILEMGRSTATE_RUNNING)
164 || (pAioMgr->enmState == PDMACEPFILEMGRSTATE_SUSPENDING))
165 {
166 ASMAtomicWriteBool(&pAioMgr->fWaitingEventSem, true);
167 if (!ASMAtomicReadBool(&pAioMgr->fWokenUp))
168 rc = RTSemEventWait(pAioMgr->EventSem, pAioMgr->msBwLimitExpired);
169 ASMAtomicWriteBool(&pAioMgr->fWaitingEventSem, false);
170 Assert(RT_SUCCESS(rc) || rc == VERR_TIMEOUT);
171
172 LogFlow(("Got woken up\n"));
173 ASMAtomicWriteBool(&pAioMgr->fWokenUp, false);
174
175 /* Process endpoint events first. */
176 PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint = pAioMgr->pEndpointsHead;
177 while (pEndpoint)
178 {
179 pAioMgr->msBwLimitExpired = RT_INDEFINITE_WAIT;
180 rc = pdmacFileAioMgrFailsafeProcessEndpoint(pAioMgr, pEndpoint);
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
195 pEndpointNew->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE;
196
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;
202
203 pAioMgr->cEndpoints++;
204
205 /*
206 * Process the task list the first time. There might be pending requests
207 * if the endpoint was migrated from another endpoint.
208 */
209 rc = pdmacFileAioMgrFailsafeProcessEndpoint(pAioMgr, pEndpointNew);
210 AssertRC(rc);
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
218 pEndpointRemove->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_REMOVING;
219
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;
230
231 pAioMgr->cEndpoints--;
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
239 pEndpointClose->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_CLOSING;
240
241 /* Make sure all tasks finished. */
242 rc = pdmacFileAioMgrFailsafeProcessEndpoint(pAioMgr, pEndpointClose);
243 AssertRC(rc);
244 break;
245 }
246 case PDMACEPFILEAIOMGRBLOCKINGEVENT_SHUTDOWN:
247 pAioMgr->enmState = PDMACEPFILEMGRSTATE_SHUTDOWN;
248 break;
249 case PDMACEPFILEAIOMGRBLOCKINGEVENT_SUSPEND:
250 pAioMgr->enmState = PDMACEPFILEMGRSTATE_SUSPENDING;
251 break;
252 case PDMACEPFILEAIOMGRBLOCKINGEVENT_RESUME:
253 pAioMgr->enmState = PDMACEPFILEMGRSTATE_RUNNING;
254 break;
255 default:
256 AssertMsgFailed(("Invalid event type %d\n", pAioMgr->enmBlockingEvent));
257 }
258
259 ASMAtomicWriteBool(&pAioMgr->fBlockingEventPending, false);
260 pAioMgr->enmBlockingEvent = PDMACEPFILEAIOMGRBLOCKINGEVENT_INVALID;
261
262 /* Release the waiting thread. */
263 rc = RTSemEventSignal(pAioMgr->EventSemBlock);
264 AssertRC(rc);
265 }
266 }
267
268 return rc;
269}
270
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette