1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "librbd/exclusive_lock/PostAcquireRequest.h"
5 #include "cls/lock/cls_lock_client.h"
6 #include "cls/lock/cls_lock_types.h"
7 #include "common/dout.h"
8 #include "common/errno.h"
9 #include "common/WorkQueue.h"
10 #include "include/stringify.h"
11 #include "librbd/ExclusiveLock.h"
12 #include "librbd/ImageCtx.h"
13 #include "librbd/ImageState.h"
14 #include "librbd/ImageWatcher.h"
15 #include "librbd/Journal.h"
16 #include "librbd/ObjectMap.h"
17 #include "librbd/Utils.h"
18 #include "librbd/image/RefreshRequest.h"
19 #include "librbd/journal/Policy.h"
21 #define dout_subsys ceph_subsys_rbd
23 #define dout_prefix *_dout << "librbd::exclusive_lock::PostAcquireRequest: " \
24 << this << " " << __func__ << ": "
27 namespace exclusive_lock
{
29 using util::create_async_context_callback
;
30 using util::create_context_callback
;
31 using util::create_rados_callback
;
34 PostAcquireRequest
<I
>* PostAcquireRequest
<I
>::create(I
&image_ctx
,
37 return new PostAcquireRequest(image_ctx
, on_acquire
, on_finish
);
41 PostAcquireRequest
<I
>::PostAcquireRequest(I
&image_ctx
, Context
*on_acquire
,
43 : m_image_ctx(image_ctx
),
44 m_on_acquire(on_acquire
),
45 m_on_finish(create_async_context_callback(image_ctx
, on_finish
)),
46 m_object_map(nullptr), m_journal(nullptr), m_error_result(0) {
50 PostAcquireRequest
<I
>::~PostAcquireRequest() {
51 if (!m_prepare_lock_completed
) {
52 m_image_ctx
.state
->handle_prepare_lock_complete();
58 void PostAcquireRequest
<I
>::send() {
63 void PostAcquireRequest
<I
>::send_refresh() {
64 if (!m_image_ctx
.state
->is_refresh_required()) {
65 send_open_object_map();
69 CephContext
*cct
= m_image_ctx
.cct
;
70 ldout(cct
, 10) << dendl
;
72 using klass
= PostAcquireRequest
<I
>;
73 Context
*ctx
= create_async_context_callback(
74 m_image_ctx
, create_context_callback
<klass
, &klass::handle_refresh
>(this));
76 // ImageState is blocked waiting for lock to complete -- safe to directly
78 image::RefreshRequest
<I
> *req
= image::RefreshRequest
<I
>::create(
79 m_image_ctx
, true, false, ctx
);
84 void PostAcquireRequest
<I
>::handle_refresh(int r
) {
85 CephContext
*cct
= m_image_ctx
.cct
;
86 ldout(cct
, 10) << "r=" << r
<< dendl
;
89 // next issued IO or op will (re)-refresh the image and shut down lock
90 ldout(cct
, 5) << "exclusive lock dynamically disabled" << dendl
;
93 lderr(cct
) << "failed to refresh image: " << cpp_strerror(r
) << dendl
;
100 send_open_object_map();
103 template <typename I
>
104 void PostAcquireRequest
<I
>::send_open_journal() {
105 // alert caller that we now own the exclusive lock
106 m_on_acquire
->complete(0);
107 m_on_acquire
= nullptr;
109 bool journal_enabled
;
111 std::shared_lock image_locker
{m_image_ctx
.image_lock
};
112 journal_enabled
= (m_image_ctx
.test_features(RBD_FEATURE_JOURNALING
,
113 m_image_ctx
.image_lock
) &&
114 !m_image_ctx
.get_journal_policy()->journal_disabled());
116 if (!journal_enabled
) {
122 CephContext
*cct
= m_image_ctx
.cct
;
123 ldout(cct
, 10) << dendl
;
125 using klass
= PostAcquireRequest
<I
>;
126 Context
*ctx
= create_context_callback
<klass
, &klass::handle_open_journal
>(
128 m_journal
= m_image_ctx
.create_journal();
130 // journal playback requires object map (if enabled) and itself
133 m_journal
->open(ctx
);
136 template <typename I
>
137 void PostAcquireRequest
<I
>::handle_open_journal(int r
) {
138 CephContext
*cct
= m_image_ctx
.cct
;
139 ldout(cct
, 10) << "r=" << r
<< dendl
;
143 lderr(cct
) << "failed to open journal: " << cpp_strerror(r
) << dendl
;
144 send_close_journal();
148 send_allocate_journal_tag();
151 template <typename I
>
152 void PostAcquireRequest
<I
>::send_allocate_journal_tag() {
153 CephContext
*cct
= m_image_ctx
.cct
;
154 ldout(cct
, 10) << dendl
;
156 std::shared_lock image_locker
{m_image_ctx
.image_lock
};
157 using klass
= PostAcquireRequest
<I
>;
158 Context
*ctx
= create_context_callback
<
159 klass
, &klass::handle_allocate_journal_tag
>(this, m_journal
);
160 m_image_ctx
.get_journal_policy()->allocate_tag_on_lock(ctx
);
163 template <typename I
>
164 void PostAcquireRequest
<I
>::handle_allocate_journal_tag(int r
) {
165 CephContext
*cct
= m_image_ctx
.cct
;
166 ldout(cct
, 10) << "r=" << r
<< dendl
;
170 lderr(cct
) << "failed to allocate journal tag: " << cpp_strerror(r
)
172 send_close_journal();
179 template <typename I
>
180 void PostAcquireRequest
<I
>::send_close_journal() {
181 CephContext
*cct
= m_image_ctx
.cct
;
182 ldout(cct
, 10) << dendl
;
184 using klass
= PostAcquireRequest
<I
>;
185 Context
*ctx
= create_context_callback
<klass
, &klass::handle_close_journal
>(
187 m_journal
->close(ctx
);
190 template <typename I
>
191 void PostAcquireRequest
<I
>::handle_close_journal(int r
) {
192 CephContext
*cct
= m_image_ctx
.cct
;
193 ldout(cct
, 10) << "r=" << r
<< dendl
;
197 lderr(cct
) << "failed to close journal: " << cpp_strerror(r
) << dendl
;
200 send_close_object_map();
203 template <typename I
>
204 void PostAcquireRequest
<I
>::send_open_object_map() {
205 if (!m_image_ctx
.test_features(RBD_FEATURE_OBJECT_MAP
)) {
210 CephContext
*cct
= m_image_ctx
.cct
;
211 ldout(cct
, 10) << dendl
;
213 using klass
= PostAcquireRequest
<I
>;
214 Context
*ctx
= create_context_callback
<klass
, &klass::handle_open_object_map
>(
217 m_object_map
= m_image_ctx
.create_object_map(CEPH_NOSNAP
);
218 m_object_map
->open(ctx
);
221 template <typename I
>
222 void PostAcquireRequest
<I
>::handle_open_object_map(int r
) {
223 CephContext
*cct
= m_image_ctx
.cct
;
224 ldout(cct
, 10) << "r=" << r
<< dendl
;
227 lderr(cct
) << "failed to open object map: " << cpp_strerror(r
) << dendl
;
229 m_object_map
= nullptr;
242 template <typename I
>
243 void PostAcquireRequest
<I
>::send_close_object_map() {
244 if (m_object_map
== nullptr) {
250 CephContext
*cct
= m_image_ctx
.cct
;
251 ldout(cct
, 10) << dendl
;
253 using klass
= PostAcquireRequest
<I
>;
254 Context
*ctx
= create_context_callback
<
255 klass
, &klass::handle_close_object_map
>(this);
256 m_object_map
->close(ctx
);
259 template <typename I
>
260 void PostAcquireRequest
<I
>::handle_close_object_map(int r
) {
261 CephContext
*cct
= m_image_ctx
.cct
;
262 ldout(cct
, 10) << "r=" << r
<< dendl
;
265 lderr(cct
) << "failed to close object map: " << cpp_strerror(r
) << dendl
;
272 template <typename I
>
273 void PostAcquireRequest
<I
>::apply() {
275 std::unique_lock image_locker
{m_image_ctx
.image_lock
};
276 ceph_assert(m_image_ctx
.object_map
== nullptr);
277 m_image_ctx
.object_map
= m_object_map
;
279 ceph_assert(m_image_ctx
.journal
== nullptr);
280 m_image_ctx
.journal
= m_journal
;
283 m_prepare_lock_completed
= true;
284 m_image_ctx
.state
->handle_prepare_lock_complete();
287 template <typename I
>
288 void PostAcquireRequest
<I
>::revert() {
289 std::unique_lock image_locker
{m_image_ctx
.image_lock
};
290 m_image_ctx
.object_map
= nullptr;
291 m_image_ctx
.journal
= nullptr;
300 ceph_assert(m_error_result
< 0);
303 template <typename I
>
304 void PostAcquireRequest
<I
>::finish() {
305 m_on_finish
->complete(m_error_result
);
309 } // namespace exclusive_lock
310 } // namespace librbd
312 template class librbd::exclusive_lock::PostAcquireRequest
<librbd::ImageCtx
>;