7c2869
From 6191553d871dc004185a504682176cd7afb9deef Mon Sep 17 00:00:00 2001
7c2869
From: karthik-us <ksubrahm@redhat.com>
7c2869
Date: Fri, 23 Feb 2018 15:12:19 +0530
7c2869
Subject: [PATCH 669/675] cluster/afr: Make afr_fsync a transaction
7c2869
7c2869
Upstream patch: https://review.gluster.org/#/c/19621/
7c2869
7c2869
BUG: 1583733
7c2869
Change-Id: I5a336e8d33dd9612c0c81f1b1020aa10ebc09220
7c2869
Signed-off-by: karthik-us <ksubrahm@redhat.com>
7c2869
Reviewed-on: https://code.engineering.redhat.com/gerrit/140574
7c2869
Tested-by: RHGS Build Bot <nigelb@redhat.com>
7c2869
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
7c2869
---
7c2869
 xlators/cluster/afr/src/afr-common.c      | 165 ------------------------------
7c2869
 xlators/cluster/afr/src/afr-inode-write.c | 106 +++++++++++++++++++
7c2869
 xlators/cluster/afr/src/afr-inode-write.h |   4 +
7c2869
 xlators/cluster/afr/src/afr.c             |   2 +-
7c2869
 xlators/cluster/afr/src/afr.h             |   4 +
7c2869
 5 files changed, 115 insertions(+), 166 deletions(-)
7c2869
7c2869
diff --git a/xlators/cluster/afr/src/afr-common.c b/xlators/cluster/afr/src/afr-common.c
7c2869
index d96a819..cfd3d60 100644
7c2869
--- a/xlators/cluster/afr/src/afr-common.c
7c2869
+++ b/xlators/cluster/afr/src/afr-common.c
7c2869
@@ -3218,171 +3218,6 @@ out:
7c2869
         return 0;
7c2869
 }
7c2869
 
