Ensure that cache name is set and unique
Signed-off-by: Robert Baldyga <robert.baldyga@intel.com>
This commit is contained in:
		@@ -57,10 +57,10 @@ typedef enum {
 | 
				
			|||||||
	/** Start cache failure */
 | 
						/** Start cache failure */
 | 
				
			||||||
	OCF_ERR_START_CACHE_FAIL,
 | 
						OCF_ERR_START_CACHE_FAIL,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/** Cache ID does not exist */
 | 
						/** Cache ID/name does not exist */
 | 
				
			||||||
	OCF_ERR_CACHE_NOT_EXIST,
 | 
						OCF_ERR_CACHE_NOT_EXIST,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/** Cache ID already exists */
 | 
						/** Cache ID/name already exists */
 | 
				
			||||||
	OCF_ERR_CACHE_EXIST,
 | 
						OCF_ERR_CACHE_EXIST,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/** Too many core devices in cache */
 | 
						/** Too many core devices in cache */
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -101,6 +101,22 @@ uint32_t ocf_mngt_cache_get_count(ocf_ctx_t ctx);
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
int ocf_mngt_cache_get_by_id(ocf_ctx_t ctx, ocf_cache_id_t id, ocf_cache_t *cache);
 | 
					int ocf_mngt_cache_get_by_id(ocf_ctx_t ctx, ocf_cache_id_t id, ocf_cache_t *cache);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @brief Get OCF cache by name
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @note This function on success also increases reference counter
 | 
				
			||||||
 | 
					 *       in given cache
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @param[in] ctx OCF context
 | 
				
			||||||
 | 
					 * @param[in] name OCF cache name
 | 
				
			||||||
 | 
					 * @param[out] cache OCF cache handle
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @retval 0 Get cache successfully
 | 
				
			||||||
 | 
					 * @retval -OCF_ERR_CACHE_NOT_EXIST Cache with given name doesn't exist
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					int ocf_mngt_cache_get_by_name(ocf_ctx_t ctx, const char* name,
 | 
				
			||||||
 | 
							ocf_cache_t *cache);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * @brief Increment reference counter of cache
 | 
					 * @brief Increment reference counter of cache
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
@@ -247,8 +263,7 @@ struct ocf_mngt_cache_config {
 | 
				
			|||||||
	ocf_cache_id_t id;
 | 
						ocf_cache_id_t id;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * @brief Cache name. In case of being NULL, cache id is stringified to
 | 
						 * @brief Cache name
 | 
				
			||||||
	 *		cache name
 | 
					 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	const char *name;
 | 
						const char *name;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -309,13 +324,15 @@ struct ocf_mngt_cache_config {
 | 
				
			|||||||
/**
 | 
					/**
 | 
				
			||||||
 * @brief Initialize core config to default values
 | 
					 * @brief Initialize core config to default values
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 | 
					 * @note This function doesn't initialize name field which has no default
 | 
				
			||||||
 | 
					 *       value and is required to be set by user.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 * @param[in] cfg Cache config stucture
 | 
					 * @param[in] cfg Cache config stucture
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
static inline void ocf_mngt_cache_config_set_default(
 | 
					static inline void ocf_mngt_cache_config_set_default(
 | 
				
			||||||
		struct ocf_mngt_cache_config *cfg)
 | 
							struct ocf_mngt_cache_config *cfg)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	cfg->id = OCF_CACHE_ID_INVALID;
 | 
						cfg->id = OCF_CACHE_ID_INVALID;
 | 
				
			||||||
	cfg->name = NULL;
 | 
					 | 
				
			||||||
	cfg->cache_mode = ocf_cache_mode_default;
 | 
						cfg->cache_mode = ocf_cache_mode_default;
 | 
				
			||||||
	cfg->eviction_policy = ocf_eviction_default;
 | 
						cfg->eviction_policy = ocf_eviction_default;
 | 
				
			||||||
	cfg->promotion_policy = ocf_promotion_default;
 | 
						cfg->promotion_policy = ocf_promotion_default;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -726,18 +726,20 @@ static int _ocf_mngt_init_prepare_cache(struct ocf_cache_mngt_init_params *param
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (cfg->name) {
 | 
						/* Check if cache with specified name exists */
 | 
				
			||||||
		ret = env_strncpy(cache_name, sizeof(cache_name) - 1,
 | 
						ret = ocf_mngt_cache_get_by_name(param->ctx, cfg->name, &cache);
 | 
				
			||||||
				cfg->name, sizeof(cache_name) - 1);
 | 
						if (!ret) {
 | 
				
			||||||
		if (ret)
 | 
							ocf_mngt_cache_put(cache);
 | 
				
			||||||
			goto out;
 | 
							/* Cache already exist */
 | 
				
			||||||
	} else {
 | 
							ret = -OCF_ERR_CACHE_EXIST;
 | 
				
			||||||
		ret = snprintf(cache_name, sizeof(cache_name),
 | 
							goto out;
 | 
				
			||||||
				"cache%hu", param->id);
 | 
					 | 
				
			||||||
		if (ret < 0)
 | 
					 | 
				
			||||||
			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);
 | 
						ocf_log(param->ctx, log_info, "Inserting cache %s\n", cache_name);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ret = _ocf_mngt_init_new_cache(param);
 | 
						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)
 | 
						if (cfg->id > OCF_CACHE_ID_MAX)
 | 
				
			||||||
		return -OCF_ERR_INVAL;
 | 
							return -OCF_ERR_INVAL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!cfg->name)
 | 
				
			||||||
 | 
							return -OCF_ERR_INVAL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (!ocf_cache_mode_is_valid(cfg->cache_mode))
 | 
						if (!ocf_cache_mode_is_valid(cfg->cache_mode))
 | 
				
			||||||
		return -OCF_ERR_INVALID_CACHE_MODE;
 | 
							return -OCF_ERR_INVALID_CACHE_MODE;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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;
 | 
						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 void (*ocf_lock_fn_t)(ocf_async_lock_waiter_t waiter);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef int (*ocf_trylock_fn_t)(ocf_async_lock_t lock);
 | 
					typedef int (*ocf_trylock_fn_t)(ocf_async_lock_t lock);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user