Index: trunk/src/VBox/Devices/Storage/VBoxHDD.cpp
===================================================================
--- trunk/src/VBox/Devices/Storage/VBoxHDD.cpp	(revision 138)
+++ trunk/src/VBox/Devices/Storage/VBoxHDD.cpp	(revision 139)
@@ -213,5 +213,5 @@
 *   Internal Functions for header access                                       *
 *******************************************************************************/
-static VDIIMAGETYPE getImageType(PVDIHEADER ph)
+static inline VDIIMAGETYPE getImageType(PVDIHEADER ph)
 {
     switch (GET_MAJOR_HEADER_VERSION(ph))
@@ -224,5 +224,5 @@
 }
 
-static unsigned getImageFlags(PVDIHEADER ph)
+static inline unsigned getImageFlags(PVDIHEADER ph)
 {
     switch (GET_MAJOR_HEADER_VERSION(ph))
@@ -235,5 +235,5 @@
 }
 
-static char *getImageComment(PVDIHEADER ph)
+static inline char *getImageComment(PVDIHEADER ph)
 {
     switch (GET_MAJOR_HEADER_VERSION(ph))
@@ -246,5 +246,5 @@
 }
 
-static unsigned getImageBlocksOffset(PVDIHEADER ph)
+static inline unsigned getImageBlocksOffset(PVDIHEADER ph)
 {
     switch (GET_MAJOR_HEADER_VERSION(ph))
@@ -257,5 +257,5 @@
 }
 
-static unsigned getImageDataOffset(PVDIHEADER ph)
+static inline unsigned getImageDataOffset(PVDIHEADER ph)
 {
     switch (GET_MAJOR_HEADER_VERSION(ph))
@@ -269,5 +269,5 @@
 }
 
-static PVDIDISKGEOMETRY getImageGeometry(PVDIHEADER ph)
+static inline PVDIDISKGEOMETRY getImageGeometry(PVDIHEADER ph)
 {
     switch (GET_MAJOR_HEADER_VERSION(ph))
@@ -280,5 +280,5 @@
 }
 
-static PDMBIOSTRANSLATION getImageTranslation(PVDIHEADER ph)
+static inline PDMBIOSTRANSLATION getImageTranslation(PVDIHEADER ph)
 {
     switch (GET_MAJOR_HEADER_VERSION(ph))
@@ -291,18 +291,15 @@
 }
 
-static void setImageTranslation(PVDIHEADER ph, PDMBIOSTRANSLATION enmTranslation)
+static inline void setImageTranslation(PVDIHEADER ph, PDMBIOSTRANSLATION enmTranslation)
 {
     switch (GET_MAJOR_HEADER_VERSION(ph))
     {
-        case 0:
-            return;
-        case 1:
-            ph->u.v1.u32Translation = (uint32_t)enmTranslation;
-            return;
+        case 0:                                                     return;
+        case 1: ph->u.v1.u32Translation = (uint32_t)enmTranslation; return;
     }
     AssertFailed();
 }
 
-static uint64_t getImageDiskSize(PVDIHEADER ph)
+static inline uint64_t getImageDiskSize(PVDIHEADER ph)
 {
     switch (GET_MAJOR_HEADER_VERSION(ph))
@@ -315,5 +312,5 @@
 }
 
-static unsigned getImageBlockSize(PVDIHEADER ph)
+static inline unsigned getImageBlockSize(PVDIHEADER ph)
 {
     switch (GET_MAJOR_HEADER_VERSION(ph))
@@ -326,5 +323,5 @@
 }
 
-static unsigned getImageExtraBlockSize(PVDIHEADER ph)
+static inline unsigned getImageExtraBlockSize(PVDIHEADER ph)
 {
     switch (GET_MAJOR_HEADER_VERSION(ph))
@@ -337,5 +334,5 @@
 }
 
-static unsigned getImageBlocks(PVDIHEADER ph)
+static inline unsigned getImageBlocks(PVDIHEADER ph)
 {
     switch (GET_MAJOR_HEADER_VERSION(ph))
@@ -348,5 +345,5 @@
 }
 
-static unsigned getImageBlocksAllocated(PVDIHEADER ph)
+static inline unsigned getImageBlocksAllocated(PVDIHEADER ph)
 {
     switch (GET_MAJOR_HEADER_VERSION(ph))
@@ -359,19 +356,15 @@
 }
 
-static void setImageBlocksAllocated(PVDIHEADER ph, unsigned cBlocks)
+static inline void setImageBlocksAllocated(PVDIHEADER ph, unsigned cBlocks)
 {
     switch (GET_MAJOR_HEADER_VERSION(ph))
     {
-        case 0:
-            ph->u.v0.cBlocksAllocated = cBlocks;
-            return;
-        case 1:
-            ph->u.v1.cBlocksAllocated = cBlocks;
-            return;
+        case 0: ph->u.v0.cBlocksAllocated = cBlocks; return;
+        case 1: ph->u.v1.cBlocksAllocated = cBlocks; return;
     }
     AssertFailed();
 }
 
-static PRTUUID getImageCreationUUID(PVDIHEADER ph)
+static inline PRTUUID getImageCreationUUID(PVDIHEADER ph)
 {
     switch (GET_MAJOR_HEADER_VERSION(ph))
@@ -384,5 +377,5 @@
 }
 
-static PRTUUID getImageModificationUUID(PVDIHEADER ph)
+static inline PRTUUID getImageModificationUUID(PVDIHEADER ph)
 {
     switch (GET_MAJOR_HEADER_VERSION(ph))
@@ -395,5 +388,5 @@
 }
 
-static PRTUUID getImageParentUUID(PVDIHEADER ph)
+static inline PRTUUID getImageParentUUID(PVDIHEADER ph)
 {
     switch (GET_MAJOR_HEADER_VERSION(ph))
@@ -406,5 +399,5 @@
 }
 
