VirtualBox

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

最後變更 在這個檔案從57358是 57358,由 vboxsync 提交於 10 年 前

*: scm cleanup run.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 46.9 KB
 
1/* $Id: PDMAsyncCompletionFile.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
2/** @file
3 * PDM Async I/O - Transport data asynchronous in R3 using EMT.
4 */
5
6/*
7 * Copyright (C) 2006-2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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
301void 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 pAioMgr The async I/O manager to destroy.
512 */
513static void pdmacFileAioMgrDestroy(PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile, PPDMACEPFILEMGR pAioMgr)
514{
515 int rc = pdmacFileAioMgrShutdown(pAioMgr);
516 AssertRC(rc);
517
518 /* Unlink from the list. */
519 rc = RTCritSectEnter(&pEpClassFile->CritSect);
520 AssertRC(rc);
521
522 PPDMACEPFILEMGR pPrev = pAioMgr->pPrev;
523 PPDMACEPFILEMGR pNext = pAioMgr->pNext;
524
525 if (pPrev)
526 pPrev->pNext = pNext;
527 else
528 pEpClassFile->pAioMgrHead = pNext;
529
530 if (pNext)
531 pNext->pPrev = pPrev;
532
533 pEpClassFile->cAioMgrs--;
534 rc = RTCritSectLeave(&pEpClassFile->CritSect);
535 AssertRC(rc);
536
537 /* Free the resources. */
538 RTCritSectDelete(&pAioMgr->CritSectBlockingEvent);
539 RTSemEventDestroy(pAioMgr->EventSem);
540 if (pAioMgr->enmMgrType != PDMACEPFILEMGRTYPE_SIMPLE)
541 pdmacFileAioMgrNormalDestroy(pAioMgr);
542
543 MMR3HeapFree(pAioMgr);
544}
545
546static int pdmacFileMgrTypeFromName(const char *pszVal, PPDMACEPFILEMGRTYPE penmMgrType)
547{
548 int rc = VINF_SUCCESS;
549
550 if (!RTStrCmp(pszVal, "Simple"))
551 *penmMgrType = PDMACEPFILEMGRTYPE_SIMPLE;
552 else if (!RTStrCmp(pszVal, "Async"))
553 *penmMgrType = PDMACEPFILEMGRTYPE_ASYNC;
554 else
555 rc = VERR_CFGM_CONFIG_UNKNOWN_VALUE;
556
557 return rc;
558}
559
560static const char *pdmacFileMgrTypeToName(PDMACEPFILEMGRTYPE enmMgrType)
561{
562 if (enmMgrType == PDMACEPFILEMGRTYPE_SIMPLE)
563 return "Simple";
564 if (enmMgrType == PDMACEPFILEMGRTYPE_ASYNC)
565 return "Async";
566
567 return NULL;
568}
569
570static int pdmacFileBackendTypeFromName(const char *pszVal, PPDMACFILEEPBACKEND penmBackendType)
571{
572 int rc = VINF_SUCCESS;
573
574 if (!RTStrCmp(pszVal, "Buffered"))
575 *penmBackendType = PDMACFILEEPBACKEND_BUFFERED;
576 else if (!RTStrCmp(pszVal, "NonBuffered"))
577 *penmBackendType = PDMACFILEEPBACKEND_NON_BUFFERED;
578 else
579 rc = VERR_CFGM_CONFIG_UNKNOWN_VALUE;
580
581 return rc;
582}
583
584static const char *pdmacFileBackendTypeToName(PDMACFILEEPBACKEND enmBackendType)
585{
586 if (enmBackendType == PDMACFILEEPBACKEND_BUFFERED)
587 return "Buffered";
588 if (enmBackendType == PDMACFILEEPBACKEND_NON_BUFFERED)
589 return "NonBuffered";
590
591 return NULL;
592}
593
594#ifdef VBOX_WITH_DEBUGGER
595
596/**
597 * @callback_method_impl{FNDBGCCMD, The '.injecterror' command.}
598 */
599static DECLCALLBACK(int) pdmacEpFileErrorInject(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR pArgs, unsigned cArgs)
600{
601 /*
602 * Validate input.
603 */
604 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
605 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, -1, cArgs == 3);
606 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, pArgs[0].enmType == DBGCVAR_TYPE_STRING);
607 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 1, pArgs[1].enmType == DBGCVAR_TYPE_STRING);
608 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 2, pArgs[2].enmType == DBGCVAR_TYPE_NUMBER);
609
610 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile;
611 pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pUVM->pdm.s.apAsyncCompletionEndpointClass[PDMASYNCCOMPLETIONEPCLASSTYPE_FILE];
612
613 /* Syntax is "read|write <filename> <status code>" */
614 bool fWrite;
615 if (!RTStrCmp(pArgs[0].u.pszString, "read"))
616 fWrite = false;
617 else if (!RTStrCmp(pArgs[0].u.pszString, "write"))
618 fWrite = true;
619 else
620 return DBGCCmdHlpFail(pCmdHlp, pCmd, "invalid transfer direction '%s'", pArgs[0].u.pszString);
621
622 int32_t rcToInject = (int32_t)pArgs[2].u.u64Number;
623 if ((uint64_t)rcToInject != pArgs[2].u.u64Number)
624 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The status code '%lld' is out of range", pArgs[0].u.u64Number);
625
626 /*
627 * Search for the matching endpoint.
628 */
629 RTCritSectEnter(&pEpClassFile->Core.CritSect);
630
631 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEpClassFile->Core.pEndpointsHead;
632 while (pEpFile)
633 {
634 if (!RTStrCmp(pArgs[1].u.pszString, RTPathFilename(pEpFile->Core.pszUri)))
635 break;
636 pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEpFile->Core.pNext;
637 }
638
639 if (pEpFile)
640 {
641 /*
642 * Do the job.
643 */
644 if (fWrite)
645 ASMAtomicXchgS32(&pEpFile->rcReqWrite, rcToInject);
646 else
647 ASMAtomicXchgS32(&pEpFile->rcReqRead, rcToInject);
648
649 DBGCCmdHlpPrintf(pCmdHlp, "Injected %Rrc into '%s' for %s\n",
650 (int)rcToInject, pArgs[1].u.pszString, pArgs[0].u.pszString);
651 }
652
653 RTCritSectLeave(&pEpClassFile->Core.CritSect);
654
655 if (!pEpFile)
656 return DBGCCmdHlpFail(pCmdHlp, pCmd, "No file with name '%s' found", pArgs[1].u.pszString);
657 return VINF_SUCCESS;
658}
659
660# ifdef PDM_ASYNC_COMPLETION_FILE_WITH_DELAY
661/**
662 * @callback_method_impl{FNDBGCCMD, The '.injectdelay' command.}
663 */
664static DECLCALLBACK(int) pdmacEpFileDelayInject(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR pArgs, unsigned cArgs)
665{
666 /*
667 * Validate input.
668 */
669 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
670 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, -1, cArgs >= 3);
671 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, pArgs[0].enmType == DBGCVAR_TYPE_STRING);
672 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 1, pArgs[1].enmType == DBGCVAR_TYPE_STRING);
673 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 2, pArgs[2].enmType == DBGCVAR_TYPE_NUMBER);
674
675 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile;
676 pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pUVM->pdm.s.apAsyncCompletionEndpointClass[PDMASYNCCOMPLETIONEPCLASSTYPE_FILE];
677
678 /* Syntax is "read|write|flush|any <filename> <delay> [reqs]" */
679 PDMACFILEREQTYPEDELAY enmDelayType = PDMACFILEREQTYPEDELAY_ANY;
680 if (!RTStrCmp(pArgs[0].u.pszString, "read"))
681 enmDelayType = PDMACFILEREQTYPEDELAY_READ;
682 else if (!RTStrCmp(pArgs[0].u.pszString, "write"))
683 enmDelayType = PDMACFILEREQTYPEDELAY_WRITE;
684 else if (!RTStrCmp(pArgs[0].u.pszString, "flush"))
685 enmDelayType = PDMACFILEREQTYPEDELAY_FLUSH;
686 else if (!RTStrCmp(pArgs[0].u.pszString, "any"))
687 enmDelayType = PDMACFILEREQTYPEDELAY_ANY;
688 else
689 return DBGCCmdHlpFail(pCmdHlp, pCmd, "invalid transfer direction '%s'", pArgs[0].u.pszString);
690
691 uint32_t msDelay = (uint32_t)pArgs[2].u.u64Number;
692 if ((uint64_t)msDelay != pArgs[2].u.u64Number)
693 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The delay '%lld' is out of range", pArgs[0].u.u64Number);
694
695 uint32_t cReqsDelay = 1;
696 uint32_t msJitter = 0;
697 if (cArgs >= 4)
698 msJitter = (uint32_t)pArgs[3].u.u64Number;
699 if (cArgs == 5)
700 cReqsDelay = (uint32_t)pArgs[4].u.u64Number;
701
702 /*
703 * Search for the matching endpoint.
704 */
705 RTCritSectEnter(&pEpClassFile->Core.CritSect);
706
707 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEpClassFile->Core.pEndpointsHead;
708 while (pEpFile)
709 {
710 if (!RTStrCmp(pArgs[1].u.pszString, RTPathFilename(pEpFile->Core.pszUri)))
711 break;
712 pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEpFile->Core.pNext;
713 }
714
715 if (pEpFile)
716 {
717 ASMAtomicWriteSize(&pEpFile->enmTypeDelay, enmDelayType);
718 ASMAtomicWriteU32(&pEpFile->msDelay, msDelay);
719 ASMAtomicWriteU32(&pEpFile->msJitter, msJitter);
720 ASMAtomicWriteU32(&pEpFile->cReqsDelay, cReqsDelay);
721
722 DBGCCmdHlpPrintf(pCmdHlp, "Injected delay for the next %u requests of %u ms into '%s' for %s\n",
723 cReqsDelay, msDelay, pArgs[1].u.pszString, pArgs[0].u.pszString);
724 }
725
726 RTCritSectLeave(&pEpClassFile->Core.CritSect);
727
728 if (!pEpFile)
729 return DBGCCmdHlpFail(pCmdHlp, pCmd, "No file with name '%s' found", pArgs[1].u.pszString);
730 return VINF_SUCCESS;
731}
732
733static DECLCALLBACK(void) pdmacR3TimerCallback(PVM pVM, PTMTIMER pTimer, void *pvUser)
734{
735 uint64_t tsCur = RTTimeProgramMilliTS();
736 uint64_t cMilliesNext = UINT64_MAX;
737 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pvUser;
738
739 ASMAtomicWriteU64(&pEpClassFile->cMilliesNext, UINT64_MAX);
740
741 /* Go through all endpoints and check for expired requests. */
742 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEpClassFile->Core.pEndpointsHead;
743
744 while (pEpFile)
745 {
746 /* Check for an expired delay. */
747 if (pEpFile->pDelayedHead != NULL)
748 {
749 PPDMASYNCCOMPLETIONTASKFILE pTaskFile = ASMAtomicXchgPtrT(&pEpFile->pDelayedHead, NULL, PPDMASYNCCOMPLETIONTASKFILE);
750
751 while (pTaskFile)
752 {
753 PPDMASYNCCOMPLETIONTASKFILE pTmp = pTaskFile;
754 pTaskFile = pTaskFile->pDelayedNext;
755
756 if (tsCur >= pTmp->tsDelayEnd)
757 {
758 LogRel(("AIOMgr: Delayed request %#p completed\n", pTmp));
759 pdmR3AsyncCompletionCompleteTask(&pTmp->Core, pTmp->rc, true);
760 }
761 else
762 {
763 /* Prepend to the delayed list again. */
764 PPDMASYNCCOMPLETIONTASKFILE pHead = NULL;
765
766 if (pTmp->tsDelayEnd - tsCur < cMilliesNext)
767 cMilliesNext = pTmp->tsDelayEnd - tsCur;
768
769 do
770 {
771 pHead = ASMAtomicReadPtrT(&pEpFile->pDelayedHead, PPDMASYNCCOMPLETIONTASKFILE);
772 pTmp->pDelayedNext = pHead;
773 } while (!ASMAtomicCmpXchgPtr(&pEpFile->pDelayedHead, pTmp, pHead));
774 }
775 }
776 }
777
778 pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEpFile->Core.pNext;
779 }
780
781 if (cMilliesNext < pEpClassFile->cMilliesNext)
782 {
783 ASMAtomicWriteU64(&pEpClassFile->cMilliesNext, cMilliesNext);
784 TMTimerSetMillies(pEpClassFile->pTimer, cMilliesNext);
785 }
786}
787
788# endif /* PDM_ASYNC_COMPLETION_FILE_WITH_DELAY */
789
790#endif /* VBOX_WITH_DEBUGGER */
791
792static int pdmacFileInitialize(PPDMASYNCCOMPLETIONEPCLASS pClassGlobals, PCFGMNODE pCfgNode)
793{
794 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pClassGlobals;
795 RTFILEAIOLIMITS AioLimits; /** < Async I/O limitations. */
796
797 int rc = RTFileAioGetLimits(&AioLimits);
798#ifdef DEBUG
799 if (RT_SUCCESS(rc) && RTEnvExist("VBOX_ASYNC_IO_FAILBACK"))
800 rc = VERR_ENV_VAR_NOT_FOUND;
801#endif
802 if (RT_FAILURE(rc))
803 {
804 LogRel(("AIO: Async I/O manager not supported (rc=%Rrc). Falling back to simple manager\n", rc));
805 pEpClassFile->enmMgrTypeOverride = PDMACEPFILEMGRTYPE_SIMPLE;
806 pEpClassFile->enmEpBackendDefault = PDMACFILEEPBACKEND_BUFFERED;
807 }
808 else
809 {
810 pEpClassFile->uBitmaskAlignment = AioLimits.cbBufferAlignment ? ~((RTR3UINTPTR)AioLimits.cbBufferAlignment - 1) : RTR3UINTPTR_MAX;
811 pEpClassFile->cReqsOutstandingMax = AioLimits.cReqsOutstandingMax;
812
813 if (pCfgNode)
814 {
815 /* Query the default manager type */
816 char *pszVal = NULL;
817 rc = CFGMR3QueryStringAllocDef(pCfgNode, "IoMgr", &pszVal, "Async");
818 AssertLogRelRCReturn(rc, rc);
819
820 rc = pdmacFileMgrTypeFromName(pszVal, &pEpClassFile->enmMgrTypeOverride);
821 MMR3HeapFree(pszVal);
822 if (RT_FAILURE(rc))
823 return rc;
824
825 LogRel(("AIOMgr: Default manager type is '%s'\n", pdmacFileMgrTypeToName(pEpClassFile->enmMgrTypeOverride)));
826
827 /* Query default backend type */
828 rc = CFGMR3QueryStringAllocDef(pCfgNode, "FileBackend", &pszVal, "NonBuffered");
829 AssertLogRelRCReturn(rc, rc);
830
831 rc = pdmacFileBackendTypeFromName(pszVal, &pEpClassFile->enmEpBackendDefault);
832 MMR3HeapFree(pszVal);
833 if (RT_FAILURE(rc))
834 return rc;
835
836 LogRel(("AIOMgr: Default file backend is '%s'\n", pdmacFileBackendTypeToName(pEpClassFile->enmEpBackendDefault)));
837
838#ifdef RT_OS_LINUX
839 if ( pEpClassFile->enmMgrTypeOverride == PDMACEPFILEMGRTYPE_ASYNC
840 && pEpClassFile->enmEpBackendDefault == PDMACFILEEPBACKEND_BUFFERED)
841 {
842 LogRel(("AIOMgr: Linux does not support buffered async I/O, changing to non buffered\n"));
843 pEpClassFile->enmEpBackendDefault = PDMACFILEEPBACKEND_NON_BUFFERED;
844 }
845#endif
846 }
847 else
848 {
849 /* No configuration supplied, set defaults */
850 pEpClassFile->enmEpBackendDefault = PDMACFILEEPBACKEND_NON_BUFFERED;
851 pEpClassFile->enmMgrTypeOverride = PDMACEPFILEMGRTYPE_ASYNC;
852 }
853 }
854
855 /* Init critical section. */
856 rc = RTCritSectInit(&pEpClassFile->CritSect);
857
858#ifdef VBOX_WITH_DEBUGGER
859 /* Install the error injection handler. */
860 if (RT_SUCCESS(rc))
861 {
862 rc = DBGCRegisterCommands(&g_aCmds[0], RT_ELEMENTS(g_aCmds));
863 AssertRC(rc);
864 }
865
866#ifdef PDM_ASYNC_COMPLETION_FILE_WITH_DELAY
867 rc = TMR3TimerCreateInternal(pEpClassFile->Core.pVM, TMCLOCK_REAL, pdmacR3TimerCallback, pEpClassFile, "AC Delay", &pEpClassFile->pTimer);
868 AssertRC(rc);
869 pEpClassFile->cMilliesNext = UINT64_MAX;
870#endif
871#endif
872
873 return rc;
874}
875
876static void pdmacFileTerminate(PPDMASYNCCOMPLETIONEPCLASS pClassGlobals)
877{
878 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pClassGlobals;
879
880 /* All endpoints should be closed at this point. */
881 AssertMsg(!pEpClassFile->Core.pEndpointsHead, ("There are still endpoints left\n"));
882
883 /* Destroy all left async I/O managers. */
884 while (pEpClassFile->pAioMgrHead)
885 pdmacFileAioMgrDestroy(pEpClassFile, pEpClassFile->pAioMgrHead);
886
887 RTCritSectDelete(&pEpClassFile->CritSect);
888}
889
890static int pdmacFileEpInitialize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint,
891 const char *pszUri, uint32_t fFlags)
892{
893 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
894 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->pEpClass;
895 PDMACEPFILEMGRTYPE enmMgrType = pEpClassFile->enmMgrTypeOverride;
896 PDMACFILEEPBACKEND enmEpBackend = pEpClassFile->enmEpBackendDefault;
897
898 AssertMsgReturn((fFlags & ~(PDMACEP_FILE_FLAGS_READ_ONLY | PDMACEP_FILE_FLAGS_DONT_LOCK | PDMACEP_FILE_FLAGS_HOST_CACHE_ENABLED)) == 0,
899 ("PDMAsyncCompletion: Invalid flag specified\n"), VERR_INVALID_PARAMETER);
900
901 unsigned fFileFlags = RTFILE_O_OPEN;
902
903 /*
904 * Revert to the simple manager and the buffered backend if
905 * the host cache should be enabled.
906 */
907 if (fFlags & PDMACEP_FILE_FLAGS_HOST_CACHE_ENABLED)
908 {
909 enmMgrType = PDMACEPFILEMGRTYPE_SIMPLE;
910 enmEpBackend = PDMACFILEEPBACKEND_BUFFERED;
911 }
912
913 if (fFlags & PDMACEP_FILE_FLAGS_READ_ONLY)
914 fFileFlags |= RTFILE_O_READ | RTFILE_O_DENY_NONE;
915 else
916 {
917 fFileFlags |= RTFILE_O_READWRITE;
918
919 /*
920 * Opened in read/write mode. Check whether the caller wants to
921 * avoid the lock. Return an error in case caching is enabled
922 * because this can lead to data corruption.
923 */
924 if (fFlags & PDMACEP_FILE_FLAGS_DONT_LOCK)
925 fFileFlags |= RTFILE_O_DENY_NONE;
926 else
927 fFileFlags |= RTFILE_O_DENY_WRITE;
928 }
929
930 if (enmMgrType == PDMACEPFILEMGRTYPE_ASYNC)
931 fFileFlags |= RTFILE_O_ASYNC_IO;
932
933 int rc;
934 if (enmEpBackend == PDMACFILEEPBACKEND_NON_BUFFERED)
935 {
936 /*
937 * We only disable the cache if the size of the file is a multiple of 512.
938 * Certain hosts like Windows, Linux and Solaris require that transfer sizes
939 * are aligned to the volume sector size.
940 * If not we just make sure that the data is written to disk with RTFILE_O_WRITE_THROUGH
941 * which will trash the host cache but ensures that the host cache will not
942 * contain dirty buffers.
943 */
944 RTFILE hFile;
945 rc = RTFileOpen(&hFile, pszUri, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
946 if (RT_SUCCESS(rc))
947 {
948 uint64_t cbSize;
949
950 rc = RTFileGetSize(hFile, &cbSize);
951
952 if (RT_SUCCESS(rc) && ((cbSize % 512) == 0))
953 fFileFlags |= RTFILE_O_NO_CACHE;
954 else
955 {
956 /* Downgrade to the buffered backend */
957 enmEpBackend = PDMACFILEEPBACKEND_BUFFERED;
958
959#ifdef RT_OS_LINUX
960 fFileFlags &= ~RTFILE_O_ASYNC_IO;
961 enmMgrType = PDMACEPFILEMGRTYPE_SIMPLE;
962#endif
963 }
964 RTFileClose(hFile);
965 }
966 }
967
968 /* Open with final flags. */
969 rc = RTFileOpen(&pEpFile->hFile, pszUri, fFileFlags);
970 if ( rc == VERR_INVALID_FUNCTION
971 || rc == VERR_INVALID_PARAMETER)
972 {
973 LogRel(("AIOMgr: pdmacFileEpInitialize: RTFileOpen %s / %08x failed with %Rrc\n",
974 pszUri, fFileFlags, rc));
975 /*
976 * Solaris doesn't support directio on ZFS so far. :-\
977 * Trying to enable it returns VERR_INVALID_FUNCTION
978 * (ENOTTY). Remove it and hope for the best.
979 * ZFS supports write throttling in case applications
980 * write more data than can be synced to the disk
981 * without blocking the whole application.
982 *
983 * On Linux we have the same problem with cifs.
984 * Have to disable async I/O here too because it requires O_DIRECT.
985 */
986 fFileFlags &= ~RTFILE_O_NO_CACHE;
987 enmEpBackend = PDMACFILEEPBACKEND_BUFFERED;
988
989#ifdef RT_OS_LINUX
990 fFileFlags &= ~RTFILE_O_ASYNC_IO;
991 enmMgrType = PDMACEPFILEMGRTYPE_SIMPLE;
992#endif
993
994 /* Open again. */
995 rc = RTFileOpen(&pEpFile->hFile, pszUri, fFileFlags);
996
997 if (RT_FAILURE(rc))
998 {
999 LogRel(("AIOMgr: pdmacFileEpInitialize: RTFileOpen %s / %08x failed AGAIN(!) with %Rrc\n",
1000 pszUri, fFileFlags, rc));
1001 }
1002 }
1003
1004 if (RT_SUCCESS(rc))
1005 {
1006 pEpFile->fFlags = fFileFlags;
1007
1008 rc = RTFileGetSize(pEpFile->hFile, (uint64_t *)&pEpFile->cbFile);
1009 if (RT_SUCCESS(rc))
1010 {
1011 /* Initialize the segment cache */
1012 rc = MMR3HeapAllocZEx(pEpClassFile->Core.pVM, MM_TAG_PDM_ASYNC_COMPLETION,
1013 sizeof(PDMACTASKFILE),
1014 (void **)&pEpFile->pTasksFreeHead);
1015 if (RT_SUCCESS(rc))
1016 {
1017 PPDMACEPFILEMGR pAioMgr = NULL;
1018
1019 pEpFile->pTasksFreeTail = pEpFile->pTasksFreeHead;
1020 pEpFile->cTasksCached = 0;
1021 pEpFile->enmBackendType = enmEpBackend;
1022 /*
1023 * Disable async flushes on Solaris for now.
1024 * They cause weird hangs which needs more investigations.
1025 */
1026#ifndef RT_OS_SOLARIS
1027 pEpFile->fAsyncFlushSupported = true;
1028#else
1029 pEpFile->fAsyncFlushSupported = false;
1030#endif
1031
1032 if (enmMgrType == PDMACEPFILEMGRTYPE_SIMPLE)
1033 {
1034 /* Simple mode. Every file has its own async I/O manager. */
1035 rc = pdmacFileAioMgrCreate(pEpClassFile, &pAioMgr, PDMACEPFILEMGRTYPE_SIMPLE);
1036 }
1037 else
1038 {
1039 pAioMgr = pEpClassFile->pAioMgrHead;
1040
1041 /* Check for an idling manager of the same type */
1042 while (pAioMgr)
1043 {
1044 if (pAioMgr->enmMgrType == enmMgrType)
1045 break;
1046 pAioMgr = pAioMgr->pNext;
1047 }
1048
1049 if (!pAioMgr)
1050 rc = pdmacFileAioMgrCreate(pEpClassFile, &pAioMgr, enmMgrType);
1051 }
1052
1053 if (RT_SUCCESS(rc))
1054 {
1055 pEpFile->AioMgr.pTreeRangesLocked = (PAVLRFOFFTREE)RTMemAllocZ(sizeof(AVLRFOFFTREE));
1056 if (!pEpFile->AioMgr.pTreeRangesLocked)
1057 rc = VERR_NO_MEMORY;
1058 else
1059 {
1060 pEpFile->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE;
1061
1062 /* Assign the endpoint to the thread. */
1063 rc = pdmacFileAioMgrAddEndpoint(pAioMgr, pEpFile);
1064 if (RT_FAILURE(rc))
1065 {
1066 RTMemFree(pEpFile->AioMgr.pTreeRangesLocked);
1067 MMR3HeapFree(pEpFile->pTasksFreeHead);
1068 }
1069 }
1070 }
1071 else if (rc == VERR_FILE_AIO_INSUFFICIENT_EVENTS)
1072 {
1073 PUVM pUVM = VMR3GetUVM(pEpClassFile->Core.pVM);
1074#if defined(RT_OS_LINUX)
1075 rc = VMR3SetError(pUVM, rc, RT_SRC_POS,
1076 N_("Failed to create I/O manager for VM due to insufficient resources on the host. "
1077 "Either increase the amount of allowed events in /proc/sys/fs/aio-max-nr or enable "
1078 "the host I/O cache"));
1079#else
1080 rc = VMR3SetError(pUVM, rc, RT_SRC_POS,
1081 N_("Failed to create I/O manager for VM due to insufficient resources on the host. "
1082 "Enable the host I/O cache"));
1083#endif
1084 }
1085 else
1086 {
1087 PUVM pUVM = VMR3GetUVM(pEpClassFile->Core.pVM);
1088 rc = VMR3SetError(pUVM, rc, RT_SRC_POS,
1089 N_("Failed to create I/O manager for VM due to an unknown error"));
1090 }
1091 }
1092 }
1093
1094 if (RT_FAILURE(rc))
1095 RTFileClose(pEpFile->hFile);
1096 }
1097
1098#ifdef VBOX_WITH_STATISTICS
1099 if (RT_SUCCESS(rc))
1100 {
1101 STAMR3RegisterF(pEpClassFile->Core.pVM, &pEpFile->StatRead,
1102 STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS,
1103 STAMUNIT_TICKS_PER_CALL, "Time taken to read from the endpoint",
1104 "/PDM/AsyncCompletion/File/%s/Read", RTPathFilename(pEpFile->Core.pszUri));
1105
1106 STAMR3RegisterF(pEpClassFile->Core.pVM, &pEpFile->StatWrite,
1107 STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS,
1108 STAMUNIT_TICKS_PER_CALL, "Time taken to write to the endpoint",
1109 "/PDM/AsyncCompletion/File/%s/Write", RTPathFilename(pEpFile->Core.pszUri));
1110 }
1111#endif
1112
1113 if (RT_SUCCESS(rc))
1114 LogRel(("AIOMgr: Endpoint for file '%s' (flags %08x) created successfully\n", pszUri, pEpFile->fFlags));
1115
1116 return rc;
1117}
1118
1119static int pdmacFileEpRangesLockedDestroy(PAVLRFOFFNODECORE pNode, void *pvUser)
1120{
1121 NOREF(pNode); NOREF(pvUser);
1122 AssertMsgFailed(("The locked ranges tree should be empty at that point\n"));
1123 return VINF_SUCCESS;
1124}
1125
1126static int pdmacFileEpClose(PPDMASYNCCOMPLETIONENDPOINT pEndpoint)
1127{
1128 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1129 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->pEpClass;
1130
1131 /* Make sure that all tasks finished for this endpoint. */
1132 int rc = pdmacFileAioMgrCloseEndpoint(pEpFile->pAioMgr, pEpFile);
1133 AssertRC(rc);
1134
1135 /*
1136 * If the async I/O manager is in failsafe mode this is the only endpoint
1137 * he processes and thus can be destroyed now.
1138 */
1139 if (pEpFile->pAioMgr->enmMgrType == PDMACEPFILEMGRTYPE_SIMPLE)
1140 pdmacFileAioMgrDestroy(pEpClassFile, pEpFile->pAioMgr);
1141
1142 /* Free cached tasks. */
1143 PPDMACTASKFILE pTask = pEpFile->pTasksFreeHead;
1144
1145 while (pTask)
1146 {
1147 PPDMACTASKFILE pTaskFree = pTask;
1148 pTask = pTask->pNext;
1149 MMR3HeapFree(pTaskFree);
1150 }
1151
1152 /* Destroy the locked ranges tree now. */
1153 RTAvlrFileOffsetDestroy(pEpFile->AioMgr.pTreeRangesLocked, pdmacFileEpRangesLockedDestroy, NULL);
1154
1155 RTFileClose(pEpFile->hFile);
1156
1157#ifdef VBOX_WITH_STATISTICS
1158 /* Not sure if this might be unnecessary because of similar statement in pdmR3AsyncCompletionStatisticsDeregister? */
1159 STAMR3DeregisterF(pEpClassFile->Core.pVM->pUVM, "/PDM/AsyncCompletion/File/%s/*", RTPathFilename(pEpFile->Core.pszUri));
1160#endif
1161
1162 return VINF_SUCCESS;
1163}
1164
1165static int pdmacFileEpRead(PPDMASYNCCOMPLETIONTASK pTask,
1166 PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
1167 PCRTSGSEG paSegments, size_t cSegments,
1168 size_t cbRead)
1169{
1170 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1171
1172 LogFlowFunc(("pTask=%#p pEndpoint=%#p off=%RTfoff paSegments=%#p cSegments=%zu cbRead=%zu\n",
1173 pTask, pEndpoint, off, paSegments, cSegments, cbRead));
1174
1175 if (RT_UNLIKELY((uint64_t)off + cbRead > pEpFile->cbFile))
1176 return VERR_EOF;
1177
1178 STAM_PROFILE_ADV_START(&pEpFile->StatRead, Read);
1179 pdmacFileEpTaskInit(pTask, cbRead);
1180 int rc = pdmacFileEpTaskInitiate(pTask, pEndpoint, off, paSegments, cSegments, cbRead,
1181 PDMACTASKFILETRANSFER_READ);
1182 STAM_PROFILE_ADV_STOP(&pEpFile->StatRead, Read);
1183
1184 return rc;
1185}
1186
1187static int pdmacFileEpWrite(PPDMASYNCCOMPLETIONTASK pTask,
1188 PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
1189 PCRTSGSEG paSegments, size_t cSegments,
1190 size_t cbWrite)
1191{
1192 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1193
1194 if (RT_UNLIKELY(pEpFile->fReadonly))
1195 return VERR_NOT_SUPPORTED;
1196
1197 STAM_PROFILE_ADV_START(&pEpFile->StatWrite, Write);
1198
1199 pdmacFileEpTaskInit(pTask, cbWrite);
1200
1201 int rc = pdmacFileEpTaskInitiate(pTask, pEndpoint, off, paSegments, cSegments, cbWrite,
1202 PDMACTASKFILETRANSFER_WRITE);
1203
1204 STAM_PROFILE_ADV_STOP(&pEpFile->StatWrite, Write);
1205
1206 return rc;
1207}
1208
1209static int pdmacFileEpFlush(PPDMASYNCCOMPLETIONTASK pTask,
1210 PPDMASYNCCOMPLETIONENDPOINT pEndpoint)
1211{
1212 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1213 PPDMASYNCCOMPLETIONTASKFILE pTaskFile = (PPDMASYNCCOMPLETIONTASKFILE)pTask;
1214
1215 if (RT_UNLIKELY(pEpFile->fReadonly))
1216 return VERR_NOT_SUPPORTED;
1217
1218 pdmacFileEpTaskInit(pTask, 0);
1219
1220 PPDMACTASKFILE pIoTask = pdmacFileTaskAlloc(pEpFile);
1221 if (RT_UNLIKELY(!pIoTask))
1222 return VERR_NO_MEMORY;
1223
1224 pIoTask->pEndpoint = pEpFile;
1225 pIoTask->enmTransferType = PDMACTASKFILETRANSFER_FLUSH;
1226 pIoTask->pvUser = pTaskFile;
1227 pIoTask->pfnCompleted = pdmacFileEpTaskCompleted;
1228 pdmacFileEpAddTask(pEpFile, pIoTask);
1229
1230 return VINF_AIO_TASK_PENDING;
1231}
1232
1233static int pdmacFileEpGetSize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, uint64_t *pcbSize)
1234{
1235 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1236
1237 *pcbSize = ASMAtomicReadU64(&pEpFile->cbFile);
1238
1239 return VINF_SUCCESS;
1240}
1241
1242static int pdmacFileEpSetSize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, uint64_t cbSize)
1243{
1244 int rc;
1245 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1246
1247 rc = RTFileSetSize(pEpFile->hFile, cbSize);
1248 if (RT_SUCCESS(rc))
1249 ASMAtomicWriteU64(&pEpFile->cbFile, cbSize);
1250
1251 return rc;
1252}
1253
1254const PDMASYNCCOMPLETIONEPCLASSOPS g_PDMAsyncCompletionEndpointClassFile =
1255{
1256 /* u32Version */
1257 PDMAC_EPCLASS_OPS_VERSION,
1258 /* pcszName */
1259 "File",
1260 /* enmClassType */
1261 PDMASYNCCOMPLETIONEPCLASSTYPE_FILE,
1262 /* cbEndpointClassGlobal */
1263 sizeof(PDMASYNCCOMPLETIONEPCLASSFILE),
1264 /* cbEndpoint */
1265 sizeof(PDMASYNCCOMPLETIONENDPOINTFILE),
1266 /* cbTask */
1267 sizeof(PDMASYNCCOMPLETIONTASKFILE),
1268 /* pfnInitialize */
1269 pdmacFileInitialize,
1270 /* pfnTerminate */
1271 pdmacFileTerminate,
1272 /* pfnEpInitialize. */
1273 pdmacFileEpInitialize,
1274 /* pfnEpClose */
1275 pdmacFileEpClose,
1276 /* pfnEpRead */
1277 pdmacFileEpRead,
1278 /* pfnEpWrite */
1279 pdmacFileEpWrite,
1280 /* pfnEpFlush */
1281 pdmacFileEpFlush,
1282 /* pfnEpGetSize */
1283 pdmacFileEpGetSize,
1284 /* pfnEpSetSize */
1285 pdmacFileEpSetSize,
1286 /* u32VersionEnd */
1287 PDMAC_EPCLASS_OPS_VERSION
1288};
1289
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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