1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include <boost/variant.hpp>
5 #include "cls/rbd/cls_rbd_types.h"
6 #include "common/Formatter.h"
11 void MirrorPeer::encode(bufferlist &bl) const {
12 ENCODE_START(1, 1, bl);
14 ::encode(cluster_name, bl);
15 ::encode(client_name, bl);
16 ::encode(pool_id, bl);
20 void MirrorPeer::decode(bufferlist::iterator &it) {
23 ::decode(cluster_name, it);
24 ::decode(client_name, it);
25 ::decode(pool_id, it);
29 void MirrorPeer::dump(Formatter *f) const {
30 f->dump_string("uuid", uuid);
31 f->dump_string("cluster_name", cluster_name);
32 f->dump_string("client_name", client_name);
33 f->dump_int("pool_id", pool_id);
36 void MirrorPeer::generate_test_instances(std::list<MirrorPeer*> &o) {
37 o.push_back(new MirrorPeer());
38 o.push_back(new MirrorPeer("uuid-123", "cluster name", "client name", 123));
41 bool MirrorPeer::operator==(const MirrorPeer &rhs) const {
42 return (uuid == rhs.uuid &&
43 cluster_name == rhs.cluster_name &&
44 client_name == rhs.client_name &&
45 pool_id == rhs.pool_id);
48 std::ostream& operator<<(std::ostream& os, const MirrorMode& mirror_mode) {
49 switch (mirror_mode) {
50 case MIRROR_MODE_DISABLED:
53 case MIRROR_MODE_IMAGE:
56 case MIRROR_MODE_POOL:
60 os << "unknown (" << static_cast<uint32_t>(mirror_mode) << ")";
66 std::ostream& operator<<(std::ostream& os, const MirrorPeer& peer) {
68 << "uuid=" << peer.uuid << ", "
69 << "cluster_name=" << peer.cluster_name << ", "
70 << "client_name=" << peer.client_name;
71 if (peer.pool_id != -1) {
72 os << ", pool_id=" << peer.pool_id;
78 void MirrorImage::encode(bufferlist &bl) const {
79 ENCODE_START(1, 1, bl);
80 ::encode(global_image_id, bl);
81 ::encode(static_cast<uint8_t>(state), bl);
85 void MirrorImage::decode(bufferlist::iterator &it) {
88 ::decode(global_image_id, it);
89 ::decode(int_state, it);
90 state = static_cast<MirrorImageState>(int_state);
94 void MirrorImage::dump(Formatter *f) const {
95 f->dump_string("global_image_id", global_image_id);
96 f->dump_int("state", state);
99 void MirrorImage::generate_test_instances(std::list<MirrorImage*> &o) {
100 o.push_back(new MirrorImage());
101 o.push_back(new MirrorImage("uuid-123", MIRROR_IMAGE_STATE_ENABLED));
102 o.push_back(new MirrorImage("uuid-abc", MIRROR_IMAGE_STATE_DISABLING));
105 bool MirrorImage::operator==(const MirrorImage &rhs) const {
106 return global_image_id == rhs.global_image_id && state == rhs.state;
109 bool MirrorImage::operator<(const MirrorImage &rhs) const {
110 return global_image_id < rhs.global_image_id ||
111 (global_image_id == rhs.global_image_id && state < rhs.state);
114 std::ostream& operator<<(std::ostream& os, const MirrorImageState& mirror_state) {
115 switch (mirror_state) {
116 case MIRROR_IMAGE_STATE_DISABLING:
119 case MIRROR_IMAGE_STATE_ENABLED:
123 os << "unknown (" << static_cast<uint32_t>(mirror_state) << ")";
129 std::ostream& operator<<(std::ostream& os, const MirrorImage& mirror_image) {
131 << "global_image_id=" << mirror_image.global_image_id << ", "
132 << "state=" << mirror_image.state << "]";
136 void MirrorImageStatus::encode(bufferlist &bl) const {
137 ENCODE_START(1, 1, bl);
139 ::encode(description, bl);
140 ::encode(last_update, bl);
145 void MirrorImageStatus::decode(bufferlist::iterator &it) {
148 ::decode(description, it);
149 ::decode(last_update, it);
154 void MirrorImageStatus::dump(Formatter *f) const {
155 f->dump_string("state", state_to_string());
156 f->dump_string("description", description);
157 f->dump_stream("last_update") << last_update;
160 std::string MirrorImageStatus::state_to_string() const {
161 std::stringstream ss;
162 ss << (up ? "up+" : "down+") << state;
166 void MirrorImageStatus::generate_test_instances(
167 std::list<MirrorImageStatus*> &o) {
168 o.push_back(new MirrorImageStatus());
169 o.push_back(new MirrorImageStatus(MIRROR_IMAGE_STATUS_STATE_REPLAYING));
170 o.push_back(new MirrorImageStatus(MIRROR_IMAGE_STATUS_STATE_ERROR, "error"));
173 bool MirrorImageStatus::operator==(const MirrorImageStatus &rhs) const {
174 return state == rhs.state && description == rhs.description && up == rhs.up;
177 std::ostream& operator<<(std::ostream& os, const MirrorImageStatusState& state) {
179 case MIRROR_IMAGE_STATUS_STATE_UNKNOWN:
182 case MIRROR_IMAGE_STATUS_STATE_ERROR:
185 case MIRROR_IMAGE_STATUS_STATE_SYNCING:
188 case MIRROR_IMAGE_STATUS_STATE_STARTING_REPLAY:
189 os << "starting_replay";
191 case MIRROR_IMAGE_STATUS_STATE_REPLAYING:
194 case MIRROR_IMAGE_STATUS_STATE_STOPPING_REPLAY:
195 os << "stopping_replay";
197 case MIRROR_IMAGE_STATUS_STATE_STOPPED:
201 os << "unknown (" << static_cast<uint32_t>(state) << ")";
207 std::ostream& operator<<(std::ostream& os, const MirrorImageStatus& status) {
209 << "state=" << status.state_to_string() << ", "
210 << "description=" << status.description << ", "
211 << "last_update=" << status.last_update << "]";
215 void GroupImageSpec::encode(bufferlist &bl) const {
216 ENCODE_START(1, 1, bl);
217 ::encode(image_id, bl);
218 ::encode(pool_id, bl);
222 void GroupImageSpec::decode(bufferlist::iterator &it) {
224 ::decode(image_id, it);
225 ::decode(pool_id, it);
229 void GroupImageSpec::dump(Formatter *f) const {
230 f->dump_string("image_id", image_id);
231 f->dump_int("pool_id", pool_id);
234 int GroupImageSpec::from_key(const std::string &image_key,
235 GroupImageSpec *spec) {
236 if (nullptr == spec) return -EINVAL;
237 int prefix_len = cls::rbd::RBD_GROUP_IMAGE_KEY_PREFIX.size();
238 std::string data_string = image_key.substr(prefix_len,
239 image_key.size() - prefix_len);
240 size_t p = data_string.find("_");
241 if (std::string::npos == p) {
244 data_string[p] = ' ';
246 istringstream iss(data_string);
249 iss >> std::hex >> pool_id >> image_id;
251 spec->image_id = image_id;
252 spec->pool_id = pool_id;
256 std::string GroupImageSpec::image_key() {
261 oss << RBD_GROUP_IMAGE_KEY_PREFIX << std::setw(16)
262 << std::setfill('0') << std::hex << pool_id << "_" << image_id;
267 void GroupImageStatus::encode(bufferlist &bl) const {
268 ENCODE_START(1, 1, bl);
274 void GroupImageStatus::decode(bufferlist::iterator &it) {
281 std::string GroupImageStatus::state_to_string() const {
282 std::stringstream ss;
283 if (state == GROUP_IMAGE_LINK_STATE_INCOMPLETE) {
286 if (state == GROUP_IMAGE_LINK_STATE_ATTACHED) {
292 void GroupImageStatus::dump(Formatter *f) const {
294 f->dump_string("state", state_to_string());
297 void GroupSpec::encode(bufferlist &bl) const {
298 ENCODE_START(1, 1, bl);
299 ::encode(pool_id, bl);
300 ::encode(group_id, bl);
304 void GroupSpec::decode(bufferlist::iterator &it) {
306 ::decode(pool_id, it);
307 ::decode(group_id, it);
311 void GroupSpec::dump(Formatter *f) const {
312 f->dump_string("group_id", group_id);
313 f->dump_int("pool_id", pool_id);
316 bool GroupSpec::is_valid() const {
317 return (!group_id.empty()) && (pool_id != -1);
320 void GroupSnapshotNamespace::encode(bufferlist& bl) const {
321 ::encode(group_pool, bl);
322 ::encode(group_id, bl);
323 ::encode(snapshot_id, bl);
326 void GroupSnapshotNamespace::decode(bufferlist::iterator& it) {
327 ::decode(group_pool, it);
328 ::decode(group_id, it);
329 ::decode(snapshot_id, it);
332 void GroupSnapshotNamespace::dump(Formatter *f) const {
333 f->dump_int("group_pool", group_pool);
334 f->dump_string("group_id", group_id);
335 f->dump_int("snapshot_id", snapshot_id);
338 class EncodeSnapshotNamespaceVisitor : public boost::static_visitor<void> {
340 explicit EncodeSnapshotNamespaceVisitor(bufferlist &bl) : m_bl(bl) {
343 template <typename T>
344 inline void operator()(const T& t) const {
345 ::encode(static_cast<uint32_t>(T::SNAPSHOT_NAMESPACE_TYPE), m_bl);
353 class DecodeSnapshotNamespaceVisitor : public boost::static_visitor<void> {
355 DecodeSnapshotNamespaceVisitor(bufferlist::iterator &iter)
359 template <typename T>
360 inline void operator()(T& t) const {
364 bufferlist::iterator &m_iter;
367 class DumpSnapshotNamespaceVisitor : public boost::static_visitor<void> {
369 explicit DumpSnapshotNamespaceVisitor(Formatter *formatter, const std::string &key)
370 : m_formatter(formatter), m_key(key) {}
372 template <typename T>
373 inline void operator()(const T& t) const {
374 auto type = T::SNAPSHOT_NAMESPACE_TYPE;
375 m_formatter->dump_string(m_key.c_str(), stringify(type));
379 ceph::Formatter *m_formatter;
383 class GetTypeVisitor : public boost::static_visitor<SnapshotNamespaceType> {
385 template <typename T>
386 inline SnapshotNamespaceType operator()(const T&) const {
387 return static_cast<SnapshotNamespaceType>(T::SNAPSHOT_NAMESPACE_TYPE);
392 SnapshotNamespaceType SnapshotNamespaceOnDisk::get_namespace_type() const {
393 return static_cast<SnapshotNamespaceType>(boost::apply_visitor(GetTypeVisitor(),
394 snapshot_namespace));
397 void SnapshotNamespaceOnDisk::encode(bufferlist& bl) const {
398 ENCODE_START(1, 1, bl);
399 boost::apply_visitor(EncodeSnapshotNamespaceVisitor(bl), snapshot_namespace);
403 void SnapshotNamespaceOnDisk::decode(bufferlist::iterator &p)
407 ::decode(snap_type, p);
409 case cls::rbd::SNAPSHOT_NAMESPACE_TYPE_USER:
410 snapshot_namespace = UserSnapshotNamespace();
412 case cls::rbd::SNAPSHOT_NAMESPACE_TYPE_GROUP:
413 snapshot_namespace = GroupSnapshotNamespace();
416 snapshot_namespace = UnknownSnapshotNamespace();
419 boost::apply_visitor(DecodeSnapshotNamespaceVisitor(p), snapshot_namespace);
423 void SnapshotNamespaceOnDisk::dump(Formatter *f) const {
424 boost::apply_visitor(DumpSnapshotNamespaceVisitor(f, "snapshot_namespace_type"), snapshot_namespace);
427 void SnapshotNamespaceOnDisk::generate_test_instances(std::list<SnapshotNamespaceOnDisk *> &o) {
428 o.push_back(new SnapshotNamespaceOnDisk(UserSnapshotNamespace()));
429 o.push_back(new SnapshotNamespaceOnDisk(GroupSnapshotNamespace(0, "10152ae8944a", 1)));
430 o.push_back(new SnapshotNamespaceOnDisk(GroupSnapshotNamespace(5, "1018643c9869", 3)));
433 std::ostream& operator<<(std::ostream& os, const UserSnapshotNamespace& ns) {
438 std::ostream& operator<<(std::ostream& os, const GroupSnapshotNamespace& ns) {
440 << " group_pool=" << ns.group_pool
441 << " group_id=" << ns.group_id
442 << " snapshot_id=" << ns.snapshot_id << "]";
446 std::ostream& operator<<(std::ostream& os, const UnknownSnapshotNamespace& ns) {
451 void TrashImageSpec::encode(bufferlist& bl) const {
452 ENCODE_START(1, 1, bl);
453 ::encode(source, bl);
455 ::encode(deletion_time, bl);
456 ::encode(deferment_end_time, bl);
460 void TrashImageSpec::decode(bufferlist::iterator &it) {
462 ::decode(source, it);
464 ::decode(deletion_time, it);
465 ::decode(deferment_end_time, it);
469 void TrashImageSpec::dump(Formatter *f) const {
471 case TRASH_IMAGE_SOURCE_USER:
472 f->dump_string("source", "user");
474 case TRASH_IMAGE_SOURCE_MIRRORING:
475 f->dump_string("source", "rbd_mirror");
477 f->dump_string("name", name);
478 f->dump_unsigned("deletion_time", deletion_time);
479 f->dump_unsigned("deferment_end_time", deferment_end_time);