-static PRTUUID getImageParentModificationUUID(PVDIHEADER ph)
+static inline PRTUUID getImageParentModificationUUID(PVDIHEADER ph)
 {
     switch (GET_MAJOR_HEADER_VERSION(ph))
@@ -421,12 +414,12 @@
  * Note: for speed reasons block size should be a power of 2 !
  */
-#define VDI_IMAGE_DEFAULT_BLOCK_SIZE   (1 * 1024 * 1024)
+#define VDI_IMAGE_DEFAULT_BLOCK_SIZE            _1M
 
 /**
  * fModified bit flags.
  */
-#define VDI_IMAGE_MODIFIED_FLAG                 (0x01)
-#define VDI_IMAGE_MODIFIED_FIRST                (0x02)
-#define VDI_IMAGE_MODIFIED_DISABLE_UUID_UPDATE  (0x04)
+#define VDI_IMAGE_MODIFIED_FLAG                 BIT(0)
+#define VDI_IMAGE_MODIFIED_FIRST                BIT(1)
+#define VDI_IMAGE_MODIFIED_DISABLE_UUID_UPDATE  BIT(2)
 
 /**
@@ -533,29 +526,19 @@
 static unsigned getPowerOfTwo(unsigned uNumber);
 static void vdiInitPreHeader(PVDIPREHEADER pPreHdr);
-static int vdiValidatePreHeader(PVDIPREHEADER pPreHdr);
-static void vdiInitHeader(PVDIHEADER pHeader,
-                          VDIIMAGETYPE enmType,
-                          uint32_t fFlags,
-                          const char *pszComment,
-                          uint64_t cbDisk,
-                          uint32_t cbBlock,
+static int  vdiValidatePreHeader(PVDIPREHEADER pPreHdr);
+static void vdiInitHeader(PVDIHEADER pHeader, VDIIMAGETYPE enmType, uint32_t fFlags,
+                          const char *pszComment, uint64_t cbDisk, uint32_t cbBlock,
                           uint32_t cbBlockExtra);
-static int vdiValidateHeader(PVDIHEADER pHeader);
-static int vdiCreateImage(const char *pszFilename,
-                          VDIIMAGETYPE enmType,
-                          unsigned fFlags,
-                          uint64_t cbSize,
-                          const char *pszComment,
-                          PVDIIMAGEDESC pParent,
+static int  vdiValidateHeader(PVDIHEADER pHeader);
+static int  vdiCreateImage(const char *pszFilename, VDIIMAGETYPE enmType, unsigned fFlags,
+                          uint64_t cbSize, const char *pszComment, PVDIIMAGEDESC pParent,
                           PFNVMPROGRESS pfnProgress, void *pvUser);
 static void vdiInitImageDesc(PVDIIMAGEDESC pImage);
 static void vdiSetupImageDesc(PVDIIMAGEDESC pImage);
-static int vdiOpenImage(PVDIIMAGEDESC *ppImage,
-                        const char *pszFilename,
-                        unsigned fOpen,
+static int  vdiOpenImage(PVDIIMAGEDESC *ppImage, const char *pszFilename, unsigned fOpen,
                         PVDIIMAGEDESC pParent);
-static int vdiUpdateHeader(PVDIIMAGEDESC pImage);
-static int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock);
-static int vdiUpdateBlocks(PVDIIMAGEDESC pImage);
+static int  vdiUpdateHeader(PVDIIMAGEDESC pImage);
+static int  vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock);
+static int  vdiUpdateBlocks(PVDIIMAGEDESC pImage);
 static void vdiSetModifiedFlag(PVDIIMAGEDESC pImage);
 static void vdiResetModifiedFlag(PVDIIMAGEDESC pImage);
@@ -566,32 +549,41 @@
 static void vdiFlushImage(PVDIIMAGEDESC pImage);
 static void vdiCloseImage(PVDIIMAGEDESC pImage);
-static int vdiReadInBlock(PVDIIMAGEDESC pImage, unsigned uBlock, unsigned offRead, unsigned cbToRead, void *pvBuf);
-static int vdiFillBlockByZeroes(PVDIDISK pDisk, PVDIIMAGEDESC pImage, unsigned uBlock);
-static int vdiWriteInBlock(PVDIDISK pDisk, PVDIIMAGEDESC pImage, unsigned uBlock, unsigned offWrite, unsigned cbToWrite, const void *pvBuf);
-static int vdiCopyBlock(PVDIDISK pDisk, PVDIIMAGEDESC pImage, unsigned uBlock);
-static int vdiMergeImages(PVDIIMAGEDESC pImageFrom, PVDIIMAGEDESC pImageTo, bool fParentToChild,
+static int  vdiReadInBlock(PVDIIMAGEDESC pImage, unsigned uBlock, unsigned offRead,
+                           unsigned cbToRead, void *pvBuf);
+static int  vdiFillBlockByZeroes(PVDIDISK pDisk, PVDIIMAGEDESC pImage, unsigned uBlock);
+static int  vdiWriteInBlock(PVDIDISK pDisk, PVDIIMAGEDESC pImage, unsigned uBlock,
+                            unsigned offWrite, unsigned cbToWrite, const void *pvBuf);
+static int  vdiCopyBlock(PVDIDISK pDisk, PVDIIMAGEDESC pImage, unsigned uBlock);
+static int  vdiMergeImages(PVDIIMAGEDESC pImageFrom, PVDIIMAGEDESC pImageTo, bool fParentToChild,
                           PFNVMPROGRESS pfnProgress, void *pvUser);
-static int vdiCommitToImage(PVDIDISK pDisk, PVDIIMAGEDESC pDstImage,
-                            PFNVMPROGRESS pfnProgress, void *pvUser);
 static void vdiInitVDIDisk(PVDIDISK pDisk);
 static void vdiAddImageToList(PVDIDISK pDisk, PVDIIMAGEDESC pImage);
 static void vdiRemoveImageFromList(PVDIDISK pDisk, PVDIIMAGEDESC pImage);
 static PVDIIMAGEDESC vdiGetImageByNumber(PVDIDISK pDisk, int nImage);
-static int vdiChangeImageMode(PVDIIMAGEDESC pImage, bool fReadOnly);
-static int vdiUpdateReadOnlyHeader(PVDIIMAGEDESC pImage);
+static int  vdiChangeImageMode(PVDIIMAGEDESC pImage, bool fReadOnly);
+static int  vdiUpdateReadOnlyHeader(PVDIIMAGEDESC pImage);
+
+static int  vdiCommitToImage(PVDIDISK pDisk, PVDIIMAGEDESC pDstImage,
+                            PFNVMPROGRESS pfnProgress, void *pvUser);
 static void vdiDumpImage(PVDIIMAGEDESC pImage);
 
-static DECLCALLBACK(int) vdiConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle);
+static DECLCALLBACK(int)  vdiConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle);
 static DECLCALLBACK(void) vdiDestruct(PPDMDRVINS pDrvIns);
-static DECLCALLBACK(int) vdiRead(PPDMIMEDIA pInterface, uint64_t off, void *pvBuf, size_t cbRead);
-static DECLCALLBACK(int) vdiWrite(PPDMIMEDIA pInterface, uint64_t off, const void *pvBuf, size_t cbWrite);
-static DECLCALLBACK(int) vdiFlush(PPDMIMEDIA pInterface);
+static DECLCALLBACK(int)  vdiRead(PPDMIMEDIA pInterface,
+                                  uint64_t off, void *pvBuf, size_t cbRead);
+static DECLCALLBACK(int)  vdiWrite(PPDMIMEDIA pInterface,
+                                   uint64_t off, const void *pvBuf, size_t cbWrite);
+static DECLCALLBACK(int)  vdiFlush(PPDMIMEDIA pInterface);
 static DECLCALLBACK(uint64_t) vdiGetSize(PPDMIMEDIA pInterface);
-static DECLCALLBACK(int) vdiBiosGetGeometry(PPDMIMEDIA pInterface, uint32_t *pcCylinders, uint32_t *pcHeads, uint32_t *pcSectors);
-static DECLCALLBACK(int) vdiBiosSetGeometry(PPDMIMEDIA pInterface, uint32_t cCylinders, uint32_t cHeads, uint32_t cSectors);
-static DECLCALLBACK(int) vdiGetUuid(PPDMIMEDIA pInterface, PRTUUID pUuid);
+static DECLCALLBACK(int)  vdiBiosGetGeometry(PPDMIMEDIA pInterface, uint32_t *pcCylinders,
+                                             uint32_t *pcHeads, uint32_t *pcSectors);
+static DECLCALLBACK(int)  vdiBiosSetGeometry(PPDMIMEDIA pInterface, uint32_t cCylinders,
+                                             uint32_t cHeads, uint32_t cSectors);
+static DECLCALLBACK(int)  vdiGetUuid(PPDMIMEDIA pInterface, PRTUUID pUuid);
 static DECLCALLBACK(bool) vdiIsReadOnly(PPDMIMEDIA pInterface);
-static DECLCALLBACK(int) vdiBiosGetTranslation(PPDMIMEDIA pInterface, PPDMBIOSTRANSLATION penmTranslation);
-static DECLCALLBACK(int) vdiBiosSetTranslation(PPDMIMEDIA pInterface, PDMBIOSTRANSLATION enmTranslation);
+static DECLCALLBACK(int)  vdiBiosGetTranslation(PPDMIMEDIA pInterface,
+                                                PPDMBIOSTRANSLATION penmTranslation);
+static DECLCALLBACK(int)  vdiBiosSetTranslation(PPDMIMEDIA pInterface,
+                                                PDMBIOSTRANSLATION enmTranslation);
 static DECLCALLBACK(void *) vdiQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface);
 
@@ -643,10 +635,6 @@
  * @param   pHeader     Assumes it was initially initialized to all zeros.
  */
-static void vdiInitHeader(PVDIHEADER pHeader,
-                          VDIIMAGETYPE enmType,
-                          uint32_t fFlags,
-                          const char *pszComment,
-                          uint64_t cbDisk,
-                          uint32_t cbBlock,
+static void vdiInitHeader(PVDIHEADER pHeader, VDIIMAGETYPE enmType, uint32_t fFlags,
+                          const char *pszComment, uint64_t cbDisk, uint32_t cbBlock,
                           uint32_t cbBlockExtra)
 {
@@ -755,16 +743,24 @@
     /* Check common header parameters. */
 
+    bool failure = false;
+
     if (    getImageType(pHeader) < VDI_IMAGE_TYPE_FIRST
         ||  getImageType(pHeader) > VDI_IMAGE_TYPE_LAST)
-        return VERR_VDI_INVALID_TYPE;
+    {
+        LogRel(("VDI: bad image type %d\n", getImageType(pHeader)));
+        failure = true;
+    }
 
     if (getImageFlags(pHeader) & ~VDI_IMAGE_FLAGS_MASK)
-        return VERR_VDI_INVALID_FLAGS;
+    {
+        LogRel(("VDI: bad image flags %08x\n", getImageFlags(pHeader)));
+        failure = true;
+    }
 
     if ((getImageGeometry(pHeader))->cbSector != VDI_GEOMETRY_SECTOR_SIZE)
     {
-        LogRel(("VDI: wrong section size (%d != %d)\n",
+        LogRel(("VDI: wrong sector size (%d != %d)\n",
                (getImageGeometry(pHeader))->cbSector, VDI_GEOMETRY_SECTOR_SIZE));
-        return VERR_VDI_INVALID_HEADER;
+        failure = true;
     }
 
@@ -777,5 +773,5 @@
               getImageDiskSize(pHeader), getImageBlockSize(pHeader),
               getImageBlocks(pHeader), getPowerOfTwo(getImageBlockSize(pHeader))));
-        return VERR_VDI_INVALID_HEADER;
+        failure = true;
     }
 
@@ -786,5 +782,5 @@
               getImageBlocksAllocated(pHeader), getImageBlocks(pHeader),
               getImageBlockSize(pHeader), getImageDiskSize(pHeader)));
