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