17b94a
From 9fd966aa6879ac9867381629f82eca24b950d731 Mon Sep 17 00:00:00 2001
17b94a
From: Mohammed Rafi KC <rkavunga@redhat.com>
17b94a
Date: Sun, 2 Jun 2019 01:36:33 +0530
17b94a
Subject: [PATCH 175/178] ec/fini: Fix race between xlator cleanup and on going
17b94a
 async fop
17b94a
17b94a
Problem:
17b94a
While we process a cleanup, there is a chance for a race between
17b94a
async operations, for example ec_launch_replace_heal. So this can
17b94a
lead to invalid mem access.
17b94a
17b94a
Solution:
17b94a
Just like we track on going heal fops, we can also track fops like
17b94a
ec_launch_replace_heal, so that we can decide when to send a
17b94a
PARENT_DOWN request.
17b94a
17b94a
> upstream patch : https://review.gluster.org/#/c/glusterfs/+/22798/
17b94a
17b94a
>Change-Id: I055391c5c6c34d58aef7336847f3b570cb831298
17b94a
>fixes: bz#1703948
17b94a
>Signed-off-by: Mohammed Rafi KC <rkavunga@redhat.com>
17b94a
17b94a
Change-Id: I055391c5c6c34d58aef7336847f3b570cb831298
17b94a
BUG: 1714588
17b94a
Signed-off-by: Mohammed Rafi KC <rkavunga@redhat.com>
17b94a
Reviewed-on: https://code.engineering.redhat.com/gerrit/172801
17b94a
Tested-by: RHGS Build Bot <nigelb@redhat.com>
17b94a
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
17b94a
---
17b94a
 xlators/cluster/ec/src/ec-common.c | 10 ++++++++++
17b94a
 xlators/cluster/ec/src/ec-common.h |  2 ++
17b94a
 xlators/cluster/ec/src/ec-data.c   |  4 +++-
17b94a
 xlators/cluster/ec/src/ec-heal.c   | 17 +++++++++++++++--
17b94a
 xlators/cluster/ec/src/ec-types.h  |  1 +
17b94a
 xlators/cluster/ec/src/ec.c        | 37 +++++++++++++++++++++++++------------
17b94a
 6 files changed, 56 insertions(+), 15 deletions(-)
17b94a
17b94a
diff --git a/xlators/cluster/ec/src/ec-common.c b/xlators/cluster/ec/src/ec-common.c
17b94a
index e85aa8b..9cc6395 100644
17b94a
--- a/xlators/cluster/ec/src/ec-common.c
17b94a
+++ b/xlators/cluster/ec/src/ec-common.c
17b94a
@@ -2955,3 +2955,13 @@ ec_manager(ec_fop_data_t *fop, int32_t error)
17b94a
 
17b94a
     __ec_manager(fop, error);
17b94a
 }
17b94a
+
17b94a
+gf_boolean_t
17b94a
+__ec_is_last_fop(ec_t *ec)
17b94a
+{
17b94a
+    if ((list_empty(&ec->pending_fops)) &&
17b94a
+        (GF_ATOMIC_GET(ec->async_fop_count) == 0)) {
17b94a
+        return _gf_true;
17b94a
+    }
17b94a
+    return _gf_false;
17b94a
+}
17b94a
diff --git a/xlators/cluster/ec/src/ec-common.h b/xlators/cluster/ec/src/ec-common.h
17b94a
index e948342..bf6c97d 100644
17b94a
--- a/xlators/cluster/ec/src/ec-common.h
17b94a
+++ b/xlators/cluster/ec/src/ec-common.h
17b94a
@@ -204,4 +204,6 @@ void
17b94a
 ec_reset_entry_healing(ec_fop_data_t *fop);
17b94a
 char *
17b94a
 ec_msg_str(ec_fop_data_t *fop);
17b94a
+gf_boolean_t
17b94a
+__ec_is_last_fop(ec_t *ec);
17b94a
 #endif /* __EC_COMMON_H__ */
