From 636834dae38c2ed2db720da86dfe2665191f9209 Mon Sep 17 00:00:00 2001 From: Michal Mielewczyk Date: Mon, 8 Jul 2019 09:16:12 -0400 Subject: [PATCH] Check for existing metadata when attaching cache. Instead of overriding existing metadata silently, return appropriate error. Signed-off-by: Michal Mielewczyk --- inc/ocf_err.h | 3 +++ src/mngt/ocf_mngt_cache.c | 22 ++++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/inc/ocf_err.h b/inc/ocf_err.h index b1752d4..b87c41f 100644 --- a/inc/ocf_err.h +++ b/inc/ocf_err.h @@ -39,6 +39,9 @@ typedef enum { /** No metadata found on device */ OCF_ERR_NO_METADATA, + /** Cache metadata found on device */ + OCF_ERR_METADATA_FOUND, + /** Invalid volume type */ OCF_ERR_INVAL_VOLUME_TYPE, diff --git a/src/mngt/ocf_mngt_cache.c b/src/mngt/ocf_mngt_cache.c index fca2ab8..c359179 100644 --- a/src/mngt/ocf_mngt_cache.c +++ b/src/mngt/ocf_mngt_cache.c @@ -922,8 +922,23 @@ static void _ocf_mngt_attach_load_properties_end(void *priv, int error, context->metadata.status = error; - if (error) - OCF_PL_NEXT_RET(context->pipeline); + if (error) { + /* + * If --load option wasn't used and old metadata doesn't exist on the + * device, dismiss error. + */ + if (error == -OCF_ERR_NO_METADATA && + cache->device->init_mode != ocf_init_mode_load) + OCF_PL_NEXT_RET(context->pipeline); + else + OCF_PL_FINISH_RET(context->pipeline, error); + } else if (cache->device->init_mode != ocf_init_mode_load) { + /* + * To prevent silent metadata overriding, return error if old metadata + * was detected but --load flag wasn't used. + */ + OCF_PL_FINISH_RET(context->pipeline, -OCF_ERR_METADATA_FOUND); + } context->metadata.shutdown_status = properties->shutdown_status; context->metadata.dirty_flushed = properties->dirty_flushed; @@ -949,6 +964,9 @@ static void _ocf_mngt_attach_load_properties(ocf_pipeline_t pipeline, context->metadata.dirty_flushed = DIRTY_FLUSHED; context->metadata.line_size = context->cfg.cache_line_size; + if (context->cfg.force) + OCF_PL_NEXT_RET(context->pipeline); + if (cache->device->init_mode == ocf_init_mode_metadata_volatile) OCF_PL_NEXT_RET(context->pipeline);