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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "CreateImageRequest.h"
#include "CloseImageRequest.h"
#include "OpenImageRequest.h"
#include "common/debug.h"
#include "common/errno.h"
#include "cls/rbd/cls_rbd_client.h"
#include "librbd/ImageCtx.h"
#include "librbd/ImageState.h"
#include "librbd/internal.h"
#include "librbd/Utils.h"
#include "librbd/asio/ContextWQ.h"
#include "librbd/image/CreateRequest.h"
#include "librbd/image/CloneRequest.h"
#include "tools/rbd_mirror/PoolMetaCache.h"
#include "tools/rbd_mirror/Types.h"
#include "tools/rbd_mirror/Threads.h"
#include "tools/rbd_mirror/image_replayer/Utils.h"
#include "tools/rbd_mirror/image_sync/Utils.h"
#include <boost/algorithm/string/predicate.hpp>
#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_rbd_mirror
#undef dout_prefix
#define dout_prefix *_dout << "rbd::mirror::image_replayer::CreateImageRequest: " \
<< this << " " << __func__ << ": "
using librbd::util::create_async_context_callback;
using librbd::util::create_context_callback;
using librbd::util::create_rados_callback;
namespace rbd {
namespace mirror {
namespace image_replayer {
template <typename I>
CreateImageRequest<I>::CreateImageRequest(
Threads<I>* threads,
librados::IoCtx &local_io_ctx,
const std::string &global_image_id,
const std::string &remote_mirror_uuid,
const std::string &local_image_name,
const std::string &local_image_id,
I *remote_image_ctx,
PoolMetaCache* pool_meta_cache,
cls::rbd::MirrorImageMode mirror_image_mode,
Context *on_finish)
: m_threads(threads), m_local_io_ctx(local_io_ctx),
m_global_image_id(global_image_id),
m_remote_mirror_uuid(remote_mirror_uuid),
m_local_image_name(local_image_name), m_local_image_id(local_image_id),
m_remote_image_ctx(remote_image_ctx),
m_pool_meta_cache(pool_meta_cache),
m_mirror_image_mode(mirror_image_mode), m_on_finish(on_finish) {
}
template <typename I>
void CreateImageRequest<I>::send() {
int r = validate_parent();
if (r < 0) {
error(r);
return;
}
if (m_remote_parent_spec.pool_id == -1) {
create_image();
} else {
get_parent_global_image_id();
}
}
template <typename I>
void CreateImageRequest<I>::create_image() {
dout(10) << dendl;
using klass = CreateImageRequest<I>;
Context *ctx = create_context_callback<
klass, &klass::handle_create_image>(this);
std::shared_lock image_locker{m_remote_image_ctx->image_lock};
auto& config{
reinterpret_cast<CephContext*>(m_local_io_ctx.cct())->_conf};
librbd::ImageOptions image_options;
populate_image_options(&image_options);
auto req = librbd::image::CreateRequest<I>::create(
config, m_local_io_ctx, m_local_image_name, m_local_image_id,
m_remote_image_ctx->size, image_options, 0U, m_mirror_image_mode,
m_global_image_id, m_remote_mirror_uuid, m_remote_image_ctx->op_work_queue,
ctx);
req->send();
}
template <typename I>
void CreateImageRequest<I>::handle_create_image(int r) {
dout(10) << "r=" << r << dendl;
if (r == -EBADF) {
dout(5) << "image id " << m_local_image_id << " already in-use" << dendl;
finish(r);
return;
} else if (r < 0) {
derr << "failed to create local image: " << cpp_strerror(r) << dendl;
finish(r);
return;
}
finish(0);
}
template <typename I>
void CreateImageRequest<I>::get_parent_global_image_id() {
dout(10) << dendl;
librados::ObjectReadOperation op;
librbd::cls_client::mirror_image_get_start(&op,
m_remote_parent_spec.image_id);
librados::AioCompletion *aio_comp = create_rados_callback<
CreateImageRequest<I>,
&CreateImageRequest<I>::handle_get_parent_global_image_id>(this);
m_out_bl.clear();
int r = m_remote_parent_io_ctx.aio_operate(RBD_MIRRORING, aio_comp, &op,
&m_out_bl);
ceph_assert(r == 0);
aio_comp->release();
}
template <typename I>
void CreateImageRequest<I>::handle_get_parent_global_image_id(int r) {
dout(10) << "r=" << r << dendl;
if (r == 0) {
cls::rbd::MirrorImage mirror_image;
auto iter = m_out_bl.cbegin();
r = librbd::cls_client::mirror_image_get_finish(&iter, &mirror_image);
if (r == 0) {
m_parent_global_image_id = mirror_image.global_image_id;
dout(15) << "parent_global_image_id=" << m_parent_global_image_id
<< dendl;
}
}
if (r == -ENOENT) {
dout(10) << "parent image " << m_remote_parent_spec.image_id
<< " not mirrored" << dendl;
finish(r);
return;
} else if (r < 0) {
derr << "failed to retrieve global image id for parent image "
<< m_remote_parent_spec.image_id << ": " << cpp_strerror(r) << dendl;
finish(r);
return;
}
get_local_parent_image_id();
}
template <typename I>
void CreateImageRequest<I>::get_local_parent_image_id() {
dout(10) << dendl;
librados::ObjectReadOperation op;
librbd::cls_client::mirror_image_get_image_id_start(
&op, m_parent_global_image_id);
librados::AioCompletion *aio_comp = create_rados_callback<
CreateImageRequest<I>,
&CreateImageRequest<I>::handle_get_local_parent_image_id>(this);
m_out_bl.clear();
int r = m_local_parent_io_ctx.aio_operate(RBD_MIRRORING, aio_comp, &op,
&m_out_bl);
ceph_assert(r == 0);
aio_comp->release();
}
template <typename I>
void CreateImageRequest<I>::handle_get_local_parent_image_id(int r) {
dout(10) << "r=" << r << dendl;
if (r == 0) {
auto iter = m_out_bl.cbegin();
r = librbd::cls_client::mirror_image_get_image_id_finish(
&iter, &m_local_parent_spec.image_id);
}
if (r == -ENOENT) {
dout(10) << "parent image " << m_parent_global_image_id << " not "
<< "registered locally" << dendl;
finish(r);
return;
} else if (r < 0) {
derr << "failed to retrieve local image id for parent image "
<< m_parent_global_image_id << ": " << cpp_strerror(r) << dendl;
finish(r);
return;
}
open_remote_parent_image();
}
template <typename I>
void CreateImageRequest<I>::open_remote_parent_image() {
dout(10) << dendl;
Context *ctx = create_context_callback<
CreateImageRequest<I>,
&CreateImageRequest<I>::handle_open_remote_parent_image>(this);
OpenImageRequest<I> *request = OpenImageRequest<I>::create(
m_remote_parent_io_ctx, &m_remote_parent_image_ctx,
m_remote_parent_spec.image_id, true, ctx);
request->send();
}
template <typename I>
void CreateImageRequest<I>::handle_open_remote_parent_image(int r) {
dout(10) << "r=" << r << dendl;
if (r < 0) {
derr << "failed to open remote parent image " << m_parent_pool_name << "/"
<< m_remote_parent_spec.image_id << dendl;
finish(r);
return;
}
clone_image();
}
template <typename I>
void CreateImageRequest<I>::clone_image() {
dout(10) << dendl;
LocalPoolMeta local_parent_pool_meta;
int r = m_pool_meta_cache->get_local_pool_meta(
m_local_parent_io_ctx.get_id(), &local_parent_pool_meta);
if (r < 0) {
derr << "failed to retrieve local parent mirror uuid for pool "
<< m_local_parent_io_ctx.get_id() << dendl;
m_ret_val = r;
close_remote_parent_image();
return;
}
// ensure no image sync snapshots for the local cluster exist in the
// remote image
bool found_parent_snap = false;
bool found_image_sync_snap = false;
std::string snap_name;
cls::rbd::SnapshotNamespace snap_namespace;
{
auto snap_prefix = image_sync::util::get_snapshot_name_prefix(
local_parent_pool_meta.mirror_uuid);
std::shared_lock remote_image_locker(m_remote_parent_image_ctx->image_lock);
for (auto snap_info : m_remote_parent_image_ctx->snap_info) {
if (snap_info.first == m_remote_parent_spec.snap_id) {
found_parent_snap = true;
snap_name = snap_info.second.name;
snap_namespace = snap_info.second.snap_namespace;
} else if (boost::starts_with(snap_info.second.name, snap_prefix)) {
found_image_sync_snap = true;
}
}
}
if (!found_parent_snap) {
dout(15) << "remote parent image snapshot not found" << dendl;
m_ret_val = -ENOENT;
close_remote_parent_image();
return;
} else if (found_image_sync_snap) {
dout(15) << "parent image not synced to local cluster" << dendl;
m_ret_val = -ENOENT;
close_remote_parent_image();
return;
}
librbd::ImageOptions opts;
populate_image_options(&opts);
auto& config{
reinterpret_cast<CephContext*>(m_local_io_ctx.cct())->_conf};
using klass = CreateImageRequest<I>;
Context *ctx = create_context_callback<
klass, &klass::handle_clone_image>(this);
librbd::image::CloneRequest<I> *req = librbd::image::CloneRequest<I>::create(
config, m_local_parent_io_ctx, m_local_parent_spec.image_id, snap_name,
snap_namespace, CEPH_NOSNAP, m_local_io_ctx, m_local_image_name,
m_local_image_id, opts, m_mirror_image_mode, m_global_image_id,
m_remote_mirror_uuid, m_remote_image_ctx->op_work_queue, ctx);
req->send();
}
template <typename I>
void CreateImageRequest<I>::handle_clone_image(int r) {
dout(10) << "r=" << r << dendl;
if (r == -EBADF) {
dout(5) << "image id " << m_local_image_id << " already in-use" << dendl;
m_ret_val = r;
} else if (r < 0) {
derr << "failed to clone image " << m_parent_pool_name << "/"
<< m_remote_parent_spec.image_id << " to "
<< m_local_image_name << dendl;
m_ret_val = r;
}
close_remote_parent_image();
}
template <typename I>
void CreateImageRequest<I>::close_remote_parent_image() {
dout(10) << dendl;
Context *ctx = create_context_callback<
CreateImageRequest<I>,
&CreateImageRequest<I>::handle_close_remote_parent_image>(this);
CloseImageRequest<I> *request = CloseImageRequest<I>::create(
&m_remote_parent_image_ctx, ctx);
request->send();
}
template <typename I>
void CreateImageRequest<I>::handle_close_remote_parent_image(int r) {
dout(10) << "r=" << r << dendl;
if (r < 0) {
derr << "error encountered closing remote parent image: "
<< cpp_strerror(r) << dendl;
}
finish(m_ret_val);
}
template <typename I>
void CreateImageRequest<I>::error(int r) {
dout(10) << "r=" << r << dendl;
m_threads->work_queue->queue(create_context_callback<
CreateImageRequest<I>, &CreateImageRequest<I>::finish>(this), r);
}
template <typename I>
void CreateImageRequest<I>::finish(int r) {
dout(10) << "r=" << r << dendl;
m_on_finish->complete(r);
delete this;
}
template <typename I>
int CreateImageRequest<I>::validate_parent() {
std::shared_lock owner_locker{m_remote_image_ctx->owner_lock};
std::shared_lock image_locker{m_remote_image_ctx->image_lock};
m_remote_parent_spec = m_remote_image_ctx->parent_md.spec;
// scan all remote snapshots for a linked parent
for (auto &snap_info_pair : m_remote_image_ctx->snap_info) {
auto &parent_spec = snap_info_pair.second.parent.spec;
if (parent_spec.pool_id == -1) {
continue;
} else if (m_remote_parent_spec.pool_id == -1) {
m_remote_parent_spec = parent_spec;
continue;
}
if (m_remote_parent_spec != parent_spec) {
derr << "remote image parent spec mismatch" << dendl;
return -EINVAL;
}
}
if (m_remote_parent_spec.pool_id == -1) {
return 0;
}
// map remote parent pool to local parent pool
int r = librbd::util::create_ioctx(
m_remote_image_ctx->md_ctx, "remote parent pool",
m_remote_parent_spec.pool_id, m_remote_parent_spec.pool_namespace,
&m_remote_parent_io_ctx);
if (r < 0) {
derr << "failed to open remote parent pool " << m_remote_parent_spec.pool_id
<< ": " << cpp_strerror(r) << dendl;
return r;
}
m_parent_pool_name = m_remote_parent_io_ctx.get_pool_name();
librados::Rados local_rados(m_local_io_ctx);
r = local_rados.ioctx_create(m_parent_pool_name.c_str(),
m_local_parent_io_ctx);
if (r < 0) {
derr << "failed to open local parent pool " << m_parent_pool_name << ": "
<< cpp_strerror(r) << dendl;
return r;
}
m_local_parent_io_ctx.set_namespace(m_remote_parent_io_ctx.get_namespace());
return 0;
}
template <typename I>
void CreateImageRequest<I>::populate_image_options(
librbd::ImageOptions* image_options) {
image_options->set(RBD_IMAGE_OPTION_FEATURES,
m_remote_image_ctx->features);
image_options->set(RBD_IMAGE_OPTION_ORDER, m_remote_image_ctx->order);
image_options->set(RBD_IMAGE_OPTION_STRIPE_UNIT,
m_remote_image_ctx->stripe_unit);
image_options->set(RBD_IMAGE_OPTION_STRIPE_COUNT,
m_remote_image_ctx->stripe_count);
// Determine the data pool for the local image as follows:
// 1. If the local pool has a default data pool, use it.
// 2. If the remote image has a data pool different from its metadata pool and
// a pool with the same name exists locally, use it.
// 3. Don't set the data pool explicitly.
std::string data_pool;
librados::Rados local_rados(m_local_io_ctx);
auto default_data_pool = g_ceph_context->_conf.get_val<std::string>("rbd_default_data_pool");
auto remote_md_pool = m_remote_image_ctx->md_ctx.get_pool_name();
auto remote_data_pool = m_remote_image_ctx->data_ctx.get_pool_name();
if (default_data_pool != "") {
data_pool = default_data_pool;
} else if (remote_data_pool != remote_md_pool) {
if (local_rados.pool_lookup(remote_data_pool.c_str()) >= 0) {
data_pool = remote_data_pool;
}
}
if (data_pool != "") {
image_options->set(RBD_IMAGE_OPTION_DATA_POOL, data_pool);
}
if (m_remote_parent_spec.pool_id != -1) {
uint64_t clone_format = 1;
if (m_remote_image_ctx->test_op_features(
RBD_OPERATION_FEATURE_CLONE_CHILD)) {
clone_format = 2;
}
image_options->set(RBD_IMAGE_OPTION_CLONE_FORMAT, clone_format);
}
}
} // namespace image_replayer
} // namespace mirror
} // namespace rbd
template class rbd::mirror::image_replayer::CreateImageRequest<librbd::ImageCtx>;
|