summaryrefslogtreecommitdiffstats
path: root/src/tools/rbd_mirror/image_replayer/GetMirrorImageIdRequest.cc
blob: 74e975373832433f75c8ac45567b49f8b675f7d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "tools/rbd_mirror/image_replayer/GetMirrorImageIdRequest.h"
#include "include/rados/librados.hpp"
#include "cls/rbd/cls_rbd_client.h"
#include "common/debug.h"
#include "common/errno.h"
#include "librbd/ImageCtx.h"
#include "librbd/Utils.h"

#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_rbd_mirror
#undef dout_prefix
#define dout_prefix *_dout << "rbd::mirror::image_replayer::" \
                           << "GetMirrorImageIdRequest: " << this << " " \
                           << __func__ << ": "

namespace rbd {
namespace mirror {
namespace image_replayer {

using librbd::util::create_rados_callback;

template <typename I>
void GetMirrorImageIdRequest<I>::send() {
  dout(20) << dendl;
  get_image_id();
}

template <typename I>
void GetMirrorImageIdRequest<I>::get_image_id() {
  dout(20) << dendl;

  // attempt to cross-reference a image id by the global image id
  librados::ObjectReadOperation op;
  librbd::cls_client::mirror_image_get_image_id_start(&op, m_global_image_id);

  librados::AioCompletion *aio_comp = create_rados_callback<
    GetMirrorImageIdRequest<I>,
    &GetMirrorImageIdRequest<I>::handle_get_image_id>(
      this);
  int r = m_io_ctx.aio_operate(RBD_MIRRORING, aio_comp, &op, &m_out_bl);
  ceph_assert(r == 0);
  aio_comp->release();
}

template <typename I>
void GetMirrorImageIdRequest<I>::handle_get_image_id(int r) {
  if (r == 0) {
    auto iter = m_out_bl.cbegin();
    r = librbd::cls_client::mirror_image_get_image_id_finish(
      &iter, m_image_id);
  }

  dout(20) << "r=" << r << ", "
           << "image_id=" << *m_image_id << dendl;

  if (r < 0) {
    if (r == -ENOENT) {
      dout(10) << "global image " << m_global_image_id << " not registered"
               << dendl;
    } else {
      derr << "failed to retrieve image id: " << cpp_strerror(r) << dendl;
    }
    finish(r);
    return;
  }

  finish(0);
}

template <typename I>
void GetMirrorImageIdRequest<I>::finish(int r) {
  dout(20) << "r=" << r << dendl;

  m_on_finish->complete(r);
  delete this;
}

} // namespace image_replayer
} // namespace mirror
} // namespace rbd

template class rbd::mirror::image_replayer::GetMirrorImageIdRequest<librbd::ImageCtx>;