Index: /trunk/include/iprt/err.h
===================================================================
--- /trunk/include/iprt/err.h	(revision 75992)
+++ /trunk/include/iprt/err.h	(revision 75993)
@@ -1091,4 +1091,6 @@
 /** Requires process elevation (UAC). */
 #define VERR_PROC_ELEVATION_REQUIRED        (-22419)
+/** Incompatible configuration requested. */
+#define VERR_INCOMPATIBLE_CONFIG            (-22420)
 /** @} */
 
Index: /trunk/src/VBox/HostServices/SharedFolders/mappings.cpp
===================================================================
--- /trunk/src/VBox/HostServices/SharedFolders/mappings.cpp	(revision 75992)
+++ /trunk/src/VBox/HostServices/SharedFolders/mappings.cpp	(revision 75993)
@@ -32,4 +32,5 @@
 #include <iprt/path.h>
 #include <iprt/string.h>
+#include <VBox/AssertGuest.h>
 
 #ifdef UNITTEST
@@ -722,4 +723,7 @@
     }
 
+    SHFLROOT RootTmp;
+    if (!pRoot)
+        pRoot = &RootTmp;
     if (BIT_FLAG(pClient->fu32Flags, SHFL_CF_UTF8))
     {
@@ -727,10 +731,10 @@
         PRTUTF16 utf16Name;
 
-        rc = RTStrToUtf16 ((const char *) pszMapName->String.utf8, &utf16Name);
+        rc = RTStrToUtf16((const char *) pszMapName->String.utf8, &utf16Name);
         if (RT_FAILURE (rc))
             return rc;
 
         pFolderMapping = vbsfMappingGetByName(utf16Name, pRoot);
-        RTUtf16Free (utf16Name);
+        RTUtf16Free(utf16Name);
     }
     else
@@ -744,7 +748,26 @@
     }
 
+    /*
+     * Check for reference count overflows and settings compatibility.
+     * For paranoid reasons, we don't allow modifying the case sensitivity
+     * setting while there are other mappings of a folder.
+     */
+    AssertLogRelReturn(*pRoot < RT_ELEMENTS(pClient->acMappings), VERR_INTERNAL_ERROR);
+    AssertLogRelReturn(!pClient->fHasMappingCounts || pClient->acMappings[*pRoot] < _32K, VERR_TOO_MANY_OPENS);
+    ASSERT_GUEST_LOGREL_MSG_RETURN(   pFolderMapping->cMappings == 0
+                                   || pFolderMapping->fGuestCaseSensitive == fCaseSensitive,
+                                   ("Incompatible case sensitivity setting: %s: %u mappings, %ssenitive, requested %ssenitive!\n",
+                                    pFolderMapping->pszFolderName, pFolderMapping->cMappings,
+                                    pFolderMapping->fGuestCaseSensitive ? "" : "in",  fCaseSensitive ? "" : "in"),
+                                   VERR_INCOMPATIBLE_CONFIG);
+
+    /*
+     * Go ahead and map it.
+     */
+    if (pClient->fHasMappingCounts)
+        pClient->acMappings[*pRoot] += 1;
     pFolderMapping->cMappings++;
-    Assert(pFolderMapping->cMappings == 1 || pFolderMapping->fGuestCaseSensitive == fCaseSensitive);
     pFolderMapping->fGuestCaseSensitive = fCaseSensitive;
+    Log(("vbsfMmapFolder (cMappings=%u, acMappings[%u]=%u)\n", pFolderMapping->cMappings, *pRoot, pClient->acMappings[*pRoot]));
     return VINF_SUCCESS;
 }
@@ -775,10 +798,17 @@
         return VERR_FILE_NOT_FOUND;
     }
-
     Assert(pFolderMapping->fValid == true && pFolderMapping->cMappings > 0);
+
+    AssertLogRelReturn(root < RT_ELEMENTS(pClient->acMappings), VERR_INTERNAL_ERROR);
+    AssertLogRelReturn(!pClient->fHasMappingCounts || pClient->acMappings[root] > 0, VERR_INVALID_HANDLE);
+
+    if (pClient->fHasMappingCounts)
+        pClient->acMappings[root] -= 1;
+
     if (pFolderMapping->cMappings > 0)
         pFolderMapping->cMappings--;
 
