]> git.proxmox.com Git - ceph.git/blob - ceph/src/librbd/api/PoolMetadata.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / librbd / api / PoolMetadata.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/api/PoolMetadata.h"
5 #include "cls/rbd/cls_rbd_client.h"
6 #include "common/dout.h"
7 #include "common/errno.h"
8 #include "librbd/Utils.h"
9 #include "librbd/api/Config.h"
10
11 #define dout_subsys ceph_subsys_rbd
12 #undef dout_prefix
13 #define dout_prefix *_dout << "librbd::PoolMetadata: " << __func__ << ": "
14
15 namespace librbd {
16 namespace api {
17
18 template <typename I>
19 int PoolMetadata<I>::get(librados::IoCtx& io_ctx,
20 const std::string &key, std::string *value) {
21 CephContext *cct = (CephContext *)io_ctx.cct();
22
23 int r = cls_client::metadata_get(&io_ctx, RBD_INFO, key, value);
24 if (r < 0 && r != -ENOENT) {
25 lderr(cct) << "failed reading metadata " << key << ": " << cpp_strerror(r)
26 << dendl;
27 }
28
29 return r;
30 }
31
32 template <typename I>
33 int PoolMetadata<I>::set(librados::IoCtx& io_ctx, const std::string &key,
34 const std::string &value) {
35 CephContext *cct = (CephContext *)io_ctx.cct();
36
37 std::string config_key;
38 if (util::is_metadata_config_override(key, &config_key)) {
39 if (!librbd::api::Config<I>::is_option_name(io_ctx, config_key)) {
40 lderr(cct) << "validation for " << key
41 << " failed: not allowed pool level override" << dendl;
42 return -EINVAL;
43 }
44 int r = ConfigProxy{false}.set_val(config_key.c_str(), value);
45 if (r < 0) {
46 lderr(cct) << "validation for " << key << " failed: " << cpp_strerror(r)
47 << dendl;
48 return -EINVAL;
49 }
50 }
51
52 ceph::bufferlist bl;
53 bl.append(value);
54
55 int r = cls_client::metadata_set(&io_ctx, RBD_INFO, {{key, bl}});
56 if (r < 0) {
57 lderr(cct) << "failed setting metadata " << key << ": " << cpp_strerror(r)
58 << dendl;
59 return r;
60 }
61
62 return 0;
63 }
64
65 template <typename I>
66 int PoolMetadata<I>::remove(librados::IoCtx& io_ctx, const std::string &key) {
67 CephContext *cct = (CephContext *)io_ctx.cct();
68
69 std::string value;
70 int r = cls_client::metadata_get(&io_ctx, RBD_INFO, key, &value);
71 if (r < 0) {
72 if (r == -ENOENT) {
73 ldout(cct, 1) << "metadata " << key << " does not exist" << dendl;
74 } else {
75 lderr(cct) << "failed reading metadata " << key << ": " << cpp_strerror(r)
76 << dendl;
77 }
78 return r;
79 }
80
81 r = cls_client::metadata_remove(&io_ctx, RBD_INFO, key);
82 if (r < 0) {
83 lderr(cct) << "failed removing metadata " << key << ": " << cpp_strerror(r)
84 << dendl;
85 return r;
86 }
87
88 return 0;
89 }
90
91 template <typename I>
92 int PoolMetadata<I>::list(librados::IoCtx& io_ctx, const std::string &start,
93 uint64_t max,
94 std::map<std::string, ceph::bufferlist> *pairs) {
95 CephContext *cct = (CephContext *)io_ctx.cct();
96
97 int r = cls_client::metadata_list(&io_ctx, RBD_INFO, start, max, pairs);
98 if (r == -ENOENT) {
99 r = 0;
100 } else if (r < 0) {
101 lderr(cct) << "failed listing metadata: " << cpp_strerror(r)
102 << dendl;
103 return r;
104 }
105
106 return 0;
107 }
108
109 } // namespace api
110 } // namespace librbd
111
112 template class librbd::api::PoolMetadata<librbd::ImageCtx>;