-        return VERR_VDI_INVALID_HEADER;
+        failure = true;
     }
 
@@ -794,14 +790,12 @@
         LogRel(("VDI: wrong extra size (%d, %d)\n",
                getImageExtraBlockSize(pHeader), getPowerOfTwo(getImageExtraBlockSize(pHeader))));
-        return VERR_VDI_INVALID_HEADER;
-    }
-
-    if ( (uint64_t)getImageBlockSize(pHeader) * getImageBlocks(pHeader) < getImageDiskSize(pHeader))
+        failure = true;
+    }
+
+    if ((uint64_t)getImageBlockSize(pHeader) * getImageBlocks(pHeader) < getImageDiskSize(pHeader))
     {
         LogRel(("VDI: wrong disk size (%d, %d, %lld)\n",
-               getImageBlockSize(pHeader),
-               getImageBlocks(pHeader),
-               getImageDiskSize(pHeader)));
-        return VERR_VDI_INVALID_HEADER;
+               getImageBlockSize(pHeader), getImageBlocks(pHeader), getImageDiskSize(pHeader)));
+        failure = true;
     }
 
@@ -809,13 +803,14 @@
     {
         LogRel(("VDI: uuid of creator is 0\n"));
-        return VERR_VDI_INVALID_HEADER;
-    }
+        failure = true;
+    }
+
     if (RTUuidIsNull(getImageModificationUUID(pHeader)))
     {
         LogRel(("VDI: uuid of modificator is 0\n"));
-        return VERR_VDI_INVALID_HEADER;
-    }
-
-    return VINF_SUCCESS;
+        failure = true;
+    }
+
+    return failure ? VERR_VDI_INVALID_HEADER : VINF_SUCCESS;
 }
 
