1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "CreateImageRequest.h"
5 #include "CloseImageRequest.h"
6 #include "OpenImageRequest.h"
7 #include "common/errno.h"
8 #include "common/WorkQueue.h"
9 #include "cls/rbd/cls_rbd_client.h"
10 #include "librbd/ImageCtx.h"
11 #include "librbd/ImageState.h"
12 #include "librbd/internal.h"
13 #include "librbd/Utils.h"
14 #include "librbd/image/CreateRequest.h"
15 #include "librbd/image/CloneRequest.h"
17 #define dout_context g_ceph_context
18 #define dout_subsys ceph_subsys_rbd_mirror
20 #define dout_prefix *_dout << "rbd::mirror::image_replayer::CreateImageRequest: " \
21 << this << " " << __func__
23 using librbd::util::create_context_callback
;
24 using librbd::util::create_rados_callback
;
28 namespace image_replayer
{
31 CreateImageRequest
<I
>::CreateImageRequest(librados::IoCtx
&local_io_ctx
,
32 ContextWQ
*work_queue
,
33 const std::string
&global_image_id
,
34 const std::string
&remote_mirror_uuid
,
35 const std::string
&local_image_name
,
37 std::string
*local_image_id
,
39 : m_local_io_ctx(local_io_ctx
), m_work_queue(work_queue
),
40 m_global_image_id(global_image_id
),
41 m_remote_mirror_uuid(remote_mirror_uuid
),
42 m_local_image_name(local_image_name
), m_remote_image_ctx(remote_image_ctx
),
43 m_local_image_id(local_image_id
), m_on_finish(on_finish
) {
47 void CreateImageRequest
<I
>::send() {
48 int r
= validate_parent();
54 if (m_remote_parent_spec
.pool_id
== -1) {
57 get_parent_global_image_id();
62 void CreateImageRequest
<I
>::create_image() {
65 using klass
= CreateImageRequest
<I
>;
66 Context
*ctx
= create_context_callback
<klass
, &klass::handle_create_image
>(this);
68 RWLock::RLocker
snap_locker(m_remote_image_ctx
->snap_lock
);
70 librbd::ImageOptions image_options
;
71 image_options
.set(RBD_IMAGE_OPTION_FEATURES
, m_remote_image_ctx
->features
);
72 image_options
.set(RBD_IMAGE_OPTION_ORDER
, m_remote_image_ctx
->order
);
73 image_options
.set(RBD_IMAGE_OPTION_STRIPE_UNIT
,
74 m_remote_image_ctx
->stripe_unit
);
75 image_options
.set(RBD_IMAGE_OPTION_STRIPE_COUNT
,
76 m_remote_image_ctx
->stripe_count
);
78 *m_local_image_id
= librbd::util::generate_image_id(m_local_io_ctx
);;
80 librbd::image::CreateRequest
<I
> *req
= librbd::image::CreateRequest
<I
>::create(
81 m_local_io_ctx
, m_local_image_name
, *m_local_image_id
,
82 m_remote_image_ctx
->size
, image_options
, m_global_image_id
,
83 m_remote_mirror_uuid
, false, m_remote_image_ctx
->op_work_queue
, ctx
);
88 void CreateImageRequest
<I
>::handle_create_image(int r
) {
89 dout(20) << ": r=" << r
<< dendl
;
91 derr
<< ": failed to create local image: " << cpp_strerror(r
) << dendl
;
100 void CreateImageRequest
<I
>::get_parent_global_image_id() {
103 librados::ObjectReadOperation op
;
104 librbd::cls_client::mirror_image_get_start(&op
, m_remote_parent_spec
.image_id
);
106 librados::AioCompletion
*aio_comp
= create_rados_callback
<
107 CreateImageRequest
<I
>,
108 &CreateImageRequest
<I
>::handle_get_parent_global_image_id
>(this);
110 int r
= m_remote_parent_io_ctx
.aio_operate(RBD_MIRRORING
, aio_comp
, &op
,
116 template <typename I
>
117 void CreateImageRequest
<I
>::handle_get_parent_global_image_id(int r
) {
118 dout(20) << ": r=" << r
<< dendl
;
120 cls::rbd::MirrorImage mirror_image
;
121 bufferlist::iterator iter
= m_out_bl
.begin();
122 r
= librbd::cls_client::mirror_image_get_finish(&iter
, &mirror_image
);
124 m_parent_global_image_id
= mirror_image
.global_image_id
;
125 dout(20) << ": parent_global_image_id=" << m_parent_global_image_id
131 dout(10) << ": parent image " << m_remote_parent_spec
.image_id
<< " not mirrored"
136 derr
<< ": failed to retrieve global image id for parent image "
137 << m_remote_parent_spec
.image_id
<< ": " << cpp_strerror(r
) << dendl
;
142 get_local_parent_image_id();
145 template <typename I
>
146 void CreateImageRequest
<I
>::get_local_parent_image_id() {
149 librados::ObjectReadOperation op
;
150 librbd::cls_client::mirror_image_get_image_id_start(
151 &op
, m_parent_global_image_id
);
153 librados::AioCompletion
*aio_comp
= create_rados_callback
<
154 CreateImageRequest
<I
>,
155 &CreateImageRequest
<I
>::handle_get_local_parent_image_id
>(this);
157 int r
= m_local_parent_io_ctx
.aio_operate(RBD_MIRRORING
, aio_comp
, &op
,
163 template <typename I
>
164 void CreateImageRequest
<I
>::handle_get_local_parent_image_id(int r
) {
165 dout(20) << ": r=" << r
<< dendl
;
168 bufferlist::iterator iter
= m_out_bl
.begin();
169 r
= librbd::cls_client::mirror_image_get_image_id_finish(
170 &iter
, &m_local_parent_spec
.image_id
);
174 dout(10) << ": parent image " << m_parent_global_image_id
<< " not "
175 << "registered locally" << dendl
;
179 derr
<< ": failed to retrieve local image id for parent image "
180 << m_parent_global_image_id
<< ": " << cpp_strerror(r
) << dendl
;
185 open_remote_parent_image();
188 template <typename I
>
189 void CreateImageRequest
<I
>::open_remote_parent_image() {
192 Context
*ctx
= create_context_callback
<
193 CreateImageRequest
<I
>,
194 &CreateImageRequest
<I
>::handle_open_remote_parent_image
>(this);
195 OpenImageRequest
<I
> *request
= OpenImageRequest
<I
>::create(
196 m_remote_parent_io_ctx
, &m_remote_parent_image_ctx
,
197 m_remote_parent_spec
.image_id
, true, ctx
);
201 template <typename I
>
202 void CreateImageRequest
<I
>::handle_open_remote_parent_image(int r
) {
203 dout(20) << ": r=" << r
<< dendl
;
205 derr
<< ": failed to open remote parent image " << m_parent_pool_name
<< "/"
206 << m_remote_parent_spec
.image_id
<< dendl
;
211 open_local_parent_image();
214 template <typename I
>
215 void CreateImageRequest
<I
>::open_local_parent_image() {
218 Context
*ctx
= create_context_callback
<
219 CreateImageRequest
<I
>,
220 &CreateImageRequest
<I
>::handle_open_local_parent_image
>(this);
221 OpenImageRequest
<I
> *request
= OpenImageRequest
<I
>::create(
222 m_local_parent_io_ctx
, &m_local_parent_image_ctx
,
223 m_local_parent_spec
.image_id
, true, ctx
);
227 template <typename I
>
228 void CreateImageRequest
<I
>::handle_open_local_parent_image(int r
) {
229 dout(20) << ": r=" << r
<< dendl
;
231 derr
<< ": failed to open local parent image " << m_parent_pool_name
<< "/"
232 << m_local_parent_spec
.image_id
<< dendl
;
234 close_remote_parent_image();
238 set_local_parent_snap();
241 template <typename I
>
242 void CreateImageRequest
<I
>::set_local_parent_snap() {
246 RWLock::RLocker
remote_snap_locker(m_remote_parent_image_ctx
->snap_lock
);
247 auto it
= m_remote_parent_image_ctx
->snap_info
.find(
248 m_remote_parent_spec
.snap_id
);
249 if (it
!= m_remote_parent_image_ctx
->snap_info
.end()) {
250 m_parent_snap_name
= it
->second
.name
;
254 if (m_parent_snap_name
.empty()) {
256 close_local_parent_image();
259 dout(20) << ": parent_snap_name=" << m_parent_snap_name
<< dendl
;
261 Context
*ctx
= create_context_callback
<
262 CreateImageRequest
<I
>,
263 &CreateImageRequest
<I
>::handle_set_local_parent_snap
>(this);
264 m_local_parent_image_ctx
->state
->snap_set(cls::rbd::UserSnapshotNamespace(),
269 template <typename I
>
270 void CreateImageRequest
<I
>::handle_set_local_parent_snap(int r
) {
271 dout(20) << ": r=" << r
<< dendl
;
273 derr
<< ": failed to set parent snapshot " << m_parent_snap_name
274 << ": " << cpp_strerror(r
) << dendl
;
276 close_local_parent_image();
283 template <typename I
>
284 void CreateImageRequest
<I
>::clone_image() {
287 librbd::ImageOptions opts
;
288 opts
.set(RBD_IMAGE_OPTION_FEATURES
, m_remote_image_ctx
->features
);
289 opts
.set(RBD_IMAGE_OPTION_ORDER
, m_remote_image_ctx
->order
);
290 opts
.set(RBD_IMAGE_OPTION_STRIPE_UNIT
, m_remote_image_ctx
->stripe_unit
);
291 opts
.set(RBD_IMAGE_OPTION_STRIPE_COUNT
, m_remote_image_ctx
->stripe_count
);
293 *m_local_image_id
= librbd::util::generate_image_id(m_local_io_ctx
);;
295 using klass
= CreateImageRequest
<I
>;
296 Context
*ctx
= create_context_callback
<klass
, &klass::handle_clone_image
>(this);
298 librbd::image::CloneRequest
<I
> *req
= librbd::image::CloneRequest
<I
>::create(
299 m_local_parent_image_ctx
, m_local_io_ctx
, m_local_image_name
,
300 *m_local_image_id
, opts
, m_global_image_id
, m_remote_mirror_uuid
,
301 m_remote_image_ctx
->op_work_queue
, ctx
);
305 template <typename I
>
306 void CreateImageRequest
<I
>::handle_clone_image(int r
) {
307 dout(20) << ": r=" << r
<< dendl
;
309 derr
<< ": failed to clone image " << m_parent_pool_name
<< "/"
310 << m_local_parent_image_ctx
->name
<< " to "
311 << m_local_image_name
<< dendl
;
315 close_local_parent_image();
318 template <typename I
>
319 void CreateImageRequest
<I
>::close_local_parent_image() {
321 Context
*ctx
= create_context_callback
<
322 CreateImageRequest
<I
>,
323 &CreateImageRequest
<I
>::handle_close_local_parent_image
>(this);
324 CloseImageRequest
<I
> *request
= CloseImageRequest
<I
>::create(
325 &m_local_parent_image_ctx
, ctx
);
329 template <typename I
>
330 void CreateImageRequest
<I
>::handle_close_local_parent_image(int r
) {
331 dout(20) << ": r=" << r
<< dendl
;
333 derr
<< ": error encountered closing local parent image: "
334 << cpp_strerror(r
) << dendl
;
337 close_remote_parent_image();
340 template <typename I
>
341 void CreateImageRequest
<I
>::close_remote_parent_image() {
343 Context
*ctx
= create_context_callback
<
344 CreateImageRequest
<I
>,
345 &CreateImageRequest
<I
>::handle_close_remote_parent_image
>(this);
346 CloseImageRequest
<I
> *request
= CloseImageRequest
<I
>::create(
347 &m_remote_parent_image_ctx
, ctx
);
351 template <typename I
>
352 void CreateImageRequest
<I
>::handle_close_remote_parent_image(int r
) {
353 dout(20) << ": r=" << r
<< dendl
;
355 derr
<< ": error encountered closing remote parent image: "
356 << cpp_strerror(r
) << dendl
;
362 template <typename I
>
363 void CreateImageRequest
<I
>::error(int r
) {
364 dout(20) << ": r=" << r
<< dendl
;
366 m_work_queue
->queue(create_context_callback
<
367 CreateImageRequest
<I
>, &CreateImageRequest
<I
>::finish
>(this), r
);
370 template <typename I
>
371 void CreateImageRequest
<I
>::finish(int r
) {
372 dout(20) << ": r=" << r
<< dendl
;
373 m_on_finish
->complete(r
);
377 template <typename I
>
378 int CreateImageRequest
<I
>::validate_parent() {
379 RWLock::RLocker
owner_locker(m_remote_image_ctx
->owner_lock
);
380 RWLock::RLocker
snap_locker(m_remote_image_ctx
->snap_lock
);
382 m_remote_parent_spec
= m_remote_image_ctx
->parent_md
.spec
;
384 // scan all remote snapshots for a linked parent
385 for (auto &snap_info_pair
: m_remote_image_ctx
->snap_info
) {
386 auto &parent_spec
= snap_info_pair
.second
.parent
.spec
;
387 if (parent_spec
.pool_id
== -1) {
389 } else if (m_remote_parent_spec
.pool_id
== -1) {
390 m_remote_parent_spec
= parent_spec
;
394 if (m_remote_parent_spec
!= parent_spec
) {
395 derr
<< ": remote image parent spec mismatch" << dendl
;
400 if (m_remote_parent_spec
.pool_id
== -1) {
404 // map remote parent pool to local parent pool
405 librados::Rados
remote_rados(m_remote_image_ctx
->md_ctx
);
406 int r
= remote_rados
.ioctx_create2(m_remote_parent_spec
.pool_id
,
407 m_remote_parent_io_ctx
);
409 derr
<< ": failed to open remote parent pool " << m_remote_parent_spec
.pool_id
410 << ": " << cpp_strerror(r
) << dendl
;
414 m_parent_pool_name
= m_remote_parent_io_ctx
.get_pool_name();
416 librados::Rados
local_rados(m_local_io_ctx
);
417 r
= local_rados
.ioctx_create(m_parent_pool_name
.c_str(),
418 m_local_parent_io_ctx
);
420 derr
<< ": failed to open local parent pool " << m_parent_pool_name
<< ": "
421 << cpp_strerror(r
) << dendl
;
428 } // namespace image_replayer
429 } // namespace mirror
432 template class rbd::mirror::image_replayer::CreateImageRequest
<librbd::ImageCtx
>;