1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "librbd/mirror/EnableRequest.h"
5 #include "common/dout.h"
6 #include "common/errno.h"
7 #include "cls/rbd/cls_rbd_client.h"
8 #include "librbd/ImageState.h"
9 #include "librbd/Journal.h"
10 #include "librbd/MirroringWatcher.h"
11 #include "librbd/Utils.h"
13 #define dout_subsys ceph_subsys_rbd
15 #define dout_prefix *_dout << "librbd::mirror::EnableRequest: "
20 using util::create_context_callback;
21 using util::create_rados_callback;
24 EnableRequest<I>::EnableRequest(librados::IoCtx &io_ctx,
25 const std::string &image_id,
26 const std::string &non_primary_global_image_id,
27 ContextWQ *op_work_queue, Context *on_finish)
28 : m_io_ctx(io_ctx), m_image_id(image_id),
29 m_non_primary_global_image_id(non_primary_global_image_id),
30 m_op_work_queue(op_work_queue), m_on_finish(on_finish),
31 m_cct(reinterpret_cast<CephContext*>(io_ctx.cct())) {
35 void EnableRequest<I>::send() {
40 void EnableRequest<I>::send_get_tag_owner() {
41 if (!m_non_primary_global_image_id.empty()) {
43 send_get_mirror_image();
45 ldout(m_cct, 10) << this << " " << __func__ << dendl;
47 using klass = EnableRequest<I>;
48 Context *ctx = create_context_callback<
49 klass, &klass::handle_get_tag_owner>(this);
50 librbd::Journal<>::is_tag_owner(m_io_ctx, m_image_id, &m_is_primary,
51 m_op_work_queue, ctx);
55 Context *EnableRequest<I>::handle_get_tag_owner(int *result) {
56 ldout(m_cct, 10) << this << " " << __func__ << ": r=" << *result << dendl;
59 lderr(m_cct) << "failed to check tag ownership: " << cpp_strerror(*result)
65 lderr(m_cct) << "last journal tag not owned by local cluster" << dendl;
70 send_get_mirror_image();
75 void EnableRequest<I>::send_get_mirror_image() {
76 ldout(m_cct, 10) << this << " " << __func__ << dendl;
78 librados::ObjectReadOperation op;
79 cls_client::mirror_image_get_start(&op, m_image_id);
81 using klass = EnableRequest<I>;
82 librados::AioCompletion *comp =
83 create_rados_callback<klass, &klass::handle_get_mirror_image>(this);
85 int r = m_io_ctx.aio_operate(RBD_MIRRORING, comp, &op, &m_out_bl);
91 Context *EnableRequest<I>::handle_get_mirror_image(int *result) {
92 ldout(m_cct, 10) << this << " " << __func__ << ": r=" << *result << dendl;
95 bufferlist::iterator iter = m_out_bl.begin();
96 *result = cls_client::mirror_image_get_finish(&iter, &m_mirror_image);
100 if (m_mirror_image.state == cls::rbd::MIRROR_IMAGE_STATE_ENABLED) {
101 ldout(m_cct, 10) << this << " " << __func__
102 << ": mirroring is already enabled" << dendl;
104 lderr(m_cct) << "currently disabling" << dendl;
110 if (*result != -ENOENT) {
111 lderr(m_cct) << "failed to retreive mirror image: " << cpp_strerror(*result)
117 m_mirror_image.state = cls::rbd::MIRROR_IMAGE_STATE_ENABLED;
118 if (m_non_primary_global_image_id.empty()) {
120 uuid_gen.generate_random();
121 m_mirror_image.global_image_id = uuid_gen.to_string();
123 m_mirror_image.global_image_id = m_non_primary_global_image_id;
126 send_set_mirror_image();
130 template <typename I>
131 void EnableRequest<I>::send_set_mirror_image() {
132 ldout(m_cct, 10) << this << " " << __func__ << dendl;
134 librados::ObjectWriteOperation op;
135 cls_client::mirror_image_set(&op, m_image_id, m_mirror_image);
137 using klass = EnableRequest<I>;
138 librados::AioCompletion *comp =
139 create_rados_callback<klass, &klass::handle_set_mirror_image>(this);
141 int r = m_io_ctx.aio_operate(RBD_MIRRORING, comp, &op);
146 template <typename I>
147 Context *EnableRequest<I>::handle_set_mirror_image(int *result) {
148 ldout(m_cct, 10) << this << " " << __func__ << ": r=" << *result << dendl;
151 lderr(m_cct) << "failed to enable mirroring: " << cpp_strerror(*result)
156 send_notify_mirroring_watcher();
160 template <typename I>
161 void EnableRequest<I>::send_notify_mirroring_watcher() {
162 ldout(m_cct, 10) << this << " " << __func__ << dendl;
164 using klass = EnableRequest<I>;
165 Context *ctx = create_context_callback<
166 klass, &klass::handle_notify_mirroring_watcher>(this);
168 MirroringWatcher<>::notify_image_updated(m_io_ctx,
169 cls::rbd::MIRROR_IMAGE_STATE_ENABLED,
171 m_mirror_image.global_image_id, ctx);
174 template <typename I>
175 Context *EnableRequest<I>::handle_notify_mirroring_watcher(int *result) {
176 ldout(m_cct, 10) << this << " " << __func__ << ": r=" << *result << dendl;
179 lderr(m_cct) << "failed to send update notification: "
180 << cpp_strerror(*result) << dendl;
187 } // namespace mirror
188 } // namespace librbd
190 template class librbd::mirror::EnableRequest<librbd::ImageCtx>;