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); diff --git a/tests/functional/pyocf/types/shared.py b/tests/functional/pyocf/types/shared.py index 6f53dda..2467599 100644 --- a/tests/functional/pyocf/types/shared.py +++ b/tests/functional/pyocf/types/shared.py @@ -20,6 +20,7 @@ class OcfErrorCode(IntEnum): OCF_ERR_NO_LOCK = auto() OCF_ERR_METADATA_VER = auto() OCF_ERR_NO_METADATA = auto() + OCF_ERR_METADATA_FOUND = auto() OCF_ERR_INVAL_VOLUME_TYPE = auto() OCF_ERR_UNKNOWN = auto() OCF_ERR_TOO_MANY_CACHES = auto() diff --git a/tests/functional/tests/basic/test_pyocf.py b/tests/functional/tests/basic/test_pyocf.py index 172bce6..872d187 100644 --- a/tests/functional/tests/basic/test_pyocf.py +++ b/tests/functional/tests/basic/test_pyocf.py @@ -62,7 +62,7 @@ def test_start_corrupted_metadata_lba(pyocf_ctx): def test_load_cache_no_preexisting_data(pyocf_ctx): cache_device = Volume(S.from_MiB(30)) - with pytest.raises(OcfError, match="OCF_ERR_START_CACHE_FAIL"): + with pytest.raises(OcfError, match="OCF_ERR_NO_METADATA"): cache = Cache.load_from_device(cache_device)