VirtualBox

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

Last change on this file since 27766 was 27738, checked in by vboxsync, 15 years ago

AsyncCompletion: Add a new method to set the size of an endpoint

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 39.2 KB
Line 
1/* $Id: PDMAsyncCompletionFile.cpp 27738 2010-03-26 13:21:55Z 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 (enmMgrType == PDMACEPFILEMGRTYPE_ASYNC)
783 fFileFlags |= RTFILE_O_ASYNC_IO;
784
785 if (enmEpBackend == PDMACFILEEPBACKEND_NON_BUFFERED)
786 {
787 /*
788 * We only disable the cache if the size of the file is a multiple of 512.
789 * Certain hosts like Windows, Linux and Solaris require that transfer sizes
790 * are aligned to the volume sector size.
791 * If not we just make sure that the data is written to disk with RTFILE_O_WRITE_THROUGH
792 * which will trash the host cache but ensures that the host cache will not
793 * contain dirty buffers.
794 */
795 RTFILE File = NIL_RTFILE;
796
797 rc = RTFileOpen(&File, pszUri, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
798 if (RT_SUCCESS(rc))
799 {
800 uint64_t cbSize;
801
802 rc = RTFileGetSize(File, &cbSize);
803 if (RT_SUCCESS(rc) && ((cbSize % 512) == 0))
804 fFileFlags |= RTFILE_O_NO_CACHE;
805 else
806 {
807 /* Downgrade to the buffered backend */
808 enmEpBackend = PDMACFILEEPBACKEND_BUFFERED;
809
810#ifdef RT_OS_LINUX
811 fFileFlags &= ~RTFILE_O_ASYNC_IO;
812 enmMgrType = PDMACEPFILEMGRTYPE_SIMPLE;
813#endif
814 }
815 RTFileClose(File);
816 }
817 }
818
819 /* Open with final flags. */
820 rc = RTFileOpen(&pEpFile->File, pszUri, fFileFlags);
821 if ((rc == VERR_INVALID_FUNCTION) || (rc == VERR_INVALID_PARAMETER))
822 {
823 LogRel(("pdmacFileEpInitialize: RTFileOpen %s / %08x failed with %Rrc\n",
824 pszUri, fFileFlags, rc));
825 /*
826 * Solaris doesn't support directio on ZFS so far. :-\
827 * Trying to enable it returns VERR_INVALID_FUNCTION
828 * (ENOTTY). Remove it and hope for the best.
829 * ZFS supports write throttling in case applications
830 * write more data than can be synced to the disk
831 * without blocking the whole application.
832 *
833 * On Linux we have the same problem with cifs.
834 * Have to disable async I/O here too because it requires O_DIRECT.
835 */
836 fFileFlags &= ~RTFILE_O_NO_CACHE;
837 enmEpBackend = PDMACFILEEPBACKEND_BUFFERED;
838
839#ifdef RT_OS_LINUX
840 fFileFlags &= ~RTFILE_O_ASYNC_IO;
841 enmMgrType = PDMACEPFILEMGRTYPE_SIMPLE;
842#endif
843
844 /* Open again. */
845 rc = RTFileOpen(&pEpFile->File, pszUri, fFileFlags);
846
847 if (RT_FAILURE(rc))
848 {
849 LogRel(("pdmacFileEpInitialize: RTFileOpen %s / %08x failed AGAIN(!) with %Rrc\n",
850 pszUri, fFileFlags, rc));
851 }
852 }
853
854 if (RT_SUCCESS(rc))
855 {
856 pEpFile->fFlags = fFileFlags;
857
858 rc = RTFileGetSize(pEpFile->File, (uint64_t *)&pEpFile->cbFile);
859 if (RT_SUCCESS(rc) && (pEpFile->cbFile == 0))
860 {
861 /* Could be a block device */
862 rc = RTFileSeek(pEpFile->File, 0, RTFILE_SEEK_END, (uint64_t *)&pEpFile->cbFile);
863 }
864
865 if (RT_SUCCESS(rc))
866 {
867 /* Initialize the segment cache */
868 rc = MMR3HeapAllocZEx(pEpClassFile->Core.pVM, MM_TAG_PDM_ASYNC_COMPLETION,
869 sizeof(PDMACTASKFILE),
870 (void **)&pEpFile->pTasksFreeHead);
871 if (RT_SUCCESS(rc))
872 {
873 PPDMACEPFILEMGR pAioMgr = NULL;
874
875 pEpFile->cbEndpoint = pEpFile->cbFile;
876 pEpFile->pTasksFreeTail = pEpFile->pTasksFreeHead;
877 pEpFile->cTasksCached = 0;
878 pEpFile->pBwMgr = pEpClassFile->pBwMgr;
879 pEpFile->enmBackendType = enmEpBackend;
880 pdmacFileBwRef(pEpFile->pBwMgr);
881
882 if (enmMgrType == PDMACEPFILEMGRTYPE_SIMPLE)
883 {
884 /* Simple mode. Every file has its own async I/O manager. */
885 rc = pdmacFileAioMgrCreate(pEpClassFile, &pAioMgr, PDMACEPFILEMGRTYPE_SIMPLE);
886 AssertRC(rc);
887 }
888 else
889 {
890 if ( (fFlags & PDMACEP_FILE_FLAGS_CACHING)
891 && (pEpClassFile->fCacheEnabled))
892 {
893 pEpFile->fCaching = true;
894 rc = pdmacFileEpCacheInit(pEpFile, pEpClassFile);
895 if (RT_FAILURE(rc))
896 {
897 LogRel(("AIOMgr: Endpoint for \"%s\" was opened with caching but initializing cache failed. Disabled caching\n", pszUri));
898 pEpFile->fCaching = false;
899 }
900 }
901
902 pAioMgr = pEpClassFile->pAioMgrHead;
903
904 /* Check for an idling manager of the same type */
905 while (pAioMgr)
906 {
907 if (pAioMgr->enmMgrType == enmMgrType)
908 break;
909 pAioMgr = pAioMgr->pNext;
910 }
911
912 if (!pAioMgr)
913 {
914 rc = pdmacFileAioMgrCreate(pEpClassFile, &pAioMgr, enmMgrType);
915 AssertRC(rc);
916 }
917 }
918
919 pEpFile->AioMgr.pTreeRangesLocked = (PAVLRFOFFTREE)RTMemAllocZ(sizeof(AVLRFOFFTREE));
920 if (!pEpFile->AioMgr.pTreeRangesLocked)
921 rc = VERR_NO_MEMORY;
922 else
923 {
924 pEpFile->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE;
925
926 /* Assign the endpoint to the thread. */
927 rc = pdmacFileAioMgrAddEndpoint(pAioMgr, pEpFile);
928 if (RT_FAILURE(rc))
929 {
930 RTMemFree(pEpFile->AioMgr.pTreeRangesLocked);
931 MMR3HeapFree(pEpFile->pTasksFreeHead);
932 pdmacFileBwUnref(pEpFile->pBwMgr);
933 }
934 }
935 }
936 }
937
938 if (RT_FAILURE(rc))
939 RTFileClose(pEpFile->File);
940 }
941
942#ifdef VBOX_WITH_STATISTICS
943 if (RT_SUCCESS(rc))
944 {
945 STAMR3RegisterF(pEpClassFile->Core.pVM, &pEpFile->StatRead,
946 STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS,
947 STAMUNIT_TICKS_PER_CALL, "Time taken to read from the endpoint",
948 "/PDM/AsyncCompletion/File/%s/Read", RTPathFilename(pEpFile->Core.pszUri));
949
950 STAMR3RegisterF(pEpClassFile->Core.pVM, &pEpFile->StatWrite,
951 STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS,
952 STAMUNIT_TICKS_PER_CALL, "Time taken to write to the endpoint",
953 "/PDM/AsyncCompletion/File/%s/Write", RTPathFilename(pEpFile->Core.pszUri));
954 }
955#endif
956
957 if (RT_SUCCESS(rc))
958 LogRel(("AIOMgr: Endpoint for file '%s' (flags %08x) created successfully\n", pszUri, pEpFile->fFlags));
959
960 return rc;
961}
962
963static int pdmacFileEpRangesLockedDestroy(PAVLRFOFFNODECORE pNode, void *pvUser)
964{
965 AssertMsgFailed(("The locked ranges tree should be empty at that point\n"));
966 return VINF_SUCCESS;
967}
968
969static int pdmacFileEpClose(PPDMASYNCCOMPLETIONENDPOINT pEndpoint)
970{
971 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
972 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->pEpClass;
973
974 /* Make sure that all tasks finished for this endpoint. */
975 int rc = pdmacFileAioMgrCloseEndpoint(pEpFile->pAioMgr, pEpFile);
976 AssertRC(rc);
977
978 /* endpoint and real file size should better be equal now. */
979 AssertMsg(pEpFile->cbFile == pEpFile->cbEndpoint,
980 ("Endpoint and real file size should match now!\n"));
981
982 /*
983 * If the async I/O manager is in failsafe mode this is the only endpoint
984 * he processes and thus can be destroyed now.
985 */
986 if (pEpFile->pAioMgr->enmMgrType == PDMACEPFILEMGRTYPE_SIMPLE)
987 pdmacFileAioMgrDestroy(pEpClassFile, pEpFile->pAioMgr);
988
989 /* Free cached tasks. */
990 PPDMACTASKFILE pTask = pEpFile->pTasksFreeHead;
991
992 while (pTask)
993 {
994 PPDMACTASKFILE pTaskFree = pTask;
995 pTask = pTask->pNext;
996 MMR3HeapFree(pTaskFree);
997 }
998
999 /* Free the cached data. */
1000 if (pEpFile->fCaching)
1001 pdmacFileEpCacheDestroy(pEpFile);
1002
1003 /* Remove from the bandwidth manager */
1004 pdmacFileBwUnref(pEpFile->pBwMgr);
1005
1006 /* Destroy the locked ranges tree now. */
1007 RTAvlrFileOffsetDestroy(pEpFile->AioMgr.pTreeRangesLocked, pdmacFileEpRangesLockedDestroy, NULL);
1008
1009 RTFileClose(pEpFile->File);
1010
1011#ifdef VBOX_WITH_STATISTICS
1012 STAMR3Deregister(pEpClassFile->Core.pVM, &pEpFile->StatRead);
1013 STAMR3Deregister(pEpClassFile->Core.pVM, &pEpFile->StatWrite);
1014#endif
1015
1016 return VINF_SUCCESS;
1017}
1018
1019static int pdmacFileEpRead(PPDMASYNCCOMPLETIONTASK pTask,
1020 PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
1021 PCPDMDATASEG paSegments, size_t cSegments,
1022 size_t cbRead)
1023{
1024 int rc = VINF_SUCCESS;
1025 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1026
1027 STAM_PROFILE_ADV_START(&pEpFile->StatRead, Read);
1028
1029 if (pEpFile->fCaching)
1030 rc = pdmacFileEpCacheRead(pEpFile, (PPDMASYNCCOMPLETIONTASKFILE)pTask,
1031 off, paSegments, cSegments, cbRead);
1032 else
1033 rc = pdmacFileEpTaskInitiate(pTask, pEndpoint, off, paSegments, cSegments, cbRead,
1034 PDMACTASKFILETRANSFER_READ);
1035
1036 STAM_PROFILE_ADV_STOP(&pEpFile->StatRead, Read);
1037
1038 return rc;
1039}
1040
1041static int pdmacFileEpWrite(PPDMASYNCCOMPLETIONTASK pTask,
1042 PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
1043 PCPDMDATASEG paSegments, size_t cSegments,
1044 size_t cbWrite)
1045{
1046 int rc = VINF_SUCCESS;
1047 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1048
1049 if (RT_UNLIKELY(pEpFile->fReadonly))
1050 return VERR_NOT_SUPPORTED;
1051
1052 STAM_PROFILE_ADV_START(&pEpFile->StatWrite, Write);
1053
1054 if (pEpFile->fCaching)
1055 rc = pdmacFileEpCacheWrite(pEpFile, (PPDMASYNCCOMPLETIONTASKFILE)pTask,
1056 off, paSegments, cSegments, cbWrite);
1057 else
1058 rc = pdmacFileEpTaskInitiate(pTask, pEndpoint, off, paSegments, cSegments, cbWrite,
1059 PDMACTASKFILETRANSFER_WRITE);
1060
1061 STAM_PROFILE_ADV_STOP(&pEpFile->StatWrite, Write);
1062
1063 /* Increase endpoint size. */
1064 if ( RT_SUCCESS(rc)
1065 && ((uint64_t)off + cbWrite) > pEpFile->cbEndpoint)
1066 ASMAtomicWriteU64(&pEpFile->cbEndpoint, (uint64_t)off + cbWrite);
1067
1068 return rc;
1069}
1070
1071static int pdmacFileEpFlush(PPDMASYNCCOMPLETIONTASK pTask,
1072 PPDMASYNCCOMPLETIONENDPOINT pEndpoint)
1073{
1074 int rc = VINF_SUCCESS;
1075 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1076 PPDMASYNCCOMPLETIONTASKFILE pTaskFile = (PPDMASYNCCOMPLETIONTASKFILE)pTask;
1077
1078 if (RT_UNLIKELY(pEpFile->fReadonly))
1079 return VERR_NOT_SUPPORTED;
1080
1081 pTaskFile->cbTransferLeft = 0;
1082
1083 if (pEpFile->fCaching)
1084 rc = pdmacFileEpCacheFlush(pEpFile, pTaskFile);
1085 else
1086 {
1087 PPDMACTASKFILE pIoTask = pdmacFileTaskAlloc(pEpFile);
1088 AssertPtr(pIoTask);
1089
1090 pIoTask->pEndpoint = pEpFile;
1091 pIoTask->enmTransferType = PDMACTASKFILETRANSFER_FLUSH;
1092 pIoTask->pvUser = pTaskFile;
1093 pIoTask->pfnCompleted = pdmacFileEpTaskCompleted;
1094 pdmacFileEpAddTask(pEpFile, pIoTask);
1095 rc = VINF_AIO_TASK_PENDING;
1096 }
1097
1098 return rc;
1099}
1100
1101static int pdmacFileEpGetSize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, uint64_t *pcbSize)
1102{
1103 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1104
1105 *pcbSize = ASMAtomicReadU64(&pEpFile->cbEndpoint);
1106
1107 return VINF_SUCCESS;
1108}
1109
1110static int pdmacFileEpSetSize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, uint64_t cbSize)
1111{
1112 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1113
1114 ASMAtomicWriteU64(&pEpFile->cbEndpoint, cbSize);
1115 return RTFileSetSize(pEpFile->File, cbSize);
1116}
1117
1118const PDMASYNCCOMPLETIONEPCLASSOPS g_PDMAsyncCompletionEndpointClassFile =
1119{
1120 /* u32Version */
1121 PDMAC_EPCLASS_OPS_VERSION,
1122 /* pcszName */
1123 "File",
1124 /* enmClassType */
1125 PDMASYNCCOMPLETIONEPCLASSTYPE_FILE,
1126 /* cbEndpointClassGlobal */
1127 sizeof(PDMASYNCCOMPLETIONEPCLASSFILE),
1128 /* cbEndpoint */
1129 sizeof(PDMASYNCCOMPLETIONENDPOINTFILE),
1130 /* cbTask */
1131 sizeof(PDMASYNCCOMPLETIONTASKFILE),
1132 /* pfnInitialize */
1133 pdmacFileInitialize,
1134 /* pfnTerminate */
1135 pdmacFileTerminate,
1136 /* pfnEpInitialize. */
1137 pdmacFileEpInitialize,
1138 /* pfnEpClose */
1139 pdmacFileEpClose,
1140 /* pfnEpRead */
1141 pdmacFileEpRead,
1142 /* pfnEpWrite */
1143 pdmacFileEpWrite,
1144 /* pfnEpFlush */
1145 pdmacFileEpFlush,
1146 /* pfnEpGetSize */
1147 pdmacFileEpGetSize,
1148 /* pfnEpSetSize */
1149 pdmacFileEpSetSize,
1150 /* u32VersionEnd */
1151 PDMAC_EPCLASS_OPS_VERSION
1152};
1153
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