Index: /trunk/src/VBox/Storage/QED.cpp
===================================================================
--- /trunk/src/VBox/Storage/QED.cpp	(revision 63791)
+++ /trunk/src/VBox/Storage/QED.cpp	(revision 63792)
@@ -853,39 +853,4 @@
 
 /**
- * Flush image data to disk - version for async I/O.
- *
- * @returns VBox status code.
- * @param   pImage    The image instance data.
- * @param   pIoCtx    The I/o context
- */
-static int qedFlushImageAsync(PQEDIMAGE pImage, PVDIOCTX pIoCtx)
-{
-    int rc = VINF_SUCCESS;
-
-    if (   pImage->pStorage
-        && !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
-    {
-        QedHeader Header;
-
-        Assert(!(pImage->cbTable % pImage->cbCluster));
-        rc = qedTblWrite(pImage, pIoCtx, pImage->offL1Table, pImage->paL1Table,
-                         NULL, NULL);
-        if (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
-        {
-            /* Write header. */
-            qedHdrConvertFromHostEndianess(pImage, &Header);
-            rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
-                                        0, &Header, sizeof(Header),
-                                        pIoCtx, NULL, NULL);
-            if (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
-                rc = vdIfIoIntFileFlush(pImage->pIfIo, pImage->pStorage,
-                                        pIoCtx, NULL, NULL);
-        }
-    }
-
-    return rc;
-}
-
-/**
  * Checks whether the given cluster offset is valid.
  *
@@ -1151,6 +1116,4 @@
 static int qedOpenImage(PQEDIMAGE pImage, unsigned uOpenFlags)
 {
-    int rc;
-
     pImage->uOpenFlags = uOpenFlags;
 
@@ -1163,119 +1126,112 @@
      * even if opening the image file fails.
      */
-    rc = qedL2TblCacheCreate(pImage);
-    if (RT_FAILURE(rc))
-    {
+    int rc = qedL2TblCacheCreate(pImage);
+    if (RT_SUCCESS(rc))
+    {
+        /* Open the image. */
+        rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename,
+                               VDOpenFlagsToFileOpenFlags(uOpenFlags,
+                                                          false /* fCreate */),
+                               &pImage->pStorage);
+        if (RT_SUCCESS(rc))
+        {
+            uint64_t cbFile;
+            rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
+            if (   RT_SUCCESS(rc)
+                && cbFile > sizeof(QedHeader))
+            {
+                QedHeader Header;
+
+                rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, 0, &Header, sizeof(Header));
+                if (   RT_SUCCESS(rc)
+                    && qedHdrConvertToHostEndianess(&Header))
+                {
+                    if (   !(Header.u64FeatureFlags & ~QED_FEATURE_MASK)
+                        && !(Header.u64FeatureFlags & QED_FEATURE_BACKING_FILE_NO_PROBE))
+                    {
+                        if (Header.u64FeatureFlags & QED_FEATURE_NEED_CHECK)
+                        {
+                            /* Image needs checking. */
+                            if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
+                                rc = qedCheckImage(pImage, &Header);
+                            else
+                                rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
+                                               N_("Qed: Image '%s' needs checking but is opened readonly"),
+                                               pImage->pszFilename);
+                        }
+
+                        if (   RT_SUCCESS(rc)
+                            && (Header.u64FeatureFlags & QED_FEATURE_BACKING_FILE))
+                        {
+                            /* Load backing filename from image. */
+                            pImage->pszBackingFilename = (char *)RTMemAllocZ(Header.u32BackingFilenameSize + 1); /* +1 for \0 terminator. */
+                            if (pImage->pszBackingFilename)
+                            {
+                                pImage->cbBackingFilename  = Header.u32BackingFilenameSize;
+                                pImage->offBackingFilename = Header.u32OffBackingFilename;
+                                rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
+                                                           Header.u32OffBackingFilename, pImage->pszBackingFilename,
+                                                           Header.u32BackingFilenameSize);
+                            }
+                            else
+                                rc = VERR_NO_MEMORY;
+                        }
+
+                        if (RT_SUCCESS(rc))
+                        {
+                            pImage->cbImage       = cbFile;
+                            pImage->cbCluster     = Header.u32ClusterSize;
+                            pImage->cbTable       = Header.u32TableSize * pImage->cbCluster;
+                            pImage->cTableEntries = pImage->cbTable / sizeof(uint64_t);
+                            pImage->offL1Table    = Header.u64OffL1Table;
+                            pImage->cbSize        = Header.u64Size;
+                            qedTableMasksInit(pImage);
+
+                            /* Allocate L1 table. */
+                            pImage->paL1Table     = (uint64_t *)RTMemAllocZ(pImage->cbTable);
+                            if (pImage->paL1Table)
+                            {
+                                /* Read from the image. */
+                                rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
+                                                           pImage->offL1Table, pImage->paL1Table,
+                                                           pImage->cbTable);
+                                if (RT_SUCCESS(rc))
+                                {
+                                    qedTableConvertToHostEndianess(pImage->paL1Table, pImage->cTableEntries);
+
+                                    /* If the consistency check succeeded, clear the flag by flushing the image. */
+                                    if (Header.u64FeatureFlags & QED_FEATURE_NEED_CHECK)
+                                        rc = qedFlushImage(pImage);
+                                }
+                                else
+                                    rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
+                                                   N_("Qed: Reading the L1 table for image '%s' failed"),
+                                                   pImage->pszFilename);
+                            }
+                            else
+                                rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
+                                               N_("Qed: Out of memory allocating L1 table for image '%s'"),
+                                               pImage->pszFilename);
+                        }
+                    }
+                    else
+                        rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
+                                       N_("Qed: The image '%s' makes use of unsupported features"),
+                                       pImage->pszFilename);
+                }
+                else if (RT_SUCCESS(rc))
+                    rc = VERR_VD_GEN_INVALID_HEADER;
+            }
+            else if (RT_SUCCESS(rc))
+                rc = VERR_VD_GEN_INVALID_HEADER;
+        }
+        /* else: Do NOT signal an appropriate error here, as the VD layer has the
+         *       choice of retrying the open if it failed. */
+    }
+    else
         rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
                        N_("Qed: Creating the L2 table cache for image '%s' failed"),
                        pImage->pszFilename);
 
