14f8ab
From bd553499909d2d57fd05696dc7604901cef3a36a Mon Sep 17 00:00:00 2001
14f8ab
From: Soumya Koduri <skoduri@redhat.com>
14f8ab
Date: Fri, 7 Jun 2019 17:20:15 +0530
14f8ab
Subject: [PATCH 200/221] gfapi: fix incorrect initialization of upcall syncop
14f8ab
 arguments
14f8ab
14f8ab
While sending upcall notifications via synctasks, the argument used to
14f8ab
carry relevant data for these tasks is not initialized properly. This patch
14f8ab
is to fix the same.
14f8ab
14f8ab
This is backport of below upstream fix -
14f8ab
mainline: https://review.gluster.org/22839
14f8ab
release-6: https://review.gluster.org/22871
14f8ab
14f8ab
Change-Id: I9fa8f841e71d3c37d3819fbd430382928c07176c
14f8ab
fixes: bz#1717784
14f8ab
Signed-off-by: Soumya Koduri <skoduri@redhat.com>
14f8ab
Reviewed-on: https://code.engineering.redhat.com/gerrit/173508
14f8ab
Tested-by: RHGS Build Bot <nigelb@redhat.com>
14f8ab
Reviewed-by: Kaleb Keithley <kkeithle@redhat.com>
14f8ab
---
14f8ab
 api/src/glfs-fops.c | 109 ++++++++++++++++++++++++++++++++++------------------
14f8ab
 1 file changed, 72 insertions(+), 37 deletions(-)
14f8ab
14f8ab
diff --git a/api/src/glfs-fops.c b/api/src/glfs-fops.c
14f8ab
index 01ba60b..396f18c 100644
14f8ab
--- a/api/src/glfs-fops.c
14f8ab
+++ b/api/src/glfs-fops.c
14f8ab
@@ -34,7 +34,7 @@
14f8ab
 
14f8ab
 struct upcall_syncop_args {
14f8ab
     struct glfs *fs;
14f8ab
-    struct gf_upcall *upcall_data;
14f8ab
+    struct glfs_upcall *up_arg;
14f8ab
 };
14f8ab
 
14f8ab
 #define READDIRBUF_SIZE (sizeof(struct dirent) + GF_NAME_MAX + 1)
14f8ab
@@ -5714,12 +5714,28 @@ out:
14f8ab
 }
14f8ab
 
14f8ab
 static int
14f8ab
+upcall_syncop_args_free(struct upcall_syncop_args *args)
14f8ab
+{
14f8ab
+    if (args && args->up_arg)
14f8ab
+        GLFS_FREE(args->up_arg);
14f8ab
+    GF_FREE(args);
14f8ab
+    return 0;
14f8ab
+}
14f8ab
+
14f8ab
+static int
14f8ab
 glfs_upcall_syncop_cbk(int ret, call_frame_t *frame, void *opaque)
14f8ab
 {
14f8ab
     struct upcall_syncop_args *args = opaque;
14f8ab
 
14f8ab
-    GF_FREE(args->upcall_data);
14f8ab
-    GF_FREE(args);
14f8ab
+    /* Here we not using upcall_syncop_args_free as application
14f8ab
+     * will be cleaning up the args->up_arg using glfs_free
14f8ab
+     * post processing upcall.
14f8ab
+     */
14f8ab
+    if (ret) {
14f8ab
+        upcall_syncop_args_free(args);
14f8ab
+    } else
14f8ab
+        GF_FREE(args);
14f8ab
+
14f8ab
     return 0;
14f8ab
 }
14f8ab
 
14f8ab
@@ -5727,13 +5743,29 @@ static int
14f8ab
 glfs_cbk_upcall_syncop(void *opaque)
14f8ab
 {
14f8ab
     struct upcall_syncop_args *args = opaque;
14f8ab
-    int ret = -1;
14f8ab
     struct glfs_upcall *up_arg = NULL;
14f8ab
     struct glfs *fs;
14f8ab
-    struct gf_upcall *upcall_data;
14f8ab
 
14f8ab
     fs = args->fs;
14f8ab
-    upcall_data = args->upcall_data;
14f8ab
+    up_arg = args->up_arg;
14f8ab
+
14f8ab
+    if (fs->up_cbk && up_arg) {
14f8ab
+        (fs->up_cbk)(up_arg, fs->up_data);
14f8ab
+        return 0;
14f8ab
+    }
14f8ab
+
14f8ab
+    return -1;
14f8ab
+}
14f8ab
+
14f8ab
+static struct upcall_syncop_args *
14f8ab
+upcall_syncop_args_init(struct glfs *fs, struct gf_upcall *upcall_data)
14f8ab
+{
14f8ab
+    struct upcall_syncop_args *args = NULL;
14f8ab
+    int ret = -1;
14f8ab
+    struct glfs_upcall *up_arg = NULL;
14f8ab
+
14f8ab
+    if (!fs || !upcall_data)
14f8ab
+        goto out;
14f8ab
 
14f8ab
     up_arg = GLFS_CALLOC(1, sizeof(struct gf_upcall), glfs_release_upcall,
14f8ab
                          glfs_mt_upcall_entry_t);
14f8ab
@@ -5754,33 +5786,51 @@ glfs_cbk_upcall_syncop(void *opaque)
14f8ab
             errno = EINVAL;
14f8ab
     }
14f8ab
 
