diff --git a/lib/bdev/ocf/ctx.c b/lib/bdev/ocf/ctx.c index be1f8ba45e..84734f0d23 100644 --- a/lib/bdev/ocf/ctx.c +++ b/lib/bdev/ocf/ctx.c @@ -288,9 +288,31 @@ vbdev_ocf_ctx_data_secure_erase(ctx_data_t *ctx_data) } } +int vbdev_ocf_queue_create(ocf_cache_t cache, ocf_queue_t *queue, const struct ocf_queue_ops *ops) +{ + int rc; + struct vbdev_ocf_cache_ctx *ctx = ocf_cache_get_priv(cache); + + pthread_mutex_lock(&ctx->lock); + rc = ocf_queue_create(cache, queue, ops); + pthread_mutex_unlock(&ctx->lock); + return rc; +} + +void vbdev_ocf_queue_put(ocf_queue_t queue) +{ + ocf_cache_t cache = ocf_queue_get_cache(queue); + struct vbdev_ocf_cache_ctx *ctx = ocf_cache_get_priv(cache); + + pthread_mutex_lock(&ctx->lock); + ocf_queue_put(queue); + pthread_mutex_unlock(&ctx->lock); +} + void vbdev_ocf_cache_ctx_put(struct vbdev_ocf_cache_ctx *ctx) { if (env_atomic_dec_return(&ctx->refcnt) == 0) { + pthread_mutex_destroy(&ctx->lock); free(ctx); } } diff --git a/lib/bdev/ocf/ctx.h b/lib/bdev/ocf/ctx.h index bc70b07642..2a77e5a53b 100644 --- a/lib/bdev/ocf/ctx.h +++ b/lib/bdev/ocf/ctx.h @@ -44,6 +44,7 @@ extern ocf_ctx_t vbdev_ocf_ctx; /* Context of cache instance */ struct vbdev_ocf_cache_ctx { + pthread_mutex_t lock; env_atomic refcnt; }; @@ -53,4 +54,9 @@ void vbdev_ocf_cache_ctx_get(struct vbdev_ocf_cache_ctx *ctx); int vbdev_ocf_ctx_init(void); void vbdev_ocf_ctx_cleanup(void); +/* Thread safe queue creation and deletion + * These are wrappers for original ocf_queue_create() and ocf_queue_put() */ +int vbdev_ocf_queue_create(ocf_cache_t cache, ocf_queue_t *queue, const struct ocf_queue_ops *ops); +void vbdev_ocf_queue_put(ocf_queue_t queue); + #endif diff --git a/lib/bdev/ocf/vbdev_ocf.c b/lib/bdev/ocf/vbdev_ocf.c index 844f39156b..e9a4cba014 100644 --- a/lib/bdev/ocf/vbdev_ocf.c +++ b/lib/bdev/ocf/vbdev_ocf.c @@ -708,7 +708,7 @@ io_device_create_cb(void *io_device, void *ctx_buf) struct vbdev_ocf_qcxt *qctx = ctx_buf; int rc; - rc = ocf_queue_create(vbdev->ocf_cache, &qctx->queue, &queue_ops); + rc = vbdev_ocf_queue_create(vbdev->ocf_cache, &qctx->queue, &queue_ops); if (rc) { return rc; } @@ -742,7 +742,7 @@ io_device_destroy_cb(void *io_device, void *ctx_buf) spdk_strerror(ENOMEM)); } - ocf_queue_put(qctx->queue); + vbdev_ocf_queue_put(qctx->queue); } static void @@ -854,6 +854,7 @@ start_cache(struct vbdev_ocf *vbdev) } vbdev_ocf_cache_ctx_get(vbdev->cache_ctx); + pthread_mutex_init(&vbdev->cache_ctx->lock, NULL); rc = ocf_mngt_cache_start(vbdev_ocf_ctx, &vbdev->ocf_cache, &vbdev->cfg.cache); if (rc) {