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