From 0aeedf657c66db2208c0038c6b63aaaba3eaab94 Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Thu, 4 Oct 2012 11:04:53 -0700 Subject: [PATCH] librbd: generate format_string in ImageCtx Prebuild the format string on image load so that it is handy at all times. Signed-off-by: Sage Weil --- src/librbd/ImageCtx.cc | 25 +++++++++++++++++++++++-- src/librbd/ImageCtx.h | 2 ++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/librbd/ImageCtx.cc b/src/librbd/ImageCtx.cc index 1b673e18ac40f..512d8cbc39ec6 100644 --- a/src/librbd/ImageCtx.cc +++ b/src/librbd/ImageCtx.cc @@ -45,7 +45,9 @@ namespace librbd { parent_lock("librbd::ImageCtx::parent_lock"), refresh_lock("librbd::ImageCtx::refresh_lock"), old_format(true), - order(0), size(0), features(0), id(image_id), parent(NULL), + order(0), size(0), features(0), + format_string(NULL), + id(image_id), parent(NULL), stripe_unit(0), stripe_count(0), object_cacher(NULL), writeback_handler(NULL), object_set(NULL) { @@ -92,6 +94,7 @@ namespace librbd { delete object_set; object_set = NULL; } + delete format_string; } int ImageCtx::init() { @@ -153,9 +156,21 @@ namespace librbd { layout.fl_object_size = 1ull << order; layout.fl_pg_pool = data_ctx.get_id(); // FIXME: pool id overflow? + delete format_string; + size_t len = object_prefix.length() + 16; + format_string = new char[len]; + if (old_format) { + snprintf(format_string, len, "%s.%%012llx", object_prefix.c_str()); + } else { + snprintf(format_string, len, "%s.%%016llx", object_prefix.c_str()); + } + ldout(cct, 10) << "init_layout stripe_unit " << stripe_unit << " stripe_count " << stripe_count - << " object_size " << layout.fl_object_size << dendl; + << " object_size " << layout.fl_object_size + << " prefix " << object_prefix + << " format " << format_string + << dendl; } void ImageCtx::perf_start(string name) { @@ -266,6 +281,12 @@ namespace librbd { return 1ull << order; } + string ImageCtx::get_object_name(uint64_t num) const { + char buf[object_prefix.length() + 32]; + snprintf(buf, sizeof(buf), format_string, num); + return string(buf); + } + uint64_t ImageCtx::get_stripe_unit() const { return stripe_unit; diff --git a/src/librbd/ImageCtx.h b/src/librbd/ImageCtx.h index f952ef621defc..1c58a4ce026cb 100644 --- a/src/librbd/ImageCtx.h +++ b/src/librbd/ImageCtx.h @@ -72,6 +72,7 @@ namespace librbd { uint64_t size; uint64_t features; std::string object_prefix; + char *format_string; std::string header_oid; std::string id; // only used for new-format images parent_info parent_md; @@ -106,6 +107,7 @@ namespace librbd { uint64_t get_size() const; uint64_t get_object_size() const; + string get_object_name(uint64_t num) const; uint64_t get_num_objects() const; uint64_t get_stripe_unit() const; uint64_t get_stripe_count() const; -- 2.39.5