f338ef
From 52dc121c412de9c1cc3058d782b949dc7b25dc3e Mon Sep 17 00:00:00 2001
f338ef
From: Soumya Koduri <skoduri@redhat.com>
f338ef
Date: Thu, 25 Jul 2019 12:56:12 +0530
f338ef
Subject: [PATCH 264/265] gfapi: Fix deadlock while processing upcall
f338ef
f338ef
As mentioned in bug1733166, there could be potential deadlock
f338ef
while processing upcalls depending on how each xlator choose
f338ef
to act on it. The right way of fixing such issues
f338ef
is to change rpc callback communication process.
f338ef
- https://github.com/gluster/glusterfs/issues/697
f338ef
f338ef
Till then, making changes in gfapi layer to avoid any I/O
f338ef
processing.
f338ef
f338ef
This is backport of below mainline patch
f338ef
> https://review.gluster.org/#/c/glusterfs/+/23108/
f338ef
> bz#1733166
f338ef
> https://review.gluster.org/#/c/glusterfs/+/23107/ (release-6)
f338ef
f338ef
Change-Id: I2079e95339e5d761d5060707f4555cfacab95c83
f338ef
fixes: bz#1733520
f338ef
Signed-off-by: Soumya Koduri <skoduri@redhat.com>
f338ef
Reviewed-on: https://code.engineering.redhat.com/gerrit/177675
f338ef
Tested-by: RHGS Build Bot <nigelb@redhat.com>
f338ef
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
f338ef
---
f338ef
 api/src/glfs-fops.c | 164 +++++++++++++++++++++++++++++++++++++++++-----------
f338ef
 1 file changed, 131 insertions(+), 33 deletions(-)
f338ef
f338ef
diff --git a/api/src/glfs-fops.c b/api/src/glfs-fops.c
f338ef
index 396f18c..e6adea5 100644
f338ef
--- a/api/src/glfs-fops.c
f338ef
+++ b/api/src/glfs-fops.c
f338ef
@@ -34,7 +34,7 @@
f338ef
 
f338ef
 struct upcall_syncop_args {
f338ef
     struct glfs *fs;
f338ef
-    struct glfs_upcall *up_arg;
f338ef
+    struct gf_upcall upcall_data;
f338ef
 };
f338ef
 
f338ef
 #define READDIRBUF_SIZE (sizeof(struct dirent) + GF_NAME_MAX + 1)
f338ef
@@ -5716,8 +5716,28 @@ out:
f338ef
 static int
f338ef
 upcall_syncop_args_free(struct upcall_syncop_args *args)
f338ef
 {
f338ef
-    if (args && args->up_arg)
f338ef
-        GLFS_FREE(args->up_arg);
f338ef
+    dict_t *dict = NULL;
f338ef
+    struct gf_upcall *upcall_data = NULL;
f338ef
+
f338ef
+    if (args) {
f338ef
+        upcall_data = &args->upcall_data;
f338ef
+        switch (upcall_data->event_type) {
f338ef
+            case GF_UPCALL_CACHE_INVALIDATION:
f338ef
+                dict = ((struct gf_upcall_cache_invalidation *)(upcall_data
f338ef
+                                                                    ->data))
f338ef
+                           ->dict;
f338ef
+                break;
f338ef
+            case GF_UPCALL_RECALL_LEASE:
f338ef
+                dict = ((struct gf_upcall_recall_lease *)(upcall_data->data))
f338ef
+                           ->dict;
f338ef
+                break;
f338ef
+        }
f338ef
+        if (dict)
f338ef
+            dict_unref(dict);
f338ef
+
f338ef
+        GF_FREE(upcall_data->client_uid);
f338ef
+        GF_FREE(upcall_data->data);
f338ef
+    }
f338ef
     GF_FREE(args);
f338ef
     return 0;
f338ef
 }
f338ef
@@ -5727,14 +5747,7 @@ glfs_upcall_syncop_cbk(int ret, call_frame_t *frame, void *opaque)
f338ef
 {
f338ef
     struct upcall_syncop_args *args = opaque;
f338ef
 
f338ef
-    /* Here we not using upcall_syncop_args_free as application
f338ef
-     * will be cleaning up the args->up_arg using glfs_free
f338ef
-     * post processing upcall.
f338ef
-     */
f338ef
-    if (ret) {
f338ef
-        upcall_syncop_args_free(args);
f338ef
-    } else
f338ef
-        GF_FREE(args);
f338ef
+    (void)upcall_syncop_args_free(args);
f338ef
 
f338ef
     return 0;
f338ef
 }
