]> git.proxmox.com Git - ceph.git/blob - ceph/src/librbd/image/GetMetadataRequest.cc
import 15.2.0 Octopus source
[ceph.git] / ceph / src / librbd / image / GetMetadataRequest.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include "librbd/image/GetMetadataRequest.h"
5 #include "cls/rbd/cls_rbd_client.h"
6 #include "common/dout.h"
7 #include "common/errno.h"
8 #include "include/ceph_assert.h"
9 #include "librbd/ImageCtx.h"
10 #include "librbd/Utils.h"
11 #include <boost/algorithm/string/predicate.hpp>
12
13 #define dout_subsys ceph_subsys_rbd
14 #undef dout_prefix
15 #define dout_prefix *_dout << "librbd::image::GetMetadataRequest: " \
16 << this << " " << __func__ << ": "
17
18 #define MAX_KEYS 64U
19
20 namespace librbd {
21 namespace image {
22 namespace {
23
24 static const std::string INTERNAL_KEY_PREFIX{".rbd"};
25
26 } // anonymous namespace
27
28 using util::create_rados_callback;
29
30 template <typename I>
31 GetMetadataRequest<I>::GetMetadataRequest(
32 IoCtx &io_ctx, const std::string &oid, bool filter_internal,
33 const std::string& filter_key_prefix, const std::string& last_key,
34 uint32_t max_results, KeyValues* key_values, Context *on_finish)
35 : m_io_ctx(io_ctx), m_oid(oid), m_filter_internal(filter_internal),
36 m_filter_key_prefix(filter_key_prefix), m_last_key(last_key),
37 m_max_results(max_results), m_key_values(key_values),
38 m_on_finish(on_finish),
39 m_cct(reinterpret_cast<CephContext*>(m_io_ctx.cct())) {
40 }
41
42 template <typename I>
43 void GetMetadataRequest<I>::send() {
44 metadata_list();
45 }
46
47 template <typename I>
48 void GetMetadataRequest<I>::metadata_list() {
49 ldout(m_cct, 15) << "start_key=" << m_last_key << dendl;
50
51 m_expected_results = MAX_KEYS;
52 if (m_max_results > 0) {
53 m_expected_results = std::min<uint32_t>(
54 m_expected_results, m_max_results - m_key_values->size());
55 }
56
57 librados::ObjectReadOperation op;
58 cls_client::metadata_list_start(&op, m_last_key, m_expected_results);
59
60 auto aio_comp = create_rados_callback<
61 GetMetadataRequest<I>, &GetMetadataRequest<I>::handle_metadata_list>(this);
62 m_out_bl.clear();
63 m_io_ctx.aio_operate(m_oid, aio_comp, &op, &m_out_bl);
64 aio_comp->release();
65 }
66
67 template <typename I>
68 void GetMetadataRequest<I>::handle_metadata_list(int r) {
69 ldout(m_cct, 15) << "r=" << r << dendl;
70
71 KeyValues metadata;
72 if (r == 0) {
73 auto it = m_out_bl.cbegin();
74 r = cls_client::metadata_list_finish(&it, &metadata);
75 }
76
77 if (r == -ENOENT || r == -EOPNOTSUPP) {
78 finish(0);
79 return;
80 } else if (r < 0) {
81 lderr(m_cct) << "failed to retrieve image metadata: " << cpp_strerror(r)
82 << dendl;
83 finish(r);
84 return;
85 }
86
87 for (auto it = metadata.begin(); it != metadata.end(); ++it) {
88 if (m_filter_internal &&
89 boost::starts_with(it->first, INTERNAL_KEY_PREFIX)) {
90 continue;
91 } else if (!m_filter_key_prefix.empty() &&
92 !boost::starts_with(it->first, m_filter_key_prefix)) {
93 continue;
94 }
95 m_key_values->insert({it->first, std::move(it->second)});
96 }
97 if (!metadata.empty()) {
98 m_last_key = metadata.rbegin()->first;
99 }
100
101 if (metadata.size() == m_expected_results &&
102 (m_max_results == 0 || m_key_values->size() < m_max_results)) {
103 metadata_list();
104 return;
105 }
106
107 finish(0);
108 }
109
110 template <typename I>
111 void GetMetadataRequest<I>::finish(int r) {
112 ldout(m_cct, 15) << "r=" << r << dendl;
113
114 m_on_finish->complete(r);
115 delete this;
116 }
117
118 } // namespace image
119 } // namespace librbd
120
121 template class librbd::image::GetMetadataRequest<librbd::ImageCtx>;