QCOW.cpp revision 8bc5d8f11ce2dc440fd0b56e290ad9643280722f
/* $Id$ */
/** @file
* QCOW - QCOW Disk image.
*/
/*
* Copyright (C) 2011 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 *
*******************************************************************************/
#include <VBox/vd-plugin.h>
/**
* The QCOW backend implements support for the qemu copy on write format (short QCOW)
* There is no official specification available but the format is described
* at http://people.gnome.org/~markmc/qcow-image-format.html for version 2
* and http://people.gnome.org/~markmc/qcow-image-format-version-1.html for version 1.
*
* Missing things to implement:
* - v2 image creation and handling of the reference count table. (Blocker to enable support for V2 images)
* - cluster encryption
* - cluster compression
* - compaction
* - resizing
*/
/*******************************************************************************
* Structures in a QCOW image, big endian *
*******************************************************************************/
#pragma pack(1)
typedef struct QCowHeader
{
/** Magic value. */
/** Version of the image. */
/** Version dependent data. */
union
{
/** Version 1. */
struct
{
/** Backing file offset. */
/** Size of the backing file. */
/** mtime (Modification time?) - can be ignored. */
/** Logical size of the image in bytes. */
/** Number of bits in the virtual offset used as a cluster offset. */
/** Number of bits in the virtual offset used for the L2 index. */
/** Padding because the header is not packed in the original source. */
/** Used cryptographic method. */
/** Offset of the L1 table in the image in bytes. */
} v1;
/** Version 2. */
struct
{
/** Backing file offset. */
/** Size of the backing file. */
/** Number of bits in the virtual offset used as a cluster offset. */
/** Logical size of the image. */
/** Used cryptographic method. */
/** Size of the L1 table in entries (each 8bytes big). */
/** Offset of the L1 table in the image in bytes. */
/** Start of the refcount table in the image. */
/** Size of the refcount table in clusters. */
/** Number of snapshots in the image. */
/** Offset of the first snapshot header in the image. */
} v2;
} Version;
} QCowHeader;
#pragma pack()
/** Pointer to a on disk QCOW header. */
typedef QCowHeader *PQCowHeader;
/** QCOW magic value. */
/** Size of the V1 header. */
#define QCOW_V1_HDR_SIZE (48)
/** Size of the V2 header. */
#define QCOW_V2_HDR_SIZE (72)
/*******************************************************************************
* Constants And Macros, Structures and Typedefs *
*******************************************************************************/
/**
* QCOW L2 cache entry.
*/
typedef struct QCOWL2CACHEENTRY
{
/** 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. */
/** QCOW default cluster size for image version 2. */
/** QCOW default cluster size for image version 1. */
/** QCOW default L2 table size in clusters. */
#define QCOW_L2_CLUSTERS_DEFAULT (1)
/**
* QCOW image data structure.
*/
typedef struct QCOWIMAGE
{
/** 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. */
/** Image version. */
unsigned uVersion;
/** MTime field - used only to preserve value in opened images, unmodified otherwise. */
/** 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 table. */
/** Size of an L1 rounded to the next cluster size. */
/** Pointer to the L1 table. */
/** Offset of the L1 table. */
/** Size of the L2 table in bytes. */
/** Number of entries in the L2 table. */
/** Memory occupied by the L2 table cache. */
/** The sorted L2 entry list used for searching. */
/** The LRU L2 entry list used for eviction. */
/** Offset of the refcount table. */
/** Size of the refcount table in bytes. */
/** Offset mask for a cluster. */
/** 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. */
} QCOWIMAGE, *PQCOWIMAGE;
/**
* State of the async cluster allocation.
*/
typedef enum QCOWCLUSTERASYNCALLOCSTATE
{
/** Invalid. */
/** L2 table allocation. */
/** Link L2 table into L1. */
/** Allocate user data cluster. */
/** Link user data cluster. */
/** 32bit blowup. */
QCOWCLUSTERASYNCALLOCSTATE_32BIT_HACK = 0x7fffffff
/**
* Data needed to track async cluster allocation.
*/
typedef struct QCOWCLUSTERASYNCALLOC
{
/** 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_aQCowFileExtensions[] =
{
{"qcow", VDTYPE_HDD},
{"qcow2", VDTYPE_HDD},
};
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/**
* 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++;
}
}
/**
* 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;
{
}
{
pHeader->Version.v2.u64RefcountTableOffset = RT_BE2H_U64(pHeader->Version.v2.u64RefcountTableOffset);
pHeader->Version.v2.u32RefcountTableClusters = RT_BE2H_U32(pHeader->Version.v2.u32RefcountTableClusters);
}
else
return false;
return true;
}
/**
* Creates a QCOW header from the given image state.
*
* @returns nothing.
* @param pImage Image instance data.
* @param pHeader Pointer to the header to convert.
* @param pcbHeader Where to store the size of the header to write.
*/
{
{
}
{
pHeader->Version.v2.u32RefcountTableClusters = RT_H2BE_U32(pImage->cbRefcountTable / pImage->cbCluster);
}
else
}
/**
* 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_LITTLE_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_LITTLE_ENDIAN)
#endif
}
else
{
}
}
else
rc = VERR_NO_MEMORY;
}
if (RT_SUCCESS(rc))
return rc;
}
/**
* 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 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;
LogFlowFunc(("pImage=%#p idxL1=%u idxL2=%u offCluster=%u poffImage=%#p\n",
{
if (RT_SUCCESS(rc))
{
/* Get real file offset. */
else
}
}
return rc;
}
/**
* Returns the real image offset for a given cluster or an error if the cluster is not
* yet allocated- version for async I/O.
*
* @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_LITTLE_ENDIAN)
if (paL1TblImg)
{
}
else
rc = VERR_NO_MEMORY;
#else
/* Write L1 table directly. */
#endif
if (RT_SUCCESS(rc))
{
/* Write 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_LITTLE_ENDIAN)
if (paL1TblImg)
{
}
else
rc = VERR_NO_MEMORY;
#else
/* Write L1 table directly. */
#endif
{
/* Write header. */
}
}
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)
{
{
{
}
else
N_("QCow: Encrypted image '%s' is not supported"),
}
{
N_("QCow: Encrypted image '%s' is not supported"),
N_("QCow: Image '%s' contains snapshots which is not supported"),
else
{
N_("QCow: Image '%s' uses version 2 which is not supported"),
#if 0 /** @todo: Add support for the reference count table. */
#endif
}
}
else
if ( RT_SUCCESS(rc)
&& pImage->offBackingFilename)
{
/* Load backing filename from image. */
pImage->pszFilename = (char *)RTMemAllocZ(pImage->cbBackingFilename + 1); /* +1 for \0 terminator. */
if (pImage->pszFilename)
{
}
else
rc = VERR_NO_MEMORY;
}
if ( RT_SUCCESS(rc)
&& pImage->offRefcountTable)
{
/* Load refcount table. */
AssertMsgFailed(("TODO\n"));
}
if (RT_SUCCESS(rc))
{
/* Allocate L1 table. */
{
/* Read from the image. */
if (RT_SUCCESS(rc))
{
if (RT_FAILURE(rc))
N_("QCow: Creating the L2 table cache for image '%s' failed"),
}
else
N_("QCow: Reading the L1 table for image '%s' failed"),
}
else
N_("QCow: Out of memory allocating L1 table for image '%s'"),
}
}
else if (RT_SUCCESS(rc))
}
else
out:
if (RT_FAILURE(rc))
qcowFreeImage(pImage, false);
return rc;
}
/**
* Internal: Create a qcow 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_("QCow: 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_("QCow: 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_("QCow: cannot allocate memory for L1 table of image '%s'"),
goto out;
}
if (RT_FAILURE(rc))
{
rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("QCow: Failed to create L2 cache for image '%s'"),
goto out;
}
if (RT_SUCCESS(rc))
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 qcowAsyncClusterAllocRollback(PQCOWIMAGE pImage, PVDIOCTX pIoCtx, PQCOWCLUSTERASYNCALLOC pClusterAlloc)
{
int rc = VINF_SUCCESS;
switch (pClusterAlloc->enmAllocState)
{
{
/* Assumption right now is that the L1 table is not modified if the link fails. */
qcowL2TblCacheEntryFree(pImage, pClusterAlloc->pL2Entry); /* Free it, it is not in the cache yet. */
}
{
/* Assumption right now is that the L2 table is not modified if the link fails. */
}
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) qcowAsyncClusterAllocUpdate(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)
&& cbFile > sizeof(QCowHeader))
{
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)
{
qcowFreeImage(pImage, false);
if (RT_FAILURE(rc))
{
goto out;
}
}
*ppBackendData = pImage;
}
else
out:
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnRename */
{
int rc = VINF_SUCCESS;
/* Check arguments. */
if ( !pImage
|| !pszFilename
|| !*pszFilename)
{
goto out;
}
/* Close the image. */
if (RT_FAILURE(rc))
goto out;
/* Rename the file. */
if (RT_FAILURE(rc))
{
/* The move failed, try to reopen the original image. */
if (RT_FAILURE(rc2))
goto out;
}
/* Update pImage with the new information. */
/* Open the old image with new name. */
if (RT_FAILURE(rc))
goto out;
out:
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnClose */
{
int rc;
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnRead */
{
LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbToRead=%zu pcbActuallyRead=%#p\n",
uint32_t offCluster = 0;
int rc;
|| cbToRead == 0)
{
goto out;
}
/* Clip read size to remain in the cluster. */
/* Get offset in image. */
if (RT_SUCCESS(rc))
{
}
&& pcbActuallyRead)
out:
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnWrite */
{
LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n",
uint32_t offCluster = 0;
int rc;
{
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. */
{
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 (RT_FAILURE(rc))
break;
/* Write the L1 link now. */
if (RT_FAILURE(rc))
break;
}
else
if (RT_SUCCESS(rc))
{
/* Allocate new cluster for the data. */
/* Write data. */
if (RT_FAILURE(rc))
break;
/* Link L2 table and update it. */
}
} 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;
}
/** @copydoc VBOXHDDBACKEND::pfnFlush */
static int qcowFlush(void *pBackendData)
{
int rc;
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetVersion */
static unsigned qcowGetVersion(void *pBackendData)
{
if (pImage)
else
return 0;
}
/** @copydoc VBOXHDDBACKEND::pfnGetSize */
{
return cb;
}
/** @copydoc VBOXHDDBACKEND::pfnGetFileSize */
{
if (pImage)
{
{
if (RT_SUCCESS(rc))
}
}
return cb;
}
/** @copydoc VBOXHDDBACKEND::pfnGetPCHSGeometry */
static int qcowGetPCHSGeometry(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 qcowSetPCHSGeometry(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 qcowGetLCHSGeometry(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 qcowSetLCHSGeometry(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 qcowGetImageFlags(void *pBackendData)
{
unsigned uImageFlags;
if (pImage)
else
uImageFlags = 0;
return uImageFlags;
}
/** @copydoc VBOXHDDBACKEND::pfnGetOpenFlags */
static unsigned qcowGetOpenFlags(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. */
if (!pImage || (uOpenFlags & ~(VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_ASYNC_IO)))
{
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 qcowDump(void *pBackendData)
{
if (pImage)
{
}
}
/** @copydoc VBOXHDDBACKEND::pfnGetParentFilename */
{
int rc = VINF_SUCCESS;
if (pImage)
if (pImage->pszFilename)
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))
NULL);
}
}
}
else
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. */
&offFile);
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. */
&offImage);
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;
}
{
/* pszBackendName */
"QCOW",
/* cbSize */
sizeof(VBOXHDDBACKEND),
/* uBackendCaps */
/* paFileExtensions */
/* paConfigInfo */
NULL,
/* hPlugin */
/* pfnCheckIfValid */
/* pfnOpen */
/* pfnCreate */
/* pfnRename */
/* pfnClose */
/* pfnRead */
/* pfnWrite */
/* pfnFlush */
/* pfnGetVersion */
/* pfnGetSize */
/* pfnGetFileSize */
/* pfnGetPCHSGeometry */
/* pfnSetPCHSGeometry */
/* pfnGetLCHSGeometry */
/* pfnSetLCHSGeometry */
/* pfnGetImageFlags */
/* pfnGetOpenFlags */
/* pfnSetOpenFlags */
/* pfnGetComment */
/* pfnSetComment */
/* pfnGetUuid */
/* pfnSetUuid */
/* pfnGetModificationUuid */
/* pfnSetModificationUuid */
/* pfnGetParentUuid */
/* pfnSetParentUuid */
/* pfnGetParentModificationUuid */
/* pfnSetParentModificationUuid */
/* pfnDump */
/* pfnGetTimeStamp */
NULL,
/* pfnGetParentTimeStamp */
NULL,
/* pfnSetParentTimeStamp */
NULL,
/* pfnGetParentFilename */
/* pfnSetParentFilename */
/* pfnAsyncRead */
/* pfnAsyncWrite */
/* pfnAsyncFlush */
/* pfnComposeLocation */
/* pfnComposeName */
/* pfnCompact */
NULL,
/* pfnResize */
};