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