diff options
| -rw-r--r-- | cli/src/cli-cmd-volume.c | 42 | ||||
| -rw-r--r-- | cli/src/cli-rpc-ops.c | 161 | ||||
| -rw-r--r-- | cli/src/cli-xml-output.c | 239 | ||||
| -rw-r--r-- | cli/src/cli.h | 16 | 
4 files changed, 303 insertions, 155 deletions
diff --git a/cli/src/cli-cmd-volume.c b/cli/src/cli-cmd-volume.c index 84209adf936..7b83351bf29 100644 --- a/cli/src/cli-cmd-volume.c +++ b/cli/src/cli-cmd-volume.c @@ -1283,6 +1283,8 @@ cli_cmd_quota_handle_list_all (const char **words, dict_t *options)          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,};          xdata = dict_new ();          if (!xdata) { @@ -1307,7 +1309,13 @@ cli_cmd_quota_handle_list_all (const char **words, dict_t *options)           * quota enabled as cli_get_soft_limit() handles that           */          if (!_limits_set_on_volume (volname)) { -                cli_out ("quota: No quota configured on volume %s", volname); +                snprintf (err_str, sizeof (err_str), "No quota configured on " +                          "volume %s", volname); +                if (global_state->mode & GLUSTER_MODE_XML) { +                        xml_err_flag = _gf_true; +                } else { +                        cli_out ("quota: %s", err_str); +                }                  ret = 0;                  goto out;          } @@ -1358,7 +1366,18 @@ cli_cmd_quota_handle_list_all (const char **words, dict_t *options)          CLI_LOCAL_INIT (local, words, frame, xdata);          proc = &cli_quotad_clnt.proctable[GF_AGGREGATOR_GETLIMIT]; -        print_quota_list_header (); +        if (!(global_state->mode & GLUSTER_MODE_XML)) { +                print_quota_list_header (); +        } else { +                ret = cli_xml_output_vol_quota_limit_list_begin +                        (local, 0, 0, NULL); +                if (ret) { +                        gf_log ("cli", GF_LOG_ERROR, "Error in printing " +                                "xml output"); +                        goto out; +                } +        } +          gfid_str = GF_CALLOC (1, gf_common_mt_char, 64);          if (!gfid_str) {                  ret = -1; @@ -1394,12 +1413,31 @@ cli_cmd_quota_handle_list_all (const char **words, dict_t *options)                  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; +                } +        } +          if (count > 0) {                  ret = all_failed? -1: 0;          } else {                  ret = 0;          } + +  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 (fd != -1) {                  close (fd);          } diff --git a/cli/src/cli-rpc-ops.c b/cli/src/cli-rpc-ops.c index 1af1ca52af1..53464d79fac 100644 --- a/cli/src/cli-rpc-ops.c +++ b/cli/src/cli-rpc-ops.c @@ -2402,7 +2402,8 @@ out:  }  static int -print_quota_list_output (char *mountdir, char *default_sl, char *path) +print_quota_list_output (cli_local_t *local, char *mountdir, +                         char *default_sl, char *path)  {          int64_t used_space       = 0;          int64_t avail            = 0; @@ -2435,10 +2436,30 @@ print_quota_list_output (char *mountdir, char *default_sl, char *path)  #if defined(ENOATTR) && (ENOATTR != ENODATA)                  case ENOATTR:  #endif -                        cli_err ("%-40s %s", path, "Limit not set"); +                      if (global_state->mode & GLUSTER_MODE_XML) { +                                ret = cli_quota_list_xml_error +                                        (local, path, "Limit not set"); +                                if (ret) { +                                        gf_log ("cli", GF_LOG_ERROR, "Failed " +                                                "to print xml output"); +                                        goto out; +                                } +                        } else { +                                cli_err ("%-40s %s", path, strerror (errno)); +                        }                          break;                  default: -                        cli_err ("%-40s %s", path, strerror (errno)); +                      if (global_state->mode & GLUSTER_MODE_XML) { +                                ret = cli_quota_list_xml_error +                                        (local, path, strerror (errno)); +                                if (ret) { +                                        gf_log ("cli", GF_LOG_ERROR, "Failed " +                                                "to print xml output"); +                                        goto out; +                                } +                        } else { +                                cli_err ("%-40s %s", path, strerror (errno)); +                        }                          break;                  } @@ -2465,9 +2486,21 @@ print_quota_list_output (char *mountdir, char *default_sl, char *path)                               &used_space, sizeof (used_space));          if (ret < 0) { -                cli_out ("%-40s %7s %9s %11s %7s %15s %20s", -                         path, hl_str, sl_final, -                         "N/A", "N/A", "N/A", "N/A"); +                if (global_state->mode & GLUSTER_MODE_XML) { +                        ret = cli_quota_xml_output (local, path, hl_str, +                                                    sl_final, "N/A", +                                                    "N/A", "N/A", "N/A"); +                        if (ret) { +                                gf_log ("cli", GF_LOG_ERROR, "Failed to " +                                        "output in xml format for quota " +                                        "list command"); +                        } +                        goto out; +                } else { +                        cli_out ("%-40s %7s %9s %11s %7s %15s %20s", +                                 path, hl_str, sl_final, +                                 "N/A", "N/A", "N/A", "N/A"); +                }          } else {                  used_space = ntoh64 (used_space); @@ -2486,6 +2519,20 @@ print_quota_list_output (char *mountdir, char *default_sl, char *path)                  }                  avail_str = gf_uint64_2human_readable (avail); + +                if (global_state->mode & GLUSTER_MODE_XML) { +                        ret = cli_quota_xml_output (local, path, hl_str, +                                                 sl_final, used_str, +                                                 avail_str, sl ? "Yes" : "No", +                                                 hl ? "Yes" : "No"); +                        if (ret) { +                                gf_log ("cli", GF_LOG_ERROR, "Failed to " +                                        "output in xml format for quota " +                                        "list command"); +                        } +                        goto out; +                } +                  if (used_str == NULL) {                          cli_out ("%-40s %7s %9s %11"PRIu64                                   "%9"PRIu64" %15s %18s", path, hl_str, @@ -2506,14 +2553,17 @@ out:  }  int -gf_cli_print_limit_list_from_dict (char *volname, dict_t *dict, -                                   char *default_sl, int count, char *op_errstr) +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[1024]         = {0,}; -        char mountdir[PATH_MAX] = {0,}; -        char *path              = NULL; +        int             ret                     = -1; +        int             i                       = 0; +        char            key[1024]               = {0,}; +        char            mountdir[PATH_MAX]      = {0,}; +        char            *path                   = NULL; +        gf_boolean_t    xml_err_flag            = _gf_false; +        char            err_str[NAME_MAX]       = {0,};          if (!dict|| count <= 0)                  goto out; @@ -2522,8 +2572,14 @@ gf_cli_print_limit_list_from_dict (char *volname, dict_t *dict,           * to list them           */          if (!_limits_set_on_volume (volname)) { +                snprintf (err_str, sizeof (err_str), "No quota configured on " +                          "volume %s", volname); +                if (global_state->mode & GLUSTER_MODE_XML) { +                        xml_err_flag = _gf_true; +                } else { +                        cli_out ("quota: %s", err_str); +                }                  ret = 0; -                cli_out ("quota: No quota configured on volume %s", volname);                  goto out;          } @@ -2533,12 +2589,22 @@ gf_cli_print_limit_list_from_dict (char *volname, dict_t *dict,                  goto out;          } -        cli_out ("                  Path                   Hard-limit " -                 "Soft-limit   Used  Available  Soft-limit exceeded?" -                 "  Hard-limit exceeded?"); -        cli_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, +                                "Error outputting xml begin"); +                        goto out; +                } +        } else { +                cli_out ("                  Path                   Hard-limit " +                         "Soft-limit   Used  Available  Soft-limit exceeded?" +                         "  Hard-limit exceeded?"); +                cli_out ("-----------------------------------------------------" +                         "-----------------------------------------------------" +                         "-----------------"); +        }          while (count--) {                  snprintf (key, sizeof (key), "path%d", i++); @@ -2554,10 +2620,18 @@ gf_cli_print_limit_list_from_dict (char *volname, dict_t *dict,                  if (ret)                          goto out;                  GLUSTERD_GET_QUOTA_AUX_MOUNT_PATH (mountdir, volname, path); -                ret = print_quota_list_output (mountdir, default_sl, path); - +                ret = print_quota_list_output (local, mountdir, default_sl, +                                               path);          } +  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"); +                } +        }          return ret;  } @@ -2649,6 +2723,20 @@ print_quota_list_from_quotad (call_frame_t *frame, dict_t *rsp_dict)                          hl = sl = _gf_true;                  }                  avail_str = gf_uint64_2human_readable (avail); + +                if (global_state->mode & GLUSTER_MODE_XML) { +                        ret = cli_quota_xml_output (local, path, hl_str, +                                                sl_final, used_str, +                                                avail_str, sl ? "Yes" : "No", +                                                hl ? "Yes" : "No"); +                        if (ret) { +                                gf_log ("cli", GF_LOG_ERROR, "Failed in " +                                        "printing xml output for quota list " +                                        "command"); +                        } +                        goto out; +                } +                  if (used_str == NULL)                          cli_out ("%-40s %7s %9s %11"PRIu64                                   "%9"PRIu64" %15s %20s", path, hl_str, @@ -2760,8 +2848,9 @@ out:  }  void -gf_cli_quota_list (char *volname, dict_t *dict, int count, char *op_errstr, -                   char *default_sl) +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)  {          GF_VALIDATE_OR_GOTO ("cli", volname, out); @@ -2769,8 +2858,9 @@ gf_cli_quota_list (char *volname, dict_t *dict, int count, char *op_errstr,                  goto out;          if (count > 0) -                gf_cli_print_limit_list_from_dict (volname, dict, default_sl, -                                                   count, op_errstr); +                gf_cli_print_limit_list_from_dict (local, volname, dict, +                                                   default_sl, count, op_ret, +                                                   op_errno, op_errstr);  out:          return;  } @@ -2873,18 +2963,19 @@ gf_cli_quota_cbk (struct rpc_req *req, struct iovec *iov,                  gf_log (frame->this->name, GF_LOG_TRACE, "failed to get count");          if (type == GF_QUOTA_OPTION_TYPE_LIST) { +                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 -                                (volname, limit_list, rsp.op_ret, -                                 rsp.op_errno, rsp.op_errstr); -                        if (ret) -                                gf_log ("cli", GF_LOG_ERROR, -                                        "Error outputting to xml"); +                        ret = cli_xml_output_vol_quota_limit_list_end (local); +                        if (ret < 0) { +                                ret = -1; +                                gf_log ("cli", GF_LOG_ERROR, "Error in printing" +                                       " xml output"); +                        }                          goto out;                  } - -                gf_cli_quota_list (volname, dict, entry_count, rsp.op_errstr, -                                   default_sl);          }  xml_output: diff --git a/cli/src/cli-xml-output.c b/cli/src/cli-xml-output.c index 90b184d9f3f..f5806e5d42c 100644 --- a/cli/src/cli-xml-output.c +++ b/cli/src/cli-xml-output.c @@ -2872,137 +2872,47 @@ out:  }  int -cli_xml_output_vol_quota_limit_list (char *volname, char *limit_list, -                                     int op_ret, int op_errno, -                                     char *op_errstr) +cli_xml_output_vol_quota_limit_list_end (cli_local_t *local)  {  #if (HAVE_LIB_XML) -        int                     ret = -1; -        xmlTextWriterPtr        writer = NULL; -        xmlDocPtr               doc = NULL; -        int64_t                 size = 0; -        int64_t                 limit_value = 0; -        int                     i = 0; -        int                     j = 0; -        int                     k = 0; -        int                     len = 0; -        char                    *size_str = NULL; -        char                    path[PATH_MAX] = {0,}; -        char                    ret_str[1024] = {0,}; -        char                    value[1024] = {0,}; -        char                    mountdir[] = "/tmp/mountXXXXXX"; -        char                    abspath[PATH_MAX] = {0,}; -        runner_t                runner = {0,}; - -        GF_ASSERT (volname); -        GF_ASSERT (limit_list); - -        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; - -        /* <volQuota> */ -        ret = xmlTextWriterStartElement (writer, (xmlChar *)"volQuota"); -        XML_RET_CHECK_AND_GOTO (ret, out); - -        if (!limit_list) -                goto cont; - -        len = strlen (limit_list); -        if (len == 0) -                goto cont; - -        if (mkdtemp (mountdir) == NULL) { -                gf_log ("cli", GF_LOG_ERROR, "failed to create a temporary" -                        " mount directory"); -                ret = -1; -                goto out; -        } +        int     ret = -1; -        ret = runcmd (SBIN_DIR"/glusterfs", "-s", "localhost", -                      "--volfile-id", volname, "-l", -                      DEFAULT_LOG_FILE_DIRECTORY"/quota-list-xml.log", -                      mountdir, NULL); +        ret = xmlTextWriterEndElement (local->writer);          if (ret) { -                gf_log ("cli", GF_LOG_ERROR, -                        "failed to mount glusterfs client"); -                ret = -1; -                rmdir (mountdir); -                goto cont; +                goto out;          } -        while (i < len) { -                j = 0; -                k = 0; -                size = 0; - -                while (limit_list[i] != ':') -                        path[k++] = limit_list[i++]; -                path[k] = '\0'; - -                i++; - -                while (limit_list[i] != ',' && limit_list[i] != '\0') -                        value[j++] = limit_list[i++]; -                i++; - -                snprintf (abspath, sizeof (abspath), "%s/%s", mountdir, path); -                ret = sys_lgetxattr (abspath, "trusted.limit.list", -                                     (void *)ret_str, 4096); -                if (ret >= 0) { -                        sscanf (ret_str, "%"SCNd64",%"SCNd64, &size, -                                &limit_value); -                        size_str = gf_uint64_2human_readable ((uint64_t)size); -                } - -                /* <quota> */ -                ret = xmlTextWriterStartElement (writer, (xmlChar *)"quota"); -                XML_RET_CHECK_AND_GOTO (ret, unmount); - -                ret = xmlTextWriterWriteFormatElement -                        (writer, (xmlChar *)"path", "%s", path); -                XML_RET_CHECK_AND_GOTO (ret, unmount); - -                ret = xmlTextWriterWriteFormatElement -                        (writer, (xmlChar *)"limit", "%s", value); -                XML_RET_CHECK_AND_GOTO (ret, unmount); +        ret = cli_end_xml_output (local->writer, local->doc); -                if (size_str) { -                        ret = xmlTextWriterWriteFormatElement -                                (writer, (xmlChar *)"size", "%s", size_str); -                        XML_RET_CHECK_AND_GOTO (ret, unmount); -                        GF_FREE (size_str); -                } else { -                        ret = xmlTextWriterWriteFormatElement -                                (writer, (xmlChar *)"size", "%"PRId64, size); -                        XML_RET_CHECK_AND_GOTO (ret, unmount); -                } +out: +        return ret; +#else +        return 0; +#endif +} -                /* </quota> */ -                ret = xmlTextWriterEndElement (writer); -                XML_RET_CHECK_AND_GOTO (ret, unmount); +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; -unmount: -        ret = gf_umount_lazy ("cli", mountdir, 1); +        ret = cli_xml_output_common (local->writer, op_ret, op_errno, +                                     op_errstr);          if (ret) -                gf_log ("cli", GF_LOG_WARNING, "error unmounting %s: %s", -                        mountdir, strerror (errno)); +                goto out; -cont: -        /* </volQuota> */ -        ret = xmlTextWriterEndElement (writer); +        /* <volQuota> */ +        ret = xmlTextWriterStartElement (local->writer, (xmlChar *)"volQuota");          XML_RET_CHECK_AND_GOTO (ret, out); -        ret = cli_end_xml_output (writer, doc);  out: -        GF_FREE (size_str);          gf_log ("cli", GF_LOG_DEBUG, "Returning %d", ret);          return ret;  #else @@ -5683,3 +5593,102 @@ out:          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, char *hl_str, +                      char *sl_final, void *used, void *avail, char *sl, +                      char *hl) +{ +#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", +                                               "%s", hl_str); +        XML_RET_CHECK_AND_GOTO (ret, out); + +        ret = xmlTextWriterWriteFormatElement (local->writer, +                                              (xmlChar *)"soft_limit", +                                               "%s", sl_final); +        XML_RET_CHECK_AND_GOTO (ret, out); + +        if ((char *)used) { +                ret = xmlTextWriterWriteFormatElement +                        (local->writer, (xmlChar *)"used_space", "%s", +                        (char *)used); +        } else { +                ret = xmlTextWriterWriteFormatElement +                        (local->writer, (xmlChar *)"user_space", "%11"PRIu64, +                        *(long unsigned int *)used); +        } +        XML_RET_CHECK_AND_GOTO (ret, out); + +        if ((char *)avail) { +                ret = xmlTextWriterWriteFormatElement +                        (local->writer, (xmlChar *)"avail_space", "%s", +                        (char *)avail); +        } else { +                ret = xmlTextWriterWriteFormatElement +                        (local->writer, (xmlChar *)"avail_space", "%11"PRIu64, +                        *(long unsigned int *)avail); +        } +        XML_RET_CHECK_AND_GOTO (ret, out); + +        ret = xmlTextWriterWriteFormatElement (local->writer, +                                              (xmlChar *)"sl_exceeded", +                                               "%s", sl); +        XML_RET_CHECK_AND_GOTO (ret, out); + +        ret = xmlTextWriterWriteFormatElement (local->writer, +                                               (xmlChar *)"hl_exceeded", +                                               "%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.h b/cli/src/cli.h index e10dfafcbf3..3c63945934a 100644 --- a/cli/src/cli.h +++ b/cli/src/cli.h @@ -356,9 +356,19 @@ int  cli_xml_output_vol_info (cli_local_t *local, dict_t *dict);  int -cli_xml_output_vol_quota_limit_list (char *volname, char *limit_list, -                                      int op_ret, int op_errno, -                                      char *op_errstr); +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, char *hl_str, +                      char *sl_final, void *used, void *avail, +                      char *sl, char *hl);  int  cli_xml_output_peer_status (dict_t *dict, int op_ret, int op_errno,  | 
