VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/PDMAsyncCompletionFile.cpp@ 74795

Last change on this file since 74795 was 69111, checked in by vboxsync, 7 years ago

(C) year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 47.4 KB
Line 
1/* $Id: PDMAsyncCompletionFile.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
2/** @file
3 * PDM Async I/O - Transport data asynchronous in R3 using EMT.
4 */
5
6/*
7 * Copyright (C) 2006-2017 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/vmm/pdm.h>
25#include <VBox/vmm/mm.h>
26#include <VBox/vmm/vm.h>
27#include <VBox/err.h>
28#include <VBox/log.h>
29#include <VBox/dbg.h>
30#include <VBox/vmm/uvm.h>
31#include <VBox/vmm/tm.h>
32
33#include <iprt/asm.h>
34#include <iprt/assert.h>
35#include <iprt/critsect.h>
36#include <iprt/env.h>
37#include <iprt/file.h>
38#include <iprt/mem.h>
39#include <iprt/semaphore.h>
40#include <iprt/string.h>
41#include <iprt/thread.h>
42#include <iprt/path.h>
43#include <iprt/rand.h>
44
45#include "PDMAsyncCompletionFileInternal.h"
46
47
48/*********************************************************************************************************************************
49* Internal Functions *
50*********************************************************************************************************************************/
51#ifdef VBOX_WITH_DEBUGGER
52static FNDBGCCMD pdmacEpFileErrorInject;
53# ifdef PDM_ASYNC_COMPLETION_FILE_WITH_DELAY
54static FNDBGCCMD pdmacEpFileDelayInject;
55# endif
56#endif
57
58
59/*********************************************************************************************************************************
60* Global Variables *
61*********************************************************************************************************************************/
62#ifdef VBOX_WITH_DEBUGGER
63static const DBGCVARDESC g_aInjectErrorArgs[] =
64{
65 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
66 { 1, 1, DBGCVAR_CAT_STRING, 0, "direction", "write/read." },
67 { 1, 1, DBGCVAR_CAT_STRING, 0, "filename", "Filename." },
68 { 1, 1, DBGCVAR_CAT_NUMBER, 0, "errcode", "VBox status code." },
69};
70
71# ifdef PDM_ASYNC_COMPLETION_FILE_WITH_DELAY
72static const DBGCVARDESC g_aInjectDelayArgs[] =
73{
74 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
75 { 1, 1, DBGCVAR_CAT_STRING, 0, "direction", "write|read|flush|any." },
76 { 1, 1, DBGCVAR_CAT_STRING, 0, "filename", "Filename." },
77 { 1, 1, DBGCVAR_CAT_NUMBER, 0, "delay", "Delay in milliseconds." },
78 { 1, 1, DBGCVAR_CAT_NUMBER, 0, "jitter", "Jitter of the delay." },
79 { 1, 1, DBGCVAR_CAT_NUMBER, 0, "reqs", "Number of requests to delay." }
80
81};
82# endif
83
84/** Command descriptors. */
85static const DBGCCMD g_aCmds[] =
86{
87 /* pszCmd, cArgsMin, cArgsMax, paArgDesc, cArgDescs, fFlags, pfnHandler pszSyntax,.pszDescription */
88 { "injecterror", 3, 3, &g_aInjectErrorArgs[0], 3, 0, pdmacEpFileErrorInject, "", "Inject error into I/O subsystem." }
89# ifdef PDM_ASYNC_COMPLETION_FILE_WITH_DELAY
90 ,{ "injectdelay", 3, 5, &g_aInjectDelayArgs[0], RT_ELEMENTS(g_aInjectDelayArgs), 0, pdmacEpFileDelayInject, "", "Inject a delay of a request." }
91# endif
92};
93#endif
94
95
96/**
97 * Frees a task.
98 *
99 * @returns nothing.
100 * @param pEndpoint Pointer to the endpoint the segment was for.
101 * @param pTask The task to free.
102 */
103void pdmacFileTaskFree(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint, PPDMACTASKFILE pTask)
104{
105 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClass = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->Core.pEpClass;
106
107 LogFlowFunc((": pEndpoint=%p pTask=%p\n", pEndpoint, pTask));
108
109 /* Try the per endpoint cache first. */
110 if (pEndpoint->cTasksCached < pEpClass->cTasksCacheMax)
111 {
112 /* Add it to the list. */
113 pEndpoint->pTasksFreeTail->pNext = pTask;
114 pEndpoint->pTasksFreeTail = pTask;
115 ASMAtomicIncU32(&pEndpoint->cTasksCached);
116 }
117 else
118 {
119 Log(("Freeing task %p because all caches are full\n", pTask));
120 MMR3HeapFree(pTask);
121 }
122}
123
124/**
125 * Allocates a task segment
126 *
127 * @returns Pointer to the new task segment or NULL
128 * @param pEndpoint Pointer to the endpoint
129 */
130PPDMACTASKFILE pdmacFileTaskAlloc(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
131{
132 PPDMACTASKFILE pTask = NULL;
133
134 /* Try the small per endpoint cache first. */
135 if (pEndpoint->pTasksFreeHead == pEndpoint->pTasksFreeTail)
136 {
137 /* Try the bigger endpoint class cache. */
138 PPDMASYNCCOMPLETIONEPCLASSFILE pEndpointClass = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->Core.pEpClass;
139
140 /*
141 * Allocate completely new.
142 * If this fails we return NULL.
143 */
144 int rc = MMR3HeapAllocZEx(pEndpointClass->Core.pVM, MM_TAG_PDM_ASYNC_COMPLETION,
145 sizeof(PDMACTASKFILE),
146 (void **)&pTask);
147 if (RT_FAILURE(rc))
148 pTask = NULL;
149
150 LogFlow(("Allocated task %p\n", pTask));
151 }
152 else
153 {
154 /* Grab a free task from the head. */
155 AssertMsg(pEndpoint->cTasksCached > 0, ("No tasks cached but list contains more than one element\n"));
156
157 pTask = pEndpoint->pTasksFreeHead;
158 pEndpoint->pTasksFreeHead = pTask->pNext;
159 ASMAtomicDecU32(&pEndpoint->cTasksCached);
160 }
161
162 pTask->pNext = NULL;
163
164 return pTask;
165}
166
167PPDMACTASKFILE pdmacFileEpGetNewTasks(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
168{
169 /*
170 * Get pending tasks.
171 */
172 PPDMACTASKFILE pTasks = ASMAtomicXchgPtrT(&pEndpoint->pTasksNewHead, NULL, PPDMACTASKFILE);
173
174 /* Reverse the list to process in FIFO order. */
175 if (pTasks)
176 {
177 PPDMACTASKFILE pTask = pTasks;
178
179 pTasks = NULL;
180
181 while (pTask)
182 {
183 PPDMACTASKFILE pCur = pTask;
184 pTask = pTask->pNext;
185 pCur->pNext = pTasks;
186 pTasks = pCur;
187 }
188 }
189
190 return pTasks;
191}
192
193static void pdmacFileAioMgrWakeup(PPDMACEPFILEMGR pAioMgr)
194{
195 bool fWokenUp = ASMAtomicXchgBool(&pAioMgr->fWokenUp, true);
196 if (!fWokenUp)
197 {
198 bool fWaitingEventSem = ASMAtomicReadBool(&pAioMgr->fWaitingEventSem);
199 if (fWaitingEventSem)
200 {
201 int rc = RTSemEventSignal(pAioMgr->EventSem);
202 AssertRC(rc);
203 }
204 }
205}
206
207static int pdmacFileAioMgrWaitForBlockingEvent(PPDMACEPFILEMGR pAioMgr, PDMACEPFILEAIOMGRBLOCKINGEVENT enmEvent)
208{
209 ASMAtomicWriteU32((volatile uint32_t *)&pAioMgr->enmBlockingEvent, enmEvent);
210 Assert(!pAioMgr->fBlockingEventPending);
211 ASMAtomicXchgBool(&pAioMgr->fBlockingEventPending, true);
212
213 /* Wakeup the async I/O manager */
214 pdmacFileAioMgrWakeup(pAioMgr);
215
216 /* Wait for completion. */
217 int rc = RTSemEventWait(pAioMgr->EventSemBlock, RT_INDEFINITE_WAIT);
218 AssertRC(rc);
219
220 ASMAtomicXchgBool(&pAioMgr->fBlockingEventPending, false);
221 ASMAtomicWriteU32((volatile uint32_t *)&pAioMgr->enmBlockingEvent, PDMACEPFILEAIOMGRBLOCKINGEVENT_INVALID);
222
223 return rc;
224}
225
226int pdmacFileAioMgrAddEndpoint(PPDMACEPFILEMGR pAioMgr, PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
227{
228 LogFlowFunc(("pAioMgr=%#p pEndpoint=%#p{%s}\n", pAioMgr, pEndpoint, pEndpoint->Core.pszUri));
229
230 /* Update the assigned I/O manager. */
231 ASMAtomicWritePtr(&pEndpoint->pAioMgr, pAioMgr);
232
233 int rc = RTCritSectEnter(&pAioMgr->CritSectBlockingEvent);
234 AssertRCReturn(rc, rc);
235
236 ASMAtomicWritePtr(&pAioMgr->BlockingEventData.AddEndpoint.pEndpoint, pEndpoint);
237 rc = pdmacFileAioMgrWaitForBlockingEvent(pAioMgr, PDMACEPFILEAIOMGRBLOCKINGEVENT_ADD_ENDPOINT);
238 ASMAtomicWriteNullPtr(&pAioMgr->BlockingEventData.AddEndpoint.pEndpoint);
239
240 RTCritSectLeave(&pAioMgr->CritSectBlockingEvent);
241
242 return rc;
243}
244
245#ifdef SOME_UNUSED_FUNCTION
246static int pdmacFileAioMgrRemoveEndpoint(PPDMACEPFILEMGR pAioMgr, PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
247{
248 int rc = RTCritSectEnter(&pAioMgr->CritSectBlockingEvent);
249 AssertRCReturn(rc, rc);
250
251 ASMAtomicWritePtr(&pAioMgr->BlockingEventData.RemoveEndpoint.pEndpoint, pEndpoint);
252 rc = pdmacFileAioMgrWaitForBlockingEvent(pAioMgr, PDMACEPFILEAIOMGRBLOCKINGEVENT_REMOVE_ENDPOINT);
253 ASMAtomicWriteNullPtr(&pAioMgr->BlockingEventData.RemoveEndpoint.pEndpoint);
254
255 RTCritSectLeave(&pAioMgr->CritSectBlockingEvent);
256
257 return rc;
258}
259#endif
260
261static int pdmacFileAioMgrCloseEndpoint(PPDMACEPFILEMGR pAioMgr, PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
262{
263 int rc = RTCritSectEnter(&pAioMgr->CritSectBlockingEvent);
264 AssertRCReturn(rc, rc);
265
266 ASMAtomicWritePtr(&pAioMgr->BlockingEventData.CloseEndpoint.pEndpoint, pEndpoint);
267 rc = pdmacFileAioMgrWaitForBlockingEvent(pAioMgr, PDMACEPFILEAIOMGRBLOCKINGEVENT_CLOSE_ENDPOINT);
268 ASMAtomicWriteNullPtr(&pAioMgr->BlockingEventData.CloseEndpoint.pEndpoint);
269
270 RTCritSectLeave(&pAioMgr->CritSectBlockingEvent);
271
272 return rc;
273}
274
275static int pdmacFileAioMgrShutdown(PPDMACEPFILEMGR pAioMgr)
276{
277 int rc = RTCritSectEnter(&pAioMgr->CritSectBlockingEvent);
278 AssertRCReturn(rc, rc);
279
280 rc = pdmacFileAioMgrWaitForBlockingEvent(pAioMgr, PDMACEPFILEAIOMGRBLOCKINGEVENT_SHUTDOWN);
281
282 RTCritSectLeave(&pAioMgr->CritSectBlockingEvent);
283
284 return rc;
285}
286
287int pdmacFileEpAddTask(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint, PPDMACTASKFILE pTask)
288{
289 PPDMACTASKFILE pNext;
290 do
291 {
292 pNext = pEndpoint->pTasksNewHead;
293 pTask->pNext = pNext;
294 } while (!ASMAtomicCmpXchgPtr(&pEndpoint->pTasksNewHead, pTask, pNext));
295
296 pdmacFileAioMgrWakeup(ASMAtomicReadPtrT(&pEndpoint->pAioMgr, PPDMACEPFILEMGR));
297
298 return VINF_SUCCESS;
299}
300
301static DECLCALLBACK(void) pdmacFileEpTaskCompleted(PPDMACTASKFILE pTask, void *pvUser, int rc)
302{
303 PPDMASYNCCOMPLETIONTASKFILE pTaskFile = (PPDMASYNCCOMPLETIONTASKFILE)pvUser;
304
305 LogFlowFunc(("pTask=%#p pvUser=%#p rc=%Rrc\n", pTask, pvUser, rc));
306
307 if (pTask->enmTransferType == PDMACTASKFILETRANSFER_FLUSH)
308 pdmR3AsyncCompletionCompleteTask(&pTaskFile->Core, rc, true);
309 else
310 {
311 Assert((uint32_t)pTask->DataSeg.cbSeg == pTask->DataSeg.cbSeg && (int32_t)pTask->DataSeg.cbSeg >= 0);
312 uint32_t uOld = ASMAtomicSubS32(&pTaskFile->cbTransferLeft, (int32_t)pTask->DataSeg.cbSeg);
313
314 /* The first error will be returned. */
315 if (RT_FAILURE(rc))
316 ASMAtomicCmpXchgS32(&pTaskFile->rc, rc, VINF_SUCCESS);
317#ifdef VBOX_WITH_DEBUGGER
318 else
319 {
320 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pTaskFile->Core.pEndpoint;
321
322 /* Overwrite with injected error code. */
323 if (pTask->enmTransferType == PDMACTASKFILETRANSFER_READ)
324 rc = ASMAtomicXchgS32(&pEpFile->rcReqRead, VINF_SUCCESS);
325 else
326 rc = ASMAtomicXchgS32(&pEpFile->rcReqWrite, VINF_SUCCESS);
327
328 if (RT_FAILURE(rc))
329 ASMAtomicCmpXchgS32(&pTaskFile->rc, rc, VINF_SUCCESS);
330 }
331#endif
332
333 if (!(uOld - pTask->DataSeg.cbSeg)
334 && !ASMAtomicXchgBool(&pTaskFile->fCompleted, true))
335 {
336#ifdef PDM_ASYNC_COMPLETION_FILE_WITH_DELAY
337 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pTaskFile->Core.pEndpoint;
338 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEpFile->Core.pEpClass;
339
340 /* Check if we should delay completion of the request. */
341 if ( ASMAtomicReadU32(&pEpFile->msDelay) > 0
342 && ASMAtomicReadU32(&pEpFile->cReqsDelay) > 0)
343 {
344 uint64_t tsDelay = pEpFile->msDelay;
345
346 if (pEpFile->msJitter)
347 tsDelay = (RTRandU32() % 100) > 50 ? pEpFile->msDelay + (RTRandU32() % pEpFile->msJitter)
348 : pEpFile->msDelay - (RTRandU32() % pEpFile->msJitter);
349 ASMAtomicDecU32(&pEpFile->cReqsDelay);
350
351 /* Arm the delay. */
352 pTaskFile->tsDelayEnd = RTTimeProgramMilliTS() + tsDelay;
353
354 /* Append to the list. */
355 PPDMASYNCCOMPLETIONTASKFILE pHead = NULL;
356 do
357 {
358 pHead = ASMAtomicReadPtrT(&pEpFile->pDelayedHead, PPDMASYNCCOMPLETIONTASKFILE);
359 pTaskFile->pDelayedNext = pHead;
360 } while (!ASMAtomicCmpXchgPtr(&pEpFile->pDelayedHead, pTaskFile, pHead));
361
362 if (tsDelay < pEpClassFile->cMilliesNext)
363 {
364 ASMAtomicWriteU64(&pEpClassFile->cMilliesNext, tsDelay);
365 TMTimerSetMillies(pEpClassFile->pTimer, tsDelay);
366 }
367
368 LogRel(("AIOMgr: Delaying request %#p for %u ms\n", pTaskFile, tsDelay));
369 }
370 else
371#endif
372 pdmR3AsyncCompletionCompleteTask(&pTaskFile->Core, pTaskFile->rc, true);
373 }
374 }
375}
376
377DECLINLINE(void) pdmacFileEpTaskInit(PPDMASYNCCOMPLETIONTASK pTask, size_t cbTransfer)
378{
379 PPDMASYNCCOMPLETIONTASKFILE pTaskFile = (PPDMASYNCCOMPLETIONTASKFILE)pTask;
380
381 Assert((uint32_t)cbTransfer == cbTransfer && (int32_t)cbTransfer >= 0);
382 ASMAtomicWriteS32(&pTaskFile->cbTransferLeft, (int32_t)cbTransfer);
383 ASMAtomicWriteBool(&pTaskFile->fCompleted, false);
384 ASMAtomicWriteS32(&pTaskFile->rc, VINF_SUCCESS);
385}
386
387int pdmacFileEpTaskInitiate(PPDMASYNCCOMPLETIONTASK pTask,
388 PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
389 PCRTSGSEG paSegments, size_t cSegments,
390 size_t cbTransfer, PDMACTASKFILETRANSFER enmTransfer)
391{
392 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
393 PPDMASYNCCOMPLETIONTASKFILE pTaskFile = (PPDMASYNCCOMPLETIONTASKFILE)pTask;
394
395 Assert( (enmTransfer == PDMACTASKFILETRANSFER_READ)
396 || (enmTransfer == PDMACTASKFILETRANSFER_WRITE));
397
398 for (size_t i = 0; i < cSegments; i++)
399 {
400 PPDMACTASKFILE pIoTask = pdmacFileTaskAlloc(pEpFile);
401 AssertPtr(pIoTask);
402
403 pIoTask->pEndpoint = pEpFile;
404 pIoTask->enmTransferType = enmTransfer;
405 pIoTask->Off = off;
406 pIoTask->DataSeg.cbSeg = paSegments[i].cbSeg;
407 pIoTask->DataSeg.pvSeg = paSegments[i].pvSeg;
408 pIoTask->pvUser = pTaskFile;
409 pIoTask->pfnCompleted = pdmacFileEpTaskCompleted;
410
411 /* Send it off to the I/O manager. */
412 pdmacFileEpAddTask(pEpFile, pIoTask);
413 off += paSegments[i].cbSeg;
414 cbTransfer -= paSegments[i].cbSeg;
415 }
416
417 AssertMsg(!cbTransfer, ("Incomplete transfer %u bytes left\n", cbTransfer));
418
419 return VINF_AIO_TASK_PENDING;
420}
421
422/**
423 * Creates a new async I/O manager.
424 *
425 * @returns VBox status code.
426 * @param pEpClass Pointer to the endpoint class data.
427 * @param ppAioMgr Where to store the pointer to the new async I/O manager on success.
428 * @param enmMgrType Wanted manager type - can be overwritten by the global override.
429 */
430int pdmacFileAioMgrCreate(PPDMASYNCCOMPLETIONEPCLASSFILE pEpClass, PPPDMACEPFILEMGR ppAioMgr,
431 PDMACEPFILEMGRTYPE enmMgrType)
432{
433 LogFlowFunc((": Entered\n"));
434
435 PPDMACEPFILEMGR pAioMgrNew;
436 int rc = MMR3HeapAllocZEx(pEpClass->Core.pVM, MM_TAG_PDM_ASYNC_COMPLETION, sizeof(PDMACEPFILEMGR), (void **)&pAioMgrNew);
437 if (RT_SUCCESS(rc))
438 {
439 if (enmMgrType < pEpClass->enmMgrTypeOverride)
440 pAioMgrNew->enmMgrType = enmMgrType;
441 else
442 pAioMgrNew->enmMgrType = pEpClass->enmMgrTypeOverride;
443
444 pAioMgrNew->msBwLimitExpired = RT_INDEFINITE_WAIT;
445
446 rc = RTSemEventCreate(&pAioMgrNew->EventSem);
447 if (RT_SUCCESS(rc))
448 {
449 rc = RTSemEventCreate(&pAioMgrNew->EventSemBlock);
450 if (RT_SUCCESS(rc))
451 {
452 rc = RTCritSectInit(&pAioMgrNew->CritSectBlockingEvent);
453 if (RT_SUCCESS(rc))
454 {
455 /* Init the rest of the manager. */
456 if (pAioMgrNew->enmMgrType != PDMACEPFILEMGRTYPE_SIMPLE)
457 rc = pdmacFileAioMgrNormalInit(pAioMgrNew);
458
459 if (RT_SUCCESS(rc))
460 {
461 pAioMgrNew->enmState = PDMACEPFILEMGRSTATE_RUNNING;
462
463 rc = RTThreadCreateF(&pAioMgrNew->Thread,
464 pAioMgrNew->enmMgrType == PDMACEPFILEMGRTYPE_SIMPLE
465 ? pdmacFileAioMgrFailsafe
466 : pdmacFileAioMgrNormal,
467 pAioMgrNew,
468 0,
469 RTTHREADTYPE_IO,
470 0,
471 "AioMgr%d-%s", pEpClass->cAioMgrs,
472 pAioMgrNew->enmMgrType == PDMACEPFILEMGRTYPE_SIMPLE
473 ? "F"
474 : "N");
475 if (RT_SUCCESS(rc))
476 {
477 /* Link it into the list. */
478 RTCritSectEnter(&pEpClass->CritSect);
479 pAioMgrNew->pNext = pEpClass->pAioMgrHead;
480 if (pEpClass->pAioMgrHead)
481 pEpClass->pAioMgrHead->pPrev = pAioMgrNew;
482 pEpClass->pAioMgrHead = pAioMgrNew;
483 pEpClass->cAioMgrs++;
484 RTCritSectLeave(&pEpClass->CritSect);
485
486 *ppAioMgr = pAioMgrNew;
487
488 Log(("PDMAC: Successfully created new file AIO Mgr {%s}\n", RTThreadGetName(pAioMgrNew->Thread)));
489 return VINF_SUCCESS;
490 }
491 pdmacFileAioMgrNormalDestroy(pAioMgrNew);
492 }
493 RTCritSectDelete(&pAioMgrNew->CritSectBlockingEvent);
494 }
495 RTSemEventDestroy(pAioMgrNew->EventSem);
496 }
497 RTSemEventDestroy(pAioMgrNew->EventSemBlock);
498 }
499 MMR3HeapFree(pAioMgrNew);
500 }
501
502 LogFlowFunc((": Leave rc=%Rrc\n", rc));
503
504 return rc;
505}
506
507/**
508 * Destroys a async I/O manager.
509 *
510 * @returns nothing.
511 * @param pEpClassFile Pointer to globals for the file endpoint class.
512 * @param pAioMgr The async I/O manager to destroy.
513 */
514static void pdmacFileAioMgrDestroy(PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile, PPDMACEPFILEMGR pAioMgr)
515{
516 int rc = pdmacFileAioMgrShutdown(pAioMgr);
517 AssertRC(rc);
518
519 /* Unlink from the list. */
520 rc = RTCritSectEnter(&pEpClassFile->CritSect);
521 AssertRC(rc);
522
523 PPDMACEPFILEMGR pPrev = pAioMgr->pPrev;
524 PPDMACEPFILEMGR pNext = pAioMgr->pNext;
525
526 if (pPrev)
527 pPrev->pNext = pNext;
528 else
529 pEpClassFile->pAioMgrHead = pNext;
530
531 if (pNext)
532 pNext->pPrev = pPrev;
533
534 pEpClassFile->cAioMgrs--;
535 rc = RTCritSectLeave(&pEpClassFile->CritSect);
536 AssertRC(rc);
537
538 /* Free the resources. */
539 RTCritSectDelete(&pAioMgr->CritSectBlockingEvent);
540 RTSemEventDestroy(pAioMgr->EventSem);
541 RTSemEventDestroy(pAioMgr->EventSemBlock);
542 if (pAioMgr->enmMgrType != PDMACEPFILEMGRTYPE_SIMPLE)
543 pdmacFileAioMgrNormalDestroy(pAioMgr);
544
545 MMR3HeapFree(pAioMgr);
546}
547
548static int pdmacFileMgrTypeFromName(const char *pszVal, PPDMACEPFILEMGRTYPE penmMgrType)
549{
550 int rc = VINF_SUCCESS;
551
552 if (!RTStrCmp(pszVal, "Simple"))
553 *penmMgrType = PDMACEPFILEMGRTYPE_SIMPLE;
554 else if (!RTStrCmp(pszVal, "Async"))
555 *penmMgrType = PDMACEPFILEMGRTYPE_ASYNC;
556 else
557 rc = VERR_CFGM_CONFIG_UNKNOWN_VALUE;
558
559 return rc;
560}
561
562static const char *pdmacFileMgrTypeToName(PDMACEPFILEMGRTYPE enmMgrType)
563{
564 if (enmMgrType == PDMACEPFILEMGRTYPE_SIMPLE)
565 return "Simple";
566 if (enmMgrType == PDMACEPFILEMGRTYPE_ASYNC)
567 return "Async";
568
569 return NULL;
570}
571
572static int pdmacFileBackendTypeFromName(const char *pszVal, PPDMACFILEEPBACKEND penmBackendType)
573{
574 int rc = VINF_SUCCESS;
575
576 if (!RTStrCmp(pszVal, "Buffered"))
577 *penmBackendType = PDMACFILEEPBACKEND_BUFFERED;
578 else if (!RTStrCmp(pszVal, "NonBuffered"))
579 *penmBackendType = PDMACFILEEPBACKEND_NON_BUFFERED;
580 else
581 rc = VERR_CFGM_CONFIG_UNKNOWN_VALUE;
582
583 return rc;
584}
585
586static const char *pdmacFileBackendTypeToName(PDMACFILEEPBACKEND enmBackendType)
587{
588 if (enmBackendType == PDMACFILEEPBACKEND_BUFFERED)
589 return "Buffered";
590 if (enmBackendType == PDMACFILEEPBACKEND_NON_BUFFERED)
591 return "NonBuffered";
592
593 return NULL;
594}
595
596#ifdef VBOX_WITH_DEBUGGER
597
598/**
599 * @callback_method_impl{FNDBGCCMD, The '.injecterror' command.}
600 */
601static DECLCALLBACK(int) pdmacEpFileErrorInject(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR pArgs, unsigned cArgs)
602{
603 /*
604 * Validate input.
605 */
606 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
607 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, -1, cArgs == 3);
608 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, pArgs[0].enmType == DBGCVAR_TYPE_STRING);
609 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 1, pArgs[1].enmType == DBGCVAR_TYPE_STRING);
610 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 2, pArgs[2].enmType == DBGCVAR_TYPE_NUMBER);
611
612 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile;
613 pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pUVM->pdm.s.apAsyncCompletionEndpointClass[PDMASYNCCOMPLETIONEPCLASSTYPE_FILE];
614
615 /* Syntax is "read|write <filename> <status code>" */
616 bool fWrite;
617 if (!RTStrCmp(pArgs[0].u.pszString, "read"))
618 fWrite = false;
619 else if (!RTStrCmp(pArgs[0].u.pszString, "write"))
620 fWrite = true;
621 else
622 return DBGCCmdHlpFail(pCmdHlp, pCmd, "invalid transfer direction '%s'", pArgs[0].u.pszString);
623
624 int32_t rcToInject = (int32_t)pArgs[2].u.u64Number;
625 if ((uint64_t)rcToInject != pArgs[2].u.u64Number)
626 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The status code '%lld' is out of range", pArgs[0].u.u64Number);
627
628 /*
629 * Search for the matching endpoint.
630 */
631 RTCritSectEnter(&pEpClassFile->Core.CritSect);
632
633 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEpClassFile->Core.pEndpointsHead;
634 while (pEpFile)
635 {
636 if (!RTStrCmp(pArgs[1].u.pszString, RTPathFilename(pEpFile->Core.pszUri)))
637 break;
638 pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEpFile->Core.pNext;
639 }
640
641 if (pEpFile)
642 {
643 /*
644 * Do the job.
645 */
646 if (fWrite)
647 ASMAtomicXchgS32(&pEpFile->rcReqWrite, rcToInject);
648 else
649 ASMAtomicXchgS32(&pEpFile->rcReqRead, rcToInject);
650
651 DBGCCmdHlpPrintf(pCmdHlp, "Injected %Rrc into '%s' for %s\n",
652 (int)rcToInject, pArgs[1].u.pszString, pArgs[0].u.pszString);
653 }
654
655 RTCritSectLeave(&pEpClassFile->Core.CritSect);
656
657 if (!pEpFile)
658 return DBGCCmdHlpFail(pCmdHlp, pCmd, "No file with name '%s' found", pArgs[1].u.pszString);
659 return VINF_SUCCESS;
660}
661
662# ifdef PDM_ASYNC_COMPLETION_FILE_WITH_DELAY
663/**
664 * @callback_method_impl{FNDBGCCMD, The '.injectdelay' command.}
665 */
666static DECLCALLBACK(int) pdmacEpFileDelayInject(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR pArgs, unsigned cArgs)
667{
668 /*
669 * Validate input.
670 */
671 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
672 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, -1, cArgs >= 3);
673 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, pArgs[0].enmType == DBGCVAR_TYPE_STRING);
674 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 1, pArgs[1].enmType == DBGCVAR_TYPE_STRING);
675 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 2, pArgs[2].enmType == DBGCVAR_TYPE_NUMBER);
676
677 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile;
678 pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pUVM->pdm.s.apAsyncCompletionEndpointClass[PDMASYNCCOMPLETIONEPCLASSTYPE_FILE];
679
680 /* Syntax is "read|write|flush|any <filename> <delay> [reqs]" */
681 PDMACFILEREQTYPEDELAY enmDelayType = PDMACFILEREQTYPEDELAY_ANY;
682 if (!RTStrCmp(pArgs[0].u.pszString, "read"))
683 enmDelayType = PDMACFILEREQTYPEDELAY_READ;
684 else if (!RTStrCmp(pArgs[0].u.pszString, "write"))
685 enmDelayType = PDMACFILEREQTYPEDELAY_WRITE;
686 else if (!RTStrCmp(pArgs[0].u.pszString, "flush"))
687 enmDelayType = PDMACFILEREQTYPEDELAY_FLUSH;
688 else if (!RTStrCmp(pArgs[0].u.pszString, "any"))
689 enmDelayType = PDMACFILEREQTYPEDELAY_ANY;
690 else
691 return DBGCCmdHlpFail(pCmdHlp, pCmd, "invalid transfer direction '%s'", pArgs[0].u.pszString);
692
693 uint32_t msDelay = (uint32_t)pArgs[2].u.u64Number;
694 if ((uint64_t)msDelay != pArgs[2].u.u64Number)
695 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The delay '%lld' is out of range", pArgs[0].u.u64Number);
696
697 uint32_t cReqsDelay = 1;
698 uint32_t msJitter = 0;
699 if (cArgs >= 4)
700 msJitter = (uint32_t)pArgs[3].u.u64Number;
701 if (cArgs == 5)
702 cReqsDelay = (uint32_t)pArgs[4].u.u64Number;
703
704 /*
705 * Search for the matching endpoint.
706 */
707 RTCritSectEnter(&pEpClassFile->Core.CritSect);
708
709 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEpClassFile->Core.pEndpointsHead;
710 while (pEpFile)
711 {
712 if (!RTStrCmp(pArgs[1].u.pszString, RTPathFilename(pEpFile->Core.pszUri)))
713 break;
714 pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEpFile->Core.pNext;
715 }
716
717 if (pEpFile)
718 {
719 ASMAtomicWriteSize(&pEpFile->enmTypeDelay, enmDelayType);
720 ASMAtomicWriteU32(&pEpFile->msDelay, msDelay);
721 ASMAtomicWriteU32(&pEpFile->msJitter, msJitter);
722 ASMAtomicWriteU32(&pEpFile->cReqsDelay, cReqsDelay);
723
724 DBGCCmdHlpPrintf(pCmdHlp, "Injected delay for the next %u requests of %u ms into '%s' for %s\n",
725 cReqsDelay, msDelay, pArgs[1].u.pszString, pArgs[0].u.pszString);
726 }
727
728 RTCritSectLeave(&pEpClassFile->Core.CritSect);
729
730 if (!pEpFile)
731 return DBGCCmdHlpFail(pCmdHlp, pCmd, "No file with name '%s' found", pArgs[1].u.pszString);
732 return VINF_SUCCESS;
733}
734
735static DECLCALLBACK(void) pdmacR3TimerCallback(PVM pVM, PTMTIMER pTimer, void *pvUser)
736{
737 uint64_t tsCur = RTTimeProgramMilliTS();
738 uint64_t cMilliesNext = UINT64_MAX;
739 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pvUser;
740
741 ASMAtomicWriteU64(&pEpClassFile->cMilliesNext, UINT64_MAX);
742
743 /* Go through all endpoints and check for expired requests. */
744 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEpClassFile->Core.pEndpointsHead;
745
746 while (pEpFile)
747 {
748 /* Check for an expired delay. */
749 if (pEpFile->pDelayedHead != NULL)
750 {
751 PPDMASYNCCOMPLETIONTASKFILE pTaskFile = ASMAtomicXchgPtrT(&pEpFile->pDelayedHead, NULL, PPDMASYNCCOMPLETIONTASKFILE);
752
753 while (pTaskFile)
754 {
755 PPDMASYNCCOMPLETIONTASKFILE pTmp = pTaskFile;
756 pTaskFile = pTaskFile->pDelayedNext;
757
758 if (tsCur >= pTmp->tsDelayEnd)
759 {
760 LogRel(("AIOMgr: Delayed request %#p completed\n", pTmp));
761 pdmR3AsyncCompletionCompleteTask(&pTmp->Core, pTmp->rc, true);
762 }
763 else
764 {
765 /* Prepend to the delayed list again. */
766 PPDMASYNCCOMPLETIONTASKFILE pHead = NULL;
767
768 if (pTmp->tsDelayEnd - tsCur < cMilliesNext)
769 cMilliesNext = pTmp->tsDelayEnd - tsCur;
770
771 do
772 {
773 pHead = ASMAtomicReadPtrT(&pEpFile->pDelayedHead, PPDMASYNCCOMPLETIONTASKFILE);
774 pTmp->pDelayedNext = pHead;
775 } while (!ASMAtomicCmpXchgPtr(&pEpFile->pDelayedHead, pTmp, pHead));
776 }
777 }
778 }
779
780 pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEpFile->Core.pNext;
781 }
782
783 if (cMilliesNext < pEpClassFile->cMilliesNext)
784 {
785 ASMAtomicWriteU64(&pEpClassFile->cMilliesNext, cMilliesNext);
786 TMTimerSetMillies(pEpClassFile->pTimer, cMilliesNext);
787 }
788}
789
790# endif /* PDM_ASYNC_COMPLETION_FILE_WITH_DELAY */
791
792#endif /* VBOX_WITH_DEBUGGER */
793
794static DECLCALLBACK(int) pdmacFileInitialize(PPDMASYNCCOMPLETIONEPCLASS pClassGlobals, PCFGMNODE pCfgNode)
795{
796 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pClassGlobals;
797 RTFILEAIOLIMITS AioLimits; /** < Async I/O limitations. */
798
799 int rc = RTFileAioGetLimits(&AioLimits);
800#ifdef DEBUG
801 if (RT_SUCCESS(rc) && RTEnvExist("VBOX_ASYNC_IO_FAILBACK"))
802 rc = VERR_ENV_VAR_NOT_FOUND;
803#endif
804 if (RT_FAILURE(rc))
805 {
806 LogRel(("AIO: Async I/O manager not supported (rc=%Rrc). Falling back to simple manager\n", rc));
807 pEpClassFile->enmMgrTypeOverride = PDMACEPFILEMGRTYPE_SIMPLE;
808 pEpClassFile->enmEpBackendDefault = PDMACFILEEPBACKEND_BUFFERED;
809 }
810 else
811 {
812 pEpClassFile->uBitmaskAlignment = AioLimits.cbBufferAlignment ? ~((RTR3UINTPTR)AioLimits.cbBufferAlignment - 1) : RTR3UINTPTR_MAX;
813 pEpClassFile->cReqsOutstandingMax = AioLimits.cReqsOutstandingMax;
814
815 if (pCfgNode)
816 {
817 /* Query the default manager type */
818 char *pszVal = NULL;
819 rc = CFGMR3QueryStringAllocDef(pCfgNode, "IoMgr", &pszVal, "Async");
820 AssertLogRelRCReturn(rc, rc);
821
822 rc = pdmacFileMgrTypeFromName(pszVal, &pEpClassFile->enmMgrTypeOverride);
823 MMR3HeapFree(pszVal);
824 if (RT_FAILURE(rc))
825 return rc;
826
827 LogRel(("AIOMgr: Default manager type is '%s'\n", pdmacFileMgrTypeToName(pEpClassFile->enmMgrTypeOverride)));
828
829 /* Query default backend type */
830 rc = CFGMR3QueryStringAllocDef(pCfgNode, "FileBackend", &pszVal, "NonBuffered");
831 AssertLogRelRCReturn(rc, rc);
832
833 rc = pdmacFileBackendTypeFromName(pszVal, &pEpClassFile->enmEpBackendDefault);
834 MMR3HeapFree(pszVal);
835 if (RT_FAILURE(rc))
836 return rc;
837
838 LogRel(("AIOMgr: Default file backend is '%s'\n", pdmacFileBackendTypeToName(pEpClassFile->enmEpBackendDefault)));
839
840#ifdef RT_OS_LINUX
841 if ( pEpClassFile->enmMgrTypeOverride == PDMACEPFILEMGRTYPE_ASYNC
842 && pEpClassFile->enmEpBackendDefault == PDMACFILEEPBACKEND_BUFFERED)
843 {
844 LogRel(("AIOMgr: Linux does not support buffered async I/O, changing to non buffered\n"));
845 pEpClassFile->enmEpBackendDefault = PDMACFILEEPBACKEND_NON_BUFFERED;
846 }
847#endif
848 }
849 else
850 {
851 /* No configuration supplied, set defaults */
852 pEpClassFile->enmEpBackendDefault = PDMACFILEEPBACKEND_NON_BUFFERED;
853 pEpClassFile->enmMgrTypeOverride = PDMACEPFILEMGRTYPE_ASYNC;
854 }
855 }
856
857 /* Init critical section. */
858 rc = RTCritSectInit(&pEpClassFile->CritSect);
859
860#ifdef VBOX_WITH_DEBUGGER
861 /* Install the error injection handler. */
862 if (RT_SUCCESS(rc))
863 {
864 rc = DBGCRegisterCommands(&g_aCmds[0], RT_ELEMENTS(g_aCmds));
865 AssertRC(rc);
866 }
867
868#ifdef PDM_ASYNC_COMPLETION_FILE_WITH_DELAY
869 rc = TMR3TimerCreateInternal(pEpClassFile->Core.pVM, TMCLOCK_REAL, pdmacR3TimerCallback, pEpClassFile, "AC Delay", &pEpClassFile->pTimer);
870 AssertRC(rc);
871 pEpClassFile->cMilliesNext = UINT64_MAX;
872#endif
873#endif
874
875 return rc;
876}
877
878static DECLCALLBACK(void) pdmacFileTerminate(PPDMASYNCCOMPLETIONEPCLASS pClassGlobals)
879{
880 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pClassGlobals;
881
882 /* All endpoints should be closed at this point. */
883 AssertMsg(!pEpClassFile->Core.pEndpointsHead, ("There are still endpoints left\n"));
884
885 /* Destroy all left async I/O managers. */
886 while (pEpClassFile->pAioMgrHead)
887 pdmacFileAioMgrDestroy(pEpClassFile, pEpClassFile->pAioMgrHead);
888
889 RTCritSectDelete(&pEpClassFile->CritSect);
890}
891
892static DECLCALLBACK(int) pdmacFileEpInitialize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint,
893 const char *pszUri, uint32_t fFlags)
894{
895 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
896 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->pEpClass;
897 PDMACEPFILEMGRTYPE enmMgrType = pEpClassFile->enmMgrTypeOverride;
898 PDMACFILEEPBACKEND enmEpBackend = pEpClassFile->enmEpBackendDefault;
899
900 AssertMsgReturn((fFlags & ~(PDMACEP_FILE_FLAGS_READ_ONLY | PDMACEP_FILE_FLAGS_DONT_LOCK | PDMACEP_FILE_FLAGS_HOST_CACHE_ENABLED)) == 0,
901 ("PDMAsyncCompletion: Invalid flag specified\n"), VERR_INVALID_PARAMETER);
902
903 unsigned fFileFlags = RTFILE_O_OPEN;
904
905 /*
906 * Revert to the simple manager and the buffered backend if
907 * the host cache should be enabled.
908 */
909 if (fFlags & PDMACEP_FILE_FLAGS_HOST_CACHE_ENABLED)
910 {
911 enmMgrType = PDMACEPFILEMGRTYPE_SIMPLE;
912 enmEpBackend = PDMACFILEEPBACKEND_BUFFERED;
913 }
914
915 if (fFlags & PDMACEP_FILE_FLAGS_READ_ONLY)
916 fFileFlags |= RTFILE_O_READ | RTFILE_O_DENY_NONE;
917 else
918 {
919 fFileFlags |= RTFILE_O_READWRITE;
920
921 /*
922 * Opened in read/write mode. Check whether the caller wants to
923 * avoid the lock. Return an error in case caching is enabled
924 * because this can lead to data corruption.
925 */
926 if (fFlags & PDMACEP_FILE_FLAGS_DONT_LOCK)
927 fFileFlags |= RTFILE_O_DENY_NONE;
928 else
929 fFileFlags |= RTFILE_O_DENY_WRITE;
930 }
931
932 if (enmMgrType == PDMACEPFILEMGRTYPE_ASYNC)
933 fFileFlags |= RTFILE_O_ASYNC_IO;
934
935 int rc;
936 if (enmEpBackend == PDMACFILEEPBACKEND_NON_BUFFERED)
937 {
938 /*
939 * We only disable the cache if the size of the file is a multiple of 512.
940 * Certain hosts like Windows, Linux and Solaris require that transfer sizes
941 * are aligned to the volume sector size.
942 * If not we just make sure that the data is written to disk with RTFILE_O_WRITE_THROUGH
943 * which will trash the host cache but ensures that the host cache will not
944 * contain dirty buffers.
945 */
946 RTFILE hFile;
947 rc = RTFileOpen(&hFile, pszUri, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
948 if (RT_SUCCESS(rc))
949 {
950 uint64_t cbSize;
951
952 rc = RTFileGetSize(hFile, &cbSize);
953
954 if (RT_SUCCESS(rc) && ((cbSize % 512) == 0))
955 fFileFlags |= RTFILE_O_NO_CACHE;
956 else
957 {
958 /* Downgrade to the buffered backend */
959 enmEpBackend = PDMACFILEEPBACKEND_BUFFERED;
960
961#ifdef RT_OS_LINUX
962 fFileFlags &= ~RTFILE_O_ASYNC_IO;
963 enmMgrType = PDMACEPFILEMGRTYPE_SIMPLE;
964#endif
965 }
966 RTFileClose(hFile);
967 }
968 }
969
970 /* Open with final flags. */
971 rc = RTFileOpen(&pEpFile->hFile, pszUri, fFileFlags);
972 if ( rc == VERR_INVALID_FUNCTION
973 || rc == VERR_INVALID_PARAMETER)
974 {
975 LogRel(("AIOMgr: pdmacFileEpInitialize: RTFileOpen %s / %08x failed with %Rrc\n",
976 pszUri, fFileFlags, rc));
977 /*
978 * Solaris doesn't support directio on ZFS so far. :-\
979 * Trying to enable it returns VERR_INVALID_FUNCTION
980 * (ENOTTY). Remove it and hope for the best.
981 * ZFS supports write throttling in case applications
982 * write more data than can be synced to the disk
983 * without blocking the whole application.
984 *
985 * On Linux we have the same problem with cifs.
986 * Have to disable async I/O here too because it requires O_DIRECT.
987 */
988 fFileFlags &= ~RTFILE_O_NO_CACHE;
989 enmEpBackend = PDMACFILEEPBACKEND_BUFFERED;
990
991#ifdef RT_OS_LINUX
992 fFileFlags &= ~RTFILE_O_ASYNC_IO;
993 enmMgrType = PDMACEPFILEMGRTYPE_SIMPLE;
994#endif
995
996 /* Open again. */
997 rc = RTFileOpen(&pEpFile->hFile, pszUri, fFileFlags);
998
999 if (RT_FAILURE(rc))
1000 {
1001 LogRel(("AIOMgr: pdmacFileEpInitialize: RTFileOpen %s / %08x failed AGAIN(!) with %Rrc\n",
1002 pszUri, fFileFlags, rc));
1003 }
1004 }
1005
1006 if (RT_SUCCESS(rc))
1007 {
1008 pEpFile->fFlags = fFileFlags;
1009
1010 rc = RTFileGetSize(pEpFile->hFile, (uint64_t *)&pEpFile->cbFile);
1011 if (RT_SUCCESS(rc))
1012 {
1013 /* Initialize the segment cache */
1014 rc = MMR3HeapAllocZEx(pEpClassFile->Core.pVM, MM_TAG_PDM_ASYNC_COMPLETION,
1015 sizeof(PDMACTASKFILE),
1016 (void **)&pEpFile->pTasksFreeHead);
1017 if (RT_SUCCESS(rc))
1018 {
1019 PPDMACEPFILEMGR pAioMgr = NULL;
1020
1021 pEpFile->pTasksFreeTail = pEpFile->pTasksFreeHead;
1022 pEpFile->cTasksCached = 0;
1023 pEpFile->enmBackendType = enmEpBackend;
1024 /*
1025 * Disable async flushes on Solaris for now.
1026 * They cause weird hangs which needs more investigations.
1027 */
1028#ifndef RT_OS_SOLARIS
1029 pEpFile->fAsyncFlushSupported = true;
1030#else
1031 pEpFile->fAsyncFlushSupported = false;
1032#endif
1033
1034 if (enmMgrType == PDMACEPFILEMGRTYPE_SIMPLE)
1035 {
1036 /* Simple mode. Every file has its own async I/O manager. */
1037 rc = pdmacFileAioMgrCreate(pEpClassFile, &pAioMgr, PDMACEPFILEMGRTYPE_SIMPLE);
1038 }
1039 else
1040 {
1041 pAioMgr = pEpClassFile->pAioMgrHead;
1042
1043 /* Check for an idling manager of the same type */
1044 while (pAioMgr)
1045 {
1046 if (pAioMgr->enmMgrType == enmMgrType)
1047 break;
1048 pAioMgr = pAioMgr->pNext;
1049 }
1050
1051 if (!pAioMgr)
1052 rc = pdmacFileAioMgrCreate(pEpClassFile, &pAioMgr, enmMgrType);
1053 }
1054
1055 if (RT_SUCCESS(rc))
1056 {
1057 pEpFile->AioMgr.pTreeRangesLocked = (PAVLRFOFFTREE)RTMemAllocZ(sizeof(AVLRFOFFTREE));
1058 if (!pEpFile->AioMgr.pTreeRangesLocked)
1059 rc = VERR_NO_MEMORY;
1060 else
1061 {
1062 pEpFile->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE;
1063
1064 /* Assign the endpoint to the thread. */
1065 rc = pdmacFileAioMgrAddEndpoint(pAioMgr, pEpFile);
1066 if (RT_FAILURE(rc))
1067 {
1068 RTMemFree(pEpFile->AioMgr.pTreeRangesLocked);
1069 MMR3HeapFree(pEpFile->pTasksFreeHead);
1070 }
1071 }
1072 }
1073 else if (rc == VERR_FILE_AIO_INSUFFICIENT_EVENTS)
1074 {
1075 PUVM pUVM = VMR3GetUVM(pEpClassFile->Core.pVM);
1076#if defined(RT_OS_LINUX)
1077 rc = VMR3SetError(pUVM, rc, RT_SRC_POS,
1078 N_("Failed to create I/O manager for VM due to insufficient resources on the host. "
1079 "Either increase the amount of allowed events in /proc/sys/fs/aio-max-nr or enable "
1080 "the host I/O cache"));
1081#else
1082 rc = VMR3SetError(pUVM, rc, RT_SRC_POS,
1083 N_("Failed to create I/O manager for VM due to insufficient resources on the host. "
1084 "Enable the host I/O cache"));
1085#endif
1086 }
1087 else
1088 {
1089 PUVM pUVM = VMR3GetUVM(pEpClassFile->Core.pVM);
1090 rc = VMR3SetError(pUVM, rc, RT_SRC_POS,
1091 N_("Failed to create I/O manager for VM due to an unknown error"));
1092 }
1093 }
1094 }
1095
1096 if (RT_FAILURE(rc))
1097 RTFileClose(pEpFile->hFile);
1098 }
1099
1100#ifdef VBOX_WITH_STATISTICS
1101 if (RT_SUCCESS(rc))
1102 {
1103 STAMR3RegisterF(pEpClassFile->Core.pVM, &pEpFile->StatRead,
1104 STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS,
1105 STAMUNIT_TICKS_PER_CALL, "Time taken to read from the endpoint",
1106 "/PDM/AsyncCompletion/File/%s/%d/Read", RTPathFilename(pEpFile->Core.pszUri), pEpFile->Core.iStatId);
1107
1108 STAMR3RegisterF(pEpClassFile->Core.pVM, &pEpFile->StatWrite,
1109 STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS,
1110 STAMUNIT_TICKS_PER_CALL, "Time taken to write to the endpoint",
1111 "/PDM/AsyncCompletion/File/%s/%d/Write", RTPathFilename(pEpFile->Core.pszUri), pEpFile->Core.iStatId);
1112 }
1113#endif
1114
1115 if (RT_SUCCESS(rc))
1116 LogRel(("AIOMgr: Endpoint for file '%s' (flags %08x) created successfully\n", pszUri, pEpFile->fFlags));
1117
1118 return rc;
1119}
1120
1121static DECLCALLBACK(int) pdmacFileEpRangesLockedDestroy(PAVLRFOFFNODECORE pNode, void *pvUser)
1122{
1123 NOREF(pNode); NOREF(pvUser);
1124 AssertMsgFailed(("The locked ranges tree should be empty at that point\n"));
1125 return VINF_SUCCESS;
1126}
1127
1128static DECLCALLBACK(int) pdmacFileEpClose(PPDMASYNCCOMPLETIONENDPOINT pEndpoint)
1129{
1130 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1131 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->pEpClass;
1132
1133 /* Make sure that all tasks finished for this endpoint. */
1134 int rc = pdmacFileAioMgrCloseEndpoint(pEpFile->pAioMgr, pEpFile);
1135 AssertRC(rc);
1136
1137 /*
1138 * If the async I/O manager is in failsafe mode this is the only endpoint
1139 * he processes and thus can be destroyed now.
1140 */
1141 if (pEpFile->pAioMgr->enmMgrType == PDMACEPFILEMGRTYPE_SIMPLE)
1142 pdmacFileAioMgrDestroy(pEpClassFile, pEpFile->pAioMgr);
1143
1144 /* Free cached tasks. */
1145 PPDMACTASKFILE pTask = pEpFile->pTasksFreeHead;
1146
1147 while (pTask)
1148 {
1149 PPDMACTASKFILE pTaskFree = pTask;
1150 pTask = pTask->pNext;
1151 MMR3HeapFree(pTaskFree);
1152 }
1153
1154 /* Destroy the locked ranges tree now. */
1155 RTAvlrFileOffsetDestroy(pEpFile->AioMgr.pTreeRangesLocked, pdmacFileEpRangesLockedDestroy, NULL);
1156 RTMemFree(pEpFile->AioMgr.pTreeRangesLocked);
1157 pEpFile->AioMgr.pTreeRangesLocked = NULL;
1158
1159 RTFileClose(pEpFile->hFile);
1160
1161#ifdef VBOX_WITH_STATISTICS
1162 /* Not sure if this might be unnecessary because of similar statement in pdmR3AsyncCompletionStatisticsDeregister? */
1163 STAMR3DeregisterF(pEpClassFile->Core.pVM->pUVM, "/PDM/AsyncCompletion/File/%s/*", RTPathFilename(pEpFile->Core.pszUri));
1164#endif
1165
1166 return VINF_SUCCESS;
1167}
1168
1169static DECLCALLBACK(int) pdmacFileEpRead(PPDMASYNCCOMPLETIONTASK pTask,
1170 PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
1171 PCRTSGSEG paSegments, size_t cSegments,
1172 size_t cbRead)
1173{
1174 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1175
1176 LogFlowFunc(("pTask=%#p pEndpoint=%#p off=%RTfoff paSegments=%#p cSegments=%zu cbRead=%zu\n",
1177 pTask, pEndpoint, off, paSegments, cSegments, cbRead));
1178
1179 if (RT_UNLIKELY((uint64_t)off + cbRead > pEpFile->cbFile))
1180 return VERR_EOF;
1181
1182 STAM_PROFILE_ADV_START(&pEpFile->StatRead, Read);
1183 pdmacFileEpTaskInit(pTask, cbRead);
1184 int rc = pdmacFileEpTaskInitiate(pTask, pEndpoint, off, paSegments, cSegments, cbRead,
1185 PDMACTASKFILETRANSFER_READ);
1186 STAM_PROFILE_ADV_STOP(&pEpFile->StatRead, Read);
1187
1188 return rc;
1189}
1190
1191static DECLCALLBACK(int) pdmacFileEpWrite(PPDMASYNCCOMPLETIONTASK pTask,
1192 PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
1193 PCRTSGSEG paSegments, size_t cSegments,
1194 size_t cbWrite)
1195{
1196 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1197
1198 if (RT_UNLIKELY(pEpFile->fReadonly))
1199 return VERR_NOT_SUPPORTED;
1200
1201 STAM_PROFILE_ADV_START(&pEpFile->StatWrite, Write);
1202
1203 pdmacFileEpTaskInit(pTask, cbWrite);
1204
1205 int rc = pdmacFileEpTaskInitiate(pTask, pEndpoint, off, paSegments, cSegments, cbWrite,
1206 PDMACTASKFILETRANSFER_WRITE);
1207
1208 STAM_PROFILE_ADV_STOP(&pEpFile->StatWrite, Write);
1209
1210 return rc;
1211}
1212
1213static DECLCALLBACK(int) pdmacFileEpFlush(PPDMASYNCCOMPLETIONTASK pTask,
1214 PPDMASYNCCOMPLETIONENDPOINT pEndpoint)
1215{
1216 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1217 PPDMASYNCCOMPLETIONTASKFILE pTaskFile = (PPDMASYNCCOMPLETIONTASKFILE)pTask;
1218
1219 if (RT_UNLIKELY(pEpFile->fReadonly))
1220 return VERR_NOT_SUPPORTED;
1221
1222 pdmacFileEpTaskInit(pTask, 0);
1223
1224 PPDMACTASKFILE pIoTask = pdmacFileTaskAlloc(pEpFile);
1225 if (RT_UNLIKELY(!pIoTask))
1226 return VERR_NO_MEMORY;
1227
1228 pIoTask->pEndpoint = pEpFile;
1229 pIoTask->enmTransferType = PDMACTASKFILETRANSFER_FLUSH;
1230 pIoTask->pvUser = pTaskFile;
1231 pIoTask->pfnCompleted = pdmacFileEpTaskCompleted;
1232 pdmacFileEpAddTask(pEpFile, pIoTask);
1233
1234 return VINF_AIO_TASK_PENDING;
1235}
1236
1237static DECLCALLBACK(int) pdmacFileEpGetSize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, uint64_t *pcbSize)
1238{
1239 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1240
1241 *pcbSize = ASMAtomicReadU64(&pEpFile->cbFile);
1242
1243 return VINF_SUCCESS;
1244}
1245
1246static DECLCALLBACK(int) pdmacFileEpSetSize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, uint64_t cbSize)
1247{
1248 int rc;
1249 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1250
1251 rc = RTFileSetSize(pEpFile->hFile, cbSize);
1252 if (RT_SUCCESS(rc))
1253 ASMAtomicWriteU64(&pEpFile->cbFile, cbSize);
1254
1255 return rc;
1256}
1257
1258const PDMASYNCCOMPLETIONEPCLASSOPS g_PDMAsyncCompletionEndpointClassFile =
1259{
1260 /* u32Version */
1261 PDMAC_EPCLASS_OPS_VERSION,
1262 /* pcszName */
1263 "File",
1264 /* enmClassType */
1265 PDMASYNCCOMPLETIONEPCLASSTYPE_FILE,
1266 /* cbEndpointClassGlobal */
1267 sizeof(PDMASYNCCOMPLETIONEPCLASSFILE),
1268 /* cbEndpoint */
1269 sizeof(PDMASYNCCOMPLETIONENDPOINTFILE),
1270 /* cbTask */
1271 sizeof(PDMASYNCCOMPLETIONTASKFILE),
1272 /* pfnInitialize */
1273 pdmacFileInitialize,
1274 /* pfnTerminate */
1275 pdmacFileTerminate,
1276 /* pfnEpInitialize. */
1277 pdmacFileEpInitialize,
1278 /* pfnEpClose */
1279 pdmacFileEpClose,
1280 /* pfnEpRead */
1281 pdmacFileEpRead,
1282 /* pfnEpWrite */
1283 pdmacFileEpWrite,
1284 /* pfnEpFlush */
1285 pdmacFileEpFlush,
1286 /* pfnEpGetSize */
1287 pdmacFileEpGetSize,
1288 /* pfnEpSetSize */
1289 pdmacFileEpSetSize,
1290 /* u32VersionEnd */
1291 PDMAC_EPCLASS_OPS_VERSION
1292};
1293
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use