e7a346
From 9f670a342ffed3eee7cb91a67dcc2f2a27600983 Mon Sep 17 00:00:00 2001
e7a346
From: karthik-us <ksubrahm@redhat.com>
e7a346
Date: Fri, 23 Feb 2018 15:12:19 +0530
e7a346
Subject: [PATCH 191/201] cluster/afr: Make afr_fsync a transaction
e7a346
e7a346
Upstream patch: https://review.gluster.org/#/c/19621/
e7a346
e7a346
Change-Id: I713401feb96393f668efb074f2d5b870d19e6fda
e7a346
BUG: 1552425
e7a346
Signed-off-by: karthik-us <ksubrahm@redhat.com>
e7a346
Reviewed-on: https://code.engineering.redhat.com/gerrit/131942
e7a346
Tested-by: RHGS Build Bot <nigelb@redhat.com>
e7a346
Reviewed-by: Pranith Kumar Karampuri <pkarampu@redhat.com>
e7a346
---
e7a346
 xlators/cluster/afr/src/afr-common.c      | 163 ------------------------------
e7a346
 xlators/cluster/afr/src/afr-inode-write.c | 108 ++++++++++++++++++++
e7a346
 xlators/cluster/afr/src/afr-inode-write.h |   4 +
e7a346
 xlators/cluster/afr/src/afr.c             |   2 +-
e7a346
 xlators/cluster/afr/src/afr.h             |   4 +
e7a346
 5 files changed, 117 insertions(+), 164 deletions(-)
e7a346
e7a346
diff --git a/xlators/cluster/afr/src/afr-common.c b/xlators/cluster/afr/src/afr-common.c
e7a346
index 855e568..a790402 100644
e7a346
--- a/xlators/cluster/afr/src/afr-common.c
e7a346
+++ b/xlators/cluster/afr/src/afr-common.c
e7a346
@@ -3435,169 +3435,6 @@ out:
e7a346
         return 0;
e7a346
 }
e7a346
 
