From 4b59b86a191a43d88423a1f2659392b8bd9ff849 Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Wed, 26 Oct 2022 16:57:41 +0800
Subject: [PATCH 33/43] device mapper bugfix

Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
 .../layer_store/graphdriver/devmapper/driver_devmapper.c | 9 +++++++--
 .../graphdriver/devmapper/wrapper_devmapper.c            | 8 ++++----
 2 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/driver_devmapper.c b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/driver_devmapper.c
index 0a4e5e57..dd231bd6 100644
--- a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/driver_devmapper.c
+++ b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/driver_devmapper.c
@@ -231,6 +231,9 @@ char *devmapper_mount_layer(const char *id, const struct graphdriver *driver,
     if (rootfs == NULL) {
         ERROR("Failed to join devmapper rootfs %s", mnt_point_dir);
         ret = -1;
+        if (unmount_device(id, mnt_point_dir, driver->devset) != 0) {
+            DEBUG("devmapper: unmount %s failed", mnt_point_dir);
+        }
         goto out;
     }
 
@@ -246,6 +249,7 @@ char *devmapper_mount_layer(const char *id, const struct graphdriver *driver,
     id_file = util_path_join(mnt_point_dir, "id");
     if (!util_file_exists(id_file)) {
         if (util_atomic_write_file(id_file, id, strlen(id), SECURE_CONFIG_FILE_MODE, true) != 0) {
+            ret = -1;
             if (unmount_device(id, mnt_point_dir, driver->devset) != 0) {
                 DEBUG("devmapper: unmount %s failed", mnt_point_dir);
             }
@@ -350,13 +354,14 @@ int devmapper_apply_diff(const char *id, const struct graphdriver *driver, const
     if (archive_unpack(content, layer_fs, &options, &err) != 0) {
         ERROR("devmapper: failed to unpack to %s: %s", layer_fs, err);
         ret = -1;
-        goto out;
+        goto umount_out;
     }
 
+umount_out:
+    // umount layer if devmapper_mount_layer success
     if (devmapper_umount_layer(id, driver) != 0) {
         ERROR("devmapper: failed to umount layer %s", id);
         ret = -1;
-        goto out;
     }
 
 out:
diff --git a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/wrapper_devmapper.c b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/wrapper_devmapper.c
index 7ae1fd40..c8f3d6d8 100644
--- a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/wrapper_devmapper.c
+++ b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/wrapper_devmapper.c
@@ -28,10 +28,10 @@
 #include "utils.h"
 #include "err_msg.h"
 
-static bool g_dm_saw_busy = false;
-static bool g_dm_saw_exist = false;
-static bool g_dm_saw_enxio = false; // no such device or address
-static bool g_dm_saw_eno_data = false; // no data available
+static __thread bool g_dm_saw_busy = false;
+static __thread bool g_dm_saw_exist = false;
+static __thread bool g_dm_saw_enxio = false; // no such device or address
+static __thread bool g_dm_saw_eno_data = false; // no data available
 static int64_t dm_udev_wait_timeout = 0;
 
 char *dev_strerror(int errnum)
-- 
2.25.1

