Merge pull request #66 from robertbaldyga/metadata-probe-asynchronous

Make metadata probe asynchronous
This commit is contained in:
Michal Rakowski 2019-03-04 12:26:34 +01:00 committed by GitHub
commit 794b008127
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 245 additions and 222 deletions

View File

@ -51,20 +51,37 @@ struct ocf_atomic_metadata {
int ocf_metadata_get_atomic_entry(ocf_cache_t cache, uint64_t addr, int ocf_metadata_get_atomic_entry(ocf_cache_t cache, uint64_t addr,
struct ocf_atomic_metadata *entry); struct ocf_atomic_metadata *entry);
/**
* @brief Metadata probe status
*/
struct ocf_metadata_probe_status {
/** Cache was graceful stopped */
bool clean_shutdown;
/** Cache contains dirty data */
bool cache_dirty;
};
/**
* @brief Metadata probe completion callback
*
* @param[in] priv Completion context
* @param[in] error Error code (zero on success)
* @param[in] status Structure describing metadata probe status
*/
typedef void (*ocf_metadata_probe_end_t)(void *priv, int error,
struct ocf_metadata_probe_status *status);
/** /**
* @brief Probe cache device * @brief Probe cache device
* *
* @param[in] ctx handle to object designating ocf context * @param[in] ctx handle to object designating ocf context
* @param[in] cache_volume Cache volume * @param[in] volume Cache volume
* @param[out] clean_shutdown Cache was graceful stopped * @param[in] cmpl Completion callback
* @param[out] cache_dirty Cache is dirty * @param[in] priv Completion context
*
* @retval 0 Probe successfully performed
* @retval -ENODATA Cache has not been detected
* @retval Non-zero ERROR
*/ */
int ocf_metadata_probe(ocf_ctx_t ctx, ocf_volume_t cache_volume, void ocf_metadata_probe(ocf_ctx_t ctx, ocf_volume_t volume,
bool *clean_shutdown, bool *cache_dirty); ocf_metadata_probe_end_t cmpl, void *priv);
/** /**
* @brief Check if sectors in cache line before given address are invalid * @brief Check if sectors in cache line before given address are invalid

View File

@ -152,58 +152,74 @@ void ocf_metadata_flush_do_asynch(struct ocf_cache *cache,
cache->metadata.iface.flush_do_asynch(cache, req, complete); cache->metadata.iface.flush_do_asynch(cache, req, complete);
} }
static inline int ocf_metadata_check_properties(void) struct ocf_metadata_read_sb_ctx;
typedef void (*ocf_metadata_read_sb_end_t)(
struct ocf_metadata_read_sb_ctx *context);
struct ocf_metadata_read_sb_ctx {
struct ocf_superblock_config superblock;
ocf_metadata_read_sb_end_t cmpl;
ocf_ctx_t ctx;
void *priv1;
void *priv2;
int error;
};
static void ocf_metadata_read_sb_complete(struct ocf_io *io, int error)
{ {
uint32_t field_offset; struct ocf_metadata_read_sb_ctx *context = io->priv1;
ctx_data_t *data = ocf_io_get_data(io);
/* Because metadata basic properties are on the beginning of super block if (!error) {
* read/write only first page of supper block. /* Read data from data into super block buffer */
* ctx_data_rd_check(context->ctx, &context->superblock, data,
* For safety reason check if offset of metadata properties are in first sizeof(context->superblock));
* page of super block.
*
* Maybe in future super block fields order may be changed and metadata
* variant may go out first page of super block
*/
field_offset = offsetof(struct ocf_superblock_config, line_size);
ENV_BUG_ON(field_offset >= PAGE_SIZE);
/* The same checking for magic number */
field_offset = offsetof(struct ocf_superblock_config, magic_number);
ENV_BUG_ON(field_offset >= PAGE_SIZE);
/* The same checking for IO interface type */
field_offset = offsetof(struct ocf_superblock_config, cache_mode);
ENV_BUG_ON(field_offset >= PAGE_SIZE);
/* And the same for version location within superblock structure */
field_offset = offsetof(struct ocf_superblock_config, metadata_version);
ENV_BUG_ON(field_offset >= PAGE_SIZE);
return 0;
} }
static int ocf_metadata_read_properties(ocf_ctx_t ctx,
ocf_volume_t cache_volume, context->error = error;
struct ocf_superblock_config *superblock) context->cmpl(context);
ctx_data_free(context->ctx, data);
ocf_io_put(io);
env_free(context);
}
static int ocf_metadata_read_sb(ocf_ctx_t ctx, ocf_volume_t volume,
ocf_metadata_read_sb_end_t cmpl, void *priv1, void *priv2)
{ {
struct ocf_metadata_read_sb_ctx *context;
size_t sb_pages = BYTES_TO_PAGES(sizeof(context->superblock));
ctx_data_t *data; ctx_data_t *data;
struct ocf_io *io; struct ocf_io *io;
int result = 0; int result = 0;
if (ocf_metadata_check_properties()) /* Allocate memory for first page of super block */
return -EINVAL; context = env_zalloc(sizeof(*context), ENV_MEM_NORMAL);
if (!context) {
ocf_log(ctx, log_err, "Memory allocation error");
return -ENOMEM;
}
context->cmpl = cmpl;
context->ctx = ctx;
context->priv1 = priv1;
context->priv2 = priv2;
/* Allocate resources for IO */ /* Allocate resources for IO */
io = ocf_volume_new_io(cache_volume); io = ocf_volume_new_io(volume);
data = ctx_data_alloc(ctx, 1); if (!io) {
/* Check allocation result */
if (!io || !data) {
ocf_log(ctx, log_err, "Memory allocation error"); ocf_log(ctx, log_err, "Memory allocation error");
result = -ENOMEM; result = -ENOMEM;
goto out; goto err_io;
}
data = ctx_data_alloc(ctx, sb_pages);
if (!data) {
ocf_log(ctx, log_err, "Memory allocation error");
result = -ENOMEM;
goto err_data;
} }
/* /*
@ -214,176 +230,142 @@ static int ocf_metadata_read_properties(ocf_ctx_t ctx,
if (result) { if (result) {
ocf_log(ctx, log_err, "Metadata IO configuration error\n"); ocf_log(ctx, log_err, "Metadata IO configuration error\n");
result = -EIO; result = -EIO;
goto out; goto err_set_data;
}
ocf_io_configure(io, 0, PAGE_SIZE, OCF_READ, 0, 0);
result = ocf_submit_io_wait(io);
if (result) {
ocf_log(ctx, log_err, "Metadata IO request submit error\n");
result = -EIO;
goto out;
} }
/* Read data from data into super block buffer */ ocf_io_configure(io, 0, sb_pages * PAGE_SIZE, OCF_READ, 0, 0);
ctx_data_rd_check(ctx, superblock, data,
PAGE_SIZE);
out: ocf_io_set_cmpl(io, context, NULL, ocf_metadata_read_sb_complete);
if (io) ocf_volume_submit_io(io);
ocf_io_put(io);
return 0;
err_set_data:
ctx_data_free(ctx, data); ctx_data_free(ctx, data);
err_data:
ocf_io_put(io);
err_io:
env_free(context);
return result; return result;
} }
/** static void ocf_metadata_load_properties_cmpl(
* @brief function loads individual properties from metadata set struct ocf_metadata_read_sb_ctx *context)
* @param cache_volume volume from which to load metadata
* @param variant - field to which save metadata variant; if NULL,
* metadata variant won't be read.
* @param cache mode; if NULL is passed it won't be read
* @param shutdown_status - dirty shutdown or clean shutdown
* @param dirty_flushed - if all dirty data was flushed prior to closing
* the cache
* @return 0 upon successful completion
*/
int ocf_metadata_load_properties(ocf_volume_t cache_volume,
ocf_cache_line_size_t *line_size,
ocf_metadata_layout_t *layout,
ocf_cache_mode_t *cache_mode,
enum ocf_metadata_shutdown_status *shutdown_status,
uint8_t *dirty_flushed)
{ {
struct ocf_superblock_config *superblock; struct ocf_metadata_load_properties properties;
int err_value = 0; struct ocf_superblock_config *superblock = &context->superblock;
ocf_metadata_load_properties_end_t cmpl = context->priv1;
/* Allocate first page of super block */ void *priv = context->priv2;
superblock = env_zalloc(PAGE_SIZE, ENV_MEM_NORMAL); ocf_ctx_t ctx = context->ctx;
if (!superblock) {
ocf_cache_log(cache_volume->cache, log_err,
"Allocation memory error");
return -ENOMEM;
}
OCF_DEBUG_TRACE(cache);
err_value = ocf_metadata_read_properties(cache_volume->cache->owner,
cache_volume, superblock);
if (err_value)
goto ocf_metadata_load_variant_ERROR;
if (superblock->magic_number != CACHE_MAGIC_NUMBER) { if (superblock->magic_number != CACHE_MAGIC_NUMBER) {
err_value = -ENODATA; ocf_log(ctx, log_info, "Cannot detect pre-existing metadata\n");
ocf_cache_log(cache_volume->cache, log_info, cmpl(priv, -ENODATA, NULL);
"Can not detect pre-existing metadata\n"); return;
goto ocf_metadata_load_variant_ERROR;
} }
if (METADATA_VERSION() != superblock->metadata_version) { if (METADATA_VERSION() != superblock->metadata_version) {
err_value = -EBADF; ocf_log(ctx, log_err, "Metadata version mismatch!\n");
ocf_cache_log(cache_volume->cache, log_err, cmpl(priv, -EBADF, NULL);
"Metadata version mismatch!\n"); return;
goto ocf_metadata_load_variant_ERROR;
} }
if (line_size) { if (!ocf_cache_line_size_is_valid(superblock->line_size)) {
if (ocf_cache_line_size_is_valid(superblock->line_size)) { ocf_log(ctx, log_err, "ERROR: Invalid cache line size!\n");
*line_size = superblock->line_size; cmpl(priv, -EINVAL, NULL);
} else { return;
err_value = -EINVAL;
ocf_cache_log(cache_volume->cache, log_err,
"ERROR: Invalid cache line size!\n");
}
} }
if (layout) { if ((unsigned)superblock->metadata_layout >= ocf_metadata_layout_max) {
if (superblock->metadata_layout >= ocf_metadata_layout_max || ocf_log(ctx, log_err, "ERROR: Invalid metadata layout!\n");
superblock->metadata_layout < 0) { cmpl(priv, -EINVAL, NULL);
err_value = -EINVAL; return;
ocf_cache_log(cache_volume->cache, log_err,
"ERROR: Invalid metadata layout!\n");
} else {
*layout = superblock->metadata_layout;
}
} }
if (cache_mode) { if (superblock->cache_mode >= ocf_cache_mode_max) {
if (superblock->cache_mode < ocf_cache_mode_max) { ocf_log(ctx, log_err, "ERROR: Invalid cache mode!\n");
*cache_mode = superblock->cache_mode; cmpl(priv, -EINVAL, NULL);
} else { return;
ocf_cache_log(cache_volume->cache, log_err,
"ERROR: Invalid cache mode!\n");
err_value = -EINVAL;
}
} }
if (shutdown_status != NULL) { if (superblock->clean_shutdown > ocf_metadata_clean_shutdown) {
if (superblock->clean_shutdown <= ocf_metadata_clean_shutdown) { ocf_log(ctx, log_err, "ERROR: Invalid shutdown status!\n");
*shutdown_status = superblock->clean_shutdown; cmpl(priv, -EINVAL, NULL);
} else { return;
ocf_cache_log(cache_volume->cache, log_err,
"ERROR: Invalid shutdown status!\n");
err_value = -EINVAL;
}
} }
if (dirty_flushed != NULL) { if (superblock->dirty_flushed > DIRTY_FLUSHED) {
if (superblock->dirty_flushed <= DIRTY_FLUSHED) { ocf_log(ctx, log_err, "ERROR: Invalid flush status!\n");
*dirty_flushed = superblock->dirty_flushed; cmpl(priv, -EINVAL, NULL);
} else { return;
ocf_cache_log(cache_volume->cache, log_err,
"ERROR: Invalid flush status!\n");
err_value = -EINVAL;
}
} }
ocf_metadata_load_variant_ERROR: properties.line_size = superblock->line_size;
properties.layout = superblock->metadata_layout;
properties.cache_mode = superblock->cache_mode;
properties.shutdown_status = superblock->clean_shutdown;
properties.dirty_flushed = superblock->dirty_flushed;
env_free(superblock); cmpl(priv, 0, &properties);
return err_value;
} }
int ocf_metadata_probe(ocf_ctx_t ctx, ocf_volume_t cache_volume, void ocf_metadata_load_properties(ocf_volume_t volume,
bool *clean_shutdown, bool *cache_dirty) ocf_metadata_load_properties_end_t cmpl, void *priv)
{ {
struct ocf_superblock_config *superblock; int result;
int result = 0;
OCF_CHECK_NULL(ctx);
OCF_CHECK_NULL(cache_volume);
/* Allocate first page of super block */
superblock = env_zalloc(PAGE_SIZE, ENV_MEM_NORMAL);
if (!superblock) {
ocf_log(ctx, log_err, "Allocation memory error");
return -ENOMEM;
}
OCF_DEBUG_TRACE(cache); OCF_DEBUG_TRACE(cache);
result = ocf_metadata_read_properties(ctx, cache_volume, superblock); result = ocf_metadata_read_sb(volume->cache->owner, volume,
ocf_metadata_load_properties_cmpl, cmpl, priv);
if (result) if (result)
goto ocf_metadata_probe_END; cmpl(priv, result, NULL);
}
static void ocf_metadata_probe_cmpl(struct ocf_metadata_read_sb_ctx *context)
{
struct ocf_metadata_probe_status status;
struct ocf_superblock_config *superblock = &context->superblock;
ocf_metadata_probe_end_t cmpl = context->priv1;
void *priv = context->priv2;
if (superblock->magic_number != CACHE_MAGIC_NUMBER) { if (superblock->magic_number != CACHE_MAGIC_NUMBER) {
result = -ENODATA; cmpl(priv, -ENODATA, NULL);
goto ocf_metadata_probe_END; return;
} }
if (clean_shutdown != NULL) { if (METADATA_VERSION() != superblock->metadata_version) {
*clean_shutdown = (superblock->clean_shutdown != cmpl(priv, -EBADF, NULL);
return;
}
if (superblock->clean_shutdown > ocf_metadata_clean_shutdown) {
cmpl(priv, -EINVAL, NULL);
return;
}
if (superblock->dirty_flushed > DIRTY_FLUSHED) {
cmpl(priv, -EINVAL, NULL);
return;
}
status.clean_shutdown = (superblock->clean_shutdown !=
ocf_metadata_dirty_shutdown); ocf_metadata_dirty_shutdown);
status.cache_dirty = (superblock->dirty_flushed == DIRTY_NOT_FLUSHED);
cmpl(priv, 0, &status);
} }
if (cache_dirty != NULL) void ocf_metadata_probe(ocf_ctx_t ctx, ocf_volume_t volume,
*cache_dirty = (superblock->dirty_flushed == DIRTY_NOT_FLUSHED); ocf_metadata_probe_end_t cmpl, void *priv)
{
int result;
if (METADATA_VERSION() != superblock->metadata_version) OCF_CHECK_NULL(ctx);
result = -EBADF; OCF_CHECK_NULL(volume);
ocf_metadata_probe_END: result = ocf_metadata_read_sb(ctx, volume, ocf_metadata_probe_cmpl,
cmpl, priv);
env_free(superblock); if (result)
return result; cmpl(priv, result, NULL);
} }

