]>
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 | ||
4 | #ifndef CEPH_LIBRBD_UTILS_H | |
5 | #define CEPH_LIBRBD_UTILS_H | |
6 | ||
7 | #include "include/rados/librados.hpp" | |
8 | #include "include/rbd_types.h" | |
9 | #include "include/Context.h" | |
10 | ||
11 | #include <atomic> | |
12 | #include <type_traits> | |
13 | ||
14 | namespace librbd { | |
15 | ||
16 | class ImageCtx; | |
17 | ||
18 | namespace util { | |
19 | ||
20 | namespace detail { | |
21 | ||
22 | template <typename T> | |
23 | void rados_callback(rados_completion_t c, void *arg) { | |
24 | reinterpret_cast<T*>(arg)->complete(rados_aio_get_return_value(c)); | |
25 | } | |
26 | ||
27 | template <typename T, void(T::*MF)(int)> | |
28 | void rados_callback(rados_completion_t c, void *arg) { | |
29 | T *obj = reinterpret_cast<T*>(arg); | |
30 | int r = rados_aio_get_return_value(c); | |
31 | (obj->*MF)(r); | |
32 | } | |
33 | ||
34 | template <typename T, Context*(T::*MF)(int*), bool destroy> | |
35 | void rados_state_callback(rados_completion_t c, void *arg) { | |
36 | T *obj = reinterpret_cast<T*>(arg); | |
37 | int r = rados_aio_get_return_value(c); | |
38 | Context *on_finish = (obj->*MF)(&r); | |
39 | if (on_finish != nullptr) { | |
40 | on_finish->complete(r); | |
41 | if (destroy) { | |
42 | delete obj; | |
43 | } | |
44 | } | |
45 | } | |
46 | ||
47 | template <typename T, void (T::*MF)(int)> | |
48 | class C_CallbackAdapter : public Context { | |
49 | T *obj; | |
50 | public: | |
51 | C_CallbackAdapter(T *obj) : obj(obj) { | |
52 | } | |
53 | ||
54 | protected: | |
55 | void finish(int r) override { | |
56 | (obj->*MF)(r); | |
57 | } | |
58 | }; | |
59 | ||
60 | template <typename T, Context*(T::*MF)(int*), bool destroy> | |
61 | class C_StateCallbackAdapter : public Context { | |
62 | T *obj; | |
63 | public: | |
64 | C_StateCallbackAdapter(T *obj) : obj(obj){ | |
65 | } | |
66 | ||
67 | protected: | |
68 | void complete(int r) override { | |
69 | Context *on_finish = (obj->*MF)(&r); | |
70 | if (on_finish != nullptr) { | |
71 | on_finish->complete(r); | |
72 | if (destroy) { | |
73 | delete obj; | |
74 | } | |
75 | } | |
76 | Context::complete(r); | |
77 | } | |
78 | void finish(int r) override { | |
79 | } | |
80 | }; | |
81 | ||
82 | template <typename WQ> | |
83 | struct C_AsyncCallback : public Context { | |
84 | WQ *op_work_queue; | |
85 | Context *on_finish; | |
86 | ||
87 | C_AsyncCallback(WQ *op_work_queue, Context *on_finish) | |
88 | : op_work_queue(op_work_queue), on_finish(on_finish) { | |
89 | } | |
90 | void finish(int r) override { | |
91 | op_work_queue->queue(on_finish, r); | |
92 | } | |
93 | }; | |
94 | ||
95 | } // namespace detail | |
96 | ||
97 | std::string generate_image_id(librados::IoCtx &ioctx); | |
98 | ||
99 | const std::string group_header_name(const std::string &group_id); | |
100 | const std::string id_obj_name(const std::string &name); | |
101 | const std::string header_name(const std::string &image_id); | |
102 | const std::string old_header_name(const std::string &image_name); | |
103 | std::string unique_lock_name(const std::string &name, void *address); | |
104 | ||
105 | librados::AioCompletion *create_rados_callback(Context *on_finish); | |
106 | ||
107 | template <typename T> | |
108 | librados::AioCompletion *create_rados_callback(T *obj) { | |
109 | return librados::Rados::aio_create_completion( | |
110 | obj, &detail::rados_callback<T>, nullptr); | |
111 | } | |
112 | ||
113 | template <typename T, void(T::*MF)(int)> | |
114 | librados::AioCompletion *create_rados_callback(T *obj) { | |
115 | return librados::Rados::aio_create_completion( | |
116 | obj, &detail::rados_callback<T, MF>, nullptr); | |
117 | } | |
118 | ||
119 | template <typename T, Context*(T::*MF)(int*), bool destroy=true> | |
120 | librados::AioCompletion *create_rados_callback(T *obj) { | |
121 | return librados::Rados::aio_create_completion( | |
122 | obj, &detail::rados_state_callback<T, MF, destroy>, nullptr); | |
123 | } | |
124 | ||
125 | template <typename T, void(T::*MF)(int) = &T::complete> | |
126 | Context *create_context_callback(T *obj) { | |
127 | return new detail::C_CallbackAdapter<T, MF>(obj); | |
128 | } | |
129 | ||
130 | template <typename T, Context*(T::*MF)(int*), bool destroy=true> | |
131 | Context *create_context_callback(T *obj) { | |
132 | return new detail::C_StateCallbackAdapter<T, MF, destroy>(obj); | |
133 | } | |
134 | ||
135 | template <typename I> | |
136 | Context *create_async_context_callback(I &image_ctx, Context *on_finish) { | |
137 | // use async callback to acquire a clean lock context | |
138 | return new detail::C_AsyncCallback< | |
139 | typename std::decay<decltype(*image_ctx.op_work_queue)>::type>( | |
140 | image_ctx.op_work_queue, on_finish); | |
141 | } | |
142 | ||
143 | template <typename WQ> | |
144 | Context *create_async_context_callback(WQ *work_queue, Context *on_finish) { | |
145 | // use async callback to acquire a clean lock context | |
146 | return new detail::C_AsyncCallback<WQ>(work_queue, on_finish); | |
147 | } | |
148 | ||
149 | // TODO: temporary until AioCompletion supports templated ImageCtx | |
150 | inline ImageCtx *get_image_ctx(ImageCtx *image_ctx) { | |
151 | return image_ctx; | |
152 | } | |
153 | ||
154 | /// helper for tracking in-flight async ops when coordinating | |
155 | /// a shut down of the invoking class instance | |
156 | class AsyncOpTracker { | |
157 | public: | |
158 | void start_op() { | |
159 | m_refs++; | |
160 | } | |
161 | ||
162 | void finish_op() { | |
163 | if (--m_refs == 0 && m_on_finish != nullptr) { | |
164 | Context *on_finish = nullptr; | |
165 | std::swap(on_finish, m_on_finish); | |
166 | on_finish->complete(0); | |
167 | } | |
168 | } | |
169 | ||
170 | template <typename I> | |
171 | void wait(I &image_ctx, Context *on_finish) { | |
172 | assert(m_on_finish == nullptr); | |
173 | ||
174 | on_finish = create_async_context_callback(image_ctx, on_finish); | |
175 | if (m_refs == 0) { | |
176 | on_finish->complete(0); | |
177 | return; | |
178 | } | |
179 | m_on_finish = on_finish; | |
180 | } | |
181 | ||
182 | private: | |
183 | std::atomic<uint64_t> m_refs = { 0 }; | |
184 | Context *m_on_finish = nullptr; | |
185 | }; | |
186 | ||
187 | uint64_t get_rbd_default_features(CephContext* cct); | |
188 | ||
189 | bool calc_sparse_extent(const bufferptr &bp, | |
190 | size_t sparse_size, | |
191 | uint64_t length, | |
192 | size_t *write_offset, | |
193 | size_t *write_length, | |
194 | size_t *offset); | |
195 | } // namespace util | |
196 | ||
197 | } // namespace librbd | |
198 | ||
199 | #endif // CEPH_LIBRBD_UTILS_H |