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