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