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
;
57 get_local_image_name();
61 void PrepareLocalImageRequest
<I
>::get_local_image_name() {
64 librados::ObjectReadOperation op
;
65 librbd::cls_client::dir_get_name_start(&op
, *m_local_image_id
);
68 librados::AioCompletion
*aio_comp
= create_rados_callback
<
69 PrepareLocalImageRequest
<I
>,
70 &PrepareLocalImageRequest
<I
>::handle_get_local_image_name
>(this);
71 int r
= m_io_ctx
.aio_operate(RBD_DIRECTORY
, aio_comp
, &op
, &m_out_bl
);
77 void PrepareLocalImageRequest
<I
>::handle_get_local_image_name(int r
) {
78 dout(20) << "r=" << r
<< dendl
;
81 bufferlist::iterator it
= m_out_bl
.begin();
82 r
= librbd::cls_client::dir_get_name_finish(&it
, m_local_image_name
);
87 derr
<< "failed to retrieve image name: " << cpp_strerror(r
) << dendl
;
97 void PrepareLocalImageRequest
<I
>::get_mirror_state() {
100 librados::ObjectReadOperation op
;
101 librbd::cls_client::mirror_image_get_start(&op
, *m_local_image_id
);
104 librados::AioCompletion
*aio_comp
= create_rados_callback
<
105 PrepareLocalImageRequest
<I
>,
106 &PrepareLocalImageRequest
<I
>::handle_get_mirror_state
>(this);
107 int r
= m_io_ctx
.aio_operate(RBD_MIRRORING
, aio_comp
, &op
, &m_out_bl
);
112 template <typename I
>
113 void PrepareLocalImageRequest
<I
>::handle_get_mirror_state(int r
) {
114 dout(20) << ": r=" << r
<< dendl
;
116 cls::rbd::MirrorImage mirror_image
;
118 bufferlist::iterator iter
= m_out_bl
.begin();
119 r
= librbd::cls_client::mirror_image_get_finish(&iter
, &mirror_image
);
123 derr
<< "failed to retrieve image mirror state: " << cpp_strerror(r
)
129 // TODO save current mirror state to determine if we should
130 // delete a partially formed image
131 // (e.g. MIRROR_IMAGE_STATE_CREATING/DELETING)
136 template <typename I
>
137 void PrepareLocalImageRequest
<I
>::get_tag_owner() {
138 // deduce the class type for the journal to support unit tests
139 using Journal
= typename
std::decay
<
140 typename
std::remove_pointer
<decltype(std::declval
<I
>().journal
)>
145 Context
*ctx
= create_context_callback
<
146 PrepareLocalImageRequest
<I
>,
147 &PrepareLocalImageRequest
<I
>::handle_get_tag_owner
>(this);
148 Journal::get_tag_owner(m_io_ctx
, *m_local_image_id
, m_tag_owner
,
152 template <typename I
>
153 void PrepareLocalImageRequest
<I
>::handle_get_tag_owner(int r
) {
154 dout(20) << "r=" << r
<< ", "
155 << "tag_owner=" << *m_tag_owner
<< dendl
;
158 derr
<< "failed to retrieve journal tag owner: " << cpp_strerror(r
)
167 template <typename I
>
168 void PrepareLocalImageRequest
<I
>::finish(int r
) {
169 dout(20) << "r=" << r
<< dendl
;
171 m_on_finish
->complete(r
);
175 } // namespace image_replayer
176 } // namespace mirror
179 template class rbd::mirror::image_replayer::PrepareLocalImageRequest
<librbd::ImageCtx
>;