From 6860cdbd2ce865afd5f2f90ec29505cd3a8db692 Mon Sep 17 00:00:00 2001 From: Robert Baldyga Date: Tue, 22 Jan 2019 13:14:35 +0100 Subject: [PATCH] Reorganize context ops Signed-off-by: Robert Baldyga --- example/simple/src/ctx.c | 99 ++++++++------- inc/ocf_ctx.h | 118 ++++++++---------- inc/ocf_logger.h | 19 +-- inc/ocf_types.h | 5 + src/ocf_ctx.c | 70 ++++------- src/ocf_ctx_priv.h | 71 ++++------- src/ocf_logger.c | 52 ++++++-- src/ocf_logger_priv.h | 14 ++- .../metadata/metadata_io.c/metadata_io.c | 4 +- .../_cache_mng_set_cache_mode_test.c | 2 +- ...gt_cache_set_fallback_pt_error_threshold.c | 2 +- 11 files changed, 227 insertions(+), 229 deletions(-) diff --git a/example/simple/src/ctx.c b/example/simple/src/ctx.c index 25c049b..7f9599f 100644 --- a/example/simple/src/ctx.c +++ b/example/simple/src/ctx.c @@ -59,7 +59,7 @@ static void ctx_data_munlock(ctx_data_t *ctx_data) /* * Read data into flat memory buffer. */ -static uint32_t ctx_data_rd(void *dst, ctx_data_t *src, uint32_t size) +static uint32_t ctx_data_read(void *dst, ctx_data_t *src, uint32_t size) { struct dobj_data *data = src; @@ -71,7 +71,7 @@ static uint32_t ctx_data_rd(void *dst, ctx_data_t *src, uint32_t size) /* * Write data from flat memory buffer. */ -static uint32_t ctx_data_wr(ctx_data_t *dst, const void *src, uint32_t size) +static uint32_t ctx_data_write(ctx_data_t *dst, const void *src, uint32_t size) { struct dobj_data *data = dst; @@ -115,7 +115,7 @@ static uint32_t ctx_data_seek(ctx_data_t *dst, ctx_data_seek_t seek, /* * Copy data from one structure to another. */ -static uint64_t ctx_data_cpy(ctx_data_t *dst, ctx_data_t *src, +static uint64_t ctx_data_copy(ctx_data_t *dst, ctx_data_t *src, uint64_t to, uint64_t from, uint64_t bytes) { struct dobj_data *data_dst = dst; @@ -212,48 +212,12 @@ static void ctx_metadata_updater_stop(ocf_metadata_updater_t mu) { } -/* - * This structure describes context ops. They are splitted into few categories: - * - data ops, providing context specific data handing interface, - * - queue ops, providing interface for starting, stoping and kicking - * queue thread in both synchronous and asynchronous way, - * - cleaner ops, providing interface to start and stop clener thread, - * - metadata updater ops, providing interface for starting, stoping - * and kicking metadata updater thread. - */ -static const struct ocf_ctx_ops ctx_ops = { - .name = "OCF Example", - - .data_alloc = ctx_data_alloc, - .data_free = ctx_data_free, - .data_mlock = ctx_data_mlock, - .data_munlock = ctx_data_munlock, - .data_rd = ctx_data_rd, - .data_wr = ctx_data_wr, - .data_zero = ctx_data_zero, - .data_seek = ctx_data_seek, - .data_cpy = ctx_data_cpy, - .data_secure_erase = ctx_data_secure_erase, - - .queue_init = ctx_queue_init, - .queue_kick_sync = ctx_queue_kick_sync, - .queue_kick = ctx_queue_kick_async, - .queue_stop = ctx_queue_stop, - - .cleaner_init = ctx_cleaner_init, - .cleaner_stop = ctx_cleaner_stop, - - .metadata_updater_init = ctx_metadata_updater_init, - .metadata_updater_kick = ctx_metadata_updater_kick, - .metadata_updater_stop = ctx_metadata_updater_stop, -}; - /* * Function prividing interface for printing to log used by OCF internals. * It can handle differently messages at varous log levels. */ -static int ctx_log_printf(const struct ocf_logger *logger, - ocf_logger_lvl_t lvl, const char *fmt, va_list args) +static int ctx_logger_printf(ocf_logger_t logger, ocf_logger_lvl_t lvl, + const char *fmt, va_list args) { FILE *lfile = stdout; @@ -272,7 +236,7 @@ static int ctx_log_printf(const struct ocf_logger *logger, * Function prividing interface for printing current stack. Used for debugging, * and for providing additional information in log in case of errors. */ -static int ctx_log_dump_stack(const struct ocf_logger *logger) +static int ctx_logger_dump_stack(ocf_logger_t logger) { void *trace[CTX_LOG_TRACE_DEPTH]; char **messages = NULL; @@ -290,11 +254,52 @@ static int ctx_log_dump_stack(const struct ocf_logger *logger) } /* - * Structure containng logger ops. + * This structure describes context ops. They are splitted into few categories: + * - data ops, providing context specific data handing interface, + * - queue ops, providing interface for starting, stoping and kicking + * queue thread in both synchronous and asynchronous way, + * - cleaner ops, providing interface to start and stop clener thread, + * - metadata updater ops, providing interface for starting, stoping + * and kicking metadata updater thread. */ -static const struct ocf_logger logger = { - .printf = ctx_log_printf, - .dump_stack = ctx_log_dump_stack, +static const struct ocf_ctx_ops ctx_ops = { + .name = "OCF Example", + + .data = { + .alloc = ctx_data_alloc, + .free = ctx_data_free, + .mlock = ctx_data_mlock, + .munlock = ctx_data_munlock, + .read = ctx_data_read, + .write = ctx_data_write, + .zero = ctx_data_zero, + .seek = ctx_data_seek, + .copy = ctx_data_copy, + .secure_erase = ctx_data_secure_erase, + }, + + .queue = { + .init = ctx_queue_init, + .kick_sync = ctx_queue_kick_sync, + .kick = ctx_queue_kick_async, + .stop = ctx_queue_stop, + }, + + .cleaner = { + .init = ctx_cleaner_init, + .stop = ctx_cleaner_stop, + }, + + .metadata_updater = { + .init = ctx_metadata_updater_init, + .kick = ctx_metadata_updater_kick, + .stop = ctx_metadata_updater_stop, + }, + + .logger = { + .printf = ctx_logger_printf, + .dump_stack = ctx_logger_dump_stack, + }, }; /* @@ -309,8 +314,6 @@ int ctx_init(ocf_ctx_t *ctx) if (ret) return ret; - ocf_ctx_set_logger(*ctx, &logger); - ret = dobj_init(*ctx); if (ret) { ocf_ctx_exit(*ctx); diff --git a/inc/ocf_ctx.h b/inc/ocf_ctx.h index b48bff9..e331405 100644 --- a/inc/ocf_ctx.h +++ b/inc/ocf_ctx.h @@ -26,20 +26,9 @@ typedef enum { } ctx_data_seek_t; /** - * @brief OCF context specific operation + * @brief Context data representation ops */ -struct ocf_ctx_ops { - /** - * @brief The name of the environment which provides platform - * interface for cache engine - */ - const char *name; - - /** - * @name Context data buffer operations - * @{ - */ - +struct ocf_data_ops { /** * @brief Allocate contest data buffer * @@ -47,14 +36,14 @@ struct ocf_ctx_ops { * * @return Context data buffer */ - ctx_data_t *(*data_alloc)(uint32_t pages); + ctx_data_t *(*alloc)(uint32_t pages); /** * @brief Free context data buffer * * @param[in] data Contex data buffer which shall be freed */ - void (*data_free)(ctx_data_t *data); + void (*free)(ctx_data_t *data); /** * @brief Lock context data buffer to disable swap-out @@ -64,14 +53,14 @@ struct ocf_ctx_ops { * @retval 0 Memory locked successfully * @retval Non-zero Memory locking failure */ - int (*data_mlock)(ctx_data_t *data); + int (*mlock)(ctx_data_t *data); /** * @brief Unlock context data buffer * * @param[in] data Contex data buffer which shall be unlocked */ - void (*data_munlock)(ctx_data_t *data); + void (*munlock)(ctx_data_t *data); /** * @brief Read from environment data buffer into raw data buffer @@ -82,7 +71,7 @@ struct ocf_ctx_ops { * * @return Number of read bytes */ - uint32_t (*data_rd)(void *dst, ctx_data_t *src, uint32_t size); + uint32_t (*read)(void *dst, ctx_data_t *src, uint32_t size); /** * @brief Write raw data buffer into context data buffer @@ -93,7 +82,7 @@ struct ocf_ctx_ops { * * @return Number of written bytes */ - uint32_t (*data_wr)(ctx_data_t *dst, const void *src, uint32_t size); + uint32_t (*write)(ctx_data_t *dst, const void *src, uint32_t size); /** * @brief Zero context data buffer @@ -103,7 +92,7 @@ struct ocf_ctx_ops { * * @return Number of zeroed bytes */ - uint32_t (*data_zero)(ctx_data_t *dst, uint32_t size); + uint32_t (*zero)(ctx_data_t *dst, uint32_t size); /** * @brief Seek read/write head in context data buffer for specified @@ -115,8 +104,7 @@ struct ocf_ctx_ops { * * @return Number of seek bytes */ - uint32_t (*data_seek)(ctx_data_t *dst, - ctx_data_seek_t seek, uint32_t size); + uint32_t (*seek)(ctx_data_t *dst, ctx_data_seek_t seek, uint32_t size); /** * @brief Copy context data buffer content @@ -129,7 +117,7 @@ struct ocf_ctx_ops { * * @return Number of bytes copied */ - uint64_t (*data_cpy)(ctx_data_t *dst, ctx_data_t *src, + uint64_t (*copy)(ctx_data_t *dst, ctx_data_t *src, uint64_t to, uint64_t from, uint64_t bytes); /** @@ -137,16 +125,13 @@ struct ocf_ctx_ops { * * @param[in] dst Contex data buffer which shall be erased */ - void (*data_secure_erase)(ctx_data_t *dst); + void (*secure_erase)(ctx_data_t *dst); +}; - /** - * @} - */ - - /** - * @name I/O queue operations - * @{ - */ +/** + * @brief I/O queue operations + */ +struct ocf_queue_ops { /** * @brief Initialize I/O queue. * @@ -158,7 +143,7 @@ struct ocf_ctx_ops { * @retval 0 I/O queue has been initializaed successfully * @retval Non-zero I/O queue initialization failure */ - int (*queue_init)(ocf_queue_t q); + int (*init)(ocf_queue_t q); /** * @brief Kick I/O queue processing @@ -169,7 +154,7 @@ struct ocf_ctx_ops { * * @param[in] q I/O queue to be kicked */ - void (*queue_kick)(ocf_queue_t q); + void (*kick)(ocf_queue_t q); /** * @brief Kick I/O queue processing @@ -181,23 +166,20 @@ struct ocf_ctx_ops { * * @param[in] q I/O queue to be kicked */ - void (*queue_kick_sync)(ocf_queue_t q); + void (*kick_sync)(ocf_queue_t q); /** * @brief Stop I/O queue * * @param[in] q I/O queue beeing stopped */ - void (*queue_stop)(ocf_queue_t q); + void (*stop)(ocf_queue_t q); +}; - /** - * @} - */ - - /** - * @name Cleaner operations - * @{ - */ +/** + * @brief Cleaner operations + */ +struct ocf_cleaner_ops { /** * @brief Initialize cleaner. * @@ -209,23 +191,20 @@ struct ocf_ctx_ops { * @retval 0 Cleaner has been initializaed successfully * @retval Non-zero Cleaner initialization failure */ - int (*cleaner_init)(ocf_cleaner_t c); + int (*init)(ocf_cleaner_t c); /** * @brief Stop cleaner * * @param[in] c Descriptor of cleaner beeing stopped */ - void (*cleaner_stop)(ocf_cleaner_t c); + void (*stop)(ocf_cleaner_t c); +}; - /** - * @} - */ - - /** - * @name Metadata updater operations - * @{ - */ +/** + * @brief Metadata updater operations + */ +struct ocf_metadata_updater_ops { /** * @brief Initialize metadata updater. * @@ -237,7 +216,7 @@ struct ocf_ctx_ops { * @retval 0 Metadata updater has been initializaed successfully * @retval Non-zero I/O queue initialization failure */ - int (*metadata_updater_init)(ocf_metadata_updater_t mu); + int (*init)(ocf_metadata_updater_t mu); /** * @brief Kick metadata updater processing @@ -247,18 +226,31 @@ struct ocf_ctx_ops { * * @param[in] mu Metadata updater to be kicked */ - void (*metadata_updater_kick)(ocf_metadata_updater_t mu); + void (*kick)(ocf_metadata_updater_t mu); /** * @brief Stop metadata updater * * @param[in] mu Metadata updater beeing stopped */ - void (*metadata_updater_stop)(ocf_metadata_updater_t mu); + void (*stop)(ocf_metadata_updater_t mu); +}; +/** + * @brief OCF context specific operation + */ +struct ocf_ctx_ops { /** - * @} + * @brief The name of the environment which provides platform + * interface for cache engine */ + const char *name; + + struct ocf_data_ops data; + struct ocf_queue_ops queue; + struct ocf_cleaner_ops cleaner; + struct ocf_metadata_updater_ops metadata_updater; + struct ocf_logger_ops logger; }; /** @@ -322,16 +314,6 @@ int ocf_ctx_get_data_obj_type_id(ocf_ctx_t ctx, ocf_data_obj_type_t type); int ocf_ctx_data_obj_create(ocf_ctx_t ctx, ocf_data_obj_t *obj, struct ocf_data_obj_uuid *uuid, uint8_t type_id); -/** - * @brief Set OCF context logger - * - * @param[in] ctx OCF context - * @param[in] logger Structure describing logger - * - * @return Zero when success, otherwise an error - */ -int ocf_ctx_set_logger(ocf_ctx_t ctx, const struct ocf_logger *logger); - /** * @brief Initialize OCF context * diff --git a/inc/ocf_logger.h b/inc/ocf_logger.h index 7ce9ca7..1cbf149 100644 --- a/inc/ocf_logger.h +++ b/inc/ocf_logger.h @@ -11,6 +11,7 @@ * @brief Logger API */ +#include #include /** @@ -27,15 +28,17 @@ typedef enum { log_debug, } ocf_logger_lvl_t; -struct ocf_logger { - int (*open)(const struct ocf_logger *logger); - void (*close)(const struct ocf_logger *logger); - int (*printf)(const struct ocf_logger *logger, ocf_logger_lvl_t lvl, +struct ocf_logger_ops { + int (*open)(ocf_logger_t logger); + void (*close)(ocf_logger_t logger); + int (*printf)(ocf_logger_t logger, ocf_logger_lvl_t lvl, const char *fmt, va_list args); - int (*printf_rl)(const char *func_name); - int (*dump_stack)(const struct ocf_logger *logger); - - void *priv; + int (*printf_rl)(ocf_logger_t logger, const char *func_name); + int (*dump_stack)(ocf_logger_t logger); }; +void ocf_logger_set_priv(ocf_logger_t logger, void *priv); + +void *ocf_logger_get_priv(ocf_logger_t logger); + #endif /* __OCF_LOGGER_H__ */ diff --git a/inc/ocf_types.h b/inc/ocf_types.h index 551bafb..b000e13 100644 --- a/inc/ocf_types.h +++ b/inc/ocf_types.h @@ -92,4 +92,9 @@ typedef struct ocf_cleaner *ocf_cleaner_t; */ typedef struct ocf_metadata_updater *ocf_metadata_updater_t; +/** + * @brief handle to logger + */ +typedef struct ocf_logger *ocf_logger_t; + #endif diff --git a/src/ocf_ctx.c b/src/ocf_ctx.c index ae9cdae..afa9a2d 100644 --- a/src/ocf_ctx.c +++ b/src/ocf_ctx.c @@ -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; diff --git a/src/ocf_ctx_priv.h b/src/ocf_ctx_priv.h index 3703857..75902c2 100644 --- a/src/ocf_ctx_priv.h +++ b/src/ocf_ctx_priv.h @@ -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); } /** diff --git a/src/ocf_logger.c b/src/ocf_logger.c index 6250ab2..0acbbe5 100644 --- a/src/ocf_logger.c +++ b/src/ocf_logger.c @@ -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; +} + diff --git a/src/ocf_logger_priv.h b/src/ocf_logger_priv.h index 2fec971..d26929d 100644 --- a/src/ocf_logger_priv.h +++ b/src/ocf_logger_priv.h @@ -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__ */ diff --git a/tests/unit/tests/metadata/metadata_io.c/metadata_io.c b/tests/unit/tests/metadata/metadata_io.c/metadata_io.c index 7c5d31b..73e9fec 100644 --- a/tests/unit/tests/metadata/metadata_io.c/metadata_io.c +++ b/tests/unit/tests/metadata/metadata_io.c/metadata_io.c @@ -58,8 +58,8 @@ ocf_ctx_t __wrap_ocf_cache_get_ctx(ocf_cache_t cache) { } -int __wrap_ocf_log_raw(const struct ocf_logger *logger, ocf_logger_lvl_t lvl, - const char *fmt, ...) +int __wrap_ocf_log_raw(ocf_logger_t logger, ocf_logger_lvl_t lvl, + const char *fmt, ...) { } diff --git a/tests/unit/tests/mngt/ocf_mngt_cache.c/_cache_mng_set_cache_mode_test.c b/tests/unit/tests/mngt/ocf_mngt_cache.c/_cache_mng_set_cache_mode_test.c index 109569f..f8bdd8e 100644 --- a/tests/unit/tests/mngt/ocf_mngt_cache.c/_cache_mng_set_cache_mode_test.c +++ b/tests/unit/tests/mngt/ocf_mngt_cache.c/_cache_mng_set_cache_mode_test.c @@ -58,7 +58,7 @@ ocf_ctx_t __wrap_ocf_cache_get_ctx(ocf_cache_t cache) return cache->owner; } -int __wrap_ocf_log_raw(const struct ocf_logger *logger, ocf_logger_lvl_t lvl, +int __wrap_ocf_log_raw(ocf_logger_t logger, ocf_logger_lvl_t lvl, const char *fmt, ...) { function_called(); diff --git a/tests/unit/tests/mngt/ocf_mngt_cache.c/ocf_mngt_cache_set_fallback_pt_error_threshold.c b/tests/unit/tests/mngt/ocf_mngt_cache.c/ocf_mngt_cache_set_fallback_pt_error_threshold.c index d714068..aaf9d1f 100644 --- a/tests/unit/tests/mngt/ocf_mngt_cache.c/ocf_mngt_cache_set_fallback_pt_error_threshold.c +++ b/tests/unit/tests/mngt/ocf_mngt_cache.c/ocf_mngt_cache_set_fallback_pt_error_threshold.c @@ -35,7 +35,7 @@ #include "../ocf_ctx_priv.h" #include "../cleaning/cleaning.h" -int __wrap_ocf_log_raw(const struct ocf_logger *logger, ocf_logger_lvl_t lvl, +int __wrap_ocf_log_raw(ocf_logger_t logger, ocf_logger_lvl_t lvl, const char *fmt, ...) { function_called();