VDIHDDCore.cpp revision 19a26ab547c9219a638a0508bd56456d6e67e439
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @file
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Virtual Disk Image (VDI), Core Code.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/*
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Copyright (C) 2006-2007 innotek GmbH
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *
a0a3a26a4065b9401681a8c99a11bd83e08f94ccvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * available from http://www.virtualbox.org. This file is free software;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * you can redistribute it and/or modify it under the terms of the GNU
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * General Public License (GPL) as published by the Free Software
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/*******************************************************************************
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync* Header Files *
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync*******************************************************************************/
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#define LOG_GROUP LOG_GROUP_VD_VDI
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include "VBoxHDD-newInternal.h"
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync#define VBOX_VDICORE_VD /* Signal that the header is included from here. */
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync#include "VDICore.h"
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync#include <VBox/err.h>
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync#include <VBox/log.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <iprt/alloc.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <iprt/assert.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <iprt/uuid.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <iprt/file.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <iprt/string.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#include <iprt/asm.h>
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#define VDI_IMAGE_DEFAULT_BLOCK_SIZE _1M
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/*******************************************************************************
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync* Internal Functions *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync*******************************************************************************/
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic unsigned getPowerOfTwo(unsigned uNumber);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic void vdiInitPreHeader(PVDIPREHEADER pPreHdr);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiValidatePreHeader(PVDIPREHEADER pPreHdr);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic void vdiInitHeader(PVDIHEADER pHeader, VDIMAGETYPE enmType, uint32_t uImageFlags,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync const char *pszComment, uint64_t cbDisk, uint32_t cbBlock,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uint32_t cbBlockExtra);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiValidateHeader(PVDIHEADER pHeader);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic void vdiSetupImageDesc(PVDIIMAGEDESC pImage);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiUpdateHeader(PVDIIMAGEDESC pImage);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic void vdiFreeImage(PVDIIMAGEDESC pImage, bool fDelete);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync/**
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync * Internal: signal an error to the frontend.
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncDECLINLINE(int) vdiError(PVDIIMAGEDESC pImage, int rc, RT_SRC_POS_DECL,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync const char *pszFormat, ...)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
acca7ab818eb8c2832aa27e27ce53133fba927dbvboxsync va_list va;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync va_start(va, pszFormat);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pImage->pfnError)
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync pImage->pfnError(pImage->pvErrorUser, rc, RT_SRC_POS_ARGS,
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync pszFormat, va);
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync va_end(va);
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync return rc;
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync}
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync/**
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync * internal: return power of 2 or 0 if num error.
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync */
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsyncstatic unsigned getPowerOfTwo(unsigned uNumber)
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync{
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync if (uNumber == 0)
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync return 0;
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync unsigned uPower2 = 0;
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync while ((uNumber & 1) == 0)
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync {
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync uNumber >>= 1;
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync uPower2++;
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync }
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync return uNumber == 1 ? uPower2 : 0;
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Internal: Init VDI preheader.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
5eb36887f6970e0033f63fa135f3bb8fbfd6059bvboxsyncstatic void vdiInitPreHeader(PVDIPREHEADER pPreHdr)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pPreHdr->u32Signature = VDI_IMAGE_SIGNATURE;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pPreHdr->u32Version = VDI_IMAGE_VERSION;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync memset(pPreHdr->szFileInfo, 0, sizeof(pPreHdr->szFileInfo));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync strncat(pPreHdr->szFileInfo, VDI_IMAGE_FILE_INFO, sizeof(pPreHdr->szFileInfo));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Internal: check VDI preheader.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiValidatePreHeader(PVDIPREHEADER pPreHdr)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pPreHdr->u32Signature != VDI_IMAGE_SIGNATURE)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return VERR_VDI_INVALID_SIGNATURE;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if ( VDI_GET_VERSION_MAJOR(pPreHdr->u32Version) != VDI_IMAGE_VERSION_MAJOR
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync && pPreHdr->u32Version != 0x00000002) /* old version. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return VERR_VDI_UNSUPPORTED_VERSION;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return VINF_SUCCESS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync/**
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync * Internal: Init VDI header. Always use latest header version.
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync * @param pHeader Assumes it was initially initialized to all zeros.
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync */
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsyncstatic void vdiInitHeader(PVDIHEADER pHeader, VDIMAGETYPE enmType, uint32_t uImageFlags,
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync const char *pszComment, uint64_t cbDisk, uint32_t cbBlock,
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync uint32_t cbBlockExtra)
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pHeader->uVersion = VDI_IMAGE_VERSION;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pHeader->u.v1.cbHeader = sizeof(VDIHEADER1);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pHeader->u.v1.u32Type = (uint32_t)( enmType == VD_IMAGE_TYPE_NORMAL
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync ? VDI_IMAGE_TYPE_NORMAL
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync : VDI_IMAGE_TYPE_DIFF);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pHeader->u.v1.fFlags = uImageFlags;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#ifdef VBOX_STRICT
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync char achZero[VDI_IMAGE_COMMENT_SIZE] = {0};
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Assert(!memcmp(pHeader->u.v1.szComment, achZero, VDI_IMAGE_COMMENT_SIZE));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync#endif
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pHeader->u.v1.szComment[0] = '\0';
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync if (pszComment)
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync {
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync AssertMsg(strlen(pszComment) < sizeof(pHeader->u.v1.szComment),
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync ("HDD Comment is too long, cb=%d\n", strlen(pszComment)));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync strncat(pHeader->u.v1.szComment, pszComment, sizeof(pHeader->u.v1.szComment));
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync }
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync /* Mark the legacy geometry not-calculated. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pHeader->u.v1.LegacyGeometry.cCylinders = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pHeader->u.v1.LegacyGeometry.cHeads = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pHeader->u.v1.LegacyGeometry.cSectors = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pHeader->u.v1.LegacyGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pHeader->u.v1.u32Dummy = 0; /* used to be the translation value */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync pHeader->u.v1.cbDisk = cbDisk;
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync pHeader->u.v1.cbBlock = cbBlock;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pHeader->u.v1.cBlocks = (uint32_t)(cbDisk / cbBlock);
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync if (cbDisk % cbBlock)
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync pHeader->u.v1.cBlocks++;
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync pHeader->u.v1.cbBlockExtra = cbBlockExtra;
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync pHeader->u.v1.cBlocksAllocated = 0;
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync /* Init offsets. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pHeader->u.v1.offBlocks = RT_ALIGN_32(sizeof(VDIPREHEADER) + sizeof(VDIHEADER1), VDI_GEOMETRY_SECTOR_SIZE);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pHeader->u.v1.offData = RT_ALIGN_32(pHeader->u.v1.offBlocks + (pHeader->u.v1.cBlocks * sizeof(VDIIMAGEBLOCKPOINTER)), VDI_GEOMETRY_SECTOR_SIZE);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Init uuids. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTUuidCreate(&pHeader->u.v1.uuidCreate);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTUuidClear(&pHeader->u.v1.uuidModify);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTUuidClear(&pHeader->u.v1.uuidLinkage);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTUuidClear(&pHeader->u.v1.uuidParentModify);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Mark LCHS geometry not-calculated. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pHeader->u.v1plus.LCHSGeometry.cCylinders = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pHeader->u.v1plus.LCHSGeometry.cHeads = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pHeader->u.v1plus.LCHSGeometry.cSectors = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pHeader->u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Internal: Check VDI header.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiValidateHeader(PVDIHEADER pHeader)
a0a3a26a4065b9401681a8c99a11bd83e08f94ccvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Check version-dependend header parameters. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync switch (GET_MAJOR_HEADER_VERSION(pHeader))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync case 0:
a0a3a26a4065b9401681a8c99a11bd83e08f94ccvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Old header version. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync break;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync case 1:
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Current header version. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pHeader->u.v1.cbHeader < sizeof(VDIHEADER1))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogRel(("VDI: v1 header size wrong (%d < %d)\n",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pHeader->u.v1.cbHeader, sizeof(VDIHEADER1)));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return VERR_VDI_INVALID_HEADER;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (getImageBlocksOffset(pHeader) < (sizeof(VDIPREHEADER) + sizeof(VDIHEADER1)))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogRel(("VDI: v1 blocks offset wrong (%d < %d)\n",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync getImageBlocksOffset(pHeader), sizeof(VDIPREHEADER) + sizeof(VDIHEADER1)));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return VERR_VDI_INVALID_HEADER;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (getImageDataOffset(pHeader) < (getImageBlocksOffset(pHeader) + getImageBlocks(pHeader) * sizeof(VDIIMAGEBLOCKPOINTER)))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogRel(("VDI: v1 image data offset wrong (%d < %d)\n",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync getImageDataOffset(pHeader), getImageBlocksOffset(pHeader) + getImageBlocks(pHeader) * sizeof(VDIIMAGEBLOCKPOINTER)));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return VERR_VDI_INVALID_HEADER;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync break;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync default:
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Unsupported. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return VERR_VDI_UNSUPPORTED_VERSION;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Check common header parameters. */
a0a3a26a4065b9401681a8c99a11bd83e08f94ccvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync bool fFailed = false;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if ( getImageType(pHeader) < VDI_IMAGE_TYPE_FIRST
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync || getImageType(pHeader) > VDI_IMAGE_TYPE_LAST)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogRel(("VDI: bad image type %d\n", getImageType(pHeader)));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync fFailed = true;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (getImageFlags(pHeader) & ~VD_VDI_IMAGE_FLAGS_MASK)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogRel(("VDI: bad image flags %08x\n", getImageFlags(pHeader)));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync fFailed = true;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if ( getImageLCHSGeometry(pHeader)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync && (getImageLCHSGeometry(pHeader))->cbSector != VDI_GEOMETRY_SECTOR_SIZE)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
7755c0a4b7b215f612d9d27848c2584ffafe7a66vboxsync LogRel(("VDI: wrong sector size (%d != %d)\n",
7755c0a4b7b215f612d9d27848c2584ffafe7a66vboxsync (getImageLCHSGeometry(pHeader))->cbSector, VDI_GEOMETRY_SECTOR_SIZE));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync fFailed = true;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if ( getImageDiskSize(pHeader) == 0
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync || getImageBlockSize(pHeader) == 0
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync || getImageBlocks(pHeader) == 0
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync || getPowerOfTwo(getImageBlockSize(pHeader)) == 0)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogRel(("VDI: wrong size (%lld, %d, %d, %d)\n",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync getImageDiskSize(pHeader), getImageBlockSize(pHeader),
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync getImageBlocks(pHeader), getPowerOfTwo(getImageBlockSize(pHeader))));
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync fFailed = true;
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync }
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync if (getImageBlocksAllocated(pHeader) > getImageBlocks(pHeader))
602e0e27740395dba64bee2e0a8aef023ebd7650vboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogRel(("VDI: too many blocks allocated (%d > %d)\n"
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync " blocksize=%d disksize=%lld\n",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync getImageBlocksAllocated(pHeader), getImageBlocks(pHeader),
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync getImageBlockSize(pHeader), getImageDiskSize(pHeader)));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync fFailed = true;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if ( getImageExtraBlockSize(pHeader) != 0
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync && getPowerOfTwo(getImageExtraBlockSize(pHeader)) == 0)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogRel(("VDI: wrong extra size (%d, %d)\n",
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync getImageExtraBlockSize(pHeader), getPowerOfTwo(getImageExtraBlockSize(pHeader))));
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync fFailed = true;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync }
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync if ((uint64_t)getImageBlockSize(pHeader) * getImageBlocks(pHeader) < getImageDiskSize(pHeader))
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync {
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync LogRel(("VDI: wrong disk size (%d, %d, %lld)\n",
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync getImageBlockSize(pHeader), getImageBlocks(pHeader), getImageDiskSize(pHeader)));
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync fFailed = true;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync }
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync if (RTUuidIsNull(getImageCreationUUID(pHeader)))
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync {
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync LogRel(("VDI: uuid of creator is 0\n"));
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync fFailed = true;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync }
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RTUuidIsNull(getImageModificationUUID(pHeader)))
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogRel(("VDI: uuid of modificator is 0\n"));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync fFailed = true;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
78df65edff21c11c537f38e736707ea434ab5623vboxsync return fFailed ? VERR_VDI_INVALID_HEADER : VINF_SUCCESS;
78df65edff21c11c537f38e736707ea434ab5623vboxsync}
78df65edff21c11c537f38e736707ea434ab5623vboxsync
78df65edff21c11c537f38e736707ea434ab5623vboxsync/**
78df65edff21c11c537f38e736707ea434ab5623vboxsync * Internal: Set up VDIIMAGEDESC structure by image header.
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync */
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsyncstatic void vdiSetupImageDesc(PVDIIMAGEDESC pImage)
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync{
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync pImage->uImageFlags = getImageFlags(&pImage->Header);
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync pImage->offStartBlocks = getImageBlocksOffset(&pImage->Header);
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync pImage->offStartData = getImageDataOffset(&pImage->Header);
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync pImage->uBlockMask = getImageBlockSize(&pImage->Header) - 1;
78df65edff21c11c537f38e736707ea434ab5623vboxsync pImage->uShiftIndex2Offset =
78df65edff21c11c537f38e736707ea434ab5623vboxsync pImage->uShiftOffset2Index = getPowerOfTwo(getImageBlockSize(&pImage->Header));
78df65edff21c11c537f38e736707ea434ab5623vboxsync pImage->offStartBlockData = getImageExtraBlockSize(&pImage->Header);
78df65edff21c11c537f38e736707ea434ab5623vboxsync if (pImage->offStartBlockData != 0)
78df65edff21c11c537f38e736707ea434ab5623vboxsync pImage->uShiftIndex2Offset += getPowerOfTwo(pImage->offStartBlockData);
78df65edff21c11c537f38e736707ea434ab5623vboxsync}
78df65edff21c11c537f38e736707ea434ab5623vboxsync
78df65edff21c11c537f38e736707ea434ab5623vboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Internal: Create VDI image file.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiCreateImage(PVDIIMAGEDESC pImage, VDIMAGETYPE enmType,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uint64_t cbSize, unsigned uImageFlags,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync const char *pszComment,
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync PCPDMMEDIAGEOMETRY pPCHSGeometry,
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync PCPDMMEDIAGEOMETRY pLCHSGeometry,
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync PFNVMPROGRESS pfnProgress, void *pvUser,
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync unsigned uPercentStart, unsigned uPercentSpan)
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync{
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync int rc;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync RTFILE File;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uint64_t cbTotal;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uint64_t cbFill;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uint64_t uOff;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Special check for comment length. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if ( VALID_PTR(pszComment)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync && strlen(pszComment) >= VDI_IMAGE_COMMENT_SIZE)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = vdiError(pImage, VERR_VDI_COMMENT_TOO_LONG, RT_SRC_POS, N_("VDI: comment is too long for '%s'"), pImage->pszFilename);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Assert(VALID_PTR(pPCHSGeometry));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Assert(VALID_PTR(pLCHSGeometry));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vdiInitPreHeader(&pImage->PreHeader);
78df65edff21c11c537f38e736707ea434ab5623vboxsync vdiInitHeader(&pImage->Header, enmType, uImageFlags, pszComment, cbSize, VDI_IMAGE_DEFAULT_BLOCK_SIZE, 0);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Save PCHS geometry. Not much work, and makes the flow of information
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * quite a bit clearer - relying on the higher level isn't obvious. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->PCHSGeometry = *pPCHSGeometry;
78df65edff21c11c537f38e736707ea434ab5623vboxsync /* Set LCHS geometry (legacy geometry is ignored for the current 1.1+). */
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync pImage->Header.u.v1plus.LCHSGeometry.cCylinders = pLCHSGeometry->cCylinders;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync pImage->Header.u.v1plus.LCHSGeometry.cHeads = pLCHSGeometry->cHeads;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync pImage->Header.u.v1plus.LCHSGeometry.cSectors = pLCHSGeometry->cSectors;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync pImage->Header.u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync pImage->paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header));
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync if (!pImage->paBlocks)
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync {
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync rc = VERR_NO_MEMORY;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync goto out;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync }
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync if (enmType != VD_IMAGE_TYPE_FIXED)
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* for growing images mark all blocks in paBlocks as free. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync for (unsigned i = 0; i < pImage->Header.u.v1.cBlocks; i++)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* for fixed images mark all blocks in paBlocks as allocated */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync for (unsigned i = 0; i < pImage->Header.u.v1.cBlocks; i++)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->paBlocks[i] = i;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->Header.u.v1.cBlocksAllocated = pImage->Header.u.v1.cBlocks;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Setup image parameters. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vdiSetupImageDesc(pImage);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* create file */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = RTFileOpen(&File, pImage->pszFilename,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTFILE_O_READWRITE | RTFILE_O_CREATE | RTFILE_O_DENY_ALL);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (VBOX_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: cannot create image '%s'"), pImage->pszFilename);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync cbTotal = pImage->offStartData
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync + ((uint64_t)getImageBlocks(&pImage->Header) << pImage->uShiftIndex2Offset);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (enmType == VD_IMAGE_TYPE_FIXED)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* check the free space on the disk and leave early if there is not
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * sufficient space available */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTFOFF cbFree = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = RTFsQuerySizes(pImage->pszFilename, NULL, &cbFree, NULL, NULL);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (VBOX_SUCCESS(rc) /* ignore errors */ && ((uint64_t)cbFree < cbTotal))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = vdiError(pImage, VERR_DISK_FULL, RT_SRC_POS, N_("VDI: disk would overflow creating image '%s'"), pImage->pszFilename);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (enmType == VD_IMAGE_TYPE_FIXED)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /*
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Allocate & commit whole file if fixed image, it must be more
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * effective than expanding file by write operations.
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = RTFileSetSize(pImage->File, cbTotal);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Set file size to hold header and blocks array. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = RTFileSetSize(pImage->File, pImage->offStartData);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (VBOX_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: setting image size failed for '%s'"), pImage->pszFilename);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Generate image last-modify uuid */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTUuidCreate(getImageModificationUUID(&pImage->Header));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Write pre-header. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = RTFileWriteAt(pImage->File, 0, &pImage->PreHeader, sizeof(pImage->PreHeader), NULL);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (VBOX_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing pre-header failed for '%s'"), pImage->pszFilename);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Write header. */
48807baed22246206b9855f41d1fd106f0679b3avboxsync rc = RTFileWriteAt(pImage->File, sizeof(pImage->PreHeader), &pImage->Header.u.v1plus, sizeof(pImage->Header.u.v1plus), NULL);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (VBOX_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing header failed for '%s'"), pImage->pszFilename);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = RTFileWriteAt(pImage->File, pImage->offStartBlocks,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->paBlocks,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER),
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync NULL);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (VBOX_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing block pointers failed for '%s'"), pImage->pszFilename);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (enmType == VD_IMAGE_TYPE_FIXED)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Fill image with zeroes. We do this for every fixed-size image since on some systems
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * (for example Windows Vista), it takes ages to write a block near the end of a sparse
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * file and the guest could complain about an ATA timeout. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /** @todo Starting with Linux 2.6.23, there is an fallocate() system call.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Currently supported file systems are ext4 and ocfs2. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Allocate a temporary zero-filled buffer. Use a bigger block size to optimize writing */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync const size_t cbBuf = 128 * _1K;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync void *pvBuf = RTMemTmpAllocZ(cbBuf);
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync if (!pvBuf)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VERR_NO_MEMORY;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync cbFill = (uint64_t)getImageBlocks(&pImage->Header) << pImage->uShiftIndex2Offset;
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync uOff = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* do loop to fill all image. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync while (uOff < cbFill)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync unsigned cbChunk = (unsigned)RT_MIN(cbFill, cbBuf);
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync
090c459b9e90ca46e2ce2b8c81533ade3b23f3e9vboxsync rc = RTFileWriteAt(pImage->File, pImage->offStartData + uOff,
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync pvBuf, cbChunk, NULL);
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync if (VBOX_FAILURE(rc))
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync {
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing block failed for '%s'"), pImage->pszFilename);
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync goto out;
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync }
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync uOff += cbChunk;
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync if (pfnProgress)
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync {
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync rc = pfnProgress(NULL /* WARNING! pVM=NULL */,
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync uPercentStart + uOff * uPercentSpan / cbFill,
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync pvUser);
090c459b9e90ca46e2ce2b8c81533ade3b23f3e9vboxsync if (VBOX_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTMemTmpFree(pvBuf);
090c459b9e90ca46e2ce2b8c81533ade3b23f3e9vboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncout:
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (VBOX_SUCCESS(rc) && pfnProgress)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pfnProgress(NULL /* WARNING! pVM=NULL */,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uPercentStart + uPercentSpan, pvUser);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (VBOX_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vdiFreeImage(pImage, rc != VERR_ALREADY_EXISTS);
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync return rc;
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync}
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync/**
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync * Internal: Open a VDI image.
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync */
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsyncstatic int vdiOpenImage(PVDIIMAGEDESC pImage, unsigned uOpenFlags)
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync{
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync int rc;
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync RTFILE File;
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync pImage->uOpenFlags = uOpenFlags;
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync /*
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync * Open the image.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = RTFileOpen(&File, pImage->pszFilename,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uOpenFlags & VD_OPEN_FLAGS_READONLY
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync ? RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync : RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync if (VBOX_FAILURE(rc))
cbfe19957042925c835c6aee6722a0e7af852783vboxsync {
cbfe19957042925c835c6aee6722a0e7af852783vboxsync /* Do NOT signal an appropriate error here, as the VD layer has the
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync * choice of retrying the open if it failed. */
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync goto out;
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync }
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync pImage->File = File;
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync
cbfe19957042925c835c6aee6722a0e7af852783vboxsync /* Read pre-header. */
cbfe19957042925c835c6aee6722a0e7af852783vboxsync rc = RTFileReadAt(File, 0, &pImage->PreHeader, sizeof(pImage->PreHeader),
cbfe19957042925c835c6aee6722a0e7af852783vboxsync NULL);
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync if (VBOX_FAILURE(rc))
cbfe19957042925c835c6aee6722a0e7af852783vboxsync {
cbfe19957042925c835c6aee6722a0e7af852783vboxsync rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading pre-header in '%s'"), pImage->pszFilename);
cbfe19957042925c835c6aee6722a0e7af852783vboxsync goto out;
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync }
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync rc = vdiValidatePreHeader(&pImage->PreHeader);
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync if (VBOX_FAILURE(rc))
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync {
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: invalid pre-header in '%s'"), pImage->pszFilename);
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync goto out;
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync }
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Read header. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->Header.uVersion = pImage->PreHeader.u32Version;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync case 0:
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = RTFileReadAt(File, sizeof(pImage->PreHeader),
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync &pImage->Header.u.v0, sizeof(pImage->Header.u.v0),
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync NULL);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (VBOX_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading v0 header in '%s'"), pImage->pszFilename);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync break;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync case 1:
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = RTFileReadAt(File, sizeof(pImage->PreHeader),
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync &pImage->Header.u.v1, sizeof(pImage->Header.u.v1),
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync NULL);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (VBOX_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading v1 header in '%s'"), pImage->pszFilename);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Convert VDI 1.1 images to VDI 1.1+ on open in read/write mode.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Conversion is harmless, as any VirtualBox version supporting VDI
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * 1.1 doesn't touch fields it doesn't know about. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync && GET_MINOR_HEADER_VERSION(&pImage->Header) == 1
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync && pImage->Header.u.v1.cbHeader < sizeof(pImage->Header.u.v1plus))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->Header.u.v1plus.cbHeader = sizeof(pImage->Header.u.v1plus);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Mark LCHS geometry not-calculated. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->Header.u.v1plus.LCHSGeometry.cCylinders = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->Header.u.v1plus.LCHSGeometry.cHeads = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->Header.u.v1plus.LCHSGeometry.cSectors = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->Header.u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else if (pImage->Header.u.v1.cbHeader >= sizeof(pImage->Header.u.v1plus))
a0a3a26a4065b9401681a8c99a11bd83e08f94ccvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Read the actual VDI 1.1+ header completely. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = RTFileReadAt(File, sizeof(pImage->PreHeader), &pImage->Header.u.v1plus, sizeof(pImage->Header.u.v1plus), NULL);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (VBOX_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading v1.1+ header in '%s'"), pImage->pszFilename);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync break;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync default:
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = vdiError(pImage, VERR_VDI_UNSUPPORTED_VERSION, RT_SRC_POS, N_("VDI: unsupported major version %u in '%s'"), GET_MAJOR_HEADER_VERSION(&pImage->Header), pImage->pszFilename);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = vdiValidateHeader(&pImage->Header);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (VBOX_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = vdiError(pImage, VERR_VDI_UNSUPPORTED_VERSION, RT_SRC_POS, N_("VDI: invalid header in '%s'"), pImage->pszFilename);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Setup image parameters by header. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vdiSetupImageDesc(pImage);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Allocate memory for blocks array. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!pImage->paBlocks)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VERR_NO_MEMORY;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Read blocks array. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = RTFileReadAt(pImage->File, pImage->offStartBlocks, pImage->paBlocks,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER),
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync NULL);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncout:
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (VBOX_FAILURE(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vdiFreeImage(pImage, false);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Internal: Save header to file.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiUpdateHeader(PVDIIMAGEDESC pImage)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync case 0:
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = RTFileWriteAt(pImage->File, sizeof(VDIPREHEADER), &pImage->Header.u.v0, sizeof(pImage->Header.u.v0), NULL);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync break;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync case 1:
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pImage->Header.u.v1plus.cbHeader < sizeof(pImage->Header.u.v1plus))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = RTFileWriteAt(pImage->File, sizeof(VDIPREHEADER), &pImage->Header.u.v1, sizeof(pImage->Header.u.v1), NULL);
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync else
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync rc = RTFileWriteAt(pImage->File, sizeof(VDIPREHEADER), &pImage->Header.u.v1plus, sizeof(pImage->Header.u.v1plus), NULL);
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync break;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync default:
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync rc = VERR_VDI_UNSUPPORTED_VERSION;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync break;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync }
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync AssertMsgRC(rc, ("vdiUpdateHeader failed, filename=\"%s\" rc=%Vrc\n", pImage->pszFilename, rc));
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync return rc;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync}
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync/**
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync * Internal: Save block pointer to file, save header to file.
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync */
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsyncstatic int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock)
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync{
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync /* Update image header. */
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync int rc = vdiUpdateHeader(pImage);
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync if (VBOX_SUCCESS(rc))
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync {
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync /* write only one block pointer. */
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync rc = RTFileWriteAt(pImage->File,
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync pImage->offStartBlocks + uBlock * sizeof(VDIIMAGEBLOCKPOINTER),
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync &pImage->paBlocks[uBlock],
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync sizeof(VDIIMAGEBLOCKPOINTER),
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync NULL);
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync AssertMsgRC(rc, ("vdiUpdateBlockInfo failed to update block=%u, filename=\"%s\", rc=%Vrc\n",
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync uBlock, pImage->pszFilename, rc));
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync }
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync return rc;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync}
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync/**
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync * Internal: Flush the image file to disk.
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync */
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsyncstatic void vdiFlushImage(PVDIIMAGEDESC pImage)
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync{
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync {
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync /* Save header. */
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync int rc = vdiUpdateHeader(pImage);
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync AssertMsgRC(rc, ("vdiUpdateHeader() failed, filename=\"%s\", rc=%Vrc\n",
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync pImage->pszFilename, rc));
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync RTFileFlush(pImage->File);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync}
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/**
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Internal: Free all allocated space for representing an image, and optionally
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * delete the image from disk.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic void vdiFreeImage(PVDIIMAGEDESC pImage, bool fDelete)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Assert(pImage);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vdiFlushImage(pImage);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pImage->File != NIL_RTFILE)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTFileClose(pImage->File);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->File = NIL_RTFILE;
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pImage->paBlocks)
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync {
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync RTMemFree(pImage->paBlocks);
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync pImage->paBlocks = NULL;
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync }
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync if (fDelete && pImage->pszFilename)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTFileDelete(pImage->pszFilename);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @copydoc VBOXHDDBACKEND::pfnCheckIfValid */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiCheckIfValid(const char *pszFilename)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("pszFilename=\"%s\"\n", pszFilename));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc = VINF_SUCCESS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDIIMAGEDESC pImage;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if ( !pszFilename
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync || !*pszFilename)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VERR_INVALID_PARAMETER;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!pImage)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync rc = VERR_NO_MEMORY;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->pszFilename = pszFilename;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync rc = vdiOpenImage(pImage, VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_READONLY);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vdiFreeImage(pImage, false);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncout:
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync LogFlowFunc(("returns %Vrc\n", rc));
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync return rc;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync}
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync/** @copydoc VBOXHDDBACKEND::pfnOpen */
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsyncstatic int vdiOpen(const char *pszFilename, unsigned uOpenFlags,
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync PFNVDERROR pfnError, void *pvErrorUser,
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync void **ppBackendData)
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync{
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x ppBackendData=%#p\n", pszFilename, uOpenFlags, ppBackendData));
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync int rc;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync PVDIIMAGEDESC pImage;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync /* Check open flags. All valid flags are supported. */
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync {
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync rc = VERR_INVALID_PARAMETER;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync goto out;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync }
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync /* Check remaining arguments. */
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync if ( !VALID_PTR(pszFilename)
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync || !*pszFilename)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VERR_INVALID_PARAMETER;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!pImage)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VERR_NO_MEMORY;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->pszFilename = pszFilename;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->paBlocks = NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->pfnError = pfnError;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->pvErrorUser = pvErrorUser;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = vdiOpenImage(pImage, uOpenFlags);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (VBOX_SUCCESS(rc))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *ppBackendData = pImage;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncout:
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("returns %Vrc (pBackendData=%#p)\n", rc, *ppBackendData));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @copydoc VBOXHDDBACKEND::pfnCreate */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiCreate(const char *pszFilename, VDIMAGETYPE enmType,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uint64_t cbSize, unsigned uImageFlags,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync const char *pszComment,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PCPDMMEDIAGEOMETRY pPCHSGeometry,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PCPDMMEDIAGEOMETRY pLCHSGeometry,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync unsigned uOpenFlags, PFNVMPROGRESS pfnProgress,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync void *pvUser, unsigned uPercentStart,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync unsigned uPercentSpan, PFNVDERROR pfnError,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync void *pvErrorUser, void **ppBackendData)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("pszFilename=\"%s\" enmType=%d cbSize=%llu uImageFlags=%#x pszComment=\"%s\" pPCHSGeometry=%#p pLCHSGeometry=%#p uOpenFlags=%#x pfnProgress=%#p pvUser=%#p uPercentStart=%u uPercentSpan=%u pfnError=%#p pvErrorUser=%#p ppBackendData=%#p", pszFilename, enmType, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, uOpenFlags, pfnProgress, pvUser, uPercentStart, uPercentSpan, pfnError, pvErrorUser, ppBackendData));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDIIMAGEDESC pImage;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Check open flags. All valid flags are supported. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VERR_INVALID_PARAMETER;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Check remaining arguments. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if ( !pszFilename
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync || !*pszFilename
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync || (enmType != VD_IMAGE_TYPE_NORMAL && enmType != VD_IMAGE_TYPE_FIXED)
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync || cbSize < VDI_IMAGE_DEFAULT_BLOCK_SIZE
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync || !VALID_PTR(pPCHSGeometry)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync || !VALID_PTR(pLCHSGeometry))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VERR_INVALID_PARAMETER;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!pImage)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VERR_NO_MEMORY;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync pImage->pszFilename = pszFilename;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->paBlocks = NULL;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->pfnError = pfnError;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->pvErrorUser = pvErrorUser;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = vdiCreateImage(pImage, enmType, cbSize, uImageFlags, pszComment,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pPCHSGeometry, pLCHSGeometry,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pfnProgress, pvUser, uPercentStart, uPercentSpan);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (VBOX_SUCCESS(rc))
0433783a793d5ced0dc7f9dcf102143b66e1cd2cvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* So far the image is opened in read/write mode. Make sure the
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * image is opened in read-only mode if the caller requested that. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
0433783a793d5ced0dc7f9dcf102143b66e1cd2cvboxsync {
0433783a793d5ced0dc7f9dcf102143b66e1cd2cvboxsync vdiFreeImage(pImage, false);
0433783a793d5ced0dc7f9dcf102143b66e1cd2cvboxsync rc = vdiOpenImage(pImage, uOpenFlags);
0433783a793d5ced0dc7f9dcf102143b66e1cd2cvboxsync if (VBOX_FAILURE(rc))
0433783a793d5ced0dc7f9dcf102143b66e1cd2cvboxsync goto out;
0433783a793d5ced0dc7f9dcf102143b66e1cd2cvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *ppBackendData = pImage;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncout:
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("returns %Vrc (pBackendData=%#p)\n", rc, *ppBackendData));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @copydoc VBOXHDDBACKEND::pfnRename */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiRename(void *pBackendData, const char *pszFilename)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc = VERR_NOT_IMPLEMENTED;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("returns %Vrc\n", rc));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return rc;
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @copydoc VBOXHDDBACKEND::pfnClose */
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsyncstatic int vdiClose(void *pBackendData, bool fDelete)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc = VINF_SUCCESS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Freeing a never allocated image (e.g. because the open failed) is
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * not signalled as an error. After all nothing bad happens. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pImage)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vdiFreeImage(pImage, fDelete);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("returns %Vrc\n", rc));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
29a65fa8b740a860f86812328cf900b8d68d93bevboxsync
37a7e3e6f9ce5f6cabeb3f734044e9e8ca9cb1bfvboxsync/** @copydoc VBOXHDDBACKEND::pfnRead */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiRead(void *pBackendData, uint64_t uOffset, void *pvBuf,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cbToRead, size_t *pcbActuallyRead)
29a65fa8b740a860f86812328cf900b8d68d93bevboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbToRead=%zu pcbActuallyRead=%#p\n", pBackendData, uOffset, pvBuf, cbToRead, pcbActuallyRead));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync unsigned uBlock;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync unsigned offRead;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Assert(pImage);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Assert(uOffset % 512 == 0);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Assert(cbToRead % 512 == 0);
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync if ( uOffset + cbToRead > getImageDiskSize(&pImage->Header)
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync || cbToRead == 0)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VERR_INVALID_PARAMETER;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Calculate starting block number and offset inside it. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync offRead = (unsigned)uOffset & pImage->uBlockMask;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Clip read range to at most the rest of the block. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync cbToRead = RT_MIN(cbToRead, getImageBlockSize(&pImage->Header) - offRead);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Assert(!(cbToRead % 512));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_FREE)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VERR_VDI_BLOCK_FREE;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync else if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync memset(pvBuf, 0, cbToRead);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VINF_SUCCESS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* block present in image file */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uint64_t u64Offset = ((uint64_t)pImage->paBlocks[uBlock] << pImage->uShiftIndex2Offset)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync + (pImage->offStartData + pImage->offStartBlockData + offRead);
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync rc = RTFileReadAt(pImage->File, u64Offset, pvBuf, cbToRead, NULL);
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync }
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync
af0a09edb4c1431b606fe207d4138da008f67f13vboxsync if (VBOX_SUCCESS(rc))
af0a09edb4c1431b606fe207d4138da008f67f13vboxsync *pcbActuallyRead = cbToRead;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncout:
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("returns %Vrc\n", rc));
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync return rc;
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync}
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync/**@copydoc VBOXHDDBACKEND::pfnWrite */
602e0e27740395dba64bee2e0a8aef023ebd7650vboxsyncstatic int vdiWrite(void *pBackendData, uint64_t uOffset, const void *pvBuf,
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync size_t cbToWrite, size_t *pcbWriteProcess,
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync size_t *pcbPreRead, size_t *pcbPostRead)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n", pBackendData, uOffset, pvBuf, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync unsigned uBlock;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync unsigned offWrite;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc = VINF_SUCCESS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Assert(pImage);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Assert(uOffset % 512 == 0);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Assert(cbToWrite % 512 == 0);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VERR_VDI_IMAGE_READ_ONLY;
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync goto out;
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync }
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync if (cbToWrite == 0)
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync {
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync rc = VERR_INVALID_PARAMETER;
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync goto out;
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* No size check here, will do that later. For dynamic images which are
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * not multiples of the block size in length, this would prevent writing to
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * the last grain. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Calculate starting block number and offset inside it. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync offWrite = (unsigned)uOffset & pImage->uBlockMask;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Clip write range to at most the rest of the block. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync cbToWrite = RT_MIN(cbToWrite, getImageBlockSize(&pImage->Header) - offWrite);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Assert(!(cbToWrite % 512));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Block is either free or zero. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_ZEROES)
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync && ( pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync || cbToWrite == getImageBlockSize(&pImage->Header)))
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync {
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync /* If the destination block is unallocated at this point, it's
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync * either a zero block or a block which hasn't been used so far
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync * (which also means that it's a zero block. Don't need to write
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync * anything to this block if the data consists of just zeroes. */
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync Assert(cbToWrite % 4 == 0);
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync Assert(cbToWrite * 8 <= UINT32_MAX);
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync if (ASMBitFirstSet((volatile void *)pvBuf, (uint32_t)cbToWrite * 8) == -1)
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync {
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync pImage->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync goto out;
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync }
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync }
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync if (cbToWrite == getImageBlockSize(&pImage->Header))
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync {
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync /* Full block write to previously unallocated block.
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync * Allocate block and write data. */
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync rc = RTFileWriteAt(pImage->File,
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync ((uint64_t)cBlocksAllocated << pImage->uShiftIndex2Offset)
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync + pImage->offStartData,
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync pvBuf, cbToWrite, NULL);
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync if (VBOX_FAILURE(rc))
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync goto out;
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync pImage->paBlocks[uBlock] = cBlocksAllocated;
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync setImageBlocksAllocated(&pImage->Header, cBlocksAllocated + 1);
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync rc = vdiUpdateBlockInfo(pImage, uBlock);
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync if (VBOX_FAILURE(rc))
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync goto out;
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync *pcbPreRead = 0;
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync *pcbPostRead = 0;
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync }
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync else
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync {
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync /* Trying to do a partial write to an unallocated block. Don't do
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync * anything except letting the upper layer know what to do. */
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync *pcbPreRead = offWrite % getImageBlockSize(&pImage->Header);
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync *pcbPostRead = getImageBlockSize(&pImage->Header) - cbToWrite - *pcbPreRead;
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync rc = VERR_VDI_BLOCK_FREE;
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync }
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = RTFileWriteAt(pImage->File,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync ((uint64_t)pImage->paBlocks[uBlock] << pImage->uShiftIndex2Offset)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync + pImage->offStartData + pImage->offStartBlockData + offWrite,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pvBuf, cbToWrite, NULL);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncout:
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("returns %Vrc\n", rc));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @copydoc VBOXHDDBACKEND::pfnFlush */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiFlush(void *pBackendData)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync LogFlowFunc(("pBackendData=%#p\n", pBackendData));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync int rc = VINF_SUCCESS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync vdiFlushImage(pImage);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("returns %Vrc\n", rc));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
8d7d8eab781262b9db094374b166806c03786fa2vboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @copydoc VBOXHDDBACKEND::pfnGetVersion */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic unsigned vdiGetVersion(void *pBackendData)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("pBackendData=%#p\n", pBackendData));
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync unsigned uVersion;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Assert(pImage);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pImage)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uVersion = pImage->PreHeader.u32Version;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uVersion = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("returns %#x\n", uVersion));
bc5c5b5cd56ce8e64ada076691018f1dff17b2c2vboxsync return uVersion;
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync}
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync/** @copydoc VBOXHDDBACKEND::pfnGetImageType */
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsyncstatic int vdiGetImageType(void *pBackendData, PVDIMAGETYPE penmImageType)
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("pBackendData=%#p penmImageType=%#p\n", pBackendData, penmImageType));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc = VINF_SUCCESS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Assert(pImage);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Assert(penmImageType);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pImage)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *penmImageType = getImageType(&pImage->Header) == VDI_IMAGE_TYPE_NORMAL
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsync ? VD_IMAGE_TYPE_NORMAL
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync : VD_IMAGE_TYPE_DIFF;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VERR_VDI_NOT_OPENED;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("returns %Vrc enmImageType=%u\n", rc, *penmImageType));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @copydoc VBOXHDDBACKEND::pfnGetSize */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic uint64_t vdiGetSize(void *pBackendData)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("pBackendData=%#p\n", pBackendData));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uint64_t cbSize;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Assert(pImage);
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pImage)
090c459b9e90ca46e2ce2b8c81533ade3b23f3e9vboxsync cbSize = getImageDiskSize(&pImage->Header);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync cbSize = 0;
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("returns %llu\n", cbSize));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return cbSize;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync}
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync/** @copydoc VBOXHDDBACKEND::pfnGetFileSize */
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsyncstatic uint64_t vdiGetFileSize(void *pBackendData)
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync{
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync LogFlowFunc(("pBackendData=%#p\n", pBackendData));
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync uint64_t cb = 0;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync Assert(pImage);
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync if (pImage)
11c2b573e2625474a51ae55ee1f3f82936f125davboxsync {
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync uint64_t cbFile;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync if (pImage->File != NIL_RTFILE)
cbfe19957042925c835c6aee6722a0e7af852783vboxsync {
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync int rc = RTFileGetSize(pImage->File, &cbFile);
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync if (VBOX_SUCCESS(rc))
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync cb += cbFile;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync }
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync }
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync LogFlowFunc(("returns %lld\n", cb));
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync return cb;
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync}
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync/** @copydoc VBOXHDDBACKEND::pfnGetPCHSGeometry */
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsyncstatic int vdiGetPCHSGeometry(void *pBackendData,
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync PPDMMEDIAGEOMETRY pPCHSGeometry)
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync{
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
db07105a44f53e9e2377ade02cd3dbe482647cb9vboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
db07105a44f53e9e2377ade02cd3dbe482647cb9vboxsync int rc;
db07105a44f53e9e2377ade02cd3dbe482647cb9vboxsync
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync Assert(pImage);
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync if (pImage)
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync {
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync if (pImage->PCHSGeometry.cCylinders)
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync {
04b02ffb8824a60fd37777bc1f7d2f35104a274cvboxsync *pPCHSGeometry = pImage->PCHSGeometry;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VINF_SUCCESS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VERR_VDI_GEOMETRY_NOT_SET;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VERR_VDI_NOT_OPENED;
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync LogFlowFunc(("returns %Vrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync return rc;
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync}
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync/** @copydoc VBOXHDDBACKEND::pfnSetPCHSGeometry */
cbfe19957042925c835c6aee6722a0e7af852783vboxsyncstatic int vdiSetPCHSGeometry(void *pBackendData,
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync PCPDMMEDIAGEOMETRY pPCHSGeometry)
cbfe19957042925c835c6aee6722a0e7af852783vboxsync{
cbfe19957042925c835c6aee6722a0e7af852783vboxsync LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
04b02ffb8824a60fd37777bc1f7d2f35104a274cvboxsync int rc;
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync Assert(pImage);
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pImage)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync rc = VERR_VDI_IMAGE_READ_ONLY;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->PCHSGeometry = *pPCHSGeometry;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VINF_SUCCESS;
f45123ab6638fb0a030ffbfa96598635efd24a01vboxsync }
f45123ab6638fb0a030ffbfa96598635efd24a01vboxsync else
f45123ab6638fb0a030ffbfa96598635efd24a01vboxsync rc = VERR_VDI_NOT_OPENED;
f45123ab6638fb0a030ffbfa96598635efd24a01vboxsync
f45123ab6638fb0a030ffbfa96598635efd24a01vboxsyncout:
f45123ab6638fb0a030ffbfa96598635efd24a01vboxsync LogFlowFunc(("returns %Vrc\n", rc));
f45123ab6638fb0a030ffbfa96598635efd24a01vboxsync return rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @copydoc VBOXHDDBACKEND::pfnGetLCHSGeometry */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiGetLCHSGeometry(void *pBackendData,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PPDMMEDIAGEOMETRY pLCHSGeometry)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
a0a3a26a4065b9401681a8c99a11bd83e08f94ccvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Assert(pImage);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pImage)
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync {
78df65edff21c11c537f38e736707ea434ab5623vboxsync VDIDISKGEOMETRY DummyGeo = { 0, 0, 0, VDI_GEOMETRY_SECTOR_SIZE };
78df65edff21c11c537f38e736707ea434ab5623vboxsync PVDIDISKGEOMETRY pGeometry = getImageLCHSGeometry(&pImage->Header);
78df65edff21c11c537f38e736707ea434ab5623vboxsync if (!pGeometry)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pGeometry = &DummyGeo;
78df65edff21c11c537f38e736707ea434ab5623vboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if ( pGeometry->cCylinders > 0
78df65edff21c11c537f38e736707ea434ab5623vboxsync && pGeometry->cHeads > 0
78df65edff21c11c537f38e736707ea434ab5623vboxsync && pGeometry->cSectors > 0)
78df65edff21c11c537f38e736707ea434ab5623vboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pLCHSGeometry->cCylinders = pGeometry->cCylinders;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pLCHSGeometry->cHeads = pGeometry->cHeads;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pLCHSGeometry->cSectors = pGeometry->cSectors;
78df65edff21c11c537f38e736707ea434ab5623vboxsync rc = VINF_SUCCESS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VERR_VDI_GEOMETRY_NOT_SET;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VERR_VDI_NOT_OPENED;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("returns %Vrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return rc;
0433783a793d5ced0dc7f9dcf102143b66e1cd2cvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @copydoc VBOXHDDBACKEND::pfnSetLCHSGeometry */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiSetLCHSGeometry(void *pBackendData,
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync PCPDMMEDIAGEOMETRY pLCHSGeometry)
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync{
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDIDISKGEOMETRY pGeometry;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Assert(pImage);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pImage)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
37a7e3e6f9ce5f6cabeb3f734044e9e8ca9cb1bfvboxsync if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
0433783a793d5ced0dc7f9dcf102143b66e1cd2cvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VERR_VDI_IMAGE_READ_ONLY;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync goto out;
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync }
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync
740011a32913a7f759d5b747def605a2b820c3fcvboxsync pGeometry = getImageLCHSGeometry(&pImage->Header);
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync if (pGeometry)
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync {
740011a32913a7f759d5b747def605a2b820c3fcvboxsync pGeometry->cCylinders = pLCHSGeometry->cCylinders;
740011a32913a7f759d5b747def605a2b820c3fcvboxsync pGeometry->cHeads = pLCHSGeometry->cHeads;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pGeometry->cSectors = pLCHSGeometry->cSectors;
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync pGeometry->cbSector = VDI_GEOMETRY_SECTOR_SIZE;
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync /* Update header information in base image file. */
01283af355575e02820380d73a30a474f425fd3dvboxsync vdiFlushImage(pImage);
01283af355575e02820380d73a30a474f425fd3dvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VINF_SUCCESS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VERR_VDI_NOT_OPENED;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncout:
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("returns %Vrc\n", rc));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return rc;
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync}
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync/** @copydoc VBOXHDDBACKEND::pfnGetImageFlags */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic unsigned vdiGetImageFlags(void *pBackendData)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("pBackendData=%#p\n", pBackendData));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync unsigned uImageFlags;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Assert(pImage);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pImage)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uImageFlags = getImageFlags(&pImage->Header);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
37a7e3e6f9ce5f6cabeb3f734044e9e8ca9cb1bfvboxsync uImageFlags = 0;
0433783a793d5ced0dc7f9dcf102143b66e1cd2cvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("returns %#x\n", uImageFlags));
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync return uImageFlags;
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync}
740011a32913a7f759d5b747def605a2b820c3fcvboxsync
740011a32913a7f759d5b747def605a2b820c3fcvboxsync/** @copydoc VBOXHDDBACKEND::pfnGetOpenFlags */
740011a32913a7f759d5b747def605a2b820c3fcvboxsyncstatic unsigned vdiGetOpenFlags(void *pBackendData)
740011a32913a7f759d5b747def605a2b820c3fcvboxsync{
740011a32913a7f759d5b747def605a2b820c3fcvboxsync LogFlowFunc(("pBackendData=%#p\n", pBackendData));
be302cc3dbc037aaebbadbf9057662a88b3b23a8vboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
be302cc3dbc037aaebbadbf9057662a88b3b23a8vboxsync unsigned uOpenFlags;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync Assert(pImage);
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync if (pImage)
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync uOpenFlags = pImage->uOpenFlags;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uOpenFlags = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync LogFlowFunc(("returns %#x\n", uOpenFlags));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return uOpenFlags;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
d8707f50ca9ff2792d9acb7addb52467098d1b47vboxsync/** @copydoc VBOXHDDBACKEND::pfnSetOpenFlags */
d8707f50ca9ff2792d9acb7addb52467098d1b47vboxsyncstatic int vdiSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
d8707f50ca9ff2792d9acb7addb52467098d1b47vboxsync{
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync LogFlowFunc(("pBackendData=%#p\n uOpenFlags=%#x", pBackendData, uOpenFlags));
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync int rc;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync const char *pszFilename;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync /* Image must be opened and the new flags must be valid. Just readonly flag
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * is supported. */
be302cc3dbc037aaebbadbf9057662a88b3b23a8vboxsync if (!pImage || uOpenFlags & ~VD_OPEN_FLAGS_READONLY)
d8707f50ca9ff2792d9acb7addb52467098d1b47vboxsync {
be302cc3dbc037aaebbadbf9057662a88b3b23a8vboxsync rc = VERR_INVALID_PARAMETER;
d8707f50ca9ff2792d9acb7addb52467098d1b47vboxsync goto out;
d8707f50ca9ff2792d9acb7addb52467098d1b47vboxsync }
be302cc3dbc037aaebbadbf9057662a88b3b23a8vboxsync
d8707f50ca9ff2792d9acb7addb52467098d1b47vboxsync /* Implement this operation via reopening the image. */
d8707f50ca9ff2792d9acb7addb52467098d1b47vboxsync pszFilename = pImage->pszFilename;
d8707f50ca9ff2792d9acb7addb52467098d1b47vboxsync vdiFreeImage(pImage, false);
d8707f50ca9ff2792d9acb7addb52467098d1b47vboxsync rc = vdiOpenImage(pImage, uOpenFlags);
d8707f50ca9ff2792d9acb7addb52467098d1b47vboxsync
d8707f50ca9ff2792d9acb7addb52467098d1b47vboxsyncout:
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("returns %Vrc\n", rc));
be302cc3dbc037aaebbadbf9057662a88b3b23a8vboxsync return rc;
be302cc3dbc037aaebbadbf9057662a88b3b23a8vboxsync}
be302cc3dbc037aaebbadbf9057662a88b3b23a8vboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @copydoc VBOXHDDBACKEND::pfnGetComment */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiGetComment(void *pBackendData, char *pszComment,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cbComment)
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc = VINF_SUCCESS;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync Assert(pImage);
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync if (pImage)
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync char *pszTmp = getImageComment(&pImage->Header);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync unsigned cb = strlen(pszTmp);
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync if (cb < cbComment)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* memcpy is much better than strncpy. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync memcpy(pszComment, pszTmp, cb + 1);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync rc = VERR_BUFFER_OVERFLOW;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync else
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync rc = VERR_VDI_NOT_OPENED;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync LogFlowFunc(("returns %Vrc comment=\"%s\"\n", rc, pszComment));
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync return rc;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync}
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync/** @copydoc VBOXHDDBACKEND::pfnGetComment */
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsyncstatic int vdiSetComment(void *pBackendData, const char *pszComment)
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync{
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync int rc;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync Assert(pImage);
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync {
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync rc = VERR_VDI_IMAGE_READ_ONLY;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync goto out;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pImage)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cchComment = pszComment ? strlen(pszComment) : 0;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync if (cchComment >= VDI_IMAGE_COMMENT_SIZE)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync LogFunc(("pszComment is too long, %d bytes!\n", cchComment));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VERR_VDI_COMMENT_TOO_LONG;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync goto out;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync }
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* we don't support old style images */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync {
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync /*
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync * Update the comment field, making sure to zero out all of the previous comment.
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync */
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync memset(pImage->Header.u.v1.szComment, '\0', VDI_IMAGE_COMMENT_SIZE);
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync memcpy(pImage->Header.u.v1.szComment, pszComment, cchComment);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* write out new the header */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = vdiUpdateHeader(pImage);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync rc = VERR_VDI_UNSUPPORTED_VERSION;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync else
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync rc = VERR_VDI_NOT_OPENED;
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsyncout:
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync LogFlowFunc(("returns %Vrc\n", rc));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @copydoc VBOXHDDBACKEND::pfnGetUuid */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiGetUuid(void *pBackendData, PRTUUID pUuid)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Assert(pImage);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pImage)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *pUuid = *getImageCreationUUID(&pImage->Header);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VINF_SUCCESS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VERR_VDI_NOT_OPENED;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("returns %Vrc (%Vuuid)\n", rc, pUuid));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
8e29dabca04cf55fde8755871d14abde99ef7236vboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @copydoc VBOXHDDBACKEND::pfnSetUuid */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiSetUuid(void *pBackendData, PCRTUUID pUuid)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("pBackendData=%#p Uuid=%Vuuid\n", pBackendData, pUuid));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc = VINF_SUCCESS;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Assert(pImage);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pImage)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->Header.u.v1.uuidCreate = *pUuid;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Make it possible to clone old VDIs. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->Header.u.v0.uuidCreate = *pUuid;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFunc(("Version is not supported!\n"));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VERR_VDI_UNSUPPORTED_VERSION;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VERR_VDI_IMAGE_READ_ONLY;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VERR_VDI_NOT_OPENED;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("returns %Vrc\n", rc));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync/** @copydoc VBOXHDDBACKEND::pfnGetModificationUuid */
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsyncstatic int vdiGetModificationUuid(void *pBackendData, PRTUUID pUuid)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync{
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync int rc;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync Assert(pImage);
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync if (pImage)
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync {
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync *pUuid = *getImageModificationUUID(&pImage->Header);
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync rc = VINF_SUCCESS;
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync }
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync else
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = VERR_VDI_NOT_OPENED;
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync LogFlowFunc(("returns %Vrc (%Vuuid)\n", rc, pUuid));
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync return rc;
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync}
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @copydoc VBOXHDDBACKEND::pfnSetModificationUuid */
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsyncstatic int vdiSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync{
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync LogFlowFunc(("pBackendData=%#p Uuid=%Vuuid\n", pBackendData, pUuid));
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync int rc = VINF_SUCCESS;
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync Assert(pImage);
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync if (pImage)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->Header.u.v1.uuidModify = *pUuid;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Make it possible to clone old VDIs. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->Header.u.v0.uuidModify = *pUuid;
else
{
LogFunc(("Version is not supported!\n"));
rc = VERR_VDI_UNSUPPORTED_VERSION;
}
}
else
rc = VERR_VDI_IMAGE_READ_ONLY;
}
else
rc = VERR_VDI_NOT_OPENED;
LogFlowFunc(("returns %Vrc\n", rc));
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetParentUuid */
static int vdiGetParentUuid(void *pBackendData, PRTUUID pUuid)
{
LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
int rc;
Assert(pImage);
if (pImage)
{
*pUuid = *getImageParentUUID(&pImage->Header);
rc = VINF_SUCCESS;
}
else
rc = VERR_VDI_NOT_OPENED;
LogFlowFunc(("returns %Vrc (%Vuuid)\n", rc, pUuid));
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetParentUuid */
static int vdiSetParentUuid(void *pBackendData, PCRTUUID pUuid)
{
LogFlowFunc(("pBackendData=%#p Uuid=%Vuuid\n", pBackendData, pUuid));
PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
int rc = VINF_SUCCESS;
Assert(pImage);
if (pImage)
{
if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
{
if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
pImage->Header.u.v1.uuidLinkage = *pUuid;
/* Make it possible to clone old VDIs. */
else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
pImage->Header.u.v0.uuidLinkage = *pUuid;
else
{
LogFunc(("Version is not supported!\n"));
rc = VERR_VDI_UNSUPPORTED_VERSION;
}
}
else
rc = VERR_VDI_IMAGE_READ_ONLY;
}
else
rc = VERR_VDI_NOT_OPENED;
LogFlowFunc(("returns %Vrc\n", rc));
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetParentModificationUuid */
static int vdiGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
{
LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
int rc;
Assert(pImage);
if (pImage)
{
*pUuid = *getImageParentModificationUUID(&pImage->Header);
rc = VINF_SUCCESS;
}
else
rc = VERR_VDI_NOT_OPENED;
LogFlowFunc(("returns %Vrc (%Vuuid)\n", rc, pUuid));
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetParentModificationUuid */
static int vdiSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
{
LogFlowFunc(("pBackendData=%#p Uuid=%Vuuid\n", pBackendData, pUuid));
PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
int rc = VINF_SUCCESS;
Assert(pImage);
if (pImage)
{
if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
{
if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
pImage->Header.u.v1.uuidParentModify = *pUuid;
else
{
LogFunc(("Version is not supported!\n"));
rc = VERR_VDI_UNSUPPORTED_VERSION;
}
}
else
rc = VERR_VDI_IMAGE_READ_ONLY;
}
else
rc = VERR_VDI_NOT_OPENED;
LogFlowFunc(("returns %Vrc\n", rc));
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnDump */
static void vdiDump(void *pBackendData)
{
PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
RTLogPrintf("Dumping VDI image \"%s\" mode=%s uOpenFlags=%X File=%08X\n",
pImage->pszFilename,
(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY) ? "r/o" : "r/w",
pImage->uOpenFlags,
pImage->File);
RTLogPrintf("Header: Version=%08X Type=%X Flags=%X Size=%llu\n",
pImage->PreHeader.u32Version,
getImageType(&pImage->Header),
getImageFlags(&pImage->Header),
getImageDiskSize(&pImage->Header));
RTLogPrintf("Header: cbBlock=%u cbBlockExtra=%u cBlocks=%u cBlocksAllocated=%u\n",
getImageBlockSize(&pImage->Header),
getImageExtraBlockSize(&pImage->Header),
getImageBlocks(&pImage->Header),
getImageBlocksAllocated(&pImage->Header));
RTLogPrintf("Header: offBlocks=%u offData=%u\n",
getImageBlocksOffset(&pImage->Header),
getImageDataOffset(&pImage->Header));
PVDIDISKGEOMETRY pg = getImageLCHSGeometry(&pImage->Header);
if (pg)
RTLogPrintf("Header: Geometry: C/H/S=%u/%u/%u cbSector=%u\n",
pg->cCylinders, pg->cHeads, pg->cSectors, pg->cbSector);
RTLogPrintf("Header: uuidCreation={%Vuuid}\n", getImageCreationUUID(&pImage->Header));
RTLogPrintf("Header: uuidModification={%Vuuid}\n", getImageModificationUUID(&pImage->Header));
RTLogPrintf("Header: uuidParent={%Vuuid}\n", getImageParentUUID(&pImage->Header));
if (GET_MAJOR_HEADER_VERSION(&pImage->Header) >= 1)
RTLogPrintf("Header: uuidParentModification={%Vuuid}\n", getImageParentModificationUUID(&pImage->Header));
RTLogPrintf("Image: fFlags=%08X offStartBlocks=%u offStartData=%u\n",
pImage->uImageFlags, pImage->offStartBlocks, pImage->offStartData);
RTLogPrintf("Image: uBlockMask=%08X uShiftIndex2Offset=%u uShiftOffset2Index=%u offStartBlockData=%u\n",
pImage->uBlockMask,
pImage->uShiftIndex2Offset,
pImage->uShiftOffset2Index,
pImage->offStartBlockData);
unsigned uBlock, cBlocksNotFree, cBadBlocks, cBlocks = getImageBlocks(&pImage->Header);
for (uBlock=0, cBlocksNotFree=0, cBadBlocks=0; uBlock<cBlocks; uBlock++)
{
if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
{
cBlocksNotFree++;
if (pImage->paBlocks[uBlock] >= cBlocks)
cBadBlocks++;
}
}
if (cBlocksNotFree != getImageBlocksAllocated(&pImage->Header))
{
RTLogPrintf("!! WARNING: %u blocks actually allocated (cBlocksAllocated=%u) !!\n",
cBlocksNotFree, getImageBlocksAllocated(&pImage->Header));
}
if (cBadBlocks)
{
RTLogPrintf("!! WARNING: %u bad blocks found !!\n",
cBadBlocks);
}
}
VBOXHDDBACKEND g_VDIBackend =
{
/* pszBackendName */
"VDI",
/* cbSize */
sizeof(VBOXHDDBACKEND),
/* pfnCheckIfValid */
vdiCheckIfValid,
/* pfnOpen */
vdiOpen,
/* pfnCreate */
vdiCreate,
/* pfnRename */
vdiRename,
/* pfnClose */
vdiClose,
/* pfnRead */
vdiRead,
/* pfnWrite */
vdiWrite,
/* pfnFlush */
vdiFlush,
/* pfnGetVersion */
vdiGetVersion,
/* pfnGetImageType */
vdiGetImageType,
/* pfnGetSize */
vdiGetSize,
/* pfnGetFileSize */
vdiGetFileSize,
/* pfnGetPCHSGeometry */
vdiGetPCHSGeometry,
/* pfnSetPCHSGeometry */
vdiSetPCHSGeometry,
/* pfnGetLCHSGeometry */
vdiGetLCHSGeometry,
/* pfnSetLCHSGeometry */
vdiSetLCHSGeometry,
/* pfnGetImageFlags */
vdiGetImageFlags,
/* pfnGetOpenFlags */
vdiGetOpenFlags,
/* pfnSetOpenFlags */
vdiSetOpenFlags,
/* pfnGetComment */
vdiGetComment,
/* pfnSetComment */
vdiSetComment,
/* pfnGetUuid */
vdiGetUuid,
/* pfnSetUuid */
vdiSetUuid,
/* pfnGetModificationUuid */
vdiGetModificationUuid,
/* pfnSetModificationUuid */
vdiSetModificationUuid,
/* pfnGetParentUuid */
vdiGetParentUuid,
/* pfnSetParentUuid */
vdiSetParentUuid,
/* pfnGetParentModificationUuid */
vdiGetParentModificationUuid,
/* pfnSetParentModificationUuid */
vdiSetParentModificationUuid,
/* pfnDump */
vdiDump
};