]>
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 | #ifndef CEPH_LIBRBD_IMAGECTX_H | |
4 | #define CEPH_LIBRBD_IMAGECTX_H | |
5 | ||
6 | #include "include/int_types.h" | |
7 | ||
8 | #include <list> | |
9 | #include <map> | |
11fdf7f2 | 10 | #include <set> |
7c673cae FG |
11 | #include <string> |
12 | #include <vector> | |
13 | ||
11fdf7f2 | 14 | #include "common/config_proxy.h" |
7c673cae FG |
15 | #include "common/event_socket.h" |
16 | #include "common/Mutex.h" | |
17 | #include "common/Readahead.h" | |
18 | #include "common/RWLock.h" | |
19 | #include "common/snap_types.h" | |
31f18b77 | 20 | #include "common/zipkin_trace.h" |
7c673cae FG |
21 | |
22 | #include "include/buffer_fwd.h" | |
23 | #include "include/rbd/librbd.hpp" | |
24 | #include "include/rbd_types.h" | |
25 | #include "include/types.h" | |
26 | #include "include/xlist.h" | |
7c673cae FG |
27 | |
28 | #include "cls/rbd/cls_rbd_types.h" | |
29 | #include "cls/rbd/cls_rbd_client.h" | |
30 | #include "librbd/AsyncRequest.h" | |
31 | #include "librbd/Types.h" | |
32 | ||
33 | class CephContext; | |
34 | class ContextWQ; | |
35 | class Finisher; | |
36 | class PerfCounters; | |
37 | class ThreadPool; | |
38 | class SafeTimer; | |
39 | ||
40 | namespace librbd { | |
41 | ||
7c673cae FG |
42 | template <typename> class ExclusiveLock; |
43 | template <typename> class ImageState; | |
44 | template <typename> class ImageWatcher; | |
45 | template <typename> class Journal; | |
46 | class LibrbdAdminSocketHook; | |
47 | template <typename> class ObjectMap; | |
48 | template <typename> class Operations; | |
7c673cae FG |
49 | |
50 | namespace cache { struct ImageCache; } | |
51 | namespace exclusive_lock { struct Policy; } | |
52 | namespace io { | |
53 | class AioCompletion; | |
31f18b77 | 54 | class AsyncOperation; |
b32b8144 | 55 | template <typename> class CopyupRequest; |
224ce89b | 56 | template <typename> class ImageRequestWQ; |
11fdf7f2 | 57 | template <typename> class ObjectDispatcher; |
7c673cae FG |
58 | } |
59 | namespace journal { struct Policy; } | |
60 | ||
61 | namespace operation { | |
62 | template <typename> class ResizeRequest; | |
63 | } | |
64 | ||
65 | struct ImageCtx { | |
11fdf7f2 TL |
66 | static const string METADATA_CONF_PREFIX; |
67 | ||
7c673cae | 68 | CephContext *cct; |
11fdf7f2 TL |
69 | ConfigProxy config; |
70 | std::set<std::string> config_overrides; | |
71 | ||
7c673cae FG |
72 | PerfCounters *perfcounter; |
73 | struct rbd_obj_header_ondisk header; | |
74 | ::SnapContext snapc; | |
75 | std::vector<librados::snap_t> snaps; // this mirrors snapc.snaps, but is in | |
76 | // a format librados can understand | |
77 | std::map<librados::snap_t, SnapInfo> snap_info; | |
78 | std::map<std::pair<cls::rbd::SnapshotNamespace, std::string>, librados::snap_t> snap_ids; | |
11fdf7f2 | 79 | uint64_t open_snap_id = CEPH_NOSNAP; |
7c673cae FG |
80 | uint64_t snap_id; |
81 | bool snap_exists; // false if our snap_id was deleted | |
82 | // whether the image was opened read-only. cannot be changed after opening | |
83 | bool read_only; | |
7c673cae FG |
84 | |
85 | std::map<rados::cls::lock::locker_id_t, | |
86 | rados::cls::lock::locker_info_t> lockers; | |
87 | bool exclusive_locked; | |
88 | std::string lock_tag; | |
89 | ||
90 | std::string name; | |
91 | cls::rbd::SnapshotNamespace snap_namespace; | |
92 | std::string snap_name; | |
93 | IoCtx data_ctx, md_ctx; | |
94 | ImageWatcher<ImageCtx> *image_watcher; | |
95 | Journal<ImageCtx> *journal; | |
96 | ||
97 | /** | |
98 | * Lock ordering: | |
99 | * | |
11fdf7f2 TL |
100 | * owner_lock, md_lock, snap_lock, parent_lock, |
101 | * object_map_lock, async_op_lock, timestamp_lock | |
7c673cae FG |
102 | */ |
103 | RWLock owner_lock; // protects exclusive lock leadership updates | |
104 | RWLock md_lock; // protects access to the mutable image metadata that | |
105 | // isn't guarded by other locks below, and blocks writes | |
106 | // when held exclusively, so snapshots can be consistent. | |
107 | // Fields guarded include: | |
7c673cae FG |
108 | // total_bytes_read |
109 | // exclusive_locked | |
110 | // lock_tag | |
111 | // lockers | |
7c673cae FG |
112 | RWLock snap_lock; // protects snapshot-related member variables, |
113 | // features (and associated helper classes), and flags | |
11fdf7f2 | 114 | RWLock timestamp_lock; |
7c673cae FG |
115 | RWLock parent_lock; // protects parent_md and parent |
116 | RWLock object_map_lock; // protects object map updates and object_map itself | |
117 | Mutex async_ops_lock; // protects async_ops and async_requests | |
118 | Mutex copyup_list_lock; // protects copyup_waiting_list | |
119 | Mutex completed_reqs_lock; // protects completed_reqs | |
120 | ||
121 | unsigned extra_read_flags; | |
122 | ||
123 | bool old_format; | |
124 | uint8_t order; | |
125 | uint64_t size; | |
126 | uint64_t features; | |
127 | std::string object_prefix; | |
128 | char *format_string; | |
129 | std::string header_oid; | |
130 | std::string id; // only used for new-format images | |
11fdf7f2 | 131 | ParentImageInfo parent_md; |
7c673cae | 132 | ImageCtx *parent; |
b32b8144 | 133 | ImageCtx *child = nullptr; |
11fdf7f2 | 134 | MigrationInfo migration_info; |
7c673cae FG |
135 | cls::rbd::GroupSpec group_spec; |
136 | uint64_t stripe_unit, stripe_count; | |
137 | uint64_t flags; | |
11fdf7f2 TL |
138 | uint64_t op_features = 0; |
139 | bool operations_disabled = false; | |
31f18b77 | 140 | utime_t create_timestamp; |
11fdf7f2 TL |
141 | utime_t access_timestamp; |
142 | utime_t modify_timestamp; | |
7c673cae FG |
143 | |
144 | file_layout_t layout; | |
145 | ||
146 | cache::ImageCache *image_cache = nullptr; | |
7c673cae FG |
147 | |
148 | Readahead readahead; | |
149 | uint64_t total_bytes_read; | |
150 | ||
b32b8144 | 151 | std::map<uint64_t, io::CopyupRequest<ImageCtx>*> copyup_list; |
7c673cae | 152 | |
31f18b77 | 153 | xlist<io::AsyncOperation*> async_ops; |
7c673cae FG |
154 | xlist<AsyncRequest<>*> async_requests; |
155 | std::list<Context*> async_requests_waiters; | |
156 | ||
157 | ImageState<ImageCtx> *state; | |
158 | Operations<ImageCtx> *operations; | |
159 | ||
160 | ExclusiveLock<ImageCtx> *exclusive_lock; | |
161 | ObjectMap<ImageCtx> *object_map; | |
162 | ||
163 | xlist<operation::ResizeRequest<ImageCtx>*> resize_reqs; | |
164 | ||
224ce89b | 165 | io::ImageRequestWQ<ImageCtx> *io_work_queue; |
11fdf7f2 TL |
166 | io::ObjectDispatcher<ImageCtx> *io_object_dispatcher = nullptr; |
167 | ||
7c673cae FG |
168 | xlist<io::AioCompletion*> completed_reqs; |
169 | EventSocket event_socket; | |
170 | ||
171 | ContextWQ *op_work_queue; | |
172 | ||
11fdf7f2 TL |
173 | bool ignore_migrating = false; |
174 | ||
175 | /// Cached latency-sensitive configuration settings | |
7c673cae FG |
176 | bool non_blocking_aio; |
177 | bool cache; | |
178 | bool cache_writethrough_until_flush; | |
7c673cae | 179 | uint64_t cache_max_dirty; |
b32b8144 | 180 | uint64_t sparse_read_threshold_bytes; |
7c673cae FG |
181 | uint64_t readahead_max_bytes; |
182 | uint64_t readahead_disable_after_bytes; | |
183 | bool clone_copy_on_read; | |
7c673cae | 184 | bool enable_alloc_hint; |
11fdf7f2 | 185 | uint32_t discard_granularity_bytes = 0; |
181888fb | 186 | bool blkin_trace_all; |
11fdf7f2 TL |
187 | uint64_t mirroring_replay_delay; |
188 | uint64_t mtime_update_interval; | |
189 | uint64_t atime_update_interval; | |
7c673cae FG |
190 | |
191 | LibrbdAdminSocketHook *asok_hook; | |
192 | ||
193 | exclusive_lock::Policy *exclusive_lock_policy = nullptr; | |
194 | journal::Policy *journal_policy = nullptr; | |
195 | ||
31f18b77 FG |
196 | ZTracer::Endpoint trace_endpoint; |
197 | ||
7c673cae FG |
198 | // unit test mock helpers |
199 | static ImageCtx* create(const std::string &image_name, | |
200 | const std::string &image_id, | |
201 | const char *snap, IoCtx& p, bool read_only) { | |
202 | return new ImageCtx(image_name, image_id, snap, p, read_only); | |
203 | } | |
11fdf7f2 TL |
204 | static ImageCtx* create(const std::string &image_name, |
205 | const std::string &image_id, | |
206 | librados::snap_t snap_id, IoCtx& p, | |
207 | bool read_only) { | |
208 | return new ImageCtx(image_name, image_id, snap_id, p, read_only); | |
209 | } | |
7c673cae FG |
210 | void destroy() { |
211 | delete this; | |
212 | } | |
213 | ||
214 | /** | |
215 | * Either image_name or image_id must be set. | |
216 | * If id is not known, pass the empty std::string, | |
217 | * and init() will look it up. | |
218 | */ | |
219 | ImageCtx(const std::string &image_name, const std::string &image_id, | |
220 | const char *snap, IoCtx& p, bool read_only); | |
11fdf7f2 TL |
221 | ImageCtx(const std::string &image_name, const std::string &image_id, |
222 | librados::snap_t snap_id, IoCtx& p, bool read_only); | |
7c673cae FG |
223 | ~ImageCtx(); |
224 | void init(); | |
225 | void shutdown(); | |
eafe8130 | 226 | void init_layout(int64_t pool_id); |
7c673cae FG |
227 | void perf_start(std::string name); |
228 | void perf_stop(); | |
229 | void set_read_flag(unsigned flag); | |
230 | int get_read_flags(librados::snap_t snap_id); | |
11fdf7f2 | 231 | int snap_set(uint64_t snap_id); |
7c673cae | 232 | void snap_unset(); |
11fdf7f2 TL |
233 | librados::snap_t get_snap_id(const cls::rbd::SnapshotNamespace& in_snap_namespace, |
234 | const std::string& in_snap_name) const; | |
7c673cae FG |
235 | const SnapInfo* get_snap_info(librados::snap_t in_snap_id) const; |
236 | int get_snap_name(librados::snap_t in_snap_id, | |
237 | std::string *out_snap_name) const; | |
238 | int get_snap_namespace(librados::snap_t in_snap_id, | |
239 | cls::rbd::SnapshotNamespace *out_snap_namespace) const; | |
240 | int get_parent_spec(librados::snap_t in_snap_id, | |
11fdf7f2 | 241 | cls::rbd::ParentImageSpec *pspec) const; |
7c673cae FG |
242 | int is_snap_protected(librados::snap_t in_snap_id, |
243 | bool *is_protected) const; | |
244 | int is_snap_unprotected(librados::snap_t in_snap_id, | |
245 | bool *is_unprotected) const; | |
246 | ||
247 | uint64_t get_current_size() const; | |
248 | uint64_t get_object_size() const; | |
249 | string get_object_name(uint64_t num) const; | |
250 | uint64_t get_stripe_unit() const; | |
251 | uint64_t get_stripe_count() const; | |
252 | uint64_t get_stripe_period() const; | |
31f18b77 | 253 | utime_t get_create_timestamp() const; |
11fdf7f2 TL |
254 | utime_t get_access_timestamp() const; |
255 | utime_t get_modify_timestamp() const; | |
256 | ||
257 | void set_access_timestamp(utime_t at); | |
258 | void set_modify_timestamp(utime_t at); | |
7c673cae FG |
259 | |
260 | void add_snap(cls::rbd::SnapshotNamespace in_snap_namespace, | |
261 | std::string in_snap_name, | |
262 | librados::snap_t id, | |
11fdf7f2 | 263 | uint64_t in_size, const ParentImageInfo &parent, |
7c673cae FG |
264 | uint8_t protection_status, uint64_t flags, utime_t timestamp); |
265 | void rm_snap(cls::rbd::SnapshotNamespace in_snap_namespace, | |
266 | std::string in_snap_name, | |
267 | librados::snap_t id); | |
268 | uint64_t get_image_size(librados::snap_t in_snap_id) const; | |
269 | uint64_t get_object_count(librados::snap_t in_snap_id) const; | |
270 | bool test_features(uint64_t test_features) const; | |
271 | bool test_features(uint64_t test_features, | |
272 | const RWLock &in_snap_lock) const; | |
11fdf7f2 TL |
273 | bool test_op_features(uint64_t op_features) const; |
274 | bool test_op_features(uint64_t op_features, | |
275 | const RWLock &in_snap_lock) const; | |
7c673cae | 276 | int get_flags(librados::snap_t in_snap_id, uint64_t *flags) const; |
91327a77 AA |
277 | int test_flags(librados::snap_t in_snap_id, |
278 | uint64_t test_flags, bool *flags_set) const; | |
279 | int test_flags(librados::snap_t in_snap_id, | |
280 | uint64_t test_flags, const RWLock &in_snap_lock, | |
31f18b77 | 281 | bool *flags_set) const; |
7c673cae FG |
282 | int update_flags(librados::snap_t in_snap_id, uint64_t flag, bool enabled); |
283 | ||
11fdf7f2 | 284 | const ParentImageInfo* get_parent_info(librados::snap_t in_snap_id) const; |
7c673cae FG |
285 | int64_t get_parent_pool_id(librados::snap_t in_snap_id) const; |
286 | std::string get_parent_image_id(librados::snap_t in_snap_id) const; | |
287 | uint64_t get_parent_snap_id(librados::snap_t in_snap_id) const; | |
288 | int get_parent_overlap(librados::snap_t in_snap_id, | |
289 | uint64_t *overlap) const; | |
7c673cae FG |
290 | void register_watch(Context *on_finish); |
291 | uint64_t prune_parent_extents(vector<pair<uint64_t,uint64_t> >& objectx, | |
292 | uint64_t overlap); | |
293 | ||
7c673cae FG |
294 | void cancel_async_requests(); |
295 | void cancel_async_requests(Context *on_finish); | |
296 | ||
b32b8144 FG |
297 | void apply_metadata(const std::map<std::string, bufferlist> &meta, |
298 | bool thread_safe); | |
7c673cae FG |
299 | |
300 | ExclusiveLock<ImageCtx> *create_exclusive_lock(); | |
301 | ObjectMap<ImageCtx> *create_object_map(uint64_t snap_id); | |
302 | Journal<ImageCtx> *create_journal(); | |
303 | ||
304 | void clear_pending_completions(); | |
305 | ||
306 | void set_image_name(const std::string &name); | |
307 | ||
308 | void notify_update(); | |
309 | void notify_update(Context *on_finish); | |
310 | ||
311 | exclusive_lock::Policy *get_exclusive_lock_policy() const; | |
312 | void set_exclusive_lock_policy(exclusive_lock::Policy *policy); | |
313 | ||
314 | journal::Policy *get_journal_policy() const; | |
315 | void set_journal_policy(journal::Policy *policy); | |
316 | ||
28e407b8 AA |
317 | bool is_writeback_cache_enabled() const; |
318 | ||
7c673cae FG |
319 | static void get_thread_pool_instance(CephContext *cct, |
320 | ThreadPool **thread_pool, | |
321 | ContextWQ **op_work_queue); | |
322 | static void get_timer_instance(CephContext *cct, SafeTimer **timer, | |
323 | Mutex **timer_lock); | |
324 | }; | |
325 | } | |
326 | ||
327 | #endif |