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