14f8ab
-    if (!ret && (up_arg->reason != GLFS_UPCALL_EVENT_NULL)) {
14f8ab
-        /* It could so happen that the file which got
14f8ab
-         * upcall notification may have got deleted by
14f8ab
-         * the same client. In such cases up_arg->reason
14f8ab
-         * is set to GLFS_UPCALL_EVENT_NULL. No need to
14f8ab
-         * send upcall then */
14f8ab
-        (fs->up_cbk)(up_arg, fs->up_data);
14f8ab
-    } else if (up_arg->reason == GLFS_UPCALL_EVENT_NULL) {
14f8ab
+    /* It could so happen that the file which got
14f8ab
+     * upcall notification may have got deleted by
14f8ab
+     * the same client. In such cases up_arg->reason
14f8ab
+     * is set to GLFS_UPCALL_EVENT_NULL. No need to
14f8ab
+     * send upcall then
14f8ab
+     */
14f8ab
+    if (up_arg->reason == GLFS_UPCALL_EVENT_NULL) {
14f8ab
         gf_msg(THIS->name, GF_LOG_DEBUG, errno, API_MSG_INVALID_ENTRY,
14f8ab
                "Upcall_EVENT_NULL received. Skipping it.");
14f8ab
         goto out;
14f8ab
-    } else {
14f8ab
+    } else if (ret) {
14f8ab
         gf_msg(THIS->name, GF_LOG_ERROR, errno, API_MSG_INVALID_ENTRY,
14f8ab
                "Upcall entry validation failed.");
14f8ab
         goto out;
14f8ab
     }
14f8ab
 
14f8ab
+    args = GF_CALLOC(1, sizeof(struct upcall_syncop_args),
14f8ab
+                     glfs_mt_upcall_entry_t);
14f8ab
+    if (!args) {
14f8ab
+        gf_msg(THIS->name, GF_LOG_ERROR, ENOMEM, API_MSG_ALLOC_FAILED,
14f8ab
+               "Upcall syncop args allocation failed.");
14f8ab
+        goto out;
14f8ab
+    }
14f8ab
+
14f8ab
+    /* Note: we are not taking any ref on fs here.
14f8ab
+     * Ideally applications have to unregister for upcall events
14f8ab
+     * or stop polling for upcall events before performing
14f8ab
+     * glfs_fini. And as for outstanding synctasks created, we wait
14f8ab
+     * for all syncenv threads to finish tasks before cleaning up the
14f8ab
+     * fs->ctx. Hence it seems safe to process these callback
14f8ab
+     * notification without taking any lock/ref.
14f8ab
+     */
14f8ab
+    args->fs = fs;
14f8ab
+    args->up_arg = up_arg;
14f8ab
+
14f8ab
     /* application takes care of calling glfs_free on up_arg post
14f8ab
      * their processing */
14f8ab
-    ret = 0;
14f8ab
 
14f8ab
+    return args;
14f8ab
 out:
14f8ab
-    if (ret && up_arg) {
14f8ab
+    if (up_arg) {
14f8ab
         GLFS_FREE(up_arg);
14f8ab
     }
14f8ab
 
14f8ab
-    return 0;
14f8ab
+    return NULL;
14f8ab
 }
14f8ab
 
14f8ab
 static void
14f8ab
@@ -5797,24 +5847,10 @@ glfs_cbk_upcall_data(struct glfs *fs, struct gf_upcall *upcall_data)
14f8ab
         goto out;
14f8ab
     }
14f8ab
 
14f8ab
-    args = GF_CALLOC(1, sizeof(struct upcall_syncop_args),
14f8ab
-                     glfs_mt_upcall_entry_t);
14f8ab
-    if (!args) {
14f8ab
-        gf_msg(THIS->name, GF_LOG_ERROR, ENOMEM, API_MSG_ALLOC_FAILED,
14f8ab
-               "Upcall syncop args allocation failed.");
14f8ab
-        goto out;
14f8ab
-    }
14f8ab
+    args = upcall_syncop_args_init(fs, upcall_data);
14f8ab
 
14f8ab
-    /* Note: we are not taking any ref on fs here.
14f8ab
-     * Ideally applications have to unregister for upcall events
14f8ab
-     * or stop polling for upcall events before performing
14f8ab
-     * glfs_fini. And as for outstanding synctasks created, we wait
14f8ab
-     * for all syncenv threads to finish tasks before cleaning up the
14f8ab
-     * fs->ctx. Hence it seems safe to process these callback
14f8ab
-     * notification without taking any lock/ref.
14f8ab
-     */
14f8ab
-    args->fs = fs;
14f8ab
-    args->upcall_data = gf_memdup(upcall_data, sizeof(*upcall_data));
14f8ab
+    if (!args)
14f8ab
+        goto out;
14f8ab
 
14f8ab
     ret = synctask_new(THIS->ctx->env, glfs_cbk_upcall_syncop,
14f8ab
                        glfs_upcall_syncop_cbk, NULL, args);
14f8ab
@@ -5823,8 +5859,7 @@ glfs_cbk_upcall_data(struct glfs *fs, struct gf_upcall *upcall_data)
14f8ab
         gf_msg(THIS->name, GF_LOG_ERROR, errno, API_MSG_UPCALL_SYNCOP_FAILED,
14f8ab
                "Synctak for Upcall event_type(%d) and gfid(%s) failed",
14f8ab
                upcall_data->event_type, (char *)(upcall_data->gfid));
14f8ab
-        GF_FREE(args->upcall_data);
14f8ab
-        GF_FREE(args);
14f8ab
+        upcall_syncop_args_free(args);
14f8ab
     }
14f8ab
 
14f8ab
 out:
14f8ab
-- 
14f8ab
1.8.3.1
14f8ab