VBoxHDD.cpp revision f687f34bd232be13744edbc0cc5155fa5d4540ed
/* $Id$ */
/** @file
* VBoxHDD - VBox HDD Container implementation.
*/
/*
* Copyright (C) 2006-2010 Sun Microsystems, Inc.
*
* 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.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_VD
#include <iprt/memcache.h>
#include <iprt/critsect.h>
#include <VBox/VBoxHDD-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 VDIASYNCIOSTORAGE
{
/** File handle. */
/** Completion callback. */
/** Thread for async access. */
/**
* 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 *pvBackendData;
/** 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. */
/** Per image I/O interface. */
/** Pointer to list of VD interfaces, per-image. */
/** Disk this image is part of */
/**
* uModified bit flags.
*/
#define VD_IMAGE_MODIFIED_FLAG RT_BIT(0)
/**
* VBox HDD Container main structure, private part.
*/
struct VBOXHDD
{
/** Structure signature (VBOXHDDDISK_SIGNATURE). */
/** Number of opened images. */
unsigned cImages;
/** Base image. */
/** Last opened image in the chain.
* The same as pBase if only one image is used. */
/** 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. */
/** I/O interface for the disk. */
/** I/O interface callback table for the images. */
/** Async I/O interface to the upper layer. */
/** Async I/O interface callback table. */
/** Fallback async I/O interface. */
/** Callback table for the fallback async 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 last image is currently written to and needs to grow.
* Other write requests which will grow the image too need to be deferred to
* prevent data corruption. - Protected by the critical section.
*/
volatile bool fGrowing;
/** List of waiting requests. - Protected by the critical section. */
};
/**
* 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
{
/** Node in the list of deferred requests. */
/** 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. */
/** S/G buffer */
/** Flag whether the I/O context is blocked because it is in the growing list. */
bool fBlocked;
/** 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 transfered from the parent if this context completes. */
/** Number of bytes to pre read */
/** Number of bytes to post read. */
/** 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. */
/** Number of bytes to wite left. */
} Optimized;
} Write;
} Child;
} Type;
} VDIOCTX;
/**
* I/O task.
*/
typedef struct VDIOTASK
{
/** Pointer to the I/O context the task belongs. */
/** Flag whether this is a meta data transfer. */
bool fMeta;
/** Type dependent data. */
union
{
/** User data transfer. */
struct
{
/** Number of bytes this task transfered. */
} User;
/** Meta data transfer. */
struct
{
/** Completion callback from the backend */
/** User data */
void *pvMetaUser;
/** Image the task was created for. */
} Meta;
} Type;
/**
* Storage handle.
*/
typedef struct VDIOSTORAGE
{
/** Image this storage handle belongs to. */
union
{
/** Storage handle */
void *pStorage;
/** File handle for the limited I/O version. */
} u;
} VDIOSTORAGE;
extern VBOXHDDBACKEND g_RawBackend;
extern VBOXHDDBACKEND g_VmdkBackend;
extern VBOXHDDBACKEND g_VDIBackend;
extern VBOXHDDBACKEND g_VhdBackend;
extern VBOXHDDBACKEND g_ParallelsBackend;
#ifdef VBOX_WITH_ISCSI
extern VBOXHDDBACKEND g_ISCSIBackend;
#endif
static unsigned g_cBackends = 0;
static PVBOXHDDBACKEND aStaticBackends[] =
{
#ifdef VBOX_WITH_ISCSI
#endif
};
/**
* 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: 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: 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: read the specified amount of data in whatever blocks the backend
* will give us.
*/
{
int rc;
/* 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;
/*
* 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)
{
rc = VINF_SUCCESS;
}
cbRead -= cbThisRead;
uOffset += cbThisRead;
return rc;
}
void *pvAllocation,
{
{
pIoCtx->cMetaTransfersPending = 0;
}
return pIoCtx;
}
void *pvAllocation,
{
{
}
return pIoCtx;
}
void *pvAllocation,
{
{
}
return pIoCtx;
}
{
if (pIoTask)
{
}
return pIoTask;
}
{
if (pIoTask)
{
}
return pIoTask;
}
{
if (pIoCtx->pvAllocation)
}
{
}
{
}
{
}
{
}
{
}
{
}
{
}
{
int rc = VINF_SUCCESS;
if ( !pIoCtx->cbTransferLeft
&& !pIoCtx->pfnIoCtxTransfer)
return VINF_VD_ASYNC_IO_FINISHED;
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->cMetaTransfersPending)
else if (RT_SUCCESS(rc))
LogFlowFunc(("pIoCtx=%#p rc=%Rrc cbTransferLeft=%u cMetaTransfersPending=%u fComplete=%RTbool\n",
return rc;
}
/**
* 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)
{
{
pIoCtx, &cbThisRead);
}
}
if (rc == VERR_VD_BLOCK_FREE)
{
/* No image in the chain contains the data for the block. */
rc = VINF_SUCCESS;
}
if (RT_FAILURE(rc))
break;
cbToRead -= cbThisRead;
uOffset += cbThisRead;
if (rc == VERR_VD_NOT_ENOUGH_METADATA)
{
/* Save the current state. */
}
return rc;
}
/**
* internal: parent image read wrapper for compacting.
*/
{
}
/**
* internal: mark the disk as not modified.
*/
{
{
/* generate new last-modified uuid */
{
RTUuidCreate(&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)
{
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 cannnot 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)
'\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 cannnot 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. */
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.
*/
{
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;
}
cbWrite -= cbThisWrite;
uOffset += cbThisWrite;
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 cannnot 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;
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)
/* 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. */
Assert(cbPostRead == 0);
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;
int rc;
if (cbPostRead)
{
/* Figure out how much we cannnot 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;
/* 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)
{
/*
* If there is a growing request already put this one onto the waiting list.
* It will be restarted if the current request completes.
*/
{
break;
}
else
{
/*
* Allocate segment and buffer in one go.
* A bit hackish but avoids the need to allocate memory twice.
*/
pTmp, 1,
pTmp,
if (!VALID_PTR(pIoCtxWrite))
{
rc = VERR_NO_MEMORY;
break;
}
/* Set the state to growing. */
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;
if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
{
/*
* 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;
return rc;
}
/**
* internal: scans plugin directory and loads the backends have been found.
*/
static int vdLoadDynamicBackends()
{
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. */
char *pszPluginFilter;
if (RT_FAILURE(rc))
{
rc = VERR_NO_MEMORY;
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. */
/* 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 (RT_FAILURE(rc))
{
rc = VERR_NO_MEMORY;
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;
}
/**
* VD async I/O interface open callback.
*/
void **ppStorage)
{
if (!pStorage)
return VERR_NO_MEMORY;
else
fOpen |= RTFILE_O_CREATE;
else
fOpen |= RTFILE_O_OPEN;
/* Open the file. */
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
return rc;
}
/**
* VD async I/O interface close callback.
*/
{
return VINF_SUCCESS;
}
/**
* 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;
}
{
int rc = VINF_SUCCESS;
LogFlowFunc(("Task completed pIoTask=%#p pIoCtx=%#p pDisk=%#p\n",
else
{
}
{
/* Continue the transfer */
if ( rc == VINF_VD_ASYNC_IO_FINISHED
{
if (pIoCtx->pIoCtxParent)
{
LogFlowFunc(("I/O context transfered %u bytes for the parent pIoCtxParent=%p\n",
/* Update the parent state. */
/*
* A completed child write means that we finsihed growing the image.
* We have to process any pending writes now.
*/
if ( rc == VINF_VD_ASYNC_IO_FINISHED
{
}
/* Process any pending writes. */
{
/* Process the list. */
do
{
pIoCtxWait->fBlocked = false;
if ( rc == VINF_VD_ASYNC_IO_FINISHED
{
pIoCtxWait->rcReq);
}
} while (!RTListIsEmpty(&ListTmp));
}
else
}
else
}
}
return VINF_SUCCESS;
}
/**
* VD I/O interface callback for opening a file.
*/
{
int rc = VINF_SUCCESS;
if (!pIoStorage)
return VERR_NO_MEMORY;
&pIoStorage->u.pStorage);
if (RT_SUCCESS(rc))
else
return rc;
}
{
pIoStorage->u.pStorage);
return VINF_SUCCESS;
}
{
pIoStorage->u.pStorage,
pcbSize);
}
{
pIoStorage->u.pStorage,
cbSize);
}
{
pIoStorage->u.pStorage,
}
{
pIoStorage->u.pStorage,
pcbRead);
}
{
pIoStorage->u.pStorage);
}
{
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;
#ifdef DEBUG
for (unsigned i = 0; i < cSegments; i++)
("Segment %u is invalid\n", i));
#endif
if (!pIoTask)
return VERR_NO_MEMORY;
void *pvTask;
pIoStorage->u.pStorage,
&pvTask);
if (rc2 == VINF_SUCCESS)
{
}
else if (rc2 == VERR_VD_ASYNC_IO_IN_PROGRESS)
rc = VINF_SUCCESS;
else if (RT_FAILURE(rc2))
{
break;
}
uOffset += cbTaskRead;
cbRead -= cbTaskRead;
}
return rc;
}
{
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;
#ifdef DEBUG
for (unsigned i = 0; i < cSegments; i++)
("Segment %u is invalid\n", i));
#endif
if (!pIoTask)
return VERR_NO_MEMORY;
void *pvTask;
pIoStorage->u.pStorage,
&pvTask);
if (rc2 == VINF_SUCCESS)
{
}
else if (rc2 == VERR_VD_ASYNC_IO_IN_PROGRESS)
rc = VINF_SUCCESS;
else if (RT_FAILURE(rc2))
{
break;
}
uOffset += cbTaskWrite;
cbWrite -= cbTaskWrite;
}
return rc;
}
void *pvMetaUser)
{
int rc = VINF_SUCCESS;
if (!pIoTask)
return VERR_NO_MEMORY;
pIoStorage->u.pStorage,
&pvTask);
if (rc2 == VINF_SUCCESS)
{
}
else if (rc2 == VERR_VD_ASYNC_IO_IN_PROGRESS)
else if (RT_FAILURE(rc2))
return rc;
}
void *pvMetaUser)
{
int rc = VINF_SUCCESS;
if (!pIoTask)
return VERR_NO_MEMORY;
pIoStorage->u.pStorage,
&pvTask);
if (rc2 == VINF_SUCCESS)
{
}
else if (rc2 == VERR_VD_ASYNC_IO_IN_PROGRESS)
rc = VINF_SUCCESS;
else if (RT_FAILURE(rc2))
return rc;
}
{
int rc = VINF_SUCCESS;
if (!pIoTask)
return VERR_NO_MEMORY;
pIoStorage->u.pStorage,
&pvTask);
if (rc2 == VINF_SUCCESS)
{
}
else if (rc2 == VERR_VD_ASYNC_IO_IN_PROGRESS)
rc = VINF_SUCCESS;
else if (RT_FAILURE(rc2))
return rc;
}
{
}
{
}
{
}
/**
* VD I/O interface callback for opening a file (limited version for VDGetFormat).
*/
{
int rc = VINF_SUCCESS;
if (!pIoStorage)
return VERR_NO_MEMORY;
else
fOpen |= RTFILE_O_CREATE;
else
fOpen |= RTFILE_O_OPEN;
if (RT_SUCCESS(rc))
else
return rc;
}
{
return VINF_SUCCESS;
}
{
}
{
}
{
}
{
}
{
}
/**
* internal: send output to the log (unconditionally).
*/
{
return VINF_SUCCESS;
}
/**
* Initializes HDD backends.
*
* @returns VBox status code.
*/
VBOXDDU_DECL(int) VDInit(void)
{
if (RT_SUCCESS(rc))
rc = vdLoadDynamicBackends();
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;
for (unsigned i = 0; i < cBackends; i++)
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 capablities of a backend indentified 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 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)
if (pDisk->pInterfaceAsyncIO)
else
{
/* Create fallback async I/O interface */
}
/* Create the I/O callback table. */
}
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 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),
if (!g_apBackends)
VDInit();
/* 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))
{
/* 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;
}
/* Set up the I/O interface. */
&pImage->pvBackendData);
/* 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->pvBackendData);
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
}
}
/* 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;
}
/**
* 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. */
if (RT_FAILURE(rc))
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->pvBackendData);
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, 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 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 ParentUuid=%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. */
("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;
}
/* Set up the 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->pvBackendData);
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);
if (RT_SUCCESS(rc2))
}
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;
}
/**
* 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. */
fLockRead = 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. */
fLockRead = 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;
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 the
*
* This is done before we do the actual merge to prevent an incosistent
* chain if the mode change fails for some reason.
*/
if (pImageFrom->pNext)
{
if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
{
if (RT_FAILURE(rc))
break;
}
}
/* 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;
if (RT_FAILURE(rc))
break;
}
else
rc = VINF_SUCCESS;
fLockWrite = true;
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;
}
}
/* 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 neccessary.
*/
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 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 pVDIfsOperation=%#p pDstVDIfsImage=%#p pDstVDIfsOperation=%#p\n",
pDiskFrom, nImage, pDiskTo, pszBackend, pszFilename, fMoveByRename, cbSize, pVDIfsOperation, pDstVDIfsImage, pDstVDIfsOperation));
if (pIfProgress)
if (pDstIfProgress)
do {
/* Check arguments. */
fLockReadFrom = true;
/* Move the image. */
{
/* Rename only works when backends are the same. */
if ( fMoveByRename
{
fLockReadFrom = false;
fLockWriteFrom = true;
rc = pImageFrom->Backend->pfnRename(pImageFrom->pvBackendData, 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;
}
PDMMEDIAGEOMETRY PCHSGeometryFrom = {0, 0, 0};
PDMMEDIAGEOMETRY LCHSGeometryFrom = {0, 0, 0};
{
if (pDstUuid)
else
}
else
{
if (RT_FAILURE(rc))
}
rc = pImageFrom->Backend->pfnGetModificationUuid(pImageFrom->pvBackendData, &ImageModificationUuid);
if (RT_FAILURE(rc))
if (RT_FAILURE(rc))
rc = pImageFrom->Backend->pfnGetParentModificationUuid(pImageFrom->pvBackendData, &ParentModificationUuid);
if (RT_FAILURE(rc))
char szComment[1024];
if (RT_FAILURE(rc))
szComment[0] = '\0';
else
unsigned uOpenFlagsFrom;
fLockReadFrom = false;
if (pszFilename)
{
if (cbSize == 0)
cbSize = cbSizeFrom;
/* Create destination image with the properties of the 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 (uImageFlags & VD_IMAGE_FLAGS_DIFF)
{
fLockWriteTo = true;
} else {
/** @todo hack to force creation of a fixed image for
* the RAW backend, which can't handle anything else. */
/* 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. */
/** @todo double-check if the VHD backend correctly handles
* PCHS and LCHS geometry. also reconsider our current paranoia
* level when it comes to geometry settings here and in the
* backends. */
{
}
fLockWriteTo = true;
}
if (RT_FAILURE(rc))
break;
}
else
{
if (cbSizeTo == 0)
{
break;
}
if (cbSize == 0)
}
fLockWriteTo = false;
/* Allocate tmp buffer. */
if (!pvBuf)
{
rc = VERR_NO_MEMORY;
break;
}
/* 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;
if (RT_FAILURE(rc))
break;
fLockReadFrom = false;
fLockWriteTo = true;
if (RT_FAILURE(rc))
break;
fLockWriteTo = 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;
}
{
/** @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))
/** @todo double-check this - it makes little sense to copy over the parent modification uuid,
* as the destination image can have a totally different parent. */
#if 0
#endif
}
} 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;
}
/**
* 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 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;
{
/* 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",
} 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",
} 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;
} 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, counds 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));
int (*pfnMessage)(void *, const char *, ...) = NULL;
else
{
}
fLockRead = true;
{
}
} while (0);
if (RT_UNLIKELY(fLockRead))
{
}
}
/**
* Query if asynchronous operations are supported for this disk.
*
* @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 pfAIOSupported Where to store if async IO is supported.
*/
{
int rc = VINF_SUCCESS;
int rc2;
bool fLockRead = false;
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
/* Check arguments. */
("pfAIOSupported=%#p\n", pfAIOSupported),
fLockRead = true;
else
*pfAIOSupported = false;
} while (0);
if (RT_UNLIKELY(fLockRead))
{
}
return rc;
}
{
int rc = VERR_VD_BLOCK_FREE;
int rc2;
bool fLockRead = false;
LogFlowFunc(("pDisk=%#p uOffset=%llu paSeg=%p cSeg=%u 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),
("paSeg=%#p\n", paSeg),
("cSeg=%zu\n", cSeg),
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);
{
}
return rc;
}
{
int rc;
int rc2;
bool fLockWrite = false;
LogFlowFunc(("pDisk=%#p uOffset=%llu paSeg=%p cSeg=%u 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),
("paSeg=%#p\n", paSeg),
("cSeg=%zu\n", cSeg),
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);
{
}
return rc;
}
{
int rc;
int rc2;
bool fLockWrite = false;
do
{
/* sanity check */
AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
fLockWrite = true;
0, NULL, 0,
if (!pIoCtx)
{
rc = VERR_NO_MEMORY;
break;
}
} while (0);
{
}
if (RT_SUCCESS(rc))
{
if ( !pIoCtx->cbTransferLeft
{
}
else
{
LogFlow(("cbTransferLeft=%u cMetaTransfersPending=%u fComplete=%RTbool\n",
}
}
return rc;
}
#if 0
/** @copydoc VBOXHDDBACKEND::pfnComposeLocation */
{
return NULL;
}
/** @copydoc VBOXHDDBACKEND::pfnComposeName */
{
return NULL;
}
#endif