-    if (   pFolderMapping->cMappings == 0
+    uint32_t const cMappings = pFolderMapping->cMappings;
+    if (   cMappings == 0
         && pFolderMapping->fPlaceholder)
     {
@@ -790,5 +820,5 @@
     }
 
-    Log(("vbsfUnmapFolder\n"));
+    Log(("vbsfUnmapFolder (cMappings=%u, acMappings[%u]=%u)\n", cMappings, root, pClient->acMappings[root]));
     return rc;
 }
Index: /trunk/src/VBox/HostServices/SharedFolders/service.cpp
===================================================================
--- /trunk/src/VBox/HostServices/SharedFolders/service.cpp	(revision 75992)
+++ /trunk/src/VBox/HostServices/SharedFolders/service.cpp	(revision 75993)
@@ -156,10 +156,10 @@
 static DECLCALLBACK(int) svcConnect (void *, uint32_t u32ClientID, void *pvClient, uint32_t fRequestor, bool fRestoring)
 {
-    RT_NOREF(u32ClientID, pvClient, fRequestor, fRestoring);
-    int rc = VINF_SUCCESS;
-
+    RT_NOREF(u32ClientID, fRequestor, fRestoring);
+    SHFLCLIENTDATA *pClient = (SHFLCLIENTDATA *)pvClient;
     Log(("SharedFolders host service: connected, u32ClientID = %u\n", u32ClientID));
 
-    return rc;
+    pClient->fHasMappingCounts = true;
+    return VINF_SUCCESS;
 }
 
@@ -264,12 +264,15 @@
         return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
 
-    /* Restore the client data (flags + path delimiter at the moment) */
+    /* Restore the client data (flags + path delimiter + mapping counts (new) at the moment) */
     rc = SSMR3GetU32(pSSM, &len);
     AssertRCReturn(rc, rc);
 
-    if (len != sizeof(*pClient))
-        return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
-
-    rc = SSMR3GetMem(pSSM, pClient, sizeof(*pClient));
+    if (len == RT_UOFFSETOF(SHFLCLIENTDATA, acMappings))
+        pClient->fHasMappingCounts = false;
+    else if (len != sizeof(*pClient))
+        return SSMR3SetLoadError(pSSM, VERR_SSM_DATA_UNIT_FORMAT_CHANGED, RT_SRC_POS,
+                                 "Saved SHFLCLIENTDATA size %u differs from current %u!\n", len, sizeof(*pClient));
+
+    rc = SSMR3GetMem(pSSM, pClient, len);
     AssertRCReturn(rc, rc);
 
Index: /trunk/src/VBox/HostServices/SharedFolders/shfl.h
===================================================================
--- /trunk/src/VBox/HostServices/SharedFolders/shfl.h	(revision 75992)
+++ /trunk/src/VBox/HostServices/SharedFolders/shfl.h	(revision 75993)
@@ -20,4 +20,5 @@
 #include <VBox/err.h>
 #include <VBox/hgcmsvc.h>
+#include <VBox/shflsvc.h>
 
 #include <VBox/log.h>
@@ -54,4 +55,12 @@
     /** Path delimiter. */
     RTUTF16  PathDelimiter;
+    /** Currently unused.   */
+    uint8_t  bPadding;
+    /** Set if the client has mapping usage counts.
+     * This is for helping with saved state. */
+    uint8_t  fHasMappingCounts;
+    /** Mapping counts for each root ID so we can unmap the folders when the
+     *  session disconnects or the VM resets. */
+    uint16_t acMappings[SHFL_MAX_MAPPINGS];
 } SHFLCLIENTDATA;
 /** Pointer to a SHFLCLIENTDATA structure. */
Index: /trunk/src/VBox/HostServices/SharedFolders/vbsf.cpp
===================================================================
--- /trunk/src/VBox/HostServices/SharedFolders/vbsf.cpp	(revision 75992)
+++ /trunk/src/VBox/HostServices/SharedFolders/vbsf.cpp	(revision 75993)
@@ -2071,4 +2071,13 @@
         }
     }
+
+    for (uint32_t i = 0; i < RT_ELEMENTS(pClient->acMappings); i++)
+        if (pClient->acMappings[i])
+        {
+            uint16_t cMappings = pClient->acMappings[i];
+            while (cMappings-- > 0)
+                vbsfUnmapFolder(pClient, i);
+        }
+
     return VINF_SUCCESS;
 }
