Get cache on queue creation and put it on queue destruction
This prevents situation when cache object is being destroyed
while some queues still exist.
#include "engine/cache_engine.h"
#include "ocf_def_priv.h"
-static void ocf_init_queue(ocf_queue_t q)
-{
- env_atomic_set(&q->io_no, 0);
- env_spinlock_init(&q->io_list_lock);
- INIT_LIST_HEAD(&q->io_list);
- env_atomic_set(&q->ref_count, 1);
-}
-
int ocf_queue_create(ocf_cache_t cache, ocf_queue_t *queue,
const struct ocf_queue_ops *ops)
{
ocf_queue_t tmp_queue;
+ int result;
OCF_CHECK_NULL(cache);
+ result = ocf_mngt_cache_get(cache);
+ if (result)
+ return result;
+
tmp_queue = env_zalloc(sizeof(*tmp_queue), ENV_MEM_NORMAL);
- if (!tmp_queue)
- return -ENOMEM;
+ if (!tmp_queue) {
+ ocf_mngt_cache_put(cache);
+ return -OCF_ERR_NO_MEM;
+ }
+ env_atomic_set(&tmp_queue->io_no, 0);
+ env_spinlock_init(&tmp_queue->io_list_lock);
+ INIT_LIST_HEAD(&tmp_queue->io_list);
+ env_atomic_set(&tmp_queue->ref_count, 1);
tmp_queue->cache = cache;
- ocf_init_queue(tmp_queue);
-
tmp_queue->ops = ops;
list_add(&tmp_queue->list, &cache->io_queues);
@@ -58,6 +59,7 @@ void ocf_queue_put(ocf_queue_t queue)
if (env_atomic_dec_return(&queue->ref_count) == 0) {
list_del(&queue->list);
queue->ops->stop(queue);
+ ocf_mngt_cache_put(queue->cache);
env_free(queue);
}
}
