]> git.proxmox.com Git - ceph.git/blame - ceph/src/tools/rbd_mirror/image_replayer/PrepareLocalImageRequest.cc
update sources to 12.2.7
[ceph.git] / ceph / src / tools / rbd_mirror / image_replayer / PrepareLocalImageRequest.cc
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3
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"
d2e6a577 12#include "tools/rbd_mirror/image_replayer/GetMirrorImageIdRequest.h"
7c673cae
FG
13#include <type_traits>
14
15#define dout_context g_ceph_context
16#define dout_subsys ceph_subsys_rbd_mirror
17#undef dout_prefix
18#define dout_prefix *_dout << "rbd::mirror::image_replayer::" \
19 << "PrepareLocalImageRequest: " << this << " " \
20 << __func__ << ": "
21
22namespace rbd {
23namespace mirror {
24namespace image_replayer {
25
26using librbd::util::create_context_callback;
27using librbd::util::create_rados_callback;
28
29template <typename I>
30void PrepareLocalImageRequest<I>::send() {
31 dout(20) << dendl;
32 get_local_image_id();
33}
34
35template <typename I>
36void PrepareLocalImageRequest<I>::get_local_image_id() {
37 dout(20) << dendl;
38
d2e6a577 39 Context *ctx = create_context_callback<
7c673cae 40 PrepareLocalImageRequest<I>,
d2e6a577
FG
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);
44 req->send();
7c673cae
FG
45}
46
47template <typename I>
48void PrepareLocalImageRequest<I>::handle_get_local_image_id(int r) {
7c673cae
FG
49 dout(20) << "r=" << r << ", "
50 << "local_image_id=" << *m_local_image_id << dendl;
51
52 if (r < 0) {
7c673cae
FG
53 finish(r);
54 return;
55 }
56
28e407b8
AA
57 get_local_image_name();
58}
59
60template <typename I>
61void PrepareLocalImageRequest<I>::get_local_image_name() {
62 dout(20) << dendl;
63
64 librados::ObjectReadOperation op;
65 librbd::cls_client::dir_get_name_start(&op, *m_local_image_id);
66
67 m_out_bl.clear();
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);
72 assert(r == 0);
73 aio_comp->release();
74}
75
76template <typename I>
77void PrepareLocalImageRequest<I>::handle_get_local_image_name(int r) {
78 dout(20) << "r=" << r << dendl;
79
80 if (r == 0) {
81 bufferlist::iterator it = m_out_bl.begin();
82 r = librbd::cls_client::dir_get_name_finish(&it, m_local_image_name);
83 }
84
85 if (r < 0) {
86 if (r != -ENOENT) {
87 derr << "failed to retrieve image name: " << cpp_strerror(r) << dendl;
88 }
89 finish(r);
90 return;
91 }
92
7c673cae
FG
93 get_mirror_state();
94}
95
96template <typename I>
97void PrepareLocalImageRequest<I>::get_mirror_state() {
98 dout(20) << dendl;
99
100 librados::ObjectReadOperation op;
101 librbd::cls_client::mirror_image_get_start(&op, *m_local_image_id);
102
103 m_out_bl.clear();
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);
108 assert(r == 0);
109 aio_comp->release();
110}
111
112template <typename I>
113void PrepareLocalImageRequest<I>::handle_get_mirror_state(int r) {
114 dout(20) << ": r=" << r << dendl;
115
116 cls::rbd::MirrorImage mirror_image;
117 if (r == 0) {
118 bufferlist::iterator iter = m_out_bl.begin();
119 r = librbd::cls_client::mirror_image_get_finish(&iter, &mirror_image);
120 }
121
122 if (r < 0) {
123 derr << "failed to retrieve image mirror state: " << cpp_strerror(r)
124 << dendl;
125 finish(r);
126 return;
127 }
128
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)
132
133 get_tag_owner();
134}
135
136template <typename I>
137void 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)>
141 ::type>::type;
142
143 dout(20) << dendl;
144
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,
149 m_work_queue, ctx);
150}
151
152template <typename I>
153void PrepareLocalImageRequest<I>::handle_get_tag_owner(int r) {
154 dout(20) << "r=" << r << ", "
155 << "tag_owner=" << *m_tag_owner << dendl;
156
157 if (r < 0) {
158 derr << "failed to retrieve journal tag owner: " << cpp_strerror(r)
159 << dendl;
160 finish(r);
161 return;
162 }
163
164 finish(0);
165}
166
167template <typename I>
168void PrepareLocalImageRequest<I>::finish(int r) {
169 dout(20) << "r=" << r << dendl;
170
171 m_on_finish->complete(r);
172 delete this;
173}
174
175} // namespace image_replayer
176} // namespace mirror
177} // namespace rbd
178
179template class rbd::mirror::image_replayer::PrepareLocalImageRequest<librbd::ImageCtx>;