VirtualBox

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

Last change on this file since 36013 was 36001, checked in by vboxsync, 14 years ago

AsyncCompletion: Add a new flag to enable the host cache, to make using bandwidth groups possible for buffered I/O

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