e3c68b
From 2cf22e54c8424949607c4a20df84887b838b2702 Mon Sep 17 00:00:00 2001
e3c68b
From: Krutika Dhananjay <kdhananj@redhat.com>
e3c68b
Date: Fri, 15 May 2020 11:29:36 +0530
e3c68b
Subject: [PATCH 380/382] features/shard: Aggregate size, block-count in iatt
e3c68b
 before unwinding setxattr
e3c68b
e3c68b
Backport of:
e3c68b
> Upstream patch - https://review.gluster.org/c/glusterfs/+/24471
e3c68b
> Fixes: #1243
e3c68b
> Change-Id: I4da0eceb4235b91546df79270bcc0af8cd64e9ea
e3c68b
> Signed-off-by: Krutika Dhananjay <kdhananj@redhat.com>
e3c68b
e3c68b
Posix translator returns pre and postbufs in the dict in {F}SETXATTR fops.
e3c68b
These iatts are further cached at layers like md-cache.
e3c68b
Shard translator, in its current state, simply returns these values without
e3c68b
updating the aggregated file size and block-count.
e3c68b
e3c68b
This patch fixes this problem.
e3c68b
e3c68b
Change-Id: I4da0eceb4235b91546df79270bcc0af8cd64e9ea
e3c68b
BUG: 1823423
e3c68b
Signed-off-by: Krutika Dhananjay <kdhananj@redhat.com>
e3c68b
Reviewed-on: https://code.engineering.redhat.com/gerrit/201135
e3c68b
Tested-by: RHGS Build Bot <nigelb@redhat.com>
e3c68b
Reviewed-by: Xavi Hernandez Juan <xhernandez@redhat.com>
e3c68b
---
e3c68b
 tests/bugs/shard/issue-1243.t      |  31 ++++++
e3c68b
 xlators/features/shard/src/shard.c | 218 +++++++++++++++++++++++++++++++++----
e3c68b
 2 files changed, 225 insertions(+), 24 deletions(-)
e3c68b
 create mode 100644 tests/bugs/shard/issue-1243.t
e3c68b
e3c68b
diff --git a/tests/bugs/shard/issue-1243.t b/tests/bugs/shard/issue-1243.t
e3c68b
new file mode 100644
e3c68b
index 0000000..b0c092c
e3c68b
--- /dev/null
e3c68b
+++ b/tests/bugs/shard/issue-1243.t
e3c68b
@@ -0,0 +1,31 @@
e3c68b
+#!/bin/bash
e3c68b
+
e3c68b
+. $(dirname $0)/../../include.rc
e3c68b
+
e3c68b
+cleanup;
e3c68b
+
e3c68b
+TEST glusterd
e3c68b
+TEST pidof glusterd
e3c68b
+TEST $CLI volume create $V0 $H0:$B0/${V0}{0,1}
e3c68b
+TEST $CLI volume set $V0 features.shard on
e3c68b
+TEST $CLI volume set $V0 features.shard-block-size 4MB
e3c68b
+TEST $CLI volume set $V0 performance.quick-read off
e3c68b
+TEST $CLI volume set $V0 performance.io-cache off
e3c68b
+TEST $CLI volume set $V0 performance.read-ahead off
e3c68b
+TEST $CLI volume set $V0 performance.strict-o-direct on
e3c68b
+TEST $CLI volume start $V0
e3c68b
+
e3c68b
+TEST $GFS --volfile-id=$V0 --volfile-server=$H0 $M0
e3c68b
+
e3c68b
+TEST $CLI volume set $V0 md-cache-timeout 10
e3c68b
+
e3c68b
+# Write data into a file such that its size crosses shard-block-size
e3c68b
+TEST dd if=/dev/zero of=$M0/foo bs=1048576 count=8 oflag=direct
e3c68b
+
e3c68b
+# Execute a setxattr on the file.
e3c68b
+TEST setfattr -n trusted.libvirt -v some-value $M0/foo
e3c68b
+
e3c68b
+# Size of the file should be the aggregated size, not the shard-block-size
e3c68b
+EXPECT '8388608' stat -c %s $M0/foo
e3c68b
+
e3c68b
+cleanup
e3c68b
diff --git a/xlators/features/shard/src/shard.c b/xlators/features/shard/src/shard.c
e3c68b
index ee38ed2..6ae4c41 100644
e3c68b
--- a/xlators/features/shard/src/shard.c
e3c68b
+++ b/xlators/features/shard/src/shard.c
e3c68b
@@ -5929,36 +5929,206 @@ out:
e3c68b
   return 0;
