Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions fabrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ int fabrics_discovery(const char *desc, int argc, char **argv, bool connect)

out_free:
if (dump_config)
nvme_dump_config(ctx, NULL);
nvme_dump_config(ctx, STDOUT_FILENO);

return ret;
}
Expand Down Expand Up @@ -678,7 +678,7 @@ int fabrics_connect(const char *desc, int argc, char **argv)
}

if (dump_config)
nvme_dump_config(ctx, NULL);
nvme_dump_config(ctx, STDERR_FILENO);

return 0;
}
Expand Down Expand Up @@ -951,11 +951,16 @@ int fabrics_config(const char *desc, int argc, char **argv)
}
}

if (update_config)
nvme_dump_config(ctx, config_file);
if (update_config) {
_cleanup_fd_ int fd = -1;

fd = open(config_file, O_RDONLY, 0);
if (fd != -1)
nvme_dump_config(ctx, fd);
}

if (dump_config)
nvme_dump_config(ctx, NULL);
nvme_dump_config(ctx, STDOUT_FILENO);

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion libnvme/libnvme/nvme.i
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ struct nvme_ns {
nvme_refresh_topology($self);
}
void dump_config() {
nvme_dump_config($self, NULL);
nvme_dump_config($self, STDERR_FILENO);
}
}

Expand Down
23 changes: 12 additions & 11 deletions libnvme/src/nvme/fabrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -2765,25 +2765,26 @@ void nvmf_nbft_free(struct nvme_global_ctx *ctx, struct nbft_file_entry *head)
}
}

static bool validate_uri(struct nbft_info_discovery *dd,
static bool validate_uri(struct nvme_global_ctx *ctx,
struct nbft_info_discovery *dd,
struct nvme_fabrics_uri *uri)
{
if (!uri) {
fprintf(stderr,
"Discovery Descriptor %d: failed to parse URI %s\n",
dd->index, dd->uri);
nvme_msg(ctx, LOG_ERR,
"Discovery Descriptor %d: failed to parse URI %s\n",
dd->index, dd->uri);
return false;
}
if (strcmp(uri->scheme, "nvme") != 0) {
fprintf(stderr,
"Discovery Descriptor %d: unsupported scheme '%s'\n",
dd->index, uri->scheme);
nvme_msg(ctx, LOG_ERR,
"Discovery Descriptor %d: unsupported scheme '%s'\n",
dd->index, uri->scheme);
return false;
}
if (!uri->protocol || strcmp(uri->protocol, "tcp") != 0) {
fprintf(stderr,
"Discovery Descriptor %d: unsupported transport '%s'\n",
dd->index, uri->protocol);
nvme_msg(ctx, LOG_ERR,
"Discovery Descriptor %d: unsupported transport '%s'\n",
dd->index, uri->protocol);
return false;
}

Expand Down Expand Up @@ -3110,7 +3111,7 @@ int nvmf_discovery_nbft(struct nvme_global_ctx *ctx,
ret = nvme_parse_uri((*dd)->uri, &uri);
if (ret)
continue;
if (!validate_uri(*dd, uri))
if (!validate_uri(ctx, *dd, uri))
continue;

host_traddr = NULL;
Expand Down
18 changes: 6 additions & 12 deletions libnvme/src/nvme/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ static void json_update_subsys(struct json_object *subsys_array,
}
}

int json_update_config(struct nvme_global_ctx *ctx, const char *config_file)
int json_update_config(struct nvme_global_ctx *ctx, int fd)
{
nvme_host_t h;
struct json_object *json_root, *host_obj;
Expand Down Expand Up @@ -432,18 +432,12 @@ int json_update_config(struct nvme_global_ctx *ctx, const char *config_file)
json_object_put(host_obj);
}
}
if (!config_file) {
ret = json_object_to_fd(1, json_root,
JSON_C_TO_STRING_PRETTY |
JSON_C_TO_STRING_NOSLASHESCAPE);
printf("\n");
} else
ret = json_object_to_file_ext(config_file, json_root,
JSON_C_TO_STRING_PRETTY |
JSON_C_TO_STRING_NOSLASHESCAPE);
ret = json_object_to_fd(fd, json_root,
JSON_C_TO_STRING_PRETTY |
JSON_C_TO_STRING_NOSLASHESCAPE);
write(fd, "\n", 1);
if (ret < 0) {
nvme_msg(ctx, LOG_ERR, "Failed to write to %s, %s\n",
config_file ? "stdout" : config_file,
nvme_msg(ctx, LOG_ERR, "Failed to write JSON config file: %s\n",
json_util_get_last_err());
ret = -EIO;
}
Expand Down
2 changes: 1 addition & 1 deletion libnvme/src/nvme/private.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ int nvme_set_attr(const char *dir, const char *attr, const char *value);

int json_read_config(struct nvme_global_ctx *ctx, const char *config_file);

int json_update_config(struct nvme_global_ctx *ctx, const char *config_file);
int json_update_config(struct nvme_global_ctx *ctx, int fd);

int json_dump_tree(struct nvme_global_ctx *ctx);

Expand Down
4 changes: 2 additions & 2 deletions libnvme/src/nvme/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,9 @@ int nvme_scan(const char *config_file, struct nvme_global_ctx **ctxp)
return ret;
}

int nvme_dump_config(struct nvme_global_ctx *ctx, const char *config_file)
int nvme_dump_config(struct nvme_global_ctx *ctx, int fd)
{
return json_update_config(ctx, config_file);
return json_update_config(ctx, fd);
}

int nvme_dump_tree(struct nvme_global_ctx *ctx)
Expand Down
9 changes: 4 additions & 5 deletions libnvme/src/nvme/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -1398,15 +1398,14 @@ void nvme_refresh_topology(struct nvme_global_ctx *ctx);
/**
* nvme_dump_config() - Print the JSON configuration
* @ctx: &struct nvme_global_ctx object
* @config_file: JSON configuration file.
* @fd: File descriptor to write the JSON configuration.
*
* Prints the current contents of the JSON configuration
* file to the config_file. If connfig_file is not provided it prints
* to stdout.
* Writes the current contents of the JSON configuration
* to the file descriptor fd.
*
* Return: 0 on success, or negative error code otherwise.
*/
int nvme_dump_config(struct nvme_global_ctx *ctx, const char *config_file);
int nvme_dump_config(struct nvme_global_ctx *ctx, int fd);

/**
* nvme_dump_tree() - Dump internal object tree
Expand Down
3 changes: 2 additions & 1 deletion libnvme/test/config/config-dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

#include <libnvme.h>
Expand All @@ -29,7 +30,7 @@ static bool config_dump(const char *file)
if (err)
goto out;

err = nvme_dump_config(ctx, NULL);
err = nvme_dump_config(ctx, STDOUT_FILENO);
if (err)
goto out;

Expand Down
3 changes: 2 additions & 1 deletion libnvme/test/config/psk-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

#include <libnvme.h>
Expand Down Expand Up @@ -69,7 +70,7 @@ static bool psk_json_test(char *file)
if (!import_export_key(ctx, c))
goto out;

err = nvme_dump_config(ctx, NULL);
err = nvme_dump_config(ctx, STDOUT_FILENO);
if (err)
goto out;

Expand Down