-        goto out;
-    }
-
-    /*
-     * Open the image.
-     */
-    rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename,
-                           VDOpenFlagsToFileOpenFlags(uOpenFlags,
-                                                      false /* fCreate */),
-                           &pImage->pStorage);
-    if (RT_FAILURE(rc))
-    {
-        /* Do NOT signal an appropriate error here, as the VD layer has the
-         * choice of retrying the open if it failed. */
-        goto out;
-    }
-
-    uint64_t cbFile;
-    QedHeader Header;
-    rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
-    if (RT_FAILURE(rc))
-        goto out;
-    if (cbFile > sizeof(Header))
-    {
-        rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, 0, &Header, sizeof(Header));
-        if (   RT_SUCCESS(rc)
-            && qedHdrConvertToHostEndianess(&Header))
-        {
-            if (   !(Header.u64FeatureFlags & ~QED_FEATURE_MASK)
-                && !(Header.u64FeatureFlags & QED_FEATURE_BACKING_FILE_NO_PROBE))
-            {
-                if (Header.u64FeatureFlags & QED_FEATURE_NEED_CHECK)
-                {
-                    /* Image needs checking. */
-                    if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
-                        rc = qedCheckImage(pImage, &Header);
-                    else
-                        rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
-                                       N_("Qed: Image '%s' needs checking but is opened readonly"),
-                                       pImage->pszFilename);
-                }
-
-                if (   RT_SUCCESS(rc)
-                    && (Header.u64FeatureFlags & QED_FEATURE_BACKING_FILE))
-                {
-                    /* Load backing filename from image. */
-                    pImage->pszBackingFilename = (char *)RTMemAllocZ(Header.u32BackingFilenameSize + 1); /* +1 for \0 terminator. */
-                    if (pImage->pszBackingFilename)
-                    {
-                        pImage->cbBackingFilename  = Header.u32BackingFilenameSize;
-                        pImage->offBackingFilename = Header.u32OffBackingFilename;
-                        rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
-                                                   Header.u32OffBackingFilename, pImage->pszBackingFilename,
-                                                   Header.u32BackingFilenameSize);
-                    }
-                    else
-                        rc = VERR_NO_MEMORY;
-                }
-
-                if (RT_SUCCESS(rc))
-                {
-                    pImage->cbImage       = cbFile;
-                    pImage->cbCluster     = Header.u32ClusterSize;
-                    pImage->cbTable       = Header.u32TableSize * pImage->cbCluster;
-                    pImage->cTableEntries = pImage->cbTable / sizeof(uint64_t);
-                    pImage->offL1Table    = Header.u64OffL1Table;
-                    pImage->cbSize        = Header.u64Size;
-                    qedTableMasksInit(pImage);
-
-                    /* Allocate L1 table. */
-                    pImage->paL1Table     = (uint64_t *)RTMemAllocZ(pImage->cbTable);
-                    if (pImage->paL1Table)
-                    {
-                        /* Read from the image. */
-                        rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
-                                                   pImage->offL1Table, pImage->paL1Table,
-                                                   pImage->cbTable);
-                        if (RT_SUCCESS(rc))
-                        {
-                            qedTableConvertToHostEndianess(pImage->paL1Table, pImage->cTableEntries);
-
-                            /* If the consistency check succeeded, clear the flag by flushing the image. */
-                            if (Header.u64FeatureFlags & QED_FEATURE_NEED_CHECK)
-                                rc = qedFlushImage(pImage);
-                        }
-                        else
-                            rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
-                                           N_("Qed: Reading the L1 table for image '%s' failed"),
-                                           pImage->pszFilename);
-                    }
-                    else
-                        rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
-                                       N_("Qed: Out of memory allocating L1 table for image '%s'"),
-                                       pImage->pszFilename);
-                }
-            }
-            else
-                rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
-                               N_("Qed: The image '%s' makes use of unsupported features"),
-                               pImage->pszFilename);
-        }
-        else if (RT_SUCCESS(rc))
-            rc = VERR_VD_GEN_INVALID_HEADER;
-    }
-    else
-        rc = VERR_VD_GEN_INVALID_HEADER;
-
-out:
     if (RT_FAILURE(rc))
         qedFreeImage(pImage, false);
@@ -1290,74 +1246,66 @@
                           PCVDGEOMETRY pPCHSGeometry,
                           PCVDGEOMETRY pLCHSGeometry, unsigned uOpenFlags,
-                          PFNVDPROGRESS pfnProgress, void *pvUser,
+                          PVDINTERFACEPROGRESS pIfProgress,
                           unsigned uPercentStart, unsigned uPercentSpan)
 {
     RT_NOREF1(pszComment);
     int rc;
-    int32_t fOpen;
-
-    if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
-    {
+
+    if (!(uImageFlags & VD_IMAGE_FLAGS_FIXED))
+    {
+        rc = qedL2TblCacheCreate(pImage);
+        if (RT_SUCCESS(rc))
+        {
+            pImage->uOpenFlags   = uOpenFlags & ~VD_OPEN_FLAGS_READONLY;
+            pImage->uImageFlags  = uImageFlags;
+            pImage->PCHSGeometry = *pPCHSGeometry;
+            pImage->LCHSGeometry = *pLCHSGeometry;
+
+            pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
+            pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
+            AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
+
+            /* Create image file. */
+            uint32_t fOpen = VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags, true /* fCreate */);
+            rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename, fOpen, &pImage->pStorage);
+            if (RT_SUCCESS(rc))
+            {
+                /* Init image state. */
+                pImage->cbSize             = cbSize;
+                pImage->cbCluster          = QED_CLUSTER_SIZE_DEFAULT;
+                pImage->cbTable            = qedCluster2Byte(pImage, QED_TABLE_SIZE_DEFAULT);
+                pImage->cTableEntries      = pImage->cbTable / sizeof(uint64_t);
+                pImage->offL1Table         = qedCluster2Byte(pImage, 1); /* Cluster 0 is the header. */
+                pImage->cbImage            = (1 * pImage->cbCluster) + pImage->cbTable; /* Header + L1 table size. */
+                pImage->cbBackingFilename  = 0;
+                pImage->offBackingFilename = 0;
+                qedTableMasksInit(pImage);
+
+                /* Init L1 table. */
+                pImage->paL1Table = (uint64_t *)RTMemAllocZ(pImage->cbTable);
+                if (RT_LIKELY(pImage->paL1Table))
+                {
+                    vdIfProgress(pIfProgress, uPercentStart + uPercentSpan * 98 / 100);
+                    rc = qedFlushImage(pImage);
+                }
+                else
+                    rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS, N_("Qed: cannot allocate memory for L1 table of image '%s'"),
+                                   pImage->pszFilename);
+            }
+            else
+                rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("Qed: cannot create image '%s'"), pImage->pszFilename);
+        }
+        else
+            rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("Qed: Failed to create L2 cache for image '%s'"),
+                           pImage->pszFilename);
+    }
+    else
         rc = vdIfError(pImage->pIfError, VERR_VD_INVALID_TYPE, RT_SRC_POS, N_("Qed: cannot create fixed image '%s'"), pImage->pszFilename);
-        goto out;
-    }
-
-    pImage->uOpenFlags   = uOpenFlags & ~VD_OPEN_FLAGS_READONLY;
-    pImage->uImageFlags  = uImageFlags;
-    pImage->PCHSGeometry = *pPCHSGeometry;
-    pImage->LCHSGeometry = *pLCHSGeometry;
-
-    pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
-    pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
-    AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
-
-    /* Create image file. */
-    fOpen = VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags, true /* fCreate */);
-    rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename, fOpen, &pImage->pStorage);
-    if (RT_FAILURE(rc))
-    {
-        rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("Qed: cannot create image '%s'"), pImage->pszFilename);
-        goto out;
-    }
-
-    /* Init image state. */
-    pImage->cbSize             = cbSize;
-    pImage->cbCluster          = QED_CLUSTER_SIZE_DEFAULT;
-    pImage->cbTable            = qedCluster2Byte(pImage, QED_TABLE_SIZE_DEFAULT);
-    pImage->cTableEntries      = pImage->cbTable / sizeof(uint64_t);
-    pImage->offL1Table         = qedCluster2Byte(pImage, 1); /* Cluster 0 is the header. */
-    pImage->cbImage            = (1 * pImage->cbCluster) + pImage->cbTable; /* Header + L1 table size. */
-    pImage->cbBackingFilename  = 0;
-    pImage->offBackingFilename = 0;
-    qedTableMasksInit(pImage);
-
-    /* Init L1 table. */
-    pImage->paL1Table     = (uint64_t *)RTMemAllocZ(pImage->cbTable);
-    if (!pImage->paL1Table)
-    {
-        rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS, N_("Qed: cannot allocate memory for L1 table of image '%s'"),
-                       pImage->pszFilename);
-        goto out;
-    }
-
-    rc = qedL2TblCacheCreate(pImage);
-    if (RT_FAILURE(rc))
-    {
-        rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("Qed: Failed to create L2 cache for image '%s'"),
-                       pImage->pszFilename);
-        goto out;
-    }
-
-    if (RT_SUCCESS(rc) && pfnProgress)
-        pfnProgress(pvUser, uPercentStart + uPercentSpan * 98 / 100);
-
-    rc = qedFlushImage(pImage);
-
-out:
-    if (RT_SUCCESS(rc) && pfnProgress)
-        pfnProgress(pvUser, uPercentStart + uPercentSpan);
-
-    if (RT_FAILURE(rc))
+
+    if (RT_SUCCESS(rc))
+        vdIfProgress(pIfProgress, uPercentStart + uPercentSpan);
+    else
         qedFreeImage(pImage, rc != VERR_ALREADY_EXISTS);
