74b1de
From 10e9f850017d58fcd813ccce253784280326f1d0 Mon Sep 17 00:00:00 2001
74b1de
From: Anuradha Talur <atalur@commvault.com>
74b1de
Date: Tue, 20 Nov 2018 13:15:26 -0800
74b1de
Subject: [PATCH 152/169] storage/posix: changes with respect to cloudsync
74b1de
74b1de
Main changes include logic to update iatt buf
74b1de
with file size from extended attributes in posix
74b1de
rather than having this logic in cloudsync xlator.
74b1de
74b1de
backport of:https://review.gluster.org/#/c/glusterfs/+/21694/
74b1de
74b1de
> Change-Id: I44f5f8df7a01e496372557fe2f4eff368dbdaa33
74b1de
> fixes: bz#1642168
74b1de
> Signed-off-by: Anuradha Talur <atalur@commvault.com>
74b1de
74b1de
Change-Id: I34880d856fb3add4ce88d64021d08d95405fc1c1
74b1de
Signed-off-by: Susant Palai <spalai@redhat.com>
74b1de
Reviewed-on: https://code.engineering.redhat.com/gerrit/172191
74b1de
Tested-by: RHGS Build Bot <nigelb@redhat.com>
74b1de
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
74b1de
---
74b1de
 xlators/storage/posix/src/posix-entry-ops.c    |   1 +
74b1de
 xlators/storage/posix/src/posix-helpers.c      |  50 +++++++++
74b1de
 xlators/storage/posix/src/posix-inode-fd-ops.c | 139 ++++++++++++++++++++++---
74b1de
 xlators/storage/posix/src/posix.h              |   2 +
74b1de
 4 files changed, 177 insertions(+), 15 deletions(-)
74b1de
74b1de
diff --git a/xlators/storage/posix/src/posix-entry-ops.c b/xlators/storage/posix/src/posix-entry-ops.c
74b1de
index fbd83c4..b24a052 100644
74b1de
--- a/xlators/storage/posix/src/posix-entry-ops.c
74b1de
+++ b/xlators/storage/posix/src/posix-entry-ops.c
74b1de
@@ -272,6 +272,7 @@ posix_lookup(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *xdata)
74b1de
         }
74b1de
     }
74b1de
 
