d2787b
From 2c6c4ad77ba5511a62846af932840deb5bc389ae Mon Sep 17 00:00:00 2001
d2787b
From: Tamar Shacked <tshacked@redhat.com>
d2787b
Date: Mon, 7 Jun 2021 12:25:57 +0300
d2787b
Subject: [PATCH 584/584] dht - fixing xattr inconsistency
d2787b
d2787b
The scenario of setting an xattr to a dir, killing one of the bricks,
d2787b
removing the xattr, bringing back the brick results in xattr
d2787b
inconsistency - The downed brick will still have the xattr, but the rest
d2787b
won't.
d2787b
This patch add a mechanism that will remove the extra xattrs during
d2787b
lookup.
d2787b
d2787b
Backport of:
d2787b
> Upstream-patch-link: https://review.gluster.org/#/c/glusterfs/+/24687/
d2787b
> fixes: #1324
d2787b
> Change-Id: Ifec0b7aea6cd40daa8b0319b881191cf83e031d1
d2787b
> Signed-off-by: Barak Sason Rofman <bsasonro@redhat.com>
d2787b
d2787b
BUG: 1600379
d2787b
Change-Id: I588f69b283e5354cd362d74486d6ec6d226ecc96
d2787b
Signed-off-by: Tamar Shacked <tshacked@redhat.com>
d2787b
Signed-off-by: srijan-sivakumar <ssivakum@redhat.com>
d2787b
Reviewed-on: https://code.engineering.redhat.com/gerrit/c/rhs-glusterfs/+/245560
d2787b
Tested-by: RHGS Build Bot <nigelb@redhat.com>
d2787b
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
d2787b
---
d2787b
 libglusterfs/src/common-utils.c                | 20 +++++++-
d2787b
 libglusterfs/src/glusterfs/common-utils.h      |  6 +++
d2787b
 tests/bugs/distribute/bug-1600379.t            | 54 ++++++++++++++++++++
d2787b
 xlators/cluster/dht/src/dht-common.c           | 14 ++----
d2787b
 xlators/cluster/dht/src/dht-common.h           |  4 --
d2787b
 xlators/cluster/dht/src/dht-helper.c           |  4 ++
d2787b
 xlators/cluster/dht/src/dht-selfheal.c         | 11 ++++
d2787b
 xlators/storage/posix/src/posix-helpers.c      | 19 +++++++
d2787b
 xlators/storage/posix/src/posix-inode-fd-ops.c | 69 ++++++++++++++++++++++++++
d2787b
 xlators/storage/posix/src/posix.h              |  3 ++
d2787b
 10 files changed, 189 insertions(+), 15 deletions(-)
d2787b
 create mode 100644 tests/bugs/distribute/bug-1600379.t
d2787b
d2787b
diff --git a/libglusterfs/src/common-utils.c b/libglusterfs/src/common-utils.c
d2787b
index c2dfe28..d8b7c6e 100644
d2787b
--- a/libglusterfs/src/common-utils.c
d2787b
+++ b/libglusterfs/src/common-utils.c
d2787b
@@ -54,6 +54,7 @@
d2787b
 #include "xxhash.h"
d2787b
 #include <ifaddrs.h>
d2787b
 #include "glusterfs/libglusterfs-messages.h"
d2787b
+#include "glusterfs/glusterfs-acl.h"
d2787b
 #include "protocol-common.h"
d2787b
 #ifdef __FreeBSD__
d2787b
 #include <pthread_np.h>
d2787b
@@ -82,12 +83,21 @@ gf_boolean_t gf_signal_on_assert = false;
d2787b
 typedef int32_t (*rw_op_t)(int32_t fd, char *buf, int32_t size);
d2787b
 typedef int32_t (*rwv_op_t)(int32_t fd, const struct iovec *buf, int32_t size);
d2787b
 