+
     return rc;
 }
@@ -1514,5 +1462,4 @@
     LogFlowFunc(("pszFilename=\"%s\" pVDIfsDisk=%#p pVDIfsImage=%#p\n", pszFilename, pVDIfsDisk, pVDIfsImage));
     PVDIOSTORAGE pStorage = NULL;
-    uint64_t cbFile;
     int rc = VINF_SUCCESS;
 
@@ -1520,11 +1467,5 @@
     PVDINTERFACEIOINT pIfIo = VDIfIoIntGet(pVDIfsImage);
     AssertPtrReturn(pIfIo, VERR_INVALID_PARAMETER);
-
-    if (   !VALID_PTR(pszFilename)
-        || !*pszFilename)
-    {
-        rc = VERR_INVALID_PARAMETER;
-        goto out;
-    }
+    AssertReturn((VALID_PTR(pszFilename) && *pszFilename), VERR_INVALID_PARAMETER);
 
     /*
@@ -1537,4 +1478,6 @@
     if (RT_SUCCESS(rc))
     {
+        uint64_t cbFile;
+
         rc = vdIfIoIntFileGetSize(pIfIo, pStorage, &cbFile);
         if (   RT_SUCCESS(rc)
@@ -1546,8 +1489,5 @@
             if (   RT_SUCCESS(rc)
                 && qedHdrConvertToHostEndianess(&Header))
-            {
                 *penmType = VDTYPE_HDD;
-                rc = VINF_SUCCESS;
-            }
             else
                 rc = VERR_VD_GEN_INVALID_HEADER;
@@ -1560,5 +1500,4 @@
         vdIfIoIntFileClose(pIfIo, pStorage);
 
-out:
     LogFlowFunc(("returns %Rrc\n", rc));
     return rc;
@@ -1570,44 +1509,31 @@
                                  VDTYPE enmType, void **ppBackendData)
 {
-    LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p enmType=%u ppBackendData=%#p\n", pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, enmType, ppBackendData));
+    RT_NOREF1(enmType); /**< @todo r=klaus make use of the type info. */
+
+    LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p enmType=%u ppBackendData=%#p\n",
+                 pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, enmType, ppBackendData));
     int rc;
-    PQEDIMAGE pImage;
-
-    NOREF(enmType); /**< @todo r=klaus make use of the type info. */
 
     /* Check open flags. All valid flags are supported. */
-    if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
-    {
-        rc = VERR_INVALID_PARAMETER;
-        goto out;
-    }
-
-    /* Check remaining arguments. */
-    if (   !VALID_PTR(pszFilename)
-        || !*pszFilename)
-    {
-        rc = VERR_INVALID_PARAMETER;
-        goto out;
-    }
-
-
-    pImage = (PQEDIMAGE)RTMemAllocZ(sizeof(QEDIMAGE));
-    if (!pImage)
-    {
+    AssertReturn(!(uOpenFlags & ~VD_OPEN_FLAGS_MASK), VERR_INVALID_PARAMETER);
+    AssertReturn((VALID_PTR(pszFilename) && *pszFilename), VERR_INVALID_PARAMETER);
+
+    PQEDIMAGE pImage = (PQEDIMAGE)RTMemAllocZ(sizeof(QEDIMAGE));
+    if (RT_LIKELY(pImage))
+    {
+        pImage->pszFilename = pszFilename;
+        pImage->pStorage = NULL;
+        pImage->pVDIfsDisk = pVDIfsDisk;
+        pImage->pVDIfsImage = pVDIfsImage;
+
+        rc = qedOpenImage(pImage, uOpenFlags);
+        if (RT_SUCCESS(rc))
+            *ppBackendData = pImage;
+        else
+            RTMemFree(pImage);
+    }
+    else
         rc = VERR_NO_MEMORY;
-        goto out;
-    }
-    pImage->pszFilename = pszFilename;
-    pImage->pStorage = NULL;
-    pImage->pVDIfsDisk = pVDIfsDisk;
-    pImage->pVDIfsImage = pVDIfsImage;
-
-    rc = qedOpenImage(pImage, uOpenFlags);
-    if (RT_SUCCESS(rc))
-        *ppBackendData = pImage;
-    else
-        RTMemFree(pImage);
-
-out:
+
     LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
     return rc;
@@ -1628,73 +1554,49 @@
                  pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, enmType, ppBackendData));
     int rc;
-    PQEDIMAGE pImage;
-
-    PFNVDPROGRESS pfnProgress = NULL;
-    void *pvUser = NULL;
-    PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
-    if (pIfProgress)
-    {
-        pfnProgress = pIfProgress->pfnProgress;
-        pvUser = pIfProgress->Core.pvUser;
-    }
 
     /* Check the VD container type. */
     if (enmType != VDTYPE_HDD)
-    {
-        rc = VERR_VD_INVALID_TYPE;
-        goto out;
-    }
+        return VERR_VD_INVALID_TYPE;
 
     /* Check open flags. All valid flags are supported. */
-    if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
-    {
-        rc = VERR_INVALID_PARAMETER;
-        goto out;
-    }
-
-    /* Check remaining arguments. */
-    if (   !VALID_PTR(pszFilename)
-        || !*pszFilename
-        || !VALID_PTR(pPCHSGeometry)
-        || !VALID_PTR(pLCHSGeometry))
-    {
-        rc = VERR_INVALID_PARAMETER;
-        goto out;
-    }
-
-    pImage = (PQEDIMAGE)RTMemAllocZ(sizeof(QEDIMAGE));
-    if (!pImage)
-    {
+    AssertReturn(!(uOpenFlags & ~VD_OPEN_FLAGS_MASK), VERR_INVALID_PARAMETER);
+    AssertReturn(   VALID_PTR(pszFilename)
+                 && *pszFilename
+                 && VALID_PTR(pPCHSGeometry)
+                 && VALID_PTR(pLCHSGeometry), VERR_INVALID_PARAMETER);
+
+    PQEDIMAGE pImage = (PQEDIMAGE)RTMemAllocZ(sizeof(QEDIMAGE));
+    if (RT_LIKELY(pImage))
+    {
+        PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
+
+        pImage->pszFilename = pszFilename;
+        pImage->pStorage = NULL;
+        pImage->pVDIfsDisk = pVDIfsDisk;
+        pImage->pVDIfsImage = pVDIfsImage;
+
+        rc = qedCreateImage(pImage, cbSize, uImageFlags, pszComment,
+                            pPCHSGeometry, pLCHSGeometry, uOpenFlags,
+                            pIfProgress, uPercentStart, uPercentSpan);
+        if (RT_SUCCESS(rc))
+        {
+            /* So far the image is opened in read/write mode. Make sure the
+             * image is opened in read-only mode if the caller requested that. */
+            if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
+            {
+                qedFreeImage(pImage, false);
+                rc = qedOpenImage(pImage, uOpenFlags);
+            }
+
+            if (RT_SUCCESS(rc))
+                *ppBackendData = pImage;
+        }
+
+        if RT_FAILURE(rc)
+            RTMemFree(pImage);
+    }
+    else
         rc = VERR_NO_MEMORY;
-        goto out;
-    }
-    pImage->pszFilename = pszFilename;
-    pImage->pStorage = NULL;
-    pImage->pVDIfsDisk = pVDIfsDisk;
-    pImage->pVDIfsImage = pVDIfsImage;
-
-    rc = qedCreateImage(pImage, cbSize, uImageFlags, pszComment,
-                        pPCHSGeometry, pLCHSGeometry, uOpenFlags,
-                        pfnProgress, pvUser, uPercentStart, uPercentSpan);
-    if (RT_SUCCESS(rc))
-    {
-        /* So far the image is opened in read/write mode. Make sure the
-         * image is opened in read-only mode if the caller requested that. */
-        if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
-        {
-            qedFreeImage(pImage, false);
-            rc = qedOpenImage(pImage, uOpenFlags);
-            if (RT_FAILURE(rc))
-            {
-                RTMemFree(pImage);
-                goto out;
-            }
-        }
-        *ppBackendData = pImage;
-    }
-    else
-        RTMemFree(pImage);
-
-out:
+
     LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
     return rc;
