]> git.proxmox.com Git - ceph.git/blob - ceph/src/librbd/exclusive_lock/ImageDispatch.h
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / librbd / exclusive_lock / ImageDispatch.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #ifndef CEPH_LIBRBD_EXCLUSIVE_LOCK_IMAGE_DISPATCH_H
5 #define CEPH_LIBRBD_EXCLUSIVE_LOCK_IMAGE_DISPATCH_H
6
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"
14 #include <atomic>
15 #include <list>
16 #include <unordered_set>
17
18 struct Context;
19
20 namespace librbd {
21
22 struct ImageCtx;
23
24 namespace io {
25 struct AioCompletion;
26 }
27
28 namespace exclusive_lock {
29
30 template <typename ImageCtxT>
31 class ImageDispatch : public io::ImageDispatchInterface {
32 public:
33 static ImageDispatch* create(ImageCtxT* image_ctx) {
34 return new ImageDispatch(image_ctx);
35 }
36 void destroy() {
37 delete this;
38 }
39
40 ImageDispatch(ImageCtxT* image_ctx);
41
42 io::ImageDispatchLayer get_dispatch_layer() const override {
43 return io::IMAGE_DISPATCH_LAYER_EXCLUSIVE_LOCK;
44 }
45
46 void set_require_lock(bool init_shutdown,
47 io::Direction direction, Context* on_finish);
48 void unset_require_lock(io::Direction direction);
49
50 void shut_down(Context* on_finish) override;
51
52 bool read(
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;
59 bool write(
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;
65 bool discard(
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;
72 bool write_same(
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;
85 bool flush(
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;
91
92 bool list_snaps(
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 {
99 return false;
100 }
101
102 bool invalidate_cache(Context* on_finish) override {
103 return false;
104 }
105
106 private:
107 typedef std::list<Context*> Contexts;
108 typedef std::unordered_set<uint64_t> Tids;
109
110 ImageCtxT* m_image_ctx;
111 mutable ceph::shared_mutex m_lock;
112
113 bool m_require_lock_on_read = false;
114 bool m_require_lock_on_write = false;
115
116 Contexts m_on_dispatches;
117
118 bool set_require_lock(io::Direction direction, bool enabled);
119
120 bool is_lock_required(bool read_op) const;
121
122 bool needs_exclusive_lock(bool read_op, uint64_t tid,
123 io::DispatchResult* dispatch_result,
124 Context* on_dispatched);
125
126 void handle_acquire_lock(int r);
127 };
128
129 } // namespace exclusiv_lock
130 } // namespace librbd
131
132 extern template class librbd::exclusive_lock::ImageDispatch<librbd::ImageCtx>;
133
134 #endif // CEPH_LIBRBD_EXCLUSIVE_LOCK_IMAGE_DISPATCH_H