View File

@ -306,12 +306,19 @@ static inline ocf_cache_line_t ocf_metadata_entries_hash(
return cache->metadata.iface.entries_hash(cache); return cache->metadata.iface.entries_hash(cache);
} }
int ocf_metadata_load_properties(ocf_volume_t cache_volume, struct ocf_metadata_load_properties {
ocf_cache_line_size_t *line_size, enum ocf_metadata_shutdown_status shutdown_status;
ocf_metadata_layout_t *layout, uint8_t dirty_flushed;
ocf_cache_mode_t *cache_mode, ocf_metadata_layout_t layout;
enum ocf_metadata_shutdown_status *shutdown_status, ocf_cache_line_size_t line_size;
uint8_t *dirty_flushed); ocf_cache_mode_t cache_mode;
};
typedef void (*ocf_metadata_load_properties_end_t)(void *priv, int error,
struct ocf_metadata_load_properties *properties);
void ocf_metadata_load_properties(ocf_volume_t volume,
ocf_metadata_load_properties_end_t cmpl, void *priv);
/** /**
* @brief Validate cache line size * @brief Validate cache line size

View File

@ -863,57 +863,75 @@ end:
return ret; return ret;
} }
struct _ocf_mngt_load_properties_context {
struct ocf_cachemng_attach_params *params;
env_completion complete;
};
static void _ocf_mngt_load_properties_end(void *priv, int error,
struct ocf_metadata_load_properties *properties)
{
struct _ocf_mngt_load_properties_context *context = priv;
struct ocf_cachemng_attach_params *params = context->params;
ocf_cache_t cache = params->cache;
if (error)
goto out;
params->metadata.shutdown_status = properties->shutdown_status;
params->metadata.dirty_flushed = properties->shutdown_status;
if (cache->device->init_mode == ocf_init_mode_load) {
params->metadata.line_size = properties->line_size;
cache->conf_meta->metadata_layout = properties->layout;
cache->conf_meta->cache_mode = properties->cache_mode;
}
out:
params->metadata.status = error;
env_completion_complete(&context->complete);
}
/** /**
* Prepare metadata accordingly to mode (for load/recovery read from disk) * Prepare metadata accordingly to mode (for load/recovery read from disk)
*/ */
static int _ocf_mngt_init_prepare_metadata( static int _ocf_mngt_init_prepare_metadata(
struct ocf_cachemng_attach_params *attach_params) struct ocf_cachemng_attach_params *params)
{ {
int ret; struct _ocf_mngt_load_properties_context context;
int i; ocf_cache_t cache = params->cache;
ocf_cache_t cache = attach_params->cache; int ret, i;
ocf_cache_line_size_t line_size = attach_params->metadata.line_size ?
attach_params->metadata.line_size : context.params = params;
cache->metadata.settings.size; env_completion_init(&context.complete);
OCF_ASSERT_PLUGGED(cache); OCF_ASSERT_PLUGGED(cache);
if (cache->device->init_mode != ocf_init_mode_metadata_volatile) { if (cache->device->init_mode != ocf_init_mode_metadata_volatile) {
if (cache->device->init_mode == ocf_init_mode_load) { ocf_metadata_load_properties(&cache->device->volume,
attach_params->metadata.status = ocf_metadata_load_properties( _ocf_mngt_load_properties_end, &context);
&cache->device->volume,
&line_size, env_completion_wait(&context.complete);
&cache->conf_meta->metadata_layout, if (params->load && params->metadata.status) {
&cache->conf_meta->cache_mode,
&attach_params->metadata.shutdown_status,
&attach_params->metadata.dirty_flushed);
if (attach_params->metadata.status) {
ret = -OCF_ERR_START_CACHE_FAIL; ret = -OCF_ERR_START_CACHE_FAIL;
return ret; return ret;
} }
} else {
attach_params->metadata.status = ocf_metadata_load_properties(
&cache->device->volume,
NULL, NULL, NULL,
&attach_params->metadata.shutdown_status,
&attach_params->metadata.dirty_flushed);
/* don't handle result; if no valid metadata is present
* on caching device, we are about to use, it's not an issue
*/
}
} }
params->metadata.line_size = params->metadata.line_size ?:
cache->metadata.settings.size;
/* /*
* Initialize variable size metadata segments * Initialize variable size metadata segments
*/ */
if (ocf_metadata_init_variable_size(cache, attach_params->device_size, if (ocf_metadata_init_variable_size(cache, params->device_size,
line_size, params->metadata.line_size,
cache->conf_meta->metadata_layout)) { cache->conf_meta->metadata_layout)) {
return -OCF_ERR_START_CACHE_FAIL; return -OCF_ERR_START_CACHE_FAIL;
} }
ocf_cache_log(cache, log_debug, "Cache attached\n"); ocf_cache_log(cache, log_debug, "Cache attached\n");
attach_params->flags.attached_metadata_inited = true; params->flags.attached_metadata_inited = true;
for (i = 0; i < OCF_IO_CLASS_MAX + 1; ++i) { for (i = 0; i < OCF_IO_CLASS_MAX + 1; ++i) {
cache->user_parts[i].runtime = cache->user_parts[i].runtime =
@ -924,7 +942,7 @@ static int _ocf_mngt_init_prepare_metadata(
ret = ocf_concurrency_init(cache); ret = ocf_concurrency_init(cache);
if (!ret) if (!ret)
attach_params->flags.concurrency_inited = 1; params->flags.concurrency_inited = 1;
return ret; return ret;
} }

View File

@ -56,7 +56,6 @@ int ocf_submit_volume_flush_wait(ocf_volume_t volume)
env_completion_wait(&cntx.complete); env_completion_wait(&cntx.complete);
return cntx.error; return cntx.error;
} }
static void ocf_submit_volume_discard_wait_io(struct ocf_io *io, int error) static void ocf_submit_volume_discard_wait_io(struct ocf_io *io, int error)