@@ -1709,38 +1611,29 @@
 
     /* Check arguments. */
-    if (   !pImage
-        || !pszFilename
-        || !*pszFilename)
-    {
-        rc = VERR_INVALID_PARAMETER;
-        goto out;
-    }
+    AssertReturn((pImage && pszFilename && *pszFilename), VERR_INVALID_PARAMETER);
 
     /* Close the image. */
     rc = qedFreeImage(pImage, false);
-    if (RT_FAILURE(rc))
-        goto out;
-
-    /* Rename the file. */
-    rc = vdIfIoIntFileMove(pImage->pIfIo, pImage->pszFilename, pszFilename, 0);
-    if (RT_FAILURE(rc))
-    {
-        /* The move failed, try to reopen the original image. */
-        int rc2 = qedOpenImage(pImage, pImage->uOpenFlags);
-        if (RT_FAILURE(rc2))
-            rc = rc2;
-
-        goto out;
-    }
-
-    /* Update pImage with the new information. */
-    pImage->pszFilename = pszFilename;
-
-    /* Open the old image with new name. */
-    rc = qedOpenImage(pImage, pImage->uOpenFlags);
-    if (RT_FAILURE(rc))
-        goto out;
-
-out:
+    if (RT_SUCCESS(rc))
+    {
+        /* Rename the file. */
+        rc = vdIfIoIntFileMove(pImage->pIfIo, pImage->pszFilename, pszFilename, 0);
+        if (RT_SUCCESS(rc))
+        {
+            /* Update pImage with the new information. */
+            pImage->pszFilename = pszFilename;
+
+            /* Open the old image with new name. */
+            rc = qedOpenImage(pImage, pImage->uOpenFlags);
+        }
+        else
+        {
+            /* The move failed, try to reopen the original image. */
+            int rc2 = qedOpenImage(pImage, pImage->uOpenFlags);
+            if (RT_FAILURE(rc2))
+                rc = rc2;
+        }
+    }
+
     LogFlowFunc(("returns %Rrc\n", rc));
     return rc;
@@ -1752,7 +1645,6 @@
     LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
     PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
-    int rc;
-
-    rc = qedFreeImage(pImage, fDelete);
+
+    int rc = qedFreeImage(pImage, fDelete);
     RTMemFree(pImage);
 
@@ -1761,4 +1653,5 @@
 }
 
