e3c68b
From 4097a748cbb7616d78886b35e3360177d570b7a6 Mon Sep 17 00:00:00 2001
e3c68b
From: Krutika Dhananjay <kdhananj@redhat.com>
e3c68b
Date: Fri, 22 May 2020 13:25:26 +0530
e3c68b
Subject: [PATCH 382/382] features/shard: Aggregate file size, block-count
e3c68b
 before unwinding removexattr
e3c68b
e3c68b
Posix translator returns pre and postbufs in the dict in {F}REMOVEXATTR 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
Upstream patch:
e3c68b
> Upstream patch link: https://review.gluster.org/c/glusterfs/+/24480
e3c68b
> Change-Id: I4b2dd41ede472c5829af80a67401ec5a6376d872
e3c68b
> Fixes: #1243
e3c68b
> Signed-off-by: Krutika Dhananjay <kdhananj@redhat.com>
e3c68b
e3c68b
Change-Id: I4b2dd41ede472c5829af80a67401ec5a6376d872
e3c68b
BUG: 1823423
e3c68b
Signed-off-by: Xavi Hernandez <xhernandez@redhat.com>
e3c68b
Reviewed-on: https://code.engineering.redhat.com/gerrit/201456
e3c68b
Tested-by: RHGS Build Bot <nigelb@redhat.com>
e3c68b
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
e3c68b
---
e3c68b
 tests/bugs/shard/issue-1243.t      |  12 ++
e3c68b
 xlators/features/shard/src/shard.c | 293 ++++++++++++++++++++++++++-----------
e3c68b
 xlators/features/shard/src/shard.h |   1 +
e3c68b
 3 files changed, 224 insertions(+), 82 deletions(-)
e3c68b
e3c68b
diff --git a/tests/bugs/shard/issue-1243.t b/tests/bugs/shard/issue-1243.t
e3c68b
index b0c092c..ba22d2b 100644
e3c68b
--- a/tests/bugs/shard/issue-1243.t
e3c68b
+++ b/tests/bugs/shard/issue-1243.t
e3c68b
@@ -1,6 +1,7 @@
e3c68b
 #!/bin/bash
e3c68b
 
e3c68b
 . $(dirname $0)/../../include.rc
e3c68b
+. $(dirname $0)/../../volume.rc
e3c68b
 
e3c68b
 cleanup;
e3c68b
 
e3c68b
@@ -22,10 +23,21 @@ TEST $CLI volume set $V0 md-cache-timeout 10
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
+EXPECT_WITHIN $UMOUNT_TIMEOUT "Y" force_umount $M0
e3c68b
+TEST $GFS --volfile-id=$V0 --volfile-server=$H0 $M0
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
+EXPECT_WITHIN $UMOUNT_TIMEOUT "Y" force_umount $M0
e3c68b
+TEST $GFS --volfile-id=$V0 --volfile-server=$H0 $M0
e3c68b
+
e3c68b
+# Execute a removexattr on the file.
e3c68b
+TEST setfattr -x trusted.libvirt $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
 cleanup
