1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "librbd/managed_lock/BreakRequest.h"
5 #include "common/dout.h"
6 #include "common/errno.h"
7 #include "include/neorados/RADOS.hpp"
8 #include "include/stringify.h"
9 #include "cls/lock/cls_lock_client.h"
10 #include "cls/lock/cls_lock_types.h"
11 #include "librbd/AsioEngine.h"
12 #include "librbd/ImageCtx.h"
13 #include "librbd/Utils.h"
14 #include "librbd/asio/ContextWQ.h"
15 #include "librbd/asio/Utils.h"
16 #include "librbd/managed_lock/GetLockerRequest.h"
18 #define dout_subsys ceph_subsys_rbd
20 #define dout_prefix *_dout << "librbd::managed_lock::BreakRequest: " << this \
21 << " " << __func__ << ": "
24 namespace managed_lock
{
26 using util::create_context_callback
;
27 using util::create_rados_callback
;
30 BreakRequest
<I
>::BreakRequest(librados::IoCtx
& ioctx
,
31 AsioEngine
& asio_engine
,
32 const std::string
& oid
, const Locker
&locker
,
33 bool exclusive
, bool blocklist_locker
,
34 uint32_t blocklist_expire_seconds
,
35 bool force_break_lock
, Context
*on_finish
)
36 : m_ioctx(ioctx
), m_cct(reinterpret_cast<CephContext
*>(m_ioctx
.cct())),
37 m_asio_engine(asio_engine
), m_oid(oid
), m_locker(locker
),
38 m_exclusive(exclusive
), m_blocklist_locker(blocklist_locker
),
39 m_blocklist_expire_seconds(blocklist_expire_seconds
),
40 m_force_break_lock(force_break_lock
), m_on_finish(on_finish
) {
44 void BreakRequest
<I
>::send() {
49 void BreakRequest
<I
>::send_get_watchers() {
50 ldout(m_cct
, 10) << dendl
;
52 librados::ObjectReadOperation op
;
53 op
.list_watchers(&m_watchers
, &m_watchers_ret_val
);
55 using klass
= BreakRequest
<I
>;
56 librados::AioCompletion
*rados_completion
=
57 create_rados_callback
<klass
, &klass::handle_get_watchers
>(this);
59 int r
= m_ioctx
.aio_operate(m_oid
, rados_completion
, &op
, &m_out_bl
);
61 rados_completion
->release();
65 void BreakRequest
<I
>::handle_get_watchers(int r
) {
66 ldout(m_cct
, 10) << "r=" << r
<< dendl
;
69 r
= m_watchers_ret_val
;
72 lderr(m_cct
) << "failed to retrieve watchers: " << cpp_strerror(r
)
78 bool found_alive_locker
= false;
79 for (auto &watcher
: m_watchers
) {
80 ldout(m_cct
, 20) << "watcher=["
81 << "addr=" << watcher
.addr
<< ", "
82 << "entity=client." << watcher
.watcher_id
<< "]" << dendl
;
84 if ((strncmp(m_locker
.address
.c_str(),
85 watcher
.addr
, sizeof(watcher
.addr
)) == 0) &&
86 (m_locker
.handle
== watcher
.cookie
)) {
87 ldout(m_cct
, 10) << "lock owner is still alive" << dendl
;
88 found_alive_locker
= true;
92 if (!m_force_break_lock
&& found_alive_locker
) {
100 template <typename I
>
101 void BreakRequest
<I
>::send_get_locker() {
102 ldout(m_cct
, 10) << dendl
;
104 using klass
= BreakRequest
<I
>;
105 Context
*ctx
= create_context_callback
<klass
, &klass::handle_get_locker
>(
107 auto req
= GetLockerRequest
<I
>::create(m_ioctx
, m_oid
, m_exclusive
,
108 &m_refreshed_locker
, ctx
);
112 template <typename I
>
113 void BreakRequest
<I
>::handle_get_locker(int r
) {
114 ldout(m_cct
, 10) << "r=" << r
<< dendl
;
117 ldout(m_cct
, 5) << "no lock owner" << dendl
;
120 } else if (r
< 0 && r
!= -EBUSY
) {
121 lderr(m_cct
) << "failed to retrieve lockers: " << cpp_strerror(r
) << dendl
;
125 m_refreshed_locker
= {};
128 if (m_refreshed_locker
!= m_locker
|| m_refreshed_locker
== Locker
{}) {
129 ldout(m_cct
, 5) << "no longer lock owner" << dendl
;
137 template <typename I
>
138 void BreakRequest
<I
>::send_blocklist() {
139 if (!m_blocklist_locker
) {
144 entity_name_t entity_name
= entity_name_t::CLIENT(m_ioctx
.get_instance_id());
145 ldout(m_cct
, 10) << "local entity=" << entity_name
<< ", "
146 << "locker entity=" << m_locker
.entity
<< dendl
;
148 if (m_locker
.entity
== entity_name
) {
149 lderr(m_cct
) << "attempting to self-blocklist" << dendl
;
154 entity_addr_t locker_addr
;
155 if (!locker_addr
.parse(m_locker
.address
)) {
156 lderr(m_cct
) << "unable to parse locker address: " << m_locker
.address
162 std::optional
<std::chrono::seconds
> expire
;
163 if (m_blocklist_expire_seconds
!= 0) {
164 expire
= std::chrono::seconds(m_blocklist_expire_seconds
);
166 m_asio_engine
.get_rados_api().blocklist_add(
167 m_locker
.address
, expire
,
168 librbd::asio::util::get_callback_adapter(
169 [this](int r
) { handle_blocklist(r
); }));
172 template <typename I
>
173 void BreakRequest
<I
>::handle_blocklist(int r
) {
174 ldout(m_cct
, 10) << "r=" << r
<< dendl
;
177 lderr(m_cct
) << "failed to blocklist lock owner: " << cpp_strerror(r
)
186 template <typename I
>
187 void BreakRequest
<I
>::wait_for_osd_map() {
188 ldout(m_cct
, 10) << dendl
;
190 m_asio_engine
.get_rados_api().wait_for_latest_osd_map(
191 librbd::asio::util::get_callback_adapter(
192 [this](int r
) { handle_wait_for_osd_map(r
); }));
195 template <typename I
>
196 void BreakRequest
<I
>::handle_wait_for_osd_map(int r
) {
197 ldout(m_cct
, 10) << "r=" << r
<< dendl
;
200 lderr(m_cct
) << "failed to wait for updated OSD map: " << cpp_strerror(r
)
209 template <typename I
>
210 void BreakRequest
<I
>::send_break_lock() {
211 ldout(m_cct
, 10) << dendl
;
213 librados::ObjectWriteOperation op
;
214 rados::cls::lock::break_lock(&op
, RBD_LOCK_NAME
, m_locker
.cookie
,
217 using klass
= BreakRequest
<I
>;
218 librados::AioCompletion
*rados_completion
=
219 create_rados_callback
<klass
, &klass::handle_break_lock
>(this);
220 int r
= m_ioctx
.aio_operate(m_oid
, rados_completion
, &op
);
222 rados_completion
->release();
225 template <typename I
>
226 void BreakRequest
<I
>::handle_break_lock(int r
) {
227 ldout(m_cct
, 10) << "r=" << r
<< dendl
;
229 if (r
< 0 && r
!= -ENOENT
) {
230 lderr(m_cct
) << "failed to break lock: " << cpp_strerror(r
) << dendl
;
238 template <typename I
>
239 void BreakRequest
<I
>::finish(int r
) {
240 ldout(m_cct
, 10) << "r=" << r
<< dendl
;
242 m_on_finish
->complete(r
);
246 } // namespace managed_lock
247 } // namespace librbd
249 template class librbd::managed_lock::BreakRequest
<librbd::ImageCtx
>;