Parallels.cpp revision 6a084dcaae277db9310cbba28991235f49594389
/* $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. */
/** Pointer to the per-disk VD interface list. */
/** Pointer to the per-image VD interface list. */
/** Error interface. */
/** I/O interface. */
/** 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. 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;
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;
}
/**
* Internal: Create a parallels image.
*/
unsigned uImageFlags, const char *pszComment,
unsigned uPercentStart, unsigned uPercentSpan)
{
int rc = VINF_SUCCESS;
if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
{
rc = vdIfError(pImage->pIfError, VERR_VD_INVALID_TYPE, RT_SRC_POS, N_("Parallels: cannot create fixed image '%s'. Create a raw image"), pImage->pszFilename);
goto out;
}
{
/* Set defaults. */
pImage->PCHSGeometry.cCylinders = pImage->cbSize / (512 * pImage->PCHSGeometry.cSectors * pImage->PCHSGeometry.cHeads);
}
/* Create image file. */
if (RT_FAILURE(rc))
{
rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("Parallels: cannot create image '%s'"), pImage->pszFilename);
goto out;
}
/* Setup image state. */
pImage->fAllocationBitmapChanged = true;
pImage->cbFileCurrent = sizeof(ParallelsHeader) + pImage->cAllocationBitmapEntries * sizeof(uint32_t);
/* Round to next sector boundary. */
pImage->pAllocationBitmap = (uint32_t *)RTMemAllocZ(pImage->cAllocationBitmapEntries * sizeof(uint32_t));
if (!pImage->pAllocationBitmap)
rc = VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
{
/* Write header and allocation bitmap. */
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
}
out:
if (RT_FAILURE(rc))
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnCheckIfValid */
{
int rc;
false /* fCreate */),
&pStorage);
if (RT_FAILURE(rc))
return rc;
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;
{
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;
}
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 = VINF_SUCCESS;
if (pIfProgress)
{
}
/* Check open flags. All valid flags are supported. */
if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
{
goto out;
}
/* Check remaining arguments. */
if ( !VALID_PTR(pszFilename)
|| !*pszFilename
|| !VALID_PTR(pPCHSGeometry)
|| !VALID_PTR(pLCHSGeometry))
{
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)
{
parallelsFreeImage(pImage, false);
if (RT_FAILURE(rc))
{
goto out;
}
}
*ppBackendData = pImage;
}
else
out:
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 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. */
LogFlowFunc(("AllocationBitmap[%u]=%u uSector=%u cbToRead=%zu cAllocationBitmapEntries=%u\n",
else
{
uint64_t uOffsetInFile = ((uint64_t)pImage->pAllocationBitmap[iIndexInAllocationTable] + uSector) * 512;
}
}
if ( ( RT_SUCCESS(rc)
|| rc == VERR_VD_BLOCK_FREE)
&& pcbActuallyRead)
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnWrite */
{
LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#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. */
LogFlowFunc(("AllocationBitmap[%u]=%u uSector=%u cbToWrite=%zu cAllocationBitmapEntries=%u\n",
{
&& !(fWrite & VD_WRITE_NO_ALLOC))
{
/* 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;
/* Allocate new chunk in the file. */
pImage->fAllocationBitmapChanged = true;
/*
* Write the new block at the current end of the file.
*/
}
else
{
/* Trying to do a partial write to an unallocated cluster. Don't do
* anything except letting the upper layer know what to do. */
}
}
else
{
}
}
if (pcbWriteProcess)
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. */
{
goto out;
}
/* Implement this operation via reopening the image. */
parallelsFreeImage(pImage, false);
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
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::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. */
}
*pcbPreRead = 0;
*pcbPostRead = 0;
}
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,
/* pfnAsyncRead */
/* pfnAsyncWrite */
/* pfnAsyncFlush */
/* pfnComposeLocation */
/* pfnComposeName */
/* pfnCompact */
NULL,
/* pfnResize */
NULL,
/* pfnDiscard */
NULL,
/* pfnAsyncDiscard */
NULL,
/* pfnRepair */
};