VirtualBox

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

Last change on this file since 43667 was 43625, checked in by vboxsync, 12 years ago

Bug fixes

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

© 2023 Oracle
ContactPrivacy policyTerms of Use