diff options
author | Prasanna Kumar Kalever <prasanna.kalever@redhat.com> | 2017-01-23 14:44:08 +0530 |
---|---|---|
committer | Prasanna Kumar Kalever <prasanna.kalever@redhat.com> | 2017-01-30 19:31:50 +0530 |
commit | 57b2a8c90ae326b1d34bbdab520204a3d8585f59 (patch) | |
tree | 603a4f1093543c1eb0cf0a81433762547cc422df | |
parent | 562f4d1e1a0f8e8419b9ed56d3bbd06d296a984a (diff) |
gluster-block: code refactor
Signed-off-by: Prasanna Kumar Kalever <prasanna.kalever@redhat.com>
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | common.c | 71 | ||||
-rw-r--r-- | common.h | 21 | ||||
-rw-r--r-- | gluster-block.c | 664 | ||||
-rw-r--r-- | gluster-blockd.c | 271 |
5 files changed, 281 insertions, 750 deletions
@@ -13,10 +13,10 @@ CC = gcc CLIENT = gluster-block -CDEP = glfs-operations.o utils.o rpc/block_clnt.c rpc/block_xdr.c gluster-block.o +CDEP = glfs-operations.o utils.o common.o rpc/block_clnt.c rpc/block_xdr.c gluster-block.o SERVER = gluster-blockd -SDEP = rpc/block_svc.o rpc/block_clnt.c rpc/block_xdr.o gluster-blockd.o utils.o glfs-operations.o +SDEP = rpc/block_svc.o rpc/block_clnt.c rpc/block_xdr.o gluster-blockd.o utils.o common.o glfs-operations.o CFLAGS = -g -ggdb -Wall -lpthread LIBS := $(shell pkg-config --libs uuid glusterfs-api) diff --git a/common.c b/common.c new file mode 100644 index 0000000..aa9ad82 --- /dev/null +++ b/common.c @@ -0,0 +1,71 @@ +/* + Copyright (c) 2016 Red Hat, Inc. <http://www.redhat.com> + This file is part of gluster-block. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + + +# include "common.h" + + + +size_t +glusterBlockCreateParseSize(char *value) +{ + char *postfix; + char *tmp; + size_t sizef; + + if (!value) + return -1; + + sizef = strtod(value, &postfix); + if (sizef < 0) { + ERROR("%s", "size cannot be negative number\n"); + return -1; + } + + tmp = postfix; + if (*postfix == ' ') + tmp = tmp + 1; + + switch (*tmp) { + case 'Y': + sizef *= 1024; + /* fall through */ + case 'Z': + sizef *= 1024; + /* fall through */ + case 'E': + sizef *= 1024; + /* fall through */ + case 'P': + sizef *= 1024; + /* fall through */ + case 'T': + sizef *= 1024; + /* fall through */ + case 'G': + sizef *= 1024; + /* fall through */ + case 'M': + sizef *= 1024; + /* fall through */ + case 'K': + case 'k': + sizef *= 1024; + /* fall through */ + case 'b': + case '\0': + return sizef; + break; + default: + ERROR("%s", "You may use k/K, M, G or T suffixes for " + "kilobytes, megabytes, gigabytes and terabytes."); + return -1; + } +} diff --git a/common.h b/common.h new file mode 100644 index 0000000..46d40ee --- /dev/null +++ b/common.h @@ -0,0 +1,21 @@ +/* + Copyright (c) 2016 Red Hat, Inc. <http://www.redhat.com> + This file is part of gluster-block. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + + +# ifndef _COMMON_H +# define _COMMON_H 1 + +# include "utils.h" + + +size_t glusterBlockCreateParseSize(char *value); + + +# endif /* _COMMON_H */ diff --git a/gluster-block.c b/gluster-block.c index 632fcef..ef2ba1b 100644 --- a/gluster-block.c +++ b/gluster-block.c @@ -11,19 +11,12 @@ # define _GNU_SOURCE /* See feature_test_macros(7) */ -# include <string.h> -# include <unistd.h> # include <getopt.h> -# include <uuid/uuid.h> -# include "utils.h" -#include "rpc/block.h" -# include "glfs-operations.h" +# include "common.h" +# include "rpc/block.h" -# define UUID_BUF_SIZE 50 -# define CFG_STRING_SIZE 256 - # define LIST "list" # define CREATE "create" # define DELETE "delete" @@ -32,17 +25,6 @@ # define BLOCKHOST "block-host" # define HELP "help" -# define GLFS_PATH "/backstores/user:glfs" -# define TARGETCLI_GLFS "targetcli "GLFS_PATH -# define TARGETCLI_ISCSI "targetcli /iscsi" -# define TARGETCLI_SAVE "targetcli / saveconfig" -# define ATTRIBUTES "generate_node_acls=1 demo_mode_write_protect=0" -# define BACKEND_CFGSTR "ls | grep ' %s ' | cut -d'[' -f2 | cut -d']' -f1" -# define LUNS_LIST "ls | grep -v user:glfs | cut -d'-' -f2 | cut -d' ' -f2" - -# define IQN_PREFIX "iqn.2016-12.org.gluster-block:" - -# define MSERVER_DELIMITER "," typedef enum opterations { CREATE_CLI = 1, @@ -51,19 +33,15 @@ typedef enum opterations { DELETE_CLI = 4 } opterations; -typedef struct blockServerDef { - size_t nhosts; - char **hosts; -} blockServerDef; -typedef blockServerDef *blockServerDefPtr; - -static void -gluster_block_cli_1(void *cobj, opterations opt, blockResponse **reply) +static int +gluster_block_cli_1(void *cobj, opterations opt, char **out) { CLIENT *clnt; int sockfd, len; + int ret = -1; struct sockaddr_un saun; + blockResponse *reply = NULL; if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { perror("client: socket"); @@ -85,35 +63,46 @@ gluster_block_cli_1(void *cobj, opterations opt, blockResponse **reply) clnt_pcreateerror ("localhost"); exit (1); } -switch(opt) { + switch(opt) { case CREATE_CLI: - *reply = block_create_cli_1((blockCreateCli *)cobj, clnt); - if (*reply == NULL) { + reply = block_create_cli_1((blockCreateCli *)cobj, clnt); + if (reply == NULL) { clnt_perror (clnt, "call failed gluster-block"); } break; case DELETE_CLI: - *reply = block_delete_cli_1((blockDeleteCli *)cobj, clnt); - if (*reply == NULL) { + reply = block_delete_cli_1((blockDeleteCli *)cobj, clnt); + if (reply == NULL) { clnt_perror (clnt, "call failed gluster-block"); } break; case INFO_CLI: - *reply = block_info_cli_1((blockInfoCli *)cobj, clnt); - if (*reply == NULL) { + reply = block_info_cli_1((blockInfoCli *)cobj, clnt); + if (reply == NULL) { clnt_perror (clnt, "call failed gluster-block"); } break; case LIST_CLI: - *reply = block_list_cli_1((blockListCli *)cobj, clnt); - if (*reply == NULL) { + reply = block_list_cli_1((blockListCli *)cobj, clnt); + if (reply == NULL) { clnt_perror (clnt, "call failed gluster-block"); } break; -} - printf("%s\n", (*reply)->out); + } + + if (GB_STRDUP(*out, reply->out) < 0) { + ret = -1; + goto out; + } + ret = reply->exit; + +out: + if (!clnt_freeres(clnt, (xdrproc_t) xdr_blockResponse, (char *) reply)) + clnt_perror (clnt, "clnt_freeres failed"); clnt_destroy (clnt); + + return ret; } @@ -139,183 +128,13 @@ glusterBlockHelp(void) } -void -blockServerDefFree(blockServerDefPtr blkServers) -{ - size_t i; - - if (!blkServers) - return; - - for (i = 0; i < blkServers->nhosts; i++) - GB_FREE(blkServers->hosts[i]); - GB_FREE(blkServers->hosts); - GB_FREE(blkServers); -} - -/* -static void -glusterBlockDefFree(glusterBlockDefPtr blk) -{ - if (!blk) - return; - - GB_FREE(blk->volume); - GB_FREE(blk->host); - GB_FREE(blk->filename); - GB_FREE(blk); -} -*/ - -static blockServerDefPtr -blockServerParse(char *blkServers) -{ - blockServerDefPtr list; - char *tmp = blkServers; - size_t i = 0; - - if (!blkServers) - return NULL; - - if (GB_ALLOC(list) < 0) - return NULL; - - /* count number of servers */ - while (*tmp) { - if (*tmp == ',') - list->nhosts++; - tmp++; - } - list->nhosts++; - tmp = blkServers; /* reset addr */ - - - if (GB_ALLOC_N(list->hosts, list->nhosts) < 0) - goto fail; - - for (i = 0; tmp != NULL; i++) { - if (GB_STRDUP(list->hosts[i], strsep(&tmp, MSERVER_DELIMITER)) < 0) - goto fail; - } - - return list; - - fail: - blockServerDefFree(list); - return NULL; -} - - -static size_t -glusterBlockCreateParseSize(char *value) -{ - char *postfix; - char *tmp; - size_t sizef; - - if (!value) - return -1; - - sizef = strtod(value, &postfix); - if (sizef < 0) { - ERROR("%s", "size cannot be negative number\n"); - return -1; - } - - tmp = postfix; - if (*postfix == ' ') - tmp = tmp + 1; - - switch (*tmp) { - case 'Y': - sizef *= 1024; - /* fall through */ - case 'Z': - sizef *= 1024; - /* fall through */ - case 'E': - sizef *= 1024; - /* fall through */ - case 'P': - sizef *= 1024; - /* fall through */ - case 'T': - sizef *= 1024; - /* fall through */ - case 'G': - sizef *= 1024; - /* fall through */ - case 'M': - sizef *= 1024; - /* fall through */ - case 'K': - case 'k': - sizef *= 1024; - /* fall through */ - case 'b': - case '\0': - return sizef; - break; - default: - ERROR("%s", "You may use k/K, M, G or T suffixes for " - "kilobytes, megabytes, gigabytes and terabytes."); - return -1; - } -} - -/* -static char * -glusterBlockListGetHumanReadableSize(size_t bytes) -{ - char *size; - char *types[] = {"Byte(s)", "KiB", "MiB", "GiB", - "TiB", "PiB", "EiB", "ZiB", "YiB"}; - size_t i; - - if (bytes < 1024) { - asprintf(&size, "%zu %s", bytes, *(types)); - return size; - } - - for (i = 1; i < 8; i++) { - bytes /= 1024; - if (bytes < 1024) { - asprintf(&size, "%zu %s ", bytes, *(types + i)); - return size; - } - } - - return NULL; -} -*/ - static int glusterBlockCreate(int count, char **options, char *name) { int c; int ret = 0; -// char *cmd = NULL; -// char *exec = NULL; -// char *iqn = NULL; -// char *blkServers = NULL; -// blockServerDefPtr list; -// uuid_t out; -// glusterBlockDefPtr blk; - blockResponse *reply; + char *out = NULL; static blockCreateCli cobj; -// size_t i; - -/* - if (GB_ALLOC(&cobj) < 0) - return -1; - if (GB_ALLOC(blk) < 0) - return -1; - - blk->filename = CALLOC(UUID_BUF_SIZE); - - uuid_generate(out); - uuid_unparse(out, blk->filename); -*/ if (!name) { ERROR("%s", "Insufficient arguments supplied for" @@ -346,8 +165,8 @@ glusterBlockCreate(int count, char **options, char *name) switch (c) { case 'b': - //blkServers = optarg; - GB_STRDUP(cobj.block_hosts, optarg); + if (GB_STRDUP(cobj.block_hosts, optarg) < 0) + return -1; break; case 'v': @@ -379,13 +198,7 @@ glusterBlockCreate(int count, char **options, char *name) break; } } -/* - if (blkServers) { - list = blockServerParse(blkServers); - } else { - list = blockServerParse("localhost"); - } -*/ + /* Print any remaining command line arguments (not options). */ if (optind < count) { ERROR("%s", "non-option ARGV-elements: "); @@ -404,292 +217,36 @@ glusterBlockCreate(int count, char **options, char *name) goto out; } - gluster_block_cli_1(&cobj, CREATE_CLI, &reply); - - + ret = gluster_block_cli_1(&cobj, CREATE_CLI, &out); - - -/* - ret = glusterBlockCreateEntry(blk); - if (ret) { - ERROR("%s volume: %s host: %s", - FAILED_CREATING_FILE, blk->volume, blk->host); - goto out; - } - - if (asprintf(&cmd, "%s %s %s %zu %s@%s/%s %s", TARGETCLI_GLFS, - CREATE, name, blk->size, blk->volume, blk->host, - blk->filename, blk->filename) < 0) - goto out; - - for (i = 0; i < list->nhosts; i++) { - MSG("[OnHost: %s]", list->hosts[i]); - gluster_block_1(list->hosts[i], cmd, &reply); - if (!reply || reply->exit) { - ERROR("%s on host: %s", - FAILED_CREATING_BACKEND, list->hosts[i]); - ret = -1; - goto out; - } - MSG("%s", reply->out); - - asprintf(&iqn, "%s%s", IQN_PREFIX, blk->filename); - asprintf(&exec, "%s %s %s", TARGETCLI_ISCSI, CREATE, iqn); - gluster_block_1(list->hosts[i], exec, &reply); - if (!reply || reply->exit) { - ERROR("%s on host: %s", - FAILED_CREATING_IQN, list->hosts[i]); - ret = -1; - goto out; - } - MSG("%s", reply->out); - GB_FREE(exec); - - asprintf(&exec, "%s/%s/tpg1/luns %s %s/%s", - TARGETCLI_ISCSI, iqn, CREATE, GLFS_PATH, name); - gluster_block_1(list->hosts[i], exec, &reply); - if (!reply || reply->exit) { - ERROR("%s on host: %s", - FAILED_CREATING_LUN, list->hosts[i]); - ret = -1; - goto out; - } - MSG("%s", reply->out); - GB_FREE(exec); - - asprintf(&exec, "%s/%s/tpg1 set attribute %s", - TARGETCLI_ISCSI, iqn, ATTRIBUTES); - gluster_block_1(list->hosts[i], exec, &reply); - if (!reply || reply->exit) { - ERROR("%s on host: %s", - FAILED_SETTING_ATTRIBUTES, list->hosts[i]); - ret = -1; - goto out; - } - MSG("%s", reply->out); - GB_FREE(exec); - GB_FREE(iqn); - - gluster_block_1(list->hosts[i], TARGETCLI_SAVE, &reply); - if (!reply || reply->exit) { - ERROR("%s on host: %s", - FAILED_SAVEING_CONFIG, list->hosts[i]); - ret = -1; - goto out; - } - MSG("%s", reply->out); - putchar('\n'); - } - - out: - GB_FREE(cmd); - GB_FREE(exec); - GB_FREE(iqn); - glusterBlockDefFree(blk); - blockServerDefFree(list); -*/ + MSG("%s", out); out: - return reply->exit; -} - -/* -static int -glusterBlockParseCfgStringToDef(char* cfgstring, - glusterBlockDefPtr blk) -{ - int ret = 0; - char *p, *sep; - - // part before '@' is the volume name - p = cfgstring; - sep = strchr(p, '@'); - if (!sep) { - ret = -1; - goto fail; - } - - *sep = '\0'; - if (GB_STRDUP(blk->volume, p) < 0) { - ret = -1; - goto fail; - } - - // part between '@' and '/' is the server name - p = sep + 1; - sep = strchr(p, '/'); - if (!sep) { - ret = -1; - goto fail; - } - - *sep = '\0'; - if (GB_STRDUP(blk->host, p) < 0) { - ret = -1; - goto fail; - } - - // part between '/' and '(' is the filename - p = sep + 1; - sep = strchr(p, '('); - if (!sep) { - ret = -1; - goto fail; - } - - *(sep - 1) = '\0'; // discard extra space at end of filename - if (GB_STRDUP(blk->filename, p) < 0) { - ret = -1; - goto fail; - } - - // part between '(' and ')' is the size - p = sep + 1; - sep = strchr(p, ')'); - if (!sep) { - ret = -1; - goto fail; - } - - *sep = '\0'; - blk->size = glusterBlockCreateParseSize(p); - if (blk->size < 0) { - ERROR("%s", "failed while parsing size"); - ret = -1; - goto fail; - } - - - // part between ')' and '\n' is the status - p = sep + 1; - sep = strchr(p, '\n'); - if (!sep) { - ret = -1; - goto fail; - } - - *sep = '\0'; - if (!strcmp(p, " activated")) - blk->status = true; - - return 0; - - fail: - glusterBlockDefFree(blk); + GB_FREE(cobj.block_hosts); + GB_FREE(out); return ret; } -*/ -/* -static char * -getCfgstring(char* name, char *blkServer) -{ - char *cmd; - char *exec; - char *buf = NULL; - blockResponse *reply = NULL; - asprintf(&cmd, "%s %s", TARGETCLI_GLFS, BACKEND_CFGSTR); - asprintf(&exec, cmd, name); - - //gluster_block_1(blkServer, exec, &reply); - if (!reply || reply->exit) { - ERROR("%s on host: %s", - FAILED_GATHERING_CFGSTR, blkServer); - } - - GB_FREE(cmd); - GB_FREE(exec); - - if(reply->out) - buf = strdup(reply->out); - - return buf; -} -*/ - -/* TODO: need to implement sessions [ list | detail ] [sid] */ static int glusterBlockList(char *blkServers) { - /*size_t i; - int ret; - char *cmd; - char *pos; - char *size; - char *cfgstring = NULL; - glusterBlockDefPtr blk = NULL;*/ - static blockListCli cobj; - blockResponse *reply = NULL; - - GB_STRDUP(cobj.block_hosts, blkServers); + char *out = NULL; + int ret = -1; - gluster_block_cli_1(&cobj, LIST_CLI, &reply); - - /* - MSG("%s", "BlockName Volname Host Size Status"); - - asprintf(&cmd, "%s %s", TARGETCLI_GLFS, LUNS_LIST); - - for (i = 0; i < blkServers->nhosts; i++) { - MSG("[OnHost: %s]", blkServers->hosts[i]); - //gluster_block_1(blkServers->hosts[i], cmd, &reply); - if (!reply || reply->exit) { - ERROR("%s on host: %s", - FAILED_LIST_BACKEND, blkServers->hosts[i]); - ret = -1; - goto fail; - } - - pos = strtok (reply->out, "\n"); - while (pos != NULL) { - - cfgstring = getCfgstring(pos, blkServers->hosts[i]); - if (!cfgstring) { - ERROR("%s", "failed while gathering CfgString"); - ret = -1; - goto fail; - } - - if (GB_ALLOC(blk) < 0) - goto fail; - - ret = glusterBlockParseCfgStringToDef(cfgstring, blk); - if (ret) { - ERROR("%s", "failed while parsing CfgString to glusterBlockDef"); - goto fail; - } - - fprintf(stdout, " %s %s %s %s %s\n", - pos, blk->volume, blk->host, - size = glusterBlockListGetHumanReadableSize(blk->size), - blk->status ? "Online" : "Offline"); - - GB_FREE(cfgstring); - glusterBlockDefFree(blk); - pos = strtok (NULL, "\n"); - } - - putchar('\n'); - - GB_FREE(size); + if (GB_STRDUP(cobj.block_hosts, blkServers) < 0) { + return -1; } - GB_FREE(cmd); - return 0; - - fail: - glusterBlockDefFree(blk); - GB_FREE(cfgstring); + ret = gluster_block_cli_1(&cobj, LIST_CLI, &out); - GB_FREE(cmd); + MSG("%s", out); + GB_FREE(cobj.block_hosts); + GB_FREE(out); - return -1; - */ - return 0; + return ret; } @@ -697,84 +254,21 @@ static int glusterBlockDelete(char* name, char *blkServers) { static blockDeleteCli cobj; - blockResponse *reply; + char *out = NULL; + int ret = -1; strcpy(cobj.block_name, name); - GB_STRDUP(cobj.block_hosts, blkServers); - - gluster_block_cli_1(&cobj, DELETE_CLI, &reply); - - /* - - asprintf(&cmd, "%s %s %s", TARGETCLI_GLFS, DELETE, name); - - for (i = 0; i < blkServers->nhosts; i++) { - MSG("[OnHost: %s]", blkServers->hosts[i]); - if (cfgstring) - GB_FREE(cfgstring); - cfgstring = getCfgstring(name, blkServers->hosts[i]); - if (!cfgstring) { - ERROR("%s", "failed while gathering CfgString"); - ret = -1; - goto fail; - } - - //gluster_block_1(blkServers->hosts[i], cmd, &reply); - if (!reply || reply->exit) { - ERROR("%s on host: %s", - FAILED_DELETING_BACKEND, blkServers->hosts[i]); - ret = -1; - goto fail; - } - MSG("%s", reply->out); - if (!blk) { - if (GB_ALLOC(blk) < 0) - goto fail; - - ret = glusterBlockParseCfgStringToDef(cfgstring, blk); - if (ret) { - ERROR("%s", "failed while parsing CfgString to glusterBlockDef"); - goto fail; - } - } - - asprintf(&iqn, "%s%s", IQN_PREFIX, blk->filename); - asprintf(&exec, "%s %s %s", TARGETCLI_ISCSI, DELETE, iqn); - //gluster_block_1(blkServers->hosts[i], exec, &reply); - if (!reply || reply->exit) { - ERROR("%s on host: %s", - FAILED_DELETING_IQN, blkServers->hosts[i]); - ret = -1; - goto fail; - } - MSG("%s", reply->out); - GB_FREE(exec); - GB_FREE(iqn); - - //gluster_block_1(blkServers->hosts[i], TARGETCLI_SAVE, &reply); - if (!reply || reply->exit) { - ERROR("%s on host: %s", - FAILED_SAVEING_CONFIG, blkServers->hosts[i]); - ret = -1; - goto fail; - } - MSG("%s", reply->out); - putchar('\n'); + if (GB_STRDUP(cobj.block_hosts, blkServers) < 0) { + return -1; } - //ret = glusterBlockDeleteEntry(blk); - if (ret) { - ERROR("%s volume: %s host: %s", - FAILED_DELETING_FILE, blk->volume, blk->host); - } + ret = gluster_block_cli_1(&cobj, DELETE_CLI, &out); - fail: - glusterBlockDefFree(blk); - GB_FREE(cfgstring); - GB_FREE(exec); - GB_FREE(cmd); -*/ - return reply->exit; + MSG("%s", out); + GB_FREE(cobj.block_hosts); + GB_FREE(out); + + return ret; } @@ -782,32 +276,21 @@ static int glusterBlockInfo(char* name, char *blkServers) { static blockInfoCli cobj; - blockResponse *reply; + char *out = NULL; + int ret = -1; strcpy(cobj.block_name, name); - GB_STRDUP(cobj.block_hosts, blkServers); - - gluster_block_cli_1(&cobj, INFO_CLI, &reply); + if (GB_STRDUP(cobj.block_hosts, blkServers) < 0) { + return -1; + } - /*asprintf(&cmd, "%s/%s %s", TARGETCLI_GLFS, name, INFO); + ret = gluster_block_cli_1(&cobj, INFO_CLI, &out); - for (i = 0; i < blkServers->nhosts; i++) { - MSG("[OnHost: %s]", blkServers->hosts[i]); - gluster_block_1(blkServers->hosts[i], cmd, &reply); - if (!reply || reply->exit) { - ret = -1; - ERROR("%s on host: %s", - FAILED_GATHERING_INFO, blkServers->hosts[i]); - goto fail; - } - MSG("%s", reply->out); - putchar('\n'); - } + MSG("%s", out); + GB_FREE(cobj.block_hosts); + GB_FREE(out); - fail: - GB_FREE(cmd); -*/ - return 0; + return ret; } @@ -819,8 +302,6 @@ glusterBlockParseArgs(int count, char **options) int optFlag = 0; char *block = NULL; char *blkServers = NULL; - //blockServerDefPtr list = NULL; - char *liststr; while (1) { static const struct option long_options[] = { @@ -885,27 +366,19 @@ glusterBlockParseArgs(int count, char **options) } opt: - if (blkServers) { - //list = blockServerParse(blkServers); - liststr = blkServers; - } else { - //list = blockServerParse("localhost"); - liststr = "localhost"; - } - switch (optFlag) { case 'l': - ret = glusterBlockList(liststr); + ret = glusterBlockList(blkServers); if (ret) ERROR("%s", FAILED_LIST); break; case 'i': - ret = glusterBlockInfo(block, liststr); + ret = glusterBlockInfo(block, blkServers); if (ret) ERROR("%s", FAILED_INFO); break; case 'd': - ret = glusterBlockDelete(block, liststr); + ret = glusterBlockDelete(block, blkServers); if (ret) ERROR("%s", FAILED_DELETE); break; @@ -920,7 +393,6 @@ glusterBlockParseArgs(int count, char **options) MSG("Hint: %s --help", options[0]); } - //blockServerDefFree(list); return ret; } diff --git a/gluster-blockd.c b/gluster-blockd.c index 238f249..8cca89b 100644 --- a/gluster-blockd.c +++ b/gluster-blockd.c @@ -1,35 +1,49 @@ -#define _GNU_SOURCE /* See feature_test_macros(7) */ -#include <stdio.h> -#include <netdb.h> -#include <sys/socket.h> +/* + Copyright (c) 2016 Red Hat, Inc. <http://www.redhat.com> + This file is part of gluster-block. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +# define _GNU_SOURCE /* See feature_test_macros(7) */ + +# include <stdio.h> +# include <netdb.h> +# include <sys/socket.h> # include <uuid/uuid.h> -#include "rpc/block.h" -#include "utils.h" -#include "glfs-operations.h" -# define UUID_BUF_SIZE 256 +# include "rpc/block.h" +# include "common.h" +# include "glfs-operations.h" + -# define CFG_STRING_SIZE 256 +# define UUID_BUF_SIZE 256 -# define LIST "list" -# define CREATE "create" -# define DELETE "delete" -# define INFO "info" -# define MODIFY "modify" -# define BLOCKHOST "block-host" -# define HELP "help" +# define CREATE "create" +# define LIST "list" +# define INFO "info" +# define DELETE "delete" -# define GLFS_PATH "/backstores/user:glfs" -# define TARGETCLI_GLFS "targetcli "GLFS_PATH -# define TARGETCLI_ISCSI "targetcli /iscsi" -# define TARGETCLI_SAVE "targetcli / saveconfig" -# define ATTRIBUTES "generate_node_acls=1 demo_mode_write_protect=0" -# define BACKEND_CFGSTR "ls | grep ' %s ' | cut -d'[' -f2 | cut -d']' -f1" -# define LUNS_LIST "ls | grep -v user:glfs | cut -d'-' -f2 | cut -d' ' -f2" +# define GLFS_PATH "/backstores/user:glfs" +# define TARGETCLI_GLFS "targetcli "GLFS_PATH +# define TARGETCLI_ISCSI "targetcli /iscsi" +# define TARGETCLI_SAVE "targetcli / saveconfig" +# define ATTRIBUTES "generate_node_acls=1 demo_mode_write_protect=0" +# define BACKEND_CFGSTR "ls | grep ' %s ' | cut -d'[' -f2 | cut -d']' -f1" +# define LUNS_LIST "ls | grep -v user:glfs | cut -d'-' -f2 | cut -d' ' -f2" +# define IQN_PREFIX "iqn.2016-12.org.gluster-block:" -# define IQN_PREFIX "iqn.2016-12.org.gluster-block:" +# define MSERVER_DELIMITER "," -# define MSERVER_DELIMITER "," + +typedef struct blockServerDef { + size_t nhosts; + char **hosts; +} blockServerDef; +typedef blockServerDef *blockServerDefPtr; typedef enum opterations { CREATE_SRV = 1, @@ -39,11 +53,6 @@ typedef enum opterations { EXEC_SRV = 5 } opterations; -typedef struct blockServerDef { - size_t nhosts; - char **hosts; -} blockServerDef; -typedef blockServerDef *blockServerDefPtr; static void gluster_block_1(char *host, void *cobj, opterations opt, blockResponse **reply) @@ -57,6 +66,7 @@ gluster_block_1(char *host, void *cobj, opterations opt, blockResponse **reply) perror("gluster-blockd: socket"); exit(1); } + server = gethostbyname(host); if (server == NULL) { fprintf(stderr,"ERROR, no such host\n"); @@ -80,31 +90,37 @@ gluster_block_1(char *host, void *cobj, opterations opt, blockResponse **reply) } switch(opt) { - case CREATE_SRV: - *reply = block_create_1((blockCreate *)cobj, clnt); - if (*reply == NULL) { - clnt_perror (clnt, "call failed gluster-block"); - } - break; - case DELETE_SRV: - *reply = block_delete_1((blockDelete *)cobj, clnt); - if (*reply == NULL) { - clnt_perror (clnt, "call failed gluster-block"); - } - break; - case INFO_SRV: - case LIST_SRV: - break; - case EXEC_SRV: - *reply = block_exec_1((char **)&cobj, clnt); - if (*reply == NULL) { - clnt_perror (clnt, "call failed gluster-block"); - } - break; + case CREATE_SRV: + *reply = block_create_1((blockCreate *)cobj, clnt); + if (*reply == NULL) { + clnt_perror (clnt, "call failed gluster-block"); + } + break; + case DELETE_SRV: + *reply = block_delete_1((blockDelete *)cobj, clnt); + if (*reply == NULL) { + clnt_perror (clnt, "call failed gluster-block"); + } + break; + case INFO_SRV: + case LIST_SRV: + break; + case EXEC_SRV: + *reply = block_exec_1((char **)&cobj, clnt); + if (*reply == NULL) { + clnt_perror (clnt, "call failed gluster-block"); + } + break; } + +/* + if (!clnt_freeres(clnt, (xdrproc_t) xdr_blockResponse, (char *) reply)) + clnt_perror (clnt, "clnt_freeres failed"); +*/ clnt_destroy (clnt); } + void blockServerDefFree(blockServerDefPtr blkServers) { @@ -119,6 +135,7 @@ blockServerDefFree(blockServerDefPtr blkServers) GB_FREE(blkServers); } + static blockServerDefPtr blockServerParse(char *blkServers) { @@ -126,12 +143,12 @@ blockServerParse(char *blkServers) char *tmp = blkServers; size_t i = 0; - if (!blkServers) - blkServers = "localhost"; - if (GB_ALLOC(list) < 0) return NULL; + if (!blkServers) + blkServers = "localhost"; + /* count number of servers */ while (*tmp) { if (*tmp == ',') @@ -157,6 +174,7 @@ fail: return NULL; } + static char * getCfgstring(char* name, char *blkServer) { @@ -177,12 +195,15 @@ getCfgstring(char* name, char *blkServer) GB_FREE(cmd); GB_FREE(exec); - if(reply->out) - buf = strdup(reply->out); + if(reply->out) { + if (GB_STRDUP(buf, reply->out) < 0) + return NULL; + } return buf; } + blockResponse * block_create_cli_1_svc(blockCreateCli *blk, struct svc_req *rqstp) { @@ -228,66 +249,14 @@ block_create_cli_1_svc(blockCreateCli *blk, struct svc_req *rqstp) } strcpy(reply->out, savereply); + out: + blockServerDefFree(list); + GB_FREE(cobj); + return reply; } -static size_t -glusterBlockCreateParseSize(char *value) -{ - char *postfix; - char *tmp; - size_t sizef; - - if (!value) - return -1; - - sizef = strtod(value, &postfix); - if (sizef < 0) { - ERROR("%s", "size cannot be negative number\n"); - return -1; - } - - tmp = postfix; - if (*postfix == ' ') - tmp = tmp + 1; - - switch (*tmp) { - case 'Y': - sizef *= 1024; - /* fall through */ - case 'Z': - sizef *= 1024; - /* fall through */ - case 'E': - sizef *= 1024; - /* fall through */ - case 'P': - sizef *= 1024; - /* fall through */ - case 'T': - sizef *= 1024; - /* fall through */ - case 'G': - sizef *= 1024; - /* fall through */ - case 'M': - sizef *= 1024; - /* fall through */ - case 'K': - case 'k': - sizef *= 1024; - /* fall through */ - case 'b': - case '\0': - return sizef; - break; - default: - ERROR("%s", "You may use k/K, M, G or T suffixes for " - "kilobytes, megabytes, gigabytes and terabytes."); - return -1; - } -} static int glusterBlockParseCfgStringToDef(char* cfgstring, @@ -305,11 +274,6 @@ glusterBlockParseCfgStringToDef(char* cfgstring, } *sep = '\0'; - /*if (GB_STRDUP(blk->volume, p) < 0) { - ret = -1; - goto fail; - } - */ strcpy(blk->volume, p); /* part between '@' and '/' is the server name */ @@ -321,11 +285,6 @@ glusterBlockParseCfgStringToDef(char* cfgstring, } *sep = '\0'; - /*if (GB_STRDUP(blk->volfileserver, p) < 0) { - ret = -1; - goto fail; - } - */ strcpy(blk->volfileserver, p); /* part between '/' and '(' is the filename */ @@ -337,11 +296,6 @@ glusterBlockParseCfgStringToDef(char* cfgstring, } *(sep - 1) = '\0'; /* discard extra space at end of filename */ - /*if (GB_STRDUP(blk->gbid, p) < 0) { - ret = -1; - goto fail; - } - */ strcpy(blk->gbid, p); /* part between '(' and ')' is the size */ @@ -360,28 +314,11 @@ glusterBlockParseCfgStringToDef(char* cfgstring, goto fail; } - - /* part between ')' and '\n' is the status */ -/* - p = sep + 1; - sep = strchr(p, '\n'); - if (!sep) { - ret = -1; - goto fail; - } - - *sep = '\0'; - if (!strcmp(p, " activated")) - blk->status = true; -*/ - return 0; - fail: -// glusterBlockDefFree(blk); - return ret; } + blockResponse * block_create_1_svc(blockCreate *blk, struct svc_req *rqstp) { @@ -410,11 +347,11 @@ block_create_1_svc(blockCreate *blk, struct svc_req *rqstp) asprintf(&exec, "%s && %s && %s && %s && %s", backstore, iqn, lun, attr, TARGETCLI_SAVE); if(GB_ALLOC(obj) < 0) - return NULL; + goto out; if (GB_ALLOC_N(obj->out, 4096) < 0) { GB_FREE(obj); - return NULL; + goto out; } fp = popen(exec, "r"); @@ -428,9 +365,18 @@ block_create_1_svc(blockCreate *blk, struct svc_req *rqstp) obj->exit = WEXITSTATUS(pclose(fp)); } + out: + GB_FREE(exec); + GB_FREE(attr); + GB_FREE(lun); + GB_FREE(iqn); + GB_FREE(backstore); + return obj; + } + blockResponse * block_delete_cli_1_svc(blockDeleteCli *blk, struct svc_req *rqstp) { @@ -483,7 +429,13 @@ block_delete_cli_1_svc(blockDeleteCli *blk, struct svc_req *rqstp) ERROR("%s volume: %s host: %s", FAILED_DELETING_FILE, blkcfg->volume, blkcfg->volfileserver); } + out: + blockServerDefFree(list); + GB_FREE(cfgstring); + GB_FREE(blkcfg); + GB_FREE(cobj); + return reply; } @@ -492,8 +444,8 @@ blockResponse * block_delete_1_svc(blockDelete *blk, struct svc_req *rqstp) { FILE *fp; - char *backstore = NULL; char *iqn = NULL; + char *backstore = NULL; char *exec = NULL; blockResponse *obj = NULL; @@ -505,11 +457,11 @@ block_delete_1_svc(blockDelete *blk, struct svc_req *rqstp) asprintf(&exec, "%s && %s", backstore, iqn); if(GB_ALLOC(obj) < 0) - return NULL; + goto out; if (GB_ALLOC_N(obj->out, 4096) < 0) { GB_FREE(obj); - return NULL; + goto out; } fp = popen(exec, "r"); @@ -523,9 +475,15 @@ block_delete_1_svc(blockDelete *blk, struct svc_req *rqstp) obj->exit = WEXITSTATUS(pclose(fp)); } +out: + GB_FREE(exec); + GB_FREE(backstore); + GB_FREE(iqn); + return obj; } + blockResponse * block_exec_1_svc(char **cmd, struct svc_req *rqstp) { @@ -554,6 +512,7 @@ block_exec_1_svc(char **cmd, struct svc_req *rqstp) return obj; } + blockResponse * block_list_cli_1_svc(blockListCli *blk, struct svc_req *rqstp) { @@ -579,10 +538,15 @@ block_list_cli_1_svc(blockListCli *blk, struct svc_req *rqstp) } strcpy(reply->out, savereply); -out: + + out: + blockServerDefFree(list); + GB_FREE(cmd); + return reply; } + blockResponse * block_info_cli_1_svc(blockInfoCli *blk, struct svc_req *rqstp) { @@ -609,6 +573,9 @@ block_info_cli_1_svc(blockInfoCli *blk, struct svc_req *rqstp) } strcpy(reply->out, savereply); -out: + + out: + blockServerDefFree(list); + return reply; } |