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