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