summaryrefslogtreecommitdiffstats
path: root/src/librbd/managed_lock/BreakRequest.cc
blob: e482d221e6fccf1e04a7fa433ee3cc4cecf71dfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "librbd/managed_lock/BreakRequest.h"
#include "common/dout.h"
#include "common/errno.h"
#include "include/neorados/RADOS.hpp"
#include "include/stringify.h"
#include "cls/lock/cls_lock_client.h"
#include "cls/lock/cls_lock_types.h"
#include "librbd/AsioEngine.h"
#include "librbd/ImageCtx.h"
#include "librbd/Utils.h"
#include "librbd/asio/ContextWQ.h"
#include "librbd/asio/Utils.h"
#include "librbd/managed_lock/GetLockerRequest.h"

#define dout_subsys ceph_subsys_rbd
#undef dout_prefix
#define dout_prefix *_dout << "librbd::managed_lock::BreakRequest: " << this \
                           << " " << __func__ << ": "

namespace librbd {
namespace managed_lock {

using util::create_context_callback;
using util::create_rados_callback;

template <typename I>
BreakRequest<I>::BreakRequest(librados::IoCtx& ioctx,
                              AsioEngine& asio_engine,
                              const std::string& oid, const Locker &locker,
                              bool exclusive, bool blocklist_locker,
                              uint32_t blocklist_expire_seconds,
                              bool force_break_lock, Context *on_finish)
  : m_ioctx(ioctx), m_cct(reinterpret_cast<CephContext *>(m_ioctx.cct())),
    m_asio_engine(asio_engine), m_oid(oid), m_locker(locker),
    m_exclusive(exclusive), m_blocklist_locker(blocklist_locker),
    m_blocklist_expire_seconds(blocklist_expire_seconds),
    m_force_break_lock(force_break_lock), m_on_finish(on_finish) {
}

template <typename I>
void BreakRequest<I>::send() {
  send_get_watchers();
}

template <typename I>
void BreakRequest<I>::send_get_watchers() {
  ldout(m_cct, 10) << dendl;

  librados::ObjectReadOperation op;
  op.list_watchers(&m_watchers, &m_watchers_ret_val);

  using klass = BreakRequest<I>;
  librados::AioCompletion *rados_completion =
    create_rados_callback<klass, &klass::handle_get_watchers>(this);
  m_out_bl.clear();
  int r = m_ioctx.aio_operate(m_oid, rados_completion, &op, &m_out_bl);
  ceph_assert(r == 0);
  rados_completion->release();
}

template <typename I>
void BreakRequest<I>::handle_get_watchers(int r) {
  ldout(m_cct, 10) << "r=" << r << dendl;

  if (r == 0) {
    r = m_watchers_ret_val;
  }
  if (r < 0) {
    lderr(m_cct) << "failed to retrieve watchers: " << cpp_strerror(r)
                 << dendl;
    finish(r);
    return;
  }

  bool found_alive_locker = false;
  for (auto &watcher : m_watchers) {
    ldout(m_cct, 20) << "watcher=["
                     << "addr=" << watcher.addr << ", "
                     << "entity=client." << watcher.watcher_id << "]" << dendl;

    if ((strncmp(m_locker.address.c_str(),
                 watcher.addr, sizeof(watcher.addr)) == 0) &&
        (m_locker.handle == watcher.cookie)) {
      ldout(m_cct, 10) << "lock owner is still alive" << dendl;
      found_alive_locker = true;
    }
  }

  if (!m_force_break_lock && found_alive_locker) {
    finish(-EAGAIN);
    return;
  }

  send_get_locker();
}

template <typename I>
void BreakRequest<I>::send_get_locker() {
  ldout(m_cct, 10) << dendl;

  using klass = BreakRequest<I>;
  Context *ctx = create_context_callback<klass, &klass::handle_get_locker>(
    this);
  auto req = GetLockerRequest<I>::create(m_ioctx, m_oid, m_exclusive,
                                         &m_refreshed_locker, ctx);
  req->send();
}

template <typename I>
void BreakRequest<I>::handle_get_locker(int r) {
  ldout(m_cct, 10) << "r=" << r << dendl;

  if (r == -ENOENT) {
    ldout(m_cct, 5) << "no lock owner" << dendl;
    finish(0);
    return;
  } else if (r < 0 && r != -EBUSY) {
    lderr(m_cct) << "failed to retrieve lockers: " << cpp_strerror(r) << dendl;
    finish(r);
    return;
  } else if (r < 0) {
    m_refreshed_locker = {};
  }

  if (m_refreshed_locker != m_locker || m_refreshed_locker == Locker{}) {
    ldout(m_cct, 5) << "no longer lock owner" << dendl;
    finish(-EAGAIN);
    return;
  }

  send_blocklist();
}

template <typename I>
void BreakRequest<I>::send_blocklist() {
  if (!m_blocklist_locker) {
    send_break_lock();
    return;
  }

  entity_name_t entity_name = entity_name_t::CLIENT(m_ioctx.get_instance_id());
  ldout(m_cct, 10) << "local entity=" << entity_name << ", "
                   << "locker entity=" << m_locker.entity << dendl;

  if (m_locker.entity == entity_name) {
    lderr(m_cct) << "attempting to self-blocklist" << dendl;
    finish(-EINVAL);
    return;
  }

  entity_addr_t locker_addr;
  if (!locker_addr.parse(m_locker.address.c_str(), 0)) {
    lderr(m_cct) << "unable to parse locker address: " << m_locker.address
                 << dendl;
    finish(-EINVAL);
    return;
  }

  std::optional<std::chrono::seconds> expire;
  if (m_blocklist_expire_seconds != 0) {
    expire = std::chrono::seconds(m_blocklist_expire_seconds);
  }
  m_asio_engine.get_rados_api().blocklist_add(
    m_locker.address, expire,
    librbd::asio::util::get_callback_adapter(
      [this](int r) { handle_blocklist(r); }));
}

template <typename I>
void BreakRequest<I>::handle_blocklist(int r) {
  ldout(m_cct, 10) << "r=" << r << dendl;

  if (r < 0) {
    lderr(m_cct) << "failed to blocklist lock owner: " << cpp_strerror(r)
                 << dendl;
    finish(r);
    return;
  }

  wait_for_osd_map();
}

template <typename I>
void BreakRequest<I>::wait_for_osd_map() {
  ldout(m_cct, 10) << dendl;

  m_asio_engine.get_rados_api().wait_for_latest_osd_map(
    librbd::asio::util::get_callback_adapter(
      [this](int r) { handle_wait_for_osd_map(r); }));
}

template <typename I>
void BreakRequest<I>::handle_wait_for_osd_map(int r) {
  ldout(m_cct, 10) << "r=" << r << dendl;

  if (r < 0) {
    lderr(m_cct) << "failed to wait for updated OSD map: " << cpp_strerror(r)
                 << dendl;
    finish(r);
    return;
  }

  send_break_lock();
}

template <typename I>
void BreakRequest<I>::send_break_lock() {
  ldout(m_cct, 10) << dendl;

  librados::ObjectWriteOperation op;
  rados::cls::lock::break_lock(&op, RBD_LOCK_NAME, m_locker.cookie,
                               m_locker.entity);

  using klass = BreakRequest<I>;
  librados::AioCompletion *rados_completion =
    create_rados_callback<klass, &klass::handle_break_lock>(this);
  int r = m_ioctx.aio_operate(m_oid, rados_completion, &op);
  ceph_assert(r == 0);
  rados_completion->release();
}

template <typename I>
void BreakRequest<I>::handle_break_lock(int r) {
  ldout(m_cct, 10) << "r=" << r << dendl;

  if (r < 0 && r != -ENOENT) {
    lderr(m_cct) << "failed to break lock: " << cpp_strerror(r) << dendl;
    finish(r);
    return;
  }

  finish(0);
}

template <typename I>
void BreakRequest<I>::finish(int r) {
  ldout(m_cct, 10) << "r=" << r << dendl;

  m_on_finish->complete(r);
  delete this;
}

} // namespace managed_lock
} // namespace librbd

template class librbd::managed_lock::BreakRequest<librbd::ImageCtx>;