bdev/raid: Factor out add raid bdev config operation

Change-Id: I79abce53526fa71981a969cc5599f496f4606f35
Signed-off-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
Reviewed-on: https://review.gerrithub.io/420327
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Chandler-Test-Pool: SPDK Automated Test System <sys_sgsw@intel.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
Reviewed-by: Kunal Sablok <kunal.sablok@intel.com>
This commit is contained in:
Shuhei Matsumoto 2018-07-25 10:44:33 +09:00 committed by Jim Harris
parent eff04f8e4c
commit e67338d464
3 changed files with 69 additions and 58 deletions

View File

@ -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: * brief:
* raid_bdev_parse_raid is used to parse the raid bdev from config file based on * 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; const char *base_bdev_name;
uint32_t i, j; uint32_t i, j;
struct raid_bdev_config *raid_bdev_config, *tmp; struct raid_bdev_config *raid_bdev_config, *tmp;
int rc;
raid_name = spdk_conf_section_get_val(conf_section, "Name"); raid_name = spdk_conf_section_get_val(conf_section, "Name");
if (raid_name == NULL) { 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, SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "%s %d %d %d\n", raid_name, strip_size, num_base_bdevs,
raid_level); raid_level);
TAILQ_FOREACH(tmp, &g_spdk_raid_config.raid_bdev_config_head, link) { rc = raid_bdev_config_add(raid_name, strip_size, num_base_bdevs, raid_level,
if (!strcmp(tmp->name, raid_name)) { &raid_bdev_config);
SPDK_ERRLOG("Duplicate raid bdev name found in config file %s\n", raid_name); if (rc != 0) {
return -1; SPDK_ERRLOG("Failed to add raid bdev config\n");
} return rc;
}
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;
} }
for (i = 0; true; i++) { for (i = 0; true; i++) {

View File

@ -228,6 +228,8 @@ extern struct raid_config g_spdk_raid_config;
void raid_bdev_remove_base_bdev(void *ctx); void raid_bdev_remove_base_bdev(void *ctx);
int raid_bdev_add_base_device(struct spdk_bdev *bdev); 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); void raid_bdev_config_cleanup(struct raid_bdev_config *raid_cfg);
#endif // SPDK_BDEV_RAID_INTERNAL_H #endif // SPDK_BDEV_RAID_INTERNAL_H

View File

@ -311,9 +311,9 @@ spdk_rpc_construct_raid_bdev(struct spdk_jsonrpc_request *request,
struct rpc_construct_raid_bdev req = {}; struct rpc_construct_raid_bdev req = {};
struct spdk_json_write_ctx *w; struct spdk_json_write_ctx *w;
struct raid_bdev_ctxt *raid_bdev_ctxt; struct raid_bdev_ctxt *raid_bdev_ctxt;
struct raid_base_bdev_config *base_bdevs;
struct raid_bdev_config *raid_bdev_config; struct raid_bdev_config *raid_bdev_config;
struct spdk_bdev *base_bdev; struct spdk_bdev *base_bdev;
int rc;
if (spdk_json_decode_object(params, rpc_construct_raid_bdev_decoders, if (spdk_json_decode_object(params, rpc_construct_raid_bdev_decoders,
SPDK_COUNTOF(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; return;
} }
base_bdevs = calloc(req.base_bdevs.num_base_bdevs, sizeof(struct raid_base_bdev_config)); rc = raid_bdev_config_add(req.name, req.strip_size, req.base_bdevs.num_base_bdevs, req.raid_level,
if (base_bdevs == NULL) { &raid_bdev_config);
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, spdk_strerror(ENOMEM)); if (rc != 0) {
spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, spdk_strerror(-rc));
free_rpc_construct_raid_bdev(&req); free_rpc_construct_raid_bdev(&req);
return; 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++) { 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]); 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) { if (raid_bdev_config->base_bdev[i].bdev_name == NULL) {