QED.cpp revision 35f5e9bef11abdf4f242ba8140229704fa063a0a
/* $Id$ */
/** @file
* QED - QED Disk image.
*/
/*
* Copyright (C) 2011-2013 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_VD_QED
#include <VBox/vd-plugin.h>
/**
* The QED backend implements support for the qemu enhanced disk format (short QED)
* The specification for the format is available under http://wiki.qemu.org/Features/QED/Specification
*
* Missing things to implement:
* - compaction
* - resizing which requires block relocation (very rare case)
*/
/*******************************************************************************
* Structures in a QED image, little endian *
*******************************************************************************/
#pragma pack(1)
typedef struct QedHeader
{
/** Magic value. */
/** Cluster size in bytes. */
/** Size of L1 and L2 tables in clusters. */
/** size of this header structure in clusters. */
/** Features used for the image. */
/** Compatibility features used for the image. */
/** Self resetting feature bits. */
/** Offset of the L1 table in bytes. */
/** Logical image size as seen by the guest. */
/** Offset of the backing filename in bytes. */
/** Size of the backing filename. */
} QedHeader;
#pragma pack()
/** Pointer to a on disk QED header. */
typedef QedHeader *PQedHeader;
/** QED magic value. */
/** Cluster size minimum. */
/** Cluster size maximum. */
/** L1 and L2 Table size minimum. */
#define QED_TABLE_SIZE_MIN 1
/** L1 and L2 Table size maximum. */
#define QED_TABLE_SIZE_MAX 16
/** QED default cluster size when creating an image. */
/** The default table size in clusters. */
#define QED_TABLE_SIZE_DEFAULT 4
/** Feature flags.
* @{
*/
/** Image uses a backing file to provide data for unallocated clusters. */
#define QED_FEATURE_BACKING_FILE RT_BIT_64(0)
/** Image needs checking before use. */
/** Don't probe for format of the backing file, treat as raw image. */
/** Mask of valid features. */
#define QED_FEATURE_MASK (QED_FEATURE_BACKING_FILE | QED_FEATURE_NEED_CHECK | QED_FEATURE_BACKING_FILE_NO_PROBE)
/** @} */
/** Compatibility feature flags.
* @{
*/
/** Mask of valid compatibility features. */
#define QED_COMPAT_FEATURE_MASK (0)
/** @} */
/** Autoreset feature flags.
* @{
*/
/** Mask of valid autoreset features. */
#define QED_AUTORESET_FEATURE_MASK (0)
/** @} */
/*******************************************************************************
* Constants And Macros, Structures and Typedefs *
*******************************************************************************/
/**
* QED L2 cache entry.
*/
typedef struct QEDL2CACHEENTRY
{
/** List node for the search list. */
/** List node for the LRU list. */
/** Reference counter. */
/** The offset of the L2 table, used as search key. */
/** Pointer to the cached L2 table. */
/** Maximum amount of memory the cache is allowed to use. */
/**
* QED image data structure.
*/
typedef struct QEDIMAGE
{
/** Image name. */
const char *pszFilename;
/** Storage handle. */
/** Pointer to the per-disk VD interface list. */
/** Pointer to the per-image VD interface list. */
/** Error interface. */
/** I/O interface. */
/** Open flags passed by VBoxHD layer. */
unsigned uOpenFlags;
/** Image flags defined during creation or determined during open. */
unsigned uImageFlags;
/** Total size of the image. */
/** Physical geometry of this image. */
/** Logical geometry of this image. */
/** Filename of the backing file if any. */
char *pszBackingFilename;
/** Offset of the filename in the image. */
/** Size of the backing filename excluding \0. */
/** Size of the image, multiple of clusters. */
/** Cluster size in bytes. */
/** Number of entries in the L1 and L2 table. */
/** Size of an L1 or L2 table rounded to the next cluster size. */
/** Pointer to the L1 table. */
/** Offset of the L1 table. */
/** Offset mask for a cluster. */
/** L1 table mask to get the L1 index. */
/** Number of bits to shift to get the L1 index. */
/** L2 table mask to get the L2 index. */
/** Number of bits to shift to get the L2 index. */
/** Memory occupied by the L2 table cache. */
/** The sorted L2 entry list used for searching. */
/** The LRU L2 entry list used for eviction. */
/**
* State of the async cluster allocation.
*/
typedef enum QEDCLUSTERASYNCALLOCSTATE
{
/** Invalid. */
/** L2 table allocation. */
/** Link L2 table into L1. */
/** Allocate user data cluster. */
/** Link user data cluster. */
/** 32bit blowup. */
QEDCLUSTERASYNCALLOCSTATE_32BIT_HACK = 0x7fffffff
/**
* Data needed to track async cluster allocation.
*/
typedef struct QEDCLUSTERASYNCALLOC
{
/** The state of the cluster allocation. */
/** Old image size to rollback in case of an error. */
/** L1 index to link if any. */
/** L2 index to link, required in any case. */
/** Start offset of the allocated cluster. */
/** L2 cache entry if a L2 table is allocated. */
/** Number of bytes to write. */
/*******************************************************************************
* Static Variables *
*******************************************************************************/
/** NULL-terminated array of supported file extensions. */
static const VDFILEEXTENSION s_aQedFileExtensions[] =
{
{"qed", VDTYPE_HDD},
};
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/**
* Converts the image header to the host endianess and performs basic checks.
*
* @returns Whether the given header is valid or not.
* @param pHeader Pointer to the header to convert.
*/
{
return false;
return false;
return false;
return false;
return true;
}
/**
* Creates a QED header from the given image state.
*
* @returns nothing.
* @param pImage Image instance data.
* @param pHeader Pointer to the header to convert.
*/
{
pHeader->u64FeatureFlags = RT_H2LE_U64(pImage->pszBackingFilename ? QED_FEATURE_BACKING_FILE : UINT64_C(0));
}
/**
* Convert table entries from little endian to host endianess.
*
* @returns nothing.
* @param paTbl Pointer to the table.
* @param cEntries Number of entries in the table.
*/
{
while(cEntries-- > 0)
{
paTbl++;
}
}
/**
* Convert table entries from host to little endian format.
*
* @returns nothing.
* @param paTblImg Pointer to the table which will store the little endian table.
* @param paTbl The source table to convert.
* @param cEntries Number of entries in the table.
*/
{
while(cEntries-- > 0)
{
paTbl++;
paTblImg++;
}
}
/**
* Creates the L2 table cache.
*
* @returns VBox status code.
* @param pImage The image instance data.
*/
{
return VINF_SUCCESS;
}
/**
* Destroys the L2 table cache.
*
* @returns nothing.
* @param pImage The image instance data.
*/
{
{
}
}
/**
* Returns the L2 table matching the given offset or NULL if none could be found.
*
* @returns Pointer to the L2 table cache entry or NULL.
* @param pImage The image instance data.
* @param offL2Tbl Offset of the L2 table to search for.
*/
{
{
break;
}
{
/* Update LRU list. */
return pL2Entry;
}
else
return NULL;
}
/**
* Releases a L2 table cache entry.
*
* @returns nothing.
* @param pL2Entry The L2 cache entry.
*/
{
}
/**
* Allocates a new L2 table from the cache evicting old entries if required.
*
* @returns Pointer to the L2 cache entry or NULL.
* @param pImage The image instance data.
*/
{
int rc = VINF_SUCCESS;
{
/* Add a new entry. */
if (pL2Entry)
{
{
}
else
{
}
}
}
else
{
/* Evict the last not in use entry and use it */
{
break;
}
{
}
else
}
return pL2Entry;
}
/**
* Frees a L2 table cache entry.
*
* @returns nothing.
* @param pImage The image instance data.
* @param pL2Entry The L2 cache entry to free.
*/
{
}
/**
* Inserts an entry in the L2 table cache.
*
* @returns nothing.
* @param pImage The image instance data.
* @param pL2Entry The L2 cache entry to insert.
*/
{
/* Insert at the top of the LRU list. */
{
}
else
{
/* Insert into search list. */
else
{
bool fInserted = false;
{
{
fInserted = true;
break;
}
}
}
}
}
/**
* Fetches the L2 from the given offset trying the LRU cache first and
* reading it from the image after a cache miss.
*
* @returns VBox status code.
* @param pImage Image instance data.
* @param offL2Tbl The offset of the L2 table in the image.
* @param ppL2Entry Where to store the L2 table on success.
*/
{
int rc = VINF_SUCCESS;
/* Try to fetch the L2 table from the cache first. */
if (!pL2Entry)
{
LogFlowFunc(("Reading L2 table from image\n"));
if (pL2Entry)
{
/* Read from the image. */
if (RT_SUCCESS(rc))
{
#if defined(RT_BIG_ENDIAN)
#endif
}
else
{
}
}
else
rc = VERR_NO_MEMORY;
}
if (RT_SUCCESS(rc))
return rc;
}
/**
* Fetches the L2 from the given offset trying the LRU cache first and
* reading it from the image after a cache miss - version for async I/O.
*
* @returns VBox status code.
* @param pImage Image instance data.
* @param pIoCtx The I/O context.
* @param offL2Tbl The offset of the L2 table in the image.
* @param ppL2Entry Where to store the L2 table on success.
*/
{
int rc = VINF_SUCCESS;
/* Try to fetch the L2 table from the cache first. */
if (!pL2Entry)
{
if (pL2Entry)
{
/* Read from the image. */
if (RT_SUCCESS(rc))
{
#if defined(RT_BIG_ENDIAN)
#endif
}
else
{
}
}
else
rc = VERR_NO_MEMORY;
}
if (RT_SUCCESS(rc))
return rc;
}
/**
* Return power of 2 or 0 if num error.
*
* @returns The power of 2 or 0 if the given number is not a power of 2.
* @param u32 The number.
*/
{
if (u32 == 0)
return 0;
while ((u32 & 1) == 0)
{
u32 >>= 1;
uPower2++;
}
}
/**
* Sets the L1, L2 and offset bitmasks and L1 and L2 bit shift members.
*
* @returns nothing.
* @param pImage The image instance data.
*/
{
}
/**
* Converts a given logical offset into the
*
* @returns nothing.
* @param pImage The image instance data.
* @param off The logical offset to convert.
* @param pidxL1 Where to store the index in the L1 table on success.
* @param pidxL2 Where to store the index in the L2 table on success.
* @param poffCluster Where to store the offset in the cluster on success.
*/
{
}
/**
* Converts Cluster size to a byte size.
*
* @returns Number of bytes derived from the given number of clusters.
* @param pImage The image instance data.
* @param cClusters The clusters to convert.
*/
{
}
/**
* Converts number of bytes to cluster size rounding to the next cluster.
*
* @returns Number of bytes derived from the given number of clusters.
* @param pImage The image instance data.
* @param cb Number of bytes to convert.
*/
{
}
/**
* Allocates a new cluster in the image.
*
* @returns The start offset of the new cluster in the image.
* @param pImage The image instance data.
* @param cCLusters Number of clusters to allocate.
*/
{
return offCluster;
}
/**
* Returns the real image offset for a given cluster or an error if the cluster is not
* yet allocated.
*
* @returns VBox status code.
* VERR_VD_BLOCK_FREE if the cluster is not yet allocated.
* @param pImage The image instance data.
* @param pIoCtx The I/O context.
* @param idxL1 The L1 index.
* @param idxL2 The L2 index.
* @param offCluster Offset inside the cluster.
* @param poffImage Where to store the image offset on success;
*/
{
int rc = VERR_VD_BLOCK_FREE;
{
&pL2Entry);
if (RT_SUCCESS(rc))
{
/* Get real file offset. */
else
}
}
return rc;
}
/**
* Internal. Flush image data to disk.
*/
{
int rc = VINF_SUCCESS;
{
#if defined(RT_BIG_ENDIAN)
if (paL1TblImg)
{
}
else
rc = VERR_NO_MEMORY;
#else
/* Write L1 table directly. */
#endif
if (RT_SUCCESS(rc))
{
/* Write header. */
sizeof(Header));
if (RT_SUCCESS(rc))
}
}
return rc;
}
/**
* Flush image data to disk - version for async I/O.
*
* @returns VBox status code.
* @param pImage The image instance data.
* @param pIoCtx The I/o context
*/
{
int rc = VINF_SUCCESS;
{
#if defined(RT_BIG_ENDIAN)
if (paL1TblImg)
{
}
else
rc = VERR_NO_MEMORY;
#else
/* Write L1 table directly. */
#endif
{
/* Write header. */
}
}
return rc;
}
/**
* Checks whether the given cluster offset is valid.
*
* @returns Whether the given cluster offset is valid.
* @param offCluster The table offset to check.
* @param cbFile The real file size of the image.
* @param cbCluster The cluster size in bytes.
*/
{
}
/**
* Checks whether the given table offset is valid.
*
* @returns Whether the given table offset is valid.
* @param offTbl The table offset to check.
* @param cbFile The real file size of the image.
* @param cbTable The table size in bytes.
* @param cbCluster The cluster size in bytes.
*/
DECLINLINE(bool) qedIsTblOffsetValid(uint64_t offTbl, uint64_t cbFile, size_t cbTable, size_t cbCluster)
{
}
/**
* Sets the specified range in the cluster bitmap checking whether any of the clusters is already
* used before.
*
* @returns Whether the range was clear and is set now.
* @param pvClusterBitmap The cluster bitmap to use.
* @param offClusterStart The first cluster to check and set.
* @param offClusterEnd The first cluster to not check and set anymore.
*/
static bool qedClusterBitmapCheckAndSet(void *pvClusterBitmap, uint32_t offClusterStart, uint32_t offClusterEnd)
{
return false;
return true;
}
/**
* Checks the given image for consistency, usually called when the
* QED_FEATURE_NEED_CHECK bit is set.
*
* @returns VBox status code.
* @retval VINF_SUCCESS when the image can be accessed.
* @param pImage The image instance data.
* @param pHeader The header to use for checking.
*
* @note It is not required that the image state is fully initialized Only
* The I/O interface and storage handle need to be valid.
* @note The header must be converted to the host CPU endian format already
* and should be validated already.
*/
{
void *pvClusterBitmap = NULL;
int rc = VINF_SUCCESS;
do
{
if (RT_FAILURE(rc))
{
N_("Qed: Querying the file size of image '%s' failed"),
break;
}
/* Allocate L1 table. */
if (!paL1Tbl)
{
N_("Qed: Allocating memory for the L1 table for image '%s' failed"),
break;
}
if (!paL2Tbl)
{
N_("Qed: Allocating memory for the L2 table for image '%s' failed"),
break;
}
if (!pvClusterBitmap)
{
N_("Qed: Allocating memory for the cluster bitmap for image '%s' failed"),
break;
}
/* Validate L1 table offset. */
{
N_("Qed: L1 table offset of image '%s' is corrupt (%llu)"),
break;
}
/* Read L1 table. */
if (RT_FAILURE(rc))
{
N_("Qed: Reading the L1 table from image '%s' failed"),
break;
}
/* Mark the L1 table in cluster bitmap. */
bool fSet = qedClusterBitmapCheckAndSet(pvClusterBitmap, offClusterStart, offClusterStart + pHeader->u32TableSize);
/* Scan the L1 and L2 tables for invalid entries. */
{
continue; /* Skip unallocated clusters. */
{
N_("Qed: Entry %d of the L1 table from image '%s' is invalid (%llu)"),
break;
}
/* Now check that the clusters are not allocated already. */
fSet = qedClusterBitmapCheckAndSet(pvClusterBitmap, offClusterStart, offClusterStart + pHeader->u32TableSize);
if (!fSet)
{
N_("Qed: Entry %d of the L1 table from image '%s' points to a already used cluster (%llu)"),
break;
}
/* Read the linked L2 table and check it. */
if (RT_FAILURE(rc))
{
N_("Qed: Reading the L2 table from image '%s' failed"),
break;
}
/* Check all L2 entries. */
{
continue; /* Skip unallocated clusters. */
{
N_("Qed: Entry %d of the L2 table from image '%s' is invalid (%llu)"),
break;
}
/* Now check that the clusters are not allocated already. */
if (!fSet)
{
N_("Qed: Entry %d of the L2 table from image '%s' points to a already used cluster (%llu)"),
break;
}
}
}
} while(0);
if (paL1Tbl)
if (paL2Tbl)
if (pvClusterBitmap)
return rc;
}
/**
* Internal. Free all allocated space for representing an image except pImage,
* and optionally delete the image from disk.
*/
{
int rc = VINF_SUCCESS;
/* Freeing a never allocated image (e.g. because the open failed) is
* not signalled as an error. After all nothing bad happens. */
if (pImage)
{
{
/* No point updating the file that is deleted anyway. */
if (!fDelete)
}
if (pImage->pszBackingFilename)
{
}
}
return rc;
}
/**
* Internal: Open an image, constructing all necessary data structures.
*/
{
int rc;
/*
* Open the image.
*/
false /* fCreate */),
if (RT_FAILURE(rc))
{
/* Do NOT signal an appropriate error here, as the VD layer has the
* choice of retrying the open if it failed. */
goto out;
}
if (RT_FAILURE(rc))
goto out;
{
if ( RT_SUCCESS(rc)
{
{
{
/* Image needs checking. */
if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
else
N_("Qed: Image '%s' needs checking but is opened readonly"),
}
if ( RT_SUCCESS(rc)
{
/* Load backing filename from image. */
pImage->pszBackingFilename = (char *)RTMemAllocZ(Header.u32BackingFilenameSize + 1); /* +1 for \0 terminator. */
if (pImage->pszBackingFilename)
{
}
else
rc = VERR_NO_MEMORY;
}
if (RT_SUCCESS(rc))
{
/* Allocate L1 table. */
{
/* Read from the image. */
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
/* If the consistency check succeeded, clear the flag by flushing the image. */
}
else
N_("Qed: Creating the L2 table cache for image '%s' failed"),
}
else
N_("Qed: Reading the L1 table for image '%s' failed"),
}
else
N_("Qed: Out of memory allocating L1 table for image '%s'"),
}
}
else
N_("Qed: The image '%s' makes use of unsupported features"),
}
else if (RT_SUCCESS(rc))
}
else
out:
if (RT_FAILURE(rc))
qedFreeImage(pImage, false);
return rc;
}
/**
* Internal: Create a qed image.
*/
unsigned uImageFlags, const char *pszComment,
unsigned uPercentStart, unsigned uPercentSpan)
{
int rc;
if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
{
rc = vdIfError(pImage->pIfError, VERR_VD_INVALID_TYPE, RT_SRC_POS, N_("Qed: cannot create fixed image '%s'"), pImage->pszFilename);
goto out;
}
/* Create image file. */
if (RT_FAILURE(rc))
{
rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("Qed: cannot create image '%s'"), pImage->pszFilename);
goto out;
}
/* Init image state. */
pImage->cbBackingFilename = 0;
pImage->offBackingFilename = 0;
/* Init L1 table. */
{
rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS, N_("Qed: cannot allocate memory for L1 table of image '%s'"),
goto out;
}
if (RT_FAILURE(rc))
{
rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("Qed: Failed to create L2 cache for image '%s'"),
goto out;
}
out:
if (RT_FAILURE(rc))
return rc;
}
/**
* Rollback anything done during async cluster allocation.
*
* @returns VBox status code.
* @param pImage The image instance data.
* @param pIoCtx The I/O context.
* @param pClusterAlloc The cluster allocation to rollback.
*/
static int qedAsyncClusterAllocRollback(PQEDIMAGE pImage, PVDIOCTX pIoCtx, PQEDCLUSTERASYNCALLOC pClusterAlloc)
{
int rc = VINF_SUCCESS;
switch (pClusterAlloc->enmAllocState)
{
{
/* Assumption right now is that the L1 table is not modified if the link fails. */
break;
}
{
/* Assumption right now is that the L2 table is not modified if the link fails. */
break;
}
default:
}
return rc;
}
/**
* Updates the state of the async cluster allocation.
*
* @returns VBox status code.
* @param pBackendData The opaque backend data.
* @param pIoCtx I/O context associated with this request.
* @param rcReq Status code for the completed request.
*/
static DECLCALLBACK(int) qedAsyncClusterAllocUpdate(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq)
{
int rc = VINF_SUCCESS;
if (RT_FAILURE(rcReq))
switch (pClusterAlloc->enmAllocState)
{
{
/* Update the link in the on disk L1 table now. */
if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
break;
else if (RT_FAILURE(rc))
{
/* Rollback. */
break;
}
/* Success, fall through. */
}
{
/* L2 link updated in L1 , save L2 entry in cache and allocate new user data cluster. */
/* Update the link in the in memory L1 table now. */
/* Write data. */
if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
break;
else if (RT_FAILURE(rc))
{
break;
}
}
{
/* Link L2 table and update it. */
if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
break;
else if (RT_FAILURE(rc))
{
break;
}
}
{
/* Everything done without errors, signal completion. */
rc = VINF_SUCCESS;
break;
}
default:
AssertMsgFailed(("Invalid async cluster allocation state %d\n",
}
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnCheckIfValid */
{
LogFlowFunc(("pszFilename=\"%s\" pVDIfsDisk=%#p pVDIfsImage=%#p\n", pszFilename, pVDIfsDisk, pVDIfsImage));
int rc = VINF_SUCCESS;
/* Get I/O interface. */
if ( !VALID_PTR(pszFilename)
|| !*pszFilename)
{
goto out;
}
/*
* Open the file and read the footer.
*/
false /* fCreate */),
&pStorage);
if (RT_SUCCESS(rc))
if ( RT_SUCCESS(rc)
{
if ( RT_SUCCESS(rc)
{
*penmType = VDTYPE_HDD;
rc = VINF_SUCCESS;
}
else
}
else
if (pStorage)
out:
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnOpen */
{
LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p ppBackendData=%#p\n", pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, ppBackendData));
int rc;
/* Check open flags. All valid flags are supported. */
if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
{
goto out;
}
/* Check remaining arguments. */
if ( !VALID_PTR(pszFilename)
|| !*pszFilename)
{
goto out;
}
if (!pImage)
{
rc = VERR_NO_MEMORY;
goto out;
}
if (RT_SUCCESS(rc))
*ppBackendData = pImage;
else
out:
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnCreate */
unsigned uImageFlags, const char *pszComment,
unsigned uPercentStart, unsigned uPercentSpan,
{
LogFlowFunc(("pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" pPCHSGeometry=%#p pLCHSGeometry=%#p Uuid=%RTuuid uOpenFlags=%#x uPercentStart=%u uPercentSpan=%u pVDIfsDisk=%#p pVDIfsImage=%#p pVDIfsOperation=%#p ppBackendData=%#p",
pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, ppBackendData));
int rc;
if (pIfProgress)
{
}
/* Check open flags. All valid flags are supported. */
if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
{
goto out;
}
/* Check remaining arguments. */
if ( !VALID_PTR(pszFilename)
|| !*pszFilename
|| !VALID_PTR(pPCHSGeometry)
|| !VALID_PTR(pLCHSGeometry))
{
goto out;
}
if (!pImage)
{
rc = VERR_NO_MEMORY;
goto out;
}
if (RT_SUCCESS(rc))
{
* image is opened in read-only mode if the caller requested that. */
if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
{
qedFreeImage(pImage, false);
if (RT_FAILURE(rc))
{
goto out;
}
}
*ppBackendData = pImage;
}
else
out:
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnRename */
{
int rc = VINF_SUCCESS;
/* Check arguments. */
if ( !pImage
|| !pszFilename
|| !*pszFilename)
{
goto out;
}
/* Close the image. */
if (RT_FAILURE(rc))
goto out;
/* Rename the file. */
if (RT_FAILURE(rc))
{
/* The move failed, try to reopen the original image. */
if (RT_FAILURE(rc2))
goto out;
}
/* Update pImage with the new information. */
/* Open the old image with new name. */
if (RT_FAILURE(rc))
goto out;
out:
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnClose */
{
int rc;
return rc;
}
{
LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToRead=%zu pcbActuallyRead=%#p\n",
uint32_t offCluster = 0;
int rc;
{
goto out;
}
|| cbToRead == 0)
{
goto out;
}
/* Clip read size to remain in the cluster. */
/* Get offset in image. */
if (RT_SUCCESS(rc))
if ( ( RT_SUCCESS(rc)
|| rc == VERR_VD_BLOCK_FREE
|| rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
&& pcbActuallyRead)
out:
return rc;
}
{
LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n",
uint32_t offCluster = 0;
int rc = VINF_SUCCESS;
{
goto out;
}
{
goto out;
}
|| cbToWrite == 0)
{
goto out;
}
/* Convert offset to L1, L2 index and cluster offset. */
/* Clip write size to remain in the cluster. */
/* Get offset in image. */
if (RT_SUCCESS(rc))
else if (rc == VERR_VD_BLOCK_FREE)
{
&& !(fWrite & VD_WRITE_NO_ALLOC))
{
/* Full cluster write to previously unallocated cluster.
* Allocate cluster and write data. */
Assert(!offCluster);
do
{
uint64_t idxUpdateLe = 0;
/* Check if we have to allocate a new cluster for L2 tables. */
{
/* Allocate new async cluster allocation state. */
if (RT_UNLIKELY(!pL2ClusterAlloc))
{
rc = VERR_NO_MEMORY;
break;
}
if (!pL2Entry)
{
rc = VERR_NO_MEMORY;
break;
}
/*
* Write the L2 table first and link to the L1 table afterwards.
* If something unexpected happens the worst case which can happen
* is a leak of some clusters.
*/
if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
break;
else if (RT_FAILURE(rc))
{
break;
}
}
else
{
&pL2Entry);
if (RT_SUCCESS(rc))
{
/* Allocate new async cluster allocation state. */
if (RT_UNLIKELY(!pDataClusterAlloc))
{
rc = VERR_NO_MEMORY;
break;
}
/* Allocate new cluster for the data. */
/* Write data. */
if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
break;
else if (RT_FAILURE(rc))
{
break;
}
}
}
} while (0);
*pcbPreRead = 0;
*pcbPostRead = 0;
}
else
{
/* Trying to do a partial write to an unallocated cluster. Don't do
* anything except letting the upper layer know what to do. */
*pcbPreRead = offCluster;
}
}
if (pcbWriteProcess)
out:
return rc;
}
{
int rc = VINF_SUCCESS;
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetVersion */
static unsigned qedGetVersion(void *pBackendData)
{
if (pImage)
return 1;
else
return 0;
}
/** @copydoc VBOXHDDBACKEND::pfnGetSectorSize */
{
cb = 512;
return cb;
}
/** @copydoc VBOXHDDBACKEND::pfnGetSize */
{
return cb;
}
/** @copydoc VBOXHDDBACKEND::pfnGetFileSize */
{
if (pImage)
{
{
if (RT_SUCCESS(rc))
}
}
return cb;
}
/** @copydoc VBOXHDDBACKEND::pfnGetPCHSGeometry */
static int qedGetPCHSGeometry(void *pBackendData,
{
int rc;
if (pImage)
{
{
rc = VINF_SUCCESS;
}
else
}
else
LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetPCHSGeometry */
static int qedSetPCHSGeometry(void *pBackendData,
{
LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
int rc;
if (pImage)
{
{
goto out;
}
rc = VINF_SUCCESS;
}
else
out:
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetLCHSGeometry */
static int qedGetLCHSGeometry(void *pBackendData,
{
int rc;
if (pImage)
{
{
rc = VINF_SUCCESS;
}
else
}
else
LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetLCHSGeometry */
static int qedSetLCHSGeometry(void *pBackendData,
{
LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
int rc;
if (pImage)
{
{
goto out;
}
rc = VINF_SUCCESS;
}
else
out:
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetImageFlags */
static unsigned qedGetImageFlags(void *pBackendData)
{
unsigned uImageFlags;
if (pImage)
else
uImageFlags = 0;
return uImageFlags;
}
/** @copydoc VBOXHDDBACKEND::pfnGetOpenFlags */
static unsigned qedGetOpenFlags(void *pBackendData)
{
unsigned uOpenFlags;
if (pImage)
else
uOpenFlags = 0;
return uOpenFlags;
}
/** @copydoc VBOXHDDBACKEND::pfnSetOpenFlags */
{
int rc;
/* Image must be opened and the new flags must be valid. */
{
goto out;
}
/* Implement this operation via reopening the image. */
if (RT_FAILURE(rc))
goto out;
out:
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetComment */
{
LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
int rc;
if (pImage)
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetComment */
{
int rc;
if (pImage)
{
else
}
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetUuid */
{
int rc;
if (pImage)
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetUuid */
{
int rc;
if (pImage)
{
else
}
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetModificationUuid */
{
int rc;
if (pImage)
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetModificationUuid */
{
int rc;
if (pImage)
{
else
}
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetParentUuid */
{
int rc;
if (pImage)
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetParentUuid */
{
int rc;
if (pImage)
{
else
}
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetParentModificationUuid */
{
int rc;
if (pImage)
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetParentModificationUuid */
{
int rc;
if (pImage)
{
else
}
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnDump */
static void qedDump(void *pBackendData)
{
if (pImage)
{
}
}
/** @copydoc VBOXHDDBACKEND::pfnGetParentFilename */
{
int rc = VINF_SUCCESS;
if (pImage)
if (pImage->pszBackingFilename)
else
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetParentFilename */
{
int rc = VINF_SUCCESS;
if (pImage)
{
else if ( pImage->pszBackingFilename
else
{
if (pImage->pszBackingFilename)
if (!pImage->pszBackingFilename)
rc = VERR_NO_MEMORY;
else
{
if (!pImage->offBackingFilename)
{
/* Allocate new cluster. */
}
if (RT_SUCCESS(rc))
}
}
}
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnResize */
unsigned uPercentStart, unsigned uPercentSpan,
{
int rc = VINF_SUCCESS;
/* Making the image smaller is not supported at the moment. */
{
/*
* It is enough to just update the size field in the header to complete
* growing. With the default cluster and table sizes the image can be expanded
* to 64TB without overflowing the L1 and L2 tables making block relocation
* superfluous.
* @todo: The rare case where block relocation is still required (non default
* implemented yet and resizing such an image will fail with an error.
*/
if (qedByte2Cluster(pImage, pImage->cbTable)*pImage->cTableEntries*pImage->cTableEntries*pImage->cbCluster < cbSize)
N_("Qed: Resizing the image '%s' is not supported because it would overflow the L1 and L2 table\n"),
else
{
if (RT_FAILURE(rc))
{
}
}
}
/* Same size doesn't change the image at all. */
return rc;
}
{
/* pszBackendName */
"QED",
/* cbSize */
sizeof(VBOXHDDBACKEND),
/* uBackendCaps */
/* paFileExtensions */
/* paConfigInfo */
NULL,
/* hPlugin */
/* pfnCheckIfValid */
/* pfnOpen */
/* pfnCreate */
/* pfnRename */
/* pfnClose */
/* pfnRead */
/* pfnWrite */
/* pfnFlush */
/* pfnDiscard */
NULL,
/* pfnGetVersion */
/* pfnGetSectorSize */
/* pfnGetSize */
/* pfnGetFileSize */
/* pfnGetPCHSGeometry */
/* pfnSetPCHSGeometry */
/* pfnGetLCHSGeometry */
/* pfnSetLCHSGeometry */
/* pfnGetImageFlags */
/* pfnGetOpenFlags */
/* pfnSetOpenFlags */
/* pfnGetComment */
/* pfnSetComment */
/* pfnGetUuid */
/* pfnSetUuid */
/* pfnGetModificationUuid */
/* pfnSetModificationUuid */
/* pfnGetParentUuid */
/* pfnSetParentUuid */
/* pfnGetParentModificationUuid */
/* pfnSetParentModificationUuid */
/* pfnDump */
/* pfnGetTimeStamp */
NULL,
/* pfnGetParentTimeStamp */
NULL,
/* pfnSetParentTimeStamp */
NULL,
/* pfnGetParentFilename */
/* pfnSetParentFilename */
/* pfnComposeLocation */
/* pfnComposeName */
/* pfnCompact */
NULL,
/* pfnResize */
/* pfnRepair */
};