From 4f0735b5032126f810ab1077d378cd2cebc76a5c Mon Sep 17 00:00:00 2001 From: Robert Baldyga Date: Sat, 20 Jul 2019 13:51:03 +0200 Subject: [PATCH] Ensure that core name is set and unique Signed-off-by: Robert Baldyga --- inc/ocf_core.h | 25 ++++++++++---------- inc/ocf_err.h | 6 +++++ inc/ocf_mngt.h | 3 +-- src/mngt/ocf_mngt_core.c | 50 +++++++++++++++++++++++++--------------- src/ocf_cache_priv.h | 7 ++++++ src/ocf_core.c | 17 ++++++++++++++ src/ocf_core_priv.h | 9 ++------ 7 files changed, 77 insertions(+), 40 deletions(-) diff --git a/inc/ocf_core.h b/inc/ocf_core.h index d7825f5..24db086 100644 --- a/inc/ocf_core.h +++ b/inc/ocf_core.h @@ -16,6 +16,19 @@ #include "ocf_io.h" #include "ocf_mngt.h" +/** + * @brief Get OCF core by name + * + * @param[in] cache OCF cache + * @param[in] name Core name + * @param[out] core OCF core handle + * + * @retval 0 Get cache successfully + * @retval -OCF_ERR_CORE_NOT_EXIST Core with given name doesn't exist + */ +int ocf_core_get_by_name(ocf_cache_t cache, const char *name, + ocf_core_t *core); + /** * @brief Obtain cache object from core * @@ -82,18 +95,6 @@ ocf_seq_cutoff_policy ocf_core_get_seq_cutoff_policy(ocf_core_t core); */ ocf_core_id_t ocf_core_get_id(ocf_core_t core); -/** - * @brief Set name of given core object - * - * @param[in] core Core object - * @param[in] src Source of Core name - * @param[in] src_size Size of src - * - * @retval 0 Success - * @retval Non-zero Fail - */ -int ocf_core_set_name(ocf_core_t core, const char *src, size_t src_size); - /** * @brief Get name of given core object * diff --git a/inc/ocf_err.h b/inc/ocf_err.h index 10ecc12..52345a3 100644 --- a/inc/ocf_err.h +++ b/inc/ocf_err.h @@ -60,9 +60,15 @@ typedef enum { /** Cache ID/name does not exist */ OCF_ERR_CACHE_NOT_EXIST, + /** Core ID/name does not exist */ + OCF_ERR_CORE_NOT_EXIST, + /** Cache ID/name already exists */ OCF_ERR_CACHE_EXIST, + /** Core ID/name already exists */ + OCF_ERR_CORE_EXIST, + /** Too many core devices in cache */ OCF_ERR_TOO_MANY_CORES, diff --git a/inc/ocf_mngt.h b/inc/ocf_mngt.h index 2fb117d..f945ce4 100644 --- a/inc/ocf_mngt.h +++ b/inc/ocf_mngt.h @@ -58,7 +58,7 @@ struct ocf_mngt_core_config { /** * @brief Initialize core config to default values * - * @note This function doesn't initiialize uuid and volume_type fields + * @note This function doesn't initialize name, uuid and volume_type fields * which have no default values and are required to be set by user. * * @param[in] cfg Core config stucture @@ -67,7 +67,6 @@ static inline void ocf_mngt_core_config_set_default( struct ocf_mngt_core_config *cfg) { cfg->core_id = OCF_CORE_ID_INVALID; - cfg->name = NULL; cfg->try_add = false; cfg->seq_cutoff_threshold = 1024; cfg->user_metadata.data = NULL; diff --git a/src/mngt/ocf_mngt_core.c b/src/mngt/ocf_mngt_core.c index cfe10bd..867886d 100644 --- a/src/mngt/ocf_mngt_core.c +++ b/src/mngt/ocf_mngt_core.c @@ -119,7 +119,6 @@ struct ocf_cache_add_core_context { void *priv; ocf_pipeline_t pipeline; struct ocf_mngt_core_config cfg; - char core_name[OCF_CORE_NAME_SIZE]; ocf_cache_t cache; ocf_core_t core; @@ -346,8 +345,9 @@ static int __ocf_mngt_lookup_core_uuid(ocf_cache_t cache, if (!env_strncmp(core->volume.uuid.data, cfg->uuid.data, OCF_MIN(core->volume.uuid.size, - cfg->uuid.size))) + cfg->uuid.size))) { return i; + } } return OCF_CORE_MAX; @@ -468,27 +468,22 @@ static void ocf_mngt_cache_add_core_prepare(ocf_pipeline_t pipeline, { struct ocf_cache_add_core_context *context = priv; ocf_cache_t cache = context->cache; - char *core_name = context->core_name; + ocf_core_t core; int result; result = _ocf_mngt_find_core_id(cache, &context->cfg); if (result) OCF_PL_FINISH_RET(context->pipeline, result); - if (context->cfg.name) { - result = env_strncpy(core_name, sizeof(context->core_name), - context->cfg.name, sizeof(context->core_name)); - if (result) - OCF_PL_FINISH_RET(context->pipeline, result); - } else { - result = snprintf(core_name, sizeof(context->core_name), - "core%hu", context->cfg.core_id); - if (result < 0) - OCF_PL_FINISH_RET(context->pipeline, result); - } + if (!context->cfg.name) + OCF_PL_FINISH_RET(context->pipeline, -OCF_ERR_INVAL); + + result = ocf_core_get_by_name(cache, context->cfg.name, &core); + if (!result && !context->cfg.try_add) + OCF_PL_FINISH_RET(context->pipeline, -OCF_ERR_CORE_EXIST); result = ocf_core_set_name(&cache->core[context->cfg.core_id], - core_name, sizeof(context->core_name)); + context->cfg.name, OCF_CORE_NAME_SIZE); if (result) OCF_PL_FINISH_RET(context->pipeline, result); @@ -500,7 +495,7 @@ static void ocf_mngt_cache_add_core_insert(ocf_pipeline_t pipeline, { struct ocf_cache_add_core_context *context = priv; ocf_cache_t cache = context->cache; - char *core_name = context->core_name; + const char *core_name = context->cfg.name; int result; ocf_cache_log(cache, log_debug, "Inserting core %s\n", core_name); @@ -540,10 +535,10 @@ static void ocf_mngt_cache_add_core_finish(ocf_pipeline_t pipeline, if (error == -OCF_ERR_CORE_NOT_AVAIL) { ocf_cache_log(cache, log_err, "Core %s is zero size\n", - context->core_name); + context->cfg.name); } ocf_cache_log(cache, log_err, "Adding core %s failed\n", - context->core_name); + context->cfg.name); goto out; } @@ -551,6 +546,7 @@ static void ocf_mngt_cache_add_core_finish(ocf_pipeline_t pipeline, out: context->cmpl(cache, core, context->priv, error); + env_vfree(context->cfg.name); env_vfree(context->cfg.uuid.data); ocf_pipeline_destroy(context->pipeline); } @@ -572,6 +568,7 @@ void ocf_mngt_cache_add_core(ocf_cache_t cache, { struct ocf_cache_add_core_context *context; ocf_pipeline_t pipeline; + char *name; void *data; int result; @@ -593,10 +590,23 @@ void ocf_mngt_cache_add_core(ocf_cache_t cache, context->cache = cache; context->cfg = *cfg; + name = env_vmalloc(OCF_CORE_NAME_SIZE); + if (!name) { + result = -OCF_ERR_NO_MEM; + goto err_pipeline; + } + + result = env_strncpy(name, OCF_CORE_NAME_SIZE, + cfg->name, OCF_CORE_NAME_SIZE); + if (result) + goto err_name; + + context->cfg.name = name; + data = env_vmalloc(cfg->uuid.size); if (!data) { result = -OCF_ERR_NO_MEM; - goto err_pipeline; + goto err_name; } result = env_memcpy(data, cfg->uuid.size, cfg->uuid.data, @@ -610,6 +620,8 @@ void ocf_mngt_cache_add_core(ocf_cache_t cache, err_uuid: env_vfree(data); +err_name: + env_vfree(context->cfg.name); err_pipeline: ocf_pipeline_destroy(context->pipeline); OCF_CMPL_RET(cache, NULL, priv, result); diff --git a/src/ocf_cache_priv.h b/src/ocf_cache_priv.h index 1078da9..782d770 100644 --- a/src/ocf_cache_priv.h +++ b/src/ocf_cache_priv.h @@ -179,6 +179,13 @@ static inline ocf_core_t ocf_cache_get_core(ocf_cache_t cache, return &cache->core[core_id]; } +#define for_each_core_all(_cache, _core, _id) \ + for (_id = 0; _core = &cache->core[_id], _id < OCF_CORE_MAX; _id++) + +#define for_each_core(_cache, _core, _id) \ + for_each_core_all(_cache, _core, _id) \ + if (_core->conf_meta->added) + #define ocf_cache_log_prefix(cache, lvl, prefix, fmt, ...) \ ocf_log_prefix(ocf_cache_get_ctx(cache), lvl, "%s" prefix, \ fmt, ocf_cache_get_name(cache), ##__VA_ARGS__) diff --git a/src/ocf_core.c b/src/ocf_core.c index 6446c6c..eef361f 100644 --- a/src/ocf_core.c +++ b/src/ocf_core.c @@ -49,6 +49,23 @@ ocf_core_id_t ocf_core_get_id(ocf_core_t core) return core_id; } +int ocf_core_get_by_name(ocf_cache_t cache, const char *name, + ocf_core_t *core) +{ + ocf_core_t i_core; + ocf_core_id_t i_core_id; + + for_each_core(cache, i_core, i_core_id) { + if (!env_strncmp(ocf_core_get_name(i_core), name, + OCF_CORE_NAME_SIZE)) { + *core = i_core; + return 0; + } + } + + return -OCF_ERR_CORE_NOT_EXIST; +} + int ocf_core_set_name(ocf_core_t core, const char *src, size_t src_size) { OCF_CHECK_NULL(core); diff --git a/src/ocf_core_priv.h b/src/ocf_core_priv.h index 0030e11..e1407b4 100644 --- a/src/ocf_core_priv.h +++ b/src/ocf_core_priv.h @@ -91,17 +91,12 @@ struct ocf_core { struct ocf_counters_core *counters; }; +int ocf_core_set_name(ocf_core_t core, const char *src, size_t src_size); + bool ocf_core_is_valid(ocf_cache_t cache, ocf_core_id_t id); int ocf_core_volume_type_init(ocf_ctx_t ctx); void ocf_core_volume_type_deinit(ocf_ctx_t ctx); -#define for_each_core_all(_cache, _core, _id) \ - for (_id = 0; _core = &cache->core[_id], _id < OCF_CORE_MAX; _id++) - -#define for_each_core(_cache, _core, _id) \ - for_each_core_all(_cache, _core, _id) \ - if (core->conf_meta->added) - #endif /* __OCF_CORE_PRIV_H__ */