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