]> git.proxmox.com Git - ceph.git/blob - ceph/src/librbd/managed_lock/BreakRequest.cc
import quincy beta 17.1.0
[ceph.git] / ceph / src / librbd / managed_lock / BreakRequest.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
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"
17
18 #define dout_subsys ceph_subsys_rbd
19 #undef dout_prefix
20 #define dout_prefix *_dout << "librbd::managed_lock::BreakRequest: " << this \
21 << " " << __func__ << ": "
22
23 namespace librbd {
24 namespace managed_lock {
25
26 using util::create_context_callback;
27 using util::create_rados_callback;
28
29 template <typename I>
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) {
41 }
42
43 template <typename I>
44 void BreakRequest<I>::send() {
45 send_get_watchers();
46 }
47
48 template <typename I>
49 void BreakRequest<I>::send_get_watchers() {
50 ldout(m_cct, 10) << dendl;
51
52 librados::ObjectReadOperation op;
53 op.list_watchers(&m_watchers, &m_watchers_ret_val);
54
55 using klass = BreakRequest<I>;
56 librados::AioCompletion *rados_completion =
57 create_rados_callback<klass, &klass::handle_get_watchers>(this);
58 m_out_bl.clear();
59 int r = m_ioctx.aio_operate(m_oid, rados_completion, &op, &m_out_bl);
60 ceph_assert(r == 0);
61 rados_completion->release();
62 }
63
64 template <typename I>
65 void BreakRequest<I>::handle_get_watchers(int r) {
66 ldout(m_cct, 10) << "r=" << r << dendl;
67
68 if (r == 0) {
69 r = m_watchers_ret_val;
70 }
71 if (r < 0) {
72 lderr(m_cct) << "failed to retrieve watchers: " << cpp_strerror(r)
73 << dendl;
74 finish(r);
75 return;
76 }
77
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;
83
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;
89 }
90 }
91
92 if (!m_force_break_lock && found_alive_locker) {
93 finish(-EAGAIN);
94 return;
95 }
96
97 send_get_locker();
98 }
99
100 template <typename I>
101 void BreakRequest<I>::send_get_locker() {
102 ldout(m_cct, 10) << dendl;
103
104 using klass = BreakRequest<I>;
105 Context *ctx = create_context_callback<klass, &klass::handle_get_locker>(
106 this);
107 auto req = GetLockerRequest<I>::create(m_ioctx, m_oid, m_exclusive,
108 &m_refreshed_locker, ctx);
109 req->send();
110 }
111
112 template <typename I>
113 void BreakRequest<I>::handle_get_locker(int r) {
114 ldout(m_cct, 10) << "r=" << r << dendl;
115
116 if (r == -ENOENT) {
117 ldout(m_cct, 5) << "no lock owner" << dendl;
118 finish(0);
119 return;
120 } else if (r < 0 && r != -EBUSY) {
121 lderr(m_cct) << "failed to retrieve lockers: " << cpp_strerror(r) << dendl;
122 finish(r);
123 return;
124 } else if (r < 0) {
125 m_refreshed_locker = {};
126 }
127
128 if (m_refreshed_locker != m_locker || m_refreshed_locker == Locker{}) {
129 ldout(m_cct, 5) << "no longer lock owner" << dendl;
130 finish(-EAGAIN);
131 return;
132 }
133
134 send_blocklist();
135 }
136
137 template <typename I>
138 void BreakRequest<I>::send_blocklist() {
139 if (!m_blocklist_locker) {
140 send_break_lock();
141 return;
142 }
143
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;
147
148 if (m_locker.entity == entity_name) {
149 lderr(m_cct) << "attempting to self-blocklist" << dendl;
150 finish(-EINVAL);
151 return;
152 }
153
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
157 << dendl;
158 finish(-EINVAL);
159 return;
160 }
161
162 std::optional<std::chrono::seconds> expire;
163 if (m_blocklist_expire_seconds != 0) {
164 expire = std::chrono::seconds(m_blocklist_expire_seconds);
165 }
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); }));
170 }
171
172 template <typename I>
173 void BreakRequest<I>::handle_blocklist(int r) {
174 ldout(m_cct, 10) << "r=" << r << dendl;
175
176 if (r < 0) {
177 lderr(m_cct) << "failed to blocklist lock owner: " << cpp_strerror(r)
178 << dendl;
179 finish(r);
180 return;
181 }
182
183 wait_for_osd_map();
184 }
185
186 template <typename I>
187 void BreakRequest<I>::wait_for_osd_map() {
188 ldout(m_cct, 10) << dendl;
189
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); }));
193 }
194
195 template <typename I>
196 void BreakRequest<I>::handle_wait_for_osd_map(int r) {
197 ldout(m_cct, 10) << "r=" << r << dendl;
198
199 if (r < 0) {
200 lderr(m_cct) << "failed to wait for updated OSD map: " << cpp_strerror(r)
201 << dendl;
202 finish(r);
203 return;
204 }
205
206 send_break_lock();
207 }
208
209 template <typename I>
210 void BreakRequest<I>::send_break_lock() {
211 ldout(m_cct, 10) << dendl;
212
213 librados::ObjectWriteOperation op;
214 rados::cls::lock::break_lock(&op, RBD_LOCK_NAME, m_locker.cookie,
215 m_locker.entity);
216
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);
221 ceph_assert(r == 0);
222 rados_completion->release();
223 }
224
225 template <typename I>
226 void BreakRequest<I>::handle_break_lock(int r) {
227 ldout(m_cct, 10) << "r=" << r << dendl;
228
229 if (r < 0 && r != -ENOENT) {
230 lderr(m_cct) << "failed to break lock: " << cpp_strerror(r) << dendl;
231 finish(r);
232 return;
233 }
234
235 finish(0);
236 }
237
238 template <typename I>
239 void BreakRequest<I>::finish(int r) {
240 ldout(m_cct, 10) << "r=" << r << dendl;
241
242 m_on_finish->complete(r);
243 delete this;
244 }
245
246 } // namespace managed_lock
247 } // namespace librbd
248
249 template class librbd::managed_lock::BreakRequest<librbd::ImageCtx>;