From 72a02695064384dc8565a22fb786fbd639770afa Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Mon, 5 Jun 2023 07:56:41 +0000
Subject: [PATCH 01/15] [RO] refactor remote ro code

---
 CI/pr-gateway.sh                              |  2 +-
 .../leftover_cleanup/leftover_cleanup_api.c   |  2 +-
 .../graphdriver/overlay2/driver_overlay2.c    | 36 +++++--------------
 .../oci/storage/layer_store/layer_store.c     | 21 ++++-------
 src/utils/http/parser.c                       |  2 +-
 5 files changed, 17 insertions(+), 46 deletions(-)

diff --git a/CI/pr-gateway.sh b/CI/pr-gateway.sh
index 93b07c44..b3da52d0 100755
--- a/CI/pr-gateway.sh
+++ b/CI/pr-gateway.sh
@@ -111,7 +111,7 @@ pushd iSulad
 rm -rf build
 mkdir build
 pushd build
-cmake -DDEBUG=ON -DCMAKE_INSTALL_PREFIX=/usr -DENABLE_UT=ON -DENABLE_SHIM_V2=OFF ../ || exit 1
+cmake -DDEBUG=ON -DCMAKE_INSTALL_PREFIX=/usr -DENABLE_UT=ON -DENABLE_REMOTE_LAYER_STORE=ON -DENABLE_SHIM_V2=OFF ../ || exit 1
 make -j $(nproc) || exit 1
 ctest -V
 popd
diff --git a/src/daemon/modules/container/leftover_cleanup/leftover_cleanup_api.c b/src/daemon/modules/container/leftover_cleanup/leftover_cleanup_api.c
index 8f077916..a20dbc3a 100644
--- a/src/daemon/modules/container/leftover_cleanup/leftover_cleanup_api.c
+++ b/src/daemon/modules/container/leftover_cleanup/leftover_cleanup_api.c
@@ -25,7 +25,7 @@ int clean_module_init(const isulad_daemon_configs *args)
     if (args->storage_enable_remote_layer) {
         // need to disable cleanup
         // cause cleanup may cleanup local broken RO layer
-        // while this RO layer is valid for remote 
+        // while this RO layer is valid for remote
         return 0;
     }
 
