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