]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
rbd-mirror: helper state machine for closing librbd images
authorJason Dillaman <dillaman@redhat.com>
Mon, 14 Mar 2016 02:08:16 +0000 (22:08 -0400)
committerJosh Durgin <jdurgin@redhat.com>
Tue, 15 Mar 2016 00:26:08 +0000 (17:26 -0700)
librbd ImageCtx's cannot be deleted from within the librbd thread.  This
state machine will perform the deletion from the rbd-mirror work queue
thread.

Signed-off-by: Jason Dillaman <dillaman@redhat.com>
src/tools/rbd_mirror/image_replayer/CloseImageRequest.cc [new file with mode: 0644]
src/tools/rbd_mirror/image_replayer/CloseImageRequest.h [new file with mode: 0644]

diff --git a/src/tools/rbd_mirror/image_replayer/CloseImageRequest.cc b/src/tools/rbd_mirror/image_replayer/CloseImageRequest.cc
new file mode 100644 (file)
index 0000000..3751245
--- /dev/null
@@ -0,0 +1,84 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "CloseImageRequest.h"
+#include "common/dout.h"
+#include "common/errno.h"
+#include "common/WorkQueue.h"
+#include "librbd/ImageCtx.h"
+#include "librbd/ImageState.h"
+#include "librbd/Utils.h"
+
+#define dout_subsys ceph_subsys_rbd_mirror
+#undef dout_prefix
+#define dout_prefix *_dout << "rbd::mirror::image_replayer::CloseImageRequest: " \
+                           << this << " " << __func__
+
+namespace rbd {
+namespace mirror {
+namespace image_replayer {
+
+using librbd::util::create_context_callback;
+
+template <typename I>
+CloseImageRequest<I>::CloseImageRequest(I **image_ctx, ContextWQ *work_queue,
+                                        Context *on_finish)
+  : m_image_ctx(image_ctx), m_work_queue(work_queue), m_on_finish(on_finish) {
+}
+
+template <typename I>
+void CloseImageRequest<I>::send() {
+  close_image();
+}
+
+template <typename I>
+void CloseImageRequest<I>::close_image() {
+  dout(20) << dendl;
+
+  Context *ctx = create_context_callback<
+    CloseImageRequest<I>, &CloseImageRequest<I>::handle_close_image>(this);
+  (*m_image_ctx)->state->close(ctx);
+}
+
+template <typename I>
+void CloseImageRequest<I>::handle_close_image(int r) {
+  dout(20) << ": r=" << r << dendl;
+
+  if (r < 0) {
+    derr << "error encountered while closing image: " << cpp_strerror(r)
+         << dendl;
+  }
+
+  switch_thread_context();
+}
+
+template <typename I>
+void CloseImageRequest<I>::switch_thread_context() {
+  dout(20) << dendl;
+
+  // swap the librbd thread context for the rbd-mirror thread context
+  Context *ctx = create_context_callback<
+    CloseImageRequest<I>, &CloseImageRequest<I>::handle_switch_thread_context>(
+      this);
+  m_work_queue->queue(ctx, 0);
+}
+
+template <typename I>
+void CloseImageRequest<I>::handle_switch_thread_context(int r) {
+  dout(20) << dendl;
+
+  assert(r == 0);
+
+  delete *m_image_ctx;
+  *m_image_ctx = nullptr;
+
+  m_on_finish->complete(0);
+  delete this;
+}
+
+} // namespace image_replayer
+} // namespace mirror
+} // namespace rbd
+
+template class rbd::mirror::image_replayer::CloseImageRequest<librbd::ImageCtx>;
+
diff --git a/src/tools/rbd_mirror/image_replayer/CloseImageRequest.h b/src/tools/rbd_mirror/image_replayer/CloseImageRequest.h
new file mode 100644 (file)
index 0000000..8c43297
--- /dev/null
@@ -0,0 +1,66 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#ifndef RBD_MIRROR_IMAGE_REPLAYER_CLOSE_IMAGE_REQUEST_H
+#define RBD_MIRROR_IMAGE_REPLAYER_CLOSE_IMAGE_REQUEST_H
+
+#include "include/int_types.h"
+#include "librbd/ImageCtx.h"
+#include <string>
+
+class Context;
+class ContextWQ;
+namespace librbd { class ImageCtx; }
+
+namespace rbd {
+namespace mirror {
+namespace image_replayer {
+
+template <typename ImageCtxT = librbd::ImageCtx>
+class CloseImageRequest {
+public:
+  static CloseImageRequest* create(ImageCtxT **image_ctx, ContextWQ *work_queue,
+                                   Context *on_finish) {
+    return new CloseImageRequest(image_ctx, work_queue, on_finish);
+  }
+
+  CloseImageRequest(ImageCtxT **image_ctx, ContextWQ *work_queue,
+                    Context *on_finish);
+
+  void send();
+
+private:
+  /**
+   * @verbatim
+   *
+   * <start>
+   *    |
+   *    v
+   * CLOSE_IMAGE
+   *    |
+   *    v
+   * SWITCH_CONTEXT
+   *    |
+   *    v
+   * <finish>
+   *
+   * @endverbatim
+   */
+  ImageCtxT **m_image_ctx;
+  ContextWQ *m_work_queue;
+  Context *m_on_finish;
+
+  void close_image();
+  void handle_close_image(int r);
+
+  void switch_thread_context();
+  void handle_switch_thread_context(int r);
+};
+
+} // namespace image_replayer
+} // namespace mirror
+} // namespace rbd
+
+extern template class rbd::mirror::image_replayer::CloseImageRequest<librbd::ImageCtx>;
+
+#endif // RBD_MIRROR_IMAGE_REPLAYER_CLOSE_IMAGE_REQUEST_H