VD.cpp revision 167dd9573ca1cd5035a092df89e71701b92bdd7e
/* $Id$ */
/** @file
* VBoxHDD - VBox HDD Container implementation.
*/
/*
* Copyright (C) 2006-2010 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_VD
#define RT_STRICT
#define LOG_ENABLED
#include <iprt/memcache.h>
#include <iprt/critsect.h>
#include <VBox/vd-plugin.h>
#include <VBox/vd-cache-plugin.h>
#define VBOXHDDDISK_SIGNATURE 0x6f0e2a7d
/** Buffer size used for merging images. */
/** Maximum number of segments in one I/O task. */
#define VD_IO_TASK_SEGMENTS_MAX 64
/**
* VD async I/O interface storage descriptor.
*/
typedef struct VDIIOFALLBACKSTORAGE
{
/** File handle. */
/** Completion callback. */
/** Thread for async access. */
/**
* Structure containing everything I/O related
* for the image and cache descriptors.
*/
typedef struct VDIO
{
/** I/O interface to the upper layer. */
/** I/O interface callback table. */
/** Per image internal I/O interface. */
/** Fallback I/O interface, only used if the caller doesn't provide it. */
/** Opaque backend data. */
void *pBackendData;
/** Disk this image is part of */
/**
* VBox HDD Container image descriptor.
*/
typedef struct VDIMAGE
{
/** Link to parent image descriptor, if any. */
/** Link to child image descriptor, if any. */
/** Container base filename. (UTF-8) */
char *pszFilename;
/** Data managed by the backend which keeps the actual info. */
void *pBackendData;
/** Cached sanitized image flags. */
unsigned uImageFlags;
/** Image open flags (only those handled generically in this code and which
* the backends will never ever see). */
unsigned uOpenFlags;
/** Function pointers for the various backend methods. */
/** Pointer to list of VD interfaces, per-image. */
/** I/O related things. */
/**
* uModified bit flags.
*/
#define VD_IMAGE_MODIFIED_FLAG RT_BIT(0)
/**
* VBox HDD Cache image descriptor.
*/
typedef struct VDCACHE
{
/** Cache base filename. (UTF-8) */
char *pszFilename;
/** Data managed by the backend which keeps the actual info. */
void *pBackendData;
/** Cached sanitized image flags. */
unsigned uImageFlags;
/** Image open flags (only those handled generically in this code and which
* the backends will never ever see). */
unsigned uOpenFlags;
/** Function pointers for the various backend methods. */
/** Pointer to list of VD interfaces, per-cache. */
/** I/O related things. */
/**
* VBox HDD Container main structure, private part.
*/
struct VBOXHDD
{
/** Structure signature (VBOXHDDDISK_SIGNATURE). */
/** Image type. */
/** Number of opened images. */
unsigned cImages;
/** Base image. */
/** Last opened image in the chain.
* The same as pBase if only one image is used. */
/** If a merge to one of the parents is running this may be non-NULL
* to indicate to what image the writes should be additionally relayed. */
/** Flags representing the modification state. */
unsigned uModified;
/** Cached size of this disk. */
/** Cached PCHS geometry for this disk. */
/** Cached LCHS geometry for this disk. */
/** Pointer to list of VD interfaces, per-disk. */
/** Pointer to the common interface structure for error reporting. */
/** Pointer to the error interface callbacks we use if available. */
/** Pointer to the optional thread synchronization interface. */
/** Pointer to the optional thread synchronization callbacks. */
/** Internal I/O interface callback table for the images. */
/** Callback table for the fallback I/O interface. */
/** Memory cache for I/O contexts */
/** Memory cache for I/O tasks. */
/** Critical section protecting the disk against concurrent access. */
/** Flag whether the disk is currently locked by growing write or a flush
* request. Other flush or growing write requests need to wait until
* the current one completes.
*/
volatile bool fLocked;
/** List of waiting requests. - Protected by the critical section. */
/** I/O context which locked the disk. */
/** Pointer to the L2 disk cache if any. */
};
# define VD_THREAD_IS_CRITSECT_OWNER(Disk) \
do \
{ \
("Thread does not own critical section\n"));\
} while(0)
/**
* VBox parent read descriptor, used internally for compaction.
*/
typedef struct VDPARENTSTATEDESC
{
/** Pointer to disk descriptor. */
/** Pointer to image descriptor. */
/**
* Transfer direction.
*/
typedef enum VDIOCTXTXDIR
{
/** Read */
VDIOCTXTXDIR_READ = 0,
/** Write */
/** Flush */
/** 32bit hack */
VDIOCTXTXDIR_32BIT_HACK = 0x7fffffff
/** Transfer function */
/** Pointer to a transfer function. */
typedef FNVDIOCTXTRANSFER *PFNVDIOCTXTRANSFER;
/**
* I/O context
*/
typedef struct VDIOCTX
{
/** Disk this is request is for. */
/** Return code. */
int rcReq;
/** Transfer direction */
/** Number of bytes left until this context completes. */
volatile uint32_t cbTransferLeft;
/** Current offset */
/** Number of bytes to transfer */
volatile size_t cbTransfer;
/** Current image in the chain. */
/** Start image to read from. pImageCur is reset to this
* value after it reached the first image in the chain. */
/** S/G buffer */
/** Flag whether the I/O context is blocked because it is in the growing list. */
bool fBlocked;
/** Number of data transfers currently pending. */
volatile uint32_t cDataTransfersPending;
/** How many meta data transfers are pending. */
volatile uint32_t cMetaTransfersPending;
/** Flag whether the request finished */
volatile bool fComplete;
/** Temporary allocated memory which is freed
* when the context completes. */
void *pvAllocation;
/** Transfer function. */
/** Next transfer part after the current one completed. */
union
{
/** Root data */
struct
{
/** Completion callback */
/** User argument 1 passed on completion. */
void *pvUser1;
/** User argument 1 passed on completion. */
void *pvUser2;
} Root;
/** Child data */
struct
{
/** Saved start offset */
/** Saved transfer size */
/** Number of bytes transferred from the parent if this context completes. */
/** Number of bytes to pre read */
/** Number of bytes to post read. */
/** Number of bytes to write left in the parent. */
/** Write type dependent data. */
union
{
/** Optimized */
struct
{
/** Bytes to fill to satisfy the block size. Not part of the virtual disk. */
/** Bytes to copy instead of reading from the parent */
/** Bytes to read from the image. */
} Optimized;
} Write;
} Child;
} Type;
} VDIOCTX;
typedef struct VDIOCTXDEFERRED
{
/** Node in the list of deferred requests.
* A request can be deferred if the image is growing
* and the request accesses the same range or if
* the backend needs to read or write metadata from the disk
* before it can continue. */
/** I/O context this entry points to. */
/**
* I/O task.
*/
typedef struct VDIOTASK
{
/** Storage this task belongs to. */
/** Optional completion callback. */
/** Opaque user data. */
void *pvUser;
/** Flag whether this is a meta data transfer. */
bool fMeta;
/** Type dependent data. */
union
{
/** User data transfer. */
struct
{
/** Number of bytes this task transferred. */
/** Pointer to the I/O context the task belongs. */
} User;
/** Meta data transfer. */
struct
{
/** Meta transfer this task is for. */
} Meta;
} Type;
/**
* Storage handle.
*/
typedef struct VDIOSTORAGE
{
/** Image I/O state this storage handle belongs to. */
/** AVL tree for pending async metadata transfers. */
/** Storage handle */
void *pStorage;
} VDIOSTORAGE;
/**
* Metadata transfer.
*
* @note This entry can't be freed if either the list is not empty or
* the reference counter is not 0.
* The assumption is that the backends don't need to read huge amounts of
* metadata to complete a transfer so the additional memory overhead should
* be relatively small.
*/
typedef struct VDMETAXFER
{
/** AVL core for fast search (the file offset is the key) */
/** I/O storage for this transfer. */
/** Flags. */
/** List of I/O contexts waiting for this metadata transfer to complete. */
/** Number of references to this entry. */
unsigned cRefs;
/** Size of the data stored with this entry. */
/** Data stored - variable size. */
} VDMETAXFER;
/**
* The transfer direction for the metadata.
*/
#define VDMETAXFER_TXDIR_MASK 0x3
#define VDMETAXFER_TXDIR_NONE 0x0
#define VDMETAXFER_TXDIR_WRITE 0x1
#define VDMETAXFER_TXDIR_READ 0x2
#define VDMETAXFER_TXDIR_FLUSH 0x3
extern VBOXHDDBACKEND g_RawBackend;
extern VBOXHDDBACKEND g_VmdkBackend;
extern VBOXHDDBACKEND g_VDIBackend;
extern VBOXHDDBACKEND g_VhdBackend;
extern VBOXHDDBACKEND g_ParallelsBackend;
extern VBOXHDDBACKEND g_DmgBackend;
extern VBOXHDDBACKEND g_ISCSIBackend;
static unsigned g_cBackends = 0;
static PVBOXHDDBACKEND aStaticBackends[] =
{
};
/**
* Supported backends for the disk cache.
*/
extern VDCACHEBACKEND g_VciCacheBackend;
static unsigned g_cCacheBackends = 0;
static PVDCACHEBACKEND aStaticCacheBackends[] =
{
};
/**
* internal: add several backends.
*/
{
if (RT_UNLIKELY(!pTmp))
return VERR_NO_MEMORY;
g_apBackends = pTmp;
g_cBackends += cBackends;
return VINF_SUCCESS;
}
/**
* internal: add single backend.
*/
{
}
/**
* internal: add several cache backends.
*/
{
if (RT_UNLIKELY(!pTmp))
return VERR_NO_MEMORY;
return VINF_SUCCESS;
}
/**
* internal: add single cache backend.
*/
{
}
/**
* internal: issue error message.
*/
const char *pszFormat, ...)
{
pDisk->pInterfaceErrorCallbacks->pfnError(pDisk->pInterfaceError->pvUser, rc, RT_SRC_POS_ARGS, pszFormat, va);
return rc;
}
/**
* internal: thread synchronization, start read.
*/
{
int rc = VINF_SUCCESS;
return rc;
}
/**
* internal: thread synchronization, finish read.
*/
{
int rc = VINF_SUCCESS;
return rc;
}
/**
* internal: thread synchronization, start write.
*/
{
int rc = VINF_SUCCESS;
return rc;
}
/**
* internal: thread synchronization, finish write.
*/
{
int rc = VINF_SUCCESS;
return rc;
}
/**
* internal: find image format backend.
*/
{
int rc = VINF_SUCCESS;
if (!g_apBackends)
VDInit();
for (unsigned i = 0; i < g_cBackends; i++)
{
{
pBackend = g_apBackends[i];
break;
}
}
return rc;
}
/**
* internal: find cache format backend.
*/
{
int rc = VINF_SUCCESS;
if (!g_apCacheBackends)
VDInit();
for (unsigned i = 0; i < g_cCacheBackends; i++)
{
{
pBackend = g_apCacheBackends[i];
break;
}
}
return rc;
}
/**
* internal: add image structure to the end of images list.
*/
{
{
}
else
{
}
}
/**
* internal: remove image structure from the images list.
*/
{
else
else
}
/**
* internal: find image by index into the images list.
*/
{
if (nImage == VD_LAST_IMAGE)
{
nImage--;
}
return pImage;
}
/**
* Internal: Tries to read the desired range from the given cache.
*
* @returns VBox status code.
* @retval VERR_VD_BLOCK_FREE if the block is not in the cache.
* pcbRead will be set to the number of bytes not in the cache.
* Everything thereafter might be in the cache.
* @param pCache The cache to read from.
* @param uOffset Offset of the virtual disk to read.
* @param pvBuf Where to store the read data.
* @param cbRead How much to read.
* @param pcbRead Where to store the number of bytes actually read.
* On success this indicates the number of bytes read from the cache.
* If VERR_VD_BLOCK_FREE is returned this gives the number of bytes
* which are not in the cache.
* In both cases everything beyond this value
* might or might not be in the cache.
*/
{
int rc = VINF_SUCCESS;
LogFlowFunc(("pCache=%#p uOffset=%llu pvBuf=%#p cbRead=%zu pcbRead=%#p\n",
return rc;
}
/**
* Internal: Writes data for the given block into the cache.
*
* @returns VBox status code.
* @param pCache The cache to write to.
* @param uOffset Offset of the virtual disk to write to teh cache.
* @param pcvBuf The data to write.
* @param cbWrite How much to write.
* @param pcbWritten How much data could be written, optional.
*/
{
int rc = VINF_SUCCESS;
LogFlowFunc(("pCache=%#p uOffset=%llu pvBuf=%#p cbWrite=%zu pcbWritten=%#p\n",
if (pcbWritten)
else
{
do
{
} while ( cbWrite
&& RT_SUCCESS(rc));
}
LogFlowFunc(("returns rc=%Rrc pcbWritten=%zu\n",
return rc;
}
/**
* Internal: Reads a given amount of data from the image chain of the disk.
**/
{
int rc = VINF_SUCCESS;
*pcbThisRead = 0;
/*
* Try to read from the given image.
* If the block is not allocated read from override chain if present.
*/
&cbThisRead);
if (rc == VERR_VD_BLOCK_FREE)
{
{
&cbThisRead);
}
}
return rc;
}
/**
* internal: read the specified amount of data in whatever blocks the backend
* will give us.
*/
bool fZeroFreeBlocks, bool fUpdateCache)
{
int rc = VINF_SUCCESS;
bool fAllFree = true;
size_t cbBufClear = 0;
/* Loop until all read. */
do
{
/* Search for image with allocated block. Do not attempt to read more
* than the previous reads marked as valid. Otherwise this would return
* stale data when different block sizes are used for the images. */
cbThisRead = cbRead;
&& !pImageParentOverride)
{
cbThisRead, &cbThisRead);
if (rc == VERR_VD_BLOCK_FREE)
{
/* If the read was successful, write the data back into the cache. */
if ( RT_SUCCESS(rc)
&& fUpdateCache)
{
cbThisRead, NULL);
}
}
}
else
{
/** @todo can be be replaced by vdDiskReadHelper if it proves to be reliable,
* don't want to be responsible for data corruption...
*/
/*
* Try to read from the given image.
* If the block is not allocated read from override chain if present.
*/
&cbThisRead);
if (rc == VERR_VD_BLOCK_FREE)
{
{
&cbThisRead);
}
}
}
/* No image in the chain contains the data for the block. */
if (rc == VERR_VD_BLOCK_FREE)
{
/* Fill the free space with 0 if we are told to do so
* or a previous read returned valid data. */
if (fZeroFreeBlocks || !fAllFree)
else
cbBufClear += cbThisRead;
rc = VINF_SUCCESS;
}
else if (RT_SUCCESS(rc))
{
/* First not free block, fill the space before with 0. */
if (!fZeroFreeBlocks)
{
cbBufClear = 0;
fAllFree = false;
}
}
cbRead -= cbThisRead;
uOffset += cbThisRead;
}
{
{
pIoCtx->cDataTransfersPending = 0;
pIoCtx->cMetaTransfersPending = 0;
/* There is no S/G list for a flush request. */
if (enmTxDir != VDIOCTXTXDIR_FLUSH)
else
}
return pIoCtx;
}
void *pvAllocation,
{
{
}
return pIoCtx;
}
{
{
}
return pIoCtx;
}
DECLINLINE(PVDIOTASK) vdIoTaskUserAlloc(PVDIOSTORAGE pIoStorage, PFNVDXFERCOMPLETED pfnComplete, void *pvUser, PVDIOCTX pIoCtx, uint32_t cbTransfer)
{
if (pIoTask)
{
}
return pIoTask;
}
DECLINLINE(PVDIOTASK) vdIoTaskMetaAlloc(PVDIOSTORAGE pIoStorage, PFNVDXFERCOMPLETED pfnComplete, void *pvUser, PVDMETAXFER pMetaXfer)
{
if (pIoTask)
{
}
return pIoTask;
}
{
if (pIoCtx->pvAllocation)
#ifdef DEBUG
#endif
}
{
}
{
}
{
{
}
return pMetaXfer;
}
{
if (!pDeferred)
return VERR_NO_MEMORY;
return VINF_SUCCESS;
}
{
}
{
}
{
}
{
}
{
}
{
int rc = VINF_SUCCESS;
if ( !pIoCtx->cbTransferLeft
&& !pIoCtx->pfnIoCtxTransfer)
{
goto out;
}
/*
* We complete the I/O context in case of an error
* if there is no I/O task pending.
*/
&& !pIoCtx->cDataTransfersPending)
{
goto out;
}
/* Don't change anything if there is a metadata transfer pending or we are blocked. */
if ( pIoCtx->cMetaTransfersPending
{
goto out;
}
if (pIoCtx->pfnIoCtxTransfer)
{
/* Call the transfer function advancing to the next while there is no error. */
while ( pIoCtx->pfnIoCtxTransfer
&& RT_SUCCESS(rc))
{
/* Advance to the next part of the transfer if the current one succeeded. */
if (RT_SUCCESS(rc))
{
}
}
}
if ( RT_SUCCESS(rc)
&& !pIoCtx->cbTransferLeft
&& !pIoCtx->cDataTransfersPending)
else if ( RT_SUCCESS(rc)
|| rc == VERR_VD_IOCTX_HALT)
{
/*
* The I/O context completed if we have an error and there is no data
* or meta data transfer pending.
*/
if ( !pIoCtx->cMetaTransfersPending
&& !pIoCtx->cDataTransfersPending)
else
}
out:
LogFlowFunc(("pIoCtx=%#p rc=%Rrc cbTransferLeft=%u cMetaTransfersPending=%u fComplete=%RTbool\n",
return rc;
}
{
}
{
int rc = VINF_SUCCESS;
{
if (RT_SUCCESS(rc))
}
else
{
}
return rc;
}
{
LogFlowFunc(("pDisk=%#p pIoCtx=%#p fProcessDeferredReqs=%RTbool\n",
if (fProcessDeferredReqs)
{
/* Process any pending writes if the current request didn't caused another growing. */
{
/* Process the list. */
do
{
int rc;
pIoCtxWait->fBlocked = false;
if ( rc == VINF_VD_ASYNC_IO_FINISHED
{
pIoCtxWait->rcReq);
}
} while (!RTListIsEmpty(&ListTmp));
}
else
}
LogFlowFunc(("returns\n"));
}
/**
* internal: read the specified amount of data in whatever blocks the backend
* will give us - async version.
*/
{
int rc;
/* Loop until all reads started or we have a backend which needs to read metadata. */
do
{
/* Search for image with allocated block. Do not attempt to read more
* than the previous reads marked as valid. Otherwise this would return
* stale data when different block sizes are used for the images. */
/*
* Try to read from the given image.
* If the block is not allocated read from override chain if present.
*/
pIoCtx, &cbThisRead);
if (rc == VERR_VD_BLOCK_FREE)
{
&& rc == VERR_VD_BLOCK_FREE)
{
pIoCtx, &cbThisRead);
}
}
/* The task state will be updated on success already, don't do it here!. */
if (rc == VERR_VD_BLOCK_FREE)
{
/* No image in the chain contains the data for the block. */
rc = VINF_SUCCESS;
}
else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
rc = VINF_SUCCESS;
else if (rc == VERR_VD_IOCTX_HALT)
{
uOffset += cbThisRead;
cbToRead -= cbThisRead;
}
if (RT_FAILURE(rc))
break;
cbToRead -= cbThisRead;
uOffset += cbThisRead;
if ( rc == VERR_VD_NOT_ENOUGH_METADATA
|| rc == VERR_VD_IOCTX_HALT)
{
/* Save the current state. */
}
return rc;
}
/**
* internal: parent image read wrapper for compacting.
*/
{
false /* fUpdateCache */);
}
/**
* internal: mark the disk as not modified.
*/
{
{
/* generate new last-modified uuid */
{
RTUuidCreate(&Uuid);
&Uuid);
&Uuid);
}
}
}
/**
* internal: mark the disk as modified.
*/
{
{
/* First modify, so create a UUID and ensure it's written to disk. */
}
}
/**
* internal: write a complete block (only used for diff images), taking the
* remaining data from parent images. This implementation does not optimize
* anything (except that it tries to read only that portions from parent
* images that are really needed).
*/
void *pvTmp)
{
int rc = VINF_SUCCESS;
/* Read the data that goes before the write to fill the block. */
if (cbPreRead)
{
/*
* Updating the cache doesn't make sense here because
* this will be done after the complete block was written.
*/
true /* fZeroFreeBlocks*/,
false /* fUpdateCache */);
if (RT_FAILURE(rc))
return rc;
}
/* Copy the data to the right place in the buffer. */
/* Read the data that goes after the write to fill the block. */
if (cbPostRead)
{
/* If we have data to be written, use that instead of reading
* data from the image. */
if (cbWrite > cbThisWrite)
else
cbWriteCopy = 0;
/* Figure out how much we cannot read from the image, because
* the last block to write might exceed the nominal size of the
* image for technical reasons. */
else
cbFill = 0;
/* The rest must be read from the image. */
/* Now assemble the remaining data. */
if (cbWriteCopy)
if (cbReadImage)
cbReadImage, true /* fZeroFreeBlocks */,
false /* fUpdateCache */);
if (RT_FAILURE(rc))
return rc;
/* Zero out the remainder of this block. Will never be visible, as this
* is beyond the limit of the image. */
if (cbFill)
'\0', cbFill);
}
/* Write the full block to the virtual disk. */
Assert(cbPostRead == 0);
return rc;
}
/**
* internal: write a complete block (only used for diff images), taking the
* remaining data from parent images. This implementation optimizes out writes
* that do not change the data relative to the state as of the parent images.
* All backends which support differential/growing images support this.
*/
void *pvTmp)
{
size_t cbWriteCopy = 0;
size_t cbReadImage = 0;
int rc;
if (cbPostRead)
{
/* Figure out how much we cannot read from the image, because
* the last block to write might exceed the nominal size of the
* image for technical reasons. */
/* If we have data to be written, use that instead of reading
* data from the image. */
if (cbWrite > cbThisWrite)
/* The rest must be read from the image. */
}
/* Read the entire data of the block so that we can compare whether it will
* be modified by the write or not. */
true /* fZeroFreeBlocks */,
false /* fUpdateCache */);
if (RT_FAILURE(rc))
return rc;
/* Check if the write would modify anything in this block. */
{
/* Block is completely unchanged, so no need to write anything. */
return VINF_SUCCESS;
}
/* Copy the data to the right place in the buffer. */
/* Handle the data that goes after the write to fill the block. */
if (cbPostRead)
{
/* Now assemble the remaining data. */
if (cbWriteCopy)
/* Zero out the remainder of this block. Will never be visible, as this
* is beyond the limit of the image. */
if (cbFill)
'\0', cbFill);
}
/* Write the full block to the virtual disk. */
Assert(cbPostRead == 0);
return rc;
}
/**
* internal: write buffer to the image, taking care of block boundaries and
* write optimizations.
*/
bool fUpdateCache)
{
int rc;
unsigned fWrite;
/* Loop until all written. */
do
{
/* Try to write the possibly partial block to the last opened image.
* This works when the block is already allocated in this image or
* if it is a full-block write (and allocation isn't suppressed below).
* For image formats which don't support zero blocks, it's beneficial
* to avoid unnecessarily allocating unchanged blocks. This prevents
* unwanted expanding of images. VMDK is an example. */
? 0 : VD_WRITE_NO_ALLOC;
&cbPostRead, fWrite);
if (rc == VERR_VD_BLOCK_FREE)
{
{
/* Optimized write, suppress writing to a so far unallocated
* block if the data is in fact not changed. */
}
else
{
/* Normal write, not optimized in any way. The block will
* be written no matter what. This will usually (unless the
* backend has some further optimization enabled) cause the
* block to be allocated. */
}
if (RT_FAILURE(rc))
break;
}
/* Update the cache on success */
if ( RT_SUCCESS(rc)
&& fUpdateCache)
return rc;
}
/**
* Flush helper async version.
*/
{
int rc = VINF_SUCCESS;
if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
rc = VINF_SUCCESS;
return rc;
}
/**
* internal: mark the disk as modified - async version.
*/
{
int rc = VINF_SUCCESS;
{
if (RT_SUCCESS(rc))
{
/* First modify, so create a UUID and ensure it's written to disk. */
{
if (pIoCtxFlush)
{
if (rc == VINF_VD_ASYNC_IO_FINISHED)
{
}
else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
{
}
else /* Another error */
}
else
rc = VERR_NO_MEMORY;
}
}
}
return rc;
}
/**
* internal: write a complete block (only used for diff images), taking the
* remaining data from parent images. This implementation does not optimize
* anything (except that it tries to read only that portions from parent
* images that are really needed) - async version.
*/
{
int rc = VINF_SUCCESS;
#if 0
/* Read the data that goes before the write to fill the block. */
if (cbPreRead)
{
if (RT_FAILURE(rc))
return rc;
}
/* Copy the data to the right place in the buffer. */
/* Read the data that goes after the write to fill the block. */
if (cbPostRead)
{
/* If we have data to be written, use that instead of reading
* data from the image. */
if (cbWrite > cbThisWrite)
else
cbWriteCopy = 0;
/* Figure out how much we cannot read from the image, because
* the last block to write might exceed the nominal size of the
* image for technical reasons. */
else
cbFill = 0;
/* The rest must be read from the image. */
/* Now assemble the remaining data. */
if (cbWriteCopy)
{
}
if (cbReadImage)
if (RT_FAILURE(rc))
return rc;
/* Zero out the remainder of this block. Will never be visible, as this
* is beyond the limit of the image. */
if (cbFill)
{
}
}
if ( !pIoCtxDst->cbTransferLeft
{
/* Write the full block to the virtual disk. */
Assert(cbPostRead == 0);
}
else
{
LogFlow(("cbTransferLeft=%u cMetaTransfersPending=%u fComplete=%RTbool\n",
}
return rc;
#endif
return VERR_NOT_IMPLEMENTED;
}
{
int rc = VINF_SUCCESS;
if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
rc = VINF_SUCCESS;
else if (rc == VERR_VD_IOCTX_HALT)
{
rc = VINF_SUCCESS;
}
return rc;
}
{
int rc = VINF_SUCCESS;
size_t cbThisWrite = 0;
/* Check if the write would modify anything in this block. */
{
{
/* Block is completely unchanged, so no need to write anything. */
LogFlowFunc(("Block didn't changed\n"));
return VINF_VD_ASYNC_IO_FINISHED;
}
}
/* Copy the data to the right place in the buffer. */
/* Handle the data that goes after the write to fill the block. */
if (cbPostRead)
{
/* Now assemble the remaining data. */
if (cbWriteCopy)
{
/*
* The S/G buffer of the parent needs to be cloned because
* it is not allowed to modify the state.
*/
}
/* Zero out the remainder of this block. Will never be visible, as this
* is beyond the limit of the image. */
if (cbFill)
{
}
}
/* Write the full block to the virtual disk. */
return rc;
}
{
int rc = VINF_SUCCESS;
if (pIoCtx->cbTransferLeft)
if ( RT_SUCCESS(rc)
&& ( pIoCtx->cbTransferLeft
|| pIoCtx->cMetaTransfersPending))
else
return rc;
}
/**
* internal: write a complete block (only used for diff images), taking the
* remaining data from parent images. This implementation optimizes out writes
* that do not change the data relative to the state as of the parent images.
* All backends which support differential/growing images support this - async version.
*/
{
size_t cbWriteCopy = 0;
size_t cbReadImage = 0;
if (cbPostRead)
{
/* Figure out how much we cannot read from the image, because
* the last block to write might exceed the nominal size of the
* image for technical reasons. */
/* If we have data to be written, use that instead of reading
* data from the image. */
if (cbWrite > cbThisWrite)
/* The rest must be read from the image. */
}
/* Read the entire data of the block so that we can compare whether it will
* be modified by the write or not. */
/* Next step */
return VINF_SUCCESS;
}
/**
* internal: write buffer to the image, taking care of block boundaries and
* write optimizations - async version.
*/
{
int rc;
unsigned fWrite;
return rc;
/* Loop until all written. */
do
{
/* Try to write the possibly partial block to the last opened image.
* This works when the block is already allocated in this image or
* if it is a full-block write (and allocation isn't suppressed below).
* For image formats which don't support zero blocks, it's beneficial
* to avoid unnecessarily allocating unchanged blocks. This prevents
* unwanted expanding of images. VMDK is an example. */
? 0 : VD_WRITE_NO_ALLOC;
&cbThisWrite, &cbPreRead,
&cbPostRead, fWrite);
if (rc == VERR_VD_BLOCK_FREE)
{
/* Lock the disk .*/
if (RT_SUCCESS(rc))
{
/*
* Allocate segment and buffer in one go.
* A bit hackish but avoids the need to allocate memory twice.
*/
PRTSGBUF pTmp = (PRTSGBUF)RTMemAlloc(cbPreRead + cbThisWrite + cbPostRead + sizeof(RTSGSEG) + sizeof(RTSGBUF));
pTmp,
pTmp,
if (!VALID_PTR(pIoCtxWrite))
{
rc = VERR_NO_MEMORY;
break;
}
LogFlowFunc(("Disk is growing because of pIoCtx=%#p pIoCtxWrite=%#p\n",
pIoCtx, pIoCtxWrite));
/* Process the write request */
{
break;
}
else if ( rc == VINF_VD_ASYNC_IO_FINISHED
{
LogFlow(("Child write request completed\n"));
rc = VINF_SUCCESS;
}
else
{
LogFlow(("Child write pending\n"));
cbWrite -= cbThisWrite;
uOffset += cbThisWrite;
break;
}
}
else
{
break;
}
}
if (rc == VERR_VD_IOCTX_HALT)
{
cbWrite -= cbThisWrite;
uOffset += cbThisWrite;
break;
}
else if (rc == VERR_VD_NOT_ENOUGH_METADATA)
break;
cbWrite -= cbThisWrite;
uOffset += cbThisWrite;
if ( rc == VERR_VD_ASYNC_IO_IN_PROGRESS
|| rc == VERR_VD_IOCTX_HALT)
{
/*
* Tell the caller that we don't need to go back here because all
* writes are initiated.
*/
if (!cbWrite)
rc = VINF_SUCCESS;
}
return rc;
}
/**
* Flush helper async version.
*/
{
int rc = VINF_SUCCESS;
if (RT_SUCCESS(rc))
{
if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
rc = VINF_SUCCESS;
else if (rc == VINF_VD_ASYNC_IO_FINISHED)
}
return rc;
}
/**
* internal: scans plugin directory and loads the backends have been found.
*/
static int vdLoadDynamicBackends()
{
#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
int rc = VINF_SUCCESS;
/* Enumerate plugin backends. */
char szPath[RTPATH_MAX];
if (RT_FAILURE(rc))
return rc;
/* To get all entries with VBoxHDD as prefix. */
if (!pszPluginFilter)
return VERR_NO_STR_MEMORY;
/* The plugins are in the same directory as the other shared libs. */
if (RT_FAILURE(rc))
{
/* On Windows the above immediately signals that there are no
* files matching, while on other platforms enumerating the
* files below fails. Either way: no plugins. */
goto out;
}
if (!pPluginDirEntry)
{
rc = VERR_NO_MEMORY;
goto out;
}
while ((rc = RTDirReadEx(pPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK)) != VERR_NO_MORE_FILES)
{
char *pszPluginPath = NULL;
if (rc == VERR_BUFFER_OVERFLOW)
{
/* allocate new buffer. */
if (!pPluginDirEntry)
{
rc = VERR_NO_MEMORY;
break;
}
/* Retry. */
rc = RTDirReadEx(pPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
if (RT_FAILURE(rc))
break;
}
else if (RT_FAILURE(rc))
break;
/* We got the new entry. */
continue;
/* Prepend the path to the libraries. */
if (!pszPluginPath)
{
break;
}
if (RT_SUCCESS(rc))
{
{
LogFunc(("error resolving the entry point %s in plugin %s, rc=%Rrc, pfnHDDFormat=%#p\n", VBOX_HDDFORMAT_LOAD_NAME, pPluginDirEntry->szName, rc, pfnHDDFormatLoad));
if (RT_SUCCESS(rc))
}
if (RT_SUCCESS(rc))
{
/* Get the function table. */
{
}
else
LogFunc(("ignored plugin '%s': pBackend->cbSize=%d rc=%Rrc\n", pszPluginPath, pBackend->cbSize, rc));
}
else
if (RT_FAILURE(rc))
}
}
out:
if (rc == VERR_NO_MORE_FILES)
rc = VINF_SUCCESS;
if (pPluginDirEntry)
if (pPluginDir)
return rc;
#else
return VINF_SUCCESS;
#endif
}
/**
* internal: scans plugin directory and loads the cache backends have been found.
*/
static int vdLoadDynamicCacheBackends()
{
#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
int rc = VINF_SUCCESS;
/* Enumerate plugin backends. */
char szPath[RTPATH_MAX];
if (RT_FAILURE(rc))
return rc;
/* To get all entries with VBoxHDD as prefix. */
if (!pszPluginFilter)
{
return rc;
}
/* The plugins are in the same directory as the other shared libs. */
if (RT_FAILURE(rc))
{
/* On Windows the above immediately signals that there are no
* files matching, while on other platforms enumerating the
* files below fails. Either way: no plugins. */
goto out;
}
if (!pPluginDirEntry)
{
rc = VERR_NO_MEMORY;
goto out;
}
while ((rc = RTDirReadEx(pPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK)) != VERR_NO_MORE_FILES)
{
char *pszPluginPath = NULL;
if (rc == VERR_BUFFER_OVERFLOW)
{
/* allocate new buffer. */
if (!pPluginDirEntry)
{
rc = VERR_NO_MEMORY;
break;
}
/* Retry. */
rc = RTDirReadEx(pPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
if (RT_FAILURE(rc))
break;
}
else if (RT_FAILURE(rc))
break;
/* We got the new entry. */
continue;
/* Prepend the path to the libraries. */
if (!pszPluginPath)
{
break;
}
if (RT_SUCCESS(rc))
{
{
LogFunc(("error resolving the entry point %s in plugin %s, rc=%Rrc, pfnVDCacheLoad=%#p\n",
if (RT_SUCCESS(rc))
}
if (RT_SUCCESS(rc))
{
/* Get the function table. */
{
}
else
LogFunc(("ignored plugin '%s': pBackend->cbSize=%d rc=%Rrc\n", pszPluginPath, pBackend->cbSize, rc));
}
else
if (RT_FAILURE(rc))
}
}
out:
if (rc == VERR_NO_MORE_FILES)
rc = VINF_SUCCESS;
if (pPluginDirEntry)
if (pPluginDir)
return rc;
#else
return VINF_SUCCESS;
#endif
}
/**
* VD async I/O interface open callback.
*/
void **ppStorage)
{
if (!pStorage)
return VERR_NO_MEMORY;
/* Open the file. */
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
return rc;
}
/**
* VD async I/O interface close callback.
*/
{
return VINF_SUCCESS;
}
{
return RTFileDelete(pcszFilename);
}
{
}
{
}
static int vdIOGetModificationTimeFallback(void *pvUser, const char *pcszFilename, PRTTIMESPEC pModificationTime)
{
if (RT_SUCCESS(rc))
return rc;
}
/**
* VD async I/O interface callback for retrieving the file size.
*/
{
}
/**
* VD async I/O interface callback for setting the file size.
*/
{
}
/**
* VD async I/O interface callback for a synchronous write to the file.
*/
{
}
/**
* VD async I/O interface callback for a synchronous read from the file.
*/
{
}
/**
* VD async I/O interface callback for a synchronous flush of the file data.
*/
{
}
/**
* VD async I/O interface callback for a asynchronous read from the file.
*/
void **ppTask)
{
return VERR_NOT_IMPLEMENTED;
}
/**
* VD async I/O interface callback for a asynchronous write to the file.
*/
void **ppTask)
{
return VERR_NOT_IMPLEMENTED;
}
/**
* VD async I/O interface callback for a asynchronous flush of the file data.
*/
void *pvCompletion, void **ppTask)
{
return VERR_NOT_IMPLEMENTED;
}
/**
* Internal - Continues an I/O context after
* it was halted because of an active transfer.
*/
{
int rc = VINF_SUCCESS;
if (RT_FAILURE(rcReq))
{
/* Continue the transfer */
if ( rc == VINF_VD_ASYNC_IO_FINISHED
{
if (pIoCtx->pIoCtxParent)
{
{
LogFlowFunc(("I/O context transferred %u bytes for the parent pIoCtxParent=%p\n",
/* Update the parent state. */
}
else
/*
* A completed child write means that we finished growing the image.
* We have to process any pending writes now.
*/
/* Unblock the parent */
pIoCtxParent->fBlocked = false;
if ( rc == VINF_VD_ASYNC_IO_FINISHED
{
LogFlowFunc(("Parent I/O context completed pIoCtxParent=%#p rcReq=%Rrc\n", pIoCtxParent, pIoCtxParent->rcReq));
}
/* Process any pending writes if the current request didn't caused another growing. */
{
/* Process the list. */
do
{
pIoCtxWait->fBlocked = false;
if ( rc == VINF_VD_ASYNC_IO_FINISHED
{
pIoCtxWait->rcReq);
}
} while (!RTListIsEmpty(&ListTmp));
}
else
}
else
{
{
}
else
{
}
}
}
}
return VINF_SUCCESS;
}
/**
* Internal - Called when user transfer completed.
*/
{
int rc = VINF_SUCCESS;
bool fIoCtxContinue = true;
LogFlowFunc(("pIoStorage=%#p pIoCtx=%#p pfnComplete=%#p pvUser=%#p cbTransfer=%zu rcReq=%Rrc\n",
if (pfnComplete)
if (RT_SUCCESS(rc))
else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
rc = VINF_SUCCESS;
return rc;
}
/**
* Internal - Called when a meta transfer completed.
*/
static int vdMetaXferCompleted(PVDIOSTORAGE pIoStorage, PFNVDXFERCOMPLETED pfnComplete, void *pvUser,
{
bool fFlush;
LogFlowFunc(("pIoStorage=%#p pfnComplete=%#p pvUser=%#p pMetaXfer=%#p rcReq=%Rrc\n",
if (!fFlush)
{
if (RT_FAILURE(rcReq))
{
/* Remove from the AVL tree. */
}
else
{
/* Increase the reference counter to make sure it doesn't go away before the last context is processed. */
}
}
else
/* Go through the waiting list and continue the I/O contexts. */
while (!RTListIsEmpty(&ListIoCtxWaiting))
{
int rc = VINF_SUCCESS;
bool fContinue = true;
if (pfnComplete)
if (RT_SUCCESS(rc))
{
}
else
}
/* Remove if not used anymore. */
{
{
/* Remove from the AVL tree. */
}
}
else if (fFlush)
return VINF_SUCCESS;
}
{
int rc = VINF_SUCCESS;
else
return rc;
}
/**
* VD I/O interface callback for opening a file.
*/
{
int rc = VINF_SUCCESS;
if (!pIoStorage)
return VERR_NO_MEMORY;
/* Create the AVl tree. */
if (pIoStorage->pTreeMetaXfers)
{
&pIoStorage->pStorage);
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
}
else
rc = VERR_NO_MEMORY;
return rc;
}
{
AssertMsgFailed(("Tree should be empty at this point!\n"));
return VINF_SUCCESS;
}
{
return VINF_SUCCESS;
}
{
}
unsigned fMove)
{
}
{
}
{
}
{
pcbSize);
}
{
cbSize);
}
{
}
{
pcbRead);
}
{
}
{
int rc = VINF_SUCCESS;
LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pIoCtx=%#p cbRead=%u\n",
/* Build the S/G array and spawn a new I/O task */
while (cbRead)
{
unsigned cSegments = VD_IO_TASK_SEGMENTS_MAX;
size_t cbTaskRead = 0;
Assert(cbTaskRead > 0);
#ifdef RT_STRICT
for (unsigned i = 0; i < cSegments; i++)
("Segment %u is invalid\n", i));
#endif
if (!pIoTask)
return VERR_NO_MEMORY;
void *pvTask;
&pvTask);
if (RT_SUCCESS(rc))
{
}
else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
{
break;
}
uOffset += cbTaskRead;
cbRead -= cbTaskRead;
}
return rc;
}
void *pvCompleteUser)
{
int rc = VINF_SUCCESS;
LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pIoCtx=%#p cbWrite=%u\n",
/* Build the S/G array and spawn a new I/O task */
while (cbWrite)
{
unsigned cSegments = VD_IO_TASK_SEGMENTS_MAX;
size_t cbTaskWrite = 0;
Assert(cbTaskWrite > 0);
#ifdef DEBUG
for (unsigned i = 0; i < cSegments; i++)
("Segment %u is invalid\n", i));
#endif
PVDIOTASK pIoTask = vdIoTaskUserAlloc(pIoStorage, pfnComplete, pvCompleteUser, pIoCtx, cbTaskWrite);
if (!pIoTask)
return VERR_NO_MEMORY;
void *pvTask;
&pvTask);
if (RT_SUCCESS(rc))
{
}
else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
{
break;
}
uOffset += cbTaskWrite;
cbWrite -= cbTaskWrite;
}
return rc;
}
void *pvCompleteUser)
{
int rc = VINF_SUCCESS;
LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pvBuf=%#p cbRead=%u\n",
if (!pMetaXfer)
{
#ifdef RT_STRICT
pMetaXfer = (PVDMETAXFER)RTAvlrFileOffsetGetBestFit(pIoStorage->pTreeMetaXfers, uOffset, false /* fAbove */);
("Overlapping meta transfers!\n"));
#endif
/* Allocate a new meta transfer. */
if (!pMetaXfer)
return VERR_NO_MEMORY;
if (!pIoTask)
{
return VERR_NO_MEMORY;
}
&pvTask);
{
}
else
if (RT_SUCCESS(rc))
{
}
}
{
/* If it is pending add the request to the list. */
{
}
else
{
/* Transfer the data. */
*ppMetaXfer = pMetaXfer;
}
}
return rc;
}
void *pvCompleteUser)
{
int rc = VINF_SUCCESS;
bool fInTree = false;
LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pvBuf=%#p cbWrite=%u\n",
if (!pMetaXfer)
{
/* Allocate a new meta transfer. */
if (!pMetaXfer)
return VERR_NO_MEMORY;
}
else
{
fInTree = true;
}
if (!pIoTask)
{
return VERR_NO_MEMORY;
}
&pvTask);
if (RT_SUCCESS(rc))
{
{
}
}
else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
{
if (!fInTree)
{
}
}
else
{
}
return rc;
}
{
{
/* Free the meta data entry. */
}
}
void *pvCompleteUser)
{
int rc = VINF_SUCCESS;
LogFlowFunc(("pvUser=%#p pIoStorage=%#p pIoCtx=%#p\n",
/* Allocate a new meta transfer. */
if (!pMetaXfer)
return VERR_NO_MEMORY;
if (!pIoTask)
{
return VERR_NO_MEMORY;
}
if (RT_SUCCESS(rc))
{
}
else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
return rc;
}
{
return cbCopied;
}
{
return cbCopied;
}
{
return cbSet;
}
{
return cbCreated;
}
{
/*
* Grab the disk critical section to avoid races with other threads which
* might still modify the I/O context.
* Example is that iSCSI is doing an asynchronous write but calls us already
* while the other thread is still hanging in vdWriteHelperAsync and couldn't update
* the fBlocked state yet.
* It can overwrite the state to true before we call vdIoCtxContinue and the
* the request would hang indefinite.
*/
/* Continue */
/* Clear the pointer to next transfer function in case we have nothing to transfer anymore.
if (!pIoCtx->cbTransferLeft)
}
/**
* VD I/O interface callback for opening a file (limited version for VDGetFormat).
*/
{
int rc = VINF_SUCCESS;
if (!pIoStorage)
return VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
else
return rc;
}
{
return VINF_SUCCESS;
}
{
}
{
}
{
}
static int vdIOIntGetModificationTimeLimited(void *pvUser,
const char *pcszFilename,
{
}
{
}
{
}
{
return pInterfaceIOCallbacks->pfnWriteSync(NULL, pIoStorage->pStorage, uOffset, pvBuf, cbWrite, pcbWritten);
}
{
return pInterfaceIOCallbacks->pfnReadSync(NULL, pIoStorage->pStorage, uOffset, pvBuf, cbRead, pcbRead);
}
{
}
/**
* internal: send output to the log (unconditionally).
*/
{
return VINF_SUCCESS;
}
{
return rc;
}
/**
* internal: adjust PCHS geometry
*/
{
/* Fix broken PCHS geometry. Can happen for two reasons: either the backend
* mixes up PCHS and LCHS, or the application used to create the source
* image has put garbage in it. Additionally, if the PCHS geometry covers
* more than the image size, set it back to the default. */
|| pPCHS->cCylinders == 0
{
}
}
/**
* internal: adjust PCHS geometry
*/
{
/* Fix broken LCHS geometry. Can happen for two reasons: either the backend
* mixes up PCHS and LCHS, or the application used to create the source
* image has put garbage in it. The fix in this case is to clear the LCHS
* geometry to trigger autodetection when it is used next. If the geometry
* already says "please autodetect" (cylinders=0) keep it. */
&& pLCHS->cCylinders != 0)
{
pLCHS->cCylinders = 0;
}
/* Always recompute the number of cylinders stored in the LCHS
* geometry if it isn't set to "autotedetect" at the moment.
* This is very useful if the destination image size is
* larger or smaller than the source image size. Do not modify
* the number of heads and sectors. Windows guests hate it. */
if ( pLCHS->cCylinders != 0
{
Assert(!(RT_MIN(cbSize / 512 / pLCHS->cHeads / pLCHS->cSectors, 1024) - (uint32_t)RT_MIN(cbSize / 512 / pLCHS->cHeads / pLCHS->cSectors, 1024)));
}
}
/**
* Initializes HDD backends.
*
* @returns VBox status code.
*/
VBOXDDU_DECL(int) VDInit(void)
{
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
rc = vdLoadDynamicBackends();
if (RT_SUCCESS(rc))
}
}
LogRel(("VDInit finished\n"));
return rc;
}
/**
* Destroys loaded HDD backends.
*
* @returns VBox status code.
*/
VBOXDDU_DECL(int) VDShutdown(void)
{
unsigned cBackends = g_cBackends;
if (!pBackends)
return VERR_INTERNAL_ERROR;
g_cBackends = 0;
g_apBackends = NULL;
#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
for (unsigned i = 0; i < cBackends; i++)
#endif
/* Clear the supported cache backends. */
g_cCacheBackends = 0;
#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
for (unsigned i = 0; i < cBackends; i++)
#endif
if (pCacheBackends)
return VINF_SUCCESS;
}
/**
* Lists all HDD backends and their capabilities in a caller-provided buffer.
*
* @returns VBox status code.
* VERR_BUFFER_OVERFLOW if not enough space is passed.
* @param cEntriesAlloc Number of list entries available.
* @param pEntries Pointer to array for the entries.
* @param pcEntriesUsed Number of entries returned.
*/
unsigned *pcEntriesUsed)
{
int rc = VINF_SUCCESS;
unsigned cEntries = 0;
LogFlowFunc(("cEntriesAlloc=%u pEntries=%#p pcEntriesUsed=%#p\n", cEntriesAlloc, pEntries, pcEntriesUsed));
/* Check arguments. */
("cEntriesAlloc=%u\n", cEntriesAlloc),
("pEntries=%#p\n", pEntries),
("pcEntriesUsed=%#p\n", pcEntriesUsed),
if (!g_apBackends)
VDInit();
if (cEntriesAlloc < g_cBackends)
{
return VERR_BUFFER_OVERFLOW;
}
for (unsigned i = 0; i < g_cBackends; i++)
{
}
return rc;
}
/**
* Lists the capabilities of a backend identified by its name.
*
* @returns VBox status code.
* @param pszBackend The backend name.
* @param pEntries Pointer to an entry.
*/
{
/* Check arguments. */
("pszBackend=%#p\n", pszBackend),
("pEntry=%#p\n", pEntry),
if (!g_apBackends)
VDInit();
/* Go through loaded backends. */
for (unsigned i = 0; i < g_cBackends; i++)
{
{
return VINF_SUCCESS;
}
}
return VERR_NOT_FOUND;
}
/**
* Allocates and initializes an empty HDD container.
* No image files are opened.
*
* @returns VBox status code.
* @param pVDIfsDisk Pointer to the per-disk VD interface list.
* @param enmType Type of the image container.
* @param ppDisk Where to store the reference to HDD container.
*/
{
int rc = VINF_SUCCESS;
do
{
/* Check arguments. */
("ppDisk=%#p\n", ppDisk),
if (pDisk)
{
/* Create the I/O ctx cache */
if (RT_FAILURE(rc))
{
break;
}
/* Create the I/O task cache */
if (RT_FAILURE(rc))
{
break;
}
/* Create critical section. */
if (RT_FAILURE(rc))
{
break;
}
if (pDisk->pInterfaceError)
if (pDisk->pInterfaceThreadSync)
/* Create fallback I/O callback table */
/*
* Create the internal I/O callback table.
* The interface is per-image but no need to duplicate the
* callback table every time.
*/
}
else
{
rc = VERR_NO_MEMORY;
break;
}
} while (0);
return rc;
}
/**
* Destroys HDD container.
* If container has opened image files they will be closed.
*
* @param pDisk Pointer to HDD container.
*/
{
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
} while (0);
LogFlowFunc(("returns\n"));
}
/**
* Try to get the backend name which can use this image.
*
* @returns VBox status code.
* VINF_SUCCESS if a plugin was found.
* ppszFormat contains the string which can be used as backend name.
* VERR_NOT_SUPPORTED if no backend was found.
* @param pVDIfsDisk Pointer to the per-disk VD interface list.
* @param pVDIfsImage Pointer to the per-image VD interface list.
* @param pszFilename Name of the image file for which the backend is queried.
* @param ppszFormat Receives pointer of the UTF-8 string which contains the format name.
* The returned pointer must be freed using RTStrFree().
*/
{
int rc = VERR_NOT_SUPPORTED;
/* Check arguments. */
("ppszFormat=%#p\n", ppszFormat),
("penmType=%#p\n", penmType),
if (!g_apBackends)
VDInit();
if (!pInterfaceIO)
{
/*
* Caller doesn't provide an I/O interface, create our own using the
* native file API.
*/
}
else
/* Set up the internal I/O interface. */
/* Find the backend supporting this file format. */
for (unsigned i = 0; i < g_cBackends; i++)
{
if (g_apBackends[i]->pfnCheckIfValid)
{
if ( RT_SUCCESS(rc)
/* The correct backend has been found, but there is a small
* incompatibility so that the file cannot be used. Stop here
* and signal success - the actual open will of course fail,
* but that will create a really sensible error message. */
|| ( rc != VERR_VD_GEN_INVALID_HEADER
&& rc != VERR_VD_VDI_INVALID_HEADER
&& rc != VERR_VD_VHD_INVALID_HEADER
&& rc != VERR_VD_RAW_INVALID_HEADER
&& rc != VERR_VD_DMG_INVALID_HEADER))
{
/* Copy the name into the new string. */
if (!pszFormat)
{
rc = VERR_NO_MEMORY;
break;
}
*ppszFormat = pszFormat;
rc = VINF_SUCCESS;
break;
}
}
}
/* Try the cache backends. */
if (rc == VERR_NOT_SUPPORTED)
{
for (unsigned i = 0; i < g_cCacheBackends; i++)
{
if (g_apCacheBackends[i]->pfnProbe)
{
if ( RT_SUCCESS(rc)
|| (rc != VERR_VD_GEN_INVALID_HEADER))
{
/* Copy the name into the new string. */
if (!pszFormat)
{
rc = VERR_NO_MEMORY;
break;
}
*ppszFormat = pszFormat;
rc = VINF_SUCCESS;
break;
}
}
}
}
return rc;
}
/**
* Opens an image file.
*
* The first opened image file in HDD container must have a base image type,
* others (next opened images) must be a differencing or undo images.
* Linkage is checked for differencing image to be in consistence with the previously opened image.
* mode, then the last image is reopened in read-only with deny write sharing mode. This allows
* other processes to use images in read-only mode too.
*
* Use VDIsReadOnly to check open mode.
*
* @returns VBox status code.
* @param pDisk Pointer to HDD container.
* @param pszBackend Name of the image file backend to use.
* @param pszFilename Name of the image file to open.
* @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
* @param pVDIfsImage Pointer to the per-image VD interface list.
*/
const char *pszFilename, unsigned uOpenFlags,
{
int rc = VINF_SUCCESS;
int rc2;
bool fLockWrite = false;
LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uOpenFlags=%#x, pVDIfsImage=%#p\n",
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
("uOpenFlags=%#x\n", uOpenFlags),
/* Set up image descriptor. */
if (!pImage)
{
rc = VERR_NO_MEMORY;
break;
}
if (!pImage->pszFilename)
{
rc = VERR_NO_MEMORY;
break;
}
if (RT_FAILURE(rc))
break;
{
break;
}
/*
* Fail if the the backend can't do async I/O but the
* flag is set.
*/
&& (uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO))
{
break;
}
/* Set up the I/O interface. */
else
{
}
/* Set up the internal I/O interface. */
&pImage->pBackendData);
/* If the open in read-write mode failed, retry in read-only mode. */
if (RT_FAILURE(rc))
{
if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY)
&& ( rc == VERR_ACCESS_DENIED
|| rc == VERR_PERMISSION_DENIED
|| rc == VERR_WRITE_PROTECT
|| rc == VERR_SHARING_VIOLATION
|| rc == VERR_FILE_LOCK_FAILED))
&pImage->pBackendData);
if (RT_FAILURE(rc))
{
break;
}
}
/* Lock disk for writing, as we modify pDisk information below. */
fLockWrite = true;
/* Check image type. As the image itself has only partial knowledge
* whether it's a base image or not, this info is derived here. The
* base image can be fixed or normal, all others must be normal or
* diff images. Some image formats don't distinguish between normal
* and diff images, so this must be corrected here. */
unsigned uImageFlags;
if (RT_FAILURE(rc))
if ( RT_SUCCESS(rc)
&& !(uOpenFlags & VD_OPEN_FLAGS_INFO))
{
&& (uImageFlags & VD_IMAGE_FLAGS_DIFF))
{
break;
}
{
if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
{
break;
}
else
}
}
/* Ensure we always get correct diff information, even if the backend
* doesn't actually have a stored flag for this. It must not return
* bogus information for the parent UUID if it is not a diff image. */
/* Force sane optimization settings. It's not worth avoiding writes
* to fixed size images. The overhead would have almost no payback. */
if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
/** @todo optionally check UUIDs */
/* Cache disk information. */
/* Cache PCHS geometry. */
&pDisk->PCHSGeometry);
if (RT_FAILURE(rc2))
{
}
else
{
/* Make sure the PCHS geometry is properly clipped. */
}
/* Cache LCHS geometry. */
&pDisk->LCHSGeometry);
if (RT_FAILURE(rc2))
{
}
else
{
/* Make sure the LCHS geometry is properly clipped. */
}
{
/* Switch previous image to read-only mode. */
unsigned uOpenFlagsPrevImg;
if (!(uOpenFlagsPrevImg & VD_OPEN_FLAGS_READONLY))
{
}
}
if (RT_SUCCESS(rc))
{
/* Image successfully opened, make it the last image. */
if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
}
else
{
/* Error detected, but image opened. Close image. */
}
} while (0);
if (RT_UNLIKELY(fLockWrite))
{
}
if (RT_FAILURE(rc))
{
if (pImage)
{
if (pImage->pszFilename)
}
}
return rc;
}
/**
* Opens a cache image.
*
* @return VBox status code.
* @param pDisk Pointer to the HDD container which should use the cache image.
* @param pszBackend Name of the cache file backend to use (case insensitive).
* @param pszFilename Name of the cache image to open.
* @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
* @param pVDIfsCache Pointer to the per-cache VD interface list.
*/
const char *pszFilename, unsigned uOpenFlags,
{
int rc = VINF_SUCCESS;
int rc2;
bool fLockWrite = false;
LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uOpenFlags=%#x, pVDIfsCache=%#p\n",
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
("uOpenFlags=%#x\n", uOpenFlags),
/* Set up image descriptor. */
if (!pCache)
{
rc = VERR_NO_MEMORY;
break;
}
if (!pCache->pszFilename)
{
rc = VERR_NO_MEMORY;
break;
}
if (RT_FAILURE(rc))
break;
{
break;
}
/* Set up the I/O interface. */
else
{
}
/* Set up the internal I/O interface. */
&pCache->pBackendData);
/* If the open in read-write mode failed, retry in read-only mode. */
if (RT_FAILURE(rc))
{
if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY)
&& ( rc == VERR_ACCESS_DENIED
|| rc == VERR_PERMISSION_DENIED
|| rc == VERR_WRITE_PROTECT
|| rc == VERR_SHARING_VIOLATION
|| rc == VERR_FILE_LOCK_FAILED))
&pCache->pBackendData);
if (RT_FAILURE(rc))
{
break;
}
}
/* Lock disk for writing, as we modify pDisk information below. */
fLockWrite = true;
/*
* Check that the modification UUID of the cache and last image
* match. If not the image was modified in-between without the cache.
* The cache might contain stale data.
*/
&UuidCache);
if (RT_SUCCESS(rc))
{
&UuidImage);
if (RT_SUCCESS(rc))
{
}
}
/*
* We assume that the user knows what he is doing if one of the images
* doesn't support the modification uuid.
*/
if (rc == VERR_NOT_SUPPORTED)
rc = VINF_SUCCESS;
if (RT_SUCCESS(rc))
{
/* Cache successfully opened, make it the current one. */
else
}
if (RT_FAILURE(rc))
{
/* Error detected, but image opened. Close image. */
}
} while (0);
if (RT_UNLIKELY(fLockWrite))
{
}
if (RT_FAILURE(rc))
{
if (pCache)
{
if (pCache->pszFilename)
}
}
return rc;
}
/**
* Creates and opens a new base image file.
*
* @returns VBox status code.
* @param pDisk Pointer to HDD container.
* @param pszBackend Name of the image file backend to use.
* @param pszFilename Name of the image file to create.
* @param cbSize Image size in bytes.
* @param uImageFlags Flags specifying special image features.
* @param pszComment Pointer to image comment. NULL is ok.
* @param pPCHSGeometry Pointer to physical disk geometry <= (16383,16,63). Not NULL.
* @param pLCHSGeometry Pointer to logical disk geometry <= (x,255,63). Not NULL.
* @param pUuid New UUID of the image. If NULL, a new UUID is created.
* @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
* @param pVDIfsImage Pointer to the per-image VD interface list.
* @param pVDIfsOperation Pointer to the per-operation VD interface list.
*/
unsigned uImageFlags, const char *pszComment,
{
int rc = VINF_SUCCESS;
int rc2;
bool fLockWrite = false, fLockRead = false;
LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" PCHS=%u/%u/%u LCHS=%u/%u/%u Uuid=%RTuuid uOpenFlags=%#x pVDIfsImage=%#p pVDIfsOperation=%#p\n",
if (pIfProgress)
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
("cbSize=%llu\n", cbSize),
("uImageFlags=%#x\n", uImageFlags),
/* The PCHS geometry fields may be 0 to leave it for later. */
("pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pPCHSGeometry,
/* The LCHS geometry fields may be 0 to leave it to later autodetection. */
("pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pLCHSGeometry,
/* The UUID may be NULL. */
("uOpenFlags=%#x\n", uOpenFlags),
/* Check state. Needs a temporary read lock. Holding the write lock
* all the time would be blocking other activities for too long. */
fLockRead = true;
("Create base image cannot be done with other images open\n"),
fLockRead = false;
/* Set up image descriptor. */
if (!pImage)
{
rc = VERR_NO_MEMORY;
break;
}
if (!pImage->pszFilename)
{
rc = VERR_NO_MEMORY;
break;
}
/* Set up the I/O interface. */
else
{
}
/* Set up the internal I/O interface. */
if (RT_FAILURE(rc))
break;
{
break;
}
{
break;
}
/* Create UUID if the caller didn't specify one. */
if (!pUuid)
{
if (RT_FAILURE(rc))
{
N_("VD: cannot generate UUID for image '%s'"),
break;
}
}
0, 99,
&pImage->pBackendData);
if (RT_SUCCESS(rc))
{
/* Force sane optimization settings. It's not worth avoiding writes
* to fixed size images. The overhead would have almost no payback. */
if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
/* Lock disk for writing, as we modify pDisk information below. */
fLockWrite = true;
/** @todo optionally check UUIDs */
/* Re-check state, as the lock wasn't held and another image
* creation call could have been done by another thread. */
("Create base image cannot be done with other images open\n"),
}
if (RT_SUCCESS(rc))
{
/* Cache disk information. */
/* Cache PCHS geometry. */
&pDisk->PCHSGeometry);
if (RT_FAILURE(rc2))
{
}
else
{
/* Make sure the CHS geometry is properly clipped. */
}
/* Cache LCHS geometry. */
&pDisk->LCHSGeometry);
if (RT_FAILURE(rc2))
{
}
else
{
/* Make sure the CHS geometry is properly clipped. */
}
/* Image successfully opened, make it the last image. */
if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
}
else
{
/* Error detected, image may or may not be opened. Close and delete
* image if it was opened. */
if (pImage->pBackendData)
{
}
}
} while (0);
if (RT_UNLIKELY(fLockWrite))
{
}
else if (RT_UNLIKELY(fLockRead))
{
}
if (RT_FAILURE(rc))
{
if (pImage)
{
if (pImage->pszFilename)
}
}
return rc;
}
/**
* Creates and opens a new differencing image file in HDD container.
* See comments for VDOpen function about differencing images.
*
* @returns VBox status code.
* @param pDisk Pointer to HDD container.
* @param pszBackend Name of the image file backend to use.
* @param pszFilename Name of the differencing image file to create.
* @param uImageFlags Flags specifying special image features.
* @param pszComment Pointer to image comment. NULL is ok.
* @param pUuid New UUID of the image. If NULL, a new UUID is created.
* @param pParentUuid New parent UUID of the image. If NULL, the UUID is queried automatically.
* @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
* @param pVDIfsImage Pointer to the per-image VD interface list.
* @param pVDIfsOperation Pointer to the per-operation VD interface list.
*/
const char *pszFilename, unsigned uImageFlags,
{
int rc = VINF_SUCCESS;
int rc2;
bool fLockWrite = false, fLockRead = false;
LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uImageFlags=%#x pszComment=\"%s\" Uuid=%RTuuid uOpenFlags=%#x pVDIfsImage=%#p pVDIfsOperation=%#p\n",
pDisk, pszBackend, pszFilename, uImageFlags, pszComment, pUuid, uOpenFlags, pVDIfsImage, pVDIfsOperation));
if (pIfProgress)
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
("uImageFlags=%#x\n", uImageFlags),
/* The UUID may be NULL. */
/* The parent UUID may be NULL. */
("uOpenFlags=%#x\n", uOpenFlags),
/* Check state. Needs a temporary read lock. Holding the write lock
* all the time would be blocking other activities for too long. */
fLockRead = true;
("Create diff image cannot be done without other images open\n"),
fLockRead = false;
/* Set up image descriptor. */
if (!pImage)
{
rc = VERR_NO_MEMORY;
break;
}
if (!pImage->pszFilename)
{
rc = VERR_NO_MEMORY;
break;
}
if (RT_FAILURE(rc))
break;
{
break;
}
{
break;
}
/* Set up the I/O interface. */
else
{
}
/* Set up the internal I/O interface. */
/* Create UUID if the caller didn't specify one. */
if (!pUuid)
{
if (RT_FAILURE(rc))
{
N_("VD: cannot generate UUID for image '%s'"),
break;
}
}
0, 99,
&pImage->pBackendData);
if (RT_SUCCESS(rc))
{
/* Lock disk for writing, as we modify pDisk information below. */
fLockWrite = true;
/* Switch previous image to read-only mode. */
unsigned uOpenFlagsPrevImg;
if (!(uOpenFlagsPrevImg & VD_OPEN_FLAGS_READONLY))
{
}
/** @todo optionally check UUIDs */
/* Re-check state, as the lock wasn't held and another image
* creation call could have been done by another thread. */
("Create diff image cannot be done without other images open\n"),
}
if (RT_SUCCESS(rc))
{
{
Uuid = *pParentUuid;
}
else
{
&Uuid);
if (RT_SUCCESS(rc2))
}
&Uuid);
if (RT_SUCCESS(rc2))
&Uuid);
&ts);
else
}
if (RT_SUCCESS(rc))
{
/* Image successfully opened, make it the last image. */
if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
}
else
{
/* Error detected, but image opened. Close and delete image. */
}
} while (0);
if (RT_UNLIKELY(fLockWrite))
{
}
else if (RT_UNLIKELY(fLockRead))
{
}
if (RT_FAILURE(rc))
{
if (pImage)
{
if (pImage->pszFilename)
}
}
return rc;
}
/**
* Creates and opens new cache image file in HDD container.
*
* @return VBox status code.
* @param pDisk Name of the cache file backend to use (case insensitive).
* @param pszFilename Name of the differencing cache file to create.
* @param cbSize Maximum size of the cache.
* @param uImageFlags Flags specifying special cache features.
* @param pszComment Pointer to image comment. NULL is ok.
* @param pUuid New UUID of the image. If NULL, a new UUID is created.
* @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
* @param pVDIfsCache Pointer to the per-cache VD interface list.
* @param pVDIfsOperation Pointer to the per-operation VD interface list.
*/
unsigned uImageFlags, const char *pszComment,
{
int rc = VINF_SUCCESS;
int rc2;
bool fLockWrite = false, fLockRead = false;
LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" Uuid=%RTuuid uOpenFlags=%#x pVDIfsImage=%#p pVDIfsOperation=%#p\n",
pDisk, pszBackend, pszFilename, cbSize, uImageFlags, pszComment, pUuid, uOpenFlags, pVDIfsCache, pVDIfsOperation));
if (pIfProgress)
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
("cbSize=%llu\n", cbSize),
("uImageFlags=%#x\n", uImageFlags),
/* The UUID may be NULL. */
("uOpenFlags=%#x\n", uOpenFlags),
/* Check state. Needs a temporary read lock. Holding the write lock
* all the time would be blocking other activities for too long. */
fLockRead = true;
("Create cache image cannot be done with a cache already attached\n"),
fLockRead = false;
/* Set up image descriptor. */
if (!pCache)
{
rc = VERR_NO_MEMORY;
break;
}
if (!pCache->pszFilename)
{
rc = VERR_NO_MEMORY;
break;
}
if (RT_FAILURE(rc))
break;
{
break;
}
/* Set up the I/O interface. */
else
{
}
/* Set up the internal I/O interface. */
/* Create UUID if the caller didn't specify one. */
if (!pUuid)
{
if (RT_FAILURE(rc))
{
N_("VD: cannot generate UUID for image '%s'"),
break;
}
}
0, 99,
&pCache->pBackendData);
if (RT_SUCCESS(rc))
{
/* Lock disk for writing, as we modify pDisk information below. */
fLockWrite = true;
/* Re-check state, as the lock wasn't held and another image
* creation call could have been done by another thread. */
("Create cache image cannot be done with another cache open\n"),
}
if ( RT_SUCCESS(rc)
{
/* Set same modification Uuid as the last image. */
if (RT_SUCCESS(rc))
{
}
if (rc == VERR_NOT_SUPPORTED)
rc = VINF_SUCCESS;
}
if (RT_SUCCESS(rc))
{
/* Cache successfully created. */
}
else
{
/* Error detected, but image opened. Close and delete image. */
}
} while (0);
if (RT_UNLIKELY(fLockWrite))
{
}
else if (RT_UNLIKELY(fLockRead))
{
}
if (RT_FAILURE(rc))
{
if (pCache)
{
if (pCache->pszFilename)
}
}
return rc;
}
/**
* As a side effect the source image and potentially the other images which
* are also merged to the destination are deleted from both the disk and the
* images in the HDD container.
*
* @returns VBox status code.
* @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
* @param pDisk Pointer to HDD container.
* @param nImageFrom Name of the image file to merge from.
* @param nImageTo Name of the image file to merge to.
* @param pVDIfsOperation Pointer to the per-operation VD interface list.
*/
{
int rc = VINF_SUCCESS;
int rc2;
bool fLockWrite = false, fLockRead = false;
LogFlowFunc(("pDisk=%#p nImageFrom=%u nImageTo=%u pVDIfsOperation=%#p\n",
if (pIfProgress)
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* For simplicity reasons lock for writing as the image reopen below
* might need it. After all the reopen is usually needed. */
fLockWrite = true;
if (!pImageFrom || !pImageTo)
{
break;
}
/* Make sure destination image is writable. */
if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
{
if (RT_FAILURE(rc))
break;
}
/* Get size of destination image. */
fLockWrite = false;
/* Allocate tmp buffer. */
if (!pvBuf)
{
rc = VERR_NO_MEMORY;
break;
}
/* Merging is done directly on the images itself. This potentially
* causes trouble if the disk is full in the middle of operation. */
if (nImageFrom < nImageTo)
{
/* Merge parent state into child. This means writing all not
* allocated blocks in the destination image which are allocated in
* the images to be merged. */
do
{
/* Need to hold the write lock during a read-write operation. */
fLockWrite = true;
&cbThisRead);
if (rc == VERR_VD_BLOCK_FREE)
{
/* Search for image with allocated block. Do not attempt to
* read more than the previous reads marked as valid.
* Otherwise this would return stale data when different
* block sizes are used for the images. */
{
&cbThisRead);
}
if (rc != VERR_VD_BLOCK_FREE)
{
if (RT_FAILURE(rc))
break;
/* Updating the cache is required because this might be a live merge. */
true /* fUpdateCache */);
if (RT_FAILURE(rc))
break;
}
else
rc = VINF_SUCCESS;
}
else if (RT_FAILURE(rc))
break;
fLockWrite = false;
uOffset += cbThisRead;
{
/** @todo r=klaus: this can update the progress to the same
* percentage over and over again if the image format makes
* relatively small increments. */
if (RT_FAILURE(rc))
break;
}
}
else
{
/*
* We may need to update the parent uuid of the child coming after
*
* This is done before we do the actual merge to prevent an
* inconsistent chain if the mode change fails for some reason.
*/
if (pImageFrom->pNext)
{
/* Take the write lock. */
fLockWrite = true;
if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
{
if (RT_FAILURE(rc))
break;
}
fLockWrite = false;
}
/* If the merge is from the last image we have to relay all writes
* to the merge destination as well, so that concurrent writes
* (in case of a live merge) are handled correctly. */
if (!pImageFrom->pNext)
{
/* Take the write lock. */
fLockWrite = true;
fLockWrite = false;
}
/* Merge child state into parent. This means writing all blocks
* which are allocated in the image up to the source image to the
* destination image. */
do
{
/* Need to hold the write lock during a read-write operation. */
fLockWrite = true;
/* Search for image with allocated block. Do not attempt to
* read more than the previous reads marked as valid. Otherwise
* this would return stale data when different block sizes are
* used for the images. */
{
cbThisRead, &cbThisRead);
}
if (rc != VERR_VD_BLOCK_FREE)
{
if (RT_FAILURE(rc))
break;
cbThisRead, true /* fUpdateCache */);
if (RT_FAILURE(rc))
break;
}
else
rc = VINF_SUCCESS;
fLockWrite = false;
uOffset += cbThisRead;
{
/** @todo r=klaus: this can update the progress to the same
* percentage over and over again if the image format makes
* relatively small increments. */
if (RT_FAILURE(rc))
break;
}
/* In case we set up a "write proxy" image above we must clear
* this again now to prevent stray writes. Failure or not. */
if (!pImageFrom->pNext)
{
/* Take the write lock. */
fLockWrite = true;
fLockWrite = false;
}
}
/*
* Leave in case of an error to avoid corrupted data in the image chain
* (includes cancelling the operation by the user).
*/
if (RT_FAILURE(rc))
break;
/* Need to hold the write lock while finishing the merge. */
fLockWrite = true;
/* Update parent UUID so that image chain is consistent. */
if (nImageFrom < nImageTo)
{
if (pImageFrom->pPrev)
{
&Uuid);
}
else
RTUuidClear(&Uuid);
&Uuid);
}
else
{
/* Update the parent uuid of the child of the last merged image. */
if (pImageFrom->pNext)
{
&Uuid);
&Uuid);
}
}
/* Delete the no longer needed images. */
{
if (nImageFrom < nImageTo)
else
}
/* Make sure destination image is back to read only if necessary. */
{
if (RT_FAILURE(rc))
break;
}
/*
* Make sure the child is readonly
* for the child -> parent merge direction
* if necessary.
*/
if ( nImageFrom > nImageTo
&& pImageChild
{
if (RT_FAILURE(rc))
break;
}
} while (0);
if (RT_UNLIKELY(fLockWrite))
{
}
else if (RT_UNLIKELY(fLockRead))
{
}
if (pvBuf)
return rc;
}
/**
* Copies an image from one HDD container to another.
* The copy is opened in the target HDD container.
* It is possible to convert between different image formats, because the
* backend for the destination may be different from the source.
* If both the source and destination reference the same HDD container,
* The source container is unchanged if the move operation fails, otherwise
* the image at the new location is opened in the same way as the old one was.
*
* @returns VBox status code.
* @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
* @param pDiskFrom Pointer to source HDD container.
* @param nImage Image number, counts from 0. 0 is always base image of container.
* @param pDiskTo Pointer to destination HDD container.
* @param pszBackend Name of the image file backend to use.
* @param pszFilename New name of the image (may be NULL if pDiskFrom == pDiskTo).
* @param fMoveByRename If true, attempt to perform a move by renaming (if successful the new size is ignored).
* @param cbSize New image size (0 means leave unchanged).
* @param uImageFlags Flags specifying special destination image features.
* @param pDstUuid New UUID of the destination image. If NULL, a new UUID is created.
* This parameter is used if and only if a true copy is created.
* @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
* Only used if the destination image is created.
* @param pVDIfsOperation Pointer to the per-operation VD interface list.
* @param pDstVDIfsImage Pointer to the per-image VD interface list, for the
* destination image.
* @param pDstVDIfsOperation Pointer to the per-image VD interface list,
* for the destination image.
*/
const char *pszBackend, const char *pszFilename,
{
int rc = VINF_SUCCESS;
int rc2;
LogFlowFunc(("pDiskFrom=%#p nImage=%u pDiskTo=%#p pszBackend=\"%s\" pszFilename=\"%s\" fMoveByRename=%d cbSize=%llu uImageFlags=%#x pDstUuid=%#p uOpenFlags=%#x pVDIfsOperation=%#p pDstVDIfsImage=%#p pDstVDIfsOperation=%#p\n",
pDiskFrom, nImage, pDiskTo, pszBackend, pszFilename, fMoveByRename, cbSize, uImageFlags, pDstUuid, uOpenFlags, pVDIfsOperation, pDstVDIfsImage, pDstVDIfsOperation));
if (pIfProgress)
if (pDstIfProgress)
do {
/* Check arguments. */
fLockReadFrom = true;
/* Move the image. */
{
/* Rename only works when backends are the same, are file based
* and the rename method is implemented. */
if ( fMoveByRename
{
fLockReadFrom = false;
fLockWriteFrom = true;
rc = pImageFrom->Backend->pfnRename(pImageFrom->pBackendData, pszFilename ? pszFilename : pImageFrom->pszFilename);
break;
}
* requested, but the rename attempt failed or it wasn't possible.
* Must now copy image to temp location. */
AssertReleaseMsgFailed(("VDCopy: moving by copy/delete not implemented\n"));
}
/* pszFilename is allowed to be NULL, as this indicates copy to the existing image. */
if (cbSizeFrom == 0)
{
break;
}
VDGEOMETRY PCHSGeometryFrom = {0, 0, 0};
VDGEOMETRY LCHSGeometryFrom = {0, 0, 0};
{
if (pDstUuid)
else
}
else
{
if (RT_FAILURE(rc))
}
if (RT_FAILURE(rc))
char szComment[1024];
if (RT_FAILURE(rc))
szComment[0] = '\0';
else
fLockReadFrom = false;
if (pszFilename)
{
if (cbSize == 0)
cbSize = cbSizeFrom;
/* Create destination image with the properties of source image. */
/** @todo replace the VDCreateDiff/VDCreateBase calls by direct
* calls to the backend. Unifies the code and reduces the API
* dependencies. Would also make the synchronization explicit. */
if (cImagesTo > 0)
{
NULL /* pParentUuid */,
fLockWriteTo = true;
} else {
/** @todo hack to force creation of a fixed image for
* the RAW backend, which can't handle anything else. */
fLockWriteTo = true;
}
if (RT_FAILURE(rc))
break;
}
else
{
if (cbSizeTo == 0)
{
break;
}
if (cbSize == 0)
/* Update the geometry in the destination image. */
}
fLockWriteTo = false;
/* Allocate tmp buffer. */
if (!pvBuf)
{
rc = VERR_NO_MEMORY;
break;
}
/* Whether we can take the optimized copy path (false) or not.
* Don't optimize if the image existed or if it is a child image. */
/* Copy the data. */
do
{
/* Note that we don't attempt to synchronize cross-disk accesses.
* It wouldn't be very difficult to do, just the lock order would
* need to be defined somehow to prevent deadlocks. Postpone such
* magic as there is no use case for this. */
fLockReadFrom = true;
/*
* Updating the cache doesn't make any sense
* as we are looping once through the image.
*/
false /* fUpdateCache */);
break;
fLockReadFrom = false;
if (rc != VERR_VD_BLOCK_FREE)
{
fLockWriteTo = true;
cbThisRead, false /* fUpdateCache */);
if (RT_FAILURE(rc))
break;
fLockWriteTo = false;
}
else /* Don't propagate the error to the outside */
rc = VINF_SUCCESS;
uOffset += cbThisRead;
{
/** @todo r=klaus: this can update the progress to the same
* percentage over and over again if the image format makes
* relatively small increments. */
if (RT_FAILURE(rc))
break;
}
{
/** @todo r=klaus: this can update the progress to the same
* percentage over and over again if the image format makes
* relatively small increments. */
if (RT_FAILURE(rc))
break;
}
if (RT_SUCCESS(rc))
{
fLockWriteTo = true;
/* Only set modification UUID if it is non-null, since the source
* backend might not provide a valid modification UUID. */
if (!RTUuidIsNull(&ImageModificationUuid))
/* Set the requested open flags if they differ from the value
* required for creating the image and copying the contents. */
if ( pImageTo && pszFilename
}
} while (0);
{
/* Take the write lock only if it is not taken. Not worth making the
* above code even more complicated. */
if (RT_UNLIKELY(!fLockWriteTo))
{
fLockWriteTo = true;
}
/* Error detected, but new image created. Remove image from list. */
/* Close and delete image. */
/* Free remaining resources. */
if (pImageTo->pszFilename)
}
if (RT_UNLIKELY(fLockWriteTo))
{
}
if (RT_UNLIKELY(fLockWriteFrom))
{
}
else if (RT_UNLIKELY(fLockReadFrom))
{
}
if (pvBuf)
if (RT_SUCCESS(rc))
{
}
return rc;
}
/**
* Optimizes the storage consumption of an image. Typically the unused blocks
* have to be wiped with zeroes to achieve a substantial reduced storage use.
* Another optimization done is reordering the image blocks, which can provide
* a significant performance boost, as reads and writes tend to use less random
* file offsets.
*
* @return VBox status code.
* @return VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
* @return VERR_VD_IMAGE_READ_ONLY if image is not writable.
* @return VERR_NOT_SUPPORTED if this kind of image can be compacted, but
* the code for this isn't implemented yet.
* @param pDisk Pointer to HDD container.
* @param nImage Image number, counts from 0. 0 is always base image of container.
* @param pVDIfsOperation Pointer to the per-operation VD interface list.
*/
{
int rc = VINF_SUCCESS;
int rc2;
bool fLockRead = false, fLockWrite = false;
LogFlowFunc(("pDisk=%#p nImage=%u pVDIfsOperation=%#p\n",
if (pIfProgress)
do {
/* Check arguments. */
fLockRead = true;
/* If there is no compact callback for not file based backends then
* the backend doesn't need compaction. No need to make much fuss about
* this. For file based ones signal this as not yet supported. */
{
else
rc = VINF_SUCCESS;
break;
}
/* Insert interface for reading parent state into per-operation list,
* if there is a parent image. */
{
}
fLockRead = false;
fLockWrite = true;
0, 99,
} while (0);
if (RT_UNLIKELY(fLockWrite))
{
}
else if (RT_UNLIKELY(fLockRead))
{
}
if (pvBuf)
if (pvTmp)
if (RT_SUCCESS(rc))
{
}
return rc;
}
/**
* Resizes the the given disk image to the given size.
*
* @return VBox status
* @return VERR_VD_IMAGE_READ_ONLY if image is not writable.
* @return VERR_NOT_SUPPORTED if this kind of image can be compacted, but
*
* @param pDisk Pointer to the HDD container.
* @param cbSize New size of the image.
* @param pPCHSGeometry Pointer to the new physical disk geometry <= (16383,16,63). Not NULL.
* @param pLCHSGeometry Pointer to the new logical disk geometry <= (x,255,63). Not NULL.
* @param pVDIfsOperation Pointer to the per-operation VD interface list.
*/
{
/** @todo r=klaus resizing was designed to be part of VDCopy, so having a separate function is not desirable. */
int rc = VINF_SUCCESS;
int rc2;
bool fLockRead = false, fLockWrite = false;
LogFlowFunc(("pDisk=%#p cbSize=%llu pVDIfsOperation=%#p\n",
if (pIfProgress)
do {
/* Check arguments. */
fLockRead = true;
/* Not supported if the disk has child images attached. */
rc = VERR_NOT_SUPPORTED);
/* If there is no compact callback for not file based backends then
* the backend doesn't need compaction. No need to make much fuss about
* this. For file based ones signal this as not yet supported. */
{
else
rc = VINF_SUCCESS;
break;
}
fLockRead = false;
fLockWrite = true;
if (pPCHSGeometry->cCylinders == 0)
{
/* Auto-detect marker, calculate new value ourself. */
PCHSGeometryOld.cCylinders = RT_MIN(cbSize / 512 / PCHSGeometryOld.cHeads / PCHSGeometryOld.cSectors, 16383);
else if (rc == VERR_VD_GEOMETRY_NOT_SET)
rc = VINF_SUCCESS;
}
else
if (pLCHSGeometry->cCylinders == 0)
{
/* Auto-detect marker, calculate new value ourself. */
else if (rc == VERR_VD_GEOMETRY_NOT_SET)
rc = VINF_SUCCESS;
}
else
if (RT_SUCCESS(rc))
0, 99,
} while (0);
if (RT_UNLIKELY(fLockWrite))
{
}
else if (RT_UNLIKELY(fLockRead))
{
}
if (RT_SUCCESS(rc))
{
}
return rc;
}
/**
* Closes the last opened image file in HDD container.
* If previous image file was opened in read-only mode (the normal case) and
* the last opened image is in read-write mode then the previous image will be
*
* @returns VBox status code.
* @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
* @param pDisk Pointer to HDD container.
* @param fDelete If true, delete the image from the host disk.
*/
{
int rc = VINF_SUCCESS;
int rc2;
bool fLockWrite = false;
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Not worth splitting this up into a read lock phase and write
* lock phase, as closing an image is a relatively fast operation
* dominated by the part which needs the write lock. */
fLockWrite = true;
if (!pImage)
{
break;
}
/* Remove image from list of opened images. */
/* Close (and optionally delete) image. */
/* Free remaining resources related to the image. */
if (!pImage)
break;
* like this (if possible) after closing this image. Set the open flags
* accordingly. */
if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
{
}
/* Cache disk information. */
/* Cache PCHS geometry. */
&pDisk->PCHSGeometry);
if (RT_FAILURE(rc2))
{
}
else
{
/* Make sure the PCHS geometry is properly clipped. */
}
/* Cache LCHS geometry. */
&pDisk->LCHSGeometry);
if (RT_FAILURE(rc2))
{
}
else
{
/* Make sure the LCHS geometry is properly clipped. */
}
} while (0);
if (RT_UNLIKELY(fLockWrite))
{
}
return rc;
}
/**
* Closes the currently opened cache image file in HDD container.
*
* @return VBox status code.
* @return VERR_VD_NOT_OPENED if no cache is opened in HDD container.
* @param pDisk Pointer to HDD container.
* @param fDelete If true, delete the image from the host disk.
*/
{
int rc = VINF_SUCCESS;
int rc2;
bool fLockWrite = false;
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
fLockWrite = true;
if (pCache->pszFilename)
} while (0);
if (RT_LIKELY(fLockWrite))
{
}
return rc;
}
/**
* Closes all opened image files in HDD container.
*
* @returns VBox status code.
* @param pDisk Pointer to HDD container.
*/
{
int rc = VINF_SUCCESS;
int rc2;
bool fLockWrite = false;
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Lock the entire operation. */
fLockWrite = true;
if (pCache)
{
if (pCache->pszFilename)
}
{
/* Remove image from list of opened images. */
/* Close image. */
/* Free remaining resources related to the image. */
}
} while (0);
if (RT_UNLIKELY(fLockWrite))
{
}
return rc;
}
/**
* Read data from virtual HDD.
*
* @returns VBox status code.
* @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
* @param pDisk Pointer to HDD container.
* @param uOffset Offset of first reading byte from start of disk.
* @param pvBuf Pointer to buffer for reading data.
* @param cbRead Number of bytes to read.
*/
{
int rc = VINF_SUCCESS;
int rc2;
bool fLockRead = false;
LogFlowFunc(("pDisk=%#p uOffset=%llu pvBuf=%p cbRead=%zu\n",
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
("pvBuf=%#p\n", pvBuf),
("cbRead=%zu\n", cbRead),
fLockRead = true;
("uOffset=%llu cbRead=%zu pDisk->cbSize=%llu\n",
true /* fZeroFreeBlocks */,
true /* fUpdateCache */);
} while (0);
if (RT_UNLIKELY(fLockRead))
{
}
return rc;
}
/**
* Write data to virtual HDD.
*
* @returns VBox status code.
* @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
* @param pDisk Pointer to HDD container.
* @param uOffset Offset of the first byte being
* written from start of disk.
* @param pvBuf Pointer to buffer for writing data.
* @param cbWrite Number of bytes to write.
*/
{
int rc = VINF_SUCCESS;
int rc2;
bool fLockWrite = false;
LogFlowFunc(("pDisk=%#p uOffset=%llu pvBuf=%p cbWrite=%zu\n",
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
("pvBuf=%#p\n", pvBuf),
("cbWrite=%zu\n", cbWrite),
fLockWrite = true;
("uOffset=%llu cbWrite=%zu pDisk->cbSize=%llu\n",
true /* fUpdateCache */);
if (RT_FAILURE(rc))
break;
/* If there is a merge (in the direction towards a parent) running
* concurrently then we have to also "relay" the write to this parent,
* as the merge position might be already past the position where
* this write is going. The "context" of the write can come from the
* natural chain, since merging either already did or will take care
* of the "other" content which is might be needed to fill the block
* to a full allocation size. The cache doesn't need to be touched
* as this write is covered by the previous one. */
} while (0);
if (RT_UNLIKELY(fLockWrite))
{
}
return rc;
}
/**
* Make sure the on disk representation of a virtual HDD is up to date.
*
* @returns VBox status code.
* @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
* @param pDisk Pointer to HDD container.
*/
{
int rc = VINF_SUCCESS;
int rc2;
bool fLockWrite = false;
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
fLockWrite = true;
if ( RT_SUCCESS(rc)
} while (0);
if (RT_UNLIKELY(fLockWrite))
{
}
return rc;
}
/**
* Get number of opened images in HDD container.
*
* @returns Number of opened images for HDD container. 0 if no images have been opened.
* @param pDisk Pointer to HDD container.
*/
{
unsigned cImages;
int rc2;
bool fLockRead = false;
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
fLockRead = true;
} while (0);
if (RT_UNLIKELY(fLockRead))
{
}
return cImages;
}
/**
*
* @returns Virtual disk ReadOnly status.
* @returns true if no image is opened in HDD container.
* @param pDisk Pointer to HDD container.
*/
{
bool fReadOnly;
int rc2;
bool fLockRead = false;
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
fLockRead = true;
unsigned uOpenFlags;
} while (0);
if (RT_UNLIKELY(fLockRead))
{
}
return fReadOnly;
}
/**
* Get total capacity of an image in HDD container.
*
* @returns Virtual disk size in bytes.
* @returns 0 if no image with specified number was not opened.
* @param pDisk Pointer to HDD container.
* @param nImage Image number, counts from 0. 0 is always base image of container.
*/
{
int rc2;
bool fLockRead = false;
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
fLockRead = true;
} while (0);
if (RT_UNLIKELY(fLockRead))
{
}
return cbSize;
}
/**
* Get total file size of an image in HDD container.
*
* @returns Virtual disk size in bytes.
* @returns 0 if no image is opened in HDD container.
* @param pDisk Pointer to HDD container.
* @param nImage Image number, counts from 0. 0 is always base image of container.
*/
{
int rc2;
bool fLockRead = false;
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
fLockRead = true;
} while (0);
if (RT_UNLIKELY(fLockRead))
{
}
return cbSize;
}
/**
* Get virtual disk PCHS geometry stored in HDD container.
*
* @returns VBox status code.
* @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
* @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
* @param pDisk Pointer to HDD container.
* @param nImage Image number, counts from 0. 0 is always base image of container.
* @param pPCHSGeometry Where to store PCHS geometry. Not NULL.
*/
{
int rc = VINF_SUCCESS;
int rc2;
bool fLockRead = false;
LogFlowFunc(("pDisk=%#p nImage=%u pPCHSGeometry=%#p\n",
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
("pPCHSGeometry=%#p\n", pPCHSGeometry),
fLockRead = true;
{
/* Use cached information if possible. */
else
}
else
} while (0);
if (RT_UNLIKELY(fLockRead))
{
}
return rc;
}
/**
* Store virtual disk PCHS geometry in HDD container.
*
* Note that in case of unrecoverable error all images in HDD container will be closed.
*
* @returns VBox status code.
* @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
* @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
* @param pDisk Pointer to HDD container.
* @param nImage Image number, counts from 0. 0 is always base image of container.
* @param pPCHSGeometry Where to load PCHS geometry from. Not NULL.
*/
{
int rc = VINF_SUCCESS;
int rc2;
bool fLockWrite = false;
LogFlowFunc(("pDisk=%#p nImage=%u pPCHSGeometry=%#p PCHS=%u/%u/%u\n",
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
("pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pPCHSGeometry,
fLockWrite = true;
{
{
/* Only update geometry if it is changed. Avoids similar checks
* in every backend. Most of the time the new geometry is set
* to the previous values, so no need to go through the hassle
* of updating an image which could be opened in read-only mode
* right now. */
/* Cache new geometry values in any case. */
&pDisk->PCHSGeometry);
if (RT_FAILURE(rc2))
{
}
else
{
/* Make sure the CHS geometry is properly clipped. */
}
}
}
else
{
&PCHS);
if ( RT_FAILURE(rc)
{
/* Only update geometry if it is changed. Avoids similar checks
* in every backend. Most of the time the new geometry is set
* to the previous values, so no need to go through the hassle
* of updating an image which could be opened in read-only mode
* right now. */
}
}
} while (0);
if (RT_UNLIKELY(fLockWrite))
{
}
return rc;
}
/**
* Get virtual disk LCHS geometry stored in HDD container.
*
* @returns VBox status code.
* @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
* @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
* @param pDisk Pointer to HDD container.
* @param nImage Image number, counts from 0. 0 is always base image of container.
* @param pLCHSGeometry Where to store LCHS geometry. Not NULL.
*/
{
int rc = VINF_SUCCESS;
int rc2;
bool fLockRead = false;
LogFlowFunc(("pDisk=%#p nImage=%u pLCHSGeometry=%#p\n",
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
("pLCHSGeometry=%#p\n", pLCHSGeometry),
fLockRead = true;
{
/* Use cached information if possible. */
else
}
else
} while (0);
if (RT_UNLIKELY(fLockRead))
{
}
return rc;
}
/**
* Store virtual disk LCHS geometry in HDD container.
*
* Note that in case of unrecoverable error all images in HDD container will be closed.
*
* @returns VBox status code.
* @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
* @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
* @param pDisk Pointer to HDD container.
* @param nImage Image number, counts from 0. 0 is always base image of container.
* @param pLCHSGeometry Where to load LCHS geometry from. Not NULL.
*/
{
int rc = VINF_SUCCESS;
int rc2;
bool fLockWrite = false;
LogFlowFunc(("pDisk=%#p nImage=%u pLCHSGeometry=%#p LCHS=%u/%u/%u\n",
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
("pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pLCHSGeometry,
fLockWrite = true;
{
{
/* Only update geometry if it is changed. Avoids similar checks
* in every backend. Most of the time the new geometry is set
* to the previous values, so no need to go through the hassle
* of updating an image which could be opened in read-only mode
* right now. */
/* Cache new geometry values in any case. */
&pDisk->LCHSGeometry);
if (RT_FAILURE(rc2))
{
}
else
{
/* Make sure the CHS geometry is properly clipped. */
}
}
}
else
{
&LCHS);
if ( RT_FAILURE(rc)
{
/* Only update geometry if it is changed. Avoids similar checks
* in every backend. Most of the time the new geometry is set
* to the previous values, so no need to go through the hassle
* of updating an image which could be opened in read-only mode
* right now. */
}
}
} while (0);
if (RT_UNLIKELY(fLockWrite))
{
}
return rc;
}
/**
* Get version of image in HDD container.
*
* @returns VBox status code.
* @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
* @param pDisk Pointer to HDD container.
* @param nImage Image number, counts from 0. 0 is always base image of container.
* @param puVersion Where to store the image version.
*/
unsigned *puVersion)
{
int rc = VINF_SUCCESS;
int rc2;
bool fLockRead = false;
LogFlowFunc(("pDisk=%#p nImage=%u puVersion=%#p\n",
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
("puVersion=%#p\n", puVersion),
fLockRead = true;
} while (0);
if (RT_UNLIKELY(fLockRead))
{
}
return rc;
}
/**
* List the capabilities of image backend in HDD container.
*
* @returns VBox status code.
* @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
* @param pDisk Pointer to the HDD container.
* @param nImage Image number, counts from 0. 0 is always base image of container.
* @param pbackendInfo Where to store the backend information.
*/
{
int rc = VINF_SUCCESS;
int rc2;
bool fLockRead = false;
LogFlowFunc(("pDisk=%#p nImage=%u pBackendInfo=%#p\n",
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
("pBackendInfo=%#p\n", pBackendInfo),
fLockRead = true;
} while (0);
if (RT_UNLIKELY(fLockRead))
{
}
return rc;
}
/**
* Get flags of image in HDD container.
*
* @returns VBox status code.
* @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
* @param pDisk Pointer to HDD container.
* @param nImage Image number, counts from 0. 0 is always base image of container.
* @param puImageFlags Where to store the image flags.
*/
unsigned *puImageFlags)
{
int rc = VINF_SUCCESS;
int rc2;
bool fLockRead = false;
LogFlowFunc(("pDisk=%#p nImage=%u puImageFlags=%#p\n",
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
("puImageFlags=%#p\n", puImageFlags),
fLockRead = true;
} while (0);
if (RT_UNLIKELY(fLockRead))
{
}
return rc;
}
/**
* Get open flags of image in HDD container.
*
* @returns VBox status code.
* @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
* @param pDisk Pointer to HDD container.
* @param nImage Image number, counts from 0. 0 is always base image of container.
* @param puOpenFlags Where to store the image open flags.
*/
unsigned *puOpenFlags)
{
int rc = VINF_SUCCESS;
int rc2;
bool fLockRead = false;
LogFlowFunc(("pDisk=%#p nImage=%u puOpenFlags=%#p\n",
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
("puOpenFlags=%#p\n", puOpenFlags),
fLockRead = true;
} while (0);
if (RT_UNLIKELY(fLockRead))
{
}
return rc;
}
/**
* Set open flags of image in HDD container.
* Note that in case of unrecoverable error all images in HDD container will be closed.
*
* @returns VBox status code.
* @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
* @param pDisk Pointer to HDD container.
* @param nImage Image number, counts from 0. 0 is always base image of container.
* @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
*/
unsigned uOpenFlags)
{
int rc;
int rc2;
bool fLockWrite = false;
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
("uOpenFlags=%#x\n", uOpenFlags),
fLockWrite = true;
} while (0);
if (RT_UNLIKELY(fLockWrite))
{
}
return rc;
}
/**
* Get base filename of image in HDD container. Some image formats use
* other filenames as well, so don't use this for anything but informational
* purposes.
*
* @returns VBox status code.
* @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
* @returns VERR_BUFFER_OVERFLOW if pszFilename buffer too small to hold filename.
* @param pDisk Pointer to HDD container.
* @param nImage Image number, counts from 0. 0 is always base image of container.
* @param pszFilename Where to store the image file name.
* @param cbFilename Size of buffer pszFilename points to.
*/
char *pszFilename, unsigned cbFilename)
{
int rc;
int rc2;
bool fLockRead = false;
LogFlowFunc(("pDisk=%#p nImage=%u pszFilename=%#p cbFilename=%u\n",
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
("cbFilename=%u\n", cbFilename),
fLockRead = true;
if (cb <= cbFilename)
{
rc = VINF_SUCCESS;
}
else
{
}
} while (0);
if (RT_UNLIKELY(fLockRead))
{
}
return rc;
}
/**
* Get the comment line of image in HDD container.
*
* @returns VBox status code.
* @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
* @returns VERR_BUFFER_OVERFLOW if pszComment buffer too small to hold comment text.
* @param pDisk Pointer to HDD container.
* @param nImage Image number, counts from 0. 0 is always base image of container.
* @param pszComment Where to store the comment string of image. NULL is ok.
* @param cbComment The size of pszComment buffer. 0 is ok.
*/
char *pszComment, unsigned cbComment)
{
int rc;
int rc2;
bool fLockRead = false;
LogFlowFunc(("pDisk=%#p nImage=%u pszComment=%#p cbComment=%u\n",
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
("cbComment=%u\n", cbComment),
fLockRead = true;
} while (0);
if (RT_UNLIKELY(fLockRead))
{
}
return rc;
}
/**
* Changes the comment line of image in HDD container.
*
* @returns VBox status code.
* @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
* @param pDisk Pointer to HDD container.
* @param nImage Image number, counts from 0. 0 is always base image of container.
* @param pszComment New comment string (UTF-8). NULL is allowed to reset the comment.
*/
const char *pszComment)
{
int rc;
int rc2;
bool fLockWrite = false;
LogFlowFunc(("pDisk=%#p nImage=%u pszComment=%#p \"%s\"\n",
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
fLockWrite = true;
} while (0);
if (RT_UNLIKELY(fLockWrite))
{
}
return rc;
}
/**
* Get UUID of image in HDD container.
*
* @returns VBox status code.
* @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
* @param pDisk Pointer to HDD container.
* @param nImage Image number, counts from 0. 0 is always base image of container.
* @param pUuid Where to store the image creation UUID.
*/
{
int rc;
int rc2;
bool fLockRead = false;
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
("pUuid=%#p\n", pUuid),
fLockRead = true;
} while (0);
if (RT_UNLIKELY(fLockRead))
{
}
return rc;
}
/**
* Set the image's UUID. Should not be used by normal applications.
*
* @returns VBox status code.
* @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
* @param pDisk Pointer to HDD container.
* @param nImage Image number, counts from 0. 0 is always base image of container.
* @param pUuid New UUID of the image. If NULL, a new UUID is created.
*/
{
int rc;
int rc2;
bool fLockWrite = false;
LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
("pUuid=%#p\n", pUuid),
fLockWrite = true;
if (!pUuid)
{
RTUuidCreate(&Uuid);
}
} while (0);
if (RT_UNLIKELY(fLockWrite))
{
}
return rc;
}
/**
* Get last modification UUID of image in HDD container.
*
* @returns VBox status code.
* @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
* @param pDisk Pointer to HDD container.
* @param nImage Image number, counts from 0. 0 is always base image of container.
* @param pUuid Where to store the image modification UUID.
*/
{
int rc = VINF_SUCCESS;
int rc2;
bool fLockRead = false;
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
("pUuid=%#p\n", pUuid),
fLockRead = true;
pUuid);
} while (0);
if (RT_UNLIKELY(fLockRead))
{
}
return rc;
}
/**
* Set the image's last modification UUID. Should not be used by normal applications.
*
* @returns VBox status code.
* @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
* @param pDisk Pointer to HDD container.
* @param nImage Image number, counts from 0. 0 is always base image of container.
* @param pUuid New modification UUID of the image. If NULL, a new UUID is created.
*/
{
int rc;
int rc2;
bool fLockWrite = false;
LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
("pUuid=%#p\n", pUuid),
fLockWrite = true;
if (!pUuid)
{
RTUuidCreate(&Uuid);
}
pUuid);
} while (0);
if (RT_UNLIKELY(fLockWrite))
{
}
return rc;
}
/**
* Get parent UUID of image in HDD container.
*
* @returns VBox status code.
* @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
* @param pDisk Pointer to HDD container.
* @param nImage Image number, counts from 0. 0 is always base image of container.
* @param pUuid Where to store the parent image UUID.
*/
{
int rc = VINF_SUCCESS;
int rc2;
bool fLockRead = false;
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
("pUuid=%#p\n", pUuid),
fLockRead = true;
} while (0);
if (RT_UNLIKELY(fLockRead))
{
}
return rc;
}
/**
* Set the image's parent UUID. Should not be used by normal applications.
*
* @returns VBox status code.
* @param pDisk Pointer to HDD container.
* @param nImage Image number, counts from 0. 0 is always base image of container.
* @param pUuid New parent UUID of the image. If NULL, a new UUID is created.
*/
{
int rc;
int rc2;
bool fLockWrite = false;
LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
("pUuid=%#p\n", pUuid),
fLockWrite = true;
if (!pUuid)
{
RTUuidCreate(&Uuid);
}
} while (0);
if (RT_UNLIKELY(fLockWrite))
{
}
return rc;
}
/**
* Debug helper - dumps all opened images in HDD container into the log file.
*
* @param pDisk Pointer to HDD container.
*/
{
int rc2;
bool fLockRead = false;
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
fLockRead = true;
{
}
} while (0);
if (RT_UNLIKELY(fLockRead))
{
}
}
{
int rc = VERR_VD_BLOCK_FREE;
int rc2;
bool fLockRead = false;
LogFlowFunc(("pDisk=%#p uOffset=%llu pcSgBuf=%#p cbRead=%zu pvUser1=%#p pvUser2=%#p\n",
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
("cbRead=%zu\n", cbRead),
("pcSgBuf=%#p\n", pcSgBuf),
fLockRead = true;
("uOffset=%llu cbRead=%zu pDisk->cbSize=%llu\n",
if (!pIoCtx)
{
rc = VERR_NO_MEMORY;
break;
}
if (rc == VINF_VD_ASYNC_IO_FINISHED)
{
else
}
} while (0);
|| rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
{
}
return rc;
}
{
int rc;
int rc2;
bool fLockWrite = false;
LogFlowFunc(("pDisk=%#p uOffset=%llu cSgBuf=%#p cbWrite=%zu pvUser1=%#p pvUser2=%#p\n",
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
("cbWrite=%zu\n", cbWrite),
("pcSgBuf=%#p\n", pcSgBuf),
fLockWrite = true;
("uOffset=%llu cbWrite=%zu pDisk->cbSize=%llu\n",
if (!pIoCtx)
{
rc = VERR_NO_MEMORY;
break;
}
if (rc == VINF_VD_ASYNC_IO_FINISHED)
{
else
}
} while (0);
|| rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
{
}
return rc;
}
{
int rc;
int rc2;
bool fLockWrite = false;
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
fLockWrite = true;
if (!pIoCtx)
{
rc = VERR_NO_MEMORY;
break;
}
if (rc == VINF_VD_ASYNC_IO_FINISHED)
{
else
}
} while (0);
|| rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
{
}
return rc;
}