e3c68b
diff --git a/xlators/features/shard/src/shard.c b/xlators/features/shard/src/shard.c
e3c68b
index 6ae4c41..2e2ef5d 100644
e3c68b
--- a/xlators/features/shard/src/shard.c
e3c68b
+++ b/xlators/features/shard/src/shard.c
e3c68b
@@ -442,6 +442,9 @@ void shard_local_wipe(shard_local_t *local) {
e3c68b
   loc_wipe(&local->int_entrylk.loc);
e3c68b
   loc_wipe(&local->newloc);
e3c68b
 
e3c68b
+  if (local->name)
e3c68b
+    GF_FREE(local->name);
e3c68b
+
e3c68b
   if (local->int_entrylk.basename)
e3c68b
     GF_FREE(local->int_entrylk.basename);
e3c68b
   if (local->fd)
e3c68b
@@ -5819,46 +5822,216 @@ int32_t shard_readdirp(call_frame_t *frame, xlator_t *this, fd_t *fd,
e3c68b
   return 0;
e3c68b
 }
e3c68b
 
e3c68b
-int32_t shard_removexattr(call_frame_t *frame, xlator_t *this, loc_t *loc,
e3c68b
-                          const char *name, dict_t *xdata) {
e3c68b
-  int op_errno = EINVAL;
e3c68b
+int32_t
e3c68b
+shard_modify_and_set_iatt_in_dict(dict_t *xdata, shard_local_t *local,
e3c68b
+                                  char *key)
e3c68b
+{
e3c68b
+    int ret = 0;
e3c68b
+    struct iatt *tmpbuf = NULL;
e3c68b
+    struct iatt *stbuf = NULL;
e3c68b
+    data_t *data = NULL;
e3c68b
 
e3c68b
-  if (frame->root->pid != GF_CLIENT_PID_GSYNCD) {
e3c68b
-    GF_IF_NATIVE_XATTR_GOTO(SHARD_XATTR_PREFIX "*", name, op_errno, out);
e3c68b
-  }
e3c68b
+    if (!xdata)
e3c68b
+        return 0;
e3c68b
 
e3c68b
-  if (xdata && (frame->root->pid != GF_CLIENT_PID_GSYNCD)) {
e3c68b
-    dict_del(xdata, GF_XATTR_SHARD_BLOCK_SIZE);
e3c68b
-    dict_del(xdata, GF_XATTR_SHARD_FILE_SIZE);
e3c68b
-  }
e3c68b
+    data = dict_get(xdata, key);
e3c68b
+    if (!data)
e3c68b
+        return 0;
e3c68b
 
e3c68b
-  STACK_WIND_TAIL(frame, FIRST_CHILD(this),
e3c68b
-                  FIRST_CHILD(this)->fops->removexattr, loc, name, xdata);
e3c68b
-  return 0;
e3c68b
-out:
e3c68b
-  shard_common_failure_unwind(GF_FOP_REMOVEXATTR, frame, -1, op_errno);
e3c68b
-  return 0;
e3c68b
+    tmpbuf = data_to_iatt(data, key);
e3c68b
+    stbuf = GF_MALLOC(sizeof(struct iatt), gf_common_mt_char);
e3c68b
+    if (stbuf == NULL) {
e3c68b
+        local->op_ret = -1;
e3c68b
+        local->op_errno = ENOMEM;
e3c68b
+        goto err;
e3c68b
+    }
e3c68b
+    *stbuf = *tmpbuf;
e3c68b
+    stbuf->ia_size = local->prebuf.ia_size;
e3c68b
+    stbuf->ia_blocks = local->prebuf.ia_blocks;
e3c68b
+    ret = dict_set_iatt(xdata, key, stbuf, false);
e3c68b
+    if (ret < 0) {
e3c68b
+        local->op_ret = -1;
e3c68b
+        local->op_errno = ENOMEM;
e3c68b
+        goto err;
e3c68b
+    }
e3c68b
+    return 0;
e3c68b
+
e3c68b
+err:
e3c68b
+    GF_FREE(stbuf);
e3c68b
+    return -1;
e3c68b
 }
e3c68b
 
e3c68b
-int32_t shard_fremovexattr(call_frame_t *frame, xlator_t *this, fd_t *fd,
e3c68b
-                           const char *name, dict_t *xdata) {
e3c68b
-  int op_errno = EINVAL;
e3c68b
+int32_t
e3c68b
+shard_common_remove_xattr_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
e3c68b
+                              int32_t op_ret, int32_t op_errno, dict_t *xdata)
e3c68b
+{
e3c68b
+    int ret = -1;
e3c68b
+    shard_local_t *local = NULL;
e3c68b
 
e3c68b
-  if (frame->root->pid != GF_CLIENT_PID_GSYNCD) {
e3c68b
-    GF_IF_NATIVE_XATTR_GOTO(SHARD_XATTR_PREFIX "*", name, op_errno, out);
e3c68b
-  }
e3c68b
+    local = frame->local;
e3c68b
 
e3c68b
-  if (xdata && (frame->root->pid != GF_CLIENT_PID_GSYNCD)) {
e3c68b
-    dict_del(xdata, GF_XATTR_SHARD_BLOCK_SIZE);
e3c68b
-    dict_del(xdata, GF_XATTR_SHARD_FILE_SIZE);
e3c68b
-  }
e3c68b
+    if (op_ret < 0) {
e3c68b
+        local->op_ret = op_ret;
e3c68b
+        local->op_errno = op_errno;
e3c68b
+        goto err;
e3c68b
+    }
e3c68b
 
e3c68b
-  STACK_WIND_TAIL(frame, FIRST_CHILD(this),
e3c68b
-                  FIRST_CHILD(this)->fops->fremovexattr, fd, name, xdata);
e3c68b
-  return 0;
e3c68b
-out:
e3c68b
-  shard_common_failure_unwind(GF_FOP_FREMOVEXATTR, frame, -1, op_errno);
e3c68b
-  return 0;
e3c68b
+    ret = shard_modify_and_set_iatt_in_dict(xdata, local, GF_PRESTAT);
e3c68b
+    if (ret < 0)
e3c68b
+        goto err;
e3c68b
+
e3c68b
+    ret = shard_modify_and_set_iatt_in_dict(xdata, local, GF_POSTSTAT);
e3c68b
+    if (ret < 0)
e3c68b
+        goto err;
e3c68b
+
e3c68b
+    if (local->fd)
e3c68b
+        SHARD_STACK_UNWIND(fremovexattr, frame, local->op_ret, local->op_errno,
e3c68b
+                           xdata);
e3c68b
+    else
e3c68b
+        SHARD_STACK_UNWIND(removexattr, frame, local->op_ret, local->op_errno,
e3c68b
+                           xdata);
e3c68b
+    return 0;
e3c68b
+
e3c68b
+err:
e3c68b
+    shard_common_failure_unwind(local->fop, frame, local->op_ret,
e3c68b
+                                local->op_errno);
e3c68b
+    return 0;
e3c68b
+}
e3c68b
+
e3c68b
+int32_t
e3c68b
+shard_post_lookup_remove_xattr_handler(call_frame_t *frame, xlator_t *this)
e3c68b
+{
e3c68b
+    shard_local_t *local = NULL;
e3c68b
+
e3c68b
+    local = frame->local;
e3c68b
+
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_remove_xattr_cbk, FIRST_CHILD(this),
e3c68b
+                   FIRST_CHILD(this)->fops->fremovexattr, local->fd,
e3c68b
+                   local->name, local->xattr_req);
e3c68b
+    else
e3c68b
+        STACK_WIND(frame, shard_common_remove_xattr_cbk, FIRST_CHILD(this),
e3c68b
+                   FIRST_CHILD(this)->fops->removexattr, &local->loc,
e3c68b
+                   local->name, local->xattr_req);
e3c68b
+    return 0;
e3c68b
+}
e3c68b
+
e3c68b
+int32_t
e3c68b
+shard_common_remove_xattr(call_frame_t *frame, xlator_t *this,
e3c68b
+                          glusterfs_fop_t fop, loc_t *loc, fd_t *fd,
e3c68b
+                          const char *name, dict_t *xdata)
e3c68b
+{
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->removexattr, loc, name,
e3c68b
+                            xdata);
e3c68b
+        else
e3c68b
+            STACK_WIND_TAIL(frame, FIRST_CHILD(this),
e3c68b
+                            FIRST_CHILD(this)->fops->fremovexattr, fd, name,
e3c68b
+                            xdata);
e3c68b
+        return 0;
e3c68b
+    }
e3c68b
+
e3c68b
+    /* If shard's special xattrs are attempted to be removed,
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_NATIVE_XATTR_GOTO(SHARD_XATTR_PREFIX "*", name, op_errno, err);
e3c68b
+    }
e3c68b
+
e3c68b
+    /* Repeat the same check for bulk-removexattr */
e3c68b
+    if (xdata && (frame->root->pid != GF_CLIENT_PID_GSYNCD)) {
e3c68b
+        dict_del(xdata, GF_XATTR_SHARD_BLOCK_SIZE);
e3c68b
+        dict_del(xdata, GF_XATTR_SHARD_FILE_SIZE);
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->removexattr, loc, name,
e3c68b
+                            xdata);
e3c68b
+        else
e3c68b
+            STACK_WIND_TAIL(frame, FIRST_CHILD(this),
e3c68b
+                            FIRST_CHILD(this)->fops->fremovexattr, fd, name,
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
+
e3c68b
+    if (name) {
e3c68b
+        local->name = gf_strdup(name);
e3c68b
+        if (!local->name)
e3c68b
+            goto err;
e3c68b
+    }
e3c68b
+
e3c68b
+    if (xdata)
e3c68b
+        local->xattr_req = 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_remove_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
e3c68b
+shard_removexattr(call_frame_t *frame, xlator_t *this, loc_t *loc,
e3c68b
+                  const char *name, dict_t *xdata)
e3c68b
+{
e3c68b
+    shard_common_remove_xattr(frame, this, GF_FOP_REMOVEXATTR, loc, NULL, name,
e3c68b
+                              xdata);
e3c68b
+    return 0;
e3c68b
+}
e3c68b
+
e3c68b
+int32_t
e3c68b
+shard_fremovexattr(call_frame_t *frame, xlator_t *this, fd_t *fd,
e3c68b
+                   const char *name, dict_t *xdata)
e3c68b
+{
e3c68b
+    shard_common_remove_xattr(frame, this, GF_FOP_FREMOVEXATTR, NULL, fd, name,
e3c68b
+                              xdata);
e3c68b
+    return 0;
e3c68b
 }
e3c68b
 
e3c68b
 int32_t shard_fgetxattr_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
e3c68b
@@ -5933,10 +6106,6 @@ 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
     local = frame->local;
e3c68b
@@ -5947,52 +6116,14 @@ int32_t shard_common_set_xattr_cbk(call_frame_t *frame, void *cookie,
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
+    ret = shard_modify_and_set_iatt_in_dict(xdata, local, GF_PRESTAT);
e3c68b
+    if (ret < 0)
e3c68b
+        goto err;
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
+    ret = shard_modify_and_set_iatt_in_dict(xdata, local, GF_POSTSTAT);
e3c68b
+    if (ret < 0)
e3c68b
+        goto err;
e3c68b
 
e3c68b
-unwind:
e3c68b
     if (local->fd)
e3c68b
         SHARD_STACK_UNWIND(fsetxattr, frame, local->op_ret, local->op_errno,
e3c68b
                            xdata);
e3c68b
@@ -6002,8 +6133,6 @@ unwind:
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
diff --git a/xlators/features/shard/src/shard.h b/xlators/features/shard/src/shard.h
e3c68b
index 04abd62..1721417 100644
e3c68b
--- a/xlators/features/shard/src/shard.h
e3c68b
+++ b/xlators/features/shard/src/shard.h
e3c68b
@@ -318,6 +318,7 @@ typedef struct shard_local {
e3c68b
     uint32_t deletion_rate;
e3c68b
     gf_boolean_t cleanup_required;
e3c68b
     uuid_t base_gfid;
e3c68b
+    char *name;
e3c68b
 } shard_local_t;
e3c68b
 
e3c68b
 typedef struct shard_inode_ctx {
e3c68b
-- 
e3c68b
1.8.3.1
e3c68b