Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / librbd / managed_lock / GetLockerRequest.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/GetLockerRequest.h"
5 #include "cls/lock/cls_lock_client.h"
6 #include "cls/lock/cls_lock_types.h"
7 #include "common/dout.h"
8 #include "common/errno.h"
9 #include "include/stringify.h"
10 #include "librbd/ImageCtx.h"
11 #include "librbd/Utils.h"
12 #include "librbd/managed_lock/Types.h"
13 #include "librbd/managed_lock/Utils.h"
14
15 #define dout_subsys ceph_subsys_rbd
16 #undef dout_prefix
17 #define dout_prefix *_dout << "librbd::managed_lock::GetLockerRequest: " \
18                            << this << " " << __func__ << ": "
19
20 namespace librbd {
21 namespace managed_lock {
22
23 using librbd::util::create_rados_callback;
24
25 template <typename I>
26 GetLockerRequest<I>::GetLockerRequest(librados::IoCtx& ioctx,
27                                       const std::string& oid, bool exclusive,
28                                       Locker *locker, Context *on_finish)
29   : m_ioctx(ioctx), m_cct(reinterpret_cast<CephContext *>(m_ioctx.cct())),
30     m_oid(oid), m_exclusive(exclusive), m_locker(locker),
31     m_on_finish(on_finish) {
32 }
33
34 template <typename I>
35 void GetLockerRequest<I>::send() {
36   send_get_lockers();
37 }
38
39 template <typename I>
40 void GetLockerRequest<I>::send_get_lockers() {
41   ldout(m_cct, 10) << dendl;
42
43   librados::ObjectReadOperation op;
44   rados::cls::lock::get_lock_info_start(&op, RBD_LOCK_NAME);
45
46   using klass = GetLockerRequest<I>;
47   librados::AioCompletion *rados_completion =
48     create_rados_callback<klass, &klass::handle_get_lockers>(this);
49   m_out_bl.clear();
50   int r = m_ioctx.aio_operate(m_oid, rados_completion, &op, &m_out_bl);
51   assert(r == 0);
52   rados_completion->release();
53 }
54
55 template <typename I>
56 void GetLockerRequest<I>::handle_get_lockers(int r) {
57   ldout(m_cct, 10) << "r=" << r << dendl;
58
59   std::map<rados::cls::lock::locker_id_t,
60            rados::cls::lock::locker_info_t> lockers;
61   ClsLockType lock_type = LOCK_NONE;
62   std::string lock_tag;
63   if (r == 0) {
64     bufferlist::iterator it = m_out_bl.begin();
65     r = rados::cls::lock::get_lock_info_finish(&it, &lockers, &lock_type,
66                                                &lock_tag);
67   }
68
69   if (r < 0) {
70     lderr(m_cct) << "failed to retrieve lockers: " << cpp_strerror(r) << dendl;
71     finish(r);
72     return;
73   }
74
75   if (lockers.empty()) {
76     ldout(m_cct, 20) << "no lockers detected" << dendl;
77     finish(-ENOENT);
78     return;
79   }
80
81   if (lock_tag != util::get_watcher_lock_tag()) {
82     ldout(m_cct, 5) <<"locked by external mechanism: tag=" << lock_tag << dendl;
83     finish(-EBUSY);
84     return;
85   }
86
87   if (m_exclusive && lock_type == LOCK_SHARED) {
88     ldout(m_cct, 5) << "incompatible shared lock type detected" << dendl;
89     finish(-EBUSY);
90     return;
91   } else if (!m_exclusive && lock_type == LOCK_EXCLUSIVE) {
92     ldout(m_cct, 5) << "incompatible exclusive lock type detected" << dendl;
93     finish(-EBUSY);
94     return;
95   }
96
97   std::map<rados::cls::lock::locker_id_t,
98            rados::cls::lock::locker_info_t>::iterator iter = lockers.begin();
99   if (!util::decode_lock_cookie(iter->first.cookie, &m_locker->handle)) {
100     ldout(m_cct, 5) << "locked by external mechanism: "
101                     << "cookie=" << iter->first.cookie << dendl;
102     finish(-EBUSY);
103     return;
104   }
105
106   m_locker->entity = iter->first.locker;
107   m_locker->cookie = iter->first.cookie;
108   m_locker->address = stringify(iter->second.addr);
109   if (m_locker->cookie.empty() || m_locker->address.empty()) {
110     ldout(m_cct, 20) << "no valid lockers detected" << dendl;
111     finish(-ENOENT);
112     return;
113   }
114
115   ldout(m_cct, 10) << "retrieved exclusive locker: "
116                  << m_locker->entity << "@" << m_locker->address << dendl;
117   finish(0);
118 }
119
120 template <typename I>
121 void GetLockerRequest<I>::finish(int r) {
122   ldout(m_cct, 10) << "r=" << r << dendl;
123
124   m_on_finish->complete(r);
125   delete this;
126 }
127
128 } // namespace managed_lock
129 } // namespace librbd
130
131 template class librbd::managed_lock::GetLockerRequest<librbd::ImageCtx>;