b7d4d7
From 775d500cd136bd8c940faaeffde1217c25a87e3d Mon Sep 17 00:00:00 2001
b7d4d7
From: Yaniv Kaul <ykaul@redhat.com>
b7d4d7
Date: Sun, 2 Jun 2019 21:14:18 +0300
b7d4d7
Subject: [PATCH 535/538] (multiple files) use dict_allocate_and_serialize()
b7d4d7
 where applicable.
b7d4d7
b7d4d7
This function does length, allocation and serialization for you.
b7d4d7
b7d4d7
Upstream patch:
b7d4d7
> Upstream-patch-link: https://review.gluster.org/#/c/glusterfs/+/22800
b7d4d7
> Change-Id: I142a259952a2fe83dd719442afaefe4a43a8e55e
b7d4d7
> updates: bz#1193929
b7d4d7
> Signed-off-by: Yaniv Kaul <ykaul@redhat.com>
b7d4d7
b7d4d7
Change-Id: I142a259952a2fe83dd719442afaefe4a43a8e55e
b7d4d7
BUG: 1911292
b7d4d7
Signed-off-by: Yaniv Kaul <ykaul@redhat.com>
b7d4d7
Reviewed-on: https://code.engineering.redhat.com/gerrit/228611
b7d4d7
Tested-by: RHGS Build Bot <nigelb@redhat.com>
b7d4d7
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
b7d4d7
---
b7d4d7
 xlators/cluster/afr/src/afr-inode-read.c       | 34 +++++---------------------
b7d4d7
 xlators/cluster/ec/src/ec-combine.c            | 16 +++---------
b7d4d7
 xlators/features/locks/src/posix.c             | 23 +++--------------
b7d4d7
 xlators/protocol/client/src/client-handshake.c | 14 +++--------
b7d4d7
 xlators/protocol/server/src/server-handshake.c | 24 +++++++-----------
b7d4d7
 xlators/protocol/server/src/server-helpers.c   | 27 +++-----------------
b7d4d7
 6 files changed, 28 insertions(+), 110 deletions(-)
b7d4d7
b7d4d7
diff --git a/xlators/cluster/afr/src/afr-inode-read.c b/xlators/cluster/afr/src/afr-inode-read.c
b7d4d7
index 523a5b4..cf305af 100644
b7d4d7
--- a/xlators/cluster/afr/src/afr-inode-read.c
b7d4d7
+++ b/xlators/cluster/afr/src/afr-inode-read.c
b7d4d7
@@ -948,24 +948,13 @@ unlock:
b7d4d7
             goto unwind;
b7d4d7
         }
b7d4d7
 