17b94a
diff --git a/xlators/cluster/ec/src/ec-data.c b/xlators/cluster/ec/src/ec-data.c
17b94a
index 6ef9340..8d2d9a1 100644
17b94a
--- a/xlators/cluster/ec/src/ec-data.c
17b94a
+++ b/xlators/cluster/ec/src/ec-data.c
17b94a
@@ -202,11 +202,13 @@ ec_handle_last_pending_fop_completion(ec_fop_data_t *fop, gf_boolean_t *notify)
17b94a
 {
17b94a
     ec_t *ec = fop->xl->private;
17b94a
 
17b94a
+    *notify = _gf_false;
17b94a
+
17b94a
     if (!list_empty(&fop->pending_list)) {
17b94a
         LOCK(&ec->lock);
17b94a
         {
17b94a
             list_del_init(&fop->pending_list);
17b94a
-            *notify = list_empty(&ec->pending_fops);
17b94a
+            *notify = __ec_is_last_fop(ec);
17b94a
         }
17b94a
         UNLOCK(&ec->lock);
17b94a
     }
17b94a
diff --git a/xlators/cluster/ec/src/ec-heal.c b/xlators/cluster/ec/src/ec-heal.c
17b94a
index 8844c29..237fea2 100644
17b94a
--- a/xlators/cluster/ec/src/ec-heal.c
17b94a
+++ b/xlators/cluster/ec/src/ec-heal.c
17b94a
@@ -2814,8 +2814,20 @@ int
17b94a
 ec_replace_heal_done(int ret, call_frame_t *heal, void *opaque)
17b94a
 {
17b94a
     ec_t *ec = opaque;
17b94a
+    gf_boolean_t last_fop = _gf_false;
17b94a
 
17b94a
+    if (GF_ATOMIC_DEC(ec->async_fop_count) == 0) {
17b94a
+        LOCK(&ec->lock);
17b94a
+        {
17b94a
+            last_fop = __ec_is_last_fop(ec);
17b94a
+        }
17b94a
+        UNLOCK(&ec->lock);
17b94a
+    }
17b94a
     gf_msg_debug(ec->xl->name, 0, "getxattr on bricks is done ret %d", ret);
17b94a
+
17b94a
+    if (last_fop)
17b94a
+        ec_pending_fops_completed(ec);
17b94a
+
17b94a
     return 0;
17b94a
 }
17b94a
 
17b94a
@@ -2869,14 +2881,15 @@ ec_launch_replace_heal(ec_t *ec)
17b94a
 {
17b94a
     int ret = -1;
17b94a
 
17b94a
-    if (!ec)
17b94a
-        return ret;
17b94a
     ret = synctask_new(ec->xl->ctx->env, ec_replace_brick_heal_wrap,
17b94a
                        ec_replace_heal_done, NULL, ec);
17b94a
+
17b94a
     if (ret < 0) {
17b94a
         gf_msg_debug(ec->xl->name, 0, "Heal failed for replace brick ret = %d",
17b94a
                      ret);
17b94a
+        ec_replace_heal_done(-1, NULL, ec);
17b94a
     }
17b94a
+
17b94a
     return ret;
17b94a
 }
17b94a
 
17b94a
diff --git a/xlators/cluster/ec/src/ec-types.h b/xlators/cluster/ec/src/ec-types.h
17b94a
index 1c295c0..4dbf4a3 100644
17b94a
--- a/xlators/cluster/ec/src/ec-types.h
17b94a
+++ b/xlators/cluster/ec/src/ec-types.h
17b94a
@@ -643,6 +643,7 @@ struct _ec {
17b94a
     uintptr_t xl_notify;      /* Bit flag representing
17b94a
                                  notification for bricks. */
17b94a
     uintptr_t node_mask;
17b94a
+    gf_atomic_t async_fop_count; /* Number of on going asynchronous fops. */
17b94a
     xlator_t **xl_list;
17b94a
     gf_lock_t lock;
17b94a
     gf_timer_t *timer;
17b94a
diff --git a/xlators/cluster/ec/src/ec.c b/xlators/cluster/ec/src/ec.c
17b94a
index df5912c..f0d58c0 100644
17b94a
--- a/xlators/cluster/ec/src/ec.c
17b94a
+++ b/xlators/cluster/ec/src/ec.c
17b94a
@@ -355,6 +355,7 @@ ec_notify_cbk(void *data)
17b94a
     ec_t *ec = data;
17b94a
     glusterfs_event_t event = GF_EVENT_MAXVAL;
17b94a
     gf_boolean_t propagate = _gf_false;
17b94a
+    gf_boolean_t launch_heal = _gf_false;
17b94a
 
17b94a
     LOCK(&ec->lock);
17b94a
     {
17b94a
@@ -384,6 +385,11 @@ ec_notify_cbk(void *data)
17b94a
              * still bricks DOWN, they will be healed when they
17b94a
              * come up. */
17b94a
             ec_up(ec->xl, ec);
17b94a
+
17b94a
+            if (ec->shd.iamshd && !ec->shutdown) {
17b94a
+                launch_heal = _gf_true;
17b94a
+                GF_ATOMIC_INC(ec->async_fop_count);
17b94a
+            }
17b94a
         }
17b94a
 
17b94a
         propagate = _gf_true;
17b94a
@@ -391,13 +397,12 @@ ec_notify_cbk(void *data)
17b94a
 unlock:
17b94a
     UNLOCK(&ec->lock);
17b94a
 
17b94a
+    if (launch_heal) {
17b94a
+        /* We have just brought the volume UP, so we trigger
17b94a
+         * a self-heal check on the root directory. */
17b94a
+        ec_launch_replace_heal(ec);
17b94a
+    }
17b94a
     if (propagate) {
17b94a
-        if ((event == GF_EVENT_CHILD_UP) && ec->shd.iamshd) {
17b94a
-            /* We have just brought the volume UP, so we trigger
17b94a
-             * a self-heal check on the root directory. */
17b94a
-            ec_launch_replace_heal(ec);
17b94a
-        }
17b94a
-
17b94a
         default_notify(ec->xl, event, NULL);
17b94a
     }
17b94a
 }
17b94a
@@ -425,7 +430,7 @@ ec_disable_delays(ec_t *ec)
17b94a
 {
17b94a
     ec->shutdown = _gf_true;
17b94a
 
17b94a
-    return list_empty(&ec->pending_fops);
17b94a
+    return __ec_is_last_fop(ec);
17b94a
 }
17b94a
 
17b94a
 void
17b94a
@@ -603,7 +608,10 @@ ec_notify(xlator_t *this, int32_t event, void *data, void *data2)
17b94a
         if (event == GF_EVENT_CHILD_UP) {
17b94a
             /* We need to trigger a selfheal if a brick changes
17b94a
              * to UP state. */
17b94a
-            needs_shd_check = ec_set_up_state(ec, mask, mask);
17b94a
+            if (ec_set_up_state(ec, mask, mask) && ec->shd.iamshd &&
17b94a
+                !ec->shutdown) {
17b94a
+                needs_shd_check = _gf_true;
17b94a
+            }
17b94a
         } else if (event == GF_EVENT_CHILD_DOWN) {
17b94a
             ec_set_up_state(ec, mask, 0);
17b94a
         }
17b94a
@@ -633,17 +641,21 @@ ec_notify(xlator_t *this, int32_t event, void *data, void *data2)
17b94a
             }
