diff --git a/lib/bdev/raid/bdev_raid.c b/lib/bdev/raid/bdev_raid.c index 3cdc1ea5b..d0c5b979c 100644 --- a/lib/bdev/raid/bdev_raid.c +++ b/lib/bdev/raid/bdev_raid.c @@ -817,6 +817,62 @@ raid_bdev_free(void) } } +/* + * brief + * raid_bdev_config_add function adds config for newly created raid bdev. + * + * params: + * raid_name - name for raid bdev. + * strip_size - strip size in KB + * num_base_bdevs - number of base bdevs. + * raid_level - raid level, only raid level 0 is supported. + * _raid_bdev_config - Pointer to newly added configuration + */ +int +raid_bdev_config_add(const char *raid_name, int strip_size, int num_base_bdevs, + int raid_level, struct raid_bdev_config **_raid_bdev_config) +{ + struct raid_bdev_config *raid_cfg; + + TAILQ_FOREACH(raid_cfg, &g_spdk_raid_config.raid_bdev_config_head, link) { + if (!strcmp(raid_cfg->name, raid_name)) { + SPDK_ERRLOG("Duplicate raid bdev name found in config file %s\n", + raid_name); + return -EEXIST; + } + } + + raid_cfg = calloc(1, sizeof(*raid_cfg)); + if (raid_cfg == NULL) { + SPDK_ERRLOG("unable to allocate memory\n"); + return -ENOMEM; + } + + raid_cfg->name = strdup(raid_name); + if (!raid_cfg->name) { + free(raid_cfg); + SPDK_ERRLOG("unable to allocate memory\n"); + return -ENOMEM; + } + raid_cfg->strip_size = strip_size; + raid_cfg->num_base_bdevs = num_base_bdevs; + raid_cfg->raid_level = raid_level; + + raid_cfg->base_bdev = calloc(num_base_bdevs, sizeof(*raid_cfg->base_bdev)); + if (raid_cfg->base_bdev == NULL) { + free(raid_cfg->name); + free(raid_cfg); + SPDK_ERRLOG("unable to allocate memory\n"); + return -ENOMEM; + } + + TAILQ_INSERT_TAIL(&g_spdk_raid_config.raid_bdev_config_head, raid_cfg, link); + g_spdk_raid_config.total_raid_bdev++; + + *_raid_bdev_config = raid_cfg; + return 0; +} + /* * brief: * raid_bdev_parse_raid is used to parse the raid bdev from config file based on @@ -852,6 +908,7 @@ raid_bdev_parse_raid(struct spdk_conf_section *conf_section) const char *base_bdev_name; uint32_t i, j; struct raid_bdev_config *raid_bdev_config, *tmp; + int rc; raid_name = spdk_conf_section_get_val(conf_section, "Name"); if (raid_name == NULL) { @@ -877,36 +934,11 @@ raid_bdev_parse_raid(struct spdk_conf_section *conf_section) SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "%s %d %d %d\n", raid_name, strip_size, num_base_bdevs, raid_level); - TAILQ_FOREACH(tmp, &g_spdk_raid_config.raid_bdev_config_head, link) { - if (!strcmp(tmp->name, raid_name)) { - SPDK_ERRLOG("Duplicate raid bdev name found in config file %s\n", raid_name); - return -1; - } - } - - raid_bdev_config = calloc(1, sizeof(*raid_bdev_config)); - if (raid_bdev_config == NULL) { - SPDK_ERRLOG("unable to allocate memory\n"); - return -ENOMEM; - } - - raid_bdev_config->name = strdup(raid_name); - if (!raid_bdev_config->name) { - free(raid_bdev_config); - SPDK_ERRLOG("unable to allocate memory\n"); - return -ENOMEM; - } - raid_bdev_config->strip_size = strip_size; - raid_bdev_config->num_base_bdevs = num_base_bdevs; - raid_bdev_config->raid_level = raid_level; - TAILQ_INSERT_TAIL(&g_spdk_raid_config.raid_bdev_config_head, raid_bdev_config, link); - g_spdk_raid_config.total_raid_bdev++; - - raid_bdev_config->base_bdev = calloc(num_base_bdevs, sizeof(*raid_bdev_config->base_bdev)); - if (raid_bdev_config->base_bdev == NULL) { - raid_bdev_config_cleanup(raid_bdev_config); - SPDK_ERRLOG("unable to allocate memory\n"); - return -ENOMEM; + rc = raid_bdev_config_add(raid_name, strip_size, num_base_bdevs, raid_level, + &raid_bdev_config); + if (rc != 0) { + SPDK_ERRLOG("Failed to add raid bdev config\n"); + return rc; } for (i = 0; true; i++) { diff --git a/lib/bdev/raid/bdev_raid.h b/lib/bdev/raid/bdev_raid.h index 55be707c4..91767dc17 100644 --- a/lib/bdev/raid/bdev_raid.h +++ b/lib/bdev/raid/bdev_raid.h @@ -228,6 +228,8 @@ extern struct raid_config g_spdk_raid_config; void raid_bdev_remove_base_bdev(void *ctx); int raid_bdev_add_base_device(struct spdk_bdev *bdev); +int raid_bdev_config_add(const char *raid_name, int strip_size, int num_base_bdevs, + int raid_level, struct raid_bdev_config **_raid_bdev_config); void raid_bdev_config_cleanup(struct raid_bdev_config *raid_cfg); #endif // SPDK_BDEV_RAID_INTERNAL_H diff --git a/lib/bdev/raid/bdev_raid_rpc.c b/lib/bdev/raid/bdev_raid_rpc.c index 9943016ac..379ed1adb 100644 --- a/lib/bdev/raid/bdev_raid_rpc.c +++ b/lib/bdev/raid/bdev_raid_rpc.c @@ -311,9 +311,9 @@ spdk_rpc_construct_raid_bdev(struct spdk_jsonrpc_request *request, struct rpc_construct_raid_bdev req = {}; struct spdk_json_write_ctx *w; struct raid_bdev_ctxt *raid_bdev_ctxt; - struct raid_base_bdev_config *base_bdevs; struct raid_bdev_config *raid_bdev_config; struct spdk_bdev *base_bdev; + int rc; if (spdk_json_decode_object(params, rpc_construct_raid_bdev_decoders, SPDK_COUNTOF(rpc_construct_raid_bdev_decoders), @@ -345,37 +345,14 @@ spdk_rpc_construct_raid_bdev(struct spdk_jsonrpc_request *request, return; } - base_bdevs = calloc(req.base_bdevs.num_base_bdevs, sizeof(struct raid_base_bdev_config)); - if (base_bdevs == NULL) { - spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, spdk_strerror(ENOMEM)); + rc = raid_bdev_config_add(req.name, req.strip_size, req.base_bdevs.num_base_bdevs, req.raid_level, + &raid_bdev_config); + if (rc != 0) { + spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, spdk_strerror(-rc)); free_rpc_construct_raid_bdev(&req); return; } - /* Allocate the new raid bdev config entry */ - raid_bdev_config = calloc(1, sizeof(*raid_bdev_config)); - if (raid_bdev_config == NULL) { - free(base_bdevs); - spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, spdk_strerror(ENOMEM)); - free_rpc_construct_raid_bdev(&req); - return; - } - - raid_bdev_config->name = strdup(req.name); - if (raid_bdev_config->name == NULL) { - free(base_bdevs); - free(raid_bdev_config); - spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, spdk_strerror(ENOMEM)); - free_rpc_construct_raid_bdev(&req); - return; - } - - raid_bdev_config->strip_size = req.strip_size; - raid_bdev_config->num_base_bdevs = req.base_bdevs.num_base_bdevs; - raid_bdev_config->raid_level = req.raid_level; - TAILQ_INSERT_TAIL(&g_spdk_raid_config.raid_bdev_config_head, raid_bdev_config, link); - g_spdk_raid_config.total_raid_bdev++; - raid_bdev_config->base_bdev = base_bdevs; for (size_t i = 0; i < raid_bdev_config->num_base_bdevs; i++) { raid_bdev_config->base_bdev[i].bdev_name = strdup(req.base_bdevs.base_bdevs[i]); if (raid_bdev_config->base_bdev[i].bdev_name == NULL) {