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