VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/DrvVD.cpp@ 12467

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

Failures because rc is not reset. (#3175)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 36.0 KB
Line 
1/** $Id: DrvVD.cpp 12467 2008-09-15 15:22:03Z 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 */
68typedef 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
82typedef 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 */
95typedef 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
137static 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 */
148static 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 */
168static 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
182static 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
189static 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
198static 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
210static 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
222static 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
231static 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
240static 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
249static 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
274static bool drvvdCfgAreValuesValid(PVDCFGNODE pNode, const char *pszzValid)
275{
276 return CFGMR3AreValuesValid((PCFGMNODE)pNode, pszzValid);
277}
278
279static 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
287static int drvvdCfgQuerySize(PVDCFGNODE pNode, const char *pszName, size_t *pcb)
288{
289 return CFGMR3QuerySize((PCFGMNODE)pNode, pszName, pcb);
290}
291
292static int drvvdCfgQueryInteger(PVDCFGNODE pNode, const char *pszName, uint64_t *pu64)
293{
294 return CFGMR3QueryInteger((PCFGMNODE)pNode, pszName, pu64);
295}
296
297static int drvvdCfgQueryIntegerDef(PVDCFGNODE pNode, const char *pszName, uint64_t *pu64, uint64_t u64Def)
298{
299 return CFGMR3QueryInteger((PCFGMNODE)pNode, pszName, pu64);
300}
301
302static int drvvdCfgQueryString(PVDCFGNODE pNode, const char *pszName, char *pszString, size_t cchString)
303{
304 return CFGMR3QueryString((PCFGMNODE)pNode, pszName, pszString, cchString);
305}
306
307static 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
312static 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 */
323static 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 */
338static 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 */
353static 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 */
363static 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 */
373static 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 */
383static 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 */
400static 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 */
412static 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 */
429static 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 */
441static 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
454static 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
466static 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
482static 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 */
512static 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 */
548static 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 rc = VINF_SUCCESS;
673 for (;;)
674 {
675 bool fValid;
676
677 if (pCurNode == pCfgHandle)
678 {
679 /* Toplevel configuration additionally contains the global image
680 * open flags. Some might be converted to per-image flags later. */
681 fValid = CFGMR3AreValuesValid(pCurNode,
682 "Format\0Path\0"
683 "ReadOnly\0HonorZeroWrites\0");
684 }
685 else
686 {
687 /* All other image configurations only contain image name and
688 * the format information. */
689 fValid = CFGMR3AreValuesValid(pCurNode, "Format\0Path\0");
690 }
691 if (!fValid)
692 {
693 rc = PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES,
694 RT_SRC_POS, N_("DrvVD: Configuration error: keys incorrect at level %d"), iLevel);
695 break;
696 }
697
698
699 PCFGMNODE pParent = CFGMR3GetChild(pCurNode, "Parent");
700 if (!pParent)
701 break;
702 pCurNode = pParent;
703 iLevel++;
704 }
705
706 /*
707 * Open the images.
708 */
709 if (RT_SUCCESS(rc))
710 {
711 rc = VDCreate(pThis->pVDIfsDisk, &pThis->pDisk);
712 /* Error message is already set correctly. */
713 }
714
715 while (pCurNode && RT_SUCCESS(rc))
716 {
717 /* Allocate per-image data. */
718 PVBOXIMAGE pImage = drvvdNewImage(pThis);
719 if (!pImage)
720 {
721 rc = VERR_NO_MEMORY;
722 break;
723 }
724
725 /*
726 * Read the image configuration.
727 */
728 rc = CFGMR3QueryStringAlloc(pCurNode, "Path", &pszName);
729 if (RT_FAILURE(rc))
730 {
731 rc = PDMDRV_SET_ERROR(pDrvIns, rc,
732 N_("DrvVD: Configuration error: Querying \"Path\" as string failed"));
733 break;
734 }
735
736 rc = CFGMR3QueryStringAlloc(pCfgHandle, "Format", &pszFormat);
737 if (RT_FAILURE(rc))
738 {
739 rc = PDMDRV_SET_ERROR(pDrvIns, rc,
740 N_("DrvVD: Configuration error: Querying \"Format\" as string failed"));
741 break;
742 }
743
744 if (iLevel == 0)
745 {
746 rc = CFGMR3QueryBool(pCurNode, "ReadOnly", &fReadOnly);
747 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
748 fReadOnly = false;
749 else if (RT_FAILURE(rc))
750 {
751 rc = PDMDRV_SET_ERROR(pDrvIns, rc,
752 N_("DrvVD: Configuration error: Querying \"ReadOnly\" as boolean failed"));
753 break;
754 }
755
756 rc = CFGMR3QueryBool(pCfgHandle, "HonorZeroWrites", &fHonorZeroWrites);
757 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
758 fHonorZeroWrites = false;
759 else if (RT_FAILURE(rc))
760 {
761 rc = PDMDRV_SET_ERROR(pDrvIns, rc,
762 N_("DrvVD: Configuration error: Querying \"HonorZeroWrites\" as boolean failed"));
763 break;
764 }
765 }
766 else
767 {
768 fReadOnly = true;
769 fHonorZeroWrites = false;
770 }
771
772 PCFGMNODE pCfg = CFGMR3GetChild(pCurNode, "VDConfig");
773 rc = VDInterfaceAdd(&pImage->VDIConfig, "DrvVD_Config", VDINTERFACETYPE_CONFIG,
774 &pThis->VDIConfigCallbacks, pCfg, &pImage->pVDIfsImage);
775 AssertRC(rc);
776
777 /*
778 * Open the image.
779 */
780 unsigned uOpenFlags;
781 if (fReadOnly)
782 uOpenFlags = VD_OPEN_FLAGS_READONLY;
783 else
784 uOpenFlags = VD_OPEN_FLAGS_NORMAL;
785 if (fHonorZeroWrites)
786 uOpenFlags |= VD_OPEN_FLAGS_HONOR_ZEROES;
787 if (pThis->pDrvMediaAsyncPort)
788 uOpenFlags |= VD_OPEN_FLAGS_ASYNC_IO;
789
790 /** Try to open backend in asyc I/O mode first. */
791 rc = VDOpen(pThis->pDisk, pszFormat, pszName, uOpenFlags, pImage->pVDIfsImage);
792 if (rc == VERR_NOT_SUPPORTED)
793 {
794 /* Seems async I/O is not supported by the backend, open in normal mode. */
795 uOpenFlags &= ~VD_OPEN_FLAGS_ASYNC_IO;
796 rc = VDOpen(pThis->pDisk, pszFormat, pszName, uOpenFlags, pImage->pVDIfsImage);
797 }
798
799 if (RT_SUCCESS(rc))
800 Log(("%s: %d - Opened '%s' in %s mode\n", __FUNCTION__,
801 iLevel, pszName,
802 VDIsReadOnly(pThis->pDisk) ? "read-only" : "read-write"));
803 else
804 {
805 AssertMsgFailed(("Failed to open image '%s' rc=%Rrc\n", pszName, rc));
806 break;
807 }
808
809
810 MMR3HeapFree(pszName);
811 pszName = NULL;
812 MMR3HeapFree(pszFormat);
813 pszFormat = NULL;
814
815 /* next */
816 iLevel--;
817 pCurNode = CFGMR3GetParent(pCurNode);
818 }
819
820 if (RT_FAILURE(rc))
821 {
822 if (VALID_PTR(pThis->pDisk))
823 {
824 VDDestroy(pThis->pDisk);
825 pThis->pDisk = NULL;
826 }
827 drvvdFreeImages(pThis);
828 if (VALID_PTR(pszName))
829 MMR3HeapFree(pszName);
830 if (VALID_PTR(pszFormat))
831 MMR3HeapFree(pszFormat);
832
833 return rc;
834 }
835
836 /*
837 * Check for async I/O support. Every opened image has to support
838 * it.
839 */
840 pThis->fAsyncIOSupported = true;
841 for (unsigned i = 0; i < VDGetCount(pThis->pDisk); i++)
842 {
843 VDBACKENDINFO vdBackendInfo;
844
845 rc = VDBackendInfoSingle(pThis->pDisk, i, &vdBackendInfo);
846 AssertRC(rc);
847
848 if (vdBackendInfo.uBackendCaps & VD_CAP_ASYNC)
849 {
850 /*
851 * Backend indicates support for at least some files.
852 * Check if current file is supported with async I/O)
853 */
854 rc = VDImageIsAsyncIOSupported(pThis->pDisk, i, &pThis->fAsyncIOSupported);
855 AssertRC(rc);
856
857 /*
858 * Check if current image is supported.
859 * If not we can stop checking because
860 * at least one does not support it.
861 */
862 if (!pThis->fAsyncIOSupported)
863 break;
864 }
865 else
866 {
867 pThis->fAsyncIOSupported = false;
868 break;
869 }
870 }
871
872 /* Create cache if async I/O is supported. */
873 if (pThis->fAsyncIOSupported)
874 {
875 rc = RTCacheCreate(&pThis->pCache, 0, sizeof(DRVVDASYNCTASK), RTOBJCACHE_PROTECT_INSERT);
876 AssertMsg(RT_SUCCESS(rc), ("Failed to create cache rc=%Rrc\n", rc));
877 }
878
879 LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
880 return rc;
881}
882
883/**
884 * Destruct a driver instance.
885 *
886 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
887 * resources can be freed correctly.
888 *
889 * @param pDrvIns The driver instance data.
890 */
891static DECLCALLBACK(void) drvvdDestruct(PPDMDRVINS pDrvIns)
892{
893 int rc;
894 PVBOXDISK pThis = PDMINS_2_DATA(pDrvIns, PVBOXDISK);
895 LogFlow(("%s:\n", __FUNCTION__));
896
897 drvvdFreeImages(pThis);
898 if (pThis->pCache)
899 {
900 rc = RTCacheDestroy(pThis->pCache);
901 AssertRC(rc);
902 }
903}
904
905
906/**
907 * When the VM has been suspended we'll change the image mode to read-only
908 * so that main and others can read the VDIs. This is important when
909 * saving state and so forth.
910 *
911 * @param pDrvIns The driver instance data.
912 */
913static DECLCALLBACK(void) drvvdSuspend(PPDMDRVINS pDrvIns)
914{
915 LogFlow(("%s:\n", __FUNCTION__));
916 PVBOXDISK pThis = PDMINS_2_DATA(pDrvIns, PVBOXDISK);
917 if (!VDIsReadOnly(pThis->pDisk))
918 {
919 unsigned uOpenFlags;
920 int rc = VDGetOpenFlags(pThis->pDisk, VD_LAST_IMAGE, &uOpenFlags);
921 AssertRC(rc);
922 uOpenFlags |= VD_OPEN_FLAGS_READONLY;
923 rc = VDSetOpenFlags(pThis->pDisk, VD_LAST_IMAGE, uOpenFlags);
924 AssertRC(rc);
925 pThis->fTempReadOnly = true;
926 }
927}
928
929/**
930 * Before the VM resumes we'll have to undo the read-only mode change
931 * done in drvvdSuspend.
932 *
933 * @param pDrvIns The driver instance data.
934 */
935static DECLCALLBACK(void) drvvdResume(PPDMDRVINS pDrvIns)
936{
937 LogFlow(("%s:\n", __FUNCTION__));
938 PVBOXDISK pThis = PDMINS_2_DATA(pDrvIns, PVBOXDISK);
939 if (pThis->fTempReadOnly)
940 {
941 unsigned uOpenFlags;
942 int rc = VDGetOpenFlags(pThis->pDisk, VD_LAST_IMAGE, &uOpenFlags);
943 AssertRC(rc);
944 uOpenFlags &= ~VD_OPEN_FLAGS_READONLY;
945 rc = VDSetOpenFlags(pThis->pDisk, VD_LAST_IMAGE, uOpenFlags);
946 AssertRC(rc);
947 pThis->fTempReadOnly = false;
948 }
949}
950
951static DECLCALLBACK(void) drvvdPowerOff(PPDMDRVINS pDrvIns)
952{
953 LogFlow(("%s:\n", __FUNCTION__));
954 PVBOXDISK pThis = PDMINS_2_DATA(pDrvIns, PVBOXDISK);
955
956 /*
957 * We must close the disk here to ensure that
958 * the backend closes all files before the
959 * async transport driver is destructed.
960 */
961 int rc = VDCloseAll(pThis->pDisk);
962 AssertRC(rc);
963}
964
965/**
966 * VBox disk container media driver registration record.
967 */
968const PDMDRVREG g_DrvVD =
969{
970 /* u32Version */
971 PDM_DRVREG_VERSION,
972 /* szDriverName */
973 "VD",
974 /* pszDescription */
975 "Generic VBox disk media driver.",
976 /* fFlags */
977 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
978 /* fClass. */
979 PDM_DRVREG_CLASS_MEDIA,
980 /* cMaxInstances */
981 ~0,
982 /* cbInstance */
983 sizeof(VBOXDISK),
984 /* pfnConstruct */
985 drvvdConstruct,
986 /* pfnDestruct */
987 drvvdDestruct,
988 /* pfnIOCtl */
989 NULL,
990 /* pfnPowerOn */
991 NULL,
992 /* pfnReset */
993 NULL,
994 /* pfnSuspend */
995 drvvdSuspend,
996 /* pfnResume */
997 drvvdResume,
998 /* pfnDetach */
999 NULL,
1000 /* pfnPowerOff */
1001 drvvdPowerOff
1002};
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