VCICacheCore.cpp revision 617fbf5ba046109c0555dbafd6e9de76f76ba026
/* $Id$ */
/** @file
* VCICacheCore - VirtualBox Cache Image, Core Code.
*/
/*
* Copyright (C) 2006-2010 Oracle Corporation
*
* 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 (GPL) 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <VBox/VBoxHDD-CachePlugin.h>
/*******************************************************************************
* Constants And Macros, Structures and Typedefs *
*******************************************************************************/
/**
* Raw image data structure.
*/
typedef struct VCICACHE
{
/** Image name. */
const char *pszFilename;
/** Storage handle. */
/** I/O interface. */
/** Async I/O interface callbacks. */
/** Pointer to the per-disk VD interface list. */
/** Pointer to the per-image VD interface list. */
/** Error callback. */
/** Opaque data for error callback. */
/** Open flags passed by VBoxHD layer. */
unsigned uOpenFlags;
/** Image flags defined during creation or determined during open. */
unsigned uImageFlags;
/** Total size of the image. */
/*******************************************************************************
* Static Variables *
*******************************************************************************/
/** NULL-terminated array of supported file extensions. */
static const char *const s_apszVciFileExtensions[] =
{
"vci",
};
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/**
* Internal: signal an error to the frontend.
*/
const char *pszFormat, ...)
{
if (pImage->pInterfaceError)
return rc;
}
{
int rc = VINF_SUCCESS;
if (fCreate)
return rc;
}
{
int rc = VINF_SUCCESS;
return rc;
}
{
}
{
pcbSize);
}
{
cbSize);
}
static int vciFileWriteSync(PVCICACHE pImage, uint64_t off, const void *pcvBuf, size_t cbWrite, size_t *pcbWritten)
{
}
static int vciFileReadSync(PVCICACHE pImage, uint64_t off, void *pvBuf, size_t cbRead, size_t *pcbRead)
{
pcbRead);
}
{
}
/**
* Internal: Open an image, constructing all necessary data structures.
*/
{
int rc;
if (uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO)
return VERR_NOT_SUPPORTED;
if (pImage->pInterfaceError)
#ifdef VBOX_WITH_NEW_IO_CODE
/* Try to get I/O interface. */
#endif
/*
* Open the image.
*/
if (RT_FAILURE(rc))
{
/* Do NOT signal an appropriate error here, as the VD layer has the
* choice of retrying the open if it failed. */
goto out;
}
if (RT_FAILURE(rc))
goto out;
{
goto out;
}
out:
if (RT_FAILURE(rc))
vciFreeImage(pImage, false);
return rc;
}
/**
* Internal: Create a vci image.
*/
unsigned uImageFlags, const char *pszComment,
unsigned uPercentStart, unsigned uPercentSpan)
{
int rc;
if (uImageFlags & VD_IMAGE_FLAGS_DIFF)
{
rc = vciError(pImage, VERR_VD_RAW_INVALID_TYPE, RT_SRC_POS, N_("Raw: cannot create diff image '%s'"), pImage->pszFilename);
goto out;
}
if (pImage->pInterfaceError)
#ifdef VBOX_WITH_NEW_IO_CODE
/* Try to get async I/O interface. */
#endif
/* Create image file. */
if (RT_FAILURE(rc))
{
goto out;
}
/* Check the free space on the disk and leave early if there is not
* sufficient space available. */
{
rc = vciError(pImage, VERR_DISK_FULL, RT_SRC_POS, N_("Raw: disk would overflow creating image '%s'"), pImage->pszFilename);
goto out;
}
/* Allocate & commit whole file if fixed image, it must be more
* effective than expanding file by write operations. */
if (RT_FAILURE(rc))
{
rc = vciError(pImage, rc, RT_SRC_POS, N_("Raw: setting image size failed for '%s'"), pImage->pszFilename);
goto out;
}
/* Fill image with zeroes. We do this for every fixed-size image since on
* some systems (for example Windows Vista), it takes ages to write a block
* near the end of a sparse file and the guest could complain about an ATA
* timeout. */
if (!pvBuf)
{
rc = VERR_NO_MEMORY;
goto out;
}
uOff = 0;
/* Write data to all image blocks. */
{
if (RT_FAILURE(rc))
{
rc = vciError(pImage, rc, RT_SRC_POS, N_("Raw: writing block failed for '%s'"), pImage->pszFilename);
goto out;
}
if (pfnProgress)
{
if (RT_FAILURE(rc))
goto out;
}
}
out:
if (RT_FAILURE(rc))
return rc;
}
/**
* Internal. Free all allocated space for representing an image, and optionally
* delete the image from disk.
*/
{
if (vciFileOpened(pImage))
{
}
}
/**
* Internal. Flush image data to disk.
*/
{
int rc = VINF_SUCCESS;
if ( vciFileOpened(pImage)
return rc;
}
/** @copydoc VDCACHEBACKEND::pfnProbe */
{
int rc = VINF_SUCCESS;
if ( !VALID_PTR(pszFilename)
|| !*pszFilename)
{
goto out;
}
/* Always return failure, to avoid opening everything as a vci image. */
out:
return rc;
}
/** @copydoc VDCACHEBACKEND::pfnOpen */
void **ppvBackendData)
{
LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p ppvBackendData=%#p\n", pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, ppvBackendData));
int rc;
/* Check open flags. All valid flags are supported. */
if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
{
goto out;
}
/* Check remaining arguments. */
if ( !VALID_PTR(pszFilename)
|| !*pszFilename)
{
goto out;
}
if (!pImage)
{
rc = VERR_NO_MEMORY;
goto out;
}
if (RT_SUCCESS(rc))
*ppvBackendData = pImage;
else
out:
return rc;
}
/** @copydoc VDCACHEBACKEND::pfnCreate */
unsigned uImageFlags, const char *pszComment,
unsigned uOpenFlags, unsigned uPercentStart,
void **ppvBackendData)
{
LogFlowFunc(("pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" Uuid=%RTuuid uOpenFlags=%#x uPercentStart=%u uPercentSpan=%u pVDIfsDisk=%#p pVDIfsImage=%#p pVDIfsOperation=%#p ppvBackendData=%#p",
pszFilename, cbSize, uImageFlags, pszComment, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, ppvBackendData));
int rc;
if (pIfProgress)
{
if (pCbProgress)
}
/* Check open flags. All valid flags are supported. */
if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
{
goto out;
}
/* Check remaining arguments. */
if ( !VALID_PTR(pszFilename)
|| !*pszFilename)
{
goto out;
}
if (!pImage)
{
rc = VERR_NO_MEMORY;
goto out;
}
if (RT_SUCCESS(rc))
{
* image is opened in read-only mode if the caller requested that. */
if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
{
vciFreeImage(pImage, false);
if (RT_FAILURE(rc))
{
goto out;
}
}
*ppvBackendData = pImage;
}
else
out:
return rc;
}
/** @copydoc VDCACHEBACKEND::pfnClose */
{
int rc = VINF_SUCCESS;
/* Freeing a never allocated image (e.g. because the open failed) is
* not signalled as an error. After all nothing bad happens. */
if (pImage)
{
}
return rc;
}
/** @copydoc VDCACHEBACKEND::pfnRead */
{
LogFlowFunc(("pvBackendData=%#p uOffset=%llu pvBuf=%#p cbToRead=%zu pcbActuallyRead=%#p\n", pvBackendData, uOffset, pvBuf, cbToRead, pcbActuallyRead));
int rc;
|| cbToRead == 0)
{
goto out;
}
out:
return rc;
}
/** @copydoc VDCACHEBACKEND::pfnWrite */
{
LogFlowFunc(("pvBackendData=%#p uOffset=%llu pvBuf=%#p cbToWrite=%zu pcbWriteProcess=%#p\n",
int rc;
{
goto out;
}
|| cbToWrite == 0)
{
goto out;
}
if (pcbWriteProcess)
out:
return rc;
}
/** @copydoc VDCACHEBACKEND::pfnFlush */
static int vciFlush(void *pvBackendData)
{
int rc;
return rc;
}
/** @copydoc VDCACHEBACKEND::pfnGetVersion */
static unsigned vciGetVersion(void *pvBackendData)
{
if (pImage)
return 1;
else
return 0;
}
/** @copydoc VDCACHEBACKEND::pfnGetSize */
{
if (pImage)
else
return 0;
}
/** @copydoc VDCACHEBACKEND::pfnGetFileSize */
{
if (pImage)
{
if (vciFileOpened(pImage))
{
if (RT_SUCCESS(rc))
}
}
return cb;
}
/** @copydoc VDCACHEBACKEND::pfnGetImageFlags */
static unsigned vciGetImageFlags(void *pvBackendData)
{
unsigned uImageFlags;
if (pImage)
else
uImageFlags = 0;
return uImageFlags;
}
/** @copydoc VDCACHEBACKEND::pfnGetOpenFlags */
static unsigned vciGetOpenFlags(void *pvBackendData)
{
unsigned uOpenFlags;
if (pImage)
else
uOpenFlags = 0;
return uOpenFlags;
}
/** @copydoc VDCACHEBACKEND::pfnSetOpenFlags */
{
int rc;
/* Image must be opened and the new flags must be valid. Just readonly and
* info flags are supported. */
{
goto out;
}
/* Implement this operation via reopening the image. */
vciFreeImage(pImage, false);
out:
return rc;
}
/** @copydoc VDCACHEBACKEND::pfnGetComment */
{
LogFlowFunc(("pvBackendData=%#p pszComment=%#p cbComment=%zu\n", pvBackendData, pszComment, cbComment));
int rc;
if (pImage)
{
if (pszComment)
*pszComment = '\0';
rc = VINF_SUCCESS;
}
else
return rc;
}
/** @copydoc VDCACHEBACKEND::pfnSetComment */
{
int rc;
{
goto out;
}
if (pImage)
else
out:
return rc;
}
/** @copydoc VDCACHEBACKEND::pfnGetUuid */
{
int rc;
if (pImage)
else
return rc;
}
/** @copydoc VDCACHEBACKEND::pfnSetUuid */
{
int rc;
if (pImage)
{
else
}
else
return rc;
}
/** @copydoc VDCACHEBACKEND::pfnGetModificationUuid */
{
int rc;
if (pImage)
else
return rc;
}
/** @copydoc VDCACHEBACKEND::pfnSetModificationUuid */
{
int rc;
if (pImage)
{
else
}
else
return rc;
}
/** @copydoc VDCACHEBACKEND::pfnDump */
static void vciDump(void *pvBackendData)
{
}
{
int rc = VINF_SUCCESS;
if (RT_SUCCESS(rc))
return rc;
}
{
int rc = VINF_SUCCESS;
if (RT_SUCCESS(rc))
return rc;
}
{
int rc = VERR_NOT_IMPLEMENTED;
return rc;
}
{
/* pszBackendName */
"vci",
/* cbSize */
sizeof(VDCACHEBACKEND),
/* uBackendCaps */
/* papszFileExtensions */
/* paConfigInfo */
NULL,
/* hPlugin */
/* pfnProbe */
/* pfnOpen */
/* pfnCreate */
/* pfnClose */
/* pfnRead */
/* pfnWrite */
/* pfnFlush */
/* pfnGetVersion */
/* pfnGetSize */
/* pfnGetFileSize */
/* pfnGetImageFlags */
/* pfnGetOpenFlags */
/* pfnSetOpenFlags */
/* pfnGetComment */
/* pfnSetComment */
/* pfnGetUuid */
/* pfnSetUuid */
/* pfnGetModificationUuid */
/* pfnSetModificationUuid */
/* pfnDump */
/* pfnAsyncRead */
/* pfnAsyncWrite */
/* pfnAsyncFlush */
/* pfnComposeLocation */
NULL,
/* pfnComposeName */
};