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