VirtualBox

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

Last change on this file since 55896 was 55383, checked in by vboxsync, 10 years ago

Log nit.

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

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