]> git.proxmox.com Git - ceph.git/blame - ceph/src/tools/rbd_mirror/image_replayer/OpenImageRequest.cc
import 15.2.2 octopus source
[ceph.git] / ceph / src / tools / rbd_mirror / image_replayer / OpenImageRequest.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 "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
18namespace rbd {
19namespace mirror {
20namespace image_replayer {
21
22using librbd::util::create_context_callback;
23
24template <typename I>
25OpenImageRequest<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
32template <typename I>
33void OpenImageRequest<I>::send() {
34 send_open_image();
35}
36
37template <typename I>
38void 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
54template <typename I>
55void 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
68template <typename I>
69void 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
80template class rbd::mirror::image_replayer::OpenImageRequest<librbd::ImageCtx>;