Reorganize context ops

Signed-off-by: Robert Baldyga <robert.baldyga@intel.com>
This commit is contained in:
Robert Baldyga
2019-01-22 13:14:35 +01:00
parent e5a13ef186
commit 6860cdbd2c
11 changed files with 227 additions and 229 deletions

View File

@@ -110,42 +110,13 @@ int ocf_ctx_data_obj_create(ocf_ctx_t ctx, ocf_data_obj_t *obj,
return ocf_dobj_create(obj, ctx->data_obj_type[type_id], uuid);
}
/*
*
*/
int ocf_ctx_set_logger(ocf_ctx_t ctx, const struct ocf_logger *logger)
{
int ret = 0;
OCF_CHECK_NULL(ctx);
OCF_CHECK_NULL(logger);
env_mutex_lock(&ctx->lock);
if (ctx->logger) {
ret = -EINVAL;
goto out;
}
if (logger->open) {
ret = logger->open(logger);
if (ret)
goto out;
}
ctx->logger = logger;
out:
env_mutex_unlock(&ctx->lock);
return ret;
}
/*
*
*/
int ocf_ctx_init(ocf_ctx_t *ctx, const struct ocf_ctx_ops *ops)
{
ocf_ctx_t ocf_ctx;
int ret;
OCF_CHECK_NULL(ctx);
OCF_CHECK_NULL(ops);
@@ -155,26 +126,36 @@ int ocf_ctx_init(ocf_ctx_t *ctx, const struct ocf_ctx_ops *ops)
return -ENOMEM;
INIT_LIST_HEAD(&ocf_ctx->caches);
if (env_mutex_init(&ocf_ctx->lock)) {
env_free(ocf_ctx);
return -ENOMEM;
}
ret = env_mutex_init(&ocf_ctx->lock);
if (ret)
goto err_ctx;
ocf_ctx->ctx_ops = ops;
ocf_ctx->logger.ops = &ops->logger;
if (ocf_utils_init(ocf_ctx)) {
env_free(ocf_ctx);
return -ENOMEM;
}
ret = ocf_logger_open(&ocf_ctx->logger);
if (ret)
goto err_ctx;
if (ocf_core_data_obj_type_init(ocf_ctx)) {
ocf_utils_deinit(ocf_ctx);
env_free(ocf_ctx);
return -EINVAL;
}
ret = ocf_utils_init(ocf_ctx);
if (ret)
goto err_logger;
ret = ocf_core_data_obj_type_init(ocf_ctx);
if (ret)
goto err_utils;
*ctx = ocf_ctx;
return 0;
err_utils:
ocf_utils_deinit(ocf_ctx);
err_logger:
ocf_logger_close(&ocf_ctx->logger);
err_ctx:
env_free(ocf_ctx);
return ret;
}
/*
@@ -197,8 +178,7 @@ int ocf_ctx_exit(ocf_ctx_t ctx)
ocf_core_data_obj_type_deinit(ctx);
ocf_utils_deinit(ctx);
if (ctx->logger && ctx->logger->close)
ctx->logger->close(ctx->logger);
ocf_logger_close(&ctx->logger);
env_free(ctx);
return 0;

View File

@@ -17,7 +17,7 @@
*/
struct ocf_ctx {
const struct ocf_ctx_ops *ctx_ops;
const struct ocf_logger *logger;
struct ocf_logger logger;
struct ocf_data_obj_type *data_obj_type[OCF_DATA_OBJ_TYPE_MAX];
env_mutex lock;
struct list_head caches;
@@ -32,34 +32,17 @@ struct ocf_ctx {
} resources;
};
#define ocf_log_prefix(ctx, lvl, prefix, fmt, ...) ({ \
int __ocf_log_return_value = 0; \
if (ctx->logger) { \
__ocf_log_return_value = ocf_log_raw(ctx->logger, \
lvl, prefix fmt, ##__VA_ARGS__); \
} \
__ocf_log_return_value; \
})
#define ocf_log_prefix(ctx, lvl, prefix, fmt, ...) \
ocf_log_raw(&ctx->logger, lvl, prefix fmt, ##__VA_ARGS__)
#define ocf_log(ctx, lvl, fmt, ...) \
ocf_log_prefix(ctx, lvl, "", fmt, ##__VA_ARGS__)
#define ocf_log_rl(ctx) ({ \
int __ocf_log_return_value = 0; \
if (ctx->logger) { \
__ocf_log_return_value = ocf_log_raw_rl(ctx->logger, \
__func__); \
} \
__ocf_log_return_value; \
})
#define ocf_log_rl(ctx) \
ocf_log_raw_rl(&ctx->logger, __func__)
#define ocf_log_stack_trace(ctx) ({ \
int __ocf_log_return_value = 0; \
if (ctx->logger) { \
__ocf_log_return_value = ocf_log_stack_trace_raw(ctx->logger); \
} \
__ocf_log_return_value; \
})
#define ocf_log_stack_trace(ctx) \
ocf_log_stack_trace_raw(&ctx->logger)
/**
* @name Environment data buffer operations wrappers
@@ -67,34 +50,34 @@ struct ocf_ctx {
*/
static inline void *ctx_data_alloc(ocf_ctx_t ctx, uint32_t pages)
{
return ctx->ctx_ops->data_alloc(pages);
return ctx->ctx_ops->data.alloc(pages);
}
static inline void ctx_data_free(ocf_ctx_t ctx, ctx_data_t *data)
{
ctx->ctx_ops->data_free(data);
ctx->ctx_ops->data.free(data);
}
static inline int ctx_data_mlock(ocf_ctx_t ctx, ctx_data_t *data)
{
return ctx->ctx_ops->data_mlock(data);
return ctx->ctx_ops->data.mlock(data);
}
static inline void ctx_data_munlock(ocf_ctx_t ctx, ctx_data_t *data)
{
ctx->ctx_ops->data_munlock(data);
ctx->ctx_ops->data.munlock(data);
}
static inline uint32_t ctx_data_rd(ocf_ctx_t ctx, void *dst,
ctx_data_t *src, uint32_t size)
{
return ctx->ctx_ops->data_rd(dst, src, size);
return ctx->ctx_ops->data.read(dst, src, size);
}
static inline uint32_t ctx_data_wr(ocf_ctx_t ctx, ctx_data_t *dst,
const void *src, uint32_t size)
{
return ctx->ctx_ops->data_wr(dst, src, size);
return ctx->ctx_ops->data.write(dst, src, size);
}
static inline void ctx_data_rd_check(ocf_ctx_t ctx, void *dst,
@@ -116,7 +99,7 @@ static inline void ctx_data_wr_check(ocf_ctx_t ctx, ctx_data_t *dst,
static inline uint32_t ctx_data_zero(ocf_ctx_t ctx, ctx_data_t *dst,
uint32_t size)
{
return ctx->ctx_ops->data_zero(dst, size);
return ctx->ctx_ops->data.zero(dst, size);
}
static inline void ctx_data_zero_check(ocf_ctx_t ctx, ctx_data_t *dst,
@@ -130,7 +113,7 @@ static inline void ctx_data_zero_check(ocf_ctx_t ctx, ctx_data_t *dst,
static inline uint32_t ctx_data_seek(ocf_ctx_t ctx, ctx_data_t *dst,
ctx_data_seek_t seek, uint32_t size)
{
return ctx->ctx_ops->data_seek(dst, seek, size);
return ctx->ctx_ops->data.seek(dst, seek, size);
}
static inline void ctx_data_seek_check(ocf_ctx_t ctx, ctx_data_t *dst,
@@ -144,59 +127,59 @@ static inline void ctx_data_seek_check(ocf_ctx_t ctx, ctx_data_t *dst,
static inline uint64_t ctx_data_cpy(ocf_ctx_t ctx, ctx_data_t *dst, ctx_data_t *src,
uint64_t to, uint64_t from, uint64_t bytes)
{
return ctx->ctx_ops->data_cpy(dst, src, to, from, bytes);
return ctx->ctx_ops->data.copy(dst, src, to, from, bytes);
}
static inline void ctx_data_secure_erase(ocf_ctx_t ctx, ctx_data_t *dst)
{
return ctx->ctx_ops->data_secure_erase(dst);
return ctx->ctx_ops->data.secure_erase(dst);
}
static inline int ctx_queue_init(ocf_ctx_t ctx, ocf_queue_t queue)
{
return ctx->ctx_ops->queue_init(queue);
return ctx->ctx_ops->queue.init(queue);
}
static inline void ctx_queue_kick(ocf_ctx_t ctx, ocf_queue_t queue,
bool allow_sync)
{
if (allow_sync && ctx->ctx_ops->queue_kick_sync)
ctx->ctx_ops->queue_kick_sync(queue);
if (allow_sync && ctx->ctx_ops->queue.kick_sync)
ctx->ctx_ops->queue.kick_sync(queue);
else
ctx->ctx_ops->queue_kick(queue);
ctx->ctx_ops->queue.kick(queue);
}
static inline void ctx_queue_stop(ocf_ctx_t ctx, ocf_queue_t queue)
{
ctx->ctx_ops->queue_stop(queue);
ctx->ctx_ops->queue.stop(queue);
}
static inline int ctx_cleaner_init(ocf_ctx_t ctx, ocf_cleaner_t cleaner)
{
return ctx->ctx_ops->cleaner_init(cleaner);
return ctx->ctx_ops->cleaner.init(cleaner);
}
static inline void ctx_cleaner_stop(ocf_ctx_t ctx, ocf_cleaner_t cleaner)
{
ctx->ctx_ops->cleaner_stop(cleaner);
ctx->ctx_ops->cleaner.stop(cleaner);
}
static inline int ctx_metadata_updater_init(ocf_ctx_t ctx,
ocf_metadata_updater_t mu)
{
return ctx->ctx_ops->metadata_updater_init(mu);
return ctx->ctx_ops->metadata_updater.init(mu);
}
static inline void ctx_metadata_updater_kick(ocf_ctx_t ctx,
ocf_metadata_updater_t mu)
{
ctx->ctx_ops->metadata_updater_kick(mu);
ctx->ctx_ops->metadata_updater.kick(mu);
}
static inline void ctx_metadata_updater_stop(ocf_ctx_t ctx,
ocf_metadata_updater_t mu)
{
ctx->ctx_ops->metadata_updater_stop(mu);
ctx->ctx_ops->metadata_updater.stop(mu);
}
/**

View File

@@ -6,40 +6,74 @@
#include "ocf_env.h"
#include "ocf/ocf_logger.h"
#include "ocf_logger_priv.h"
#include "ocf_priv.h"
/*
*
*/
__attribute__((format(printf, 3, 4)))
int ocf_log_raw(const struct ocf_logger *logger, ocf_logger_lvl_t lvl,
int ocf_log_raw(ocf_logger_t logger, ocf_logger_lvl_t lvl,
const char *fmt, ...)
{
va_list args;
int ret;
if (!logger->printf)
if (!logger->ops->printf)
return -ENOTSUP;
va_start(args, fmt);
ret = logger->printf(logger, lvl, fmt, args);
ret = logger->ops->printf(logger, lvl, fmt, args);
va_end(args);
return ret;
}
int ocf_log_raw_rl(const struct ocf_logger *logger, const char *func_name)
int ocf_log_raw_rl(ocf_logger_t logger, const char *func_name)
{
if (!logger->printf_rl)
if (!logger->ops->printf_rl)
return -ENOTSUP;
return logger->printf_rl(func_name);
return logger->ops->printf_rl(logger, func_name);
}
/*
*
*/
int ocf_log_stack_trace_raw(const struct ocf_logger *logger)
int ocf_log_stack_trace_raw(ocf_logger_t logger)
{
return !logger->dump_stack ? -ENOTSUP :
logger->dump_stack(logger);
if (!logger->ops->dump_stack)
return -ENOTSUP;
return logger->ops->dump_stack(logger);
}
int ocf_logger_open(ocf_logger_t logger)
{
if (!logger->ops->open)
return 0;
return logger->ops->open(logger);
}
void ocf_logger_close(ocf_logger_t logger)
{
if (!logger->ops->close)
return;
logger->ops->close(logger);
}
void ocf_logger_set_priv(ocf_logger_t logger, void *priv)
{
OCF_CHECK_NULL(logger);
logger->priv = priv;
}
void *ocf_logger_get_priv(ocf_logger_t logger)
{
OCF_CHECK_NULL(logger);
return logger->priv;
}

View File

@@ -8,13 +8,21 @@
#include "ocf/ocf_logger.h"
struct ocf_logger {
const struct ocf_logger_ops *ops;
void *priv;
};
__attribute__((format(printf, 3, 4)))
int ocf_log_raw(const struct ocf_logger *logger, ocf_logger_lvl_t lvl,
int ocf_log_raw(ocf_logger_t logger, ocf_logger_lvl_t lvl,
const char *fmt, ...);
int ocf_log_raw_rl(const struct ocf_logger *logger, const char *func_name);
int ocf_log_raw_rl(ocf_logger_t logger, const char *func_name);
int ocf_log_stack_trace_raw(const struct ocf_logger *logger);
int ocf_log_stack_trace_raw(ocf_logger_t logger);
int ocf_logger_open(ocf_logger_t logger);
void ocf_logger_close(ocf_logger_t logger);
#endif /* __OCF_LOGGER_PRIV_H__ */