Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / librbd / exclusive_lock / PreAcquireRequest.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/exclusive_lock/PreAcquireRequest.h"
5 #include "librbd/Utils.h"
6 #include "common/dout.h"
7 #include "common/errno.h"
8 #include "librbd/ImageCtx.h"
9 #include "librbd/ImageWatcher.h"
10 #include "librbd/ImageState.h"
11
12 #define dout_subsys ceph_subsys_rbd
13 #undef dout_prefix
14 #define dout_prefix *_dout << "librbd::exclusive_lock::PreAcquireRequest: " \
15                            << this << " " << __func__ << ": "
16
17 namespace librbd {
18 namespace exclusive_lock {
19
20 using util::create_async_context_callback;
21 using util::create_context_callback;
22 using util::create_rados_callback;
23
24 template <typename I>
25 PreAcquireRequest<I>* PreAcquireRequest<I>::create(I &image_ctx,
26                                                    Context *on_finish) {
27   return new PreAcquireRequest(image_ctx, on_finish);
28 }
29
30 template <typename I>
31 PreAcquireRequest<I>::PreAcquireRequest(I &image_ctx, Context *on_finish)
32   : m_image_ctx(image_ctx),
33     m_on_finish(create_async_context_callback(image_ctx, on_finish)),
34     m_error_result(0) {
35 }
36
37 template <typename I>
38 PreAcquireRequest<I>::~PreAcquireRequest() {
39 }
40
41 template <typename I>
42 void PreAcquireRequest<I>::send() {
43   send_prepare_lock();
44 }
45
46 template <typename I>
47 void PreAcquireRequest<I>::send_prepare_lock() {
48   CephContext *cct = m_image_ctx.cct;
49   ldout(cct, 10) << dendl;
50
51   // acquire the lock if the image is not busy performing other actions
52   Context *ctx = create_context_callback<
53     PreAcquireRequest<I>, &PreAcquireRequest<I>::handle_prepare_lock>(this);
54   m_image_ctx.state->prepare_lock(ctx);
55 }
56
57 template <typename I>
58 void PreAcquireRequest<I>::handle_prepare_lock(int r) {
59   CephContext *cct = m_image_ctx.cct;
60   ldout(cct, 10) << "r=" << r << dendl;
61
62   send_flush_notifies();
63 }
64
65 template <typename I>
66 void PreAcquireRequest<I>::send_flush_notifies() {
67   CephContext *cct = m_image_ctx.cct;
68   ldout(cct, 10) << dendl;
69
70   using klass = PreAcquireRequest<I>;
71   Context *ctx = create_context_callback<klass, &klass::handle_flush_notifies>(
72     this);
73   m_image_ctx.image_watcher->flush(ctx);
74 }
75
76 template <typename I>
77 void PreAcquireRequest<I>::handle_flush_notifies(int r) {
78   CephContext *cct = m_image_ctx.cct;
79   ldout(cct, 10) << dendl;
80
81   assert(r == 0);
82   finish();
83 }
84
85 template <typename I>
86 void PreAcquireRequest<I>::finish() {
87   m_on_finish->complete(m_error_result);
88   delete this;
89 }
90
91 } // namespace exclusive_lock
92 } // namespace librbd
93
94 template class librbd::exclusive_lock::PreAcquireRequest<librbd::ImageCtx>;