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