1 | /* $Id: PDMAsyncCompletionFileFailsafe.cpp 27920 2010-03-31 19:02:48Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PDM Async I/O - Transport data asynchronous in R3 using EMT.
|
---|
4 | * Simple File I/O manager.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2008 Sun Microsystems, Inc.
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
19 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
20 | * additional information or have any questions.
|
---|
21 | */
|
---|
22 | #define LOG_GROUP LOG_GROUP_PDM_ASYNC_COMPLETION
|
---|
23 | #include <iprt/types.h>
|
---|
24 | #include <iprt/assert.h>
|
---|
25 | #include <VBox/log.h>
|
---|
26 |
|
---|
27 | #include "PDMAsyncCompletionFileInternal.h"
|
---|
28 |
|
---|
29 | static int pdmacFileAioMgrFailsafeProcessEndpointTaskList(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint,
|
---|
30 | PPDMACTASKFILE pTasks)
|
---|
31 | {
|
---|
32 | int rc = VINF_SUCCESS;
|
---|
33 |
|
---|
34 | while (pTasks)
|
---|
35 | {
|
---|
36 | PPDMACTASKFILE pCurr = pTasks;
|
---|
37 |
|
---|
38 | pTasks = pTasks->pNext;
|
---|
39 |
|
---|
40 | switch (pCurr->enmTransferType)
|
---|
41 | {
|
---|
42 | case PDMACTASKFILETRANSFER_FLUSH:
|
---|
43 | {
|
---|
44 | rc = RTFileFlush(pEndpoint->File);
|
---|
45 | break;
|
---|
46 | }
|
---|
47 | case PDMACTASKFILETRANSFER_READ:
|
---|
48 | case PDMACTASKFILETRANSFER_WRITE:
|
---|
49 | {
|
---|
50 | if (pCurr->enmTransferType == PDMACTASKFILETRANSFER_READ)
|
---|
51 | {
|
---|
52 | rc = RTFileReadAt(pEndpoint->File, pCurr->Off,
|
---|
53 | pCurr->DataSeg.pvSeg,
|
---|
54 | pCurr->DataSeg.cbSeg,
|
---|
55 | NULL);
|
---|
56 | }
|
---|
57 | else
|
---|
58 | {
|
---|
59 | if (RT_UNLIKELY((uint64_t)pCurr->Off + pCurr->DataSeg.cbSeg > pEndpoint->cbFile))
|
---|
60 | {
|
---|
61 | ASMAtomicWriteU64(&pEndpoint->cbFile, pCurr->Off + pCurr->DataSeg.cbSeg);
|
---|
62 | RTFileSetSize(pEndpoint->File, pCurr->Off + pCurr->DataSeg.cbSeg);
|
---|
63 | }
|
---|
64 |
|
---|
65 | rc = RTFileWriteAt(pEndpoint->File, pCurr->Off,
|
---|
66 | pCurr->DataSeg.pvSeg,
|
---|
67 | pCurr->DataSeg.cbSeg,
|
---|
68 | NULL);
|
---|
69 | }
|
---|
70 |
|
---|
71 | break;
|
---|
72 | }
|
---|
73 | default:
|
---|
74 | AssertMsgFailed(("Invalid transfer type %d\n", pTasks->enmTransferType));
|
---|
75 | }
|
---|
76 |
|
---|
77 | pCurr->pfnCompleted(pCurr, pCurr->pvUser, rc);
|
---|
78 | pdmacFileTaskFree(pEndpoint, pCurr);
|
---|
79 | }
|
---|
80 |
|
---|
81 | return VINF_SUCCESS;
|
---|
82 | }
|
---|
83 |
|
---|
84 | static int pdmacFileAioMgrFailsafeProcessEndpoint(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
|
---|
85 | {
|
---|
86 | int rc = VINF_SUCCESS;
|
---|
87 | PPDMACTASKFILE pTasks = pEndpoint->AioMgr.pReqsPendingHead;
|
---|
88 |
|
---|
89 | pEndpoint->AioMgr.pReqsPendingHead = NULL;
|
---|
90 | pEndpoint->AioMgr.pReqsPendingTail = NULL;
|
---|
91 |
|
---|
92 | /* Process the request pending list first in case the endpoint was migrated due to an error. */
|
---|
93 | if (pTasks)
|
---|
94 | rc = pdmacFileAioMgrFailsafeProcessEndpointTaskList(pEndpoint, pTasks);
|
---|
95 |
|
---|
96 | if (RT_SUCCESS(rc))
|
---|
97 | {
|
---|
98 | pTasks = pdmacFileEpGetNewTasks(pEndpoint);
|
---|
99 |
|
---|
100 | if (pTasks)
|
---|
101 | rc = pdmacFileAioMgrFailsafeProcessEndpointTaskList(pEndpoint, pTasks);
|
---|
102 | }
|
---|
103 |
|
---|
104 | return rc;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * A fallback method in case something goes wrong with the normal
|
---|
109 | * I/O manager.
|
---|
110 | */
|
---|
111 | int pdmacFileAioMgrFailsafe(RTTHREAD ThreadSelf, void *pvUser)
|
---|
112 | {
|
---|
113 | int rc = VINF_SUCCESS;
|
---|
114 | PPDMACEPFILEMGR pAioMgr = (PPDMACEPFILEMGR)pvUser;
|
---|
115 |
|
---|
116 | while ( (pAioMgr->enmState == PDMACEPFILEMGRSTATE_RUNNING)
|
---|
117 | || (pAioMgr->enmState == PDMACEPFILEMGRSTATE_SUSPENDING))
|
---|
118 | {
|
---|
119 | if (!ASMAtomicReadBool(&pAioMgr->fWokenUp))
|
---|
120 | {
|
---|
121 | ASMAtomicWriteBool(&pAioMgr->fWaitingEventSem, true);
|
---|
122 | rc = RTSemEventWait(pAioMgr->EventSem, RT_INDEFINITE_WAIT);
|
---|
123 | ASMAtomicWriteBool(&pAioMgr->fWaitingEventSem, false);
|
---|
124 | AssertRC(rc);
|
---|
125 | }
|
---|
126 | ASMAtomicXchgBool(&pAioMgr->fWokenUp, false);
|
---|
127 |
|
---|
128 | /* Process endpoint events first. */
|
---|
129 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint = pAioMgr->pEndpointsHead;
|
---|
130 | while (pEndpoint)
|
---|
131 | {
|
---|
132 | rc = pdmacFileAioMgrFailsafeProcessEndpoint(pEndpoint);
|
---|
133 | AssertRC(rc);
|
---|
134 | pEndpoint = pEndpoint->AioMgr.pEndpointNext;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /* Now check for an external blocking event. */
|
---|
138 | if (pAioMgr->fBlockingEventPending)
|
---|
139 | {
|
---|
140 | switch (pAioMgr->enmBlockingEvent)
|
---|
141 | {
|
---|
142 | case PDMACEPFILEAIOMGRBLOCKINGEVENT_ADD_ENDPOINT:
|
---|
143 | {
|
---|
144 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpointNew = pAioMgr->BlockingEventData.AddEndpoint.pEndpoint;
|
---|
145 | AssertMsg(VALID_PTR(pEndpointNew), ("Adding endpoint event without a endpoint to add\n"));
|
---|
146 |
|
---|
147 | pEndpointNew->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE;
|
---|
148 |
|
---|
149 | pEndpointNew->AioMgr.pEndpointNext = pAioMgr->pEndpointsHead;
|
---|
150 | pEndpointNew->AioMgr.pEndpointPrev = NULL;
|
---|
151 | if (pAioMgr->pEndpointsHead)
|
---|
152 | pAioMgr->pEndpointsHead->AioMgr.pEndpointPrev = pEndpointNew;
|
---|
153 | pAioMgr->pEndpointsHead = pEndpointNew;
|
---|
154 |
|
---|
155 | pAioMgr->cEndpoints++;
|
---|
156 |
|
---|
157 | /*
|
---|
158 | * Process the task list the first time. There might be pending requests
|
---|
159 | * if the endpoint was migrated from another endpoint.
|
---|
160 | */
|
---|
161 | rc = pdmacFileAioMgrFailsafeProcessEndpoint(pEndpointNew);
|
---|
162 | AssertRC(rc);
|
---|
163 | break;
|
---|
164 | }
|
---|
165 | case PDMACEPFILEAIOMGRBLOCKINGEVENT_REMOVE_ENDPOINT:
|
---|
166 | {
|
---|
167 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpointRemove = pAioMgr->BlockingEventData.RemoveEndpoint.pEndpoint;
|
---|
168 | AssertMsg(VALID_PTR(pEndpointRemove), ("Removing endpoint event without a endpoint to remove\n"));
|
---|
169 |
|
---|
170 | pEndpointRemove->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_REMOVING;
|
---|
171 |
|
---|
172 | PPDMASYNCCOMPLETIONENDPOINTFILE pPrev = pEndpointRemove->AioMgr.pEndpointPrev;
|
---|
173 | PPDMASYNCCOMPLETIONENDPOINTFILE pNext = pEndpointRemove->AioMgr.pEndpointNext;
|
---|
174 |
|
---|
175 | if (pPrev)
|
---|
176 | pPrev->AioMgr.pEndpointNext = pNext;
|
---|
177 | else
|
---|
178 | pAioMgr->pEndpointsHead = pNext;
|
---|
179 |
|
---|
180 | if (pNext)
|
---|
181 | pNext->AioMgr.pEndpointPrev = pPrev;
|
---|
182 |
|
---|
183 | pAioMgr->cEndpoints--;
|
---|
184 | break;
|
---|
185 | }
|
---|
186 | case PDMACEPFILEAIOMGRBLOCKINGEVENT_CLOSE_ENDPOINT:
|
---|
187 | {
|
---|
188 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpointClose = pAioMgr->BlockingEventData.CloseEndpoint.pEndpoint;
|
---|
189 | AssertMsg(VALID_PTR(pEndpointClose), ("Close endpoint event without a endpoint to Close\n"));
|
---|
190 |
|
---|
191 | pEndpointClose->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_CLOSING;
|
---|
192 |
|
---|
193 | /* Make sure all tasks finished. */
|
---|
194 | rc = pdmacFileAioMgrFailsafeProcessEndpoint(pEndpointClose);
|
---|
195 | AssertRC(rc);
|
---|
196 | break;
|
---|
197 | }
|
---|
198 | case PDMACEPFILEAIOMGRBLOCKINGEVENT_SHUTDOWN:
|
---|
199 | pAioMgr->enmState = PDMACEPFILEMGRSTATE_SHUTDOWN;
|
---|
200 | break;
|
---|
201 | case PDMACEPFILEAIOMGRBLOCKINGEVENT_SUSPEND:
|
---|
202 | pAioMgr->enmState = PDMACEPFILEMGRSTATE_SUSPENDING;
|
---|
203 | break;
|
---|
204 | case PDMACEPFILEAIOMGRBLOCKINGEVENT_RESUME:
|
---|
205 | pAioMgr->enmState = PDMACEPFILEMGRSTATE_RUNNING;
|
---|
206 | break;
|
---|
207 | default:
|
---|
208 | AssertMsgFailed(("Invalid event type %d\n", pAioMgr->enmBlockingEvent));
|
---|
209 | }
|
---|
210 |
|
---|
211 | /* Release the waiting thread. */
|
---|
212 | rc = RTSemEventSignal(pAioMgr->EventSemBlock);
|
---|
213 | AssertRC(rc);
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | return rc;
|
---|
218 | }
|
---|
219 |
|
---|