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
6 #include "include/int_types.h"
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"
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"
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"
37 #include <boost/lockfree/policies.hpp>
38 #include <boost/lockfree/queue.hpp>
43 } // namespace neorados
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
;
58 namespace asio
{ struct ContextWQ
; }
59 namespace crypto
{ class CryptoInterface
; }
60 namespace exclusive_lock
{ struct Policy
; }
64 template <typename
> class CopyupRequest
;
65 struct ImageDispatcherInterface
;
66 struct ObjectDispatcherInterface
;
68 namespace journal
{ struct Policy
; }
71 template <typename
> class ResizeRequest
;
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();
82 return lhs
.second
< rhs
.second
;
86 static const string METADATA_CONF_PREFIX
;
90 std::set
<std::string
> config_overrides
;
92 PerfCounters
*perfcounter
;
93 struct rbd_obj_header_ondisk header
;
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
;
101 bool snap_exists
; // false if our snap_id was deleted
102 // whether the image was opened read-only. cannot be changed after opening
104 uint32_t read_only_flags
= 0U;
105 uint32_t read_only_mask
= ~0U;
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
;
113 cls::rbd::SnapshotNamespace snap_namespace
;
114 std::string snap_name
;
116 std::shared_ptr
<AsioEngine
> asio_engine
;
118 // New ASIO-style RADOS API
119 neorados::RADOS
& rados_api
;
122 librados::IoCtx data_ctx
;
123 librados::IoCtx md_ctx
;
125 ConfigWatcher
<ImageCtx
> *config_watcher
= nullptr;
126 ImageWatcher
<ImageCtx
> *image_watcher
;
127 Journal
<ImageCtx
> *journal
;
132 * owner_lock, image_lock
133 * async_op_lock, timestamp_lock
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:
147 // parent_md and parent
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
153 unsigned extra_read_flags
; // librados::OPERATION_*
159 std::string object_prefix
;
161 std::string header_oid
;
162 std::string id
; // only used for new-format images
163 ParentImageInfo parent_md
;
165 ImageCtx
*child
= nullptr;
166 MigrationInfo migration_info
;
167 cls::rbd::GroupSpec group_spec
;
168 uint64_t stripe_unit
, stripe_count
;
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
;
176 file_layout_t layout
;
179 std::atomic
<uint64_t> total_bytes_read
= {0};
181 std::map
<uint64_t, io::CopyupRequest
<ImageCtx
>*> copyup_list
;
183 xlist
<io::AsyncOperation
*> async_ops
;
184 xlist
<AsyncRequest
<>*> async_requests
;
185 std::list
<Context
*> async_requests_waiters
;
187 ImageState
<ImageCtx
> *state
;
188 Operations
<ImageCtx
> *operations
;
190 ExclusiveLock
<ImageCtx
> *exclusive_lock
;
191 ObjectMap
<ImageCtx
> *object_map
;
193 xlist
<operation::ResizeRequest
<ImageCtx
>*> resize_reqs
;
195 io::ImageDispatcherInterface
*io_image_dispatcher
= nullptr;
196 io::ObjectDispatcherInterface
*io_object_dispatcher
= nullptr;
198 asio::ContextWQ
*op_work_queue
;
200 PluginRegistry
<ImageCtx
>* plugin_registry
;
202 typedef boost::lockfree::queue
<
204 boost::lockfree::allocator
<ceph::allocator
<void>>> Completions
;
206 Completions event_socket_completions
;
207 EventSocket event_socket
;
209 bool ignore_migrating
= false;
210 bool disable_zero_copy
= false;
211 bool enable_sparse_copyup
= false;
213 /// Cached latency-sensitive configuration settings
214 bool non_blocking_aio
;
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
;
229 LibrbdAdminSocketHook
*asok_hook
;
231 exclusive_lock::Policy
*exclusive_lock_policy
= nullptr;
232 journal::Policy
*journal_policy
= nullptr;
234 ZTracer::Endpoint trace_endpoint
;
236 crypto::CryptoInterface
* crypto
= nullptr;
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
);
244 static ImageCtx
* create(const std::string
&image_name
,
245 const std::string
&image_id
,
246 librados::snap_t snap_id
, IoCtx
& p
,
248 return new ImageCtx(image_name
, image_id
, snap_id
, p
, read_only
);
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.
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
);
263 void init_layout(int64_t pool_id
);
264 void perf_start(std::string name
);
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
);
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;
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;
294 void set_access_timestamp(utime_t at
);
295 void set_modify_timestamp(utime_t at
);
297 void add_snap(cls::rbd::SnapshotNamespace in_snap_namespace
,
298 std::string in_snap_name
,
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
);
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
,
332 void cancel_async_requests();
333 void cancel_async_requests(Context
*on_finish
);
335 void apply_metadata(const std::map
<std::string
, bufferlist
> &meta
,
338 ExclusiveLock
<ImageCtx
> *create_exclusive_lock();
339 ObjectMap
<ImageCtx
> *create_object_map(uint64_t snap_id
);
340 Journal
<ImageCtx
> *create_journal();
342 void set_image_name(const std::string
&name
);
344 void notify_update();
345 void notify_update(Context
*on_finish
);
347 exclusive_lock::Policy
*get_exclusive_lock_policy() const;
348 void set_exclusive_lock_policy(exclusive_lock::Policy
*policy
);
350 journal::Policy
*get_journal_policy() const;
351 void set_journal_policy(journal::Policy
*policy
);
353 void rebuild_data_io_context();
354 IOContext
get_data_io_context() const;
355 IOContext
duplicate_data_io_context() const;
357 static void get_timer_instance(CephContext
*cct
, SafeTimer
**timer
,
358 ceph::mutex
**timer_lock
);
361 std::shared_ptr
<neorados::IOContext
> data_io_context
;