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