VirtualBox

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

Last change on this file since 24359 was 24359, checked in by vboxsync, 16 years ago

AsyncCompletion: CFGM key to disable the cache globally

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 29.4 KB
Line 
1/* $Id: PDMAsyncCompletionFile.cpp 24359 2009-11-04 22:28:48Z 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.215389.xyz. 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 RT_STRICT
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);
324 }
325 else
326 {
327 uint32_t uOld = ASMAtomicSubU32(&pTaskFile->cbTransferLeft, pTask->DataSeg.cbSeg);
328
329 if (!(uOld - pTask->DataSeg.cbSeg)
330 && !ASMAtomicXchgBool(&pTaskFile->fCompleted, true))
331 pdmR3AsyncCompletionCompleteTask(&pTaskFile->Core);
332 }
333}
334
335int pdmacFileEpTaskInitiate(PPDMASYNCCOMPLETIONTASK pTask,
336 PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
337 PCPDMDATASEG paSegments, size_t cSegments,
338 size_t cbTransfer, PDMACTASKFILETRANSFER enmTransfer)
339{
340 int rc = VINF_SUCCESS;
341 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
342 PPDMASYNCCOMPLETIONTASKFILE pTaskFile = (PPDMASYNCCOMPLETIONTASKFILE)pTask;
343 PPDMACEPFILEMGR pAioMgr = pEpFile->pAioMgr;
344
345 Assert( (enmTransfer == PDMACTASKFILETRANSFER_READ)
346 || (enmTransfer == PDMACTASKFILETRANSFER_WRITE));
347
348 ASMAtomicWriteS32(&pTaskFile->cbTransferLeft, cbTransfer);
349 ASMAtomicWriteBool(&pTaskFile->fCompleted, false);
350
351 for (unsigned i = 0; i < cSegments; i++)
352 {
353 PPDMACTASKFILE pIoTask = pdmacFileTaskAlloc(pEpFile);
354 AssertPtr(pIoTask);
355
356 pIoTask->pEndpoint = pEpFile;
357 pIoTask->enmTransferType = enmTransfer;
358 pIoTask->Off = off;
359 pIoTask->DataSeg.cbSeg = paSegments[i].cbSeg;
360 pIoTask->DataSeg.pvSeg = paSegments[i].pvSeg;
361 pIoTask->pvUser = pTaskFile;
362 pIoTask->pfnCompleted = pdmacFileEpTaskCompleted;
363
364 /* Send it off to the I/O manager. */
365 pdmacFileEpAddTask(pEpFile, pIoTask);
366 off += paSegments[i].cbSeg;
367 cbTransfer -= paSegments[i].cbSeg;
368 }
369
370 AssertMsg(!cbTransfer, ("Incomplete transfer %u bytes left\n", cbTransfer));
371
372 if (ASMAtomicReadS32(&pTaskFile->cbTransferLeft) == 0
373 && !ASMAtomicXchgBool(&pTaskFile->fCompleted, true))
374 pdmR3AsyncCompletionCompleteTask(pTask);
375
376 return VINF_SUCCESS;
377}
378
379/**
380 * Creates a new async I/O manager.
381 *
382 * @returns VBox status code.
383 * @param pEpClass Pointer to the endpoint class data.
384 * @param ppAioMgr Where to store the pointer to the new async I/O manager on success.
385 * @param fFailsafe Flag to force a failsafe manager even if the global flag is not set.
386 */
387int pdmacFileAioMgrCreate(PPDMASYNCCOMPLETIONEPCLASSFILE pEpClass, PPPDMACEPFILEMGR ppAioMgr, bool fFailsafe)
388{
389 int rc = VINF_SUCCESS;
390 PPDMACEPFILEMGR pAioMgrNew;
391
392 LogFlowFunc((": Entered\n"));
393
394 rc = MMR3HeapAllocZEx(pEpClass->Core.pVM, MM_TAG_PDM_ASYNC_COMPLETION, sizeof(PDMACEPFILEMGR), (void **)&pAioMgrNew);
395 if (RT_SUCCESS(rc))
396 {
397 pAioMgrNew->fFailsafe = fFailsafe ? true : pEpClass->fFailsafe;
398
399 rc = RTSemEventCreate(&pAioMgrNew->EventSem);
400 if (RT_SUCCESS(rc))
401 {
402 rc = RTSemEventCreate(&pAioMgrNew->EventSemBlock);
403 if (RT_SUCCESS(rc))
404 {
405 rc = RTCritSectInit(&pAioMgrNew->CritSectBlockingEvent);
406 if (RT_SUCCESS(rc))
407 {
408 /* Init the rest of the manager. */
409 if (!pAioMgrNew->fFailsafe)
410 rc = pdmacFileAioMgrNormalInit(pAioMgrNew);
411
412 if (RT_SUCCESS(rc))
413 {
414 pAioMgrNew->enmState = PDMACEPFILEMGRSTATE_RUNNING;
415
416 rc = RTThreadCreateF(&pAioMgrNew->Thread,
417 pAioMgrNew->fFailsafe
418 ? pdmacFileAioMgrFailsafe
419 : pdmacFileAioMgrNormal,
420 pAioMgrNew,
421 0,
422 RTTHREADTYPE_IO,
423 0,
424 "AioMgr%d-%s", pEpClass->cAioMgrs,
425 pAioMgrNew->fFailsafe
426 ? "F"
427 : "N");
428 if (RT_SUCCESS(rc))
429 {
430 /* Link it into the list. */
431 RTCritSectEnter(&pEpClass->CritSect);
432 pAioMgrNew->pNext = pEpClass->pAioMgrHead;
433 if (pEpClass->pAioMgrHead)
434 pEpClass->pAioMgrHead->pPrev = pAioMgrNew;
435 pEpClass->pAioMgrHead = pAioMgrNew;
436 pEpClass->cAioMgrs++;
437 RTCritSectLeave(&pEpClass->CritSect);
438
439 *ppAioMgr = pAioMgrNew;
440
441 Log(("PDMAC: Successfully created new file AIO Mgr {%s}\n", RTThreadGetName(pAioMgrNew->Thread)));
442 return VINF_SUCCESS;
443 }
444 pdmacFileAioMgrNormalDestroy(pAioMgrNew);
445 }
446 RTCritSectDelete(&pAioMgrNew->CritSectBlockingEvent);
447 }
448 RTSemEventDestroy(pAioMgrNew->EventSem);
449 }
450 RTSemEventDestroy(pAioMgrNew->EventSemBlock);
451 }
452 MMR3HeapFree(pAioMgrNew);
453 }
454
455 LogFlowFunc((": Leave rc=%Rrc\n", rc));
456
457 return rc;
458}
459
460/**
461 * Destroys a async I/O manager.
462 *
463 * @returns nothing.
464 * @param pAioMgr The async I/O manager to destroy.
465 */
466static void pdmacFileAioMgrDestroy(PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile, PPDMACEPFILEMGR pAioMgr)
467{
468 int rc = pdmacFileAioMgrShutdown(pAioMgr);
469 AssertRC(rc);
470
471 /* Unlink from the list. */
472 rc = RTCritSectEnter(&pEpClassFile->CritSect);
473 AssertRC(rc);
474
475 PPDMACEPFILEMGR pPrev = pAioMgr->pPrev;
476 PPDMACEPFILEMGR pNext = pAioMgr->pNext;
477
478 if (pPrev)
479 pPrev->pNext = pNext;
480 else
481 pEpClassFile->pAioMgrHead = pNext;
482
483 if (pNext)
484 pNext->pPrev = pPrev;
485
486 pEpClassFile->cAioMgrs--;
487
488 rc = RTCritSectLeave(&pEpClassFile->CritSect);
489 AssertRC(rc);
490
491 /* Free the ressources. */
492 RTCritSectDelete(&pAioMgr->CritSectBlockingEvent);
493 RTSemEventDestroy(pAioMgr->EventSem);
494 if (!pAioMgr->fFailsafe)
495 pdmacFileAioMgrNormalDestroy(pAioMgr);
496
497 MMR3HeapFree(pAioMgr);
498}
499
500static int pdmacFileInitialize(PPDMASYNCCOMPLETIONEPCLASS pClassGlobals, PCFGMNODE pCfgNode)
501{
502 int rc = VINF_SUCCESS;
503 RTFILEAIOLIMITS AioLimits; /** < Async I/O limitations. */
504
505 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pClassGlobals;
506
507 rc = RTFileAioGetLimits(&AioLimits);
508#ifdef DEBUG
509 if (RT_SUCCESS(rc) && RTEnvExist("VBOX_ASYNC_IO_FAILBACK"))
510 rc = VERR_ENV_VAR_NOT_FOUND;
511#endif
512 if (RT_FAILURE(rc))
513 {
514 LogRel(("AIO: Async I/O manager not supported (rc=%Rrc). Falling back to failsafe manager\n",
515 rc));
516 pEpClassFile->fFailsafe = true;
517 }
518 else
519 {
520 pEpClassFile->uBitmaskAlignment = AioLimits.cbBufferAlignment ? ~((RTR3UINTPTR)AioLimits.cbBufferAlignment - 1) : RTR3UINTPTR_MAX;
521 pEpClassFile->cReqsOutstandingMax = AioLimits.cReqsOutstandingMax;
522 pEpClassFile->fFailsafe = false;
523 }
524
525 /* Init critical section. */
526 rc = RTCritSectInit(&pEpClassFile->CritSect);
527 if (RT_SUCCESS(rc))
528 {
529 /* Check if the cache was disabled by the user. */
530 rc = CFGMR3QueryBoolDef(pCfgNode, "CacheEnabled", &pEpClassFile->fCacheEnabled, true);
531 AssertLogRelRCReturn(rc, rc);
532
533 if (pEpClassFile->fCacheEnabled)
534 {
535 /* Init cache structure */
536 rc = pdmacFileCacheInit(pEpClassFile, pCfgNode);
537 if (RT_FAILURE(rc))
538 {
539 RTCritSectDelete(&pEpClassFile->CritSect);
540 pEpClassFile->fCacheEnabled = false;
541 LogRel(("AIOMgr: Failed to initialise the cache (rc=%Rrc), disabled caching\n"));
542 }
543 }
544 else
545 LogRel(("AIOMgr: Cache was globally disabled\n"));
546 }
547
548 return rc;
549}
550
551static void pdmacFileTerminate(PPDMASYNCCOMPLETIONEPCLASS pClassGlobals)
552{
553 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pClassGlobals;
554
555 /* All endpoints should be closed at this point. */
556 AssertMsg(!pEpClassFile->Core.pEndpointsHead, ("There are still endpoints left\n"));
557
558 /* Destroy all left async I/O managers. */
559 while (pEpClassFile->pAioMgrHead)
560 pdmacFileAioMgrDestroy(pEpClassFile, pEpClassFile->pAioMgrHead);
561
562 /* Destroy the cache. */
563 if (pEpClassFile->fCacheEnabled)
564 pdmacFileCacheDestroy(pEpClassFile);
565
566 RTCritSectDelete(&pEpClassFile->CritSect);
567}
568
569static int pdmacFileEpInitialize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint,
570 const char *pszUri, uint32_t fFlags)
571{
572 int rc = VINF_SUCCESS;
573 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
574 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->pEpClass;
575 bool fUseFailsafeManager = pEpClassFile->fFailsafe;
576
577 AssertMsgReturn((fFlags & ~(PDMACEP_FILE_FLAGS_READ_ONLY | PDMACEP_FILE_FLAGS_CACHING)) == 0,
578 ("PDMAsyncCompletion: Invalid flag specified\n"), VERR_INVALID_PARAMETER);
579
580 unsigned fFileFlags = fFlags & PDMACEP_FILE_FLAGS_READ_ONLY
581 ? RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE
582 : RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE;
583
584 if (!pEpClassFile->fFailsafe)
585 {
586 fFileFlags |= (RTFILE_O_ASYNC_IO | RTFILE_O_WRITE_THROUGH);
587
588 /*
589 * We only disable the cache if the size of the file is a multiple of 512.
590 * Certain hosts like Windows, Linux and Solaris require that transfer sizes
591 * are aligned to the volume sector size.
592 * If not we just make sure that the data is written to disk with RTFILE_O_WRITE_THROUGH
593 * which will trash the host cache but ensures that the host cache will not
594 * contain dirty buffers.
595 */
596 RTFILE File = NIL_RTFILE;
597
598 rc = RTFileOpen(&File, pszUri, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
599 if (RT_SUCCESS(rc))
600 {
601 uint64_t cbSize;
602
603 rc = RTFileGetSize(File, &cbSize);
604 if (RT_SUCCESS(rc) && ((cbSize % 512) == 0))
605 {
606 fFileFlags &= ~RTFILE_O_WRITE_THROUGH;
607 fFileFlags |= RTFILE_O_NO_CACHE;
608 }
609
610 pEpFile->cbFile = cbSize;
611
612 RTFileClose(File);
613 }
614 }
615
616 /* Open with final flags. */
617 rc = RTFileOpen(&pEpFile->File, pszUri, fFileFlags);
618 if ((rc == VERR_INVALID_FUNCTION) || (rc == VERR_INVALID_PARAMETER))
619 {
620 LogRel(("pdmacFileEpInitialize: RTFileOpen %s / %08x failed with %Rrc\n",
621 pszUri, fFileFlags, rc));
622 /*
623 * Solaris doesn't support directio on ZFS so far. :-\
624 * Trying to enable it returns VERR_INVALID_FUNCTION
625 * (ENOTTY). Remove it and hope for the best.
626 * ZFS supports write throttling in case applications
627 * write more data than can be synced to the disk
628 * without blocking the whole application.
629 *
630 * On Linux we have the same problem with cifs.
631 * Have to disable async I/O here too because it requires O_DIRECT.
632 */
633 fFileFlags &= ~RTFILE_O_NO_CACHE;
634
635#ifdef RT_OS_LINUX
636 fFileFlags &= ~RTFILE_O_ASYNC_IO;
637 fUseFailsafeManager = true;
638#endif
639
640 /* Open again. */
641 rc = RTFileOpen(&pEpFile->File, pszUri, fFileFlags);
642
643 if (RT_FAILURE(rc))
644 {
645 LogRel(("pdmacFileEpInitialize: RTFileOpen %s / %08x failed AGAIN(!) with %Rrc\n",
646 pszUri, fFileFlags, rc));
647 }
648 }
649
650 if (RT_SUCCESS(rc))
651 {
652 pEpFile->fFlags = fFileFlags;
653
654 rc = RTFileGetSize(pEpFile->File, (uint64_t *)&pEpFile->cbFile);
655 if (RT_SUCCESS(rc))
656 {
657 /* Initialize the segment cache */
658 rc = MMR3HeapAllocZEx(pEpClassFile->Core.pVM, MM_TAG_PDM_ASYNC_COMPLETION,
659 sizeof(PDMACTASKFILE),
660 (void **)&pEpFile->pTasksFreeHead);
661 if (RT_SUCCESS(rc))
662 {
663 PPDMACEPFILEMGR pAioMgr = NULL;
664
665 pEpFile->pTasksFreeTail = pEpFile->pTasksFreeHead;
666 pEpFile->cTasksCached = 0;
667
668 if (fUseFailsafeManager)
669 {
670 /* Safe mode. Every file has its own async I/O manager. */
671 rc = pdmacFileAioMgrCreate(pEpClassFile, &pAioMgr, true);
672 AssertRC(rc);
673 }
674 else
675 {
676 if ( (fFlags & PDMACEP_FILE_FLAGS_CACHING)
677 && (pEpClassFile->fCacheEnabled))
678 {
679 pEpFile->fCaching = true;
680 rc = pdmacFileEpCacheInit(pEpFile, pEpClassFile);
681 if (RT_FAILURE(rc))
682 {
683 LogRel(("AIOMgr: Endpoint for \"%s\" was opened with caching but initializing cache failed. Disabled caching\n", pszUri));
684 pEpFile->fCaching = false;
685 }
686 }
687
688 pAioMgr = pEpClassFile->pAioMgrHead;
689
690 /* Check for an idling not failsafe one or create new if not found */
691 while (pAioMgr && pAioMgr->fFailsafe)
692 pAioMgr = pAioMgr->pNext;
693
694 if (!pAioMgr)
695 {
696 rc = pdmacFileAioMgrCreate(pEpClassFile, &pAioMgr, false);
697 AssertRC(rc);
698 }
699 }
700
701 pEpFile->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE;
702
703 /* Assign the endpoint to the thread. */
704 rc = pdmacFileAioMgrAddEndpoint(pAioMgr, pEpFile);
705 if (RT_FAILURE(rc))
706 MMR3HeapFree(pEpFile->pTasksFreeHead);
707 }
708 }
709
710 if (RT_FAILURE(rc))
711 RTFileClose(pEpFile->File);
712 }
713
714#ifdef VBOX_WITH_STATISTICS
715 if (RT_SUCCESS(rc))
716 {
717 STAMR3RegisterF(pEpClassFile->Core.pVM, &pEpFile->StatRead,
718 STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS,
719 STAMUNIT_TICKS_PER_CALL, "Time taken to read from the endpoint",
720 "/PDM/AsyncCompletion/File/%s/Read", RTPathFilename(pEpFile->Core.pszUri));
721
722 STAMR3RegisterF(pEpClassFile->Core.pVM, &pEpFile->StatWrite,
723 STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS,
724 STAMUNIT_TICKS_PER_CALL, "Time taken to write to the endpoint",
725 "/PDM/AsyncCompletion/File/%s/Write", RTPathFilename(pEpFile->Core.pszUri));
726 }
727#endif
728
729 return rc;
730}
731
732static int pdmacFileEpClose(PPDMASYNCCOMPLETIONENDPOINT pEndpoint)
733{
734 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
735 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->pEpClass;
736
737 /* Make sure that all tasks finished for this endpoint. */
738 int rc = pdmacFileAioMgrCloseEndpoint(pEpFile->pAioMgr, pEpFile);
739 AssertRC(rc);
740
741 /*
742 * If the async I/O manager is in failsafe mode this is the only endpoint
743 * he processes and thus can be destroyed now.
744 */
745 if (pEpFile->pAioMgr->fFailsafe)
746 pdmacFileAioMgrDestroy(pEpClassFile, pEpFile->pAioMgr);
747
748 /* Free cached tasks. */
749 PPDMACTASKFILE pTask = pEpFile->pTasksFreeHead;
750
751 while (pTask)
752 {
753 PPDMACTASKFILE pTaskFree = pTask;
754 pTask = pTask->pNext;
755 MMR3HeapFree(pTaskFree);
756 }
757
758 /* Free the cached data. */
759 if (pEpFile->fCaching)
760 pdmacFileEpCacheDestroy(pEpFile);
761
762 RTFileClose(pEpFile->File);
763
764#ifdef VBOX_WITH_STATISTICS
765 STAMR3Deregister(pEpClassFile->Core.pVM, &pEpFile->StatRead);
766 STAMR3Deregister(pEpClassFile->Core.pVM, &pEpFile->StatWrite);
767#endif
768
769 return VINF_SUCCESS;
770}
771
772static int pdmacFileEpRead(PPDMASYNCCOMPLETIONTASK pTask,
773 PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
774 PCPDMDATASEG paSegments, size_t cSegments,
775 size_t cbRead)
776{
777 int rc = VINF_SUCCESS;
778 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
779
780 STAM_PROFILE_ADV_START(&pEpFile->StatRead, Read);
781
782 if (pEpFile->fCaching)
783 rc = pdmacFileEpCacheRead(pEpFile, (PPDMASYNCCOMPLETIONTASKFILE)pTask,
784 off, paSegments, cSegments, cbRead);
785 else
786 rc = pdmacFileEpTaskInitiate(pTask, pEndpoint, off, paSegments, cSegments, cbRead,
787 PDMACTASKFILETRANSFER_READ);
788
789 STAM_PROFILE_ADV_STOP(&pEpFile->StatRead, Read);
790
791 return rc;
792}
793
794static int pdmacFileEpWrite(PPDMASYNCCOMPLETIONTASK pTask,
795 PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
796 PCPDMDATASEG paSegments, size_t cSegments,
797 size_t cbWrite)
798{
799 int rc = VINF_SUCCESS;
800 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
801
802 if (RT_UNLIKELY(pEpFile->fReadonly))
803 return VERR_NOT_SUPPORTED;
804
805 STAM_PROFILE_ADV_START(&pEpFile->StatWrite, Write);
806
807 if (pEpFile->fCaching)
808 rc = pdmacFileEpCacheWrite(pEpFile, (PPDMASYNCCOMPLETIONTASKFILE)pTask,
809 off, paSegments, cSegments, cbWrite);
810 else
811 rc = pdmacFileEpTaskInitiate(pTask, pEndpoint, off, paSegments, cSegments, cbWrite,
812 PDMACTASKFILETRANSFER_WRITE);
813
814 STAM_PROFILE_ADV_STOP(&pEpFile->StatWrite, Write);
815
816 return rc;
817}
818
819static int pdmacFileEpFlush(PPDMASYNCCOMPLETIONTASK pTask,
820 PPDMASYNCCOMPLETIONENDPOINT pEndpoint)
821{
822 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
823 PPDMASYNCCOMPLETIONTASKFILE pTaskFile = (PPDMASYNCCOMPLETIONTASKFILE)pTask;
824
825 if (RT_UNLIKELY(pEpFile->fReadonly))
826 return VERR_NOT_SUPPORTED;
827
828 pTaskFile->cbTransferLeft = 0;
829
830 PPDMACTASKFILE pIoTask = pdmacFileTaskAlloc(pEpFile);
831 AssertPtr(pIoTask);
832
833 pIoTask->pEndpoint = pEpFile;
834 pIoTask->enmTransferType = PDMACTASKFILETRANSFER_FLUSH;
835 pIoTask->pvUser = pTaskFile;
836 pIoTask->pfnCompleted = pdmacFileEpTaskCompleted;
837 pdmacFileEpAddTask(pEpFile, pIoTask);
838
839 return VINF_SUCCESS;
840}
841
842static int pdmacFileEpGetSize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, uint64_t *pcbSize)
843{
844 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
845
846 *pcbSize = ASMAtomicReadU64(&pEpFile->cbFile);
847
848 return VINF_SUCCESS;
849}
850
851const PDMASYNCCOMPLETIONEPCLASSOPS g_PDMAsyncCompletionEndpointClassFile =
852{
853 /* u32Version */
854 PDMAC_EPCLASS_OPS_VERSION,
855 /* pcszName */
856 "File",
857 /* enmClassType */
858 PDMASYNCCOMPLETIONEPCLASSTYPE_FILE,
859 /* cbEndpointClassGlobal */
860 sizeof(PDMASYNCCOMPLETIONEPCLASSFILE),
861 /* cbEndpoint */
862 sizeof(PDMASYNCCOMPLETIONENDPOINTFILE),
863 /* cbTask */
864 sizeof(PDMASYNCCOMPLETIONTASKFILE),
865 /* pfnInitialize */
866 pdmacFileInitialize,
867 /* pfnTerminate */
868 pdmacFileTerminate,
869 /* pfnEpInitialize. */
870 pdmacFileEpInitialize,
871 /* pfnEpClose */
872 pdmacFileEpClose,
873 /* pfnEpRead */
874 pdmacFileEpRead,
875 /* pfnEpWrite */
876 pdmacFileEpWrite,
877 /* pfnEpFlush */
878 pdmacFileEpFlush,
879 /* pfnEpGetSize */
880 pdmacFileEpGetSize,
881 /* u32VersionEnd */
882 PDMAC_EPCLASS_OPS_VERSION
883};
884
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