2 * Copyright(c) 2019 Intel Corporation
3 * SPDX-License-Identifier: BSD-3-Clause-Clear
13 #define PAGE_SIZE 4096
16 * Allocate structure representing data for io operations.
18 ctx_data_t
*ctx_data_alloc(uint32_t pages
)
20 struct volume_data
*data
;
22 data
= malloc(sizeof(*data
));
23 data
->ptr
= malloc(pages
* PAGE_SIZE
);
30 * Free data structure.
32 void ctx_data_free(ctx_data_t
*ctx_data
)
34 struct volume_data
*data
= ctx_data
;
44 * This function is supposed to set protection of data pages against swapping.
45 * Can be non-implemented if not needed.
47 static int ctx_data_mlock(ctx_data_t
*ctx_data
)
53 * Stop protecting data pages against swapping.
55 static void ctx_data_munlock(ctx_data_t
*ctx_data
)
60 * Read data into flat memory buffer.
62 static uint32_t ctx_data_read(void *dst
, ctx_data_t
*src
, uint32_t size
)
64 struct volume_data
*data
= src
;
66 memcpy(dst
, data
->ptr
+ data
->offset
, size
);
72 * Write data from flat memory buffer.
74 static uint32_t ctx_data_write(ctx_data_t
*dst
, const void *src
, uint32_t size
)
76 struct volume_data
*data
= dst
;
78 memcpy(data
->ptr
+ data
->offset
, src
, size
);
84 * Fill data with zeros.
86 static uint32_t ctx_data_zero(ctx_data_t
*dst
, uint32_t size
)
88 struct volume_data
*data
= dst
;
90 memset(data
->ptr
+ data
->offset
, 0, size
);
96 * Perform seek operation on data.
98 static uint32_t ctx_data_seek(ctx_data_t
*dst
, ctx_data_seek_t seek
,
101 struct volume_data
*data
= dst
;
104 case ctx_data_seek_begin
:
105 data
->offset
= offset
;
107 case ctx_data_seek_current
:
108 data
->offset
+= offset
;
116 * Copy data from one structure to another.
118 static uint64_t ctx_data_copy(ctx_data_t
*dst
, ctx_data_t
*src
,
119 uint64_t to
, uint64_t from
, uint64_t bytes
)
121 struct volume_data
*data_dst
= dst
;
122 struct volume_data
*data_src
= src
;
124 memcpy(data_dst
->ptr
+ to
, data_src
->ptr
+ from
, bytes
);
130 * Perform secure erase of data (e.g. fill pages with zeros).
131 * Can be left non-implemented if not needed.
133 static void ctx_data_secure_erase(ctx_data_t
*ctx_data
)
138 * Initialize cleaner thread. Cleaner thread is left non-implemented,
139 * to keep this example as simple as possible.
141 static int ctx_cleaner_init(ocf_cleaner_t c
)
147 * Kick cleaner thread. Cleaner thread is left non-implemented,
148 * to keep this example as simple as possible.
150 static void ctx_cleaner_kick(ocf_cleaner_t c
)
155 * Stop cleaner thread. Cleaner thread is left non-implemented, to keep
156 * this example as simple as possible.
158 static void ctx_cleaner_stop(ocf_cleaner_t c
)
163 * Initialize metadata updater thread. Metadata updater thread is left
164 * non-implemented to keep this example as simple as possible.
166 static int ctx_metadata_updater_init(ocf_metadata_updater_t mu
)
172 * Kick metadata updater thread. Metadata updater thread is left
173 * non-implemented to keep this example as simple as possible.
175 static void ctx_metadata_updater_kick(ocf_metadata_updater_t mu
)
177 ocf_metadata_updater_run(mu
);
181 * Stop metadata updater thread. Metadata updater thread is left
182 * non-implemented to keep this example as simple as possible.
184 static void ctx_metadata_updater_stop(ocf_metadata_updater_t mu
)
189 * Function prividing interface for printing to log used by OCF internals.
190 * It can handle differently messages at varous log levels.
192 static int ctx_logger_print(ocf_logger_t logger
, ocf_logger_lvl_t lvl
,
193 const char *fmt
, va_list args
)
195 FILE *lfile
= stdout
;
203 return vfprintf(lfile
, fmt
, args
);
206 #define CTX_LOG_TRACE_DEPTH 16
209 * Function prividing interface for printing current stack. Used for debugging,
210 * and for providing additional information in log in case of errors.
212 static int ctx_logger_dump_stack(ocf_logger_t logger
)
214 void *trace
[CTX_LOG_TRACE_DEPTH
];
215 char **messages
= NULL
;
218 size
= backtrace(trace
, CTX_LOG_TRACE_DEPTH
);
219 messages
= backtrace_symbols(trace
, size
);
220 printf("[stack trace]>>>\n");
221 for (i
= 0; i
< size
; ++i
)
222 printf("%s\n", messages
[i
]);
223 printf("<<<[stack trace]\n");
230 * This structure describes context config, containing simple context info
231 * and pointers to ops callbacks. Ops are splitted into few categories:
232 * - data ops, providing context specific data handing interface,
233 * - cleaner ops, providing interface to start and stop clener thread,
234 * - metadata updater ops, providing interface for starting, stoping
235 * and kicking metadata updater thread.
236 * - logger ops, providing interface for text message logging
238 static const struct ocf_ctx_config ctx_cfg
= {
239 .name
= "OCF Example",
242 .alloc
= ctx_data_alloc
,
243 .free
= ctx_data_free
,
244 .mlock
= ctx_data_mlock
,
245 .munlock
= ctx_data_munlock
,
246 .read
= ctx_data_read
,
247 .write
= ctx_data_write
,
248 .zero
= ctx_data_zero
,
249 .seek
= ctx_data_seek
,
250 .copy
= ctx_data_copy
,
251 .secure_erase
= ctx_data_secure_erase
,
255 .init
= ctx_cleaner_init
,
256 .kick
= ctx_cleaner_kick
,
257 .stop
= ctx_cleaner_stop
,
260 .metadata_updater
= {
261 .init
= ctx_metadata_updater_init
,
262 .kick
= ctx_metadata_updater_kick
,
263 .stop
= ctx_metadata_updater_stop
,
267 .print
= ctx_logger_print
,
268 .dump_stack
= ctx_logger_dump_stack
,
275 * Function initializing context. Prepares context, sets logger and
276 * registers volume type.
278 int ctx_init(ocf_ctx_t
*ctx
)
282 ret
= ocf_ctx_create(ctx
, &ctx_cfg
);
286 ret
= volume_init(*ctx
);
296 * Function cleaning up context. Unregisters volume type and
297 * deinitializes context.
299 void ctx_cleanup(ocf_ctx_t ctx
)