b7d4d7
-        len = dict_serialized_length(local->dict);
b7d4d7
-        if (len <= 0) {
b7d4d7
-            goto unwind;
b7d4d7
-        }
b7d4d7
-
b7d4d7
-        lockinfo_buf = GF_CALLOC(1, len, gf_common_mt_char);
b7d4d7
-        if (!lockinfo_buf) {
b7d4d7
+        op_ret = dict_allocate_and_serialize(
b7d4d7
+            local->dict, (char **)&lockinfo_buf, (unsigned int *)&len;;
b7d4d7
+        if (op_ret != 0) {
b7d4d7
             local->op_ret = -1;
b7d4d7
-            local->op_errno = ENOMEM;
b7d4d7
             goto unwind;
b7d4d7
         }
b7d4d7
 
b7d4d7
-        op_ret = dict_serialize(local->dict, lockinfo_buf);
b7d4d7
-        if (op_ret < 0) {
b7d4d7
-            local->op_ret = -1;
b7d4d7
-            local->op_errno = -op_ret;
b7d4d7
-        }
b7d4d7
-
b7d4d7
         op_ret = dict_set_dynptr(newdict, GF_XATTR_LOCKINFO_KEY,
b7d4d7
                                  (void *)lockinfo_buf, len);
b7d4d7
         if (op_ret < 0) {
b7d4d7
@@ -1064,24 +1053,13 @@ unlock:
b7d4d7
             goto unwind;
b7d4d7
         }
b7d4d7
 
b7d4d7
-        len = dict_serialized_length(local->dict);
b7d4d7
-        if (len <= 0) {
b7d4d7
-            goto unwind;
b7d4d7
-        }
b7d4d7
-
b7d4d7
-        lockinfo_buf = GF_CALLOC(1, len, gf_common_mt_char);
b7d4d7
-        if (!lockinfo_buf) {
b7d4d7
+        op_ret = dict_allocate_and_serialize(
b7d4d7
+            local->dict, (char **)&lockinfo_buf, (unsigned int *)&len;;
b7d4d7
+        if (op_ret != 0) {
b7d4d7
             local->op_ret = -1;
b7d4d7
-            local->op_errno = ENOMEM;
b7d4d7
             goto unwind;
b7d4d7
         }
b7d4d7
 
b7d4d7
-        op_ret = dict_serialize(local->dict, lockinfo_buf);
b7d4d7
-        if (op_ret < 0) {
b7d4d7
-            local->op_ret = -1;
b7d4d7
-            local->op_errno = -op_ret;
b7d4d7
-        }
b7d4d7
-
b7d4d7
         op_ret = dict_set_dynptr(newdict, GF_XATTR_LOCKINFO_KEY,
b7d4d7
                                  (void *)lockinfo_buf, len);
b7d4d7
         if (op_ret < 0) {
b7d4d7
diff --git a/xlators/cluster/ec/src/ec-combine.c b/xlators/cluster/ec/src/ec-combine.c
b7d4d7
index 99e5534..9d712b3 100644
b7d4d7
--- a/xlators/cluster/ec/src/ec-combine.c
b7d4d7
+++ b/xlators/cluster/ec/src/ec-combine.c
b7d4d7
@@ -486,22 +486,12 @@ ec_dict_data_merge(ec_cbk_data_t *cbk, int32_t which, char *key)
b7d4d7
 
b7d4d7
     tmp = NULL;
b7d4d7
 
b7d4d7
-    len = dict_serialized_length(lockinfo);
b7d4d7
-    if (len < 0) {
b7d4d7
-        err = len;
b7d4d7
-
b7d4d7
-        goto out;
b7d4d7
-    }
b7d4d7
-    ptr = GF_MALLOC(len, gf_common_mt_char);
b7d4d7
-    if (ptr == NULL) {
b7d4d7
-        err = -ENOMEM;
b7d4d7
-
b7d4d7
-        goto out;
b7d4d7
-    }
b7d4d7
-    err = dict_serialize(lockinfo, ptr);
b7d4d7
+    err = dict_allocate_and_serialize(lockinfo, (char **)&ptr,
b7d4d7
+                                      (unsigned int *)&len;;
b7d4d7
     if (err != 0) {
b7d4d7
         goto out;
b7d4d7
     }
b7d4d7
+
b7d4d7
     dict = (which == EC_COMBINE_XDATA) ? cbk->xdata : cbk->dict;
b7d4d7
     err = dict_set_dynptr(dict, key, ptr, len);
b7d4d7
     if (err != 0) {
b7d4d7
diff --git a/xlators/features/locks/src/posix.c b/xlators/features/locks/src/posix.c
b7d4d7
index 5ae0125..cdd1ff7 100644
b7d4d7
--- a/xlators/features/locks/src/posix.c
b7d4d7
+++ b/xlators/features/locks/src/posix.c
b7d4d7
@@ -1547,8 +1547,9 @@ pl_fgetxattr_handle_lockinfo(xlator_t *this, fd_t *fd, dict_t *dict,
b7d4d7
         goto out;
b7d4d7
     }
b7d4d7
 
b7d4d7
-    len = dict_serialized_length(tmp);
b7d4d7
-    if (len < 0) {
b7d4d7
+    op_ret = dict_allocate_and_serialize(tmp, (char **)&buf,
b7d4d7
+                                         (unsigned int *)&len;;
b7d4d7
+    if (op_ret != 0) {
b7d4d7
         *op_errno = -op_ret;
b7d4d7
         op_ret = -1;
b7d4d7
         gf_log(this->name, GF_LOG_WARNING,
b7d4d7
@@ -1558,24 +1559,6 @@ pl_fgetxattr_handle_lockinfo(xlator_t *this, fd_t *fd, dict_t *dict,
b7d4d7
         goto out;
b7d4d7
     }
b7d4d7
 
b7d4d7
-    buf = GF_CALLOC(1, len, gf_common_mt_char);
b7d4d7
-    if (buf == NULL) {
b7d4d7
-        op_ret = -1;
b7d4d7
-        *op_errno = ENOMEM;
b7d4d7
-        goto out;
b7d4d7
-    }
b7d4d7
-
b7d4d7
-    op_ret = dict_serialize(tmp, buf);
b7d4d7
-    if (op_ret < 0) {
b7d4d7
-        *op_errno = -op_ret;
b7d4d7
-        op_ret = -1;
b7d4d7
-        gf_log(this->name, GF_LOG_WARNING,
b7d4d7
-               "dict_serialize failed (%s) while handling lockinfo "
b7d4d7
-               "for fd (ptr: %p inode-gfid:%s)",
b7d4d7
-               strerror(*op_errno), fd, uuid_utoa(fd->inode->gfid));
b7d4d7
-        goto out;
b7d4d7
-    }
b7d4d7
-
b7d4d7
     op_ret = dict_set_dynptr(dict, GF_XATTR_LOCKINFO_KEY, buf, len);
b7d4d7
     if (op_ret < 0) {
b7d4d7
         *op_errno = -op_ret;
b7d4d7
diff --git a/xlators/protocol/client/src/client-handshake.c b/xlators/protocol/client/src/client-handshake.c
b7d4d7
index 0002361..6b20d92 100644
b7d4d7
--- a/xlators/protocol/client/src/client-handshake.c
b7d4d7
+++ b/xlators/protocol/client/src/client-handshake.c
b7d4d7
@@ -1286,18 +1286,10 @@ client_setvolume(xlator_t *this, struct rpc_clnt *rpc)
b7d4d7
                "Failed to set client opversion in handshake message");
b7d4d7
     }
b7d4d7
 
b7d4d7
-    ret = dict_serialized_length(options);
b7d4d7
-    if (ret < 0) {
b7d4d7
-        gf_msg(this->name, GF_LOG_ERROR, 0, PC_MSG_DICT_ERROR,
b7d4d7
-               "failed to get serialized length of dict");
b7d4d7
+    ret = dict_allocate_and_serialize(options, (char **)&req.dict.dict_val,
b7d4d7
+                                      &req.dict.dict_len);
b7d4d7
+    if (ret != 0) {
b7d4d7
         ret = -1;
b7d4d7
-        goto fail;
b7d4d7
-    }
b7d4d7
-    req.dict.dict_len = ret;
b7d4d7
-    req.dict.dict_val = GF_CALLOC(1, req.dict.dict_len,
b7d4d7
-                                  gf_client_mt_clnt_req_buf_t);
b7d4d7
-    ret = dict_serialize(options, req.dict.dict_val);
b7d4d7
-    if (ret < 0) {
b7d4d7
         gf_msg(this->name, GF_LOG_ERROR, 0, PC_MSG_DICT_SERIALIZE_FAIL,
b7d4d7
                "failed to serialize "
b7d4d7
                "dictionary");
b7d4d7
diff --git a/xlators/protocol/server/src/server-handshake.c b/xlators/protocol/server/src/server-handshake.c
b7d4d7
index eeca73c..54dc030 100644
b7d4d7
--- a/xlators/protocol/server/src/server-handshake.c
b7d4d7
+++ b/xlators/protocol/server/src/server-handshake.c
b7d4d7
@@ -676,22 +676,16 @@ fail:
b7d4d7
     GF_ASSERT(rsp);
b7d4d7
 
b7d4d7
     rsp->op_ret = 0;
b7d4d7
-    ret = dict_serialized_length(reply);
b7d4d7
-    if (ret > 0) {
b7d4d7
-        rsp->dict.dict_len = ret;
b7d4d7
-        rsp->dict.dict_val = GF_CALLOC(1, rsp->dict.dict_len,
b7d4d7
-                                       gf_server_mt_rsp_buf_t);
b7d4d7
-        if (rsp->dict.dict_val) {
b7d4d7
-            ret = dict_serialize(reply, rsp->dict.dict_val);
b7d4d7
-            if (ret < 0) {
b7d4d7
-                gf_msg_debug("server-handshake", 0,
b7d4d7
-                             "failed "
b7d4d7
-                             "to serialize reply dict");
b7d4d7
-                op_ret = -1;
b7d4d7
-                op_errno = -ret;
b7d4d7
-            }
b7d4d7
-        }
b7d4d7
+
b7d4d7
+    ret = dict_allocate_and_serialize(reply, (char **)&rsp->dict.dict_val,
b7d4d7
+                                      &rsp->dict.dict_len);
b7d4d7
+    if (ret != 0) {
b7d4d7
+        ret = -1;
b7d4d7
+        gf_msg_debug("server-handshake", 0, "failed to serialize reply dict");
b7d4d7
+        op_ret = -1;
b7d4d7
+        op_errno = -ret;
b7d4d7
     }
b7d4d7
+
b7d4d7
     rsp->op_ret = op_ret;
b7d4d7
     rsp->op_errno = gf_errno_to_error(op_errno);
b7d4d7
 
b7d4d7
diff --git a/xlators/protocol/server/src/server-helpers.c b/xlators/protocol/server/src/server-helpers.c
b7d4d7
index e74a24d..33959b5 100644
b7d4d7
--- a/xlators/protocol/server/src/server-helpers.c
b7d4d7
+++ b/xlators/protocol/server/src/server-helpers.c
b7d4d7
@@ -902,7 +902,6 @@ serialize_rsp_direntp(gf_dirent_t *entries, gfs3_readdirp_rsp *rsp)
b7d4d7
     gfs3_dirplist *trav = NULL;
b7d4d7
     gfs3_dirplist *prev = NULL;
b7d4d7
     int ret = -1;
b7d4d7
-    int temp = 0;
b7d4d7
 
b7d4d7
     GF_VALIDATE_OR_GOTO("server", entries, out);
b7d4d7
     GF_VALIDATE_OR_GOTO("server", rsp, out);
b7d4d7
@@ -923,28 +922,10 @@ serialize_rsp_direntp(gf_dirent_t *entries, gfs3_readdirp_rsp *rsp)
b7d4d7
 
b7d4d7
         /* if 'dict' is present, pack it */
b7d4d7
         if (entry->dict) {
b7d4d7
-            temp = dict_serialized_length(entry->dict);
b7d4d7
-
b7d4d7
-            if (temp < 0) {
b7d4d7
-                gf_msg(THIS->name, GF_LOG_ERROR, EINVAL, PS_MSG_INVALID_ENTRY,
b7d4d7
-                       "failed to get "
b7d4d7
-                       "serialized length of reply dict");
b7d4d7
-                errno = EINVAL;
b7d4d7
-                trav->dict.dict_len = 0;
b7d4d7
-                goto out;
b7d4d7
-            }
b7d4d7
-            trav->dict.dict_len = temp;
b7d4d7
-
b7d4d7
-            trav->dict.dict_val = GF_CALLOC(1, trav->dict.dict_len,
b7d4d7
-                                            gf_server_mt_rsp_buf_t);
b7d4d7
-            if (!trav->dict.dict_val) {
b7d4d7
-                errno = ENOMEM;
b7d4d7
-                trav->dict.dict_len = 0;
b7d4d7
-                goto out;
b7d4d7
-            }
b7d4d7
-
b7d4d7
-            ret = dict_serialize(entry->dict, trav->dict.dict_val);
b7d4d7
-            if (ret < 0) {
b7d4d7
+            ret = dict_allocate_and_serialize(entry->dict,
b7d4d7
+                                              (char **)&trav->dict.dict_val,
b7d4d7
+                                              &trav->dict.dict_len);
b7d4d7
+            if (ret != 0) {
b7d4d7
                 gf_msg(THIS->name, GF_LOG_ERROR, 0, PS_MSG_DICT_SERIALIZE_FAIL,
b7d4d7
                        "failed to serialize reply dict");
b7d4d7
                 errno = -ret;
b7d4d7
-- 
b7d4d7
1.8.3.1
b7d4d7