]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/rbd/Utils.h
update sources to 12.2.7
[ceph.git] / ceph / src / tools / rbd / Utils.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_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_SNAP_PROTECTION_STATUS 'p'
50
51 #define RBD_EXPORT_IMAGE_ORDER 'O'
52 #define RBD_EXPORT_IMAGE_FEATURES 'T'
53 #define RBD_EXPORT_IMAGE_STRIPE_UNIT 'U'
54 #define RBD_EXPORT_IMAGE_STRIPE_COUNT 'C'
55 #define RBD_EXPORT_IMAGE_META 'M'
56 #define RBD_EXPORT_IMAGE_END 'E'
57
58 enum SnapshotPresence {
59 SNAPSHOT_PRESENCE_NONE,
60 SNAPSHOT_PRESENCE_PERMITTED,
61 SNAPSHOT_PRESENCE_REQUIRED
62 };
63
64 enum SpecValidation {
65 SPEC_VALIDATION_FULL,
66 SPEC_VALIDATION_SNAP,
67 SPEC_VALIDATION_NONE
68 };
69
70 struct ProgressContext : public librbd::ProgressContext {
71 const char *operation;
72 bool progress;
73 int last_pc;
74
75 ProgressContext(const char *o, bool no_progress)
76 : operation(o), progress(!no_progress), last_pc(0) {
77 }
78
79 int update_progress(uint64_t offset, uint64_t total) override;
80 void finish();
81 void fail();
82 };
83
84 template <typename T, void(T::*MF)(int)>
85 librbd::RBD::AioCompletion *create_aio_completion(T *t) {
86 return new librbd::RBD::AioCompletion(
87 t, &detail::aio_completion_callback<T, MF>);
88 }
89
90 void aio_context_callback(librbd::completion_t completion, void *arg);
91
92 int read_string(int fd, unsigned max, std::string *out);
93
94 int extract_spec(const std::string &spec, std::string *pool_name,
95 std::string *image_name, std::string *snap_name,
96 SpecValidation spec_validation);
97
98 int extract_group_spec(const std::string &spec,
99 std::string *pool_name,
100 std::string *group_name);
101
102 int extract_image_id_spec(const std::string &spec, std::string *pool_name,
103 std::string *image_id);
104
105 std::string get_positional_argument(
106 const boost::program_options::variables_map &vm, size_t index);
107
108 std::string get_default_pool_name();
109 std::string get_pool_name(const boost::program_options::variables_map &vm,
110 size_t *arg_index);
111
112 int get_pool_image_snapshot_names(
113 const boost::program_options::variables_map &vm,
114 argument_types::ArgumentModifier mod, size_t *spec_arg_index,
115 std::string *pool_name, std::string *image_name, std::string *snap_name,
116 SnapshotPresence snapshot_presence, SpecValidation spec_validation,
117 bool image_required = true);
118
119 int get_pool_snapshot_names(const boost::program_options::variables_map &vm,
120 argument_types::ArgumentModifier mod,
121 size_t *spec_arg_index, std::string *pool_name,
122 std::string *snap_name,
123 SnapshotPresence snapshot_presence,
124 SpecValidation spec_validation);
125
126 int get_special_pool_group_names(const boost::program_options::variables_map &vm,
127 size_t *arg_index,
128 std::string *group_pool_name,
129 std::string *group_name);
130
131 int get_special_pool_image_names(const boost::program_options::variables_map &vm,
132 size_t *arg_index,
133 std::string *image_pool_name,
134 std::string *image_name);
135
136 int get_pool_image_id(const boost::program_options::variables_map &vm,
137 size_t *arg_index, std::string *image_pool_name,
138 std::string *image_id);
139
140 int get_pool_group_names(const boost::program_options::variables_map &vm,
141 argument_types::ArgumentModifier mod,
142 size_t *spec_arg_index,
143 std::string *pool_name,
144 std::string *group_name);
145
146 int get_pool_journal_names(
147 const boost::program_options::variables_map &vm,
148 argument_types::ArgumentModifier mod, size_t *spec_arg_index,
149 std::string *pool_name, std::string *journal_name);
150
151 int validate_snapshot_name(argument_types::ArgumentModifier mod,
152 const std::string &snap_name,
153 SnapshotPresence snapshot_presence,
154 SpecValidation spec_validation);
155
156 int get_image_options(const boost::program_options::variables_map &vm,
157 bool get_format, librbd::ImageOptions* opts);
158
159 int get_journal_options(const boost::program_options::variables_map &vm,
160 librbd::ImageOptions *opts);
161
162 int get_image_size(const boost::program_options::variables_map &vm,
163 uint64_t *size);
164
165 int get_path(const boost::program_options::variables_map &vm,
166 const std::string &positional_path, std::string *path);
167
168 int get_formatter(const boost::program_options::variables_map &vm,
169 argument_types::Format::Formatter *formatter);
170
171 void init_context();
172
173 int init(const std::string &pool_name, librados::Rados *rados,
174 librados::IoCtx *io_ctx);
175
176 int init_io_ctx(librados::Rados &rados, const std::string &pool_name,
177 librados::IoCtx *io_ctx);
178
179 int open_image(librados::IoCtx &io_ctx, const std::string &image_name,
180 bool read_only, librbd::Image *image);
181
182 int open_image_by_id(librados::IoCtx &io_ctx, const std::string &image_id,
183 bool read_only, librbd::Image *image);
184
185 int init_and_open_image(const std::string &pool_name,
186 const std::string &image_name,
187 const std::string &image_id,
188 const std::string &snap_name, bool read_only,
189 librados::Rados *rados, librados::IoCtx *io_ctx,
190 librbd::Image *image);
191
192 int snap_set(librbd::Image &image, const std::string &snap_name);
193
194 void calc_sparse_extent(const bufferptr &bp,
195 size_t sparse_size,
196 size_t buffer_offset,
197 uint64_t length,
198 size_t *write_length,
199 bool *zeroed);
200
201 bool check_if_image_spec_present(const boost::program_options::variables_map &vm,
202 argument_types::ArgumentModifier mod,
203 size_t spec_arg_index);
204
205 std::string image_id(librbd::Image& image);
206
207 std::string mirror_image_state(librbd::mirror_image_state_t mirror_image_state);
208 std::string mirror_image_status_state(librbd::mirror_image_status_state_t state);
209 std::string mirror_image_status_state(librbd::mirror_image_status_t status);
210
211 std::string timestr(time_t t);
212
213 // duplicate here to not include librbd_internal lib
214 uint64_t get_rbd_default_features(CephContext* cct);
215
216 } // namespace utils
217 } // namespace rbd
218
219 #endif // CEPH_RBD_UTILS_H