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