+/** @copydoc VDIMAGEBACKEND::pfnWrite */
 static DECLCALLBACK(int) qedRead(void *pBackendData, uint64_t uOffset, size_t cbToRead,
                                  PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
@@ -1771,22 +1664,10 @@
     uint32_t idxL2      = 0;
     uint64_t offFile    = 0;
-    int rc;
 
     AssertPtr(pImage);
     Assert(uOffset % 512 == 0);
     Assert(cbToRead % 512 == 0);
-
-    if (!VALID_PTR(pIoCtx) || !cbToRead)
-    {
-        rc = VERR_INVALID_PARAMETER;
-        goto out;
-    }
-
-    if (   uOffset + cbToRead > pImage->cbSize
-        || cbToRead == 0)
-    {
-        rc = VERR_INVALID_PARAMETER;
-        goto out;
-    }
+    AssertReturn((VALID_PTR(pIoCtx) && cbToRead), VERR_INVALID_PARAMETER);
+    AssertReturn(uOffset + cbToRead <= pImage->cbSize, VERR_INVALID_PARAMETER);
 
     qedConvertLogicalOffset(pImage, uOffset, &idxL1, &idxL2, &offCluster);
@@ -1796,5 +1677,5 @@
 
     /* Get offset in image. */
-    rc = qedConvertToImageOffset(pImage, pIoCtx, idxL1, idxL2, offCluster, &offFile);
+    int rc = qedConvertToImageOffset(pImage, pIoCtx, idxL1, idxL2, offCluster, &offFile);
     if (RT_SUCCESS(rc))
         rc = vdIfIoIntFileReadUser(pImage->pIfIo, pImage->pStorage, offFile,
@@ -1807,9 +1688,9 @@
         *pcbActuallyRead = cbToRead;
 
-out:
     LogFlowFunc(("returns %Rrc\n", rc));
     return rc;
 }
 
+/** @copydoc VDIMAGEBACKEND::pfnRead */
 static DECLCALLBACK(int) qedWrite(void *pBackendData, uint64_t uOffset, size_t cbToWrite,
                                   PVDIOCTX pIoCtx, size_t *pcbWriteProcess, size_t *pcbPreRead,
@@ -1828,114 +1709,43 @@
     Assert(!(uOffset % 512));
     Assert(!(cbToWrite % 512));
-
-    if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
-    {
-        rc = VERR_VD_IMAGE_READ_ONLY;
-        goto out;
-    }
-
-    if (!VALID_PTR(pIoCtx) || !cbToWrite)
-    {
-        rc = VERR_INVALID_PARAMETER;
-        goto out;
-    }
-
-    if (   uOffset + cbToWrite > pImage->cbSize
-        || cbToWrite == 0)
-    {
-        rc = VERR_INVALID_PARAMETER;
-        goto out;
-    }
-
-    /* Convert offset to L1, L2 index and cluster offset. */
-    qedConvertLogicalOffset(pImage, uOffset, &idxL1, &idxL2, &offCluster);
-
-    /* Clip write size to remain in the cluster. */
-    cbToWrite = RT_MIN(cbToWrite, pImage->cbCluster - offCluster);
-    Assert(!(cbToWrite % 512));
-
-    /* Get offset in image. */
-    rc = qedConvertToImageOffset(pImage, pIoCtx, idxL1, idxL2, offCluster, &offImage);
-    if (RT_SUCCESS(rc))
-        rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
-                                    offImage, pIoCtx, cbToWrite, NULL, NULL);
-    else if (rc == VERR_VD_BLOCK_FREE)
-    {
-        if (   cbToWrite == pImage->cbCluster
-            && !(fWrite & VD_WRITE_NO_ALLOC))
-        {
-            PQEDL2CACHEENTRY pL2Entry = NULL;
-
-            /* Full cluster write to previously unallocated cluster.
-             * Allocate cluster and write data. */
-            Assert(!offCluster);
-
-            do
+    AssertReturn((VALID_PTR(pIoCtx) && cbToWrite), VERR_INVALID_PARAMETER);
+    AssertReturn(uOffset + cbToWrite <= pImage->cbSize, VERR_INVALID_PARAMETER);
+
+    if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
+    {
+        /* Convert offset to L1, L2 index and cluster offset. */
+        qedConvertLogicalOffset(pImage, uOffset, &idxL1, &idxL2, &offCluster);
+
+        /* Clip write size to remain in the cluster. */
+        cbToWrite = RT_MIN(cbToWrite, pImage->cbCluster - offCluster);
+        Assert(!(cbToWrite % 512));
+
+        /* Get offset in image. */
+        rc = qedConvertToImageOffset(pImage, pIoCtx, idxL1, idxL2, offCluster, &offImage);
+        if (RT_SUCCESS(rc))
+            rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
+                                        offImage, pIoCtx, cbToWrite, NULL, NULL);
+        else if (rc == VERR_VD_BLOCK_FREE)
+        {
+            if (   cbToWrite == pImage->cbCluster
+                && !(fWrite & VD_WRITE_NO_ALLOC))
             {
-                /* Check if we have to allocate a new cluster for L2 tables. */
-                if (!pImage->paL1Table[idxL1])
+                PQEDL2CACHEENTRY pL2Entry = NULL;
+
+                /* Full cluster write to previously unallocated cluster.
+                 * Allocate cluster and write data. */
+                Assert(!offCluster);
+
+                do
                 {
-                    uint64_t offL2Tbl;
-                    PQEDCLUSTERASYNCALLOC pL2ClusterAlloc = NULL;
-
-                    /* Allocate new async cluster allocation state. */
-                    pL2ClusterAlloc = (PQEDCLUSTERASYNCALLOC)RTMemAllocZ(sizeof(QEDCLUSTERASYNCALLOC));
-                    if (RT_UNLIKELY(!pL2ClusterAlloc))
+                    /* Check if we have to allocate a new cluster for L2 tables. */
+                    if (!pImage->paL1Table[idxL1])
                     {
-                        rc = VERR_NO_MEMORY;
-                        break;
-                    }
-
-                    pL2Entry = qedL2TblCacheEntryAlloc(pImage);
-                    if (!pL2Entry)
-                    {
-                        rc = VERR_NO_MEMORY;
-                        RTMemFree(pL2ClusterAlloc);
-                        break;
-                    }
-
-                    offL2Tbl = qedClusterAllocate(pImage, qedByte2Cluster(pImage, pImage->cbTable));
-                    pL2Entry->offL2Tbl = offL2Tbl;
-                    memset(pL2Entry->paL2Tbl, 0, pImage->cbTable);
-
-                    pL2ClusterAlloc->enmAllocState = QEDCLUSTERASYNCALLOCSTATE_L2_ALLOC;
-                    pL2ClusterAlloc->cbImageOld    = offL2Tbl;
-                    pL2ClusterAlloc->offClusterNew = offL2Tbl;
-                    pL2ClusterAlloc->idxL1         = idxL1;
-                    pL2ClusterAlloc->idxL2         = idxL2;
-                    pL2ClusterAlloc->cbToWrite     = cbToWrite;
-                    pL2ClusterAlloc->pL2Entry      = pL2Entry;
-
-                    /*
-                     * Write the L2 table first and link to the L1 table afterwards.
-                     * If something unexpected happens the worst case which can happen
-                     * is a leak of some clusters.
-                     */
-                    rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
-                                                offL2Tbl, pL2Entry->paL2Tbl, pImage->cbTable, pIoCtx,
-                                                qedAsyncClusterAllocUpdate, pL2ClusterAlloc);
-                    if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
-                        break;
-                    else if (RT_FAILURE(rc))
-                    {
-                        RTMemFree(pL2ClusterAlloc);
-                        qedL2TblCacheEntryFree(pImage, pL2Entry);
-                        break;
-                    }
-
-                    rc = qedAsyncClusterAllocUpdate(pImage, pIoCtx, pL2ClusterAlloc, rc);
-                }
-                else
-                {
-                    rc = qedL2TblCacheFetchAsync(pImage, pIoCtx, pImage->paL1Table[idxL1],
-                                                 &pL2Entry);
-
-                    if (RT_SUCCESS(rc))
-                    {
-                        PQEDCLUSTERASYNCALLOC pDataClusterAlloc = NULL;
+                        uint64_t offL2Tbl;
+                        PQEDCLUSTERASYNCALLOC pL2ClusterAlloc = NULL;
 
                         /* Allocate new async cluster allocation state. */
-                        pDataClusterAlloc = (PQEDCLUSTERASYNCALLOC)RTMemAllocZ(sizeof(QEDCLUSTERASYNCALLOC));
-                        if (RT_UNLIKELY(!pDataClusterAlloc))
+                        pL2ClusterAlloc = (PQEDCLUSTERASYNCALLOC)RTMemAllocZ(sizeof(QEDCLUSTERASYNCALLOC));
+                        if (RT_UNLIKELY(!pL2ClusterAlloc))
                         {
                             rc = VERR_NO_MEMORY;
@@ -1943,54 +1753,112 @@
                         }
 
-                        /* Allocate new cluster for the data. */
-                        uint64_t offData = qedClusterAllocate(pImage, 1);
-
-                        pDataClusterAlloc->enmAllocState = QEDCLUSTERASYNCALLOCSTATE_USER_ALLOC;
-                        pDataClusterAlloc->cbImageOld    = offData;
-                        pDataClusterAlloc->offClusterNew = offData;
-                        pDataClusterAlloc->idxL1         = idxL1;
-                        pDataClusterAlloc->idxL2         = idxL2;
-                        pDataClusterAlloc->cbToWrite     = cbToWrite;
-                        pDataClusterAlloc->pL2Entry      = pL2Entry;
-
-                        /* Write data. */
-                        rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
-                                                    offData, pIoCtx, cbToWrite,
-                                                    qedAsyncClusterAllocUpdate, pDataClusterAlloc);
+                        pL2Entry = qedL2TblCacheEntryAlloc(pImage);
+                        if (!pL2Entry)
+                        {
+                            rc = VERR_NO_MEMORY;
+                            RTMemFree(pL2ClusterAlloc);
+                            break;
+                        }
+
+                        offL2Tbl = qedClusterAllocate(pImage, qedByte2Cluster(pImage, pImage->cbTable));
+                        pL2Entry->offL2Tbl = offL2Tbl;
+                        memset(pL2Entry->paL2Tbl, 0, pImage->cbTable);
+
+                        pL2ClusterAlloc->enmAllocState = QEDCLUSTERASYNCALLOCSTATE_L2_ALLOC;
+                        pL2ClusterAlloc->cbImageOld    = offL2Tbl;
+                        pL2ClusterAlloc->offClusterNew = offL2Tbl;
+                        pL2ClusterAlloc->idxL1         = idxL1;
+                        pL2ClusterAlloc->idxL2         = idxL2;
+                        pL2ClusterAlloc->cbToWrite     = cbToWrite;
+                        pL2ClusterAlloc->pL2Entry      = pL2Entry;
+
+                        /*
+                         * Write the L2 table first and link to the L1 table afterwards.
+                         * If something unexpected happens the worst case which can happen
+                         * is a leak of some clusters.
+                         */
+                        rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
+                                                    offL2Tbl, pL2Entry->paL2Tbl, pImage->cbTable, pIoCtx,
+                                                    qedAsyncClusterAllocUpdate, pL2ClusterAlloc);
                         if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
                             break;
                         else if (RT_FAILURE(rc))
                         {
-                            RTMemFree(pDataClusterAlloc);
+                            RTMemFree(pL2ClusterAlloc);
+                            qedL2TblCacheEntryFree(pImage, pL2Entry);
                             break;
                         }
 
-                        rc = qedAsyncClusterAllocUpdate(pImage, pIoCtx, pDataClusterAlloc, rc);
+                        rc = qedAsyncClusterAllocUpdate(pImage, pIoCtx, pL2ClusterAlloc, rc);
                     }
-                }
-
-            } while (0);
-
-            *pcbPreRead = 0;
-            *pcbPostRead = 0;
-        }
-        else
-        {
-            /* Trying to do a partial write to an unallocated cluster. Don't do
-             * anything except letting the upper layer know what to do. */
-            *pcbPreRead = offCluster;
-            *pcbPostRead = pImage->cbCluster - cbToWrite - *pcbPreRead;
-        }
-    }
-
-    if (pcbWriteProcess)
-        *pcbWriteProcess = cbToWrite;
-
-
-out:
+                    else
+                    {
+                        rc = qedL2TblCacheFetchAsync(pImage, pIoCtx, pImage->paL1Table[idxL1],
+                                                     &pL2Entry);
+
+                        if (RT_SUCCESS(rc))
+                        {
+                            PQEDCLUSTERASYNCALLOC pDataClusterAlloc = NULL;
+
+                            /* Allocate new async cluster allocation state. */
+                            pDataClusterAlloc = (PQEDCLUSTERASYNCALLOC)RTMemAllocZ(sizeof(QEDCLUSTERASYNCALLOC));
+                            if (RT_UNLIKELY(!pDataClusterAlloc))
+                            {
+                                rc = VERR_NO_MEMORY;
+                                break;
+                            }
+
+                            /* Allocate new cluster for the data. */
+                            uint64_t offData = qedClusterAllocate(pImage, 1);
+
+                            pDataClusterAlloc->enmAllocState = QEDCLUSTERASYNCALLOCSTATE_USER_ALLOC;
+                            pDataClusterAlloc->cbImageOld    = offData;
+                            pDataClusterAlloc->offClusterNew = offData;
+                            pDataClusterAlloc->idxL1         = idxL1;
+                            pDataClusterAlloc->idxL2         = idxL2;
+                            pDataClusterAlloc->cbToWrite     = cbToWrite;
+                            pDataClusterAlloc->pL2Entry      = pL2Entry;
+
+                            /* Write data. */
+                            rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
+                                                        offData, pIoCtx, cbToWrite,
+                                                        qedAsyncClusterAllocUpdate, pDataClusterAlloc);
+                            if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
+                                break;
+                            else if (RT_FAILURE(rc))
+                            {
+                                RTMemFree(pDataClusterAlloc);
+                                break;
+                            }
+
+                            rc = qedAsyncClusterAllocUpdate(pImage, pIoCtx, pDataClusterAlloc, rc);
+                        }
+                    }
+
+                } while (0);
+
+                *pcbPreRead = 0;
+                *pcbPostRead = 0;
+            }
+            else
+            {
+                /* Trying to do a partial write to an unallocated cluster. Don't do
+                 * anything except letting the upper layer know what to do. */
+                *pcbPreRead = offCluster;
+                *pcbPostRead = pImage->cbCluster - cbToWrite - *pcbPreRead;
+            }
+        }
+
+        if (pcbWriteProcess)
+            *pcbWriteProcess = cbToWrite;
+    }
+    else
+        rc = VERR_VD_IMAGE_READ_ONLY;
+
     LogFlowFunc(("returns %Rrc\n", rc));
     return rc;
 }
 
