Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / 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 "common/WorkQueue.h"
8 #include "include/stringify.h"
9 #include "cls/lock/cls_lock_client.h"
10 #include "cls/lock/cls_lock_types.h"
11 #include "librbd/ImageCtx.h"
12 #include "librbd/Utils.h"
13 #include "librbd/managed_lock/GetLockerRequest.h"
14
15 #define dout_subsys ceph_subsys_rbd
16 #undef dout_prefix
17 #define dout_prefix *_dout << "librbd::managed_lock::BreakRequest: " << this \
18                            << " " << __func__ << ": "
19
20 namespace librbd {
21 namespace managed_lock {
22
23 using util::create_context_callback;
24 using util::create_rados_callback;
25
26 namespace {
27
28 struct C_BlacklistClient : public Context {
29   librados::IoCtx &ioctx;
30   std::string locker_address;
31   uint32_t expire_seconds;
32   Context *on_finish;
33
34   C_BlacklistClient(librados::IoCtx &ioctx, const std::string &locker_address,
35                     uint32_t expire_seconds, Context *on_finish)
36     : ioctx(ioctx), locker_address(locker_address),
37       expire_seconds(expire_seconds), on_finish(on_finish) {
38   }
39
40   void finish(int r) override {
41     librados::Rados rados(ioctx);
42     r = rados.blacklist_add(locker_address, expire_seconds);
43     on_finish->complete(r);
44   }
45 };
46
47 } // anonymous namespace
48
49 template <typename I>
50 BreakRequest<I>::BreakRequest(librados::IoCtx& ioctx, ContextWQ *work_queue,
51                               const std::string& oid, const Locker &locker,
52                               bool exclusive, bool blacklist_locker,
53                               uint32_t blacklist_expire_seconds,
54                               bool force_break_lock, Context *on_finish)
55   : m_ioctx(ioctx), m_cct(reinterpret_cast<CephContext *>(m_ioctx.cct())),
56     m_work_queue(work_queue), m_oid(oid), m_locker(locker),
57     m_exclusive(exclusive), m_blacklist_locker(blacklist_locker),
58     m_blacklist_expire_seconds(blacklist_expire_seconds),
59     m_force_break_lock(force_break_lock), m_on_finish(on_finish) {
60 }
61
62 template <typename I>
63 void BreakRequest<I>::send() {
64   send_get_watchers();
65 }
66
67 template <typename I>
68 void BreakRequest<I>::send_get_watchers() {
69   ldout(m_cct, 10) << dendl;
70
71   librados::ObjectReadOperation op;
72   op.list_watchers(&m_watchers, &m_watchers_ret_val);
73
74   using klass = BreakRequest<I>;
75   librados::AioCompletion *rados_completion =
76     create_rados_callback<klass, &klass::handle_get_watchers>(this);
77   m_out_bl.clear();
78   int r = m_ioctx.aio_operate(m_oid, rados_completion, &op, &m_out_bl);
79   assert(r == 0);
80   rados_completion->release();
81 }
82
83 template <typename I>
84 void BreakRequest<I>::handle_get_watchers(int r) {
85   ldout(m_cct, 10) << "r=" << r << dendl;
86
87   if (r == 0) {
88     r = m_watchers_ret_val;
89   }
90   if (r < 0) {
91     lderr(m_cct) << "failed to retrieve watchers: " << cpp_strerror(r)
92                  << dendl;
93     finish(r);
94     return;
95   }
96
97   bool found_alive_locker = false;
98   for (auto &watcher : m_watchers) {
99     ldout(m_cct, 20) << "watcher=["
100                      << "addr=" << watcher.addr << ", "
101                      << "entity=client." << watcher.watcher_id << "]" << dendl;
102
103     if ((strncmp(m_locker.address.c_str(),
104                  watcher.addr, sizeof(watcher.addr)) == 0) &&
105         (m_locker.handle == watcher.cookie)) {
106       ldout(m_cct, 10) << "lock owner is still alive" << dendl;
107       found_alive_locker = true;
108     }
109   }
110
111   if (!m_force_break_lock && found_alive_locker) {
112     finish(-EAGAIN);
113     return;
114   }
115
116   send_get_locker();
117 }
118
119 template <typename I>
120 void BreakRequest<I>::send_get_locker() {
121   ldout(m_cct, 10) << dendl;
122
123   using klass = BreakRequest<I>;
124   Context *ctx = create_context_callback<klass, &klass::handle_get_locker>(
125     this);
126   auto req = GetLockerRequest<I>::create(m_ioctx, m_oid, m_exclusive,
127                                          &m_refreshed_locker, ctx);
128   req->send();
129 }
130
131 template <typename I>
132 void BreakRequest<I>::handle_get_locker(int r) {
133   ldout(m_cct, 10) << "r=" << r << dendl;
134
135   if (r == -ENOENT) {
136     ldout(m_cct, 5) << "no lock owner" << dendl;
137     finish(0);
138     return;
139   } else if (r < 0 && r != -EBUSY) {
140     lderr(m_cct) << "failed to retrieve lockers: " << cpp_strerror(r) << dendl;
141     finish(r);
142     return;
143   } else if (r < 0) {
144     m_refreshed_locker = {};
145   }
146
147   if (m_refreshed_locker != m_locker || m_refreshed_locker == Locker{}) {
148     ldout(m_cct, 5) << "no longer lock owner" << dendl;
149     finish(-EAGAIN);
150     return;
151   }
152
153   send_blacklist();
154 }
155
156 template <typename I>
157 void BreakRequest<I>::send_blacklist() {
158   if (!m_blacklist_locker) {
159     send_break_lock();
160     return;
161   }
162
163   entity_name_t entity_name = entity_name_t::CLIENT(m_ioctx.get_instance_id());
164   ldout(m_cct, 10) << "local entity=" << entity_name << ", "
165                    << "locker entity=" << m_locker.entity << dendl;
166
167   if (m_locker.entity == entity_name) {
168     lderr(m_cct) << "attempting to self-blacklist" << dendl;
169     finish(-EINVAL);
170     return;
171   }
172
173   // TODO: need async version of RadosClient::blacklist_add
174   using klass = BreakRequest<I>;
175   Context *ctx = create_context_callback<klass, &klass::handle_blacklist>(
176     this);
177   m_work_queue->queue(new C_BlacklistClient(m_ioctx, m_locker.address,
178                                             m_blacklist_expire_seconds, ctx),
179                       0);
180 }
181
182 template <typename I>
183 void BreakRequest<I>::handle_blacklist(int r) {
184   ldout(m_cct, 10) << "r=" << r << dendl;
185
186   if (r < 0) {
187     lderr(m_cct) << "failed to blacklist lock owner: " << cpp_strerror(r)
188                  << dendl;
189     finish(r);
190     return;
191   }
192   send_break_lock();
193 }
194
195 template <typename I>
196 void BreakRequest<I>::send_break_lock() {
197   ldout(m_cct, 10) << dendl;
198
199   librados::ObjectWriteOperation op;
200   rados::cls::lock::break_lock(&op, RBD_LOCK_NAME, m_locker.cookie,
201                                m_locker.entity);
202
203   using klass = BreakRequest<I>;
204   librados::AioCompletion *rados_completion =
205     create_rados_callback<klass, &klass::handle_break_lock>(this);
206   int r = m_ioctx.aio_operate(m_oid, rados_completion, &op);
207   assert(r == 0);
208   rados_completion->release();
209 }
210
211 template <typename I>
212 void BreakRequest<I>::handle_break_lock(int r) {
213   ldout(m_cct, 10) << "r=" << r << dendl;
214
215   if (r < 0 && r != -ENOENT) {
216     lderr(m_cct) << "failed to break lock: " << cpp_strerror(r) << dendl;
217     finish(r);
218     return;
219   }
220
221   finish(0);
222 }
223
224 template <typename I>
225 void BreakRequest<I>::finish(int r) {
226   ldout(m_cct, 10) << "r=" << r << dendl;
227
228   m_on_finish->complete(r);
229   delete this;
230 }
231
232 } // namespace managed_lock
233 } // namespace librbd
234
235 template class librbd::managed_lock::BreakRequest<librbd::ImageCtx>;