Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / cls / rbd / cls_rbd_types.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #ifndef CEPH_CLS_RBD_TYPES_H
5 #define CEPH_CLS_RBD_TYPES_H
6
7 #include <boost/variant.hpp>
8 #include "include/int_types.h"
9 #include "include/buffer.h"
10 #include "include/encoding.h"
11 #include "include/stringify.h"
12 #include "include/utime.h"
13 #include <iosfwd>
14 #include <string>
15
16 #define RBD_GROUP_REF "rbd_group_ref"
17
18 namespace ceph { class Formatter; }
19
20 namespace cls {
21 namespace rbd {
22
23 static const uint32_t MAX_OBJECT_MAP_OBJECT_COUNT = 256000000;
24 static const string RBD_GROUP_IMAGE_KEY_PREFIX = "image_";
25
26 enum MirrorMode {
27   MIRROR_MODE_DISABLED = 0,
28   MIRROR_MODE_IMAGE    = 1,
29   MIRROR_MODE_POOL     = 2
30 };
31
32 enum GroupImageLinkState {
33   GROUP_IMAGE_LINK_STATE_ATTACHED,
34   GROUP_IMAGE_LINK_STATE_INCOMPLETE
35 };
36
37 inline void encode(const GroupImageLinkState &state, bufferlist& bl,
38                    uint64_t features=0)
39 {
40   ::encode(static_cast<uint8_t>(state), bl);
41 }
42
43 inline void decode(GroupImageLinkState &state, bufferlist::iterator& it)
44 {
45   uint8_t int_state;
46   ::decode(int_state, it);
47   state = static_cast<GroupImageLinkState>(int_state);
48 }
49
50 struct MirrorPeer {
51   MirrorPeer() {
52   }
53   MirrorPeer(const std::string &uuid, const std::string &cluster_name,
54              const std::string &client_name, int64_t pool_id)
55     : uuid(uuid), cluster_name(cluster_name), client_name(client_name),
56       pool_id(pool_id) {
57   }
58
59   std::string uuid;
60   std::string cluster_name;
61   std::string client_name;
62   int64_t pool_id = -1;
63
64   inline bool is_valid() const {
65     return (!uuid.empty() && !cluster_name.empty() && !client_name.empty());
66   }
67
68   void encode(bufferlist &bl) const;
69   void decode(bufferlist::iterator &it);
70   void dump(Formatter *f) const;
71
72   static void generate_test_instances(std::list<MirrorPeer*> &o);
73
74   bool operator==(const MirrorPeer &rhs) const;
75 };
76
77 std::ostream& operator<<(std::ostream& os, const MirrorMode& mirror_mode);
78 std::ostream& operator<<(std::ostream& os, const MirrorPeer& peer);
79
80 WRITE_CLASS_ENCODER(MirrorPeer);
81
82 enum MirrorImageState {
83   MIRROR_IMAGE_STATE_DISABLING = 0,
84   MIRROR_IMAGE_STATE_ENABLED   = 1,
85   MIRROR_IMAGE_STATE_DISABLED  = 2,
86 };
87
88 struct MirrorImage {
89   MirrorImage() {}
90   MirrorImage(const std::string &global_image_id, MirrorImageState state)
91     : global_image_id(global_image_id), state(state) {}
92
93   std::string global_image_id;
94   MirrorImageState state = MIRROR_IMAGE_STATE_DISABLING;
95
96   void encode(bufferlist &bl) const;
97   void decode(bufferlist::iterator &it);
98   void dump(Formatter *f) const;
99
100   static void generate_test_instances(std::list<MirrorImage*> &o);
101
102   bool operator==(const MirrorImage &rhs) const;
103   bool operator<(const MirrorImage &rhs) const;
104 };
105
106 std::ostream& operator<<(std::ostream& os, const MirrorImageState& mirror_state);
107 std::ostream& operator<<(std::ostream& os, const MirrorImage& mirror_image);
108
109 WRITE_CLASS_ENCODER(MirrorImage);
110
111 enum MirrorImageStatusState {
112   MIRROR_IMAGE_STATUS_STATE_UNKNOWN         = 0,
113   MIRROR_IMAGE_STATUS_STATE_ERROR           = 1,
114   MIRROR_IMAGE_STATUS_STATE_SYNCING         = 2,
115   MIRROR_IMAGE_STATUS_STATE_STARTING_REPLAY = 3,
116   MIRROR_IMAGE_STATUS_STATE_REPLAYING       = 4,
117   MIRROR_IMAGE_STATUS_STATE_STOPPING_REPLAY = 5,
118   MIRROR_IMAGE_STATUS_STATE_STOPPED         = 6,
119 };
120
121 inline void encode(const MirrorImageStatusState &state, bufferlist& bl,
122                    uint64_t features=0)
123 {
124   ::encode(static_cast<uint8_t>(state), bl);
125 }
126
127 inline void decode(MirrorImageStatusState &state, bufferlist::iterator& it)
128 {
129   uint8_t int_state;
130   ::decode(int_state, it);
131   state = static_cast<MirrorImageStatusState>(int_state);
132 }
133
134 struct MirrorImageStatus {
135   MirrorImageStatus() {}
136   MirrorImageStatus(MirrorImageStatusState state,
137                     const std::string &description = "")
138     : state(state), description(description) {}
139
140   MirrorImageStatusState state = MIRROR_IMAGE_STATUS_STATE_UNKNOWN;
141   std::string description;
142   utime_t last_update;
143   bool up = false;
144
145   void encode(bufferlist &bl) const;
146   void decode(bufferlist::iterator &it);
147   void dump(Formatter *f) const;
148
149   std::string state_to_string() const;
150
151   static void generate_test_instances(std::list<MirrorImageStatus*> &o);
152
153   bool operator==(const MirrorImageStatus &rhs) const;
154 };
155
156 std::ostream& operator<<(std::ostream& os, const MirrorImageStatus& status);
157 std::ostream& operator<<(std::ostream& os, const MirrorImageStatusState& state);
158
159 WRITE_CLASS_ENCODER(MirrorImageStatus);
160
161 struct GroupImageSpec {
162   GroupImageSpec() {}
163
164   GroupImageSpec(const std::string &image_id, int64_t pool_id)
165     : image_id(image_id), pool_id(pool_id) {}
166
167   static int from_key(const std::string &image_key, GroupImageSpec *spec);
168
169   std::string image_id;
170   int64_t pool_id = -1;
171
172   void encode(bufferlist &bl) const;
173   void decode(bufferlist::iterator &it);
174   void dump(Formatter *f) const;
175
176   std::string image_key();
177
178 };
179
180 WRITE_CLASS_ENCODER(GroupImageSpec);
181
182 struct GroupImageStatus {
183   GroupImageStatus() {}
184   GroupImageStatus(const std::string &image_id,
185                    int64_t pool_id,
186                    GroupImageLinkState state)
187     : spec(image_id, pool_id), state(state) {}
188
189   GroupImageStatus(GroupImageSpec spec,
190                    GroupImageLinkState state)
191     : spec(spec), state(state) {}
192
193   GroupImageSpec spec;
194   GroupImageLinkState state = GROUP_IMAGE_LINK_STATE_INCOMPLETE;
195
196   void encode(bufferlist &bl) const;
197   void decode(bufferlist::iterator &it);
198   void dump(Formatter *f) const;
199
200   std::string state_to_string() const;
201 };
202
203 WRITE_CLASS_ENCODER(GroupImageStatus);
204
205 struct GroupSpec {
206   GroupSpec() {}
207   GroupSpec(const std::string &group_id, int64_t pool_id)
208     : group_id(group_id), pool_id(pool_id) {}
209
210   std::string group_id;
211   int64_t pool_id = -1;
212
213   void encode(bufferlist &bl) const;
214   void decode(bufferlist::iterator &it);
215   void dump(Formatter *f) const;
216   bool is_valid() const;
217 };
218
219 WRITE_CLASS_ENCODER(GroupSpec);
220
221 enum SnapshotNamespaceType {
222   SNAPSHOT_NAMESPACE_TYPE_USER = 0,
223   SNAPSHOT_NAMESPACE_TYPE_GROUP = 1
224 };
225
226 struct UserSnapshotNamespace {
227   static const uint32_t SNAPSHOT_NAMESPACE_TYPE = SNAPSHOT_NAMESPACE_TYPE_USER;
228
229   UserSnapshotNamespace() {}
230
231   void encode(bufferlist& bl) const {}
232   void decode(bufferlist::iterator& it) {}
233
234   void dump(Formatter *f) const {}
235
236   inline bool operator==(const UserSnapshotNamespace& usn) const {
237     return true;
238   }
239
240   inline bool operator<(const UserSnapshotNamespace& usn) const {
241     return false;
242   }
243
244 };
245
246 std::ostream& operator<<(std::ostream& os, const UserSnapshotNamespace& ns);
247
248 struct GroupSnapshotNamespace {
249   static const uint32_t SNAPSHOT_NAMESPACE_TYPE = SNAPSHOT_NAMESPACE_TYPE_GROUP;
250
251   GroupSnapshotNamespace() {}
252
253   GroupSnapshotNamespace(int64_t _group_pool,
254                          const string &_group_id,
255                          const snapid_t &_snapshot_id) :group_pool(_group_pool),
256                                                         group_id(_group_id),
257                                                         snapshot_id(_snapshot_id) {}
258
259   int64_t group_pool;
260   string group_id;
261   snapid_t snapshot_id;
262
263   void encode(bufferlist& bl) const;
264   void decode(bufferlist::iterator& it);
265
266   void dump(Formatter *f) const;
267
268   inline bool operator==(const GroupSnapshotNamespace& gsn) const {
269     return group_pool == gsn.group_pool &&
270            group_id == gsn.group_id &&
271            snapshot_id == gsn.snapshot_id;
272   }
273
274   inline bool operator<(const GroupSnapshotNamespace& gsn) const {
275     if (group_pool < gsn.group_pool) {
276       return true;
277     } else if (group_id < gsn.group_id) {
278       return true;
279     } else {
280       return snapshot_id < gsn.snapshot_id;
281     }
282   }
283
284 };
285
286 std::ostream& operator<<(std::ostream& os, const GroupSnapshotNamespace& ns);
287
288 struct UnknownSnapshotNamespace {
289   static const uint32_t SNAPSHOT_NAMESPACE_TYPE = static_cast<uint32_t>(-1);
290
291   UnknownSnapshotNamespace() {}
292
293   void encode(bufferlist& bl) const {}
294   void decode(bufferlist::iterator& it) {}
295   void dump(Formatter *f) const {}
296   inline bool operator==(const UnknownSnapshotNamespace& gsn) const {
297     return true;
298   }
299
300   inline bool operator<(const UnknownSnapshotNamespace& usn) const {
301     return false;
302   }
303 };
304
305 std::ostream& operator<<(std::ostream& os, const UnknownSnapshotNamespace& ns);
306
307 typedef boost::variant<UserSnapshotNamespace, GroupSnapshotNamespace, UnknownSnapshotNamespace> SnapshotNamespace;
308
309
310 struct SnapshotNamespaceOnDisk {
311
312   SnapshotNamespaceOnDisk() : snapshot_namespace(UnknownSnapshotNamespace()) {}
313   SnapshotNamespaceOnDisk(const SnapshotNamespace &sn) : snapshot_namespace(sn) {}
314
315   SnapshotNamespace snapshot_namespace;
316
317   SnapshotNamespaceType get_namespace_type() const;
318
319   void encode(bufferlist& bl) const;
320   void decode(bufferlist::iterator& it);
321   void dump(Formatter *f) const;
322
323   static void generate_test_instances(std::list<SnapshotNamespaceOnDisk *> &o);
324
325   inline bool operator==(const SnapshotNamespaceOnDisk& gsn) const {
326     return snapshot_namespace == gsn.snapshot_namespace;
327   }
328 };
329 WRITE_CLASS_ENCODER(SnapshotNamespaceOnDisk);
330
331 enum TrashImageSource {
332   TRASH_IMAGE_SOURCE_USER = 0,
333   TRASH_IMAGE_SOURCE_MIRRORING = 1
334 };
335
336 inline void encode(const TrashImageSource &source, bufferlist& bl,
337                    uint64_t features=0)
338 {
339   ::encode(static_cast<uint8_t>(source), bl);
340 }
341
342 inline void decode(TrashImageSource &source, bufferlist::iterator& it)
343 {
344   uint8_t int_source;
345   ::decode(int_source, it);
346   source = static_cast<TrashImageSource>(int_source);
347 }
348
349 struct TrashImageSpec {
350   TrashImageSource source = TRASH_IMAGE_SOURCE_USER;
351   std::string name;
352   utime_t deletion_time; // time of deletion
353   utime_t deferment_end_time;
354
355   TrashImageSpec() {}
356   TrashImageSpec(TrashImageSource source, const std::string &name,
357                    utime_t deletion_time, utime_t deferment_end_time) :
358     source(source), name(name), deletion_time(deletion_time),
359     deferment_end_time(deferment_end_time) {}
360
361   void encode(bufferlist &bl) const;
362   void decode(bufferlist::iterator& it);
363   void dump(Formatter *f) const;
364 };
365 WRITE_CLASS_ENCODER(TrashImageSpec);
366
367 } // namespace rbd
368 } // namespace cls
369
370 using cls::rbd::encode;
371 using cls::rbd::decode;
372
373 #endif // CEPH_CLS_RBD_TYPES_H