74b1de
+    posix_update_iatt_buf(&buf, -1, real_path, xdata);
74b1de
     if (priv->update_pgfid_nlinks) {
74b1de
         if (!gf_uuid_is_null(loc->pargfid) && !IA_ISDIR(buf.ia_type)) {
74b1de
             MAKE_PGFID_XATTR_KEY(pgfid_xattr_key, PGFID_XATTR_KEY_PREFIX,
74b1de
diff --git a/xlators/storage/posix/src/posix-helpers.c b/xlators/storage/posix/src/posix-helpers.c
74b1de
index 37e33a9..d0fd45a 100644
74b1de
--- a/xlators/storage/posix/src/posix-helpers.c
74b1de
+++ b/xlators/storage/posix/src/posix-helpers.c
74b1de
@@ -3453,3 +3453,53 @@ posix_check_dev_file(xlator_t *this, inode_t *inode, char *fop, int *op_errno)
74b1de
 out:
74b1de
     return ret;
74b1de
 }
74b1de
+
74b1de
+void
74b1de
+posix_update_iatt_buf(struct iatt *buf, int fd, char *loc, dict_t *xattr_req)
74b1de
+{
74b1de
+    int ret = 0;
74b1de
+    char val[4096] = {
74b1de
+        0,
74b1de
+    };
74b1de
+
74b1de
+    if (!xattr_req)
74b1de
+        return;
74b1de
+
74b1de
+    if (!(dict_getn(xattr_req, GF_CS_OBJECT_STATUS,
74b1de
+                    strlen(GF_CS_OBJECT_STATUS))))
74b1de
+        return;
74b1de
+
74b1de
+    if (fd != -1) {
74b1de
+        ret = sys_fgetxattr(fd, GF_CS_OBJECT_SIZE, &val, sizeof(val));
74b1de
+        if (ret > 0) {
74b1de
+            buf->ia_size = atoll(val);
74b1de
+        } else {
74b1de
+            /* Safe to assume that the other 2 xattrs are also not set*/
74b1de
+            return;
74b1de
+        }
74b1de
+        ret = sys_fgetxattr(fd, GF_CS_BLOCK_SIZE, &val, sizeof(val));
74b1de
+        if (ret > 0) {
74b1de
+            buf->ia_blksize = atoll(val);
74b1de
+        }
74b1de
+        ret = sys_fgetxattr(fd, GF_CS_NUM_BLOCKS, &val, sizeof(val));
74b1de
+        if (ret > 0) {
74b1de
+            buf->ia_blocks = atoll(val);
74b1de
+        }
74b1de
+    } else {
74b1de
+        ret = sys_lgetxattr(loc, GF_CS_OBJECT_SIZE, &val, sizeof(val));
74b1de
+        if (ret > 0) {
74b1de
+            buf->ia_size = atoll(val);
74b1de
+        } else {
74b1de
+            /* Safe to assume that the other 2 xattrs are also not set*/
74b1de
+            return;
74b1de
+        }
74b1de
+        ret = sys_lgetxattr(loc, GF_CS_BLOCK_SIZE, &val, sizeof(val));
74b1de
+        if (ret > 0) {
74b1de
+            buf->ia_blksize = atoll(val);
74b1de
+        }
74b1de
+        ret = sys_lgetxattr(loc, GF_CS_NUM_BLOCKS, &val, sizeof(val));
74b1de
+        if (ret > 0) {
74b1de
+            buf->ia_blocks = atoll(val);
74b1de
+        }
74b1de
+    }
74b1de
+}
74b1de
diff --git a/xlators/storage/posix/src/posix-inode-fd-ops.c b/xlators/storage/posix/src/posix-inode-fd-ops.c
74b1de
index 7dbbd3d..065fced 100644
74b1de
--- a/xlators/storage/posix/src/posix-inode-fd-ops.c
74b1de
+++ b/xlators/storage/posix/src/posix-inode-fd-ops.c
74b1de
@@ -108,6 +108,63 @@ extern char *marker_xattrs[];
74b1de
 static char *disallow_removexattrs[] = {GF_XATTR_VOL_ID_KEY, GFID_XATTR_KEY,
74b1de
                                         NULL};
74b1de
 
74b1de
+void
74b1de
+posix_cs_build_xattr_rsp(xlator_t *this, dict_t **rsp, dict_t *req, int fd,
74b1de
+                         char *loc)
74b1de
+{
74b1de
+    int ret = 0;
74b1de
+    uuid_t uuid;
74b1de
+
74b1de
+    if (!(dict_getn(req, GF_CS_OBJECT_STATUS, strlen(GF_CS_OBJECT_STATUS))))
74b1de
+        return;
74b1de
+
74b1de
+    if (!(*rsp)) {
74b1de
+        *rsp = dict_new();
74b1de
+        if (!(*rsp)) {
74b1de
+            return;
74b1de
+        }
74b1de
+    }
74b1de
+
74b1de
+    if (fd != -1) {
74b1de
+        if (dict_getn(req, GF_CS_XATTR_ARCHIVE_UUID,
74b1de
+                      strlen(GF_CS_XATTR_ARCHIVE_UUID))) {
74b1de
+            ret = sys_fgetxattr(fd, GF_CS_XATTR_ARCHIVE_UUID, uuid, 16);
74b1de
+            if (ret > 0) {
74b1de
+                ret = dict_set_gfuuid(*rsp, GF_CS_XATTR_ARCHIVE_UUID, uuid,
74b1de
+                                      true);
74b1de
+                if (ret) {
74b1de
+                    gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_DICT_SET_FAILED,
74b1de
+                           "%s: Failed to set "
74b1de
+                           "dictionary value for %s for fd %d",
74b1de
+                           uuid_utoa(uuid), GF_CS_XATTR_ARCHIVE_UUID, fd);
74b1de
+                }
74b1de
+            } else {
74b1de
+                gf_msg_debug(this->name, 0, "getxattr failed on %s for fd %d",
74b1de
+                             GF_CS_XATTR_ARCHIVE_UUID, fd);
74b1de
+            }
74b1de
+        }
74b1de
+    } else {
74b1de
+        if (dict_getn(req, GF_CS_XATTR_ARCHIVE_UUID,
74b1de
+                      strlen(GF_CS_XATTR_ARCHIVE_UUID))) {
74b1de
+            ret = sys_lgetxattr(loc, GF_CS_XATTR_ARCHIVE_UUID, uuid, 16);
74b1de
+            if (ret > 0) {
74b1de
+                ret = dict_set_gfuuid(*rsp, GF_CS_XATTR_ARCHIVE_UUID, uuid,
74b1de
+                                      true);
74b1de
+                if (ret) {
74b1de
+                    gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_DICT_SET_FAILED,
74b1de
+                           "%s: Failed to set "
74b1de
+                           "dictionary value for %s for loc %s",
74b1de
+                           uuid_utoa(uuid), GF_CS_XATTR_ARCHIVE_UUID, loc);
74b1de
+                }
74b1de
+            } else {
74b1de
+                gf_msg_debug(this->name, 0, "getxattr failed on %s for %s",
74b1de
+                             GF_CS_XATTR_ARCHIVE_UUID, loc);
74b1de
+            }
74b1de
+        }
74b1de
+    }
74b1de
+    return;
74b1de
+}
74b1de
+
74b1de
 int32_t
