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