VirtualBox

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

Last change on this file since 54752 was 54752, checked in by vboxsync, 9 years ago

Log cosmetics.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use