74b1de
 posix_stat(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *xdata)
74b1de
 {
74b1de
@@ -150,8 +207,11 @@ posix_stat(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *xdata)
74b1de
 
74b1de
         posix_cs_maintenance(this, NULL, loc, NULL, &buf, real_path, xdata,
74b1de
                              &xattr_rsp, _gf_true);
74b1de
+
74b1de
+        posix_cs_build_xattr_rsp(this, &xattr_rsp, xdata, -1, real_path);
74b1de
     }
74b1de
 
74b1de
+    posix_update_iatt_buf(&buf, -1, real_path, xdata);
74b1de
     op_ret = 0;
74b1de
 
74b1de
 out:
74b1de
@@ -422,6 +482,8 @@ posix_setattr(call_frame_t *frame, xlator_t *this, loc_t *loc,
74b1de
     if (xdata)
74b1de
         xattr_rsp = posix_xattr_fill(this, real_path, loc, NULL, -1, xdata,
74b1de
                                      &statpost);
74b1de
+    posix_update_iatt_buf(&statpre, -1, real_path, xdata);
74b1de
+    posix_update_iatt_buf(&statpost, -1, real_path, xdata);
74b1de
     op_ret = 0;
74b1de
 
74b1de
 out:
74b1de
@@ -898,6 +960,7 @@ posix_do_zerofill(call_frame_t *frame, xlator_t *this, fd_t *fd, off_t offset,
74b1de
         }
74b1de
     }
74b1de
 
74b1de
+    posix_update_iatt_buf(statpre, pfd->fd, NULL, xdata);
74b1de
     /* See if we can use FALLOC_FL_ZERO_RANGE to perform the zero fill.
74b1de
      * If it fails, fall back to _posix_do_zerofill() and an optional fsync.
74b1de
      */
74b1de
@@ -1366,6 +1429,7 @@ posix_truncate(call_frame_t *frame, xlator_t *this, loc_t *loc, off_t offset,
74b1de
         }
74b1de
     }
74b1de
 
74b1de
+    posix_update_iatt_buf(&prebuf, -1, real_path, xdata);
74b1de
     op_ret = sys_truncate(real_path, offset);
74b1de
     if (op_ret == -1) {
74b1de
         op_errno = errno;
74b1de
@@ -1405,6 +1469,10 @@ posix_open(call_frame_t *frame, xlator_t *this, loc_t *loc, int32_t flags,
74b1de
     int32_t _fd = -1;
74b1de
     struct posix_fd *pfd = NULL;
74b1de
     struct posix_private *priv = NULL;
74b1de
+    struct iatt preop = {
74b1de
+        0,
74b1de
+    };
74b1de
+    dict_t *rsp_xdata = NULL;
74b1de
     struct iatt stbuf = {
74b1de
         0,
74b1de
     };
74b1de
@@ -1471,6 +1539,18 @@ posix_open(call_frame_t *frame, xlator_t *this, loc_t *loc, int32_t flags,
74b1de
     pfd->flags = flags;
74b1de
     pfd->fd = _fd;
74b1de
 
74b1de
+    if (xdata) {
74b1de
+        op_ret = posix_fdstat(this, fd->inode, pfd->fd, &preop);
74b1de
+        if (op_ret == -1) {
74b1de
+            gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSTAT_FAILED,
74b1de
+                   "pre-operation fstat failed on fd=%p", fd);
74b1de
+            goto out;
74b1de
+        }
74b1de
+
74b1de
+        posix_cs_maintenance(this, fd, NULL, &pfd->fd, &preop, NULL, xdata,
74b1de
+                             &rsp_xdata, _gf_true);
74b1de
+    }
74b1de
+
74b1de
     op_ret = fd_ctx_set(fd, this, (uint64_t)(long)pfd);
74b1de
     if (op_ret)
74b1de
         gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_FD_PATH_SETTING_FAILED,
74b1de
@@ -1488,7 +1568,7 @@ out:
74b1de
 
74b1de
     SET_TO_OLD_FS_ID();
74b1de
 
74b1de
-    STACK_UNWIND_STRICT(open, frame, op_ret, op_errno, fd, NULL);
74b1de
+    STACK_UNWIND_STRICT(open, frame, op_ret, op_errno, fd, rsp_xdata);
74b1de
 
74b1de
     return 0;
74b1de
 }
74b1de
@@ -1573,6 +1653,7 @@ posix_readv(call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size,
74b1de
         }
74b1de
     }
