1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "tools/rbd_mirror/image_replayer/PrepareLocalImageRequest.h"
5 #include "include/rados/librados.hpp"
6 #include "cls/rbd/cls_rbd_client.h"
7 #include "common/errno.h"
8 #include "librbd/ImageCtx.h"
9 #include "librbd/Journal.h"
10 #include "librbd/Utils.h"
11 #include "tools/rbd_mirror/Threads.h"
12 #include "tools/rbd_mirror/image_replayer/GetMirrorImageIdRequest.h"
13 #include <type_traits>
15 #define dout_context g_ceph_context
16 #define dout_subsys ceph_subsys_rbd_mirror
18 #define dout_prefix *_dout << "rbd::mirror::image_replayer::" \
19 << "PrepareLocalImageRequest: " << this << " " \
24 namespace image_replayer {
26 using librbd::util::create_context_callback;
27 using librbd::util::create_rados_callback;
30 void PrepareLocalImageRequest<I>::send() {
36 void PrepareLocalImageRequest<I>::get_local_image_id() {
39 Context *ctx = create_context_callback<
40 PrepareLocalImageRequest<I>,
41 &PrepareLocalImageRequest<I>::handle_get_local_image_id>(this);
42 auto req = GetMirrorImageIdRequest<I>::create(m_io_ctx, m_global_image_id,
43 m_local_image_id, ctx);
48 void PrepareLocalImageRequest<I>::handle_get_local_image_id(int r) {
49 dout(20) << "r=" << r << ", "
50 << "local_image_id=" << *m_local_image_id << dendl;
61 void PrepareLocalImageRequest<I>::get_mirror_state() {
64 librados::ObjectReadOperation op;
65 librbd::cls_client::mirror_image_get_start(&op, *m_local_image_id);
68 librados::AioCompletion *aio_comp = create_rados_callback<
69 PrepareLocalImageRequest<I>,
70 &PrepareLocalImageRequest<I>::handle_get_mirror_state>(this);
71 int r = m_io_ctx.aio_operate(RBD_MIRRORING, aio_comp, &op, &m_out_bl);
77 void PrepareLocalImageRequest<I>::handle_get_mirror_state(int r) {
78 dout(20) << ": r=" << r << dendl;
80 cls::rbd::MirrorImage mirror_image;
82 bufferlist::iterator iter = m_out_bl.begin();
83 r = librbd::cls_client::mirror_image_get_finish(&iter, &mirror_image);
87 derr << "failed to retrieve image mirror state: " << cpp_strerror(r)
93 // TODO save current mirror state to determine if we should
94 // delete a partially formed image
95 // (e.g. MIRROR_IMAGE_STATE_CREATING/DELETING)
100 template <typename I>
101 void PrepareLocalImageRequest<I>::get_tag_owner() {
102 // deduce the class type for the journal to support unit tests
103 using Journal = typename std::decay<
104 typename std::remove_pointer<decltype(std::declval<I>().journal)>
109 Context *ctx = create_context_callback<
110 PrepareLocalImageRequest<I>,
111 &PrepareLocalImageRequest<I>::handle_get_tag_owner>(this);
112 Journal::get_tag_owner(m_io_ctx, *m_local_image_id, m_tag_owner,
116 template <typename I>
117 void PrepareLocalImageRequest<I>::handle_get_tag_owner(int r) {
118 dout(20) << "r=" << r << ", "
119 << "tag_owner=" << *m_tag_owner << dendl;
122 derr << "failed to retrieve journal tag owner: " << cpp_strerror(r)
131 template <typename I>
132 void PrepareLocalImageRequest<I>::finish(int r) {
133 dout(20) << "r=" << r << dendl;
135 m_on_finish->complete(r);
139 } // namespace image_replayer
140 } // namespace mirror
143 template class rbd::mirror::image_replayer::PrepareLocalImageRequest<librbd::ImageCtx>;