VirtualBox

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

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

PCDBGCCMD & PFNDBGCCMD: Drop the return type & variable. Functions will be added separately from commands (superset of DBGCCMD).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 41.2 KB
Line 
1/* $Id: PDMAsyncCompletionFile.cpp 35696 2011-01-24 18:03:33Z 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 rc = RTSemEventCreate(&pAioMgrNew->EventSem);
435 if (RT_SUCCESS(rc))
436 {
437 rc = RTSemEventCreate(&pAioMgrNew->EventSemBlock);
438 if (RT_SUCCESS(rc))
439 {
440 rc = RTCritSectInit(&pAioMgrNew->CritSectBlockingEvent);
441 if (RT_SUCCESS(rc))
442 {
443 /* Init the rest of the manager. */
444 if (pAioMgrNew->enmMgrType != PDMACEPFILEMGRTYPE_SIMPLE)
445 rc = pdmacFileAioMgrNormalInit(pAioMgrNew);
446
447 if (RT_SUCCESS(rc))
448 {
449 pAioMgrNew->enmState = PDMACEPFILEMGRSTATE_RUNNING;
450
451 rc = RTThreadCreateF(&pAioMgrNew->Thread,
452 pAioMgrNew->enmMgrType == PDMACEPFILEMGRTYPE_SIMPLE
453 ? pdmacFileAioMgrFailsafe
454 : pdmacFileAioMgrNormal,
455 pAioMgrNew,
456 0,
457 RTTHREADTYPE_IO,
458 0,
459 "AioMgr%d-%s", pEpClass->cAioMgrs,
460 pAioMgrNew->enmMgrType == PDMACEPFILEMGRTYPE_SIMPLE
461 ? "F"
462 : "N");
463 if (RT_SUCCESS(rc))
464 {
465 /* Link it into the list. */
466 RTCritSectEnter(&pEpClass->CritSect);
467 pAioMgrNew->pNext = pEpClass->pAioMgrHead;
468 if (pEpClass->pAioMgrHead)
469 pEpClass->pAioMgrHead->pPrev = pAioMgrNew;
470 pEpClass->pAioMgrHead = pAioMgrNew;
471 pEpClass->cAioMgrs++;
472 RTCritSectLeave(&pEpClass->CritSect);
473
474 *ppAioMgr = pAioMgrNew;
475
476 Log(("PDMAC: Successfully created new file AIO Mgr {%s}\n", RTThreadGetName(pAioMgrNew->Thread)));
477 return VINF_SUCCESS;
478 }
479 pdmacFileAioMgrNormalDestroy(pAioMgrNew);
480 }
481 RTCritSectDelete(&pAioMgrNew->CritSectBlockingEvent);
482 }
483 RTSemEventDestroy(pAioMgrNew->EventSem);
484 }
485 RTSemEventDestroy(pAioMgrNew->EventSemBlock);
486 }
487 MMR3HeapFree(pAioMgrNew);
488 }
489
490 LogFlowFunc((": Leave rc=%Rrc\n", rc));
491
492 return rc;
493}
494
495/**
496 * Destroys a async I/O manager.
497 *
498 * @returns nothing.
499 * @param pAioMgr The async I/O manager to destroy.
500 */
501static void pdmacFileAioMgrDestroy(PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile, PPDMACEPFILEMGR pAioMgr)
502{
503 int rc = pdmacFileAioMgrShutdown(pAioMgr);
504 AssertRC(rc);
505
506 /* Unlink from the list. */
507 rc = RTCritSectEnter(&pEpClassFile->CritSect);
508 AssertRC(rc);
509
510 PPDMACEPFILEMGR pPrev = pAioMgr->pPrev;
511 PPDMACEPFILEMGR pNext = pAioMgr->pNext;
512
513 if (pPrev)
514 pPrev->pNext = pNext;
515 else
516 pEpClassFile->pAioMgrHead = pNext;
517
518 if (pNext)
519 pNext->pPrev = pPrev;
520
521 pEpClassFile->cAioMgrs--;
522 rc = RTCritSectLeave(&pEpClassFile->CritSect);
523 AssertRC(rc);
524
525 /* Free the resources. */
526 RTCritSectDelete(&pAioMgr->CritSectBlockingEvent);
527 RTSemEventDestroy(pAioMgr->EventSem);
528 if (pAioMgr->enmMgrType != PDMACEPFILEMGRTYPE_SIMPLE)
529 pdmacFileAioMgrNormalDestroy(pAioMgr);
530
531 MMR3HeapFree(pAioMgr);
532}
533
534static int pdmacFileMgrTypeFromName(const char *pszVal, PPDMACEPFILEMGRTYPE penmMgrType)
535{
536 int rc = VINF_SUCCESS;
537
538 if (!RTStrCmp(pszVal, "Simple"))
539 *penmMgrType = PDMACEPFILEMGRTYPE_SIMPLE;
540 else if (!RTStrCmp(pszVal, "Async"))
541 *penmMgrType = PDMACEPFILEMGRTYPE_ASYNC;
542 else
543 rc = VERR_CFGM_CONFIG_UNKNOWN_VALUE;
544
545 return rc;
546}
547
548static const char *pdmacFileMgrTypeToName(PDMACEPFILEMGRTYPE enmMgrType)
549{
550 if (enmMgrType == PDMACEPFILEMGRTYPE_SIMPLE)
551 return "Simple";
552 if (enmMgrType == PDMACEPFILEMGRTYPE_ASYNC)
553 return "Async";
554
555 return NULL;
556}
557
558static int pdmacFileBackendTypeFromName(const char *pszVal, PPDMACFILEEPBACKEND penmBackendType)
559{
560 int rc = VINF_SUCCESS;
561
562 if (!RTStrCmp(pszVal, "Buffered"))
563 *penmBackendType = PDMACFILEEPBACKEND_BUFFERED;
564 else if (!RTStrCmp(pszVal, "NonBuffered"))
565 *penmBackendType = PDMACFILEEPBACKEND_NON_BUFFERED;
566 else
567 rc = VERR_CFGM_CONFIG_UNKNOWN_VALUE;
568
569 return rc;
570}
571
572static const char *pdmacFileBackendTypeToName(PDMACFILEEPBACKEND enmBackendType)
573{
574 if (enmBackendType == PDMACFILEEPBACKEND_BUFFERED)
575 return "Buffered";
576 if (enmBackendType == PDMACFILEEPBACKEND_NON_BUFFERED)
577 return "NonBuffered";
578
579 return NULL;
580}
581
582/**
583 * Get the size of the given file.
584 * Works for block devices too.
585 *
586 * @returns VBox status code.
587 * @param hFile The file handle.
588 * @param pcbSize Where to store the size of the file on success.
589 */
590static int pdmacFileEpNativeGetSize(RTFILE hFile, uint64_t *pcbSize)
591{
592 int rc = VINF_SUCCESS;
593 uint64_t cbSize = 0;
594
595 rc = RTFileGetSize(hFile, &cbSize);
596 if (RT_SUCCESS(rc) && (cbSize != 0))
597 *pcbSize = cbSize;
598 else
599 {
600#ifdef RT_OS_WINDOWS
601 DISK_GEOMETRY DriveGeo;
602 DWORD cbDriveGeo;
603 if (DeviceIoControl((HANDLE)hFile,
604 IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0,
605 &DriveGeo, sizeof(DriveGeo), &cbDriveGeo, NULL))
606 {
607 if ( DriveGeo.MediaType == FixedMedia
608 || DriveGeo.MediaType == RemovableMedia)
609 {
610 cbSize = DriveGeo.Cylinders.QuadPart
611 * DriveGeo.TracksPerCylinder
612 * DriveGeo.SectorsPerTrack
613 * DriveGeo.BytesPerSector;
614
615 GET_LENGTH_INFORMATION DiskLenInfo;
616 DWORD junk;
617 if (DeviceIoControl((HANDLE)hFile,
618 IOCTL_DISK_GET_LENGTH_INFO, NULL, 0,
619 &DiskLenInfo, sizeof(DiskLenInfo), &junk, (LPOVERLAPPED)NULL))
620 {
621 /* IOCTL_DISK_GET_LENGTH_INFO is supported -- override cbSize. */
622 cbSize = DiskLenInfo.Length.QuadPart;
623 }
624
625 rc = VINF_SUCCESS;
626 }
627 else
628 {
629 rc = VERR_INVALID_PARAMETER;
630 }
631 }
632 else
633 {
634 rc = RTErrConvertFromWin32(GetLastError());
635 }
636#elif defined(RT_OS_DARWIN)
637 struct stat DevStat;
638 if (!fstat(hFile, &DevStat) && S_ISBLK(DevStat.st_mode))
639 {
640 uint64_t cBlocks;
641 uint32_t cbBlock;
642 if (!ioctl(hFile, DKIOCGETBLOCKCOUNT, &cBlocks))
643 {
644 if (!ioctl(hFile, DKIOCGETBLOCKSIZE, &cbBlock))
645 cbSize = cBlocks * cbBlock;
646 else
647 rc = RTErrConvertFromErrno(errno);
648 }
649 else
650 rc = RTErrConvertFromErrno(errno);
651 }
652 else
653 rc = VERR_INVALID_PARAMETER;
654#elif defined(RT_OS_SOLARIS)
655 struct stat DevStat;
656 if (!fstat(hFile, &DevStat) && ( S_ISBLK(DevStat.st_mode)
657 || S_ISCHR(DevStat.st_mode)))
658 {
659 struct dk_minfo mediainfo;
660 if (!ioctl(hFile, DKIOCGMEDIAINFO, &mediainfo))
661 cbSize = mediainfo.dki_capacity * mediainfo.dki_lbsize;
662 else
663 rc = RTErrConvertFromErrno(errno);
664 }
665 else
666 rc = VERR_INVALID_PARAMETER;
667#elif defined(RT_OS_FREEBSD)
668 struct stat DevStat;
669 if (!fstat(hFile, &DevStat) && S_ISCHR(DevStat.st_mode))
670 {
671 off_t cbMedia = 0;
672 if (!ioctl(hFile, DIOCGMEDIASIZE, &cbMedia))
673 {
674 cbSize = cbMedia;
675 }
676 else
677 rc = RTErrConvertFromErrno(errno);
678 }
679 else
680 rc = VERR_INVALID_PARAMETER;
681#else
682 /* Could be a block device */
683 rc = RTFileSeek(hFile, 0, RTFILE_SEEK_END, &cbSize);
684#endif
685
686 if (RT_SUCCESS(rc) && (cbSize != 0))
687 *pcbSize = cbSize;
688 else if (RT_SUCCESS(rc))
689 rc = VERR_NOT_SUPPORTED;
690 }
691
692 return rc;
693}
694
695#ifdef VBOX_WITH_DEBUGGER
696/**
697 * Error inject callback.
698 */
699static DECLCALLBACK(int) pdmacEpFileErrorInject(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR pArgs, unsigned cArgs)
700{
701 /*
702 * Validate input.
703 */
704 DBGC_CMDHLP_REQ_VM_RET(pCmdHlp, pCmd, pVM);
705 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, -1, cArgs == 3);
706 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 0, pArgs[0].enmType == DBGCVAR_TYPE_STRING);
707 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 1, pArgs[1].enmType == DBGCVAR_TYPE_STRING);
708 DBGC_CMDHLP_ASSERT_PARSER_RET(pCmdHlp, pCmd, 2, pArgs[2].enmType == DBGCVAR_TYPE_NUMBER);
709
710 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile;
711 pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pVM->pUVM->pdm.s.apAsyncCompletionEndpointClass[PDMASYNCCOMPLETIONEPCLASSTYPE_FILE];
712
713 /* Syntax is "read|write <filename> <status code>" */
714 bool fWrite;
715 if (!RTStrCmp(pArgs[0].u.pszString, "read"))
716 fWrite = false;
717 else if (!RTStrCmp(pArgs[0].u.pszString, "write"))
718 fWrite = true;
719 else
720 return DBGCCmdHlpFail(pCmdHlp, pCmd, "invalid transfer direction '%s'", pArgs[0].u.pszString);
721
722 int32_t rcToInject = (int32_t)pArgs[2].u.u64Number;
723 if ((uint64_t)rcToInject != pArgs[2].u.u64Number)
724 return DBGCCmdHlpFail(pCmdHlp, pCmd, "The status code '%lld' is out of range", pArgs[0].u.u64Number);
725
726
727 /*
728 * Search for the matching endpoint.
729 */
730 RTCritSectEnter(&pEpClassFile->Core.CritSect);
731
732 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEpClassFile->Core.pEndpointsHead;
733 while (pEpFile)
734 {
735 if (!RTStrCmp(pArgs[1].u.pszString, RTPathFilename(pEpFile->Core.pszUri)))
736 break;
737 pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEpFile->Core.pNext;
738 }
739 if (pEpFile)
740 {
741 /*
742 * Do the job.
743 */
744 if (fWrite)
745 ASMAtomicXchgS32(&pEpFile->rcReqWrite, rcToInject);
746 else
747 ASMAtomicXchgS32(&pEpFile->rcReqRead, rcToInject);
748
749 DBGCCmdHlpPrintf(pCmdHlp, "Injected %Rrc into '%s' for %s\n",
750 (int)rcToInject, pArgs[1].u.pszString, pArgs[0].u.pszString);
751 }
752
753 RTCritSectLeave(&pEpClassFile->Core.CritSect);
754
755 if (!pEpFile)
756 return DBGCCmdHlpFail(pCmdHlp, pCmd, "No file with name '%s' found", pArgs[1].u.pszString);
757 return VINF_SUCCESS;
758}
759#endif /* VBOX_WITH_DEBUGGER */
760
761static int pdmacFileInitialize(PPDMASYNCCOMPLETIONEPCLASS pClassGlobals, PCFGMNODE pCfgNode)
762{
763 int rc = VINF_SUCCESS;
764 RTFILEAIOLIMITS AioLimits; /** < Async I/O limitations. */
765
766 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pClassGlobals;
767
768 rc = RTFileAioGetLimits(&AioLimits);
769#ifdef DEBUG
770 if (RT_SUCCESS(rc) && RTEnvExist("VBOX_ASYNC_IO_FAILBACK"))
771 rc = VERR_ENV_VAR_NOT_FOUND;
772#endif
773 if (RT_FAILURE(rc))
774 {
775 LogRel(("AIO: Async I/O manager not supported (rc=%Rrc). Falling back to simple manager\n",
776 rc));
777 pEpClassFile->enmMgrTypeOverride = PDMACEPFILEMGRTYPE_SIMPLE;
778 pEpClassFile->enmEpBackendDefault = PDMACFILEEPBACKEND_BUFFERED;
779 }
780 else
781 {
782 pEpClassFile->uBitmaskAlignment = AioLimits.cbBufferAlignment ? ~((RTR3UINTPTR)AioLimits.cbBufferAlignment - 1) : RTR3UINTPTR_MAX;
783 pEpClassFile->cReqsOutstandingMax = AioLimits.cReqsOutstandingMax;
784
785 if (pCfgNode)
786 {
787 /* Query the default manager type */
788 char *pszVal = NULL;
789 rc = CFGMR3QueryStringAllocDef(pCfgNode, "IoMgr", &pszVal, "Async");
790 AssertLogRelRCReturn(rc, rc);
791
792 rc = pdmacFileMgrTypeFromName(pszVal, &pEpClassFile->enmMgrTypeOverride);
793 MMR3HeapFree(pszVal);
794 if (RT_FAILURE(rc))
795 return rc;
796
797 LogRel(("AIOMgr: Default manager type is \"%s\"\n", pdmacFileMgrTypeToName(pEpClassFile->enmMgrTypeOverride)));
798
799 /* Query default backend type */
800 rc = CFGMR3QueryStringAllocDef(pCfgNode, "FileBackend", &pszVal, "NonBuffered");
801 AssertLogRelRCReturn(rc, rc);
802
803 rc = pdmacFileBackendTypeFromName(pszVal, &pEpClassFile->enmEpBackendDefault);
804 MMR3HeapFree(pszVal);
805 if (RT_FAILURE(rc))
806 return rc;
807
808 LogRel(("AIOMgr: Default file backend is \"%s\"\n", pdmacFileBackendTypeToName(pEpClassFile->enmEpBackendDefault)));
809
810#ifdef RT_OS_LINUX
811 if ( pEpClassFile->enmMgrTypeOverride == PDMACEPFILEMGRTYPE_ASYNC
812 && pEpClassFile->enmEpBackendDefault == PDMACFILEEPBACKEND_BUFFERED)
813 {
814 LogRel(("AIOMgr: Linux does not support buffered async I/O, changing to non buffered\n"));
815 pEpClassFile->enmEpBackendDefault = PDMACFILEEPBACKEND_NON_BUFFERED;
816 }
817#endif
818 }
819 else
820 {
821 /* No configuration supplied, set defaults */
822 pEpClassFile->enmEpBackendDefault = PDMACFILEEPBACKEND_NON_BUFFERED;
823 pEpClassFile->enmMgrTypeOverride = PDMACEPFILEMGRTYPE_ASYNC;
824 }
825 }
826
827 /* Init critical section. */
828 rc = RTCritSectInit(&pEpClassFile->CritSect);
829
830#ifdef VBOX_WITH_DEBUGGER
831 /* Install the error injection handler. */
832 if (RT_SUCCESS(rc))
833 {
834 rc = DBGCRegisterCommands(&g_aCmds[0], RT_ELEMENTS(g_aCmds));
835 AssertRC(rc);
836 }
837#endif
838
839 return rc;
840}
841
842static void pdmacFileTerminate(PPDMASYNCCOMPLETIONEPCLASS pClassGlobals)
843{
844 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pClassGlobals;
845
846 /* All endpoints should be closed at this point. */
847 AssertMsg(!pEpClassFile->Core.pEndpointsHead, ("There are still endpoints left\n"));
848
849 /* Destroy all left async I/O managers. */
850 while (pEpClassFile->pAioMgrHead)
851 pdmacFileAioMgrDestroy(pEpClassFile, pEpClassFile->pAioMgrHead);
852
853 RTCritSectDelete(&pEpClassFile->CritSect);
854}
855
856static int pdmacFileEpInitialize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint,
857 const char *pszUri, uint32_t fFlags)
858{
859 int rc = VINF_SUCCESS;
860 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
861 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->pEpClass;
862 PDMACEPFILEMGRTYPE enmMgrType = pEpClassFile->enmMgrTypeOverride;
863 PDMACFILEEPBACKEND enmEpBackend = pEpClassFile->enmEpBackendDefault;
864
865 AssertMsgReturn((fFlags & ~(PDMACEP_FILE_FLAGS_READ_ONLY | PDMACEP_FILE_FLAGS_DONT_LOCK)) == 0,
866 ("PDMAsyncCompletion: Invalid flag specified\n"), VERR_INVALID_PARAMETER);
867
868 unsigned fFileFlags = RTFILE_O_OPEN;
869
870 if (fFlags & PDMACEP_FILE_FLAGS_READ_ONLY)
871 fFileFlags |= RTFILE_O_READ | RTFILE_O_DENY_NONE;
872 else
873 {
874 fFileFlags |= RTFILE_O_READWRITE;
875
876 /*
877 * Opened in read/write mode. Check whether the caller wants to
878 * avoid the lock. Return an error in case caching is enabled
879 * because this can lead to data corruption.
880 */
881 if (fFlags & PDMACEP_FILE_FLAGS_DONT_LOCK)
882 fFileFlags |= RTFILE_O_DENY_NONE;
883 else
884 fFileFlags |= RTFILE_O_DENY_WRITE;
885 }
886
887 if (enmMgrType == PDMACEPFILEMGRTYPE_ASYNC)
888 fFileFlags |= RTFILE_O_ASYNC_IO;
889
890 if (enmEpBackend == PDMACFILEEPBACKEND_NON_BUFFERED)
891 {
892 /*
893 * We only disable the cache if the size of the file is a multiple of 512.
894 * Certain hosts like Windows, Linux and Solaris require that transfer sizes
895 * are aligned to the volume sector size.
896 * If not we just make sure that the data is written to disk with RTFILE_O_WRITE_THROUGH
897 * which will trash the host cache but ensures that the host cache will not
898 * contain dirty buffers.
899 */
900 RTFILE File = NIL_RTFILE;
901
902 rc = RTFileOpen(&File, pszUri, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
903 if (RT_SUCCESS(rc))
904 {
905 uint64_t cbSize;
906
907 rc = pdmacFileEpNativeGetSize(File, &cbSize);
908 Assert(RT_FAILURE(rc) || cbSize != 0);
909
910 if (RT_SUCCESS(rc) && ((cbSize % 512) == 0))
911 fFileFlags |= RTFILE_O_NO_CACHE;
912 else
913 {
914 /* Downgrade to the buffered backend */
915 enmEpBackend = PDMACFILEEPBACKEND_BUFFERED;
916
917#ifdef RT_OS_LINUX
918 fFileFlags &= ~RTFILE_O_ASYNC_IO;
919 enmMgrType = PDMACEPFILEMGRTYPE_SIMPLE;
920#endif
921 }
922 RTFileClose(File);
923 }
924 }
925
926 /* Open with final flags. */
927 rc = RTFileOpen(&pEpFile->File, pszUri, fFileFlags);
928 if ((rc == VERR_INVALID_FUNCTION) || (rc == VERR_INVALID_PARAMETER))
929 {
930 LogRel(("pdmacFileEpInitialize: RTFileOpen %s / %08x failed with %Rrc\n",
931 pszUri, fFileFlags, rc));
932 /*
933 * Solaris doesn't support directio on ZFS so far. :-\
934 * Trying to enable it returns VERR_INVALID_FUNCTION
935 * (ENOTTY). Remove it and hope for the best.
936 * ZFS supports write throttling in case applications
937 * write more data than can be synced to the disk
938 * without blocking the whole application.
939 *
940 * On Linux we have the same problem with cifs.
941 * Have to disable async I/O here too because it requires O_DIRECT.
942 */
943 fFileFlags &= ~RTFILE_O_NO_CACHE;
944 enmEpBackend = PDMACFILEEPBACKEND_BUFFERED;
945
946#ifdef RT_OS_LINUX
947 fFileFlags &= ~RTFILE_O_ASYNC_IO;
948 enmMgrType = PDMACEPFILEMGRTYPE_SIMPLE;
949#endif
950
951 /* Open again. */
952 rc = RTFileOpen(&pEpFile->File, pszUri, fFileFlags);
953
954 if (RT_FAILURE(rc))
955 {
956 LogRel(("pdmacFileEpInitialize: RTFileOpen %s / %08x failed AGAIN(!) with %Rrc\n",
957 pszUri, fFileFlags, rc));
958 }
959 }
960
961 if (RT_SUCCESS(rc))
962 {
963 pEpFile->fFlags = fFileFlags;
964
965 rc = pdmacFileEpNativeGetSize(pEpFile->File, (uint64_t *)&pEpFile->cbFile);
966 Assert(RT_FAILURE(rc) || pEpFile->cbFile != 0);
967
968 if (RT_SUCCESS(rc))
969 {
970 /* Initialize the segment cache */
971 rc = MMR3HeapAllocZEx(pEpClassFile->Core.pVM, MM_TAG_PDM_ASYNC_COMPLETION,
972 sizeof(PDMACTASKFILE),
973 (void **)&pEpFile->pTasksFreeHead);
974 if (RT_SUCCESS(rc))
975 {
976 PPDMACEPFILEMGR pAioMgr = NULL;
977
978 pEpFile->pTasksFreeTail = pEpFile->pTasksFreeHead;
979 pEpFile->cTasksCached = 0;
980 pEpFile->enmBackendType = enmEpBackend;
981 /*
982 * Disable async flushes on Solaris for now.
983 * They cause weird hangs which needs more investigations.
984 */
985#ifndef RT_OS_SOLARIS
986 pEpFile->fAsyncFlushSupported = true;
987#else
988 pEpFile->fAsyncFlushSupported = false;
989#endif
990
991 if (enmMgrType == PDMACEPFILEMGRTYPE_SIMPLE)
992 {
993 /* Simple mode. Every file has its own async I/O manager. */
994 rc = pdmacFileAioMgrCreate(pEpClassFile, &pAioMgr, PDMACEPFILEMGRTYPE_SIMPLE);
995 AssertRC(rc);
996 }
997 else
998 {
999 pAioMgr = pEpClassFile->pAioMgrHead;
1000
1001 /* Check for an idling manager of the same type */
1002 while (pAioMgr)
1003 {
1004 if (pAioMgr->enmMgrType == enmMgrType)
1005 break;
1006 pAioMgr = pAioMgr->pNext;
1007 }
1008
1009 if (!pAioMgr)
1010 {
1011 rc = pdmacFileAioMgrCreate(pEpClassFile, &pAioMgr, enmMgrType);
1012 AssertRC(rc);
1013 }
1014 }
1015
1016 pEpFile->AioMgr.pTreeRangesLocked = (PAVLRFOFFTREE)RTMemAllocZ(sizeof(AVLRFOFFTREE));
1017 if (!pEpFile->AioMgr.pTreeRangesLocked)
1018 rc = VERR_NO_MEMORY;
1019 else
1020 {
1021 pEpFile->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE;
1022
1023 /* Assign the endpoint to the thread. */
1024 rc = pdmacFileAioMgrAddEndpoint(pAioMgr, pEpFile);
1025 if (RT_FAILURE(rc))
1026 {
1027 RTMemFree(pEpFile->AioMgr.pTreeRangesLocked);
1028 MMR3HeapFree(pEpFile->pTasksFreeHead);
1029 }
1030 }
1031 }
1032 }
1033
1034 if (RT_FAILURE(rc))
1035 RTFileClose(pEpFile->File);
1036 }
1037
1038#ifdef VBOX_WITH_STATISTICS
1039 if (RT_SUCCESS(rc))
1040 {
1041 STAMR3RegisterF(pEpClassFile->Core.pVM, &pEpFile->StatRead,
1042 STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS,
1043 STAMUNIT_TICKS_PER_CALL, "Time taken to read from the endpoint",
1044 "/PDM/AsyncCompletion/File/%s/Read", RTPathFilename(pEpFile->Core.pszUri));
1045
1046 STAMR3RegisterF(pEpClassFile->Core.pVM, &pEpFile->StatWrite,
1047 STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS,
1048 STAMUNIT_TICKS_PER_CALL, "Time taken to write to the endpoint",
1049 "/PDM/AsyncCompletion/File/%s/Write", RTPathFilename(pEpFile->Core.pszUri));
1050 }
1051#endif
1052
1053 if (RT_SUCCESS(rc))
1054 LogRel(("AIOMgr: Endpoint for file '%s' (flags %08x) created successfully\n", pszUri, pEpFile->fFlags));
1055
1056 return rc;
1057}
1058
1059static int pdmacFileEpRangesLockedDestroy(PAVLRFOFFNODECORE pNode, void *pvUser)
1060{
1061 AssertMsgFailed(("The locked ranges tree should be empty at that point\n"));
1062 return VINF_SUCCESS;
1063}
1064
1065static int pdmacFileEpClose(PPDMASYNCCOMPLETIONENDPOINT pEndpoint)
1066{
1067 int rc = VINF_SUCCESS;
1068 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1069 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->pEpClass;
1070
1071 /* Make sure that all tasks finished for this endpoint. */
1072 rc = pdmacFileAioMgrCloseEndpoint(pEpFile->pAioMgr, pEpFile);
1073 AssertRC(rc);
1074
1075 /*
1076 * If the async I/O manager is in failsafe mode this is the only endpoint
1077 * he processes and thus can be destroyed now.
1078 */
1079 if (pEpFile->pAioMgr->enmMgrType == PDMACEPFILEMGRTYPE_SIMPLE)
1080 pdmacFileAioMgrDestroy(pEpClassFile, pEpFile->pAioMgr);
1081
1082 /* Free cached tasks. */
1083 PPDMACTASKFILE pTask = pEpFile->pTasksFreeHead;
1084
1085 while (pTask)
1086 {
1087 PPDMACTASKFILE pTaskFree = pTask;
1088 pTask = pTask->pNext;
1089 MMR3HeapFree(pTaskFree);
1090 }
1091
1092 /* Destroy the locked ranges tree now. */
1093 RTAvlrFileOffsetDestroy(pEpFile->AioMgr.pTreeRangesLocked, pdmacFileEpRangesLockedDestroy, NULL);
1094
1095 RTFileClose(pEpFile->File);
1096
1097#ifdef VBOX_WITH_STATISTICS
1098 STAMR3Deregister(pEpClassFile->Core.pVM, &pEpFile->StatRead);
1099 STAMR3Deregister(pEpClassFile->Core.pVM, &pEpFile->StatWrite);
1100#endif
1101
1102 return VINF_SUCCESS;
1103}
1104
1105static int pdmacFileEpRead(PPDMASYNCCOMPLETIONTASK pTask,
1106 PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
1107 PCRTSGSEG paSegments, size_t cSegments,
1108 size_t cbRead)
1109{
1110 int rc = VINF_SUCCESS;
1111 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1112
1113 LogFlowFunc(("pTask=%#p pEndpoint=%#p off=%RTfoff paSegments=%#p cSegments=%zu cbRead=%zu\n",
1114 pTask, pEndpoint, off, paSegments, cSegments, cbRead));
1115
1116 STAM_PROFILE_ADV_START(&pEpFile->StatRead, Read);
1117
1118 pdmacFileEpTaskInit(pTask, cbRead);
1119
1120 rc = pdmacFileEpTaskInitiate(pTask, pEndpoint, off, paSegments, cSegments, cbRead,
1121 PDMACTASKFILETRANSFER_READ);
1122
1123 STAM_PROFILE_ADV_STOP(&pEpFile->StatRead, Read);
1124
1125 return rc;
1126}
1127
1128static int pdmacFileEpWrite(PPDMASYNCCOMPLETIONTASK pTask,
1129 PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
1130 PCRTSGSEG paSegments, size_t cSegments,
1131 size_t cbWrite)
1132{
1133 int rc = VINF_SUCCESS;
1134 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1135
1136 if (RT_UNLIKELY(pEpFile->fReadonly))
1137 return VERR_NOT_SUPPORTED;
1138
1139 STAM_PROFILE_ADV_START(&pEpFile->StatWrite, Write);
1140
1141 pdmacFileEpTaskInit(pTask, cbWrite);
1142
1143 rc = pdmacFileEpTaskInitiate(pTask, pEndpoint, off, paSegments, cSegments, cbWrite,
1144 PDMACTASKFILETRANSFER_WRITE);
1145
1146 STAM_PROFILE_ADV_STOP(&pEpFile->StatWrite, Write);
1147
1148 return rc;
1149}
1150
1151static int pdmacFileEpFlush(PPDMASYNCCOMPLETIONTASK pTask,
1152 PPDMASYNCCOMPLETIONENDPOINT pEndpoint)
1153{
1154 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1155 PPDMASYNCCOMPLETIONTASKFILE pTaskFile = (PPDMASYNCCOMPLETIONTASKFILE)pTask;
1156
1157 if (RT_UNLIKELY(pEpFile->fReadonly))
1158 return VERR_NOT_SUPPORTED;
1159
1160 pdmacFileEpTaskInit(pTask, 0);
1161
1162 PPDMACTASKFILE pIoTask = pdmacFileTaskAlloc(pEpFile);
1163 if (RT_UNLIKELY(!pIoTask))
1164 return VERR_NO_MEMORY;
1165
1166 pIoTask->pEndpoint = pEpFile;
1167 pIoTask->enmTransferType = PDMACTASKFILETRANSFER_FLUSH;
1168 pIoTask->pvUser = pTaskFile;
1169 pIoTask->pfnCompleted = pdmacFileEpTaskCompleted;
1170 pdmacFileEpAddTask(pEpFile, pIoTask);
1171
1172 return VINF_AIO_TASK_PENDING;
1173}
1174
1175static int pdmacFileEpGetSize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, uint64_t *pcbSize)
1176{
1177 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1178
1179 *pcbSize = ASMAtomicReadU64(&pEpFile->cbFile);
1180
1181 return VINF_SUCCESS;
1182}
1183
1184static int pdmacFileEpSetSize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, uint64_t cbSize)
1185{
1186 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1187
1188 ASMAtomicWriteU64(&pEpFile->cbFile, cbSize);
1189 return RTFileSetSize(pEpFile->File, cbSize);
1190}
1191
1192const PDMASYNCCOMPLETIONEPCLASSOPS g_PDMAsyncCompletionEndpointClassFile =
1193{
1194 /* u32Version */
1195 PDMAC_EPCLASS_OPS_VERSION,
1196 /* pcszName */
1197 "File",
1198 /* enmClassType */
1199 PDMASYNCCOMPLETIONEPCLASSTYPE_FILE,
1200 /* cbEndpointClassGlobal */
1201 sizeof(PDMASYNCCOMPLETIONEPCLASSFILE),
1202 /* cbEndpoint */
1203 sizeof(PDMASYNCCOMPLETIONENDPOINTFILE),
1204 /* cbTask */
1205 sizeof(PDMASYNCCOMPLETIONTASKFILE),
1206 /* pfnInitialize */
1207 pdmacFileInitialize,
1208 /* pfnTerminate */
1209 pdmacFileTerminate,
1210 /* pfnEpInitialize. */
1211 pdmacFileEpInitialize,
1212 /* pfnEpClose */
1213 pdmacFileEpClose,
1214 /* pfnEpRead */
1215 pdmacFileEpRead,
1216 /* pfnEpWrite */
1217 pdmacFileEpWrite,
1218 /* pfnEpFlush */
1219 pdmacFileEpFlush,
1220 /* pfnEpGetSize */
1221 pdmacFileEpGetSize,
1222 /* pfnEpSetSize */
1223 pdmacFileEpSetSize,
1224 /* u32VersionEnd */
1225 PDMAC_EPCLASS_OPS_VERSION
1226};
1227
Note: See TracBrowser for help on using the repository browser.

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