From 859d62f4d6a1dbb9c7c5f25de06bed90aca22ead Mon Sep 17 00:00:00 2001 From: Ilya Dryomov Date: Wed, 5 May 2021 12:29:44 +0200 Subject: [PATCH] librbd/cache/pwl: fix parsing of cache_type in create_image_cache_state() cache_type is a string (currently "ssd" or "rwl"), not an int. When coerced to an int, the result is 0 -- JSONFormattable ends up calling atoi() under the hood and atoi() does not detect errors... As the value of rbd_persistent_cache_mode option is validated by the option parser (enum type) and, separately, ImageCacheState::cache_type is validated in InitRequest::get_image_cache_state() when creating an instance of the approprate WriteLog, don't attempt to validate it in ImageCacheState::create_image_cache_state(). Fixes: https://tracker.ceph.com/issues/50668 Signed-off-by: Ilya Dryomov (cherry picked from commit ec7af7ede9beacec91140996abe343a5db48004f) --- src/librbd/cache/pwl/ImageCacheState.cc | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/src/librbd/cache/pwl/ImageCacheState.cc b/src/librbd/cache/pwl/ImageCacheState.cc index 4fb6f529f2577..11074befb57da 100644 --- a/src/librbd/cache/pwl/ImageCacheState.cc +++ b/src/librbd/cache/pwl/ImageCacheState.cc @@ -54,8 +54,9 @@ ImageCacheState::ImageCacheState( present = (bool)f["present"]; empty = (bool)f["empty"]; clean = (bool)f["clean"]; - host = (string)f["pwl_host"]; - path = (string)f["pwl_path"]; + cache_type = f["cache_type"]; + host = f["pwl_host"]; + path = f["pwl_path"]; uint64_t pwl_size; std::istringstream iss(f["pwl_size"]); iss >> pwl_size; @@ -142,19 +143,10 @@ ImageCacheState* ImageCacheState::create_image_cache_state( } bool cache_exists = (bool)f["present"]; - int cache_type = (int)f["cache_type"]; - - switch (cache_type) { - case IMAGE_CACHE_TYPE_SSD: - case IMAGE_CACHE_TYPE_RWL: - if (!cache_exists) { - cache_state = new ImageCacheState(image_ctx, plugin_api); - } else { - cache_state = new ImageCacheState(image_ctx, f, plugin_api); - } - break; - default: - r = -EINVAL; + if (!cache_exists) { + cache_state = new ImageCacheState(image_ctx, plugin_api); + } else { + cache_state = new ImageCacheState(image_ctx, f, plugin_api); } } return cache_state; -- 2.39.5