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
,
36 const 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_local_image_id(local_image_id
),
43 m_remote_image_ctx(remote_image_ctx
), 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 librbd::image::CreateRequest
<I
> *req
= librbd::image::CreateRequest
<I
>::create(
79 m_local_io_ctx
, m_local_image_name
, m_local_image_id
,
80 m_remote_image_ctx
->size
, image_options
, m_global_image_id
,
81 m_remote_mirror_uuid
, false, m_remote_image_ctx
->op_work_queue
, ctx
);
86 void CreateImageRequest
<I
>::handle_create_image(int r
) {
87 dout(20) << ": r=" << r
<< dendl
;
89 derr
<< ": failed to create local image: " << cpp_strerror(r
) << dendl
;
98 void CreateImageRequest
<I
>::get_parent_global_image_id() {
101 librados::ObjectReadOperation op
;
102 librbd::cls_client::mirror_image_get_start(&op
, m_remote_parent_spec
.image_id
);
104 librados::AioCompletion
*aio_comp
= create_rados_callback
<
105 CreateImageRequest
<I
>,
106 &CreateImageRequest
<I
>::handle_get_parent_global_image_id
>(this);
108 int r
= m_remote_parent_io_ctx
.aio_operate(RBD_MIRRORING
, aio_comp
, &op
,
114 template <typename I
>
115 void CreateImageRequest
<I
>::handle_get_parent_global_image_id(int r
) {
116 dout(20) << ": r=" << r
<< dendl
;
118 cls::rbd::MirrorImage mirror_image
;
119 bufferlist::iterator iter
= m_out_bl
.begin();
120 r
= librbd::cls_client::mirror_image_get_finish(&iter
, &mirror_image
);
122 m_parent_global_image_id
= mirror_image
.global_image_id
;
123 dout(20) << ": parent_global_image_id=" << m_parent_global_image_id
129 dout(10) << ": parent image " << m_remote_parent_spec
.image_id
<< " not mirrored"
134 derr
<< ": failed to retrieve global image id for parent image "
135 << m_remote_parent_spec
.image_id
<< ": " << cpp_strerror(r
) << dendl
;
140 get_local_parent_image_id();
143 template <typename I
>
144 void CreateImageRequest
<I
>::get_local_parent_image_id() {
147 librados::ObjectReadOperation op
;
148 librbd::cls_client::mirror_image_get_image_id_start(
149 &op
, m_parent_global_image_id
);
151 librados::AioCompletion
*aio_comp
= create_rados_callback
<
152 CreateImageRequest
<I
>,
153 &CreateImageRequest
<I
>::handle_get_local_parent_image_id
>(this);
155 int r
= m_local_parent_io_ctx
.aio_operate(RBD_MIRRORING
, aio_comp
, &op
,
161 template <typename I
>
162 void CreateImageRequest
<I
>::handle_get_local_parent_image_id(int r
) {
163 dout(20) << ": r=" << r
<< dendl
;
166 bufferlist::iterator iter
= m_out_bl
.begin();
167 r
= librbd::cls_client::mirror_image_get_image_id_finish(
168 &iter
, &m_local_parent_spec
.image_id
);
172 dout(10) << ": parent image " << m_parent_global_image_id
<< " not "
173 << "registered locally" << dendl
;
177 derr
<< ": failed to retrieve local image id for parent image "
178 << m_parent_global_image_id
<< ": " << cpp_strerror(r
) << dendl
;
183 open_remote_parent_image();
186 template <typename I
>
187 void CreateImageRequest
<I
>::open_remote_parent_image() {
190 Context
*ctx
= create_context_callback
<
191 CreateImageRequest
<I
>,
192 &CreateImageRequest
<I
>::handle_open_remote_parent_image
>(this);
193 OpenImageRequest
<I
> *request
= OpenImageRequest
<I
>::create(
194 m_remote_parent_io_ctx
, &m_remote_parent_image_ctx
,
195 m_remote_parent_spec
.image_id
, true, ctx
);
199 template <typename I
>
200 void CreateImageRequest
<I
>::handle_open_remote_parent_image(int r
) {
201 dout(20) << ": r=" << r
<< dendl
;
203 derr
<< ": failed to open remote parent image " << m_parent_pool_name
<< "/"
204 << m_remote_parent_spec
.image_id
<< dendl
;
209 open_local_parent_image();
212 template <typename I
>
213 void CreateImageRequest
<I
>::open_local_parent_image() {
216 Context
*ctx
= create_context_callback
<
217 CreateImageRequest
<I
>,
218 &CreateImageRequest
<I
>::handle_open_local_parent_image
>(this);
219 OpenImageRequest
<I
> *request
= OpenImageRequest
<I
>::create(
220 m_local_parent_io_ctx
, &m_local_parent_image_ctx
,
221 m_local_parent_spec
.image_id
, true, ctx
);
225 template <typename I
>
226 void CreateImageRequest
<I
>::handle_open_local_parent_image(int r
) {
227 dout(20) << ": r=" << r
<< dendl
;
229 derr
<< ": failed to open local parent image " << m_parent_pool_name
<< "/"
230 << m_local_parent_spec
.image_id
<< dendl
;
232 close_remote_parent_image();
236 set_local_parent_snap();
239 template <typename I
>
240 void CreateImageRequest
<I
>::set_local_parent_snap() {
244 RWLock::RLocker
remote_snap_locker(m_remote_parent_image_ctx
->snap_lock
);
245 auto it
= m_remote_parent_image_ctx
->snap_info
.find(
246 m_remote_parent_spec
.snap_id
);
247 if (it
!= m_remote_parent_image_ctx
->snap_info
.end()) {
248 m_parent_snap_name
= it
->second
.name
;
252 if (m_parent_snap_name
.empty()) {
254 close_local_parent_image();
257 dout(20) << ": parent_snap_name=" << m_parent_snap_name
<< dendl
;
259 Context
*ctx
= create_context_callback
<
260 CreateImageRequest
<I
>,
261 &CreateImageRequest
<I
>::handle_set_local_parent_snap
>(this);
262 m_local_parent_image_ctx
->state
->snap_set(cls::rbd::UserSnapshotNamespace(),
267 template <typename I
>
268 void CreateImageRequest
<I
>::handle_set_local_parent_snap(int r
) {
269 dout(20) << ": r=" << r
<< dendl
;
271 derr
<< ": failed to set parent snapshot " << m_parent_snap_name
272 << ": " << cpp_strerror(r
) << dendl
;
274 close_local_parent_image();
281 template <typename I
>
282 void CreateImageRequest
<I
>::clone_image() {
285 librbd::ImageOptions opts
;
286 opts
.set(RBD_IMAGE_OPTION_FEATURES
, m_remote_image_ctx
->features
);
287 opts
.set(RBD_IMAGE_OPTION_ORDER
, m_remote_image_ctx
->order
);
288 opts
.set(RBD_IMAGE_OPTION_STRIPE_UNIT
, m_remote_image_ctx
->stripe_unit
);
289 opts
.set(RBD_IMAGE_OPTION_STRIPE_COUNT
, m_remote_image_ctx
->stripe_count
);
291 using klass
= CreateImageRequest
<I
>;
292 Context
*ctx
= create_context_callback
<klass
, &klass::handle_clone_image
>(this);
294 librbd::image::CloneRequest
<I
> *req
= librbd::image::CloneRequest
<I
>::create(
295 m_local_parent_image_ctx
, m_local_io_ctx
, m_local_image_name
,
296 m_local_image_id
, opts
, m_global_image_id
, m_remote_mirror_uuid
,
297 m_remote_image_ctx
->op_work_queue
, ctx
);
301 template <typename I
>
302 void CreateImageRequest
<I
>::handle_clone_image(int r
) {
303 dout(20) << ": r=" << r
<< dendl
;
305 derr
<< ": failed to clone image " << m_parent_pool_name
<< "/"
306 << m_local_parent_image_ctx
->name
<< " to "
307 << m_local_image_name
<< dendl
;
311 close_local_parent_image();
314 template <typename I
>
315 void CreateImageRequest
<I
>::close_local_parent_image() {
317 Context
*ctx
= create_context_callback
<
318 CreateImageRequest
<I
>,
319 &CreateImageRequest
<I
>::handle_close_local_parent_image
>(this);
320 CloseImageRequest
<I
> *request
= CloseImageRequest
<I
>::create(
321 &m_local_parent_image_ctx
, ctx
);
325 template <typename I
>
326 void CreateImageRequest
<I
>::handle_close_local_parent_image(int r
) {
327 dout(20) << ": r=" << r
<< dendl
;
329 derr
<< ": error encountered closing local parent image: "
330 << cpp_strerror(r
) << dendl
;
333 close_remote_parent_image();
336 template <typename I
>
337 void CreateImageRequest
<I
>::close_remote_parent_image() {
339 Context
*ctx
= create_context_callback
<
340 CreateImageRequest
<I
>,
341 &CreateImageRequest
<I
>::handle_close_remote_parent_image
>(this);
342 CloseImageRequest
<I
> *request
= CloseImageRequest
<I
>::create(
343 &m_remote_parent_image_ctx
, ctx
);
347 template <typename I
>
348 void CreateImageRequest
<I
>::handle_close_remote_parent_image(int r
) {
349 dout(20) << ": r=" << r
<< dendl
;
351 derr
<< ": error encountered closing remote parent image: "
352 << cpp_strerror(r
) << dendl
;
358 template <typename I
>
359 void CreateImageRequest
<I
>::error(int r
) {
360 dout(20) << ": r=" << r
<< dendl
;
362 m_work_queue
->queue(create_context_callback
<
363 CreateImageRequest
<I
>, &CreateImageRequest
<I
>::finish
>(this), r
);
366 template <typename I
>
367 void CreateImageRequest
<I
>::finish(int r
) {
368 dout(20) << ": r=" << r
<< dendl
;
369 m_on_finish
->complete(r
);
373 template <typename I
>
374 int CreateImageRequest
<I
>::validate_parent() {
375 RWLock::RLocker
owner_locker(m_remote_image_ctx
->owner_lock
);
376 RWLock::RLocker
snap_locker(m_remote_image_ctx
->snap_lock
);
378 m_remote_parent_spec
= m_remote_image_ctx
->parent_md
.spec
;
380 // scan all remote snapshots for a linked parent
381 for (auto &snap_info_pair
: m_remote_image_ctx
->snap_info
) {
382 auto &parent_spec
= snap_info_pair
.second
.parent
.spec
;
383 if (parent_spec
.pool_id
== -1) {
385 } else if (m_remote_parent_spec
.pool_id
== -1) {
386 m_remote_parent_spec
= parent_spec
;
390 if (m_remote_parent_spec
!= parent_spec
) {
391 derr
<< ": remote image parent spec mismatch" << dendl
;
396 if (m_remote_parent_spec
.pool_id
== -1) {
400 // map remote parent pool to local parent pool
401 librados::Rados
remote_rados(m_remote_image_ctx
->md_ctx
);
402 int r
= remote_rados
.ioctx_create2(m_remote_parent_spec
.pool_id
,
403 m_remote_parent_io_ctx
);
405 derr
<< ": failed to open remote parent pool " << m_remote_parent_spec
.pool_id
406 << ": " << cpp_strerror(r
) << dendl
;
410 m_parent_pool_name
= m_remote_parent_io_ctx
.get_pool_name();
412 librados::Rados
local_rados(m_local_io_ctx
);
413 r
= local_rados
.ioctx_create(m_parent_pool_name
.c_str(),
414 m_local_parent_io_ctx
);
416 derr
<< ": failed to open local parent pool " << m_parent_pool_name
<< ": "
417 << cpp_strerror(r
) << dendl
;
424 } // namespace image_replayer
425 } // namespace mirror
428 template class rbd::mirror::image_replayer::CreateImageRequest
<librbd::ImageCtx
>;