]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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_RBD_UTILS_H | |
5 | #define CEPH_RBD_UTILS_H | |
6 | ||
7 | #include "include/int_types.h" | |
8 | #include "include/rados/librados.hpp" | |
9 | #include "include/rbd/librbd.hpp" | |
10 | #include "tools/rbd/ArgumentTypes.h" | |
9f95a23c | 11 | #include <map> |
7c673cae FG |
12 | #include <string> |
13 | #include <boost/program_options.hpp> | |
14 | ||
15 | namespace rbd { | |
16 | namespace utils { | |
17 | ||
18 | namespace detail { | |
19 | ||
20 | template <typename T, void(T::*MF)(int)> | |
21 | void aio_completion_callback(librbd::completion_t completion, | |
22 | void *arg) { | |
23 | librbd::RBD::AioCompletion *aio_completion = | |
24 | reinterpret_cast<librbd::RBD::AioCompletion*>(completion); | |
25 | ||
26 | // complete the AIO callback in separate thread context | |
27 | T *t = reinterpret_cast<T *>(arg); | |
28 | int r = aio_completion->get_return_value(); | |
29 | aio_completion->release(); | |
30 | ||
31 | (t->*MF)(r); | |
32 | } | |
33 | ||
34 | } // namespace detail | |
35 | ||
36 | static const std::string RBD_DIFF_BANNER ("rbd diff v1\n"); | |
37 | static const size_t RBD_DEFAULT_SPARSE_SIZE = 4096; | |
38 | ||
39 | static const std::string RBD_IMAGE_BANNER_V2 ("rbd image v2\n"); | |
40 | static const std::string RBD_IMAGE_DIFFS_BANNER_V2 ("rbd image diffs v2\n"); | |
41 | static const std::string RBD_DIFF_BANNER_V2 ("rbd diff v2\n"); | |
42 | ||
43 | #define RBD_DIFF_FROM_SNAP 'f' | |
44 | #define RBD_DIFF_TO_SNAP 't' | |
45 | #define RBD_DIFF_IMAGE_SIZE 's' | |
46 | #define RBD_DIFF_WRITE 'w' | |
47 | #define RBD_DIFF_ZERO 'z' | |
48 | #define RBD_DIFF_END 'e' | |
49 | ||
28e407b8 AA |
50 | #define RBD_SNAP_PROTECTION_STATUS 'p' |
51 | ||
7c673cae FG |
52 | #define RBD_EXPORT_IMAGE_ORDER 'O' |
53 | #define RBD_EXPORT_IMAGE_FEATURES 'T' | |
54 | #define RBD_EXPORT_IMAGE_STRIPE_UNIT 'U' | |
55 | #define RBD_EXPORT_IMAGE_STRIPE_COUNT 'C' | |
28e407b8 | 56 | #define RBD_EXPORT_IMAGE_META 'M' |
7c673cae FG |
57 | #define RBD_EXPORT_IMAGE_END 'E' |
58 | ||
59 | enum SnapshotPresence { | |
60 | SNAPSHOT_PRESENCE_NONE, | |
61 | SNAPSHOT_PRESENCE_PERMITTED, | |
62 | SNAPSHOT_PRESENCE_REQUIRED | |
63 | }; | |
64 | ||
65 | enum SpecValidation { | |
66 | SPEC_VALIDATION_FULL, | |
67 | SPEC_VALIDATION_SNAP, | |
68 | SPEC_VALIDATION_NONE | |
69 | }; | |
70 | ||
71 | struct ProgressContext : public librbd::ProgressContext { | |
72 | const char *operation; | |
73 | bool progress; | |
74 | int last_pc; | |
75 | ||
76 | ProgressContext(const char *o, bool no_progress) | |
77 | : operation(o), progress(!no_progress), last_pc(0) { | |
78 | } | |
79 | ||
80 | int update_progress(uint64_t offset, uint64_t total) override; | |
81 | void finish(); | |
82 | void fail(); | |
83 | }; | |
84 | ||
33c7a0ef TL |
85 | int get_percentage(uint64_t part, uint64_t whole); |
86 | ||
7c673cae FG |
87 | template <typename T, void(T::*MF)(int)> |
88 | librbd::RBD::AioCompletion *create_aio_completion(T *t) { | |
89 | return new librbd::RBD::AioCompletion( | |
90 | t, &detail::aio_completion_callback<T, MF>); | |
91 | } | |
92 | ||
93 | void aio_context_callback(librbd::completion_t completion, void *arg); | |
94 | ||
95 | int read_string(int fd, unsigned max, std::string *out); | |
96 | ||
97 | int extract_spec(const std::string &spec, std::string *pool_name, | |
11fdf7f2 TL |
98 | std::string *namespace_name, std::string *name, |
99 | std::string *snap_name, SpecValidation spec_validation); | |
7c673cae FG |
100 | |
101 | std::string get_positional_argument( | |
102 | const boost::program_options::variables_map &vm, size_t index); | |
103 | ||
f67539c2 | 104 | void normalize_pool_name(std::string* pool_name); |
31f18b77 | 105 | std::string get_default_pool_name(); |
f67539c2 | 106 | |
39ae355f TL |
107 | int get_image_or_snap_spec(const boost::program_options::variables_map &vm, |
108 | std::string *spec); | |
109 | ||
110 | void append_options_as_args(const std::vector<std::string> &options, | |
111 | std::vector<std::string> *args); | |
112 | ||
11fdf7f2 | 113 | int get_pool_and_namespace_names( |
2a845540 | 114 | const boost::program_options::variables_map &vm, bool validate_pool_name, |
11fdf7f2 | 115 | std::string* pool_name, std::string* namespace_name, size_t *arg_index); |
7c673cae FG |
116 | |
117 | int get_pool_image_snapshot_names( | |
118 | const boost::program_options::variables_map &vm, | |
119 | argument_types::ArgumentModifier mod, size_t *spec_arg_index, | |
11fdf7f2 TL |
120 | std::string *pool_name, std::string *namespace_name, |
121 | std::string *image_name, std::string *snap_name, bool image_name_required, | |
122 | SnapshotPresence snapshot_presence, SpecValidation spec_validation); | |
7c673cae | 123 | |
11fdf7f2 | 124 | int get_pool_generic_snapshot_names( |
7c673cae FG |
125 | const boost::program_options::variables_map &vm, |
126 | argument_types::ArgumentModifier mod, size_t *spec_arg_index, | |
11fdf7f2 TL |
127 | const std::string& pool_key, std::string *pool_name, |
128 | std::string *namespace_name, const std::string& generic_key, | |
129 | const std::string& generic_key_desc, std::string *generic_name, | |
130 | std::string *snap_name, bool generic_name_required, | |
131 | SnapshotPresence snapshot_presence, SpecValidation spec_validation); | |
132 | ||
133 | int get_pool_image_id(const boost::program_options::variables_map &vm, | |
134 | size_t *spec_arg_index, | |
135 | std::string *pool_name, | |
136 | std::string *namespace_name, | |
137 | std::string *image_id); | |
7c673cae FG |
138 | |
139 | int validate_snapshot_name(argument_types::ArgumentModifier mod, | |
140 | const std::string &snap_name, | |
141 | SnapshotPresence snapshot_presence, | |
142 | SpecValidation spec_validation); | |
143 | ||
144 | int get_image_options(const boost::program_options::variables_map &vm, | |
145 | bool get_format, librbd::ImageOptions* opts); | |
146 | ||
147 | int get_journal_options(const boost::program_options::variables_map &vm, | |
148 | librbd::ImageOptions *opts); | |
149 | ||
11fdf7f2 TL |
150 | int get_flatten_option(const boost::program_options::variables_map &vm, |
151 | librbd::ImageOptions *opts); | |
152 | ||
7c673cae FG |
153 | int get_image_size(const boost::program_options::variables_map &vm, |
154 | uint64_t *size); | |
155 | ||
156 | int get_path(const boost::program_options::variables_map &vm, | |
11fdf7f2 | 157 | size_t *arg_index, std::string *path); |
7c673cae FG |
158 | |
159 | int get_formatter(const boost::program_options::variables_map &vm, | |
160 | argument_types::Format::Formatter *formatter); | |
161 | ||
f67539c2 TL |
162 | int get_snap_create_flags(const boost::program_options::variables_map &vm, |
163 | uint32_t *flags); | |
164 | ||
7c673cae FG |
165 | void init_context(); |
166 | ||
11fdf7f2 | 167 | int init_rados(librados::Rados *rados); |
7c673cae | 168 | |
f67539c2 | 169 | int init(const std::string& pool_name, const std::string& namespace_name, |
11fdf7f2 | 170 | librados::Rados *rados, librados::IoCtx *io_ctx); |
f67539c2 | 171 | int init_io_ctx(librados::Rados &rados, std::string pool_name, |
11fdf7f2 TL |
172 | const std::string& namespace_name, librados::IoCtx *io_ctx); |
173 | int set_namespace(const std::string& namespace_name, librados::IoCtx *io_ctx); | |
174 | ||
175 | void disable_cache(); | |
7c673cae FG |
176 | |
177 | int open_image(librados::IoCtx &io_ctx, const std::string &image_name, | |
178 | bool read_only, librbd::Image *image); | |
179 | ||
180 | int open_image_by_id(librados::IoCtx &io_ctx, const std::string &image_id, | |
181 | bool read_only, librbd::Image *image); | |
182 | ||
183 | int init_and_open_image(const std::string &pool_name, | |
11fdf7f2 | 184 | const std::string &namespace_name, |
7c673cae FG |
185 | const std::string &image_name, |
186 | const std::string &image_id, | |
187 | const std::string &snap_name, bool read_only, | |
188 | librados::Rados *rados, librados::IoCtx *io_ctx, | |
189 | librbd::Image *image); | |
190 | ||
191 | int snap_set(librbd::Image &image, const std::string &snap_name); | |
192 | ||
193 | void calc_sparse_extent(const bufferptr &bp, | |
194 | size_t sparse_size, | |
195 | size_t buffer_offset, | |
196 | uint64_t length, | |
197 | size_t *write_length, | |
198 | bool *zeroed); | |
199 | ||
11fdf7f2 TL |
200 | bool is_not_user_snap_namespace(librbd::Image* image, |
201 | const librbd::snap_info_t &snap_info); | |
7c673cae FG |
202 | |
203 | std::string image_id(librbd::Image& image); | |
204 | ||
9f95a23c TL |
205 | std::string mirror_image_mode( |
206 | librbd::mirror_image_mode_t mirror_image_mode); | |
207 | std::string mirror_image_state( | |
208 | librbd::mirror_image_state_t mirror_image_state); | |
209 | std::string mirror_image_status_state( | |
210 | librbd::mirror_image_status_state_t state); | |
211 | std::string mirror_image_site_status_state( | |
212 | const librbd::mirror_image_site_status_t& status); | |
213 | std::string mirror_image_global_status_state( | |
214 | const librbd::mirror_image_global_status_t& status); | |
215 | ||
216 | int get_local_mirror_image_status( | |
217 | const librbd::mirror_image_global_status_t& status, | |
218 | librbd::mirror_image_site_status_t* local_status); | |
7c673cae FG |
219 | |
220 | std::string timestr(time_t t); | |
221 | ||
222 | // duplicate here to not include librbd_internal lib | |
223 | uint64_t get_rbd_default_features(CephContext* cct); | |
224 | ||
9f95a23c TL |
225 | void get_mirror_peer_sites( |
226 | librados::IoCtx& io_ctx, | |
227 | std::vector<librbd::mirror_peer_site_t>* mirror_peers); | |
228 | void get_mirror_peer_mirror_uuids_to_names( | |
229 | const std::vector<librbd::mirror_peer_site_t>& mirror_peers, | |
230 | std::map<std::string, std::string>* fsid_to_name); | |
231 | void populate_unknown_mirror_image_site_statuses( | |
232 | const std::vector<librbd::mirror_peer_site_t>& mirror_peers, | |
233 | librbd::mirror_image_global_status_t* global_status); | |
234 | ||
235 | int mgr_command(librados::Rados& rados, const std::string& cmd, | |
236 | const std::map<std::string, std::string> &args, | |
237 | std::ostream *out_os, std::ostream *err_os); | |
238 | ||
7c673cae FG |
239 | } // namespace utils |
240 | } // namespace rbd | |
241 | ||
242 | #endif // CEPH_RBD_UTILS_H |