]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
librbd: async methods to create/remove object map
authorMykola Golub <mgolub@mirantis.com>
Wed, 31 Aug 2016 19:39:20 +0000 (22:39 +0300)
committerMykola Golub <mgolub@mirantis.com>
Wed, 28 Sep 2016 12:17:18 +0000 (15:17 +0300)
Signed-off-by: Mykola Golub <mgolub@mirantis.com>
src/librbd/CMakeLists.txt
src/librbd/object_map/CreateRequest.cc [new file with mode: 0644]
src/librbd/object_map/CreateRequest.h [new file with mode: 0644]
src/librbd/object_map/RemoveRequest.cc [new file with mode: 0644]
src/librbd/object_map/RemoveRequest.h [new file with mode: 0644]

index df392b2526d88534e32ec34f6403d8b5cc232c97..81d2d6490702ebd8bf76f340722fb9471501b0ab 100644 (file)
@@ -39,6 +39,7 @@ set(librbd_internal_srcs
   image/OpenRequest.cc
   image/RefreshParentRequest.cc
   image/RefreshRequest.cc
+  image/SetFlagsRequest.cc
   image/SetSnapRequest.cc
   image_watcher/Notifier.cc
   image_watcher/NotifyLockOwner.cc
@@ -47,10 +48,12 @@ set(librbd_internal_srcs
   journal/CreateRequest.cc
   journal/Replay.cc
   journal/StandardPolicy.cc
+  object_map/CreateRequest.cc
   object_map/InvalidateRequest.cc
   object_map/LockRequest.cc
-  object_map/Request.cc
   object_map/RefreshRequest.cc
+  object_map/RemoveRequest.cc
+  object_map/Request.cc
   object_map/ResizeRequest.cc
   object_map/SnapshotCreateRequest.cc
   object_map/SnapshotRemoveRequest.cc
diff --git a/src/librbd/object_map/CreateRequest.cc b/src/librbd/object_map/CreateRequest.cc
new file mode 100644 (file)
index 0000000..2bc4c34
--- /dev/null
@@ -0,0 +1,93 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "librbd/object_map/CreateRequest.h"
+#include "include/assert.h"
+#include "common/dout.h"
+#include "common/errno.h"
+#include "cls/rbd/cls_rbd_client.h"
+#include "librbd/ImageCtx.h"
+#include "librbd/ObjectMap.h"
+#include "librbd/Utils.h"
+
+#define dout_subsys ceph_subsys_rbd
+#undef dout_prefix
+#define dout_prefix *_dout << "librbd::object_map::CreateRequest: "
+
+namespace librbd {
+namespace object_map {
+
+using util::create_context_callback;
+using util::create_rados_ack_callback;
+
+template <typename I>
+CreateRequest<I>::CreateRequest(I *image_ctx, Context *on_finish)
+  : m_image_ctx(image_ctx), m_on_finish(on_finish) {
+}
+
+template <typename I>
+void CreateRequest<I>::send() {
+  CephContext *cct = m_image_ctx->cct;
+
+  uint64_t max_size = m_image_ctx->size;
+
+  {
+    RWLock::WLocker snap_locker(m_image_ctx->snap_lock);
+    m_snap_ids.push_back(CEPH_NOSNAP);
+    for (auto it : m_image_ctx->snap_info) {
+      max_size = MAX(max_size, it.second.size);
+      m_snap_ids.push_back(it.first);
+    }
+
+    if (ObjectMap::is_compatible(m_image_ctx->layout, max_size)) {
+      send_object_map_resize();
+      return;
+    }
+  }
+
+  lderr(cct) << "image size not compatible with object map" << dendl;
+  m_on_finish->complete(-EINVAL);
+}
+
+template <typename I>
+void CreateRequest<I>::send_object_map_resize() {
+  CephContext *cct = m_image_ctx->cct;
+  ldout(cct, 20) << __func__ << dendl;
+
+  Context *ctx = create_context_callback<
+    CreateRequest<I>, &CreateRequest<I>::handle_object_map_resize>(this);
+  C_Gather *gather_ctx = new C_Gather(cct, ctx);
+
+  for (auto snap_id : m_snap_ids) {
+    librados::ObjectWriteOperation op;
+    uint64_t snap_size = m_image_ctx->get_image_size(snap_id);
+
+    cls_client::object_map_resize(&op, Striper::get_num_objects(
+                                   m_image_ctx->layout, snap_size),
+                                 OBJECT_NONEXISTENT);
+
+    std::string oid(ObjectMap::object_map_name(m_image_ctx->id, snap_id));
+    librados::AioCompletion *comp = create_rados_ack_callback(gather_ctx->new_sub());
+    int r = m_image_ctx->md_ctx.aio_operate(oid, comp, &op);
+    assert(r == 0);
+    comp->release();
+  }
+  gather_ctx->activate();
+}
+
+template <typename I>
+Context *CreateRequest<I>::handle_object_map_resize(int *result) {
+  CephContext *cct = m_image_ctx->cct;
+  ldout(cct, 20) << __func__ << ": r=" << *result << dendl;
+
+  if (*result < 0) {
+    lderr(cct) << "object map resize failed: " << cpp_strerror(*result)
+              << dendl;
+  }
+  return m_on_finish;
+}
+
+} // namespace object_map
+} // namespace librbd
+
+template class librbd::object_map::CreateRequest<librbd::ImageCtx>;
diff --git a/src/librbd/object_map/CreateRequest.h b/src/librbd/object_map/CreateRequest.h
new file mode 100644 (file)
index 0000000..6929abe
--- /dev/null
@@ -0,0 +1,60 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#ifndef CEPH_LIBRBD_OBJECT_MAP_CREATE_REQUEST_H
+#define CEPH_LIBRBD_OBJECT_MAP_CREATE_REQUEST_H
+
+#include "include/buffer.h"
+#include "common/Mutex.h"
+#include <map>
+#include <string>
+
+class Context;
+
+namespace librbd {
+
+class ImageCtx;
+
+namespace object_map {
+
+template <typename ImageCtxT = ImageCtx>
+class CreateRequest {
+public:
+  static CreateRequest *create(ImageCtxT *image_ctx, Context *on_finish) {
+    return new CreateRequest(image_ctx, on_finish);
+  }
+
+  void send();
+
+private:
+  /**
+   * @verbatim
+   *
+   * <start>
+   *    |         .  .  .
+   *    v         v     .
+   * OBJECT_MAP_RESIZE  . (for every snapshot)
+   *    |         .     .
+   *    v         .  .  .
+   * <finis>
+   *
+   * @endverbatim
+   */
+
+  CreateRequest(ImageCtxT *image_ctx, Context *on_finish);
+
+  ImageCtxT *m_image_ctx;
+  Context *m_on_finish;
+
+  std::vector<uint64_t> m_snap_ids;
+
+  void send_object_map_resize();
+  Context *handle_object_map_resize(int *result);
+};
+
+} // namespace object_map
+} // namespace librbd
+
+extern template class librbd::object_map::CreateRequest<librbd::ImageCtx>;
+
+#endif // CEPH_LIBRBD_OBJECT_MAP_CREATE_REQUEST_H
diff --git a/src/librbd/object_map/RemoveRequest.cc b/src/librbd/object_map/RemoveRequest.cc
new file mode 100644 (file)
index 0000000..d41349c
--- /dev/null
@@ -0,0 +1,89 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "librbd/object_map/RemoveRequest.h"
+#include "common/dout.h"
+#include "common/errno.h"
+#include "cls/rbd/cls_rbd_client.h"
+#include "librbd/ImageCtx.h"
+#include "librbd/ObjectMap.h"
+#include "librbd/Utils.h"
+#include "include/assert.h"
+
+#define dout_subsys ceph_subsys_rbd
+#undef dout_prefix
+#define dout_prefix *_dout << "librbd::object_map::RemoveRequest: "
+
+namespace librbd {
+namespace object_map {
+
+using util::create_rados_ack_callback;
+
+template <typename I>
+RemoveRequest<I>::RemoveRequest(I *image_ctx, Context *on_finish)
+  : m_image_ctx(image_ctx), m_on_finish(on_finish),
+    m_lock("object_map::RemoveRequest::m_lock") {
+}
+
+template <typename I>
+void RemoveRequest<I>::send() {
+  send_remove_object_map();
+}
+
+template <typename I>
+void RemoveRequest<I>::send_remove_object_map() {
+  CephContext *cct = m_image_ctx->cct;
+  ldout(cct, 20) << __func__ << dendl;
+
+  RWLock::WLocker snap_locker(m_image_ctx->snap_lock);
+  std::vector<uint64_t> snap_ids;
+  snap_ids.push_back(CEPH_NOSNAP);
+  for (auto it : m_image_ctx->snap_info) {
+    snap_ids.push_back(it.first);
+  }
+
+  Mutex::Locker locker(m_lock);
+  assert(m_ref_counter == 0);
+
+  for (auto snap_id : snap_ids) {
+    m_ref_counter++;
+    std::string oid(ObjectMap::object_map_name(m_image_ctx->id, snap_id));
+    using klass = RemoveRequest<I>;
+    librados::AioCompletion *comp =
+      create_rados_ack_callback<klass, &klass::handle_remove_object_map>(this);
+
+    int r = m_image_ctx->md_ctx.aio_remove(oid, comp);
+    assert(r == 0);
+    comp->release();
+  }
+}
+
+template <typename I>
+Context *RemoveRequest<I>::handle_remove_object_map(int *result) {
+  CephContext *cct = m_image_ctx->cct;
+  ldout(cct, 20) << __func__ << ": r=" << *result << dendl;
+
+  {
+    Mutex::Locker locker(m_lock);
+    assert(m_ref_counter > 0);
+    m_ref_counter--;
+
+    if (*result < 0 && *result != -ENOENT) {
+      lderr(cct) << "failed to remove object map: " << cpp_strerror(*result)
+                << dendl;
+      m_error_result = *result;
+    }
+    if (m_ref_counter > 0) {
+      return nullptr;
+    }
+  }
+  if (m_error_result < 0) {
+    *result = m_error_result;
+  }
+  return m_on_finish;
+}
+
+} // namespace object_map
+} // namespace librbd
+
+template class librbd::object_map::RemoveRequest<librbd::ImageCtx>;
diff --git a/src/librbd/object_map/RemoveRequest.h b/src/librbd/object_map/RemoveRequest.h
new file mode 100644 (file)
index 0000000..1353ef9
--- /dev/null
@@ -0,0 +1,62 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#ifndef CEPH_LIBRBD_OBJECT_MAP_REMOVE_REQUEST_H
+#define CEPH_LIBRBD_OBJECT_MAP_REMOVE_REQUEST_H
+
+#include "include/buffer.h"
+#include "common/Mutex.h"
+#include <map>
+#include <string>
+
+class Context;
+
+namespace librbd {
+
+class ImageCtx;
+
+namespace object_map {
+
+template <typename ImageCtxT = ImageCtx>
+class RemoveRequest {
+public:
+  static RemoveRequest *create(ImageCtxT *image_ctx, Context *on_finish) {
+    return new RemoveRequest(image_ctx, on_finish);
+  }
+
+  void send();
+
+private:
+  /**
+   * @verbatim
+   *
+   * <start>
+   *    |          .  .  .
+   *    v          v     .
+   * REMOVE_OBJECT_MAP   . (for every snapshot)
+   *    |          .     .
+   *    v          .  .  .
+   * <finis>
+   *
+   * @endverbatim
+   */
+
+  RemoveRequest(ImageCtxT *image_ctx, Context *on_finish);
+
+  ImageCtxT *m_image_ctx;
+  Context *m_on_finish;
+
+  int m_error_result = 0;
+  int m_ref_counter = 0;
+  mutable Mutex m_lock;
+
+  void send_remove_object_map();
+  Context *handle_remove_object_map(int *result);
+};
+
+} // namespace object_map
+} // namespace librbd
+
+extern template class librbd::object_map::RemoveRequest<librbd::ImageCtx>;
+
+#endif // CEPH_LIBRBD_OBJECT_MAP_REMOVE_REQUEST_H