VirtualBox

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

Last change on this file since 26638 was 26527, checked in by vboxsync, 15 years ago

dang

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 31.9 KB
Line 
1/* $Id: PDMAsyncCompletionFile.cpp 26527 2010-02-15 03:42:05Z 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 RT_STRICT
28//#define DEBUG
29#include "PDMInternal.h"
30#include <VBox/pdm.h>
31#include <VBox/mm.h>
32#include <VBox/vm.h>
33#include <VBox/err.h>
34#include <VBox/log.h>
35
36#include <iprt/asm.h>
37#include <iprt/assert.h>
38#include <iprt/critsect.h>
39#include <iprt/env.h>
40#include <iprt/file.h>
41#include <iprt/mem.h>
42#include <iprt/semaphore.h>
43#include <iprt/string.h>
44#include <iprt/thread.h>
45#include <iprt/path.h>
46
47#include "PDMAsyncCompletionFileInternal.h"
48
49/**
50 * Frees a task.
51 *
52 * @returns nothing.
53 * @param pEndpoint Pointer to the endpoint the segment was for.
54 * @param pTask The task to free.
55 */
56void pdmacFileTaskFree(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint,
57 PPDMACTASKFILE pTask)
58{
59 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClass = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->Core.pEpClass;
60
61 LogFlowFunc((": pEndpoint=%p pTask=%p\n", pEndpoint, pTask));
62
63 /* Try the per endpoint cache first. */
64 if (pEndpoint->cTasksCached < pEpClass->cTasksCacheMax)
65 {
66 /* Add it to the list. */
67 pEndpoint->pTasksFreeTail->pNext = pTask;
68 pEndpoint->pTasksFreeTail = pTask;
69 ASMAtomicIncU32(&pEndpoint->cTasksCached);
70 }
71 else if (false)
72 {
73 /* Bigger class cache */
74 }
75 else
76 {
77 Log(("Freeing task %p because all caches are full\n", pTask));
78 MMR3HeapFree(pTask);
79 }
80}
81
82/**
83 * Allocates a task segment
84 *
85 * @returns Pointer to the new task segment or NULL
86 * @param pEndpoint Pointer to the endpoint
87 */
88PPDMACTASKFILE pdmacFileTaskAlloc(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
89{
90 PPDMACTASKFILE pTask = NULL;
91
92 /* Try the small per endpoint cache first. */
93 if (pEndpoint->pTasksFreeHead == pEndpoint->pTasksFreeTail)
94 {
95 /* Try the bigger endpoint class cache. */
96 PPDMASYNCCOMPLETIONEPCLASSFILE pEndpointClass = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->Core.pEpClass;
97
98#if 0
99 /* We start with the assigned slot id to distribute the load when allocating new tasks. */
100 unsigned iSlot = pEndpoint->iSlotStart;
101 do
102 {
103 pTask = (PPDMASYNCCOMPLETIONTASK)ASMAtomicXchgPtr((void * volatile *)&pEndpointClass->apTaskCache[iSlot], NULL);
104 if (pTask)
105 break;
106
107 iSlot = (iSlot + 1) % RT_ELEMENTS(pEndpointClass->apTaskCache);
108 } while (iSlot != pEndpoint->iSlotStart);
109#endif
110 if (!pTask)
111 {
112 /*
113 * Allocate completely new.
114 * If this fails we return NULL.
115 */
116 int rc = MMR3HeapAllocZEx(pEndpointClass->Core.pVM, MM_TAG_PDM_ASYNC_COMPLETION,
117 sizeof(PDMACTASKFILE),
118 (void **)&pTask);
119 if (RT_FAILURE(rc))
120 pTask = NULL;
121
122 LogFlow(("Allocated task %p\n", pTask));
123 }
124#if 0
125 else
126 {
127 /* Remove the first element and put the rest into the slot again. */
128 PPDMASYNCCOMPLETIONTASK pTaskHeadNew = pTask->pNext;
129
130 pTaskHeadNew->pPrev = NULL;
131
132 /* Put back into the list adding any new tasks. */
133 while (true)
134 {
135 bool fChanged = ASMAtomicCmpXchgPtr((void * volatile *)&pEndpointClass->apTaskCache[iSlot], pTaskHeadNew, NULL);
136
137 if (fChanged)
138 break;
139
140 PPDMASYNCCOMPLETIONTASK pTaskHead = (PPDMASYNCCOMPLETIONTASK)ASMAtomicXchgPtr((void * volatile *)&pEndpointClass->apTaskCache[iSlot], NULL);
141
142 /* The new task could be taken inbetween */
143 if (pTaskHead)
144 {
145 /* Go to the end of the probably much shorter new list. */
146 PPDMASYNCCOMPLETIONTASK pTaskTail = pTaskHead;
147 while (pTaskTail->pNext)
148 pTaskTail = pTaskTail->pNext;
149
150 /* Concatenate */
151 pTaskTail->pNext = pTaskHeadNew;
152
153 pTaskHeadNew = pTaskHead;
154 }
155 /* Another round trying to change the list. */
156 }
157 /* We got a task from the global cache so decrement the counter */
158 ASMAtomicDecU32(&pEndpointClass->cTasksCached);
159 }
160#endif
161 }
162 else
163 {
164 /* Grab a free task from the head. */
165 AssertMsg(pEndpoint->cTasksCached > 0, ("No tasks cached but list contains more than one element\n"));
166
167 pTask = pEndpoint->pTasksFreeHead;
168 pEndpoint->pTasksFreeHead = pTask->pNext;
169 ASMAtomicDecU32(&pEndpoint->cTasksCached);
170 }
171
172 pTask->pNext = NULL;
173
174 return pTask;
175}
176
177PPDMACTASKFILE pdmacFileEpGetNewTasks(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
178{
179 PPDMACTASKFILE pTasks = NULL;
180
181 /*
182 * Get pending tasks.
183 */
184 pTasks = (PPDMACTASKFILE)ASMAtomicXchgPtr((void * volatile *)&pEndpoint->pTasksNewHead, NULL);
185
186 /* Reverse the list to process in FIFO order. */
187 if (pTasks)
188 {
189 PPDMACTASKFILE pTask = pTasks;
190
191 pTasks = NULL;
192
193 while (pTask)
194 {
195 PPDMACTASKFILE pCur = pTask;
196 pTask = pTask->pNext;
197 pCur->pNext = pTasks;
198 pTasks = pCur;
199 }
200 }
201
202 return pTasks;
203}
204
205static void pdmacFileAioMgrWakeup(PPDMACEPFILEMGR pAioMgr)
206{
207 bool fWokenUp = ASMAtomicXchgBool(&pAioMgr->fWokenUp, true);
208
209 if (!fWokenUp)
210 {
211 int rc = VINF_SUCCESS;
212 bool fWaitingEventSem = ASMAtomicReadBool(&pAioMgr->fWaitingEventSem);
213
214 if (fWaitingEventSem)
215 rc = RTSemEventSignal(pAioMgr->EventSem);
216
217 AssertRC(rc);
218 }
219}
220
221static int pdmacFileAioMgrWaitForBlockingEvent(PPDMACEPFILEMGR pAioMgr, PDMACEPFILEAIOMGRBLOCKINGEVENT enmEvent)
222{
223 int rc = VINF_SUCCESS;
224
225 ASMAtomicWriteU32((volatile uint32_t *)&pAioMgr->enmBlockingEvent, enmEvent);
226 Assert(!pAioMgr->fBlockingEventPending);
227 ASMAtomicXchgBool(&pAioMgr->fBlockingEventPending, true);
228
229 /* Wakeup the async I/O manager */
230 pdmacFileAioMgrWakeup(pAioMgr);
231
232 /* Wait for completion. */
233 rc = RTSemEventWait(pAioMgr->EventSemBlock, RT_INDEFINITE_WAIT);
234 AssertRC(rc);
235
236 ASMAtomicXchgBool(&pAioMgr->fBlockingEventPending, false);
237 ASMAtomicWriteU32((volatile uint32_t *)&pAioMgr->enmBlockingEvent, PDMACEPFILEAIOMGRBLOCKINGEVENT_INVALID);
238
239 return rc;
240}
241
242int pdmacFileAioMgrAddEndpoint(PPDMACEPFILEMGR pAioMgr, PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
243{
244 int rc;
245
246 rc = RTCritSectEnter(&pAioMgr->CritSectBlockingEvent);
247 AssertRCReturn(rc, rc);
248
249 ASMAtomicWritePtr((void * volatile *)&pAioMgr->BlockingEventData.AddEndpoint.pEndpoint, pEndpoint);
250 rc = pdmacFileAioMgrWaitForBlockingEvent(pAioMgr, PDMACEPFILEAIOMGRBLOCKINGEVENT_ADD_ENDPOINT);
251
252 RTCritSectLeave(&pAioMgr->CritSectBlockingEvent);
253
254 if (RT_SUCCESS(rc))
255 ASMAtomicWritePtr((void * volatile *)&pEndpoint->pAioMgr, pAioMgr);
256
257 return rc;
258}
259
260static int pdmacFileAioMgrRemoveEndpoint(PPDMACEPFILEMGR pAioMgr, PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
261{
262 int rc;
263
264 rc = RTCritSectEnter(&pAioMgr->CritSectBlockingEvent);
265 AssertRCReturn(rc, rc);
266
267 ASMAtomicWritePtr((void * volatile *)&pAioMgr->BlockingEventData.RemoveEndpoint.pEndpoint, pEndpoint);
268 rc = pdmacFileAioMgrWaitForBlockingEvent(pAioMgr, PDMACEPFILEAIOMGRBLOCKINGEVENT_REMOVE_ENDPOINT);
269
270 RTCritSectLeave(&pAioMgr->CritSectBlockingEvent);
271
272 return rc;
273}
274
275static int pdmacFileAioMgrCloseEndpoint(PPDMACEPFILEMGR pAioMgr, PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
276{
277 int rc;
278
279 rc = RTCritSectEnter(&pAioMgr->CritSectBlockingEvent);
280 AssertRCReturn(rc, rc);
281
282 ASMAtomicWritePtr((void * volatile *)&pAioMgr->BlockingEventData.CloseEndpoint.pEndpoint, pEndpoint);
283 rc = pdmacFileAioMgrWaitForBlockingEvent(pAioMgr, PDMACEPFILEAIOMGRBLOCKINGEVENT_CLOSE_ENDPOINT);
284
285 RTCritSectLeave(&pAioMgr->CritSectBlockingEvent);
286
287 return rc;
288}
289
290static int pdmacFileAioMgrShutdown(PPDMACEPFILEMGR pAioMgr)
291{
292 int rc;
293
294 rc = RTCritSectEnter(&pAioMgr->CritSectBlockingEvent);
295 AssertRCReturn(rc, rc);
296
297 rc = pdmacFileAioMgrWaitForBlockingEvent(pAioMgr, PDMACEPFILEAIOMGRBLOCKINGEVENT_SHUTDOWN);
298
299 RTCritSectLeave(&pAioMgr->CritSectBlockingEvent);
300
301 return rc;
302}
303
304int pdmacFileEpAddTask(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint, PPDMACTASKFILE pTask)
305{
306 PPDMACTASKFILE pNext;
307 do
308 {
309 pNext = pEndpoint->pTasksNewHead;
310 pTask->pNext = pNext;
311 } while (!ASMAtomicCmpXchgPtr((void * volatile *)&pEndpoint->pTasksNewHead, (void *)pTask, (void *)pNext));
312
313 pdmacFileAioMgrWakeup((PPDMACEPFILEMGR)ASMAtomicReadPtr((void * volatile *)&pEndpoint->pAioMgr));
314
315 return VINF_SUCCESS;
316}
317
318void pdmacFileEpTaskCompleted(PPDMACTASKFILE pTask, void *pvUser)
319{
320 PPDMASYNCCOMPLETIONTASKFILE pTaskFile = (PPDMASYNCCOMPLETIONTASKFILE)pvUser;
321
322 if (pTask->enmTransferType == PDMACTASKFILETRANSFER_FLUSH)
323 {
324 pdmR3AsyncCompletionCompleteTask(&pTaskFile->Core, true);
325 }
326 else
327 {
328 Assert((uint32_t)pTask->DataSeg.cbSeg == pTask->DataSeg.cbSeg && (int32_t)pTask->DataSeg.cbSeg >= 0);
329 uint32_t uOld = ASMAtomicSubS32(&pTaskFile->cbTransferLeft, (int32_t)pTask->DataSeg.cbSeg);
330
331 if (!(uOld - pTask->DataSeg.cbSeg)
332 && !ASMAtomicXchgBool(&pTaskFile->fCompleted, true))
333 pdmR3AsyncCompletionCompleteTask(&pTaskFile->Core, true);
334 }
335}
336
337int pdmacFileEpTaskInitiate(PPDMASYNCCOMPLETIONTASK pTask,
338 PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
339 PCPDMDATASEG paSegments, size_t cSegments,
340 size_t cbTransfer, PDMACTASKFILETRANSFER enmTransfer)
341{
342 int rc = VINF_SUCCESS;
343 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
344 PPDMASYNCCOMPLETIONTASKFILE pTaskFile = (PPDMASYNCCOMPLETIONTASKFILE)pTask;
345 PPDMACEPFILEMGR pAioMgr = pEpFile->pAioMgr;
346
347 Assert( (enmTransfer == PDMACTASKFILETRANSFER_READ)
348 || (enmTransfer == PDMACTASKFILETRANSFER_WRITE));
349
350 Assert((uint32_t)cbTransfer == cbTransfer && (int32_t)cbTransfer >= 0);
351 ASMAtomicWriteS32(&pTaskFile->cbTransferLeft, (int32_t)cbTransfer);
352 ASMAtomicWriteBool(&pTaskFile->fCompleted, false);
353
354 for (unsigned i = 0; i < cSegments; i++)
355 {
356 PPDMACTASKFILE pIoTask = pdmacFileTaskAlloc(pEpFile);
357 AssertPtr(pIoTask);
358
359 pIoTask->pEndpoint = pEpFile;
360 pIoTask->enmTransferType = enmTransfer;
361 pIoTask->Off = off;
362 pIoTask->DataSeg.cbSeg = paSegments[i].cbSeg;
363 pIoTask->DataSeg.pvSeg = paSegments[i].pvSeg;
364 pIoTask->pvUser = pTaskFile;
365 pIoTask->pfnCompleted = pdmacFileEpTaskCompleted;
366
367 /* Send it off to the I/O manager. */
368 pdmacFileEpAddTask(pEpFile, pIoTask);
369 off += paSegments[i].cbSeg;
370 cbTransfer -= paSegments[i].cbSeg;
371 }
372
373 AssertMsg(!cbTransfer, ("Incomplete transfer %u bytes left\n", cbTransfer));
374
375 if (ASMAtomicReadS32(&pTaskFile->cbTransferLeft) == 0
376 && !ASMAtomicXchgBool(&pTaskFile->fCompleted, true))
377 pdmR3AsyncCompletionCompleteTask(pTask, false);
378 else
379 rc = VINF_AIO_TASK_PENDING;
380
381 return rc;
382}
383
384/**
385 * Creates a new async I/O manager.
386 *
387 * @returns VBox status code.
388 * @param pEpClass Pointer to the endpoint class data.
389 * @param ppAioMgr Where to store the pointer to the new async I/O manager on success.
390 * @param fFailsafe Flag to force a failsafe manager even if the global flag is not set.
391 */
392int pdmacFileAioMgrCreate(PPDMASYNCCOMPLETIONEPCLASSFILE pEpClass, PPPDMACEPFILEMGR ppAioMgr, bool fFailsafe)
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 pAioMgrNew->fFailsafe = fFailsafe || pEpClass->fFailsafe;
403
404 rc = RTSemEventCreate(&pAioMgrNew->EventSem);
405 if (RT_SUCCESS(rc))
406 {
407 rc = RTSemEventCreate(&pAioMgrNew->EventSemBlock);
408 if (RT_SUCCESS(rc))
409 {
410 rc = RTCritSectInit(&pAioMgrNew->CritSectBlockingEvent);
411 if (RT_SUCCESS(rc))
412 {
413 /* Init the rest of the manager. */
414 if (!pAioMgrNew->fFailsafe)
415 rc = pdmacFileAioMgrNormalInit(pAioMgrNew);
416
417 if (RT_SUCCESS(rc))
418 {
419 pAioMgrNew->enmState = PDMACEPFILEMGRSTATE_RUNNING;
420
421 rc = RTThreadCreateF(&pAioMgrNew->Thread,
422 pAioMgrNew->fFailsafe
423 ? pdmacFileAioMgrFailsafe
424 : pdmacFileAioMgrNormal,
425 pAioMgrNew,
426 0,
427 RTTHREADTYPE_IO,
428 0,
429 "AioMgr%d-%s", pEpClass->cAioMgrs,
430 pAioMgrNew->fFailsafe
431 ? "F"
432 : "N");
433 if (RT_SUCCESS(rc))
434 {
435 /* Link it into the list. */
436 RTCritSectEnter(&pEpClass->CritSect);
437 pAioMgrNew->pNext = pEpClass->pAioMgrHead;
438 if (pEpClass->pAioMgrHead)
439 pEpClass->pAioMgrHead->pPrev = pAioMgrNew;
440 pEpClass->pAioMgrHead = pAioMgrNew;
441 pEpClass->cAioMgrs++;
442 RTCritSectLeave(&pEpClass->CritSect);
443
444 *ppAioMgr = pAioMgrNew;
445
446 Log(("PDMAC: Successfully created new file AIO Mgr {%s}\n", RTThreadGetName(pAioMgrNew->Thread)));
447 return VINF_SUCCESS;
448 }
449 pdmacFileAioMgrNormalDestroy(pAioMgrNew);
450 }
451 RTCritSectDelete(&pAioMgrNew->CritSectBlockingEvent);
452 }
453 RTSemEventDestroy(pAioMgrNew->EventSem);
454 }
455 RTSemEventDestroy(pAioMgrNew->EventSemBlock);
456 }
457 MMR3HeapFree(pAioMgrNew);
458 }
459
460 LogFlowFunc((": Leave rc=%Rrc\n", rc));
461
462 return rc;
463}
464
465/**
466 * Destroys a async I/O manager.
467 *
468 * @returns nothing.
469 * @param pAioMgr The async I/O manager to destroy.
470 */
471static void pdmacFileAioMgrDestroy(PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile, PPDMACEPFILEMGR pAioMgr)
472{
473 int rc = pdmacFileAioMgrShutdown(pAioMgr);
474 AssertRC(rc);
475
476 /* Unlink from the list. */
477 rc = RTCritSectEnter(&pEpClassFile->CritSect);
478 AssertRC(rc);
479
480 PPDMACEPFILEMGR pPrev = pAioMgr->pPrev;
481 PPDMACEPFILEMGR pNext = pAioMgr->pNext;
482
483 if (pPrev)
484 pPrev->pNext = pNext;
485 else
486 pEpClassFile->pAioMgrHead = pNext;
487
488 if (pNext)
489 pNext->pPrev = pPrev;
490
491 pEpClassFile->cAioMgrs--;
492
493 rc = RTCritSectLeave(&pEpClassFile->CritSect);
494 AssertRC(rc);
495
496 /* Free the ressources. */
497 RTCritSectDelete(&pAioMgr->CritSectBlockingEvent);
498 RTSemEventDestroy(pAioMgr->EventSem);
499 if (!pAioMgr->fFailsafe)
500 pdmacFileAioMgrNormalDestroy(pAioMgr);
501
502 MMR3HeapFree(pAioMgr);
503}
504
505static int pdmacFileInitialize(PPDMASYNCCOMPLETIONEPCLASS pClassGlobals, PCFGMNODE pCfgNode)
506{
507 int rc = VINF_SUCCESS;
508 RTFILEAIOLIMITS AioLimits; /** < Async I/O limitations. */
509
510 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pClassGlobals;
511
512 rc = RTFileAioGetLimits(&AioLimits);
513#ifdef DEBUG
514 if (RT_SUCCESS(rc) && RTEnvExist("VBOX_ASYNC_IO_FAILBACK"))
515 rc = VERR_ENV_VAR_NOT_FOUND;
516#endif
517 if (RT_FAILURE(rc))
518 {
519 LogRel(("AIO: Async I/O manager not supported (rc=%Rrc). Falling back to failsafe manager\n",
520 rc));
521 pEpClassFile->fFailsafe = true;
522 }
523 else
524 {
525 pEpClassFile->uBitmaskAlignment = AioLimits.cbBufferAlignment ? ~((RTR3UINTPTR)AioLimits.cbBufferAlignment - 1) : RTR3UINTPTR_MAX;
526 pEpClassFile->cReqsOutstandingMax = AioLimits.cReqsOutstandingMax;
527
528 /* The user can force the failsafe manager. */
529 rc = CFGMR3QueryBoolDef(pCfgNode, "UseFailsafeIo", &pEpClassFile->fFailsafe, false);
530 AssertLogRelRCReturn(rc, rc);
531
532 if (pEpClassFile->fFailsafe)
533 LogRel(("AIOMgr: Failsafe I/O was requested by user\n"));
534 }
535
536 /* Init critical section. */
537 rc = RTCritSectInit(&pEpClassFile->CritSect);
538 if (RT_SUCCESS(rc))
539 {
540 /* Check if the host cache should be used too. */
541#ifndef RT_OS_LINUX
542 rc = CFGMR3QueryBoolDef(pCfgNode, "HostCacheEnabled", &pEpClassFile->fHostCacheEnabled, false);
543 AssertLogRelRCReturn(rc, rc);
544#else
545 /*
546 * Host cache + async I/O is not supported on Linux. Check if the user enabled the cache,
547 * leave a warning and disable it always.
548 */
549 bool fDummy;
550 rc = CFGMR3QueryBool(pCfgNode, "HostCacheEnabled", &fDummy);
551 if (RT_SUCCESS(rc))
552 LogRel(("AIOMgr: The host cache is not supported with async I/O on Linux\n"));
553
554 pEpClassFile->fHostCacheEnabled = false;
555#endif
556
557 /* Check if the cache was disabled by the user. */
558 rc = CFGMR3QueryBoolDef(pCfgNode, "CacheEnabled", &pEpClassFile->fCacheEnabled, true);
559 AssertLogRelRCReturn(rc, rc);
560
561 if (pEpClassFile->fCacheEnabled)
562 {
563 /* Init cache structure */
564 rc = pdmacFileCacheInit(pEpClassFile, pCfgNode);
565 if (RT_FAILURE(rc))
566 {
567 RTCritSectDelete(&pEpClassFile->CritSect);
568 pEpClassFile->fCacheEnabled = false;
569 LogRel(("AIOMgr: Failed to initialise the cache (rc=%Rrc), disabled caching\n"));
570 }
571 }
572 else
573 LogRel(("AIOMgr: Cache was globally disabled\n"));
574 }
575
576 return rc;
577}
578
579static void pdmacFileTerminate(PPDMASYNCCOMPLETIONEPCLASS pClassGlobals)
580{
581 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pClassGlobals;
582
583 /* All endpoints should be closed at this point. */
584 AssertMsg(!pEpClassFile->Core.pEndpointsHead, ("There are still endpoints left\n"));
585
586 /* Destroy all left async I/O managers. */
587 while (pEpClassFile->pAioMgrHead)
588 pdmacFileAioMgrDestroy(pEpClassFile, pEpClassFile->pAioMgrHead);
589
590 /* Destroy the cache. */
591 if (pEpClassFile->fCacheEnabled)
592 pdmacFileCacheDestroy(pEpClassFile);
593
594 RTCritSectDelete(&pEpClassFile->CritSect);
595}
596
597static int pdmacFileEpInitialize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint,
598 const char *pszUri, uint32_t fFlags)
599{
600 int rc = VINF_SUCCESS;
601 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
602 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->pEpClass;
603 bool fUseFailsafeManager = pEpClassFile->fFailsafe;
604
605 AssertMsgReturn((fFlags & ~(PDMACEP_FILE_FLAGS_READ_ONLY | PDMACEP_FILE_FLAGS_CACHING)) == 0,
606 ("PDMAsyncCompletion: Invalid flag specified\n"), VERR_INVALID_PARAMETER);
607
608 unsigned fFileFlags = fFlags & PDMACEP_FILE_FLAGS_READ_ONLY
609 ? RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE
610 : RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE;
611
612 if (!pEpClassFile->fFailsafe)
613 {
614 fFileFlags |= (RTFILE_O_ASYNC_IO | RTFILE_O_WRITE_THROUGH);
615
616 /*
617 * We only disable the cache if the size of the file is a multiple of 512.
618 * Certain hosts like Windows, Linux and Solaris require that transfer sizes
619 * are aligned to the volume sector size.
620 * If not we just make sure that the data is written to disk with RTFILE_O_WRITE_THROUGH
621 * which will trash the host cache but ensures that the host cache will not
622 * contain dirty buffers.
623 */
624 RTFILE File = NIL_RTFILE;
625
626 rc = RTFileOpen(&File, pszUri, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
627 if (RT_SUCCESS(rc))
628 {
629 uint64_t cbSize;
630
631 rc = RTFileGetSize(File, &cbSize);
632 if (RT_SUCCESS(rc) && ((cbSize % 512) == 0))
633 {
634 fFileFlags &= ~RTFILE_O_WRITE_THROUGH;
635
636#if defined(RT_OS_LINUX)
637 AssertMsg(!pEpClassFile->fHostCacheEnabled, ("Host cache + async I/O is not supported on Linux\n"));
638 fFileFlags |= RTFILE_O_NO_CACHE;
639#else
640 if (!pEpClassFile->fHostCacheEnabled)
641 fFileFlags |= RTFILE_O_NO_CACHE;
642#endif
643 }
644
645 pEpFile->cbFile = cbSize;
646
647 RTFileClose(File);
648 }
649 }
650
651 /* Open with final flags. */
652 rc = RTFileOpen(&pEpFile->File, pszUri, fFileFlags);
653 if ((rc == VERR_INVALID_FUNCTION) || (rc == VERR_INVALID_PARAMETER))
654 {
655 LogRel(("pdmacFileEpInitialize: RTFileOpen %s / %08x failed with %Rrc\n",
656 pszUri, fFileFlags, rc));
657 /*
658 * Solaris doesn't support directio on ZFS so far. :-\
659 * Trying to enable it returns VERR_INVALID_FUNCTION
660 * (ENOTTY). Remove it and hope for the best.
661 * ZFS supports write throttling in case applications
662 * write more data than can be synced to the disk
663 * without blocking the whole application.
664 *
665 * On Linux we have the same problem with cifs.
666 * Have to disable async I/O here too because it requires O_DIRECT.
667 */
668 fFileFlags &= ~RTFILE_O_NO_CACHE;
669
670#ifdef RT_OS_LINUX
671 fFileFlags &= ~RTFILE_O_ASYNC_IO;
672 fUseFailsafeManager = true;
673#endif
674
675 /* Open again. */
676 rc = RTFileOpen(&pEpFile->File, pszUri, fFileFlags);
677
678 if (RT_FAILURE(rc))
679 {
680 LogRel(("pdmacFileEpInitialize: RTFileOpen %s / %08x failed AGAIN(!) with %Rrc\n",
681 pszUri, fFileFlags, rc));
682 }
683 }
684
685 if (RT_SUCCESS(rc))
686 {
687 pEpFile->fFlags = fFileFlags;
688
689 rc = RTFileGetSize(pEpFile->File, (uint64_t *)&pEpFile->cbFile);
690 if (RT_SUCCESS(rc) && (pEpFile->cbFile == 0))
691 {
692 /* Could be a block device */
693 rc = RTFileSeek(pEpFile->File, 0, RTFILE_SEEK_END, (uint64_t *)&pEpFile->cbFile);
694 }
695
696 if (RT_SUCCESS(rc))
697 {
698 /* Initialize the segment cache */
699 rc = MMR3HeapAllocZEx(pEpClassFile->Core.pVM, MM_TAG_PDM_ASYNC_COMPLETION,
700 sizeof(PDMACTASKFILE),
701 (void **)&pEpFile->pTasksFreeHead);
702 if (RT_SUCCESS(rc))
703 {
704 PPDMACEPFILEMGR pAioMgr = NULL;
705
706 pEpFile->pTasksFreeTail = pEpFile->pTasksFreeHead;
707 pEpFile->cTasksCached = 0;
708
709 if (fUseFailsafeManager)
710 {
711 /* Safe mode. Every file has its own async I/O manager. */
712 rc = pdmacFileAioMgrCreate(pEpClassFile, &pAioMgr, true);
713 AssertRC(rc);
714 }
715 else
716 {
717 if ( (fFlags & PDMACEP_FILE_FLAGS_CACHING)
718 && (pEpClassFile->fCacheEnabled))
719 {
720 pEpFile->fCaching = true;
721 rc = pdmacFileEpCacheInit(pEpFile, pEpClassFile);
722 if (RT_FAILURE(rc))
723 {
724 LogRel(("AIOMgr: Endpoint for \"%s\" was opened with caching but initializing cache failed. Disabled caching\n", pszUri));
725 pEpFile->fCaching = false;
726 }
727 }
728
729 pAioMgr = pEpClassFile->pAioMgrHead;
730
731 /* Check for an idling not failsafe one or create new if not found */
732 while (pAioMgr && pAioMgr->fFailsafe)
733 pAioMgr = pAioMgr->pNext;
734
735 if (!pAioMgr)
736 {
737 rc = pdmacFileAioMgrCreate(pEpClassFile, &pAioMgr, false);
738 AssertRC(rc);
739 }
740 }
741
742 pEpFile->AioMgr.pTreeRangesLocked = (PAVLRFOFFTREE)RTMemAllocZ(sizeof(AVLRFOFFTREE));
743 if (!pEpFile->AioMgr.pTreeRangesLocked)
744 rc = VERR_NO_MEMORY;
745 else
746 {
747 pEpFile->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE;
748
749 /* Assign the endpoint to the thread. */
750 rc = pdmacFileAioMgrAddEndpoint(pAioMgr, pEpFile);
751 if (RT_FAILURE(rc))
752 {
753 RTMemFree(pEpFile->AioMgr.pTreeRangesLocked);
754 MMR3HeapFree(pEpFile->pTasksFreeHead);
755 }
756 }
757 }
758 }
759
760 if (RT_FAILURE(rc))
761 RTFileClose(pEpFile->File);
762 }
763
764#ifdef VBOX_WITH_STATISTICS
765 if (RT_SUCCESS(rc))
766 {
767 STAMR3RegisterF(pEpClassFile->Core.pVM, &pEpFile->StatRead,
768 STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS,
769 STAMUNIT_TICKS_PER_CALL, "Time taken to read from the endpoint",
770 "/PDM/AsyncCompletion/File/%s/Read", RTPathFilename(pEpFile->Core.pszUri));
771
772 STAMR3RegisterF(pEpClassFile->Core.pVM, &pEpFile->StatWrite,
773 STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS,
774 STAMUNIT_TICKS_PER_CALL, "Time taken to write to the endpoint",
775 "/PDM/AsyncCompletion/File/%s/Write", RTPathFilename(pEpFile->Core.pszUri));
776 }
777#endif
778
779 return rc;
780}
781
782static int pdmacFileEpRangesLockedDestroy(PAVLRFOFFNODECORE pNode, void *pvUser)
783{
784 AssertMsgFailed(("The locked ranges tree should be empty at that point\n"));
785 return VINF_SUCCESS;
786}
787
788static int pdmacFileEpClose(PPDMASYNCCOMPLETIONENDPOINT pEndpoint)
789{
790 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
791 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->pEpClass;
792
793 /* Make sure that all tasks finished for this endpoint. */
794 int rc = pdmacFileAioMgrCloseEndpoint(pEpFile->pAioMgr, pEpFile);
795 AssertRC(rc);
796
797 /*
798 * If the async I/O manager is in failsafe mode this is the only endpoint
799 * he processes and thus can be destroyed now.
800 */
801 if (pEpFile->pAioMgr->fFailsafe)
802 pdmacFileAioMgrDestroy(pEpClassFile, pEpFile->pAioMgr);
803
804 /* Free cached tasks. */
805 PPDMACTASKFILE pTask = pEpFile->pTasksFreeHead;
806
807 while (pTask)
808 {
809 PPDMACTASKFILE pTaskFree = pTask;
810 pTask = pTask->pNext;
811 MMR3HeapFree(pTaskFree);
812 }
813
814 /* Free the cached data. */
815 if (pEpFile->fCaching)
816 pdmacFileEpCacheDestroy(pEpFile);
817
818 /* Destroy the locked ranges tree now. */
819 RTAvlrFileOffsetDestroy(pEpFile->AioMgr.pTreeRangesLocked, pdmacFileEpRangesLockedDestroy, NULL);
820
821 RTFileClose(pEpFile->File);
822
823#ifdef VBOX_WITH_STATISTICS
824 STAMR3Deregister(pEpClassFile->Core.pVM, &pEpFile->StatRead);
825 STAMR3Deregister(pEpClassFile->Core.pVM, &pEpFile->StatWrite);
826#endif
827
828 return VINF_SUCCESS;
829}
830
831static int pdmacFileEpRead(PPDMASYNCCOMPLETIONTASK pTask,
832 PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
833 PCPDMDATASEG paSegments, size_t cSegments,
834 size_t cbRead)
835{
836 int rc = VINF_SUCCESS;
837 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
838
839 STAM_PROFILE_ADV_START(&pEpFile->StatRead, Read);
840
841 if (pEpFile->fCaching)
842 rc = pdmacFileEpCacheRead(pEpFile, (PPDMASYNCCOMPLETIONTASKFILE)pTask,
843 off, paSegments, cSegments, cbRead);
844 else
845 rc = pdmacFileEpTaskInitiate(pTask, pEndpoint, off, paSegments, cSegments, cbRead,
846 PDMACTASKFILETRANSFER_READ);
847
848 STAM_PROFILE_ADV_STOP(&pEpFile->StatRead, Read);
849
850 return rc;
851}
852
853static int pdmacFileEpWrite(PPDMASYNCCOMPLETIONTASK pTask,
854 PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
855 PCPDMDATASEG paSegments, size_t cSegments,
856 size_t cbWrite)
857{
858 int rc = VINF_SUCCESS;
859 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
860
861 if (RT_UNLIKELY(pEpFile->fReadonly))
862 return VERR_NOT_SUPPORTED;
863
864 STAM_PROFILE_ADV_START(&pEpFile->StatWrite, Write);
865
866 if (pEpFile->fCaching)
867 rc = pdmacFileEpCacheWrite(pEpFile, (PPDMASYNCCOMPLETIONTASKFILE)pTask,
868 off, paSegments, cSegments, cbWrite);
869 else
870 rc = pdmacFileEpTaskInitiate(pTask, pEndpoint, off, paSegments, cSegments, cbWrite,
871 PDMACTASKFILETRANSFER_WRITE);
872
873 STAM_PROFILE_ADV_STOP(&pEpFile->StatWrite, Write);
874
875 return rc;
876}
877
878static int pdmacFileEpFlush(PPDMASYNCCOMPLETIONTASK pTask,
879 PPDMASYNCCOMPLETIONENDPOINT pEndpoint)
880{
881 int rc = VINF_SUCCESS;
882 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
883 PPDMASYNCCOMPLETIONTASKFILE pTaskFile = (PPDMASYNCCOMPLETIONTASKFILE)pTask;
884
885 if (RT_UNLIKELY(pEpFile->fReadonly))
886 return VERR_NOT_SUPPORTED;
887
888 pTaskFile->cbTransferLeft = 0;
889
890 if (pEpFile->fCaching)
891 rc = pdmacFileEpCacheFlush(pEpFile, pTaskFile);
892 else
893 {
894 PPDMACTASKFILE pIoTask = pdmacFileTaskAlloc(pEpFile);
895 AssertPtr(pIoTask);
896
897 pIoTask->pEndpoint = pEpFile;
898 pIoTask->enmTransferType = PDMACTASKFILETRANSFER_FLUSH;
899 pIoTask->pvUser = pTaskFile;
900 pIoTask->pfnCompleted = pdmacFileEpTaskCompleted;
901 pdmacFileEpAddTask(pEpFile, pIoTask);
902 rc = VINF_AIO_TASK_PENDING;
903 }
904
905 return rc;
906}
907
908static int pdmacFileEpGetSize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, uint64_t *pcbSize)
909{
910 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
911
912 *pcbSize = ASMAtomicReadU64(&pEpFile->cbFile);
913
914 return VINF_SUCCESS;
915}
916
917const PDMASYNCCOMPLETIONEPCLASSOPS g_PDMAsyncCompletionEndpointClassFile =
918{
919 /* u32Version */
920 PDMAC_EPCLASS_OPS_VERSION,
921 /* pcszName */
922 "File",
923 /* enmClassType */
924 PDMASYNCCOMPLETIONEPCLASSTYPE_FILE,
925 /* cbEndpointClassGlobal */
926 sizeof(PDMASYNCCOMPLETIONEPCLASSFILE),
927 /* cbEndpoint */
928 sizeof(PDMASYNCCOMPLETIONENDPOINTFILE),
929 /* cbTask */
930 sizeof(PDMASYNCCOMPLETIONTASKFILE),
931 /* pfnInitialize */
932 pdmacFileInitialize,
933 /* pfnTerminate */
934 pdmacFileTerminate,
935 /* pfnEpInitialize. */
936 pdmacFileEpInitialize,
937 /* pfnEpClose */
938 pdmacFileEpClose,
939 /* pfnEpRead */
940 pdmacFileEpRead,
941 /* pfnEpWrite */
942 pdmacFileEpWrite,
943 /* pfnEpFlush */
944 pdmacFileEpFlush,
945 /* pfnEpGetSize */
946 pdmacFileEpGetSize,
947 /* u32VersionEnd */
948 PDMAC_EPCLASS_OPS_VERSION
949};
950
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