This simplifies the collie codes a lot.

Signed-off-by: MORITA Kazutaka <[email protected]>
---
 collie/cluster.c |  63 ++++++----------------
 collie/collie.c  |  14 ++---
 collie/collie.h  |   3 +-
 collie/common.c  |  76 +++++++-------------------
 collie/debug.c   |  16 ++----
 collie/node.c    |  54 ++++++-------------
 collie/vdi.c     | 161 +++++++++++++++----------------------------------------
 7 files changed, 100 insertions(+), 287 deletions(-)

diff --git a/collie/cluster.c b/collie/cluster.c
index 083801c..af9f33b 100644
--- a/collie/cluster.c
+++ b/collie/cluster.c
@@ -41,25 +41,17 @@ static struct cluster_cmd_data {
 
 static int list_store(void)
 {
-       int fd, ret;
+       int ret;
        struct sd_req hdr;
        struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
        char buf[512] = { 0 };
 
-       fd = connect_to(sdhost, sdport);
-       if (fd < 0)
-               return EXIT_SYSFAIL;
-
        sd_init_req(&hdr, SD_OP_GET_STORE_LIST);
        hdr.data_length = 512;
 
-       ret = collie_exec_req(fd, &hdr, buf);
-       close(fd);
-
-       if (ret) {
-               fprintf(stderr, "Failed to connect\n");
+       ret = collie_exec_req(sdhost, sdport, &hdr, buf);
+       if (ret < 0)
                return EXIT_SYSFAIL;
-       }
 
        if (rsp->result != SD_RES_SUCCESS) {
                fprintf(stderr, "Restore failed: %s\n",
@@ -82,7 +74,7 @@ static int list_store(void)
 
 static int cluster_format(int argc, char **argv)
 {
-       int fd, ret;
+       int ret;
        struct sd_so_req hdr;
        struct sd_so_rsp *rsp = (struct sd_so_rsp *)&hdr;
        struct timeval tv;
@@ -90,20 +82,13 @@ static int cluster_format(int argc, char **argv)
        static DECLARE_BITMAP(vdi_inuse, SD_NR_VDIS);
        unsigned long nr;
 
-       fd = connect_to(sdhost, sdport);
-       if (fd < 0)
-               return EXIT_SYSFAIL;
-
        sd_init_req((struct sd_req *)&hdr, SD_OP_READ_VDIS);
        hdr.data_length = sizeof(vdi_inuse);
 
-       ret = collie_exec_req(fd, (struct sd_req *)&hdr, &vdi_inuse);
-       if (ret < 0) {
-               fprintf(stderr, "Failed to read VDIs from %s:%d\n",
-                       sdhost, sdport);
-               close(fd);
+       ret = collie_exec_req(sdhost, sdport, (struct sd_req *)&hdr,
+                             &vdi_inuse);
+       if (ret < 0)
                return EXIT_SYSFAIL;
-       }
 
        for (nr = 0; nr < SD_NR_VDIS; nr++)
                if (test_bit(nr, vdi_inuse))
@@ -131,13 +116,9 @@ static int cluster_format(int argc, char **argv)
        hdr.flags |= SD_FLAG_CMD_WRITE;
 
        printf("using backend %s store\n", store_name);
-       ret = collie_exec_req(fd, (struct sd_req *)&hdr, store_name);
-       close(fd);
-
-       if (ret) {
-               fprintf(stderr, "Failed to connect\n");
+       ret = collie_exec_req(sdhost, sdport, (struct sd_req *)&hdr, 
store_name);
+       if (ret < 0)
                return EXIT_SYSFAIL;
-       }
 
        if (rsp->result != SD_RES_SUCCESS) {
                fprintf(stderr, "Format failed: %s\n",
@@ -153,7 +134,7 @@ static int cluster_format(int argc, char **argv)
 
 static int cluster_info(int argc, char **argv)
 {
-       int i, fd, ret;
+       int i, ret;
        struct sd_req hdr;
        struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
        struct epoch_log *logs;
@@ -174,17 +155,11 @@ again:
                goto again;
        }
 
-       fd = connect_to(sdhost, sdport);
-       if (fd < 0)
-               goto error;
-
        sd_init_req(&hdr, SD_OP_STAT_CLUSTER);
        hdr.data_length = log_length;
 
-       ret = collie_exec_req(fd, &hdr, logs);
-       close(fd);
-
-       if (ret != 0)
+       ret = collie_exec_req(sdhost, sdport, &hdr, logs);
+       if (ret < 0)
                goto error;
 
        if (!raw_output)
@@ -282,27 +257,19 @@ static void print_list(void *buf, unsigned len)
 
 static int list_snap(void)
 {
-       int fd, ret = EXIT_SYSFAIL;
+       int ret = EXIT_SYSFAIL;
        struct sd_req hdr;
        struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
        void *buf;
 
        buf = xmalloc(SD_DATA_OBJ_SIZE);
 
-       fd = connect_to(sdhost, sdport);
-       if (fd < 0)
-               goto out;
-
        sd_init_req(&hdr, SD_OP_GET_SNAP_FILE);
        hdr.data_length = SD_DATA_OBJ_SIZE;
 
-       ret = collie_exec_req(fd, &hdr, buf);
-       close(fd);
-
-       if (ret) {
-               fprintf(stderr, "Failed to connect\n");
+       ret = collie_exec_req(sdhost, sdport, &hdr, buf);
+       if (ret < 0)
                goto out;
-       }
 
        if (rsp->result != SD_RES_SUCCESS) {
                fprintf(stderr, "Listing snapshots failed: %s\n",
diff --git a/collie/collie.c b/collie/collie.c
index 045d175..e798207 100644
--- a/collie/collie.c
+++ b/collie/collie.c
@@ -47,17 +47,13 @@ unsigned master_idx;
 
 int update_node_list(int max_nodes, uint32_t epoch)
 {
-       int fd, ret;
+       int ret;
        unsigned int size;
        char *buf = NULL;
        struct sd_node *ent;
        struct sd_node_req hdr;
        struct sd_node_rsp *rsp = (struct sd_node_rsp *)&hdr;
 
-       fd = connect_to(sdhost, sdport);
-       if (fd < 0)
-               return -1;
-
        size = sizeof(*ent) * max_nodes;
        buf = xzalloc(size);
        sd_init_req((struct sd_req *)&hdr, SD_OP_GET_NODE_LIST);
@@ -65,11 +61,9 @@ int update_node_list(int max_nodes, uint32_t epoch)
 
        hdr.data_length = size;
 
-       ret = collie_exec_req(fd, (struct sd_req *)&hdr, buf);
-       if (ret) {
-               ret = -1;
+       ret = collie_exec_req(sdhost, sdport, (struct sd_req *)&hdr, buf);
+       if (ret < 0)
                goto out;
-       }
 
        if (rsp->result != SD_RES_SUCCESS) {
                fprintf(stderr, "Failed to update node list: %s\n",
@@ -98,8 +92,6 @@ int update_node_list(int max_nodes, uint32_t epoch)
 out:
        if (buf)
                free(buf);
-       if (fd >= 0)
-               close(fd);
 
        return ret;
 }
diff --git a/collie/collie.h b/collie/collie.h
index 311ad97..5973748 100644
--- a/collie/collie.h
+++ b/collie/collie.h
@@ -68,9 +68,8 @@ int sd_read_object(uint64_t oid, void *data, unsigned int 
datalen,
 int sd_write_object(uint64_t oid, uint64_t cow_oid, void *data,
                    unsigned int datalen, uint64_t offset, uint32_t flags,
                    int copies, bool create, bool direct);
+int collie_exec_req(const char *host, int port, struct sd_req *hdr, void 
*data);
 int send_light_req(struct sd_req *hdr, const char *host, int port);
-int send_light_req_get_response(struct sd_req *hdr, const char *host, int 
port);
-int collie_exec_req(int sockfd, struct sd_req *hdr, void *data);
 int do_generic_subcommand(struct subcommand *sub, int argc, char **argv);
 int update_node_list(int max_nodes, uint32_t epoch);
 void confirm(const char *message);
diff --git a/collie/common.c b/collie/common.c
index 3253de2..0642ee0 100644
--- a/collie/common.c
+++ b/collie/common.c
@@ -47,13 +47,7 @@ int sd_read_object(uint64_t oid, void *data, unsigned int 
datalen,
 {
        struct sd_req hdr;
        struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
-       int fd, ret;
-
-       fd = connect_to(sdhost, sdport);
-       if (fd < 0) {
-               fprintf(stderr, "Failed to connect\n");
-               return SD_RES_EIO;
-       }
+       int ret;
 
        sd_init_req(&hdr, SD_OP_READ_OBJ);
        hdr.data_length = datalen;
@@ -63,10 +57,8 @@ int sd_read_object(uint64_t oid, void *data, unsigned int 
datalen,
        if (direct)
                hdr.flags |= SD_FLAG_CMD_DIRECT;
 
-       ret = collie_exec_req(fd, &hdr, data);
-       close(fd);
-
-       if (ret) {
+       ret = collie_exec_req(sdhost, sdport, &hdr, data);
+       if (ret < 0) {
                fprintf(stderr, "Failed to read object %" PRIx64 "\n", oid);
                return SD_RES_EIO;
        }
@@ -88,13 +80,7 @@ int sd_write_object(uint64_t oid, uint64_t cow_oid, void 
*data,
 {
        struct sd_req hdr;
        struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
-       int fd, ret;
-
-       fd = connect_to(sdhost, sdport);
-       if (fd < 0) {
-               fprintf(stderr, "Failed to connect\n");
-               return SD_RES_EIO;
-       }
+       int ret;
 
        if (create)
                sd_init_req(&hdr, SD_OP_CREATE_AND_WRITE_OBJ);
@@ -113,10 +99,8 @@ int sd_write_object(uint64_t oid, uint64_t cow_oid, void 
*data,
        hdr.obj.cow_oid = cow_oid;
        hdr.obj.offset = offset;
 
-       ret = collie_exec_req(fd, &hdr, data);
-       close(fd);
-
-       if (ret) {
+       ret = collie_exec_req(sdhost, sdport, &hdr, data);
+       if (ret < 0) {
                fprintf(stderr, "Failed to write object %" PRIx64 "\n", oid);
                return SD_RES_EIO;
        }
@@ -131,31 +115,19 @@ int sd_write_object(uint64_t oid, uint64_t cow_oid, void 
*data,
 
 int parse_vdi(vdi_parser_func_t func, size_t size, void *data)
 {
-       int ret, fd;
+       int ret;
        unsigned long nr;
        static struct sheepdog_inode i;
        struct sd_req req;
        static DECLARE_BITMAP(vdi_inuse, SD_NR_VDIS);
        unsigned int rlen = sizeof(vdi_inuse);
 
-       fd = connect_to(sdhost, sdport);
-       if (fd < 0) {
-               fprintf(stderr, "Failed to connect to %s:%d\n", sdhost, sdport);
-               ret = -1;
-               goto out;
-       }
-
        sd_init_req(&req, SD_OP_READ_VDIS);
        req.data_length = sizeof(vdi_inuse);
 
-       ret = collie_exec_req(fd, &req, &vdi_inuse);
-       if (ret < 0) {
-               fprintf(stderr, "Failed to read VDIs from %s:%d\n",
-                       sdhost, sdport);
-               close(fd);
+       ret = collie_exec_req(sdhost, sdport, &req, &vdi_inuse);
+       if (ret < 0)
                goto out;
-       }
-       close(fd);
 
        for (nr = 0; nr < SD_NR_VDIS; nr++) {
                uint64_t oid;
@@ -199,33 +171,32 @@ out:
        return ret;
 }
 
-int send_light_req_get_response(struct sd_req *hdr, const char *host, int port)
+int collie_exec_req(const char *host, int port, struct sd_req *hdr, void *data)
 {
        int fd, ret;
        struct sd_rsp *rsp = (struct sd_rsp *)hdr;
 
        fd = connect_to(host, port);
-       if (fd < 0)
-               return -1;
-
-       ret = collie_exec_req(fd, hdr, NULL);
-       close(fd);
-       if (ret) {
-               fprintf(stderr, "failed to connect to  %s:%d\n",
+       if (fd < 0) {
+               fprintf(stderr, "Failed to connect to %s:%d\n",
                        host, port);
                return -1;
        }
 
-       if (rsp->result != SD_RES_SUCCESS)
-               return rsp->result;
+       /* Retry hard for collie because we can't get the newest epoch */
+       ret = exec_req(fd, hdr, data, NULL, 0);
+       close(fd);
 
-       return SD_RES_SUCCESS;
+       if (ret)
+               return -1;
+
+       return rsp->result;
 }
 
 /* Light request only contains header, without body content. */
 int send_light_req(struct sd_req *hdr, const char *host, int port)
 {
-       int ret = send_light_req_get_response(hdr, host, port);
+       int ret = collie_exec_req(host, port, hdr, NULL);
 
        if (ret == -1)
                return -1;
@@ -239,13 +210,6 @@ int send_light_req(struct sd_req *hdr, const char *host, 
int port)
        return 0;
 }
 
-
-int collie_exec_req(int sockfd, struct sd_req *hdr, void *data)
-{
-       /* Retry hard for collie because we can't get the newest epoch */
-       return exec_req(sockfd, hdr, data, NULL, 0);
-}
-
 int do_generic_subcommand(struct subcommand *sub, int argc, char **argv)
 {
        int i, ret;
diff --git a/collie/debug.c b/collie/debug.c
index bbbf3a6..ace5737 100644
--- a/collie/debug.c
+++ b/collie/debug.c
@@ -74,7 +74,7 @@ static const char *tracefile = "/tmp/tracefile";
 
 static int trace_read_buffer(void)
 {
-       int fd, ret, tfd;
+       int ret, tfd;
        struct sd_req hdr;
        struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
 #define TRACE_BUF_LEN      (1024 * 1024 * 20)
@@ -86,21 +86,13 @@ static int trace_read_buffer(void)
                return EXIT_SYSFAIL;
        }
 
-       fd = connect_to(sdhost, sdport);
-       if (fd < 0)
-               return EXIT_SYSFAIL;
-
 read_buffer:
        sd_init_req(&hdr, SD_OP_TRACE_READ_BUF);
        hdr.data_length = TRACE_BUF_LEN;
 
-       ret = collie_exec_req(fd, &hdr, buf);
-
-       if (ret) {
-               fprintf(stderr, "Failed to connect\n");
-               close(fd);
+       ret = collie_exec_req(sdhost, sdport, &hdr, buf);
+       if (ret < 0)
                return EXIT_SYSFAIL;
-       }
 
        if (rsp->result == SD_RES_AGAIN)
                goto read_buffer;
@@ -108,7 +100,6 @@ read_buffer:
        if (rsp->result != SD_RES_SUCCESS) {
                fprintf(stderr, "Trace failed: %s\n",
                                sd_strerror(rsp->result));
-               close(fd);
                return EXIT_FAILURE;
        }
 
@@ -118,7 +109,6 @@ read_buffer:
                goto read_buffer;
        }
 
-       close(fd);
        free(buf);
        return EXIT_SUCCESS;
 }
diff --git a/collie/node.c b/collie/node.c
index 16f4f07..64114db 100644
--- a/collie/node.c
+++ b/collie/node.c
@@ -129,8 +129,8 @@ static int node_recovery(int argc, char **argv)
 
                sd_init_req((struct sd_req *)&req, SD_OP_STAT_RECOVERY);
 
-               ret = send_light_req_get_response((struct sd_req *)&req, host,
-                                                 sd_nodes[i].nid.port);
+               ret = collie_exec_req(host, sd_nodes[i].nid.port,
+                                     (struct sd_req *)&req, NULL);
                if (ret == SD_RES_NODE_IN_RECOVERY) {
                        addr_to_str(host, sizeof(host),
                                        sd_nodes[i].nid.addr, 
sd_nodes[i].nid.port);
@@ -146,7 +146,7 @@ static int node_recovery(int argc, char **argv)
 static int node_cache(int argc, char **argv)
 {
        char *p;
-       int fd, ret;
+       int ret;
        uint32_t cache_size;
        struct sd_req hdr;
        struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
@@ -157,21 +157,13 @@ static int node_cache(int argc, char **argv)
                return EXIT_FAILURE;
        }
 
-       fd = connect_to(sdhost, sdport);
-       if (fd < 0)
-               return EXIT_FAILURE;
-
        sd_init_req(&hdr, SD_OP_SET_CACHE_SIZE);
        hdr.flags = SD_FLAG_CMD_WRITE;
        hdr.data_length = sizeof(cache_size);
 
-       ret = collie_exec_req(fd, &hdr, (void *)&cache_size);
-       close(fd);
-
-       if (ret) {
-               fprintf(stderr, "Failed to connect\n");
+       ret = collie_exec_req(sdhost, sdport, &hdr, (void *)&cache_size);
+       if (ret < 0)
                return EXIT_FAILURE;
-       }
 
        if (rsp->result != SD_RES_SUCCESS) {
                fprintf(stderr, "specify max cache size failed: %s\n",
@@ -223,23 +215,16 @@ static int node_md_info(struct node_id *nid)
        char size_str[UINT64_DECIMAL_SIZE], used_str[UINT64_DECIMAL_SIZE];
        struct sd_req hdr;
        struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
-       int fd, ret, i;
-
-       fd = connect_to_addr(nid->addr, nid->port);
-       if (fd < 0) {
-               fprintf(stderr, "Failed to connect %d\n", fd);
-               return EXIT_FAILURE;
-       }
+       int ret, i;
+       char host[HOST_NAME_MAX];
 
        sd_init_req(&hdr, SD_OP_MD_INFO);
        hdr.data_length = sizeof(info);
 
-       ret = collie_exec_req(fd, &hdr, &info);
-       close(fd);
-       if (ret) {
-               fprintf(stderr, "Failed to connect\n");
-               return EXIT_FAILURE;
-       }
+       addr_to_str(host, sizeof(host), nid->addr, 0);
+       ret = collie_exec_req(host, nid->port, &hdr, &info);
+       if (ret < 0)
+               return EXIT_SYSFAIL;
 
        if (rsp->result != SD_RES_SUCCESS) {
                fprintf(stderr, "failed to get multi-disk infomation: %s\n",
@@ -286,13 +271,7 @@ static int do_plug_unplug(char *disks, bool plug)
 {
        struct sd_req hdr;
        struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
-       int fd, ret;
-
-       fd = connect_to(sdhost, sdport);
-       if (fd < 0) {
-               fprintf(stderr, "Failed to connect %s:%d\n", sdhost, sdport);
-               return EXIT_FAILURE;
-       }
+       int ret;
 
        if (plug)
                sd_init_req(&hdr, SD_OP_MD_PLUG);
@@ -301,12 +280,9 @@ static int do_plug_unplug(char *disks, bool plug)
        hdr.flags = SD_FLAG_CMD_WRITE;
        hdr.data_length = strlen(disks) + 1;
 
-       ret = collie_exec_req(fd, &hdr, disks);
-       close(fd);
-       if (ret) {
-               fprintf(stderr, "Failed to connect\n");
-               return EXIT_FAILURE;
-       }
+       ret = collie_exec_req(sdhost, sdport, &hdr, disks);
+       if (ret < 0)
+               return EXIT_SYSFAIL;
 
        if (rsp->result != SD_RES_SUCCESS) {
                fprintf(stderr, "Failed to execute request, look for sheep.log"
diff --git a/collie/vdi.c b/collie/vdi.c
index 5603f97..5594a51 100644
--- a/collie/vdi.c
+++ b/collie/vdi.c
@@ -293,7 +293,7 @@ static int get_data_oid(char *sheep, uint64_t oid, struct 
sd_rsp *rsp,
 static void parse_objs(uint64_t oid, obj_parser_func_t func, void *data, 
unsigned size)
 {
        char name[128];
-       int i, fd, ret, cb_ret;
+       int i, ret, cb_ret;
        char *buf;
 
        buf = xzalloc(size);
@@ -301,12 +301,6 @@ static void parse_objs(uint64_t oid, obj_parser_func_t 
func, void *data, unsigne
                struct sd_req hdr;
                struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
 
-               addr_to_str(name, sizeof(name), sd_nodes[i].nid.addr, 0);
-
-               fd = connect_to(name, sd_nodes[i].nid.port);
-               if (fd < 0)
-                       break;
-
                sd_init_req(&hdr, SD_OP_READ_PEER);
                hdr.data_length = size;
                hdr.flags = 0;
@@ -314,21 +308,19 @@ static void parse_objs(uint64_t oid, obj_parser_func_t 
func, void *data, unsigne
 
                hdr.obj.oid = oid;
 
-               ret = collie_exec_req(fd, &hdr, buf);
-               close(fd);
+               addr_to_str(name, sizeof(name), sd_nodes[i].nid.addr, 0);
+               ret = collie_exec_req(name, sd_nodes[i].nid.port, &hdr, buf);
+               if (ret < 0)
+                       continue;
 
                snprintf(name + strlen(name), sizeof(name) - strlen(name),
                         ":%d", sd_nodes[i].nid.port);
 
-               if (ret)
-                       fprintf(stderr, "Failed to connect to %s\n", name);
-               else {
-                       untrim_zero_sectors(buf, rsp->obj.offset,
-                                           rsp->data_length, size);
-                       cb_ret = func(name, oid, rsp, buf, data);
-                       if (cb_ret)
-                               break;
-               }
+               untrim_zero_sectors(buf, rsp->obj.offset,
+                                   rsp->data_length, size);
+               cb_ret = func(name, oid, rsp, buf, data);
+               if (cb_ret)
+                       break;
        }
 
        free(buf);
@@ -385,15 +377,11 @@ static int vdi_graph(int argc, char **argv)
 static int find_vdi_name(const char *vdiname, uint32_t snapid, const char *tag,
                         uint32_t *vid, int for_snapshot)
 {
-       int ret, fd;
+       int ret;
        struct sd_req hdr;
        struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
        char buf[SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN];
 
-       fd = connect_to(sdhost, sdport);
-       if (fd < 0)
-               return -1;
-
        memset(buf, 0, sizeof(buf));
        pstrcpy(buf, SD_MAX_VDI_LEN, vdiname);
        if (tag)
@@ -407,24 +395,18 @@ static int find_vdi_name(const char *vdiname, uint32_t 
snapid, const char *tag,
        hdr.flags = SD_FLAG_CMD_WRITE;
        hdr.vdi.snapid = snapid;
 
-       ret = collie_exec_req(fd, &hdr, buf);
-       if (ret) {
-               ret = -1;
-               goto out;
-       }
+       ret = collie_exec_req(sdhost, sdport, &hdr, buf);
+       if (ret < 0)
+               return -1;
 
        if (rsp->result != SD_RES_SUCCESS) {
                fprintf(stderr, "Cannot get VDI info for %s %d %s: %s\n",
                        vdiname, snapid, tag, sd_strerror(rsp->result));
-               ret = -1;
-               goto out;
+               return -1;
        }
        *vid = rsp->vdi.vdi_id;
 
-       ret = 0;
-out:
-       close(fd);
-       return ret;
+       return 0;
 }
 
 static int read_vdi_obj(const char *vdiname, int snapid, const char *tag,
@@ -466,15 +448,9 @@ static int do_vdi_create(const char *vdiname, int64_t 
vdi_size,
 {
        struct sd_req hdr;
        struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
-       int fd, ret;
+       int ret;
        char buf[SD_MAX_VDI_LEN];
 
-       fd = connect_to(sdhost, sdport);
-       if (fd < 0) {
-               fprintf(stderr, "Failed to connect\n");
-               return EXIT_SYSFAIL;
-       }
-
        memset(buf, 0, sizeof(buf));
        pstrcpy(buf, SD_MAX_VDI_LEN, vdiname);
 
@@ -487,14 +463,9 @@ static int do_vdi_create(const char *vdiname, int64_t 
vdi_size,
        hdr.vdi.vdi_size = roundup(vdi_size, 512);
        hdr.vdi.copies = nr_copies;
 
-       ret = collie_exec_req(fd, &hdr, buf);
-
-       close(fd);
-
-       if (ret) {
-               fprintf(stderr, "Failed to send a request\n");
+       ret = collie_exec_req(sdhost, sdport, &hdr, buf);
+       if (ret < 0)
                return EXIT_SYSFAIL;
-       }
 
        if (rsp->result != SD_RES_SUCCESS) {
                fprintf(stderr, "Failed to create VDI %s: %s\n", vdiname,
@@ -720,7 +691,7 @@ static int vdi_resize(int argc, char **argv)
 
 static int do_vdi_delete(const char *vdiname, int snap_id, const char 
*snap_tag)
 {
-       int fd, ret;
+       int ret;
        struct sd_req hdr;
        struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
        char data[SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN];
@@ -741,10 +712,6 @@ static int do_vdi_delete(const char *vdiname, int snap_id, 
const char *snap_tag)
                return EXIT_FAILURE;
        }
 
-       fd = connect_to(sdhost, sdport);
-       if (fd < 0)
-               return EXIT_SYSFAIL;
-
        sd_init_req(&hdr, SD_OP_DEL_VDI);
        hdr.flags = SD_FLAG_CMD_WRITE;
        hdr.data_length = sizeof(data);
@@ -754,13 +721,9 @@ static int do_vdi_delete(const char *vdiname, int snap_id, 
const char *snap_tag)
        if (snap_tag)
                pstrcpy(data + SD_MAX_VDI_LEN, SD_MAX_VDI_TAG_LEN, snap_tag);
 
-       ret = collie_exec_req(fd, &hdr, data);
-       close(fd);
-
-       if (ret) {
-               fprintf(stderr, "Failed to connect\n");
+       ret = collie_exec_req(sdhost, sdport, &hdr, data);
+       if (ret < 0)
                return EXIT_SYSFAIL;
-       }
 
        if (rsp->result != SD_RES_SUCCESS) {
                fprintf(stderr, "Failed to delete %s: %s\n", vdiname,
@@ -872,7 +835,7 @@ static int vdi_object(int argc, char **argv)
 
 static int do_track_object(uint64_t oid, uint8_t nr_copies)
 {
-       int i, j, fd, ret;
+       int i, j, ret;
        struct sd_req hdr;
        struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
        struct sd_vnode *vnodes;
@@ -884,17 +847,12 @@ static int do_track_object(uint64_t oid, uint8_t 
nr_copies)
        log_length = sd_epoch * sizeof(struct epoch_log);
        logs = xmalloc(log_length);
        vnodes = xmalloc(sizeof(*vnodes) * SD_MAX_VNODES);
-       fd = connect_to(sdhost, sdport);
-       if (fd < 0)
-               goto error;
 
        sd_init_req(&hdr, SD_OP_STAT_CLUSTER);
        hdr.data_length = log_length;
 
-       ret = collie_exec_req(fd, &hdr, logs);
-       close(fd);
-
-       if (ret != 0)
+       ret = collie_exec_req(sdhost, sdport, &hdr, logs);
+       if (ret < 0)
                goto error;
 
        if (rsp->result != SD_RES_SUCCESS) {
@@ -1008,7 +966,7 @@ static int find_vdi_attr_oid(const char *vdiname, const 
char *tag, uint32_t snap
 {
        struct sd_req hdr;
        struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
-       int fd, ret;
+       int ret;
        struct sheepdog_vdi_attr vattr;
 
        memset(&vattr, 0, sizeof(vattr));
@@ -1021,12 +979,6 @@ static int find_vdi_attr_oid(const char *vdiname, const 
char *tag, uint32_t snap
                memcpy(vattr.value, value, value_len);
        }
 
-       fd = connect_to(sdhost, sdport);
-       if (fd < 0) {
-               fprintf(stderr, "Failed to connect\n\n");
-               return SD_RES_EIO;
-       }
-
        sd_init_req(&hdr, SD_OP_GET_VDI_ATTR);
        hdr.flags = SD_FLAG_CMD_WRITE;
        hdr.data_length = SD_ATTR_OBJ_SIZE;
@@ -1039,25 +991,18 @@ static int find_vdi_attr_oid(const char *vdiname, const 
char *tag, uint32_t snap
        if (delete)
                hdr.flags |= SD_FLAG_CMD_DEL;
 
-       ret = collie_exec_req(fd, &hdr, &vattr);
-       if (ret) {
-               ret = SD_RES_EIO;
-               goto out;
-       }
+       ret = collie_exec_req(sdhost, sdport, &hdr, &vattr);
+       if (ret < 0)
+               return SD_RES_EIO;
 
-       if (rsp->result != SD_RES_SUCCESS) {
-               ret = rsp->result;
-               goto out;
-       }
+       if (rsp->result != SD_RES_SUCCESS)
+               return rsp->result;
 
        *vid = rsp->vdi.vdi_id;
        *oid = vid_to_attr_oid(rsp->vdi.vdi_id, rsp->vdi.attr_id);
        *nr_copies = rsp->vdi.copies;
 
-       ret = SD_RES_SUCCESS;
-out:
-       close(fd);
-       return ret;
+       return SD_RES_SUCCESS;
 }
 
 static int vdi_setattr(int argc, char **argv)
@@ -1361,20 +1306,12 @@ static void *read_object_from(const struct sd_vnode 
*vnode, uint64_t oid)
 {
        struct sd_req hdr;
        struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
-       int fd, ret;
+       int ret;
        char name[128];
        void *buf;
 
        buf = xmalloc(SD_DATA_OBJ_SIZE);
 
-       addr_to_str(name, sizeof(name), vnode->nid.addr, 0);
-       fd = connect_to(name, vnode->nid.port);
-       if (fd < 0) {
-               fprintf(stderr, "FATAL: failed to connect to %s:%"PRIu32"\n",
-                       name, vnode->nid.port);
-               exit(EXIT_FAILURE);
-       }
-
        sd_init_req(&hdr, SD_OP_READ_PEER);
        hdr.epoch = sd_epoch;
        hdr.flags = 0;
@@ -1382,15 +1319,13 @@ static void *read_object_from(const struct sd_vnode 
*vnode, uint64_t oid)
 
        hdr.obj.oid = oid;
 
-       ret = collie_exec_req(fd, &hdr, buf);
-       close(fd);
+       addr_to_str(name, sizeof(name), vnode->nid.addr, 0);
+       ret = collie_exec_req(name, vnode->nid.port, &hdr, buf);
 
-       if (ret) {
-               fprintf(stderr, "FATAL: failed to execute request\n");
-               exit(EXIT_FAILURE);
-       }
+       if (ret < 0)
+               exit(EXIT_SYSFAIL);
 
-       switch (rsp->result)  {
+       switch (rsp->result) {
        case SD_RES_SUCCESS:
                untrim_zero_sectors(buf, rsp->obj.offset, rsp->data_length,
                                    SD_DATA_OBJ_SIZE);
@@ -1411,17 +1346,9 @@ static void write_object_to(const struct sd_vnode 
*vnode, uint64_t oid,
 {
        struct sd_req hdr;
        struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
-       int fd, ret;
+       int ret;
        char name[128];
 
-       addr_to_str(name, sizeof(name), vnode->nid.addr, 0);
-       fd = connect_to(name, vnode->nid.port);
-       if (fd < 0) {
-               fprintf(stderr, "FATAL: failed to connect to %s:%"PRIu32"\n",
-                       name, vnode->nid.port);
-               exit(EXIT_FAILURE);
-       }
-
        if (create)
                sd_init_req(&hdr, SD_OP_CREATE_AND_WRITE_PEER);
        else
@@ -1431,13 +1358,11 @@ static void write_object_to(const struct sd_vnode 
*vnode, uint64_t oid,
        hdr.data_length = SD_DATA_OBJ_SIZE;
        hdr.obj.oid = oid;
 
-       ret = collie_exec_req(fd, &hdr, buf);
-       close(fd);
+       addr_to_str(name, sizeof(name), vnode->nid.addr, 0);
+       ret = collie_exec_req(name, vnode->nid.port, &hdr, buf);
 
-       if (ret) {
-               fprintf(stderr, "FATAL: failed to execute request\n");
-               exit(EXIT_FAILURE);
-       }
+       if (ret < 0)
+               exit(EXIT_SYSFAIL);
 
        if (rsp->result != SD_RES_SUCCESS) {
                fprintf(stderr, "FATAL: failed to write, %s\n",
-- 
1.8.1.3.566.gaa39828

-- 
sheepdog mailing list
[email protected]
http://lists.wpkg.org/mailman/listinfo/sheepdog

Reply via email to