diff options
Diffstat (limited to 'cli/src')
| -rw-r--r-- | cli/src/Makefile.am | 35 | ||||
| -rw-r--r-- | cli/src/cli-cmd-global.c | 195 | ||||
| -rw-r--r-- | cli/src/cli-cmd-misc.c | 147 | ||||
| -rw-r--r-- | cli/src/cli-cmd-parser.c | 6275 | ||||
| -rw-r--r-- | cli/src/cli-cmd-peer.c | 409 | ||||
| -rw-r--r-- | cli/src/cli-cmd-snapshot.c | 135 | ||||
| -rw-r--r-- | cli/src/cli-cmd-system.c | 624 | ||||
| -rw-r--r-- | cli/src/cli-cmd-volume.c | 3493 | ||||
| -rw-r--r-- | cli/src/cli-cmd.c | 560 | ||||
| -rw-r--r-- | cli/src/cli-cmd.h | 140 | ||||
| -rw-r--r-- | cli/src/cli-mem-types.h | 40 | ||||
| -rw-r--r-- | cli/src/cli-quotad-client.c | 146 | ||||
| -rw-r--r-- | cli/src/cli-quotad-client.h | 29 | ||||
| -rw-r--r-- | cli/src/cli-rl.c | 528 | ||||
| -rw-r--r-- | cli/src/cli-rpc-ops.c | 10949 | ||||
| -rw-r--r-- | cli/src/cli-xml-output.c | 5839 | ||||
| -rw-r--r-- | cli/src/cli.c | 1083 | ||||
| -rw-r--r-- | cli/src/cli.h | 573 | ||||
| -rw-r--r-- | cli/src/cli3_1-cops.c | 1786 | ||||
| -rw-r--r-- | cli/src/input.c | 126 | ||||
| -rw-r--r-- | cli/src/registry.c | 519 |
21 files changed, 29120 insertions, 4511 deletions
diff --git a/cli/src/Makefile.am b/cli/src/Makefile.am index fbbf44dd080..16063f27c7f 100644 --- a/cli/src/Makefile.am +++ b/cli/src/Makefile.am @@ -1,24 +1,37 @@ sbin_PROGRAMS = gluster -gluster_SOURCES = cli.c registry.c input.c cli-cmd.c cli-rl.c \ - cli-cmd-volume.c cli-cmd-peer.c cli3_1-cops.c cli-cmd-parser.c\ - cli-cmd-misc.c +gluster_SOURCES = cli.c registry.c input.c cli-cmd.c cli-rl.c cli-cmd-global.c \ + cli-cmd-volume.c cli-cmd-peer.c cli-rpc-ops.c cli-cmd-parser.c\ + cli-cmd-system.c cli-cmd-misc.c cli-xml-output.c cli-quotad-client.c cli-cmd-snapshot.c -gluster_LDADD = $(top_builddir)/libglusterfs/src/libglusterfs.la $(GF_LDADD)\ +gluster_LDADD = $(top_builddir)/libglusterfs/src/libglusterfs.la $(GF_LDADD) \ + $(top_builddir)/libglusterd/src/libglusterd.la \ $(RLLIBS) $(top_builddir)/rpc/xdr/src/libgfxdr.la \ - $(top_builddir)/rpc/rpc-lib/src/libgfrpc.la + $(top_builddir)/rpc/rpc-lib/src/libgfrpc.la \ + $(XML_LIBS) -gluster_LDFLAGS = $(GF_LDFLAGS) $(GF_GLUSTERFS_LDFLAGS) -noinst_HEADERS = cli.h cli-mem-types.h cli-cmd.h +gluster_LDFLAGS = $(GF_LDFLAGS) +noinst_HEADERS = cli.h cli-mem-types.h cli-cmd.h cli-quotad-client.h -AM_CFLAGS = -fPIC -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -D$(GF_HOST_OS)\ - -I$(top_srcdir)/libglusterfs/src -I$(top_srcdir)/rpc/rpc-lib/src\ - -I$(top_srcdir)/rpc/xdr/src\ +AM_CPPFLAGS = $(GF_CPPFLAGS) \ + -I$(top_srcdir)/libglusterfs/src -I$(top_srcdir)/rpc/rpc-lib/src \ + -I$(top_srcdir)/rpc/xdr/src -I$(top_srcdir)/libglusterd/src \ + -I$(top_builddir)/rpc/xdr/src \ -DDATADIR=\"$(localstatedir)\" \ - -DCONFDIR=\"$(sysconfdir)/glusterfs\" $(GF_GLUSTERFS_CFLAGS) + -DCONFDIR=\"$(sysconfdir)/glusterfs\" \ + -DGSYNCD_PREFIX=\"$(GLUSTERFS_LIBEXECDIR)\"\ + -DGLFSHEAL_PREFIX=\"$(GLUSTERFS_LIBEXECDIR)\"\ + -DSYNCDAEMON_COMPILE=$(SYNCDAEMON_COMPILE) +AM_CFLAGS = -Wall $(GF_CFLAGS) $(XML_CFLAGS) CLEANFILES = $(top_builddir)/libglusterfs/src/libglusterfs.la: $(MAKE) -C $(top_builddir)/libglusterfs/src/ all + +$(top_builddir)/libglusterd/src/libglusterd.la: + $(MAKE) -C $(top_builddir)/libglusterd/src/ all + +install-data-hook: + $(mkdir_p) $(DESTDIR)$(localstatedir)/run/gluster diff --git a/cli/src/cli-cmd-global.c b/cli/src/cli-cmd-global.c new file mode 100644 index 00000000000..2c9a5f01bb1 --- /dev/null +++ b/cli/src/cli-cmd-global.c @@ -0,0 +1,195 @@ +/* + Copyright (c) 2015 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + 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 <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <stdint.h> +#include <pthread.h> + +#include <sys/socket.h> +#include <netdb.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <netinet/in.h> + +#include "cli.h" +#include "cli-cmd.h" +#include "cli-mem-types.h" +#include "cli1-xdr.h" +#include <glusterfs/run.h> +#include <glusterfs/syscall.h> +#include <glusterfs/common-utils.h> + +int +cli_cmd_global_help_cbk(struct cli_state *state, struct cli_cmd_word *in_word, + const char **words, int wordcount); +int +cli_cmd_get_state_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount); + +int +cli_cmd_ganesha_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount); + +struct cli_cmd global_cmds[] = { + { + "global help", + cli_cmd_global_help_cbk, + "list global commands", + }, + { + "get-state [<daemon>] [[odir </path/to/output/dir/>] " + "[file <filename>]] [detail|volumeoptions]", + cli_cmd_get_state_cbk, + "Get local state representation of mentioned daemon", + }, + { + "nfs-ganesha {enable| disable} ", + cli_cmd_ganesha_cbk, + "Enable/disable NFS-Ganesha support", + }, + {NULL, NULL, NULL}}; + +int +cli_cmd_global_help_cbk(struct cli_state *state, struct cli_cmd_word *in_word, + const char **words, int wordcount) +{ + struct cli_cmd *cmd = NULL; + struct cli_cmd *global_cmd = NULL; + int count = 0; + + cmd = GF_MALLOC(sizeof(global_cmds), cli_mt_cli_cmd); + memcpy(cmd, global_cmds, sizeof(global_cmds)); + count = (sizeof(global_cmds) / sizeof(struct cli_cmd)); + cli_cmd_sort(cmd, count); + + cli_out("\ngluster global commands"); + cli_out("========================\n"); + for (global_cmd = cmd; global_cmd->pattern; global_cmd++) + if (_gf_false == global_cmd->disable) + cli_out("%s - %s", global_cmd->pattern, global_cmd->desc); + + cli_out("\n"); + GF_FREE(cmd); + return 0; +} + +int +cli_cmd_global_register(struct cli_state *state) +{ + int ret = 0; + struct cli_cmd *cmd = NULL; + for (cmd = global_cmds; cmd->pattern; cmd++) { + ret = cli_cmd_register(&state->tree, cmd); + if (ret) + goto out; + } +out: + return ret; +} + +int +cli_cmd_ganesha_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) + +{ + int sent = 0; + int parse_error = 0; + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + dict_t *options = NULL; + cli_local_t *local = NULL; + char *op_errstr = NULL; + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_GANESHA]; + + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) + goto out; + + ret = cli_cmd_ganesha_parse(state, words, wordcount, &options, &op_errstr); + if (ret) { + if (op_errstr) { + cli_err("%s", op_errstr); + GF_FREE(op_errstr); + } else + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } + + CLI_LOCAL_INIT(local, words, frame, options); + + if (proc->fn) { + ret = proc->fn(frame, THIS, options); + } + +out: + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_out("Setting global option failed"); + } + + CLI_STACK_DESTROY(frame); + return ret; +} + +int +cli_cmd_get_state_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) +{ + int sent = 0; + int parse_error = 0; + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + dict_t *options = NULL; + cli_local_t *local = NULL; + char *op_errstr = NULL; + + ret = cli_cmd_get_state_parse(state, words, wordcount, &options, + &op_errstr); + + if (ret) { + if (op_errstr) { + cli_err("%s", op_errstr); + cli_usage_out(word->pattern); + GF_FREE(op_errstr); + } else + cli_usage_out(word->pattern); + + parse_error = 1; + goto out; + } + + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + ret = -1; + goto out; + } + + CLI_LOCAL_INIT(local, words, frame, options); + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_GET_STATE]; + if (proc->fn) + ret = proc->fn(frame, THIS, options); +out: + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_out("Getting daemon state failed"); + } + + CLI_STACK_DESTROY(frame); + + return ret; +} diff --git a/cli/src/cli-cmd-misc.c b/cli/src/cli-cmd-misc.c index 306a7628faa..e961d88da86 100644 --- a/cli/src/cli-cmd-misc.c +++ b/cli/src/cli-cmd-misc.c @@ -1,100 +1,117 @@ /* - Copyright (c) 2010 Gluster, Inc. <http://www.gluster.com> - This file is part of GlusterFS. - - GlusterFS is free software; you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - GlusterFS is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see - <http://www.gnu.org/licenses/>. -*/ + Copyright (c) 2010-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + 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 <stdio.h> #include <string.h> #include <stdlib.h> #include <stdint.h> #include <pthread.h> -#ifndef _CONFIG_H -#define _CONFIG_H -#include "config.h" -#endif - #include "cli.h" #include "cli-cmd.h" #include "cli-mem-types.h" #include "protocol-common.h" -extern struct rpc_clnt *global_rpc; - -extern rpc_clnt_prog_t *cli_rpc_prog; - extern struct cli_cmd volume_cmds[]; +extern struct cli_cmd bitrot_cmds[]; +extern struct cli_cmd quota_cmds[]; extern struct cli_cmd cli_probe_cmds[]; extern struct cli_cmd cli_log_cmds[]; +extern struct cli_cmd cli_system_cmds[]; +extern struct cli_cmd cli_bd_cmds[]; +extern struct cli_cmd snapshot_cmds[]; +extern struct cli_cmd global_cmds[]; struct cli_cmd cli_misc_cmds[]; int -cli_cmd_quit_cbk (struct cli_state *state, struct cli_cmd_word *word, - const char **words, int wordcount) +cli_cmd_quit_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) { - exit (0); + exit(0); } -int -cli_cmd_display_help (struct cli_state *state, struct cli_cmd_word *in_word, - const char **words, int wordcount) +static gf_boolean_t +cli_is_help_command(const char *pattern) { - struct cli_cmd *cmd = NULL; - - for (cmd = volume_cmds; cmd->pattern; cmd++) - cli_out ("%s - %s", cmd->pattern, cmd->desc); - - for (cmd = cli_probe_cmds; cmd->pattern; cmd++) - cli_out ("%s - %s", cmd->pattern, cmd->desc); - - for (cmd = cli_misc_cmds; cmd->pattern; cmd++) - cli_out ("%s - %s", cmd->pattern, cmd->desc); + /* FixFixFix + * This is not the best way to determine whether + * this is a help command + */ + if (strstr(pattern, "help")) + return _gf_true; + + return _gf_false; +} - if (!state->rl_enabled) - exit (0); +int +cli_cmd_display_help(struct cli_state *state, struct cli_cmd_word *in_word, + const char **words, int wordcount) +{ + static struct cli_cmd *cmd[] = { + cli_misc_cmds, cli_probe_cmds, volume_cmds, bitrot_cmds, + quota_cmds, snapshot_cmds, global_cmds, NULL}; + struct cli_cmd *cmd_ind = NULL; + int i = 0; + gf_boolean_t list_all = _gf_false; + + /* cli_system_cmds commands for internal usage + they are not exposed + */ + + /* If "help all" */ + if (wordcount == 2) + list_all = _gf_true; + + for (i = 0; cmd[i] != NULL; i++) { + for (cmd_ind = cmd[i]; cmd_ind->pattern; cmd_ind++) { + if ((_gf_false == cmd_ind->disable) && + cli_is_help_command(cmd_ind->pattern)) { + if (list_all && (cmd_ind->cbk)) { + cmd_ind->cbk(state, in_word, words, wordcount); + } else { + cli_out(" %-25s- %s", cmd_ind->pattern, cmd_ind->desc); + } + } + } + } - return 0; + cli_out("\n"); + return 0; } -struct cli_cmd cli_misc_cmds[] = { - { "quit", - cli_cmd_quit_cbk, - "quit"}, +struct cli_cmd cli_help_cmds[] = { + {"help [all]", cli_cmd_display_help, "display help for command classes"}, - { "help", - cli_cmd_display_help, - "display command options"}, + {NULL, NULL, NULL}}; - { NULL, NULL, NULL } -}; +struct cli_cmd cli_misc_cmds[] = {{"quit", cli_cmd_quit_cbk, "quit"}, + {"exit", cli_cmd_quit_cbk, "exit"}, + {NULL, NULL, NULL}}; int -cli_cmd_misc_register (struct cli_state *state) +cli_cmd_misc_register(struct cli_state *state) { - int ret = 0; - struct cli_cmd *cmd = NULL; - - for (cmd = cli_misc_cmds; cmd->pattern; cmd++) { - ret = cli_cmd_register (&state->tree, cmd->pattern, cmd->cbk, - cmd->desc); - if (ret) - goto out; - } + int ret = 0; + struct cli_cmd *cmd = NULL; + + for (cmd = cli_misc_cmds; cmd->pattern; cmd++) { + ret = cli_cmd_register(&state->tree, cmd); + if (ret) + goto out; + } + + for (cmd = cli_help_cmds; cmd->pattern; cmd++) { + ret = cli_cmd_register(&state->tree, cmd); + if (ret) + goto out; + } out: - return ret; + return ret; } diff --git a/cli/src/cli-cmd-parser.c b/cli/src/cli-cmd-parser.c index f5b263a6383..34620b4a31b 100644 --- a/cli/src/cli-cmd-parser.c +++ b/cli/src/cli-cmd-parser.c @@ -1,911 +1,5946 @@ /* - Copyright (c) 2010 Gluster, Inc. <http://www.gluster.com> - This file is part of GlusterFS. - - GlusterFS is free software; you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - GlusterFS is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see - <http://www.gnu.org/licenses/>. -*/ + Copyright (c) 2010-2013 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + 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 <stdio.h> #include <string.h> #include <stdlib.h> #include <stdint.h> #include <pthread.h> - -#ifndef _CONFIG_H -#define _CONFIG_H -#include "config.h" -#endif +#include <fnmatch.h> +#include <time.h> #include "cli.h" #include "cli-cmd.h" #include "cli-mem-types.h" -#include "dict.h" +#include <glusterfs/dict.h> +#include <glusterfs/list.h> #include "protocol-common.h" #include "cli1-xdr.h" -int32_t -cli_cmd_volume_create_parse (const char **words, int wordcount, dict_t **options) +#define MAX_SNAP_DESCRIPTION_LEN 1024 + +static struct snap_config_opt_vals_ snap_confopt_vals[] = { + {.op_name = "snap-max-hard-limit", + .question = "Changing snapshot-max-hard-limit " + "will limit the creation of new snapshots " + "if they exceed the new limit.\n" + "Do you want to continue?"}, + {.op_name = "snap-max-soft-limit", + .question = "If Auto-delete is enabled, snap-max-soft-limit will" + " trigger deletion of oldest snapshot, on the " + "creation of new snapshot, when the " + "snap-max-soft-limit is reached.\n" + "Do you want to change the snap-max-soft-limit?"}, + {.op_name = "both", + .question = "Changing snapshot-max-hard-limit " + "will limit the creation of new snapshots " + "if they exceed the new snapshot-max-hard-limit.\n" + "If Auto-delete is enabled, snap-max-soft-limit will" + " trigger deletion of oldest snapshot, on the " + "creation of new snapshot, when the " + "snap-max-soft-limit is reached.\n" + "Do you want to continue?"}, + { + .op_name = NULL, + }}; + +enum cli_snap_config_set_types { + GF_SNAP_CONFIG_SET_HARD = 0, + GF_SNAP_CONFIG_SET_SOFT = 1, + GF_SNAP_CONFIG_SET_BOTH = 2, +}; +typedef enum cli_snap_config_set_types cli_snap_config_set_types; + +typedef struct _cli_brick { + struct list_head list; + const char *name; + int32_t len; +} cli_brick_t; + +int +cli_cmd_validate_volume(char *volname); + +static const char * +id_sel(void *wcon) { - dict_t *dict = NULL; - char *volname = NULL; - char *delimiter = NULL; - int ret = -1; - gf1_cluster_type type = GF_CLUSTER_TYPE_NONE; - int count = 1; - int brick_count = 0, brick_index = 0; - int brick_list_size = 1; - char brick_list[120000] = {0,}; - int i = 0; - char *tmp_list = NULL; - char *tmpptr = NULL; - int j = 0; - char *host_name = NULL; - char *tmp = NULL; - char *freeptr = NULL; - char *trans_type = NULL; - int32_t index = 0; - - GF_ASSERT (words); - GF_ASSERT (options); + return (const char *)wcon; +} - GF_ASSERT ((strcmp (words[0], "volume")) == 0); - GF_ASSERT ((strcmp (words[1], "create")) == 0); +static char * +str_getunamb(const char *tok, char **opwords) +{ + return (char *)cli_getunamb(tok, (void **)opwords, id_sel); +} - dict = dict_new (); +int32_t +cli_cmd_ta_brick_parse(const char **words, int wordcount, char **ta_brick) +{ + char *host_name = NULL; + char *tmp_host = NULL; + char *delimiter = NULL; + cli_brick_t *brick = NULL; + int ret = 0; + + GF_ASSERT(words); + GF_ASSERT(wordcount); + + if (validate_brick_name((char *)words[wordcount - 1])) { + cli_err( + "Wrong brick type: %s, use <HOSTNAME>:" + "<export-dir-abs-path>", + words[wordcount - 1]); + ret = -1; + goto out; + } else { + delimiter = strrchr(words[wordcount - 1], ':'); + ret = gf_canonicalize_path(delimiter + 1); + if (ret) + goto out; + } + + tmp_host = gf_strdup((char *)words[wordcount - 1]); + if (!tmp_host) { + gf_log("cli", GF_LOG_ERROR, "Out of memory"); + ret = -1; + goto out; + } + get_host_name(tmp_host, &host_name); + if (!host_name) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, + "Unable to retrieve " + "hostname"); + goto out; + } + + if (!(strcmp(host_name, "localhost") && strcmp(host_name, "127.0.0.1") && + strncmp(host_name, "0.", 2))) { + cli_err( + "Please provide a valid hostname/ip other " + "than localhost, 127.0.0.1 or loopback " + "address (0.0.0.0 to 0.255.255.255)."); + ret = -1; + goto out; + } + if (!valid_internet_address(host_name, _gf_false, _gf_false)) { + cli_err( + "internet address '%s' does not conform to " + "standards", + host_name); + } + + brick = GF_MALLOC(sizeof(cli_brick_t), gf_common_list_node); + if (brick == NULL) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Out of memory"); + goto out; + } + + brick->name = words[wordcount - 1]; + brick->len = strlen(words[wordcount - 1]); + *ta_brick = GF_MALLOC(brick->len + 3, gf_common_mt_char); + if (*ta_brick == NULL) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Out of memory"); + goto out; + } + + strcat(*ta_brick, " "); + strcat(*ta_brick, brick->name); + strcat(*ta_brick, " "); +out: + if (tmp_host) { + GF_FREE(tmp_host); + tmp_host = NULL; + } + if (brick) { + GF_FREE(brick); + brick = NULL; + } + + return ret; +} - if (!dict) +int32_t +cli_cmd_bricks_parse(const char **words, int wordcount, int brick_index, + char **bricks, int *brick_count) +{ + int ret = 0; + char *delimiter = NULL; + char *host_name = NULL; + char *tmp_host = NULL; + char *bricks_str = NULL; + int len = 0; + int brick_list_len = 1; /* For initial space */ + struct list_head brick_list = { + 0, + }; + cli_brick_t *brick = NULL; + + GF_ASSERT(words); + GF_ASSERT(wordcount); + GF_ASSERT(bricks); + GF_ASSERT(brick_index > 0); + GF_ASSERT(brick_index < wordcount); + + INIT_LIST_HEAD(&brick_list); + + while (brick_index < wordcount) { + if (validate_brick_name((char *)words[brick_index])) { + cli_err( + "Wrong brick type: %s, use <HOSTNAME>:" + "<export-dir-abs-path>", + words[brick_index]); + ret = -1; + goto out; + } else { + delimiter = strrchr(words[brick_index], ':'); + ret = gf_canonicalize_path(delimiter + 1); + if (ret) goto out; + } - if (wordcount < 3) + tmp_host = gf_strdup((char *)words[brick_index]); + if (!tmp_host) { + gf_log("cli", GF_LOG_ERROR, "Out of memory"); + ret = -1; + goto out; + } + get_host_name(tmp_host, &host_name); + if (!host_name) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, + "Unable to allocate " + "memory"); + GF_FREE(tmp_host); + goto out; + } + + if (!(strcmp(host_name, "localhost") && + strcmp(host_name, "127.0.0.1") && strncmp(host_name, "0.", 2))) { + cli_err( + "Please provide a valid hostname/ip other " + "than localhost, 127.0.0.1 or loopback " + "address (0.0.0.0 to 0.255.255.255)."); + ret = -1; + GF_FREE(tmp_host); + goto out; + } + if (!valid_internet_address(host_name, _gf_false, _gf_false)) { + cli_err( + "internet address '%s' does not conform to " + "standards", + host_name); + } + GF_FREE(tmp_host); + list_for_each_entry(brick, &brick_list, list) + { + if (strcmp(brick->name, words[brick_index]) == 0) { + ret = -1; + cli_err("Found duplicate exports %s", words[brick_index]); goto out; + } + } - volname = (char *)words[2]; + brick = GF_MALLOC(sizeof(cli_brick_t), gf_common_list_node); + if (brick == NULL) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Out of memory"); + goto out; + } + len = strlen(words[brick_index]); + brick->name = words[brick_index]; + brick->len = len; + list_add_tail(&brick->list, &brick_list); + + brick_list_len += len + 1; /* Brick name + space */ + ++(*brick_count); + ++brick_index; + } + + /* If brick count is not valid exit here */ + if (!*brick_count) { + cli_err("No bricks specified"); + ret = -1; + goto out; + } + + brick_list_len++; /* For terminating null char */ + + bricks_str = GF_MALLOC(brick_list_len, gf_common_mt_char); + if (bricks_str == NULL) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Out of memory"); + goto out; + } + *bricks = bricks_str; + *bricks_str = ' '; + bricks_str++; + while (!list_empty(&brick_list)) { + brick = list_first_entry(&brick_list, cli_brick_t, list); + list_del_init(&brick->list); + memcpy(bricks_str, brick->name, brick->len); + bricks_str[brick->len] = ' '; + bricks_str += brick->len + 1; + GF_FREE(brick); + } + *bricks_str = 0; - GF_ASSERT (volname); +out: + while (!list_empty(&brick_list)) { + brick = list_first_entry(&brick_list, cli_brick_t, list); + list_del_init(&brick->list); + GF_FREE(brick); + } - /* Validate the volume name here itself */ - { - if (volname[0] == '-') - goto out; + return ret; +} - if (!strcmp (volname, "all")) - goto out; +int32_t +cli_cmd_create_disperse_check(struct cli_state *state, int *disperse, + int *redundancy, int *data, int count) +{ + int i = 0; + int tmp = 0; + gf_answer_t answer = GF_ANSWER_NO; + char question[128]; + + const char *question1 = + "There isn't an optimal redundancy value " + "for this configuration. Do you want to " + "create the volume with redundancy 1 ?"; + + const char *question2 = + "The optimal redundancy for this " + "configuration is %d. Do you want to create " + "the volume with this value ?"; + + const char *question3 = + "This configuration is not optimal on most " + "workloads. Do you want to use it ?"; + + const char *question4 = + "Redundancy for this configuration is %d. " + "Do you want to create " + "the volume with this value ?"; + + if (*data > 0) { + if (*disperse > 0 && *redundancy > 0) { + if (*disperse != (*data + *redundancy)) { + cli_err( + "Disperse count(%d) should be equal " + "to sum of disperse-data count(%d) and " + "redundancy count(%d)", + *disperse, *data, *redundancy); + return -1; + } + } else if (*redundancy > 0) { + *disperse = *data + *redundancy; + } else if (*disperse > 0) { + *redundancy = *disperse - *data; + } else { + if ((count - *data) >= *data) { + cli_err( + "Please provide redundancy count " + "along with disperse-data count"); + return -1; + } else { + sprintf(question, question4, count - *data); + answer = cli_cmd_get_confirmation(state, question); + if (answer == GF_ANSWER_NO) + return -1; + *redundancy = count - *data; + *disperse = count; + } + } + } - if (strchr (volname, '/')) - goto out; + if (*disperse <= 0) { + if (count < 3) { + cli_err( + "number of bricks must be greater " + "than 2"); + + return -1; + } + *disperse = count; + } - if (strlen (volname) > 512) + if (*redundancy == -1) { + tmp = *disperse - 1; + for (i = tmp / 2; (i > 0) && ((tmp & -tmp) != tmp); i--, tmp--) + ; + + if (i == 0) { + answer = cli_cmd_get_confirmation(state, question1); + if (answer == GF_ANSWER_NO) + return -1; + + *redundancy = 1; + } else { + *redundancy = *disperse - tmp; + if (*redundancy > 1) { + sprintf(question, question2, *redundancy); + answer = cli_cmd_get_confirmation(state, question); + if (answer == GF_ANSWER_NO) + return -1; + } + } + + tmp = 0; + } else { + tmp = *disperse - *redundancy; + } + + if ((*redundancy < 1) || (*redundancy > (*disperse - 1) / 2)) { + cli_err( + "redundancy must be greater than or equal to 1 and " + "less than %d for a disperse %d volume", + (*disperse + 1) / 2, *disperse); + + return -1; + } + + if ((tmp & -tmp) != tmp) { + answer = cli_cmd_get_confirmation(state, question3); + if (answer == GF_ANSWER_NO) + return -1; + } + + return 0; +} + +static int32_t +cli_validate_disperse_volume(char *word, gf1_cluster_type type, + const char **words, int32_t wordcount, + int32_t index, int32_t *disperse_count, + int32_t *redundancy_count, int32_t *data_count) +{ + int ret = -1; + + switch (type) { + case GF_CLUSTER_TYPE_NONE: + case GF_CLUSTER_TYPE_DISPERSE: + if (strcmp(word, "disperse") == 0) { + if (*disperse_count >= 0) { + cli_err("disperse option given twice"); + goto out; + } + if (wordcount < (index + 2)) { + goto out; + } + ret = gf_string2int(words[index + 1], disperse_count); + if (ret == -1 && errno == EINVAL) { + *disperse_count = 0; + ret = 1; + } else if (ret == -1) { + goto out; + } else { + if (*disperse_count < 3) { + cli_err( + "disperse count must " + "be greater than 2"); goto out; + } + ret = 2; + } + } else if (strcmp(word, "disperse-data") == 0) { + if (*data_count >= 0) { + cli_err("disperse-data option given twice"); + goto out; + } + if (wordcount < (index + 2)) { + goto out; + } + ret = gf_string2int(words[index + 1], data_count); + if (ret == -1 || *data_count < 2) { + cli_err("disperse-data must be greater than 1"); + goto out; + } + ret = 2; + } else if (strcmp(word, "redundancy") == 0) { + if (*redundancy_count >= 0) { + cli_err("redundancy option given twice"); + goto out; + } + if (wordcount < (index + 2)) { + goto out; + } + ret = gf_string2int(words[index + 1], redundancy_count); + if (ret == -1 || *redundancy_count < 1) { + cli_err("redundancy must be greater than 0"); + goto out; + } + ret = 2; + } + break; + case GF_CLUSTER_TYPE_REPLICATE: + cli_err( + "replicated-dispersed volume is not " + "supported"); + goto out; + default: + cli_err("Invalid type given"); + break; + } +out: + return ret; +} - for (i = 0; i < strlen (volname); i++) - if (!isalnum (volname[i]) && (volname[i] != '_') && (volname[i] != '-')) - goto out; +int32_t +cli_validate_volname(const char *volname) +{ + int32_t ret = -1; + int32_t i = -1; + int volname_len; + static const char *const invalid_volnames[] = {"volume", + "type", + "subvolumes", + "option", + "end-volume", + "all", + "volume_not_in_ring", + "description", + "force", + "snap-max-hard-limit", + "snap-max-soft-limit", + "auto-delete", + "activate-on-create", + NULL}; + + if (volname[0] == '-') + goto out; + + for (i = 0; invalid_volnames[i]; i++) { + if (!strcmp(volname, invalid_volnames[i])) { + cli_err("\"%s\" cannot be the name of a volume.", volname); + goto out; + } + } + + if (strchr(volname, '/')) + goto out; + + volname_len = strlen(volname); + if (volname_len > GD_VOLUME_NAME_MAX) { + cli_err("Volume name exceeds %d characters.", GD_VOLUME_NAME_MAX); + goto out; + } + + for (i = 0; i < volname_len; i++) { + if (!isalnum(volname[i]) && (volname[i] != '_') && + (volname[i] != '-')) { + cli_err( + "Volume name should not contain \"%c\"" + " character.\nVolume names can only" + "contain alphanumeric, '-' and '_' " + "characters.", + volname[i]); + goto out; } + } + ret = 0; +out: + return ret; +} - ret = dict_set_str (dict, "volname", volname); - if (ret) +int32_t +cli_cmd_volume_create_parse(struct cli_state *state, const char **words, + int wordcount, dict_t **options, char **brick_list) +{ + dict_t *dict = NULL; + char *volname = NULL; + int ret = -1; + gf1_cluster_type type = GF_CLUSTER_TYPE_NONE; + int sub_count = 1; + int brick_index = 0; + char *trans_type = NULL; + int32_t index = 0; + char *bricks = NULL; + char *ta_brick = NULL; + int32_t brick_count = 0; + static char *opwords[] = {"replica", "stripe", "transport", + "disperse", "redundancy", "disperse-data", + "arbiter", "thin-arbiter", NULL}; + + char *w = NULL; + int op_count = 0; + int32_t replica_count = 1; + int32_t arbiter_count = 0; + int32_t thin_arbiter_count = 0; + int32_t stripe_count = 1; + int32_t disperse_count = -1; + int32_t redundancy_count = -1; + int32_t disperse_data_count = -1; + gf_boolean_t is_force = _gf_false; + int wc = wordcount; + gf_answer_t answer = GF_ANSWER_NO; + const char *question = NULL; + + GF_ASSERT(words); + GF_ASSERT(options); + + dict = dict_new(); + + if (!dict) + goto out; + + if (wordcount < 3) + goto out; + + volname = (char *)words[2]; + + GF_ASSERT(volname); + + /* Validate the volume name here itself */ + if (cli_validate_volname(volname) < 0) + goto out; + + if (wordcount < 4) { + ret = -1; + goto out; + } + + type = GF_CLUSTER_TYPE_NONE; + index = 3; + + while (op_count < 3) { + ret = -1; + w = str_getunamb(words[index], opwords); + if (!w) { + break; + } else if ((strcmp(w, "replica")) == 0) { + switch (type) { + case GF_CLUSTER_TYPE_STRIPE_REPLICATE: + case GF_CLUSTER_TYPE_REPLICATE: + cli_err("replica option given twice"); + goto out; + case GF_CLUSTER_TYPE_NONE: + type = GF_CLUSTER_TYPE_REPLICATE; + break; + case GF_CLUSTER_TYPE_STRIPE: + cli_err("stripe option not supported"); + goto out; + case GF_CLUSTER_TYPE_DISPERSE: + cli_err( + "replicated-dispersed volume is not " + "supported"); + goto out; + default: + cli_err("Invalid type given"); + goto out; + } + + if (wordcount < (index + 2)) { + ret = -1; goto out; + } - if (wordcount < 4) { + replica_count = strtol(words[index + 1], NULL, 0); + if (replica_count < 2) { + cli_err( + "replica count should be greater" + " than 1"); ret = -1; goto out; - } - if ((strcasecmp (words[3], "replica")) == 0) { - type = GF_CLUSTER_TYPE_REPLICATE; - if (wordcount < 5) { - ret = -1; - goto out; - } - count = strtol (words[4], NULL, 0); - if (!count) { - /* Wrong number of replica count */ + } + + index += 2; + if (words[index]) { + if (!strcmp(words[index], "arbiter")) { + ret = gf_string2int(words[index + 1], &arbiter_count); + if ((ret == -1) || (arbiter_count != 1)) { + cli_err( + "For arbiter " + "configuration, " + "replica count must be" + " 2 and arbiter count " + "must be 1. The 3rd " + "brick of the replica " + "will be the arbiter"); ret = -1; goto out; - } - ret = dict_set_int32 (dict, "replica-count", count); - if (ret) + } + ret = dict_set_int32(dict, "arbiter-count", arbiter_count); + if (ret) goto out; - if (count < 2) { + index += 2; + } else if (!strcmp(words[index], "thin-arbiter")) { + ret = gf_string2int(words[index + 1], &thin_arbiter_count); + if ((ret == -1) || (thin_arbiter_count != 1) || + (replica_count != 2)) { + cli_err( + "For thin-arbiter " + "configuration, " + "replica count must be" + " 2 and thin-arbiter count " + "must be 1. The 3rd " + "brick of the replica " + "will be the thin-arbiter brick"); ret = -1; goto out; - } - brick_index = 5; - } else if ((strcasecmp (words[3], "stripe")) == 0) { - type = GF_CLUSTER_TYPE_STRIPE; - if (wordcount < 5) { - ret = -1; + } + ret = dict_set_int32(dict, "thin-arbiter-count", + thin_arbiter_count); + if (ret) goto out; + index += 2; } - count = strtol (words[4], NULL, 0); - if (!count) { - /* Wrong number of stripe count */ + } + + /* Do this to keep glusterd happy with sending + "replica 3 arbiter 1" options to server */ + if ((arbiter_count == 1) && (replica_count == 2)) + replica_count += arbiter_count; + + if (replica_count == 2 && thin_arbiter_count == 0) { + if (strcmp(words[wordcount - 1], "force")) { + question = + "Replica 2 volumes are prone" + " to split-brain. Use " + "Arbiter or Replica 3 to " + "avoid this. See: " + "http://docs.gluster.org/en/latest/" + "Administrator%20Guide/" + "Split%20brain%20and%20ways%20to%20deal%20with%20it/." + "\nDo you still want to " + "continue?\n"; + answer = cli_cmd_get_confirmation(state, question); + if (GF_ANSWER_NO == answer) { + gf_log("cli", GF_LOG_ERROR, + "Volume create " + "cancelled, exiting"); ret = -1; goto out; + } } - ret = dict_set_int32 (dict, "stripe-count", count); - if (ret) - goto out; - if (count < 2) { - ret = -1; - goto out; - } - brick_index = 5; + } + ret = dict_set_int32(dict, "replica-count", replica_count); + if (ret) + goto out; + + } else if ((strcmp(w, "stripe")) == 0) { + cli_err("stripe option not supported"); + goto out; + } else if ((strcmp(w, "transport")) == 0) { + if (trans_type) { + cli_err( + "'transport' option given more" + " than one time"); + goto out; + } + if ((strcasecmp(words[index + 1], "tcp") == 0)) { + trans_type = gf_strdup("tcp"); + } else if ((strcasecmp(words[index + 1], "rdma") == 0)) { + trans_type = gf_strdup("rdma"); + } else if ((strcasecmp(words[index + 1], "tcp,rdma") == 0) || + (strcasecmp(words[index + 1], "rdma,tcp") == 0)) { + trans_type = gf_strdup("tcp,rdma"); + } else { + gf_log("", GF_LOG_ERROR, + "incorrect transport" + " protocol specified"); + ret = -1; + goto out; + } + index += 2; + + } else if ((strcmp(w, "disperse") == 0) || + (strcmp(w, "redundancy") == 0) || + (strcmp(w, "disperse-data") == 0)) { + ret = cli_validate_disperse_volume( + w, type, words, wordcount, index, &disperse_count, + &redundancy_count, &disperse_data_count); + if (ret < 0) + goto out; + index += ret; + type = GF_CLUSTER_TYPE_DISPERSE; + } else if ((strcmp(w, "arbiter") == 0)) { + cli_err( + "arbiter option must be preceded by replica " + "option."); + ret = -1; + goto out; + } else if ((strcmp(w, "thin-arbiter") == 0)) { + cli_err( + "thin-arbiter option must be preceded by replica " + "option."); + ret = -1; + goto out; } else { - type = GF_CLUSTER_TYPE_NONE; - brick_index = 3; + GF_ASSERT(!"opword mismatch"); + ret = -1; + goto out; } + op_count++; + } - ret = dict_set_int32 (dict, "type", type); - if (ret) - goto out; + if (!trans_type) + trans_type = gf_strdup("tcp"); + + if (index >= wordcount) { + ret = -1; + goto out; + } - if (type) - index = 5; + brick_index = index; + + if (strcmp(words[wordcount - 1], "force") == 0) { + is_force = _gf_true; + wc = wordcount - 1; + } + + // Exclude the thin-arbiter-brick i.e. last brick in the bricks list + if (thin_arbiter_count == 1) { + ret = cli_cmd_bricks_parse(words, wc - 1, brick_index, &bricks, + &brick_count); + if (ret) + goto out; + + ret = cli_cmd_ta_brick_parse(words, wc, &ta_brick); + + } else { + ret = cli_cmd_bricks_parse(words, wc, brick_index, &bricks, + &brick_count); + } + + if (ret) + goto out; + + if (type == GF_CLUSTER_TYPE_DISPERSE) { + ret = cli_cmd_create_disperse_check(state, &disperse_count, + &redundancy_count, + &disperse_data_count, brick_count); + if (!ret) + ret = dict_set_int32(dict, "disperse-count", disperse_count); + if (!ret) + ret = dict_set_int32(dict, "redundancy-count", redundancy_count); + if (ret) + goto out; + + sub_count = disperse_count; + } else + sub_count = stripe_count * replica_count; + + if (brick_count % sub_count) { + if (type == GF_CLUSTER_TYPE_STRIPE) + cli_err( + "number of bricks is not a multiple of " + "stripe count"); + else if (type == GF_CLUSTER_TYPE_REPLICATE) + cli_err( + "number of bricks is not a multiple of " + "replica count"); + else if (type == GF_CLUSTER_TYPE_DISPERSE) + cli_err( + "number of bricks is not a multiple of " + "disperse count"); else - index = 3; - - if (strcasecmp(words[index], "transport") == 0) { - brick_index = index+2; - if ((strcasecmp (words[index+1], "tcp") == 0)) { - trans_type = gf_strdup ("tcp"); - } else if ((strcasecmp (words[index+1], "rdma") == 0)) { - trans_type = gf_strdup ("rdma"); - } else { - gf_log ("", GF_LOG_ERROR, "incorrect transport" - " protocol specified"); - ret = -1; - goto out; - } - } else { - trans_type = gf_strdup ("tcp"); - } + cli_err( + "number of bricks given doesn't match " + "required count"); + + ret = -1; + goto out; + } + + /* Everything is parsed fine. start setting info in dict */ + ret = dict_set_str(dict, "volname", volname); + if (ret) + goto out; + + ret = dict_set_int32(dict, "type", type); + if (ret) + goto out; + + ret = dict_set_dynstr(dict, "transport", trans_type); + if (ret) + goto out; + trans_type = NULL; + + ret = dict_set_dynstr(dict, "bricks", bricks); + if (ret) + goto out; + + if (thin_arbiter_count == 1) { + ret = dict_set_dynstr(dict, "ta-brick", ta_brick); + if (ret) + goto out; + } + + ret = dict_set_int32(dict, "count", brick_count); + if (ret) + goto out; + + ret = dict_set_int32(dict, "force", is_force); + if (ret) + goto out; + + *options = dict; + *brick_list = bricks; +out: + if (ret) { + GF_FREE(bricks); + GF_FREE(ta_brick); + gf_log("cli", GF_LOG_ERROR, "Unable to parse create volume CLI"); + if (dict) + dict_unref(dict); + } + + GF_FREE(trans_type); + + return ret; +} - ret = dict_set_str (dict, "transport", trans_type); +int32_t +cli_cmd_volume_reset_parse(const char **words, int wordcount, dict_t **options) +{ + dict_t *dict = NULL; + char *volname = NULL; + int ret = -1; + + GF_ASSERT(words); + GF_ASSERT(options); + + dict = dict_new(); + + if (!dict) + goto out; + + if (wordcount < 3) + goto out; + + if (wordcount > 5) + goto out; + + volname = (char *)words[2]; + + if (!volname) { + ret = -1; + goto out; + } + + ret = dict_set_str(dict, "volname", volname); + if (ret) + goto out; + + if (wordcount == 3) { + ret = dict_set_str(dict, "key", "all"); if (ret) + goto out; + } + + if (wordcount >= 4) { + if (!strcmp("force", (char *)words[3])) { + ret = dict_set_int32(dict, "force", 1); + if (ret) goto out; - - strcpy (brick_list, " "); - while (brick_index < wordcount) { - delimiter = strchr (words[brick_index], ':'); - if (!delimiter || delimiter == words[brick_index] - || *(delimiter+1) != '/') { - gf_log ("cli", GF_LOG_ERROR, - "wrong brick type, use <HOSTNAME>:<export-dir>"); - ret = -1; - goto out; - } - if ((brick_list_size + strlen (words[brick_index]) + 1) > 120000) { - gf_log ("cli", GF_LOG_ERROR, - "total brick list is larger than a request " - "can take (brick_count %d)", brick_count); + ret = dict_set_str(dict, "key", "all"); + if (ret) + goto out; + } else { + ret = dict_set_str(dict, "key", (char *)words[3]); + if (ret) + goto out; + } + } + + if (wordcount == 5) { + if (strcmp("force", (char *)words[4])) { + ret = -1; + goto out; + } else { + ret = dict_set_int32(dict, "force", 1); + if (ret) + goto out; + } + } + + *options = dict; + +out: + if (ret && dict) { + dict_unref(dict); + } + + return ret; +} + +int32_t +cli_cmd_get_state_parse(struct cli_state *state, const char **words, + int wordcount, dict_t **options, char **op_errstr) +{ + dict_t *dict = NULL; + int ret = -1; + char *odir = NULL; + char *filename = NULL; + char *daemon_name = NULL; + int count = 0; + uint32_t cmd = 0; + + GF_VALIDATE_OR_GOTO("cli", options, out); + GF_VALIDATE_OR_GOTO("cli", words, out); + + dict = dict_new(); + if (!dict) + goto out; + + if (wordcount < 1 || wordcount > 7) { + *op_errstr = gf_strdup( + "Problem parsing arguments." + " Check usage."); + goto out; + } + + if (wordcount >= 1) { + gf_asprintf(&daemon_name, "%s", "glusterd"); + + for (count = 1; count < wordcount; count++) { + if (strcmp(words[count], "odir") == 0 || + strcmp(words[count], "file") == 0) { + if (strcmp(words[count], "odir") == 0) { + if (++count < wordcount) { + odir = (char *)words[count]; + continue; + } else { ret = -1; goto out; - } - - host_name = gf_strdup(words[brick_index]); - if (!host_name) { + } + } else if (strcmp(words[count], "file") == 0) { + if (++count < wordcount) { + filename = (char *)words[count]; + continue; + } else { ret = -1; - gf_log("cli",GF_LOG_ERROR, "Unable to allocate " - "memory"); goto out; + } } - freeptr = host_name; - - strtok_r(host_name, ":", &tmp); - if (!(strcmp(host_name, "localhost") && - strcmp (host_name, "127.0.0.1"))) { - cli_out ("Please provide a valid hostname/ip other " - "localhost or 127.0.0.1"); + } else { + if (count > 1) { + if (count == wordcount - 1) { + if (strcmp(words[count], "detail") == 0) { + cmd = GF_CLI_GET_STATE_DETAIL; + continue; + } else if (strcmp(words[count], "volumeoptions") == 0) { + cmd = GF_CLI_GET_STATE_VOLOPTS; + continue; + } + } else { + *op_errstr = gf_strdup( + "Problem" + " parsing arguments. " + "Check usage."); ret = -1; - GF_FREE(freeptr); goto out; + } } - GF_FREE (freeptr); - tmp_list = strdup(brick_list+1); - j = 0; - while(( brick_count != 0) && (j < brick_count)) { - strtok_r (tmp_list, " ", &tmpptr); - if (!(strcmp (tmp_list, words[brick_index]))) { - ret = -1; - cli_out ("Found duplicate" - " exports %s",words[brick_index]); - goto out; - } - tmp_list = tmpptr; - j++; - } - strcat (brick_list, words[brick_index]); - strcat (brick_list, " "); - brick_list_size += (strlen (words[brick_index]) + 1); - ++brick_count; - ++brick_index; - /* - char key[50]; - snprintf (key, 50, "brick%d", ++brick_count); - ret = dict_set_str (dict, key, (char *)words[brick_index++]); + if (strcmp(words[count], "glusterd") == 0) { + continue; + } else { + if (count == wordcount - 1) { + if (strcmp(words[count], "detail") == 0) { + cmd = GF_CLI_GET_STATE_DETAIL; + continue; + } else if (strcmp(words[count], "volumeoptions") == 0) { + cmd = GF_CLI_GET_STATE_VOLOPTS; + continue; + } + } - if (ret) - goto out; - */ + *op_errstr = gf_strdup( + "glusterd is " + "the only supported daemon."); + ret = -1; + goto out; + } + } } - /* If brick-count is not valid when replica or stripe is - given, exit here */ - if (brick_count % count) { - ret = -1; + ret = dict_set_dynstr(dict, "daemon", daemon_name); + if (ret) { + *op_errstr = gf_strdup( + "Command failed. Please check " + " log file for more details."); + gf_log(THIS->name, GF_LOG_ERROR, + "Setting daemon name to dictionary failed"); + goto out; + } + daemon_name = NULL; + + if (odir) { + ret = dict_set_str(dict, "odir", odir); + if (ret) { + *op_errstr = gf_strdup( + "Command failed. Please" + " check log file for" + " more details."); + gf_log(THIS->name, GF_LOG_ERROR, + "Setting output directory to" + "dictionary failed"); goto out; + } } - ret = dict_set_str (dict, "bricks", brick_list); - if (ret) + if (filename) { + ret = dict_set_str(dict, "filename", filename); + if (ret) { + *op_errstr = gf_strdup( + "Command failed. Please" + " check log file for" + " more details."); + gf_log(THIS->name, GF_LOG_ERROR, + "Setting filename to dictionary failed"); goto out; + } + } - ret = dict_set_int32 (dict, "count", brick_count); - if (ret) + if (cmd) { + ret = dict_set_uint32(dict, "getstate-cmd", cmd); + if (ret) { + *op_errstr = gf_strdup( + "Command failed. Please" + " check log file for" + " more details."); + gf_log(THIS->name, GF_LOG_ERROR, + "Setting " + "get-state command type to dictionary " + "failed"); goto out; + } + } + } +out: + if (dict) *options = dict; -out: - if (ret) { - gf_log ("cli", GF_LOG_ERROR, "Unable to parse create volume CLI"); - if (dict) - dict_destroy (dict); - } - if (trans_type) - GF_FREE (trans_type); + if (ret && dict) + dict_unref(dict); - return ret; + GF_FREE(daemon_name); + + return ret; } +int32_t +cli_cmd_inode_quota_parse(const char **words, int wordcount, dict_t **options) +{ + dict_t *dict = NULL; + char *volname = NULL; + int ret = -1; + + GF_ASSERT(words); + GF_ASSERT(options); + + dict = dict_new(); + if (!dict) { + gf_log("cli", GF_LOG_ERROR, "dict_new failed"); + goto out; + } + + if (wordcount != 4) + goto out; + + volname = (char *)words[2]; + if (!volname) { + ret = -1; + goto out; + } + + /* Validate the volume name here itself */ + if (cli_validate_volname(volname) < 0) + goto out; + + ret = dict_set_str(dict, "volname", volname); + if (ret < 0) + goto out; + + if (strcmp(words[3], "enable") != 0) { + cli_out("Invalid quota option : %s", words[3]); + ret = -1; + goto out; + } + + ret = dict_set_int32(dict, "type", GF_QUOTA_OPTION_TYPE_ENABLE_OBJECTS); + if (ret < 0) + goto out; + + *options = dict; +out: + if (ret < 0) { + if (dict) + dict_unref(dict); + } + + return ret; +} int32_t -cli_cmd_volume_set_parse (const char **words, int wordcount, dict_t **options) +cli_cmd_quota_parse(const char **words, int wordcount, dict_t **options) { - dict_t *dict = NULL; - char *volname = NULL; - int ret = -1; - int count = 0; - char *key = NULL; - char *value = NULL; - int i = 0; - char str[50] = {0,}; + dict_t *dict = NULL; + char *volname = NULL; + int ret = -1; + int i = -1; + char key[20] = { + 0, + }; + int64_t value = 0; + gf_quota_type type = GF_QUOTA_OPTION_TYPE_NONE; + static char *opwords[] = {"enable", + "disable", + "limit-usage", + "remove", + "list", + "alert-time", + "soft-timeout", + "hard-timeout", + "default-soft-limit", + "limit-objects", + "list-objects", + "remove-objects", + NULL}; + char *w = NULL; + uint32_t time = 0; + double percent = 0; + char *end_ptr = NULL; + int64_t limit = 0; + + GF_ASSERT(words); + GF_ASSERT(options); + + dict = dict_new(); + if (!dict) { + gf_log("cli", GF_LOG_ERROR, "dict_new failed"); + goto out; + } + + if (wordcount < 4) { + if ((wordcount == 3) && !(strcmp(words[2], "help"))) { + ret = 1; + } + goto out; + } + + volname = (char *)words[2]; + if (!volname) { + ret = -1; + goto out; + } + + /* Validate the volume name here itself */ + if (cli_validate_volname(volname) < 0) + goto out; + + ret = dict_set_str(dict, "volname", volname); + if (ret < 0) + goto out; + + w = str_getunamb(words[3], opwords); + if (!w) { + cli_out("Invalid quota option : %s", words[3]); + ret = -1; + goto out; + } + + if (strcmp(w, "enable") == 0) { + if (wordcount == 4) { + type = GF_QUOTA_OPTION_TYPE_ENABLE; + ret = 0; + goto set_type; + } else { + ret = -1; + goto out; + } + } - GF_ASSERT (words); - GF_ASSERT (options); + if (strcmp(w, "disable") == 0) { + if (wordcount == 4) { + type = GF_QUOTA_OPTION_TYPE_DISABLE; + ret = 0; + goto set_type; + } else { + ret = -1; + goto out; + } + } + + if (strcmp(w, "limit-usage") == 0) { + type = GF_QUOTA_OPTION_TYPE_LIMIT_USAGE; + } else if (strcmp(w, "limit-objects") == 0) { + type = GF_QUOTA_OPTION_TYPE_LIMIT_OBJECTS; + } + + if (type == GF_QUOTA_OPTION_TYPE_LIMIT_USAGE || + type == GF_QUOTA_OPTION_TYPE_LIMIT_OBJECTS) { + if (wordcount < 6 || wordcount > 7) { + ret = -1; + goto out; + } - GF_ASSERT ((strcmp (words[0], "volume")) == 0); - GF_ASSERT ((strcmp (words[1], "set")) == 0); + if (words[4][0] != '/') { + cli_err("Please enter absolute path"); + ret = -1; + goto out; + } + ret = dict_set_str(dict, "path", (char *)words[4]); + if (ret) + goto out; - dict = dict_new (); + if (!words[5]) { + cli_err("Please enter the limit value to be set"); + ret = -1; + goto out; + } - if (!dict) + if (type == GF_QUOTA_OPTION_TYPE_LIMIT_USAGE) { + ret = gf_string2bytesize_int64(words[5], &value); + if (ret != 0 || value <= 0) { + if (errno == ERANGE || value <= 0) { + ret = -1; + cli_err( + "Please enter an integer " + "value in the range of " + "(1 - %" PRId64 ")", + INT64_MAX); + } else + cli_err( + "Please enter a correct " + "value"); + goto out; + } + } else { + errno = 0; + limit = strtol(words[5], &end_ptr, 10); + if (errno == ERANGE || errno == EINVAL || limit <= 0 || + strcmp(end_ptr, "") != 0) { + ret = -1; + cli_err( + "Please enter an integer value in " + "the range 1 - %" PRId64, + INT64_MAX); goto out; + } + } + + ret = dict_set_str(dict, "hard-limit", (char *)words[5]); + if (ret < 0) + goto out; - if (wordcount < 3) + if (wordcount == 7) { + ret = gf_string2percent(words[6], &percent); + if (ret != 0 || percent > 100) { + ret = -1; + cli_err( + "Please enter a correct value " + "in the range of 0 to 100"); goto out; + } - volname = (char *)words[2]; + ret = dict_set_str(dict, "soft-limit", (char *)words[6]); + if (ret < 0) + goto out; + } - GF_ASSERT (volname); + goto set_type; + } - ret = dict_set_str (dict, "volname", volname); + if (strcmp(w, "remove") == 0) { + if (wordcount != 5) { + ret = -1; + goto out; + } - if (ret) - goto out; + type = GF_QUOTA_OPTION_TYPE_REMOVE; - for (i = 3; i < wordcount; i++) { - key = strtok ((char *)words[i], "="); - value = strtok (NULL, "="); + if (words[4][0] != '/') { + cli_err("Please enter absolute path"); + ret = -1; + goto out; + } - GF_ASSERT (key); - GF_ASSERT (value); + ret = dict_set_str(dict, "path", (char *)words[4]); + if (ret < 0) + goto out; + goto set_type; + } - count++; + if (strcmp(w, "remove-objects") == 0) { + if (wordcount != 5) { + ret = -1; + goto out; + } - sprintf (str, "key%d", count); - ret = dict_set_str (dict, str, key); - if (ret) - goto out; + type = GF_QUOTA_OPTION_TYPE_REMOVE_OBJECTS; - sprintf (str, "value%d", count); - ret = dict_set_str (dict, str, value); + if (words[4][0] != '/') { + cli_err("Please enter absolute path"); + ret = -1; + goto out; + } - if (ret) - goto out; + ret = dict_set_str(dict, "path", (char *)words[4]); + if (ret < 0) + goto out; + goto set_type; + } + + if (strcmp(w, "list") == 0) { + type = GF_QUOTA_OPTION_TYPE_LIST; + + if (words[4] && words[4][0] != '/') { + cli_err("Please enter absolute path"); + ret = -1; + goto out; } - ret = dict_set_int32 (dict, "count", count); + i = 4; + while (i < wordcount) { + snprintf(key, 20, "path%d", i - 4); - if (ret) + ret = dict_set_str(dict, key, (char *)words[i++]); + if (ret < 0) goto out; + } - *options = dict; + ret = dict_set_int32(dict, "count", i - 4); + if (ret < 0) + goto out; -out: + goto set_type; + } + + if (strcmp(w, "list-objects") == 0) { + type = GF_QUOTA_OPTION_TYPE_LIST_OBJECTS; + + i = 4; + while (i < wordcount) { + snprintf(key, 20, "path%d", i - 4); + + ret = dict_set_str(dict, key, (char *)words[i++]); + if (ret < 0) { + gf_log("cli", GF_LOG_ERROR, + "Failed to set " + "quota patch in request dictionary"); + goto out; + } + } + + ret = dict_set_int32(dict, "count", i - 4); + if (ret < 0) { + gf_log("cli", GF_LOG_ERROR, + "Failed to set quota " + "limit count in request dictionary"); + goto out; + } + + goto set_type; + } + + if (strcmp(w, "alert-time") == 0) { + if (wordcount != 5) { + ret = -1; + goto out; + } + type = GF_QUOTA_OPTION_TYPE_ALERT_TIME; + + ret = gf_string2time(words[4], &time); if (ret) { - if (dict) - dict_destroy (dict); + cli_err( + "Invalid argument %s. Please enter a valid " + "string", + words[4]); + goto out; } - return ret; + ret = dict_set_str(dict, "value", (char *)words[4]); + if (ret < 0) + goto out; + goto set_type; + } + + if (strcmp(w, "soft-timeout") == 0) { + if (wordcount != 5) { + ret = -1; + goto out; + } + type = GF_QUOTA_OPTION_TYPE_SOFT_TIMEOUT; + + ret = gf_string2time(words[4], &time); + if (ret) { + cli_err( + "Invalid argument %s. Please enter a valid " + "string", + words[4]); + goto out; + } + + ret = dict_set_str(dict, "value", (char *)words[4]); + if (ret < 0) + goto out; + goto set_type; + } + + if (strcmp(w, "hard-timeout") == 0) { + if (wordcount != 5) { + ret = -1; + goto out; + } + type = GF_QUOTA_OPTION_TYPE_HARD_TIMEOUT; + + ret = gf_string2time(words[4], &time); + if (ret) { + cli_err( + "Invalid argument %s. Please enter a valid " + "string", + words[4]); + goto out; + } + + ret = dict_set_str(dict, "value", (char *)words[4]); + if (ret < 0) + goto out; + goto set_type; + } + if (strcmp(w, "default-soft-limit") == 0) { + if (wordcount != 5) { + ret = -1; + goto out; + } + type = GF_QUOTA_OPTION_TYPE_DEFAULT_SOFT_LIMIT; + + ret = dict_set_str(dict, "value", (char *)words[4]); + if (ret < 0) + goto out; + goto set_type; + } else { + GF_ASSERT(!"opword mismatch"); + } + +set_type: + ret = dict_set_int32(dict, "type", type); + if (ret < 0) + goto out; + + *options = dict; +out: + if (ret < 0) { + if (dict) + dict_unref(dict); + } + + return ret; +} + +static gf_boolean_t +cli_is_key_spl(char *key) +{ + return (strcmp(key, "group") == 0); +} + +static int32_t +cli_add_key_group_value(dict_t *dict, const char *name, const char *value, + int32_t id, char **op_errstr) +{ + char *key = NULL; + char *data = NULL; + int32_t ret = -1; + + ret = gf_asprintf(&key, "%s%d", name, id); + if (ret < 0) { + goto out; + } + data = gf_strdup(value); + if (data == NULL) { + gf_log(THIS->name, GF_LOG_ERROR, "Failed to allocate memory for data"); + ret = -1; + goto out; + } + + ret = dict_set_dynstr(dict, key, data); + if (ret == 0) { + data = NULL; + } + +out: + GF_FREE(key); + GF_FREE(data); + + if ((ret != 0) && (op_errstr != NULL)) { + *op_errstr = gf_strdup("Failed to allocate memory"); + } + + return ret; +} + +static int +cli_add_key_group(dict_t *dict, char *key, char *value, char **op_errstr) +{ + int ret = -1; + int opt_count = 0; + char *saveptr = NULL; + char *tok_key = NULL; + char *tok_val = NULL; + char *tagpath = NULL; + char line[PATH_MAX + 256] = { + 0, + }; + FILE *fp = NULL; + + ret = gf_asprintf(&tagpath, "%s/groups/%s", GLUSTERD_DEFAULT_WORKDIR, + value); + if (ret == -1) { + tagpath = NULL; + goto out; + } + + fp = fopen(tagpath, "r"); + if (!fp) { + ret = -1; + if (op_errstr) { + gf_asprintf(op_errstr, + "Unable to open file '%s'. " + "Error: %s", + tagpath, strerror(errno)); + } + goto out; + } + + opt_count = 0; + while (fgets(line, sizeof(line), fp) != NULL) { + if (strlen(line) >= sizeof(line) - 1) { + ret = -1; + if (op_errstr != NULL) { + *op_errstr = gf_strdup("Line too long"); + } + goto out; + } + + /* Treat line that start with "#" as comments */ + if ('#' == line[0]) + continue; + + opt_count++; + tok_key = strtok_r(line, "=", &saveptr); + tok_val = strtok_r(NULL, "\r\n", &saveptr); + if (!tok_key || !tok_val) { + ret = -1; + if (op_errstr) { + gf_asprintf(op_errstr, + "'%s' file format " + "not valid.", + tagpath); + } + goto out; + } + + ret = cli_add_key_group_value(dict, "key", tok_key, opt_count, + op_errstr); + if (ret != 0) { + goto out; + } + ret = cli_add_key_group_value(dict, "value", tok_val, opt_count, + op_errstr); + if (ret != 0) { + goto out; + } + } + + if (!opt_count) { + ret = -1; + if (op_errstr) { + gf_asprintf(op_errstr, "'%s' file format not valid.", tagpath); + } + goto out; + } + ret = dict_set_int32(dict, "count", opt_count); +out: + + GF_FREE(tagpath); + + if (fp) + fclose(fp); + + return ret; } int32_t -cli_cmd_volume_add_brick_parse (const char **words, int wordcount, - dict_t **options) +cli_cmd_volume_set_parse(struct cli_state *state, const char **words, + int wordcount, dict_t **options, char **op_errstr) { - dict_t *dict = NULL; - char *volname = NULL; - char *delimiter = NULL; - int ret = -1; - gf1_cluster_type type = GF_CLUSTER_TYPE_NONE; - int count = 0; - //char key[50] = {0,}; - int brick_count = 0, brick_index = 0; - int brick_list_size = 1; - char brick_list[120000] = {0,}; - int j = 0; - char *tmp_list = NULL; - char *tmpptr = NULL; - char *host_name = NULL; - char *tmp = NULL; - char *freeptr = NULL; + dict_t *dict = NULL; + char *volname = NULL; + int ret = -1; + int count = 0; + char *key = NULL; + char *value = NULL; + int i = 0; + char str[50] = { + 0, + }; + const char *question = NULL; + gf_answer_t answer = GF_ANSWER_NO; - GF_ASSERT (words); - GF_ASSERT (options); + GF_ASSERT(words); + GF_ASSERT(options); - GF_ASSERT ((strcmp (words[0], "volume")) == 0); - GF_ASSERT ((strcmp (words[1], "add-brick")) == 0); + dict = dict_new(); - dict = dict_new (); + if (!dict) + goto out; - if (!dict) - goto out; + if (wordcount < 3) + goto out; - if (wordcount < 3) - goto out; + volname = (char *)words[2]; - volname = (char *)words[2]; + GF_ASSERT(volname); - GF_ASSERT (volname); + ret = dict_set_str(dict, "volname", volname); - ret = dict_set_str (dict, "volname", volname); + if (ret) + goto out; + if (!strcmp(volname, "all")) { + ret = dict_set_str(dict, "globalname", "All"); + if (ret) { + gf_log(THIS->name, GF_LOG_ERROR, "dict set on global key failed."); + goto out; + } + + ret = dict_set_int32(dict, "hold_global_locks", _gf_true); + if (ret) { + gf_log(THIS->name, GF_LOG_ERROR, "dict set on global key failed."); + goto out; + } + } + + if ((!strcmp(volname, "help") || !strcmp(volname, "help-xml")) && + wordcount == 3) { + ret = dict_set_str(dict, volname, volname); if (ret) + goto out; + + } else if (wordcount < 5) { + ret = -1; + goto out; + + } else if (wordcount == 5 && cli_is_key_spl((char *)words[3])) { + key = (char *)words[3]; + value = (char *)words[4]; + if (!key || !value) { + ret = -1; + goto out; + } + + ret = gf_strip_whitespace(value, strlen(value)); + if (ret == -1) + goto out; + + if (strlen(value) == 0) { + ret = -1; + goto out; + } + + ret = cli_add_key_group(dict, key, value, op_errstr); + if (ret == 0) + *options = dict; + goto out; + } + + for (i = 3; i < wordcount; i += 2) { + key = (char *)words[i]; + value = (char *)words[i + 1]; + + if (!key || !value) { + ret = -1; + goto out; + } + + count++; + + if (fnmatch("user.*", key, FNM_NOESCAPE) != 0) { + ret = gf_strip_whitespace(value, strlen(value)); + if (ret == -1) goto out; + } + + if (strlen(value) == 0) { + ret = -1; + goto out; + } + + if (cli_is_key_spl(key)) { + ret = -1; + goto out; + } - if (wordcount < 4) { + sprintf(str, "key%d", count); + ret = dict_set_str(dict, str, key); + if (ret) + goto out; + + sprintf(str, "value%d", count); + ret = dict_set_str(dict, str, value); + + if (ret) + goto out; + + if ((!strcmp(key, "cluster.enable-shared-storage")) && + (!strcmp(value, "disable"))) { + question = + "Disabling cluster.enable-shared-storage " + "will delete the shared storage volume" + "(gluster_shared_storage), which is used " + "by snapshot scheduler, geo-replication " + "and NFS-Ganesha. Do you still want to " + "continue?"; + answer = cli_cmd_get_confirmation(state, question); + if (GF_ANSWER_NO == answer) { + gf_log("cli", GF_LOG_ERROR, + "Operation " + "cancelled, exiting"); + *op_errstr = gf_strdup("Aborted by user."); ret = -1; goto out; + } } + if ((!strcmp(key, "nfs.disable")) && (!strcmp(value, "off"))) { + question = + "Gluster NFS is being deprecated in favor " + "of NFS-Ganesha Enter \"yes\" to continue " + "using Gluster NFS"; + answer = cli_cmd_get_confirmation(state, question); + if (GF_ANSWER_NO == answer) { + gf_log("cli", GF_LOG_ERROR, + "Operation " + "cancelled, exiting"); + *op_errstr = gf_strdup("Aborted by user."); + ret = -1; + goto out; + } + } + } - if ((strcasecmp (words[3], "replica")) == 0) { - type = GF_CLUSTER_TYPE_REPLICATE; - if (wordcount < 5) { - ret = -1; - goto out; - } + ret = dict_set_int32(dict, "count", wordcount - 3); - errno = 0; - count = strtol (words[4], NULL, 0); - if (errno == ERANGE && (count == LONG_MAX || count == LONG_MIN)) - goto out; + if (ret) + goto out; - brick_index = 5; - } else if ((strcasecmp (words[3], "stripe")) == 0) { - type = GF_CLUSTER_TYPE_STRIPE; - if (wordcount < 5) { - ret = -1; - goto out; - } + *options = dict; - errno = 0; - count = strtol (words[4], NULL, 0); - if (errno == ERANGE && (count == LONG_MAX || count == LONG_MIN)) - goto out; +out: + if (ret && dict) + dict_unref(dict); - brick_index = 5; - } else { - brick_index = 3; + return ret; +} + +int32_t +cli_cmd_volume_add_brick_parse(struct cli_state *state, const char **words, + int wordcount, dict_t **options, int *ret_type) +{ + dict_t *dict = NULL; + char *volname = NULL; + int ret = -1; + int brick_count = 0, brick_index = 0; + char *bricks = NULL; + static char *opwords_cl[] = {"replica", "stripe", NULL}; + gf1_cluster_type type = GF_CLUSTER_TYPE_NONE; + int count = 1; + int arbiter_count = 0; + char *w = NULL; + int index; + gf_boolean_t is_force = _gf_false; + int wc = wordcount; + gf_answer_t answer = GF_ANSWER_NO; + const char *question = NULL; + + GF_ASSERT(words); + GF_ASSERT(options); + + dict = dict_new(); + + if (!dict) + goto out; + + if (wordcount < 3) + goto out; + + volname = (char *)words[2]; + + GF_ASSERT(volname); + + ret = dict_set_str(dict, "volname", volname); + + if (ret) + goto out; + + if (wordcount < 4) { + ret = -1; + goto out; + } + if (wordcount < 6) { + /* seems no options are given, go directly to the parse_brick */ + brick_index = 3; + type = GF_CLUSTER_TYPE_NONE; + goto parse_bricks; + } + + w = str_getunamb(words[3], opwords_cl); + if (!w) { + type = GF_CLUSTER_TYPE_NONE; + index = 3; + } else if ((strcmp(w, "replica")) == 0) { + type = GF_CLUSTER_TYPE_REPLICATE; + count = strtol(words[4], NULL, 0); + if (!count || (count < 2)) { + cli_err("replica count should be greater than 1"); + ret = -1; + goto out; + } + ret = dict_set_int32(dict, "replica-count", count); + if (ret) + goto out; + index = 5; + if (words[index] && !strcmp(words[index], "arbiter")) { + arbiter_count = strtol(words[6], NULL, 0); + if (arbiter_count != 1 || count != 3) { + cli_err( + "For arbiter configuration, replica " + "count must be 3 and arbiter count " + "must be 1. The 3rd brick of the " + "replica will be the arbiter"); + ret = -1; + goto out; + } + ret = dict_set_int32(dict, "arbiter-count", arbiter_count); + if (ret) + goto out; + index = 7; } - strcpy (brick_list, " "); - while (brick_index < wordcount) { - delimiter = strchr (words[brick_index], ':'); - if (!delimiter || delimiter == words[brick_index] - || *(delimiter+1) != '/') { - gf_log ("cli", GF_LOG_ERROR, - "wrong brick type, use <HOSTNAME>:<export-dir>"); - ret = -1; - goto out; - } - if ((brick_list_size + strlen (words[brick_index]) + 1) > 120000) { - gf_log ("cli", GF_LOG_ERROR, - "total brick list is larger than a request " - "can take (brick_count %d)", brick_count); - ret = -1; - goto out; + if (count == 2) { + if (strcmp(words[wordcount - 1], "force")) { + question = + "Replica 2 volumes are prone to " + "split-brain. Use Arbiter or " + "Replica 3 to avoid this. See: " + "http://docs.gluster.org/en/latest/Administrator%20Guide/" + "Split%20brain%20and%20ways%20to%20deal%20with%20it/." + "\nDo you still want to continue?\n"; + answer = cli_cmd_get_confirmation(state, question); + if (GF_ANSWER_NO == answer) { + gf_log("cli", GF_LOG_ERROR, + "Add brick" + " cancelled, exiting"); + ret = -1; + goto out; } + } + } + } else if ((strcmp(w, "stripe")) == 0) { + cli_err("stripe option not supported"); + goto out; + } else { + GF_ASSERT(!"opword mismatch"); + ret = -1; + goto out; + } - host_name = gf_strdup(words[brick_index]); - if (!host_name) { - ret = -1; - gf_log ("cli", GF_LOG_ERROR, "unable to allocate " - "memory"); - goto out; - } - freeptr = host_name; - strtok_r(host_name, ":", &tmp); - if (!(strcmp(host_name, "localhost") && - strcmp (host_name, "127.0.0.1"))) { - cli_out ("Please provide a valid hostname/ip other " - "localhost or 127.0.0.1"); - ret = -1; - GF_FREE (freeptr); - goto out; - } - GF_FREE (freeptr); - - tmp_list = strdup(brick_list+1); - j = 0; - while(( brick_count != 0) && (j < brick_count)) { - strtok_r (tmp_list, " ", &tmpptr); - if (!(strcmp (tmp_list, words[brick_index]))) { - ret = -1; - cli_out ("Found duplicate" - " exports %s",words[brick_index]); - goto out; - } - tmp_list = tmpptr; - j++; - } - - strcat (brick_list, words[brick_index]); - strcat (brick_list, " "); - brick_list_size += (strlen (words[brick_index]) + 1); - ++brick_count; - ++brick_index; - /* - char key[50]; - snprintf (key, 50, "brick%d", ++brick_count); - ret = dict_set_str (dict, key, (char *)words[brick_index++]); + brick_index = index; - if (ret) - goto out; - */ +parse_bricks: + + if (strcmp(words[wordcount - 1], "force") == 0) { + is_force = _gf_true; + wc = wordcount - 1; + } + + ret = cli_cmd_bricks_parse(words, wc, brick_index, &bricks, &brick_count); + if (ret) + goto out; + + ret = dict_set_dynstr(dict, "bricks", bricks); + if (ret) + goto out; + + ret = dict_set_int32(dict, "count", brick_count); + + if (ret) + goto out; + + ret = dict_set_int32(dict, "force", is_force); + if (ret) + goto out; + + *options = dict; + +out: + if (ret_type) + *ret_type = type; + + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to parse add-brick CLI"); + if (dict) + dict_unref(dict); + } + + return ret; +} + +int32_t +cli_cmd_volume_remove_brick_parse(struct cli_state *state, const char **words, + int wordcount, dict_t **options, + int *question, int *brick_count, + int32_t *comm) +{ + dict_t *dict = NULL; + char *volname = NULL; + char *delimiter = NULL; + int ret = -1; + char key[50]; + int brick_index = 0; + int32_t tmp_index = 0; + int32_t j = 0; + char *tmp_brick = NULL; + char *tmp_brick1 = NULL; + static char *type_opword[] = {"replica", NULL}; + static char *opwords[] = {"start", "commit", "stop", + "status", "force", NULL}; + char *w = NULL; + int32_t command = GF_OP_CMD_NONE; + long count = 0; + gf_answer_t answer = GF_ANSWER_NO; + const char *ques = NULL; + + GF_ASSERT(words); + GF_ASSERT(options); + + if (wordcount < 5) + goto out; + + dict = dict_new(); + if (!dict) + goto out; + + volname = (char *)words[2]; + + GF_ASSERT(volname); + + ret = dict_set_str(dict, "volname", volname); + if (ret) + goto out; + + brick_index = 3; + w = str_getunamb(words[3], type_opword); + if (w && !strcmp("replica", w)) { + if (wordcount < 6) { + ret = -1; + goto out; + } + count = strtol(words[4], NULL, 0); + if (count < 1) { + cli_err( + "replica count should be greater than 0 in " + "case of remove-brick"); + ret = -1; + goto out; + } + + if (count == 2) { + if (strcmp(words[wordcount - 1], "force")) { + ques = + "Replica 2 volumes are prone to " + "split-brain. Use Arbiter or Replica 3 " + "to avoid this. See: " + "http://docs.gluster.org/en/latest/Administrator%20Guide/" + "Split%20brain%20and%20ways%20to%20deal%20with%20it/." + "\nDo you still want to continue?\n"; + answer = cli_cmd_get_confirmation(state, ques); + if (GF_ANSWER_NO == answer) { + gf_log("cli", GF_LOG_ERROR, + "Remove " + "brick cancelled, exiting"); + ret = -1; + goto out; + } + } } - ret = dict_set_str (dict, "bricks", brick_list); + + ret = dict_set_int32(dict, "replica-count", count); if (ret) + goto out; + brick_index = 5; + } else if (w) { + GF_ASSERT(!"opword mismatch"); + } + + w = str_getunamb(words[wordcount - 1], opwords); + if (!w) { + ret = -1; + goto out; + } else { + /* handled this option */ + wordcount--; + if (!strcmp("start", w)) { + command = GF_OP_CMD_START; + if (question) + *question = 1; + } else if (!strcmp("commit", w)) { + command = GF_OP_CMD_COMMIT; + } else if (!strcmp("stop", w)) { + command = GF_OP_CMD_STOP; + } else if (!strcmp("status", w)) { + command = GF_OP_CMD_STATUS; + } else if (!strcmp("force", w)) { + command = GF_OP_CMD_COMMIT_FORCE; + if (question) + *question = 1; + } else { + GF_ASSERT(!"opword mismatch"); + ret = -1; + goto out; + } + } + + ret = dict_set_int32(dict, "command", command); + if (ret) + gf_log("cli", GF_LOG_INFO, "failed to set 'command' %d", command); + + tmp_index = brick_index; + tmp_brick = GF_MALLOC(2048 * sizeof(*tmp_brick), gf_common_mt_char); + + if (!tmp_brick) { + gf_log("", GF_LOG_ERROR, + "cli_cmd_volume_remove_brick_parse: " + "Unable to get memory"); + ret = -1; + goto out; + } + + tmp_brick1 = GF_MALLOC(2048 * sizeof(*tmp_brick1), gf_common_mt_char); + + if (!tmp_brick1) { + gf_log("", GF_LOG_ERROR, + "cli_cmd_volume_remove_brick_parse: " + "Unable to get memory"); + ret = -1; + goto out; + } + + while (brick_index < wordcount) { + if (validate_brick_name((char *)words[brick_index])) { + cli_err( + "wrong brick type: %s, use <HOSTNAME>:" + "<export-dir-abs-path>", + words[brick_index]); + ret = -1; + goto out; + } else { + delimiter = strrchr(words[brick_index], ':'); + ret = gf_canonicalize_path(delimiter + 1); + if (ret) + goto out; + } + + j = tmp_index; + strcpy(tmp_brick, words[brick_index]); + while (j < brick_index) { + strcpy(tmp_brick1, words[j]); + if (!(strcmp(tmp_brick, tmp_brick1))) { + gf_log("", GF_LOG_ERROR, + "Duplicate bricks" + " found %s", + words[brick_index]); + cli_err("Duplicate bricks found %s", words[brick_index]); + ret = -1; goto out; + } + j++; + } + snprintf(key, 50, "brick%d", ++(*brick_count)); + ret = dict_set_str(dict, key, (char *)words[brick_index++]); - ret = dict_set_int32 (dict, "count", brick_count); + if (ret) + goto out; + } + if (command != GF_OP_CMD_STATUS && command != GF_OP_CMD_STOP) { + ret = dict_set_int32(dict, "count", *brick_count); if (ret) - goto out; + goto out; + } - *options = dict; + *options = dict; out: - if (ret) { - gf_log ("cli", GF_LOG_ERROR, "Unable to parse add-brick CLI"); - if (dict) - dict_destroy (dict); - } + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to parse remove-brick CLI"); + if (dict) + dict_unref(dict); + } - return ret; + GF_FREE(tmp_brick); + GF_FREE(tmp_brick1); + + *comm = command; + + return ret; } +int32_t +cli_cmd_brick_op_validate_bricks(const char **words, dict_t *dict, int src, + int dst) +{ + int ret = -1; + char *delimiter = NULL; + + if (validate_brick_name((char *)words[src])) { + cli_err( + "wrong brick type: %s, use " + "<HOSTNAME>:<export-dir-abs-path>", + words[3]); + ret = -1; + goto out; + } else { + delimiter = strrchr((char *)words[src], '/'); + ret = gf_canonicalize_path(delimiter); + if (ret) + goto out; + } + + ret = dict_set_str(dict, "src-brick", (char *)words[src]); + if (ret) + goto out; + + if (dst == -1) { + ret = 0; + goto out; + } + + if (validate_brick_name((char *)words[dst])) { + cli_err( + "wrong brick type: %s, use " + "<HOSTNAME>:<export-dir-abs-path>", + words[dst]); + ret = -1; + goto out; + } else { + delimiter = strrchr((char *)words[dst], '/'); + ret = gf_canonicalize_path(delimiter); + if (ret) + goto out; + } + + ret = dict_set_str(dict, "dst-brick", (char *)words[dst]); + if (ret) + goto out; + ret = 0; +out: + return ret; +} + +int32_t +cli_cmd_volume_reset_brick_parse(const char **words, int wordcount, + dict_t **options) +{ + int ret = -1; + char *volname = NULL; + dict_t *dict = NULL; + + if (wordcount < 5 || wordcount > 7) + goto out; + + dict = dict_new(); + + if (!dict) + goto out; + + volname = (char *)words[2]; + + ret = dict_set_str(dict, "volname", volname); + if (ret) + goto out; + + if (wordcount == 5) { + if (strcmp(words[4], "start")) { + cli_err( + "Invalid option '%s' for reset-brick. Please " + "enter valid reset-brick command", + words[4]); + ret = -1; + goto out; + } + + ret = cli_cmd_brick_op_validate_bricks(words, dict, 3, -1); + if (ret) + goto out; + + ret = dict_set_str(dict, "operation", "GF_RESET_OP_START"); + if (ret) + goto out; + } else if (wordcount == 6) { + if (strcmp(words[5], "commit")) { + cli_err( + "Invalid option '%s' for reset-brick. Please " + "enter valid reset-brick command", + words[5]); + ret = -1; + goto out; + } + + ret = cli_cmd_brick_op_validate_bricks(words, dict, 3, 4); + if (ret) + goto out; + + ret = dict_set_str(dict, "operation", "GF_RESET_OP_COMMIT"); + if (ret) + goto out; + } else if (wordcount == 7) { + if (strcmp(words[5], "commit") || strcmp(words[6], "force")) { + cli_err( + "Invalid option '%s %s' for reset-brick. Please " + "enter valid reset-brick command", + words[5], words[6]); + ret = -1; + goto out; + } + + ret = cli_cmd_brick_op_validate_bricks(words, dict, 3, 4); + if (ret) + goto out; + + ret = dict_set_str(dict, "operation", "GF_RESET_OP_COMMIT_FORCE"); + if (ret) + goto out; + } + + *options = dict; + +out: + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to parse reset-brick CLI"); + if (dict) + dict_unref(dict); + } + + return ret; +} int32_t -cli_cmd_volume_remove_brick_parse (const char **words, int wordcount, +cli_cmd_volume_replace_brick_parse(const char **words, int wordcount, dict_t **options) { - dict_t *dict = NULL; - char *volname = NULL; - char *delimiter = NULL; - int ret = -1; - gf1_cluster_type type = GF_CLUSTER_TYPE_NONE; - int count = 0; - char key[50]; - int brick_count = 0, brick_index = 0; - int32_t tmp_index = 0; - int32_t j = 0; - char *tmp_brick = NULL; - char *tmp_brick1 = NULL; + int ret = -1; + char *volname = NULL; + dict_t *dict = NULL; + + GF_ASSERT(words); + GF_ASSERT(options); + + if (wordcount != 7) { + ret = -1; + goto out; + } + + dict = dict_new(); + + if (!dict) { + gf_log("cli", GF_LOG_ERROR, "Failed to allocate dictionary"); + goto out; + } + + volname = (char *)words[2]; + + GF_ASSERT(volname); + + ret = dict_set_str(dict, "volname", volname); + if (ret) + goto out; + + ret = cli_cmd_brick_op_validate_bricks(words, dict, 3, 4); + if (ret) + goto out; + + /* commit force option */ + if (strcmp("commit", words[5]) || strcmp("force", words[6])) { + cli_err( + "Invalid option '%s' '%s' for replace-brick. Please " + "enter valid replace-brick command", + words[5], words[6]); + ret = -1; + goto out; + } - GF_ASSERT (words); - GF_ASSERT (options); + ret = dict_set_str(dict, "operation", "GF_REPLACE_OP_COMMIT_FORCE"); + if (ret) + goto out; - GF_ASSERT ((strcmp (words[0], "volume")) == 0); - GF_ASSERT ((strcmp (words[1], "remove-brick")) == 0); + *options = dict; - dict = dict_new (); +out: + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to parse reset-brick CLI"); + if (dict) + dict_unref(dict); + } + + return ret; +} - if (!dict) +int32_t +cli_cmd_log_filename_parse(const char **words, int wordcount, dict_t **options) +{ + dict_t *dict = NULL; + char *volname = NULL; + char *str = NULL; + int ret = -1; + char *delimiter = NULL; + + GF_ASSERT(words); + GF_ASSERT(options); + + dict = dict_new(); + if (!dict) + goto out; + + volname = (char *)words[3]; + GF_ASSERT(volname); + + ret = dict_set_str(dict, "volname", volname); + if (ret) + goto out; + + str = (char *)words[4]; + if (strchr(str, ':')) { + delimiter = strchr(words[4], ':'); + if (!delimiter || delimiter == words[4] || *(delimiter + 1) != '/') { + cli_err( + "wrong brick type: %s, use <HOSTNAME>:" + "<export-dir-abs-path>", + words[4]); + ret = -1; + goto out; + } else { + ret = gf_canonicalize_path(delimiter + 1); + if (ret) goto out; + } + ret = dict_set_str(dict, "brick", str); + if (ret) + goto out; + /* Path */ + str = (char *)words[5]; + ret = dict_set_str(dict, "path", str); + if (ret) + goto out; + } else { + ret = dict_set_str(dict, "path", str); + if (ret) + goto out; + } + + *options = dict; - if (wordcount < 3) +out: + if (ret && dict) + dict_unref(dict); + + return ret; +} + +int32_t +cli_cmd_log_level_parse(const char **words, int worcount, dict_t **options) +{ + dict_t *dict = NULL; + int ret = -1; + + GF_ASSERT(words); + GF_ASSERT(options); + + /* + * loglevel command format: + * > volume log level <VOL> <XLATOR[*]> <LOGLEVEL> + * > volume log level colon-o posix WARNING + * > volume log level colon-o replicate* DEBUG + * > volume log level coon-o * TRACE + */ + + GF_ASSERT((strncmp(words[0], "volume", 6) == 0)); + GF_ASSERT((strncmp(words[1], "log", 3) == 0)); + GF_ASSERT((strncmp(words[2], "level", 5) == 0)); + + ret = glusterd_check_log_level(words[5]); + if (ret == -1) { + cli_err("Invalid log level [%s] specified", words[5]); + cli_err( + "Valid values for loglevel: (DEBUG|WARNING|ERROR" + "|CRITICAL|NONE|TRACE)"); + goto out; + } + + dict = dict_new(); + if (!dict) + goto out; + + GF_ASSERT(words[3]); + GF_ASSERT(words[4]); + + ret = dict_set_str(dict, "volname", (char *)words[3]); + if (ret) + goto out; + + ret = dict_set_str(dict, "xlator", (char *)words[4]); + if (ret) + goto out; + + ret = dict_set_str(dict, "loglevel", (char *)words[5]); + if (ret) + goto out; + + *options = dict; + +out: + if (ret && dict) + dict_unref(dict); + + return ret; +} + +int32_t +cli_cmd_log_locate_parse(const char **words, int wordcount, dict_t **options) +{ + dict_t *dict = NULL; + char *volname = NULL; + char *str = NULL; + int ret = -1; + char *delimiter = NULL; + + GF_ASSERT(words); + GF_ASSERT(options); + + dict = dict_new(); + if (!dict) + goto out; + + volname = (char *)words[3]; + GF_ASSERT(volname); + + ret = dict_set_str(dict, "volname", volname); + if (ret) + goto out; + + if (words[4]) { + delimiter = strchr(words[4], ':'); + if (!delimiter || delimiter == words[4] || *(delimiter + 1) != '/') { + cli_err( + "wrong brick type: %s, use <HOSTNAME>:" + "<export-dir-abs-path>", + words[4]); + ret = -1; + goto out; + } else { + ret = gf_canonicalize_path(delimiter + 1); + if (ret) goto out; + } + str = (char *)words[4]; + ret = dict_set_str(dict, "brick", str); + if (ret) + goto out; + } - volname = (char *)words[2]; + *options = dict; + +out: + if (ret && dict) + dict_unref(dict); - GF_ASSERT (volname); + return ret; +} + +int32_t +cli_cmd_log_rotate_parse(const char **words, int wordcount, dict_t **options) +{ + dict_t *dict = NULL; + char *volname = NULL; + char *str = NULL; + int ret = -1; + char *delimiter = NULL; - ret = dict_set_str (dict, "volname", volname); + GF_ASSERT(words); + GF_ASSERT(options); + dict = dict_new(); + if (!dict) + goto out; + + if (strcmp("rotate", words[3]) == 0) + volname = (char *)words[2]; + GF_ASSERT(volname); + + ret = dict_set_str(dict, "volname", volname); + if (ret) + goto out; + + if (words[4]) { + delimiter = strchr(words[4], ':'); + if (!delimiter || delimiter == words[4] || *(delimiter + 1) != '/') { + cli_err( + "wrong brick type: %s, use <HOSTNAME>:" + "<export-dir-abs-path>", + words[4]); + ret = -1; + goto out; + } else { + ret = gf_canonicalize_path(delimiter + 1); + if (ret) + goto out; + } + str = (char *)words[4]; + ret = dict_set_str(dict, "brick", str); if (ret) + goto out; + } + + *options = dict; + +out: + if (ret && dict) + dict_unref(dict); + + return ret; +} + +static gf_boolean_t +gsyncd_url_check(const char *w) +{ + return !!strpbrk(w, ":/"); +} + +static gf_boolean_t +valid_slave_gsyncd_url(const char *w) +{ + if (strstr(w, ":::")) + return _gf_false; + else if (strstr(w, "::")) + return _gf_true; + else + return _gf_false; +} + +static gf_boolean_t +gsyncd_glob_check(const char *w) +{ + return !!strpbrk(w, "*?["); +} + +static int +config_parse(const char **words, int wordcount, dict_t *dict, unsigned cmdi, + unsigned glob) +{ + int32_t ret = -1; + int32_t i = -1; + char *append_str = NULL; + size_t append_len = 0; + char *subop = NULL; + char *ret_chkpt = NULL; + struct tm checkpoint_time; + char chkpt_buf[20] = ""; + + switch ((wordcount - 1) - cmdi) { + case 0: + subop = gf_strdup("get-all"); + break; + case 1: + if (words[cmdi + 1][0] == '!') { + (words[cmdi + 1])++; + if (gf_asprintf(&subop, "del%s", glob ? "-glob" : "") == -1) + subop = NULL; + } else + subop = gf_strdup("get"); + + ret = dict_set_str(dict, "op_name", ((char *)words[cmdi + 1])); + if (ret < 0) goto out; + break; + default: + if (gf_asprintf(&subop, "set%s", glob ? "-glob" : "") == -1) + subop = NULL; - if (wordcount < 4) { - ret = -1; + ret = dict_set_str(dict, "op_name", ((char *)words[cmdi + 1])); + if (ret < 0) goto out; - } - if ((strcasecmp (words[3], "replica")) == 0) { - type = GF_CLUSTER_TYPE_REPLICATE; - if (wordcount < 5) { - ret = -1; - goto out; - } - errno = 0; - count = strtol (words[4], NULL, 0); - if (errno == ERANGE && (count == LONG_MAX || count == LONG_MIN)) - goto out; + /* join the varargs by spaces to get the op_value */ - brick_index = 5; - } else if ((strcasecmp (words[3], "stripe")) == 0) { - type = GF_CLUSTER_TYPE_STRIPE; - if (wordcount < 5) { - ret = -1; - goto out; + for (i = cmdi + 2; i < wordcount; i++) + append_len += (strlen(words[i]) + 1); + /* trailing strcat will add two bytes, make space for that */ + append_len++; + + /* strcat is used on this allocation and hence expected to be + * initiatlized to 0. So GF_CALLOC is used. + */ + append_str = GF_CALLOC(1, append_len, cli_mt_append_str); + if (!append_str) { + ret = -1; + goto out; + } + + for (i = cmdi + 2; i < wordcount; i++) { + strcat(append_str, words[i]); + strcat(append_str, " "); + } + append_str[append_len - 2] = '\0'; + /* "checkpoint now" is special: we resolve that "now" */ + if ((strcmp(words[cmdi + 1], "checkpoint") == 0) && + (strcmp(append_str, "now") == 0)) { + struct timeval tv = { + 0, + }; + + ret = gettimeofday(&tv, NULL); + if (ret == -1) + goto out; + + GF_FREE(append_str); + append_str = GF_MALLOC(300, cli_mt_append_str); + if (!append_str) { + ret = -1; + goto out; + } + snprintf(append_str, 300, "%" GF_PRI_SECOND, tv.tv_sec); + } else if ((strcmp(words[cmdi + 1], "checkpoint") == 0) && + (strcmp(append_str, "now") != 0)) { + memset(&checkpoint_time, 0, sizeof(struct tm)); + ret_chkpt = strptime(append_str, "%Y-%m-%d %H:%M:%S", + &checkpoint_time); + + if (ret_chkpt == NULL || *ret_chkpt != '\0') { + ret = -1; + cli_err( + "Invalid Checkpoint label. Use format " + "\"Y-m-d H:M:S\", Example: 2016-10-25 15:30:45"); + goto out; } + GF_FREE(append_str); + append_str = GF_MALLOC(300, cli_mt_append_str); + if (!append_str) { + ret = -1; + goto out; + } + strftime(chkpt_buf, sizeof(chkpt_buf), "%s", &checkpoint_time); + snprintf(append_str, 300, "%s", chkpt_buf); + } - errno = 0; - count = strtol (words[4], NULL, 0); - if (errno == ERANGE && (count == LONG_MAX || count == LONG_MIN)) - goto out; + ret = dict_set_dynstr(dict, "op_value", append_str); + if (ret != 0) { + goto out; + } + append_str = NULL; + } - brick_index = 5; - } else { - brick_index = 3; + ret = -1; + if (subop) { + ret = dict_set_dynstr(dict, "subop", subop); + if (!ret) + subop = NULL; + } + +out: + GF_FREE(append_str); + GF_FREE(subop); + + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} + +/* ssh_port_parse: Parses and validates when ssh_port is given. + * ssh_index refers to index of ssh_port and + * type refers to either push-pem or no-verify + */ + +static int32_t +parse_ssh_port(const char **words, int wordcount, dict_t *dict, unsigned *cmdi, + int ssh_index, char *type) +{ + int ret = 0; + char *end_ptr = NULL; + int64_t limit = 0; + + if (!strcmp((char *)words[ssh_index], "ssh-port")) { + if (strcmp((char *)words[ssh_index - 1], "create")) { + ret = -1; + goto out; + } + (*cmdi)++; + limit = strtol(words[ssh_index + 1], &end_ptr, 10); + if (errno == ERANGE || errno == EINVAL || limit <= 0 || + strcmp(end_ptr, "") != 0) { + ret = -1; + cli_err("Please enter an integer value for ssh_port "); + goto out; } - ret = dict_set_int32 (dict, "type", type); + ret = dict_set_int32(dict, "ssh_port", limit); + if (ret) + goto out; + (*cmdi)++; + } else if (strcmp((char *)words[ssh_index + 1], "create")) { + ret = -1; + goto out; + } + + ret = dict_set_int32(dict, type, 1); + if (ret) + goto out; + (*cmdi)++; +out: + return ret; +} + +static int32_t +force_push_pem_no_verify_parse(const char **words, int wordcount, dict_t *dict, + unsigned *cmdi) +{ + int32_t ret = 0; + + if (!strcmp((char *)words[wordcount - 1], "force")) { + if ((strcmp((char *)words[wordcount - 2], "start")) && + (strcmp((char *)words[wordcount - 2], "stop")) && + (strcmp((char *)words[wordcount - 2], "create")) && + (strcmp((char *)words[wordcount - 2], "no-verify")) && + (strcmp((char *)words[wordcount - 2], "push-pem")) && + (strcmp((char *)words[wordcount - 2], "pause")) && + (strcmp((char *)words[wordcount - 2], "resume"))) { + ret = -1; + goto out; + } + ret = dict_set_int32n(dict, "force", SLEN("force"), 1); if (ret) + goto out; + (*cmdi)++; + + if (!strcmp((char *)words[wordcount - 2], "push-pem")) { + ret = parse_ssh_port(words, wordcount, dict, cmdi, wordcount - 4, + "push_pem"); + if (ret) + goto out; + } else if (!strcmp((char *)words[wordcount - 2], "no-verify")) { + ret = parse_ssh_port(words, wordcount, dict, cmdi, wordcount - 4, + "no_verify"); + if (ret) goto out; + } + } else if (!strcmp((char *)words[wordcount - 1], "push-pem")) { + ret = parse_ssh_port(words, wordcount, dict, cmdi, wordcount - 3, + "push_pem"); + if (ret) + goto out; + } else if (!strcmp((char *)words[wordcount - 1], "no-verify")) { + ret = parse_ssh_port(words, wordcount, dict, cmdi, wordcount - 3, + "no_verify"); + if (ret) + goto out; + } + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} - tmp_index = brick_index; - tmp_brick = GF_MALLOC(2048 * sizeof(*tmp_brick), gf_common_mt_char); +int32_t +cli_cmd_gsync_set_parse(struct cli_state *state, const char **words, + int wordcount, dict_t **options, char **errstr) +{ + int32_t ret = -1; + dict_t *dict = NULL; + gf1_cli_gsync_set type = GF_GSYNC_OPTION_TYPE_NONE; + int i = 0; + unsigned masteri = 0; + unsigned slavei = 0; + unsigned glob = 0; + unsigned cmdi = 0; + static char *opwords[] = {"create", "status", "start", "stop", + "config", "force", "delete", "ssh-port", + "no-verify", "push-pem", "detail", "pause", + "resume", NULL}; + char *w = NULL; + char *save_ptr = NULL; + char *slave_temp = NULL; + char *token = NULL; + gf_answer_t answer = GF_ANSWER_NO; + const char *question = NULL; + + GF_ASSERT(words); + GF_ASSERT(options); + + dict = dict_new(); + if (!dict) + goto out; + + /* new syntax: + * + * volume geo-replication $m $s create [[ssh-port n] [[no-verify] | + * [push-pem]]] [force] volume geo-replication [$m [$s]] status [detail] + * volume geo-replication [$m] $s config [[!]$opt [$val]] + * volume geo-replication $m $s start|stop [force] + * volume geo-replication $m $s delete [reset-sync-time] + * volume geo-replication $m $s pause [force] + * volume geo-replication $m $s resume [force] + */ + + if (wordcount < 3) + goto out; + + for (i = 2; i <= 3 && i < wordcount - 1; i++) { + if (gsyncd_glob_check(words[i])) + glob = i; + if (gsyncd_url_check(words[i])) { + slavei = i; + break; + } + } + + if (glob && !slavei) + /* glob is allowed only for config, thus it implies there is a + * slave argument; but that might have not been recognized on + * the first scan as it's url characteristics has been covered + * by the glob syntax. + * + * In this case, the slave is perforce the last glob-word -- the + * upcoming one is neither glob, nor url, so it's definitely not + * the slave. + */ + slavei = glob; + if (slavei) { + cmdi = slavei + 1; + if (slavei == 3) + masteri = 2; + } else if (i <= 4) { + if (strtail("detail", (char *)words[wordcount - 1])) { + cmdi = wordcount - 2; + if (i == 4) + masteri = 2; + } else { + /* no $s, can only be status cmd + * (with either a single $m before it or nothing) + * -- these conditions imply that i <= 3 after + * the iteration and that i is the successor of + * the (0 or 1 length) sequence of $m-s. + */ + cmdi = i; + if (i == 3) + masteri = 2; + } + } else + goto out; + + /* now check if input really complies syntax + * (in a somewhat redundant way, in favor + * transparent soundness) + */ + + if (masteri && gsyncd_url_check(words[masteri])) + goto out; + + if (slavei && !glob && !valid_slave_gsyncd_url(words[slavei])) { + gf_asprintf(errstr, "Invalid slave url: %s", words[slavei]); + goto out; + } + + w = str_getunamb(words[cmdi], opwords); + if (!w) + goto out; + + if (strcmp(w, "create") == 0) { + type = GF_GSYNC_OPTION_TYPE_CREATE; + + if (!masteri || !slavei) + goto out; + } else if (strcmp(w, "status") == 0) { + type = GF_GSYNC_OPTION_TYPE_STATUS; + + if (slavei && !masteri) + goto out; + } else if (strcmp(w, "config") == 0) { + type = GF_GSYNC_OPTION_TYPE_CONFIG; + + if (!slavei) + goto out; + } else if (strcmp(w, "start") == 0) { + type = GF_GSYNC_OPTION_TYPE_START; + + if (!masteri || !slavei) + goto out; + } else if (strcmp(w, "stop") == 0) { + type = GF_GSYNC_OPTION_TYPE_STOP; + + if (!masteri || !slavei) + goto out; + } else if (strcmp(w, "delete") == 0) { + type = GF_GSYNC_OPTION_TYPE_DELETE; + + if (!masteri || !slavei) + goto out; + } else if (strcmp(w, "pause") == 0) { + type = GF_GSYNC_OPTION_TYPE_PAUSE; + + if (!masteri || !slavei) + goto out; + } else if (strcmp(w, "resume") == 0) { + type = GF_GSYNC_OPTION_TYPE_RESUME; + + if (!masteri || !slavei) + goto out; + } else + GF_ASSERT(!"opword mismatch"); + + ret = force_push_pem_no_verify_parse(words, wordcount, dict, &cmdi); + if (ret) + goto out; + + if (strtail("detail", (char *)words[wordcount - 1])) { + if (!strtail("status", (char *)words[wordcount - 2])) { + ret = -1; + goto out; + } - if (!tmp_brick) { - gf_log ("",GF_LOG_ERROR,"cli_cmd_volume_remove_brick_parse: " - "Unable to get memory"); + ret = dict_set_uint32(dict, "status-detail", _gf_true); + if (ret) + goto out; + cmdi++; + } + + if (type == GF_GSYNC_OPTION_TYPE_DELETE && + !strcmp((char *)words[wordcount - 1], "reset-sync-time")) { + if (strcmp((char *)words[wordcount - 2], "delete")) { + ret = -1; + goto out; + } + ret = dict_set_uint32(dict, "reset-sync-time", _gf_true); + if (ret) + goto out; + cmdi++; + } + + if (type != GF_GSYNC_OPTION_TYPE_CONFIG && (cmdi < wordcount - 1 || glob)) { + ret = -1; + goto out; + } + + /* If got so far, input is valid, assemble the message */ + + ret = 0; + + if (masteri) { + ret = dict_set_str(dict, "master", (char *)words[masteri]); + if (!ret) + ret = dict_set_str(dict, "volname", (char *)words[masteri]); + } + if (!ret && slavei) { + /* If geo-rep is created with root user using the syntax + * gluster vol geo-rep <mastervol> root@<slavehost> ... + * pass down only <slavehost> else pass as it is. + */ + slave_temp = gf_strdup(words[slavei]); + if (slave_temp == NULL) { + ret = -1; + goto out; + } + token = strtok_r(slave_temp, "@", &save_ptr); + if (token && !strcmp(token, "root")) { + ret = dict_set_str(dict, "slave", (char *)words[slavei] + 5); + } else { + ret = dict_set_str(dict, "slave", (char *)words[slavei]); + } + } + if (!ret) + ret = dict_set_int32(dict, "type", type); + if (!ret && type == GF_GSYNC_OPTION_TYPE_CONFIG) { + if (!strcmp((char *)words[wordcount - 2], "ignore-deletes") && + !strcmp((char *)words[wordcount - 1], "true")) { + question = + "There exists ~15 seconds delay for the option to take" + " effect from stime of the corresponding brick. Please" + " check the log for the time, the option is effective." + " Proceed"; + + answer = cli_cmd_get_confirmation(state, question); + + if (GF_ANSWER_NO == answer) { + gf_log("cli", GF_LOG_INFO, + "Operation " + "cancelled, exiting"); + *errstr = gf_strdup("Aborted by user."); ret = -1; goto out; + } + } + + ret = config_parse(words, wordcount, dict, cmdi, glob); + } + +out: + if (slave_temp) + GF_FREE(slave_temp); + if (ret && dict) + dict_unref(dict); + else + *options = dict; + + return ret; +} + +int32_t +cli_cmd_volume_profile_parse(const char **words, int wordcount, + dict_t **options) +{ + dict_t *dict = NULL; + char *volname = NULL; + int ret = -1; + gf1_cli_stats_op op = GF_CLI_STATS_NONE; + gf1_cli_info_op info_op = GF_CLI_INFO_NONE; + gf_boolean_t is_peek = _gf_false; + + static char *opwords[] = {"start", "stop", "info", NULL}; + char *w = NULL; + + GF_ASSERT(words); + GF_ASSERT(options); + + dict = dict_new(); + if (!dict) + goto out; + + if (wordcount < 4) + goto out; + + volname = (char *)words[2]; + + ret = dict_set_str(dict, "volname", volname); + if (ret) + goto out; + + w = str_getunamb(words[3], opwords); + if (!w) { + ret = -1; + goto out; + } + + if ((strcmp(w, "start") == 0 || strcmp(w, "stop") == 0) && wordcount > 5) { + ret = -1; + goto out; + } + + if (strcmp(w, "info") == 0 && wordcount > 7) { + ret = -1; + goto out; + } + + if (strcmp(w, "start") == 0) { + op = GF_CLI_STATS_START; + } else if (strcmp(w, "stop") == 0) { + op = GF_CLI_STATS_STOP; + } else if (strcmp(w, "info") == 0) { + op = GF_CLI_STATS_INFO; + info_op = GF_CLI_INFO_ALL; + if (wordcount > 4) { + if (strcmp(words[4], "incremental") == 0) { + info_op = GF_CLI_INFO_INCREMENTAL; + if (wordcount > 5 && strcmp(words[5], "peek") == 0) { + is_peek = _gf_true; + } + } else if (strcmp(words[4], "cumulative") == 0) { + info_op = GF_CLI_INFO_CUMULATIVE; + } else if (strcmp(words[4], "clear") == 0) { + info_op = GF_CLI_INFO_CLEAR; + } else if (strcmp(words[4], "peek") == 0) { + is_peek = _gf_true; + } } - - tmp_brick1 = GF_MALLOC(2048 * sizeof(*tmp_brick1), gf_common_mt_char); + } else + GF_ASSERT(!"opword mismatch"); + + ret = dict_set_int32(dict, "op", (int32_t)op); + if (ret) + goto out; + + ret = dict_set_int32(dict, "info-op", (int32_t)info_op); + if (ret) + goto out; + + ret = dict_set_int32(dict, "peek", is_peek); + if (ret) + goto out; - if (!tmp_brick1) { - gf_log ("",GF_LOG_ERROR,"cli_cmd_volume_remove_brick_parse: " - "Unable to get memory"); + if (!strcmp(words[wordcount - 1], "nfs")) { + ret = dict_set_int32(dict, "nfs", _gf_true); + if (ret) + goto out; + } + + *options = dict; +out: + if (ret && dict) + dict_unref(dict); + return ret; +} + +int32_t +cli_cmd_volume_top_parse(const char **words, int wordcount, dict_t **options) +{ + dict_t *dict = NULL; + char *volname = NULL; + char *value = NULL; + char *key = NULL; + int ret = -1; + gf1_cli_stats_op op = GF_CLI_STATS_NONE; + gf1_cli_top_op top_op = GF_CLI_TOP_NONE; + int32_t list_cnt = -1; + int index = 0; + int perf = 0; + int32_t blk_size = 0; + int count = 0; + gf_boolean_t nfs = _gf_false; + char *delimiter = NULL; + static char *opwords[] = {"open", "read", "write", + "opendir", "readdir", "read-perf", + "write-perf", "clear", NULL}; + char *w = NULL; + + GF_ASSERT(words); + GF_ASSERT(options); + + dict = dict_new(); + if (!dict) + goto out; + + if (wordcount < 4) + goto out; + + volname = (char *)words[2]; + + ret = dict_set_str(dict, "volname", volname); + if (ret) + goto out; + + op = GF_CLI_STATS_TOP; + ret = dict_set_int32(dict, "op", (int32_t)op); + if (ret) + goto out; + + w = str_getunamb(words[3], opwords); + if (!w) { + ret = -1; + goto out; + } + if (strcmp(w, "open") == 0) { + top_op = GF_CLI_TOP_OPEN; + } else if (strcmp(w, "read") == 0) { + top_op = GF_CLI_TOP_READ; + } else if (strcmp(w, "write") == 0) { + top_op = GF_CLI_TOP_WRITE; + } else if (strcmp(w, "opendir") == 0) { + top_op = GF_CLI_TOP_OPENDIR; + } else if (strcmp(w, "readdir") == 0) { + top_op = GF_CLI_TOP_READDIR; + } else if (strcmp(w, "read-perf") == 0) { + top_op = GF_CLI_TOP_READ_PERF; + perf = 1; + } else if (strcmp(w, "write-perf") == 0) { + top_op = GF_CLI_TOP_WRITE_PERF; + perf = 1; + } else if (strcmp(w, "clear") == 0) { + ret = dict_set_int32(dict, "clear-stats", 1); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not set clear-stats in dict"); + goto out; + } + } else + GF_ASSERT(!"opword mismatch"); + ret = dict_set_int32(dict, "top-op", (int32_t)top_op); + if (ret) + goto out; + + if ((wordcount > 4) && !strcmp(words[4], "nfs")) { + nfs = _gf_true; + ret = dict_set_int32(dict, "nfs", nfs); + if (ret) + goto out; + index = 5; + } else { + index = 4; + } + + for (; index < wordcount; index += 2) { + key = (char *)words[index]; + value = (char *)words[index + 1]; + + if (!key || !value) { + ret = -1; + goto out; + } + if (!strcmp(key, "brick")) { + delimiter = strchr(value, ':'); + if (!delimiter || delimiter == value || *(delimiter + 1) != '/') { + cli_err( + "wrong brick type: %s, use <HOSTNAME>:" + "<export-dir-abs-path>", + value); + ret = -1; + goto out; + } else { + ret = gf_canonicalize_path(delimiter + 1); + if (ret) + goto out; + } + ret = dict_set_str(dict, "brick", value); + + } else if (!strcmp(key, "list-cnt")) { + ret = gf_is_str_int(value); + if (!ret) + list_cnt = atoi(value); + if (ret || (list_cnt < 0) || (list_cnt > 100)) { + cli_err("list-cnt should be between 0 to 100"); ret = -1; goto out; + } + } else if (perf && !nfs && !strcmp(key, "bs")) { + ret = gf_is_str_int(value); + if (!ret) + blk_size = atoi(value); + if (ret || (blk_size <= 0)) { + if (blk_size < 0) + cli_err( + "block size is an invalid" + " number"); + else + cli_err( + "block size should be an " + "integer greater than zero"); + ret = -1; + goto out; + } + ret = dict_set_uint32(dict, "blk-size", (uint32_t)blk_size); + } else if (perf && !nfs && !strcmp(key, "count")) { + ret = gf_is_str_int(value); + if (!ret) + count = atoi(value); + if (ret || (count <= 0)) { + if (count < 0) + cli_err("count is an invalid number"); + else + cli_err( + "count should be an integer " + "greater than zero"); + + ret = -1; + goto out; + } + ret = dict_set_uint32(dict, "blk-cnt", count); + } else { + ret = -1; + goto out; } + if (ret) { + gf_log("", GF_LOG_WARNING, + "Dict set failed for " + "key %s", + key); + goto out; + } + } + if (list_cnt == -1) + list_cnt = 100; + ret = dict_set_int32(dict, "list-cnt", list_cnt); + if (ret) { + gf_log("", GF_LOG_WARNING, "Dict set failed for list_cnt"); + goto out; + } + + if ((blk_size > 0) ^ (count > 0)) { + cli_err("Need to give both 'bs' and 'count'"); + ret = -1; + goto out; + } else if (((uint64_t)blk_size * count) > (10 * GF_UNIT_GB)) { + cli_err("'bs * count' value %" PRIu64 + " is greater than " + "maximum allowed value of 10GB", + ((uint64_t)blk_size * count)); + ret = -1; + goto out; + } + + *options = dict; +out: + if (ret && dict) + dict_unref(dict); + return ret; +} - while (brick_index < wordcount) { - delimiter = strchr(words[brick_index], ':'); - if (!delimiter || delimiter == words[brick_index] - || *(delimiter+1) != '/') { - gf_log ("cli", GF_LOG_ERROR, - "wrong brick type, use <HOSTNAME>:<export-dir>"); - ret = -1; - goto out; - } - j = tmp_index; - strcpy(tmp_brick, words[brick_index]); - while ( j < brick_index) { - strcpy(tmp_brick1, words[j]); - if (!(strcmp (tmp_brick, tmp_brick1))) { - gf_log("",GF_LOG_ERROR, "Duplicate bricks" - " found %s", words[brick_index]); - cli_out("Duplicate bricks found %s", - words[brick_index]); - ret = -1; - goto out; - } - j++; +uint32_t +cli_cmd_get_statusop(const char *arg) +{ + int i = 0; + uint32_t ret = GF_CLI_STATUS_NONE; + char *w = NULL; + static char *opwords[] = {"detail", "mem", "clients", "fd", "inode", + "callpool", "tasks", "client-list", NULL}; + static struct { + char *opname; + uint32_t opcode; + } optable[] = {{"detail", GF_CLI_STATUS_DETAIL}, + {"mem", GF_CLI_STATUS_MEM}, + {"clients", GF_CLI_STATUS_CLIENTS}, + {"fd", GF_CLI_STATUS_FD}, + {"inode", GF_CLI_STATUS_INODE}, + {"callpool", GF_CLI_STATUS_CALLPOOL}, + {"tasks", GF_CLI_STATUS_TASKS}, + {"client-list", GF_CLI_STATUS_CLIENT_LIST}, + {NULL}}; + + w = str_getunamb(arg, opwords); + if (!w) { + gf_log("cli", GF_LOG_DEBUG, "Not a status op %s", arg); + goto out; + } + + for (i = 0; optable[i].opname; i++) { + if (!strcmp(w, optable[i].opname)) { + ret = optable[i].opcode; + break; + } + } + +out: + return ret; +} + +int +cli_cmd_volume_status_parse(const char **words, int wordcount, dict_t **options) +{ + dict_t *dict = NULL; + int ret = -1; + uint32_t cmd = 0; + + GF_ASSERT(options); + + dict = dict_new(); + if (!dict) + goto out; + + switch (wordcount) { + case 2: + cmd = GF_CLI_STATUS_ALL; + ret = 0; + break; + + case 3: + if (!strcmp(words[2], "all")) { + cmd = GF_CLI_STATUS_ALL; + ret = 0; + + } else { + cmd = GF_CLI_STATUS_VOL; + ret = dict_set_str(dict, "volname", (char *)words[2]); + } + + break; + + case 4: + cmd = cli_cmd_get_statusop(words[3]); + + if (!strcmp(words[2], "all")) { + if (cmd == GF_CLI_STATUS_NONE) { + cli_err("%s is not a valid status option", words[3]); + ret = -1; + goto out; } - snprintf (key, 50, "brick%d", ++brick_count); - ret = dict_set_str (dict, key, (char *)words[brick_index++]); + cmd |= GF_CLI_STATUS_ALL; + ret = 0; + } else { + ret = dict_set_str(dict, "volname", (char *)words[2]); if (ret) - goto out; + goto out; + + if (cmd == GF_CLI_STATUS_NONE) { + if (!strcmp(words[3], "nfs")) { + cmd |= GF_CLI_STATUS_NFS; + } else if (!strcmp(words[3], "shd")) { + cmd |= GF_CLI_STATUS_SHD; + } else if (!strcmp(words[3], "quotad")) { + cmd |= GF_CLI_STATUS_QUOTAD; + } else if (!strcmp(words[3], "snapd")) { + cmd |= GF_CLI_STATUS_SNAPD; + } else if (!strcmp(words[3], "bitd")) { + cmd |= GF_CLI_STATUS_BITD; + } else if (!strcmp(words[3], "scrub")) { + cmd |= GF_CLI_STATUS_SCRUB; + } else { + cmd = GF_CLI_STATUS_BRICK; + ret = dict_set_str(dict, "brick", (char *)words[3]); + } + + } else { + cmd |= GF_CLI_STATUS_VOL; + ret = 0; + } + } + + break; + + case 5: + if (!strcmp(words[2], "all")) { + cli_err("Cannot specify brick/nfs for \"all\""); + ret = -1; + goto out; + } + + cmd = cli_cmd_get_statusop(words[4]); + if (cmd == GF_CLI_STATUS_NONE) { + cli_err("%s is not a valid status option", words[4]); + ret = -1; + goto out; + } + + ret = dict_set_str(dict, "volname", (char *)words[2]); + if (ret) + goto out; + + if (!strcmp(words[3], "nfs")) { + if (cmd == GF_CLI_STATUS_FD || cmd == GF_CLI_STATUS_DETAIL || + cmd == GF_CLI_STATUS_TASKS) { + cli_err( + "Detail/FD/Tasks status not available" + " for NFS Servers"); + ret = -1; + goto out; + } + cmd |= GF_CLI_STATUS_NFS; + } else if (!strcmp(words[3], "shd")) { + if (cmd == GF_CLI_STATUS_FD || cmd == GF_CLI_STATUS_CLIENTS || + cmd == GF_CLI_STATUS_DETAIL || cmd == GF_CLI_STATUS_TASKS) { + cli_err( + "Detail/FD/Clients/Tasks status not " + "available for Self-heal Daemons"); + ret = -1; + goto out; + } + cmd |= GF_CLI_STATUS_SHD; + } else if (!strcmp(words[3], "quotad")) { + if (cmd == GF_CLI_STATUS_FD || cmd == GF_CLI_STATUS_CLIENTS || + cmd == GF_CLI_STATUS_DETAIL || cmd == GF_CLI_STATUS_INODE) { + cli_err( + "Detail/FD/Clients/Inode status not " + "available for Quota Daemon"); + ret = -1; + goto out; + } + cmd |= GF_CLI_STATUS_QUOTAD; + } else if (!strcmp(words[3], "snapd")) { + if (cmd == GF_CLI_STATUS_FD || cmd == GF_CLI_STATUS_CLIENTS || + cmd == GF_CLI_STATUS_DETAIL || cmd == GF_CLI_STATUS_INODE) { + cli_err( + "Detail/FD/Clients/Inode status not " + "available for snap daemon"); + ret = -1; + goto out; + } + cmd |= GF_CLI_STATUS_SNAPD; + } else { + if (cmd == GF_CLI_STATUS_TASKS) { + cli_err( + "Tasks status not available for " + "bricks"); + ret = -1; + goto out; + } + cmd |= GF_CLI_STATUS_BRICK; + ret = dict_set_str(dict, "brick", (char *)words[3]); + } + break; + + default: + goto out; + } + + if (ret) + goto out; + + ret = dict_set_int32(dict, "cmd", cmd); + if (ret) + goto out; + + *options = dict; + +out: + if (ret && dict) + dict_unref(dict); + + return ret; +} + +gf_boolean_t +cli_cmd_validate_dumpoption(const char *arg, char **option) +{ + static char *opwords[] = {"all", "nfs", "mem", "iobuf", "callpool", + "priv", "fd", "inode", "history", "inodectx", + "fdctx", "quotad", NULL}; + char *w = NULL; + + w = str_getunamb(arg, opwords); + if (!w) { + gf_log("cli", GF_LOG_DEBUG, "Unknown statedump option %s", arg); + return _gf_false; + } + *option = w; + return _gf_true; +} + +int +cli_cmd_volume_statedump_options_parse(const char **words, int wordcount, + dict_t **options) +{ + int ret = 0; + int i = 0; + dict_t *dict = NULL; + int option_cnt = 0; + char *option = NULL; + char *option_str = NULL; + char *tmp_str = NULL; + char *tmp = NULL; + char *ip_addr = NULL; + char *pid = NULL; + + if ((wordcount >= 5) && ((strcmp(words[3], "client")) == 0)) { + tmp = gf_strdup(words[4]); + if (!tmp) { + ret = -1; + goto out; } + ip_addr = strtok(tmp, ":"); + pid = strtok(NULL, ":"); + if (valid_internet_address(ip_addr, _gf_true, _gf_false) && pid && + gf_valid_pid(pid, strlen(pid))) { + ret = gf_asprintf(&option_str, "%s %s %s", words[3], ip_addr, pid); + if (ret < 0) { + goto out; + } + option_cnt = 3; + } else { + ret = -1; + goto out; + } + } else { + for (i = 3; i < wordcount; i++, option_cnt++) { + if (!cli_cmd_validate_dumpoption(words[i], &option)) { + ret = -1; + goto out; + } + tmp_str = option_str; + option_str = NULL; + ret = gf_asprintf(&option_str, "%s%s ", tmp_str ? tmp_str : "", + option); + GF_FREE(tmp_str); + if (ret < 0) { + goto out; + } + } + if (option_str && (strstr(option_str, "nfs")) && + strstr(option_str, "quotad")) { + ret = -1; + goto out; + } + } + + dict = dict_new(); + if (!dict) { + ret = -1; + goto out; + } + + /* dynamic string in dict is freed up when dict is freed up, and hence + if option_str is NULL pass in an duplicate empty string to the same */ + ret = dict_set_dynstr(dict, "options", + (option_str ? option_str : gf_strdup(""))); + if (ret) + goto out; + option_str = NULL; + + ret = dict_set_int32(dict, "option_cnt", option_cnt); + if (ret) + goto out; + + *options = dict; +out: + GF_FREE(tmp); + GF_FREE(option_str); + if (ret && dict) + dict_unref(dict); + if (ret) + gf_log("cli", GF_LOG_ERROR, "Error parsing dumpoptions"); + return ret; +} - ret = dict_set_int32 (dict, "count", brick_count); +int +cli_cmd_volume_clrlks_opts_parse(const char **words, int wordcount, + dict_t **options) +{ + int ret = -1; + int i = 0; + dict_t *dict = NULL; + char *kind_opts[4] = {"blocked", "granted", "all", NULL}; + char *types[4] = {"inode", "entry", "posix", NULL}; + char *free_ptr = NULL; + + dict = dict_new(); + if (!dict) + goto out; + + if (strcmp(words[4], "kind")) + goto out; + + for (i = 0; kind_opts[i]; i++) { + if (!strcmp(words[5], kind_opts[i])) { + free_ptr = gf_strdup(words[5]); + ret = dict_set_dynstr(dict, "kind", free_ptr); + if (ret) + goto out; + free_ptr = NULL; + break; + } + } + if (i == 3) + goto out; + + ret = -1; + for (i = 0; types[i]; i++) { + if (!strcmp(words[6], types[i])) { + free_ptr = gf_strdup(words[6]); + ret = dict_set_dynstr(dict, "type", free_ptr); + if (ret) + goto out; + free_ptr = NULL; + break; + } + } + if (i == 3) + goto out; + if (wordcount == 8) { + free_ptr = gf_strdup(words[7]); + ret = dict_set_dynstr(dict, "opts", free_ptr); if (ret) + goto out; + free_ptr = NULL; + } + + ret = 0; + *options = dict; +out: + if (ret) { + GF_FREE(free_ptr); + dict_unref(dict); + } + + return ret; +} + +static int +extract_hostname_path_from_token(const char *tmp_words, char **hostname, + char **path) +{ + int ret = 0; + char *delimiter = NULL; + char *tmp_host = NULL; + char *host_name = NULL; + char *words = NULL; + int str_len = 0; + *hostname = NULL; + *path = NULL; + + str_len = strlen(tmp_words) + 1; + words = GF_MALLOC(str_len, gf_common_mt_char); + if (!words) { + ret = -1; + goto out; + } + + snprintf(words, str_len, "%s", tmp_words); + + if (validate_brick_name(words)) { + cli_err( + "Wrong brick type: %s, use <HOSTNAME>:" + "<export-dir-abs-path>", + words); + ret = -1; + goto out; + } else { + delimiter = strrchr(words, ':'); + ret = gf_canonicalize_path(delimiter + 1); + if (ret) { + goto out; + } else { + str_len = strlen(delimiter + 1) + 1; + *path = GF_MALLOC(str_len, gf_common_mt_char); + if (!*path) { + ret = -1; goto out; + } + snprintf(*path, str_len, "%s", delimiter + 1); + } + } + + tmp_host = gf_strdup(words); + if (!tmp_host) { + gf_log("cli", GF_LOG_ERROR, "Out of memory"); + ret = -1; + goto out; + } + get_host_name(tmp_host, &host_name); + if (!host_name) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, + "Unable to allocate " + "memory"); + goto out; + } + if (!(strcmp(host_name, "localhost") && strcmp(host_name, "127.0.0.1") && + strncmp(host_name, "0.", 2))) { + cli_err( + "Please provide a valid hostname/ip other " + "than localhost, 127.0.0.1 or loopback " + "address (0.0.0.0 to 0.255.255.255)."); + ret = -1; + goto out; + } + if (!valid_internet_address(host_name, _gf_false, _gf_false)) { + cli_err( + "internet address '%s' does not conform to " + "standards", + host_name); + ret = -1; + goto out; + } + + str_len = strlen(host_name) + 1; + *hostname = GF_MALLOC(str_len, gf_common_mt_char); + if (!*hostname) { + ret = -1; + goto out; + } + snprintf(*hostname, str_len, "%s", host_name); + ret = 0; - *options = dict; +out: + GF_FREE(words); + GF_FREE(tmp_host); + return ret; +} + +static int +set_hostname_path_in_dict(const char *token, dict_t *dict, int heal_op) +{ + char *hostname = NULL; + char *path = NULL; + int ret = 0; + + ret = extract_hostname_path_from_token(token, &hostname, &path); + if (ret) + goto out; + + switch (heal_op) { + case GF_SHD_OP_SBRAIN_HEAL_FROM_BRICK: + ret = dict_set_dynstr(dict, "heal-source-hostname", hostname); + if (ret) + goto out; + hostname = NULL; + ret = dict_set_dynstr(dict, "heal-source-brickpath", path); + if (ret) { + goto out; + } + path = NULL; + break; + case GF_SHD_OP_STATISTICS_HEAL_COUNT_PER_REPLICA: + ret = dict_set_dynstr(dict, "per-replica-cmd-hostname", hostname); + if (ret) + goto out; + hostname = NULL; + ret = dict_set_dynstr(dict, "per-replica-cmd-path", path); + if (ret) { + goto out; + } + path = NULL; + break; + default: + ret = -1; + break; + } out: - if (ret) { - gf_log ("cli", GF_LOG_ERROR, "Unable to parse remove-brick CLI"); - if (dict) - dict_destroy (dict); + GF_FREE(hostname); + GF_FREE(path); + return ret; +} + +static int +heal_command_type_get(const char *command) +{ + int i = 0; + /* subcommands are set as NULL */ + char *heal_cmds[GF_SHD_OP_HEAL_DISABLE + 1] = { + [GF_SHD_OP_INVALID] = NULL, + [GF_SHD_OP_HEAL_INDEX] = NULL, + [GF_SHD_OP_HEAL_FULL] = "full", + [GF_SHD_OP_INDEX_SUMMARY] = "info", + [GF_SHD_OP_SPLIT_BRAIN_FILES] = NULL, + [GF_SHD_OP_STATISTICS] = "statistics", + [GF_SHD_OP_STATISTICS_HEAL_COUNT] = NULL, + [GF_SHD_OP_STATISTICS_HEAL_COUNT_PER_REPLICA] = NULL, + [GF_SHD_OP_SBRAIN_HEAL_FROM_BIGGER_FILE] = NULL, + [GF_SHD_OP_SBRAIN_HEAL_FROM_BRICK] = NULL, + [GF_SHD_OP_HEAL_ENABLE] = "enable", + [GF_SHD_OP_HEAL_DISABLE] = "disable", + }; + + for (i = 0; i <= GF_SHD_OP_HEAL_DISABLE; i++) { + if (heal_cmds[i] && (strcmp(heal_cmds[i], command) == 0)) + return i; + } + + return GF_SHD_OP_INVALID; +} + +int +cli_cmd_volume_heal_options_parse(const char **words, int wordcount, + dict_t **options) +{ + int ret = 0; + dict_t *dict = NULL; + gf_xl_afr_op_t op = GF_SHD_OP_INVALID; + + dict = dict_new(); + if (!dict) { + gf_log(THIS->name, GF_LOG_ERROR, "Failed to create the dict"); + ret = -1; + goto out; + } + + ret = dict_set_str(dict, "volname", (char *)words[2]); + if (ret) { + gf_log(THIS->name, GF_LOG_ERROR, "failed to set volname"); + goto out; + } + + if (wordcount == 3) { + ret = dict_set_int32(dict, "heal-op", GF_SHD_OP_HEAL_INDEX); + goto done; + } + + if (wordcount == 4) { + op = heal_command_type_get(words[3]); + if (op == GF_SHD_OP_INVALID) { + ret = -1; + goto out; } - if (tmp_brick) - GF_FREE (tmp_brick); - if (tmp_brick1) - GF_FREE (tmp_brick1); + ret = dict_set_int32(dict, "heal-op", op); + goto done; + } - return ret; + if (wordcount == 5) { + if (strcmp(words[3], "info") && strcmp(words[3], "statistics") && + strcmp(words[3], "granular-entry-heal")) { + ret = -1; + goto out; + } + + if (!strcmp(words[3], "info")) { + if (!strcmp(words[4], "split-brain")) { + ret = dict_set_int32(dict, "heal-op", + GF_SHD_OP_SPLIT_BRAIN_FILES); + goto done; + } + if (!strcmp(words[4], "summary")) { + ret = dict_set_int32(dict, "heal-op", GF_SHD_OP_HEAL_SUMMARY); + goto done; + } + } + + if (!strcmp(words[3], "statistics")) { + if (!strcmp(words[4], "heal-count")) { + ret = dict_set_int32(dict, "heal-op", + GF_SHD_OP_STATISTICS_HEAL_COUNT); + goto done; + } + } + + if (!strcmp(words[3], "granular-entry-heal")) { + if (!strcmp(words[4], "enable")) { + ret = dict_set_int32(dict, "heal-op", + GF_SHD_OP_GRANULAR_ENTRY_HEAL_ENABLE); + goto done; + } else if (!strcmp(words[4], "disable")) { + ret = dict_set_int32(dict, "heal-op", + GF_SHD_OP_GRANULAR_ENTRY_HEAL_DISABLE); + goto done; + } + } + + ret = -1; + goto out; + } + if (wordcount == 6) { + if (strcmp(words[3], "split-brain")) { + ret = -1; + goto out; + } + if (!strcmp(words[4], "bigger-file")) { + ret = dict_set_int32(dict, "heal-op", + GF_SHD_OP_SBRAIN_HEAL_FROM_BIGGER_FILE); + if (ret) + goto out; + ret = dict_set_str(dict, "file", (char *)words[5]); + if (ret) + goto out; + goto done; + } + if (!strcmp(words[4], "latest-mtime")) { + ret = dict_set_int32(dict, "heal-op", + GF_SHD_OP_SBRAIN_HEAL_FROM_LATEST_MTIME); + if (ret) + goto out; + ret = dict_set_str(dict, "file", (char *)words[5]); + if (ret) + goto out; + goto done; + } + if (!strcmp(words[4], "source-brick")) { + ret = dict_set_int32(dict, "heal-op", + GF_SHD_OP_SBRAIN_HEAL_FROM_BRICK); + if (ret) + goto out; + ret = set_hostname_path_in_dict(words[5], dict, + GF_SHD_OP_SBRAIN_HEAL_FROM_BRICK); + if (ret) + goto out; + goto done; + } + ret = -1; + goto out; + } + if (wordcount == 7) { + if (!strcmp(words[3], "statistics") && + !strcmp(words[4], "heal-count") && !strcmp(words[5], "replica")) { + ret = dict_set_int32(dict, "heal-op", + GF_SHD_OP_STATISTICS_HEAL_COUNT_PER_REPLICA); + if (ret) + goto out; + ret = set_hostname_path_in_dict( + words[6], dict, GF_SHD_OP_STATISTICS_HEAL_COUNT_PER_REPLICA); + if (ret) + goto out; + goto done; + } + if (!strcmp(words[3], "split-brain") && + !strcmp(words[4], "source-brick")) { + ret = dict_set_int32(dict, "heal-op", + GF_SHD_OP_SBRAIN_HEAL_FROM_BRICK); + ret = set_hostname_path_in_dict(words[5], dict, + GF_SHD_OP_SBRAIN_HEAL_FROM_BRICK); + if (ret) + goto out; + ret = dict_set_str(dict, "file", (char *)words[6]); + if (ret) + goto out; + goto done; + } + } + ret = -1; + goto out; +done: + *options = dict; +out: + if (ret && dict) { + dict_unref(dict); + *options = NULL; + } + + return ret; } +int +cli_cmd_volume_defrag_parse(const char **words, int wordcount, dict_t **options) +{ + dict_t *dict = NULL; + int ret = -1; + char *option = NULL; + char *volname = NULL; + char *command = NULL; + gf_cli_defrag_type cmd = 0; + + GF_ASSERT(words); + GF_ASSERT(options); + + dict = dict_new(); + if (!dict) + goto out; + + if (!((wordcount == 4) || (wordcount == 5))) + goto out; + + if (wordcount == 4) { + if (strcmp(words[3], "start") && strcmp(words[3], "stop") && + strcmp(words[3], "status")) + goto out; + } else { + if (strcmp(words[3], "fix-layout") && strcmp(words[3], "start")) + goto out; + } + + volname = (char *)words[2]; + + if (wordcount == 4) { + command = (char *)words[3]; + } + if (wordcount == 5) { + if ((strcmp(words[3], "fix-layout") || strcmp(words[4], "start")) && + (strcmp(words[3], "start") || strcmp(words[4], "force"))) { + ret = -1; + goto out; + } + command = (char *)words[3]; + option = (char *)words[4]; + } + + if (strcmp(command, "start") == 0) { + cmd = GF_DEFRAG_CMD_START; + if (option && strcmp(option, "force") == 0) { + cmd = GF_DEFRAG_CMD_START_FORCE; + } + goto done; + } + + if (strcmp(command, "fix-layout") == 0) { + cmd = GF_DEFRAG_CMD_START_LAYOUT_FIX; + goto done; + } + if (strcmp(command, "stop") == 0) { + cmd = GF_DEFRAG_CMD_STOP; + goto done; + } + if (strcmp(command, "status") == 0) { + cmd = GF_DEFRAG_CMD_STATUS; + } + +done: + ret = dict_set_str(dict, "volname", volname); + + if (ret) { + gf_log(THIS->name, GF_LOG_ERROR, "failed to set dict"); + goto out; + } + + ret = dict_set_int32(dict, "rebalance-command", (int32_t)cmd); + + if (ret) { + gf_log(THIS->name, GF_LOG_ERROR, "failed to set dict"); + goto out; + } + + *options = dict; + +out: + if (ret && dict) + dict_unref(dict); + + return ret; +} int32_t -cli_cmd_volume_replace_brick_parse (const char **words, int wordcount, - dict_t **options) +cli_snap_create_desc_parse(dict_t *dict, const char **words, size_t wordcount, + int32_t desc_opt_loc) { - dict_t *dict = NULL; - char *volname = NULL; - int ret = -1; - char *op = NULL; - int op_index = 0; - char *delimiter = NULL; - gf1_cli_replace_op replace_op = GF_REPLACE_OP_NONE; + int32_t ret = -1; + char *desc = NULL; + int32_t desc_len = 0; + int len; + + desc = GF_MALLOC(MAX_SNAP_DESCRIPTION_LEN + 1, gf_common_mt_char); + if (!desc) { + ret = -1; + goto out; + } + + len = strlen(words[desc_opt_loc]); + if (len >= MAX_SNAP_DESCRIPTION_LEN) { + cli_out( + "snapshot create: description truncated: " + "Description provided is longer than 1024 characters"); + desc_len = MAX_SNAP_DESCRIPTION_LEN; + } else { + desc_len = len; + } + + snprintf(desc, desc_len + 1, "%s", words[desc_opt_loc]); + /* Calculating the size of the description as given by the user */ + + ret = dict_set_dynstr(dict, "description", desc); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Unable to save snap " + "description"); + goto out; + } + + ret = 0; +out: + if (ret && desc) + GF_FREE(desc); - GF_ASSERT (words); - GF_ASSERT (options); + return ret; +} - GF_ASSERT ((strcmp (words[0], "volume")) == 0); - GF_ASSERT ((strcmp (words[1], "replace-brick")) == 0); +/* Function to check whether the Volume name is repeated */ +int +cli_check_if_volname_repeated(const char **words, unsigned int start_index, + uint64_t cur_index) +{ + uint64_t i = -1; + int ret = 0; - dict = dict_new (); + GF_ASSERT(words); - if (!dict) - goto out; + for (i = start_index; i < cur_index; i++) { + if (strcmp(words[i], words[cur_index]) == 0) { + ret = -1; + goto out; + } + } +out: + return ret; +} - if (wordcount < 3) - goto out; +/* snapshot clone <clonename> <snapname> + * @arg-0, dict : Request Dictionary to be sent to server side. + * @arg-1, words : Contains individual words of CLI command. + * @arg-2, wordcount: Contains number of words present in the CLI command. + * + * return value : -1 on failure + * 0 on success + */ +int +cli_snap_clone_parse(dict_t *dict, const char **words, int wordcount) +{ + uint64_t i = 0; + int ret = -1; + char *clonename = NULL; + unsigned int cmdi = 2; + /* cmdi is command index, here cmdi is "2" (gluster snapshot clone)*/ + + GF_ASSERT(words); + GF_ASSERT(dict); + + if (wordcount == cmdi + 1) { + cli_err("Invalid Syntax."); + gf_log("cli", GF_LOG_ERROR, + "Invalid number of words for snap clone command"); + goto out; + } + + if (strlen(words[cmdi]) >= GLUSTERD_MAX_SNAP_NAME) { + cli_err( + "snapshot clone: failed: clonename cannot exceed " + "255 characters."); + gf_log("cli", GF_LOG_ERROR, "Clone name too long"); + + goto out; + } + + clonename = (char *)words[cmdi]; + for (i = 0; i < strlen(clonename); i++) { + /* Following volume name convention */ + if (!isalnum(clonename[i]) && + (clonename[i] != '_' && (clonename[i] != '-'))) { + /* TODO : Is this message enough?? */ + cli_err( + "Clonename can contain only alphanumeric, " + "\"-\" and \"_\" characters"); + goto out; + } + } + + ret = dict_set_int32(dict, "volcount", 1); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not save volcount"); + goto out; + } + + ret = dict_set_str(dict, "clonename", (char *)words[cmdi]); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Could not save clone " + "name(%s)", + (char *)words[cmdi]); + goto out; + } + + /* Filling snap name in the dictionary */ + ret = dict_set_str(dict, "snapname", (char *)words[cmdi + 1]); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Could not " + "save snap name(%s)", + (char *)words[cmdi + 1]); + goto out; + } + + ret = 0; - volname = (char *)words[2]; +out: + return ret; +} - GF_ASSERT (volname); +/* snapshot create <snapname> <vol-name(s)> [description <description>] + * [force] + * @arg-0, dict : Request Dictionary to be sent to server side. + * @arg-1, words : Contains individual words of CLI command. + * @arg-2, wordcount: Contains number of words present in the CLI command. + * + * return value : -1 on failure + * 0 on success + */ +int +cli_snap_create_parse(dict_t *dict, const char **words, int wordcount) +{ + uint64_t i = 0; + int ret = -1; + uint64_t volcount = 0; + char key[PATH_MAX] = ""; + char *snapname = NULL; + unsigned int cmdi = 2; + int flags = 0; + /* cmdi is command index, here cmdi is "2" (gluster snapshot create)*/ + + GF_ASSERT(words); + GF_ASSERT(dict); + + if (wordcount <= cmdi + 1) { + cli_err("Invalid Syntax."); + gf_log("cli", GF_LOG_ERROR, "Too less words for snap create command"); + goto out; + } + + if (strlen(words[cmdi]) >= GLUSTERD_MAX_SNAP_NAME) { + cli_err( + "snapshot create: failed: snapname cannot exceed " + "255 characters."); + gf_log("cli", GF_LOG_ERROR, "Snapname too long"); + + goto out; + } + + snapname = (char *)words[cmdi]; + for (i = 0; i < strlen(snapname); i++) { + /* Following volume name convention */ + if (!isalnum(snapname[i]) && + (snapname[i] != '_' && (snapname[i] != '-'))) { + /* TODO : Is this message enough?? */ + cli_err( + "Snapname can contain only alphanumeric, " + "\"-\" and \"_\" characters"); + goto out; + } + } + + ret = dict_set_str(dict, "snapname", (char *)words[cmdi]); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Could not save snap " + "name(%s)", + (char *)words[cmdi]); + goto out; + } + + /* Filling volume name in the dictionary */ + for (i = cmdi + 1; + i < wordcount && (strcmp(words[i], "description")) != 0 && + (strcmp(words[i], "force") != 0) && + (strcmp(words[i], "no-timestamp") != 0); + i++) { + volcount++; + /* volume index starts from 1 */ + ret = snprintf(key, sizeof(key), "volname%" PRIu64, volcount); + if (ret < 0) { + goto out; + } - ret = dict_set_str (dict, "volname", volname); + ret = dict_set_str(dict, key, (char *)words[i]); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Could not " + "save volume name(%s)", + (char *)words[i]); + goto out; + } - if (ret) - goto out; + if (i >= cmdi + 2) { + ret = -1; + cli_err( + "Creating multiple volume snapshot is not " + "supported as of now"); + goto out; + } + /* TODO : remove this above condition check once + * multiple volume snapshot is supported */ + } + + if (volcount == 0) { + ret = -1; + cli_err("Please provide the volume name"); + gf_log("cli", GF_LOG_ERROR, "Invalid Syntax"); + goto out; + } + + ret = dict_set_int32(dict, "volcount", volcount); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not save volcount"); + goto out; + } + + /* Verify how we got out of "for" loop, + * if it is by reaching wordcount limit then goto "out", + * because we need not parse for "description","force" and + * "no-timestamp" after this. + */ + if (i == wordcount) { + goto out; + } + + if (strcmp(words[i], "no-timestamp") == 0) { + ret = dict_set_int32n(dict, "no-timestamp", SLEN("no-timestamp"), 1); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Could not save " + "time-stamp option"); + } + if (i == (wordcount - 1)) + goto out; + i++; + } + + if ((strcmp(words[i], "description")) == 0) { + ++i; + if (i > (wordcount - 1)) { + ret = -1; + cli_err("Please provide a description"); + gf_log("cli", GF_LOG_ERROR, "Description not provided"); + goto out; + } - if (wordcount < 4) { - ret = -1; - goto out; + ret = cli_snap_create_desc_parse(dict, words, wordcount, i); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Could not save snap " + "description"); + goto out; } - delimiter = strchr ((char *)words[3], ':'); - if (delimiter && delimiter != words[3] - && *(delimiter+1) == '/') { - ret = dict_set_str (dict, "src-brick", (char *)words[3]); + if (i == (wordcount - 1)) + goto out; + i++; + /* point the index to next word. + * As description might be follwed by force option. + * Before that, check if wordcount limit is reached + */ + } + + if (strcmp(words[i], "force") == 0) { + flags = GF_CLI_FLAG_OP_FORCE; + + } else { + ret = -1; + cli_err("Invalid Syntax."); + gf_log("cli", GF_LOG_ERROR, "Invalid Syntax"); + goto out; + } + + /* Check if the command has anything after "force" keyword */ + if (++i < wordcount) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Invalid Syntax"); + goto out; + } + + ret = 0; - if (ret) - goto out; +out: + if (ret == 0) { + /*Adding force flag in either of the case i.e force set + * or unset*/ + ret = dict_set_int32(dict, "flags", flags); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Could not save " + "snap force option"); + } + } + return ret; +} - if (wordcount < 5) { - ret = -1; - goto out; - } +/* snapshot list [volname] + * @arg-0, dict : Request Dictionary to be sent to server side. + * @arg-1, words : Contains individual words of CLI command. + * @arg-2, wordcount: Contains number of words present in the CLI command. + * + * return value : -1 on failure + * 0 on success + */ +int +cli_snap_list_parse(dict_t *dict, const char **words, int wordcount) +{ + int ret = -1; + + GF_ASSERT(words); + GF_ASSERT(dict); + + if (wordcount < 2 || wordcount > 3) { + gf_log("cli", GF_LOG_ERROR, "Invalid Syntax"); + goto out; + } + + if (wordcount == 2) { + ret = 0; + goto out; + } + + ret = dict_set_str(dict, "volname", (char *)words[2]); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to save volname in dictionary"); + goto out; + } +out: + return ret; +} - delimiter = strchr ((char *)words[4], ':'); - if (!delimiter || delimiter == words[4] - || *(delimiter+1) != '/') { - gf_log ("cli", GF_LOG_ERROR, - "wrong brick type, use <HOSTNAME>:<export-dir>"); - ret = -1; - goto out; - } +/* snapshot info [(snapname | volume <volname>)] + * @arg-0, dict : Request Dictionary to be sent to server side. + * @arg-1, words : Contains individual words of CLI command. + * @arg-2, wordcount: Contains number of words present in the CLI command. + * + * return value : -1 on failure + * 0 on success + */ +int +cli_snap_info_parse(dict_t *dict, const char **words, int wordcount) +{ + int ret = -1; + int32_t cmd = GF_SNAP_INFO_TYPE_ALL; + unsigned int cmdi = 2; + /* cmdi is command index, here cmdi is "2" (gluster snapshot info)*/ + + GF_ASSERT(words); + GF_ASSERT(dict); + + if (wordcount > 4 || wordcount < cmdi) { + gf_log("cli", GF_LOG_ERROR, "Invalid syntax"); + goto out; + } + + if (wordcount == cmdi) { + ret = 0; + goto out; + } + + /* If 3rd word is not "volume", then it must + * be snapname. + */ + if (strcmp(words[cmdi], "volume") != 0) { + ret = dict_set_str(dict, "snapname", (char *)words[cmdi]); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Unable to save " + "snapname %s", + words[cmdi]); + goto out; + } - ret = dict_set_str (dict, "dst-brick", (char *)words[4]); + /* Once snap name is parsed, if we encounter any other + * word then fail it. Invalid Syntax. + * example : snapshot info <snapname> word + */ + if ((cmdi + 1) != wordcount) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Invalid Syntax"); + goto out; + } - if (ret) - goto out; + cmd = GF_SNAP_INFO_TYPE_SNAP; + ret = 0; + goto out; + /* No need to continue the parsing once we + * get the snapname + */ + } + + /* If 3rd word is "volume", then check if next word + * is present. As, "snapshot info volume" is an + * invalid command. + */ + if ((cmdi + 1) == wordcount) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Invalid Syntax"); + goto out; + } + + ret = dict_set_str(dict, "volname", (char *)words[wordcount - 1]); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Could not save " + "volume name %s", + words[wordcount - 1]); + goto out; + } + cmd = GF_SNAP_INFO_TYPE_VOL; +out: + if (ret == 0) { + ret = dict_set_int32(dict, "sub-cmd", cmd); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Could not save " + "type of snapshot info"); + } + } + return ret; +} - op_index = 5; +/* snapshot restore <snapname> + * @arg-0, dict : Request Dictionary to be sent to server side. + * @arg-1, words : Contains individual words of CLI command. + * @arg-2, wordcount: Contains number of words present in the CLI command. + * + * return value : -1 on failure + * 0 on success + */ +int +cli_snap_restore_parse(dict_t *dict, const char **words, int wordcount, + struct cli_state *state) +{ + int ret = -1; + const char *question = NULL; + gf_answer_t answer = GF_ANSWER_NO; + + GF_ASSERT(words); + GF_ASSERT(dict); + + if (wordcount != 3) { + gf_log("cli", GF_LOG_ERROR, "Invalid Syntax"); + goto out; + } + + ret = dict_set_str(dict, "snapname", (char *)words[2]); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to save snap-name %s", words[2]); + goto out; + } + + question = + "Restore operation will replace the " + "original volume with the snapshotted volume. " + "Do you still want to continue?"; + + answer = cli_cmd_get_confirmation(state, question); + if (GF_ANSWER_NO == answer) { + ret = 1; + gf_log("cli", GF_LOG_ERROR, + "User cancelled a snapshot " + "restore operation for snap %s", + (char *)words[2]); + goto out; + } +out: + return ret; +} + +/* snapshot activate <snapname> [force] + * @arg-0, dict : Request Dictionary to be sent to server side. + * @arg-1, words : Contains individual words of CLI command. + * @arg-2, wordcount: Contains number of words present in the CLI command. + * + * return value : -1 on failure + * 0 on success + */ +int +cli_snap_activate_parse(dict_t *dict, const char **words, int wordcount) +{ + int ret = -1; + int flags = 0; + + GF_ASSERT(words); + GF_ASSERT(dict); + + if ((wordcount < 3) || (wordcount > 4)) { + gf_log("cli", GF_LOG_ERROR, "Invalid Syntax"); + goto out; + } + + ret = dict_set_str(dict, "snapname", (char *)words[2]); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to save snap-name %s", words[2]); + goto out; + } + + if (wordcount == 4) { + if (!strcmp("force", (char *)words[3])) { + flags = GF_CLI_FLAG_OP_FORCE; } else { - op_index = 3; + gf_log("cli", GF_LOG_ERROR, "Invalid option"); + ret = -1; + goto out; } + } + ret = dict_set_int32(dict, "flags", flags); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to save force option"); + goto out; + } +out: + return ret; +} - if (wordcount < (op_index + 1)) { - ret = -1; - goto out; - } +/* snapshot deactivate <snapname> + * @arg-0, dict : Request Dictionary to be sent to server side. + * @arg-1, words : Contains individual words of CLI command. + * @arg-2, wordcount: Contains number of words present in the CLI command. + * + * return value : -1 on failure + * 0 on success + * 1 if user cancelled the request + */ +int +cli_snap_deactivate_parse(dict_t *dict, const char **words, int wordcount, + struct cli_state *state) +{ + int ret = -1; + gf_answer_t answer = GF_ANSWER_NO; + const char *question = + "Deactivating snap will make its " + "data inaccessible. Do you want to " + "continue?"; + + GF_ASSERT(words); + GF_ASSERT(dict); + + if ((wordcount != 3)) { + gf_log("cli", GF_LOG_ERROR, "Invalid Syntax"); + goto out; + } + + ret = dict_set_str(dict, "snapname", (char *)words[2]); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to save snap-name %s", words[2]); + goto out; + } + + answer = cli_cmd_get_confirmation(state, question); + if (GF_ANSWER_NO == answer) { + ret = 1; + gf_log("cli", GF_LOG_DEBUG, + "User cancelled " + "snapshot deactivate operation"); + goto out; + } - op = (char *) words[op_index]; +out: + return ret; +} - if (!strcasecmp ("start", op)) { - replace_op = GF_REPLACE_OP_START; - } else if (!strcasecmp ("commit", op)) { - replace_op = GF_REPLACE_OP_COMMIT; - } else if (!strcasecmp ("pause", op)) { - replace_op = GF_REPLACE_OP_PAUSE; - } else if (!strcasecmp ("abort", op)) { - replace_op = GF_REPLACE_OP_ABORT; - } else if (!strcasecmp ("status", op)) { - replace_op = GF_REPLACE_OP_STATUS; +/* snapshot delete (all | snapname | volume <volname>) + * @arg-0, dict : Request Dictionary to be sent to server side. + * @arg-1, words : Contains individual words of CLI command. + * @arg-2, wordcount: Contains number of words present in the CLI command. + * + * return value : -1 on failure + * 0 on success + * 1 if user cancel the operation + */ +int +cli_snap_delete_parse(dict_t *dict, const char **words, int wordcount, + struct cli_state *state) +{ + int ret = -1; + const char *question = NULL; + int32_t cmd = -1; + unsigned int cmdi = 2; + gf_answer_t answer = GF_ANSWER_NO; + + GF_ASSERT(words); + GF_ASSERT(dict); + + if (wordcount > 4 || wordcount <= cmdi) { + gf_log("cli", GF_LOG_ERROR, "Invalid Syntax"); + goto out; + } + + question = + "Deleting snap will erase all the information about " + "the snap. Do you still want to continue?"; + + if (strcmp(words[cmdi], "all") == 0) { + ret = 0; + cmd = GF_SNAP_DELETE_TYPE_ALL; + } else if (strcmp(words[cmdi], "volume") == 0) { + if (++cmdi == wordcount) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Invalid Syntax"); + goto out; } - if (replace_op == GF_REPLACE_OP_NONE) { - ret = -1; - goto out; + ret = dict_set_str(dict, "volname", (char *)words[cmdi]); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Could not save " + "volume name %s", + words[wordcount - 1]); + goto out; } + cmd = GF_SNAP_DELETE_TYPE_VOL; + } else { + ret = dict_set_str(dict, "snapname", (char *)words[cmdi]); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Unable to save " + "snapname %s", + words[2]); + goto out; + } + cmd = GF_SNAP_DELETE_TYPE_SNAP; + } + + if ((cmdi + 1) != wordcount) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Invalid Syntax"); + goto out; + } + + if (cmd == GF_SNAP_DELETE_TYPE_SNAP) { + answer = cli_cmd_get_confirmation(state, question); + if (GF_ANSWER_NO == answer) { + ret = 1; + gf_log("cli", GF_LOG_DEBUG, + "User cancelled " + "snapshot delete operation for snap %s", + (char *)words[2]); + goto out; + } + } + + ret = dict_set_int32(dict, "sub-cmd", cmd); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Could not save " + "type of snapshot delete"); + } +out: + return ret; +} - ret = dict_set_int32 (dict, "operation", (int32_t) replace_op); +/* snapshot status [(snapname | volume <volname>)] + * @arg-0, dict : Request Dictionary to be sent to server side. + * @arg-1, words : Contains individual words of CLI command. + * @arg-2, wordcount: Contains number of words present in the CLI command. + * + * return value : -1 on failure + * 0 on success + */ +int +cli_snap_status_parse(dict_t *dict, const char **words, int wordcount) +{ + int ret = -1; + int32_t cmd = GF_SNAP_STATUS_TYPE_ALL; + unsigned int cmdi = 2; + /* cmdi is command index, here cmdi is "2" (gluster snapshot status)*/ + + GF_ASSERT(words); + GF_ASSERT(dict); + + if (wordcount > 4 || wordcount < cmdi) { + gf_log("cli", GF_LOG_ERROR, "Invalid Syntax"); + goto out; + } + + if (wordcount == cmdi) { + ret = 0; + goto out; + } + + /* if 3rd word is not "volume", then it must be "snapname" + */ + if (strcmp(words[cmdi], "volume") != 0) { + ret = dict_set_str(dict, "snapname", (char *)words[cmdi]); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Count not save " + "snap name %s", + words[cmdi]); + goto out; + } - if (ret) - goto out; + if ((cmdi + 1) != wordcount) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Invalid Syntax"); + goto out; + } - *options = dict; + ret = 0; + cmd = GF_SNAP_STATUS_TYPE_SNAP; + goto out; + } + + /* If 3rd word is "volume", then check if next word is present. + * As, "snapshot info volume" is an invalid command + */ + if ((cmdi + 1) == wordcount) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Invalid Syntax"); + goto out; + } + + ret = dict_set_str(dict, "volname", (char *)words[wordcount - 1]); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Count not save " + "volume name %s", + words[wordcount - 1]); + goto out; + } + cmd = GF_SNAP_STATUS_TYPE_VOL; out: + if (ret == 0) { + ret = dict_set_int32(dict, "sub-cmd", cmd); if (ret) { - gf_log ("cli", GF_LOG_ERROR, "Unable to parse remove-brick CLI"); - if (dict) - dict_destroy (dict); + gf_log("cli", GF_LOG_ERROR, + "Could not save cmd " + "of snapshot status"); } + } - return ret; + return ret; } +/* return value: + * -1 in case of failure. + * 0 in case of success. + */ int32_t -cli_cmd_log_filename_parse (const char **words, int wordcount, dict_t **options) +cli_snap_config_limit_parse(const char **words, dict_t *dict, + unsigned int wordcount, unsigned int index, + char *key) { - dict_t *dict = NULL; - char *volname = NULL; - char *str = NULL; - int ret = -1; + int ret = -1; + int limit = 0; + char *end_ptr = NULL; + + GF_ASSERT(words); + GF_ASSERT(dict); + GF_ASSERT(key); + + if (index >= wordcount) { + ret = -1; + cli_err("Please provide a value for %s.", key); + gf_log("cli", GF_LOG_ERROR, "Value not provided for %s", key); + goto out; + } + + limit = strtol(words[index], &end_ptr, 10); + + if (limit <= 0 || strcmp(end_ptr, "") != 0) { + ret = -1; + cli_err( + "Please enter an integer value " + "greater than zero for %s", + key); + goto out; + } + + ret = dict_set_int32(dict, key, limit); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Could not set " + "%s in dictionary", + key); + goto out; + } + + ret = dict_set_dynstr_with_alloc(dict, "globalname", "All"); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not set global key"); + goto out; + } + ret = dict_set_int32(dict, "hold_global_locks", _gf_true); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not set global locks"); + goto out; + } - GF_ASSERT (words); - GF_ASSERT (options); +out: + return ret; +} - GF_ASSERT ((strcmp (words[0], "volume")) == 0); - GF_ASSERT ((strcmp (words[1], "log")) == 0); - GF_ASSERT ((strcmp (words[2], "filename")) == 0); +/* function cli_snap_config_parse + * Config Syntax : gluster snapshot config [volname] + * [snap-max-hard-limit <count>] + * [snap-max-soft-limit <count>] + * + return value: <0 on failure + 1 if user cancels the operation, or limit value is out of + range + 0 on success + + NOTE : snap-max-soft-limit can only be set for system. +*/ +int32_t +cli_snap_config_parse(const char **words, int wordcount, dict_t *dict, + struct cli_state *state) +{ + int ret = -1; + gf_answer_t answer = GF_ANSWER_NO; + gf_boolean_t vol_presence = _gf_false; + struct snap_config_opt_vals_ *conf_vals = NULL; + int8_t hard_limit = 0; + int8_t soft_limit = 0; + int8_t config_type = -1; + const char *question = NULL; + unsigned int cmdi = 2; + /* cmdi is command index, here cmdi is "2" (gluster snapshot config)*/ + + GF_ASSERT(words); + GF_ASSERT(dict); + GF_ASSERT(state); + + if ((wordcount < 2) || (wordcount > 7)) { + gf_log("cli", GF_LOG_ERROR, "Invalid wordcount(%d)", wordcount); + goto out; + } + + if (wordcount == 2) { + config_type = GF_SNAP_CONFIG_DISPLAY; + ret = 0; + goto set; + } + + /* auto-delete cannot be a volume name */ + /* Check whether the 3rd word is volname */ + if (strcmp(words[cmdi], "snap-max-hard-limit") != 0 && + strcmp(words[cmdi], "snap-max-soft-limit") != 0 && + strcmp(words[cmdi], "auto-delete") != 0 && + strcmp(words[cmdi], "activate-on-create") != 0) { + ret = dict_set_str(dict, "volname", (char *)words[cmdi]); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to set volname"); + goto out; + } + cmdi++; + vol_presence = _gf_true; - dict = dict_new (); - if (!dict) - goto out; + if (cmdi == wordcount) { + config_type = GF_SNAP_CONFIG_DISPLAY; + ret = 0; + goto set; + } + } - volname = (char *)words[3]; - GF_ASSERT (volname); + config_type = GF_SNAP_CONFIG_TYPE_SET; - ret = dict_set_str (dict, "volname", volname); - if (ret) - goto out; + if (strcmp(words[cmdi], "snap-max-hard-limit") == 0) { + ret = cli_snap_config_limit_parse(words, dict, wordcount, ++cmdi, + "snap-max-hard-limit"); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to parse snap " + "config hard limit"); + goto out; + } + hard_limit = 1; - str = (char *)words[4]; - if (strchr (str, ':')) { - ret = dict_set_str (dict, "brick", str); - if (ret) - goto out; - /* Path */ - str = (char *)words[5]; - ret = dict_set_str (dict, "path", str); - if (ret) - goto out; - } else { - ret = dict_set_str (dict, "path", str); - if (ret) - goto out; + if (++cmdi == wordcount) { + ret = 0; + goto set; + } + } + + if (strcmp(words[cmdi], "snap-max-soft-limit") == 0) { + if (vol_presence == 1) { + ret = -1; + cli_err( + "Soft limit cannot be set to individual " + "volumes."); + gf_log("cli", GF_LOG_ERROR, + "Soft limit cannot be " + "set to volumes"); + goto out; } - *options = dict; + ret = cli_snap_config_limit_parse(words, dict, wordcount, ++cmdi, + "snap-max-soft-limit"); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to parse snap " + "config soft limit"); + goto out; + } + + if (++cmdi != wordcount) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Invalid Syntax"); + goto out; + } + soft_limit = 1; + } + + if (hard_limit || soft_limit) + goto set; + + if (strcmp(words[cmdi], "auto-delete") == 0) { + if (vol_presence == 1) { + ret = -1; + cli_err( + "As of now, auto-delete option cannot be set " + "to volumes"); + gf_log("cli", GF_LOG_ERROR, + "auto-delete option " + "cannot be set to volumes"); + goto out; + } + + if (++cmdi >= wordcount) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Invalid Syntax"); + goto out; + } + + ret = dict_set_str(dict, "auto-delete", (char *)words[cmdi]); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to set " + "value of auto-delete in request " + "dictionary"); + goto out; + } + + if (++cmdi != wordcount) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Invalid Syntax"); + goto out; + } + } else if (strcmp(words[cmdi], "activate-on-create") == 0) { + if (vol_presence == 1) { + ret = -1; + cli_err( + "As of now, activate-on-create option " + "cannot be set to volumes"); + gf_log("cli", GF_LOG_ERROR, + "activate-on-create " + "option cannot be set to volumes"); + goto out; + } + + if (++cmdi >= wordcount) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Invalid Syntax"); + goto out; + } + + ret = dict_set_str(dict, "snap-activate-on-create", + (char *)words[cmdi]); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to set value " + "of activate-on-create in request dictionary"); + goto out; + } + + if (++cmdi != wordcount) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Invalid Syntax"); + goto out; + } + } else { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Invalid Syntax"); + goto out; + } + + ret = 0; /* Success */ + +set: + ret = dict_set_int32(dict, "config-command", config_type); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Unable to set " + "config-command"); + goto out; + } + + if (config_type == GF_SNAP_CONFIG_TYPE_SET && (hard_limit || soft_limit)) { + conf_vals = snap_confopt_vals; + if (hard_limit && soft_limit) { + question = conf_vals[GF_SNAP_CONFIG_SET_BOTH].question; + } else if (soft_limit) { + question = conf_vals[GF_SNAP_CONFIG_SET_SOFT].question; + } else if (hard_limit) { + question = conf_vals[GF_SNAP_CONFIG_SET_HARD].question; + } + + answer = cli_cmd_get_confirmation(state, question); + if (GF_ANSWER_NO == answer) { + ret = 1; + gf_log("cli", GF_LOG_DEBUG, + "User cancelled " + "snapshot config operation"); + } + } out: - if (ret && dict) - dict_destroy (dict); + return ret; +} - return ret; +int +validate_op_name(const char *op, const char *opname, char **opwords) +{ + int ret = -1; + int i = 0; + + GF_ASSERT(opname); + GF_ASSERT(opwords); + + for (i = 0; opwords[i] != NULL; i++) { + if (strcmp(opwords[i], opname) == 0) { + cli_out("\"%s\" cannot be a %s", opname, op); + goto out; + } + } + ret = 0; +out: + return ret; } int32_t -cli_cmd_log_locate_parse (const char **words, int wordcount, dict_t **options) +cli_cmd_snapshot_parse(const char **words, int wordcount, dict_t **options, + struct cli_state *state) { - dict_t *dict = NULL; - char *volname = NULL; - char *str = NULL; - int ret = -1; + int32_t ret = -1; + dict_t *dict = NULL; + gf1_cli_snapshot type = GF_SNAP_OPTION_TYPE_NONE; + char *w = NULL; + static char *opwords[] = {"create", "delete", "restore", "activate", + "deactivate", "list", "status", "config", + "info", "clone", NULL}; + static char *invalid_snapnames[] = {"description", "force", "volume", "all", + NULL}; + static char *invalid_volnames[] = {"volume", + "type", + "subvolumes", + "option", + "end-volume", + "all", + "volume_not_in_ring", + "description", + "force", + "snap-max-hard-limit", + "snap-max-soft-limit", + "auto-delete", + "activate-on-create", + NULL}; + + GF_ASSERT(words); + GF_ASSERT(options); + GF_ASSERT(state); + + dict = dict_new(); + if (!dict) + goto out; + + /* Lowest wordcount possible */ + if (wordcount < 2) { + gf_log("", GF_LOG_ERROR, "Invalid command: Not enough arguments"); + goto out; + } + + w = str_getunamb(words[1], opwords); + if (!w) { + /* Checks if the operation is a valid operation */ + gf_log("", GF_LOG_ERROR, "Opword Mismatch"); + goto out; + } + + if (!strcmp(w, "create")) { + type = GF_SNAP_OPTION_TYPE_CREATE; + } else if (!strcmp(w, "list")) { + type = GF_SNAP_OPTION_TYPE_LIST; + } else if (!strcmp(w, "info")) { + type = GF_SNAP_OPTION_TYPE_INFO; + } else if (!strcmp(w, "delete")) { + type = GF_SNAP_OPTION_TYPE_DELETE; + } else if (!strcmp(w, "config")) { + type = GF_SNAP_OPTION_TYPE_CONFIG; + } else if (!strcmp(w, "restore")) { + type = GF_SNAP_OPTION_TYPE_RESTORE; + } else if (!strcmp(w, "status")) { + type = GF_SNAP_OPTION_TYPE_STATUS; + } else if (!strcmp(w, "activate")) { + type = GF_SNAP_OPTION_TYPE_ACTIVATE; + } else if (!strcmp(w, "deactivate")) { + type = GF_SNAP_OPTION_TYPE_DEACTIVATE; + } else if (!strcmp(w, "clone")) { + type = GF_SNAP_OPTION_TYPE_CLONE; + } + + if (type != GF_SNAP_OPTION_TYPE_CONFIG && + type != GF_SNAP_OPTION_TYPE_STATUS) { + ret = dict_set_int32(dict, "hold_snap_locks", _gf_true); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Unable to set hold-snap-locks value " + "as _gf_true"); + goto out; + } + } - GF_ASSERT (words); - GF_ASSERT (options); + /* Following commands does not require volume locks */ + if (type == GF_SNAP_OPTION_TYPE_STATUS || + type == GF_SNAP_OPTION_TYPE_ACTIVATE || + type == GF_SNAP_OPTION_TYPE_DEACTIVATE) { + ret = dict_set_int32(dict, "hold_vol_locks", _gf_false); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Setting volume lock " + "flag failed"); + goto out; + } + } + + /* Check which op is intended */ + switch (type) { + case GF_SNAP_OPTION_TYPE_CREATE: + /* Syntax : + * gluster snapshot create <snapname> <vol-name(s)> + * [no-timestamp] + * [description <description>] + * [force] + */ + /* In cases where the snapname is not given then + * parsing fails & snapname cannot be "description", + * "force" and "volume", that check is made here + */ + if (wordcount == 2) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Invalid Syntax"); + goto out; + } - GF_ASSERT ((strcmp (words[0], "volume")) == 0); - GF_ASSERT ((strcmp (words[1], "log")) == 0); - GF_ASSERT ((strcmp (words[2], "locate")) == 0); + ret = validate_op_name("snapname", words[2], invalid_snapnames); + if (ret) { + goto out; + } - dict = dict_new (); - if (!dict) + ret = cli_snap_create_parse(dict, words, wordcount); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "create command parsing failed."); goto out; + } + break; + + case GF_SNAP_OPTION_TYPE_CLONE: + /* Syntax : + * gluster snapshot clone <clonename> <snapname> + */ + /* In cases where the clonename is not given then + * parsing fails & snapname cannot be "description", + * "force" and "volume", that check is made here + */ + if (wordcount == 2) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Invalid Syntax"); + goto out; + } - volname = (char *)words[3]; - GF_ASSERT (volname); + ret = validate_op_name("clonename", words[2], invalid_volnames); + if (ret) { + goto out; + } - ret = dict_set_str (dict, "volname", volname); - if (ret) + ret = cli_snap_clone_parse(dict, words, wordcount); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "clone command parsing failed."); goto out; + } + break; + + case GF_SNAP_OPTION_TYPE_INFO: + /* Syntax : + * gluster snapshot info [(snapname] | [vol <volname>)] + */ + ret = cli_snap_info_parse(dict, words, wordcount); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to parse " + "snapshot info command"); + goto out; + } + break; + + case GF_SNAP_OPTION_TYPE_LIST: + /* Syntax : + * gluster snaphsot list [volname] + */ + + ret = cli_snap_list_parse(dict, words, wordcount); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to parse " + "snapshot list command"); + goto out; + } + break; + + case GF_SNAP_OPTION_TYPE_DELETE: + /* Syntax : + * snapshot delete (all | snapname | volume <volname>) + */ + ret = cli_snap_delete_parse(dict, words, wordcount, state); + if (ret) { + /* A positive ret value means user cancelled + * the command */ + if (ret < 0) { + gf_log("cli", GF_LOG_ERROR, + "Failed to parse " + "snapshot delete command"); + } + goto out; + } + break; + + case GF_SNAP_OPTION_TYPE_CONFIG: + /* snapshot config [volname] [snap-max-hard-limit <count>] + * [snap-max-soft-limit <percent>] */ + ret = cli_snap_config_parse(words, wordcount, dict, state); + if (ret) { + if (ret < 0) + gf_log("cli", GF_LOG_ERROR, + "config command parsing failed."); + goto out; + } - str = (char *)words[4]; - if (str && strchr (str, ':')) { - ret = dict_set_str (dict, "brick", str); - if (ret) - goto out; + ret = dict_set_int32(dict, "type", GF_SNAP_OPTION_TYPE_CONFIG); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Unable to set " + "config type"); + ret = -1; + goto out; + } + break; + + case GF_SNAP_OPTION_TYPE_STATUS: { + /* Syntax : + * gluster snapshot status [(snapname | + * volume <volname>)] + */ + ret = cli_snap_status_parse(dict, words, wordcount); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to parse " + "snapshot status command"); + goto out; + } + break; } + case GF_SNAP_OPTION_TYPE_RESTORE: + /* Syntax: + * snapshot restore <snapname> + */ + ret = cli_snap_restore_parse(dict, words, wordcount, state); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to parse " + "restore command"); + goto out; + } + break; + + case GF_SNAP_OPTION_TYPE_ACTIVATE: + /* Syntax: + * snapshot activate <snapname> [force] + */ + ret = cli_snap_activate_parse(dict, words, wordcount); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to parse " + "start command"); + goto out; + } + break; + case GF_SNAP_OPTION_TYPE_DEACTIVATE: + /* Syntax: + * snapshot deactivate <snapname> + */ + ret = cli_snap_deactivate_parse(dict, words, wordcount, state); + if (ret) { + /* A positive ret value means user cancelled + * the command */ + if (ret < 0) { + gf_log("cli", GF_LOG_ERROR, + "Failed to parse deactivate " + "command"); + } + goto out; + } + break; + + default: + ret = -1; + gf_log("", GF_LOG_ERROR, "Opword Mismatch"); + goto out; + } + + ret = dict_set_int32(dict, "type", type); + if (ret) { + gf_log("", GF_LOG_ERROR, "Failed to set type."); + goto out; + } + /* If you got so far, input is valid */ + ret = 0; +out: + if (ret) { + if (dict) + dict_unref(dict); + } else *options = dict; -out: - if (ret && dict) - dict_destroy (dict); + return ret; +} + +int +cli_cmd_validate_volume(char *volname) +{ + int i = 0; + int ret = -1; + int volname_len; + + if (volname[0] == '-') + return ret; + + if (!strcmp(volname, "all")) { + cli_err("\"all\" cannot be the name of a volume."); + return ret; + } + + if (strchr(volname, '/')) { + cli_err("Volume name should not contain \"/\" character."); + return ret; + } + volname_len = strlen(volname); + if (volname_len > GD_VOLUME_NAME_MAX) { + cli_err("Volname can not exceed %d characters.", GD_VOLUME_NAME_MAX); return ret; + } + + for (i = 0; i < volname_len; i++) + if (!isalnum(volname[i]) && (volname[i] != '_') && + (volname[i] != '-')) { + cli_err( + "Volume name should not contain \"%c\"" + " character.\nVolume names can only" + "contain alphanumeric, '-' and '_' " + "characters.", + volname[i]); + return ret; + } + + ret = 0; + + return ret; } int32_t -cli_cmd_log_rotate_parse (const char **words, int wordcount, dict_t **options) +cli_cmd_bitrot_parse(const char **words, int wordcount, dict_t **options) { - dict_t *dict = NULL; - char *volname = NULL; - char *str = NULL; - int ret = -1; + int32_t ret = -1; + char *w = NULL; + char *volname = NULL; + static char *opwords[] = {"enable", "disable", "scrub-throttle", + "scrub-frequency", "scrub", "signing-time", + "signer-threads", NULL}; + static char *scrub_throt_values[] = {"lazy", "normal", "aggressive", NULL}; + static char *scrub_freq_values[] = { + "hourly", "daily", "weekly", "biweekly", "monthly", "minute", NULL}; + static char *scrub_values[] = {"pause", "resume", "status", "ondemand", + NULL}; + dict_t *dict = NULL; + gf_bitrot_type type = GF_BITROT_OPTION_TYPE_NONE; + int32_t expiry_time = 0; + int32_t signer_th_count = 0; + + GF_ASSERT(words); + GF_ASSERT(options); + + /* Hack to print out bitrot help properly */ + if ((wordcount == 3) && !(strcmp(words[2], "help"))) { + ret = 1; + return ret; + } + + if (wordcount < 4 || wordcount > 5) { + gf_log("cli", GF_LOG_ERROR, "Invalid syntax"); + goto out; + } + + dict = dict_new(); + if (!dict) + goto out; + + volname = (char *)words[2]; + if (!volname) { + ret = -1; + goto out; + } + + ret = cli_cmd_validate_volume(volname); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to validate volume name"); + goto out; + } + + ret = dict_set_str(dict, "volname", volname); + if (ret) { + cli_out("Failed to set volume name in dictionary "); + goto out; + } + + w = str_getunamb(words[3], opwords); + if (!w) { + cli_out("Invalid bit rot option : %s", words[3]); + ret = -1; + goto out; + } + + if (strcmp(w, "enable") == 0) { + if (wordcount == 4) { + type = GF_BITROT_OPTION_TYPE_ENABLE; + ret = 0; + goto set_type; + } else { + ret = -1; + goto out; + } + } - GF_ASSERT (words); - GF_ASSERT (options); + if (strcmp(w, "disable") == 0) { + if (wordcount == 4) { + type = GF_BITROT_OPTION_TYPE_DISABLE; + ret = 0; + goto set_type; + } else { + ret = -1; + goto out; + } + } + + if (!strcmp(w, "scrub-throttle")) { + if (!words[4]) { + cli_err( + "Missing scrub-throttle value for bitrot " + "option"); + ret = -1; + goto out; + } else { + w = str_getunamb(words[4], scrub_throt_values); + if (!w) { + cli_err( + "Invalid scrub-throttle option for " + "bitrot"); + ret = -1; + goto out; + } else { + type = GF_BITROT_OPTION_TYPE_SCRUB_THROTTLE; + ret = dict_set_str(dict, "scrub-throttle-value", + (char *)words[4]); + if (ret) { + cli_out( + "Failed to set scrub-throttle " + "value in the dict"); + goto out; + } + goto set_type; + } + } + } - GF_ASSERT ((strcmp (words[0], "volume")) == 0); - GF_ASSERT ((strcmp (words[1], "log")) == 0); - GF_ASSERT ((strcmp (words[2], "rotate")) == 0); + if (!strcmp(words[3], "scrub-frequency")) { + if (!words[4]) { + cli_err("Missing scrub-frequency value"); + ret = -1; + goto out; + } else { + w = str_getunamb(words[4], scrub_freq_values); + if (!w) { + cli_err("Invalid frequency option for bitrot"); + ret = -1; + goto out; + } else { + type = GF_BITROT_OPTION_TYPE_SCRUB_FREQ; + ret = dict_set_str(dict, "scrub-frequency-value", + (char *)words[4]); + if (ret) { + cli_out( + "Failed to set dict for " + "bitrot"); + goto out; + } + goto set_type; + } + } + } - dict = dict_new (); - if (!dict) + if (!strcmp(words[3], "scrub")) { + if (!words[4]) { + cli_err("Missing scrub value for bitrot option"); + ret = -1; + goto out; + } else { + w = str_getunamb(words[4], scrub_values); + if (!w) { + cli_err("Invalid scrub option for bitrot"); + ret = -1; goto out; + } else { + if (strcmp(words[4], "status") == 0) { + type = GF_BITROT_CMD_SCRUB_STATUS; + } else if (strcmp(words[4], "ondemand") == 0) { + type = GF_BITROT_CMD_SCRUB_ONDEMAND; + } else { + type = GF_BITROT_OPTION_TYPE_SCRUB; + } + ret = dict_set_str(dict, "scrub-value", (char *)words[4]); + if (ret) { + cli_out( + "Failed to set dict for " + "bitrot"); + goto out; + } + goto set_type; + } + } + } + + if (!strcmp(words[3], "signing-time")) { + if (!words[4]) { + cli_err( + "Missing signing-time value for bitrot " + "option"); + ret = -1; + goto out; + } else { + type = GF_BITROT_OPTION_TYPE_EXPIRY_TIME; - volname = (char *)words[3]; - GF_ASSERT (volname); + expiry_time = strtol(words[4], NULL, 0); + if (expiry_time < 1) { + cli_err( + "Expiry time value should not be less" + " than 1"); + ret = -1; + goto out; + } - ret = dict_set_str (dict, "volname", volname); - if (ret) + ret = dict_set_uint32(dict, "expiry-time", + (unsigned int)expiry_time); + if (ret) { + cli_out("Failed to set dict for bitrot"); goto out; + } + goto set_type; + } + } else if (!strcmp(words[3], "signer-threads")) { + if (!words[4]) { + cli_err( + "Missing signer-thread value for bitrot " + "option"); + ret = -1; + goto out; + } else { + type = GF_BITROT_OPTION_TYPE_SIGNER_THREADS; - str = (char *)words[4]; - if (str && strchr (str, ':')) { - ret = dict_set_str (dict, "brick", str); - if (ret) - goto out; + signer_th_count = strtol(words[4], NULL, 0); + if (signer_th_count < 1) { + cli_err("signer-thread count should not be less than 1"); + ret = -1; + goto out; + } + + ret = dict_set_uint32(dict, "signer-threads", + (unsigned int)signer_th_count); + if (ret) { + cli_out("Failed to set dict for bitrot"); + goto out; + } + goto set_type; } + } else { + cli_err( + "Invalid option %s for bitrot. Please enter valid " + "bitrot option", + words[3]); + ret = -1; + goto out; + } +set_type: + ret = dict_set_int32(dict, "type", type); + if (ret < 0) + goto out; + + *options = dict; - *options = dict; +out: + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to parse bitrot command"); + if (dict) + dict_unref(dict); + } + return ret; +} + +/* Parsing global option for NFS-Ganesha config + * gluster nfs-ganesha enable/disable */ + +int32_t +cli_cmd_ganesha_parse(struct cli_state *state, const char **words, + int wordcount, dict_t **options, char **op_errstr) +{ + dict_t *dict = NULL; + int ret = -1; + char *key = NULL; + char *value = NULL; + char *w = NULL; + static char *opwords[] = {"enable", "disable", NULL}; + const char *question = NULL; + gf_answer_t answer = GF_ANSWER_NO; + + GF_ASSERT(words); + GF_ASSERT(options); + + dict = dict_new(); + + if (!dict) + goto out; + + if (wordcount != 2) + goto out; + + key = (char *)words[0]; + value = (char *)words[1]; + + if (!key || !value) { + cli_out("Usage : nfs-ganesha <enable/disable>"); + ret = -1; + goto out; + } + + ret = gf_strip_whitespace(value, strlen(value)); + if (ret == -1) + goto out; + + if (strcmp(key, "nfs-ganesha")) { + gf_asprintf(op_errstr, + "Global option: error: ' %s '" + "is not a valid global option.", + key); + ret = -1; + goto out; + } + + w = str_getunamb(value, opwords); + if (!w) { + cli_out( + "Invalid global option \n" + "Usage : nfs-ganesha <enable/disable>"); + ret = -1; + goto out; + } + + if (strcmp(value, "enable") == 0) { + question = + "Enabling NFS-Ganesha requires Gluster-NFS to be " + "disabled across the trusted pool. Do you " + "still want to continue?\n"; + } else if (strcmp(value, "disable") == 0) { + question = + "Disabling NFS-Ganesha will tear down the entire " + "ganesha cluster across the trusted pool. Do you " + "still want to continue?\n"; + } else { + ret = -1; + goto out; + } + answer = cli_cmd_get_confirmation(state, question); + if (GF_ANSWER_NO == answer) { + gf_log("cli", GF_LOG_ERROR, + "Global operation " + "cancelled, exiting"); + ret = -1; + goto out; + } + cli_out("This will take a few minutes to complete. Please wait .."); + + ret = dict_set_str(dict, "key", key); + if (ret) { + gf_log(THIS->name, GF_LOG_ERROR, "dict set on key failed"); + goto out; + } + + ret = dict_set_str(dict, "value", value); + if (ret) { + gf_log(THIS->name, GF_LOG_ERROR, "dict set on value failed"); + goto out; + } + + ret = dict_set_str(dict, "globalname", "All"); + if (ret) { + gf_log(THIS->name, GF_LOG_ERROR, + "dict set on global" + " key failed."); + goto out; + } + + ret = dict_set_int32(dict, "hold_global_locks", _gf_true); + if (ret) { + gf_log(THIS->name, GF_LOG_ERROR, + "dict set on global key " + "failed."); + goto out; + } + + *options = dict; out: - if (ret && dict) - dict_destroy (dict); + if (ret) + dict_unref(dict); - return ret; + return ret; } diff --git a/cli/src/cli-cmd-peer.c b/cli/src/cli-cmd-peer.c index fef29c164ec..084998701d8 100644 --- a/cli/src/cli-cmd-peer.c +++ b/cli/src/cli-cmd-peer.c @@ -1,232 +1,317 @@ /* - Copyright (c) 2010 Gluster, Inc. <http://www.gluster.com> - This file is part of GlusterFS. - - GlusterFS is free software; you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - GlusterFS is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see - <http://www.gnu.org/licenses/>. -*/ + Copyright (c) 2010-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + 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 <stdio.h> #include <string.h> #include <stdlib.h> #include <stdint.h> #include <pthread.h> -#ifndef _CONFIG_H -#define _CONFIG_H -#include "config.h" -#endif - #include "cli.h" #include "cli-cmd.h" #include "cli-mem-types.h" +#include "cli1-xdr.h" #include "protocol-common.h" +#include <glusterfs/events.h> -extern struct rpc_clnt *global_rpc; - -extern rpc_clnt_prog_t *cli_rpc_prog; - -int cli_cmd_peer_help_cbk (struct cli_state *state, struct cli_cmd_word *in_word, +int +cli_cmd_peer_help_cbk(struct cli_state *state, struct cli_cmd_word *in_word, const char **words, int wordcount); -void -cli_cmd_probe_usage () +int +cli_cmd_peer_probe_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) { - cli_out ("Usage: probe <hostname>"); -} + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + dict_t *dict = NULL; + int sent = 0; + int parse_error = 0; + cli_local_t *local = NULL; + + if (!(wordcount == 3)) { + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_PROBE]; + + dict = dict_new(); + if (!dict) + goto out; + + ret = dict_set_str(dict, "hostname", (char *)words[2]); + if (ret) + goto out; + + ret = valid_internet_address((char *)words[2], _gf_false, _gf_false); + if (ret == 1) { + ret = 0; + } else { + cli_out("%s is an invalid address", words[2]); + cli_usage_out(word->pattern); + parse_error = 1; + ret = -1; + goto out; + } + /* if (words[3]) { + ret = dict_set_str (dict, "port", (char *)words[3]); + if (ret) + goto out; + } + */ + + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + ret = -1; + goto out; + } + + CLI_LOCAL_INIT(local, words, frame, dict); + + if (proc->fn) { + ret = proc->fn(frame, THIS, dict); + } -void -cli_cmd_deprobe_usage () -{ - cli_out ("Usage: detach <hostname>"); -} +out: + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_out("Peer probe failed"); + } -void -cli_cmd_peer_status_usage () -{ - cli_out ("Usage: peer status"); + CLI_STACK_DESTROY(frame); + + if (ret == 0) { + gf_event(EVENT_PEER_ATTACH, "host=%s", (char *)words[2]); + } + + return ret; } int -cli_cmd_peer_probe_cbk (struct cli_state *state, struct cli_cmd_word *word, - const char **words, int wordcount) +cli_cmd_peer_deprobe_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) { - int ret = -1; - rpc_clnt_procedure_t *proc = NULL; - call_frame_t *frame = NULL; - dict_t *dict = NULL; - - if (!(wordcount == 3)) { - cli_cmd_probe_usage (); - goto out; + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + dict_t *dict = NULL; + int flags = 0; + int sent = 0; + int parse_error = 0; + cli_local_t *local = NULL; + gf_answer_t answer = GF_ANSWER_NO; + const char *question = NULL; + + if ((wordcount < 3) || (wordcount > 4)) { + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } + question = + "All clients mounted through the peer which is getting detached need " + "to be remounted using one of the other active peers in the trusted " + "storage pool to ensure client gets notification on any changes done " + "on the gluster configuration and if the same has been done do you " + "want to proceed?"; + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_DEPROBE]; + + dict = dict_new(); + + ret = dict_set_str(dict, "hostname", (char *)words[2]); + if (ret) + goto out; + + /* if (words[3]) { + ret = dict_set_str (dict, "port", (char *)words[3]); + if (ret) + goto out; + } + */ + if (wordcount == 4) { + if (!strcmp("force", words[3])) + flags |= GF_CLI_FLAG_OP_FORCE; + else { + ret = -1; + cli_usage_out(word->pattern); + parse_error = 1; + goto out; } + } + ret = dict_set_int32(dict, "flags", flags); + if (ret) + goto out; + answer = cli_cmd_get_confirmation(state, question); + if (GF_ANSWER_NO == answer) { + ret = 0; + goto out; + } + + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + ret = -1; + goto out; + } + + CLI_LOCAL_INIT(local, words, frame, dict); + + if (proc->fn) { + ret = proc->fn(frame, THIS, dict); + } - proc = &cli_rpc_prog->proctable[GF1_CLI_PROBE]; - - frame = create_frame (THIS, THIS->ctx->pool); - if (!frame) - goto out; +out: + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_out("Peer detach failed"); + } - dict = dict_new (); - if (!dict) - goto out; + CLI_STACK_DESTROY(frame); - ret = dict_set_str (dict, "hostname", (char *)words[2]); - if (ret) - goto out; + if (ret == 0) { + gf_event(EVENT_PEER_DETACH, "host=%s", (char *)words[2]); + } -/* if (words[3]) { - ret = dict_set_str (dict, "port", (char *)words[3]); - if (ret) - goto out; - } -*/ - if (proc->fn) { - ret = proc->fn (frame, THIS, dict); - } - -out: - if (ret) - cli_out ("Probe failed"); - return ret; + return ret; } - int -cli_cmd_peer_deprobe_cbk (struct cli_state *state, struct cli_cmd_word *word, - const char **words, int wordcount) +cli_cmd_peer_status_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) { - int ret = -1; - rpc_clnt_procedure_t *proc = NULL; - call_frame_t *frame = NULL; - dict_t *dict = NULL; - - if (!(wordcount == 3) ) { - cli_cmd_deprobe_usage (); - goto out; - } + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + int sent = 0; + int parse_error = 0; - proc = &cli_rpc_prog->proctable[GF1_CLI_DEPROBE]; + if (wordcount != 2) { + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } - frame = create_frame (THIS, THIS->ctx->pool); - if (!frame) - goto out; + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_LIST_FRIENDS]; - dict = dict_new (); + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) + goto out; - ret = dict_set_str (dict, "hostname", (char *)words[2]); - if (ret) - goto out; - -/* if (words[3]) { - ret = dict_set_str (dict, "port", (char *)words[3]); - if (ret) - goto out; - } -*/ - if (proc->fn) { - ret = proc->fn (frame, THIS, dict); - } + if (proc->fn) { + ret = proc->fn(frame, THIS, (void *)GF_CLI_LIST_PEERS); + } out: - if (ret) - cli_out ("Detach failed"); + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_out("Peer status failed"); + } + + CLI_STACK_DESTROY(frame); - return ret; + return ret; } int -cli_cmd_peer_status_cbk (struct cli_state *state, struct cli_cmd_word *word, - const char **words, int wordcount) +cli_cmd_pool_list_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) { - int ret = -1; - rpc_clnt_procedure_t *proc = NULL; - call_frame_t *frame = NULL; + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + int sent = 0; + int parse_error = 0; - if (wordcount != 2) { - cli_cmd_peer_status_usage (); - goto out; - } + if (wordcount != 2) { + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } - proc = &cli_rpc_prog->proctable[GF1_CLI_LIST_FRIENDS]; + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_LIST_FRIENDS]; - frame = create_frame (THIS, THIS->ctx->pool); - if (!frame) - goto out; + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) + goto out; - if (proc->fn) { - ret = proc->fn (frame, THIS, (char *)words[1] ); - } + if (proc->fn) { + ret = proc->fn(frame, THIS, (void *)GF_CLI_LIST_POOL_NODES); + } out: - if (ret) - cli_out ("Command Execution failed"); - return ret; + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_err("pool list: command execution failed"); + } + + CLI_STACK_DESTROY(frame); + + return ret; } struct cli_cmd cli_probe_cmds[] = { - { "peer probe <HOSTNAME>", - cli_cmd_peer_probe_cbk, - "probe peer specified by <HOSTNAME>"}, + {"peer probe { <HOSTNAME> | <IP-address> }", cli_cmd_peer_probe_cbk, + "probe peer specified by <HOSTNAME>"}, + + {"peer detach { <HOSTNAME> | <IP-address> } [force]", + cli_cmd_peer_deprobe_cbk, "detach peer specified by <HOSTNAME>"}, - { "peer detach <HOSTNAME>", - cli_cmd_peer_deprobe_cbk, - "detach peer specified by <HOSTNAME>"}, + {"peer status", cli_cmd_peer_status_cbk, "list status of peers"}, - { "peer status", - cli_cmd_peer_status_cbk, - "list status of peers"}, - - { "peer --help", - cli_cmd_peer_help_cbk, - "Help command for peer "}, + {"peer help", cli_cmd_peer_help_cbk, "display help for peer commands"}, + {"pool list", cli_cmd_pool_list_cbk, + "list all the nodes in the pool (including localhost)"}, - { NULL, NULL, NULL } -}; + {NULL, NULL, NULL}}; int -cli_cmd_peer_help_cbk (struct cli_state *state, struct cli_cmd_word *in_word, +cli_cmd_peer_help_cbk(struct cli_state *state, struct cli_cmd_word *in_word, const char **words, int wordcount) { - struct cli_cmd *cmd = NULL; + struct cli_cmd *cmd = NULL; + struct cli_cmd *probe_cmd = NULL; + int count = 0; - + cli_out("\ngluster peer commands"); + cli_out("======================\n"); - for (cmd = cli_probe_cmds; cmd->pattern; cmd++) - cli_out ("%s - %s", cmd->pattern, cmd->desc); + cmd = GF_MALLOC(sizeof(cli_probe_cmds), cli_mt_cli_cmd); + memcpy(cmd, cli_probe_cmds, sizeof(cli_probe_cmds)); + count = (sizeof(cli_probe_cmds) / sizeof(struct cli_cmd)); + cli_cmd_sort(cmd, count); - + for (probe_cmd = cmd; probe_cmd->pattern; probe_cmd++) + cli_out("%s - %s", probe_cmd->pattern, probe_cmd->desc); - if (!state->rl_enabled) - exit (0); + GF_FREE(cmd); - return 0; + cli_out("\n"); + return 0; } int -cli_cmd_probe_register (struct cli_state *state) +cli_cmd_probe_register(struct cli_state *state) { - int ret = 0; - struct cli_cmd *cmd = NULL; - - for (cmd = cli_probe_cmds; cmd->pattern; cmd++) { - ret = cli_cmd_register (&state->tree, cmd->pattern, cmd->cbk, - cmd->desc); - if (ret) - goto out; - } + int ret = 0; + struct cli_cmd *cmd = NULL; + + for (cmd = cli_probe_cmds; cmd->pattern; cmd++) { + ret = cli_cmd_register(&state->tree, cmd); + if (ret) + goto out; + } out: - return ret; + return ret; } diff --git a/cli/src/cli-cmd-snapshot.c b/cli/src/cli-cmd-snapshot.c new file mode 100644 index 00000000000..859d6b2e40d --- /dev/null +++ b/cli/src/cli-cmd-snapshot.c @@ -0,0 +1,135 @@ +/* + Copyright (c) 2013-2014 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + 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 <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <stdint.h> +#include <pthread.h> + +#include "cli.h" +#include "cli-cmd.h" +#include "cli-mem-types.h" + +int +cli_cmd_snapshot_help_cbk(struct cli_state *state, struct cli_cmd_word *in_word, + const char **words, int wordcount); + +int +cli_cmd_snapshot_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) +{ + int ret = 0; + int parse_err = 0; + dict_t *options = NULL; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + cli_local_t *local = NULL; + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_SNAP]; + + /* Parses the command entered by the user */ + ret = cli_cmd_snapshot_parse(words, wordcount, &options, state); + if (ret) { + if (ret < 0) { + cli_usage_out(word->pattern); + parse_err = 1; + } else { + /* User might have cancelled the snapshot operation */ + ret = 0; + } + goto out; + } + + frame = create_frame(THIS, THIS->ctx->pool); + if (frame == NULL) { + ret = -1; + goto out; + } + + CLI_LOCAL_INIT(local, words, frame, options); + + if (proc->fn) + ret = proc->fn(frame, THIS, options); + +out: + if (ret && parse_err == 0) + cli_out("Snapshot command failed"); + + CLI_STACK_DESTROY(frame); + + return ret; +} + +struct cli_cmd snapshot_cmds[] = { + {"snapshot help", cli_cmd_snapshot_help_cbk, + "display help for snapshot commands"}, + {"snapshot create <snapname> <volname> [no-timestamp] " + "[description <description>] [force]", + cli_cmd_snapshot_cbk, "Snapshot Create."}, + {"snapshot clone <clonename> <snapname>", cli_cmd_snapshot_cbk, + "Snapshot Clone."}, + {"snapshot restore <snapname>", cli_cmd_snapshot_cbk, "Snapshot Restore."}, + {"snapshot status [(snapname | volume <volname>)]", cli_cmd_snapshot_cbk, + "Snapshot Status."}, + {"snapshot info [(snapname | volume <volname>)]", cli_cmd_snapshot_cbk, + "Snapshot Info."}, + {"snapshot list [volname]", cli_cmd_snapshot_cbk, "Snapshot List."}, + {"snapshot config [volname] ([snap-max-hard-limit <count>] " + "[snap-max-soft-limit <percent>]) " + "| ([auto-delete <enable|disable>])" + "| ([activate-on-create <enable|disable>])", + cli_cmd_snapshot_cbk, "Snapshot Config."}, + {"snapshot delete (all | snapname | volume <volname>)", + cli_cmd_snapshot_cbk, "Snapshot Delete."}, + {"snapshot activate <snapname> [force]", cli_cmd_snapshot_cbk, + "Activate snapshot volume."}, + {"snapshot deactivate <snapname>", cli_cmd_snapshot_cbk, + "Deactivate snapshot volume."}, + {NULL, NULL, NULL}}; + +int +cli_cmd_snapshot_help_cbk(struct cli_state *state, struct cli_cmd_word *in_word, + const char **words, int wordcount) +{ + struct cli_cmd *cmd = NULL; + struct cli_cmd *snap_cmd = NULL; + int count = 0; + + cmd = GF_MALLOC(sizeof(snapshot_cmds), cli_mt_cli_cmd); + memcpy(cmd, snapshot_cmds, sizeof(snapshot_cmds)); + count = (sizeof(snapshot_cmds) / sizeof(struct cli_cmd)); + cli_cmd_sort(cmd, count); + + cli_out("\ngluster snapshot commands"); + cli_out("=========================\n"); + + for (snap_cmd = cmd; snap_cmd->pattern; snap_cmd++) + if (_gf_false == snap_cmd->disable) + cli_out("%s - %s", snap_cmd->pattern, snap_cmd->desc); + cli_out("\n"); + + GF_FREE(cmd); + return 0; +} + +int +cli_cmd_snapshot_register(struct cli_state *state) +{ + int ret = 0; + struct cli_cmd *cmd = NULL; + + for (cmd = snapshot_cmds; cmd->pattern; cmd++) { + ret = cli_cmd_register(&state->tree, cmd); + if (ret) + goto out; + } +out: + return ret; +} diff --git a/cli/src/cli-cmd-system.c b/cli/src/cli-cmd-system.c new file mode 100644 index 00000000000..801e8f4efed --- /dev/null +++ b/cli/src/cli-cmd-system.c @@ -0,0 +1,624 @@ +/* + Copyright (c) 2010-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + 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 <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <stdint.h> +#include <pthread.h> + +#include "cli.h" +#include "cli-cmd.h" +#include "cli-mem-types.h" +#include "protocol-common.h" + +int +cli_cmd_system_help_cbk(struct cli_state *state, struct cli_cmd_word *in_word, + const char **words, int wordcount); + +int +cli_cmd_copy_file_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount); + +int +cli_cmd_sys_exec_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount); + +int +cli_cmd_getspec_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) +{ + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + dict_t *dict = NULL; + + if (wordcount != 3) { + cli_usage_out(word->pattern); + goto out; + } + + dict = dict_new(); + if (!dict) + goto out; + + ret = dict_set_str(dict, "volid", (char *)words[2]); + if (ret) + goto out; + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_GETSPEC]; + if (proc->fn) { + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + ret = -1; + goto out; + } + ret = proc->fn(frame, THIS, dict); + } + +out: + if (!proc && ret) { + if (wordcount > 1) + cli_out("Fetching spec for volume %s failed", (char *)words[2]); + } + + if (dict) + dict_unref(dict); + + CLI_STACK_DESTROY(frame); + return ret; +} + +int +cli_cmd_pmap_b2p_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) +{ + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + dict_t *dict = NULL; + + if (wordcount != 4) { + cli_usage_out(word->pattern); + goto out; + } + + dict = dict_new(); + if (!dict) + goto out; + + ret = dict_set_str(dict, "brick", (char *)words[3]); + if (ret) + goto out; + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_PMAP_PORTBYBRICK]; + if (proc->fn) { + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + ret = -1; + goto out; + } + ret = proc->fn(frame, THIS, dict); + } + +out: + if (!proc && ret) { + if (wordcount > 1) + cli_out("Fetching spec for volume %s failed", (char *)words[3]); + } + + if (dict) + dict_unref(dict); + + CLI_STACK_DESTROY(frame); + return ret; +} + +int +cli_cmd_fsm_log_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) +{ + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + char *name = ""; + + if ((wordcount != 4) && (wordcount != 3)) { + cli_usage_out(word->pattern); + goto out; + } + + if (wordcount == 4) + name = (char *)words[3]; + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_FSM_LOG]; + if (proc && proc->fn) { + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) + goto out; + ret = proc->fn(frame, THIS, (void *)name); + } +out: + return ret; +} + +int +cli_cmd_getwd_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) +{ + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + + if (wordcount != 2) { + cli_usage_out(word->pattern); + goto out; + } + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_GETWD]; + if (proc && proc->fn) { + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) + goto out; + ret = proc->fn(frame, THIS, NULL); + } +out: + return ret; +} + +static dict_t * +make_seq_dict(int argc, char **argv) +{ + char index[] = "4294967296"; // 1<<32 + int i = 0; + int len; + int ret = 0; + dict_t *dict = dict_new(); + + if (!dict) + return NULL; + + for (i = 0; i < argc; i++) { + len = snprintf(index, sizeof(index), "%d", i); + ret = dict_set_strn(dict, index, len, argv[i]); + if (ret == -1) + break; + } + + if (ret) { + dict_unref(dict); + dict = NULL; + } + + return dict; +} + +int +cli_cmd_mount_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) +{ + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + int ret = -1; + dict_t *dict = NULL; + void *dataa[] = {NULL, NULL}; + + if (wordcount < 4) { + cli_usage_out(word->pattern); + goto out; + } + + dict = make_seq_dict(wordcount - 3, (char **)words + 3); + if (!dict) + goto out; + + dataa[0] = (void *)words[2]; + dataa[1] = dict; + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_MOUNT]; + if (proc && proc->fn) { + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) + goto out; + ret = proc->fn(frame, THIS, dataa); + } + +out: + if (dict) + dict_unref(dict); + + if (!proc && ret) + cli_out("Mount command failed"); + + return ret; +} + +int +cli_cmd_umount_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) +{ + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + int ret = -1; + dict_t *dict = NULL; + + if (!(wordcount == 3 || + (wordcount == 4 && strcmp(words[3], "lazy") == 0))) { + cli_usage_out(word->pattern); + goto out; + } + + dict = dict_new(); + if (!dict) + goto out; + + ret = dict_set_str(dict, "path", (char *)words[2]); + if (ret != 0) + goto out; + ret = dict_set_int32(dict, "lazy", wordcount == 4); + if (ret != 0) + goto out; + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_UMOUNT]; + if (proc && proc->fn) { + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + gf_log(THIS->name, GF_LOG_ERROR, "failed to create frame"); + ret = -1; + goto out; + } + ret = proc->fn(frame, THIS, dict); + } + +out: + if (dict) + dict_unref(dict); + + if (!proc && ret) + cli_out("Umount command failed"); + + return ret; +} + +int +cli_cmd_uuid_get_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) +{ + int ret = -1; + int sent = 0; + int parse_error = 0; + dict_t *dict = NULL; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + cli_local_t *local = NULL; + xlator_t *this = NULL; + + this = THIS; + if (wordcount != 3) { + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_UUID_GET]; + frame = create_frame(this, this->ctx->pool); + if (!frame) + goto out; + + dict = dict_new(); + if (!dict) + goto out; + + CLI_LOCAL_INIT(local, words, frame, dict); + if (proc->fn) + ret = proc->fn(frame, this, dict); + +out: + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_out("uuid get failed"); + } + + if (dict) + dict_unref(dict); + + CLI_STACK_DESTROY(frame); + return ret; +} + +int +cli_cmd_uuid_reset_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) +{ + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + int sent = 0; + int parse_error = 0; + gf_answer_t answer = GF_ANSWER_NO; + char *question = NULL; + cli_local_t *local = NULL; + dict_t *dict = NULL; + xlator_t *this = NULL; + + question = + "Resetting uuid changes the uuid of local glusterd. " + "Do you want to continue?"; + + if (wordcount != 3) { + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_UUID_RESET]; + + this = THIS; + frame = create_frame(this, this->ctx->pool); + if (!frame) + goto out; + + dict = dict_new(); + if (!dict) { + ret = -1; + goto out; + } + CLI_LOCAL_INIT(local, words, frame, dict); + answer = cli_cmd_get_confirmation(state, question); + + if (GF_ANSWER_NO == answer) { + ret = 0; + goto out; + } + + // send NULL as argument since no dictionary is sent to glusterd + if (proc->fn) { + ret = proc->fn(frame, this, dict); + } + +out: + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_out("uuid reset failed"); + } + + if (dict) + dict_unref(dict); + + CLI_STACK_DESTROY(frame); + + return ret; +} + +static struct cli_cmd cli_system_cmds[] = { + {"system:: getspec <VOLNAME>", cli_cmd_getspec_cbk, + "fetch the volume file for the volume <VOLNAME>"}, + + {"system:: portmap brick2port <BRICK>", cli_cmd_pmap_b2p_cbk, + "query which port <BRICK> listens on"}, + + {"system:: fsm log [<peer-name>]", cli_cmd_fsm_log_cbk, + "display fsm transitions"}, + + {"system:: getwd", cli_cmd_getwd_cbk, "query glusterd work directory"}, + + {"system:: mount <label> <args...>", cli_cmd_mount_cbk, "request a mount"}, + + {"system:: umount <path> [lazy]", cli_cmd_umount_cbk, "request an umount"}, + + {"system:: uuid get", cli_cmd_uuid_get_cbk, "get uuid of glusterd"}, + + {"system:: uuid reset", cli_cmd_uuid_reset_cbk, + "reset the uuid of glusterd"}, + + {"system:: help", cli_cmd_system_help_cbk, + "display help for system commands"}, + + {"system:: copy file [<filename>]", cli_cmd_copy_file_cbk, + "Copy file from current node's $working_dir to " + "$working_dir of all cluster nodes"}, + + {"system:: execute <command> <args>", cli_cmd_sys_exec_cbk, + "Execute the command on all the nodes " + "in the cluster and display their output."}, + + {NULL, NULL, NULL}}; + +int +cli_cmd_sys_exec_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) +{ + char cmd_arg_name[PATH_MAX] = ""; + char *command = NULL; + char *saveptr = NULL; + char *tmp = NULL; + int ret = -1; + int i = -1; + int len; + int cmd_args_count = 0; + int in_cmd_args_count = 0; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + dict_t *dict = NULL; + cli_local_t *local = NULL; + + if ((wordcount < 3) || (words[2] == NULL)) { + cli_usage_out(word->pattern); + goto out; + } + + command = strtok_r((char *)words[2], " ", &saveptr); + if (command == NULL) { + gf_log("cli", GF_LOG_ERROR, "Failed to parse command"); + goto out; + } + + dict = dict_new(); + if (!dict) + goto out; + + do { + tmp = strtok_r(NULL, " ", &saveptr); + if (tmp) { + in_cmd_args_count++; + snprintf(cmd_arg_name, sizeof(cmd_arg_name), "cmd_arg_%d", + in_cmd_args_count); + ret = dict_set_str(dict, cmd_arg_name, tmp); + if (ret) { + gf_log("", GF_LOG_ERROR, + "Unable to set " + "%s in dict", + cmd_arg_name); + goto out; + } + } + } while (tmp); + + cmd_args_count = wordcount - 3; + + ret = dict_set_str(dict, "command", command); + if (ret) { + gf_log("", GF_LOG_ERROR, "Unable to set command in dict"); + goto out; + } + + for (i = 1; i <= cmd_args_count; i++) { + in_cmd_args_count++; + len = snprintf(cmd_arg_name, sizeof(cmd_arg_name), "cmd_arg_%d", + in_cmd_args_count); + ret = dict_set_strn(dict, cmd_arg_name, len, (char *)words[2 + i]); + if (ret) { + gf_log("", GF_LOG_ERROR, "Unable to set %s in dict", cmd_arg_name); + goto out; + } + } + + ret = dict_set_int32(dict, "cmd_args_count", in_cmd_args_count); + if (ret) { + gf_log("", GF_LOG_ERROR, "Unable to set cmd_args_count in dict"); + goto out; + } + + ret = dict_set_str(dict, "volname", "N/A"); + if (ret) { + gf_log("", GF_LOG_ERROR, "Unable to set volname in dict"); + goto out; + } + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_SYS_EXEC]; + if (proc->fn) { + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + gf_log(THIS->name, GF_LOG_ERROR, "failed to create frame"); + ret = -1; + goto out; + } + CLI_LOCAL_INIT(local, words, frame, dict); + ret = proc->fn(frame, THIS, (void *)dict); + + /* proc->fn is processed synchronously, which means that the + * execution flow won't return here until the operation is + * fully processed, including any related callback. For this + * reason, it's safe to destroy the stack here, since no one + * can still be using it. Additionally, it's not easy to move + * the stack destroy to the callback executed after completion + * of the operation because there are multiple things than can + * fail even before having queued the callback, so we would + * still need to destroy the stack if proc->fn returns an + * error. */ + CLI_STACK_DESTROY(frame); + dict = NULL; + } +out: + if (dict != NULL) { + dict_unref(dict); + } + + return ret; +} + +int +cli_cmd_copy_file_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) +{ + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + char *filename = ""; + dict_t *dict = NULL; + cli_local_t *local = NULL; + + if (wordcount != 4) { + cli_usage_out(word->pattern); + goto out; + } + + dict = dict_new(); + if (!dict) + goto out; + + filename = (char *)words[3]; + ret = dict_set_str(dict, "source", filename); + if (ret) + gf_log("", GF_LOG_ERROR, "Unable to set filename in dict"); + + ret = dict_set_str(dict, "volname", "N/A"); + if (ret) + gf_log("", GF_LOG_ERROR, "Unable to set volname in dict"); + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_COPY_FILE]; + if (proc && proc->fn) { + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + gf_log(THIS->name, GF_LOG_ERROR, "failed to create frame"); + ret = -1; + goto out; + } + CLI_LOCAL_INIT(local, words, frame, dict); + ret = proc->fn(frame, THIS, (void *)dict); + } +out: + return ret; +} + +int +cli_cmd_system_help_cbk(struct cli_state *state, struct cli_cmd_word *in_word, + const char **words, int wordcount) +{ + struct cli_cmd *cmd = NULL; + struct cli_cmd *system_cmd = NULL; + int count = 0; + + cmd = GF_MALLOC(sizeof(cli_system_cmds), cli_mt_cli_cmd); + memcpy(cmd, cli_system_cmds, sizeof(cli_system_cmds)); + count = (sizeof(cli_system_cmds) / sizeof(struct cli_cmd)); + cli_cmd_sort(cmd, count); + + for (system_cmd = cmd; system_cmd->pattern; system_cmd++) + cli_out("%s - %s", system_cmd->pattern, system_cmd->desc); + + GF_FREE(cmd); + return 0; +} + +int +cli_cmd_system_register(struct cli_state *state) +{ + int ret = 0; + struct cli_cmd *cmd = NULL; + + for (cmd = cli_system_cmds; cmd->pattern; cmd++) { + ret = cli_cmd_register(&state->tree, cmd); + if (ret) + goto out; + } +out: + return ret; +} diff --git a/cli/src/cli-cmd-volume.c b/cli/src/cli-cmd-volume.c index 09f89137445..f238851586e 100644 --- a/cli/src/cli-cmd-volume.c +++ b/cli/src/cli-cmd-volume.c @@ -1,787 +1,3278 @@ /* - Copyright (c) 2010 Gluster, Inc. <http://www.gluster.com> - This file is part of GlusterFS. - - GlusterFS is free software; you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - GlusterFS is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see - <http://www.gnu.org/licenses/>. -*/ + Copyright (c) 2010-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + 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 <stdio.h> #include <string.h> #include <stdlib.h> #include <stdint.h> #include <pthread.h> -#ifndef _CONFIG_H -#define _CONFIG_H -#include "config.h" -#endif +#include <sys/socket.h> +#include <netdb.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <netinet/in.h> #include "cli.h" #include "cli-cmd.h" #include "cli-mem-types.h" #include "cli1-xdr.h" +#include <glusterfs/run.h> +#include <glusterfs/syscall.h> +#include <glusterfs/common-utils.h> +#include <glusterfs/events.h> -extern struct rpc_clnt *global_rpc; +extern rpc_clnt_prog_t cli_quotad_clnt; -extern rpc_clnt_prog_t *cli_rpc_prog; +static int +gf_asprintf_append(char **string_ptr, const char *format, ...); int -cli_cmd_volume_help_cbk (struct cli_state *state, struct cli_cmd_word *in_word, - const char **words, int wordcount); +cli_cmd_volume_help_cbk(struct cli_state *state, struct cli_cmd_word *in_word, + const char **words, int wordcount); -void -cli_cmd_volume_start_usage () +int +cli_cmd_bitrot_help_cbk(struct cli_state *state, struct cli_cmd_word *in_word, + const char **words, int wordcount); + +int +cli_cmd_quota_help_cbk(struct cli_state *state, struct cli_cmd_word *in_word, + const char **words, int wordcount); + +int +cli_cmd_volume_info_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) { - cli_out ("Usage: volume start <VOLNAME>"); + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + cli_cmd_volume_get_ctx_t ctx = { + 0, + }; + cli_local_t *local = NULL; + int sent = 0; + int parse_error = 0; + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_GET_VOLUME]; + + if ((wordcount == 2) || (wordcount == 3 && !strcmp(words[2], "all"))) { + ctx.flags = GF_CLI_GET_NEXT_VOLUME; + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_GET_NEXT_VOLUME]; + } else if (wordcount == 3) { + ctx.flags = GF_CLI_GET_VOLUME; + ctx.volname = (char *)words[2]; + if (strlen(ctx.volname) > GD_VOLUME_NAME_MAX) { + cli_out("Invalid volume name"); + goto out; + } + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_GET_VOLUME]; + } else { + cli_usage_out(word->pattern); + parse_error = 1; + return -1; + } + + local = cli_local_get(); + + if (!local) + goto out; + + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) + goto out; + + local->get_vol.flags = ctx.flags; + if (ctx.volname) + local->get_vol.volname = gf_strdup(ctx.volname); + + frame->local = local; + + if (proc->fn) { + ret = proc->fn(frame, THIS, &ctx); + } + +out: + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_out("Getting Volume information failed!"); + } + + CLI_STACK_DESTROY(frame); + + return ret; } -void -cli_cmd_volume_stop_usage () +int +cli_cmd_sync_volume_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) { - cli_out ("Usage: volume stop <VOLNAME> [force]"); + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + int sent = 0; + int parse_error = 0; + dict_t *dict = NULL; + cli_local_t *local = NULL; + gf_answer_t answer = GF_ANSWER_NO; + const char *question = + "Sync volume may make data " + "inaccessible while the sync " + "is in progress. Do you want " + "to continue?"; + + if ((wordcount < 3) || (wordcount > 4)) { + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } + + dict = dict_new(); + if (!dict) + goto out; + + if ((wordcount == 3) || !strcmp(words[3], "all")) { + ret = dict_set_int32(dict, "flags", (int32_t)GF_CLI_SYNC_ALL); + if (ret) { + gf_log(THIS->name, GF_LOG_ERROR, + "failed to set" + "flag"); + goto out; + } + } else { + ret = dict_set_str(dict, "volname", (char *)words[3]); + if (ret) { + gf_log(THIS->name, GF_LOG_ERROR, + "failed to set " + "volume"); + goto out; + } + } + + ret = dict_set_str(dict, "hostname", (char *)words[2]); + if (ret) { + gf_log(THIS->name, GF_LOG_ERROR, "failed to set hostname"); + goto out; + } + + if (!(state->mode & GLUSTER_MODE_SCRIPT)) { + answer = cli_cmd_get_confirmation(state, question); + if (GF_ANSWER_NO == answer) { + ret = 0; + goto out; + } + } + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_SYNC_VOLUME]; + + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + gf_log(THIS->name, GF_LOG_ERROR, "failed to create frame"); + ret = -1; + goto out; + } + + CLI_LOCAL_INIT(local, words, frame, dict); + + if (proc->fn) { + ret = proc->fn(frame, THIS, dict); + } + +out: + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_out("Volume sync failed"); + } + + CLI_STACK_DESTROY(frame); + + return ret; } -void -cli_cmd_volume_rename_usage () +int +cli_cmd_volume_create_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) { - cli_out ("Usage: volume rename <VOLNAME> <NEW-VOLNAME>"); + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + dict_t *options = NULL; + int sent = 0; + int parse_error = 0; + cli_local_t *local = NULL; + char *trans_type = NULL; + char *bricks = NULL; + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_CREATE_VOLUME]; + + ret = cli_cmd_volume_create_parse(state, words, wordcount, &options, + &bricks); + + if (ret) { + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } + + ret = dict_get_str(options, "transport", &trans_type); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to get transport type"); + goto out; + } + + if (state->mode & GLUSTER_MODE_WIGNORE) { + ret = dict_set_int32(options, "force", _gf_true); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to set force " + "option"); + goto out; + } + } + + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + ret = -1; + goto out; + } + + CLI_LOCAL_INIT(local, words, frame, options); + + if (proc->fn) { + ret = proc->fn(frame, THIS, options); + } + +out: + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_out("Volume create failed"); + } + + if (ret == 0) { + gf_event(EVENT_VOLUME_CREATE, "name=%s;bricks=%s", (char *)words[2], + bricks); + } + + CLI_STACK_DESTROY(frame); + return ret; } -void -cli_cmd_volume_delete_usage () +int +cli_cmd_volume_delete_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) { - cli_out ("Usage: volume delete <VOLNAME>"); + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + char *volname = NULL; + gf_answer_t answer = GF_ANSWER_NO; + const char *question = NULL; + int sent = 0; + int parse_error = 0; + cli_local_t *local = NULL; + dict_t *dict = NULL; + + question = + "Deleting volume will erase all information about the volume. " + "Do you want to continue?"; + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_DELETE_VOLUME]; + + if (wordcount != 3) { + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } + + volname = (char *)words[2]; + + dict = dict_new(); + if (!dict) + goto out; + + ret = dict_set_str(dict, "volname", volname); + if (ret) { + gf_log(THIS->name, GF_LOG_WARNING, "dict set failed"); + goto out; + } + + if (!strcmp(volname, GLUSTER_SHARED_STORAGE)) { + question = + "Deleting the shared storage volume" + "(gluster_shared_storage), will affect features " + "like snapshot scheduler, geo-replication " + "and NFS-Ganesha. Do you still want to " + "continue?"; + } + + answer = cli_cmd_get_confirmation(state, question); + if (GF_ANSWER_NO == answer) { + ret = 0; + goto out; + } + + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + ret = -1; + goto out; + } + + CLI_LOCAL_INIT(local, words, frame, dict); + + if (proc->fn) { + ret = proc->fn(frame, THIS, dict); + } + +out: + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_out("Volume delete failed"); + } + + CLI_STACK_DESTROY(frame); + + if (ret == 0 && GF_ANSWER_YES == answer) { + gf_event(EVENT_VOLUME_DELETE, "name=%s", (char *)words[2]); + } + + return ret; } int -cli_cmd_volume_info_cbk (struct cli_state *state, struct cli_cmd_word *word, +cli_cmd_volume_start_cbk(struct cli_state *state, struct cli_cmd_word *word, const char **words, int wordcount) { - int ret = -1; - rpc_clnt_procedure_t *proc = NULL; - call_frame_t *frame = NULL; - cli_cmd_volume_get_ctx_t ctx = {0,}; - cli_local_t *local = NULL; - - proc = &cli_rpc_prog->proctable[GF1_CLI_GET_VOLUME]; + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + int sent = 0; + int parse_error = 0; + dict_t *dict = NULL; + int flags = 0; + cli_local_t *local = NULL; + + if (wordcount < 3 || wordcount > 4) { + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } + + if (!words[2]) + goto out; + + if (wordcount == 4) { + if (!strcmp("force", words[3])) { + flags |= GF_CLI_FLAG_OP_FORCE; + } else { + ret = -1; + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } + } - frame = create_frame (THIS, THIS->ctx->pool); - if (!frame) - goto out; + dict = dict_new(); + if (!dict) { + goto out; + } - if ((wordcount == 2) || (wordcount == 3 && - !strcmp (words[2], "all"))) { - ctx.flags = GF_CLI_GET_NEXT_VOLUME; - proc = &cli_rpc_prog->proctable[GF1_CLI_GET_NEXT_VOLUME]; - } else if (wordcount == 3) { - ctx.flags = GF_CLI_GET_VOLUME; - ctx.volname = (char *)words[2]; - if (strlen (ctx.volname) > 1024) { - cli_out ("Invalid volume name"); - goto out; - } - proc = &cli_rpc_prog->proctable[GF1_CLI_GET_VOLUME]; - } + ret = dict_set_str(dict, "volname", (char *)words[2]); + if (ret) { + gf_log(THIS->name, GF_LOG_ERROR, "dict set failed"); + goto out; + } - local = cli_local_get (); + ret = dict_set_int32(dict, "flags", flags); + if (ret) { + gf_log(THIS->name, GF_LOG_ERROR, "dict set failed"); + goto out; + } - if (!local) - goto out; + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_START_VOLUME]; - local->u.get_vol.flags = ctx.flags; - if (ctx.volname) - local->u.get_vol.volname = gf_strdup (ctx.volname); + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + ret = -1; + goto out; + } - frame->local = local; + CLI_LOCAL_INIT(local, words, frame, dict); - if (proc->fn) { - ret = proc->fn (frame, THIS, &ctx); - } + if (proc->fn) { + ret = proc->fn(frame, THIS, dict); + } out: - if (ret) - cli_out ("Getting Volume information failed!"); - return ret; + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_out("Volume start failed"); + } + + CLI_STACK_DESTROY(frame); + if (ret == 0) { + gf_event(EVENT_VOLUME_START, "name=%s;force=%d", (char *)words[2], + (flags & GF_CLI_FLAG_OP_FORCE)); + } + + return ret; } -void -cli_cmd_volume_create_usage () +gf_answer_t +cli_cmd_get_confirmation(struct cli_state *state, const char *question) { - cli_out ("Usage: volume create <NEW-VOLNAME> " - "[stripe <COUNT>] [replica <COUNT>] [transport <tcp|rdma>] " - "<NEW-BRICK> ..."); + char answer[5] = { + '\0', + }; + int flush = '\0'; + size_t len; + + if (state->mode & GLUSTER_MODE_SCRIPT) + return GF_ANSWER_YES; + + printf("%s (y/n) ", question); + + if (fgets(answer, 4, stdin) == NULL) { + cli_out("gluster cli read error"); + goto out; + } + + len = strlen(answer); + + if (len && answer[len - 1] == '\n') { + answer[--len] = '\0'; + } else { + do { + flush = getchar(); + } while (flush != '\n'); + } + + if (len > 3) + goto out; + + if (!strcasecmp(answer, "y") || !strcasecmp(answer, "yes")) + return GF_ANSWER_YES; + + else if (!strcasecmp(answer, "n") || !strcasecmp(answer, "no")) + return GF_ANSWER_NO; + +out: + cli_out("Invalid input, please enter y/n"); + + return GF_ANSWER_NO; } int -cli_cmd_volume_create_cbk (struct cli_state *state, struct cli_cmd_word *word, - const char **words, int wordcount) +cli_cmd_volume_stop_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) { - int ret = -1; - rpc_clnt_procedure_t *proc = NULL; - call_frame_t *frame = NULL; - dict_t *options = NULL; + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + int flags = 0; + gf_answer_t answer = GF_ANSWER_NO; + int sent = 0; + int parse_error = 0; + dict_t *dict = NULL; + char *volname = NULL; + cli_local_t *local = NULL; + + const char *question = + "Stopping volume will make its data inaccessible. " + "Do you want to continue?"; + + if (wordcount < 3 || wordcount > 4) { + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } + + volname = (char *)words[2]; + + dict = dict_new(); + ret = dict_set_str(dict, "volname", volname); + if (ret) { + gf_log(THIS->name, GF_LOG_ERROR, "dict set failed"); + goto out; + } + + if (!strcmp(volname, GLUSTER_SHARED_STORAGE)) { + question = + "Stopping the shared storage volume" + "(gluster_shared_storage), will affect features " + "like snapshot scheduler, geo-replication " + "and NFS-Ganesha. Do you still want to " + "continue?"; + } + + if (wordcount == 4) { + if (!strcmp("force", words[3])) { + flags |= GF_CLI_FLAG_OP_FORCE; + } else { + ret = -1; + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } + } - proc = &cli_rpc_prog->proctable[GF1_CLI_CREATE_VOLUME]; + ret = dict_set_int32(dict, "flags", flags); + if (ret) { + gf_log(THIS->name, GF_LOG_ERROR, "dict set failed"); + goto out; + } - frame = create_frame (THIS, THIS->ctx->pool); - if (!frame) - goto out; + answer = cli_cmd_get_confirmation(state, question); - ret = cli_cmd_volume_create_parse (words, wordcount, &options); + if (GF_ANSWER_NO == answer) { + ret = 0; + goto out; + } - if (ret) { - cli_cmd_volume_create_usage (); - goto out; - } + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_STOP_VOLUME]; - if (proc->fn) { - ret = proc->fn (frame, THIS, options); - } + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + ret = -1; + goto out; + } -out: - if (ret) { - if (wordcount > 2) { - char *volname = (char *) words[2]; - cli_out ("Creating Volume %s failed",volname ); - } - } - if (options) - dict_unref (options); + CLI_LOCAL_INIT(local, words, frame, dict); - return ret; -} + if (proc->fn) { + ret = proc->fn(frame, THIS, dict); + } +out: + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_out("Volume stop on '%s' failed", volname); + } + + CLI_STACK_DESTROY(frame); + if (dict) + dict_unref(dict); + + if (ret == 0 && GF_ANSWER_YES == answer) { + gf_event(EVENT_VOLUME_STOP, "name=%s;force=%d", (char *)words[2], + (flags & GF_CLI_FLAG_OP_FORCE)); + } + + return ret; +} int -cli_cmd_volume_delete_cbk (struct cli_state *state, struct cli_cmd_word *word, - const char **words, int wordcount) +cli_cmd_volume_rename_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) { - int ret = -1; - rpc_clnt_procedure_t *proc = NULL; - call_frame_t *frame = NULL; - char *volname = NULL; + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + dict_t *dict = NULL; + int sent = 0; + int parse_error = 0; - proc = &cli_rpc_prog->proctable[GF1_CLI_DELETE_VOLUME]; + if (wordcount != 4) { + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } - frame = create_frame (THIS, THIS->ctx->pool); - if (!frame) - goto out; + dict = dict_new(); + if (!dict) + goto out; - if (wordcount != 3) { - cli_cmd_volume_delete_usage (); - goto out; - } + ret = dict_set_str(dict, "old-volname", (char *)words[2]); - volname = (char *)words[2]; + if (ret) + goto out; - if (proc->fn) { - ret = proc->fn (frame, THIS, volname); + ret = dict_set_str(dict, "new-volname", (char *)words[3]); + + if (ret) + goto out; + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_RENAME_VOLUME]; + + if (proc->fn) { + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + ret = -1; + goto out; } + ret = proc->fn(frame, THIS, dict); + } out: - if (ret && volname) - cli_out ("Deleting Volume %s failed", volname); + if (dict) + dict_unref(dict); - return ret; -} + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_out("Volume rename on '%s' failed", (char *)words[2]); + } + CLI_STACK_DESTROY(frame); + + return ret; +} int -cli_cmd_volume_start_cbk (struct cli_state *state, struct cli_cmd_word *word, +cli_cmd_volume_defrag_cbk(struct cli_state *state, struct cli_cmd_word *word, const char **words, int wordcount) { - int ret = -1; - rpc_clnt_procedure_t *proc = NULL; - call_frame_t *frame = NULL; - char *volname = NULL; + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + dict_t *dict = NULL; + int sent = 0; + int parse_error = 0; + cli_local_t *local = NULL; +#if (USE_EVENTS) + eventtypes_t event = EVENT_LAST; +#endif +#ifdef GF_SOLARIS_HOST_OS + cli_out("Command not supported on Solaris"); + goto out; +#endif - frame = create_frame (THIS, THIS->ctx->pool); - if (!frame) - goto out; + ret = cli_cmd_volume_defrag_parse(words, wordcount, &dict); - if (wordcount != 3) { - cli_cmd_volume_start_usage (); - goto out; - } + if (ret) { + cli_usage_out(word->pattern); + parse_error = 1; + } - volname = (char *)words[2]; + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_DEFRAG_VOLUME]; - proc = &cli_rpc_prog->proctable[GF1_CLI_START_VOLUME]; + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + ret = -1; + goto out; + } - if (proc->fn) { - ret = proc->fn (frame, THIS, volname); - } + CLI_LOCAL_INIT(local, words, frame, dict); + + if (proc->fn) { + ret = proc->fn(frame, THIS, dict); + } out: - if (!proc && ret && volname) - cli_out ("Starting Volume %s failed", volname); + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_out("Volume rebalance failed"); + } else { +#if (USE_EVENTS) + if (!(strcmp(words[wordcount - 1], "start")) || + !(strcmp(words[wordcount - 1], "force"))) { + event = EVENT_VOLUME_REBALANCE_START; + } else if (!strcmp(words[wordcount - 1], "stop")) { + event = EVENT_VOLUME_REBALANCE_STOP; + } - return ret; + if (event != EVENT_LAST) + gf_event(event, "volume=%s", (char *)words[2]); +#endif + } + + CLI_STACK_DESTROY(frame); + + return ret; } +int +cli_cmd_volume_reset_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) +{ + int sent = 0; + int parse_error = 0; + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + dict_t *options = NULL; + cli_local_t *local = NULL; +#if (USE_EVENTS) + int ret1 = -1; + char *tmp_opt = NULL; +#endif + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_RESET_VOLUME]; + + ret = cli_cmd_volume_reset_parse(words, wordcount, &options); + if (ret) { + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } + + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + ret = -1; + goto out; + } + + CLI_LOCAL_INIT(local, words, frame, options); + + if (proc->fn) { + ret = proc->fn(frame, THIS, options); + } + +out: + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_out("Volume reset failed"); + } + +#if (USE_EVENTS) + if (ret == 0) { + ret1 = dict_get_str(options, "key", &tmp_opt); + if (ret1) + tmp_opt = ""; + + gf_event(EVENT_VOLUME_RESET, "name=%s;option=%s", (char *)words[2], + tmp_opt); + } +#endif + + CLI_STACK_DESTROY(frame); + + return ret; +} int -cli_cmd_volume_stop_cbk (struct cli_state *state, struct cli_cmd_word *word, +cli_cmd_volume_profile_cbk(struct cli_state *state, struct cli_cmd_word *word, const char **words, int wordcount) { - int ret = -1; - rpc_clnt_procedure_t *proc = NULL; - call_frame_t *frame = NULL; - int flags = 0; - gf1_cli_stop_vol_req req = {0,}; + int sent = 0; + int parse_error = 0; - frame = create_frame (THIS, THIS->ctx->pool); - if (!frame) - goto out; + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + dict_t *options = NULL; + cli_local_t *local = NULL; - if (wordcount < 3 || wordcount > 4) { - cli_cmd_volume_stop_usage (); - goto out; - } + ret = cli_cmd_volume_profile_parse(words, wordcount, &options); - req.volname = (char *)words[2]; - if (!req.volname) - goto out; + if (ret) { + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } - if (wordcount == 4) { - if (!strcmp("force", words[3])) { - flags |= GF_CLI_FLAG_OP_FORCE; - } else { - ret = -1; - cli_cmd_volume_stop_usage (); - goto out; - } - } + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_PROFILE_VOLUME]; - req.flags = flags; - proc = &cli_rpc_prog->proctable[GF1_CLI_STOP_VOLUME]; + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + gf_log(THIS->name, GF_LOG_ERROR, "failed to create frame"); + ret = -1; + goto out; + } - if (proc->fn) { - ret = proc->fn (frame, THIS, &req); - } + CLI_LOCAL_INIT(local, words, frame, options); + + if (proc->fn) { + ret = proc->fn(frame, THIS, options); + } out: - if (!proc && ret && req.volname) - cli_out ("Stopping Volume %s failed", req.volname); + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_out("Volume profile failed"); + } - return ret; -} + CLI_STACK_DESTROY(frame); + return ret; +} int -cli_cmd_volume_rename_cbk (struct cli_state *state, struct cli_cmd_word *word, - const char **words, int wordcount) +cli_cmd_volume_set_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) { - int ret = -1; - rpc_clnt_procedure_t *proc = NULL; - call_frame_t *frame = NULL; - dict_t *dict = NULL; + int sent = 0; + int parse_error = 0; + + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + dict_t *options = NULL; + cli_local_t *local = NULL; + char *op_errstr = NULL; + +#if (USE_EVENTS) + int ret1 = -1; + int i = 1; + char dict_key[50] = { + 0, + }; + char *tmp_opt = NULL; + char *opts_str = NULL; + int num_options = 0; +#endif + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_SET_VOLUME]; - frame = create_frame (THIS, THIS->ctx->pool); - if (!frame) - goto out; + ret = cli_cmd_volume_set_parse(state, words, wordcount, &options, + &op_errstr); + if (ret) { + if (op_errstr) { + cli_err("%s", op_errstr); + GF_FREE(op_errstr); + } else + cli_usage_out(word->pattern); - dict = dict_new (); - if (!dict) - goto out; + parse_error = 1; + goto out; + } - if (wordcount != 4) { - cli_cmd_volume_rename_usage (); - goto out; + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + ret = -1; + goto out; + } + + CLI_LOCAL_INIT(local, words, frame, options); + + if (proc->fn) { + ret = proc->fn(frame, THIS, options); + } + +out: + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_out("Volume set failed"); + } + +#if (USE_EVENTS) + if (ret == 0 && strcmp(words[2], "help") != 0) { + ret1 = dict_get_int32(options, "count", &num_options); + if (ret1) { + num_options = 0; + goto end; + } else { + num_options = num_options / 2; } - ret = dict_set_str (dict, "old-volname", (char *)words[2]); + char *free_list_key[num_options]; + char *free_list_val[num_options]; + for (i = 0; i < num_options; i++) { + free_list_key[i] = NULL; + free_list_val[i] = NULL; + } + /* Initialize opts_str */ + opts_str = ""; + + /* Prepare String in format options=KEY1,VALUE1,KEY2,VALUE2 */ + for (i = 1; i <= num_options; i++) { + sprintf(dict_key, "key%d", i); + ret1 = dict_get_str(options, dict_key, &tmp_opt); + if (ret1) + tmp_opt = ""; + + gf_asprintf(&opts_str, "%s,%s", opts_str, tmp_opt); + free_list_key[i - 1] = opts_str; + + sprintf(dict_key, "value%d", i); + ret1 = dict_get_str(options, dict_key, &tmp_opt); + if (ret1) + tmp_opt = ""; + + gf_asprintf(&opts_str, "%s,%s", opts_str, tmp_opt); + free_list_val[i - 1] = opts_str; + } - if (ret) - goto out; + gf_event(EVENT_VOLUME_SET, "name=%s;options=%s", (char *)words[2], + opts_str); - ret = dict_set_str (dict, "new-volname", (char *)words[3]); + /* Allocated by gf_strdup and gf_asprintf */ + for (i = 0; i < num_options; i++) { + GF_FREE(free_list_key[i]); + GF_FREE(free_list_val[i]); + } + } +#endif - if (ret) - goto out; +end: + CLI_STACK_DESTROY(frame); - proc = &cli_rpc_prog->proctable[GF1_CLI_RENAME_VOLUME]; + return ret; +} - if (proc->fn) { - ret = proc->fn (frame, THIS, dict); +static int +cli_event_remove_brick_str(dict_t *options, char **event_str, + eventtypes_t *event) +{ + int ret = -1; + char *bricklist = NULL; + char *brick = NULL; + char *volname = NULL; + char key[256] = { + 0, + }; + const char *eventstrformat = "volume=%s;bricks=%s"; + int32_t command = 0; + int32_t i = 1; + int32_t count = 0; + int32_t eventstrlen = 1; + int bricklen = 0; + char *tmp_ptr = NULL; + + if (!options || !event_str || !event) + goto out; + + ret = dict_get_str(options, "volname", &volname); + if (ret || !volname) { + gf_log("cli", GF_LOG_ERROR, "Failed to fetch volname"); + ret = -1; + goto out; + } + /* Get the list of bricks for the event */ + ret = dict_get_int32(options, "command", &command); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to fetch command"); + ret = -1; + goto out; + } + + switch (command) { + case GF_OP_CMD_START: + *event = EVENT_VOLUME_REMOVE_BRICK_START; + break; + case GF_OP_CMD_COMMIT: + *event = EVENT_VOLUME_REMOVE_BRICK_COMMIT; + break; + case GF_OP_CMD_COMMIT_FORCE: + *event = EVENT_VOLUME_REMOVE_BRICK_FORCE; + break; + case GF_OP_CMD_STOP: + *event = EVENT_VOLUME_REMOVE_BRICK_STOP; + break; + default: + *event = EVENT_LAST; + break; + } + + ret = -1; + + if (*event == EVENT_LAST) { + goto out; + } + + /* I could just get this from words[] but this is cleaner in case the + * format changes */ + while (i) { + snprintf(key, sizeof(key), "brick%d", i); + ret = dict_get_str(options, key, &brick); + if (ret) { + break; } + eventstrlen += strlen(brick) + 1; + i++; + } + + count = --i; + eventstrlen += 1; + + bricklist = GF_CALLOC(eventstrlen, sizeof(char), gf_common_mt_char); + if (!bricklist) { + gf_log(THIS->name, GF_LOG_ERROR, + "memory allocation failed for" + "bricklist"); + ret = -1; + goto out; + } + + tmp_ptr = bricklist; + + i = 1; + while (i <= count) { + snprintf(key, sizeof(key), "brick%d", i); + ret = dict_get_str(options, key, &brick); + if (ret) { + break; + } + snprintf(tmp_ptr, eventstrlen, "%s ", brick); + bricklen = strlen(brick); + eventstrlen -= (bricklen + 1); + tmp_ptr += (bricklen + 1); + i++; + } + + if (!ret) { + gf_asprintf(event_str, eventstrformat, volname, bricklist); + } else { + gf_asprintf(event_str, eventstrformat, volname, "<unavailable>"); + } + + ret = 0; out: - if (!proc && ret) { - if (dict) - dict_destroy (dict); - if (wordcount > 2) - cli_out ("Renaming Volume %s failed", (char *)words[2]); + GF_FREE(bricklist); + return ret; +} + +int +cli_cmd_volume_add_brick_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) +{ + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + dict_t *options = NULL; + int sent = 0; + int parse_error = 0; + gf_answer_t answer = GF_ANSWER_NO; + cli_local_t *local = NULL; + +#if (USE_EVENTS) + char *event_str = NULL; + char *bricks = NULL; + const char *eventstrformat = "volume=%s;bricks=%s"; +#endif + + const char *question = + "Changing the 'stripe count' of the volume is " + "not a supported feature. In some cases it may result in data " + "loss on the volume. Also there may be issues with regular " + "filesystem operations on the volume after the change. Do you " + "really want to continue with 'stripe' count option ? "; + + ret = cli_cmd_volume_add_brick_parse(state, words, wordcount, &options, 0); + if (ret) { + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } + + /* TODO: there are challenges in supporting changing of + stripe-count, until it is properly supported give warning to user */ + if (dict_get(options, "stripe-count")) { + answer = cli_cmd_get_confirmation(state, question); + + if (GF_ANSWER_NO == answer) { + ret = 0; + goto out; } + } + +#if (USE_EVENTS) + /* Get the list of bricks for the event */ + + ret = dict_get_str(options, "bricks", &bricks); + + if (!ret) { + gf_asprintf(&event_str, eventstrformat, (char *)words[2], + &bricks[1] /*Skip leading space*/); + } else { + gf_asprintf(&event_str, eventstrformat, (char *)words[2], + "<unavailable>"); + } +#endif + + if (state->mode & GLUSTER_MODE_WIGNORE) { + ret = dict_set_int32(options, "force", _gf_true); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to set force " + "option"); + goto out; + } + } + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_ADD_BRICK]; + + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + ret = -1; + goto out; + } + + CLI_LOCAL_INIT(local, words, frame, options); + + if (proc->fn) { + ret = proc->fn(frame, THIS, options); + } - return ret; +out: + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_out("Volume add-brick failed"); + } else { +#if (USE_EVENTS) + gf_event(EVENT_VOLUME_ADD_BRICK, "%s", event_str); +#endif + } +#if (USE_EVENTS) + GF_FREE(event_str); +#endif + + CLI_STACK_DESTROY(frame); + return ret; } -void -cli_cmd_volume_defrag_usage () +int +cli_get_soft_limit(dict_t *options, const char **words, dict_t *xdata) +{ + call_frame_t *frame = NULL; + cli_local_t *local = NULL; + rpc_clnt_procedure_t *proc = NULL; + char *default_sl = NULL; + char *default_sl_dup = NULL; + int ret = -1; + + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + ret = -1; + goto out; + } + + // We need a ref on @options to prevent CLI_STACK_DESTROY + // from destroying it prematurely. + dict_ref(options); + CLI_LOCAL_INIT(local, words, frame, options); + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_QUOTA]; + ret = proc->fn(frame, THIS, options); + + ret = dict_get_str(options, "default-soft-limit", &default_sl); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get default soft limit"); + goto out; + } + + default_sl_dup = gf_strdup(default_sl); + if (!default_sl_dup) { + ret = -1; + goto out; + } + + ret = dict_set_dynstr(xdata, "default-soft-limit", default_sl_dup); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to set default soft limit"); + GF_FREE(default_sl_dup); + goto out; + } + +out: + CLI_STACK_DESTROY(frame); + return ret; +} + +/* Checks if at least one limit has been set on the volume + * + * Returns true if at least one limit is set. Returns false otherwise. + */ +gf_boolean_t +_limits_set_on_volume(char *volname, int type) { - cli_out ("Usage: volume rebalance <VOLNAME> <start|stop|status>"); + gf_boolean_t limits_set = _gf_false; + int ret = -1; + char quota_conf_file[PATH_MAX] = { + 0, + }; + int fd = -1; + char buf[16] = { + 0, + }; + float version = 0.0f; + char gfid_type_stored = 0; + char gfid_type = 0; + + /* TODO: fix hardcoding; Need to perform an RPC call to glusterd + * to fetch working directory + */ + snprintf(quota_conf_file, sizeof quota_conf_file, "%s/vols/%s/quota.conf", + GLUSTERD_DEFAULT_WORKDIR, volname); + fd = open(quota_conf_file, O_RDONLY); + if (fd == -1) + goto out; + + ret = quota_conf_read_version(fd, &version); + if (ret) + goto out; + + if (type == GF_QUOTA_OPTION_TYPE_LIST) + gfid_type = GF_QUOTA_CONF_TYPE_USAGE; + else + gfid_type = GF_QUOTA_CONF_TYPE_OBJECTS; + + /* Try to read at least one gfid of type 'gfid_type' */ + while (1) { + ret = quota_conf_read_gfid(fd, buf, &gfid_type_stored, version); + if (ret <= 0) + break; + + if (gfid_type_stored == gfid_type) { + limits_set = _gf_true; + break; + } + } +out: + if (fd != -1) + sys_close(fd); + + return limits_set; } int -cli_cmd_volume_defrag_cbk (struct cli_state *state, struct cli_cmd_word *word, - const char **words, int wordcount) +cli_cmd_quota_handle_list_all(const char **words, dict_t *options) { - int ret = -1; - rpc_clnt_procedure_t *proc = NULL; - call_frame_t *frame = NULL; - dict_t *dict = NULL; + int all_failed = 1; + int count = 0; + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + cli_local_t *local = NULL; + call_frame_t *frame = NULL; + dict_t *xdata = NULL; + char gfid_str[UUID_CANONICAL_FORM_LEN + 1]; + char *volname = NULL; + char *volname_dup = NULL; + unsigned char buf[16] = {0}; + int fd = -1; + char quota_conf_file[PATH_MAX] = {0}; + gf_boolean_t xml_err_flag = _gf_false; + char err_str[NAME_MAX] = { + 0, + }; + int32_t type = 0; + char gfid_type = 0; + float version = 0.0f; + int32_t max_count = 0; + + xdata = dict_new(); + if (!xdata) { + ret = -1; + goto out; + } + + ret = dict_get_str(options, "volname", &volname); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get volume name"); + goto out; + } + + ret = dict_get_int32(options, "type", &type); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get quota option type"); + goto out; + } + + ret = dict_set_int32(xdata, "type", type); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to set type in xdata"); + goto out; + } + + ret = cli_get_soft_limit(options, words, xdata); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to fetch default " + "soft-limit"); + goto out; + } + + /* Check if at least one limit is set on volume. No need to check for + * quota enabled as cli_get_soft_limit() handles that + */ + if (!_limits_set_on_volume(volname, type)) { + snprintf(err_str, sizeof(err_str), + "No%s quota configured on" + " volume %s", + (type == GF_QUOTA_OPTION_TYPE_LIST) ? "" : " inode", volname); + if (global_state->mode & GLUSTER_MODE_XML) { + xml_err_flag = _gf_true; + } else { + cli_out("quota: %s", err_str); + } + ret = 0; + goto out; + } + + volname_dup = gf_strdup(volname); + if (!volname_dup) { + ret = -1; + goto out; + } + + ret = dict_set_dynstr(xdata, "volume-uuid", volname_dup); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to set volume-uuid"); + GF_FREE(volname_dup); + goto out; + } + + // TODO: fix hardcoding; Need to perform an RPC call to glusterd + // to fetch working directory + snprintf(quota_conf_file, sizeof quota_conf_file, "%s/vols/%s/quota.conf", + GLUSTERD_DEFAULT_WORKDIR, volname); + fd = open(quota_conf_file, O_RDONLY); + if (fd == -1) { + // This may because no limits were yet set on the volume + gf_log("cli", GF_LOG_TRACE, + "Unable to open " + "quota.conf"); + ret = 0; + goto out; + } + + ret = quota_conf_read_version(fd, &version); + if (ret) + goto out; + + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + ret = -1; + goto out; + } + + CLI_LOCAL_INIT(local, words, frame, xdata); + proc = &cli_quotad_clnt.proctable[GF_AGGREGATOR_GETLIMIT]; + + for (count = 0;; count++) { + ret = quota_conf_read_gfid(fd, buf, &gfid_type, version); + if (ret == 0) { + break; + } else if (ret < 0) { + gf_log(THIS->name, GF_LOG_CRITICAL, + "Quota " + "configuration store may be corrupt."); + goto out; + } - frame = create_frame (THIS, THIS->ctx->pool); - if (!frame) - goto out; + if ((type == GF_QUOTA_OPTION_TYPE_LIST && + gfid_type == GF_QUOTA_CONF_TYPE_OBJECTS) || + (type == GF_QUOTA_OPTION_TYPE_LIST_OBJECTS && + gfid_type == GF_QUOTA_CONF_TYPE_USAGE)) + continue; + + max_count++; + } + ret = dict_set_int32(xdata, "max_count", max_count); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to set max_count"); + goto out; + } + + ret = sys_lseek(fd, 0L, SEEK_SET); + if (ret < 0) { + gf_log(THIS->name, GF_LOG_ERROR, + "failed to move offset to " + "the beginning: %s", + strerror(errno)); + goto out; + } + ret = quota_conf_read_version(fd, &version); + if (ret) + goto out; + + for (count = 0;; count++) { + ret = quota_conf_read_gfid(fd, buf, &gfid_type, version); + if (ret == 0) { + break; + } else if (ret < 0) { + gf_log(THIS->name, GF_LOG_CRITICAL, + "Quota " + "configuration store may be corrupt."); + goto out; + } - dict = dict_new (); - if (!dict) - goto out; + if ((type == GF_QUOTA_OPTION_TYPE_LIST && + gfid_type == GF_QUOTA_CONF_TYPE_OBJECTS) || + (type == GF_QUOTA_OPTION_TYPE_LIST_OBJECTS && + gfid_type == GF_QUOTA_CONF_TYPE_USAGE)) + continue; - if (wordcount != 4) { - cli_cmd_volume_defrag_usage(); - goto out; + uuid_utoa_r(buf, gfid_str); + ret = dict_set_str(xdata, "gfid", gfid_str); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to set gfid"); + goto out; } - ret = dict_set_str (dict, "volname", (char *)words[2]); - if (ret) - goto out; + ret = proc->fn(frame, THIS, xdata); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to get quota " + "limits for %s", + uuid_utoa((unsigned char *)buf)); + } - ret = dict_set_str (dict, "command", (char *)words[3]); - if (ret) - goto out; + dict_del(xdata, "gfid"); + all_failed = all_failed && ret; + } + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_vol_quota_limit_list_end(local); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Error in printing " + "xml output"); + goto out; + } + } - proc = &cli_rpc_prog->proctable[GF1_CLI_DEFRAG_VOLUME]; + if (count > 0) { + ret = all_failed ? -1 : 0; + } else { + ret = 0; + } - if (proc->fn) { - ret = proc->fn (frame, THIS, dict); +out: + if (xml_err_flag) { + ret = cli_xml_output_str("volQuota", NULL, -1, 0, err_str); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Error outputting in " + "xml format"); } + } + if (xdata) + dict_unref(xdata); + + if (fd != -1) { + sys_close(fd); + } + + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Could not fetch and display quota" + " limits"); + } + CLI_STACK_DESTROY(frame); + return ret; +} + +int +cli_cmd_bitrot_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) +{ + int ret = -1; + int parse_err = 0; + call_frame_t *frame = NULL; + dict_t *options = NULL; + cli_local_t *local = NULL; + rpc_clnt_procedure_t *proc = NULL; + int sent = 0; +#if (USE_EVENTS) + int cmd_type = -1; + int ret1 = -1; + int event_type = -1; + char *tmp = NULL; + char *events_str = NULL; + char *volname = NULL; +#endif + + ret = cli_cmd_bitrot_parse(words, wordcount, &options); + if (ret < 0) { + cli_usage_out(word->pattern); + parse_err = 1; + goto out; + } + + if (ret == 1) { + /* this is 'volume bitrot help' */ + cli_cmd_bitrot_help_cbk(state, word, words, wordcount); + ret = 0; + goto out2; + } + + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + ret = -1; + goto out; + } + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_BITROT]; + + CLI_LOCAL_INIT(local, words, frame, options); + + if (proc->fn) { + ret = proc->fn(frame, THIS, options); + } out: - if (!proc && ret) { - if (dict) - dict_destroy (dict); + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_err == 0)) + cli_err( + "Bit rot command failed. Please check the cli " + "logs for more details"); + } + +#if (USE_EVENTS) + if (ret == 0) { + ret1 = dict_get_int32(options, "type", &cmd_type); + if (ret1) + cmd_type = -1; + else { + ret1 = dict_get_str(options, "volname", &volname); + if (ret1) + volname = ""; + } - if (wordcount > 2) - cli_out ("Rebalance of Volume %s failed", - (char *)words[2]); + switch (cmd_type) { + case GF_BITROT_OPTION_TYPE_ENABLE: + event_type = EVENT_BITROT_ENABLE; + break; + case GF_BITROT_OPTION_TYPE_DISABLE: + event_type = EVENT_BITROT_DISABLE; + break; + case GF_BITROT_CMD_SCRUB_ONDEMAND: + event_type = EVENT_BITROT_SCRUB_ONDEMAND; + break; + case GF_BITROT_OPTION_TYPE_SCRUB_THROTTLE: + event_type = EVENT_BITROT_SCRUB_THROTTLE; + ret1 = dict_get_str(options, "scrub-throttle-value", &tmp); + if (ret1) + tmp = ""; + gf_asprintf(&events_str, "name=%s;value=%s", volname, tmp); + break; + case GF_BITROT_OPTION_TYPE_SCRUB_FREQ: + event_type = EVENT_BITROT_SCRUB_FREQ; + ret1 = dict_get_str(options, "scrub-frequency-value", &tmp); + if (ret1) + tmp = ""; + gf_asprintf(&events_str, "name=%s;value=%s", volname, tmp); + break; + case GF_BITROT_OPTION_TYPE_SCRUB: + event_type = EVENT_BITROT_SCRUB_OPTION; + ret1 = dict_get_str(options, "scrub-value", &tmp); + if (ret1) + tmp = ""; + gf_asprintf(&events_str, "name=%s;value=%s", volname, tmp); + break; + default: + break; } - return 0; -} + if (event_type > -1) + gf_event(event_type, "%s", events_str); + + if (events_str) + GF_FREE(events_str); + } +#endif + CLI_STACK_DESTROY(frame); +out2: + return ret; +} int -cli_cmd_volume_set_cbk (struct cli_state *state, struct cli_cmd_word *word, - const char **words, int wordcount) +cli_cmd_quota_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) { - cli_cmd_broadcast_response (0); - return 0; + int ret = 0; + int parse_err = 0; + int32_t type = 0; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + dict_t *options = NULL; + gf_answer_t answer = GF_ANSWER_NO; + cli_local_t *local = NULL; + int sent = 0; + char *volname = NULL; + const char *question = + "Disabling quota will delete all the quota " + "configuration. Do you want to continue?"; + + // parse **words into options dictionary + if (strcmp(words[1], "inode-quota") == 0) { + ret = cli_cmd_inode_quota_parse(words, wordcount, &options); + if (ret < 0) { + cli_usage_out(word->pattern); + parse_err = 1; + goto out; + } + } else { + ret = cli_cmd_quota_parse(words, wordcount, &options); + + if (ret == 1) { + cli_cmd_quota_help_cbk(state, word, words, wordcount); + ret = 0; + goto out; + } + if (ret < 0) { + cli_usage_out(word->pattern); + parse_err = 1; + goto out; + } + } + + ret = dict_get_int32(options, "type", &type); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get opcode"); + goto out; + } + + // handle quota-disable and quota-list-all different from others + switch (type) { + case GF_QUOTA_OPTION_TYPE_DISABLE: + answer = cli_cmd_get_confirmation(state, question); + if (answer == GF_ANSWER_NO) + goto out; + break; + case GF_QUOTA_OPTION_TYPE_LIST: + case GF_QUOTA_OPTION_TYPE_LIST_OBJECTS: + if (wordcount != 4) + break; + ret = cli_cmd_quota_handle_list_all(words, options); + goto out; + default: + break; + } + + ret = dict_get_str(options, "volname", &volname); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get volume name"); + goto out; + } + + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + ret = -1; + goto out; + } + + CLI_LOCAL_INIT(local, words, frame, options); + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_QUOTA]; + + if (proc->fn) + ret = proc->fn(frame, THIS, options); + +out: + if (ret) { + cli_cmd_sent_status_get(&sent); + if (sent == 0 && parse_err == 0) + cli_out( + "Quota command failed. Please check the cli " + "logs for more details"); + } + if (options) + dict_unref(options); + + /* Events for Quota */ + if (ret == 0) { + switch (type) { + case GF_QUOTA_OPTION_TYPE_ENABLE: + gf_event(EVENT_QUOTA_ENABLE, "volume=%s", volname); + break; + case GF_QUOTA_OPTION_TYPE_DISABLE: + gf_event(EVENT_QUOTA_DISABLE, "volume=%s", volname); + break; + case GF_QUOTA_OPTION_TYPE_LIMIT_USAGE: + gf_event(EVENT_QUOTA_SET_USAGE_LIMIT, + "volume=%s;" + "path=%s;limit=%s", + volname, words[4], words[5]); + break; + case GF_QUOTA_OPTION_TYPE_LIMIT_OBJECTS: + gf_event(EVENT_QUOTA_SET_OBJECTS_LIMIT, + "volume=%s;" + "path=%s;limit=%s", + volname, words[4], words[5]); + break; + case GF_QUOTA_OPTION_TYPE_REMOVE: + gf_event(EVENT_QUOTA_REMOVE_USAGE_LIMIT, + "volume=%s;" + "path=%s", + volname, words[4]); + break; + case GF_QUOTA_OPTION_TYPE_REMOVE_OBJECTS: + gf_event(EVENT_QUOTA_REMOVE_OBJECTS_LIMIT, + "volume=%s;" + "path=%s", + volname, words[4]); + break; + case GF_QUOTA_OPTION_TYPE_ALERT_TIME: + gf_event(EVENT_QUOTA_ALERT_TIME, "volume=%s;time=%s", volname, + words[4]); + break; + case GF_QUOTA_OPTION_TYPE_SOFT_TIMEOUT: + gf_event(EVENT_QUOTA_SOFT_TIMEOUT, + "volume=%s;" + "soft-timeout=%s", + volname, words[4]); + break; + case GF_QUOTA_OPTION_TYPE_HARD_TIMEOUT: + gf_event(EVENT_QUOTA_HARD_TIMEOUT, + "volume=%s;" + "hard-timeout=%s", + volname, words[4]); + break; + case GF_QUOTA_OPTION_TYPE_DEFAULT_SOFT_LIMIT: + gf_event(EVENT_QUOTA_DEFAULT_SOFT_LIMIT, + "volume=%s;" + "default-soft-limit=%s", + volname, words[4]); + break; + } + } + + CLI_STACK_DESTROY(frame); + return ret; } -void -cli_cmd_volume_add_brick_usage () +int +cli_cmd_volume_remove_brick_cbk(struct cli_state *state, + struct cli_cmd_word *word, const char **words, + int wordcount) { - cli_out ("Usage: volume add-brick <VOLNAME> " - "<NEW-BRICK> ..."); + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + dict_t *options = NULL; + gf_answer_t answer = GF_ANSWER_NO; + int brick_count = 0; + int sent = 0; + int parse_error = 0; + int need_question = 0; + cli_local_t *local = NULL; + char *volname = NULL; +#if (USE_EVENTS) + eventtypes_t event = EVENT_LAST; + char *event_str = NULL; + int event_ret = -1; +#endif + int32_t command = GF_OP_CMD_NONE; + char *question = NULL; + + ret = cli_cmd_volume_remove_brick_parse(state, words, wordcount, &options, + &need_question, &brick_count, + &command); + if (ret) { + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } + + if (command == GF_OP_CMD_COMMIT_FORCE) { + question = + "Remove-brick force will not migrate files from the " + "removed bricks, so they will no longer be available" + " on the volume.\nDo you want to continue?"; + } else if (command == GF_OP_CMD_START) { + question = + "It is recommended that remove-brick be run with" + " cluster.force-migration option disabled to prevent" + " possible data corruption. Doing so will ensure that" + " files that receive writes during migration will not" + " be migrated and will need to be manually copied" + " after the remove-brick commit operation. Please" + " check the value of the option and update accordingly." + " \nDo you want to continue with your current" + " cluster.force-migration settings?"; + } + + if (!brick_count) { + cli_err("No bricks specified"); + cli_usage_out(word->pattern); + parse_error = 1; + ret = -1; + goto out; + } + ret = dict_get_str(options, "volname", &volname); + if (ret || !volname) { + gf_log("cli", GF_LOG_ERROR, "Failed to fetch volname"); + ret = -1; + goto out; + } + +#if (USE_EVENTS) + event_ret = cli_event_remove_brick_str(options, &event_str, &event); +#endif + + if (!strcmp(volname, GLUSTER_SHARED_STORAGE)) { + question = + "Removing brick from the shared storage volume" + "(gluster_shared_storage), will affect features " + "like snapshot scheduler, geo-replication " + "and NFS-Ganesha. Do you still want to " + "continue?"; + need_question = _gf_true; + } + + if (!(state->mode & GLUSTER_MODE_SCRIPT) && need_question) { + answer = cli_cmd_get_confirmation(state, question); + if (GF_ANSWER_NO == answer) { + ret = 0; + goto out; + } + } + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_REMOVE_BRICK]; + + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + ret = -1; + goto out; + } + + CLI_LOCAL_INIT(local, words, frame, options); + + if (proc->fn) { + ret = proc->fn(frame, THIS, options); + } + +out: + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_out("Volume remove-brick failed"); + } +#if (USE_EVENTS) + if (!ret && !event_ret) + gf_event(event, "%s", event_str); + if (event_str) + GF_FREE(event_str); + +#endif + + CLI_STACK_DESTROY(frame); + if (options) + dict_unref(options); + + return ret; } int -cli_cmd_volume_add_brick_cbk (struct cli_state *state, - struct cli_cmd_word *word, const char **words, - int wordcount) +cli_cmd_volume_reset_brick_cbk(struct cli_state *state, + struct cli_cmd_word *word, const char **words, + int wordcount) { - int ret = -1; - rpc_clnt_procedure_t *proc = NULL; - call_frame_t *frame = NULL; - dict_t *options = NULL; + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + dict_t *options = NULL; + int sent = 0; + int parse_error = 0; + cli_local_t *local = NULL; + +#ifdef GF_SOLARIS_HOST_OS + cli_out("Command not supported on Solaris"); + goto out; +#endif + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_RESET_BRICK]; - frame = create_frame (THIS, THIS->ctx->pool); - if (!frame) - goto out; + ret = cli_cmd_volume_reset_brick_parse(words, wordcount, &options); - ret = cli_cmd_volume_add_brick_parse (words, wordcount, &options); + if (ret) { + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } + if (state->mode & GLUSTER_MODE_WIGNORE_PARTITION) { + ret = dict_set_int32(options, "ignore-partition", _gf_true); if (ret) { - cli_cmd_volume_add_brick_usage (); - goto out; + gf_log("cli", GF_LOG_ERROR, + "Failed to set ignore-" + "partition option"); + goto out; } + } - proc = &cli_rpc_prog->proctable[GF1_CLI_ADD_BRICK]; + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + ret = -1; + goto out; + } - if (proc->fn) { - ret = proc->fn (frame, THIS, options); - } + CLI_LOCAL_INIT(local, words, frame, options); + + if (proc->fn) { + ret = proc->fn(frame, THIS, options); + } out: - if (!proc && ret) { - if (wordcount > 2) { - char *volname = (char *) words[2]; - cli_out ("Adding brick to Volume %s failed",volname ); - } + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_out("Volume reset-brick failed"); + } else { + if (wordcount > 5) { + gf_event(EVENT_BRICK_RESET_COMMIT, + "Volume=%s;source-brick=%s;" + "destination-brick=%s", + (char *)words[2], (char *)words[3], (char *)words[4]); + } else { + gf_event(EVENT_BRICK_RESET_START, "Volume=%s;source-brick=%s", + (char *)words[2], (char *)words[3]); } - return ret; -} + } + CLI_STACK_DESTROY(frame); + return ret; +} -void -cli_cmd_volume_remove_brick_usage () +int +cli_cmd_volume_replace_brick_cbk(struct cli_state *state, + struct cli_cmd_word *word, const char **words, + int wordcount) { - cli_out ("Usage: volume remove-brick <VOLNAME> " - "<BRICK> ..."); + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + dict_t *options = NULL; + int sent = 0; + int parse_error = 0; + cli_local_t *local = NULL; + +#ifdef GF_SOLARIS_HOST_OS + cli_out("Command not supported on Solaris"); + goto out; +#endif + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_REPLACE_BRICK]; + + ret = cli_cmd_volume_replace_brick_parse(words, wordcount, &options); + + if (ret) { + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } + + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + ret = -1; + goto out; + } + + CLI_LOCAL_INIT(local, words, frame, options); + + if (proc->fn) { + ret = proc->fn(frame, THIS, options); + } + +out: + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_out("Volume replace-brick failed"); + } else { + gf_event(EVENT_BRICK_REPLACE, + "Volume=%s;source-brick=%s;destination-brick=%s", + (char *)words[2], (char *)words[3], (char *)words[4]); + } + CLI_STACK_DESTROY(frame); + + return ret; } int -cli_cmd_volume_remove_brick_cbk (struct cli_state *state, +cli_cmd_volume_set_transport_cbk(struct cli_state *state, struct cli_cmd_word *word, const char **words, int wordcount) { - int ret = -1; - rpc_clnt_procedure_t *proc = NULL; - call_frame_t *frame = NULL; - dict_t *options = NULL; + cli_cmd_broadcast_response(0); + return 0; +} - frame = create_frame (THIS, THIS->ctx->pool); - if (!frame) - goto out; +int +cli_cmd_volume_top_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) +{ + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + dict_t *options = NULL; + int sent = 0; + int parse_error = 0; + cli_local_t *local = NULL; - ret = cli_cmd_volume_remove_brick_parse (words, wordcount, &options); + ret = cli_cmd_volume_top_parse(words, wordcount, &options); - if (ret) { - cli_cmd_volume_remove_brick_usage (); - goto out; - } + if (ret) { + parse_error = 1; + cli_usage_out(word->pattern); + goto out; + } - proc = &cli_rpc_prog->proctable[GF1_CLI_REMOVE_BRICK]; + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_TOP_VOLUME]; - if (proc->fn) { - ret = proc->fn (frame, THIS, options); - } + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + gf_log(THIS->name, GF_LOG_ERROR, "failed to create frame"); + ret = -1; + goto out; + } + + CLI_LOCAL_INIT(local, words, frame, options); + + if (proc->fn) { + ret = proc->fn(frame, THIS, options); + } out: - if (!proc && ret) { - if (wordcount > 2) { - char *volname = (char *) words[2]; - cli_out ("Removing brick from Volume %s failed",volname ); - } + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_out("Volume top failed"); + } + + CLI_STACK_DESTROY(frame); + + return ret; +} + +int +cli_cmd_log_rotate_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) +{ + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + dict_t *options = NULL; + int sent = 0; + int parse_error = 0; + cli_local_t *local = NULL; + + if (!((wordcount == 4) || (wordcount == 5))) { + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } + + if (!(strcmp("rotate", words[3]) == 0)) { + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_LOG_ROTATE]; + + ret = cli_cmd_log_rotate_parse(words, wordcount, &options); + if (ret) + goto out; + + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + gf_log(THIS->name, GF_LOG_ERROR, "failed to create frame"); + ret = -1; + goto out; + } + + CLI_LOCAL_INIT(local, words, frame, options); + + if (proc->fn) { + ret = proc->fn(frame, THIS, options); + } + +out: + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_out("Volume log rotate failed"); + } + CLI_STACK_DESTROY(frame); + + return ret; +} + +#if (SYNCDAEMON_COMPILE) +static int +cli_check_gsync_present() +{ + char buff[PATH_MAX] = { + 0, + }; + runner_t runner = { + 0, + }; + char *ptr = NULL; + int ret = 0; + + ret = setenv("_GLUSTERD_CALLED_", "1", 1); + if (-1 == ret) { + gf_log("", GF_LOG_WARNING, + "setenv syscall failed, hence could" + "not assert if geo-replication is installed"); + goto out; + } + + runinit(&runner); + runner_add_args(&runner, GSYNCD_PREFIX "/gsyncd", "--version", NULL); + runner_redir(&runner, STDOUT_FILENO, RUN_PIPE); + ret = runner_start(&runner); + if (ret == -1) { + gf_log("", GF_LOG_INFO, "geo-replication not installed"); + goto out; + } + + ptr = fgets(buff, sizeof(buff), runner_chio(&runner, STDOUT_FILENO)); + if (ptr) { + if (!strstr(buff, "gsyncd")) { + ret = -1; + goto out; } - return ret; + } else { + ret = -1; + goto out; + } + + ret = runner_end(&runner); + if (ret) + gf_log("", GF_LOG_ERROR, "geo-replication not installed"); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret ? -1 : 0; } void -cli_cmd_volume_replace_brick_usage () +cli_cmd_check_gsync_exists_cbk(struct cli_cmd *this) { - cli_out("Usage: volume replace-brick <VOLNAME> " - "(<BRICK> <NEW-BRICK>) start|pause|abort|status"); -} + int ret = 0; + ret = cli_check_gsync_present(); + if (ret) + this->disable = _gf_true; +} +#endif int -cli_cmd_volume_replace_brick_cbk (struct cli_state *state, - struct cli_cmd_word *word, - const char **words, - int wordcount) +cli_cmd_volume_gsync_set_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) { - int ret = -1; - rpc_clnt_procedure_t *proc = NULL; - call_frame_t *frame = NULL; - dict_t *options = NULL; + int ret = 0; + int parse_err = 0; + dict_t *options = NULL; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + cli_local_t *local = NULL; + char *errstr = NULL; +#if (USE_EVENTS) + int ret1 = -1; + int cmd_type = -1; + int tmpi = 0; + char *tmp = NULL; + char *events_str = NULL; + int event_type = -1; +#endif - proc = &cli_rpc_prog->proctable[GF1_CLI_REPLACE_BRICK]; + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_GSYNC_SET]; - frame = create_frame (THIS, THIS->ctx->pool); - if (!frame) - goto out; + ret = cli_cmd_gsync_set_parse(state, words, wordcount, &options, &errstr); + if (ret) { + if (errstr) { + cli_err("%s", errstr); + GF_FREE(errstr); + } else { + cli_usage_out(word->pattern); + } + parse_err = 1; + goto out; + } - ret = cli_cmd_volume_replace_brick_parse (words, wordcount, &options); + frame = create_frame(THIS, THIS->ctx->pool); + if (frame == NULL) { + ret = -1; + goto out; + } - if (ret) { - cli_cmd_volume_replace_brick_usage (); - goto out; - } + CLI_LOCAL_INIT(local, words, frame, options); - if (proc->fn) { - ret = proc->fn (frame, THIS, options); - } + if (proc->fn) + ret = proc->fn(frame, THIS, options); out: - if (ret) { - if (wordcount > 2) { - char *volname = (char *) words[2]; - cli_out ("Replacing brick from Volume %s failed",volname ); + if (ret && parse_err == 0) + cli_out(GEOREP " command failed"); + +#if (USE_EVENTS) + if (ret == 0) { + events_str = gf_strdup(""); + + /* Type of Geo-rep Action - Create, Start etc */ + ret1 = dict_get_int32(options, "type", &cmd_type); + if (ret1) + cmd_type = -1; + + /* Only capture Events for modification commands */ + switch (cmd_type) { + case GF_GSYNC_OPTION_TYPE_CREATE: + event_type = EVENT_GEOREP_CREATE; + break; + case GF_GSYNC_OPTION_TYPE_START: + event_type = EVENT_GEOREP_START; + break; + case GF_GSYNC_OPTION_TYPE_STOP: + event_type = EVENT_GEOREP_STOP; + break; + case GF_GSYNC_OPTION_TYPE_PAUSE: + event_type = EVENT_GEOREP_PAUSE; + break; + case GF_GSYNC_OPTION_TYPE_RESUME: + event_type = EVENT_GEOREP_RESUME; + break; + case GF_GSYNC_OPTION_TYPE_DELETE: + event_type = EVENT_GEOREP_DELETE; + break; + case GF_GSYNC_OPTION_TYPE_CONFIG: + ret1 = dict_get_str(options, "subop", &tmp); + if (ret1) + tmp = ""; + + /* For Config Set additionally capture key and value */ + /* For Config Reset capture key */ + if (strcmp(tmp, "set") == 0) { + event_type = EVENT_GEOREP_CONFIG_SET; + + ret1 = dict_get_str(options, "op_name", &tmp); + if (ret1) + tmp = ""; + + gf_asprintf_append(&events_str, "%soption=%s;", events_str, + tmp); + + ret1 = dict_get_str(options, "op_value", &tmp); + if (ret1) + tmp = ""; + + gf_asprintf_append(&events_str, "%svalue=%s;", events_str, + tmp); + } else if (strcmp(tmp, "del") == 0) { + event_type = EVENT_GEOREP_CONFIG_RESET; + + ret1 = dict_get_str(options, "op_name", &tmp); + if (ret1) + tmp = ""; + + gf_asprintf_append(&events_str, "%soption=%s;", events_str, + tmp); } + break; + default: + break; + } + + if (event_type > -1) { + /* Capture all optional arguments used */ + ret1 = dict_get_int32(options, "force", &tmpi); + if (ret1 == 0) { + gf_asprintf_append(&events_str, "%sforce=%d;", events_str, + tmpi); + } + ret1 = dict_get_int32(options, "push_pem", &tmpi); + if (ret1 == 0) { + gf_asprintf_append(&events_str, "%spush_pem=%d;", events_str, + tmpi); + } + ret1 = dict_get_int32(options, "no_verify", &tmpi); + if (ret1 == 0) { + gf_asprintf_append(&events_str, "%sno_verify=%d;", events_str, + tmpi); + } + + ret1 = dict_get_int32(options, "ssh_port", &tmpi); + if (ret1 == 0) { + gf_asprintf_append(&events_str, "%sssh_port=%d;", events_str, + tmpi); + } + + ret1 = dict_get_int32(options, "reset-sync-time", &tmpi); + if (ret1 == 0) { + gf_asprintf_append(&events_str, "%sreset_sync_time=%d;", + events_str, tmpi); + } + /* Capture Master and Slave Info */ + ret1 = dict_get_str(options, "master", &tmp); + if (ret1) + tmp = ""; + gf_asprintf_append(&events_str, "%smaster=%s;", events_str, tmp); + + ret1 = dict_get_str(options, "slave", &tmp); + if (ret1) + tmp = ""; + gf_asprintf_append(&events_str, "%sslave=%s", events_str, tmp); + + gf_event(event_type, "%s", events_str); } - return ret; + /* Allocated by gf_strdup and gf_asprintf */ + if (events_str) + GF_FREE(events_str); + } +#endif + + CLI_STACK_DESTROY(frame); + + return ret; } +int +cli_cmd_volume_status_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) +{ + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + dict_t *dict = NULL; + uint32_t cmd = 0; + cli_local_t *local = NULL; + + ret = cli_cmd_volume_status_parse(words, wordcount, &dict); + + if (ret) { + cli_usage_out(word->pattern); + goto out; + } + + ret = dict_get_uint32(dict, "cmd", &cmd); + if (ret) + goto out; + + if (!(cmd & GF_CLI_STATUS_ALL)) { + /* for one volume or brick */ + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_STATUS_VOLUME]; + } else { + /* volume status all or all detail */ + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_STATUS_ALL]; + } + + if (!proc->fn) { + ret = -1; + goto out; + } + + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + gf_log(THIS->name, GF_LOG_ERROR, "failed to create frame"); + ret = -1; + goto out; + } + + CLI_LOCAL_INIT(local, words, frame, dict); + + ret = proc->fn(frame, THIS, dict); + +out: + CLI_STACK_DESTROY(frame); + + return ret; +} int -cli_cmd_volume_set_transport_cbk (struct cli_state *state, - struct cli_cmd_word *word, - const char **words, int wordcount) +cli_get_detail_status(dict_t *dict, int i, cli_volume_status_t *status) { - cli_cmd_broadcast_response (0); - return 0; + uint64_t free = 0; + uint64_t total = 0; + char key[1024] = {0}; + int ret = 0; + + snprintf(key, sizeof(key), "brick%d.free", i); + ret = dict_get_uint64(dict, key, &free); + + status->free = gf_uint64_2human_readable(free); + if (!status->free) + goto out; + + snprintf(key, sizeof(key), "brick%d.total", i); + ret = dict_get_uint64(dict, key, &total); + + status->total = gf_uint64_2human_readable(total); + if (!status->total) + goto out; + + snprintf(key, sizeof(key), "brick%d.device", i); + ret = dict_get_str(dict, key, &(status->device)); + if (ret) + status->device = NULL; + + snprintf(key, sizeof(key), "brick%d.block_size", i); + ret = dict_get_uint64(dict, key, &(status->block_size)); + if (ret) { + ret = 0; + status->block_size = 0; + } + + snprintf(key, sizeof(key), "brick%d.mnt_options", i); + ret = dict_get_str(dict, key, &(status->mount_options)); + if (ret) + status->mount_options = NULL; + + snprintf(key, sizeof(key), "brick%d.fs_name", i); + ret = dict_get_str(dict, key, &(status->fs_name)); + if (ret) { + ret = 0; + status->fs_name = NULL; + } + + snprintf(key, sizeof(key), "brick%d.inode_size", i); + ret = dict_get_str(dict, key, &(status->inode_size)); + if (ret) + status->inode_size = NULL; + + snprintf(key, sizeof(key), "brick%d.total_inodes", i); + ret = dict_get_uint64(dict, key, &(status->total_inodes)); + if (ret) + status->total_inodes = 0; + + snprintf(key, sizeof(key), "brick%d.free_inodes", i); + ret = dict_get_uint64(dict, key, &(status->free_inodes)); + if (ret) { + ret = 0; + status->free_inodes = 0; + } + +out: + return ret; } void -cli_cmd_log_filename_usage () +cli_print_detailed_status(cli_volume_status_t *status) { - cli_out ("Usage: volume log filename <VOLNAME> [BRICK] <PATH>"); + cli_out("%-20s : %-20s", "Brick", status->brick); + + if (status->online) { + cli_out("%-20s : %-20d", "TCP Port", status->port); + cli_out("%-20s : %-20d", "RDMA Port", status->rdma_port); + } else { + cli_out("%-20s : %-20s", "TCP Port", "N/A"); + cli_out("%-20s : %-20s", "RDMA Port", "N/A"); + } + + cli_out("%-20s : %-20c", "Online", (status->online) ? 'Y' : 'N'); + cli_out("%-20s : %-20s", "Pid", status->pid_str); + + if (status->fs_name) + cli_out("%-20s : %-20s", "File System", status->fs_name); + else + cli_out("%-20s : %-20s", "File System", "N/A"); + + if (status->device) + cli_out("%-20s : %-20s", "Device", status->device); + else + cli_out("%-20s : %-20s", "Device", "N/A"); + + if (status->mount_options) { + cli_out("%-20s : %-20s", "Mount Options", status->mount_options); + } else { + cli_out("%-20s : %-20s", "Mount Options", "N/A"); + } + + if (status->inode_size) { + cli_out("%-20s : %-20s", "Inode Size", status->inode_size); + } else { + cli_out("%-20s : %-20s", "Inode Size", "N/A"); + } + if (status->free) + cli_out("%-20s : %-20s", "Disk Space Free", status->free); + else + cli_out("%-20s : %-20s", "Disk Space Free", "N/A"); + + if (status->total) + cli_out("%-20s : %-20s", "Total Disk Space", status->total); + else + cli_out("%-20s : %-20s", "Total Disk Space", "N/A"); + + if (status->total_inodes) { + cli_out("%-20s : %-20" GF_PRI_INODE, "Inode Count", + status->total_inodes); + } else { + cli_out("%-20s : %-20s", "Inode Count", "N/A"); + } + + if (status->free_inodes) { + cli_out("%-20s : %-20" GF_PRI_INODE, "Free Inodes", + status->free_inodes); + } else { + cli_out("%-20s : %-20s", "Free Inodes", "N/A"); + } } int -cli_cmd_log_filename_cbk (struct cli_state *state, struct cli_cmd_word *word, - const char **words, int wordcount) +cli_print_brick_status(cli_volume_status_t *status) { - int ret = -1; - rpc_clnt_procedure_t *proc = NULL; - call_frame_t *frame = NULL; - dict_t *options = NULL; - - if (!((wordcount == 5) || (wordcount == 6))) { - cli_cmd_log_filename_usage (); - goto out; + int fieldlen = CLI_VOL_STATUS_BRICK_LEN; + int bricklen = 0; + char *p = NULL; + int num_spaces = 0; + + p = status->brick; + bricklen = strlen(p); + while (bricklen > 0) { + if (bricklen > fieldlen) { + cli_out("%.*s", fieldlen, p); + p += fieldlen; + bricklen -= fieldlen; + } else { + num_spaces = (fieldlen - bricklen) + 1; + printf("%s", p); + while (num_spaces-- != 0) + printf(" "); + if (status->port || status->rdma_port) { + if (status->online) + cli_out("%-10d%-11d%-8c%-5s", status->port, + status->rdma_port, status->online ? 'Y' : 'N', + status->pid_str); + else + cli_out("%-10s%-11s%-8c%-5s", "N/A", "N/A", + status->online ? 'Y' : 'N', status->pid_str); + } else + cli_out("%-10s%-11s%-8c%-5s", "N/A", "N/A", + status->online ? 'Y' : 'N', status->pid_str); + bricklen = 0; } + } - proc = &cli_rpc_prog->proctable[GF1_CLI_LOG_FILENAME]; + return 0; +} - frame = create_frame (THIS, THIS->ctx->pool); - if (!frame) - goto out; +#define NEEDS_GLFS_HEAL(op) \ + ((op == GF_SHD_OP_SBRAIN_HEAL_FROM_BIGGER_FILE) || \ + (op == GF_SHD_OP_SBRAIN_HEAL_FROM_LATEST_MTIME) || \ + (op == GF_SHD_OP_SBRAIN_HEAL_FROM_BRICK) || \ + (op == GF_SHD_OP_INDEX_SUMMARY) || (op == GF_SHD_OP_SPLIT_BRAIN_FILES) || \ + (op == GF_SHD_OP_GRANULAR_ENTRY_HEAL_ENABLE) || \ + (op == GF_SHD_OP_HEAL_SUMMARY)) - ret = cli_cmd_log_filename_parse (words, wordcount, &options); - if (ret) - goto out; +int +cli_launch_glfs_heal(int heal_op, dict_t *options) +{ + char buff[PATH_MAX] = {0}; + runner_t runner = {0}; + char *filename = NULL; + char *hostname = NULL; + char *path = NULL; + char *volname = NULL; + char *out = NULL; + int ret = 0; + + runinit(&runner); + ret = dict_get_str(options, "volname", &volname); + runner_add_args(&runner, GLFSHEAL_PREFIX "/glfsheal", volname, NULL); + runner_redir(&runner, STDOUT_FILENO, RUN_PIPE); + + switch (heal_op) { + case GF_SHD_OP_INDEX_SUMMARY: + if (global_state->mode & GLUSTER_MODE_XML) { + runner_add_args(&runner, "--xml", NULL); + } + break; + case GF_SHD_OP_SBRAIN_HEAL_FROM_BIGGER_FILE: + ret = dict_get_str(options, "file", &filename); + runner_add_args(&runner, "bigger-file", filename, NULL); + break; + case GF_SHD_OP_SBRAIN_HEAL_FROM_LATEST_MTIME: + ret = dict_get_str(options, "file", &filename); + runner_add_args(&runner, "latest-mtime", filename, NULL); + break; + case GF_SHD_OP_SBRAIN_HEAL_FROM_BRICK: + ret = dict_get_str(options, "heal-source-hostname", &hostname); + ret = dict_get_str(options, "heal-source-brickpath", &path); + runner_add_args(&runner, "source-brick", NULL); + runner_argprintf(&runner, "%s:%s", hostname, path); + if (dict_get_str(options, "file", &filename) == 0) + runner_argprintf(&runner, "%s", filename); + break; + case GF_SHD_OP_SPLIT_BRAIN_FILES: + runner_add_args(&runner, "split-brain-info", NULL); + if (global_state->mode & GLUSTER_MODE_XML) { + runner_add_args(&runner, "--xml", NULL); + } + break; + case GF_SHD_OP_GRANULAR_ENTRY_HEAL_ENABLE: + case GF_SHD_OP_GRANULAR_ENTRY_HEAL_DISABLE: + runner_add_args(&runner, "granular-entry-heal-op", NULL); + break; + case GF_SHD_OP_HEAL_SUMMARY: + runner_add_args(&runner, "info-summary", NULL); + if (global_state->mode & GLUSTER_MODE_XML) { + runner_add_args(&runner, "--xml", NULL); + } + break; + default: + ret = -1; + goto out; + } + if (global_state->mode & GLUSTER_MODE_GLFSHEAL_NOLOG) + runner_add_args(&runner, "--nolog", NULL); + ret = runner_start(&runner); + if (ret == -1) + goto out; + while (( + out = fgets(buff, sizeof(buff), runner_chio(&runner, STDOUT_FILENO)))) { + printf("%s", out); + } + ret = runner_end(&runner); - if (proc->fn) { - ret = proc->fn (frame, THIS, options); - } +out: + return ret; +} +int +cli_cmd_volume_heal_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) +{ + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + int sent = 0; + int parse_error = 0; + dict_t *options = NULL; + xlator_t *this = NULL; + cli_local_t *local = NULL; + int heal_op = 0; + + this = THIS; + + if (wordcount < 3) { + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } + + ret = cli_cmd_volume_heal_options_parse(words, wordcount, &options); + if (ret) { + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } + ret = dict_get_int32(options, "heal-op", &heal_op); + if (ret < 0) + goto out; + if (NEEDS_GLFS_HEAL(heal_op)) { + ret = cli_launch_glfs_heal(heal_op, options); + if (ret < 0) + goto out; + if (heal_op != GF_SHD_OP_GRANULAR_ENTRY_HEAL_ENABLE) + goto out; + } + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_HEAL_VOLUME]; + + frame = create_frame(this, this->ctx->pool); + if (!frame) { + ret = -1; + goto out; + } + + CLI_LOCAL_INIT(local, words, frame, options); + + if (proc->fn) { + ret = proc->fn(frame, THIS, options); + } out: - if (ret) - cli_out ("setting log filename failed"); + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0) && + !(global_state->mode & GLUSTER_MODE_XML)) { + cli_out("Volume heal failed."); + } + } - if (options) - dict_destroy (options); + if (options) + dict_unref(options); - return ret; -} + CLI_STACK_DESTROY(frame); + return ret; +} -void -cli_cmd_log_locate_usage () +int +cli_cmd_volume_statedump_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) { - cli_out ("Usage: volume log locate <VOLNAME> [BRICK]"); + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + dict_t *options = NULL; + int sent = 0; + int parse_error = 0; + cli_local_t *local = NULL; + + if (wordcount < 3) { + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } + + if (wordcount >= 3) { + ret = cli_cmd_volume_statedump_options_parse(words, wordcount, + &options); + if (ret) { + parse_error = 1; + gf_log("cli", GF_LOG_ERROR, + "Error parsing " + "statedump options"); + cli_out("Error parsing options"); + cli_usage_out(word->pattern); + } + } + + ret = dict_set_str(options, "volname", (char *)words[2]); + if (ret) + goto out; + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_STATEDUMP_VOLUME]; + + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + ret = -1; + goto out; + } + + CLI_LOCAL_INIT(local, words, frame, options); + + if (proc->fn) { + ret = proc->fn(frame, THIS, options); + } + +out: + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_out("Volume statedump failed"); + } + + CLI_STACK_DESTROY(frame); + + return ret; } int -cli_cmd_log_locate_cbk (struct cli_state *state, struct cli_cmd_word *word, +cli_cmd_volume_list_cbk(struct cli_state *state, struct cli_cmd_word *word, const char **words, int wordcount) { - int ret = -1; - rpc_clnt_procedure_t *proc = NULL; - call_frame_t *frame = NULL; - dict_t *options = NULL; - - if (!((wordcount == 4) || (wordcount == 5))) { - cli_cmd_log_locate_usage (); - goto out; - } + int ret = -1; + call_frame_t *frame = NULL; + rpc_clnt_procedure_t *proc = NULL; + int sent = 0; + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_LIST_VOLUME]; + if (proc->fn) { + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) + goto out; + ret = proc->fn(frame, THIS, NULL); + } - proc = &cli_rpc_prog->proctable[GF1_CLI_LOG_LOCATE]; +out: + if (ret) { + cli_cmd_sent_status_get(&sent); + if (sent == 0) + cli_out("Volume list failed"); + } - frame = create_frame (THIS, THIS->ctx->pool); - if (!frame) - goto out; + CLI_STACK_DESTROY(frame); - ret = cli_cmd_log_locate_parse (words, wordcount, &options); - if (ret) - goto out; + return ret; +} - if (proc->fn) { - ret = proc->fn (frame, THIS, options); - } +int +cli_cmd_volume_clearlocks_cbk(struct cli_state *state, + struct cli_cmd_word *word, const char **words, + int wordcount) +{ + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + dict_t *options = NULL; + int sent = 0; + int parse_error = 0; + cli_local_t *local = NULL; + + if (wordcount < 7 || wordcount > 8) { + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } + + ret = cli_cmd_volume_clrlks_opts_parse(words, wordcount, &options); + if (ret) { + parse_error = 1; + gf_log("cli", GF_LOG_ERROR, + "Error parsing " + "clear-locks options"); + cli_out("Error parsing options"); + cli_usage_out(word->pattern); + } + + ret = dict_set_str(options, "volname", (char *)words[2]); + if (ret) + goto out; + + ret = dict_set_str(options, "path", (char *)words[3]); + if (ret) + goto out; + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_CLRLOCKS_VOLUME]; + + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + ret = -1; + goto out; + } + + CLI_LOCAL_INIT(local, words, frame, options); + + if (proc->fn) { + ret = proc->fn(frame, THIS, options); + } out: - if (ret) - cli_out ("getting log file location information failed"); - - if (options) - dict_destroy (options); + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_out("Volume clear-locks failed"); + } + CLI_STACK_DESTROY(frame); - return ret; + return ret; } -void -cli_cmd_log_rotate_usage () +int +cli_cmd_volume_barrier_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) { - cli_out ("Usage: volume log rotate <VOLNAME> [BRICK]"); + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + dict_t *options = NULL; + int sent = 0; + int parse_error = 0; + cli_local_t *local = NULL; + + if (wordcount != 4) { + cli_usage_out(word->pattern); + parse_error = 1; + goto out; + } + + options = dict_new(); + if (!options) { + ret = -1; + goto out; + } + ret = dict_set_str(options, "volname", (char *)words[2]); + if (ret) + goto out; + + ret = dict_set_str(options, "barrier", (char *)words[3]); + if (ret) + goto out; + + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_BARRIER_VOLUME]; + + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + ret = -1; + goto out; + } + + CLI_LOCAL_INIT(local, words, frame, options); + + if (proc->fn) + ret = proc->fn(frame, THIS, options); + +out: + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_error == 0)) + cli_err("Volume barrier failed"); + } + CLI_STACK_DESTROY(frame); + + return ret; } int -cli_cmd_log_rotate_cbk (struct cli_state *state, struct cli_cmd_word *word, - const char **words, int wordcount) +cli_cmd_volume_getopt_cbk(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount) { - int ret = -1; - rpc_clnt_procedure_t *proc = NULL; - call_frame_t *frame = NULL; - dict_t *options = NULL; + int ret = -1; + rpc_clnt_procedure_t *proc = NULL; + call_frame_t *frame = NULL; + dict_t *options = NULL; + int sent = 0; + int parse_err = 0; + cli_local_t *local = NULL; - if (!((wordcount == 4) || (wordcount == 5))) { - cli_cmd_log_rotate_usage (); - goto out; - } + if (wordcount != 4) { + cli_usage_out(word->pattern); + parse_err = 1; + goto out; + } - proc = &cli_rpc_prog->proctable[GF1_CLI_LOG_ROTATE]; + options = dict_new(); + if (!options) + goto out; - frame = create_frame (THIS, THIS->ctx->pool); - if (!frame) - goto out; + ret = dict_set_str(options, "volname", (char *)words[2]); + if (ret) + goto out; - ret = cli_cmd_log_rotate_parse (words, wordcount, &options); - if (ret) - goto out; + ret = dict_set_str(options, "key", (char *)words[3]); + if (ret) + goto out; - if (proc->fn) { - ret = proc->fn (frame, THIS, options); - } + proc = &cli_rpc_prog->proctable[GLUSTER_CLI_GET_VOL_OPT]; -out: - if (ret) - cli_out ("getting log file location information failed"); + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + ret = -1; + goto out; + } + + CLI_LOCAL_INIT(local, words, frame, options); - if (options) - dict_destroy (options); + if (proc->fn) + ret = proc->fn(frame, THIS, options); - return ret; +out: + if (ret) { + cli_cmd_sent_status_get(&sent); + if ((sent == 0) && (parse_err == 0)) + cli_err("Volume get option failed"); + } + CLI_STACK_DESTROY(frame); + return ret; } +/* This is a bit of a hack to display the help. The current bitrot cmd + * format does not work well when registering the cmds. + * Ideally the should have been of the form + * gluster volume bitrot <subcommand> <volumename> ... + */ + +struct cli_cmd bitrot_cmds[] = { + + {"volume bitrot help", cli_cmd_bitrot_help_cbk, + "display help for volume bitrot commands"}, + + {"volume bitrot <VOLNAME> {enable|disable}", NULL, /*cli_cmd_bitrot_cbk,*/ + "Enable/disable bitrot for volume <VOLNAME>"}, + + {"volume bitrot <VOLNAME> signing-time <time-in-secs>", + NULL, /*cli_cmd_bitrot_cbk,*/ + "Waiting time for an object after last fd is closed to start signing " + "process"}, + + {"volume bitrot <VOLNAME> signer-threads <count>", + NULL, /*cli_cmd_bitrot_cbk,*/ + "Number of signing process threads. Usually set to number of available " + "cores"}, + + {"volume bitrot <VOLNAME> scrub-throttle {lazy|normal|aggressive}", + NULL, /*cli_cmd_bitrot_cbk,*/ + "Set the speed of the scrubber for volume <VOLNAME>"}, + + {"volume bitrot <VOLNAME> scrub-frequency {hourly|daily|weekly|biweekly" + "|monthly}", + NULL, /*cli_cmd_bitrot_cbk,*/ + "Set the frequency of the scrubber for volume <VOLNAME>"}, + + {"volume bitrot <VOLNAME> scrub {pause|resume|status|ondemand}", + NULL, /*cli_cmd_bitrot_cbk,*/ + "Pause/resume the scrubber for <VOLNAME>. Status displays the status of " + "the scrubber. ondemand starts the scrubber immediately."}, + + {"volume bitrot <VOLNAME> {enable|disable}\n" + "volume bitrot <VOLNAME> signing-time <time-in-secs>\n" + "volume bitrot <VOLNAME> signer-threads <count>\n" + "volume bitrot <volname> scrub-throttle {lazy|normal|aggressive}\n" + "volume bitrot <volname> scrub-frequency {hourly|daily|weekly|biweekly" + "|monthly}\n" + "volume bitrot <volname> scrub {pause|resume|status|ondemand}", + cli_cmd_bitrot_cbk, NULL}, + + {NULL, NULL, NULL}}; + +struct cli_cmd quota_cmds[] = { + + /* Quota commands */ + {"volume quota help", cli_cmd_quota_help_cbk, + "display help for volume quota commands"}, + + {"volume quota <VOLNAME> {enable|disable|list [<path> ...]| " + "list-objects [<path> ...] | remove <path>| remove-objects <path> | " + "default-soft-limit <percent>}", + cli_cmd_quota_cbk, "Enable/disable and configure quota for <VOLNAME>"}, + + {"volume quota <VOLNAME> {limit-usage <path> <size> [<percent>]}", + cli_cmd_quota_cbk, "Set maximum size for <path> for <VOLNAME>"}, + + {"volume quota <VOLNAME> {limit-objects <path> <number> [<percent>]}", + cli_cmd_quota_cbk, + "Set the maximum number of entries allowed in <path> for <VOLNAME>"}, + + {"volume quota <VOLNAME> {alert-time|soft-timeout|hard-timeout} {<time>}", + cli_cmd_quota_cbk, "Set quota timeout for <VOLNAME>"}, + + {"volume inode-quota <VOLNAME> enable", cli_cmd_quota_cbk, + "Enable/disable inode-quota for <VOLNAME>"}, + + {"volume quota <VOLNAME> {enable|disable|list [<path> ...]| " + "list-objects [<path> ...] | remove <path>| remove-objects <path> | " + "default-soft-limit <percent>}\n" + "volume quota <VOLNAME> {limit-usage <path> <size> [<percent>]}\n" + "volume quota <VOLNAME> {limit-objects <path> <number> [<percent>]}\n" + "volume quota <VOLNAME> {alert-time|soft-timeout|hard-timeout} {<time>}", + cli_cmd_quota_cbk, NULL}, + + {NULL, NULL, NULL}}; struct cli_cmd volume_cmds[] = { - { "volume info [all|<VOLNAME>]", - cli_cmd_volume_info_cbk, - "list information of all volumes"}, + {"volume help", cli_cmd_volume_help_cbk, + "display help for volume commands"}, - { "volume create <NEW-VOLNAME> [stripe <COUNT>] [replica <COUNT>] <NEW-BRICK> ...", - cli_cmd_volume_create_cbk, - "create a new volume of specified type with mentioned bricks"}, + {"volume info [all|<VOLNAME>]", cli_cmd_volume_info_cbk, + "list information of all volumes"}, - { "volume delete <VOLNAME>", - cli_cmd_volume_delete_cbk, - "delete volume specified by <VOLNAME>"}, + {"volume create <NEW-VOLNAME> [stripe <COUNT>] " + "[[replica <COUNT> [arbiter <COUNT>]]|[replica 2 thin-arbiter 1]] " + "[disperse [<COUNT>]] [disperse-data <COUNT>] [redundancy <COUNT>] " + "[transport <tcp|rdma|tcp,rdma>] <NEW-BRICK> <TA-BRICK>" + "... [force]", - { "volume start <VOLNAME>", - cli_cmd_volume_start_cbk, - "start volume specified by <VOLNAME>"}, + cli_cmd_volume_create_cbk, + "create a new volume of specified type with mentioned bricks"}, - { "volume stop <VOLNAME> [force]", - cli_cmd_volume_stop_cbk, - "stop volume specified by <VOLNAME>"}, + {"volume delete <VOLNAME>", cli_cmd_volume_delete_cbk, + "delete volume specified by <VOLNAME>"}, - { "volume rename <VOLNAME> <NEW-VOLNAME>", - cli_cmd_volume_rename_cbk, - "rename volume <VOLNAME> to <NEW-VOLNAME>"}, + {"volume start <VOLNAME> [force]", cli_cmd_volume_start_cbk, + "start volume specified by <VOLNAME>"}, - { "volume add-brick <VOLNAME> [(replica <COUNT>)|(stripe <COUNT>)] <NEW-BRICK> ...", - cli_cmd_volume_add_brick_cbk, - "add brick to volume <VOLNAME>"}, + {"volume stop <VOLNAME> [force]", cli_cmd_volume_stop_cbk, + "stop volume specified by <VOLNAME>"}, - { "volume remove-brick <VOLNAME> [(replica <COUNT>)|(stripe <COUNT>)] <BRICK> ...", - cli_cmd_volume_remove_brick_cbk, - "remove brick from volume <VOLNAME>"}, + /*{ "volume rename <VOLNAME> <NEW-VOLNAME>", + cli_cmd_volume_rename_cbk, + "rename volume <VOLNAME> to <NEW-VOLNAME>"},*/ - { "volume rebalance <VOLNAME> start", - cli_cmd_volume_defrag_cbk, - "start rebalance of volume <VOLNAME>"}, + {"volume add-brick <VOLNAME> [<stripe|replica> <COUNT> " + "[arbiter <COUNT>]] <NEW-BRICK> ... [force]", + cli_cmd_volume_add_brick_cbk, "add brick to volume <VOLNAME>"}, - { "volume rebalance <VOLNAME> stop", - cli_cmd_volume_defrag_cbk, - "stop rebalance of volume <VOLNAME>"}, + {"volume remove-brick <VOLNAME> [replica <COUNT>] <BRICK> ..." + " <start|stop|status|commit|force>", + cli_cmd_volume_remove_brick_cbk, "remove brick from volume <VOLNAME>"}, - { "volume rebalance <VOLNAME> status", - cli_cmd_volume_defrag_cbk, - "rebalance status of volume <VOLNAME>"}, + {"volume rebalance <VOLNAME> {{fix-layout start} | {start " + "[force]|stop|status}}", + cli_cmd_volume_defrag_cbk, "rebalance operations"}, - { "volume replace-brick <VOLNAME> (<BRICK> <NEW-BRICK>) start|pause|abort|status", - cli_cmd_volume_replace_brick_cbk, - "replace-brick operations"}, + {"volume replace-brick <VOLNAME> <SOURCE-BRICK> <NEW-BRICK> " + "{commit force}", + cli_cmd_volume_replace_brick_cbk, "replace-brick operations"}, - { "volume set-transport <VOLNAME> <TRANSPORT-TYPE> [<TRANSPORT-TYPE>] ...", - cli_cmd_volume_set_transport_cbk, - "set transport type for volume <VOLNAME>"}, + /*{ "volume set-transport <VOLNAME> <TRANSPORT-TYPE> [<TRANSPORT-TYPE>] + ...", cli_cmd_volume_set_transport_cbk, "set transport type for volume + <VOLNAME>"},*/ - { "volume set <VOLNAME> <KEY> <VALUE>", - cli_cmd_volume_set_cbk, - "set options for volume <VOLNAME>"}, + {"volume set <VOLNAME> <KEY> <VALUE>", cli_cmd_volume_set_cbk, + "set options for volume <VOLNAME>"}, - { "volume --help", - cli_cmd_volume_help_cbk, - "display help for the volume command"}, + {"volume set <VOLNAME> group <GROUP>", cli_cmd_volume_set_cbk, + "This option can be used for setting multiple pre-defined volume options " + "where group_name is a file under /var/lib/glusterd/groups containing one " + "key value pair per line"}, - { "volume log filename <VOLNAME> [BRICK] <PATH>", - cli_cmd_log_filename_cbk, - "set the log file for corresponding volume/brick"}, + {"volume log <VOLNAME> rotate [BRICK]", cli_cmd_log_rotate_cbk, + "rotate the log file for corresponding volume/brick"}, - { "volume log locate <VOLNAME> [BRICK]", - cli_cmd_log_locate_cbk, - "locate the log file for corresponding volume/brick"}, + {"volume sync <HOSTNAME> [all|<VOLNAME>]", cli_cmd_sync_volume_cbk, + "sync the volume information from a peer"}, - { "volume log rotate <VOLNAME> [BRICK]", - cli_cmd_log_rotate_cbk, - "rotate the log file for corresponding volume/brick"}, + {"volume reset <VOLNAME> [option] [force]", cli_cmd_volume_reset_cbk, + "reset all the reconfigured options"}, - { NULL, NULL, NULL } -}; +#if (SYNCDAEMON_COMPILE) + {"volume " GEOREP " [<MASTER-VOLNAME>] [<SLAVE-IP>]::[<SLAVE-VOLNAME>] {" + "\\\n create [[ssh-port n] [[no-verify] \\\n | [push-pem]]] [force] \\\n" + " | start [force] \\\n | stop [force] \\\n | pause [force] \\\n | resume " + "[force] \\\n" + " | config [[[\\!]<option>] [<value>]] \\\n | status " + "[detail] \\\n | delete [reset-sync-time]} ", + cli_cmd_volume_gsync_set_cbk, "Geo-sync operations", + cli_cmd_check_gsync_exists_cbk}, +#endif + + {"volume profile <VOLNAME> {start|info [peek|incremental " + "[peek]|cumulative|clear]|stop} [nfs]", + cli_cmd_volume_profile_cbk, "volume profile operations"}, + + {"volume top <VOLNAME> {open|read|write|opendir|readdir|clear} [nfs|brick " + "<brick>] [list-cnt <value>] | " + "{read-perf|write-perf} [bs <size> count <count>] " + "[brick <brick>] [list-cnt <value>]", + cli_cmd_volume_top_cbk, "volume top operations"}, + + {"volume status [all | <VOLNAME> [nfs|shd|<BRICK>|quotad]]" + " [detail|clients|mem|inode|fd|callpool|tasks|client-list]", + cli_cmd_volume_status_cbk, + "display status of all or specified volume(s)/brick"}, + + {"volume heal <VOLNAME> [enable | disable | full |" + "statistics [heal-count [replica <HOSTNAME:BRICKNAME>]] |" + "info [summary | split-brain] |" + "split-brain {bigger-file <FILE> | latest-mtime <FILE> |" + "source-brick <HOSTNAME:BRICKNAME> [<FILE>]} |" + "granular-entry-heal {enable | disable}]", + cli_cmd_volume_heal_cbk, + "self-heal commands on volume specified by <VOLNAME>"}, + + {"volume statedump <VOLNAME> [[nfs|quotad] [all|mem|iobuf|callpool|" + "priv|fd|inode|history]... | [client <hostname:process-id>]]", + cli_cmd_volume_statedump_cbk, "perform statedump on bricks"}, + + {"volume list", cli_cmd_volume_list_cbk, "list all volumes in cluster"}, + + {"volume clear-locks <VOLNAME> <path> kind {blocked|granted|all}" + "{inode [range]|entry [basename]|posix [range]}", + cli_cmd_volume_clearlocks_cbk, "Clear locks held on path"}, + {"volume barrier <VOLNAME> {enable|disable}", cli_cmd_volume_barrier_cbk, + "Barrier/unbarrier file operations on a volume"}, + {"volume get <VOLNAME|all> <key|all>", cli_cmd_volume_getopt_cbk, + "Get the value of the all options or given option for volume <VOLNAME>" + " or all option. gluster volume get all all is to get all global " + "options"}, + + {"volume reset-brick <VOLNAME> <SOURCE-BRICK> {{start} |" + " {<NEW-BRICK> commit}}", + cli_cmd_volume_reset_brick_cbk, "reset-brick operations"}, + + {NULL, NULL, NULL}}; int -cli_cmd_volume_help_cbk (struct cli_state *state, struct cli_cmd_word *in_word, - const char **words, int wordcount) +cli_cmd_quota_help_cbk(struct cli_state *state, struct cli_cmd_word *in_word, + const char **words, int wordcount) { - struct cli_cmd *cmd = NULL; + struct cli_cmd *cmd = NULL; + struct cli_cmd *quota_cmd = NULL; + int count = 0; - for (cmd = volume_cmds; cmd->pattern; cmd++) - cli_out ("%s - %s", cmd->pattern, cmd->desc); + cmd = GF_MALLOC(sizeof(quota_cmds), cli_mt_cli_cmd); + memcpy(cmd, quota_cmds, sizeof(quota_cmds)); + count = (sizeof(quota_cmds) / sizeof(struct cli_cmd)); + cli_cmd_sort(cmd, count); + cli_out("\ngluster quota commands"); + cli_out("=======================\n"); - if (!state->rl_enabled) - exit (0); + for (quota_cmd = cmd; quota_cmd->pattern; quota_cmd++) + if ((_gf_false == quota_cmd->disable) && (quota_cmd->desc)) + cli_out("%s - %s", quota_cmd->pattern, quota_cmd->desc); - return 0; + cli_out("\n"); + GF_FREE(cmd); + + return 0; } int -cli_cmd_volume_register (struct cli_state *state) +cli_cmd_bitrot_help_cbk(struct cli_state *state, struct cli_cmd_word *in_word, + const char **words, int wordcount) { - int ret = 0; - struct cli_cmd *cmd = NULL; + struct cli_cmd *cmd = NULL; + struct cli_cmd *bitrot_cmd = NULL; + int count = 0; + + cmd = GF_MALLOC(sizeof(bitrot_cmds), cli_mt_cli_cmd); + memcpy(cmd, bitrot_cmds, sizeof(bitrot_cmds)); + count = (sizeof(bitrot_cmds) / sizeof(struct cli_cmd)); + cli_cmd_sort(cmd, count); + + cli_out("\ngluster bitrot commands"); + cli_out("========================\n"); + + for (bitrot_cmd = cmd; bitrot_cmd->pattern; bitrot_cmd++) + if ((_gf_false == bitrot_cmd->disable) && (bitrot_cmd->desc)) + cli_out("%s - %s", bitrot_cmd->pattern, bitrot_cmd->desc); + + cli_out("\n"); + GF_FREE(cmd); + + return 0; +} + +int +cli_cmd_volume_help_cbk(struct cli_state *state, struct cli_cmd_word *in_word, + const char **words, int wordcount) +{ + struct cli_cmd *cmd = NULL; + struct cli_cmd *vol_cmd = NULL; + int count = 0; + + cmd = GF_MALLOC(sizeof(volume_cmds), cli_mt_cli_cmd); + memcpy(cmd, volume_cmds, sizeof(volume_cmds)); + count = (sizeof(volume_cmds) / sizeof(struct cli_cmd)); + cli_cmd_sort(cmd, count); + + cli_out("\ngluster volume commands"); + cli_out("========================\n"); + + for (vol_cmd = cmd; vol_cmd->pattern; vol_cmd++) + if (_gf_false == vol_cmd->disable) + cli_out("%s - %s", vol_cmd->pattern, vol_cmd->desc); + + cli_out("\n"); + GF_FREE(cmd); + return 0; +} + +int +cli_cmd_volume_register(struct cli_state *state) +{ + int ret = 0; + struct cli_cmd *cmd = NULL; + + for (cmd = volume_cmds; cmd->pattern; cmd++) { + ret = cli_cmd_register(&state->tree, cmd); + if (ret) + goto out; + } + + for (cmd = bitrot_cmds; cmd->pattern; cmd++) { + ret = cli_cmd_register(&state->tree, cmd); + if (ret) + goto out; + } + + for (cmd = quota_cmds; cmd->pattern; cmd++) { + ret = cli_cmd_register(&state->tree, cmd); + if (ret) + goto out; + } - for (cmd = volume_cmds; cmd->pattern; cmd++) { - ret = cli_cmd_register (&state->tree, cmd->pattern, cmd->cbk, - cmd->desc); - if (ret) - goto out; - } out: - return ret; + return ret; +} + +static int +gf_asprintf_append(char **string_ptr, const char *format, ...) +{ + va_list arg; + int rv = 0; + char *tmp = *string_ptr; + + va_start(arg, format); + rv = gf_vasprintf(string_ptr, format, arg); + va_end(arg); + + if (tmp) + GF_FREE(tmp); + + return rv; } diff --git a/cli/src/cli-cmd.c b/cli/src/cli-cmd.c index 934936af92d..2d458b16a56 100644 --- a/cli/src/cli-cmd.c +++ b/cli/src/cli-cmd.c @@ -1,339 +1,411 @@ /* - Copyright (c) 2010 Gluster, Inc. <http://www.gluster.com> - This file is part of GlusterFS. - - GlusterFS is free software; you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - GlusterFS is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see - <http://www.gnu.org/licenses/>. -*/ + Copyright (c) 2010-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + 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 <stdio.h> #include <string.h> #include <stdlib.h> #include <stdint.h> #include <pthread.h> -#ifndef _CONFIG_H -#define _CONFIG_H -#include "config.h" -#endif - #include "cli.h" #include "cli-cmd.h" #include "cli-mem-types.h" +#include "protocol-common.h" #include <fnmatch.h> static int cmd_done; static int cmd_sent; -static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; -static pthread_mutex_t cond_mutex = PTHREAD_MUTEX_INITIALIZER; -static pthread_cond_t conn = PTHREAD_COND_INITIALIZER; -static pthread_mutex_t conn_mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; +static pthread_mutex_t cond_mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_cond_t conn = PTHREAD_COND_INITIALIZER; +static pthread_mutex_t conn_mutex = PTHREAD_MUTEX_INITIALIZER; -int cli_op_ret = 0; -int connected = 0; +int cli_op_ret = 0; +static gf_boolean_t connected = _gf_false; -int cli_cmd_log_help_cbk (struct cli_state *state, struct cli_cmd_word *in_word, - const char **words, int wordcount); - -static gf_boolean_t -cli_cmd_needs_connection (struct cli_cmd_word *word) +static unsigned +cli_cmd_needs_connection(struct cli_cmd_word *word) { - if (!strcasecmp ("quit", word->word)) - return _gf_false; + if (!strcasecmp("quit", word->word)) + return 0; - if (!strcasecmp ("help", word->word)) - return _gf_false; + if (!strcasecmp("help", word->word)) + return 0; + + if (!strcasecmp("getwd", word->word)) + return 1; + + if (!strcasecmp("exit", word->word)) + return 0; - return _gf_true; + return cli_default_conn_timeout; } int -cli_cmd_process (struct cli_state *state, int argc, char **argv) +cli_cmd_status_reset(void) { - int ret = 0; - struct cli_cmd_word *word = NULL; - struct cli_cmd_word *next = NULL; - int i = 0; - gf_boolean_t await_conn = _gf_false; - - word = &state->tree.root; - - if (!argc) - return 0; - - for (i = 0; i < argc; i++) { - next = cli_cmd_nextword (word, argv[i]); - - word = next; - if (!word) - break; - - if (word->cbkfn) - break; - } - - if (!word) { - cli_out ("unrecognized word: %s (position %d)", - argv[i], i); - return -1; - } - - if (!word->cbkfn) { - cli_out ("unrecognized command"); - return -1; - } - - if ( strcmp (word->word,"--help")==0 ) - goto callback; - - await_conn = cli_cmd_needs_connection (word); + int ret = 0; - if (await_conn) { - ret = cli_cmd_await_connected (); - if (ret) { - cli_out ("Connection failed. Please check if gluster " - "daemon is operational."); - gf_log ("", GF_LOG_NORMAL, "Exiting with: %d", ret); - exit (ret); - } + ret = cli_cmd_lock(); + { + if (ret == 0) { + cmd_sent = 0; + cmd_done = 0; } - -callback: - ret = word->cbkfn (state, word, (const char **)argv, argc); - - return ret; + } + ret = cli_cmd_unlock(); + return ret; } - int -cli_cmd_input_token_count (const char *text) +cli_cmd_sent_status_get(int *status) { - int count = 0; - const char *trav = NULL; - int is_spc = 1; - - for (trav = text; *trav; trav++) { - if (*trav == ' ') { - is_spc = 1; - } else { - if (is_spc) { - count++; - is_spc = 0; - } - } - } - - return count; + int ret = 0; + GF_ASSERT(status); + + ret = cli_cmd_lock(); + { + if (ret == 0) + *status = cmd_sent; + } + ret = cli_cmd_unlock(); + return ret; } - int -cli_cmd_process_line (struct cli_state *state, const char *text) +cli_cmd_process(struct cli_state *state, int argc, char **argv) { - int count = 0; - char **tokens = NULL; - char **tokenp = NULL; - char *token = NULL; - char *copy = NULL; - char *saveptr = NULL; - int i = 0; - int ret = -1; + int ret = 0; + struct cli_cmd_word *word = NULL; + struct cli_cmd_word *next = NULL; + int i = 0; - count = cli_cmd_input_token_count (text); + word = &state->tree.root; - tokens = calloc (count + 1, sizeof (*tokens)); - if (!tokens) - return -1; + if (!argc) + return 0; - copy = strdup (text); - if (!copy) - goto out; + for (i = 0; i < argc; i++) { + next = cli_cmd_nextword(word, argv[i]); - tokenp = tokens; + word = next; + if (!word) + break; - for (token = strtok_r (copy, " \t\r\n", &saveptr); token; - token = strtok_r (NULL, " \t\r\n", &saveptr)) { - *tokenp = strdup (token); + if (word->cbkfn) + break; + } - if (!*tokenp) - goto out; - tokenp++; - i++; + if (!word) { + cli_out("unrecognized word: %s (position %d)\n", argv[i], i); + usage(); + return -1; + } - } + if (!word->cbkfn) { + cli_out("unrecognized command\n"); + usage(); + return -1; + } - ret = cli_cmd_process (state, count, tokens); -out: - if (copy) - free (copy); + if (strcmp(word->word, "help") == 0) + goto callback; - if (tokens) - cli_cmd_tokens_destroy (tokens); + state->await_connected = cli_cmd_needs_connection(word); - return ret; -} + ret = cli_cmd_await_connected(state->await_connected); + if (ret) { + cli_out( + "Connection failed. Please check if gluster " + "daemon is operational."); + gf_log("", GF_LOG_INFO, "Exiting with: %d", ret); + exit(ret); + } +callback: + ret = word->cbkfn(state, word, (const char **)argv, argc); + (void)cli_cmd_status_reset(); + return ret; +} int -cli_cmds_register (struct cli_state *state) +cli_cmd_input_token_count(const char *text) { - int ret = 0; - - ret = cli_cmd_volume_register (state); - if (ret) - goto out; - - ret = cli_cmd_probe_register (state); - if (ret) - goto out; - - ret = cli_cmd_misc_register (state); - if (ret) - goto out; + int count = 0; + const char *trav = NULL; + int is_spc = 1; + + for (trav = text; *trav; trav++) { + if (*trav == ' ') { + is_spc = 1; + } else { + if (is_spc) { + count++; + is_spc = 0; + } + } + } -out: - return ret; + return count; } int -cli_cmd_cond_init () +cli_cmd_process_line(struct cli_state *state, const char *text) { + int count = 0; + char **tokens = NULL; + char **tokenp = NULL; + char *token = NULL; + char *copy = NULL; + char *saveptr = NULL; + int i = 0; + int ret = -1; + + count = cli_cmd_input_token_count(text); + + tokens = calloc(count + 1, sizeof(*tokens)); + if (!tokens) + return -1; + + copy = strdup(text); + if (!copy) + goto out; + + tokenp = tokens; + + for (token = strtok_r(copy, " \t\r\n", &saveptr); token; + token = strtok_r(NULL, " \t\r\n", &saveptr)) { + *tokenp = strdup(token); + + if (!*tokenp) + goto out; + tokenp++; + i++; + } + + ret = cli_cmd_process(state, count, tokens); +out: + free(copy); - pthread_mutex_init (&cond_mutex, NULL); - pthread_cond_init (&cond, NULL); - - pthread_mutex_init (&conn_mutex, NULL); - pthread_cond_init (&conn, NULL); + if (tokens) + cli_cmd_tokens_destroy(tokens); - return 0; + return ret; } int -cli_cmd_lock () +cli_cmds_register(struct cli_state *state) { - pthread_mutex_lock (&cond_mutex); - return 0; + int ret = 0; + + ret = cli_cmd_volume_register(state); + if (ret) + goto out; + + ret = cli_cmd_probe_register(state); + if (ret) + goto out; + + ret = cli_cmd_system_register(state); + if (ret) + goto out; + + ret = cli_cmd_misc_register(state); + if (ret) + goto out; + + ret = cli_cmd_snapshot_register(state); + if (ret) + goto out; + ret = cli_cmd_global_register(state); + if (ret) + goto out; +out: + return ret; } int -cli_cmd_unlock () +cli_cmd_lock() { - pthread_mutex_unlock (&cond_mutex); - return 0; + pthread_mutex_lock(&cond_mutex); + return 0; } int -cli_cmd_await_response () +cli_cmd_unlock() { - struct timespec ts = {0,}; - int ret = 0; - - cli_op_ret = -1; - - time (&ts.tv_sec); - ts.tv_sec += CLI_DEFAULT_CMD_TIMEOUT; - while (!cmd_done && !ret) { - ret = pthread_cond_timedwait (&cond, &cond_mutex, - &ts); - } - - cmd_done = 0; + pthread_mutex_unlock(&cond_mutex); + return 0; +} - cli_cmd_unlock (); +static void +seconds_from_now(unsigned secs, struct timespec *ts) +{ + struct timeval tv = { + 0, + }; - if (ret) - return ret; + gettimeofday(&tv, NULL); - return cli_op_ret; + ts->tv_sec = tv.tv_sec + secs; + ts->tv_nsec = tv.tv_usec * 1000; } int -cli_cmd_broadcast_response (int32_t status) +cli_cmd_await_response(unsigned time) { + struct timespec ts = { + 0, + }; + int ret = 0; + + cli_op_ret = -1; + + seconds_from_now(time, &ts); + while (!cmd_done && !ret) { + ret = pthread_cond_timedwait(&cond, &cond_mutex, &ts); + } + + if (!cmd_done) { + if (ret == ETIMEDOUT) + cli_out("Error : Request timed out"); + else + cli_out("Error : Command returned with error code:%d", ret); + } + cmd_done = 0; + + return cli_op_ret; +} - pthread_mutex_lock (&cond_mutex); - { - if (!cmd_sent) - goto out; - cmd_done = 1; - cli_op_ret = status; - pthread_cond_broadcast (&cond); - } - +/* This function must be called _only_ after all actions associated with + * command processing is complete. Otherwise, gluster process may exit before + * reporting results to stdout/stderr. */ +int +cli_cmd_broadcast_response(int32_t status) +{ + pthread_mutex_lock(&cond_mutex); + { + if (!cmd_sent) + goto out; + cmd_done = 1; + cli_op_ret = status; + pthread_cond_broadcast(&cond); + } out: - pthread_mutex_unlock (&cond_mutex); - return 0; + pthread_mutex_unlock(&cond_mutex); + return 0; } int32_t -cli_cmd_await_connected () +cli_cmd_await_connected(unsigned conn_timo) { - int32_t ret = 0; - struct timespec ts = {0,}; - - pthread_mutex_lock (&conn_mutex); - { - time (&ts.tv_sec); - ts.tv_sec += CLI_DEFAULT_CONN_TIMEOUT; - while (!connected && !ret) { - ret = pthread_cond_timedwait (&conn, &conn_mutex, - &ts); - } - } - pthread_mutex_unlock (&conn_mutex); + int32_t ret = 0; + struct timespec ts = { + 0, + }; + if (!conn_timo) + return 0; + + pthread_mutex_lock(&conn_mutex); + { + seconds_from_now(conn_timo, &ts); + while (!connected && !ret) { + ret = pthread_cond_timedwait(&conn, &conn_mutex, &ts); + } + } + pthread_mutex_unlock(&conn_mutex); - return ret; + return ret; } int32_t -cli_cmd_broadcast_connected () +cli_cmd_broadcast_connected(gf_boolean_t status) { - pthread_mutex_lock (&conn_mutex); - { - connected = 1; - pthread_cond_broadcast (&conn); - } + pthread_mutex_lock(&conn_mutex); + { + connected = status; + pthread_cond_broadcast(&conn); + } + pthread_mutex_unlock(&conn_mutex); + + return 0; +} - pthread_mutex_unlock (&conn_mutex); +gf_boolean_t +cli_cmd_connected(void) +{ + gf_boolean_t status; - return 0; + pthread_mutex_lock(&conn_mutex); + { + status = connected; + } + pthread_mutex_unlock(&conn_mutex); + + return status; +} + +int +cli_cmd_submit(struct rpc_clnt *rpc, void *req, call_frame_t *frame, + rpc_clnt_prog_t *prog, int procnum, struct iobref *iobref, + xlator_t *this, fop_cbk_fn_t cbkfn, xdrproc_t xdrproc) +{ + int ret = -1; + unsigned timeout = 0; + + if ((GLUSTER_CLI_PROFILE_VOLUME == procnum) || + (GLUSTER_CLI_HEAL_VOLUME == procnum) || + (GLUSTER_CLI_GANESHA == procnum)) + timeout = cli_ten_minutes_timeout; + else + timeout = cli_default_conn_timeout; + + cli_cmd_lock(); + cmd_sent = 0; + ret = cli_submit_request(rpc, req, frame, prog, procnum, NULL, this, cbkfn, + xdrproc); + + if (!ret) { + cmd_sent = 1; + ret = cli_cmd_await_response(timeout); + } + + cli_cmd_unlock(); + + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; } int -cli_cmd_submit (void *req, call_frame_t *frame, - rpc_clnt_prog_t *prog, - int procnum, struct iobref *iobref, - cli_serialize_t sfunc, xlator_t *this, - fop_cbk_fn_t cbkfn) +cli_cmd_pattern_cmp(void *a, void *b) +{ + struct cli_cmd *ia = NULL; + struct cli_cmd *ib = NULL; + int ret = 0; + + ia = a; + ib = b; + if (strcmp(ia->pattern, ib->pattern) > 0) + ret = 1; + else if (strcmp(ia->pattern, ib->pattern) < 0) + ret = -1; + else + ret = 0; + return ret; +} + +void +cli_cmd_sort(struct cli_cmd *cmd, int count) { - int ret = -1; - - cli_cmd_lock (); - cmd_sent = 0; - ret = cli_submit_request (req, frame, prog, - procnum, NULL, sfunc, - this, cbkfn); - - if (!ret) { - cmd_sent = 1; - ret = cli_cmd_await_response (); - } else - cli_cmd_unlock (); - - gf_log ("cli", GF_LOG_DEBUG, "Returning %d", ret); - return ret; + gf_array_insertionsort(cmd, 1, count - 2, sizeof(struct cli_cmd), + cli_cmd_pattern_cmp); } diff --git a/cli/src/cli-cmd.h b/cli/src/cli-cmd.h index b7cd7aadc63..c1c068c7085 100644 --- a/cli/src/cli-cmd.h +++ b/cli/src/cli-cmd.h @@ -1,69 +1,129 @@ /* - Copyright (c) 2006-2009 Gluster, Inc. <http://www.gluster.com> + Copyright (c) 2006-2012 Red Hat, Inc. <http://www.redhat.com> This file is part of GlusterFS. - GlusterFS is free software; you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - GlusterFS is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see - <http://www.gnu.org/licenses/>. + 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 __CLI_CMD_H__ #define __CLI_CMD_H__ -#ifndef _CONFIG_H -#define _CONFIG_H -#include "config.h" -#endif +#include <netdb.h> #include "cli.h" +#include <glusterfs/list.h> + +#define GLUSTER_SHARED_STORAGE "gluster_shared_storage" + +#define CLI_LOCAL_INIT(local, words, frame, dictionary) \ + do { \ + local = cli_local_get(); \ + \ + if (local) { \ + local->words = words; \ + if (dictionary) \ + local->dict = dictionary; \ + if (frame) \ + frame->local = local; \ + } \ + } while (0) + +#define CLI_STACK_DESTROY(_frame) \ + do { \ + if (_frame) { \ + if (_frame->local) { \ + gf_log("cli", GF_LOG_DEBUG, \ + "frame->local " \ + "is not NULL (%p)", \ + _frame->local); \ + cli_local_wipe(_frame->local); \ + _frame->local = NULL; \ + } \ + STACK_DESTROY(_frame->root); \ + } \ + } while (0); + +typedef enum { GF_ANSWER_YES = 1, GF_ANSWER_NO = 2 } gf_answer_t; struct cli_cmd { - const char *pattern; - cli_cmd_cbk_t *cbk; - const char *desc; + const char *pattern; + cli_cmd_cbk_t *cbk; + const char *desc; + cli_cmd_reg_cbk_t *reg_cbk; /* callback to check in runtime if the * + * command should be enabled or disabled */ + gf_boolean_t disable; }; struct cli_cmd_volume_get_ctx_ { - char *volname; - int flags; + char *volname; + int flags; }; +typedef struct cli_profile_info_ { + uint64_t fop_hits; + double min_latency; + double max_latency; + double avg_latency; + char *fop_name; + double percentage_avg_latency; +} cli_profile_info_t; + typedef struct cli_cmd_volume_get_ctx_ cli_cmd_volume_get_ctx_t; -int cli_cmd_volume_register (struct cli_state *state); +int +cli_cmd_volume_register(struct cli_state *state); -int cli_cmd_probe_register (struct cli_state *state); +int +cli_cmd_probe_register(struct cli_state *state); -int cli_cmd_misc_register (struct cli_state *state); +int +cli_cmd_system_register(struct cli_state *state); -struct cli_cmd_word *cli_cmd_nextword (struct cli_cmd_word *word, - const char *text); -void cli_cmd_tokens_destroy (char **tokens); +int +cli_cmd_snapshot_register(struct cli_state *state); -int cli_cmd_await_response (); +int +cli_cmd_global_register(struct cli_state *state); -int cli_cmd_broadcast_response (int32_t status); +int +cli_cmd_misc_register(struct cli_state *state); -int cli_cmd_cond_init (); +struct cli_cmd_word * +cli_cmd_nextword(struct cli_cmd_word *word, const char *text); +void +cli_cmd_tokens_destroy(char **tokens); -int cli_cmd_lock (); +int +cli_cmd_await_response(unsigned time); -int cli_cmd_unlock (); +int +cli_cmd_broadcast_response(int32_t status); int -cli_cmd_submit (void *req, call_frame_t *frame, - rpc_clnt_prog_t *prog, - int procnum, struct iobref *iobref, - cli_serialize_t sfunc, xlator_t *this, - fop_cbk_fn_t cbkfn); +cli_cmd_lock(); + +int +cli_cmd_unlock(); + +int +cli_cmd_submit(struct rpc_clnt *rpc, void *req, call_frame_t *frame, + rpc_clnt_prog_t *prog, int procnum, struct iobref *iobref, + xlator_t *this, fop_cbk_fn_t cbkfn, xdrproc_t xdrproc); + +int +cli_cmd_pattern_cmp(void *a, void *b); + +void +cli_cmd_sort(struct cli_cmd *cmd, int count); + +gf_answer_t +cli_cmd_get_confirmation(struct cli_state *state, const char *question); +int +cli_cmd_sent_status_get(int *status); + +gf_boolean_t +_limits_set_on_volume(char *volname, int type); + #endif /* __CLI_CMD_H__ */ diff --git a/cli/src/cli-mem-types.h b/cli/src/cli-mem-types.h index 86e346641ff..b42b4dd86c2 100644 --- a/cli/src/cli-mem-types.h +++ b/cli/src/cli-mem-types.h @@ -1,38 +1,30 @@ /* - Copyright (c) 2010 Gluster, Inc. <http://www.gluster.com> + Copyright (c) 2010-2012 Red Hat, Inc. <http://www.redhat.com> This file is part of GlusterFS. - GlusterFS is free software; you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - GlusterFS is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see - <http://www.gnu.org/licenses/>. + 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 __CLI_MEM_TYPES_H__ #define __CLI_MEM_TYPES_H__ -#include "mem-types.h" +#include <glusterfs/mem-types.h> #define CLI_MEM_TYPE_START (gf_common_mt_end + 1) enum cli_mem_types_ { - cli_mt_xlator_list_t = CLI_MEM_TYPE_START, - cli_mt_xlator_t, - cli_mt_xlator_cmdline_option_t, - cli_mt_char, - cli_mt_call_pool_t, - cli_mt_cli_local_t, - cli_mt_cli_get_vol_ctx_t, - cli_mt_end + cli_mt_xlator_list_t = CLI_MEM_TYPE_START, + cli_mt_xlator_t, + cli_mt_xlator_cmdline_option_t, + cli_mt_char, + cli_mt_call_pool_t, + cli_mt_cli_local_t, + cli_mt_cli_get_vol_ctx_t, + cli_mt_append_str, + cli_mt_cli_cmd, + cli_mt_end }; diff --git a/cli/src/cli-quotad-client.c b/cli/src/cli-quotad-client.c new file mode 100644 index 00000000000..772b8f75bd9 --- /dev/null +++ b/cli/src/cli-quotad-client.c @@ -0,0 +1,146 @@ +/* + Copyright (c) 2010-2013 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + 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 "cli-quotad-client.h" + +int +cli_quotad_submit_request(void *req, call_frame_t *frame, rpc_clnt_prog_t *prog, + int procnum, struct iobref *iobref, xlator_t *this, + fop_cbk_fn_t cbkfn, xdrproc_t xdrproc) +{ + int ret = -1; + int count = 0; + struct iovec iov = { + 0, + }; + struct iobuf *iobuf = NULL; + char new_iobref = 0; + ssize_t xdr_size = 0; + + GF_ASSERT(this); + + if (req) { + xdr_size = xdr_sizeof(xdrproc, req); + iobuf = iobuf_get2(this->ctx->iobuf_pool, xdr_size); + if (!iobuf) { + goto out; + }; + + if (!iobref) { + iobref = iobref_new(); + if (!iobref) { + goto out; + } + + new_iobref = 1; + } + + iobref_add(iobref, iobuf); + + iov.iov_base = iobuf->ptr; + iov.iov_len = iobuf_size(iobuf); + + /* Create the xdr payload */ + ret = xdr_serialize_generic(iov, req, xdrproc); + if (ret == -1) { + goto out; + } + iov.iov_len = ret; + count = 1; + } + + /* Send the msg */ + ret = rpc_clnt_submit(global_quotad_rpc, prog, procnum, cbkfn, &iov, count, + NULL, 0, iobref, frame, NULL, 0, NULL, 0, NULL); + ret = 0; + +out: + if (new_iobref) + iobref_unref(iobref); + if (iobuf) + iobuf_unref(iobuf); + + return ret; +} + +int +cli_quotad_notify(struct rpc_clnt *rpc, void *mydata, rpc_clnt_event_t event, + void *data) +{ + xlator_t *this = NULL; + int ret = 0; + + this = mydata; + + switch (event) { + case RPC_CLNT_CONNECT: { + gf_log(this->name, GF_LOG_TRACE, "got RPC_CLNT_CONNECT"); + break; + } + + case RPC_CLNT_DISCONNECT: { + gf_log(this->name, GF_LOG_TRACE, "got RPC_CLNT_DISCONNECT"); + break; + } + + default: + gf_log(this->name, GF_LOG_TRACE, "got some other RPC event %d", + event); + ret = 0; + break; + } + + return ret; +} + +struct rpc_clnt * +cli_quotad_clnt_init(xlator_t *this, dict_t *options) +{ + struct rpc_clnt *rpc = NULL; + int ret = -1; + + ret = dict_set_nstrn(options, "transport.address-family", + SLEN("transport.address-family"), "unix", + SLEN("unix")); + if (ret) + goto out; + + ret = dict_set_nstrn(options, "transport-type", SLEN("transport-type"), + "socket", SLEN("socket")); + if (ret) + goto out; + + ret = dict_set_nstrn(options, "transport.socket.connect-path", + SLEN("transport.socket.connect-path"), + "/var/run/gluster/quotad.socket", + SLEN("/var/run/gluster/quotad.socket")); + if (ret) + goto out; + + rpc = rpc_clnt_new(options, this, this->name, 16); + if (!rpc) + goto out; + + ret = rpc_clnt_register_notify(rpc, cli_quotad_notify, this); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "failed to register notify"); + goto out; + } + + rpc_clnt_start(rpc); +out: + if (ret) { + if (rpc) + rpc_clnt_unref(rpc); + rpc = NULL; + } + + return rpc; +} diff --git a/cli/src/cli-quotad-client.h b/cli/src/cli-quotad-client.h new file mode 100644 index 00000000000..71a44e5916b --- /dev/null +++ b/cli/src/cli-quotad-client.h @@ -0,0 +1,29 @@ +/* + Copyright (c) 2010-2013 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + 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 "cli.h" +#include <glusterfs/compat-errno.h> +#include <glusterfs/compat.h> +#include "cli-cmd.h" +#include "cli1-xdr.h" +#include "xdr-generic.h" +#include "protocol-common.h" +#include "cli-mem-types.h" + +int +cli_quotad_submit_request(void *req, call_frame_t *frame, rpc_clnt_prog_t *prog, + int procnum, struct iobref *iobref, xlator_t *this, + fop_cbk_fn_t cbkfn, xdrproc_t xdrproc); + +struct rpc_clnt * +cli_quotad_clnt_init(xlator_t *this, dict_t *options); + +int +cli_quotad_notify(struct rpc_clnt *rpc, void *mydata, rpc_clnt_event_t event, + void *data); diff --git a/cli/src/cli-rl.c b/cli/src/cli-rl.c index 5d1310858ec..7a38a0b882a 100644 --- a/cli/src/cli-rl.c +++ b/cli/src/cli-rl.c @@ -1,38 +1,23 @@ /* - Copyright (c) 2010 Gluster, Inc. <http://www.gluster.com> - This file is part of GlusterFS. - - GlusterFS is free software; you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - GlusterFS is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see - <http://www.gnu.org/licenses/>. -*/ + Copyright (c) 2010-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + 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 <stdio.h> #include <string.h> #include <stdlib.h> #include <stdint.h> #include <pthread.h> -#ifndef _CONFIG_H -#define _CONFIG_H -#include "config.h" -#endif - #include "cli.h" #include "cli-cmd.h" #include "cli-mem-types.h" -#include "event.h" +#include <glusterfs/gf-event.h> #include <fnmatch.h> @@ -42,357 +27,376 @@ #include <readline/readline.h> #include <readline/history.h> - int -cli_rl_out (struct cli_state *state, const char *fmt, va_list ap) +cli_rl_out(struct cli_state *state, const char *fmt, va_list ap) { - int tmp_rl_point = rl_point; - int n = rl_end; - int i = 0; - int ret = 0; - - if (rl_end >= 0 ) { - rl_kill_text (0, rl_end); - rl_redisplay (); - } + int tmp_rl_point = rl_point; + int n = rl_end; + int ret = 0; - printf ("\r"); + if (rl_end >= 0) { + rl_kill_text(0, rl_end); + rl_redisplay(); + } - for (i = 0; i <= strlen (state->prompt); i++) - printf (" "); + printf("\r%*s\r", (int)strlen(state->prompt), ""); - printf ("\r"); + ret = vprintf(fmt, ap); - ret = vprintf (fmt, ap); + printf("\n"); + fflush(stdout); - printf ("\n"); - fflush(stdout); + if (n) { + rl_do_undo(); + rl_point = tmp_rl_point; + rl_reset_line_state(); + } - if (n) { - rl_do_undo (); - rl_point = tmp_rl_point; - rl_reset_line_state (); - } - - return ret; + return ret; } +int +cli_rl_err(struct cli_state *state, const char *fmt, va_list ap) +{ + int tmp_rl_point = rl_point; + int n = rl_end; + int ret = 0; + + if (rl_end >= 0) { + rl_kill_text(0, rl_end); + rl_redisplay(); + } + + fprintf(stderr, "\r%*s\r", (int)strlen(state->prompt), ""); + + ret = vfprintf(stderr, fmt, ap); + + fprintf(stderr, "\n"); + fflush(stderr); + + if (n) { + rl_do_undo(); + rl_point = tmp_rl_point; + rl_reset_line_state(); + } + + return ret; +} void -cli_rl_process_line (char *line) +cli_rl_process_line(char *line) { - struct cli_state *state = NULL; - int ret = 0; + struct cli_state *state = NULL; + int ret = 0; - state = global_state; + state = global_state; - state->rl_processing = 1; - { - ret = cli_cmd_process_line (state, line); - add_history (line); - } - state->rl_processing = 0; -} + state->rl_processing = 1; + { + ret = cli_cmd_process_line(state, line); + if (ret) + gf_log(THIS->name, GF_LOG_WARNING, "failed to process line"); + add_history(line); + } + state->rl_processing = 0; +} -int -cli_rl_stdin (int fd, int idx, void *data, - int poll_out, int poll_in, int poll_err) +void +cli_rl_stdin(int fd, int idx, int gen, void *data, int poll_out, int poll_in, + int poll_err, char event_thread_died) { - rl_callback_read_char (); + struct cli_state *state = NULL; - return 0; -} + state = data; + + rl_callback_read_char(); + + gf_event_handled(state->ctx->event_pool, fd, idx, gen); + return; +} char * -cli_rl_autocomplete_entry (const char *text, int times) +cli_rl_autocomplete_entry(const char *text, int times) { - struct cli_state *state = NULL; - char *retp = NULL; + struct cli_state *state = NULL; + char *retp = NULL; - state = global_state; + state = global_state; - if (!state->matchesp) - return NULL; + if (!state->matchesp) + return NULL; - retp = *state->matchesp; + retp = *state->matchesp; - state->matchesp++; + state->matchesp++; - return retp ? strdup (retp) : NULL; + return retp ? strdup(retp) : NULL; } - int -cli_rl_token_count (const char *text) +cli_rl_token_count(const char *text) { - int count = 0; - const char *trav = NULL; - int is_spc = 1; - - for (trav = text; *trav; trav++) { - if (*trav == ' ') { - is_spc = 1; - } else { - if (is_spc) { - count++; - is_spc = 0; - } - } + int count = 0; + const char *trav = NULL; + int is_spc = 1; + + for (trav = text; *trav; trav++) { + if (*trav == ' ') { + is_spc = 1; + } else { + if (is_spc) { + count++; + is_spc = 0; + } } + } - if (is_spc) - /* what needs to be autocompleted is a full - new word, and not extend the last word - */ - count++; + if (is_spc) + /* what needs to be autocompleted is a full + new word, and not extend the last word + */ + count++; - return count; + return count; } - char ** -cli_rl_tokenize (const char *text) +cli_rl_tokenize(const char *text) { - int count = 0; - char **tokens = NULL; - char **tokenp = NULL; - char *token = NULL; - char *copy = NULL; - char *saveptr = NULL; - int i = 0; - - count = cli_rl_token_count (text); - - tokens = calloc (count + 1, sizeof (*tokens)); - if (!tokens) - return NULL; - - copy = strdup (text); - if (!copy) - goto out; - - tokenp = tokens; - - for (token = strtok_r (copy, " \t\r\n", &saveptr); token; - token = strtok_r (NULL, " \t\r\n", &saveptr)) { - *tokenp = strdup (token); - - if (!*tokenp) - goto out; - tokenp++; - i++; - - } + int count = 0; + char **tokens = NULL; + char **tokenp = NULL; + char *token = NULL; + char *copy = NULL; + char *saveptr = NULL; + int i = 0; + + count = cli_rl_token_count(text); + + tokens = calloc(count + 1, sizeof(*tokens)); + if (!tokens) + return NULL; - if (i < count) { - /* symoblize that what needs to be autocompleted is - the full set of possible nextwords, and not extend - the last word - */ - *tokenp = strdup (""); - if (!*tokenp) - goto out; - tokenp++; - i++; - } + copy = strdup(text); + if (!copy) + goto out; + + tokenp = tokens; + + for (token = strtok_r(copy, " \t\r\n", &saveptr); token; + token = strtok_r(NULL, " \t\r\n", &saveptr)) { + *tokenp = strdup(token); + + if (!*tokenp) + goto out; + tokenp++; + i++; + } + + if (i < count) { + /* symbolize that what needs to be autocompleted is + the full set of possible nextwords, and not extend + the last word + */ + *tokenp = strdup(""); + if (!*tokenp) + goto out; + tokenp++; + i++; + } out: - if (copy) - free (copy); + free(copy); - if (i < count) { - cli_cmd_tokens_destroy (tokens); - tokens = NULL; - } + if (i < count) { + cli_cmd_tokens_destroy(tokens); + tokens = NULL; + } - return tokens; + return tokens; } - char ** -cli_rl_get_matches (struct cli_state *state, struct cli_cmd_word *word, - const char *text) +cli_rl_get_matches(struct cli_state *state, struct cli_cmd_word *word, + const char *text) { - char **matches = NULL; - char **matchesp = NULL; - struct cli_cmd_word **next = NULL; - int count = 0; - int len = 0; + char **matches = NULL; + char **matchesp = NULL; + struct cli_cmd_word **next = NULL; + int count = 0; + int len = 0; - len = strlen (text); + len = strlen(text); - if (!word->nextwords) - return NULL; + if (!word->nextwords) + return NULL; - for (next = word->nextwords; *next; next++) - count++; + for (next = word->nextwords; *next; next++) + count++; - matches = calloc (count + 1, sizeof (*matches)); - matchesp = matches; + matches = calloc(count + 1, sizeof(*matches)); + matchesp = matches; - for (next = word->nextwords; *next; next++) { - if ((*next)->match) { - continue; - } + for (next = word->nextwords; *next; next++) { + if ((*next)->match) { + continue; + } - if (strncmp ((*next)->word, text, len) == 0) { - *matchesp = strdup ((*next)->word); - matchesp++; - } + if (strncmp((*next)->word, text, len) == 0) { + *matchesp = strdup((*next)->word); + matchesp++; } + } - return matches; + return matches; } - int -cli_rl_autocomplete_prepare (struct cli_state *state, const char *text) +cli_rl_autocomplete_prepare(struct cli_state *state, const char *text) { - struct cli_cmd_word *word = NULL; - struct cli_cmd_word *next = NULL; - char **tokens = NULL; - char **tokenp = NULL; - char *token = NULL; - char **matches = NULL; - - tokens = cli_rl_tokenize (text); - if (!tokens) - return 0; - - word = &state->tree.root; - - for (tokenp = tokens; (token = *tokenp); tokenp++) { - if (!*(tokenp+1)) { - /* last word */ - break; - } - - next = cli_cmd_nextword (word, token); - word = next; - if (!word) - break; + struct cli_cmd_word *word = NULL; + struct cli_cmd_word *next = NULL; + char **tokens = NULL; + char **tokenp = NULL; + char *token = NULL; + char **matches = NULL; + + tokens = cli_rl_tokenize(text); + if (!tokens) + return 0; + + word = &state->tree.root; + + for (tokenp = tokens; (token = *tokenp); tokenp++) { + if (!*(tokenp + 1)) { + /* last word */ + break; } + next = cli_cmd_nextword(word, token); + word = next; if (!word) - goto out; + break; + } + + if (!word || !token) + goto out; - matches = cli_rl_get_matches (state, word, token); + matches = cli_rl_get_matches(state, word, token); - state->matches = matches; - state->matchesp = matches; + state->matches = matches; + state->matchesp = matches; out: - cli_cmd_tokens_destroy (tokens); - return 0; + cli_cmd_tokens_destroy(tokens); + return 0; } - int -cli_rl_autocomplete_cleanup (struct cli_state *state) +cli_rl_autocomplete_cleanup(struct cli_state *state) { - if (state->matches) - cli_cmd_tokens_destroy (state->matches); + if (state->matches) + cli_cmd_tokens_destroy(state->matches); - state->matches = NULL; - state->matchesp = NULL; + state->matches = NULL; + state->matchesp = NULL; - return 0; + return 0; } - char ** -cli_rl_autocomplete (const char *text, int start, int end) +cli_rl_autocomplete(const char *text, int start, int end) { - struct cli_state *state = NULL; - char **matches = NULL; - char save = 0; + struct cli_state *state = NULL; + char **matches = NULL; + char save = 0; - state = global_state; + state = global_state; - /* hack to make the autocompletion code neater */ - /* fake it as though the cursor is at the end of line */ + /* hack to make the autocompletion code neater */ + /* fake it as though the cursor is at the end of line */ - save = rl_line_buffer[rl_point]; - rl_line_buffer[rl_point] = 0; + save = rl_line_buffer[rl_point]; + rl_line_buffer[rl_point] = 0; - cli_rl_autocomplete_prepare (state, rl_line_buffer); + cli_rl_autocomplete_prepare(state, rl_line_buffer); - matches = rl_completion_matches (text, cli_rl_autocomplete_entry); + matches = rl_completion_matches(text, cli_rl_autocomplete_entry); - cli_rl_autocomplete_cleanup (state); + cli_rl_autocomplete_cleanup(state); - rl_line_buffer[rl_point] = save; + rl_line_buffer[rl_point] = save; - return matches; + return matches; } - static char * -complete_none (const char *txt, int times) +complete_none(const char *txt, int times) { - return NULL; + return NULL; } - void * -cli_rl_input (void *_data) +cli_rl_input(void *_data) { - struct cli_state *state = NULL; - char *line = NULL; + struct cli_state *state = NULL; + char *line = NULL; - state = _data; + state = _data; - for (;;) { - line = readline (state->prompt); - if (!line) - break; + fprintf(stderr, + "Welcome to gluster prompt, type 'help' to see the available " + "commands.\n"); + for (;;) { + line = readline(state->prompt); + if (!line) + exit(0); // break; - cli_rl_process_line (line); + if (*line) + cli_rl_process_line(line); - free (line); - } + free(line); + } - return NULL; + return NULL; } - int -cli_rl_enable (struct cli_state *state) +cli_rl_enable(struct cli_state *state) { - int ret = 0; - - rl_pre_input_hook = NULL; - rl_attempted_completion_function = cli_rl_autocomplete; - rl_completion_entry_function = complete_none; - - if (!state->rl_async) { - ret = pthread_create (&state->input, NULL, - cli_rl_input, state); - if (ret == 0) - state->rl_enabled = 1; - goto out; - } + int ret = 0; + + rl_pre_input_hook = NULL; + rl_attempted_completion_function = cli_rl_autocomplete; + rl_completion_entry_function = complete_none; - ret = event_register (state->ctx->event_pool, 0, cli_rl_stdin, state, - 1, 0); - if (ret == -1) - goto out; + if (!state->rl_async) { + ret = pthread_create(&state->input, NULL, cli_rl_input, state); + if (ret == 0) + state->rl_enabled = 1; + goto out; + } - state->rl_enabled = 1; - rl_callback_handler_install (state->prompt, cli_rl_process_line); + ret = gf_event_register(state->ctx->event_pool, 0, cli_rl_stdin, state, 1, + 0, 0); + if (ret == -1) + goto out; + + state->rl_enabled = 1; + rl_callback_handler_install(state->prompt, cli_rl_process_line); out: - return state->rl_enabled; + return state->rl_enabled; } #else /* HAVE_READLINE */ int -cli_rl_enable (struct cli_state *state) +cli_rl_enable(struct cli_state *state) { - return 0; + return 0; } #endif /* HAVE_READLINE */ diff --git a/cli/src/cli-rpc-ops.c b/cli/src/cli-rpc-ops.c new file mode 100644 index 00000000000..9b6b0c7fa50 --- /dev/null +++ b/cli/src/cli-rpc-ops.c @@ -0,0 +1,10949 @@ +/* + Copyright (c) 2010-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + 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. +*/ + +/* Widths of various columns in top read/write-perf output + * Total width of top read/write-perf should be 80 chars + * including one space between column + */ +#define VOL_TOP_PERF_FILENAME_DEF_WIDTH 47 +#define VOL_TOP_PERF_FILENAME_ALT_WIDTH 44 +#define VOL_TOP_PERF_SPEED_WIDTH 4 +#define VOL_TOP_PERF_TIME_WIDTH 26 + +#define INDENT_MAIN_HEAD "%-25s %s " + +#define RETURNING "Returning %d" +#define XML_ERROR "Error outputting to xml" +#define XDR_DECODE_FAIL "Failed to decode xdr response" +#define DICT_SERIALIZE_FAIL "Failed to serialize to data to dictionary" +#define DICT_UNSERIALIZE_FAIL "Failed to unserialize the dictionary" + +/* Do not show estimates if greater than this number */ +#define REBAL_ESTIMATE_SEC_UPPER_LIMIT (60 * 24 * 3600) +#define REBAL_ESTIMATE_START_TIME 600 + +#include "cli.h" +#include <glusterfs/compat-errno.h> +#include "cli-cmd.h" +#include <sys/uio.h> +#include <stdlib.h> +#include <sys/mount.h> +#include <glusterfs/compat.h> +#include "cli-mem-types.h" +#include <glusterfs/syscall.h> +#include "glusterfs3.h" +#include "portmap-xdr.h" +#include <glusterfs/byte-order.h> + +#include <glusterfs/run.h> +#include <glusterfs/events.h> + +enum gf_task_types { GF_TASK_TYPE_REBALANCE, GF_TASK_TYPE_REMOVE_BRICK }; + +rpc_clnt_prog_t cli_quotad_clnt; + +static int32_t +gf_cli_remove_brick(call_frame_t *frame, xlator_t *this, void *data); + +char *cli_vol_status_str[] = { + "Created", + "Started", + "Stopped", +}; + +char *cli_vol_task_status_str[] = {"not started", + "in progress", + "stopped", + "completed", + "failed", + "fix-layout in progress", + "fix-layout stopped", + "fix-layout completed", + "fix-layout failed", + "unknown"}; + +static int32_t +gf_cli_snapshot(call_frame_t *frame, xlator_t *this, void *data); + +static int32_t +gf_cli_get_volume(call_frame_t *frame, xlator_t *this, void *data); + +static int +cli_to_glusterd(gf_cli_req *req, call_frame_t *frame, fop_cbk_fn_t cbkfn, + xdrproc_t xdrproc, dict_t *dict, int procnum, xlator_t *this, + rpc_clnt_prog_t *prog, struct iobref *iobref); + +static int +add_cli_cmd_timeout_to_dict(dict_t *dict); + +static rpc_clnt_prog_t cli_handshake_prog = { + .progname = "cli handshake", + .prognum = GLUSTER_HNDSK_PROGRAM, + .progver = GLUSTER_HNDSK_VERSION, +}; + +static rpc_clnt_prog_t cli_pmap_prog = { + .progname = "cli portmap", + .prognum = GLUSTER_PMAP_PROGRAM, + .progver = GLUSTER_PMAP_VERSION, +}; + +static void +gf_free_xdr_cli_rsp(gf_cli_rsp rsp) +{ + if (rsp.dict.dict_val) { + free(rsp.dict.dict_val); + } + if (rsp.op_errstr) { + free(rsp.op_errstr); + } +} + +static void +gf_free_xdr_getspec_rsp(gf_getspec_rsp rsp) +{ + if (rsp.spec) { + free(rsp.spec); + } + if (rsp.xdata.xdata_val) { + free(rsp.xdata.xdata_val); + } +} + +static void +gf_free_xdr_fsm_log_rsp(gf1_cli_fsm_log_rsp rsp) +{ + if (rsp.op_errstr) { + free(rsp.op_errstr); + } + if (rsp.fsm_log.fsm_log_val) { + free(rsp.fsm_log.fsm_log_val); + } +} + +static int +gf_cli_probe_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + char msg[1024] = "success"; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + // rsp.op_ret = -1; + // rsp.op_errno = EINVAL; + goto out; + } + + gf_log("cli", GF_LOG_INFO, "Received resp to probe"); + + if (rsp.op_errstr && rsp.op_errstr[0] != '\0') { + snprintf(msg, sizeof(msg), "%s", rsp.op_errstr); + if (rsp.op_ret) { + gf_log("cli", GF_LOG_ERROR, "%s", msg); + } + } + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_str(NULL, (rsp.op_ret) ? NULL : msg, rsp.op_ret, + rsp.op_errno, (rsp.op_ret) ? msg : NULL); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + + if (!rsp.op_ret) + cli_out("peer probe: %s", msg); + else + cli_err("peer probe: failed: %s", msg); + + ret = rsp.op_ret; + +out: + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + return ret; +} + +static int +gf_cli_deprobe_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + char msg[1024] = "success"; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + // rsp.op_ret = -1; + // rsp.op_errno = EINVAL; + goto out; + } + + gf_log("cli", GF_LOG_INFO, "Received resp to deprobe"); + + if (rsp.op_ret) { + if (rsp.op_errstr[0] != '\0') { + snprintf(msg, sizeof(msg), "%s", rsp.op_errstr); + gf_log("cli", GF_LOG_ERROR, "%s", rsp.op_errstr); + } + } + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_str(NULL, (rsp.op_ret) ? NULL : msg, rsp.op_ret, + rsp.op_errno, (rsp.op_ret) ? msg : NULL); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + + if (!rsp.op_ret) + cli_out("peer detach: %s", msg); + else + cli_err("peer detach: failed: %s", msg); + + ret = rsp.op_ret; + +out: + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + return ret; +} + +static int +gf_cli_output_peer_hostnames(dict_t *dict, int count, const char *prefix) +{ + int ret = -1; + char key[512] = { + 0, + }; + int i = 0; + char *hostname = NULL; + + cli_out("Other names:"); + /* Starting from friend.hostname1, as friend.hostname0 will be the same + * as friend.hostname + */ + for (i = 1; i < count; i++) { + ret = snprintf(key, sizeof(key), "%s.hostname%d", prefix, i); + ret = dict_get_strn(dict, key, ret, &hostname); + if (ret) + break; + cli_out("%s", hostname); + hostname = NULL; + } + + return ret; +} + +static int +gf_cli_output_peer_status(dict_t *dict, int count) +{ + int ret = -1; + char *uuid_buf = NULL; + char *hostname_buf = NULL; + int32_t i = 1; + char key[256] = { + 0, + }; + int keylen; + char *state = NULL; + int32_t connected = 0; + const char *connected_str = NULL; + int hostname_count = 0; + + cli_out("Number of Peers: %d", count); + i = 1; + while (i <= count) { + keylen = snprintf(key, sizeof(key), "friend%d.uuid", i); + ret = dict_get_strn(dict, key, keylen, &uuid_buf); + if (ret) + goto out; + + keylen = snprintf(key, sizeof(key), "friend%d.hostname", i); + ret = dict_get_strn(dict, key, keylen, &hostname_buf); + if (ret) + goto out; + + keylen = snprintf(key, sizeof(key), "friend%d.connected", i); + ret = dict_get_int32n(dict, key, keylen, &connected); + if (ret) + goto out; + if (connected) + connected_str = "Connected"; + else + connected_str = "Disconnected"; + + keylen = snprintf(key, sizeof(key), "friend%d.state", i); + ret = dict_get_strn(dict, key, keylen, &state); + if (ret) + goto out; + + cli_out("\nHostname: %s\nUuid: %s\nState: %s (%s)", hostname_buf, + uuid_buf, state, connected_str); + + keylen = snprintf(key, sizeof(key), "friend%d.hostname_count", i); + ret = dict_get_int32n(dict, key, keylen, &hostname_count); + /* Print other addresses only if there are more than 1. + */ + if ((ret == 0) && (hostname_count > 1)) { + snprintf(key, sizeof(key), "friend%d", i); + ret = gf_cli_output_peer_hostnames(dict, hostname_count, key); + if (ret) { + gf_log("cli", GF_LOG_WARNING, + "error outputting peer other names"); + goto out; + } + } + i++; + } + + ret = 0; +out: + return ret; +} + +static int +gf_cli_output_pool_list(dict_t *dict, int count) +{ + int ret = -1; + char *uuid_buf = NULL; + char *hostname_buf = NULL; + int32_t hostname_len = 8; /*min len 8 chars*/ + int32_t i = 1; + char key[64] = { + 0, + }; + int keylen; + int32_t connected = 0; + const char *connected_str = NULL; + + if (count <= 0) + goto out; + + while (i <= count) { + keylen = snprintf(key, sizeof(key), "friend%d.hostname", i); + ret = dict_get_strn(dict, key, keylen, &hostname_buf); + if (ret) + goto out; + + ret = strlen(hostname_buf); + if (ret > hostname_len) + hostname_len = ret; + + i++; + } + + cli_out("UUID\t\t\t\t\t%-*s\tState", hostname_len, "Hostname"); + + i = 1; + while (i <= count) { + keylen = snprintf(key, sizeof(key), "friend%d.uuid", i); + ret = dict_get_strn(dict, key, keylen, &uuid_buf); + if (ret) + goto out; + + keylen = snprintf(key, sizeof(key), "friend%d.hostname", i); + ret = dict_get_strn(dict, key, keylen, &hostname_buf); + if (ret) + goto out; + + keylen = snprintf(key, sizeof(key), "friend%d.connected", i); + ret = dict_get_int32n(dict, key, keylen, &connected); + if (ret) + goto out; + if (connected) + connected_str = "Connected"; + else + connected_str = "Disconnected"; + + cli_out("%s\t%-*s\t%s ", uuid_buf, hostname_len, hostname_buf, + connected_str); + i++; + } + + ret = 0; +out: + return ret; +} + +/* function pointer for gf_cli_output_{pool_list,peer_status} */ +typedef int (*cli_friend_output_fn)(dict_t *, int); + +static int +gf_cli_list_friends_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf1_cli_peer_list_rsp rsp = { + 0, + }; + int ret = -1; + dict_t *dict = NULL; + char msg[1024] = { + 0, + }; + const char *cmd = NULL; + cli_friend_output_fn friend_output_fn; + call_frame_t *frame = NULL; + unsigned long flags = 0; + + if (-1 == req->rpc_status) { + goto out; + } + + GF_ASSERT(myframe); + + frame = myframe; + + flags = (long)frame->local; + + if (flags == GF_CLI_LIST_POOL_NODES) { + cmd = "pool list"; + friend_output_fn = &gf_cli_output_pool_list; + } else { + cmd = "peer status"; + friend_output_fn = &gf_cli_output_peer_status; + } + + /* 'free' the flags set by gf_cli_list_friends */ + frame->local = NULL; + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf1_cli_peer_list_rsp); + if (ret < 0) { + gf_log(frame->this->name, GF_LOG_ERROR, XDR_DECODE_FAIL); + // rsp.op_ret = -1; + // rsp.op_errno = EINVAL; + goto out; + } + + gf_log("cli", GF_LOG_DEBUG, "Received resp to list: %d", rsp.op_ret); + + if (!rsp.op_ret) { + if (!rsp.friends.friends_len) { + snprintf(msg, sizeof(msg), "%s: No peers present", cmd); + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_peer_status(dict, rsp.op_ret, rsp.op_errno, + msg); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + cli_err("%s", msg); + ret = 0; + goto out; + } + + dict = dict_new(); + + if (!dict) { + ret = -1; + goto out; + } + + ret = dict_unserialize(rsp.friends.friends_val, rsp.friends.friends_len, + &dict); + + if (ret) { + gf_log("", GF_LOG_ERROR, DICT_UNSERIALIZE_FAIL); + goto out; + } + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_peer_status(dict, rsp.op_ret, rsp.op_errno, + msg); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + + ret = dict_get_int32_sizen(dict, "count", &count); + if (ret) { + goto out; + } + + ret = friend_output_fn(dict, count); + if (ret) { + goto out; + } + } else { + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_peer_status(dict, rsp.op_ret, rsp.op_errno, + NULL); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + } else { + ret = -1; + } + goto out; + } + + ret = 0; + +out: + if (ret) + cli_err("%s: failed", cmd); + + cli_cmd_broadcast_response(ret); + + if (dict) + dict_unref(dict); + + if (rsp.friends.friends_val) { + free(rsp.friends.friends_val); + } + return ret; +} + +static int +gf_cli_get_state_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + dict_t *dict = NULL; + char *daemon_name = NULL; + char *ofilepath = NULL; + + GF_VALIDATE_OR_GOTO("cli", myframe, out); + + if (-1 == req->rpc_status) { + goto out; + } + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + goto out; + } + + dict = dict_new(); + + if (!dict) { + ret = -1; + goto out; + } + + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, &dict); + if (ret) + goto out; + + if (rsp.op_ret) { + if (strcmp(rsp.op_errstr, "")) + cli_err("Failed to get daemon state: %s", rsp.op_errstr); + else + cli_err( + "Failed to get daemon state. Check glusterd" + " log file for more details"); + } else { + ret = dict_get_str_sizen(dict, "daemon", &daemon_name); + if (ret) + gf_log("cli", GF_LOG_ERROR, "Couldn't get daemon name"); + + ret = dict_get_str_sizen(dict, "ofilepath", &ofilepath); + if (ret) + gf_log("cli", GF_LOG_ERROR, "Couldn't get filepath"); + + if (daemon_name && ofilepath) + cli_out("%s state dumped to %s", daemon_name, ofilepath); + } + + ret = rsp.op_ret; + +out: + if (dict) + dict_unref(dict); + + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + + return ret; +} + +static void +cli_out_options(char *substr, char *optstr, char *valstr) +{ + char *ptr1 = NULL; + char *ptr2 = NULL; + + ptr1 = substr; + ptr2 = optstr; + + while (ptr1) { + /* Avoiding segmentation fault. */ + if (!ptr2) + return; + if (*ptr1 != *ptr2) + break; + ptr1++; + ptr2++; + } + + if (*ptr2 == '\0') + return; + cli_out("%s: %s", ptr2, valstr); +} + +static int +_gf_cli_output_volinfo_opts(dict_t *d, char *k, data_t *v, void *tmp) +{ + int ret = 0; + char *key = NULL; + char *ptr = NULL; + data_t *value = NULL; + + key = tmp; + + ptr = strstr(k, "option."); + if (ptr) { + value = v; + if (!value) { + ret = -1; + goto out; + } + cli_out_options(key, k, v->data); + } +out: + return ret; +} + +static int +print_brick_details(dict_t *dict, int volcount, int start_index, int end_index, + int replica_count) +{ + char key[64] = { + 0, + }; + int keylen; + int index = start_index; + int isArbiter = 0; + int ret = -1; + char *brick = NULL; + + while (index <= end_index) { + keylen = snprintf(key, sizeof(key), "volume%d.brick%d", volcount, + index); + ret = dict_get_strn(dict, key, keylen, &brick); + if (ret) + goto out; + keylen = snprintf(key, sizeof(key), "volume%d.brick%d.isArbiter", + volcount, index); + if (dict_getn(dict, key, keylen)) + isArbiter = 1; + else + isArbiter = 0; + + if (isArbiter) + cli_out("Brick%d: %s (arbiter)", index, brick); + else + cli_out("Brick%d: %s", index, brick); + index++; + } + ret = 0; +out: + return ret; +} + +static void +gf_cli_print_number_of_bricks(int type, int brick_count, int dist_count, + int stripe_count, int replica_count, + int disperse_count, int redundancy_count, + int arbiter_count) +{ + if (type == GF_CLUSTER_TYPE_NONE) { + cli_out("Number of Bricks: %d", brick_count); + } else if (type == GF_CLUSTER_TYPE_DISPERSE) { + cli_out("Number of Bricks: %d x (%d + %d) = %d", + (brick_count / dist_count), disperse_count - redundancy_count, + redundancy_count, brick_count); + } else { + /* For both replicate and stripe, dist_count is + good enough */ + if (arbiter_count == 0) { + cli_out("Number of Bricks: %d x %d = %d", + (brick_count / dist_count), dist_count, brick_count); + } else { + cli_out("Number of Bricks: %d x (%d + %d) = %d", + (brick_count / dist_count), dist_count - arbiter_count, + arbiter_count, brick_count); + } + } +} + +static int +gf_cli_get_volume_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + int ret = -1; + int opt_count = 0; + int32_t i = 0; + int32_t j = 1; + int32_t status = 0; + int32_t type = 0; + int32_t brick_count = 0; + int32_t dist_count = 0; + int32_t stripe_count = 0; + int32_t replica_count = 0; + int32_t disperse_count = 0; + int32_t redundancy_count = 0; + int32_t arbiter_count = 0; + int32_t snap_count = 0; + int32_t thin_arbiter_count = 0; + int32_t vol_type = 0; + int32_t transport = 0; + char *volume_id_str = NULL; + char *volname = NULL; + char *ta_brick = NULL; + dict_t *dict = NULL; + cli_local_t *local = NULL; + char key[64] = {0}; + int keylen; + char err_str[2048] = {0}; + gf_cli_rsp rsp = {0}; + char *caps __attribute__((unused)) = NULL; + int k __attribute__((unused)) = 0; + call_frame_t *frame = NULL; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) + goto out; + + frame = myframe; + + GF_ASSERT(frame->local); + + local = frame->local; + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(frame->this->name, GF_LOG_ERROR, XDR_DECODE_FAIL); + goto out; + } + + gf_log("cli", GF_LOG_INFO, "Received resp to get vol: %d", rsp.op_ret); + + if (!rsp.dict.dict_len) { + if (global_state->mode & GLUSTER_MODE_XML) + goto xml_output; + cli_err("No volumes present"); + ret = 0; + goto out; + } + + dict = dict_new(); + + if (!dict) { + ret = -1; + goto out; + } + + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, &dict); + + if (ret) { + gf_log("cli", GF_LOG_ERROR, DICT_UNSERIALIZE_FAIL); + goto out; + } + + ret = dict_get_int32_sizen(dict, "count", &count); + if (ret) + goto out; + + if (!count) { + switch (local->get_vol.flags) { + case GF_CLI_GET_NEXT_VOLUME: + GF_FREE(local->get_vol.volname); + local->get_vol.volname = NULL; + ret = 0; + goto out; + + case GF_CLI_GET_VOLUME: + snprintf(err_str, sizeof(err_str), "Volume %s does not exist", + local->get_vol.volname); + ret = -1; + if (!(global_state->mode & GLUSTER_MODE_XML)) + goto out; + } + } + + if (rsp.op_ret) { + if (global_state->mode & GLUSTER_MODE_XML) + goto xml_output; + ret = -1; + goto out; + } + +xml_output: + if (global_state->mode & GLUSTER_MODE_XML) { + /* For GET_NEXT_VOLUME output is already begun in + * and will also end in gf_cli_get_next_volume() + */ + if (local->get_vol.flags == GF_CLI_GET_VOLUME) { + ret = cli_xml_output_vol_info_begin(local, rsp.op_ret, rsp.op_errno, + rsp.op_errstr); + if (ret) { + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + } + + if (dict) { + ret = cli_xml_output_vol_info(local, dict); + if (ret) { + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + } + + if (local->get_vol.flags == GF_CLI_GET_VOLUME) { + ret = cli_xml_output_vol_info_end(local); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + } + goto out; + } + + while (i < count) { + cli_out(" "); + keylen = snprintf(key, sizeof(key), "volume%d.name", i); + ret = dict_get_strn(dict, key, keylen, &volname); + if (ret) + goto out; + + keylen = snprintf(key, sizeof(key), "volume%d.type", i); + ret = dict_get_int32n(dict, key, keylen, &type); + if (ret) + goto out; + + keylen = snprintf(key, sizeof(key), "volume%d.status", i); + ret = dict_get_int32n(dict, key, keylen, &status); + if (ret) + goto out; + + keylen = snprintf(key, sizeof(key), "volume%d.brick_count", i); + ret = dict_get_int32n(dict, key, keylen, &brick_count); + if (ret) + goto out; + + keylen = snprintf(key, sizeof(key), "volume%d.dist_count", i); + ret = dict_get_int32n(dict, key, keylen, &dist_count); + if (ret) + goto out; + + keylen = snprintf(key, sizeof(key), "volume%d.stripe_count", i); + ret = dict_get_int32n(dict, key, keylen, &stripe_count); + if (ret) + goto out; + + keylen = snprintf(key, sizeof(key), "volume%d.replica_count", i); + ret = dict_get_int32n(dict, key, keylen, &replica_count); + if (ret) + goto out; + + keylen = snprintf(key, sizeof(key), "volume%d.disperse_count", i); + ret = dict_get_int32n(dict, key, keylen, &disperse_count); + if (ret) + goto out; + + keylen = snprintf(key, sizeof(key), "volume%d.redundancy_count", i); + ret = dict_get_int32n(dict, key, keylen, &redundancy_count); + if (ret) + goto out; + + keylen = snprintf(key, sizeof(key), "volume%d.arbiter_count", i); + ret = dict_get_int32n(dict, key, keylen, &arbiter_count); + if (ret) + goto out; + + keylen = snprintf(key, sizeof(key), "volume%d.transport", i); + ret = dict_get_int32n(dict, key, keylen, &transport); + if (ret) + goto out; + + keylen = snprintf(key, sizeof(key), "volume%d.volume_id", i); + ret = dict_get_strn(dict, key, keylen, &volume_id_str); + if (ret) + goto out; + + keylen = snprintf(key, sizeof(key), "volume%d.snap_count", i); + ret = dict_get_int32n(dict, key, keylen, &snap_count); + if (ret) + goto out; + + keylen = snprintf(key, sizeof(key), "volume%d.thin_arbiter_count", i); + ret = dict_get_int32n(dict, key, keylen, &thin_arbiter_count); + if (ret) + goto out; + + // Distributed (stripe/replicate/stripe-replica) setups + vol_type = get_vol_type(type, dist_count, brick_count); + + cli_out("Volume Name: %s", volname); + cli_out("Type: %s", vol_type_str[vol_type]); + cli_out("Volume ID: %s", volume_id_str); + cli_out("Status: %s", cli_vol_status_str[status]); + cli_out("Snapshot Count: %d", snap_count); + + gf_cli_print_number_of_bricks( + type, brick_count, dist_count, stripe_count, replica_count, + disperse_count, redundancy_count, arbiter_count); + + cli_out("Transport-type: %s", + ((transport == 0) ? "tcp" + : (transport == 1) ? "rdma" : "tcp,rdma")); + j = 1; + + GF_FREE(local->get_vol.volname); + local->get_vol.volname = gf_strdup(volname); + + cli_out("Bricks:"); + ret = print_brick_details(dict, i, j, brick_count, replica_count); + if (ret) + goto out; + + if (thin_arbiter_count) { + snprintf(key, sizeof(key), "volume%d.thin_arbiter_brick", i); + ret = dict_get_str(dict, key, &ta_brick); + if (ret) + goto out; + cli_out("Thin-arbiter-path: %s", ta_brick); + } + + snprintf(key, sizeof(key), "volume%d.opt_count", i); + ret = dict_get_int32(dict, key, &opt_count); + if (ret) + goto out; + + if (!opt_count) + goto out; + + cli_out("Options Reconfigured:"); + + snprintf(key, sizeof(key), "volume%d.option.", i); + + ret = dict_foreach(dict, _gf_cli_output_volinfo_opts, key); + if (ret) + goto out; + + i++; + } + + ret = 0; +out: + if (ret) + cli_err("%s", err_str); + + cli_cmd_broadcast_response(ret); + + if (dict) + dict_unref(dict); + + gf_free_xdr_cli_rsp(rsp); + + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + return ret; +} + +static int +gf_cli_create_volume_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + cli_local_t *local = NULL; + char *volname = NULL; + dict_t *rsp_dict = NULL; + call_frame_t *frame = NULL; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + frame = myframe; + + GF_ASSERT(frame->local); + + local = frame->local; + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(frame->this->name, GF_LOG_ERROR, XDR_DECODE_FAIL); + goto out; + } + + gf_log("cli", GF_LOG_INFO, "Received resp to create volume"); + + if (global_state->mode & GLUSTER_MODE_XML) { + if (rsp.op_ret == 0) { + rsp_dict = dict_new(); + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, + &rsp_dict); + if (ret) { + gf_log("cli", GF_LOG_ERROR, DICT_UNSERIALIZE_FAIL); + goto out; + } + } + + ret = cli_xml_output_vol_create(rsp_dict, rsp.op_ret, rsp.op_errno, + rsp.op_errstr); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + + ret = dict_get_str_sizen(local->dict, "volname", &volname); + if (ret) + goto out; + + if (rsp.op_ret && strcmp(rsp.op_errstr, "")) + cli_err("volume create: %s: failed: %s", volname, rsp.op_errstr); + else if (rsp.op_ret) + cli_err("volume create: %s: failed", volname); + else + cli_out( + "volume create: %s: success: " + "please start the volume to access data", + volname); + + ret = rsp.op_ret; + +out: + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + + if (rsp_dict) + dict_unref(rsp_dict); + return ret; +} + +static int +gf_cli_delete_volume_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + cli_local_t *local = NULL; + char *volname = NULL; + call_frame_t *frame = NULL; + dict_t *rsp_dict = NULL; + + if (-1 == req->rpc_status) { + goto out; + } + + GF_ASSERT(myframe); + frame = myframe; + + GF_ASSERT(frame->local); + + local = frame->local; + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(frame->this->name, GF_LOG_ERROR, XDR_DECODE_FAIL); + goto out; + } + + gf_log("cli", GF_LOG_INFO, "Received resp to delete volume"); + + if (global_state->mode & GLUSTER_MODE_XML) { + if (rsp.op_ret == 0) { + rsp_dict = dict_new(); + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, + &rsp_dict); + if (ret) { + gf_log("cli", GF_LOG_ERROR, DICT_UNSERIALIZE_FAIL); + goto out; + } + } + + ret = cli_xml_output_generic_volume("volDelete", rsp_dict, rsp.op_ret, + rsp.op_errno, rsp.op_errstr); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + + ret = dict_get_str_sizen(local->dict, "volname", &volname); + if (ret) { + gf_log(frame->this->name, GF_LOG_ERROR, "dict get failed"); + goto out; + } + + if (rsp.op_ret && strcmp(rsp.op_errstr, "")) + cli_err("volume delete: %s: failed: %s", volname, rsp.op_errstr); + else if (rsp.op_ret) + cli_err("volume delete: %s: failed", volname); + else + cli_out("volume delete: %s: success", volname); + + ret = rsp.op_ret; + +out: + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + + if (rsp_dict) + dict_unref(rsp_dict); + gf_log("", GF_LOG_DEBUG, RETURNING, ret); + return ret; +} + +static int +gf_cli3_1_uuid_get_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + char *uuid_str = NULL; + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + cli_local_t *local = NULL; + call_frame_t *frame = NULL; + dict_t *dict = NULL; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) + goto out; + + frame = myframe; + + GF_ASSERT(frame->local); + + local = frame->local; + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(frame->this->name, GF_LOG_ERROR, XDR_DECODE_FAIL); + goto out; + } + + frame->local = NULL; + + gf_log("cli", GF_LOG_INFO, "Received resp to uuid get"); + + dict = dict_new(); + if (!dict) { + ret = -1; + goto out; + } + + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, &dict); + if (ret) { + gf_log("cli", GF_LOG_ERROR, DICT_UNSERIALIZE_FAIL); + goto out; + } + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_dict("uuidGenerate", dict, rsp.op_ret, + rsp.op_errno, rsp.op_errstr); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + + if (rsp.op_ret) { + if (strcmp(rsp.op_errstr, "") == 0) + cli_err("Get uuid was unsuccessful"); + else + cli_err("%s", rsp.op_errstr); + + } else { + ret = dict_get_str_sizen(dict, "uuid", &uuid_str); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get uuid from dictionary"); + goto out; + } + cli_out("UUID: %s", uuid_str); + } + ret = rsp.op_ret; + +out: + cli_cmd_broadcast_response(ret); + cli_local_wipe(local); + gf_free_xdr_cli_rsp(rsp); + + if (dict) + dict_unref(dict); + + gf_log("", GF_LOG_DEBUG, RETURNING, ret); + return ret; +} + +static int +gf_cli3_1_uuid_reset_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + cli_local_t *local = NULL; + call_frame_t *frame = NULL; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + frame = myframe; + + GF_ASSERT(frame->local); + + local = frame->local; + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(frame->this->name, GF_LOG_ERROR, XDR_DECODE_FAIL); + goto out; + } + + frame->local = NULL; + + gf_log("cli", GF_LOG_INFO, "Received resp to uuid reset"); + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_dict("uuidReset", NULL, rsp.op_ret, rsp.op_errno, + rsp.op_errstr); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + + if (rsp.op_ret && strcmp(rsp.op_errstr, "")) + cli_err("%s", rsp.op_errstr); + else + cli_out("resetting the peer uuid has been %s", + (rsp.op_ret) ? "unsuccessful" : "successful"); + ret = rsp.op_ret; + +out: + cli_cmd_broadcast_response(ret); + cli_local_wipe(local); + gf_free_xdr_cli_rsp(rsp); + + gf_log("", GF_LOG_DEBUG, RETURNING, ret); + return ret; +} + +static int +gf_cli_start_volume_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + cli_local_t *local = NULL; + char *volname = NULL; + call_frame_t *frame = NULL; + dict_t *rsp_dict = NULL; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + frame = myframe; + + GF_ASSERT(frame->local); + + local = frame->local; + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(frame->this->name, GF_LOG_ERROR, XDR_DECODE_FAIL); + goto out; + } + + gf_log("cli", GF_LOG_INFO, "Received resp to start volume"); + + if (global_state->mode & GLUSTER_MODE_XML) { + if (rsp.op_ret == 0) { + rsp_dict = dict_new(); + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, + &rsp_dict); + if (ret) { + gf_log("cli", GF_LOG_ERROR, DICT_UNSERIALIZE_FAIL); + goto out; + } + } + + ret = cli_xml_output_generic_volume("volStart", rsp_dict, rsp.op_ret, + rsp.op_errno, rsp.op_errstr); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + + ret = dict_get_str_sizen(local->dict, "volname", &volname); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "dict get failed"); + goto out; + } + + if (rsp.op_ret && strcmp(rsp.op_errstr, "")) + cli_err("volume start: %s: failed: %s", volname, rsp.op_errstr); + else if (rsp.op_ret) + cli_err("volume start: %s: failed", volname); + else + cli_out("volume start: %s: success", volname); + + ret = rsp.op_ret; + +out: + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + + if (rsp_dict) + dict_unref(rsp_dict); + return ret; +} + +static int +gf_cli_stop_volume_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + cli_local_t *local = NULL; + char *volname = NULL; + call_frame_t *frame = NULL; + dict_t *rsp_dict = NULL; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + frame = myframe; + + GF_ASSERT(frame->local); + + local = frame->local; + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(frame->this->name, GF_LOG_ERROR, XDR_DECODE_FAIL); + goto out; + } + + gf_log("cli", GF_LOG_INFO, "Received resp to stop volume"); + + if (global_state->mode & GLUSTER_MODE_XML) { + if (rsp.op_ret == 0) { + rsp_dict = dict_new(); + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, + &rsp_dict); + if (ret) { + gf_log("cli", GF_LOG_ERROR, DICT_UNSERIALIZE_FAIL); + goto out; + } + } + + ret = cli_xml_output_generic_volume("volStop", rsp_dict, rsp.op_ret, + rsp.op_errno, rsp.op_errstr); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + + ret = dict_get_str_sizen(local->dict, "volname", &volname); + if (ret) { + gf_log(frame->this->name, GF_LOG_ERROR, + "Unable to get volname from dict"); + goto out; + } + + if (rsp.op_ret && strcmp(rsp.op_errstr, "")) + cli_err("volume stop: %s: failed: %s", volname, rsp.op_errstr); + else if (rsp.op_ret) + cli_err("volume stop: %s: failed", volname); + else + cli_out("volume stop: %s: success", volname); + + ret = rsp.op_ret; + +out: + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + + if (rsp_dict) + dict_unref(rsp_dict); + + return ret; +} + +static int +gf_cli_print_rebalance_status(dict_t *dict, enum gf_task_types task_type) +{ + int ret = -1; + int count = 0; + int i = 1; + char key[64] = { + 0, + }; + int keylen; + gf_defrag_status_t status_rcd = GF_DEFRAG_STATUS_NOT_STARTED; + uint64_t files = 0; + uint64_t size = 0; + uint64_t lookup = 0; + char *node_name = NULL; + uint64_t failures = 0; + uint64_t skipped = 0; + double elapsed = 0; + char *status_str = NULL; + char *size_str = NULL; + int32_t hrs = 0; + uint32_t min = 0; + uint32_t sec = 0; + gf_boolean_t fix_layout = _gf_false; + uint64_t max_time = 0; + uint64_t max_elapsed = 0; + uint64_t time_left = 0; + gf_boolean_t show_estimates = _gf_false; + + ret = dict_get_int32_sizen(dict, "count", &count); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "count not set"); + goto out; + } + + for (i = 1; i <= count; i++) { + keylen = snprintf(key, sizeof(key), "status-%d", i); + ret = dict_get_int32n(dict, key, keylen, (int32_t *)&status_rcd); + /* If information from a node is missing we should skip + * the node and try to fetch information of other nodes. + * If information is not found for all nodes, we should + * error out. + */ + if (!ret) + break; + if (ret && i == count) { + gf_log("cli", GF_LOG_TRACE, "failed to get status"); + goto out; + } + } + + /* Fix layout will be sent to all nodes for the volume + so every status should be of type + GF_DEFRAG_STATUS_LAYOUT_FIX* + */ + + if ((task_type == GF_TASK_TYPE_REBALANCE) && + (status_rcd >= GF_DEFRAG_STATUS_LAYOUT_FIX_STARTED)) { + fix_layout = _gf_true; + } + + if (fix_layout) { + cli_out("%35s %41s %27s", "Node", "status", "run time in h:m:s"); + cli_out("%35s %41s %27s", "---------", "-----------", "------------"); + } else { + cli_out("%40s %16s %13s %13s %13s %13s %20s %18s", "Node", + "Rebalanced-files", "size", "scanned", "failures", "skipped", + "status", + "run time in" + " h:m:s"); + cli_out("%40s %16s %13s %13s %13s %13s %20s %18s", "---------", + "-----------", "-----------", "-----------", "-----------", + "-----------", "------------", "--------------"); + } + + for (i = 1; i <= count; i++) { + /* Reset the variables to prevent carryover of values */ + node_name = NULL; + files = 0; + size = 0; + lookup = 0; + skipped = 0; + status_str = NULL; + elapsed = 0; + time_left = 0; + + /* Check if status is NOT_STARTED, and continue early */ + keylen = snprintf(key, sizeof(key), "status-%d", i); + + ret = dict_get_int32n(dict, key, keylen, (int32_t *)&status_rcd); + if (ret == -ENOENT) { + gf_log("cli", GF_LOG_TRACE, "count %d %d", count, i); + gf_log("cli", GF_LOG_TRACE, "failed to get status"); + gf_log("cli", GF_LOG_ERROR, "node down and has failed to set dict"); + continue; + /* skip this node if value not available*/ + } else if (ret) { + gf_log("cli", GF_LOG_TRACE, "count %d %d", count, i); + gf_log("cli", GF_LOG_TRACE, "failed to get status"); + continue; + /* skip this node if value not available*/ + } + + if (GF_DEFRAG_STATUS_NOT_STARTED == status_rcd) + continue; + + if (GF_DEFRAG_STATUS_STARTED == status_rcd) + show_estimates = _gf_true; + + keylen = snprintf(key, sizeof(key), "node-name-%d", i); + ret = dict_get_strn(dict, key, keylen, &node_name); + if (ret) + gf_log("cli", GF_LOG_TRACE, "failed to get node-name"); + + snprintf(key, sizeof(key), "files-%d", i); + ret = dict_get_uint64(dict, key, &files); + if (ret) + gf_log("cli", GF_LOG_TRACE, "failed to get file count"); + + snprintf(key, sizeof(key), "size-%d", i); + ret = dict_get_uint64(dict, key, &size); + if (ret) + gf_log("cli", GF_LOG_TRACE, "failed to get size of xfer"); + + snprintf(key, sizeof(key), "lookups-%d", i); + ret = dict_get_uint64(dict, key, &lookup); + if (ret) + gf_log("cli", GF_LOG_TRACE, "failed to get lookedup file count"); + + snprintf(key, sizeof(key), "failures-%d", i); + ret = dict_get_uint64(dict, key, &failures); + if (ret) + gf_log("cli", GF_LOG_TRACE, "failed to get failures count"); + + snprintf(key, sizeof(key), "skipped-%d", i); + ret = dict_get_uint64(dict, key, &skipped); + if (ret) + gf_log("cli", GF_LOG_TRACE, "failed to get skipped count"); + + /* For remove-brick include skipped count into failure count*/ + if (task_type != GF_TASK_TYPE_REBALANCE) { + failures += skipped; + skipped = 0; + } + + snprintf(key, sizeof(key), "run-time-%d", i); + ret = dict_get_double(dict, key, &elapsed); + if (ret) + gf_log("cli", GF_LOG_TRACE, "failed to get run-time"); + + snprintf(key, sizeof(key), "time-left-%d", i); + ret = dict_get_uint64(dict, key, &time_left); + if (ret) + gf_log("cli", GF_LOG_TRACE, "failed to get time left"); + + if (elapsed > max_elapsed) + max_elapsed = elapsed; + + if (time_left > max_time) + max_time = time_left; + + /* Check for array bound */ + if (status_rcd >= GF_DEFRAG_STATUS_MAX) + status_rcd = GF_DEFRAG_STATUS_MAX; + + status_str = cli_vol_task_status_str[status_rcd]; + size_str = gf_uint64_2human_readable(size); + hrs = elapsed / 3600; + min = ((uint64_t)elapsed % 3600) / 60; + sec = ((uint64_t)elapsed % 3600) % 60; + + if (fix_layout) { + cli_out("%35s %50s %8d:%d:%d", node_name, status_str, hrs, min, + sec); + } else { + if (size_str) { + cli_out("%40s %16" PRIu64 + " %13s" + " %13" PRIu64 " %13" PRIu64 " %13" PRIu64 + " %20s " + "%8d:%02d:%02d", + node_name, files, size_str, lookup, failures, skipped, + status_str, hrs, min, sec); + } else { + cli_out("%40s %16" PRIu64 " %13" PRIu64 " %13" PRIu64 + " %13" PRIu64 " %13" PRIu64 + " %20s" + " %8d:%02d:%02d", + node_name, files, size, lookup, failures, skipped, + status_str, hrs, min, sec); + } + } + GF_FREE(size_str); + } + + /* Max time will be non-zero if rebalance is still running */ + if (max_time) { + hrs = max_time / 3600; + min = (max_time % 3600) / 60; + sec = (max_time % 3600) % 60; + + if (hrs < REBAL_ESTIMATE_SEC_UPPER_LIMIT) { + cli_out( + "Estimated time left for rebalance to " + "complete : %8d:%02d:%02d", + hrs, min, sec); + } else { + cli_out( + "Estimated time left for rebalance to " + "complete : > 2 months. Please try again " + "later."); + } + } else { + /* Rebalance will return 0 if it could not calculate the + * estimates or if it is complete. + */ + if (!show_estimates) { + goto out; + } + if (max_elapsed <= REBAL_ESTIMATE_START_TIME) { + cli_out( + "The estimated time for rebalance to complete " + "will be unavailable for the first 10 " + "minutes."); + } else { + cli_out( + "Rebalance estimated time unavailable. Please " + "try again later."); + } + } +out: + return ret; +} + +static int +gf_cli_defrag_volume_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + cli_local_t *local = NULL; + char *volname = NULL; + call_frame_t *frame = NULL; + int cmd = 0; + int ret = -1; + dict_t *dict = NULL; + char msg[1024] = { + 0, + }; + char *task_id_str = NULL; + + if (-1 == req->rpc_status) { + goto out; + } + + GF_ASSERT(myframe); + + frame = myframe; + + GF_ASSERT(frame->local); + + local = frame->local; + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(frame->this->name, GF_LOG_ERROR, XDR_DECODE_FAIL); + goto out; + } + + ret = dict_get_str_sizen(local->dict, "volname", &volname); + if (ret) { + gf_log(frame->this->name, GF_LOG_ERROR, "Failed to get volname"); + goto out; + } + + ret = dict_get_int32_sizen(local->dict, "rebalance-command", + (int32_t *)&cmd); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get command"); + goto out; + } + + if (rsp.dict.dict_len) { + /* Unserialize the dictionary */ + dict = dict_new(); + + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, &dict); + if (ret < 0) { + gf_log("glusterd", GF_LOG_ERROR, DICT_UNSERIALIZE_FAIL); + goto out; + } + } + + if (!((cmd == GF_DEFRAG_CMD_STOP) || (cmd == GF_DEFRAG_CMD_STATUS)) && + !(global_state->mode & GLUSTER_MODE_XML)) { + ret = dict_get_str_sizen(dict, GF_REBALANCE_TID_KEY, &task_id_str); + if (ret) { + gf_log("cli", GF_LOG_WARNING, "failed to get %s from dict", + GF_REBALANCE_TID_KEY); + } + if (rsp.op_ret && strcmp(rsp.op_errstr, "")) { + snprintf(msg, sizeof(msg), "%s", rsp.op_errstr); + } else { + if (!rsp.op_ret) { + /* append errstr in the cli msg for successful + * case since unlock failures can be highlighted + * event though rebalance command was successful + */ + snprintf(msg, sizeof(msg), + "Rebalance on %s has been " + "started successfully. Use " + "rebalance status command to" + " check status of the " + "rebalance process.\nID: %s", + volname, task_id_str); + } else { + snprintf(msg, sizeof(msg), + "Starting rebalance on volume %s has " + "been unsuccessful.", + volname); + } + } + goto done; + } + + if (cmd == GF_DEFRAG_CMD_STOP) { + if (rsp.op_ret == -1) { + if (strcmp(rsp.op_errstr, "")) + snprintf(msg, sizeof(msg), "%s", rsp.op_errstr); + else + snprintf(msg, sizeof(msg), "rebalance volume %s stop failed", + volname); + goto done; + } else { + /* append errstr in the cli msg for successful case + * since unlock failures can be highlighted event though + * rebalance command was successful */ + snprintf(msg, sizeof(msg), + "rebalance process may be in the middle of a " + "file migration.\nThe process will be fully " + "stopped once the migration of the file is " + "complete.\nPlease check rebalance process " + "for completion before doing any further " + "brick related tasks on the volume.\n%s", + rsp.op_errstr); + } + } + if (cmd == GF_DEFRAG_CMD_STATUS) { + if (rsp.op_ret == -1) { + if (strcmp(rsp.op_errstr, "")) + snprintf(msg, sizeof(msg), "%s", rsp.op_errstr); + else + snprintf(msg, sizeof(msg), + "Failed to get the status of rebalance process"); + goto done; + } else { + snprintf(msg, sizeof(msg), "%s", rsp.op_errstr); + } + } + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_vol_rebalance(cmd, dict, rsp.op_ret, rsp.op_errno, + rsp.op_errstr); + goto out; + } + + ret = gf_cli_print_rebalance_status(dict, GF_TASK_TYPE_REBALANCE); + if (ret) + gf_log("cli", GF_LOG_ERROR, "Failed to print rebalance status"); + +done: + if (global_state->mode & GLUSTER_MODE_XML) + cli_xml_output_str("volRebalance", msg, rsp.op_ret, rsp.op_errno, + rsp.op_errstr); + else { + if (rsp.op_ret) + cli_err("volume rebalance: %s: failed%s%s", volname, + strlen(msg) ? ": " : "", msg); + else + cli_out("volume rebalance: %s: success%s%s", volname, + strlen(msg) ? ": " : "", msg); + } + ret = rsp.op_ret; + +out: + gf_free_xdr_cli_rsp(rsp); + if (dict) + dict_unref(dict); + cli_cmd_broadcast_response(ret); + return ret; +} + +static int +gf_cli_rename_volume_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + char msg[1024] = { + 0, + }; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + goto out; + } + + gf_log("cli", GF_LOG_INFO, "Received resp to probe"); + snprintf(msg, sizeof(msg), "Rename volume %s", + (rsp.op_ret) ? "unsuccessful" : "successful"); + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_str("volRename", msg, rsp.op_ret, rsp.op_errno, + rsp.op_errstr); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + + if (rsp.op_ret) + cli_err("volume rename: failed"); + else + cli_out("volume rename: success"); + + ret = rsp.op_ret; + +out: + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + return ret; +} + +static int +gf_cli_reset_volume_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + char msg[1024] = { + 0, + }; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + goto out; + } + + gf_log("cli", GF_LOG_INFO, "Received resp to reset"); + + if (strcmp(rsp.op_errstr, "")) + snprintf(msg, sizeof(msg), "%s", rsp.op_errstr); + else + snprintf(msg, sizeof(msg), "reset volume %s", + (rsp.op_ret) ? "unsuccessful" : "successful"); + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_str("volReset", msg, rsp.op_ret, rsp.op_errno, + rsp.op_errstr); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + + if (rsp.op_ret) + cli_err("volume reset: failed: %s", msg); + else + cli_out("volume reset: success: %s", msg); + + ret = rsp.op_ret; + +out: + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + return ret; +} + +static int +gf_cli_ganesha_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + dict_t *dict = NULL; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + goto out; + } + + gf_log("cli", GF_LOG_DEBUG, "Received resp to ganesha"); + + dict = dict_new(); + + if (!dict) { + ret = -1; + goto out; + } + + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, &dict); + if (ret) + goto out; + + if (rsp.op_ret) { + if (strcmp(rsp.op_errstr, "")) + cli_err("nfs-ganesha: failed: %s", rsp.op_errstr); + else + cli_err("nfs-ganesha: failed"); + } + + else { + cli_out("nfs-ganesha : success "); + } + + ret = rsp.op_ret; + +out: + if (dict) + dict_unref(dict); + cli_cmd_broadcast_response(ret); + return ret; +} + +static char * +is_server_debug_xlator(void *myframe) +{ + call_frame_t *frame = NULL; + cli_local_t *local = NULL; + char **words = NULL; + char *key = NULL; + char *value = NULL; + char *debug_xlator = NULL; + + frame = myframe; + local = frame->local; + words = (char **)local->words; + + while (*words != NULL) { + if (strstr(*words, "trace") == NULL && + strstr(*words, "error-gen") == NULL) { + words++; + continue; + } + + key = *words; + words++; + value = *words; + if (value == NULL) + break; + if (strstr(value, "client")) { + words++; + continue; + } else { + if (!(strstr(value, "posix") || strstr(value, "acl") || + strstr(value, "locks") || strstr(value, "io-threads") || + strstr(value, "marker") || strstr(value, "index"))) { + words++; + continue; + } else { + debug_xlator = gf_strdup(key); + break; + } + } + } + + return debug_xlator; +} + +static int +gf_cli_set_volume_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + dict_t *dict = NULL; + char *help_str = NULL; + char msg[1024] = { + 0, + }; + char *debug_xlator = NULL; + char tmp_str[512] = { + 0, + }; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + goto out; + } + + gf_log("cli", GF_LOG_INFO, "Received resp to set"); + + dict = dict_new(); + + if (!dict) { + ret = -1; + goto out; + } + + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, &dict); + if (ret) { + gf_log("cli", GF_LOG_ERROR, DICT_UNSERIALIZE_FAIL); + goto out; + } + + /* For brick processes graph change does not happen on the fly. + * The process has to be restarted. So this is a check from the + * volume set option such that if debug xlators such as trace/errorgen + * are provided in the set command, warn the user. + */ + debug_xlator = is_server_debug_xlator(myframe); + + if (dict_get_str_sizen(dict, "help-str", &help_str) && !msg[0]) + snprintf(msg, sizeof(msg), "Set volume %s", + (rsp.op_ret) ? "unsuccessful" : "successful"); + if (rsp.op_ret == 0 && debug_xlator) { + snprintf(tmp_str, sizeof(tmp_str), + "\n%s translator has been " + "added to the server volume file. Please restart the" + " volume for enabling the translator", + debug_xlator); + } + + if ((global_state->mode & GLUSTER_MODE_XML) && (help_str == NULL)) { + ret = cli_xml_output_str("volSet", msg, rsp.op_ret, rsp.op_errno, + rsp.op_errstr); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + + if (rsp.op_ret) { + if (strcmp(rsp.op_errstr, "")) + cli_err("volume set: failed: %s", rsp.op_errstr); + else + cli_err("volume set: failed"); + } else { + if (help_str == NULL) { + if (debug_xlator == NULL) + cli_out("volume set: success"); + else + cli_out("volume set: success%s", tmp_str); + } else { + cli_out("%s", help_str); + } + } + + ret = rsp.op_ret; + +out: + if (dict) + dict_unref(dict); + GF_FREE(debug_xlator); + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + return ret; +} + +int +gf_cli_add_brick_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + char msg[1024] = { + 0, + }; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + goto out; + } + + gf_log("cli", GF_LOG_INFO, "Received resp to add brick"); + + if (rsp.op_ret && strcmp(rsp.op_errstr, "")) + snprintf(msg, sizeof(msg), "%s", rsp.op_errstr); + else + snprintf(msg, sizeof(msg), "Add Brick %s", + (rsp.op_ret) ? "unsuccessful" : "successful"); + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_str("volAddBrick", msg, rsp.op_ret, rsp.op_errno, + rsp.op_errstr); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + + if (rsp.op_ret) + cli_err("volume add-brick: failed: %s", rsp.op_errstr); + else + cli_out("volume add-brick: success"); + ret = rsp.op_ret; + +out: + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + return ret; +} + +static int +gf_cli3_remove_brick_status_cbk(struct rpc_req *req, struct iovec *iov, + int count, void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + dict_t *dict = NULL; + char msg[1024] = { + 0, + }; + int32_t command = 0; + gf1_op_commands cmd = GF_OP_CMD_NONE; + cli_local_t *local = NULL; + call_frame_t *frame = NULL; + const char *cmd_str; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + frame = myframe; + + GF_ASSERT(frame->local); + + local = frame->local; + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(frame->this->name, GF_LOG_ERROR, XDR_DECODE_FAIL); + goto out; + } + + ret = dict_get_int32_sizen(local->dict, "command", &command); + if (ret) + goto out; + + cmd = command; + + switch (cmd) { + case GF_OP_CMD_STOP: + cmd_str = "stop"; + break; + case GF_OP_CMD_STATUS: + cmd_str = "status"; + break; + default: + cmd_str = "unknown"; + break; + } + + ret = rsp.op_ret; + if (rsp.op_ret == -1) { + if (strcmp(rsp.op_errstr, "")) + snprintf(msg, sizeof(msg), "volume remove-brick %s: failed: %s", + cmd_str, rsp.op_errstr); + else + snprintf(msg, sizeof(msg), "volume remove-brick %s: failed", + cmd_str); + + if (global_state->mode & GLUSTER_MODE_XML) + goto xml_output; + + cli_err("%s", msg); + goto out; + } + + if (rsp.dict.dict_len) { + /* Unserialize the dictionary */ + dict = dict_new(); + + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, &dict); + if (ret < 0) { + strncpy(msg, DICT_UNSERIALIZE_FAIL, sizeof(msg)); + + if (global_state->mode & GLUSTER_MODE_XML) { + rsp.op_ret = -1; + goto xml_output; + } + + gf_log("cli", GF_LOG_ERROR, "%s", msg); + goto out; + } + } + +xml_output: + if (global_state->mode & GLUSTER_MODE_XML) { + if (strcmp(rsp.op_errstr, "")) { + ret = cli_xml_output_vol_remove_brick(_gf_true, dict, rsp.op_ret, + rsp.op_errno, rsp.op_errstr, + "volRemoveBrick"); + } else { + ret = cli_xml_output_vol_remove_brick(_gf_true, dict, rsp.op_ret, + rsp.op_errno, msg, + "volRemoveBrick"); + } + goto out; + } + + ret = gf_cli_print_rebalance_status(dict, GF_TASK_TYPE_REMOVE_BRICK); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to print remove-brick rebalance status"); + goto out; + } + + if ((cmd == GF_OP_CMD_STOP) && (rsp.op_ret == 0)) { + cli_out( + "'remove-brick' process may be in the middle of a " + "file migration.\nThe process will be fully stopped " + "once the migration of the file is complete.\nPlease " + "check remove-brick process for completion before " + "doing any further brick related tasks on the " + "volume."); + } + +out: + if (dict) + dict_unref(dict); + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + return ret; +} + +static int +gf_cli_remove_brick_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + char msg[1024] = { + 0, + }; + gf1_op_commands cmd = GF_OP_CMD_NONE; + char *cmd_str = "unknown"; + cli_local_t *local = NULL; + call_frame_t *frame = NULL; + char *task_id_str = NULL; + dict_t *rsp_dict = NULL; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + frame = myframe; + + GF_ASSERT(frame->local); + + local = frame->local; + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(frame->this->name, GF_LOG_ERROR, XDR_DECODE_FAIL); + goto out; + } + + ret = dict_get_int32_sizen(local->dict, "command", (int32_t *)&cmd); + if (ret) { + gf_log("", GF_LOG_ERROR, "failed to get command"); + goto out; + } + + if (rsp.dict.dict_len) { + rsp_dict = dict_new(); + if (!rsp_dict) { + ret = -1; + goto out; + } + + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, &rsp_dict); + if (ret) { + gf_log("cli", GF_LOG_ERROR, DICT_UNSERIALIZE_FAIL); + goto out; + } + } + + switch (cmd) { + case GF_OP_CMD_DETACH_START: + case GF_OP_CMD_START: + cmd_str = "start"; + + ret = dict_get_str_sizen(rsp_dict, GF_REMOVE_BRICK_TID_KEY, + &task_id_str); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "remove-brick-id is not present in dict"); + } + break; + case GF_OP_CMD_COMMIT: + cmd_str = "commit"; + break; + case GF_OP_CMD_COMMIT_FORCE: + cmd_str = "commit force"; + break; + default: + cmd_str = "unknown"; + break; + } + + gf_log("cli", GF_LOG_INFO, "Received resp to remove brick"); + + if (rsp.op_ret && strcmp(rsp.op_errstr, "")) + snprintf(msg, sizeof(msg), "%s", rsp.op_errstr); + else + snprintf(msg, sizeof(msg), "Remove Brick %s %s", cmd_str, + (rsp.op_ret) ? "unsuccessful" : "successful"); + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_vol_remove_brick(_gf_false, rsp_dict, rsp.op_ret, + rsp.op_errno, msg, + "volRemoveBrick"); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + + if (rsp.op_ret) { + cli_err("volume remove-brick %s: failed: %s", cmd_str, msg); + } else { + cli_out("volume remove-brick %s: success", cmd_str); + if (GF_OP_CMD_START == cmd && task_id_str != NULL) + cli_out("ID: %s", task_id_str); + if (GF_OP_CMD_COMMIT == cmd) + cli_out( + "Check the removed bricks to ensure all files " + "are migrated.\nIf files with data are " + "found on the brick path, copy them via a " + "gluster mount point before re-purposing the " + "removed brick. "); + } + + ret = rsp.op_ret; + +out: + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + + if (rsp_dict) + dict_unref(rsp_dict); + + return ret; +} + +static int +gf_cli_reset_brick_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + cli_local_t *local = NULL; + call_frame_t *frame = NULL; + const char *rb_operation_str = NULL; + dict_t *rsp_dict = NULL; + char msg[1024] = { + 0, + }; + char *reset_op = NULL; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + frame = myframe; + + GF_ASSERT(frame->local); + + local = frame->local; + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(frame->this->name, GF_LOG_ERROR, XDR_DECODE_FAIL); + goto out; + } + + ret = dict_get_str_sizen(local->dict, "operation", &reset_op); + if (ret) { + gf_log(frame->this->name, GF_LOG_ERROR, "dict_get on operation failed"); + goto out; + } + + if (strcmp(reset_op, "GF_RESET_OP_START") && + strcmp(reset_op, "GF_RESET_OP_COMMIT") && + strcmp(reset_op, "GF_RESET_OP_COMMIT_FORCE")) { + ret = -1; + goto out; + } + + if (rsp.dict.dict_len) { + /* Unserialize the dictionary */ + rsp_dict = dict_new(); + + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, &rsp_dict); + if (ret < 0) { + gf_log(frame->this->name, GF_LOG_ERROR, DICT_UNSERIALIZE_FAIL); + goto out; + } + } + + if (rsp.op_ret && (strcmp(rsp.op_errstr, ""))) { + rb_operation_str = rsp.op_errstr; + } else { + if (!strcmp(reset_op, "GF_RESET_OP_START")) { + if (rsp.op_ret) + rb_operation_str = "reset-brick start operation failed"; + else + rb_operation_str = "reset-brick start operation successful"; + } else if (!strcmp(reset_op, "GF_RESET_OP_COMMIT")) { + if (rsp.op_ret) + rb_operation_str = "reset-brick commit operation failed"; + else + rb_operation_str = "reset-brick commit operation successful"; + } else if (!strcmp(reset_op, "GF_RESET_OP_COMMIT_FORCE")) { + if (rsp.op_ret) + rb_operation_str = "reset-brick commit force operation failed"; + else + rb_operation_str = + "reset-brick commit force operation successful"; + } + } + + gf_log("cli", GF_LOG_INFO, "Received resp to reset brick"); + snprintf(msg, sizeof(msg), "%s", + rb_operation_str ? rb_operation_str : "Unknown operation"); + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_vol_replace_brick(rsp_dict, rsp.op_ret, + rsp.op_errno, msg); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + + if (rsp.op_ret) + cli_err("volume reset-brick: failed: %s", msg); + else + cli_out("volume reset-brick: success: %s", msg); + ret = rsp.op_ret; + +out: + if (frame) + frame->local = NULL; + + if (local) + cli_local_wipe(local); + + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + if (rsp_dict) + dict_unref(rsp_dict); + + return ret; +} +static int +gf_cli_replace_brick_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + cli_local_t *local = NULL; + call_frame_t *frame = NULL; + const char *rb_operation_str = NULL; + dict_t *rsp_dict = NULL; + char msg[1024] = { + 0, + }; + char *replace_op = NULL; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + frame = myframe; + + GF_ASSERT(frame->local); + + local = frame->local; + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(frame->this->name, GF_LOG_ERROR, XDR_DECODE_FAIL); + goto out; + } + + ret = dict_get_str_sizen(local->dict, "operation", &replace_op); + if (ret) { + gf_log(frame->this->name, GF_LOG_ERROR, "dict_get on operation failed"); + goto out; + } + + if (rsp.dict.dict_len) { + /* Unserialize the dictionary */ + rsp_dict = dict_new(); + + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, &rsp_dict); + if (ret < 0) { + gf_log(frame->this->name, GF_LOG_ERROR, DICT_UNSERIALIZE_FAIL); + goto out; + } + } + + if (!strcmp(replace_op, "GF_REPLACE_OP_COMMIT_FORCE")) { + if (rsp.op_ret || ret) + rb_operation_str = "replace-brick commit force operation failed"; + else + rb_operation_str = + "replace-brick commit force operation successful"; + } else { + gf_log(frame->this->name, GF_LOG_DEBUG, "Unknown operation"); + } + + if (rsp.op_ret && (strcmp(rsp.op_errstr, ""))) { + rb_operation_str = rsp.op_errstr; + } + + gf_log("cli", GF_LOG_INFO, "Received resp to replace brick"); + snprintf(msg, sizeof(msg), "%s", + rb_operation_str ? rb_operation_str : "Unknown operation"); + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_vol_replace_brick(rsp_dict, rsp.op_ret, + rsp.op_errno, msg); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + + if (rsp.op_ret) + cli_err("volume replace-brick: failed: %s", msg); + else + cli_out("volume replace-brick: success: %s", msg); + ret = rsp.op_ret; + +out: + if (frame) + frame->local = NULL; + + if (local) + cli_local_wipe(local); + + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + if (rsp_dict) + dict_unref(rsp_dict); + + return ret; +} + +static int +gf_cli_log_rotate_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + char msg[1024] = { + 0, + }; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + goto out; + } + + gf_log("cli", GF_LOG_DEBUG, "Received resp to log rotate"); + + if (rsp.op_ret && strcmp(rsp.op_errstr, "")) + snprintf(msg, sizeof(msg), "%s", rsp.op_errstr); + else + snprintf(msg, sizeof(msg), "log rotate %s", + (rsp.op_ret) ? "unsuccessful" : "successful"); + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_str("volLogRotate", msg, rsp.op_ret, rsp.op_errno, + rsp.op_errstr); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + + if (rsp.op_ret) + cli_err("volume log-rotate: failed: %s", msg); + else + cli_out("volume log-rotate: success"); + ret = rsp.op_ret; + +out: + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + + return ret; +} + +static int +gf_cli_sync_volume_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + char msg[1024] = { + 0, + }; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + goto out; + } + + gf_log("cli", GF_LOG_DEBUG, "Received resp to sync"); + + if (rsp.op_ret && strcmp(rsp.op_errstr, "")) + snprintf(msg, sizeof(msg), "volume sync: failed: %s", rsp.op_errstr); + else + snprintf(msg, sizeof(msg), "volume sync: %s", + (rsp.op_ret) ? "failed" : "success"); + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_str("volSync", msg, rsp.op_ret, rsp.op_errno, + rsp.op_errstr); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + + if (rsp.op_ret) + cli_err("%s", msg); + else + cli_out("%s", msg); + ret = rsp.op_ret; + +out: + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + return ret; +} + +static int +print_quota_list_usage_output(cli_local_t *local, char *path, int64_t avail, + char *sl_str, quota_limits_t *limits, + quota_meta_t *used_space, gf_boolean_t sl, + gf_boolean_t hl, double sl_num, + gf_boolean_t limit_set) +{ + int32_t ret = -1; + char *used_str = NULL; + char *avail_str = NULL; + char *hl_str = NULL; + char *sl_val = NULL; + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_quota_xml_output(local, path, limits->hl, sl_str, sl_num, + used_space->size, avail, sl ? "Yes" : "No", + hl ? "Yes" : "No", limit_set); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to output in xml format for quota list command"); + } + goto out; + } + + used_str = gf_uint64_2human_readable(used_space->size); + + if (limit_set) { + hl_str = gf_uint64_2human_readable(limits->hl); + sl_val = gf_uint64_2human_readable(sl_num); + + if (!used_str) { + cli_out("%-40s %7s %7s(%s) %8" PRIu64 "%9" PRIu64 + "" + "%15s %18s", + path, hl_str, sl_str, sl_val, used_space->size, avail, + sl ? "Yes" : "No", hl ? "Yes" : "No"); + } else { + avail_str = gf_uint64_2human_readable(avail); + cli_out("%-40s %7s %7s(%s) %8s %7s %15s %20s", path, hl_str, sl_str, + sl_val, used_str, avail_str, sl ? "Yes" : "No", + hl ? "Yes" : "No"); + } + } else { + cli_out("%-36s %10s %10s %14s %9s %15s %18s", path, "N/A", "N/A", + used_str, "N/A", "N/A", "N/A"); + } + + ret = 0; +out: + GF_FREE(hl_str); + GF_FREE(used_str); + GF_FREE(avail_str); + GF_FREE(sl_val); + + return ret; +} + +static int +print_quota_list_object_output(cli_local_t *local, char *path, int64_t avail, + char *sl_str, quota_limits_t *limits, + quota_meta_t *used_space, gf_boolean_t sl, + gf_boolean_t hl, double sl_num, + gf_boolean_t limit_set) +{ + int32_t ret = -1; + int64_t sl_val = sl_num; + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_quota_object_xml_output(local, path, sl_str, sl_val, limits, + used_space, avail, sl ? "Yes" : "No", + hl ? "Yes" : "No", limit_set); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to output in xml format for quota list command"); + } + goto out; + } + + if (limit_set) { + cli_out("%-40s %9" PRIu64 " %9s(%" PRId64 ") %10" PRIu64 + "" + "%10" PRIu64 " %11" PRIu64 " %15s %20s", + path, limits->hl, sl_str, sl_val, used_space->file_count, + used_space->dir_count, avail, sl ? "Yes" : "No", + hl ? "Yes" : "No"); + } else { + cli_out("%-40s %9s %9s %10" PRIu64 " %10" PRIu64 " %11s %15s %20s", + path, "N/A", "N/A", used_space->file_count, + used_space->dir_count, "N/A", "N/A", "N/A"); + } + ret = 0; + +out: + + return ret; +} + +static int +print_quota_list_output(cli_local_t *local, char *path, char *default_sl, + quota_limits_t *limits, quota_meta_t *used_space, + int type, gf_boolean_t limit_set) +{ + int64_t avail = 0; + char percent_str[20] = {0}; + char *sl_final = NULL; + int ret = -1; + double sl_num = 0; + gf_boolean_t sl = _gf_false; + gf_boolean_t hl = _gf_false; + int64_t used_size = 0; + + GF_ASSERT(local); + GF_ASSERT(path); + + if (limit_set) { + if (limits->sl < 0) { + ret = gf_string2percent(default_sl, &sl_num); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "could not convert default soft limit to percent"); + goto out; + } + sl_num = (sl_num * limits->hl) / 100; + sl_final = default_sl; + } else { + sl_num = (limits->sl * limits->hl) / 100; + ret = snprintf(percent_str, sizeof(percent_str), "%" PRIu64 "%%", + limits->sl); + if (ret < 0) + goto out; + sl_final = percent_str; + } + if (type == GF_QUOTA_OPTION_TYPE_LIST) + used_size = used_space->size; + else + used_size = used_space->file_count + used_space->dir_count; + + if (limits->hl > used_size) { + avail = limits->hl - used_size; + hl = _gf_false; + if (used_size > sl_num) + sl = _gf_true; + else + sl = _gf_false; + } else { + avail = 0; + hl = sl = _gf_true; + } + } + + if (type == GF_QUOTA_OPTION_TYPE_LIST) + ret = print_quota_list_usage_output(local, path, avail, sl_final, + limits, used_space, sl, hl, sl_num, + limit_set); + else + ret = print_quota_list_object_output(local, path, avail, sl_final, + limits, used_space, sl, hl, sl_num, + limit_set); +out: + return ret; +} + +static int +print_quota_list_from_mountdir(cli_local_t *local, char *mountdir, + char *default_sl, char *path, int type) +{ + int ret = -1; + ssize_t xattr_size = 0; + quota_limits_t limits = { + 0, + }; + quota_meta_t used_space = { + 0, + }; + char *key = NULL; + gf_boolean_t limit_set = _gf_true; + + GF_ASSERT(local); + GF_ASSERT(mountdir); + GF_ASSERT(path); + + if (type == GF_QUOTA_OPTION_TYPE_LIST) + key = QUOTA_LIMIT_KEY; + else + key = QUOTA_LIMIT_OBJECTS_KEY; + + ret = sys_lgetxattr(mountdir, key, (void *)&limits, sizeof(limits)); + if (ret < 0) { + gf_log("cli", GF_LOG_ERROR, + "Failed to get the xattr %s on %s. Reason : %s", key, mountdir, + strerror(errno)); + + switch (errno) { +#if defined(ENODATA) + case ENODATA: +#endif +#if defined(ENOATTR) && (ENOATTR != ENODATA) + case ENOATTR: +#endif + /* If it's an ENOATTR, quota/inode-quota is + * configured(limit is set at least for one directory). + * The user is trying to issue 'list/list-objects' + * command for a directory on which quota limit is + * not set and we are showing the used-space in case + * of list-usage and showing (dir_count, file_count) + * in case of list-objects. Other labels are + * shown "N/A". + */ + + limit_set = _gf_false; + goto enoattr; + break; + + default: + cli_err("%-40s %s", path, strerror(errno)); + break; + } + + goto out; + } + + limits.hl = ntoh64(limits.hl); + limits.sl = ntoh64(limits.sl); + +enoattr: + xattr_size = sys_lgetxattr(mountdir, QUOTA_SIZE_KEY, NULL, 0); + if (xattr_size < (sizeof(int64_t) * 2) && + type == GF_QUOTA_OPTION_TYPE_LIST_OBJECTS) { + ret = -1; + + /* This can happen when glusterfs is upgraded from 3.6 to 3.7 + * and the xattr healing is not completed. + */ + } else if (xattr_size > (sizeof(int64_t) * 2)) { + ret = sys_lgetxattr(mountdir, QUOTA_SIZE_KEY, &used_space, + sizeof(used_space)); + } else if (xattr_size > 0) { + /* This is for compatibility. + * Older version had only file usage + */ + ret = sys_lgetxattr(mountdir, QUOTA_SIZE_KEY, &(used_space.size), + sizeof(used_space.size)); + used_space.file_count = 0; + used_space.dir_count = 0; + } else { + ret = -1; + } + + if (ret < 0) { + gf_log("cli", GF_LOG_ERROR, "Failed to get quota size on path %s: %s", + mountdir, strerror(errno)); + print_quota_list_empty(path, type); + goto out; + } + + used_space.size = ntoh64(used_space.size); + used_space.file_count = ntoh64(used_space.file_count); + used_space.dir_count = ntoh64(used_space.dir_count); + + ret = print_quota_list_output(local, path, default_sl, &limits, &used_space, + type, limit_set); +out: + return ret; +} + +static int +gluster_remove_auxiliary_mount(char *volname) +{ + int ret = -1; + char mountdir[PATH_MAX] = { + 0, + }; + xlator_t *this = NULL; + + this = THIS; + GF_ASSERT(this); + + GLUSTERD_GET_QUOTA_LIST_MOUNT_PATH(mountdir, volname, "/"); + ret = gf_umount_lazy(this->name, mountdir, 1); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "umount on %s failed, reason : %s", + mountdir, strerror(errno)); + } + + return ret; +} + +static int +gf_cli_print_limit_list_from_dict(cli_local_t *local, char *volname, + dict_t *dict, char *default_sl, int count, + int op_ret, int op_errno, char *op_errstr) +{ + int ret = -1; + int i = 0; + char key[32] = { + 0, + }; + int keylen; + char mountdir[PATH_MAX] = { + 0, + }; + char *path = NULL; + int type = -1; + + if (!dict || count <= 0) + goto out; + + ret = dict_get_int32_sizen(dict, "type", &type); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get quota type"); + goto out; + } + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_vol_quota_limit_list_begin(local, op_ret, op_errno, + op_errstr); + if (ret) { + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + } else { + print_quota_list_header(type); + } + + while (count--) { + keylen = snprintf(key, sizeof(key), "path%d", i++); + + ret = dict_get_strn(dict, key, keylen, &path); + if (ret < 0) { + gf_log("cli", GF_LOG_DEBUG, "Path not present in limit list"); + continue; + } + + ret = gf_canonicalize_path(path); + if (ret) + goto out; + GLUSTERD_GET_QUOTA_LIST_MOUNT_PATH(mountdir, volname, path); + ret = print_quota_list_from_mountdir(local, mountdir, default_sl, path, + type); + } + +out: + return ret; +} + +static int +print_quota_list_from_quotad(call_frame_t *frame, dict_t *rsp_dict) +{ + char *path = NULL; + char *default_sl = NULL; + int ret = -1; + cli_local_t *local = NULL; + dict_t *gd_rsp_dict = NULL; + quota_meta_t used_space = { + 0, + }; + quota_limits_t limits = { + 0, + }; + quota_limits_t *size_limits = NULL; + int32_t type = 0; + int32_t success_count = 0; + + GF_ASSERT(frame); + + local = frame->local; + gd_rsp_dict = local->dict; + + ret = dict_get_int32_sizen(rsp_dict, "type", &type); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get type"); + goto out; + } + + ret = dict_get_str_sizen(rsp_dict, GET_ANCESTRY_PATH_KEY, &path); + if (ret) { + gf_log("cli", GF_LOG_WARNING, "path key is not present in dict"); + goto out; + } + + ret = dict_get_str_sizen(gd_rsp_dict, "default-soft-limit", &default_sl); + if (ret) { + gf_log(frame->this->name, GF_LOG_ERROR, + "failed to get default soft limit"); + goto out; + } + + if (type == GF_QUOTA_OPTION_TYPE_LIST) { + ret = dict_get_bin(rsp_dict, QUOTA_LIMIT_KEY, (void **)&size_limits); + if (ret) { + gf_log("cli", GF_LOG_WARNING, "limit key not present in dict on %s", + path); + goto out; + } + } else { + ret = dict_get_bin(rsp_dict, QUOTA_LIMIT_OBJECTS_KEY, + (void **)&size_limits); + if (ret) { + gf_log("cli", GF_LOG_WARNING, + "object limit key not present in dict on %s", path); + goto out; + } + } + + limits.hl = ntoh64(size_limits->hl); + limits.sl = ntoh64(size_limits->sl); + + if (type == GF_QUOTA_OPTION_TYPE_LIST) + ret = quota_dict_get_meta(rsp_dict, QUOTA_SIZE_KEY, + SLEN(QUOTA_SIZE_KEY), &used_space); + else + ret = quota_dict_get_inode_meta(rsp_dict, QUOTA_SIZE_KEY, + SLEN(QUOTA_SIZE_KEY), &used_space); + + if (ret < 0) { + gf_log("cli", GF_LOG_WARNING, "size key not present in dict"); + print_quota_list_empty(path, type); + goto out; + } + + LOCK(&local->lock); + { + ret = dict_get_int32_sizen(gd_rsp_dict, "quota-list-success-count", + &success_count); + if (ret) + success_count = 0; + + ret = dict_set_int32_sizen(gd_rsp_dict, "quota-list-success-count", + success_count + 1); + } + UNLOCK(&local->lock); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to set quota-list-success-count in dict"); + goto out; + } + + if (success_count == 0) { + if (!(global_state->mode & GLUSTER_MODE_XML)) { + print_quota_list_header(type); + } else { + ret = cli_xml_output_vol_quota_limit_list_begin(local, 0, 0, NULL); + if (ret) { + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + } + } + + ret = print_quota_list_output(local, path, default_sl, &limits, &used_space, + type, _gf_true); +out: + return ret; +} + +static void * +cli_cmd_broadcast_response_detached(void *opaque) +{ + int32_t ret = 0; + + ret = (intptr_t)opaque; + cli_cmd_broadcast_response(ret); + + return NULL; +} + +static int32_t +cli_quota_compare_path(struct list_head *list1, struct list_head *list2) +{ + struct list_node *node1 = NULL; + struct list_node *node2 = NULL; + dict_t *dict1 = NULL; + dict_t *dict2 = NULL; + char *path1 = NULL; + char *path2 = NULL; + int ret = 0; + + node1 = list_entry(list1, struct list_node, list); + node2 = list_entry(list2, struct list_node, list); + + dict1 = node1->ptr; + dict2 = node2->ptr; + + ret = dict_get_str_sizen(dict1, GET_ANCESTRY_PATH_KEY, &path1); + if (ret < 0) + return 0; + + ret = dict_get_str_sizen(dict2, GET_ANCESTRY_PATH_KEY, &path2); + if (ret < 0) + return 0; + + return strcmp(path1, path2); +} + +static int +cli_quotad_getlimit_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + /*TODO: we need to gather the path, hard-limit, soft-limit and used space*/ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + dict_t *dict = NULL; + struct list_node *node = NULL; + struct list_node *tmpnode = NULL; + call_frame_t *frame = NULL; + cli_local_t *local = NULL; + int32_t list_count = 0; + pthread_t th_id = { + 0, + }; + int32_t max_count = 0; + + GF_ASSERT(myframe); + + frame = myframe; + + GF_ASSERT(frame->local); + + local = frame->local; + + LOCK(&local->lock); + { + ret = dict_get_int32_sizen(local->dict, "quota-list-count", + &list_count); + if (ret) + list_count = 0; + + list_count++; + ret = dict_set_int32_sizen(local->dict, "quota-list-count", list_count); + } + UNLOCK(&local->lock); + + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to set quota-list-count in dict"); + goto out; + } + + if (-1 == req->rpc_status) { + if (list_count == 0) + cli_err( + "Connection failed. Please check if quota " + "daemon is operational."); + ret = -1; + goto out; + } + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(frame->this->name, GF_LOG_ERROR, XDR_DECODE_FAIL); + goto out; + } + + if (rsp.op_ret) { + ret = -1; + if (strcmp(rsp.op_errstr, "")) + cli_err("quota command failed : %s", rsp.op_errstr); + else + cli_err("quota command : failed"); + goto out; + } + + if (rsp.dict.dict_len) { + /* Unserialize the dictionary */ + dict = dict_new(); + + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, &dict); + if (ret < 0) { + gf_log("cli", GF_LOG_ERROR, DICT_UNSERIALIZE_FAIL); + goto out; + } + + node = list_node_add_order(dict, &local->dict_list, + cli_quota_compare_path); + if (node == NULL) { + gf_log("cli", GF_LOG_ERROR, "failed to add node to the list"); + dict_unref(dict); + ret = -1; + goto out; + } + } + + ret = dict_get_int32_sizen(local->dict, "max_count", &max_count); + if (ret < 0) { + gf_log("cli", GF_LOG_ERROR, "failed to get max_count"); + goto out; + } + + if (list_count == max_count) { + list_for_each_entry_safe(node, tmpnode, &local->dict_list, list) + { + dict = node->ptr; + print_quota_list_from_quotad(frame, dict); + list_node_del(node); + dict_unref(dict); + } + } + +out: + /* Bad Fix: CLI holds the lock to process a command. + * When processing quota list command, below sequence of steps executed + * in the same thread and causing deadlock + * + * 1) CLI holds the lock + * 2) Send rpc_clnt_submit request to quotad for quota usage + * 3) If quotad is down, rpc_clnt_submit invokes cbk function with error + * 4) cbk function cli_quotad_getlimit_cbk invokes + * cli_cmd_broadcast_response which tries to hold lock to broadcast + * the results and hangs, because same thread has already holding + * the lock + * + * Broadcasting response in a separate thread which is not a + * good fix. This needs to be re-visted with better solution + */ + if (ret == -1) { + ret = pthread_create(&th_id, NULL, cli_cmd_broadcast_response_detached, + (void *)-1); + if (ret) + gf_log("cli", GF_LOG_ERROR, "pthread_create failed: %s", + strerror(errno)); + } else { + cli_cmd_broadcast_response(ret); + } + gf_free_xdr_cli_rsp(rsp); + + return ret; +} + +static int +cli_quotad_getlimit(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + int ret = 0; + dict_t *dict = NULL; + + if (!frame || !this || !data) { + ret = -1; + goto out; + } + + dict = data; + ret = add_cli_cmd_timeout_to_dict(dict); + + ret = dict_allocate_and_serialize(dict, &req.dict.dict_val, + &req.dict.dict_len); + if (ret < 0) { + gf_log(this->name, GF_LOG_ERROR, DICT_SERIALIZE_FAIL); + + goto out; + } + + ret = cli_cmd_submit(global_quotad_rpc, &req, frame, &cli_quotad_clnt, + GF_AGGREGATOR_GETLIMIT, NULL, this, + cli_quotad_getlimit_cbk, (xdrproc_t)xdr_gf_cli_req); + +out: + GF_FREE(req.dict.dict_val); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + return ret; +} + +static void +gf_cli_quota_list(cli_local_t *local, char *volname, dict_t *dict, + char *default_sl, int count, int op_ret, int op_errno, + char *op_errstr) +{ + if (!cli_cmd_connected()) + goto out; + + if (count > 0) { + GF_VALIDATE_OR_GOTO("cli", volname, out); + + gf_cli_print_limit_list_from_dict(local, volname, dict, default_sl, + count, op_ret, op_errno, op_errstr); + } +out: + return; +} + +static int +gf_cli_quota_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + dict_t *dict = NULL; + char *volname = NULL; + int32_t type = 0; + call_frame_t *frame = NULL; + char *default_sl = NULL; + cli_local_t *local = NULL; + char *default_sl_dup = NULL; + int32_t entry_count = 0; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + frame = myframe; + + GF_ASSERT(frame->local); + + local = frame->local; + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(frame->this->name, GF_LOG_ERROR, XDR_DECODE_FAIL); + goto out; + } + + if (rsp.op_ret) { + ret = -1; + if (global_state->mode & GLUSTER_MODE_XML) + goto xml_output; + + if (strcmp(rsp.op_errstr, "")) { + cli_err("quota command failed : %s", rsp.op_errstr); + if (rsp.op_ret == -ENOENT) + cli_err("please enter the path relative to the volume"); + } else { + cli_err("quota command : failed"); + } + + goto out; + } + + if (rsp.dict.dict_len) { + /* Unserialize the dictionary */ + dict = dict_new(); + + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, &dict); + if (ret < 0) { + gf_log("cli", GF_LOG_ERROR, DICT_UNSERIALIZE_FAIL); + goto out; + } + } + + gf_log("cli", GF_LOG_DEBUG, "Received resp to quota command"); + + ret = dict_get_str_sizen(dict, "default-soft-limit", &default_sl); + if (ret) + gf_log(frame->this->name, GF_LOG_TRACE, + "failed to get default soft limit"); + + // default-soft-limit is part of rsp_dict only iff we sent + // GLUSTER_CLI_QUOTA with type being GF_QUOTA_OPTION_TYPE_LIST + if (default_sl) { + default_sl_dup = gf_strdup(default_sl); + if (!default_sl_dup) { + ret = -1; + goto out; + } + ret = dict_set_dynstr_sizen(local->dict, "default-soft-limit", + default_sl_dup); + if (ret) { + gf_log(frame->this->name, GF_LOG_TRACE, + "failed to set default soft limit"); + GF_FREE(default_sl_dup); + } + } + + ret = dict_get_str_sizen(dict, "volname", &volname); + if (ret) + gf_log(frame->this->name, GF_LOG_ERROR, "failed to get volname"); + + ret = dict_get_int32_sizen(dict, "type", &type); + if (ret) + gf_log(frame->this->name, GF_LOG_TRACE, "failed to get type"); + + ret = dict_get_int32_sizen(dict, "count", &entry_count); + if (ret) + gf_log(frame->this->name, GF_LOG_TRACE, "failed to get count"); + + if ((type == GF_QUOTA_OPTION_TYPE_LIST) || + (type == GF_QUOTA_OPTION_TYPE_LIST_OBJECTS)) { + gf_cli_quota_list(local, volname, dict, default_sl, entry_count, + rsp.op_ret, rsp.op_errno, rsp.op_errstr); + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_vol_quota_limit_list_end(local); + if (ret < 0) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + } + goto out; + } + } + +xml_output: + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_str("volQuota", NULL, rsp.op_ret, rsp.op_errno, + rsp.op_errstr); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + + if (!rsp.op_ret && type != GF_QUOTA_OPTION_TYPE_LIST && + type != GF_QUOTA_OPTION_TYPE_LIST_OBJECTS) + cli_out("volume quota : success"); + + ret = rsp.op_ret; +out: + + if ((type == GF_QUOTA_OPTION_TYPE_LIST) || + (type == GF_QUOTA_OPTION_TYPE_LIST_OBJECTS)) { + gluster_remove_auxiliary_mount(volname); + } + + cli_cmd_broadcast_response(ret); + if (dict) + dict_unref(dict); + + gf_free_xdr_cli_rsp(rsp); + + return ret; +} + +static int +gf_cli_getspec_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_getspec_rsp rsp = { + 0, + }; + int ret = -1; + char *spec = NULL; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_getspec_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + goto out; + } + + if (rsp.op_ret == -1) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + "getspec failed"); + ret = -1; + goto out; + } + + gf_log("cli", GF_LOG_INFO, "Received resp to getspec"); + + spec = GF_MALLOC(rsp.op_ret + 1, cli_mt_char); + if (!spec) { + gf_log("", GF_LOG_ERROR, "out of memory"); + ret = -1; + goto out; + } + memcpy(spec, rsp.spec, rsp.op_ret); + spec[rsp.op_ret] = '\0'; + cli_out("%s", spec); + GF_FREE(spec); + + ret = 0; + +out: + cli_cmd_broadcast_response(ret); + gf_free_xdr_getspec_rsp(rsp); + return ret; +} + +static int +gf_cli_pmap_b2p_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + pmap_port_by_brick_rsp rsp = { + 0, + }; + int ret = -1; + char *spec = NULL; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_pmap_port_by_brick_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + goto out; + } + + if (rsp.op_ret == -1) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + "pump_b2p failed"); + ret = -1; + goto out; + } + + gf_log("cli", GF_LOG_INFO, "Received resp to pmap b2p"); + + cli_out("%d", rsp.port); + GF_FREE(spec); + + ret = rsp.op_ret; + +out: + cli_cmd_broadcast_response(ret); + return ret; +} + +static int32_t +gf_cli_probe(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = { + { + 0, + }, + }; + int ret = 0; + dict_t *dict = NULL; + int port = 0; + + if (!frame || !this || !data) { + ret = -1; + goto out; + } + + dict = data; + + ret = dict_get_int32_sizen(dict, "port", &port); + if (ret) { + ret = dict_set_int32_sizen(dict, "port", CLI_GLUSTERD_PORT); + if (ret) + goto out; + } + + ret = cli_to_glusterd(&req, frame, gf_cli_probe_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, GLUSTER_CLI_PROBE, + this, cli_rpc_prog, NULL); + +out: + GF_FREE(req.dict.dict_val); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + + return ret; +} + +static int32_t +gf_cli_deprobe(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = { + { + 0, + }, + }; + int ret = 0; + dict_t *dict = NULL; + int port = 0; + int flags = 0; + + if (!frame || !this || !data) { + ret = -1; + goto out; + } + + dict = data; + ret = dict_get_int32_sizen(dict, "port", &port); + if (ret) { + ret = dict_set_int32_sizen(dict, "port", CLI_GLUSTERD_PORT); + if (ret) + goto out; + } + + ret = dict_get_int32_sizen(dict, "flags", &flags); + if (ret) { + ret = dict_set_int32_sizen(dict, "flags", 0); + if (ret) + goto out; + } + + ret = cli_to_glusterd(&req, frame, gf_cli_deprobe_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, GLUSTER_CLI_DEPROBE, + this, cli_rpc_prog, NULL); + +out: + GF_FREE(req.dict.dict_val); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + + return ret; +} + +static int32_t +gf_cli_list_friends(call_frame_t *frame, xlator_t *this, void *data) +{ + gf1_cli_peer_list_req req = { + 0, + }; + int ret = 0; + unsigned long flags = 0; + + if (!frame || !this) { + ret = -1; + goto out; + } + + GF_ASSERT(frame->local == NULL); + + flags = (long)data; + req.flags = flags; + frame->local = (void *)flags; + ret = cli_cmd_submit( + NULL, &req, frame, cli_rpc_prog, GLUSTER_CLI_LIST_FRIENDS, NULL, this, + gf_cli_list_friends_cbk, (xdrproc_t)xdr_gf1_cli_peer_list_req); + +out: + if (ret && frame) { + /* + * If everything goes fine, gf_cli_list_friends_cbk() + * [invoked through cli_cmd_submit()]resets the + * frame->local to NULL. In case cli_cmd_submit() + * fails in between, RESET frame->local here. + */ + frame->local = NULL; + } + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + return ret; +} + +static int32_t +gf_cli_get_state(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = { + { + 0, + }, + }; + int ret = 0; + dict_t *dict = NULL; + + dict = data; + + ret = cli_to_glusterd(&req, frame, gf_cli_get_state_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, + GLUSTER_CLI_GET_STATE, this, cli_rpc_prog, NULL); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + GF_FREE(req.dict.dict_val); + + return ret; +} + +static int32_t +gf_cli_get_next_volume(call_frame_t *frame, xlator_t *this, void *data) +{ + int ret = 0; + cli_cmd_volume_get_ctx_t *ctx = NULL; + cli_local_t *local = NULL; + + if (!frame || !this || !data) { + ret = -1; + goto out; + } + + ctx = data; + local = frame->local; + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_vol_info_begin(local, 0, 0, ""); + if (ret) { + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + } + + ret = gf_cli_get_volume(frame, this, data); + + if (!local || !local->get_vol.volname) { + if ((global_state->mode & GLUSTER_MODE_XML)) + goto end_xml; + + cli_err("No volumes present"); + goto out; + } + + ctx->volname = local->get_vol.volname; + + while (ctx->volname) { + ret = gf_cli_get_volume(frame, this, ctx); + if (ret) + goto out; + ctx->volname = local->get_vol.volname; + } + +end_xml: + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_vol_info_end(local); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + } + +out: + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + return ret; +} + +static int32_t +gf_cli_get_volume(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + int ret = 0; + cli_cmd_volume_get_ctx_t *ctx = NULL; + dict_t *dict = NULL; + int32_t flags = 0; + + if (!this || !data) { + ret = -1; + goto out; + } + + ctx = data; + + dict = dict_new(); + if (!dict) { + gf_log(THIS->name, GF_LOG_ERROR, "Failed to create the dict"); + ret = -1; + goto out; + } + + if (ctx->volname) { + ret = dict_set_str_sizen(dict, "volname", ctx->volname); + if (ret) + goto out; + } + + flags = ctx->flags; + ret = dict_set_int32_sizen(dict, "flags", flags); + if (ret) { + gf_log(frame->this->name, GF_LOG_ERROR, "failed to set flags"); + goto out; + } + + ret = dict_allocate_and_serialize(dict, &req.dict.dict_val, + &req.dict.dict_len); + if (ret) { + gf_log(frame->this->name, GF_LOG_ERROR, DICT_SERIALIZE_FAIL); + goto out; + } + + ret = cli_cmd_submit(NULL, &req, frame, cli_rpc_prog, + GLUSTER_CLI_GET_VOLUME, NULL, this, + gf_cli_get_volume_cbk, (xdrproc_t)xdr_gf_cli_req); + +out: + if (dict) + dict_unref(dict); + + GF_FREE(req.dict.dict_val); + + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + return ret; +} + +static int32_t +gf_cli3_1_uuid_get(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + int ret = 0; + dict_t *dict = NULL; + + dict = data; + ret = cli_to_glusterd(&req, frame, gf_cli3_1_uuid_get_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, GLUSTER_CLI_UUID_GET, + this, cli_rpc_prog, NULL); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + GF_FREE(req.dict.dict_val); + return ret; +} + +static int32_t +gf_cli3_1_uuid_reset(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + int ret = 0; + dict_t *dict = NULL; + + dict = data; + ret = cli_to_glusterd(&req, frame, gf_cli3_1_uuid_reset_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, + GLUSTER_CLI_UUID_RESET, this, cli_rpc_prog, NULL); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + GF_FREE(req.dict.dict_val); + return ret; +} + +static int32_t +gf_cli_create_volume(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + int ret = 0; + dict_t *dict = NULL; + + dict = data; + + ret = cli_to_glusterd(&req, frame, gf_cli_create_volume_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, + GLUSTER_CLI_CREATE_VOLUME, this, cli_rpc_prog, NULL); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + + GF_FREE(req.dict.dict_val); + + return ret; +} + +static int32_t +gf_cli_delete_volume(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + int ret = 0; + dict_t *dict = NULL; + + dict = data; + + ret = cli_to_glusterd(&req, frame, gf_cli_delete_volume_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, + GLUSTER_CLI_DELETE_VOLUME, this, cli_rpc_prog, NULL); + GF_FREE(req.dict.dict_val); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + + return ret; +} + +static int32_t +gf_cli_start_volume(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + int ret = 0; + dict_t *dict = NULL; + + dict = data; + + ret = cli_to_glusterd(&req, frame, gf_cli_start_volume_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, + GLUSTER_CLI_START_VOLUME, this, cli_rpc_prog, NULL); + + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + GF_FREE(req.dict.dict_val); + + return ret; +} + +static int32_t +gf_cli_stop_volume(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + int ret = 0; + dict_t *dict = data; + + dict = data; + + ret = cli_to_glusterd(&req, frame, gf_cli_stop_volume_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, + GLUSTER_CLI_STOP_VOLUME, this, cli_rpc_prog, NULL); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + GF_FREE(req.dict.dict_val); + + return ret; +} + +static int32_t +gf_cli_defrag_volume(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + int ret = 0; + dict_t *dict = NULL; + + dict = data; + + ret = cli_to_glusterd(&req, frame, gf_cli_defrag_volume_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, + GLUSTER_CLI_DEFRAG_VOLUME, this, cli_rpc_prog, NULL); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + GF_FREE(req.dict.dict_val); + + return ret; +} + +static int32_t +gf_cli_rename_volume(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + int ret = 0; + dict_t *dict = NULL; + + if (!frame || !this || !data) { + ret = -1; + goto out; + } + + dict = data; + + ret = dict_allocate_and_serialize(dict, &req.dict.dict_val, + &req.dict.dict_len); + if (ret < 0) { + gf_log(this->name, GF_LOG_ERROR, DICT_SERIALIZE_FAIL); + + goto out; + } + + ret = cli_cmd_submit(NULL, &req, frame, cli_rpc_prog, + GLUSTER_CLI_RENAME_VOLUME, NULL, this, + gf_cli_rename_volume_cbk, (xdrproc_t)xdr_gf_cli_req); + +out: + GF_FREE(req.dict.dict_val); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + + return ret; +} + +static int32_t +gf_cli_reset_volume(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + int ret = 0; + dict_t *dict = NULL; + + dict = data; + + ret = cli_to_glusterd(&req, frame, gf_cli_reset_volume_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, + GLUSTER_CLI_RESET_VOLUME, this, cli_rpc_prog, NULL); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + GF_FREE(req.dict.dict_val); + return ret; +} + +static int32_t +gf_cli_ganesha(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + int ret = 0; + dict_t *dict = NULL; + + dict = data; + + ret = cli_to_glusterd(&req, frame, gf_cli_ganesha_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, GLUSTER_CLI_GANESHA, + this, cli_rpc_prog, NULL); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + + return ret; +} + +static int32_t +gf_cli_set_volume(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + int ret = 0; + dict_t *dict = NULL; + + dict = data; + + ret = cli_to_glusterd(&req, frame, gf_cli_set_volume_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, + GLUSTER_CLI_SET_VOLUME, this, cli_rpc_prog, NULL); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + GF_FREE(req.dict.dict_val); + + return ret; +} + +int32_t +gf_cli_add_brick(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + int ret = 0; + dict_t *dict = NULL; + + dict = data; + + ret = cli_to_glusterd(&req, frame, gf_cli_add_brick_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, + GLUSTER_CLI_ADD_BRICK, this, cli_rpc_prog, NULL); + + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + + GF_FREE(req.dict.dict_val); + + return ret; +} + +static int32_t +gf_cli_remove_brick(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + ; + gf_cli_req status_req = {{ + 0, + }}; + ; + int ret = 0; + dict_t *dict = NULL; + int32_t command = 0; + int32_t cmd = 0; + + if (!frame || !this) { + ret = -1; + goto out; + } + + dict = data; + + ret = dict_get_int32_sizen(dict, "command", &command); + if (ret) + goto out; + + if ((command != GF_OP_CMD_STATUS) && (command != GF_OP_CMD_STOP)) { + ret = cli_to_glusterd( + &req, frame, gf_cli_remove_brick_cbk, (xdrproc_t)xdr_gf_cli_req, + dict, GLUSTER_CLI_REMOVE_BRICK, this, cli_rpc_prog, NULL); + } else { + /* Need rebalance status to be sent :-) */ + if (command == GF_OP_CMD_STATUS) + cmd |= GF_DEFRAG_CMD_STATUS; + else + cmd |= GF_DEFRAG_CMD_STOP; + + ret = dict_set_int32_sizen(dict, "rebalance-command", (int32_t)cmd); + if (ret) { + gf_log(this->name, GF_LOG_ERROR, "Failed to set dict"); + goto out; + } + + ret = cli_to_glusterd( + &status_req, frame, gf_cli3_remove_brick_status_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, GLUSTER_CLI_DEFRAG_VOLUME, this, + cli_rpc_prog, NULL); + } + +out: + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + + GF_FREE(req.dict.dict_val); + + GF_FREE(status_req.dict.dict_val); + + return ret; +} + +static int32_t +gf_cli_reset_brick(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + int ret = 0; + dict_t *dict = NULL; + char *dst_brick = NULL; + char *op = NULL; + + if (!frame || !this || !data) { + ret = -1; + goto out; + } + + dict = data; + + ret = dict_get_str_sizen(dict, "operation", &op); + if (ret) { + gf_log(this->name, GF_LOG_DEBUG, "dict_get on operation failed"); + goto out; + } + + if (!strcmp(op, "GF_RESET_OP_COMMIT") || + !strcmp(op, "GF_RESET_OP_COMMIT_FORCE")) { + ret = dict_get_str_sizen(dict, "dst-brick", &dst_brick); + if (ret) { + gf_log(this->name, GF_LOG_DEBUG, "dict_get on dst-brick failed"); + goto out; + } + } + + ret = cli_to_glusterd(&req, frame, gf_cli_reset_brick_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, + GLUSTER_CLI_RESET_BRICK, this, cli_rpc_prog, NULL); + +out: + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + + GF_FREE(req.dict.dict_val); + + return ret; +} + +static int32_t +gf_cli_replace_brick(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + int ret = 0; + dict_t *dict = NULL; + int32_t op = 0; + + if (!frame || !this || !data) { + ret = -1; + goto out; + } + + dict = data; + + ret = dict_get_int32_sizen(dict, "operation", &op); + if (ret) { + gf_log(this->name, GF_LOG_DEBUG, "dict_get on operation failed"); + goto out; + } + + ret = cli_to_glusterd(&req, frame, gf_cli_replace_brick_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, + GLUSTER_CLI_REPLACE_BRICK, this, cli_rpc_prog, NULL); + +out: + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + + GF_FREE(req.dict.dict_val); + + return ret; +} + +static int32_t +gf_cli_log_rotate(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + int ret = 0; + dict_t *dict = NULL; + + dict = data; + + ret = cli_to_glusterd(&req, frame, gf_cli_log_rotate_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, + GLUSTER_CLI_LOG_ROTATE, this, cli_rpc_prog, NULL); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + + GF_FREE(req.dict.dict_val); + return ret; +} + +static int32_t +gf_cli_sync_volume(call_frame_t *frame, xlator_t *this, void *data) +{ + int ret = 0; + gf_cli_req req = {{ + 0, + }}; + dict_t *dict = NULL; + + dict = data; + + ret = cli_to_glusterd(&req, frame, gf_cli_sync_volume_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, + GLUSTER_CLI_SYNC_VOLUME, this, cli_rpc_prog, NULL); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + GF_FREE(req.dict.dict_val); + + return ret; +} + +static int32_t +gf_cli_getspec(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_getspec_req req = { + 0, + }; + int ret = 0; + dict_t *dict = NULL; + dict_t *op_dict = NULL; + + if (!frame || !this) { + ret = -1; + goto out; + } + + dict = data; + + ret = dict_get_str_sizen(dict, "volid", &req.key); + if (ret) + goto out; + + op_dict = dict_new(); + if (!op_dict) { + ret = -1; + goto out; + } + + // Set the supported min and max op-versions, so glusterd can make a + // decision + ret = dict_set_int32_sizen(op_dict, "min-op-version", GD_OP_VERSION_MIN); + if (ret) { + gf_log(THIS->name, GF_LOG_ERROR, + "Failed to set min-op-version in request dict"); + goto out; + } + + ret = dict_set_int32_sizen(op_dict, "max-op-version", GD_OP_VERSION_MAX); + if (ret) { + gf_log(THIS->name, GF_LOG_ERROR, + "Failed to set max-op-version in request dict"); + goto out; + } + + ret = dict_allocate_and_serialize(op_dict, &req.xdata.xdata_val, + &req.xdata.xdata_len); + if (ret < 0) { + gf_log(THIS->name, GF_LOG_ERROR, DICT_SERIALIZE_FAIL); + goto out; + } + + ret = cli_cmd_submit(NULL, &req, frame, &cli_handshake_prog, + GF_HNDSK_GETSPEC, NULL, this, gf_cli_getspec_cbk, + (xdrproc_t)xdr_gf_getspec_req); + +out: + if (op_dict) { + dict_unref(op_dict); + } + GF_FREE(req.xdata.xdata_val); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + + return ret; +} + +static int32_t +gf_cli_quota(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + int ret = 0; + dict_t *dict = NULL; + + dict = data; + + ret = cli_to_glusterd(&req, frame, gf_cli_quota_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, GLUSTER_CLI_QUOTA, + this, cli_rpc_prog, NULL); + GF_FREE(req.dict.dict_val); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + return ret; +} + +static int32_t +gf_cli_pmap_b2p(call_frame_t *frame, xlator_t *this, void *data) +{ + pmap_port_by_brick_req req = { + 0, + }; + int ret = 0; + dict_t *dict = NULL; + + if (!frame || !this) { + ret = -1; + goto out; + } + + dict = data; + + ret = dict_get_str_sizen(dict, "brick", &req.brick); + if (ret) + goto out; + + ret = cli_cmd_submit(NULL, &req, frame, &cli_pmap_prog, GF_PMAP_PORTBYBRICK, + NULL, this, gf_cli_pmap_b2p_cbk, + (xdrproc_t)xdr_pmap_port_by_brick_req); + +out: + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + + return ret; +} + +static int +gf_cli_fsm_log_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf1_cli_fsm_log_rsp rsp = { + 0, + }; + int ret = -1; + dict_t *dict = NULL; + int tr_count = 0; + char key[64] = {0}; + int keylen; + int i = 0; + char *old_state = NULL; + char *new_state = NULL; + char *event = NULL; + char *time = NULL; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf1_cli_fsm_log_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + goto out; + } + + if (rsp.op_ret) { + if (strcmp(rsp.op_errstr, "")) + cli_err("%s", rsp.op_errstr); + cli_err("fsm log unsuccessful"); + ret = rsp.op_ret; + goto out; + } + + dict = dict_new(); + if (!dict) { + ret = -1; + goto out; + } + + ret = dict_unserialize(rsp.fsm_log.fsm_log_val, rsp.fsm_log.fsm_log_len, + &dict); + + if (ret) { + cli_err(DICT_UNSERIALIZE_FAIL); + goto out; + } + + ret = dict_get_int32_sizen(dict, "count", &tr_count); + if (!ret && tr_count) + cli_out("number of transitions: %d", tr_count); + else + cli_err("No transitions"); + for (i = 0; i < tr_count; i++) { + keylen = snprintf(key, sizeof(key), "log%d-old-state", i); + ret = dict_get_strn(dict, key, keylen, &old_state); + if (ret) + goto out; + + keylen = snprintf(key, sizeof(key), "log%d-event", i); + ret = dict_get_strn(dict, key, keylen, &event); + if (ret) + goto out; + + keylen = snprintf(key, sizeof(key), "log%d-new-state", i); + ret = dict_get_strn(dict, key, keylen, &new_state); + if (ret) + goto out; + + keylen = snprintf(key, sizeof(key), "log%d-time", i); + ret = dict_get_strn(dict, key, keylen, &time); + if (ret) + goto out; + cli_out( + "Old State: [%s]\n" + "New State: [%s]\n" + "Event : [%s]\n" + "timestamp: [%s]\n", + old_state, new_state, event, time); + } + + ret = rsp.op_ret; + +out: + cli_cmd_broadcast_response(ret); + if (dict) { + dict_unref(dict); + } + gf_free_xdr_fsm_log_rsp(rsp); + + return ret; +} + +static int32_t +gf_cli_fsm_log(call_frame_t *frame, xlator_t *this, void *data) +{ + int ret = -1; + gf1_cli_fsm_log_req req = { + 0, + }; + + GF_ASSERT(frame); + GF_ASSERT(this); + GF_ASSERT(data); + + if (!frame || !this || !data) + goto out; + req.name = data; + ret = cli_cmd_submit(NULL, &req, frame, cli_rpc_prog, GLUSTER_CLI_FSM_LOG, + NULL, this, gf_cli_fsm_log_cbk, + (xdrproc_t)xdr_gf1_cli_fsm_log_req); + +out: + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + + return ret; +} + +static int +gf_cli_gsync_config_command(dict_t *dict) +{ + runner_t runner = { + 0, + }; + char *subop = NULL; + char *gwd = NULL; + char *slave = NULL; + char *confpath = NULL; + char *master = NULL; + char *op_name = NULL; + int ret = -1; + char conf_path[PATH_MAX] = ""; + + if (dict_get_str_sizen(dict, "subop", &subop) != 0) + return -1; + + if (strcmp(subop, "get") != 0 && strcmp(subop, "get-all") != 0) { + cli_out(GEOREP " config updated successfully"); + return 0; + } + + if (dict_get_str_sizen(dict, "glusterd_workdir", &gwd) != 0 || + dict_get_str_sizen(dict, "slave", &slave) != 0) + return -1; + + if (dict_get_str_sizen(dict, "master", &master) != 0) + master = NULL; + if (dict_get_str_sizen(dict, "op_name", &op_name) != 0) + op_name = NULL; + + ret = dict_get_str_sizen(dict, "conf_path", &confpath); + if (ret || !confpath) { + ret = snprintf(conf_path, sizeof(conf_path) - 1, + "%s/" GEOREP "/gsyncd_template.conf", gwd); + conf_path[ret] = '\0'; + confpath = conf_path; + } + + runinit(&runner); + runner_add_args(&runner, GSYNCD_PREFIX "/gsyncd", "-c", NULL); + runner_argprintf(&runner, "%s", confpath); + runner_argprintf(&runner, "--iprefix=%s", DATADIR); + if (master) + runner_argprintf(&runner, ":%s", master); + runner_add_arg(&runner, slave); + runner_argprintf(&runner, "--config-%s", subop); + if (op_name) + runner_add_arg(&runner, op_name); + + return runner_run(&runner); +} + +static int +gf_cli_print_status(char **title_values, gf_gsync_status_t **sts_vals, + int *spacing, int gsync_count, int number_of_fields, + int is_detail) +{ + int i = 0; + int j = 0; + int ret = 0; + int status_fields = 8; /* Indexed at 0 */ + int total_spacing = 0; + char **output_values = NULL; + char *tmp = NULL; + char *hyphens = NULL; + + /* calculating spacing for hyphens */ + for (i = 0; i < number_of_fields; i++) { + /* Suppressing detail output for status */ + if ((!is_detail) && (i > status_fields)) { + /* Suppressing detailed output for + * status */ + continue; + } + spacing[i] += 3; /* Adding extra space to + distinguish between fields */ + total_spacing += spacing[i]; + } + total_spacing += 4; /* For the spacing between the fields */ + + /* char pointers for each field */ + output_values = GF_MALLOC(number_of_fields * sizeof(char *), + gf_common_mt_char); + if (!output_values) { + ret = -1; + goto out; + } + for (i = 0; i < number_of_fields; i++) { + output_values[i] = GF_CALLOC(spacing[i] + 1, sizeof(char), + gf_common_mt_char); + if (!output_values[i]) { + ret = -1; + goto out; + } + } + + cli_out(" "); + + /* setting the title "NODE", "MASTER", etc. from title_values[] + and printing the same */ + for (j = 0; j < number_of_fields; j++) { + if ((!is_detail) && (j > status_fields)) { + /* Suppressing detailed output for + * status */ + output_values[j][0] = '\0'; + continue; + } + memset(output_values[j], ' ', spacing[j]); + memcpy(output_values[j], title_values[j], strlen(title_values[j])); + output_values[j][spacing[j]] = '\0'; + } + cli_out("%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s", output_values[0], + output_values[1], output_values[2], output_values[3], + output_values[4], output_values[5], output_values[6], + output_values[7], output_values[8], output_values[9], + output_values[10], output_values[11], output_values[12], + output_values[13], output_values[14], output_values[15]); + + hyphens = GF_MALLOC((total_spacing + 1) * sizeof(char), gf_common_mt_char); + if (!hyphens) { + ret = -1; + goto out; + } + + /* setting and printing the hyphens */ + memset(hyphens, '-', total_spacing); + hyphens[total_spacing] = '\0'; + cli_out("%s", hyphens); + + for (i = 0; i < gsync_count; i++) { + for (j = 0; j < number_of_fields; j++) { + if ((!is_detail) && (j > status_fields)) { + /* Suppressing detailed output for + * status */ + output_values[j][0] = '\0'; + continue; + } + tmp = get_struct_variable(j, sts_vals[i]); + if (!tmp) { + gf_log("", GF_LOG_ERROR, "struct member empty."); + ret = -1; + goto out; + } + memset(output_values[j], ' ', spacing[j]); + memcpy(output_values[j], tmp, strlen(tmp)); + output_values[j][spacing[j]] = '\0'; + } + + cli_out("%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s", + output_values[0], output_values[1], output_values[2], + output_values[3], output_values[4], output_values[5], + output_values[6], output_values[7], output_values[8], + output_values[9], output_values[10], output_values[11], + output_values[12], output_values[13], output_values[14], + output_values[15]); + } + +out: + if (output_values) { + for (i = 0; i < number_of_fields; i++) { + if (output_values[i]) + GF_FREE(output_values[i]); + } + GF_FREE(output_values); + } + + if (hyphens) + GF_FREE(hyphens); + + return ret; +} + +int +gf_gsync_status_t_comparator(const void *p, const void *q) +{ + char *slavekey1 = NULL; + char *slavekey2 = NULL; + + slavekey1 = get_struct_variable(20, (*(gf_gsync_status_t **)p)); + slavekey2 = get_struct_variable(20, (*(gf_gsync_status_t **)q)); + if (!slavekey1 || !slavekey2) { + gf_log("cli", GF_LOG_ERROR, "struct member empty."); + return 0; + } + + return strcmp(slavekey1, slavekey2); +} + +static int +gf_cli_read_status_data(dict_t *dict, gf_gsync_status_t **sts_vals, + int *spacing, int gsync_count, int number_of_fields) +{ + char *tmp = NULL; + char sts_val_name[PATH_MAX] = ""; + int ret = 0; + int i = 0; + int j = 0; + + /* Storing per node status info in each object */ + for (i = 0; i < gsync_count; i++) { + snprintf(sts_val_name, sizeof(sts_val_name), "status_value%d", i); + + /* Fetching the values from dict, and calculating + the max length for each field */ + ret = dict_get_bin(dict, sts_val_name, (void **)&(sts_vals[i])); + if (ret) + goto out; + + for (j = 0; j < number_of_fields; j++) { + tmp = get_struct_variable(j, sts_vals[i]); + if (!tmp) { + gf_log("", GF_LOG_ERROR, "struct member empty."); + ret = -1; + goto out; + } + if (strlen(tmp) > spacing[j]) + spacing[j] = strlen(tmp); + } + } + + /* Sort based on Session Slave */ + qsort(sts_vals, gsync_count, sizeof(gf_gsync_status_t *), + gf_gsync_status_t_comparator); + +out: + return ret; +} + +static int +gf_cli_gsync_status_output(dict_t *dict, gf_boolean_t is_detail) +{ + int gsync_count = 0; + int i = 0; + int ret = 0; + int spacing[16] = {0}; + int num_of_fields = 16; + char errmsg[1024] = ""; + char *master = NULL; + char *slave = NULL; + static char *title_values[] = {"MASTER NODE", + "MASTER VOL", + "MASTER BRICK", + "SLAVE USER", + "SLAVE", + "SLAVE NODE", + "STATUS", + "CRAWL STATUS", + "LAST_SYNCED", + "ENTRY", + "DATA", + "META", + "FAILURES", + "CHECKPOINT TIME", + "CHECKPOINT COMPLETED", + "CHECKPOINT COMPLETION TIME"}; + gf_gsync_status_t **sts_vals = NULL; + + /* Checks if any session is active or not */ + ret = dict_get_int32_sizen(dict, "gsync-count", &gsync_count); + if (ret) { + ret = dict_get_str_sizen(dict, "master", &master); + + ret = dict_get_str_sizen(dict, "slave", &slave); + + if (master) { + if (slave) + snprintf(errmsg, sizeof(errmsg), + "No active geo-replication sessions between %s" + " and %s", + master, slave); + else + snprintf(errmsg, sizeof(errmsg), + "No active geo-replication sessions for %s", master); + } else + snprintf(errmsg, sizeof(errmsg), + "No active geo-replication sessions"); + + gf_log("cli", GF_LOG_INFO, "%s", errmsg); + cli_out("%s", errmsg); + ret = -1; + goto out; + } + + for (i = 0; i < num_of_fields; i++) + spacing[i] = strlen(title_values[i]); + + /* gsync_count = number of nodes reporting output. + each sts_val object will store output of each + node */ + sts_vals = GF_MALLOC(gsync_count * sizeof(gf_gsync_status_t *), + gf_common_mt_char); + if (!sts_vals) { + ret = -1; + goto out; + } + + ret = gf_cli_read_status_data(dict, sts_vals, spacing, gsync_count, + num_of_fields); + if (ret) { + gf_log("", GF_LOG_ERROR, "Unable to read status data"); + goto out; + } + + ret = gf_cli_print_status(title_values, sts_vals, spacing, gsync_count, + num_of_fields, is_detail); + if (ret) { + gf_log("", GF_LOG_ERROR, "Unable to print status output"); + goto out; + } + +out: + if (sts_vals) + GF_FREE(sts_vals); + + return ret; +} + +static int32_t +write_contents_to_common_pem_file(dict_t *dict, int output_count) +{ + char *workdir = NULL; + char common_pem_file[PATH_MAX] = ""; + char *output = NULL; + char output_name[32] = ""; + int bytes_written = 0; + int fd = -1; + int ret = -1; + int i = -1; + + ret = dict_get_str_sizen(dict, "glusterd_workdir", &workdir); + if (ret || !workdir) { + gf_log("", GF_LOG_ERROR, "Unable to fetch workdir"); + ret = -1; + goto out; + } + + snprintf(common_pem_file, sizeof(common_pem_file), + "%s/geo-replication/common_secret.pem.pub", workdir); + + sys_unlink(common_pem_file); + + fd = open(common_pem_file, O_WRONLY | O_CREAT, 0600); + if (fd == -1) { + gf_log("", GF_LOG_ERROR, "Failed to open %s Error : %s", + common_pem_file, strerror(errno)); + ret = -1; + goto out; + } + + for (i = 1; i <= output_count; i++) { + ret = snprintf(output_name, sizeof(output_name), "output_%d", i); + ret = dict_get_strn(dict, output_name, ret, &output); + if (ret) { + gf_log("", GF_LOG_ERROR, "Failed to get %s.", output_name); + cli_out("Unable to fetch output."); + } + if (output) { + bytes_written = sys_write(fd, output, strlen(output)); + if (bytes_written != strlen(output)) { + gf_log("", GF_LOG_ERROR, "Failed to write to %s", + common_pem_file); + ret = -1; + goto out; + } + /* Adding the new line character */ + bytes_written = sys_write(fd, "\n", 1); + if (bytes_written != 1) { + gf_log("", GF_LOG_ERROR, "Failed to add new line char"); + ret = -1; + goto out; + } + output = NULL; + } + } + + cli_out("Common secret pub file present at %s", common_pem_file); + ret = 0; +out: + if (fd >= 0) + sys_close(fd); + + gf_log("", GF_LOG_DEBUG, RETURNING, ret); + return ret; +} + +static int +gf_cli_sys_exec_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + int ret = -1; + int output_count = -1; + int i = -1; + char *output = NULL; + char *command = NULL; + char output_name[32] = ""; + gf_cli_rsp rsp = { + 0, + }; + dict_t *dict = NULL; + + GF_ASSERT(myframe); + + if (req->rpc_status == -1) { + goto out; + } + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + goto out; + } + + dict = dict_new(); + + if (!dict) { + ret = -1; + goto out; + } + + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, &dict); + + if (ret) + goto out; + + if (rsp.op_ret) { + cli_err("%s", rsp.op_errstr ? rsp.op_errstr : "Command failed."); + ret = rsp.op_ret; + goto out; + } + + ret = dict_get_int32_sizen(dict, "output_count", &output_count); + if (ret) { + cli_out("Command executed successfully."); + ret = 0; + goto out; + } + + ret = dict_get_str_sizen(dict, "command", &command); + if (ret) { + gf_log("", GF_LOG_ERROR, "Unable to get command from dict"); + goto out; + } + + if (!strcmp(command, "gsec_create")) { + ret = write_contents_to_common_pem_file(dict, output_count); + if (!ret) + goto out; + } + + for (i = 1; i <= output_count; i++) { + ret = snprintf(output_name, sizeof(output_name), "output_%d", i); + ret = dict_get_strn(dict, output_name, ret, &output); + if (ret) { + gf_log("", GF_LOG_ERROR, "Failed to get %s.", output_name); + cli_out("Unable to fetch output."); + } + if (output) { + cli_out("%s", output); + output = NULL; + } + } + + ret = 0; +out: + if (dict) + dict_unref(dict); + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + return ret; +} + +static int +gf_cli_copy_file_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + int ret = -1; + gf_cli_rsp rsp = { + 0, + }; + dict_t *dict = NULL; + + GF_ASSERT(myframe); + + if (req->rpc_status == -1) { + goto out; + } + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + goto out; + } + + dict = dict_new(); + + if (!dict) { + ret = -1; + goto out; + } + + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, &dict); + + if (ret) + goto out; + + if (rsp.op_ret) { + cli_err("%s", rsp.op_errstr ? rsp.op_errstr : "Copy unsuccessful"); + ret = rsp.op_ret; + goto out; + } + + cli_out("Successfully copied file."); + +out: + if (dict) + dict_unref(dict); + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + return ret; +} + +static int +gf_cli_gsync_set_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + int ret = -1; + gf_cli_rsp rsp = { + 0, + }; + dict_t *dict = NULL; + char *gsync_status = NULL; + char *master = NULL; + char *slave = NULL; + int32_t type = 0; + gf_boolean_t status_detail = _gf_false; + + GF_ASSERT(myframe); + + if (req->rpc_status == -1) { + goto out; + } + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + goto out; + } + + dict = dict_new(); + + if (!dict) { + ret = -1; + goto out; + } + + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, &dict); + + if (ret) + goto out; + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_vol_gsync(dict, rsp.op_ret, rsp.op_errno, + rsp.op_errstr); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + + if (rsp.op_ret) { + cli_err("%s", + rsp.op_errstr ? rsp.op_errstr : GEOREP " command unsuccessful"); + ret = rsp.op_ret; + goto out; + } + + ret = dict_get_str_sizen(dict, "gsync-status", &gsync_status); + if (!ret) + cli_out("%s", gsync_status); + + ret = dict_get_int32_sizen(dict, "type", &type); + if (ret) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + "failed to get type"); + goto out; + } + + switch (type) { + case GF_GSYNC_OPTION_TYPE_START: + case GF_GSYNC_OPTION_TYPE_STOP: + if (dict_get_str_sizen(dict, "master", &master) != 0) + master = "???"; + if (dict_get_str_sizen(dict, "slave", &slave) != 0) + slave = "???"; + + cli_out( + "%s " GEOREP " session between %s & %s has been successful", + type == GF_GSYNC_OPTION_TYPE_START ? "Starting" : "Stopping", + master, slave); + break; + + case GF_GSYNC_OPTION_TYPE_PAUSE: + case GF_GSYNC_OPTION_TYPE_RESUME: + if (dict_get_str_sizen(dict, "master", &master) != 0) + master = "???"; + if (dict_get_str_sizen(dict, "slave", &slave) != 0) + slave = "???"; + + cli_out("%s " GEOREP " session between %s & %s has been successful", + type == GF_GSYNC_OPTION_TYPE_PAUSE ? "Pausing" : "Resuming", + master, slave); + break; + + case GF_GSYNC_OPTION_TYPE_CONFIG: + ret = gf_cli_gsync_config_command(dict); + break; + + case GF_GSYNC_OPTION_TYPE_STATUS: + status_detail = dict_get_str_boolean(dict, "status-detail", + _gf_false); + ret = gf_cli_gsync_status_output(dict, status_detail); + break; + + case GF_GSYNC_OPTION_TYPE_DELETE: + if (dict_get_str_sizen(dict, "master", &master) != 0) + master = "???"; + if (dict_get_str_sizen(dict, "slave", &slave) != 0) + slave = "???"; + cli_out("Deleting " GEOREP + " session between %s & %s has been successful", + master, slave); + break; + + case GF_GSYNC_OPTION_TYPE_CREATE: + if (dict_get_str_sizen(dict, "master", &master) != 0) + master = "???"; + if (dict_get_str_sizen(dict, "slave", &slave) != 0) + slave = "???"; + cli_out("Creating " GEOREP + " session between %s & %s has been successful", + master, slave); + break; + + default: + cli_out(GEOREP " command executed successfully"); + } + +out: + if (dict) + dict_unref(dict); + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + return ret; +} + +static int32_t +gf_cli_sys_exec(call_frame_t *frame, xlator_t *this, void *data) +{ + int ret = 0; + dict_t *dict = NULL; + gf_cli_req req = {{ + 0, + }}; + + dict = data; + + ret = cli_to_glusterd(&req, frame, gf_cli_sys_exec_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, GLUSTER_CLI_SYS_EXEC, + this, cli_rpc_prog, NULL); + if (ret) + if (!frame || !this || !data) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Invalid data"); + } + + GF_FREE(req.dict.dict_val); + return ret; +} + +static int32_t +gf_cli_copy_file(call_frame_t *frame, xlator_t *this, void *data) +{ + int ret = 0; + dict_t *dict = NULL; + gf_cli_req req = {{ + 0, + }}; + + dict = data; + + ret = cli_to_glusterd(&req, frame, gf_cli_copy_file_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, + GLUSTER_CLI_COPY_FILE, this, cli_rpc_prog, NULL); + if (ret) + if (!frame || !this || !data) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Invalid data"); + } + + GF_FREE(req.dict.dict_val); + return ret; +} + +static int32_t +gf_cli_gsync_set(call_frame_t *frame, xlator_t *this, void *data) +{ + int ret = 0; + dict_t *dict = NULL; + gf_cli_req req = {{ + 0, + }}; + + dict = data; + + ret = cli_to_glusterd(&req, frame, gf_cli_gsync_set_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, + GLUSTER_CLI_GSYNC_SET, this, cli_rpc_prog, NULL); + GF_FREE(req.dict.dict_val); + + return ret; +} + +int +cli_profile_info_percentage_cmp(void *a, void *b) +{ + cli_profile_info_t *ia = NULL; + cli_profile_info_t *ib = NULL; + + ia = a; + ib = b; + if (ia->percentage_avg_latency < ib->percentage_avg_latency) + return -1; + else if (ia->percentage_avg_latency > ib->percentage_avg_latency) + return 1; + + return 0; +} + +static void +cmd_profile_volume_brick_out(dict_t *dict, int count, int interval) +{ + char key[256] = {0}; + int i = 0; + uint64_t sec = 0; + uint64_t r_count = 0; + uint64_t w_count = 0; + uint64_t rb_counts[32] = {0}; + uint64_t wb_counts[32] = {0}; + cli_profile_info_t profile_info[GF_FOP_MAXVALUE] = {{0}}; + cli_profile_info_t upcall_info[GF_UPCALL_FLAGS_MAXVALUE] = { + {0}, + }; + char output[128] = {0}; + int per_line = 0; + char read_blocks[128] = {0}; + char write_blocks[128] = {0}; + int index = 0; + int is_header_printed = 0; + int ret = 0; + double total_percentage_latency = 0; + + for (i = 0; i < 32; i++) { + snprintf(key, sizeof(key), "%d-%d-read-%" PRIu32, count, interval, + (1U << i)); + ret = dict_get_uint64(dict, key, &rb_counts[i]); + if (ret) { + gf_log("cli", GF_LOG_DEBUG, "failed to get %s from dict", key); + } + } + + for (i = 0; i < 32; i++) { + snprintf(key, sizeof(key), "%d-%d-write-%" PRIu32, count, interval, + (1U << i)); + ret = dict_get_uint64(dict, key, &wb_counts[i]); + if (ret) { + gf_log("cli", GF_LOG_DEBUG, "failed to get %s from dict", key); + } + } + + for (i = 0; i < GF_UPCALL_FLAGS_MAXVALUE; i++) { + snprintf(key, sizeof(key), "%d-%d-%d-upcall-hits", count, interval, i); + ret = dict_get_uint64(dict, key, &upcall_info[i].fop_hits); + if (ret) { + gf_log("cli", GF_LOG_DEBUG, "failed to get %s from dict", key); + } + upcall_info[i].fop_name = (char *)gf_upcall_list[i]; + } + + for (i = 0; i < GF_FOP_MAXVALUE; i++) { + snprintf(key, sizeof(key), "%d-%d-%d-hits", count, interval, i); + ret = dict_get_uint64(dict, key, &profile_info[i].fop_hits); + if (ret) { + gf_log("cli", GF_LOG_DEBUG, "failed to get %s from dict", key); + } + + snprintf(key, sizeof(key), "%d-%d-%d-avglatency", count, interval, i); + ret = dict_get_double(dict, key, &profile_info[i].avg_latency); + if (ret) { + gf_log("cli", GF_LOG_DEBUG, "failed to get %s from dict", key); + } + + snprintf(key, sizeof(key), "%d-%d-%d-minlatency", count, interval, i); + ret = dict_get_double(dict, key, &profile_info[i].min_latency); + if (ret) { + gf_log("cli", GF_LOG_DEBUG, "failed to get %s from dict", key); + } + + snprintf(key, sizeof(key), "%d-%d-%d-maxlatency", count, interval, i); + ret = dict_get_double(dict, key, &profile_info[i].max_latency); + if (ret) { + gf_log("cli", GF_LOG_DEBUG, "failed to get %s from dict", key); + } + profile_info[i].fop_name = (char *)gf_fop_list[i]; + + total_percentage_latency += (profile_info[i].fop_hits * + profile_info[i].avg_latency); + } + if (total_percentage_latency) { + for (i = 0; i < GF_FOP_MAXVALUE; i++) { + profile_info[i] + .percentage_avg_latency = 100 * ((profile_info[i].avg_latency * + profile_info[i].fop_hits) / + total_percentage_latency); + } + gf_array_insertionsort(profile_info, 1, GF_FOP_MAXVALUE - 1, + sizeof(cli_profile_info_t), + cli_profile_info_percentage_cmp); + } + snprintf(key, sizeof(key), "%d-%d-duration", count, interval); + ret = dict_get_uint64(dict, key, &sec); + if (ret) { + gf_log("cli", GF_LOG_DEBUG, "failed to get %s from dict", key); + } + + snprintf(key, sizeof(key), "%d-%d-total-read", count, interval); + ret = dict_get_uint64(dict, key, &r_count); + if (ret) { + gf_log("cli", GF_LOG_DEBUG, "failed to get %s from dict", key); + } + + snprintf(key, sizeof(key), "%d-%d-total-write", count, interval); + ret = dict_get_uint64(dict, key, &w_count); + if (ret) { + gf_log("cli", GF_LOG_DEBUG, "failed to get %s from dict", key); + } + + if (interval == -1) + cli_out("Cumulative Stats:"); + else + cli_out("Interval %d Stats:", interval); + snprintf(output, sizeof(output), "%14s", "Block Size:"); + snprintf(read_blocks, sizeof(read_blocks), "%14s", "No. of Reads:"); + snprintf(write_blocks, sizeof(write_blocks), "%14s", "No. of Writes:"); + index = 14; + for (i = 0; i < 32; i++) { + if ((rb_counts[i] == 0) && (wb_counts[i] == 0)) + continue; + per_line++; + snprintf(output + index, sizeof(output) - index, "%19" PRIu32 "b+ ", + (1U << i)); + if (rb_counts[i]) { + snprintf(read_blocks + index, sizeof(read_blocks) - index, + "%21" PRId64 " ", rb_counts[i]); + } else { + snprintf(read_blocks + index, sizeof(read_blocks) - index, "%21s ", + "0"); + } + if (wb_counts[i]) { + snprintf(write_blocks + index, sizeof(write_blocks) - index, + "%21" PRId64 " ", wb_counts[i]); + } else { + snprintf(write_blocks + index, sizeof(write_blocks) - index, + "%21s ", "0"); + } + index += 22; + if (per_line == 3) { + cli_out("%s", output); + cli_out("%s", read_blocks); + cli_out("%s", write_blocks); + cli_out(" "); + per_line = 0; + snprintf(output, sizeof(output), "%14s", "Block Size:"); + snprintf(read_blocks, sizeof(read_blocks), "%14s", "No. of Reads:"); + snprintf(write_blocks, sizeof(write_blocks), "%14s", + "No. of Writes:"); + index = 14; + } + } + + if (per_line != 0) { + cli_out("%s", output); + cli_out("%s", read_blocks); + cli_out("%s", write_blocks); + } + for (i = 0; i < GF_FOP_MAXVALUE; i++) { + if (profile_info[i].fop_hits == 0) + continue; + if (is_header_printed == 0) { + cli_out("%10s %13s %13s %13s %14s %11s", "%-latency", "Avg-latency", + "Min-Latency", "Max-Latency", "No. of calls", "Fop"); + cli_out("%10s %13s %13s %13s %14s %11s", "---------", "-----------", + "-----------", "-----------", "------------", "----"); + is_header_printed = 1; + } + if (profile_info[i].fop_hits) { + cli_out( + "%10.2lf %10.2lf us %10.2lf us %10.2lf us" + " %14" PRId64 " %11s", + profile_info[i].percentage_avg_latency, + profile_info[i].avg_latency, profile_info[i].min_latency, + profile_info[i].max_latency, profile_info[i].fop_hits, + profile_info[i].fop_name); + } + } + + for (i = 0; i < GF_UPCALL_FLAGS_MAXVALUE; i++) { + if (upcall_info[i].fop_hits == 0) + continue; + if (upcall_info[i].fop_hits) { + cli_out( + "%10.2lf %10.2lf us %10.2lf us %10.2lf us" + " %14" PRId64 " %11s", + upcall_info[i].percentage_avg_latency, + upcall_info[i].avg_latency, upcall_info[i].min_latency, + upcall_info[i].max_latency, upcall_info[i].fop_hits, + upcall_info[i].fop_name); + } + } + + cli_out(" "); + cli_out("%12s: %" PRId64 " seconds", "Duration", sec); + cli_out("%12s: %" PRId64 " bytes", "Data Read", r_count); + cli_out("%12s: %" PRId64 " bytes", "Data Written", w_count); + cli_out(" "); +} + +static int32_t +gf_cli_profile_volume_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + dict_t *dict = NULL; + gf1_cli_stats_op op = GF_CLI_STATS_NONE; + char key[64] = {0}; + int len; + int interval = 0; + int i = 1; + int32_t brick_count = 0; + char *volname = NULL; + char *brick = NULL; + char str[1024] = { + 0, + }; + int stats_cleared = 0; + gf1_cli_info_op info_op = GF_CLI_INFO_NONE; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + gf_log("cli", GF_LOG_DEBUG, "Received resp to profile"); + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + goto out; + } + + dict = dict_new(); + + if (!dict) { + ret = -1; + goto out; + } + + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, &dict); + + if (ret) { + gf_log("", GF_LOG_ERROR, DICT_UNSERIALIZE_FAIL); + goto out; + } + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_vol_profile(dict, rsp.op_ret, rsp.op_errno, + rsp.op_errstr); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + + ret = dict_get_int32_sizen(dict, "op", (int32_t *)&op); + if (ret) + goto out; + + ret = dict_get_str_sizen(dict, "volname", &volname); + if (ret) + goto out; + + if (rsp.op_ret && strcmp(rsp.op_errstr, "")) { + cli_err("%s", rsp.op_errstr); + } else { + switch (op) { + case GF_CLI_STATS_START: + cli_out("Starting volume profile on %s has been %s ", volname, + (rsp.op_ret) ? "unsuccessful" : "successful"); + break; + case GF_CLI_STATS_STOP: + cli_out("Stopping volume profile on %s has been %s ", volname, + (rsp.op_ret) ? "unsuccessful" : "successful"); + break; + case GF_CLI_STATS_INFO: + break; + default: + cli_out("volume profile on %s has been %s ", volname, + (rsp.op_ret) ? "unsuccessful" : "successful"); + break; + } + } + + if (rsp.op_ret) { + ret = rsp.op_ret; + goto out; + } + + if (GF_CLI_STATS_INFO != op) { + ret = 0; + goto out; + } + + ret = dict_get_int32_sizen(dict, "count", &brick_count); + if (ret) + goto out; + + if (!brick_count) { + cli_err("All bricks of volume %s are down.", volname); + goto out; + } + + ret = dict_get_int32_sizen(dict, "info-op", (int32_t *)&info_op); + if (ret) + goto out; + + while (i <= brick_count) { + len = snprintf(key, sizeof(key), "%d-brick", i); + ret = dict_get_strn(dict, key, len, &brick); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Couldn't get brick name"); + goto out; + } + + ret = dict_get_str_boolean(dict, "nfs", _gf_false); + + if (ret) + len = snprintf(str, sizeof(str), "NFS Server : %s", brick); + else + len = snprintf(str, sizeof(str), "Brick: %s", brick); + cli_out("%s", str); + memset(str, '-', len); + cli_out("%s", str); + + if (GF_CLI_INFO_CLEAR == info_op) { + len = snprintf(key, sizeof(key), "%d-stats-cleared", i); + ret = dict_get_int32n(dict, key, len, &stats_cleared); + if (ret) + goto out; + cli_out(stats_cleared ? "Cleared stats." + : "Failed to clear stats."); + } else { + len = snprintf(key, sizeof(key), "%d-cumulative", i); + ret = dict_get_int32n(dict, key, len, &interval); + if (ret == 0) + cmd_profile_volume_brick_out(dict, i, interval); + + len = snprintf(key, sizeof(key), "%d-interval", i); + ret = dict_get_int32n(dict, key, len, &interval); + if (ret == 0) + cmd_profile_volume_brick_out(dict, i, interval); + } + i++; + } + ret = rsp.op_ret; + +out: + if (dict) + dict_unref(dict); + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + return ret; +} + +static int32_t +gf_cli_profile_volume(call_frame_t *frame, xlator_t *this, void *data) +{ + int ret = -1; + gf_cli_req req = {{ + 0, + }}; + dict_t *dict = NULL; + + GF_ASSERT(frame); + GF_ASSERT(this); + GF_ASSERT(data); + + dict = data; + + ret = cli_to_glusterd(&req, frame, gf_cli_profile_volume_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, + GLUSTER_CLI_PROFILE_VOLUME, this, cli_rpc_prog, NULL); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + + GF_FREE(req.dict.dict_val); + return ret; +} + +static int32_t +gf_cli_top_volume_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + dict_t *dict = NULL; + gf1_cli_stats_op op = GF_CLI_STATS_NONE; + char key[256] = {0}; + int keylen; + int i = 0; + int32_t brick_count = 0; + char brick[1024]; + int32_t members = 0; + char *filename; + char *bricks; + uint64_t value = 0; + int32_t j = 0; + gf1_cli_top_op top_op = GF_CLI_TOP_NONE; + uint64_t nr_open = 0; + uint64_t max_nr_open = 0; + double throughput = 0; + double time = 0; + int32_t time_sec = 0; + long int time_usec = 0; + char timestr[GF_TIMESTR_SIZE] = { + 0, + }; + char *openfd_str = NULL; + gf_boolean_t nfs = _gf_false; + gf_boolean_t clear_stats = _gf_false; + int stats_cleared = 0; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + gf_log("cli", GF_LOG_DEBUG, "Received resp to top"); + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + goto out; + } + + if (rsp.op_ret) { + if (strcmp(rsp.op_errstr, "")) + cli_err("%s", rsp.op_errstr); + cli_err("volume top unsuccessful"); + ret = rsp.op_ret; + goto out; + } + + dict = dict_new(); + + if (!dict) { + ret = -1; + goto out; + } + + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, &dict); + + if (ret) { + gf_log("", GF_LOG_ERROR, DICT_UNSERIALIZE_FAIL); + goto out; + } + + ret = dict_get_int32_sizen(dict, "op", (int32_t *)&op); + + if (op != GF_CLI_STATS_TOP) { + ret = 0; + goto out; + } + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_vol_top(dict, rsp.op_ret, rsp.op_errno, + rsp.op_errstr); + if (ret) { + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + } + goto out; + } + + ret = dict_get_int32_sizen(dict, "count", &brick_count); + if (ret) + goto out; + keylen = snprintf(key, sizeof(key), "%d-top-op", 1); + ret = dict_get_int32n(dict, key, keylen, (int32_t *)&top_op); + if (ret) + goto out; + + clear_stats = dict_get_str_boolean(dict, "clear-stats", _gf_false); + + while (i < brick_count) { + i++; + keylen = snprintf(brick, sizeof(brick), "%d-brick", i); + ret = dict_get_strn(dict, brick, keylen, &bricks); + if (ret) + goto out; + + nfs = dict_get_str_boolean(dict, "nfs", _gf_false); + + if (clear_stats) { + keylen = snprintf(key, sizeof(key), "%d-stats-cleared", i); + ret = dict_get_int32n(dict, key, keylen, &stats_cleared); + if (ret) + goto out; + cli_out(stats_cleared ? "Cleared stats for %s %s" + : "Failed to clear stats for %s %s", + nfs ? "NFS server on" : "brick", bricks); + continue; + } + + if (nfs) + cli_out("NFS Server : %s", bricks); + else + cli_out("Brick: %s", bricks); + + keylen = snprintf(key, sizeof(key), "%d-members", i); + ret = dict_get_int32n(dict, key, keylen, &members); + + switch (top_op) { + case GF_CLI_TOP_OPEN: + snprintf(key, sizeof(key), "%d-current-open", i); + ret = dict_get_uint64(dict, key, &nr_open); + if (ret) + break; + snprintf(key, sizeof(key), "%d-max-open", i); + ret = dict_get_uint64(dict, key, &max_nr_open); + if (ret) + goto out; + keylen = snprintf(key, sizeof(key), "%d-max-openfd-time", i); + ret = dict_get_strn(dict, key, keylen, &openfd_str); + if (ret) + goto out; + cli_out("Current open fds: %" PRIu64 ", Max open fds: %" PRIu64 + ", Max openfd time: %s", + nr_open, max_nr_open, openfd_str); + case GF_CLI_TOP_READ: + case GF_CLI_TOP_WRITE: + case GF_CLI_TOP_OPENDIR: + case GF_CLI_TOP_READDIR: + if (!members) { + continue; + } + cli_out("Count\t\tfilename\n======================="); + break; + case GF_CLI_TOP_READ_PERF: + case GF_CLI_TOP_WRITE_PERF: + snprintf(key, sizeof(key), "%d-throughput", i); + ret = dict_get_double(dict, key, &throughput); + if (!ret) { + snprintf(key, sizeof(key), "%d-time", i); + ret = dict_get_double(dict, key, &time); + } + if (!ret) + cli_out("Throughput %.2f MBps time %.4f secs", throughput, + time / 1e6); + + if (!members) { + continue; + } + cli_out("%*s %-*s %-*s", VOL_TOP_PERF_SPEED_WIDTH, "MBps", + VOL_TOP_PERF_FILENAME_DEF_WIDTH, "Filename", + VOL_TOP_PERF_TIME_WIDTH, "Time"); + cli_out("%*s %-*s %-*s", VOL_TOP_PERF_SPEED_WIDTH, + "====", VOL_TOP_PERF_FILENAME_DEF_WIDTH, + "========", VOL_TOP_PERF_TIME_WIDTH, "===="); + break; + default: + goto out; + } + + for (j = 1; j <= members; j++) { + keylen = snprintf(key, sizeof(key), "%d-filename-%d", i, j); + ret = dict_get_strn(dict, key, keylen, &filename); + if (ret) + break; + snprintf(key, sizeof(key), "%d-value-%d", i, j); + ret = dict_get_uint64(dict, key, &value); + if (ret) + goto out; + if (top_op == GF_CLI_TOP_READ_PERF || + top_op == GF_CLI_TOP_WRITE_PERF) { + keylen = snprintf(key, sizeof(key), "%d-time-sec-%d", i, j); + ret = dict_get_int32n(dict, key, keylen, (int32_t *)&time_sec); + if (ret) + goto out; + keylen = snprintf(key, sizeof(key), "%d-time-usec-%d", i, j); + ret = dict_get_int32n(dict, key, keylen, (int32_t *)&time_usec); + if (ret) + goto out; + gf_time_fmt(timestr, sizeof timestr, time_sec, gf_timefmt_FT); + snprintf(timestr + strlen(timestr), + sizeof timestr - strlen(timestr), ".%ld", time_usec); + if (strlen(filename) < VOL_TOP_PERF_FILENAME_DEF_WIDTH) + cli_out("%*" PRIu64 " %-*s %-*s", VOL_TOP_PERF_SPEED_WIDTH, + value, VOL_TOP_PERF_FILENAME_DEF_WIDTH, filename, + VOL_TOP_PERF_TIME_WIDTH, timestr); + else + cli_out("%*" PRIu64 " ...%-*s %-*s", + VOL_TOP_PERF_SPEED_WIDTH, value, + VOL_TOP_PERF_FILENAME_ALT_WIDTH, + filename + strlen(filename) - + VOL_TOP_PERF_FILENAME_ALT_WIDTH, + VOL_TOP_PERF_TIME_WIDTH, timestr); + } else { + cli_out("%" PRIu64 "\t\t%s", value, filename); + } + } + } + ret = rsp.op_ret; + +out: + cli_cmd_broadcast_response(ret); + + if (dict) + dict_unref(dict); + + gf_free_xdr_cli_rsp(rsp); + return ret; +} + +static int32_t +gf_cli_top_volume(call_frame_t *frame, xlator_t *this, void *data) +{ + int ret = -1; + gf_cli_req req = {{ + 0, + }}; + dict_t *dict = NULL; + + GF_ASSERT(frame); + GF_ASSERT(this); + GF_ASSERT(data); + + dict = data; + + ret = cli_to_glusterd(&req, frame, gf_cli_top_volume_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, + GLUSTER_CLI_PROFILE_VOLUME, this, cli_rpc_prog, NULL); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + GF_FREE(req.dict.dict_val); + return ret; +} + +static int +gf_cli_getwd_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf1_cli_getwd_rsp rsp = { + 0, + }; + int ret = -1; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf1_cli_getwd_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + goto out; + } + + if (rsp.op_ret == -1) { + cli_err("getwd failed"); + ret = rsp.op_ret; + goto out; + } + + gf_log("cli", GF_LOG_INFO, "Received resp to getwd"); + + cli_out("%s", rsp.wd); + + ret = 0; + +out: + cli_cmd_broadcast_response(ret); + if (rsp.wd) { + free(rsp.wd); + } + + return ret; +} + +static int32_t +gf_cli_getwd(call_frame_t *frame, xlator_t *this, void *data) +{ + int ret = -1; + gf1_cli_getwd_req req = { + 0, + }; + + GF_ASSERT(frame); + GF_ASSERT(this); + + if (!frame || !this) + goto out; + + ret = cli_cmd_submit(NULL, &req, frame, cli_rpc_prog, GLUSTER_CLI_GETWD, + NULL, this, gf_cli_getwd_cbk, + (xdrproc_t)xdr_gf1_cli_getwd_req); + +out: + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + + return ret; +} + +static void +cli_print_volume_status_mempool(dict_t *dict, char *prefix) +{ + int ret = -1; + int32_t mempool_count = 0; + char *name = NULL; + int32_t hotcount = 0; + int32_t coldcount = 0; + uint64_t paddedsizeof = 0; + uint64_t alloccount = 0; + int32_t maxalloc = 0; + uint64_t pool_misses = 0; + int32_t maxstdalloc = 0; + char key[128] = { + /* prefix is really small 'brick%d' really */ + 0, + }; + int keylen; + int i = 0; + + GF_ASSERT(dict); + GF_ASSERT(prefix); + + keylen = snprintf(key, sizeof(key), "%s.mempool-count", prefix); + ret = dict_get_int32n(dict, key, keylen, &mempool_count); + if (ret) + goto out; + + cli_out("Mempool Stats\n-------------"); + cli_out("%-30s %9s %9s %12s %10s %8s %8s %12s", "Name", "HotCount", + "ColdCount", "PaddedSizeof", "AllocCount", "MaxAlloc", "Misses", + "Max-StdAlloc"); + cli_out("%-30s %9s %9s %12s %10s %8s %8s %12s", "----", "--------", + "---------", "------------", "----------", "--------", "--------", + "------------"); + + for (i = 0; i < mempool_count; i++) { + keylen = snprintf(key, sizeof(key), "%s.pool%d.name", prefix, i); + ret = dict_get_strn(dict, key, keylen, &name); + if (ret) + goto out; + + keylen = snprintf(key, sizeof(key), "%s.pool%d.hotcount", prefix, i); + ret = dict_get_int32n(dict, key, keylen, &hotcount); + if (ret) + goto out; + + keylen = snprintf(key, sizeof(key), "%s.pool%d.coldcount", prefix, i); + ret = dict_get_int32n(dict, key, keylen, &coldcount); + if (ret) + goto out; + + snprintf(key, sizeof(key), "%s.pool%d.paddedsizeof", prefix, i); + ret = dict_get_uint64(dict, key, &paddedsizeof); + if (ret) + goto out; + + snprintf(key, sizeof(key), "%s.pool%d.alloccount", prefix, i); + ret = dict_get_uint64(dict, key, &alloccount); + if (ret) + goto out; + + keylen = snprintf(key, sizeof(key), "%s.pool%d.max_alloc", prefix, i); + ret = dict_get_int32n(dict, key, keylen, &maxalloc); + if (ret) + goto out; + + keylen = snprintf(key, sizeof(key), "%s.pool%d.max-stdalloc", prefix, + i); + ret = dict_get_int32n(dict, key, keylen, &maxstdalloc); + if (ret) + goto out; + + snprintf(key, sizeof(key), "%s.pool%d.pool-misses", prefix, i); + ret = dict_get_uint64(dict, key, &pool_misses); + if (ret) + goto out; + + cli_out("%-30s %9d %9d %12" PRIu64 " %10" PRIu64 " %8d %8" PRIu64 + " %12d", + name, hotcount, coldcount, paddedsizeof, alloccount, maxalloc, + pool_misses, maxstdalloc); + } + +out: + return; +} + +static void +cli_print_volume_status_mem(dict_t *dict, gf_boolean_t notbrick) +{ + int ret = -1; + char *volname = NULL; + char *hostname = NULL; + char *path = NULL; + int online = -1; + char key[64] = { + 0, + }; + int brick_index_max = -1; + int other_count = 0; + int index_max = 0; + int val = 0; + int i = 0; + + GF_ASSERT(dict); + + ret = dict_get_str_sizen(dict, "volname", &volname); + if (ret) + goto out; + cli_out("Memory status for volume : %s", volname); + + ret = dict_get_int32_sizen(dict, "brick-index-max", &brick_index_max); + if (ret) + goto out; + ret = dict_get_int32_sizen(dict, "other-count", &other_count); + if (ret) + goto out; + + index_max = brick_index_max + other_count; + + for (i = 0; i <= index_max; i++) { + cli_out("----------------------------------------------"); + + snprintf(key, sizeof(key), "brick%d.hostname", i); + ret = dict_get_str(dict, key, &hostname); + if (ret) + continue; + snprintf(key, sizeof(key), "brick%d.path", i); + ret = dict_get_str(dict, key, &path); + if (ret) + continue; + if (notbrick) + cli_out("%s : %s", hostname, path); + else + cli_out("Brick : %s:%s", hostname, path); + + snprintf(key, sizeof(key), "brick%d.status", i); + ret = dict_get_int32(dict, key, &online); + if (ret) + goto out; + if (!online) { + if (notbrick) + cli_out("%s is offline", hostname); + else + cli_out("Brick is offline"); + continue; + } + + cli_out("Mallinfo\n--------"); + + snprintf(key, sizeof(key), "brick%d.mallinfo.arena", i); + ret = dict_get_int32(dict, key, &val); + if (ret) + goto out; + cli_out("%-8s : %d", "Arena", val); + + snprintf(key, sizeof(key), "brick%d.mallinfo.ordblks", i); + ret = dict_get_int32(dict, key, &val); + if (ret) + goto out; + cli_out("%-8s : %d", "Ordblks", val); + + snprintf(key, sizeof(key), "brick%d.mallinfo.smblks", i); + ret = dict_get_int32(dict, key, &val); + if (ret) + goto out; + cli_out("%-8s : %d", "Smblks", val); + + snprintf(key, sizeof(key), "brick%d.mallinfo.hblks", i); + ret = dict_get_int32(dict, key, &val); + if (ret) + goto out; + cli_out("%-8s : %d", "Hblks", val); + + snprintf(key, sizeof(key), "brick%d.mallinfo.hblkhd", i); + ret = dict_get_int32(dict, key, &val); + if (ret) + goto out; + cli_out("%-8s : %d", "Hblkhd", val); + + snprintf(key, sizeof(key), "brick%d.mallinfo.usmblks", i); + ret = dict_get_int32(dict, key, &val); + if (ret) + goto out; + cli_out("%-8s : %d", "Usmblks", val); + + snprintf(key, sizeof(key), "brick%d.mallinfo.fsmblks", i); + ret = dict_get_int32(dict, key, &val); + if (ret) + goto out; + cli_out("%-8s : %d", "Fsmblks", val); + + snprintf(key, sizeof(key), "brick%d.mallinfo.uordblks", i); + ret = dict_get_int32(dict, key, &val); + if (ret) + goto out; + cli_out("%-8s : %d", "Uordblks", val); + + snprintf(key, sizeof(key), "brick%d.mallinfo.fordblks", i); + ret = dict_get_int32(dict, key, &val); + if (ret) + goto out; + cli_out("%-8s : %d", "Fordblks", val); + + snprintf(key, sizeof(key), "brick%d.mallinfo.keepcost", i); + ret = dict_get_int32(dict, key, &val); + if (ret) + goto out; + cli_out("%-8s : %d", "Keepcost", val); + + cli_out(" "); + snprintf(key, sizeof(key), "brick%d", i); + cli_print_volume_status_mempool(dict, key); + } +out: + cli_out("----------------------------------------------\n"); + return; +} + +static void +cli_print_volume_status_client_list(dict_t *dict, gf_boolean_t notbrick) +{ + int ret = -1; + char *volname = NULL; + int client_count = 0; + int current_count = 0; + char key[64] = { + 0, + }; + int i = 0; + int total = 0; + char *name = NULL; + gf_boolean_t is_fuse_done = _gf_false; + gf_boolean_t is_gfapi_done = _gf_false; + gf_boolean_t is_rebalance_done = _gf_false; + gf_boolean_t is_glustershd_done = _gf_false; + gf_boolean_t is_quotad_done = _gf_false; + gf_boolean_t is_snapd_done = _gf_false; + + GF_ASSERT(dict); + + ret = dict_get_str_sizen(dict, "volname", &volname); + if (ret) + goto out; + cli_out("Client connections for volume %s", volname); + + ret = dict_get_int32_sizen(dict, "client-count", &client_count); + if (ret) + goto out; + + cli_out("%-48s %15s", "Name", "count"); + cli_out("%-48s %15s", "-----", "------"); + for (i = 0; i < client_count; i++) { + name = NULL; + snprintf(key, sizeof(key), "client%d.name", i); + ret = dict_get_str(dict, key, &name); + + if (!strncmp(name, "fuse", 4)) { + if (!is_fuse_done) { + is_fuse_done = _gf_true; + ret = dict_get_int32_sizen(dict, "fuse-count", ¤t_count); + if (ret) + goto out; + total = total + current_count; + goto print; + } + continue; + } else if (!strncmp(name, "gfapi", 5)) { + if (!is_gfapi_done) { + is_gfapi_done = _gf_true; + ret = dict_get_int32_sizen(dict, "gfapi-count", ¤t_count); + if (ret) + goto out; + total = total + current_count; + goto print; + } + continue; + } else if (!strcmp(name, "rebalance")) { + if (!is_rebalance_done) { + is_rebalance_done = _gf_true; + ret = dict_get_int32_sizen(dict, "rebalance-count", + ¤t_count); + if (ret) + goto out; + total = total + current_count; + goto print; + } + continue; + } else if (!strcmp(name, "glustershd")) { + if (!is_glustershd_done) { + is_glustershd_done = _gf_true; + ret = dict_get_int32_sizen(dict, "glustershd-count", + ¤t_count); + if (ret) + goto out; + total = total + current_count; + goto print; + } + continue; + } else if (!strcmp(name, "quotad")) { + if (!is_quotad_done) { + is_quotad_done = _gf_true; + ret = dict_get_int32_sizen(dict, "quotad-count", + ¤t_count); + if (ret) + goto out; + total = total + current_count; + goto print; + } + continue; + } else if (!strcmp(name, "snapd")) { + if (!is_snapd_done) { + is_snapd_done = _gf_true; + ret = dict_get_int32_sizen(dict, "snapd-count", ¤t_count); + if (ret) + goto out; + total = total + current_count; + goto print; + } + continue; + } + + print: + cli_out("%-48s %15d", name, current_count); + } +out: + cli_out("\ntotal clients for volume %s : %d ", volname, total); + cli_out( + "-----------------------------------------------------------------\n"); + return; +} + +static void +cli_print_volume_status_clients(dict_t *dict, gf_boolean_t notbrick) +{ + int ret = -1; + char *volname = NULL; + int brick_index_max = -1; + int other_count = 0; + int index_max = 0; + char *hostname = NULL; + char *path = NULL; + int online = -1; + int client_count = 0; + char *clientname = NULL; + uint64_t bytesread = 0; + uint64_t byteswrite = 0; + uint32_t opversion = 0; + char key[128] = { + 0, + }; + int i = 0; + int j = 0; + + GF_ASSERT(dict); + + ret = dict_get_str_sizen(dict, "volname", &volname); + if (ret) + goto out; + cli_out("Client connections for volume %s", volname); + + ret = dict_get_int32_sizen(dict, "brick-index-max", &brick_index_max); + if (ret) + goto out; + ret = dict_get_int32_sizen(dict, "other-count", &other_count); + if (ret) + goto out; + + index_max = brick_index_max + other_count; + + for (i = 0; i <= index_max; i++) { + hostname = NULL; + path = NULL; + online = -1; + client_count = 0; + clientname = NULL; + bytesread = 0; + byteswrite = 0; + + cli_out("----------------------------------------------"); + + snprintf(key, sizeof(key), "brick%d.hostname", i); + ret = dict_get_str(dict, key, &hostname); + + snprintf(key, sizeof(key), "brick%d.path", i); + ret = dict_get_str(dict, key, &path); + + if (hostname && path) { + if (notbrick) + cli_out("%s : %s", hostname, path); + else + cli_out("Brick : %s:%s", hostname, path); + } + snprintf(key, sizeof(key), "brick%d.status", i); + ret = dict_get_int32(dict, key, &online); + if (!online) { + if (notbrick) + cli_out("%s is offline", hostname); + else + cli_out("Brick is offline"); + continue; + } + + snprintf(key, sizeof(key), "brick%d.clientcount", i); + ret = dict_get_int32(dict, key, &client_count); + + if (hostname && path) + cli_out("Clients connected : %d", client_count); + if (client_count == 0) + continue; + + cli_out("%-48s %15s %15s %15s", "Hostname", "BytesRead", "BytesWritten", + "OpVersion"); + cli_out("%-48s %15s %15s %15s", "--------", "---------", "------------", + "---------"); + for (j = 0; j < client_count; j++) { + snprintf(key, sizeof(key), "brick%d.client%d.hostname", i, j); + ret = dict_get_str(dict, key, &clientname); + + snprintf(key, sizeof(key), "brick%d.client%d.bytesread", i, j); + ret = dict_get_uint64(dict, key, &bytesread); + + snprintf(key, sizeof(key), "brick%d.client%d.byteswrite", i, j); + ret = dict_get_uint64(dict, key, &byteswrite); + + snprintf(key, sizeof(key), "brick%d.client%d.opversion", i, j); + ret = dict_get_uint32(dict, key, &opversion); + + cli_out("%-48s %15" PRIu64 " %15" PRIu64 " %15" PRIu32, clientname, + bytesread, byteswrite, opversion); + } + } +out: + cli_out("----------------------------------------------\n"); + return; +} + +#ifdef DEBUG /* this function is only used in debug */ +static void +cli_print_volume_status_inode_entry(dict_t *dict, char *prefix) +{ + int ret = -1; + char key[1024] = { + 0, + }; + char *gfid = NULL; + uint64_t nlookup = 0; + uint32_t ref = 0; + int ia_type = 0; + char inode_type; + + GF_ASSERT(dict); + GF_ASSERT(prefix); + + snprintf(key, sizeof(key), "%s.gfid", prefix); + ret = dict_get_str(dict, key, &gfid); + if (ret) + goto out; + + snprintf(key, sizeof(key), "%s.nlookup", prefix); + ret = dict_get_uint64(dict, key, &nlookup); + if (ret) + goto out; + + snprintf(key, sizeof(key), "%s.ref", prefix); + ret = dict_get_uint32(dict, key, &ref); + if (ret) + goto out; + + snprintf(key, sizeof(key), "%s.ia_type", prefix); + ret = dict_get_int32(dict, key, &ia_type); + if (ret) + goto out; + + switch (ia_type) { + case IA_IFREG: + inode_type = 'R'; + break; + case IA_IFDIR: + inode_type = 'D'; + break; + case IA_IFLNK: + inode_type = 'L'; + break; + case IA_IFBLK: + inode_type = 'B'; + break; + case IA_IFCHR: + inode_type = 'C'; + break; + case IA_IFIFO: + inode_type = 'F'; + break; + case IA_IFSOCK: + inode_type = 'S'; + break; + default: + inode_type = 'I'; + break; + } + + cli_out("%-40s %14" PRIu64 " %14" PRIu32 " %9c", gfid, nlookup, ref, + inode_type); + +out: + return; +} +#endif + +static void +cli_print_volume_status_itables(dict_t *dict, char *prefix) +{ + int ret = -1; + char key[1024] = { + 0, + }; + uint32_t active_size = 0; + uint32_t lru_size = 0; + uint32_t purge_size = 0; + uint32_t lru_limit = 0; +#ifdef DEBUG + int i = 0; +#endif + GF_ASSERT(dict); + GF_ASSERT(prefix); + + snprintf(key, sizeof(key), "%s.lru_limit", prefix); + ret = dict_get_uint32(dict, key, &lru_limit); + if (ret) + goto out; + cli_out("LRU limit : %u", lru_limit); + + snprintf(key, sizeof(key), "%s.active_size", prefix); + ret = dict_get_uint32(dict, key, &active_size); + if (ret) + goto out; + +#ifdef DEBUG + if (active_size != 0) { + cli_out("Active inodes:"); + cli_out("%-40s %14s %14s %9s", "GFID", "Lookups", "Ref", "IA type"); + cli_out("%-40s %14s %14s %9s", "----", "-------", "---", "-------"); + } + for (i = 0; i < active_size; i++) { + snprintf(key, sizeof(key), "%s.active%d", prefix, i); + cli_print_volume_status_inode_entry(dict, key); + } + cli_out(" "); +#else + cli_out("Active Inodes : %u", active_size); + +#endif + + snprintf(key, sizeof(key), "%s.lru_size", prefix); + ret = dict_get_uint32(dict, key, &lru_size); + if (ret) + goto out; + +#ifdef DEBUG + if (lru_size != 0) { + cli_out("LRU inodes:"); + cli_out("%-40s %14s %14s %9s", "GFID", "Lookups", "Ref", "IA type"); + cli_out("%-40s %14s %14s %9s", "----", "-------", "---", "-------"); + } + for (i = 0; i < lru_size; i++) { + snprintf(key, sizeof(key), "%s.lru%d", prefix, i); + cli_print_volume_status_inode_entry(dict, key); + } + cli_out(" "); +#else + cli_out("LRU Inodes : %u", lru_size); +#endif + + snprintf(key, sizeof(key), "%s.purge_size", prefix); + ret = dict_get_uint32(dict, key, &purge_size); + if (ret) + goto out; +#ifdef DEBUG + if (purge_size != 0) { + cli_out("Purged inodes:"); + cli_out("%-40s %14s %14s %9s", "GFID", "Lookups", "Ref", "IA type"); + cli_out("%-40s %14s %14s %9s", "----", "-------", "---", "-------"); + } + for (i = 0; i < purge_size; i++) { + snprintf(key, sizeof(key), "%s.purge%d", prefix, i); + cli_print_volume_status_inode_entry(dict, key); + } +#else + cli_out("Purge Inodes : %u", purge_size); +#endif + +out: + return; +} + +static void +cli_print_volume_status_inode(dict_t *dict, gf_boolean_t notbrick) +{ + int ret = -1; + char *volname = NULL; + int brick_index_max = -1; + int other_count = 0; + int index_max = 0; + char *hostname = NULL; + char *path = NULL; + int online = -1; + int conn_count = 0; + char key[64] = { + 0, + }; + int i = 0; + int j = 0; + + GF_ASSERT(dict); + + ret = dict_get_str_sizen(dict, "volname", &volname); + if (ret) + goto out; + cli_out("Inode tables for volume %s", volname); + + ret = dict_get_int32_sizen(dict, "brick-index-max", &brick_index_max); + if (ret) + goto out; + ret = dict_get_int32_sizen(dict, "other-count", &other_count); + if (ret) + goto out; + + index_max = brick_index_max + other_count; + + for (i = 0; i <= index_max; i++) { + cli_out("----------------------------------------------"); + + snprintf(key, sizeof(key), "brick%d.hostname", i); + ret = dict_get_str(dict, key, &hostname); + if (ret) + goto out; + snprintf(key, sizeof(key), "brick%d.path", i); + ret = dict_get_str(dict, key, &path); + if (ret) + goto out; + if (notbrick) + cli_out("%s : %s", hostname, path); + else + cli_out("Brick : %s:%s", hostname, path); + + snprintf(key, sizeof(key), "brick%d.status", i); + ret = dict_get_int32(dict, key, &online); + if (ret) + goto out; + if (!online) { + if (notbrick) + cli_out("%s is offline", hostname); + else + cli_out("Brick is offline"); + continue; + } + + snprintf(key, sizeof(key), "brick%d.conncount", i); + ret = dict_get_int32(dict, key, &conn_count); + if (ret) + goto out; + + for (j = 0; j < conn_count; j++) { + if (conn_count > 1) + cli_out("Connection %d:", j + 1); + + snprintf(key, sizeof(key), "brick%d.conn%d.itable", i, j); + cli_print_volume_status_itables(dict, key); + cli_out(" "); + } + } +out: + cli_out("----------------------------------------------"); + return; +} + +void +cli_print_volume_status_fdtable(dict_t *dict, char *prefix) +{ + int ret = -1; + char key[256] = { + 0, + }; + int refcount = 0; + uint32_t maxfds = 0; + int firstfree = 0; + int openfds = 0; + int fd_pid = 0; + int fd_refcount = 0; + int fd_flags = 0; + int i = 0; + + GF_ASSERT(dict); + GF_ASSERT(prefix); + + snprintf(key, sizeof(key), "%s.refcount", prefix); + ret = dict_get_int32(dict, key, &refcount); + if (ret) + goto out; + + snprintf(key, sizeof(key), "%s.maxfds", prefix); + ret = dict_get_uint32(dict, key, &maxfds); + if (ret) + goto out; + + snprintf(key, sizeof(key), "%s.firstfree", prefix); + ret = dict_get_int32(dict, key, &firstfree); + if (ret) + goto out; + + cli_out("RefCount = %d MaxFDs = %d FirstFree = %d", refcount, maxfds, + firstfree); + + snprintf(key, sizeof(key), "%s.openfds", prefix); + ret = dict_get_int32(dict, key, &openfds); + if (ret) + goto out; + if (0 == openfds) { + cli_err("No open fds"); + goto out; + } + + cli_out("%-19s %-19s %-19s %-19s", "FD Entry", "PID", "RefCount", "Flags"); + cli_out("%-19s %-19s %-19s %-19s", "--------", "---", "--------", "-----"); + + for (i = 0; i < maxfds; i++) { + snprintf(key, sizeof(key), "%s.fdentry%d.pid", prefix, i); + ret = dict_get_int32(dict, key, &fd_pid); + if (ret) + continue; + + snprintf(key, sizeof(key), "%s.fdentry%d.refcount", prefix, i); + ret = dict_get_int32(dict, key, &fd_refcount); + if (ret) + continue; + + snprintf(key, sizeof(key), "%s.fdentry%d.flags", prefix, i); + ret = dict_get_int32(dict, key, &fd_flags); + if (ret) + continue; + + cli_out("%-19d %-19d %-19d %-19d", i, fd_pid, fd_refcount, fd_flags); + } + +out: + return; +} + +static void +cli_print_volume_status_fd(dict_t *dict, gf_boolean_t notbrick) +{ + int ret = -1; + char *volname = NULL; + int brick_index_max = -1; + int other_count = 0; + int index_max = 0; + char *hostname = NULL; + char *path = NULL; + int online = -1; + int conn_count = 0; + char key[64] = { + 0, + }; + int i = 0; + int j = 0; + + GF_ASSERT(dict); + + ret = dict_get_str_sizen(dict, "volname", &volname); + if (ret) + goto out; + cli_out("FD tables for volume %s", volname); + + ret = dict_get_int32_sizen(dict, "brick-index-max", &brick_index_max); + if (ret) + goto out; + ret = dict_get_int32_sizen(dict, "other-count", &other_count); + if (ret) + goto out; + + index_max = brick_index_max + other_count; + + for (i = 0; i <= index_max; i++) { + cli_out("----------------------------------------------"); + + snprintf(key, sizeof(key), "brick%d.hostname", i); + ret = dict_get_str(dict, key, &hostname); + if (ret) + goto out; + snprintf(key, sizeof(key), "brick%d.path", i); + ret = dict_get_str(dict, key, &path); + if (ret) + goto out; + + if (notbrick) + cli_out("%s : %s", hostname, path); + else + cli_out("Brick : %s:%s", hostname, path); + + snprintf(key, sizeof(key), "brick%d.status", i); + ret = dict_get_int32(dict, key, &online); + if (ret) + goto out; + if (!online) { + if (notbrick) + cli_out("%s is offline", hostname); + else + cli_out("Brick is offline"); + continue; + } + + snprintf(key, sizeof(key), "brick%d.conncount", i); + ret = dict_get_int32(dict, key, &conn_count); + if (ret) + goto out; + + for (j = 0; j < conn_count; j++) { + cli_out("Connection %d:", j + 1); + + snprintf(key, sizeof(key), "brick%d.conn%d.fdtable", i, j); + cli_print_volume_status_fdtable(dict, key); + cli_out(" "); + } + } +out: + cli_out("----------------------------------------------"); + return; +} + +static void +cli_print_volume_status_call_frame(dict_t *dict, char *prefix) +{ + int ret = -1; + char key[1024] = { + 0, + }; + int ref_count = 0; + char *translator = 0; + int complete = 0; + char *parent = NULL; + char *wind_from = NULL; + char *wind_to = NULL; + char *unwind_from = NULL; + char *unwind_to = NULL; + + if (!dict || !prefix) + return; + + snprintf(key, sizeof(key), "%s.refcount", prefix); + ret = dict_get_int32(dict, key, &ref_count); + if (ret) + return; + + snprintf(key, sizeof(key), "%s.translator", prefix); + ret = dict_get_str(dict, key, &translator); + if (ret) + return; + + snprintf(key, sizeof(key), "%s.complete", prefix); + ret = dict_get_int32(dict, key, &complete); + if (ret) + return; + + cli_out(" Ref Count = %d", ref_count); + cli_out(" Translator = %s", translator); + cli_out(" Completed = %s", (complete ? "Yes" : "No")); + + snprintf(key, sizeof(key), "%s.parent", prefix); + ret = dict_get_str(dict, key, &parent); + if (!ret) + cli_out(" Parent = %s", parent); + + snprintf(key, sizeof(key), "%s.windfrom", prefix); + ret = dict_get_str(dict, key, &wind_from); + if (!ret) + cli_out(" Wind From = %s", wind_from); + + snprintf(key, sizeof(key), "%s.windto", prefix); + ret = dict_get_str(dict, key, &wind_to); + if (!ret) + cli_out(" Wind To = %s", wind_to); + + snprintf(key, sizeof(key), "%s.unwindfrom", prefix); + ret = dict_get_str(dict, key, &unwind_from); + if (!ret) + cli_out(" Unwind From = %s", unwind_from); + + snprintf(key, sizeof(key), "%s.unwindto", prefix); + ret = dict_get_str(dict, key, &unwind_to); + if (!ret) + cli_out(" Unwind To = %s", unwind_to); +} + +static void +cli_print_volume_status_call_stack(dict_t *dict, char *prefix) +{ + int ret = -1; + char key[256] = { + 0, + }; + int uid = 0; + int gid = 0; + int pid = 0; + uint64_t unique = 0; + // char *op = NULL; + int count = 0; + int i = 0; + + if (!prefix) + return; + + snprintf(key, sizeof(key), "%s.uid", prefix); + ret = dict_get_int32(dict, key, &uid); + if (ret) + return; + + snprintf(key, sizeof(key), "%s.gid", prefix); + ret = dict_get_int32(dict, key, &gid); + if (ret) + return; + + snprintf(key, sizeof(key), "%s.pid", prefix); + ret = dict_get_int32(dict, key, &pid); + if (ret) + return; + + snprintf(key, sizeof(key), "%s.unique", prefix); + ret = dict_get_uint64(dict, key, &unique); + if (ret) + return; + + /* + snprintf (key, sizeof (key), "%s.op", prefix); + ret = dict_get_str (dict, key, &op); + if (ret) + return; + */ + + snprintf(key, sizeof(key), "%s.count", prefix); + ret = dict_get_int32(dict, key, &count); + if (ret) + return; + + cli_out(" UID : %d", uid); + cli_out(" GID : %d", gid); + cli_out(" PID : %d", pid); + cli_out(" Unique : %" PRIu64, unique); + // cli_out ("\tOp : %s", op); + cli_out(" Frames : %d", count); + + for (i = 0; i < count; i++) { + cli_out(" Frame %d", i + 1); + + snprintf(key, sizeof(key), "%s.frame%d", prefix, i); + cli_print_volume_status_call_frame(dict, key); + } + + cli_out(" "); +} + +static void +cli_print_volume_status_callpool(dict_t *dict, gf_boolean_t notbrick) +{ + int ret = -1; + char *volname = NULL; + int brick_index_max = -1; + int other_count = 0; + int index_max = 0; + char *hostname = NULL; + char *path = NULL; + int online = -1; + int call_count = 0; + char key[64] = { + 0, + }; + int i = 0; + int j = 0; + + GF_ASSERT(dict); + + ret = dict_get_str_sizen(dict, "volname", &volname); + if (ret) + goto out; + cli_out("Pending calls for volume %s", volname); + + ret = dict_get_int32_sizen(dict, "brick-index-max", &brick_index_max); + if (ret) + goto out; + ret = dict_get_int32_sizen(dict, "other-count", &other_count); + if (ret) + goto out; + + index_max = brick_index_max + other_count; + + for (i = 0; i <= index_max; i++) { + cli_out("----------------------------------------------"); + + snprintf(key, sizeof(key), "brick%d.hostname", i); + ret = dict_get_str(dict, key, &hostname); + if (ret) + goto out; + snprintf(key, sizeof(key), "brick%d.path", i); + ret = dict_get_str(dict, key, &path); + if (ret) + goto out; + + if (notbrick) + cli_out("%s : %s", hostname, path); + else + cli_out("Brick : %s:%s", hostname, path); + + snprintf(key, sizeof(key), "brick%d.status", i); + ret = dict_get_int32(dict, key, &online); + if (ret) + goto out; + if (!online) { + if (notbrick) + cli_out("%s is offline", hostname); + else + cli_out("Brick is offline"); + continue; + } + + snprintf(key, sizeof(key), "brick%d.callpool.count", i); + ret = dict_get_int32(dict, key, &call_count); + if (ret) + goto out; + cli_out("Pending calls: %d", call_count); + + if (0 == call_count) + continue; + + for (j = 0; j < call_count; j++) { + cli_out("Call Stack%d", j + 1); + + snprintf(key, sizeof(key), "brick%d.callpool.stack%d", i, j); + cli_print_volume_status_call_stack(dict, key); + } + } + +out: + cli_out("----------------------------------------------"); + return; +} + +static void +cli_print_volume_status_tasks(dict_t *dict) +{ + int ret = -1; + int i = 0; + int j = 0; + int count = 0; + int task_count = 0; + int status = 0; + char *op = NULL; + char *task_id_str = NULL; + char *volname = NULL; + char key[64] = { + 0, + }; + char task[32] = { + 0, + }; + char *brick = NULL; + + ret = dict_get_int32_sizen(dict, "tasks", &task_count); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get tasks count"); + return; + } + + ret = dict_get_str_sizen(dict, "volname", &volname); + if (ret) + goto out; + + cli_out("Task Status of Volume %s", volname); + cli_print_line(CLI_BRICK_STATUS_LINE_LEN); + + if (task_count == 0) { + cli_out("There are no active volume tasks"); + cli_out(" "); + return; + } + + for (i = 0; i < task_count; i++) { + snprintf(key, sizeof(key), "task%d.type", i); + ret = dict_get_str(dict, key, &op); + if (ret) + return; + cli_out("%-20s : %-20s", "Task", op); + + snprintf(key, sizeof(key), "task%d.id", i); + ret = dict_get_str(dict, key, &task_id_str); + if (ret) + return; + cli_out("%-20s : %-20s", "ID", task_id_str); + + snprintf(key, sizeof(key), "task%d.status", i); + ret = dict_get_int32(dict, key, &status); + if (ret) + return; + + snprintf(task, sizeof(task), "task%d", i); + + if (!strcmp(op, "Remove brick")) { + snprintf(key, sizeof(key), "%s.count", task); + ret = dict_get_int32(dict, key, &count); + if (ret) + goto out; + + cli_out("%-20s", "Removed bricks:"); + + for (j = 1; j <= count; j++) { + snprintf(key, sizeof(key), "%s.brick%d", task, j); + ret = dict_get_str(dict, key, &brick); + if (ret) + goto out; + + cli_out("%-20s", brick); + } + } + cli_out("%-20s : %-20s", "Status", cli_vol_task_status_str[status]); + cli_out(" "); + } + +out: + return; +} + +static int +gf_cli_status_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + int ret = -1; + int brick_index_max = -1; + int other_count = 0; + int index_max = 0; + int i = 0; + int type = -1; + int hot_brick_count = -1; + int pid = -1; + uint32_t cmd = 0; + gf_boolean_t notbrick = _gf_false; + char key[64] = { + 0, + }; + char *hostname = NULL; + char *path = NULL; + char *volname = NULL; + dict_t *dict = NULL; + gf_cli_rsp rsp = { + 0, + }; + cli_volume_status_t status = {0}; + cli_local_t *local = NULL; + gf_boolean_t wipe_local = _gf_false; + char msg[1024] = { + 0, + }; + + GF_ASSERT(myframe); + + if (req->rpc_status == -1) + goto out; + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + goto out; + } + + gf_log("cli", GF_LOG_DEBUG, "Received response to status cmd"); + + local = ((call_frame_t *)myframe)->local; + if (!local) { + local = cli_local_get(); + if (!local) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Failed to get local"); + goto out; + } + wipe_local = _gf_true; + } + + if (rsp.op_ret) { + if (strcmp(rsp.op_errstr, "")) + snprintf(msg, sizeof(msg), "%s", rsp.op_errstr); + else + snprintf(msg, sizeof(msg), + "Unable to obtain volume status information."); + + if (global_state->mode & GLUSTER_MODE_XML) { + if (!local->all) + cli_xml_output_str("volStatus", msg, rsp.op_ret, rsp.op_errno, + rsp.op_errstr); + ret = 0; + goto out; + } + + cli_err("%s", msg); + if (local && local->all) { + ret = 0; + cli_out(" "); + } else + ret = -1; + + goto out; + } + + dict = dict_new(); + if (!dict) { + gf_log(THIS->name, GF_LOG_ERROR, "Failed to create the dict"); + ret = -1; + goto out; + } + + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, &dict); + if (ret) + goto out; + + ret = dict_get_uint32(dict, "cmd", &cmd); + if (ret) + goto out; + + if ((cmd & GF_CLI_STATUS_ALL)) { + if (local && local->dict) { + dict_ref(dict); + ret = dict_set_static_ptr(local->dict, "rsp-dict", dict); + ret = 0; + } else { + gf_log("cli", GF_LOG_ERROR, "local not found"); + ret = -1; + } + goto out; + } + + if (global_state->mode & GLUSTER_MODE_XML) { + if (!local->all) { + ret = cli_xml_output_vol_status_begin(local, rsp.op_ret, + rsp.op_errno, rsp.op_errstr); + if (ret) { + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto xml_end; + } + } + if (cmd & GF_CLI_STATUS_TASKS) { + ret = cli_xml_output_vol_status_tasks_detail(local, dict); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Error outputting to xml"); + goto xml_end; + } + } else { + ret = cli_xml_output_vol_status(local, dict); + if (ret) { + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto xml_end; + } + } + + xml_end: + if (!local->all) { + ret = cli_xml_output_vol_status_end(local); + if (ret) { + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + } + } + goto out; + } + + if ((cmd & GF_CLI_STATUS_NFS) || (cmd & GF_CLI_STATUS_SHD) || + (cmd & GF_CLI_STATUS_QUOTAD) || (cmd & GF_CLI_STATUS_SNAPD) || + (cmd & GF_CLI_STATUS_BITD) || (cmd & GF_CLI_STATUS_SCRUB)) + notbrick = _gf_true; + + switch (cmd & GF_CLI_STATUS_MASK) { + case GF_CLI_STATUS_MEM: + cli_print_volume_status_mem(dict, notbrick); + goto cont; + break; + case GF_CLI_STATUS_CLIENTS: + cli_print_volume_status_clients(dict, notbrick); + goto cont; + break; + case GF_CLI_STATUS_CLIENT_LIST: + cli_print_volume_status_client_list(dict, notbrick); + goto cont; + break; + case GF_CLI_STATUS_INODE: + cli_print_volume_status_inode(dict, notbrick); + goto cont; + break; + case GF_CLI_STATUS_FD: + cli_print_volume_status_fd(dict, notbrick); + goto cont; + break; + case GF_CLI_STATUS_CALLPOOL: + cli_print_volume_status_callpool(dict, notbrick); + goto cont; + break; + case GF_CLI_STATUS_TASKS: + cli_print_volume_status_tasks(dict); + goto cont; + break; + default: + break; + } + + ret = dict_get_str_sizen(dict, "volname", &volname); + if (ret) + goto out; + + ret = dict_get_int32_sizen(dict, "brick-index-max", &brick_index_max); + if (ret) + goto out; + + ret = dict_get_int32_sizen(dict, "other-count", &other_count); + if (ret) + goto out; + + index_max = brick_index_max + other_count; + + ret = dict_get_int32_sizen(dict, "type", &type); + if (ret) + goto out; + + ret = dict_get_int32_sizen(dict, "hot_brick_count", &hot_brick_count); + if (ret) + goto out; + + cli_out("Status of volume: %s", volname); + + if ((cmd & GF_CLI_STATUS_DETAIL) == 0) { + cli_out("%-*s %s %s %s %s", CLI_VOL_STATUS_BRICK_LEN, + "Gluster process", "TCP Port", "RDMA Port", "Online", "Pid"); + cli_print_line(CLI_BRICK_STATUS_LINE_LEN); + } + + status.brick = GF_MALLOC(PATH_MAX + 256, gf_common_mt_strdup); + if (!status.brick) { + errno = ENOMEM; + ret = -1; + goto out; + } + + for (i = 0; i <= index_max; i++) { + status.rdma_port = 0; + + snprintf(key, sizeof(key), "brick%d.hostname", i); + ret = dict_get_str(dict, key, &hostname); + if (ret) + continue; + + snprintf(key, sizeof(key), "brick%d.path", i); + ret = dict_get_str(dict, key, &path); + if (ret) + continue; + + /* Brick/not-brick is handled separately here as all + * types of nodes are contained in the default output + */ + status.brick[0] = '\0'; + if (!strcmp(hostname, "NFS Server") || + !strcmp(hostname, "Self-heal Daemon") || + !strcmp(hostname, "Quota Daemon") || + !strcmp(hostname, "Snapshot Daemon") || + !strcmp(hostname, "Scrubber Daemon") || + !strcmp(hostname, "Bitrot Daemon")) + snprintf(status.brick, PATH_MAX + 255, "%s on %s", hostname, path); + else { + snprintf(key, sizeof(key), "brick%d.rdma_port", i); + ret = dict_get_int32(dict, key, &(status.rdma_port)); + if (ret) + continue; + snprintf(status.brick, PATH_MAX + 255, "Brick %s:%s", hostname, + path); + } + + snprintf(key, sizeof(key), "brick%d.port", i); + ret = dict_get_int32(dict, key, &(status.port)); + if (ret) + continue; + + snprintf(key, sizeof(key), "brick%d.status", i); + ret = dict_get_int32(dict, key, &(status.online)); + if (ret) + continue; + + snprintf(key, sizeof(key), "brick%d.pid", i); + ret = dict_get_int32(dict, key, &pid); + if (ret) + continue; + if (pid == -1) + ret = gf_asprintf(&(status.pid_str), "%s", "N/A"); + else + ret = gf_asprintf(&(status.pid_str), "%d", pid); + + if (ret == -1) + goto out; + + if ((cmd & GF_CLI_STATUS_DETAIL)) { + ret = cli_get_detail_status(dict, i, &status); + if (ret) + goto out; + cli_print_line(CLI_BRICK_STATUS_LINE_LEN); + cli_print_detailed_status(&status); + } else { + cli_print_brick_status(&status); + } + + /* Allocatated memory using gf_asprintf*/ + GF_FREE(status.pid_str); + } + cli_out(" "); + + if ((cmd & GF_CLI_STATUS_MASK) == GF_CLI_STATUS_NONE) + cli_print_volume_status_tasks(dict); +cont: + ret = rsp.op_ret; + +out: + if (dict) + dict_unref(dict); + GF_FREE(status.brick); + if (local && wipe_local) { + cli_local_wipe(local); + } + + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + return ret; +} + +static int32_t +gf_cli_status_volume(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + int ret = -1; + dict_t *dict = NULL; + + dict = data; + + ret = cli_to_glusterd(&req, frame, gf_cli_status_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, + GLUSTER_CLI_STATUS_VOLUME, this, cli_rpc_prog, NULL); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + GF_FREE(req.dict.dict_val); + return ret; +} + +static int +gf_cli_status_volume_all(call_frame_t *frame, xlator_t *this, void *data) +{ + int i = 0; + int ret = -1; + int vol_count = -1; + uint32_t cmd = 0; + char key[1024] = {0}; + char *volname = NULL; + void *vol_dict = NULL; + dict_t *dict = NULL; + cli_local_t *local = NULL; + + if (!frame) + goto out; + + if (!frame->local) + goto out; + + local = frame->local; + + ret = dict_get_uint32(local->dict, "cmd", &cmd); + if (ret) + goto out; + + local->all = _gf_true; + + ret = gf_cli_status_volume(frame, this, data); + + if (ret) + goto out; + + ret = dict_get_ptr(local->dict, "rsp-dict", &vol_dict); + if (ret) + goto out; + + ret = dict_get_int32_sizen((dict_t *)vol_dict, "vol_count", &vol_count); + if (ret) { + cli_err("Failed to get names of volumes"); + goto out; + } + + /* remove the "all" flag in cmd */ + cmd &= ~GF_CLI_STATUS_ALL; + cmd |= GF_CLI_STATUS_VOL; + + if (global_state->mode & GLUSTER_MODE_XML) { + // TODO: Pass proper op_* values + ret = cli_xml_output_vol_status_begin(local, 0, 0, NULL); + if (ret) { + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto xml_end; + } + } + + if (vol_count == 0 && !(global_state->mode & GLUSTER_MODE_XML)) { + cli_err("No volumes present"); + ret = 0; + goto out; + } + + for (i = 0; i < vol_count; i++) { + dict = dict_new(); + if (!dict) + goto out; + + ret = snprintf(key, sizeof(key), "vol%d", i); + ret = dict_get_strn(vol_dict, key, ret, &volname); + if (ret) + goto out; + + ret = dict_set_str_sizen(dict, "volname", volname); + if (ret) + goto out; + + ret = dict_set_uint32(dict, "cmd", cmd); + if (ret) + goto out; + + ret = gf_cli_status_volume(frame, this, dict); + if (ret) + goto out; + + dict_unref(dict); + } + +xml_end: + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_vol_status_end(local); + } + +out: + if (ret) + gf_log("cli", GF_LOG_ERROR, "status all failed"); + + if (vol_dict) + dict_unref(vol_dict); + + if (ret && dict) + dict_unref(dict); + + if (local) + cli_local_wipe(local); + + if (frame) + frame->local = NULL; + + return ret; +} + +static int +gf_cli_mount_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf1_cli_mount_rsp rsp = { + 0, + }; + int ret = -1; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf1_cli_mount_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + goto out; + } + + gf_log("cli", GF_LOG_INFO, "Received resp to mount"); + + if (rsp.op_ret == 0) { + ret = 0; + cli_out("%s", rsp.path); + } else { + /* weird sounding but easy to parse... */ + cli_err("%d : failed with this errno (%s)", rsp.op_errno, + strerror(rsp.op_errno)); + ret = -1; + } + +out: + cli_cmd_broadcast_response(ret); + if (rsp.path) { + free(rsp.path); + } + return ret; +} + +static int32_t +gf_cli_mount(call_frame_t *frame, xlator_t *this, void *data) +{ + gf1_cli_mount_req req = { + 0, + }; + int ret = -1; + void **dataa = data; + char *label = NULL; + dict_t *dict = NULL; + + if (!frame || !this || !data) + goto out; + + label = dataa[0]; + dict = dataa[1]; + + req.label = label; + ret = dict_allocate_and_serialize(dict, &req.dict.dict_val, + &req.dict.dict_len); + if (ret) { + ret = -1; + goto out; + } + + ret = cli_cmd_submit(NULL, &req, frame, cli_rpc_prog, GLUSTER_CLI_MOUNT, + NULL, this, gf_cli_mount_cbk, + (xdrproc_t)xdr_gf1_cli_mount_req); + +out: + GF_FREE(req.dict.dict_val); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + return ret; +} + +static int +gf_cli_umount_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf1_cli_umount_rsp rsp = { + 0, + }; + int ret = -1; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf1_cli_umount_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + goto out; + } + + gf_log("cli", GF_LOG_INFO, "Received resp to mount"); + + if (rsp.op_ret == 0) + ret = 0; + else { + cli_err("umount failed"); + ret = -1; + } + +out: + cli_cmd_broadcast_response(ret); + return ret; +} + +static int32_t +gf_cli_umount(call_frame_t *frame, xlator_t *this, void *data) +{ + gf1_cli_umount_req req = { + 0, + }; + int ret = -1; + dict_t *dict = NULL; + + if (!frame || !this || !data) + goto out; + + dict = data; + + ret = dict_get_str_sizen(dict, "path", &req.path); + if (ret == 0) + ret = dict_get_int32_sizen(dict, "lazy", &req.lazy); + + if (ret) { + ret = -1; + goto out; + } + + ret = cli_cmd_submit(NULL, &req, frame, cli_rpc_prog, GLUSTER_CLI_UMOUNT, + NULL, this, gf_cli_umount_cbk, + (xdrproc_t)xdr_gf1_cli_umount_req); + +out: + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + return ret; +} + +static void +cmd_heal_volume_statistics_out(dict_t *dict, int brick) +{ + uint64_t num_entries = 0; + int ret = 0; + char key[256] = {0}; + char *hostname = NULL; + uint64_t i = 0; + uint64_t healed_count = 0; + uint64_t split_brain_count = 0; + uint64_t heal_failed_count = 0; + char *start_time_str = NULL; + char *end_time_str = NULL; + char *crawl_type = NULL; + int progress = -1; + + snprintf(key, sizeof key, "%d-hostname", brick); + ret = dict_get_str(dict, key, &hostname); + if (ret) + goto out; + cli_out("------------------------------------------------"); + cli_out("\nCrawl statistics for brick no %d", brick); + cli_out("Hostname of brick %s", hostname); + + snprintf(key, sizeof key, "statistics-%d-count", brick); + ret = dict_get_uint64(dict, key, &num_entries); + if (ret) + goto out; + + for (i = 0; i < num_entries; i++) { + snprintf(key, sizeof key, "statistics_crawl_type-%d-%" PRIu64, brick, + i); + ret = dict_get_str(dict, key, &crawl_type); + if (ret) + goto out; + + snprintf(key, sizeof key, "statistics_healed_cnt-%d-%" PRIu64, brick, + i); + ret = dict_get_uint64(dict, key, &healed_count); + if (ret) + goto out; + + snprintf(key, sizeof key, "statistics_sb_cnt-%d-%" PRIu64, brick, i); + ret = dict_get_uint64(dict, key, &split_brain_count); + if (ret) + goto out; + snprintf(key, sizeof key, "statistics_heal_failed_cnt-%d-%" PRIu64, + brick, i); + ret = dict_get_uint64(dict, key, &heal_failed_count); + if (ret) + goto out; + snprintf(key, sizeof key, "statistics_strt_time-%d-%" PRIu64, brick, i); + ret = dict_get_str(dict, key, &start_time_str); + if (ret) + goto out; + snprintf(key, sizeof key, "statistics_end_time-%d-%" PRIu64, brick, i); + ret = dict_get_str(dict, key, &end_time_str); + if (ret) + goto out; + snprintf(key, sizeof key, "statistics_inprogress-%d-%" PRIu64, brick, + i); + ret = dict_get_int32(dict, key, &progress); + if (ret) + goto out; + + cli_out("\nStarting time of crawl: %s", start_time_str); + if (progress == 1) + cli_out("Crawl is in progress"); + else + cli_out("Ending time of crawl: %s", end_time_str); + + cli_out("Type of crawl: %s", crawl_type); + cli_out("No. of entries healed: %" PRIu64, healed_count); + cli_out("No. of entries in split-brain: %" PRIu64, split_brain_count); + cli_out("No. of heal failed entries: %" PRIu64, heal_failed_count); + } + +out: + return; +} + +static void +cmd_heal_volume_brick_out(dict_t *dict, int brick) +{ + uint64_t num_entries = 0; + int ret = 0; + char key[64] = {0}; + char *hostname = NULL; + char *path = NULL; + char *status = NULL; + uint64_t i = 0; + uint32_t time = 0; + char timestr[GF_TIMESTR_SIZE] = {0}; + char *shd_status = NULL; + + snprintf(key, sizeof key, "%d-hostname", brick); + ret = dict_get_str(dict, key, &hostname); + if (ret) + goto out; + snprintf(key, sizeof key, "%d-path", brick); + ret = dict_get_str(dict, key, &path); + if (ret) + goto out; + cli_out("\nBrick %s:%s", hostname, path); + + snprintf(key, sizeof key, "%d-status", brick); + ret = dict_get_str(dict, key, &status); + if (status && status[0] != '\0') + cli_out("Status: %s", status); + + snprintf(key, sizeof key, "%d-shd-status", brick); + ret = dict_get_str(dict, key, &shd_status); + + if (!shd_status) { + snprintf(key, sizeof key, "%d-count", brick); + ret = dict_get_uint64(dict, key, &num_entries); + cli_out("Number of entries: %" PRIu64, num_entries); + + for (i = 0; i < num_entries; i++) { + snprintf(key, sizeof key, "%d-%" PRIu64, brick, i); + ret = dict_get_str(dict, key, &path); + if (ret) + continue; + time = 0; + snprintf(key, sizeof key, "%d-%" PRIu64 "-time", brick, i); + ret = dict_get_uint32(dict, key, &time); + if (ret || !time) { + cli_out("%s", path); + } else { + gf_time_fmt(timestr, sizeof timestr, time, gf_timefmt_FT); + if (i == 0) { + cli_out("at path on brick"); + cli_out("-----------------------------------"); + } + cli_out("%s %s", timestr, path); + } + } + } + +out: + return; +} + +static void +cmd_heal_volume_statistics_heal_count_out(dict_t *dict, int brick) +{ + uint64_t num_entries = 0; + int ret = 0; + char key[64] = {0}; + char *hostname = NULL; + char *path = NULL; + char *status = NULL; + char *shd_status = NULL; + + snprintf(key, sizeof key, "%d-hostname", brick); + ret = dict_get_str(dict, key, &hostname); + if (ret) + goto out; + snprintf(key, sizeof key, "%d-path", brick); + ret = dict_get_str(dict, key, &path); + if (ret) + goto out; + cli_out("\nBrick %s:%s", hostname, path); + + snprintf(key, sizeof key, "%d-status", brick); + ret = dict_get_str(dict, key, &status); + if (status && strlen(status)) + cli_out("Status: %s", status); + + snprintf(key, sizeof key, "%d-shd-status", brick); + ret = dict_get_str(dict, key, &shd_status); + + if (!shd_status) { + snprintf(key, sizeof key, "%d-hardlinks", brick); + ret = dict_get_uint64(dict, key, &num_entries); + if (ret) + cli_out("No gathered input for this brick"); + else + cli_out("Number of entries: %" PRIu64, num_entries); + } + +out: + return; +} + +static int +gf_is_cli_heal_get_command(gf_xl_afr_op_t heal_op) +{ + /* If the command is get command value is 1 otherwise 0, for + invalid commands -1 */ + static int get_cmds[GF_SHD_OP_HEAL_DISABLE + 1] = { + [GF_SHD_OP_INVALID] = -1, + [GF_SHD_OP_HEAL_INDEX] = 0, + [GF_SHD_OP_HEAL_FULL] = 0, + [GF_SHD_OP_INDEX_SUMMARY] = 1, + [GF_SHD_OP_SPLIT_BRAIN_FILES] = 1, + [GF_SHD_OP_STATISTICS] = 1, + [GF_SHD_OP_STATISTICS_HEAL_COUNT] = 1, + [GF_SHD_OP_STATISTICS_HEAL_COUNT_PER_REPLICA] = 1, + [GF_SHD_OP_HEAL_ENABLE] = 0, + [GF_SHD_OP_HEAL_DISABLE] = 0, + }; + + if (heal_op > GF_SHD_OP_INVALID && heal_op <= GF_SHD_OP_HEAL_DISABLE) + return get_cmds[heal_op] == 1; + return _gf_false; +} + +int +gf_cli_heal_volume_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + cli_local_t *local = NULL; + char *volname = NULL; + call_frame_t *frame = NULL; + dict_t *dict = NULL; + int brick_count = 0; + int i = 0; + gf_xl_afr_op_t heal_op = GF_SHD_OP_INVALID; + const char *operation = NULL; + const char *substr = NULL; + const char *heal_op_str = NULL; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) { + goto out; + } + + frame = myframe; + + GF_ASSERT(frame->local); + + local = frame->local; + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(frame->this->name, GF_LOG_ERROR, XDR_DECODE_FAIL); + goto out; + } + + ret = dict_get_int32_sizen(local->dict, "heal-op", (int32_t *)&heal_op); + // TODO: Proper XML output + //#if (HAVE_LIB_XML) + // if (global_state->mode & GLUSTER_MODE_XML) { + // ret = cli_xml_output_dict ("volHeal", dict, rsp.op_ret, + // rsp.op_errno, rsp.op_errstr); + // if (ret) + // gf_log ("cli", GF_LOG_ERROR, XML_ERROR); + // goto out; + // } + //#endif + + ret = dict_get_str_sizen(local->dict, "volname", &volname); + if (ret) { + gf_log(frame->this->name, GF_LOG_ERROR, "failed to get volname"); + goto out; + } + + gf_log("cli", GF_LOG_INFO, "Received resp to heal volume"); + + operation = "Gathering "; + substr = ""; + switch (heal_op) { + case GF_SHD_OP_HEAL_INDEX: + operation = "Launching heal operation "; + heal_op_str = "to perform index self heal"; + substr = "\nUse heal info commands to check status."; + break; + case GF_SHD_OP_HEAL_FULL: + operation = "Launching heal operation "; + heal_op_str = "to perform full self heal"; + substr = "\nUse heal info commands to check status."; + break; + case GF_SHD_OP_INDEX_SUMMARY: + heal_op_str = "list of entries to be healed"; + break; + case GF_SHD_OP_SPLIT_BRAIN_FILES: + heal_op_str = "list of split brain entries"; + break; + case GF_SHD_OP_STATISTICS: + heal_op_str = "crawl statistics"; + break; + case GF_SHD_OP_STATISTICS_HEAL_COUNT: + heal_op_str = "count of entries to be healed"; + break; + case GF_SHD_OP_STATISTICS_HEAL_COUNT_PER_REPLICA: + heal_op_str = "count of entries to be healed per replica"; + break; + case GF_SHD_OP_SBRAIN_HEAL_FROM_BIGGER_FILE: + case GF_SHD_OP_SBRAIN_HEAL_FROM_LATEST_MTIME: + case GF_SHD_OP_SBRAIN_HEAL_FROM_BRICK: + case GF_SHD_OP_HEAL_SUMMARY: + case GF_SHD_OP_HEALED_FILES: + case GF_SHD_OP_HEAL_FAILED_FILES: + /* These cases are never hit; they're coded just to silence the + * compiler warnings.*/ + break; + + case GF_SHD_OP_INVALID: + heal_op_str = "invalid heal op"; + break; + case GF_SHD_OP_HEAL_ENABLE: + operation = ""; + heal_op_str = "Enable heal"; + break; + case GF_SHD_OP_HEAL_DISABLE: + operation = ""; + heal_op_str = "Disable heal"; + break; + case GF_SHD_OP_GRANULAR_ENTRY_HEAL_ENABLE: + operation = ""; + heal_op_str = "Enable granular entry heal"; + break; + case GF_SHD_OP_GRANULAR_ENTRY_HEAL_DISABLE: + operation = ""; + heal_op_str = "Disable granular entry heal"; + break; + } + + if (rsp.op_ret) { + if (strcmp(rsp.op_errstr, "")) { + cli_err("%s%s on volume %s has been unsuccessful:", operation, + heal_op_str, volname); + cli_err("%s", rsp.op_errstr); + } + ret = rsp.op_ret; + goto out; + } else { + cli_out("%s%s on volume %s has been successful %s", operation, + heal_op_str, volname, substr); + } + + ret = rsp.op_ret; + if (!gf_is_cli_heal_get_command(heal_op)) + goto out; + + dict = dict_new(); + if (!dict) { + ret = -1; + goto out; + } + + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, &dict); + + if (ret) { + gf_log("", GF_LOG_ERROR, DICT_UNSERIALIZE_FAIL); + goto out; + } + + ret = dict_get_int32_sizen(dict, "count", &brick_count); + if (ret) + goto out; + + if (!brick_count) { + cli_err("All bricks of volume %s are down.", volname); + ret = -1; + goto out; + } + + switch (heal_op) { + case GF_SHD_OP_STATISTICS: + for (i = 0; i < brick_count; i++) + cmd_heal_volume_statistics_out(dict, i); + break; + case GF_SHD_OP_STATISTICS_HEAL_COUNT: + case GF_SHD_OP_STATISTICS_HEAL_COUNT_PER_REPLICA: + for (i = 0; i < brick_count; i++) + cmd_heal_volume_statistics_heal_count_out(dict, i); + break; + case GF_SHD_OP_INDEX_SUMMARY: + case GF_SHD_OP_SPLIT_BRAIN_FILES: + for (i = 0; i < brick_count; i++) + cmd_heal_volume_brick_out(dict, i); + break; + default: + break; + } + + ret = rsp.op_ret; + +out: + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + if (dict) + dict_unref(dict); + return ret; +} + +static int32_t +gf_cli_heal_volume(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + int ret = 0; + dict_t *dict = NULL; + + dict = data; + + ret = cli_to_glusterd(&req, frame, gf_cli_heal_volume_cbk, + (xdrproc_t)xdr_gf_cli_req, dict, + GLUSTER_CLI_HEAL_VOLUME, this, cli_rpc_prog, NULL); + + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + + GF_FREE(req.dict.dict_val); + + return ret; +} + +static int32_t +gf_cli_statedump_volume_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + char msg[1024] = "Volume statedump successful"; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) + goto out; + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + goto out; + } + gf_log("cli", GF_LOG_DEBUG, "Received response to statedump"); + if (rsp.op_ret) + snprintf(msg, sizeof(msg), "%s", rsp.op_errstr); + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_str("volStatedump", msg, rsp.op_ret, rsp.op_errno, + rsp.op_errstr); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + + if (rsp.op_ret) + cli_err("volume statedump: failed: %s", msg); + else + cli_out("volume statedump: success"); + ret = rsp.op_ret; + +out: + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + return ret; +} + +static int32_t +gf_cli_statedump_volume(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + dict_t *options = NULL; + int ret = -1; + + options = data; + + ret = cli_to_glusterd( + &req, frame, gf_cli_statedump_volume_cbk, (xdrproc_t)xdr_gf_cli_req, + options, GLUSTER_CLI_STATEDUMP_VOLUME, this, cli_rpc_prog, NULL); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + + GF_FREE(req.dict.dict_val); + return ret; +} + +static int32_t +gf_cli_list_volume_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + int ret = -1; + gf_cli_rsp rsp = { + 0, + }; + dict_t *dict = NULL; + int vol_count = 0; + ; + char *volname = NULL; + char key[1024] = { + 0, + }; + int i = 0; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) + goto out; + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + goto out; + } + + dict = dict_new(); + if (!dict) { + ret = -1; + goto out; + } + + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, &dict); + if (ret) { + gf_log("cli", GF_LOG_ERROR, DICT_UNSERIALIZE_FAIL); + goto out; + } + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_vol_list(dict, rsp.op_ret, rsp.op_errno, + rsp.op_errstr); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + + if (rsp.op_ret) + cli_err("%s", rsp.op_errstr); + else { + ret = dict_get_int32_sizen(dict, "count", &vol_count); + if (ret) + goto out; + + if (vol_count == 0) { + cli_err("No volumes present in cluster"); + goto out; + } + for (i = 0; i < vol_count; i++) { + ret = snprintf(key, sizeof(key), "volume%d", i); + ret = dict_get_strn(dict, key, ret, &volname); + if (ret) + goto out; + cli_out("%s", volname); + } + } + + ret = rsp.op_ret; + +out: + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + + if (dict) + dict_unref(dict); + return ret; +} + +static int32_t +gf_cli_list_volume(call_frame_t *frame, xlator_t *this, void *data) +{ + int ret = -1; + gf_cli_req req = {{ + 0, + }}; + + ret = cli_cmd_submit(NULL, &req, frame, cli_rpc_prog, + GLUSTER_CLI_LIST_VOLUME, NULL, this, + gf_cli_list_volume_cbk, (xdrproc_t)xdr_gf_cli_req); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + return ret; +} + +static int32_t +gf_cli_clearlocks_volume_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + char *lk_summary = NULL; + dict_t *dict = NULL; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) + goto out; + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + goto out; + } + gf_log("cli", GF_LOG_DEBUG, "Received response to clear-locks"); + + if (rsp.op_ret) { + cli_err("Volume clear-locks unsuccessful"); + cli_err("%s", rsp.op_errstr); + + } else { + if (!rsp.dict.dict_len) { + cli_err("Possibly no locks cleared"); + ret = 0; + goto out; + } + + dict = dict_new(); + + if (!dict) { + ret = -1; + goto out; + } + + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, &dict); + + if (ret) { + gf_log("cli", GF_LOG_ERROR, DICT_UNSERIALIZE_FAIL); + goto out; + } + + ret = dict_get_str(dict, "lk-summary", &lk_summary); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Unable to get lock summary from dictionary"); + goto out; + } + cli_out("Volume clear-locks successful"); + cli_out("%s", lk_summary); + } + + ret = rsp.op_ret; + +out: + if (dict) + dict_unref(dict); + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + return ret; +} + +static int32_t +gf_cli_clearlocks_volume(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + dict_t *options = NULL; + int ret = -1; + + options = data; + + ret = cli_to_glusterd( + &req, frame, gf_cli_clearlocks_volume_cbk, (xdrproc_t)xdr_gf_cli_req, + options, GLUSTER_CLI_CLRLOCKS_VOLUME, this, cli_rpc_prog, NULL); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + + GF_FREE(req.dict.dict_val); + return ret; +} + +static int32_t +cli_snapshot_remove_reply(gf_cli_rsp *rsp, dict_t *dict, call_frame_t *frame) +{ + int32_t ret = -1; + char *snap_name = NULL; + int32_t delete_cmd = -1; + cli_local_t *local = NULL; + + GF_ASSERT(frame); + GF_ASSERT(rsp); + GF_ASSERT(dict); + + local = frame->local; + + ret = dict_get_int32_sizen(dict, "sub-cmd", &delete_cmd); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not get sub-cmd"); + goto end; + } + + if ((global_state->mode & GLUSTER_MODE_XML) && + (delete_cmd == GF_SNAP_DELETE_TYPE_SNAP)) { + ret = cli_xml_output_snap_delete_begin(local, rsp->op_ret, + rsp->op_errno, rsp->op_errstr); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to create xml output for delete"); + goto end; + } + } + + if (rsp->op_ret && !(global_state->mode & GLUSTER_MODE_XML)) { + cli_err("snapshot delete: failed: %s", + rsp->op_errstr ? rsp->op_errstr + : "Please check log file for details"); + ret = rsp->op_ret; + goto out; + } + + if (delete_cmd == GF_SNAP_DELETE_TYPE_ALL || + delete_cmd == GF_SNAP_DELETE_TYPE_VOL) { + local = ((call_frame_t *)frame)->local; + if (!local) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "frame->local is NULL"); + goto out; + } + + /* During first call back of snapshot delete of type + * ALL and VOL, We will get the snapcount and snapnames. + * Hence to make the subsequent rpc calls for individual + * snapshot delete, We need to save it in local dictionary. + */ + dict_copy(dict, local->dict); + ret = 0; + goto out; + } + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_snapshot_delete(local, dict, rsp); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to create xml output for snapshot delete command"); + goto out; + } + /* Error out in case of the op already failed */ + if (rsp->op_ret) { + ret = rsp->op_ret; + goto out; + } + } else { + ret = dict_get_str_sizen(dict, "snapname", &snap_name); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get snapname"); + goto out; + } + + cli_out("snapshot delete: %s: snap removed successfully", snap_name); + } + ret = 0; + +out: + if ((global_state->mode & GLUSTER_MODE_XML) && + (delete_cmd == GF_SNAP_DELETE_TYPE_SNAP)) { + ret = cli_xml_output_snap_delete_end(local); + } +end: + return ret; +} + +static int +cli_snapshot_config_display(dict_t *dict, gf_cli_rsp *rsp) +{ + char buf[PATH_MAX] = ""; + char *volname = NULL; + int ret = -1; + int config_command = 0; + uint64_t value = 0; + uint64_t hard_limit = 0; + uint64_t soft_limit = 0; + uint64_t i = 0; + uint64_t voldisplaycount = 0; + char *auto_delete = NULL; + char *snap_activate = NULL; + + GF_ASSERT(dict); + GF_ASSERT(rsp); + + if (rsp->op_ret) { + cli_err("Snapshot Config : failed: %s", + rsp->op_errstr ? rsp->op_errstr + : "Please check log file for details"); + ret = rsp->op_ret; + goto out; + } + + ret = dict_get_int32_sizen(dict, "config-command", &config_command); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not fetch config type"); + goto out; + } + + ret = dict_get_uint64(dict, "snap-max-hard-limit", &hard_limit); + /* Ignore the error, as the key specified is optional */ + ret = dict_get_uint64(dict, "snap-max-soft-limit", &soft_limit); + + ret = dict_get_str_sizen(dict, "auto-delete", &auto_delete); + + ret = dict_get_str_sizen(dict, "snap-activate-on-create", &snap_activate); + + if (!hard_limit && !soft_limit && + config_command != GF_SNAP_CONFIG_DISPLAY && !auto_delete && + !snap_activate) { + ret = -1; + gf_log(THIS->name, GF_LOG_ERROR, "Could not fetch config-key"); + goto out; + } + + ret = dict_get_str_sizen(dict, "volname", &volname); + /* Ignore the error, as volname is optional */ + + if (!volname) { + volname = "System"; + } + + switch (config_command) { + case GF_SNAP_CONFIG_TYPE_SET: + if (hard_limit && soft_limit) { + cli_out( + "snapshot config: snap-max-hard-limit " + "& snap-max-soft-limit for system set successfully"); + } else if (hard_limit) { + cli_out( + "snapshot config: snap-max-hard-limit " + "for %s set successfully", + volname); + } else if (soft_limit) { + cli_out( + "snapshot config: snap-max-soft-limit " + "for %s set successfully", + volname); + } else if (auto_delete) { + cli_out("snapshot config: auto-delete successfully set"); + } else if (snap_activate) { + cli_out("snapshot config: activate-on-create successfully set"); + } + break; + + case GF_SNAP_CONFIG_DISPLAY: + cli_out("\nSnapshot System Configuration:"); + ret = dict_get_uint64(dict, "snap-max-hard-limit", &value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Could not fetch snap_max_hard_limit for %s", volname); + ret = -1; + goto out; + } + cli_out("snap-max-hard-limit : %" PRIu64, value); + + ret = dict_get_uint64(dict, "snap-max-soft-limit", &soft_limit); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Could not fetch snap-max-soft-limit for %s", volname); + ret = -1; + goto out; + } + cli_out("snap-max-soft-limit : %" PRIu64 "%%", soft_limit); + + cli_out("auto-delete : %s", auto_delete); + + cli_out("activate-on-create : %s\n", snap_activate); + + cli_out("Snapshot Volume Configuration:"); + + ret = dict_get_uint64(dict, "voldisplaycount", &voldisplaycount); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not fetch voldisplaycount"); + ret = -1; + goto out; + } + + for (i = 0; i < voldisplaycount; i++) { + ret = snprintf(buf, sizeof(buf), "volume%" PRIu64 "-volname", + i); + ret = dict_get_strn(dict, buf, ret, &volname); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not fetch %s", buf); + ret = -1; + goto out; + } + cli_out("\nVolume : %s", volname); + + snprintf(buf, sizeof(buf), + "volume%" PRIu64 "-snap-max-hard-limit", i); + ret = dict_get_uint64(dict, buf, &value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not fetch %s", buf); + ret = -1; + goto out; + } + cli_out("snap-max-hard-limit : %" PRIu64, value); + + snprintf(buf, sizeof(buf), + "volume%" PRIu64 "-active-hard-limit", i); + ret = dict_get_uint64(dict, buf, &value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Could not fetch" + " effective snap_max_hard_limit for %s", + volname); + ret = -1; + goto out; + } + cli_out("Effective snap-max-hard-limit : %" PRIu64, value); + + snprintf(buf, sizeof(buf), + "volume%" PRIu64 "-snap-max-soft-limit", i); + ret = dict_get_uint64(dict, buf, &value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not fetch %s", buf); + ret = -1; + goto out; + } + cli_out("Effective snap-max-soft-limit : %" PRIu64 + " " + "(%" PRIu64 "%%)", + value, soft_limit); + } + break; + default: + break; + } + + ret = 0; +out: + return ret; +} + +/* This function is used to print the volume related information + * of a snap. + * + * arg - 0, dict : Response Dictionary. + * arg - 1, prefix str : snaplist.snap{0..}.vol{0..}.* + */ +static int +cli_get_each_volinfo_in_snap(dict_t *dict, char *keyprefix, + gf_boolean_t snap_driven) +{ + char key[PATH_MAX] = ""; + char *get_buffer = NULL; + int value = 0; + int ret = -1; + char indent[5] = "\t"; + char *volname = NULL; + + GF_ASSERT(dict); + GF_ASSERT(keyprefix); + + if (snap_driven) { + ret = snprintf(key, sizeof(key), "%s.volname", keyprefix); + if (ret < 0) { + goto out; + } + + ret = dict_get_str(dict, key, &get_buffer); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get %s", key); + goto out; + } + cli_out("%s" INDENT_MAIN_HEAD "%s", indent, "Snap Volume Name", ":", + get_buffer); + + ret = snprintf(key, sizeof(key), "%s.origin-volname", keyprefix); + if (ret < 0) { + goto out; + } + + ret = dict_get_str(dict, key, &volname); + if (ret) { + gf_log("cli", GF_LOG_WARNING, "Failed to get %s", key); + cli_out("%-12s", "Origin:"); + } + cli_out("%s" INDENT_MAIN_HEAD "%s", indent, "Origin Volume name", ":", + volname); + + ret = snprintf(key, sizeof(key), "%s.snapcount", keyprefix); + if (ret < 0) { + goto out; + } + + ret = dict_get_int32(dict, key, &value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get %s", key); + goto out; + } + cli_out("%s%s %s %s %d", indent, "Snaps taken for", volname, ":", + value); + + ret = snprintf(key, sizeof(key), "%s.snaps-available", keyprefix); + if (ret < 0) { + goto out; + } + + ret = dict_get_int32(dict, key, &value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get %s", key); + goto out; + } + cli_out("%s%s %s %s %d", indent, "Snaps available for", volname, ":", + value); + } + + ret = snprintf(key, sizeof(key), "%s.vol-status", keyprefix); + if (ret < 0) { + goto out; + } + + ret = dict_get_str(dict, key, &get_buffer); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get %s", key); + goto out; + } + cli_out("%s" INDENT_MAIN_HEAD "%s", indent, "Status", ":", get_buffer); +out: + return ret; +} + +/* This function is used to print snap related information + * arg - 0, dict : Response dictionary. + * arg - 1, prefix_str : snaplist.snap{0..}.* + */ +static int +cli_get_volinfo_in_snap(dict_t *dict, char *keyprefix) +{ + char key[PATH_MAX] = ""; + int i = 0; + int volcount = 0; + int ret = -1; + + GF_ASSERT(dict); + GF_ASSERT(keyprefix); + + ret = snprintf(key, sizeof(key), "%s.vol-count", keyprefix); + if (ret < 0) { + goto out; + } + + ret = dict_get_int32(dict, key, &volcount); + for (i = 1; i <= volcount; i++) { + ret = snprintf(key, sizeof(key), "%s.vol%d", keyprefix, i); + if (ret < 0) { + goto out; + } + ret = cli_get_each_volinfo_in_snap(dict, key, _gf_true); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Could not list details of volume in a snap"); + goto out; + } + cli_out(" "); + } + +out: + return ret; +} + +static int +cli_get_each_snap_info(dict_t *dict, char *prefix_str, gf_boolean_t snap_driven) +{ + char key_buffer[PATH_MAX] = ""; + char *get_buffer = NULL; + int ret = -1; + char indent[5] = ""; + + GF_ASSERT(dict); + GF_ASSERT(prefix_str); + + if (!snap_driven) + strcat(indent, "\t"); + + ret = snprintf(key_buffer, sizeof(key_buffer), "%s.snapname", prefix_str); + if (ret < 0) { + goto out; + } + + ret = dict_get_str(dict, key_buffer, &get_buffer); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to fetch snapname %s ", key_buffer); + goto out; + } + cli_out("%s" INDENT_MAIN_HEAD "%s", indent, "Snapshot", ":", get_buffer); + + ret = snprintf(key_buffer, sizeof(key_buffer), "%s.snap-id", prefix_str); + if (ret < 0) { + goto out; + } + + ret = dict_get_str(dict, key_buffer, &get_buffer); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to fetch snap-id %s ", key_buffer); + goto out; + } + cli_out("%s" INDENT_MAIN_HEAD "%s", indent, "Snap UUID", ":", get_buffer); + + ret = snprintf(key_buffer, sizeof(key_buffer), "%s.snap-desc", prefix_str); + if (ret < 0) { + goto out; + } + + ret = dict_get_str(dict, key_buffer, &get_buffer); + if (!ret) { + /* Ignore error for description */ + cli_out("%s" INDENT_MAIN_HEAD "%s", indent, "Description", ":", + get_buffer); + } + + ret = snprintf(key_buffer, sizeof(key_buffer), "%s.snap-time", prefix_str); + if (ret < 0) { + goto out; + } + + ret = dict_get_str(dict, key_buffer, &get_buffer); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to fetch snap-time %s ", + prefix_str); + goto out; + } + cli_out("%s" INDENT_MAIN_HEAD "%s", indent, "Created", ":", get_buffer); + + if (snap_driven) { + cli_out("%-12s", "Snap Volumes:\n"); + ret = cli_get_volinfo_in_snap(dict, prefix_str); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to list details of the snaps"); + goto out; + } + } +out: + return ret; +} + +/* This is a generic function to print snap related information. + * arg - 0, dict : Response Dictionary + */ +static int +cli_call_snapshot_info(dict_t *dict, gf_boolean_t bool_snap_driven) +{ + int snap_count = 0; + char key[32] = ""; + int ret = -1; + int i = 0; + + GF_ASSERT(dict); + + ret = dict_get_int32_sizen(dict, "snapcount", &snap_count); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to get snapcount"); + goto out; + } + + if (snap_count == 0) { + cli_out("No snapshots present"); + } + + for (i = 1; i <= snap_count; i++) { + ret = snprintf(key, sizeof(key), "snap%d", i); + if (ret < 0) { + goto out; + } + ret = cli_get_each_snap_info(dict, key, bool_snap_driven); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to print snap details"); + goto out; + } + } +out: + return ret; +} + +static int +cli_get_snaps_in_volume(dict_t *dict) +{ + int ret = -1; + int i = 0; + int count = 0; + int avail = 0; + char key[32] = ""; + char *get_buffer = NULL; + + GF_ASSERT(dict); + + ret = dict_get_str_sizen(dict, "origin-volname", &get_buffer); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not fetch origin-volname"); + goto out; + } + cli_out(INDENT_MAIN_HEAD "%s", "Volume Name", ":", get_buffer); + + ret = dict_get_int32_sizen(dict, "snapcount", &avail); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not fetch snapcount"); + goto out; + } + cli_out(INDENT_MAIN_HEAD "%d", "Snaps Taken", ":", avail); + + ret = dict_get_int32_sizen(dict, "snaps-available", &count); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not fetch snaps-available"); + goto out; + } + cli_out(INDENT_MAIN_HEAD "%d", "Snaps Available", ":", count); + + for (i = 1; i <= avail; i++) { + snprintf(key, sizeof(key), "snap%d", i); + ret = cli_get_each_snap_info(dict, key, _gf_false); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to print snap details"); + goto out; + } + + ret = snprintf(key, sizeof(key), "snap%d.vol1", i); + if (ret < 0) { + goto out; + } + ret = cli_get_each_volinfo_in_snap(dict, key, _gf_false); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Could not get volume related information"); + goto out; + } + + cli_out(" "); + } +out: + return ret; +} + +static int +cli_snapshot_list(dict_t *dict) +{ + int snapcount = 0; + char key[32] = ""; + int ret = -1; + int i = 0; + char *get_buffer = NULL; + + GF_ASSERT(dict); + + ret = dict_get_int32_sizen(dict, "snapcount", &snapcount); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not fetch snap count"); + goto out; + } + + if (snapcount == 0) { + cli_out("No snapshots present"); + } + + for (i = 1; i <= snapcount; i++) { + ret = snprintf(key, sizeof(key), "snapname%d", i); + if (ret < 0) { + goto out; + } + + ret = dict_get_strn(dict, key, ret, &get_buffer); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not get %s ", key); + goto out; + } else { + cli_out("%s", get_buffer); + } + } +out: + return ret; +} + +static int +cli_get_snap_volume_status(dict_t *dict, char *key_prefix) +{ + int ret = -1; + char key[PATH_MAX] = ""; + char *buffer = NULL; + int brickcount = 0; + int i = 0; + int pid = 0; + + GF_ASSERT(dict); + GF_ASSERT(key_prefix); + + ret = snprintf(key, sizeof(key), "%s.brickcount", key_prefix); + if (ret < 0) { + goto out; + } + ret = dict_get_int32(dict, key, &brickcount); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to fetch brickcount"); + goto out; + } + + for (i = 0; i < brickcount; i++) { + ret = snprintf(key, sizeof(key), "%s.brick%d.path", key_prefix, i); + if (ret < 0) { + goto out; + } + + ret = dict_get_str(dict, key, &buffer); + if (ret) { + gf_log("cli", GF_LOG_INFO, "Unable to get Brick Path"); + continue; + } + cli_out("\n\t%-17s %s %s", "Brick Path", ":", buffer); + + ret = snprintf(key, sizeof(key), "%s.brick%d.vgname", key_prefix, i); + if (ret < 0) { + goto out; + } + + ret = dict_get_str(dict, key, &buffer); + if (ret) { + gf_log("cli", GF_LOG_INFO, "Unable to get Volume Group"); + cli_out("\t%-17s %s %s", "Volume Group", ":", "N/A"); + } else + cli_out("\t%-17s %s %s", "Volume Group", ":", buffer); + + ret = snprintf(key, sizeof(key), "%s.brick%d.status", key_prefix, i); + if (ret < 0) { + goto out; + } + + ret = dict_get_str(dict, key, &buffer); + if (ret) { + gf_log("cli", GF_LOG_INFO, "Unable to get Brick Running"); + cli_out("\t%-17s %s %s", "Brick Running", ":", "N/A"); + } else + cli_out("\t%-17s %s %s", "Brick Running", ":", buffer); + + ret = snprintf(key, sizeof(key), "%s.brick%d.pid", key_prefix, i); + if (ret < 0) { + goto out; + } + + ret = dict_get_int32(dict, key, &pid); + if (ret) { + gf_log("cli", GF_LOG_INFO, "Unable to get pid"); + cli_out("\t%-17s %s %s", "Brick PID", ":", "N/A"); + } else + cli_out("\t%-17s %s %d", "Brick PID", ":", pid); + + ret = snprintf(key, sizeof(key), "%s.brick%d.data", key_prefix, i); + if (ret < 0) { + goto out; + } + + ret = dict_get_str(dict, key, &buffer); + if (ret) { + gf_log("cli", GF_LOG_INFO, "Unable to get Data Percent"); + cli_out("\t%-17s %s %s", "Data Percentage", ":", "N/A"); + } else + cli_out("\t%-17s %s %s", "Data Percentage", ":", buffer); + + ret = snprintf(key, sizeof(key), "%s.brick%d.lvsize", key_prefix, i); + if (ret < 0) { + goto out; + } + ret = dict_get_str(dict, key, &buffer); + if (ret) { + gf_log("cli", GF_LOG_INFO, "Unable to get LV Size"); + cli_out("\t%-17s %s %s", "LV Size", ":", "N/A"); + } else + cli_out("\t%-17s %s %s", "LV Size", ":", buffer); + } + + ret = 0; +out: + return ret; +} + +static int +cli_get_single_snap_status(dict_t *dict, char *keyprefix) +{ + int ret = -1; + char key[64] = ""; /* keyprefix is ""status.snap0" */ + int i = 0; + int volcount = 0; + char *get_buffer = NULL; + + GF_ASSERT(dict); + GF_ASSERT(keyprefix); + + ret = snprintf(key, sizeof(key), "%s.snapname", keyprefix); + if (ret < 0) { + goto out; + } + + ret = dict_get_str(dict, key, &get_buffer); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to get snapname"); + goto out; + } + cli_out("\nSnap Name : %s", get_buffer); + + ret = snprintf(key, sizeof(key), "%s.uuid", keyprefix); + if (ret < 0) { + goto out; + } + + ret = dict_get_str(dict, key, &get_buffer); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to get snap UUID"); + goto out; + } + cli_out("Snap UUID : %s", get_buffer); + + ret = snprintf(key, sizeof(key), "%s.volcount", keyprefix); + if (ret < 0) { + goto out; + } + + ret = dict_get_int32(dict, key, &volcount); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to get volume count"); + goto out; + } + + for (i = 0; i < volcount; i++) { + ret = snprintf(key, sizeof(key), "%s.vol%d", keyprefix, i); + if (ret < 0) { + goto out; + } + + ret = cli_get_snap_volume_status(dict, key); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not get snap volume status"); + goto out; + } + } +out: + return ret; +} + +static int32_t +cli_populate_req_dict_for_delete(dict_t *snap_dict, dict_t *dict, size_t index) +{ + int32_t ret = -1; + char key[PATH_MAX] = ""; + char *buffer = NULL; + + GF_ASSERT(snap_dict); + GF_ASSERT(dict); + + ret = dict_set_int32_sizen(snap_dict, "sub-cmd", GF_SNAP_DELETE_TYPE_ITER); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Could not save command type in snap dictionary"); + goto out; + } + + ret = snprintf(key, sizeof(key), "snapname%zu", index); + if (ret < 0) { + goto out; + } + + ret = dict_get_str(dict, key, &buffer); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get snapname"); + goto out; + } + + ret = dict_set_dynstr_with_alloc(snap_dict, "snapname", buffer); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to save snapname"); + goto out; + } + + ret = dict_set_int32_sizen(snap_dict, "type", GF_SNAP_OPTION_TYPE_DELETE); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to save command type"); + goto out; + } + + ret = dict_set_dynstr_with_alloc(snap_dict, "cmd-str", "snapshot delete"); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not save command string as delete"); + goto out; + } +out: + return ret; +} + +static int +cli_populate_req_dict_for_status(dict_t *snap_dict, dict_t *dict, int index) +{ + int ret = -1; + char key[PATH_MAX] = ""; + char *buffer = NULL; + + GF_ASSERT(snap_dict); + GF_ASSERT(dict); + + ret = dict_set_uint32(snap_dict, "sub-cmd", GF_SNAP_STATUS_TYPE_ITER); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not save command type in snap dict"); + goto out; + } + + ret = snprintf(key, sizeof(key), "status.snap%d.snapname", index); + if (ret < 0) { + goto out; + } + + ret = dict_get_strn(dict, key, ret, &buffer); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not get snapname"); + goto out; + } + + ret = dict_set_str_sizen(snap_dict, "snapname", buffer); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not save snapname in snap dict"); + goto out; + } + + ret = dict_set_int32_sizen(snap_dict, "type", GF_SNAP_OPTION_TYPE_STATUS); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not save command type"); + goto out; + } + + ret = dict_set_dynstr_with_alloc(snap_dict, "cmd-str", "snapshot status"); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not save command string as status"); + goto out; + } + + ret = dict_set_int32_sizen(snap_dict, "hold_vol_locks", _gf_false); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Setting volume lock flag failed"); + goto out; + } + +out: + return ret; +} + +static int +cli_snapshot_status(dict_t *dict, gf_cli_rsp *rsp, call_frame_t *frame) +{ + int ret = -1; + int status_cmd = -1; + cli_local_t *local = NULL; + + GF_ASSERT(dict); + GF_ASSERT(rsp); + GF_ASSERT(frame); + + local = ((call_frame_t *)frame)->local; + if (!local) { + gf_log("cli", GF_LOG_ERROR, "frame->local is NULL"); + goto out; + } + + if (rsp->op_ret) { + if (rsp->op_errstr) { + ret = dict_set_dynstr_with_alloc(local->dict, "op_err_str", + rsp->op_errstr); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to set op_errstr in local dictionary"); + goto out; + } + } + ret = rsp->op_ret; + goto out; + } + + ret = dict_get_int32_sizen(dict, "sub-cmd", &status_cmd); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not fetch status type"); + goto out; + } + + if ((status_cmd != GF_SNAP_STATUS_TYPE_SNAP) && + (status_cmd != GF_SNAP_STATUS_TYPE_ITER)) { + dict_copy(dict, local->dict); + goto out; + } + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_snapshot_status_single_snap(local, dict, "status.snap0"); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to create xml output for snapshot status"); + } + } else { + ret = cli_get_single_snap_status(dict, "status.snap0"); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not fetch status of snap"); + } + } + +out: + return ret; +} + +static int +gf_cli_generate_snapshot_event(gf_cli_rsp *rsp, dict_t *dict, int32_t type, + char *snap_name, char *volname, char *snap_uuid, + char *clone_name) +{ + int ret = -1; + int config_command = 0; + int32_t delete_cmd = -1; + uint64_t hard_limit = 0; + uint64_t soft_limit = 0; + char *auto_delete = NULL; + char *snap_activate = NULL; + char msg[PATH_MAX] = { + 0, + }; + char option[512] = { + 0, + }; + + GF_VALIDATE_OR_GOTO("cli", dict, out); + GF_VALIDATE_OR_GOTO("cli", rsp, out); + + switch (type) { + case GF_SNAP_OPTION_TYPE_CREATE: + if (!snap_name) { + gf_log("cli", GF_LOG_ERROR, "Failed to get snap name"); + goto out; + } + + if (!volname) { + gf_log("cli", GF_LOG_ERROR, "Failed to get volume name"); + goto out; + } + + if (rsp->op_ret != 0) { + gf_event(EVENT_SNAPSHOT_CREATE_FAILED, + "snapshot_name=%s;volume_name=%s;error=%s", snap_name, + volname, + rsp->op_errstr ? rsp->op_errstr + : "Please check log file for details"); + ret = 0; + break; + } + + if (!snap_uuid) { + gf_log("cli", GF_LOG_ERROR, "Failed to get snap uuid"); + goto out; + } + + gf_event(EVENT_SNAPSHOT_CREATED, + "snapshot_name=%s;volume_name=%s;snapshot_uuid=%s", + snap_name, volname, snap_uuid); + + ret = 0; + break; + + case GF_SNAP_OPTION_TYPE_ACTIVATE: + if (!snap_name) { + gf_log("cli", GF_LOG_ERROR, "Failed to get snap name"); + goto out; + } + + if (rsp->op_ret != 0) { + gf_event(EVENT_SNAPSHOT_ACTIVATE_FAILED, + "snapshot_name=%s;error=%s", snap_name, + rsp->op_errstr ? rsp->op_errstr + : "Please check log file for details"); + ret = 0; + break; + } + + if (!snap_uuid) { + gf_log("cli", GF_LOG_ERROR, "Failed to get snap uuid"); + goto out; + } + + gf_event(EVENT_SNAPSHOT_ACTIVATED, + "snapshot_name=%s;snapshot_uuid=%s", snap_name, snap_uuid); + + ret = 0; + break; + + case GF_SNAP_OPTION_TYPE_DEACTIVATE: + if (!snap_name) { + gf_log("cli", GF_LOG_ERROR, "Failed to get snap name"); + goto out; + } + + if (rsp->op_ret != 0) { + gf_event(EVENT_SNAPSHOT_DEACTIVATE_FAILED, + "snapshot_name=%s;error=%s", snap_name, + rsp->op_errstr ? rsp->op_errstr + : "Please check log file for details"); + ret = 0; + break; + } + + if (!snap_uuid) { + gf_log("cli", GF_LOG_ERROR, "Failed to get snap uuid"); + goto out; + } + + gf_event(EVENT_SNAPSHOT_DEACTIVATED, + "snapshot_name=%s;snapshot_uuid=%s", snap_name, snap_uuid); + + ret = 0; + break; + + case GF_SNAP_OPTION_TYPE_RESTORE: + if (!snap_name) { + gf_log("cli", GF_LOG_ERROR, "Failed to get snap name"); + goto out; + } + + if (rsp->op_ret != 0) { + gf_event(EVENT_SNAPSHOT_RESTORE_FAILED, + "snapshot_name=%s;error=%s", snap_name, + rsp->op_errstr ? rsp->op_errstr + : "Please check log file for details"); + ret = 0; + break; + } + + if (!snap_uuid) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Failed to get snap uuid"); + goto out; + } + + if (!volname) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Failed to get volname"); + goto out; + } + + gf_event(EVENT_SNAPSHOT_RESTORED, + "snapshot_name=%s;snapshot_uuid=%s;volume_name=%s", + snap_name, snap_uuid, volname); + + ret = 0; + break; + + case GF_SNAP_OPTION_TYPE_DELETE: + ret = dict_get_int32_sizen(dict, "sub-cmd", &delete_cmd); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not get sub-cmd"); + goto out; + } + + /* + * Need not generate any event (success or failure) for delete * + * all, as it will trigger individual delete for all snapshots * + */ + if (delete_cmd == GF_SNAP_DELETE_TYPE_ALL) { + ret = 0; + break; + } + + if (!snap_name) { + gf_log("cli", GF_LOG_ERROR, "Failed to get snap name"); + goto out; + } + + if (rsp->op_ret != 0) { + gf_event(EVENT_SNAPSHOT_DELETE_FAILED, + "snapshot_name=%s;error=%s", snap_name, + rsp->op_errstr ? rsp->op_errstr + : "Please check log file for details"); + ret = 0; + break; + } + + if (!snap_uuid) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Failed to get snap uuid"); + goto out; + } + + gf_event(EVENT_SNAPSHOT_DELETED, + "snapshot_name=%s;snapshot_uuid=%s", snap_name, snap_uuid); + + ret = 0; + break; + + case GF_SNAP_OPTION_TYPE_CLONE: + if (!clone_name) { + gf_log("cli", GF_LOG_ERROR, "Failed to get clone name"); + goto out; + } + + if (!snap_name) { + gf_log("cli", GF_LOG_ERROR, "Failed to get snapname name"); + goto out; + } + + if (rsp->op_ret != 0) { + gf_event(EVENT_SNAPSHOT_CLONE_FAILED, + "snapshot_name=%s;clone_name=%s;error=%s", snap_name, + clone_name, + rsp->op_errstr ? rsp->op_errstr + : "Please check log file for details"); + ret = 0; + break; + } + + if (!snap_uuid) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Failed to get snap uuid"); + goto out; + } + + gf_event(EVENT_SNAPSHOT_CLONED, + "snapshot_name=%s;clone_name=%s;clone_uuid=%s", snap_name, + clone_name, snap_uuid); + + ret = 0; + break; + + case GF_SNAP_OPTION_TYPE_CONFIG: + if (rsp->op_ret != 0) { + gf_event(EVENT_SNAPSHOT_CONFIG_UPDATE_FAILED, "error=%s", + rsp->op_errstr ? rsp->op_errstr + : "Please check log file for details"); + ret = 0; + break; + } + + ret = dict_get_int32_sizen(dict, "config-command", &config_command); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not fetch config type"); + goto out; + } + + if (config_command == GF_SNAP_CONFIG_DISPLAY) { + ret = 0; + break; + } + + /* These are optional parameters therefore ignore the error */ + ret = dict_get_uint64(dict, "snap-max-hard-limit", &hard_limit); + ret = dict_get_uint64(dict, "snap-max-soft-limit", &soft_limit); + ret = dict_get_str_sizen(dict, "auto-delete", &auto_delete); + ret = dict_get_str_sizen(dict, "snap-activate-on-create", + &snap_activate); + + if (!hard_limit && !soft_limit && !auto_delete && !snap_activate) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, + "At least one option from " + "snap-max-hard-limit, snap-max-soft-limit, " + "auto-delete and snap-activate-on-create " + "should be set"); + goto out; + } + + if (hard_limit || soft_limit) { + snprintf(option, sizeof(option), "%s=%" PRIu64, + hard_limit ? "hard_limit" : "soft_limit", + hard_limit ? hard_limit : soft_limit); + } else if (auto_delete || snap_activate) { + snprintf(option, sizeof(option), "%s=%s", + auto_delete ? "auto-delete" : "snap-activate", + auto_delete ? auto_delete : snap_activate); + } + + volname = NULL; + ret = dict_get_str_sizen(dict, "volname", &volname); + + snprintf(msg, sizeof(msg), "config_type=%s;%s", + volname ? "volume_config" : "system_config", option); + + gf_event(EVENT_SNAPSHOT_CONFIG_UPDATED, "%s", msg); + + ret = 0; + break; + + default: + gf_log("cli", GF_LOG_WARNING, + "Cannot generate event for unknown type."); + ret = 0; + goto out; + } + +out: + return ret; +} + +/* + * Fetch necessary data from dict at one place instead of * + * repeating the same code again and again. * + */ +static int +gf_cli_snapshot_get_data_from_dict(dict_t *dict, char **snap_name, + char **volname, char **snap_uuid, + int8_t *soft_limit_flag, char **clone_name) +{ + int ret = -1; + + GF_VALIDATE_OR_GOTO("cli", dict, out); + + if (snap_name) { + ret = dict_get_str_sizen(dict, "snapname", snap_name); + if (ret) { + gf_log("cli", GF_LOG_DEBUG, "failed to get snapname from dict"); + } + } + + if (volname) { + ret = dict_get_str_sizen(dict, "volname1", volname); + if (ret) { + gf_log("cli", GF_LOG_DEBUG, "failed to get volname1 from dict"); + } + } + + if (snap_uuid) { + ret = dict_get_str_sizen(dict, "snapuuid", snap_uuid); + if (ret) { + gf_log("cli", GF_LOG_DEBUG, "failed to get snapuuid from dict"); + } + } + + if (soft_limit_flag) { + ret = dict_get_int8(dict, "soft-limit-reach", soft_limit_flag); + if (ret) { + gf_log("cli", GF_LOG_DEBUG, + "failed to get soft-limit-reach from dict"); + } + } + + if (clone_name) { + ret = dict_get_str_sizen(dict, "clonename", clone_name); + if (ret) { + gf_log("cli", GF_LOG_DEBUG, "failed to get clonename from dict"); + } + } + + ret = 0; +out: + return ret; +} + +static int +gf_cli_snapshot_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + int ret = -1; + gf_cli_rsp rsp = { + 0, + }; + dict_t *dict = NULL; + char *snap_name = NULL; + char *clone_name = NULL; + int32_t type = 0; + call_frame_t *frame = NULL; + gf_boolean_t snap_driven = _gf_false; + int8_t soft_limit_flag = -1; + char *volname = NULL; + char *snap_uuid = NULL; + + GF_ASSERT(myframe); + + if (req->rpc_status == -1) { + goto out; + } + + frame = myframe; + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(frame->this->name, GF_LOG_ERROR, XDR_DECODE_FAIL); + goto out; + } + + dict = dict_new(); + + if (!dict) { + ret = -1; + goto out; + } + + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, &dict); + + if (ret) + goto out; + + ret = dict_get_int32_sizen(dict, "type", &type); + if (ret) { + gf_log(frame->this->name, GF_LOG_ERROR, "failed to get type"); + goto out; + } + + ret = gf_cli_snapshot_get_data_from_dict( + dict, &snap_name, &volname, &snap_uuid, &soft_limit_flag, &clone_name); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to fetch data from dict."); + goto out; + } + +#if (USE_EVENTS) + ret = gf_cli_generate_snapshot_event(&rsp, dict, type, snap_name, volname, + snap_uuid, clone_name); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to generate snapshot event"); + goto out; + } +#endif + + /* Snapshot status and delete command is handled separately */ + if (global_state->mode & GLUSTER_MODE_XML && + GF_SNAP_OPTION_TYPE_STATUS != type && + GF_SNAP_OPTION_TYPE_DELETE != type) { + ret = cli_xml_output_snapshot(type, dict, rsp.op_ret, rsp.op_errno, + rsp.op_errstr); + if (ret) { + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + } + + goto out; + } + + switch (type) { + case GF_SNAP_OPTION_TYPE_CREATE: + if (rsp.op_ret) { + cli_err("snapshot create: failed: %s", + rsp.op_errstr ? rsp.op_errstr + : "Please check log file for details"); + ret = rsp.op_ret; + goto out; + } + + if (!snap_name) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Failed to get snap name"); + goto out; + } + + if (!volname) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, "Failed to get volume name"); + goto out; + } + + cli_out("snapshot create: success: Snap %s created successfully", + snap_name); + + if (soft_limit_flag == 1) { + cli_out( + "Warning: Soft-limit of volume (%s) is " + "reached. Snapshot creation is not possible " + "once hard-limit is reached.", + volname); + } + ret = 0; + break; + + case GF_SNAP_OPTION_TYPE_CLONE: + if (rsp.op_ret) { + cli_err("snapshot clone: failed: %s", + rsp.op_errstr ? rsp.op_errstr + : "Please check log file for details"); + ret = rsp.op_ret; + goto out; + } + + if (!clone_name) { + gf_log("cli", GF_LOG_ERROR, "Failed to get clone name"); + goto out; + } + + if (!snap_name) { + gf_log("cli", GF_LOG_ERROR, "Failed to get snapname name"); + goto out; + } + + cli_out("snapshot clone: success: Clone %s created successfully", + clone_name); + + ret = 0; + break; + + case GF_SNAP_OPTION_TYPE_RESTORE: + if (rsp.op_ret) { + cli_err("snapshot restore: failed: %s", + rsp.op_errstr ? rsp.op_errstr + : "Please check log file for details"); + ret = rsp.op_ret; + goto out; + } + + if (!snap_name) { + gf_log("cli", GF_LOG_ERROR, "Failed to get snap name"); + goto out; + } + + cli_out("Snapshot restore: %s: Snap restored successfully", + snap_name); + + ret = 0; + break; + case GF_SNAP_OPTION_TYPE_ACTIVATE: + if (rsp.op_ret) { + cli_err("snapshot activate: failed: %s", + rsp.op_errstr ? rsp.op_errstr + : "Please check log file for details"); + ret = rsp.op_ret; + goto out; + } + + if (!snap_name) { + gf_log("cli", GF_LOG_ERROR, "Failed to get snap name"); + goto out; + } + + cli_out("Snapshot activate: %s: Snap activated successfully", + snap_name); + + ret = 0; + break; + + case GF_SNAP_OPTION_TYPE_DEACTIVATE: + if (rsp.op_ret) { + cli_err("snapshot deactivate: failed: %s", + rsp.op_errstr ? rsp.op_errstr + : "Please check log file for details"); + ret = rsp.op_ret; + goto out; + } + + if (!snap_name) { + gf_log("cli", GF_LOG_ERROR, "Failed to get snap name"); + goto out; + } + + cli_out("Snapshot deactivate: %s: Snap deactivated successfully", + snap_name); + + ret = 0; + break; + case GF_SNAP_OPTION_TYPE_INFO: + if (rsp.op_ret) { + cli_err("Snapshot info : failed: %s", + rsp.op_errstr ? rsp.op_errstr + : "Please check log file for details"); + ret = rsp.op_ret; + goto out; + } + + snap_driven = dict_get_str_boolean(dict, "snap-driven", _gf_false); + if (snap_driven == _gf_true) { + ret = cli_call_snapshot_info(dict, snap_driven); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Snapshot info failed"); + goto out; + } + } else if (snap_driven == _gf_false) { + ret = cli_get_snaps_in_volume(dict); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Snapshot info failed"); + goto out; + } + } + break; + + case GF_SNAP_OPTION_TYPE_CONFIG: + ret = cli_snapshot_config_display(dict, &rsp); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to display snapshot config output."); + goto out; + } + break; + + case GF_SNAP_OPTION_TYPE_LIST: + if (rsp.op_ret) { + cli_err("Snapshot list : failed: %s", + rsp.op_errstr ? rsp.op_errstr + : "Please check log file for details"); + ret = rsp.op_ret; + goto out; + } + + ret = cli_snapshot_list(dict); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to display snapshot list"); + goto out; + } + break; + + case GF_SNAP_OPTION_TYPE_DELETE: + ret = cli_snapshot_remove_reply(&rsp, dict, frame); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to delete snap"); + goto out; + } + break; + + case GF_SNAP_OPTION_TYPE_STATUS: + ret = cli_snapshot_status(dict, &rsp, frame); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to display snapshot status output."); + goto out; + } + break; + + default: + cli_err("Unknown command executed"); + ret = -1; + goto out; + } +out: + if (dict) + dict_unref(dict); + cli_cmd_broadcast_response(ret); + + free(rsp.dict.dict_val); + free(rsp.op_errstr); + + return ret; +} + +int32_t +gf_cli_snapshot_for_delete(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + int32_t ret = -1; + int32_t cmd = -1; + cli_local_t *local = NULL; + dict_t *snap_dict = NULL; + int32_t snapcount = 0; + int i = 0; + char question[PATH_MAX] = ""; + char *volname = NULL; + gf_answer_t answer = GF_ANSWER_NO; + + GF_VALIDATE_OR_GOTO("cli", frame, out); + GF_VALIDATE_OR_GOTO("cli", frame->local, out); + GF_VALIDATE_OR_GOTO("cli", this, out); + GF_VALIDATE_OR_GOTO("cli", data, out); + + local = frame->local; + + ret = dict_get_int32_sizen(local->dict, "sub-cmd", &cmd); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get sub-cmd"); + goto out; + } + + /* No need multiple RPCs for individual snapshot delete*/ + if (cmd == GF_SNAP_DELETE_TYPE_SNAP) { + ret = 0; + goto out; + } + + ret = dict_get_int32_sizen(local->dict, "snapcount", &snapcount); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not get snapcount"); + goto out; + } + + if (global_state->mode & GLUSTER_MODE_XML) { +#ifdef HAVE_LIB_XML + ret = xmlTextWriterWriteFormatElement( + local->writer, (xmlChar *)"snapCount", "%d", snapcount); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to write xml element \"snapCount\""); + goto out; + } +#endif /* HAVE_LIB_XML */ + } else if (snapcount == 0) { + cli_out("No snapshots present"); + goto out; + } + + if (cmd == GF_SNAP_DELETE_TYPE_ALL) { + snprintf(question, sizeof(question), + "System contains %d snapshot(s).\nDo you still " + "want to continue and delete them? ", + snapcount); + } else { + ret = dict_get_str_sizen(local->dict, "volname", &volname); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to fetch volname from local dictionary"); + goto out; + } + + snprintf(question, sizeof(question), + "Volume (%s) contains %d snapshot(s).\nDo you still want to " + "continue and delete them? ", + volname, snapcount); + } + + answer = cli_cmd_get_confirmation(global_state, question); + if (GF_ANSWER_NO == answer) { + ret = 0; + gf_log("cli", GF_LOG_DEBUG, + "User cancelled snapshot delete operation for snap delete"); + goto out; + } + + for (i = 1; i <= snapcount; i++) { + ret = -1; + + snap_dict = dict_new(); + if (!snap_dict) + goto out; + + ret = cli_populate_req_dict_for_delete(snap_dict, local->dict, i); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Could not populate snap request dictionary"); + goto out; + } + + ret = cli_to_glusterd(&req, frame, gf_cli_snapshot_cbk, + (xdrproc_t)xdr_gf_cli_req, snap_dict, + GLUSTER_CLI_SNAP, this, cli_rpc_prog, NULL); + if (ret) { + /* Fail the operation if deleting one of the + * snapshots is failed + */ + gf_log("cli", GF_LOG_ERROR, + "cli_to_glusterd for snapshot delete failed"); + goto out; + } + dict_unref(snap_dict); + snap_dict = NULL; + } + +out: + if (snap_dict) + dict_unref(snap_dict); + GF_FREE(req.dict.dict_val); + + return ret; +} + +static int32_t +gf_cli_snapshot_for_status(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + int ret = -1; + int32_t cmd = -1; + cli_local_t *local = NULL; + dict_t *snap_dict = NULL; + int snapcount = 0; + int i = 0; + + GF_VALIDATE_OR_GOTO("cli", frame, out); + GF_VALIDATE_OR_GOTO("cli", frame->local, out); + GF_VALIDATE_OR_GOTO("cli", this, out); + GF_VALIDATE_OR_GOTO("cli", data, out); + + local = frame->local; + + ret = dict_get_int32_sizen(local->dict, "sub-cmd", &cmd); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get sub-cmd"); + goto out; + } + + /* Snapshot status of single snap (i.e. GF_SNAP_STATUS_TYPE_SNAP) + * is already handled. Therefore we can return from here. + * If want to get status of all snaps in the system or volume then + * we should get them one by one.*/ + if ((cmd == GF_SNAP_STATUS_TYPE_SNAP) || + (cmd == GF_SNAP_STATUS_TYPE_ITER)) { + ret = 0; + goto out; + } + + ret = dict_get_int32_sizen(local->dict, "status.snapcount", &snapcount); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not get snapcount"); + goto out; + } + + if (snapcount == 0 && !(global_state->mode & GLUSTER_MODE_XML)) { + cli_out("No snapshots present"); + } + + for (i = 0; i < snapcount; i++) { + ret = -1; + + snap_dict = dict_new(); + if (!snap_dict) + goto out; + + ret = cli_populate_req_dict_for_status(snap_dict, local->dict, i); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Could not populate snap request dictionary"); + goto out; + } + + ret = cli_to_glusterd(&req, frame, gf_cli_snapshot_cbk, + (xdrproc_t)xdr_gf_cli_req, snap_dict, + GLUSTER_CLI_SNAP, this, cli_rpc_prog, NULL); + + /* Ignore the return value and error for snapshot + * status of type "ALL" or "VOL" + * + * Scenario : There might be case where status command + * and delete command might be issued at the same time. + * In that case when status tried to fetch detail of + * snap which has been deleted by concurrent command, + * then it will show snapshot not present. Which will + * not be appropriate. + */ + if (ret && (cmd != GF_SNAP_STATUS_TYPE_ALL && + cmd != GF_SNAP_STATUS_TYPE_VOL)) { + gf_log("cli", GF_LOG_ERROR, + "cli_to_glusterd for snapshot status failed"); + goto out; + } + dict_unref(snap_dict); + snap_dict = NULL; + } + + ret = 0; +out: + if (snap_dict) + dict_unref(snap_dict); + GF_FREE(req.dict.dict_val); + + return ret; +} + +static int32_t +gf_cli_snapshot(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + dict_t *options = NULL; + int ret = -1; + int tmp_ret = -1; + cli_local_t *local = NULL; + char *err_str = NULL; + int type = -1; + + if (!frame || !frame->local || !this || !data) + goto out; + + local = frame->local; + + options = data; + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_snapshot_begin_composite_op(local); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to begin snapshot xml composite op"); + goto out; + } + } + + ret = cli_to_glusterd(&req, frame, gf_cli_snapshot_cbk, + (xdrproc_t)xdr_gf_cli_req, options, GLUSTER_CLI_SNAP, + this, cli_rpc_prog, NULL); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "cli_to_glusterd for snapshot failed"); + goto xmlend; + } + + ret = dict_get_int32_sizen(local->dict, "type", &type); + + if (GF_SNAP_OPTION_TYPE_STATUS == type) { + ret = gf_cli_snapshot_for_status(frame, this, data); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "cli to glusterd for snapshot status command failed"); + } + + goto xmlend; + } + + if (GF_SNAP_OPTION_TYPE_DELETE == type) { + ret = gf_cli_snapshot_for_delete(frame, this, data); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "cli to glusterd for snapshot delete command failed"); + } + + goto xmlend; + } + + ret = 0; + +xmlend: + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_snapshot_end_composite_op(local); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to end snapshot xml composite op"); + goto out; + } + } +out: + if (ret && local && GF_SNAP_OPTION_TYPE_STATUS == type) { + tmp_ret = dict_get_str_sizen(local->dict, "op_err_str", &err_str); + if (tmp_ret || !err_str) { + cli_err("Snapshot Status : failed: %s", + "Please check log file for details"); + } else { + cli_err("Snapshot Status : failed: %s", err_str); + dict_del_sizen(local->dict, "op_err_str"); + } + } + + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + + GF_FREE(req.dict.dict_val); + + if (global_state->mode & GLUSTER_MODE_XML) { + /* XML mode handles its own error */ + ret = 0; + } + return ret; +} + +static int32_t +gf_cli_barrier_volume_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) + goto out; + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + goto out; + } + gf_log("cli", GF_LOG_DEBUG, "Received response to barrier"); + + if (rsp.op_ret) { + if (rsp.op_errstr && (strlen(rsp.op_errstr) > 1)) { + cli_err("volume barrier: command unsuccessful : %s", rsp.op_errstr); + } else { + cli_err("volume barrier: command unsuccessful"); + } + } else { + cli_out("volume barrier: command successful"); + } + ret = rsp.op_ret; + +out: + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + return ret; +} + +static int +gf_cli_barrier_volume(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + dict_t *options = NULL; + int ret = -1; + + options = data; + + ret = cli_to_glusterd(&req, frame, gf_cli_barrier_volume_cbk, + (xdrproc_t)xdr_gf_cli_req, options, + GLUSTER_CLI_BARRIER_VOLUME, this, cli_rpc_prog, NULL); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + + GF_FREE(req.dict.dict_val); + return ret; +} + +static int32_t +gf_cli_get_vol_opt_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + gf_cli_rsp rsp = { + 0, + }; + int ret = -1; + dict_t *dict = NULL; + char *key = NULL; + char *value = NULL; + char msg[1024] = { + 0, + }; + int i = 0; + char dict_key[50] = { + 0, + }; + + GF_ASSERT(myframe); + + if (-1 == req->rpc_status) + goto out; + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + goto out; + } + gf_log("cli", GF_LOG_DEBUG, "Received response to get volume option"); + + if (rsp.op_ret) { + if (strcmp(rsp.op_errstr, "")) + snprintf(msg, sizeof(msg), "volume get option: failed: %s", + rsp.op_errstr); + else + snprintf(msg, sizeof(msg), "volume get option: failed"); + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_str("volGetopts", msg, rsp.op_ret, + rsp.op_errno, rsp.op_errstr); + if (ret) { + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + } + } else { + cli_err("%s", msg); + } + ret = rsp.op_ret; + goto out_nolog; + } + dict = dict_new(); + + if (!dict) { + ret = -1; + goto out; + } + + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, &dict); + if (ret) { + gf_log("cli", GF_LOG_ERROR, DICT_UNSERIALIZE_FAIL); + goto out; + } + + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_vol_getopts(dict, rsp.op_ret, rsp.op_errno, + rsp.op_errstr); + if (ret) { + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + ret = 0; + } + goto out; + } + + ret = dict_get_str_sizen(dict, "warning", &value); + if (!ret) { + cli_out("%s", value); + } + + ret = dict_get_int32_sizen(dict, "count", &count); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to retrieve count from the dictionary"); + goto out; + } + + if (count <= 0) { + gf_log("cli", GF_LOG_ERROR, "Value of count :%d is invalid", count); + ret = -1; + goto out; + } + + cli_out("%-40s%-40s", "Option", "Value"); + cli_out("%-40s%-40s", "------", "-----"); + for (i = 1; i <= count; i++) { + ret = snprintf(dict_key, sizeof dict_key, "key%d", i); + ret = dict_get_strn(dict, dict_key, ret, &key); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to retrieve %s from the dictionary", dict_key); + goto out; + } + ret = snprintf(dict_key, sizeof dict_key, "value%d", i); + ret = dict_get_strn(dict, dict_key, ret, &value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to retrieve key value for %s from the dictionary", + dict_key); + goto out; + } + cli_out("%-40s%-40s", key, value); + } + +out: + if (ret) { + cli_out( + "volume get option failed. Check the cli/glusterd log " + "file for more details"); + } + +out_nolog: + if (dict) + dict_unref(dict); + cli_cmd_broadcast_response(ret); + gf_free_xdr_cli_rsp(rsp); + return ret; +} + +static int +gf_cli_get_vol_opt(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + dict_t *options = NULL; + int ret = -1; + + options = data; + + ret = cli_to_glusterd(&req, frame, gf_cli_get_vol_opt_cbk, + (xdrproc_t)xdr_gf_cli_req, options, + GLUSTER_CLI_GET_VOL_OPT, this, cli_rpc_prog, NULL); + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + + GF_FREE(req.dict.dict_val); + return ret; +} + +static int +add_cli_cmd_timeout_to_dict(dict_t *dict) +{ + int ret = 0; + + if (cli_default_conn_timeout > 120) { + ret = dict_set_uint32(dict, "timeout", cli_default_conn_timeout); + if (ret) { + gf_log("cli", GF_LOG_INFO, "Failed to save timeout to dict"); + } + } + return ret; +} + +static int +cli_to_glusterd(gf_cli_req *req, call_frame_t *frame, fop_cbk_fn_t cbkfn, + xdrproc_t xdrproc, dict_t *dict, int procnum, xlator_t *this, + rpc_clnt_prog_t *prog, struct iobref *iobref) +{ + int ret = 0; + size_t len = 0; + char *cmd = NULL; + int i = 0; + const char **words = NULL; + cli_local_t *local = NULL; + + if (!this || !frame || !frame->local || !dict) { + ret = -1; + goto out; + } + + local = frame->local; + + if (!local->words) { + ret = -1; + goto out; + } + + words = local->words; + + while (words[i]) + len += strlen(words[i++]) + 1; + + cmd = GF_MALLOC(len + 1, gf_common_mt_char); + if (!cmd) { + ret = -1; + goto out; + } + cmd[0] = '\0'; + + for (i = 0; words[i]; i++) { + strncat(cmd, words[i], len - 1); + if (words[i + 1] != NULL) + strncat(cmd, " ", len - 1); + } + + ret = dict_set_dynstr_sizen(dict, "cmd-str", cmd); + if (ret) + goto out; + + ret = add_cli_cmd_timeout_to_dict(dict); + + ret = dict_allocate_and_serialize(dict, &(req->dict).dict_val, + &(req->dict).dict_len); + + if (ret < 0) { + gf_log(this->name, GF_LOG_DEBUG, + "failed to get serialized length of dict"); + goto out; + } + + ret = cli_cmd_submit(NULL, req, frame, prog, procnum, iobref, this, cbkfn, + (xdrproc_t)xdrproc); +out: + return ret; +} + +static int +gf_cli_print_bitrot_scrub_status(dict_t *dict) +{ + int i = 1; + int j = 0; + int ret = -1; + int count = 0; + char key[64] = { + 0, + }; + char *volname = NULL; + char *node_name = NULL; + char *scrub_freq = NULL; + char *state_scrub = NULL; + char *scrub_impact = NULL; + char *bad_file_str = NULL; + char *scrub_log_file = NULL; + char *bitrot_log_file = NULL; + uint64_t scrub_files = 0; + uint64_t unsigned_files = 0; + uint64_t scrub_time = 0; + uint64_t days = 0; + uint64_t hours = 0; + uint64_t minutes = 0; + uint64_t seconds = 0; + char *last_scrub = NULL; + uint64_t error_count = 0; + int8_t scrub_running = 0; + char *scrub_state_op = NULL; + + ret = dict_get_int32_sizen(dict, "count", &count); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "failed to get count value from dictionary"); + goto out; + } + + ret = dict_get_str_sizen(dict, "volname", &volname); + if (ret) + gf_log("cli", GF_LOG_TRACE, "failed to get volume name"); + + ret = dict_get_str_sizen(dict, "features.scrub", &state_scrub); + if (ret) + gf_log("cli", GF_LOG_TRACE, "failed to get scrub state value"); + + ret = dict_get_str_sizen(dict, "features.scrub-throttle", &scrub_impact); + if (ret) + gf_log("cli", GF_LOG_TRACE, "failed to get scrub impact value"); + + ret = dict_get_str_sizen(dict, "features.scrub-freq", &scrub_freq); + if (ret) + gf_log("cli", GF_LOG_TRACE, "failed to get scrub -freq value"); + + ret = dict_get_str_sizen(dict, "bitrot_log_file", &bitrot_log_file); + if (ret) + gf_log("cli", GF_LOG_TRACE, "failed to get bitrot log file location"); + + ret = dict_get_str_sizen(dict, "scrub_log_file", &scrub_log_file); + if (ret) + gf_log("cli", GF_LOG_TRACE, "failed to get scrubber log file location"); + + for (i = 1; i <= count; i++) { + snprintf(key, sizeof(key), "scrub-running-%d", i); + ret = dict_get_int8(dict, key, &scrub_running); + if (ret) + gf_log("cli", GF_LOG_TRACE, "failed to get scrubbed files"); + if (scrub_running) + break; + } + + if (scrub_running) + gf_asprintf(&scrub_state_op, "%s (In Progress)", state_scrub); + else + gf_asprintf(&scrub_state_op, "%s (Idle)", state_scrub); + + cli_out("\n%s: %s\n", "Volume name ", volname); + + cli_out("%s: %s\n", "State of scrub", scrub_state_op); + + cli_out("%s: %s\n", "Scrub impact", scrub_impact); + + cli_out("%s: %s\n", "Scrub frequency", scrub_freq); + + cli_out("%s: %s\n", "Bitrot error log location", bitrot_log_file); + + cli_out("%s: %s\n", "Scrubber error log location", scrub_log_file); + + for (i = 1; i <= count; i++) { + /* Reset the variables to prevent carryover of values */ + node_name = NULL; + last_scrub = NULL; + scrub_time = 0; + error_count = 0; + scrub_files = 0; + unsigned_files = 0; + + snprintf(key, sizeof(key), "node-name-%d", i); + ret = dict_get_str(dict, key, &node_name); + if (ret) + gf_log("cli", GF_LOG_TRACE, "failed to get node-name"); + + snprintf(key, sizeof(key), "scrubbed-files-%d", i); + ret = dict_get_uint64(dict, key, &scrub_files); + if (ret) + gf_log("cli", GF_LOG_TRACE, "failed to get scrubbed files"); + + snprintf(key, sizeof(key), "unsigned-files-%d", i); + ret = dict_get_uint64(dict, key, &unsigned_files); + if (ret) + gf_log("cli", GF_LOG_TRACE, "failed to get unsigned files"); + + snprintf(key, sizeof(key), "scrub-duration-%d", i); + ret = dict_get_uint64(dict, key, &scrub_time); + if (ret) + gf_log("cli", GF_LOG_TRACE, "failed to get last scrub duration"); + + snprintf(key, sizeof(key), "last-scrub-time-%d", i); + ret = dict_get_str(dict, key, &last_scrub); + if (ret) + gf_log("cli", GF_LOG_TRACE, "failed to get last scrub time"); + snprintf(key, sizeof(key), "error-count-%d", i); + ret = dict_get_uint64(dict, key, &error_count); + if (ret) + gf_log("cli", GF_LOG_TRACE, "failed to get error count"); + + cli_out("\n%s\n", + "========================================================="); + + cli_out("%s: %s\n", "Node", node_name); + + cli_out("%s: %" PRIu64 "\n", "Number of Scrubbed files", scrub_files); + + cli_out("%s: %" PRIu64 "\n", "Number of Skipped files", unsigned_files); + + if ((!last_scrub) || !strcmp(last_scrub, "")) + cli_out("%s: %s\n", "Last completed scrub time", + "Scrubber pending to complete."); + else + cli_out("%s: %s\n", "Last completed scrub time", last_scrub); + + /* Printing last scrub duration time in human readable form*/ + seconds = scrub_time % 60; + minutes = (scrub_time / 60) % 60; + hours = (scrub_time / 3600) % 24; + days = scrub_time / 86400; + cli_out("%s: %" PRIu64 ":%" PRIu64 ":%" PRIu64 ":%" PRIu64 "\n", + "Duration of last scrub (D:M:H:M:S)", days, hours, minutes, + seconds); + + cli_out("%s: %" PRIu64 "\n", "Error count", error_count); + + if (error_count) { + cli_out("%s:\n", "Corrupted object's [GFID]"); + /* Printing list of bad file's (Corrupted object's)*/ + for (j = 0; j < error_count; j++) { + snprintf(key, sizeof(key), "quarantine-%d-%d", j, i); + ret = dict_get_str(dict, key, &bad_file_str); + if (!ret) { + cli_out("%s\n", bad_file_str); + } + } + } + } + cli_out("%s\n", + "========================================================="); + +out: + GF_FREE(scrub_state_op); + return 0; +} + +static int +gf_cli_bitrot_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + int ret = -1; + int type = 0; + gf_cli_rsp rsp = { + 0, + }; + dict_t *dict = NULL; + char *scrub_cmd = NULL; + char *volname = NULL; + char *cmd_str = NULL; + char *cmd_op = NULL; + + GF_ASSERT(myframe); + + if (req->rpc_status == -1) { + goto out; + } + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_gf_cli_rsp); + if (ret < 0) { + gf_log(((call_frame_t *)myframe)->this->name, GF_LOG_ERROR, + XDR_DECODE_FAIL); + goto out; + } + + if (rsp.op_ret) { + ret = -1; + if (global_state->mode & GLUSTER_MODE_XML) + goto xml_output; + + if (strcmp(rsp.op_errstr, "")) + cli_err("Bitrot command failed : %s", rsp.op_errstr); + else + cli_err("Bitrot command : failed"); + + goto out; + } + + if (rsp.dict.dict_len) { + /* Unserialize the dictionary */ + dict = dict_new(); + + if (!dict) { + ret = -1; + goto out; + } + + ret = dict_unserialize(rsp.dict.dict_val, rsp.dict.dict_len, &dict); + + if (ret) { + gf_log("cli", GF_LOG_ERROR, DICT_UNSERIALIZE_FAIL); + goto out; + } + } + + gf_log("cli", GF_LOG_DEBUG, "Received resp to bit rot command"); + + ret = dict_get_int32_sizen(dict, "type", &type); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get command type"); + goto out; + } + + if ((type == GF_BITROT_CMD_SCRUB_STATUS) && + !(global_state->mode & GLUSTER_MODE_XML)) { + ret = gf_cli_print_bitrot_scrub_status(dict); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to print bitrot scrub status"); + } + goto out; + } + + /* Ignoring the error, as using dict val for cli output only */ + ret = dict_get_str_sizen(dict, "volname", &volname); + if (ret) + gf_log("cli", GF_LOG_TRACE, "failed to get volume name"); + + ret = dict_get_str_sizen(dict, "scrub-value", &scrub_cmd); + if (ret) + gf_log("cli", GF_LOG_TRACE, "Failed to get scrub command"); + + ret = dict_get_str_sizen(dict, "cmd-str", &cmd_str); + if (ret) + gf_log("cli", GF_LOG_TRACE, "failed to get command string"); + + if (cmd_str) + cmd_op = strrchr(cmd_str, ' ') + 1; + + switch (type) { + case GF_BITROT_OPTION_TYPE_ENABLE: + cli_out("volume bitrot: success bitrot enabled for volume %s", + volname); + ret = 0; + goto out; + case GF_BITROT_OPTION_TYPE_DISABLE: + cli_out("volume bitrot: success bitrot disabled for volume %s", + volname); + ret = 0; + goto out; + case GF_BITROT_CMD_SCRUB_ONDEMAND: + cli_out("volume bitrot: scrubber started ondemand for volume %s", + volname); + ret = 0; + goto out; + case GF_BITROT_OPTION_TYPE_SCRUB: + if (!strncmp("pause", scrub_cmd, sizeof("pause"))) + cli_out("volume bitrot: scrubber paused for volume %s", + volname); + if (!strncmp("resume", scrub_cmd, sizeof("resume"))) + cli_out("volume bitrot: scrubber resumed for volume %s", + volname); + ret = 0; + goto out; + case GF_BITROT_OPTION_TYPE_SCRUB_FREQ: + cli_out( + "volume bitrot: scrub-frequency is set to %s " + "successfully for volume %s", + cmd_op, volname); + ret = 0; + goto out; + case GF_BITROT_OPTION_TYPE_SCRUB_THROTTLE: + cli_out( + "volume bitrot: scrub-throttle is set to %s " + "successfully for volume %s", + cmd_op, volname); + ret = 0; + goto out; + } + +xml_output: + if (global_state->mode & GLUSTER_MODE_XML) { + ret = cli_xml_output_vol_profile(dict, rsp.op_ret, rsp.op_errno, + rsp.op_errstr); + if (ret) + gf_log("cli", GF_LOG_ERROR, XML_ERROR); + goto out; + } + + if (!rsp.op_ret) + cli_out("volume bitrot: success"); + + ret = rsp.op_ret; + +out: + if (dict) + dict_unref(dict); + + gf_free_xdr_cli_rsp(rsp); + cli_cmd_broadcast_response(ret); + return ret; +} + +static int32_t +gf_cli_bitrot(call_frame_t *frame, xlator_t *this, void *data) +{ + gf_cli_req req = {{ + 0, + }}; + dict_t *options = NULL; + int ret = -1; + + options = data; + + ret = cli_to_glusterd(&req, frame, gf_cli_bitrot_cbk, + (xdrproc_t)xdr_gf_cli_req, options, + GLUSTER_CLI_BITROT, this, cli_rpc_prog, NULL); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "cli_to_glusterd for bitrot failed"); + goto out; + } + +out: + gf_log("cli", GF_LOG_DEBUG, RETURNING, ret); + + GF_FREE(req.dict.dict_val); + + return ret; +} + +static struct rpc_clnt_procedure gluster_cli_actors[GLUSTER_CLI_MAXVALUE] = { + [GLUSTER_CLI_NULL] = {"NULL", NULL}, + [GLUSTER_CLI_PROBE] = {"PROBE_QUERY", gf_cli_probe}, + [GLUSTER_CLI_DEPROBE] = {"DEPROBE_QUERY", gf_cli_deprobe}, + [GLUSTER_CLI_LIST_FRIENDS] = {"LIST_FRIENDS", gf_cli_list_friends}, + [GLUSTER_CLI_UUID_RESET] = {"UUID_RESET", gf_cli3_1_uuid_reset}, + [GLUSTER_CLI_UUID_GET] = {"UUID_GET", gf_cli3_1_uuid_get}, + [GLUSTER_CLI_CREATE_VOLUME] = {"CREATE_VOLUME", gf_cli_create_volume}, + [GLUSTER_CLI_DELETE_VOLUME] = {"DELETE_VOLUME", gf_cli_delete_volume}, + [GLUSTER_CLI_START_VOLUME] = {"START_VOLUME", gf_cli_start_volume}, + [GLUSTER_CLI_STOP_VOLUME] = {"STOP_VOLUME", gf_cli_stop_volume}, + [GLUSTER_CLI_RENAME_VOLUME] = {"RENAME_VOLUME", gf_cli_rename_volume}, + [GLUSTER_CLI_DEFRAG_VOLUME] = {"DEFRAG_VOLUME", gf_cli_defrag_volume}, + [GLUSTER_CLI_GET_VOLUME] = {"GET_VOLUME", gf_cli_get_volume}, + [GLUSTER_CLI_GET_NEXT_VOLUME] = {"GET_NEXT_VOLUME", gf_cli_get_next_volume}, + [GLUSTER_CLI_SET_VOLUME] = {"SET_VOLUME", gf_cli_set_volume}, + [GLUSTER_CLI_ADD_BRICK] = {"ADD_BRICK", gf_cli_add_brick}, + [GLUSTER_CLI_REMOVE_BRICK] = {"REMOVE_BRICK", gf_cli_remove_brick}, + [GLUSTER_CLI_REPLACE_BRICK] = {"REPLACE_BRICK", gf_cli_replace_brick}, + [GLUSTER_CLI_LOG_ROTATE] = {"LOG ROTATE", gf_cli_log_rotate}, + [GLUSTER_CLI_GETSPEC] = {"GETSPEC", gf_cli_getspec}, + [GLUSTER_CLI_PMAP_PORTBYBRICK] = {"PMAP PORTBYBRICK", gf_cli_pmap_b2p}, + [GLUSTER_CLI_SYNC_VOLUME] = {"SYNC_VOLUME", gf_cli_sync_volume}, + [GLUSTER_CLI_RESET_VOLUME] = {"RESET_VOLUME", gf_cli_reset_volume}, + [GLUSTER_CLI_FSM_LOG] = {"FSM_LOG", gf_cli_fsm_log}, + [GLUSTER_CLI_GSYNC_SET] = {"GSYNC_SET", gf_cli_gsync_set}, + [GLUSTER_CLI_PROFILE_VOLUME] = {"PROFILE_VOLUME", gf_cli_profile_volume}, + [GLUSTER_CLI_QUOTA] = {"QUOTA", gf_cli_quota}, + [GLUSTER_CLI_TOP_VOLUME] = {"TOP_VOLUME", gf_cli_top_volume}, + [GLUSTER_CLI_GETWD] = {"GETWD", gf_cli_getwd}, + [GLUSTER_CLI_STATUS_VOLUME] = {"STATUS_VOLUME", gf_cli_status_volume}, + [GLUSTER_CLI_STATUS_ALL] = {"STATUS_ALL", gf_cli_status_volume_all}, + [GLUSTER_CLI_MOUNT] = {"MOUNT", gf_cli_mount}, + [GLUSTER_CLI_UMOUNT] = {"UMOUNT", gf_cli_umount}, + [GLUSTER_CLI_HEAL_VOLUME] = {"HEAL_VOLUME", gf_cli_heal_volume}, + [GLUSTER_CLI_STATEDUMP_VOLUME] = {"STATEDUMP_VOLUME", + gf_cli_statedump_volume}, + [GLUSTER_CLI_LIST_VOLUME] = {"LIST_VOLUME", gf_cli_list_volume}, + [GLUSTER_CLI_CLRLOCKS_VOLUME] = {"CLEARLOCKS_VOLUME", + gf_cli_clearlocks_volume}, + [GLUSTER_CLI_COPY_FILE] = {"COPY_FILE", gf_cli_copy_file}, + [GLUSTER_CLI_SYS_EXEC] = {"SYS_EXEC", gf_cli_sys_exec}, + [GLUSTER_CLI_SNAP] = {"SNAP", gf_cli_snapshot}, + [GLUSTER_CLI_BARRIER_VOLUME] = {"BARRIER VOLUME", gf_cli_barrier_volume}, + [GLUSTER_CLI_GET_VOL_OPT] = {"GET_VOL_OPT", gf_cli_get_vol_opt}, + [GLUSTER_CLI_BITROT] = {"BITROT", gf_cli_bitrot}, + [GLUSTER_CLI_GET_STATE] = {"GET_STATE", gf_cli_get_state}, + [GLUSTER_CLI_RESET_BRICK] = {"RESET_BRICK", gf_cli_reset_brick}, + [GLUSTER_CLI_GANESHA] = {"GANESHA", gf_cli_ganesha}, +}; + +struct rpc_clnt_program cli_prog = { + .progname = "Gluster CLI", + .prognum = GLUSTER_CLI_PROGRAM, + .progver = GLUSTER_CLI_VERSION, + .numproc = GLUSTER_CLI_MAXVALUE, + .proctable = gluster_cli_actors, +}; + +static struct rpc_clnt_procedure cli_quotad_procs[GF_AGGREGATOR_MAXVALUE] = { + [GF_AGGREGATOR_NULL] = {"NULL", NULL}, + [GF_AGGREGATOR_LOOKUP] = {"LOOKUP", NULL}, + [GF_AGGREGATOR_GETLIMIT] = {"GETLIMIT", cli_quotad_getlimit}, +}; + +struct rpc_clnt_program cli_quotad_clnt = { + .progname = "CLI Quotad client", + .prognum = GLUSTER_AGGREGATOR_PROGRAM, + .progver = GLUSTER_AGGREGATOR_VERSION, + .numproc = GF_AGGREGATOR_MAXVALUE, + .proctable = cli_quotad_procs, +}; diff --git a/cli/src/cli-xml-output.c b/cli/src/cli-xml-output.c new file mode 100644 index 00000000000..069de75801c --- /dev/null +++ b/cli/src/cli-xml-output.c @@ -0,0 +1,5839 @@ +/* + Copyright (c) 2010-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + 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 <stdlib.h> +#include "cli.h" +#include "cli1-xdr.h" +#include <glusterfs/run.h> +#include <glusterfs/compat.h> +#include <glusterfs/syscall.h> +#include <glusterfs/upcall-utils.h> + +enum gf_task_types { GF_TASK_TYPE_REBALANCE, GF_TASK_TYPE_REMOVE_BRICK }; + +/* + * IMPORTANT NOTE: + * All exported functions in this file which use libxml need use a + * #if (HAVE_LIB_XML), #else, #endif + * For eg, + * int exported_func () { + * #if (HAVE_LIB_XML) + * <Stuff using libxml> + * #else + * return 0; + * #endif + * } + * + * All other functions, which are called internally within this file need to be + * within #if (HAVE_LIB_XML), #endif statements + * For eg, + * #if (HAVE_LIB_XML) + * int internal_func () + * { + * } + * #endif + * + * Following the above format ensures that all xml related code is compiled + * only when libxml2 is present, and also keeps the rest of the codebase free + * of #if (HAVE_LIB_XML) + */ + +#if (HAVE_LIB_XML) + +#include <libxml/encoding.h> +#include <libxml/xmlwriter.h> + +#define XML_RET_CHECK_AND_GOTO(ret, label) \ + do { \ + if (ret < 0) { \ + ret = -1; \ + goto label; \ + } else \ + ret = 0; \ + } while (0) + +int +cli_begin_xml_output(xmlTextWriterPtr *writer, xmlDocPtr *doc) +{ + int ret = -1; + + *writer = xmlNewTextWriterDoc(doc, 0); + if (*writer == NULL) { + ret = -1; + goto out; + } + + ret = xmlTextWriterStartDocument(*writer, "1.0", "UTF-8", "yes"); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* <cliOutput> */ + ret = xmlTextWriterStartElement(*writer, (xmlChar *)"cliOutput"); + XML_RET_CHECK_AND_GOTO(ret, out); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} + +int +cli_end_xml_output(xmlTextWriterPtr writer, xmlDocPtr doc) +{ + int ret = -1; + + /* </cliOutput> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterEndDocument(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* Dump xml document to stdout and pretty format it */ + xmlSaveFormatFileEnc("-", doc, "UTF-8", 1); + + xmlFreeTextWriter(writer); + xmlFreeDoc(doc); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} + +int +cli_xml_output_common(xmlTextWriterPtr writer, int op_ret, int op_errno, + char *op_errstr) +{ + int ret = -1; + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"opRet", "%d", + op_ret); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"opErrno", "%d", + op_errno); + XML_RET_CHECK_AND_GOTO(ret, out); + + if (op_errstr) + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"opErrstr", + "%s", op_errstr); + else + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"opErrstr", + "%s", ""); + + XML_RET_CHECK_AND_GOTO(ret, out); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} +#endif + +int +cli_xml_output_str(char *op, char *str, int op_ret, int op_errno, + char *op_errstr) +{ +#if (HAVE_LIB_XML) + int ret = -1; + xmlTextWriterPtr writer = NULL; + xmlDocPtr doc = NULL; + + ret = cli_begin_xml_output(&writer, &doc); + if (ret) + goto out; + + ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr); + if (ret) + goto out; + + if (op) { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"cliOp", "%s", + op); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + if (str) { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"output", "%s", + str); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + ret = cli_end_xml_output(writer, doc); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +#else + return 0; +#endif +} + +#if (HAVE_LIB_XML) +int +cli_xml_output_data_pair(dict_t *this, char *key, data_t *value, void *data) +{ + int ret = -1; + xmlTextWriterPtr *writer = NULL; + + writer = (xmlTextWriterPtr *)data; + + ret = xmlTextWriterWriteFormatElement(*writer, (xmlChar *)key, "%s", + value->data); + + XML_RET_CHECK_AND_GOTO(ret, out); +out: + return ret; +} +#endif + +int +cli_xml_output_dict(char *op, dict_t *dict, int op_ret, int op_errno, + char *op_errstr) +{ +#if (HAVE_LIB_XML) + int ret = -1; + xmlTextWriterPtr writer = NULL; + xmlDocPtr doc = NULL; + + ret = cli_begin_xml_output(&writer, &doc); + if (ret) + goto out; + + ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr); + if (ret) + goto out; + + /* <"op"> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)op); + XML_RET_CHECK_AND_GOTO(ret, out); + + if (dict) + dict_foreach(dict, cli_xml_output_data_pair, &writer); + + /* </"op"> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = cli_end_xml_output(writer, doc); +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +#else + return 0; +#endif +} + +#if (HAVE_LIB_XML) +int +cli_xml_output_vol_status_common(xmlTextWriterPtr writer, dict_t *dict, + int brick_index, int *online, + gf_boolean_t *node_present) +{ + int ret = -1; + char *hostname = NULL; + char *path = NULL; + char *uuid = NULL; + int port = 0; + int rdma_port = 0; + int status = 0; + int pid = 0; + char key[1024] = { + 0, + }; + + snprintf(key, sizeof(key), "brick%d.hostname", brick_index); + ret = dict_get_str(dict, key, &hostname); + if (ret) { + *node_present = _gf_false; + goto out; + } + *node_present = _gf_true; + + /* <node> + * will be closed in the calling function cli_xml_output_vol_status()*/ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"node"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"hostname", "%s", + hostname); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "brick%d.path", brick_index); + ret = dict_get_str(dict, key, &path); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"path", "%s", + path); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "brick%d.peerid", brick_index); + ret = dict_get_str(dict, key, &uuid); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"peerid", "%s", + uuid); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "brick%d.status", brick_index); + ret = dict_get_int32(dict, key, &status); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"status", "%d", + status); + XML_RET_CHECK_AND_GOTO(ret, out); + *online = status; + + snprintf(key, sizeof(key), "brick%d.port", brick_index); + ret = dict_get_int32(dict, key, &port); + if (ret) + goto out; + + snprintf(key, sizeof(key), "brick%d.rdma_port", brick_index); + ret = dict_get_int32(dict, key, &rdma_port); + + /* If the process is either offline or doesn't provide a port (shd) + * port = "N/A" + * else print the port number of the process. + */ + + /* + * Tag 'port' can be removed once console management is started + * to support new tag ports. + */ + + if (*online == 1 && port != 0) + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"port", "%d", + port); + else + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"port", "%s", + "N/A"); + XML_RET_CHECK_AND_GOTO(ret, out); + ret = xmlTextWriterStartElement(writer, (xmlChar *)"ports"); + if (*online == 1 && (port != 0 || rdma_port != 0)) { + if (port) { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"tcp", + "%d", port); + } else { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"tcp", + "%s", "N/A"); + } + XML_RET_CHECK_AND_GOTO(ret, out); + if (rdma_port) { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"rdma", + "%d", rdma_port); + } else { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"rdma", + "%s", "N/A"); + } + XML_RET_CHECK_AND_GOTO(ret, out); + } else { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"tcp", "%s", + "N/A"); + XML_RET_CHECK_AND_GOTO(ret, out); + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"rdma", "%s", + "N/A"); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "brick%d.pid", brick_index); + ret = dict_get_int32(dict, key, &pid); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"pid", "%d", pid); + XML_RET_CHECK_AND_GOTO(ret, out); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} + +int +cli_xml_output_vol_status_detail(xmlTextWriterPtr writer, dict_t *dict, + int brick_index) +{ + int ret = -1; + uint64_t size_total = 0; + uint64_t size_free = 0; + char *device = NULL; + uint64_t block_size = 0; + char *mnt_options = NULL; + char *fs_name = NULL; + char *inode_size = NULL; + uint64_t inodes_total = 0; + uint64_t inodes_free = 0; + char key[1024] = { + 0, + }; + + snprintf(key, sizeof(key), "brick%d.total", brick_index); + ret = dict_get_uint64(dict, key, &size_total); + if (!ret) { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"sizeTotal", + "%" PRIu64, size_total); + XML_RET_CHECK_AND_GOTO(ret, out); + } + snprintf(key, sizeof(key), "brick%d.free", brick_index); + ret = dict_get_uint64(dict, key, &size_free); + if (!ret) { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"sizeFree", + "%" PRIu64, size_free); + XML_RET_CHECK_AND_GOTO(ret, out); + } + snprintf(key, sizeof(key), "brick%d.device", brick_index); + ret = dict_get_str(dict, key, &device); + if (!ret) { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"device", "%s", + device); + XML_RET_CHECK_AND_GOTO(ret, out); + } + snprintf(key, sizeof(key), "brick%d.block_size", brick_index); + ret = dict_get_uint64(dict, key, &block_size); + if (!ret) { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"blockSize", + "%" PRIu64, block_size); + XML_RET_CHECK_AND_GOTO(ret, out); + } + snprintf(key, sizeof(key), "brick%d.mnt_options", brick_index); + ret = dict_get_str(dict, key, &mnt_options); + if (!ret) { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"mntOptions", + "%s", mnt_options); + XML_RET_CHECK_AND_GOTO(ret, out); + } + snprintf(key, sizeof(key), "brick%d.fs_name", brick_index); + ret = dict_get_str(dict, key, &fs_name); + if (!ret) { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"fsName", "%s", + fs_name); + XML_RET_CHECK_AND_GOTO(ret, out); + } + snprintf(key, sizeof(key), "brick%d.inode_size", brick_index); + ret = dict_get_str(dict, key, &inode_size); + if (!ret) { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"inodeSize", + "%s", fs_name); + XML_RET_CHECK_AND_GOTO(ret, out); + } + snprintf(key, sizeof(key), "brick%d.total_inodes", brick_index); + ret = dict_get_uint64(dict, key, &inodes_total); + if (!ret) { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"inodesTotal", + "%" PRIu64, inodes_total); + XML_RET_CHECK_AND_GOTO(ret, out); + } + snprintf(key, sizeof(key), "brick%d.free_inodes", brick_index); + ret = dict_get_uint64(dict, key, &inodes_free); + if (!ret) { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"inodesFree", + "%" PRIu64, inodes_free); + XML_RET_CHECK_AND_GOTO(ret, out); + } else { + ret = 0; + } + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} + +int +cli_xml_output_vol_status_mempool(xmlTextWriterPtr writer, dict_t *dict, + char *prefix) +{ + int ret = -1; + int mempool_count = 0; + char *name = NULL; + int hotcount = 0; + int coldcount = 0; + uint64_t paddedsizeof = 0; + uint64_t alloccount = 0; + int maxalloc = 0; + char key[1024] = { + 0, + }; + int i = 0; + + /* <mempool> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"mempool"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.mempool-count", prefix); + ret = dict_get_int32(dict, key, &mempool_count); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"count", "%d", + mempool_count); + XML_RET_CHECK_AND_GOTO(ret, out); + + for (i = 0; i < mempool_count; i++) { + /* <pool> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"pool"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.pool%d.name", prefix, i); + ret = dict_get_str(dict, key, &name); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s", + name); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.pool%d.hotcount", prefix, i); + ret = dict_get_int32(dict, key, &hotcount); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"hotCount", + "%d", hotcount); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.pool%d.coldcount", prefix, i); + ret = dict_get_int32(dict, key, &coldcount); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"coldCount", + "%d", coldcount); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.pool%d.paddedsizeof", prefix, i); + ret = dict_get_uint64(dict, key, &paddedsizeof); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement( + writer, (xmlChar *)"padddedSizeOf", "%" PRIu64, paddedsizeof); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.pool%d.alloccount", prefix, i); + ret = dict_get_uint64(dict, key, &alloccount); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"allocCount", + "%" PRIu64, alloccount); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.pool%d.max_alloc", prefix, i); + ret = dict_get_int32(dict, key, &maxalloc); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"maxAlloc", + "%d", maxalloc); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.pool%d.pool-misses", prefix, i); + ret = dict_get_uint64(dict, key, &alloccount); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"poolMisses", + "%" PRIu64, alloccount); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.pool%d.max-stdalloc", prefix, i); + ret = dict_get_int32(dict, key, &maxalloc); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"maxStdAlloc", + "%d", maxalloc); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </pool> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + /* </mempool> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} + +int +cli_xml_output_vol_status_mem(xmlTextWriterPtr writer, dict_t *dict, + int brick_index) +{ + int ret = -1; + int arena = 0; + int ordblks = 0; + int smblks = 0; + int hblks = 0; + int hblkhd = 0; + int usmblks = 0; + int fsmblks = 0; + int uordblks = 0; + int fordblks = 0; + int keepcost = 0; + char key[1024] = { + 0, + }; + + /* <memStatus> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"memStatus"); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* <mallinfo> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"mallinfo"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "brick%d.mallinfo.arena", brick_index); + ret = dict_get_int32(dict, key, &arena); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"arena", "%d", + arena); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "brick%d.mallinfo.ordblks", brick_index); + ret = dict_get_int32(dict, key, &ordblks); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"ordblks", "%d", + ordblks); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "brick%d.mallinfo.smblks", brick_index); + ret = dict_get_int32(dict, key, &smblks); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"smblks", "%d", + smblks); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "brick%d.mallinfo.hblks", brick_index); + ret = dict_get_int32(dict, key, &hblks); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"hblks", "%d", + hblks); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "brick%d.mallinfo.hblkhd", brick_index); + ret = dict_get_int32(dict, key, &hblkhd); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"hblkhd", "%d", + hblkhd); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "brick%d.mallinfo.usmblks", brick_index); + ret = dict_get_int32(dict, key, &usmblks); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"usmblks", "%d", + usmblks); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "brick%d.mallinfo.fsmblks", brick_index); + ret = dict_get_int32(dict, key, &fsmblks); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"fsmblks", "%d", + fsmblks); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "brick%d.mallinfo.uordblks", brick_index); + ret = dict_get_int32(dict, key, &uordblks); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"uordblks", "%d", + uordblks); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "brick%d.mallinfo.fordblks", brick_index); + ret = dict_get_int32(dict, key, &fordblks); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"fordblks", "%d", + fordblks); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "brick%d.mallinfo.keepcost", brick_index); + ret = dict_get_int32(dict, key, &keepcost); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"keepcost", "%d", + keepcost); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </mallinfo> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "brick%d", brick_index); + ret = cli_xml_output_vol_status_mempool(writer, dict, key); + if (ret) + goto out; + + /* </memStatus> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} + +int +cli_xml_output_vol_status_clients(xmlTextWriterPtr writer, dict_t *dict, + int brick_index) +{ + int ret = -1; + int client_count = 0; + char *hostname = NULL; + uint64_t bytes_read = 0; + uint64_t bytes_write = 0; + uint32_t opversion = 0; + char key[1024] = { + 0, + }; + int i = 0; + + /* <clientsStatus> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"clientsStatus"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "brick%d.clientcount", brick_index); + ret = dict_get_int32(dict, key, &client_count); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"clientCount", + "%d", client_count); + XML_RET_CHECK_AND_GOTO(ret, out); + + for (i = 0; i < client_count; i++) { + /* <client> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"client"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "brick%d.client%d.hostname", brick_index, i); + ret = dict_get_str(dict, key, &hostname); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"hostname", + "%s", hostname); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "brick%d.client%d.bytesread", brick_index, + i); + ret = dict_get_uint64(dict, key, &bytes_read); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"bytesRead", + "%" PRIu64, bytes_read); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "brick%d.client%d.byteswrite", brick_index, + i); + ret = dict_get_uint64(dict, key, &bytes_write); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"bytesWrite", + "%" PRIu64, bytes_write); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "brick%d.client%d.opversion", brick_index, + i); + ret = dict_get_uint32(dict, key, &opversion); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"opVersion", + "%" PRIu32, opversion); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </client> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + /* </clientsStatus> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} + +int +cli_xml_output_vol_status_inode_entry(xmlTextWriterPtr writer, dict_t *dict, + char *prefix) +{ + int ret = -1; + char *gfid = NULL; + uint64_t nlookup = 0; + uint32_t ref = 0; + int ia_type = 0; + char key[1024] = { + 0, + }; + + /* <inode> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"inode"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.gfid", prefix); + ret = dict_get_str(dict, key, &gfid); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"gfid", "%s", + gfid); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.nlookup", prefix); + ret = dict_get_uint64(dict, key, &nlookup); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"nLookup", + "%" PRIu64, nlookup); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.ref", prefix); + ret = dict_get_uint32(dict, key, &ref); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"ref", "%" PRIu32, + ref); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.ia_type", prefix); + ret = dict_get_int32(dict, key, &ia_type); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"iaType", "%d", + ia_type); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </inode> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} + +int +cli_xml_output_vol_status_itable(xmlTextWriterPtr writer, dict_t *dict, + char *prefix) +{ + int ret = -1; + uint32_t active_size = 0; + uint32_t lru_size = 0; + uint32_t purge_size = 0; + char key[1024] = { + 0, + }; + int i = 0; + + snprintf(key, sizeof(key), "%s.active_size", prefix); + ret = dict_get_uint32(dict, key, &active_size); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"activeSize", + "%" PRIu32, active_size); + XML_RET_CHECK_AND_GOTO(ret, out); + if (active_size != 0) { + /* <active> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"active"); + XML_RET_CHECK_AND_GOTO(ret, out); + + for (i = 0; i < active_size; i++) { + snprintf(key, sizeof(key), "%s.active%d", prefix, i); + ret = cli_xml_output_vol_status_inode_entry(writer, dict, key); + if (ret) + goto out; + } + /* </active> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + snprintf(key, sizeof(key), "%s.lru_size", prefix); + ret = dict_get_uint32(dict, key, &lru_size); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"lruSize", + "%" PRIu32, lru_size); + XML_RET_CHECK_AND_GOTO(ret, out); + if (lru_size != 0) { + /* <lru> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"lru"); + XML_RET_CHECK_AND_GOTO(ret, out); + + for (i = 0; i < lru_size; i++) { + snprintf(key, sizeof(key), "%s.lru%d", prefix, i); + ret = cli_xml_output_vol_status_inode_entry(writer, dict, key); + if (ret) + goto out; + } + /* </lru> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + snprintf(key, sizeof(key), "%s.purge_size", prefix); + ret = dict_get_uint32(dict, key, &purge_size); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"purgeSize", + "%" PRIu32, purge_size); + XML_RET_CHECK_AND_GOTO(ret, out); + if (purge_size != 0) { + /* <purge> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"purge"); + XML_RET_CHECK_AND_GOTO(ret, out); + + for (i = 0; i < purge_size; i++) { + snprintf(key, sizeof(key), "%s.purge%d", prefix, i); + ret = cli_xml_output_vol_status_inode_entry(writer, dict, key); + if (ret) + goto out; + } + /* </purge> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + } + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} + +int +cli_xml_output_vol_status_inode(xmlTextWriterPtr writer, dict_t *dict, + int brick_index) +{ + int ret = -1; + int conn_count = 0; + char key[1024] = { + 0, + }; + int i = 0; + + /* <inodeStatus> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"inodeStatus"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "brick%d.conncount", brick_index); + ret = dict_get_int32(dict, key, &conn_count); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"connections", + "%d", conn_count); + XML_RET_CHECK_AND_GOTO(ret, out); + + for (i = 0; i < conn_count; i++) { + /* <connection> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"connection"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "brick%d.conn%d.itable", brick_index, i); + ret = cli_xml_output_vol_status_itable(writer, dict, key); + if (ret) + goto out; + + /* </connection> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + /* </inodeStatus> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} + +int +cli_xml_output_vol_status_fdtable(xmlTextWriterPtr writer, dict_t *dict, + char *prefix) +{ + int ret = -1; + int refcount = 0; + uint32_t maxfds = 0; + int firstfree = 0; + int openfds = 0; + int fd_pid = 0; + int fd_refcount = 0; + int fd_flags = 0; + char key[1024] = { + 0, + }; + int i = 0; + + /* <fdTable> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"fdTable"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.refcount", prefix); + ret = dict_get_int32(dict, key, &refcount); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"refCount", "%d", + refcount); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.maxfds", prefix); + ret = dict_get_uint32(dict, key, &maxfds); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"maxFds", + "%" PRIu32, maxfds); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.firstfree", prefix); + ret = dict_get_int32(dict, key, &firstfree); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"firstFree", "%d", + firstfree); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.openfds", prefix); + ret = dict_get_int32(dict, key, &openfds); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"openFds", "%d", + openfds); + XML_RET_CHECK_AND_GOTO(ret, out); + + for (i = 0; i < maxfds; i++) { + snprintf(key, sizeof(key), "%s.fdentry%d.pid", prefix, i); + ret = dict_get_int32(dict, key, &fd_pid); + if (ret) + continue; + + snprintf(key, sizeof(key), "%s.fdentry%d.refcount", prefix, i); + ret = dict_get_int32(dict, key, &fd_refcount); + if (ret) + continue; + + snprintf(key, sizeof(key), "%s.fdentry%d.flags", prefix, i); + ret = dict_get_int32(dict, key, &fd_flags); + if (ret) + continue; + + /* <fd> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"fd"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"entry", "%d", + i + 1); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"pid", "%d", + fd_pid); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"refCount", + "%d", fd_refcount); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"flags", "%d", + fd_flags); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </fd> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + /* </fdTable> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} + +int +cli_xml_output_vol_status_fd(xmlTextWriterPtr writer, dict_t *dict, + int brick_index) +{ + int ret = -1; + int conn_count = 0; + char key[1024] = { + 0, + }; + int i = 0; + + /* <fdStatus> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"fdStatus"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "brick%d.conncount", brick_index); + ret = dict_get_int32(dict, key, &conn_count); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"connections", + "%d", conn_count); + XML_RET_CHECK_AND_GOTO(ret, out); + + for (i = 0; i < conn_count; i++) { + /* <connection> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"connection"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "brick%d.conn%d.fdtable", brick_index, i); + ret = cli_xml_output_vol_status_fdtable(writer, dict, key); + if (ret) + goto out; + + /* </connection> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + /* </fdStatus> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} + +int +cli_xml_output_vol_status_callframe(xmlTextWriterPtr writer, dict_t *dict, + char *prefix) +{ + int ret = -1; + int ref_count = 0; + char *translator = NULL; + int complete = 0; + char *parent = NULL; + char *wind_from = NULL; + char *wind_to = NULL; + char *unwind_from = NULL; + char *unwind_to = NULL; + char key[1024] = { + 0, + }; + + /* <callFrame> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"callFrame"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.refcount", prefix); + ret = dict_get_int32(dict, key, &ref_count); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"refCount", "%d", + ref_count); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.translator", prefix); + ret = dict_get_str(dict, key, &translator); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"translator", "%s", + translator); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.complete", prefix); + ret = dict_get_int32(dict, key, &complete); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"complete", "%d", + complete); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.parent", prefix); + ret = dict_get_str(dict, key, &parent); + if (!ret) { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"parent", "%s", + parent); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + snprintf(key, sizeof(key), "%s.windfrom", prefix); + ret = dict_get_str(dict, key, &wind_from); + if (!ret) { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"windFrom", + "%s", wind_from); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + snprintf(key, sizeof(key), "%s.windto", prefix); + ret = dict_get_str(dict, key, &wind_to); + if (!ret) { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"windTo", "%s", + wind_to); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + snprintf(key, sizeof(key), "%s.unwindfrom", prefix); + ret = dict_get_str(dict, key, &unwind_from); + if (!ret) { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"unwindFrom", + "%s", unwind_from); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + snprintf(key, sizeof(key), "%s.unwindto", prefix); + ret = dict_get_str(dict, key, &unwind_to); + if (!ret) { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"unwindTo", + "%s", unwind_to); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + /* </callFrame> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} + +int +cli_xml_output_vol_status_callstack(xmlTextWriterPtr writer, dict_t *dict, + char *prefix) +{ + int ret = -1; + int uid = 0; + int gid = 0; + int pid = 0; + uint64_t unique = 0; + int frame_count = 0; + char key[1024] = { + 0, + }; + int i = 0; + + /* <callStack> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"callStack"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.uid", prefix); + ret = dict_get_int32(dict, key, &uid); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"uid", "%d", uid); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.gid", prefix); + ret = dict_get_int32(dict, key, &gid); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"gid", "%d", gid); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.pid", prefix); + ret = dict_get_int32(dict, key, &pid); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"pid", "%d", pid); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.unique", prefix); + ret = dict_get_uint64(dict, key, &unique); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"unique", + "%" PRIu64, unique); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.count", prefix); + ret = dict_get_int32(dict, key, &frame_count); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"frameCount", "%d", + frame_count); + XML_RET_CHECK_AND_GOTO(ret, out); + + for (i = 0; i < frame_count; i++) { + snprintf(key, sizeof(key), "%s.frame%d", prefix, i); + ret = cli_xml_output_vol_status_callframe(writer, dict, key); + if (ret) + goto out; + } + + /* </callStack> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} + +int +cli_xml_output_vol_status_callpool(xmlTextWriterPtr writer, dict_t *dict, + int brick_index) +{ + int ret = -1; + int call_count = 0; + char key[1024] = { + 0, + }; + int i = 0; + + /* <callpoolStatus> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"callpoolStatus"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "brick%d.callpool.count", brick_index); + ret = dict_get_int32(dict, key, &call_count); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"count", "%d", + call_count); + XML_RET_CHECK_AND_GOTO(ret, out); + + for (i = 0; i < call_count; i++) { + snprintf(key, sizeof(key), "brick%d.callpool.stack%d", brick_index, i); + ret = cli_xml_output_vol_status_callstack(writer, dict, key); + if (ret) + goto out; + } + + /* </callpoolStatus> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} +#endif + +int +cli_xml_output_vol_status_begin(cli_local_t *local, int op_ret, int op_errno, + char *op_errstr) +{ +#if (HAVE_LIB_XML) + int ret = -1; + + ret = cli_begin_xml_output(&(local->writer), &(local->doc)); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = cli_xml_output_common(local->writer, op_ret, op_errno, op_errstr); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* <volStatus> */ + ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"volStatus"); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* <volumes> */ + ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"volumes"); + XML_RET_CHECK_AND_GOTO(ret, out); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +#else + return 0; +#endif +} + +int +cli_xml_output_vol_status_end(cli_local_t *local) +{ +#if (HAVE_LIB_XML) + int ret = -1; + + /* </volumes> */ + ret = xmlTextWriterEndElement(local->writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </volStatus> */ + ret = xmlTextWriterEndElement(local->writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = cli_end_xml_output(local->writer, local->doc); +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +#else + return 0; +#endif +} + +#if (HAVE_LIB_XML) +int +cli_xml_output_remove_brick_task_params(xmlTextWriterPtr writer, dict_t *dict, + char *prefix) +{ + int ret = -1; + char key[1024] = { + 0, + }; + int count = 0; + int i = 0; + char *brick = NULL; + + /* <params> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"params"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.count", prefix); + ret = dict_get_int32(dict, key, &count); + if (ret) + goto out; + + for (i = 1; i <= count; i++) { + snprintf(key, sizeof(key), "%s.brick%d", prefix, i); + ret = dict_get_str(dict, key, &brick); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"brick", "%s", + brick); + XML_RET_CHECK_AND_GOTO(ret, out); + brick = NULL; + } + + /* </param> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} + +int +cli_xml_output_vol_status_tasks(cli_local_t *local, dict_t *dict) +{ + int ret = -1; + char *task_type = NULL; + char *task_id_str = NULL; + int status = 0; + int tasks = 0; + char key[1024] = { + 0, + }; + int i = 0; + + /* <tasks> */ + ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"tasks"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_int32(dict, "tasks", &tasks); + if (ret) + goto out; + + for (i = 0; i < tasks; i++) { + /* <task> */ + ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"task"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "task%d.type", i); + ret = dict_get_str(dict, key, &task_type); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"type", + "%s", task_type); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "task%d.id", i); + ret = dict_get_str(dict, key, &task_id_str); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"id", + "%s", task_id_str); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "task%d.status", i); + ret = dict_get_int32(dict, key, &status); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement( + local->writer, (xmlChar *)"status", "%d", status); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(local->writer, + (xmlChar *)"statusStr", "%s", + cli_vol_task_status_str[status]); + + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "task%d", i); + if (!strcmp(task_type, "Remove brick")) { + ret = cli_xml_output_remove_brick_task_params(local->writer, dict, + key); + if (ret) + goto out; + } + + /* </task> */ + ret = xmlTextWriterEndElement(local->writer); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + /* </tasks> */ + ret = xmlTextWriterEndElement(local->writer); + XML_RET_CHECK_AND_GOTO(ret, out); +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} + +#endif + +int +cli_xml_output_vol_status_tasks_detail(cli_local_t *local, dict_t *dict) +{ +#if (HAVE_LIB_XML) + int ret = -1; + char *volname = NULL; + + /*<volume>*/ + ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"volume"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_str(dict, "volname", &volname); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"volName", + "%s", volname); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = cli_xml_output_vol_status_tasks(local, dict); + if (ret) + goto out; + + /* </volume> */ + ret = xmlTextWriterEndElement(local->writer); + XML_RET_CHECK_AND_GOTO(ret, out); + +out: + return ret; +#else + return 0; +#endif +} + +int +cli_xml_output_vol_status(cli_local_t *local, dict_t *dict) +{ +#if (HAVE_LIB_XML) + int ret = -1; + char *volname = NULL; + int brick_count = 0; + int brick_index_max = -1; + int other_count = 0; + int index_max = 0; + uint32_t cmd = GF_CLI_STATUS_NONE; + int online = 0; + gf_boolean_t node_present = _gf_true; + int i; + int type = -1; + + /* <volume> */ + ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"volume"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_str(dict, "volname", &volname); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"volName", + "%s", volname); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_int32(dict, "count", &brick_count); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"nodeCount", + "%d", brick_count); + + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_uint32(dict, "cmd", &cmd); + if (ret) + goto out; + + ret = dict_get_int32(dict, "brick-index-max", &brick_index_max); + if (ret) + goto out; + ret = dict_get_int32(dict, "other-count", &other_count); + if (ret) + goto out; + + index_max = brick_index_max + other_count; + + ret = dict_get_int32(dict, "type", &type); + if (ret) + goto out; + + for (i = 0; i <= index_max; i++) { + ret = cli_xml_output_vol_status_common(local->writer, dict, i, &online, + &node_present); + if (ret) { + if (node_present) + goto out; + else + continue; + } + + switch (cmd & GF_CLI_STATUS_MASK) { + case GF_CLI_STATUS_DETAIL: + ret = cli_xml_output_vol_status_detail(local->writer, dict, i); + if (ret) + goto out; + break; + + case GF_CLI_STATUS_MEM: + if (online) { + ret = cli_xml_output_vol_status_mem(local->writer, dict, i); + if (ret) + goto out; + } + break; + + case GF_CLI_STATUS_CLIENTS: + if (online) { + ret = cli_xml_output_vol_status_clients(local->writer, dict, + i); + if (ret) + goto out; + } + break; + + case GF_CLI_STATUS_INODE: + if (online) { + ret = cli_xml_output_vol_status_inode(local->writer, dict, + i); + if (ret) + goto out; + } + break; + + case GF_CLI_STATUS_FD: + if (online) { + ret = cli_xml_output_vol_status_fd(local->writer, dict, i); + if (ret) + goto out; + } + break; + + case GF_CLI_STATUS_CALLPOOL: + if (online) { + ret = cli_xml_output_vol_status_callpool(local->writer, + dict, i); + if (ret) + goto out; + } + break; + default: + break; + } + + /* </node> was opened in cli_xml_output_vol_status_common()*/ + ret = xmlTextWriterEndElement(local->writer); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + /* Tasks are only present when a normal volume status call is done on a + * single volume or on all volumes + */ + if (((cmd & GF_CLI_STATUS_MASK) == GF_CLI_STATUS_NONE) && + (cmd & (GF_CLI_STATUS_VOL | GF_CLI_STATUS_ALL))) { + ret = cli_xml_output_vol_status_tasks(local, dict); + if (ret) + goto out; + } + + /* </volume> */ + ret = xmlTextWriterEndElement(local->writer); + XML_RET_CHECK_AND_GOTO(ret, out); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +#else + return 0; +#endif +} + +#if (HAVE_LIB_XML) +int +cli_xml_output_vol_top_rw_perf(xmlTextWriterPtr writer, dict_t *dict, + int brick_index, int member_index) +{ + int ret = -1; + char *filename = NULL; + uint64_t throughput = 0; + struct timeval tv = { + 0, + }; + char timestr[GF_TIMESTR_SIZE] = { + 0, + }; + char key[1024] = { + 0, + }; + + /* <file> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"file"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%d-filename-%d", brick_index, member_index); + ret = dict_get_str(dict, key, &filename); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"filename", "%s", + filename); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%d-value-%d", brick_index, member_index); + ret = dict_get_uint64(dict, key, &throughput); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"count", + "%" PRIu64, throughput); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%d-time-sec-%d", brick_index, member_index); + ret = dict_get_int32(dict, key, (int32_t *)&tv.tv_sec); + if (ret) + goto out; + + snprintf(key, sizeof(key), "%d-time-usec-%d", brick_index, member_index); + ret = dict_get_int32(dict, key, (int32_t *)&tv.tv_usec); + if (ret) + goto out; + + gf_time_fmt_tv(timestr, sizeof timestr, &tv, gf_timefmt_FT); + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"time", "%s", + timestr); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </file> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} + +int +cli_xml_output_vol_top_other(xmlTextWriterPtr writer, dict_t *dict, + int brick_index, int member_index) +{ + int ret = -1; + char *filename = NULL; + uint64_t count = 0; + char key[1024] = { + 0, + }; + + /* <file> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"file"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%d-filename-%d", brick_index, member_index); + ret = dict_get_str(dict, key, &filename); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"filename", "%s", + filename); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%d-value-%d", brick_index, member_index); + ret = dict_get_uint64(dict, key, &count); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"count", + "%" PRIu64, count); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </file> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} +#endif + +int +cli_xml_output_vol_top(dict_t *dict, int op_ret, int op_errno, char *op_errstr) +{ +#if (HAVE_LIB_XML) + int ret = -1; + xmlTextWriterPtr writer = NULL; + xmlDocPtr doc = NULL; + int brick_count = 0; + int top_op = GF_CLI_TOP_NONE; + char *brick_name = NULL; + int members = 0; + uint64_t current_open = 0; + uint64_t max_open = 0; + char *max_open_time = NULL; + double throughput = 0.0; + double time_taken = 0.0; + char key[1024] = { + 0, + }; + int i = 0; + int j = 0; + + ret = cli_begin_xml_output(&writer, &doc); + if (ret) + goto out; + + ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr); + if (ret) + goto out; + + /* <volTop> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"volTop"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_int32(dict, "count", &brick_count); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"brickCount", "%d", + brick_count); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_int32(dict, "1-top-op", &top_op); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"topOp", "%d", + top_op); + XML_RET_CHECK_AND_GOTO(ret, out); + + while (i < brick_count) { + i++; + + /* <brick> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"brick"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%d-brick", i); + ret = dict_get_str(dict, key, &brick_name); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s", + brick_name); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%d-members", i); + ret = dict_get_int32(dict, key, &members); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"members", + "%d", members); + XML_RET_CHECK_AND_GOTO(ret, out); + + switch (top_op) { + case GF_CLI_TOP_OPEN: + snprintf(key, sizeof(key), "%d-current-open", i); + ret = dict_get_uint64(dict, key, ¤t_open); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement( + writer, (xmlChar *)"currentOpen", "%" PRIu64, current_open); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%d-max-open", i); + ret = dict_get_uint64(dict, key, &max_open); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement( + writer, (xmlChar *)"maxOpen", "%" PRIu64, max_open); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%d-max-openfd-time", i); + ret = dict_get_str(dict, key, &max_open_time); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement( + writer, (xmlChar *)"maxOpenTime", "%s", max_open_time); + XML_RET_CHECK_AND_GOTO(ret, out); + + case GF_CLI_TOP_READ: + case GF_CLI_TOP_WRITE: + case GF_CLI_TOP_OPENDIR: + case GF_CLI_TOP_READDIR: + + break; + + case GF_CLI_TOP_READ_PERF: + case GF_CLI_TOP_WRITE_PERF: + snprintf(key, sizeof(key), "%d-throughput", i); + ret = dict_get_double(dict, key, &throughput); + if (!ret) { + snprintf(key, sizeof(key), "%d-time", i); + ret = dict_get_double(dict, key, &time_taken); + } + + if (!ret) { + ret = xmlTextWriterWriteFormatElement( + writer, (xmlChar *)"throughput", "%f", throughput); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement( + writer, (xmlChar *)"timeTaken", "%f", time_taken); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + break; + + default: + ret = -1; + goto out; + } + + for (j = 1; j <= members; j++) { + if (top_op == GF_CLI_TOP_READ_PERF || + top_op == GF_CLI_TOP_WRITE_PERF) { + ret = cli_xml_output_vol_top_rw_perf(writer, dict, i, j); + } else { + ret = cli_xml_output_vol_top_other(writer, dict, i, j); + } + if (ret) + goto out; + } + + /* </brick> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + /* </volTop> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + ret = cli_end_xml_output(writer, doc); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +#else + return 0; +#endif +} + +#if (HAVE_LIB_XML) +int +cli_xml_output_vol_profile_stats(xmlTextWriterPtr writer, dict_t *dict, + int brick_index, int interval) +{ + int ret = -1; + uint64_t read_count = 0; + uint64_t write_count = 0; + uint64_t hits = 0; + double avg_latency = 0.0; + double max_latency = 0.0; + double min_latency = 0.0; + uint64_t duration = 0; + uint64_t total_read = 0; + uint64_t total_write = 0; + char key[1024] = {0}; + int i = 0; + + /* <cumulativeStats> || <intervalStats> */ + if (interval == -1) + ret = xmlTextWriterStartElement(writer, (xmlChar *)"cumulativeStats"); + else + ret = xmlTextWriterStartElement(writer, (xmlChar *)"intervalStats"); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* <blockStats> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"blockStats"); + XML_RET_CHECK_AND_GOTO(ret, out); + + for (i = 0; i < 32; i++) { + /* <block> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"block"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"size", + "%" PRIu32, (1U << i)); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%d-%d-read-%" PRIu32, brick_index, interval, + (1U << i)); + ret = dict_get_uint64(dict, key, &read_count); + if (ret) + read_count = 0; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"reads", + "%" PRIu64, read_count); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%d-%d-write-%" PRIu32, brick_index, + interval, (1U << i)); + ret = dict_get_uint64(dict, key, &write_count); + if (ret) + write_count = 0; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"writes", + "%" PRIu64, write_count); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </block> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + /* </blockStats> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* <fopStats> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"fopStats"); + XML_RET_CHECK_AND_GOTO(ret, out); + + for (i = 0; i < GF_FOP_MAXVALUE; i++) { + snprintf(key, sizeof(key), "%d-%d-%d-hits", brick_index, interval, i); + ret = dict_get_uint64(dict, key, &hits); + if (ret) + goto cont; + + snprintf(key, sizeof(key), "%d-%d-%d-avglatency", brick_index, interval, + i); + ret = dict_get_double(dict, key, &avg_latency); + if (ret) + goto cont; + + snprintf(key, sizeof(key), "%d-%d-%d-minlatency", brick_index, interval, + i); + ret = dict_get_double(dict, key, &min_latency); + if (ret) + goto cont; + + snprintf(key, sizeof(key), "%d-%d-%d-maxlatency", brick_index, interval, + i); + ret = dict_get_double(dict, key, &max_latency); + if (ret) + goto cont; + + /* <fop> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"fop"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s", + gf_fop_list[i]); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"hits", + "%" PRIu64, hits); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"avgLatency", + "%f", avg_latency); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"minLatency", + "%f", min_latency); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"maxLatency", + "%f", max_latency); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </fop> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + cont: + hits = 0; + avg_latency = 0.0; + min_latency = 0.0; + max_latency = 0.0; + } + + for (i = 0; i < GF_UPCALL_FLAGS_MAXVALUE; i++) { + hits = 0; + avg_latency = 0.0; + min_latency = 0.0; + max_latency = 0.0; + + snprintf(key, sizeof(key), "%d-%d-%d-upcall-hits", brick_index, + interval, i); + ret = dict_get_uint64(dict, key, &hits); + if (ret) + continue; + + /* <fop> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"fop"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s", + gf_fop_list[i]); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"hits", + "%" PRIu64, hits); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"avgLatency", + "%f", avg_latency); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"minLatency", + "%f", min_latency); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"maxLatency", + "%f", max_latency); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </fop> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + /* </fopStats> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%d-%d-duration", brick_index, interval); + ret = dict_get_uint64(dict, key, &duration); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"duration", + "%" PRIu64, duration); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%d-%d-total-read", brick_index, interval); + ret = dict_get_uint64(dict, key, &total_read); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"totalRead", + "%" PRIu64, total_read); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%d-%d-total-write", brick_index, interval); + ret = dict_get_uint64(dict, key, &total_write); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"totalWrite", + "%" PRIu64, total_write); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </cumulativeStats> || </intervalStats> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} +#endif + +int +cli_xml_output_vol_profile(dict_t *dict, int op_ret, int op_errno, + char *op_errstr) +{ +#if (HAVE_LIB_XML) + int ret = -1; + xmlTextWriterPtr writer = NULL; + xmlDocPtr doc = NULL; + char *volname = NULL; + int op = GF_CLI_STATS_NONE; + int info_op = GF_CLI_INFO_NONE; + int brick_count = 0; + char *brick_name = NULL; + int interval = 0; + char key[1024] = { + 0, + }; + int i = 0; + int stats_cleared = 0; + + ret = cli_begin_xml_output(&writer, &doc); + if (ret) + goto out; + + ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr); + if (ret) + goto out; + + /* <volProfile> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"volProfile"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_str(dict, "volname", &volname); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"volname", "%s", + volname); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_int32(dict, "op", &op); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"profileOp", "%d", + op); + XML_RET_CHECK_AND_GOTO(ret, out); + + if (GF_CLI_STATS_INFO != op) + goto cont; + + ret = dict_get_int32(dict, "count", &brick_count); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"brickCount", "%d", + brick_count); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_int32(dict, "info-op", &info_op); + if (ret) + goto out; + + while (i < brick_count) { + i++; + + /* <brick> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"brick"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%d-brick", i); + ret = dict_get_str(dict, key, &brick_name); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"brickName", + "%s", brick_name); + XML_RET_CHECK_AND_GOTO(ret, out); + + if (GF_CLI_INFO_CLEAR == info_op) { + snprintf(key, sizeof(key), "%d-stats-cleared", i); + ret = dict_get_int32(dict, key, &stats_cleared); + if (ret) + goto out; + + ret = xmlTextWriterWriteFormatElement( + writer, (xmlChar *)"clearStats", "%s", + stats_cleared ? "Cleared stats." : "Failed to clear stats."); + XML_RET_CHECK_AND_GOTO(ret, out); + } else { + snprintf(key, sizeof(key), "%d-cumulative", i); + ret = dict_get_int32(dict, key, &interval); + if (ret == 0) { + ret = cli_xml_output_vol_profile_stats(writer, dict, i, + interval); + if (ret) + goto out; + } + + snprintf(key, sizeof(key), "%d-interval", i); + ret = dict_get_int32(dict, key, &interval); + if (ret == 0) { + ret = cli_xml_output_vol_profile_stats(writer, dict, i, + interval); + if (ret) + goto out; + } + } + + /* </brick> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + } + +cont: + /* </volProfile> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = cli_end_xml_output(writer, doc); +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +#else + return 0; +#endif +} + +int +cli_xml_output_vol_list(dict_t *dict, int op_ret, int op_errno, char *op_errstr) +{ +#if (HAVE_LIB_XML) + int ret = -1; + xmlTextWriterPtr writer = NULL; + xmlDocPtr doc = NULL; + int count = 0; + char *volname = NULL; + char key[1024] = { + 0, + }; + int i = 0; + + ret = cli_begin_xml_output(&writer, &doc); + if (ret) + goto out; + + ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr); + if (ret) + goto out; + + /* <volList> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"volList"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_int32(dict, "count", &count); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"count", "%d", + count); + XML_RET_CHECK_AND_GOTO(ret, out); + + for (i = 0; i < count; i++) { + snprintf(key, sizeof(key), "volume%d", i); + ret = dict_get_str(dict, key, &volname); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"volume", "%s", + volname); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + /* </volList> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = cli_end_xml_output(writer, doc); +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +#else + return 0; +#endif +} + +#if (HAVE_LIB_XML) +int +cli_xml_output_vol_info_option(xmlTextWriterPtr writer, char *substr, + char *optstr, char *valstr) +{ + int ret = -1; + char *ptr1 = NULL; + char *ptr2 = NULL; + + ptr1 = substr; + ptr2 = optstr; + + while (ptr1) { + if (*ptr1 != *ptr2) + break; + ptr1++; + ptr2++; + if (!*ptr1) + break; + if (!*ptr2) + break; + } + if (*ptr2 == '\0') + goto out; + + /* <option> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"option"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s", + ptr2); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"value", "%s", + valstr); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </option> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} + +struct tmp_xml_option_logger { + char *key; + xmlTextWriterPtr writer; +}; + +static int +_output_vol_info_option(dict_t *d, char *k, data_t *v, void *data) +{ + int ret = 0; + char *ptr = NULL; + struct tmp_xml_option_logger *tmp = NULL; + + tmp = data; + + ptr = strstr(k, "option."); + if (!ptr) + goto out; + + if (!v) { + ret = -1; + goto out; + } + ret = cli_xml_output_vol_info_option(tmp->writer, tmp->key, k, v->data); + +out: + return ret; +} + +int +cli_xml_output_vol_info_options(xmlTextWriterPtr writer, dict_t *dict, + char *prefix) +{ + int ret = -1; + int opt_count = 0; + char key[1024] = { + 0, + }; + struct tmp_xml_option_logger tmp = { + 0, + }; + + snprintf(key, sizeof(key), "%s.opt_count", prefix); + ret = dict_get_int32(dict, key, &opt_count); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"optCount", "%d", + opt_count); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* <options> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"options"); + XML_RET_CHECK_AND_GOTO(ret, out); + snprintf(key, sizeof(key), "%s.option.", prefix); + + tmp.key = key; + tmp.writer = writer; + ret = dict_foreach(dict, _output_vol_info_option, &tmp); + if (ret) + goto out; + + /* </options> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} +#endif + +int +cli_xml_output_vol_info(cli_local_t *local, dict_t *dict) +{ +#if (HAVE_LIB_XML) + int ret = 0; + int count = 0; + char *volname = NULL; + char *volume_id = NULL; + char *uuid = NULL; + int type = 0; + int status = 0; + int brick_count = 0; + int dist_count = 0; + int stripe_count = 0; + int replica_count = 0; + int arbiter_count = 0; + int snap_count = 0; + int isArbiter = 0; + int disperse_count = 0; + int redundancy_count = 0; + int transport = 0; + char *brick = NULL; + char key[1024] = { + 0, + }; + int i = 0; + int j = 1; + char *caps __attribute__((unused)) = NULL; + int k __attribute__((unused)) = 0; + + ret = dict_get_int32(dict, "count", &count); + if (ret) + goto out; + + for (i = 0; i < count; i++) { + /* <volume> */ + ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"volume"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "volume%d.name", i); + ret = dict_get_str(dict, key, &volname); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"name", + "%s", volname); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "volume%d.volume_id", i); + ret = dict_get_str(dict, key, &volume_id); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"id", + "%s", volume_id); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "volume%d.status", i); + ret = dict_get_int32(dict, key, &status); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement( + local->writer, (xmlChar *)"status", "%d", status); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(local->writer, + (xmlChar *)"statusStr", "%s", + cli_vol_status_str[status]); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "volume%d.snap_count", i); + ret = dict_get_int32(dict, key, &snap_count); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement( + local->writer, (xmlChar *)"snapshotCount", "%d", snap_count); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "volume%d.brick_count", i); + ret = dict_get_int32(dict, key, &brick_count); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement( + local->writer, (xmlChar *)"brickCount", "%d", brick_count); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "volume%d.dist_count", i); + ret = dict_get_int32(dict, key, &dist_count); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(local->writer, + (xmlChar *)"distCount", "%d", + (brick_count / dist_count)); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "volume%d.stripe_count", i); + ret = dict_get_int32(dict, key, &stripe_count); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement( + local->writer, (xmlChar *)"stripeCount", "%d", stripe_count); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "volume%d.replica_count", i); + ret = dict_get_int32(dict, key, &replica_count); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement( + local->writer, (xmlChar *)"replicaCount", "%d", replica_count); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "volume%d.arbiter_count", i); + ret = dict_get_int32(dict, key, &arbiter_count); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement( + local->writer, (xmlChar *)"arbiterCount", "%d", arbiter_count); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "volume%d.disperse_count", i); + ret = dict_get_int32(dict, key, &disperse_count); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement( + local->writer, (xmlChar *)"disperseCount", "%d", disperse_count); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "volume%d.redundancy_count", i); + ret = dict_get_int32(dict, key, &redundancy_count); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(local->writer, + (xmlChar *)"redundancyCount", + "%d", redundancy_count); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "volume%d.type", i); + ret = dict_get_int32(dict, key, &type); + if (ret) + goto out; + /* For Distributed-(stripe,replicate,stipe-replicate,disperse) + types + */ + type = get_vol_type(type, dist_count, brick_count); + + ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"type", + "%d", type); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement( + local->writer, (xmlChar *)"typeStr", "%s", vol_type_str[type]); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "volume%d.transport", i); + ret = dict_get_int32(dict, key, &transport); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement( + local->writer, (xmlChar *)"transport", "%d", transport); + XML_RET_CHECK_AND_GOTO(ret, out); + + j = 1; + + /* <bricks> */ + ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"bricks"); + XML_RET_CHECK_AND_GOTO(ret, out); + + while (j <= brick_count) { + ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"brick"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "volume%d.brick%d.uuid", i, j); + ret = dict_get_str(dict, key, &uuid); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatAttribute( + local->writer, (xmlChar *)"uuid", "%s", uuid); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "volume%d.brick%d", i, j); + ret = dict_get_str(dict, key, &brick); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatString(local->writer, "%s", brick); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement( + local->writer, (xmlChar *)"name", "%s", brick); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement( + local->writer, (xmlChar *)"hostUuid", "%s", uuid); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "volume%d.brick%d.isArbiter", i, j); + if (dict_get(dict, key)) + isArbiter = 1; + else + isArbiter = 0; + ret = xmlTextWriterWriteFormatElement( + local->writer, (xmlChar *)"isArbiter", "%d", isArbiter); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </brick> */ + ret = xmlTextWriterEndElement(local->writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + j++; + } + /* </bricks> */ + ret = xmlTextWriterEndElement(local->writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "volume%d", i); + ret = cli_xml_output_vol_info_options(local->writer, dict, key); + if (ret) + goto out; + + /* </volume> */ + ret = xmlTextWriterEndElement(local->writer); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + if (volname) { + GF_FREE(local->get_vol.volname); + local->get_vol.volname = gf_strdup(volname); + local->vol_count += count; + } +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +#else + return 0; +#endif +} + +int +cli_xml_output_vol_info_begin(cli_local_t *local, int op_ret, int op_errno, + char *op_errstr) +{ +#if (HAVE_LIB_XML) + int ret = -1; + + GF_ASSERT(local); + + ret = cli_begin_xml_output(&(local->writer), &(local->doc)); + if (ret) + goto out; + + ret = cli_xml_output_common(local->writer, op_ret, op_errno, op_errstr); + if (ret) + goto out; + + /* <volInfo> */ + ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"volInfo"); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* <volumes> */ + ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"volumes"); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* Init vol count */ + local->vol_count = 0; + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +#else + return 0; +#endif +} + +int +cli_xml_output_vol_info_end(cli_local_t *local) +{ +#if (HAVE_LIB_XML) + int ret = -1; + + GF_ASSERT(local); + + ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"count", + "%d", local->vol_count); + XML_RET_CHECK_AND_GOTO(ret, out); + /* </volumes> */ + ret = xmlTextWriterEndElement(local->writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </volInfo> */ + ret = xmlTextWriterEndElement(local->writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = cli_end_xml_output(local->writer, local->doc); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +#else + return 0; +#endif +} + +int +cli_xml_output_vol_quota_limit_list_end(cli_local_t *local) +{ +#if (HAVE_LIB_XML) + int ret = -1; + + ret = xmlTextWriterEndElement(local->writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = cli_end_xml_output(local->writer, local->doc); + +out: + return ret; +#else + return 0; +#endif +} + +int +cli_xml_output_vol_quota_limit_list_begin(cli_local_t *local, int op_ret, + int op_errno, char *op_errstr) +{ +#if (HAVE_LIB_XML) + int ret = -1; + + ret = cli_begin_xml_output(&(local->writer), &(local->doc)); + if (ret) + goto out; + + ret = cli_xml_output_common(local->writer, op_ret, op_errno, op_errstr); + if (ret) + goto out; + + /* <volQuota> */ + ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"volQuota"); + XML_RET_CHECK_AND_GOTO(ret, out); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +#else + return 0; +#endif +} + +#if (HAVE_LIB_XML) +static int +cli_xml_output_peer_hostnames(xmlTextWriterPtr writer, dict_t *dict, + const char *prefix, int count) +{ + int ret = -1; + int i = 0; + char *hostname = NULL; + char key[1024] = { + 0, + }; + + /* <hostnames> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"hostnames"); + XML_RET_CHECK_AND_GOTO(ret, out); + + for (i = 0; i < count; i++) { + ret = snprintf(key, sizeof(key), "%s.hostname%d", prefix, i); + if (ret < 0) + goto out; + ret = dict_get_str(dict, key, &hostname); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"hostname", + "%s", hostname); + XML_RET_CHECK_AND_GOTO(ret, out); + hostname = NULL; + } + + /* </hostnames> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} +#endif + +int +cli_xml_output_peer_status(dict_t *dict, int op_ret, int op_errno, + char *op_errstr) +{ +#if (HAVE_LIB_XML) + int ret = -1; + xmlTextWriterPtr writer = NULL; + xmlDocPtr doc = NULL; + int count = 0; + char *uuid = NULL; + char *hostname = NULL; + int connected = 0; + int state_id = 0; + char *state_str = NULL; + int hostname_count = 0; + int i = 1; + char key[1024] = { + 0, + }; + + ret = cli_begin_xml_output(&writer, &doc); + if (ret) + goto out; + + ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr); + if (ret) + goto out; + + /* <peerStatus> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"peerStatus"); + XML_RET_CHECK_AND_GOTO(ret, out); + + if (!dict) + goto cont; + + ret = dict_get_int32(dict, "count", &count); + if (ret) + goto out; + + while (i <= count) { + /* <peer> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"peer"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "friend%d.uuid", i); + ret = dict_get_str(dict, key, &uuid); + if (ret) + goto out; + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"uuid", "%s", + uuid); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "friend%d.hostname", i); + ret = dict_get_str(dict, key, &hostname); + if (ret) + goto out; + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"hostname", + "%s", hostname); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "friend%d.hostname_count", i); + ret = dict_get_int32(dict, key, &hostname_count); + if ((ret == 0) && (hostname_count > 0)) { + snprintf(key, sizeof(key), "friend%d", i); + ret = cli_xml_output_peer_hostnames(writer, dict, key, + hostname_count); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + snprintf(key, sizeof(key), "friend%d.connected", i); + ret = dict_get_int32(dict, key, &connected); + if (ret) + goto out; + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"connected", + "%d", connected); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "friend%d.stateId", i); + ret = dict_get_int32(dict, key, &state_id); + if (!ret) { + /* ignore */ + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"state", + "%d", state_id); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + snprintf(key, sizeof(key), "friend%d.state", i); + ret = dict_get_str(dict, key, &state_str); + if (!ret) { + /* ignore */ + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"stateStr", + "%s", state_str); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + /* </peer> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + i++; + } + +cont: + /* </peerStatus> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = cli_end_xml_output(writer, doc); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +#else + return 0; +#endif +} + +#if (HAVE_LIB_XML) +/* Used for rebalance stop/status, remove-brick status */ +int +cli_xml_output_vol_rebalance_status(xmlTextWriterPtr writer, dict_t *dict, + enum gf_task_types task_type) +{ + int ret = -1; + int count = 0; + char *node_name = NULL; + char *node_uuid = NULL; + uint64_t files = 0; + uint64_t size = 0; + uint64_t lookups = 0; + int status_rcd = 0; + uint64_t failures = 0; + uint64_t skipped = 0; + uint64_t total_files = 0; + uint64_t total_size = 0; + uint64_t total_lookups = 0; + uint64_t total_failures = 0; + uint64_t total_skipped = 0; + char key[1024] = { + 0, + }; + int i = 0; + int overall_status = -1; + double elapsed = 0; + double overall_elapsed = 0; + + if (!dict) { + ret = 0; + goto out; + } + + ret = dict_get_int32(dict, "count", &count); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"nodeCount", "%d", + count); + XML_RET_CHECK_AND_GOTO(ret, out); + + while (i < count) { + i++; + /* Getting status early, to skip nodes that don't have the + * rebalance process started + */ + snprintf(key, sizeof(key), "status-%d", i); + ret = dict_get_int32(dict, key, &status_rcd); + + /* If glusterd is down it fails to get the status, try + getting status from other nodes */ + if (ret) + continue; + if (GF_DEFRAG_STATUS_NOT_STARTED == status_rcd) + continue; + + /* <node> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"node"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "node-name-%d", i); + ret = dict_get_str(dict, key, &node_name); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"nodeName", + "%s", node_name); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "node-uuid-%d", i); + ret = dict_get_str(dict, key, &node_uuid); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"id", "%s", + node_uuid); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "files-%d", i); + ret = dict_get_uint64(dict, key, &files); + if (ret) + goto out; + total_files += files; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"files", + "%" PRIu64, files); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "size-%d", i); + ret = dict_get_uint64(dict, key, &size); + if (ret) + goto out; + total_size += size; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"size", + "%" PRIu64, size); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "lookups-%d", i); + ret = dict_get_uint64(dict, key, &lookups); + if (ret) + goto out; + total_lookups += lookups; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"lookups", + "%" PRIu64, lookups); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "failures-%d", i); + ret = dict_get_uint64(dict, key, &failures); + if (ret) + goto out; + + snprintf(key, sizeof(key), "skipped-%d", i); + + ret = dict_get_uint64(dict, key, &skipped); + if (ret) + goto out; + + if (task_type == GF_TASK_TYPE_REMOVE_BRICK) { + failures += skipped; + skipped = 0; + } + + total_failures += failures; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"failures", + "%" PRIu64, failures); + XML_RET_CHECK_AND_GOTO(ret, out); + + total_skipped += skipped; + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"skipped", + "%" PRIu64, skipped); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"status", "%d", + status_rcd); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement( + writer, (xmlChar *)"statusStr", "%s", + cli_vol_task_status_str[status_rcd]); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "run-time-%d", i); + ret = dict_get_double(dict, key, &elapsed); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"runtime", + "%.2f", elapsed); + XML_RET_CHECK_AND_GOTO(ret, out); + + if (elapsed > overall_elapsed) { + overall_elapsed = elapsed; + } + + /* Rebalance has 5 states, + * NOT_STARTED, STARTED, STOPPED, COMPLETE, FAILED + * The precedence used to determine the aggregate status is as + * below, + * STARTED > FAILED > STOPPED > COMPLETE > NOT_STARTED + */ + /* TODO: Move this to a common place utilities that both CLI and + * glusterd need. + * Till then if the below algorithm is changed, change it in + * glusterd_volume_status_aggregate_tasks_status in + * glusterd-utils.c + */ + + if (-1 == overall_status) + overall_status = status_rcd; + int rank[] = {[GF_DEFRAG_STATUS_STARTED] = 1, + [GF_DEFRAG_STATUS_FAILED] = 2, + [GF_DEFRAG_STATUS_STOPPED] = 3, + [GF_DEFRAG_STATUS_COMPLETE] = 4, + [GF_DEFRAG_STATUS_NOT_STARTED] = 5}; + if (rank[status_rcd] <= rank[overall_status]) + overall_status = status_rcd; + + /* </node> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + /* Aggregate status */ + /* <aggregate> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"aggregate"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"files", + "%" PRIu64, total_files); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"size", "%" PRIu64, + total_size); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"lookups", + "%" PRIu64, total_lookups); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"failures", + "%" PRIu64, total_failures); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"skipped", + "%" PRIu64, total_skipped); + XML_RET_CHECK_AND_GOTO(ret, out); + + if (overall_status == -1) { + overall_status = status_rcd; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"status", "%d", + overall_status); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement( + writer, (xmlChar *)"statusStr", "%s", + cli_vol_task_status_str[overall_status]); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"runtime", "%.2f", + overall_elapsed); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </aggregate> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} +#endif + +int +cli_xml_output_vol_rebalance(gf_cli_defrag_type op, dict_t *dict, int op_ret, + int op_errno, char *op_errstr) +{ +#if (HAVE_LIB_XML) + int ret = -1; + xmlTextWriterPtr writer = NULL; + xmlDocPtr doc = NULL; + char *task_id_str = NULL; + + ret = cli_begin_xml_output(&writer, &doc); + if (ret) + goto out; + + ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr); + if (ret) + goto out; + + /* <volRebalance> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"volRebalance"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_str(dict, GF_REBALANCE_TID_KEY, &task_id_str); + if (ret == 0) { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"task-id", + "%s", task_id_str); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"op", "%d", op); + XML_RET_CHECK_AND_GOTO(ret, out); + + if ((GF_DEFRAG_CMD_STOP == op) || (GF_DEFRAG_CMD_STATUS == op)) { + ret = cli_xml_output_vol_rebalance_status(writer, dict, + GF_TASK_TYPE_REBALANCE); + if (ret) + goto out; + } + + /* </volRebalance> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = cli_end_xml_output(writer, doc); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +#else + return 0; +#endif +} + +int +cli_xml_output_vol_remove_brick(gf_boolean_t status_op, dict_t *dict, + int op_ret, int op_errno, char *op_errstr, + const char *op) +{ +#if (HAVE_LIB_XML) + int ret = -1; + xmlTextWriterPtr writer = NULL; + xmlDocPtr doc = NULL; + char *task_id_str = NULL; + + ret = cli_begin_xml_output(&writer, &doc); + if (ret) + goto out; + + ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr); + if (ret) + goto out; + + ret = xmlTextWriterStartElement(writer, (xmlChar *)op); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_str(dict, GF_REMOVE_BRICK_TID_KEY, &task_id_str); + if (ret == 0) { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"task-id", + "%s", task_id_str); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + if (status_op) { + ret = cli_xml_output_vol_rebalance_status(writer, dict, + GF_TASK_TYPE_REMOVE_BRICK); + if (ret) + goto out; + } + + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = cli_end_xml_output(writer, doc); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +#else + return 0; +#endif +} + +int +cli_xml_output_vol_replace_brick(dict_t *dict, int op_ret, int op_errno, + char *op_errstr) +{ +#if (HAVE_LIB_XML) + int ret = -1; + xmlTextWriterPtr writer = NULL; + xmlDocPtr doc = NULL; + + ret = cli_begin_xml_output(&writer, &doc); + if (ret) + goto out; + + ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr); + if (ret) + goto out; + + ret = cli_end_xml_output(writer, doc); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +#else + return 0; +#endif +} + +int +cli_xml_output_vol_create(dict_t *dict, int op_ret, int op_errno, + char *op_errstr) +{ +#if (HAVE_LIB_XML) + int ret = -1; + xmlTextWriterPtr writer = NULL; + xmlDocPtr doc = NULL; + char *volname = NULL; + char *volid = NULL; + + ret = cli_begin_xml_output(&writer, &doc); + if (ret) + goto out; + + ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr); + if (ret) + goto out; + + if (dict) { + /* <volCreate> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"volCreate"); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* <volume> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"volume"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_str(dict, "volname", &volname); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s", + volname); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_str(dict, "volume-id", &volid); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"id", "%s", + volid); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </volume> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </volCreate> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + ret = cli_end_xml_output(writer, doc); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +#else + return 0; +#endif +} + +int +cli_xml_output_generic_volume(char *op, dict_t *dict, int op_ret, int op_errno, + char *op_errstr) +{ +#if (HAVE_LIB_XML) + int ret = -1; + xmlTextWriterPtr writer = NULL; + xmlDocPtr doc = NULL; + char *volname = NULL; + char *volid = NULL; + + GF_ASSERT(op); + + ret = cli_begin_xml_output(&writer, &doc); + if (ret) + goto out; + + ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr); + if (ret) + goto out; + + if (dict) { + /* <"op"> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)op); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* <volume> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"volume"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_str(dict, "volname", &volname); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s", + volname); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_str(dict, "vol-id", &volid); + if (ret) + goto out; + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"id", "%s", + volid); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </volume> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </"op"> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + ret = cli_end_xml_output(writer, doc); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +#else + return 0; +#endif +} + +#if (HAVE_LIB_XML) +int +_output_gsync_config(FILE *fp, xmlTextWriterPtr writer, char *op_name) +{ + char resbuf[256 + PATH_MAX] = { + 0, + }; + char *ptr = NULL; + char *v = NULL; + int blen = sizeof(resbuf); + int ret = 0; + + for (;;) { + ptr = fgets(resbuf, blen, fp); + if (!ptr) + break; + + v = resbuf + strlen(resbuf) - 1; + while (isspace(*v)) { + /* strip trailing space */ + *v-- = '\0'; + } + if (v == resbuf) { + /* skip empty line */ + continue; + } + + if (op_name != NULL) { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)op_name, + "%s", resbuf); + XML_RET_CHECK_AND_GOTO(ret, out); + goto out; + } + + v = strchr(resbuf, ':'); + if (!v) { + ret = -1; + goto out; + } + *v++ = '\0'; + while (isspace(*v)) + v++; + v = gf_strdup(v); + if (!v) { + ret = -1; + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)resbuf, "%s", + v); + XML_RET_CHECK_AND_GOTO(ret, out); + } +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} +#endif + +#if (HAVE_LIB_XML) +int +get_gsync_config(runner_t *runner, + int (*op_conf)(FILE *fp, xmlTextWriterPtr writer, + char *op_name), + xmlTextWriterPtr writer, char *op_name) +{ + int ret = 0; + + runner_redir(runner, STDOUT_FILENO, RUN_PIPE); + if (runner_start(runner) != 0) { + gf_log("cli", GF_LOG_ERROR, "spawning child failed"); + return -1; + } + + ret = op_conf(runner_chio(runner, STDOUT_FILENO), writer, op_name); + + ret |= runner_end(runner); + if (ret) + gf_log("cli", GF_LOG_ERROR, "reading data from child failed"); + + return ret ? -1 : 0; +} +#endif + +#if (HAVE_LIB_XML) +int +cli_xml_generate_gsync_config(dict_t *dict, xmlTextWriterPtr writer) +{ + runner_t runner = { + 0, + }; + char *subop = NULL; + char *gwd = NULL; + char *slave = NULL; + char *confpath = NULL; + char *master = NULL; + char *op_name = NULL; + int ret = -1; + char conf_path[PATH_MAX] = ""; + + if (dict_get_str(dict, "subop", &subop) != 0) { + ret = -1; + goto out; + } + + if (strcmp(subop, "get") != 0 && strcmp(subop, "get-all") != 0) { + ret = xmlTextWriterWriteFormatElement( + writer, (xmlChar *)"message", "%s", + GEOREP " config updated successfully"); + XML_RET_CHECK_AND_GOTO(ret, out); + ret = 0; + goto out; + } + + if (dict_get_str(dict, "glusterd_workdir", &gwd) != 0 || + dict_get_str(dict, "slave", &slave) != 0) { + ret = -1; + goto out; + } + + if (dict_get_str(dict, "master", &master) != 0) + master = NULL; + + if (dict_get_str(dict, "op_name", &op_name) != 0) + op_name = NULL; + + ret = dict_get_str(dict, "conf_path", &confpath); + if (!confpath) { + ret = snprintf(conf_path, sizeof(conf_path) - 1, + "%s/" GEOREP "/gsyncd_template.conf", gwd); + conf_path[ret] = '\0'; + confpath = conf_path; + } + + runinit(&runner); + runner_add_args(&runner, GSYNCD_PREFIX "/gsyncd", "-c", NULL); + runner_argprintf(&runner, "%s", confpath); + runner_argprintf(&runner, "--iprefix=%s", DATADIR); + + if (master) + runner_argprintf(&runner, ":%s", master); + + runner_add_arg(&runner, slave); + runner_argprintf(&runner, "--config-%s", subop); + + if (op_name) + runner_add_arg(&runner, op_name); + + ret = get_gsync_config(&runner, _output_gsync_config, writer, op_name); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} +#endif + +#if (HAVE_LIB_XML) +int +cli_xml_output_vol_gsync_status(dict_t *dict, xmlTextWriterPtr writer) +{ + int ret = -1; + int i = 1; + int j = 0; + int count = 0; + const int number_of_fields = 20; + int closed = 1; + int session_closed = 1; + gf_gsync_status_t **status_values = NULL; + char status_value_name[PATH_MAX] = ""; + char *tmp = NULL; + char *volume = NULL; + char *volume_next = NULL; + char *slave = NULL; + char *slave_next = NULL; + static const char *title_values[] = { + "master_node", "", "master_brick", "slave_user", "slave", "slave_node", + "status", "crawl_status", + /* last_synced */ + "", "entry", "data", "meta", "failures", + /* checkpoint_time */ + "", "checkpoint_completed", + /* checkpoint_completion_time */ + "", "master_node_uuid", + /* last_synced_utc */ + "last_synced", + /* checkpoint_time_utc */ + "checkpoint_time", + /* checkpoint_completion_time_utc */ + "checkpoint_completion_time"}; + + GF_ASSERT(dict); + + ret = dict_get_int32(dict, "gsync-count", &count); + if (ret) + goto out; + + status_values = GF_MALLOC(count * sizeof(gf_gsync_status_t *), + gf_common_mt_char); + if (!status_values) { + ret = -1; + goto out; + } + + for (i = 0; i < count; i++) { + status_values[i] = GF_CALLOC(1, sizeof(gf_gsync_status_t), + gf_common_mt_char); + if (!status_values[i]) { + ret = -1; + goto out; + } + + snprintf(status_value_name, sizeof(status_value_name), "status_value%d", + i); + + ret = dict_get_bin(dict, status_value_name, + (void **)&(status_values[i])); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "struct member empty."); + goto out; + } + } + + qsort(status_values, count, sizeof(gf_gsync_status_t *), + gf_gsync_status_t_comparator); + + for (i = 0; i < count; i++) { + if (closed) { + ret = xmlTextWriterStartElement(writer, (xmlChar *)"volume"); + XML_RET_CHECK_AND_GOTO(ret, out); + + tmp = get_struct_variable(1, status_values[i]); + if (!tmp) { + gf_log("cli", GF_LOG_ERROR, "struct member empty."); + ret = -1; + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", + "%s", tmp); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterStartElement(writer, (xmlChar *)"sessions"); + XML_RET_CHECK_AND_GOTO(ret, out); + + closed = 0; + } + + if (session_closed) { + ret = xmlTextWriterStartElement(writer, (xmlChar *)"session"); + XML_RET_CHECK_AND_GOTO(ret, out); + + session_closed = 0; + + tmp = get_struct_variable(21, status_values[i]); + if (!tmp) { + gf_log("cli", GF_LOG_ERROR, "struct member empty."); + ret = -1; + goto out; + } + + ret = xmlTextWriterWriteFormatElement( + writer, (xmlChar *)"session_slave", "%s", tmp); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + ret = xmlTextWriterStartElement(writer, (xmlChar *)"pair"); + XML_RET_CHECK_AND_GOTO(ret, out); + + for (j = 0; j < number_of_fields; j++) { + /* XML ignore fields */ + if (strcmp(title_values[j], "") == 0) + continue; + + tmp = get_struct_variable(j, status_values[i]); + if (!tmp) { + gf_log("cli", GF_LOG_ERROR, "struct member empty."); + ret = -1; + goto out; + } + + ret = xmlTextWriterWriteFormatElement( + writer, (xmlChar *)title_values[j], "%s", tmp); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + if (i + 1 < count) { + slave = get_struct_variable(20, status_values[i]); + slave_next = get_struct_variable(20, status_values[i + 1]); + volume = get_struct_variable(1, status_values[i]); + volume_next = get_struct_variable(1, status_values[i + 1]); + if (!slave || !slave_next || !volume || !volume_next) { + gf_log("cli", GF_LOG_ERROR, "struct member empty."); + ret = -1; + goto out; + } + + if (strcmp(volume, volume_next) != 0) { + closed = 1; + session_closed = 1; + + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + } else if (strcmp(slave, slave_next) != 0) { + session_closed = 1; + + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + } + } else { + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + } + } +out: + if (status_values) + GF_FREE(status_values); + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} +#endif + +int +cli_xml_output_vol_gsync(dict_t *dict, int op_ret, int op_errno, + char *op_errstr) +{ +#if (HAVE_LIB_XML) + int ret = -1; + xmlTextWriterPtr writer = NULL; + xmlDocPtr doc = NULL; + char *master = NULL; + char *slave = NULL; + int type = 0; + + GF_ASSERT(dict); + + ret = cli_begin_xml_output(&writer, &doc); + if (ret) + goto out; + + ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr); + if (ret) + goto out; + + /* <geoRep> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"geoRep"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_int32(dict, "type", &type); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get type"); + goto out; + } + + switch (type) { + case GF_GSYNC_OPTION_TYPE_START: + case GF_GSYNC_OPTION_TYPE_STOP: + case GF_GSYNC_OPTION_TYPE_PAUSE: + case GF_GSYNC_OPTION_TYPE_RESUME: + case GF_GSYNC_OPTION_TYPE_CREATE: + case GF_GSYNC_OPTION_TYPE_DELETE: + if (dict_get_str(dict, "master", &master) != 0) + master = "???"; + if (dict_get_str(dict, "slave", &slave) != 0) + slave = "???"; + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"master", + "%s", master); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"slave", + "%s", slave); + XML_RET_CHECK_AND_GOTO(ret, out); + + break; + + case GF_GSYNC_OPTION_TYPE_CONFIG: + if (op_ret == 0) { + ret = xmlTextWriterStartElement(writer, (xmlChar *)"config"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = cli_xml_generate_gsync_config(dict, writer); + if (ret) + goto out; + + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + break; + case GF_GSYNC_OPTION_TYPE_STATUS: + ret = cli_xml_output_vol_gsync_status(dict, writer); + if (ret) { + gf_log("cli", GF_LOG_DEBUG, "Failed to get gsync status"); + goto out; + } + break; + default: + ret = 0; + break; + } + + /* </geoRep> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = cli_end_xml_output(writer, doc); +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +#else + return 0; +#endif +} + +#if (HAVE_LIB_XML) +/* This function will generate snapshot create output in xml format. + * + * @param writer xmlTextWriterPtr + * @param doc xmlDocPtr + * @param dict dict containing create output + * + * @return 0 on success and -1 on failure + */ +static int +cli_xml_snapshot_create(xmlTextWriterPtr writer, xmlDocPtr doc, dict_t *dict) +{ + int ret = -1; + char *str_value = NULL; + + GF_ASSERT(writer); + GF_ASSERT(doc); + GF_ASSERT(dict); + + /* <snapCreate> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapCreate"); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* <snapshot> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapshot"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_str(dict, "snapname", &str_value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get snap name"); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s", + str_value); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_str(dict, "snapuuid", &str_value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get snap uuid"); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"uuid", "%s", + str_value); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </snapshot> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </snapCreate> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = 0; +out: + return ret; +} + +/* This function will generate snapshot clone output in xml format. + * + * @param writer xmlTextWriterPtr + * @param doc xmlDocPtr + * @param dict dict containing create output + * + * @return 0 on success and -1 on failure + */ +static int +cli_xml_snapshot_clone(xmlTextWriterPtr writer, xmlDocPtr doc, dict_t *dict) +{ + int ret = -1; + char *str_value = NULL; + + GF_VALIDATE_OR_GOTO("cli", writer, out); + GF_VALIDATE_OR_GOTO("cli", doc, out); + GF_VALIDATE_OR_GOTO("cli", dict, out); + + /* <CloneCreate> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"CloneCreate"); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* <volume> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"volume"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_str(dict, "clonename", &str_value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get clone name"); + goto out; + } + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s", + str_value); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_str(dict, "snapuuid", &str_value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get clone uuid"); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"uuid", "%s", + str_value); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </volume> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </CloneCreate> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = 0; +out: + return ret; +} + +/* This function will generate snapshot restore output in xml format. + * + * @param writer xmlTextWriterPtr + * @param doc xmlDocPtr + * @param dict dict containing restore output + * + * @return 0 on success and -1 on failure + */ +static int +cli_xml_snapshot_restore(xmlTextWriterPtr writer, xmlDocPtr doc, dict_t *dict) +{ + int ret = -1; + char *str_value = NULL; + + GF_ASSERT(writer); + GF_ASSERT(doc); + GF_ASSERT(dict); + + /* <snapRestore> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapRestore"); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* <volume> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"volume"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_str(dict, "volname", &str_value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get vol name"); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s", + str_value); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_str(dict, "volid", &str_value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get volume id"); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"uuid", "%s", + str_value); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </volume> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* <snapshot> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapshot"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_str(dict, "snapname", &str_value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get snap name"); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s", + str_value); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_str(dict, "snapuuid", &str_value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get snap uuid"); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"uuid", "%s", + str_value); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </snapshot> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </snapRestore> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = 0; +out: + + return ret; +} + +/* This function will generate snapshot list output in xml format. + * + * @param writer xmlTextWriterPtr + * @param doc xmlDocPtr + * @param dict dict containing list output + * + * @return 0 on success and -1 on failure + */ +static int +cli_xml_snapshot_list(xmlTextWriterPtr writer, xmlDocPtr doc, dict_t *dict) +{ + int ret = -1; + int i = 0; + int snapcount = 0; + char *str_value = NULL; + char key[PATH_MAX] = ""; + + GF_ASSERT(writer); + GF_ASSERT(doc); + GF_ASSERT(dict); + + /* <snapList> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapList"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_int32(dict, "snapcount", &snapcount); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get snapcount"); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"count", "%d", + snapcount); + XML_RET_CHECK_AND_GOTO(ret, out); + + for (i = 1; i <= snapcount; ++i) { + ret = snprintf(key, sizeof(key), "snapname%d", i); + if (ret < 0) { + goto out; + } + + ret = dict_get_str(dict, key, &str_value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not get %s ", key); + goto out; + } else { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"snapshot", + "%s", str_value); + XML_RET_CHECK_AND_GOTO(ret, out); + } + } + + /* </snapList> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = 0; +out: + + return ret; +} + +/* This function will generate xml output for origin volume + * of the given snapshot. + * + * @param writer xmlTextWriterPtr + * @param doc xmlDocPtr + * @param dict dict containing info output + * @param keyprefix prefix for dictionary key + * + * @return 0 on success and -1 on failure + */ +static int +cli_xml_snapshot_info_orig_vol(xmlTextWriterPtr writer, xmlDocPtr doc, + dict_t *dict, char *keyprefix) +{ + int ret = -1; + int value = 0; + char *buffer = NULL; + char key[PATH_MAX] = ""; + + GF_ASSERT(dict); + GF_ASSERT(keyprefix); + GF_ASSERT(writer); + GF_ASSERT(doc); + + /* <originVolume> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"originVolume"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%sorigin-volname", keyprefix); + + ret = dict_get_str(dict, key, &buffer); + if (ret) { + gf_log("cli", GF_LOG_WARNING, "Failed to get %s", key); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s", + buffer); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%ssnapcount", keyprefix); + + ret = dict_get_int32(dict, key, &value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get %s", key); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"snapCount", "%d", + value); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%ssnaps-available", keyprefix); + + ret = dict_get_int32(dict, key, &value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get %s", key); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"snapRemaining", + "%d", value); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </originVolume> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = 0; +out: + return ret; +} + +/* This function will generate xml output of snapshot volume info. + * + * @param writer xmlTextWriterPtr + * @param doc xmlDocPtr + * @param dict dict containing info output + * @param keyprefix key prefix for dictionary + * @param snap_driven boolean to check if output is based of volume + * or snapshot + * + * @return 0 on success and -1 on failure + */ +static int +cli_xml_snapshot_info_snap_vol(xmlTextWriterPtr writer, xmlDocPtr doc, + dict_t *dict, char *keyprefix, + gf_boolean_t snap_driven) +{ + char key[PATH_MAX] = ""; + char *buffer = NULL; + int ret = -1; + + GF_ASSERT(dict); + GF_ASSERT(keyprefix); + GF_ASSERT(writer); + GF_ASSERT(doc); + + /* <snapVolume> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapVolume"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = snprintf(key, sizeof(key), "%s.volname", keyprefix); + if (ret < 0) + goto out; + + ret = dict_get_str(dict, key, &buffer); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get %s", key); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s", + buffer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = snprintf(key, sizeof(key), "%s.vol-status", keyprefix); + if (ret < 0) + goto out; + + ret = dict_get_str(dict, key, &buffer); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get %s", key); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"status", "%s", + buffer); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* If the command is snap_driven then we need to show origin volume + * info. Else this is shown in the start of info display.*/ + if (snap_driven) { + ret = snprintf(key, sizeof(key), "%s.", keyprefix); + if (ret < 0) + goto out; + + ret = cli_xml_snapshot_info_orig_vol(writer, doc, dict, key); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to create " + "xml output for snapshot's origin volume"); + goto out; + } + } + + /* </snapVolume> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = 0; +out: + return ret; +} + +/* This function will generate snapshot info of individual snapshot + * in xml format. + * + * @param writer xmlTextWriterPtr + * @param doc xmlDocPtr + * @param dict dict containing info output + * @param keyprefix key prefix for dictionary + * @param snap_driven boolean to check if output is based of volume + * or snapshot + * + * @return 0 on success and -1 on failure + */ +static int +cli_xml_snapshot_info_per_snap(xmlTextWriterPtr writer, xmlDocPtr doc, + dict_t *dict, char *keyprefix, + gf_boolean_t snap_driven) +{ + char key_buffer[PATH_MAX] = ""; + char *buffer = NULL; + int volcount = 0; + int ret = -1; + int i = 0; + + GF_ASSERT(dict); + GF_ASSERT(keyprefix); + GF_ASSERT(writer); + GF_ASSERT(doc); + + /* <snapshot> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapshot"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = snprintf(key_buffer, sizeof(key_buffer), "%s.snapname", keyprefix); + if (ret < 0) + goto out; + + ret = dict_get_str(dict, key_buffer, &buffer); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to fetch snapname %s ", key_buffer); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s", + buffer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = snprintf(key_buffer, sizeof(key_buffer), "%s.snap-id", keyprefix); + if (ret < 0) + goto out; + + ret = dict_get_str(dict, key_buffer, &buffer); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to fetch snap-id %s ", key_buffer); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"uuid", "%s", + buffer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = snprintf(key_buffer, sizeof(key_buffer), "%s.snap-desc", keyprefix); + if (ret < 0) + goto out; + + ret = dict_get_str(dict, key_buffer, &buffer); + if (!ret) { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"description", + "%s", buffer); + } else { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"description", + "%s", ""); + } + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = snprintf(key_buffer, sizeof(key_buffer), "%s.snap-time", keyprefix); + if (ret < 0) + goto out; + + ret = dict_get_str(dict, key_buffer, &buffer); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to fetch snap-time %s ", keyprefix); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"createTime", "%s", + buffer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = snprintf(key_buffer, sizeof(key_buffer), "%s.vol-count", keyprefix); + if (ret < 0) + goto out; + + ret = dict_get_int32(dict, key_buffer, &volcount); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Fail to get snap vol count"); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"volCount", "%d", + volcount); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_int32(dict, key_buffer, &volcount); + /* Display info of each snapshot volume */ + for (i = 1; i <= volcount; i++) { + ret = snprintf(key_buffer, sizeof(key_buffer), "%s.vol%d", keyprefix, + i); + if (ret < 0) + goto out; + + ret = cli_xml_snapshot_info_snap_vol(writer, doc, dict, key_buffer, + snap_driven); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Could not list " + "details of volume in a snap"); + goto out; + } + } + + /* </snapshot> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); +out: + return ret; +} + +/* This function will generate snapshot info output in xml format. + * + * @param writer xmlTextWriterPtr + * @param doc xmlDocPtr + * @param dict dict containing info output + * + * @return 0 on success and -1 on failure + */ +static int +cli_xml_snapshot_info(xmlTextWriterPtr writer, xmlDocPtr doc, dict_t *dict) +{ + int ret = -1; + int i = 0; + int snapcount = 0; + char key[PATH_MAX] = ""; + gf_boolean_t snap_driven = _gf_false; + + GF_ASSERT(writer); + GF_ASSERT(doc); + GF_ASSERT(dict); + + /* <snapInfo> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapInfo"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snap_driven = dict_get_str_boolean(dict, "snap-driven", _gf_false); + + /* If the approach is volume based then we should display origin volume + * information first followed by per snap info*/ + if (!snap_driven) { + ret = cli_xml_snapshot_info_orig_vol(writer, doc, dict, ""); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to create " + "xml output for snapshot's origin volume"); + goto out; + } + } + + ret = dict_get_int32(dict, "snapcount", &snapcount); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get snapcount"); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"count", "%d", + snapcount); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* <snapshots> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapshots"); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* Get snapshot info of individual snapshots */ + for (i = 1; i <= snapcount; ++i) { + snprintf(key, sizeof(key), "snap%d", i); + + ret = cli_xml_snapshot_info_per_snap(writer, doc, dict, key, + snap_driven); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not get %s ", key); + goto out; + } + } + + /* </snapshots> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </snapInfo> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = 0; +out: + + return ret; +} + +/* This function will generate snapshot status of individual + * snapshot volume in xml format. + * + * @param writer xmlTextWriterPtr + * @param doc xmlDocPtr + * @param dict dict containing status output + * @param keyprefix key prefix for dictionary + * + * @return 0 on success and -1 on failure + */ +static int +cli_xml_snapshot_volume_status(xmlTextWriterPtr writer, xmlDocPtr doc, + dict_t *dict, const char *keyprefix) +{ + int ret = -1; + int brickcount = 0; + int i = 0; + int pid = 0; + char *buffer = NULL; + char key[PATH_MAX] = ""; + + GF_ASSERT(writer); + GF_ASSERT(doc); + GF_ASSERT(dict); + GF_ASSERT(keyprefix); + + ret = snprintf(key, sizeof(key), "%s.brickcount", keyprefix); + if (ret < 0) + goto out; + + ret = dict_get_int32(dict, key, &brickcount); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to fetch brickcount"); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"brickCount", "%d", + brickcount); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* Get status of every brick belonging to the snapshot volume */ + for (i = 0; i < brickcount; i++) { + /* <snapInfo> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"brick"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = snprintf(key, sizeof(key), "%s.brick%d.path", keyprefix, i); + if (ret < 0) + goto out; + + ret = dict_get_str(dict, key, &buffer); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to get Brick Path"); + /* + * If path itself is not present, then end * + * this brick's status and continue to the * + * brick * + */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + continue; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"path", "%s", + buffer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = snprintf(key, sizeof(key), "%s.brick%d.vgname", keyprefix, i); + if (ret < 0) + goto out; + + ret = dict_get_str(dict, key, &buffer); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to get Volume Group"); + ret = xmlTextWriterWriteFormatElement( + writer, (xmlChar *)"volumeGroup", "N/A"); + } else + ret = xmlTextWriterWriteFormatElement( + writer, (xmlChar *)"volumeGroup", "%s", buffer); + + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = snprintf(key, sizeof(key), "%s.brick%d.status", keyprefix, i); + if (ret < 0) + goto out; + + ret = dict_get_str(dict, key, &buffer); + if (ret) { + gf_log("cli", GF_LOG_INFO, "Unable to get Brick Running"); + ret = xmlTextWriterWriteFormatElement( + writer, (xmlChar *)"brick_running", "N/A"); + } else + ret = xmlTextWriterWriteFormatElement( + writer, (xmlChar *)"brick_running", "%s", buffer); + + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = snprintf(key, sizeof(key), "%s.brick%d.pid", keyprefix, i); + if (ret < 0) + goto out; + + ret = dict_get_int32(dict, key, &pid); + if (ret) { + gf_log("cli", GF_LOG_INFO, "Unable to get pid"); + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"pid", + "N/A"); + } else + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"pid", + "%d", pid); + + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = snprintf(key, sizeof(key), "%s.brick%d.data", keyprefix, i); + if (ret < 0) + goto out; + + ret = dict_get_str(dict, key, &buffer); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to get Data Percent"); + ret = xmlTextWriterWriteFormatElement( + writer, (xmlChar *)"data_percentage", "N/A"); + } else + ret = xmlTextWriterWriteFormatElement( + writer, (xmlChar *)"data_percentage", "%s", buffer); + + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = snprintf(key, sizeof(key), "%s.brick%d.lvsize", keyprefix, i); + if (ret < 0) + goto out; + + ret = dict_get_str(dict, key, &buffer); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to get LV Size"); + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"lvSize", + "N/A"); + } else { + /* Truncate any newline character */ + buffer = strtok(buffer, "\n"); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"lvSize", + "%s", buffer); + } + + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </brick> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + } + +out: + return ret; +} + +/* This function will generate snapshot status of individual + * snapshot in xml format. + * + * @param writer xmlTextWriterPtr + * @param doc xmlDocPtr + * @param dict dict containing status output + * + * @return 0 on success and -1 on failure + */ +static int +cli_xml_snapshot_status_per_snap(xmlTextWriterPtr writer, xmlDocPtr doc, + dict_t *dict, const char *keyprefix) +{ + int ret = -1; + int volcount = 0; + int i = 0; + char *buffer = NULL; + char key[PATH_MAX] = ""; + + GF_ASSERT(writer); + GF_ASSERT(doc); + GF_ASSERT(dict); + GF_ASSERT(keyprefix); + + ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapshot"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.snapname", keyprefix); + + ret = dict_get_str(dict, key, &buffer); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to get snapname"); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s", + buffer); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.uuid", keyprefix); + + ret = dict_get_str(dict, key, &buffer); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to get snap UUID"); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"uuid", "%s", + buffer); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.volcount", keyprefix); + + ret = dict_get_int32(dict, key, &volcount); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Unable to get volume count"); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"volCount", "%d", + volcount); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* Get snapshot status of individual snapshot volume */ + for (i = 0; i < volcount; i++) { + /* <volume> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"volume"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(key, sizeof(key), "%s.vol%d", keyprefix, i); + + ret = cli_xml_snapshot_volume_status(writer, doc, dict, key); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not get snap volume status"); + goto out; + } + + /* </volume> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + /* </snapshot> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = 0; +out: + return ret; +} + +/* This function will generate snapshot status output in xml format. + * + * @param writer xmlTextWriterPtr + * @param doc xmlDocPtr + * @param dict dict containing status output + * + * @return 0 on success and -1 on failure + */ +static int +cli_xml_snapshot_status(xmlTextWriterPtr writer, xmlDocPtr doc, dict_t *dict) +{ + int ret = -1; + int snapcount = 0; + int i = 0; + int status_cmd = 0; + char key[PATH_MAX] = ""; + + GF_ASSERT(writer); + GF_ASSERT(doc); + GF_ASSERT(dict); + + /* <snapStatus> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapStatus"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_int32(dict, "sub-cmd", &status_cmd); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not fetch status type"); + goto out; + } + + if ((GF_SNAP_STATUS_TYPE_SNAP == status_cmd) || + (GF_SNAP_STATUS_TYPE_ITER == status_cmd)) { + snapcount = 1; + } else { + ret = dict_get_int32(dict, "status.snapcount", &snapcount); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not get snapcount"); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"count", "%d", + snapcount); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + for (i = 0; i < snapcount; i++) { + snprintf(key, sizeof(key), "status.snap%d", i); + + ret = cli_xml_snapshot_status_per_snap(writer, doc, dict, key); + if (ret < 0) { + gf_log("cli", GF_LOG_ERROR, + "failed to create xml " + "output for snapshot status"); + goto out; + } + } + + /* </snapStatus> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = 0; +out: + + return ret; +} + +/* This function will generate snapshot config show output in xml format. + * + * @param writer xmlTextWriterPtr + * @param doc xmlDocPtr + * @param dict dict containing status output + * + * @return 0 on success and -1 on failure + */ +static int +cli_xml_snapshot_config_show(xmlTextWriterPtr writer, xmlDocPtr doc, + dict_t *dict) +{ + int ret = -1; + uint64_t i = 0; + uint64_t value = 0; + uint64_t volcount = 0; + char buf[PATH_MAX] = ""; + char *str_value = NULL; + + GF_ASSERT(writer); + GF_ASSERT(doc); + GF_ASSERT(dict); + + /* <systemConfig> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"systemConfig"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_uint64(dict, "snap-max-hard-limit", &value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to get " + "snap-max-hard-limit"); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"hardLimit", + "%" PRIu64, value); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_uint64(dict, "snap-max-soft-limit", &value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to get " + "snap-max-soft-limit"); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"softLimit", + "%" PRIu64 "%%", value); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_str(dict, "auto-delete", &str_value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not fetch auto-delete"); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"autoDelete", "%s", + str_value); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_str(dict, "snap-activate-on-create", &str_value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Could not fetch snap-activate-on-create-delete"); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"activateOnCreate", + "%s", str_value); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </systemConfig> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* <volumeConfig> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"volumeConfig"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_uint64(dict, "voldisplaycount", &volcount); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not fetch volcount"); + goto out; + } + + /* Get config of all the volumes */ + for (i = 0; i < volcount; i++) { + /* <volume> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"volume"); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(buf, sizeof(buf), "volume%" PRIu64 "-volname", i); + ret = dict_get_str(dict, buf, &str_value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not fetch %s", buf); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s", + str_value); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(buf, sizeof(buf), "volume%" PRIu64 "-snap-max-hard-limit", i); + ret = dict_get_uint64(dict, buf, &value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not fetch %s", buf); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"hardLimit", + "%" PRIu64, value); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(buf, sizeof(buf), "volume%" PRIu64 "-active-hard-limit", i); + ret = dict_get_uint64(dict, buf, &value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Could not fetch" + " effective snap_max_hard_limit for " + "%s", + str_value); + goto out; + } + + ret = xmlTextWriterWriteFormatElement( + writer, (xmlChar *)"effectiveHardLimit", "%" PRIu64, value); + XML_RET_CHECK_AND_GOTO(ret, out); + + snprintf(buf, sizeof(buf), "volume%" PRIu64 "-snap-max-soft-limit", i); + ret = dict_get_uint64(dict, buf, &value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not fetch %s", buf); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"softLimit", + "%" PRIu64, value); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </volume> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + /* </volume> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = 0; +out: + return ret; +} + +/* This function will generate snapshot config set output in xml format. + * + * @param writer xmlTextWriterPtr + * @param doc xmlDocPtr + * @param dict dict containing status output + * + * @return 0 on success and -1 on failure + */ +static int +cli_xml_snapshot_config_set(xmlTextWriterPtr writer, xmlDocPtr doc, + dict_t *dict) +{ + int ret = -1; + uint64_t hard_limit = 0; + uint64_t soft_limit = 0; + char *volname = NULL; + char *auto_delete = NULL; + char *snap_activate = NULL; + + GF_ASSERT(writer); + GF_ASSERT(doc); + GF_ASSERT(dict); + + /* This is optional parameter therefore ignore the error */ + ret = dict_get_uint64(dict, "snap-max-hard-limit", &hard_limit); + /* This is optional parameter therefore ignore the error */ + ret = dict_get_uint64(dict, "snap-max-soft-limit", &soft_limit); + ret = dict_get_str(dict, "auto-delete", &auto_delete); + ret = dict_get_str(dict, "snap-activate-on-create", &snap_activate); + + if (!hard_limit && !soft_limit && !auto_delete && !snap_activate) { + ret = -1; + gf_log("cli", GF_LOG_ERROR, + "At least one option from " + "snap-max-hard-limit, snap-max-soft-limit, auto-delete" + " and snap-activate-on-create should be set"); + goto out; + } + + /* Ignore the error, as volname is optional */ + ret = dict_get_str(dict, "volname", &volname); + + if (NULL == volname) { + /* <systemConfig> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"systemConfig"); + } else { + /* <volumeConfig> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"volumeConfig"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s", + volname); + } + + XML_RET_CHECK_AND_GOTO(ret, out); + + if (hard_limit) { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"newHardLimit", + "%" PRIu64, hard_limit); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + if (soft_limit) { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"newSoftLimit", + "%" PRIu64, soft_limit); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + if (auto_delete) { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"autoDelete", + "%s", auto_delete); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + if (snap_activate) { + ret = xmlTextWriterWriteFormatElement( + writer, (xmlChar *)"activateOnCreate", "%s", snap_activate); + XML_RET_CHECK_AND_GOTO(ret, out); + } + + /* </volumeConfig> or </systemConfig> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = 0; +out: + return ret; +} + +/* This function will generate snapshot config output in xml format. + * + * @param writer xmlTextWriterPtr + * @param doc xmlDocPtr + * @param dict dict containing config output + * + * @return 0 on success and -1 on failure + */ +static int +cli_xml_snapshot_config(xmlTextWriterPtr writer, xmlDocPtr doc, dict_t *dict) +{ + int ret = -1; + int config_command = 0; + + GF_ASSERT(writer); + GF_ASSERT(doc); + GF_ASSERT(dict); + + /* <snapConfig> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapConfig"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_int32(dict, "config-command", &config_command); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Could not fetch config type"); + goto out; + } + + switch (config_command) { + case GF_SNAP_CONFIG_TYPE_SET: + ret = cli_xml_snapshot_config_set(writer, doc, dict); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to create xml " + "output for snapshot config set command"); + goto out; + } + + break; + case GF_SNAP_CONFIG_DISPLAY: + ret = cli_xml_snapshot_config_show(writer, doc, dict); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to create xml " + "output for snapshot config show command"); + goto out; + } + break; + default: + gf_log("cli", GF_LOG_ERROR, "Unknown config command :%d", + config_command); + ret = -1; + goto out; + } + + /* </snapConfig> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = 0; +out: + + return ret; +} + +/* This function will generate snapshot activate or + * deactivate output in xml format. + * + * @param writer xmlTextWriterPtr + * @param doc xmlDocPtr + * @param dict dict containing activate or deactivate output + * + * @return 0 on success and -1 on failure + */ +static int +cli_xml_snapshot_activate_deactivate(xmlTextWriterPtr writer, xmlDocPtr doc, + dict_t *dict, int cmd) +{ + int ret = -1; + char *buffer = NULL; + char *tag = NULL; + + GF_ASSERT(writer); + GF_ASSERT(doc); + GF_ASSERT(dict); + + if (GF_SNAP_OPTION_TYPE_ACTIVATE == cmd) { + tag = "snapActivate"; + } else if (GF_SNAP_OPTION_TYPE_DEACTIVATE == cmd) { + tag = "snapDeactivate"; + } else { + gf_log("cli", GF_LOG_ERROR, "invalid command %d", cmd); + goto out; + } + + /* <snapActivate> or <snapDeactivate> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)tag); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* <snapshot> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapshot"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_str(dict, "snapname", &buffer); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get snap name"); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s", + buffer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_str(dict, "snapuuid", &buffer); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get snap uuid"); + goto out; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"uuid", "%s", + buffer); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </snapshot> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </snapActivate> or </snapDeactivate> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = 0; +out: + + return ret; +} +#endif /* HAVE_LIB_XML */ + +/* This function will generate snapshot delete output in xml format. + * + * @param writer xmlTextWriterPtr + * @param doc xmlDocPtr + * @param dict dict containing delete output + * @param rsp cli response + * + * @return 0 on success and -1 on failure + */ +int +cli_xml_snapshot_delete(cli_local_t *local, dict_t *dict, gf_cli_rsp *rsp) +{ + int ret = -1; +#ifdef HAVE_LIB_XML + char *str_value = NULL; + xmlTextWriterPtr writer = local->writer; + xmlDocPtr doc = local->doc; + + GF_ASSERT(writer); + GF_ASSERT(doc); + GF_ASSERT(dict); + + /* <snapshot> */ + ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapshot"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_str(dict, "snapname", &str_value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get snap name"); + goto xmlend; + } + + if (!rsp->op_ret) { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"status", + "Success"); + } else { + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"status", + "Failure"); + XML_RET_CHECK_AND_GOTO(ret, xmlend); + + ret = cli_xml_output_common(writer, rsp->op_ret, rsp->op_errno, + rsp->op_errstr); + } + XML_RET_CHECK_AND_GOTO(ret, xmlend); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s", + str_value); + XML_RET_CHECK_AND_GOTO(ret, xmlend); + + ret = dict_get_str(dict, "snapuuid", &str_value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get snap uuid"); + goto xmlend; + } + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"uuid", "%s", + str_value); + XML_RET_CHECK_AND_GOTO(ret, out); + +xmlend: + /* </snapshot> */ + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + +#endif /* HAVE_LIB_XML */ + ret = 0; +out: + + return ret; +} + +int +cli_xml_output_snap_status_begin(cli_local_t *local, int op_ret, int op_errno, + char *op_errstr) +{ +#if (HAVE_LIB_XML) + int ret = -1; + + GF_ASSERT(local); + + ret = cli_begin_xml_output(&(local->writer), &(local->doc)); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = cli_xml_output_common(local->writer, op_ret, op_errno, op_errstr); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* <snapStatus> */ + ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"snapStatus"); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* <snapshots> */ + ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"snapshots"); + XML_RET_CHECK_AND_GOTO(ret, out); + +out: + gf_log("cli", GF_LOG_TRACE, "Returning %d", ret); + return ret; +#else + return 0; +#endif +} + +int +cli_xml_output_snap_status_end(cli_local_t *local) +{ +#if (HAVE_LIB_XML) + int ret = -1; + + GF_ASSERT(local); + + /* </snapshots> */ + ret = xmlTextWriterEndElement(local->writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </snapStatus> */ + ret = xmlTextWriterEndElement(local->writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = cli_end_xml_output(local->writer, local->doc); +out: + gf_log("cli", GF_LOG_TRACE, "Returning %d", ret); + return ret; +#else + return 0; +#endif +} + +int +cli_xml_output_snap_delete_begin(cli_local_t *local, int op_ret, int op_errno, + char *op_errstr) +{ +#if (HAVE_LIB_XML) + int ret = -1; + int delete_cmd = -1; + + GF_ASSERT(local); + + ret = cli_begin_xml_output(&(local->writer), &(local->doc)); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_int32(local->dict, "sub-cmd", &delete_cmd); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Failed to get sub-cmd"); + goto out; + } + + ret = cli_xml_output_common(local->writer, op_ret, op_errno, op_errstr); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* <snapStatus> */ + ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"snapDelete"); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* <snapshots> */ + ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"snapshots"); + XML_RET_CHECK_AND_GOTO(ret, out); + +out: + gf_log("cli", GF_LOG_TRACE, "Returning %d", ret); + return ret; +#else + return 0; +#endif +} + +int +cli_xml_output_snap_delete_end(cli_local_t *local) +{ +#if (HAVE_LIB_XML) + int ret = -1; + + GF_ASSERT(local); + + /* </snapshots> */ + ret = xmlTextWriterEndElement(local->writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + /* </snapDelete> */ + ret = xmlTextWriterEndElement(local->writer); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = cli_end_xml_output(local->writer, local->doc); +out: + gf_log("cli", GF_LOG_TRACE, "Returning %d", ret); + return ret; +#else + return 0; +#endif +} +/* This function will generate xml output for all the snapshot commands + * + * @param cmd_type command type + * @param dict dict containing snapshot command output + * @param op_ret return value of the snapshot command + * @param op_errno errno for the snapshot command + * @param op_errstr error string for the snapshot command + * + * @return 0 on success and -1 on failure + */ +int +cli_xml_output_snapshot(int cmd_type, dict_t *dict, int op_ret, int op_errno, + char *op_errstr) +{ +#if (HAVE_LIB_XML) + int ret = -1; + xmlTextWriterPtr writer = NULL; + xmlDocPtr doc = NULL; + + GF_ASSERT(dict); + + ret = cli_begin_xml_output(&writer, &doc); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to output " + "xml begin block"); + goto out; + } + + ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to output " + "xml common block"); + goto out; + } + + /* In case of command failure just printing the error message is good + * enough */ + if (0 != op_ret) { + goto end; + } + + switch (cmd_type) { + case GF_SNAP_OPTION_TYPE_CREATE: + ret = cli_xml_snapshot_create(writer, doc, dict); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to create " + "xml output for snapshot create command"); + goto out; + } + break; + case GF_SNAP_OPTION_TYPE_CLONE: + ret = cli_xml_snapshot_clone(writer, doc, dict); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to create " + "xml output for snapshot clone command"); + goto out; + } + break; + case GF_SNAP_OPTION_TYPE_RESTORE: + ret = cli_xml_snapshot_restore(writer, doc, dict); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to create " + "xml output for snapshot restore command"); + goto out; + } + break; + case GF_SNAP_OPTION_TYPE_LIST: + ret = cli_xml_snapshot_list(writer, doc, dict); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to create " + "xml output for snapshot list command"); + goto out; + } + break; + case GF_SNAP_OPTION_TYPE_STATUS: + ret = cli_xml_snapshot_status(writer, doc, dict); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to create" + "xml output for snapshot status command"); + goto out; + } + break; + case GF_SNAP_OPTION_TYPE_INFO: + ret = cli_xml_snapshot_info(writer, doc, dict); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to create " + "xml output for snapshot info command"); + goto out; + } + break; + case GF_SNAP_OPTION_TYPE_ACTIVATE: + case GF_SNAP_OPTION_TYPE_DEACTIVATE: + ret = cli_xml_snapshot_activate_deactivate(writer, doc, dict, + cmd_type); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to create " + "xml output for snapshot config command"); + } + break; + case GF_SNAP_OPTION_TYPE_CONFIG: + ret = cli_xml_snapshot_config(writer, doc, dict); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to create " + "xml output for snapshot config command"); + } + break; + default: + gf_log("cli", GF_LOG_ERROR, "Unexpected snapshot command: %d", + cmd_type); + goto out; + } + +end: + ret = cli_end_xml_output(writer, doc); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to output " + "xml end block"); + goto out; + } + + ret = 0; +out: + return ret; +#else + return 0; +#endif /* HAVE_LIB_XML */ +} + +int +cli_xml_snapshot_begin_composite_op(cli_local_t *local) +{ + int ret = -1; +#ifdef HAVE_LIB_XML + int cmd = -1; + int type = -1; + + ret = dict_get_int32(local->dict, "sub-cmd", &cmd); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to get " + "sub-cmd"); + ret = 0; + goto out; + } + + if (cmd == GF_SNAP_STATUS_TYPE_ITER || cmd == GF_SNAP_DELETE_TYPE_SNAP) { + ret = 0; + goto out; + } + + ret = dict_get_int32(local->dict, "type", &type); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to get snapshot " + "command type from dictionary"); + goto out; + } + + if (GF_SNAP_OPTION_TYPE_STATUS == type) + ret = cli_xml_output_snap_status_begin(local, 0, 0, NULL); + else if (GF_SNAP_OPTION_TYPE_DELETE == type) + ret = cli_xml_output_snap_delete_begin(local, 0, 0, NULL); + + if (ret) { + gf_log("cli", GF_LOG_ERROR, "Error creating xml output"); + goto out; + } + +#endif /* HAVE_LIB_XML */ + ret = 0; +out: + return ret; +} + +int +cli_xml_snapshot_end_composite_op(cli_local_t *local) +{ + int ret = -1; +#ifdef HAVE_LIB_XML + int cmd = -1; + int type = -1; + + ret = dict_get_int32(local->dict, "sub-cmd", &cmd); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to get " + "sub-cmd"); + ret = 0; + goto out; + } + + if (cmd == GF_SNAP_STATUS_TYPE_ITER || cmd == GF_SNAP_DELETE_TYPE_SNAP) { + ret = 0; + goto out; + } + + ret = dict_get_int32(local->dict, "type", &type); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to get snapshot " + "command type from dictionary"); + goto out; + } + + if (GF_SNAP_OPTION_TYPE_STATUS == type) + ret = cli_xml_output_snap_status_end(local); + else if (GF_SNAP_OPTION_TYPE_DELETE == type) + ret = cli_xml_output_snap_delete_end(local); + + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Error creating xml " + "output"); + goto out; + } +#endif /* HAVE_LIB_XML */ + ret = 0; +out: + return ret; +} + +int +cli_xml_snapshot_status_single_snap(cli_local_t *local, dict_t *dict, char *key) +{ +#if (HAVE_LIB_XML) + int ret = -1; + + GF_VALIDATE_OR_GOTO("cli", (local != NULL), out); + GF_VALIDATE_OR_GOTO("cli", (dict != NULL), out); + GF_VALIDATE_OR_GOTO("cli", (key != NULL), out); + + ret = cli_xml_snapshot_status_per_snap(local->writer, local->doc, dict, + key); +out: + return ret; +#else + return 0; +#endif /* HAVE_LIB_XML */ +} + +int +cli_xml_output_vol_getopts(dict_t *dict, int op_ret, int op_errno, + char *op_errstr) +{ +#if (HAVE_LIB_XML) + int i = 0; + int ret = -1; + int count = 0; + xmlTextWriterPtr writer = NULL; + xmlDocPtr doc = NULL; + char *key = NULL; + char *value = NULL; + char dict_key[50] = { + 0, + }; + + ret = cli_begin_xml_output(&writer, &doc); + if (ret) + goto out; + + ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr); + if (ret) + goto out; + + ret = xmlTextWriterStartElement(writer, (xmlChar *)"volGetopts"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = dict_get_int32(dict, "count", &count); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to retrieve count " + "from the dictionary"); + goto out; + } + if (count <= 0) { + gf_log("cli", GF_LOG_ERROR, + "Value of count :%d is " + "invalid", + count); + ret = -1; + goto out; + } + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"count", "%d", + count); + + XML_RET_CHECK_AND_GOTO(ret, out); + + for (i = 1; i <= count; i++) { + sprintf(dict_key, "key%d", i); + ret = dict_get_str(dict, dict_key, &key); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to" + " retrieve %s from the " + "dictionary", + dict_key); + goto out; + } + sprintf(dict_key, "value%d", i); + ret = dict_get_str(dict, dict_key, &value); + if (ret) { + gf_log("cli", GF_LOG_ERROR, + "Failed to " + "retrieve key value for %s from" + "the dictionary", + dict_key); + goto out; + } + ret = xmlTextWriterStartElement(writer, (xmlChar *)"Opt"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"Option", "%s", + key); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"Value", "%s", + value); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterEndElement(writer); + XML_RET_CHECK_AND_GOTO(ret, out); + } + ret = cli_end_xml_output(writer, doc); + +out: + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +#else + return 0; +#endif /* HAVE_LIB_XML */ +} + +int +cli_quota_list_xml_error(cli_local_t *local, char *path, char *errstr) +{ +#if (HAVE_LIB_XML) + int ret = -1; + + ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"limit"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"path", + "%s", path); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"errstr", + "%s", errstr); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterEndElement(local->writer); + XML_RET_CHECK_AND_GOTO(ret, out); + +out: + return ret; +#else + return 0; +#endif +} + +int +cli_quota_xml_output(cli_local_t *local, char *path, int64_t hl_str, + char *sl_final, int64_t sl_num, int64_t used, + int64_t avail, char *sl, char *hl, gf_boolean_t limit_set) +{ +#if (HAVE_LIB_XML) + int ret = -1; + + ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"limit"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"path", + "%s", path); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement( + local->writer, (xmlChar *)"hard_limit", !limit_set ? "N/A" : "%" PRId64, + hl_str); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(local->writer, + (xmlChar *)"soft_limit_percent", + !limit_set ? "N/A" : "%s", sl_final); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement( + local->writer, (xmlChar *)"soft_limit_value", + !limit_set ? "N/A" : "%" PRId64, sl_num); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement( + local->writer, (xmlChar *)"used_space", "%" PRId64, used); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement( + local->writer, (xmlChar *)"avail_space", + !limit_set ? "N/A" : "%" PRId64, avail); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement( + local->writer, (xmlChar *)"sl_exceeded", !limit_set ? "N/A" : "%s", sl); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement( + local->writer, (xmlChar *)"hl_exceeded", !limit_set ? "N/A" : "%s", hl); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterEndElement(local->writer); + XML_RET_CHECK_AND_GOTO(ret, out); + +out: + return ret; +#else + return 0; +#endif /* HAVE_LIB_XML */ +} + +int +cli_quota_object_xml_output(cli_local_t *local, char *path, char *sl_str, + int64_t sl_val, quota_limits_t *limits, + quota_meta_t *used_space, int64_t avail, char *sl, + char *hl, gf_boolean_t limit_set) +{ +#if (HAVE_LIB_XML) + int ret = -1; + + ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"limit"); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"path", + "%s", path); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement( + local->writer, (xmlChar *)"hard_limit", !limit_set ? "N/A" : "%" PRId64, + limits->hl); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(local->writer, + (xmlChar *)"soft_limit_percent", + !limit_set ? "N/A" : "%s", sl_str); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement( + local->writer, (xmlChar *)"soft_limit_value", + !limit_set ? "N/A" : "%" PRIu64, sl_val); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(local->writer, + (xmlChar *)"file_count", "%" PRId64, + used_space->file_count); + + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"dir_count", + "%" PRIu64, used_space->dir_count); + + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"available", + !limit_set ? "N/A" : "%" PRId64, + avail); + + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement( + local->writer, (xmlChar *)"sl_exceeded", !limit_set ? "N/A" : "%s", sl); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterWriteFormatElement( + local->writer, (xmlChar *)"hl_exceeded", !limit_set ? "N/A" : "%s", hl); + XML_RET_CHECK_AND_GOTO(ret, out); + + ret = xmlTextWriterEndElement(local->writer); + XML_RET_CHECK_AND_GOTO(ret, out); + +out: + return ret; +#else + return 0; +#endif /* HAVE_LIB_XML */ +} diff --git a/cli/src/cli.c b/cli/src/cli.c index 48490ddf4c5..a52b39c5fb8 100644 --- a/cli/src/cli.c +++ b/cli/src/cli.c @@ -1,22 +1,12 @@ /* - Copyright (c) 2010 Gluster, Inc. <http://www.gluster.com> - This file is part of GlusterFS. - - GlusterFS is free software; you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - GlusterFS is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see - <http://www.gnu.org/licenses/>. -*/ + Copyright (c) 2010-2016 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + 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 <stdio.h> #include <string.h> #include <stdlib.h> @@ -39,473 +29,884 @@ #include <semaphore.h> #include <errno.h> -#ifndef _CONFIG_H -#define _CONFIG_H -#include "config.h" -#endif - #ifdef HAVE_MALLOC_H #include <malloc.h> #endif -#ifdef HAVE_MALLOC_STATS -#ifdef DEBUG -#include <mcheck.h> -#endif -#endif - #include "cli.h" +#include "cli-quotad-client.h" #include "cli-cmd.h" #include "cli-mem-types.h" -#include "xlator.h" -#include "glusterfs.h" -#include "compat.h" -#include "logging.h" -#include "dict.h" -#include "list.h" -#include "timer.h" -#include "stack.h" -#include "revision.h" -#include "common-utils.h" -#include "event.h" -#include "globals.h" -#include "syscall.h" -#include "call-stub.h" +#include <glusterfs/xlator.h> +#include <glusterfs/glusterfs.h> +#include <glusterfs/compat.h> +#include <glusterfs/logging.h> +#include <glusterfs/dict.h> +#include <glusterfs/list.h> +#include <glusterfs/timer.h> +#include <glusterfs/stack.h> +#include <glusterfs/revision.h> +#include <glusterfs/common-utils.h> +#include <glusterfs/gf-event.h> +#include <glusterfs/syscall.h> +#include <glusterfs/call-stub.h> #include <fnmatch.h> -extern int connected; -/* using argp for command line parsing */ +#include "xdr-generic.h" -const char *argp_program_version = "" \ - PACKAGE_NAME" "PACKAGE_VERSION" built on "__DATE__" "__TIME__ \ - "\nRepository revision: " GLUSTERFS_REPOSITORY_REVISION "\n" \ - "Copyright (c) 2006-2010 Gluster Inc. " \ - "<http://www.gluster.com>\n" \ - "GlusterFS comes with ABSOLUTELY NO WARRANTY.\n" \ - "You may redistribute copies of GlusterFS under the terms of "\ - "the GNU Affero General Public License."; +/* using argp for command line parsing */ +const char *argp_program_version = + "" PACKAGE_NAME " " PACKAGE_VERSION + "\nRepository revision: " GLUSTERFS_REPOSITORY_REVISION + "\n" + "Copyright (c) 2006-2016 Red Hat, Inc. " + "<https://www.gluster.org/>\n" + "GlusterFS comes with ABSOLUTELY NO WARRANTY.\n" + "It is licensed to you under your choice of the GNU Lesser\n" + "General Public License, version 3 or any later version (LGPLv3\n" + "or later), or the GNU General Public License, version 2 (GPLv2),\n" + "in all cases as published by the Free Software Foundation."; const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">"; - +struct rpc_clnt *global_quotad_rpc; struct rpc_clnt *global_rpc; rpc_clnt_prog_t *cli_rpc_prog; +extern struct rpc_clnt_program cli_prog; -extern struct rpc_clnt_program cli3_1_prog; +int cli_default_conn_timeout = 120; +int cli_ten_minutes_timeout = 600; - - - -static char * -generate_uuid () +static int +glusterfs_ctx_defaults_init(glusterfs_ctx_t *ctx) { - char tmp_str[1024] = {0,}; - char hostname[256] = {0,}; - struct timeval tv = {0,}; - struct tm now = {0, }; - char now_str[32]; - - if (gettimeofday (&tv, NULL) == -1) { - gf_log ("glusterfsd", GF_LOG_ERROR, - "gettimeofday: failed %s", - strerror (errno)); - } - - if (gethostname (hostname, 256) == -1) { - gf_log ("glusterfsd", GF_LOG_ERROR, - "gethostname: failed %s", - strerror (errno)); - } + cmd_args_t *cmd_args = NULL; + struct rlimit lim = { + 0, + }; + call_pool_t *pool = NULL; + int ret = -1; + + if (!ctx) + return ret; - localtime_r (&tv.tv_sec, &now); - strftime (now_str, 32, "%Y/%m/%d-%H:%M:%S", &now); - snprintf (tmp_str, 1024, "%s-%d-%s:%" -#ifdef GF_DARWIN_HOST_OS - PRId32, -#else - "ld", -#endif - hostname, getpid(), now_str, tv.tv_usec); + ret = xlator_mem_acct_init(THIS, cli_mt_end); + if (ret != 0) { + gf_log("cli", GF_LOG_ERROR, "Memory accounting init failed."); + return ret; + } + + /* Resetting ret to -1 to so in case of failure + * we can relese allocated resource. + */ + ret = -1; + + ctx->process_uuid = generate_glusterfs_ctx_id(); + if (!ctx->process_uuid) { + gf_log("cli", GF_LOG_ERROR, "Failed to generate uuid."); + goto out; + } + + ctx->page_size = 128 * GF_UNIT_KB; + + ctx->iobuf_pool = iobuf_pool_new(); + if (!ctx->iobuf_pool) { + gf_log("cli", GF_LOG_ERROR, "Failed to create iobuf pool."); + goto out; + } + + ctx->event_pool = gf_event_pool_new(DEFAULT_EVENT_POOL_SIZE, + STARTING_EVENT_THREADS); + if (!ctx->event_pool) { + gf_log("cli", GF_LOG_ERROR, "Failed to create event pool."); + goto out; + } + + pool = GF_CALLOC(1, sizeof(call_pool_t), cli_mt_call_pool_t); + if (!pool) { + gf_log("cli", GF_LOG_ERROR, "Failed to create call pool."); + goto out; + } + + /* frame_mem_pool size 112 * 64 */ + pool->frame_mem_pool = mem_pool_new(call_frame_t, 32); + if (!pool->frame_mem_pool) { + gf_log("cli", GF_LOG_ERROR, "Failed to create frame mem pool."); + goto out; + } + + /* stack_mem_pool size 256 * 128 */ + pool->stack_mem_pool = mem_pool_new(call_stack_t, 16); + + if (!pool->stack_mem_pool) { + gf_log("cli", GF_LOG_ERROR, "Failed to create stack mem pool."); + goto out; + } + + ctx->stub_mem_pool = mem_pool_new(call_stub_t, 16); + if (!ctx->stub_mem_pool) { + gf_log("cli", GF_LOG_ERROR, "Failed to stub mem pool."); + goto out; + } + + ctx->dict_pool = mem_pool_new(dict_t, 32); + if (!ctx->dict_pool) { + gf_log("cli", GF_LOG_ERROR, "Failed to create dict pool."); + goto out; + } + + ctx->dict_pair_pool = mem_pool_new(data_pair_t, 512); + if (!ctx->dict_pair_pool) { + gf_log("cli", GF_LOG_ERROR, "Failed to create dict pair pool."); + goto out; + } + + ctx->dict_data_pool = mem_pool_new(data_t, 512); + if (!ctx->dict_data_pool) { + gf_log("cli", GF_LOG_ERROR, "Failed to create dict data pool."); + goto out; + } + + ctx->logbuf_pool = mem_pool_new(log_buf_t, 256); + if (!ctx->logbuf_pool) { + gf_log("cli", GF_LOG_ERROR, "Failed to create logbuf pool."); + goto out; + } + + INIT_LIST_HEAD(&pool->all_frames); + LOCK_INIT(&pool->lock); + ctx->pool = pool; + + cmd_args = &ctx->cmd_args; + + INIT_LIST_HEAD(&cmd_args->xlator_options); + + lim.rlim_cur = RLIM_INFINITY; + lim.rlim_max = RLIM_INFINITY; + setrlimit(RLIMIT_CORE, &lim); + + ret = 0; - return gf_strdup (tmp_str); +out: + if (ret != 0) { + if (pool) { + mem_pool_destroy(pool->frame_mem_pool); + mem_pool_destroy(pool->stack_mem_pool); + } + GF_FREE(pool); + pool = NULL; + GF_FREE(ctx->process_uuid); + mem_pool_destroy(ctx->stub_mem_pool); + mem_pool_destroy(ctx->dict_pool); + mem_pool_destroy(ctx->dict_pair_pool); + mem_pool_destroy(ctx->dict_data_pool); + mem_pool_destroy(ctx->logbuf_pool); + } + + return ret; } static int -glusterfs_ctx_defaults_init (glusterfs_ctx_t *ctx) +logging_init(glusterfs_ctx_t *ctx, struct cli_state *state) { - cmd_args_t *cmd_args = NULL; - struct rlimit lim = {0, }; - call_pool_t *pool = NULL; - - xlator_mem_acct_init (THIS, cli_mt_end); - - ctx->process_uuid = generate_uuid (); - if (!ctx->process_uuid) - return -1; - - ctx->page_size = 128 * GF_UNIT_KB; - - ctx->iobuf_pool = iobuf_pool_new (8 * GF_UNIT_MB, ctx->page_size); - if (!ctx->iobuf_pool) - return -1; + char *log_file = state->log_file ? state->log_file + : DEFAULT_CLI_LOG_FILE_DIRECTORY + "/cli.log"; + + /* passing ident as NULL means to use default ident for syslog */ + if (gf_log_init(ctx, log_file, NULL) == -1) { + fprintf(stderr, "ERROR: failed to open logfile %s\n", log_file); + } + + /* CLI should not have something to DEBUG after the release, + hence defaulting to INFO loglevel */ + gf_log_set_loglevel(ctx, (state->log_level == GF_LOG_NONE) + ? GF_LOG_INFO + : state->log_level); + + return 0; +} - ctx->event_pool = event_pool_new (DEFAULT_EVENT_POOL_SIZE); - if (!ctx->event_pool) - return -1; +int +cli_submit_request(struct rpc_clnt *rpc, void *req, call_frame_t *frame, + rpc_clnt_prog_t *prog, int procnum, struct iobref *iobref, + xlator_t *this, fop_cbk_fn_t cbkfn, xdrproc_t xdrproc) +{ + int ret = -1; + int count = 0; + struct iovec iov = { + 0, + }; + struct iobuf *iobuf = NULL; + char new_iobref = 0; + ssize_t xdr_size = 0; + + GF_ASSERT(this); + + if (req) { + xdr_size = xdr_sizeof(xdrproc, req); + iobuf = iobuf_get2(this->ctx->iobuf_pool, xdr_size); + if (!iobuf) { + goto out; + }; - pool = GF_CALLOC (1, sizeof (call_pool_t), - cli_mt_call_pool_t); - if (!pool) - return -1; + if (!iobref) { + iobref = iobref_new(); + if (!iobref) { + goto out; + } - /* frame_mem_pool size 112 * 16k */ - pool->frame_mem_pool = mem_pool_new (call_frame_t, 16384); + new_iobref = 1; + } - if (!pool->frame_mem_pool) - return -1; + iobref_add(iobref, iobuf); - /* stack_mem_pool size 256 * 8k */ - pool->stack_mem_pool = mem_pool_new (call_stack_t, 8192); + iov.iov_base = iobuf->ptr; + iov.iov_len = iobuf_size(iobuf); - if (!pool->stack_mem_pool) - return -1; + /* Create the xdr payload */ + ret = xdr_serialize_generic(iov, req, xdrproc); + if (ret == -1) { + goto out; + } + iov.iov_len = ret; + count = 1; + } - ctx->stub_mem_pool = mem_pool_new (call_stub_t, 1024); - if (!ctx->stub_mem_pool) - return -1; + if (!rpc) + rpc = global_rpc; + /* Send the msg */ + ret = rpc_clnt_submit(rpc, prog, procnum, cbkfn, &iov, count, NULL, 0, + iobref, frame, NULL, 0, NULL, 0, NULL); + ret = 0; - INIT_LIST_HEAD (&pool->all_frames); - LOCK_INIT (&pool->lock); - ctx->pool = pool; +out: + if (new_iobref) + iobref_unref(iobref); + if (iobuf) + iobuf_unref(iobuf); + return ret; +} - pthread_mutex_init (&(ctx->lock), NULL); +int +cli_rpc_notify(struct rpc_clnt *rpc, void *mydata, rpc_clnt_event_t event, + void *data) +{ + xlator_t *this = NULL; + int ret = 0; - cmd_args = &ctx->cmd_args; + this = mydata; - /* parsing command line arguments */ - cmd_args->log_file = "/dev/stderr"; - cmd_args->log_level = GF_LOG_NONE; + switch (event) { + case RPC_CLNT_CONNECT: { + cli_cmd_broadcast_connected(_gf_true); + gf_log(this->name, GF_LOG_TRACE, "got RPC_CLNT_CONNECT"); + break; + } - INIT_LIST_HEAD (&cmd_args->xlator_options); + case RPC_CLNT_DISCONNECT: { + cli_cmd_broadcast_connected(_gf_false); + gf_log(this->name, GF_LOG_TRACE, "got RPC_CLNT_DISCONNECT"); + if (!global_state->prompt && global_state->await_connected) { + ret = 1; + cli_out( + "Connection failed. Please check if gluster " + "daemon is operational."); + exit(ret); + } + break; + } - lim.rlim_cur = RLIM_INFINITY; - lim.rlim_max = RLIM_INFINITY; - setrlimit (RLIMIT_CORE, &lim); + default: + gf_log(this->name, GF_LOG_TRACE, "got some other RPC event %d", + event); + ret = 0; + break; + } - return 0; + return ret; } - -static int -logging_init (glusterfs_ctx_t *ctx) +static gf_boolean_t +is_valid_int(char *str) { - cmd_args_t *cmd_args = NULL; - - cmd_args = &ctx->cmd_args; - - if (gf_log_init (cmd_args->log_file) == -1) { - fprintf (stderr, - "failed to open logfile %s. exiting\n", - cmd_args->log_file); - return -1; - } - - gf_log_set_loglevel (cmd_args->log_level); - - return 0; + if (*str == '-') + ++str; + + /* Handle empty string or just "-".*/ + if (!*str) + return _gf_false; + + /* Check for non-digit chars in the rest of the string */ + while (*str) { + if (!isdigit(*str)) + return _gf_false; + else + ++str; + } + return _gf_true; } +/* + * ret: 0: option successfully processed + * 1: signalling end of option list + * -1: unknown option + * -2: parsing issue (avoid unknown option error) + */ int -cli_submit_request (void *req, call_frame_t *frame, - rpc_clnt_prog_t *prog, - int procnum, struct iobref *iobref, - cli_serialize_t sfunc, xlator_t *this, - fop_cbk_fn_t cbkfn) +cli_opt_parse(char *opt, struct cli_state *state) { - int ret = -1; - int count = 0; - char start_ping = 0; - struct iovec iov = {0, }; - struct iobuf *iobuf = NULL; - char new_iobref = 0; + char *oarg = NULL; + gf_boolean_t secure_mgmt_tmp = 0; + + if (strcmp(opt, "") == 0) + return 1; + if (strcmp(opt, "help") == 0) { + cli_out( + " peer help - display help for peer commands\n" + " volume help - display help for volume commands\n" + " volume bitrot help - display help for volume" + " bitrot commands\n" + " volume quota help - display help for volume" + " quota commands\n" + " snapshot help - display help for snapshot commands\n" + " global help - list global commands\n"); + exit(0); + } + + if (strcmp(opt, "version") == 0) { + cli_out("%s", argp_program_version); + exit(0); + } + + if (strcmp(opt, "print-logdir") == 0) { + cli_out("%s", DEFAULT_LOG_FILE_DIRECTORY); + exit(0); + } + + if (strcmp(opt, "print-statedumpdir") == 0) { + cli_out("%s", DEFAULT_VAR_RUN_DIRECTORY); + exit(0); + } + + if (strcmp(opt, "xml") == 0) { +#if (HAVE_LIB_XML) + state->mode |= GLUSTER_MODE_XML; +#else + cli_err("XML output not supported. Ignoring '--xml' option"); +#endif + return 0; + } - GF_ASSERT (this); + if (strcmp(opt, "nolog") == 0) { + state->mode |= GLUSTER_MODE_GLFSHEAL_NOLOG; + return 0; + } - iobuf = iobuf_get (this->ctx->iobuf_pool); - if (!iobuf) { - goto out; - }; + if (strcmp(opt, "wignore-partition") == 0) { + state->mode |= GLUSTER_MODE_WIGNORE_PARTITION; + return 0; + } - if (!iobref) { - iobref = iobref_new (); - if (!iobref) { - goto out; - } + if (strcmp(opt, "wignore") == 0) { + state->mode |= GLUSTER_MODE_WIGNORE; + return 0; + } - new_iobref = 1; + oarg = strtail(opt, "mode="); + if (oarg) { + if (strcmp(oarg, "script") == 0) { + state->mode |= GLUSTER_MODE_SCRIPT; + return 0; } - iobref_add (iobref, iobuf); + if (strcmp(oarg, "interactive") == 0) + return 0; - iov.iov_base = iobuf->ptr; - iov.iov_len = 128 * GF_UNIT_KB; + return -1; + } + oarg = strtail(opt, "remote-host="); + if (oarg) { + state->remote_host = oarg; + return 0; + } - /* Create the xdr payload */ - if (req && sfunc) { - ret = sfunc (iov, req); - if (ret == -1) { - goto out; - } - iov.iov_len = ret; - count = 1; - } + oarg = strtail(opt, "inet6"); + if (oarg) { + state->address_family = "inet6"; + return 0; + } - /* Send the msg */ - ret = rpc_clnt_submit (global_rpc, prog, procnum, cbkfn, - &iov, count, - NULL, 0, iobref, frame, NULL, 0, NULL, 0, NULL); - - if (ret == 0) { - pthread_mutex_lock (&global_rpc->conn.lock); - { - if (!global_rpc->conn.ping_started) { - start_ping = 1; - } - } - pthread_mutex_unlock (&global_rpc->conn.lock); + oarg = strtail(opt, "log-file="); + if (oarg) { + state->log_file = oarg; + return 0; + } + oarg = strtail(opt, "timeout="); + if (oarg) { + if (!is_valid_int(oarg) || atoi(oarg) <= 0) { + cli_err("timeout value should be a positive integer"); + return -2; /* -2 instead of -1 to avoid unknown option + error */ } + cli_default_conn_timeout = atoi(oarg); + return 0; + } + + oarg = strtail(opt, "log-level="); + if (oarg) { + int log_level = glusterd_check_log_level(oarg); + if (log_level == -1) + return -1; + state->log_level = (gf_loglevel_t)log_level; + return 0; + } - if (start_ping) - //client_start_ping ((void *) this); - - ret = 0; + oarg = strtail(opt, "glusterd-sock="); + if (oarg) { + state->glusterd_sock = oarg; + return 0; + } + + oarg = strtail(opt, "secure-mgmt="); + if (oarg) { + if (gf_string2boolean(oarg, &secure_mgmt_tmp) == 0) { + if (secure_mgmt_tmp) { + /* See declaration for why this is an int. */ + state->ctx->secure_mgmt = 1; + } + } else { + cli_err("invalid secure-mgmt value (ignored)"); + } + return 0; + } -out: - return ret; + return -1; } int -cli_rpc_notify (struct rpc_clnt *rpc, void *mydata, rpc_clnt_event_t event, - void *data) +parse_cmdline(int argc, char *argv[], struct cli_state *state) { - xlator_t *this = NULL; - int ret = 0; - - this = mydata; - - switch (event) { - case RPC_CLNT_CONNECT: - { - - cli_cmd_broadcast_connected (); - gf_log (this->name, GF_LOG_TRACE, "got RPC_CLNT_CONNECT"); - break; + int ret = 0; + int i = 0; + int j = 0; + char *opt = NULL; + + state->argc = argc - 1; + state->argv = &argv[1]; + + /* Do this first so that an option can override. */ + if (sys_access(SECURE_ACCESS_FILE, F_OK) == 0) { + state->ctx->secure_mgmt = 1; + state->ctx->ssl_cert_depth = glusterfs_read_secure_access_file(); + } + + if (state->argc > GEO_REP_CMD_CONFIG_INDEX && + strtail(state->argv[GEO_REP_CMD_INDEX], "geo") && + strtail(state->argv[GEO_REP_CMD_CONFIG_INDEX], "co")) + goto done; + + for (i = 0; i < state->argc; i++) { + opt = strtail(state->argv[i], "--"); + if (!opt) + continue; + ret = cli_opt_parse(opt, state); + if (ret == -1) { + cli_out("unrecognized option --%s\n", opt); + usage(); + return ret; + } else if (ret == -2) { + return ret; } - - case RPC_CLNT_DISCONNECT: - { - gf_log (this->name, GF_LOG_TRACE, "got RPC_CLNT_DISCONNECT"); - connected = 0; - break; + for (j = i; j < state->argc - 1; j++) + state->argv[j] = state->argv[j + 1]; + state->argc--; + /* argv shifted, next check should be at i again */ + i--; + if (ret == 1) { + /* end of cli options */ + ret = 0; + break; } + } - default: - gf_log (this->name, GF_LOG_TRACE, - "got some other RPC event %d", event); - ret = 0; - break; - } +done: + state->argv[state->argc] = NULL; - return ret; + return ret; } int -parse_cmdline (int argc, char *argv[], struct cli_state *state) +cli_cmd_tree_init(struct cli_cmd_tree *tree) { - int ret = 0; + struct cli_cmd_word *root = NULL; + int ret = 0; - state->argc=argc-1; - state->argv=&argv[1]; + root = &tree->root; + root->tree = tree; - return ret; + return ret; } - int -cli_cmd_tree_init (struct cli_cmd_tree *tree) +cli_state_init(struct cli_state *state) { - struct cli_cmd_word *root = NULL; - int ret = 0; + struct cli_cmd_tree *tree = NULL; + int ret = 0; - root = &tree->root; - root->tree = tree; + state->log_level = GF_LOG_NONE; - return ret; -} + tree = &state->tree; + tree->state = state; + ret = cli_cmd_tree_init(tree); + + return ret; +} int -cli_state_init (struct cli_state *state) +cli_usage_out(const char *usage) { - struct cli_cmd_tree *tree = NULL; - int ret = 0; - - tree = &state->tree; - tree->state = state; + GF_ASSERT(usage); - ret = cli_cmd_tree_init (tree); + if (!usage || usage[0] == '\0') + return -1; - return ret; + cli_err("\nUsage:\n%s\n", usage); + return 0; } - int -cli_out (const char *fmt, ...) +_cli_err(const char *fmt, ...) { - struct cli_state *state = NULL; - va_list ap; - int ret = 0; - - state = global_state; + va_list ap; + int ret = 0; +#ifdef HAVE_READLINE + struct cli_state *state = global_state; +#endif - va_start (ap, fmt); + va_start(ap, fmt); #ifdef HAVE_READLINE - if (state->rl_enabled && !state->rl_processing) - return cli_rl_out(state, fmt, ap); + if (state->rl_enabled && !state->rl_processing) { + ret = cli_rl_err(state, fmt, ap); + va_end(ap); + return ret; + } #endif - ret = vprintf (fmt, ap); - printf ("\n"); + ret = vfprintf(stderr, fmt, ap); + fprintf(stderr, "\n"); + va_end(ap); + + return ret; +} + +int +_cli_out(const char *fmt, ...) +{ + va_list ap; + int ret = 0; +#ifdef HAVE_READLINE + struct cli_state *state = global_state; +#endif + va_start(ap, fmt); +#ifdef HAVE_READLINE + if (state->rl_enabled && !state->rl_processing) { + ret = cli_rl_out(state, fmt, ap); + va_end(ap); return ret; + } +#endif + + ret = vprintf(fmt, ap); + printf("\n"); + va_end(ap); + + return ret; } struct rpc_clnt * -cli_rpc_init (struct cli_state *state) +cli_quotad_clnt_rpc_init(void) { - struct rpc_clnt *rpc = NULL; - struct rpc_clnt_config rpc_cfg = {0,}; - dict_t *options = NULL; - int ret = -1; - int port = CLI_GLUSTERD_PORT; - xlator_t *this = NULL; - - - this = THIS; - cli_rpc_prog = &cli3_1_prog; - options = dict_new (); - if (!options) - goto out; + struct rpc_clnt *rpc = NULL; + dict_t *rpc_opts = NULL; + int ret = -1; + + rpc_opts = dict_new(); + if (!rpc_opts) { + ret = -1; + goto out; + } + + ret = dict_set_str(rpc_opts, "transport.address-family", "unix"); + if (ret) + goto out; + + ret = dict_set_str(rpc_opts, "transport-type", "socket"); + if (ret) + goto out; + + ret = dict_set_str(rpc_opts, "transport.socket.connect-path", + "/var/run/gluster/quotad.socket"); + if (ret) + goto out; + + rpc = cli_quotad_clnt_init(THIS, rpc_opts); + if (!rpc) + goto out; + + global_quotad_rpc = rpc; +out: + if (rpc_opts) { + dict_unref(rpc_opts); + } + return rpc; +} - ret = dict_set_str (options, "remote-host", "localhost"); +struct rpc_clnt * +cli_rpc_init(struct cli_state *state) +{ + struct rpc_clnt *rpc = NULL; + dict_t *options = NULL; + int ret = -1; + int port = CLI_GLUSTERD_PORT; + xlator_t *this = NULL; +#ifdef IPV6_DEFAULT + char *addr_family = "inet6"; +#else + char *addr_family = "inet"; +#endif + + this = THIS; + cli_rpc_prog = &cli_prog; + + options = dict_new(); + if (!options) + goto out; + + /* If address family specified in CLI */ + if (state->address_family) { + addr_family = state->address_family; + } + + /* Connect to glusterd using the specified method, giving preference + * to a unix socket connection. If nothing is specified, connect to + * the default glusterd socket. + */ + if (state->glusterd_sock) { + gf_log("cli", GF_LOG_INFO, + "Connecting to glusterd using " + "sockfile %s", + state->glusterd_sock); + ret = rpc_transport_unix_options_build(options, state->glusterd_sock, + 0); if (ret) - goto out; + goto out; + } else if (state->remote_host) { + gf_log("cli", GF_LOG_INFO, + "Connecting to remote glusterd at " + "%s", + state->remote_host); + + ret = dict_set_str(options, "remote-host", state->remote_host); + if (ret) + goto out; if (state->remote_port) - port = state->remote_port; - - rpc_cfg.remote_host = "localhost"; - rpc_cfg.remote_port = port; + port = state->remote_port; - ret = dict_set_int32 (options, "remote-port", port); + ret = dict_set_int32(options, "remote-port", port); if (ret) - goto out; + goto out; - ret = dict_set_str (options, "transport.address-family", "inet"); + ret = dict_set_str(options, "transport.address-family", addr_family); if (ret) - goto out; + goto out; + } else { + gf_log("cli", GF_LOG_DEBUG, + "Connecting to glusterd using " + "default socket"); + ret = rpc_transport_unix_options_build(options, + DEFAULT_GLUSTERD_SOCKFILE, 0); + if (ret) + goto out; + } - rpc = rpc_clnt_init (&rpc_cfg, options, this->ctx, this->name); + rpc = rpc_clnt_new(options, this, this->name, 16); + if (!rpc) + goto out; - if (rpc) { - ret = rpc_clnt_register_notify (rpc, cli_rpc_notify, this); - } + ret = rpc_clnt_register_notify(rpc, cli_rpc_notify, this); + if (ret) { + gf_log("cli", GF_LOG_ERROR, "failed to register notify"); + goto out; + } + + ret = rpc_clnt_start(rpc); out: - return rpc; + if (options) + dict_unref(options); + + if (ret) { + if (rpc) + rpc_clnt_unref(rpc); + rpc = NULL; + } + return rpc; } cli_local_t * -cli_local_get () +cli_local_get() { - cli_local_t *local = NULL; + cli_local_t *local = NULL; - local = GF_CALLOC (1, sizeof (*local), cli_mt_cli_local_t); + local = GF_CALLOC(1, sizeof(*local), cli_mt_cli_local_t); + LOCK_INIT(&local->lock); + INIT_LIST_HEAD(&local->dict_list); - return local; + return local; } void -cli_local_wipe (cli_local_t *local) +cli_local_wipe(cli_local_t *local) { - if (local) { - GF_FREE (local); - } - - return; + if (local) { + GF_FREE(local->get_vol.volname); + if (local->dict) + dict_unref(local->dict); + GF_FREE(local); + } + + return; } struct cli_state *global_state; int -main (int argc, char *argv[]) +main(int argc, char *argv[]) { - struct cli_state state = {0, }; - int ret = -1; - glusterfs_ctx_t *ctx = NULL; + struct cli_state state = { + 0, + }; + int ret = -1; + glusterfs_ctx_t *ctx = NULL; - ret = glusterfs_globals_init (); - if (ret) - return ret; + mem_pools_init(); - ctx = glusterfs_ctx_get (); - if (!ctx) - return ENOMEM; + ctx = glusterfs_ctx_new(); + if (!ctx) + return ENOMEM; - ret = glusterfs_ctx_defaults_init (ctx); - if (ret) - goto out; +#ifdef DEBUG + gf_mem_acct_enable_set(ctx); +#endif - ret = cli_state_init (&state); - if (ret) - goto out; + ret = glusterfs_globals_init(ctx); + if (ret) + return ret; - global_rpc = cli_rpc_init (&state); - if (!global_rpc) - goto out; + THIS->ctx = ctx; - state.ctx = ctx; - global_state = &state; + ret = glusterfs_ctx_defaults_init(ctx); + if (ret) + goto out; - ret = parse_cmdline (argc, argv, &state); - if (ret) - goto out; + cli_default_conn_timeout = 120; + cli_ten_minutes_timeout = 600; - ret = logging_init (ctx); - if (ret) - goto out; + ret = cli_state_init(&state); + if (ret) + goto out; - ret = cli_cmds_register (&state); - if (ret) - goto out; + state.ctx = ctx; + global_state = &state; - ret = cli_cmd_cond_init (); - if (ret) - goto out; + ret = parse_cmdline(argc, argv, &state); + if (ret) + goto out; - ret = cli_input_init (&state); - if (ret) - goto out; + ret = logging_init(ctx, &state); + if (ret) + goto out; - ret = event_dispatch (ctx->event_pool); + gf_log("cli", GF_LOG_INFO, "Started running %s with version %s", argv[0], + PACKAGE_VERSION); + + global_rpc = cli_rpc_init(&state); + if (!global_rpc) + goto out; + + global_quotad_rpc = cli_quotad_clnt_rpc_init(); + if (!global_quotad_rpc) + goto out; + + ret = cli_cmds_register(&state); + if (ret) + goto out; + + ret = cli_input_init(&state); + if (ret) + goto out; + + ret = gf_event_dispatch(ctx->event_pool); out: -// glusterfs_ctx_destroy (ctx); + // glusterfs_ctx_destroy (ctx); - return ret; + mem_pools_fini(); + + return ret; +} + +void +cli_print_line(int len) +{ + GF_ASSERT(len > 0); + + while (len--) + printf("-"); + + printf("\n"); +} + +void +print_quota_list_header(int type) +{ + if (type == GF_QUOTA_OPTION_TYPE_LIST) { + cli_out( + " Path Hard-limit " + " Soft-limit Used Available Soft-limit " + "exceeded? Hard-limit exceeded?"); + cli_out( + "-----------------------------------------------------" + "-----------------------------------------------------" + "---------------------"); + } else { + cli_out( + " Path Hard-limit " + " Soft-limit Files Dirs Available " + "Soft-limit exceeded? Hard-limit exceeded?"); + cli_out( + "-----------------------------------------------------" + "-----------------------------------------------------" + "-------------------------------------"); + } +} + +void +print_quota_list_empty(char *path, int type) +{ + if (type == GF_QUOTA_OPTION_TYPE_LIST) + cli_out("%-40s %7s %9s %10s %7s %15s %20s", path, "N/A", "N/A", "N/A", + "N/A", "N/A", "N/A"); + else + cli_out("%-40s %9s %9s %12s %10s %10s %15s %20s", path, "N/A", "N/A", + "N/A", "N/A", "N/A", "N/A", "N/A"); } diff --git a/cli/src/cli.h b/cli/src/cli.h index fdf53421f49..c0d933e8f8a 100644 --- a/cli/src/cli.h +++ b/cli/src/cli.h @@ -1,205 +1,516 @@ /* - Copyright (c) 2006-2009 Gluster, Inc. <http://www.gluster.com> + Copyright (c) 2006-2012 Red Hat, Inc. <http://www.redhat.com> This file is part of GlusterFS. - GlusterFS is free software; you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - GlusterFS is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see - <http://www.gnu.org/licenses/>. + 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 __CLI_H__ #define __CLI_H__ -#ifndef _CONFIG_H -#define _CONFIG_H -#include "config.h" -#endif - #include "rpc-clnt.h" -#include "glusterfs.h" +#include <glusterfs/glusterfs.h> #include "protocol-common.h" +#include <glusterfs/logging.h> +#include <glusterfs/quota-common-utils.h> -#define DEFAULT_EVENT_POOL_SIZE 16384 -#define CLI_GLUSTERD_PORT 6969 -#define CLI_DEFAULT_CONN_TIMEOUT 120 -#define CLI_DEFAULT_CMD_TIMEOUT 120 +#include "cli1-xdr.h" +#include "gd-common-utils.h" + +#if (HAVE_LIB_XML) +#include <libxml/encoding.h> +#include <libxml/xmlwriter.h> +#endif + +#define DEFAULT_EVENT_POOL_SIZE 16384 +#define CLI_GLUSTERD_PORT 24007 +#define DEFAULT_CLI_LOG_FILE_DIRECTORY DATADIR "/log/glusterfs" +#define CLI_VOL_STATUS_BRICK_LEN 43 +#define CLI_TAB_LENGTH 8 +#define CLI_BRICK_STATUS_LINE_LEN 78 + +/* Geo-rep command positional arguments' index */ +#define GEO_REP_CMD_INDEX 1 +#define GEO_REP_CMD_CONFIG_INDEX 4 enum argp_option_keys { - ARGP_DEBUG_KEY = 133, - ARGP_PORT_KEY = 'p', + ARGP_DEBUG_KEY = 133, + ARGP_PORT_KEY = 'p', }; +extern int cli_default_conn_timeout; +extern int cli_ten_minutes_timeout; + +typedef enum { + COLD_BRICK_COUNT, + COLD_TYPE, + COLD_DIST_COUNT, + COLD_REPLICA_COUNT, + COLD_ARBITER_COUNT, + COLD_DISPERSE_COUNT, + COLD_REDUNDANCY_COUNT, + HOT_BRICK_COUNT, + HOT_TYPE, + HOT_REPLICA_COUNT, + MAX +} values; + +#define GLUSTER_MODE_SCRIPT (1 << 0) +#define GLUSTER_MODE_ERR_FATAL (1 << 1) +#define GLUSTER_MODE_XML (1 << 2) +#define GLUSTER_MODE_WIGNORE (1 << 3) +#define GLUSTER_MODE_WIGNORE_PARTITION (1 << 4) +#define GLUSTER_MODE_GLFSHEAL_NOLOG (1 << 5) + +#define GLUSTERD_GET_QUOTA_LIST_MOUNT_PATH(abspath, volname, path) \ + do { \ + snprintf(abspath, sizeof(abspath) - 1, \ + DEFAULT_VAR_RUN_DIRECTORY "/%s_quota_list%s", volname, path); \ + } while (0) + struct cli_state; struct cli_cmd_word; struct cli_cmd_tree; +struct cli_cmd; -typedef int (cli_cmd_cbk_t)(struct cli_state *state, - struct cli_cmd_word *word, - const char **words, - int wordcount); -typedef int (cli_cmd_match_t)(struct cli_cmd_word *word); -typedef int (cli_cmd_filler_t)(struct cli_cmd_word *word); +extern char *cli_vol_status_str[]; +extern char *cli_vol_task_status_str[]; + +typedef int(cli_cmd_cbk_t)(struct cli_state *state, struct cli_cmd_word *word, + const char **words, int wordcount); +typedef void(cli_cmd_reg_cbk_t)(struct cli_cmd *this); + +typedef int(cli_cmd_match_t)(struct cli_cmd_word *word); +typedef int(cli_cmd_filler_t)(struct cli_cmd_word *word); struct cli_cmd_word { - struct cli_cmd_tree *tree; - const char *word; - cli_cmd_filler_t *filler; - cli_cmd_match_t *match; - cli_cmd_cbk_t *cbkfn; - const char *desc; - - int nextwords_cnt; - struct cli_cmd_word **nextwords; + struct cli_cmd_tree *tree; + const char *word; + cli_cmd_filler_t *filler; + cli_cmd_match_t *match; + cli_cmd_cbk_t *cbkfn; + const char *desc; + const char *pattern; + int nextwords_cnt; + struct cli_cmd_word **nextwords; }; - struct cli_cmd_tree { - struct cli_state *state; - struct cli_cmd_word root; + struct cli_state *state; + struct cli_cmd_word root; }; - struct cli_state { - int argc; - char **argv; + int argc; + char **argv; + + char debug; - char debug; + /* for events dispatching */ + glusterfs_ctx_t *ctx; - /* for events dispatching */ - glusterfs_ctx_t *ctx; + /* registry of known commands */ + struct cli_cmd_tree tree; - /* registry of known commands */ - struct cli_cmd_tree tree; + /* the thread which "executes" the command in non-interactive mode */ + /* also the thread which reads from stdin in non-readline mode */ + pthread_t input; - /* the thread which "executes" the command in non-interactive mode */ - /* also the thread which reads from stdin in non-readline mode */ - pthread_t input; + /* terminal I/O */ + const char *prompt; + int rl_enabled; + int rl_async; + int rl_processing; - /* terminal I/O */ - const char *prompt; - int rl_enabled; - int rl_async; - int rl_processing; + /* autocompletion state */ + char **matches; + char **matchesp; - /* autocompletion state */ - char **matches; - char **matchesp; + char *remote_host; + int remote_port; + int mode; + int await_connected; - int remote_port; + char *log_file; + gf_loglevel_t log_level; + + char *glusterd_sock; + char *address_family; }; struct cli_local { - union { - struct { - dict_t *dict; - } create_vol; - - struct { - char *volname; - } start_vol; - - struct { - char *volname; - int flags; - } stop_vol; - - struct { - char *volname; - } delete_vol; - - struct { - char *volname; - int cmd; - } defrag_vol; - - struct { - char *volname; - dict_t *dict; - } replace_brick; - - struct { - char *volname; - int flags; - } get_vol; - } u; + struct { + char *volname; + int flags; + } get_vol; + + dict_t *dict; + const char **words; + /* Marker for volume status all */ + gf_boolean_t all; +#if (HAVE_LIB_XML) + xmlTextWriterPtr writer; + xmlDocPtr doc; + int vol_count; +#endif + gf_lock_t lock; + struct list_head dict_list; +}; + +struct cli_volume_status { + int port; + int rdma_port; + int online; + uint64_t block_size; + uint64_t total_inodes; + uint64_t free_inodes; + char *brick; + char *pid_str; + char *free; + char *total; + char *fs_name; + char *mount_options; + char *device; + char *inode_size; }; +struct snap_config_opt_vals_ { + char *op_name; + char *question; +}; + +typedef struct cli_volume_status cli_volume_status_t; + typedef struct cli_local cli_local_t; -typedef ssize_t (*cli_serialize_t) (struct iovec outmsg, void *args); +typedef ssize_t (*cli_serialize_t)(struct iovec outmsg, void *args); extern struct cli_state *global_state; /* use only in readline callback */ -int cli_cmd_register (struct cli_cmd_tree *tree, const char *template, - cli_cmd_cbk_t cbk, const char *desc); -int cli_cmds_register (struct cli_state *state); +extern struct rpc_clnt *global_quotad_rpc; + +extern struct rpc_clnt *global_rpc; + +extern rpc_clnt_prog_t *cli_rpc_prog; -int cli_input_init (struct cli_state *state); +typedef const char *(*cli_selector_t)(void *wcon); -int cli_cmd_process (struct cli_state *state, int argc, char *argv[]); -int cli_cmd_process_line (struct cli_state *state, const char *line); +char * +get_struct_variable(int mem_num, gf_gsync_status_t *sts_val); -int cli_rl_enable (struct cli_state *state); -int cli_rl_out (struct cli_state *state, const char *fmt, va_list ap); +void * +cli_getunamb(const char *tok, void **choices, cli_selector_t sel); + +int +cli_cmd_register(struct cli_cmd_tree *tree, struct cli_cmd *cmd); +int +cli_cmds_register(struct cli_state *state); + +int +cli_input_init(struct cli_state *state); + +int +cli_cmd_process(struct cli_state *state, int argc, char *argv[]); +int +cli_cmd_process_line(struct cli_state *state, const char *line); + +int +cli_rl_enable(struct cli_state *state); +int +cli_rl_out(struct cli_state *state, const char *fmt, va_list ap); +int +cli_rl_err(struct cli_state *state, const char *fmt, va_list ap); + +int +cli_usage_out(const char *usage); -int cli_out (const char *fmt, ...); +int +_cli_out(const char *fmt, ...); +int +_cli_err(const char *fmt, ...); + +#define cli_out(fmt...) \ + do { \ + FMT_WARN(fmt); \ + \ + _cli_out(fmt); \ + \ + } while (0) + +#define cli_err(fmt...) \ + do { \ + FMT_WARN(fmt); \ + \ + _cli_err(fmt); \ + \ + } while (0) + +#define usage() \ + do { \ + cli_out( \ + " Usage: gluster [options] <help> <peer>" \ + " <pool> <volume>\n" \ + " Options:\n" \ + " --help Shows the help information\n" \ + " --version Shows the version\n" \ + " --print-logdir Shows the log directory\n" \ + " --print-statedumpdir Shows the state dump directory\n"); \ + \ + } while (0) int -cli_submit_request (void *req, call_frame_t *frame, - rpc_clnt_prog_t *prog, - int procnum, struct iobref *iobref, - cli_serialize_t sfunc, xlator_t *this, - fop_cbk_fn_t cbkfn); +cli_submit_request(struct rpc_clnt *rpc, void *req, call_frame_t *frame, + rpc_clnt_prog_t *prog, int procnum, struct iobref *iobref, + xlator_t *this, fop_cbk_fn_t cbkfn, xdrproc_t xdrproc); int32_t -cli_cmd_volume_create_parse (const char **words, int wordcount, - dict_t **options); +cli_cmd_volume_create_parse(struct cli_state *state, const char **words, + int wordcount, dict_t **options, char **bricks); int32_t -cli_cmd_volume_set_parse (const char **words, int wordcount, - dict_t **options); +cli_cmd_volume_reset_parse(const char **words, int wordcount, dict_t **opt); int32_t -cli_cmd_volume_add_brick_parse (const char **words, int wordcount, - dict_t **options); +cli_cmd_gsync_set_parse(struct cli_state *state, const char **words, + int wordcount, dict_t **opt, char **errstr); int32_t -cli_cmd_volume_remove_brick_parse (const char **words, int wordcount, - dict_t **options); +cli_cmd_quota_parse(const char **words, int wordcount, dict_t **opt); + +int32_t +cli_cmd_inode_quota_parse(const char **words, int wordcount, dict_t **opt); + +int32_t +cli_cmd_bitrot_parse(const char **words, int wordcount, dict_t **opt); + +int32_t +cli_cmd_volume_set_parse(struct cli_state *state, const char **words, + int wordcount, dict_t **options, char **op_errstr); int32_t -cli_cmd_volume_replace_brick_parse (const char **words, int wordcount, +cli_cmd_ganesha_parse(struct cli_state *state, const char **words, + int wordcount, dict_t **options, char **op_errstr); + +int32_t +cli_cmd_get_state_parse(struct cli_state *state, const char **words, + int wordcount, dict_t **options, char **op_errstr); + +int32_t +cli_cmd_volume_add_brick_parse(struct cli_state *state, const char **words, + int wordcount, dict_t **options, int *type); + +int32_t +cli_cmd_volume_remove_brick_parse(struct cli_state *state, const char **words, + int wordcount, dict_t **options, + int *question, int *brick_count, + int32_t *command); + +int32_t +cli_cmd_volume_replace_brick_parse(const char **words, int wordcount, dict_t **options); int32_t -cli_cmd_log_rotate_parse (const char **words, int wordcount, dict_t **options); +cli_cmd_volume_reset_brick_parse(const char **words, int wordcount, + dict_t **options); + +int32_t +cli_cmd_log_rotate_parse(const char **words, int wordcount, dict_t **options); int32_t -cli_cmd_log_locate_parse (const char **words, int wordcount, dict_t **options); +cli_cmd_log_locate_parse(const char **words, int wordcount, dict_t **options); int32_t -cli_cmd_log_filename_parse (const char **words, int wordcount, dict_t **options); +cli_cmd_log_filename_parse(const char **words, int wordcount, dict_t **options); -cli_local_t * cli_local_get (); +int32_t +cli_cmd_volume_statedump_options_parse(const char **words, int wordcount, + dict_t **options); +int32_t +cli_cmd_volume_clrlks_opts_parse(const char **words, int wordcount, + dict_t **options); + +cli_local_t * +cli_local_get(); void -cli_local_wipe (cli_local_t *local); +cli_local_wipe(cli_local_t *local); + +gf_boolean_t +cli_cmd_connected(); int32_t -cli_cmd_await_connected (); +cli_cmd_await_connected(unsigned timeout); int32_t -cli_cmd_broadcast_connected (); +cli_cmd_broadcast_connected(gf_boolean_t status); + +int +cli_rpc_notify(struct rpc_clnt *rpc, void *mydata, rpc_clnt_event_t event, + void *data); + +int32_t +cli_cmd_volume_profile_parse(const char **words, int wordcount, + dict_t **options); +int32_t +cli_cmd_volume_top_parse(const char **words, int wordcount, dict_t **options); + +int32_t +cli_cmd_log_level_parse(const char **words, int wordcount, dict_t **options); + +int32_t +cli_cmd_volume_status_parse(const char **words, int wordcount, + dict_t **options); + +int +cli_cmd_volume_heal_options_parse(const char **words, int wordcount, + dict_t **options); + +int +cli_cmd_volume_defrag_parse(const char **words, int wordcount, + dict_t **options); + +int +cli_print_brick_status(cli_volume_status_t *status); + +void +cli_print_detailed_status(cli_volume_status_t *status); + +int +cli_get_detail_status(dict_t *dict, int i, cli_volume_status_t *status); + +void +cli_print_line(int len); + +int +cli_xml_output_str(char *op, char *str, int op_ret, int op_errno, + char *op_errstr); + +int +cli_xml_output_dict(char *op, dict_t *dict, int op_ret, int op_errno, + char *op_errstr); + +int +cli_xml_output_vol_top(dict_t *dict, int op_ret, int op_errno, char *op_errstr); + +int +cli_xml_output_vol_profile(dict_t *dict, int op_ret, int op_errno, + char *op_errstr); + +int +cli_xml_output_vol_status_begin(cli_local_t *local, int op_ret, int op_errno, + char *op_errstr); + +int +cli_xml_output_vol_status_end(cli_local_t *local); + +int +cli_xml_output_vol_status(cli_local_t *local, dict_t *dict); + +int +cli_xml_output_vol_list(dict_t *dict, int op_ret, int op_errno, + char *op_errstr); + +int +cli_xml_output_vol_info_begin(cli_local_t *local, int op_ret, int op_errno, + char *op_errstr); + +int +cli_xml_output_vol_info_end(cli_local_t *local); + +int +cli_xml_output_vol_info(cli_local_t *local, dict_t *dict); + +int +cli_xml_output_vol_quota_limit_list_begin(cli_local_t *local, int op_ret, + int op_errno, char *op_errstr); +int +cli_xml_output_vol_quota_limit_list_end(cli_local_t *local); + +int +cli_quota_list_xml_error(cli_local_t *local, char *path, char *errstr); + +int +cli_quota_xml_output(cli_local_t *local, char *path, int64_t hl_str, + char *sl_final, int64_t sl_num, int64_t used, + int64_t avail, char *sl, char *hl, gf_boolean_t limit_set); + +int +cli_quota_object_xml_output(cli_local_t *local, char *path, char *sl_str, + int64_t sl_val, quota_limits_t *limits, + quota_meta_t *used_space, int64_t avail, char *sl, + char *hl, gf_boolean_t limit_set); + +int +cli_xml_output_peer_status(dict_t *dict, int op_ret, int op_errno, + char *op_errstr); + +int +cli_xml_output_vol_rebalance(gf_cli_defrag_type op, dict_t *dict, int op_ret, + int op_errno, char *op_errstr); + +int +cli_xml_output_vol_remove_brick(gf_boolean_t status_op, dict_t *dict, + int op_ret, int op_errno, char *op_errstr, + const char *op); + +int +cli_xml_output_vol_replace_brick(dict_t *dict, int op_ret, int op_errno, + char *op_errstr); + +int +cli_xml_output_vol_create(dict_t *dict, int op_ret, int op_errno, + char *op_errstr); + +int +cli_xml_output_generic_volume(char *op, dict_t *dict, int op_ret, int op_errno, + char *op_errstr); + +int +cli_xml_output_vol_gsync(dict_t *dict, int op_ret, int op_errno, + char *op_errstr); +int +cli_xml_output_vol_status_tasks_detail(cli_local_t *local, dict_t *dict); + +int +cli_xml_snapshot_delete(cli_local_t *local, dict_t *dict, gf_cli_rsp *rsp); + +int +cli_xml_snapshot_begin_composite_op(cli_local_t *local); + +int +cli_xml_snapshot_end_composite_op(cli_local_t *local); + +int +cli_xml_output_snap_delete_begin(cli_local_t *local, int op_ret, int op_errno, + char *op_errstr); +int +cli_xml_output_snap_delete_end(cli_local_t *local); + +int +cli_xml_output_snap_status_begin(cli_local_t *local, int op_ret, int op_errno, + char *op_errstr); +int +cli_xml_output_snap_status_end(cli_local_t *local); +int +cli_xml_output_snapshot(int cmd_type, dict_t *dict, int op_ret, int op_errno, + char *op_errstr); +int +cli_xml_snapshot_status_single_snap(cli_local_t *local, dict_t *dict, + char *key); +int32_t +cli_cmd_snapshot_parse(const char **words, int wordcount, dict_t **options, + struct cli_state *state); + +int +cli_xml_output_vol_getopts(dict_t *dict, int op_ret, int op_errno, + char *op_errstr); + +void +print_quota_list_header(int type); + +void +print_quota_list_empty(char *path, int type); int -cli_rpc_notify (struct rpc_clnt *rpc, void *mydata, rpc_clnt_event_t event, - void *data); +gf_gsync_status_t_comparator(const void *p, const void *q); #endif /* __CLI_H__ */ diff --git a/cli/src/cli3_1-cops.c b/cli/src/cli3_1-cops.c deleted file mode 100644 index c9dbb5ee35a..00000000000 --- a/cli/src/cli3_1-cops.c +++ /dev/null @@ -1,1786 +0,0 @@ -/* - Copyright (c) 2010 Gluster, Inc. <http://www.gluster.com> - This file is part of GlusterFS. - - GlusterFS is free software; you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - GlusterFS is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see - <http://www.gnu.org/licenses/>. -*/ - - -#ifndef _CONFIG_H -#define _CONFIG_H -#include "config.h" -#endif - -#include "cli.h" -#include "compat-errno.h" -#include "cli-cmd.h" -#include <sys/uio.h> - -#include "cli1-xdr.h" -#include "cli1.h" -#include "protocol-common.h" -#include "cli-mem-types.h" -#include "compat.h" - -extern rpc_clnt_prog_t *cli_rpc_prog; -extern int cli_op_ret; - -char *cli_volume_type[] = {"None", - "Stripe", - "Replicate" -}; - - -char *cli_volume_status[] = {"Created", - "Started", - "Stopped" -}; - -int32_t -gf_cli3_1_get_volume (call_frame_t *frame, xlator_t *this, - void *data); - -int -gf_cli3_1_probe_cbk (struct rpc_req *req, struct iovec *iov, - int count, void *myframe) -{ - gf1_cli_probe_rsp rsp = {0,}; - int ret = 0; - - if (-1 == req->rpc_status) { - goto out; - } - - ret = gf_xdr_to_cli_probe_rsp (*iov, &rsp); - if (ret < 0) { - gf_log ("", GF_LOG_ERROR, "error"); - //rsp.op_ret = -1; - //rsp.op_errno = EINVAL; - goto out; - } - - gf_log ("cli", GF_LOG_NORMAL, "Received resp to probe"); - if (!rsp.op_ret) { - switch (rsp.op_errno) { - case GF_PROBE_SUCCESS: - cli_out ("Probe successful"); - break; - case GF_PROBE_LOCALHOST: - cli_out ("Probe on localhost not needed"); - break; - case GF_PROBE_FRIEND: - cli_out ("Probe on host %s port %d already" - " a friend",rsp.hostname, rsp.port); - break; - default: - cli_out ("Probe returned with unknown errno %d", - rsp.op_errno); - break; - } - } - if (rsp.op_ret) { - cli_out ("Probe unsuccessfull"); - gf_log ("glusterd",GF_LOG_ERROR,"Probe failed with op_ret %d" - " and op_errno %d", rsp.op_ret, rsp.op_errno); - } - ret = rsp.op_ret; - -out: - cli_cmd_broadcast_response (ret); - return ret; -} - -int -gf_cli3_1_deprobe_cbk (struct rpc_req *req, struct iovec *iov, - int count, void *myframe) -{ - gf1_cli_deprobe_rsp rsp = {0,}; - int ret = 0; - - if (-1 == req->rpc_status) { - goto out; - } - - ret = gf_xdr_to_cli_deprobe_rsp (*iov, &rsp); - if (ret < 0) { - gf_log ("", GF_LOG_ERROR, "error"); - //rsp.op_ret = -1; - //rsp.op_errno = EINVAL; - goto out; - } - - gf_log ("cli", GF_LOG_NORMAL, "Received resp to deprobe"); - cli_out ("Detach %s", (rsp.op_ret) ? "unsuccessful": "successful"); - - - ret = rsp.op_ret; - -out: - cli_cmd_broadcast_response (ret); - return ret; -} - -int -gf_cli3_1_list_friends_cbk (struct rpc_req *req, struct iovec *iov, - int count, void *myframe) -{ - gf1_cli_peer_list_rsp rsp = {0,}; - int ret = 0; - dict_t *dict = NULL; - char *uuid_buf = NULL; - char *hostname_buf = NULL; - int32_t i = 1; - char key[256] = {0,}; - int32_t state = 0; - int32_t port = 0; - - if (-1 == req->rpc_status) { - goto out; - } - - ret = gf_xdr_to_cli_peer_list_rsp (*iov, &rsp); - if (ret < 0) { - gf_log ("", GF_LOG_ERROR, "error"); - //rsp.op_ret = -1; - //rsp.op_errno = EINVAL; - goto out; - } - - - gf_log ("cli", GF_LOG_NORMAL, "Received resp to list: %d", - rsp.op_ret); - - ret = rsp.op_ret; - - if (!rsp.op_ret) { - - if (!rsp.friends.friends_len) { - cli_out ("No peers present"); - ret = 0; - goto out; - } - - dict = dict_new (); - - if (!dict) { - ret = -1; - goto out; - } - - ret = dict_unserialize (rsp.friends.friends_val, - rsp.friends.friends_len, - &dict); - - if (ret) { - gf_log ("", GF_LOG_ERROR, - "Unable to allocate memory"); - goto out; - } - - ret = dict_get_int32 (dict, "count", &count); - - if (ret) { - goto out; - } - - cli_out ("Number of Peers: %d", count); - - while ( i <= count) { - snprintf (key, 256, "friend%d.uuid", i); - ret = dict_get_str (dict, key, &uuid_buf); - if (ret) - goto out; - - snprintf (key, 256, "friend%d.hostname", i); - ret = dict_get_str (dict, key, &hostname_buf); - if (ret) - goto out; - - snprintf (key, 256, "friend%d.port", i); - ret = dict_get_int32 (dict, key, &port); - if (ret) - goto out; - - snprintf (key, 256, "friend%d.state", i); - ret = dict_get_int32 (dict, key, &state); - if (ret) - goto out; - - cli_out ("hostname:%s, port:%d, uuid:%s, state:%d", - hostname_buf, port, uuid_buf, state); - i++; - } - } else { - ret = -1; - goto out; - } - - - ret = 0; - -out: - cli_cmd_broadcast_response (ret); - if (ret) - cli_out ("Command Execution Failed"); - - if (dict) - dict_destroy (dict); - - return ret; -} - -int -gf_cli3_1_get_volume_cbk (struct rpc_req *req, struct iovec *iov, - int count, void *myframe) -{ - gf1_cli_get_vol_rsp rsp = {0,}; - int ret = 0; - dict_t *dict = NULL; - char *volname = NULL; - int32_t i = 0; - char key[1024] = {0,}; - int32_t status = 0; - int32_t type = 0; - int32_t brick_count = 0; - char *brick = NULL; - int32_t j = 1; - cli_local_t *local = NULL; - - - if (-1 == req->rpc_status) { - goto out; - } - - ret = gf_xdr_to_cli_get_vol_rsp (*iov, &rsp); - if (ret < 0) { - gf_log ("", GF_LOG_ERROR, "error"); - //rsp.op_ret = -1; - //rsp.op_errno = EINVAL; - goto out; - } - - - gf_log ("cli", GF_LOG_NORMAL, "Received resp to get vol: %d", - rsp.op_ret); - - if (!rsp.op_ret) { - - if (!rsp.volumes.volumes_len) { - cli_out ("No volumes present"); - ret = 0; - goto out; - } - - dict = dict_new (); - - if (!dict) { - ret = -1; - goto out; - } - - ret = dict_unserialize (rsp.volumes.volumes_val, - rsp.volumes.volumes_len, - &dict); - - if (ret) { - gf_log ("", GF_LOG_ERROR, - "Unable to allocate memory"); - goto out; - } - - ret = dict_get_int32 (dict, "count", &count); - - if (ret) { - goto out; - } - - local = ((call_frame_t *)myframe)->local; - //cli_out ("Number of Volumes: %d", count); - - if (!count && (local->u.get_vol.flags == - GF_CLI_GET_NEXT_VOLUME)) { - local->u.get_vol.volname = NULL; - ret = 0; - goto out; - } else if (!count && (local->u.get_vol.flags == - GF_CLI_GET_VOLUME)) { - cli_out ("Volume %s not present", - local->u.get_vol.volname); - ret = 0; - goto out; - } - - while ( i < count) { - cli_out (""); - snprintf (key, 256, "volume%d.name", i); - ret = dict_get_str (dict, key, &volname); - if (ret) - goto out; - - snprintf (key, 256, "volume%d.type", i); - ret = dict_get_int32 (dict, key, &type); - if (ret) - goto out; - - snprintf (key, 256, "volume%d.status", i); - ret = dict_get_int32 (dict, key, &status); - if (ret) - goto out; - - snprintf (key, 256, "volume%d.brick_count", i); - ret = dict_get_int32 (dict, key, &brick_count); - if (ret) - goto out; - - - cli_out ("Volume Name: %s", volname); - cli_out ("Type: %s", cli_volume_type[type]); - cli_out ("Status: %s", cli_volume_status[status], brick_count); - cli_out ("Number of Bricks: %d", brick_count); - j = 1; - - - GF_FREE (local->u.get_vol.volname); - local->u.get_vol.volname = gf_strdup (volname); - - if (brick_count) - cli_out ("Bricks:"); - - while ( j <= brick_count) { - snprintf (key, 1024, "volume%d.brick%d", - i, j); - ret = dict_get_str (dict, key, &brick); - if (ret) - goto out; - cli_out ("Brick%d: %s", j, brick); - j++; - } - i++; - } - - - } else { - ret = -1; - goto out; - } - - - ret = 0; - -out: - cli_cmd_broadcast_response (ret); - if (ret) - cli_out ("Command Execution Failed"); - - if (dict) - dict_destroy (dict); - - gf_log ("", GF_LOG_NORMAL, "Returning: %d", ret); - return ret; -} - -int -gf_cli3_1_create_volume_cbk (struct rpc_req *req, struct iovec *iov, - int count, void *myframe) -{ - gf1_cli_create_vol_rsp rsp = {0,}; - int ret = 0; - cli_local_t *local = NULL; - char *volname = NULL; - dict_t *dict = NULL; - - if (-1 == req->rpc_status) { - goto out; - } - - ret = gf_xdr_to_cli_create_vol_rsp (*iov, &rsp); - if (ret < 0) { - gf_log ("", GF_LOG_ERROR, "error"); - goto out; - } - - local = ((call_frame_t *) (myframe))->local; - - dict = local->u.create_vol.dict; - - ret = dict_get_str (dict, "volname", &volname); - - gf_log ("cli", GF_LOG_NORMAL, "Received resp to create volume"); - cli_out ("Creation of volume %s has been %s", volname, - (rsp.op_ret) ? "unsuccessful": "successful"); - if (rsp.op_ret && rsp.op_errstr) - cli_out ("%s", rsp.op_errstr); - - ret = rsp.op_ret; - -out: - cli_cmd_broadcast_response (ret); - return ret; -} - -int -gf_cli3_1_delete_volume_cbk (struct rpc_req *req, struct iovec *iov, - int count, void *myframe) -{ - gf1_cli_delete_vol_rsp rsp = {0,}; - int ret = 0; - cli_local_t *local = NULL; - char *volname = NULL; - call_frame_t *frame = NULL; - - if (-1 == req->rpc_status) { - goto out; - } - - ret = gf_xdr_to_cli_delete_vol_rsp (*iov, &rsp); - if (ret < 0) { - gf_log ("", GF_LOG_ERROR, "error"); - goto out; - } - - frame = myframe; - local = frame->local; - - if (local) - volname = local->u.delete_vol.volname; - - - gf_log ("cli", GF_LOG_NORMAL, "Received resp to delete volume"); - cli_out ("Deleting volume %s has been %s", volname, - (rsp.op_ret) ? "unsuccessful": "successful"); - - ret = rsp.op_ret; - -out: - cli_cmd_broadcast_response (ret); - gf_log ("", GF_LOG_NORMAL, "Returning with %d", ret); - return ret; -} - -int -gf_cli3_1_start_volume_cbk (struct rpc_req *req, struct iovec *iov, - int count, void *myframe) -{ - gf1_cli_start_vol_rsp rsp = {0,}; - int ret = 0; - cli_local_t *local = NULL; - char *volname = NULL; - call_frame_t *frame = NULL; - - if (-1 == req->rpc_status) { - goto out; - } - - ret = gf_xdr_to_cli_start_vol_rsp (*iov, &rsp); - if (ret < 0) { - gf_log ("", GF_LOG_ERROR, "error"); - goto out; - } - - frame = myframe; - - if (frame) - local = frame->local; - - if (local) - volname = local->u.start_vol.volname; - - gf_log ("cli", GF_LOG_NORMAL, "Received resp to start volume"); - cli_out ("Starting volume %s has been %s", volname, - (rsp.op_ret) ? "unsuccessful": "successful"); - - ret = rsp.op_ret; - -out: - cli_cmd_broadcast_response (ret); - return ret; -} - -int -gf_cli3_1_stop_volume_cbk (struct rpc_req *req, struct iovec *iov, - int count, void *myframe) -{ - gf1_cli_stop_vol_rsp rsp = {0,}; - int ret = 0; - cli_local_t *local = NULL; - char *volname = NULL; - call_frame_t *frame = NULL; - - if (-1 == req->rpc_status) { - goto out; - } - - ret = gf_xdr_to_cli_stop_vol_rsp (*iov, &rsp); - if (ret < 0) { - gf_log ("", GF_LOG_ERROR, "error"); - goto out; - } - - frame = myframe; - - if (frame) - local = frame->local; - - if (local) - volname = local->u.start_vol.volname; - - gf_log ("cli", GF_LOG_NORMAL, "Received resp to stop volume"); - cli_out ("Stopping volume %s has been %s", volname, - (rsp.op_ret) ? "unsuccessful": "successful"); - - ret = rsp.op_ret; - -out: - cli_cmd_broadcast_response (ret); - return ret; -} - -int -gf_cli3_1_defrag_volume_cbk (struct rpc_req *req, struct iovec *iov, - int count, void *myframe) -{ - gf1_cli_defrag_vol_rsp rsp = {0,}; - cli_local_t *local = NULL; - char *volname = NULL; - call_frame_t *frame = NULL; - int cmd = 0; - int ret = 0; - - if (-1 == req->rpc_status) { - goto out; - } - - ret = gf_xdr_to_cli_defrag_vol_rsp (*iov, &rsp); - if (ret < 0) { - gf_log ("", GF_LOG_ERROR, "error"); - goto out; - } - - frame = myframe; - - if (frame) - local = frame->local; - - if (local) { - volname = local->u.defrag_vol.volname; - cmd = local->u.defrag_vol.cmd; - } - if (cmd == GF_DEFRAG_CMD_START) { - cli_out ("starting defrag on volume %s has been %s", volname, - (rsp.op_ret) ? "unsuccessful": "successful"); - } - if (cmd == GF_DEFRAG_CMD_STOP) { - if (rsp.op_ret == -1) - cli_out ("'defrag volume %s stop' failed", volname); - else - cli_out ("stopped defrag process of volume %s \n" - "(after rebalancing %"PRId64" files totaling " - "%"PRId64" bytes)", volname, rsp.files, rsp.size); - } - if (cmd == GF_DEFRAG_CMD_STATUS) { - if (rsp.op_ret == -1) - cli_out ("failed to get the status of defrag process"); - else { - char *status = "unknown"; - if (rsp.op_errno == 0) - status = "not started"; - if (rsp.op_errno == 1) - status = "in progress"; - if (rsp.op_errno == 2) - status = "stopped"; - if (rsp.op_errno == 3) - status = "completed"; - if (rsp.op_errno == 4) - status = "failed"; - - cli_out ("rebalance %s: rebalanced %"PRId64" files of size %"PRId64 - " (total files scanned %"PRId64")", status, - rsp.files, rsp.size, rsp.lookedup_files); - } - } - - if (volname) - GF_FREE (volname); - - ret = rsp.op_ret; - -out: - cli_cmd_broadcast_response (ret); - return ret; -} - -int -gf_cli3_1_rename_volume_cbk (struct rpc_req *req, struct iovec *iov, - int count, void *myframe) -{ - gf1_cli_rename_vol_rsp rsp = {0,}; - int ret = 0; - - if (-1 == req->rpc_status) { - goto out; - } - - ret = gf_xdr_to_cli_rename_vol_rsp (*iov, &rsp); - if (ret < 0) { - gf_log ("", GF_LOG_ERROR, "error"); - goto out; - } - - - gf_log ("cli", GF_LOG_NORMAL, "Received resp to probe"); - cli_out ("Rename volume %s", (rsp.op_ret) ? "unsuccessful": - "successful"); - - ret = rsp.op_ret; - -out: - cli_cmd_broadcast_response (ret); - return ret; -} - -int -gf_cli3_1_set_volume_cbk (struct rpc_req *req, struct iovec *iov, - int count, void *myframe) -{ - gf1_cli_set_vol_rsp rsp = {0,}; - int ret = 0; - - if (-1 == req->rpc_status) { - goto out; - } - - ret = gf_xdr_to_cli_set_vol_rsp (*iov, &rsp); - if (ret < 0) { - gf_log ("", GF_LOG_ERROR, "error"); - goto out; - } - - - gf_log ("cli", GF_LOG_NORMAL, "Received resp to set"); - cli_out ("Set volume %s", (rsp.op_ret) ? "unsuccessful": - "successful"); - - ret = rsp.op_ret; - -out: - cli_cmd_broadcast_response (ret); - return ret; -} - -int -gf_cli3_1_add_brick_cbk (struct rpc_req *req, struct iovec *iov, - int count, void *myframe) -{ - gf1_cli_add_brick_rsp rsp = {0,}; - int ret = 0; - - if (-1 == req->rpc_status) { - goto out; - } - - ret = gf_xdr_to_cli_add_brick_rsp (*iov, &rsp); - if (ret < 0) { - gf_log ("", GF_LOG_ERROR, "error"); - goto out; - } - - - gf_log ("cli", GF_LOG_NORMAL, "Received resp to add brick"); - cli_out ("Add Brick %s", (rsp.op_ret) ? "unsuccessful": - "successful"); - - if (rsp.op_ret && rsp.op_errstr) - cli_out ("%s", rsp.op_errstr); - ret = rsp.op_ret; - -out: - cli_cmd_broadcast_response (ret); - return ret; -} - - -int -gf_cli3_1_remove_brick_cbk (struct rpc_req *req, struct iovec *iov, - int count, void *myframe) -{ - gf1_cli_remove_brick_rsp rsp = {0,}; - int ret = 0; - - if (-1 == req->rpc_status) { - goto out; - } - - ret = gf_xdr_to_cli_remove_brick_rsp (*iov, &rsp); - if (ret < 0) { - gf_log ("", GF_LOG_ERROR, "error"); - goto out; - } - - gf_log ("cli", GF_LOG_NORMAL, "Received resp to remove brick"); - cli_out ("Remove Brick %s", (rsp.op_ret) ? "unsuccessful": - "successful"); - if (rsp.op_ret && rsp.op_errstr) - cli_out ("%s", rsp.op_errstr); - - ret = rsp.op_ret; - -out: - cli_cmd_broadcast_response (ret); - return ret; -} - - - -int -gf_cli3_1_replace_brick_cbk (struct rpc_req *req, struct iovec *iov, - int count, void *myframe) -{ - gf1_cli_replace_brick_rsp rsp = {0,}; - int ret = 0; - cli_local_t *local = NULL; - call_frame_t *frame = NULL; - dict_t *dict = NULL; - char *src_brick = NULL; - char *dst_brick = NULL; - char *status_reply = NULL; - gf1_cli_replace_op replace_op = 0; - char *rb_operation_str = NULL; - char cmd_str[8192] = {0,}; - - if (-1 == req->rpc_status) { - goto out; - } - - frame = (call_frame_t *) myframe; - - ret = gf_xdr_to_cli_replace_brick_rsp (*iov, &rsp); - if (ret < 0) { - gf_log ("", GF_LOG_ERROR, "error"); - goto out; - } - - local = frame->local; - GF_ASSERT (local); - dict = local->u.replace_brick.dict; - - ret = dict_get_int32 (dict, "operation", (int32_t *)&replace_op); - if (ret) { - gf_log ("", GF_LOG_DEBUG, - "dict_get on operation failed"); - goto out; - } - - switch (replace_op) { - case GF_REPLACE_OP_START: - if (rsp.op_ret) - rb_operation_str = "replace-brick failed to start"; - else - rb_operation_str = "replace-brick started successfully"; - break; - - case GF_REPLACE_OP_STATUS: - - status_reply = rsp.status; - if (rsp.op_ret || ret) - rb_operation_str = "replace-brick status unknown"; - else - rb_operation_str = status_reply; - - break; - - case GF_REPLACE_OP_PAUSE: - if (rsp.op_ret) - rb_operation_str = "replace-brick pause failed"; - else - rb_operation_str = "replace-brick paused successfully"; - break; - - case GF_REPLACE_OP_ABORT: - if (rsp.op_ret) - rb_operation_str = "replace-brick abort failed"; - else - rb_operation_str = "replace-brick aborted successfully"; - break; - - case GF_REPLACE_OP_COMMIT: - ret = dict_get_str (dict, "src-brick", &src_brick); - if (ret) { - gf_log ("", GF_LOG_DEBUG, - "dict_get on src-brick failed"); - goto out; - } - - ret = dict_get_str (dict, "dst-brick", &dst_brick); - if (ret) { - gf_log ("", GF_LOG_DEBUG, - "dict_get on dst-brick failed"); - goto out; - } - - snprintf (cmd_str, 4096, "gluster volume replace-brick %s %s %s abort >/dev/null", - local->u.replace_brick.volname, src_brick, dst_brick); - - ret = system (cmd_str); - if (ret) { - gf_log ("", GF_LOG_DEBUG, - "add brick failed"); - goto out; - } - - snprintf (cmd_str, 4096, "gluster volume add-brick %s %s >/dev/null", - local->u.replace_brick.volname, dst_brick); - - ret = system (cmd_str); - if (ret) { - gf_log ("", GF_LOG_DEBUG, - "add brick failed"); - goto out; - } - - snprintf (cmd_str, 4096, "gluster volume remove-brick %s %s >/dev/null", - local->u.replace_brick.volname, src_brick); - - ret = system (cmd_str); - if (ret) { - gf_log ("", GF_LOG_DEBUG, - "remove brick failed"); - goto out; - } - - if (rsp.op_ret || ret) - rb_operation_str = "replace-brick commit failed"; - else - rb_operation_str = "replace-brick commit successful"; - - break; - - default: - gf_log ("", GF_LOG_DEBUG, - "Unknown operation"); - break; - } - - - gf_log ("cli", GF_LOG_NORMAL, "Received resp to replace brick"); - cli_out ("%s", - rb_operation_str ? rb_operation_str : "Unknown operation"); - - ret = rsp.op_ret; - -out: - if (local) { - dict_unref (local->u.replace_brick.dict); - GF_FREE (local->u.replace_brick.volname); - cli_local_wipe (local); - } - - cli_cmd_broadcast_response (ret); - return ret; -} - -static int -gf_cli3_1_log_filename_cbk (struct rpc_req *req, struct iovec *iov, - int count, void *myframe) -{ - gf1_cli_log_filename_rsp rsp = {0,}; - int ret = -1; - - if (-1 == req->rpc_status) { - goto out; - } - - ret = gf_xdr_to_cli_log_filename_rsp (*iov, &rsp); - if (ret < 0) { - gf_log ("", GF_LOG_ERROR, "error"); - goto out; - } - - gf_log ("cli", GF_LOG_DEBUG, "Received resp to log filename"); - cli_out ("log filename : %s", - (rsp.op_ret) ? "unsuccessful": "successful"); - - ret = rsp.op_ret; - -out: - cli_cmd_broadcast_response (ret); - return ret; -} - -static int -gf_cli3_1_log_locate_cbk (struct rpc_req *req, struct iovec *iov, - int count, void *myframe) -{ - gf1_cli_log_locate_rsp rsp = {0,}; - int ret = -1; - - if (-1 == req->rpc_status) { - goto out; - } - - ret = gf_xdr_to_cli_log_locate_rsp (*iov, &rsp); - if (ret < 0) { - gf_log ("", GF_LOG_ERROR, "error"); - goto out; - } - - gf_log ("cli", GF_LOG_DEBUG, "Received resp to log locate"); - cli_out ("log file location: %s", rsp.path); - - ret = rsp.op_ret; - -out: - cli_cmd_broadcast_response (ret); - return ret; -} - -static int -gf_cli3_1_log_rotate_cbk (struct rpc_req *req, struct iovec *iov, - int count, void *myframe) -{ - gf1_cli_log_rotate_rsp rsp = {0,}; - int ret = -1; - - if (-1 == req->rpc_status) { - goto out; - } - - ret = gf_xdr_to_cli_log_rotate_rsp (*iov, &rsp); - if (ret < 0) { - gf_log ("", GF_LOG_ERROR, "error"); - goto out; - } - - gf_log ("cli", GF_LOG_DEBUG, "Received resp to log rotate"); - cli_out ("log rotate %s", (rsp.op_ret) ? "unsuccessful": "successful"); - - ret = rsp.op_ret; - -out: - cli_cmd_broadcast_response (ret); - return ret; -} - - - -int32_t -gf_cli3_1_probe (call_frame_t *frame, xlator_t *this, - void *data) -{ - gf1_cli_probe_req req = {0,}; - int ret = 0; - dict_t *dict = NULL; - char *hostname = NULL; - int port = 0; - - if (!frame || !this || !data) { - ret = -1; - goto out; - } - - dict = data; - ret = dict_get_str (dict, "hostname", &hostname); - if (ret) - goto out; - - ret = dict_get_int32 (dict, "port", &port); - if (ret) - port = CLI_GLUSTERD_PORT; - - req.hostname = hostname; - req.port = port; - - ret = cli_cmd_submit (&req, frame, cli_rpc_prog, - GD_MGMT_CLI_PROBE, NULL, gf_xdr_from_cli_probe_req, - this, gf_cli3_1_probe_cbk); - -out: - gf_log ("cli", GF_LOG_DEBUG, "Returning %d", ret); - return ret; -} - -int32_t -gf_cli3_1_deprobe (call_frame_t *frame, xlator_t *this, - void *data) -{ - gf1_cli_deprobe_req req = {0,}; - int ret = 0; - dict_t *dict = NULL; - char *hostname = NULL; - int port = 0; - - if (!frame || !this || !data) { - ret = -1; - goto out; - } - - dict = data; - ret = dict_get_str (dict, "hostname", &hostname); - if (ret) - goto out; - - ret = dict_get_int32 (dict, "port", &port); - if (ret) - port = CLI_GLUSTERD_PORT; - - req.hostname = hostname; - req.port = port; - - ret = cli_cmd_submit (&req, frame, cli_rpc_prog, - GD_MGMT_CLI_DEPROBE, NULL, - gf_xdr_from_cli_deprobe_req, - this, gf_cli3_1_deprobe_cbk); - -out: - gf_log ("cli", GF_LOG_DEBUG, "Returning %d", ret); - return ret; -} - -int32_t -gf_cli3_1_list_friends (call_frame_t *frame, xlator_t *this, - void *data) -{ - gf1_cli_peer_list_req req = {0,}; - int ret = 0; - - if (!frame || !this) { - ret = -1; - goto out; - } - - req.flags = GF_CLI_LIST_ALL; - - ret = cli_cmd_submit (&req, frame, cli_rpc_prog, - GD_MGMT_CLI_LIST_FRIENDS, NULL, - gf_xdr_from_cli_peer_list_req, - this, gf_cli3_1_list_friends_cbk); - -out: - gf_log ("cli", GF_LOG_DEBUG, "Returning %d", ret); - return ret; -} - -int32_t -gf_cli3_1_get_next_volume (call_frame_t *frame, xlator_t *this, - void *data) -{ - - int ret = 0; - cli_cmd_volume_get_ctx_t *ctx = NULL; - cli_local_t *local = NULL; - - if (!frame || !this || !data) { - ret = -1; - goto out; - } - - ctx = data; - - ret = gf_cli3_1_get_volume (frame, this, data); - - local = frame->local; - - if (!local || !local->u.get_vol.volname) { - cli_out ("No volumes present"); - goto out; - } - - ctx->volname = local->u.get_vol.volname; - - while (ctx->volname) { - ret = gf_cli3_1_get_volume (frame, this, ctx); - if (ret) - goto out; - ctx->volname = local->u.get_vol.volname; - } - -out: - return ret; -} - -int32_t -gf_cli3_1_get_volume (call_frame_t *frame, xlator_t *this, - void *data) -{ - gf1_cli_get_vol_req req = {0,}; - int ret = 0; - cli_cmd_volume_get_ctx_t *ctx = NULL; - dict_t *dict = NULL; - - if (!frame || !this || !data) { - ret = -1; - goto out; - } - - ctx = data; - req.flags = ctx->flags; - - dict = dict_new (); - if (!dict) - goto out; - - if (ctx->volname) { - ret = dict_set_str (dict, "volname", ctx->volname); - if (ret) - goto out; - } - - ret = dict_allocate_and_serialize (dict, - &req.dict.dict_val, - (size_t *)&req.dict.dict_len); - - ret = cli_cmd_submit (&req, frame, cli_rpc_prog, - GD_MGMT_CLI_GET_VOLUME, NULL, - gf_xdr_from_cli_get_vol_req, - this, gf_cli3_1_get_volume_cbk); - -out: - gf_log ("cli", GF_LOG_DEBUG, "Returning %d", ret); - return ret; -} - - -int32_t -gf_cli3_1_create_volume (call_frame_t *frame, xlator_t *this, - void *data) -{ - gf1_cli_create_vol_req req = {0,}; - int ret = 0; - dict_t *dict = NULL; - cli_local_t *local = NULL; - - if (!frame || !this || !data) { - ret = -1; - goto out; - } - - dict = dict_ref ((dict_t *)data); - - ret = dict_get_str (dict, "volname", &req.volname); - - if (ret) - goto out; - - ret = dict_get_int32 (dict, "type", (int32_t *)&req.type); - - if (ret) - goto out; - - ret = dict_get_int32 (dict, "count", &req.count); - if (ret) - goto out; - - ret = dict_allocate_and_serialize (dict, - &req.bricks.bricks_val, - (size_t *)&req.bricks.bricks_len); - if (ret < 0) { - gf_log (this->name, GF_LOG_DEBUG, - "failed to get serialized length of dict"); - goto out; - } - - local = cli_local_get (); - - if (local) { - local->u.create_vol.dict = dict_ref (dict); - frame->local = local; - } - - ret = cli_cmd_submit (&req, frame, cli_rpc_prog, - GD_MGMT_CLI_CREATE_VOLUME, NULL, - gf_xdr_from_cli_create_vol_req, - this, gf_cli3_1_create_volume_cbk); - - - -out: - gf_log ("cli", GF_LOG_DEBUG, "Returning %d", ret); - - if (req.bricks.bricks_val) { - GF_FREE (req.bricks.bricks_val); - } - - return ret; -} - -int32_t -gf_cli3_1_delete_volume (call_frame_t *frame, xlator_t *this, - void *data) -{ - gf1_cli_delete_vol_req req = {0,}; - int ret = 0; - cli_local_t *local = NULL; - - if (!frame || !this || !data) { - ret = -1; - goto out; - } - - local = cli_local_get (); - - if (local) { - local->u.delete_vol.volname = data; - frame->local = local; - } - - req.volname = data; - - ret = cli_cmd_submit (&req, frame, cli_rpc_prog, - GD_MGMT_CLI_DELETE_VOLUME, NULL, - gf_xdr_from_cli_delete_vol_req, - this, gf_cli3_1_delete_volume_cbk); - -out: - gf_log ("cli", GF_LOG_DEBUG, "Returning %d", ret); - - return ret; -} - -int32_t -gf_cli3_1_start_volume (call_frame_t *frame, xlator_t *this, - void *data) -{ - gf1_cli_start_vol_req req = {0,}; - int ret = 0; - cli_local_t *local = NULL; - - if (!frame || !this || !data) { - ret = -1; - goto out; - } - - local = cli_local_get (); - - if (local) { - local->u.start_vol.volname = data; - frame->local = local; - } - - req.volname = data; - - ret = cli_cmd_submit (&req, frame, cli_rpc_prog, - GD_MGMT_CLI_START_VOLUME, NULL, - gf_xdr_from_cli_start_vol_req, - this, gf_cli3_1_start_volume_cbk); - -out: - gf_log ("cli", GF_LOG_DEBUG, "Returning %d", ret); - - return ret; -} - -int32_t -gf_cli3_1_stop_volume (call_frame_t *frame, xlator_t *this, - void *data) -{ - gf1_cli_stop_vol_req req = {0,}; - int ret = 0; - cli_local_t *local = NULL; - - if (!frame || !this || !data) { - ret = -1; - goto out; - } - - req = *((gf1_cli_stop_vol_req*)data); - local = cli_local_get (); - - if (local) { - local->u.stop_vol.volname = req.volname; - local->u.stop_vol.flags = req.flags; - frame->local = local; - } - - ret = cli_cmd_submit (&req, frame, cli_rpc_prog, - GD_MGMT_CLI_STOP_VOLUME, NULL, - gf_xdr_from_cli_stop_vol_req, - this, gf_cli3_1_stop_volume_cbk); - -out: - gf_log ("cli", GF_LOG_DEBUG, "Returning %d", ret); - - return ret; -} - -int32_t -gf_cli3_1_defrag_volume (call_frame_t *frame, xlator_t *this, - void *data) -{ - gf1_cli_defrag_vol_req req = {0,}; - int ret = 0; - cli_local_t *local = NULL; - char *volname = NULL; - char *cmd_str = NULL; - dict_t *dict = NULL; - - if (!frame || !this || !data) { - ret = -1; - goto out; - } - - dict = data; - - ret = dict_get_str (dict, "volname", &volname); - if (ret) - gf_log ("", GF_LOG_DEBUG, "error"); - - ret = dict_get_str (dict, "command", &cmd_str); - if (ret) { - gf_log ("", GF_LOG_DEBUG, "error"); - goto out; - } - - if (strncasecmp (cmd_str, "start", 6) == 0) { - req.cmd = GF_DEFRAG_CMD_START; - } else if (strncasecmp (cmd_str, "stop", 5) == 0) { - req.cmd = GF_DEFRAG_CMD_STOP; - } else if (strncasecmp (cmd_str, "status", 7) == 0) { - req.cmd = GF_DEFRAG_CMD_STATUS; - } - - - local = cli_local_get (); - - if (local) { - local->u.defrag_vol.volname = gf_strdup (volname); - local->u.defrag_vol.cmd = req.cmd; - frame->local = local; - } - - req.volname = volname; - - ret = cli_cmd_submit (&req, frame, cli_rpc_prog, - GD_MGMT_CLI_DEFRAG_VOLUME, NULL, - gf_xdr_from_cli_defrag_vol_req, - this, gf_cli3_1_defrag_volume_cbk); - -out: - gf_log ("cli", GF_LOG_DEBUG, "Returning %d", ret); - - return ret; -} - -int32_t -gf_cli3_1_rename_volume (call_frame_t *frame, xlator_t *this, - void *data) -{ - gf1_cli_rename_vol_req req = {0,}; - int ret = 0; - dict_t *dict = NULL; - - if (!frame || !this || !data) { - ret = -1; - goto out; - } - - dict = data; - - ret = dict_get_str (dict, "old-volname", &req.old_volname); - - if (ret) - goto out; - - ret = dict_get_str (dict, "new-volname", &req.new_volname); - - if (ret) - goto out; - - ret = cli_cmd_submit (&req, frame, cli_rpc_prog, - GD_MGMT_CLI_RENAME_VOLUME, NULL, - gf_xdr_from_cli_rename_vol_req, - this, gf_cli3_1_rename_volume_cbk); - -out: - gf_log ("cli", GF_LOG_DEBUG, "Returning %d", ret); - - return ret; -} - -int32_t -gf_cli3_1_set_volume (call_frame_t *frame, xlator_t *this, - void *data) -{ - gf1_cli_set_vol_req req = {0,}; - int ret = 0; - dict_t *dict = NULL; - - if (!frame || !this || !data) { - ret = -1; - goto out; - } - - dict = data; - - ret = dict_get_str (dict, "volname", &req.volname); - - if (ret) - goto out; - - ret = dict_allocate_and_serialize (dict, - &req.dict.dict_val, - (size_t *)&req.dict.dict_len); - if (ret < 0) { - gf_log (this->name, GF_LOG_DEBUG, - "failed to get serialized length of dict"); - goto out; - } - - - ret = cli_cmd_submit (&req, frame, cli_rpc_prog, - GD_MGMT_CLI_SET_VOLUME, NULL, - gf_xdr_from_cli_set_vol_req, - this, gf_cli3_1_set_volume_cbk); - -out: - gf_log ("cli", GF_LOG_DEBUG, "Returning %d", ret); - - return ret; -} - -int32_t -gf_cli3_1_add_brick (call_frame_t *frame, xlator_t *this, - void *data) -{ - gf1_cli_add_brick_req req = {0,}; - int ret = 0; - dict_t *dict = NULL; - - if (!frame || !this || !data) { - ret = -1; - goto out; - } - - dict = data; - - ret = dict_get_str (dict, "volname", &req.volname); - - if (ret) - goto out; - - ret = dict_get_int32 (dict, "count", &req.count); - if (ret) - goto out; - - - ret = dict_allocate_and_serialize (dict, - &req.bricks.bricks_val, - (size_t *)&req.bricks.bricks_len); - if (ret < 0) { - gf_log (this->name, GF_LOG_DEBUG, - "failed to get serialized length of dict"); - goto out; - } - - ret = cli_cmd_submit (&req, frame, cli_rpc_prog, - GD_MGMT_CLI_ADD_BRICK, NULL, - gf_xdr_from_cli_add_brick_req, - this, gf_cli3_1_add_brick_cbk); - -out: - gf_log ("cli", GF_LOG_DEBUG, "Returning %d", ret); - - if (req.bricks.bricks_val) { - GF_FREE (req.bricks.bricks_val); - } - - return ret; -} - -int32_t -gf_cli3_1_remove_brick (call_frame_t *frame, xlator_t *this, - void *data) -{ - gf1_cli_remove_brick_req req = {0,}; - int ret = 0; - dict_t *dict = NULL; - - if (!frame || !this || !data) { - ret = -1; - goto out; - } - - dict = data; - - ret = dict_get_str (dict, "volname", &req.volname); - - if (ret) - goto out; - - ret = dict_get_int32 (dict, "type", (int32_t *)&req.type); - - if (ret) - goto out; - - ret = dict_get_int32 (dict, "count", &req.count); - - if (ret) - goto out; - - ret = dict_allocate_and_serialize (dict, - &req.bricks.bricks_val, - (size_t *)&req.bricks.bricks_len); - if (ret < 0) { - gf_log (this->name, GF_LOG_DEBUG, - "failed to get serialized length of dict"); - goto out; - } - - ret = cli_cmd_submit (&req, frame, cli_rpc_prog, - GD_MGMT_CLI_REMOVE_BRICK, NULL, - gf_xdr_from_cli_remove_brick_req, - this, gf_cli3_1_remove_brick_cbk); - - -out: - gf_log ("cli", GF_LOG_DEBUG, "Returning %d", ret); - - if (req.bricks.bricks_val) { - GF_FREE (req.bricks.bricks_val); - } - - return ret; -} - -int32_t -gf_cli3_1_replace_brick (call_frame_t *frame, xlator_t *this, - void *data) -{ - gf1_cli_replace_brick_req req = {0,}; - int ret = 0; - cli_local_t *local = NULL; - dict_t *dict = NULL; - char *src_brick = NULL; - char *dst_brick = NULL; - - if (!frame || !this || !data) { - ret = -1; - goto out; - } - - dict = data; - - local = cli_local_get (); - if (!local) { - ret = -1; - gf_log (this->name, GF_LOG_ERROR, - "Out of memory"); - goto out; - } - - local->u.replace_brick.dict = dict_ref (dict); - frame->local = local; - - ret = dict_get_int32 (dict, "operation", (int32_t *)&req.op); - if (ret) { - gf_log (this->name, GF_LOG_DEBUG, - "dict_get on operation failed"); - goto out; - } - ret = dict_get_str (dict, "volname", &req.volname); - if (ret) { - gf_log (this->name, GF_LOG_DEBUG, - "dict_get on volname failed"); - goto out; - } - - local->u.replace_brick.volname = gf_strdup (req.volname); - if (!local->u.replace_brick.volname) { - gf_log (this->name, GF_LOG_ERROR, - "Out of memory"); - ret = -1; - goto out; - } - - ret = dict_get_str (dict, "src-brick", &src_brick); - if (ret) { - gf_log (this->name, GF_LOG_DEBUG, - "dict_get on src-brick failed"); - goto out; - } - - ret = dict_get_str (dict, "dst-brick", &dst_brick); - if (ret) { - gf_log (this->name, GF_LOG_DEBUG, - "dict_get on dst-brick failed"); - goto out; - } - - gf_log (this->name, GF_LOG_DEBUG, - "Recevied command replace-brick %s with " - "%s with operation=%d", src_brick, - dst_brick, req.op); - - - ret = dict_allocate_and_serialize (dict, - &req.bricks.bricks_val, - (size_t *)&req.bricks.bricks_len); - if (ret < 0) { - gf_log (this->name, GF_LOG_DEBUG, - "failed to get serialized length of dict"); - goto out; - } - - ret = cli_cmd_submit (&req, frame, cli_rpc_prog, - GD_MGMT_CLI_REPLACE_BRICK, NULL, - gf_xdr_from_cli_replace_brick_req, - this, gf_cli3_1_replace_brick_cbk); - -out: - gf_log ("cli", GF_LOG_DEBUG, "Returning %d", ret); - - if (req.bricks.bricks_val) { - GF_FREE (req.bricks.bricks_val); - } - - return ret; -} - -int32_t -gf_cli3_1_log_filename (call_frame_t *frame, xlator_t *this, - void *data) -{ - gf1_cli_log_filename_req req = {0,}; - int ret = 0; - dict_t *dict = NULL; - - if (!frame || !this || !data) { - ret = -1; - goto out; - } - - dict = data; - - ret = dict_get_str (dict, "volname", &req.volname); - if (ret) - goto out; - - ret = dict_get_str (dict, "brick", &req.brick); - if (ret) - req.brick = ""; - - ret = dict_get_str (dict, "path", &req.path); - if (ret) - goto out; - - ret = cli_cmd_submit (&req, frame, cli_rpc_prog, - GD_MGMT_CLI_LOG_FILENAME, NULL, - gf_xdr_from_cli_log_filename_req, - this, gf_cli3_1_log_filename_cbk); - -out: - gf_log ("cli", GF_LOG_DEBUG, "Returning %d", ret); - - return ret; -} - - -int32_t -gf_cli3_1_log_locate (call_frame_t *frame, xlator_t *this, - void *data) -{ - gf1_cli_log_locate_req req = {0,}; - int ret = 0; - dict_t *dict = NULL; - - if (!frame || !this || !data) { - ret = -1; - goto out; - } - - dict = data; - - ret = dict_get_str (dict, "volname", &req.volname); - if (ret) - goto out; - - ret = dict_get_str (dict, "brick", &req.brick); - if (ret) - req.brick = ""; - - ret = cli_cmd_submit (&req, frame, cli_rpc_prog, - GD_MGMT_CLI_LOG_LOCATE, NULL, - gf_xdr_from_cli_log_locate_req, - this, gf_cli3_1_log_locate_cbk); - -out: - gf_log ("cli", GF_LOG_DEBUG, "Returning %d", ret); - - return ret; -} - -int32_t -gf_cli3_1_log_rotate (call_frame_t *frame, xlator_t *this, - void *data) -{ - gf1_cli_log_locate_req req = {0,}; - int ret = 0; - dict_t *dict = NULL; - - if (!frame || !this || !data) { - ret = -1; - goto out; - } - - dict = data; - - ret = dict_get_str (dict, "volname", &req.volname); - if (ret) - goto out; - - ret = dict_get_str (dict, "brick", &req.brick); - if (ret) - req.brick = ""; - - ret = cli_cmd_submit (&req, frame, cli_rpc_prog, - GD_MGMT_CLI_LOG_ROTATE, NULL, - gf_xdr_from_cli_log_rotate_req, - this, gf_cli3_1_log_rotate_cbk); - - -out: - gf_log ("cli", GF_LOG_DEBUG, "Returning %d", ret); - - return ret; -} - - -struct rpc_clnt_procedure gluster3_1_cli_actors[GF1_CLI_MAXVALUE] = { - [GF1_CLI_NULL] = {"NULL", NULL }, - [GF1_CLI_PROBE] = { "PROBE_QUERY", gf_cli3_1_probe}, - [GF1_CLI_DEPROBE] = { "DEPROBE_QUERY", gf_cli3_1_deprobe}, - [GF1_CLI_LIST_FRIENDS] = { "LIST_FRIENDS", gf_cli3_1_list_friends}, - [GF1_CLI_CREATE_VOLUME] = {"CREATE_VOLUME", gf_cli3_1_create_volume}, - [GF1_CLI_DELETE_VOLUME] = {"DELETE_VOLUME", gf_cli3_1_delete_volume}, - [GF1_CLI_START_VOLUME] = {"START_VOLUME", gf_cli3_1_start_volume}, - [GF1_CLI_STOP_VOLUME] = {"STOP_VOLUME", gf_cli3_1_stop_volume}, - [GF1_CLI_RENAME_VOLUME] = {"RENAME_VOLUME", gf_cli3_1_rename_volume}, - [GF1_CLI_DEFRAG_VOLUME] = {"DEFRAG_VOLUME", gf_cli3_1_defrag_volume}, - [GF1_CLI_GET_VOLUME] = {"GET_VOLUME", gf_cli3_1_get_volume}, - [GF1_CLI_GET_NEXT_VOLUME] = {"GET_NEXT_VOLUME", gf_cli3_1_get_next_volume}, - [GF1_CLI_SET_VOLUME] = {"SET_VOLUME", gf_cli3_1_set_volume}, - [GF1_CLI_ADD_BRICK] = {"ADD_BRICK", gf_cli3_1_add_brick}, - [GF1_CLI_REMOVE_BRICK] = {"REMOVE_BRICK", gf_cli3_1_remove_brick}, - [GF1_CLI_REPLACE_BRICK] = {"REPLACE_BRICK", gf_cli3_1_replace_brick}, - [GF1_CLI_LOG_FILENAME] = {"LOG FILENAME", gf_cli3_1_log_filename}, - [GF1_CLI_LOG_LOCATE] = {"LOG LOCATE", gf_cli3_1_log_locate}, - [GF1_CLI_LOG_ROTATE] = {"LOG ROTATE", gf_cli3_1_log_rotate}, -}; - -struct rpc_clnt_program cli3_1_prog = { - .progname = "CLI 3.1", - .prognum = GLUSTER3_1_CLI_PROGRAM, - .progver = GLUSTER3_1_CLI_VERSION, - .proctable = gluster3_1_cli_actors, - .numproc = GLUSTER3_1_CLI_PROCCNT, -}; diff --git a/cli/src/input.c b/cli/src/input.c index e762fbd1385..5ac1a20edb1 100644 --- a/cli/src/input.c +++ b/cli/src/input.c @@ -1,99 +1,93 @@ /* - Copyright (c) 2010 Gluster, Inc. <http://www.gluster.com> - This file is part of GlusterFS. - - GlusterFS is free software; you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - GlusterFS is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see - <http://www.gnu.org/licenses/>. -*/ + Copyright (c) 2010-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + 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 <stdio.h> #include <string.h> #include <stdlib.h> #include <stdint.h> #include <pthread.h> -#ifndef _CONFIG_H -#define _CONFIG_H -#include "config.h" -#endif - #include "cli.h" #include "cli-mem-types.h" #define CMDBUFSIZ 1024 void * -cli_batch (void *d) +cli_batch(void *d) { - struct cli_state *state = NULL; - int ret = 0; + struct cli_state *state = NULL; + int ret = 0; - state = d; + state = d; - ret = cli_cmd_process (state, state->argc, state->argv); - gf_log ("", GF_LOG_NORMAL, "Exiting with: %d", ret); - exit (ret); + ret = cli_cmd_process(state, state->argc, state->argv); - return NULL; -} + gf_log("", GF_LOG_INFO, "Exiting with: %d", ret); + exit(-ret); + return NULL; +} void * -cli_input (void *d) +cli_input(void *d) { - struct cli_state *state = NULL; - int ret = 0; - char cmdbuf[CMDBUFSIZ]; - char *cmd = NULL; - size_t len = 0; - - state = d; - - for (;;) { - printf ("%s", state->prompt); - - cmd = fgets (cmdbuf, CMDBUFSIZ, stdin); - if (!cmd) - break; - len = strlen(cmd); - if (len > 0 && cmd[len - 1] == '\n') //strip trailing \n - cmd[len - 1] = '\0'; - ret = cli_cmd_process_line (state, cmd); - } - - exit (ret); - - return NULL; + struct cli_state *state = NULL; + int ret = 0; + char cmdbuf[CMDBUFSIZ]; + char *cmd = NULL; + size_t len = 0; + + state = d; + + fprintf(stderr, + "Welcome to gluster prompt, type 'help' to see the available " + "commands.\n"); + for (;;) { + printf("%s", state->prompt); + + cmd = fgets(cmdbuf, CMDBUFSIZ, stdin); + if (!cmd) + break; + len = strlen(cmd); + if (len > 0 && cmd[len - 1] == '\n') // strip trailing \n + cmd[len - 1] = '\0'; + ret = cli_cmd_process_line(state, cmd); + if (ret != 0 && state->mode & GLUSTER_MODE_ERR_FATAL) + break; + } + + exit(-ret); + + return NULL; } - int -cli_input_init (struct cli_state *state) +cli_input_init(struct cli_state *state) { - int ret = 0; + int ret = 0; - if (state->argc) { - ret = pthread_create (&state->input, NULL, cli_batch, state); - return ret; - } + if (state->argc) { + ret = pthread_create(&state->input, NULL, cli_batch, state); + return ret; + } + if (isatty(STDIN_FILENO)) { state->prompt = "gluster> "; - cli_rl_enable (state); + cli_rl_enable(state); + } else { + state->prompt = ""; + state->mode |= GLUSTER_MODE_SCRIPT | GLUSTER_MODE_ERR_FATAL; + } - if (!state->rl_enabled) - ret = pthread_create (&state->input, NULL, cli_input, state); + if (!state->rl_enabled) + ret = pthread_create(&state->input, NULL, cli_input, state); - return ret; + return ret; } diff --git a/cli/src/registry.c b/cli/src/registry.c index c3634d974de..85f7686ade1 100644 --- a/cli/src/registry.c +++ b/cli/src/registry.c @@ -1,27 +1,12 @@ /* - Copyright (c) 2010 Gluster, Inc. <http://www.gluster.com> - This file is part of GlusterFS. - - GlusterFS is free software; you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - GlusterFS is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see - <http://www.gnu.org/licenses/>. -*/ - + Copyright (c) 2010-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. -#ifndef _CONFIG_H -#define _CONFIG_H -#include "config.h" -#endif + 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 <stdio.h> #include <string.h> @@ -30,20 +15,18 @@ #include "cli.h" #include "cli-cmd.h" - static int -__is_spc (int ch) +__is_spc(int ch) { - if (ch == ' ') - return 1; - return 0; + if (ch == ' ') + return 1; + return 0; } - static int -__is_div (int ch) +__is_div(int ch) { - switch (ch) { + switch (ch) { case '(': case ')': case '<': @@ -53,336 +36,356 @@ __is_div (int ch) case '{': case '}': case '|': - return 1; - } + return 1; + } - return 0; + return 0; } - static int -__is_word (const char *word) +__is_word(const char *word) { - return (!__is_div (*word) && !__is_spc (*word)); + return (!__is_div(*word) && !__is_spc(*word)); } - int -counter_char (int ch) +counter_char(int ch) { - switch (ch) { + switch (ch) { case '(': - return ')'; + return ')'; case '<': - return '>'; + return '>'; case '[': - return ']'; + return ']'; case '{': - return '}'; - } + return '}'; + } - return -1; + return -1; } - const char * -__is_template_balanced (const char *template) +__is_template_balanced(const char *template) { - const char *trav = NULL; - int ch = 0; - - trav = template; - - while (*trav) { - ch = *trav; - - switch (ch) { - case '<': - case '(': - case '[': - trav = __is_template_balanced (trav+1); - if (!trav) - return NULL; - if (*trav != counter_char (ch)) - return NULL; - break; - case '>': - case ')': - case ']': - return trav; - } + const char *trav = NULL; + int ch = 0; + + trav = template; + + while (*trav) { + ch = *trav; - trav++; + switch (ch) { + case '<': + case '(': + case '[': + trav = __is_template_balanced(trav + 1); + if (!trav) + return NULL; + if (*trav != counter_char(ch)) + return NULL; + break; + case '>': + case ')': + case ']': + return trav; } - return trav; -} + trav++; + } + return trav; +} int -is_template_balanced (const char *template) +is_template_balanced(const char *template) { - const char *trav = NULL; + const char *trav = NULL; - trav = __is_template_balanced (template); - if (!trav || *trav) - return -1; + trav = __is_template_balanced(template); + if (!trav || *trav) + return -1; - return 0; + return 0; } - int -cli_cmd_token_count (const char *template) +cli_cmd_token_count(const char *template) { - int count = 0; - const char *trav = NULL; - int is_alnum = 0; - - for (trav = template; *trav; trav++) { - switch (*trav) { - case '<': - case '>': - case '(': - case ')': - case '[': - case ']': - case '{': - case '}': - case '|': - count++; - /* fall through */ - case ' ': - is_alnum = 0; - break; - default: - if (!is_alnum) { - is_alnum = 1; - count++; - } + int count = 0; + const char *trav = NULL; + int is_alnum = 0; + + for (trav = template; *trav; trav++) { + switch (*trav) { + case '<': + case '>': + case '(': + case ')': + case '[': + case ']': + case '{': + case '}': + case '|': + count++; + /* fall through */ + case ' ': + is_alnum = 0; + break; + default: + if (!is_alnum) { + is_alnum = 1; + count++; } } + } - return count + 1; + return count + 1; } - void -cli_cmd_tokens_destroy (char **tokens) +cli_cmd_tokens_destroy(char **tokens) { - char **tokenp = NULL; + char **tokenp = NULL; - if (!tokens) - return; + if (!tokens) + return; - tokenp = tokens; - while (*tokenp) { - free (*tokenp); - tokenp++; - } + tokenp = tokens; + while (*tokenp) { + free(*tokenp); + tokenp++; + } - free (tokens); + free(tokens); } - int -cli_cmd_tokens_fill (char **tokens, const char *template) +cli_cmd_tokens_fill(char **tokens, const char *template) { - const char *trav = NULL; - char **tokenp = NULL; - char *token = NULL; - int ret = 0; - int ch = 0; + const char *trav = NULL; + char **tokenp = NULL; + char *token = NULL; + int ret = 0; + int ch = 0; - tokenp = tokens; + tokenp = tokens; - for (trav = template; *trav; trav++) { - ch = *trav; + for (trav = template; *trav; trav++) { + ch = *trav; - if (__is_spc (ch)) - continue; + if (__is_spc(ch)) + continue; - if (__is_div (ch)) { - token = calloc (2, 1); - if (!token) - return -1; - token[0] = ch; + if (__is_div(ch)) { + token = calloc(2, 1); + if (!token) + return -1; + token[0] = ch; - *tokenp = token; - tokenp++; + *tokenp = token; + tokenp++; - continue; - } + continue; + } - token = strdup (trav); - *tokenp = token; - tokenp++; + token = strdup(trav); + *tokenp = token; + tokenp++; - for (token++; *token; token++) { - if (__is_spc (*token) || __is_div (*token)) { - *token = 0; - break; - } - trav++; - } + for (token++; *token; token++) { + if (__is_spc(*token) || __is_div(*token)) { + *token = 0; + break; + } + trav++; } + } - return ret; + return ret; } - char ** -cli_cmd_tokenize (const char *template) +cli_cmd_tokenize(const char *template) { - char **tokens = NULL; - int ret = 0; - int count = 0; + char **tokens = NULL; + int ret = 0; + int count = 0; - ret = is_template_balanced (template); - if (ret) - return NULL; + ret = is_template_balanced(template); + if (ret) + return NULL; - count = cli_cmd_token_count (template); - if (count <= 0) - return NULL; + count = cli_cmd_token_count(template); + if (count <= 0) + return NULL; - tokens = calloc (count + 1, sizeof (char *)); - if (!tokens) - return NULL; + tokens = calloc(count + 1, sizeof(char *)); + if (!tokens) + return NULL; - ret = cli_cmd_tokens_fill (tokens, template); - if (ret) - goto err; + ret = cli_cmd_tokens_fill(tokens, template); + if (ret) + goto err; - return tokens; + return tokens; err: - cli_cmd_tokens_destroy (tokens); - return NULL; + cli_cmd_tokens_destroy(tokens); + return NULL; } - -struct cli_cmd_word * -cli_cmd_nextword (struct cli_cmd_word *word, const char *token) +void * +cli_getunamb(const char *tok, void **choices, cli_selector_t sel) { - struct cli_cmd_word *next = NULL; - struct cli_cmd_word **trav = NULL; - int ret = 0; - - if (!word->nextwords) - return NULL; - - for (trav = word->nextwords; (next = *trav); trav++) { - if (next->match) { -// ret = next->match (); - } else { - ret = strcmp (next->word, token); - } + void **wcon = NULL; + char *w = NULL; + unsigned mn = 0; + void *ret = NULL; - if (ret == 0) - break; - } + if (!choices || !tok || !*tok) + return NULL; - return next; + for (wcon = choices; *wcon; wcon++) { + w = strtail((char *)sel(*wcon), tok); + if (!w) + /* no match */ + continue; + if (!*w) + /* exact match */ + return *wcon; + + ret = *wcon; + mn++; + } + +#ifdef FORCE_MATCH_EXACT + return NULL; +#else + return (mn == 1) ? ret : NULL; +#endif } +static const char * +sel_cmd_word(void *wcon) +{ + return ((struct cli_cmd_word *)wcon)->word; +} struct cli_cmd_word * -cli_cmd_newword (struct cli_cmd_word *word, const char *token) +cli_cmd_nextword(struct cli_cmd_word *word, const char *token) { - struct cli_cmd_word **nextwords = NULL; - struct cli_cmd_word *nextword = NULL; + return (struct cli_cmd_word *)cli_getunamb(token, (void **)word->nextwords, + sel_cmd_word); +} - nextwords = realloc (word->nextwords, - (word->nextwords_cnt + 2) * sizeof (*nextwords)); - if (!nextwords) - return NULL; +struct cli_cmd_word * +cli_cmd_newword(struct cli_cmd_word *word, const char *token) +{ + struct cli_cmd_word **nextwords = NULL; + struct cli_cmd_word *nextword = NULL; - word->nextwords = nextwords; + nextwords = realloc(word->nextwords, + (word->nextwords_cnt + 2) * sizeof(*nextwords)); + if (!nextwords) + return NULL; - nextword = calloc (1, sizeof (*nextword)); - if (!nextword) - return NULL; + word->nextwords = nextwords; - nextword->word = strdup (token); - if (!nextword->word) { - free (nextword); - return NULL; - } + nextword = calloc(1, sizeof(*nextword)); + if (!nextword) + return NULL; - nextword->tree = word->tree; - nextwords[word->nextwords_cnt++] = nextword; - nextwords[word->nextwords_cnt] = NULL; + nextword->word = strdup(token); + if (!nextword->word) { + free(nextword); + return NULL; + } - return nextword; -} + nextword->tree = word->tree; + nextwords[word->nextwords_cnt++] = nextword; + nextwords[word->nextwords_cnt] = NULL; + return nextword; +} int -cli_cmd_ingest (struct cli_cmd_tree *tree, char **tokens, cli_cmd_cbk_t *cbkfn, - const char *desc) +cli_cmd_ingest(struct cli_cmd_tree *tree, char **tokens, cli_cmd_cbk_t *cbkfn, + const char *desc, const char *pattern) { - int ret = 0; - char **tokenp = NULL; - char *token = NULL; - struct cli_cmd_word *word = NULL; - struct cli_cmd_word *next = NULL; + int ret = 0; + char **tokenp = NULL; + char *token = NULL; + struct cli_cmd_word *word = NULL; + struct cli_cmd_word *next = NULL; - word = &tree->root; + word = &tree->root; - for (tokenp = tokens; (token = *tokenp); tokenp++) { - if (!__is_word (token)) - break; + for (tokenp = tokens; (token = *tokenp); tokenp++) { + if (!__is_word(token)) + break; - next = cli_cmd_nextword (word, token); - if (!next) - next = cli_cmd_newword (word, token); - - word = next; - if (!word) - break; - } + next = cli_cmd_nextword(word, token); + if (!next) + next = cli_cmd_newword(word, token); + word = next; if (!word) - return -1; + break; + } - if (word->cbkfn) { - /* warning - command already registered */ - } + if (!word) + return -1; - word->cbkfn = cbkfn; - word->desc = desc; + if (word->cbkfn) { + /* warning - command already registered */ + } - /* end of static strings in command template */ + word->cbkfn = cbkfn; + word->desc = desc; + word->pattern = pattern; - /* TODO: autocompletion beyond this point is just "nice to have" */ + /* end of static strings in command template */ - return ret; -} + /* TODO: autocompletion beyond this point is just "nice to have" */ + return ret; +} int -cli_cmd_register (struct cli_cmd_tree *tree, const char *template, - cli_cmd_cbk_t cbk, const char *desc) +cli_cmd_register(struct cli_cmd_tree *tree, struct cli_cmd *cmd) { - char **tokens = NULL; - int ret = 0; + char **tokens = NULL; + int ret = 0; - if (!template) - return -1; + GF_ASSERT(cmd); - tokens = cli_cmd_tokenize (template); - if (!tokens) - return -1; + if (cmd->reg_cbk) + cmd->reg_cbk(cmd); - ret = cli_cmd_ingest (tree, tokens, cbk, desc); - if (ret) - goto err; + if (cmd->disable) { + ret = 0; + goto out; + } - return 0; -err: - if (tokens) - cli_cmd_tokens_destroy (tokens); + tokens = cli_cmd_tokenize(cmd->pattern); + if (!tokens) { + ret = -1; + goto out; + } - return ret; -} + ret = cli_cmd_ingest(tree, tokens, cmd->cbk, cmd->desc, cmd->pattern); + if (ret) { + ret = -1; + goto out; + } + + ret = 0; +out: + if (tokens) + cli_cmd_tokens_destroy(tokens); + + gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret); + return ret; +} |