74b1de
 
74b1de
+    posix_update_iatt_buf(&preop, _fd, NULL, xdata);
74b1de
     op_ret = sys_pread(_fd, iobuf->ptr, size, offset);
74b1de
     if (op_ret == -1) {
74b1de
         op_errno = errno;
74b1de
@@ -1878,6 +1959,7 @@ posix_writev(call_frame_t *frame, xlator_t *this, fd_t *fd,
74b1de
         }
74b1de
     }
74b1de
 
74b1de
+    posix_update_iatt_buf(&preop, _fd, NULL, xdata);
74b1de
     if (locked && write_append) {
74b1de
         if (preop.ia_size == offset || (fd->flags & O_APPEND))
74b1de
             is_append = 1;
74b1de
@@ -2531,10 +2613,8 @@ posix_setxattr(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *dict,
74b1de
         0,
74b1de
     };
74b1de
     data_t *tdata = NULL;
74b1de
-    char stime[4096];
74b1de
-    char sxattr[4096];
74b1de
+    char *cs_var = NULL;
74b1de
     gf_cs_obj_state state = -1;
74b1de
-    char remotepath[4096] = {0};
74b1de
     int i = 0;
74b1de
     int len;
74b1de
 
74b1de
@@ -2588,10 +2668,11 @@ posix_setxattr(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *dict,
74b1de
                 goto unlock;
74b1de
             }
74b1de
 
74b1de
-            sprintf(stime, "%" PRId64, tmp_stbuf.ia_mtime);
74b1de
+            cs_var = alloca(4096);
74b1de
+            sprintf(cs_var, "%" PRId64, tmp_stbuf.ia_mtime);
74b1de
 
74b1de
             /*TODO: may be should consider nano-second also */
74b1de
-            if (strncmp(stime, tdata->data, tdata->len) != 0) {
74b1de
+            if (strncmp(cs_var, tdata->data, tdata->len) > 0) {
74b1de
                 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
74b1de
                        "mtime "
74b1de
                        "passed is different from seen by file now."
74b1de
@@ -2601,31 +2682,54 @@ posix_setxattr(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *dict,
74b1de
                 goto unlock;
74b1de
             }
74b1de
 
74b1de
-            len = sprintf(sxattr, "%" PRIu64, tmp_stbuf.ia_size);
74b1de
+            len = sprintf(cs_var, "%" PRIu64, tmp_stbuf.ia_size);
74b1de
 
74b1de
-            ret = sys_lsetxattr(real_path, GF_CS_OBJECT_SIZE, sxattr, len,
74b1de
+            ret = sys_lsetxattr(real_path, GF_CS_OBJECT_SIZE, cs_var, len,
74b1de
                                 flags);
74b1de
             if (ret) {
74b1de
+                op_errno = errno;
74b1de
                 gf_msg(this->name, GF_LOG_ERROR, 0, 0,
74b1de
                        "setxattr failed. key %s err %d", GF_CS_OBJECT_SIZE,
74b1de
                        ret);
74b1de
+                goto unlock;
74b1de
+            }
74b1de
+
74b1de
+            len = sprintf(cs_var, "%" PRIu64, tmp_stbuf.ia_blocks);
74b1de
+
74b1de
+            ret = sys_lsetxattr(real_path, GF_CS_NUM_BLOCKS, cs_var, len,
74b1de
+                                flags);
74b1de
+            if (ret) {
74b1de
                 op_errno = errno;
74b1de
+                gf_msg(this->name, GF_LOG_ERROR, 0, 0,
74b1de
+                       "setxattr failed. key %s err %d", GF_CS_NUM_BLOCKS, ret);
74b1de
                 goto unlock;
74b1de
             }
74b1de
 
74b1de
+            len = sprintf(cs_var, "%" PRIu32, tmp_stbuf.ia_blksize);
74b1de
+
74b1de
+            ret = sys_lsetxattr(real_path, GF_CS_BLOCK_SIZE, cs_var, len,
74b1de
+                                flags);
74b1de
+            if (ret) {
74b1de
+                op_errno = errno;
74b1de
+                gf_msg(this->name, GF_LOG_ERROR, 0, 0,
74b1de
+                       "setxattr failed. key %s err %d", GF_CS_BLOCK_SIZE, ret);
74b1de
+                goto unlock;
74b1de
+            }
74b1de
+
74b1de
+            memset(cs_var, 0, 4096);
74b1de
             if (loc->path[0] == '/') {
74b1de
                 for (i = 1; i < strlen(loc->path); i++) {
74b1de
-                    remotepath[i - 1] = loc->path[i];
74b1de
+                    cs_var[i - 1] = loc->path[i];
74b1de
                 }
74b1de
 
74b1de
-                remotepath[i] = '\0';
74b1de
-                gf_msg_debug(this->name, GF_LOG_ERROR, "remotepath %s",
74b1de
-                             remotepath);
74b1de
+                cs_var[i] = '\0';
74b1de
+                gf_msg_debug(this->name, GF_LOG_ERROR, "remotepath %s", cs_var);
74b1de
             }
74b1de
 
74b1de
-            ret = sys_lsetxattr(real_path, GF_CS_OBJECT_REMOTE, remotepath,
74b1de
-                                strlen(loc->path), flags);
74b1de
+            ret = sys_lsetxattr(real_path, GF_CS_OBJECT_REMOTE, cs_var,
74b1de
+                                strlen(cs_var), flags);
74b1de
             if (ret) {
74b1de
+                op_errno = errno;
74b1de
                 gf_log("POSIX", GF_LOG_ERROR,
74b1de
                        "setxattr failed - %s"
74b1de
                        " %d",
74b1de
@@ -2635,13 +2739,14 @@ posix_setxattr(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *dict,
74b1de
 
74b1de
             ret = sys_truncate(real_path, 0);
74b1de
             if (ret) {
74b1de
+                op_errno = errno;
74b1de
                 gf_log("POSIX", GF_LOG_ERROR,
74b1de
                        "truncate failed - %s"
74b1de
                        " %d",
74b1de
                        GF_CS_OBJECT_SIZE, ret);
74b1de
-                op_errno = errno;
74b1de
                 ret = sys_lremovexattr(real_path, GF_CS_OBJECT_REMOTE);
74b1de
                 if (ret) {
74b1de
+                    op_errno = errno;
74b1de
                     gf_log("POSIX", GF_LOG_ERROR,
74b1de
                            "removexattr "
74b1de
                            "failed post processing- %s"
74b1de
@@ -2659,6 +2764,7 @@ posix_setxattr(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *dict,
74b1de
         }
74b1de
     unlock:
74b1de
         UNLOCK(&loc->inode->lock);
74b1de
+        op_ret = ret;
74b1de
         goto out;
74b1de
     }
74b1de
 
74b1de
@@ -4927,6 +5033,7 @@ posix_ftruncate(call_frame_t *frame, xlator_t *this, fd_t *fd, off_t offset,
74b1de
         }
74b1de
     }
74b1de
 
74b1de
+    posix_update_iatt_buf(&preop, _fd, NULL, xdata);
74b1de
     op_ret = sys_ftruncate(_fd, offset);
74b1de
 
74b1de
     if (op_ret == -1) {
74b1de
@@ -5008,8 +5115,10 @@ posix_fstat(call_frame_t *frame, xlator_t *this, fd_t *fd, dict_t *xdata)
74b1de
             gf_msg(this->name, GF_LOG_ERROR, 0, 0,
74b1de
                    "file state check failed, fd %p", fd);
74b1de
         }
74b1de
+        posix_cs_build_xattr_rsp(this, &xattr_rsp, xdata, _fd, NULL);
74b1de
     }
74b1de
 
74b1de
+    posix_update_iatt_buf(&buf, _fd, NULL, xdata);
74b1de
     op_ret = 0;
74b1de
 
74b1de
 out:
74b1de
diff --git a/xlators/storage/posix/src/posix.h b/xlators/storage/posix/src/posix.h
74b1de
index d5ba08c..1da4d01 100644
74b1de
--- a/xlators/storage/posix/src/posix.h
74b1de
+++ b/xlators/storage/posix/src/posix.h
74b1de
@@ -664,4 +664,6 @@ posix_check_dev_file(xlator_t *this, inode_t *inode, char *fop, int *op_errno);
74b1de
 int
74b1de
 posix_spawn_ctx_janitor_thread(xlator_t *this);
74b1de
 
74b1de
+void
74b1de
+posix_update_iatt_buf(struct iatt *buf, int fd, char *loc, dict_t *xdata);
74b1de
 #endif /* _POSIX_H */
74b1de
-- 
74b1de
1.8.3.1
74b1de