diff --git a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/overlay2/driver_overlay2.c b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/overlay2/driver_overlay2.c
index 5ad487f9..4f7be30d 100644
--- a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/overlay2/driver_overlay2.c
+++ b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/overlay2/driver_overlay2.c
@@ -867,7 +867,7 @@ out:
 static int do_create_remote_ro(const char *id, const char *parent, const struct graphdriver *driver,
                                const struct driver_create_opts *create_opts)
 {
-    int ret = 0;
+    int ret = -1;
     int get_err = 0;
     char *ro_symlink = NULL;
     char *ro_home = NULL;
@@ -879,69 +879,51 @@ static int do_create_remote_ro(const char *id, const char *parent, const struct
     ro_home = util_path_join(driver->home, OVERLAY_RO_DIR);
     if (ro_home == NULL) {
         ERROR("Failed to join ro_home");
-        ret = -1;
-        goto out;
+        return -1;
     }
 
     layer_dir = util_path_join(ro_home, id);
     if (layer_dir == NULL) {
-        ERROR("Failed to join layer_dir");
-        ret = -1;
+        ERROR("Failed to join layer dir: %s", id);
         goto out;
     }
 
     ro_symlink = util_path_join(driver->home, id);
     if (ro_symlink == NULL) {
         ERROR("Failed to join ro_symlink");
-        ret = -1;
-        goto out;
-    }
-
-    if (layer_dir == NULL) {
-        ERROR("Failed to join layer dir:%s", id);
-        ret = -1;
         goto out;
     }
 
     if (check_parent_valid(parent, driver) != 0) {
-        ret = -1;
         goto out;
     }
 
     if (util_mkdir_p(layer_dir, 0700) != 0) {
         ERROR("Unable to create layer directory %s.", layer_dir);
-        ret = -1;
         goto out;
     }
 
     // mk symbol link
     if (symlink(layer_dir, ro_symlink) != 0) {
         SYSERROR("Unable to create symbol link to layer directory %s", layer_dir);
-        ret = -1;
         goto err_out;
     }
 
 #ifdef ENABLE_USERNS_REMAP
     if (set_file_owner_for_userns_remap(layer_dir, userns_remap) != 0) {
         ERROR("Unable to change directory %s owner for user remap.", layer_dir);
-        ret = -1;
         goto out;
     }
 #endif
 
-    if (create_opts->storage_opt != NULL && create_opts->storage_opt->len != 0) {
-        if (set_layer_quota(layer_dir, create_opts->storage_opt, driver) != 0) {
-            ERROR("Unable to set layer quota %s", layer_dir);
-            ret = -1;
-            goto err_out;
-        }
-    }
+    // quota opts only setting on rw layer
 
     if (mk_sub_directories(id, parent, layer_dir, driver->home) != 0) {
-        ret = -1;
+        ERROR("Create layer: %s sub dir failed", id);
         goto err_out;
     }
 
+    ret = 0;
     goto out;
 
 err_out:
@@ -1118,12 +1100,10 @@ int overlay2_create_ro(const char *id, const char *parent, const struct graphdri
 #ifdef ENABLE_REMOTE_LAYER_STORE
     if (driver->enable_remote_layer) {
         return do_create_remote_ro(id, parent, driver, create_opts);
-    } else {
-        return do_create(id, parent, driver, create_opts);
     }
-#else
-    return do_create(id, parent, driver, create_opts);
 #endif
+
+    return do_create(id, parent, driver, create_opts);
 }
 
 static char *read_layer_link_file(const char *layer_dir)
diff --git a/src/daemon/modules/image/oci/storage/layer_store/layer_store.c b/src/daemon/modules/image/oci/storage/layer_store/layer_store.c
index 800a1cd7..08c7e4a6 100644
--- a/src/daemon/modules/image/oci/storage/layer_store/layer_store.c
+++ b/src/daemon/modules/image/oci/storage/layer_store/layer_store.c
@@ -1329,7 +1329,7 @@ driver_remove:
     if (ret != 0) {
         (void)graphdriver_rm_layer(lid);
 #ifdef ENABLE_REMOTE_LAYER_STORE
-        if (g_enable_remote_layer) {
+        if (g_enable_remote_layer && !opts->writable) {
             (void)remote_layer_remove_ro_dir(lid);
         } else {
             (void)layer_store_remove_layer(lid);
@@ -1412,14 +1412,10 @@ static int do_delete_layer(const char *id)
     }
 
 #ifdef ENABLE_REMOTE_LAYER_STORE
-    if (!g_enable_remote_layer) {
-        ret = layer_store_remove_layer(l->slayer->id);
+    if (g_enable_remote_layer && !(l->slayer->writable)) {
+        ret = remote_layer_remove_ro_dir(l->slayer->id);
     } else {
-        if (l->slayer->writable) {
-            ret = layer_store_remove_layer(l->slayer->id);
-        } else {
-            ret = remote_layer_remove_ro_dir(l->slayer->id);
-        }
+        ret = layer_store_remove_layer(l->slayer->id);
     }
 #else
     ret = layer_store_remove_layer(l->slayer->id);
@@ -2387,11 +2383,6 @@ int remote_layer_remove_memory_stores_with_lock(const char *id)
         goto unlock_out;
     }
 
-    if (map_search(g_metadata.by_id, (void *)id) == NULL) {
-        DEBUG("remote layer already removed, don't delete: %s", id);
-        goto unlock_out;
-    }
-
     ret = remove_memory_stores(id);
 
 unlock_out:
@@ -2412,13 +2403,13 @@ static layer_t *load_one_layer_from_json(const char *id)
     nret = snprintf(tmpdir, PATH_MAX, "%s/%s", g_root_dir, id);
     if (nret < 0 || nret >= PATH_MAX) {
         ERROR("Sprintf: %s failed", id);
-        goto free_out;
+        return NULL;
     }
 
     mount_point_path = mountpoint_json_path(id);
     if (mount_point_path == NULL) {
         ERROR("Out of Memory");
-        goto free_out;
+        return NULL;
     }
 
     rpath = layer_json_path(id);
diff --git a/src/utils/http/parser.c b/src/utils/http/parser.c
index a79893ba..30b26a00 100644
--- a/src/utils/http/parser.c
+++ b/src/utils/http/parser.c
@@ -105,7 +105,7 @@ static int parser_cb_header_value(http_parser *parser, const char *buf,
                                   size_t len)
 {
     struct parsed_http_message *m = parser->data;
-    
+
     if (m->num_headers == 0) {
         ERROR("Failed to parse header value because headers num is 0");
         return -1;
-- 
2.25.1