e3c68b
 }
e3c68b
 
e3c68b
-int32_t shard_fsetxattr(call_frame_t *frame, xlator_t *this, fd_t *fd,
e3c68b
-                        dict_t *dict, int32_t flags, dict_t *xdata) {
e3c68b
-  int op_errno = EINVAL;
e3c68b
+int32_t shard_common_set_xattr_cbk(call_frame_t *frame, void *cookie,
e3c68b
+                                   xlator_t *this, int32_t op_ret,
e3c68b
+                                   int32_t op_errno, dict_t *xdata) {
e3c68b
+    int ret = -1;
e3c68b
+    struct iatt *prebuf = NULL;
e3c68b
+    struct iatt *postbuf = NULL;
e3c68b
+    struct iatt *stbuf = NULL;
e3c68b
+    data_t *data = NULL;
e3c68b
+    shard_local_t *local = NULL;
e3c68b
 
e3c68b
-  if (frame->root->pid != GF_CLIENT_PID_GSYNCD) {
e3c68b
-    GF_IF_INTERNAL_XATTR_GOTO(SHARD_XATTR_PREFIX "*", dict, op_errno, out);
e3c68b
-  }
e3c68b
+    local = frame->local;
e3c68b
 
e3c68b
-  STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->fsetxattr,
e3c68b
-                  fd, dict, flags, xdata);
e3c68b
-  return 0;
e3c68b
-out:
e3c68b
-  shard_common_failure_unwind(GF_FOP_FSETXATTR, frame, -1, op_errno);
e3c68b
-  return 0;
e3c68b
+    if (op_ret < 0) {
e3c68b
+        local->op_ret = op_ret;
e3c68b
+        local->op_errno = op_errno;
e3c68b
+        goto err;
e3c68b
+    }
e3c68b
+
e3c68b
+    if (!xdata)
e3c68b
+        goto unwind;
e3c68b
+
e3c68b
+    data = dict_get(xdata, GF_PRESTAT);
e3c68b
+    if (data) {
e3c68b
+        stbuf = data_to_iatt(data, GF_PRESTAT);
e3c68b
+        prebuf = GF_MALLOC(sizeof(struct iatt), gf_common_mt_char);
e3c68b
+        if (prebuf == NULL) {
e3c68b
+            local->op_ret = -1;
e3c68b
+            local->op_errno = ENOMEM;
e3c68b
+            goto err;
e3c68b
+        }
e3c68b
+        *prebuf = *stbuf;
e3c68b
+        prebuf->ia_size = local->prebuf.ia_size;
e3c68b
+        prebuf->ia_blocks = local->prebuf.ia_blocks;
e3c68b
+        ret = dict_set_iatt(xdata, GF_PRESTAT, prebuf, false);
e3c68b
+        if (ret < 0) {
e3c68b
+            local->op_ret = -1;
e3c68b
+            local->op_errno = ENOMEM;
e3c68b
+            goto err;
e3c68b
+        }
e3c68b
+        prebuf = NULL;
e3c68b
+    }
e3c68b
+
e3c68b
+    data = dict_get(xdata, GF_POSTSTAT);
e3c68b
+    if (data) {
e3c68b
+        stbuf = data_to_iatt(data, GF_POSTSTAT);
e3c68b
+        postbuf = GF_MALLOC(sizeof(struct iatt), gf_common_mt_char);
e3c68b
+        if (postbuf == NULL) {
e3c68b
+            local->op_ret = -1;
e3c68b
+            local->op_errno = ENOMEM;
e3c68b
+            goto err;
e3c68b
+        }
e3c68b
+        *postbuf = *stbuf;
e3c68b
+        postbuf->ia_size = local->prebuf.ia_size;
e3c68b
+        postbuf->ia_blocks = local->prebuf.ia_blocks;
e3c68b
+        ret = dict_set_iatt(xdata, GF_POSTSTAT, postbuf, false);
e3c68b
+        if (ret < 0) {
e3c68b
+            local->op_ret = -1;
e3c68b
+            local->op_errno = ENOMEM;
e3c68b
+            goto err;
e3c68b
+        }
e3c68b
+        postbuf = NULL;
e3c68b
+    }
e3c68b
+
e3c68b
+unwind:
e3c68b
+    if (local->fd)
e3c68b
+        SHARD_STACK_UNWIND(fsetxattr, frame, local->op_ret, local->op_errno,
e3c68b
+                           xdata);
e3c68b
+    else
e3c68b
+        SHARD_STACK_UNWIND(setxattr, frame, local->op_ret, local->op_errno,
e3c68b
+                           xdata);
e3c68b
+    return 0;
e3c68b
+
e3c68b
+err:
e3c68b
+    GF_FREE(prebuf);
e3c68b
+    GF_FREE(postbuf);
e3c68b
+    shard_common_failure_unwind(local->fop, frame, local->op_ret,
e3c68b
+                                local->op_errno);
e3c68b
+    return 0;
e3c68b
 }
