1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #ifndef CEPH_LIBRBD_EXCLUSIVE_LOCK_IMAGE_DISPATCH_H
5 #define CEPH_LIBRBD_EXCLUSIVE_LOCK_IMAGE_DISPATCH_H
7 #include "librbd/io/ImageDispatchInterface.h"
8 #include "include/int_types.h"
9 #include "include/buffer.h"
10 #include "common/ceph_mutex.h"
11 #include "common/zipkin_trace.h"
12 #include "librbd/io/ReadResult.h"
13 #include "librbd/io/Types.h"
16 #include <unordered_set>
28 namespace exclusive_lock
{
30 template <typename ImageCtxT
>
31 class ImageDispatch
: public io::ImageDispatchInterface
{
33 static ImageDispatch
* create(ImageCtxT
* image_ctx
) {
34 return new ImageDispatch(image_ctx
);
40 ImageDispatch(ImageCtxT
* image_ctx
);
42 io::ImageDispatchLayer
get_dispatch_layer() const override
{
43 return io::IMAGE_DISPATCH_LAYER_EXCLUSIVE_LOCK
;
46 void set_require_lock(bool init_shutdown
,
47 io::Direction direction
, Context
* on_finish
);
48 void unset_require_lock(io::Direction direction
);
50 void shut_down(Context
* on_finish
) override
;
53 io::AioCompletion
* aio_comp
, io::Extents
&&image_extents
,
54 io::ReadResult
&&read_result
, IOContext io_context
, int op_flags
,
55 int read_flags
, const ZTracer::Trace
&parent_trace
, uint64_t tid
,
56 std::atomic
<uint32_t>* image_dispatch_flags
,
57 io::DispatchResult
* dispatch_result
, Context
** on_finish
,
58 Context
* on_dispatched
) override
;
60 io::AioCompletion
* aio_comp
, io::Extents
&&image_extents
, bufferlist
&&bl
,
61 IOContext io_context
, int op_flags
, const ZTracer::Trace
&parent_trace
,
62 uint64_t tid
, std::atomic
<uint32_t>* image_dispatch_flags
,
63 io::DispatchResult
* dispatch_result
, Context
** on_finish
,
64 Context
* on_dispatched
) override
;
66 io::AioCompletion
* aio_comp
, io::Extents
&&image_extents
,
67 uint32_t discard_granularity_bytes
, IOContext io_context
,
68 const ZTracer::Trace
&parent_trace
, uint64_t tid
,
69 std::atomic
<uint32_t>* image_dispatch_flags
,
70 io::DispatchResult
* dispatch_result
, Context
** on_finish
,
71 Context
* on_dispatched
) override
;
73 io::AioCompletion
* aio_comp
, io::Extents
&&image_extents
, bufferlist
&&bl
,
74 IOContext io_context
, int op_flags
, const ZTracer::Trace
&parent_trace
,
75 uint64_t tid
, std::atomic
<uint32_t>* image_dispatch_flags
,
76 io::DispatchResult
* dispatch_result
, Context
** on_finish
,
77 Context
* on_dispatched
) override
;
78 bool compare_and_write(
79 io::AioCompletion
* aio_comp
, io::Extents
&&image_extents
,
80 bufferlist
&&cmp_bl
, bufferlist
&&bl
, uint64_t *mismatch_offset
,
81 IOContext io_context
, int op_flags
, const ZTracer::Trace
&parent_trace
,
82 uint64_t tid
, std::atomic
<uint32_t>* image_dispatch_flags
,
83 io::DispatchResult
* dispatch_result
, Context
** on_finish
,
84 Context
* on_dispatched
) override
;
86 io::AioCompletion
* aio_comp
, io::FlushSource flush_source
,
87 const ZTracer::Trace
&parent_trace
, uint64_t tid
,
88 std::atomic
<uint32_t>* image_dispatch_flags
,
89 io::DispatchResult
* dispatch_result
, Context
** on_finish
,
90 Context
* on_dispatched
) override
;
93 io::AioCompletion
* aio_comp
, io::Extents
&& image_extents
,
94 io::SnapIds
&& snap_ids
, int list_snaps_flags
,
95 io::SnapshotDelta
* snapshot_delta
, const ZTracer::Trace
&parent_trace
,
96 uint64_t tid
, std::atomic
<uint32_t>* image_dispatch_flags
,
97 io::DispatchResult
* dispatch_result
, Context
** on_finish
,
98 Context
* on_dispatched
) override
{
102 bool invalidate_cache(Context
* on_finish
) override
{
107 typedef std::list
<Context
*> Contexts
;
108 typedef std::unordered_set
<uint64_t> Tids
;
110 ImageCtxT
* m_image_ctx
;
111 mutable ceph::shared_mutex m_lock
;
113 bool m_require_lock_on_read
= false;
114 bool m_require_lock_on_write
= false;
116 Contexts m_on_dispatches
;
118 bool set_require_lock(io::Direction direction
, bool enabled
);
120 bool is_lock_required(bool read_op
) const;
122 bool needs_exclusive_lock(bool read_op
, uint64_t tid
,
123 io::DispatchResult
* dispatch_result
,
124 Context
* on_dispatched
);
126 void handle_acquire_lock(int r
);
129 } // namespace exclusiv_lock
130 } // namespace librbd
132 extern template class librbd::exclusive_lock::ImageDispatch
<librbd::ImageCtx
>;
134 #endif // CEPH_LIBRBD_EXCLUSIVE_LOCK_IMAGE_DISPATCH_H