1 | /* $Id: PDMAsyncCompletionFileFailsafe.cpp 25270 2009-12-09 14:49:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PDM Async I/O - Transport data asynchronous in R3 using EMT.
|
---|
4 | * Failsafe 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 | #define RT_STRICT
|
---|
24 | #include <iprt/types.h>
|
---|
25 | #include <iprt/assert.h>
|
---|
26 | #include <VBox/log.h>
|
---|
27 |
|
---|
28 | #include "PDMAsyncCompletionFileInternal.h"
|
---|
29 |
|
---|
30 | static int pdmacFileAioMgrFailsafeProcessEndpointTaskList(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint,
|
---|
31 | PPDMACTASKFILE pTasks)
|
---|
32 | {
|
---|
33 | int rc = VINF_SUCCESS;
|
---|
34 |
|
---|
35 | while (pTasks)
|
---|
36 | {
|
---|
37 | PPDMACTASKFILE pCurr = pTasks;
|
---|
38 |
|
---|
39 | pTasks = pTasks->pNext;
|
---|
40 |
|
---|
41 | switch (pCurr->enmTransferType)
|
---|
42 | {
|
---|
43 | case PDMACTASKFILETRANSFER_FLUSH:
|
---|
44 | {
|
---|
45 | rc = RTFileFlush(pEndpoint->File);
|
---|
46 | break;
|
---|
47 | }
|
---|
48 | case PDMACTASKFILETRANSFER_READ:
|
---|
49 | case PDMACTASKFILETRANSFER_WRITE:
|
---|
50 | {
|
---|
51 | if (pCurr->enmTransferType == PDMACTASKFILETRANSFER_READ)
|
---|
52 | {
|
---|
53 | rc = RTFileReadAt(pEndpoint->File, pCurr->Off,
|
---|
54 | pCurr->DataSeg.pvSeg,
|
---|
55 | pCurr->DataSeg.cbSeg,
|
---|
56 | NULL);
|
---|
57 | }
|
---|
58 | else
|
---|
59 | {
|
---|
60 | if (RT_UNLIKELY((uint64_t)pCurr->Off + pCurr->DataSeg.cbSeg > pEndpoint->cbFile))
|
---|
61 | {
|
---|
62 | ASMAtomicWriteU64(&pEndpoint->cbFile, pCurr->Off + pCurr->DataSeg.cbSeg);
|
---|
63 | RTFileSetSize(pEndpoint->File, pCurr->Off + pCurr->DataSeg.cbSeg);
|
---|
64 | }
|
---|
65 |
|
---|
66 | rc = RTFileWriteAt(pEndpoint->File, pCurr->Off,
|
---|
67 | pCurr->DataSeg.pvSeg,
|
---|
68 | pCurr->DataSeg.cbSeg,
|
---|
69 | NULL);
|
---|
70 | }
|
---|
71 |
|
---|
72 | break;
|
---|
73 | }
|
---|
74 | default:
|
---|
75 | AssertMsgFailed(("Invalid transfer type %d\n", pTasks->enmTransferType));
|
---|
76 | }
|
---|
77 |
|
---|
78 | AssertRC(rc);
|
---|
79 |
|
---|
80 | pCurr->pfnCompleted(pCurr, pCurr->pvUser);
|
---|
81 | pdmacFileTaskFree(pEndpoint, pCurr);
|
---|
82 | }
|
---|
83 |
|
---|
84 | return rc;
|
---|
85 | }
|
---|
86 |
|
---|
87 | static int pdmacFileAioMgrFailsafeProcessEndpoint(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
|
---|
88 | {
|
---|
89 | int rc = VINF_SUCCESS;
|
---|
90 | PPDMACTASKFILE pTasks = pEndpoint->AioMgr.pReqsPendingHead;
|
---|
91 |
|
---|
92 | pEndpoint->AioMgr.pReqsPendingHead = NULL;
|
---|
93 | pEndpoint->AioMgr.pReqsPendingTail = NULL;
|
---|
94 |
|
---|
95 | /* Process the request pending list first in case the endpoint was migrated due to an error. */
|
---|
96 | if (pTasks)
|
---|
97 | rc = pdmacFileAioMgrFailsafeProcessEndpointTaskList(pEndpoint, pTasks);
|
---|
98 |
|
---|
99 | if (RT_SUCCESS(rc))
|
---|
100 | {
|
---|
101 | pTasks = pdmacFileEpGetNewTasks(pEndpoint);
|
---|
102 |
|
---|
103 | if (pTasks)
|
---|
104 | rc = pdmacFileAioMgrFailsafeProcessEndpointTaskList(pEndpoint, pTasks);
|
---|
105 | }
|
---|
106 |
|
---|
107 | return rc;
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * A fallback method in case something goes wrong with the normal
|
---|
112 | * I/O manager.
|
---|
113 | */
|
---|
114 | int pdmacFileAioMgrFailsafe(RTTHREAD ThreadSelf, void *pvUser)
|
---|
115 | {
|
---|
116 | int rc = VINF_SUCCESS;
|
---|
117 | PPDMACEPFILEMGR pAioMgr = (PPDMACEPFILEMGR)pvUser;
|
---|
118 |
|
---|
119 | while ( (pAioMgr->enmState == PDMACEPFILEMGRSTATE_RUNNING)
|
---|
120 | || (pAioMgr->enmState == PDMACEPFILEMGRSTATE_SUSPENDING))
|
---|
121 | {
|
---|
122 | if (!ASMAtomicReadBool(&pAioMgr->fWokenUp))
|
---|
123 | {
|
---|
124 | ASMAtomicWriteBool(&pAioMgr->fWaitingEventSem, true);
|
---|
125 | rc = RTSemEventWait(pAioMgr->EventSem, RT_INDEFINITE_WAIT);
|
---|
126 | ASMAtomicWriteBool(&pAioMgr->fWaitingEventSem, false);
|
---|
127 | AssertRC(rc);
|
---|
128 | }
|
---|
129 | ASMAtomicXchgBool(&pAioMgr->fWokenUp, false);
|
---|
130 |
|
---|
131 | /* Process endpoint events first. */
|
---|
132 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint = pAioMgr->pEndpointsHead;
|
---|
133 | while (pEndpoint)
|
---|
134 | {
|
---|
135 | rc = pdmacFileAioMgrFailsafeProcessEndpoint(pEndpoint);
|
---|
136 | AssertRC(rc);
|
---|
137 | pEndpoint = pEndpoint->AioMgr.pEndpointNext;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /* Now check for an external blocking event. */
|
---|
141 | if (pAioMgr->fBlockingEventPending)
|
---|
142 | {
|
---|
143 | switch (pAioMgr->enmBlockingEvent)
|
---|
144 | {
|
---|
145 | case PDMACEPFILEAIOMGRBLOCKINGEVENT_ADD_ENDPOINT:
|
---|
146 | {
|
---|
147 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpointNew = pAioMgr->BlockingEventData.AddEndpoint.pEndpoint;
|
---|
148 | AssertMsg(VALID_PTR(pEndpointNew), ("Adding endpoint event without a endpoint to add\n"));
|
---|
149 |
|
---|
150 | pEndpointNew->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE;
|
---|
151 |
|
---|
152 | pEndpointNew->AioMgr.pEndpointNext = pAioMgr->pEndpointsHead;
|
---|
153 | pEndpointNew->AioMgr.pEndpointPrev = NULL;
|
---|
154 | if (pAioMgr->pEndpointsHead)
|
---|
155 | pAioMgr->pEndpointsHead->AioMgr.pEndpointPrev = pEndpointNew;
|
---|
156 | pAioMgr->pEndpointsHead = pEndpointNew;
|
---|
157 |
|
---|
158 | pAioMgr->cEndpoints++;
|
---|
159 |
|
---|
160 | /*
|
---|
161 | * Process the task list the first time. There might be pending requests
|
---|
162 | * if the endpoint was migrated from another endpoint.
|
---|
163 | */
|
---|
164 | rc = pdmacFileAioMgrFailsafeProcessEndpoint(pEndpointNew);
|
---|
165 | AssertRC(rc);
|
---|
166 | break;
|
---|
167 | }
|
---|
168 | case PDMACEPFILEAIOMGRBLOCKINGEVENT_REMOVE_ENDPOINT:
|
---|
169 | {
|
---|
170 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpointRemove = pAioMgr->BlockingEventData.RemoveEndpoint.pEndpoint;
|
---|
171 | AssertMsg(VALID_PTR(pEndpointRemove), ("Removing endpoint event without a endpoint to remove\n"));
|
---|
172 |
|
---|
173 | pEndpointRemove->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_REMOVING;
|
---|
174 |
|
---|
175 | PPDMASYNCCOMPLETIONENDPOINTFILE pPrev = pEndpointRemove->AioMgr.pEndpointPrev;
|
---|
176 | PPDMASYNCCOMPLETIONENDPOINTFILE pNext = pEndpointRemove->AioMgr.pEndpointNext;
|
---|
177 |
|
---|
178 | if (pPrev)
|
---|
179 | pPrev->AioMgr.pEndpointNext = pNext;
|
---|
180 | else
|
---|
181 | pAioMgr->pEndpointsHead = pNext;
|
---|
182 |
|
---|
183 | if (pNext)
|
---|
184 | pNext->AioMgr.pEndpointPrev = pPrev;
|
---|
185 |
|
---|
186 | pAioMgr->cEndpoints--;
|
---|
187 | break;
|
---|
188 | }
|
---|
189 | case PDMACEPFILEAIOMGRBLOCKINGEVENT_CLOSE_ENDPOINT:
|
---|
190 | {
|
---|
191 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpointClose = pAioMgr->BlockingEventData.CloseEndpoint.pEndpoint;
|
---|
192 | AssertMsg(VALID_PTR(pEndpointClose), ("Close endpoint event without a endpoint to Close\n"));
|
---|
193 |
|
---|
194 | pEndpointClose->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_CLOSING;
|
---|
195 |
|
---|
196 | /* Make sure all tasks finished. */
|
---|
197 | rc = pdmacFileAioMgrFailsafeProcessEndpoint(pEndpointClose);
|
---|
198 | AssertRC(rc);
|
---|
199 | break;
|
---|
200 | }
|
---|
201 | case PDMACEPFILEAIOMGRBLOCKINGEVENT_SHUTDOWN:
|
---|
202 | pAioMgr->enmState = PDMACEPFILEMGRSTATE_SHUTDOWN;
|
---|
203 | break;
|
---|
204 | case PDMACEPFILEAIOMGRBLOCKINGEVENT_SUSPEND:
|
---|
205 | pAioMgr->enmState = PDMACEPFILEMGRSTATE_SUSPENDING;
|
---|
206 | break;
|
---|
207 | case PDMACEPFILEAIOMGRBLOCKINGEVENT_RESUME:
|
---|
208 | pAioMgr->enmState = PDMACEPFILEMGRSTATE_RUNNING;
|
---|
209 | break;
|
---|
210 | default:
|
---|
211 | AssertMsgFailed(("Invalid event type %d\n", pAioMgr->enmBlockingEvent));
|
---|
212 | }
|
---|
213 |
|
---|
214 | /* Release the waiting thread. */
|
---|
215 | rc = RTSemEventSignal(pAioMgr->EventSemBlock);
|
---|
216 | AssertRC(rc);
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | return rc;
|
---|
221 | }
|
---|
222 |
|
---|