1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
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"
15 #define dout_subsys ceph_subsys_rbd
17 #define dout_prefix *_dout << "librbd::managed_lock::GetLockerRequest: " \
18 << this << " " << __func__ << ": "
21 namespace managed_lock {
23 using librbd::util::create_rados_callback;
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) {
35 void GetLockerRequest<I>::send() {
40 void GetLockerRequest<I>::send_get_lockers() {
41 ldout(m_cct, 10) << dendl;
43 librados::ObjectReadOperation op;
44 rados::cls::lock::get_lock_info_start(&op, RBD_LOCK_NAME);
46 using klass = GetLockerRequest<I>;
47 librados::AioCompletion *rados_completion =
48 create_rados_callback<klass, &klass::handle_get_lockers>(this);
50 int r = m_ioctx.aio_operate(m_oid, rados_completion, &op, &m_out_bl);
52 rados_completion->release();
56 void GetLockerRequest<I>::handle_get_lockers(int r) {
57 ldout(m_cct, 10) << "r=" << r << dendl;
59 std::map<rados::cls::lock::locker_id_t,
60 rados::cls::lock::locker_info_t> lockers;
61 ClsLockType lock_type = LOCK_NONE;
64 bufferlist::iterator it = m_out_bl.begin();
65 r = rados::cls::lock::get_lock_info_finish(&it, &lockers, &lock_type,
70 lderr(m_cct) << "failed to retrieve lockers: " << cpp_strerror(r) << dendl;
75 if (lockers.empty()) {
76 ldout(m_cct, 20) << "no lockers detected" << dendl;
81 if (lock_tag != util::get_watcher_lock_tag()) {
82 ldout(m_cct, 5) <<"locked by external mechanism: tag=" << lock_tag << dendl;
87 if (m_exclusive && lock_type == LOCK_SHARED) {
88 ldout(m_cct, 5) << "incompatible shared lock type detected" << dendl;
91 } else if (!m_exclusive && lock_type == LOCK_EXCLUSIVE) {
92 ldout(m_cct, 5) << "incompatible exclusive lock type detected" << dendl;
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;
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;
115 ldout(m_cct, 10) << "retrieved exclusive locker: "
116 << m_locker->entity << "@" << m_locker->address << dendl;
120 template <typename I>
121 void GetLockerRequest<I>::finish(int r) {
122 ldout(m_cct, 10) << "r=" << r << dendl;
124 m_on_finish->complete(r);
128 } // namespace managed_lock
129 } // namespace librbd
131 template class librbd::managed_lock::GetLockerRequest<librbd::ImageCtx>;