libsnapshot: Fix unmapping getting skipped for userspace snapshots.

When the dm-user device isn't shut down, COW files cannot be deleted.

Bug: 208944665
Test: vts_libsnapshot_test
Change-Id: I350c8c88c69f9b151032635b0229d281a4bb47ce
diff --git a/fs_mgr/libsnapshot/snapshot.cpp b/fs_mgr/libsnapshot/snapshot.cpp
index f4584d2..e6e17bd 100644
--- a/fs_mgr/libsnapshot/snapshot.cpp
+++ b/fs_mgr/libsnapshot/snapshot.cpp
@@ -668,9 +668,15 @@
 bool SnapshotManager::UnmapSnapshot(LockedFile* lock, const std::string& name) {
     CHECK(lock);
 
-    if (!DeleteDeviceIfExists(name)) {
-        LOG(ERROR) << "Could not delete snapshot device: " << name;
-        return false;
+    if (UpdateUsesUserSnapshots(lock)) {
+        if (!UnmapUserspaceSnapshotDevice(lock, name)) {
+            return false;
+        }
+    } else {
+        if (!DeleteDeviceIfExists(name)) {
+            LOG(ERROR) << "Could not delete snapshot device: " << name;
+            return false;
+        }
     }
     return true;
 }
@@ -2429,10 +2435,8 @@
                                                  const std::string& target_partition_name) {
     CHECK(lock);
 
-    if (!UpdateUsesUserSnapshots(lock)) {
-        if (!UnmapSnapshot(lock, target_partition_name)) {
-            return false;
-        }
+    if (!UnmapSnapshot(lock, target_partition_name)) {
+        return false;
     }
 
     if (!UnmapCowDevices(lock, target_partition_name)) {
@@ -2530,16 +2534,10 @@
     CHECK(lock);
     if (!EnsureImageManager()) return false;
 
-    if (UpdateUsesCompression(lock)) {
-        if (UpdateUsesUserSnapshots(lock)) {
-            if (!UnmapUserspaceSnapshotDevice(lock, name)) {
-                return false;
-            }
-        } else {
-            auto dm_user_name = GetDmUserCowName(name, GetSnapshotDriver(lock));
-            if (!UnmapDmUserDevice(dm_user_name)) {
-                return false;
-            }
+    if (UpdateUsesCompression(lock) && !UpdateUsesUserSnapshots(lock)) {
+        auto dm_user_name = GetDmUserCowName(name, GetSnapshotDriver(lock));
+        if (!UnmapDmUserDevice(dm_user_name)) {
+            return false;
         }
     }