e3c68b
 
e3c68b
-int32_t shard_setxattr(call_frame_t *frame, xlator_t *this, loc_t *loc,
e3c68b
-                       dict_t *dict, int32_t flags, dict_t *xdata) {
e3c68b
-  int op_errno = EINVAL;
e3c68b
+int32_t shard_post_lookup_set_xattr_handler(call_frame_t *frame,
e3c68b
+                                            xlator_t *this) {
e3c68b
+    shard_local_t *local = NULL;
e3c68b
 
e3c68b
-  if (frame->root->pid != GF_CLIENT_PID_GSYNCD) {
e3c68b
-    GF_IF_INTERNAL_XATTR_GOTO(SHARD_XATTR_PREFIX "*", dict, op_errno, out);
e3c68b
-  }
e3c68b
+    local = frame->local;
e3c68b
 
e3c68b
-  STACK_WIND_TAIL(frame, FIRST_CHILD(this), FIRST_CHILD(this)->fops->setxattr,
e3c68b
-                  loc, dict, flags, xdata);
e3c68b
-  return 0;
e3c68b
-out:
e3c68b
-  shard_common_failure_unwind(GF_FOP_SETXATTR, frame, -1, op_errno);
e3c68b
-  return 0;
e3c68b
+    if (local->op_ret < 0) {
e3c68b
+        shard_common_failure_unwind(local->fop, frame, local->op_ret,
e3c68b
+                                    local->op_errno);
e3c68b
+        return 0;
e3c68b
+    }
e3c68b
+
e3c68b
+    if (local->fd)
e3c68b
+        STACK_WIND(frame, shard_common_set_xattr_cbk, FIRST_CHILD(this),
e3c68b
+                   FIRST_CHILD(this)->fops->fsetxattr, local->fd,
e3c68b
+                   local->xattr_req, local->flags, local->xattr_rsp);
e3c68b
+    else
e3c68b
+        STACK_WIND(frame, shard_common_set_xattr_cbk, FIRST_CHILD(this),
e3c68b
+                   FIRST_CHILD(this)->fops->setxattr, &local->loc,
e3c68b
+                   local->xattr_req, local->flags, local->xattr_rsp);
e3c68b
+    return 0;
e3c68b
+}
e3c68b
+
e3c68b
+int32_t shard_common_set_xattr(call_frame_t *frame, xlator_t *this,
e3c68b
+                               glusterfs_fop_t fop, loc_t *loc, fd_t *fd,
e3c68b
+                               dict_t *dict, int32_t flags, dict_t *xdata) {
e3c68b
+    int ret = -1;
e3c68b
+    int op_errno = ENOMEM;
e3c68b
+    uint64_t block_size = 0;
e3c68b
+    shard_local_t *local = NULL;
e3c68b
+    inode_t *inode = loc ? loc->inode : fd->inode;
e3c68b
+
e3c68b
+    if ((IA_ISDIR(inode->ia_type)) || (IA_ISLNK(inode->ia_type))) {
e3c68b
+        if (loc)
e3c68b
+            STACK_WIND_TAIL(frame, FIRST_CHILD(this),
e3c68b
+                            FIRST_CHILD(this)->fops->setxattr, loc, dict, flags,
e3c68b
+                            xdata);
e3c68b
+        else
e3c68b
+            STACK_WIND_TAIL(frame, FIRST_CHILD(this),
e3c68b
+                            FIRST_CHILD(this)->fops->fsetxattr, fd, dict, flags,
e3c68b
+                            xdata);
e3c68b
+        return 0;
e3c68b
+    }
e3c68b
+
e3c68b
+    /* Sharded or not, if shard's special xattrs are attempted to be set,
e3c68b
+     * fail the fop with EPERM (except if the client is gsyncd.
e3c68b
+     */
e3c68b
+    if (frame->root->pid != GF_CLIENT_PID_GSYNCD) {
e3c68b
+        GF_IF_INTERNAL_XATTR_GOTO(SHARD_XATTR_PREFIX "*", dict, op_errno, err);
e3c68b
+    }
e3c68b
+
e3c68b
+    ret = shard_inode_ctx_get_block_size(inode, this, &block_size);
e3c68b
+    if (ret) {
e3c68b
+        gf_msg(this->name, GF_LOG_ERROR, 0, SHARD_MSG_INODE_CTX_GET_FAILED,
e3c68b
+               "Failed to get block size from inode ctx of %s",
e3c68b
+               uuid_utoa(inode->gfid));
e3c68b
+        goto err;
e3c68b
+    }
e3c68b
+
e3c68b
+    if (!block_size || frame->root->pid == GF_CLIENT_PID_GSYNCD) {
e3c68b
+        if (loc)
e3c68b
+            STACK_WIND_TAIL(frame, FIRST_CHILD(this),
e3c68b
+                            FIRST_CHILD(this)->fops->setxattr, loc, dict, flags,
e3c68b
+                            xdata);
e3c68b
+        else
e3c68b
+            STACK_WIND_TAIL(frame, FIRST_CHILD(this),
e3c68b
+                            FIRST_CHILD(this)->fops->fsetxattr, fd, dict, flags,
e3c68b
+                            xdata);
e3c68b
+        return 0;
e3c68b
+    }
e3c68b
+
e3c68b
+    local = mem_get0(this->local_pool);
e3c68b
+    if (!local)
e3c68b
+        goto err;
e3c68b
+
e3c68b
+    frame->local = local;
e3c68b
+    local->fop = fop;
e3c68b
+    if (loc) {
e3c68b
+        if (loc_copy(&local->loc, loc) != 0)
e3c68b
+            goto err;
e3c68b
+    }
e3c68b
+
e3c68b
+    if (fd) {
e3c68b
+        local->fd = fd_ref(fd);
e3c68b
+        local->loc.inode = inode_ref(fd->inode);
e3c68b
+        gf_uuid_copy(local->loc.gfid, fd->inode->gfid);
e3c68b
+    }
e3c68b
+    local->flags = flags;
e3c68b
+    /* Reusing local->xattr_req and local->xattr_rsp to store the setxattr dict
e3c68b
+     * and the xdata dict
e3c68b
+     */
e3c68b
+    if (dict)
e3c68b
+        local->xattr_req = dict_ref(dict);
e3c68b
+    if (xdata)
e3c68b
+        local->xattr_rsp = dict_ref(xdata);
e3c68b
+
e3c68b
+    /* To-Do: Switch from LOOKUP which is path-based, to FSTAT if the fop is
e3c68b
+     * on an fd. This comes under a generic class of bugs in shard tracked by
e3c68b
+     * bz #1782428.
e3c68b
+     */
e3c68b
+    shard_lookup_base_file(frame, this, &local->loc,
e3c68b
+                           shard_post_lookup_set_xattr_handler);
e3c68b
+    return 0;
e3c68b
+err:
e3c68b
+    shard_common_failure_unwind(fop, frame, -1, op_errno);
e3c68b
+    return 0;
e3c68b
+}
e3c68b
+
e3c68b
+int32_t shard_fsetxattr(call_frame_t *frame, xlator_t *this, fd_t *fd,
e3c68b
+                        dict_t *dict, int32_t flags, dict_t *xdata) {
e3c68b
+    shard_common_set_xattr(frame, this, GF_FOP_FSETXATTR, NULL, fd, dict, flags,
e3c68b
+                           xdata);
e3c68b
+    return 0;
e3c68b
+}
e3c68b
+
e3c68b
+int32_t shard_setxattr(call_frame_t *frame, xlator_t *this, loc_t *loc,
e3c68b
+                       dict_t *dict, int32_t flags, dict_t *xdata) {
e3c68b
+    shard_common_set_xattr(frame, this, GF_FOP_SETXATTR, loc, NULL, dict, flags,
e3c68b
+                           xdata);
e3c68b
+    return 0;
e3c68b
 }
e3c68b
 
e3c68b
 int shard_post_setattr_handler(call_frame_t *frame, xlator_t *this) {
e3c68b
-- 
e3c68b
1.8.3.1
e3c68b