Index: /trunk/src/VBox/Storage/QCOW.cpp
===================================================================
--- /trunk/src/VBox/Storage/QCOW.cpp	(revision 64828)
+++ /trunk/src/VBox/Storage/QCOW.cpp	(revision 64829)
@@ -248,4 +248,8 @@
     uint32_t            cL2Shift;
 
+    /** Pointer to the L2 table we are currently allocating
+     * (can be only one at a time). */
+    PQCOWL2CACHEENTRY   pL2TblAlloc;
+
 } QCOWIMAGE, *PQCOWIMAGE;
 
@@ -500,4 +504,11 @@
 static PQCOWL2CACHEENTRY qcowL2TblCacheRetain(PQCOWIMAGE pImage, uint64_t offL2Tbl)
 {
+    if (   pImage->pL2TblAlloc
+        && pImage->pL2TblAlloc->offL2Tbl == offL2Tbl)
+    {
+        pImage->pL2TblAlloc->cRefs++;
+        return pImage->pL2TblAlloc;
+    }
+
     PQCOWL2CACHEENTRY pL2Entry;
     RTListForEach(&pImage->ListSearch, pL2Entry, QCOWL2CACHEENTRY, NodeSearch)
@@ -1322,8 +1333,10 @@
             /* Revert the L1 table entry */
             pImage->paL1Table[pClusterAlloc->idxL1] = 0;
+            pImage->pL2TblAlloc = NULL;
 
             /* Assumption right now is that the L1 table is not modified on storage if the link fails. */
             rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage, pClusterAlloc->offNextClusterOld);
             qcowL2TblCacheEntryRelease(pClusterAlloc->pL2Entry); /* Release L2 cache entry. */
+            Assert(!pClusterAlloc->pL2Entry->cRefs);
             qcowL2TblCacheEntryFree(pImage, pClusterAlloc->pL2Entry); /* Free it, it is not in the cache yet. */
             break;
@@ -1394,4 +1407,5 @@
             uint64_t offData = qcowClusterAllocate(pImage, 1);
 
+            pImage->pL2TblAlloc = NULL;
             qcowL2TblCacheEntryInsert(pImage, pClusterAlloc->pL2Entry);
 
@@ -1763,4 +1777,8 @@
                         pL2ClusterAlloc->cbToWrite         = cbToWrite;
                         pL2ClusterAlloc->pL2Entry          = pL2Entry;
+
+                        pImage->pL2TblAlloc = pL2Entry;
+
+                        LogFlowFunc(("Allocating new L2 table at cluster offset %llu\n", offL2Tbl));
 
                         /*
@@ -1785,4 +1803,6 @@
                     else
                     {
+                        LogFlowFunc(("Fetching L2 table at cluster offset %llu\n", pImage->paL1Table[idxL1]));
+
                         rc = qcowL2TblCacheFetch(pImage, pIoCtx, pImage->paL1Table[idxL1],
                                                  &pL2Entry);
