summaryrefslogtreecommitdiffstats
path: root/src/librbd/ExclusiveLock.cc
blob: bc148b1f5cd485a8e0aa93b5c1147986db90745f (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "librbd/cache/Utils.h"
#include "librbd/ExclusiveLock.h"
#include "librbd/ImageCtx.h"
#include "librbd/ImageWatcher.h"
#include "librbd/ImageState.h"
#include "librbd/exclusive_lock/ImageDispatch.h"
#include "librbd/exclusive_lock/PreAcquireRequest.h"
#include "librbd/exclusive_lock/PostAcquireRequest.h"
#include "librbd/exclusive_lock/PreReleaseRequest.h"
#include "librbd/io/ImageDispatcherInterface.h"
#include "librbd/Utils.h"
#include "librbd/asio/ContextWQ.h"
#include "common/ceph_mutex.h"
#include "common/dout.h"

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

namespace librbd {

using namespace exclusive_lock;
using librbd::util::create_context_callback;

template <typename I>
using ML = ManagedLock<I>;

template <typename I>
ExclusiveLock<I>::ExclusiveLock(I &image_ctx)
  : RefCountedObject(image_ctx.cct),
    ML<I>(image_ctx.md_ctx, *image_ctx.asio_engine, image_ctx.header_oid,
          image_ctx.image_watcher, managed_lock::EXCLUSIVE,
          image_ctx.config.template get_val<bool>("rbd_blocklist_on_break_lock"),
          image_ctx.config.template get_val<uint64_t>("rbd_blocklist_expire_seconds")),
    m_image_ctx(image_ctx) {
  std::lock_guard locker{ML<I>::m_lock};
  ML<I>::set_state_uninitialized();
}

template <typename I>
bool ExclusiveLock<I>::accept_request(OperationRequestType request_type,
                                      int *ret_val) const {
  std::lock_guard locker{ML<I>::m_lock};

  bool accept_request =
    (!ML<I>::is_state_shutdown() && ML<I>::is_state_locked() &&
     (m_request_blocked_count == 0 ||
      m_image_ctx.get_exclusive_lock_policy()->accept_blocked_request(
        request_type)));
  if (ret_val != nullptr) {
    *ret_val = accept_request ? 0 : m_request_blocked_ret_val;
  }

  ldout(m_image_ctx.cct, 20) << "=" << accept_request << " (request_type="
                             << request_type << ")" << dendl;
  return accept_request;
}

template <typename I>
bool ExclusiveLock<I>::accept_ops() const {
  std::lock_guard locker{ML<I>::m_lock};
  bool accept = accept_ops(ML<I>::m_lock);
  ldout(m_image_ctx.cct, 20) << "=" << accept << dendl;
  return accept;
}

template <typename I>
bool ExclusiveLock<I>::accept_ops(const ceph::mutex &lock) const {
  return (!ML<I>::is_state_shutdown() &&
          (ML<I>::is_state_locked() || ML<I>::is_state_post_acquiring()));
}

template <typename I>
void ExclusiveLock<I>::set_require_lock(bool init_shutdown,
                                        io::Direction direction,
                                        Context* on_finish) {
  m_image_dispatch->set_require_lock(init_shutdown, direction, on_finish);
}

template <typename I>
void ExclusiveLock<I>::unset_require_lock(io::Direction direction) {
  m_image_dispatch->unset_require_lock(direction);
}

template <typename I>
void ExclusiveLock<I>::block_requests(int r) {
  std::lock_guard locker{ML<I>::m_lock};

  m_request_blocked_count++;
  if (m_request_blocked_ret_val == 0) {
    m_request_blocked_ret_val = r;
  }

  ldout(m_image_ctx.cct, 20) << ": r=" << r << dendl;
}

template <typename I>
void ExclusiveLock<I>::unblock_requests() {
  std::lock_guard locker{ML<I>::m_lock};

  ceph_assert(m_request_blocked_count > 0);
  m_request_blocked_count--;
  if (m_request_blocked_count == 0) {
    m_request_blocked_ret_val = 0;
  }

  ldout(m_image_ctx.cct, 20) << dendl;
}

template <typename I>
int ExclusiveLock<I>::get_unlocked_op_error() const {
  if (m_image_ctx.image_watcher->is_blocklisted()) {
    return -EBLOCKLISTED;
  }
  return -EROFS;
}

template <typename I>
void ExclusiveLock<I>::init(uint64_t features, Context *on_init) {
  ceph_assert(ceph_mutex_is_locked(m_image_ctx.owner_lock));

  on_init = create_context_callback<Context>(on_init, this);

  ldout(m_image_ctx.cct, 10) << ": features=" << features << dendl;

  {
    std::lock_guard locker{ML<I>::m_lock};
    ML<I>::set_state_initializing();
  }

  m_image_dispatch = exclusive_lock::ImageDispatch<I>::create(&m_image_ctx);
  m_image_ctx.io_image_dispatcher->register_dispatch(m_image_dispatch);

  on_init = new LambdaContext([this, on_init](int r) {
      {
        std::lock_guard locker{ML<I>::m_lock};
        ML<I>::set_state_unlocked();
      }

      on_init->complete(r);
    });

  bool pwl_enabled = cache::util::is_pwl_enabled(m_image_ctx);
  if (m_image_ctx.clone_copy_on_read ||
      (features & RBD_FEATURE_JOURNALING) != 0 ||
      pwl_enabled) {
    m_image_dispatch->set_require_lock(true, io::DIRECTION_BOTH, on_init);
  } else {
    m_image_dispatch->set_require_lock(true, io::DIRECTION_WRITE, on_init);
  }
}

template <typename I>
void ExclusiveLock<I>::shut_down(Context *on_shut_down) {
  ldout(m_image_ctx.cct, 10) << dendl;

  auto ref = ceph::ref_t<ExclusiveLock<I>>(this);
  on_shut_down = create_context_callback<Context>(on_shut_down, this);

  ML<I>::shut_down(on_shut_down);

  // if stalled in request state machine -- abort
  handle_peer_notification(0);
}

template <typename I>
void ExclusiveLock<I>::handle_peer_notification(int r) {
  std::lock_guard locker{ML<I>::m_lock};
  if (!ML<I>::is_state_waiting_for_lock()) {
    return;
  }

  ldout(m_image_ctx.cct, 10) << dendl;
  ceph_assert(ML<I>::is_action_acquire_lock());

  m_acquire_lock_peer_ret_val = r;
  ML<I>::execute_next_action();
}

template <typename I>
Context *ExclusiveLock<I>::start_op(int* ret_val) {
  ceph_assert(ceph_mutex_is_locked(m_image_ctx.owner_lock));
  std::lock_guard locker{ML<I>::m_lock};

  if (!accept_ops(ML<I>::m_lock)) {
    *ret_val = get_unlocked_op_error();
    return nullptr;
  }

  m_async_op_tracker.start_op();
  return new LambdaContext([this](int r) {
      m_async_op_tracker.finish_op();
    });
}

template <typename I>
void ExclusiveLock<I>::shutdown_handler(int r, Context *on_finish) {
  ldout(m_image_ctx.cct, 10) << dendl;

  {
    std::unique_lock owner_locker{m_image_ctx.owner_lock};
    m_image_ctx.exclusive_lock = nullptr;
  }

  on_finish = new LambdaContext([this, on_finish](int r) {
      m_image_dispatch = nullptr;
      m_image_ctx.image_watcher->flush(on_finish);
    });
  m_image_ctx.io_image_dispatcher->shut_down_dispatch(
    m_image_dispatch->get_dispatch_layer(), on_finish);
}

template <typename I>
void ExclusiveLock<I>::pre_acquire_lock_handler(Context *on_finish) {
  ldout(m_image_ctx.cct, 10) << dendl;

  int acquire_lock_peer_ret_val = 0;
  {
    std::lock_guard locker{ML<I>::m_lock};
    std::swap(acquire_lock_peer_ret_val, m_acquire_lock_peer_ret_val);
  }

  if (acquire_lock_peer_ret_val == -EROFS) {
    ldout(m_image_ctx.cct, 10) << ": peer nacked lock request" << dendl;
    on_finish->complete(acquire_lock_peer_ret_val);
    return;
  }

  PreAcquireRequest<I> *req = PreAcquireRequest<I>::create(m_image_ctx,
                                                           on_finish);
  m_image_ctx.op_work_queue->queue(new LambdaContext([req](int r) {
    req->send();
  }));
}

template <typename I>
void ExclusiveLock<I>::post_acquire_lock_handler(int r, Context *on_finish) {
  ldout(m_image_ctx.cct, 10) << ": r=" << r << dendl;

  if (r == -EROFS) {
    // peer refused to release the exclusive lock
    on_finish->complete(r);
    return;
  } else if (r < 0) {
    ML<I>::m_lock.lock();
    ceph_assert(ML<I>::is_state_acquiring());

    // PostAcquire state machine will not run, so we need complete prepare
    m_image_ctx.state->handle_prepare_lock_complete();

    // if lock is in-use by another client, request the lock
    if (ML<I>::is_action_acquire_lock() && (r == -EBUSY || r == -EAGAIN)) {
      ML<I>::set_state_waiting_for_lock();
      ML<I>::m_lock.unlock();

      // request the lock from a peer
      m_image_ctx.image_watcher->notify_request_lock();

      // inform manage lock that we have interrupted the state machine
      r = -ECANCELED;
    } else {
      ML<I>::m_lock.unlock();

      // clear error if peer owns lock
      if (r == -EAGAIN) {
        r = 0;
      }
    }

    on_finish->complete(r);
    return;
  }

  std::lock_guard locker{ML<I>::m_lock};
  m_pre_post_callback = on_finish;
  using EL = ExclusiveLock<I>;
  PostAcquireRequest<I> *req = PostAcquireRequest<I>::create(m_image_ctx,
      util::create_context_callback<EL, &EL::handle_post_acquiring_lock>(this),
      util::create_context_callback<EL, &EL::handle_post_acquired_lock>(this));

  m_image_ctx.op_work_queue->queue(new LambdaContext([req](int r) {
    req->send();
  }));
}

template <typename I>
void ExclusiveLock<I>::handle_post_acquiring_lock(int r) {
  ldout(m_image_ctx.cct, 10) << dendl;

  std::lock_guard locker{ML<I>::m_lock};

  ceph_assert(r == 0);

  // lock is owned at this point
  ML<I>::set_state_post_acquiring();
}

template <typename I>
void ExclusiveLock<I>::handle_post_acquired_lock(int r) {
  ldout(m_image_ctx.cct, 10) << ": r=" << r << dendl;

  Context *on_finish = nullptr;
  {
    std::lock_guard locker{ML<I>::m_lock};
    ceph_assert(ML<I>::is_state_acquiring() ||
                ML<I>::is_state_post_acquiring());

    assert (m_pre_post_callback != nullptr);
    std::swap(m_pre_post_callback, on_finish);
  }

  if (r < 0) {
    on_finish->complete(r);
    return;
  }

  m_image_ctx.perfcounter->tset(l_librbd_lock_acquired_time,
                                ceph_clock_now());
  m_image_ctx.image_watcher->notify_acquired_lock();
  m_image_dispatch->unset_require_lock(io::DIRECTION_BOTH);

  on_finish->complete(0);
}

template <typename I>
void ExclusiveLock<I>::pre_release_lock_handler(bool shutting_down,
                                                Context *on_finish) {
  ldout(m_image_ctx.cct, 10) << dendl;
  std::lock_guard locker{ML<I>::m_lock};

  auto req = PreReleaseRequest<I>::create(
    m_image_ctx, m_image_dispatch, shutting_down, m_async_op_tracker,
    on_finish);
  m_image_ctx.op_work_queue->queue(new LambdaContext([req](int r) {
    req->send();
  }));
}

template <typename I>
void ExclusiveLock<I>::post_release_lock_handler(bool shutting_down, int r,
                                                 Context *on_finish) {
  ldout(m_image_ctx.cct, 10) << ": r=" << r << " shutting_down="
                             << shutting_down << dendl;
  if (!shutting_down) {
    {
      std::lock_guard locker{ML<I>::m_lock};
      ceph_assert(ML<I>::is_state_pre_releasing() ||
                  ML<I>::is_state_releasing());
    }

    if (r >= 0) {
      m_image_ctx.image_watcher->notify_released_lock();
    }

    on_finish->complete(r);
  } else {
    {
      std::unique_lock owner_locker{m_image_ctx.owner_lock};
      m_image_ctx.exclusive_lock = nullptr;
    }

    on_finish = new LambdaContext([this, r, on_finish](int) {
        m_image_dispatch = nullptr;
        m_image_ctx.image_watcher->notify_released_lock();
        on_finish->complete(r);
      });
    m_image_ctx.io_image_dispatcher->shut_down_dispatch(
      m_image_dispatch->get_dispatch_layer(), on_finish);
  }
}

template <typename I>
void ExclusiveLock<I>::post_reacquire_lock_handler(int r, Context *on_finish) {
  ldout(m_image_ctx.cct, 10) << dendl;
  if (r >= 0) {
    m_image_ctx.image_watcher->notify_acquired_lock();
  }

  on_finish->complete(r);
}

} // namespace librbd

template class librbd::ExclusiveLock<librbd::ImageCtx>;