7c2869
-/* }}} */
7c2869
-
7c2869
-
7c2869
-/* {{{ fsync */
7c2869
-
7c2869
-int
7c2869
-afr_fsync_unwind_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
7c2869
-                      int32_t op_ret, int32_t op_errno, struct iatt *prebuf,
7c2869
-                      struct iatt *postbuf, dict_t *xdata)
7c2869
-{
7c2869
-        AFR_STACK_UNWIND (fsync, frame, op_ret, op_errno, prebuf, postbuf,
7c2869
-                          xdata);
7c2869
-        return 0;
7c2869
-}
7c2869
-
7c2869
-int
7c2869
-afr_fsync_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
7c2869
-               int32_t op_ret, int32_t op_errno, struct iatt *prebuf,
7c2869
-               struct iatt *postbuf, dict_t *xdata)
7c2869
-{
7c2869
-        afr_local_t *local = NULL;
7c2869
-        afr_private_t *priv = NULL;
7c2869
-        int i = 0;
7c2869
-        int call_count = -1;
7c2869
-        int child_index = (long) cookie;
7c2869
-	int read_subvol = 0;
7c2869
-	call_stub_t *stub = NULL;
7c2869
-
7c2869
-        local = frame->local;
7c2869
-        priv = this->private;
7c2869
-
7c2869
-        LOCK (&frame->lock);
7c2869
-        {
7c2869
-                local->replies[child_index].valid = 1;
7c2869
-                local->replies[child_index].op_ret = op_ret;
7c2869
-                local->replies[child_index].op_errno = op_errno;
7c2869
-                if (op_ret == 0) {
7c2869
-                        if (prebuf)
7c2869
-                                local->replies[child_index].prestat = *prebuf;
7c2869
-                        if (postbuf)
7c2869
-                                local->replies[child_index].poststat = *postbuf;
7c2869
-                        if (xdata)
7c2869
-                                local->replies[child_index].xdata =
7c2869
-                                        dict_ref (xdata);
7c2869
-                }
7c2869
-        }
7c2869
-        UNLOCK (&frame->lock);
7c2869
-
7c2869
-        call_count = afr_frame_return (frame);
7c2869
-
7c2869
-        if (call_count == 0) {
7c2869
-                local->op_ret = -1;
7c2869
-                local->op_errno = afr_final_errno (local, priv);
7c2869
-	        read_subvol = afr_data_subvol_get (local->inode, this, NULL,
7c2869
-                                                   local->readable, NULL, NULL);
7c2869
-                /* Pick a reply that is valid and readable, with a preference
7c2869
-                 * given to read_subvol. */
7c2869
-                for (i = 0; i < priv->child_count; i++) {
7c2869
-                        if (!local->replies[i].valid)
7c2869
-                                continue;
7c2869
-                        if (local->replies[i].op_ret != 0)
7c2869
-                                continue;
7c2869
-                        if (!local->readable[i])
7c2869
-                                continue;
7c2869
-                        local->op_ret = local->replies[i].op_ret;
7c2869
-                        local->op_errno = local->replies[i].op_errno;
7c2869
-                        local->cont.inode_wfop.prebuf =
7c2869
-                                local->replies[i].prestat;
7c2869
-                        local->cont.inode_wfop.postbuf =
7c2869
-                                local->replies[i].poststat;
7c2869
-                        if (local->replies[i].xdata) {
7c2869
-                                if (local->xdata_rsp)
7c2869
-                                        dict_unref (local->xdata_rsp);
7c2869
-                                local->xdata_rsp =
7c2869
-                                        dict_ref (local->replies[i].xdata);
7c2869
-                        }
7c2869
-                        if (i == read_subvol)
7c2869
-                                break;
7c2869
-                }
7c2869
-
7c2869
-		/* Make a stub out of the frame, and register it
7c2869
-		   with the waking up post-op. When the call-stub resumes,
7c2869
-		   we are guaranteed that there was no post-op pending
7c2869
-		   (i.e changelogs were unset in the server). This is an
7c2869
-		   essential "guarantee", that fsync() returns only after
7c2869
-		   completely finishing EVERYTHING, including the delayed
7c2869
-		   post-op. This guarantee is expected by FUSE graph switching
7c2869
-		   for example.
7c2869
-		*/
7c2869
-		stub = fop_fsync_cbk_stub (frame, afr_fsync_unwind_cbk,
7c2869
-                                           local->op_ret, local->op_errno,
7c2869
-                                           &local->cont.inode_wfop.prebuf,
7c2869
-                                           &local->cont.inode_wfop.postbuf,
7c2869
-                                           local->xdata_rsp);
7c2869
-		if (!stub) {
7c2869
-			AFR_STACK_UNWIND (fsync, frame, -1, ENOMEM, 0, 0, 0);
7c2869
-			return 0;
7c2869
-		}
7c2869
-
7c2869
-		/* If no new unstable writes happened between the
7c2869
-		   time we cleared the unstable write witness flag in afr_fsync
7c2869
-		   and now, calling afr_delayed_changelog_wake_up() should
7c2869
-		   wake up and skip over the fsync phase and go straight to
7c2869
-		   afr_changelog_post_op_now()
7c2869
-		*/
7c2869
-		afr_delayed_changelog_wake_resume (this, local->fd, stub);
7c2869
-        }
7c2869
-
7c2869
-        return 0;
7c2869
-}
7c2869
-
7c2869
-
7c2869
-int
7c2869
-afr_fsync (call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t datasync,
7c2869
-	   dict_t *xdata)
7c2869
-{
7c2869
-	afr_private_t *priv = NULL;
7c2869
-        afr_local_t *local = NULL;
7c2869
-        int i = 0;
7c2869
-        int32_t call_count = 0;
7c2869
-        int32_t op_errno = ENOMEM;
7c2869
-
7c2869
-	priv = this->private;
7c2869
-
7c2869
-	local = AFR_FRAME_INIT (frame, op_errno);
7c2869
-	if (!local)
7c2869
-		goto out;
7c2869
-
7c2869
-        call_count = local->call_count;
7c2869
-	if (!call_count) {
7c2869
-		op_errno = ENOTCONN;
7c2869
-		goto out;
7c2869
-	}
7c2869
-
7c2869
-        local->fd = fd_ref (fd);
7c2869
-
7c2869
-	if (afr_fd_has_witnessed_unstable_write (this, fd)) {
7c2869
-		/* don't care. we only wanted to CLEAR the bit */
7c2869
-	}
7c2869
-
7c2869
-	local->inode = inode_ref (fd->inode);
7c2869
-
7c2869
-        for (i = 0; i < priv->child_count; i++) {
7c2869
-                if (local->child_up[i]) {
7c2869
-                        STACK_WIND_COOKIE (frame, afr_fsync_cbk,
7c2869
-                                           (void *) (long) i,
7c2869
-                                           priv->children[i],
7c2869
-                                           priv->children[i]->fops->fsync,
7c2869
-                                           fd, datasync, xdata);
7c2869
-                        if (!--call_count)
7c2869
-                                break;
7c2869
-                }
7c2869
-        }
7c2869
-
7c2869
-	return 0;
7c2869
-out:
7c2869
-	AFR_STACK_UNWIND (fsync, frame, -1, op_errno, NULL, NULL, NULL);
7c2869
-
7c2869
-        return 0;
7c2869
-}
7c2869
-
7c2869
-/* }}} */
7c2869
-
7c2869
-/* {{{ fsync */
7c2869
-
7c2869
 int
