1 | /** $Id: DrvVD.cpp 12463 2008-09-15 15:18:58Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * VBox storage devices:
|
---|
5 | * Media implementation for VBox disk container
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2006-2008 Sun Microsystems, Inc.
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | *
|
---|
19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
20 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
21 | * additional information or have any questions.
|
---|
22 | */
|
---|
23 |
|
---|
24 |
|
---|
25 | /*******************************************************************************
|
---|
26 | * Header files *
|
---|
27 | *******************************************************************************/
|
---|
28 | #define LOG_GROUP LOG_GROUP_DRV_VD
|
---|
29 | #include <VBox/VBoxHDD-new.h>
|
---|
30 | #include <VBox/pdmdrv.h>
|
---|
31 | #include <iprt/alloc.h>
|
---|
32 | #include <iprt/assert.h>
|
---|
33 | #include <iprt/uuid.h>
|
---|
34 | #include <iprt/file.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 | #include <iprt/cache.h>
|
---|
37 |
|
---|
38 | #include "Builtins.h"
|
---|
39 |
|
---|
40 |
|
---|
41 | /*******************************************************************************
|
---|
42 | * Defined types, constants and macros *
|
---|
43 | *******************************************************************************/
|
---|
44 |
|
---|
45 | /** Converts a pointer to VDIDISK::IMedia to a PVBOXDISK. */
|
---|
46 | #define PDMIMEDIA_2_VBOXDISK(pInterface) \
|
---|
47 | ( (PVBOXDISK)((uintptr_t)pInterface - RT_OFFSETOF(VBOXDISK, IMedia)) )
|
---|
48 |
|
---|
49 | /** Converts a pointer to PDMDRVINS::IBase to a PPDMDRVINS. */
|
---|
50 | #define PDMIBASE_2_DRVINS(pInterface) \
|
---|
51 | ( (PPDMDRVINS)((uintptr_t)pInterface - RT_OFFSETOF(PDMDRVINS, IBase)) )
|
---|
52 |
|
---|
53 | /** Converts a pointer to PDMDRVINS::IBase to a PVBOXDISK. */
|
---|
54 | #define PDMIBASE_2_VBOXDISK(pInterface) \
|
---|
55 | ( PDMINS_2_DATA(PDMIBASE_2_DRVINS(pInterface), PVBOXDISK) )
|
---|
56 |
|
---|
57 | /** Converts a pointer to VBOXDISK::IMediaAsync to a PVBOXDISK. */
|
---|
58 | #define PDMIMEDIAASYNC_2_VBOXDISK(pInterface) \
|
---|
59 | ( (PVBOXDISK)((uintptr_t)pInterface - RT_OFFSETOF(VBOXDISK, IMediaAsync)) )
|
---|
60 |
|
---|
61 | /** Converts a pointer to VBOXDISK::ITransportAsyncPort to a PVBOXDISK. */
|
---|
62 | #define PDMITRANSPORTASYNCPORT_2_VBOXDISK(pInterface) \
|
---|
63 | ( (PVBOXDISK)((uintptr_t)pInterface - RT_OFFSETOF(VBOXDISK, ITransportAsyncPort)) )
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Structure for an async I/O task.
|
---|
67 | */
|
---|
68 | typedef struct DRVVDASYNCTASK
|
---|
69 | {
|
---|
70 | /** Callback which is called on completion. */
|
---|
71 | PFNVDCOMPLETED pfnCompleted;
|
---|
72 | /** Opqaue user data which is passed on completion. */
|
---|
73 | void *pvUser;
|
---|
74 | /** Opaque user data the caller passed on transfer initiation. */
|
---|
75 | void *pvUserCaller;
|
---|
76 | } DRVVDASYNCTASK, *PDRVVDASYNCTASK;
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * VBox disk container, image information, private part.
|
---|
80 | */
|
---|
81 |
|
---|
82 | typedef struct VBOXIMAGE
|
---|
83 | {
|
---|
84 | /** Pointer to next image. */
|
---|
85 | struct VBOXIMAGE *pNext;
|
---|
86 | /** Pointer to list of VD interfaces. Per-image. */
|
---|
87 | PVDINTERFACE pVDIfsImage;
|
---|
88 | /** Common structure for the configuration information interface. */
|
---|
89 | VDINTERFACE VDIConfig;
|
---|
90 | } VBOXIMAGE, *PVBOXIMAGE;
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * VBox disk container media main structure, private part.
|
---|
94 | */
|
---|
95 | typedef struct VBOXDISK
|
---|
96 | {
|
---|
97 | /** The VBox disk container. */
|
---|
98 | PVBOXHDD pDisk;
|
---|
99 | /** The media interface. */
|
---|
100 | PDMIMEDIA IMedia;
|
---|
101 | /** Pointer to the driver instance. */
|
---|
102 | PPDMDRVINS pDrvIns;
|
---|
103 | /** Flag whether suspend has changed image open mode to read only. */
|
---|
104 | bool fTempReadOnly;
|
---|
105 | /** Pointer to list of VD interfaces. Per-disk. */
|
---|
106 | PVDINTERFACE pVDIfsDisk;
|
---|
107 | /** Common structure for the supported error interface. */
|
---|
108 | VDINTERFACE VDIError;
|
---|
109 | /** Callback table for error interface. */
|
---|
110 | VDINTERFACEERROR VDIErrorCallbacks;
|
---|
111 | /** Common structure for the supported async I/O interface. */
|
---|
112 | VDINTERFACE VDIAsyncIO;
|
---|
113 | /** Callback table for async I/O interface. */
|
---|
114 | VDINTERFACEASYNCIO VDIAsyncIOCallbacks;
|
---|
115 | /** Callback table for the configuration information interface. */
|
---|
116 | VDINTERFACECONFIG VDIConfigCallbacks;
|
---|
117 | /** Flag whether opened disk suppports async I/O operations. */
|
---|
118 | bool fAsyncIOSupported;
|
---|
119 | /** The async media interface. */
|
---|
120 | PDMIMEDIAASYNC IMediaAsync;
|
---|
121 | /** The async media port interface above. */
|
---|
122 | PPDMIMEDIAASYNCPORT pDrvMediaAsyncPort;
|
---|
123 | /** Pointer to the asynchronous media driver below. */
|
---|
124 | PPDMITRANSPORTASYNC pDrvTransportAsync;
|
---|
125 | /** Async transport port interface. */
|
---|
126 | PDMITRANSPORTASYNCPORT ITransportAsyncPort;
|
---|
127 | /** Our cache to reduce allocation overhead. */
|
---|
128 | PRTOBJCACHE pCache;
|
---|
129 | /** Pointer to the list of data we need to keep per image. */
|
---|
130 | PVBOXIMAGE pImages;
|
---|
131 | } VBOXDISK, *PVBOXDISK;
|
---|
132 |
|
---|
133 | /*******************************************************************************
|
---|
134 | * Error reporting callback *
|
---|
135 | *******************************************************************************/
|
---|
136 |
|
---|
137 | static void drvvdErrorCallback(void *pvUser, int rc, RT_SRC_POS_DECL,
|
---|
138 | const char *pszFormat, va_list va)
|
---|
139 | {
|
---|
140 | PPDMDRVINS pDrvIns = (PPDMDRVINS)pvUser;
|
---|
141 | pDrvIns->pDrvHlp->pfnVMSetErrorV(pDrvIns, rc, RT_SRC_POS_ARGS, pszFormat, va);
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Internal: allocate new image descriptor and put it in the list
|
---|
147 | */
|
---|
148 | static PVBOXIMAGE drvvdNewImage(PVBOXDISK pThis)
|
---|
149 | {
|
---|
150 | AssertPtr(pThis);
|
---|
151 | PVBOXIMAGE pImage = (PVBOXIMAGE)RTMemAllocZ(sizeof(VBOXIMAGE));
|
---|
152 | if (pImage)
|
---|
153 | {
|
---|
154 | pImage->pVDIfsImage = NULL;
|
---|
155 | PVBOXIMAGE *pp = &pThis->pImages;
|
---|
156 | while (*pp != NULL)
|
---|
157 | pp = &(*pp)->pNext;
|
---|
158 | *pp = pImage;
|
---|
159 | pImage->pNext = NULL;
|
---|
160 | }
|
---|
161 |
|
---|
162 | return pImage;
|
---|
163 | }
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Internal: free the list of images descriptors.
|
---|
167 | */
|
---|
168 | static void drvvdFreeImages(PVBOXDISK pThis)
|
---|
169 | {
|
---|
170 | while (pThis->pImages != NULL)
|
---|
171 | {
|
---|
172 | PVBOXIMAGE p = pThis->pImages;
|
---|
173 | pThis->pImages = pThis->pImages->pNext;
|
---|
174 | RTMemFree(p);
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | /*******************************************************************************
|
---|
179 | * VD Async I/O interface implementation *
|
---|
180 | *******************************************************************************/
|
---|
181 |
|
---|
182 | static DECLCALLBACK(int) drvvdAsyncIOOpen(void *pvUser, const char *pszLocation, bool fReadonly, void **ppStorage)
|
---|
183 | {
|
---|
184 | PVBOXDISK pDrvVD = (PVBOXDISK)pvUser;
|
---|
185 |
|
---|
186 | return pDrvVD->pDrvTransportAsync->pfnOpen(pDrvVD->pDrvTransportAsync, pszLocation, fReadonly, ppStorage);
|
---|
187 | }
|
---|
188 |
|
---|
189 | static DECLCALLBACK(int) drvvdAsyncIOClose(void *pvUser, void *pStorage)
|
---|
190 | {
|
---|
191 | PVBOXDISK pDrvVD = (PVBOXDISK)pvUser;
|
---|
192 |
|
---|
193 | AssertMsg(pDrvVD->pDrvTransportAsync, ("Asynchronous function called but no async transport interface below\n"));
|
---|
194 |
|
---|
195 | return pDrvVD->pDrvTransportAsync->pfnClose(pDrvVD->pDrvTransportAsync, pStorage);
|
---|
196 | }
|
---|
197 |
|
---|
198 | static DECLCALLBACK(int) drvvdAsyncIORead(void *pvUser, void *pStorage, uint64_t uOffset,
|
---|
199 | size_t cbRead, void *pvBuf, size_t *pcbRead)
|
---|
200 | {
|
---|
201 | PVBOXDISK pDrvVD = (PVBOXDISK)pvUser;
|
---|
202 |
|
---|
203 | AssertMsg(pDrvVD->pDrvTransportAsync, ("Asynchronous function called but no async transport interface below\n"));
|
---|
204 |
|
---|
205 | return pDrvVD->pDrvTransportAsync->pfnReadSynchronous(pDrvVD->pDrvTransportAsync,
|
---|
206 | pStorage,
|
---|
207 | uOffset, pvBuf, cbRead, pcbRead);
|
---|
208 | }
|
---|
209 |
|
---|
210 | static DECLCALLBACK(int) drvvdAsyncIOWrite(void *pvUser, void *pStorage, uint64_t uOffset,
|
---|
211 | size_t cbWrite, const void *pvBuf, size_t *pcbWritten)
|
---|
212 | {
|
---|
213 | PVBOXDISK pDrvVD = (PVBOXDISK)pvUser;
|
---|
214 |
|
---|
215 | AssertMsg(pDrvVD->pDrvTransportAsync, ("Asynchronous function called but no async transport interface below\n"));
|
---|
216 |
|
---|
217 | return pDrvVD->pDrvTransportAsync->pfnWriteSynchronous(pDrvVD->pDrvTransportAsync,
|
---|
218 | pStorage,
|
---|
219 | uOffset, pvBuf, cbWrite, pcbWritten);
|
---|
220 | }
|
---|
221 |
|
---|
222 | static DECLCALLBACK(int) drvvdAsyncIOFlush(void *pvUser, void *pStorage)
|
---|
223 | {
|
---|
224 | PVBOXDISK pDrvVD = (PVBOXDISK)pvUser;
|
---|
225 |
|
---|
226 | AssertMsg(pDrvVD->pDrvTransportAsync, ("Asynchronous function called but no async transport interface below\n"));
|
---|
227 |
|
---|
228 | return pDrvVD->pDrvTransportAsync->pfnFlushSynchronous(pDrvVD->pDrvTransportAsync, pStorage);
|
---|
229 | }
|
---|
230 |
|
---|
231 | static DECLCALLBACK(int) drvvdAsyncIOPrepareRead(void *pvUser, void *pStorage, uint64_t uOffset, void *pvBuf, size_t cbRead, void **ppTask)
|
---|
232 | {
|
---|
233 | PVBOXDISK pDrvVD = (PVBOXDISK)pvUser;
|
---|
234 |
|
---|
235 | AssertMsg(pDrvVD->pDrvTransportAsync, ("Asynchronous function called but no async transport interface below\n"));
|
---|
236 |
|
---|
237 | return pDrvVD->pDrvTransportAsync->pfnPrepareRead(pDrvVD->pDrvTransportAsync, pStorage, uOffset, pvBuf, cbRead, ppTask);
|
---|
238 | }
|
---|
239 |
|
---|
240 | static DECLCALLBACK(int) drvvdAsyncIOPrepareWrite(void *pvUser, void *pStorage, uint64_t uOffset, void *pvBuf, size_t cbWrite, void **ppTask)
|
---|
241 | {
|
---|
242 | PVBOXDISK pDrvVD = (PVBOXDISK)pvUser;
|
---|
243 |
|
---|
244 | AssertMsg(pDrvVD->pDrvTransportAsync, ("Asynchronous function called but no async transport interface below\n"));
|
---|
245 |
|
---|
246 | return pDrvVD->pDrvTransportAsync->pfnPrepareWrite(pDrvVD->pDrvTransportAsync, pStorage, uOffset, pvBuf, cbWrite, ppTask);
|
---|
247 | }
|
---|
248 |
|
---|
249 | static DECLCALLBACK(int) drvvdAsyncIOTasksSubmit(void *pvUser, void *apTasks[], unsigned cTasks, void *pvUser2,
|
---|
250 | void *pvUserCaller, PFNVDCOMPLETED pfnTasksCompleted)
|
---|
251 | {
|
---|
252 | PVBOXDISK pDrvVD = (PVBOXDISK)pvUser;
|
---|
253 | PDRVVDASYNCTASK pDrvVDAsyncTask;
|
---|
254 | int rc;
|
---|
255 |
|
---|
256 | AssertMsg(pDrvVD->pDrvTransportAsync, ("Asynchronous function called but no async transport interface below\n"));
|
---|
257 |
|
---|
258 | rc = RTCacheRequest(pDrvVD->pCache, (void **)&pDrvVDAsyncTask);
|
---|
259 |
|
---|
260 | if (RT_FAILURE(rc))
|
---|
261 | return rc;
|
---|
262 |
|
---|
263 | pDrvVDAsyncTask->pfnCompleted = pfnTasksCompleted;
|
---|
264 | pDrvVDAsyncTask->pvUser = pvUser2;
|
---|
265 | pDrvVDAsyncTask->pvUserCaller = pvUserCaller;
|
---|
266 |
|
---|
267 | return pDrvVD->pDrvTransportAsync->pfnTasksSubmit(pDrvVD->pDrvTransportAsync, apTasks, cTasks, pDrvVDAsyncTask);
|
---|
268 | }
|
---|
269 |
|
---|
270 | /*******************************************************************************
|
---|
271 | * VD Configuration interface implementation *
|
---|
272 | *******************************************************************************/
|
---|
273 |
|
---|
274 | static bool drvvdCfgAreValuesValid(PVDCFGNODE pNode, const char *pszzValid)
|
---|
275 | {
|
---|
276 | return CFGMR3AreValuesValid((PCFGMNODE)pNode, pszzValid);
|
---|
277 | }
|
---|
278 |
|
---|
279 | static int drvvdCfgQueryType(PVDCFGNODE pNode, const char *pszName, PVDCFGVALUETYPE penmType)
|
---|
280 | {
|
---|
281 | Assert(VDCFGVALUETYPE_INTEGER == (VDCFGVALUETYPE)CFGMVALUETYPE_INTEGER);
|
---|
282 | Assert(VDCFGVALUETYPE_STRING == (VDCFGVALUETYPE)CFGMVALUETYPE_STRING);
|
---|
283 | Assert(VDCFGVALUETYPE_BYTES == (VDCFGVALUETYPE)CFGMVALUETYPE_BYTES);
|
---|
284 | return CFGMR3QueryType((PCFGMNODE)pNode, pszName, (PCFGMVALUETYPE)penmType);
|
---|
285 | }
|
---|
286 |
|
---|
287 | static int drvvdCfgQuerySize(PVDCFGNODE pNode, const char *pszName, size_t *pcb)
|
---|
288 | {
|
---|
289 | return CFGMR3QuerySize((PCFGMNODE)pNode, pszName, pcb);
|
---|
290 | }
|
---|
291 |
|
---|
292 | static int drvvdCfgQueryInteger(PVDCFGNODE pNode, const char *pszName, uint64_t *pu64)
|
---|
293 | {
|
---|
294 | return CFGMR3QueryInteger((PCFGMNODE)pNode, pszName, pu64);
|
---|
295 | }
|
---|
296 |
|
---|
297 | static int drvvdCfgQueryIntegerDef(PVDCFGNODE pNode, const char *pszName, uint64_t *pu64, uint64_t u64Def)
|
---|
298 | {
|
---|
299 | return CFGMR3QueryInteger((PCFGMNODE)pNode, pszName, pu64);
|
---|
300 | }
|
---|
301 |
|
---|
302 | static int drvvdCfgQueryString(PVDCFGNODE pNode, const char *pszName, char *pszString, size_t cchString)
|
---|
303 | {
|
---|
304 | return CFGMR3QueryString((PCFGMNODE)pNode, pszName, pszString, cchString);
|
---|
305 | }
|
---|
306 |
|
---|
307 | static int drvvdCfgQueryStringDef(PVDCFGNODE pNode, const char *pszName, char *pszString, size_t cchString, const char *pszDef)
|
---|
308 | {
|
---|
309 | return CFGMR3QueryStringDef((PCFGMNODE)pNode, pszName, pszString, cchString, pszDef);
|
---|
310 | }
|
---|
311 |
|
---|
312 | static int drvvdCfgQueryBytes(PVDCFGNODE pNode, const char *pszName, void *pvData, size_t cbData)
|
---|
313 | {
|
---|
314 | return CFGMR3QueryBytes((PCFGMNODE)pNode, pszName, pvData, cbData);
|
---|
315 | }
|
---|
316 |
|
---|
317 |
|
---|
318 | /*******************************************************************************
|
---|
319 | * Media interface methods *
|
---|
320 | *******************************************************************************/
|
---|
321 |
|
---|
322 | /** @copydoc PDMIMEDIA::pfnRead */
|
---|
323 | static DECLCALLBACK(int) drvvdRead(PPDMIMEDIA pInterface,
|
---|
324 | uint64_t off, void *pvBuf, size_t cbRead)
|
---|
325 | {
|
---|
326 | LogFlow(("%s: off=%#llx pvBuf=%p cbRead=%d\n", __FUNCTION__,
|
---|
327 | off, pvBuf, cbRead));
|
---|
328 | PVBOXDISK pThis = PDMIMEDIA_2_VBOXDISK(pInterface);
|
---|
329 | int rc = VDRead(pThis->pDisk, off, pvBuf, cbRead);
|
---|
330 | if (RT_SUCCESS(rc))
|
---|
331 | Log2(("%s: off=%#llx pvBuf=%p cbRead=%d %.*Vhxd\n", __FUNCTION__,
|
---|
332 | off, pvBuf, cbRead, cbRead, pvBuf));
|
---|
333 | LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
|
---|
334 | return rc;
|
---|
335 | }
|
---|
336 |
|
---|
337 | /** @copydoc PDMIMEDIA::pfnWrite */
|
---|
338 | static DECLCALLBACK(int) drvvdWrite(PPDMIMEDIA pInterface,
|
---|
339 | uint64_t off, const void *pvBuf,
|
---|
340 | size_t cbWrite)
|
---|
341 | {
|
---|
342 | LogFlow(("%s: off=%#llx pvBuf=%p cbWrite=%d\n", __FUNCTION__,
|
---|
343 | off, pvBuf, cbWrite));
|
---|
344 | PVBOXDISK pThis = PDMIMEDIA_2_VBOXDISK(pInterface);
|
---|
345 | Log2(("%s: off=%#llx pvBuf=%p cbWrite=%d %.*Vhxd\n", __FUNCTION__,
|
---|
346 | off, pvBuf, cbWrite, cbWrite, pvBuf));
|
---|
347 | int rc = VDWrite(pThis->pDisk, off, pvBuf, cbWrite);
|
---|
348 | LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
|
---|
349 | return rc;
|
---|
350 | }
|
---|
351 |
|
---|
352 | /** @copydoc PDMIMEDIA::pfnFlush */
|
---|
353 | static DECLCALLBACK(int) drvvdFlush(PPDMIMEDIA pInterface)
|
---|
354 | {
|
---|
355 | LogFlow(("%s:\n", __FUNCTION__));
|
---|
356 | PVBOXDISK pThis = PDMIMEDIA_2_VBOXDISK(pInterface);
|
---|
357 | int rc = VDFlush(pThis->pDisk);
|
---|
358 | LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
|
---|
359 | return rc;
|
---|
360 | }
|
---|
361 |
|
---|
362 | /** @copydoc PDMIMEDIA::pfnGetSize */
|
---|
363 | static DECLCALLBACK(uint64_t) drvvdGetSize(PPDMIMEDIA pInterface)
|
---|
364 | {
|
---|
365 | LogFlow(("%s:\n", __FUNCTION__));
|
---|
366 | PVBOXDISK pThis = PDMIMEDIA_2_VBOXDISK(pInterface);
|
---|
367 | uint64_t cb = VDGetSize(pThis->pDisk, VD_LAST_IMAGE);
|
---|
368 | LogFlow(("%s: returns %#llx (%llu)\n", __FUNCTION__, cb, cb));
|
---|
369 | return cb;
|
---|
370 | }
|
---|
371 |
|
---|
372 | /** @copydoc PDMIMEDIA::pfnIsReadOnly */
|
---|
373 | static DECLCALLBACK(bool) drvvdIsReadOnly(PPDMIMEDIA pInterface)
|
---|
374 | {
|
---|
375 | LogFlow(("%s:\n", __FUNCTION__));
|
---|
376 | PVBOXDISK pThis = PDMIMEDIA_2_VBOXDISK(pInterface);
|
---|
377 | bool f = VDIsReadOnly(pThis->pDisk);
|
---|
378 | LogFlow(("%s: returns %d\n", __FUNCTION__, f));
|
---|
379 | return f;
|
---|
380 | }
|
---|
381 |
|
---|
382 | /** @copydoc PDMIMEDIA::pfnBiosGetPCHSGeometry */
|
---|
383 | static DECLCALLBACK(int) drvvdBiosGetPCHSGeometry(PPDMIMEDIA pInterface,
|
---|
384 | PPDMMEDIAGEOMETRY pPCHSGeometry)
|
---|
385 | {
|
---|
386 | LogFlow(("%s:\n", __FUNCTION__));
|
---|
387 | PVBOXDISK pThis = PDMIMEDIA_2_VBOXDISK(pInterface);
|
---|
388 | int rc = VDGetPCHSGeometry(pThis->pDisk, VD_LAST_IMAGE, pPCHSGeometry);
|
---|
389 | if (RT_FAILURE(rc))
|
---|
390 | {
|
---|
391 | Log(("%s: geometry not available.\n", __FUNCTION__));
|
---|
392 | rc = VERR_PDM_GEOMETRY_NOT_SET;
|
---|
393 | }
|
---|
394 | LogFlow(("%s: returns %Rrc (CHS=%d/%d/%d)\n", __FUNCTION__,
|
---|
395 | rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
|
---|
396 | return rc;
|
---|
397 | }
|
---|
398 |
|
---|
399 | /** @copydoc PDMIMEDIA::pfnBiosSetPCHSGeometry */
|
---|
400 | static DECLCALLBACK(int) drvvdBiosSetPCHSGeometry(PPDMIMEDIA pInterface,
|
---|
401 | PCPDMMEDIAGEOMETRY pPCHSGeometry)
|
---|
402 | {
|
---|
403 | LogFlow(("%s: CHS=%d/%d/%d\n", __FUNCTION__,
|
---|
404 | pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
|
---|
405 | PVBOXDISK pThis = PDMIMEDIA_2_VBOXDISK(pInterface);
|
---|
406 | int rc = VDSetPCHSGeometry(pThis->pDisk, VD_LAST_IMAGE, pPCHSGeometry);
|
---|
407 | LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
|
---|
408 | return rc;
|
---|
409 | }
|
---|
410 |
|
---|
411 | /** @copydoc PDMIMEDIA::pfnBiosGetLCHSGeometry */
|
---|
412 | static DECLCALLBACK(int) drvvdBiosGetLCHSGeometry(PPDMIMEDIA pInterface,
|
---|
413 | PPDMMEDIAGEOMETRY pLCHSGeometry)
|
---|
414 | {
|
---|
415 | LogFlow(("%s:\n", __FUNCTION__));
|
---|
416 | PVBOXDISK pThis = PDMIMEDIA_2_VBOXDISK(pInterface);
|
---|
417 | int rc = VDGetLCHSGeometry(pThis->pDisk, VD_LAST_IMAGE, pLCHSGeometry);
|
---|
418 | if (RT_FAILURE(rc))
|
---|
419 | {
|
---|
420 | Log(("%s: geometry not available.\n", __FUNCTION__));
|
---|
421 | rc = VERR_PDM_GEOMETRY_NOT_SET;
|
---|
422 | }
|
---|
423 | LogFlow(("%s: returns %Rrc (CHS=%d/%d/%d)\n", __FUNCTION__,
|
---|
424 | rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
|
---|
425 | return rc;
|
---|
426 | }
|
---|
427 |
|
---|
428 | /** @copydoc PDMIMEDIA::pfnBiosSetLCHSGeometry */
|
---|
429 | static DECLCALLBACK(int) drvvdBiosSetLCHSGeometry(PPDMIMEDIA pInterface,
|
---|
430 | PCPDMMEDIAGEOMETRY pLCHSGeometry)
|
---|
431 | {
|
---|
432 | LogFlow(("%s: CHS=%d/%d/%d\n", __FUNCTION__,
|
---|
433 | pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
|
---|
434 | PVBOXDISK pThis = PDMIMEDIA_2_VBOXDISK(pInterface);
|
---|
435 | int rc = VDSetLCHSGeometry(pThis->pDisk, VD_LAST_IMAGE, pLCHSGeometry);
|
---|
436 | LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
|
---|
437 | return rc;
|
---|
438 | }
|
---|
439 |
|
---|
440 | /** @copydoc PDMIMEDIA::pfnGetUuid */
|
---|
441 | static DECLCALLBACK(int) drvvdGetUuid(PPDMIMEDIA pInterface, PRTUUID pUuid)
|
---|
442 | {
|
---|
443 | LogFlow(("%s:\n", __FUNCTION__));
|
---|
444 | PVBOXDISK pThis = PDMIMEDIA_2_VBOXDISK(pInterface);
|
---|
445 | int rc = VDGetUuid(pThis->pDisk, 0, pUuid);
|
---|
446 | LogFlow(("%s: returns %Rrc ({%RTuuid})\n", __FUNCTION__, rc, pUuid));
|
---|
447 | return rc;
|
---|
448 | }
|
---|
449 |
|
---|
450 | /*******************************************************************************
|
---|
451 | * Async Media interface methods *
|
---|
452 | *******************************************************************************/
|
---|
453 |
|
---|
454 | static DECLCALLBACK(int) drvvdStartRead(PPDMIMEDIAASYNC pInterface, uint64_t uOffset,
|
---|
455 | PPDMDATASEG paSeg, unsigned cSeg,
|
---|
456 | size_t cbRead, void *pvUser)
|
---|
457 | {
|
---|
458 | LogFlow(("%s: uOffset=%#llx paSeg=%#p cSeg=%u cbRead=%d\n pvUser=%#p", __FUNCTION__,
|
---|
459 | uOffset, paSeg, cSeg, cbRead, pvUser));
|
---|
460 | PVBOXDISK pThis = PDMIMEDIAASYNC_2_VBOXDISK(pInterface);
|
---|
461 | int rc = VDAsyncRead(pThis->pDisk, uOffset, cbRead, paSeg, cSeg, pvUser);
|
---|
462 | LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
|
---|
463 | return rc;
|
---|
464 | }
|
---|
465 |
|
---|
466 | static DECLCALLBACK(int) drvvdStartWrite(PPDMIMEDIAASYNC pInterface, uint64_t uOffset,
|
---|
467 | PPDMDATASEG paSeg, unsigned cSeg,
|
---|
468 | size_t cbWrite, void *pvUser)
|
---|
469 | {
|
---|
470 | LogFlow(("%s: uOffset=%#llx paSeg=%#p cSeg=%u cbWrite=%d\n pvUser=%#p", __FUNCTION__,
|
---|
471 | uOffset, paSeg, cSeg, cbWrite, pvUser));
|
---|
472 | PVBOXDISK pThis = PDMIMEDIAASYNC_2_VBOXDISK(pInterface);
|
---|
473 | int rc = VDAsyncWrite(pThis->pDisk, uOffset, cbWrite, paSeg, cSeg, pvUser);
|
---|
474 | LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
|
---|
475 | return rc;
|
---|
476 | }
|
---|
477 |
|
---|
478 | /*******************************************************************************
|
---|
479 | * Async transport port interface methods *
|
---|
480 | *******************************************************************************/
|
---|
481 |
|
---|
482 | static DECLCALLBACK(int) drvvdTasksCompleteNotify(PPDMITRANSPORTASYNCPORT pInterface, void *pvUser)
|
---|
483 | {
|
---|
484 | PVBOXDISK pThis = PDMITRANSPORTASYNCPORT_2_VBOXDISK(pInterface);
|
---|
485 | PDRVVDASYNCTASK pDrvVDAsyncTask = (PDRVVDASYNCTASK)pvUser;
|
---|
486 | int rc = VINF_VDI_ASYNC_IO_FINISHED;
|
---|
487 |
|
---|
488 | /* Having a completion callback for a task is not mandatory. */
|
---|
489 | if (pDrvVDAsyncTask->pfnCompleted)
|
---|
490 | rc = pDrvVDAsyncTask->pfnCompleted(pDrvVDAsyncTask->pvUser);
|
---|
491 |
|
---|
492 | /* Check if the request is finished. */
|
---|
493 | if (rc == VINF_VDI_ASYNC_IO_FINISHED)
|
---|
494 | {
|
---|
495 | rc = pThis->pDrvMediaAsyncPort->pfnTransferCompleteNotify(pThis->pDrvMediaAsyncPort, pDrvVDAsyncTask->pvUserCaller);
|
---|
496 | }
|
---|
497 | else if (rc == VERR_VDI_ASYNC_IO_IN_PROGRESS)
|
---|
498 | rc = VINF_SUCCESS;
|
---|
499 |
|
---|
500 | rc = RTCacheInsert(pThis->pCache, pDrvVDAsyncTask);
|
---|
501 | AssertRC(rc);
|
---|
502 |
|
---|
503 | return rc;
|
---|
504 | }
|
---|
505 |
|
---|
506 |
|
---|
507 | /*******************************************************************************
|
---|
508 | * Base interface methods *
|
---|
509 | *******************************************************************************/
|
---|
510 |
|
---|
511 | /** @copydoc PDMIBASE::pfnQueryInterface */
|
---|
512 | static DECLCALLBACK(void *) drvvdQueryInterface(PPDMIBASE pInterface,
|
---|
513 | PDMINTERFACE enmInterface)
|
---|
514 | {
|
---|
515 | PPDMDRVINS pDrvIns = PDMIBASE_2_DRVINS(pInterface);
|
---|
516 | PVBOXDISK pThis = PDMINS_2_DATA(pDrvIns, PVBOXDISK);
|
---|
517 | switch (enmInterface)
|
---|
518 | {
|
---|
519 | case PDMINTERFACE_BASE:
|
---|
520 | return &pDrvIns->IBase;
|
---|
521 | case PDMINTERFACE_MEDIA:
|
---|
522 | return &pThis->IMedia;
|
---|
523 | case PDMINTERFACE_MEDIA_ASYNC:
|
---|
524 | return pThis->fAsyncIOSupported ? &pThis->IMediaAsync : NULL;
|
---|
525 | case PDMINTERFACE_TRANSPORT_ASYNC_PORT:
|
---|
526 | return &pThis->ITransportAsyncPort;
|
---|
527 | default:
|
---|
528 | return NULL;
|
---|
529 | }
|
---|
530 | }
|
---|
531 |
|
---|
532 |
|
---|
533 | /*******************************************************************************
|
---|
534 | * Driver methods *
|
---|
535 | *******************************************************************************/
|
---|
536 |
|
---|
537 |
|
---|
538 | /**
|
---|
539 | * Construct a VBox disk media driver instance.
|
---|
540 | *
|
---|
541 | * @returns VBox status.
|
---|
542 | * @param pDrvIns The driver instance data.
|
---|
543 | * If the registration structure is needed, pDrvIns->pDrvReg points to it.
|
---|
544 | * @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
|
---|
545 | * of the driver instance. It's also found in pDrvIns->pCfgHandle as it's expected
|
---|
546 | * to be used frequently in this function.
|
---|
547 | */
|
---|
548 | static DECLCALLBACK(int) drvvdConstruct(PPDMDRVINS pDrvIns,
|
---|
549 | PCFGMNODE pCfgHandle)
|
---|
550 | {
|
---|
551 | LogFlow(("%s:\n", __FUNCTION__));
|
---|
552 | PVBOXDISK pThis = PDMINS_2_DATA(pDrvIns, PVBOXDISK);
|
---|
553 | int rc = VINF_SUCCESS;
|
---|
554 | char *pszName = NULL; /**< The path of the disk image file. */
|
---|
555 | char *pszFormat = NULL; /**< The format backed to use for this image. */
|
---|
556 | bool fReadOnly; /**< True if the media is readonly. */
|
---|
557 | bool fHonorZeroWrites; /**< True if zero blocks should be written. */
|
---|
558 |
|
---|
559 | /*
|
---|
560 | * Init the static parts.
|
---|
561 | */
|
---|
562 | pDrvIns->IBase.pfnQueryInterface = drvvdQueryInterface;
|
---|
563 | pThis->pDrvIns = pDrvIns;
|
---|
564 | pThis->fTempReadOnly = false;
|
---|
565 | pThis->pDisk = NULL;
|
---|
566 |
|
---|
567 | /* IMedia */
|
---|
568 | pThis->IMedia.pfnRead = drvvdRead;
|
---|
569 | pThis->IMedia.pfnWrite = drvvdWrite;
|
---|
570 | pThis->IMedia.pfnFlush = drvvdFlush;
|
---|
571 | pThis->IMedia.pfnGetSize = drvvdGetSize;
|
---|
572 | pThis->IMedia.pfnIsReadOnly = drvvdIsReadOnly;
|
---|
573 | pThis->IMedia.pfnBiosGetPCHSGeometry = drvvdBiosGetPCHSGeometry;
|
---|
574 | pThis->IMedia.pfnBiosSetPCHSGeometry = drvvdBiosSetPCHSGeometry;
|
---|
575 | pThis->IMedia.pfnBiosGetLCHSGeometry = drvvdBiosGetLCHSGeometry;
|
---|
576 | pThis->IMedia.pfnBiosSetLCHSGeometry = drvvdBiosSetLCHSGeometry;
|
---|
577 | pThis->IMedia.pfnGetUuid = drvvdGetUuid;
|
---|
578 |
|
---|
579 | /* IMediaAsync */
|
---|
580 | pThis->IMediaAsync.pfnStartRead = drvvdStartRead;
|
---|
581 | pThis->IMediaAsync.pfnStartWrite = drvvdStartWrite;
|
---|
582 |
|
---|
583 | /* ITransportAsyncPort */
|
---|
584 | pThis->ITransportAsyncPort.pfnTaskCompleteNotify = drvvdTasksCompleteNotify;
|
---|
585 |
|
---|
586 | /* Initialize supported VD interfaces. */
|
---|
587 | pThis->pVDIfsDisk = NULL;
|
---|
588 |
|
---|
589 | pThis->VDIErrorCallbacks.cbSize = sizeof(VDINTERFACEERROR);
|
---|
590 | pThis->VDIErrorCallbacks.enmInterface = VDINTERFACETYPE_ERROR;
|
---|
591 | pThis->VDIErrorCallbacks.pfnError = drvvdErrorCallback;
|
---|
592 |
|
---|
593 | rc = VDInterfaceAdd(&pThis->VDIError, "DrvVD_VDIError", VDINTERFACETYPE_ERROR,
|
---|
594 | &pThis->VDIErrorCallbacks, pDrvIns, &pThis->pVDIfsDisk);
|
---|
595 | AssertRC(rc);
|
---|
596 |
|
---|
597 | pThis->VDIAsyncIOCallbacks.cbSize = sizeof(VDINTERFACEASYNCIO);
|
---|
598 | pThis->VDIAsyncIOCallbacks.enmInterface = VDINTERFACETYPE_ASYNCIO;
|
---|
599 | pThis->VDIAsyncIOCallbacks.pfnOpen = drvvdAsyncIOOpen;
|
---|
600 | pThis->VDIAsyncIOCallbacks.pfnClose = drvvdAsyncIOClose;
|
---|
601 | pThis->VDIAsyncIOCallbacks.pfnRead = drvvdAsyncIORead;
|
---|
602 | pThis->VDIAsyncIOCallbacks.pfnWrite = drvvdAsyncIOWrite;
|
---|
603 | pThis->VDIAsyncIOCallbacks.pfnFlush = drvvdAsyncIOFlush;
|
---|
604 | pThis->VDIAsyncIOCallbacks.pfnPrepareRead = drvvdAsyncIOPrepareRead;
|
---|
605 | pThis->VDIAsyncIOCallbacks.pfnPrepareWrite = drvvdAsyncIOPrepareWrite;
|
---|
606 | pThis->VDIAsyncIOCallbacks.pfnTasksSubmit = drvvdAsyncIOTasksSubmit;
|
---|
607 |
|
---|
608 | rc = VDInterfaceAdd(&pThis->VDIAsyncIO, "DrvVD_AsyncIO", VDINTERFACETYPE_ASYNCIO,
|
---|
609 | &pThis->VDIAsyncIOCallbacks, pThis, &pThis->pVDIfsDisk);
|
---|
610 | AssertRC(rc);
|
---|
611 |
|
---|
612 | /* This is just prepared here, the actual interface is per-image, so it's
|
---|
613 | * added later. No need to have separate callback tables. */
|
---|
614 | pThis->VDIConfigCallbacks.cbSize = sizeof(VDINTERFACECONFIG);
|
---|
615 | pThis->VDIConfigCallbacks.enmInterface = VDINTERFACETYPE_CONFIG;
|
---|
616 | pThis->VDIConfigCallbacks.pfnAreValuesValid = drvvdCfgAreValuesValid;
|
---|
617 | pThis->VDIConfigCallbacks.pfnQueryType = drvvdCfgQueryType;
|
---|
618 | pThis->VDIConfigCallbacks.pfnQuerySize = drvvdCfgQuerySize;
|
---|
619 | pThis->VDIConfigCallbacks.pfnQueryInteger = drvvdCfgQueryInteger;
|
---|
620 | pThis->VDIConfigCallbacks.pfnQueryIntegerDef = drvvdCfgQueryIntegerDef;
|
---|
621 | pThis->VDIConfigCallbacks.pfnQueryString = drvvdCfgQueryString;
|
---|
622 | pThis->VDIConfigCallbacks.pfnQueryStringDef = drvvdCfgQueryStringDef;
|
---|
623 | pThis->VDIConfigCallbacks.pfnQueryBytes = drvvdCfgQueryBytes;
|
---|
624 |
|
---|
625 | /* List of images is empty now. */
|
---|
626 | pThis->pImages = NULL;
|
---|
627 |
|
---|
628 | /* Try to attach async media port interface above.*/
|
---|
629 | pThis->pDrvMediaAsyncPort = (PPDMIMEDIAASYNCPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_MEDIA_ASYNC_PORT);
|
---|
630 |
|
---|
631 | /*
|
---|
632 | * Attach the async transport driver below of the device above us implements the
|
---|
633 | * async interface.
|
---|
634 | */
|
---|
635 | if (pThis->pDrvMediaAsyncPort)
|
---|
636 | {
|
---|
637 | /* Try to attach the driver. */
|
---|
638 | PPDMIBASE pBase;
|
---|
639 |
|
---|
640 | rc = pDrvIns->pDrvHlp->pfnAttach(pDrvIns, &pBase);
|
---|
641 | if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
|
---|
642 | {
|
---|
643 | /*
|
---|
644 | * Though the device supports async I/O the backend seems to not support it.
|
---|
645 | * Revert to non async I/O.
|
---|
646 | */
|
---|
647 | pThis->pDrvMediaAsyncPort = NULL;
|
---|
648 | }
|
---|
649 | else if (RT_FAILURE(rc))
|
---|
650 | {
|
---|
651 | AssertMsgFailed(("Failed to attach async transport driver below rc=%Rrc\n", rc));
|
---|
652 | }
|
---|
653 | else
|
---|
654 | {
|
---|
655 | /* Success query the async transport interface. */
|
---|
656 | pThis->pDrvTransportAsync = (PPDMITRANSPORTASYNC)pBase->pfnQueryInterface(pBase, PDMINTERFACE_TRANSPORT_ASYNC);
|
---|
657 | if (!pThis->pDrvTransportAsync)
|
---|
658 | {
|
---|
659 | /* Whoops. */
|
---|
660 | AssertMsgFailed(("Configuration error: No async transport interface below!\n"));
|
---|
661 | return VERR_PDM_MISSING_INTERFACE_ABOVE;
|
---|
662 | }
|
---|
663 | }
|
---|
664 | }
|
---|
665 |
|
---|
666 | /*
|
---|
667 | * Validate configuration and find all parent images.
|
---|
668 | * It's sort of up side down from the image dependency tree.
|
---|
669 | */
|
---|
670 | unsigned iLevel = 0;
|
---|
671 | PCFGMNODE pCurNode = pCfgHandle;
|
---|
672 | for (;;)
|
---|
673 | {
|
---|
674 | bool fValid;
|
---|
675 |
|
---|
676 | if (pCurNode == pCfgHandle)
|
---|
677 | {
|
---|
678 | /* Toplevel configuration additionally contains the global image
|
---|
679 | * open flags. Some might be converted to per-image flags later. */
|
---|
680 | fValid = CFGMR3AreValuesValid(pCurNode,
|
---|
681 | "Format\0Path\0"
|
---|
682 | "ReadOnly\0HonorZeroWrites\0");
|
---|
683 | }
|
---|
684 | else
|
---|
685 | {
|
---|
686 | /* All other image configurations only contain image name and
|
---|
687 | * the format information. */
|
---|
688 | fValid = CFGMR3AreValuesValid(pCurNode, "Format\0Path\0");
|
---|
689 | }
|
---|
690 | if (!fValid)
|
---|
691 | {
|
---|
692 | rc = PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES,
|
---|
693 | RT_SRC_POS, N_("DrvVD: Configuration error: keys incorrect at level %d"), iLevel);
|
---|
694 | break;
|
---|
695 | }
|
---|
696 |
|
---|
697 | PCFGMNODE pParent = CFGMR3GetChild(pCurNode, "Parent");
|
---|
698 | if (!pParent)
|
---|
699 | break;
|
---|
700 | pCurNode = pParent;
|
---|
701 | iLevel++;
|
---|
702 | }
|
---|
703 |
|
---|
704 | /*
|
---|
705 | * Open the images.
|
---|
706 | */
|
---|
707 | if (RT_SUCCESS(rc))
|
---|
708 | {
|
---|
709 | rc = VDCreate(pThis->pVDIfsDisk, &pThis->pDisk);
|
---|
710 | /* Error message is already set correctly. */
|
---|
711 | }
|
---|
712 |
|
---|
713 | while (pCurNode && RT_SUCCESS(rc))
|
---|
714 | {
|
---|
715 | /* Allocate per-image data. */
|
---|
716 | PVBOXIMAGE pImage = drvvdNewImage(pThis);
|
---|
717 | if (!pImage)
|
---|
718 | {
|
---|
719 | rc = VERR_NO_MEMORY;
|
---|
720 | break;
|
---|
721 | }
|
---|
722 |
|
---|
723 | /*
|
---|
724 | * Read the image configuration.
|
---|
725 | */
|
---|
726 | rc = CFGMR3QueryStringAlloc(pCurNode, "Path", &pszName);
|
---|
727 | if (RT_FAILURE(rc))
|
---|
728 | {
|
---|
729 | rc = PDMDRV_SET_ERROR(pDrvIns, rc,
|
---|
730 | N_("DrvVD: Configuration error: Querying \"Path\" as string failed"));
|
---|
731 | break;
|
---|
732 | }
|
---|
733 |
|
---|
734 | rc = CFGMR3QueryStringAlloc(pCfgHandle, "Format", &pszFormat);
|
---|
735 | if (RT_FAILURE(rc))
|
---|
736 | {
|
---|
737 | rc = PDMDRV_SET_ERROR(pDrvIns, rc,
|
---|
738 | N_("DrvVD: Configuration error: Querying \"Format\" as string failed"));
|
---|
739 | break;
|
---|
740 | }
|
---|
741 |
|
---|
742 | if (iLevel == 0)
|
---|
743 | {
|
---|
744 | rc = CFGMR3QueryBool(pCurNode, "ReadOnly", &fReadOnly);
|
---|
745 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
746 | fReadOnly = false;
|
---|
747 | else if (RT_FAILURE(rc))
|
---|
748 | {
|
---|
749 | rc = PDMDRV_SET_ERROR(pDrvIns, rc,
|
---|
750 | N_("DrvVD: Configuration error: Querying \"ReadOnly\" as boolean failed"));
|
---|
751 | break;
|
---|
752 | }
|
---|
753 |
|
---|
754 | rc = CFGMR3QueryBool(pCfgHandle, "HonorZeroWrites", &fHonorZeroWrites);
|
---|
755 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
756 | fHonorZeroWrites = false;
|
---|
757 | else if (RT_FAILURE(rc))
|
---|
758 | {
|
---|
759 | rc = PDMDRV_SET_ERROR(pDrvIns, rc,
|
---|
760 | N_("DrvVD: Configuration error: Querying \"HonorZeroWrites\" as boolean failed"));
|
---|
761 | break;
|
---|
762 | }
|
---|
763 | }
|
---|
764 | else
|
---|
765 | {
|
---|
766 | fReadOnly = true;
|
---|
767 | fHonorZeroWrites = false;
|
---|
768 | }
|
---|
769 |
|
---|
770 | PCFGMNODE pCfg = CFGMR3GetChild(pCurNode, "VDConfig");
|
---|
771 | rc = VDInterfaceAdd(&pImage->VDIConfig, "DrvVD_Config", VDINTERFACETYPE_CONFIG,
|
---|
772 | &pThis->VDIConfigCallbacks, pCfg, &pImage->pVDIfsImage);
|
---|
773 | AssertRC(rc);
|
---|
774 |
|
---|
775 | /*
|
---|
776 | * Open the image.
|
---|
777 | */
|
---|
778 | unsigned uOpenFlags;
|
---|
779 | if (fReadOnly)
|
---|
780 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
781 | else
|
---|
782 | uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
783 | if (fHonorZeroWrites)
|
---|
784 | uOpenFlags |= VD_OPEN_FLAGS_HONOR_ZEROES;
|
---|
785 | if (pThis->pDrvMediaAsyncPort)
|
---|
786 | uOpenFlags |= VD_OPEN_FLAGS_ASYNC_IO;
|
---|
787 |
|
---|
788 | /** Try to open backend in asyc I/O mode first. */
|
---|
789 | rc = VDOpen(pThis->pDisk, pszFormat, pszName, uOpenFlags, pImage->pVDIfsImage);
|
---|
790 | if (rc == VERR_NOT_SUPPORTED)
|
---|
791 | {
|
---|
792 | /* Seems async I/O is not supported by the backend, open in normal mode. */
|
---|
793 | uOpenFlags &= ~VD_OPEN_FLAGS_ASYNC_IO;
|
---|
794 | rc = VDOpen(pThis->pDisk, pszFormat, pszName, uOpenFlags, pImage->pVDIfsImage);
|
---|
795 | }
|
---|
796 |
|
---|
797 | if (RT_SUCCESS(rc))
|
---|
798 | Log(("%s: %d - Opened '%s' in %s mode\n", __FUNCTION__,
|
---|
799 | iLevel, pszName,
|
---|
800 | VDIsReadOnly(pThis->pDisk) ? "read-only" : "read-write"));
|
---|
801 | else
|
---|
802 | {
|
---|
803 | AssertMsgFailed(("Failed to open image '%s' rc=%Rrc\n", pszName, rc));
|
---|
804 | break;
|
---|
805 | }
|
---|
806 |
|
---|
807 |
|
---|
808 | MMR3HeapFree(pszName);
|
---|
809 | pszName = NULL;
|
---|
810 | MMR3HeapFree(pszFormat);
|
---|
811 | pszFormat = NULL;
|
---|
812 |
|
---|
813 | /* next */
|
---|
814 | iLevel--;
|
---|
815 | pCurNode = CFGMR3GetParent(pCurNode);
|
---|
816 | }
|
---|
817 |
|
---|
818 | if (RT_FAILURE(rc))
|
---|
819 | {
|
---|
820 | if (VALID_PTR(pThis->pDisk))
|
---|
821 | {
|
---|
822 | VDDestroy(pThis->pDisk);
|
---|
823 | pThis->pDisk = NULL;
|
---|
824 | }
|
---|
825 | drvvdFreeImages(pThis);
|
---|
826 | if (VALID_PTR(pszName))
|
---|
827 | MMR3HeapFree(pszName);
|
---|
828 | if (VALID_PTR(pszFormat))
|
---|
829 | MMR3HeapFree(pszFormat);
|
---|
830 |
|
---|
831 | return rc;
|
---|
832 | }
|
---|
833 |
|
---|
834 | /*
|
---|
835 | * Check for async I/O support. Every opened image has to support
|
---|
836 | * it.
|
---|
837 | */
|
---|
838 | pThis->fAsyncIOSupported = true;
|
---|
839 | for (unsigned i = 0; i < VDGetCount(pThis->pDisk); i++)
|
---|
840 | {
|
---|
841 | VDBACKENDINFO vdBackendInfo;
|
---|
842 |
|
---|
843 | rc = VDBackendInfoSingle(pThis->pDisk, i, &vdBackendInfo);
|
---|
844 | AssertRC(rc);
|
---|
845 |
|
---|
846 | if (vdBackendInfo.uBackendCaps & VD_CAP_ASYNC)
|
---|
847 | {
|
---|
848 | /*
|
---|
849 | * Backend indicates support for at least some files.
|
---|
850 | * Check if current file is supported with async I/O)
|
---|
851 | */
|
---|
852 | rc = VDImageIsAsyncIOSupported(pThis->pDisk, i, &pThis->fAsyncIOSupported);
|
---|
853 | AssertRC(rc);
|
---|
854 |
|
---|
855 | /*
|
---|
856 | * Check if current image is supported.
|
---|
857 | * If not we can stop checking because
|
---|
858 | * at least one does not support it.
|
---|
859 | */
|
---|
860 | if (!pThis->fAsyncIOSupported)
|
---|
861 | break;
|
---|
862 | }
|
---|
863 | else
|
---|
864 | {
|
---|
865 | pThis->fAsyncIOSupported = false;
|
---|
866 | break;
|
---|
867 | }
|
---|
868 | }
|
---|
869 |
|
---|
870 | /* Create cache if async I/O is supported. */
|
---|
871 | if (pThis->fAsyncIOSupported)
|
---|
872 | {
|
---|
873 | rc = RTCacheCreate(&pThis->pCache, 0, sizeof(DRVVDASYNCTASK), RTOBJCACHE_PROTECT_INSERT);
|
---|
874 | AssertMsg(RT_SUCCESS(rc), ("Failed to create cache rc=%Rrc\n", rc));
|
---|
875 | }
|
---|
876 |
|
---|
877 | LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
|
---|
878 | return rc;
|
---|
879 | }
|
---|
880 |
|
---|
881 | /**
|
---|
882 | * Destruct a driver instance.
|
---|
883 | *
|
---|
884 | * Most VM resources are freed by the VM. This callback is provided so that any non-VM
|
---|
885 | * resources can be freed correctly.
|
---|
886 | *
|
---|
887 | * @param pDrvIns The driver instance data.
|
---|
888 | */
|
---|
889 | static DECLCALLBACK(void) drvvdDestruct(PPDMDRVINS pDrvIns)
|
---|
890 | {
|
---|
891 | int rc;
|
---|
892 | PVBOXDISK pThis = PDMINS_2_DATA(pDrvIns, PVBOXDISK);
|
---|
893 | LogFlow(("%s:\n", __FUNCTION__));
|
---|
894 |
|
---|
895 | drvvdFreeImages(pThis);
|
---|
896 | if (pThis->pCache)
|
---|
897 | {
|
---|
898 | rc = RTCacheDestroy(pThis->pCache);
|
---|
899 | AssertRC(rc);
|
---|
900 | }
|
---|
901 | }
|
---|
902 |
|
---|
903 |
|
---|
904 | /**
|
---|
905 | * When the VM has been suspended we'll change the image mode to read-only
|
---|
906 | * so that main and others can read the VDIs. This is important when
|
---|
907 | * saving state and so forth.
|
---|
908 | *
|
---|
909 | * @param pDrvIns The driver instance data.
|
---|
910 | */
|
---|
911 | static DECLCALLBACK(void) drvvdSuspend(PPDMDRVINS pDrvIns)
|
---|
912 | {
|
---|
913 | LogFlow(("%s:\n", __FUNCTION__));
|
---|
914 | PVBOXDISK pThis = PDMINS_2_DATA(pDrvIns, PVBOXDISK);
|
---|
915 | if (!VDIsReadOnly(pThis->pDisk))
|
---|
916 | {
|
---|
917 | unsigned uOpenFlags;
|
---|
918 | int rc = VDGetOpenFlags(pThis->pDisk, VD_LAST_IMAGE, &uOpenFlags);
|
---|
919 | AssertRC(rc);
|
---|
920 | uOpenFlags |= VD_OPEN_FLAGS_READONLY;
|
---|
921 | rc = VDSetOpenFlags(pThis->pDisk, VD_LAST_IMAGE, uOpenFlags);
|
---|
922 | AssertRC(rc);
|
---|
923 | pThis->fTempReadOnly = true;
|
---|
924 | }
|
---|
925 | }
|
---|
926 |
|
---|
927 | /**
|
---|
928 | * Before the VM resumes we'll have to undo the read-only mode change
|
---|
929 | * done in drvvdSuspend.
|
---|
930 | *
|
---|
931 | * @param pDrvIns The driver instance data.
|
---|
932 | */
|
---|
933 | static DECLCALLBACK(void) drvvdResume(PPDMDRVINS pDrvIns)
|
---|
934 | {
|
---|
935 | LogFlow(("%s:\n", __FUNCTION__));
|
---|
936 | PVBOXDISK pThis = PDMINS_2_DATA(pDrvIns, PVBOXDISK);
|
---|
937 | if (pThis->fTempReadOnly)
|
---|
938 | {
|
---|
939 | unsigned uOpenFlags;
|
---|
940 | int rc = VDGetOpenFlags(pThis->pDisk, VD_LAST_IMAGE, &uOpenFlags);
|
---|
941 | AssertRC(rc);
|
---|
942 | uOpenFlags &= ~VD_OPEN_FLAGS_READONLY;
|
---|
943 | rc = VDSetOpenFlags(pThis->pDisk, VD_LAST_IMAGE, uOpenFlags);
|
---|
944 | AssertRC(rc);
|
---|
945 | pThis->fTempReadOnly = false;
|
---|
946 | }
|
---|
947 | }
|
---|
948 |
|
---|
949 | static DECLCALLBACK(void) drvvdPowerOff(PPDMDRVINS pDrvIns)
|
---|
950 | {
|
---|
951 | LogFlow(("%s:\n", __FUNCTION__));
|
---|
952 | PVBOXDISK pThis = PDMINS_2_DATA(pDrvIns, PVBOXDISK);
|
---|
953 |
|
---|
954 | /*
|
---|
955 | * We must close the disk here to ensure that
|
---|
956 | * the backend closes all files before the
|
---|
957 | * async transport driver is destructed.
|
---|
958 | */
|
---|
959 | int rc = VDCloseAll(pThis->pDisk);
|
---|
960 | AssertRC(rc);
|
---|
961 | }
|
---|
962 |
|
---|
963 | /**
|
---|
964 | * VBox disk container media driver registration record.
|
---|
965 | */
|
---|
966 | const PDMDRVREG g_DrvVD =
|
---|
967 | {
|
---|
968 | /* u32Version */
|
---|
969 | PDM_DRVREG_VERSION,
|
---|
970 | /* szDriverName */
|
---|
971 | "VD",
|
---|
972 | /* pszDescription */
|
---|
973 | "Generic VBox disk media driver.",
|
---|
974 | /* fFlags */
|
---|
975 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
976 | /* fClass. */
|
---|
977 | PDM_DRVREG_CLASS_MEDIA,
|
---|
978 | /* cMaxInstances */
|
---|
979 | ~0,
|
---|
980 | /* cbInstance */
|
---|
981 | sizeof(VBOXDISK),
|
---|
982 | /* pfnConstruct */
|
---|
983 | drvvdConstruct,
|
---|
984 | /* pfnDestruct */
|
---|
985 | drvvdDestruct,
|
---|
986 | /* pfnIOCtl */
|
---|
987 | NULL,
|
---|
988 | /* pfnPowerOn */
|
---|
989 | NULL,
|
---|
990 | /* pfnReset */
|
---|
991 | NULL,
|
---|
992 | /* pfnSuspend */
|
---|
993 | drvvdSuspend,
|
---|
994 | /* pfnResume */
|
---|
995 | drvvdResume,
|
---|
996 | /* pfnDetach */
|
---|
997 | NULL,
|
---|
998 | /* pfnPowerOff */
|
---|
999 | drvvdPowerOff
|
---|
1000 | };
|
---|