+/** @copydoc VDIMAGEBACKEND::pfnFlush */
 static DECLCALLBACK(int) qedFlush(void *pBackendData, PVDIOCTX pIoCtx)
 {
@@ -1999,10 +1867,27 @@
     int rc = VINF_SUCCESS;
 
-    Assert(pImage);
-
-    if (VALID_PTR(pIoCtx))
-        rc = qedFlushImageAsync(pImage, pIoCtx);
-    else
-        rc = VERR_INVALID_PARAMETER;
+    AssertPtr(pImage);
+    AssertPtrReturn(pIoCtx, VERR_INVALID_PARAMETER);
+
+    if (   pImage->pStorage
+        && !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
+    {
+        QedHeader Header;
+
+        Assert(!(pImage->cbTable % pImage->cbCluster));
+        rc = qedTblWrite(pImage, pIoCtx, pImage->offL1Table, pImage->paL1Table,
+                         NULL, NULL);
+        if (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
+        {
+            /* Write header. */
+            qedHdrConvertFromHostEndianess(pImage, &Header);
+            rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
+                                        0, &Header, sizeof(Header),
+                                        pIoCtx, NULL, NULL);
+            if (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
+                rc = vdIfIoIntFileFlush(pImage->pIfIo, pImage->pStorage,
+                                        pIoCtx, NULL, NULL);
+        }
+    }
 
     LogFlowFunc(("returns %Rrc\n", rc));
@@ -2016,10 +1901,7 @@
     PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
 
-    AssertPtr(pImage);
-
-    if (pImage)
-        return 1;
-    else
-        return 0;
+    AssertPtrReturn(pImage, 0);
+
+    return 1;
 }
 
@@ -2031,7 +1913,7 @@
     uint32_t cb = 0;
 
-    AssertPtr(pImage);
-
-    if (pImage && pImage->pStorage)
+    AssertPtrReturn(pImage, 0);
+
+    if (pImage->pStorage)
         cb = 512;
 
@@ -2047,7 +1929,7 @@
     uint64_t cb = 0;
 
-    AssertPtr(pImage);
-
-    if (pImage && pImage->pStorage)
+    AssertPtrReturn(pImage, 0);
+
+    if (pImage->pStorage)
         cb = pImage->cbSize;
 
@@ -2063,15 +1945,12 @@
     uint64_t cb = 0;
 
-    AssertPtr(pImage);
-
-    if (pImage)
-    {
-        uint64_t cbFile;
-        if (pImage->pStorage)
-        {
-            int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
-            if (RT_SUCCESS(rc))
-                cb += cbFile;
-        }
+    AssertPtrReturn(pImage, 0);
+
+    uint64_t cbFile;
+    if (pImage->pStorage)
+    {
+        int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
+        if (RT_SUCCESS(rc))
+            cb += cbFile;
     }
 
@@ -2086,20 +1965,12 @@
     LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
     PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
-    int rc;
-
-    AssertPtr(pImage);
-
-    if (pImage)
-    {
-        if (pImage->PCHSGeometry.cCylinders)
-        {
-            *pPCHSGeometry = pImage->PCHSGeometry;
-            rc = VINF_SUCCESS;
-        }
-        else
-            rc = VERR_VD_GEOMETRY_NOT_SET;
-    }
+    int rc = VINF_SUCCESS;
+
+    AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
+
+    if (pImage->PCHSGeometry.cCylinders)
+        *pPCHSGeometry = pImage->PCHSGeometry;
     else
-        rc = VERR_VD_NOT_OPENED;
+        rc = VERR_VD_GEOMETRY_NOT_SET;
 
     LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
@@ -2111,81 +1982,54 @@
                                             PCVDGEOMETRY pPCHSGeometry)
 {
-    LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
-    PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
-    int rc;
-
-    AssertPtr(pImage);
-
-    if (pImage)
-    {
-        if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
-        {
-            rc = VERR_VD_IMAGE_READ_ONLY;
-            goto out;
-        }
-
+    LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n",
+                 pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
+    PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
+    int rc = VINF_SUCCESS;
+
+    AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
+
+    if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
+        rc = VERR_VD_IMAGE_READ_ONLY;
+    else
         pImage->PCHSGeometry = *pPCHSGeometry;
-        rc = VINF_SUCCESS;
-    }
+
+    LogFlowFunc(("returns %Rrc\n", rc));
+    return rc;
+}
+
+/** @copydoc VDIMAGEBACKEND::pfnGetLCHSGeometry */
+static DECLCALLBACK(int) qedGetLCHSGeometry(void *pBackendData, PVDGEOMETRY pLCHSGeometry)
+{
+    LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
+    PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
+    int rc = VINF_SUCCESS;
+
+    AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
+
+    if (pImage->LCHSGeometry.cCylinders)
+        *pLCHSGeometry = pImage->LCHSGeometry;
     else
-        rc = VERR_VD_NOT_OPENED;
-
-out:
-    LogFlowFunc(("returns %Rrc\n", rc));
-    return rc;
-}
-
-/** @copydoc VDIMAGEBACKEND::pfnGetLCHSGeometry */
-static DECLCALLBACK(int) qedGetLCHSGeometry(void *pBackendData,
-                                            PVDGEOMETRY pLCHSGeometry)
-{
-     LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
-    PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
-    int rc;
-
-    AssertPtr(pImage);
-
-    if (pImage)
-    {
-        if (pImage->LCHSGeometry.cCylinders)
-        {
-            *pLCHSGeometry = pImage->LCHSGeometry;
-            rc = VINF_SUCCESS;
-        }
-        else
-            rc = VERR_VD_GEOMETRY_NOT_SET;
-    }
+        rc = VERR_VD_GEOMETRY_NOT_SET;
+
+    LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders,
+                 pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
+    return rc;
+}
+
+/** @copydoc VDIMAGEBACKEND::pfnSetLCHSGeometry */
+static DECLCALLBACK(int) qedSetLCHSGeometry(void *pBackendData, PCVDGEOMETRY pLCHSGeometry)
+{
+    LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData,
+                 pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
+    PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
+    int rc = VINF_SUCCESS;
+
+    AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
+
+    if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
+        rc = VERR_VD_IMAGE_READ_ONLY;
     else
-        rc = VERR_VD_NOT_OPENED;
-
-    LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
-    return rc;
-}
-
-/** @copydoc VDIMAGEBACKEND::pfnSetLCHSGeometry */
-static DECLCALLBACK(int) qedSetLCHSGeometry(void *pBackendData,
-                                            PCVDGEOMETRY pLCHSGeometry)
-{
-    LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
-    PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
-    int rc;
-
-    AssertPtr(pImage);
-
-    if (pImage)
-    {
-        if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
-        {
-            rc = VERR_VD_IMAGE_READ_ONLY;
-            goto out;
-        }
-
         pImage->LCHSGeometry = *pLCHSGeometry;
-        rc = VINF_SUCCESS;
-    }
-    else
-        rc = VERR_VD_NOT_OPENED;
-
-out:
+
     LogFlowFunc(("returns %Rrc\n", rc));
     return rc;
@@ -2197,15 +2041,9 @@
     LogFlowFunc(("pBackendData=%#p\n", pBackendData));
     PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
-    unsigned uImageFlags;
-
-    AssertPtr(pImage);
-
-    if (pImage)
-        uImageFlags = pImage->uImageFlags;
-    else
-        uImageFlags = 0;
-
-    LogFlowFunc(("returns %#x\n", uImageFlags));
-    return uImageFlags;
+
+    AssertPtrReturn(pImage, 0);
+
+    LogFlowFunc(("returns %#x\n", pImage->uImageFlags));
+    return pImage->uImageFlags;
 }
 
@@ -2215,15 +2053,9 @@
     LogFlowFunc(("pBackendData=%#p\n", pBackendData));
     PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
-    unsigned uOpenFlags;
-
-    AssertPtr(pImage);
-
-    if (pImage)
-        uOpenFlags = pImage->uOpenFlags;
-    else
-        uOpenFlags = 0;
-
-    LogFlowFunc(("returns %#x\n", uOpenFlags));
-    return uOpenFlags;
+
+    AssertPtrReturn(pImage, 0);
+
+    LogFlowFunc(("returns %#x\n", pImage->uOpenFlags));
+    return pImage->uOpenFlags;
 }
 
@@ -2233,21 +2065,19 @@
     LogFlowFunc(("pBackendData=%#p\n uOpenFlags=%#x", pBackendData, uOpenFlags));
     PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
-    int rc;
+    int rc = VINF_SUCCESS;
 
     /* Image must be opened and the new flags must be valid. */
     if (!pImage || (uOpenFlags & ~(  VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO
-                                   | VD_OPEN_FLAGS_ASYNC_IO | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)))
-    {
+                                   | VD_OPEN_FLAGS_ASYNC_IO | VD_OPEN_FLAGS_SHAREABLE
+                                   | VD_OPEN_FLAGS_SEQUENTIAL | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)))
         rc = VERR_INVALID_PARAMETER;