e7a346
-/* }}} */
e7a346
-
e7a346
-
e7a346
-/* {{{ fsync */
e7a346
-
e7a346
-int
e7a346
-afr_fsync_unwind_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
e7a346
-                      int32_t op_ret, int32_t op_errno, struct iatt *prebuf,
e7a346
-                      struct iatt *postbuf, dict_t *xdata)
e7a346
-{
e7a346
-        AFR_STACK_UNWIND (fsync, frame, op_ret, op_errno, prebuf, postbuf,
e7a346
-                          xdata);
e7a346
-        return 0;
e7a346
-}
e7a346
-
e7a346
-int
e7a346
-afr_fsync_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
e7a346
-               int32_t op_ret, int32_t op_errno, struct iatt *prebuf,
e7a346
-               struct iatt *postbuf, dict_t *xdata)
e7a346
-{
e7a346
-        afr_local_t *local = NULL;
e7a346
-        afr_private_t *priv = NULL;
e7a346
-        int i = 0;
e7a346
-        int call_count = -1;
e7a346
-        int child_index = (long) cookie;
e7a346
-	int read_subvol = 0;
e7a346
-	call_stub_t *stub = NULL;
e7a346
-
e7a346
-        local = frame->local;
e7a346
-        priv = this->private;
e7a346
-
e7a346
-        LOCK (&frame->lock);
e7a346
-        {
e7a346
-                local->replies[child_index].valid = 1;
e7a346
-                local->replies[child_index].op_ret = op_ret;
e7a346
-                local->replies[child_index].op_errno = op_errno;
e7a346
-                if (op_ret == 0) {
e7a346
-                        if (prebuf)
e7a346
-                                local->replies[child_index].prestat = *prebuf;
e7a346
-                        if (postbuf)
e7a346
-                                local->replies[child_index].poststat = *postbuf;
e7a346
-                        if (xdata)
e7a346
-                                local->replies[child_index].xdata =
e7a346
-                                        dict_ref (xdata);
e7a346
-                }
e7a346
-        }
e7a346
-        UNLOCK (&frame->lock);
e7a346
-
e7a346
-        call_count = afr_frame_return (frame);
e7a346
-
e7a346
-        if (call_count == 0) {
e7a346
-                local->op_ret = -1;
e7a346
-                local->op_errno = afr_final_errno (local, priv);
e7a346
-	        read_subvol = afr_data_subvol_get (local->inode, this, NULL,
e7a346
-                                                   local->readable, NULL, NULL);
e7a346
-                /* Pick a reply that is valid and readable, with a preference
e7a346
-                 * given to read_subvol. */
e7a346
-                for (i = 0; i < priv->child_count; i++) {
e7a346
-                        if (!local->replies[i].valid)
e7a346
-                                continue;
e7a346
-                        if (local->replies[i].op_ret != 0)
e7a346
-                                continue;
e7a346
-                        if (!local->readable[i])
e7a346
-                                continue;
e7a346
-                        local->op_ret = local->replies[i].op_ret;
e7a346
-                        local->op_errno = local->replies[i].op_errno;
e7a346
-                        local->cont.inode_wfop.prebuf =
e7a346
-                                local->replies[i].prestat;
e7a346
-                        local->cont.inode_wfop.postbuf =
e7a346
-                                local->replies[i].poststat;
e7a346
-                        if (local->replies[i].xdata) {
e7a346
-                                if (local->xdata_rsp)
e7a346
-                                        dict_unref (local->xdata_rsp);
e7a346
-                                local->xdata_rsp =
e7a346
-                                        dict_ref (local->replies[i].xdata);
e7a346
-                        }
e7a346
-                        if (i == read_subvol)
e7a346
-                                break;
e7a346
-                }
e7a346
-
e7a346
-		/* Make a stub out of the frame, and register it
e7a346
-		   with the waking up post-op. When the call-stub resumes,
e7a346
-		   we are guaranteed that there was no post-op pending
e7a346
-		   (i.e changelogs were unset in the server). This is an
e7a346
-		   essential "guarantee", that fsync() returns only after
e7a346
-		   completely finishing EVERYTHING, including the delayed
e7a346
-		   post-op. This guarantee is expected by FUSE graph switching
e7a346
-		   for example.
e7a346
-		*/
e7a346
-		stub = fop_fsync_cbk_stub (frame, afr_fsync_unwind_cbk,
e7a346
-                                           local->op_ret, local->op_errno,
e7a346
-                                           &local->cont.inode_wfop.prebuf,
e7a346
-                                           &local->cont.inode_wfop.postbuf,
e7a346
-                                           local->xdata_rsp);
e7a346
-		if (!stub) {
e7a346
-			AFR_STACK_UNWIND (fsync, frame, -1, ENOMEM, 0, 0, 0);
e7a346
-			return 0;
e7a346
-		}
e7a346
-
e7a346
-		/* If no new unstable writes happened between the
e7a346
-		   time we cleared the unstable write witness flag in afr_fsync
e7a346
-		   and now, calling afr_delayed_changelog_wake_up() should
e7a346
-		   wake up and skip over the fsync phase and go straight to
e7a346
-		   afr_changelog_post_op_now()
e7a346
-		*/
e7a346
-		afr_delayed_changelog_wake_resume (this, local->fd, stub);
e7a346
-        }
e7a346
-
e7a346
-        return 0;
e7a346
-}
e7a346
-
e7a346
-
e7a346
-int
e7a346
-afr_fsync (call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t datasync,
e7a346
-	   dict_t *xdata)
e7a346
-{
e7a346
-	afr_private_t *priv = NULL;
e7a346
-        afr_local_t *local = NULL;
e7a346
-        int i = 0;
e7a346
-        int32_t call_count = 0;
e7a346
-        int32_t op_errno = ENOMEM;
e7a346
-
e7a346
-	priv = this->private;
e7a346
-
e7a346
-	local = AFR_FRAME_INIT (frame, op_errno);
e7a346
-	if (!local)
e7a346
-		goto out;
e7a346
-
e7a346
-        local->op = GF_FOP_FSYNC;
e7a346
-	if (!afr_is_consistent_io_possible (local, priv, &op_errno))
e7a346
-		goto out;
e7a346
-
e7a346
-        local->fd = fd_ref (fd);
e7a346
-
e7a346
-	if (afr_fd_has_witnessed_unstable_write (this, fd)) {
e7a346
-		/* don't care. we only wanted to CLEAR the bit */
e7a346
-	}
e7a346
-
e7a346
-	local->inode = inode_ref (fd->inode);
e7a346
-
e7a346
-        call_count = local->call_count;
e7a346
-        for (i = 0; i < priv->child_count; i++) {
e7a346
-                if (local->child_up[i]) {
e7a346
-                        STACK_WIND_COOKIE (frame, afr_fsync_cbk,
e7a346
-                                           (void *) (long) i,
e7a346
-                                           priv->children[i],
e7a346
-                                           priv->children[i]->fops->fsync,
e7a346
-                                           fd, datasync, xdata);
e7a346
-                        if (!--call_count)
e7a346
-                                break;
e7a346
-                }
e7a346
-        }
e7a346
-
e7a346
-	return 0;
e7a346
-out:
e7a346
-	AFR_STACK_UNWIND (fsync, frame, -1, op_errno, NULL, NULL, NULL);
e7a346
-
e7a346
-        return 0;
e7a346
-}
e7a346
-
e7a346
-/* }}} */
e7a346
-
e7a346
-/* {{{ fsync */
e7a346
 
