VDIHDDCore.cpp revision 19a26ab547c9219a638a0508bd56456d6e67e439
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Virtual Disk Image (VDI), Core Code.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Copyright (C) 2006-2007 innotek GmbH
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* Header Files *
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync*******************************************************************************/
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync#define VBOX_VDICORE_VD /* Signal that the header is included from here. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/*******************************************************************************
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync* Internal Functions *
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync*******************************************************************************/
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,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic void vdiSetupImageDesc(PVDIIMAGEDESC pImage);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic void vdiFreeImage(PVDIIMAGEDESC pImage, bool fDelete);
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync * Internal: signal an error to the frontend.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncDECLINLINE(int) vdiError(PVDIIMAGEDESC pImage, int rc, RT_SRC_POS_DECL,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync const char *pszFormat, ...)
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync pImage->pfnError(pImage->pvErrorUser, rc, RT_SRC_POS_ARGS,
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync * internal: return power of 2 or 0 if num error.
ae5fc6ddd3d2ccdb941b1aacd23f6791fecc3354vboxsync unsigned uPower2 = 0;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Internal: Init VDI preheader.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync memset(pPreHdr->szFileInfo, 0, sizeof(pPreHdr->szFileInfo));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync strncat(pPreHdr->szFileInfo, VDI_IMAGE_FILE_INFO, sizeof(pPreHdr->szFileInfo));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Internal: check VDI preheader.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiValidatePreHeader(PVDIPREHEADER pPreHdr)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if ( VDI_GET_VERSION_MAJOR(pPreHdr->u32Version) != VDI_IMAGE_VERSION_MAJOR
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync && pPreHdr->u32Version != 0x00000002) /* old version. */
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync * Internal: Init VDI header. Always use latest header version.
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync * @param pHeader Assumes it was initially initialized to all zeros.
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsyncstatic void vdiInitHeader(PVDIHEADER pHeader, VDIMAGETYPE enmType, uint32_t uImageFlags,
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync const char *pszComment, uint64_t cbDisk, uint32_t cbBlock,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pHeader->u.v1.u32Type = (uint32_t)( enmType == VD_IMAGE_TYPE_NORMAL
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Assert(!memcmp(pHeader->u.v1.szComment, achZero, VDI_IMAGE_COMMENT_SIZE));
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 /* Mark the legacy geometry not-calculated. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pHeader->u.v1.LegacyGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pHeader->u.v1.u32Dummy = 0; /* used to be the translation value */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pHeader->u.v1.cBlocks = (uint32_t)(cbDisk / cbBlock);
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 /* Init uuids. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Mark LCHS geometry not-calculated. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pHeader->u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Internal: Check VDI header.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Check version-dependend header parameters. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Old header version. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Current header version. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (getImageBlocksOffset(pHeader) < (sizeof(VDIPREHEADER) + sizeof(VDIHEADER1)))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync getImageBlocksOffset(pHeader), sizeof(VDIPREHEADER) + sizeof(VDIHEADER1)));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (getImageDataOffset(pHeader) < (getImageBlocksOffset(pHeader) + getImageBlocks(pHeader) * sizeof(VDIIMAGEBLOCKPOINTER)))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogRel(("VDI: v1 image data offset wrong (%d < %d)\n",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync getImageDataOffset(pHeader), getImageBlocksOffset(pHeader) + getImageBlocks(pHeader) * sizeof(VDIIMAGEBLOCKPOINTER)));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Unsupported. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Check common header parameters. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync bool fFailed = false;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogRel(("VDI: bad image type %d\n", getImageType(pHeader)));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (getImageFlags(pHeader) & ~VD_VDI_IMAGE_FLAGS_MASK)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogRel(("VDI: bad image flags %08x\n", getImageFlags(pHeader)));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync && (getImageLCHSGeometry(pHeader))->cbSector != VDI_GEOMETRY_SECTOR_SIZE)
7755c0a4b7b215f612d9d27848c2584ffafe7a66vboxsync (getImageLCHSGeometry(pHeader))->cbSector, VDI_GEOMETRY_SECTOR_SIZE));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync getImageDiskSize(pHeader), getImageBlockSize(pHeader),
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync getImageBlocks(pHeader), getPowerOfTwo(getImageBlockSize(pHeader))));
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync if (getImageBlocksAllocated(pHeader) > getImageBlocks(pHeader))
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 && getPowerOfTwo(getImageExtraBlockSize(pHeader)) == 0)
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync getImageExtraBlockSize(pHeader), getPowerOfTwo(getImageExtraBlockSize(pHeader))));
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync if ((uint64_t)getImageBlockSize(pHeader) * getImageBlocks(pHeader) < getImageDiskSize(pHeader))
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync getImageBlockSize(pHeader), getImageBlocks(pHeader), getImageDiskSize(pHeader)));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (RTUuidIsNull(getImageModificationUUID(pHeader)))
78df65edff21c11c537f38e736707ea434ab5623vboxsync return fFailed ? VERR_VDI_INVALID_HEADER : VINF_SUCCESS;
78df65edff21c11c537f38e736707ea434ab5623vboxsync * Internal: Set up VDIIMAGEDESC structure by image header.
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->uShiftOffset2Index = getPowerOfTwo(getImageBlockSize(&pImage->Header));
78df65edff21c11c537f38e736707ea434ab5623vboxsync pImage->offStartBlockData = getImageExtraBlockSize(&pImage->Header);
78df65edff21c11c537f38e736707ea434ab5623vboxsync pImage->uShiftIndex2Offset += getPowerOfTwo(pImage->offStartBlockData);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Internal: Create VDI image file.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiCreateImage(PVDIIMAGEDESC pImage, VDIMAGETYPE enmType,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Special check for comment length. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = vdiError(pImage, VERR_VDI_COMMENT_TOO_LONG, RT_SRC_POS, N_("VDI: comment is too long for '%s'"), pImage->pszFilename);
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. */
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 pImage->paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* for growing images mark all blocks in paBlocks as free. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync for (unsigned i = 0; i < pImage->Header.u.v1.cBlocks; i++)
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->Header.u.v1.cBlocksAllocated = pImage->Header.u.v1.cBlocks;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Setup image parameters. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* create file */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTFILE_O_READWRITE | RTFILE_O_CREATE | RTFILE_O_DENY_ALL);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: cannot create image '%s'"), pImage->pszFilename);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync + ((uint64_t)getImageBlocks(&pImage->Header) << pImage->uShiftIndex2Offset);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* check the free space on the disk and leave early if there is not
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * sufficient space available */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = RTFsQuerySizes(pImage->pszFilename, NULL, &cbFree, NULL, NULL);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (VBOX_SUCCESS(rc) /* ignore errors */ && ((uint64_t)cbFree < cbTotal))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = vdiError(pImage, VERR_DISK_FULL, RT_SRC_POS, N_("VDI: disk would overflow creating image '%s'"), pImage->pszFilename);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Allocate & commit whole file if fixed image, it must be more
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * effective than expanding file by write operations.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Set file size to hold header and blocks array. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = RTFileSetSize(pImage->File, pImage->offStartData);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: setting image size failed for '%s'"), pImage->pszFilename);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Generate image last-modify uuid */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync RTUuidCreate(getImageModificationUUID(&pImage->Header));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Write pre-header. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = RTFileWriteAt(pImage->File, 0, &pImage->PreHeader, sizeof(pImage->PreHeader), NULL);
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing pre-header failed for '%s'"), pImage->pszFilename);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Write header. */
48807baed22246206b9855f41d1fd106f0679b3avboxsync rc = RTFileWriteAt(pImage->File, sizeof(pImage->PreHeader), &pImage->Header.u.v1plus, sizeof(pImage->Header.u.v1plus), NULL);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing header failed for '%s'"), pImage->pszFilename);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = RTFileWriteAt(pImage->File, pImage->offStartBlocks,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER),
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing block pointers failed for '%s'"), pImage->pszFilename);
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 /** @todo Starting with Linux 2.6.23, there is an fallocate() system call.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Currently supported file systems are ext4 and ocfs2. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Allocate a temporary zero-filled buffer. Use a bigger block size to optimize writing */
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync cbFill = (uint64_t)getImageBlocks(&pImage->Header) << pImage->uShiftIndex2Offset;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* do loop to fill all image. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync unsigned cbChunk = (unsigned)RT_MIN(cbFill, cbBuf);
090c459b9e90ca46e2ce2b8c81533ade3b23f3e9vboxsync rc = RTFileWriteAt(pImage->File, pImage->offStartData + uOff,
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing block failed for '%s'"), pImage->pszFilename);
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync * Internal: Open a VDI image.
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsyncstatic int vdiOpenImage(PVDIIMAGEDESC pImage, unsigned uOpenFlags)
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync * Open the image.
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync ? RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync : RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
cbfe19957042925c835c6aee6722a0e7af852783vboxsync /* Do NOT signal an appropriate error here, as the VD layer has the
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync * choice of retrying the open if it failed. */
cbfe19957042925c835c6aee6722a0e7af852783vboxsync /* Read pre-header. */
cbfe19957042925c835c6aee6722a0e7af852783vboxsync rc = RTFileReadAt(File, 0, &pImage->PreHeader, sizeof(pImage->PreHeader),
cbfe19957042925c835c6aee6722a0e7af852783vboxsync rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading pre-header in '%s'"), pImage->pszFilename);
7fee49908ea4b9f6cb4f9cc745633c4969ed6318vboxsync rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: invalid pre-header in '%s'"), pImage->pszFilename);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Read header. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->Header.uVersion = pImage->PreHeader.u32Version;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading v0 header in '%s'"), pImage->pszFilename);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading v1 header in '%s'"), pImage->pszFilename);
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 && pImage->Header.u.v1.cbHeader < sizeof(pImage->Header.u.v1plus))
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.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else if (pImage->Header.u.v1.cbHeader >= sizeof(pImage->Header.u.v1plus))
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 rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading v1.1+ header in '%s'"), pImage->pszFilename);
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 rc = vdiError(pImage, VERR_VDI_UNSUPPORTED_VERSION, RT_SRC_POS, N_("VDI: invalid header in '%s'"), pImage->pszFilename);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Setup image parameters by header. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Allocate memory for blocks array. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage->paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Read blocks array. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = RTFileReadAt(pImage->File, pImage->offStartBlocks, pImage->paBlocks,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER),
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Internal: Save header to file.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = RTFileWriteAt(pImage->File, sizeof(VDIPREHEADER), &pImage->Header.u.v0, sizeof(pImage->Header.u.v0), NULL);
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 rc = RTFileWriteAt(pImage->File, sizeof(VDIPREHEADER), &pImage->Header.u.v1plus, sizeof(pImage->Header.u.v1plus), NULL);
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync AssertMsgRC(rc, ("vdiUpdateHeader failed, filename=\"%s\" rc=%Vrc\n", pImage->pszFilename, rc));
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync * Internal: Save block pointer to file, save header to file.
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsyncstatic int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock)
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync /* Update image header. */
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync /* write only one block pointer. */
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync pImage->offStartBlocks + uBlock * sizeof(VDIIMAGEBLOCKPOINTER),
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync AssertMsgRC(rc, ("vdiUpdateBlockInfo failed to update block=%u, filename=\"%s\", rc=%Vrc\n",
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync * Internal: Flush the image file to disk.
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync /* Save header. */
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync AssertMsgRC(rc, ("vdiUpdateHeader() failed, filename=\"%s\", rc=%Vrc\n",
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * Internal: Free all allocated space for representing an image, and optionally
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * delete the image from disk.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic void vdiFreeImage(PVDIIMAGEDESC pImage, bool fDelete)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @copydoc VBOXHDDBACKEND::pfnCheckIfValid */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("pszFilename=\"%s\"\n", pszFilename));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync rc = vdiOpenImage(pImage, VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_READONLY);
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync/** @copydoc VBOXHDDBACKEND::pfnOpen */
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsyncstatic int vdiOpen(const char *pszFilename, unsigned uOpenFlags,
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x ppBackendData=%#p\n", pszFilename, uOpenFlags, ppBackendData));
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync /* Check open flags. All valid flags are supported. */
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync /* Check remaining arguments. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("returns %Vrc (pBackendData=%#p)\n", rc, *ppBackendData));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @copydoc VBOXHDDBACKEND::pfnCreate */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiCreate(const char *pszFilename, VDIMAGETYPE enmType,
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 /* Check open flags. All valid flags are supported. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Check remaining arguments. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync || (enmType != VD_IMAGE_TYPE_NORMAL && enmType != VD_IMAGE_TYPE_FIXED)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync rc = vdiCreateImage(pImage, enmType, cbSize, uImageFlags, pszComment,
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 LogFlowFunc(("returns %Vrc (pBackendData=%#p)\n", rc, *ppBackendData));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @copydoc VBOXHDDBACKEND::pfnRename */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiRename(void *pBackendData, const char *pszFilename)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @copydoc VBOXHDDBACKEND::pfnClose */
6abae205eac4f4a16f6ad5a2a71ed1f36d6a57b0vboxsyncstatic int vdiClose(void *pBackendData, bool fDelete)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Freeing a never allocated image (e.g. because the open failed) is
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync * not signalled as an error. After all nothing bad happens. */
37a7e3e6f9ce5f6cabeb3f734044e9e8ca9cb1bfvboxsync/** @copydoc VBOXHDDBACKEND::pfnRead */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiRead(void *pBackendData, uint64_t uOffset, void *pvBuf,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbToRead=%zu pcbActuallyRead=%#p\n", pBackendData, uOffset, pvBuf, cbToRead, pcbActuallyRead));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync if ( uOffset + cbToRead > getImageDiskSize(&pImage->Header)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Calculate starting block number and offset inside it. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Clip read range to at most the rest of the block. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync cbToRead = RT_MIN(cbToRead, getImageBlockSize(&pImage->Header) - offRead);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_FREE)
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync else if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO)
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);
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync/**@copydoc VBOXHDDBACKEND::pfnWrite */
602e0e27740395dba64bee2e0a8aef023ebd7650vboxsyncstatic int vdiWrite(void *pBackendData, uint64_t uOffset, const void *pvBuf,
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 /* 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 /* Calculate starting block number and offset inside it. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Clip write range to at most the rest of the block. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync cbToWrite = RT_MIN(cbToWrite, getImageBlockSize(&pImage->Header) - offWrite);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
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 /* 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. */
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync if (ASMBitFirstSet((volatile void *)pvBuf, (uint32_t)cbToWrite * 8) == -1)
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync if (cbToWrite == getImageBlockSize(&pImage->Header))
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync /* Full block write to previously unallocated block.
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync * Allocate block and write data. */
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync ((uint64_t)cBlocksAllocated << pImage->uShiftIndex2Offset)
5d6df5999c0e844db1af3c6def0a9abac5120d3bvboxsync setImageBlocksAllocated(&pImage->Header, cBlocksAllocated + 1);
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;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync ((uint64_t)pImage->paBlocks[uBlock] << pImage->uShiftIndex2Offset)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync + pImage->offStartData + pImage->offStartBlockData + offWrite,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @copydoc VBOXHDDBACKEND::pfnFlush */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @copydoc VBOXHDDBACKEND::pfnGetVersion */
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsync/** @copydoc VBOXHDDBACKEND::pfnGetImageType */
ed82223f8f7be1435bf9b203a0ee4fbdfc856dc8vboxsyncstatic int vdiGetImageType(void *pBackendData, PVDIMAGETYPE penmImageType)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("pBackendData=%#p penmImageType=%#p\n", pBackendData, penmImageType));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync *penmImageType = getImageType(&pImage->Header) == VDI_IMAGE_TYPE_NORMAL
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("returns %Vrc enmImageType=%u\n", rc, *penmImageType));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @copydoc VBOXHDDBACKEND::pfnGetSize */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync/** @copydoc VBOXHDDBACKEND::pfnGetFileSize */
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync/** @copydoc VBOXHDDBACKEND::pfnGetPCHSGeometry */
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
db07105a44f53e9e2377ade02cd3dbe482647cb9vboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync LogFlowFunc(("returns %Vrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync/** @copydoc VBOXHDDBACKEND::pfnSetPCHSGeometry */
cbfe19957042925c835c6aee6722a0e7af852783vboxsync LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @copydoc VBOXHDDBACKEND::pfnGetLCHSGeometry */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
a0a3a26a4065b9401681a8c99a11bd83e08f94ccvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
78df65edff21c11c537f38e736707ea434ab5623vboxsync VDIDISKGEOMETRY DummyGeo = { 0, 0, 0, VDI_GEOMETRY_SECTOR_SIZE };
78df65edff21c11c537f38e736707ea434ab5623vboxsync PVDIDISKGEOMETRY pGeometry = getImageLCHSGeometry(&pImage->Header);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("returns %Vrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @copydoc VBOXHDDBACKEND::pfnSetLCHSGeometry */
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync /* Update header information in base image file. */
63a3c5be7ac89d0d5b3386988f8c2f78df0cacf6vboxsync/** @copydoc VBOXHDDBACKEND::pfnGetImageFlags */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic unsigned vdiGetImageFlags(void *pBackendData)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
740011a32913a7f759d5b747def605a2b820c3fcvboxsync/** @copydoc VBOXHDDBACKEND::pfnGetOpenFlags */
be302cc3dbc037aaebbadbf9057662a88b3b23a8vboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
d8707f50ca9ff2792d9acb7addb52467098d1b47vboxsync/** @copydoc VBOXHDDBACKEND::pfnSetOpenFlags */
d8707f50ca9ff2792d9acb7addb52467098d1b47vboxsyncstatic int vdiSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync LogFlowFunc(("pBackendData=%#p\n uOpenFlags=%#x", pBackendData, uOpenFlags));
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
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 /* Implement this operation via reopening the image. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @copydoc VBOXHDDBACKEND::pfnGetComment */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiGetComment(void *pBackendData, char *pszComment,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* memcpy is much better than strncpy. */
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync LogFlowFunc(("returns %Vrc comment=\"%s\"\n", rc, pszComment));
b2e90826ea719b22452d1ff7b977d4f40995b428vboxsync/** @copydoc VBOXHDDBACKEND::pfnGetComment */
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsyncstatic int vdiSetComment(void *pBackendData, const char *pszComment)
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync size_t cchComment = pszComment ? strlen(pszComment) : 0;
f3abe19bdaeea1b354674327ee95707281a6ad83vboxsync LogFunc(("pszComment is too long, %d bytes!\n", cchComment));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* we don't support old style images */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync * Update the comment field, making sure to zero out all of the previous comment.
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync memset(pImage->Header.u.v1.szComment, '\0', VDI_IMAGE_COMMENT_SIZE);
f632be0ea31134f1ce343e84c90d7984d2bf96b2vboxsync memcpy(pImage->Header.u.v1.szComment, pszComment, cchComment);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* write out new the header */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @copydoc VBOXHDDBACKEND::pfnGetUuid */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiGetUuid(void *pBackendData, PRTUUID pUuid)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("returns %Vrc (%Vuuid)\n", rc, pUuid));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @copydoc VBOXHDDBACKEND::pfnSetUuid */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncstatic int vdiSetUuid(void *pBackendData, PCRTUUID pUuid)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync LogFlowFunc(("pBackendData=%#p Uuid=%Vuuid\n", pBackendData, pUuid));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Make it possible to clone old VDIs. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync/** @copydoc VBOXHDDBACKEND::pfnGetModificationUuid */
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsyncstatic int vdiGetModificationUuid(void *pBackendData, PRTUUID pUuid)
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync *pUuid = *getImageModificationUUID(&pImage->Header);
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync LogFlowFunc(("returns %Vrc (%Vuuid)\n", rc, pUuid));
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync/** @copydoc VBOXHDDBACKEND::pfnSetModificationUuid */
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsyncstatic int vdiSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync LogFlowFunc(("pBackendData=%#p Uuid=%Vuuid\n", pBackendData, pUuid));
b14965bee0bfb359e6ce9db6beaa4add9935fe3cvboxsync PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync /* Make it possible to clone old VDIs. */
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
return rc;
int rc;
if (pImage)
return rc;
if (pImage)
return rc;
int rc;
if (pImage)
return rc;
if (pImage)
return rc;
if (pg)
RTLogPrintf("Header: uuidParentModification={%Vuuid}\n", getImageParentModificationUUID(&pImage->Header));
RTLogPrintf("Image: uBlockMask=%08X uShiftIndex2Offset=%u uShiftOffset2Index=%u offStartBlockData=%u\n",
cBadBlocks++;
if (cBadBlocks)
sizeof(VBOXHDDBACKEND),