17b94a
From 9912a432dc3493007462f76c5933d04a160814ae Mon Sep 17 00:00:00 2001
17b94a
From: Pranith Kumar K <pkarampu@redhat.com>
17b94a
Date: Thu, 20 Jun 2019 17:05:49 +0530
17b94a
Subject: [PATCH 198/221] cluster/ec: Prevent double pre-op xattrops
17b94a
17b94a
Problem:
17b94a
Race:
17b94a
Thread-1                                    Thread-2
17b94a
1) Does ec_get_size_version() to perform
17b94a
pre-op fxattrop as part of write-1
17b94a
                                           2) Calls ec_set_dirty_flag() in
17b94a
                                              ec_get_size_version() for write-2.
17b94a
					      This sets dirty[] to 1
17b94a
3) Completes executing
17b94a
ec_prepare_update_cbk leading to
17b94a
ctx->dirty[] = '1'
17b94a
					   4) Takes LOCK(inode->lock) to check if there are
17b94a
					      any flags and sets dirty-flag because
17b94a
				              lock->waiting_flag is 0 now. This leads to
17b94a
					      fxattrop to increment on-disk dirty[] to '2'
17b94a
17b94a
At the end of the writes the file will be marked for heal even when it doesn't need heal.
17b94a
17b94a
Fix:
17b94a
Perform ec_set_dirty_flag() and other checks inside LOCK() to prevent dirty[] to be marked
17b94a
as '1' in step 2) above
17b94a
17b94a
 > Upstream-patch: https://review.gluster.org/c/glusterfs/+/22907
17b94a
17b94a
fixes: bz#1600918
17b94a
Change-Id: Icac2ab39c0b1e7e154387800fbededc561612865
17b94a
Signed-off-by: Pranith Kumar K <pkarampu@redhat.com>
17b94a
Reviewed-on: https://code.engineering.redhat.com/gerrit/174385
17b94a
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
17b94a
Tested-by: Atin Mukherjee <amukherj@redhat.com>
17b94a
---
17b94a
 tests/basic/ec/ec-dirty-flags.t    | 23 +++++++++++++++++++++++
17b94a
 xlators/cluster/ec/src/ec-common.c | 13 +++++++------
17b94a
 2 files changed, 30 insertions(+), 6 deletions(-)
17b94a
 create mode 100644 tests/basic/ec/ec-dirty-flags.t
17b94a
17b94a
diff --git a/tests/basic/ec/ec-dirty-flags.t b/tests/basic/ec/ec-dirty-flags.t
17b94a
new file mode 100644
17b94a
index 0000000..68e6610
17b94a
--- /dev/null
17b94a
+++ b/tests/basic/ec/ec-dirty-flags.t
17b94a
@@ -0,0 +1,23 @@
17b94a
+#!/bin/bash
17b94a
+
17b94a
+. $(dirname $0)/../../include.rc
17b94a
+. $(dirname $0)/../../volume.rc
17b94a
+
17b94a
+# This checks if the fop keeps the dirty flags settings correctly after
17b94a
+# finishing the fop.
17b94a
+
17b94a
+cleanup
17b94a
+TEST glusterd
17b94a
+TEST pidof glusterd
17b94a
+TEST $CLI volume create $V0 disperse 3 redundancy 1 $H0:$B0/${V0}{0..2}
17b94a
+TEST $CLI volume heal $V0 disable
17b94a
+TEST $CLI volume start $V0
17b94a
+
17b94a
+TEST $GFS --volfile-id=/$V0 --volfile-server=$H0 $M0;
17b94a
+EXPECT_WITHIN $CHILD_UP_TIMEOUT "3" ec_child_up_count $V0 0
17b94a
+cd $M0
17b94a
+for i in {1..1000}; do dd if=/dev/zero of=file-${i} bs=512k count=2; done
17b94a
+cd -
17b94a
+EXPECT "^0$" get_pending_heal_count $V0
17b94a
+
17b94a
+cleanup
17b94a
diff --git a/xlators/cluster/ec/src/ec-common.c b/xlators/cluster/ec/src/ec-common.c
17b94a
index 9cc6395..35c2256 100644
17b94a
--- a/xlators/cluster/ec/src/ec-common.c
17b94a
+++ b/xlators/cluster/ec/src/ec-common.c
17b94a
@@ -1405,6 +1405,10 @@ ec_get_size_version(ec_lock_link_t *link)
17b94a
         !ec_is_data_fop(fop->id))
17b94a
         link->optimistic_changelog = _gf_true;
17b94a
 
17b94a
+    memset(&loc, 0, sizeof(loc));
17b94a
+
17b94a
+    LOCK(&lock->loc.inode->lock);
17b94a
+
17b94a
     set_dirty = ec_set_dirty_flag(link, ctx, dirty);
17b94a
 
17b94a
     /* If ec metadata has already been retrieved, do not try again. */
17b94a
@@ -1412,20 +1416,16 @@ ec_get_size_version(ec_lock_link_t *link)
17b94a
         if (ec_is_data_fop(fop->id)) {
17b94a
             fop->healing |= lock->healing;
17b94a
         }
17b94a
-        return;
17b94a
+        goto unlock;
17b94a
     }
17b94a
 
17b94a
     /* Determine if there's something we need to retrieve for the current
17b94a
      * operation. */
17b94a
     if (!set_dirty && !lock->query && (lock->loc.inode->ia_type != IA_IFREG) &&
17b94a
         (lock->loc.inode->ia_type != IA_INVAL)) {
17b94a
-        return;
17b94a
+        goto unlock;
17b94a
     }
17b94a
 
17b94a
-    memset(&loc, 0, sizeof(loc));
17b94a
-
17b94a
-    LOCK(&lock->loc.inode->lock);
17b94a
-
17b94a
     changed_flags = ec_set_xattrop_flags_and_params(lock, link, dirty);
17b94a
     if (link->waiting_flags) {
17b94a
         /* This fop needs to wait until all its flags are cleared which
17b94a
@@ -1436,6 +1436,7 @@ ec_get_size_version(ec_lock_link_t *link)
17b94a
         GF_ASSERT(!changed_flags);
17b94a
     }
17b94a
 
17b94a
+unlock:
17b94a
     UNLOCK(&lock->loc.inode->lock);
17b94a
 
17b94a
     if (!changed_flags)
17b94a
-- 
17b94a
1.8.3.1
17b94a