]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/rbd_mirror/image_replayer/OpenImageRequest.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / tools / rbd_mirror / image_replayer / OpenImageRequest.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include "OpenImageRequest.h"
5 #include "common/errno.h"
6 #include "librbd/ImageCtx.h"
7 #include "librbd/ImageState.h"
8 #include "librbd/Utils.h"
9 #include <type_traits>
10
11 #define dout_context g_ceph_context
12 #define dout_subsys ceph_subsys_rbd_mirror
13 #undef dout_prefix
14 #define dout_prefix *_dout << "rbd::mirror::image_replayer::OpenImageRequest: " \
15 << this << " " << __func__ << " "
16
17 namespace rbd {
18 namespace mirror {
19 namespace image_replayer {
20
21 using librbd::util::create_context_callback;
22
23 template <typename I>
24 OpenImageRequest<I>::OpenImageRequest(librados::IoCtx &io_ctx, I **image_ctx,
25 const std::string &image_id,
26 bool read_only, Context *on_finish)
27 : m_io_ctx(io_ctx), m_image_ctx(image_ctx), m_image_id(image_id),
28 m_read_only(read_only), m_on_finish(on_finish) {
29 }
30
31 template <typename I>
32 void OpenImageRequest<I>::send() {
33 send_open_image();
34 }
35
36 template <typename I>
37 void OpenImageRequest<I>::send_open_image() {
38 dout(20) << dendl;
39
40 *m_image_ctx = I::create("", m_image_id, nullptr, m_io_ctx, m_read_only);
41
42 Context *ctx = create_context_callback<
43 OpenImageRequest<I>, &OpenImageRequest<I>::handle_open_image>(
44 this);
45 (*m_image_ctx)->state->open(false, ctx);
46 }
47
48 template <typename I>
49 void OpenImageRequest<I>::handle_open_image(int r) {
50 dout(20) << ": r=" << r << dendl;
51
52 if (r < 0) {
53 derr << ": failed to open image '" << m_image_id << "': "
54 << cpp_strerror(r) << dendl;
55 (*m_image_ctx)->destroy();
56 *m_image_ctx = nullptr;
57 }
58
59 finish(r);
60 }
61
62 template <typename I>
63 void OpenImageRequest<I>::finish(int r) {
64 dout(20) << ": r=" << r << dendl;
65
66 m_on_finish->complete(r);
67 delete this;
68 }
69
70 } // namespace image_replayer
71 } // namespace mirror
72 } // namespace rbd
73
74 template class rbd::mirror::image_replayer::OpenImageRequest<librbd::ImageCtx>;