]>
Commit | Line | Data |
---|---|---|
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 "OpenImageRequest.h" | |
11fdf7f2 | 5 | #include "common/debug.h" |
7c673cae FG |
6 | #include "common/errno.h" |
7 | #include "librbd/ImageCtx.h" | |
8 | #include "librbd/ImageState.h" | |
9 | #include "librbd/Utils.h" | |
10 | #include <type_traits> | |
11 | ||
12 | #define dout_context g_ceph_context | |
13 | #define dout_subsys ceph_subsys_rbd_mirror | |
14 | #undef dout_prefix | |
15 | #define dout_prefix *_dout << "rbd::mirror::image_replayer::OpenImageRequest: " \ | |
16 | << this << " " << __func__ << " " | |
17 | ||
18 | namespace rbd { | |
19 | namespace mirror { | |
20 | namespace image_replayer { | |
21 | ||
22 | using librbd::util::create_context_callback; | |
23 | ||
24 | template <typename I> | |
25 | OpenImageRequest<I>::OpenImageRequest(librados::IoCtx &io_ctx, I **image_ctx, | |
26 | const std::string &image_id, | |
27 | bool read_only, Context *on_finish) | |
28 | : m_io_ctx(io_ctx), m_image_ctx(image_ctx), m_image_id(image_id), | |
29 | m_read_only(read_only), m_on_finish(on_finish) { | |
30 | } | |
31 | ||
32 | template <typename I> | |
33 | void OpenImageRequest<I>::send() { | |
34 | send_open_image(); | |
35 | } | |
36 | ||
37 | template <typename I> | |
38 | void OpenImageRequest<I>::send_open_image() { | |
39 | dout(20) << dendl; | |
40 | ||
41 | *m_image_ctx = I::create("", m_image_id, nullptr, m_io_ctx, m_read_only); | |
42 | ||
1911f103 TL |
43 | if (!m_read_only) { |
44 | // ensure non-primary images can be modified | |
45 | (*m_image_ctx)->read_only_mask = ~librbd::IMAGE_READ_ONLY_FLAG_NON_PRIMARY; | |
46 | } | |
47 | ||
7c673cae FG |
48 | Context *ctx = create_context_callback< |
49 | OpenImageRequest<I>, &OpenImageRequest<I>::handle_open_image>( | |
50 | this); | |
11fdf7f2 | 51 | (*m_image_ctx)->state->open(0, ctx); |
7c673cae FG |
52 | } |
53 | ||
54 | template <typename I> | |
55 | void OpenImageRequest<I>::handle_open_image(int r) { | |
56 | dout(20) << ": r=" << r << dendl; | |
57 | ||
58 | if (r < 0) { | |
59 | derr << ": failed to open image '" << m_image_id << "': " | |
60 | << cpp_strerror(r) << dendl; | |
61 | (*m_image_ctx)->destroy(); | |
62 | *m_image_ctx = nullptr; | |
63 | } | |
64 | ||
65 | finish(r); | |
66 | } | |
67 | ||
68 | template <typename I> | |
69 | void OpenImageRequest<I>::finish(int r) { | |
70 | dout(20) << ": r=" << r << dendl; | |
71 | ||
72 | m_on_finish->complete(r); | |
73 | delete this; | |
74 | } | |
75 | ||
76 | } // namespace image_replayer | |
77 | } // namespace mirror | |
78 | } // namespace rbd | |
79 | ||
80 | template class rbd::mirror::image_replayer::OpenImageRequest<librbd::ImageCtx>; |