17b94a
         } else {
17b94a
             propagate = _gf_false;
17b94a
+            needs_shd_check = _gf_false;
17b94a
+        }
17b94a
+
17b94a
+        if (needs_shd_check) {
17b94a
+            GF_ATOMIC_INC(ec->async_fop_count);
17b94a
         }
17b94a
     }
17b94a
 unlock:
17b94a
     UNLOCK(&ec->lock);
17b94a
 
17b94a
 done:
17b94a
+    if (needs_shd_check) {
17b94a
+        ec_launch_replace_heal(ec);
17b94a
+    }
17b94a
     if (propagate) {
17b94a
-        if (needs_shd_check && ec->shd.iamshd) {
17b94a
-            ec_launch_replace_heal(ec);
17b94a
-        }
17b94a
-
17b94a
         error = default_notify(this, event, data);
17b94a
     }
17b94a
 
17b94a
@@ -705,6 +717,7 @@ init(xlator_t *this)
17b94a
     ec->xl = this;
17b94a
     LOCK_INIT(&ec->lock);
17b94a
 
17b94a
+    GF_ATOMIC_INIT(ec->async_fop_count, 0);
17b94a
     INIT_LIST_HEAD(&ec->pending_fops);
17b94a
     INIT_LIST_HEAD(&ec->heal_waiting);
17b94a
     INIT_LIST_HEAD(&ec->healing);
17b94a
-- 
17b94a
1.8.3.1
17b94a