summaryrefslogtreecommitdiffstats
path: root/src/librbd/image/AttachParentRequest.cc
blob: d0c35b6a91cb1cbcc760baeeee5ff9457b2ef135 (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
86
87
88
89
90
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "librbd/image/AttachParentRequest.h"
#include "common/dout.h"
#include "common/errno.h"
#include "cls/rbd/cls_rbd_client.h"
#include "librbd/ImageCtx.h"
#include "librbd/Utils.h"

#define dout_subsys ceph_subsys_rbd
#undef dout_prefix
#define dout_prefix *_dout << "librbd::image::AttachParentRequest: " << this \
                           << " " << __func__ << ": "

namespace librbd {
namespace image {

using util::create_rados_callback;

template <typename I>
void AttachParentRequest<I>::send() {
  attach_parent();
}

template <typename I>
void AttachParentRequest<I>::attach_parent() {
  auto cct = m_image_ctx.cct;
  ldout(cct, 5) << "parent_image_spec=" << m_parent_image_spec << dendl;

  librados::ObjectWriteOperation op;
  if (!m_legacy_parent) {
    librbd::cls_client::parent_attach(&op, m_parent_image_spec,
                                      m_parent_overlap, m_reattach);
  } else {
    librbd::cls_client::set_parent(&op, m_parent_image_spec, m_parent_overlap);
  }

  auto aio_comp = create_rados_callback<
    AttachParentRequest<I>,
    &AttachParentRequest<I>::handle_attach_parent>(this);
  int r = m_image_ctx.md_ctx.aio_operate(m_image_ctx.header_oid, aio_comp, &op);
  ceph_assert(r == 0);
  aio_comp->release();
}

template <typename I>
void AttachParentRequest<I>::handle_attach_parent(int r) {
  auto cct = m_image_ctx.cct;
  ldout(cct, 5) << dendl;

  if (!m_legacy_parent && r == -EOPNOTSUPP && !m_reattach) {
    if (m_parent_image_spec.pool_namespace ==
          m_image_ctx.md_ctx.get_namespace()) {
      m_parent_image_spec.pool_namespace = "";
    }
    if (m_parent_image_spec.pool_namespace.empty()) {
      ldout(cct, 10) << "retrying using legacy parent method" << dendl;
      m_legacy_parent = true;
      attach_parent();
      return;
    }

    // namespaces require newer OSDs
    r = -EXDEV;
  }

  if (r < 0) {
    lderr(cct) << "attach parent encountered an error: " << cpp_strerror(r)
               << dendl;
    finish(r);
    return;
  }

  finish(0);
}

template <typename I>
void AttachParentRequest<I>::finish(int r) {
  auto cct = m_image_ctx.cct;
  ldout(cct, 5) << "r=" << r << dendl;

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

} // namespace image
} // namespace librbd

template class librbd::image::AttachParentRequest<librbd::ImageCtx>;