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