7c2869
 afr_fsyncdir_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
7c2869
 		  int32_t op_ret, int32_t op_errno, dict_t *xdata)
7c2869
diff --git a/xlators/cluster/afr/src/afr-inode-write.c b/xlators/cluster/afr/src/afr-inode-write.c
7c2869
index 04bbf21..c740599 100644
7c2869
--- a/xlators/cluster/afr/src/afr-inode-write.c
7c2869
+++ b/xlators/cluster/afr/src/afr-inode-write.c
7c2869
@@ -2511,3 +2511,109 @@ out:
7c2869
 	AFR_STACK_UNWIND (fxattrop, frame, -1, op_errno, NULL, NULL);
7c2869
         return 0;
7c2869
 }
7c2869
+
7c2869
+
7c2869
+int
7c2869
+afr_fsync_unwind (call_frame_t *frame, xlator_t *this)
7c2869
+{
7c2869
+        afr_local_t *local = NULL;
7c2869
+        call_frame_t   *main_frame = NULL;
7c2869
+
7c2869
+        local = frame->local;
7c2869
+
7c2869
+        main_frame = afr_transaction_detach_fop_frame (frame);
7c2869
+        if (!main_frame)
7c2869
+                return 0;
7c2869
+
7c2869
+        AFR_STACK_UNWIND (fsync, main_frame, local->op_ret, local->op_errno,
7c2869
+                          &local->cont.inode_wfop.prebuf,
7c2869
+                          &local->cont.inode_wfop.postbuf, local->xdata_rsp);
7c2869
+
7c2869
+        return 0;
7c2869
+}
7c2869
+
7c2869
+
7c2869
+int
7c2869
+afr_fsync_wind_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
7c2869
+                    int32_t op_ret, int32_t op_errno, struct iatt *prebuf,
7c2869
+                    struct iatt *postbuf, dict_t *xdata)
7c2869
+{
7c2869
+        return __afr_inode_write_cbk (frame, cookie, this, op_ret, op_errno,
7c2869
+                                      prebuf, postbuf, NULL, xdata);
7c2869
+}
7c2869
+
7c2869
+
7c2869
+int
7c2869
+afr_fsync_wind (call_frame_t *frame, xlator_t *this, int subvol)
7c2869
+{
7c2869
+        afr_local_t *local = NULL;
7c2869
+        afr_private_t *priv = NULL;
7c2869
+
7c2869
+        local = frame->local;
7c2869
+        priv = this->private;
7c2869
+
7c2869
+        STACK_WIND_COOKIE (frame, afr_fsync_wind_cbk, (void *)(long) subvol,
7c2869
+                           priv->children[subvol],
7c2869
+                           priv->children[subvol]->fops->fsync,
7c2869
+                           local->fd, local->cont.fsync.datasync,
7c2869
+                           local->xdata_req);
7c2869
+        return 0;
7c2869
+}
7c2869
+
7c2869
+int
7c2869
+afr_fsync (call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t datasync,
7c2869
+	   dict_t *xdata)
7c2869
+{
7c2869
+        afr_local_t *local = NULL;
7c2869
+        call_frame_t *transaction_frame = NULL;
7c2869
+        int ret = -1;
7c2869
+        int32_t op_errno = ENOMEM;
7c2869
+
7c2869
+        transaction_frame = copy_frame (frame);
7c2869
+        if (!transaction_frame)
7c2869
+                goto out;
7c2869
+
7c2869
+	local = AFR_FRAME_INIT (transaction_frame, op_errno);
7c2869
+	if (!local)
7c2869
+		goto out;
7c2869
+
7c2869
+        if (xdata)
7c2869
+                local->xdata_req = dict_copy_with_ref (xdata, NULL);
7c2869
+        else
7c2869
+                local->xdata_req = dict_new ();
7c2869
+
7c2869
+        if (!local->xdata_req)
7c2869
+                goto out;
7c2869
+
7c2869
+        local->fd = fd_ref (fd);
7c2869
+        local->inode = inode_ref (fd->inode);
7c2869
+
7c2869
+        local->op = GF_FOP_FSYNC;
7c2869
+        local->cont.fsync.datasync = datasync;
7c2869
+
7c2869
+	if (afr_fd_has_witnessed_unstable_write (this, fd)) {
7c2869
+		/* don't care. we only wanted to CLEAR the bit */
7c2869
+	}
7c2869
+
7c2869
+        local->transaction.wind   = afr_fsync_wind;
7c2869
+        local->transaction.fop    = __afr_txn_write_fop;
7c2869
+        local->transaction.done   = __afr_txn_write_done;
7c2869
+        local->transaction.unwind = afr_fsync_unwind;
7c2869
+
7c2869
+        local->transaction.main_frame = frame;
7c2869
+
7c2869
+        ret = afr_transaction (transaction_frame, this, AFR_DATA_TRANSACTION);
7c2869
+        if (ret < 0) {
7c2869
+                op_errno = -ret;
7c2869
+                goto out;
7c2869
+        }
7c2869
+
7c2869
+	return 0;
7c2869
+out:
7c2869
+	if (transaction_frame)
7c2869
+		AFR_STACK_DESTROY (transaction_frame);
7c2869
+
7c2869
+	AFR_STACK_UNWIND (fsync, frame, -1, op_errno, NULL, NULL, NULL);
7c2869
+
7c2869
+        return 0;
7c2869
+}
7c2869
diff --git a/xlators/cluster/afr/src/afr-inode-write.h b/xlators/cluster/afr/src/afr-inode-write.h
7c2869
index e174cc2..1e8bb5c 100644
7c2869
--- a/xlators/cluster/afr/src/afr-inode-write.h
7c2869
+++ b/xlators/cluster/afr/src/afr-inode-write.h
7c2869
@@ -87,4 +87,8 @@ afr_xattrop (call_frame_t *frame, xlator_t *this, loc_t *loc,
7c2869
 int32_t
7c2869
 afr_fxattrop (call_frame_t *frame, xlator_t *this, fd_t *fd,
7c2869
               gf_xattrop_flags_t optype, dict_t *xattr, dict_t *xdata);
7c2869
+
7c2869
+int
7c2869
+afr_fsync (call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t datasync,
7c2869
+	   dict_t *xdata);
7c2869
 #endif /* __INODE_WRITE_H__ */
7c2869
diff --git a/xlators/cluster/afr/src/afr.c b/xlators/cluster/afr/src/afr.c
7c2869
index 375bd1f..fc56486 100644
7c2869
--- a/xlators/cluster/afr/src/afr.c
7c2869
+++ b/xlators/cluster/afr/src/afr.c
7c2869
@@ -562,7 +562,6 @@ struct xlator_fops fops = {
7c2869
         .lk          = afr_lk,
7c2869
         .flush       = afr_flush,
7c2869
         .statfs      = afr_statfs,
7c2869
-        .fsync       = afr_fsync,
7c2869
         .fsyncdir    = afr_fsyncdir,
7c2869
         .xattrop     = afr_xattrop,
7c2869
         .fxattrop    = afr_fxattrop,
7c2869
@@ -594,6 +593,7 @@ struct xlator_fops fops = {
7c2869
         .fallocate   = afr_fallocate,
7c2869
         .discard     = afr_discard,
7c2869
         .zerofill    = afr_zerofill,
7c2869
+        .fsync       = afr_fsync,
7c2869
 
7c2869
         /* dir read */
7c2869
         .opendir     = afr_opendir,
7c2869
diff --git a/xlators/cluster/afr/src/afr.h b/xlators/cluster/afr/src/afr.h
7c2869
index f6a1a6a..304efa1 100644
7c2869
--- a/xlators/cluster/afr/src/afr.h
7c2869
+++ b/xlators/cluster/afr/src/afr.h
7c2869
@@ -702,6 +702,10 @@ typedef struct _afr_local {
7c2869
                         gf_seek_what_t what;
7c2869
                 } seek;
7c2869
 
7c2869
+                struct {
7c2869
+                        int32_t datasync;
7c2869
+                } fsync;
7c2869
+
7c2869
         } cont;
7c2869
 
7c2869
         struct {
7c2869
-- 
7c2869
1.8.3.1
7c2869