From 3d25f5148c70a16b95ab06a91a2c3dceeebfcff2 Mon Sep 17 00:00:00 2001 From: Jason Dillaman Date: Tue, 17 Nov 2015 10:42:34 -0500 Subject: [PATCH] librbd: moved object name helpers to librbd::util namespace Signed-off-by: Jason Dillaman --- src/CMakeLists.txt | 1 + src/librbd/AsyncObjectThrottle.cc | 3 ++- src/librbd/ImageCtx.cc | 27 ++++++++++++----------- src/librbd/ImageWatcher.cc | 12 +++++------ src/librbd/Makefile.am | 1 + src/librbd/Utils.cc | 31 +++++++++++++++++++++++++++ src/librbd/Utils.h | 5 +++++ src/librbd/internal.cc | 31 ++++++--------------------- src/librbd/internal.h | 5 ----- src/librbd/operation/RenameRequest.cc | 9 ++++---- 10 files changed, 71 insertions(+), 54 deletions(-) create mode 100644 src/librbd/Utils.cc diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1a2f1db554d95..5950d64d24865 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -888,6 +888,7 @@ if(${WITH_RBD}) librbd/LibrbdAdminSocketHook.cc librbd/LibrbdWriteback.cc librbd/ObjectMap.cc + librbd/Utils.cc librbd/object_map/InvalidateRequest.cc librbd/object_map/Request.cc librbd/object_map/ResizeRequest.cc diff --git a/src/librbd/AsyncObjectThrottle.cc b/src/librbd/AsyncObjectThrottle.cc index e4b07faf364d2..3bf195c9c449a 100644 --- a/src/librbd/AsyncObjectThrottle.cc +++ b/src/librbd/AsyncObjectThrottle.cc @@ -7,6 +7,7 @@ #include "librbd/AsyncRequest.h" #include "librbd/ImageCtx.h" #include "librbd/internal.h" +#include "librbd/Utils.h" namespace librbd { @@ -16,7 +17,7 @@ AsyncObjectThrottle::AsyncObjectThrottle( const AsyncRequest* async_request, T &image_ctx, const ContextFactory& context_factory, Context *ctx, ProgressContext *prog_ctx, uint64_t object_no, uint64_t end_object_no) - : m_lock(unique_lock_name("librbd::AsyncThrottle::m_lock", this)), + : m_lock(util::unique_lock_name("librbd::AsyncThrottle::m_lock", this)), m_async_request(async_request), m_image_ctx(image_ctx), m_context_factory(context_factory), m_ctx(ctx), m_prog_ctx(prog_ctx), m_object_no(object_no), m_end_object_no(end_object_no), m_current_ops(0), diff --git a/src/librbd/ImageCtx.cc b/src/librbd/ImageCtx.cc index 5faad48cf5392..6ad3415dd7132 100644 --- a/src/librbd/ImageCtx.cc +++ b/src/librbd/ImageCtx.cc @@ -21,6 +21,7 @@ #include "librbd/LibrbdAdminSocketHook.h" #include "librbd/ObjectMap.h" #include "librbd/operation/ResizeRequest.h" +#include "librbd/Utils.h" #include @@ -131,16 +132,16 @@ struct C_InvalidateCache : public Context { journal(NULL), refresh_seq(0), last_refresh(0), - owner_lock(unique_lock_name("librbd::ImageCtx::owner_lock", this)), - md_lock(unique_lock_name("librbd::ImageCtx::md_lock", this)), - cache_lock(unique_lock_name("librbd::ImageCtx::cache_lock", this)), - snap_lock(unique_lock_name("librbd::ImageCtx::snap_lock", this)), - parent_lock(unique_lock_name("librbd::ImageCtx::parent_lock", this)), - refresh_lock(unique_lock_name("librbd::ImageCtx::refresh_lock", this)), - object_map_lock(unique_lock_name("librbd::ImageCtx::object_map_lock", this)), - async_ops_lock(unique_lock_name("librbd::ImageCtx::async_ops_lock", this)), - copyup_list_lock(unique_lock_name("librbd::ImageCtx::copyup_list_lock", this)), - completed_reqs_lock(unique_lock_name("librbd::ImageCtx::completed_reqs_lock", this)), + owner_lock(util::unique_lock_name("librbd::ImageCtx::owner_lock", this)), + md_lock(util::unique_lock_name("librbd::ImageCtx::md_lock", this)), + cache_lock(util::unique_lock_name("librbd::ImageCtx::cache_lock", this)), + snap_lock(util::unique_lock_name("librbd::ImageCtx::snap_lock", this)), + parent_lock(util::unique_lock_name("librbd::ImageCtx::parent_lock", this)), + refresh_lock(util::unique_lock_name("librbd::ImageCtx::refresh_lock", this)), + object_map_lock(util::unique_lock_name("librbd::ImageCtx::object_map_lock", this)), + async_ops_lock(util::unique_lock_name("librbd::ImageCtx::async_ops_lock", this)), + copyup_list_lock(util::unique_lock_name("librbd::ImageCtx::copyup_list_lock", this)), + completed_reqs_lock(util::unique_lock_name("librbd::ImageCtx::completed_reqs_lock", this)), extra_read_flags(0), old_format(true), order(0), size(0), features(0), @@ -216,7 +217,7 @@ struct C_InvalidateCache : public Context { if (!old_format) { if (!id.length()) { - r = cls_client::get_id(&md_ctx, id_obj_name(name), &id); + r = cls_client::get_id(&md_ctx, util::id_obj_name(name), &id); if (r < 0) { lderr(cct) << "error reading image id: " << cpp_strerror(r) << dendl; @@ -224,7 +225,7 @@ struct C_InvalidateCache : public Context { } } - header_oid = header_name(id); + header_oid = util::header_name(id); apply_metadata_confs(); r = cls_client::get_immutable_metadata(&md_ctx, header_oid, &object_prefix, &order); @@ -245,7 +246,7 @@ struct C_InvalidateCache : public Context { init_layout(); } else { apply_metadata_confs(); - header_oid = old_header_name(name); + header_oid = util::old_header_name(name); } string pname = string("librbd-") + id + string("-") + diff --git a/src/librbd/ImageWatcher.cc b/src/librbd/ImageWatcher.cc index f06856e22014f..8ffd751930f89 100644 --- a/src/librbd/ImageWatcher.cc +++ b/src/librbd/ImageWatcher.cc @@ -6,6 +6,7 @@ #include "librbd/internal.h" #include "librbd/ObjectMap.h" #include "librbd/TaskFinisher.h" +#include "librbd/Utils.h" #include "cls/lock/cls_lock_client.h" #include "cls/lock/cls_lock_types.h" #include "include/encoding.h" @@ -32,17 +33,16 @@ static const double RETRY_DELAY_SECONDS = 1.0; ImageWatcher::ImageWatcher(ImageCtx &image_ctx) : m_image_ctx(image_ctx), - m_watch_lock(unique_lock_name("librbd::ImageWatcher::m_watch_lock", this)), + m_watch_lock(util::unique_lock_name("librbd::ImageWatcher::m_watch_lock", this)), m_watch_ctx(*this), m_watch_handle(0), m_watch_state(WATCH_STATE_UNREGISTERED), - m_refresh_lock(unique_lock_name("librbd::ImageWatcher::m_refresh_lock", - this)), + m_refresh_lock(util::unique_lock_name("librbd::ImageWatcher::m_refresh_lock", this)), m_lock_supported(false), m_lock_owner_state(LOCK_OWNER_STATE_NOT_LOCKED), - m_listeners_lock(unique_lock_name("librbd::ImageWatcher::m_listeners_lock", this)), + m_listeners_lock(util::unique_lock_name("librbd::ImageWatcher::m_listeners_lock", this)), m_listeners_in_use(false), m_task_finisher(new TaskFinisher(*m_image_ctx.cct)), - m_async_request_lock(unique_lock_name("librbd::ImageWatcher::m_async_request_lock", this)), - m_owner_client_id_lock(unique_lock_name("librbd::ImageWatcher::m_owner_client_id_lock", this)) + m_async_request_lock(util::unique_lock_name("librbd::ImageWatcher::m_async_request_lock", this)), + m_owner_client_id_lock(util::unique_lock_name("librbd::ImageWatcher::m_owner_client_id_lock", this)) { } diff --git a/src/librbd/Makefile.am b/src/librbd/Makefile.am index 5a3bd9462f9e9..a21bb0a4a2fb6 100644 --- a/src/librbd/Makefile.am +++ b/src/librbd/Makefile.am @@ -25,6 +25,7 @@ librbd_internal_la_SOURCES = \ librbd/LibrbdAdminSocketHook.cc \ librbd/LibrbdWriteback.cc \ librbd/ObjectMap.cc \ + librbd/Utils.cc \ librbd/object_map/InvalidateRequest.cc \ librbd/object_map/Request.cc \ librbd/object_map/ResizeRequest.cc \ diff --git a/src/librbd/Utils.cc b/src/librbd/Utils.cc new file mode 100644 index 0000000000000..d89418f490cd4 --- /dev/null +++ b/src/librbd/Utils.cc @@ -0,0 +1,31 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab + +#include "librbd/Utils.h" +#include "include/rbd_types.h" +#include "include/stringify.h" + +namespace librbd { +namespace util { + +const std::string id_obj_name(const std::string &name) +{ + return RBD_ID_PREFIX + name; +} + +const std::string header_name(const std::string &image_id) +{ + return RBD_HEADER_PREFIX + image_id; +} + +const std::string old_header_name(const std::string &image_name) +{ + return image_name + RBD_SUFFIX; +} + +std::string unique_lock_name(const std::string &name, void *address) { + return name + " (" + stringify(address) + ")"; +} + +} // namespace util +} // namespace librbd diff --git a/src/librbd/Utils.h b/src/librbd/Utils.h index bbb30b1ecd043..0986f060b981d 100644 --- a/src/librbd/Utils.h +++ b/src/librbd/Utils.h @@ -86,6 +86,11 @@ struct C_AsyncCallback : public Context { } // namespace detail +const std::string id_obj_name(const std::string &name); +const std::string header_name(const std::string &image_id); +const std::string old_header_name(const std::string &image_name); +std::string unique_lock_name(const std::string &name, void *address); + template librados::AioCompletion *create_rados_ack_callback(T *obj) { return librados::Rados::aio_create_completion( diff --git a/src/librbd/internal.cc b/src/librbd/internal.cc index 8ee94a5af290b..24150d94d58fd 100644 --- a/src/librbd/internal.cc +++ b/src/librbd/internal.cc @@ -260,36 +260,17 @@ int validate_pool(IoCtx &io_ctx, CephContext *cct) { } // anonymous namespace - const string id_obj_name(const string &name) - { - return RBD_ID_PREFIX + name; - } - - const string header_name(const string &image_id) - { - return RBD_HEADER_PREFIX + image_id; - } - - const string old_header_name(const string &image_name) - { - return image_name + RBD_SUFFIX; - } - - std::string unique_lock_name(const std::string &name, void *address) { - return name + " (" + stringify(address) + ")"; - } - int detect_format(IoCtx &io_ctx, const string &name, bool *old_format, uint64_t *size) { CephContext *cct = (CephContext *)io_ctx.cct(); if (old_format) *old_format = true; - int r = io_ctx.stat(old_header_name(name), size, NULL); + int r = io_ctx.stat(util::old_header_name(name), size, NULL); if (r == -ENOENT) { if (old_format) *old_format = false; - r = io_ctx.stat(id_obj_name(name), size, NULL); + r = io_ctx.stat(util::id_obj_name(name), size, NULL); if (r < 0) return r; } else if (r < 0) { @@ -1199,7 +1180,7 @@ int validate_pool(IoCtx &io_ctx, CephContext *cct) { bufferlist bl; bl.append((const char *)&header, sizeof(header)); - string header_oid = old_header_name(imgname); + string header_oid = util::old_header_name(imgname); r = io_ctx.write(header_oid, bl, bl.length(), 0); if (r < 0) { lderr(cct) << "Error writing image header: " << cpp_strerror(r) @@ -1237,7 +1218,7 @@ int validate_pool(IoCtx &io_ctx, CephContext *cct) { return r; } - id_obj = id_obj_name(imgname); + id_obj = util::id_obj_name(imgname); r = io_ctx.create(id_obj, true); if (r < 0) { @@ -1264,7 +1245,7 @@ int validate_pool(IoCtx &io_ctx, CephContext *cct) { } oss << RBD_DATA_PREFIX << id; - header_oid = header_name(id); + header_oid = util::header_name(id); r = cls_client::create_image(&io_ctx, header_oid, size, order, features, oss.str()); if (r < 0) { @@ -2251,7 +2232,7 @@ int validate_pool(IoCtx &io_ctx, CephContext *cct) { } ldout(cct, 2) << "removing id object..." << dendl; - r = io_ctx.remove(id_obj_name(imgname)); + r = io_ctx.remove(util::id_obj_name(imgname)); if (r < 0 && r != -ENOENT) { lderr(cct) << "error removing id object: " << cpp_strerror(r) << dendl; return r; diff --git a/src/librbd/internal.h b/src/librbd/internal.h index a672b52a3eb07..042ba01634d4d 100644 --- a/src/librbd/internal.h +++ b/src/librbd/internal.h @@ -68,11 +68,6 @@ namespace librbd { } }; - const std::string id_obj_name(const std::string &name); - const std::string header_name(const std::string &image_id); - const std::string old_header_name(const std::string &image_name); - std::string unique_lock_name(const std::string &name, void *address); - int detect_format(librados::IoCtx &io_ctx, const std::string &name, bool *old_format, uint64_t *size); diff --git a/src/librbd/operation/RenameRequest.cc b/src/librbd/operation/RenameRequest.cc index 316229c66c7dc..aa5e09fa155f8 100644 --- a/src/librbd/operation/RenameRequest.cc +++ b/src/librbd/operation/RenameRequest.cc @@ -7,6 +7,7 @@ #include "include/rados/librados.hpp" #include "librbd/ImageCtx.h" #include "librbd/internal.h" +#include "librbd/Utils.h" #define dout_subsys ceph_subsys_rbd #undef dout_prefix @@ -46,10 +47,10 @@ template RenameRequest::RenameRequest(I &image_ctx, Context *on_finish, const std::string &dest_name) : Request(image_ctx, on_finish), m_dest_name(dest_name), - m_source_oid(image_ctx.old_format ? old_header_name(image_ctx.name) : - id_obj_name(image_ctx.name)), - m_dest_oid(image_ctx.old_format ? old_header_name(dest_name) : - id_obj_name(dest_name)) { + m_source_oid(image_ctx.old_format ? util::old_header_name(image_ctx.name) : + util::id_obj_name(image_ctx.name)), + m_dest_oid(image_ctx.old_format ? util::old_header_name(dest_name) : + util::id_obj_name(dest_name)) { } template -- 2.39.5