Parallels.cpp revision cd6f71bc352f550074f1ba2c830a2cf2f0b3dd46
/* $Id$ */
/** @file
*
* Parallels hdd disk 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.
*/
#define LOG_GROUP LOG_GROUP_VD_PARALLELS
#include <VBox/vd-plugin.h>
#define PARALLELS_HEADER_MAGIC "WithoutFreeSpace"
#define PARALLELS_DISK_VERSION 2
/** The header of the parallels disk. */
#pragma pack(1)
typedef struct ParallelsHeader
{
/** The magic header to identify a parallels hdd image. */
char HeaderIdentifier[16];
/** The version of the disk image. */
/** The number of heads the hdd has. */
/** Number of cylinders. */
/** Number of sectors per track. */
/** Number of entries in the allocation bitmap. */
/** Total number of sectors. */
/** Padding. */
char Padding[24];
#pragma pack()
/**
* Parallels image structure.
*/
typedef struct PARALLELSIMAGE
{
/** Image file name. */
const char *pszFilename;
/** Opaque storage handle. */
/** I/O interface. */
/** I/O interface callbacks. */
/** Pointer to the per-disk VD interface list. */
/** Pointer to the per-image VD interface list. */
/** Error interface. */
/** Error interface callbacks. */
/** Open flags passed by VBoxHDD layer. */
unsigned uOpenFlags;
/** Image flags defined during creation or determined during open. */
unsigned uImageFlags;
/** Total size of the image. */
/** Physical geometry of this image. */
/** Logical geometry of this image. */
/** Pointer to the allocation bitmap. */
/** Entries in the allocation bitmap. */
/** Flag whether the allocation bitmap was changed. */
bool fAllocationBitmapChanged;
/** Current file size. */
/*******************************************************************************
* Static Variables *
*******************************************************************************/
/** NULL-terminated array of supported file extensions. */
static const VDFILEEXTENSION s_aParallelsFileExtensions[] =
{
{"hdd", VDTYPE_HDD},
};
/***************************************************
* Internal functions *
**************************************************/
/**
* Internal: signal an error to the frontend.
*/
const char *pszFormat, ...)
{
return rc;
}
/**
* Internal: signal an informational message to the frontend.
*/
{
int rc = VINF_SUCCESS;
return rc;
}
{
}
{
}
{
}
{
}
{
}
{
}
{
}
{
}
{
}
{
cbRead);
}
void *pvCompleteUser)
{
}
void *pvCompleteUser)
{
}
void *pvCompleteUser)
{
}
/**
* Internal. Flush image data to disk.
*/
{
int rc = VINF_SUCCESS;
return VINF_SUCCESS;
&& (pImage->fAllocationBitmapChanged))
{
pImage->fAllocationBitmapChanged = false;
/* Write the allocation bitmap to the file. */
NULL);
if (RT_FAILURE(rc))
return rc;
}
/* Flush file. */
return rc;
}
/**
* Internal. Free all allocated space for representing an image except pImage,
* and optionally delete the image from disk.
*/
{
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)
{
{
/* No point updating the file that is deleted anyway. */
if (!fDelete)
}
if (pImage->pAllocationBitmap)
{
}
}
return rc;
}
{
int rc = VINF_SUCCESS;
/* Try to get error interface. */
if (pImage->pInterfaceError)
/* Get I/O interface. */
false /* fCreate */));
if (RT_FAILURE(rc))
goto out;
if (RT_FAILURE(rc))
goto out;
if (RT_FAILURE(rc))
goto out;
{
/* Check if the file has hdd as extension. It is a fixed size raw image then. */
{
goto out;
}
/* This is a fixed size image. */
uint64_t cCylinders = pImage->cbSize / (512 * pImage->PCHSGeometry.cSectors * pImage->PCHSGeometry.cHeads);
}
else
{
{
goto out;
}
{
goto out;
}
pImage->pAllocationBitmap = (uint32_t *)RTMemAllocZ((uint32_t)pImage->cAllocationBitmapEntries * sizeof(uint32_t));
if (!pImage->pAllocationBitmap)
{
rc = VERR_NO_MEMORY;
goto out;
}
NULL);
if (RT_FAILURE(rc))
goto out;
}
out:
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnCheckIfValid */
{
int rc;
/* Get I/O interface. */
false /* fCreate */),
&pStorage);
if (RT_FAILURE(rc))
return rc;
0, ¶llelsHeader,
sizeof(ParallelsHeader), NULL);
if (RT_SUCCESS(rc))
{
rc = VINF_SUCCESS;
else
{
/*
* The image may be an fixed size image.
* Unfortunately fixed sized parallels images
* are just raw files hence no magic header to
* check for.
* The code succeeds if the file is a multiple
* of 512 and if the file extensions is *.hdd
*/
char *pszExtension;
&cbFile);
{
return VERR_VD_PARALLELS_INVALID_HEADER;
}
else
rc = VINF_SUCCESS;
}
}
if (RT_SUCCESS(rc))
*penmType = VDTYPE_HDD;
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnOpen */
{
LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p ppBackendData=%#p\n", pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, ppBackendData));
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;
}
/** @todo r=klaus why this duplicate check, async is not claimed... */
if (uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO)
{
goto out;
}
if (!pImage)
{
rc = VERR_NO_MEMORY;
goto out;
}
pImage->fAllocationBitmapChanged = false;
if (RT_SUCCESS(rc))
*ppBackendData = pImage;
else
out:
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnCreate */
unsigned uImageFlags, const char *pszComment,
unsigned uOpenFlags, unsigned uPercentStart,
{
LogFlowFunc(("pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" pPCHSGeometry=%#p pLCHSGeometry=%#p Uuid=%RTuuid uOpenFlags=%#x uPercentStart=%u uPercentSpan=%u pVDIfsDisk=%#p pVDIfsImage=%#p pVDIfsOperation=%#p ppBackendData=%#p", pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, ppBackendData));
int rc = VERR_NOT_IMPLEMENTED;
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnRename */
{
int rc = VINF_SUCCESS;
/* Check arguments. */
if ( !pImage
|| !pszFilename
|| !*pszFilename)
{
goto out;
}
/* Close the image. */
if (RT_FAILURE(rc))
goto out;
/* Rename the file. */
if (RT_FAILURE(rc))
{
/* The move failed, try to reopen the original image. */
if (RT_FAILURE(rc2))
goto out;
}
/* Update pImage with the new information. */
/* Open the old image with new name. */
if (RT_FAILURE(rc))
goto out;
out:
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnClose */
{
int rc;
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnRead */
{
LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbBuf=%zu pcbActuallyRead=%#p\n", pBackendData, uOffset, pvBuf, cbBuf, pcbActuallyRead));
int rc = VINF_SUCCESS;
{
}
else
{
/* Calculate offset in the real file. */
/* One chunk in the file is always one track big. */
{
}
else
{
}
}
if (RT_SUCCESS(rc))
{
if (pcbActuallyRead)
*pcbActuallyRead = cbBuf;
Log2(("parallelsRead: off=%#llx pvBuf=%p cbBuf=%d\n"
"%.*Rhxd\n",
}
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnWrite */
{
LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbBuf=%zu pcbWriteProcess=%#p\n", pBackendData, uOffset, pvBuf, cbBuf, pcbWriteProcess));
int rc = VINF_SUCCESS;
{
}
else
{
/** Calculate offset in the real file. */
/** One chunk in the file is always one track big. */
{
/* Allocate new chunk in the file. */
pImage->fAllocationBitmapChanged = true;
if (!pNewBlock)
{
rc = VERR_NO_MEMORY;
goto out;
}
memcpy(pNewBlock + (uOffset - ((uint64_t)iIndexInAllocationTable * pImage->PCHSGeometry.cSectors * 512)),
/*
* Write the new block at the current end of the file.
*/
NULL);
}
else
{
}
}
if (pcbWriteProcess)
*pcbWriteProcess = cbBuf;
/* Stay on the safe side. Do not run the risk of confusing the higher
* level, as that can be pretty lethal to image consistency. */
*pcbPreRead = 0;
*pcbPostRead = 0;
out:
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnFlush */
static int parallelsFlush(void *pBackendData)
{
int rc;
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetVersion */
static unsigned parallelsGetVersion(void *pBackendData)
{
if (pImage)
return PARALLELS_DISK_VERSION;
else
return 0;
}
/** @copydoc VBOXHDDBACKEND::pfnGetSize */
{
return cb;
}
/** @copydoc VBOXHDDBACKEND::pfnGetFileSize */
{
return cb;
}
/** @copydoc VBOXHDDBACKEND::pfnGetPCHSGeometry */
static int parallelsGetPCHSGeometry(void *pBackendData,
{
int rc;
if (pImage)
{
{
rc = VINF_SUCCESS;
}
else
}
else
LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetPCHSGeometry */
static int parallelsSetPCHSGeometry(void *pBackendData,
{
LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
int rc;
if (pImage)
{
{
goto out;
}
rc = VINF_SUCCESS;
}
else
out:
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetLCHSGeometry */
static int parallelsGetLCHSGeometry(void *pBackendData,
{
int rc;
if (pImage)
{
{
rc = VINF_SUCCESS;
}
else
}
else
LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetLCHSGeometry */
static int parallelsSetLCHSGeometry(void *pBackendData,
{
LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
int rc;
if (pImage)
{
{
goto out;
}
rc = VINF_SUCCESS;
}
else
out:
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetImageFlags */
static unsigned parallelsGetImageFlags(void *pBackendData)
{
unsigned uImageFlags;
if (pImage)
else
uImageFlags = 0;
return uImageFlags;
}
/** @copydoc VBOXHDDBACKEND::pfnGetOpenFlags */
static unsigned parallelsGetOpenFlags(void *pBackendData)
{
unsigned uOpenFlags;
if (pImage)
else
uOpenFlags = 0;
return uOpenFlags;
}
/** @copydoc VBOXHDDBACKEND::pfnSetOpenFlags */
{
int rc;
/* Image must be opened and the new flags must be valid. */
/** @todo r=klaus add VD_OPEN_FLAGS_ASYNC_IO when async io has been tested */
if (!pImage || (uOpenFlags & ~(VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_SHAREABLE | VD_OPEN_FLAGS_SEQUENTIAL)))
{
goto out;
}
/* Implement this operation via reopening the image. */
parallelsFreeImage(pImage, true);
out:
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetComment */
{
LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
int rc;
if (pImage)
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetComment */
{
int rc;
if (pImage)
{
else
}
else
out:
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetUuid */
{
int rc;
if (pImage)
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetUuid */
{
int rc;
if (pImage)
{
else
}
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetModificationUuid */
{
int rc;
if (pImage)
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetModificationUuid */
{
int rc;
if (pImage)
{
else
}
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetParentUuid */
{
int rc;
if (pImage)
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetParentUuid */
{
int rc;
if (pImage)
{
else
}
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetParentModificationUuid */
{
int rc;
if (pImage)
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetParentModificationUuid */
{
int rc;
if (pImage)
{
else
}
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnDump */
static void parallelsDump(void *pBackendData)
{
if (pImage)
{
}
}
/** @copydoc VBOXHDDBACKEND::pfnIsAsyncIOSupported */
static bool parallelsIsAsyncIOSupported(void *pBackendData)
{
#if 0 /** @todo: Remove when tested */
return true;
#else
return false;
#endif
}
/** @copydoc VBOXHDDBACKEND::pfnAsyncRead */
{
LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToRead=%zu pcbActuallyRead=%#p\n",
int rc = VINF_SUCCESS;
{
}
else
{
/* Calculate offset in the real file. */
/* One chunk in the file is always one track big. */
{
}
else
{
}
}
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnAsyncWrite */
{
LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToWrite=%zu pcbWriteProcess=%#p\n",
int rc = VINF_SUCCESS;
{
}
else
{
/* Calculate offset in the real file. */
/* One chunk in the file is always one track big. */
{
if (fWrite & VD_WRITE_NO_ALLOC)
{
if (pcbWriteProcess)
return VERR_VD_BLOCK_FREE;
}
/* Allocate new chunk in the file. */
pImage->fAllocationBitmapChanged = true;
/*
* Write the new block at the current end of the file.
*/
{
/* Write the changed allocation bitmap entry. */
/** @todo: Error handling. */
}
}
else
{
}
}
if (pcbWriteProcess)
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnAsyncFlush */
{
int rc = VINF_SUCCESS;
/* Flush the file, everything is up to date already. */
return rc;
}
{
/* pszBackendName */
"Parallels",
/* cbSize */
sizeof(VBOXHDDBACKEND),
/* uBackendCaps */
/* paFileExtensions */
/* paConfigInfo */
NULL,
/* hPlugin */
/* pfnCheckIfValid */
/* pfnOpen */
/* pfnCreate */
/* pfnRename */
/* pfnClose */
/* pfnRead */
/* pfnWrite */
/* pfnFlush */
/* pfnGetVersion */
/* pfnGetSize */
/* pfnGetFileSize */
/* pfnGetPCHSGeometry */
/* pfnSetPCHSGeometry */
/* pfnGetLCHSGeometry */
/* pfnSetLCHSGeometry */
/* pfnGetImageFlags */
/* pfnGetOpenFlags */
/* pfnSetOpenFlags */
/* pfnGetComment */
/* pfnSetComment */
/* pfnGetUuid */
/* pfnSetUuid */
/* pfnGetModificationUuid */
/* pfnSetModificationUuid */
/* pfnGetParentUuid */
/* pfnSetParentUuid */
/* pfnGetParentModificationUuid */
/* pfnSetParentModificationUuid */
/* pfnDump */
/* pfnGetTimeStamp */
NULL,
/* pfnGetParentTimeStamp */
NULL,
/* pfnSetParentTimeStamp */
NULL,
/* pfnGetParentFilename */
NULL,
/* pfnSetParentFilename */
NULL,
/* pfnIsAsyncIOSupported */
/* pfnAsyncRead */
/* pfnAsyncWrite */
/* pfnAsyncFlush */
/* pfnComposeLocation */
/* pfnComposeName */
/* pfnCompact */
NULL,
/* pfnResize */
};