f338ef
@@ -5743,29 +5756,17 @@ static int
f338ef
 glfs_cbk_upcall_syncop(void *opaque)
f338ef
 {
f338ef
     struct upcall_syncop_args *args = opaque;
f338ef
+    struct gf_upcall *upcall_data = NULL;
f338ef
     struct glfs_upcall *up_arg = NULL;
f338ef
     struct glfs *fs;
f338ef
+    int ret = -1;
f338ef
 
f338ef
     fs = args->fs;
f338ef
-    up_arg = args->up_arg;
f338ef
-
f338ef
-    if (fs->up_cbk && up_arg) {
f338ef
-        (fs->up_cbk)(up_arg, fs->up_data);
f338ef
-        return 0;
f338ef
-    }
f338ef
-
f338ef
-    return -1;
f338ef
-}
f338ef
+    upcall_data = &args->upcall_data;
f338ef
 
f338ef
-static struct upcall_syncop_args *
f338ef
-upcall_syncop_args_init(struct glfs *fs, struct gf_upcall *upcall_data)
f338ef
-{
f338ef
-    struct upcall_syncop_args *args = NULL;
f338ef
-    int ret = -1;
f338ef
-    struct glfs_upcall *up_arg = NULL;
f338ef
-
f338ef
-    if (!fs || !upcall_data)
f338ef
+    if (!upcall_data) {
f338ef
         goto out;
f338ef
+    }
f338ef
 
f338ef
     up_arg = GLFS_CALLOC(1, sizeof(struct gf_upcall), glfs_release_upcall,
f338ef
                          glfs_mt_upcall_entry_t);
f338ef
@@ -5795,6 +5796,8 @@ upcall_syncop_args_init(struct glfs *fs, struct gf_upcall *upcall_data)
f338ef
     if (up_arg->reason == GLFS_UPCALL_EVENT_NULL) {
f338ef
         gf_msg(THIS->name, GF_LOG_DEBUG, errno, API_MSG_INVALID_ENTRY,
f338ef
                "Upcall_EVENT_NULL received. Skipping it.");
f338ef
+        ret = 0;
f338ef
+        GLFS_FREE(up_arg);
f338ef
         goto out;
f338ef
     } else if (ret) {
f338ef
         gf_msg(THIS->name, GF_LOG_ERROR, errno, API_MSG_INVALID_ENTRY,
f338ef
@@ -5802,6 +5805,85 @@ upcall_syncop_args_init(struct glfs *fs, struct gf_upcall *upcall_data)
f338ef
         goto out;
f338ef
     }
f338ef
 
f338ef
+    if (fs->up_cbk && up_arg)
f338ef
+        (fs->up_cbk)(up_arg, fs->up_data);
f338ef
+
f338ef
+    /* application takes care of calling glfs_free on up_arg post
f338ef
+     * their processing */
f338ef
+
f338ef
+out:
f338ef
+    return ret;
f338ef
+}
f338ef
+
f338ef
+static struct gf_upcall_cache_invalidation *
f338ef
+gf_copy_cache_invalidation(struct gf_upcall_cache_invalidation *src)
f338ef
+{
f338ef
+    struct gf_upcall_cache_invalidation *dst = NULL;
f338ef
+
f338ef
+    if (!src)
f338ef
+        goto out;
f338ef
+
f338ef
+    dst = GF_CALLOC(1, sizeof(struct gf_upcall_cache_invalidation),
f338ef
+                    glfs_mt_upcall_entry_t);
f338ef
+
f338ef
+    if (!dst) {
f338ef
+        gf_msg(THIS->name, GF_LOG_ERROR, ENOMEM, API_MSG_ALLOC_FAILED,
f338ef
+               "Upcall entry allocation failed.");
f338ef
+        goto out;
f338ef
+    }
f338ef
+
f338ef
+    dst->flags = src->flags;
f338ef
+    dst->expire_time_attr = src->expire_time_attr;
f338ef
+    dst->stat = src->stat;
f338ef
+    dst->p_stat = src->p_stat;
f338ef
+    dst->oldp_stat = src->oldp_stat;
f338ef
+
f338ef
+    if (src->dict)
f338ef
+        dst->dict = dict_copy_with_ref(src->dict, NULL);
f338ef
+
f338ef
+    return dst;
f338ef
+out:
f338ef
+    return NULL;
f338ef
+}
f338ef
+
f338ef
+static struct gf_upcall_recall_lease *
f338ef
+gf_copy_recall_lease(struct gf_upcall_recall_lease *src)
f338ef
+{
f338ef
+    struct gf_upcall_recall_lease *dst = NULL;
f338ef
+
f338ef
+    if (!src)
f338ef
+        goto out;
f338ef
+
f338ef
+    dst = GF_CALLOC(1, sizeof(struct gf_upcall_recall_lease),
f338ef
+                    glfs_mt_upcall_entry_t);
f338ef
+
f338ef
+    if (!dst) {
f338ef
+        gf_msg(THIS->name, GF_LOG_ERROR, ENOMEM, API_MSG_ALLOC_FAILED,
f338ef
+               "Upcall entry allocation failed.");
f338ef
+        goto out;
f338ef
+    }
f338ef
+
f338ef
+    dst->lease_type = src->lease_type;
f338ef
+    memcpy(dst->tid, src->tid, 16);
f338ef
+
f338ef
+    if (src->dict)
f338ef
+        dst->dict = dict_copy_with_ref(src->dict, NULL);
f338ef
+
f338ef
+    return dst;
f338ef
+out:
f338ef
+    return NULL;
f338ef
+}
f338ef
+
f338ef
+static struct upcall_syncop_args *
f338ef
+upcall_syncop_args_init(struct glfs *fs, struct gf_upcall *upcall_data)
f338ef
+{
f338ef
+    struct upcall_syncop_args *args = NULL;
f338ef
+    int ret = -1;
f338ef
+    struct gf_upcall *t_data = NULL;
f338ef
+
f338ef
+    if (!fs || !upcall_data)
f338ef
+        goto out;
f338ef
+
f338ef
     args = GF_CALLOC(1, sizeof(struct upcall_syncop_args),
f338ef
                      glfs_mt_upcall_entry_t);
f338ef
     if (!args) {
f338ef
@@ -5819,15 +5901,31 @@ upcall_syncop_args_init(struct glfs *fs, struct gf_upcall *upcall_data)
f338ef
      * notification without taking any lock/ref.
f338ef
      */
f338ef
     args->fs = fs;
f338ef
-    args->up_arg = up_arg;
f338ef
+    t_data = &(args->upcall_data);
f338ef
+    t_data->client_uid = gf_strdup(upcall_data->client_uid);
f338ef
 
f338ef
-    /* application takes care of calling glfs_free on up_arg post
f338ef
-     * their processing */
f338ef
+    gf_uuid_copy(t_data->gfid, upcall_data->gfid);
f338ef
+    t_data->event_type = upcall_data->event_type;
f338ef
+
f338ef
+    switch (t_data->event_type) {
f338ef
+        case GF_UPCALL_CACHE_INVALIDATION:
f338ef
+            t_data->data = gf_copy_cache_invalidation(
f338ef
+                (struct gf_upcall_cache_invalidation *)upcall_data->data);
f338ef
+            break;
f338ef
+        case GF_UPCALL_RECALL_LEASE:
f338ef
+            t_data->data = gf_copy_recall_lease(
f338ef
+                (struct gf_upcall_recall_lease *)upcall_data->data);
f338ef
+            break;
f338ef
+    }
f338ef
+
f338ef
+    if (!t_data->data)
f338ef
+        goto out;
f338ef
 
f338ef
     return args;
f338ef
 out:
f338ef
-    if (up_arg) {
f338ef
-        GLFS_FREE(up_arg);
f338ef
+    if (ret) {
f338ef
+        GF_FREE(args->upcall_data.client_uid);
f338ef
+        GF_FREE(args);
f338ef
     }
f338ef
 
f338ef
     return NULL;
f338ef
-- 
f338ef
1.8.3.1
f338ef