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