VirtualBox

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

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

Storage/DrvVD: warning fix

  • 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 11459 2008-08-18 11:31:25Z 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 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
832 /*
833 * Check for async I/O support. Every opened image has to support
834 * it.
835 */
836 pThis->fAsyncIOSupported = true;
837 for (unsigned i = 0; i < VDGetCount(pThis->pDisk); i++)
838 {
839 VDBACKENDINFO vdBackendInfo;
840
841 rc = VDBackendInfoSingle(pThis->pDisk, i, &vdBackendInfo);
842 AssertRC(rc);
843
844 if (vdBackendInfo.uBackendCaps & VD_CAP_ASYNC)
845 {
846 /*
847 * Backend indicates support for at least some files.
848 * Check if current file is supported with async I/O)
849 */
850 rc = VDImageIsAsyncIOSupported(pThis->pDisk, i, &pThis->fAsyncIOSupported);
851 AssertRC(rc);
852
853 /*
854 * Check if current image is supported.
855 * If not we can stop checking because
856 * at least one does not support it.
857 */
858 if (!pThis->fAsyncIOSupported)
859 break;
860 }
861 else
862 {
863 pThis->fAsyncIOSupported = false;
864 break;
865 }
866 }
867
868 /* Create cache if async I/O is supported. */
869 if (pThis->fAsyncIOSupported)
870 {
871 rc = RTCacheCreate(&pThis->pCache, 0, sizeof(DRVVDASYNCTASK), RTOBJCACHE_PROTECT_INSERT);
872 AssertMsg(RT_SUCCESS(rc), ("Failed to create cache rc=%Rrc\n", rc));
873 }
874
875 LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
876 return rc;
877}
878
879/**
880 * Destruct a driver instance.
881 *
882 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
883 * resources can be freed correctly.
884 *
885 * @param pDrvIns The driver instance data.
886 */
887static DECLCALLBACK(void) drvvdDestruct(PPDMDRVINS pDrvIns)
888{
889 int rc;
890 PVBOXDISK pThis = PDMINS_2_DATA(pDrvIns, PVBOXDISK);
891 LogFlow(("%s:\n", __FUNCTION__));
892
893 drvvdFreeImages(pThis);
894 if (pThis->pCache)
895 {
896 rc = RTCacheDestroy(pThis->pCache);
897 AssertRC(rc);
898 }
899}
900
901
902/**
903 * When the VM has been suspended we'll change the image mode to read-only
904 * so that main and others can read the VDIs. This is important when
905 * saving state and so forth.
906 *
907 * @param pDrvIns The driver instance data.
908 */
909static DECLCALLBACK(void) drvvdSuspend(PPDMDRVINS pDrvIns)
910{
911 LogFlow(("%s:\n", __FUNCTION__));
912 PVBOXDISK pThis = PDMINS_2_DATA(pDrvIns, PVBOXDISK);
913 if (!VDIsReadOnly(pThis->pDisk))
914 {
915 unsigned uOpenFlags;
916 int rc = VDGetOpenFlags(pThis->pDisk, VD_LAST_IMAGE, &uOpenFlags);
917 AssertRC(rc);
918 uOpenFlags |= VD_OPEN_FLAGS_READONLY;
919 rc = VDSetOpenFlags(pThis->pDisk, VD_LAST_IMAGE, uOpenFlags);
920 AssertRC(rc);
921 pThis->fTempReadOnly = true;
922 }
923}
924
925/**
926 * Before the VM resumes we'll have to undo the read-only mode change
927 * done in drvvdSuspend.
928 *
929 * @param pDrvIns The driver instance data.
930 */
931static DECLCALLBACK(void) drvvdResume(PPDMDRVINS pDrvIns)
932{
933 LogFlow(("%s:\n", __FUNCTION__));
934 PVBOXDISK pThis = PDMINS_2_DATA(pDrvIns, PVBOXDISK);
935 if (pThis->fTempReadOnly)
936 {
937 unsigned uOpenFlags;
938 int rc = VDGetOpenFlags(pThis->pDisk, VD_LAST_IMAGE, &uOpenFlags);
939 AssertRC(rc);
940 uOpenFlags &= ~VD_OPEN_FLAGS_READONLY;
941 rc = VDSetOpenFlags(pThis->pDisk, VD_LAST_IMAGE, uOpenFlags);
942 AssertRC(rc);
943 pThis->fTempReadOnly = false;
944 }
945}
946
947static DECLCALLBACK(void) drvvdPowerOff(PPDMDRVINS pDrvIns)
948{
949 LogFlow(("%s:\n", __FUNCTION__));
950 PVBOXDISK pThis = PDMINS_2_DATA(pDrvIns, PVBOXDISK);
951
952 /*
953 * We must close the disk here to ensure that
954 * the backend closes all files before the
955 * async transport driver is destructed.
956 */
957 int rc = VDCloseAll(pThis->pDisk);
958 AssertRC(rc);
959}
960
961/**
962 * VBox disk container media driver registration record.
963 */
964const PDMDRVREG g_DrvVD =
965{
966 /* u32Version */
967 PDM_DRVREG_VERSION,
968 /* szDriverName */
969 "VD",
970 /* pszDescription */
971 "Generic VBox disk media driver.",
972 /* fFlags */
973 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
974 /* fClass. */
975 PDM_DRVREG_CLASS_MEDIA,
976 /* cMaxInstances */
977 ~0,
978 /* cbInstance */
979 sizeof(VBOXDISK),
980 /* pfnConstruct */
981 drvvdConstruct,
982 /* pfnDestruct */
983 drvvdDestruct,
984 /* pfnIOCtl */
985 NULL,
986 /* pfnPowerOn */
987 NULL,
988 /* pfnReset */
989 NULL,
990 /* pfnSuspend */
991 drvvdSuspend,
992 /* pfnResume */
993 drvvdResume,
994 /* pfnDetach */
995 NULL,
996 /* pfnPowerOff */
997 drvvdPowerOff
998};
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