@@ -836,10 +831,11 @@
 static void vdiSetupImageDesc(PVDIIMAGEDESC pImage)
 {
-    pImage->fFlags = getImageFlags(&pImage->Header);
-    pImage->offStartBlocks = getImageBlocksOffset(&pImage->Header);
-    pImage->offStartData = getImageDataOffset(&pImage->Header);
-    pImage->uBlockMask = getImageBlockSize(&pImage->Header) - 1;
-    pImage->uShiftIndex2Offset = pImage->uShiftOffset2Index = getPowerOfTwo(getImageBlockSize(&pImage->Header));
-    pImage->offStartBlockData = getImageExtraBlockSize(&pImage->Header);
+    pImage->fFlags             = getImageFlags(&pImage->Header);
+    pImage->offStartBlocks     = getImageBlocksOffset(&pImage->Header);
+    pImage->offStartData       = getImageDataOffset(&pImage->Header);
+    pImage->uBlockMask         = getImageBlockSize(&pImage->Header) - 1;
+    pImage->uShiftIndex2Offset =
+    pImage->uShiftOffset2Index = getPowerOfTwo(getImageBlockSize(&pImage->Header));
+    pImage->offStartBlockData  = getImageExtraBlockSize(&pImage->Header);
     if (pImage->offStartBlockData != 0)
         pImage->uShiftIndex2Offset += getPowerOfTwo(pImage->offStartBlockData);
@@ -849,10 +845,6 @@
  * internal: create image.
  */
-static int vdiCreateImage(const char *pszFilename,
-                          VDIIMAGETYPE enmType,
-                          unsigned fFlags,
-                          uint64_t cbSize,
-                          const char *pszComment,
-                          PVDIIMAGEDESC pParent,
+static int vdiCreateImage(const char *pszFilename, VDIIMAGETYPE enmType, unsigned fFlags,
+                          uint64_t cbSize, const char *pszComment, PVDIIMAGEDESC pParent,
                           PFNVMPROGRESS pfnProgress, void *pvUser)
 {
@@ -1015,5 +1007,5 @@
                     if (pfnProgress)
                     {
-                        rc = pfnProgress(NULL /* Achtung! pVM=NULL  */,
+                        rc = pfnProgress(NULL /* WARNING! pVM=NULL  */,
                                          (unsigned)(((cbDisk - cbFill) * 100) / cbDisk),
                                          pvUser);
@@ -1048,5 +1040,5 @@
     if (    VBOX_SUCCESS(rc)
         &&  pfnProgress)
-        pfnProgress(NULL /* Achtung! pVM=NULL  */, 100, pvUser);
+        pfnProgress(NULL /* WARNING! pVM=NULL  */, 100, pvUser);
 
     Log(("vdiCreateImage: done, filename=\"%s\", rc=%Vrc\n", pszFilename, rc));
@@ -1059,8 +1051,6 @@
  * @internal
  */
-static int vdiOpenImage(PVDIIMAGEDESC *ppImage,
-                        const char *pszFilename,
-                        unsigned fOpen,
-                        PVDIIMAGEDESC pParent)
+static int vdiOpenImage(PVDIIMAGEDESC *ppImage, const char *pszFilename,
+                        unsigned fOpen, PVDIIMAGEDESC pParent)
 {
     /*
@@ -1151,4 +1141,5 @@
     if (VBOX_FAILURE(rc))
         goto l_open_failed;
+
     rc = vdiValidateHeader(&pImage->Header);
     if (VBOX_FAILURE(rc))
@@ -1181,6 +1172,8 @@
 
         /* Check linkage data. */
-        if (    RTUuidCompare(getImageParentUUID(&pImage->Header), getImageCreationUUID(&pParent->Header))
-            ||  RTUuidCompare(getImageParentModificationUUID(&pImage->Header), getImageModificationUUID(&pParent->Header)))
+        if (    RTUuidCompare(getImageParentUUID(&pImage->Header),
+                              getImageCreationUUID(&pParent->Header))
+            ||  RTUuidCompare(getImageParentModificationUUID(&pImage->Header),
+                              getImageModificationUUID(&pParent->Header)))
         {
             rc = VERR_VDI_IMAGES_UUID_MISMATCH;
@@ -1233,8 +1226,6 @@
     if (VBOX_FAILURE(rc))
         goto l_open_failed;
-    rc = RTFileRead(pImage->File,
-                    pImage->paBlocks,
-                    getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER),
-                    NULL);
+    rc = RTFileRead(pImage->File, pImage->paBlocks,
+                    getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER), NULL);
     if (VBOX_FAILURE(rc))
         goto l_open_failed;
@@ -1432,5 +1423,6 @@
  * note: uBlock must be valid, readed data must not overlap block bounds.
  */
-static int vdiReadInBlock(PVDIIMAGEDESC pImage, unsigned uBlock, unsigned offRead, unsigned cbToRead, void *pvBuf)
+static int vdiReadInBlock(PVDIIMAGEDESC pImage, unsigned uBlock, unsigned offRead,
+                          unsigned cbToRead, void *pvBuf)
 {
     if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
@@ -1610,14 +1602,14 @@
         }
 
+        unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
+        pImage->paBlocks[uBlock] = cBlocksAllocated;
+        setImageBlocksAllocated(&pImage->Header, cBlocksAllocated + 1);
+
         if (    pImage->fFlags & VDI_IMAGE_FLAGS_ZERO_EXPAND
             ||  pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO)
         {
             /* Fill newly allocated block by zeroes. */
-            unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
-            pImage->paBlocks[uBlock] = cBlocksAllocated;
-            setImageBlocksAllocated(&pImage->Header, cBlocksAllocated + 1);
-
-            if (    offWrite != 0
-                ||  cbToWrite != getImageBlockSize(&pImage->Header))
+
+            if (offWrite || cbToWrite != getImageBlockSize(&pImage->Header))
             {
                 rc = vdiFillBlockByZeroes(pDisk, pImage, uBlock);
@@ -1626,11 +1618,5 @@
             }
         }
-        else
-        {
-            /* No need to fill block by zeroes. */
-            unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
-            pImage->paBlocks[uBlock] = cBlocksAllocated;
-            setImageBlocksAllocated(&pImage->Header, cBlocksAllocated + 1);
-        }
+
         rc = vdiUpdateBlockInfo(pImage, uBlock);
         if (VBOX_FAILURE(rc))
@@ -1729,9 +1715,7 @@
 
     /* Calculate starting block number and offset inside it. */
-    unsigned uBlock = (unsigned)(offStart >> pImage->uShiftOffset2Index);
-    unsigned offWrite = (unsigned)offStart & pImage->uBlockMask;
-
-    /* Save block size here for speed optimization. */
-    unsigned cbBlock = getImageBlockSize(&pImage->Header);
+    unsigned uBlock   = (unsigned)(offStart >> pImage->uShiftOffset2Index);
+    unsigned offWrite = (unsigned)offStart   & pImage->uBlockMask;
+    unsigned cbBlock  = getImageBlockSize(&pImage->Header);
 
     /* loop through blocks */
@@ -1870,5 +1854,5 @@
             if (pfnProgress)
             {
-                pfnProgress(NULL /* Achtung! pVM=NULL  */,
+                pfnProgress(NULL /* WARNING! pVM=NULL  */,
                             (uBlock * 100) / cBlocks,
                             pvUser);
@@ -1931,5 +1915,5 @@
             if (pfnProgress)
             {
-                pfnProgress(NULL /* Achtung! pVM=NULL  */,
+                pfnProgress(NULL /* WARNING! pVM=NULL  */,
                             (uBlock * 100) / cBlocks,
                             pvUser);
@@ -1944,4 +1928,6 @@
 
 /**
+ * @note    Only used by tstVDI.
+ *
  * internal: commit last image(s) to selected previous image.
  * note: all images accessed across this call must be opened in R/W mode.
@@ -2032,5 +2018,5 @@
         if (pfnProgress)
         {
-            pfnProgress(NULL /* Achtung! pVM=NULL  */,
+            pfnProgress(NULL /* WARNING! pVM=NULL  */,
                         (uBlock * 100) / cBlocks,
                         pvUser);
@@ -2083,5 +2069,5 @@
     if (pfnProgress)
     {
-        pfnProgress(NULL /* Achtung! pVM=NULL  */, 100, pvUser);
+        pfnProgress(NULL /* WARNING! pVM=NULL */, 100, pvUser);
        /* Note: commiting is non breakable operation, skipping rc here. */
     }
@@ -2105,12 +2091,7 @@
  * @param   cbComment       The size of pszComment buffer. 0 is ok.
  */
-IDER3DECL(int) VDICheckImage(const char *pszFilename,
-                             unsigned *puVersion,
-                             PVDIIMAGETYPE penmType,
-                             uint64_t *pcbSize,
-                             PRTUUID pUuid,
-                             PRTUUID pParentUuid,
-                             char *pszComment,
-                             unsigned cbComment)
+IDER3DECL(int) VDICheckImage(const char *pszFilename, unsigned *puVersion, PVDIIMAGETYPE penmType,
+                             uint64_t *pcbSize, PRTUUID pUuid, PRTUUID pParentUuid,
+                             char *pszComment, unsigned cbComment)
 {
     LogFlow(("VDICheckImage:\n"));
@@ -2246,6 +2227,6 @@
  * @param   pvUser          User argument for the progress callback.
  */
-IDER3DECL(int) VDICreateBaseImage(const char *pszFilename, VDIIMAGETYPE enmType, uint64_t cbSize, const char *pszComment,
-                                  PFNVMPROGRESS pfnProgress, void *pvUser)
+IDER3DECL(int) VDICreateBaseImage(const char *pszFilename, VDIIMAGETYPE enmType, uint64_t cbSize,
+                                  const char *pszComment, PFNVMPROGRESS pfnProgress, void *pvUser)
 {
     LogFlow(("VDICreateBaseImage:\n"));
@@ -2278,6 +2259,7 @@
  * @param   pvUser          User argument for the progress callback.
  */
-IDER3DECL(int) VDICreateDifferenceImage(const char *pszFilename, const char *pszParent, const char *pszComment,
-                                        PFNVMPROGRESS pfnProgress, void *pvUser)
+IDER3DECL(int) VDICreateDifferenceImage(const char *pszFilename, const char *pszParent,
+                                        const char *pszComment, PFNVMPROGRESS pfnProgress,
+                                        void *pvUser)
 {
     LogFlow(("VDICreateDifferenceImage:\n"));
@@ -2343,6 +2325,6 @@
  * @param   pvUser          User argument for the progress callback.
  */
-IDER3DECL(int) VDICopyImage(const char *pszDstFilename, const char *pszSrcFilename, const char *pszComment,
-                            PFNVMPROGRESS pfnProgress, void *pvUser)
+IDER3DECL(int) VDICopyImage(const char *pszDstFilename, const char *pszSrcFilename,
+                            const char *pszComment, PFNVMPROGRESS pfnProgress, void *pvUser)
 {
     LogFlow(("VDICopyImage:\n"));
@@ -2461,5 +2443,5 @@
                             {
                                 c++;
-                                rc = pfnProgress(NULL /* Achtung! pVM=NULL  */,
+                                rc = pfnProgress(NULL /* WARNING! pVM=NULL  */,
                                                  (c * 100) / cBlocks,
                                                  pvUser);
@@ -2486,5 +2468,5 @@
 
         if (pfnProgress)
-            pfnProgress(NULL /* Achtung! pVM=NULL  */, 100, pvUser);
+            pfnProgress(NULL /* WARNING! pVM=NULL  */, 100, pvUser);
     }
 
@@ -2498,4 +2480,5 @@
 /**
  * Shrinks growing image file by removing zeroed data blocks.
+ * @note    Only used by vditool
  *
  * @returns VBox status code.
@@ -2534,6 +2517,6 @@
 
     /* Working data. */
-    unsigned cbBlock = getImageBlockSize(&pImage->Header);
-    unsigned cBlocks = getImageBlocks(&pImage->Header);
+    unsigned cbBlock          = getImageBlockSize(&pImage->Header);
+    unsigned cBlocks          = getImageBlocks(&pImage->Header);
     unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
 
@@ -2550,9 +2533,11 @@
     unsigned cBlocksAllocated2 = (unsigned)(cbData >> pImage->uShiftIndex2Offset);
     if (cbData != (uint64_t)cBlocksAllocated << pImage->uShiftIndex2Offset)
-        Log(("VDIShrinkImage: invalid image file length, cbBlock=%u cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu\n",
+        Log(("VDIShrinkImage: invalid image file length, cbBlock=%u cBlocks=%u "
+             "cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu\n",
              cbBlock, cBlocks, cBlocksAllocated, cBlocksAllocated2, cbData));
 
     /* Allocate second blocks array for back resolving. */
-    PVDIIMAGEBLOCKPOINTER paBlocks2 = (PVDIIMAGEBLOCKPOINTER)RTMemTmpAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * cBlocks);
+    PVDIIMAGEBLOCKPOINTER paBlocks2 =
+                (PVDIIMAGEBLOCKPOINTER)RTMemTmpAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * cBlocks);
     if (!paBlocks2)
     {
@@ -2586,5 +2571,6 @@
             else
             {
-                Log(("VDIShrinkImage: block n=%u -> uBlock=%u is out of blocks range! (cbBlock=%u cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu)\n",
+                Log(("VDIShrinkImage: block n=%u -> uBlock=%u is out of blocks range! (cbBlock=%u "
+                     "cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu)\n",
                      n, uBlock, cbBlock, cBlocks, cBlocksAllocated, cBlocksAllocated2, cbData));
                 /* free link to invalid block. */
@@ -2610,6 +2596,8 @@
                 if (VBOX_FAILURE(rc))
                 {
-                    Log(("VDIShrinkImage: seek rc=%Vrc filename=\"%s\" uBlock=%u cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu\n",
-                         rc, pImage->szFilename, uBlock, cBlocks, cBlocksAllocated, cBlocksAllocated2, cbData));
+                    Log(("VDIShrinkImage: seek rc=%Vrc filename=\"%s\" uBlock=%u cBlocks=%u "
+                         "cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu\n",
+                         rc, pImage->szFilename, uBlock, cBlocks, cBlocksAllocated,
+                         cBlocksAllocated2, cbData));
                     break;
                 }
@@ -2617,6 +2605,8 @@
                 if (VBOX_FAILURE(rc))
                 {
-                    Log(("VDIShrinkImage: read rc=%Vrc filename=\"%s\" cbBlock=%u uBlock=%u cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu\n",
-                         rc, pImage->szFilename, cbBlock, uBlock, cBlocks, cBlocksAllocated, cBlocksAllocated2, cbData));
+                    Log(("VDIShrinkImage: read rc=%Vrc filename=\"%s\" cbBlock=%u uBlock=%u "
+                         "cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu\n",
+                         rc, pImage->szFilename, cbBlock, uBlock, cBlocks, cBlocksAllocated,
+                         cBlocksAllocated2, cbData));
                     break;
                 }
@@ -2643,6 +2633,8 @@
                         if (VBOX_FAILURE(rc))
                         {
-                            Log(("VDIShrinkImage: seek(2) rc=%Vrc filename=\"%s\" uBlockWrite=%u cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu\n",
-                                 rc, pImage->szFilename, uBlockWrite, cBlocks, cBlocksAllocated, cBlocksAllocated2, cbData));
+                            Log(("VDIShrinkImage: seek(2) rc=%Vrc filename=\"%s\" uBlockWrite=%u "
+                                 "cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu\n",
+                                 rc, pImage->szFilename, uBlockWrite, cBlocks, cBlocksAllocated,
+                                 cBlocksAllocated2, cbData));
                             break;
                         }
@@ -2650,6 +2642,9 @@
                         if (VBOX_FAILURE(rc))
                         {
-                            Log(("VDIShrinkImage: write rc=%Vrc filename=\"%s\" cbBlock=%u uBlockWrite=%u cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu\n",
-                                 rc, pImage->szFilename, cbBlock, uBlockWrite, cBlocks, cBlocksAllocated, cBlocksAllocated2, cbData));
+                            Log(("VDIShrinkImage: write rc=%Vrc filename=\"%s\" cbBlock=%u "
+                                 "uBlockWrite=%u cBlocks=%u cBlocksAllocated=%u "
+                                 "cBlocksAllocated2=%u cbData=%llu\n",
+                                 rc, pImage->szFilename, cbBlock, uBlockWrite, cBlocks,
+                                 cBlocksAllocated, cBlocksAllocated2, cbData));
                             break;
                         }
@@ -2672,5 +2667,5 @@
             if (pfnProgress)
             {
-                pfnProgress(NULL /* Achtung! pVM=NULL  */,
+                pfnProgress(NULL /* WARNING! pVM=NULL  */,
                             (uBlock * 100) / cBlocksAllocated2,
                             pvUser);
@@ -2707,5 +2702,5 @@
         rc = vdiUpdateBlocks(pImage);
         if (pfnProgress)
-            pfnProgress(NULL /* Achtung! pVM=NULL  */, 100, pvUser);
+            pfnProgress(NULL /* WARNING! pVM=NULL */, 100, pvUser);
     }
 
@@ -2723,4 +2718,5 @@
 /**
  * Converts image file from older VDI formats to current one.
+ * @note    Only used by vditool
  *
  * @returns VBox status code.
@@ -2849,5 +2845,5 @@
                 {
                     c++;
-                    pfnProgress(NULL /* Achtung! pVM=NULL  */,
+                    pfnProgress(NULL /* WARNING! pVM=NULL  */,
                                 (c * 100) / cMoves,
                                 pvUser);
@@ -2888,5 +2884,5 @@
     if (pfnProgress)
     {
-        pfnProgress(NULL /* Achtung! pVM=NULL  */, 100, pvUser);
+        pfnProgress(NULL /* WARNING! pVM=NULL  */, 100, pvUser);
        /* Note: conversion is non breakable operation, skipping rc here. */
     }
@@ -3155,5 +3151,5 @@
     if (pfnProgress)
     {
-        pfnProgress(NULL /* Achtung! pVM=NULL  */, 100, pvUser);
+        pfnProgress(NULL /* WARNING! pVM=NULL */, 100, pvUser);
        /* Note: commiting is non breakable operation, skipping rc here. */
     }
@@ -4007,4 +4003,6 @@
  * Note that in case of unrecoverable error all images of HDD container will be closed.
  *
+ * @note    Only used by tstVDI.
+ *
  * @returns VBox status code.
  * @param   pDisk           Pointer to VDI HDD container.
@@ -4069,4 +4067,6 @@
 
 /**
+ * @note    Only used by tstVDI.
+ *
  * Creates and opens a new differencing image file in HDD container.
  * See comments for VDIDiskOpenImage function about differencing images.
@@ -4079,6 +4079,7 @@
  * @param   pvUser          User argument for the progress callback.
  */
-IDER3DECL(int) VDIDiskCreateOpenDifferenceImage(PVDIDISK pDisk, const char *pszFilename, const char *pszComment,
-                                                PFNVMPROGRESS pfnProgress, void *pvUser)
+IDER3DECL(int) VDIDiskCreateOpenDifferenceImage(PVDIDISK pDisk, const char *pszFilename,
+                                                const char *pszComment, PFNVMPROGRESS pfnProgress,
+                                                void *pvUser)
 {
     LogFlow(("VDIDiskCreateOpenDifferenceImage:\n"));
@@ -4115,4 +4116,6 @@
 
 /**
+ * @note    Only used by tstVDI.
+ *
  * internal: debug image dump.
  */
@@ -4138,5 +4141,5 @@
                 getImageDataOffset(&pImage->Header));
     PVDIDISKGEOMETRY pg = getImageGeometry(&pImage->Header);
-    RTLogPrintf("Header->Geometry: C/H/S=%u/%u/%u cbSector=%u Mode=%u\n",
+    RTLogPrintf("Header: Geometry: C/H/S=%u/%u/%u cbSector=%u Mode=%u\n",
                 pg->cCylinders, pg->cHeads, pg->cSectors, pg->cbSector,
                 getImageTranslation(&pImage->Header));
@@ -4146,15 +4149,37 @@
     if (GET_MAJOR_HEADER_VERSION(&pImage->Header) >= 1)
         RTLogPrintf("Header: uuidParentModification={%Vuuid}\n", getImageParentModificationUUID(&pImage->Header));
-    RTLogPrintf("Image: fFlags=%08X offStartBlocks=%u offStartData=%u\n",
+    RTLogPrintf("Image:  fFlags=%08X offStartBlocks=%u offStartData=%u\n",
                 pImage->fFlags, pImage->offStartBlocks, pImage->offStartData);
-    RTLogPrintf("Image: uBlockMask=%08X uShiftIndex2Offset=%u uShiftOffset2Index=%u offStartBlockData=%u\n",
+    RTLogPrintf("Image:  uBlockMask=%08X uShiftIndex2Offset=%u uShiftOffset2Index=%u offStartBlockData=%u\n",
                 pImage->uBlockMask,
                 pImage->uShiftIndex2Offset,
                 pImage->uShiftOffset2Index,
                 pImage->offStartBlockData);
+
+    unsigned uBlock, cBlocksNotFree, cBadBlocks, cBlocks = getImageBlocks(&pImage->Header);
+    for (uBlock=0, cBlocksNotFree=0, cBadBlocks=0; uBlock<cBlocks; uBlock++)
+    {
+        if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
+        {
+            cBlocksNotFree++;
+            if (pImage->paBlocks[uBlock] >= cBlocks)
+                cBadBlocks++;
+        }
+    }
+    if (cBlocksNotFree != getImageBlocksAllocated(&pImage->Header))
+    {
+        RTLogPrintf("!! WARNING: %u blocks actually allocated (cBlocksAllocated=%u) !!\n",
+                cBlocksNotFree, getImageBlocksAllocated(&pImage->Header));
+    }
+    if (cBadBlocks)
+    {
+        RTLogPrintf("!! WARNING: %u bad blocks found !!\n",
+                cBadBlocks);
+    }
 }
 
 /**
  * Debug helper - dumps all opened images of HDD container into the log file.
+ * @note    Only used by tstVDI and vditool
  *
  * @param   pDisk           Pointer to VDI HDD container.
@@ -4243,8 +4268,7 @@
         int rc = CFGMR3QueryStringAlloc(pCurNode, "Path", &pszName);
         if (VBOX_FAILURE(rc))
-        {
-            AssertMsgFailed(("Configuration error: %d - query for \"Path\" string return %Vrc.\n", iLevel, rc));
-            return rc;
-        }
+            return PDMDRV_SET_ERROR(pDrvIns, rc,
+                                    N_("VHDD: Configuration error: Querying \"Path\" as "
+                                       "string failed"));
 
         bool fReadOnly;
@@ -4254,7 +4278,8 @@
         else if (VBOX_FAILURE(rc))
         {
-            AssertMsgFailed(("Configuration error: %d - query for \"ReadOnly\" boolean return %Vrc.\n", iLevel, rc));
             MMR3HeapFree(pszName);
-            return rc;
+            return PDMDRV_SET_ERROR(pDrvIns, rc,
+                                    N_("VHDD: Configuration error: Querying \"ReadOnly\" as "
+                                       "boolean failed"));
         }
 
@@ -4262,5 +4287,6 @@
          * Open the image.
          */
-        rc = VDIDiskOpenImage(pData, pszName, !fReadOnly ? VDI_OPEN_FLAGS_NORMAL : VDI_OPEN_FLAGS_READONLY);
+        rc = VDIDiskOpenImage(pData, pszName, fReadOnly ? VDI_OPEN_FLAGS_READONLY
+                                                        : VDI_OPEN_FLAGS_NORMAL);
         if (VBOX_SUCCESS(rc))
             Log(("vdiConstruct: %d - Opened '%s' in %s mode\n",
@@ -4284,8 +4310,7 @@
     int rc = CFGMR3QueryStringAlloc(pCfgHandle, "Path", &pszName);
     if (VBOX_FAILURE(rc))
-    {
-        AssertMsgFailed(("Configuration error: query for \"Path\" string return %Vrc.\n", rc));
-        return rc;
-    }
+        return PDMDRV_SET_ERROR(pDrvIns, rc,
+                                N_("VHDD: Configuration error: Querying \"Path\" as "
+                                   "string failed"));
 
     /** True if the media is readonly. */
@@ -4296,6 +4321,8 @@
     else if (VBOX_FAILURE(rc))
     {
-        AssertMsgFailed(("Configuration error: query for \"ReadOnly\" boolean return %Vrc.\n", rc));
         MMR3HeapFree(pszName);
+        return PDMDRV_SET_ERROR(pDrvIns, rc,
+                                N_("VHDD: Configuration error: Querying \"ReadOnly\" as "
+                                   "boolean failed"));
         return rc;
     }
@@ -4304,7 +4331,9 @@
      * Open the image.
      */
-    rc = VDIDiskOpenImage(pData, pszName, !fReadOnly ? VDI_OPEN_FLAGS_NORMAL : VDI_OPEN_FLAGS_READONLY);
+    rc = VDIDiskOpenImage(pData, pszName, fReadOnly ? VDI_OPEN_FLAGS_READONLY
+                                                    : VDI_OPEN_FLAGS_NORMAL);
     if (VBOX_SUCCESS(rc))
-        Log(("vdiConstruct: Opened '%s' in %s mode\n", pszName, VDIDiskIsReadOnly(pData) ? "read-only" : "read-write"));
+        Log(("vdiConstruct: Opened '%s' in %s mode\n",
+              pszName, VDIDiskIsReadOnly(pData) ? "read-only" : "read-write"));
     else
         AssertMsgFailed(("Failed to open image '%s' rc=%Vrc\n", pszName, rc));
@@ -4325,5 +4354,5 @@
  * @param   pDrvIns     The driver instance data.
  */
-static DECLCALLBACK(void)   vdiDestruct(PPDMDRVINS pDrvIns)
+static DECLCALLBACK(void) vdiDestruct(PPDMDRVINS pDrvIns)
 {
     LogFlow(("vdiDestruct:\n"));
@@ -4480,5 +4509,6 @@
 
 /** @copydoc PDMIMEDIA::pfnBiosGetTranslation */
-static DECLCALLBACK(int) vdiBiosGetTranslation(PPDMIMEDIA pInterface, PPDMBIOSTRANSLATION penmTranslation)
+static DECLCALLBACK(int) vdiBiosGetTranslation(PPDMIMEDIA pInterface,
+                                               PPDMBIOSTRANSLATION penmTranslation)
 {
     PVDIDISK pData = PDMIMEDIA_2_VDIDISK(pInterface);
@@ -4489,5 +4519,6 @@
 
 /** @copydoc PDMIMEDIA::pfnBiosSetTranslation */
-static DECLCALLBACK(int) vdiBiosSetTranslation(PPDMIMEDIA pInterface, PDMBIOSTRANSLATION enmTranslation)
+static DECLCALLBACK(int) vdiBiosSetTranslation(PPDMIMEDIA pInterface,
+                                               PDMBIOSTRANSLATION enmTranslation)
 {
     PVDIDISK pData = PDMIMEDIA_2_VDIDISK(pInterface);
