summaryrefslogtreecommitdiffstats
path: root/src/librbd/DeepCopyRequest.cc
blob: af26ef0c9d78ee9d218d908159fd24977097b3c9 (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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "DeepCopyRequest.h"
#include "common/errno.h"
#include "librbd/ExclusiveLock.h"
#include "librbd/ImageCtx.h"
#include "librbd/ObjectMap.h"
#include "librbd/Utils.h"
#include "librbd/deep_copy/ImageCopyRequest.h"
#include "librbd/deep_copy/MetadataCopyRequest.h"
#include "librbd/deep_copy/SnapshotCopyRequest.h"
#include "librbd/internal.h"

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

namespace librbd {

using namespace librbd::deep_copy;

using librbd::util::create_context_callback;
using librbd::util::create_rados_callback;
using librbd::util::unique_lock_name;

template <typename I>
DeepCopyRequest<I>::DeepCopyRequest(I *src_image_ctx, I *dst_image_ctx,
                                    librados::snap_t src_snap_id_start,
                                    librados::snap_t src_snap_id_end,
                                    librados::snap_t dst_snap_id_start,
                                    bool flatten,
                                    const ObjectNumber &object_number,
                                    asio::ContextWQ *work_queue,
                                    SnapSeqs *snap_seqs,
                                    deep_copy::Handler *handler,
                                    Context *on_finish)
  : RefCountedObject(dst_image_ctx->cct), m_src_image_ctx(src_image_ctx),
    m_dst_image_ctx(dst_image_ctx), m_src_snap_id_start(src_snap_id_start),
    m_src_snap_id_end(src_snap_id_end), m_dst_snap_id_start(dst_snap_id_start),
    m_flatten(flatten), m_object_number(object_number),
    m_work_queue(work_queue), m_snap_seqs(snap_seqs), m_handler(handler),
    m_on_finish(on_finish), m_cct(dst_image_ctx->cct),
    m_lock(ceph::make_mutex(unique_lock_name("DeepCopyRequest::m_lock", this))) {
}

template <typename I>
DeepCopyRequest<I>::~DeepCopyRequest() {
  ceph_assert(m_snapshot_copy_request == nullptr);
  ceph_assert(m_image_copy_request == nullptr);
}

template <typename I>
void DeepCopyRequest<I>::send() {
  if (!m_src_image_ctx->data_ctx.is_valid()) {
    lderr(m_cct) << "missing data pool for source image" << dendl;
    finish(-ENODEV);
    return;
  }

  if (!m_dst_image_ctx->data_ctx.is_valid()) {
    lderr(m_cct) << "missing data pool for destination image" << dendl;
    finish(-ENODEV);
    return;
  }

  int r = validate_copy_points();
  if (r < 0) {
    finish(r);
    return;
  }

  send_copy_snapshots();
}

template <typename I>
void DeepCopyRequest<I>::cancel() {
  std::lock_guard locker{m_lock};

  ldout(m_cct, 20) << dendl;

  m_canceled = true;

  if (m_snapshot_copy_request != nullptr) {
    m_snapshot_copy_request->cancel();
  }

  if (m_image_copy_request != nullptr) {
    m_image_copy_request->cancel();
  }
}

template <typename I>
void DeepCopyRequest<I>::send_copy_snapshots() {
  m_lock.lock();
  if (m_canceled) {
    m_lock.unlock();
    finish(-ECANCELED);
    return;
  }

  ldout(m_cct, 20) << dendl;

  Context *ctx = create_context_callback<
    DeepCopyRequest<I>, &DeepCopyRequest<I>::handle_copy_snapshots>(this);
  m_snapshot_copy_request = SnapshotCopyRequest<I>::create(
    m_src_image_ctx, m_dst_image_ctx, m_src_snap_id_start, m_src_snap_id_end,
    m_dst_snap_id_start, m_flatten, m_work_queue, m_snap_seqs, ctx);
  m_snapshot_copy_request->get();
  m_lock.unlock();

  m_snapshot_copy_request->send();
}

template <typename I>
void DeepCopyRequest<I>::handle_copy_snapshots(int r) {
  ldout(m_cct, 20) << "r=" << r << dendl;

  {
    std::lock_guard locker{m_lock};
    m_snapshot_copy_request->put();
    m_snapshot_copy_request = nullptr;
    if (r == 0 && m_canceled) {
      r = -ECANCELED;
    }
  }

  if (r == -ECANCELED) {
    ldout(m_cct, 10) << "snapshot copy canceled" << dendl;
    finish(r);
    return;
  } else if (r < 0) {
    lderr(m_cct) << "failed to copy snapshot metadata: " << cpp_strerror(r)
                 << dendl;
    finish(r);
    return;
  }

  if (m_src_snap_id_end == CEPH_NOSNAP) {
    (*m_snap_seqs)[CEPH_NOSNAP] = CEPH_NOSNAP;
  }

  send_copy_image();
}

template <typename I>
void DeepCopyRequest<I>::send_copy_image() {
  m_lock.lock();
  if (m_canceled) {
    m_lock.unlock();
    finish(-ECANCELED);
    return;
  }

  ldout(m_cct, 20) << dendl;

  Context *ctx = create_context_callback<
    DeepCopyRequest<I>, &DeepCopyRequest<I>::handle_copy_image>(this);
  m_image_copy_request = ImageCopyRequest<I>::create(
    m_src_image_ctx, m_dst_image_ctx, m_src_snap_id_start, m_src_snap_id_end,
    m_dst_snap_id_start, m_flatten, m_object_number, *m_snap_seqs, m_handler,
    ctx);
  m_image_copy_request->get();
  m_lock.unlock();

  m_image_copy_request->send();
}

template <typename I>
void DeepCopyRequest<I>::handle_copy_image(int r) {
  ldout(m_cct, 20) << "r=" << r << dendl;

  {
    std::lock_guard locker{m_lock};
    m_image_copy_request->put();
    m_image_copy_request = nullptr;
    if (r == 0 && m_canceled) {
      r = -ECANCELED;
    }
  }

  if (r == -ECANCELED) {
    ldout(m_cct, 10) << "image copy canceled" << dendl;
    finish(r);
    return;
  } else if (r < 0) {
    lderr(m_cct) << "failed to copy image: " << cpp_strerror(r) << dendl;
    finish(r);
    return;
  }

  send_copy_object_map();
}

template <typename I>
void DeepCopyRequest<I>::send_copy_object_map() {
  m_dst_image_ctx->owner_lock.lock_shared();
  m_dst_image_ctx->image_lock.lock_shared();

  if (!m_dst_image_ctx->test_features(RBD_FEATURE_OBJECT_MAP,
                                      m_dst_image_ctx->image_lock)) {
    m_dst_image_ctx->image_lock.unlock_shared();
    m_dst_image_ctx->owner_lock.unlock_shared();
    send_copy_metadata();
    return;
  }
  if (m_src_snap_id_end == CEPH_NOSNAP) {
    m_dst_image_ctx->image_lock.unlock_shared();
    m_dst_image_ctx->owner_lock.unlock_shared();
    send_refresh_object_map();
    return;
  }

  ceph_assert(m_dst_image_ctx->object_map != nullptr);

  ldout(m_cct, 20) << dendl;

  Context *finish_op_ctx = nullptr;
  int r;
  if (m_dst_image_ctx->exclusive_lock != nullptr) {
    finish_op_ctx = m_dst_image_ctx->exclusive_lock->start_op(&r);
  }
  if (finish_op_ctx == nullptr) {
    lderr(m_cct) << "lost exclusive lock" << dendl;
    m_dst_image_ctx->image_lock.unlock_shared();
    m_dst_image_ctx->owner_lock.unlock_shared();
    finish(r);
    return;
  }

  // rollback the object map (copy snapshot object map to HEAD)
  auto ctx = new LambdaContext([this, finish_op_ctx](int r) {
      handle_copy_object_map(r);
      finish_op_ctx->complete(0);
    });
  ceph_assert(m_snap_seqs->count(m_src_snap_id_end) > 0);
  librados::snap_t copy_snap_id = (*m_snap_seqs)[m_src_snap_id_end];
  m_dst_image_ctx->object_map->rollback(copy_snap_id, ctx);
  m_dst_image_ctx->image_lock.unlock_shared();
  m_dst_image_ctx->owner_lock.unlock_shared();
}

template <typename I>
void DeepCopyRequest<I>::handle_copy_object_map(int r) {
  ldout(m_cct, 20) << dendl;

  if (r < 0) {
    lderr(m_cct) << "failed to roll back object map: " << cpp_strerror(r)
                 << dendl;
    finish(r);
    return;
  }

  send_refresh_object_map();
}

template <typename I>
void DeepCopyRequest<I>::send_refresh_object_map() {
  int r;
  Context *finish_op_ctx = nullptr;
  {
    std::shared_lock owner_locker{m_dst_image_ctx->owner_lock};
    if (m_dst_image_ctx->exclusive_lock != nullptr) {
      finish_op_ctx = m_dst_image_ctx->exclusive_lock->start_op(&r);
    }
  }
  if (finish_op_ctx == nullptr) {
    lderr(m_cct) << "lost exclusive lock" << dendl;
    finish(r);
    return;
  }

  ldout(m_cct, 20) << dendl;

  auto ctx = new LambdaContext([this, finish_op_ctx](int r) {
      handle_refresh_object_map(r);
      finish_op_ctx->complete(0);
    });
  m_object_map = m_dst_image_ctx->create_object_map(CEPH_NOSNAP);
  m_object_map->open(ctx);
}

template <typename I>
void DeepCopyRequest<I>::handle_refresh_object_map(int r) {
  ldout(m_cct, 20) << "r=" << r << dendl;

  if (r < 0) {
    lderr(m_cct) << "failed to open object map: " << cpp_strerror(r)
                 << dendl;
    delete m_object_map;

    finish(r);
    return;
  }

  {
    std::unique_lock image_locker{m_dst_image_ctx->image_lock};
    std::swap(m_dst_image_ctx->object_map, m_object_map);
  }
  m_object_map->put();

  send_copy_metadata();
}

template <typename I>
void DeepCopyRequest<I>::send_copy_metadata() {
  ldout(m_cct, 20) << dendl;

  Context *ctx = create_context_callback<
    DeepCopyRequest<I>, &DeepCopyRequest<I>::handle_copy_metadata>(this);
  auto request = MetadataCopyRequest<I>::create(m_src_image_ctx,
                                                m_dst_image_ctx, ctx);
  request->send();
}

template <typename I>
void DeepCopyRequest<I>::handle_copy_metadata(int r) {
  ldout(m_cct, 20) << "r=" << r << dendl;

  if (r < 0) {
    lderr(m_cct) << "failed to copy metadata: " << cpp_strerror(r) << dendl;
    finish(r);
    return;
  }

  finish(0);
}

template <typename I>
int DeepCopyRequest<I>::validate_copy_points() {
  std::shared_lock image_locker{m_src_image_ctx->image_lock};

  if (m_src_snap_id_start != 0 &&
      m_src_image_ctx->snap_info.find(m_src_snap_id_start) ==
      m_src_image_ctx->snap_info.end()) {
    lderr(m_cct) << "invalid start snap_id " << m_src_snap_id_start << dendl;
    return -EINVAL;
  }

  if (m_src_snap_id_end != CEPH_NOSNAP &&
      m_src_image_ctx->snap_info.find(m_src_snap_id_end) ==
      m_src_image_ctx->snap_info.end()) {
    lderr(m_cct) << "invalid end snap_id " << m_src_snap_id_end << dendl;
    return -EINVAL;
  }

  return 0;
}

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

  m_on_finish->complete(r);
  put();
}

} // namespace librbd

template class librbd::DeepCopyRequest<librbd::ImageCtx>;