190130
From ba23e6d8f4eff11a228816149a8a1ccd6df41146 Mon Sep 17 00:00:00 2001
190130
From: Susant Palai <spalai@redhat.com>
190130
Date: Fri, 27 Dec 2019 12:06:19 +0530
190130
Subject: [PATCH 415/449] dht: Fix stale-layout and create issue
190130
190130
Problem: With lookup-optimize set to on by default, a client with
190130
stale-layout can create a new file on a wrong subvol. This will lead to
190130
possible duplicate files if two different clients attempt to create the
190130
same file with two different layouts.
190130
190130
Solution: Send in-memory layout to be cross checked at posix before
190130
commiting a "create". In case of a mismatch, sync the client layout with
190130
that of the server and attempt the create fop one more time.
190130
190130
test: Manual, testcase(attached)
190130
190130
(Backport of https://review.gluster.org/#/c/glusterfs/+/23927/)
190130
190130
BUG: 1748865
190130
Change-Id: I6c82c97418654ae8eb3b81ab65f1247aa4002ceb
190130
Signed-off-by: Susant Palai <spalai@redhat.com>
190130
Reviewed-on: https://code.engineering.redhat.com/gerrit/202465
190130
Tested-by: RHGS Build Bot <nigelb@redhat.com>
190130
Reviewed-by: Mohit Agrawal <moagrawa@redhat.com>
190130
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
190130
---
190130
 tests/bugs/distribute/bug-1786679.t              |  69 +++++++++++
190130
 xlators/cluster/dht/src/dht-common.c             | 147 ++++++++++++++++++++---
190130
 xlators/cluster/dht/src/dht-common.h             |   6 +
190130
 xlators/protocol/client/src/client-rpc-fops_v2.c |   9 +-
190130
 xlators/storage/posix/src/posix-entry-ops.c      |  29 ++++-
190130
 xlators/storage/posix/src/posix-helpers.c        |  76 ++++++++++++
190130
 xlators/storage/posix/src/posix.h                |   4 +
190130
 7 files changed, 321 insertions(+), 19 deletions(-)
190130
 create mode 100755 tests/bugs/distribute/bug-1786679.t
190130
190130
diff --git a/tests/bugs/distribute/bug-1786679.t b/tests/bugs/distribute/bug-1786679.t
190130
new file mode 100755
190130
index 0000000..219ce51
190130
--- /dev/null
190130
+++ b/tests/bugs/distribute/bug-1786679.t
190130
@@ -0,0 +1,69 @@
190130
+#!/bin/bash
190130
+
190130
+SCRIPT_TIMEOUT=250
190130
+
190130
+. $(dirname $0)/../../include.rc
190130
+. $(dirname $0)/../../volume.rc
190130
+. $(dirname $0)/../../dht.rc
190130
+
190130
+
190130
+# create 2 subvols
190130
+# create a dir
190130
+# create a file
190130
+# change layout
190130
+# remove the file
190130
+# execute create from a different mount
190130
+# Without the patch, the file will be present on both of the bricks
190130
+
190130
+cleanup
190130
+
190130
+function get_layout () {
190130
+
190130
+layout=`getfattr -n trusted.glusterfs.dht -e hex $1 2>&1 | grep dht | gawk -F"=" '{print $2}'`
190130
+
190130
+echo $layout
190130
+
190130
+}
190130
+
190130
+function set_layout()
190130
+{
190130
+    setfattr -n  "trusted.glusterfs.dht" -v $1 $2
190130
+}
190130
+
190130
+TEST glusterd
190130
+TEST pidof glusterd
190130
+
190130
+BRICK1=$B0/${V0}-0
190130
+BRICK2=$B0/${V0}-1
190130
+
190130
+TEST $CLI volume create $V0 $H0:$BRICK1 $H0:$BRICK2
190130
+TEST $CLI volume start $V0
190130
+
190130
+# Mount FUSE and create symlink
190130
+TEST glusterfs -s $H0 --volfile-id $V0 $M0
190130
+TEST mkdir $M0/dir
190130
+TEST touch $M0/dir/file
190130
+TEST ! stat "$BRICK1/dir/file"
190130
+TEST stat "$BRICK2/dir/file"
190130
+
190130
+layout1="$(get_layout "$BRICK1/dir")"
190130
+layout2="$(get_layout "$BRICK2/dir")"
190130
+
190130
+TEST set_layout $layout1 "$BRICK2/dir"
190130
+TEST set_layout $layout2 "$BRICK1/dir"
190130
+
190130
+TEST rm $M0/dir/file -f
190130
+TEST gluster v set $V0 client-log-level DEBUG
190130
+
190130
+#Without the patch in place, this client will create the file in $BRICK2
190130
+#which will lead to two files being on both the bricks when a new client
190130
+#create the file with the same name
190130
+TEST touch $M0/dir/file
190130
+
190130
+TEST glusterfs -s $H0 --volfile-id $V0 $M1
190130
+TEST touch $M1/dir/file
190130
+
190130
+TEST stat "$BRICK1/dir/file"
190130
+TEST ! stat "$BRICK2/dir/file"
190130
+
190130
+cleanup
190130
diff --git a/xlators/cluster/dht/src/dht-common.c b/xlators/cluster/dht/src/dht-common.c
190130
index 7890e7a..6aa18f3 100644
190130
--- a/xlators/cluster/dht/src/dht-common.c
190130
+++ b/xlators/cluster/dht/src/dht-common.c
190130
@@ -8262,6 +8262,11 @@ dht_create_cbk(call_frame_t *frame, void *cookie, xlator_t *this, int op_ret,
190130
     xlator_t *prev = NULL;
190130
     int ret = -1;
190130
     dht_local_t *local = NULL;
190130
+    gf_boolean_t parent_layout_changed = _gf_false;
190130
+    char pgfid[GF_UUID_BUF_SIZE] = {0};
190130
+    xlator_t *subvol = NULL;
190130
+
190130
+    local = frame->local;
190130
 
190130
     local = frame->local;
190130
     if (!local) {
190130
@@ -8270,8 +8275,69 @@ dht_create_cbk(call_frame_t *frame, void *cookie, xlator_t *this, int op_ret,
190130
         goto out;
190130
     }
190130
 
190130
-    if (op_ret == -1)
190130
+    if (op_ret == -1) {
190130
+        local->op_errno = op_errno;
190130
+        parent_layout_changed = (xdata &&
190130
+                                 dict_get(xdata, GF_PREOP_CHECK_FAILED))
190130
+                                    ? _gf_true
190130
+                                    : _gf_false;
190130
+
190130
+        if (parent_layout_changed) {
190130
+            if (local && local->lock[0].layout.parent_layout.locks) {
190130
+                /* Returning failure as the layout could not be fixed even under
190130
+                 * the lock */
190130
+                goto out;
190130
+            }
190130
+
190130
+            gf_uuid_unparse(local->loc.parent->gfid, pgfid);
190130
+            gf_msg(this->name, GF_LOG_INFO, 0, DHT_MSG_PARENT_LAYOUT_CHANGED,
190130
+                   "create (%s/%s) (path: %s): parent layout "
190130
+                   "changed. Attempting a layout refresh and then a "
190130
+                   "retry",
190130
+                   pgfid, local->loc.name, local->loc.path);
190130
+
190130
+            /*
190130
+              dht_refresh_layout needs directory info in local->loc.Hence,
190130
+              storing the parent_loc in local->loc and storing the create
190130
+              context in local->loc2. We will restore this information in
190130
+              dht_creation_do.
190130
+             */
190130
+
190130
+            loc_wipe(&local->loc2);
190130
+
190130
+            ret = loc_copy(&local->loc2, &local->loc);
190130
+            if (ret) {
190130
+                gf_msg(this->name, GF_LOG_ERROR, ENOMEM, DHT_MSG_NO_MEMORY,
190130
+                       "loc_copy failed %s", local->loc.path);
190130
+
190130
+                goto out;
190130
+            }
190130
+
190130
+            loc_wipe(&local->loc);
190130
+
190130
+            ret = dht_build_parent_loc(this, &local->loc, &local->loc2,
190130
+                                       &op_errno);
190130
+
190130
+            if (ret) {
190130
+                gf_msg(this->name, GF_LOG_ERROR, ENOMEM, DHT_MSG_LOC_FAILED,
190130
+                       "parent loc build failed");
190130
+                goto out;
190130
+            }
190130
+
190130
+            subvol = dht_subvol_get_hashed(this, &local->loc2);
190130
+
190130
+            ret = dht_create_lock(frame, subvol);
190130
+            if (ret < 0) {
190130
+                gf_msg(this->name, GF_LOG_ERROR, 0, DHT_MSG_INODE_LK_ERROR,
190130
+                       "locking parent failed");
190130
+                goto out;
190130
+            }
190130
+
190130
+            return 0;
190130
+        }
190130
+
190130
         goto out;
190130
+    }
190130
 
190130
     prev = cookie;
190130
 
190130
@@ -8392,6 +8458,8 @@ dht_create_wind_to_avail_subvol(call_frame_t *frame, xlator_t *this,
190130
         gf_msg_debug(this->name, 0, "creating %s on %s", loc->path,
190130
                      subvol->name);
190130
 
190130
+        dht_set_parent_layout_in_dict(loc, this, local);
190130
+
190130
         STACK_WIND_COOKIE(frame, dht_create_cbk, subvol, subvol,
190130
                           subvol->fops->create, loc, flags, mode, umask, fd,
190130
                           params);
190130
@@ -8400,10 +8468,6 @@ dht_create_wind_to_avail_subvol(call_frame_t *frame, xlator_t *this,
190130
         avail_subvol = dht_free_disk_available_subvol(this, subvol, local);
190130
 
190130
         if (avail_subvol != subvol) {
190130
-            local->params = dict_ref(params);
190130
-            local->flags = flags;
190130
-            local->mode = mode;
190130
-            local->umask = umask;
190130
             local->cached_subvol = avail_subvol;
190130
             local->hashed_subvol = subvol;
190130
 
190130
@@ -8419,6 +8483,8 @@ dht_create_wind_to_avail_subvol(call_frame_t *frame, xlator_t *this,
190130
         gf_msg_debug(this->name, 0, "creating %s on %s", loc->path,
190130
                      subvol->name);
190130
 
190130
+        dht_set_parent_layout_in_dict(loc, this, local);
190130
+
190130
         STACK_WIND_COOKIE(frame, dht_create_cbk, subvol, subvol,
190130
                           subvol->fops->create, loc, flags, mode, umask, fd,
190130
                           params);
190130
@@ -8680,6 +8746,60 @@ err:
190130
 }
190130
 
190130
 int
190130
+dht_set_parent_layout_in_dict(loc_t *loc, xlator_t *this, dht_local_t *local)
190130
+{
190130
+    dht_conf_t *conf = this->private;
190130
+    dht_layout_t *parent_layout = NULL;
190130
+    int *parent_disk_layout = NULL;
190130
+    xlator_t *hashed_subvol = NULL;
190130
+    char pgfid[GF_UUID_BUF_SIZE] = {0};
190130
+    int ret = 0;
190130
+
190130
+    gf_uuid_unparse(loc->parent->gfid, pgfid);
190130
+
190130
+    parent_layout = dht_layout_get(this, loc->parent);
190130
+    hashed_subvol = dht_subvol_get_hashed(this, loc);
190130
+
190130
+    ret = dht_disk_layout_extract_for_subvol(this, parent_layout, hashed_subvol,
190130
+                                             &parent_disk_layout);
190130
+    if (ret == -1) {
190130
+        gf_msg(this->name, GF_LOG_WARNING, local->op_errno,
190130
+               DHT_MSG_PARENT_LAYOUT_CHANGED,
190130
+               "%s (%s/%s) (path: %s): "
190130
+               "extracting in-memory layout of parent failed. ",
190130
+               gf_fop_list[local->fop], pgfid, loc->name, loc->path);
190130
+        goto err;
190130
+    }
190130
+
190130
+    ret = dict_set_str_sizen(local->params, GF_PREOP_PARENT_KEY,
190130
+                             conf->xattr_name);
190130
+    if (ret < 0) {
190130
+        gf_msg(this->name, GF_LOG_WARNING, local->op_errno,
190130
+               DHT_MSG_PARENT_LAYOUT_CHANGED,
190130
+               "%s (%s/%s) (path: %s): "
190130
+               "setting %s key in params dictionary failed. ",
190130
+               gf_fop_list[local->fop], pgfid, loc->name, loc->path,
190130
+               GF_PREOP_PARENT_KEY);
190130
+        goto err;
190130
+    }
190130
+
190130
+    ret = dict_set_bin(local->params, conf->xattr_name, parent_disk_layout,
190130
+                       4 * 4);
190130
+    if (ret < 0) {
190130
+        gf_msg(this->name, GF_LOG_WARNING, local->op_errno,
190130
+               DHT_MSG_PARENT_LAYOUT_CHANGED,
190130
+               "%s (%s/%s) (path: %s): "
190130
+               "setting parent-layout in params dictionary failed. ",
190130
+               gf_fop_list[local->fop], pgfid, loc->name, loc->path);
190130
+        goto err;
190130
+    }
190130
+
190130
+err:
190130
+    dht_layout_unref(this, parent_layout);
190130
+    return ret;
190130
+}
190130
+
190130
+int
190130
 dht_create(call_frame_t *frame, xlator_t *this, loc_t *loc, int32_t flags,
190130
            mode_t mode, mode_t umask, fd_t *fd, dict_t *params)
190130
 {
190130
@@ -8705,6 +8825,11 @@ dht_create(call_frame_t *frame, xlator_t *this, loc_t *loc, int32_t flags,
190130
         goto err;
190130
     }
190130
 
190130
+    local->params = dict_ref(params);
190130
+    local->flags = flags;
190130
+    local->mode = mode;
190130
+    local->umask = umask;
190130
+
190130
     if (dht_filter_loc_subvol_key(this, loc, &local->loc, &subvol)) {
190130
         gf_msg(this->name, GF_LOG_INFO, 0, DHT_MSG_SUBVOL_INFO,
190130
                "creating %s on %s (got create on %s)", local->loc.path,
190130
@@ -8720,10 +8845,6 @@ dht_create(call_frame_t *frame, xlator_t *this, loc_t *loc, int32_t flags,
190130
 
190130
         if (hashed_subvol && (hashed_subvol != subvol)) {
190130
             /* Create the linkto file and then the data file */
190130
-            local->params = dict_ref(params);
190130
-            local->flags = flags;
190130
-            local->mode = mode;
190130
-            local->umask = umask;
190130
             local->cached_subvol = subvol;
190130
             local->hashed_subvol = hashed_subvol;
190130
 
190130
@@ -8736,6 +8857,9 @@ dht_create(call_frame_t *frame, xlator_t *this, loc_t *loc, int32_t flags,
190130
          * file as we expect a lookup everywhere if there are problems
190130
          * with the parent layout
190130
          */
190130
+
190130
+        dht_set_parent_layout_in_dict(loc, this, local);
190130
+
190130
         STACK_WIND_COOKIE(frame, dht_create_cbk, subvol, subvol,
190130
                           subvol->fops->create, &local->loc, flags, mode, umask,
190130
                           fd, params);
190130
@@ -8787,11 +8911,6 @@ dht_create(call_frame_t *frame, xlator_t *this, loc_t *loc, int32_t flags,
190130
                     goto err;
190130
                 }
190130
 
190130
-                local->params = dict_ref(params);
190130
-                local->flags = flags;
190130
-                local->mode = mode;
190130
-                local->umask = umask;
190130
-
190130
                 loc_wipe(&local->loc);
190130
 
190130
                 ret = dht_build_parent_loc(this, &local->loc, loc, &op_errno);
190130
diff --git a/xlators/cluster/dht/src/dht-common.h b/xlators/cluster/dht/src/dht-common.h
190130
index 8e65111..1b3e826 100644
190130
--- a/xlators/cluster/dht/src/dht-common.h
190130
+++ b/xlators/cluster/dht/src/dht-common.h
190130
@@ -1549,4 +1549,10 @@ dht_check_remote_fd_failed_error(dht_local_t *local, int op_ret, int op_errno);
190130
 int
190130
 dht_dir_layout_error_check(xlator_t *this, inode_t *inode);
190130
 
190130
+int32_t
190130
+dht_create_lock(call_frame_t *frame, xlator_t *subvol);
190130
+
190130
+int
190130
+dht_set_parent_layout_in_dict(loc_t *loc, xlator_t *this, dht_local_t *local);
190130
+
190130
 #endif /* _DHT_H */
190130
diff --git a/xlators/protocol/client/src/client-rpc-fops_v2.c b/xlators/protocol/client/src/client-rpc-fops_v2.c
190130
index 2673b6e..613dda8 100644
190130
--- a/xlators/protocol/client/src/client-rpc-fops_v2.c
190130
+++ b/xlators/protocol/client/src/client-rpc-fops_v2.c
190130
@@ -2094,11 +2094,12 @@ client4_0_create_cbk(struct rpc_req *req, struct iovec *iov, int count,
190130
         goto out;
190130
     }
190130
 
190130
+    ret = client_post_create_v2(this, &rsp, &stbuf, &preparent, &postparent,
190130
+                                local, &xdata);
190130
+    if (ret < 0)
190130
+        goto out;
190130
+
190130
     if (-1 != rsp.op_ret) {
190130
-        ret = client_post_create_v2(this, &rsp, &stbuf, &preparent, &postparent,
190130
-                                    local, &xdata);
190130
-        if (ret < 0)
190130
-            goto out;
190130
         ret = client_add_fd_to_saved_fds(frame->this, fd, &local->loc,
190130
                                          local->flags, rsp.fd, 0);
190130
         if (ret) {
190130
diff --git a/xlators/storage/posix/src/posix-entry-ops.c b/xlators/storage/posix/src/posix-entry-ops.c
190130
index bea0bbf..65650b3 100644
190130
--- a/xlators/storage/posix/src/posix-entry-ops.c
190130
+++ b/xlators/storage/posix/src/posix-entry-ops.c
190130
@@ -2070,6 +2070,8 @@ posix_create(call_frame_t *frame, xlator_t *this, loc_t *loc, int32_t flags,
190130
     gf_boolean_t entry_created = _gf_false, gfid_set = _gf_false;
190130
     mode_t mode_bit = 0;
190130
 
190130
+    dict_t *xdata_rsp = dict_ref(xdata);
190130
+
190130
     DECLARE_OLD_FS_ID_VAR;
190130
 
190130
     VALIDATE_OR_GOTO(frame, out);
190130
@@ -2118,6 +2120,28 @@ posix_create(call_frame_t *frame, xlator_t *this, loc_t *loc, int32_t flags,
190130
         was_present = 0;
190130
     }
190130
 
190130
+    if (!was_present) {
190130
+        if (posix_is_layout_stale(xdata, par_path, this)) {
190130
+            op_ret = -1;
190130
+            op_errno = EIO;
190130
+            if (!xdata_rsp) {
190130
+                xdata_rsp = dict_new();
190130
+                if (!xdata_rsp) {
190130
+                    op_errno = ENOMEM;
190130
+                    goto out;
190130
+                }
190130
+            }
190130
+
190130
+            if (dict_set_int32_sizen(xdata_rsp, GF_PREOP_CHECK_FAILED, 1) ==
190130
+                -1) {
190130
+                gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_DICT_SET_FAILED,
190130
+                       "setting key %s in dict failed", GF_PREOP_CHECK_FAILED);
190130
+            }
190130
+
190130
+            goto out;
190130
+        }
190130
+    }
190130
+
190130
     if (priv->o_direct)
190130
         _flags |= O_DIRECT;
190130
 
190130
@@ -2239,7 +2263,10 @@ out:
190130
 
190130
     STACK_UNWIND_STRICT(create, frame, op_ret, op_errno, fd,
190130
                         (loc) ? loc->inode : NULL, &stbuf, &preparent,
190130
-                        &postparent, xdata);
190130
+                        &postparent, xdata_rsp);
190130
+
190130
+    if (xdata_rsp)
190130
+        dict_unref(xdata_rsp);
190130
 
190130
     return 0;
190130
 }
190130
diff --git a/xlators/storage/posix/src/posix-helpers.c b/xlators/storage/posix/src/posix-helpers.c
190130
index 35dd3b6..2c27d22 100644
190130
--- a/xlators/storage/posix/src/posix-helpers.c
190130
+++ b/xlators/storage/posix/src/posix-helpers.c
190130
@@ -3559,3 +3559,79 @@ posix_update_iatt_buf(struct iatt *buf, int fd, char *loc, dict_t *xattr_req)
190130
         }
190130
     }
190130
 }
190130
+
190130
+gf_boolean_t
190130
+posix_is_layout_stale(dict_t *xdata, char *par_path, xlator_t *this)
190130
+{
190130
+    int op_ret = 0;
190130
+    ssize_t size = 0;
190130
+    char value_buf[4096] = {
190130
+        0,
190130
+    };
190130
+    gf_boolean_t have_val = _gf_false;
190130
+    data_t *arg_data = NULL;
190130
+    char *xattr_name = NULL;
190130
+    gf_boolean_t is_stale = _gf_false;
190130
+
190130
+    op_ret = dict_get_str_sizen(xdata, GF_PREOP_PARENT_KEY, &xattr_name);
190130
+    if (xattr_name == NULL) {
190130
+        op_ret = 0;
190130
+        goto out;
190130
+    }
190130
+
190130
+    arg_data = dict_get(xdata, xattr_name);
190130
+    if (!arg_data) {
190130
+        op_ret = 0;
190130
+        goto out;
190130
+    }
190130
+
190130
+    size = sys_lgetxattr(par_path, xattr_name, value_buf,
190130
+                         sizeof(value_buf) - 1);
190130
+
190130
+    if (size >= 0) {
190130
+        have_val = _gf_true;
190130
+    } else {
190130
+        if (errno == ERANGE) {
190130
+            gf_msg(this->name, GF_LOG_INFO, errno, P_MSG_PREOP_CHECK_FAILED,
190130
+                   "getxattr on key (%s) path (%s) failed due to"
190130
+                   " buffer overflow",
190130
+                   xattr_name, par_path);
190130
+            size = sys_lgetxattr(par_path, xattr_name, NULL, 0);
190130
+        }
190130
+        if (size < 0) {
190130
+            op_ret = -1;
190130
+            gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_PREOP_CHECK_FAILED,
190130
+                   "getxattr on key (%s)  failed, path : %s", xattr_name,
190130
+                   par_path);
190130
+            goto out;
190130
+        }
190130
+    }
190130
+
190130
+    if (!have_val) {
190130
+        size = sys_lgetxattr(par_path, xattr_name, value_buf, size);
190130
+        if (size < 0) {
190130
+            gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_PREOP_CHECK_FAILED,
190130
+                   "getxattr on key (%s) failed (%s)", xattr_name,
190130
+                   strerror(errno));
190130
+            goto out;
190130
+        }
190130
+    }
190130
+
190130
+    if ((arg_data->len != size) || (memcmp(arg_data->data, value_buf, size))) {
190130
+        gf_msg(this->name, GF_LOG_INFO, EIO, P_MSG_PREOP_CHECK_FAILED,
190130
+               "failing preop as on-disk xattr value differs from argument "
190130
+               "value for key %s",
190130
+               xattr_name);
190130
+        op_ret = -1;
190130
+    }
190130
+
190130
+out:
190130
+    dict_del_sizen(xdata, xattr_name);
190130
+    dict_del_sizen(xdata, GF_PREOP_PARENT_KEY);
190130
+
190130
+    if (op_ret == -1) {
190130
+        is_stale = _gf_true;
190130
+    }
190130
+
190130
+    return is_stale;
190130
+}
190130
diff --git a/xlators/storage/posix/src/posix.h b/xlators/storage/posix/src/posix.h
190130
index dd51062..ac9d83c 100644
190130
--- a/xlators/storage/posix/src/posix.h
190130
+++ b/xlators/storage/posix/src/posix.h
190130
@@ -671,4 +671,8 @@ posix_spawn_ctx_janitor_thread(xlator_t *this);
190130
 
190130
 void
190130
 posix_update_iatt_buf(struct iatt *buf, int fd, char *loc, dict_t *xdata);
190130
+
190130
+gf_boolean_t
190130
+posix_is_layout_stale(dict_t *xdata, char *par_path, xlator_t *this);
190130
+
190130
 #endif /* _POSIX_H */
190130
-- 
190130
1.8.3.1
190130