Index: /trunk/src/VBox/Storage/QED.cpp
===================================================================
--- /trunk/src/VBox/Storage/QED.cpp	(revision 64831)
+++ /trunk/src/VBox/Storage/QED.cpp	(revision 64832)
@@ -207,4 +207,8 @@
     uint32_t            cL2Shift;
 
+    /** Pointer to the L2 table we are currently allocating
+     * (can be only one at a time). */
+    PQEDL2CACHEENTRY    pL2TblAlloc;
+
     /** Memory occupied by the L2 table cache. */
     size_t              cbL2Cache;
@@ -418,4 +422,11 @@
 static PQEDL2CACHEENTRY qedL2TblCacheRetain(PQEDIMAGE pImage, uint64_t offL2Tbl)
 {
+    if (   pImage->pL2TblAlloc
+        && pImage->pL2TblAlloc->offL2Tbl == offL2Tbl)
+    {
+        pImage->pL2TblAlloc->cRefs++;
+        return pImage->pL2TblAlloc;
+    }
+
     PQEDL2CACHEENTRY pL2Entry;
     RTListForEach(&pImage->ListSearch, pL2Entry, QEDL2CACHEENTRY, NodeSearch)
@@ -1328,8 +1339,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->cbImageOld);
             qedL2TblCacheEntryRelease(pClusterAlloc->pL2Entry); /* Release L2 cache entry. */
+            Assert(!pClusterAlloc->pL2Entry->cRefs);
             qedL2TblCacheEntryFree(pImage, pClusterAlloc->pL2Entry); /* Free it, it is not in the cache yet. */
             break;
@@ -1399,4 +1412,5 @@
             uint64_t offData = qedClusterAllocate(pImage, 1);
 
+            pImage->pL2TblAlloc = NULL;
             qedL2TblCacheEntryInsert(pImage, pClusterAlloc->pL2Entry);
 
@@ -1769,4 +1783,8 @@
                         pL2ClusterAlloc->cbToWrite     = cbToWrite;
                         pL2ClusterAlloc->pL2Entry      = pL2Entry;
+
+                        pImage->pL2TblAlloc = pL2Entry;
+
+                        LogFlowFunc(("Allocating new L2 table at cluster offset %llu\n", offL2Tbl));
 
                         /*
@@ -1791,4 +1809,6 @@
                     else
                     {
+                        LogFlowFunc(("Fetching L2 table at cluster offset %llu\n", pImage->paL1Table[idxL1]));
+
                         rc = qedL2TblCacheFetchAsync(pImage, pIoCtx, pImage->paL1Table[idxL1],
                                                      &pL2Entry);
