]>
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" | |
11 | #include <string> | |
12 | #include <boost/program_options.hpp> | |
13 | ||
14 | namespace rbd { | |
15 | namespace utils { | |
16 | ||
17 | namespace detail { | |
18 | ||
19 | template <typename T, void(T::*MF)(int)> | |
20 | void aio_completion_callback(librbd::completion_t completion, | |
21 | void *arg) { | |
22 | librbd::RBD::AioCompletion *aio_completion = | |
23 | reinterpret_cast<librbd::RBD::AioCompletion*>(completion); | |
24 | ||
25 | // complete the AIO callback in separate thread context | |
26 | T *t = reinterpret_cast<T *>(arg); | |
27 | int r = aio_completion->get_return_value(); | |
28 | aio_completion->release(); | |
29 | ||
30 | (t->*MF)(r); | |
31 | } | |
32 | ||
33 | } // namespace detail | |
34 | ||
35 | static const std::string RBD_DIFF_BANNER ("rbd diff v1\n"); | |
36 | static const size_t RBD_DEFAULT_SPARSE_SIZE = 4096; | |
37 | ||
38 | static const std::string RBD_IMAGE_BANNER_V2 ("rbd image v2\n"); | |
39 | static const std::string RBD_IMAGE_DIFFS_BANNER_V2 ("rbd image diffs v2\n"); | |
40 | static const std::string RBD_DIFF_BANNER_V2 ("rbd diff v2\n"); | |
41 | ||
42 | #define RBD_DIFF_FROM_SNAP 'f' | |
43 | #define RBD_DIFF_TO_SNAP 't' | |
44 | #define RBD_DIFF_IMAGE_SIZE 's' | |
45 | #define RBD_DIFF_WRITE 'w' | |
46 | #define RBD_DIFF_ZERO 'z' | |
47 | #define RBD_DIFF_END 'e' | |
48 | ||
49 | #define RBD_EXPORT_IMAGE_ORDER 'O' | |
50 | #define RBD_EXPORT_IMAGE_FEATURES 'T' | |
51 | #define RBD_EXPORT_IMAGE_STRIPE_UNIT 'U' | |
52 | #define RBD_EXPORT_IMAGE_STRIPE_COUNT 'C' | |
53 | #define RBD_EXPORT_IMAGE_END 'E' | |
54 | ||
55 | enum SnapshotPresence { | |
56 | SNAPSHOT_PRESENCE_NONE, | |
57 | SNAPSHOT_PRESENCE_PERMITTED, | |
58 | SNAPSHOT_PRESENCE_REQUIRED | |
59 | }; | |
60 | ||
61 | enum SpecValidation { | |
62 | SPEC_VALIDATION_FULL, | |
63 | SPEC_VALIDATION_SNAP, | |
64 | SPEC_VALIDATION_NONE | |
65 | }; | |
66 | ||
67 | struct ProgressContext : public librbd::ProgressContext { | |
68 | const char *operation; | |
69 | bool progress; | |
70 | int last_pc; | |
71 | ||
72 | ProgressContext(const char *o, bool no_progress) | |
73 | : operation(o), progress(!no_progress), last_pc(0) { | |
74 | } | |
75 | ||
76 | int update_progress(uint64_t offset, uint64_t total) override; | |
77 | void finish(); | |
78 | void fail(); | |
79 | }; | |
80 | ||
81 | template <typename T, void(T::*MF)(int)> | |
82 | librbd::RBD::AioCompletion *create_aio_completion(T *t) { | |
83 | return new librbd::RBD::AioCompletion( | |
84 | t, &detail::aio_completion_callback<T, MF>); | |
85 | } | |
86 | ||
87 | void aio_context_callback(librbd::completion_t completion, void *arg); | |
88 | ||
89 | int read_string(int fd, unsigned max, std::string *out); | |
90 | ||
91 | int extract_spec(const std::string &spec, std::string *pool_name, | |
92 | std::string *image_name, std::string *snap_name, | |
93 | SpecValidation spec_validation); | |
94 | ||
95 | int extract_group_spec(const std::string &spec, | |
96 | std::string *pool_name, | |
97 | std::string *group_name); | |
98 | ||
99 | int extract_image_id_spec(const std::string &spec, std::string *pool_name, | |
100 | std::string *image_id); | |
101 | ||
102 | std::string get_positional_argument( | |
103 | const boost::program_options::variables_map &vm, size_t index); | |
104 | ||
31f18b77 | 105 | std::string get_default_pool_name(); |
7c673cae FG |
106 | std::string get_pool_name(const boost::program_options::variables_map &vm, |
107 | size_t *arg_index); | |
108 | ||
109 | int get_pool_image_snapshot_names( | |
110 | const boost::program_options::variables_map &vm, | |
111 | argument_types::ArgumentModifier mod, size_t *spec_arg_index, | |
112 | std::string *pool_name, std::string *image_name, std::string *snap_name, | |
113 | SnapshotPresence snapshot_presence, SpecValidation spec_validation, | |
114 | bool image_required = true); | |
115 | ||
116 | int get_pool_snapshot_names(const boost::program_options::variables_map &vm, | |
117 | argument_types::ArgumentModifier mod, | |
118 | size_t *spec_arg_index, std::string *pool_name, | |
119 | std::string *snap_name, | |
120 | SnapshotPresence snapshot_presence, | |
121 | SpecValidation spec_validation); | |
122 | ||
123 | int get_special_pool_group_names(const boost::program_options::variables_map &vm, | |
124 | size_t *arg_index, | |
125 | std::string *group_pool_name, | |
126 | std::string *group_name); | |
127 | ||
128 | int get_special_pool_image_names(const boost::program_options::variables_map &vm, | |
129 | size_t *arg_index, | |
130 | std::string *image_pool_name, | |
131 | std::string *image_name); | |
132 | ||
133 | int get_pool_image_id(const boost::program_options::variables_map &vm, | |
134 | size_t *arg_index, std::string *image_pool_name, | |
135 | std::string *image_id); | |
136 | ||
137 | int get_pool_group_names(const boost::program_options::variables_map &vm, | |
138 | argument_types::ArgumentModifier mod, | |
139 | size_t *spec_arg_index, | |
140 | std::string *pool_name, | |
141 | std::string *group_name); | |
142 | ||
143 | int get_pool_journal_names( | |
144 | const boost::program_options::variables_map &vm, | |
145 | argument_types::ArgumentModifier mod, size_t *spec_arg_index, | |
146 | std::string *pool_name, std::string *journal_name); | |
147 | ||
148 | int validate_snapshot_name(argument_types::ArgumentModifier mod, | |
149 | const std::string &snap_name, | |
150 | SnapshotPresence snapshot_presence, | |
151 | SpecValidation spec_validation); | |
152 | ||
153 | int get_image_options(const boost::program_options::variables_map &vm, | |
154 | bool get_format, librbd::ImageOptions* opts); | |
155 | ||
156 | int get_journal_options(const boost::program_options::variables_map &vm, | |
157 | librbd::ImageOptions *opts); | |
158 | ||
159 | int get_image_size(const boost::program_options::variables_map &vm, | |
160 | uint64_t *size); | |
161 | ||
162 | int get_path(const boost::program_options::variables_map &vm, | |
163 | const std::string &positional_path, std::string *path); | |
164 | ||
165 | int get_formatter(const boost::program_options::variables_map &vm, | |
166 | argument_types::Format::Formatter *formatter); | |
167 | ||
168 | void init_context(); | |
169 | ||
170 | int init(const std::string &pool_name, librados::Rados *rados, | |
171 | librados::IoCtx *io_ctx); | |
172 | ||
173 | int init_io_ctx(librados::Rados &rados, const std::string &pool_name, | |
174 | librados::IoCtx *io_ctx); | |
175 | ||
176 | int open_image(librados::IoCtx &io_ctx, const std::string &image_name, | |
177 | bool read_only, librbd::Image *image); | |
178 | ||
179 | int open_image_by_id(librados::IoCtx &io_ctx, const std::string &image_id, | |
180 | bool read_only, librbd::Image *image); | |
181 | ||
182 | int init_and_open_image(const std::string &pool_name, | |
183 | const std::string &image_name, | |
184 | const std::string &image_id, | |
185 | const std::string &snap_name, bool read_only, | |
186 | librados::Rados *rados, librados::IoCtx *io_ctx, | |
187 | librbd::Image *image); | |
188 | ||
189 | int snap_set(librbd::Image &image, const std::string &snap_name); | |
190 | ||
191 | void calc_sparse_extent(const bufferptr &bp, | |
192 | size_t sparse_size, | |
193 | size_t buffer_offset, | |
194 | uint64_t length, | |
195 | size_t *write_length, | |
196 | bool *zeroed); | |
197 | ||
198 | bool check_if_image_spec_present(const boost::program_options::variables_map &vm, | |
199 | argument_types::ArgumentModifier mod, | |
200 | size_t spec_arg_index); | |
201 | ||
202 | std::string image_id(librbd::Image& image); | |
203 | ||
204 | std::string mirror_image_state(librbd::mirror_image_state_t mirror_image_state); | |
205 | std::string mirror_image_status_state(librbd::mirror_image_status_state_t state); | |
206 | std::string mirror_image_status_state(librbd::mirror_image_status_t status); | |
207 | ||
208 | std::string timestr(time_t t); | |
209 | ||
210 | // duplicate here to not include librbd_internal lib | |
211 | uint64_t get_rbd_default_features(CephContext* cct); | |
212 | ||
213 | } // namespace utils | |
214 | } // namespace rbd | |
215 | ||
216 | #endif // CEPH_RBD_UTILS_H |