VirtualBox

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

Last change on this file since 27476 was 27474, checked in by vboxsync, 15 years ago

AsyncCompletion: Release log entry for every opened endpoint

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 38.2 KB
Line 
1/* $Id: PDMAsyncCompletionFile.cpp 27474 2010-03-18 11:01:13Z vboxsync $ */
2/** @file
3 * PDM Async I/O - Transport data asynchronous in R3 using EMT.
4 */
5
6/*
7 * Copyright (C) 2006-2009 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#define LOG_GROUP LOG_GROUP_PDM_ASYNC_COMPLETION
27//#define DEBUG
28#include "PDMInternal.h"
29#include <VBox/pdm.h>
30#include <VBox/mm.h>
31#include <VBox/vm.h>
32#include <VBox/err.h>
33#include <VBox/log.h>
34
35#include <iprt/asm.h>
36#include <iprt/assert.h>
37#include <iprt/critsect.h>
38#include <iprt/env.h>
39#include <iprt/file.h>
40#include <iprt/mem.h>
41#include <iprt/semaphore.h>
42#include <iprt/string.h>
43#include <iprt/thread.h>
44#include <iprt/path.h>
45
46#include "PDMAsyncCompletionFileInternal.h"
47
48/**
49 * Frees a task.
50 *
51 * @returns nothing.
52 * @param pEndpoint Pointer to the endpoint the segment was for.
53 * @param pTask The task to free.
54 */
55void pdmacFileTaskFree(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint,
56 PPDMACTASKFILE pTask)
57{
58 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClass = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->Core.pEpClass;
59
60 LogFlowFunc((": pEndpoint=%p pTask=%p\n", pEndpoint, pTask));
61
62 /* Try the per endpoint cache first. */
63 if (pEndpoint->cTasksCached < pEpClass->cTasksCacheMax)
64 {
65 /* Add it to the list. */
66 pEndpoint->pTasksFreeTail->pNext = pTask;
67 pEndpoint->pTasksFreeTail = pTask;
68 ASMAtomicIncU32(&pEndpoint->cTasksCached);
69 }
70 else if (false)
71 {
72 /* Bigger class cache */
73 }
74 else
75 {
76 Log(("Freeing task %p because all caches are full\n", pTask));
77 MMR3HeapFree(pTask);
78 }
79}
80
81/**
82 * Allocates a task segment
83 *
84 * @returns Pointer to the new task segment or NULL
85 * @param pEndpoint Pointer to the endpoint
86 */
87PPDMACTASKFILE pdmacFileTaskAlloc(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
88{
89 PPDMACTASKFILE pTask = NULL;
90
91 /* Try the small per endpoint cache first. */
92 if (pEndpoint->pTasksFreeHead == pEndpoint->pTasksFreeTail)
93 {
94 /* Try the bigger endpoint class cache. */
95 PPDMASYNCCOMPLETIONEPCLASSFILE pEndpointClass = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->Core.pEpClass;
96
97#if 0
98 /* We start with the assigned slot id to distribute the load when allocating new tasks. */
99 unsigned iSlot = pEndpoint->iSlotStart;
100 do
101 {
102 pTask = (PPDMASYNCCOMPLETIONTASK)ASMAtomicXchgPtr((void * volatile *)&pEndpointClass->apTaskCache[iSlot], NULL);
103 if (pTask)
104 break;
105
106 iSlot = (iSlot + 1) % RT_ELEMENTS(pEndpointClass->apTaskCache);
107 } while (iSlot != pEndpoint->iSlotStart);
108#endif
109 if (!pTask)
110 {
111 /*
112 * Allocate completely new.
113 * If this fails we return NULL.
114 */
115 int rc = MMR3HeapAllocZEx(pEndpointClass->Core.pVM, MM_TAG_PDM_ASYNC_COMPLETION,
116 sizeof(PDMACTASKFILE),
117 (void **)&pTask);
118 if (RT_FAILURE(rc))
119 pTask = NULL;
120
121 LogFlow(("Allocated task %p\n", pTask));
122 }
123#if 0
124 else
125 {
126 /* Remove the first element and put the rest into the slot again. */
127 PPDMASYNCCOMPLETIONTASK pTaskHeadNew = pTask->pNext;
128
129 pTaskHeadNew->pPrev = NULL;
130
131 /* Put back into the list adding any new tasks. */
132 while (true)
133 {
134 bool fChanged = ASMAtomicCmpXchgPtr((void * volatile *)&pEndpointClass->apTaskCache[iSlot], pTaskHeadNew, NULL);
135
136 if (fChanged)
137 break;
138
139 PPDMASYNCCOMPLETIONTASK pTaskHead = (PPDMASYNCCOMPLETIONTASK)ASMAtomicXchgPtr((void * volatile *)&pEndpointClass->apTaskCache[iSlot], NULL);
140
141 /* The new task could be taken inbetween */
142 if (pTaskHead)
143 {
144 /* Go to the end of the probably much shorter new list. */
145 PPDMASYNCCOMPLETIONTASK pTaskTail = pTaskHead;
146 while (pTaskTail->pNext)
147 pTaskTail = pTaskTail->pNext;
148
149 /* Concatenate */
150 pTaskTail->pNext = pTaskHeadNew;
151
152 pTaskHeadNew = pTaskHead;
153 }
154 /* Another round trying to change the list. */
155 }
156 /* We got a task from the global cache so decrement the counter */
157 ASMAtomicDecU32(&pEndpointClass->cTasksCached);
158 }
159#endif
160 }
161 else
162 {
163 /* Grab a free task from the head. */
164 AssertMsg(pEndpoint->cTasksCached > 0, ("No tasks cached but list contains more than one element\n"));
165
166 pTask = pEndpoint->pTasksFreeHead;
167 pEndpoint->pTasksFreeHead = pTask->pNext;
168 ASMAtomicDecU32(&pEndpoint->cTasksCached);
169 }
170
171 pTask->pNext = NULL;
172
173 return pTask;
174}
175
176PPDMACTASKFILE pdmacFileEpGetNewTasks(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
177{
178 PPDMACTASKFILE pTasks = NULL;
179
180 /*
181 * Get pending tasks.
182 */
183 pTasks = (PPDMACTASKFILE)ASMAtomicXchgPtr((void * volatile *)&pEndpoint->pTasksNewHead, NULL);
184
185 /* Reverse the list to process in FIFO order. */
186 if (pTasks)
187 {
188 PPDMACTASKFILE pTask = pTasks;
189
190 pTasks = NULL;
191
192 while (pTask)
193 {
194 PPDMACTASKFILE pCur = pTask;
195 pTask = pTask->pNext;
196 pCur->pNext = pTasks;
197 pTasks = pCur;
198 }
199 }
200
201 return pTasks;
202}
203
204static void pdmacFileAioMgrWakeup(PPDMACEPFILEMGR pAioMgr)
205{
206 bool fWokenUp = ASMAtomicXchgBool(&pAioMgr->fWokenUp, true);
207
208 if (!fWokenUp)
209 {
210 int rc = VINF_SUCCESS;
211 bool fWaitingEventSem = ASMAtomicReadBool(&pAioMgr->fWaitingEventSem);
212
213 if (fWaitingEventSem)
214 rc = RTSemEventSignal(pAioMgr->EventSem);
215
216 AssertRC(rc);
217 }
218}
219
220static int pdmacFileAioMgrWaitForBlockingEvent(PPDMACEPFILEMGR pAioMgr, PDMACEPFILEAIOMGRBLOCKINGEVENT enmEvent)
221{
222 int rc = VINF_SUCCESS;
223
224 ASMAtomicWriteU32((volatile uint32_t *)&pAioMgr->enmBlockingEvent, enmEvent);
225 Assert(!pAioMgr->fBlockingEventPending);
226 ASMAtomicXchgBool(&pAioMgr->fBlockingEventPending, true);
227
228 /* Wakeup the async I/O manager */
229 pdmacFileAioMgrWakeup(pAioMgr);
230
231 /* Wait for completion. */
232 rc = RTSemEventWait(pAioMgr->EventSemBlock, RT_INDEFINITE_WAIT);
233 AssertRC(rc);
234
235 ASMAtomicXchgBool(&pAioMgr->fBlockingEventPending, false);
236 ASMAtomicWriteU32((volatile uint32_t *)&pAioMgr->enmBlockingEvent, PDMACEPFILEAIOMGRBLOCKINGEVENT_INVALID);
237
238 return rc;
239}
240
241int pdmacFileAioMgrAddEndpoint(PPDMACEPFILEMGR pAioMgr, PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
242{
243 int rc;
244
245 rc = RTCritSectEnter(&pAioMgr->CritSectBlockingEvent);
246 AssertRCReturn(rc, rc);
247
248 ASMAtomicWritePtr((void * volatile *)&pAioMgr->BlockingEventData.AddEndpoint.pEndpoint, pEndpoint);
249 rc = pdmacFileAioMgrWaitForBlockingEvent(pAioMgr, PDMACEPFILEAIOMGRBLOCKINGEVENT_ADD_ENDPOINT);
250
251 RTCritSectLeave(&pAioMgr->CritSectBlockingEvent);
252
253 if (RT_SUCCESS(rc))
254 ASMAtomicWritePtr((void * volatile *)&pEndpoint->pAioMgr, pAioMgr);
255
256 return rc;
257}
258
259static int pdmacFileAioMgrRemoveEndpoint(PPDMACEPFILEMGR pAioMgr, PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
260{
261 int rc;
262
263 rc = RTCritSectEnter(&pAioMgr->CritSectBlockingEvent);
264 AssertRCReturn(rc, rc);
265
266 ASMAtomicWritePtr((void * volatile *)&pAioMgr->BlockingEventData.RemoveEndpoint.pEndpoint, pEndpoint);
267 rc = pdmacFileAioMgrWaitForBlockingEvent(pAioMgr, PDMACEPFILEAIOMGRBLOCKINGEVENT_REMOVE_ENDPOINT);
268
269 RTCritSectLeave(&pAioMgr->CritSectBlockingEvent);
270
271 return rc;
272}
273
274static int pdmacFileAioMgrCloseEndpoint(PPDMACEPFILEMGR pAioMgr, PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
275{
276 int rc;
277
278 rc = RTCritSectEnter(&pAioMgr->CritSectBlockingEvent);
279 AssertRCReturn(rc, rc);
280
281 ASMAtomicWritePtr((void * volatile *)&pAioMgr->BlockingEventData.CloseEndpoint.pEndpoint, pEndpoint);
282 rc = pdmacFileAioMgrWaitForBlockingEvent(pAioMgr, PDMACEPFILEAIOMGRBLOCKINGEVENT_CLOSE_ENDPOINT);
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((void * volatile *)&pEndpoint->pTasksNewHead, (void *)pTask, (void *)pNext));
311
312 pdmacFileAioMgrWakeup((PPDMACEPFILEMGR)ASMAtomicReadPtr((void * volatile *)&pEndpoint->pAioMgr));
313
314 return VINF_SUCCESS;
315}
316
317void pdmacFileEpTaskCompleted(PPDMACTASKFILE pTask, void *pvUser)
318{
319 PPDMASYNCCOMPLETIONTASKFILE pTaskFile = (PPDMASYNCCOMPLETIONTASKFILE)pvUser;
320
321 if (pTask->enmTransferType == PDMACTASKFILETRANSFER_FLUSH)
322 {
323 pdmR3AsyncCompletionCompleteTask(&pTaskFile->Core, true);
324 }
325 else
326 {
327 Assert((uint32_t)pTask->DataSeg.cbSeg == pTask->DataSeg.cbSeg && (int32_t)pTask->DataSeg.cbSeg >= 0);
328 uint32_t uOld = ASMAtomicSubS32(&pTaskFile->cbTransferLeft, (int32_t)pTask->DataSeg.cbSeg);
329
330 if (!(uOld - pTask->DataSeg.cbSeg)
331 && !ASMAtomicXchgBool(&pTaskFile->fCompleted, true))
332 pdmR3AsyncCompletionCompleteTask(&pTaskFile->Core, true);
333 }
334}
335
336int pdmacFileEpTaskInitiate(PPDMASYNCCOMPLETIONTASK pTask,
337 PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
338 PCPDMDATASEG paSegments, size_t cSegments,
339 size_t cbTransfer, PDMACTASKFILETRANSFER enmTransfer)
340{
341 int rc = VINF_SUCCESS;
342 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
343 PPDMASYNCCOMPLETIONTASKFILE pTaskFile = (PPDMASYNCCOMPLETIONTASKFILE)pTask;
344 PPDMACEPFILEMGR pAioMgr = pEpFile->pAioMgr;
345
346 Assert( (enmTransfer == PDMACTASKFILETRANSFER_READ)
347 || (enmTransfer == PDMACTASKFILETRANSFER_WRITE));
348
349 Assert((uint32_t)cbTransfer == cbTransfer && (int32_t)cbTransfer >= 0);
350 ASMAtomicWriteS32(&pTaskFile->cbTransferLeft, (int32_t)cbTransfer);
351 ASMAtomicWriteBool(&pTaskFile->fCompleted, false);
352
353 for (unsigned i = 0; i < cSegments; i++)
354 {
355 PPDMACTASKFILE pIoTask = pdmacFileTaskAlloc(pEpFile);
356 AssertPtr(pIoTask);
357
358 pIoTask->pEndpoint = pEpFile;
359 pIoTask->enmTransferType = enmTransfer;
360 pIoTask->Off = off;
361 pIoTask->DataSeg.cbSeg = paSegments[i].cbSeg;
362 pIoTask->DataSeg.pvSeg = paSegments[i].pvSeg;
363 pIoTask->pvUser = pTaskFile;
364 pIoTask->pfnCompleted = pdmacFileEpTaskCompleted;
365
366 /* Send it off to the I/O manager. */
367 pdmacFileEpAddTask(pEpFile, pIoTask);
368 off += paSegments[i].cbSeg;
369 cbTransfer -= paSegments[i].cbSeg;
370 }
371
372 AssertMsg(!cbTransfer, ("Incomplete transfer %u bytes left\n", cbTransfer));
373
374 if (ASMAtomicReadS32(&pTaskFile->cbTransferLeft) == 0
375 && !ASMAtomicXchgBool(&pTaskFile->fCompleted, true))
376 pdmR3AsyncCompletionCompleteTask(pTask, false);
377 else
378 rc = VINF_AIO_TASK_PENDING;
379
380 return rc;
381}
382
383/**
384 * Creates a new async I/O manager.
385 *
386 * @returns VBox status code.
387 * @param pEpClass Pointer to the endpoint class data.
388 * @param ppAioMgr Where to store the pointer to the new async I/O manager on success.
389 * @param enmMgrType Wanted manager type - can be overwritten by the global override.
390 */
391int pdmacFileAioMgrCreate(PPDMASYNCCOMPLETIONEPCLASSFILE pEpClass, PPPDMACEPFILEMGR ppAioMgr,
392 PDMACEPFILEMGRTYPE enmMgrType)
393{
394 int rc = VINF_SUCCESS;
395 PPDMACEPFILEMGR pAioMgrNew;
396
397 LogFlowFunc((": Entered\n"));
398
399 rc = MMR3HeapAllocZEx(pEpClass->Core.pVM, MM_TAG_PDM_ASYNC_COMPLETION, sizeof(PDMACEPFILEMGR), (void **)&pAioMgrNew);
400 if (RT_SUCCESS(rc))
401 {
402 if (enmMgrType < pEpClass->enmMgrTypeOverride)
403 pAioMgrNew->enmMgrType = enmMgrType;
404 else
405 pAioMgrNew->enmMgrType = pEpClass->enmMgrTypeOverride;
406
407 rc = RTSemEventCreate(&pAioMgrNew->EventSem);
408 if (RT_SUCCESS(rc))
409 {
410 rc = RTSemEventCreate(&pAioMgrNew->EventSemBlock);
411 if (RT_SUCCESS(rc))
412 {
413 rc = RTCritSectInit(&pAioMgrNew->CritSectBlockingEvent);
414 if (RT_SUCCESS(rc))
415 {
416 /* Init the rest of the manager. */
417 if (pAioMgrNew->enmMgrType != PDMACEPFILEMGRTYPE_SIMPLE)
418 rc = pdmacFileAioMgrNormalInit(pAioMgrNew);
419
420 if (RT_SUCCESS(rc))
421 {
422 pAioMgrNew->enmState = PDMACEPFILEMGRSTATE_RUNNING;
423
424 rc = RTThreadCreateF(&pAioMgrNew->Thread,
425 pAioMgrNew->enmMgrType == PDMACEPFILEMGRTYPE_SIMPLE
426 ? pdmacFileAioMgrFailsafe
427 : pdmacFileAioMgrNormal,
428 pAioMgrNew,
429 0,
430 RTTHREADTYPE_IO,
431 0,
432 "AioMgr%d-%s", pEpClass->cAioMgrs,
433 pAioMgrNew->enmMgrType == PDMACEPFILEMGRTYPE_SIMPLE
434 ? "F"
435 : "N");
436 if (RT_SUCCESS(rc))
437 {
438 /* Link it into the list. */
439 RTCritSectEnter(&pEpClass->CritSect);
440 pAioMgrNew->pNext = pEpClass->pAioMgrHead;
441 if (pEpClass->pAioMgrHead)
442 pEpClass->pAioMgrHead->pPrev = pAioMgrNew;
443 pEpClass->pAioMgrHead = pAioMgrNew;
444 pEpClass->cAioMgrs++;
445 RTCritSectLeave(&pEpClass->CritSect);
446
447 *ppAioMgr = pAioMgrNew;
448
449 Log(("PDMAC: Successfully created new file AIO Mgr {%s}\n", RTThreadGetName(pAioMgrNew->Thread)));
450 return VINF_SUCCESS;
451 }
452 pdmacFileAioMgrNormalDestroy(pAioMgrNew);
453 }
454 RTCritSectDelete(&pAioMgrNew->CritSectBlockingEvent);
455 }
456 RTSemEventDestroy(pAioMgrNew->EventSem);
457 }
458 RTSemEventDestroy(pAioMgrNew->EventSemBlock);
459 }
460 MMR3HeapFree(pAioMgrNew);
461 }
462
463 LogFlowFunc((": Leave rc=%Rrc\n", rc));
464
465 return rc;
466}
467
468/**
469 * Destroys a async I/O manager.
470 *
471 * @returns nothing.
472 * @param pAioMgr The async I/O manager to destroy.
473 */
474static void pdmacFileAioMgrDestroy(PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile, PPDMACEPFILEMGR pAioMgr)
475{
476 int rc = pdmacFileAioMgrShutdown(pAioMgr);
477 AssertRC(rc);
478
479 /* Unlink from the list. */
480 rc = RTCritSectEnter(&pEpClassFile->CritSect);
481 AssertRC(rc);
482
483 PPDMACEPFILEMGR pPrev = pAioMgr->pPrev;
484 PPDMACEPFILEMGR pNext = pAioMgr->pNext;
485
486 if (pPrev)
487 pPrev->pNext = pNext;
488 else
489 pEpClassFile->pAioMgrHead = pNext;
490
491 if (pNext)
492 pNext->pPrev = pPrev;
493
494 pEpClassFile->cAioMgrs--;
495 rc = RTCritSectLeave(&pEpClassFile->CritSect);
496 AssertRC(rc);
497
498 /* Free the ressources. */
499 RTCritSectDelete(&pAioMgr->CritSectBlockingEvent);
500 RTSemEventDestroy(pAioMgr->EventSem);
501 if (pAioMgr->enmMgrType != PDMACEPFILEMGRTYPE_SIMPLE)
502 pdmacFileAioMgrNormalDestroy(pAioMgr);
503
504 MMR3HeapFree(pAioMgr);
505}
506
507static int pdmacFileBwMgrInitialize(PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile,
508 PCFGMNODE pCfgNode, PPPDMACFILEBWMGR ppBwMgr)
509{
510 int rc = VINF_SUCCESS;
511 PPDMACFILEBWMGR pBwMgr = NULL;
512
513 rc = MMR3HeapAllocZEx(pEpClassFile->Core.pVM, MM_TAG_PDM_ASYNC_COMPLETION,
514 sizeof(PDMACFILEBWMGR),
515 (void **)&pBwMgr);
516 if (RT_SUCCESS(rc))
517 {
518 /* Init I/O flow control. */
519 rc = CFGMR3QueryU32Def(pCfgNode, "VMTransferPerSecMax", &pBwMgr->cbVMTransferPerSecMax, UINT32_MAX);
520 AssertLogRelRCReturn(rc, rc);
521 rc = CFGMR3QueryU32Def(pCfgNode, "VMTransferPerSecStart", &pBwMgr->cbVMTransferPerSecStart, 5 * _1M);
522 AssertLogRelRCReturn(rc, rc);
523 rc = CFGMR3QueryU32Def(pCfgNode, "VMTransferPerSecStep", &pBwMgr->cbVMTransferPerSecStep, _1M);
524 AssertLogRelRCReturn(rc, rc);
525
526 pBwMgr->cbVMTransferAllowed = pBwMgr->cbVMTransferPerSecStart;
527 pBwMgr->tsUpdatedLast = RTTimeSystemNanoTS();
528
529 if (pBwMgr->cbVMTransferPerSecMax != UINT32_MAX)
530 LogRel(("AIOMgr: I/O bandwidth limited to %u bytes/sec\n", pBwMgr->cbVMTransferPerSecMax));
531 else
532 LogRel(("AIOMgr: I/O bandwidth not limited\n"));
533
534 *ppBwMgr = pBwMgr;
535 }
536
537 return rc;
538}
539
540static void pdmacFileBwMgrDestroy(PPDMACFILEBWMGR pBwMgr)
541{
542 MMR3HeapFree(pBwMgr);
543}
544
545static void pdmacFileBwRef(PPDMACFILEBWMGR pBwMgr)
546{
547 pBwMgr->cRefs++;
548}
549
550static void pdmacFileBwUnref(PPDMACFILEBWMGR pBwMgr)
551{
552 Assert(pBwMgr->cRefs > 0);
553 pBwMgr->cRefs--;
554}
555
556bool pdmacFileBwMgrIsTransferAllowed(PPDMACFILEBWMGR pBwMgr, uint32_t cbTransfer)
557{
558 bool fAllowed = false;
559
560 LogFlowFunc(("pBwMgr=%p cbTransfer=%u\n", pBwMgr, cbTransfer));
561
562 uint32_t cbOld = ASMAtomicSubU32(&pBwMgr->cbVMTransferAllowed, cbTransfer);
563 if (RT_LIKELY(cbOld >= cbTransfer))
564 fAllowed = true;
565 else
566 {
567 /* We are out of ressources Check if we can update again. */
568 uint64_t tsNow = RTTimeSystemNanoTS();
569 uint64_t tsUpdatedLast = ASMAtomicUoReadU64(&pBwMgr->tsUpdatedLast);
570
571 if (tsNow - tsUpdatedLast >= (1000*1000*1000))
572 {
573 if (ASMAtomicCmpXchgU64(&pBwMgr->tsUpdatedLast, tsNow, tsUpdatedLast))
574 {
575 if (pBwMgr->cbVMTransferPerSecStart < pBwMgr->cbVMTransferPerSecMax)
576 {
577 pBwMgr->cbVMTransferPerSecStart = RT_MIN(pBwMgr->cbVMTransferPerSecMax, pBwMgr->cbVMTransferPerSecStart + pBwMgr->cbVMTransferPerSecStep);
578 LogFlow(("AIOMgr: Increasing maximum bandwidth to %u bytes/sec\n", pBwMgr->cbVMTransferPerSecStart));
579 }
580
581 /* Update */
582 ASMAtomicWriteU32(&pBwMgr->cbVMTransferAllowed, pBwMgr->cbVMTransferPerSecStart - cbTransfer);
583 fAllowed = true;
584 LogFlow(("AIOMgr: Refreshed bandwidth\n"));
585 }
586 }
587 else
588 ASMAtomicAddU32(&pBwMgr->cbVMTransferAllowed, cbTransfer);
589 }
590
591 LogFlowFunc(("fAllowed=%RTbool\n", fAllowed));
592
593 return fAllowed;
594}
595
596static int pdmacFileMgrTypeFromName(const char *pszVal, PPDMACEPFILEMGRTYPE penmMgrType)
597{
598 int rc = VINF_SUCCESS;
599
600 if (!RTStrCmp(pszVal, "Simple"))
601 *penmMgrType = PDMACEPFILEMGRTYPE_SIMPLE;
602 else if (!RTStrCmp(pszVal, "Async"))
603 *penmMgrType = PDMACEPFILEMGRTYPE_ASYNC;
604 else
605 rc = VERR_CFGM_CONFIG_UNKNOWN_VALUE;
606
607 return rc;
608}
609
610static const char *pdmacFileMgrTypeToName(PDMACEPFILEMGRTYPE enmMgrType)
611{
612 if (enmMgrType == PDMACEPFILEMGRTYPE_SIMPLE)
613 return "Simple";
614 if (enmMgrType == PDMACEPFILEMGRTYPE_ASYNC)
615 return "Async";
616
617 return NULL;
618}
619
620static int pdmacFileBackendTypeFromName(const char *pszVal, PPDMACFILEEPBACKEND penmBackendType)
621{
622 int rc = VINF_SUCCESS;
623
624 if (!RTStrCmp(pszVal, "Buffered"))
625 *penmBackendType = PDMACFILEEPBACKEND_BUFFERED;
626 else if (!RTStrCmp(pszVal, "NonBuffered"))
627 *penmBackendType = PDMACFILEEPBACKEND_NON_BUFFERED;
628 else
629 rc = VERR_CFGM_CONFIG_UNKNOWN_VALUE;
630
631 return rc;
632}
633
634static const char *pdmacFileBackendTypeToName(PDMACFILEEPBACKEND enmBackendType)
635{
636 if (enmBackendType == PDMACFILEEPBACKEND_BUFFERED)
637 return "Buffered";
638 if (enmBackendType == PDMACFILEEPBACKEND_NON_BUFFERED)
639 return "NonBuffered";
640
641 return NULL;
642}
643
644static int pdmacFileInitialize(PPDMASYNCCOMPLETIONEPCLASS pClassGlobals, PCFGMNODE pCfgNode)
645{
646 int rc = VINF_SUCCESS;
647 RTFILEAIOLIMITS AioLimits; /** < Async I/O limitations. */
648
649 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pClassGlobals;
650
651 rc = RTFileAioGetLimits(&AioLimits);
652#ifdef DEBUG
653 if (RT_SUCCESS(rc) && RTEnvExist("VBOX_ASYNC_IO_FAILBACK"))
654 rc = VERR_ENV_VAR_NOT_FOUND;
655#endif
656 if (RT_FAILURE(rc))
657 {
658 LogRel(("AIO: Async I/O manager not supported (rc=%Rrc). Falling back to simple manager\n",
659 rc));
660 pEpClassFile->enmMgrTypeOverride = PDMACEPFILEMGRTYPE_SIMPLE;
661 pEpClassFile->enmEpBackendDefault = PDMACFILEEPBACKEND_BUFFERED;
662 }
663 else
664 {
665 pEpClassFile->uBitmaskAlignment = AioLimits.cbBufferAlignment ? ~((RTR3UINTPTR)AioLimits.cbBufferAlignment - 1) : RTR3UINTPTR_MAX;
666 pEpClassFile->cReqsOutstandingMax = AioLimits.cReqsOutstandingMax;
667
668 if (pCfgNode)
669 {
670 /* Query the default manager type */
671 char *pszVal = NULL;
672 rc = CFGMR3QueryStringAllocDef(pCfgNode, "IoMgr", &pszVal, "Async");
673 AssertLogRelRCReturn(rc, rc);
674
675 rc = pdmacFileMgrTypeFromName(pszVal, &pEpClassFile->enmMgrTypeOverride);
676 MMR3HeapFree(pszVal);
677 if (RT_FAILURE(rc))
678 return rc;
679
680 LogRel(("AIOMgr: Default manager type is \"%s\"\n", pdmacFileMgrTypeToName(pEpClassFile->enmMgrTypeOverride)));
681
682 /* Query default backend type */
683#ifndef RT_OS_LINUX
684 rc = CFGMR3QueryStringAllocDef(pCfgNode, "FileBackend", &pszVal, "Buffered");
685#else /* Linux can't use buffered with async */
686 rc = CFGMR3QueryStringAllocDef(pCfgNode, "FileBackend", &pszVal, "NonBuffered");
687#endif
688 AssertLogRelRCReturn(rc, rc);
689
690 rc = pdmacFileBackendTypeFromName(pszVal, &pEpClassFile->enmEpBackendDefault);
691 MMR3HeapFree(pszVal);
692 if (RT_FAILURE(rc))
693 return rc;
694
695 LogRel(("AIOMgr: Default file backend is \"%s\"\n", pdmacFileBackendTypeToName(pEpClassFile->enmEpBackendDefault)));
696
697#ifdef RT_OS_LINUX
698 if ( pEpClassFile->enmMgrTypeOverride == PDMACEPFILEMGRTYPE_ASYNC
699 && pEpClassFile->enmEpBackendDefault == PDMACFILEEPBACKEND_BUFFERED)
700 {
701 LogRel(("AIOMgr: Linux does not support buffered async I/O, changing to non buffered\n"));
702 pEpClassFile->enmEpBackendDefault = PDMACFILEEPBACKEND_NON_BUFFERED;
703 }
704#endif
705 }
706 else
707 {
708 /* No configuration supplied, set defaults */
709 pEpClassFile->enmMgrTypeOverride = PDMACEPFILEMGRTYPE_ASYNC;
710#ifdef RT_OS_LINUX
711 pEpClassFile->enmEpBackendDefault = PDMACFILEEPBACKEND_NON_BUFFERED;
712#else
713 pEpClassFile->enmEpBackendDefault = PDMACFILEEPBACKEND_BUFFERED;
714#endif
715 }
716 }
717
718 /* Init critical section. */
719 rc = RTCritSectInit(&pEpClassFile->CritSect);
720 if (RT_SUCCESS(rc))
721 {
722 /* Check if the cache was disabled by the user. */
723 rc = CFGMR3QueryBoolDef(pCfgNode, "CacheEnabled", &pEpClassFile->fCacheEnabled, true);
724 AssertLogRelRCReturn(rc, rc);
725
726 if (pEpClassFile->fCacheEnabled)
727 {
728 /* Init cache structure */
729 rc = pdmacFileCacheInit(pEpClassFile, pCfgNode);
730 if (RT_FAILURE(rc))
731 {
732 pEpClassFile->fCacheEnabled = false;
733 LogRel(("AIOMgr: Failed to initialise the cache (rc=%Rrc), disabled caching\n"));
734 }
735 }
736 else
737 LogRel(("AIOMgr: Cache was globally disabled\n"));
738
739 rc = pdmacFileBwMgrInitialize(pEpClassFile, pCfgNode, &pEpClassFile->pBwMgr);
740 if (RT_FAILURE(rc))
741 RTCritSectDelete(&pEpClassFile->CritSect);
742 }
743
744 return rc;
745}
746
747static void pdmacFileTerminate(PPDMASYNCCOMPLETIONEPCLASS pClassGlobals)
748{
749 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pClassGlobals;
750
751 /* All endpoints should be closed at this point. */
752 AssertMsg(!pEpClassFile->Core.pEndpointsHead, ("There are still endpoints left\n"));
753
754 /* Destroy all left async I/O managers. */
755 while (pEpClassFile->pAioMgrHead)
756 pdmacFileAioMgrDestroy(pEpClassFile, pEpClassFile->pAioMgrHead);
757
758 /* Destroy the cache. */
759 if (pEpClassFile->fCacheEnabled)
760 pdmacFileCacheDestroy(pEpClassFile);
761
762 RTCritSectDelete(&pEpClassFile->CritSect);
763 pdmacFileBwMgrDestroy(pEpClassFile->pBwMgr);
764}
765
766static int pdmacFileEpInitialize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint,
767 const char *pszUri, uint32_t fFlags)
768{
769 int rc = VINF_SUCCESS;
770 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
771 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->pEpClass;
772 PDMACEPFILEMGRTYPE enmMgrType = pEpClassFile->enmMgrTypeOverride;
773 PDMACFILEEPBACKEND enmEpBackend = pEpClassFile->enmEpBackendDefault;
774
775 AssertMsgReturn((fFlags & ~(PDMACEP_FILE_FLAGS_READ_ONLY | PDMACEP_FILE_FLAGS_CACHING)) == 0,
776 ("PDMAsyncCompletion: Invalid flag specified\n"), VERR_INVALID_PARAMETER);
777
778 unsigned fFileFlags = fFlags & PDMACEP_FILE_FLAGS_READ_ONLY
779 ? RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE
780 : RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE;
781
782 if (enmEpBackend == PDMACFILEEPBACKEND_NON_BUFFERED)
783 {
784 /*
785 * We only disable the cache if the size of the file is a multiple of 512.
786 * Certain hosts like Windows, Linux and Solaris require that transfer sizes
787 * are aligned to the volume sector size.
788 * If not we just make sure that the data is written to disk with RTFILE_O_WRITE_THROUGH
789 * which will trash the host cache but ensures that the host cache will not
790 * contain dirty buffers.
791 */
792 RTFILE File = NIL_RTFILE;
793
794 rc = RTFileOpen(&File, pszUri, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
795 if (RT_SUCCESS(rc))
796 {
797 uint64_t cbSize;
798
799 rc = RTFileGetSize(File, &cbSize);
800 if (RT_SUCCESS(rc) && ((cbSize % 512) == 0))
801 fFileFlags |= RTFILE_O_NO_CACHE;
802 else
803 {
804 /* Downgrade to the buffered backend */
805 enmEpBackend = PDMACFILEEPBACKEND_BUFFERED;
806 }
807 RTFileClose(File);
808 }
809 }
810
811 /* Open with final flags. */
812 rc = RTFileOpen(&pEpFile->File, pszUri, fFileFlags);
813 if ((rc == VERR_INVALID_FUNCTION) || (rc == VERR_INVALID_PARAMETER))
814 {
815 LogRel(("pdmacFileEpInitialize: RTFileOpen %s / %08x failed with %Rrc\n",
816 pszUri, fFileFlags, rc));
817 /*
818 * Solaris doesn't support directio on ZFS so far. :-\
819 * Trying to enable it returns VERR_INVALID_FUNCTION
820 * (ENOTTY). Remove it and hope for the best.
821 * ZFS supports write throttling in case applications
822 * write more data than can be synced to the disk
823 * without blocking the whole application.
824 *
825 * On Linux we have the same problem with cifs.
826 * Have to disable async I/O here too because it requires O_DIRECT.
827 */
828 fFileFlags &= ~RTFILE_O_NO_CACHE;
829 enmEpBackend = PDMACFILEEPBACKEND_BUFFERED;
830
831#ifdef RT_OS_LINUX
832 fFileFlags &= ~RTFILE_O_ASYNC_IO;
833 enmMgrType = PDMACEPFILEMGRTYPE_SIMPLE;
834#endif
835
836 /* Open again. */
837 rc = RTFileOpen(&pEpFile->File, pszUri, fFileFlags);
838
839 if (RT_FAILURE(rc))
840 {
841 LogRel(("pdmacFileEpInitialize: RTFileOpen %s / %08x failed AGAIN(!) with %Rrc\n",
842 pszUri, fFileFlags, rc));
843 }
844 }
845
846 if (RT_SUCCESS(rc))
847 {
848 pEpFile->fFlags = fFileFlags;
849
850 rc = RTFileGetSize(pEpFile->File, (uint64_t *)&pEpFile->cbFile);
851 if (RT_SUCCESS(rc) && (pEpFile->cbFile == 0))
852 {
853 /* Could be a block device */
854 rc = RTFileSeek(pEpFile->File, 0, RTFILE_SEEK_END, (uint64_t *)&pEpFile->cbFile);
855 }
856
857 if (RT_SUCCESS(rc))
858 {
859 /* Initialize the segment cache */
860 rc = MMR3HeapAllocZEx(pEpClassFile->Core.pVM, MM_TAG_PDM_ASYNC_COMPLETION,
861 sizeof(PDMACTASKFILE),
862 (void **)&pEpFile->pTasksFreeHead);
863 if (RT_SUCCESS(rc))
864 {
865 PPDMACEPFILEMGR pAioMgr = NULL;
866
867 pEpFile->pTasksFreeTail = pEpFile->pTasksFreeHead;
868 pEpFile->cTasksCached = 0;
869 pEpFile->pBwMgr = pEpClassFile->pBwMgr;
870 pEpFile->enmBackendType = enmEpBackend;
871 pdmacFileBwRef(pEpFile->pBwMgr);
872
873 if (enmMgrType == PDMACEPFILEMGRTYPE_SIMPLE)
874 {
875 /* Simple mode. Every file has its own async I/O manager. */
876 rc = pdmacFileAioMgrCreate(pEpClassFile, &pAioMgr, PDMACEPFILEMGRTYPE_SIMPLE);
877 AssertRC(rc);
878 }
879 else
880 {
881 if ( (fFlags & PDMACEP_FILE_FLAGS_CACHING)
882 && (pEpClassFile->fCacheEnabled))
883 {
884 pEpFile->fCaching = true;
885 rc = pdmacFileEpCacheInit(pEpFile, pEpClassFile);
886 if (RT_FAILURE(rc))
887 {
888 LogRel(("AIOMgr: Endpoint for \"%s\" was opened with caching but initializing cache failed. Disabled caching\n", pszUri));
889 pEpFile->fCaching = false;
890 }
891 }
892
893 pAioMgr = pEpClassFile->pAioMgrHead;
894
895 /* Check for an idling manager of the same type */
896 while (pAioMgr)
897 {
898 if (pAioMgr->enmMgrType == enmMgrType)
899 break;
900 pAioMgr = pAioMgr->pNext;
901 }
902
903 if (!pAioMgr)
904 {
905 rc = pdmacFileAioMgrCreate(pEpClassFile, &pAioMgr, enmMgrType);
906 AssertRC(rc);
907 }
908 }
909
910 pEpFile->AioMgr.pTreeRangesLocked = (PAVLRFOFFTREE)RTMemAllocZ(sizeof(AVLRFOFFTREE));
911 if (!pEpFile->AioMgr.pTreeRangesLocked)
912 rc = VERR_NO_MEMORY;
913 else
914 {
915 pEpFile->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE;
916
917 /* Assign the endpoint to the thread. */
918 rc = pdmacFileAioMgrAddEndpoint(pAioMgr, pEpFile);
919 if (RT_FAILURE(rc))
920 {
921 RTMemFree(pEpFile->AioMgr.pTreeRangesLocked);
922 MMR3HeapFree(pEpFile->pTasksFreeHead);
923 pdmacFileBwUnref(pEpFile->pBwMgr);
924 }
925 }
926 }
927 }
928
929 if (RT_FAILURE(rc))
930 RTFileClose(pEpFile->File);
931 }
932
933#ifdef VBOX_WITH_STATISTICS
934 if (RT_SUCCESS(rc))
935 {
936 STAMR3RegisterF(pEpClassFile->Core.pVM, &pEpFile->StatRead,
937 STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS,
938 STAMUNIT_TICKS_PER_CALL, "Time taken to read from the endpoint",
939 "/PDM/AsyncCompletion/File/%s/Read", RTPathFilename(pEpFile->Core.pszUri));
940
941 STAMR3RegisterF(pEpClassFile->Core.pVM, &pEpFile->StatWrite,
942 STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS,
943 STAMUNIT_TICKS_PER_CALL, "Time taken to write to the endpoint",
944 "/PDM/AsyncCompletion/File/%s/Write", RTPathFilename(pEpFile->Core.pszUri));
945 }
946#endif
947
948 if (RT_SUCCESS(rc))
949 LogRel(("AIOMgr: Endpoint for file '%s' (flags %08x) created successfully\n", pszUri, pEpFile->fFlags));
950
951 return rc;
952}
953
954static int pdmacFileEpRangesLockedDestroy(PAVLRFOFFNODECORE pNode, void *pvUser)
955{
956 AssertMsgFailed(("The locked ranges tree should be empty at that point\n"));
957 return VINF_SUCCESS;
958}
959
960static int pdmacFileEpClose(PPDMASYNCCOMPLETIONENDPOINT pEndpoint)
961{
962 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
963 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->pEpClass;
964
965 /* Make sure that all tasks finished for this endpoint. */
966 int rc = pdmacFileAioMgrCloseEndpoint(pEpFile->pAioMgr, pEpFile);
967 AssertRC(rc);
968
969 /*
970 * If the async I/O manager is in failsafe mode this is the only endpoint
971 * he processes and thus can be destroyed now.
972 */
973 if (pEpFile->pAioMgr->enmMgrType == PDMACEPFILEMGRTYPE_SIMPLE)
974 pdmacFileAioMgrDestroy(pEpClassFile, pEpFile->pAioMgr);
975
976 /* Free cached tasks. */
977 PPDMACTASKFILE pTask = pEpFile->pTasksFreeHead;
978
979 while (pTask)
980 {
981 PPDMACTASKFILE pTaskFree = pTask;
982 pTask = pTask->pNext;
983 MMR3HeapFree(pTaskFree);
984 }
985
986 /* Free the cached data. */
987 if (pEpFile->fCaching)
988 pdmacFileEpCacheDestroy(pEpFile);
989
990 /* Remove from the bandwidth manager */
991 pdmacFileBwUnref(pEpFile->pBwMgr);
992
993 /* Destroy the locked ranges tree now. */
994 RTAvlrFileOffsetDestroy(pEpFile->AioMgr.pTreeRangesLocked, pdmacFileEpRangesLockedDestroy, NULL);
995
996 RTFileClose(pEpFile->File);
997
998#ifdef VBOX_WITH_STATISTICS
999 STAMR3Deregister(pEpClassFile->Core.pVM, &pEpFile->StatRead);
1000 STAMR3Deregister(pEpClassFile->Core.pVM, &pEpFile->StatWrite);
1001#endif
1002
1003 return VINF_SUCCESS;
1004}
1005
1006static int pdmacFileEpRead(PPDMASYNCCOMPLETIONTASK pTask,
1007 PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
1008 PCPDMDATASEG paSegments, size_t cSegments,
1009 size_t cbRead)
1010{
1011 int rc = VINF_SUCCESS;
1012 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1013
1014 STAM_PROFILE_ADV_START(&pEpFile->StatRead, Read);
1015
1016 if (pEpFile->fCaching)
1017 rc = pdmacFileEpCacheRead(pEpFile, (PPDMASYNCCOMPLETIONTASKFILE)pTask,
1018 off, paSegments, cSegments, cbRead);
1019 else
1020 rc = pdmacFileEpTaskInitiate(pTask, pEndpoint, off, paSegments, cSegments, cbRead,
1021 PDMACTASKFILETRANSFER_READ);
1022
1023 STAM_PROFILE_ADV_STOP(&pEpFile->StatRead, Read);
1024
1025 return rc;
1026}
1027
1028static int pdmacFileEpWrite(PPDMASYNCCOMPLETIONTASK pTask,
1029 PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
1030 PCPDMDATASEG paSegments, size_t cSegments,
1031 size_t cbWrite)
1032{
1033 int rc = VINF_SUCCESS;
1034 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1035
1036 if (RT_UNLIKELY(pEpFile->fReadonly))
1037 return VERR_NOT_SUPPORTED;
1038
1039 STAM_PROFILE_ADV_START(&pEpFile->StatWrite, Write);
1040
1041 if (pEpFile->fCaching)
1042 rc = pdmacFileEpCacheWrite(pEpFile, (PPDMASYNCCOMPLETIONTASKFILE)pTask,
1043 off, paSegments, cSegments, cbWrite);
1044 else
1045 rc = pdmacFileEpTaskInitiate(pTask, pEndpoint, off, paSegments, cSegments, cbWrite,
1046 PDMACTASKFILETRANSFER_WRITE);
1047
1048 STAM_PROFILE_ADV_STOP(&pEpFile->StatWrite, Write);
1049
1050 return rc;
1051}
1052
1053static int pdmacFileEpFlush(PPDMASYNCCOMPLETIONTASK pTask,
1054 PPDMASYNCCOMPLETIONENDPOINT pEndpoint)
1055{
1056 int rc = VINF_SUCCESS;
1057 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1058 PPDMASYNCCOMPLETIONTASKFILE pTaskFile = (PPDMASYNCCOMPLETIONTASKFILE)pTask;
1059
1060 if (RT_UNLIKELY(pEpFile->fReadonly))
1061 return VERR_NOT_SUPPORTED;
1062
1063 pTaskFile->cbTransferLeft = 0;
1064
1065 if (pEpFile->fCaching)
1066 rc = pdmacFileEpCacheFlush(pEpFile, pTaskFile);
1067 else
1068 {
1069 PPDMACTASKFILE pIoTask = pdmacFileTaskAlloc(pEpFile);
1070 AssertPtr(pIoTask);
1071
1072 pIoTask->pEndpoint = pEpFile;
1073 pIoTask->enmTransferType = PDMACTASKFILETRANSFER_FLUSH;
1074 pIoTask->pvUser = pTaskFile;
1075 pIoTask->pfnCompleted = pdmacFileEpTaskCompleted;
1076 pdmacFileEpAddTask(pEpFile, pIoTask);
1077 rc = VINF_AIO_TASK_PENDING;
1078 }
1079
1080 return rc;
1081}
1082
1083static int pdmacFileEpGetSize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, uint64_t *pcbSize)
1084{
1085 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1086
1087 *pcbSize = ASMAtomicReadU64(&pEpFile->cbFile);
1088
1089 return VINF_SUCCESS;
1090}
1091
1092const PDMASYNCCOMPLETIONEPCLASSOPS g_PDMAsyncCompletionEndpointClassFile =
1093{
1094 /* u32Version */
1095 PDMAC_EPCLASS_OPS_VERSION,
1096 /* pcszName */
1097 "File",
1098 /* enmClassType */
1099 PDMASYNCCOMPLETIONEPCLASSTYPE_FILE,
1100 /* cbEndpointClassGlobal */
1101 sizeof(PDMASYNCCOMPLETIONEPCLASSFILE),
1102 /* cbEndpoint */
1103 sizeof(PDMASYNCCOMPLETIONENDPOINTFILE),
1104 /* cbTask */
1105 sizeof(PDMASYNCCOMPLETIONTASKFILE),
1106 /* pfnInitialize */
1107 pdmacFileInitialize,
1108 /* pfnTerminate */
1109 pdmacFileTerminate,
1110 /* pfnEpInitialize. */
1111 pdmacFileEpInitialize,
1112 /* pfnEpClose */
1113 pdmacFileEpClose,
1114 /* pfnEpRead */
1115 pdmacFileEpRead,
1116 /* pfnEpWrite */
1117 pdmacFileEpWrite,
1118 /* pfnEpFlush */
1119 pdmacFileEpFlush,
1120 /* pfnEpGetSize */
1121 pdmacFileEpGetSize,
1122 /* u32VersionEnd */
1123 PDMAC_EPCLASS_OPS_VERSION
1124};
1125
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