]> git.proxmox.com Git - ceph.git/blob - ceph/src/librbd/ImageCtx.h
import ceph 16.2.7
[ceph.git] / ceph / src / librbd / ImageCtx.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 #ifndef CEPH_LIBRBD_IMAGECTX_H
4 #define CEPH_LIBRBD_IMAGECTX_H
5
6 #include "include/int_types.h"
7
8 #include <atomic>
9 #include <list>
10 #include <map>
11 #include <memory>
12 #include <set>
13 #include <string>
14 #include <vector>
15
16 #include "common/allocator.h"
17 #include "common/Timer.h"
18 #include "common/ceph_mutex.h"
19 #include "common/config_proxy.h"
20 #include "common/event_socket.h"
21 #include "common/Readahead.h"
22 #include "common/snap_types.h"
23 #include "common/zipkin_trace.h"
24
25 #include "include/common_fwd.h"
26 #include "include/buffer_fwd.h"
27 #include "include/rbd/librbd.hpp"
28 #include "include/rbd_types.h"
29 #include "include/types.h"
30 #include "include/xlist.h"
31
32 #include "cls/rbd/cls_rbd_types.h"
33 #include "cls/rbd/cls_rbd_client.h"
34 #include "librbd/AsyncRequest.h"
35 #include "librbd/Types.h"
36
37 #include <boost/lockfree/policies.hpp>
38 #include <boost/lockfree/queue.hpp>
39
40 namespace neorados {
41 class IOContext;
42 class RADOS;
43 } // namespace neorados
44
45 namespace librbd {
46
47 struct AsioEngine;
48 template <typename> class ConfigWatcher;
49 template <typename> class ExclusiveLock;
50 template <typename> class ImageState;
51 template <typename> class ImageWatcher;
52 template <typename> class Journal;
53 class LibrbdAdminSocketHook;
54 template <typename> class ObjectMap;
55 template <typename> class Operations;
56 template <typename> class PluginRegistry;
57
58 namespace asio { struct ContextWQ; }
59 namespace crypto { class CryptoInterface; }
60 namespace exclusive_lock { struct Policy; }
61 namespace io {
62 class AioCompletion;
63 class AsyncOperation;
64 template <typename> class CopyupRequest;
65 struct ImageDispatcherInterface;
66 struct ObjectDispatcherInterface;
67 }
68 namespace journal { struct Policy; }
69
70 namespace operation {
71 template <typename> class ResizeRequest;
72 }
73
74 struct ImageCtx {
75 typedef std::pair<cls::rbd::SnapshotNamespace, std::string> SnapKey;
76 struct SnapKeyComparator {
77 inline bool operator()(const SnapKey& lhs, const SnapKey& rhs) const {
78 // only compare by namespace type and name
79 if (lhs.first.which() != rhs.first.which()) {
80 return lhs.first.which() < rhs.first.which();
81 }
82 return lhs.second < rhs.second;
83 }
84 };
85
86 static const string METADATA_CONF_PREFIX;
87
88 CephContext *cct;
89 ConfigProxy config;
90 std::set<std::string> config_overrides;
91
92 PerfCounters *perfcounter;
93 struct rbd_obj_header_ondisk header;
94 ::SnapContext snapc;
95 std::vector<librados::snap_t> snaps; // this mirrors snapc.snaps, but is in
96 // a format librados can understand
97 std::map<librados::snap_t, SnapInfo> snap_info;
98 std::map<SnapKey, librados::snap_t, SnapKeyComparator> snap_ids;
99 uint64_t open_snap_id = CEPH_NOSNAP;
100 uint64_t snap_id;
101 bool snap_exists; // false if our snap_id was deleted
102 // whether the image was opened read-only. cannot be changed after opening
103 bool read_only;
104 uint32_t read_only_flags = 0U;
105 uint32_t read_only_mask = ~0U;
106
107 std::map<rados::cls::lock::locker_id_t,
108 rados::cls::lock::locker_info_t> lockers;
109 bool exclusive_locked;
110 std::string lock_tag;
111
112 std::string name;
113 cls::rbd::SnapshotNamespace snap_namespace;
114 std::string snap_name;
115
116 std::shared_ptr<AsioEngine> asio_engine;
117
118 // New ASIO-style RADOS API
119 neorados::RADOS& rados_api;
120
121 // Legacy RADOS API
122 librados::IoCtx data_ctx;
123 librados::IoCtx md_ctx;
124
125 ConfigWatcher<ImageCtx> *config_watcher = nullptr;
126 ImageWatcher<ImageCtx> *image_watcher;
127 Journal<ImageCtx> *journal;
128
129 /**
130 * Lock ordering:
131 *
132 * owner_lock, image_lock
133 * async_op_lock, timestamp_lock
134 */
135 ceph::shared_mutex owner_lock; // protects exclusive lock leadership updates
136 mutable ceph::shared_mutex image_lock; // protects snapshot-related member variables,
137 // features (and associated helper classes), and flags
138 // protects access to the mutable image metadata that
139 // isn't guarded by other locks below, and blocks writes
140 // when held exclusively, so snapshots can be consistent.
141 // Fields guarded include:
142 // total_bytes_read
143 // exclusive_locked
144 // lock_tag
145 // lockers
146 // object_map
147 // parent_md and parent
148
149 ceph::shared_mutex timestamp_lock; // protects (create/access/modify)_timestamp
150 ceph::mutex async_ops_lock; // protects async_ops and async_requests
151 ceph::mutex copyup_list_lock; // protects copyup_waiting_list
152
153 unsigned extra_read_flags; // librados::OPERATION_*
154
155 bool old_format;
156 uint8_t order;
157 uint64_t size;
158 uint64_t features;
159 std::string object_prefix;
160 char *format_string;
161 std::string header_oid;
162 std::string id; // only used for new-format images
163 ParentImageInfo parent_md;
164 ImageCtx *parent;
165 ImageCtx *child = nullptr;
166 MigrationInfo migration_info;
167 cls::rbd::GroupSpec group_spec;
168 uint64_t stripe_unit, stripe_count;
169 uint64_t flags;
170 uint64_t op_features = 0;
171 bool operations_disabled = false;
172 utime_t create_timestamp;
173 utime_t access_timestamp;
174 utime_t modify_timestamp;
175
176 file_layout_t layout;
177
178 Readahead readahead;
179 std::atomic<uint64_t> total_bytes_read = {0};
180
181 std::map<uint64_t, io::CopyupRequest<ImageCtx>*> copyup_list;
182
183 xlist<io::AsyncOperation*> async_ops;
184 xlist<AsyncRequest<>*> async_requests;
185 std::list<Context*> async_requests_waiters;
186
187 ImageState<ImageCtx> *state;
188 Operations<ImageCtx> *operations;
189
190 ExclusiveLock<ImageCtx> *exclusive_lock;
191 ObjectMap<ImageCtx> *object_map;
192
193 xlist<operation::ResizeRequest<ImageCtx>*> resize_reqs;
194
195 io::ImageDispatcherInterface *io_image_dispatcher = nullptr;
196 io::ObjectDispatcherInterface *io_object_dispatcher = nullptr;
197
198 asio::ContextWQ *op_work_queue;
199
200 PluginRegistry<ImageCtx>* plugin_registry;
201
202 typedef boost::lockfree::queue<
203 io::AioCompletion*,
204 boost::lockfree::allocator<ceph::allocator<void>>> Completions;
205
206 Completions event_socket_completions;
207 EventSocket event_socket;
208
209 bool ignore_migrating = false;
210 bool disable_zero_copy = false;
211 bool enable_sparse_copyup = false;
212
213 /// Cached latency-sensitive configuration settings
214 bool non_blocking_aio;
215 bool cache;
216 uint64_t sparse_read_threshold_bytes;
217 uint64_t readahead_max_bytes = 0;
218 uint64_t readahead_disable_after_bytes = 0;
219 bool clone_copy_on_read;
220 bool enable_alloc_hint;
221 uint32_t alloc_hint_flags = 0U;
222 uint32_t read_flags = 0U; // librados::OPERATION_*
223 uint32_t discard_granularity_bytes = 0;
224 bool blkin_trace_all;
225 uint64_t mirroring_replay_delay;
226 uint64_t mtime_update_interval;
227 uint64_t atime_update_interval;
228
229 LibrbdAdminSocketHook *asok_hook;
230
231 exclusive_lock::Policy *exclusive_lock_policy = nullptr;
232 journal::Policy *journal_policy = nullptr;
233
234 ZTracer::Endpoint trace_endpoint;
235
236 crypto::CryptoInterface* crypto = nullptr;
237
238 // unit test mock helpers
239 static ImageCtx* create(const std::string &image_name,
240 const std::string &image_id,
241 const char *snap, IoCtx& p, bool read_only) {
242 return new ImageCtx(image_name, image_id, snap, p, read_only);
243 }
244 static ImageCtx* create(const std::string &image_name,
245 const std::string &image_id,
246 librados::snap_t snap_id, IoCtx& p,
247 bool read_only) {
248 return new ImageCtx(image_name, image_id, snap_id, p, read_only);
249 }
250
251 /**
252 * Either image_name or image_id must be set.
253 * If id is not known, pass the empty std::string,
254 * and init() will look it up.
255 */
256 ImageCtx(const std::string &image_name, const std::string &image_id,
257 const char *snap, IoCtx& p, bool read_only);
258 ImageCtx(const std::string &image_name, const std::string &image_id,
259 librados::snap_t snap_id, IoCtx& p, bool read_only);
260 ~ImageCtx();
261 void init();
262 void shutdown();
263 void init_layout(int64_t pool_id);
264 void perf_start(std::string name);
265 void perf_stop();
266 void set_read_flag(unsigned flag);
267 int get_read_flags(librados::snap_t snap_id);
268 int snap_set(uint64_t snap_id);
269 void snap_unset();
270 librados::snap_t get_snap_id(const cls::rbd::SnapshotNamespace& in_snap_namespace,
271 const std::string& in_snap_name) const;
272 const SnapInfo* get_snap_info(librados::snap_t in_snap_id) const;
273 int get_snap_name(librados::snap_t in_snap_id,
274 std::string *out_snap_name) const;
275 int get_snap_namespace(librados::snap_t in_snap_id,
276 cls::rbd::SnapshotNamespace *out_snap_namespace) const;
277 int get_parent_spec(librados::snap_t in_snap_id,
278 cls::rbd::ParentImageSpec *pspec) const;
279 int is_snap_protected(librados::snap_t in_snap_id,
280 bool *is_protected) const;
281 int is_snap_unprotected(librados::snap_t in_snap_id,
282 bool *is_unprotected) const;
283
284 uint64_t get_current_size() const;
285 uint64_t get_object_size() const;
286 string get_object_name(uint64_t num) const;
287 uint64_t get_stripe_unit() const;
288 uint64_t get_stripe_count() const;
289 uint64_t get_stripe_period() const;
290 utime_t get_create_timestamp() const;
291 utime_t get_access_timestamp() const;
292 utime_t get_modify_timestamp() const;
293
294 void set_access_timestamp(utime_t at);
295 void set_modify_timestamp(utime_t at);
296
297 void add_snap(cls::rbd::SnapshotNamespace in_snap_namespace,
298 std::string in_snap_name,
299 librados::snap_t id,
300 uint64_t in_size, const ParentImageInfo &parent,
301 uint8_t protection_status, uint64_t flags, utime_t timestamp);
302 void rm_snap(cls::rbd::SnapshotNamespace in_snap_namespace,
303 std::string in_snap_name,
304 librados::snap_t id);
305 uint64_t get_image_size(librados::snap_t in_snap_id) const;
306 uint64_t get_effective_image_size(librados::snap_t in_snap_id) const;
307 uint64_t get_object_count(librados::snap_t in_snap_id) const;
308 bool test_features(uint64_t test_features) const;
309 bool test_features(uint64_t test_features,
310 const ceph::shared_mutex &in_image_lock) const;
311 bool test_op_features(uint64_t op_features) const;
312 bool test_op_features(uint64_t op_features,
313 const ceph::shared_mutex &in_image_lock) const;
314 int get_flags(librados::snap_t in_snap_id, uint64_t *flags) const;
315 int test_flags(librados::snap_t in_snap_id,
316 uint64_t test_flags, bool *flags_set) const;
317 int test_flags(librados::snap_t in_snap_id,
318 uint64_t test_flags, const ceph::shared_mutex &in_image_lock,
319 bool *flags_set) const;
320 int update_flags(librados::snap_t in_snap_id, uint64_t flag, bool enabled);
321
322 const ParentImageInfo* get_parent_info(librados::snap_t in_snap_id) const;
323 int64_t get_parent_pool_id(librados::snap_t in_snap_id) const;
324 std::string get_parent_image_id(librados::snap_t in_snap_id) const;
325 uint64_t get_parent_snap_id(librados::snap_t in_snap_id) const;
326 int get_parent_overlap(librados::snap_t in_snap_id,
327 uint64_t *overlap) const;
328 void register_watch(Context *on_finish);
329 uint64_t prune_parent_extents(vector<pair<uint64_t,uint64_t> >& objectx,
330 uint64_t overlap);
331
332 void cancel_async_requests();
333 void cancel_async_requests(Context *on_finish);
334
335 void apply_metadata(const std::map<std::string, bufferlist> &meta,
336 bool thread_safe);
337
338 ExclusiveLock<ImageCtx> *create_exclusive_lock();
339 ObjectMap<ImageCtx> *create_object_map(uint64_t snap_id);
340 Journal<ImageCtx> *create_journal();
341
342 void set_image_name(const std::string &name);
343
344 void notify_update();
345 void notify_update(Context *on_finish);
346
347 exclusive_lock::Policy *get_exclusive_lock_policy() const;
348 void set_exclusive_lock_policy(exclusive_lock::Policy *policy);
349
350 journal::Policy *get_journal_policy() const;
351 void set_journal_policy(journal::Policy *policy);
352
353 void rebuild_data_io_context();
354 IOContext get_data_io_context() const;
355 IOContext duplicate_data_io_context() const;
356
357 static void get_timer_instance(CephContext *cct, SafeTimer **timer,
358 ceph::mutex **timer_lock);
359
360 private:
361 std::shared_ptr<neorados::IOContext> data_io_context;
362 };
363 }
364
365 #endif