233933
From 90254e4ae9455fa0a126f83700978a9314eb79ea Mon Sep 17 00:00:00 2001
233933
From: Anuradha Talur <atalur@commvault.com>
233933
Date: Thu, 29 Nov 2018 12:54:21 -0800
233933
Subject: [PATCH 153/169] features/cloudsync : Added some new functions
233933
233933
This patch contains the following changes:
233933
1) Store ID info will now be stored in the inode ctx
233933
2) Added new readv type where read is made directly
233933
   from the remote store. This choice is made by
233933
   volume set operation.
233933
3) cs_forget() was missing. Added it.
233933
233933
backport of:https://review.gluster.org/#/c/glusterfs/+/21757/
233933
233933
> Change-Id: Ie3232b3d7ffb5313a03f011b0553b19793eedfa2
233933
> fixes: bz#1642168
233933
> Signed-off-by: Anuradha Talur <atalur@commvault.com>
233933
233933
Change-Id: I089e5a8c93049cf6bfabf011673796e38e78d7ee
233933
Signed-off-by: Susant Palai <spalai@redhat.com>
233933
Reviewed-on: https://code.engineering.redhat.com/gerrit/172192
233933
Tested-by: RHGS Build Bot <nigelb@redhat.com>
233933
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
233933
---
233933
 xlators/features/cloudsync/src/cloudsync-common.c  |  16 +
233933
 xlators/features/cloudsync/src/cloudsync-common.h  |  35 ++
233933
 xlators/features/cloudsync/src/cloudsync-fops-c.py |  12 +-
233933
 .../features/cloudsync/src/cloudsync-mem-types.h   |   1 +
233933
 xlators/features/cloudsync/src/cloudsync.c         | 600 ++++++++++++++++++---
233933
 xlators/features/cloudsync/src/cloudsync.h         |  20 +
233933
 xlators/mgmt/glusterd/src/glusterd-volume-set.c    |   7 +-
233933
 7 files changed, 597 insertions(+), 94 deletions(-)
233933
233933
diff --git a/xlators/features/cloudsync/src/cloudsync-common.c b/xlators/features/cloudsync/src/cloudsync-common.c
233933
index aee1f06..445a31b 100644
233933
--- a/xlators/features/cloudsync/src/cloudsync-common.c
233933
+++ b/xlators/features/cloudsync/src/cloudsync-common.c
233933
@@ -11,6 +11,20 @@
233933
 #include "cloudsync-common.h"
233933
 
233933
 void
233933
+cs_xattrinfo_wipe(cs_local_t *local)
233933
+{
233933
+    if (local->xattrinfo.lxattr) {
233933
+        if (local->xattrinfo.lxattr->file_path)
233933
+            GF_FREE(local->xattrinfo.lxattr->file_path);
233933
+
233933
+        if (local->xattrinfo.lxattr->volname)
233933
+            GF_FREE(local->xattrinfo.lxattr->volname);
233933
+
233933
+        GF_FREE(local->xattrinfo.lxattr);
233933
+    }
233933
+}
233933
+
233933
+void
233933
 cs_local_wipe(xlator_t *this, cs_local_t *local)
233933
 {
233933
     if (!local)
233933
@@ -40,5 +54,7 @@ cs_local_wipe(xlator_t *this, cs_local_t *local)
233933
     if (local->remotepath)
233933
         GF_FREE(local->remotepath);
233933
 
233933
+    cs_xattrinfo_wipe(local);
233933
+
233933
     mem_put(local);
233933
 }
233933
diff --git a/xlators/features/cloudsync/src/cloudsync-common.h b/xlators/features/cloudsync/src/cloudsync-common.h
233933
index 7b3520c..11d2334 100644
233933
--- a/xlators/features/cloudsync/src/cloudsync-common.h
233933
+++ b/xlators/features/cloudsync/src/cloudsync-common.h
233933
@@ -14,9 +14,23 @@
233933
 #include <glusterfs/call-stub.h>
233933
 #include <glusterfs/xlator.h>
233933
 #include <glusterfs/syncop.h>
233933
+#include <glusterfs/compat-errno.h>
233933
 #include "cloudsync-mem-types.h"
233933
 #include "cloudsync-messages.h"
233933
 
