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 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
, const ZTracer::Trace
&parent_trace
,
68 uint64_t tid
, std::atomic
<uint32_t>* image_dispatch_flags
,
69 io::DispatchResult
* dispatch_result
, Context
** on_finish
,
70 Context
* on_dispatched
) override
;
72 io::AioCompletion
* aio_comp
, io::Extents
&&image_extents
, bufferlist
&&bl
,
73 int op_flags
, const ZTracer::Trace
&parent_trace
,
74 uint64_t tid
, std::atomic
<uint32_t>* image_dispatch_flags
,
75 io::DispatchResult
* dispatch_result
, Context
** on_finish
,
76 Context
* on_dispatched
) override
;
77 bool compare_and_write(
78 io::AioCompletion
* aio_comp
, io::Extents
&&image_extents
,
79 bufferlist
&&cmp_bl
, bufferlist
&&bl
, uint64_t *mismatch_offset
,
80 int op_flags
, const ZTracer::Trace
&parent_trace
,
81 uint64_t tid
, std::atomic
<uint32_t>* image_dispatch_flags
,
82 io::DispatchResult
* dispatch_result
, Context
** on_finish
,
83 Context
* on_dispatched
) override
;
85 io::AioCompletion
* aio_comp
, io::FlushSource flush_source
,
86 const ZTracer::Trace
&parent_trace
, uint64_t tid
,
87 std::atomic
<uint32_t>* image_dispatch_flags
,
88 io::DispatchResult
* dispatch_result
, Context
** on_finish
,
89 Context
* on_dispatched
) override
;
92 io::AioCompletion
* aio_comp
, io::Extents
&& image_extents
,
93 io::SnapIds
&& snap_ids
, int list_snaps_flags
,
94 io::SnapshotDelta
* snapshot_delta
, const ZTracer::Trace
&parent_trace
,
95 uint64_t tid
, std::atomic
<uint32_t>* image_dispatch_flags
,
96 io::DispatchResult
* dispatch_result
, Context
** on_finish
,
97 Context
* on_dispatched
) override
{
101 bool invalidate_cache(Context
* on_finish
) override
{
106 typedef std::list
<Context
*> Contexts
;
107 typedef std::unordered_set
<uint64_t> Tids
;
109 ImageCtxT
* m_image_ctx
;
110 mutable ceph::shared_mutex m_lock
;
112 bool m_require_lock_on_read
= false;
113 bool m_require_lock_on_write
= false;
115 Contexts m_on_dispatches
;
117 bool set_require_lock(io::Direction direction
, bool enabled
);
119 bool is_lock_required(bool read_op
) const;
121 bool needs_exclusive_lock(bool read_op
, uint64_t tid
,
122 io::DispatchResult
* dispatch_result
,
123 Context
* on_dispatched
);
125 void handle_acquire_lock(int r
);
128 } // namespace exclusiv_lock
129 } // namespace librbd
131 extern template class librbd::exclusive_lock::ImageDispatch
<librbd::ImageCtx
>;
133 #endif // CEPH_LIBRBD_EXCLUSIVE_LOCK_IMAGE_DISPATCH_H