1 | /* $Id: PDMAsyncCompletionFile.cpp 29450 2010-05-13 15:35:35Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PDM Async I/O - Transport data asynchronous in R3 using EMT.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2009 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_PDM_ASYNC_COMPLETION
|
---|
23 | #include "PDMInternal.h"
|
---|
24 | #include <VBox/pdm.h>
|
---|
25 | #include <VBox/mm.h>
|
---|
26 | #include <VBox/vm.h>
|
---|
27 | #include <VBox/err.h>
|
---|
28 | #include <VBox/log.h>
|
---|
29 |
|
---|
30 | #include <iprt/asm.h>
|
---|
31 | #include <iprt/assert.h>
|
---|
32 | #include <iprt/critsect.h>
|
---|
33 | #include <iprt/env.h>
|
---|
34 | #include <iprt/file.h>
|
---|
35 | #include <iprt/mem.h>
|
---|
36 | #include <iprt/semaphore.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 | #include <iprt/thread.h>
|
---|
39 | #include <iprt/path.h>
|
---|
40 |
|
---|
41 | #include "PDMAsyncCompletionFileInternal.h"
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Frees a task.
|
---|
45 | *
|
---|
46 | * @returns nothing.
|
---|
47 | * @param pEndpoint Pointer to the endpoint the segment was for.
|
---|
48 | * @param pTask The task to free.
|
---|
49 | */
|
---|
50 | void pdmacFileTaskFree(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint,
|
---|
51 | PPDMACTASKFILE pTask)
|
---|
52 | {
|
---|
53 | PPDMASYNCCOMPLETIONEPCLASSFILE pEpClass = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->Core.pEpClass;
|
---|
54 |
|
---|
55 | LogFlowFunc((": pEndpoint=%p pTask=%p\n", pEndpoint, pTask));
|
---|
56 |
|
---|
57 | /* Try the per endpoint cache first. */
|
---|
58 | if (pEndpoint->cTasksCached < pEpClass->cTasksCacheMax)
|
---|
59 | {
|
---|
60 | /* Add it to the list. */
|
---|
61 | pEndpoint->pTasksFreeTail->pNext = pTask;
|
---|
62 | pEndpoint->pTasksFreeTail = pTask;
|
---|
63 | ASMAtomicIncU32(&pEndpoint->cTasksCached);
|
---|
64 | }
|
---|
65 | else
|
---|
66 | {
|
---|
67 | Log(("Freeing task %p because all caches are full\n", pTask));
|
---|
68 | MMR3HeapFree(pTask);
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Allocates a task segment
|
---|
74 | *
|
---|
75 | * @returns Pointer to the new task segment or NULL
|
---|
76 | * @param pEndpoint Pointer to the endpoint
|
---|
77 | */
|
---|
78 | PPDMACTASKFILE pdmacFileTaskAlloc(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
|
---|
79 | {
|
---|
80 | PPDMACTASKFILE pTask = NULL;
|
---|
81 |
|
---|
82 | /* Try the small per endpoint cache first. */
|
---|
83 | if (pEndpoint->pTasksFreeHead == pEndpoint->pTasksFreeTail)
|
---|
84 | {
|
---|
85 | /* Try the bigger endpoint class cache. */
|
---|
86 | PPDMASYNCCOMPLETIONEPCLASSFILE pEndpointClass = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->Core.pEpClass;
|
---|
87 |
|
---|
88 | /*
|
---|
89 | * Allocate completely new.
|
---|
90 | * If this fails we return NULL.
|
---|
91 | */
|
---|
92 | int rc = MMR3HeapAllocZEx(pEndpointClass->Core.pVM, MM_TAG_PDM_ASYNC_COMPLETION,
|
---|
93 | sizeof(PDMACTASKFILE),
|
---|
94 | (void **)&pTask);
|
---|
95 | if (RT_FAILURE(rc))
|
---|
96 | pTask = NULL;
|
---|
97 |
|
---|
98 | LogFlow(("Allocated task %p\n", pTask));
|
---|
99 | }
|
---|
100 | else
|
---|
101 | {
|
---|
102 | /* Grab a free task from the head. */
|
---|
103 | AssertMsg(pEndpoint->cTasksCached > 0, ("No tasks cached but list contains more than one element\n"));
|
---|
104 |
|
---|
105 | pTask = pEndpoint->pTasksFreeHead;
|
---|
106 | pEndpoint->pTasksFreeHead = pTask->pNext;
|
---|
107 | ASMAtomicDecU32(&pEndpoint->cTasksCached);
|
---|
108 | }
|
---|
109 |
|
---|
110 | pTask->pNext = NULL;
|
---|
111 |
|
---|
112 | return pTask;
|
---|
113 | }
|
---|
114 |
|
---|
115 | PPDMACTASKFILE pdmacFileEpGetNewTasks(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
|
---|
116 | {
|
---|
117 | PPDMACTASKFILE pTasks = NULL;
|
---|
118 |
|
---|
119 | /*
|
---|
120 | * Get pending tasks.
|
---|
121 | */
|
---|
122 | pTasks = (PPDMACTASKFILE)ASMAtomicXchgPtr((void * volatile *)&pEndpoint->pTasksNewHead, NULL);
|
---|
123 |
|
---|
124 | /* Reverse the list to process in FIFO order. */
|
---|
125 | if (pTasks)
|
---|
126 | {
|
---|
127 | PPDMACTASKFILE pTask = pTasks;
|
---|
128 |
|
---|
129 | pTasks = NULL;
|
---|
130 |
|
---|
131 | while (pTask)
|
---|
132 | {
|
---|
133 | PPDMACTASKFILE pCur = pTask;
|
---|
134 | pTask = pTask->pNext;
|
---|
135 | pCur->pNext = pTasks;
|
---|
136 | pTasks = pCur;
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | return pTasks;
|
---|
141 | }
|
---|
142 |
|
---|
143 | static void pdmacFileAioMgrWakeup(PPDMACEPFILEMGR pAioMgr)
|
---|
144 | {
|
---|
145 | bool fWokenUp = ASMAtomicXchgBool(&pAioMgr->fWokenUp, true);
|
---|
146 |
|
---|
147 | if (!fWokenUp)
|
---|
148 | {
|
---|
149 | int rc = VINF_SUCCESS;
|
---|
150 | bool fWaitingEventSem = ASMAtomicReadBool(&pAioMgr->fWaitingEventSem);
|
---|
151 |
|
---|
152 | if (fWaitingEventSem)
|
---|
153 | rc = RTSemEventSignal(pAioMgr->EventSem);
|
---|
154 |
|
---|
155 | AssertRC(rc);
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | static int pdmacFileAioMgrWaitForBlockingEvent(PPDMACEPFILEMGR pAioMgr, PDMACEPFILEAIOMGRBLOCKINGEVENT enmEvent)
|
---|
160 | {
|
---|
161 | int rc = VINF_SUCCESS;
|
---|
162 |
|
---|
163 | ASMAtomicWriteU32((volatile uint32_t *)&pAioMgr->enmBlockingEvent, enmEvent);
|
---|
164 | Assert(!pAioMgr->fBlockingEventPending);
|
---|
165 | ASMAtomicXchgBool(&pAioMgr->fBlockingEventPending, true);
|
---|
166 |
|
---|
167 | /* Wakeup the async I/O manager */
|
---|
168 | pdmacFileAioMgrWakeup(pAioMgr);
|
---|
169 |
|
---|
170 | /* Wait for completion. */
|
---|
171 | rc = RTSemEventWait(pAioMgr->EventSemBlock, RT_INDEFINITE_WAIT);
|
---|
172 | AssertRC(rc);
|
---|
173 |
|
---|
174 | ASMAtomicXchgBool(&pAioMgr->fBlockingEventPending, false);
|
---|
175 | ASMAtomicWriteU32((volatile uint32_t *)&pAioMgr->enmBlockingEvent, PDMACEPFILEAIOMGRBLOCKINGEVENT_INVALID);
|
---|
176 |
|
---|
177 | return rc;
|
---|
178 | }
|
---|
179 |
|
---|
180 | int pdmacFileAioMgrAddEndpoint(PPDMACEPFILEMGR pAioMgr, PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
|
---|
181 | {
|
---|
182 | int rc;
|
---|
183 |
|
---|
184 | rc = RTCritSectEnter(&pAioMgr->CritSectBlockingEvent);
|
---|
185 | AssertRCReturn(rc, rc);
|
---|
186 |
|
---|
187 | ASMAtomicWritePtr((void * volatile *)&pAioMgr->BlockingEventData.AddEndpoint.pEndpoint, pEndpoint);
|
---|
188 | rc = pdmacFileAioMgrWaitForBlockingEvent(pAioMgr, PDMACEPFILEAIOMGRBLOCKINGEVENT_ADD_ENDPOINT);
|
---|
189 |
|
---|
190 | RTCritSectLeave(&pAioMgr->CritSectBlockingEvent);
|
---|
191 |
|
---|
192 | if (RT_SUCCESS(rc))
|
---|
193 | ASMAtomicWritePtr((void * volatile *)&pEndpoint->pAioMgr, pAioMgr);
|
---|
194 |
|
---|
195 | return rc;
|
---|
196 | }
|
---|
197 |
|
---|
198 | static int pdmacFileAioMgrRemoveEndpoint(PPDMACEPFILEMGR pAioMgr, PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
|
---|
199 | {
|
---|
200 | int rc;
|
---|
201 |
|
---|
202 | rc = RTCritSectEnter(&pAioMgr->CritSectBlockingEvent);
|
---|
203 | AssertRCReturn(rc, rc);
|
---|
204 |
|
---|
205 | ASMAtomicWritePtr((void * volatile *)&pAioMgr->BlockingEventData.RemoveEndpoint.pEndpoint, pEndpoint);
|
---|
206 | rc = pdmacFileAioMgrWaitForBlockingEvent(pAioMgr, PDMACEPFILEAIOMGRBLOCKINGEVENT_REMOVE_ENDPOINT);
|
---|
207 |
|
---|
208 | RTCritSectLeave(&pAioMgr->CritSectBlockingEvent);
|
---|
209 |
|
---|
210 | return rc;
|
---|
211 | }
|
---|
212 |
|
---|
213 | static int pdmacFileAioMgrCloseEndpoint(PPDMACEPFILEMGR pAioMgr, PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
|
---|
214 | {
|
---|
215 | int rc;
|
---|
216 |
|
---|
217 | rc = RTCritSectEnter(&pAioMgr->CritSectBlockingEvent);
|
---|
218 | AssertRCReturn(rc, rc);
|
---|
219 |
|
---|
220 | ASMAtomicWritePtr((void * volatile *)&pAioMgr->BlockingEventData.CloseEndpoint.pEndpoint, pEndpoint);
|
---|
221 | rc = pdmacFileAioMgrWaitForBlockingEvent(pAioMgr, PDMACEPFILEAIOMGRBLOCKINGEVENT_CLOSE_ENDPOINT);
|
---|
222 |
|
---|
223 | RTCritSectLeave(&pAioMgr->CritSectBlockingEvent);
|
---|
224 |
|
---|
225 | return rc;
|
---|
226 | }
|
---|
227 |
|
---|
228 | static int pdmacFileAioMgrShutdown(PPDMACEPFILEMGR pAioMgr)
|
---|
229 | {
|
---|
230 | int rc;
|
---|
231 |
|
---|
232 | rc = RTCritSectEnter(&pAioMgr->CritSectBlockingEvent);
|
---|
233 | AssertRCReturn(rc, rc);
|
---|
234 |
|
---|
235 | rc = pdmacFileAioMgrWaitForBlockingEvent(pAioMgr, PDMACEPFILEAIOMGRBLOCKINGEVENT_SHUTDOWN);
|
---|
236 |
|
---|
237 | RTCritSectLeave(&pAioMgr->CritSectBlockingEvent);
|
---|
238 |
|
---|
239 | return rc;
|
---|
240 | }
|
---|
241 |
|
---|
242 | int pdmacFileEpAddTask(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint, PPDMACTASKFILE pTask)
|
---|
243 | {
|
---|
244 | PPDMACTASKFILE pNext;
|
---|
245 | do
|
---|
246 | {
|
---|
247 | pNext = pEndpoint->pTasksNewHead;
|
---|
248 | pTask->pNext = pNext;
|
---|
249 | } while (!ASMAtomicCmpXchgPtr((void * volatile *)&pEndpoint->pTasksNewHead, (void *)pTask, (void *)pNext));
|
---|
250 |
|
---|
251 | pdmacFileAioMgrWakeup((PPDMACEPFILEMGR)ASMAtomicReadPtr((void * volatile *)&pEndpoint->pAioMgr));
|
---|
252 |
|
---|
253 | return VINF_SUCCESS;
|
---|
254 | }
|
---|
255 |
|
---|
256 | void pdmacFileEpTaskCompleted(PPDMACTASKFILE pTask, void *pvUser, int rc)
|
---|
257 | {
|
---|
258 | PPDMASYNCCOMPLETIONTASKFILE pTaskFile = (PPDMASYNCCOMPLETIONTASKFILE)pvUser;
|
---|
259 |
|
---|
260 | LogFlowFunc(("pTask=%#p pvUser=%#p rc=%Rrc\n", pTask, pvUser, rc));
|
---|
261 |
|
---|
262 | if (pTask->enmTransferType == PDMACTASKFILETRANSFER_FLUSH)
|
---|
263 | {
|
---|
264 | pdmR3AsyncCompletionCompleteTask(&pTaskFile->Core, rc, true);
|
---|
265 | }
|
---|
266 | else
|
---|
267 | {
|
---|
268 | Assert((uint32_t)pTask->DataSeg.cbSeg == pTask->DataSeg.cbSeg && (int32_t)pTask->DataSeg.cbSeg >= 0);
|
---|
269 | uint32_t uOld = ASMAtomicSubS32(&pTaskFile->cbTransferLeft, (int32_t)pTask->DataSeg.cbSeg);
|
---|
270 |
|
---|
271 | /* The first error will be returned. */
|
---|
272 | if (RT_FAILURE(rc))
|
---|
273 | ASMAtomicCmpXchgS32(&pTaskFile->rc, rc, VINF_SUCCESS);
|
---|
274 |
|
---|
275 | if (!(uOld - pTask->DataSeg.cbSeg)
|
---|
276 | && !ASMAtomicXchgBool(&pTaskFile->fCompleted, true))
|
---|
277 | pdmR3AsyncCompletionCompleteTask(&pTaskFile->Core, pTaskFile->rc, true);
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 | DECLINLINE(void) pdmacFileEpTaskInit(PPDMASYNCCOMPLETIONTASK pTask, size_t cbTransfer)
|
---|
282 | {
|
---|
283 | PPDMASYNCCOMPLETIONTASKFILE pTaskFile = (PPDMASYNCCOMPLETIONTASKFILE)pTask;
|
---|
284 |
|
---|
285 | Assert((uint32_t)cbTransfer == cbTransfer && (int32_t)cbTransfer >= 0);
|
---|
286 | ASMAtomicWriteS32(&pTaskFile->cbTransferLeft, (int32_t)cbTransfer);
|
---|
287 | ASMAtomicWriteBool(&pTaskFile->fCompleted, false);
|
---|
288 | ASMAtomicWriteS32(&pTaskFile->rc, VINF_SUCCESS);
|
---|
289 | }
|
---|
290 |
|
---|
291 | int pdmacFileEpTaskInitiate(PPDMASYNCCOMPLETIONTASK pTask,
|
---|
292 | PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
|
---|
293 | PCRTSGSEG paSegments, size_t cSegments,
|
---|
294 | size_t cbTransfer, PDMACTASKFILETRANSFER enmTransfer)
|
---|
295 | {
|
---|
296 | int rc = VINF_SUCCESS;
|
---|
297 | PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
|
---|
298 | PPDMASYNCCOMPLETIONTASKFILE pTaskFile = (PPDMASYNCCOMPLETIONTASKFILE)pTask;
|
---|
299 | PPDMACEPFILEMGR pAioMgr = pEpFile->pAioMgr;
|
---|
300 |
|
---|
301 | Assert( (enmTransfer == PDMACTASKFILETRANSFER_READ)
|
---|
302 | || (enmTransfer == PDMACTASKFILETRANSFER_WRITE));
|
---|
303 |
|
---|
304 | for (unsigned i = 0; i < cSegments; i++)
|
---|
305 | {
|
---|
306 | PPDMACTASKFILE pIoTask = pdmacFileTaskAlloc(pEpFile);
|
---|
307 | AssertPtr(pIoTask);
|
---|
308 |
|
---|
309 | pIoTask->pEndpoint = pEpFile;
|
---|
310 | pIoTask->enmTransferType = enmTransfer;
|
---|
311 | pIoTask->Off = off;
|
---|
312 | pIoTask->DataSeg.cbSeg = paSegments[i].cbSeg;
|
---|
313 | pIoTask->DataSeg.pvSeg = paSegments[i].pvSeg;
|
---|
314 | pIoTask->pvUser = pTaskFile;
|
---|
315 | pIoTask->pfnCompleted = pdmacFileEpTaskCompleted;
|
---|
316 |
|
---|
317 | /* Send it off to the I/O manager. */
|
---|
318 | pdmacFileEpAddTask(pEpFile, pIoTask);
|
---|
319 | off += paSegments[i].cbSeg;
|
---|
320 | cbTransfer -= paSegments[i].cbSeg;
|
---|
321 | }
|
---|
322 |
|
---|
323 | AssertMsg(!cbTransfer, ("Incomplete transfer %u bytes left\n", cbTransfer));
|
---|
324 |
|
---|
325 | if (ASMAtomicReadS32(&pTaskFile->cbTransferLeft) == 0
|
---|
326 | && !ASMAtomicXchgBool(&pTaskFile->fCompleted, true))
|
---|
327 | pdmR3AsyncCompletionCompleteTask(pTask, pTaskFile->rc, false);
|
---|
328 | else
|
---|
329 | rc = VINF_AIO_TASK_PENDING;
|
---|
330 |
|
---|
331 | return rc;
|
---|
332 | }
|
---|
333 |
|
---|
334 | /**
|
---|
335 | * Creates a new async I/O manager.
|
---|
336 | *
|
---|
337 | * @returns VBox status code.
|
---|
338 | * @param pEpClass Pointer to the endpoint class data.
|
---|
339 | * @param ppAioMgr Where to store the pointer to the new async I/O manager on success.
|
---|
340 | * @param enmMgrType Wanted manager type - can be overwritten by the global override.
|
---|
341 | */
|
---|
342 | int pdmacFileAioMgrCreate(PPDMASYNCCOMPLETIONEPCLASSFILE pEpClass, PPPDMACEPFILEMGR ppAioMgr,
|
---|
343 | PDMACEPFILEMGRTYPE enmMgrType)
|
---|
344 | {
|
---|
345 | int rc = VINF_SUCCESS;
|
---|
346 | PPDMACEPFILEMGR pAioMgrNew;
|
---|
347 |
|
---|
348 | LogFlowFunc((": Entered\n"));
|
---|
349 |
|
---|
350 | rc = MMR3HeapAllocZEx(pEpClass->Core.pVM, MM_TAG_PDM_ASYNC_COMPLETION, sizeof(PDMACEPFILEMGR), (void **)&pAioMgrNew);
|
---|
351 | if (RT_SUCCESS(rc))
|
---|
352 | {
|
---|
353 | if (enmMgrType < pEpClass->enmMgrTypeOverride)
|
---|
354 | pAioMgrNew->enmMgrType = enmMgrType;
|
---|
355 | else
|
---|
356 | pAioMgrNew->enmMgrType = pEpClass->enmMgrTypeOverride;
|
---|
357 |
|
---|
358 | rc = RTSemEventCreate(&pAioMgrNew->EventSem);
|
---|
359 | if (RT_SUCCESS(rc))
|
---|
360 | {
|
---|
361 | rc = RTSemEventCreate(&pAioMgrNew->EventSemBlock);
|
---|
362 | if (RT_SUCCESS(rc))
|
---|
363 | {
|
---|
364 | rc = RTCritSectInit(&pAioMgrNew->CritSectBlockingEvent);
|
---|
365 | if (RT_SUCCESS(rc))
|
---|
366 | {
|
---|
367 | /* Init the rest of the manager. */
|
---|
368 | if (pAioMgrNew->enmMgrType != PDMACEPFILEMGRTYPE_SIMPLE)
|
---|
369 | rc = pdmacFileAioMgrNormalInit(pAioMgrNew);
|
---|
370 |
|
---|
371 | if (RT_SUCCESS(rc))
|
---|
372 | {
|
---|
373 | pAioMgrNew->enmState = PDMACEPFILEMGRSTATE_RUNNING;
|
---|
374 |
|
---|
375 | rc = RTThreadCreateF(&pAioMgrNew->Thread,
|
---|
376 | pAioMgrNew->enmMgrType == PDMACEPFILEMGRTYPE_SIMPLE
|
---|
377 | ? pdmacFileAioMgrFailsafe
|
---|
378 | : pdmacFileAioMgrNormal,
|
---|
379 | pAioMgrNew,
|
---|
380 | 0,
|
---|
381 | RTTHREADTYPE_IO,
|
---|
382 | 0,
|
---|
383 | "AioMgr%d-%s", pEpClass->cAioMgrs,
|
---|
384 | pAioMgrNew->enmMgrType == PDMACEPFILEMGRTYPE_SIMPLE
|
---|
385 | ? "F"
|
---|
386 | : "N");
|
---|
387 | if (RT_SUCCESS(rc))
|
---|
388 | {
|
---|
389 | /* Link it into the list. */
|
---|
390 | RTCritSectEnter(&pEpClass->CritSect);
|
---|
391 | pAioMgrNew->pNext = pEpClass->pAioMgrHead;
|
---|
392 | if (pEpClass->pAioMgrHead)
|
---|
393 | pEpClass->pAioMgrHead->pPrev = pAioMgrNew;
|
---|
394 | pEpClass->pAioMgrHead = pAioMgrNew;
|
---|
395 | pEpClass->cAioMgrs++;
|
---|
396 | RTCritSectLeave(&pEpClass->CritSect);
|
---|
397 |
|
---|
398 | *ppAioMgr = pAioMgrNew;
|
---|
399 |
|
---|
400 | Log(("PDMAC: Successfully created new file AIO Mgr {%s}\n", RTThreadGetName(pAioMgrNew->Thread)));
|
---|
401 | return VINF_SUCCESS;
|
---|
402 | }
|
---|
403 | pdmacFileAioMgrNormalDestroy(pAioMgrNew);
|
---|
404 | }
|
---|
405 | RTCritSectDelete(&pAioMgrNew->CritSectBlockingEvent);
|
---|
406 | }
|
---|
407 | RTSemEventDestroy(pAioMgrNew->EventSem);
|
---|
408 | }
|
---|
409 | RTSemEventDestroy(pAioMgrNew->EventSemBlock);
|
---|
410 | }
|
---|
411 | MMR3HeapFree(pAioMgrNew);
|
---|
412 | }
|
---|
413 |
|
---|
414 | LogFlowFunc((": Leave rc=%Rrc\n", rc));
|
---|
415 |
|
---|
416 | return rc;
|
---|
417 | }
|
---|
418 |
|
---|
419 | /**
|
---|
420 | * Destroys a async I/O manager.
|
---|
421 | *
|
---|
422 | * @returns nothing.
|
---|
423 | * @param pAioMgr The async I/O manager to destroy.
|
---|
424 | */
|
---|
425 | static void pdmacFileAioMgrDestroy(PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile, PPDMACEPFILEMGR pAioMgr)
|
---|
426 | {
|
---|
427 | int rc = pdmacFileAioMgrShutdown(pAioMgr);
|
---|
428 | AssertRC(rc);
|
---|
429 |
|
---|
430 | /* Unlink from the list. */
|
---|
431 | rc = RTCritSectEnter(&pEpClassFile->CritSect);
|
---|
432 | AssertRC(rc);
|
---|
433 |
|
---|
434 | PPDMACEPFILEMGR pPrev = pAioMgr->pPrev;
|
---|
435 | PPDMACEPFILEMGR pNext = pAioMgr->pNext;
|
---|
436 |
|
---|
437 | if (pPrev)
|
---|
438 | pPrev->pNext = pNext;
|
---|
439 | else
|
---|
440 | pEpClassFile->pAioMgrHead = pNext;
|
---|
441 |
|
---|
442 | if (pNext)
|
---|
443 | pNext->pPrev = pPrev;
|
---|
444 |
|
---|
445 | pEpClassFile->cAioMgrs--;
|
---|
446 | rc = RTCritSectLeave(&pEpClassFile->CritSect);
|
---|
447 | AssertRC(rc);
|
---|
448 |
|
---|
449 | /* Free the ressources. */
|
---|
450 | RTCritSectDelete(&pAioMgr->CritSectBlockingEvent);
|
---|
451 | RTSemEventDestroy(pAioMgr->EventSem);
|
---|
452 | if (pAioMgr->enmMgrType != PDMACEPFILEMGRTYPE_SIMPLE)
|
---|
453 | pdmacFileAioMgrNormalDestroy(pAioMgr);
|
---|
454 |
|
---|
455 | MMR3HeapFree(pAioMgr);
|
---|
456 | }
|
---|
457 |
|
---|
458 | static int pdmacFileBwMgrInitialize(PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile,
|
---|
459 | PCFGMNODE pCfgNode, PPPDMACFILEBWMGR ppBwMgr)
|
---|
460 | {
|
---|
461 | int rc = VINF_SUCCESS;
|
---|
462 | PPDMACFILEBWMGR pBwMgr = NULL;
|
---|
463 |
|
---|
464 | rc = MMR3HeapAllocZEx(pEpClassFile->Core.pVM, MM_TAG_PDM_ASYNC_COMPLETION,
|
---|
465 | sizeof(PDMACFILEBWMGR),
|
---|
466 | (void **)&pBwMgr);
|
---|
467 | if (RT_SUCCESS(rc))
|
---|
468 | {
|
---|
469 | /* Init I/O flow control. */
|
---|
470 | rc = CFGMR3QueryU32Def(pCfgNode, "VMTransferPerSecMax", &pBwMgr->cbVMTransferPerSecMax, UINT32_MAX);
|
---|
471 | AssertLogRelRCReturn(rc, rc);
|
---|
472 | rc = CFGMR3QueryU32Def(pCfgNode, "VMTransferPerSecStart", &pBwMgr->cbVMTransferPerSecStart, UINT32_MAX /*5 * _1M*/);
|
---|
473 | AssertLogRelRCReturn(rc, rc);
|
---|
474 | rc = CFGMR3QueryU32Def(pCfgNode, "VMTransferPerSecStep", &pBwMgr->cbVMTransferPerSecStep, _1M);
|
---|
475 | AssertLogRelRCReturn(rc, rc);
|
---|
476 |
|
---|
477 | pBwMgr->cbVMTransferAllowed = pBwMgr->cbVMTransferPerSecStart;
|
---|
478 | pBwMgr->tsUpdatedLast = RTTimeSystemNanoTS();
|
---|
479 |
|
---|
480 | if (pBwMgr->cbVMTransferPerSecMax != UINT32_MAX)
|
---|
481 | LogRel(("AIOMgr: I/O bandwidth limited to %u bytes/sec\n", pBwMgr->cbVMTransferPerSecMax));
|
---|
482 | else
|
---|
483 | LogRel(("AIOMgr: I/O bandwidth not limited\n"));
|
---|
484 |
|
---|
485 | *ppBwMgr = pBwMgr;
|
---|
486 | }
|
---|
487 |
|
---|
488 | return rc;
|
---|
489 | }
|
---|
490 |
|
---|
491 | static void pdmacFileBwMgrDestroy(PPDMACFILEBWMGR pBwMgr)
|
---|
492 | {
|
---|
493 | MMR3HeapFree(pBwMgr);
|
---|
494 | }
|
---|
495 |
|
---|
496 | static void pdmacFileBwRef(PPDMACFILEBWMGR pBwMgr)
|
---|
497 | {
|
---|
498 | pBwMgr->cRefs++;
|
---|
499 | }
|
---|
500 |
|
---|
501 | static void pdmacFileBwUnref(PPDMACFILEBWMGR pBwMgr)
|
---|
502 | {
|
---|
503 | Assert(pBwMgr->cRefs > 0);
|
---|
504 | pBwMgr->cRefs--;
|
---|
505 | }
|
---|
506 |
|
---|
507 | bool pdmacFileBwMgrIsTransferAllowed(PPDMACFILEBWMGR pBwMgr, uint32_t cbTransfer)
|
---|
508 | {
|
---|
509 | bool fAllowed = false;
|
---|
510 |
|
---|
511 | LogFlowFunc(("pBwMgr=%p cbTransfer=%u\n", pBwMgr, cbTransfer));
|
---|
512 |
|
---|
513 | uint32_t cbOld = ASMAtomicSubU32(&pBwMgr->cbVMTransferAllowed, cbTransfer);
|
---|
514 | if (RT_LIKELY(cbOld >= cbTransfer))
|
---|
515 | fAllowed = true;
|
---|
516 | else
|
---|
517 | {
|
---|
518 | /* We are out of ressources Check if we can update again. */
|
---|
519 | uint64_t tsNow = RTTimeSystemNanoTS();
|
---|
520 | uint64_t tsUpdatedLast = ASMAtomicUoReadU64(&pBwMgr->tsUpdatedLast);
|
---|
521 |
|
---|
522 | if (tsNow - tsUpdatedLast >= (1000*1000*1000))
|
---|
523 | {
|
---|
524 | if (ASMAtomicCmpXchgU64(&pBwMgr->tsUpdatedLast, tsNow, tsUpdatedLast))
|
---|
525 | {
|
---|
526 | if (pBwMgr->cbVMTransferPerSecStart < pBwMgr->cbVMTransferPerSecMax)
|
---|
527 | {
|
---|
528 | pBwMgr->cbVMTransferPerSecStart = RT_MIN(pBwMgr->cbVMTransferPerSecMax, pBwMgr->cbVMTransferPerSecStart + pBwMgr->cbVMTransferPerSecStep);
|
---|
529 | LogFlow(("AIOMgr: Increasing maximum bandwidth to %u bytes/sec\n", pBwMgr->cbVMTransferPerSecStart));
|
---|
530 | }
|
---|
531 |
|
---|
532 | /* Update */
|
---|
533 | ASMAtomicWriteU32(&pBwMgr->cbVMTransferAllowed, pBwMgr->cbVMTransferPerSecStart - cbTransfer);
|
---|
534 | fAllowed = true;
|
---|
535 | LogFlow(("AIOMgr: Refreshed bandwidth\n"));
|
---|
536 | }
|
---|
537 | }
|
---|
538 | else
|
---|
539 | ASMAtomicAddU32(&pBwMgr->cbVMTransferAllowed, cbTransfer);
|
---|
540 | }
|
---|
541 |
|
---|
542 | LogFlowFunc(("fAllowed=%RTbool\n", fAllowed));
|
---|
543 |
|
---|
544 | return fAllowed;
|
---|
545 | }
|
---|
546 |
|
---|
547 | static int pdmacFileMgrTypeFromName(const char *pszVal, PPDMACEPFILEMGRTYPE penmMgrType)
|
---|
548 | {
|
---|
549 | int rc = VINF_SUCCESS;
|
---|
550 |
|
---|
551 | if (!RTStrCmp(pszVal, "Simple"))
|
---|
552 | *penmMgrType = PDMACEPFILEMGRTYPE_SIMPLE;
|
---|
553 | else if (!RTStrCmp(pszVal, "Async"))
|
---|
554 | *penmMgrType = PDMACEPFILEMGRTYPE_ASYNC;
|
---|
555 | else
|
---|
556 | rc = VERR_CFGM_CONFIG_UNKNOWN_VALUE;
|
---|
557 |
|
---|
558 | return rc;
|
---|
559 | }
|
---|
560 |
|
---|
561 | static const char *pdmacFileMgrTypeToName(PDMACEPFILEMGRTYPE enmMgrType)
|
---|
562 | {
|
---|
563 | if (enmMgrType == PDMACEPFILEMGRTYPE_SIMPLE)
|
---|
564 | return "Simple";
|
---|
565 | if (enmMgrType == PDMACEPFILEMGRTYPE_ASYNC)
|
---|
566 | return "Async";
|
---|
567 |
|
---|
568 | return NULL;
|
---|
569 | }
|
---|
570 |
|
---|
571 | static int pdmacFileBackendTypeFromName(const char *pszVal, PPDMACFILEEPBACKEND penmBackendType)
|
---|
572 | {
|
---|
573 | int rc = VINF_SUCCESS;
|
---|
574 |
|
---|
575 | if (!RTStrCmp(pszVal, "Buffered"))
|
---|
576 | *penmBackendType = PDMACFILEEPBACKEND_BUFFERED;
|
---|
577 | else if (!RTStrCmp(pszVal, "NonBuffered"))
|
---|
578 | *penmBackendType = PDMACFILEEPBACKEND_NON_BUFFERED;
|
---|
579 | else
|
---|
580 | rc = VERR_CFGM_CONFIG_UNKNOWN_VALUE;
|
---|
581 |
|
---|
582 | return rc;
|
---|
583 | }
|
---|
584 |
|
---|
585 | static const char *pdmacFileBackendTypeToName(PDMACFILEEPBACKEND enmBackendType)
|
---|
586 | {
|
---|
587 | if (enmBackendType == PDMACFILEEPBACKEND_BUFFERED)
|
---|
588 | return "Buffered";
|
---|
589 | if (enmBackendType == PDMACFILEEPBACKEND_NON_BUFFERED)
|
---|
590 | return "NonBuffered";
|
---|
591 |
|
---|
592 | return NULL;
|
---|
593 | }
|
---|
594 |
|
---|
595 | static int pdmacFileInitialize(PPDMASYNCCOMPLETIONEPCLASS pClassGlobals, PCFGMNODE pCfgNode)
|
---|
596 | {
|
---|
597 | int rc = VINF_SUCCESS;
|
---|
598 | RTFILEAIOLIMITS AioLimits; /** < Async I/O limitations. */
|
---|
599 |
|
---|
600 | PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pClassGlobals;
|
---|
601 |
|
---|
602 | rc = RTFileAioGetLimits(&AioLimits);
|
---|
603 | #ifdef DEBUG
|
---|
604 | if (RT_SUCCESS(rc) && RTEnvExist("VBOX_ASYNC_IO_FAILBACK"))
|
---|
605 | rc = VERR_ENV_VAR_NOT_FOUND;
|
---|
606 | #endif
|
---|
607 | if (RT_FAILURE(rc))
|
---|
608 | {
|
---|
609 | LogRel(("AIO: Async I/O manager not supported (rc=%Rrc). Falling back to simple manager\n",
|
---|
610 | rc));
|
---|
611 | pEpClassFile->enmMgrTypeOverride = PDMACEPFILEMGRTYPE_SIMPLE;
|
---|
612 | pEpClassFile->enmEpBackendDefault = PDMACFILEEPBACKEND_BUFFERED;
|
---|
613 | }
|
---|
614 | else
|
---|
615 | {
|
---|
616 | pEpClassFile->uBitmaskAlignment = AioLimits.cbBufferAlignment ? ~((RTR3UINTPTR)AioLimits.cbBufferAlignment - 1) : RTR3UINTPTR_MAX;
|
---|
617 | pEpClassFile->cReqsOutstandingMax = AioLimits.cReqsOutstandingMax;
|
---|
618 |
|
---|
619 | if (pCfgNode)
|
---|
620 | {
|
---|
621 | /* Query the default manager type */
|
---|
622 | char *pszVal = NULL;
|
---|
623 | rc = CFGMR3QueryStringAllocDef(pCfgNode, "IoMgr", &pszVal, "Async");
|
---|
624 | AssertLogRelRCReturn(rc, rc);
|
---|
625 |
|
---|
626 | rc = pdmacFileMgrTypeFromName(pszVal, &pEpClassFile->enmMgrTypeOverride);
|
---|
627 | MMR3HeapFree(pszVal);
|
---|
628 | if (RT_FAILURE(rc))
|
---|
629 | return rc;
|
---|
630 |
|
---|
631 | LogRel(("AIOMgr: Default manager type is \"%s\"\n", pdmacFileMgrTypeToName(pEpClassFile->enmMgrTypeOverride)));
|
---|
632 |
|
---|
633 | /* Query default backend type */
|
---|
634 | rc = CFGMR3QueryStringAllocDef(pCfgNode, "FileBackend", &pszVal, "NonBuffered");
|
---|
635 | AssertLogRelRCReturn(rc, rc);
|
---|
636 |
|
---|
637 | rc = pdmacFileBackendTypeFromName(pszVal, &pEpClassFile->enmEpBackendDefault);
|
---|
638 | MMR3HeapFree(pszVal);
|
---|
639 | if (RT_FAILURE(rc))
|
---|
640 | return rc;
|
---|
641 |
|
---|
642 | LogRel(("AIOMgr: Default file backend is \"%s\"\n", pdmacFileBackendTypeToName(pEpClassFile->enmEpBackendDefault)));
|
---|
643 |
|
---|
644 | #ifdef RT_OS_LINUX
|
---|
645 | if ( pEpClassFile->enmMgrTypeOverride == PDMACEPFILEMGRTYPE_ASYNC
|
---|
646 | && pEpClassFile->enmEpBackendDefault == PDMACFILEEPBACKEND_BUFFERED)
|
---|
647 | {
|
---|
648 | LogRel(("AIOMgr: Linux does not support buffered async I/O, changing to non buffered\n"));
|
---|
649 | pEpClassFile->enmEpBackendDefault = PDMACFILEEPBACKEND_NON_BUFFERED;
|
---|
650 | }
|
---|
651 | #endif
|
---|
652 | }
|
---|
653 | else
|
---|
654 | {
|
---|
655 | /* No configuration supplied, set defaults */
|
---|
656 | pEpClassFile->enmEpBackendDefault = PDMACFILEEPBACKEND_NON_BUFFERED;
|
---|
657 | }
|
---|
658 | }
|
---|
659 |
|
---|
660 | /* Init critical section. */
|
---|
661 | rc = RTCritSectInit(&pEpClassFile->CritSect);
|
---|
662 | if (RT_SUCCESS(rc))
|
---|
663 | {
|
---|
664 | /* Check if the cache was disabled by the user. */
|
---|
665 | rc = CFGMR3QueryBoolDef(pCfgNode, "CacheEnabled", &pEpClassFile->fCacheEnabled, true);
|
---|
666 | AssertLogRelRCReturn(rc, rc);
|
---|
667 |
|
---|
668 | if (pEpClassFile->fCacheEnabled)
|
---|
669 | {
|
---|
670 | /* Init cache structure */
|
---|
671 | rc = pdmacFileCacheInit(pEpClassFile, pCfgNode);
|
---|
672 | if (RT_FAILURE(rc))
|
---|
673 | {
|
---|
674 | pEpClassFile->fCacheEnabled = false;
|
---|
675 | LogRel(("AIOMgr: Failed to initialise the cache (rc=%Rrc), disabled caching\n"));
|
---|
676 | }
|
---|
677 | }
|
---|
678 | else
|
---|
679 | LogRel(("AIOMgr: Cache was globally disabled\n"));
|
---|
680 |
|
---|
681 | rc = pdmacFileBwMgrInitialize(pEpClassFile, pCfgNode, &pEpClassFile->pBwMgr);
|
---|
682 | if (RT_FAILURE(rc))
|
---|
683 | RTCritSectDelete(&pEpClassFile->CritSect);
|
---|
684 | }
|
---|
685 |
|
---|
686 | return rc;
|
---|
687 | }
|
---|
688 |
|
---|
689 | static void pdmacFileTerminate(PPDMASYNCCOMPLETIONEPCLASS pClassGlobals)
|
---|
690 | {
|
---|
691 | PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pClassGlobals;
|
---|
692 |
|
---|
693 | /* All endpoints should be closed at this point. */
|
---|
694 | AssertMsg(!pEpClassFile->Core.pEndpointsHead, ("There are still endpoints left\n"));
|
---|
695 |
|
---|
696 | /* Destroy all left async I/O managers. */
|
---|
697 | while (pEpClassFile->pAioMgrHead)
|
---|
698 | pdmacFileAioMgrDestroy(pEpClassFile, pEpClassFile->pAioMgrHead);
|
---|
699 |
|
---|
700 | /* Destroy the cache. */
|
---|
701 | if (pEpClassFile->fCacheEnabled)
|
---|
702 | pdmacFileCacheDestroy(pEpClassFile);
|
---|
703 |
|
---|
704 | RTCritSectDelete(&pEpClassFile->CritSect);
|
---|
705 | pdmacFileBwMgrDestroy(pEpClassFile->pBwMgr);
|
---|
706 | }
|
---|
707 |
|
---|
708 | static int pdmacFileEpInitialize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint,
|
---|
709 | const char *pszUri, uint32_t fFlags)
|
---|
710 | {
|
---|
711 | int rc = VINF_SUCCESS;
|
---|
712 | PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
|
---|
713 | PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->pEpClass;
|
---|
714 | PDMACEPFILEMGRTYPE enmMgrType = pEpClassFile->enmMgrTypeOverride;
|
---|
715 | PDMACFILEEPBACKEND enmEpBackend = pEpClassFile->enmEpBackendDefault;
|
---|
716 |
|
---|
717 | AssertMsgReturn((fFlags & ~(PDMACEP_FILE_FLAGS_READ_ONLY | PDMACEP_FILE_FLAGS_CACHING)) == 0,
|
---|
718 | ("PDMAsyncCompletion: Invalid flag specified\n"), VERR_INVALID_PARAMETER);
|
---|
719 |
|
---|
720 | unsigned fFileFlags = fFlags & PDMACEP_FILE_FLAGS_READ_ONLY
|
---|
721 | ? RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE
|
---|
722 | : RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE;
|
---|
723 |
|
---|
724 | if (enmMgrType == PDMACEPFILEMGRTYPE_ASYNC)
|
---|
725 | fFileFlags |= RTFILE_O_ASYNC_IO;
|
---|
726 |
|
---|
727 | if (enmEpBackend == PDMACFILEEPBACKEND_NON_BUFFERED)
|
---|
728 | {
|
---|
729 | /*
|
---|
730 | * We only disable the cache if the size of the file is a multiple of 512.
|
---|
731 | * Certain hosts like Windows, Linux and Solaris require that transfer sizes
|
---|
732 | * are aligned to the volume sector size.
|
---|
733 | * If not we just make sure that the data is written to disk with RTFILE_O_WRITE_THROUGH
|
---|
734 | * which will trash the host cache but ensures that the host cache will not
|
---|
735 | * contain dirty buffers.
|
---|
736 | */
|
---|
737 | RTFILE File = NIL_RTFILE;
|
---|
738 |
|
---|
739 | rc = RTFileOpen(&File, pszUri, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
740 | if (RT_SUCCESS(rc))
|
---|
741 | {
|
---|
742 | uint64_t cbSize;
|
---|
743 |
|
---|
744 | rc = RTFileGetSize(File, &cbSize);
|
---|
745 | if (RT_SUCCESS(rc) && ((cbSize % 512) == 0))
|
---|
746 | fFileFlags |= RTFILE_O_NO_CACHE;
|
---|
747 | else
|
---|
748 | {
|
---|
749 | /* Downgrade to the buffered backend */
|
---|
750 | enmEpBackend = PDMACFILEEPBACKEND_BUFFERED;
|
---|
751 |
|
---|
752 | #ifdef RT_OS_LINUX
|
---|
753 | fFileFlags &= ~RTFILE_O_ASYNC_IO;
|
---|
754 | enmMgrType = PDMACEPFILEMGRTYPE_SIMPLE;
|
---|
755 | #endif
|
---|
756 | }
|
---|
757 | RTFileClose(File);
|
---|
758 | }
|
---|
759 | }
|
---|
760 |
|
---|
761 | /* Open with final flags. */
|
---|
762 | rc = RTFileOpen(&pEpFile->File, pszUri, fFileFlags);
|
---|
763 | if ((rc == VERR_INVALID_FUNCTION) || (rc == VERR_INVALID_PARAMETER))
|
---|
764 | {
|
---|
765 | LogRel(("pdmacFileEpInitialize: RTFileOpen %s / %08x failed with %Rrc\n",
|
---|
766 | pszUri, fFileFlags, rc));
|
---|
767 | /*
|
---|
768 | * Solaris doesn't support directio on ZFS so far. :-\
|
---|
769 | * Trying to enable it returns VERR_INVALID_FUNCTION
|
---|
770 | * (ENOTTY). Remove it and hope for the best.
|
---|
771 | * ZFS supports write throttling in case applications
|
---|
772 | * write more data than can be synced to the disk
|
---|
773 | * without blocking the whole application.
|
---|
774 | *
|
---|
775 | * On Linux we have the same problem with cifs.
|
---|
776 | * Have to disable async I/O here too because it requires O_DIRECT.
|
---|
777 | */
|
---|
778 | fFileFlags &= ~RTFILE_O_NO_CACHE;
|
---|
779 | enmEpBackend = PDMACFILEEPBACKEND_BUFFERED;
|
---|
780 |
|
---|
781 | #ifdef RT_OS_LINUX
|
---|
782 | fFileFlags &= ~RTFILE_O_ASYNC_IO;
|
---|
783 | enmMgrType = PDMACEPFILEMGRTYPE_SIMPLE;
|
---|
784 | #endif
|
---|
785 |
|
---|
786 | /* Open again. */
|
---|
787 | rc = RTFileOpen(&pEpFile->File, pszUri, fFileFlags);
|
---|
788 |
|
---|
789 | if (RT_FAILURE(rc))
|
---|
790 | {
|
---|
791 | LogRel(("pdmacFileEpInitialize: RTFileOpen %s / %08x failed AGAIN(!) with %Rrc\n",
|
---|
792 | pszUri, fFileFlags, rc));
|
---|
793 | }
|
---|
794 | }
|
---|
795 |
|
---|
796 | if (RT_SUCCESS(rc))
|
---|
797 | {
|
---|
798 | pEpFile->fFlags = fFileFlags;
|
---|
799 |
|
---|
800 | rc = RTFileGetSize(pEpFile->File, (uint64_t *)&pEpFile->cbFile);
|
---|
801 | if (RT_SUCCESS(rc) && (pEpFile->cbFile == 0))
|
---|
802 | {
|
---|
803 | /* Could be a block device */
|
---|
804 | rc = RTFileSeek(pEpFile->File, 0, RTFILE_SEEK_END, (uint64_t *)&pEpFile->cbFile);
|
---|
805 | }
|
---|
806 |
|
---|
807 | if (RT_SUCCESS(rc))
|
---|
808 | {
|
---|
809 | /* Initialize the segment cache */
|
---|
810 | rc = MMR3HeapAllocZEx(pEpClassFile->Core.pVM, MM_TAG_PDM_ASYNC_COMPLETION,
|
---|
811 | sizeof(PDMACTASKFILE),
|
---|
812 | (void **)&pEpFile->pTasksFreeHead);
|
---|
813 | if (RT_SUCCESS(rc))
|
---|
814 | {
|
---|
815 | PPDMACEPFILEMGR pAioMgr = NULL;
|
---|
816 |
|
---|
817 | pEpFile->cbEndpoint = pEpFile->cbFile;
|
---|
818 | pEpFile->pTasksFreeTail = pEpFile->pTasksFreeHead;
|
---|
819 | pEpFile->cTasksCached = 0;
|
---|
820 | pEpFile->pBwMgr = pEpClassFile->pBwMgr;
|
---|
821 | pEpFile->enmBackendType = enmEpBackend;
|
---|
822 | pEpFile->fAsyncFlushSupported = true;
|
---|
823 | pdmacFileBwRef(pEpFile->pBwMgr);
|
---|
824 |
|
---|
825 | if (enmMgrType == PDMACEPFILEMGRTYPE_SIMPLE)
|
---|
826 | {
|
---|
827 | /* Simple mode. Every file has its own async I/O manager. */
|
---|
828 | rc = pdmacFileAioMgrCreate(pEpClassFile, &pAioMgr, PDMACEPFILEMGRTYPE_SIMPLE);
|
---|
829 | AssertRC(rc);
|
---|
830 | }
|
---|
831 | else
|
---|
832 | {
|
---|
833 | if ( (fFlags & PDMACEP_FILE_FLAGS_CACHING)
|
---|
834 | && (pEpClassFile->fCacheEnabled))
|
---|
835 | {
|
---|
836 | pEpFile->fCaching = true;
|
---|
837 | rc = pdmacFileEpCacheInit(pEpFile, pEpClassFile);
|
---|
838 | if (RT_FAILURE(rc))
|
---|
839 | {
|
---|
840 | LogRel(("AIOMgr: Endpoint for \"%s\" was opened with caching but initializing cache failed. Disabled caching\n", pszUri));
|
---|
841 | pEpFile->fCaching = false;
|
---|
842 | }
|
---|
843 | }
|
---|
844 |
|
---|
845 | pAioMgr = pEpClassFile->pAioMgrHead;
|
---|
846 |
|
---|
847 | /* Check for an idling manager of the same type */
|
---|
848 | while (pAioMgr)
|
---|
849 | {
|
---|
850 | if (pAioMgr->enmMgrType == enmMgrType)
|
---|
851 | break;
|
---|
852 | pAioMgr = pAioMgr->pNext;
|
---|
853 | }
|
---|
854 |
|
---|
855 | if (!pAioMgr)
|
---|
856 | {
|
---|
857 | rc = pdmacFileAioMgrCreate(pEpClassFile, &pAioMgr, enmMgrType);
|
---|
858 | AssertRC(rc);
|
---|
859 | }
|
---|
860 | }
|
---|
861 |
|
---|
862 | pEpFile->AioMgr.pTreeRangesLocked = (PAVLRFOFFTREE)RTMemAllocZ(sizeof(AVLRFOFFTREE));
|
---|
863 | if (!pEpFile->AioMgr.pTreeRangesLocked)
|
---|
864 | rc = VERR_NO_MEMORY;
|
---|
865 | else
|
---|
866 | {
|
---|
867 | pEpFile->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE;
|
---|
868 |
|
---|
869 | /* Assign the endpoint to the thread. */
|
---|
870 | rc = pdmacFileAioMgrAddEndpoint(pAioMgr, pEpFile);
|
---|
871 | if (RT_FAILURE(rc))
|
---|
872 | {
|
---|
873 | RTMemFree(pEpFile->AioMgr.pTreeRangesLocked);
|
---|
874 | MMR3HeapFree(pEpFile->pTasksFreeHead);
|
---|
875 | pdmacFileBwUnref(pEpFile->pBwMgr);
|
---|
876 | }
|
---|
877 | }
|
---|
878 | }
|
---|
879 | }
|
---|
880 |
|
---|
881 | if (RT_FAILURE(rc))
|
---|
882 | RTFileClose(pEpFile->File);
|
---|
883 | }
|
---|
884 |
|
---|
885 | #ifdef VBOX_WITH_STATISTICS
|
---|
886 | if (RT_SUCCESS(rc))
|
---|
887 | {
|
---|
888 | STAMR3RegisterF(pEpClassFile->Core.pVM, &pEpFile->StatRead,
|
---|
889 | STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS,
|
---|
890 | STAMUNIT_TICKS_PER_CALL, "Time taken to read from the endpoint",
|
---|
891 | "/PDM/AsyncCompletion/File/%s/Read", RTPathFilename(pEpFile->Core.pszUri));
|
---|
892 |
|
---|
893 | STAMR3RegisterF(pEpClassFile->Core.pVM, &pEpFile->StatWrite,
|
---|
894 | STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS,
|
---|
895 | STAMUNIT_TICKS_PER_CALL, "Time taken to write to the endpoint",
|
---|
896 | "/PDM/AsyncCompletion/File/%s/Write", RTPathFilename(pEpFile->Core.pszUri));
|
---|
897 | }
|
---|
898 | #endif
|
---|
899 |
|
---|
900 | if (RT_SUCCESS(rc))
|
---|
901 | LogRel(("AIOMgr: Endpoint for file '%s' (flags %08x) created successfully\n", pszUri, pEpFile->fFlags));
|
---|
902 |
|
---|
903 | return rc;
|
---|
904 | }
|
---|
905 |
|
---|
906 | static int pdmacFileEpRangesLockedDestroy(PAVLRFOFFNODECORE pNode, void *pvUser)
|
---|
907 | {
|
---|
908 | AssertMsgFailed(("The locked ranges tree should be empty at that point\n"));
|
---|
909 | return VINF_SUCCESS;
|
---|
910 | }
|
---|
911 |
|
---|
912 | static int pdmacFileEpClose(PPDMASYNCCOMPLETIONENDPOINT pEndpoint)
|
---|
913 | {
|
---|
914 | PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
|
---|
915 | PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->pEpClass;
|
---|
916 |
|
---|
917 | /* Make sure that all tasks finished for this endpoint. */
|
---|
918 | int rc = pdmacFileAioMgrCloseEndpoint(pEpFile->pAioMgr, pEpFile);
|
---|
919 | AssertRC(rc);
|
---|
920 |
|
---|
921 | /* endpoint and real file size should better be equal now. */
|
---|
922 | AssertMsg(pEpFile->cbFile == pEpFile->cbEndpoint,
|
---|
923 | ("Endpoint and real file size should match now!\n"));
|
---|
924 |
|
---|
925 | /*
|
---|
926 | * If the async I/O manager is in failsafe mode this is the only endpoint
|
---|
927 | * he processes and thus can be destroyed now.
|
---|
928 | */
|
---|
929 | if (pEpFile->pAioMgr->enmMgrType == PDMACEPFILEMGRTYPE_SIMPLE)
|
---|
930 | pdmacFileAioMgrDestroy(pEpClassFile, pEpFile->pAioMgr);
|
---|
931 |
|
---|
932 | /* Free cached tasks. */
|
---|
933 | PPDMACTASKFILE pTask = pEpFile->pTasksFreeHead;
|
---|
934 |
|
---|
935 | while (pTask)
|
---|
936 | {
|
---|
937 | PPDMACTASKFILE pTaskFree = pTask;
|
---|
938 | pTask = pTask->pNext;
|
---|
939 | MMR3HeapFree(pTaskFree);
|
---|
940 | }
|
---|
941 |
|
---|
942 | /* Free the cached data. */
|
---|
943 | if (pEpFile->fCaching)
|
---|
944 | pdmacFileEpCacheDestroy(pEpFile);
|
---|
945 |
|
---|
946 | /* Remove from the bandwidth manager */
|
---|
947 | pdmacFileBwUnref(pEpFile->pBwMgr);
|
---|
948 |
|
---|
949 | /* Destroy the locked ranges tree now. */
|
---|
950 | RTAvlrFileOffsetDestroy(pEpFile->AioMgr.pTreeRangesLocked, pdmacFileEpRangesLockedDestroy, NULL);
|
---|
951 |
|
---|
952 | RTFileClose(pEpFile->File);
|
---|
953 |
|
---|
954 | #ifdef VBOX_WITH_STATISTICS
|
---|
955 | STAMR3Deregister(pEpClassFile->Core.pVM, &pEpFile->StatRead);
|
---|
956 | STAMR3Deregister(pEpClassFile->Core.pVM, &pEpFile->StatWrite);
|
---|
957 | #endif
|
---|
958 |
|
---|
959 | return VINF_SUCCESS;
|
---|
960 | }
|
---|
961 |
|
---|
962 | static int pdmacFileEpRead(PPDMASYNCCOMPLETIONTASK pTask,
|
---|
963 | PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
|
---|
964 | PCRTSGSEG paSegments, size_t cSegments,
|
---|
965 | size_t cbRead)
|
---|
966 | {
|
---|
967 | int rc = VINF_SUCCESS;
|
---|
968 | PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
|
---|
969 |
|
---|
970 | LogFlowFunc(("pTask=%#p pEndpoint=%#p off=%RTfoff paSegments=%#p cSegments=%zu cbRead=%zu\n",
|
---|
971 | pTask, pEndpoint, off, paSegments, cSegments, cbRead));
|
---|
972 |
|
---|
973 | STAM_PROFILE_ADV_START(&pEpFile->StatRead, Read);
|
---|
974 |
|
---|
975 | pdmacFileEpTaskInit(pTask, cbRead);
|
---|
976 |
|
---|
977 | if (pEpFile->fCaching)
|
---|
978 | rc = pdmacFileEpCacheRead(pEpFile, (PPDMASYNCCOMPLETIONTASKFILE)pTask,
|
---|
979 | off, paSegments, cSegments, cbRead);
|
---|
980 | else
|
---|
981 | rc = pdmacFileEpTaskInitiate(pTask, pEndpoint, off, paSegments, cSegments, cbRead,
|
---|
982 | PDMACTASKFILETRANSFER_READ);
|
---|
983 |
|
---|
984 | STAM_PROFILE_ADV_STOP(&pEpFile->StatRead, Read);
|
---|
985 |
|
---|
986 | return rc;
|
---|
987 | }
|
---|
988 |
|
---|
989 | static int pdmacFileEpWrite(PPDMASYNCCOMPLETIONTASK pTask,
|
---|
990 | PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
|
---|
991 | PCRTSGSEG paSegments, size_t cSegments,
|
---|
992 | size_t cbWrite)
|
---|
993 | {
|
---|
994 | int rc = VINF_SUCCESS;
|
---|
995 | PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
|
---|
996 |
|
---|
997 | if (RT_UNLIKELY(pEpFile->fReadonly))
|
---|
998 | return VERR_NOT_SUPPORTED;
|
---|
999 |
|
---|
1000 | STAM_PROFILE_ADV_START(&pEpFile->StatWrite, Write);
|
---|
1001 |
|
---|
1002 | pdmacFileEpTaskInit(pTask, cbWrite);
|
---|
1003 |
|
---|
1004 | if (pEpFile->fCaching)
|
---|
1005 | rc = pdmacFileEpCacheWrite(pEpFile, (PPDMASYNCCOMPLETIONTASKFILE)pTask,
|
---|
1006 | off, paSegments, cSegments, cbWrite);
|
---|
1007 | else
|
---|
1008 | rc = pdmacFileEpTaskInitiate(pTask, pEndpoint, off, paSegments, cSegments, cbWrite,
|
---|
1009 | PDMACTASKFILETRANSFER_WRITE);
|
---|
1010 |
|
---|
1011 | STAM_PROFILE_ADV_STOP(&pEpFile->StatWrite, Write);
|
---|
1012 |
|
---|
1013 | /* Increase endpoint size. */
|
---|
1014 | if ( RT_SUCCESS(rc)
|
---|
1015 | && ((uint64_t)off + cbWrite) > pEpFile->cbEndpoint)
|
---|
1016 | ASMAtomicWriteU64(&pEpFile->cbEndpoint, (uint64_t)off + cbWrite);
|
---|
1017 |
|
---|
1018 | return rc;
|
---|
1019 | }
|
---|
1020 |
|
---|
1021 | static int pdmacFileEpFlush(PPDMASYNCCOMPLETIONTASK pTask,
|
---|
1022 | PPDMASYNCCOMPLETIONENDPOINT pEndpoint)
|
---|
1023 | {
|
---|
1024 | PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
|
---|
1025 | PPDMASYNCCOMPLETIONTASKFILE pTaskFile = (PPDMASYNCCOMPLETIONTASKFILE)pTask;
|
---|
1026 |
|
---|
1027 | if (RT_UNLIKELY(pEpFile->fReadonly))
|
---|
1028 | return VERR_NOT_SUPPORTED;
|
---|
1029 |
|
---|
1030 | pdmacFileEpTaskInit(pTask, 0);
|
---|
1031 |
|
---|
1032 | if (pEpFile->fCaching)
|
---|
1033 | {
|
---|
1034 | int rc = pdmacFileEpCacheFlush(pEpFile, pTaskFile);
|
---|
1035 | AssertRC(rc);
|
---|
1036 | }
|
---|
1037 |
|
---|
1038 | PPDMACTASKFILE pIoTask = pdmacFileTaskAlloc(pEpFile);
|
---|
1039 | if (RT_UNLIKELY(!pIoTask))
|
---|
1040 | return VERR_NO_MEMORY;
|
---|
1041 |
|
---|
1042 | pIoTask->pEndpoint = pEpFile;
|
---|
1043 | pIoTask->enmTransferType = PDMACTASKFILETRANSFER_FLUSH;
|
---|
1044 | pIoTask->pvUser = pTaskFile;
|
---|
1045 | pIoTask->pfnCompleted = pdmacFileEpTaskCompleted;
|
---|
1046 | pdmacFileEpAddTask(pEpFile, pIoTask);
|
---|
1047 |
|
---|
1048 | return VINF_AIO_TASK_PENDING;
|
---|
1049 | }
|
---|
1050 |
|
---|
1051 | static int pdmacFileEpGetSize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, uint64_t *pcbSize)
|
---|
1052 | {
|
---|
1053 | PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
|
---|
1054 |
|
---|
1055 | *pcbSize = ASMAtomicReadU64(&pEpFile->cbEndpoint);
|
---|
1056 |
|
---|
1057 | return VINF_SUCCESS;
|
---|
1058 | }
|
---|
1059 |
|
---|
1060 | static int pdmacFileEpSetSize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, uint64_t cbSize)
|
---|
1061 | {
|
---|
1062 | PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
|
---|
1063 |
|
---|
1064 | ASMAtomicWriteU64(&pEpFile->cbEndpoint, cbSize);
|
---|
1065 | return RTFileSetSize(pEpFile->File, cbSize);
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 | const PDMASYNCCOMPLETIONEPCLASSOPS g_PDMAsyncCompletionEndpointClassFile =
|
---|
1069 | {
|
---|
1070 | /* u32Version */
|
---|
1071 | PDMAC_EPCLASS_OPS_VERSION,
|
---|
1072 | /* pcszName */
|
---|
1073 | "File",
|
---|
1074 | /* enmClassType */
|
---|
1075 | PDMASYNCCOMPLETIONEPCLASSTYPE_FILE,
|
---|
1076 | /* cbEndpointClassGlobal */
|
---|
1077 | sizeof(PDMASYNCCOMPLETIONEPCLASSFILE),
|
---|
1078 | /* cbEndpoint */
|
---|
1079 | sizeof(PDMASYNCCOMPLETIONENDPOINTFILE),
|
---|
1080 | /* cbTask */
|
---|
1081 | sizeof(PDMASYNCCOMPLETIONTASKFILE),
|
---|
1082 | /* pfnInitialize */
|
---|
1083 | pdmacFileInitialize,
|
---|
1084 | /* pfnTerminate */
|
---|
1085 | pdmacFileTerminate,
|
---|
1086 | /* pfnEpInitialize. */
|
---|
1087 | pdmacFileEpInitialize,
|
---|
1088 | /* pfnEpClose */
|
---|
1089 | pdmacFileEpClose,
|
---|
1090 | /* pfnEpRead */
|
---|
1091 | pdmacFileEpRead,
|
---|
1092 | /* pfnEpWrite */
|
---|
1093 | pdmacFileEpWrite,
|
---|
1094 | /* pfnEpFlush */
|
---|
1095 | pdmacFileEpFlush,
|
---|
1096 | /* pfnEpGetSize */
|
---|
1097 | pdmacFileEpGetSize,
|
---|
1098 | /* pfnEpSetSize */
|
---|
1099 | pdmacFileEpSetSize,
|
---|
1100 | /* u32VersionEnd */
|
---|
1101 | PDMAC_EPCLASS_OPS_VERSION
|
---|
1102 | };
|
---|
1103 |
|
---|