summaryrefslogtreecommitdiffstats
path: root/src/librbd/deep_copy/MetadataCopyRequest.cc
blob: cbce9a19eacefe0831118874d96d8277ddc9a549 (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "MetadataCopyRequest.h"
#include "common/dout.h"
#include "common/errno.h"
#include "cls/rbd/cls_rbd_client.h"
#include "librbd/Utils.h"

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

namespace librbd {
namespace deep_copy {

namespace {

const uint64_t MAX_METADATA_ITEMS = 128;

} // anonymous namespace

using librbd::util::create_rados_callback;

template <typename I>
MetadataCopyRequest<I>::MetadataCopyRequest(I *src_image_ctx, I *dst_image_ctx,
                                            Context *on_finish)
  : m_src_image_ctx(src_image_ctx), m_dst_image_ctx(dst_image_ctx),
    m_on_finish(on_finish), m_cct(dst_image_ctx->cct) {
}

template <typename I>
void MetadataCopyRequest<I>::send() {
  list_src_metadata();
}

template <typename I>
void MetadataCopyRequest<I>::list_src_metadata() {
  ldout(m_cct, 20) << "start_key=" << m_last_metadata_key << dendl;

  librados::ObjectReadOperation op;
  librbd::cls_client::metadata_list_start(&op, m_last_metadata_key, MAX_METADATA_ITEMS);

  librados::AioCompletion *aio_comp = create_rados_callback<
    MetadataCopyRequest<I>,
    &MetadataCopyRequest<I>::handle_list_src_metadata>(this);
  m_out_bl.clear();
  m_src_image_ctx->md_ctx.aio_operate(m_src_image_ctx->header_oid,
                                      aio_comp, &op, &m_out_bl);
  aio_comp->release();
}

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

  Metadata metadata;
  if (r == 0) {
    auto it = m_out_bl.cbegin();
    r = librbd::cls_client::metadata_list_finish(&it, &metadata);
  }

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

  if (metadata.empty()) {
    finish(0);
    return;
  }

  m_last_metadata_key = metadata.rbegin()->first;
  m_more_metadata = (metadata.size() >= MAX_METADATA_ITEMS);
  set_dst_metadata(metadata);
}

template <typename I>
void MetadataCopyRequest<I>::set_dst_metadata(const Metadata& metadata) {
  ldout(m_cct, 20) << "count=" << metadata.size() << dendl;

  librados::ObjectWriteOperation op;
  librbd::cls_client::metadata_set(&op, metadata);

  librados::AioCompletion *aio_comp = create_rados_callback<
    MetadataCopyRequest<I>,
    &MetadataCopyRequest<I>::handle_set_dst_metadata>(this);
  m_dst_image_ctx->md_ctx.aio_operate(m_dst_image_ctx->header_oid, aio_comp,
                                      &op);
  aio_comp->release();
}

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

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

  if (m_more_metadata) {
    list_src_metadata();
    return;
  }

  finish(0);
}

template <typename I>
void MetadataCopyRequest<I>::finish(int r) {
  ldout(m_cct, 20) << "r=" << r << dendl;
  m_on_finish->complete(r);
  delete this;
}

} // namespace deep_copy
} // namespace librbd

template class librbd::deep_copy::MetadataCopyRequest<librbd::ImageCtx>;