Ensure that cache name is set and unique

Signed-off-by: Robert Baldyga <robert.baldyga@intel.com>
This commit is contained in:
Robert Baldyga
2019-07-20 12:49:57 +02:00
parent 331b99397f
commit 901b39031f
4 changed files with 75 additions and 15 deletions

View File

@@ -726,18 +726,20 @@ static int _ocf_mngt_init_prepare_cache(struct ocf_cache_mngt_init_params *param
}
}
if (cfg->name) {
ret = env_strncpy(cache_name, sizeof(cache_name) - 1,
cfg->name, sizeof(cache_name) - 1);
if (ret)
goto out;
} else {
ret = snprintf(cache_name, sizeof(cache_name),
"cache%hu", param->id);
if (ret < 0)
goto out;
/* Check if cache with specified name exists */
ret = ocf_mngt_cache_get_by_name(param->ctx, cfg->name, &cache);
if (!ret) {
ocf_mngt_cache_put(cache);
/* Cache already exist */
ret = -OCF_ERR_CACHE_EXIST;
goto out;
}
ret = env_strncpy(cache_name, sizeof(cache_name),
cfg->name, sizeof(cache_name));
if (ret)
goto out;
ocf_log(param->ctx, log_info, "Inserting cache %s\n", cache_name);
ret = _ocf_mngt_init_new_cache(param);
@@ -1643,6 +1645,9 @@ static int _ocf_mngt_cache_validate_cfg(struct ocf_mngt_cache_config *cfg)
if (cfg->id > OCF_CACHE_ID_MAX)
return -OCF_ERR_INVAL;
if (!cfg->name)
return -OCF_ERR_INVAL;
if (!ocf_cache_mode_is_valid(cfg->cache_mode))
return -OCF_ERR_INVALID_CACHE_MODE;

View File

@@ -183,6 +183,44 @@ int ocf_mngt_cache_get_by_id(ocf_ctx_t ocf_ctx, ocf_cache_id_t id, ocf_cache_t *
return error;
}
int ocf_mngt_cache_get_by_name(ocf_ctx_t ctx, const char *name,
ocf_cache_t *cache)
{
struct ocf_cache *instance = NULL;
struct ocf_cache *iter = NULL;
OCF_CHECK_NULL(ctx);
OCF_CHECK_NULL(cache);
/* Lock caches list */
env_rmutex_lock(&ctx->lock);
list_for_each_entry(iter, &ctx->caches, list) {
if (!env_strncmp(ocf_cache_get_name(iter), name,
OCF_CACHE_NAME_SIZE)) {
instance = iter;
break;
}
}
if (instance) {
/* if cache is either fully initialized or during recovery */
if (!ocf_refcnt_inc(&instance->refcnt.cache)) {
/* Cache not initialized yet */
instance = NULL;
}
}
env_rmutex_unlock(&ctx->lock);
if (!instance)
return -OCF_ERR_CACHE_NOT_EXIST;
*cache = instance;
return 0;
}
typedef void (*ocf_lock_fn_t)(ocf_async_lock_waiter_t waiter);
typedef int (*ocf_trylock_fn_t)(ocf_async_lock_t lock);