summaryrefslogtreecommitdiffstats
path: root/src/librbd/image/AttachChildRequest.cc
blob: 2f74191ed328ddf35157f94932105a48140cc8ef (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

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

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

namespace librbd {
namespace image {

using util::create_context_callback;
using util::create_rados_callback;

template <typename I>
AttachChildRequest<I>::AttachChildRequest(
    I *image_ctx, I *parent_image_ctx, const librados::snap_t &parent_snap_id,
    I *old_parent_image_ctx, const librados::snap_t &old_parent_snap_id,
    uint32_t clone_format, Context* on_finish)
    : m_image_ctx(image_ctx), m_parent_image_ctx(parent_image_ctx),
      m_parent_snap_id(parent_snap_id),
      m_old_parent_image_ctx(old_parent_image_ctx),
      m_old_parent_snap_id(old_parent_snap_id), m_clone_format(clone_format),
      m_on_finish(on_finish), m_cct(m_image_ctx->cct) {
}

template <typename I>
void AttachChildRequest<I>::send() {
  if (m_clone_format == 1) {
    v1_add_child();
  } else {
    v2_set_op_feature();
  }
}

template <typename I>
void AttachChildRequest<I>::v1_add_child() {
  ldout(m_cct, 15) << dendl;

  librados::ObjectWriteOperation op;
  cls_client::add_child(&op, {m_parent_image_ctx->md_ctx.get_id(), "",
                              m_parent_image_ctx->id,
                              m_parent_snap_id}, m_image_ctx->id);

  using klass = AttachChildRequest<I>;
  librados::AioCompletion *comp =
    create_rados_callback<klass, &klass::handle_v1_add_child>(this);
  int r = m_image_ctx->md_ctx.aio_operate(RBD_CHILDREN, comp, &op);
  ceph_assert(r == 0);
  comp->release();
}

template <typename I>
void AttachChildRequest<I>::handle_v1_add_child(int r) {
  ldout(m_cct, 15) << "r=" << r << dendl;

  if (r < 0) {
    if (r == -EEXIST && m_old_parent_image_ctx != nullptr) {
      ldout(m_cct, 5) << "child already exists" << dendl;
    } else {
      lderr(m_cct) << "couldn't add child: " << cpp_strerror(r) << dendl;
      finish(r);
      return;
    }
  }

  v1_refresh();
}

template <typename I>
void AttachChildRequest<I>::v1_refresh() {
  ldout(m_cct, 15) << dendl;

  using klass = AttachChildRequest<I>;
  RefreshRequest<I> *req = RefreshRequest<I>::create(
      *m_parent_image_ctx, false, false,
      create_context_callback<klass, &klass::handle_v1_refresh>(this));
  req->send();
}

template <typename I>
void AttachChildRequest<I>::handle_v1_refresh(int r) {
  ldout(m_cct, 15) << "r=" << r << dendl;

  bool snap_protected = false;
  if (r == 0) {
    std::shared_lock image_locker{m_parent_image_ctx->image_lock};
    r = m_parent_image_ctx->is_snap_protected(m_parent_snap_id,
                                              &snap_protected);
  }

  if (r < 0 || !snap_protected) {
    lderr(m_cct) << "validate protected failed" << dendl;
    finish(-EINVAL);
    return;
  }

  v1_remove_child_from_old_parent();
}

template <typename I>
void AttachChildRequest<I>::v1_remove_child_from_old_parent() {
  if (m_old_parent_image_ctx == nullptr) {
    finish(0);
    return;
  }

  ldout(m_cct, 15) << dendl;

  librados::ObjectWriteOperation op;
  cls_client::remove_child(&op, {m_old_parent_image_ctx->md_ctx.get_id(),
                                 m_old_parent_image_ctx->md_ctx.get_namespace(),
                                 m_old_parent_image_ctx->id,
                                 m_old_parent_snap_id}, m_image_ctx->id);

  using klass = AttachChildRequest<I>;
  librados::AioCompletion *comp = create_rados_callback<
      klass, &klass::handle_v1_remove_child_from_old_parent>(this);
  int r = m_image_ctx->md_ctx.aio_operate(RBD_CHILDREN, comp, &op);
  ceph_assert(r == 0);
  comp->release();
}

template <typename I>
void AttachChildRequest<I>::handle_v1_remove_child_from_old_parent(int r) {
  ldout(m_cct, 15) << "r=" << r << dendl;

  if (r < 0 && r != -ENOENT) {
    lderr(m_cct) << "couldn't remove child: " << cpp_strerror(r) << dendl;
    finish(r);
    return;
  }

  finish(0);
}

template <typename I>
void AttachChildRequest<I>::v2_set_op_feature() {
  ldout(m_cct, 15) << dendl;

  librados::ObjectWriteOperation op;
  cls_client::op_features_set(&op, RBD_OPERATION_FEATURE_CLONE_CHILD,
                              RBD_OPERATION_FEATURE_CLONE_CHILD);

  using klass = AttachChildRequest<I>;
  auto aio_comp = create_rados_callback<
      klass, &klass::handle_v2_set_op_feature>(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 AttachChildRequest<I>::handle_v2_set_op_feature(int r) {
  ldout(m_cct, 15) << "r=" << r << dendl;

  if (r < 0) {
    lderr(m_cct) << "failed to enable clone v2: " << cpp_strerror(r) << dendl;
    finish(r);
    return;
  }

  v2_child_attach();
}

template <typename I>
void AttachChildRequest<I>::v2_child_attach() {
  ldout(m_cct, 15) << dendl;

  librados::ObjectWriteOperation op;
  cls_client::child_attach(&op, m_parent_snap_id,
                           {m_image_ctx->md_ctx.get_id(),
                            m_image_ctx->md_ctx.get_namespace(),
                            m_image_ctx->id});

  using klass = AttachChildRequest<I>;
  auto aio_comp = create_rados_callback<
      klass, &klass::handle_v2_child_attach>(this);
  int r = m_parent_image_ctx->md_ctx.aio_operate(m_parent_image_ctx->header_oid,
                                                 aio_comp, &op);
  ceph_assert(r == 0);
  aio_comp->release();
}

template <typename I>
void AttachChildRequest<I>::handle_v2_child_attach(int r) {
  ldout(m_cct, 15) << "r=" << r << dendl;

  if (r < 0) {
    if (r == -EEXIST && m_old_parent_image_ctx != nullptr) {
      ldout(m_cct, 5) << "child already exists" << dendl;
    } else {
      lderr(m_cct) << "failed to attach child image: " << cpp_strerror(r)
                   << dendl;
      finish(r);
      return;
    }
  }

  v2_child_detach_from_old_parent();
}

template <typename I>
void AttachChildRequest<I>::v2_child_detach_from_old_parent() {
  if (m_old_parent_image_ctx == nullptr) {
    finish(0);
    return;
  }

  ldout(m_cct, 15) << dendl;

  librados::ObjectWriteOperation op;
  cls_client::child_detach(&op, m_old_parent_snap_id,
                           {m_image_ctx->md_ctx.get_id(),
                            m_image_ctx->md_ctx.get_namespace(),
                            m_image_ctx->id});

  using klass = AttachChildRequest<I>;
  auto aio_comp = create_rados_callback<
      klass, &klass::handle_v2_child_detach_from_old_parent>(this);
  int r = m_old_parent_image_ctx->md_ctx.aio_operate(
      m_old_parent_image_ctx->header_oid, aio_comp, &op);
  ceph_assert(r == 0);
  aio_comp->release();
}

template <typename I>
void AttachChildRequest<I>::handle_v2_child_detach_from_old_parent(int r) {
  ldout(m_cct, 15) << "r=" << r << dendl;

  if (r < 0 && r != -ENOENT) {
    lderr(m_cct) << "failed to detach child image: " << cpp_strerror(r)
                 << dendl;
    finish(r);
    return;
  }

  finish(0);
}

template <typename I>
void AttachChildRequest<I>::finish(int r) {
  ldout(m_cct, 5) << "r=" << r << dendl;

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

} // namespace image
} // namespace librbd

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