d2787b
-void gf_assert(void)
d2787b
+char *xattrs_to_heal[] = {"user.",
d2787b
+                          POSIX_ACL_ACCESS_XATTR,
d2787b
+                          POSIX_ACL_DEFAULT_XATTR,
d2787b
+                          QUOTA_LIMIT_KEY,
d2787b
+                          QUOTA_LIMIT_OBJECTS_KEY,
d2787b
+                          GF_SELINUX_XATTR_KEY,
d2787b
+                          GF_XATTR_MDATA_KEY,
d2787b
+                          NULL};
d2787b
+
d2787b
+void
d2787b
+gf_assert(void)
d2787b
 {
d2787b
     if (gf_signal_on_assert) {
d2787b
         raise(SIGCONT);
d2787b
     }
d2787b
-
d2787b
 }
d2787b
 
d2787b
 void
d2787b
@@ -5430,3 +5440,9 @@ gf_d_type_from_ia_type(ia_type_t type)
d2787b
             return DT_UNKNOWN;
d2787b
     }
d2787b
 }
d2787b
+
d2787b
+char **
d2787b
+get_xattrs_to_heal()
d2787b
+{
d2787b
+    return xattrs_to_heal;
d2787b
+}
d2787b
diff --git a/libglusterfs/src/glusterfs/common-utils.h b/libglusterfs/src/glusterfs/common-utils.h
d2787b
index bd48b6f..8439bb6 100644
d2787b
--- a/libglusterfs/src/glusterfs/common-utils.h
d2787b
+++ b/libglusterfs/src/glusterfs/common-utils.h
d2787b
@@ -183,6 +183,12 @@ enum _gf_xlator_ipc_targets {
d2787b
 typedef enum _gf_special_pid gf_special_pid_t;
d2787b
 typedef enum _gf_xlator_ipc_targets _gf_xlator_ipc_targets_t;
d2787b
 
d2787b
+/* Array to hold custom xattr keys */
d2787b
+extern char *xattrs_to_heal[];
d2787b
+
d2787b
+char **
d2787b
+get_xattrs_to_heal();
d2787b
+
d2787b
 /* The DHT file rename operation is not a straightforward rename.
d2787b
  * It involves creating linkto and linkfiles, and can unlink or rename the
d2787b
  * source file depending on the hashed and cached subvols for the source
d2787b
diff --git a/tests/bugs/distribute/bug-1600379.t b/tests/bugs/distribute/bug-1600379.t
d2787b
new file mode 100644
d2787b
index 0000000..8d2f615
d2787b
--- /dev/null
d2787b
+++ b/tests/bugs/distribute/bug-1600379.t
d2787b
@@ -0,0 +1,54 @@
d2787b
+#!/bin/bash
d2787b
+
d2787b
+. $(dirname $0)/../../include.rc
d2787b
+. $(dirname $0)/../../volume.rc
d2787b
+
d2787b
+# Initialize
d2787b
+#------------------------------------------------------------
d2787b
+cleanup;
d2787b
+
d2787b
+# Start glusterd
d2787b
+TEST glusterd;
d2787b
+TEST pidof glusterd;
d2787b
+TEST $CLI volume info;
d2787b
+
d2787b
+# Create a volume
d2787b
+TEST $CLI volume create $V0 $H0:$B0/${V0}{1,2}
d2787b
+
d2787b
+# Verify volume creation
d2787b
+EXPECT "$V0" volinfo_field $V0 'Volume Name';
d2787b
+EXPECT 'Created' volinfo_field $V0 'Status';
d2787b
+
d2787b
+# Start volume and verify successful start
d2787b
+TEST $CLI volume start $V0;
d2787b
+EXPECT 'Started' volinfo_field $V0 'Status';
d2787b
+TEST glusterfs --volfile-id=$V0 --volfile-server=$H0 --entry-timeout=0 $M0;
d2787b
+#------------------------------------------------------------
d2787b
+
d2787b
+# Test case - Remove xattr from killed brick on lookup
d2787b
+#------------------------------------------------------------
d2787b
+# Create a dir and set custom xattr
d2787b
+TEST mkdir $M0/testdir
d2787b
+TEST setfattr -n user.attr -v val $M0/testdir
d2787b
+xattr_val=`getfattr -d $B0/${V0}2/testdir | awk '{print $1}'`;
d2787b
+TEST ${xattr_val}='user.attr="val"';
d2787b
+
d2787b
+# Kill 2nd brick process
d2787b
+TEST kill_brick $V0 $H0 $B0/${V0}2
d2787b
+EXPECT_WITHIN ${PROCESS_UP_TIMEOUT} "1" online_brick_count
d2787b
+
d2787b
+# Remove custom xattr
d2787b
+TEST setfattr -x user.attr $M0/testdir
d2787b
+
d2787b
+# Bring up the killed brick process
d2787b
+TEST $CLI volume start $V0 force
d2787b
+
d2787b
+# Perform lookup
d2787b
+sleep 5
d2787b
+TEST ls $M0/testdir
d2787b
+
d2787b
+# Check brick xattrs
d2787b
+xattr_val_2=`getfattr -d $B0/${V0}2/testdir`;
d2787b
+TEST [ ${xattr_val_2} = ''] ;
d2787b
+
d2787b
+cleanup;
d2787b
diff --git a/xlators/cluster/dht/src/dht-common.c b/xlators/cluster/dht/src/dht-common.c
d2787b
index ce0fbbf..edfc6e7 100644
d2787b
--- a/xlators/cluster/dht/src/dht-common.c
d2787b
+++ b/xlators/cluster/dht/src/dht-common.c
d2787b
@@ -19,6 +19,7 @@
d2787b
 #include <glusterfs/byte-order.h>
d2787b
 #include <glusterfs/quota-common-utils.h>
d2787b
 #include <glusterfs/upcall-utils.h>
d2787b
+#include <glusterfs/common-utils.h>
d2787b
 
d2787b
 #include <sys/time.h>
d2787b
 #include <libgen.h>
d2787b
@@ -127,15 +128,6 @@ dht_read_iatt_from_xdata(xlator_t *this, dict_t *xdata, struct iatt *stbuf)
d2787b
 int
d2787b
 dht_rmdir_unlock(call_frame_t *frame, xlator_t *this);
d2787b
 
d2787b
-char *xattrs_to_heal[] = {"user.",
d2787b
-                          POSIX_ACL_ACCESS_XATTR,
d2787b
-                          POSIX_ACL_DEFAULT_XATTR,
d2787b
-                          QUOTA_LIMIT_KEY,
d2787b
-                          QUOTA_LIMIT_OBJECTS_KEY,
d2787b
-                          GF_SELINUX_XATTR_KEY,
d2787b
-                          GF_XATTR_MDATA_KEY,
d2787b
-                          NULL};
d2787b
-
d2787b
 char *dht_dbg_vxattrs[] = {DHT_DBG_HASHED_SUBVOL_PATTERN, NULL};
d2787b
 
d2787b
 /* Return true if key exists in array
d2787b
@@ -143,6 +135,8 @@ char *dht_dbg_vxattrs[] = {DHT_DBG_HASHED_SUBVOL_PATTERN, NULL};
d2787b
 static gf_boolean_t
d2787b
 dht_match_xattr(const char *key)
d2787b
 {
d2787b
+    char **xattrs_to_heal = get_xattrs_to_heal();
d2787b
+
d2787b
     return gf_get_index_by_elem(xattrs_to_heal, (char *)key) >= 0;
d2787b
 }
d2787b
 
d2787b
@@ -5399,11 +5393,13 @@ dht_dir_common_set_remove_xattr(call_frame_t *frame, xlator_t *this, loc_t *loc,
d2787b
     int call_cnt = 0;
d2787b
     dht_local_t *local = NULL;
d2787b
     char gfid_local[GF_UUID_BUF_SIZE] = {0};
d2787b
+    char **xattrs_to_heal;
d2787b
 
d2787b
     conf = this->private;
d2787b
     local = frame->local;
d2787b
     call_cnt = conf->subvolume_cnt;
d2787b
     local->flags = flags;
d2787b
+    xattrs_to_heal = get_xattrs_to_heal();
d2787b
 
d2787b
     if (!gf_uuid_is_null(local->gfid)) {
d2787b
         gf_uuid_unparse(local->gfid, gfid_local);
d2787b
diff --git a/xlators/cluster/dht/src/dht-common.h b/xlators/cluster/dht/src/dht-common.h
d2787b
index 132b3b3..b856c68 100644
d2787b
--- a/xlators/cluster/dht/src/dht-common.h
d2787b
+++ b/xlators/cluster/dht/src/dht-common.h
d2787b
@@ -54,10 +54,6 @@
d2787b
 #define DHT_DBG_HASHED_SUBVOL_PATTERN "dht.file.hashed-subvol.*"
d2787b
 #define DHT_DBG_HASHED_SUBVOL_KEY "dht.file.hashed-subvol."
d2787b
 
d2787b
-/* Array to hold custom xattr keys
d2787b
- */
d2787b
-extern char *xattrs_to_heal[];
d2787b
-
d2787b
 /* Rebalance nodeuuid flags */
d2787b
 #define REBAL_NODEUUID_MINE 0x01
d2787b
 
d2787b
diff --git a/xlators/cluster/dht/src/dht-helper.c b/xlators/cluster/dht/src/dht-helper.c
d2787b
index 4f7370d..4c3940a 100644
d2787b
--- a/xlators/cluster/dht/src/dht-helper.c
d2787b
+++ b/xlators/cluster/dht/src/dht-helper.c
d2787b
@@ -2289,6 +2289,7 @@ dht_dir_set_heal_xattr(xlator_t *this, dht_local_t *local, dict_t *dst,
d2787b
     int luret = -1;
d2787b
     int luflag = -1;
d2787b
     int i = 0;
d2787b
+    char **xattrs_to_heal;
d2787b
 
d2787b
     if (!src || !dst) {
d2787b
         gf_msg(this->name, GF_LOG_WARNING, EINVAL, DHT_MSG_DICT_SET_FAILED,
d2787b
@@ -2305,6 +2306,9 @@ dht_dir_set_heal_xattr(xlator_t *this, dht_local_t *local, dict_t *dst,
d2787b
        and set it to dst dict, here index start from 1 because
d2787b
        user xattr already checked in previous statement
d2787b
     */
d2787b
+
d2787b
+    xattrs_to_heal = get_xattrs_to_heal();
d2787b
+
d2787b
     for (i = 1; xattrs_to_heal[i]; i++) {
d2787b
         keyval = dict_get(src, xattrs_to_heal[i]);
d2787b
         if (keyval) {
d2787b
diff --git a/xlators/cluster/dht/src/dht-selfheal.c b/xlators/cluster/dht/src/dht-selfheal.c
d2787b
index f4e17d1..8af7301 100644
d2787b
--- a/xlators/cluster/dht/src/dht-selfheal.c
d2787b
+++ b/xlators/cluster/dht/src/dht-selfheal.c
d2787b
@@ -2315,6 +2315,15 @@ dht_dir_heal_xattrs(void *data)
d2787b
         if (subvol == mds_subvol)
d2787b
             continue;
d2787b
         if (uret || uflag) {
d2787b
+            /* Custom xattr heal is required - let posix handle it */
d2787b
+            ret = dict_set_int8(xdata, "sync_backend_xattrs", _gf_true);
d2787b
+            if (ret) {
d2787b
+                gf_smsg(this->name, GF_LOG_WARNING, 0, DHT_MSG_DICT_SET_FAILED,
d2787b
+                        "path=%s", local->loc.path, "key=%s",
d2787b
+                        "sync_backend_xattrs", NULL);
d2787b
+                goto out;
d2787b
+            }
d2787b
+
d2787b
             ret = syncop_setxattr(subvol, &local->loc, user_xattr, 0, xdata,
d2787b
                                   NULL);
d2787b
             if (ret) {
d2787b
@@ -2325,6 +2334,8 @@ dht_dir_heal_xattrs(void *data)
d2787b
                        "user xattr on path %s on "
d2787b
                        "subvol %s, gfid = %s ",
d2787b
                        local->loc.path, subvol->name, gfid);
d2787b
+            } else {
d2787b
+                dict_del(xdata, "sync_backend_xattrs");
d2787b
             }
d2787b
         }
d2787b
     }
d2787b
diff --git a/xlators/storage/posix/src/posix-helpers.c b/xlators/storage/posix/src/posix-helpers.c
d2787b
index 16351d8..40a9ee4 100644
d2787b
--- a/xlators/storage/posix/src/posix-helpers.c
d2787b
+++ b/xlators/storage/posix/src/posix-helpers.c
d2787b
@@ -3656,3 +3656,22 @@ out:
d2787b
 
d2787b
     return is_stale;
d2787b
 }
d2787b
+
d2787b
+/* Delete user xattr from the file at the file-path specified by data and from
d2787b
+ * dict */
d2787b
+int
d2787b
+posix_delete_user_xattr(dict_t *dict, char *k, data_t *v, void *data)
d2787b
+{
d2787b
+    int ret;
d2787b
+    char *real_path = data;
d2787b
+
d2787b
+    ret = sys_lremovexattr(real_path, k);
d2787b
+    if (ret) {
d2787b
+        gf_msg("posix-helpers", GF_LOG_ERROR, P_MSG_XATTR_NOT_REMOVED, errno,
d2787b
+               "removexattr failed. key %s path %s", k, real_path);
d2787b
+    }
d2787b
+
d2787b
+    dict_del(dict, k);
d2787b
+
d2787b
+    return ret;
d2787b
+}
d2787b
diff --git a/xlators/storage/posix/src/posix-inode-fd-ops.c b/xlators/storage/posix/src/posix-inode-fd-ops.c
d2787b
index 4c2983a..be22c5e 100644
d2787b
--- a/xlators/storage/posix/src/posix-inode-fd-ops.c
d2787b
+++ b/xlators/storage/posix/src/posix-inode-fd-ops.c
d2787b
@@ -62,6 +62,7 @@
d2787b
 #include <glusterfs/events.h>
d2787b
 #include "posix-gfid-path.h"
d2787b
 #include <glusterfs/compat-uuid.h>
d2787b
+#include <glusterfs/common-utils.h>
d2787b
 
d2787b
 extern char *marker_xattrs[];
d2787b
 #define ALIGN_SIZE 4096
d2787b
@@ -2733,6 +2734,7 @@ posix_setxattr(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *dict,
d2787b
     int32_t ret = 0;
d2787b
     ssize_t acl_size = 0;
d2787b
     dict_t *xattr = NULL;
d2787b
+    dict_t *subvol_xattrs = NULL;
d2787b
     posix_xattr_filler_t filler = {
d2787b
         0,
d2787b
     };
d2787b
@@ -2748,6 +2750,10 @@ posix_setxattr(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *dict,
d2787b
     struct mdata_iatt mdata_iatt = {
d2787b
         0,
d2787b
     };
d2787b
+    int8_t sync_backend_xattrs = _gf_false;
d2787b
+    data_pair_t *custom_xattrs;
d2787b
+    data_t *keyval = NULL;
d2787b
+    char **xattrs_to_heal = get_xattrs_to_heal();
d2787b
 
d2787b
     DECLARE_OLD_FS_ID_VAR;
d2787b
     SET_FS_ID(frame->root->uid, frame->root->gid);
d2787b
@@ -2930,6 +2936,66 @@ posix_setxattr(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *dict,
d2787b
         goto out;
d2787b
     }
d2787b
 
d2787b
+    ret = dict_get_int8(xdata, "sync_backend_xattrs", &sync_backend_xattrs);
d2787b
+    if (ret) {
d2787b
+        gf_msg_debug(this->name, -ret, "Unable to get sync_backend_xattrs");
d2787b
+    }
d2787b
+
d2787b
+    if (sync_backend_xattrs) {
d2787b
+        /* List all custom xattrs */
d2787b
+        subvol_xattrs = dict_new();
d2787b
+        if (!subvol_xattrs)
d2787b
+            goto out;
d2787b
+
d2787b
+        ret = dict_set_int32_sizen(xdata, "list-xattr", 1);
d2787b
+        if (ret) {
d2787b
+            gf_msg(this->name, GF_LOG_ERROR, 0, ENOMEM,
d2787b
+                   "Unable to set list-xattr in dict ");
d2787b
+            goto out;
d2787b
+        }
d2787b
+
d2787b
+        subvol_xattrs = posix_xattr_fill(this, real_path, loc, NULL, -1, xdata,
d2787b
+                                         NULL);
d2787b
+
d2787b
+        /* Remove all user xattrs from the file */
d2787b
+        dict_foreach_fnmatch(subvol_xattrs, "user.*", posix_delete_user_xattr,
d2787b
+                             real_path);
d2787b
+
d2787b
+        /* Remove all custom xattrs from the file */
d2787b
+        for (i = 1; xattrs_to_heal[i]; i++) {
d2787b
+            keyval = dict_get(subvol_xattrs, xattrs_to_heal[i]);
d2787b
+            if (keyval) {
d2787b
+                ret = sys_lremovexattr(real_path, xattrs_to_heal[i]);
d2787b
+                if (ret) {
d2787b
+                    gf_msg(this->name, GF_LOG_ERROR, P_MSG_XATTR_NOT_REMOVED,
d2787b
+                           errno, "removexattr failed. key %s path %s",
d2787b
+                           xattrs_to_heal[i], loc->path);
d2787b
+                    goto out;
d2787b
+                }
d2787b
+
d2787b
+                dict_del(subvol_xattrs, xattrs_to_heal[i]);
d2787b
+                keyval = NULL;
d2787b
+            }
d2787b
+        }
d2787b
+
d2787b
+        /* Set custom xattrs based on info provided by DHT */
d2787b
+        custom_xattrs = dict->members_list;
d2787b
+
d2787b
+        while (custom_xattrs != NULL) {
d2787b
+            ret = sys_lsetxattr(real_path, custom_xattrs->key,
d2787b
+                                custom_xattrs->value->data,
d2787b
+                                custom_xattrs->value->len, flags);
d2787b
+            if (ret) {
d2787b
+                op_errno = errno;
d2787b
+                gf_log(this->name, GF_LOG_ERROR, "setxattr failed - %s %d",
d2787b
+                       custom_xattrs->key, ret);
d2787b
+                goto out;
d2787b
+            }
d2787b
+
d2787b
+            custom_xattrs = custom_xattrs->next;
d2787b
+        }
d2787b
+    }
d2787b
+
d2787b
     xattr = dict_new();
d2787b
     if (!xattr)
d2787b
         goto out;
d2787b
@@ -3037,6 +3103,9 @@ out:
d2787b
     if (xattr)
d2787b
         dict_unref(xattr);
d2787b
 
d2787b
+    if (subvol_xattrs)
d2787b
+        dict_unref(subvol_xattrs);
d2787b
+
d2787b
     return 0;
d2787b
 }
d2787b
 
d2787b
diff --git a/xlators/storage/posix/src/posix.h b/xlators/storage/posix/src/posix.h
d2787b
index 4be979c..b357d34 100644
d2787b
--- a/xlators/storage/posix/src/posix.h
d2787b
+++ b/xlators/storage/posix/src/posix.h
d2787b
@@ -686,4 +686,7 @@ posix_update_iatt_buf(struct iatt *buf, int fd, char *loc, dict_t *xdata);
d2787b
 gf_boolean_t
d2787b
 posix_is_layout_stale(dict_t *xdata, char *par_path, xlator_t *this);
d2787b
 
d2787b
+int
d2787b
+posix_delete_user_xattr(dict_t *dict, char *k, data_t *v, void *data);
d2787b
+
d2787b
 #endif /* _POSIX_H */
d2787b
-- 
d2787b
1.8.3.1
d2787b