-        goto out;
-    }
-
-    /* Implement this operation via reopening the image. */
-    rc = qedFreeImage(pImage, false);
-    if (RT_FAILURE(rc))
-        goto out;
-    rc = qedOpenImage(pImage, uOpenFlags);
-
-out:
+    else
+    {
+        /* Implement this operation via reopening the image. */
+        rc = qedFreeImage(pImage, false);
+        if (RT_SUCCESS(rc))
+            rc = qedOpenImage(pImage, uOpenFlags);
+    }
+
     LogFlowFunc(("returns %Rrc\n", rc));
     return rc;
@@ -2261,15 +2091,9 @@
     LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
     PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
-    int rc;
-
-    AssertPtr(pImage);
-
-    if (pImage)
-        rc = VERR_NOT_SUPPORTED;
-    else
-        rc = VERR_VD_NOT_OPENED;
-
-    LogFlowFunc(("returns %Rrc comment='%s'\n", rc, pszComment));
-    return rc;
+
+    AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
+
+    LogFlowFunc(("returns %Rrc comment='%s'\n", VERR_NOT_SUPPORTED, pszComment));
+    return VERR_NOT_SUPPORTED;
 }
 
@@ -2280,17 +2104,12 @@
     LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
     PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
+
+    AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
+
     int rc;
-
-    AssertPtr(pImage);
-
-    if (pImage)
-    {
-        if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
-            rc = VERR_VD_IMAGE_READ_ONLY;
-        else
-            rc = VERR_NOT_SUPPORTED;
-    }
+    if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
+        rc = VERR_VD_IMAGE_READ_ONLY;
     else
-        rc = VERR_VD_NOT_OPENED;
+        rc = VERR_NOT_SUPPORTED;
 
     LogFlowFunc(("returns %Rrc\n", rc));
@@ -2304,15 +2123,9 @@
     LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
     PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
-    int rc;
-
-    AssertPtr(pImage);
-
-    if (pImage)
-        rc = VERR_NOT_SUPPORTED;
-    else
-        rc = VERR_VD_NOT_OPENED;
-
-    LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
-    return rc;
+
+    AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
+
+    LogFlowFunc(("returns %Rrc (%RTuuid)\n", VERR_NOT_SUPPORTED, pUuid));
+    return VERR_NOT_SUPPORTED;
 }
 
@@ -2323,18 +2136,12 @@
     LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
     PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
+
+    AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
+
     int rc;
-
-    LogFlowFunc(("%RTuuid\n", pUuid));
-    AssertPtr(pImage);
-
-    if (pImage)
-    {
-        if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
-            rc = VERR_NOT_SUPPORTED;
-        else
-            rc = VERR_VD_IMAGE_READ_ONLY;
-    }
+    if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
+        rc = VERR_VD_IMAGE_READ_ONLY;
     else
-        rc = VERR_VD_NOT_OPENED;
+        rc = VERR_NOT_SUPPORTED;
 
     LogFlowFunc(("returns %Rrc\n", rc));
@@ -2348,15 +2155,9 @@
     LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
     PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
-    int rc;
-
-    AssertPtr(pImage);
-
-    if (pImage)
-        rc = VERR_NOT_SUPPORTED;
-    else
-        rc = VERR_VD_NOT_OPENED;
-
-    LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
-    return rc;
+
+    AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
+
+    LogFlowFunc(("returns %Rrc (%RTuuid)\n", VERR_NOT_SUPPORTED, pUuid));
+    return VERR_NOT_SUPPORTED;
 }
 
