DrvBlock.cpp revision c911dd50bdd3aca93812bb0725f14c30e4b761cf
/** @file
*
* VBox storage devices:
* Generic block driver
*/
/*
* Copyright (C) 2006-2007 innotek GmbH
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License as published by the Free Software Foundation,
* in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
* distribution. VirtualBox OSE is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY of any kind.
*
* If you received this file as part of a commercial VirtualBox
* distribution, then only the terms of your commercial VirtualBox
* license agreement apply instead of the previous paragraph.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DRV_BLOCK
#include <string.h>
#include "Builtins.h"
/** @def VBOX_PERIODIC_FLUSH
* Enable support for periodically flushing the VDI to disk. This may prove
* useful for those nasty problems with the ultra-slow host filesystems.
* If this is enabled, it can be configured via the CFGM key
* "VBoxInternal/Devices/piix3ide/0/LUN#<x>/Config/FlushInterval". <x>
* must be replaced with the correct LUN number of the disk that should
* do the periodic flushes. The value of the key is the number of bytes
* written between flushes. A value of 0 (the default) denotes no flushes. */
#define VBOX_PERIODIC_FLUSH
/** @def VBOX_IGNORE_FLUSH
* Enable support for ignoring VDI flush requests. This can be useful for
* filesystems that show bad guest IDE write performance (especially with
* Windows guests). NOTE that this does not disable the flushes caused by
* the periodic flush cache feature above.
* If this feature is enabled, it can be configured via the CFGM key
* "VBoxInternal/Devices/piix3ide/0/LUN#<x>/Config/IgnoreFlush". <x>
* must be replaced with the correct LUN number of the disk that should
* ignore flush requests. The value of the key is a boolean. The default
* is to honor flushes, i.e. false. */
#define VBOX_IGNORE_FLUSH
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Block driver instance data.
*/
typedef struct DRVBLOCK
{
/** Pointer driver instance. */
/** Drive type. */
/** Locked indicator. */
bool fLocked;
/** Mountable indicator. */
bool fMountable;
/** Visible to the BIOS. */
bool fBiosVisible;
/** Whether or not enmTranslation is valid. */
bool fTranslationSet;
#ifdef VBOX_PERIODIC_FLUSH
/** HACK: Configuration value for number of bytes written after which to flush. */
/** HACK: Current count for the number of bytes written since the last flush. */
#endif /* VBOX_PERIODIC_FLUSH */
#ifdef VBOX_IGNORE_FLUSH
/** HACK: Disable flushes for this drive. */
bool fIgnoreFlush;
#endif /* VBOX_IGNORE_FLUSH */
/** Pointer to the media driver below us.
* This is NULL if the media is not mounted. */
/** Pointer to the block port interface above us. */
/** Pointer to the mount notify interface above us. */
/** Our block interface. */
/** Our block interface. */
/** Our mountable interface. */
/** Uuid of the drive. */
/** BIOS Geometry: Translation mode. */
/** BIOS Geometry: Cylinders. */
/** BIOS Geometry: Heads. */
/** BIOS Geometry: Sectors. */
/* -=-=-=-=- IBlock -=-=-=-=- */
/** Makes a PDRVBLOCK out of a PPDMIBLOCK. */
#define PDMIBLOCK_2_DRVBLOCK(pInterface) ( (PDRVBLOCK)((uintptr_t)pInterface - RT_OFFSETOF(DRVBLOCK, IBlock)) )
/** @copydoc PDMIBLOCK::pfnRead */
static DECLCALLBACK(int) drvblockRead(PPDMIBLOCK pInterface, uint64_t off, void *pvBuf, size_t cbRead)
{
/*
* Check the state.
*/
{
AssertMsgFailed(("Invalid state! Not mounted!\n"));
return VERR_PDM_MEDIA_NOT_MOUNTED;
}
return rc;
}
/** @copydoc PDMIBLOCK::pfnWrite */
static DECLCALLBACK(int) drvblockWrite(PPDMIBLOCK pInterface, uint64_t off, const void *pvBuf, size_t cbWrite)
{
/*
* Check the state.
*/
{
AssertMsgFailed(("Invalid state! Not mounted!\n"));
return VERR_PDM_MEDIA_NOT_MOUNTED;
}
#ifdef VBOX_PERIODIC_FLUSH
if (pData->cbFlushInterval)
{
{
pData->cbDataWritten = 0;
}
}
#endif /* VBOX_PERIODIC_FLUSH */
return rc;
}
/** @copydoc PDMIBLOCK::pfnFlush */
{
/*
* Check the state.
*/
{
AssertMsgFailed(("Invalid state! Not mounted!\n"));
return VERR_PDM_MEDIA_NOT_MOUNTED;
}
#ifdef VBOX_IGNORE_FLUSH
if (pData->fIgnoreFlush)
return VINF_SUCCESS;
#endif /* VBOX_IGNORE_FLUSH */
if (rc == VERR_NOT_IMPLEMENTED)
rc = VINF_SUCCESS;
return rc;
}
/** @copydoc PDMIBLOCK::pfnIsReadOnly */
{
/*
* Check the state.
*/
return false;
return fRc;
}
/** @copydoc PDMIBLOCK::pfnGetSize */
{
/*
* Check the state.
*/
return 0;
return cb;
}
/** @copydoc PDMIBLOCK::pfnGetType */
{
}
/** @copydoc PDMIBLOCK::pfnGetUuid */
{
/*
* Copy the uuid.
*/
return VINF_SUCCESS;
}
/* -=-=-=-=- IBlockBios -=-=-=-=- */
/** Makes a PDRVBLOCK out of a PPDMIBLOCKBIOS. */
#define PDMIBLOCKBIOS_2_DRVBLOCK(pInterface) ( (PDRVBLOCK((uintptr_t)pInterface - RT_OFFSETOF(DRVBLOCK, IBlockBios))) )
/** @copydoc PDMIBLOCKBIOS::pfnGetGeometry */
static DECLCALLBACK(int) drvblockGetGeometry(PPDMIBLOCKBIOS pInterface, uint32_t *pcCylinders, uint32_t *pcHeads, uint32_t *pcSectors)
{
/*
* Check the state.
*/
return VERR_PDM_MEDIA_NOT_MOUNTED;
/*
* Use configured/cached values if present.
*/
if ( pData->cCylinders > 0
{
LogFlow(("drvblockGetGeometry: returns VINF_SUCCESS {%d,%d,%d}\n", pData->cCylinders, pData->cHeads, pData->cSectors));
return VINF_SUCCESS;
}
/*
* Call media.
*/
if (VBOX_SUCCESS(rc))
{
LogFlow(("drvblockGetGeometry: returns %Vrc {%d,%d,%d}\n", rc, pData->cCylinders, pData->cHeads, pData->cSectors));
}
else if (rc == VERR_NOT_IMPLEMENTED)
{
}
return rc;
}
/** @copydoc PDMIBLOCKBIOS::pfnSetGeometry */
static DECLCALLBACK(int) drvblockSetGeometry(PPDMIBLOCKBIOS pInterface, uint32_t cCylinders, uint32_t cHeads, uint32_t cSectors)
{
LogFlow(("drvblockSetGeometry: cCylinders=%d cHeads=%d cSectors=%d\n", cCylinders, cHeads, cSectors));
/*
* Check the state.
*/
{
AssertMsgFailed(("Invalid state! Not mounted!\n"));
return VERR_PDM_MEDIA_NOT_MOUNTED;
}
/*
* Call media. Ignore the not implemented return code.
*/
if ( VBOX_SUCCESS(rc)
|| rc == VERR_NOT_IMPLEMENTED)
{
rc = VINF_SUCCESS;
}
return rc;
}
/** @copydoc PDMIBLOCKBIOS::pfnGetTranslation */
static DECLCALLBACK(int) drvblockGetTranslation(PPDMIBLOCKBIOS pInterface, PPDMBIOSTRANSLATION penmTranslation)
{
/*
* Check the state.
*/
{
LogFlow(("drvblockGetTranslation: returns VERR_PDM_MEDIA_NOT_MOUNTED\n"));
return VERR_PDM_MEDIA_NOT_MOUNTED;
}
/*
* Use configured/cached data if present.
*/
if (pData->fTranslationSet)
{
return VINF_SUCCESS;
}
/*
* Call media. Handle the not implemented status code.
*/
if (VBOX_SUCCESS(rc))
{
pData->fTranslationSet = true;
}
else if (rc == VERR_NOT_IMPLEMENTED)
return rc;
}
/** @copydoc PDMIBLOCKBIOS::pfnSetTranslation */
static DECLCALLBACK(int) drvblockSetTranslation(PPDMIBLOCKBIOS pInterface, PDMBIOSTRANSLATION enmTranslation)
{
/*
* Check the state.
*/
{
AssertMsgFailed(("Invalid state! Not mounted!\n"));
return VERR_PDM_MEDIA_NOT_MOUNTED;
}
/*
* Call media. Ignore the not implemented return code.
*/
if ( VBOX_SUCCESS(rc)
|| rc == VERR_NOT_IMPLEMENTED)
{
pData->fTranslationSet = true;
rc = VINF_SUCCESS;
}
return rc;
}
/** @copydoc PDMIBLOCKBIOS::pfnIsVisible */
{
return pData->fBiosVisible;
}
/** @copydoc PDMIBLOCKBIOS::pfnGetType */
{
}
/* -=-=-=-=- IMount -=-=-=-=- */
/** Makes a PDRVBLOCK out of a PPDMIMOUNT. */
#define PDMIMOUNT_2_DRVBLOCK(pInterface) ( (PDRVBLOCK)((uintptr_t)pInterface - RT_OFFSETOF(DRVBLOCK, IMount)) )
/** @copydoc PDMIMOUNT::pfnMount */
static DECLCALLBACK(int) drvblockMount(PPDMIMOUNT pInterface, const char *pszFilename, const char *pszCoreDriver)
{
LogFlow(("drvblockMount: pszFilename=%p:{%s} pszCoreDriver=%p:{%s}\n", pszFilename, pszFilename, pszCoreDriver, pszCoreDriver));
/*
* Validate state.
*/
{
AssertMsgFailed(("Already mounted\n"));
return VERR_PDM_MEDIA_MOUNTED;
}
/*
* Prepare configuration.
*/
if (pszFilename)
{
if (VBOX_FAILURE(rc))
{
return rc;
}
}
/*
* Attach the media driver and query it's interface.
*/
if (VBOX_FAILURE(rc))
{
return rc;
}
{
/*
* Initialize state.
*/
#ifdef VBOX_PERIODIC_FLUSH
pData->cbDataWritten = 0;
#endif /* VBOX_PERIODIC_FLUSH */
/*
*/
if (pData->pDrvMountNotify)
Log(("drvblockMount: Success\n"));
return VINF_SUCCESS;
}
else
/*
* Failed, detatch the media driver.
*/
AssertMsgFailed(("No media interface!\n"));
return rc;
}
/** @copydoc PDMIMOUNT::pfnUnmount */
{
/*
* Validate state.
*/
{
Log(("drvblockUmount: Not mounted\n"));
return VERR_PDM_MEDIA_NOT_MOUNTED;
}
{
Log(("drvblockUmount: Locked\n"));
return VERR_PDM_MEDIA_LOCKED;
}
/* Media is no longer locked even if it was previously. */
/*
* Detach the media driver and query it's interface.
*/
if (VBOX_FAILURE(rc))
{
return rc;
}
/*
*/
if (pData->pDrvMountNotify)
Log(("drvblockUnmount: success\n"));
return VINF_SUCCESS;
}
/** @copydoc PDMIMOUNT::pfnIsMounted */
{
}
/** @copydoc PDMIMOUNT::pfnLock */
{
return VINF_SUCCESS;
}
/** @copydoc PDMIMOUNT::pfnUnlock */
{
return VINF_SUCCESS;
}
/** @copydoc PDMIMOUNT::pfnIsLocked */
{
}
/* -=-=-=-=- IBase -=-=-=-=- */
/** @copydoc PDMIBASE::pfnQueryInterface. */
{
switch (enmInterface)
{
case PDMINTERFACE_BASE:
case PDMINTERFACE_BLOCK:
case PDMINTERFACE_BLOCK_BIOS:
case PDMINTERFACE_MOUNT:
default:
return NULL;
}
}
/* -=-=-=-=- driver interface -=-=-=-=- */
/** @copydoc FNPDMDRVDETACH. */
{
}
/**
* Construct a block driver instance.
*
* @returns VBox status.
* @param pDrvIns The driver instance data.
* If the registration structure is needed, pDrvIns->pDrvReg points to it.
* @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
* of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
* iInstance it's expected to be used a bit in this function.
*/
{
/*
* Validate configuration.
*/
#if defined(VBOX_PERIODIC_FLUSH) || defined(VBOX_IGNORE_FLUSH)
if (!CFGMR3AreValuesValid(pCfgHandle, "Type\0Locked\0BIOSVisible\0AttachFailError\0Cylinders\0Heads\0Sectors\0Translation\0Mountable\0FlushInterval\0IgnoreFlush\0"))
#else /* !(VBOX_PERIODIC_FLUSH || VBOX_IGNORE_FLUSH) */
if (!CFGMR3AreValuesValid(pCfgHandle, "Type\0Locked\0BIOSVisible\0AttachFailError\0Cylinders\0Heads\0Sectors\0Translation\0Mountable\0"))
#endif /* !(VBOX_PERIODIC_FLUSH || VBOX_IGNORE_FLUSH) */
/*
* Initialize most of the data members.
*/
/* IBase. */
/* IBlock. */
/* IBlockBios. */
/* IMount. */
/*
*/
pData->pDrvBlockPort = (PPDMIBLOCKPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_BLOCK_PORT);
if (!pData->pDrvBlockPort)
{
AssertMsgFailed(("Configuration error: No block port interface above!\n"));
return VERR_PDM_MISSING_INTERFACE_ABOVE;
}
pData->pDrvMountNotify = (PPDMIMOUNTNOTIFY)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_MOUNT_NOTIFY);
/*
* Query configuration.
*/
/* type */
char *psz;
if (VBOX_FAILURE(rc))
{
return VERR_PDM_BLOCK_NO_TYPE;
}
else
{
return VERR_PDM_BLOCK_UNKNOWN_TYPE;
}
/* Mountable */
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
pData->fMountable = false;
else if (VBOX_FAILURE(rc))
{
return rc;
}
/* Locked */
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
else if (VBOX_FAILURE(rc))
{
return rc;
}
/* BIOS visible */
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
pData->fBiosVisible = true;
else if (VBOX_FAILURE(rc))
{
return rc;
}
/** @todo AttachFailError is currently completely ignored. */
/* Cylinders */
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
pData->cCylinders = 0;
else if (VBOX_FAILURE(rc))
{
return rc;
}
/* Heads */
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
else if (VBOX_FAILURE(rc))
{
return rc;
}
/* Sectors */
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
else if (VBOX_FAILURE(rc))
{
return rc;
}
/* Translation */
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
{
pData->fTranslationSet = false;
}
else if (VBOX_SUCCESS(rc))
{
else
{
}
pData->fTranslationSet = true;
}
else
{
return VERR_PDM_BLOCK_NO_TYPE;
}
/* Uuid */
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
else if (VBOX_SUCCESS(rc))
{
if (VBOX_FAILURE(rc))
{
return rc;
}
}
else
{
return VERR_PDM_BLOCK_NO_TYPE;
}
#ifdef VBOX_PERIODIC_FLUSH
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
pData->cbFlushInterval = 0;
else if (VBOX_FAILURE(rc))
{
return rc;
}
#endif /* VBOX_PERIODIC_FLUSH */
#ifdef VBOX_IGNORE_FLUSH
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
else if (VBOX_FAILURE(rc))
{
return rc;
}
#endif /* VBOX_IGNORE_FLUSH */
/*
* Try attach driver below and query it's media interface.
*/
if ( rc == VERR_PDM_NO_ATTACHED_DRIVER
return VINF_SUCCESS;
if (VBOX_FAILURE(rc))
{
return rc;
}
{
AssertMsgFailed(("Configuration error: No media interface below!\n"));
return VERR_PDM_MISSING_INTERFACE_BELOW;
}
{
}
return VINF_SUCCESS;
}
/**
* Block driver registration record.
*/
const PDMDRVREG g_DrvBlock =
{
/* u32Version */
/* szDriverName */
"Block",
/* pszDescription */
"Generic block driver.",
/* fFlags */
/* fClass. */
/* cMaxInstances */
~0,
/* cbInstance */
sizeof(DRVBLOCK),
/* pfnConstruct */
/* pfnDestruct */
NULL,
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
NULL,
/* pfnSuspend */
NULL,
/* pfnResume */
NULL,
/* pfnDetach */
};