e7a346
 int
e7a346
 afr_fsyncdir_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
e7a346
diff --git a/xlators/cluster/afr/src/afr-inode-write.c b/xlators/cluster/afr/src/afr-inode-write.c
e7a346
index f0231b7..0e50443 100644
e7a346
--- a/xlators/cluster/afr/src/afr-inode-write.c
e7a346
+++ b/xlators/cluster/afr/src/afr-inode-write.c
e7a346
@@ -2539,3 +2539,111 @@ out:
e7a346
 	AFR_STACK_UNWIND (fxattrop, frame, -1, op_errno, NULL, NULL);
e7a346
         return 0;
e7a346
 }
e7a346
+
e7a346
+
e7a346
+int
e7a346
+afr_fsync_unwind (call_frame_t *frame, xlator_t *this)
e7a346
+{
e7a346
+        afr_local_t *local = NULL;
e7a346
+        call_frame_t   *main_frame = NULL;
e7a346
+
e7a346
+        local = frame->local;
e7a346
+
e7a346
+        main_frame = afr_transaction_detach_fop_frame (frame);
e7a346
+        if (!main_frame)
e7a346
+                return 0;
e7a346
+
e7a346
+        AFR_STACK_UNWIND (fsync, main_frame, local->op_ret, local->op_errno,
e7a346
+                          &local->cont.inode_wfop.prebuf,
e7a346
+                          &local->cont.inode_wfop.postbuf, local->xdata_rsp);
e7a346
+
e7a346
+        return 0;
e7a346
+}
e7a346
+
e7a346
+
e7a346
+int
e7a346
+afr_fsync_wind_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
e7a346
+                    int32_t op_ret, int32_t op_errno, struct iatt *prebuf,
e7a346
+                    struct iatt *postbuf, dict_t *xdata)
e7a346
+{
e7a346
+        return __afr_inode_write_cbk (frame, cookie, this, op_ret, op_errno,
e7a346
+                                      prebuf, postbuf, NULL, xdata);
e7a346
+}
e7a346
+
e7a346
+
e7a346
+int
e7a346
+afr_fsync_wind (call_frame_t *frame, xlator_t *this, int subvol)
e7a346
+{
e7a346
+        afr_local_t *local = NULL;
e7a346
+        afr_private_t *priv = NULL;
e7a346
+
e7a346
+        local = frame->local;
e7a346
+        priv = this->private;
e7a346
+
e7a346
+        STACK_WIND_COOKIE (frame, afr_fsync_wind_cbk, (void *)(long) subvol,
e7a346
+                           priv->children[subvol],
e7a346
+                           priv->children[subvol]->fops->fsync,
e7a346
+                           local->fd, local->cont.fsync.datasync,
e7a346
+                           local->xdata_req);
e7a346
+        return 0;
e7a346
+}
e7a346
+
e7a346
+int
e7a346
+afr_fsync (call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t datasync,
e7a346
+	   dict_t *xdata)
e7a346
+{
e7a346
+        afr_local_t *local = NULL;
e7a346
+        call_frame_t *transaction_frame = NULL;
e7a346
+        int ret = -1;
e7a346
+        int32_t op_errno = ENOMEM;
e7a346
+
e7a346
+        transaction_frame = copy_frame (frame);
e7a346
+        if (!transaction_frame)
e7a346
+                goto out;
e7a346
+
e7a346
+	local = AFR_FRAME_INIT (transaction_frame, op_errno);
e7a346
+	if (!local)
e7a346
+		goto out;
e7a346
+
e7a346
+        if (xdata)
e7a346
+                local->xdata_req = dict_copy_with_ref (xdata, NULL);
e7a346
+        else
e7a346
+                local->xdata_req = dict_new ();
e7a346
+
e7a346
+        if (!local->xdata_req)
e7a346
+                goto out;
e7a346
+
e7a346
+        local->fd = fd_ref (fd);
e7a346
+        ret = afr_set_inode_local (this, local, fd->inode);
e7a346
+        if (ret)
e7a346
+                goto out;
e7a346
+
e7a346
+        local->op = GF_FOP_FSYNC;
e7a346
+        local->cont.fsync.datasync = datasync;
e7a346
+
e7a346
+	if (afr_fd_has_witnessed_unstable_write (this, fd)) {
e7a346
+		/* don't care. we only wanted to CLEAR the bit */
e7a346
+	}
e7a346
+
e7a346
+        local->transaction.wind   = afr_fsync_wind;
e7a346
+        local->transaction.fop    = __afr_txn_write_fop;
e7a346
+        local->transaction.done   = __afr_txn_write_done;
e7a346
+        local->transaction.unwind = afr_fsync_unwind;
e7a346
+
e7a346
+        local->transaction.main_frame = frame;
e7a346
+
e7a346
+        ret = afr_transaction (transaction_frame, this, AFR_DATA_TRANSACTION);
e7a346
+        if (ret < 0) {
e7a346
+                op_errno = -ret;
e7a346
+                goto out;
e7a346
+        }
e7a346
+
e7a346
+	return 0;
e7a346
+out:
e7a346
+	if (transaction_frame)
e7a346
+		AFR_STACK_DESTROY (transaction_frame);
e7a346
+
e7a346
+	AFR_STACK_UNWIND (fsync, frame, -1, op_errno, NULL, NULL, NULL);
e7a346
+
e7a346
+        return 0;
e7a346
+}
e7a346
diff --git a/xlators/cluster/afr/src/afr-inode-write.h b/xlators/cluster/afr/src/afr-inode-write.h
e7a346
index e174cc2..1e8bb5c 100644
e7a346
--- a/xlators/cluster/afr/src/afr-inode-write.h
e7a346
+++ b/xlators/cluster/afr/src/afr-inode-write.h
e7a346
@@ -87,4 +87,8 @@ afr_xattrop (call_frame_t *frame, xlator_t *this, loc_t *loc,
e7a346
 int32_t
e7a346
 afr_fxattrop (call_frame_t *frame, xlator_t *this, fd_t *fd,
e7a346
               gf_xattrop_flags_t optype, dict_t *xattr, dict_t *xdata);
e7a346
+
e7a346
+int
e7a346
+afr_fsync (call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t datasync,
e7a346
+	   dict_t *xdata);
e7a346
 #endif /* __INODE_WRITE_H__ */
e7a346
diff --git a/xlators/cluster/afr/src/afr.c b/xlators/cluster/afr/src/afr.c
e7a346
index 9493fbb..aa48e76 100644
e7a346
--- a/xlators/cluster/afr/src/afr.c
e7a346
+++ b/xlators/cluster/afr/src/afr.c
e7a346
@@ -664,7 +664,6 @@ struct xlator_fops fops = {
e7a346
         .lk          = afr_lk,
e7a346
         .flush       = afr_flush,
e7a346
         .statfs      = afr_statfs,
e7a346
-        .fsync       = afr_fsync,
e7a346
         .fsyncdir    = afr_fsyncdir,
e7a346
         .inodelk     = afr_inodelk,
e7a346
         .finodelk    = afr_finodelk,
e7a346
@@ -696,6 +695,7 @@ struct xlator_fops fops = {
e7a346
         .zerofill    = afr_zerofill,
e7a346
         .xattrop     = afr_xattrop,
e7a346
         .fxattrop    = afr_fxattrop,
e7a346
+        .fsync       = afr_fsync,
e7a346
 
e7a346
         /*inode open*/
e7a346
         .opendir     = afr_opendir,
e7a346
diff --git a/xlators/cluster/afr/src/afr.h b/xlators/cluster/afr/src/afr.h
e7a346
index b6f5388..11278fb 100644
e7a346
--- a/xlators/cluster/afr/src/afr.h
e7a346
+++ b/xlators/cluster/afr/src/afr.h
e7a346
@@ -725,6 +725,10 @@ typedef struct _afr_local {
e7a346
                         gf_seek_what_t what;
e7a346
                 } seek;
e7a346
 
e7a346
+                struct {
e7a346
+                        int32_t datasync;
e7a346
+                } fsync;
e7a346
+
e7a346
         } cont;
e7a346
 
e7a346
         struct {
e7a346
-- 
e7a346
1.8.3.1
e7a346