@@ -2367,17 +2168,12 @@
     LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
     PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
+
+    AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
+
     int rc;
-
-    AssertPtr(pImage);
-
-    if (pImage)
-    {
-        if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
-            rc = VERR_NOT_SUPPORTED;
-        else
-            rc = VERR_VD_IMAGE_READ_ONLY;
-    }
+    if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
+        rc = VERR_VD_IMAGE_READ_ONLY;
     else
-        rc = VERR_VD_NOT_OPENED;
+        rc = VERR_NOT_SUPPORTED;
 
     LogFlowFunc(("returns %Rrc\n", rc));
@@ -2391,15 +2187,9 @@
     LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
     PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
-    int rc;
-
-    AssertPtr(pImage);
-
-    if (pImage)
-        rc = VERR_NOT_SUPPORTED;
-    else
-        rc = VERR_VD_NOT_OPENED;
-
-    LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
-    return rc;
+
+    AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
+
+    LogFlowFunc(("returns %Rrc (%RTuuid)\n", VERR_NOT_SUPPORTED, pUuid));
+    return VERR_NOT_SUPPORTED;
 }
 
@@ -2410,17 +2200,12 @@
     LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
     PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
+
+    AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
+
     int rc;
-
-    AssertPtr(pImage);
-
-    if (pImage)
-    {
-        if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
-            rc = VERR_NOT_SUPPORTED;
-        else
-            rc = VERR_VD_IMAGE_READ_ONLY;
-    }
+    if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
+        rc = VERR_VD_IMAGE_READ_ONLY;
     else
-        rc = VERR_VD_NOT_OPENED;
+        rc = VERR_NOT_SUPPORTED;
 
     LogFlowFunc(("returns %Rrc\n", rc));
@@ -2434,15 +2219,9 @@
     LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
     PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
-    int rc;
-
-    AssertPtr(pImage);
-
-    if (pImage)
-        rc = VERR_NOT_SUPPORTED;
-    else
-        rc = VERR_VD_NOT_OPENED;
-
-    LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
-    return rc;
+
+    AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
+
+    LogFlowFunc(("returns %Rrc (%RTuuid)\n", VERR_NOT_SUPPORTED, pUuid));
+    return VERR_NOT_SUPPORTED;
 }
 
@@ -2453,17 +2232,12 @@
     LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
     PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
+
+    AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
+
     int rc;
-
-    AssertPtr(pImage);
-
-    if (pImage)
-    {
-        if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
-            rc = VERR_NOT_SUPPORTED;
-        else
-            rc = VERR_VD_IMAGE_READ_ONLY;
-    }
+    if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
+        rc = VERR_VD_IMAGE_READ_ONLY;
     else
-        rc = VERR_VD_NOT_OPENED;
+        rc = VERR_NOT_SUPPORTED;
 
     LogFlowFunc(("returns %Rrc\n", rc));
@@ -2476,12 +2250,9 @@
     PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
 
-    AssertPtr(pImage);
-    if (pImage)
-    {
-        vdIfErrorMessage(pImage->pIfError, "Header: Geometry PCHS=%u/%u/%u LCHS=%u/%u/%u cbSector=%llu\n",
-                         pImage->PCHSGeometry.cCylinders, pImage->PCHSGeometry.cHeads, pImage->PCHSGeometry.cSectors,
-                         pImage->LCHSGeometry.cCylinders, pImage->LCHSGeometry.cHeads, pImage->LCHSGeometry.cSectors,
-                         pImage->cbSize / 512);
-    }
+    AssertPtrReturnVoid(pImage);
+    vdIfErrorMessage(pImage->pIfError, "Header: Geometry PCHS=%u/%u/%u LCHS=%u/%u/%u cbSector=%llu\n",
+                     pImage->PCHSGeometry.cCylinders, pImage->PCHSGeometry.cHeads, pImage->PCHSGeometry.cSectors,
+                     pImage->LCHSGeometry.cCylinders, pImage->LCHSGeometry.cHeads, pImage->LCHSGeometry.cSectors,
+                     pImage->cbSize / 512);
 }
 
@@ -2492,12 +2263,10 @@
     PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
 
-    AssertPtr(pImage);
-    if (pImage)
-        if (pImage->pszBackingFilename)
-            *ppszParentFilename = RTStrDup(pImage->pszBackingFilename);
-        else
-            rc = VERR_NOT_SUPPORTED;
+    AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
+
+    if (pImage->pszBackingFilename)
+        *ppszParentFilename = RTStrDup(pImage->pszBackingFilename);
     else
-        rc = VERR_VD_NOT_OPENED;
+        rc = VERR_NOT_SUPPORTED;
 
     LogFlowFunc(("returns %Rrc\n", rc));
@@ -2511,43 +2280,39 @@
     PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
 
-    AssertPtr(pImage);
-    if (pImage)
-    {
-        if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
-            rc = VERR_VD_IMAGE_READ_ONLY;
-        else if (   pImage->pszBackingFilename
-                 && (strlen(pszParentFilename) > pImage->cbBackingFilename))
-            rc = VERR_NOT_SUPPORTED; /* The new filename is longer than the old one. */
+    AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
+
+    if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
+        rc = VERR_VD_IMAGE_READ_ONLY;
+    else if (   pImage->pszBackingFilename
+             && (strlen(pszParentFilename) > pImage->cbBackingFilename))
+        rc = VERR_NOT_SUPPORTED; /* The new filename is longer than the old one. */
+    else
+    {
+        if (pImage->pszBackingFilename)
+            RTStrFree(pImage->pszBackingFilename);
+        pImage->pszBackingFilename = RTStrDup(pszParentFilename);
+        if (!pImage->pszBackingFilename)
+            rc = VERR_NO_MEMORY;
         else
         {
-            if (pImage->pszBackingFilename)
-                RTStrFree(pImage->pszBackingFilename);
-            pImage->pszBackingFilename = RTStrDup(pszParentFilename);
-            if (!pImage->pszBackingFilename)
-                rc = VERR_NO_MEMORY;
-            else
+            if (!pImage->offBackingFilename)
             {
-                if (!pImage->offBackingFilename)
-                {
-                    /* Allocate new cluster. */
-                    uint64_t offData = qedClusterAllocate(pImage, 1);
-
-                    Assert((offData & UINT32_MAX) == offData);
-                    pImage->offBackingFilename = (uint32_t)offData;
-                    pImage->cbBackingFilename  = (uint32_t)strlen(pszParentFilename);
-                    rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage,
-                                              offData + pImage->cbCluster);
-                }
-
-                if (RT_SUCCESS(rc))
-                    rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
-                                                pImage->offBackingFilename,
-                                                pImage->pszBackingFilename,
-                                                strlen(pImage->pszBackingFilename));
+                /* Allocate new cluster. */
+                uint64_t offData = qedClusterAllocate(pImage, 1);
+
+                Assert((offData & UINT32_MAX) == offData);
+                pImage->offBackingFilename = (uint32_t)offData;
+                pImage->cbBackingFilename  = (uint32_t)strlen(pszParentFilename);
+                rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage,
+                                          offData + pImage->cbCluster);
             }
-        }
-    }
-    else
-        rc = VERR_VD_NOT_OPENED;
+
+            if (RT_SUCCESS(rc))
+                rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
+                                            pImage->offBackingFilename,
+                                            pImage->pszBackingFilename,
+                                            strlen(pImage->pszBackingFilename));
+        }
+    }
 
     LogFlowFunc(("returns %Rrc\n", rc));