233933
+typedef struct cs_loc_xattr {
233933
+    char *file_path;
233933
+    uuid_t uuid;
233933
+    uuid_t gfid;
233933
+    char *volname;
233933
+} cs_loc_xattr_t;
233933
+
233933
+typedef struct cs_size_xattr {
233933
+    uint64_t size;
233933
+    uint64_t blksize;
233933
+    uint64_t blocks;
233933
+} cs_size_xattr_t;
233933
+
233933
 typedef struct cs_local {
233933
     loc_t loc;
233933
     fd_t *fd;
233933
@@ -34,10 +48,25 @@ typedef struct cs_local {
233933
     int call_cnt;
233933
     inode_t *inode;
233933
     char *remotepath;
233933
+
233933
+    struct {
233933
+        /* offset, flags and size are the information needed
233933
+         * by read fop for remote read operation. These will be
233933
+         * populated in cloudsync read fop, before being passed
233933
+         * on to the plugin performing remote read.
233933
+         */
233933
+        off_t offset;
233933
+        uint32_t flags;
233933
+        size_t size;
233933
+        cs_loc_xattr_t *lxattr;
233933
+    } xattrinfo;
233933
+
233933
 } cs_local_t;
233933
 
233933
 typedef int (*fop_download_t)(call_frame_t *frame, void *config);
233933
 
233933
+typedef int (*fop_remote_read_t)(call_frame_t *, void *);
233933
+
233933
 typedef void *(*store_init)(xlator_t *this);
233933
 
233933
 typedef int (*store_reconfigure)(xlator_t *this, dict_t *options);
233933
@@ -48,6 +77,7 @@ struct cs_remote_stores {
233933
     char *name;                    /* store name */
233933
     void *config;                  /* store related information */
233933
     fop_download_t dlfop;          /* store specific download function */
233933
+    fop_remote_read_t rdfop;       /* store specific read function */
233933
     store_init init;               /* store init to initialize store config */
233933
     store_reconfigure reconfigure; /* reconfigure store config */
233933
     store_fini fini;
233933
@@ -59,11 +89,15 @@ typedef struct cs_private {
233933
     struct cs_remote_stores *stores;
233933
     gf_boolean_t abortdl;
233933
     pthread_spinlock_t lock;
233933
+    gf_boolean_t remote_read;
233933
 } cs_private_t;
233933
 
233933
 void
233933
 cs_local_wipe(xlator_t *this, cs_local_t *local);
233933
 
233933
+void
233933
+cs_xattrinfo_wipe(cs_local_t *local);
233933
+
233933
 #define CS_STACK_UNWIND(fop, frame, params...)                                 \
233933
     do {                                                                       \
233933
         cs_local_t *__local = NULL;                                            \
233933
@@ -90,6 +124,7 @@ cs_local_wipe(xlator_t *this, cs_local_t *local);
233933
 
233933
 typedef struct store_methods {
233933
     int (*fop_download)(call_frame_t *frame, void *config);
233933
+    int (*fop_remote_read)(call_frame_t *, void *);
233933
     /* return type should be the store config */
233933
     void *(*fop_init)(xlator_t *this);
233933
     int (*fop_reconfigure)(xlator_t *this, dict_t *options);
233933
diff --git a/xlators/features/cloudsync/src/cloudsync-fops-c.py b/xlators/features/cloudsync/src/cloudsync-fops-c.py
233933
index 3122bd3..a7a2201 100755
233933
--- a/xlators/features/cloudsync/src/cloudsync-fops-c.py
233933
+++ b/xlators/features/cloudsync/src/cloudsync-fops-c.py
233933
@@ -137,15 +137,15 @@ cs_@NAME@_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
233933
                         } else {
233933
                                 __cs_inode_ctx_update (this, fd->inode, val);
233933
                                 gf_msg (this->name, GF_LOG_INFO, 0, 0,
233933
-                                        " state = %ld", val);
233933
+                                        " state = %" PRIu64, val);
233933
 
233933
                                 if (local->call_cnt == 1 &&
233933
                                     (val == GF_CS_REMOTE ||
233933
                                      val == GF_CS_DOWNLOADING))  {
233933
                                         gf_msg (this->name, GF_LOG_INFO, 0,
233933
                                                 0, " will repair and download "
233933
-                                                "the file, current state : %ld",
233933
-                                                val);
233933
+                                                "the file, current state : %"
233933
+                                                PRIu64, val);
233933
                                         goto repair;
233933
                                 } else {
233933
                                         gf_msg (this->name, GF_LOG_ERROR, 0, 0,
233933
@@ -274,7 +274,7 @@ fd_ops = ['readv', 'writev', 'flush', 'fsync', 'fsyncdir', 'ftruncate',
233933
 # These are the current actual lists used to generate the code
233933
 
233933
 # The following list contains fops which are fd based that modifies data
233933
-fd_data_modify_op_fop_template = ['readv', 'writev', 'flush', 'fsync',
233933
+fd_data_modify_op_fop_template = ['writev', 'flush', 'fsync',
233933
                                   'ftruncate', 'rchecksum', 'fallocate',
233933
                                   'discard', 'zerofill', 'seek']
233933
 
233933
@@ -284,8 +284,8 @@ loc_stat_op_fop_template = ['lookup', 'stat', 'discover', 'access', 'setattr',
233933
                             'getattr']
233933
 
233933
 # These fops need a separate implementation
233933
-special_fops = ['readdirp', 'statfs', 'setxattr', 'unlink', 'getxattr',
233933
-                'truncate', 'fstat']
233933
+special_fops = ['statfs', 'setxattr', 'unlink', 'getxattr',
233933
+                'truncate', 'fstat', 'readv']
233933
 
233933
 def gen_defaults():
233933
     for name in ops:
233933
diff --git a/xlators/features/cloudsync/src/cloudsync-mem-types.h b/xlators/features/cloudsync/src/cloudsync-mem-types.h
233933
index 9e6837a..2203464 100644
233933
--- a/xlators/features/cloudsync/src/cloudsync-mem-types.h
233933
+++ b/xlators/features/cloudsync/src/cloudsync-mem-types.h
233933
@@ -16,6 +16,7 @@ enum cs_mem_types_ {
233933
     gf_cs_mt_cs_private_t = gf_common_mt_end + 1,
233933
     gf_cs_mt_cs_remote_stores_t,
233933
     gf_cs_mt_cs_inode_ctx_t,
233933
+    gf_cs_mt_cs_lxattr_t,
233933
     gf_cs_mt_end
233933
 };
233933
 #endif /* __CLOUDSYNC_MEM_TYPES_H__ */
233933
diff --git a/xlators/features/cloudsync/src/cloudsync.c b/xlators/features/cloudsync/src/cloudsync.c
233933
index fbdcdf7..2240fc3 100644
233933
--- a/xlators/features/cloudsync/src/cloudsync.c
233933
+++ b/xlators/features/cloudsync/src/cloudsync.c
233933
@@ -16,6 +16,7 @@
233933
 #include <glusterfs/call-stub.h>
233933
 #include "cloudsync-autogen-fops.h"
233933
 
233933
+#include <string.h>
233933
 #include <dlfcn.h>
233933
 
233933
 void
233933
@@ -72,6 +73,8 @@ cs_init(xlator_t *this)
233933
 
233933
     this->private = priv;
233933
 
233933
+    GF_OPTION_INIT("cloudsync-remote-read", priv->remote_read, bool, out);
233933
+
233933
     /* temp workaround. Should be configurable through glusterd*/
233933
     per_vol = _gf_true;
233933
 
233933
@@ -135,6 +138,18 @@ cs_init(xlator_t *this)
233933
 
233933
         (void)dlerror();
233933
 
233933
+        if (priv->remote_read) {
233933
+            priv->stores->rdfop = store_methods->fop_remote_read;
233933
+            if (!priv->stores->rdfop) {
233933
+                gf_msg(this->name, GF_LOG_ERROR, 0, 0,
233933
+                       "failed to get"
233933
+                       " read fop %s",
233933
+                       dlerror());
233933
+                ret = -1;
233933
+                goto out;
233933
+            }
233933
+        }
233933
+
233933
         priv->stores->dlfop = store_methods->fop_download;
233933
         if (!priv->stores->dlfop) {
233933
             gf_msg(this->name, GF_LOG_ERROR, 0, 0,
233933
@@ -196,6 +211,22 @@ out:
233933
     return ret;
233933
 }
233933
 
233933
+int
233933
+cs_forget(xlator_t *this, inode_t *inode)
233933
+{
233933
+    uint64_t ctx_int = 0;
233933
+    cs_inode_ctx_t *ctx = NULL;
233933
+
233933
+    inode_ctx_del(inode, this, &ctx_int);
233933
+    if (!ctx_int)
233933
+        return 0;
233933
+
233933
+    ctx = (cs_inode_ctx_t *)(uintptr_t)ctx_int;
233933
+
233933
+    GF_FREE(ctx);
233933
+    return 0;
233933
+}
233933
+
233933
 void
233933
 cs_fini(xlator_t *this)
233933
 {
233933
@@ -217,6 +248,9 @@ cs_reconfigure(xlator_t *this, dict_t *options)
233933
         goto out;
233933
     }
233933
 
233933
+    GF_OPTION_RECONF("cloudsync-remote-read", priv->remote_read, options, bool,
233933
+                     out);
233933
+
233933
     /* needed only for per volume configuration*/
233933
     ret = priv->stores->reconfigure(this, options);
233933
 
233933
@@ -242,59 +276,6 @@ out:
233933
 }
233933
 
233933
 int32_t
233933
-cs_readdirp_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
233933
-                int32_t op_ret, int32_t op_errno, gf_dirent_t *entries,
233933
-                dict_t *xdata)
233933
-{
233933
-    gf_dirent_t *tmp = NULL;
233933
-    char *sxattr = NULL;
233933
-    uint64_t ia_size = 0;
233933
-    int ret = 0;
233933
-
233933
-    list_for_each_entry(tmp, &entries->list, list)
233933
-    {
233933
-        ret = dict_get_str(tmp->dict, GF_CS_OBJECT_SIZE, &sxattr);
233933
-        if (ret) {
233933
-            gf_msg_trace(this->name, 0, "size xattr found");
233933
-            continue;
233933
-        }
233933
-
233933
-        ia_size = atoll(sxattr);
233933
-        tmp->d_stat.ia_size = ia_size;
233933
-    }
233933
-
233933
-    STACK_UNWIND_STRICT(readdirp, frame, op_ret, op_errno, entries, xdata);
233933
-    return 0;
233933
-}
233933
-
233933
-int32_t
233933
-cs_readdirp(call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size,
233933
-            off_t off, dict_t *xdata)
233933
-{
233933
-    int ret = 0;
233933
-    int op_errno = ENOMEM;
233933
-
233933
-    if (!xdata) {
233933
-        xdata = dict_new();
233933
-        if (!xdata) {
233933
-            goto err;
233933
-        }
233933
-    }
233933
-
233933
-    ret = dict_set_int32(xdata, GF_CS_OBJECT_SIZE, 1);
233933
-    if (ret) {
233933
-        goto err;
233933
-    }
233933
-
233933
-    STACK_WIND(frame, cs_readdirp_cbk, FIRST_CHILD(this),
233933
-               FIRST_CHILD(this)->fops->readdirp, fd, size, off, xdata);
233933
-    return 0;
233933
-err:
233933
-    STACK_UNWIND_STRICT(readdirp, frame, -1, op_errno, NULL, NULL);
233933
-    return 0;
233933
-}
233933
-
233933
-int32_t
233933
 cs_truncate_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
233933
                 int32_t op_ret, int32_t op_errno, struct iatt *prebuf,
233933
                 struct iatt *postbuf, dict_t *xdata)
233933
@@ -305,7 +286,6 @@ cs_truncate_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
233933
 
233933
     local = frame->local;
233933
 
233933
-    /* Do we need lock here? */
233933
     local->call_cnt++;
233933
 
233933
     if (op_ret == -1) {
233933
@@ -320,13 +300,13 @@ cs_truncate_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
233933
                 goto unwind;
233933
             } else {
233933
                 __cs_inode_ctx_update(this, local->loc.inode, val);
233933
-                gf_msg(this->name, GF_LOG_INFO, 0, 0, " state = %ld", val);
233933
+                gf_msg(this->name, GF_LOG_INFO, 0, 0, " state = %" PRIu64, val);
233933
 
233933
                 if (local->call_cnt == 1 &&
233933
                     (val == GF_CS_REMOTE || val == GF_CS_DOWNLOADING)) {
233933
                     gf_msg(this->name, GF_LOG_WARNING, 0, 0,
233933
                            "will repair and download "
233933
-                           "the file, current state : %ld",
233933
+                           "the file, current state : %" PRIu64,
233933
                            val);
233933
                     goto repair;
233933
                 } else {
233933
@@ -665,7 +645,7 @@ cs_fstat_cbk(call_frame_t *frame, void *cookie, xlator_t *this, int32_t op_ret,
233933
     if (op_ret == 0) {
233933
         ret = dict_get_uint64(xdata, GF_CS_OBJECT_STATUS, &val;;
233933
         if (!ret) {
233933
-            gf_msg_debug(this->name, 0, "state %ld", val);
233933
+            gf_msg_debug(this->name, 0, "state %" PRIu64, val);
233933
             ret = __cs_inode_ctx_update(this, fd->inode, val);
233933
             if (ret) {
233933
                 gf_msg(this->name, GF_LOG_ERROR, 0, 0, "ctx update failed");
233933
@@ -831,7 +811,7 @@ out:
233933
     return 0;
233933
 }
233933
 
233933
-void *
233933
+int
233933
 cs_download_task(void *arg)
233933
 {
233933
     call_frame_t *frame = NULL;
233933
@@ -842,7 +822,6 @@ cs_download_task(void *arg)
233933
     fd_t *fd = NULL;
233933
     cs_local_t *local = NULL;
233933
     dict_t *dict = NULL;
233933
-    int *retval = NULL;
233933
 
233933
     frame = (call_frame_t *)arg;
233933
 
233933
@@ -850,13 +829,6 @@ cs_download_task(void *arg)
233933
 
233933
     priv = this->private;
233933
 
233933
-    retval = GF_CALLOC(1, sizeof(int), gf_common_mt_int);
233933
-    if (!retval) {
233933
-        gf_msg(this->name, GF_LOG_ERROR, 0, 0, "insufficient memory");
233933
-        ret = -1;
233933
-        goto out;
233933
-    }
233933
-
233933
     if (!priv->stores) {
233933
         gf_msg(this->name, GF_LOG_ERROR, 0, 0,
233933
                "No remote store "
233933
@@ -972,20 +944,13 @@ out:
233933
         local->dlfd = NULL;
233933
     }
233933
 
233933
-    if (retval) {
233933
-        *retval = ret;
233933
-        pthread_exit(retval);
233933
-    } else {
233933
-        pthread_exit(&ret;;
233933
-    }
233933
+    return ret;
233933
 }
233933
 
233933
 int
233933
 cs_download(call_frame_t *frame)
233933
 {
233933
-    int *retval = NULL;
233933
     int ret = 0;
233933
-    pthread_t dthread;
233933
     cs_local_t *local = NULL;
233933
     xlator_t *this = NULL;
233933
 
233933
@@ -1000,16 +965,406 @@ cs_download(call_frame_t *frame)
233933
         goto out;
233933
     }
233933
 
233933
-    ret = gf_thread_create(&dthread, NULL, &cs_download_task, (void *)frame,
233933
-                           "downloadthread");
233933
+    ret = cs_download_task((void *)frame);
233933
+out:
233933
+    return ret;
233933
+}
233933
 
233933
-    pthread_join(dthread, (void **)&retval);
233933
+int
233933
+cs_set_xattr_req(call_frame_t *frame)
233933
+{
233933
+    cs_local_t *local = NULL;
233933
+    GF_UNUSED int ret = 0;
233933
+
233933
+    local = frame->local;
233933
+
233933
+    /* When remote reads are performed (i.e. reads on remote store),
233933
+     * there needs to be a way to associate a file on gluster volume
233933
+     * with its correspnding file on the remote store. In order to do
233933
+     * that, a unique key can be maintained as an xattr
233933
+     * (GF_CS_XATTR_ARCHIVE_UUID)on the stub file on gluster bricks.
233933
+     * This xattr should be provided to the plugin to
233933
+     * perform the read fop on the correct file. This assumes that the file
233933
+     * hierarchy and name need not be the same on remote store as that of
233933
+     * the gluster volume.
233933
+     */
233933
+    ret = dict_set_str(local->xattr_req, GF_CS_XATTR_ARCHIVE_UUID, "1");
233933
+
233933
+    return 0;
233933
+}
233933
 
233933
-    ret = *retval;
233933
+int
233933
+cs_update_xattrs(call_frame_t *frame, dict_t *xdata)
233933
+{
233933
+    cs_local_t *local = NULL;
233933
+    xlator_t *this = NULL;
233933
+    int size = -1;
233933
+    GF_UNUSED int ret = 0;
233933
+
233933
+    local = frame->local;
233933
+    this = frame->this;
233933
+
233933
+    local->xattrinfo.lxattr = GF_CALLOC(1, sizeof(cs_loc_xattr_t),
233933
+                                        gf_cs_mt_cs_lxattr_t);
233933
+    if (!local->xattrinfo.lxattr) {
233933
+        local->op_ret = -1;
233933
+        local->op_errno = ENOMEM;
233933
+        goto err;
233933
+    }
233933
+
233933
+    gf_uuid_copy(local->xattrinfo.lxattr->gfid, local->loc.gfid);
233933
+
233933
+    if (local->remotepath) {
233933
+        local->xattrinfo.lxattr->file_path = gf_strdup(local->remotepath);
233933
+        if (!local->xattrinfo.lxattr->file_path) {
233933
+            local->op_ret = -1;
233933
+            local->op_errno = ENOMEM;
233933
+            goto err;
233933
+        }
233933
+    }
233933
+
233933
+    ret = dict_get_gfuuid(xdata, GF_CS_XATTR_ARCHIVE_UUID,
233933
+                          &(local->xattrinfo.lxattr->uuid));
233933
+
233933
+    if (ret) {
233933
+        gf_uuid_clear(local->xattrinfo.lxattr->uuid);
233933
+    }
233933
+    size = strlen(this->name) - strlen("-cloudsync") + 1;
233933
+    local->xattrinfo.lxattr->volname = GF_CALLOC(1, size, gf_common_mt_char);
233933
+    if (!local->xattrinfo.lxattr->volname) {
233933
+        local->op_ret = -1;
233933
+        local->op_errno = ENOMEM;
233933
+        goto err;
233933
+    }
233933
+    strncpy(local->xattrinfo.lxattr->volname, this->name, size - 1);
233933
+    local->xattrinfo.lxattr->volname[size - 1] = '\0';
233933
+
233933
+    return 0;
233933
+err:
233933
+    cs_xattrinfo_wipe(local);
233933
+    return -1;
233933
+}
233933
+
233933
+int
233933
+cs_serve_readv(call_frame_t *frame, off_t offset, size_t size, uint32_t flags)
233933
+{
233933
+    xlator_t *this = NULL;
233933
+    cs_private_t *priv = NULL;
233933
+    int ret = -1;
233933
+    fd_t *fd = NULL;
233933
+    cs_local_t *local = NULL;
233933
+
233933
+    local = frame->local;
233933
+    this = frame->this;
233933
+    priv = this->private;
233933
+
233933
+    if (!local->remotepath) {
233933
+        ret = -1;
233933
+        gf_msg(this->name, GF_LOG_ERROR, 0, 0,
233933
+               "remote path not"
233933
+               " available. Check posix logs to resolve");
233933
+        goto out;
233933
+    }
233933
+
233933
+    if (!priv->stores) {
233933
+        gf_msg(this->name, GF_LOG_ERROR, 0, 0,
233933
+               "No remote store "
233933
+               "plugins found");
233933
+        ret = -1;
233933
+        goto out;
233933
+    }
233933
+
233933
+    if (local->fd) {
233933
+        fd = fd_anonymous(local->fd->inode);
233933
+    } else {
233933
+        fd = fd_anonymous(local->loc.inode);
233933
+    }
233933
+
233933
+    local->xattrinfo.size = size;
233933
+    local->xattrinfo.offset = offset;
233933
+    local->xattrinfo.flags = flags;
233933
+
233933
+    if (!fd) {
233933
+        gf_msg("CS", GF_LOG_ERROR, 0, 0, "fd creation failed");
233933
+        ret = -1;
233933
+        goto out;
233933
+    }
233933
+
233933
+    local->dlfd = fd;
233933
+    local->dloffset = offset;
233933
+
233933
+    /*this calling method is for per volume setting */
233933
+    ret = priv->stores->rdfop(frame, priv->stores->config);
233933
+    if (ret) {
233933
+        gf_msg(this->name, GF_LOG_ERROR, 0, 0,
233933
+               "read failed"
233933
+               ", remotepath: %s",
233933
+               local->remotepath);
233933
+        ret = -1;
233933
+        goto out;
233933
+    } else {
233933
+        gf_msg(this->name, GF_LOG_INFO, 0, 0,
233933
+               "read success, path"
233933
+               " : %s",
233933
+               local->remotepath);
233933
+    }
233933
 
233933
 out:
233933
-    if (retval)
233933
-        GF_FREE(retval);
233933
+    if (fd) {
233933
+        fd_unref(fd);
233933
+        local->dlfd = NULL;
233933
+    }
233933
+    return ret;
233933
+}
233933
+
233933
+int32_t
233933
+cs_readv_cbk(call_frame_t *frame, void *cookie, xlator_t *this, int32_t op_ret,
233933
+             int32_t op_errno, struct iovec *vector, int32_t count,
233933
+             struct iatt *stbuf, struct iobref *iobref, dict_t *xdata)
233933
+{
233933
+    cs_local_t *local = NULL;
233933
+    int ret = 0;
233933
+    uint64_t val = 0;
233933
+    fd_t *fd = NULL;
233933
+
233933
+    local = frame->local;
233933
+    fd = local->fd;
233933
+
233933
+    local->call_cnt++;
233933
+
233933
+    if (op_ret == -1) {
233933
+        ret = dict_get_uint64(xdata, GF_CS_OBJECT_STATUS, &val;;
233933
+        if (ret == 0) {
233933
+            if (val == GF_CS_ERROR) {
233933
+                gf_msg(this->name, GF_LOG_ERROR, 0, 0,
233933
+                       "could not get file state, unwinding");
233933
+                op_ret = -1;
233933
+                op_errno = EIO;
233933
+                goto unwind;
233933
+            } else {
233933
+                __cs_inode_ctx_update(this, fd->inode, val);
233933
+                gf_msg(this->name, GF_LOG_INFO, 0, 0, " state = %" PRIu64, val);
233933
+
233933
+                if (local->call_cnt == 1 &&
233933
+                    (val == GF_CS_REMOTE || val == GF_CS_DOWNLOADING)) {
233933
+                    gf_msg(this->name, GF_LOG_INFO, 0, 0,
233933
+                           " will read from remote : %" PRIu64, val);
233933
+                    goto repair;
233933
+                } else {
233933
+                    gf_msg(this->name, GF_LOG_ERROR, 0, 0,
233933
+                           "second readv, Unwinding");
233933
+                    goto unwind;
233933
+                }
233933
+            }
233933
+        } else {
233933
+            gf_msg(this->name, GF_LOG_ERROR, 0, 0,
233933
+                   "file state "
233933
+                   "could not be figured, unwinding");
233933
+            goto unwind;
233933
+        }
233933
+    } else {
233933
+        /* successful readv => file is local */
233933
+        __cs_inode_ctx_update(this, fd->inode, GF_CS_LOCAL);
233933
+        gf_msg(this->name, GF_LOG_INFO, 0, 0,
233933
+               "state : GF_CS_LOCAL"
233933
+               ", readv successful");
233933
+
233933
+        goto unwind;
233933
+    }
233933
+
233933
+repair:
233933
+    ret = locate_and_execute(frame);
233933
+    if (ret) {
233933
+        goto unwind;
233933
+    }
233933
+
233933
+    return 0;
233933
+
233933
+unwind:
233933
+    CS_STACK_UNWIND(readv, frame, op_ret, op_errno, vector, count, stbuf,
233933
+                    iobref, xdata);
233933
+
233933
+    return 0;
233933
+}
233933
+
233933
+int32_t
233933
+cs_resume_readv(call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size,
233933
+                off_t offset, uint32_t flags, dict_t *xdata)
233933
+{
233933
+    int ret = 0;
233933
+
233933
+    ret = cs_resume_postprocess(this, frame, fd->inode);
233933
+    if (ret) {
233933
+        goto unwind;
233933
+    }
233933
+
233933
+    cs_inodelk_unlock(frame);
233933
+
233933
+    STACK_WIND(frame, cs_readv_cbk, FIRST_CHILD(this),
233933
+               FIRST_CHILD(this)->fops->readv, fd, size, offset, flags, xdata);
233933
+
233933
+    return 0;
233933
+
233933
+unwind:
233933
+    cs_inodelk_unlock(frame);
233933
+
233933
+    cs_common_cbk(frame);
233933
+
233933
+    return 0;
233933
+}
233933
+
233933
+int32_t
233933
+cs_resume_remote_readv(call_frame_t *frame, xlator_t *this, fd_t *fd,
233933
+                       size_t size, off_t offset, uint32_t flags, dict_t *xdata)
233933
+{
233933
+    int ret = 0;
233933
+    cs_local_t *local = NULL;
233933
+    gf_cs_obj_state state = -1;
233933
+    cs_inode_ctx_t *ctx = NULL;
233933
+
233933
+    cs_inodelk_unlock(frame);
233933
+
233933
+    local = frame->local;
233933
+    if (!local) {
233933
+        ret = -1;
233933
+        goto unwind;
233933
+    }
233933
+
233933
+    __cs_inode_ctx_get(this, fd->inode, &ctx;;
233933
+
233933
+    state = __cs_get_file_state(this, fd->inode, ctx);
233933
+    if (state == GF_CS_ERROR) {
233933
+        gf_msg(this->name, GF_LOG_ERROR, 0, 0,
233933
+               "status is GF_CS_ERROR."
233933
+               " Aborting readv");
233933
+        local->op_ret = -1;
233933
+        local->op_errno = EREMOTE;
233933
+        ret = -1;
233933
+        goto unwind;
233933
+    }
233933
+
233933
+    /* Serve readv from remote store only if it is remote. */
233933
+    gf_msg_debug(this->name, 0, "status of file %s is %d",
233933
+                 local->remotepath ? local->remotepath : "", state);
233933
+
233933
+    /* We will reach this condition if local inode ctx had REMOTE
233933
+     * state when the control was in cs_readv but after stat
233933
+     * we got an updated state saying that the file is LOCAL.
233933
+     */
233933
+    if (state == GF_CS_LOCAL) {
233933
+        STACK_WIND(frame, cs_readv_cbk, FIRST_CHILD(this),
233933
+                   FIRST_CHILD(this)->fops->readv, fd, size, offset, flags,
233933
+                   xdata);
233933
+    } else if (state == GF_CS_REMOTE) {
233933
+        ret = cs_resume_remote_readv_postprocess(this, frame, fd->inode, offset,
233933
+                                                 size, flags);
233933
+        /* Failed to submit the remote readv fop to plugin */
233933
+        if (ret) {
233933
+            local->op_ret = -1;
233933
+            local->op_errno = EREMOTE;
233933
+            goto unwind;
233933
+        }
233933
+        /* When the file is in any other intermediate state,
233933
+         * we should not perform remote reads.
233933
+         */
233933
+    } else {
233933
+        local->op_ret = -1;
233933
+        local->op_errno = EINVAL;
233933
+        goto unwind;
233933
+    }
233933
+
233933
+    return 0;
233933
+
233933
+unwind:
233933
+    cs_common_cbk(frame);
233933
+
233933
+    return 0;
233933
+}
233933
+
233933
+int32_t
233933
+cs_readv(call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size,
233933
+         off_t offset, uint32_t flags, dict_t *xdata)
233933
+{
233933
+    int op_errno = -1;
233933
+    cs_local_t *local = NULL;
233933
+    int ret = 0;
233933
+    cs_inode_ctx_t *ctx = NULL;
233933
+    gf_cs_obj_state state = -1;
233933
+    cs_private_t *priv = NULL;
233933
+
233933
+    VALIDATE_OR_GOTO(frame, err);
233933
+    VALIDATE_OR_GOTO(this, err);
233933
+    VALIDATE_OR_GOTO(fd, err);
233933
+
233933
+    priv = this->private;
233933
+
233933
+    local = cs_local_init(this, frame, NULL, fd, GF_FOP_READ);
233933
+    if (!local) {
233933
+        gf_msg(this->name, GF_LOG_ERROR, 0, 0, "local init failed");
233933
+        op_errno = ENOMEM;
233933
+        goto err;
233933
+    }
233933
+
233933
+    __cs_inode_ctx_get(this, fd->inode, &ctx;;
233933
+
233933
+    if (ctx)
233933
+        state = __cs_get_file_state(this, fd->inode, ctx);
233933
+    else
233933
+        state = GF_CS_LOCAL;
233933
+
233933
+    local->xattr_req = xdata ? dict_ref(xdata) : (xdata = dict_new());
233933
+
233933
+    ret = dict_set_uint32(local->xattr_req, GF_CS_OBJECT_STATUS, 1);
233933
+    if (ret) {
233933
+        gf_msg(this->name, GF_LOG_ERROR, 0, 0,
233933
+               "dict_set failed key:"
233933
+               " %s",
233933
+               GF_CS_OBJECT_STATUS);
233933
+        goto err;
233933
+    }
233933
+
233933
+    if (priv->remote_read) {
233933
+        local->stub = fop_readv_stub(frame, cs_resume_remote_readv, fd, size,
233933
+                                     offset, flags, xdata);
233933
+    } else {
233933
+        local->stub = fop_readv_stub(frame, cs_resume_readv, fd, size, offset,
233933
+                                     flags, xdata);
233933
+    }
233933
+    if (!local->stub) {
233933
+        gf_msg(this->name, GF_LOG_ERROR, 0, 0, "insufficient memory");
233933
+        op_errno = ENOMEM;
233933
+        goto err;
233933
+    }
233933
+
233933
+    if (state == GF_CS_LOCAL) {
233933
+        STACK_WIND(frame, cs_readv_cbk, FIRST_CHILD(this),
233933
+                   FIRST_CHILD(this)->fops->readv, fd, size, offset, flags,
233933
+                   xdata);
233933
+    } else {
233933
+        local->call_cnt++;
233933
+        ret = locate_and_execute(frame);
233933
+        if (ret) {
233933
+            op_errno = ENOMEM;
233933
+            goto err;
233933
+        }
233933
+    }
233933
+
233933
+    return 0;
233933
+
233933
+err:
233933
+    CS_STACK_UNWIND(readv, frame, -1, op_errno, NULL, -1, NULL, NULL, NULL);
233933
+
233933
+    return 0;
233933
+}
233933
+
233933
+int
233933
+cs_resume_remote_readv_postprocess(xlator_t *this, call_frame_t *frame,
233933
+                                   inode_t *inode, off_t offset, size_t size,
233933
+                                   uint32_t flags)
233933
+{
233933
+    int ret = 0;
233933
+
233933
+    ret = cs_serve_readv(frame, offset, size, flags);
233933
 
233933
     return ret;
233933
 }
233933
@@ -1059,7 +1414,7 @@ cs_stat_check_cbk(call_frame_t *frame, void *cookie, xlator_t *this, int op_ret,
233933
                 goto err;
233933
             } else {
233933
                 ret = __cs_inode_ctx_update(this, inode, val);
233933
-                gf_msg_debug(this->name, 0, "status : %lu", val);
233933
+                gf_msg_debug(this->name, 0, "status : %" PRIu64, val);
233933
                 if (ret) {
233933
                     gf_msg(this->name, GF_LOG_ERROR, 0, 0, "ctx update failed");
233933
                     local->op_ret = -1;
233933
@@ -1087,6 +1442,10 @@ cs_stat_check_cbk(call_frame_t *frame, void *cookie, xlator_t *this, int op_ret,
233933
             gf_msg_debug(this->name, 0, "NULL filepath");
233933
         }
233933
 
233933
+        ret = cs_update_xattrs(frame, xdata);
233933
+        if (ret)
233933
+            goto err;
233933
+
233933
         local->op_ret = 0;
233933
         local->xattr_rsp = dict_ref(xdata);
233933
         memcpy(&local->stbuf, stbuf, sizeof(struct iatt));
233933
@@ -1121,6 +1480,8 @@ cs_do_stat_check(call_frame_t *main_frame)
233933
         goto err;
233933
     }
233933
 
233933
+    cs_set_xattr_req(main_frame);
233933
+
233933
     if (local->fd) {
233933
         STACK_WIND(main_frame, cs_stat_check_cbk, FIRST_CHILD(this),
233933
                    FIRST_CHILD(this)->fops->fstat, local->fd, local->xattr_req);
233933
@@ -1177,6 +1538,10 @@ cs_common_cbk(call_frame_t *frame)
233933
                             NULL, NULL, NULL);
233933
             break;
233933
 
233933
+        case GF_FOP_TRUNCATE:
233933
+            CS_STACK_UNWIND(truncate, frame, local->op_ret, local->op_errno,
233933
+                            NULL, NULL, NULL);
233933
+            break;
233933
         default:
233933
             break;
233933
     }
233933
@@ -1427,7 +1792,7 @@ __cs_inode_ctx_get(xlator_t *this, inode_t *inode, cs_inode_ctx_t **ctx)
233933
     if (ret)
233933
         *ctx = NULL;
233933
     else
233933
-        *ctx = (cs_inode_ctx_t *)ctxint;
233933
+        *ctx = (cs_inode_ctx_t *)(uintptr_t)ctxint;
233933
 
233933
     return;
233933
 }
233933
@@ -1452,7 +1817,7 @@ __cs_inode_ctx_update(xlator_t *this, inode_t *inode, uint64_t val)
233933
 
233933
             ctx->state = val;
233933
 
233933
-            ctxint = (uint64_t)ctx;
233933
+            ctxint = (uint64_t)(uintptr_t)ctx;
233933
 
233933
             ret = __inode_ctx_set(inode, this, &ctxint);
233933
             if (ret) {
233933
@@ -1460,7 +1825,7 @@ __cs_inode_ctx_update(xlator_t *this, inode_t *inode, uint64_t val)
233933
                 goto out;
233933
             }
233933
         } else {
233933
-            ctx = (cs_inode_ctx_t *)ctxint;
233933
+            ctx = (cs_inode_ctx_t *)(uintptr_t)ctxint;
233933
 
233933
             ctx->state = val;
233933
         }
233933
@@ -1483,7 +1848,7 @@ cs_inode_ctx_reset(xlator_t *this, inode_t *inode)
233933
         return 0;
233933
     }
233933
 
233933
-    ctx = (cs_inode_ctx_t *)ctxint;
233933
+    ctx = (cs_inode_ctx_t *)(uintptr_t)ctxint;
233933
 
233933
     GF_FREE(ctx);
233933
     return 0;
233933
@@ -1532,6 +1897,57 @@ cs_resume_postprocess(xlator_t *this, call_frame_t *frame, inode_t *inode)
233933
 out:
233933
     return ret;
233933
 }
233933
+
233933
+int32_t
233933
+__cs_get_dict_str(char **str, dict_t *xattr, const char *name, int *errnum)
233933
+{
233933
+    data_t *data = NULL;
233933
+    int ret = -1;
233933
+
233933
+    assert(str != NULL);
233933
+
233933
+    data = dict_get(xattr, (char *)name);
233933
+    if (!data) {
233933
+        *errnum = ENODATA;
233933
+        goto out;
233933
+    }
233933
+
233933
+    *str = GF_CALLOC(data->len + 1, sizeof(char), gf_common_mt_char);
233933
+    if (!(*str)) {
233933
+        *errnum = ENOMEM;
233933
+        goto out;
233933
+    }
233933
+
233933
+    memcpy(*str, data->data, sizeof(char) * (data->len));
233933
+    return 0;
233933
+
233933
+out:
233933
+    return ret;
233933
+}
233933
+
233933
+int32_t
233933
+__cs_get_dict_uuid(uuid_t uuid, dict_t *xattr, const char *name, int *errnum)
233933
+{
233933
+    data_t *data = NULL;
233933
+    int ret = -1;
233933
+
233933
+    assert(uuid != NULL);
233933
+
233933
+    data = dict_get(xattr, (char *)name);
233933
+    if (!data) {
233933
+        *errnum = ENODATA;
233933
+        goto out;
233933
+    }
233933
+
233933
+    assert(data->len == sizeof(uuid_t));
233933
+
233933
+    gf_uuid_copy(uuid, (unsigned char *)data->data);
233933
+    return 0;
233933
+
233933
+out:
233933
+    return ret;
233933
+}
233933
+
233933
 int32_t
233933
 cs_fdctx_to_dict(xlator_t *this, fd_t *fd, dict_t *dict)
233933
 {
233933
@@ -1606,7 +2022,6 @@ cs_notify(xlator_t *this, int event, void *data, ...)
233933
 
233933
 struct xlator_fops cs_fops = {
233933
     .stat = cs_stat,
233933
-    .readdirp = cs_readdirp,
233933
     .truncate = cs_truncate,
233933
     .seek = cs_seek,
233933
     .statfs = cs_statfs,
233933
@@ -1627,7 +2042,9 @@ struct xlator_fops cs_fops = {
233933
     .zerofill = cs_zerofill,
233933
 };
233933
 
233933
-struct xlator_cbks cs_cbks = {};
233933
+struct xlator_cbks cs_cbks = {
233933
+    .forget = cs_forget,
233933
+};
233933
 
233933
 struct xlator_dumpops cs_dumpops = {
233933
     .fdctx_to_dict = cs_fdctx_to_dict,
233933
@@ -1647,6 +2064,15 @@ struct volume_options cs_options[] = {
233933
     {.key = {"cloudsync-storetype"},
233933
      .type = GF_OPTION_TYPE_STR,
233933
      .description = "Defines which remote store is enabled"},
233933
+    {.key = {"cloudsync-remote-read"},
233933
+     .type = GF_OPTION_TYPE_BOOL,
233933
+     .description = "Defines a remote read fop when on"},
233933
+    {.key = {"cloudsync-store-id"},
233933
+     .type = GF_OPTION_TYPE_STR,
233933
+     .description = "Defines a volume wide store id"},
233933
+    {.key = {"cloudsync-product-id"},
233933
+     .type = GF_OPTION_TYPE_STR,
233933
+     .description = "Defines a volume wide product id"},
233933
     {.key = {NULL}},
233933
 };
233933
 
233933
diff --git a/xlators/features/cloudsync/src/cloudsync.h b/xlators/features/cloudsync/src/cloudsync.h
233933
index dbdb207..0cb800a 100644
233933
--- a/xlators/features/cloudsync/src/cloudsync.h
233933
+++ b/xlators/features/cloudsync/src/cloudsync.h
233933
@@ -19,6 +19,7 @@
233933
 #include "cloudsync-common.h"
233933
 #include "cloudsync-autogen-fops.h"
233933
 
233933
+#define ALIGN_SIZE 4096
233933
 #define CS_LOCK_DOMAIN "cs.protect.file.stat"
233933
 typedef struct cs_dlstore {
233933
     off_t off;
233933
@@ -29,6 +30,7 @@ typedef struct cs_dlstore {
233933
 } cs_dlstore;
233933
 
233933
 typedef struct cs_inode_ctx {
233933
+    cs_loc_xattr_t locxattr;
233933
     gf_cs_obj_state state;
233933
 } cs_inode_ctx_t;
233933
 
233933
@@ -100,4 +102,22 @@ cs_truncate_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
233933
 int32_t
233933
 cs_resume_truncate(call_frame_t *frame, xlator_t *this, loc_t *loc,
233933
                    off_t offset, dict_t *xattr_req);
233933
+
233933
+int32_t
233933
+cs_readv_cbk(call_frame_t *frame, void *cookie, xlator_t *this, int32_t op_ret,
233933
+             int32_t op_errno, struct iovec *vector, int32_t count,
233933
+             struct iatt *stbuf, struct iobref *iobref, dict_t *xdata);
233933
+int32_t
233933
+cs_resume_readv(call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size,
233933
+                off_t offset, uint32_t flags, dict_t *xdata);
233933
+int32_t
233933
+cs_readv(call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size,
233933
+         off_t offset, uint32_t flags, dict_t *xdata);
233933
+
233933
+int
233933
+cs_resume_remote_readv_postprocess(xlator_t *this, call_frame_t *frame,
233933
+                                   inode_t *inode, off_t offset, size_t size,
233933
+                                   uint32_t flags);
233933
+int
233933
+cs_serve_readv(call_frame_t *frame, off_t offset, size_t size, uint32_t flags);
233933
 #endif /* __CLOUDSYNC_H__ */
233933
diff --git a/xlators/mgmt/glusterd/src/glusterd-volume-set.c b/xlators/mgmt/glusterd/src/glusterd-volume-set.c
233933
index 4b32fb6..73abf37 100644
233933
--- a/xlators/mgmt/glusterd/src/glusterd-volume-set.c
233933
+++ b/xlators/mgmt/glusterd/src/glusterd-volume-set.c
233933
@@ -3693,7 +3693,7 @@ struct volopt_map_entry glusterd_volopt_map[] = {
233933
      .op_version = GD_OP_VERSION_5_0,
233933
      .description = "enable/disable noatime option with ctime enabled.",
233933
      .flags = VOLOPT_FLAG_CLIENT_OPT | VOLOPT_FLAG_XLATOR_OPT},
233933
-    {.key = "feature.cloudsync-storetype",
233933
+    {.key = "features.cloudsync-storetype",
233933
      .voltype = "features/cloudsync",
233933
      .op_version = GD_OP_VERSION_5_0,
233933
      .flags = VOLOPT_FLAG_CLIENT_OPT},
233933
@@ -3721,4 +3721,9 @@ struct volopt_map_entry glusterd_volopt_map[] = {
233933
      .validate_fn = validate_boolean,
233933
      .description = "option to enforce mandatory lock on a file",
233933
      .flags = VOLOPT_FLAG_XLATOR_OPT},
233933
+    {.key = "features.cloudsync-remote-read",
233933
+     .voltype = "features/cloudsync",
233933
+     .value = "off",
233933
+     .op_version = GD_OP_VERSION_6_0,
233933
+     .flags = VOLOPT_FLAG_CLIENT_OPT},
233933
     {.key = NULL}};
233933
-- 
233933
1.8.3.1
233933